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(struct net *net,
732 const union nf_inet_addr *src,
733 const union nf_inet_addr *dst,
734 u_int8_t family)
735{
736 const struct nf_afinfo *afinfo;
737 int ret = 0;
738
739
740 afinfo = nf_get_afinfo(family);
741 if (!afinfo)
742 return 0;
743
744 switch (family) {
745 case AF_INET: {
746 struct flowi4 fl1, fl2;
747 struct rtable *rt1, *rt2;
748
749 memset(&fl1, 0, sizeof(fl1));
750 fl1.daddr = src->ip;
751
752 memset(&fl2, 0, sizeof(fl2));
753 fl2.daddr = dst->ip;
754 if (!afinfo->route(net, (struct dst_entry **)&rt1,
755 flowi4_to_flowi(&fl1), false)) {
756 if (!afinfo->route(net, (struct dst_entry **)&rt2,
757 flowi4_to_flowi(&fl2), false)) {
758 if (rt_nexthop(rt1, fl1.daddr) ==
759 rt_nexthop(rt2, fl2.daddr) &&
760 rt1->dst.dev == rt2->dst.dev)
761 ret = 1;
762 dst_release(&rt2->dst);
763 }
764 dst_release(&rt1->dst);
765 }
766 break;
767 }
768#if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV6)
769 case AF_INET6: {
770 struct flowi6 fl1, fl2;
771 struct rt6_info *rt1, *rt2;
772
773 memset(&fl1, 0, sizeof(fl1));
774 fl1.daddr = src->in6;
775
776 memset(&fl2, 0, sizeof(fl2));
777 fl2.daddr = dst->in6;
778 if (!afinfo->route(net, (struct dst_entry **)&rt1,
779 flowi6_to_flowi(&fl1), false)) {
780 if (!afinfo->route(net, (struct dst_entry **)&rt2,
781 flowi6_to_flowi(&fl2), false)) {
782 if (ipv6_addr_equal(rt6_nexthop(rt1, &fl1.daddr),
783 rt6_nexthop(rt2, &fl2.daddr)) &&
784 rt1->dst.dev == rt2->dst.dev)
785 ret = 1;
786 dst_release(&rt2->dst);
787 }
788 dst_release(&rt1->dst);
789 }
790 break;
791 }
792#endif
793 }
794 return ret;
795
796}
797
798
799static int expect_callforwarding(struct sk_buff *skb,
800 struct nf_conn *ct,
801 enum ip_conntrack_info ctinfo,
802 unsigned int protoff,
803 unsigned char **data, int dataoff,
804 TransportAddress *taddr)
805{
806 int dir = CTINFO2DIR(ctinfo);
807 int ret = 0;
808 __be16 port;
809 union nf_inet_addr addr;
810 struct nf_conntrack_expect *exp;
811 struct net *net = nf_ct_net(ct);
812 typeof(nat_callforwarding_hook) nat_callforwarding;
813
814
815 if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
816 return 0;
817
818
819
820 if (callforward_filter &&
821 callforward_do_filter(net, &addr, &ct->tuplehash[!dir].tuple.src.u3,
822 nf_ct_l3num(ct))) {
823 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
824 return 0;
825 }
826
827
828 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
829 return -1;
830 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
831 &ct->tuplehash[!dir].tuple.src.u3, &addr,
832 IPPROTO_TCP, NULL, &port);
833 exp->helper = nf_conntrack_helper_q931;
834
835 if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
836 &ct->tuplehash[!dir].tuple.dst.u3,
837 sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
838 (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
839 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
840 ct->status & IPS_NAT_MASK) {
841
842 ret = nat_callforwarding(skb, ct, ctinfo,
843 protoff, data, dataoff,
844 taddr, port, exp);
845 } else {
846 if (nf_ct_expect_related(exp) == 0) {
847 pr_debug("nf_ct_q931: expect Call Forwarding ");
848 nf_ct_dump_tuple(&exp->tuple);
849 } else
850 ret = -1;
851 }
852
853 nf_ct_expect_put(exp);
854
855 return ret;
856}
857
858
859static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
860 enum ip_conntrack_info ctinfo,
861 unsigned int protoff,
862 unsigned char **data, int dataoff,
863 Setup_UUIE *setup)
864{
865 int dir = CTINFO2DIR(ctinfo);
866 int ret;
867 int i;
868 __be16 port;
869 union nf_inet_addr addr;
870 typeof(set_h225_addr_hook) set_h225_addr;
871
872 pr_debug("nf_ct_q931: Setup\n");
873
874 if (setup->options & eSetup_UUIE_h245Address) {
875 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
876 &setup->h245Address);
877 if (ret < 0)
878 return -1;
879 }
880
881 set_h225_addr = rcu_dereference(set_h225_addr_hook);
882 if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
883 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
884 ct->status & IPS_NAT_MASK &&
885 get_h225_addr(ct, *data, &setup->destCallSignalAddress,
886 &addr, &port) &&
887 memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
888 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
889 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3,
890 ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
891 ret = set_h225_addr(skb, protoff, data, dataoff,
892 &setup->destCallSignalAddress,
893 &ct->tuplehash[!dir].tuple.src.u3,
894 ct->tuplehash[!dir].tuple.src.u.tcp.port);
895 if (ret < 0)
896 return -1;
897 }
898
899 if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
900 (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
901 ct->status & IPS_NAT_MASK &&
902 get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
903 &addr, &port) &&
904 memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
905 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
906 &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3,
907 ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
908 ret = set_h225_addr(skb, protoff, data, dataoff,
909 &setup->sourceCallSignalAddress,
910 &ct->tuplehash[!dir].tuple.dst.u3,
911 ct->tuplehash[!dir].tuple.dst.u.tcp.port);
912 if (ret < 0)
913 return -1;
914 }
915
916 if (setup->options & eSetup_UUIE_fastStart) {
917 for (i = 0; i < setup->fastStart.count; i++) {
918 ret = process_olc(skb, ct, ctinfo,
919 protoff, data, dataoff,
920 &setup->fastStart.item[i]);
921 if (ret < 0)
922 return -1;
923 }
924 }
925
926 return 0;
927}
928
929
930static int process_callproceeding(struct sk_buff *skb,
931 struct nf_conn *ct,
932 enum ip_conntrack_info ctinfo,
933 unsigned int protoff,
934 unsigned char **data, int dataoff,
935 CallProceeding_UUIE *callproc)
936{
937 int ret;
938 int i;
939
940 pr_debug("nf_ct_q931: CallProceeding\n");
941
942 if (callproc->options & eCallProceeding_UUIE_h245Address) {
943 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
944 &callproc->h245Address);
945 if (ret < 0)
946 return -1;
947 }
948
949 if (callproc->options & eCallProceeding_UUIE_fastStart) {
950 for (i = 0; i < callproc->fastStart.count; i++) {
951 ret = process_olc(skb, ct, ctinfo,
952 protoff, data, dataoff,
953 &callproc->fastStart.item[i]);
954 if (ret < 0)
955 return -1;
956 }
957 }
958
959 return 0;
960}
961
962
963static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
964 enum ip_conntrack_info ctinfo,
965 unsigned int protoff,
966 unsigned char **data, int dataoff,
967 Connect_UUIE *connect)
968{
969 int ret;
970 int i;
971
972 pr_debug("nf_ct_q931: Connect\n");
973
974 if (connect->options & eConnect_UUIE_h245Address) {
975 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
976 &connect->h245Address);
977 if (ret < 0)
978 return -1;
979 }
980
981 if (connect->options & eConnect_UUIE_fastStart) {
982 for (i = 0; i < connect->fastStart.count; i++) {
983 ret = process_olc(skb, ct, ctinfo,
984 protoff, data, dataoff,
985 &connect->fastStart.item[i]);
986 if (ret < 0)
987 return -1;
988 }
989 }
990
991 return 0;
992}
993
994
995static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
996 enum ip_conntrack_info ctinfo,
997 unsigned int protoff,
998 unsigned char **data, int dataoff,
999 Alerting_UUIE *alert)
1000{
1001 int ret;
1002 int i;
1003
1004 pr_debug("nf_ct_q931: Alerting\n");
1005
1006 if (alert->options & eAlerting_UUIE_h245Address) {
1007 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1008 &alert->h245Address);
1009 if (ret < 0)
1010 return -1;
1011 }
1012
1013 if (alert->options & eAlerting_UUIE_fastStart) {
1014 for (i = 0; i < alert->fastStart.count; i++) {
1015 ret = process_olc(skb, ct, ctinfo,
1016 protoff, data, dataoff,
1017 &alert->fastStart.item[i]);
1018 if (ret < 0)
1019 return -1;
1020 }
1021 }
1022
1023 return 0;
1024}
1025
1026
1027static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
1028 enum ip_conntrack_info ctinfo,
1029 unsigned int protoff,
1030 unsigned char **data, int dataoff,
1031 Facility_UUIE *facility)
1032{
1033 int ret;
1034 int i;
1035
1036 pr_debug("nf_ct_q931: Facility\n");
1037
1038 if (facility->reason.choice == eFacilityReason_callForwarded) {
1039 if (facility->options & eFacility_UUIE_alternativeAddress)
1040 return expect_callforwarding(skb, ct, ctinfo,
1041 protoff, data, dataoff,
1042 &facility->
1043 alternativeAddress);
1044 return 0;
1045 }
1046
1047 if (facility->options & eFacility_UUIE_h245Address) {
1048 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1049 &facility->h245Address);
1050 if (ret < 0)
1051 return -1;
1052 }
1053
1054 if (facility->options & eFacility_UUIE_fastStart) {
1055 for (i = 0; i < facility->fastStart.count; i++) {
1056 ret = process_olc(skb, ct, ctinfo,
1057 protoff, data, dataoff,
1058 &facility->fastStart.item[i]);
1059 if (ret < 0)
1060 return -1;
1061 }
1062 }
1063
1064 return 0;
1065}
1066
1067
1068static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1069 enum ip_conntrack_info ctinfo,
1070 unsigned int protoff,
1071 unsigned char **data, int dataoff,
1072 Progress_UUIE *progress)
1073{
1074 int ret;
1075 int i;
1076
1077 pr_debug("nf_ct_q931: Progress\n");
1078
1079 if (progress->options & eProgress_UUIE_h245Address) {
1080 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1081 &progress->h245Address);
1082 if (ret < 0)
1083 return -1;
1084 }
1085
1086 if (progress->options & eProgress_UUIE_fastStart) {
1087 for (i = 0; i < progress->fastStart.count; i++) {
1088 ret = process_olc(skb, ct, ctinfo,
1089 protoff, data, dataoff,
1090 &progress->fastStart.item[i]);
1091 if (ret < 0)
1092 return -1;
1093 }
1094 }
1095
1096 return 0;
1097}
1098
1099
1100static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1101 enum ip_conntrack_info ctinfo,
1102 unsigned int protoff, unsigned char **data, int dataoff,
1103 Q931 *q931)
1104{
1105 H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1106 int i;
1107 int ret = 0;
1108
1109 switch (pdu->h323_message_body.choice) {
1110 case eH323_UU_PDU_h323_message_body_setup:
1111 ret = process_setup(skb, ct, ctinfo, protoff, data, dataoff,
1112 &pdu->h323_message_body.setup);
1113 break;
1114 case eH323_UU_PDU_h323_message_body_callProceeding:
1115 ret = process_callproceeding(skb, ct, ctinfo,
1116 protoff, data, dataoff,
1117 &pdu->h323_message_body.
1118 callProceeding);
1119 break;
1120 case eH323_UU_PDU_h323_message_body_connect:
1121 ret = process_connect(skb, ct, ctinfo, protoff, data, dataoff,
1122 &pdu->h323_message_body.connect);
1123 break;
1124 case eH323_UU_PDU_h323_message_body_alerting:
1125 ret = process_alerting(skb, ct, ctinfo, protoff, data, dataoff,
1126 &pdu->h323_message_body.alerting);
1127 break;
1128 case eH323_UU_PDU_h323_message_body_facility:
1129 ret = process_facility(skb, ct, ctinfo, protoff, data, dataoff,
1130 &pdu->h323_message_body.facility);
1131 break;
1132 case eH323_UU_PDU_h323_message_body_progress:
1133 ret = process_progress(skb, ct, ctinfo, protoff, data, dataoff,
1134 &pdu->h323_message_body.progress);
1135 break;
1136 default:
1137 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1138 pdu->h323_message_body.choice);
1139 break;
1140 }
1141
1142 if (ret < 0)
1143 return -1;
1144
1145 if (pdu->options & eH323_UU_PDU_h245Control) {
1146 for (i = 0; i < pdu->h245Control.count; i++) {
1147 ret = process_h245(skb, ct, ctinfo,
1148 protoff, data, dataoff,
1149 &pdu->h245Control.item[i]);
1150 if (ret < 0)
1151 return -1;
1152 }
1153 }
1154
1155 return 0;
1156}
1157
1158
1159static int q931_help(struct sk_buff *skb, unsigned int protoff,
1160 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1161{
1162 static Q931 q931;
1163 unsigned char *data = NULL;
1164 int datalen;
1165 int dataoff;
1166 int ret;
1167
1168
1169 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
1170 return NF_ACCEPT;
1171
1172 pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1173
1174 spin_lock_bh(&nf_h323_lock);
1175
1176
1177 while (get_tpkt_data(skb, protoff, ct, ctinfo,
1178 &data, &datalen, &dataoff)) {
1179 pr_debug("nf_ct_q931: TPKT len=%d ", datalen);
1180 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1181
1182
1183 ret = DecodeQ931(data, datalen, &q931);
1184 if (ret < 0) {
1185 pr_debug("nf_ct_q931: decoding error: %s\n",
1186 ret == H323_ERROR_BOUND ?
1187 "out of bound" : "out of range");
1188
1189 break;
1190 }
1191
1192
1193 if (process_q931(skb, ct, ctinfo, protoff,
1194 &data, dataoff, &q931) < 0)
1195 goto drop;
1196 }
1197
1198 spin_unlock_bh(&nf_h323_lock);
1199 return NF_ACCEPT;
1200
1201 drop:
1202 spin_unlock_bh(&nf_h323_lock);
1203 nf_ct_helper_log(skb, ct, "cannot process Q.931 message");
1204 return NF_DROP;
1205}
1206
1207
1208static const struct nf_conntrack_expect_policy q931_exp_policy = {
1209
1210 .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4,
1211 .timeout = 240,
1212};
1213
1214static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1215 {
1216 .name = "Q.931",
1217 .me = THIS_MODULE,
1218 .data_len = sizeof(struct nf_ct_h323_master),
1219 .tuple.src.l3num = AF_INET,
1220 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1221 .tuple.dst.protonum = IPPROTO_TCP,
1222 .help = q931_help,
1223 .expect_policy = &q931_exp_policy,
1224 },
1225 {
1226 .name = "Q.931",
1227 .me = THIS_MODULE,
1228 .tuple.src.l3num = AF_INET6,
1229 .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT),
1230 .tuple.dst.protonum = IPPROTO_TCP,
1231 .help = q931_help,
1232 .expect_policy = &q931_exp_policy,
1233 },
1234};
1235
1236
1237static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1238 int *datalen)
1239{
1240 const struct udphdr *uh;
1241 struct udphdr _uh;
1242 int dataoff;
1243
1244 uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1245 if (uh == NULL)
1246 return NULL;
1247 dataoff = protoff + sizeof(_uh);
1248 if (dataoff >= skb->len)
1249 return NULL;
1250 *datalen = skb->len - dataoff;
1251 return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1252}
1253
1254
1255static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1256 union nf_inet_addr *addr,
1257 __be16 port)
1258{
1259 struct net *net = nf_ct_net(ct);
1260 struct nf_conntrack_expect *exp;
1261 struct nf_conntrack_tuple tuple;
1262
1263 memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1264 tuple.src.u.tcp.port = 0;
1265 memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1266 tuple.dst.u.tcp.port = port;
1267 tuple.dst.protonum = IPPROTO_TCP;
1268
1269 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1270 if (exp && exp->master == ct)
1271 return exp;
1272 return NULL;
1273}
1274
1275
1276static int set_expect_timeout(struct nf_conntrack_expect *exp,
1277 unsigned int timeout)
1278{
1279 if (!exp || !del_timer(&exp->timeout))
1280 return 0;
1281
1282 exp->timeout.expires = jiffies + timeout * HZ;
1283 add_timer(&exp->timeout);
1284
1285 return 1;
1286}
1287
1288
1289static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1290 enum ip_conntrack_info ctinfo,
1291 unsigned int protoff, unsigned char **data,
1292 TransportAddress *taddr, int count)
1293{
1294 struct nf_ct_h323_master *info = nfct_help_data(ct);
1295 int dir = CTINFO2DIR(ctinfo);
1296 int ret = 0;
1297 int i;
1298 __be16 port;
1299 union nf_inet_addr addr;
1300 struct nf_conntrack_expect *exp;
1301 typeof(nat_q931_hook) nat_q931;
1302
1303
1304 for (i = 0; i < count; i++) {
1305 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1306 memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1307 sizeof(addr)) == 0 && port != 0)
1308 break;
1309 }
1310
1311 if (i >= count)
1312 return 0;
1313
1314
1315 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1316 return -1;
1317 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1318 gkrouted_only ?
1319 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1320 &ct->tuplehash[!dir].tuple.dst.u3,
1321 IPPROTO_TCP, NULL, &port);
1322 exp->helper = nf_conntrack_helper_q931;
1323 exp->flags = NF_CT_EXPECT_PERMANENT;
1324
1325 nat_q931 = rcu_dereference(nat_q931_hook);
1326 if (nat_q931 && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1327 ct->status & IPS_NAT_MASK) {
1328 ret = nat_q931(skb, ct, ctinfo, protoff, data,
1329 taddr, i, port, exp);
1330 } else {
1331 if (nf_ct_expect_related(exp) == 0) {
1332 pr_debug("nf_ct_ras: expect Q.931 ");
1333 nf_ct_dump_tuple(&exp->tuple);
1334
1335
1336 info->sig_port[dir] = port;
1337 } else
1338 ret = -1;
1339 }
1340
1341 nf_ct_expect_put(exp);
1342
1343 return ret;
1344}
1345
1346
1347static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1348 enum ip_conntrack_info ctinfo,
1349 unsigned int protoff,
1350 unsigned char **data, GatekeeperRequest *grq)
1351{
1352 typeof(set_ras_addr_hook) set_ras_addr;
1353
1354 pr_debug("nf_ct_ras: GRQ\n");
1355
1356 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1357 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1358 ct->status & IPS_NAT_MASK)
1359 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1360 &grq->rasAddress, 1);
1361 return 0;
1362}
1363
1364
1365static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1366 enum ip_conntrack_info ctinfo,
1367 unsigned int protoff,
1368 unsigned char **data, GatekeeperConfirm *gcf)
1369{
1370 int dir = CTINFO2DIR(ctinfo);
1371 int ret = 0;
1372 __be16 port;
1373 union nf_inet_addr addr;
1374 struct nf_conntrack_expect *exp;
1375
1376 pr_debug("nf_ct_ras: GCF\n");
1377
1378 if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1379 return 0;
1380
1381
1382 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1383 port == ct->tuplehash[dir].tuple.src.u.udp.port)
1384 return 0;
1385
1386
1387 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1388 return 0;
1389
1390
1391 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1392 return -1;
1393 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1394 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1395 IPPROTO_UDP, NULL, &port);
1396 exp->helper = nf_conntrack_helper_ras;
1397
1398 if (nf_ct_expect_related(exp) == 0) {
1399 pr_debug("nf_ct_ras: expect RAS ");
1400 nf_ct_dump_tuple(&exp->tuple);
1401 } else
1402 ret = -1;
1403
1404 nf_ct_expect_put(exp);
1405
1406 return ret;
1407}
1408
1409
1410static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1411 enum ip_conntrack_info ctinfo,
1412 unsigned int protoff,
1413 unsigned char **data, RegistrationRequest *rrq)
1414{
1415 struct nf_ct_h323_master *info = nfct_help_data(ct);
1416 int ret;
1417 typeof(set_ras_addr_hook) set_ras_addr;
1418
1419 pr_debug("nf_ct_ras: RRQ\n");
1420
1421 ret = expect_q931(skb, ct, ctinfo, protoff, data,
1422 rrq->callSignalAddress.item,
1423 rrq->callSignalAddress.count);
1424 if (ret < 0)
1425 return -1;
1426
1427 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1428 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1429 ct->status & IPS_NAT_MASK) {
1430 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1431 rrq->rasAddress.item,
1432 rrq->rasAddress.count);
1433 if (ret < 0)
1434 return -1;
1435 }
1436
1437 if (rrq->options & eRegistrationRequest_timeToLive) {
1438 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1439 info->timeout = rrq->timeToLive;
1440 } else
1441 info->timeout = default_rrq_ttl;
1442
1443 return 0;
1444}
1445
1446
1447static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1448 enum ip_conntrack_info ctinfo,
1449 unsigned int protoff,
1450 unsigned char **data, RegistrationConfirm *rcf)
1451{
1452 struct nf_ct_h323_master *info = nfct_help_data(ct);
1453 int dir = CTINFO2DIR(ctinfo);
1454 int ret;
1455 struct nf_conntrack_expect *exp;
1456 typeof(set_sig_addr_hook) set_sig_addr;
1457
1458 pr_debug("nf_ct_ras: RCF\n");
1459
1460 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1461 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1462 ct->status & IPS_NAT_MASK) {
1463 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1464 rcf->callSignalAddress.item,
1465 rcf->callSignalAddress.count);
1466 if (ret < 0)
1467 return -1;
1468 }
1469
1470 if (rcf->options & eRegistrationConfirm_timeToLive) {
1471 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1472 info->timeout = rcf->timeToLive;
1473 }
1474
1475 if (info->timeout > 0) {
1476 pr_debug("nf_ct_ras: set RAS connection timeout to "
1477 "%u seconds\n", info->timeout);
1478 nf_ct_refresh(ct, skb, info->timeout * HZ);
1479
1480
1481 spin_lock_bh(&nf_conntrack_expect_lock);
1482 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1483 info->sig_port[!dir]);
1484 if (exp) {
1485 pr_debug("nf_ct_ras: set Q.931 expect "
1486 "timeout to %u seconds for",
1487 info->timeout);
1488 nf_ct_dump_tuple(&exp->tuple);
1489 set_expect_timeout(exp, info->timeout);
1490 }
1491 spin_unlock_bh(&nf_conntrack_expect_lock);
1492 }
1493
1494 return 0;
1495}
1496
1497
1498static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1499 enum ip_conntrack_info ctinfo,
1500 unsigned int protoff,
1501 unsigned char **data, UnregistrationRequest *urq)
1502{
1503 struct nf_ct_h323_master *info = nfct_help_data(ct);
1504 int dir = CTINFO2DIR(ctinfo);
1505 int ret;
1506 typeof(set_sig_addr_hook) set_sig_addr;
1507
1508 pr_debug("nf_ct_ras: URQ\n");
1509
1510 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1511 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1512 ct->status & IPS_NAT_MASK) {
1513 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1514 urq->callSignalAddress.item,
1515 urq->callSignalAddress.count);
1516 if (ret < 0)
1517 return -1;
1518 }
1519
1520
1521 nf_ct_remove_expectations(ct);
1522 info->sig_port[dir] = 0;
1523 info->sig_port[!dir] = 0;
1524
1525
1526 nf_ct_refresh(ct, skb, 30 * HZ);
1527
1528 return 0;
1529}
1530
1531
1532static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1533 enum ip_conntrack_info ctinfo,
1534 unsigned int protoff,
1535 unsigned char **data, AdmissionRequest *arq)
1536{
1537 const struct nf_ct_h323_master *info = nfct_help_data(ct);
1538 int dir = CTINFO2DIR(ctinfo);
1539 __be16 port;
1540 union nf_inet_addr addr;
1541 typeof(set_h225_addr_hook) set_h225_addr;
1542
1543 pr_debug("nf_ct_ras: ARQ\n");
1544
1545 set_h225_addr = rcu_dereference(set_h225_addr_hook);
1546 if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1547 get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1548 &addr, &port) &&
1549 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1550 port == info->sig_port[dir] &&
1551 nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1552 set_h225_addr && ct->status & IPS_NAT_MASK) {
1553
1554 return set_h225_addr(skb, protoff, data, 0,
1555 &arq->destCallSignalAddress,
1556 &ct->tuplehash[!dir].tuple.dst.u3,
1557 info->sig_port[!dir]);
1558 }
1559
1560 if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1561 get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1562 &addr, &port) &&
1563 !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1564 set_h225_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1565 ct->status & IPS_NAT_MASK) {
1566
1567 return set_h225_addr(skb, protoff, data, 0,
1568 &arq->srcCallSignalAddress,
1569 &ct->tuplehash[!dir].tuple.dst.u3,
1570 port);
1571 }
1572
1573 return 0;
1574}
1575
1576
1577static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1578 enum ip_conntrack_info ctinfo,
1579 unsigned int protoff,
1580 unsigned char **data, AdmissionConfirm *acf)
1581{
1582 int dir = CTINFO2DIR(ctinfo);
1583 int ret = 0;
1584 __be16 port;
1585 union nf_inet_addr addr;
1586 struct nf_conntrack_expect *exp;
1587 typeof(set_sig_addr_hook) set_sig_addr;
1588
1589 pr_debug("nf_ct_ras: ACF\n");
1590
1591 if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1592 &addr, &port))
1593 return 0;
1594
1595 if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1596
1597 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1598 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1599 ct->status & IPS_NAT_MASK)
1600 return set_sig_addr(skb, ct, ctinfo, protoff, data,
1601 &acf->destCallSignalAddress, 1);
1602 return 0;
1603 }
1604
1605
1606 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1607 return -1;
1608 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1609 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1610 IPPROTO_TCP, NULL, &port);
1611 exp->flags = NF_CT_EXPECT_PERMANENT;
1612 exp->helper = nf_conntrack_helper_q931;
1613
1614 if (nf_ct_expect_related(exp) == 0) {
1615 pr_debug("nf_ct_ras: expect Q.931 ");
1616 nf_ct_dump_tuple(&exp->tuple);
1617 } else
1618 ret = -1;
1619
1620 nf_ct_expect_put(exp);
1621
1622 return ret;
1623}
1624
1625
1626static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1627 enum ip_conntrack_info ctinfo,
1628 unsigned int protoff,
1629 unsigned char **data, LocationRequest *lrq)
1630{
1631 typeof(set_ras_addr_hook) set_ras_addr;
1632
1633 pr_debug("nf_ct_ras: LRQ\n");
1634
1635 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1636 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1637 ct->status & IPS_NAT_MASK)
1638 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1639 &lrq->replyAddress, 1);
1640 return 0;
1641}
1642
1643
1644static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1645 enum ip_conntrack_info ctinfo,
1646 unsigned int protoff,
1647 unsigned char **data, LocationConfirm *lcf)
1648{
1649 int dir = CTINFO2DIR(ctinfo);
1650 int ret = 0;
1651 __be16 port;
1652 union nf_inet_addr addr;
1653 struct nf_conntrack_expect *exp;
1654
1655 pr_debug("nf_ct_ras: LCF\n");
1656
1657 if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1658 &addr, &port))
1659 return 0;
1660
1661
1662 if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1663 return -1;
1664 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1665 &ct->tuplehash[!dir].tuple.src.u3, &addr,
1666 IPPROTO_TCP, NULL, &port);
1667 exp->flags = NF_CT_EXPECT_PERMANENT;
1668 exp->helper = nf_conntrack_helper_q931;
1669
1670 if (nf_ct_expect_related(exp) == 0) {
1671 pr_debug("nf_ct_ras: expect Q.931 ");
1672 nf_ct_dump_tuple(&exp->tuple);
1673 } else
1674 ret = -1;
1675
1676 nf_ct_expect_put(exp);
1677
1678
1679
1680 return ret;
1681}
1682
1683
1684static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1685 enum ip_conntrack_info ctinfo,
1686 unsigned int protoff,
1687 unsigned char **data, InfoRequestResponse *irr)
1688{
1689 int ret;
1690 typeof(set_ras_addr_hook) set_ras_addr;
1691 typeof(set_sig_addr_hook) set_sig_addr;
1692
1693 pr_debug("nf_ct_ras: IRR\n");
1694
1695 set_ras_addr = rcu_dereference(set_ras_addr_hook);
1696 if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1697 ct->status & IPS_NAT_MASK) {
1698 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1699 &irr->rasAddress, 1);
1700 if (ret < 0)
1701 return -1;
1702 }
1703
1704 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1705 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1706 ct->status & IPS_NAT_MASK) {
1707 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1708 irr->callSignalAddress.item,
1709 irr->callSignalAddress.count);
1710 if (ret < 0)
1711 return -1;
1712 }
1713
1714 return 0;
1715}
1716
1717
1718static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1719 enum ip_conntrack_info ctinfo,
1720 unsigned int protoff,
1721 unsigned char **data, RasMessage *ras)
1722{
1723 switch (ras->choice) {
1724 case eRasMessage_gatekeeperRequest:
1725 return process_grq(skb, ct, ctinfo, protoff, data,
1726 &ras->gatekeeperRequest);
1727 case eRasMessage_gatekeeperConfirm:
1728 return process_gcf(skb, ct, ctinfo, protoff, data,
1729 &ras->gatekeeperConfirm);
1730 case eRasMessage_registrationRequest:
1731 return process_rrq(skb, ct, ctinfo, protoff, data,
1732 &ras->registrationRequest);
1733 case eRasMessage_registrationConfirm:
1734 return process_rcf(skb, ct, ctinfo, protoff, data,
1735 &ras->registrationConfirm);
1736 case eRasMessage_unregistrationRequest:
1737 return process_urq(skb, ct, ctinfo, protoff, data,
1738 &ras->unregistrationRequest);
1739 case eRasMessage_admissionRequest:
1740 return process_arq(skb, ct, ctinfo, protoff, data,
1741 &ras->admissionRequest);
1742 case eRasMessage_admissionConfirm:
1743 return process_acf(skb, ct, ctinfo, protoff, data,
1744 &ras->admissionConfirm);
1745 case eRasMessage_locationRequest:
1746 return process_lrq(skb, ct, ctinfo, protoff, data,
1747 &ras->locationRequest);
1748 case eRasMessage_locationConfirm:
1749 return process_lcf(skb, ct, ctinfo, protoff, data,
1750 &ras->locationConfirm);
1751 case eRasMessage_infoRequestResponse:
1752 return process_irr(skb, ct, ctinfo, protoff, data,
1753 &ras->infoRequestResponse);
1754 default:
1755 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1756 break;
1757 }
1758
1759 return 0;
1760}
1761
1762
1763static int ras_help(struct sk_buff *skb, unsigned int protoff,
1764 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1765{
1766 static RasMessage ras;
1767 unsigned char *data;
1768 int datalen = 0;
1769 int ret;
1770
1771 pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1772
1773 spin_lock_bh(&nf_h323_lock);
1774
1775
1776 data = get_udp_data(skb, protoff, &datalen);
1777 if (data == NULL)
1778 goto accept;
1779 pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1780 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1781
1782
1783 ret = DecodeRasMessage(data, datalen, &ras);
1784 if (ret < 0) {
1785 pr_debug("nf_ct_ras: decoding error: %s\n",
1786 ret == H323_ERROR_BOUND ?
1787 "out of bound" : "out of range");
1788 goto accept;
1789 }
1790
1791
1792 if (process_ras(skb, ct, ctinfo, protoff, &data, &ras) < 0)
1793 goto drop;
1794
1795 accept:
1796 spin_unlock_bh(&nf_h323_lock);
1797 return NF_ACCEPT;
1798
1799 drop:
1800 spin_unlock_bh(&nf_h323_lock);
1801 nf_ct_helper_log(skb, ct, "cannot process RAS message");
1802 return NF_DROP;
1803}
1804
1805
1806static const struct nf_conntrack_expect_policy ras_exp_policy = {
1807 .max_expected = 32,
1808 .timeout = 240,
1809};
1810
1811static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1812 {
1813 .name = "RAS",
1814 .me = THIS_MODULE,
1815 .data_len = sizeof(struct nf_ct_h323_master),
1816 .tuple.src.l3num = AF_INET,
1817 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1818 .tuple.dst.protonum = IPPROTO_UDP,
1819 .help = ras_help,
1820 .expect_policy = &ras_exp_policy,
1821 },
1822 {
1823 .name = "RAS",
1824 .me = THIS_MODULE,
1825 .data_len = sizeof(struct nf_ct_h323_master),
1826 .tuple.src.l3num = AF_INET6,
1827 .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT),
1828 .tuple.dst.protonum = IPPROTO_UDP,
1829 .help = ras_help,
1830 .expect_policy = &ras_exp_policy,
1831 },
1832};
1833
1834
1835static void __exit nf_conntrack_h323_fini(void)
1836{
1837 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1838 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1839 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1840 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1841 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1842 kfree(h323_buffer);
1843 pr_debug("nf_ct_h323: fini\n");
1844}
1845
1846
1847static int __init nf_conntrack_h323_init(void)
1848{
1849 int ret;
1850
1851 h323_buffer = kmalloc(65536, GFP_KERNEL);
1852 if (!h323_buffer)
1853 return -ENOMEM;
1854 ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1855 if (ret < 0)
1856 goto err1;
1857 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1858 if (ret < 0)
1859 goto err2;
1860 ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1861 if (ret < 0)
1862 goto err3;
1863 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1864 if (ret < 0)
1865 goto err4;
1866 ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1867 if (ret < 0)
1868 goto err5;
1869 pr_debug("nf_ct_h323: init success\n");
1870 return 0;
1871
1872err5:
1873 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1874err4:
1875 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1876err3:
1877 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1878err2:
1879 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1880err1:
1881 kfree(h323_buffer);
1882 return ret;
1883}
1884
1885
1886module_init(nf_conntrack_h323_init);
1887module_exit(nf_conntrack_h323_fini);
1888
1889EXPORT_SYMBOL_GPL(get_h225_addr);
1890EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1891EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1892EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1893EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1894EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1895EXPORT_SYMBOL_GPL(nat_t120_hook);
1896EXPORT_SYMBOL_GPL(nat_h245_hook);
1897EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1898EXPORT_SYMBOL_GPL(nat_q931_hook);
1899
1900MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1901MODULE_DESCRIPTION("H.323 connection tracking helper");
1902MODULE_LICENSE("GPL");
1903MODULE_ALIAS("ip_conntrack_h323");
1904MODULE_ALIAS_NFCT_HELPER("RAS");
1905MODULE_ALIAS_NFCT_HELPER("Q.931");
1906MODULE_ALIAS_NFCT_HELPER("H.245");
1907