small doc fix: ftrace_dyn_arch_init is called by ftrace_init
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / nf_conntrack_h323_main.c
blobc1504f71cdff08d8ad98685d73b2bba4e5a6c10f
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 len;
223 if (taddr->choice != eH245_TransportAddress_unicastAddress)
224 return 0;
226 switch (taddr->unicastAddress.choice) {
227 case eUnicastAddress_iPAddress:
228 if (nf_ct_l3num(ct) != AF_INET)
229 return 0;
230 p = data + taddr->unicastAddress.iPAddress.network;
231 len = 4;
232 break;
233 case eUnicastAddress_iP6Address:
234 if (nf_ct_l3num(ct) != AF_INET6)
235 return 0;
236 p = data + taddr->unicastAddress.iP6Address.network;
237 len = 16;
238 break;
239 default:
240 return 0;
243 memcpy(addr, p, len);
244 memset((void *)addr + len, 0, sizeof(*addr) - len);
245 memcpy(port, p + len, sizeof(__be16));
247 return 1;
250 /****************************************************************************/
251 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
252 enum ip_conntrack_info ctinfo,
253 unsigned char **data, int dataoff,
254 H245_TransportAddress *taddr)
256 int dir = CTINFO2DIR(ctinfo);
257 int ret = 0;
258 __be16 port;
259 __be16 rtp_port, rtcp_port;
260 union nf_inet_addr addr;
261 struct nf_conntrack_expect *rtp_exp;
262 struct nf_conntrack_expect *rtcp_exp;
263 typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
265 /* Read RTP or RTCP address */
266 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
267 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
268 port == 0)
269 return 0;
271 /* RTP port is even */
272 port &= htons(~1);
273 rtp_port = port;
274 rtcp_port = htons(ntohs(port) + 1);
276 /* Create expect for RTP */
277 if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
278 return -1;
279 nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
280 &ct->tuplehash[!dir].tuple.src.u3,
281 &ct->tuplehash[!dir].tuple.dst.u3,
282 IPPROTO_UDP, NULL, &rtp_port);
284 /* Create expect for RTCP */
285 if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
286 nf_ct_expect_put(rtp_exp);
287 return -1;
289 nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
290 &ct->tuplehash[!dir].tuple.src.u3,
291 &ct->tuplehash[!dir].tuple.dst.u3,
292 IPPROTO_UDP, NULL, &rtcp_port);
294 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
295 &ct->tuplehash[!dir].tuple.dst.u3,
296 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
297 (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
298 ct->status & IPS_NAT_MASK) {
299 /* NAT needed */
300 ret = nat_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
301 taddr, port, rtp_port, rtp_exp, rtcp_exp);
302 } else { /* Conntrack only */
303 if (nf_ct_expect_related(rtp_exp) == 0) {
304 if (nf_ct_expect_related(rtcp_exp) == 0) {
305 pr_debug("nf_ct_h323: expect RTP ");
306 nf_ct_dump_tuple(&rtp_exp->tuple);
307 pr_debug("nf_ct_h323: expect RTCP ");
308 nf_ct_dump_tuple(&rtcp_exp->tuple);
309 } else {
310 nf_ct_unexpect_related(rtp_exp);
311 ret = -1;
313 } else
314 ret = -1;
317 nf_ct_expect_put(rtp_exp);
318 nf_ct_expect_put(rtcp_exp);
320 return ret;
323 /****************************************************************************/
324 static int expect_t120(struct sk_buff *skb,
325 struct nf_conn *ct,
326 enum ip_conntrack_info ctinfo,
327 unsigned char **data, int dataoff,
328 H245_TransportAddress *taddr)
330 int dir = CTINFO2DIR(ctinfo);
331 int ret = 0;
332 __be16 port;
333 union nf_inet_addr addr;
334 struct nf_conntrack_expect *exp;
335 typeof(nat_t120_hook) nat_t120;
337 /* Read T.120 address */
338 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
339 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
340 port == 0)
341 return 0;
343 /* Create expect for T.120 connections */
344 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
345 return -1;
346 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
347 &ct->tuplehash[!dir].tuple.src.u3,
348 &ct->tuplehash[!dir].tuple.dst.u3,
349 IPPROTO_TCP, NULL, &port);
350 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple channels */
352 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
353 &ct->tuplehash[!dir].tuple.dst.u3,
354 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
355 (nat_t120 = rcu_dereference(nat_t120_hook)) &&
356 ct->status & IPS_NAT_MASK) {
357 /* NAT needed */
358 ret = nat_t120(skb, ct, ctinfo, data, dataoff, taddr,
359 port, exp);
360 } else { /* Conntrack only */
361 if (nf_ct_expect_related(exp) == 0) {
362 pr_debug("nf_ct_h323: expect T.120 ");
363 nf_ct_dump_tuple(&exp->tuple);
364 } else
365 ret = -1;
368 nf_ct_expect_put(exp);
370 return ret;
373 /****************************************************************************/
374 static int process_h245_channel(struct sk_buff *skb,
375 struct nf_conn *ct,
376 enum ip_conntrack_info ctinfo,
377 unsigned char **data, int dataoff,
378 H2250LogicalChannelParameters *channel)
380 int ret;
382 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
383 /* RTP */
384 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
385 &channel->mediaChannel);
386 if (ret < 0)
387 return -1;
390 if (channel->
391 options & eH2250LogicalChannelParameters_mediaControlChannel) {
392 /* RTCP */
393 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
394 &channel->mediaControlChannel);
395 if (ret < 0)
396 return -1;
399 return 0;
402 /****************************************************************************/
403 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
404 enum ip_conntrack_info ctinfo,
405 unsigned char **data, int dataoff,
406 OpenLogicalChannel *olc)
408 int ret;
410 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
412 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
413 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
415 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
416 &olc->
417 forwardLogicalChannelParameters.
418 multiplexParameters.
419 h2250LogicalChannelParameters);
420 if (ret < 0)
421 return -1;
424 if ((olc->options &
425 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
426 (olc->reverseLogicalChannelParameters.options &
427 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
428 && (olc->reverseLogicalChannelParameters.multiplexParameters.
429 choice ==
430 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
432 ret =
433 process_h245_channel(skb, ct, ctinfo, data, dataoff,
434 &olc->
435 reverseLogicalChannelParameters.
436 multiplexParameters.
437 h2250LogicalChannelParameters);
438 if (ret < 0)
439 return -1;
442 if ((olc->options & eOpenLogicalChannel_separateStack) &&
443 olc->forwardLogicalChannelParameters.dataType.choice ==
444 eDataType_data &&
445 olc->forwardLogicalChannelParameters.dataType.data.application.
446 choice == eDataApplicationCapability_application_t120 &&
447 olc->forwardLogicalChannelParameters.dataType.data.application.
448 t120.choice == eDataProtocolCapability_separateLANStack &&
449 olc->separateStack.networkAddress.choice ==
450 eNetworkAccessParameters_networkAddress_localAreaAddress) {
451 ret = expect_t120(skb, ct, ctinfo, data, dataoff,
452 &olc->separateStack.networkAddress.
453 localAreaAddress);
454 if (ret < 0)
455 return -1;
458 return 0;
461 /****************************************************************************/
462 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
463 enum ip_conntrack_info ctinfo,
464 unsigned char **data, int dataoff,
465 OpenLogicalChannelAck *olca)
467 H2250LogicalChannelAckParameters *ack;
468 int ret;
470 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
472 if ((olca->options &
473 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
474 (olca->reverseLogicalChannelParameters.options &
475 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
476 && (olca->reverseLogicalChannelParameters.multiplexParameters.
477 choice ==
478 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
480 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
481 &olca->
482 reverseLogicalChannelParameters.
483 multiplexParameters.
484 h2250LogicalChannelParameters);
485 if (ret < 0)
486 return -1;
489 if ((olca->options &
490 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
491 (olca->forwardMultiplexAckParameters.choice ==
492 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
494 ack = &olca->forwardMultiplexAckParameters.
495 h2250LogicalChannelAckParameters;
496 if (ack->options &
497 eH2250LogicalChannelAckParameters_mediaChannel) {
498 /* RTP */
499 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
500 &ack->mediaChannel);
501 if (ret < 0)
502 return -1;
505 if (ack->options &
506 eH2250LogicalChannelAckParameters_mediaControlChannel) {
507 /* RTCP */
508 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
509 &ack->mediaControlChannel);
510 if (ret < 0)
511 return -1;
515 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
516 olca->separateStack.networkAddress.choice ==
517 eNetworkAccessParameters_networkAddress_localAreaAddress) {
518 ret = expect_t120(skb, ct, ctinfo, data, dataoff,
519 &olca->separateStack.networkAddress.
520 localAreaAddress);
521 if (ret < 0)
522 return -1;
525 return 0;
528 /****************************************************************************/
529 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
530 enum ip_conntrack_info ctinfo,
531 unsigned char **data, int dataoff,
532 MultimediaSystemControlMessage *mscm)
534 switch (mscm->choice) {
535 case eMultimediaSystemControlMessage_request:
536 if (mscm->request.choice ==
537 eRequestMessage_openLogicalChannel) {
538 return process_olc(skb, ct, ctinfo, data, dataoff,
539 &mscm->request.openLogicalChannel);
541 pr_debug("nf_ct_h323: H.245 Request %d\n",
542 mscm->request.choice);
543 break;
544 case eMultimediaSystemControlMessage_response:
545 if (mscm->response.choice ==
546 eResponseMessage_openLogicalChannelAck) {
547 return process_olca(skb, ct, ctinfo, data, dataoff,
548 &mscm->response.
549 openLogicalChannelAck);
551 pr_debug("nf_ct_h323: H.245 Response %d\n",
552 mscm->response.choice);
553 break;
554 default:
555 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice);
556 break;
559 return 0;
562 /****************************************************************************/
563 static int h245_help(struct sk_buff *skb, unsigned int protoff,
564 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
566 static MultimediaSystemControlMessage mscm;
567 unsigned char *data = NULL;
568 int datalen;
569 int dataoff;
570 int ret;
572 /* Until there's been traffic both ways, don't look in packets. */
573 if (ctinfo != IP_CT_ESTABLISHED &&
574 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
575 return NF_ACCEPT;
577 pr_debug("nf_ct_h245: skblen = %u\n", skb->len);
579 spin_lock_bh(&nf_h323_lock);
581 /* Process each TPKT */
582 while (get_tpkt_data(skb, protoff, ct, ctinfo,
583 &data, &datalen, &dataoff)) {
584 pr_debug("nf_ct_h245: TPKT len=%d ", datalen);
585 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
587 /* Decode H.245 signal */
588 ret = DecodeMultimediaSystemControlMessage(data, datalen,
589 &mscm);
590 if (ret < 0) {
591 pr_debug("nf_ct_h245: decoding error: %s\n",
592 ret == H323_ERROR_BOUND ?
593 "out of bound" : "out of range");
594 /* We don't drop when decoding error */
595 break;
598 /* Process H.245 signal */
599 if (process_h245(skb, ct, ctinfo, &data, dataoff, &mscm) < 0)
600 goto drop;
603 spin_unlock_bh(&nf_h323_lock);
604 return NF_ACCEPT;
606 drop:
607 spin_unlock_bh(&nf_h323_lock);
608 if (net_ratelimit())
609 printk("nf_ct_h245: packet dropped\n");
610 return NF_DROP;
613 /****************************************************************************/
614 static const struct nf_conntrack_expect_policy h245_exp_policy = {
615 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
616 .timeout = 240,
619 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
620 .name = "H.245",
621 .me = THIS_MODULE,
622 .tuple.src.l3num = AF_UNSPEC,
623 .tuple.dst.protonum = IPPROTO_UDP,
624 .help = h245_help,
625 .expect_policy = &h245_exp_policy,
628 /****************************************************************************/
629 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
630 TransportAddress *taddr,
631 union nf_inet_addr *addr, __be16 *port)
633 const unsigned char *p;
634 int len;
636 switch (taddr->choice) {
637 case eTransportAddress_ipAddress:
638 if (nf_ct_l3num(ct) != AF_INET)
639 return 0;
640 p = data + taddr->ipAddress.ip;
641 len = 4;
642 break;
643 case eTransportAddress_ip6Address:
644 if (nf_ct_l3num(ct) != AF_INET6)
645 return 0;
646 p = data + taddr->ip6Address.ip;
647 len = 16;
648 break;
649 default:
650 return 0;
653 memcpy(addr, p, len);
654 memset((void *)addr + len, 0, sizeof(*addr) - len);
655 memcpy(port, p + len, sizeof(__be16));
657 return 1;
660 /****************************************************************************/
661 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
662 enum ip_conntrack_info ctinfo,
663 unsigned char **data, int dataoff,
664 TransportAddress *taddr)
666 int dir = CTINFO2DIR(ctinfo);
667 int ret = 0;
668 __be16 port;
669 union nf_inet_addr addr;
670 struct nf_conntrack_expect *exp;
671 typeof(nat_h245_hook) nat_h245;
673 /* Read h245Address */
674 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
675 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
676 port == 0)
677 return 0;
679 /* Create expect for h245 connection */
680 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
681 return -1;
682 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
683 &ct->tuplehash[!dir].tuple.src.u3,
684 &ct->tuplehash[!dir].tuple.dst.u3,
685 IPPROTO_TCP, NULL, &port);
686 exp->helper = &nf_conntrack_helper_h245;
688 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
689 &ct->tuplehash[!dir].tuple.dst.u3,
690 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
691 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
692 ct->status & IPS_NAT_MASK) {
693 /* NAT needed */
694 ret = nat_h245(skb, ct, ctinfo, data, dataoff, taddr,
695 port, exp);
696 } else { /* Conntrack only */
697 if (nf_ct_expect_related(exp) == 0) {
698 pr_debug("nf_ct_q931: expect H.245 ");
699 nf_ct_dump_tuple(&exp->tuple);
700 } else
701 ret = -1;
704 nf_ct_expect_put(exp);
706 return ret;
709 /* If the calling party is on the same side of the forward-to party,
710 * we don't need to track the second call */
711 static int callforward_do_filter(const union nf_inet_addr *src,
712 const union nf_inet_addr *dst,
713 u_int8_t family)
715 const struct nf_afinfo *afinfo;
716 struct flowi fl1, fl2;
717 int ret = 0;
719 /* rcu_read_lock()ed by nf_hook_slow() */
720 afinfo = nf_get_afinfo(family);
721 if (!afinfo)
722 return 0;
724 memset(&fl1, 0, sizeof(fl1));
725 memset(&fl2, 0, sizeof(fl2));
727 switch (family) {
728 case AF_INET: {
729 struct rtable *rt1, *rt2;
731 fl1.fl4_dst = src->ip;
732 fl2.fl4_dst = dst->ip;
733 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
734 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
735 if (rt1->rt_gateway == rt2->rt_gateway &&
736 rt1->u.dst.dev == rt2->u.dst.dev)
737 ret = 1;
738 dst_release(&rt2->u.dst);
740 dst_release(&rt1->u.dst);
742 break;
744 #if defined(CONFIG_NF_CONNTRACK_IPV6) || \
745 defined(CONFIG_NF_CONNTRACK_IPV6_MODULE)
746 case AF_INET6: {
747 struct rt6_info *rt1, *rt2;
749 memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst));
750 memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst));
751 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
752 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
753 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
754 sizeof(rt1->rt6i_gateway)) &&
755 rt1->u.dst.dev == rt2->u.dst.dev)
756 ret = 1;
757 dst_release(&rt2->u.dst);
759 dst_release(&rt1->u.dst);
761 break;
763 #endif
765 return ret;
769 /****************************************************************************/
770 static int expect_callforwarding(struct sk_buff *skb,
771 struct nf_conn *ct,
772 enum ip_conntrack_info ctinfo,
773 unsigned char **data, int dataoff,
774 TransportAddress *taddr)
776 int dir = CTINFO2DIR(ctinfo);
777 int ret = 0;
778 __be16 port;
779 union nf_inet_addr addr;
780 struct nf_conntrack_expect *exp;
781 typeof(nat_callforwarding_hook) nat_callforwarding;
783 /* Read alternativeAddress */
784 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
785 return 0;
787 /* If the calling party is on the same side of the forward-to party,
788 * we don't need to track the second call */
789 if (callforward_filter &&
790 callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
791 nf_ct_l3num(ct))) {
792 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
793 return 0;
796 /* Create expect for the second call leg */
797 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
798 return -1;
799 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
800 &ct->tuplehash[!dir].tuple.src.u3, &addr,
801 IPPROTO_TCP, NULL, &port);
802 exp->helper = nf_conntrack_helper_q931;
804 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
805 &ct->tuplehash[!dir].tuple.dst.u3,
806 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
807 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
808 ct->status & IPS_NAT_MASK) {
809 /* Need NAT */
810 ret = nat_callforwarding(skb, ct, ctinfo, data, dataoff,
811 taddr, port, exp);
812 } else { /* Conntrack only */
813 if (nf_ct_expect_related(exp) == 0) {
814 pr_debug("nf_ct_q931: expect Call Forwarding ");
815 nf_ct_dump_tuple(&exp->tuple);
816 } else
817 ret = -1;
820 nf_ct_expect_put(exp);
822 return ret;
825 /****************************************************************************/
826 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
827 enum ip_conntrack_info ctinfo,
828 unsigned char **data, int dataoff,
829 Setup_UUIE *setup)
831 int dir = CTINFO2DIR(ctinfo);
832 int ret;
833 int i;
834 __be16 port;
835 union nf_inet_addr addr;
836 typeof(set_h225_addr_hook) set_h225_addr;
838 pr_debug("nf_ct_q931: Setup\n");
840 if (setup->options & eSetup_UUIE_h245Address) {
841 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
842 &setup->h245Address);
843 if (ret < 0)
844 return -1;
847 set_h225_addr = rcu_dereference(set_h225_addr_hook);
848 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
849 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
850 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
851 &addr, &port) &&
852 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
853 pr_debug("nf_ct_q931: set destCallSignalAddress "
854 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
855 NIP6(*(struct in6_addr *)&addr), ntohs(port),
856 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.src.u3),
857 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
858 ret = set_h225_addr(skb, data, dataoff,
859 &setup->destCallSignalAddress,
860 &ct->tuplehash[!dir].tuple.src.u3,
861 ct->tuplehash[!dir].tuple.src.u.tcp.port);
862 if (ret < 0)
863 return -1;
866 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
867 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
868 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
869 &addr, &port) &&
870 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
871 pr_debug("nf_ct_q931: set sourceCallSignalAddress "
872 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
873 NIP6(*(struct in6_addr *)&addr), ntohs(port),
874 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.dst.u3),
875 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
876 ret = set_h225_addr(skb, data, dataoff,
877 &setup->sourceCallSignalAddress,
878 &ct->tuplehash[!dir].tuple.dst.u3,
879 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
880 if (ret < 0)
881 return -1;
884 if (setup->options & eSetup_UUIE_fastStart) {
885 for (i = 0; i < setup->fastStart.count; i++) {
886 ret = process_olc(skb, ct, ctinfo, data, dataoff,
887 &setup->fastStart.item[i]);
888 if (ret < 0)
889 return -1;
893 return 0;
896 /****************************************************************************/
897 static int process_callproceeding(struct sk_buff *skb,
898 struct nf_conn *ct,
899 enum ip_conntrack_info ctinfo,
900 unsigned char **data, int dataoff,
901 CallProceeding_UUIE *callproc)
903 int ret;
904 int i;
906 pr_debug("nf_ct_q931: CallProceeding\n");
908 if (callproc->options & eCallProceeding_UUIE_h245Address) {
909 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
910 &callproc->h245Address);
911 if (ret < 0)
912 return -1;
915 if (callproc->options & eCallProceeding_UUIE_fastStart) {
916 for (i = 0; i < callproc->fastStart.count; i++) {
917 ret = process_olc(skb, ct, ctinfo, data, dataoff,
918 &callproc->fastStart.item[i]);
919 if (ret < 0)
920 return -1;
924 return 0;
927 /****************************************************************************/
928 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
929 enum ip_conntrack_info ctinfo,
930 unsigned char **data, int dataoff,
931 Connect_UUIE *connect)
933 int ret;
934 int i;
936 pr_debug("nf_ct_q931: Connect\n");
938 if (connect->options & eConnect_UUIE_h245Address) {
939 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
940 &connect->h245Address);
941 if (ret < 0)
942 return -1;
945 if (connect->options & eConnect_UUIE_fastStart) {
946 for (i = 0; i < connect->fastStart.count; i++) {
947 ret = process_olc(skb, ct, ctinfo, data, dataoff,
948 &connect->fastStart.item[i]);
949 if (ret < 0)
950 return -1;
954 return 0;
957 /****************************************************************************/
958 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
959 enum ip_conntrack_info ctinfo,
960 unsigned char **data, int dataoff,
961 Alerting_UUIE *alert)
963 int ret;
964 int i;
966 pr_debug("nf_ct_q931: Alerting\n");
968 if (alert->options & eAlerting_UUIE_h245Address) {
969 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
970 &alert->h245Address);
971 if (ret < 0)
972 return -1;
975 if (alert->options & eAlerting_UUIE_fastStart) {
976 for (i = 0; i < alert->fastStart.count; i++) {
977 ret = process_olc(skb, ct, ctinfo, data, dataoff,
978 &alert->fastStart.item[i]);
979 if (ret < 0)
980 return -1;
984 return 0;
987 /****************************************************************************/
988 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
989 enum ip_conntrack_info ctinfo,
990 unsigned char **data, int dataoff,
991 Facility_UUIE *facility)
993 int ret;
994 int i;
996 pr_debug("nf_ct_q931: Facility\n");
998 if (facility->reason.choice == eFacilityReason_callForwarded) {
999 if (facility->options & eFacility_UUIE_alternativeAddress)
1000 return expect_callforwarding(skb, ct, ctinfo, data,
1001 dataoff,
1002 &facility->
1003 alternativeAddress);
1004 return 0;
1007 if (facility->options & eFacility_UUIE_h245Address) {
1008 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
1009 &facility->h245Address);
1010 if (ret < 0)
1011 return -1;
1014 if (facility->options & eFacility_UUIE_fastStart) {
1015 for (i = 0; i < facility->fastStart.count; i++) {
1016 ret = process_olc(skb, ct, ctinfo, data, dataoff,
1017 &facility->fastStart.item[i]);
1018 if (ret < 0)
1019 return -1;
1023 return 0;
1026 /****************************************************************************/
1027 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1028 enum ip_conntrack_info ctinfo,
1029 unsigned char **data, int dataoff,
1030 Progress_UUIE *progress)
1032 int ret;
1033 int i;
1035 pr_debug("nf_ct_q931: Progress\n");
1037 if (progress->options & eProgress_UUIE_h245Address) {
1038 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
1039 &progress->h245Address);
1040 if (ret < 0)
1041 return -1;
1044 if (progress->options & eProgress_UUIE_fastStart) {
1045 for (i = 0; i < progress->fastStart.count; i++) {
1046 ret = process_olc(skb, ct, ctinfo, data, dataoff,
1047 &progress->fastStart.item[i]);
1048 if (ret < 0)
1049 return -1;
1053 return 0;
1056 /****************************************************************************/
1057 static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1058 enum ip_conntrack_info ctinfo,
1059 unsigned char **data, int dataoff, Q931 *q931)
1061 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1062 int i;
1063 int ret = 0;
1065 switch (pdu->h323_message_body.choice) {
1066 case eH323_UU_PDU_h323_message_body_setup:
1067 ret = process_setup(skb, ct, ctinfo, data, dataoff,
1068 &pdu->h323_message_body.setup);
1069 break;
1070 case eH323_UU_PDU_h323_message_body_callProceeding:
1071 ret = process_callproceeding(skb, ct, ctinfo, data, dataoff,
1072 &pdu->h323_message_body.
1073 callProceeding);
1074 break;
1075 case eH323_UU_PDU_h323_message_body_connect:
1076 ret = process_connect(skb, ct, ctinfo, data, dataoff,
1077 &pdu->h323_message_body.connect);
1078 break;
1079 case eH323_UU_PDU_h323_message_body_alerting:
1080 ret = process_alerting(skb, ct, ctinfo, data, dataoff,
1081 &pdu->h323_message_body.alerting);
1082 break;
1083 case eH323_UU_PDU_h323_message_body_facility:
1084 ret = process_facility(skb, ct, ctinfo, data, dataoff,
1085 &pdu->h323_message_body.facility);
1086 break;
1087 case eH323_UU_PDU_h323_message_body_progress:
1088 ret = process_progress(skb, ct, ctinfo, data, dataoff,
1089 &pdu->h323_message_body.progress);
1090 break;
1091 default:
1092 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1093 pdu->h323_message_body.choice);
1094 break;
1097 if (ret < 0)
1098 return -1;
1100 if (pdu->options & eH323_UU_PDU_h245Control) {
1101 for (i = 0; i < pdu->h245Control.count; i++) {
1102 ret = process_h245(skb, ct, ctinfo, data, dataoff,
1103 &pdu->h245Control.item[i]);
1104 if (ret < 0)
1105 return -1;
1109 return 0;
1112 /****************************************************************************/
1113 static int q931_help(struct sk_buff *skb, unsigned int protoff,
1114 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1116 static Q931 q931;
1117 unsigned char *data = NULL;
1118 int datalen;
1119 int dataoff;
1120 int ret;
1122 /* Until there's been traffic both ways, don't look in packets. */
1123 if (ctinfo != IP_CT_ESTABLISHED &&
1124 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1125 return NF_ACCEPT;
1127 pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1129 spin_lock_bh(&nf_h323_lock);
1131 /* Process each TPKT */
1132 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1133 &data, &datalen, &dataoff)) {
1134 pr_debug("nf_ct_q931: TPKT len=%d ", datalen);
1135 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1137 /* Decode Q.931 signal */
1138 ret = DecodeQ931(data, datalen, &q931);
1139 if (ret < 0) {
1140 pr_debug("nf_ct_q931: decoding error: %s\n",
1141 ret == H323_ERROR_BOUND ?
1142 "out of bound" : "out of range");
1143 /* We don't drop when decoding error */
1144 break;
1147 /* Process Q.931 signal */
1148 if (process_q931(skb, ct, ctinfo, &data, dataoff, &q931) < 0)
1149 goto drop;
1152 spin_unlock_bh(&nf_h323_lock);
1153 return NF_ACCEPT;
1155 drop:
1156 spin_unlock_bh(&nf_h323_lock);
1157 if (net_ratelimit())
1158 printk("nf_ct_q931: packet dropped\n");
1159 return NF_DROP;
1162 /****************************************************************************/
1163 static const struct nf_conntrack_expect_policy q931_exp_policy = {
1164 /* T.120 and H.245 */
1165 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1166 .timeout = 240,
1169 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1171 .name = "Q.931",
1172 .me = THIS_MODULE,
1173 .tuple.src.l3num = AF_INET,
1174 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1175 .tuple.dst.protonum = IPPROTO_TCP,
1176 .help = q931_help,
1177 .expect_policy = &q931_exp_policy,
1180 .name = "Q.931",
1181 .me = THIS_MODULE,
1182 .tuple.src.l3num = AF_INET6,
1183 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1184 .tuple.dst.protonum = IPPROTO_TCP,
1185 .help = q931_help,
1186 .expect_policy = &q931_exp_policy,
1190 /****************************************************************************/
1191 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1192 int *datalen)
1194 const struct udphdr *uh;
1195 struct udphdr _uh;
1196 int dataoff;
1198 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1199 if (uh == NULL)
1200 return NULL;
1201 dataoff = protoff + sizeof(_uh);
1202 if (dataoff >= skb->len)
1203 return NULL;
1204 *datalen = skb->len - dataoff;
1205 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1208 /****************************************************************************/
1209 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1210 union nf_inet_addr *addr,
1211 __be16 port)
1213 struct net *net = nf_ct_net(ct);
1214 struct nf_conntrack_expect *exp;
1215 struct nf_conntrack_tuple tuple;
1217 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1218 tuple.src.u.tcp.port = 0;
1219 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1220 tuple.dst.u.tcp.port = port;
1221 tuple.dst.protonum = IPPROTO_TCP;
1223 exp = __nf_ct_expect_find(net, &tuple);
1224 if (exp && exp->master == ct)
1225 return exp;
1226 return NULL;
1229 /****************************************************************************/
1230 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1231 unsigned timeout)
1233 if (!exp || !del_timer(&exp->timeout))
1234 return 0;
1236 exp->timeout.expires = jiffies + timeout * HZ;
1237 add_timer(&exp->timeout);
1239 return 1;
1242 /****************************************************************************/
1243 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1244 enum ip_conntrack_info ctinfo,
1245 unsigned char **data,
1246 TransportAddress *taddr, int count)
1248 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1249 int dir = CTINFO2DIR(ctinfo);
1250 int ret = 0;
1251 int i;
1252 __be16 port;
1253 union nf_inet_addr addr;
1254 struct nf_conntrack_expect *exp;
1255 typeof(nat_q931_hook) nat_q931;
1257 /* Look for the first related address */
1258 for (i = 0; i < count; i++) {
1259 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1260 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1261 sizeof(addr)) == 0 && port != 0)
1262 break;
1265 if (i >= count) /* Not found */
1266 return 0;
1268 /* Create expect for Q.931 */
1269 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1270 return -1;
1271 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1272 gkrouted_only ? /* only accept calls from GK? */
1273 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1274 &ct->tuplehash[!dir].tuple.dst.u3,
1275 IPPROTO_TCP, NULL, &port);
1276 exp->helper = nf_conntrack_helper_q931;
1277 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1279 nat_q931 = rcu_dereference(nat_q931_hook);
1280 if (nat_q931 && ct->status & IPS_NAT_MASK) { /* Need NAT */
1281 ret = nat_q931(skb, ct, ctinfo, data, taddr, i, port, exp);
1282 } else { /* Conntrack only */
1283 if (nf_ct_expect_related(exp) == 0) {
1284 pr_debug("nf_ct_ras: expect Q.931 ");
1285 nf_ct_dump_tuple(&exp->tuple);
1287 /* Save port for looking up expect in processing RCF */
1288 info->sig_port[dir] = port;
1289 } else
1290 ret = -1;
1293 nf_ct_expect_put(exp);
1295 return ret;
1298 /****************************************************************************/
1299 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1300 enum ip_conntrack_info ctinfo,
1301 unsigned char **data, GatekeeperRequest *grq)
1303 typeof(set_ras_addr_hook) set_ras_addr;
1305 pr_debug("nf_ct_ras: GRQ\n");
1307 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1308 if (set_ras_addr && ct->status & IPS_NAT_MASK) /* NATed */
1309 return set_ras_addr(skb, ct, ctinfo, data,
1310 &grq->rasAddress, 1);
1311 return 0;
1314 /****************************************************************************/
1315 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1316 enum ip_conntrack_info ctinfo,
1317 unsigned char **data, GatekeeperConfirm *gcf)
1319 int dir = CTINFO2DIR(ctinfo);
1320 int ret = 0;
1321 __be16 port;
1322 union nf_inet_addr addr;
1323 struct nf_conntrack_expect *exp;
1325 pr_debug("nf_ct_ras: GCF\n");
1327 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1328 return 0;
1330 /* Registration port is the same as discovery port */
1331 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1332 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1333 return 0;
1335 /* Avoid RAS expectation loops. A GCF is never expected. */
1336 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1337 return 0;
1339 /* Need new expect */
1340 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1341 return -1;
1342 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1343 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1344 IPPROTO_UDP, NULL, &port);
1345 exp->helper = nf_conntrack_helper_ras;
1347 if (nf_ct_expect_related(exp) == 0) {
1348 pr_debug("nf_ct_ras: expect RAS ");
1349 nf_ct_dump_tuple(&exp->tuple);
1350 } else
1351 ret = -1;
1353 nf_ct_expect_put(exp);
1355 return ret;
1358 /****************************************************************************/
1359 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1360 enum ip_conntrack_info ctinfo,
1361 unsigned char **data, RegistrationRequest *rrq)
1363 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1364 int ret;
1365 typeof(set_ras_addr_hook) set_ras_addr;
1367 pr_debug("nf_ct_ras: RRQ\n");
1369 ret = expect_q931(skb, ct, ctinfo, data,
1370 rrq->callSignalAddress.item,
1371 rrq->callSignalAddress.count);
1372 if (ret < 0)
1373 return -1;
1375 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1376 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1377 ret = set_ras_addr(skb, ct, ctinfo, data,
1378 rrq->rasAddress.item,
1379 rrq->rasAddress.count);
1380 if (ret < 0)
1381 return -1;
1384 if (rrq->options & eRegistrationRequest_timeToLive) {
1385 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1386 info->timeout = rrq->timeToLive;
1387 } else
1388 info->timeout = default_rrq_ttl;
1390 return 0;
1393 /****************************************************************************/
1394 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1395 enum ip_conntrack_info ctinfo,
1396 unsigned char **data, RegistrationConfirm *rcf)
1398 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1399 int dir = CTINFO2DIR(ctinfo);
1400 int ret;
1401 struct nf_conntrack_expect *exp;
1402 typeof(set_sig_addr_hook) set_sig_addr;
1404 pr_debug("nf_ct_ras: RCF\n");
1406 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1407 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1408 ret = set_sig_addr(skb, ct, ctinfo, data,
1409 rcf->callSignalAddress.item,
1410 rcf->callSignalAddress.count);
1411 if (ret < 0)
1412 return -1;
1415 if (rcf->options & eRegistrationConfirm_timeToLive) {
1416 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1417 info->timeout = rcf->timeToLive;
1420 if (info->timeout > 0) {
1421 pr_debug("nf_ct_ras: set RAS connection timeout to "
1422 "%u seconds\n", info->timeout);
1423 nf_ct_refresh(ct, skb, info->timeout * HZ);
1425 /* Set expect timeout */
1426 spin_lock_bh(&nf_conntrack_lock);
1427 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1428 info->sig_port[!dir]);
1429 if (exp) {
1430 pr_debug("nf_ct_ras: set Q.931 expect "
1431 "timeout to %u seconds for",
1432 info->timeout);
1433 nf_ct_dump_tuple(&exp->tuple);
1434 set_expect_timeout(exp, info->timeout);
1436 spin_unlock_bh(&nf_conntrack_lock);
1439 return 0;
1442 /****************************************************************************/
1443 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1444 enum ip_conntrack_info ctinfo,
1445 unsigned char **data, UnregistrationRequest *urq)
1447 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1448 int dir = CTINFO2DIR(ctinfo);
1449 int ret;
1450 typeof(set_sig_addr_hook) set_sig_addr;
1452 pr_debug("nf_ct_ras: URQ\n");
1454 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1455 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1456 ret = set_sig_addr(skb, ct, ctinfo, data,
1457 urq->callSignalAddress.item,
1458 urq->callSignalAddress.count);
1459 if (ret < 0)
1460 return -1;
1463 /* Clear old expect */
1464 nf_ct_remove_expectations(ct);
1465 info->sig_port[dir] = 0;
1466 info->sig_port[!dir] = 0;
1468 /* Give it 30 seconds for UCF or URJ */
1469 nf_ct_refresh(ct, skb, 30 * HZ);
1471 return 0;
1474 /****************************************************************************/
1475 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1476 enum ip_conntrack_info ctinfo,
1477 unsigned char **data, AdmissionRequest *arq)
1479 const struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1480 int dir = CTINFO2DIR(ctinfo);
1481 __be16 port;
1482 union nf_inet_addr addr;
1483 typeof(set_h225_addr_hook) set_h225_addr;
1485 pr_debug("nf_ct_ras: ARQ\n");
1487 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1488 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1489 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1490 &addr, &port) &&
1491 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1492 port == info->sig_port[dir] &&
1493 set_h225_addr && ct->status & IPS_NAT_MASK) {
1494 /* Answering ARQ */
1495 return set_h225_addr(skb, data, 0,
1496 &arq->destCallSignalAddress,
1497 &ct->tuplehash[!dir].tuple.dst.u3,
1498 info->sig_port[!dir]);
1501 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1502 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1503 &addr, &port) &&
1504 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1505 set_h225_addr && ct->status & IPS_NAT_MASK) {
1506 /* Calling ARQ */
1507 return set_h225_addr(skb, data, 0,
1508 &arq->srcCallSignalAddress,
1509 &ct->tuplehash[!dir].tuple.dst.u3,
1510 port);
1513 return 0;
1516 /****************************************************************************/
1517 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1518 enum ip_conntrack_info ctinfo,
1519 unsigned char **data, AdmissionConfirm *acf)
1521 int dir = CTINFO2DIR(ctinfo);
1522 int ret = 0;
1523 __be16 port;
1524 union nf_inet_addr addr;
1525 struct nf_conntrack_expect *exp;
1526 typeof(set_sig_addr_hook) set_sig_addr;
1528 pr_debug("nf_ct_ras: ACF\n");
1530 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1531 &addr, &port))
1532 return 0;
1534 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1535 /* Answering ACF */
1536 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1537 if (set_sig_addr && ct->status & IPS_NAT_MASK)
1538 return set_sig_addr(skb, ct, ctinfo, data,
1539 &acf->destCallSignalAddress, 1);
1540 return 0;
1543 /* Need new expect */
1544 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1545 return -1;
1546 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1547 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1548 IPPROTO_TCP, NULL, &port);
1549 exp->flags = NF_CT_EXPECT_PERMANENT;
1550 exp->helper = nf_conntrack_helper_q931;
1552 if (nf_ct_expect_related(exp) == 0) {
1553 pr_debug("nf_ct_ras: expect Q.931 ");
1554 nf_ct_dump_tuple(&exp->tuple);
1555 } else
1556 ret = -1;
1558 nf_ct_expect_put(exp);
1560 return ret;
1563 /****************************************************************************/
1564 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1565 enum ip_conntrack_info ctinfo,
1566 unsigned char **data, LocationRequest *lrq)
1568 typeof(set_ras_addr_hook) set_ras_addr;
1570 pr_debug("nf_ct_ras: LRQ\n");
1572 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1573 if (set_ras_addr && ct->status & IPS_NAT_MASK)
1574 return set_ras_addr(skb, ct, ctinfo, data,
1575 &lrq->replyAddress, 1);
1576 return 0;
1579 /****************************************************************************/
1580 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1581 enum ip_conntrack_info ctinfo,
1582 unsigned char **data, LocationConfirm *lcf)
1584 int dir = CTINFO2DIR(ctinfo);
1585 int ret = 0;
1586 __be16 port;
1587 union nf_inet_addr addr;
1588 struct nf_conntrack_expect *exp;
1590 pr_debug("nf_ct_ras: LCF\n");
1592 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1593 &addr, &port))
1594 return 0;
1596 /* Need new expect for call signal */
1597 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1598 return -1;
1599 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1600 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1601 IPPROTO_TCP, NULL, &port);
1602 exp->flags = NF_CT_EXPECT_PERMANENT;
1603 exp->helper = nf_conntrack_helper_q931;
1605 if (nf_ct_expect_related(exp) == 0) {
1606 pr_debug("nf_ct_ras: expect Q.931 ");
1607 nf_ct_dump_tuple(&exp->tuple);
1608 } else
1609 ret = -1;
1611 nf_ct_expect_put(exp);
1613 /* Ignore rasAddress */
1615 return ret;
1618 /****************************************************************************/
1619 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1620 enum ip_conntrack_info ctinfo,
1621 unsigned char **data, InfoRequestResponse *irr)
1623 int ret;
1624 typeof(set_ras_addr_hook) set_ras_addr;
1625 typeof(set_sig_addr_hook) set_sig_addr;
1627 pr_debug("nf_ct_ras: IRR\n");
1629 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1630 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1631 ret = set_ras_addr(skb, ct, ctinfo, data,
1632 &irr->rasAddress, 1);
1633 if (ret < 0)
1634 return -1;
1637 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1638 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1639 ret = set_sig_addr(skb, ct, ctinfo, data,
1640 irr->callSignalAddress.item,
1641 irr->callSignalAddress.count);
1642 if (ret < 0)
1643 return -1;
1646 return 0;
1649 /****************************************************************************/
1650 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1651 enum ip_conntrack_info ctinfo,
1652 unsigned char **data, RasMessage *ras)
1654 switch (ras->choice) {
1655 case eRasMessage_gatekeeperRequest:
1656 return process_grq(skb, ct, ctinfo, data,
1657 &ras->gatekeeperRequest);
1658 case eRasMessage_gatekeeperConfirm:
1659 return process_gcf(skb, ct, ctinfo, data,
1660 &ras->gatekeeperConfirm);
1661 case eRasMessage_registrationRequest:
1662 return process_rrq(skb, ct, ctinfo, data,
1663 &ras->registrationRequest);
1664 case eRasMessage_registrationConfirm:
1665 return process_rcf(skb, ct, ctinfo, data,
1666 &ras->registrationConfirm);
1667 case eRasMessage_unregistrationRequest:
1668 return process_urq(skb, ct, ctinfo, data,
1669 &ras->unregistrationRequest);
1670 case eRasMessage_admissionRequest:
1671 return process_arq(skb, ct, ctinfo, data,
1672 &ras->admissionRequest);
1673 case eRasMessage_admissionConfirm:
1674 return process_acf(skb, ct, ctinfo, data,
1675 &ras->admissionConfirm);
1676 case eRasMessage_locationRequest:
1677 return process_lrq(skb, ct, ctinfo, data,
1678 &ras->locationRequest);
1679 case eRasMessage_locationConfirm:
1680 return process_lcf(skb, ct, ctinfo, data,
1681 &ras->locationConfirm);
1682 case eRasMessage_infoRequestResponse:
1683 return process_irr(skb, ct, ctinfo, data,
1684 &ras->infoRequestResponse);
1685 default:
1686 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1687 break;
1690 return 0;
1693 /****************************************************************************/
1694 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1695 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1697 static RasMessage ras;
1698 unsigned char *data;
1699 int datalen = 0;
1700 int ret;
1702 pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1704 spin_lock_bh(&nf_h323_lock);
1706 /* Get UDP data */
1707 data = get_udp_data(skb, protoff, &datalen);
1708 if (data == NULL)
1709 goto accept;
1710 pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1711 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1713 /* Decode RAS message */
1714 ret = DecodeRasMessage(data, datalen, &ras);
1715 if (ret < 0) {
1716 pr_debug("nf_ct_ras: decoding error: %s\n",
1717 ret == H323_ERROR_BOUND ?
1718 "out of bound" : "out of range");
1719 goto accept;
1722 /* Process RAS message */
1723 if (process_ras(skb, ct, ctinfo, &data, &ras) < 0)
1724 goto drop;
1726 accept:
1727 spin_unlock_bh(&nf_h323_lock);
1728 return NF_ACCEPT;
1730 drop:
1731 spin_unlock_bh(&nf_h323_lock);
1732 if (net_ratelimit())
1733 printk("nf_ct_ras: packet dropped\n");
1734 return NF_DROP;
1737 /****************************************************************************/
1738 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1739 .max_expected = 32,
1740 .timeout = 240,
1743 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1745 .name = "RAS",
1746 .me = THIS_MODULE,
1747 .tuple.src.l3num = AF_INET,
1748 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1749 .tuple.dst.protonum = IPPROTO_UDP,
1750 .help = ras_help,
1751 .expect_policy = &ras_exp_policy,
1754 .name = "RAS",
1755 .me = THIS_MODULE,
1756 .tuple.src.l3num = AF_INET6,
1757 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1758 .tuple.dst.protonum = IPPROTO_UDP,
1759 .help = ras_help,
1760 .expect_policy = &ras_exp_policy,
1764 /****************************************************************************/
1765 static void __exit nf_conntrack_h323_fini(void)
1767 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1768 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1769 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1770 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1771 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1772 kfree(h323_buffer);
1773 pr_debug("nf_ct_h323: fini\n");
1776 /****************************************************************************/
1777 static int __init nf_conntrack_h323_init(void)
1779 int ret;
1781 h323_buffer = kmalloc(65536, GFP_KERNEL);
1782 if (!h323_buffer)
1783 return -ENOMEM;
1784 ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1785 if (ret < 0)
1786 goto err1;
1787 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1788 if (ret < 0)
1789 goto err2;
1790 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1791 if (ret < 0)
1792 goto err3;
1793 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1794 if (ret < 0)
1795 goto err4;
1796 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1797 if (ret < 0)
1798 goto err5;
1799 pr_debug("nf_ct_h323: init success\n");
1800 return 0;
1802 err5:
1803 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1804 err4:
1805 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1806 err3:
1807 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1808 err2:
1809 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1810 err1:
1811 kfree(h323_buffer);
1812 return ret;
1815 /****************************************************************************/
1816 module_init(nf_conntrack_h323_init);
1817 module_exit(nf_conntrack_h323_fini);
1819 EXPORT_SYMBOL_GPL(get_h225_addr);
1820 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1821 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1822 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1823 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1824 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1825 EXPORT_SYMBOL_GPL(nat_t120_hook);
1826 EXPORT_SYMBOL_GPL(nat_h245_hook);
1827 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1828 EXPORT_SYMBOL_GPL(nat_q931_hook);
1830 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1831 MODULE_DESCRIPTION("H.323 connection tracking helper");
1832 MODULE_LICENSE("GPL");
1833 MODULE_ALIAS("ip_conntrack_h323");