blob: 7d77217de6a3b4e7821452f721c0902ca6fecd81 [file] [log] [blame]
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001/* SIP extension for IP connection tracking.
2 *
3 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
4 * based on RR's ip_conntrack_ftp.c and other modules.
Patrick McHardyf49e1aa2008-03-25 20:27:05 -07005 * (C) 2007 United Security Providers
6 * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
Patrick McHardy9fafcd72006-12-02 22:09:57 -08007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +010013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Patrick McHardy9fafcd72006-12-02 22:09:57 -080015#include <linux/module.h>
16#include <linux/ctype.h>
17#include <linux/skbuff.h>
18#include <linux/inet.h>
19#include <linux/in.h>
20#include <linux/udp.h>
Patrick McHardyf5b321b2010-02-11 12:26:19 +010021#include <linux/tcp.h>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080022#include <linux/netfilter.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080023
24#include <net/netfilter/nf_conntrack.h>
Patrick McHardy9467ee32008-03-25 20:24:04 -070025#include <net/netfilter/nf_conntrack_core.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080026#include <net/netfilter/nf_conntrack_expect.h>
27#include <net/netfilter/nf_conntrack_helper.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010028#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080029#include <linux/netfilter/nf_conntrack_sip.h>
30
Patrick McHardy9fafcd72006-12-02 22:09:57 -080031MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
33MODULE_DESCRIPTION("SIP connection tracking helper");
34MODULE_ALIAS("ip_conntrack_sip");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010035MODULE_ALIAS_NFCT_HELPER("sip");
Patrick McHardy9fafcd72006-12-02 22:09:57 -080036
37#define MAX_PORTS 8
38static unsigned short ports[MAX_PORTS];
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -080039static unsigned int ports_c;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080040module_param_array(ports, ushort, &ports_c, 0400);
41MODULE_PARM_DESC(ports, "port numbers of SIP servers");
42
43static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
44module_param(sip_timeout, uint, 0600);
45MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
46
Patrick McHardy0f32a402008-03-25 20:25:13 -070047static int sip_direct_signalling __read_mostly = 1;
48module_param(sip_direct_signalling, int, 0600);
49MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
50 "only (default 1)");
51
Patrick McHardyd901a932008-03-25 20:25:32 -070052static int sip_direct_media __read_mostly = 1;
53module_param(sip_direct_media, int, 0600);
54MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
55 "endpoints only (default 1)");
56
holger@eitzenberger.org180cf722013-09-30 17:07:28 +020057const struct nf_nat_sip_hooks *nf_nat_sip_hooks;
58EXPORT_SYMBOL_GPL(nf_nat_sip_hooks);
Patrick McHardy9fafcd72006-12-02 22:09:57 -080059
Patrick McHardyac367742008-03-25 20:18:40 -070060static 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
Jan Engelhardt13f7d632008-01-31 04:50:25 -080072static int digits_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -080073 const char *limit, int *shift)
74{
75 int len = 0;
Patrick McHardyb1ec4882008-03-25 20:10:11 -070076 while (dptr < limit && isdigit(*dptr)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -080077 dptr++;
78 len++;
79 }
80 return len;
81}
82
Simon Horman001985b2010-08-22 21:37:51 +090083static 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 == '~')
89 return 1;
90 return 0;
91}
92
93static int word_len(const char *dptr, const char *limit)
94{
95 int len = 0;
96 while (dptr < limit && iswordc(*dptr)) {
97 dptr++;
98 len++;
99 }
100 return len;
101}
102
103static int callid_len(const struct nf_conn *ct, const char *dptr,
104 const char *limit, int *shift)
105{
106 int len, domain_len;
107
108 len = word_len(dptr, limit);
109 dptr += len;
110 if (!len || dptr == limit || *dptr != '@')
111 return len;
112 dptr++;
113 len++;
114
115 domain_len = word_len(dptr, limit);
116 if (!domain_len)
117 return 0;
118 return len + domain_len;
119}
120
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700121/* get media type + port length */
122static int media_len(const struct nf_conn *ct, const char *dptr,
123 const char *limit, int *shift)
124{
125 int len = string_len(ct, dptr, limit, shift);
126
127 dptr += len;
128 if (dptr >= limit || *dptr != ' ')
129 return 0;
130 len++;
131 dptr++;
132
133 return len + digits_len(ct, dptr, limit, shift);
134}
135
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000136static int sip_parse_addr(const struct nf_conn *ct, const char *cp,
137 const char **endp, union nf_inet_addr *addr,
138 const char *limit, bool delim)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800139{
140 const char *end;
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000141 int ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800142
Simon Horman5adbb9f2010-08-22 21:37:51 +0900143 if (!ct)
144 return 0;
145
Patrick McHardyfa913dd2008-04-14 11:15:45 +0200146 memset(addr, 0, sizeof(*addr));
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200147 switch (nf_ct_l3num(ct)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800148 case AF_INET:
149 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000150 if (ret == 0)
151 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800152 break;
153 case AF_INET6:
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000154 if (cp < limit && *cp == '[')
155 cp++;
156 else if (delim)
157 return 0;
158
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800159 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000160 if (ret == 0)
161 return 0;
162
163 if (end < limit && *end == ']')
164 end++;
165 else if (delim)
166 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800167 break;
168 default:
169 BUG();
170 }
171
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800172 if (endp)
173 *endp = end;
174 return 1;
175}
176
177/* skip ip address. returns its length. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800178static int epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800179 const char *limit, int *shift)
180{
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800181 union nf_inet_addr addr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800182 const char *aux = dptr;
183
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000184 if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit, true)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700185 pr_debug("ip: %s parse failed.!\n", dptr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800186 return 0;
187 }
188
189 /* Port number */
190 if (*dptr == ':') {
191 dptr++;
192 dptr += digits_len(ct, dptr, limit, shift);
193 }
194 return dptr - aux;
195}
196
197/* get address length, skiping user info. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800198static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800199 const char *limit, int *shift)
200{
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700201 const char *start = dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800202 int s = *shift;
203
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800204 /* Search for @, but stop at the end of the line.
205 * We are inside a sip: URI, so we don't need to worry about
206 * continuation lines. */
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700207 while (dptr < limit &&
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800208 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800209 (*shift)++;
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800210 dptr++;
211 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800212
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700213 if (dptr < limit && *dptr == '@') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800214 dptr++;
215 (*shift)++;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700216 } else {
217 dptr = start;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800218 *shift = s;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700219 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800220
221 return epaddr_len(ct, dptr, limit, shift);
222}
223
Patrick McHardyac367742008-03-25 20:18:40 -0700224/* Parse a SIP request line of the form:
225 *
226 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
227 *
228 * and return the offset and length of the address contained in the Request-URI.
229 */
230int ct_sip_parse_request(const struct nf_conn *ct,
231 const char *dptr, unsigned int datalen,
Patrick McHardy624f8b72008-03-25 20:19:30 -0700232 unsigned int *matchoff, unsigned int *matchlen,
233 union nf_inet_addr *addr, __be16 *port)
Patrick McHardyac367742008-03-25 20:18:40 -0700234{
Patrick McHardy624f8b72008-03-25 20:19:30 -0700235 const char *start = dptr, *limit = dptr + datalen, *end;
Patrick McHardyac367742008-03-25 20:18:40 -0700236 unsigned int mlen;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700237 unsigned int p;
Patrick McHardyac367742008-03-25 20:18:40 -0700238 int shift = 0;
239
240 /* Skip method and following whitespace */
241 mlen = string_len(ct, dptr, limit, NULL);
242 if (!mlen)
243 return 0;
244 dptr += mlen;
245 if (++dptr >= limit)
246 return 0;
247
248 /* Find SIP URI */
Patrick McHardy54101f42010-02-11 12:23:12 +0100249 for (; dptr < limit - strlen("sip:"); dptr++) {
Patrick McHardyac367742008-03-25 20:18:40 -0700250 if (*dptr == '\r' || *dptr == '\n')
251 return -1;
Rasmus Villemoes18082742014-10-13 15:54:31 -0700252 if (strncasecmp(dptr, "sip:", strlen("sip:")) == 0) {
Patrick McHardy54101f42010-02-11 12:23:12 +0100253 dptr += strlen("sip:");
Patrick McHardyac367742008-03-25 20:18:40 -0700254 break;
Patrick McHardy54101f42010-02-11 12:23:12 +0100255 }
Patrick McHardyac367742008-03-25 20:18:40 -0700256 }
Patrick McHardy624f8b72008-03-25 20:19:30 -0700257 if (!skp_epaddr_len(ct, dptr, limit, &shift))
Patrick McHardyac367742008-03-25 20:18:40 -0700258 return 0;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700259 dptr += shift;
260
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000261 if (!sip_parse_addr(ct, dptr, &end, addr, limit, true))
Patrick McHardy624f8b72008-03-25 20:19:30 -0700262 return -1;
263 if (end < limit && *end == ':') {
264 end++;
265 p = simple_strtoul(end, (char **)&end, 10);
266 if (p < 1024 || p > 65535)
267 return -1;
268 *port = htons(p);
269 } else
270 *port = htons(SIP_PORT);
271
272 if (end == dptr)
273 return 0;
274 *matchoff = dptr - start;
275 *matchlen = end - dptr;
Patrick McHardyac367742008-03-25 20:18:40 -0700276 return 1;
277}
278EXPORT_SYMBOL_GPL(ct_sip_parse_request);
279
Patrick McHardyea45f122008-03-25 20:18:57 -0700280/* SIP header parsing: SIP headers are located at the beginning of a line, but
281 * may span several lines, in which case the continuation lines begin with a
282 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
283 * CRLF, RFC 3261 allows only CRLF, we support both.
284 *
285 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
286 * whitespace and the values. Whitespace in this context means any amount of
287 * tabs, spaces and continuation lines, which are treated as a single whitespace
288 * character.
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700289 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800290 * Some headers may appear multiple times. A comma separated list of values is
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700291 * equivalent to multiple headers.
Patrick McHardyea45f122008-03-25 20:18:57 -0700292 */
293static const struct sip_header ct_sip_hdrs[] = {
Patrick McHardy30f33e62008-03-25 20:22:20 -0700294 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700295 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
296 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
297 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100298 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
299 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
Patrick McHardy0f32a402008-03-25 20:25:13 -0700300 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700301 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
Simon Horman001985b2010-08-22 21:37:51 +0900302 [SIP_HDR_CALL_ID] = SIP_HDR("Call-Id", "i", NULL, callid_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700303};
304
305static const char *sip_follow_continuation(const char *dptr, const char *limit)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800306{
Patrick McHardyea45f122008-03-25 20:18:57 -0700307 /* Walk past newline */
308 if (++dptr >= limit)
309 return NULL;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800310
Patrick McHardyea45f122008-03-25 20:18:57 -0700311 /* Skip '\n' in CR LF */
312 if (*(dptr - 1) == '\r' && *dptr == '\n') {
313 if (++dptr >= limit)
314 return NULL;
315 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800316
Patrick McHardyea45f122008-03-25 20:18:57 -0700317 /* Continuation line? */
318 if (*dptr != ' ' && *dptr != '\t')
319 return NULL;
320
321 /* skip leading whitespace */
322 for (; dptr < limit; dptr++) {
323 if (*dptr != ' ' && *dptr != '\t')
324 break;
325 }
326 return dptr;
327}
328
329static const char *sip_skip_whitespace(const char *dptr, const char *limit)
330{
331 for (; dptr < limit; dptr++) {
332 if (*dptr == ' ')
333 continue;
334 if (*dptr != '\r' && *dptr != '\n')
335 break;
336 dptr = sip_follow_continuation(dptr, limit);
337 if (dptr == NULL)
338 return NULL;
339 }
340 return dptr;
341}
342
343/* Search within a SIP header value, dealing with continuation lines */
344static const char *ct_sip_header_search(const char *dptr, const char *limit,
345 const char *needle, unsigned int len)
346{
347 for (limit -= len; dptr < limit; dptr++) {
348 if (*dptr == '\r' || *dptr == '\n') {
349 dptr = sip_follow_continuation(dptr, limit);
350 if (dptr == NULL)
351 break;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800352 continue;
353 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800354
Rasmus Villemoes18082742014-10-13 15:54:31 -0700355 if (strncasecmp(dptr, needle, len) == 0)
Patrick McHardyea45f122008-03-25 20:18:57 -0700356 return dptr;
357 }
358 return NULL;
359}
360
361int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
362 unsigned int dataoff, unsigned int datalen,
363 enum sip_header_types type,
364 unsigned int *matchoff, unsigned int *matchlen)
365{
366 const struct sip_header *hdr = &ct_sip_hdrs[type];
367 const char *start = dptr, *limit = dptr + datalen;
368 int shift = 0;
369
370 for (dptr += dataoff; dptr < limit; dptr++) {
371 /* Find beginning of line */
372 if (*dptr != '\r' && *dptr != '\n')
373 continue;
374 if (++dptr >= limit)
375 break;
376 if (*(dptr - 1) == '\r' && *dptr == '\n') {
377 if (++dptr >= limit)
378 break;
379 }
380
381 /* Skip continuation lines */
382 if (*dptr == ' ' || *dptr == '\t')
383 continue;
384
385 /* Find header. Compact headers must be followed by a
386 * non-alphabetic character to avoid mismatches. */
387 if (limit - dptr >= hdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700388 strncasecmp(dptr, hdr->name, hdr->len) == 0)
Patrick McHardyea45f122008-03-25 20:18:57 -0700389 dptr += hdr->len;
390 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700391 strncasecmp(dptr, hdr->cname, hdr->clen) == 0 &&
Patrick McHardy135d0182010-01-19 19:06:59 +0100392 !isalpha(*(dptr + hdr->clen)))
Patrick McHardyea45f122008-03-25 20:18:57 -0700393 dptr += hdr->clen;
394 else
395 continue;
396
397 /* Find and skip colon */
398 dptr = sip_skip_whitespace(dptr, limit);
399 if (dptr == NULL)
400 break;
401 if (*dptr != ':' || ++dptr >= limit)
402 break;
403
404 /* Skip whitespace after colon */
405 dptr = sip_skip_whitespace(dptr, limit);
406 if (dptr == NULL)
407 break;
408
409 *matchoff = dptr - start;
410 if (hdr->search) {
411 dptr = ct_sip_header_search(dptr, limit, hdr->search,
412 hdr->slen);
413 if (!dptr)
414 return -1;
415 dptr += hdr->slen;
416 }
417
418 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800419 if (!*matchlen)
420 return -1;
Patrick McHardyea45f122008-03-25 20:18:57 -0700421 *matchoff = dptr - start + shift;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800422 return 1;
423 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800424 return 0;
425}
Patrick McHardyea45f122008-03-25 20:18:57 -0700426EXPORT_SYMBOL_GPL(ct_sip_get_header);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800427
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800428/* Get next header field in a list of comma separated values */
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700429static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
430 unsigned int dataoff, unsigned int datalen,
431 enum sip_header_types type,
432 unsigned int *matchoff, unsigned int *matchlen)
433{
434 const struct sip_header *hdr = &ct_sip_hdrs[type];
435 const char *start = dptr, *limit = dptr + datalen;
436 int shift = 0;
437
438 dptr += dataoff;
439
440 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
441 if (!dptr)
442 return 0;
443
444 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
445 if (!dptr)
446 return 0;
447 dptr += hdr->slen;
448
449 *matchoff = dptr - start;
450 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
451 if (!*matchlen)
452 return -1;
453 *matchoff += shift;
454 return 1;
455}
456
457/* Walk through headers until a parsable one is found or no header of the
458 * given type is left. */
459static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
460 unsigned int dataoff, unsigned int datalen,
461 enum sip_header_types type, int *in_header,
462 unsigned int *matchoff, unsigned int *matchlen)
463{
464 int ret;
465
466 if (in_header && *in_header) {
467 while (1) {
468 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
469 type, matchoff, matchlen);
470 if (ret > 0)
471 return ret;
472 if (ret == 0)
473 break;
474 dataoff += *matchoff;
475 }
476 *in_header = 0;
477 }
478
479 while (1) {
480 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
481 type, matchoff, matchlen);
482 if (ret > 0)
483 break;
484 if (ret == 0)
485 return ret;
486 dataoff += *matchoff;
487 }
488
489 if (in_header)
490 *in_header = 1;
491 return 1;
492}
493
494/* Locate a SIP header, parse the URI and return the offset and length of
495 * the address as well as the address and port themselves. A stream of
496 * headers can be parsed by handing in a non-NULL datalen and in_header
497 * pointer.
498 */
499int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
500 unsigned int *dataoff, unsigned int datalen,
501 enum sip_header_types type, int *in_header,
502 unsigned int *matchoff, unsigned int *matchlen,
503 union nf_inet_addr *addr, __be16 *port)
504{
505 const char *c, *limit = dptr + datalen;
506 unsigned int p;
507 int ret;
508
509 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
510 type, in_header, matchoff, matchlen);
511 WARN_ON(ret < 0);
512 if (ret == 0)
513 return ret;
514
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000515 if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true))
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700516 return -1;
517 if (*c == ':') {
518 c++;
519 p = simple_strtoul(c, (char **)&c, 10);
520 if (p < 1024 || p > 65535)
521 return -1;
522 *port = htons(p);
523 } else
524 *port = htons(SIP_PORT);
525
526 if (dataoff)
527 *dataoff = c - dptr;
528 return 1;
529}
530EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
531
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100532static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
533 unsigned int dataoff, unsigned int datalen,
534 const char *name,
535 unsigned int *matchoff, unsigned int *matchlen)
536{
537 const char *limit = dptr + datalen;
538 const char *start;
539 const char *end;
540
541 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
542 if (!limit)
543 limit = dptr + datalen;
544
545 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
546 if (!start)
547 return 0;
548 start += strlen(name);
549
550 end = ct_sip_header_search(start, limit, ";", strlen(";"));
551 if (!end)
552 end = limit;
553
554 *matchoff = start - dptr;
555 *matchlen = end - start;
556 return 1;
557}
558
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700559/* Parse address from header parameter and return address, offset and length */
560int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
561 unsigned int dataoff, unsigned int datalen,
562 const char *name,
563 unsigned int *matchoff, unsigned int *matchlen,
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000564 union nf_inet_addr *addr, bool delim)
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700565{
566 const char *limit = dptr + datalen;
567 const char *start, *end;
568
569 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
570 if (!limit)
571 limit = dptr + datalen;
572
573 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
574 if (!start)
575 return 0;
576
577 start += strlen(name);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000578 if (!sip_parse_addr(ct, start, &end, addr, limit, delim))
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700579 return 0;
580 *matchoff = start - dptr;
581 *matchlen = end - start;
582 return 1;
583}
584EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
585
586/* Parse numerical header parameter and return value, offset and length */
587int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
588 unsigned int dataoff, unsigned int datalen,
589 const char *name,
590 unsigned int *matchoff, unsigned int *matchlen,
591 unsigned int *val)
592{
593 const char *limit = dptr + datalen;
594 const char *start;
595 char *end;
596
597 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
598 if (!limit)
599 limit = dptr + datalen;
600
601 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
602 if (!start)
603 return 0;
604
605 start += strlen(name);
606 *val = simple_strtoul(start, &end, 0);
607 if (start == end)
608 return 0;
609 if (matchoff && matchlen) {
610 *matchoff = start - dptr;
611 *matchlen = end - start;
612 }
613 return 1;
614}
615EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
616
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100617static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
618 unsigned int dataoff, unsigned int datalen,
619 u8 *proto)
620{
621 unsigned int matchoff, matchlen;
622
623 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
624 &matchoff, &matchlen)) {
Rasmus Villemoes18082742014-10-13 15:54:31 -0700625 if (!strncasecmp(dptr + matchoff, "TCP", strlen("TCP")))
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100626 *proto = IPPROTO_TCP;
Rasmus Villemoes18082742014-10-13 15:54:31 -0700627 else if (!strncasecmp(dptr + matchoff, "UDP", strlen("UDP")))
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100628 *proto = IPPROTO_UDP;
629 else
630 return 0;
631
632 if (*proto != nf_ct_protonum(ct))
633 return 0;
634 } else
635 *proto = nf_ct_protonum(ct);
636
637 return 1;
638}
639
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000640static int sdp_parse_addr(const struct nf_conn *ct, const char *cp,
641 const char **endp, union nf_inet_addr *addr,
642 const char *limit)
643{
644 const char *end;
645 int ret;
646
647 memset(addr, 0, sizeof(*addr));
648 switch (nf_ct_l3num(ct)) {
649 case AF_INET:
650 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
651 break;
652 case AF_INET6:
653 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
654 break;
655 default:
656 BUG();
657 }
658
659 if (ret == 0)
660 return 0;
661 if (endp)
662 *endp = end;
663 return 1;
664}
665
666/* skip ip address. returns its length. */
667static int sdp_addr_len(const struct nf_conn *ct, const char *dptr,
668 const char *limit, int *shift)
669{
670 union nf_inet_addr addr;
671 const char *aux = dptr;
672
673 if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) {
674 pr_debug("ip: %s parse failed.!\n", dptr);
675 return 0;
676 }
677
678 return dptr - aux;
679}
680
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700681/* SDP header parsing: a SDP session description contains an ordered set of
682 * headers, starting with a section containing general session parameters,
683 * optionally followed by multiple media descriptions.
684 *
685 * SDP headers always start at the beginning of a line. According to RFC 2327:
686 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
687 * be tolerant and also accept records terminated with a single newline
688 * character". We handle both cases.
689 */
Patrick McHardy9a664822012-08-26 19:14:25 +0200690static const struct sip_header ct_sdp_hdrs_v4[] = {
691 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
692 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len),
693 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len),
694 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
695};
696
697static const struct sip_header ct_sdp_hdrs_v6[] = {
698 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
699 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len),
700 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len),
701 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700702};
703
704/* Linear string search within SDP header values */
705static const char *ct_sdp_header_search(const char *dptr, const char *limit,
706 const char *needle, unsigned int len)
707{
708 for (limit -= len; dptr < limit; dptr++) {
709 if (*dptr == '\r' || *dptr == '\n')
710 break;
711 if (strncmp(dptr, needle, len) == 0)
712 return dptr;
713 }
714 return NULL;
715}
716
717/* Locate a SDP header (optionally a substring within the header value),
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300718 * optionally stopping at the first occurrence of the term header, parse
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700719 * it and return the offset and length of the data we're interested in.
720 */
721int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
722 unsigned int dataoff, unsigned int datalen,
723 enum sdp_header_types type,
724 enum sdp_header_types term,
725 unsigned int *matchoff, unsigned int *matchlen)
726{
Patrick McHardy9a664822012-08-26 19:14:25 +0200727 const struct sip_header *hdrs, *hdr, *thdr;
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700728 const char *start = dptr, *limit = dptr + datalen;
729 int shift = 0;
730
Patrick McHardy9a664822012-08-26 19:14:25 +0200731 hdrs = nf_ct_l3num(ct) == NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6;
732 hdr = &hdrs[type];
733 thdr = &hdrs[term];
734
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700735 for (dptr += dataoff; dptr < limit; dptr++) {
736 /* Find beginning of line */
737 if (*dptr != '\r' && *dptr != '\n')
738 continue;
739 if (++dptr >= limit)
740 break;
741 if (*(dptr - 1) == '\r' && *dptr == '\n') {
742 if (++dptr >= limit)
743 break;
744 }
745
746 if (term != SDP_HDR_UNSPEC &&
747 limit - dptr >= thdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700748 strncasecmp(dptr, thdr->name, thdr->len) == 0)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700749 break;
750 else if (limit - dptr >= hdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700751 strncasecmp(dptr, hdr->name, hdr->len) == 0)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700752 dptr += hdr->len;
753 else
754 continue;
755
756 *matchoff = dptr - start;
757 if (hdr->search) {
758 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
759 hdr->slen);
760 if (!dptr)
761 return -1;
762 dptr += hdr->slen;
763 }
764
765 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
766 if (!*matchlen)
767 return -1;
768 *matchoff = dptr - start + shift;
769 return 1;
770 }
771 return 0;
772}
773EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
774
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700775static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
776 unsigned int dataoff, unsigned int datalen,
777 enum sdp_header_types type,
778 enum sdp_header_types term,
779 unsigned int *matchoff, unsigned int *matchlen,
780 union nf_inet_addr *addr)
781{
782 int ret;
783
784 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
785 matchoff, matchlen);
786 if (ret <= 0)
787 return ret;
788
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000789 if (!sdp_parse_addr(ct, dptr + *matchoff, NULL, addr,
790 dptr + *matchoff + *matchlen))
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700791 return -1;
792 return 1;
793}
794
Patrick McHardy0f32a402008-03-25 20:25:13 -0700795static int refresh_signalling_expectation(struct nf_conn *ct,
796 union nf_inet_addr *addr,
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100797 u8 proto, __be16 port,
Patrick McHardy0f32a402008-03-25 20:25:13 -0700798 unsigned int expires)
799{
800 struct nf_conn_help *help = nfct_help(ct);
801 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800802 struct hlist_node *next;
Patrick McHardy0f32a402008-03-25 20:25:13 -0700803 int found = 0;
804
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100805 spin_lock_bh(&nf_conntrack_expect_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800806 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -0700807 if (exp->class != SIP_EXPECT_SIGNALLING ||
808 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100809 exp->tuple.dst.protonum != proto ||
Patrick McHardy0f32a402008-03-25 20:25:13 -0700810 exp->tuple.dst.u.udp.port != port)
811 continue;
812 if (!del_timer(&exp->timeout))
813 continue;
814 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
815 exp->timeout.expires = jiffies + expires * HZ;
816 add_timer(&exp->timeout);
817 found = 1;
818 break;
819 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100820 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardy0f32a402008-03-25 20:25:13 -0700821 return found;
822}
823
824static void flush_expectations(struct nf_conn *ct, bool media)
Patrick McHardy9467ee32008-03-25 20:24:04 -0700825{
826 struct nf_conn_help *help = nfct_help(ct);
827 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800828 struct hlist_node *next;
Patrick McHardy9467ee32008-03-25 20:24:04 -0700829
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100830 spin_lock_bh(&nf_conntrack_expect_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800831 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -0700832 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
833 continue;
Patrick McHardy9467ee32008-03-25 20:24:04 -0700834 if (!del_timer(&exp->timeout))
835 continue;
836 nf_ct_unlink_expect(exp);
837 nf_ct_expect_put(exp);
Patrick McHardy0f32a402008-03-25 20:25:13 -0700838 if (!media)
839 break;
Patrick McHardy9467ee32008-03-25 20:24:04 -0700840 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100841 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardy9467ee32008-03-25 20:24:04 -0700842}
843
Patrick McHardy051966c2012-08-26 19:14:04 +0200844static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
845 unsigned int dataoff,
Patrick McHardya9c1d352008-03-25 20:25:49 -0700846 const char **dptr, unsigned int *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700847 union nf_inet_addr *daddr, __be16 port,
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700848 enum sip_expectation_classes class,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700849 unsigned int mediaoff, unsigned int medialen)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800850{
Patrick McHardya9c1d352008-03-25 20:25:49 -0700851 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
Patrick McHardy212440a2008-03-25 20:17:13 -0700852 enum ip_conntrack_info ctinfo;
853 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyana5c3a802008-10-08 11:35:09 +0200854 struct net *net = nf_ct_net(ct);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800855 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardyd901a932008-03-25 20:25:32 -0700856 union nf_inet_addr *saddr;
857 struct nf_conntrack_tuple tuple;
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700858 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
Patrick McHardya9c1d352008-03-25 20:25:49 -0700859 u_int16_t base_port;
860 __be16 rtp_port, rtcp_port;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +0200861 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800862
Patrick McHardyd901a932008-03-25 20:25:32 -0700863 saddr = NULL;
864 if (sip_direct_media) {
865 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
866 return NF_ACCEPT;
867 saddr = &ct->tuplehash[!dir].tuple.src.u3;
868 }
869
870 /* We need to check whether the registration exists before attempting
871 * to register it since we can see the same media description multiple
872 * times on different connections in case multiple endpoints receive
873 * the same call.
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700874 *
875 * RTP optimization: if we find a matching media channel expectation
876 * and both the expectation and this connection are SNATed, we assume
877 * both sides can reach each other directly and use the final
878 * destination address from the expectation. We still need to keep
879 * the NATed expectations for media that might arrive from the
880 * outside, and additionally need to expect the direct RTP stream
881 * in case it passes through us even without NAT.
Patrick McHardyd901a932008-03-25 20:25:32 -0700882 */
883 memset(&tuple, 0, sizeof(tuple));
884 if (saddr)
885 tuple.src.u3 = *saddr;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200886 tuple.src.l3num = nf_ct_l3num(ct);
Patrick McHardyd901a932008-03-25 20:25:32 -0700887 tuple.dst.protonum = IPPROTO_UDP;
888 tuple.dst.u3 = *daddr;
889 tuple.dst.u.udp.port = port;
890
891 rcu_read_lock();
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700892 do {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100893 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
Patrick McHardyd901a932008-03-25 20:25:32 -0700894
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700895 if (!exp || exp->master == ct ||
896 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
897 exp->class != class)
898 break;
Patrick McHardye1f9a462008-04-19 17:53:52 -0700899#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardy9a664822012-08-26 19:14:25 +0200900 if (!direct_rtp &&
901 (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) ||
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700902 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
903 ct->status & IPS_NAT_MASK) {
Patrick McHardy9a664822012-08-26 19:14:25 +0200904 *daddr = exp->saved_addr;
905 tuple.dst.u3 = exp->saved_addr;
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700906 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
907 direct_rtp = 1;
908 } else
Patrick McHardye1f9a462008-04-19 17:53:52 -0700909#endif
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700910 skip_expect = 1;
911 } while (!skip_expect);
Patrick McHardyd901a932008-03-25 20:25:32 -0700912
Patrick McHardya9c1d352008-03-25 20:25:49 -0700913 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
914 rtp_port = htons(base_port);
915 rtcp_port = htons(base_port + 1);
916
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700917 if (direct_rtp) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +0200918 hooks = rcu_dereference(nf_nat_sip_hooks);
919 if (hooks &&
920 !hooks->sdp_port(skb, protoff, dataoff, dptr, datalen,
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700921 mediaoff, medialen, ntohs(rtp_port)))
922 goto err1;
923 }
924
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +0200925 if (skip_expect) {
926 rcu_read_unlock();
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700927 return NF_ACCEPT;
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +0200928 }
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700929
Patrick McHardya9c1d352008-03-25 20:25:49 -0700930 rtp_exp = nf_ct_expect_alloc(ct);
931 if (rtp_exp == NULL)
932 goto err1;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200933 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -0700934 IPPROTO_UDP, NULL, &rtp_port);
935
936 rtcp_exp = nf_ct_expect_alloc(ct);
937 if (rtcp_exp == NULL)
938 goto err2;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200939 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -0700940 IPPROTO_UDP, NULL, &rtcp_port);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800941
holger@eitzenberger.org180cf722013-09-30 17:07:28 +0200942 hooks = rcu_dereference(nf_nat_sip_hooks);
943 if (hooks && ct->status & IPS_NAT_MASK && !direct_rtp)
944 ret = hooks->sdp_media(skb, protoff, dataoff, dptr,
945 datalen, rtp_exp, rtcp_exp,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700946 mediaoff, medialen, daddr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800947 else {
Patrick McHardya9c1d352008-03-25 20:25:49 -0700948 if (nf_ct_expect_related(rtp_exp) == 0) {
949 if (nf_ct_expect_related(rtcp_exp) != 0)
950 nf_ct_unexpect_related(rtp_exp);
951 else
952 ret = NF_ACCEPT;
953 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800954 }
Patrick McHardya9c1d352008-03-25 20:25:49 -0700955 nf_ct_expect_put(rtcp_exp);
956err2:
957 nf_ct_expect_put(rtp_exp);
958err1:
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +0200959 rcu_read_unlock();
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800960 return ret;
961}
962
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700963static const struct sdp_media_type sdp_media_types[] = {
964 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
965 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
Patrick McHardy9d288df2010-02-11 12:30:21 +0100966 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE),
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700967};
968
969static const struct sdp_media_type *sdp_media_type(const char *dptr,
970 unsigned int matchoff,
971 unsigned int matchlen)
972{
973 const struct sdp_media_type *t;
974 unsigned int i;
975
976 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
977 t = &sdp_media_types[i];
978 if (matchlen < t->len ||
979 strncmp(dptr + matchoff, t->name, t->len))
980 continue;
981 return t;
982 }
983 return NULL;
984}
985
Patrick McHardy051966c2012-08-26 19:14:04 +0200986static int process_sdp(struct sk_buff *skb, unsigned int protoff,
987 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -0700988 const char **dptr, unsigned int *datalen,
989 unsigned int cseq)
Patrick McHardy7d3dd042008-03-25 20:19:46 -0700990{
991 enum ip_conntrack_info ctinfo;
992 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy7d3dd042008-03-25 20:19:46 -0700993 unsigned int matchoff, matchlen;
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700994 unsigned int mediaoff, medialen;
995 unsigned int sdpoff;
996 unsigned int caddr_len, maddr_len;
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700997 unsigned int i;
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700998 union nf_inet_addr caddr, maddr, rtp_addr;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +0200999 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001000 unsigned int port;
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001001 const struct sdp_media_type *t;
1002 int ret = NF_ACCEPT;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001003
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001004 hooks = rcu_dereference(nf_nat_sip_hooks);
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001005
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001006 /* Find beginning of session description */
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001007 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001008 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001009 &matchoff, &matchlen) <= 0)
1010 return NF_ACCEPT;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001011 sdpoff = matchoff;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001012
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001013 /* The connection information is contained in the session description
1014 * and/or once per media description. The first media description marks
1015 * the end of the session description. */
1016 caddr_len = 0;
1017 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001018 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001019 &matchoff, &matchlen, &caddr) > 0)
1020 caddr_len = matchlen;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001021
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001022 mediaoff = sdpoff;
1023 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
1024 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
1025 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
1026 &mediaoff, &medialen) <= 0)
1027 break;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001028
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001029 /* Get media type and port number. A media port value of zero
1030 * indicates an inactive stream. */
1031 t = sdp_media_type(*dptr, mediaoff, medialen);
1032 if (!t) {
1033 mediaoff += medialen;
1034 continue;
1035 }
1036 mediaoff += t->len;
1037 medialen -= t->len;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001038
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001039 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
1040 if (port == 0)
1041 continue;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001042 if (port < 1024 || port > 65535) {
1043 nf_ct_helper_log(skb, ct, "wrong port %u", port);
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001044 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001045 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001046
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001047 /* The media description overrides the session description. */
1048 maddr_len = 0;
1049 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001050 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001051 &matchoff, &matchlen, &maddr) > 0) {
1052 maddr_len = matchlen;
1053 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
1054 } else if (caddr_len)
1055 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001056 else {
1057 nf_ct_helper_log(skb, ct, "cannot parse SDP message");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001058 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001059 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001060
Patrick McHardy051966c2012-08-26 19:14:04 +02001061 ret = set_expected_rtp_rtcp(skb, protoff, dataoff,
1062 dptr, datalen,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001063 &rtp_addr, htons(port), t->class,
1064 mediaoff, medialen);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001065 if (ret != NF_ACCEPT) {
1066 nf_ct_helper_log(skb, ct,
1067 "cannot add expectation for voice");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001068 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001069 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001070
1071 /* Update media connection address if present */
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001072 if (maddr_len && hooks && ct->status & IPS_NAT_MASK) {
1073 ret = hooks->sdp_addr(skb, protoff, dataoff,
Patrick McHardy9a664822012-08-26 19:14:25 +02001074 dptr, datalen, mediaoff,
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001075 SDP_HDR_CONNECTION,
1076 SDP_HDR_MEDIA,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001077 &rtp_addr);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001078 if (ret != NF_ACCEPT) {
1079 nf_ct_helper_log(skb, ct, "cannot mangle SDP");
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001080 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001081 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001082 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001083 i++;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001084 }
1085
1086 /* Update session connection and owner addresses */
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001087 hooks = rcu_dereference(nf_nat_sip_hooks);
1088 if (hooks && ct->status & IPS_NAT_MASK)
1089 ret = hooks->sdp_session(skb, protoff, dataoff,
1090 dptr, datalen, sdpoff,
1091 &rtp_addr);
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001092
1093 return ret;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001094}
Patrick McHardy051966c2012-08-26 19:14:04 +02001095static int process_invite_response(struct sk_buff *skb, unsigned int protoff,
1096 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001097 const char **dptr, unsigned int *datalen,
1098 unsigned int cseq, unsigned int code)
1099{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001100 enum ip_conntrack_info ctinfo;
1101 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001102 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001103
Patrick McHardy30f33e62008-03-25 20:22:20 -07001104 if ((code >= 100 && code <= 199) ||
1105 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001106 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001107 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001108 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001109 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001110}
1111
Patrick McHardy051966c2012-08-26 19:14:04 +02001112static int process_update_response(struct sk_buff *skb, unsigned int protoff,
1113 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001114 const char **dptr, unsigned int *datalen,
1115 unsigned int cseq, unsigned int code)
1116{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001117 enum ip_conntrack_info ctinfo;
1118 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001119 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001120
Patrick McHardy30f33e62008-03-25 20:22:20 -07001121 if ((code >= 100 && code <= 199) ||
1122 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001123 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001124 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001125 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001126 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001127}
1128
Patrick McHardy051966c2012-08-26 19:14:04 +02001129static int process_prack_response(struct sk_buff *skb, unsigned int protoff,
1130 unsigned int dataoff,
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001131 const char **dptr, unsigned int *datalen,
1132 unsigned int cseq, unsigned int code)
1133{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001134 enum ip_conntrack_info ctinfo;
1135 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001136 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001137
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001138 if ((code >= 100 && code <= 199) ||
1139 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001140 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001141 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001142 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001143 return NF_ACCEPT;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001144}
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001145
Patrick McHardy051966c2012-08-26 19:14:04 +02001146static int process_invite_request(struct sk_buff *skb, unsigned int protoff,
1147 unsigned int dataoff,
Patrick McHardy9d288df2010-02-11 12:30:21 +01001148 const char **dptr, unsigned int *datalen,
1149 unsigned int cseq)
1150{
1151 enum ip_conntrack_info ctinfo;
1152 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001153 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001154 unsigned int ret;
1155
1156 flush_expectations(ct, true);
Patrick McHardy051966c2012-08-26 19:14:04 +02001157 ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001158 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001159 ct_sip_info->invite_cseq = cseq;
Patrick McHardy9d288df2010-02-11 12:30:21 +01001160 return ret;
1161}
1162
Patrick McHardy051966c2012-08-26 19:14:04 +02001163static int process_bye_request(struct sk_buff *skb, unsigned int protoff,
1164 unsigned int dataoff,
Patrick McHardy9467ee32008-03-25 20:24:04 -07001165 const char **dptr, unsigned int *datalen,
1166 unsigned int cseq)
1167{
1168 enum ip_conntrack_info ctinfo;
1169 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1170
Patrick McHardy0f32a402008-03-25 20:25:13 -07001171 flush_expectations(ct, true);
1172 return NF_ACCEPT;
1173}
1174
1175/* Parse a REGISTER request and create a permanent expectation for incoming
1176 * signalling connections. The expectation is marked inactive and is activated
1177 * when receiving a response indicating success from the registrar.
1178 */
Patrick McHardy051966c2012-08-26 19:14:04 +02001179static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1180 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001181 const char **dptr, unsigned int *datalen,
1182 unsigned int cseq)
1183{
1184 enum ip_conntrack_info ctinfo;
1185 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001186 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001187 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001188 unsigned int matchoff, matchlen;
1189 struct nf_conntrack_expect *exp;
1190 union nf_inet_addr *saddr, daddr;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001191 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001192 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001193 u8 proto;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001194 unsigned int expires = 0;
1195 int ret;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001196
1197 /* Expected connections can not register again. */
1198 if (ct->status & IPS_EXPECTED)
1199 return NF_ACCEPT;
1200
1201 /* We must check the expiration time: a value of zero signals the
1202 * registrar to release the binding. We'll remove our expectation
1203 * when receiving the new bindings in the response, but we don't
1204 * want to create new ones.
1205 *
1206 * The expiration time may be contained in Expires: header, the
1207 * Contact: header parameters or the URI parameters.
1208 */
1209 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1210 &matchoff, &matchlen) > 0)
1211 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1212
1213 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1214 SIP_HDR_CONTACT, NULL,
1215 &matchoff, &matchlen, &daddr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001216 if (ret < 0) {
1217 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001218 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001219 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001220 return NF_ACCEPT;
1221
1222 /* We don't support third-party registrations */
1223 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1224 return NF_ACCEPT;
1225
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001226 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1227 &proto) == 0)
1228 return NF_ACCEPT;
1229
Patrick McHardy0f32a402008-03-25 20:25:13 -07001230 if (ct_sip_parse_numerical_param(ct, *dptr,
1231 matchoff + matchlen, *datalen,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001232 "expires=", NULL, NULL, &expires) < 0) {
1233 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001234 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001235 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001236
1237 if (expires == 0) {
1238 ret = NF_ACCEPT;
1239 goto store_cseq;
1240 }
1241
1242 exp = nf_ct_expect_alloc(ct);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001243 if (!exp) {
1244 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001245 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001246 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001247
1248 saddr = NULL;
1249 if (sip_direct_signalling)
1250 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1251
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001252 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001253 saddr, &daddr, proto, NULL, &port);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001254 exp->timeout.expires = sip_timeout * HZ;
1255 exp->helper = nfct_help(ct)->helper;
1256 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1257
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001258 hooks = rcu_dereference(nf_nat_sip_hooks);
1259 if (hooks && ct->status & IPS_NAT_MASK)
1260 ret = hooks->expect(skb, protoff, dataoff, dptr, datalen,
1261 exp, matchoff, matchlen);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001262 else {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001263 if (nf_ct_expect_related(exp) != 0) {
1264 nf_ct_helper_log(skb, ct, "cannot add expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001265 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001266 } else
Patrick McHardy0f32a402008-03-25 20:25:13 -07001267 ret = NF_ACCEPT;
1268 }
1269 nf_ct_expect_put(exp);
1270
1271store_cseq:
1272 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001273 ct_sip_info->register_cseq = cseq;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001274 return ret;
1275}
1276
Patrick McHardy051966c2012-08-26 19:14:04 +02001277static int process_register_response(struct sk_buff *skb, unsigned int protoff,
1278 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001279 const char **dptr, unsigned int *datalen,
1280 unsigned int cseq, unsigned int code)
1281{
1282 enum ip_conntrack_info ctinfo;
1283 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001284 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001285 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1286 union nf_inet_addr addr;
1287 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001288 u8 proto;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001289 unsigned int matchoff, matchlen, coff = 0;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001290 unsigned int expires = 0;
1291 int in_contact = 0, ret;
1292
1293 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1294 * they have received a final response from the registrar for the
1295 * previous one or the previous REGISTER request has timed out".
1296 *
1297 * However, some servers fail to detect retransmissions and send late
1298 * responses, so we store the sequence number of the last valid
1299 * request and compare it here.
1300 */
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001301 if (ct_sip_info->register_cseq != cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001302 return NF_ACCEPT;
1303
1304 if (code >= 100 && code <= 199)
1305 return NF_ACCEPT;
1306 if (code < 200 || code > 299)
1307 goto flush;
1308
1309 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1310 &matchoff, &matchlen) > 0)
1311 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1312
1313 while (1) {
1314 unsigned int c_expires = expires;
1315
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001316 ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001317 SIP_HDR_CONTACT, &in_contact,
1318 &matchoff, &matchlen,
1319 &addr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001320 if (ret < 0) {
1321 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001322 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001323 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001324 break;
1325
1326 /* We don't support third-party registrations */
1327 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1328 continue;
1329
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001330 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1331 *datalen, &proto) == 0)
1332 continue;
1333
Patrick McHardy0f32a402008-03-25 20:25:13 -07001334 ret = ct_sip_parse_numerical_param(ct, *dptr,
1335 matchoff + matchlen,
1336 *datalen, "expires=",
1337 NULL, NULL, &c_expires);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001338 if (ret < 0) {
1339 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001340 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001341 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001342 if (c_expires == 0)
1343 break;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001344 if (refresh_signalling_expectation(ct, &addr, proto, port,
1345 c_expires))
Patrick McHardy0f32a402008-03-25 20:25:13 -07001346 return NF_ACCEPT;
1347 }
1348
1349flush:
1350 flush_expectations(ct, false);
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001351 return NF_ACCEPT;
1352}
1353
Patrick McHardy30f33e62008-03-25 20:22:20 -07001354static const struct sip_handler sip_handlers[] = {
Patrick McHardy9d288df2010-02-11 12:30:21 +01001355 SIP_HANDLER("INVITE", process_invite_request, process_invite_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001356 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001357 SIP_HANDLER("ACK", process_sdp, NULL),
1358 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
Patrick McHardy9467ee32008-03-25 20:24:04 -07001359 SIP_HANDLER("BYE", process_bye_request, NULL),
Patrick McHardy0f32a402008-03-25 20:25:13 -07001360 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001361};
1362
Patrick McHardy051966c2012-08-26 19:14:04 +02001363static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
1364 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001365 const char **dptr, unsigned int *datalen)
1366{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001367 enum ip_conntrack_info ctinfo;
1368 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001369 unsigned int matchoff, matchlen, matchend;
1370 unsigned int code, cseq, i;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001371
1372 if (*datalen < strlen("SIP/2.0 200"))
1373 return NF_ACCEPT;
1374 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001375 if (!code) {
1376 nf_ct_helper_log(skb, ct, "cannot get code");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001377 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001378 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001379
1380 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001381 &matchoff, &matchlen) <= 0) {
1382 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001383 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001384 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001385 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Christophe Leroy0d35d0812016-08-03 12:41:40 +02001386 if (!cseq && *(*dptr + matchoff) != '0') {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001387 nf_ct_helper_log(skb, ct, "cannot get cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001388 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001389 }
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001390 matchend = matchoff + matchlen + 1;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001391
1392 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001393 const struct sip_handler *handler;
1394
Patrick McHardy30f33e62008-03-25 20:22:20 -07001395 handler = &sip_handlers[i];
1396 if (handler->response == NULL)
1397 continue;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001398 if (*datalen < matchend + handler->len ||
Rasmus Villemoes18082742014-10-13 15:54:31 -07001399 strncasecmp(*dptr + matchend, handler->method, handler->len))
Patrick McHardy30f33e62008-03-25 20:22:20 -07001400 continue;
Patrick McHardy051966c2012-08-26 19:14:04 +02001401 return handler->response(skb, protoff, dataoff, dptr, datalen,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001402 cseq, code);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001403 }
1404 return NF_ACCEPT;
1405}
1406
Patrick McHardy051966c2012-08-26 19:14:04 +02001407static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
1408 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001409 const char **dptr, unsigned int *datalen)
1410{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001411 enum ip_conntrack_info ctinfo;
1412 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Kevin Cernekee72665072012-12-17 18:33:58 +00001413 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1414 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001415 unsigned int matchoff, matchlen;
1416 unsigned int cseq, i;
Kevin Cernekee72665072012-12-17 18:33:58 +00001417 union nf_inet_addr addr;
1418 __be16 port;
1419
1420 /* Many Cisco IP phones use a high source port for SIP requests, but
1421 * listen for the response on port 5060. If we are the local
1422 * router for one of these phones, save the port number from the
1423 * Via: header so that nf_nat_sip can redirect the responses to
1424 * the correct port.
1425 */
1426 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1427 SIP_HDR_VIA_UDP, NULL, &matchoff,
1428 &matchlen, &addr, &port) > 0 &&
1429 port != ct->tuplehash[dir].tuple.src.u.udp.port &&
1430 nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3))
1431 ct_sip_info->forced_dport = port;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001432
1433 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001434 const struct sip_handler *handler;
1435
Patrick McHardy30f33e62008-03-25 20:22:20 -07001436 handler = &sip_handlers[i];
1437 if (handler->request == NULL)
1438 continue;
1439 if (*datalen < handler->len ||
Rasmus Villemoes18082742014-10-13 15:54:31 -07001440 strncasecmp(*dptr, handler->method, handler->len))
Patrick McHardy30f33e62008-03-25 20:22:20 -07001441 continue;
1442
1443 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001444 &matchoff, &matchlen) <= 0) {
1445 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001446 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001447 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001448 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Christophe Leroy0d35d0812016-08-03 12:41:40 +02001449 if (!cseq && *(*dptr + matchoff) != '0') {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001450 nf_ct_helper_log(skb, ct, "cannot get cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001451 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001452 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001453
Patrick McHardy051966c2012-08-26 19:14:04 +02001454 return handler->request(skb, protoff, dataoff, dptr, datalen,
1455 cseq);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001456 }
1457 return NF_ACCEPT;
1458}
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001459
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001460static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
Patrick McHardy051966c2012-08-26 19:14:04 +02001461 unsigned int protoff, unsigned int dataoff,
1462 const char **dptr, unsigned int *datalen)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001463{
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001464 const struct nf_nat_sip_hooks *hooks;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001465 int ret;
1466
Rasmus Villemoes18082742014-10-13 15:54:31 -07001467 if (strncasecmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
Patrick McHardy051966c2012-08-26 19:14:04 +02001468 ret = process_sip_request(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001469 else
Patrick McHardy051966c2012-08-26 19:14:04 +02001470 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001471
Patrick McHardy9a664822012-08-26 19:14:25 +02001472 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001473 hooks = rcu_dereference(nf_nat_sip_hooks);
1474 if (hooks && !hooks->msg(skb, protoff, dataoff,
1475 dptr, datalen)) {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001476 nf_ct_helper_log(skb, ct, "cannot NAT SIP message");
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001477 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001478 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001479 }
1480
1481 return ret;
1482}
1483
1484static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1485 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1486{
1487 struct tcphdr *th, _tcph;
1488 unsigned int dataoff, datalen;
1489 unsigned int matchoff, matchlen, clen;
1490 unsigned int msglen, origlen;
1491 const char *dptr, *end;
1492 s16 diff, tdiff = 0;
Simon Horman78748962010-09-21 21:17:30 +00001493 int ret = NF_ACCEPT;
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001494 bool term;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001495
1496 if (ctinfo != IP_CT_ESTABLISHED &&
Eric Dumazetfb048832011-05-19 15:44:27 +02001497 ctinfo != IP_CT_ESTABLISHED_REPLY)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001498 return NF_ACCEPT;
1499
1500 /* No Data ? */
1501 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1502 if (th == NULL)
1503 return NF_ACCEPT;
1504 dataoff = protoff + th->doff * 4;
1505 if (dataoff >= skb->len)
1506 return NF_ACCEPT;
1507
1508 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1509
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02001510 if (unlikely(skb_linearize(skb)))
1511 return NF_DROP;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001512
1513 dptr = skb->data + dataoff;
1514 datalen = skb->len - dataoff;
1515 if (datalen < strlen("SIP/2.0 200"))
1516 return NF_ACCEPT;
1517
1518 while (1) {
1519 if (ct_sip_get_header(ct, dptr, 0, datalen,
1520 SIP_HDR_CONTENT_LENGTH,
1521 &matchoff, &matchlen) <= 0)
1522 break;
1523
1524 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1525 if (dptr + matchoff == end)
1526 break;
1527
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001528 term = false;
1529 for (; end + strlen("\r\n\r\n") <= dptr + datalen; end++) {
1530 if (end[0] == '\r' && end[1] == '\n' &&
1531 end[2] == '\r' && end[3] == '\n') {
1532 term = true;
1533 break;
1534 }
1535 }
1536 if (!term)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001537 break;
1538 end += strlen("\r\n\r\n") + clen;
1539
1540 msglen = origlen = end - dptr;
Patrick McHardy3a7b21e2013-04-05 08:13:30 +00001541 if (msglen > datalen)
1542 return NF_ACCEPT;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001543
Patrick McHardy051966c2012-08-26 19:14:04 +02001544 ret = process_sip_msg(skb, ct, protoff, dataoff,
1545 &dptr, &msglen);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001546 /* process_sip_* functions report why this packet is dropped */
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001547 if (ret != NF_ACCEPT)
1548 break;
1549 diff = msglen - origlen;
1550 tdiff += diff;
1551
1552 dataoff += msglen;
1553 dptr += msglen;
1554 datalen = datalen + diff - msglen;
1555 }
1556
Patrick McHardy9a664822012-08-26 19:14:25 +02001557 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001558 const struct nf_nat_sip_hooks *hooks;
1559
1560 hooks = rcu_dereference(nf_nat_sip_hooks);
1561 if (hooks)
1562 hooks->seq_adjust(skb, protoff, tdiff);
Patrick McHardy48f8ac22010-02-11 12:29:38 +01001563 }
1564
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001565 return ret;
1566}
1567
1568static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
1569 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001570{
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001571 unsigned int dataoff, datalen;
1572 const char *dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001573
1574 /* No Data ? */
1575 dataoff = protoff + sizeof(struct udphdr);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001576 if (dataoff >= skb->len)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001577 return NF_ACCEPT;
1578
Herbert Xu3db05fe2007-10-15 00:53:15 -07001579 nf_ct_refresh(ct, skb, sip_timeout * HZ);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001580
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02001581 if (unlikely(skb_linearize(skb)))
1582 return NF_DROP;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001583
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001584 dptr = skb->data + dataoff;
Herbert Xu3db05fe2007-10-15 00:53:15 -07001585 datalen = skb->len - dataoff;
Patrick McHardy779382e2008-03-25 20:17:36 -07001586 if (datalen < strlen("SIP/2.0 200"))
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001587 return NF_ACCEPT;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001588
Patrick McHardy051966c2012-08-26 19:14:04 +02001589 return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001590}
1591
Gao Feng82de0be2016-07-18 11:39:23 +08001592static struct nf_conntrack_helper sip[MAX_PORTS * 4] __read_mostly;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001593
Patrick McHardy0f32a402008-03-25 20:25:13 -07001594static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1595 [SIP_EXPECT_SIGNALLING] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01001596 .name = "signalling",
Patrick McHardy0f32a402008-03-25 20:25:13 -07001597 .max_expected = 1,
1598 .timeout = 3 * 60,
1599 },
1600 [SIP_EXPECT_AUDIO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01001601 .name = "audio",
Patrick McHardya9c1d352008-03-25 20:25:49 -07001602 .max_expected = 2 * IP_CT_DIR_MAX,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001603 .timeout = 3 * 60,
1604 },
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001605 [SIP_EXPECT_VIDEO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01001606 .name = "video",
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001607 .max_expected = 2 * IP_CT_DIR_MAX,
1608 .timeout = 3 * 60,
1609 },
Patrick McHardy9d288df2010-02-11 12:30:21 +01001610 [SIP_EXPECT_IMAGE] = {
1611 .name = "image",
1612 .max_expected = IP_CT_DIR_MAX,
1613 .timeout = 3 * 60,
1614 },
Patrick McHardy6002f2662008-03-25 20:09:15 -07001615};
1616
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001617static void nf_conntrack_sip_fini(void)
1618{
Gao Feng82de0be2016-07-18 11:39:23 +08001619 nf_conntrack_helpers_unregister(sip, ports_c * 4);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001620}
1621
1622static int __init nf_conntrack_sip_init(void)
1623{
Gao Feng82de0be2016-07-18 11:39:23 +08001624 int i, ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001625
1626 if (ports_c == 0)
1627 ports[ports_c++] = SIP_PORT;
1628
1629 for (i = 0; i < ports_c; i++) {
1630 memset(&sip[i], 0, sizeof(sip[i]));
1631
Gao Feng82de0be2016-07-18 11:39:23 +08001632 nf_ct_helper_init(&sip[4 * i], AF_INET, IPPROTO_UDP, "sip",
1633 SIP_PORT, ports[i], i, sip_exp_policy,
1634 SIP_EXPECT_MAX,
1635 sizeof(struct nf_ct_sip_master), sip_help_udp,
1636 NULL, THIS_MODULE);
1637 nf_ct_helper_init(&sip[4 * i + 1], AF_INET, IPPROTO_TCP, "sip",
1638 SIP_PORT, ports[i], i, sip_exp_policy,
1639 SIP_EXPECT_MAX,
1640 sizeof(struct nf_ct_sip_master), sip_help_tcp,
1641 NULL, THIS_MODULE);
1642 nf_ct_helper_init(&sip[4 * i + 2], AF_INET6, IPPROTO_UDP, "sip",
1643 SIP_PORT, ports[i], i, sip_exp_policy,
1644 SIP_EXPECT_MAX,
1645 sizeof(struct nf_ct_sip_master), sip_help_udp,
1646 NULL, THIS_MODULE);
1647 nf_ct_helper_init(&sip[4 * i + 3], AF_INET6, IPPROTO_TCP, "sip",
1648 SIP_PORT, ports[i], i, sip_exp_policy,
1649 SIP_EXPECT_MAX,
1650 sizeof(struct nf_ct_sip_master), sip_help_tcp,
1651 NULL, THIS_MODULE);
1652 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001653
Gao Feng82de0be2016-07-18 11:39:23 +08001654 ret = nf_conntrack_helpers_register(sip, ports_c * 4);
1655 if (ret < 0) {
1656 pr_err("failed to register helpers\n");
1657 return ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001658 }
1659 return 0;
1660}
1661
1662module_init(nf_conntrack_sip_init);
1663module_exit(nf_conntrack_sip_fini);