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 <net/netfilter/nf_conntrack_zones.h>
33 #include <linux/netfilter/nf_conntrack_h323.h>
36 static unsigned int default_rrq_ttl __read_mostly = 300;
37 module_param(default_rrq_ttl, uint, 0600);
38 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
40 static int gkrouted_only __read_mostly = 1;
41 module_param(gkrouted_only, int, 0600);
42 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
44 static int callforward_filter __read_mostly = 1;
45 module_param(callforward_filter, bool, 0600);
46 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
47 "if both endpoints are on different sides "
48 "(determined by routing information)");
51 int (*set_h245_addr_hook) (struct sk_buff *skb,
52 unsigned char **data, int dataoff,
53 H245_TransportAddress *taddr,
54 union nf_inet_addr *addr, __be16 port)
56 int (*set_h225_addr_hook) (struct sk_buff *skb,
57 unsigned char **data, int dataoff,
58 TransportAddress *taddr,
59 union nf_inet_addr *addr, __be16 port)
61 int (*set_sig_addr_hook) (struct sk_buff *skb,
63 enum ip_conntrack_info ctinfo,
65 TransportAddress *taddr, int count) __read_mostly;
66 int (*set_ras_addr_hook) (struct sk_buff *skb,
68 enum ip_conntrack_info ctinfo,
70 TransportAddress *taddr, int count) __read_mostly;
71 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
73 enum ip_conntrack_info ctinfo,
74 unsigned char **data, int dataoff,
75 H245_TransportAddress *taddr,
76 __be16 port, __be16 rtp_port,
77 struct nf_conntrack_expect *rtp_exp,
78 struct nf_conntrack_expect *rtcp_exp) __read_mostly;
79 int (*nat_t120_hook) (struct sk_buff *skb,
81 enum ip_conntrack_info ctinfo,
82 unsigned char **data, int dataoff,
83 H245_TransportAddress *taddr, __be16 port,
84 struct nf_conntrack_expect *exp) __read_mostly;
85 int (*nat_h245_hook) (struct sk_buff *skb,
87 enum ip_conntrack_info ctinfo,
88 unsigned char **data, int dataoff,
89 TransportAddress *taddr, __be16 port,
90 struct nf_conntrack_expect *exp) __read_mostly;
91 int (*nat_callforwarding_hook) (struct sk_buff *skb,
93 enum ip_conntrack_info ctinfo,
94 unsigned char **data, int dataoff,
95 TransportAddress *taddr, __be16 port,
96 struct nf_conntrack_expect *exp) __read_mostly;
97 int (*nat_q931_hook) (struct sk_buff *skb,
99 enum ip_conntrack_info ctinfo,
100 unsigned char **data, TransportAddress *taddr, int idx,
101 __be16 port, struct nf_conntrack_expect *exp)
104 static DEFINE_SPINLOCK(nf_h323_lock);
105 static char *h323_buffer;
107 static struct nf_conntrack_helper nf_conntrack_helper_h245;
108 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
109 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
111 /****************************************************************************/
112 static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,
113 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
114 unsigned char **data, int *datalen, int *dataoff)
116 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
117 int dir = CTINFO2DIR(ctinfo);
118 const struct tcphdr *th;
127 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
131 /* Get TCP data offset */
132 tcpdataoff = protoff + th->doff * 4;
134 /* Get TCP data length */
135 tcpdatalen = skb->len - tcpdataoff;
136 if (tcpdatalen <= 0) /* No TCP data */
139 if (*data == NULL) { /* first TPKT */
140 /* Get first TPKT pointer */
141 tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
143 BUG_ON(tpkt == NULL);
145 /* Validate TPKT identifier */
146 if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) {
147 /* Netmeeting sends TPKT header and data separately */
148 if (info->tpkt_len[dir] > 0) {
149 pr_debug("nf_ct_h323: previous packet "
150 "indicated separate TPKT data of %hu "
151 "bytes\n", info->tpkt_len[dir]);
152 if (info->tpkt_len[dir] <= tcpdatalen) {
153 /* Yes, there was a TPKT header
156 *datalen = info->tpkt_len[dir];
161 /* Fragmented TPKT */
162 pr_debug("nf_ct_h323: fragmented TPKT\n");
166 /* It is not even a TPKT */
170 } else { /* Next TPKT */
171 tpktoff = *dataoff + *datalen;
172 tcpdatalen -= tpktoff;
173 if (tcpdatalen <= 4) /* No more TPKT */
175 tpkt = *data + *datalen;
177 /* Validate TPKT identifier */
178 if (tpkt[0] != 0x03 || tpkt[1] != 0)
182 /* Validate TPKT length */
183 tpktlen = tpkt[2] * 256 + tpkt[3];
186 if (tpktlen > tcpdatalen) {
187 if (tcpdatalen == 4) { /* Separate TPKT header */
188 /* Netmeeting sends TPKT header and data separately */
189 pr_debug("nf_ct_h323: separate TPKT header indicates "
190 "there will be TPKT data of %hu bytes\n",
192 info->tpkt_len[dir] = tpktlen - 4;
197 printk("nf_ct_h323: incomplete TPKT (fragmented?)\n");
201 /* This is the encapsulated data */
203 *datalen = tpktlen - 4;
204 *dataoff = tpktoff + 4;
207 /* Clear TPKT length */
208 info->tpkt_len[dir] = 0;
212 info->tpkt_len[dir] = 0;
216 /****************************************************************************/
217 static int get_h245_addr(struct nf_conn *ct, const unsigned char *data,
218 H245_TransportAddress *taddr,
219 union nf_inet_addr *addr, __be16 *port)
221 const unsigned char *p;
224 if (taddr->choice != eH245_TransportAddress_unicastAddress)
227 switch (taddr->unicastAddress.choice) {
228 case eUnicastAddress_iPAddress:
229 if (nf_ct_l3num(ct) != AF_INET)
231 p = data + taddr->unicastAddress.iPAddress.network;
234 case eUnicastAddress_iP6Address:
235 if (nf_ct_l3num(ct) != AF_INET6)
237 p = data + taddr->unicastAddress.iP6Address.network;
244 memcpy(addr, p, len);
245 memset((void *)addr + len, 0, sizeof(*addr) - len);
246 memcpy(port, p + len, sizeof(__be16));
251 /****************************************************************************/
252 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
253 enum ip_conntrack_info ctinfo,
254 unsigned char **data, int dataoff,
255 H245_TransportAddress *taddr)
257 int dir = CTINFO2DIR(ctinfo);
260 __be16 rtp_port, rtcp_port;
261 union nf_inet_addr addr;
262 struct nf_conntrack_expect *rtp_exp;
263 struct nf_conntrack_expect *rtcp_exp;
264 typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
266 /* Read RTP or RTCP address */
267 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
268 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
272 /* RTP port is even */
275 rtcp_port = htons(ntohs(port) + 1);
277 /* Create expect for RTP */
278 if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
280 nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
281 &ct->tuplehash[!dir].tuple.src.u3,
282 &ct->tuplehash[!dir].tuple.dst.u3,
283 IPPROTO_UDP, NULL, &rtp_port);
285 /* Create expect for RTCP */
286 if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
287 nf_ct_expect_put(rtp_exp);
290 nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
291 &ct->tuplehash[!dir].tuple.src.u3,
292 &ct->tuplehash[!dir].tuple.dst.u3,
293 IPPROTO_UDP, NULL, &rtcp_port);
295 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
296 &ct->tuplehash[!dir].tuple.dst.u3,
297 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
298 (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
299 ct->status & IPS_NAT_MASK) {
301 ret = nat_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
302 taddr, port, rtp_port, rtp_exp, rtcp_exp);
303 } else { /* Conntrack only */
304 if (nf_ct_expect_related(rtp_exp) == 0) {
305 if (nf_ct_expect_related(rtcp_exp) == 0) {
306 pr_debug("nf_ct_h323: expect RTP ");
307 nf_ct_dump_tuple(&rtp_exp->tuple);
308 pr_debug("nf_ct_h323: expect RTCP ");
309 nf_ct_dump_tuple(&rtcp_exp->tuple);
311 nf_ct_unexpect_related(rtp_exp);
318 nf_ct_expect_put(rtp_exp);
319 nf_ct_expect_put(rtcp_exp);
324 /****************************************************************************/
325 static int expect_t120(struct sk_buff *skb,
327 enum ip_conntrack_info ctinfo,
328 unsigned char **data, int dataoff,
329 H245_TransportAddress *taddr)
331 int dir = CTINFO2DIR(ctinfo);
334 union nf_inet_addr addr;
335 struct nf_conntrack_expect *exp;
336 typeof(nat_t120_hook) nat_t120;
338 /* Read T.120 address */
339 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
340 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
344 /* Create expect for T.120 connections */
345 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
347 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
348 &ct->tuplehash[!dir].tuple.src.u3,
349 &ct->tuplehash[!dir].tuple.dst.u3,
350 IPPROTO_TCP, NULL, &port);
351 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple channels */
353 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
354 &ct->tuplehash[!dir].tuple.dst.u3,
355 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
356 (nat_t120 = rcu_dereference(nat_t120_hook)) &&
357 ct->status & IPS_NAT_MASK) {
359 ret = nat_t120(skb, ct, ctinfo, data, dataoff, taddr,
361 } else { /* Conntrack only */
362 if (nf_ct_expect_related(exp) == 0) {
363 pr_debug("nf_ct_h323: expect T.120 ");
364 nf_ct_dump_tuple(&exp->tuple);
369 nf_ct_expect_put(exp);
374 /****************************************************************************/
375 static int process_h245_channel(struct sk_buff *skb,
377 enum ip_conntrack_info ctinfo,
378 unsigned char **data, int dataoff,
379 H2250LogicalChannelParameters *channel)
383 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
385 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
386 &channel->mediaChannel);
392 options & eH2250LogicalChannelParameters_mediaControlChannel) {
394 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
395 &channel->mediaControlChannel);
403 /****************************************************************************/
404 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
405 enum ip_conntrack_info ctinfo,
406 unsigned char **data, int dataoff,
407 OpenLogicalChannel *olc)
411 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
413 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
414 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
416 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
418 forwardLogicalChannelParameters.
420 h2250LogicalChannelParameters);
426 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
427 (olc->reverseLogicalChannelParameters.options &
428 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
429 && (olc->reverseLogicalChannelParameters.multiplexParameters.
431 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
434 process_h245_channel(skb, ct, ctinfo, data, dataoff,
436 reverseLogicalChannelParameters.
438 h2250LogicalChannelParameters);
443 if ((olc->options & eOpenLogicalChannel_separateStack) &&
444 olc->forwardLogicalChannelParameters.dataType.choice ==
446 olc->forwardLogicalChannelParameters.dataType.data.application.
447 choice == eDataApplicationCapability_application_t120 &&
448 olc->forwardLogicalChannelParameters.dataType.data.application.
449 t120.choice == eDataProtocolCapability_separateLANStack &&
450 olc->separateStack.networkAddress.choice ==
451 eNetworkAccessParameters_networkAddress_localAreaAddress) {
452 ret = expect_t120(skb, ct, ctinfo, data, dataoff,
453 &olc->separateStack.networkAddress.
462 /****************************************************************************/
463 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
464 enum ip_conntrack_info ctinfo,
465 unsigned char **data, int dataoff,
466 OpenLogicalChannelAck *olca)
468 H2250LogicalChannelAckParameters *ack;
471 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
474 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
475 (olca->reverseLogicalChannelParameters.options &
476 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
477 && (olca->reverseLogicalChannelParameters.multiplexParameters.
479 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
481 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
483 reverseLogicalChannelParameters.
485 h2250LogicalChannelParameters);
491 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
492 (olca->forwardMultiplexAckParameters.choice ==
493 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
495 ack = &olca->forwardMultiplexAckParameters.
496 h2250LogicalChannelAckParameters;
498 eH2250LogicalChannelAckParameters_mediaChannel) {
500 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
507 eH2250LogicalChannelAckParameters_mediaControlChannel) {
509 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
510 &ack->mediaControlChannel);
516 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
517 olca->separateStack.networkAddress.choice ==
518 eNetworkAccessParameters_networkAddress_localAreaAddress) {
519 ret = expect_t120(skb, ct, ctinfo, data, dataoff,
520 &olca->separateStack.networkAddress.
529 /****************************************************************************/
530 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
531 enum ip_conntrack_info ctinfo,
532 unsigned char **data, int dataoff,
533 MultimediaSystemControlMessage *mscm)
535 switch (mscm->choice) {
536 case eMultimediaSystemControlMessage_request:
537 if (mscm->request.choice ==
538 eRequestMessage_openLogicalChannel) {
539 return process_olc(skb, ct, ctinfo, data, dataoff,
540 &mscm->request.openLogicalChannel);
542 pr_debug("nf_ct_h323: H.245 Request %d\n",
543 mscm->request.choice);
545 case eMultimediaSystemControlMessage_response:
546 if (mscm->response.choice ==
547 eResponseMessage_openLogicalChannelAck) {
548 return process_olca(skb, ct, ctinfo, data, dataoff,
550 openLogicalChannelAck);
552 pr_debug("nf_ct_h323: H.245 Response %d\n",
553 mscm->response.choice);
556 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice);
563 /****************************************************************************/
564 static int h245_help(struct sk_buff *skb, unsigned int protoff,
565 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
567 static MultimediaSystemControlMessage mscm;
568 unsigned char *data = NULL;
573 /* Until there's been traffic both ways, don't look in packets. */
574 if (ctinfo != IP_CT_ESTABLISHED &&
575 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
578 pr_debug("nf_ct_h245: skblen = %u\n", skb->len);
580 spin_lock_bh(&nf_h323_lock);
582 /* Process each TPKT */
583 while (get_tpkt_data(skb, protoff, ct, ctinfo,
584 &data, &datalen, &dataoff)) {
585 pr_debug("nf_ct_h245: TPKT len=%d ", datalen);
586 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
588 /* Decode H.245 signal */
589 ret = DecodeMultimediaSystemControlMessage(data, datalen,
592 pr_debug("nf_ct_h245: decoding error: %s\n",
593 ret == H323_ERROR_BOUND ?
594 "out of bound" : "out of range");
595 /* We don't drop when decoding error */
599 /* Process H.245 signal */
600 if (process_h245(skb, ct, ctinfo, &data, dataoff, &mscm) < 0)
604 spin_unlock_bh(&nf_h323_lock);
608 spin_unlock_bh(&nf_h323_lock);
610 printk("nf_ct_h245: packet dropped\n");
614 /****************************************************************************/
615 static const struct nf_conntrack_expect_policy h245_exp_policy = {
616 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
620 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
623 .tuple.src.l3num = AF_UNSPEC,
624 .tuple.dst.protonum = IPPROTO_UDP,
626 .expect_policy = &h245_exp_policy,
629 /****************************************************************************/
630 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
631 TransportAddress *taddr,
632 union nf_inet_addr *addr, __be16 *port)
634 const unsigned char *p;
637 switch (taddr->choice) {
638 case eTransportAddress_ipAddress:
639 if (nf_ct_l3num(ct) != AF_INET)
641 p = data + taddr->ipAddress.ip;
644 case eTransportAddress_ip6Address:
645 if (nf_ct_l3num(ct) != AF_INET6)
647 p = data + taddr->ip6Address.ip;
654 memcpy(addr, p, len);
655 memset((void *)addr + len, 0, sizeof(*addr) - len);
656 memcpy(port, p + len, sizeof(__be16));
661 /****************************************************************************/
662 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
663 enum ip_conntrack_info ctinfo,
664 unsigned char **data, int dataoff,
665 TransportAddress *taddr)
667 int dir = CTINFO2DIR(ctinfo);
670 union nf_inet_addr addr;
671 struct nf_conntrack_expect *exp;
672 typeof(nat_h245_hook) nat_h245;
674 /* Read h245Address */
675 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
676 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
680 /* Create expect for h245 connection */
681 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
683 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
684 &ct->tuplehash[!dir].tuple.src.u3,
685 &ct->tuplehash[!dir].tuple.dst.u3,
686 IPPROTO_TCP, NULL, &port);
687 exp->helper = &nf_conntrack_helper_h245;
689 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
690 &ct->tuplehash[!dir].tuple.dst.u3,
691 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
692 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
693 ct->status & IPS_NAT_MASK) {
695 ret = nat_h245(skb, ct, ctinfo, data, dataoff, taddr,
697 } else { /* Conntrack only */
698 if (nf_ct_expect_related(exp) == 0) {
699 pr_debug("nf_ct_q931: expect H.245 ");
700 nf_ct_dump_tuple(&exp->tuple);
705 nf_ct_expect_put(exp);
710 /* If the calling party is on the same side of the forward-to party,
711 * we don't need to track the second call */
712 static int callforward_do_filter(const union nf_inet_addr *src,
713 const union nf_inet_addr *dst,
716 const struct nf_afinfo *afinfo;
717 struct flowi fl1, fl2;
720 /* rcu_read_lock()ed by nf_hook_slow() */
721 afinfo = nf_get_afinfo(family);
725 memset(&fl1, 0, sizeof(fl1));
726 memset(&fl2, 0, sizeof(fl2));
730 struct rtable *rt1, *rt2;
732 fl1.fl4_dst = src->ip;
733 fl2.fl4_dst = dst->ip;
734 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
735 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
736 if (rt1->rt_gateway == rt2->rt_gateway &&
737 rt1->u.dst.dev == rt2->u.dst.dev)
739 dst_release(&rt2->u.dst);
741 dst_release(&rt1->u.dst);
745 #if defined(CONFIG_NF_CONNTRACK_IPV6) || \
746 defined(CONFIG_NF_CONNTRACK_IPV6_MODULE)
748 struct rt6_info *rt1, *rt2;
750 memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst));
751 memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst));
752 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
753 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
754 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
755 sizeof(rt1->rt6i_gateway)) &&
756 rt1->u.dst.dev == rt2->u.dst.dev)
758 dst_release(&rt2->u.dst);
760 dst_release(&rt1->u.dst);
770 /****************************************************************************/
771 static int expect_callforwarding(struct sk_buff *skb,
773 enum ip_conntrack_info ctinfo,
774 unsigned char **data, int dataoff,
775 TransportAddress *taddr)
777 int dir = CTINFO2DIR(ctinfo);
780 union nf_inet_addr addr;
781 struct nf_conntrack_expect *exp;
782 typeof(nat_callforwarding_hook) nat_callforwarding;
784 /* Read alternativeAddress */
785 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
788 /* If the calling party is on the same side of the forward-to party,
789 * we don't need to track the second call */
790 if (callforward_filter &&
791 callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
793 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
797 /* Create expect for the second call leg */
798 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
800 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
801 &ct->tuplehash[!dir].tuple.src.u3, &addr,
802 IPPROTO_TCP, NULL, &port);
803 exp->helper = nf_conntrack_helper_q931;
805 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
806 &ct->tuplehash[!dir].tuple.dst.u3,
807 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
808 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
809 ct->status & IPS_NAT_MASK) {
811 ret = nat_callforwarding(skb, ct, ctinfo, data, dataoff,
813 } else { /* Conntrack only */
814 if (nf_ct_expect_related(exp) == 0) {
815 pr_debug("nf_ct_q931: expect Call Forwarding ");
816 nf_ct_dump_tuple(&exp->tuple);
821 nf_ct_expect_put(exp);
826 /****************************************************************************/
827 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
828 enum ip_conntrack_info ctinfo,
829 unsigned char **data, int dataoff,
832 int dir = CTINFO2DIR(ctinfo);
836 union nf_inet_addr addr;
837 typeof(set_h225_addr_hook) set_h225_addr;
839 pr_debug("nf_ct_q931: Setup\n");
841 if (setup->options & eSetup_UUIE_h245Address) {
842 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
843 &setup->h245Address);
848 set_h225_addr = rcu_dereference(set_h225_addr_hook);
849 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
850 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
851 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
853 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
854 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
855 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3,
856 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
857 ret = set_h225_addr(skb, data, dataoff,
858 &setup->destCallSignalAddress,
859 &ct->tuplehash[!dir].tuple.src.u3,
860 ct->tuplehash[!dir].tuple.src.u.tcp.port);
865 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
866 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
867 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
869 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
870 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
871 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3,
872 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
873 ret = set_h225_addr(skb, 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(skb, ct, ctinfo, data, dataoff,
884 &setup->fastStart.item[i]);
893 /****************************************************************************/
894 static int process_callproceeding(struct sk_buff *skb,
896 enum ip_conntrack_info ctinfo,
897 unsigned char **data, int dataoff,
898 CallProceeding_UUIE *callproc)
903 pr_debug("nf_ct_q931: CallProceeding\n");
905 if (callproc->options & eCallProceeding_UUIE_h245Address) {
906 ret = expect_h245(skb, 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(skb, ct, ctinfo, data, dataoff,
915 &callproc->fastStart.item[i]);
924 /****************************************************************************/
925 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
926 enum ip_conntrack_info ctinfo,
927 unsigned char **data, int dataoff,
928 Connect_UUIE *connect)
933 pr_debug("nf_ct_q931: Connect\n");
935 if (connect->options & eConnect_UUIE_h245Address) {
936 ret = expect_h245(skb, 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(skb, ct, ctinfo, data, dataoff,
945 &connect->fastStart.item[i]);
954 /****************************************************************************/
955 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
956 enum ip_conntrack_info ctinfo,
957 unsigned char **data, int dataoff,
958 Alerting_UUIE *alert)
963 pr_debug("nf_ct_q931: Alerting\n");
965 if (alert->options & eAlerting_UUIE_h245Address) {
966 ret = expect_h245(skb, 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(skb, ct, ctinfo, data, dataoff,
975 &alert->fastStart.item[i]);
984 /****************************************************************************/
985 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
986 enum ip_conntrack_info ctinfo,
987 unsigned char **data, int dataoff,
988 Facility_UUIE *facility)
993 pr_debug("nf_ct_q931: Facility\n");
995 if (facility->reason.choice == eFacilityReason_callForwarded) {
996 if (facility->options & eFacility_UUIE_alternativeAddress)
997 return expect_callforwarding(skb, ct, ctinfo, data,
1000 alternativeAddress);
1004 if (facility->options & eFacility_UUIE_h245Address) {
1005 ret = expect_h245(skb, 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(skb, ct, ctinfo, data, dataoff,
1014 &facility->fastStart.item[i]);
1023 /****************************************************************************/
1024 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1025 enum ip_conntrack_info ctinfo,
1026 unsigned char **data, int dataoff,
1027 Progress_UUIE *progress)
1032 pr_debug("nf_ct_q931: Progress\n");
1034 if (progress->options & eProgress_UUIE_h245Address) {
1035 ret = expect_h245(skb, 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(skb, ct, ctinfo, data, dataoff,
1044 &progress->fastStart.item[i]);
1053 /****************************************************************************/
1054 static int process_q931(struct sk_buff *skb, 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(skb, ct, ctinfo, data, dataoff,
1065 &pdu->h323_message_body.setup);
1067 case eH323_UU_PDU_h323_message_body_callProceeding:
1068 ret = process_callproceeding(skb, ct, ctinfo, data, dataoff,
1069 &pdu->h323_message_body.
1072 case eH323_UU_PDU_h323_message_body_connect:
1073 ret = process_connect(skb, ct, ctinfo, data, dataoff,
1074 &pdu->h323_message_body.connect);
1076 case eH323_UU_PDU_h323_message_body_alerting:
1077 ret = process_alerting(skb, ct, ctinfo, data, dataoff,
1078 &pdu->h323_message_body.alerting);
1080 case eH323_UU_PDU_h323_message_body_facility:
1081 ret = process_facility(skb, ct, ctinfo, data, dataoff,
1082 &pdu->h323_message_body.facility);
1084 case eH323_UU_PDU_h323_message_body_progress:
1085 ret = process_progress(skb, ct, ctinfo, data, dataoff,
1086 &pdu->h323_message_body.progress);
1089 pr_debug("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(skb, ct, ctinfo, data, dataoff,
1100 &pdu->h245Control.item[i]);
1109 /****************************************************************************/
1110 static int q931_help(struct sk_buff *skb, 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 pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1126 spin_lock_bh(&nf_h323_lock);
1128 /* Process each TPKT */
1129 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1130 &data, &datalen, &dataoff)) {
1131 pr_debug("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 pr_debug("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(skb, 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 const struct nf_conntrack_expect_policy q931_exp_policy = {
1161 /* T.120 and H.245 */
1162 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1166 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1170 .tuple.src.l3num = AF_INET,
1171 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1172 .tuple.dst.protonum = IPPROTO_TCP,
1174 .expect_policy = &q931_exp_policy,
1179 .tuple.src.l3num = AF_INET6,
1180 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1181 .tuple.dst.protonum = IPPROTO_TCP,
1183 .expect_policy = &q931_exp_policy,
1187 /****************************************************************************/
1188 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1191 const struct udphdr *uh;
1195 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1198 dataoff = protoff + sizeof(_uh);
1199 if (dataoff >= skb->len)
1201 *datalen = skb->len - dataoff;
1202 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1205 /****************************************************************************/
1206 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1207 union nf_inet_addr *addr,
1210 struct net *net = nf_ct_net(ct);
1211 struct nf_conntrack_expect *exp;
1212 struct nf_conntrack_tuple tuple;
1214 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1215 tuple.src.u.tcp.port = 0;
1216 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1217 tuple.dst.u.tcp.port = port;
1218 tuple.dst.protonum = IPPROTO_TCP;
1220 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1221 if (exp && exp->master == ct)
1226 /****************************************************************************/
1227 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1230 if (!exp || !del_timer(&exp->timeout))
1233 exp->timeout.expires = jiffies + timeout * HZ;
1234 add_timer(&exp->timeout);
1239 /****************************************************************************/
1240 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1241 enum ip_conntrack_info ctinfo,
1242 unsigned char **data,
1243 TransportAddress *taddr, int count)
1245 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1246 int dir = CTINFO2DIR(ctinfo);
1250 union nf_inet_addr addr;
1251 struct nf_conntrack_expect *exp;
1252 typeof(nat_q931_hook) nat_q931;
1254 /* Look for the first related address */
1255 for (i = 0; i < count; i++) {
1256 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1257 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1258 sizeof(addr)) == 0 && port != 0)
1262 if (i >= count) /* Not found */
1265 /* Create expect for Q.931 */
1266 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1268 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1269 gkrouted_only ? /* only accept calls from GK? */
1270 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1271 &ct->tuplehash[!dir].tuple.dst.u3,
1272 IPPROTO_TCP, NULL, &port);
1273 exp->helper = nf_conntrack_helper_q931;
1274 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1276 nat_q931 = rcu_dereference(nat_q931_hook);
1277 if (nat_q931 && ct->status & IPS_NAT_MASK) { /* Need NAT */
1278 ret = nat_q931(skb, ct, ctinfo, data, taddr, i, port, exp);
1279 } else { /* Conntrack only */
1280 if (nf_ct_expect_related(exp) == 0) {
1281 pr_debug("nf_ct_ras: expect Q.931 ");
1282 nf_ct_dump_tuple(&exp->tuple);
1284 /* Save port for looking up expect in processing RCF */
1285 info->sig_port[dir] = port;
1290 nf_ct_expect_put(exp);
1295 /****************************************************************************/
1296 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1297 enum ip_conntrack_info ctinfo,
1298 unsigned char **data, GatekeeperRequest *grq)
1300 typeof(set_ras_addr_hook) set_ras_addr;
1302 pr_debug("nf_ct_ras: GRQ\n");
1304 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1305 if (set_ras_addr && ct->status & IPS_NAT_MASK) /* NATed */
1306 return set_ras_addr(skb, ct, ctinfo, data,
1307 &grq->rasAddress, 1);
1311 /****************************************************************************/
1312 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1313 enum ip_conntrack_info ctinfo,
1314 unsigned char **data, GatekeeperConfirm *gcf)
1316 int dir = CTINFO2DIR(ctinfo);
1319 union nf_inet_addr addr;
1320 struct nf_conntrack_expect *exp;
1322 pr_debug("nf_ct_ras: GCF\n");
1324 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1327 /* Registration port is the same as discovery port */
1328 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1329 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1332 /* Avoid RAS expectation loops. A GCF is never expected. */
1333 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1336 /* Need new expect */
1337 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1339 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1340 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1341 IPPROTO_UDP, NULL, &port);
1342 exp->helper = nf_conntrack_helper_ras;
1344 if (nf_ct_expect_related(exp) == 0) {
1345 pr_debug("nf_ct_ras: expect RAS ");
1346 nf_ct_dump_tuple(&exp->tuple);
1350 nf_ct_expect_put(exp);
1355 /****************************************************************************/
1356 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1357 enum ip_conntrack_info ctinfo,
1358 unsigned char **data, RegistrationRequest *rrq)
1360 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1362 typeof(set_ras_addr_hook) set_ras_addr;
1364 pr_debug("nf_ct_ras: RRQ\n");
1366 ret = expect_q931(skb, ct, ctinfo, data,
1367 rrq->callSignalAddress.item,
1368 rrq->callSignalAddress.count);
1372 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1373 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1374 ret = set_ras_addr(skb, ct, ctinfo, data,
1375 rrq->rasAddress.item,
1376 rrq->rasAddress.count);
1381 if (rrq->options & eRegistrationRequest_timeToLive) {
1382 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1383 info->timeout = rrq->timeToLive;
1385 info->timeout = default_rrq_ttl;
1390 /****************************************************************************/
1391 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1392 enum ip_conntrack_info ctinfo,
1393 unsigned char **data, RegistrationConfirm *rcf)
1395 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1396 int dir = CTINFO2DIR(ctinfo);
1398 struct nf_conntrack_expect *exp;
1399 typeof(set_sig_addr_hook) set_sig_addr;
1401 pr_debug("nf_ct_ras: RCF\n");
1403 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1404 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1405 ret = set_sig_addr(skb, ct, ctinfo, data,
1406 rcf->callSignalAddress.item,
1407 rcf->callSignalAddress.count);
1412 if (rcf->options & eRegistrationConfirm_timeToLive) {
1413 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1414 info->timeout = rcf->timeToLive;
1417 if (info->timeout > 0) {
1418 pr_debug("nf_ct_ras: set RAS connection timeout to "
1419 "%u seconds\n", info->timeout);
1420 nf_ct_refresh(ct, skb, info->timeout * HZ);
1422 /* Set expect timeout */
1423 spin_lock_bh(&nf_conntrack_lock);
1424 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1425 info->sig_port[!dir]);
1427 pr_debug("nf_ct_ras: set Q.931 expect "
1428 "timeout to %u seconds for",
1430 nf_ct_dump_tuple(&exp->tuple);
1431 set_expect_timeout(exp, info->timeout);
1433 spin_unlock_bh(&nf_conntrack_lock);
1439 /****************************************************************************/
1440 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1441 enum ip_conntrack_info ctinfo,
1442 unsigned char **data, UnregistrationRequest *urq)
1444 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1445 int dir = CTINFO2DIR(ctinfo);
1447 typeof(set_sig_addr_hook) set_sig_addr;
1449 pr_debug("nf_ct_ras: URQ\n");
1451 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1452 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1453 ret = set_sig_addr(skb, ct, ctinfo, data,
1454 urq->callSignalAddress.item,
1455 urq->callSignalAddress.count);
1460 /* Clear old expect */
1461 nf_ct_remove_expectations(ct);
1462 info->sig_port[dir] = 0;
1463 info->sig_port[!dir] = 0;
1465 /* Give it 30 seconds for UCF or URJ */
1466 nf_ct_refresh(ct, skb, 30 * HZ);
1471 /****************************************************************************/
1472 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1473 enum ip_conntrack_info ctinfo,
1474 unsigned char **data, AdmissionRequest *arq)
1476 const struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1477 int dir = CTINFO2DIR(ctinfo);
1479 union nf_inet_addr addr;
1480 typeof(set_h225_addr_hook) set_h225_addr;
1482 pr_debug("nf_ct_ras: ARQ\n");
1484 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1485 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1486 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1488 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1489 port == info->sig_port[dir] &&
1490 set_h225_addr && ct->status & IPS_NAT_MASK) {
1492 return set_h225_addr(skb, data, 0,
1493 &arq->destCallSignalAddress,
1494 &ct->tuplehash[!dir].tuple.dst.u3,
1495 info->sig_port[!dir]);
1498 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1499 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1501 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1502 set_h225_addr && ct->status & IPS_NAT_MASK) {
1504 return set_h225_addr(skb, data, 0,
1505 &arq->srcCallSignalAddress,
1506 &ct->tuplehash[!dir].tuple.dst.u3,
1513 /****************************************************************************/
1514 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1515 enum ip_conntrack_info ctinfo,
1516 unsigned char **data, AdmissionConfirm *acf)
1518 int dir = CTINFO2DIR(ctinfo);
1521 union nf_inet_addr addr;
1522 struct nf_conntrack_expect *exp;
1523 typeof(set_sig_addr_hook) set_sig_addr;
1525 pr_debug("nf_ct_ras: ACF\n");
1527 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1531 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1533 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1534 if (set_sig_addr && ct->status & IPS_NAT_MASK)
1535 return set_sig_addr(skb, ct, ctinfo, data,
1536 &acf->destCallSignalAddress, 1);
1540 /* Need new expect */
1541 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1543 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1544 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1545 IPPROTO_TCP, NULL, &port);
1546 exp->flags = NF_CT_EXPECT_PERMANENT;
1547 exp->helper = nf_conntrack_helper_q931;
1549 if (nf_ct_expect_related(exp) == 0) {
1550 pr_debug("nf_ct_ras: expect Q.931 ");
1551 nf_ct_dump_tuple(&exp->tuple);
1555 nf_ct_expect_put(exp);
1560 /****************************************************************************/
1561 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1562 enum ip_conntrack_info ctinfo,
1563 unsigned char **data, LocationRequest *lrq)
1565 typeof(set_ras_addr_hook) set_ras_addr;
1567 pr_debug("nf_ct_ras: LRQ\n");
1569 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1570 if (set_ras_addr && ct->status & IPS_NAT_MASK)
1571 return set_ras_addr(skb, ct, ctinfo, data,
1572 &lrq->replyAddress, 1);
1576 /****************************************************************************/
1577 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1578 enum ip_conntrack_info ctinfo,
1579 unsigned char **data, LocationConfirm *lcf)
1581 int dir = CTINFO2DIR(ctinfo);
1584 union nf_inet_addr addr;
1585 struct nf_conntrack_expect *exp;
1587 pr_debug("nf_ct_ras: LCF\n");
1589 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1593 /* Need new expect for call signal */
1594 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1596 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1597 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1598 IPPROTO_TCP, NULL, &port);
1599 exp->flags = NF_CT_EXPECT_PERMANENT;
1600 exp->helper = nf_conntrack_helper_q931;
1602 if (nf_ct_expect_related(exp) == 0) {
1603 pr_debug("nf_ct_ras: expect Q.931 ");
1604 nf_ct_dump_tuple(&exp->tuple);
1608 nf_ct_expect_put(exp);
1610 /* Ignore rasAddress */
1615 /****************************************************************************/
1616 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1617 enum ip_conntrack_info ctinfo,
1618 unsigned char **data, InfoRequestResponse *irr)
1621 typeof(set_ras_addr_hook) set_ras_addr;
1622 typeof(set_sig_addr_hook) set_sig_addr;
1624 pr_debug("nf_ct_ras: IRR\n");
1626 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1627 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1628 ret = set_ras_addr(skb, ct, ctinfo, data,
1629 &irr->rasAddress, 1);
1634 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1635 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1636 ret = set_sig_addr(skb, ct, ctinfo, data,
1637 irr->callSignalAddress.item,
1638 irr->callSignalAddress.count);
1646 /****************************************************************************/
1647 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1648 enum ip_conntrack_info ctinfo,
1649 unsigned char **data, RasMessage *ras)
1651 switch (ras->choice) {
1652 case eRasMessage_gatekeeperRequest:
1653 return process_grq(skb, ct, ctinfo, data,
1654 &ras->gatekeeperRequest);
1655 case eRasMessage_gatekeeperConfirm:
1656 return process_gcf(skb, ct, ctinfo, data,
1657 &ras->gatekeeperConfirm);
1658 case eRasMessage_registrationRequest:
1659 return process_rrq(skb, ct, ctinfo, data,
1660 &ras->registrationRequest);
1661 case eRasMessage_registrationConfirm:
1662 return process_rcf(skb, ct, ctinfo, data,
1663 &ras->registrationConfirm);
1664 case eRasMessage_unregistrationRequest:
1665 return process_urq(skb, ct, ctinfo, data,
1666 &ras->unregistrationRequest);
1667 case eRasMessage_admissionRequest:
1668 return process_arq(skb, ct, ctinfo, data,
1669 &ras->admissionRequest);
1670 case eRasMessage_admissionConfirm:
1671 return process_acf(skb, ct, ctinfo, data,
1672 &ras->admissionConfirm);
1673 case eRasMessage_locationRequest:
1674 return process_lrq(skb, ct, ctinfo, data,
1675 &ras->locationRequest);
1676 case eRasMessage_locationConfirm:
1677 return process_lcf(skb, ct, ctinfo, data,
1678 &ras->locationConfirm);
1679 case eRasMessage_infoRequestResponse:
1680 return process_irr(skb, ct, ctinfo, data,
1681 &ras->infoRequestResponse);
1683 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1690 /****************************************************************************/
1691 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1692 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1694 static RasMessage ras;
1695 unsigned char *data;
1699 pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1701 spin_lock_bh(&nf_h323_lock);
1704 data = get_udp_data(skb, protoff, &datalen);
1707 pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1708 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1710 /* Decode RAS message */
1711 ret = DecodeRasMessage(data, datalen, &ras);
1713 pr_debug("nf_ct_ras: decoding error: %s\n",
1714 ret == H323_ERROR_BOUND ?
1715 "out of bound" : "out of range");
1719 /* Process RAS message */
1720 if (process_ras(skb, ct, ctinfo, &data, &ras) < 0)
1724 spin_unlock_bh(&nf_h323_lock);
1728 spin_unlock_bh(&nf_h323_lock);
1729 if (net_ratelimit())
1730 printk("nf_ct_ras: packet dropped\n");
1734 /****************************************************************************/
1735 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1740 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1744 .tuple.src.l3num = AF_INET,
1745 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1746 .tuple.dst.protonum = IPPROTO_UDP,
1748 .expect_policy = &ras_exp_policy,
1753 .tuple.src.l3num = AF_INET6,
1754 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1755 .tuple.dst.protonum = IPPROTO_UDP,
1757 .expect_policy = &ras_exp_policy,
1761 /****************************************************************************/
1762 static void __exit nf_conntrack_h323_fini(void)
1764 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1765 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1766 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1767 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1768 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1770 pr_debug("nf_ct_h323: fini\n");
1773 /****************************************************************************/
1774 static int __init nf_conntrack_h323_init(void)
1778 h323_buffer = kmalloc(65536, GFP_KERNEL);
1781 ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1784 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1787 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1790 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1793 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1796 pr_debug("nf_ct_h323: init success\n");
1800 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1802 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1804 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1806 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1812 /****************************************************************************/
1813 module_init(nf_conntrack_h323_init);
1814 module_exit(nf_conntrack_h323_fini);
1816 EXPORT_SYMBOL_GPL(get_h225_addr);
1817 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1818 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1819 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1820 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1821 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1822 EXPORT_SYMBOL_GPL(nat_t120_hook);
1823 EXPORT_SYMBOL_GPL(nat_h245_hook);
1824 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1825 EXPORT_SYMBOL_GPL(nat_q931_hook);
1827 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1828 MODULE_DESCRIPTION("H.323 connection tracking helper");
1829 MODULE_LICENSE("GPL");
1830 MODULE_ALIAS("ip_conntrack_h323");
1831 MODULE_ALIAS_NFCT_HELPER("h323");