xref: /linux/net/netfilter/nf_conntrack_sip.c (revision 06bc7ff0a1e0f2b0102e1314e3527a7ec0997851)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* SIP extension for IP connection tracking.
3  *
4  * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
5  * based on RR's ip_conntrack_ftp.c and other modules.
6  * (C) 2007 United Security Providers
7  * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/module.h>
13 #include <linux/ctype.h>
14 #include <linux/skbuff.h>
15 #include <linux/inet.h>
16 #include <linux/in.h>
17 #include <linux/udp.h>
18 #include <linux/tcp.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter_ipv6.h>
22 
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/nf_conntrack_expect.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_zones.h>
28 #include <linux/netfilter/nf_conntrack_sip.h>
29 
30 #define HELPER_NAME "sip"
31 
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
34 MODULE_DESCRIPTION("SIP connection tracking helper");
35 MODULE_ALIAS("ip_conntrack_sip");
36 MODULE_ALIAS_NFCT_HELPER(HELPER_NAME);
37 
38 #define MAX_PORTS	8
39 static unsigned short ports[MAX_PORTS];
40 static unsigned int ports_c;
41 module_param_array(ports, ushort, &ports_c, 0400);
42 MODULE_PARM_DESC(ports, "port numbers of SIP servers");
43 
44 static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
45 module_param(sip_timeout, uint, 0600);
46 MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
47 
48 static int sip_direct_signalling __read_mostly = 1;
49 module_param(sip_direct_signalling, int, 0600);
50 MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
51 					"only (default 1)");
52 
53 static int sip_direct_media __read_mostly = 1;
54 module_param(sip_direct_media, int, 0600);
55 MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
56 				   "endpoints only (default 1)");
57 
58 static int sip_external_media __read_mostly = 0;
59 module_param(sip_external_media, int, 0600);
60 MODULE_PARM_DESC(sip_external_media, "Expect Media streams between external "
61 				     "endpoints (default 0)");
62 
63 const struct nf_nat_sip_hooks __rcu *nf_nat_sip_hooks;
64 EXPORT_SYMBOL_GPL(nf_nat_sip_hooks);
65 
string_len(const struct nf_conn * ct,const char * dptr,const char * limit,int * shift)66 static int string_len(const struct nf_conn *ct, const char *dptr,
67 		      const char *limit, int *shift)
68 {
69 	int len = 0;
70 
71 	while (dptr < limit && isalpha(*dptr)) {
72 		dptr++;
73 		len++;
74 	}
75 	return len;
76 }
77 
digits_len(const struct nf_conn * ct,const char * dptr,const char * limit,int * shift)78 static int digits_len(const struct nf_conn *ct, const char *dptr,
79 		      const char *limit, int *shift)
80 {
81 	int len = 0;
82 	while (dptr < limit && isdigit(*dptr)) {
83 		dptr++;
84 		len++;
85 	}
86 	return len;
87 }
88 
iswordc(const char c)89 static int iswordc(const char c)
90 {
91 	if (isalnum(c) || c == '!' || c == '"' || c == '%' ||
92 	    (c >= '(' && c <= '+') || c == ':' || c == '<' || c == '>' ||
93 	    c == '?' || (c >= '[' && c <= ']') || c == '_' || c == '`' ||
94 	    c == '{' || c == '}' || c == '~' || (c >= '-' && c <= '/') ||
95 	    c == '\'')
96 		return 1;
97 	return 0;
98 }
99 
word_len(const char * dptr,const char * limit)100 static int word_len(const char *dptr, const char *limit)
101 {
102 	int len = 0;
103 	while (dptr < limit && iswordc(*dptr)) {
104 		dptr++;
105 		len++;
106 	}
107 	return len;
108 }
109 
callid_len(const struct nf_conn * ct,const char * dptr,const char * limit,int * shift)110 static int callid_len(const struct nf_conn *ct, const char *dptr,
111 		      const char *limit, int *shift)
112 {
113 	int len, domain_len;
114 
115 	len = word_len(dptr, limit);
116 	dptr += len;
117 	if (!len || dptr == limit || *dptr != '@')
118 		return len;
119 	dptr++;
120 	len++;
121 
122 	domain_len = word_len(dptr, limit);
123 	if (!domain_len)
124 		return 0;
125 	return len + domain_len;
126 }
127 
128 /* get media type + port length */
media_len(const struct nf_conn * ct,const char * dptr,const char * limit,int * shift)129 static int media_len(const struct nf_conn *ct, const char *dptr,
130 		     const char *limit, int *shift)
131 {
132 	int len = string_len(ct, dptr, limit, shift);
133 
134 	dptr += len;
135 	if (dptr >= limit || *dptr != ' ')
136 		return 0;
137 	len++;
138 	dptr++;
139 
140 	return len + digits_len(ct, dptr, limit, shift);
141 }
142 
sip_parse_addr(const struct nf_conn * ct,const char * cp,const char ** endp,union nf_inet_addr * addr,const char * limit,bool delim)143 static int sip_parse_addr(const struct nf_conn *ct, const char *cp,
144 			  const char **endp, union nf_inet_addr *addr,
145 			  const char *limit, bool delim)
146 {
147 	const char *end;
148 	int ret;
149 
150 	if (!ct)
151 		return 0;
152 
153 	memset(addr, 0, sizeof(*addr));
154 	switch (nf_ct_l3num(ct)) {
155 	case AF_INET:
156 		ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
157 		if (ret == 0)
158 			return 0;
159 		break;
160 	case AF_INET6:
161 		if (cp < limit && *cp == '[')
162 			cp++;
163 		else if (delim)
164 			return 0;
165 
166 		ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
167 		if (ret == 0)
168 			return 0;
169 
170 		if (end < limit && *end == ']')
171 			end++;
172 		else if (delim)
173 			return 0;
174 		break;
175 	default:
176 		BUG();
177 	}
178 
179 	if (endp)
180 		*endp = end;
181 	return 1;
182 }
183 
184 /* Parse optional port number after IP address.
185  * Returns false on malformed input, true otherwise.
186  * If port is non-NULL, stores parsed port in network byte order.
187  * If no port is present, sets *port to default SIP port.
188  */
sip_parse_port(const char * dptr,const char ** endp,const char * limit,__be16 * port)189 static bool sip_parse_port(const char *dptr, const char **endp,
190 			   const char *limit, __be16 *port)
191 {
192 	unsigned int p = 0;
193 	int len = 0;
194 
195 	if (dptr >= limit)
196 		return false;
197 
198 	if (*dptr != ':') {
199 		if (port)
200 			*port = htons(SIP_PORT);
201 		if (endp)
202 			*endp = dptr;
203 		return true;
204 	}
205 
206 	dptr++; /* skip ':' */
207 
208 	while (dptr < limit && isdigit(*dptr)) {
209 		p = p * 10 + (*dptr - '0');
210 		dptr++;
211 		len++;
212 		if (len > 5) /* max "65535" */
213 			return false;
214 	}
215 
216 	if (len == 0)
217 		return false;
218 
219 	/* reached limit while parsing port */
220 	if (dptr >= limit)
221 		return false;
222 
223 	if (p < 1024 || p > 65535)
224 		return false;
225 
226 	if (port)
227 		*port = htons(p);
228 
229 	if (endp)
230 		*endp = dptr;
231 
232 	return true;
233 }
234 
235 /* skip ip address. returns its length. */
epaddr_len(const struct nf_conn * ct,const char * dptr,const char * limit,int * shift)236 static int epaddr_len(const struct nf_conn *ct, const char *dptr,
237 		      const char *limit, int *shift)
238 {
239 	union nf_inet_addr addr;
240 	const char *aux = dptr;
241 
242 	if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit, true)) {
243 		pr_debug("ip: %s parse failed.!\n", dptr);
244 		return 0;
245 	}
246 
247 	if (!sip_parse_port(dptr, &dptr, limit, NULL))
248 		return 0;
249 	return dptr - aux;
250 }
251 
252 /* get address length, skiping user info. */
skp_epaddr_len(const struct nf_conn * ct,const char * dptr,const char * limit,int * shift)253 static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
254 			  const char *limit, int *shift)
255 {
256 	const char *start = dptr;
257 	int s = *shift;
258 
259 	/* Search for @, but stop at the end of the line.
260 	 * We are inside a sip: URI, so we don't need to worry about
261 	 * continuation lines. */
262 	while (dptr < limit &&
263 	       *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
264 		(*shift)++;
265 		dptr++;
266 	}
267 
268 	if (dptr < limit && *dptr == '@') {
269 		dptr++;
270 		(*shift)++;
271 	} else {
272 		dptr = start;
273 		*shift = s;
274 	}
275 
276 	return epaddr_len(ct, dptr, limit, shift);
277 }
278 
279 /* simple_strtoul stops after first non-number character.
280  * But as we're not dealing with c-strings, we can't rely on
281  * hitting \r,\n,\0 etc. before moving past end of buffer.
282  *
283  * This is a variant of simple_strtoul, but doesn't require
284  * a c-string.
285  *
286  * If value exceeds UINT_MAX, 0 is returned.
287  */
sip_strtouint(const char * cp,unsigned int len,char ** endp)288 static unsigned int sip_strtouint(const char *cp, unsigned int len, char **endp)
289 {
290 	const unsigned int max = sizeof("4294967295");
291 	unsigned int olen = len;
292 	const char *s = cp;
293 	u64 result = 0;
294 
295 	if (len > max)
296 		len = max;
297 
298 	while (olen > 0 && isdigit(*s)) {
299 		unsigned int value;
300 
301 		if (len == 0)
302 			goto err;
303 
304 		value = *s - '0';
305 		result = result * 10 + value;
306 
307 		if (result > UINT_MAX)
308 			goto err;
309 		s++;
310 		len--;
311 		olen--;
312 	}
313 
314 	if (endp)
315 		*endp = (char *)s;
316 
317 	return result;
318 err:
319 	if (endp)
320 		*endp = (char *)cp;
321 	return 0;
322 }
323 
324 /* Parse a SIP request line of the form:
325  *
326  * Request-Line = Method SP Request-URI SP SIP-Version CRLF
327  *
328  * and return the offset and length of the address contained in the Request-URI.
329  */
ct_sip_parse_request(const struct nf_conn * ct,const char * dptr,unsigned int datalen,unsigned int * matchoff,unsigned int * matchlen,union nf_inet_addr * addr,__be16 * port)330 int ct_sip_parse_request(const struct nf_conn *ct,
331 			 const char *dptr, unsigned int datalen,
332 			 unsigned int *matchoff, unsigned int *matchlen,
333 			 union nf_inet_addr *addr, __be16 *port)
334 {
335 	const char *start = dptr, *limit = dptr + datalen, *end;
336 	unsigned int mlen;
337 	int shift = 0;
338 
339 	/* Skip method and following whitespace */
340 	mlen = string_len(ct, dptr, limit, NULL);
341 	if (!mlen)
342 		return 0;
343 	dptr += mlen;
344 	if (++dptr >= limit)
345 		return 0;
346 
347 	/* Find SIP URI */
348 	for (; dptr < limit - strlen("sip:"); dptr++) {
349 		if (*dptr == '\r' || *dptr == '\n')
350 			return -1;
351 		if (strncasecmp(dptr, "sip:", strlen("sip:")) == 0) {
352 			dptr += strlen("sip:");
353 			break;
354 		}
355 	}
356 	if (!skp_epaddr_len(ct, dptr, limit, &shift))
357 		return 0;
358 	dptr += shift;
359 
360 	if (!sip_parse_addr(ct, dptr, &end, addr, limit, true))
361 		return -1;
362 	if (!sip_parse_port(end, &end, limit, port))
363 		return -1;
364 
365 	if (end == dptr)
366 		return 0;
367 	*matchoff = dptr - start;
368 	*matchlen = end - dptr;
369 	return 1;
370 }
371 EXPORT_SYMBOL_GPL(ct_sip_parse_request);
372 
373 /* SIP header parsing: SIP headers are located at the beginning of a line, but
374  * may span several lines, in which case the continuation lines begin with a
375  * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
376  * CRLF, RFC 3261 allows only CRLF, we support both.
377  *
378  * Headers are followed by (optionally) whitespace, a colon, again (optionally)
379  * whitespace and the values. Whitespace in this context means any amount of
380  * tabs, spaces and continuation lines, which are treated as a single whitespace
381  * character.
382  *
383  * Some headers may appear multiple times. A comma separated list of values is
384  * equivalent to multiple headers.
385  */
386 static const struct sip_header ct_sip_hdrs[] = {
387 	[SIP_HDR_CSEQ]			= SIP_HDR("CSeq", NULL, NULL, digits_len),
388 	[SIP_HDR_FROM]			= SIP_HDR("From", "f", "sip:", skp_epaddr_len),
389 	[SIP_HDR_TO]			= SIP_HDR("To", "t", "sip:", skp_epaddr_len),
390 	[SIP_HDR_CONTACT]		= SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
391 	[SIP_HDR_VIA_UDP]		= SIP_HDR("Via", "v", "UDP ", epaddr_len),
392 	[SIP_HDR_VIA_TCP]		= SIP_HDR("Via", "v", "TCP ", epaddr_len),
393 	[SIP_HDR_EXPIRES]		= SIP_HDR("Expires", NULL, NULL, digits_len),
394 	[SIP_HDR_CONTENT_LENGTH]	= SIP_HDR("Content-Length", "l", NULL, digits_len),
395 	[SIP_HDR_CALL_ID]		= SIP_HDR("Call-Id", "i", NULL, callid_len),
396 };
397 
sip_follow_continuation(const char * dptr,const char * limit)398 static const char *sip_follow_continuation(const char *dptr, const char *limit)
399 {
400 	/* Walk past newline */
401 	if (++dptr >= limit)
402 		return NULL;
403 
404 	/* Skip '\n' in CR LF */
405 	if (*(dptr - 1) == '\r' && *dptr == '\n') {
406 		if (++dptr >= limit)
407 			return NULL;
408 	}
409 
410 	/* Continuation line? */
411 	if (*dptr != ' ' && *dptr != '\t')
412 		return NULL;
413 
414 	/* skip leading whitespace */
415 	for (; dptr < limit; dptr++) {
416 		if (*dptr != ' ' && *dptr != '\t')
417 			break;
418 	}
419 	return dptr;
420 }
421 
sip_skip_whitespace(const char * dptr,const char * limit)422 static const char *sip_skip_whitespace(const char *dptr, const char *limit)
423 {
424 	for (; dptr < limit; dptr++) {
425 		if (*dptr == ' ' || *dptr == '\t')
426 			continue;
427 		if (*dptr != '\r' && *dptr != '\n')
428 			break;
429 		dptr = sip_follow_continuation(dptr, limit);
430 		break;
431 	}
432 	return dptr;
433 }
434 
435 /* Search within a SIP header value, dealing with continuation lines */
ct_sip_header_search(const char * dptr,const char * limit,const char * needle,unsigned int len)436 static const char *ct_sip_header_search(const char *dptr, const char *limit,
437 					const char *needle, unsigned int len)
438 {
439 	for (limit -= len; dptr < limit; dptr++) {
440 		if (*dptr == '\r' || *dptr == '\n') {
441 			dptr = sip_follow_continuation(dptr, limit);
442 			if (dptr == NULL)
443 				break;
444 			continue;
445 		}
446 
447 		if (strncasecmp(dptr, needle, len) == 0)
448 			return dptr;
449 	}
450 	return NULL;
451 }
452 
ct_sip_get_header(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,enum sip_header_types type,unsigned int * matchoff,unsigned int * matchlen)453 int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
454 		      unsigned int dataoff, unsigned int datalen,
455 		      enum sip_header_types type,
456 		      unsigned int *matchoff, unsigned int *matchlen)
457 {
458 	const struct sip_header *hdr = &ct_sip_hdrs[type];
459 	const char *start = dptr, *limit = dptr + datalen;
460 	int shift = 0;
461 
462 	for (dptr += dataoff; dptr < limit; dptr++) {
463 		/* Find beginning of line */
464 		if (*dptr != '\r' && *dptr != '\n')
465 			continue;
466 		if (++dptr >= limit)
467 			break;
468 		if (*(dptr - 1) == '\r' && *dptr == '\n') {
469 			if (++dptr >= limit)
470 				break;
471 		}
472 
473 		/* Skip continuation lines */
474 		if (*dptr == ' ' || *dptr == '\t')
475 			continue;
476 
477 		/* Find header. Compact headers must be followed by a
478 		 * non-alphabetic character to avoid mismatches. */
479 		if (limit - dptr >= hdr->len &&
480 		    strncasecmp(dptr, hdr->name, hdr->len) == 0)
481 			dptr += hdr->len;
482 		else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
483 			 strncasecmp(dptr, hdr->cname, hdr->clen) == 0 &&
484 			 !isalpha(*(dptr + hdr->clen)))
485 			dptr += hdr->clen;
486 		else
487 			continue;
488 
489 		/* Find and skip colon */
490 		dptr = sip_skip_whitespace(dptr, limit);
491 		if (dptr == NULL)
492 			break;
493 		if (*dptr != ':' || ++dptr >= limit)
494 			break;
495 
496 		/* Skip whitespace after colon */
497 		dptr = sip_skip_whitespace(dptr, limit);
498 		if (dptr == NULL)
499 			break;
500 
501 		*matchoff = dptr - start;
502 		if (hdr->search) {
503 			dptr = ct_sip_header_search(dptr, limit, hdr->search,
504 						    hdr->slen);
505 			if (!dptr)
506 				return -1;
507 			dptr += hdr->slen;
508 		}
509 
510 		*matchlen = hdr->match_len(ct, dptr, limit, &shift);
511 		if (!*matchlen)
512 			return -1;
513 		*matchoff = dptr - start + shift;
514 		return 1;
515 	}
516 	return 0;
517 }
518 EXPORT_SYMBOL_GPL(ct_sip_get_header);
519 
520 /* Get next header field in a list of comma separated values */
ct_sip_next_header(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,enum sip_header_types type,unsigned int * matchoff,unsigned int * matchlen)521 static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
522 			      unsigned int dataoff, unsigned int datalen,
523 			      enum sip_header_types type,
524 			      unsigned int *matchoff, unsigned int *matchlen)
525 {
526 	const struct sip_header *hdr = &ct_sip_hdrs[type];
527 	const char *start = dptr, *limit = dptr + datalen;
528 	int shift = 0;
529 
530 	dptr += dataoff;
531 
532 	dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
533 	if (!dptr)
534 		return 0;
535 
536 	dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
537 	if (!dptr)
538 		return 0;
539 	dptr += hdr->slen;
540 
541 	*matchoff = dptr - start;
542 	*matchlen = hdr->match_len(ct, dptr, limit, &shift);
543 	if (!*matchlen)
544 		return -1;
545 	*matchoff += shift;
546 	return 1;
547 }
548 
549 /* Walk through headers until a parsable one is found or no header of the
550  * given type is left. */
ct_sip_walk_headers(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,enum sip_header_types type,int * in_header,unsigned int * matchoff,unsigned int * matchlen)551 static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
552 			       unsigned int dataoff, unsigned int datalen,
553 			       enum sip_header_types type, int *in_header,
554 			       unsigned int *matchoff, unsigned int *matchlen)
555 {
556 	int ret;
557 
558 	if (in_header && *in_header) {
559 		while (1) {
560 			ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
561 						 type, matchoff, matchlen);
562 			if (ret > 0)
563 				return ret;
564 			if (ret == 0)
565 				break;
566 			dataoff = *matchoff;
567 		}
568 		*in_header = 0;
569 	}
570 
571 	while (1) {
572 		ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
573 					type, matchoff, matchlen);
574 		if (ret > 0)
575 			break;
576 		if (ret == 0)
577 			return ret;
578 		dataoff = *matchoff;
579 	}
580 
581 	if (in_header)
582 		*in_header = 1;
583 	return 1;
584 }
585 
586 /* Locate a SIP header, parse the URI and return the offset and length of
587  * the address as well as the address and port themselves. A stream of
588  * headers can be parsed by handing in a non-NULL datalen and in_header
589  * pointer.
590  */
ct_sip_parse_header_uri(const struct nf_conn * ct,const char * dptr,unsigned int * dataoff,unsigned int datalen,enum sip_header_types type,int * in_header,unsigned int * matchoff,unsigned int * matchlen,union nf_inet_addr * addr,__be16 * port)591 int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
592 			    unsigned int *dataoff, unsigned int datalen,
593 			    enum sip_header_types type, int *in_header,
594 			    unsigned int *matchoff, unsigned int *matchlen,
595 			    union nf_inet_addr *addr, __be16 *port)
596 {
597 	const char *c, *limit = dptr + datalen;
598 	int ret;
599 
600 	ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
601 				  type, in_header, matchoff, matchlen);
602 	WARN_ON(ret < 0);
603 	if (ret == 0)
604 		return ret;
605 
606 	if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true))
607 		return -1;
608 	if (!sip_parse_port(c, &c, limit, port))
609 		return -1;
610 
611 	if (dataoff)
612 		*dataoff = c - dptr;
613 	return 1;
614 }
615 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
616 
ct_sip_parse_param(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,const char * name,unsigned int * matchoff,unsigned int * matchlen)617 static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
618 			      unsigned int dataoff, unsigned int datalen,
619 			      const char *name,
620 			      unsigned int *matchoff, unsigned int *matchlen)
621 {
622 	const char *limit = dptr + datalen;
623 	const char *start;
624 	const char *end;
625 
626 	limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
627 	if (!limit)
628 		limit = dptr + datalen;
629 
630 	start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
631 	if (!start)
632 		return 0;
633 	start += strlen(name);
634 
635 	end = ct_sip_header_search(start, limit, ";", strlen(";"));
636 	if (!end)
637 		end = limit;
638 
639 	*matchoff = start - dptr;
640 	*matchlen = end - start;
641 	return 1;
642 }
643 
644 /* Parse address from header parameter and return address, offset and length */
ct_sip_parse_address_param(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,const char * name,unsigned int * matchoff,unsigned int * matchlen,union nf_inet_addr * addr,bool delim)645 int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
646 			       unsigned int dataoff, unsigned int datalen,
647 			       const char *name,
648 			       unsigned int *matchoff, unsigned int *matchlen,
649 			       union nf_inet_addr *addr, bool delim)
650 {
651 	const char *limit = dptr + datalen;
652 	const char *start, *end;
653 
654 	limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
655 	if (!limit)
656 		limit = dptr + datalen;
657 
658 	start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
659 	if (!start)
660 		return 0;
661 
662 	start += strlen(name);
663 	if (!sip_parse_addr(ct, start, &end, addr, limit, delim))
664 		return 0;
665 	*matchoff = start - dptr;
666 	*matchlen = end - start;
667 	return 1;
668 }
669 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
670 
671 /* Parse numerical header parameter and return value, offset and length */
ct_sip_parse_numerical_param(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,const char * name,unsigned int * matchoff,unsigned int * matchlen,unsigned int * val)672 int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
673 				 unsigned int dataoff, unsigned int datalen,
674 				 const char *name,
675 				 unsigned int *matchoff, unsigned int *matchlen,
676 				 unsigned int *val)
677 {
678 	const char *limit = dptr + datalen;
679 	const char *start;
680 	char *end;
681 
682 	limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
683 	if (!limit)
684 		limit = dptr + datalen;
685 
686 	start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
687 	if (!start)
688 		return 0;
689 
690 	start += strlen(name);
691 	*val = sip_strtouint(start, limit - start, (char **)&end);
692 	if (start == end)
693 		return -1;
694 	if (matchoff && matchlen) {
695 		*matchoff = start - dptr;
696 		*matchlen = end - start;
697 	}
698 	return 1;
699 }
700 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
701 
ct_sip_parse_transport(struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,u8 * proto)702 static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
703 				  unsigned int dataoff, unsigned int datalen,
704 				  u8 *proto)
705 {
706 	unsigned int matchoff, matchlen;
707 
708 	if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
709 			       &matchoff, &matchlen)) {
710 		if (!strncasecmp(dptr + matchoff, "TCP", strlen("TCP")))
711 			*proto = IPPROTO_TCP;
712 		else if (!strncasecmp(dptr + matchoff, "UDP", strlen("UDP")))
713 			*proto = IPPROTO_UDP;
714 		else
715 			return 0;
716 
717 		if (*proto != nf_ct_protonum(ct))
718 			return 0;
719 	} else
720 		*proto = nf_ct_protonum(ct);
721 
722 	return 1;
723 }
724 
sdp_parse_addr(const struct nf_conn * ct,const char * cp,const char ** endp,union nf_inet_addr * addr,const char * limit)725 static int sdp_parse_addr(const struct nf_conn *ct, const char *cp,
726 			  const char **endp, union nf_inet_addr *addr,
727 			  const char *limit)
728 {
729 	const char *end;
730 	int ret;
731 
732 	memset(addr, 0, sizeof(*addr));
733 	switch (nf_ct_l3num(ct)) {
734 	case AF_INET:
735 		ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
736 		break;
737 	case AF_INET6:
738 		ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
739 		break;
740 	default:
741 		BUG();
742 	}
743 
744 	if (ret == 0)
745 		return 0;
746 	if (endp)
747 		*endp = end;
748 	return 1;
749 }
750 
751 /* skip ip address. returns its length. */
sdp_addr_len(const struct nf_conn * ct,const char * dptr,const char * limit,int * shift)752 static int sdp_addr_len(const struct nf_conn *ct, const char *dptr,
753 			const char *limit, int *shift)
754 {
755 	union nf_inet_addr addr;
756 	const char *aux = dptr;
757 
758 	if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) {
759 		pr_debug("ip: %s parse failed.!\n", dptr);
760 		return 0;
761 	}
762 
763 	return dptr - aux;
764 }
765 
766 /* SDP header parsing: a SDP session description contains an ordered set of
767  * headers, starting with a section containing general session parameters,
768  * optionally followed by multiple media descriptions.
769  *
770  * SDP headers always start at the beginning of a line. According to RFC 2327:
771  * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
772  * be tolerant and also accept records terminated with a single newline
773  * character". We handle both cases.
774  */
775 static const struct sip_header ct_sdp_hdrs_v4[] = {
776 	[SDP_HDR_VERSION]	= SDP_HDR("v=", NULL, digits_len),
777 	[SDP_HDR_OWNER]		= SDP_HDR("o=", "IN IP4 ", sdp_addr_len),
778 	[SDP_HDR_CONNECTION]	= SDP_HDR("c=", "IN IP4 ", sdp_addr_len),
779 	[SDP_HDR_MEDIA]		= SDP_HDR("m=", NULL, media_len),
780 };
781 
782 static const struct sip_header ct_sdp_hdrs_v6[] = {
783 	[SDP_HDR_VERSION]	= SDP_HDR("v=", NULL, digits_len),
784 	[SDP_HDR_OWNER]		= SDP_HDR("o=", "IN IP6 ", sdp_addr_len),
785 	[SDP_HDR_CONNECTION]	= SDP_HDR("c=", "IN IP6 ", sdp_addr_len),
786 	[SDP_HDR_MEDIA]		= SDP_HDR("m=", NULL, media_len),
787 };
788 
789 /* Linear string search within SDP header values */
ct_sdp_header_search(const char * dptr,const char * limit,const char * needle,unsigned int len)790 static const char *ct_sdp_header_search(const char *dptr, const char *limit,
791 					const char *needle, unsigned int len)
792 {
793 	for (limit -= len; dptr < limit; dptr++) {
794 		if (*dptr == '\r' || *dptr == '\n')
795 			break;
796 		if (strncmp(dptr, needle, len) == 0)
797 			return dptr;
798 	}
799 	return NULL;
800 }
801 
802 /* Locate a SDP header (optionally a substring within the header value),
803  * optionally stopping at the first occurrence of the term header, parse
804  * it and return the offset and length of the data we're interested in.
805  */
ct_sip_get_sdp_header(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,enum sdp_header_types type,enum sdp_header_types term,unsigned int * matchoff,unsigned int * matchlen)806 int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
807 			  unsigned int dataoff, unsigned int datalen,
808 			  enum sdp_header_types type,
809 			  enum sdp_header_types term,
810 			  unsigned int *matchoff, unsigned int *matchlen)
811 {
812 	const struct sip_header *hdrs, *hdr, *thdr;
813 	const char *start = dptr, *limit = dptr + datalen;
814 	int shift = 0;
815 
816 	hdrs = nf_ct_l3num(ct) == NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6;
817 	hdr = &hdrs[type];
818 	thdr = &hdrs[term];
819 
820 	for (dptr += dataoff; dptr < limit; dptr++) {
821 		/* Find beginning of line */
822 		if (*dptr != '\r' && *dptr != '\n')
823 			continue;
824 		if (++dptr >= limit)
825 			break;
826 		if (*(dptr - 1) == '\r' && *dptr == '\n') {
827 			if (++dptr >= limit)
828 				break;
829 		}
830 
831 		if (term != SDP_HDR_UNSPEC &&
832 		    limit - dptr >= thdr->len &&
833 		    strncasecmp(dptr, thdr->name, thdr->len) == 0)
834 			break;
835 		else if (limit - dptr >= hdr->len &&
836 			 strncasecmp(dptr, hdr->name, hdr->len) == 0)
837 			dptr += hdr->len;
838 		else
839 			continue;
840 
841 		*matchoff = dptr - start;
842 		if (hdr->search) {
843 			dptr = ct_sdp_header_search(dptr, limit, hdr->search,
844 						    hdr->slen);
845 			if (!dptr)
846 				return -1;
847 			dptr += hdr->slen;
848 		}
849 
850 		*matchlen = hdr->match_len(ct, dptr, limit, &shift);
851 		if (!*matchlen)
852 			return -1;
853 		*matchoff = dptr - start + shift;
854 		return 1;
855 	}
856 	return 0;
857 }
858 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
859 
ct_sip_parse_sdp_addr(const struct nf_conn * ct,const char * dptr,unsigned int dataoff,unsigned int datalen,enum sdp_header_types type,enum sdp_header_types term,unsigned int * matchoff,unsigned int * matchlen,union nf_inet_addr * addr)860 static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
861 				 unsigned int dataoff, unsigned int datalen,
862 				 enum sdp_header_types type,
863 				 enum sdp_header_types term,
864 				 unsigned int *matchoff, unsigned int *matchlen,
865 				 union nf_inet_addr *addr)
866 {
867 	int ret;
868 
869 	ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
870 				    matchoff, matchlen);
871 	if (ret <= 0)
872 		return ret;
873 
874 	if (!sdp_parse_addr(ct, dptr + *matchoff, NULL, addr,
875 			    dptr + *matchoff + *matchlen))
876 		return -1;
877 	return 1;
878 }
879 
refresh_signalling_expectation(struct nf_conn * ct,union nf_inet_addr * addr,u8 proto,__be16 port,unsigned int expires)880 static int refresh_signalling_expectation(struct nf_conn *ct,
881 					  union nf_inet_addr *addr,
882 					  u8 proto, __be16 port,
883 					  unsigned int expires)
884 {
885 	struct nf_conn_help *help = nfct_help(ct);
886 	struct nf_conntrack_expect *exp;
887 	struct hlist_node *next;
888 	int found = 0;
889 
890 	spin_lock_bh(&nf_conntrack_expect_lock);
891 	hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
892 		if (exp->class != SIP_EXPECT_SIGNALLING ||
893 		    !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
894 		    exp->tuple.dst.protonum != proto ||
895 		    exp->tuple.dst.u.udp.port != port)
896 			continue;
897 		if (mod_timer_pending(&exp->timeout, jiffies + expires * HZ)) {
898 			exp->flags &= ~NF_CT_EXPECT_INACTIVE;
899 			found = 1;
900 			break;
901 		}
902 	}
903 	spin_unlock_bh(&nf_conntrack_expect_lock);
904 	return found;
905 }
906 
flush_expectations(struct nf_conn * ct,bool media)907 static void flush_expectations(struct nf_conn *ct, bool media)
908 {
909 	struct nf_conn_help *help = nfct_help(ct);
910 	struct nf_conntrack_expect *exp;
911 	struct hlist_node *next;
912 
913 	spin_lock_bh(&nf_conntrack_expect_lock);
914 	hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
915 		if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
916 			continue;
917 		if (!nf_ct_remove_expect(exp))
918 			continue;
919 		if (!media)
920 			break;
921 	}
922 	spin_unlock_bh(&nf_conntrack_expect_lock);
923 }
924 
set_expected_rtp_rtcp(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,union nf_inet_addr * daddr,__be16 port,enum sip_expectation_classes class,unsigned int mediaoff,unsigned int medialen)925 static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
926 				 unsigned int dataoff,
927 				 const char **dptr, unsigned int *datalen,
928 				 union nf_inet_addr *daddr, __be16 port,
929 				 enum sip_expectation_classes class,
930 				 unsigned int mediaoff, unsigned int medialen)
931 {
932 	struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
933 	enum ip_conntrack_info ctinfo;
934 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
935 	struct net *net = nf_ct_net(ct);
936 	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
937 	union nf_inet_addr *saddr;
938 	struct nf_conntrack_tuple tuple;
939 	int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
940 	u_int16_t base_port;
941 	__be16 rtp_port, rtcp_port;
942 	const struct nf_nat_sip_hooks *hooks;
943 
944 	saddr = NULL;
945 	if (sip_direct_media) {
946 		if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
947 			return NF_ACCEPT;
948 		saddr = &ct->tuplehash[!dir].tuple.src.u3;
949 	} else if (sip_external_media) {
950 		struct net_device *dev = skb_dst(skb)->dev;
951 		struct dst_entry *dst = NULL;
952 		struct flowi fl;
953 
954 		memset(&fl, 0, sizeof(fl));
955 
956 		switch (nf_ct_l3num(ct)) {
957 			case NFPROTO_IPV4:
958 				fl.u.ip4.daddr = daddr->ip;
959 				nf_ip_route(net, &dst, &fl, false);
960 				break;
961 
962 			case NFPROTO_IPV6:
963 				fl.u.ip6.daddr = daddr->in6;
964 				nf_ip6_route(net, &dst, &fl, false);
965 				break;
966 		}
967 
968 		/* Don't predict any conntracks when media endpoint is reachable
969 		 * through the same interface as the signalling peer.
970 		 */
971 		if (dst) {
972 			bool external_media = (dst->dev == dev);
973 
974 			dst_release(dst);
975 			if (external_media)
976 				return NF_ACCEPT;
977 		}
978 	}
979 
980 	/* We need to check whether the registration exists before attempting
981 	 * to register it since we can see the same media description multiple
982 	 * times on different connections in case multiple endpoints receive
983 	 * the same call.
984 	 *
985 	 * RTP optimization: if we find a matching media channel expectation
986 	 * and both the expectation and this connection are SNATed, we assume
987 	 * both sides can reach each other directly and use the final
988 	 * destination address from the expectation. We still need to keep
989 	 * the NATed expectations for media that might arrive from the
990 	 * outside, and additionally need to expect the direct RTP stream
991 	 * in case it passes through us even without NAT.
992 	 */
993 	memset(&tuple, 0, sizeof(tuple));
994 	if (saddr)
995 		tuple.src.u3 = *saddr;
996 	tuple.src.l3num		= nf_ct_l3num(ct);
997 	tuple.dst.protonum	= IPPROTO_UDP;
998 	tuple.dst.u3		= *daddr;
999 	tuple.dst.u.udp.port	= port;
1000 
1001 	do {
1002 		exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1003 
1004 		if (!exp || exp->master == ct ||
1005 		    exp->helper != nfct_help(ct)->helper ||
1006 		    exp->class != class)
1007 			break;
1008 #if IS_ENABLED(CONFIG_NF_NAT)
1009 		if (!direct_rtp &&
1010 		    (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) ||
1011 		     exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
1012 		    ct->status & IPS_NAT_MASK) {
1013 			*daddr			= exp->saved_addr;
1014 			tuple.dst.u3		= exp->saved_addr;
1015 			tuple.dst.u.udp.port	= exp->saved_proto.udp.port;
1016 			direct_rtp = 1;
1017 		} else
1018 #endif
1019 			skip_expect = 1;
1020 	} while (!skip_expect);
1021 
1022 	base_port = ntohs(tuple.dst.u.udp.port) & ~1;
1023 	rtp_port = htons(base_port);
1024 	rtcp_port = htons(base_port + 1);
1025 
1026 	if (direct_rtp) {
1027 		hooks = rcu_dereference(nf_nat_sip_hooks);
1028 		if (hooks &&
1029 		    !hooks->sdp_port(skb, protoff, dataoff, dptr, datalen,
1030 				     mediaoff, medialen, ntohs(rtp_port)))
1031 			goto err1;
1032 	}
1033 
1034 	if (skip_expect)
1035 		return NF_ACCEPT;
1036 
1037 	rtp_exp = nf_ct_expect_alloc(ct);
1038 	if (rtp_exp == NULL)
1039 		goto err1;
1040 	nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
1041 			  IPPROTO_UDP, NULL, &rtp_port);
1042 
1043 	rtcp_exp = nf_ct_expect_alloc(ct);
1044 	if (rtcp_exp == NULL)
1045 		goto err2;
1046 	nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
1047 			  IPPROTO_UDP, NULL, &rtcp_port);
1048 
1049 	hooks = rcu_dereference(nf_nat_sip_hooks);
1050 	if (hooks && ct->status & IPS_NAT_MASK && !direct_rtp)
1051 		ret = hooks->sdp_media(skb, protoff, dataoff, dptr,
1052 				       datalen, rtp_exp, rtcp_exp,
1053 				       mediaoff, medialen, daddr);
1054 	else {
1055 		/* -EALREADY handling works around end-points that send
1056 		 * SDP messages with identical port but different media type,
1057 		 * we pretend expectation was set up.
1058 		 * It also works in the case that SDP messages are sent with
1059 		 * identical expect tuples but for different master conntracks.
1060 		 */
1061 		int errp = nf_ct_expect_related(rtp_exp,
1062 						NF_CT_EXP_F_SKIP_MASTER);
1063 
1064 		if (errp == 0 || errp == -EALREADY) {
1065 			int errcp = nf_ct_expect_related(rtcp_exp,
1066 						NF_CT_EXP_F_SKIP_MASTER);
1067 
1068 			if (errcp == 0 || errcp == -EALREADY)
1069 				ret = NF_ACCEPT;
1070 			else if (errp == 0)
1071 				nf_ct_unexpect_related(rtp_exp);
1072 		}
1073 	}
1074 	nf_ct_expect_put(rtcp_exp);
1075 err2:
1076 	nf_ct_expect_put(rtp_exp);
1077 err1:
1078 	return ret;
1079 }
1080 
1081 static const struct sdp_media_type sdp_media_types[] = {
1082 	SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
1083 	SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
1084 	SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE),
1085 };
1086 
sdp_media_type(const char * dptr,unsigned int matchoff,unsigned int matchlen)1087 static const struct sdp_media_type *sdp_media_type(const char *dptr,
1088 						   unsigned int matchoff,
1089 						   unsigned int matchlen)
1090 {
1091 	const struct sdp_media_type *t;
1092 	unsigned int i;
1093 
1094 	for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
1095 		t = &sdp_media_types[i];
1096 		if (matchlen < t->len ||
1097 		    strncmp(dptr + matchoff, t->name, t->len))
1098 			continue;
1099 		return t;
1100 	}
1101 	return NULL;
1102 }
1103 
process_sdp(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq)1104 static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1105 		       unsigned int dataoff,
1106 		       const char **dptr, unsigned int *datalen,
1107 		       unsigned int cseq)
1108 {
1109 	enum ip_conntrack_info ctinfo;
1110 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1111 	unsigned int matchoff, matchlen;
1112 	unsigned int mediaoff, medialen;
1113 	unsigned int sdpoff;
1114 	unsigned int caddr_len, maddr_len;
1115 	unsigned int i;
1116 	union nf_inet_addr caddr, maddr, rtp_addr;
1117 	const struct nf_nat_sip_hooks *hooks;
1118 	unsigned int port;
1119 	const struct sdp_media_type *t;
1120 	int ret = NF_ACCEPT;
1121 	bool have_rtp_addr = false;
1122 
1123 	hooks = rcu_dereference(nf_nat_sip_hooks);
1124 
1125 	/* Find beginning of session description */
1126 	if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
1127 				  SDP_HDR_VERSION, SDP_HDR_UNSPEC,
1128 				  &matchoff, &matchlen) <= 0)
1129 		return NF_ACCEPT;
1130 	sdpoff = matchoff;
1131 
1132 	/* The connection information is contained in the session description
1133 	 * and/or once per media description. The first media description marks
1134 	 * the end of the session description. */
1135 	caddr_len = 0;
1136 	if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
1137 				  SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
1138 				  &matchoff, &matchlen, &caddr) > 0) {
1139 		caddr_len = matchlen;
1140 		memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
1141 		have_rtp_addr = true;
1142 	}
1143 
1144 	mediaoff = sdpoff;
1145 	for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
1146 		char *end;
1147 
1148 		if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
1149 					  SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
1150 					  &mediaoff, &medialen) <= 0)
1151 			break;
1152 
1153 		/* Get media type and port number. A media port value of zero
1154 		 * indicates an inactive stream. */
1155 		t = sdp_media_type(*dptr, mediaoff, medialen);
1156 		if (!t) {
1157 			mediaoff += medialen;
1158 			continue;
1159 		}
1160 		mediaoff += t->len;
1161 		medialen -= t->len;
1162 
1163 		port = sip_strtouint(*dptr + mediaoff, *datalen - mediaoff, (char **)&end);
1164 		if (port == 0 || *dptr + mediaoff == end)
1165 			continue;
1166 		if (port < 1024 || port > 65535) {
1167 			nf_ct_helper_log(skb, ct, "wrong port %u", port);
1168 			return NF_DROP;
1169 		}
1170 
1171 		/* The media description overrides the session description. */
1172 		maddr_len = 0;
1173 		if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
1174 					  SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
1175 					  &matchoff, &matchlen, &maddr) > 0) {
1176 			maddr_len = matchlen;
1177 			memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
1178 			have_rtp_addr = true;
1179 		} else if (caddr_len) {
1180 			memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
1181 			have_rtp_addr = true;
1182 		} else {
1183 			nf_ct_helper_log(skb, ct, "cannot parse SDP message");
1184 			return NF_DROP;
1185 		}
1186 
1187 		ret = set_expected_rtp_rtcp(skb, protoff, dataoff,
1188 					    dptr, datalen,
1189 					    &rtp_addr, htons(port), t->class,
1190 					    mediaoff, medialen);
1191 		if (ret != NF_ACCEPT) {
1192 			nf_ct_helper_log(skb, ct,
1193 					 "cannot add expectation for voice");
1194 			return ret;
1195 		}
1196 
1197 		/* Update media connection address if present */
1198 		if (maddr_len && hooks && ct->status & IPS_NAT_MASK) {
1199 			ret = hooks->sdp_addr(skb, protoff, dataoff,
1200 					      dptr, datalen, mediaoff,
1201 					      SDP_HDR_CONNECTION,
1202 					      SDP_HDR_MEDIA,
1203 					      &rtp_addr);
1204 			if (ret != NF_ACCEPT) {
1205 				nf_ct_helper_log(skb, ct, "cannot mangle SDP");
1206 				return ret;
1207 			}
1208 		}
1209 		i++;
1210 	}
1211 
1212 	/* Update session connection and owner addresses */
1213 	hooks = rcu_dereference(nf_nat_sip_hooks);
1214 	if (hooks && ct->status & IPS_NAT_MASK && have_rtp_addr)
1215 		ret = hooks->sdp_session(skb, protoff, dataoff,
1216 					 dptr, datalen, sdpoff,
1217 					 &rtp_addr);
1218 
1219 	return ret;
1220 }
process_invite_response(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq,unsigned int code)1221 static int process_invite_response(struct sk_buff *skb, unsigned int protoff,
1222 				   unsigned int dataoff,
1223 				   const char **dptr, unsigned int *datalen,
1224 				   unsigned int cseq, unsigned int code)
1225 {
1226 	enum ip_conntrack_info ctinfo;
1227 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1228 	struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1229 
1230 	if ((code >= 100 && code <= 199) ||
1231 	    (code >= 200 && code <= 299))
1232 		return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1233 	else if (ct_sip_info->invite_cseq == cseq)
1234 		flush_expectations(ct, true);
1235 	return NF_ACCEPT;
1236 }
1237 
process_update_response(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq,unsigned int code)1238 static int process_update_response(struct sk_buff *skb, unsigned int protoff,
1239 				   unsigned int dataoff,
1240 				   const char **dptr, unsigned int *datalen,
1241 				   unsigned int cseq, unsigned int code)
1242 {
1243 	enum ip_conntrack_info ctinfo;
1244 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1245 	struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1246 
1247 	if ((code >= 100 && code <= 199) ||
1248 	    (code >= 200 && code <= 299))
1249 		return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1250 	else if (ct_sip_info->invite_cseq == cseq)
1251 		flush_expectations(ct, true);
1252 	return NF_ACCEPT;
1253 }
1254 
process_prack_response(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq,unsigned int code)1255 static int process_prack_response(struct sk_buff *skb, unsigned int protoff,
1256 				  unsigned int dataoff,
1257 				  const char **dptr, unsigned int *datalen,
1258 				  unsigned int cseq, unsigned int code)
1259 {
1260 	enum ip_conntrack_info ctinfo;
1261 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1262 	struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1263 
1264 	if ((code >= 100 && code <= 199) ||
1265 	    (code >= 200 && code <= 299))
1266 		return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1267 	else if (ct_sip_info->invite_cseq == cseq)
1268 		flush_expectations(ct, true);
1269 	return NF_ACCEPT;
1270 }
1271 
process_invite_request(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq)1272 static int process_invite_request(struct sk_buff *skb, unsigned int protoff,
1273 				  unsigned int dataoff,
1274 				  const char **dptr, unsigned int *datalen,
1275 				  unsigned int cseq)
1276 {
1277 	enum ip_conntrack_info ctinfo;
1278 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1279 	struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1280 	unsigned int ret;
1281 
1282 	flush_expectations(ct, true);
1283 	ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1284 	if (ret == NF_ACCEPT)
1285 		ct_sip_info->invite_cseq = cseq;
1286 	return ret;
1287 }
1288 
process_bye_request(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq)1289 static int process_bye_request(struct sk_buff *skb, unsigned int protoff,
1290 			       unsigned int dataoff,
1291 			       const char **dptr, unsigned int *datalen,
1292 			       unsigned int cseq)
1293 {
1294 	enum ip_conntrack_info ctinfo;
1295 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1296 
1297 	flush_expectations(ct, true);
1298 	return NF_ACCEPT;
1299 }
1300 
1301 /* Parse a REGISTER request and create a permanent expectation for incoming
1302  * signalling connections. The expectation is marked inactive and is activated
1303  * when receiving a response indicating success from the registrar.
1304  */
process_register_request(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq)1305 static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1306 				    unsigned int dataoff,
1307 				    const char **dptr, unsigned int *datalen,
1308 				    unsigned int cseq)
1309 {
1310 	enum ip_conntrack_info ctinfo;
1311 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1312 	struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1313 	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1314 	unsigned int matchoff, matchlen;
1315 	struct nf_conntrack_expect *exp;
1316 	union nf_inet_addr *saddr, daddr;
1317 	const struct nf_nat_sip_hooks *hooks;
1318 	struct nf_conntrack_helper *helper;
1319 	__be16 port;
1320 	u8 proto;
1321 	unsigned int expires = 0;
1322 	int ret;
1323 
1324 	/* Expected connections can not register again. */
1325 	if (ct->status & IPS_EXPECTED)
1326 		return NF_ACCEPT;
1327 
1328 	/* We must check the expiration time: a value of zero signals the
1329 	 * registrar to release the binding. We'll remove our expectation
1330 	 * when receiving the new bindings in the response, but we don't
1331 	 * want to create new ones.
1332 	 *
1333 	 * The expiration time may be contained in Expires: header, the
1334 	 * Contact: header parameters or the URI parameters.
1335 	 */
1336 	if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1337 			      &matchoff, &matchlen) > 0)
1338 		expires = sip_strtouint(*dptr + matchoff, *datalen - matchoff, NULL);
1339 
1340 	ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1341 				      SIP_HDR_CONTACT, NULL,
1342 				      &matchoff, &matchlen, &daddr, &port);
1343 	if (ret < 0) {
1344 		nf_ct_helper_log(skb, ct, "cannot parse contact");
1345 		return NF_DROP;
1346 	} else if (ret == 0)
1347 		return NF_ACCEPT;
1348 
1349 	/* We don't support third-party registrations */
1350 	if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1351 		return NF_ACCEPT;
1352 
1353 	if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1354 				   &proto) == 0)
1355 		return NF_ACCEPT;
1356 
1357 	if (ct_sip_parse_numerical_param(ct, *dptr,
1358 					 matchoff + matchlen, *datalen,
1359 					 "expires=", NULL, NULL, &expires) < 0) {
1360 		nf_ct_helper_log(skb, ct, "cannot parse expires");
1361 		return NF_DROP;
1362 	}
1363 
1364 	if (expires == 0) {
1365 		ret = NF_ACCEPT;
1366 		goto store_cseq;
1367 	}
1368 
1369 	exp = nf_ct_expect_alloc(ct);
1370 	if (!exp) {
1371 		nf_ct_helper_log(skb, ct, "cannot alloc expectation");
1372 		return NF_DROP;
1373 	}
1374 
1375 	saddr = NULL;
1376 	if (sip_direct_signalling)
1377 		saddr = &ct->tuplehash[!dir].tuple.src.u3;
1378 
1379 	helper = rcu_dereference(nfct_help(ct)->helper);
1380 	if (!helper)
1381 		return NF_DROP;
1382 
1383 	nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
1384 			  saddr, &daddr, proto, NULL, &port);
1385 	exp->timeout.expires = sip_timeout * HZ;
1386 	rcu_assign_pointer(exp->helper, helper);
1387 	exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1388 
1389 	hooks = rcu_dereference(nf_nat_sip_hooks);
1390 	if (hooks && ct->status & IPS_NAT_MASK)
1391 		ret = hooks->expect(skb, protoff, dataoff, dptr, datalen,
1392 				    exp, matchoff, matchlen);
1393 	else {
1394 		if (nf_ct_expect_related(exp, 0) != 0) {
1395 			nf_ct_helper_log(skb, ct, "cannot add expectation");
1396 			ret = NF_DROP;
1397 		} else
1398 			ret = NF_ACCEPT;
1399 	}
1400 	nf_ct_expect_put(exp);
1401 
1402 store_cseq:
1403 	if (ret == NF_ACCEPT)
1404 		ct_sip_info->register_cseq = cseq;
1405 	return ret;
1406 }
1407 
process_register_response(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen,unsigned int cseq,unsigned int code)1408 static int process_register_response(struct sk_buff *skb, unsigned int protoff,
1409 				     unsigned int dataoff,
1410 				     const char **dptr, unsigned int *datalen,
1411 				     unsigned int cseq, unsigned int code)
1412 {
1413 	enum ip_conntrack_info ctinfo;
1414 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1415 	struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1416 	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1417 	union nf_inet_addr addr;
1418 	__be16 port;
1419 	u8 proto;
1420 	unsigned int matchoff, matchlen, coff = 0;
1421 	unsigned int expires = 0;
1422 	int in_contact = 0, ret;
1423 
1424 	/* According to RFC 3261, "UAs MUST NOT send a new registration until
1425 	 * they have received a final response from the registrar for the
1426 	 * previous one or the previous REGISTER request has timed out".
1427 	 *
1428 	 * However, some servers fail to detect retransmissions and send late
1429 	 * responses, so we store the sequence number of the last valid
1430 	 * request and compare it here.
1431 	 */
1432 	if (ct_sip_info->register_cseq != cseq)
1433 		return NF_ACCEPT;
1434 
1435 	if (code >= 100 && code <= 199)
1436 		return NF_ACCEPT;
1437 	if (code < 200 || code > 299)
1438 		goto flush;
1439 
1440 	if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1441 			      &matchoff, &matchlen) > 0)
1442 		expires = sip_strtouint(*dptr + matchoff, *datalen - matchoff, NULL);
1443 
1444 	while (1) {
1445 		unsigned int c_expires = expires;
1446 
1447 		ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
1448 					      SIP_HDR_CONTACT, &in_contact,
1449 					      &matchoff, &matchlen,
1450 					      &addr, &port);
1451 		if (ret < 0) {
1452 			nf_ct_helper_log(skb, ct, "cannot parse contact");
1453 			return NF_DROP;
1454 		} else if (ret == 0)
1455 			break;
1456 
1457 		/* We don't support third-party registrations */
1458 		if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1459 			continue;
1460 
1461 		if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1462 					   *datalen, &proto) == 0)
1463 			continue;
1464 
1465 		ret = ct_sip_parse_numerical_param(ct, *dptr,
1466 						   matchoff + matchlen,
1467 						   *datalen, "expires=",
1468 						   NULL, NULL, &c_expires);
1469 		if (ret < 0) {
1470 			nf_ct_helper_log(skb, ct, "cannot parse expires");
1471 			return NF_DROP;
1472 		}
1473 		if (c_expires == 0)
1474 			break;
1475 		if (refresh_signalling_expectation(ct, &addr, proto, port,
1476 						   c_expires))
1477 			return NF_ACCEPT;
1478 	}
1479 
1480 flush:
1481 	flush_expectations(ct, false);
1482 	return NF_ACCEPT;
1483 }
1484 
1485 static const struct sip_handler sip_handlers[] = {
1486 	SIP_HANDLER("INVITE", process_invite_request, process_invite_response),
1487 	SIP_HANDLER("UPDATE", process_sdp, process_update_response),
1488 	SIP_HANDLER("ACK", process_sdp, NULL),
1489 	SIP_HANDLER("PRACK", process_sdp, process_prack_response),
1490 	SIP_HANDLER("BYE", process_bye_request, NULL),
1491 	SIP_HANDLER("REGISTER", process_register_request, process_register_response),
1492 };
1493 
process_sip_response(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen)1494 static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
1495 				unsigned int dataoff,
1496 				const char **dptr, unsigned int *datalen)
1497 {
1498 	enum ip_conntrack_info ctinfo;
1499 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1500 	unsigned int matchoff, matchlen, matchend;
1501 	unsigned int code, cseq, i;
1502 	char *end;
1503 
1504 	if (*datalen < strlen("SIP/2.0 200"))
1505 		return NF_ACCEPT;
1506 	code = sip_strtouint(*dptr + strlen("SIP/2.0 "),
1507 			     *datalen - strlen("SIP/2.0 "), NULL);
1508 	if (!code) {
1509 		nf_ct_helper_log(skb, ct, "cannot get code");
1510 		return NF_DROP;
1511 	}
1512 
1513 	if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1514 			      &matchoff, &matchlen) <= 0) {
1515 		nf_ct_helper_log(skb, ct, "cannot parse cseq");
1516 		return NF_DROP;
1517 	}
1518 	cseq = sip_strtouint(*dptr + matchoff, *datalen - matchoff, (char **)&end);
1519 	if (*dptr + matchoff == end) {
1520 		nf_ct_helper_log(skb, ct, "cannot get cseq");
1521 		return NF_DROP;
1522 	}
1523 	matchend = matchoff + matchlen + 1;
1524 
1525 	for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1526 		const struct sip_handler *handler;
1527 
1528 		handler = &sip_handlers[i];
1529 		if (handler->response == NULL)
1530 			continue;
1531 		if (*datalen < matchend + handler->len ||
1532 		    strncasecmp(*dptr + matchend, handler->method, handler->len))
1533 			continue;
1534 		return handler->response(skb, protoff, dataoff, dptr, datalen,
1535 					 cseq, code);
1536 	}
1537 	return NF_ACCEPT;
1538 }
1539 
process_sip_request(struct sk_buff * skb,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen)1540 static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
1541 			       unsigned int dataoff,
1542 			       const char **dptr, unsigned int *datalen)
1543 {
1544 	enum ip_conntrack_info ctinfo;
1545 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1546 	struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1547 	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1548 	unsigned int matchoff, matchlen;
1549 	unsigned int cseq, i;
1550 	union nf_inet_addr addr;
1551 	__be16 port;
1552 
1553 	/* Many Cisco IP phones use a high source port for SIP requests, but
1554 	 * listen for the response on port 5060.  If we are the local
1555 	 * router for one of these phones, save the port number from the
1556 	 * Via: header so that nf_nat_sip can redirect the responses to
1557 	 * the correct port.
1558 	 */
1559 	if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1560 				    SIP_HDR_VIA_UDP, NULL, &matchoff,
1561 				    &matchlen, &addr, &port) > 0 &&
1562 	    port != ct->tuplehash[dir].tuple.src.u.udp.port &&
1563 	    nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3))
1564 		ct_sip_info->forced_dport = port;
1565 
1566 	for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1567 		const struct sip_handler *handler;
1568 		char *end;
1569 
1570 		handler = &sip_handlers[i];
1571 		if (handler->request == NULL)
1572 			continue;
1573 		if (*datalen < handler->len + 2 ||
1574 		    strncasecmp(*dptr, handler->method, handler->len))
1575 			continue;
1576 		if ((*dptr)[handler->len] != ' ' ||
1577 		    !isalpha((*dptr)[handler->len+1]))
1578 			continue;
1579 
1580 		if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1581 				      &matchoff, &matchlen) <= 0) {
1582 			nf_ct_helper_log(skb, ct, "cannot parse cseq");
1583 			return NF_DROP;
1584 		}
1585 		cseq = sip_strtouint(*dptr + matchoff, *datalen - matchoff, (char **)&end);
1586 		if (*dptr + matchoff == end) {
1587 			nf_ct_helper_log(skb, ct, "cannot get cseq");
1588 			return NF_DROP;
1589 		}
1590 
1591 		return handler->request(skb, protoff, dataoff, dptr, datalen,
1592 					cseq);
1593 	}
1594 	return NF_ACCEPT;
1595 }
1596 
process_sip_msg(struct sk_buff * skb,struct nf_conn * ct,unsigned int protoff,unsigned int dataoff,const char ** dptr,unsigned int * datalen)1597 static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
1598 			   unsigned int protoff, unsigned int dataoff,
1599 			   const char **dptr, unsigned int *datalen)
1600 {
1601 	const struct nf_nat_sip_hooks *hooks;
1602 	int ret;
1603 
1604 	if (strncasecmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1605 		ret = process_sip_request(skb, protoff, dataoff, dptr, datalen);
1606 	else
1607 		ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
1608 
1609 	if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1610 		hooks = rcu_dereference(nf_nat_sip_hooks);
1611 		if (hooks && !hooks->msg(skb, protoff, dataoff,
1612 					 dptr, datalen)) {
1613 			nf_ct_helper_log(skb, ct, "cannot NAT SIP message");
1614 			ret = NF_DROP;
1615 		}
1616 	}
1617 
1618 	return ret;
1619 }
1620 
sip_help_tcp(struct sk_buff * skb,unsigned int protoff,struct nf_conn * ct,enum ip_conntrack_info ctinfo)1621 static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1622 			struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1623 {
1624 	struct tcphdr *th, _tcph;
1625 	unsigned int dataoff, datalen;
1626 	unsigned int matchoff, matchlen;
1627 	unsigned int msglen, origlen;
1628 	const char *dptr, *end;
1629 	s16 diff, tdiff = 0;
1630 	int ret = NF_ACCEPT;
1631 	unsigned long clen;
1632 	bool term;
1633 
1634 	if (ctinfo != IP_CT_ESTABLISHED &&
1635 	    ctinfo != IP_CT_ESTABLISHED_REPLY)
1636 		return NF_ACCEPT;
1637 
1638 	/* No Data ? */
1639 	th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1640 	if (th == NULL)
1641 		return NF_ACCEPT;
1642 	dataoff = protoff + th->doff * 4;
1643 	if (dataoff >= skb->len)
1644 		return NF_ACCEPT;
1645 
1646 	nf_ct_refresh(ct, sip_timeout * HZ);
1647 
1648 	if (unlikely(skb_linearize(skb)))
1649 		return NF_DROP;
1650 
1651 	dptr = skb->data + dataoff;
1652 	datalen = skb->len - dataoff;
1653 	if (datalen < strlen("SIP/2.0 200"))
1654 		return NF_ACCEPT;
1655 
1656 	while (1) {
1657 		if (ct_sip_get_header(ct, dptr, 0, datalen,
1658 				      SIP_HDR_CONTENT_LENGTH,
1659 				      &matchoff, &matchlen) <= 0)
1660 			break;
1661 
1662 		clen = sip_strtouint(dptr + matchoff, datalen - matchoff, (char **)&end);
1663 		if (dptr + matchoff == end)
1664 			break;
1665 
1666 		if (clen > datalen)
1667 			break;
1668 
1669 		term = false;
1670 		for (; end + strlen("\r\n\r\n") <= dptr + datalen; end++) {
1671 			if (end[0] == '\r' && end[1] == '\n' &&
1672 			    end[2] == '\r' && end[3] == '\n') {
1673 				term = true;
1674 				break;
1675 			}
1676 		}
1677 		if (!term)
1678 			break;
1679 		end += strlen("\r\n\r\n") + clen;
1680 
1681 		msglen = origlen = end - dptr;
1682 		if (msglen > datalen)
1683 			return NF_ACCEPT;
1684 
1685 		ret = process_sip_msg(skb, ct, protoff, dataoff,
1686 				      &dptr, &msglen);
1687 		/* process_sip_* functions report why this packet is dropped */
1688 		if (ret != NF_ACCEPT)
1689 			break;
1690 		diff     = msglen - origlen;
1691 		tdiff   += diff;
1692 
1693 		dataoff += msglen;
1694 		dptr    += msglen;
1695 		datalen  = datalen + diff - msglen;
1696 	}
1697 
1698 	if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1699 		const struct nf_nat_sip_hooks *hooks;
1700 
1701 		hooks = rcu_dereference(nf_nat_sip_hooks);
1702 		if (hooks)
1703 			hooks->seq_adjust(skb, protoff, tdiff);
1704 	}
1705 
1706 	return ret;
1707 }
1708 
sip_help_udp(struct sk_buff * skb,unsigned int protoff,struct nf_conn * ct,enum ip_conntrack_info ctinfo)1709 static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
1710 			struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1711 {
1712 	unsigned int dataoff, datalen;
1713 	const char *dptr;
1714 
1715 	/* No Data ? */
1716 	dataoff = protoff + sizeof(struct udphdr);
1717 	if (dataoff >= skb->len)
1718 		return NF_ACCEPT;
1719 
1720 	nf_ct_refresh(ct, sip_timeout * HZ);
1721 
1722 	if (unlikely(skb_linearize(skb)))
1723 		return NF_DROP;
1724 
1725 	dptr = skb->data + dataoff;
1726 	datalen = skb->len - dataoff;
1727 	if (datalen < strlen("SIP/2.0 200"))
1728 		return NF_ACCEPT;
1729 
1730 	return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen);
1731 }
1732 
1733 static struct nf_conntrack_helper sip[MAX_PORTS * 4] __read_mostly;
1734 
1735 static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1736 	[SIP_EXPECT_SIGNALLING] = {
1737 		.name		= "signalling",
1738 		.max_expected	= 1,
1739 		.timeout	= 3 * 60,
1740 	},
1741 	[SIP_EXPECT_AUDIO] = {
1742 		.name		= "audio",
1743 		.max_expected	= 2 * IP_CT_DIR_MAX,
1744 		.timeout	= 3 * 60,
1745 	},
1746 	[SIP_EXPECT_VIDEO] = {
1747 		.name		= "video",
1748 		.max_expected	= 2 * IP_CT_DIR_MAX,
1749 		.timeout	= 3 * 60,
1750 	},
1751 	[SIP_EXPECT_IMAGE] = {
1752 		.name		= "image",
1753 		.max_expected	= IP_CT_DIR_MAX,
1754 		.timeout	= 3 * 60,
1755 	},
1756 };
1757 
nf_conntrack_sip_fini(void)1758 static void __exit nf_conntrack_sip_fini(void)
1759 {
1760 	nf_conntrack_helpers_unregister(sip, ports_c * 4);
1761 }
1762 
nf_conntrack_sip_init(void)1763 static int __init nf_conntrack_sip_init(void)
1764 {
1765 	int i, ret;
1766 
1767 	NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_sip_master));
1768 
1769 	if (ports_c == 0)
1770 		ports[ports_c++] = SIP_PORT;
1771 
1772 	for (i = 0; i < ports_c; i++) {
1773 		nf_ct_helper_init(&sip[4 * i], AF_INET, IPPROTO_UDP,
1774 				  HELPER_NAME, SIP_PORT, ports[i], i,
1775 				  sip_exp_policy, SIP_EXPECT_MAX, sip_help_udp,
1776 				  NULL, THIS_MODULE);
1777 		nf_ct_helper_init(&sip[4 * i + 1], AF_INET, IPPROTO_TCP,
1778 				  HELPER_NAME, SIP_PORT, ports[i], i,
1779 				  sip_exp_policy, SIP_EXPECT_MAX, sip_help_tcp,
1780 				  NULL, THIS_MODULE);
1781 		nf_ct_helper_init(&sip[4 * i + 2], AF_INET6, IPPROTO_UDP,
1782 				  HELPER_NAME, SIP_PORT, ports[i], i,
1783 				  sip_exp_policy, SIP_EXPECT_MAX, sip_help_udp,
1784 				  NULL, THIS_MODULE);
1785 		nf_ct_helper_init(&sip[4 * i + 3], AF_INET6, IPPROTO_TCP,
1786 				  HELPER_NAME, SIP_PORT, ports[i], i,
1787 				  sip_exp_policy, SIP_EXPECT_MAX, sip_help_tcp,
1788 				  NULL, THIS_MODULE);
1789 	}
1790 
1791 	ret = nf_conntrack_helpers_register(sip, ports_c * 4);
1792 	if (ret < 0) {
1793 		pr_err("failed to register helpers\n");
1794 		return ret;
1795 	}
1796 	return 0;
1797 }
1798 
1799 module_init(nf_conntrack_sip_init);
1800 module_exit(nf_conntrack_sip_fini);
1801