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>
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>
37 #define DEBUGP(format, args...)
41 static unsigned int default_rrq_ttl __read_mostly = 300;
42 module_param(default_rrq_ttl, uint, 0600);
43 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
45 static int gkrouted_only __read_mostly = 1;
46 module_param(gkrouted_only, int, 0600);
47 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
49 static int callforward_filter __read_mostly = 1;
50 module_param(callforward_filter, bool, 0600);
51 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
52 "if both endpoints are on different sides "
53 "(determined by routing information)");
56 int (*set_h245_addr_hook) (struct sk_buff **pskb,
57 unsigned char **data, int dataoff,
58 H245_TransportAddress *taddr,
59 union nf_conntrack_address *addr, __be16 port)
61 int (*set_h225_addr_hook) (struct sk_buff **pskb,
62 unsigned char **data, int dataoff,
63 TransportAddress *taddr,
64 union nf_conntrack_address *addr, __be16 port)
66 int (*set_sig_addr_hook) (struct sk_buff **pskb,
68 enum ip_conntrack_info ctinfo,
70 TransportAddress *taddr, int count) __read_mostly;
71 int (*set_ras_addr_hook) (struct sk_buff **pskb,
73 enum ip_conntrack_info ctinfo,
75 TransportAddress *taddr, int count) __read_mostly;
76 int (*nat_rtp_rtcp_hook) (struct sk_buff **pskb,
78 enum ip_conntrack_info ctinfo,
79 unsigned char **data, int dataoff,
80 H245_TransportAddress *taddr,
81 __be16 port, __be16 rtp_port,
82 struct nf_conntrack_expect *rtp_exp,
83 struct nf_conntrack_expect *rtcp_exp) __read_mostly;
84 int (*nat_t120_hook) (struct sk_buff **pskb,
86 enum ip_conntrack_info ctinfo,
87 unsigned char **data, int dataoff,
88 H245_TransportAddress *taddr, __be16 port,
89 struct nf_conntrack_expect *exp) __read_mostly;
90 int (*nat_h245_hook) (struct sk_buff **pskb,
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_callforwarding_hook) (struct sk_buff **pskb,
98 enum ip_conntrack_info ctinfo,
99 unsigned char **data, int dataoff,
100 TransportAddress *taddr, __be16 port,
101 struct nf_conntrack_expect *exp) __read_mostly;
102 int (*nat_q931_hook) (struct sk_buff **pskb,
104 enum ip_conntrack_info ctinfo,
105 unsigned char **data, TransportAddress *taddr, int idx,
106 __be16 port, struct nf_conntrack_expect *exp)
109 static DEFINE_SPINLOCK(nf_h323_lock);
110 static char *h323_buffer;
112 static struct nf_conntrack_helper nf_conntrack_helper_h245;
113 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
114 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
116 /****************************************************************************/
117 static int get_tpkt_data(struct sk_buff **pskb, unsigned int protoff,
118 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
119 unsigned char **data, int *datalen, int *dataoff)
121 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
122 int dir = CTINFO2DIR(ctinfo);
123 struct tcphdr _tcph, *th;
131 th = skb_header_pointer(*pskb, protoff, sizeof(_tcph), &_tcph);
135 /* Get TCP data offset */
136 tcpdataoff = protoff + th->doff * 4;
138 /* Get TCP data length */
139 tcpdatalen = (*pskb)->len - tcpdataoff;
140 if (tcpdatalen <= 0) /* No TCP data */
143 if (*data == NULL) { /* first TPKT */
144 /* Get first TPKT pointer */
145 tpkt = skb_header_pointer(*pskb, tcpdataoff, tcpdatalen,
147 BUG_ON(tpkt == NULL);
149 /* Validate TPKT identifier */
150 if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) {
151 /* Netmeeting sends TPKT header and data separately */
152 if (info->tpkt_len[dir] > 0) {
153 DEBUGP("nf_ct_h323: previous packet "
154 "indicated separate TPKT data of %hu "
155 "bytes\n", info->tpkt_len[dir]);
156 if (info->tpkt_len[dir] <= tcpdatalen) {
157 /* Yes, there was a TPKT header
160 *datalen = info->tpkt_len[dir];
165 /* Fragmented TPKT */
166 DEBUGP("nf_ct_h323: fragmented TPKT\n");
170 /* It is not even a TPKT */
174 } else { /* Next TPKT */
175 tpktoff = *dataoff + *datalen;
176 tcpdatalen -= tpktoff;
177 if (tcpdatalen <= 4) /* No more TPKT */
179 tpkt = *data + *datalen;
181 /* Validate TPKT identifier */
182 if (tpkt[0] != 0x03 || tpkt[1] != 0)
186 /* Validate TPKT length */
187 tpktlen = tpkt[2] * 256 + tpkt[3];
190 if (tpktlen > tcpdatalen) {
191 if (tcpdatalen == 4) { /* Separate TPKT header */
192 /* Netmeeting sends TPKT header and data separately */
193 DEBUGP("nf_ct_h323: separate TPKT header indicates "
194 "there will be TPKT data of %hu bytes\n",
196 info->tpkt_len[dir] = tpktlen - 4;
201 printk("nf_ct_h323: incomplete TPKT (fragmented?)\n");
205 /* This is the encapsulated data */
207 *datalen = tpktlen - 4;
208 *dataoff = tpktoff + 4;
211 /* Clear TPKT length */
212 info->tpkt_len[dir] = 0;
216 info->tpkt_len[dir] = 0;
220 /****************************************************************************/
221 static int get_h245_addr(struct nf_conn *ct, unsigned char *data,
222 H245_TransportAddress *taddr,
223 union nf_conntrack_address *addr, __be16 *port)
226 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
229 if (taddr->choice != eH245_TransportAddress_unicastAddress)
232 switch (taddr->unicastAddress.choice) {
233 case eUnicastAddress_iPAddress:
234 if (family != AF_INET)
236 p = data + taddr->unicastAddress.iPAddress.network;
239 case eUnicastAddress_iP6Address:
240 if (family != AF_INET6)
242 p = data + taddr->unicastAddress.iP6Address.network;
249 memcpy(addr, p, len);
250 memset((void *)addr + len, 0, sizeof(*addr) - len);
251 memcpy(port, p + len, sizeof(__be16));
256 /****************************************************************************/
257 static int expect_rtp_rtcp(struct sk_buff **pskb, struct nf_conn *ct,
258 enum ip_conntrack_info ctinfo,
259 unsigned char **data, int dataoff,
260 H245_TransportAddress *taddr)
262 int dir = CTINFO2DIR(ctinfo);
265 __be16 rtp_port, rtcp_port;
266 union nf_conntrack_address addr;
267 struct nf_conntrack_expect *rtp_exp;
268 struct nf_conntrack_expect *rtcp_exp;
269 typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
271 /* Read RTP or RTCP address */
272 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
273 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
277 /* RTP port is even */
280 rtcp_port = htons(ntohs(port) + 1);
282 /* Create expect for RTP */
283 if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
285 nf_ct_expect_init(rtp_exp, ct->tuplehash[!dir].tuple.src.l3num,
286 &ct->tuplehash[!dir].tuple.src.u3,
287 &ct->tuplehash[!dir].tuple.dst.u3,
288 IPPROTO_UDP, NULL, &rtp_port);
290 /* Create expect for RTCP */
291 if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
292 nf_ct_expect_put(rtp_exp);
295 nf_ct_expect_init(rtcp_exp, ct->tuplehash[!dir].tuple.src.l3num,
296 &ct->tuplehash[!dir].tuple.src.u3,
297 &ct->tuplehash[!dir].tuple.dst.u3,
298 IPPROTO_UDP, NULL, &rtcp_port);
300 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
301 &ct->tuplehash[!dir].tuple.dst.u3,
302 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
303 (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
304 ct->status & IPS_NAT_MASK) {
306 ret = nat_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
307 taddr, port, rtp_port, rtp_exp, rtcp_exp);
308 } else { /* Conntrack only */
309 if (nf_ct_expect_related(rtp_exp) == 0) {
310 if (nf_ct_expect_related(rtcp_exp) == 0) {
311 DEBUGP("nf_ct_h323: expect RTP ");
312 NF_CT_DUMP_TUPLE(&rtp_exp->tuple);
313 DEBUGP("nf_ct_h323: expect RTCP ");
314 NF_CT_DUMP_TUPLE(&rtcp_exp->tuple);
316 nf_ct_unexpect_related(rtp_exp);
323 nf_ct_expect_put(rtp_exp);
324 nf_ct_expect_put(rtcp_exp);
329 /****************************************************************************/
330 static int expect_t120(struct sk_buff **pskb,
332 enum ip_conntrack_info ctinfo,
333 unsigned char **data, int dataoff,
334 H245_TransportAddress *taddr)
336 int dir = CTINFO2DIR(ctinfo);
339 union nf_conntrack_address addr;
340 struct nf_conntrack_expect *exp;
341 typeof(nat_t120_hook) nat_t120;
343 /* Read T.120 address */
344 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
345 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
349 /* Create expect for T.120 connections */
350 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
352 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
353 &ct->tuplehash[!dir].tuple.src.u3,
354 &ct->tuplehash[!dir].tuple.dst.u3,
355 IPPROTO_TCP, NULL, &port);
356 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple channels */
358 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
359 &ct->tuplehash[!dir].tuple.dst.u3,
360 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
361 (nat_t120 = rcu_dereference(nat_t120_hook)) &&
362 ct->status & IPS_NAT_MASK) {
364 ret = nat_t120(pskb, ct, ctinfo, data, dataoff, taddr,
366 } else { /* Conntrack only */
367 if (nf_ct_expect_related(exp) == 0) {
368 DEBUGP("nf_ct_h323: expect T.120 ");
369 NF_CT_DUMP_TUPLE(&exp->tuple);
374 nf_ct_expect_put(exp);
379 /****************************************************************************/
380 static int process_h245_channel(struct sk_buff **pskb,
382 enum ip_conntrack_info ctinfo,
383 unsigned char **data, int dataoff,
384 H2250LogicalChannelParameters *channel)
388 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
390 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
391 &channel->mediaChannel);
397 options & eH2250LogicalChannelParameters_mediaControlChannel) {
399 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
400 &channel->mediaControlChannel);
408 /****************************************************************************/
409 static int process_olc(struct sk_buff **pskb, struct nf_conn *ct,
410 enum ip_conntrack_info ctinfo,
411 unsigned char **data, int dataoff,
412 OpenLogicalChannel *olc)
416 DEBUGP("nf_ct_h323: OpenLogicalChannel\n");
418 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
419 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
421 ret = process_h245_channel(pskb, ct, ctinfo, data, dataoff,
423 forwardLogicalChannelParameters.
425 h2250LogicalChannelParameters);
431 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
432 (olc->reverseLogicalChannelParameters.options &
433 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
434 && (olc->reverseLogicalChannelParameters.multiplexParameters.
436 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
439 process_h245_channel(pskb, ct, ctinfo, data, dataoff,
441 reverseLogicalChannelParameters.
443 h2250LogicalChannelParameters);
448 if ((olc->options & eOpenLogicalChannel_separateStack) &&
449 olc->forwardLogicalChannelParameters.dataType.choice ==
451 olc->forwardLogicalChannelParameters.dataType.data.application.
452 choice == eDataApplicationCapability_application_t120 &&
453 olc->forwardLogicalChannelParameters.dataType.data.application.
454 t120.choice == eDataProtocolCapability_separateLANStack &&
455 olc->separateStack.networkAddress.choice ==
456 eNetworkAccessParameters_networkAddress_localAreaAddress) {
457 ret = expect_t120(pskb, ct, ctinfo, data, dataoff,
458 &olc->separateStack.networkAddress.
467 /****************************************************************************/
468 static int process_olca(struct sk_buff **pskb, struct nf_conn *ct,
469 enum ip_conntrack_info ctinfo,
470 unsigned char **data, int dataoff,
471 OpenLogicalChannelAck *olca)
473 H2250LogicalChannelAckParameters *ack;
476 DEBUGP("nf_ct_h323: OpenLogicalChannelAck\n");
479 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
480 (olca->reverseLogicalChannelParameters.options &
481 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
482 && (olca->reverseLogicalChannelParameters.multiplexParameters.
484 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
486 ret = process_h245_channel(pskb, ct, ctinfo, data, dataoff,
488 reverseLogicalChannelParameters.
490 h2250LogicalChannelParameters);
496 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
497 (olca->forwardMultiplexAckParameters.choice ==
498 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
500 ack = &olca->forwardMultiplexAckParameters.
501 h2250LogicalChannelAckParameters;
503 eH2250LogicalChannelAckParameters_mediaChannel) {
505 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
512 eH2250LogicalChannelAckParameters_mediaControlChannel) {
514 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
515 &ack->mediaControlChannel);
521 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
522 olca->separateStack.networkAddress.choice ==
523 eNetworkAccessParameters_networkAddress_localAreaAddress) {
524 ret = expect_t120(pskb, ct, ctinfo, data, dataoff,
525 &olca->separateStack.networkAddress.
534 /****************************************************************************/
535 static int process_h245(struct sk_buff **pskb, struct nf_conn *ct,
536 enum ip_conntrack_info ctinfo,
537 unsigned char **data, int dataoff,
538 MultimediaSystemControlMessage *mscm)
540 switch (mscm->choice) {
541 case eMultimediaSystemControlMessage_request:
542 if (mscm->request.choice ==
543 eRequestMessage_openLogicalChannel) {
544 return process_olc(pskb, ct, ctinfo, data, dataoff,
545 &mscm->request.openLogicalChannel);
547 DEBUGP("nf_ct_h323: H.245 Request %d\n",
548 mscm->request.choice);
550 case eMultimediaSystemControlMessage_response:
551 if (mscm->response.choice ==
552 eResponseMessage_openLogicalChannelAck) {
553 return process_olca(pskb, ct, ctinfo, data, dataoff,
555 openLogicalChannelAck);
557 DEBUGP("nf_ct_h323: H.245 Response %d\n",
558 mscm->response.choice);
561 DEBUGP("nf_ct_h323: H.245 signal %d\n", mscm->choice);
568 /****************************************************************************/
569 static int h245_help(struct sk_buff **pskb, unsigned int protoff,
570 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
572 static MultimediaSystemControlMessage mscm;
573 unsigned char *data = NULL;
578 /* Until there's been traffic both ways, don't look in packets. */
579 if (ctinfo != IP_CT_ESTABLISHED &&
580 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
583 DEBUGP("nf_ct_h245: skblen = %u\n", (*pskb)->len);
585 spin_lock_bh(&nf_h323_lock);
587 /* Process each TPKT */
588 while (get_tpkt_data(pskb, protoff, ct, ctinfo,
589 &data, &datalen, &dataoff)) {
590 DEBUGP("nf_ct_h245: TPKT len=%d ", datalen);
591 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
593 /* Decode H.245 signal */
594 ret = DecodeMultimediaSystemControlMessage(data, datalen,
597 DEBUGP("nf_ct_h245: decoding error: %s\n",
598 ret == H323_ERROR_BOUND ?
599 "out of bound" : "out of range");
600 /* We don't drop when decoding error */
604 /* Process H.245 signal */
605 if (process_h245(pskb, ct, ctinfo, &data, dataoff, &mscm) < 0)
609 spin_unlock_bh(&nf_h323_lock);
613 spin_unlock_bh(&nf_h323_lock);
615 printk("nf_ct_h245: packet dropped\n");
619 /****************************************************************************/
620 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
623 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
625 .tuple.dst.protonum = IPPROTO_UDP,
629 /****************************************************************************/
630 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
631 TransportAddress *taddr,
632 union nf_conntrack_address *addr, __be16 *port)
635 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
638 switch (taddr->choice) {
639 case eTransportAddress_ipAddress:
640 if (family != AF_INET)
642 p = data + taddr->ipAddress.ip;
645 case eTransportAddress_ip6Address:
646 if (family != AF_INET6)
648 p = data + taddr->ip6Address.ip;
655 memcpy(addr, p, len);
656 memset((void *)addr + len, 0, sizeof(*addr) - len);
657 memcpy(port, p + len, sizeof(__be16));
662 /****************************************************************************/
663 static int expect_h245(struct sk_buff **pskb, struct nf_conn *ct,
664 enum ip_conntrack_info ctinfo,
665 unsigned char **data, int dataoff,
666 TransportAddress *taddr)
668 int dir = CTINFO2DIR(ctinfo);
671 union nf_conntrack_address addr;
672 struct nf_conntrack_expect *exp;
673 typeof(nat_h245_hook) nat_h245;
675 /* Read h245Address */
676 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
677 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
681 /* Create expect for h245 connection */
682 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
684 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
685 &ct->tuplehash[!dir].tuple.src.u3,
686 &ct->tuplehash[!dir].tuple.dst.u3,
687 IPPROTO_TCP, NULL, &port);
688 exp->helper = &nf_conntrack_helper_h245;
690 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
691 &ct->tuplehash[!dir].tuple.dst.u3,
692 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
693 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
694 ct->status & IPS_NAT_MASK) {
696 ret = nat_h245(pskb, ct, ctinfo, data, dataoff, taddr,
698 } else { /* Conntrack only */
699 if (nf_ct_expect_related(exp) == 0) {
700 DEBUGP("nf_ct_q931: expect H.245 ");
701 NF_CT_DUMP_TUPLE(&exp->tuple);
706 nf_ct_expect_put(exp);
711 /* If the calling party is on the same side of the forward-to party,
712 * we don't need to track the second call */
713 static int callforward_do_filter(union nf_conntrack_address *src,
714 union nf_conntrack_address *dst,
717 struct flowi fl1, fl2;
720 memset(&fl1, 0, sizeof(fl1));
721 memset(&fl2, 0, sizeof(fl2));
725 struct rtable *rt1, *rt2;
727 fl1.fl4_dst = src->ip;
728 fl2.fl4_dst = dst->ip;
729 if (ip_route_output_key(&rt1, &fl1) == 0) {
730 if (ip_route_output_key(&rt2, &fl2) == 0) {
731 if (rt1->rt_gateway == rt2->rt_gateway &&
732 rt1->u.dst.dev == rt2->u.dst.dev)
734 dst_release(&rt2->u.dst);
736 dst_release(&rt1->u.dst);
740 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
742 struct rt6_info *rt1, *rt2;
744 memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst));
745 memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst));
746 rt1 = (struct rt6_info *)ip6_route_output(NULL, &fl1);
748 rt2 = (struct rt6_info *)ip6_route_output(NULL, &fl2);
750 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
751 sizeof(rt1->rt6i_gateway)) &&
752 rt1->u.dst.dev == rt2->u.dst.dev)
754 dst_release(&rt2->u.dst);
756 dst_release(&rt1->u.dst);
766 /****************************************************************************/
767 static int expect_callforwarding(struct sk_buff **pskb,
769 enum ip_conntrack_info ctinfo,
770 unsigned char **data, int dataoff,
771 TransportAddress *taddr)
773 int dir = CTINFO2DIR(ctinfo);
776 union nf_conntrack_address addr;
777 struct nf_conntrack_expect *exp;
778 typeof(nat_callforwarding_hook) nat_callforwarding;
780 /* Read alternativeAddress */
781 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
784 /* If the calling party is on the same side of the forward-to party,
785 * we don't need to track the second call */
786 if (callforward_filter &&
787 callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
788 ct->tuplehash[!dir].tuple.src.l3num)) {
789 DEBUGP("nf_ct_q931: Call Forwarding not tracked\n");
793 /* Create expect for the second call leg */
794 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
796 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
797 &ct->tuplehash[!dir].tuple.src.u3, &addr,
798 IPPROTO_TCP, NULL, &port);
799 exp->helper = nf_conntrack_helper_q931;
801 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
802 &ct->tuplehash[!dir].tuple.dst.u3,
803 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
804 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
805 ct->status & IPS_NAT_MASK) {
807 ret = nat_callforwarding(pskb, ct, ctinfo, data, dataoff,
809 } else { /* Conntrack only */
810 if (nf_ct_expect_related(exp) == 0) {
811 DEBUGP("nf_ct_q931: expect Call Forwarding ");
812 NF_CT_DUMP_TUPLE(&exp->tuple);
817 nf_ct_expect_put(exp);
822 /****************************************************************************/
823 static int process_setup(struct sk_buff **pskb, struct nf_conn *ct,
824 enum ip_conntrack_info ctinfo,
825 unsigned char **data, int dataoff,
828 int dir = CTINFO2DIR(ctinfo);
832 union nf_conntrack_address addr;
833 typeof(set_h225_addr_hook) set_h225_addr;
835 DEBUGP("nf_ct_q931: Setup\n");
837 if (setup->options & eSetup_UUIE_h245Address) {
838 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
839 &setup->h245Address);
844 set_h225_addr = rcu_dereference(set_h225_addr_hook);
845 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
846 (set_h225_addr) && ct->status && IPS_NAT_MASK &&
847 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
849 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
850 DEBUGP("nf_ct_q931: set destCallSignalAddress "
851 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
852 NIP6(*(struct in6_addr *)&addr), ntohs(port),
853 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.src.u3),
854 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
855 ret = set_h225_addr(pskb, data, dataoff,
856 &setup->destCallSignalAddress,
857 &ct->tuplehash[!dir].tuple.src.u3,
858 ct->tuplehash[!dir].tuple.src.u.tcp.port);
863 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
864 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
865 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
867 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
868 DEBUGP("nf_ct_q931: set sourceCallSignalAddress "
869 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
870 NIP6(*(struct in6_addr *)&addr), ntohs(port),
871 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.dst.u3),
872 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
873 ret = set_h225_addr(pskb, data, dataoff,
874 &setup->sourceCallSignalAddress,
875 &ct->tuplehash[!dir].tuple.dst.u3,
876 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
881 if (setup->options & eSetup_UUIE_fastStart) {
882 for (i = 0; i < setup->fastStart.count; i++) {
883 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
884 &setup->fastStart.item[i]);
893 /****************************************************************************/
894 static int process_callproceeding(struct sk_buff **pskb,
896 enum ip_conntrack_info ctinfo,
897 unsigned char **data, int dataoff,
898 CallProceeding_UUIE *callproc)
903 DEBUGP("nf_ct_q931: CallProceeding\n");
905 if (callproc->options & eCallProceeding_UUIE_h245Address) {
906 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
907 &callproc->h245Address);
912 if (callproc->options & eCallProceeding_UUIE_fastStart) {
913 for (i = 0; i < callproc->fastStart.count; i++) {
914 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
915 &callproc->fastStart.item[i]);
924 /****************************************************************************/
925 static int process_connect(struct sk_buff **pskb, struct nf_conn *ct,
926 enum ip_conntrack_info ctinfo,
927 unsigned char **data, int dataoff,
928 Connect_UUIE *connect)
933 DEBUGP("nf_ct_q931: Connect\n");
935 if (connect->options & eConnect_UUIE_h245Address) {
936 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
937 &connect->h245Address);
942 if (connect->options & eConnect_UUIE_fastStart) {
943 for (i = 0; i < connect->fastStart.count; i++) {
944 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
945 &connect->fastStart.item[i]);
954 /****************************************************************************/
955 static int process_alerting(struct sk_buff **pskb, struct nf_conn *ct,
956 enum ip_conntrack_info ctinfo,
957 unsigned char **data, int dataoff,
958 Alerting_UUIE *alert)
963 DEBUGP("nf_ct_q931: Alerting\n");
965 if (alert->options & eAlerting_UUIE_h245Address) {
966 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
967 &alert->h245Address);
972 if (alert->options & eAlerting_UUIE_fastStart) {
973 for (i = 0; i < alert->fastStart.count; i++) {
974 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
975 &alert->fastStart.item[i]);
984 /****************************************************************************/
985 static int process_facility(struct sk_buff **pskb, struct nf_conn *ct,
986 enum ip_conntrack_info ctinfo,
987 unsigned char **data, int dataoff,
988 Facility_UUIE *facility)
993 DEBUGP("nf_ct_q931: Facility\n");
995 if (facility->reason.choice == eFacilityReason_callForwarded) {
996 if (facility->options & eFacility_UUIE_alternativeAddress)
997 return expect_callforwarding(pskb, ct, ctinfo, data,
1000 alternativeAddress);
1004 if (facility->options & eFacility_UUIE_h245Address) {
1005 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
1006 &facility->h245Address);
1011 if (facility->options & eFacility_UUIE_fastStart) {
1012 for (i = 0; i < facility->fastStart.count; i++) {
1013 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
1014 &facility->fastStart.item[i]);
1023 /****************************************************************************/
1024 static int process_progress(struct sk_buff **pskb, struct nf_conn *ct,
1025 enum ip_conntrack_info ctinfo,
1026 unsigned char **data, int dataoff,
1027 Progress_UUIE *progress)
1032 DEBUGP("nf_ct_q931: Progress\n");
1034 if (progress->options & eProgress_UUIE_h245Address) {
1035 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
1036 &progress->h245Address);
1041 if (progress->options & eProgress_UUIE_fastStart) {
1042 for (i = 0; i < progress->fastStart.count; i++) {
1043 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
1044 &progress->fastStart.item[i]);
1053 /****************************************************************************/
1054 static int process_q931(struct sk_buff **pskb, struct nf_conn *ct,
1055 enum ip_conntrack_info ctinfo,
1056 unsigned char **data, int dataoff, Q931 *q931)
1058 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1062 switch (pdu->h323_message_body.choice) {
1063 case eH323_UU_PDU_h323_message_body_setup:
1064 ret = process_setup(pskb, ct, ctinfo, data, dataoff,
1065 &pdu->h323_message_body.setup);
1067 case eH323_UU_PDU_h323_message_body_callProceeding:
1068 ret = process_callproceeding(pskb, ct, ctinfo, data, dataoff,
1069 &pdu->h323_message_body.
1072 case eH323_UU_PDU_h323_message_body_connect:
1073 ret = process_connect(pskb, ct, ctinfo, data, dataoff,
1074 &pdu->h323_message_body.connect);
1076 case eH323_UU_PDU_h323_message_body_alerting:
1077 ret = process_alerting(pskb, ct, ctinfo, data, dataoff,
1078 &pdu->h323_message_body.alerting);
1080 case eH323_UU_PDU_h323_message_body_facility:
1081 ret = process_facility(pskb, ct, ctinfo, data, dataoff,
1082 &pdu->h323_message_body.facility);
1084 case eH323_UU_PDU_h323_message_body_progress:
1085 ret = process_progress(pskb, ct, ctinfo, data, dataoff,
1086 &pdu->h323_message_body.progress);
1089 DEBUGP("nf_ct_q931: Q.931 signal %d\n",
1090 pdu->h323_message_body.choice);
1097 if (pdu->options & eH323_UU_PDU_h245Control) {
1098 for (i = 0; i < pdu->h245Control.count; i++) {
1099 ret = process_h245(pskb, ct, ctinfo, data, dataoff,
1100 &pdu->h245Control.item[i]);
1109 /****************************************************************************/
1110 static int q931_help(struct sk_buff **pskb, unsigned int protoff,
1111 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1114 unsigned char *data = NULL;
1119 /* Until there's been traffic both ways, don't look in packets. */
1120 if (ctinfo != IP_CT_ESTABLISHED &&
1121 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1124 DEBUGP("nf_ct_q931: skblen = %u\n", (*pskb)->len);
1126 spin_lock_bh(&nf_h323_lock);
1128 /* Process each TPKT */
1129 while (get_tpkt_data(pskb, protoff, ct, ctinfo,
1130 &data, &datalen, &dataoff)) {
1131 DEBUGP("nf_ct_q931: TPKT len=%d ", datalen);
1132 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1134 /* Decode Q.931 signal */
1135 ret = DecodeQ931(data, datalen, &q931);
1137 DEBUGP("nf_ct_q931: decoding error: %s\n",
1138 ret == H323_ERROR_BOUND ?
1139 "out of bound" : "out of range");
1140 /* We don't drop when decoding error */
1144 /* Process Q.931 signal */
1145 if (process_q931(pskb, ct, ctinfo, &data, dataoff, &q931) < 0)
1149 spin_unlock_bh(&nf_h323_lock);
1153 spin_unlock_bh(&nf_h323_lock);
1154 if (net_ratelimit())
1155 printk("nf_ct_q931: packet dropped\n");
1159 /****************************************************************************/
1160 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1164 /* T.120 and H.245 */
1165 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1167 .tuple.src.l3num = AF_INET,
1168 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1169 .tuple.dst.protonum = IPPROTO_TCP,
1175 /* T.120 and H.245 */
1176 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1178 .tuple.src.l3num = AF_INET6,
1179 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1180 .tuple.dst.protonum = IPPROTO_TCP,
1185 /****************************************************************************/
1186 static unsigned char *get_udp_data(struct sk_buff **pskb, unsigned int protoff,
1189 struct udphdr _uh, *uh;
1192 uh = skb_header_pointer(*pskb, protoff, sizeof(_uh), &_uh);
1195 dataoff = protoff + sizeof(_uh);
1196 if (dataoff >= (*pskb)->len)
1198 *datalen = (*pskb)->len - dataoff;
1199 return skb_header_pointer(*pskb, dataoff, *datalen, h323_buffer);
1202 /****************************************************************************/
1203 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1204 union nf_conntrack_address *addr,
1207 struct nf_conntrack_expect *exp;
1208 struct nf_conntrack_tuple tuple;
1210 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1211 tuple.src.u.tcp.port = 0;
1212 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1213 tuple.dst.u.tcp.port = port;
1214 tuple.dst.protonum = IPPROTO_TCP;
1216 exp = __nf_ct_expect_find(&tuple);
1217 if (exp && exp->master == ct)
1222 /****************************************************************************/
1223 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1226 if (!exp || !del_timer(&exp->timeout))
1229 exp->timeout.expires = jiffies + timeout * HZ;
1230 add_timer(&exp->timeout);
1235 /****************************************************************************/
1236 static int expect_q931(struct sk_buff **pskb, struct nf_conn *ct,
1237 enum ip_conntrack_info ctinfo,
1238 unsigned char **data,
1239 TransportAddress *taddr, int count)
1241 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1242 int dir = CTINFO2DIR(ctinfo);
1246 union nf_conntrack_address addr;
1247 struct nf_conntrack_expect *exp;
1248 typeof(nat_q931_hook) nat_q931;
1250 /* Look for the first related address */
1251 for (i = 0; i < count; i++) {
1252 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1253 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1254 sizeof(addr)) == 0 && port != 0)
1258 if (i >= count) /* Not found */
1261 /* Create expect for Q.931 */
1262 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1264 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1265 gkrouted_only ? /* only accept calls from GK? */
1266 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1267 &ct->tuplehash[!dir].tuple.dst.u3,
1268 IPPROTO_TCP, NULL, &port);
1269 exp->helper = nf_conntrack_helper_q931;
1270 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1272 nat_q931 = rcu_dereference(nat_q931_hook);
1273 if (nat_q931 && ct->status & IPS_NAT_MASK) { /* Need NAT */
1274 ret = nat_q931(pskb, ct, ctinfo, data, taddr, i, port, exp);
1275 } else { /* Conntrack only */
1276 if (nf_ct_expect_related(exp) == 0) {
1277 DEBUGP("nf_ct_ras: expect Q.931 ");
1278 NF_CT_DUMP_TUPLE(&exp->tuple);
1280 /* Save port for looking up expect in processing RCF */
1281 info->sig_port[dir] = port;
1286 nf_ct_expect_put(exp);
1291 /****************************************************************************/
1292 static int process_grq(struct sk_buff **pskb, struct nf_conn *ct,
1293 enum ip_conntrack_info ctinfo,
1294 unsigned char **data, GatekeeperRequest *grq)
1296 typeof(set_ras_addr_hook) set_ras_addr;
1298 DEBUGP("nf_ct_ras: GRQ\n");
1300 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1301 if (set_ras_addr && ct->status & IPS_NAT_MASK) /* NATed */
1302 return set_ras_addr(pskb, ct, ctinfo, data,
1303 &grq->rasAddress, 1);
1307 /****************************************************************************/
1308 static int process_gcf(struct sk_buff **pskb, struct nf_conn *ct,
1309 enum ip_conntrack_info ctinfo,
1310 unsigned char **data, GatekeeperConfirm *gcf)
1312 int dir = CTINFO2DIR(ctinfo);
1315 union nf_conntrack_address addr;
1316 struct nf_conntrack_expect *exp;
1318 DEBUGP("nf_ct_ras: GCF\n");
1320 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1323 /* Registration port is the same as discovery port */
1324 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1325 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1328 /* Avoid RAS expectation loops. A GCF is never expected. */
1329 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1332 /* Need new expect */
1333 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1335 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1336 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1337 IPPROTO_UDP, NULL, &port);
1338 exp->helper = nf_conntrack_helper_ras;
1340 if (nf_ct_expect_related(exp) == 0) {
1341 DEBUGP("nf_ct_ras: expect RAS ");
1342 NF_CT_DUMP_TUPLE(&exp->tuple);
1346 nf_ct_expect_put(exp);
1351 /****************************************************************************/
1352 static int process_rrq(struct sk_buff **pskb, struct nf_conn *ct,
1353 enum ip_conntrack_info ctinfo,
1354 unsigned char **data, RegistrationRequest *rrq)
1356 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1358 typeof(set_ras_addr_hook) set_ras_addr;
1360 DEBUGP("nf_ct_ras: RRQ\n");
1362 ret = expect_q931(pskb, ct, ctinfo, data,
1363 rrq->callSignalAddress.item,
1364 rrq->callSignalAddress.count);
1368 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1369 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1370 ret = set_ras_addr(pskb, ct, ctinfo, data,
1371 rrq->rasAddress.item,
1372 rrq->rasAddress.count);
1377 if (rrq->options & eRegistrationRequest_timeToLive) {
1378 DEBUGP("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1379 info->timeout = rrq->timeToLive;
1381 info->timeout = default_rrq_ttl;
1386 /****************************************************************************/
1387 static int process_rcf(struct sk_buff **pskb, struct nf_conn *ct,
1388 enum ip_conntrack_info ctinfo,
1389 unsigned char **data, RegistrationConfirm *rcf)
1391 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1392 int dir = CTINFO2DIR(ctinfo);
1394 struct nf_conntrack_expect *exp;
1395 typeof(set_sig_addr_hook) set_sig_addr;
1397 DEBUGP("nf_ct_ras: RCF\n");
1399 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1400 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1401 ret = set_sig_addr(pskb, ct, ctinfo, data,
1402 rcf->callSignalAddress.item,
1403 rcf->callSignalAddress.count);
1408 if (rcf->options & eRegistrationConfirm_timeToLive) {
1409 DEBUGP("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1410 info->timeout = rcf->timeToLive;
1413 if (info->timeout > 0) {
1415 ("nf_ct_ras: set RAS connection timeout to %u seconds\n",
1417 nf_ct_refresh(ct, *pskb, info->timeout * HZ);
1419 /* Set expect timeout */
1420 read_lock_bh(&nf_conntrack_lock);
1421 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1422 info->sig_port[!dir]);
1424 DEBUGP("nf_ct_ras: set Q.931 expect "
1425 "timeout to %u seconds for",
1427 NF_CT_DUMP_TUPLE(&exp->tuple);
1428 set_expect_timeout(exp, info->timeout);
1430 read_unlock_bh(&nf_conntrack_lock);
1436 /****************************************************************************/
1437 static int process_urq(struct sk_buff **pskb, struct nf_conn *ct,
1438 enum ip_conntrack_info ctinfo,
1439 unsigned char **data, UnregistrationRequest *urq)
1441 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1442 int dir = CTINFO2DIR(ctinfo);
1444 typeof(set_sig_addr_hook) set_sig_addr;
1446 DEBUGP("nf_ct_ras: URQ\n");
1448 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1449 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1450 ret = set_sig_addr(pskb, ct, ctinfo, data,
1451 urq->callSignalAddress.item,
1452 urq->callSignalAddress.count);
1457 /* Clear old expect */
1458 nf_ct_remove_expectations(ct);
1459 info->sig_port[dir] = 0;
1460 info->sig_port[!dir] = 0;
1462 /* Give it 30 seconds for UCF or URJ */
1463 nf_ct_refresh(ct, *pskb, 30 * HZ);
1468 /****************************************************************************/
1469 static int process_arq(struct sk_buff **pskb, struct nf_conn *ct,
1470 enum ip_conntrack_info ctinfo,
1471 unsigned char **data, AdmissionRequest *arq)
1473 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1474 int dir = CTINFO2DIR(ctinfo);
1476 union nf_conntrack_address addr;
1477 typeof(set_h225_addr_hook) set_h225_addr;
1479 DEBUGP("nf_ct_ras: ARQ\n");
1481 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1482 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1483 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1485 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1486 port == info->sig_port[dir] &&
1487 set_h225_addr && ct->status & IPS_NAT_MASK) {
1489 return set_h225_addr(pskb, data, 0,
1490 &arq->destCallSignalAddress,
1491 &ct->tuplehash[!dir].tuple.dst.u3,
1492 info->sig_port[!dir]);
1495 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1496 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1498 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1499 set_h225_addr && ct->status & IPS_NAT_MASK) {
1501 return set_h225_addr(pskb, data, 0,
1502 &arq->srcCallSignalAddress,
1503 &ct->tuplehash[!dir].tuple.dst.u3,
1510 /****************************************************************************/
1511 static int process_acf(struct sk_buff **pskb, struct nf_conn *ct,
1512 enum ip_conntrack_info ctinfo,
1513 unsigned char **data, AdmissionConfirm *acf)
1515 int dir = CTINFO2DIR(ctinfo);
1518 union nf_conntrack_address addr;
1519 struct nf_conntrack_expect *exp;
1520 typeof(set_sig_addr_hook) set_sig_addr;
1522 DEBUGP("nf_ct_ras: ACF\n");
1524 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1528 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1530 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1531 if (set_sig_addr && ct->status & IPS_NAT_MASK)
1532 return set_sig_addr(pskb, ct, ctinfo, data,
1533 &acf->destCallSignalAddress, 1);
1537 /* Need new expect */
1538 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1540 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1541 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1542 IPPROTO_TCP, NULL, &port);
1543 exp->flags = NF_CT_EXPECT_PERMANENT;
1544 exp->helper = nf_conntrack_helper_q931;
1546 if (nf_ct_expect_related(exp) == 0) {
1547 DEBUGP("nf_ct_ras: expect Q.931 ");
1548 NF_CT_DUMP_TUPLE(&exp->tuple);
1552 nf_ct_expect_put(exp);
1557 /****************************************************************************/
1558 static int process_lrq(struct sk_buff **pskb, struct nf_conn *ct,
1559 enum ip_conntrack_info ctinfo,
1560 unsigned char **data, LocationRequest *lrq)
1562 typeof(set_ras_addr_hook) set_ras_addr;
1564 DEBUGP("nf_ct_ras: LRQ\n");
1566 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1567 if (set_ras_addr && ct->status & IPS_NAT_MASK)
1568 return set_ras_addr(pskb, ct, ctinfo, data,
1569 &lrq->replyAddress, 1);
1573 /****************************************************************************/
1574 static int process_lcf(struct sk_buff **pskb, struct nf_conn *ct,
1575 enum ip_conntrack_info ctinfo,
1576 unsigned char **data, LocationConfirm *lcf)
1578 int dir = CTINFO2DIR(ctinfo);
1581 union nf_conntrack_address addr;
1582 struct nf_conntrack_expect *exp;
1584 DEBUGP("nf_ct_ras: LCF\n");
1586 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1590 /* Need new expect for call signal */
1591 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1593 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1594 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1595 IPPROTO_TCP, NULL, &port);
1596 exp->flags = NF_CT_EXPECT_PERMANENT;
1597 exp->helper = nf_conntrack_helper_q931;
1599 if (nf_ct_expect_related(exp) == 0) {
1600 DEBUGP("nf_ct_ras: expect Q.931 ");
1601 NF_CT_DUMP_TUPLE(&exp->tuple);
1605 nf_ct_expect_put(exp);
1607 /* Ignore rasAddress */
1612 /****************************************************************************/
1613 static int process_irr(struct sk_buff **pskb, struct nf_conn *ct,
1614 enum ip_conntrack_info ctinfo,
1615 unsigned char **data, InfoRequestResponse *irr)
1618 typeof(set_ras_addr_hook) set_ras_addr;
1619 typeof(set_sig_addr_hook) set_sig_addr;
1621 DEBUGP("nf_ct_ras: IRR\n");
1623 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1624 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1625 ret = set_ras_addr(pskb, ct, ctinfo, data,
1626 &irr->rasAddress, 1);
1631 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1632 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1633 ret = set_sig_addr(pskb, ct, ctinfo, data,
1634 irr->callSignalAddress.item,
1635 irr->callSignalAddress.count);
1643 /****************************************************************************/
1644 static int process_ras(struct sk_buff **pskb, struct nf_conn *ct,
1645 enum ip_conntrack_info ctinfo,
1646 unsigned char **data, RasMessage *ras)
1648 switch (ras->choice) {
1649 case eRasMessage_gatekeeperRequest:
1650 return process_grq(pskb, ct, ctinfo, data,
1651 &ras->gatekeeperRequest);
1652 case eRasMessage_gatekeeperConfirm:
1653 return process_gcf(pskb, ct, ctinfo, data,
1654 &ras->gatekeeperConfirm);
1655 case eRasMessage_registrationRequest:
1656 return process_rrq(pskb, ct, ctinfo, data,
1657 &ras->registrationRequest);
1658 case eRasMessage_registrationConfirm:
1659 return process_rcf(pskb, ct, ctinfo, data,
1660 &ras->registrationConfirm);
1661 case eRasMessage_unregistrationRequest:
1662 return process_urq(pskb, ct, ctinfo, data,
1663 &ras->unregistrationRequest);
1664 case eRasMessage_admissionRequest:
1665 return process_arq(pskb, ct, ctinfo, data,
1666 &ras->admissionRequest);
1667 case eRasMessage_admissionConfirm:
1668 return process_acf(pskb, ct, ctinfo, data,
1669 &ras->admissionConfirm);
1670 case eRasMessage_locationRequest:
1671 return process_lrq(pskb, ct, ctinfo, data,
1672 &ras->locationRequest);
1673 case eRasMessage_locationConfirm:
1674 return process_lcf(pskb, ct, ctinfo, data,
1675 &ras->locationConfirm);
1676 case eRasMessage_infoRequestResponse:
1677 return process_irr(pskb, ct, ctinfo, data,
1678 &ras->infoRequestResponse);
1680 DEBUGP("nf_ct_ras: RAS message %d\n", ras->choice);
1687 /****************************************************************************/
1688 static int ras_help(struct sk_buff **pskb, unsigned int protoff,
1689 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1691 static RasMessage ras;
1692 unsigned char *data;
1696 DEBUGP("nf_ct_ras: skblen = %u\n", (*pskb)->len);
1698 spin_lock_bh(&nf_h323_lock);
1701 data = get_udp_data(pskb, protoff, &datalen);
1704 DEBUGP("nf_ct_ras: RAS message len=%d ", datalen);
1705 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1707 /* Decode RAS message */
1708 ret = DecodeRasMessage(data, datalen, &ras);
1710 DEBUGP("nf_ct_ras: decoding error: %s\n",
1711 ret == H323_ERROR_BOUND ?
1712 "out of bound" : "out of range");
1716 /* Process RAS message */
1717 if (process_ras(pskb, ct, ctinfo, &data, &ras) < 0)
1721 spin_unlock_bh(&nf_h323_lock);
1725 spin_unlock_bh(&nf_h323_lock);
1726 if (net_ratelimit())
1727 printk("nf_ct_ras: packet dropped\n");
1731 /****************************************************************************/
1732 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1738 .tuple.src.l3num = AF_INET,
1739 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1740 .tuple.dst.protonum = IPPROTO_UDP,
1748 .tuple.src.l3num = AF_INET6,
1749 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1750 .tuple.dst.protonum = IPPROTO_UDP,
1755 /****************************************************************************/
1756 static void __exit nf_conntrack_h323_fini(void)
1758 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1759 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1760 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1761 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1763 DEBUGP("nf_ct_h323: fini\n");
1766 /****************************************************************************/
1767 static int __init nf_conntrack_h323_init(void)
1771 h323_buffer = kmalloc(65536, GFP_KERNEL);
1774 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1777 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1780 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1783 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1786 DEBUGP("nf_ct_h323: init success\n");
1790 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1792 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1794 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1799 /****************************************************************************/
1800 module_init(nf_conntrack_h323_init);
1801 module_exit(nf_conntrack_h323_fini);
1803 EXPORT_SYMBOL_GPL(get_h225_addr);
1804 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1805 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1806 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1807 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1808 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1809 EXPORT_SYMBOL_GPL(nat_t120_hook);
1810 EXPORT_SYMBOL_GPL(nat_h245_hook);
1811 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1812 EXPORT_SYMBOL_GPL(nat_q931_hook);
1814 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1815 MODULE_DESCRIPTION("H.323 connection tracking helper");
1816 MODULE_LICENSE("GPL");
1817 MODULE_ALIAS("ip_conntrack_h323");