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