1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * H.323 connection tracking helper
4  *
5  * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
6  * Copyright (c) 2006-2012 Patrick McHardy <kaber@trash.net>
7  *
8  * Based on the 'brute force' H.323 connection tracking module by
9  * Jozsef Kadlecsik <kadlec@netfilter.org>
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 #include <linux/netfilter_ipv6.h>
27 
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_tuple.h>
31 #include <net/netfilter/nf_conntrack_expect.h>
32 #include <net/netfilter/nf_conntrack_ecache.h>
33 #include <net/netfilter/nf_conntrack_helper.h>
34 #include <net/netfilter/nf_conntrack_zones.h>
35 #include <linux/netfilter/nf_conntrack_h323.h>
36 
37 #define H323_MAX_SIZE 65535
38 
39 /* Parameters */
40 static unsigned int default_rrq_ttl __read_mostly = 300;
41 module_param(default_rrq_ttl, uint, 0600);
42 MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ");
43 
44 static int gkrouted_only __read_mostly = 1;
45 module_param(gkrouted_only, int, 0600);
46 MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper");
47 
48 static bool callforward_filter __read_mostly = true;
49 module_param(callforward_filter, bool, 0600);
50 MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations "
51 				     "if both endpoints are on different sides "
52 				     "(determined by routing information)");
53 
54 /* Hooks for NAT */
55 int (*set_h245_addr_hook) (struct sk_buff *skb, unsigned int protoff,
56 			   unsigned char **data, int dataoff,
57 			   H245_TransportAddress *taddr,
58 			   union nf_inet_addr *addr, __be16 port)
59 			   __read_mostly;
60 int (*set_h225_addr_hook) (struct sk_buff *skb, unsigned int protoff,
61 			   unsigned char **data, int dataoff,
62 			   TransportAddress *taddr,
63 			   union nf_inet_addr *addr, __be16 port)
64 			   __read_mostly;
65 int (*set_sig_addr_hook) (struct sk_buff *skb,
66 			  struct nf_conn *ct,
67 			  enum ip_conntrack_info ctinfo,
68 			  unsigned int protoff, unsigned char **data,
69 			  TransportAddress *taddr, int count) __read_mostly;
70 int (*set_ras_addr_hook) (struct sk_buff *skb,
71 			  struct nf_conn *ct,
72 			  enum ip_conntrack_info ctinfo,
73 			  unsigned int protoff, unsigned char **data,
74 			  TransportAddress *taddr, int count) __read_mostly;
75 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
76 			  struct nf_conn *ct,
77 			  enum ip_conntrack_info ctinfo,
78 			  unsigned int protoff,
79 			  unsigned char **data, int dataoff,
80 			  H245_TransportAddress *taddr,
81 			  __be16 port, __be16 rtp_port,
82 			  struct nf_conntrack_expect *rtp_exp,
83 			  struct nf_conntrack_expect *rtcp_exp) __read_mostly;
84 int (*nat_t120_hook) (struct sk_buff *skb,
85 		      struct nf_conn *ct,
86 		      enum ip_conntrack_info ctinfo,
87 		      unsigned int protoff,
88 		      unsigned char **data, int dataoff,
89 		      H245_TransportAddress *taddr, __be16 port,
90 		      struct nf_conntrack_expect *exp) __read_mostly;
91 int (*nat_h245_hook) (struct sk_buff *skb,
92 		      struct nf_conn *ct,
93 		      enum ip_conntrack_info ctinfo,
94 		      unsigned int protoff,
95 		      unsigned char **data, int dataoff,
96 		      TransportAddress *taddr, __be16 port,
97 		      struct nf_conntrack_expect *exp) __read_mostly;
98 int (*nat_callforwarding_hook) (struct sk_buff *skb,
99 				struct nf_conn *ct,
100 				enum ip_conntrack_info ctinfo,
101 				unsigned int protoff,
102 				unsigned char **data, int dataoff,
103 				TransportAddress *taddr, __be16 port,
104 				struct nf_conntrack_expect *exp) __read_mostly;
105 int (*nat_q931_hook) (struct sk_buff *skb,
106 		      struct nf_conn *ct,
107 		      enum ip_conntrack_info ctinfo,
108 		      unsigned int protoff,
109 		      unsigned char **data, TransportAddress *taddr, int idx,
110 		      __be16 port, struct nf_conntrack_expect *exp)
111 		      __read_mostly;
112 
113 static DEFINE_SPINLOCK(nf_h323_lock);
114 static char *h323_buffer;
115 
116 static struct nf_conntrack_helper nf_conntrack_helper_h245;
117 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
118 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
119 
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)120 static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,
121 			 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
122 			 unsigned char **data, int *datalen, int *dataoff)
123 {
124 	struct nf_ct_h323_master *info = nfct_help_data(ct);
125 	int dir = CTINFO2DIR(ctinfo);
126 	const struct tcphdr *th;
127 	struct tcphdr _tcph;
128 	int tcpdatalen;
129 	int tcpdataoff;
130 	unsigned char *tpkt;
131 	int tpktlen;
132 	int tpktoff;
133 
134 	/* Get TCP header */
135 	th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
136 	if (th == NULL)
137 		return 0;
138 
139 	/* Get TCP data offset */
140 	tcpdataoff = protoff + th->doff * 4;
141 
142 	/* Get TCP data length */
143 	tcpdatalen = skb->len - tcpdataoff;
144 	if (tcpdatalen <= 0)	/* No TCP data */
145 		goto clear_out;
146 
147 	if (tcpdatalen > H323_MAX_SIZE)
148 		tcpdatalen = H323_MAX_SIZE;
149 
150 	if (*data == NULL) {	/* first TPKT */
151 		/* Get first TPKT pointer */
152 		tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
153 					  h323_buffer);
154 		if (!tpkt)
155 			goto clear_out;
156 
157 		/* Validate TPKT identifier */
158 		if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) {
159 			/* Netmeeting sends TPKT header and data separately */
160 			if (info->tpkt_len[dir] > 0) {
161 				pr_debug("nf_ct_h323: previous packet "
162 					 "indicated separate TPKT data of %hu "
163 					 "bytes\n", info->tpkt_len[dir]);
164 				if (info->tpkt_len[dir] <= tcpdatalen) {
165 					/* Yes, there was a TPKT header
166 					 * received */
167 					*data = tpkt;
168 					*datalen = info->tpkt_len[dir];
169 					*dataoff = 0;
170 					goto out;
171 				}
172 
173 				/* Fragmented TPKT */
174 				pr_debug("nf_ct_h323: fragmented TPKT\n");
175 				goto clear_out;
176 			}
177 
178 			/* It is not even a TPKT */
179 			return 0;
180 		}
181 		tpktoff = 0;
182 	} else {		/* Next TPKT */
183 		tpktoff = *dataoff + *datalen;
184 		tcpdatalen -= tpktoff;
185 		if (tcpdatalen <= 4)	/* No more TPKT */
186 			goto clear_out;
187 		tpkt = *data + *datalen;
188 
189 		/* Validate TPKT identifier */
190 		if (tpkt[0] != 0x03 || tpkt[1] != 0)
191 			goto clear_out;
192 	}
193 
194 	/* Validate TPKT length */
195 	tpktlen = tpkt[2] * 256 + tpkt[3];
196 	if (tpktlen < 4)
197 		goto clear_out;
198 	if (tpktlen > tcpdatalen) {
199 		if (tcpdatalen == 4) {	/* Separate TPKT header */
200 			/* Netmeeting sends TPKT header and data separately */
201 			pr_debug("nf_ct_h323: separate TPKT header indicates "
202 				 "there will be TPKT data of %d bytes\n",
203 				 tpktlen - 4);
204 			info->tpkt_len[dir] = tpktlen - 4;
205 			return 0;
206 		}
207 
208 		pr_debug("nf_ct_h323: incomplete TPKT (fragmented?)\n");
209 		goto clear_out;
210 	}
211 
212 	/* This is the encapsulated data */
213 	*data = tpkt + 4;
214 	*datalen = tpktlen - 4;
215 	*dataoff = tpktoff + 4;
216 
217       out:
218 	/* Clear TPKT length */
219 	info->tpkt_len[dir] = 0;
220 	return 1;
221 
222       clear_out:
223 	info->tpkt_len[dir] = 0;
224 	return 0;
225 }
226 
get_h245_addr(struct nf_conn * ct,const unsigned char * data,H245_TransportAddress * taddr,union nf_inet_addr * addr,__be16 * port)227 static int get_h245_addr(struct nf_conn *ct, const unsigned char *data,
228 			 H245_TransportAddress *taddr,
229 			 union nf_inet_addr *addr, __be16 *port)
230 {
231 	const unsigned char *p;
232 	int len;
233 
234 	if (taddr->choice != eH245_TransportAddress_unicastAddress)
235 		return 0;
236 
237 	switch (taddr->unicastAddress.choice) {
238 	case eUnicastAddress_iPAddress:
239 		if (nf_ct_l3num(ct) != AF_INET)
240 			return 0;
241 		p = data + taddr->unicastAddress.iPAddress.network;
242 		len = 4;
243 		break;
244 	case eUnicastAddress_iP6Address:
245 		if (nf_ct_l3num(ct) != AF_INET6)
246 			return 0;
247 		p = data + taddr->unicastAddress.iP6Address.network;
248 		len = 16;
249 		break;
250 	default:
251 		return 0;
252 	}
253 
254 	memcpy(addr, p, len);
255 	memset((void *)addr + len, 0, sizeof(*addr) - len);
256 	memcpy(port, p + len, sizeof(__be16));
257 
258 	return 1;
259 }
260 
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)261 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
262 			   enum ip_conntrack_info ctinfo,
263 			   unsigned int protoff,
264 			   unsigned char **data, int dataoff,
265 			   H245_TransportAddress *taddr)
266 {
267 	int dir = CTINFO2DIR(ctinfo);
268 	int ret = 0;
269 	__be16 port;
270 	__be16 rtp_port, rtcp_port;
271 	union nf_inet_addr addr;
272 	struct nf_conntrack_expect *rtp_exp;
273 	struct nf_conntrack_expect *rtcp_exp;
274 	typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
275 
276 	/* Read RTP or RTCP address */
277 	if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
278 	    memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
279 	    port == 0)
280 		return 0;
281 
282 	/* RTP port is even */
283 	rtp_port = port & ~htons(1);
284 	rtcp_port = port | htons(1);
285 
286 	/* Create expect for RTP */
287 	if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
288 		return -1;
289 	nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
290 			  &ct->tuplehash[!dir].tuple.src.u3,
291 			  &ct->tuplehash[!dir].tuple.dst.u3,
292 			  IPPROTO_UDP, NULL, &rtp_port);
293 
294 	/* Create expect for RTCP */
295 	if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
296 		nf_ct_expect_put(rtp_exp);
297 		return -1;
298 	}
299 	nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
300 			  &ct->tuplehash[!dir].tuple.src.u3,
301 			  &ct->tuplehash[!dir].tuple.dst.u3,
302 			  IPPROTO_UDP, NULL, &rtcp_port);
303 
304 	if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
305 		   &ct->tuplehash[!dir].tuple.dst.u3,
306 		   sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
307 		   (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
308 		   nf_ct_l3num(ct) == NFPROTO_IPV4 &&
309 		   ct->status & IPS_NAT_MASK) {
310 		/* NAT needed */
311 		ret = nat_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
312 				   taddr, port, rtp_port, rtp_exp, rtcp_exp);
313 	} else {		/* Conntrack only */
314 		if (nf_ct_expect_related(rtp_exp, 0) == 0) {
315 			if (nf_ct_expect_related(rtcp_exp, 0) == 0) {
316 				pr_debug("nf_ct_h323: expect RTP ");
317 				nf_ct_dump_tuple(&rtp_exp->tuple);
318 				pr_debug("nf_ct_h323: expect RTCP ");
319 				nf_ct_dump_tuple(&rtcp_exp->tuple);
320 			} else {
321 				nf_ct_unexpect_related(rtp_exp);
322 				ret = -1;
323 			}
324 		} else
325 			ret = -1;
326 	}
327 
328 	nf_ct_expect_put(rtp_exp);
329 	nf_ct_expect_put(rtcp_exp);
330 
331 	return ret;
332 }
333 
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)334 static int expect_t120(struct sk_buff *skb,
335 		       struct nf_conn *ct,
336 		       enum ip_conntrack_info ctinfo,
337 		       unsigned int protoff,
338 		       unsigned char **data, int dataoff,
339 		       H245_TransportAddress *taddr)
340 {
341 	int dir = CTINFO2DIR(ctinfo);
342 	int ret = 0;
343 	__be16 port;
344 	union nf_inet_addr addr;
345 	struct nf_conntrack_expect *exp;
346 	typeof(nat_t120_hook) nat_t120;
347 
348 	/* Read T.120 address */
349 	if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
350 	    memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
351 	    port == 0)
352 		return 0;
353 
354 	/* Create expect for T.120 connections */
355 	if ((exp = nf_ct_expect_alloc(ct)) == NULL)
356 		return -1;
357 	nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
358 			  &ct->tuplehash[!dir].tuple.src.u3,
359 			  &ct->tuplehash[!dir].tuple.dst.u3,
360 			  IPPROTO_TCP, NULL, &port);
361 	exp->flags = NF_CT_EXPECT_PERMANENT;	/* Accept multiple channels */
362 
363 	if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
364 		   &ct->tuplehash[!dir].tuple.dst.u3,
365 		   sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
366 	    (nat_t120 = rcu_dereference(nat_t120_hook)) &&
367 	    nf_ct_l3num(ct) == NFPROTO_IPV4 &&
368 	    ct->status & IPS_NAT_MASK) {
369 		/* NAT needed */
370 		ret = nat_t120(skb, ct, ctinfo, protoff, data, dataoff, taddr,
371 			       port, exp);
372 	} else {		/* Conntrack only */
373 		if (nf_ct_expect_related(exp, 0) == 0) {
374 			pr_debug("nf_ct_h323: expect T.120 ");
375 			nf_ct_dump_tuple(&exp->tuple);
376 		} else
377 			ret = -1;
378 	}
379 
380 	nf_ct_expect_put(exp);
381 
382 	return ret;
383 }
384 
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)385 static int process_h245_channel(struct sk_buff *skb,
386 				struct nf_conn *ct,
387 				enum ip_conntrack_info ctinfo,
388 				unsigned int protoff,
389 				unsigned char **data, int dataoff,
390 				H2250LogicalChannelParameters *channel)
391 {
392 	int ret;
393 
394 	if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
395 		/* RTP */
396 		ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
397 				      &channel->mediaChannel);
398 		if (ret < 0)
399 			return -1;
400 	}
401 
402 	if (channel->
403 	    options & eH2250LogicalChannelParameters_mediaControlChannel) {
404 		/* RTCP */
405 		ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
406 				      &channel->mediaControlChannel);
407 		if (ret < 0)
408 			return -1;
409 	}
410 
411 	return 0;
412 }
413 
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)414 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
415 		       enum ip_conntrack_info ctinfo,
416 		       unsigned int protoff,
417 		       unsigned char **data, int dataoff,
418 		       OpenLogicalChannel *olc)
419 {
420 	int ret;
421 
422 	pr_debug("nf_ct_h323: OpenLogicalChannel\n");
423 
424 	if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
425 	    eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
426 	{
427 		ret = process_h245_channel(skb, ct, ctinfo,
428 					   protoff, data, dataoff,
429 					   &olc->
430 					   forwardLogicalChannelParameters.
431 					   multiplexParameters.
432 					   h2250LogicalChannelParameters);
433 		if (ret < 0)
434 			return -1;
435 	}
436 
437 	if ((olc->options &
438 	     eOpenLogicalChannel_reverseLogicalChannelParameters) &&
439 	    (olc->reverseLogicalChannelParameters.options &
440 	     eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
441 	    && (olc->reverseLogicalChannelParameters.multiplexParameters.
442 		choice ==
443 		eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
444 	{
445 		ret =
446 		    process_h245_channel(skb, ct, ctinfo,
447 					 protoff, data, dataoff,
448 					 &olc->
449 					 reverseLogicalChannelParameters.
450 					 multiplexParameters.
451 					 h2250LogicalChannelParameters);
452 		if (ret < 0)
453 			return -1;
454 	}
455 
456 	if ((olc->options & eOpenLogicalChannel_separateStack) &&
457 	    olc->forwardLogicalChannelParameters.dataType.choice ==
458 	    eDataType_data &&
459 	    olc->forwardLogicalChannelParameters.dataType.data.application.
460 	    choice == eDataApplicationCapability_application_t120 &&
461 	    olc->forwardLogicalChannelParameters.dataType.data.application.
462 	    t120.choice == eDataProtocolCapability_separateLANStack &&
463 	    olc->separateStack.networkAddress.choice ==
464 	    eNetworkAccessParameters_networkAddress_localAreaAddress) {
465 		ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
466 				  &olc->separateStack.networkAddress.
467 				  localAreaAddress);
468 		if (ret < 0)
469 			return -1;
470 	}
471 
472 	return 0;
473 }
474 
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)475 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
476 			enum ip_conntrack_info ctinfo,
477 			unsigned int protoff, unsigned char **data, int dataoff,
478 			OpenLogicalChannelAck *olca)
479 {
480 	H2250LogicalChannelAckParameters *ack;
481 	int ret;
482 
483 	pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
484 
485 	if ((olca->options &
486 	     eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
487 	    (olca->reverseLogicalChannelParameters.options &
488 	     eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
489 	    && (olca->reverseLogicalChannelParameters.multiplexParameters.
490 		choice ==
491 		eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
492 	{
493 		ret = process_h245_channel(skb, ct, ctinfo,
494 					   protoff, data, dataoff,
495 					   &olca->
496 					   reverseLogicalChannelParameters.
497 					   multiplexParameters.
498 					   h2250LogicalChannelParameters);
499 		if (ret < 0)
500 			return -1;
501 	}
502 
503 	if ((olca->options &
504 	     eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
505 	    (olca->forwardMultiplexAckParameters.choice ==
506 	     eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
507 	{
508 		ack = &olca->forwardMultiplexAckParameters.
509 		    h2250LogicalChannelAckParameters;
510 		if (ack->options &
511 		    eH2250LogicalChannelAckParameters_mediaChannel) {
512 			/* RTP */
513 			ret = expect_rtp_rtcp(skb, ct, ctinfo,
514 					      protoff, data, dataoff,
515 					      &ack->mediaChannel);
516 			if (ret < 0)
517 				return -1;
518 		}
519 
520 		if (ack->options &
521 		    eH2250LogicalChannelAckParameters_mediaControlChannel) {
522 			/* RTCP */
523 			ret = expect_rtp_rtcp(skb, ct, ctinfo,
524 					      protoff, data, dataoff,
525 					      &ack->mediaControlChannel);
526 			if (ret < 0)
527 				return -1;
528 		}
529 	}
530 
531 	if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
532 		olca->separateStack.networkAddress.choice ==
533 		eNetworkAccessParameters_networkAddress_localAreaAddress) {
534 		ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
535 				  &olca->separateStack.networkAddress.
536 				  localAreaAddress);
537 		if (ret < 0)
538 			return -1;
539 	}
540 
541 	return 0;
542 }
543 
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)544 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
545 			enum ip_conntrack_info ctinfo,
546 			unsigned int protoff, unsigned char **data, int dataoff,
547 			MultimediaSystemControlMessage *mscm)
548 {
549 	switch (mscm->choice) {
550 	case eMultimediaSystemControlMessage_request:
551 		if (mscm->request.choice ==
552 		    eRequestMessage_openLogicalChannel) {
553 			return process_olc(skb, ct, ctinfo,
554 					   protoff, data, dataoff,
555 					   &mscm->request.openLogicalChannel);
556 		}
557 		pr_debug("nf_ct_h323: H.245 Request %d\n",
558 			 mscm->request.choice);
559 		break;
560 	case eMultimediaSystemControlMessage_response:
561 		if (mscm->response.choice ==
562 		    eResponseMessage_openLogicalChannelAck) {
563 			return process_olca(skb, ct, ctinfo,
564 					    protoff, data, dataoff,
565 					    &mscm->response.
566 					    openLogicalChannelAck);
567 		}
568 		pr_debug("nf_ct_h323: H.245 Response %d\n",
569 			 mscm->response.choice);
570 		break;
571 	default:
572 		pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice);
573 		break;
574 	}
575 
576 	return 0;
577 }
578 
h245_help(struct sk_buff * skb,unsigned int protoff,struct nf_conn * ct,enum ip_conntrack_info ctinfo)579 static int h245_help(struct sk_buff *skb, unsigned int protoff,
580 		     struct nf_conn *ct, enum ip_conntrack_info ctinfo)
581 {
582 	static MultimediaSystemControlMessage mscm;
583 	unsigned char *data = NULL;
584 	int datalen;
585 	int dataoff;
586 	int ret;
587 
588 	/* Until there's been traffic both ways, don't look in packets. */
589 	if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
590 		return NF_ACCEPT;
591 
592 	pr_debug("nf_ct_h245: skblen = %u\n", skb->len);
593 
594 	spin_lock_bh(&nf_h323_lock);
595 
596 	/* Process each TPKT */
597 	while (get_tpkt_data(skb, protoff, ct, ctinfo,
598 			     &data, &datalen, &dataoff)) {
599 		pr_debug("nf_ct_h245: TPKT len=%d ", datalen);
600 		nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
601 
602 		/* Decode H.245 signal */
603 		ret = DecodeMultimediaSystemControlMessage(data, datalen,
604 							   &mscm);
605 		if (ret < 0) {
606 			pr_debug("nf_ct_h245: decoding error: %s\n",
607 				 ret == H323_ERROR_BOUND ?
608 				 "out of bound" : "out of range");
609 			/* We don't drop when decoding error */
610 			break;
611 		}
612 
613 		/* Process H.245 signal */
614 		if (process_h245(skb, ct, ctinfo, protoff,
615 				 &data, dataoff, &mscm) < 0)
616 			goto drop;
617 	}
618 
619 	spin_unlock_bh(&nf_h323_lock);
620 	return NF_ACCEPT;
621 
622       drop:
623 	spin_unlock_bh(&nf_h323_lock);
624 	nf_ct_helper_log(skb, ct, "cannot process H.245 message");
625 	return NF_DROP;
626 }
627 
628 static const struct nf_conntrack_expect_policy h245_exp_policy = {
629 	.max_expected	= H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
630 	.timeout	= 240,
631 };
632 
633 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
634 	.name			= "H.245",
635 	.me			= THIS_MODULE,
636 	.tuple.src.l3num	= AF_UNSPEC,
637 	.tuple.dst.protonum	= IPPROTO_UDP,
638 	.help			= h245_help,
639 	.expect_policy		= &h245_exp_policy,
640 };
641 
get_h225_addr(struct nf_conn * ct,unsigned char * data,TransportAddress * taddr,union nf_inet_addr * addr,__be16 * port)642 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
643 		  TransportAddress *taddr,
644 		  union nf_inet_addr *addr, __be16 *port)
645 {
646 	const unsigned char *p;
647 	int len;
648 
649 	switch (taddr->choice) {
650 	case eTransportAddress_ipAddress:
651 		if (nf_ct_l3num(ct) != AF_INET)
652 			return 0;
653 		p = data + taddr->ipAddress.ip;
654 		len = 4;
655 		break;
656 	case eTransportAddress_ip6Address:
657 		if (nf_ct_l3num(ct) != AF_INET6)
658 			return 0;
659 		p = data + taddr->ip6Address.ip;
660 		len = 16;
661 		break;
662 	default:
663 		return 0;
664 	}
665 
666 	memcpy(addr, p, len);
667 	memset((void *)addr + len, 0, sizeof(*addr) - len);
668 	memcpy(port, p + len, sizeof(__be16));
669 
670 	return 1;
671 }
672 
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)673 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
674 		       enum ip_conntrack_info ctinfo,
675 		       unsigned int protoff, unsigned char **data, int dataoff,
676 		       TransportAddress *taddr)
677 {
678 	int dir = CTINFO2DIR(ctinfo);
679 	int ret = 0;
680 	__be16 port;
681 	union nf_inet_addr addr;
682 	struct nf_conntrack_expect *exp;
683 	typeof(nat_h245_hook) nat_h245;
684 
685 	/* Read h245Address */
686 	if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
687 	    memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
688 	    port == 0)
689 		return 0;
690 
691 	/* Create expect for h245 connection */
692 	if ((exp = nf_ct_expect_alloc(ct)) == NULL)
693 		return -1;
694 	nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
695 			  &ct->tuplehash[!dir].tuple.src.u3,
696 			  &ct->tuplehash[!dir].tuple.dst.u3,
697 			  IPPROTO_TCP, NULL, &port);
698 	exp->helper = &nf_conntrack_helper_h245;
699 
700 	if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
701 		   &ct->tuplehash[!dir].tuple.dst.u3,
702 		   sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
703 	    (nat_h245 = rcu_dereference(nat_h245_hook)) &&
704 	    nf_ct_l3num(ct) == NFPROTO_IPV4 &&
705 	    ct->status & IPS_NAT_MASK) {
706 		/* NAT needed */
707 		ret = nat_h245(skb, ct, ctinfo, protoff, data, dataoff, taddr,
708 			       port, exp);
709 	} else {		/* Conntrack only */
710 		if (nf_ct_expect_related(exp, 0) == 0) {
711 			pr_debug("nf_ct_q931: expect H.245 ");
712 			nf_ct_dump_tuple(&exp->tuple);
713 		} else
714 			ret = -1;
715 	}
716 
717 	nf_ct_expect_put(exp);
718 
719 	return ret;
720 }
721 
722 /* If the calling party is on the same side of the forward-to party,
723  * we don't need to track the second call
724  */
callforward_do_filter(struct net * net,const union nf_inet_addr * src,const union nf_inet_addr * dst,u_int8_t family)725 static int callforward_do_filter(struct net *net,
726 				 const union nf_inet_addr *src,
727 				 const union nf_inet_addr *dst,
728 				 u_int8_t family)
729 {
730 	int ret = 0;
731 
732 	switch (family) {
733 	case AF_INET: {
734 		struct flowi4 fl1, fl2;
735 		struct rtable *rt1, *rt2;
736 
737 		memset(&fl1, 0, sizeof(fl1));
738 		fl1.daddr = src->ip;
739 
740 		memset(&fl2, 0, sizeof(fl2));
741 		fl2.daddr = dst->ip;
742 		if (!nf_ip_route(net, (struct dst_entry **)&rt1,
743 				 flowi4_to_flowi(&fl1), false)) {
744 			if (!nf_ip_route(net, (struct dst_entry **)&rt2,
745 					 flowi4_to_flowi(&fl2), false)) {
746 				if (rt_nexthop(rt1, fl1.daddr) ==
747 				    rt_nexthop(rt2, fl2.daddr) &&
748 				    rt1->dst.dev  == rt2->dst.dev)
749 					ret = 1;
750 				dst_release(&rt2->dst);
751 			}
752 			dst_release(&rt1->dst);
753 		}
754 		break;
755 	}
756 #if IS_ENABLED(CONFIG_IPV6)
757 	case AF_INET6: {
758 		struct rt6_info *rt1, *rt2;
759 		struct flowi6 fl1, fl2;
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 (!nf_ip6_route(net, (struct dst_entry **)&rt1,
767 				  flowi6_to_flowi(&fl1), false)) {
768 			if (!nf_ip6_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) == 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 	if (*datalen > H323_MAX_SIZE)
1229 		*datalen = H323_MAX_SIZE;
1230 
1231 	return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1232 }
1233 
find_expect(struct nf_conn * ct,union nf_inet_addr * addr,__be16 port)1234 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1235 					       union nf_inet_addr *addr,
1236 					       __be16 port)
1237 {
1238 	struct net *net = nf_ct_net(ct);
1239 	struct nf_conntrack_expect *exp;
1240 	struct nf_conntrack_tuple tuple;
1241 
1242 	memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1243 	tuple.src.u.tcp.port = 0;
1244 	memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1245 	tuple.dst.u.tcp.port = port;
1246 	tuple.dst.protonum = IPPROTO_TCP;
1247 
1248 	exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1249 	if (exp && exp->master == ct)
1250 		return exp;
1251 	return NULL;
1252 }
1253 
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)1254 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1255 		       enum ip_conntrack_info ctinfo,
1256 		       unsigned int protoff, unsigned char **data,
1257 		       TransportAddress *taddr, int count)
1258 {
1259 	struct nf_ct_h323_master *info = nfct_help_data(ct);
1260 	int dir = CTINFO2DIR(ctinfo);
1261 	int ret = 0;
1262 	int i;
1263 	__be16 port;
1264 	union nf_inet_addr addr;
1265 	struct nf_conntrack_expect *exp;
1266 	typeof(nat_q931_hook) nat_q931;
1267 
1268 	/* Look for the first related address */
1269 	for (i = 0; i < count; i++) {
1270 		if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1271 		    memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1272 			   sizeof(addr)) == 0 && port != 0)
1273 			break;
1274 	}
1275 
1276 	if (i >= count)		/* Not found */
1277 		return 0;
1278 
1279 	/* Create expect for Q.931 */
1280 	if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1281 		return -1;
1282 	nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1283 			  gkrouted_only ? /* only accept calls from GK? */
1284 				&ct->tuplehash[!dir].tuple.src.u3 : NULL,
1285 			  &ct->tuplehash[!dir].tuple.dst.u3,
1286 			  IPPROTO_TCP, NULL, &port);
1287 	exp->helper = nf_conntrack_helper_q931;
1288 	exp->flags = NF_CT_EXPECT_PERMANENT;	/* Accept multiple calls */
1289 
1290 	nat_q931 = rcu_dereference(nat_q931_hook);
1291 	if (nat_q931 && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1292 	    ct->status & IPS_NAT_MASK) {	/* Need NAT */
1293 		ret = nat_q931(skb, ct, ctinfo, protoff, data,
1294 			       taddr, i, port, exp);
1295 	} else {		/* Conntrack only */
1296 		if (nf_ct_expect_related(exp, 0) == 0) {
1297 			pr_debug("nf_ct_ras: expect Q.931 ");
1298 			nf_ct_dump_tuple(&exp->tuple);
1299 
1300 			/* Save port for looking up expect in processing RCF */
1301 			info->sig_port[dir] = port;
1302 		} else
1303 			ret = -1;
1304 	}
1305 
1306 	nf_ct_expect_put(exp);
1307 
1308 	return ret;
1309 }
1310 
process_grq(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,GatekeeperRequest * grq)1311 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1312 		       enum ip_conntrack_info ctinfo,
1313 		       unsigned int protoff,
1314 		       unsigned char **data, GatekeeperRequest *grq)
1315 {
1316 	typeof(set_ras_addr_hook) set_ras_addr;
1317 
1318 	pr_debug("nf_ct_ras: GRQ\n");
1319 
1320 	set_ras_addr = rcu_dereference(set_ras_addr_hook);
1321 	if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1322 	    ct->status & IPS_NAT_MASK)	/* NATed */
1323 		return set_ras_addr(skb, ct, ctinfo, protoff, data,
1324 				    &grq->rasAddress, 1);
1325 	return 0;
1326 }
1327 
process_gcf(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,GatekeeperConfirm * gcf)1328 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1329 		       enum ip_conntrack_info ctinfo,
1330 		       unsigned int protoff,
1331 		       unsigned char **data, GatekeeperConfirm *gcf)
1332 {
1333 	int dir = CTINFO2DIR(ctinfo);
1334 	int ret = 0;
1335 	__be16 port;
1336 	union nf_inet_addr addr;
1337 	struct nf_conntrack_expect *exp;
1338 
1339 	pr_debug("nf_ct_ras: GCF\n");
1340 
1341 	if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1342 		return 0;
1343 
1344 	/* Registration port is the same as discovery port */
1345 	if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1346 	    port == ct->tuplehash[dir].tuple.src.u.udp.port)
1347 		return 0;
1348 
1349 	/* Avoid RAS expectation loops. A GCF is never expected. */
1350 	if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1351 		return 0;
1352 
1353 	/* Need new expect */
1354 	if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1355 		return -1;
1356 	nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1357 			  &ct->tuplehash[!dir].tuple.src.u3, &addr,
1358 			  IPPROTO_UDP, NULL, &port);
1359 	exp->helper = nf_conntrack_helper_ras;
1360 
1361 	if (nf_ct_expect_related(exp, 0) == 0) {
1362 		pr_debug("nf_ct_ras: expect RAS ");
1363 		nf_ct_dump_tuple(&exp->tuple);
1364 	} else
1365 		ret = -1;
1366 
1367 	nf_ct_expect_put(exp);
1368 
1369 	return ret;
1370 }
1371 
process_rrq(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,RegistrationRequest * rrq)1372 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1373 		       enum ip_conntrack_info ctinfo,
1374 		       unsigned int protoff,
1375 		       unsigned char **data, RegistrationRequest *rrq)
1376 {
1377 	struct nf_ct_h323_master *info = nfct_help_data(ct);
1378 	int ret;
1379 	typeof(set_ras_addr_hook) set_ras_addr;
1380 
1381 	pr_debug("nf_ct_ras: RRQ\n");
1382 
1383 	ret = expect_q931(skb, ct, ctinfo, protoff, data,
1384 			  rrq->callSignalAddress.item,
1385 			  rrq->callSignalAddress.count);
1386 	if (ret < 0)
1387 		return -1;
1388 
1389 	set_ras_addr = rcu_dereference(set_ras_addr_hook);
1390 	if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1391 	    ct->status & IPS_NAT_MASK) {
1392 		ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1393 				   rrq->rasAddress.item,
1394 				   rrq->rasAddress.count);
1395 		if (ret < 0)
1396 			return -1;
1397 	}
1398 
1399 	if (rrq->options & eRegistrationRequest_timeToLive) {
1400 		pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1401 		info->timeout = rrq->timeToLive;
1402 	} else
1403 		info->timeout = default_rrq_ttl;
1404 
1405 	return 0;
1406 }
1407 
process_rcf(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,RegistrationConfirm * rcf)1408 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1409 		       enum ip_conntrack_info ctinfo,
1410 		       unsigned int protoff,
1411 		       unsigned char **data, RegistrationConfirm *rcf)
1412 {
1413 	struct nf_ct_h323_master *info = nfct_help_data(ct);
1414 	int dir = CTINFO2DIR(ctinfo);
1415 	int ret;
1416 	struct nf_conntrack_expect *exp;
1417 	typeof(set_sig_addr_hook) set_sig_addr;
1418 
1419 	pr_debug("nf_ct_ras: RCF\n");
1420 
1421 	set_sig_addr = rcu_dereference(set_sig_addr_hook);
1422 	if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1423 	    ct->status & IPS_NAT_MASK) {
1424 		ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1425 					rcf->callSignalAddress.item,
1426 					rcf->callSignalAddress.count);
1427 		if (ret < 0)
1428 			return -1;
1429 	}
1430 
1431 	if (rcf->options & eRegistrationConfirm_timeToLive) {
1432 		pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1433 		info->timeout = rcf->timeToLive;
1434 	}
1435 
1436 	if (info->timeout > 0) {
1437 		pr_debug("nf_ct_ras: set RAS connection timeout to "
1438 			 "%u seconds\n", info->timeout);
1439 		nf_ct_refresh(ct, skb, info->timeout * HZ);
1440 
1441 		/* Set expect timeout */
1442 		spin_lock_bh(&nf_conntrack_expect_lock);
1443 		exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1444 				  info->sig_port[!dir]);
1445 		if (exp) {
1446 			pr_debug("nf_ct_ras: set Q.931 expect "
1447 				 "timeout to %u seconds for",
1448 				 info->timeout);
1449 			nf_ct_dump_tuple(&exp->tuple);
1450 			mod_timer_pending(&exp->timeout,
1451 					  jiffies + info->timeout * HZ);
1452 		}
1453 		spin_unlock_bh(&nf_conntrack_expect_lock);
1454 	}
1455 
1456 	return 0;
1457 }
1458 
process_urq(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,UnregistrationRequest * urq)1459 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1460 		       enum ip_conntrack_info ctinfo,
1461 		       unsigned int protoff,
1462 		       unsigned char **data, UnregistrationRequest *urq)
1463 {
1464 	struct nf_ct_h323_master *info = nfct_help_data(ct);
1465 	int dir = CTINFO2DIR(ctinfo);
1466 	int ret;
1467 	typeof(set_sig_addr_hook) set_sig_addr;
1468 
1469 	pr_debug("nf_ct_ras: URQ\n");
1470 
1471 	set_sig_addr = rcu_dereference(set_sig_addr_hook);
1472 	if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1473 	    ct->status & IPS_NAT_MASK) {
1474 		ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1475 				   urq->callSignalAddress.item,
1476 				   urq->callSignalAddress.count);
1477 		if (ret < 0)
1478 			return -1;
1479 	}
1480 
1481 	/* Clear old expect */
1482 	nf_ct_remove_expectations(ct);
1483 	info->sig_port[dir] = 0;
1484 	info->sig_port[!dir] = 0;
1485 
1486 	/* Give it 30 seconds for UCF or URJ */
1487 	nf_ct_refresh(ct, skb, 30 * HZ);
1488 
1489 	return 0;
1490 }
1491 
process_arq(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,AdmissionRequest * arq)1492 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1493 		       enum ip_conntrack_info ctinfo,
1494 		       unsigned int protoff,
1495 		       unsigned char **data, AdmissionRequest *arq)
1496 {
1497 	const struct nf_ct_h323_master *info = nfct_help_data(ct);
1498 	int dir = CTINFO2DIR(ctinfo);
1499 	__be16 port;
1500 	union nf_inet_addr addr;
1501 	typeof(set_h225_addr_hook) set_h225_addr;
1502 
1503 	pr_debug("nf_ct_ras: ARQ\n");
1504 
1505 	set_h225_addr = rcu_dereference(set_h225_addr_hook);
1506 	if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1507 	    get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1508 			  &addr, &port) &&
1509 	    !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1510 	    port == info->sig_port[dir] &&
1511 	    nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1512 	    set_h225_addr && ct->status & IPS_NAT_MASK) {
1513 		/* Answering ARQ */
1514 		return set_h225_addr(skb, protoff, data, 0,
1515 				     &arq->destCallSignalAddress,
1516 				     &ct->tuplehash[!dir].tuple.dst.u3,
1517 				     info->sig_port[!dir]);
1518 	}
1519 
1520 	if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1521 	    get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1522 			  &addr, &port) &&
1523 	    !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1524 	    set_h225_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1525 	    ct->status & IPS_NAT_MASK) {
1526 		/* Calling ARQ */
1527 		return set_h225_addr(skb, protoff, data, 0,
1528 				     &arq->srcCallSignalAddress,
1529 				     &ct->tuplehash[!dir].tuple.dst.u3,
1530 				     port);
1531 	}
1532 
1533 	return 0;
1534 }
1535 
process_acf(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,AdmissionConfirm * acf)1536 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1537 		       enum ip_conntrack_info ctinfo,
1538 		       unsigned int protoff,
1539 		       unsigned char **data, AdmissionConfirm *acf)
1540 {
1541 	int dir = CTINFO2DIR(ctinfo);
1542 	int ret = 0;
1543 	__be16 port;
1544 	union nf_inet_addr addr;
1545 	struct nf_conntrack_expect *exp;
1546 	typeof(set_sig_addr_hook) set_sig_addr;
1547 
1548 	pr_debug("nf_ct_ras: ACF\n");
1549 
1550 	if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1551 			   &addr, &port))
1552 		return 0;
1553 
1554 	if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1555 		/* Answering ACF */
1556 		set_sig_addr = rcu_dereference(set_sig_addr_hook);
1557 		if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1558 		    ct->status & IPS_NAT_MASK)
1559 			return set_sig_addr(skb, ct, ctinfo, protoff, data,
1560 					    &acf->destCallSignalAddress, 1);
1561 		return 0;
1562 	}
1563 
1564 	/* Need new expect */
1565 	if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1566 		return -1;
1567 	nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1568 			  &ct->tuplehash[!dir].tuple.src.u3, &addr,
1569 			  IPPROTO_TCP, NULL, &port);
1570 	exp->flags = NF_CT_EXPECT_PERMANENT;
1571 	exp->helper = nf_conntrack_helper_q931;
1572 
1573 	if (nf_ct_expect_related(exp, 0) == 0) {
1574 		pr_debug("nf_ct_ras: expect Q.931 ");
1575 		nf_ct_dump_tuple(&exp->tuple);
1576 	} else
1577 		ret = -1;
1578 
1579 	nf_ct_expect_put(exp);
1580 
1581 	return ret;
1582 }
1583 
process_lrq(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,LocationRequest * lrq)1584 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1585 		       enum ip_conntrack_info ctinfo,
1586 		       unsigned int protoff,
1587 		       unsigned char **data, LocationRequest *lrq)
1588 {
1589 	typeof(set_ras_addr_hook) set_ras_addr;
1590 
1591 	pr_debug("nf_ct_ras: LRQ\n");
1592 
1593 	set_ras_addr = rcu_dereference(set_ras_addr_hook);
1594 	if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1595 	    ct->status & IPS_NAT_MASK)
1596 		return set_ras_addr(skb, ct, ctinfo, protoff, data,
1597 				    &lrq->replyAddress, 1);
1598 	return 0;
1599 }
1600 
process_lcf(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,LocationConfirm * lcf)1601 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1602 		       enum ip_conntrack_info ctinfo,
1603 		       unsigned int protoff,
1604 		       unsigned char **data, LocationConfirm *lcf)
1605 {
1606 	int dir = CTINFO2DIR(ctinfo);
1607 	int ret = 0;
1608 	__be16 port;
1609 	union nf_inet_addr addr;
1610 	struct nf_conntrack_expect *exp;
1611 
1612 	pr_debug("nf_ct_ras: LCF\n");
1613 
1614 	if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1615 			   &addr, &port))
1616 		return 0;
1617 
1618 	/* Need new expect for call signal */
1619 	if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1620 		return -1;
1621 	nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1622 			  &ct->tuplehash[!dir].tuple.src.u3, &addr,
1623 			  IPPROTO_TCP, NULL, &port);
1624 	exp->flags = NF_CT_EXPECT_PERMANENT;
1625 	exp->helper = nf_conntrack_helper_q931;
1626 
1627 	if (nf_ct_expect_related(exp, 0) == 0) {
1628 		pr_debug("nf_ct_ras: expect Q.931 ");
1629 		nf_ct_dump_tuple(&exp->tuple);
1630 	} else
1631 		ret = -1;
1632 
1633 	nf_ct_expect_put(exp);
1634 
1635 	/* Ignore rasAddress */
1636 
1637 	return ret;
1638 }
1639 
process_irr(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,InfoRequestResponse * irr)1640 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1641 		       enum ip_conntrack_info ctinfo,
1642 		       unsigned int protoff,
1643 		       unsigned char **data, InfoRequestResponse *irr)
1644 {
1645 	int ret;
1646 	typeof(set_ras_addr_hook) set_ras_addr;
1647 	typeof(set_sig_addr_hook) set_sig_addr;
1648 
1649 	pr_debug("nf_ct_ras: IRR\n");
1650 
1651 	set_ras_addr = rcu_dereference(set_ras_addr_hook);
1652 	if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1653 	    ct->status & IPS_NAT_MASK) {
1654 		ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1655 				   &irr->rasAddress, 1);
1656 		if (ret < 0)
1657 			return -1;
1658 	}
1659 
1660 	set_sig_addr = rcu_dereference(set_sig_addr_hook);
1661 	if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1662 	    ct->status & IPS_NAT_MASK) {
1663 		ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1664 					irr->callSignalAddress.item,
1665 					irr->callSignalAddress.count);
1666 		if (ret < 0)
1667 			return -1;
1668 	}
1669 
1670 	return 0;
1671 }
1672 
process_ras(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,unsigned int protoff,unsigned char ** data,RasMessage * ras)1673 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1674 		       enum ip_conntrack_info ctinfo,
1675 		       unsigned int protoff,
1676 		       unsigned char **data, RasMessage *ras)
1677 {
1678 	switch (ras->choice) {
1679 	case eRasMessage_gatekeeperRequest:
1680 		return process_grq(skb, ct, ctinfo, protoff, data,
1681 				   &ras->gatekeeperRequest);
1682 	case eRasMessage_gatekeeperConfirm:
1683 		return process_gcf(skb, ct, ctinfo, protoff, data,
1684 				   &ras->gatekeeperConfirm);
1685 	case eRasMessage_registrationRequest:
1686 		return process_rrq(skb, ct, ctinfo, protoff, data,
1687 				   &ras->registrationRequest);
1688 	case eRasMessage_registrationConfirm:
1689 		return process_rcf(skb, ct, ctinfo, protoff, data,
1690 				   &ras->registrationConfirm);
1691 	case eRasMessage_unregistrationRequest:
1692 		return process_urq(skb, ct, ctinfo, protoff, data,
1693 				   &ras->unregistrationRequest);
1694 	case eRasMessage_admissionRequest:
1695 		return process_arq(skb, ct, ctinfo, protoff, data,
1696 				   &ras->admissionRequest);
1697 	case eRasMessage_admissionConfirm:
1698 		return process_acf(skb, ct, ctinfo, protoff, data,
1699 				   &ras->admissionConfirm);
1700 	case eRasMessage_locationRequest:
1701 		return process_lrq(skb, ct, ctinfo, protoff, data,
1702 				   &ras->locationRequest);
1703 	case eRasMessage_locationConfirm:
1704 		return process_lcf(skb, ct, ctinfo, protoff, data,
1705 				   &ras->locationConfirm);
1706 	case eRasMessage_infoRequestResponse:
1707 		return process_irr(skb, ct, ctinfo, protoff, data,
1708 				   &ras->infoRequestResponse);
1709 	default:
1710 		pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1711 		break;
1712 	}
1713 
1714 	return 0;
1715 }
1716 
ras_help(struct sk_buff * skb,unsigned int protoff,struct nf_conn * ct,enum ip_conntrack_info ctinfo)1717 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1718 		    struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1719 {
1720 	static RasMessage ras;
1721 	unsigned char *data;
1722 	int datalen = 0;
1723 	int ret;
1724 
1725 	pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1726 
1727 	spin_lock_bh(&nf_h323_lock);
1728 
1729 	/* Get UDP data */
1730 	data = get_udp_data(skb, protoff, &datalen);
1731 	if (data == NULL)
1732 		goto accept;
1733 	pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1734 	nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1735 
1736 	/* Decode RAS message */
1737 	ret = DecodeRasMessage(data, datalen, &ras);
1738 	if (ret < 0) {
1739 		pr_debug("nf_ct_ras: decoding error: %s\n",
1740 			 ret == H323_ERROR_BOUND ?
1741 			 "out of bound" : "out of range");
1742 		goto accept;
1743 	}
1744 
1745 	/* Process RAS message */
1746 	if (process_ras(skb, ct, ctinfo, protoff, &data, &ras) < 0)
1747 		goto drop;
1748 
1749       accept:
1750 	spin_unlock_bh(&nf_h323_lock);
1751 	return NF_ACCEPT;
1752 
1753       drop:
1754 	spin_unlock_bh(&nf_h323_lock);
1755 	nf_ct_helper_log(skb, ct, "cannot process RAS message");
1756 	return NF_DROP;
1757 }
1758 
1759 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1760 	.max_expected		= 32,
1761 	.timeout		= 240,
1762 };
1763 
1764 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1765 	{
1766 		.name			= "RAS",
1767 		.me			= THIS_MODULE,
1768 		.tuple.src.l3num	= AF_INET,
1769 		.tuple.src.u.udp.port	= cpu_to_be16(RAS_PORT),
1770 		.tuple.dst.protonum	= IPPROTO_UDP,
1771 		.help			= ras_help,
1772 		.expect_policy		= &ras_exp_policy,
1773 	},
1774 	{
1775 		.name			= "RAS",
1776 		.me			= THIS_MODULE,
1777 		.tuple.src.l3num	= AF_INET6,
1778 		.tuple.src.u.udp.port	= cpu_to_be16(RAS_PORT),
1779 		.tuple.dst.protonum	= IPPROTO_UDP,
1780 		.help			= ras_help,
1781 		.expect_policy		= &ras_exp_policy,
1782 	},
1783 };
1784 
h323_helper_init(void)1785 static int __init h323_helper_init(void)
1786 {
1787 	int ret;
1788 
1789 	ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1790 	if (ret < 0)
1791 		return ret;
1792 	ret = nf_conntrack_helpers_register(nf_conntrack_helper_q931,
1793 					ARRAY_SIZE(nf_conntrack_helper_q931));
1794 	if (ret < 0)
1795 		goto err1;
1796 	ret = nf_conntrack_helpers_register(nf_conntrack_helper_ras,
1797 					ARRAY_SIZE(nf_conntrack_helper_ras));
1798 	if (ret < 0)
1799 		goto err2;
1800 
1801 	return 0;
1802 err2:
1803 	nf_conntrack_helpers_unregister(nf_conntrack_helper_q931,
1804 					ARRAY_SIZE(nf_conntrack_helper_q931));
1805 err1:
1806 	nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1807 	return ret;
1808 }
1809 
h323_helper_exit(void)1810 static void __exit h323_helper_exit(void)
1811 {
1812 	nf_conntrack_helpers_unregister(nf_conntrack_helper_ras,
1813 					ARRAY_SIZE(nf_conntrack_helper_ras));
1814 	nf_conntrack_helpers_unregister(nf_conntrack_helper_q931,
1815 					ARRAY_SIZE(nf_conntrack_helper_q931));
1816 	nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1817 }
1818 
nf_conntrack_h323_fini(void)1819 static void __exit nf_conntrack_h323_fini(void)
1820 {
1821 	h323_helper_exit();
1822 	kfree(h323_buffer);
1823 	pr_debug("nf_ct_h323: fini\n");
1824 }
1825 
nf_conntrack_h323_init(void)1826 static int __init nf_conntrack_h323_init(void)
1827 {
1828 	int ret;
1829 
1830 	NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_h323_master));
1831 
1832 	h323_buffer = kmalloc(H323_MAX_SIZE + 1, GFP_KERNEL);
1833 	if (!h323_buffer)
1834 		return -ENOMEM;
1835 	ret = h323_helper_init();
1836 	if (ret < 0)
1837 		goto err1;
1838 	pr_debug("nf_ct_h323: init success\n");
1839 	return 0;
1840 err1:
1841 	kfree(h323_buffer);
1842 	return ret;
1843 }
1844 
1845 module_init(nf_conntrack_h323_init);
1846 module_exit(nf_conntrack_h323_fini);
1847 
1848 EXPORT_SYMBOL_GPL(get_h225_addr);
1849 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1850 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1851 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1852 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1853 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1854 EXPORT_SYMBOL_GPL(nat_t120_hook);
1855 EXPORT_SYMBOL_GPL(nat_h245_hook);
1856 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1857 EXPORT_SYMBOL_GPL(nat_q931_hook);
1858 
1859 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1860 MODULE_DESCRIPTION("H.323 connection tracking helper");
1861 MODULE_LICENSE("GPL");
1862 MODULE_ALIAS("ip_conntrack_h323");
1863 MODULE_ALIAS_NFCT_HELPER("RAS");
1864 MODULE_ALIAS_NFCT_HELPER("Q.931");
1865 MODULE_ALIAS_NFCT_HELPER("H.245");
1866