2 * H.323 connection tracking helper
4 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
5 * Copyright (c) 2006-2012 Patrick McHardy <kaber@trash.net>
7 * This source code is licensed under General Public License version 2.
9 * Based on the 'brute force' H.323 connection tracking module by
10 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 * For more information, please see http://nath323.sourceforge.net/
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/ctype.h>
18 #include <linux/inet.h>
21 #include <linux/slab.h>
22 #include <linux/udp.h>
23 #include <linux/tcp.h>
24 #include <linux/skbuff.h>
25 #include <net/route.h>
26 #include <net/ip6_route.h>
27 #include <linux/netfilter_ipv6.h>
29 #include <net/netfilter/nf_conntrack.h>
30 #include <net/netfilter/nf_conntrack_core.h>
31 #include <net/netfilter/nf_conntrack_tuple.h>
32 #include <net/netfilter/nf_conntrack_expect.h>
33 #include <net/netfilter/nf_conntrack_ecache.h>
34 #include <net/netfilter/nf_conntrack_helper.h>
35 #include <net/netfilter/nf_conntrack_zones.h>
36 #include <linux/netfilter/nf_conntrack_h323.h>
39 static unsigned int default_rrq_ttl __read_mostly = 300;
40 module_param(default_rrq_ttl, uint, 0600);
41 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
43 static int gkrouted_only __read_mostly = 1;
44 module_param(gkrouted_only, int, 0600);
45 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
47 static bool callforward_filter __read_mostly = true;
48 module_param(callforward_filter, bool, 0600);
49 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
50 "if both endpoints are on different sides "
51 "(determined by routing information)");
54 int (*set_h245_addr_hook) (struct sk_buff *skb, unsigned int protoff,
55 unsigned char **data, int dataoff,
56 H245_TransportAddress *taddr,
57 union nf_inet_addr *addr, __be16 port)
59 int (*set_h225_addr_hook) (struct sk_buff *skb, unsigned int protoff,
60 unsigned char **data, int dataoff,
61 TransportAddress *taddr,
62 union nf_inet_addr *addr, __be16 port)
64 int (*set_sig_addr_hook) (struct sk_buff *skb,
66 enum ip_conntrack_info ctinfo,
67 unsigned int protoff, unsigned char **data,
68 TransportAddress *taddr, int count) __read_mostly;
69 int (*set_ras_addr_hook) (struct sk_buff *skb,
71 enum ip_conntrack_info ctinfo,
72 unsigned int protoff, unsigned char **data,
73 TransportAddress *taddr, int count) __read_mostly;
74 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
76 enum ip_conntrack_info ctinfo,
78 unsigned char **data, int dataoff,
79 H245_TransportAddress *taddr,
80 __be16 port, __be16 rtp_port,
81 struct nf_conntrack_expect *rtp_exp,
82 struct nf_conntrack_expect *rtcp_exp) __read_mostly;
83 int (*nat_t120_hook) (struct sk_buff *skb,
85 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 *skb,
92 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_callforwarding_hook) (struct sk_buff *skb,
99 enum ip_conntrack_info ctinfo,
100 unsigned int protoff,
101 unsigned char **data, int dataoff,
102 TransportAddress *taddr, __be16 port,
103 struct nf_conntrack_expect *exp) __read_mostly;
104 int (*nat_q931_hook) (struct sk_buff *skb,
106 enum ip_conntrack_info ctinfo,
107 unsigned int protoff,
108 unsigned char **data, TransportAddress *taddr, int idx,
109 __be16 port, struct nf_conntrack_expect *exp)
112 static DEFINE_SPINLOCK(nf_h323_lock);
113 static char *h323_buffer;
115 static struct nf_conntrack_helper nf_conntrack_helper_h245;
116 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
117 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
119 static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,
120 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
121 unsigned char **data, int *datalen, int *dataoff)
123 struct nf_ct_h323_master *info = nfct_help_data(ct);
124 int dir = CTINFO2DIR(ctinfo);
125 const struct tcphdr *th;
134 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
138 /* Get TCP data offset */
139 tcpdataoff = protoff + th->doff * 4;
141 /* Get TCP data length */
142 tcpdatalen = skb->len - tcpdataoff;
143 if (tcpdatalen <= 0) /* No TCP data */
146 if (*data == NULL) { /* first TPKT */
147 /* Get first TPKT pointer */
148 tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
150 BUG_ON(tpkt == NULL);
152 /* Validate TPKT identifier */
153 if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) {
154 /* Netmeeting sends TPKT header and data separately */
155 if (info->tpkt_len[dir] > 0) {
156 pr_debug("nf_ct_h323: previous packet "
157 "indicated separate TPKT data of %hu "
158 "bytes\n", info->tpkt_len[dir]);
159 if (info->tpkt_len[dir] <= tcpdatalen) {
160 /* Yes, there was a TPKT header
163 *datalen = info->tpkt_len[dir];
168 /* Fragmented TPKT */
169 pr_debug("nf_ct_h323: fragmented TPKT\n");
173 /* It is not even a TPKT */
177 } else { /* Next TPKT */
178 tpktoff = *dataoff + *datalen;
179 tcpdatalen -= tpktoff;
180 if (tcpdatalen <= 4) /* No more TPKT */
182 tpkt = *data + *datalen;
184 /* Validate TPKT identifier */
185 if (tpkt[0] != 0x03 || tpkt[1] != 0)
189 /* Validate TPKT length */
190 tpktlen = tpkt[2] * 256 + tpkt[3];
193 if (tpktlen > tcpdatalen) {
194 if (tcpdatalen == 4) { /* Separate TPKT header */
195 /* Netmeeting sends TPKT header and data separately */
196 pr_debug("nf_ct_h323: separate TPKT header indicates "
197 "there will be TPKT data of %hu bytes\n",
199 info->tpkt_len[dir] = tpktlen - 4;
203 pr_debug("nf_ct_h323: incomplete TPKT (fragmented?)\n");
207 /* This is the encapsulated data */
209 *datalen = tpktlen - 4;
210 *dataoff = tpktoff + 4;
213 /* Clear TPKT length */
214 info->tpkt_len[dir] = 0;
218 info->tpkt_len[dir] = 0;
222 static int get_h245_addr(struct nf_conn *ct, const unsigned char *data,
223 H245_TransportAddress *taddr,
224 union nf_inet_addr *addr, __be16 *port)
226 const unsigned char *p;
229 if (taddr->choice != eH245_TransportAddress_unicastAddress)
232 switch (taddr->unicastAddress.choice) {
233 case eUnicastAddress_iPAddress:
234 if (nf_ct_l3num(ct) != AF_INET)
236 p = data + taddr->unicastAddress.iPAddress.network;
239 case eUnicastAddress_iP6Address:
240 if (nf_ct_l3num(ct) != 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 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
257 enum ip_conntrack_info ctinfo,
258 unsigned int protoff,
259 unsigned char **data, int dataoff,
260 H245_TransportAddress *taddr)
262 int dir = CTINFO2DIR(ctinfo);
265 __be16 rtp_port, rtcp_port;
266 union nf_inet_addr 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 */
278 rtp_port = port & ~htons(1);
279 rtcp_port = port | htons(1);
281 /* Create expect for RTP */
282 if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
284 nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
285 &ct->tuplehash[!dir].tuple.src.u3,
286 &ct->tuplehash[!dir].tuple.dst.u3,
287 IPPROTO_UDP, NULL, &rtp_port);
289 /* Create expect for RTCP */
290 if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
291 nf_ct_expect_put(rtp_exp);
294 nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
295 &ct->tuplehash[!dir].tuple.src.u3,
296 &ct->tuplehash[!dir].tuple.dst.u3,
297 IPPROTO_UDP, NULL, &rtcp_port);
299 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
300 &ct->tuplehash[!dir].tuple.dst.u3,
301 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
302 (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
303 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
304 ct->status & IPS_NAT_MASK) {
306 ret = nat_rtp_rtcp(skb, ct, ctinfo, protoff, 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 pr_debug("nf_ct_h323: expect RTP ");
312 nf_ct_dump_tuple(&rtp_exp->tuple);
313 pr_debug("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 static int expect_t120(struct sk_buff *skb,
331 enum ip_conntrack_info ctinfo,
332 unsigned int protoff,
333 unsigned char **data, int dataoff,
334 H245_TransportAddress *taddr)
336 int dir = CTINFO2DIR(ctinfo);
339 union nf_inet_addr 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, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
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 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
363 ct->status & IPS_NAT_MASK) {
365 ret = nat_t120(skb, ct, ctinfo, protoff, data, dataoff, taddr,
367 } else { /* Conntrack only */
368 if (nf_ct_expect_related(exp) == 0) {
369 pr_debug("nf_ct_h323: expect T.120 ");
370 nf_ct_dump_tuple(&exp->tuple);
375 nf_ct_expect_put(exp);
380 static int process_h245_channel(struct sk_buff *skb,
382 enum ip_conntrack_info ctinfo,
383 unsigned int protoff,
384 unsigned char **data, int dataoff,
385 H2250LogicalChannelParameters *channel)
389 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
391 ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
392 &channel->mediaChannel);
398 options & eH2250LogicalChannelParameters_mediaControlChannel) {
400 ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
401 &channel->mediaControlChannel);
409 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
410 enum ip_conntrack_info ctinfo,
411 unsigned int protoff,
412 unsigned char **data, int dataoff,
413 OpenLogicalChannel *olc)
417 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
419 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
420 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
422 ret = process_h245_channel(skb, ct, ctinfo,
423 protoff, data, dataoff,
425 forwardLogicalChannelParameters.
427 h2250LogicalChannelParameters);
433 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
434 (olc->reverseLogicalChannelParameters.options &
435 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
436 && (olc->reverseLogicalChannelParameters.multiplexParameters.
438 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
441 process_h245_channel(skb, ct, ctinfo,
442 protoff, data, dataoff,
444 reverseLogicalChannelParameters.
446 h2250LogicalChannelParameters);
451 if ((olc->options & eOpenLogicalChannel_separateStack) &&
452 olc->forwardLogicalChannelParameters.dataType.choice ==
454 olc->forwardLogicalChannelParameters.dataType.data.application.
455 choice == eDataApplicationCapability_application_t120 &&
456 olc->forwardLogicalChannelParameters.dataType.data.application.
457 t120.choice == eDataProtocolCapability_separateLANStack &&
458 olc->separateStack.networkAddress.choice ==
459 eNetworkAccessParameters_networkAddress_localAreaAddress) {
460 ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
461 &olc->separateStack.networkAddress.
470 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
471 enum ip_conntrack_info ctinfo,
472 unsigned int protoff, unsigned char **data, int dataoff,
473 OpenLogicalChannelAck *olca)
475 H2250LogicalChannelAckParameters *ack;
478 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
481 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
482 (olca->reverseLogicalChannelParameters.options &
483 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
484 && (olca->reverseLogicalChannelParameters.multiplexParameters.
486 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
488 ret = process_h245_channel(skb, ct, ctinfo,
489 protoff, data, dataoff,
491 reverseLogicalChannelParameters.
493 h2250LogicalChannelParameters);
499 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
500 (olca->forwardMultiplexAckParameters.choice ==
501 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
503 ack = &olca->forwardMultiplexAckParameters.
504 h2250LogicalChannelAckParameters;
506 eH2250LogicalChannelAckParameters_mediaChannel) {
508 ret = expect_rtp_rtcp(skb, ct, ctinfo,
509 protoff, data, dataoff,
516 eH2250LogicalChannelAckParameters_mediaControlChannel) {
518 ret = expect_rtp_rtcp(skb, ct, ctinfo,
519 protoff, data, dataoff,
520 &ack->mediaControlChannel);
526 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
527 olca->separateStack.networkAddress.choice ==
528 eNetworkAccessParameters_networkAddress_localAreaAddress) {
529 ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
530 &olca->separateStack.networkAddress.
539 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
540 enum ip_conntrack_info ctinfo,
541 unsigned int protoff, unsigned char **data, int dataoff,
542 MultimediaSystemControlMessage *mscm)
544 switch (mscm->choice) {
545 case eMultimediaSystemControlMessage_request:
546 if (mscm->request.choice ==
547 eRequestMessage_openLogicalChannel) {
548 return process_olc(skb, ct, ctinfo,
549 protoff, data, dataoff,
550 &mscm->request.openLogicalChannel);
552 pr_debug("nf_ct_h323: H.245 Request %d\n",
553 mscm->request.choice);
555 case eMultimediaSystemControlMessage_response:
556 if (mscm->response.choice ==
557 eResponseMessage_openLogicalChannelAck) {
558 return process_olca(skb, ct, ctinfo,
559 protoff, data, dataoff,
561 openLogicalChannelAck);
563 pr_debug("nf_ct_h323: H.245 Response %d\n",
564 mscm->response.choice);
567 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice);
574 static int h245_help(struct sk_buff *skb, unsigned int protoff,
575 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
577 static MultimediaSystemControlMessage mscm;
578 unsigned char *data = NULL;
583 /* Until there's been traffic both ways, don't look in packets. */
584 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
587 pr_debug("nf_ct_h245: skblen = %u\n", skb->len);
589 spin_lock_bh(&nf_h323_lock);
591 /* Process each TPKT */
592 while (get_tpkt_data(skb, protoff, ct, ctinfo,
593 &data, &datalen, &dataoff)) {
594 pr_debug("nf_ct_h245: TPKT len=%d ", datalen);
595 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
597 /* Decode H.245 signal */
598 ret = DecodeMultimediaSystemControlMessage(data, datalen,
601 pr_debug("nf_ct_h245: decoding error: %s\n",
602 ret == H323_ERROR_BOUND ?
603 "out of bound" : "out of range");
604 /* We don't drop when decoding error */
608 /* Process H.245 signal */
609 if (process_h245(skb, ct, ctinfo, protoff,
610 &data, dataoff, &mscm) < 0)
614 spin_unlock_bh(&nf_h323_lock);
618 spin_unlock_bh(&nf_h323_lock);
619 nf_ct_helper_log(skb, ct, "cannot process H.245 message");
623 static const struct nf_conntrack_expect_policy h245_exp_policy = {
624 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
628 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
631 .tuple.src.l3num = AF_UNSPEC,
632 .tuple.dst.protonum = IPPROTO_UDP,
634 .expect_policy = &h245_exp_policy,
637 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
638 TransportAddress *taddr,
639 union nf_inet_addr *addr, __be16 *port)
641 const unsigned char *p;
644 switch (taddr->choice) {
645 case eTransportAddress_ipAddress:
646 if (nf_ct_l3num(ct) != AF_INET)
648 p = data + taddr->ipAddress.ip;
651 case eTransportAddress_ip6Address:
652 if (nf_ct_l3num(ct) != AF_INET6)
654 p = data + taddr->ip6Address.ip;
661 memcpy(addr, p, len);
662 memset((void *)addr + len, 0, sizeof(*addr) - len);
663 memcpy(port, p + len, sizeof(__be16));
668 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
669 enum ip_conntrack_info ctinfo,
670 unsigned int protoff, unsigned char **data, int dataoff,
671 TransportAddress *taddr)
673 int dir = CTINFO2DIR(ctinfo);
676 union nf_inet_addr addr;
677 struct nf_conntrack_expect *exp;
678 typeof(nat_h245_hook) nat_h245;
680 /* Read h245Address */
681 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
682 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
686 /* Create expect for h245 connection */
687 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
689 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
690 &ct->tuplehash[!dir].tuple.src.u3,
691 &ct->tuplehash[!dir].tuple.dst.u3,
692 IPPROTO_TCP, NULL, &port);
693 exp->helper = &nf_conntrack_helper_h245;
695 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
696 &ct->tuplehash[!dir].tuple.dst.u3,
697 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
698 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
699 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
700 ct->status & IPS_NAT_MASK) {
702 ret = nat_h245(skb, ct, ctinfo, protoff, data, dataoff, taddr,
704 } else { /* Conntrack only */
705 if (nf_ct_expect_related(exp) == 0) {
706 pr_debug("nf_ct_q931: expect H.245 ");
707 nf_ct_dump_tuple(&exp->tuple);
712 nf_ct_expect_put(exp);
717 /* If the calling party is on the same side of the forward-to party,
718 * we don't need to track the second call
720 static int callforward_do_filter(struct net *net,
721 const union nf_inet_addr *src,
722 const union nf_inet_addr *dst,
729 struct flowi4 fl1, fl2;
730 struct rtable *rt1, *rt2;
732 memset(&fl1, 0, sizeof(fl1));
735 memset(&fl2, 0, sizeof(fl2));
737 if (!nf_ip_route(net, (struct dst_entry **)&rt1,
738 flowi4_to_flowi(&fl1), false)) {
739 if (!nf_ip_route(net, (struct dst_entry **)&rt2,
740 flowi4_to_flowi(&fl2), false)) {
741 if (rt_nexthop(rt1, fl1.daddr) ==
742 rt_nexthop(rt2, fl2.daddr) &&
743 rt1->dst.dev == rt2->dst.dev)
745 dst_release(&rt2->dst);
747 dst_release(&rt1->dst);
751 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV6)
753 const struct nf_ipv6_ops *v6ops;
754 struct rt6_info *rt1, *rt2;
755 struct flowi6 fl1, fl2;
757 v6ops = nf_get_ipv6_ops();
761 memset(&fl1, 0, sizeof(fl1));
762 fl1.daddr = src->in6;
764 memset(&fl2, 0, sizeof(fl2));
765 fl2.daddr = dst->in6;
766 if (!v6ops->route(net, (struct dst_entry **)&rt1,
767 flowi6_to_flowi(&fl1), false)) {
768 if (!v6ops->route(net, (struct dst_entry **)&rt2,
769 flowi6_to_flowi(&fl2), false)) {
770 if (ipv6_addr_equal(rt6_nexthop(rt1, &fl1.daddr),
771 rt6_nexthop(rt2, &fl2.daddr)) &&
772 rt1->dst.dev == rt2->dst.dev)
774 dst_release(&rt2->dst);
776 dst_release(&rt1->dst);
786 static int expect_callforwarding(struct sk_buff *skb,
788 enum ip_conntrack_info ctinfo,
789 unsigned int protoff,
790 unsigned char **data, int dataoff,
791 TransportAddress *taddr)
793 int dir = CTINFO2DIR(ctinfo);
796 union nf_inet_addr addr;
797 struct nf_conntrack_expect *exp;
798 struct net *net = nf_ct_net(ct);
799 typeof(nat_callforwarding_hook) nat_callforwarding;
801 /* Read alternativeAddress */
802 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
805 /* If the calling party is on the same side of the forward-to party,
806 * we don't need to track the second call
808 if (callforward_filter &&
809 callforward_do_filter(net, &addr, &ct->tuplehash[!dir].tuple.src.u3,
811 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
815 /* Create expect for the second call leg */
816 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
818 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
819 &ct->tuplehash[!dir].tuple.src.u3, &addr,
820 IPPROTO_TCP, NULL, &port);
821 exp->helper = nf_conntrack_helper_q931;
823 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
824 &ct->tuplehash[!dir].tuple.dst.u3,
825 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
826 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
827 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
828 ct->status & IPS_NAT_MASK) {
830 ret = nat_callforwarding(skb, ct, ctinfo,
831 protoff, data, dataoff,
833 } else { /* Conntrack only */
834 if (nf_ct_expect_related(exp) == 0) {
835 pr_debug("nf_ct_q931: expect Call Forwarding ");
836 nf_ct_dump_tuple(&exp->tuple);
841 nf_ct_expect_put(exp);
846 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
847 enum ip_conntrack_info ctinfo,
848 unsigned int protoff,
849 unsigned char **data, int dataoff,
852 int dir = CTINFO2DIR(ctinfo);
856 union nf_inet_addr addr;
857 typeof(set_h225_addr_hook) set_h225_addr;
859 pr_debug("nf_ct_q931: Setup\n");
861 if (setup->options & eSetup_UUIE_h245Address) {
862 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
863 &setup->h245Address);
868 set_h225_addr = rcu_dereference(set_h225_addr_hook);
869 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
870 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
871 ct->status & IPS_NAT_MASK &&
872 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
874 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
875 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
876 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3,
877 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
878 ret = set_h225_addr(skb, protoff, data, dataoff,
879 &setup->destCallSignalAddress,
880 &ct->tuplehash[!dir].tuple.src.u3,
881 ct->tuplehash[!dir].tuple.src.u.tcp.port);
886 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
887 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
888 ct->status & IPS_NAT_MASK &&
889 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
891 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
892 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
893 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3,
894 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
895 ret = set_h225_addr(skb, protoff, data, dataoff,
896 &setup->sourceCallSignalAddress,
897 &ct->tuplehash[!dir].tuple.dst.u3,
898 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
903 if (setup->options & eSetup_UUIE_fastStart) {
904 for (i = 0; i < setup->fastStart.count; i++) {
905 ret = process_olc(skb, ct, ctinfo,
906 protoff, data, dataoff,
907 &setup->fastStart.item[i]);
916 static int process_callproceeding(struct sk_buff *skb,
918 enum ip_conntrack_info ctinfo,
919 unsigned int protoff,
920 unsigned char **data, int dataoff,
921 CallProceeding_UUIE *callproc)
926 pr_debug("nf_ct_q931: CallProceeding\n");
928 if (callproc->options & eCallProceeding_UUIE_h245Address) {
929 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
930 &callproc->h245Address);
935 if (callproc->options & eCallProceeding_UUIE_fastStart) {
936 for (i = 0; i < callproc->fastStart.count; i++) {
937 ret = process_olc(skb, ct, ctinfo,
938 protoff, data, dataoff,
939 &callproc->fastStart.item[i]);
948 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
949 enum ip_conntrack_info ctinfo,
950 unsigned int protoff,
951 unsigned char **data, int dataoff,
952 Connect_UUIE *connect)
957 pr_debug("nf_ct_q931: Connect\n");
959 if (connect->options & eConnect_UUIE_h245Address) {
960 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
961 &connect->h245Address);
966 if (connect->options & eConnect_UUIE_fastStart) {
967 for (i = 0; i < connect->fastStart.count; i++) {
968 ret = process_olc(skb, ct, ctinfo,
969 protoff, data, dataoff,
970 &connect->fastStart.item[i]);
979 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
980 enum ip_conntrack_info ctinfo,
981 unsigned int protoff,
982 unsigned char **data, int dataoff,
983 Alerting_UUIE *alert)
988 pr_debug("nf_ct_q931: Alerting\n");
990 if (alert->options & eAlerting_UUIE_h245Address) {
991 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
992 &alert->h245Address);
997 if (alert->options & eAlerting_UUIE_fastStart) {
998 for (i = 0; i < alert->fastStart.count; i++) {
999 ret = process_olc(skb, ct, ctinfo,
1000 protoff, data, dataoff,
1001 &alert->fastStart.item[i]);
1010 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
1011 enum ip_conntrack_info ctinfo,
1012 unsigned int protoff,
1013 unsigned char **data, int dataoff,
1014 Facility_UUIE *facility)
1019 pr_debug("nf_ct_q931: Facility\n");
1021 if (facility->reason.choice == eFacilityReason_callForwarded) {
1022 if (facility->options & eFacility_UUIE_alternativeAddress)
1023 return expect_callforwarding(skb, ct, ctinfo,
1024 protoff, data, dataoff,
1026 alternativeAddress);
1030 if (facility->options & eFacility_UUIE_h245Address) {
1031 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1032 &facility->h245Address);
1037 if (facility->options & eFacility_UUIE_fastStart) {
1038 for (i = 0; i < facility->fastStart.count; i++) {
1039 ret = process_olc(skb, ct, ctinfo,
1040 protoff, data, dataoff,
1041 &facility->fastStart.item[i]);
1050 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1051 enum ip_conntrack_info ctinfo,
1052 unsigned int protoff,
1053 unsigned char **data, int dataoff,
1054 Progress_UUIE *progress)
1059 pr_debug("nf_ct_q931: Progress\n");
1061 if (progress->options & eProgress_UUIE_h245Address) {
1062 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1063 &progress->h245Address);
1068 if (progress->options & eProgress_UUIE_fastStart) {
1069 for (i = 0; i < progress->fastStart.count; i++) {
1070 ret = process_olc(skb, ct, ctinfo,
1071 protoff, data, dataoff,
1072 &progress->fastStart.item[i]);
1081 static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1082 enum ip_conntrack_info ctinfo,
1083 unsigned int protoff, unsigned char **data, int dataoff,
1086 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1090 switch (pdu->h323_message_body.choice) {
1091 case eH323_UU_PDU_h323_message_body_setup:
1092 ret = process_setup(skb, ct, ctinfo, protoff, data, dataoff,
1093 &pdu->h323_message_body.setup);
1095 case eH323_UU_PDU_h323_message_body_callProceeding:
1096 ret = process_callproceeding(skb, ct, ctinfo,
1097 protoff, data, dataoff,
1098 &pdu->h323_message_body.
1101 case eH323_UU_PDU_h323_message_body_connect:
1102 ret = process_connect(skb, ct, ctinfo, protoff, data, dataoff,
1103 &pdu->h323_message_body.connect);
1105 case eH323_UU_PDU_h323_message_body_alerting:
1106 ret = process_alerting(skb, ct, ctinfo, protoff, data, dataoff,
1107 &pdu->h323_message_body.alerting);
1109 case eH323_UU_PDU_h323_message_body_facility:
1110 ret = process_facility(skb, ct, ctinfo, protoff, data, dataoff,
1111 &pdu->h323_message_body.facility);
1113 case eH323_UU_PDU_h323_message_body_progress:
1114 ret = process_progress(skb, ct, ctinfo, protoff, data, dataoff,
1115 &pdu->h323_message_body.progress);
1118 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1119 pdu->h323_message_body.choice);
1126 if (pdu->options & eH323_UU_PDU_h245Control) {
1127 for (i = 0; i < pdu->h245Control.count; i++) {
1128 ret = process_h245(skb, ct, ctinfo,
1129 protoff, data, dataoff,
1130 &pdu->h245Control.item[i]);
1139 static int q931_help(struct sk_buff *skb, unsigned int protoff,
1140 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1143 unsigned char *data = NULL;
1148 /* Until there's been traffic both ways, don't look in packets. */
1149 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
1152 pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1154 spin_lock_bh(&nf_h323_lock);
1156 /* Process each TPKT */
1157 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1158 &data, &datalen, &dataoff)) {
1159 pr_debug("nf_ct_q931: TPKT len=%d ", datalen);
1160 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1162 /* Decode Q.931 signal */
1163 ret = DecodeQ931(data, datalen, &q931);
1165 pr_debug("nf_ct_q931: decoding error: %s\n",
1166 ret == H323_ERROR_BOUND ?
1167 "out of bound" : "out of range");
1168 /* We don't drop when decoding error */
1172 /* Process Q.931 signal */
1173 if (process_q931(skb, ct, ctinfo, protoff,
1174 &data, dataoff, &q931) < 0)
1178 spin_unlock_bh(&nf_h323_lock);
1182 spin_unlock_bh(&nf_h323_lock);
1183 nf_ct_helper_log(skb, ct, "cannot process Q.931 message");
1187 static const struct nf_conntrack_expect_policy q931_exp_policy = {
1188 /* T.120 and H.245 */
1189 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1193 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1197 .tuple.src.l3num = AF_INET,
1198 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1199 .tuple.dst.protonum = IPPROTO_TCP,
1201 .expect_policy = &q931_exp_policy,
1206 .tuple.src.l3num = AF_INET6,
1207 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1208 .tuple.dst.protonum = IPPROTO_TCP,
1210 .expect_policy = &q931_exp_policy,
1214 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1217 const struct udphdr *uh;
1221 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1224 dataoff = protoff + sizeof(_uh);
1225 if (dataoff >= skb->len)
1227 *datalen = skb->len - dataoff;
1228 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1231 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1232 union nf_inet_addr *addr,
1235 struct net *net = nf_ct_net(ct);
1236 struct nf_conntrack_expect *exp;
1237 struct nf_conntrack_tuple tuple;
1239 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1240 tuple.src.u.tcp.port = 0;
1241 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1242 tuple.dst.u.tcp.port = port;
1243 tuple.dst.protonum = IPPROTO_TCP;
1245 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1246 if (exp && exp->master == ct)
1251 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1252 enum ip_conntrack_info ctinfo,
1253 unsigned int protoff, unsigned char **data,
1254 TransportAddress *taddr, int count)
1256 struct nf_ct_h323_master *info = nfct_help_data(ct);
1257 int dir = CTINFO2DIR(ctinfo);
1261 union nf_inet_addr addr;
1262 struct nf_conntrack_expect *exp;
1263 typeof(nat_q931_hook) nat_q931;
1265 /* Look for the first related address */
1266 for (i = 0; i < count; i++) {
1267 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1268 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1269 sizeof(addr)) == 0 && port != 0)
1273 if (i >= count) /* Not found */
1276 /* Create expect for Q.931 */
1277 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1279 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1280 gkrouted_only ? /* only accept calls from GK? */
1281 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1282 &ct->tuplehash[!dir].tuple.dst.u3,
1283 IPPROTO_TCP, NULL, &port);
1284 exp->helper = nf_conntrack_helper_q931;
1285 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1287 nat_q931 = rcu_dereference(nat_q931_hook);
1288 if (nat_q931 && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1289 ct->status & IPS_NAT_MASK) { /* Need NAT */
1290 ret = nat_q931(skb, ct, ctinfo, protoff, data,
1291 taddr, i, port, exp);
1292 } else { /* Conntrack only */
1293 if (nf_ct_expect_related(exp) == 0) {
1294 pr_debug("nf_ct_ras: expect Q.931 ");
1295 nf_ct_dump_tuple(&exp->tuple);
1297 /* Save port for looking up expect in processing RCF */
1298 info->sig_port[dir] = port;
1303 nf_ct_expect_put(exp);
1308 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1309 enum ip_conntrack_info ctinfo,
1310 unsigned int protoff,
1311 unsigned char **data, GatekeeperRequest *grq)
1313 typeof(set_ras_addr_hook) set_ras_addr;
1315 pr_debug("nf_ct_ras: GRQ\n");
1317 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1318 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1319 ct->status & IPS_NAT_MASK) /* NATed */
1320 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1321 &grq->rasAddress, 1);
1325 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1326 enum ip_conntrack_info ctinfo,
1327 unsigned int protoff,
1328 unsigned char **data, GatekeeperConfirm *gcf)
1330 int dir = CTINFO2DIR(ctinfo);
1333 union nf_inet_addr addr;
1334 struct nf_conntrack_expect *exp;
1336 pr_debug("nf_ct_ras: GCF\n");
1338 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1341 /* Registration port is the same as discovery port */
1342 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1343 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1346 /* Avoid RAS expectation loops. A GCF is never expected. */
1347 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1350 /* Need new expect */
1351 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1353 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1354 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1355 IPPROTO_UDP, NULL, &port);
1356 exp->helper = nf_conntrack_helper_ras;
1358 if (nf_ct_expect_related(exp) == 0) {
1359 pr_debug("nf_ct_ras: expect RAS ");
1360 nf_ct_dump_tuple(&exp->tuple);
1364 nf_ct_expect_put(exp);
1369 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1370 enum ip_conntrack_info ctinfo,
1371 unsigned int protoff,
1372 unsigned char **data, RegistrationRequest *rrq)
1374 struct nf_ct_h323_master *info = nfct_help_data(ct);
1376 typeof(set_ras_addr_hook) set_ras_addr;
1378 pr_debug("nf_ct_ras: RRQ\n");
1380 ret = expect_q931(skb, ct, ctinfo, protoff, data,
1381 rrq->callSignalAddress.item,
1382 rrq->callSignalAddress.count);
1386 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1387 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1388 ct->status & IPS_NAT_MASK) {
1389 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1390 rrq->rasAddress.item,
1391 rrq->rasAddress.count);
1396 if (rrq->options & eRegistrationRequest_timeToLive) {
1397 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1398 info->timeout = rrq->timeToLive;
1400 info->timeout = default_rrq_ttl;
1405 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1406 enum ip_conntrack_info ctinfo,
1407 unsigned int protoff,
1408 unsigned char **data, RegistrationConfirm *rcf)
1410 struct nf_ct_h323_master *info = nfct_help_data(ct);
1411 int dir = CTINFO2DIR(ctinfo);
1413 struct nf_conntrack_expect *exp;
1414 typeof(set_sig_addr_hook) set_sig_addr;
1416 pr_debug("nf_ct_ras: RCF\n");
1418 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1419 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1420 ct->status & IPS_NAT_MASK) {
1421 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1422 rcf->callSignalAddress.item,
1423 rcf->callSignalAddress.count);
1428 if (rcf->options & eRegistrationConfirm_timeToLive) {
1429 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1430 info->timeout = rcf->timeToLive;
1433 if (info->timeout > 0) {
1434 pr_debug("nf_ct_ras: set RAS connection timeout to "
1435 "%u seconds\n", info->timeout);
1436 nf_ct_refresh(ct, skb, info->timeout * HZ);
1438 /* Set expect timeout */
1439 spin_lock_bh(&nf_conntrack_expect_lock);
1440 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1441 info->sig_port[!dir]);
1443 pr_debug("nf_ct_ras: set Q.931 expect "
1444 "timeout to %u seconds for",
1446 nf_ct_dump_tuple(&exp->tuple);
1447 mod_timer_pending(&exp->timeout,
1448 jiffies + info->timeout * HZ);
1450 spin_unlock_bh(&nf_conntrack_expect_lock);
1456 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1457 enum ip_conntrack_info ctinfo,
1458 unsigned int protoff,
1459 unsigned char **data, UnregistrationRequest *urq)
1461 struct nf_ct_h323_master *info = nfct_help_data(ct);
1462 int dir = CTINFO2DIR(ctinfo);
1464 typeof(set_sig_addr_hook) set_sig_addr;
1466 pr_debug("nf_ct_ras: URQ\n");
1468 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1469 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1470 ct->status & IPS_NAT_MASK) {
1471 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1472 urq->callSignalAddress.item,
1473 urq->callSignalAddress.count);
1478 /* Clear old expect */
1479 nf_ct_remove_expectations(ct);
1480 info->sig_port[dir] = 0;
1481 info->sig_port[!dir] = 0;
1483 /* Give it 30 seconds for UCF or URJ */
1484 nf_ct_refresh(ct, skb, 30 * HZ);
1489 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1490 enum ip_conntrack_info ctinfo,
1491 unsigned int protoff,
1492 unsigned char **data, AdmissionRequest *arq)
1494 const struct nf_ct_h323_master *info = nfct_help_data(ct);
1495 int dir = CTINFO2DIR(ctinfo);
1497 union nf_inet_addr addr;
1498 typeof(set_h225_addr_hook) set_h225_addr;
1500 pr_debug("nf_ct_ras: ARQ\n");
1502 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1503 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1504 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1506 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1507 port == info->sig_port[dir] &&
1508 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1509 set_h225_addr && ct->status & IPS_NAT_MASK) {
1511 return set_h225_addr(skb, protoff, data, 0,
1512 &arq->destCallSignalAddress,
1513 &ct->tuplehash[!dir].tuple.dst.u3,
1514 info->sig_port[!dir]);
1517 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1518 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1520 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1521 set_h225_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1522 ct->status & IPS_NAT_MASK) {
1524 return set_h225_addr(skb, protoff, data, 0,
1525 &arq->srcCallSignalAddress,
1526 &ct->tuplehash[!dir].tuple.dst.u3,
1533 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1534 enum ip_conntrack_info ctinfo,
1535 unsigned int protoff,
1536 unsigned char **data, AdmissionConfirm *acf)
1538 int dir = CTINFO2DIR(ctinfo);
1541 union nf_inet_addr addr;
1542 struct nf_conntrack_expect *exp;
1543 typeof(set_sig_addr_hook) set_sig_addr;
1545 pr_debug("nf_ct_ras: ACF\n");
1547 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1551 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1553 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1554 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1555 ct->status & IPS_NAT_MASK)
1556 return set_sig_addr(skb, ct, ctinfo, protoff, data,
1557 &acf->destCallSignalAddress, 1);
1561 /* Need new expect */
1562 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1564 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1565 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1566 IPPROTO_TCP, NULL, &port);
1567 exp->flags = NF_CT_EXPECT_PERMANENT;
1568 exp->helper = nf_conntrack_helper_q931;
1570 if (nf_ct_expect_related(exp) == 0) {
1571 pr_debug("nf_ct_ras: expect Q.931 ");
1572 nf_ct_dump_tuple(&exp->tuple);
1576 nf_ct_expect_put(exp);
1581 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1582 enum ip_conntrack_info ctinfo,
1583 unsigned int protoff,
1584 unsigned char **data, LocationRequest *lrq)
1586 typeof(set_ras_addr_hook) set_ras_addr;
1588 pr_debug("nf_ct_ras: LRQ\n");
1590 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1591 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1592 ct->status & IPS_NAT_MASK)
1593 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1594 &lrq->replyAddress, 1);
1598 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1599 enum ip_conntrack_info ctinfo,
1600 unsigned int protoff,
1601 unsigned char **data, LocationConfirm *lcf)
1603 int dir = CTINFO2DIR(ctinfo);
1606 union nf_inet_addr addr;
1607 struct nf_conntrack_expect *exp;
1609 pr_debug("nf_ct_ras: LCF\n");
1611 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1615 /* Need new expect for call signal */
1616 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1618 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1619 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1620 IPPROTO_TCP, NULL, &port);
1621 exp->flags = NF_CT_EXPECT_PERMANENT;
1622 exp->helper = nf_conntrack_helper_q931;
1624 if (nf_ct_expect_related(exp) == 0) {
1625 pr_debug("nf_ct_ras: expect Q.931 ");
1626 nf_ct_dump_tuple(&exp->tuple);
1630 nf_ct_expect_put(exp);
1632 /* Ignore rasAddress */
1637 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1638 enum ip_conntrack_info ctinfo,
1639 unsigned int protoff,
1640 unsigned char **data, InfoRequestResponse *irr)
1643 typeof(set_ras_addr_hook) set_ras_addr;
1644 typeof(set_sig_addr_hook) set_sig_addr;
1646 pr_debug("nf_ct_ras: IRR\n");
1648 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1649 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1650 ct->status & IPS_NAT_MASK) {
1651 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1652 &irr->rasAddress, 1);
1657 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1658 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1659 ct->status & IPS_NAT_MASK) {
1660 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1661 irr->callSignalAddress.item,
1662 irr->callSignalAddress.count);
1670 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1671 enum ip_conntrack_info ctinfo,
1672 unsigned int protoff,
1673 unsigned char **data, RasMessage *ras)
1675 switch (ras->choice) {
1676 case eRasMessage_gatekeeperRequest:
1677 return process_grq(skb, ct, ctinfo, protoff, data,
1678 &ras->gatekeeperRequest);
1679 case eRasMessage_gatekeeperConfirm:
1680 return process_gcf(skb, ct, ctinfo, protoff, data,
1681 &ras->gatekeeperConfirm);
1682 case eRasMessage_registrationRequest:
1683 return process_rrq(skb, ct, ctinfo, protoff, data,
1684 &ras->registrationRequest);
1685 case eRasMessage_registrationConfirm:
1686 return process_rcf(skb, ct, ctinfo, protoff, data,
1687 &ras->registrationConfirm);
1688 case eRasMessage_unregistrationRequest:
1689 return process_urq(skb, ct, ctinfo, protoff, data,
1690 &ras->unregistrationRequest);
1691 case eRasMessage_admissionRequest:
1692 return process_arq(skb, ct, ctinfo, protoff, data,
1693 &ras->admissionRequest);
1694 case eRasMessage_admissionConfirm:
1695 return process_acf(skb, ct, ctinfo, protoff, data,
1696 &ras->admissionConfirm);
1697 case eRasMessage_locationRequest:
1698 return process_lrq(skb, ct, ctinfo, protoff, data,
1699 &ras->locationRequest);
1700 case eRasMessage_locationConfirm:
1701 return process_lcf(skb, ct, ctinfo, protoff, data,
1702 &ras->locationConfirm);
1703 case eRasMessage_infoRequestResponse:
1704 return process_irr(skb, ct, ctinfo, protoff, data,
1705 &ras->infoRequestResponse);
1707 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1714 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1715 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1717 static RasMessage ras;
1718 unsigned char *data;
1722 pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1724 spin_lock_bh(&nf_h323_lock);
1727 data = get_udp_data(skb, protoff, &datalen);
1730 pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1731 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1733 /* Decode RAS message */
1734 ret = DecodeRasMessage(data, datalen, &ras);
1736 pr_debug("nf_ct_ras: decoding error: %s\n",
1737 ret == H323_ERROR_BOUND ?
1738 "out of bound" : "out of range");
1742 /* Process RAS message */
1743 if (process_ras(skb, ct, ctinfo, protoff, &data, &ras) < 0)
1747 spin_unlock_bh(&nf_h323_lock);
1751 spin_unlock_bh(&nf_h323_lock);
1752 nf_ct_helper_log(skb, ct, "cannot process RAS message");
1756 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1761 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1765 .tuple.src.l3num = AF_INET,
1766 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1767 .tuple.dst.protonum = IPPROTO_UDP,
1769 .expect_policy = &ras_exp_policy,
1774 .tuple.src.l3num = AF_INET6,
1775 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1776 .tuple.dst.protonum = IPPROTO_UDP,
1778 .expect_policy = &ras_exp_policy,
1782 static int __init h323_helper_init(void)
1786 ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1789 ret = nf_conntrack_helpers_register(nf_conntrack_helper_q931,
1790 ARRAY_SIZE(nf_conntrack_helper_q931));
1793 ret = nf_conntrack_helpers_register(nf_conntrack_helper_ras,
1794 ARRAY_SIZE(nf_conntrack_helper_ras));
1800 nf_conntrack_helpers_unregister(nf_conntrack_helper_q931,
1801 ARRAY_SIZE(nf_conntrack_helper_q931));
1803 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1807 static void __exit h323_helper_exit(void)
1809 nf_conntrack_helpers_unregister(nf_conntrack_helper_ras,
1810 ARRAY_SIZE(nf_conntrack_helper_ras));
1811 nf_conntrack_helpers_unregister(nf_conntrack_helper_q931,
1812 ARRAY_SIZE(nf_conntrack_helper_q931));
1813 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1816 static void __exit nf_conntrack_h323_fini(void)
1820 pr_debug("nf_ct_h323: fini\n");
1823 static int __init nf_conntrack_h323_init(void)
1827 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_h323_master));
1829 h323_buffer = kmalloc(65536, GFP_KERNEL);
1832 ret = h323_helper_init();
1835 pr_debug("nf_ct_h323: init success\n");
1842 module_init(nf_conntrack_h323_init);
1843 module_exit(nf_conntrack_h323_fini);
1845 EXPORT_SYMBOL_GPL(get_h225_addr);
1846 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1847 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1848 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1849 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1850 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1851 EXPORT_SYMBOL_GPL(nat_t120_hook);
1852 EXPORT_SYMBOL_GPL(nat_h245_hook);
1853 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1854 EXPORT_SYMBOL_GPL(nat_q931_hook);
1856 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1857 MODULE_DESCRIPTION("H.323 connection tracking helper");
1858 MODULE_LICENSE("GPL");
1859 MODULE_ALIAS("ip_conntrack_h323");
1860 MODULE_ALIAS_NFCT_HELPER("RAS");
1861 MODULE_ALIAS_NFCT_HELPER("Q.931");
1862 MODULE_ALIAS_NFCT_HELPER("H.245");