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