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