[NETFILTER]: nf_conntrack_h323: turn some printks into DEBUGPs
[tomato.git] / release / src-rt / linux / linux-2.6 / net / netfilter / nf_conntrack_h323_main.c
blob6efa4101733f24e5d21ebab3ffe5c5a3ec7ddc89
1 /*
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>
18 #include <linux/in.h>
19 #include <linux/ip.h>
20 #include <linux/udp.h>
21 #include <linux/tcp.h>
22 #include <linux/skbuff.h>
23 #include <net/route.h>
24 #include <net/ip6_route.h>
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_tuple.h>
29 #include <net/netfilter/nf_conntrack_expect.h>
30 #include <net/netfilter/nf_conntrack_ecache.h>
31 #include <net/netfilter/nf_conntrack_helper.h>
32 #include <linux/netfilter/nf_conntrack_h323.h>
34 #if 0
35 #define DEBUGP printk
36 #else
37 #define DEBUGP(format, args...)
38 #endif
40 /* Parameters */
41 static unsigned int default_rrq_ttl __read_mostly = 300;
42 module_param(default_rrq_ttl, uint, 0600);
43 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
45 static int gkrouted_only __read_mostly = 1;
46 module_param(gkrouted_only, int, 0600);
47 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
49 static int callforward_filter __read_mostly = 1;
50 module_param(callforward_filter, bool, 0600);
51 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
52 "if both endpoints are on different sides "
53 "(determined by routing information)");
55 /* Hooks for NAT */
56 int (*set_h245_addr_hook) (struct sk_buff *skb,
57 unsigned char **data, int dataoff,
58 H245_TransportAddress *taddr,
59 union nf_inet_addr *addr, __be16 port)
60 __read_mostly;
61 int (*set_h225_addr_hook) (struct sk_buff *skb,
62 unsigned char **data, int dataoff,
63 TransportAddress *taddr,
64 union nf_inet_addr *addr, __be16 port)
65 __read_mostly;
66 int (*set_sig_addr_hook) (struct sk_buff *skb,
67 struct nf_conn *ct,
68 enum ip_conntrack_info ctinfo,
69 unsigned char **data,
70 TransportAddress *taddr, int count) __read_mostly;
71 int (*set_ras_addr_hook) (struct sk_buff *skb,
72 struct nf_conn *ct,
73 enum ip_conntrack_info ctinfo,
74 unsigned char **data,
75 TransportAddress *taddr, int count) __read_mostly;
76 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
77 struct nf_conn *ct,
78 enum ip_conntrack_info ctinfo,
79 unsigned char **data, int dataoff,
80 H245_TransportAddress *taddr,
81 __be16 port, __be16 rtp_port,
82 struct nf_conntrack_expect *rtp_exp,
83 struct nf_conntrack_expect *rtcp_exp) __read_mostly;
84 int (*nat_t120_hook) (struct sk_buff *skb,
85 struct nf_conn *ct,
86 enum ip_conntrack_info ctinfo,
87 unsigned char **data, int dataoff,
88 H245_TransportAddress *taddr, __be16 port,
89 struct nf_conntrack_expect *exp) __read_mostly;
90 int (*nat_h245_hook) (struct sk_buff *skb,
91 struct nf_conn *ct,
92 enum ip_conntrack_info ctinfo,
93 unsigned char **data, int dataoff,
94 TransportAddress *taddr, __be16 port,
95 struct nf_conntrack_expect *exp) __read_mostly;
96 int (*nat_callforwarding_hook) (struct sk_buff *skb,
97 struct nf_conn *ct,
98 enum ip_conntrack_info ctinfo,
99 unsigned char **data, int dataoff,
100 TransportAddress *taddr, __be16 port,
101 struct nf_conntrack_expect *exp) __read_mostly;
102 int (*nat_q931_hook) (struct sk_buff *skb,
103 struct nf_conn *ct,
104 enum ip_conntrack_info ctinfo,
105 unsigned char **data, TransportAddress *taddr, int idx,
106 __be16 port, struct nf_conntrack_expect *exp)
107 __read_mostly;
109 static DEFINE_SPINLOCK(nf_h323_lock);
110 static char *h323_buffer;
112 static struct nf_conntrack_helper nf_conntrack_helper_h245;
113 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
114 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
116 /****************************************************************************/
117 static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,
118 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
119 unsigned char **data, int *datalen, int *dataoff)
121 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
122 int dir = CTINFO2DIR(ctinfo);
123 struct tcphdr _tcph, *th;
124 int tcpdatalen;
125 int tcpdataoff;
126 unsigned char *tpkt;
127 int tpktlen;
128 int tpktoff;
130 /* Get TCP header */
131 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
132 if (th == NULL)
133 return 0;
135 /* Get TCP data offset */
136 tcpdataoff = protoff + th->doff * 4;
138 /* Get TCP data length */
139 tcpdatalen = skb->len - tcpdataoff;
140 if (tcpdatalen <= 0) /* No TCP data */
141 goto clear_out;
143 if (*data == NULL) { /* first TPKT */
144 /* Get first TPKT pointer */
145 tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
146 h323_buffer);
147 BUG_ON(tpkt == NULL);
149 /* Validate TPKT identifier */
150 if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) {
151 /* Netmeeting sends TPKT header and data separately */
152 if (info->tpkt_len[dir] > 0) {
153 DEBUGP("nf_ct_h323: previous packet "
154 "indicated separate TPKT data of %hu "
155 "bytes\n", info->tpkt_len[dir]);
156 if (info->tpkt_len[dir] <= tcpdatalen) {
157 /* Yes, there was a TPKT header
158 * received */
159 *data = tpkt;
160 *datalen = info->tpkt_len[dir];
161 *dataoff = 0;
162 goto out;
165 /* Fragmented TPKT */
166 DEBUGP("nf_ct_h323: fragmented TPKT\n");
167 goto clear_out;
170 /* It is not even a TPKT */
171 return 0;
173 tpktoff = 0;
174 } else { /* Next TPKT */
175 tpktoff = *dataoff + *datalen;
176 tcpdatalen -= tpktoff;
177 if (tcpdatalen <= 4) /* No more TPKT */
178 goto clear_out;
179 tpkt = *data + *datalen;
181 /* Validate TPKT identifier */
182 if (tpkt[0] != 0x03 || tpkt[1] != 0)
183 goto clear_out;
186 /* Validate TPKT length */
187 tpktlen = tpkt[2] * 256 + tpkt[3];
188 if (tpktlen < 4)
189 goto clear_out;
190 if (tpktlen > tcpdatalen) {
191 if (tcpdatalen == 4) { /* Separate TPKT header */
192 /* Netmeeting sends TPKT header and data separately */
193 DEBUGP("nf_ct_h323: separate TPKT header indicates "
194 "there will be TPKT data of %hu bytes\n",
195 tpktlen - 4);
196 info->tpkt_len[dir] = tpktlen - 4;
197 return 0;
200 if (net_ratelimit())
201 printk("nf_ct_h323: incomplete TPKT (fragmented?)\n");
202 goto clear_out;
205 /* This is the encapsulated data */
206 *data = tpkt + 4;
207 *datalen = tpktlen - 4;
208 *dataoff = tpktoff + 4;
210 out:
211 /* Clear TPKT length */
212 info->tpkt_len[dir] = 0;
213 return 1;
215 clear_out:
216 info->tpkt_len[dir] = 0;
217 return 0;
220 /****************************************************************************/
221 static int get_h245_addr(struct nf_conn *ct, unsigned char *data,
222 H245_TransportAddress *taddr,
223 union nf_inet_addr *addr, __be16 *port)
225 unsigned char *p;
226 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
227 int len;
229 if (taddr->choice != eH245_TransportAddress_unicastAddress)
230 return 0;
232 switch (taddr->unicastAddress.choice) {
233 case eUnicastAddress_iPAddress:
234 if (family != AF_INET)
235 return 0;
236 p = data + taddr->unicastAddress.iPAddress.network;
237 len = 4;
238 break;
239 case eUnicastAddress_iP6Address:
240 if (family != AF_INET6)
241 return 0;
242 p = data + taddr->unicastAddress.iP6Address.network;
243 len = 16;
244 break;
245 default:
246 return 0;
249 memcpy(addr, p, len);
250 memset((void *)addr + len, 0, sizeof(*addr) - len);
251 memcpy(port, p + len, sizeof(__be16));
253 return 1;
256 /****************************************************************************/
257 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
258 enum ip_conntrack_info ctinfo,
259 unsigned char **data, int dataoff,
260 H245_TransportAddress *taddr)
262 int dir = CTINFO2DIR(ctinfo);
263 int ret = 0;
264 __be16 port;
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)) ||
274 port == 0)
275 return 0;
277 /* RTP port is even */
278 port &= htons(~1);
279 rtp_port = port;
280 rtcp_port = htons(ntohs(port) + 1);
282 /* Create expect for RTP */
283 if ((rtp_exp = nf_conntrack_expect_alloc(ct)) == NULL)
284 return -1;
285 nf_conntrack_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
286 ct->tuplehash[!dir].tuple.src.l3num,
287 &ct->tuplehash[!dir].tuple.src.u3,
288 &ct->tuplehash[!dir].tuple.dst.u3,
289 IPPROTO_UDP, NULL, &rtp_port);
291 /* Create expect for RTCP */
292 if ((rtcp_exp = nf_conntrack_expect_alloc(ct)) == NULL) {
293 nf_conntrack_expect_put(rtp_exp);
294 return -1;
296 nf_conntrack_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
297 ct->tuplehash[!dir].tuple.src.l3num,
298 &ct->tuplehash[!dir].tuple.src.u3,
299 &ct->tuplehash[!dir].tuple.dst.u3,
300 IPPROTO_UDP, NULL, &rtcp_port);
302 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
303 &ct->tuplehash[!dir].tuple.dst.u3,
304 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
305 (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
306 ct->status & IPS_NAT_MASK) {
307 /* NAT needed */
308 ret = nat_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
309 taddr, port, rtp_port, rtp_exp, rtcp_exp);
310 } else { /* Conntrack only */
311 if (nf_conntrack_expect_related(rtp_exp) == 0) {
312 if (nf_conntrack_expect_related(rtcp_exp) == 0) {
313 DEBUGP("nf_ct_h323: expect RTP ");
314 NF_CT_DUMP_TUPLE(&rtp_exp->tuple);
315 DEBUGP("nf_ct_h323: expect RTCP ");
316 NF_CT_DUMP_TUPLE(&rtcp_exp->tuple);
317 } else {
318 nf_conntrack_unexpect_related(rtp_exp);
319 ret = -1;
321 } else
322 ret = -1;
325 nf_conntrack_expect_put(rtp_exp);
326 nf_conntrack_expect_put(rtcp_exp);
328 return ret;
331 /****************************************************************************/
332 static int expect_t120(struct sk_buff *skb,
333 struct nf_conn *ct,
334 enum ip_conntrack_info ctinfo,
335 unsigned char **data, int dataoff,
336 H245_TransportAddress *taddr)
338 int dir = CTINFO2DIR(ctinfo);
339 int ret = 0;
340 __be16 port;
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)) ||
348 port == 0)
349 return 0;
351 /* Create expect for T.120 connections */
352 if ((exp = nf_conntrack_expect_alloc(ct)) == NULL)
353 return -1;
354 nf_conntrack_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
355 ct->tuplehash[!dir].tuple.src.l3num,
356 &ct->tuplehash[!dir].tuple.src.u3,
357 &ct->tuplehash[!dir].tuple.dst.u3,
358 IPPROTO_TCP, NULL, &port);
359 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple channels */
361 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
362 &ct->tuplehash[!dir].tuple.dst.u3,
363 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
364 (nat_t120 = rcu_dereference(nat_t120_hook)) &&
365 ct->status & IPS_NAT_MASK) {
366 /* NAT needed */
367 ret = nat_t120(skb, ct, ctinfo, data, dataoff, taddr,
368 port, exp);
369 } else { /* Conntrack only */
370 if (nf_conntrack_expect_related(exp) == 0) {
371 DEBUGP("nf_ct_h323: expect T.120 ");
372 NF_CT_DUMP_TUPLE(&exp->tuple);
373 } else
374 ret = -1;
377 nf_conntrack_expect_put(exp);
379 return ret;
382 /****************************************************************************/
383 static int process_h245_channel(struct sk_buff *skb,
384 struct nf_conn *ct,
385 enum ip_conntrack_info ctinfo,
386 unsigned char **data, int dataoff,
387 H2250LogicalChannelParameters *channel)
389 int ret;
391 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
392 /* RTP */
393 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
394 &channel->mediaChannel);
395 if (ret < 0)
396 return -1;
399 if (channel->
400 options & eH2250LogicalChannelParameters_mediaControlChannel) {
401 /* RTCP */
402 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
403 &channel->mediaControlChannel);
404 if (ret < 0)
405 return -1;
408 return 0;
411 /****************************************************************************/
412 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
413 enum ip_conntrack_info ctinfo,
414 unsigned char **data, int dataoff,
415 OpenLogicalChannel *olc)
417 int ret;
419 DEBUGP("nf_ct_h323: OpenLogicalChannel\n");
421 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
422 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
424 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
425 &olc->
426 forwardLogicalChannelParameters.
427 multiplexParameters.
428 h2250LogicalChannelParameters);
429 if (ret < 0)
430 return -1;
433 if ((olc->options &
434 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
435 (olc->reverseLogicalChannelParameters.options &
436 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
437 && (olc->reverseLogicalChannelParameters.multiplexParameters.
438 choice ==
439 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
441 ret =
442 process_h245_channel(skb, ct, ctinfo, data, dataoff,
443 &olc->
444 reverseLogicalChannelParameters.
445 multiplexParameters.
446 h2250LogicalChannelParameters);
447 if (ret < 0)
448 return -1;
451 if ((olc->options & eOpenLogicalChannel_separateStack) &&
452 olc->forwardLogicalChannelParameters.dataType.choice ==
453 eDataType_data &&
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, data, dataoff,
461 &olc->separateStack.networkAddress.
462 localAreaAddress);
463 if (ret < 0)
464 return -1;
467 return 0;
470 /****************************************************************************/
471 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
472 enum ip_conntrack_info ctinfo,
473 unsigned char **data, int dataoff,
474 OpenLogicalChannelAck *olca)
476 H2250LogicalChannelAckParameters *ack;
477 int ret;
479 DEBUGP("nf_ct_h323: OpenLogicalChannelAck\n");
481 if ((olca->options &
482 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
483 (olca->reverseLogicalChannelParameters.options &
484 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
485 && (olca->reverseLogicalChannelParameters.multiplexParameters.
486 choice ==
487 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
489 ret = process_h245_channel(skb, ct, ctinfo, data, dataoff,
490 &olca->
491 reverseLogicalChannelParameters.
492 multiplexParameters.
493 h2250LogicalChannelParameters);
494 if (ret < 0)
495 return -1;
498 if ((olca->options &
499 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
500 (olca->forwardMultiplexAckParameters.choice ==
501 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
503 ack = &olca->forwardMultiplexAckParameters.
504 h2250LogicalChannelAckParameters;
505 if (ack->options &
506 eH2250LogicalChannelAckParameters_mediaChannel) {
507 /* RTP */
508 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
509 &ack->mediaChannel);
510 if (ret < 0)
511 return -1;
514 if (ack->options &
515 eH2250LogicalChannelAckParameters_mediaControlChannel) {
516 /* RTCP */
517 ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff,
518 &ack->mediaControlChannel);
519 if (ret < 0)
520 return -1;
524 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
525 olca->separateStack.networkAddress.choice ==
526 eNetworkAccessParameters_networkAddress_localAreaAddress) {
527 ret = expect_t120(skb, ct, ctinfo, data, dataoff,
528 &olca->separateStack.networkAddress.
529 localAreaAddress);
530 if (ret < 0)
531 return -1;
534 return 0;
537 /****************************************************************************/
538 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
539 enum ip_conntrack_info ctinfo,
540 unsigned char **data, int dataoff,
541 MultimediaSystemControlMessage *mscm)
543 switch (mscm->choice) {
544 case eMultimediaSystemControlMessage_request:
545 if (mscm->request.choice ==
546 eRequestMessage_openLogicalChannel) {
547 return process_olc(skb, ct, ctinfo, data, dataoff,
548 &mscm->request.openLogicalChannel);
550 DEBUGP("nf_ct_h323: H.245 Request %d\n",
551 mscm->request.choice);
552 break;
553 case eMultimediaSystemControlMessage_response:
554 if (mscm->response.choice ==
555 eResponseMessage_openLogicalChannelAck) {
556 return process_olca(skb, ct, ctinfo, data, dataoff,
557 &mscm->response.
558 openLogicalChannelAck);
560 DEBUGP("nf_ct_h323: H.245 Response %d\n",
561 mscm->response.choice);
562 break;
563 default:
564 DEBUGP("nf_ct_h323: H.245 signal %d\n", mscm->choice);
565 break;
568 return 0;
571 /****************************************************************************/
572 static int h245_help(struct sk_buff *skb, unsigned int protoff,
573 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
575 static MultimediaSystemControlMessage mscm;
576 unsigned char *data = NULL;
577 int datalen;
578 int dataoff;
579 int ret;
581 /* Until there's been traffic both ways, don't look in packets. */
582 if (ctinfo != IP_CT_ESTABLISHED &&
583 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
584 return NF_ACCEPT;
586 DEBUGP("nf_ct_h245: skblen = %u\n", skb->len);
588 spin_lock_bh(&nf_h323_lock);
590 /* Process each TPKT */
591 while (get_tpkt_data(skb, protoff, ct, ctinfo,
592 &data, &datalen, &dataoff)) {
593 DEBUGP("nf_ct_h245: TPKT len=%d ", datalen);
594 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
596 /* Decode H.245 signal */
597 ret = DecodeMultimediaSystemControlMessage(data, datalen,
598 &mscm);
599 if (ret < 0) {
600 DEBUGP("nf_ct_h245: decoding error: %s\n",
601 ret == H323_ERROR_BOUND ?
602 "out of bound" : "out of range");
603 /* We don't drop when decoding error */
604 break;
607 /* Process H.245 signal */
608 if (process_h245(skb, ct, ctinfo, &data, dataoff, &mscm) < 0)
609 goto drop;
612 spin_unlock_bh(&nf_h323_lock);
613 return NF_ACCEPT;
615 drop:
616 spin_unlock_bh(&nf_h323_lock);
617 if (net_ratelimit())
618 printk("nf_ct_h245: packet dropped\n");
619 return NF_DROP;
622 /****************************************************************************/
623 static const struct nf_conntrack_expect_policy h245_exp_policy = {
624 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
625 .timeout = 240,
628 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
629 .name = "H.245",
630 .me = THIS_MODULE,
631 .tuple.src.l3num = AF_UNSPEC,
632 .tuple.dst.protonum = IPPROTO_UDP,
633 .mask.src.u.udp.port = __constant_htons(0xFFFF),
634 .mask.dst.protonum = 0xFF,
635 .help = h245_help,
636 .expect_policy = &h245_exp_policy,
639 /****************************************************************************/
640 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
641 TransportAddress *taddr,
642 union nf_inet_addr *addr, __be16 *port)
644 unsigned char *p;
645 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
646 int len;
648 switch (taddr->choice) {
649 case eTransportAddress_ipAddress:
650 if (family != AF_INET)
651 return 0;
652 p = data + taddr->ipAddress.ip;
653 len = 4;
654 break;
655 case eTransportAddress_ip6Address:
656 if (family != AF_INET6)
657 return 0;
658 p = data + taddr->ip6Address.ip;
659 len = 16;
660 break;
661 default:
662 return 0;
665 memcpy(addr, p, len);
666 memset((void *)addr + len, 0, sizeof(*addr) - len);
667 memcpy(port, p + len, sizeof(__be16));
669 return 1;
672 /****************************************************************************/
673 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
674 enum ip_conntrack_info ctinfo,
675 unsigned char **data, int dataoff,
676 TransportAddress *taddr)
678 int dir = CTINFO2DIR(ctinfo);
679 int ret = 0;
680 __be16 port;
681 union nf_inet_addr addr;
682 struct nf_conntrack_expect *exp;
683 typeof(nat_h245_hook) nat_h245;
685 /* Read h245Address */
686 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
687 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
688 port == 0)
689 return 0;
691 /* Create expect for h245 connection */
692 if ((exp = nf_conntrack_expect_alloc(ct)) == NULL)
693 return -1;
694 nf_conntrack_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
695 ct->tuplehash[!dir].tuple.src.l3num,
696 &ct->tuplehash[!dir].tuple.src.u3,
697 &ct->tuplehash[!dir].tuple.dst.u3,
698 IPPROTO_TCP, NULL, &port);
699 exp->helper = &nf_conntrack_helper_h245;
701 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
702 &ct->tuplehash[!dir].tuple.dst.u3,
703 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
704 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
705 ct->status & IPS_NAT_MASK) {
706 /* NAT needed */
707 ret = nat_h245(skb, ct, ctinfo, data, dataoff, taddr,
708 port, exp);
709 } else { /* Conntrack only */
710 if (nf_conntrack_expect_related(exp) == 0) {
711 DEBUGP("nf_ct_q931: expect H.245 ");
712 NF_CT_DUMP_TUPLE(&exp->tuple);
713 } else
714 ret = -1;
717 nf_conntrack_expect_put(exp);
719 return ret;
722 /* If the calling party is on the same side of the forward-to party,
723 * we don't need to track the second call */
724 static int callforward_do_filter(union nf_inet_addr *src,
725 union nf_inet_addr *dst,
726 int family)
728 struct nf_afinfo *afinfo;
729 struct flowi fl1, fl2;
730 int ret = 0;
732 /* rcu_read_lock()ed by nf_hook_slow() */
733 afinfo = nf_get_afinfo(family);
734 if (!afinfo)
735 return 0;
737 memset(&fl1, 0, sizeof(fl1));
738 memset(&fl2, 0, sizeof(fl2));
740 switch (family) {
741 case AF_INET: {
742 struct rtable *rt1, *rt2;
744 fl1.fl4_dst = src->ip;
745 fl2.fl4_dst = dst->ip;
746 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
747 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
748 if (rt1->rt_gateway == rt2->rt_gateway &&
749 rt1->u.dst.dev == rt2->u.dst.dev)
750 ret = 1;
751 dst_release(&rt2->u.dst);
753 dst_release(&rt1->u.dst);
755 break;
757 #if defined(CONFIG_NF_CONNTRACK_IPV6) || \
758 defined(CONFIG_NF_CONNTRACK_IPV6_MODULE)
759 case AF_INET6: {
760 struct rt6_info *rt1, *rt2;
762 memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst));
763 memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst));
764 if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) {
765 if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) {
766 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
767 sizeof(rt1->rt6i_gateway)) &&
768 rt1->u.dst.dev == rt2->u.dst.dev)
769 ret = 1;
770 dst_release(&rt2->u.dst);
772 dst_release(&rt1->u.dst);
774 break;
776 #endif
778 return ret;
782 /****************************************************************************/
783 static int expect_callforwarding(struct sk_buff *skb,
784 struct nf_conn *ct,
785 enum ip_conntrack_info ctinfo,
786 unsigned char **data, int dataoff,
787 TransportAddress *taddr)
789 int dir = CTINFO2DIR(ctinfo);
790 int ret = 0;
791 __be16 port;
792 union nf_inet_addr addr;
793 struct nf_conntrack_expect *exp;
794 typeof(nat_callforwarding_hook) nat_callforwarding;
796 /* Read alternativeAddress */
797 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
798 return 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 */
802 if (callforward_filter &&
803 callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
804 ct->tuplehash[!dir].tuple.src.l3num)) {
805 DEBUGP("nf_ct_q931: Call Forwarding not tracked\n");
806 return 0;
809 /* Create expect for the second call leg */
810 if ((exp = nf_conntrack_expect_alloc(ct)) == NULL)
811 return -1;
812 nf_conntrack_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
813 ct->tuplehash[!dir].tuple.src.l3num,
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 ct->status & IPS_NAT_MASK) {
823 /* Need NAT */
824 ret = nat_callforwarding(skb, ct, ctinfo, data, dataoff,
825 taddr, port, exp);
826 } else { /* Conntrack only */
827 if (nf_conntrack_expect_related(exp) == 0) {
828 DEBUGP("nf_ct_q931: expect Call Forwarding ");
829 NF_CT_DUMP_TUPLE(&exp->tuple);
830 } else
831 ret = -1;
834 nf_conntrack_expect_put(exp);
836 return ret;
839 /****************************************************************************/
840 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
841 enum ip_conntrack_info ctinfo,
842 unsigned char **data, int dataoff,
843 Setup_UUIE *setup)
845 int dir = CTINFO2DIR(ctinfo);
846 int ret;
847 int i;
848 __be16 port;
849 union nf_inet_addr addr;
850 typeof(set_h225_addr_hook) set_h225_addr;
852 DEBUGP("nf_ct_q931: Setup\n");
854 if (setup->options & eSetup_UUIE_h245Address) {
855 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
856 &setup->h245Address);
857 if (ret < 0)
858 return -1;
861 set_h225_addr = rcu_dereference(set_h225_addr_hook);
862 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
863 (set_h225_addr) && ct->status && IPS_NAT_MASK &&
864 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
865 &addr, &port) &&
866 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
867 DEBUGP("nf_ct_q931: set destCallSignalAddress "
868 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
869 NIP6(*(struct in6_addr *)&addr), ntohs(port),
870 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.src.u3),
871 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
872 ret = set_h225_addr(skb, data, dataoff,
873 &setup->destCallSignalAddress,
874 &ct->tuplehash[!dir].tuple.src.u3,
875 ct->tuplehash[!dir].tuple.src.u.tcp.port);
876 if (ret < 0)
877 return -1;
880 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
881 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
882 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
883 &addr, &port) &&
884 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
885 DEBUGP("nf_ct_q931: set sourceCallSignalAddress "
886 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
887 NIP6(*(struct in6_addr *)&addr), ntohs(port),
888 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.dst.u3),
889 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
890 ret = set_h225_addr(skb, data, dataoff,
891 &setup->sourceCallSignalAddress,
892 &ct->tuplehash[!dir].tuple.dst.u3,
893 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
894 if (ret < 0)
895 return -1;
898 if (setup->options & eSetup_UUIE_fastStart) {
899 for (i = 0; i < setup->fastStart.count; i++) {
900 ret = process_olc(skb, ct, ctinfo, data, dataoff,
901 &setup->fastStart.item[i]);
902 if (ret < 0)
903 return -1;
907 return 0;
910 /****************************************************************************/
911 static int process_callproceeding(struct sk_buff *skb,
912 struct nf_conn *ct,
913 enum ip_conntrack_info ctinfo,
914 unsigned char **data, int dataoff,
915 CallProceeding_UUIE *callproc)
917 int ret;
918 int i;
920 DEBUGP("nf_ct_q931: CallProceeding\n");
922 if (callproc->options & eCallProceeding_UUIE_h245Address) {
923 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
924 &callproc->h245Address);
925 if (ret < 0)
926 return -1;
929 if (callproc->options & eCallProceeding_UUIE_fastStart) {
930 for (i = 0; i < callproc->fastStart.count; i++) {
931 ret = process_olc(skb, ct, ctinfo, data, dataoff,
932 &callproc->fastStart.item[i]);
933 if (ret < 0)
934 return -1;
938 return 0;
941 /****************************************************************************/
942 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
943 enum ip_conntrack_info ctinfo,
944 unsigned char **data, int dataoff,
945 Connect_UUIE *connect)
947 int ret;
948 int i;
950 DEBUGP("nf_ct_q931: Connect\n");
952 if (connect->options & eConnect_UUIE_h245Address) {
953 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
954 &connect->h245Address);
955 if (ret < 0)
956 return -1;
959 if (connect->options & eConnect_UUIE_fastStart) {
960 for (i = 0; i < connect->fastStart.count; i++) {
961 ret = process_olc(skb, ct, ctinfo, data, dataoff,
962 &connect->fastStart.item[i]);
963 if (ret < 0)
964 return -1;
968 return 0;
971 /****************************************************************************/
972 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
973 enum ip_conntrack_info ctinfo,
974 unsigned char **data, int dataoff,
975 Alerting_UUIE *alert)
977 int ret;
978 int i;
980 DEBUGP("nf_ct_q931: Alerting\n");
982 if (alert->options & eAlerting_UUIE_h245Address) {
983 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
984 &alert->h245Address);
985 if (ret < 0)
986 return -1;
989 if (alert->options & eAlerting_UUIE_fastStart) {
990 for (i = 0; i < alert->fastStart.count; i++) {
991 ret = process_olc(skb, ct, ctinfo, data, dataoff,
992 &alert->fastStart.item[i]);
993 if (ret < 0)
994 return -1;
998 return 0;
1001 /****************************************************************************/
1002 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
1003 enum ip_conntrack_info ctinfo,
1004 unsigned char **data, int dataoff,
1005 Facility_UUIE *facility)
1007 int ret;
1008 int i;
1010 DEBUGP("nf_ct_q931: Facility\n");
1012 if (facility->reason.choice == eFacilityReason_callForwarded) {
1013 if (facility->options & eFacility_UUIE_alternativeAddress)
1014 return expect_callforwarding(skb, ct, ctinfo, data,
1015 dataoff,
1016 &facility->
1017 alternativeAddress);
1018 return 0;
1021 if (facility->options & eFacility_UUIE_h245Address) {
1022 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
1023 &facility->h245Address);
1024 if (ret < 0)
1025 return -1;
1028 if (facility->options & eFacility_UUIE_fastStart) {
1029 for (i = 0; i < facility->fastStart.count; i++) {
1030 ret = process_olc(skb, ct, ctinfo, data, dataoff,
1031 &facility->fastStart.item[i]);
1032 if (ret < 0)
1033 return -1;
1037 return 0;
1040 /****************************************************************************/
1041 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1042 enum ip_conntrack_info ctinfo,
1043 unsigned char **data, int dataoff,
1044 Progress_UUIE *progress)
1046 int ret;
1047 int i;
1049 DEBUGP("nf_ct_q931: Progress\n");
1051 if (progress->options & eProgress_UUIE_h245Address) {
1052 ret = expect_h245(skb, ct, ctinfo, data, dataoff,
1053 &progress->h245Address);
1054 if (ret < 0)
1055 return -1;
1058 if (progress->options & eProgress_UUIE_fastStart) {
1059 for (i = 0; i < progress->fastStart.count; i++) {
1060 ret = process_olc(skb, ct, ctinfo, data, dataoff,
1061 &progress->fastStart.item[i]);
1062 if (ret < 0)
1063 return -1;
1067 return 0;
1070 /****************************************************************************/
1071 static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1072 enum ip_conntrack_info ctinfo,
1073 unsigned char **data, int dataoff, Q931 *q931)
1075 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1076 int i;
1077 int ret = 0;
1079 switch (pdu->h323_message_body.choice) {
1080 case eH323_UU_PDU_h323_message_body_setup:
1081 ret = process_setup(skb, ct, ctinfo, data, dataoff,
1082 &pdu->h323_message_body.setup);
1083 break;
1084 case eH323_UU_PDU_h323_message_body_callProceeding:
1085 ret = process_callproceeding(skb, ct, ctinfo, data, dataoff,
1086 &pdu->h323_message_body.
1087 callProceeding);
1088 break;
1089 case eH323_UU_PDU_h323_message_body_connect:
1090 ret = process_connect(skb, ct, ctinfo, data, dataoff,
1091 &pdu->h323_message_body.connect);
1092 break;
1093 case eH323_UU_PDU_h323_message_body_alerting:
1094 ret = process_alerting(skb, ct, ctinfo, data, dataoff,
1095 &pdu->h323_message_body.alerting);
1096 break;
1097 case eH323_UU_PDU_h323_message_body_facility:
1098 ret = process_facility(skb, ct, ctinfo, data, dataoff,
1099 &pdu->h323_message_body.facility);
1100 break;
1101 case eH323_UU_PDU_h323_message_body_progress:
1102 ret = process_progress(skb, ct, ctinfo, data, dataoff,
1103 &pdu->h323_message_body.progress);
1104 break;
1105 default:
1106 DEBUGP("nf_ct_q931: Q.931 signal %d\n",
1107 pdu->h323_message_body.choice);
1108 break;
1111 if (ret < 0)
1112 return -1;
1114 if (pdu->options & eH323_UU_PDU_h245Control) {
1115 for (i = 0; i < pdu->h245Control.count; i++) {
1116 ret = process_h245(skb, ct, ctinfo, data, dataoff,
1117 &pdu->h245Control.item[i]);
1118 if (ret < 0)
1119 return -1;
1123 return 0;
1126 /****************************************************************************/
1127 static int q931_help(struct sk_buff *skb, unsigned int protoff,
1128 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1130 static Q931 q931;
1131 unsigned char *data = NULL;
1132 int datalen;
1133 int dataoff;
1134 int ret;
1136 /* Until there's been traffic both ways, don't look in packets. */
1137 if (ctinfo != IP_CT_ESTABLISHED &&
1138 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1139 return NF_ACCEPT;
1141 DEBUGP("nf_ct_q931: skblen = %u\n", skb->len);
1143 spin_lock_bh(&nf_h323_lock);
1145 /* Process each TPKT */
1146 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1147 &data, &datalen, &dataoff)) {
1148 DEBUGP("nf_ct_q931: TPKT len=%d ", datalen);
1149 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1151 /* Decode Q.931 signal */
1152 ret = DecodeQ931(data, datalen, &q931);
1153 if (ret < 0) {
1154 DEBUGP("nf_ct_q931: decoding error: %s\n",
1155 ret == H323_ERROR_BOUND ?
1156 "out of bound" : "out of range");
1157 /* We don't drop when decoding error */
1158 break;
1161 /* Process Q.931 signal */
1162 if (process_q931(skb, ct, ctinfo, &data, dataoff, &q931) < 0)
1163 goto drop;
1166 spin_unlock_bh(&nf_h323_lock);
1167 return NF_ACCEPT;
1169 drop:
1170 spin_unlock_bh(&nf_h323_lock);
1171 if (net_ratelimit())
1172 printk("nf_ct_q931: packet dropped\n");
1173 return NF_DROP;
1176 /****************************************************************************/
1177 static const struct nf_conntrack_expect_policy q931_exp_policy = {
1178 /* T.120 and H.245 */
1179 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1180 .timeout = 240,
1183 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1185 .name = "Q.931",
1186 .me = THIS_MODULE,
1187 .tuple.src.l3num = AF_INET,
1188 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1189 .tuple.dst.protonum = IPPROTO_TCP,
1190 .mask.src.l3num = 0xFFFF,
1191 .mask.src.u.tcp.port = __constant_htons(0xFFFF),
1192 .mask.dst.protonum = 0xFF,
1193 .help = q931_help,
1194 .expect_policy = &q931_exp_policy,
1197 .name = "Q.931",
1198 .me = THIS_MODULE,
1199 .tuple.src.l3num = AF_INET6,
1200 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1201 .tuple.dst.protonum = IPPROTO_TCP,
1202 .mask.src.l3num = 0xFFFF,
1203 .mask.src.u.tcp.port = __constant_htons(0xFFFF),
1204 .mask.dst.protonum = 0xFF,
1205 .help = q931_help,
1206 .expect_policy = &q931_exp_policy,
1210 /****************************************************************************/
1211 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1212 int *datalen)
1214 struct udphdr _uh, *uh;
1215 int dataoff;
1217 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1218 if (uh == NULL)
1219 return NULL;
1220 dataoff = protoff + sizeof(_uh);
1221 if (dataoff >= skb->len)
1222 return NULL;
1223 *datalen = skb->len - dataoff;
1224 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1227 /****************************************************************************/
1228 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1229 union nf_inet_addr *addr,
1230 __be16 port)
1232 struct nf_conntrack_expect *exp;
1233 struct nf_conntrack_tuple tuple;
1235 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1236 tuple.src.u.tcp.port = 0;
1237 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1238 tuple.dst.u.tcp.port = port;
1239 tuple.dst.protonum = IPPROTO_TCP;
1241 exp = __nf_conntrack_expect_find(&tuple);
1242 if (exp && exp->master == ct)
1243 return exp;
1244 return NULL;
1247 /****************************************************************************/
1248 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1249 unsigned timeout)
1251 if (!exp || !del_timer(&exp->timeout))
1252 return 0;
1254 exp->timeout.expires = jiffies + timeout * HZ;
1255 add_timer(&exp->timeout);
1257 return 1;
1260 /****************************************************************************/
1261 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1262 enum ip_conntrack_info ctinfo,
1263 unsigned char **data,
1264 TransportAddress *taddr, int count)
1266 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1267 int dir = CTINFO2DIR(ctinfo);
1268 int ret = 0;
1269 int i;
1270 __be16 port;
1271 union nf_inet_addr addr;
1272 struct nf_conntrack_expect *exp;
1273 typeof(nat_q931_hook) nat_q931;
1275 /* Look for the first related address */
1276 for (i = 0; i < count; i++) {
1277 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1278 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1279 sizeof(addr)) == 0 && port != 0)
1280 break;
1283 if (i >= count) /* Not found */
1284 return 0;
1286 /* Create expect for Q.931 */
1287 if ((exp = nf_conntrack_expect_alloc(ct)) == NULL)
1288 return -1;
1289 nf_conntrack_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1290 ct->tuplehash[!dir].tuple.src.l3num,
1291 gkrouted_only ? /* only accept calls from GK? */
1292 &ct->tuplehash[!dir].tuple.src.u3 :
1293 NULL,
1294 &ct->tuplehash[!dir].tuple.dst.u3,
1295 IPPROTO_TCP, NULL, &port);
1296 exp->helper = nf_conntrack_helper_q931;
1297 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1299 nat_q931 = rcu_dereference(nat_q931_hook);
1300 if (nat_q931 && ct->status & IPS_NAT_MASK) { /* Need NAT */
1301 ret = nat_q931(skb, ct, ctinfo, data, taddr, i, port, exp);
1302 } else { /* Conntrack only */
1303 if (nf_conntrack_expect_related(exp) == 0) {
1304 DEBUGP("nf_ct_ras: expect Q.931 ");
1305 NF_CT_DUMP_TUPLE(&exp->tuple);
1307 /* Save port for looking up expect in processing RCF */
1308 info->sig_port[dir] = port;
1309 } else
1310 ret = -1;
1313 nf_conntrack_expect_put(exp);
1315 return ret;
1318 /****************************************************************************/
1319 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1320 enum ip_conntrack_info ctinfo,
1321 unsigned char **data, GatekeeperRequest *grq)
1323 typeof(set_ras_addr_hook) set_ras_addr;
1325 DEBUGP("nf_ct_ras: GRQ\n");
1327 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1328 if (set_ras_addr && ct->status & IPS_NAT_MASK) /* NATed */
1329 return set_ras_addr(skb, ct, ctinfo, data,
1330 &grq->rasAddress, 1);
1331 return 0;
1334 /****************************************************************************/
1335 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1336 enum ip_conntrack_info ctinfo,
1337 unsigned char **data, GatekeeperConfirm *gcf)
1339 int dir = CTINFO2DIR(ctinfo);
1340 int ret = 0;
1341 __be16 port;
1342 union nf_inet_addr addr;
1343 struct nf_conntrack_expect *exp;
1345 DEBUGP("nf_ct_ras: GCF\n");
1347 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1348 return 0;
1350 /* Registration port is the same as discovery port */
1351 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1352 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1353 return 0;
1355 /* Avoid RAS expectation loops. A GCF is never expected. */
1356 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1357 return 0;
1359 /* Need new expect */
1360 if ((exp = nf_conntrack_expect_alloc(ct)) == NULL)
1361 return -1;
1362 nf_conntrack_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1363 ct->tuplehash[!dir].tuple.src.l3num,
1364 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1365 IPPROTO_UDP, NULL, &port);
1366 exp->helper = nf_conntrack_helper_ras;
1368 if (nf_conntrack_expect_related(exp) == 0) {
1369 DEBUGP("nf_ct_ras: expect RAS ");
1370 NF_CT_DUMP_TUPLE(&exp->tuple);
1371 } else
1372 ret = -1;
1374 nf_conntrack_expect_put(exp);
1376 return ret;
1379 /****************************************************************************/
1380 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1381 enum ip_conntrack_info ctinfo,
1382 unsigned char **data, RegistrationRequest *rrq)
1384 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1385 int ret;
1386 typeof(set_ras_addr_hook) set_ras_addr;
1388 DEBUGP("nf_ct_ras: RRQ\n");
1390 ret = expect_q931(skb, ct, ctinfo, data,
1391 rrq->callSignalAddress.item,
1392 rrq->callSignalAddress.count);
1393 if (ret < 0)
1394 return -1;
1396 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1397 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1398 ret = set_ras_addr(skb, ct, ctinfo, data,
1399 rrq->rasAddress.item,
1400 rrq->rasAddress.count);
1401 if (ret < 0)
1402 return -1;
1405 if (rrq->options & eRegistrationRequest_timeToLive) {
1406 DEBUGP("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1407 info->timeout = rrq->timeToLive;
1408 } else
1409 info->timeout = default_rrq_ttl;
1411 return 0;
1414 /****************************************************************************/
1415 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1416 enum ip_conntrack_info ctinfo,
1417 unsigned char **data, RegistrationConfirm *rcf)
1419 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1420 int dir = CTINFO2DIR(ctinfo);
1421 int ret;
1422 struct nf_conntrack_expect *exp;
1423 typeof(set_sig_addr_hook) set_sig_addr;
1425 DEBUGP("nf_ct_ras: RCF\n");
1427 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1428 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1429 ret = set_sig_addr(skb, ct, ctinfo, data,
1430 rcf->callSignalAddress.item,
1431 rcf->callSignalAddress.count);
1432 if (ret < 0)
1433 return -1;
1436 if (rcf->options & eRegistrationConfirm_timeToLive) {
1437 DEBUGP("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1438 info->timeout = rcf->timeToLive;
1441 if (info->timeout > 0) {
1442 DEBUGP
1443 ("nf_ct_ras: set RAS connection timeout to %u seconds\n",
1444 info->timeout);
1445 nf_ct_refresh(ct, skb, info->timeout * HZ);
1447 /* Set expect timeout */
1448 read_lock_bh(&nf_conntrack_lock);
1449 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1450 info->sig_port[!dir]);
1451 if (exp) {
1452 DEBUGP("nf_ct_ras: set Q.931 expect "
1453 "timeout to %u seconds for",
1454 info->timeout);
1455 NF_CT_DUMP_TUPLE(&exp->tuple);
1456 set_expect_timeout(exp, info->timeout);
1458 read_unlock_bh(&nf_conntrack_lock);
1461 return 0;
1464 /****************************************************************************/
1465 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1466 enum ip_conntrack_info ctinfo,
1467 unsigned char **data, UnregistrationRequest *urq)
1469 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1470 int dir = CTINFO2DIR(ctinfo);
1471 int ret;
1472 typeof(set_sig_addr_hook) set_sig_addr;
1474 DEBUGP("nf_ct_ras: URQ\n");
1476 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1477 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1478 ret = set_sig_addr(skb, ct, ctinfo, data,
1479 urq->callSignalAddress.item,
1480 urq->callSignalAddress.count);
1481 if (ret < 0)
1482 return -1;
1485 /* Clear old expect */
1486 nf_ct_remove_expectations(ct);
1487 info->sig_port[dir] = 0;
1488 info->sig_port[!dir] = 0;
1490 /* Give it 30 seconds for UCF or URJ */
1491 nf_ct_refresh(ct, skb, 30 * HZ);
1493 return 0;
1496 /****************************************************************************/
1497 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1498 enum ip_conntrack_info ctinfo,
1499 unsigned char **data, AdmissionRequest *arq)
1501 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1502 int dir = CTINFO2DIR(ctinfo);
1503 __be16 port;
1504 union nf_inet_addr addr;
1505 typeof(set_h225_addr_hook) set_h225_addr;
1507 DEBUGP("nf_ct_ras: ARQ\n");
1509 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1510 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1511 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1512 &addr, &port) &&
1513 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1514 port == info->sig_port[dir] &&
1515 set_h225_addr && ct->status & IPS_NAT_MASK) {
1516 /* Answering ARQ */
1517 return set_h225_addr(skb, data, 0,
1518 &arq->destCallSignalAddress,
1519 &ct->tuplehash[!dir].tuple.dst.u3,
1520 info->sig_port[!dir]);
1523 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1524 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1525 &addr, &port) &&
1526 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1527 set_h225_addr && ct->status & IPS_NAT_MASK) {
1528 /* Calling ARQ */
1529 return set_h225_addr(skb, data, 0,
1530 &arq->srcCallSignalAddress,
1531 &ct->tuplehash[!dir].tuple.dst.u3,
1532 port);
1535 return 0;
1538 /****************************************************************************/
1539 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1540 enum ip_conntrack_info ctinfo,
1541 unsigned char **data, AdmissionConfirm *acf)
1543 int dir = CTINFO2DIR(ctinfo);
1544 int ret = 0;
1545 __be16 port;
1546 union nf_inet_addr addr;
1547 struct nf_conntrack_expect *exp;
1548 typeof(set_sig_addr_hook) set_sig_addr;
1550 DEBUGP("nf_ct_ras: ACF\n");
1552 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1553 &addr, &port))
1554 return 0;
1556 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1557 /* Answering ACF */
1558 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1559 if (set_sig_addr && ct->status & IPS_NAT_MASK)
1560 return set_sig_addr(skb, ct, ctinfo, data,
1561 &acf->destCallSignalAddress, 1);
1562 return 0;
1565 /* Need new expect */
1566 if ((exp = nf_conntrack_expect_alloc(ct)) == NULL)
1567 return -1;
1568 nf_conntrack_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1569 ct->tuplehash[!dir].tuple.src.l3num,
1570 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1571 IPPROTO_TCP, NULL, &port);
1572 exp->flags = NF_CT_EXPECT_PERMANENT;
1573 exp->helper = nf_conntrack_helper_q931;
1575 if (nf_conntrack_expect_related(exp) == 0) {
1576 DEBUGP("nf_ct_ras: expect Q.931 ");
1577 NF_CT_DUMP_TUPLE(&exp->tuple);
1578 } else
1579 ret = -1;
1581 nf_conntrack_expect_put(exp);
1583 return ret;
1586 /****************************************************************************/
1587 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1588 enum ip_conntrack_info ctinfo,
1589 unsigned char **data, LocationRequest *lrq)
1591 typeof(set_ras_addr_hook) set_ras_addr;
1593 DEBUGP("nf_ct_ras: LRQ\n");
1595 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1596 if (set_ras_addr && ct->status & IPS_NAT_MASK)
1597 return set_ras_addr(skb, ct, ctinfo, data,
1598 &lrq->replyAddress, 1);
1599 return 0;
1602 /****************************************************************************/
1603 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1604 enum ip_conntrack_info ctinfo,
1605 unsigned char **data, LocationConfirm *lcf)
1607 int dir = CTINFO2DIR(ctinfo);
1608 int ret = 0;
1609 __be16 port;
1610 union nf_inet_addr addr;
1611 struct nf_conntrack_expect *exp;
1613 DEBUGP("nf_ct_ras: LCF\n");
1615 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1616 &addr, &port))
1617 return 0;
1619 /* Need new expect for call signal */
1620 if ((exp = nf_conntrack_expect_alloc(ct)) == NULL)
1621 return -1;
1622 nf_conntrack_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
1623 ct->tuplehash[!dir].tuple.src.l3num,
1624 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1625 IPPROTO_TCP, NULL, &port);
1626 exp->flags = NF_CT_EXPECT_PERMANENT;
1627 exp->helper = nf_conntrack_helper_q931;
1629 if (nf_conntrack_expect_related(exp) == 0) {
1630 DEBUGP("nf_ct_ras: expect Q.931 ");
1631 NF_CT_DUMP_TUPLE(&exp->tuple);
1632 } else
1633 ret = -1;
1635 nf_conntrack_expect_put(exp);
1637 /* Ignore rasAddress */
1639 return ret;
1642 /****************************************************************************/
1643 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1644 enum ip_conntrack_info ctinfo,
1645 unsigned char **data, InfoRequestResponse *irr)
1647 int ret;
1648 typeof(set_ras_addr_hook) set_ras_addr;
1649 typeof(set_sig_addr_hook) set_sig_addr;
1651 DEBUGP("nf_ct_ras: IRR\n");
1653 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1654 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1655 ret = set_ras_addr(skb, ct, ctinfo, data,
1656 &irr->rasAddress, 1);
1657 if (ret < 0)
1658 return -1;
1661 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1662 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1663 ret = set_sig_addr(skb, ct, ctinfo, data,
1664 irr->callSignalAddress.item,
1665 irr->callSignalAddress.count);
1666 if (ret < 0)
1667 return -1;
1670 return 0;
1673 /****************************************************************************/
1674 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1675 enum ip_conntrack_info ctinfo,
1676 unsigned char **data, RasMessage *ras)
1678 switch (ras->choice) {
1679 case eRasMessage_gatekeeperRequest:
1680 return process_grq(skb, ct, ctinfo, data,
1681 &ras->gatekeeperRequest);
1682 case eRasMessage_gatekeeperConfirm:
1683 return process_gcf(skb, ct, ctinfo, data,
1684 &ras->gatekeeperConfirm);
1685 case eRasMessage_registrationRequest:
1686 return process_rrq(skb, ct, ctinfo, data,
1687 &ras->registrationRequest);
1688 case eRasMessage_registrationConfirm:
1689 return process_rcf(skb, ct, ctinfo, data,
1690 &ras->registrationConfirm);
1691 case eRasMessage_unregistrationRequest:
1692 return process_urq(skb, ct, ctinfo, data,
1693 &ras->unregistrationRequest);
1694 case eRasMessage_admissionRequest:
1695 return process_arq(skb, ct, ctinfo, data,
1696 &ras->admissionRequest);
1697 case eRasMessage_admissionConfirm:
1698 return process_acf(skb, ct, ctinfo, data,
1699 &ras->admissionConfirm);
1700 case eRasMessage_locationRequest:
1701 return process_lrq(skb, ct, ctinfo, data,
1702 &ras->locationRequest);
1703 case eRasMessage_locationConfirm:
1704 return process_lcf(skb, ct, ctinfo, data,
1705 &ras->locationConfirm);
1706 case eRasMessage_infoRequestResponse:
1707 return process_irr(skb, ct, ctinfo, data,
1708 &ras->infoRequestResponse);
1709 default:
1710 DEBUGP("nf_ct_ras: RAS message %d\n", ras->choice);
1711 break;
1714 return 0;
1717 /****************************************************************************/
1718 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1719 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1721 static RasMessage ras;
1722 unsigned char *data;
1723 int datalen = 0;
1724 int ret;
1726 DEBUGP("nf_ct_ras: skblen = %u\n", skb->len);
1728 spin_lock_bh(&nf_h323_lock);
1730 /* Get UDP data */
1731 data = get_udp_data(skb, protoff, &datalen);
1732 if (data == NULL)
1733 goto accept;
1734 DEBUGP("nf_ct_ras: RAS message len=%d ", datalen);
1735 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1737 /* Decode RAS message */
1738 ret = DecodeRasMessage(data, datalen, &ras);
1739 if (ret < 0) {
1740 DEBUGP("nf_ct_ras: decoding error: %s\n",
1741 ret == H323_ERROR_BOUND ?
1742 "out of bound" : "out of range");
1743 goto accept;
1746 /* Process RAS message */
1747 if (process_ras(skb, ct, ctinfo, &data, &ras) < 0)
1748 goto drop;
1750 accept:
1751 spin_unlock_bh(&nf_h323_lock);
1752 return NF_ACCEPT;
1754 drop:
1755 spin_unlock_bh(&nf_h323_lock);
1756 if (net_ratelimit())
1757 printk("nf_ct_ras: packet dropped\n");
1758 return NF_DROP;
1761 /****************************************************************************/
1762 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1763 .max_expected = 32,
1764 .timeout = 240,
1767 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1769 .name = "RAS",
1770 .me = THIS_MODULE,
1771 .tuple.src.l3num = AF_INET,
1772 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1773 .tuple.dst.protonum = IPPROTO_UDP,
1774 .mask.src.l3num = 0xFFFF,
1775 .mask.src.u.udp.port = __constant_htons(0xFFFF),
1776 .mask.dst.protonum = 0xFF,
1777 .help = ras_help,
1778 .expect_policy = &ras_exp_policy,
1781 .name = "RAS",
1782 .me = THIS_MODULE,
1783 .tuple.src.l3num = AF_INET6,
1784 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1785 .tuple.dst.protonum = IPPROTO_UDP,
1786 .mask.src.l3num = 0xFFFF,
1787 .mask.src.u.udp.port = __constant_htons(0xFFFF),
1788 .mask.dst.protonum = 0xFF,
1789 .help = ras_help,
1790 .expect_policy = &ras_exp_policy,
1794 /****************************************************************************/
1795 static void __exit nf_conntrack_h323_fini(void)
1797 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1798 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1799 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1800 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1801 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1802 kfree(h323_buffer);
1803 DEBUGP("nf_ct_h323: fini\n");
1806 /****************************************************************************/
1807 static int __init nf_conntrack_h323_init(void)
1809 int ret;
1811 h323_buffer = kmalloc(65536, GFP_KERNEL);
1812 if (!h323_buffer)
1813 return -ENOMEM;
1814 ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1815 if (ret < 0)
1816 goto err1;
1817 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1818 if (ret < 0)
1819 goto err2;
1820 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1821 if (ret < 0)
1822 goto err3;
1823 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1824 if (ret < 0)
1825 goto err4;
1826 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1827 if (ret < 0)
1828 goto err5;
1829 DEBUGP("nf_ct_h323: init success\n");
1830 return 0;
1832 err5:
1833 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1834 err4:
1835 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1836 err3:
1837 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1838 err2:
1839 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1840 err1:
1841 kfree(h323_buffer);
1842 return ret;
1845 /****************************************************************************/
1846 module_init(nf_conntrack_h323_init);
1847 module_exit(nf_conntrack_h323_fini);
1849 EXPORT_SYMBOL_GPL(get_h225_addr);
1850 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1851 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1852 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1853 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1854 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1855 EXPORT_SYMBOL_GPL(nat_t120_hook);
1856 EXPORT_SYMBOL_GPL(nat_h245_hook);
1857 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1858 EXPORT_SYMBOL_GPL(nat_q931_hook);
1860 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1861 MODULE_DESCRIPTION("H.323 connection tracking helper");
1862 MODULE_LICENSE("GPL");
1863 MODULE_ALIAS("ip_conntrack_h323");