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