1 // SPDX-License-Identifier: GPL-2.0-only
3 * H.323 connection tracking helper
5 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
6 * Copyright (c) 2006-2012 Patrick McHardy <kaber@trash.net>
8 * Based on the 'brute force' H.323 connection tracking module by
9 * Jozsef Kadlecsik <kadlec@netfilter.org>
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/slab.h>
21 #include <linux/udp.h>
22 #include <linux/tcp.h>
23 #include <linux/skbuff.h>
24 #include <net/route.h>
25 #include <net/ip6_route.h>
26 #include <linux/netfilter_ipv6.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_tuple.h>
31 #include <net/netfilter/nf_conntrack_expect.h>
32 #include <net/netfilter/nf_conntrack_ecache.h>
33 #include <net/netfilter/nf_conntrack_helper.h>
34 #include <net/netfilter/nf_conntrack_zones.h>
35 #include <linux/netfilter/nf_conntrack_h323.h>
38 static unsigned int default_rrq_ttl __read_mostly = 300;
39 module_param(default_rrq_ttl, uint, 0600);
40 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
42 static int gkrouted_only __read_mostly = 1;
43 module_param(gkrouted_only, int, 0600);
44 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
46 static bool callforward_filter __read_mostly = true;
47 module_param(callforward_filter, bool, 0600);
48 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
49 "if both endpoints are on different sides "
50 "(determined by routing information)");
53 int (*set_h245_addr_hook) (struct sk_buff *skb, unsigned int protoff,
54 unsigned char **data, int dataoff,
55 H245_TransportAddress *taddr,
56 union nf_inet_addr *addr, __be16 port)
58 int (*set_h225_addr_hook) (struct sk_buff *skb, unsigned int protoff,
59 unsigned char **data, int dataoff,
60 TransportAddress *taddr,
61 union nf_inet_addr *addr, __be16 port)
63 int (*set_sig_addr_hook) (struct sk_buff *skb,
65 enum ip_conntrack_info ctinfo,
66 unsigned int protoff, unsigned char **data,
67 TransportAddress *taddr, int count) __read_mostly;
68 int (*set_ras_addr_hook) (struct sk_buff *skb,
70 enum ip_conntrack_info ctinfo,
71 unsigned int protoff, unsigned char **data,
72 TransportAddress *taddr, int count) __read_mostly;
73 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
75 enum ip_conntrack_info ctinfo,
77 unsigned char **data, int dataoff,
78 H245_TransportAddress *taddr,
79 __be16 port, __be16 rtp_port,
80 struct nf_conntrack_expect *rtp_exp,
81 struct nf_conntrack_expect *rtcp_exp) __read_mostly;
82 int (*nat_t120_hook) (struct sk_buff *skb,
84 enum ip_conntrack_info ctinfo,
86 unsigned char **data, int dataoff,
87 H245_TransportAddress *taddr, __be16 port,
88 struct nf_conntrack_expect *exp) __read_mostly;
89 int (*nat_h245_hook) (struct sk_buff *skb,
91 enum ip_conntrack_info ctinfo,
93 unsigned char **data, int dataoff,
94 TransportAddress *taddr, __be16 port,
95 struct nf_conntrack_expect *exp) __read_mostly;
96 int (*nat_callforwarding_hook) (struct sk_buff *skb,
98 enum ip_conntrack_info ctinfo,
100 unsigned char **data, int dataoff,
101 TransportAddress *taddr, __be16 port,
102 struct nf_conntrack_expect *exp) __read_mostly;
103 int (*nat_q931_hook) (struct sk_buff *skb,
105 enum ip_conntrack_info ctinfo,
106 unsigned int protoff,
107 unsigned char **data, TransportAddress *taddr, int idx,
108 __be16 port, struct nf_conntrack_expect *exp)
111 static DEFINE_SPINLOCK(nf_h323_lock);
112 static char *h323_buffer;
114 static struct nf_conntrack_helper nf_conntrack_helper_h245;
115 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
116 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
118 static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,
119 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
120 unsigned char **data, int *datalen, int *dataoff)
122 struct nf_ct_h323_master *info = nfct_help_data(ct);
123 int dir = CTINFO2DIR(ctinfo);
124 const struct tcphdr *th;
133 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
137 /* Get TCP data offset */
138 tcpdataoff = protoff + th->doff * 4;
140 /* Get TCP data length */
141 tcpdatalen = skb->len - tcpdataoff;
142 if (tcpdatalen <= 0) /* No TCP data */
145 if (*data == NULL) { /* first TPKT */
146 /* Get first TPKT pointer */
147 tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
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) == 0) {
310 if (nf_ct_expect_related(rtcp_exp, 0) == 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) == 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) == 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_IPV6)
753 struct rt6_info *rt1, *rt2;
754 struct flowi6 fl1, fl2;
756 memset(&fl1, 0, sizeof(fl1));
757 fl1.daddr = src->in6;
759 memset(&fl2, 0, sizeof(fl2));
760 fl2.daddr = dst->in6;
761 if (!nf_ip6_route(net, (struct dst_entry **)&rt1,
762 flowi6_to_flowi(&fl1), false)) {
763 if (!nf_ip6_route(net, (struct dst_entry **)&rt2,
764 flowi6_to_flowi(&fl2), false)) {
765 if (ipv6_addr_equal(rt6_nexthop(rt1, &fl1.daddr),
766 rt6_nexthop(rt2, &fl2.daddr)) &&
767 rt1->dst.dev == rt2->dst.dev)
769 dst_release(&rt2->dst);
771 dst_release(&rt1->dst);
781 static int expect_callforwarding(struct sk_buff *skb,
783 enum ip_conntrack_info ctinfo,
784 unsigned int protoff,
785 unsigned char **data, int dataoff,
786 TransportAddress *taddr)
788 int dir = CTINFO2DIR(ctinfo);
791 union nf_inet_addr addr;
792 struct nf_conntrack_expect *exp;
793 struct net *net = nf_ct_net(ct);
794 typeof(nat_callforwarding_hook) nat_callforwarding;
796 /* Read alternativeAddress */
797 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
800 /* If the calling party is on the same side of the forward-to party,
801 * we don't need to track the second call
803 if (callforward_filter &&
804 callforward_do_filter(net, &addr, &ct->tuplehash[!dir].tuple.src.u3,
806 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
810 /* Create expect for the second call leg */
811 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
813 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
814 &ct->tuplehash[!dir].tuple.src.u3, &addr,
815 IPPROTO_TCP, NULL, &port);
816 exp->helper = nf_conntrack_helper_q931;
818 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
819 &ct->tuplehash[!dir].tuple.dst.u3,
820 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
821 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
822 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
823 ct->status & IPS_NAT_MASK) {
825 ret = nat_callforwarding(skb, ct, ctinfo,
826 protoff, data, dataoff,
828 } else { /* Conntrack only */
829 if (nf_ct_expect_related(exp, 0) == 0) {
830 pr_debug("nf_ct_q931: expect Call Forwarding ");
831 nf_ct_dump_tuple(&exp->tuple);
836 nf_ct_expect_put(exp);
841 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
842 enum ip_conntrack_info ctinfo,
843 unsigned int protoff,
844 unsigned char **data, int dataoff,
847 int dir = CTINFO2DIR(ctinfo);
851 union nf_inet_addr addr;
852 typeof(set_h225_addr_hook) set_h225_addr;
854 pr_debug("nf_ct_q931: Setup\n");
856 if (setup->options & eSetup_UUIE_h245Address) {
857 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
858 &setup->h245Address);
863 set_h225_addr = rcu_dereference(set_h225_addr_hook);
864 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
865 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
866 ct->status & IPS_NAT_MASK &&
867 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
869 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
870 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
871 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3,
872 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
873 ret = set_h225_addr(skb, protoff, data, dataoff,
874 &setup->destCallSignalAddress,
875 &ct->tuplehash[!dir].tuple.src.u3,
876 ct->tuplehash[!dir].tuple.src.u.tcp.port);
881 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
882 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
883 ct->status & IPS_NAT_MASK &&
884 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
886 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
887 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
888 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3,
889 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
890 ret = set_h225_addr(skb, protoff, data, dataoff,
891 &setup->sourceCallSignalAddress,
892 &ct->tuplehash[!dir].tuple.dst.u3,
893 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
898 if (setup->options & eSetup_UUIE_fastStart) {
899 for (i = 0; i < setup->fastStart.count; i++) {
900 ret = process_olc(skb, ct, ctinfo,
901 protoff, data, dataoff,
902 &setup->fastStart.item[i]);
911 static int process_callproceeding(struct sk_buff *skb,
913 enum ip_conntrack_info ctinfo,
914 unsigned int protoff,
915 unsigned char **data, int dataoff,
916 CallProceeding_UUIE *callproc)
921 pr_debug("nf_ct_q931: CallProceeding\n");
923 if (callproc->options & eCallProceeding_UUIE_h245Address) {
924 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
925 &callproc->h245Address);
930 if (callproc->options & eCallProceeding_UUIE_fastStart) {
931 for (i = 0; i < callproc->fastStart.count; i++) {
932 ret = process_olc(skb, ct, ctinfo,
933 protoff, data, dataoff,
934 &callproc->fastStart.item[i]);
943 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
944 enum ip_conntrack_info ctinfo,
945 unsigned int protoff,
946 unsigned char **data, int dataoff,
947 Connect_UUIE *connect)
952 pr_debug("nf_ct_q931: Connect\n");
954 if (connect->options & eConnect_UUIE_h245Address) {
955 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
956 &connect->h245Address);
961 if (connect->options & eConnect_UUIE_fastStart) {
962 for (i = 0; i < connect->fastStart.count; i++) {
963 ret = process_olc(skb, ct, ctinfo,
964 protoff, data, dataoff,
965 &connect->fastStart.item[i]);
974 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
975 enum ip_conntrack_info ctinfo,
976 unsigned int protoff,
977 unsigned char **data, int dataoff,
978 Alerting_UUIE *alert)
983 pr_debug("nf_ct_q931: Alerting\n");
985 if (alert->options & eAlerting_UUIE_h245Address) {
986 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
987 &alert->h245Address);
992 if (alert->options & eAlerting_UUIE_fastStart) {
993 for (i = 0; i < alert->fastStart.count; i++) {
994 ret = process_olc(skb, ct, ctinfo,
995 protoff, data, dataoff,
996 &alert->fastStart.item[i]);
1005 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
1006 enum ip_conntrack_info ctinfo,
1007 unsigned int protoff,
1008 unsigned char **data, int dataoff,
1009 Facility_UUIE *facility)
1014 pr_debug("nf_ct_q931: Facility\n");
1016 if (facility->reason.choice == eFacilityReason_callForwarded) {
1017 if (facility->options & eFacility_UUIE_alternativeAddress)
1018 return expect_callforwarding(skb, ct, ctinfo,
1019 protoff, data, dataoff,
1021 alternativeAddress);
1025 if (facility->options & eFacility_UUIE_h245Address) {
1026 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1027 &facility->h245Address);
1032 if (facility->options & eFacility_UUIE_fastStart) {
1033 for (i = 0; i < facility->fastStart.count; i++) {
1034 ret = process_olc(skb, ct, ctinfo,
1035 protoff, data, dataoff,
1036 &facility->fastStart.item[i]);
1045 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1046 enum ip_conntrack_info ctinfo,
1047 unsigned int protoff,
1048 unsigned char **data, int dataoff,
1049 Progress_UUIE *progress)
1054 pr_debug("nf_ct_q931: Progress\n");
1056 if (progress->options & eProgress_UUIE_h245Address) {
1057 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1058 &progress->h245Address);
1063 if (progress->options & eProgress_UUIE_fastStart) {
1064 for (i = 0; i < progress->fastStart.count; i++) {
1065 ret = process_olc(skb, ct, ctinfo,
1066 protoff, data, dataoff,
1067 &progress->fastStart.item[i]);
1076 static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1077 enum ip_conntrack_info ctinfo,
1078 unsigned int protoff, unsigned char **data, int dataoff,
1081 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1085 switch (pdu->h323_message_body.choice) {
1086 case eH323_UU_PDU_h323_message_body_setup:
1087 ret = process_setup(skb, ct, ctinfo, protoff, data, dataoff,
1088 &pdu->h323_message_body.setup);
1090 case eH323_UU_PDU_h323_message_body_callProceeding:
1091 ret = process_callproceeding(skb, ct, ctinfo,
1092 protoff, data, dataoff,
1093 &pdu->h323_message_body.
1096 case eH323_UU_PDU_h323_message_body_connect:
1097 ret = process_connect(skb, ct, ctinfo, protoff, data, dataoff,
1098 &pdu->h323_message_body.connect);
1100 case eH323_UU_PDU_h323_message_body_alerting:
1101 ret = process_alerting(skb, ct, ctinfo, protoff, data, dataoff,
1102 &pdu->h323_message_body.alerting);
1104 case eH323_UU_PDU_h323_message_body_facility:
1105 ret = process_facility(skb, ct, ctinfo, protoff, data, dataoff,
1106 &pdu->h323_message_body.facility);
1108 case eH323_UU_PDU_h323_message_body_progress:
1109 ret = process_progress(skb, ct, ctinfo, protoff, data, dataoff,
1110 &pdu->h323_message_body.progress);
1113 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1114 pdu->h323_message_body.choice);
1121 if (pdu->options & eH323_UU_PDU_h245Control) {
1122 for (i = 0; i < pdu->h245Control.count; i++) {
1123 ret = process_h245(skb, ct, ctinfo,
1124 protoff, data, dataoff,
1125 &pdu->h245Control.item[i]);
1134 static int q931_help(struct sk_buff *skb, unsigned int protoff,
1135 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1138 unsigned char *data = NULL;
1143 /* Until there's been traffic both ways, don't look in packets. */
1144 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
1147 pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1149 spin_lock_bh(&nf_h323_lock);
1151 /* Process each TPKT */
1152 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1153 &data, &datalen, &dataoff)) {
1154 pr_debug("nf_ct_q931: TPKT len=%d ", datalen);
1155 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1157 /* Decode Q.931 signal */
1158 ret = DecodeQ931(data, datalen, &q931);
1160 pr_debug("nf_ct_q931: decoding error: %s\n",
1161 ret == H323_ERROR_BOUND ?
1162 "out of bound" : "out of range");
1163 /* We don't drop when decoding error */
1167 /* Process Q.931 signal */
1168 if (process_q931(skb, ct, ctinfo, protoff,
1169 &data, dataoff, &q931) < 0)
1173 spin_unlock_bh(&nf_h323_lock);
1177 spin_unlock_bh(&nf_h323_lock);
1178 nf_ct_helper_log(skb, ct, "cannot process Q.931 message");
1182 static const struct nf_conntrack_expect_policy q931_exp_policy = {
1183 /* T.120 and H.245 */
1184 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1188 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1192 .tuple.src.l3num = AF_INET,
1193 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1194 .tuple.dst.protonum = IPPROTO_TCP,
1196 .expect_policy = &q931_exp_policy,
1201 .tuple.src.l3num = AF_INET6,
1202 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1203 .tuple.dst.protonum = IPPROTO_TCP,
1205 .expect_policy = &q931_exp_policy,
1209 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1212 const struct udphdr *uh;
1216 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1219 dataoff = protoff + sizeof(_uh);
1220 if (dataoff >= skb->len)
1222 *datalen = skb->len - dataoff;
1223 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1226 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1227 union nf_inet_addr *addr,
1230 struct net *net = nf_ct_net(ct);
1231 struct nf_conntrack_expect *exp;
1232 struct nf_conntrack_tuple tuple;
1234 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1235 tuple.src.u.tcp.port = 0;
1236 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1237 tuple.dst.u.tcp.port = port;
1238 tuple.dst.protonum = IPPROTO_TCP;
1240 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1241 if (exp && exp->master == ct)
1246 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1247 enum ip_conntrack_info ctinfo,
1248 unsigned int protoff, unsigned char **data,
1249 TransportAddress *taddr, int count)
1251 struct nf_ct_h323_master *info = nfct_help_data(ct);
1252 int dir = CTINFO2DIR(ctinfo);
1256 union nf_inet_addr addr;
1257 struct nf_conntrack_expect *exp;
1258 typeof(nat_q931_hook) nat_q931;
1260 /* Look for the first related address */
1261 for (i = 0; i < count; i++) {
1262 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1263 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1264 sizeof(addr)) == 0 && port != 0)
1268 if (i >= count) /* Not found */
1271 /* Create expect for Q.931 */
1272 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1274 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1275 gkrouted_only ? /* only accept calls from GK? */
1276 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1277 &ct->tuplehash[!dir].tuple.dst.u3,
1278 IPPROTO_TCP, NULL, &port);
1279 exp->helper = nf_conntrack_helper_q931;
1280 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1282 nat_q931 = rcu_dereference(nat_q931_hook);
1283 if (nat_q931 && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1284 ct->status & IPS_NAT_MASK) { /* Need NAT */
1285 ret = nat_q931(skb, ct, ctinfo, protoff, data,
1286 taddr, i, port, exp);
1287 } else { /* Conntrack only */
1288 if (nf_ct_expect_related(exp, 0) == 0) {
1289 pr_debug("nf_ct_ras: expect Q.931 ");
1290 nf_ct_dump_tuple(&exp->tuple);
1292 /* Save port for looking up expect in processing RCF */
1293 info->sig_port[dir] = port;
1298 nf_ct_expect_put(exp);
1303 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1304 enum ip_conntrack_info ctinfo,
1305 unsigned int protoff,
1306 unsigned char **data, GatekeeperRequest *grq)
1308 typeof(set_ras_addr_hook) set_ras_addr;
1310 pr_debug("nf_ct_ras: GRQ\n");
1312 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1313 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1314 ct->status & IPS_NAT_MASK) /* NATed */
1315 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1316 &grq->rasAddress, 1);
1320 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1321 enum ip_conntrack_info ctinfo,
1322 unsigned int protoff,
1323 unsigned char **data, GatekeeperConfirm *gcf)
1325 int dir = CTINFO2DIR(ctinfo);
1328 union nf_inet_addr addr;
1329 struct nf_conntrack_expect *exp;
1331 pr_debug("nf_ct_ras: GCF\n");
1333 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1336 /* Registration port is the same as discovery port */
1337 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1338 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1341 /* Avoid RAS expectation loops. A GCF is never expected. */
1342 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1345 /* Need new expect */
1346 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1348 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1349 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1350 IPPROTO_UDP, NULL, &port);
1351 exp->helper = nf_conntrack_helper_ras;
1353 if (nf_ct_expect_related(exp, 0) == 0) {
1354 pr_debug("nf_ct_ras: expect RAS ");
1355 nf_ct_dump_tuple(&exp->tuple);
1359 nf_ct_expect_put(exp);
1364 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1365 enum ip_conntrack_info ctinfo,
1366 unsigned int protoff,
1367 unsigned char **data, RegistrationRequest *rrq)
1369 struct nf_ct_h323_master *info = nfct_help_data(ct);
1371 typeof(set_ras_addr_hook) set_ras_addr;
1373 pr_debug("nf_ct_ras: RRQ\n");
1375 ret = expect_q931(skb, ct, ctinfo, protoff, data,
1376 rrq->callSignalAddress.item,
1377 rrq->callSignalAddress.count);
1381 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1382 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1383 ct->status & IPS_NAT_MASK) {
1384 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1385 rrq->rasAddress.item,
1386 rrq->rasAddress.count);
1391 if (rrq->options & eRegistrationRequest_timeToLive) {
1392 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1393 info->timeout = rrq->timeToLive;
1395 info->timeout = default_rrq_ttl;
1400 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1401 enum ip_conntrack_info ctinfo,
1402 unsigned int protoff,
1403 unsigned char **data, RegistrationConfirm *rcf)
1405 struct nf_ct_h323_master *info = nfct_help_data(ct);
1406 int dir = CTINFO2DIR(ctinfo);
1408 struct nf_conntrack_expect *exp;
1409 typeof(set_sig_addr_hook) set_sig_addr;
1411 pr_debug("nf_ct_ras: RCF\n");
1413 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1414 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1415 ct->status & IPS_NAT_MASK) {
1416 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1417 rcf->callSignalAddress.item,
1418 rcf->callSignalAddress.count);
1423 if (rcf->options & eRegistrationConfirm_timeToLive) {
1424 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1425 info->timeout = rcf->timeToLive;
1428 if (info->timeout > 0) {
1429 pr_debug("nf_ct_ras: set RAS connection timeout to "
1430 "%u seconds\n", info->timeout);
1431 nf_ct_refresh(ct, skb, info->timeout * HZ);
1433 /* Set expect timeout */
1434 spin_lock_bh(&nf_conntrack_expect_lock);
1435 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1436 info->sig_port[!dir]);
1438 pr_debug("nf_ct_ras: set Q.931 expect "
1439 "timeout to %u seconds for",
1441 nf_ct_dump_tuple(&exp->tuple);
1442 mod_timer_pending(&exp->timeout,
1443 jiffies + info->timeout * HZ);
1445 spin_unlock_bh(&nf_conntrack_expect_lock);
1451 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1452 enum ip_conntrack_info ctinfo,
1453 unsigned int protoff,
1454 unsigned char **data, UnregistrationRequest *urq)
1456 struct nf_ct_h323_master *info = nfct_help_data(ct);
1457 int dir = CTINFO2DIR(ctinfo);
1459 typeof(set_sig_addr_hook) set_sig_addr;
1461 pr_debug("nf_ct_ras: URQ\n");
1463 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1464 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1465 ct->status & IPS_NAT_MASK) {
1466 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1467 urq->callSignalAddress.item,
1468 urq->callSignalAddress.count);
1473 /* Clear old expect */
1474 nf_ct_remove_expectations(ct);
1475 info->sig_port[dir] = 0;
1476 info->sig_port[!dir] = 0;
1478 /* Give it 30 seconds for UCF or URJ */
1479 nf_ct_refresh(ct, skb, 30 * HZ);
1484 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1485 enum ip_conntrack_info ctinfo,
1486 unsigned int protoff,
1487 unsigned char **data, AdmissionRequest *arq)
1489 const struct nf_ct_h323_master *info = nfct_help_data(ct);
1490 int dir = CTINFO2DIR(ctinfo);
1492 union nf_inet_addr addr;
1493 typeof(set_h225_addr_hook) set_h225_addr;
1495 pr_debug("nf_ct_ras: ARQ\n");
1497 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1498 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1499 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1501 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1502 port == info->sig_port[dir] &&
1503 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1504 set_h225_addr && ct->status & IPS_NAT_MASK) {
1506 return set_h225_addr(skb, protoff, data, 0,
1507 &arq->destCallSignalAddress,
1508 &ct->tuplehash[!dir].tuple.dst.u3,
1509 info->sig_port[!dir]);
1512 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1513 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1515 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1516 set_h225_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1517 ct->status & IPS_NAT_MASK) {
1519 return set_h225_addr(skb, protoff, data, 0,
1520 &arq->srcCallSignalAddress,
1521 &ct->tuplehash[!dir].tuple.dst.u3,
1528 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1529 enum ip_conntrack_info ctinfo,
1530 unsigned int protoff,
1531 unsigned char **data, AdmissionConfirm *acf)
1533 int dir = CTINFO2DIR(ctinfo);
1536 union nf_inet_addr addr;
1537 struct nf_conntrack_expect *exp;
1538 typeof(set_sig_addr_hook) set_sig_addr;
1540 pr_debug("nf_ct_ras: ACF\n");
1542 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1546 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1548 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1549 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1550 ct->status & IPS_NAT_MASK)
1551 return set_sig_addr(skb, ct, ctinfo, protoff, data,
1552 &acf->destCallSignalAddress, 1);
1556 /* Need new expect */
1557 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1559 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1560 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1561 IPPROTO_TCP, NULL, &port);
1562 exp->flags = NF_CT_EXPECT_PERMANENT;
1563 exp->helper = nf_conntrack_helper_q931;
1565 if (nf_ct_expect_related(exp, 0) == 0) {
1566 pr_debug("nf_ct_ras: expect Q.931 ");
1567 nf_ct_dump_tuple(&exp->tuple);
1571 nf_ct_expect_put(exp);
1576 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1577 enum ip_conntrack_info ctinfo,
1578 unsigned int protoff,
1579 unsigned char **data, LocationRequest *lrq)
1581 typeof(set_ras_addr_hook) set_ras_addr;
1583 pr_debug("nf_ct_ras: LRQ\n");
1585 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1586 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1587 ct->status & IPS_NAT_MASK)
1588 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1589 &lrq->replyAddress, 1);
1593 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1594 enum ip_conntrack_info ctinfo,
1595 unsigned int protoff,
1596 unsigned char **data, LocationConfirm *lcf)
1598 int dir = CTINFO2DIR(ctinfo);
1601 union nf_inet_addr addr;
1602 struct nf_conntrack_expect *exp;
1604 pr_debug("nf_ct_ras: LCF\n");
1606 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1610 /* Need new expect for call signal */
1611 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1613 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1614 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1615 IPPROTO_TCP, NULL, &port);
1616 exp->flags = NF_CT_EXPECT_PERMANENT;
1617 exp->helper = nf_conntrack_helper_q931;
1619 if (nf_ct_expect_related(exp, 0) == 0) {
1620 pr_debug("nf_ct_ras: expect Q.931 ");
1621 nf_ct_dump_tuple(&exp->tuple);
1625 nf_ct_expect_put(exp);
1627 /* Ignore rasAddress */
1632 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1633 enum ip_conntrack_info ctinfo,
1634 unsigned int protoff,
1635 unsigned char **data, InfoRequestResponse *irr)
1638 typeof(set_ras_addr_hook) set_ras_addr;
1639 typeof(set_sig_addr_hook) set_sig_addr;
1641 pr_debug("nf_ct_ras: IRR\n");
1643 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1644 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1645 ct->status & IPS_NAT_MASK) {
1646 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1647 &irr->rasAddress, 1);
1652 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1653 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1654 ct->status & IPS_NAT_MASK) {
1655 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1656 irr->callSignalAddress.item,
1657 irr->callSignalAddress.count);
1665 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1666 enum ip_conntrack_info ctinfo,
1667 unsigned int protoff,
1668 unsigned char **data, RasMessage *ras)
1670 switch (ras->choice) {
1671 case eRasMessage_gatekeeperRequest:
1672 return process_grq(skb, ct, ctinfo, protoff, data,
1673 &ras->gatekeeperRequest);
1674 case eRasMessage_gatekeeperConfirm:
1675 return process_gcf(skb, ct, ctinfo, protoff, data,
1676 &ras->gatekeeperConfirm);
1677 case eRasMessage_registrationRequest:
1678 return process_rrq(skb, ct, ctinfo, protoff, data,
1679 &ras->registrationRequest);
1680 case eRasMessage_registrationConfirm:
1681 return process_rcf(skb, ct, ctinfo, protoff, data,
1682 &ras->registrationConfirm);
1683 case eRasMessage_unregistrationRequest:
1684 return process_urq(skb, ct, ctinfo, protoff, data,
1685 &ras->unregistrationRequest);
1686 case eRasMessage_admissionRequest:
1687 return process_arq(skb, ct, ctinfo, protoff, data,
1688 &ras->admissionRequest);
1689 case eRasMessage_admissionConfirm:
1690 return process_acf(skb, ct, ctinfo, protoff, data,
1691 &ras->admissionConfirm);
1692 case eRasMessage_locationRequest:
1693 return process_lrq(skb, ct, ctinfo, protoff, data,
1694 &ras->locationRequest);
1695 case eRasMessage_locationConfirm:
1696 return process_lcf(skb, ct, ctinfo, protoff, data,
1697 &ras->locationConfirm);
1698 case eRasMessage_infoRequestResponse:
1699 return process_irr(skb, ct, ctinfo, protoff, data,
1700 &ras->infoRequestResponse);
1702 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1709 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1710 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1712 static RasMessage ras;
1713 unsigned char *data;
1717 pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1719 spin_lock_bh(&nf_h323_lock);
1722 data = get_udp_data(skb, protoff, &datalen);
1725 pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1726 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1728 /* Decode RAS message */
1729 ret = DecodeRasMessage(data, datalen, &ras);
1731 pr_debug("nf_ct_ras: decoding error: %s\n",
1732 ret == H323_ERROR_BOUND ?
1733 "out of bound" : "out of range");
1737 /* Process RAS message */
1738 if (process_ras(skb, ct, ctinfo, protoff, &data, &ras) < 0)
1742 spin_unlock_bh(&nf_h323_lock);
1746 spin_unlock_bh(&nf_h323_lock);
1747 nf_ct_helper_log(skb, ct, "cannot process RAS message");
1751 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1756 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1760 .tuple.src.l3num = AF_INET,
1761 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1762 .tuple.dst.protonum = IPPROTO_UDP,
1764 .expect_policy = &ras_exp_policy,
1769 .tuple.src.l3num = AF_INET6,
1770 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1771 .tuple.dst.protonum = IPPROTO_UDP,
1773 .expect_policy = &ras_exp_policy,
1777 static int __init h323_helper_init(void)
1781 ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1784 ret = nf_conntrack_helpers_register(nf_conntrack_helper_q931,
1785 ARRAY_SIZE(nf_conntrack_helper_q931));
1788 ret = nf_conntrack_helpers_register(nf_conntrack_helper_ras,
1789 ARRAY_SIZE(nf_conntrack_helper_ras));
1795 nf_conntrack_helpers_unregister(nf_conntrack_helper_q931,
1796 ARRAY_SIZE(nf_conntrack_helper_q931));
1798 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1802 static void __exit h323_helper_exit(void)
1804 nf_conntrack_helpers_unregister(nf_conntrack_helper_ras,
1805 ARRAY_SIZE(nf_conntrack_helper_ras));
1806 nf_conntrack_helpers_unregister(nf_conntrack_helper_q931,
1807 ARRAY_SIZE(nf_conntrack_helper_q931));
1808 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1811 static void __exit nf_conntrack_h323_fini(void)
1815 pr_debug("nf_ct_h323: fini\n");
1818 static int __init nf_conntrack_h323_init(void)
1822 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_h323_master));
1824 h323_buffer = kmalloc(65536, GFP_KERNEL);
1827 ret = h323_helper_init();
1830 pr_debug("nf_ct_h323: init success\n");
1837 module_init(nf_conntrack_h323_init);
1838 module_exit(nf_conntrack_h323_fini);
1840 EXPORT_SYMBOL_GPL(get_h225_addr);
1841 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1842 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1843 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1844 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1845 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1846 EXPORT_SYMBOL_GPL(nat_t120_hook);
1847 EXPORT_SYMBOL_GPL(nat_h245_hook);
1848 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1849 EXPORT_SYMBOL_GPL(nat_q931_hook);
1851 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1852 MODULE_DESCRIPTION("H.323 connection tracking helper");
1853 MODULE_LICENSE("GPL");
1854 MODULE_ALIAS("ip_conntrack_h323");
1855 MODULE_ALIAS_NFCT_HELPER("RAS");
1856 MODULE_ALIAS_NFCT_HELPER("Q.931");
1857 MODULE_ALIAS_NFCT_HELPER("H.245");