2 * H.323 connection tracking helper
4 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
6 * This source code is licensed under General Public License version 2.
8 * Based on the 'brute force' H.323 connection tracking module by
9 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
11 * For more information, please see http://nath323.sourceforge.net/
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ctype.h>
17 #include <linux/inet.h>
20 #include <linux/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>
27 #include <net/netfilter/nf_conntrack.h>
28 #include <net/netfilter/nf_conntrack_core.h>
29 #include <net/netfilter/nf_conntrack_tuple.h>
30 #include <net/netfilter/nf_conntrack_expect.h>
31 #include <net/netfilter/nf_conntrack_ecache.h>
32 #include <net/netfilter/nf_conntrack_helper.h>
33 #include <net/netfilter/nf_conntrack_zones.h>
34 #include <linux/netfilter/nf_conntrack_h323.h>
37 static unsigned int default_rrq_ttl __read_mostly = 300;
38 module_param(default_rrq_ttl, uint, 0600);
39 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
41 static int gkrouted_only __read_mostly = 1;
42 module_param(gkrouted_only, int, 0600);
43 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
45 static bool callforward_filter __read_mostly = true;
46 module_param(callforward_filter, bool, 0600);
47 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
48 "if both endpoints are on different sides "
49 "(determined by routing information)");
52 int (*set_h245_addr_hook) (struct sk_buff *skb, unsigned int protoff,
53 unsigned char **data, int dataoff,
54 H245_TransportAddress *taddr,
55 union nf_inet_addr *addr, __be16 port)
57 int (*set_h225_addr_hook) (struct sk_buff *skb, unsigned int protoff,
58 unsigned char **data, int dataoff,
59 TransportAddress *taddr,
60 union nf_inet_addr *addr, __be16 port)
62 int (*set_sig_addr_hook) (struct sk_buff *skb,
64 enum ip_conntrack_info ctinfo,
65 unsigned int protoff, unsigned char **data,
66 TransportAddress *taddr, int count) __read_mostly;
67 int (*set_ras_addr_hook) (struct sk_buff *skb,
69 enum ip_conntrack_info ctinfo,
70 unsigned int protoff, unsigned char **data,
71 TransportAddress *taddr, int count) __read_mostly;
72 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
74 enum ip_conntrack_info ctinfo,
76 unsigned char **data, int dataoff,
77 H245_TransportAddress *taddr,
78 __be16 port, __be16 rtp_port,
79 struct nf_conntrack_expect *rtp_exp,
80 struct nf_conntrack_expect *rtcp_exp) __read_mostly;
81 int (*nat_t120_hook) (struct sk_buff *skb,
83 enum ip_conntrack_info ctinfo,
85 unsigned char **data, int dataoff,
86 H245_TransportAddress *taddr, __be16 port,
87 struct nf_conntrack_expect *exp) __read_mostly;
88 int (*nat_h245_hook) (struct sk_buff *skb,
90 enum ip_conntrack_info ctinfo,
92 unsigned char **data, int dataoff,
93 TransportAddress *taddr, __be16 port,
94 struct nf_conntrack_expect *exp) __read_mostly;
95 int (*nat_callforwarding_hook) (struct sk_buff *skb,
97 enum ip_conntrack_info ctinfo,
99 unsigned char **data, int dataoff,
100 TransportAddress *taddr, __be16 port,
101 struct nf_conntrack_expect *exp) __read_mostly;
102 int (*nat_q931_hook) (struct sk_buff *skb,
104 enum ip_conntrack_info ctinfo,
105 unsigned int protoff,
106 unsigned char **data, TransportAddress *taddr, int idx,
107 __be16 port, struct nf_conntrack_expect *exp)
110 static DEFINE_SPINLOCK(nf_h323_lock);
111 static char *h323_buffer;
113 static struct nf_conntrack_helper nf_conntrack_helper_h245;
114 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
115 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
117 /****************************************************************************/
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,
149 BUG_ON(tpkt == NULL);
151 /* Validate TPKT identifier */
152 if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) {
153 /* Netmeeting sends TPKT header and data separately */
154 if (info->tpkt_len[dir] > 0) {
155 pr_debug("nf_ct_h323: previous packet "
156 "indicated separate TPKT data of %hu "
157 "bytes\n", info->tpkt_len[dir]);
158 if (info->tpkt_len[dir] <= tcpdatalen) {
159 /* Yes, there was a TPKT header
162 *datalen = info->tpkt_len[dir];
167 /* Fragmented TPKT */
168 pr_debug("nf_ct_h323: fragmented TPKT\n");
172 /* It is not even a TPKT */
176 } else { /* Next TPKT */
177 tpktoff = *dataoff + *datalen;
178 tcpdatalen -= tpktoff;
179 if (tcpdatalen <= 4) /* No more TPKT */
181 tpkt = *data + *datalen;
183 /* Validate TPKT identifier */
184 if (tpkt[0] != 0x03 || tpkt[1] != 0)
188 /* Validate TPKT length */
189 tpktlen = tpkt[2] * 256 + tpkt[3];
192 if (tpktlen > tcpdatalen) {
193 if (tcpdatalen == 4) { /* Separate TPKT header */
194 /* Netmeeting sends TPKT header and data separately */
195 pr_debug("nf_ct_h323: separate TPKT header indicates "
196 "there will be TPKT data of %hu bytes\n",
198 info->tpkt_len[dir] = tpktlen - 4;
202 pr_debug("nf_ct_h323: incomplete TPKT (fragmented?)\n");
206 /* This is the encapsulated data */
208 *datalen = tpktlen - 4;
209 *dataoff = tpktoff + 4;
212 /* Clear TPKT length */
213 info->tpkt_len[dir] = 0;
217 info->tpkt_len[dir] = 0;
221 /****************************************************************************/
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 /****************************************************************************/
257 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
258 enum ip_conntrack_info ctinfo,
259 unsigned int protoff,
260 unsigned char **data, int dataoff,
261 H245_TransportAddress *taddr)
263 int dir = CTINFO2DIR(ctinfo);
266 __be16 rtp_port, rtcp_port;
267 union nf_inet_addr addr;
268 struct nf_conntrack_expect *rtp_exp;
269 struct nf_conntrack_expect *rtcp_exp;
270 typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
272 /* Read RTP or RTCP address */
273 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
274 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
278 /* RTP port is even */
279 rtp_port = port & ~htons(1);
280 rtcp_port = port | htons(1);
282 /* Create expect for RTP */
283 if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
285 nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
286 &ct->tuplehash[!dir].tuple.src.u3,
287 &ct->tuplehash[!dir].tuple.dst.u3,
288 IPPROTO_UDP, NULL, &rtp_port);
290 /* Create expect for RTCP */
291 if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
292 nf_ct_expect_put(rtp_exp);
295 nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
296 &ct->tuplehash[!dir].tuple.src.u3,
297 &ct->tuplehash[!dir].tuple.dst.u3,
298 IPPROTO_UDP, NULL, &rtcp_port);
300 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
301 &ct->tuplehash[!dir].tuple.dst.u3,
302 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
303 (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
304 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
305 ct->status & IPS_NAT_MASK) {
307 ret = nat_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
308 taddr, port, rtp_port, rtp_exp, rtcp_exp);
309 } else { /* Conntrack only */
310 if (nf_ct_expect_related(rtp_exp) == 0) {
311 if (nf_ct_expect_related(rtcp_exp) == 0) {
312 pr_debug("nf_ct_h323: expect RTP ");
313 nf_ct_dump_tuple(&rtp_exp->tuple);
314 pr_debug("nf_ct_h323: expect RTCP ");
315 nf_ct_dump_tuple(&rtcp_exp->tuple);
317 nf_ct_unexpect_related(rtp_exp);
324 nf_ct_expect_put(rtp_exp);
325 nf_ct_expect_put(rtcp_exp);
330 /****************************************************************************/
331 static int expect_t120(struct sk_buff *skb,
333 enum ip_conntrack_info ctinfo,
334 unsigned int protoff,
335 unsigned char **data, int dataoff,
336 H245_TransportAddress *taddr)
338 int dir = CTINFO2DIR(ctinfo);
341 union nf_inet_addr addr;
342 struct nf_conntrack_expect *exp;
343 typeof(nat_t120_hook) nat_t120;
345 /* Read T.120 address */
346 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
347 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
351 /* Create expect for T.120 connections */
352 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
354 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
355 &ct->tuplehash[!dir].tuple.src.u3,
356 &ct->tuplehash[!dir].tuple.dst.u3,
357 IPPROTO_TCP, NULL, &port);
358 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple channels */
360 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
361 &ct->tuplehash[!dir].tuple.dst.u3,
362 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
363 (nat_t120 = rcu_dereference(nat_t120_hook)) &&
364 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
365 ct->status & IPS_NAT_MASK) {
367 ret = nat_t120(skb, ct, ctinfo, protoff, data, dataoff, taddr,
369 } else { /* Conntrack only */
370 if (nf_ct_expect_related(exp) == 0) {
371 pr_debug("nf_ct_h323: expect T.120 ");
372 nf_ct_dump_tuple(&exp->tuple);
377 nf_ct_expect_put(exp);
382 /****************************************************************************/
383 static int process_h245_channel(struct sk_buff *skb,
385 enum ip_conntrack_info ctinfo,
386 unsigned int protoff,
387 unsigned char **data, int dataoff,
388 H2250LogicalChannelParameters *channel)
392 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
394 ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
395 &channel->mediaChannel);
401 options & eH2250LogicalChannelParameters_mediaControlChannel) {
403 ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
404 &channel->mediaControlChannel);
412 /****************************************************************************/
413 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
414 enum ip_conntrack_info ctinfo,
415 unsigned int protoff,
416 unsigned char **data, int dataoff,
417 OpenLogicalChannel *olc)
421 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
423 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
424 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
426 ret = process_h245_channel(skb, ct, ctinfo,
427 protoff, data, dataoff,
429 forwardLogicalChannelParameters.
431 h2250LogicalChannelParameters);
437 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
438 (olc->reverseLogicalChannelParameters.options &
439 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
440 && (olc->reverseLogicalChannelParameters.multiplexParameters.
442 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
445 process_h245_channel(skb, ct, ctinfo,
446 protoff, data, dataoff,
448 reverseLogicalChannelParameters.
450 h2250LogicalChannelParameters);
455 if ((olc->options & eOpenLogicalChannel_separateStack) &&
456 olc->forwardLogicalChannelParameters.dataType.choice ==
458 olc->forwardLogicalChannelParameters.dataType.data.application.
459 choice == eDataApplicationCapability_application_t120 &&
460 olc->forwardLogicalChannelParameters.dataType.data.application.
461 t120.choice == eDataProtocolCapability_separateLANStack &&
462 olc->separateStack.networkAddress.choice ==
463 eNetworkAccessParameters_networkAddress_localAreaAddress) {
464 ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
465 &olc->separateStack.networkAddress.
474 /****************************************************************************/
475 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
476 enum ip_conntrack_info ctinfo,
477 unsigned int protoff, unsigned char **data, int dataoff,
478 OpenLogicalChannelAck *olca)
480 H2250LogicalChannelAckParameters *ack;
483 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
486 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
487 (olca->reverseLogicalChannelParameters.options &
488 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
489 && (olca->reverseLogicalChannelParameters.multiplexParameters.
491 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
493 ret = process_h245_channel(skb, ct, ctinfo,
494 protoff, data, dataoff,
496 reverseLogicalChannelParameters.
498 h2250LogicalChannelParameters);
504 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
505 (olca->forwardMultiplexAckParameters.choice ==
506 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
508 ack = &olca->forwardMultiplexAckParameters.
509 h2250LogicalChannelAckParameters;
511 eH2250LogicalChannelAckParameters_mediaChannel) {
513 ret = expect_rtp_rtcp(skb, ct, ctinfo,
514 protoff, data, dataoff,
521 eH2250LogicalChannelAckParameters_mediaControlChannel) {
523 ret = expect_rtp_rtcp(skb, ct, ctinfo,
524 protoff, data, dataoff,
525 &ack->mediaControlChannel);
531 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
532 olca->separateStack.networkAddress.choice ==
533 eNetworkAccessParameters_networkAddress_localAreaAddress) {
534 ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
535 &olca->separateStack.networkAddress.
544 /****************************************************************************/
545 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
546 enum ip_conntrack_info ctinfo,
547 unsigned int protoff, unsigned char **data, int dataoff,
548 MultimediaSystemControlMessage *mscm)
550 switch (mscm->choice) {
551 case eMultimediaSystemControlMessage_request:
552 if (mscm->request.choice ==
553 eRequestMessage_openLogicalChannel) {
554 return process_olc(skb, ct, ctinfo,
555 protoff, data, dataoff,
556 &mscm->request.openLogicalChannel);
558 pr_debug("nf_ct_h323: H.245 Request %d\n",
559 mscm->request.choice);
561 case eMultimediaSystemControlMessage_response:
562 if (mscm->response.choice ==
563 eResponseMessage_openLogicalChannelAck) {
564 return process_olca(skb, ct, ctinfo,
565 protoff, data, dataoff,
567 openLogicalChannelAck);
569 pr_debug("nf_ct_h323: H.245 Response %d\n",
570 mscm->response.choice);
573 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice);
580 /****************************************************************************/
581 static int h245_help(struct sk_buff *skb, unsigned int protoff,
582 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
584 static MultimediaSystemControlMessage mscm;
585 unsigned char *data = NULL;
590 /* Until there's been traffic both ways, don't look in packets. */
591 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
594 pr_debug("nf_ct_h245: skblen = %u\n", skb->len);
596 spin_lock_bh(&nf_h323_lock);
598 /* Process each TPKT */
599 while (get_tpkt_data(skb, protoff, ct, ctinfo,
600 &data, &datalen, &dataoff)) {
601 pr_debug("nf_ct_h245: TPKT len=%d ", datalen);
602 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
604 /* Decode H.245 signal */
605 ret = DecodeMultimediaSystemControlMessage(data, datalen,
608 pr_debug("nf_ct_h245: decoding error: %s\n",
609 ret == H323_ERROR_BOUND ?
610 "out of bound" : "out of range");
611 /* We don't drop when decoding error */
615 /* Process H.245 signal */
616 if (process_h245(skb, ct, ctinfo, protoff,
617 &data, dataoff, &mscm) < 0)
621 spin_unlock_bh(&nf_h323_lock);
625 spin_unlock_bh(&nf_h323_lock);
626 net_info_ratelimited("nf_ct_h245: packet dropped\n");
630 /****************************************************************************/
631 static const struct nf_conntrack_expect_policy h245_exp_policy = {
632 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
636 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
639 .data_len = sizeof(struct nf_ct_h323_master),
640 .tuple.src.l3num = AF_UNSPEC,
641 .tuple.dst.protonum = IPPROTO_UDP,
643 .expect_policy = &h245_exp_policy,
646 /****************************************************************************/
647 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
648 TransportAddress *taddr,
649 union nf_inet_addr *addr, __be16 *port)
651 const unsigned char *p;
654 switch (taddr->choice) {
655 case eTransportAddress_ipAddress:
656 if (nf_ct_l3num(ct) != AF_INET)
658 p = data + taddr->ipAddress.ip;
661 case eTransportAddress_ip6Address:
662 if (nf_ct_l3num(ct) != AF_INET6)
664 p = data + taddr->ip6Address.ip;
671 memcpy(addr, p, len);
672 memset((void *)addr + len, 0, sizeof(*addr) - len);
673 memcpy(port, p + len, sizeof(__be16));
678 /****************************************************************************/
679 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
680 enum ip_conntrack_info ctinfo,
681 unsigned int protoff, unsigned char **data, int dataoff,
682 TransportAddress *taddr)
684 int dir = CTINFO2DIR(ctinfo);
687 union nf_inet_addr addr;
688 struct nf_conntrack_expect *exp;
689 typeof(nat_h245_hook) nat_h245;
691 /* Read h245Address */
692 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
693 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
697 /* Create expect for h245 connection */
698 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
700 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
701 &ct->tuplehash[!dir].tuple.src.u3,
702 &ct->tuplehash[!dir].tuple.dst.u3,
703 IPPROTO_TCP, NULL, &port);
704 exp->helper = &nf_conntrack_helper_h245;
706 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
707 &ct->tuplehash[!dir].tuple.dst.u3,
708 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
709 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
710 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
711 ct->status & IPS_NAT_MASK) {
713 ret = nat_h245(skb, ct, ctinfo, protoff, data, dataoff, taddr,
715 } else { /* Conntrack only */
716 if (nf_ct_expect_related(exp) == 0) {
717 pr_debug("nf_ct_q931: expect H.245 ");
718 nf_ct_dump_tuple(&exp->tuple);
723 nf_ct_expect_put(exp);
728 /* If the calling party is on the same side of the forward-to party,
729 * we don't need to track the second call */
730 static int callforward_do_filter(const union nf_inet_addr *src,
731 const union nf_inet_addr *dst,
734 const struct nf_afinfo *afinfo;
737 /* rcu_read_lock()ed by nf_hook_slow() */
738 afinfo = nf_get_afinfo(family);
744 struct flowi4 fl1, fl2;
745 struct rtable *rt1, *rt2;
747 memset(&fl1, 0, sizeof(fl1));
750 memset(&fl2, 0, sizeof(fl2));
752 if (!afinfo->route(&init_net, (struct dst_entry **)&rt1,
753 flowi4_to_flowi(&fl1), false)) {
754 if (!afinfo->route(&init_net, (struct dst_entry **)&rt2,
755 flowi4_to_flowi(&fl2), false)) {
756 if (rt1->rt_gateway == rt2->rt_gateway &&
757 rt1->dst.dev == rt2->dst.dev)
759 dst_release(&rt2->dst);
761 dst_release(&rt1->dst);
765 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV6)
767 struct flowi6 fl1, fl2;
768 struct rt6_info *rt1, *rt2;
770 memset(&fl1, 0, sizeof(fl1));
771 fl1.daddr = src->in6;
773 memset(&fl2, 0, sizeof(fl2));
774 fl2.daddr = dst->in6;
775 if (!afinfo->route(&init_net, (struct dst_entry **)&rt1,
776 flowi6_to_flowi(&fl1), false)) {
777 if (!afinfo->route(&init_net, (struct dst_entry **)&rt2,
778 flowi6_to_flowi(&fl2), false)) {
779 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
780 sizeof(rt1->rt6i_gateway)) &&
781 rt1->dst.dev == rt2->dst.dev)
783 dst_release(&rt2->dst);
785 dst_release(&rt1->dst);
795 /****************************************************************************/
796 static int expect_callforwarding(struct sk_buff *skb,
798 enum ip_conntrack_info ctinfo,
799 unsigned int protoff,
800 unsigned char **data, int dataoff,
801 TransportAddress *taddr)
803 int dir = CTINFO2DIR(ctinfo);
806 union nf_inet_addr addr;
807 struct nf_conntrack_expect *exp;
808 typeof(nat_callforwarding_hook) nat_callforwarding;
810 /* Read alternativeAddress */
811 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
814 /* If the calling party is on the same side of the forward-to party,
815 * we don't need to track the second call */
816 if (callforward_filter &&
817 callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
819 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
823 /* Create expect for the second call leg */
824 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
826 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
827 &ct->tuplehash[!dir].tuple.src.u3, &addr,
828 IPPROTO_TCP, NULL, &port);
829 exp->helper = nf_conntrack_helper_q931;
831 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
832 &ct->tuplehash[!dir].tuple.dst.u3,
833 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
834 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
835 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
836 ct->status & IPS_NAT_MASK) {
838 ret = nat_callforwarding(skb, ct, ctinfo,
839 protoff, data, dataoff,
841 } else { /* Conntrack only */
842 if (nf_ct_expect_related(exp) == 0) {
843 pr_debug("nf_ct_q931: expect Call Forwarding ");
844 nf_ct_dump_tuple(&exp->tuple);
849 nf_ct_expect_put(exp);
854 /****************************************************************************/
855 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
856 enum ip_conntrack_info ctinfo,
857 unsigned int protoff,
858 unsigned char **data, int dataoff,
861 int dir = CTINFO2DIR(ctinfo);
865 union nf_inet_addr addr;
866 typeof(set_h225_addr_hook) set_h225_addr;
868 pr_debug("nf_ct_q931: Setup\n");
870 if (setup->options & eSetup_UUIE_h245Address) {
871 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
872 &setup->h245Address);
877 set_h225_addr = rcu_dereference(set_h225_addr_hook);
878 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
879 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
880 ct->status & IPS_NAT_MASK &&
881 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
883 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
884 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
885 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3,
886 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
887 ret = set_h225_addr(skb, protoff, data, dataoff,
888 &setup->destCallSignalAddress,
889 &ct->tuplehash[!dir].tuple.src.u3,
890 ct->tuplehash[!dir].tuple.src.u.tcp.port);
895 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
896 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
897 ct->status & IPS_NAT_MASK &&
898 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
900 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
901 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
902 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3,
903 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
904 ret = set_h225_addr(skb, protoff, data, dataoff,
905 &setup->sourceCallSignalAddress,
906 &ct->tuplehash[!dir].tuple.dst.u3,
907 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
912 if (setup->options & eSetup_UUIE_fastStart) {
913 for (i = 0; i < setup->fastStart.count; i++) {
914 ret = process_olc(skb, ct, ctinfo,
915 protoff, data, dataoff,
916 &setup->fastStart.item[i]);
925 /****************************************************************************/
926 static int process_callproceeding(struct sk_buff *skb,
928 enum ip_conntrack_info ctinfo,
929 unsigned int protoff,
930 unsigned char **data, int dataoff,
931 CallProceeding_UUIE *callproc)
936 pr_debug("nf_ct_q931: CallProceeding\n");
938 if (callproc->options & eCallProceeding_UUIE_h245Address) {
939 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
940 &callproc->h245Address);
945 if (callproc->options & eCallProceeding_UUIE_fastStart) {
946 for (i = 0; i < callproc->fastStart.count; i++) {
947 ret = process_olc(skb, ct, ctinfo,
948 protoff, data, dataoff,
949 &callproc->fastStart.item[i]);
958 /****************************************************************************/
959 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
960 enum ip_conntrack_info ctinfo,
961 unsigned int protoff,
962 unsigned char **data, int dataoff,
963 Connect_UUIE *connect)
968 pr_debug("nf_ct_q931: Connect\n");
970 if (connect->options & eConnect_UUIE_h245Address) {
971 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
972 &connect->h245Address);
977 if (connect->options & eConnect_UUIE_fastStart) {
978 for (i = 0; i < connect->fastStart.count; i++) {
979 ret = process_olc(skb, ct, ctinfo,
980 protoff, data, dataoff,
981 &connect->fastStart.item[i]);
990 /****************************************************************************/
991 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
992 enum ip_conntrack_info ctinfo,
993 unsigned int protoff,
994 unsigned char **data, int dataoff,
995 Alerting_UUIE *alert)
1000 pr_debug("nf_ct_q931: Alerting\n");
1002 if (alert->options & eAlerting_UUIE_h245Address) {
1003 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1004 &alert->h245Address);
1009 if (alert->options & eAlerting_UUIE_fastStart) {
1010 for (i = 0; i < alert->fastStart.count; i++) {
1011 ret = process_olc(skb, ct, ctinfo,
1012 protoff, data, dataoff,
1013 &alert->fastStart.item[i]);
1022 /****************************************************************************/
1023 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
1024 enum ip_conntrack_info ctinfo,
1025 unsigned int protoff,
1026 unsigned char **data, int dataoff,
1027 Facility_UUIE *facility)
1032 pr_debug("nf_ct_q931: Facility\n");
1034 if (facility->reason.choice == eFacilityReason_callForwarded) {
1035 if (facility->options & eFacility_UUIE_alternativeAddress)
1036 return expect_callforwarding(skb, ct, ctinfo,
1037 protoff, data, dataoff,
1039 alternativeAddress);
1043 if (facility->options & eFacility_UUIE_h245Address) {
1044 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1045 &facility->h245Address);
1050 if (facility->options & eFacility_UUIE_fastStart) {
1051 for (i = 0; i < facility->fastStart.count; i++) {
1052 ret = process_olc(skb, ct, ctinfo,
1053 protoff, data, dataoff,
1054 &facility->fastStart.item[i]);
1063 /****************************************************************************/
1064 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1065 enum ip_conntrack_info ctinfo,
1066 unsigned int protoff,
1067 unsigned char **data, int dataoff,
1068 Progress_UUIE *progress)
1073 pr_debug("nf_ct_q931: Progress\n");
1075 if (progress->options & eProgress_UUIE_h245Address) {
1076 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1077 &progress->h245Address);
1082 if (progress->options & eProgress_UUIE_fastStart) {
1083 for (i = 0; i < progress->fastStart.count; i++) {
1084 ret = process_olc(skb, ct, ctinfo,
1085 protoff, data, dataoff,
1086 &progress->fastStart.item[i]);
1095 /****************************************************************************/
1096 static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1097 enum ip_conntrack_info ctinfo,
1098 unsigned int protoff, unsigned char **data, int dataoff,
1101 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1105 switch (pdu->h323_message_body.choice) {
1106 case eH323_UU_PDU_h323_message_body_setup:
1107 ret = process_setup(skb, ct, ctinfo, protoff, data, dataoff,
1108 &pdu->h323_message_body.setup);
1110 case eH323_UU_PDU_h323_message_body_callProceeding:
1111 ret = process_callproceeding(skb, ct, ctinfo,
1112 protoff, data, dataoff,
1113 &pdu->h323_message_body.
1116 case eH323_UU_PDU_h323_message_body_connect:
1117 ret = process_connect(skb, ct, ctinfo, protoff, data, dataoff,
1118 &pdu->h323_message_body.connect);
1120 case eH323_UU_PDU_h323_message_body_alerting:
1121 ret = process_alerting(skb, ct, ctinfo, protoff, data, dataoff,
1122 &pdu->h323_message_body.alerting);
1124 case eH323_UU_PDU_h323_message_body_facility:
1125 ret = process_facility(skb, ct, ctinfo, protoff, data, dataoff,
1126 &pdu->h323_message_body.facility);
1128 case eH323_UU_PDU_h323_message_body_progress:
1129 ret = process_progress(skb, ct, ctinfo, protoff, data, dataoff,
1130 &pdu->h323_message_body.progress);
1133 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1134 pdu->h323_message_body.choice);
1141 if (pdu->options & eH323_UU_PDU_h245Control) {
1142 for (i = 0; i < pdu->h245Control.count; i++) {
1143 ret = process_h245(skb, ct, ctinfo,
1144 protoff, data, dataoff,
1145 &pdu->h245Control.item[i]);
1154 /****************************************************************************/
1155 static int q931_help(struct sk_buff *skb, unsigned int protoff,
1156 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1159 unsigned char *data = NULL;
1164 /* Until there's been traffic both ways, don't look in packets. */
1165 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
1168 pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1170 spin_lock_bh(&nf_h323_lock);
1172 /* Process each TPKT */
1173 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1174 &data, &datalen, &dataoff)) {
1175 pr_debug("nf_ct_q931: TPKT len=%d ", datalen);
1176 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1178 /* Decode Q.931 signal */
1179 ret = DecodeQ931(data, datalen, &q931);
1181 pr_debug("nf_ct_q931: decoding error: %s\n",
1182 ret == H323_ERROR_BOUND ?
1183 "out of bound" : "out of range");
1184 /* We don't drop when decoding error */
1188 /* Process Q.931 signal */
1189 if (process_q931(skb, ct, ctinfo, protoff,
1190 &data, dataoff, &q931) < 0)
1194 spin_unlock_bh(&nf_h323_lock);
1198 spin_unlock_bh(&nf_h323_lock);
1199 net_info_ratelimited("nf_ct_q931: packet dropped\n");
1203 /****************************************************************************/
1204 static const struct nf_conntrack_expect_policy q931_exp_policy = {
1205 /* T.120 and H.245 */
1206 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1210 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1214 .data_len = sizeof(struct nf_ct_h323_master),
1215 .tuple.src.l3num = AF_INET,
1216 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1217 .tuple.dst.protonum = IPPROTO_TCP,
1219 .expect_policy = &q931_exp_policy,
1224 .tuple.src.l3num = AF_INET6,
1225 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1226 .tuple.dst.protonum = IPPROTO_TCP,
1228 .expect_policy = &q931_exp_policy,
1232 /****************************************************************************/
1233 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1236 const struct udphdr *uh;
1240 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1243 dataoff = protoff + sizeof(_uh);
1244 if (dataoff >= skb->len)
1246 *datalen = skb->len - dataoff;
1247 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1250 /****************************************************************************/
1251 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1252 union nf_inet_addr *addr,
1255 struct net *net = nf_ct_net(ct);
1256 struct nf_conntrack_expect *exp;
1257 struct nf_conntrack_tuple tuple;
1259 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1260 tuple.src.u.tcp.port = 0;
1261 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1262 tuple.dst.u.tcp.port = port;
1263 tuple.dst.protonum = IPPROTO_TCP;
1265 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1266 if (exp && exp->master == ct)
1271 /****************************************************************************/
1272 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1273 unsigned int timeout)
1275 if (!exp || !del_timer(&exp->timeout))
1278 exp->timeout.expires = jiffies + timeout * HZ;
1279 add_timer(&exp->timeout);
1284 /****************************************************************************/
1285 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1286 enum ip_conntrack_info ctinfo,
1287 unsigned int protoff, unsigned char **data,
1288 TransportAddress *taddr, int count)
1290 struct nf_ct_h323_master *info = nfct_help_data(ct);
1291 int dir = CTINFO2DIR(ctinfo);
1295 union nf_inet_addr addr;
1296 struct nf_conntrack_expect *exp;
1297 typeof(nat_q931_hook) nat_q931;
1299 /* Look for the first related address */
1300 for (i = 0; i < count; i++) {
1301 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1302 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1303 sizeof(addr)) == 0 && port != 0)
1307 if (i >= count) /* Not found */
1310 /* Create expect for Q.931 */
1311 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1313 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1314 gkrouted_only ? /* only accept calls from GK? */
1315 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1316 &ct->tuplehash[!dir].tuple.dst.u3,
1317 IPPROTO_TCP, NULL, &port);
1318 exp->helper = nf_conntrack_helper_q931;
1319 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1321 nat_q931 = rcu_dereference(nat_q931_hook);
1322 if (nat_q931 && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1323 ct->status & IPS_NAT_MASK) { /* Need NAT */
1324 ret = nat_q931(skb, ct, ctinfo, protoff, data,
1325 taddr, i, port, exp);
1326 } else { /* Conntrack only */
1327 if (nf_ct_expect_related(exp) == 0) {
1328 pr_debug("nf_ct_ras: expect Q.931 ");
1329 nf_ct_dump_tuple(&exp->tuple);
1331 /* Save port for looking up expect in processing RCF */
1332 info->sig_port[dir] = port;
1337 nf_ct_expect_put(exp);
1342 /****************************************************************************/
1343 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1344 enum ip_conntrack_info ctinfo,
1345 unsigned int protoff,
1346 unsigned char **data, GatekeeperRequest *grq)
1348 typeof(set_ras_addr_hook) set_ras_addr;
1350 pr_debug("nf_ct_ras: GRQ\n");
1352 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1353 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1354 ct->status & IPS_NAT_MASK) /* NATed */
1355 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1356 &grq->rasAddress, 1);
1360 /****************************************************************************/
1361 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1362 enum ip_conntrack_info ctinfo,
1363 unsigned int protoff,
1364 unsigned char **data, GatekeeperConfirm *gcf)
1366 int dir = CTINFO2DIR(ctinfo);
1369 union nf_inet_addr addr;
1370 struct nf_conntrack_expect *exp;
1372 pr_debug("nf_ct_ras: GCF\n");
1374 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1377 /* Registration port is the same as discovery port */
1378 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1379 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1382 /* Avoid RAS expectation loops. A GCF is never expected. */
1383 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1386 /* Need new expect */
1387 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1389 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1390 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1391 IPPROTO_UDP, NULL, &port);
1392 exp->helper = nf_conntrack_helper_ras;
1394 if (nf_ct_expect_related(exp) == 0) {
1395 pr_debug("nf_ct_ras: expect RAS ");
1396 nf_ct_dump_tuple(&exp->tuple);
1400 nf_ct_expect_put(exp);
1405 /****************************************************************************/
1406 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1407 enum ip_conntrack_info ctinfo,
1408 unsigned int protoff,
1409 unsigned char **data, RegistrationRequest *rrq)
1411 struct nf_ct_h323_master *info = nfct_help_data(ct);
1413 typeof(set_ras_addr_hook) set_ras_addr;
1415 pr_debug("nf_ct_ras: RRQ\n");
1417 ret = expect_q931(skb, ct, ctinfo, protoff, data,
1418 rrq->callSignalAddress.item,
1419 rrq->callSignalAddress.count);
1423 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1424 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1425 ct->status & IPS_NAT_MASK) {
1426 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1427 rrq->rasAddress.item,
1428 rrq->rasAddress.count);
1433 if (rrq->options & eRegistrationRequest_timeToLive) {
1434 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1435 info->timeout = rrq->timeToLive;
1437 info->timeout = default_rrq_ttl;
1442 /****************************************************************************/
1443 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1444 enum ip_conntrack_info ctinfo,
1445 unsigned int protoff,
1446 unsigned char **data, RegistrationConfirm *rcf)
1448 struct nf_ct_h323_master *info = nfct_help_data(ct);
1449 int dir = CTINFO2DIR(ctinfo);
1451 struct nf_conntrack_expect *exp;
1452 typeof(set_sig_addr_hook) set_sig_addr;
1454 pr_debug("nf_ct_ras: RCF\n");
1456 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1457 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1458 ct->status & IPS_NAT_MASK) {
1459 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1460 rcf->callSignalAddress.item,
1461 rcf->callSignalAddress.count);
1466 if (rcf->options & eRegistrationConfirm_timeToLive) {
1467 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1468 info->timeout = rcf->timeToLive;
1471 if (info->timeout > 0) {
1472 pr_debug("nf_ct_ras: set RAS connection timeout to "
1473 "%u seconds\n", info->timeout);
1474 nf_ct_refresh(ct, skb, info->timeout * HZ);
1476 /* Set expect timeout */
1477 spin_lock_bh(&nf_conntrack_lock);
1478 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1479 info->sig_port[!dir]);
1481 pr_debug("nf_ct_ras: set Q.931 expect "
1482 "timeout to %u seconds for",
1484 nf_ct_dump_tuple(&exp->tuple);
1485 set_expect_timeout(exp, info->timeout);
1487 spin_unlock_bh(&nf_conntrack_lock);
1493 /****************************************************************************/
1494 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1495 enum ip_conntrack_info ctinfo,
1496 unsigned int protoff,
1497 unsigned char **data, UnregistrationRequest *urq)
1499 struct nf_ct_h323_master *info = nfct_help_data(ct);
1500 int dir = CTINFO2DIR(ctinfo);
1502 typeof(set_sig_addr_hook) set_sig_addr;
1504 pr_debug("nf_ct_ras: URQ\n");
1506 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1507 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1508 ct->status & IPS_NAT_MASK) {
1509 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1510 urq->callSignalAddress.item,
1511 urq->callSignalAddress.count);
1516 /* Clear old expect */
1517 nf_ct_remove_expectations(ct);
1518 info->sig_port[dir] = 0;
1519 info->sig_port[!dir] = 0;
1521 /* Give it 30 seconds for UCF or URJ */
1522 nf_ct_refresh(ct, skb, 30 * HZ);
1527 /****************************************************************************/
1528 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1529 enum ip_conntrack_info ctinfo,
1530 unsigned int protoff,
1531 unsigned char **data, AdmissionRequest *arq)
1533 const struct nf_ct_h323_master *info = nfct_help_data(ct);
1534 int dir = CTINFO2DIR(ctinfo);
1536 union nf_inet_addr addr;
1537 typeof(set_h225_addr_hook) set_h225_addr;
1539 pr_debug("nf_ct_ras: ARQ\n");
1541 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1542 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1543 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1545 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1546 port == info->sig_port[dir] &&
1547 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1548 set_h225_addr && ct->status & IPS_NAT_MASK) {
1550 return set_h225_addr(skb, protoff, data, 0,
1551 &arq->destCallSignalAddress,
1552 &ct->tuplehash[!dir].tuple.dst.u3,
1553 info->sig_port[!dir]);
1556 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1557 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1559 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1560 set_h225_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1561 ct->status & IPS_NAT_MASK) {
1563 return set_h225_addr(skb, protoff, data, 0,
1564 &arq->srcCallSignalAddress,
1565 &ct->tuplehash[!dir].tuple.dst.u3,
1572 /****************************************************************************/
1573 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1574 enum ip_conntrack_info ctinfo,
1575 unsigned int protoff,
1576 unsigned char **data, AdmissionConfirm *acf)
1578 int dir = CTINFO2DIR(ctinfo);
1581 union nf_inet_addr addr;
1582 struct nf_conntrack_expect *exp;
1583 typeof(set_sig_addr_hook) set_sig_addr;
1585 pr_debug("nf_ct_ras: ACF\n");
1587 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1591 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1593 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1594 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1595 ct->status & IPS_NAT_MASK)
1596 return set_sig_addr(skb, ct, ctinfo, protoff, data,
1597 &acf->destCallSignalAddress, 1);
1601 /* Need new expect */
1602 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1604 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1605 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1606 IPPROTO_TCP, NULL, &port);
1607 exp->flags = NF_CT_EXPECT_PERMANENT;
1608 exp->helper = nf_conntrack_helper_q931;
1610 if (nf_ct_expect_related(exp) == 0) {
1611 pr_debug("nf_ct_ras: expect Q.931 ");
1612 nf_ct_dump_tuple(&exp->tuple);
1616 nf_ct_expect_put(exp);
1621 /****************************************************************************/
1622 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1623 enum ip_conntrack_info ctinfo,
1624 unsigned int protoff,
1625 unsigned char **data, LocationRequest *lrq)
1627 typeof(set_ras_addr_hook) set_ras_addr;
1629 pr_debug("nf_ct_ras: LRQ\n");
1631 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1632 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1633 ct->status & IPS_NAT_MASK)
1634 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1635 &lrq->replyAddress, 1);
1639 /****************************************************************************/
1640 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1641 enum ip_conntrack_info ctinfo,
1642 unsigned int protoff,
1643 unsigned char **data, LocationConfirm *lcf)
1645 int dir = CTINFO2DIR(ctinfo);
1648 union nf_inet_addr addr;
1649 struct nf_conntrack_expect *exp;
1651 pr_debug("nf_ct_ras: LCF\n");
1653 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1657 /* Need new expect for call signal */
1658 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1660 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1661 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1662 IPPROTO_TCP, NULL, &port);
1663 exp->flags = NF_CT_EXPECT_PERMANENT;
1664 exp->helper = nf_conntrack_helper_q931;
1666 if (nf_ct_expect_related(exp) == 0) {
1667 pr_debug("nf_ct_ras: expect Q.931 ");
1668 nf_ct_dump_tuple(&exp->tuple);
1672 nf_ct_expect_put(exp);
1674 /* Ignore rasAddress */
1679 /****************************************************************************/
1680 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1681 enum ip_conntrack_info ctinfo,
1682 unsigned int protoff,
1683 unsigned char **data, InfoRequestResponse *irr)
1686 typeof(set_ras_addr_hook) set_ras_addr;
1687 typeof(set_sig_addr_hook) set_sig_addr;
1689 pr_debug("nf_ct_ras: IRR\n");
1691 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1692 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1693 ct->status & IPS_NAT_MASK) {
1694 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1695 &irr->rasAddress, 1);
1700 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1701 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1702 ct->status & IPS_NAT_MASK) {
1703 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1704 irr->callSignalAddress.item,
1705 irr->callSignalAddress.count);
1713 /****************************************************************************/
1714 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1715 enum ip_conntrack_info ctinfo,
1716 unsigned int protoff,
1717 unsigned char **data, RasMessage *ras)
1719 switch (ras->choice) {
1720 case eRasMessage_gatekeeperRequest:
1721 return process_grq(skb, ct, ctinfo, protoff, data,
1722 &ras->gatekeeperRequest);
1723 case eRasMessage_gatekeeperConfirm:
1724 return process_gcf(skb, ct, ctinfo, protoff, data,
1725 &ras->gatekeeperConfirm);
1726 case eRasMessage_registrationRequest:
1727 return process_rrq(skb, ct, ctinfo, protoff, data,
1728 &ras->registrationRequest);
1729 case eRasMessage_registrationConfirm:
1730 return process_rcf(skb, ct, ctinfo, protoff, data,
1731 &ras->registrationConfirm);
1732 case eRasMessage_unregistrationRequest:
1733 return process_urq(skb, ct, ctinfo, protoff, data,
1734 &ras->unregistrationRequest);
1735 case eRasMessage_admissionRequest:
1736 return process_arq(skb, ct, ctinfo, protoff, data,
1737 &ras->admissionRequest);
1738 case eRasMessage_admissionConfirm:
1739 return process_acf(skb, ct, ctinfo, protoff, data,
1740 &ras->admissionConfirm);
1741 case eRasMessage_locationRequest:
1742 return process_lrq(skb, ct, ctinfo, protoff, data,
1743 &ras->locationRequest);
1744 case eRasMessage_locationConfirm:
1745 return process_lcf(skb, ct, ctinfo, protoff, data,
1746 &ras->locationConfirm);
1747 case eRasMessage_infoRequestResponse:
1748 return process_irr(skb, ct, ctinfo, protoff, data,
1749 &ras->infoRequestResponse);
1751 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1758 /****************************************************************************/
1759 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1760 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1762 static RasMessage ras;
1763 unsigned char *data;
1767 pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1769 spin_lock_bh(&nf_h323_lock);
1772 data = get_udp_data(skb, protoff, &datalen);
1775 pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1776 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1778 /* Decode RAS message */
1779 ret = DecodeRasMessage(data, datalen, &ras);
1781 pr_debug("nf_ct_ras: decoding error: %s\n",
1782 ret == H323_ERROR_BOUND ?
1783 "out of bound" : "out of range");
1787 /* Process RAS message */
1788 if (process_ras(skb, ct, ctinfo, protoff, &data, &ras) < 0)
1792 spin_unlock_bh(&nf_h323_lock);
1796 spin_unlock_bh(&nf_h323_lock);
1797 net_info_ratelimited("nf_ct_ras: packet dropped\n");
1801 /****************************************************************************/
1802 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1807 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1811 .data_len = sizeof(struct nf_ct_h323_master),
1812 .tuple.src.l3num = AF_INET,
1813 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1814 .tuple.dst.protonum = IPPROTO_UDP,
1816 .expect_policy = &ras_exp_policy,
1821 .data_len = sizeof(struct nf_ct_h323_master),
1822 .tuple.src.l3num = AF_INET6,
1823 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1824 .tuple.dst.protonum = IPPROTO_UDP,
1826 .expect_policy = &ras_exp_policy,
1830 /****************************************************************************/
1831 static void __exit nf_conntrack_h323_fini(void)
1833 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1834 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1835 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1836 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1837 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1839 pr_debug("nf_ct_h323: fini\n");
1842 /****************************************************************************/
1843 static int __init nf_conntrack_h323_init(void)
1847 h323_buffer = kmalloc(65536, GFP_KERNEL);
1850 ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1853 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1856 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1859 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1862 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1865 pr_debug("nf_ct_h323: init success\n");
1869 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1871 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1873 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1875 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1881 /****************************************************************************/
1882 module_init(nf_conntrack_h323_init);
1883 module_exit(nf_conntrack_h323_fini);
1885 EXPORT_SYMBOL_GPL(get_h225_addr);
1886 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1887 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1888 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1889 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1890 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1891 EXPORT_SYMBOL_GPL(nat_t120_hook);
1892 EXPORT_SYMBOL_GPL(nat_h245_hook);
1893 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1894 EXPORT_SYMBOL_GPL(nat_q931_hook);
1896 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1897 MODULE_DESCRIPTION("H.323 connection tracking helper");
1898 MODULE_LICENSE("GPL");
1899 MODULE_ALIAS("ip_conntrack_h323");
1900 MODULE_ALIAS_NFCT_HELPER("RAS");
1901 MODULE_ALIAS_NFCT_HELPER("Q.931");
1902 MODULE_ALIAS_NFCT_HELPER("H.245");