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