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