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