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