netdev: ehea: bcmc_regs semaphore to mutex
[linux-2.6/kvm.git] / net / netfilter / nf_conntrack_h323_main.c
blob505052d495cfee3a1f2ab3b6c21be0c43fd3f0ad
1 /*
2 * H.323 connection tracking helper
4 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
6 * This source code is licensed under General Public License version 2.
8 * Based on the 'brute force' H.323 connection tracking module by
9 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
11 * For more information, please see http://nath323.sourceforge.net/
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ctype.h>
17 #include <linux/inet.h>
18 #include <linux/in.h>
19 #include <linux/ip.h>
20 #include <linux/udp.h>
21 #include <linux/tcp.h>
22 #include <linux/skbuff.h>
23 #include <net/route.h>
24 #include <net/ip6_route.h>
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_tuple.h>
29 #include <net/netfilter/nf_conntrack_expect.h>
30 #include <net/netfilter/nf_conntrack_ecache.h>
31 #include <net/netfilter/nf_conntrack_helper.h>
32 #include <linux/netfilter/nf_conntrack_h323.h>
34 /* Parameters */
35 static unsigned int default_rrq_ttl __read_mostly = 300;
36 module_param(default_rrq_ttl, uint, 0600);
37 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
39 static int gkrouted_only __read_mostly = 1;
40 module_param(gkrouted_only, int, 0600);
41 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
43 static int callforward_filter __read_mostly = 1;
44 module_param(callforward_filter, bool, 0600);
45 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
46 "if both endpoints are on different sides "
47 "(determined by routing information)");
49 /* Hooks for NAT */
50 int (*set_h245_addr_hook) (struct sk_buff *skb,
51 unsigned char **data, int dataoff,
52 H245_TransportAddress *taddr,
53 union nf_inet_addr *addr, __be16 port)
54 __read_mostly;
55 int (*set_h225_addr_hook) (struct sk_buff *skb,
56 unsigned char **data, int dataoff,
57 TransportAddress *taddr,
58 union nf_inet_addr *addr, __be16 port)
59 __read_mostly;
60 int (*set_sig_addr_hook) (struct sk_buff *skb,
61 struct nf_conn *ct,
62 enum ip_conntrack_info ctinfo,
63 unsigned char **data,
64 TransportAddress *taddr, int count) __read_mostly;
65 int (*set_ras_addr_hook) (struct sk_buff *skb,
66 struct nf_conn *ct,
67 enum ip_conntrack_info ctinfo,
68 unsigned char **data,
69 TransportAddress *taddr, int count) __read_mostly;
70 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
71 struct nf_conn *ct,
72 enum ip_conntrack_info ctinfo,
73 unsigned char **data, int dataoff,
74 H245_TransportAddress *taddr,
75 __be16 port, __be16 rtp_port,
76 struct nf_conntrack_expect *rtp_exp,
77 struct nf_conntrack_expect *rtcp_exp) __read_mostly;
78 int (*nat_t120_hook) (struct sk_buff *skb,
79 struct nf_conn *ct,
80 enum ip_conntrack_info ctinfo,
81 unsigned char **data, int dataoff,
82 H245_TransportAddress *taddr, __be16 port,
83 struct nf_conntrack_expect *exp) __read_mostly;
84 int (*nat_h245_hook) (struct sk_buff *skb,
85 struct nf_conn *ct,
86 enum ip_conntrack_info ctinfo,
87 unsigned char **data, int dataoff,
88 TransportAddress *taddr, __be16 port,
89 struct nf_conntrack_expect *exp) __read_mostly;
90 int (*nat_callforwarding_hook) (struct sk_buff *skb,
91 struct nf_conn *ct,
92 enum ip_conntrack_info ctinfo,
93 unsigned char **data, int dataoff,
94 TransportAddress *taddr, __be16 port,
95 struct nf_conntrack_expect *exp) __read_mostly;
96 int (*nat_q931_hook) (struct sk_buff *skb,
97 struct nf_conn *ct,
98 enum ip_conntrack_info ctinfo,
99 unsigned char **data, TransportAddress *taddr, int idx,
100 __be16 port, struct nf_conntrack_expect *exp)
101 __read_mostly;
103 static DEFINE_SPINLOCK(nf_h323_lock);
104 static char *h323_buffer;
106 static struct nf_conntrack_helper nf_conntrack_helper_h245;
107 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
108 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
110 /****************************************************************************/
111 static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,
112 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
113 unsigned char **data, int *datalen, int *dataoff)
115 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
116 int dir = CTINFO2DIR(ctinfo);
117 const struct tcphdr *th;
118 struct tcphdr _tcph;
119 int tcpdatalen;
120 int tcpdataoff;
121 unsigned char *tpkt;
122 int tpktlen;
123 int tpktoff;
125 /* Get TCP header */
126 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
127 if (th == NULL)
128 return 0;
130 /* Get TCP data offset */
131 tcpdataoff = protoff + th->doff * 4;
133 /* Get TCP data length */
134 tcpdatalen = skb->len - tcpdataoff;
135 if (tcpdatalen <= 0) /* No TCP data */
136 goto clear_out;
138 if (*data == NULL) { /* first TPKT */
139 /* Get first TPKT pointer */
140 tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
141 h323_buffer);
142 BUG_ON(tpkt == NULL);
144 /* Validate TPKT identifier */
145 if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) {
146 /* Netmeeting sends TPKT header and data separately */
147 if (info->tpkt_len[dir] > 0) {
148 pr_debug("nf_ct_h323: previous packet "
149 "indicated separate TPKT data of %hu "
150 "bytes\n", info->tpkt_len[dir]);
151 if (info->tpkt_len[dir] <= tcpdatalen) {
152 /* Yes, there was a TPKT header
153 * received */
154 *data = tpkt;
155 *datalen = info->tpkt_len[dir];
156 *dataoff = 0;
157 goto out;
160 /* Fragmented TPKT */
161 pr_debug("nf_ct_h323: fragmented TPKT\n");
162 goto clear_out;
165 /* It is not even a TPKT */
166 return 0;
168 tpktoff = 0;
169 } else { /* Next TPKT */
170 tpktoff = *dataoff + *datalen;
171 tcpdatalen -= tpktoff;
172 if (tcpdatalen <= 4) /* No more TPKT */
173 goto clear_out;
174 tpkt = *data + *datalen;
176 /* Validate TPKT identifier */
177 if (tpkt[0] != 0x03 || tpkt[1] != 0)
178 goto clear_out;
181 /* Validate TPKT length */
182 tpktlen = tpkt[2] * 256 + tpkt[3];
183 if (tpktlen < 4)
184 goto clear_out;
185 if (tpktlen > tcpdatalen) {
186 if (tcpdatalen == 4) { /* Separate TPKT header */
187 /* Netmeeting sends TPKT header and data separately */
188 pr_debug("nf_ct_h323: separate TPKT header indicates "
189 "there will be TPKT data of %hu bytes\n",
190 tpktlen - 4);
191 info->tpkt_len[dir] = tpktlen - 4;
192 return 0;
195 if (net_ratelimit())
196 printk("nf_ct_h323: incomplete TPKT (fragmented?)\n");
197 goto clear_out;
200 /* This is the encapsulated data */
201 *data = tpkt + 4;
202 *datalen = tpktlen - 4;
203 *dataoff = tpktoff + 4;
205 out:
206 /* Clear TPKT length */
207 info->tpkt_len[dir] = 0;
208 return 1;
210 clear_out:
211 info->tpkt_len[dir] = 0;
212 return 0;
215 /****************************************************************************/
216 static int get_h245_addr(struct nf_conn *ct, const unsigned char *data,
217 H245_TransportAddress *taddr,
218 union nf_inet_addr *addr, __be16 *port)
220 const unsigned char *p;
221 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
222 int len;
224 if (taddr->choice != eH245_TransportAddress_unicastAddress)
225 return 0;
227 switch (taddr->unicastAddress.choice) {
228 case eUnicastAddress_iPAddress:
229 if (family != AF_INET)
230 return 0;
231 p = data + taddr->unicastAddress.iPAddress.network;
232 len = 4;
233 break;
234 case eUnicastAddress_iP6Address:
235 if (family != AF_INET6)
236 return 0;
237 p = data + taddr->unicastAddress.iP6Address.network;
238 len = 16;
239 break;
240 default:
241 return 0;
244 memcpy(addr, p, len);
245 memset((void *)addr + len, 0, sizeof(*addr) - len);
246 memcpy(port, p + len, sizeof(__be16));
248 return 1;
251 /****************************************************************************/
252 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
253 enum ip_conntrack_info ctinfo,
254 unsigned char **data, int dataoff,
255 H245_TransportAddress *taddr)
257 int dir = CTINFO2DIR(ctinfo);
258 int ret = 0;
259 __be16 port;
260 __be16 rtp_port, rtcp_port;
261 union nf_inet_addr addr;
262 struct nf_conntrack_expect *rtp_exp;
263 struct nf_conntrack_expect *rtcp_exp;
264 typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
266 /* Read RTP or RTCP address */
267 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
268 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
269 port == 0)
270 return 0;
272 /* RTP port is even */
273 port &= htons(~1);
274 rtp_port = port;
275 rtcp_port = htons(ntohs(port) + 1);
277 /* Create expect for RTP */
278 if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
279 return -1;
280 nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
281 ct->tuplehash[!dir].tuple.src.l3num,
282 &ct->tuplehash[!dir].tuple.src.u3,
283 &ct->tuplehash[!dir].tuple.dst.u3,
284 IPPROTO_UDP, NULL, &rtp_port);
286 /* Create expect for RTCP */
287 if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
288 nf_ct_expect_put(rtp_exp);
289 return -1;
291 nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
292 ct->tuplehash[!dir].tuple.src.l3num,
293 &ct->tuplehash[!dir].tuple.src.u3,
294 &ct->tuplehash[!dir].tuple.dst.u3,
295 IPPROTO_UDP, NULL, &rtcp_port);
297 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
298 &ct->tuplehash[!dir].tuple.dst.u3,
299 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
300 (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
301 ct->status & IPS_NAT_MASK) {
302 /* NAT needed */
303 ret = nat_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
304 taddr, port, rtp_port, rtp_exp, rtcp_exp);
305 } else { /* Conntrack only */
306 if (nf_ct_expect_related(rtp_exp) == 0) {
307 if (nf_ct_expect_related(rtcp_exp) == 0) {
308 pr_debug("nf_ct_h323: expect RTP ");
309 NF_CT_DUMP_TUPLE(&rtp_exp->tuple);
310 pr_debug("nf_ct_h323: expect RTCP ");
311 NF_CT_DUMP_TUPLE(&rtcp_exp->tuple);
312 } else {
313 nf_ct_unexpect_related(rtp_exp);
314 ret = -1;
316 } else
317 ret = -1;
320 nf_ct_expect_put(rtp_exp);
321 nf_ct_expect_put(rtcp_exp);
323 return ret;
326 /****************************************************************************/
327 static int expect_t120(struct sk_buff *skb,
328 struct nf_conn *ct,
329 enum ip_conntrack_info ctinfo,
330 unsigned char **data, int dataoff,
331 H245_TransportAddress *taddr)
333 int dir = CTINFO2DIR(ctinfo);
334 int ret = 0;
335 __be16 port;
336 union nf_inet_addr addr;
337 struct nf_conntrack_expect *exp;
338 typeof(nat_t120_hook) nat_t120;
340 /* Read T.120 address */
341 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
342 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
343 port == 0)
344 return 0;
346 /* Create expect for T.120 connections */
347 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
348 return -1;
349 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
350 ct->tuplehash[!dir].tuple.src.l3num,
351 &ct->tuplehash[!dir].tuple.src.u3,
352 &ct->tuplehash[!dir].tuple.dst.u3,
353 IPPROTO_TCP, NULL, &port);
354 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple channels */
356 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
357 &ct->tuplehash[!dir].tuple.dst.u3,
358 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
359 (nat_t120 = rcu_dereference(nat_t120_hook)) &&
360 ct->status & IPS_NAT_MASK) {
361 /* NAT needed */
362 ret = nat_t120(skb, ct, ctinfo, data, dataoff, taddr,
363 port, exp);
364 } else { /* Conntrack only */
365 if (nf_ct_expect_related(exp) == 0) {
366 pr_debug("nf_ct_h323: expect T.120 ");
367 NF_CT_DUMP_TUPLE(&exp->tuple);
368 } else
369 ret = -1;
372 nf_ct_expect_put(exp);
374 return ret;
377 /****************************************************************************/
378 static int process_h245_channel(struct sk_buff *skb,
379 struct nf_conn *ct,
380 enum ip_conntrack_info ctinfo,
381 unsigned char **data, int dataoff,
382 H2250LogicalChannelParameters *channel)
384 int ret;
386 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
387 /* RTP */
388 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
389 &channel->mediaChannel);
390 if (ret < 0)
391 return -1;
394 if (channel->
395 options & eH2250LogicalChannelParameters_mediaControlChannel) {
396 /* RTCP */
397 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
398 &channel->mediaControlChannel);
399 if (ret < 0)
400 return -1;
403 return 0;
406 /****************************************************************************/
407 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
408 enum ip_conntrack_info ctinfo,
409 unsigned char **data, int dataoff,
410 OpenLogicalChannel *olc)
412 int ret;
414 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
416 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
417 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
419 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
420 &olc->
421 forwardLogicalChannelParameters.
422 multiplexParameters.
423 h2250LogicalChannelParameters);
424 if (ret < 0)
425 return -1;
428 if ((olc->options &
429 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
430 (olc->reverseLogicalChannelParameters.options &
431 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
432 && (olc->reverseLogicalChannelParameters.multiplexParameters.
433 choice ==
434 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
436 ret =
437 process_h245_channel(skb, ct, ctinfo, data, dataoff,
438 &olc->
439 reverseLogicalChannelParameters.
440 multiplexParameters.
441 h2250LogicalChannelParameters);
442 if (ret < 0)
443 return -1;
446 if ((olc->options & eOpenLogicalChannel_separateStack) &&
447 olc->forwardLogicalChannelParameters.dataType.choice ==
448 eDataType_data &&
449 olc->forwardLogicalChannelParameters.dataType.data.application.
450 choice == eDataApplicationCapability_application_t120 &&
451 olc->forwardLogicalChannelParameters.dataType.data.application.
452 t120.choice == eDataProtocolCapability_separateLANStack &&
453 olc->separateStack.networkAddress.choice ==
454 eNetworkAccessParameters_networkAddress_localAreaAddress) {
455 ret = expect_t120(skb, ct, ctinfo, data, dataoff,
456 &olc->separateStack.networkAddress.
457 localAreaAddress);
458 if (ret < 0)
459 return -1;
462 return 0;
465 /****************************************************************************/
466 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
467 enum ip_conntrack_info ctinfo,
468 unsigned char **data, int dataoff,
469 OpenLogicalChannelAck *olca)
471 H2250LogicalChannelAckParameters *ack;
472 int ret;
474 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
476 if ((olca->options &
477 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
478 (olca->reverseLogicalChannelParameters.options &
479 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
480 && (olca->reverseLogicalChannelParameters.multiplexParameters.
481 choice ==
482 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
484 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
485 &olca->
486 reverseLogicalChannelParameters.
487 multiplexParameters.
488 h2250LogicalChannelParameters);
489 if (ret < 0)
490 return -1;
493 if ((olca->options &
494 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
495 (olca->forwardMultiplexAckParameters.choice ==
496 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
498 ack = &olca->forwardMultiplexAckParameters.
499 h2250LogicalChannelAckParameters;
500 if (ack->options &
501 eH2250LogicalChannelAckParameters_mediaChannel) {
502 /* RTP */
503 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
504 &ack->mediaChannel);
505 if (ret < 0)
506 return -1;
509 if (ack->options &
510 eH2250LogicalChannelAckParameters_mediaControlChannel) {
511 /* RTCP */
512 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
513 &ack->mediaControlChannel);
514 if (ret < 0)
515 return -1;
519 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
520 olca->separateStack.networkAddress.choice ==
521 eNetworkAccessParameters_networkAddress_localAreaAddress) {
522 ret = expect_t120(skb, ct, ctinfo, data, dataoff,
523 &olca->separateStack.networkAddress.
524 localAreaAddress);
525 if (ret < 0)
526 return -1;
529 return 0;
532 /****************************************************************************/
533 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
534 enum ip_conntrack_info ctinfo,
535 unsigned char **data, int dataoff,
536 MultimediaSystemControlMessage *mscm)
538 switch (mscm->choice) {
539 case eMultimediaSystemControlMessage_request:
540 if (mscm->request.choice ==
541 eRequestMessage_openLogicalChannel) {
542 return process_olc(skb, ct, ctinfo, data, dataoff,
543 &mscm->request.openLogicalChannel);
545 pr_debug("nf_ct_h323: H.245 Request %d\n",
546 mscm->request.choice);
547 break;
548 case eMultimediaSystemControlMessage_response:
549 if (mscm->response.choice ==
550 eResponseMessage_openLogicalChannelAck) {
551 return process_olca(skb, ct, ctinfo, data, dataoff,
552 &mscm->response.
553 openLogicalChannelAck);
555 pr_debug("nf_ct_h323: H.245 Response %d\n",
556 mscm->response.choice);
557 break;
558 default:
559 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice);
560 break;
563 return 0;
566 /****************************************************************************/
567 static int h245_help(struct sk_buff *skb, unsigned int protoff,
568 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
570 static MultimediaSystemControlMessage mscm;
571 unsigned char *data = NULL;
572 int datalen;
573 int dataoff;
574 int ret;
576 /* Until there's been traffic both ways, don't look in packets. */
577 if (ctinfo != IP_CT_ESTABLISHED &&
578 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
579 return NF_ACCEPT;
581 pr_debug("nf_ct_h245: skblen = %u\n", skb->len);
583 spin_lock_bh(&nf_h323_lock);
585 /* Process each TPKT */
586 while (get_tpkt_data(skb, protoff, ct, ctinfo,
587 &data, &datalen, &dataoff)) {
588 pr_debug("nf_ct_h245: TPKT len=%d ", datalen);
589 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
591 /* Decode H.245 signal */
592 ret = DecodeMultimediaSystemControlMessage(data, datalen,
593 &mscm);
594 if (ret < 0) {
595 pr_debug("nf_ct_h245: decoding error: %s\n",
596 ret == H323_ERROR_BOUND ?
597 "out of bound" : "out of range");
598 /* We don't drop when decoding error */
599 break;
602 /* Process H.245 signal */
603 if (process_h245(skb, ct, ctinfo, &data, dataoff, &mscm) < 0)
604 goto drop;
607 spin_unlock_bh(&nf_h323_lock);
608 return NF_ACCEPT;
610 drop:
611 spin_unlock_bh(&nf_h323_lock);
612 if (net_ratelimit())
613 printk("nf_ct_h245: packet dropped\n");
614 return NF_DROP;
617 /****************************************************************************/
618 static const struct nf_conntrack_expect_policy h245_exp_policy = {
619 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
620 .timeout = 240,
623 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
624 .name = "H.245",
625 .me = THIS_MODULE,
626 .tuple.dst.protonum = IPPROTO_UDP,
627 .help = h245_help,
628 .expect_policy = &h245_exp_policy,
631 /****************************************************************************/
632 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
633 TransportAddress *taddr,
634 union nf_inet_addr *addr, __be16 *port)
636 const unsigned char *p;
637 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
638 int len;
640 switch (taddr->choice) {
641 case eTransportAddress_ipAddress:
642 if (family != AF_INET)
643 return 0;
644 p = data + taddr->ipAddress.ip;
645 len = 4;
646 break;
647 case eTransportAddress_ip6Address:
648 if (family != AF_INET6)
649 return 0;
650 p = data + taddr->ip6Address.ip;
651 len = 16;
652 break;
653 default:
654 return 0;
657 memcpy(addr, p, len);
658 memset((void *)addr + len, 0, sizeof(*addr) - len);
659 memcpy(port, p + len, sizeof(__be16));
661 return 1;
664 /****************************************************************************/
665 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
666 enum ip_conntrack_info ctinfo,
667 unsigned char **data, int dataoff,
668 TransportAddress *taddr)
670 int dir = CTINFO2DIR(ctinfo);
671 int ret = 0;
672 __be16 port;
673 union nf_inet_addr addr;
674 struct nf_conntrack_expect *exp;
675 typeof(nat_h245_hook) nat_h245;
677 /* Read h245Address */
678 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
679 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
680 port == 0)
681 return 0;
683 /* Create expect for h245 connection */
684 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
685 return -1;
686 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
687 ct->tuplehash[!dir].tuple.src.l3num,
688 &ct->tuplehash[!dir].tuple.src.u3,
689 &ct->tuplehash[!dir].tuple.dst.u3,
690 IPPROTO_TCP, NULL, &port);
691 exp->helper = &nf_conntrack_helper_h245;
693 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
694 &ct->tuplehash[!dir].tuple.dst.u3,
695 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
696 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
697 ct->status & IPS_NAT_MASK) {
698 /* NAT needed */
699 ret = nat_h245(skb, ct, ctinfo, data, dataoff, taddr,
700 port, exp);
701 } else { /* Conntrack only */
702 if (nf_ct_expect_related(exp) == 0) {
703 pr_debug("nf_ct_q931: expect H.245 ");
704 NF_CT_DUMP_TUPLE(&exp->tuple);
705 } else
706 ret = -1;
709 nf_ct_expect_put(exp);
711 return ret;
714 /* If the calling party is on the same side of the forward-to party,
715 * we don't need to track the second call */
716 static int callforward_do_filter(const union nf_inet_addr *src,
717 const union nf_inet_addr *dst, int family)
719 const struct nf_afinfo *afinfo;
720 struct flowi fl1, fl2;
721 int ret = 0;
723 /* rcu_read_lock()ed by nf_hook_slow() */
724 afinfo = nf_get_afinfo(family);
725 if (!afinfo)
726 return 0;
728 memset(&fl1, 0, sizeof(fl1));
729 memset(&fl2, 0, sizeof(fl2));
731 switch (family) {
732 case AF_INET: {
733 struct rtable *rt1, *rt2;
735 fl1.fl4_dst = src->ip;
736 fl2.fl4_dst = dst->ip;
737 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
738 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
739 if (rt1->rt_gateway == rt2->rt_gateway &&
740 rt1->u.dst.dev == rt2->u.dst.dev)
741 ret = 1;
742 dst_release(&rt2->u.dst);
744 dst_release(&rt1->u.dst);
746 break;
748 #if defined(CONFIG_NF_CONNTRACK_IPV6) || \
749 defined(CONFIG_NF_CONNTRACK_IPV6_MODULE)
750 case AF_INET6: {
751 struct rt6_info *rt1, *rt2;
753 memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst));
754 memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst));
755 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
756 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
757 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
758 sizeof(rt1->rt6i_gateway)) &&
759 rt1->u.dst.dev == rt2->u.dst.dev)
760 ret = 1;
761 dst_release(&rt2->u.dst);
763 dst_release(&rt1->u.dst);
765 break;
767 #endif
769 return ret;
773 /****************************************************************************/
774 static int expect_callforwarding(struct sk_buff *skb,
775 struct nf_conn *ct,
776 enum ip_conntrack_info ctinfo,
777 unsigned char **data, int dataoff,
778 TransportAddress *taddr)
780 int dir = CTINFO2DIR(ctinfo);
781 int ret = 0;
782 __be16 port;
783 union nf_inet_addr addr;
784 struct nf_conntrack_expect *exp;
785 typeof(nat_callforwarding_hook) nat_callforwarding;
787 /* Read alternativeAddress */
788 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
789 return 0;
791 /* If the calling party is on the same side of the forward-to party,
792 * we don't need to track the second call */
793 if (callforward_filter &&
794 callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
795 ct->tuplehash[!dir].tuple.src.l3num)) {
796 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
797 return 0;
800 /* Create expect for the second call leg */
801 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
802 return -1;
803 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
804 ct->tuplehash[!dir].tuple.src.l3num,
805 &ct->tuplehash[!dir].tuple.src.u3, &addr,
806 IPPROTO_TCP, NULL, &port);
807 exp->helper = nf_conntrack_helper_q931;
809 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
810 &ct->tuplehash[!dir].tuple.dst.u3,
811 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
812 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
813 ct->status & IPS_NAT_MASK) {
814 /* Need NAT */
815 ret = nat_callforwarding(skb, ct, ctinfo, data, dataoff,
816 taddr, port, exp);
817 } else { /* Conntrack only */
818 if (nf_ct_expect_related(exp) == 0) {
819 pr_debug("nf_ct_q931: expect Call Forwarding ");
820 NF_CT_DUMP_TUPLE(&exp->tuple);
821 } else
822 ret = -1;
825 nf_ct_expect_put(exp);
827 return ret;
830 /****************************************************************************/
831 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
832 enum ip_conntrack_info ctinfo,
833 unsigned char **data, int dataoff,
834 Setup_UUIE *setup)
836 int dir = CTINFO2DIR(ctinfo);
837 int ret;
838 int i;
839 __be16 port;
840 union nf_inet_addr addr;
841 typeof(set_h225_addr_hook) set_h225_addr;
843 pr_debug("nf_ct_q931: Setup\n");
845 if (setup->options & eSetup_UUIE_h245Address) {
846 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
847 &setup->h245Address);
848 if (ret < 0)
849 return -1;
852 set_h225_addr = rcu_dereference(set_h225_addr_hook);
853 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
854 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
855 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
856 &addr, &port) &&
857 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
858 pr_debug("nf_ct_q931: set destCallSignalAddress "
859 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
860 NIP6(*(struct in6_addr *)&addr), ntohs(port),
861 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.src.u3),
862 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
863 ret = set_h225_addr(skb, data, dataoff,
864 &setup->destCallSignalAddress,
865 &ct->tuplehash[!dir].tuple.src.u3,
866 ct->tuplehash[!dir].tuple.src.u.tcp.port);
867 if (ret < 0)
868 return -1;
871 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
872 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
873 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
874 &addr, &port) &&
875 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
876 pr_debug("nf_ct_q931: set sourceCallSignalAddress "
877 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
878 NIP6(*(struct in6_addr *)&addr), ntohs(port),
879 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.dst.u3),
880 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
881 ret = set_h225_addr(skb, data, dataoff,
882 &setup->sourceCallSignalAddress,
883 &ct->tuplehash[!dir].tuple.dst.u3,
884 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
885 if (ret < 0)
886 return -1;
889 if (setup->options & eSetup_UUIE_fastStart) {
890 for (i = 0; i < setup->fastStart.count; i++) {
891 ret = process_olc(skb, ct, ctinfo, data, dataoff,
892 &setup->fastStart.item[i]);
893 if (ret < 0)
894 return -1;
898 return 0;
901 /****************************************************************************/
902 static int process_callproceeding(struct sk_buff *skb,
903 struct nf_conn *ct,
904 enum ip_conntrack_info ctinfo,
905 unsigned char **data, int dataoff,
906 CallProceeding_UUIE *callproc)
908 int ret;
909 int i;
911 pr_debug("nf_ct_q931: CallProceeding\n");
913 if (callproc->options & eCallProceeding_UUIE_h245Address) {
914 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
915 &callproc->h245Address);
916 if (ret < 0)
917 return -1;
920 if (callproc->options & eCallProceeding_UUIE_fastStart) {
921 for (i = 0; i < callproc->fastStart.count; i++) {
922 ret = process_olc(skb, ct, ctinfo, data, dataoff,
923 &callproc->fastStart.item[i]);
924 if (ret < 0)
925 return -1;
929 return 0;
932 /****************************************************************************/
933 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
934 enum ip_conntrack_info ctinfo,
935 unsigned char **data, int dataoff,
936 Connect_UUIE *connect)
938 int ret;
939 int i;
941 pr_debug("nf_ct_q931: Connect\n");
943 if (connect->options & eConnect_UUIE_h245Address) {
944 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
945 &connect->h245Address);
946 if (ret < 0)
947 return -1;
950 if (connect->options & eConnect_UUIE_fastStart) {
951 for (i = 0; i < connect->fastStart.count; i++) {
952 ret = process_olc(skb, ct, ctinfo, data, dataoff,
953 &connect->fastStart.item[i]);
954 if (ret < 0)
955 return -1;
959 return 0;
962 /****************************************************************************/
963 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
964 enum ip_conntrack_info ctinfo,
965 unsigned char **data, int dataoff,
966 Alerting_UUIE *alert)
968 int ret;
969 int i;
971 pr_debug("nf_ct_q931: Alerting\n");
973 if (alert->options & eAlerting_UUIE_h245Address) {
974 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
975 &alert->h245Address);
976 if (ret < 0)
977 return -1;
980 if (alert->options & eAlerting_UUIE_fastStart) {
981 for (i = 0; i < alert->fastStart.count; i++) {
982 ret = process_olc(skb, ct, ctinfo, data, dataoff,
983 &alert->fastStart.item[i]);
984 if (ret < 0)
985 return -1;
989 return 0;
992 /****************************************************************************/
993 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
994 enum ip_conntrack_info ctinfo,
995 unsigned char **data, int dataoff,
996 Facility_UUIE *facility)
998 int ret;
999 int i;
1001 pr_debug("nf_ct_q931: Facility\n");
1003 if (facility->reason.choice == eFacilityReason_callForwarded) {
1004 if (facility->options & eFacility_UUIE_alternativeAddress)
1005 return expect_callforwarding(skb, ct, ctinfo, data,
1006 dataoff,
1007 &facility->
1008 alternativeAddress);
1009 return 0;
1012 if (facility->options & eFacility_UUIE_h245Address) {
1013 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
1014 &facility->h245Address);
1015 if (ret < 0)
1016 return -1;
1019 if (facility->options & eFacility_UUIE_fastStart) {
1020 for (i = 0; i < facility->fastStart.count; i++) {
1021 ret = process_olc(skb, ct, ctinfo, data, dataoff,
1022 &facility->fastStart.item[i]);
1023 if (ret < 0)
1024 return -1;
1028 return 0;
1031 /****************************************************************************/
1032 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1033 enum ip_conntrack_info ctinfo,
1034 unsigned char **data, int dataoff,
1035 Progress_UUIE *progress)
1037 int ret;
1038 int i;
1040 pr_debug("nf_ct_q931: Progress\n");
1042 if (progress->options & eProgress_UUIE_h245Address) {
1043 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
1044 &progress->h245Address);
1045 if (ret < 0)
1046 return -1;
1049 if (progress->options & eProgress_UUIE_fastStart) {
1050 for (i = 0; i < progress->fastStart.count; i++) {
1051 ret = process_olc(skb, ct, ctinfo, data, dataoff,
1052 &progress->fastStart.item[i]);
1053 if (ret < 0)
1054 return -1;
1058 return 0;
1061 /****************************************************************************/
1062 static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1063 enum ip_conntrack_info ctinfo,
1064 unsigned char **data, int dataoff, Q931 *q931)
1066 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1067 int i;
1068 int ret = 0;
1070 switch (pdu->h323_message_body.choice) {
1071 case eH323_UU_PDU_h323_message_body_setup:
1072 ret = process_setup(skb, ct, ctinfo, data, dataoff,
1073 &pdu->h323_message_body.setup);
1074 break;
1075 case eH323_UU_PDU_h323_message_body_callProceeding:
1076 ret = process_callproceeding(skb, ct, ctinfo, data, dataoff,
1077 &pdu->h323_message_body.
1078 callProceeding);
1079 break;
1080 case eH323_UU_PDU_h323_message_body_connect:
1081 ret = process_connect(skb, ct, ctinfo, data, dataoff,
1082 &pdu->h323_message_body.connect);
1083 break;
1084 case eH323_UU_PDU_h323_message_body_alerting:
1085 ret = process_alerting(skb, ct, ctinfo, data, dataoff,
1086 &pdu->h323_message_body.alerting);
1087 break;
1088 case eH323_UU_PDU_h323_message_body_facility:
1089 ret = process_facility(skb, ct, ctinfo, data, dataoff,
1090 &pdu->h323_message_body.facility);
1091 break;
1092 case eH323_UU_PDU_h323_message_body_progress:
1093 ret = process_progress(skb, ct, ctinfo, data, dataoff,
1094 &pdu->h323_message_body.progress);
1095 break;
1096 default:
1097 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1098 pdu->h323_message_body.choice);
1099 break;
1102 if (ret < 0)
1103 return -1;
1105 if (pdu->options & eH323_UU_PDU_h245Control) {
1106 for (i = 0; i < pdu->h245Control.count; i++) {
1107 ret = process_h245(skb, ct, ctinfo, data, dataoff,
1108 &pdu->h245Control.item[i]);
1109 if (ret < 0)
1110 return -1;
1114 return 0;
1117 /****************************************************************************/
1118 static int q931_help(struct sk_buff *skb, unsigned int protoff,
1119 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1121 static Q931 q931;
1122 unsigned char *data = NULL;
1123 int datalen;
1124 int dataoff;
1125 int ret;
1127 /* Until there's been traffic both ways, don't look in packets. */
1128 if (ctinfo != IP_CT_ESTABLISHED &&
1129 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1130 return NF_ACCEPT;
1132 pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1134 spin_lock_bh(&nf_h323_lock);
1136 /* Process each TPKT */
1137 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1138 &data, &datalen, &dataoff)) {
1139 pr_debug("nf_ct_q931: TPKT len=%d ", datalen);
1140 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1142 /* Decode Q.931 signal */
1143 ret = DecodeQ931(data, datalen, &q931);
1144 if (ret < 0) {
1145 pr_debug("nf_ct_q931: decoding error: %s\n",
1146 ret == H323_ERROR_BOUND ?
1147 "out of bound" : "out of range");
1148 /* We don't drop when decoding error */
1149 break;
1152 /* Process Q.931 signal */
1153 if (process_q931(skb, ct, ctinfo, &data, dataoff, &q931) < 0)
1154 goto drop;
1157 spin_unlock_bh(&nf_h323_lock);
1158 return NF_ACCEPT;
1160 drop:
1161 spin_unlock_bh(&nf_h323_lock);
1162 if (net_ratelimit())
1163 printk("nf_ct_q931: packet dropped\n");
1164 return NF_DROP;
1167 /****************************************************************************/
1168 static const struct nf_conntrack_expect_policy q931_exp_policy = {
1169 /* T.120 and H.245 */
1170 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1171 .timeout = 240,
1174 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1176 .name = "Q.931",
1177 .me = THIS_MODULE,
1178 .tuple.src.l3num = AF_INET,
1179 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1180 .tuple.dst.protonum = IPPROTO_TCP,
1181 .help = q931_help,
1182 .expect_policy = &q931_exp_policy,
1185 .name = "Q.931",
1186 .me = THIS_MODULE,
1187 .tuple.src.l3num = AF_INET6,
1188 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1189 .tuple.dst.protonum = IPPROTO_TCP,
1190 .help = q931_help,
1191 .expect_policy = &q931_exp_policy,
1195 /****************************************************************************/
1196 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1197 int *datalen)
1199 const struct udphdr *uh;
1200 struct udphdr _uh;
1201 int dataoff;
1203 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1204 if (uh == NULL)
1205 return NULL;
1206 dataoff = protoff + sizeof(_uh);
1207 if (dataoff >= skb->len)
1208 return NULL;
1209 *datalen = skb->len - dataoff;
1210 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1213 /****************************************************************************/
1214 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1215 union nf_inet_addr *addr,
1216 __be16 port)
1218 struct nf_conntrack_expect *exp;
1219 struct nf_conntrack_tuple tuple;
1221 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1222 tuple.src.u.tcp.port = 0;
1223 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1224 tuple.dst.u.tcp.port = port;
1225 tuple.dst.protonum = IPPROTO_TCP;
1227 exp = __nf_ct_expect_find(&tuple);
1228 if (exp && exp->master == ct)
1229 return exp;
1230 return NULL;
1233 /****************************************************************************/
1234 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1235 unsigned timeout)
1237 if (!exp || !del_timer(&exp->timeout))
1238 return 0;
1240 exp->timeout.expires = jiffies + timeout * HZ;
1241 add_timer(&exp->timeout);
1243 return 1;
1246 /****************************************************************************/
1247 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1248 enum ip_conntrack_info ctinfo,
1249 unsigned char **data,
1250 TransportAddress *taddr, int count)
1252 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1253 int dir = CTINFO2DIR(ctinfo);
1254 int ret = 0;
1255 int i;
1256 __be16 port;
1257 union nf_inet_addr addr;
1258 struct nf_conntrack_expect *exp;
1259 typeof(nat_q931_hook) nat_q931;
1261 /* Look for the first related address */
1262 for (i = 0; i < count; i++) {
1263 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1264 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1265 sizeof(addr)) == 0 && port != 0)
1266 break;
1269 if (i >= count) /* Not found */
1270 return 0;
1272 /* Create expect for Q.931 */
1273 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1274 return -1;
1275 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1276 ct->tuplehash[!dir].tuple.src.l3num,
1277 gkrouted_only ? /* only accept calls from GK? */
1278 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1279 &ct->tuplehash[!dir].tuple.dst.u3,
1280 IPPROTO_TCP, NULL, &port);
1281 exp->helper = nf_conntrack_helper_q931;
1282 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1284 nat_q931 = rcu_dereference(nat_q931_hook);
1285 if (nat_q931 && ct->status & IPS_NAT_MASK) { /* Need NAT */
1286 ret = nat_q931(skb, ct, ctinfo, data, taddr, i, port, exp);
1287 } else { /* Conntrack only */
1288 if (nf_ct_expect_related(exp) == 0) {
1289 pr_debug("nf_ct_ras: expect Q.931 ");
1290 NF_CT_DUMP_TUPLE(&exp->tuple);
1292 /* Save port for looking up expect in processing RCF */
1293 info->sig_port[dir] = port;
1294 } else
1295 ret = -1;
1298 nf_ct_expect_put(exp);
1300 return ret;
1303 /****************************************************************************/
1304 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1305 enum ip_conntrack_info ctinfo,
1306 unsigned char **data, GatekeeperRequest *grq)
1308 typeof(set_ras_addr_hook) set_ras_addr;
1310 pr_debug("nf_ct_ras: GRQ\n");
1312 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1313 if (set_ras_addr && ct->status & IPS_NAT_MASK) /* NATed */
1314 return set_ras_addr(skb, ct, ctinfo, data,
1315 &grq->rasAddress, 1);
1316 return 0;
1319 /****************************************************************************/
1320 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1321 enum ip_conntrack_info ctinfo,
1322 unsigned char **data, GatekeeperConfirm *gcf)
1324 int dir = CTINFO2DIR(ctinfo);
1325 int ret = 0;
1326 __be16 port;
1327 union nf_inet_addr addr;
1328 struct nf_conntrack_expect *exp;
1330 pr_debug("nf_ct_ras: GCF\n");
1332 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1333 return 0;
1335 /* Registration port is the same as discovery port */
1336 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1337 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1338 return 0;
1340 /* Avoid RAS expectation loops. A GCF is never expected. */
1341 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1342 return 0;
1344 /* Need new expect */
1345 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1346 return -1;
1347 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1348 ct->tuplehash[!dir].tuple.src.l3num,
1349 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1350 IPPROTO_UDP, NULL, &port);
1351 exp->helper = nf_conntrack_helper_ras;
1353 if (nf_ct_expect_related(exp) == 0) {
1354 pr_debug("nf_ct_ras: expect RAS ");
1355 NF_CT_DUMP_TUPLE(&exp->tuple);
1356 } else
1357 ret = -1;
1359 nf_ct_expect_put(exp);
1361 return ret;
1364 /****************************************************************************/
1365 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1366 enum ip_conntrack_info ctinfo,
1367 unsigned char **data, RegistrationRequest *rrq)
1369 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1370 int ret;
1371 typeof(set_ras_addr_hook) set_ras_addr;
1373 pr_debug("nf_ct_ras: RRQ\n");
1375 ret = expect_q931(skb, ct, ctinfo, data,
1376 rrq->callSignalAddress.item,
1377 rrq->callSignalAddress.count);
1378 if (ret < 0)
1379 return -1;
1381 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1382 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1383 ret = set_ras_addr(skb, ct, ctinfo, data,
1384 rrq->rasAddress.item,
1385 rrq->rasAddress.count);
1386 if (ret < 0)
1387 return -1;
1390 if (rrq->options & eRegistrationRequest_timeToLive) {
1391 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1392 info->timeout = rrq->timeToLive;
1393 } else
1394 info->timeout = default_rrq_ttl;
1396 return 0;
1399 /****************************************************************************/
1400 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1401 enum ip_conntrack_info ctinfo,
1402 unsigned char **data, RegistrationConfirm *rcf)
1404 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1405 int dir = CTINFO2DIR(ctinfo);
1406 int ret;
1407 struct nf_conntrack_expect *exp;
1408 typeof(set_sig_addr_hook) set_sig_addr;
1410 pr_debug("nf_ct_ras: RCF\n");
1412 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1413 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1414 ret = set_sig_addr(skb, ct, ctinfo, data,
1415 rcf->callSignalAddress.item,
1416 rcf->callSignalAddress.count);
1417 if (ret < 0)
1418 return -1;
1421 if (rcf->options & eRegistrationConfirm_timeToLive) {
1422 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1423 info->timeout = rcf->timeToLive;
1426 if (info->timeout > 0) {
1427 pr_debug("nf_ct_ras: set RAS connection timeout to "
1428 "%u seconds\n", info->timeout);
1429 nf_ct_refresh(ct, skb, info->timeout * HZ);
1431 /* Set expect timeout */
1432 spin_lock_bh(&nf_conntrack_lock);
1433 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1434 info->sig_port[!dir]);
1435 if (exp) {
1436 pr_debug("nf_ct_ras: set Q.931 expect "
1437 "timeout to %u seconds for",
1438 info->timeout);
1439 NF_CT_DUMP_TUPLE(&exp->tuple);
1440 set_expect_timeout(exp, info->timeout);
1442 spin_unlock_bh(&nf_conntrack_lock);
1445 return 0;
1448 /****************************************************************************/
1449 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1450 enum ip_conntrack_info ctinfo,
1451 unsigned char **data, UnregistrationRequest *urq)
1453 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1454 int dir = CTINFO2DIR(ctinfo);
1455 int ret;
1456 typeof(set_sig_addr_hook) set_sig_addr;
1458 pr_debug("nf_ct_ras: URQ\n");
1460 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1461 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1462 ret = set_sig_addr(skb, ct, ctinfo, data,
1463 urq->callSignalAddress.item,
1464 urq->callSignalAddress.count);
1465 if (ret < 0)
1466 return -1;
1469 /* Clear old expect */
1470 nf_ct_remove_expectations(ct);
1471 info->sig_port[dir] = 0;
1472 info->sig_port[!dir] = 0;
1474 /* Give it 30 seconds for UCF or URJ */
1475 nf_ct_refresh(ct, skb, 30 * HZ);
1477 return 0;
1480 /****************************************************************************/
1481 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1482 enum ip_conntrack_info ctinfo,
1483 unsigned char **data, AdmissionRequest *arq)
1485 const struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1486 int dir = CTINFO2DIR(ctinfo);
1487 __be16 port;
1488 union nf_inet_addr addr;
1489 typeof(set_h225_addr_hook) set_h225_addr;
1491 pr_debug("nf_ct_ras: ARQ\n");
1493 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1494 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1495 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1496 &addr, &port) &&
1497 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1498 port == info->sig_port[dir] &&
1499 set_h225_addr && ct->status & IPS_NAT_MASK) {
1500 /* Answering ARQ */
1501 return set_h225_addr(skb, data, 0,
1502 &arq->destCallSignalAddress,
1503 &ct->tuplehash[!dir].tuple.dst.u3,
1504 info->sig_port[!dir]);
1507 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1508 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1509 &addr, &port) &&
1510 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1511 set_h225_addr && ct->status & IPS_NAT_MASK) {
1512 /* Calling ARQ */
1513 return set_h225_addr(skb, data, 0,
1514 &arq->srcCallSignalAddress,
1515 &ct->tuplehash[!dir].tuple.dst.u3,
1516 port);
1519 return 0;
1522 /****************************************************************************/
1523 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1524 enum ip_conntrack_info ctinfo,
1525 unsigned char **data, AdmissionConfirm *acf)
1527 int dir = CTINFO2DIR(ctinfo);
1528 int ret = 0;
1529 __be16 port;
1530 union nf_inet_addr addr;
1531 struct nf_conntrack_expect *exp;
1532 typeof(set_sig_addr_hook) set_sig_addr;
1534 pr_debug("nf_ct_ras: ACF\n");
1536 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1537 &addr, &port))
1538 return 0;
1540 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1541 /* Answering ACF */
1542 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1543 if (set_sig_addr && ct->status & IPS_NAT_MASK)
1544 return set_sig_addr(skb, ct, ctinfo, data,
1545 &acf->destCallSignalAddress, 1);
1546 return 0;
1549 /* Need new expect */
1550 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1551 return -1;
1552 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1553 ct->tuplehash[!dir].tuple.src.l3num,
1554 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1555 IPPROTO_TCP, NULL, &port);
1556 exp->flags = NF_CT_EXPECT_PERMANENT;
1557 exp->helper = nf_conntrack_helper_q931;
1559 if (nf_ct_expect_related(exp) == 0) {
1560 pr_debug("nf_ct_ras: expect Q.931 ");
1561 NF_CT_DUMP_TUPLE(&exp->tuple);
1562 } else
1563 ret = -1;
1565 nf_ct_expect_put(exp);
1567 return ret;
1570 /****************************************************************************/
1571 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1572 enum ip_conntrack_info ctinfo,
1573 unsigned char **data, LocationRequest *lrq)
1575 typeof(set_ras_addr_hook) set_ras_addr;
1577 pr_debug("nf_ct_ras: LRQ\n");
1579 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1580 if (set_ras_addr && ct->status & IPS_NAT_MASK)
1581 return set_ras_addr(skb, ct, ctinfo, data,
1582 &lrq->replyAddress, 1);
1583 return 0;
1586 /****************************************************************************/
1587 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1588 enum ip_conntrack_info ctinfo,
1589 unsigned char **data, LocationConfirm *lcf)
1591 int dir = CTINFO2DIR(ctinfo);
1592 int ret = 0;
1593 __be16 port;
1594 union nf_inet_addr addr;
1595 struct nf_conntrack_expect *exp;
1597 pr_debug("nf_ct_ras: LCF\n");
1599 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1600 &addr, &port))
1601 return 0;
1603 /* Need new expect for call signal */
1604 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1605 return -1;
1606 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1607 ct->tuplehash[!dir].tuple.src.l3num,
1608 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1609 IPPROTO_TCP, NULL, &port);
1610 exp->flags = NF_CT_EXPECT_PERMANENT;
1611 exp->helper = nf_conntrack_helper_q931;
1613 if (nf_ct_expect_related(exp) == 0) {
1614 pr_debug("nf_ct_ras: expect Q.931 ");
1615 NF_CT_DUMP_TUPLE(&exp->tuple);
1616 } else
1617 ret = -1;
1619 nf_ct_expect_put(exp);
1621 /* Ignore rasAddress */
1623 return ret;
1626 /****************************************************************************/
1627 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1628 enum ip_conntrack_info ctinfo,
1629 unsigned char **data, InfoRequestResponse *irr)
1631 int ret;
1632 typeof(set_ras_addr_hook) set_ras_addr;
1633 typeof(set_sig_addr_hook) set_sig_addr;
1635 pr_debug("nf_ct_ras: IRR\n");
1637 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1638 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1639 ret = set_ras_addr(skb, ct, ctinfo, data,
1640 &irr->rasAddress, 1);
1641 if (ret < 0)
1642 return -1;
1645 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1646 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1647 ret = set_sig_addr(skb, ct, ctinfo, data,
1648 irr->callSignalAddress.item,
1649 irr->callSignalAddress.count);
1650 if (ret < 0)
1651 return -1;
1654 return 0;
1657 /****************************************************************************/
1658 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1659 enum ip_conntrack_info ctinfo,
1660 unsigned char **data, RasMessage *ras)
1662 switch (ras->choice) {
1663 case eRasMessage_gatekeeperRequest:
1664 return process_grq(skb, ct, ctinfo, data,
1665 &ras->gatekeeperRequest);
1666 case eRasMessage_gatekeeperConfirm:
1667 return process_gcf(skb, ct, ctinfo, data,
1668 &ras->gatekeeperConfirm);
1669 case eRasMessage_registrationRequest:
1670 return process_rrq(skb, ct, ctinfo, data,
1671 &ras->registrationRequest);
1672 case eRasMessage_registrationConfirm:
1673 return process_rcf(skb, ct, ctinfo, data,
1674 &ras->registrationConfirm);
1675 case eRasMessage_unregistrationRequest:
1676 return process_urq(skb, ct, ctinfo, data,
1677 &ras->unregistrationRequest);
1678 case eRasMessage_admissionRequest:
1679 return process_arq(skb, ct, ctinfo, data,
1680 &ras->admissionRequest);
1681 case eRasMessage_admissionConfirm:
1682 return process_acf(skb, ct, ctinfo, data,
1683 &ras->admissionConfirm);
1684 case eRasMessage_locationRequest:
1685 return process_lrq(skb, ct, ctinfo, data,
1686 &ras->locationRequest);
1687 case eRasMessage_locationConfirm:
1688 return process_lcf(skb, ct, ctinfo, data,
1689 &ras->locationConfirm);
1690 case eRasMessage_infoRequestResponse:
1691 return process_irr(skb, ct, ctinfo, data,
1692 &ras->infoRequestResponse);
1693 default:
1694 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1695 break;
1698 return 0;
1701 /****************************************************************************/
1702 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1703 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1705 static RasMessage ras;
1706 unsigned char *data;
1707 int datalen = 0;
1708 int ret;
1710 pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1712 spin_lock_bh(&nf_h323_lock);
1714 /* Get UDP data */
1715 data = get_udp_data(skb, protoff, &datalen);
1716 if (data == NULL)
1717 goto accept;
1718 pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1719 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1721 /* Decode RAS message */
1722 ret = DecodeRasMessage(data, datalen, &ras);
1723 if (ret < 0) {
1724 pr_debug("nf_ct_ras: decoding error: %s\n",
1725 ret == H323_ERROR_BOUND ?
1726 "out of bound" : "out of range");
1727 goto accept;
1730 /* Process RAS message */
1731 if (process_ras(skb, ct, ctinfo, &data, &ras) < 0)
1732 goto drop;
1734 accept:
1735 spin_unlock_bh(&nf_h323_lock);
1736 return NF_ACCEPT;
1738 drop:
1739 spin_unlock_bh(&nf_h323_lock);
1740 if (net_ratelimit())
1741 printk("nf_ct_ras: packet dropped\n");
1742 return NF_DROP;
1745 /****************************************************************************/
1746 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1747 .max_expected = 32,
1748 .timeout = 240,
1751 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1753 .name = "RAS",
1754 .me = THIS_MODULE,
1755 .tuple.src.l3num = AF_INET,
1756 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1757 .tuple.dst.protonum = IPPROTO_UDP,
1758 .help = ras_help,
1759 .expect_policy = &ras_exp_policy,
1762 .name = "RAS",
1763 .me = THIS_MODULE,
1764 .tuple.src.l3num = AF_INET6,
1765 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1766 .tuple.dst.protonum = IPPROTO_UDP,
1767 .help = ras_help,
1768 .expect_policy = &ras_exp_policy,
1772 /****************************************************************************/
1773 static void __exit nf_conntrack_h323_fini(void)
1775 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1776 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1777 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1778 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1779 kfree(h323_buffer);
1780 pr_debug("nf_ct_h323: fini\n");
1783 /****************************************************************************/
1784 static int __init nf_conntrack_h323_init(void)
1786 int ret;
1788 h323_buffer = kmalloc(65536, GFP_KERNEL);
1789 if (!h323_buffer)
1790 return -ENOMEM;
1791 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1792 if (ret < 0)
1793 goto err1;
1794 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1795 if (ret < 0)
1796 goto err2;
1797 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1798 if (ret < 0)
1799 goto err3;
1800 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1801 if (ret < 0)
1802 goto err4;
1803 pr_debug("nf_ct_h323: init success\n");
1804 return 0;
1806 err4:
1807 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1808 err3:
1809 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1810 err2:
1811 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1812 err1:
1813 return ret;
1816 /****************************************************************************/
1817 module_init(nf_conntrack_h323_init);
1818 module_exit(nf_conntrack_h323_fini);
1820 EXPORT_SYMBOL_GPL(get_h225_addr);
1821 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1822 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1823 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1824 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1825 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1826 EXPORT_SYMBOL_GPL(nat_t120_hook);
1827 EXPORT_SYMBOL_GPL(nat_h245_hook);
1828 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1829 EXPORT_SYMBOL_GPL(nat_q931_hook);
1831 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1832 MODULE_DESCRIPTION("H.323 connection tracking helper");
1833 MODULE_LICENSE("GPL");
1834 MODULE_ALIAS("ip_conntrack_h323");