[NETFILTER]: nf_conntrack: use hashtable for expectations
[deliverable/linux.git] / net / netfilter / nf_conntrack_h323_main.c
1 /*
2 * H.323 connection tracking helper
3 *
4 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
5 *
6 * This source code is licensed under General Public License version 2.
7 *
8 * Based on the 'brute force' H.323 connection tracking module by
9 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
10 *
11 * For more information, please see http://nath323.sourceforge.net/
12 */
13
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>
25
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>
33
34 #if 0
35 #define DEBUGP printk
36 #else
37 #define DEBUGP(format, args...)
38 #endif
39
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");
44
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");
48
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)");
54
55 /* Hooks for NAT */
56 int (*set_h245_addr_hook) (struct sk_buff **pskb,
57 unsigned char **data, int dataoff,
58 H245_TransportAddress *taddr,
59 union nf_conntrack_address *addr, __be16 port)
60 __read_mostly;
61 int (*set_h225_addr_hook) (struct sk_buff **pskb,
62 unsigned char **data, int dataoff,
63 TransportAddress *taddr,
64 union nf_conntrack_address *addr, __be16 port)
65 __read_mostly;
66 int (*set_sig_addr_hook) (struct sk_buff **pskb,
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 **pskb,
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 **pskb,
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 **pskb,
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 **pskb,
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 **pskb,
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 **pskb,
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;
108
109 static DEFINE_SPINLOCK(nf_h323_lock);
110 static char *h323_buffer;
111
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[];
115
116 /****************************************************************************/
117 static int get_tpkt_data(struct sk_buff **pskb, unsigned int protoff,
118 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
119 unsigned char **data, int *datalen, int *dataoff)
120 {
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;
129
130 /* Get TCP header */
131 th = skb_header_pointer(*pskb, protoff, sizeof(_tcph), &_tcph);
132 if (th == NULL)
133 return 0;
134
135 /* Get TCP data offset */
136 tcpdataoff = protoff + th->doff * 4;
137
138 /* Get TCP data length */
139 tcpdatalen = (*pskb)->len - tcpdataoff;
140 if (tcpdatalen <= 0) /* No TCP data */
141 goto clear_out;
142
143 if (*data == NULL) { /* first TPKT */
144 /* Get first TPKT pointer */
145 tpkt = skb_header_pointer(*pskb, tcpdataoff, tcpdatalen,
146 h323_buffer);
147 BUG_ON(tpkt == NULL);
148
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;
163 }
164
165 /* Fragmented TPKT */
166 if (net_ratelimit())
167 printk("nf_ct_h323: "
168 "fragmented TPKT\n");
169 goto clear_out;
170 }
171
172 /* It is not even a TPKT */
173 return 0;
174 }
175 tpktoff = 0;
176 } else { /* Next TPKT */
177 tpktoff = *dataoff + *datalen;
178 tcpdatalen -= tpktoff;
179 if (tcpdatalen <= 4) /* No more TPKT */
180 goto clear_out;
181 tpkt = *data + *datalen;
182
183 /* Validate TPKT identifier */
184 if (tpkt[0] != 0x03 || tpkt[1] != 0)
185 goto clear_out;
186 }
187
188 /* Validate TPKT length */
189 tpktlen = tpkt[2] * 256 + tpkt[3];
190 if (tpktlen < 4)
191 goto clear_out;
192 if (tpktlen > tcpdatalen) {
193 if (tcpdatalen == 4) { /* Separate TPKT header */
194 /* Netmeeting sends TPKT header and data separately */
195 DEBUGP("nf_ct_h323: separate TPKT header indicates "
196 "there will be TPKT data of %hu bytes\n",
197 tpktlen - 4);
198 info->tpkt_len[dir] = tpktlen - 4;
199 return 0;
200 }
201
202 if (net_ratelimit())
203 printk("nf_ct_h323: incomplete TPKT (fragmented?)\n");
204 goto clear_out;
205 }
206
207 /* This is the encapsulated data */
208 *data = tpkt + 4;
209 *datalen = tpktlen - 4;
210 *dataoff = tpktoff + 4;
211
212 out:
213 /* Clear TPKT length */
214 info->tpkt_len[dir] = 0;
215 return 1;
216
217 clear_out:
218 info->tpkt_len[dir] = 0;
219 return 0;
220 }
221
222 /****************************************************************************/
223 static int get_h245_addr(struct nf_conn *ct, unsigned char *data,
224 H245_TransportAddress *taddr,
225 union nf_conntrack_address *addr, __be16 *port)
226 {
227 unsigned char *p;
228 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
229 int len;
230
231 if (taddr->choice != eH245_TransportAddress_unicastAddress)
232 return 0;
233
234 switch (taddr->unicastAddress.choice) {
235 case eUnicastAddress_iPAddress:
236 if (family != AF_INET)
237 return 0;
238 p = data + taddr->unicastAddress.iPAddress.network;
239 len = 4;
240 break;
241 case eUnicastAddress_iP6Address:
242 if (family != AF_INET6)
243 return 0;
244 p = data + taddr->unicastAddress.iP6Address.network;
245 len = 16;
246 break;
247 default:
248 return 0;
249 }
250
251 memcpy(addr, p, len);
252 memset((void *)addr + len, 0, sizeof(*addr) - len);
253 memcpy(port, p + len, sizeof(__be16));
254
255 return 1;
256 }
257
258 /****************************************************************************/
259 static int expect_rtp_rtcp(struct sk_buff **pskb, struct nf_conn *ct,
260 enum ip_conntrack_info ctinfo,
261 unsigned char **data, int dataoff,
262 H245_TransportAddress *taddr)
263 {
264 int dir = CTINFO2DIR(ctinfo);
265 int ret = 0;
266 __be16 port;
267 __be16 rtp_port, rtcp_port;
268 union nf_conntrack_address addr;
269 struct nf_conntrack_expect *rtp_exp;
270 struct nf_conntrack_expect *rtcp_exp;
271 typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
272
273 /* Read RTP or RTCP address */
274 if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
275 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
276 port == 0)
277 return 0;
278
279 /* RTP port is even */
280 port &= htons(~1);
281 rtp_port = port;
282 rtcp_port = htons(ntohs(port) + 1);
283
284 /* Create expect for RTP */
285 if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
286 return -1;
287 nf_ct_expect_init(rtp_exp, ct->tuplehash[!dir].tuple.src.l3num,
288 &ct->tuplehash[!dir].tuple.src.u3,
289 &ct->tuplehash[!dir].tuple.dst.u3,
290 IPPROTO_UDP, NULL, &rtp_port);
291
292 /* Create expect for RTCP */
293 if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
294 nf_ct_expect_put(rtp_exp);
295 return -1;
296 }
297 nf_ct_expect_init(rtcp_exp, 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);
301
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(pskb, ct, ctinfo, data, dataoff,
309 taddr, port, rtp_port, rtp_exp, rtcp_exp);
310 } else { /* Conntrack only */
311 if (nf_ct_expect_related(rtp_exp) == 0) {
312 if (nf_ct_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_ct_unexpect_related(rtp_exp);
319 ret = -1;
320 }
321 } else
322 ret = -1;
323 }
324
325 nf_ct_expect_put(rtp_exp);
326 nf_ct_expect_put(rtcp_exp);
327
328 return ret;
329 }
330
331 /****************************************************************************/
332 static int expect_t120(struct sk_buff **pskb,
333 struct nf_conn *ct,
334 enum ip_conntrack_info ctinfo,
335 unsigned char **data, int dataoff,
336 H245_TransportAddress *taddr)
337 {
338 int dir = CTINFO2DIR(ctinfo);
339 int ret = 0;
340 __be16 port;
341 union nf_conntrack_address addr;
342 struct nf_conntrack_expect *exp;
343 typeof(nat_t120_hook) nat_t120;
344
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;
350
351 /* Create expect for T.120 connections */
352 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
353 return -1;
354 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
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 */
359
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 ct->status & IPS_NAT_MASK) {
365 /* NAT needed */
366 ret = nat_t120(pskb, ct, ctinfo, data, dataoff, taddr,
367 port, exp);
368 } else { /* Conntrack only */
369 if (nf_ct_expect_related(exp) == 0) {
370 DEBUGP("nf_ct_h323: expect T.120 ");
371 NF_CT_DUMP_TUPLE(&exp->tuple);
372 } else
373 ret = -1;
374 }
375
376 nf_ct_expect_put(exp);
377
378 return ret;
379 }
380
381 /****************************************************************************/
382 static int process_h245_channel(struct sk_buff **pskb,
383 struct nf_conn *ct,
384 enum ip_conntrack_info ctinfo,
385 unsigned char **data, int dataoff,
386 H2250LogicalChannelParameters *channel)
387 {
388 int ret;
389
390 if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
391 /* RTP */
392 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
393 &channel->mediaChannel);
394 if (ret < 0)
395 return -1;
396 }
397
398 if (channel->
399 options & eH2250LogicalChannelParameters_mediaControlChannel) {
400 /* RTCP */
401 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
402 &channel->mediaControlChannel);
403 if (ret < 0)
404 return -1;
405 }
406
407 return 0;
408 }
409
410 /****************************************************************************/
411 static int process_olc(struct sk_buff **pskb, struct nf_conn *ct,
412 enum ip_conntrack_info ctinfo,
413 unsigned char **data, int dataoff,
414 OpenLogicalChannel *olc)
415 {
416 int ret;
417
418 DEBUGP("nf_ct_h323: OpenLogicalChannel\n");
419
420 if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
421 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
422 {
423 ret = process_h245_channel(pskb, ct, ctinfo, data, dataoff,
424 &olc->
425 forwardLogicalChannelParameters.
426 multiplexParameters.
427 h2250LogicalChannelParameters);
428 if (ret < 0)
429 return -1;
430 }
431
432 if ((olc->options &
433 eOpenLogicalChannel_reverseLogicalChannelParameters) &&
434 (olc->reverseLogicalChannelParameters.options &
435 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
436 && (olc->reverseLogicalChannelParameters.multiplexParameters.
437 choice ==
438 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
439 {
440 ret =
441 process_h245_channel(pskb, ct, ctinfo, data, dataoff,
442 &olc->
443 reverseLogicalChannelParameters.
444 multiplexParameters.
445 h2250LogicalChannelParameters);
446 if (ret < 0)
447 return -1;
448 }
449
450 if ((olc->options & eOpenLogicalChannel_separateStack) &&
451 olc->forwardLogicalChannelParameters.dataType.choice ==
452 eDataType_data &&
453 olc->forwardLogicalChannelParameters.dataType.data.application.
454 choice == eDataApplicationCapability_application_t120 &&
455 olc->forwardLogicalChannelParameters.dataType.data.application.
456 t120.choice == eDataProtocolCapability_separateLANStack &&
457 olc->separateStack.networkAddress.choice ==
458 eNetworkAccessParameters_networkAddress_localAreaAddress) {
459 ret = expect_t120(pskb, ct, ctinfo, data, dataoff,
460 &olc->separateStack.networkAddress.
461 localAreaAddress);
462 if (ret < 0)
463 return -1;
464 }
465
466 return 0;
467 }
468
469 /****************************************************************************/
470 static int process_olca(struct sk_buff **pskb, struct nf_conn *ct,
471 enum ip_conntrack_info ctinfo,
472 unsigned char **data, int dataoff,
473 OpenLogicalChannelAck *olca)
474 {
475 H2250LogicalChannelAckParameters *ack;
476 int ret;
477
478 DEBUGP("nf_ct_h323: OpenLogicalChannelAck\n");
479
480 if ((olca->options &
481 eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
482 (olca->reverseLogicalChannelParameters.options &
483 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
484 && (olca->reverseLogicalChannelParameters.multiplexParameters.
485 choice ==
486 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
487 {
488 ret = process_h245_channel(pskb, ct, ctinfo, data, dataoff,
489 &olca->
490 reverseLogicalChannelParameters.
491 multiplexParameters.
492 h2250LogicalChannelParameters);
493 if (ret < 0)
494 return -1;
495 }
496
497 if ((olca->options &
498 eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
499 (olca->forwardMultiplexAckParameters.choice ==
500 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
501 {
502 ack = &olca->forwardMultiplexAckParameters.
503 h2250LogicalChannelAckParameters;
504 if (ack->options &
505 eH2250LogicalChannelAckParameters_mediaChannel) {
506 /* RTP */
507 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
508 &ack->mediaChannel);
509 if (ret < 0)
510 return -1;
511 }
512
513 if (ack->options &
514 eH2250LogicalChannelAckParameters_mediaControlChannel) {
515 /* RTCP */
516 ret = expect_rtp_rtcp(pskb, ct, ctinfo, data, dataoff,
517 &ack->mediaControlChannel);
518 if (ret < 0)
519 return -1;
520 }
521 }
522
523 if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
524 olca->separateStack.networkAddress.choice ==
525 eNetworkAccessParameters_networkAddress_localAreaAddress) {
526 ret = expect_t120(pskb, ct, ctinfo, data, dataoff,
527 &olca->separateStack.networkAddress.
528 localAreaAddress);
529 if (ret < 0)
530 return -1;
531 }
532
533 return 0;
534 }
535
536 /****************************************************************************/
537 static int process_h245(struct sk_buff **pskb, struct nf_conn *ct,
538 enum ip_conntrack_info ctinfo,
539 unsigned char **data, int dataoff,
540 MultimediaSystemControlMessage *mscm)
541 {
542 switch (mscm->choice) {
543 case eMultimediaSystemControlMessage_request:
544 if (mscm->request.choice ==
545 eRequestMessage_openLogicalChannel) {
546 return process_olc(pskb, ct, ctinfo, data, dataoff,
547 &mscm->request.openLogicalChannel);
548 }
549 DEBUGP("nf_ct_h323: H.245 Request %d\n",
550 mscm->request.choice);
551 break;
552 case eMultimediaSystemControlMessage_response:
553 if (mscm->response.choice ==
554 eResponseMessage_openLogicalChannelAck) {
555 return process_olca(pskb, ct, ctinfo, data, dataoff,
556 &mscm->response.
557 openLogicalChannelAck);
558 }
559 DEBUGP("nf_ct_h323: H.245 Response %d\n",
560 mscm->response.choice);
561 break;
562 default:
563 DEBUGP("nf_ct_h323: H.245 signal %d\n", mscm->choice);
564 break;
565 }
566
567 return 0;
568 }
569
570 /****************************************************************************/
571 static int h245_help(struct sk_buff **pskb, unsigned int protoff,
572 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
573 {
574 static MultimediaSystemControlMessage mscm;
575 unsigned char *data = NULL;
576 int datalen;
577 int dataoff;
578 int ret;
579
580 /* Until there's been traffic both ways, don't look in packets. */
581 if (ctinfo != IP_CT_ESTABLISHED &&
582 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
583 return NF_ACCEPT;
584 }
585 DEBUGP("nf_ct_h245: skblen = %u\n", (*pskb)->len);
586
587 spin_lock_bh(&nf_h323_lock);
588
589 /* Process each TPKT */
590 while (get_tpkt_data(pskb, protoff, ct, ctinfo,
591 &data, &datalen, &dataoff)) {
592 DEBUGP("nf_ct_h245: TPKT len=%d ", datalen);
593 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
594
595 /* Decode H.245 signal */
596 ret = DecodeMultimediaSystemControlMessage(data, datalen,
597 &mscm);
598 if (ret < 0) {
599 if (net_ratelimit())
600 printk("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;
605 }
606
607 /* Process H.245 signal */
608 if (process_h245(pskb, ct, ctinfo, &data, dataoff, &mscm) < 0)
609 goto drop;
610 }
611
612 spin_unlock_bh(&nf_h323_lock);
613 return NF_ACCEPT;
614
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;
620 }
621
622 /****************************************************************************/
623 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
624 .name = "H.245",
625 .me = THIS_MODULE,
626 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
627 .timeout = 240,
628 .tuple.dst.protonum = IPPROTO_UDP,
629 .help = h245_help
630 };
631
632 /****************************************************************************/
633 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
634 TransportAddress *taddr,
635 union nf_conntrack_address *addr, __be16 *port)
636 {
637 unsigned char *p;
638 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
639 int len;
640
641 switch (taddr->choice) {
642 case eTransportAddress_ipAddress:
643 if (family != AF_INET)
644 return 0;
645 p = data + taddr->ipAddress.ip;
646 len = 4;
647 break;
648 case eTransportAddress_ip6Address:
649 if (family != AF_INET6)
650 return 0;
651 p = data + taddr->ip6Address.ip;
652 len = 16;
653 break;
654 default:
655 return 0;
656 }
657
658 memcpy(addr, p, len);
659 memset((void *)addr + len, 0, sizeof(*addr) - len);
660 memcpy(port, p + len, sizeof(__be16));
661
662 return 1;
663 }
664
665 /****************************************************************************/
666 static int expect_h245(struct sk_buff **pskb, struct nf_conn *ct,
667 enum ip_conntrack_info ctinfo,
668 unsigned char **data, int dataoff,
669 TransportAddress *taddr)
670 {
671 int dir = CTINFO2DIR(ctinfo);
672 int ret = 0;
673 __be16 port;
674 union nf_conntrack_address addr;
675 struct nf_conntrack_expect *exp;
676 typeof(nat_h245_hook) nat_h245;
677
678 /* Read h245Address */
679 if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
680 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
681 port == 0)
682 return 0;
683
684 /* Create expect for h245 connection */
685 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
686 return -1;
687 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
688 &ct->tuplehash[!dir].tuple.src.u3,
689 &ct->tuplehash[!dir].tuple.dst.u3,
690 IPPROTO_TCP, NULL, &port);
691 exp->helper = &nf_conntrack_helper_h245;
692
693 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
694 &ct->tuplehash[!dir].tuple.dst.u3,
695 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
696 (nat_h245 = rcu_dereference(nat_h245_hook)) &&
697 ct->status & IPS_NAT_MASK) {
698 /* NAT needed */
699 ret = nat_h245(pskb, ct, ctinfo, data, dataoff, taddr,
700 port, exp);
701 } else { /* Conntrack only */
702 if (nf_ct_expect_related(exp) == 0) {
703 DEBUGP("nf_ct_q931: expect H.245 ");
704 NF_CT_DUMP_TUPLE(&exp->tuple);
705 } else
706 ret = -1;
707 }
708
709 nf_ct_expect_put(exp);
710
711 return ret;
712 }
713
714 /* If the calling party is on the same side of the forward-to party,
715 * we don't need to track the second call */
716 static int callforward_do_filter(union nf_conntrack_address *src,
717 union nf_conntrack_address *dst,
718 int family)
719 {
720 struct flowi fl1, fl2;
721 int ret = 0;
722
723 memset(&fl1, 0, sizeof(fl1));
724 memset(&fl2, 0, sizeof(fl2));
725
726 switch (family) {
727 case AF_INET: {
728 struct rtable *rt1, *rt2;
729
730 fl1.fl4_dst = src->ip;
731 fl2.fl4_dst = dst->ip;
732 if (ip_route_output_key(&rt1, &fl1) == 0) {
733 if (ip_route_output_key(&rt2, &fl2) == 0) {
734 if (rt1->rt_gateway == rt2->rt_gateway &&
735 rt1->u.dst.dev == rt2->u.dst.dev)
736 ret = 1;
737 dst_release(&rt2->u.dst);
738 }
739 dst_release(&rt1->u.dst);
740 }
741 break;
742 }
743 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
744 case AF_INET6: {
745 struct rt6_info *rt1, *rt2;
746
747 memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst));
748 memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst));
749 rt1 = (struct rt6_info *)ip6_route_output(NULL, &fl1);
750 if (rt1) {
751 rt2 = (struct rt6_info *)ip6_route_output(NULL, &fl2);
752 if (rt2) {
753 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
754 sizeof(rt1->rt6i_gateway)) &&
755 rt1->u.dst.dev == rt2->u.dst.dev)
756 ret = 1;
757 dst_release(&rt2->u.dst);
758 }
759 dst_release(&rt1->u.dst);
760 }
761 break;
762 }
763 #endif
764 }
765 return ret;
766
767 }
768
769 /****************************************************************************/
770 static int expect_callforwarding(struct sk_buff **pskb,
771 struct nf_conn *ct,
772 enum ip_conntrack_info ctinfo,
773 unsigned char **data, int dataoff,
774 TransportAddress *taddr)
775 {
776 int dir = CTINFO2DIR(ctinfo);
777 int ret = 0;
778 __be16 port;
779 union nf_conntrack_address addr;
780 struct nf_conntrack_expect *exp;
781 typeof(nat_callforwarding_hook) nat_callforwarding;
782
783 /* Read alternativeAddress */
784 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
785 return 0;
786
787 /* If the calling party is on the same side of the forward-to party,
788 * we don't need to track the second call */
789 if (callforward_filter &&
790 callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
791 ct->tuplehash[!dir].tuple.src.l3num)) {
792 DEBUGP("nf_ct_q931: Call Forwarding not tracked\n");
793 return 0;
794 }
795
796 /* Create expect for the second call leg */
797 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
798 return -1;
799 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
800 &ct->tuplehash[!dir].tuple.src.u3, &addr,
801 IPPROTO_TCP, NULL, &port);
802 exp->helper = nf_conntrack_helper_q931;
803
804 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
805 &ct->tuplehash[!dir].tuple.dst.u3,
806 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
807 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
808 ct->status & IPS_NAT_MASK) {
809 /* Need NAT */
810 ret = nat_callforwarding(pskb, ct, ctinfo, data, dataoff,
811 taddr, port, exp);
812 } else { /* Conntrack only */
813 if (nf_ct_expect_related(exp) == 0) {
814 DEBUGP("nf_ct_q931: expect Call Forwarding ");
815 NF_CT_DUMP_TUPLE(&exp->tuple);
816 } else
817 ret = -1;
818 }
819
820 nf_ct_expect_put(exp);
821
822 return ret;
823 }
824
825 /****************************************************************************/
826 static int process_setup(struct sk_buff **pskb, struct nf_conn *ct,
827 enum ip_conntrack_info ctinfo,
828 unsigned char **data, int dataoff,
829 Setup_UUIE *setup)
830 {
831 int dir = CTINFO2DIR(ctinfo);
832 int ret;
833 int i;
834 __be16 port;
835 union nf_conntrack_address addr;
836 typeof(set_h225_addr_hook) set_h225_addr;
837
838 DEBUGP("nf_ct_q931: Setup\n");
839
840 if (setup->options & eSetup_UUIE_h245Address) {
841 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
842 &setup->h245Address);
843 if (ret < 0)
844 return -1;
845 }
846
847 set_h225_addr = rcu_dereference(set_h225_addr_hook);
848 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
849 (set_h225_addr) && ct->status && IPS_NAT_MASK &&
850 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
851 &addr, &port) &&
852 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
853 DEBUGP("nf_ct_q931: set destCallSignalAddress "
854 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
855 NIP6(*(struct in6_addr *)&addr), ntohs(port),
856 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.src.u3),
857 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
858 ret = set_h225_addr(pskb, data, dataoff,
859 &setup->destCallSignalAddress,
860 &ct->tuplehash[!dir].tuple.src.u3,
861 ct->tuplehash[!dir].tuple.src.u.tcp.port);
862 if (ret < 0)
863 return -1;
864 }
865
866 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
867 (set_h225_addr) && ct->status & IPS_NAT_MASK &&
868 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
869 &addr, &port) &&
870 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
871 DEBUGP("nf_ct_q931: set sourceCallSignalAddress "
872 NIP6_FMT ":%hu->" NIP6_FMT ":%hu\n",
873 NIP6(*(struct in6_addr *)&addr), ntohs(port),
874 NIP6(*(struct in6_addr *)&ct->tuplehash[!dir].tuple.dst.u3),
875 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
876 ret = set_h225_addr(pskb, data, dataoff,
877 &setup->sourceCallSignalAddress,
878 &ct->tuplehash[!dir].tuple.dst.u3,
879 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
880 if (ret < 0)
881 return -1;
882 }
883
884 if (setup->options & eSetup_UUIE_fastStart) {
885 for (i = 0; i < setup->fastStart.count; i++) {
886 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
887 &setup->fastStart.item[i]);
888 if (ret < 0)
889 return -1;
890 }
891 }
892
893 return 0;
894 }
895
896 /****************************************************************************/
897 static int process_callproceeding(struct sk_buff **pskb,
898 struct nf_conn *ct,
899 enum ip_conntrack_info ctinfo,
900 unsigned char **data, int dataoff,
901 CallProceeding_UUIE *callproc)
902 {
903 int ret;
904 int i;
905
906 DEBUGP("nf_ct_q931: CallProceeding\n");
907
908 if (callproc->options & eCallProceeding_UUIE_h245Address) {
909 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
910 &callproc->h245Address);
911 if (ret < 0)
912 return -1;
913 }
914
915 if (callproc->options & eCallProceeding_UUIE_fastStart) {
916 for (i = 0; i < callproc->fastStart.count; i++) {
917 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
918 &callproc->fastStart.item[i]);
919 if (ret < 0)
920 return -1;
921 }
922 }
923
924 return 0;
925 }
926
927 /****************************************************************************/
928 static int process_connect(struct sk_buff **pskb, struct nf_conn *ct,
929 enum ip_conntrack_info ctinfo,
930 unsigned char **data, int dataoff,
931 Connect_UUIE *connect)
932 {
933 int ret;
934 int i;
935
936 DEBUGP("nf_ct_q931: Connect\n");
937
938 if (connect->options & eConnect_UUIE_h245Address) {
939 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
940 &connect->h245Address);
941 if (ret < 0)
942 return -1;
943 }
944
945 if (connect->options & eConnect_UUIE_fastStart) {
946 for (i = 0; i < connect->fastStart.count; i++) {
947 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
948 &connect->fastStart.item[i]);
949 if (ret < 0)
950 return -1;
951 }
952 }
953
954 return 0;
955 }
956
957 /****************************************************************************/
958 static int process_alerting(struct sk_buff **pskb, struct nf_conn *ct,
959 enum ip_conntrack_info ctinfo,
960 unsigned char **data, int dataoff,
961 Alerting_UUIE *alert)
962 {
963 int ret;
964 int i;
965
966 DEBUGP("nf_ct_q931: Alerting\n");
967
968 if (alert->options & eAlerting_UUIE_h245Address) {
969 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
970 &alert->h245Address);
971 if (ret < 0)
972 return -1;
973 }
974
975 if (alert->options & eAlerting_UUIE_fastStart) {
976 for (i = 0; i < alert->fastStart.count; i++) {
977 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
978 &alert->fastStart.item[i]);
979 if (ret < 0)
980 return -1;
981 }
982 }
983
984 return 0;
985 }
986
987 /****************************************************************************/
988 static int process_facility(struct sk_buff **pskb, struct nf_conn *ct,
989 enum ip_conntrack_info ctinfo,
990 unsigned char **data, int dataoff,
991 Facility_UUIE *facility)
992 {
993 int ret;
994 int i;
995
996 DEBUGP("nf_ct_q931: Facility\n");
997
998 if (facility->reason.choice == eFacilityReason_callForwarded) {
999 if (facility->options & eFacility_UUIE_alternativeAddress)
1000 return expect_callforwarding(pskb, ct, ctinfo, data,
1001 dataoff,
1002 &facility->
1003 alternativeAddress);
1004 return 0;
1005 }
1006
1007 if (facility->options & eFacility_UUIE_h245Address) {
1008 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
1009 &facility->h245Address);
1010 if (ret < 0)
1011 return -1;
1012 }
1013
1014 if (facility->options & eFacility_UUIE_fastStart) {
1015 for (i = 0; i < facility->fastStart.count; i++) {
1016 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
1017 &facility->fastStart.item[i]);
1018 if (ret < 0)
1019 return -1;
1020 }
1021 }
1022
1023 return 0;
1024 }
1025
1026 /****************************************************************************/
1027 static int process_progress(struct sk_buff **pskb, struct nf_conn *ct,
1028 enum ip_conntrack_info ctinfo,
1029 unsigned char **data, int dataoff,
1030 Progress_UUIE *progress)
1031 {
1032 int ret;
1033 int i;
1034
1035 DEBUGP("nf_ct_q931: Progress\n");
1036
1037 if (progress->options & eProgress_UUIE_h245Address) {
1038 ret = expect_h245(pskb, ct, ctinfo, data, dataoff,
1039 &progress->h245Address);
1040 if (ret < 0)
1041 return -1;
1042 }
1043
1044 if (progress->options & eProgress_UUIE_fastStart) {
1045 for (i = 0; i < progress->fastStart.count; i++) {
1046 ret = process_olc(pskb, ct, ctinfo, data, dataoff,
1047 &progress->fastStart.item[i]);
1048 if (ret < 0)
1049 return -1;
1050 }
1051 }
1052
1053 return 0;
1054 }
1055
1056 /****************************************************************************/
1057 static int process_q931(struct sk_buff **pskb, struct nf_conn *ct,
1058 enum ip_conntrack_info ctinfo,
1059 unsigned char **data, int dataoff, Q931 *q931)
1060 {
1061 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1062 int i;
1063 int ret = 0;
1064
1065 switch (pdu->h323_message_body.choice) {
1066 case eH323_UU_PDU_h323_message_body_setup:
1067 ret = process_setup(pskb, ct, ctinfo, data, dataoff,
1068 &pdu->h323_message_body.setup);
1069 break;
1070 case eH323_UU_PDU_h323_message_body_callProceeding:
1071 ret = process_callproceeding(pskb, ct, ctinfo, data, dataoff,
1072 &pdu->h323_message_body.
1073 callProceeding);
1074 break;
1075 case eH323_UU_PDU_h323_message_body_connect:
1076 ret = process_connect(pskb, ct, ctinfo, data, dataoff,
1077 &pdu->h323_message_body.connect);
1078 break;
1079 case eH323_UU_PDU_h323_message_body_alerting:
1080 ret = process_alerting(pskb, ct, ctinfo, data, dataoff,
1081 &pdu->h323_message_body.alerting);
1082 break;
1083 case eH323_UU_PDU_h323_message_body_facility:
1084 ret = process_facility(pskb, ct, ctinfo, data, dataoff,
1085 &pdu->h323_message_body.facility);
1086 break;
1087 case eH323_UU_PDU_h323_message_body_progress:
1088 ret = process_progress(pskb, ct, ctinfo, data, dataoff,
1089 &pdu->h323_message_body.progress);
1090 break;
1091 default:
1092 DEBUGP("nf_ct_q931: Q.931 signal %d\n",
1093 pdu->h323_message_body.choice);
1094 break;
1095 }
1096
1097 if (ret < 0)
1098 return -1;
1099
1100 if (pdu->options & eH323_UU_PDU_h245Control) {
1101 for (i = 0; i < pdu->h245Control.count; i++) {
1102 ret = process_h245(pskb, ct, ctinfo, data, dataoff,
1103 &pdu->h245Control.item[i]);
1104 if (ret < 0)
1105 return -1;
1106 }
1107 }
1108
1109 return 0;
1110 }
1111
1112 /****************************************************************************/
1113 static int q931_help(struct sk_buff **pskb, unsigned int protoff,
1114 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1115 {
1116 static Q931 q931;
1117 unsigned char *data = NULL;
1118 int datalen;
1119 int dataoff;
1120 int ret;
1121
1122 /* Until there's been traffic both ways, don't look in packets. */
1123 if (ctinfo != IP_CT_ESTABLISHED &&
1124 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1125 return NF_ACCEPT;
1126 }
1127 DEBUGP("nf_ct_q931: skblen = %u\n", (*pskb)->len);
1128
1129 spin_lock_bh(&nf_h323_lock);
1130
1131 /* Process each TPKT */
1132 while (get_tpkt_data(pskb, protoff, ct, ctinfo,
1133 &data, &datalen, &dataoff)) {
1134 DEBUGP("nf_ct_q931: TPKT len=%d ", datalen);
1135 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1136
1137 /* Decode Q.931 signal */
1138 ret = DecodeQ931(data, datalen, &q931);
1139 if (ret < 0) {
1140 if (net_ratelimit())
1141 printk("nf_ct_q931: decoding error: %s\n",
1142 ret == H323_ERROR_BOUND ?
1143 "out of bound" : "out of range");
1144 /* We don't drop when decoding error */
1145 break;
1146 }
1147
1148 /* Process Q.931 signal */
1149 if (process_q931(pskb, ct, ctinfo, &data, dataoff, &q931) < 0)
1150 goto drop;
1151 }
1152
1153 spin_unlock_bh(&nf_h323_lock);
1154 return NF_ACCEPT;
1155
1156 drop:
1157 spin_unlock_bh(&nf_h323_lock);
1158 if (net_ratelimit())
1159 printk("nf_ct_q931: packet dropped\n");
1160 return NF_DROP;
1161 }
1162
1163 /****************************************************************************/
1164 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1165 {
1166 .name = "Q.931",
1167 .me = THIS_MODULE,
1168 /* T.120 and H.245 */
1169 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1170 .timeout = 240,
1171 .tuple.src.l3num = AF_INET,
1172 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1173 .tuple.dst.protonum = IPPROTO_TCP,
1174 .help = q931_help
1175 },
1176 {
1177 .name = "Q.931",
1178 .me = THIS_MODULE,
1179 /* T.120 and H.245 */
1180 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1181 .timeout = 240,
1182 .tuple.src.l3num = AF_INET6,
1183 .tuple.src.u.tcp.port = __constant_htons(Q931_PORT),
1184 .tuple.dst.protonum = IPPROTO_TCP,
1185 .help = q931_help
1186 },
1187 };
1188
1189 /****************************************************************************/
1190 static unsigned char *get_udp_data(struct sk_buff **pskb, unsigned int protoff,
1191 int *datalen)
1192 {
1193 struct udphdr _uh, *uh;
1194 int dataoff;
1195
1196 uh = skb_header_pointer(*pskb, protoff, sizeof(_uh), &_uh);
1197 if (uh == NULL)
1198 return NULL;
1199 dataoff = protoff + sizeof(_uh);
1200 if (dataoff >= (*pskb)->len)
1201 return NULL;
1202 *datalen = (*pskb)->len - dataoff;
1203 return skb_header_pointer(*pskb, dataoff, *datalen, h323_buffer);
1204 }
1205
1206 /****************************************************************************/
1207 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1208 union nf_conntrack_address *addr,
1209 __be16 port)
1210 {
1211 struct nf_conntrack_expect *exp;
1212 struct nf_conntrack_tuple tuple;
1213
1214 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1215 tuple.src.u.tcp.port = 0;
1216 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1217 tuple.dst.u.tcp.port = port;
1218 tuple.dst.protonum = IPPROTO_TCP;
1219
1220 exp = __nf_ct_expect_find(&tuple);
1221 if (exp && exp->master == ct)
1222 return exp;
1223 return NULL;
1224 }
1225
1226 /****************************************************************************/
1227 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1228 unsigned timeout)
1229 {
1230 if (!exp || !del_timer(&exp->timeout))
1231 return 0;
1232
1233 exp->timeout.expires = jiffies + timeout * HZ;
1234 add_timer(&exp->timeout);
1235
1236 return 1;
1237 }
1238
1239 /****************************************************************************/
1240 static int expect_q931(struct sk_buff **pskb, struct nf_conn *ct,
1241 enum ip_conntrack_info ctinfo,
1242 unsigned char **data,
1243 TransportAddress *taddr, int count)
1244 {
1245 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1246 int dir = CTINFO2DIR(ctinfo);
1247 int ret = 0;
1248 int i;
1249 __be16 port;
1250 union nf_conntrack_address addr;
1251 struct nf_conntrack_expect *exp;
1252 typeof(nat_q931_hook) nat_q931;
1253
1254 /* Look for the first related address */
1255 for (i = 0; i < count; i++) {
1256 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1257 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1258 sizeof(addr)) == 0 && port != 0)
1259 break;
1260 }
1261
1262 if (i >= count) /* Not found */
1263 return 0;
1264
1265 /* Create expect for Q.931 */
1266 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1267 return -1;
1268 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1269 gkrouted_only ? /* only accept calls from GK? */
1270 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1271 &ct->tuplehash[!dir].tuple.dst.u3,
1272 IPPROTO_TCP, NULL, &port);
1273 exp->helper = nf_conntrack_helper_q931;
1274 exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */
1275
1276 nat_q931 = rcu_dereference(nat_q931_hook);
1277 if (nat_q931 && ct->status & IPS_NAT_MASK) { /* Need NAT */
1278 ret = nat_q931(pskb, ct, ctinfo, data, taddr, i, port, exp);
1279 } else { /* Conntrack only */
1280 if (nf_ct_expect_related(exp) == 0) {
1281 DEBUGP("nf_ct_ras: expect Q.931 ");
1282 NF_CT_DUMP_TUPLE(&exp->tuple);
1283
1284 /* Save port for looking up expect in processing RCF */
1285 info->sig_port[dir] = port;
1286 } else
1287 ret = -1;
1288 }
1289
1290 nf_ct_expect_put(exp);
1291
1292 return ret;
1293 }
1294
1295 /****************************************************************************/
1296 static int process_grq(struct sk_buff **pskb, struct nf_conn *ct,
1297 enum ip_conntrack_info ctinfo,
1298 unsigned char **data, GatekeeperRequest *grq)
1299 {
1300 typeof(set_ras_addr_hook) set_ras_addr;
1301
1302 DEBUGP("nf_ct_ras: GRQ\n");
1303
1304 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1305 if (set_ras_addr && ct->status & IPS_NAT_MASK) /* NATed */
1306 return set_ras_addr(pskb, ct, ctinfo, data,
1307 &grq->rasAddress, 1);
1308 return 0;
1309 }
1310
1311 /****************************************************************************/
1312 static int process_gcf(struct sk_buff **pskb, struct nf_conn *ct,
1313 enum ip_conntrack_info ctinfo,
1314 unsigned char **data, GatekeeperConfirm *gcf)
1315 {
1316 int dir = CTINFO2DIR(ctinfo);
1317 int ret = 0;
1318 __be16 port;
1319 union nf_conntrack_address addr;
1320 struct nf_conntrack_expect *exp;
1321
1322 DEBUGP("nf_ct_ras: GCF\n");
1323
1324 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1325 return 0;
1326
1327 /* Registration port is the same as discovery port */
1328 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1329 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1330 return 0;
1331
1332 /* Avoid RAS expectation loops. A GCF is never expected. */
1333 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1334 return 0;
1335
1336 /* Need new expect */
1337 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1338 return -1;
1339 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1340 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1341 IPPROTO_UDP, NULL, &port);
1342 exp->helper = nf_conntrack_helper_ras;
1343
1344 if (nf_ct_expect_related(exp) == 0) {
1345 DEBUGP("nf_ct_ras: expect RAS ");
1346 NF_CT_DUMP_TUPLE(&exp->tuple);
1347 } else
1348 ret = -1;
1349
1350 nf_ct_expect_put(exp);
1351
1352 return ret;
1353 }
1354
1355 /****************************************************************************/
1356 static int process_rrq(struct sk_buff **pskb, struct nf_conn *ct,
1357 enum ip_conntrack_info ctinfo,
1358 unsigned char **data, RegistrationRequest *rrq)
1359 {
1360 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1361 int ret;
1362 typeof(set_ras_addr_hook) set_ras_addr;
1363
1364 DEBUGP("nf_ct_ras: RRQ\n");
1365
1366 ret = expect_q931(pskb, ct, ctinfo, data,
1367 rrq->callSignalAddress.item,
1368 rrq->callSignalAddress.count);
1369 if (ret < 0)
1370 return -1;
1371
1372 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1373 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1374 ret = set_ras_addr(pskb, ct, ctinfo, data,
1375 rrq->rasAddress.item,
1376 rrq->rasAddress.count);
1377 if (ret < 0)
1378 return -1;
1379 }
1380
1381 if (rrq->options & eRegistrationRequest_timeToLive) {
1382 DEBUGP("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1383 info->timeout = rrq->timeToLive;
1384 } else
1385 info->timeout = default_rrq_ttl;
1386
1387 return 0;
1388 }
1389
1390 /****************************************************************************/
1391 static int process_rcf(struct sk_buff **pskb, struct nf_conn *ct,
1392 enum ip_conntrack_info ctinfo,
1393 unsigned char **data, RegistrationConfirm *rcf)
1394 {
1395 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1396 int dir = CTINFO2DIR(ctinfo);
1397 int ret;
1398 struct nf_conntrack_expect *exp;
1399 typeof(set_sig_addr_hook) set_sig_addr;
1400
1401 DEBUGP("nf_ct_ras: RCF\n");
1402
1403 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1404 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1405 ret = set_sig_addr(pskb, ct, ctinfo, data,
1406 rcf->callSignalAddress.item,
1407 rcf->callSignalAddress.count);
1408 if (ret < 0)
1409 return -1;
1410 }
1411
1412 if (rcf->options & eRegistrationConfirm_timeToLive) {
1413 DEBUGP("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1414 info->timeout = rcf->timeToLive;
1415 }
1416
1417 if (info->timeout > 0) {
1418 DEBUGP
1419 ("nf_ct_ras: set RAS connection timeout to %u seconds\n",
1420 info->timeout);
1421 nf_ct_refresh(ct, *pskb, info->timeout * HZ);
1422
1423 /* Set expect timeout */
1424 read_lock_bh(&nf_conntrack_lock);
1425 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1426 info->sig_port[!dir]);
1427 if (exp) {
1428 DEBUGP("nf_ct_ras: set Q.931 expect "
1429 "timeout to %u seconds for",
1430 info->timeout);
1431 NF_CT_DUMP_TUPLE(&exp->tuple);
1432 set_expect_timeout(exp, info->timeout);
1433 }
1434 read_unlock_bh(&nf_conntrack_lock);
1435 }
1436
1437 return 0;
1438 }
1439
1440 /****************************************************************************/
1441 static int process_urq(struct sk_buff **pskb, struct nf_conn *ct,
1442 enum ip_conntrack_info ctinfo,
1443 unsigned char **data, UnregistrationRequest *urq)
1444 {
1445 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1446 int dir = CTINFO2DIR(ctinfo);
1447 int ret;
1448 typeof(set_sig_addr_hook) set_sig_addr;
1449
1450 DEBUGP("nf_ct_ras: URQ\n");
1451
1452 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1453 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1454 ret = set_sig_addr(pskb, ct, ctinfo, data,
1455 urq->callSignalAddress.item,
1456 urq->callSignalAddress.count);
1457 if (ret < 0)
1458 return -1;
1459 }
1460
1461 /* Clear old expect */
1462 nf_ct_remove_expectations(ct);
1463 info->sig_port[dir] = 0;
1464 info->sig_port[!dir] = 0;
1465
1466 /* Give it 30 seconds for UCF or URJ */
1467 nf_ct_refresh(ct, *pskb, 30 * HZ);
1468
1469 return 0;
1470 }
1471
1472 /****************************************************************************/
1473 static int process_arq(struct sk_buff **pskb, struct nf_conn *ct,
1474 enum ip_conntrack_info ctinfo,
1475 unsigned char **data, AdmissionRequest *arq)
1476 {
1477 struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info;
1478 int dir = CTINFO2DIR(ctinfo);
1479 __be16 port;
1480 union nf_conntrack_address addr;
1481 typeof(set_h225_addr_hook) set_h225_addr;
1482
1483 DEBUGP("nf_ct_ras: ARQ\n");
1484
1485 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1486 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1487 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1488 &addr, &port) &&
1489 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1490 port == info->sig_port[dir] &&
1491 set_h225_addr && ct->status & IPS_NAT_MASK) {
1492 /* Answering ARQ */
1493 return set_h225_addr(pskb, data, 0,
1494 &arq->destCallSignalAddress,
1495 &ct->tuplehash[!dir].tuple.dst.u3,
1496 info->sig_port[!dir]);
1497 }
1498
1499 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1500 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1501 &addr, &port) &&
1502 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1503 set_h225_addr && ct->status & IPS_NAT_MASK) {
1504 /* Calling ARQ */
1505 return set_h225_addr(pskb, data, 0,
1506 &arq->srcCallSignalAddress,
1507 &ct->tuplehash[!dir].tuple.dst.u3,
1508 port);
1509 }
1510
1511 return 0;
1512 }
1513
1514 /****************************************************************************/
1515 static int process_acf(struct sk_buff **pskb, struct nf_conn *ct,
1516 enum ip_conntrack_info ctinfo,
1517 unsigned char **data, AdmissionConfirm *acf)
1518 {
1519 int dir = CTINFO2DIR(ctinfo);
1520 int ret = 0;
1521 __be16 port;
1522 union nf_conntrack_address addr;
1523 struct nf_conntrack_expect *exp;
1524 typeof(set_sig_addr_hook) set_sig_addr;
1525
1526 DEBUGP("nf_ct_ras: ACF\n");
1527
1528 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1529 &addr, &port))
1530 return 0;
1531
1532 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1533 /* Answering ACF */
1534 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1535 if (set_sig_addr && ct->status & IPS_NAT_MASK)
1536 return set_sig_addr(pskb, ct, ctinfo, data,
1537 &acf->destCallSignalAddress, 1);
1538 return 0;
1539 }
1540
1541 /* Need new expect */
1542 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1543 return -1;
1544 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1545 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1546 IPPROTO_TCP, NULL, &port);
1547 exp->flags = NF_CT_EXPECT_PERMANENT;
1548 exp->helper = nf_conntrack_helper_q931;
1549
1550 if (nf_ct_expect_related(exp) == 0) {
1551 DEBUGP("nf_ct_ras: expect Q.931 ");
1552 NF_CT_DUMP_TUPLE(&exp->tuple);
1553 } else
1554 ret = -1;
1555
1556 nf_ct_expect_put(exp);
1557
1558 return ret;
1559 }
1560
1561 /****************************************************************************/
1562 static int process_lrq(struct sk_buff **pskb, struct nf_conn *ct,
1563 enum ip_conntrack_info ctinfo,
1564 unsigned char **data, LocationRequest *lrq)
1565 {
1566 typeof(set_ras_addr_hook) set_ras_addr;
1567
1568 DEBUGP("nf_ct_ras: LRQ\n");
1569
1570 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1571 if (set_ras_addr && ct->status & IPS_NAT_MASK)
1572 return set_ras_addr(pskb, ct, ctinfo, data,
1573 &lrq->replyAddress, 1);
1574 return 0;
1575 }
1576
1577 /****************************************************************************/
1578 static int process_lcf(struct sk_buff **pskb, struct nf_conn *ct,
1579 enum ip_conntrack_info ctinfo,
1580 unsigned char **data, LocationConfirm *lcf)
1581 {
1582 int dir = CTINFO2DIR(ctinfo);
1583 int ret = 0;
1584 __be16 port;
1585 union nf_conntrack_address addr;
1586 struct nf_conntrack_expect *exp;
1587
1588 DEBUGP("nf_ct_ras: LCF\n");
1589
1590 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1591 &addr, &port))
1592 return 0;
1593
1594 /* Need new expect for call signal */
1595 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1596 return -1;
1597 nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1598 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1599 IPPROTO_TCP, NULL, &port);
1600 exp->flags = NF_CT_EXPECT_PERMANENT;
1601 exp->helper = nf_conntrack_helper_q931;
1602
1603 if (nf_ct_expect_related(exp) == 0) {
1604 DEBUGP("nf_ct_ras: expect Q.931 ");
1605 NF_CT_DUMP_TUPLE(&exp->tuple);
1606 } else
1607 ret = -1;
1608
1609 nf_ct_expect_put(exp);
1610
1611 /* Ignore rasAddress */
1612
1613 return ret;
1614 }
1615
1616 /****************************************************************************/
1617 static int process_irr(struct sk_buff **pskb, struct nf_conn *ct,
1618 enum ip_conntrack_info ctinfo,
1619 unsigned char **data, InfoRequestResponse *irr)
1620 {
1621 int ret;
1622 typeof(set_ras_addr_hook) set_ras_addr;
1623 typeof(set_sig_addr_hook) set_sig_addr;
1624
1625 DEBUGP("nf_ct_ras: IRR\n");
1626
1627 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1628 if (set_ras_addr && ct->status & IPS_NAT_MASK) {
1629 ret = set_ras_addr(pskb, ct, ctinfo, data,
1630 &irr->rasAddress, 1);
1631 if (ret < 0)
1632 return -1;
1633 }
1634
1635 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1636 if (set_sig_addr && ct->status & IPS_NAT_MASK) {
1637 ret = set_sig_addr(pskb, ct, ctinfo, data,
1638 irr->callSignalAddress.item,
1639 irr->callSignalAddress.count);
1640 if (ret < 0)
1641 return -1;
1642 }
1643
1644 return 0;
1645 }
1646
1647 /****************************************************************************/
1648 static int process_ras(struct sk_buff **pskb, struct nf_conn *ct,
1649 enum ip_conntrack_info ctinfo,
1650 unsigned char **data, RasMessage *ras)
1651 {
1652 switch (ras->choice) {
1653 case eRasMessage_gatekeeperRequest:
1654 return process_grq(pskb, ct, ctinfo, data,
1655 &ras->gatekeeperRequest);
1656 case eRasMessage_gatekeeperConfirm:
1657 return process_gcf(pskb, ct, ctinfo, data,
1658 &ras->gatekeeperConfirm);
1659 case eRasMessage_registrationRequest:
1660 return process_rrq(pskb, ct, ctinfo, data,
1661 &ras->registrationRequest);
1662 case eRasMessage_registrationConfirm:
1663 return process_rcf(pskb, ct, ctinfo, data,
1664 &ras->registrationConfirm);
1665 case eRasMessage_unregistrationRequest:
1666 return process_urq(pskb, ct, ctinfo, data,
1667 &ras->unregistrationRequest);
1668 case eRasMessage_admissionRequest:
1669 return process_arq(pskb, ct, ctinfo, data,
1670 &ras->admissionRequest);
1671 case eRasMessage_admissionConfirm:
1672 return process_acf(pskb, ct, ctinfo, data,
1673 &ras->admissionConfirm);
1674 case eRasMessage_locationRequest:
1675 return process_lrq(pskb, ct, ctinfo, data,
1676 &ras->locationRequest);
1677 case eRasMessage_locationConfirm:
1678 return process_lcf(pskb, ct, ctinfo, data,
1679 &ras->locationConfirm);
1680 case eRasMessage_infoRequestResponse:
1681 return process_irr(pskb, ct, ctinfo, data,
1682 &ras->infoRequestResponse);
1683 default:
1684 DEBUGP("nf_ct_ras: RAS message %d\n", ras->choice);
1685 break;
1686 }
1687
1688 return 0;
1689 }
1690
1691 /****************************************************************************/
1692 static int ras_help(struct sk_buff **pskb, unsigned int protoff,
1693 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1694 {
1695 static RasMessage ras;
1696 unsigned char *data;
1697 int datalen = 0;
1698 int ret;
1699
1700 DEBUGP("nf_ct_ras: skblen = %u\n", (*pskb)->len);
1701
1702 spin_lock_bh(&nf_h323_lock);
1703
1704 /* Get UDP data */
1705 data = get_udp_data(pskb, protoff, &datalen);
1706 if (data == NULL)
1707 goto accept;
1708 DEBUGP("nf_ct_ras: RAS message len=%d ", datalen);
1709 NF_CT_DUMP_TUPLE(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1710
1711 /* Decode RAS message */
1712 ret = DecodeRasMessage(data, datalen, &ras);
1713 if (ret < 0) {
1714 if (net_ratelimit())
1715 printk("nf_ct_ras: decoding error: %s\n",
1716 ret == H323_ERROR_BOUND ?
1717 "out of bound" : "out of range");
1718 goto accept;
1719 }
1720
1721 /* Process RAS message */
1722 if (process_ras(pskb, ct, ctinfo, &data, &ras) < 0)
1723 goto drop;
1724
1725 accept:
1726 spin_unlock_bh(&nf_h323_lock);
1727 return NF_ACCEPT;
1728
1729 drop:
1730 spin_unlock_bh(&nf_h323_lock);
1731 if (net_ratelimit())
1732 printk("nf_ct_ras: packet dropped\n");
1733 return NF_DROP;
1734 }
1735
1736 /****************************************************************************/
1737 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1738 {
1739 .name = "RAS",
1740 .me = THIS_MODULE,
1741 .max_expected = 32,
1742 .timeout = 240,
1743 .tuple.src.l3num = AF_INET,
1744 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1745 .tuple.dst.protonum = IPPROTO_UDP,
1746 .help = ras_help,
1747 },
1748 {
1749 .name = "RAS",
1750 .me = THIS_MODULE,
1751 .max_expected = 32,
1752 .timeout = 240,
1753 .tuple.src.l3num = AF_INET6,
1754 .tuple.src.u.udp.port = __constant_htons(RAS_PORT),
1755 .tuple.dst.protonum = IPPROTO_UDP,
1756 .help = ras_help,
1757 },
1758 };
1759
1760 /****************************************************************************/
1761 static void __exit nf_conntrack_h323_fini(void)
1762 {
1763 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1764 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1765 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1766 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1767 kfree(h323_buffer);
1768 DEBUGP("nf_ct_h323: fini\n");
1769 }
1770
1771 /****************************************************************************/
1772 static int __init nf_conntrack_h323_init(void)
1773 {
1774 int ret;
1775
1776 h323_buffer = kmalloc(65536, GFP_KERNEL);
1777 if (!h323_buffer)
1778 return -ENOMEM;
1779 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1780 if (ret < 0)
1781 goto err1;
1782 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1783 if (ret < 0)
1784 goto err2;
1785 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1786 if (ret < 0)
1787 goto err3;
1788 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1789 if (ret < 0)
1790 goto err4;
1791 DEBUGP("nf_ct_h323: init success\n");
1792 return 0;
1793
1794 err4:
1795 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1796 err3:
1797 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1798 err2:
1799 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1800 err1:
1801 return ret;
1802 }
1803
1804 /****************************************************************************/
1805 module_init(nf_conntrack_h323_init);
1806 module_exit(nf_conntrack_h323_fini);
1807
1808 EXPORT_SYMBOL_GPL(get_h225_addr);
1809 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1810 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1811 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1812 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1813 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1814 EXPORT_SYMBOL_GPL(nat_t120_hook);
1815 EXPORT_SYMBOL_GPL(nat_h245_hook);
1816 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1817 EXPORT_SYMBOL_GPL(nat_q931_hook);
1818
1819 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1820 MODULE_DESCRIPTION("H.323 connection tracking helper");
1821 MODULE_LICENSE("GPL");
1822 MODULE_ALIAS("ip_conntrack_h323");
This page took 0.068967 seconds and 5 git commands to generate.