]>
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 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/ctype.h> | |
15 | #include <linux/skbuff.h> | |
16 | #include <linux/inet.h> | |
17 | #include <linux/in.h> | |
18 | #include <linux/udp.h> | |
1863f096 | 19 | #include <linux/netfilter.h> |
9fafcd7b PM |
20 | |
21 | #include <net/netfilter/nf_conntrack.h> | |
9467ee38 | 22 | #include <net/netfilter/nf_conntrack_core.h> |
9fafcd7b PM |
23 | #include <net/netfilter/nf_conntrack_expect.h> |
24 | #include <net/netfilter/nf_conntrack_helper.h> | |
25 | #include <linux/netfilter/nf_conntrack_sip.h> | |
26 | ||
9fafcd7b PM |
27 | MODULE_LICENSE("GPL"); |
28 | MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>"); | |
29 | MODULE_DESCRIPTION("SIP connection tracking helper"); | |
30 | MODULE_ALIAS("ip_conntrack_sip"); | |
31 | ||
32 | #define MAX_PORTS 8 | |
33 | static unsigned short ports[MAX_PORTS]; | |
2f0d2f10 | 34 | static unsigned int ports_c; |
9fafcd7b PM |
35 | module_param_array(ports, ushort, &ports_c, 0400); |
36 | MODULE_PARM_DESC(ports, "port numbers of SIP servers"); | |
37 | ||
38 | static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT; | |
39 | module_param(sip_timeout, uint, 0600); | |
40 | MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session"); | |
41 | ||
0f32a40f PM |
42 | static int sip_direct_signalling __read_mostly = 1; |
43 | module_param(sip_direct_signalling, int, 0600); | |
44 | MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar " | |
45 | "only (default 1)"); | |
46 | ||
d901a936 PM |
47 | static int sip_direct_media __read_mostly = 1; |
48 | module_param(sip_direct_media, int, 0600); | |
49 | MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling " | |
50 | "endpoints only (default 1)"); | |
51 | ||
3db05fea | 52 | unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, |
2a6cfb22 PM |
53 | const char **dptr, |
54 | unsigned int *datalen) __read_mostly; | |
9fafcd7b PM |
55 | EXPORT_SYMBOL_GPL(nf_nat_sip_hook); |
56 | ||
0f32a40f PM |
57 | unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb, |
58 | const char **dptr, | |
59 | unsigned int *datalen, | |
60 | struct nf_conntrack_expect *exp, | |
61 | unsigned int matchoff, | |
62 | unsigned int matchlen) __read_mostly; | |
63 | EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook); | |
64 | ||
4ab9e64e PM |
65 | unsigned int (*nf_nat_sdp_addr_hook)(struct sk_buff *skb, |
66 | const char **dptr, | |
67 | unsigned int dataoff, | |
68 | unsigned int *datalen, | |
69 | enum sdp_header_types type, | |
70 | enum sdp_header_types term, | |
71 | const union nf_inet_addr *addr) | |
72 | __read_mostly; | |
73 | EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook); | |
74 | ||
c7f485ab PM |
75 | unsigned int (*nf_nat_sdp_port_hook)(struct sk_buff *skb, |
76 | const char **dptr, | |
77 | unsigned int *datalen, | |
78 | unsigned int matchoff, | |
79 | unsigned int matchlen, | |
80 | u_int16_t port) __read_mostly; | |
81 | EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook); | |
82 | ||
4ab9e64e PM |
83 | unsigned int (*nf_nat_sdp_session_hook)(struct sk_buff *skb, |
84 | const char **dptr, | |
85 | unsigned int dataoff, | |
86 | unsigned int *datalen, | |
87 | const union nf_inet_addr *addr) | |
88 | __read_mostly; | |
89 | EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook); | |
90 | ||
91 | unsigned int (*nf_nat_sdp_media_hook)(struct sk_buff *skb, | |
92 | const char **dptr, | |
93 | unsigned int *datalen, | |
94 | struct nf_conntrack_expect *rtp_exp, | |
95 | struct nf_conntrack_expect *rtcp_exp, | |
96 | unsigned int mediaoff, | |
97 | unsigned int medialen, | |
98 | union nf_inet_addr *rtp_addr) | |
99 | __read_mostly; | |
100 | EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook); | |
9fafcd7b | 101 | |
ac367740 PM |
102 | static int string_len(const struct nf_conn *ct, const char *dptr, |
103 | const char *limit, int *shift) | |
104 | { | |
105 | int len = 0; | |
106 | ||
107 | while (dptr < limit && isalpha(*dptr)) { | |
108 | dptr++; | |
109 | len++; | |
110 | } | |
111 | return len; | |
112 | } | |
113 | ||
13f7d63c | 114 | static int digits_len(const struct nf_conn *ct, const char *dptr, |
9fafcd7b PM |
115 | const char *limit, int *shift) |
116 | { | |
117 | int len = 0; | |
b1ec488b | 118 | while (dptr < limit && isdigit(*dptr)) { |
9fafcd7b PM |
119 | dptr++; |
120 | len++; | |
121 | } | |
122 | return len; | |
123 | } | |
124 | ||
0d0ab037 PM |
125 | /* get media type + port length */ |
126 | static int media_len(const struct nf_conn *ct, const char *dptr, | |
127 | const char *limit, int *shift) | |
128 | { | |
129 | int len = string_len(ct, dptr, limit, shift); | |
130 | ||
131 | dptr += len; | |
132 | if (dptr >= limit || *dptr != ' ') | |
133 | return 0; | |
134 | len++; | |
135 | dptr++; | |
136 | ||
137 | return len + digits_len(ct, dptr, limit, shift); | |
138 | } | |
139 | ||
13f7d63c JE |
140 | static int parse_addr(const struct nf_conn *ct, const char *cp, |
141 | const char **endp, union nf_inet_addr *addr, | |
142 | const char *limit) | |
9fafcd7b PM |
143 | { |
144 | const char *end; | |
145 | int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; | |
146 | int ret = 0; | |
147 | ||
fa913ddf | 148 | memset(addr, 0, sizeof(*addr)); |
9fafcd7b PM |
149 | switch (family) { |
150 | case AF_INET: | |
151 | ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end); | |
152 | break; | |
153 | case AF_INET6: | |
154 | ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end); | |
155 | break; | |
156 | default: | |
157 | BUG(); | |
158 | } | |
159 | ||
160 | if (ret == 0 || end == cp) | |
161 | return 0; | |
162 | if (endp) | |
163 | *endp = end; | |
164 | return 1; | |
165 | } | |
166 | ||
167 | /* skip ip address. returns its length. */ | |
13f7d63c | 168 | static int epaddr_len(const struct nf_conn *ct, const char *dptr, |
9fafcd7b PM |
169 | const char *limit, int *shift) |
170 | { | |
643a2c15 | 171 | union nf_inet_addr addr; |
9fafcd7b PM |
172 | const char *aux = dptr; |
173 | ||
174 | if (!parse_addr(ct, dptr, &dptr, &addr, limit)) { | |
0d53778e | 175 | pr_debug("ip: %s parse failed.!\n", dptr); |
9fafcd7b PM |
176 | return 0; |
177 | } | |
178 | ||
179 | /* Port number */ | |
180 | if (*dptr == ':') { | |
181 | dptr++; | |
182 | dptr += digits_len(ct, dptr, limit, shift); | |
183 | } | |
184 | return dptr - aux; | |
185 | } | |
186 | ||
187 | /* get address length, skiping user info. */ | |
13f7d63c | 188 | static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr, |
9fafcd7b PM |
189 | const char *limit, int *shift) |
190 | { | |
aa584eda | 191 | const char *start = dptr; |
9fafcd7b PM |
192 | int s = *shift; |
193 | ||
7da5bfbb LI |
194 | /* Search for @, but stop at the end of the line. |
195 | * We are inside a sip: URI, so we don't need to worry about | |
196 | * continuation lines. */ | |
b1ec488b | 197 | while (dptr < limit && |
7da5bfbb | 198 | *dptr != '@' && *dptr != '\r' && *dptr != '\n') { |
9fafcd7b | 199 | (*shift)++; |
7da5bfbb LI |
200 | dptr++; |
201 | } | |
9fafcd7b | 202 | |
b1ec488b | 203 | if (dptr < limit && *dptr == '@') { |
9fafcd7b PM |
204 | dptr++; |
205 | (*shift)++; | |
aa584eda PM |
206 | } else { |
207 | dptr = start; | |
9fafcd7b | 208 | *shift = s; |
aa584eda | 209 | } |
9fafcd7b PM |
210 | |
211 | return epaddr_len(ct, dptr, limit, shift); | |
212 | } | |
213 | ||
ac367740 PM |
214 | /* Parse a SIP request line of the form: |
215 | * | |
216 | * Request-Line = Method SP Request-URI SP SIP-Version CRLF | |
217 | * | |
218 | * and return the offset and length of the address contained in the Request-URI. | |
219 | */ | |
220 | int ct_sip_parse_request(const struct nf_conn *ct, | |
221 | const char *dptr, unsigned int datalen, | |
624f8b7b PM |
222 | unsigned int *matchoff, unsigned int *matchlen, |
223 | union nf_inet_addr *addr, __be16 *port) | |
ac367740 | 224 | { |
624f8b7b | 225 | const char *start = dptr, *limit = dptr + datalen, *end; |
ac367740 | 226 | unsigned int mlen; |
624f8b7b | 227 | unsigned int p; |
ac367740 PM |
228 | int shift = 0; |
229 | ||
230 | /* Skip method and following whitespace */ | |
231 | mlen = string_len(ct, dptr, limit, NULL); | |
232 | if (!mlen) | |
233 | return 0; | |
234 | dptr += mlen; | |
235 | if (++dptr >= limit) | |
236 | return 0; | |
237 | ||
238 | /* Find SIP URI */ | |
239 | limit -= strlen("sip:"); | |
240 | for (; dptr < limit; dptr++) { | |
241 | if (*dptr == '\r' || *dptr == '\n') | |
242 | return -1; | |
243 | if (strnicmp(dptr, "sip:", strlen("sip:")) == 0) | |
244 | break; | |
245 | } | |
624f8b7b | 246 | if (!skp_epaddr_len(ct, dptr, limit, &shift)) |
ac367740 | 247 | return 0; |
624f8b7b PM |
248 | dptr += shift; |
249 | ||
250 | if (!parse_addr(ct, dptr, &end, addr, limit)) | |
251 | return -1; | |
252 | if (end < limit && *end == ':') { | |
253 | end++; | |
254 | p = simple_strtoul(end, (char **)&end, 10); | |
255 | if (p < 1024 || p > 65535) | |
256 | return -1; | |
257 | *port = htons(p); | |
258 | } else | |
259 | *port = htons(SIP_PORT); | |
260 | ||
261 | if (end == dptr) | |
262 | return 0; | |
263 | *matchoff = dptr - start; | |
264 | *matchlen = end - dptr; | |
ac367740 PM |
265 | return 1; |
266 | } | |
267 | EXPORT_SYMBOL_GPL(ct_sip_parse_request); | |
268 | ||
ea45f12a PM |
269 | /* SIP header parsing: SIP headers are located at the beginning of a line, but |
270 | * may span several lines, in which case the continuation lines begin with a | |
271 | * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or | |
272 | * CRLF, RFC 3261 allows only CRLF, we support both. | |
273 | * | |
274 | * Headers are followed by (optionally) whitespace, a colon, again (optionally) | |
275 | * whitespace and the values. Whitespace in this context means any amount of | |
276 | * tabs, spaces and continuation lines, which are treated as a single whitespace | |
277 | * character. | |
05e3ced2 PM |
278 | * |
279 | * Some headers may appear multiple times. A comma seperated list of values is | |
280 | * equivalent to multiple headers. | |
ea45f12a PM |
281 | */ |
282 | static const struct sip_header ct_sip_hdrs[] = { | |
30f33e6d | 283 | [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len), |
ea45f12a PM |
284 | [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len), |
285 | [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len), | |
286 | [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len), | |
287 | [SIP_HDR_VIA] = SIP_HDR("Via", "v", "UDP ", epaddr_len), | |
0f32a40f | 288 | [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len), |
ea45f12a PM |
289 | [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len), |
290 | }; | |
291 | ||
292 | static const char *sip_follow_continuation(const char *dptr, const char *limit) | |
9fafcd7b | 293 | { |
ea45f12a PM |
294 | /* Walk past newline */ |
295 | if (++dptr >= limit) | |
296 | return NULL; | |
297 | ||
298 | /* Skip '\n' in CR LF */ | |
299 | if (*(dptr - 1) == '\r' && *dptr == '\n') { | |
300 | if (++dptr >= limit) | |
301 | return NULL; | |
302 | } | |
9fafcd7b | 303 | |
ea45f12a PM |
304 | /* Continuation line? */ |
305 | if (*dptr != ' ' && *dptr != '\t') | |
306 | return NULL; | |
9fafcd7b | 307 | |
ea45f12a PM |
308 | /* skip leading whitespace */ |
309 | for (; dptr < limit; dptr++) { | |
310 | if (*dptr != ' ' && *dptr != '\t') | |
311 | break; | |
312 | } | |
313 | return dptr; | |
314 | } | |
315 | ||
316 | static const char *sip_skip_whitespace(const char *dptr, const char *limit) | |
317 | { | |
318 | for (; dptr < limit; dptr++) { | |
319 | if (*dptr == ' ') | |
320 | continue; | |
321 | if (*dptr != '\r' && *dptr != '\n') | |
322 | break; | |
323 | dptr = sip_follow_continuation(dptr, limit); | |
324 | if (dptr == NULL) | |
325 | return NULL; | |
326 | } | |
327 | return dptr; | |
328 | } | |
329 | ||
330 | /* Search within a SIP header value, dealing with continuation lines */ | |
331 | static const char *ct_sip_header_search(const char *dptr, const char *limit, | |
332 | const char *needle, unsigned int len) | |
333 | { | |
334 | for (limit -= len; dptr < limit; dptr++) { | |
335 | if (*dptr == '\r' || *dptr == '\n') { | |
336 | dptr = sip_follow_continuation(dptr, limit); | |
337 | if (dptr == NULL) | |
338 | break; | |
9fafcd7b PM |
339 | continue; |
340 | } | |
ea45f12a PM |
341 | |
342 | if (strnicmp(dptr, needle, len) == 0) | |
343 | return dptr; | |
344 | } | |
345 | return NULL; | |
346 | } | |
347 | ||
348 | int ct_sip_get_header(const struct nf_conn *ct, const char *dptr, | |
349 | unsigned int dataoff, unsigned int datalen, | |
350 | enum sip_header_types type, | |
351 | unsigned int *matchoff, unsigned int *matchlen) | |
352 | { | |
353 | const struct sip_header *hdr = &ct_sip_hdrs[type]; | |
354 | const char *start = dptr, *limit = dptr + datalen; | |
355 | int shift = 0; | |
356 | ||
357 | for (dptr += dataoff; dptr < limit; dptr++) { | |
358 | /* Find beginning of line */ | |
359 | if (*dptr != '\r' && *dptr != '\n') | |
360 | continue; | |
361 | if (++dptr >= limit) | |
362 | break; | |
363 | if (*(dptr - 1) == '\r' && *dptr == '\n') { | |
364 | if (++dptr >= limit) | |
365 | break; | |
9fafcd7b | 366 | } |
9fafcd7b | 367 | |
ea45f12a PM |
368 | /* Skip continuation lines */ |
369 | if (*dptr == ' ' || *dptr == '\t') | |
370 | continue; | |
9fafcd7b | 371 | |
ea45f12a PM |
372 | /* Find header. Compact headers must be followed by a |
373 | * non-alphabetic character to avoid mismatches. */ | |
374 | if (limit - dptr >= hdr->len && | |
375 | strnicmp(dptr, hdr->name, hdr->len) == 0) | |
376 | dptr += hdr->len; | |
377 | else if (hdr->cname && limit - dptr >= hdr->clen + 1 && | |
378 | strnicmp(dptr, hdr->cname, hdr->clen) == 0 && | |
379 | !isalpha(*(dptr + hdr->clen + 1))) | |
380 | dptr += hdr->clen; | |
381 | else | |
382 | continue; | |
9fafcd7b | 383 | |
ea45f12a PM |
384 | /* Find and skip colon */ |
385 | dptr = sip_skip_whitespace(dptr, limit); | |
386 | if (dptr == NULL) | |
387 | break; | |
388 | if (*dptr != ':' || ++dptr >= limit) | |
389 | break; | |
390 | ||
391 | /* Skip whitespace after colon */ | |
392 | dptr = sip_skip_whitespace(dptr, limit); | |
393 | if (dptr == NULL) | |
394 | break; | |
395 | ||
396 | *matchoff = dptr - start; | |
397 | if (hdr->search) { | |
398 | dptr = ct_sip_header_search(dptr, limit, hdr->search, | |
399 | hdr->slen); | |
400 | if (!dptr) | |
401 | return -1; | |
402 | dptr += hdr->slen; | |
403 | } | |
404 | ||
405 | *matchlen = hdr->match_len(ct, dptr, limit, &shift); | |
406 | if (!*matchlen) | |
407 | return -1; | |
408 | *matchoff = dptr - start + shift; | |
9fafcd7b PM |
409 | return 1; |
410 | } | |
9fafcd7b PM |
411 | return 0; |
412 | } | |
ea45f12a | 413 | EXPORT_SYMBOL_GPL(ct_sip_get_header); |
9fafcd7b | 414 | |
05e3ced2 PM |
415 | /* Get next header field in a list of comma seperated values */ |
416 | static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr, | |
417 | unsigned int dataoff, unsigned int datalen, | |
418 | enum sip_header_types type, | |
419 | unsigned int *matchoff, unsigned int *matchlen) | |
420 | { | |
421 | const struct sip_header *hdr = &ct_sip_hdrs[type]; | |
422 | const char *start = dptr, *limit = dptr + datalen; | |
423 | int shift = 0; | |
424 | ||
425 | dptr += dataoff; | |
426 | ||
427 | dptr = ct_sip_header_search(dptr, limit, ",", strlen(",")); | |
428 | if (!dptr) | |
429 | return 0; | |
430 | ||
431 | dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen); | |
432 | if (!dptr) | |
433 | return 0; | |
434 | dptr += hdr->slen; | |
435 | ||
436 | *matchoff = dptr - start; | |
437 | *matchlen = hdr->match_len(ct, dptr, limit, &shift); | |
438 | if (!*matchlen) | |
439 | return -1; | |
440 | *matchoff += shift; | |
441 | return 1; | |
442 | } | |
443 | ||
444 | /* Walk through headers until a parsable one is found or no header of the | |
445 | * given type is left. */ | |
446 | static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr, | |
447 | unsigned int dataoff, unsigned int datalen, | |
448 | enum sip_header_types type, int *in_header, | |
449 | unsigned int *matchoff, unsigned int *matchlen) | |
450 | { | |
451 | int ret; | |
452 | ||
453 | if (in_header && *in_header) { | |
454 | while (1) { | |
455 | ret = ct_sip_next_header(ct, dptr, dataoff, datalen, | |
456 | type, matchoff, matchlen); | |
457 | if (ret > 0) | |
458 | return ret; | |
459 | if (ret == 0) | |
460 | break; | |
461 | dataoff += *matchoff; | |
462 | } | |
463 | *in_header = 0; | |
464 | } | |
465 | ||
466 | while (1) { | |
467 | ret = ct_sip_get_header(ct, dptr, dataoff, datalen, | |
468 | type, matchoff, matchlen); | |
469 | if (ret > 0) | |
470 | break; | |
471 | if (ret == 0) | |
472 | return ret; | |
473 | dataoff += *matchoff; | |
474 | } | |
475 | ||
476 | if (in_header) | |
477 | *in_header = 1; | |
478 | return 1; | |
479 | } | |
480 | ||
481 | /* Locate a SIP header, parse the URI and return the offset and length of | |
482 | * the address as well as the address and port themselves. A stream of | |
483 | * headers can be parsed by handing in a non-NULL datalen and in_header | |
484 | * pointer. | |
485 | */ | |
486 | int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr, | |
487 | unsigned int *dataoff, unsigned int datalen, | |
488 | enum sip_header_types type, int *in_header, | |
489 | unsigned int *matchoff, unsigned int *matchlen, | |
490 | union nf_inet_addr *addr, __be16 *port) | |
491 | { | |
492 | const char *c, *limit = dptr + datalen; | |
493 | unsigned int p; | |
494 | int ret; | |
495 | ||
496 | ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen, | |
497 | type, in_header, matchoff, matchlen); | |
498 | WARN_ON(ret < 0); | |
499 | if (ret == 0) | |
500 | return ret; | |
501 | ||
502 | if (!parse_addr(ct, dptr + *matchoff, &c, addr, limit)) | |
503 | return -1; | |
504 | if (*c == ':') { | |
505 | c++; | |
506 | p = simple_strtoul(c, (char **)&c, 10); | |
507 | if (p < 1024 || p > 65535) | |
508 | return -1; | |
509 | *port = htons(p); | |
510 | } else | |
511 | *port = htons(SIP_PORT); | |
512 | ||
513 | if (dataoff) | |
514 | *dataoff = c - dptr; | |
515 | return 1; | |
516 | } | |
517 | EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri); | |
518 | ||
2bbb2116 PM |
519 | /* Parse address from header parameter and return address, offset and length */ |
520 | int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr, | |
521 | unsigned int dataoff, unsigned int datalen, | |
522 | const char *name, | |
523 | unsigned int *matchoff, unsigned int *matchlen, | |
524 | union nf_inet_addr *addr) | |
525 | { | |
526 | const char *limit = dptr + datalen; | |
527 | const char *start, *end; | |
528 | ||
529 | limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(",")); | |
530 | if (!limit) | |
531 | limit = dptr + datalen; | |
532 | ||
533 | start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name)); | |
534 | if (!start) | |
535 | return 0; | |
536 | ||
537 | start += strlen(name); | |
538 | if (!parse_addr(ct, start, &end, addr, limit)) | |
539 | return 0; | |
540 | *matchoff = start - dptr; | |
541 | *matchlen = end - start; | |
542 | return 1; | |
543 | } | |
544 | EXPORT_SYMBOL_GPL(ct_sip_parse_address_param); | |
545 | ||
546 | /* Parse numerical header parameter and return value, offset and length */ | |
547 | int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr, | |
548 | unsigned int dataoff, unsigned int datalen, | |
549 | const char *name, | |
550 | unsigned int *matchoff, unsigned int *matchlen, | |
551 | unsigned int *val) | |
552 | { | |
553 | const char *limit = dptr + datalen; | |
554 | const char *start; | |
555 | char *end; | |
556 | ||
557 | limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(",")); | |
558 | if (!limit) | |
559 | limit = dptr + datalen; | |
560 | ||
561 | start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name)); | |
562 | if (!start) | |
563 | return 0; | |
564 | ||
565 | start += strlen(name); | |
566 | *val = simple_strtoul(start, &end, 0); | |
567 | if (start == end) | |
568 | return 0; | |
569 | if (matchoff && matchlen) { | |
570 | *matchoff = start - dptr; | |
571 | *matchlen = end - start; | |
572 | } | |
573 | return 1; | |
574 | } | |
575 | EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param); | |
576 | ||
3e9b4600 PM |
577 | /* SDP header parsing: a SDP session description contains an ordered set of |
578 | * headers, starting with a section containing general session parameters, | |
579 | * optionally followed by multiple media descriptions. | |
580 | * | |
581 | * SDP headers always start at the beginning of a line. According to RFC 2327: | |
582 | * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should | |
583 | * be tolerant and also accept records terminated with a single newline | |
584 | * character". We handle both cases. | |
585 | */ | |
586 | static const struct sip_header ct_sdp_hdrs[] = { | |
587 | [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len), | |
588 | [SDP_HDR_OWNER_IP4] = SDP_HDR("o=", "IN IP4 ", epaddr_len), | |
589 | [SDP_HDR_CONNECTION_IP4] = SDP_HDR("c=", "IN IP4 ", epaddr_len), | |
590 | [SDP_HDR_OWNER_IP6] = SDP_HDR("o=", "IN IP6 ", epaddr_len), | |
591 | [SDP_HDR_CONNECTION_IP6] = SDP_HDR("c=", "IN IP6 ", epaddr_len), | |
0d0ab037 | 592 | [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len), |
3e9b4600 PM |
593 | }; |
594 | ||
595 | /* Linear string search within SDP header values */ | |
596 | static const char *ct_sdp_header_search(const char *dptr, const char *limit, | |
597 | const char *needle, unsigned int len) | |
598 | { | |
599 | for (limit -= len; dptr < limit; dptr++) { | |
600 | if (*dptr == '\r' || *dptr == '\n') | |
601 | break; | |
602 | if (strncmp(dptr, needle, len) == 0) | |
603 | return dptr; | |
604 | } | |
605 | return NULL; | |
606 | } | |
607 | ||
608 | /* Locate a SDP header (optionally a substring within the header value), | |
609 | * optionally stopping at the first occurence of the term header, parse | |
610 | * it and return the offset and length of the data we're interested in. | |
611 | */ | |
612 | int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr, | |
613 | unsigned int dataoff, unsigned int datalen, | |
614 | enum sdp_header_types type, | |
615 | enum sdp_header_types term, | |
616 | unsigned int *matchoff, unsigned int *matchlen) | |
617 | { | |
618 | const struct sip_header *hdr = &ct_sdp_hdrs[type]; | |
619 | const struct sip_header *thdr = &ct_sdp_hdrs[term]; | |
620 | const char *start = dptr, *limit = dptr + datalen; | |
621 | int shift = 0; | |
622 | ||
623 | for (dptr += dataoff; dptr < limit; dptr++) { | |
624 | /* Find beginning of line */ | |
625 | if (*dptr != '\r' && *dptr != '\n') | |
626 | continue; | |
627 | if (++dptr >= limit) | |
628 | break; | |
629 | if (*(dptr - 1) == '\r' && *dptr == '\n') { | |
630 | if (++dptr >= limit) | |
631 | break; | |
632 | } | |
633 | ||
634 | if (term != SDP_HDR_UNSPEC && | |
635 | limit - dptr >= thdr->len && | |
636 | strnicmp(dptr, thdr->name, thdr->len) == 0) | |
637 | break; | |
638 | else if (limit - dptr >= hdr->len && | |
639 | strnicmp(dptr, hdr->name, hdr->len) == 0) | |
640 | dptr += hdr->len; | |
641 | else | |
642 | continue; | |
643 | ||
644 | *matchoff = dptr - start; | |
645 | if (hdr->search) { | |
646 | dptr = ct_sdp_header_search(dptr, limit, hdr->search, | |
647 | hdr->slen); | |
648 | if (!dptr) | |
649 | return -1; | |
650 | dptr += hdr->slen; | |
651 | } | |
652 | ||
653 | *matchlen = hdr->match_len(ct, dptr, limit, &shift); | |
654 | if (!*matchlen) | |
655 | return -1; | |
656 | *matchoff = dptr - start + shift; | |
657 | return 1; | |
658 | } | |
659 | return 0; | |
660 | } | |
661 | EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header); | |
662 | ||
4ab9e64e PM |
663 | static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr, |
664 | unsigned int dataoff, unsigned int datalen, | |
665 | enum sdp_header_types type, | |
666 | enum sdp_header_types term, | |
667 | unsigned int *matchoff, unsigned int *matchlen, | |
668 | union nf_inet_addr *addr) | |
669 | { | |
670 | int ret; | |
671 | ||
672 | ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term, | |
673 | matchoff, matchlen); | |
674 | if (ret <= 0) | |
675 | return ret; | |
676 | ||
677 | if (!parse_addr(ct, dptr + *matchoff, NULL, addr, | |
678 | dptr + *matchoff + *matchlen)) | |
679 | return -1; | |
680 | return 1; | |
681 | } | |
682 | ||
0f32a40f PM |
683 | static int refresh_signalling_expectation(struct nf_conn *ct, |
684 | union nf_inet_addr *addr, | |
685 | __be16 port, | |
686 | unsigned int expires) | |
9467ee38 PM |
687 | { |
688 | struct nf_conn_help *help = nfct_help(ct); | |
689 | struct nf_conntrack_expect *exp; | |
690 | struct hlist_node *n, *next; | |
0f32a40f | 691 | int found = 0; |
9467ee38 PM |
692 | |
693 | spin_lock_bh(&nf_conntrack_lock); | |
694 | hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) { | |
0f32a40f PM |
695 | if (exp->class != SIP_EXPECT_SIGNALLING || |
696 | !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) || | |
697 | exp->tuple.dst.u.udp.port != port) | |
698 | continue; | |
699 | if (!del_timer(&exp->timeout)) | |
700 | continue; | |
701 | exp->flags &= ~NF_CT_EXPECT_INACTIVE; | |
702 | exp->timeout.expires = jiffies + expires * HZ; | |
703 | add_timer(&exp->timeout); | |
704 | found = 1; | |
705 | break; | |
706 | } | |
707 | spin_unlock_bh(&nf_conntrack_lock); | |
708 | return found; | |
709 | } | |
710 | ||
711 | static void flush_expectations(struct nf_conn *ct, bool media) | |
712 | { | |
713 | struct nf_conn_help *help = nfct_help(ct); | |
714 | struct nf_conntrack_expect *exp; | |
715 | struct hlist_node *n, *next; | |
716 | ||
717 | spin_lock_bh(&nf_conntrack_lock); | |
718 | hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) { | |
719 | if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media) | |
720 | continue; | |
9467ee38 PM |
721 | if (!del_timer(&exp->timeout)) |
722 | continue; | |
723 | nf_ct_unlink_expect(exp); | |
724 | nf_ct_expect_put(exp); | |
0f32a40f PM |
725 | if (!media) |
726 | break; | |
9467ee38 PM |
727 | } |
728 | spin_unlock_bh(&nf_conntrack_lock); | |
729 | } | |
730 | ||
a9c1d359 PM |
731 | static int set_expected_rtp_rtcp(struct sk_buff *skb, |
732 | const char **dptr, unsigned int *datalen, | |
4ab9e64e | 733 | union nf_inet_addr *daddr, __be16 port, |
0d0ab037 | 734 | enum sip_expectation_classes class, |
4ab9e64e | 735 | unsigned int mediaoff, unsigned int medialen) |
9fafcd7b | 736 | { |
a9c1d359 | 737 | struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp; |
212440a7 PM |
738 | enum ip_conntrack_info ctinfo; |
739 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
9fafcd7b | 740 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
d901a936 PM |
741 | union nf_inet_addr *saddr; |
742 | struct nf_conntrack_tuple tuple; | |
9fafcd7b | 743 | int family = ct->tuplehash[!dir].tuple.src.l3num; |
c7f485ab | 744 | int direct_rtp = 0, skip_expect = 0, ret = NF_DROP; |
a9c1d359 PM |
745 | u_int16_t base_port; |
746 | __be16 rtp_port, rtcp_port; | |
c7f485ab | 747 | typeof(nf_nat_sdp_port_hook) nf_nat_sdp_port; |
4ab9e64e | 748 | typeof(nf_nat_sdp_media_hook) nf_nat_sdp_media; |
9fafcd7b | 749 | |
d901a936 PM |
750 | saddr = NULL; |
751 | if (sip_direct_media) { | |
752 | if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3)) | |
753 | return NF_ACCEPT; | |
754 | saddr = &ct->tuplehash[!dir].tuple.src.u3; | |
755 | } | |
756 | ||
757 | /* We need to check whether the registration exists before attempting | |
758 | * to register it since we can see the same media description multiple | |
759 | * times on different connections in case multiple endpoints receive | |
760 | * the same call. | |
c7f485ab PM |
761 | * |
762 | * RTP optimization: if we find a matching media channel expectation | |
763 | * and both the expectation and this connection are SNATed, we assume | |
764 | * both sides can reach each other directly and use the final | |
765 | * destination address from the expectation. We still need to keep | |
766 | * the NATed expectations for media that might arrive from the | |
767 | * outside, and additionally need to expect the direct RTP stream | |
768 | * in case it passes through us even without NAT. | |
d901a936 PM |
769 | */ |
770 | memset(&tuple, 0, sizeof(tuple)); | |
771 | if (saddr) | |
772 | tuple.src.u3 = *saddr; | |
773 | tuple.src.l3num = family; | |
774 | tuple.dst.protonum = IPPROTO_UDP; | |
775 | tuple.dst.u3 = *daddr; | |
776 | tuple.dst.u.udp.port = port; | |
777 | ||
778 | rcu_read_lock(); | |
c7f485ab PM |
779 | do { |
780 | exp = __nf_ct_expect_find(&tuple); | |
d901a936 | 781 | |
c7f485ab PM |
782 | if (!exp || exp->master == ct || |
783 | nfct_help(exp->master)->helper != nfct_help(ct)->helper || | |
784 | exp->class != class) | |
785 | break; | |
786 | ||
787 | if (exp->tuple.src.l3num == AF_INET && !direct_rtp && | |
788 | (exp->saved_ip != exp->tuple.dst.u3.ip || | |
789 | exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) && | |
790 | ct->status & IPS_NAT_MASK) { | |
791 | daddr->ip = exp->saved_ip; | |
792 | tuple.dst.u3.ip = exp->saved_ip; | |
793 | tuple.dst.u.udp.port = exp->saved_proto.udp.port; | |
794 | direct_rtp = 1; | |
795 | } else | |
796 | skip_expect = 1; | |
797 | } while (!skip_expect); | |
798 | rcu_read_unlock(); | |
d901a936 | 799 | |
a9c1d359 PM |
800 | base_port = ntohs(tuple.dst.u.udp.port) & ~1; |
801 | rtp_port = htons(base_port); | |
802 | rtcp_port = htons(base_port + 1); | |
803 | ||
c7f485ab PM |
804 | if (direct_rtp) { |
805 | nf_nat_sdp_port = rcu_dereference(nf_nat_sdp_port_hook); | |
806 | if (nf_nat_sdp_port && | |
807 | !nf_nat_sdp_port(skb, dptr, datalen, | |
808 | mediaoff, medialen, ntohs(rtp_port))) | |
809 | goto err1; | |
810 | } | |
811 | ||
812 | if (skip_expect) | |
813 | return NF_ACCEPT; | |
814 | ||
a9c1d359 PM |
815 | rtp_exp = nf_ct_expect_alloc(ct); |
816 | if (rtp_exp == NULL) | |
817 | goto err1; | |
0d0ab037 | 818 | nf_ct_expect_init(rtp_exp, class, family, saddr, daddr, |
a9c1d359 PM |
819 | IPPROTO_UDP, NULL, &rtp_port); |
820 | ||
821 | rtcp_exp = nf_ct_expect_alloc(ct); | |
822 | if (rtcp_exp == NULL) | |
823 | goto err2; | |
0d0ab037 | 824 | nf_ct_expect_init(rtcp_exp, class, family, saddr, daddr, |
a9c1d359 | 825 | IPPROTO_UDP, NULL, &rtcp_port); |
9fafcd7b | 826 | |
4ab9e64e | 827 | nf_nat_sdp_media = rcu_dereference(nf_nat_sdp_media_hook); |
c7f485ab | 828 | if (nf_nat_sdp_media && ct->status & IPS_NAT_MASK && !direct_rtp) |
4ab9e64e PM |
829 | ret = nf_nat_sdp_media(skb, dptr, datalen, rtp_exp, rtcp_exp, |
830 | mediaoff, medialen, daddr); | |
9fafcd7b | 831 | else { |
a9c1d359 PM |
832 | if (nf_ct_expect_related(rtp_exp) == 0) { |
833 | if (nf_ct_expect_related(rtcp_exp) != 0) | |
834 | nf_ct_unexpect_related(rtp_exp); | |
835 | else | |
836 | ret = NF_ACCEPT; | |
837 | } | |
9fafcd7b | 838 | } |
a9c1d359 PM |
839 | nf_ct_expect_put(rtcp_exp); |
840 | err2: | |
841 | nf_ct_expect_put(rtp_exp); | |
842 | err1: | |
9fafcd7b PM |
843 | return ret; |
844 | } | |
845 | ||
0d0ab037 PM |
846 | static const struct sdp_media_type sdp_media_types[] = { |
847 | SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO), | |
848 | SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO), | |
849 | }; | |
850 | ||
851 | static const struct sdp_media_type *sdp_media_type(const char *dptr, | |
852 | unsigned int matchoff, | |
853 | unsigned int matchlen) | |
854 | { | |
855 | const struct sdp_media_type *t; | |
856 | unsigned int i; | |
857 | ||
858 | for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) { | |
859 | t = &sdp_media_types[i]; | |
860 | if (matchlen < t->len || | |
861 | strncmp(dptr + matchoff, t->name, t->len)) | |
862 | continue; | |
863 | return t; | |
864 | } | |
865 | return NULL; | |
866 | } | |
867 | ||
7d3dd043 | 868 | static int process_sdp(struct sk_buff *skb, |
30f33e6d PM |
869 | const char **dptr, unsigned int *datalen, |
870 | unsigned int cseq) | |
7d3dd043 PM |
871 | { |
872 | enum ip_conntrack_info ctinfo; | |
873 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
874 | int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; | |
875 | unsigned int matchoff, matchlen; | |
4ab9e64e PM |
876 | unsigned int mediaoff, medialen; |
877 | unsigned int sdpoff; | |
878 | unsigned int caddr_len, maddr_len; | |
0d0ab037 | 879 | unsigned int i; |
4ab9e64e | 880 | union nf_inet_addr caddr, maddr, rtp_addr; |
7d3dd043 | 881 | unsigned int port; |
4ab9e64e | 882 | enum sdp_header_types c_hdr; |
0d0ab037 PM |
883 | const struct sdp_media_type *t; |
884 | int ret = NF_ACCEPT; | |
4ab9e64e PM |
885 | typeof(nf_nat_sdp_addr_hook) nf_nat_sdp_addr; |
886 | typeof(nf_nat_sdp_session_hook) nf_nat_sdp_session; | |
7d3dd043 | 887 | |
0d0ab037 | 888 | nf_nat_sdp_addr = rcu_dereference(nf_nat_sdp_addr_hook); |
4ab9e64e PM |
889 | c_hdr = family == AF_INET ? SDP_HDR_CONNECTION_IP4 : |
890 | SDP_HDR_CONNECTION_IP6; | |
7d3dd043 | 891 | |
4ab9e64e | 892 | /* Find beginning of session description */ |
7d3dd043 | 893 | if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen, |
4ab9e64e | 894 | SDP_HDR_VERSION, SDP_HDR_UNSPEC, |
7d3dd043 PM |
895 | &matchoff, &matchlen) <= 0) |
896 | return NF_ACCEPT; | |
4ab9e64e PM |
897 | sdpoff = matchoff; |
898 | ||
899 | /* The connection information is contained in the session description | |
900 | * and/or once per media description. The first media description marks | |
901 | * the end of the session description. */ | |
902 | caddr_len = 0; | |
903 | if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen, | |
904 | c_hdr, SDP_HDR_MEDIA, | |
905 | &matchoff, &matchlen, &caddr) > 0) | |
906 | caddr_len = matchlen; | |
907 | ||
0d0ab037 PM |
908 | mediaoff = sdpoff; |
909 | for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) { | |
910 | if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen, | |
911 | SDP_HDR_MEDIA, SDP_HDR_UNSPEC, | |
912 | &mediaoff, &medialen) <= 0) | |
913 | break; | |
7d3dd043 | 914 | |
0d0ab037 PM |
915 | /* Get media type and port number. A media port value of zero |
916 | * indicates an inactive stream. */ | |
917 | t = sdp_media_type(*dptr, mediaoff, medialen); | |
918 | if (!t) { | |
919 | mediaoff += medialen; | |
920 | continue; | |
921 | } | |
922 | mediaoff += t->len; | |
923 | medialen -= t->len; | |
7d3dd043 | 924 | |
0d0ab037 PM |
925 | port = simple_strtoul(*dptr + mediaoff, NULL, 10); |
926 | if (port == 0) | |
927 | continue; | |
928 | if (port < 1024 || port > 65535) | |
929 | return NF_DROP; | |
4ab9e64e | 930 | |
0d0ab037 PM |
931 | /* The media description overrides the session description. */ |
932 | maddr_len = 0; | |
933 | if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen, | |
934 | c_hdr, SDP_HDR_MEDIA, | |
935 | &matchoff, &matchlen, &maddr) > 0) { | |
936 | maddr_len = matchlen; | |
937 | memcpy(&rtp_addr, &maddr, sizeof(rtp_addr)); | |
938 | } else if (caddr_len) | |
939 | memcpy(&rtp_addr, &caddr, sizeof(rtp_addr)); | |
940 | else | |
941 | return NF_DROP; | |
942 | ||
943 | ret = set_expected_rtp_rtcp(skb, dptr, datalen, | |
944 | &rtp_addr, htons(port), t->class, | |
945 | mediaoff, medialen); | |
946 | if (ret != NF_ACCEPT) | |
947 | return ret; | |
4ab9e64e | 948 | |
0d0ab037 PM |
949 | /* Update media connection address if present */ |
950 | if (maddr_len && nf_nat_sdp_addr && ct->status & IPS_NAT_MASK) { | |
4ab9e64e PM |
951 | ret = nf_nat_sdp_addr(skb, dptr, mediaoff, datalen, |
952 | c_hdr, SDP_HDR_MEDIA, &rtp_addr); | |
953 | if (ret != NF_ACCEPT) | |
954 | return ret; | |
955 | } | |
0d0ab037 | 956 | i++; |
4ab9e64e PM |
957 | } |
958 | ||
959 | /* Update session connection and owner addresses */ | |
960 | nf_nat_sdp_session = rcu_dereference(nf_nat_sdp_session_hook); | |
961 | if (nf_nat_sdp_session && ct->status & IPS_NAT_MASK) | |
962 | ret = nf_nat_sdp_session(skb, dptr, sdpoff, datalen, &rtp_addr); | |
963 | ||
964 | return ret; | |
7d3dd043 | 965 | } |
30f33e6d PM |
966 | static int process_invite_response(struct sk_buff *skb, |
967 | const char **dptr, unsigned int *datalen, | |
968 | unsigned int cseq, unsigned int code) | |
969 | { | |
9467ee38 PM |
970 | enum ip_conntrack_info ctinfo; |
971 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
972 | ||
30f33e6d PM |
973 | if ((code >= 100 && code <= 199) || |
974 | (code >= 200 && code <= 299)) | |
975 | return process_sdp(skb, dptr, datalen, cseq); | |
9467ee38 | 976 | else { |
0f32a40f | 977 | flush_expectations(ct, true); |
9467ee38 PM |
978 | return NF_ACCEPT; |
979 | } | |
30f33e6d PM |
980 | } |
981 | ||
982 | static int process_update_response(struct sk_buff *skb, | |
983 | const char **dptr, unsigned int *datalen, | |
984 | unsigned int cseq, unsigned int code) | |
985 | { | |
9467ee38 PM |
986 | enum ip_conntrack_info ctinfo; |
987 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
988 | ||
30f33e6d PM |
989 | if ((code >= 100 && code <= 199) || |
990 | (code >= 200 && code <= 299)) | |
991 | return process_sdp(skb, dptr, datalen, cseq); | |
9467ee38 | 992 | else { |
0f32a40f | 993 | flush_expectations(ct, true); |
9467ee38 PM |
994 | return NF_ACCEPT; |
995 | } | |
30f33e6d PM |
996 | } |
997 | ||
595a8ecb PM |
998 | static int process_prack_response(struct sk_buff *skb, |
999 | const char **dptr, unsigned int *datalen, | |
1000 | unsigned int cseq, unsigned int code) | |
1001 | { | |
9467ee38 PM |
1002 | enum ip_conntrack_info ctinfo; |
1003 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
1004 | ||
595a8ecb PM |
1005 | if ((code >= 100 && code <= 199) || |
1006 | (code >= 200 && code <= 299)) | |
1007 | return process_sdp(skb, dptr, datalen, cseq); | |
9467ee38 | 1008 | else { |
0f32a40f | 1009 | flush_expectations(ct, true); |
9467ee38 PM |
1010 | return NF_ACCEPT; |
1011 | } | |
1012 | } | |
1013 | ||
1014 | static int process_bye_request(struct sk_buff *skb, | |
1015 | const char **dptr, unsigned int *datalen, | |
1016 | unsigned int cseq) | |
1017 | { | |
1018 | enum ip_conntrack_info ctinfo; | |
1019 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
595a8ecb | 1020 | |
0f32a40f PM |
1021 | flush_expectations(ct, true); |
1022 | return NF_ACCEPT; | |
1023 | } | |
1024 | ||
1025 | /* Parse a REGISTER request and create a permanent expectation for incoming | |
1026 | * signalling connections. The expectation is marked inactive and is activated | |
1027 | * when receiving a response indicating success from the registrar. | |
1028 | */ | |
1029 | static int process_register_request(struct sk_buff *skb, | |
1030 | const char **dptr, unsigned int *datalen, | |
1031 | unsigned int cseq) | |
1032 | { | |
1033 | enum ip_conntrack_info ctinfo; | |
1034 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
1035 | struct nf_conn_help *help = nfct_help(ct); | |
1036 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); | |
1037 | int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; | |
1038 | unsigned int matchoff, matchlen; | |
1039 | struct nf_conntrack_expect *exp; | |
1040 | union nf_inet_addr *saddr, daddr; | |
1041 | __be16 port; | |
1042 | unsigned int expires = 0; | |
1043 | int ret; | |
1044 | typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect; | |
1045 | ||
1046 | /* Expected connections can not register again. */ | |
1047 | if (ct->status & IPS_EXPECTED) | |
1048 | return NF_ACCEPT; | |
1049 | ||
1050 | /* We must check the expiration time: a value of zero signals the | |
1051 | * registrar to release the binding. We'll remove our expectation | |
1052 | * when receiving the new bindings in the response, but we don't | |
1053 | * want to create new ones. | |
1054 | * | |
1055 | * The expiration time may be contained in Expires: header, the | |
1056 | * Contact: header parameters or the URI parameters. | |
1057 | */ | |
1058 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES, | |
1059 | &matchoff, &matchlen) > 0) | |
1060 | expires = simple_strtoul(*dptr + matchoff, NULL, 10); | |
1061 | ||
1062 | ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, | |
1063 | SIP_HDR_CONTACT, NULL, | |
1064 | &matchoff, &matchlen, &daddr, &port); | |
1065 | if (ret < 0) | |
1066 | return NF_DROP; | |
1067 | else if (ret == 0) | |
1068 | return NF_ACCEPT; | |
1069 | ||
1070 | /* We don't support third-party registrations */ | |
1071 | if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr)) | |
1072 | return NF_ACCEPT; | |
1073 | ||
1074 | if (ct_sip_parse_numerical_param(ct, *dptr, | |
1075 | matchoff + matchlen, *datalen, | |
1076 | "expires=", NULL, NULL, &expires) < 0) | |
1077 | return NF_DROP; | |
1078 | ||
1079 | if (expires == 0) { | |
1080 | ret = NF_ACCEPT; | |
1081 | goto store_cseq; | |
1082 | } | |
1083 | ||
1084 | exp = nf_ct_expect_alloc(ct); | |
1085 | if (!exp) | |
1086 | return NF_DROP; | |
1087 | ||
1088 | saddr = NULL; | |
1089 | if (sip_direct_signalling) | |
1090 | saddr = &ct->tuplehash[!dir].tuple.src.u3; | |
1091 | ||
1092 | nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, family, saddr, &daddr, | |
1093 | IPPROTO_UDP, NULL, &port); | |
1094 | exp->timeout.expires = sip_timeout * HZ; | |
1095 | exp->helper = nfct_help(ct)->helper; | |
1096 | exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE; | |
1097 | ||
1098 | nf_nat_sip_expect = rcu_dereference(nf_nat_sip_expect_hook); | |
1099 | if (nf_nat_sip_expect && ct->status & IPS_NAT_MASK) | |
1100 | ret = nf_nat_sip_expect(skb, dptr, datalen, exp, | |
1101 | matchoff, matchlen); | |
1102 | else { | |
1103 | if (nf_ct_expect_related(exp) != 0) | |
1104 | ret = NF_DROP; | |
1105 | else | |
1106 | ret = NF_ACCEPT; | |
1107 | } | |
1108 | nf_ct_expect_put(exp); | |
1109 | ||
1110 | store_cseq: | |
1111 | if (ret == NF_ACCEPT) | |
1112 | help->help.ct_sip_info.register_cseq = cseq; | |
1113 | return ret; | |
1114 | } | |
1115 | ||
1116 | static int process_register_response(struct sk_buff *skb, | |
1117 | const char **dptr, unsigned int *datalen, | |
1118 | unsigned int cseq, unsigned int code) | |
1119 | { | |
1120 | enum ip_conntrack_info ctinfo; | |
1121 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
1122 | struct nf_conn_help *help = nfct_help(ct); | |
1123 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); | |
1124 | union nf_inet_addr addr; | |
1125 | __be16 port; | |
1126 | unsigned int matchoff, matchlen, dataoff = 0; | |
1127 | unsigned int expires = 0; | |
1128 | int in_contact = 0, ret; | |
1129 | ||
1130 | /* According to RFC 3261, "UAs MUST NOT send a new registration until | |
1131 | * they have received a final response from the registrar for the | |
1132 | * previous one or the previous REGISTER request has timed out". | |
1133 | * | |
1134 | * However, some servers fail to detect retransmissions and send late | |
1135 | * responses, so we store the sequence number of the last valid | |
1136 | * request and compare it here. | |
1137 | */ | |
1138 | if (help->help.ct_sip_info.register_cseq != cseq) | |
1139 | return NF_ACCEPT; | |
1140 | ||
1141 | if (code >= 100 && code <= 199) | |
1142 | return NF_ACCEPT; | |
1143 | if (code < 200 || code > 299) | |
1144 | goto flush; | |
1145 | ||
1146 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES, | |
1147 | &matchoff, &matchlen) > 0) | |
1148 | expires = simple_strtoul(*dptr + matchoff, NULL, 10); | |
1149 | ||
1150 | while (1) { | |
1151 | unsigned int c_expires = expires; | |
1152 | ||
1153 | ret = ct_sip_parse_header_uri(ct, *dptr, &dataoff, *datalen, | |
1154 | SIP_HDR_CONTACT, &in_contact, | |
1155 | &matchoff, &matchlen, | |
1156 | &addr, &port); | |
1157 | if (ret < 0) | |
1158 | return NF_DROP; | |
1159 | else if (ret == 0) | |
1160 | break; | |
1161 | ||
1162 | /* We don't support third-party registrations */ | |
1163 | if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr)) | |
1164 | continue; | |
1165 | ||
1166 | ret = ct_sip_parse_numerical_param(ct, *dptr, | |
1167 | matchoff + matchlen, | |
1168 | *datalen, "expires=", | |
1169 | NULL, NULL, &c_expires); | |
1170 | if (ret < 0) | |
1171 | return NF_DROP; | |
1172 | if (c_expires == 0) | |
1173 | break; | |
1174 | if (refresh_signalling_expectation(ct, &addr, port, c_expires)) | |
1175 | return NF_ACCEPT; | |
1176 | } | |
1177 | ||
1178 | flush: | |
1179 | flush_expectations(ct, false); | |
595a8ecb PM |
1180 | return NF_ACCEPT; |
1181 | } | |
1182 | ||
30f33e6d PM |
1183 | static const struct sip_handler sip_handlers[] = { |
1184 | SIP_HANDLER("INVITE", process_sdp, process_invite_response), | |
1185 | SIP_HANDLER("UPDATE", process_sdp, process_update_response), | |
595a8ecb PM |
1186 | SIP_HANDLER("ACK", process_sdp, NULL), |
1187 | SIP_HANDLER("PRACK", process_sdp, process_prack_response), | |
9467ee38 | 1188 | SIP_HANDLER("BYE", process_bye_request, NULL), |
0f32a40f | 1189 | SIP_HANDLER("REGISTER", process_register_request, process_register_response), |
30f33e6d PM |
1190 | }; |
1191 | ||
1192 | static int process_sip_response(struct sk_buff *skb, | |
1193 | const char **dptr, unsigned int *datalen) | |
1194 | { | |
1195 | static const struct sip_handler *handler; | |
1196 | enum ip_conntrack_info ctinfo; | |
1197 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
1198 | unsigned int matchoff, matchlen; | |
1199 | unsigned int code, cseq, dataoff, i; | |
1200 | ||
1201 | if (*datalen < strlen("SIP/2.0 200")) | |
1202 | return NF_ACCEPT; | |
1203 | code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10); | |
1204 | if (!code) | |
1205 | return NF_DROP; | |
1206 | ||
1207 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ, | |
1208 | &matchoff, &matchlen) <= 0) | |
1209 | return NF_DROP; | |
1210 | cseq = simple_strtoul(*dptr + matchoff, NULL, 10); | |
1211 | if (!cseq) | |
1212 | return NF_DROP; | |
1213 | dataoff = matchoff + matchlen + 1; | |
1214 | ||
1215 | for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) { | |
1216 | handler = &sip_handlers[i]; | |
1217 | if (handler->response == NULL) | |
1218 | continue; | |
1219 | if (*datalen < dataoff + handler->len || | |
1220 | strnicmp(*dptr + dataoff, handler->method, handler->len)) | |
1221 | continue; | |
1222 | return handler->response(skb, dptr, datalen, cseq, code); | |
1223 | } | |
1224 | return NF_ACCEPT; | |
1225 | } | |
1226 | ||
1227 | static int process_sip_request(struct sk_buff *skb, | |
1228 | const char **dptr, unsigned int *datalen) | |
1229 | { | |
1230 | static const struct sip_handler *handler; | |
1231 | enum ip_conntrack_info ctinfo; | |
1232 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); | |
1233 | unsigned int matchoff, matchlen; | |
1234 | unsigned int cseq, i; | |
1235 | ||
1236 | for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) { | |
1237 | handler = &sip_handlers[i]; | |
1238 | if (handler->request == NULL) | |
1239 | continue; | |
1240 | if (*datalen < handler->len || | |
1241 | strnicmp(*dptr, handler->method, handler->len)) | |
1242 | continue; | |
1243 | ||
1244 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ, | |
1245 | &matchoff, &matchlen) <= 0) | |
1246 | return NF_DROP; | |
1247 | cseq = simple_strtoul(*dptr + matchoff, NULL, 10); | |
1248 | if (!cseq) | |
1249 | return NF_DROP; | |
1250 | ||
1251 | return handler->request(skb, dptr, datalen, cseq); | |
1252 | } | |
1253 | return NF_ACCEPT; | |
1254 | } | |
7d3dd043 | 1255 | |
3db05fea | 1256 | static int sip_help(struct sk_buff *skb, |
9fafcd7b PM |
1257 | unsigned int protoff, |
1258 | struct nf_conn *ct, | |
1259 | enum ip_conntrack_info ctinfo) | |
1260 | { | |
9fafcd7b PM |
1261 | unsigned int dataoff, datalen; |
1262 | const char *dptr; | |
33cb1e9a | 1263 | int ret; |
9fafcd7b PM |
1264 | typeof(nf_nat_sip_hook) nf_nat_sip; |
1265 | ||
1266 | /* No Data ? */ | |
1267 | dataoff = protoff + sizeof(struct udphdr); | |
3db05fea | 1268 | if (dataoff >= skb->len) |
9fafcd7b PM |
1269 | return NF_ACCEPT; |
1270 | ||
3db05fea | 1271 | nf_ct_refresh(ct, skb, sip_timeout * HZ); |
9fafcd7b | 1272 | |
3db05fea HX |
1273 | if (!skb_is_nonlinear(skb)) |
1274 | dptr = skb->data + dataoff; | |
9fafcd7b | 1275 | else { |
0d53778e | 1276 | pr_debug("Copy of skbuff not supported yet.\n"); |
7d3dd043 | 1277 | return NF_ACCEPT; |
9fafcd7b PM |
1278 | } |
1279 | ||
3db05fea | 1280 | datalen = skb->len - dataoff; |
779382eb | 1281 | if (datalen < strlen("SIP/2.0 200")) |
7d3dd043 | 1282 | return NF_ACCEPT; |
9fafcd7b | 1283 | |
30f33e6d | 1284 | if (strnicmp(dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0) |
33cb1e9a | 1285 | ret = process_sip_request(skb, &dptr, &datalen); |
30f33e6d | 1286 | else |
33cb1e9a PM |
1287 | ret = process_sip_response(skb, &dptr, &datalen); |
1288 | ||
1289 | if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) { | |
1290 | nf_nat_sip = rcu_dereference(nf_nat_sip_hook); | |
1291 | if (nf_nat_sip && !nf_nat_sip(skb, &dptr, &datalen)) | |
1292 | ret = NF_DROP; | |
1293 | } | |
1294 | ||
1295 | return ret; | |
9fafcd7b PM |
1296 | } |
1297 | ||
1298 | static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly; | |
1299 | static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly; | |
1300 | ||
0f32a40f PM |
1301 | static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = { |
1302 | [SIP_EXPECT_SIGNALLING] = { | |
1303 | .max_expected = 1, | |
1304 | .timeout = 3 * 60, | |
1305 | }, | |
1306 | [SIP_EXPECT_AUDIO] = { | |
a9c1d359 | 1307 | .max_expected = 2 * IP_CT_DIR_MAX, |
0f32a40f PM |
1308 | .timeout = 3 * 60, |
1309 | }, | |
0d0ab037 PM |
1310 | [SIP_EXPECT_VIDEO] = { |
1311 | .max_expected = 2 * IP_CT_DIR_MAX, | |
1312 | .timeout = 3 * 60, | |
1313 | }, | |
6002f266 PM |
1314 | }; |
1315 | ||
9fafcd7b PM |
1316 | static void nf_conntrack_sip_fini(void) |
1317 | { | |
1318 | int i, j; | |
1319 | ||
1320 | for (i = 0; i < ports_c; i++) { | |
1321 | for (j = 0; j < 2; j++) { | |
1322 | if (sip[i][j].me == NULL) | |
1323 | continue; | |
1324 | nf_conntrack_helper_unregister(&sip[i][j]); | |
1325 | } | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | static int __init nf_conntrack_sip_init(void) | |
1330 | { | |
1331 | int i, j, ret; | |
1332 | char *tmpname; | |
1333 | ||
1334 | if (ports_c == 0) | |
1335 | ports[ports_c++] = SIP_PORT; | |
1336 | ||
1337 | for (i = 0; i < ports_c; i++) { | |
1338 | memset(&sip[i], 0, sizeof(sip[i])); | |
1339 | ||
1340 | sip[i][0].tuple.src.l3num = AF_INET; | |
1341 | sip[i][1].tuple.src.l3num = AF_INET6; | |
1342 | for (j = 0; j < 2; j++) { | |
1343 | sip[i][j].tuple.dst.protonum = IPPROTO_UDP; | |
1344 | sip[i][j].tuple.src.u.udp.port = htons(ports[i]); | |
0f32a40f PM |
1345 | sip[i][j].expect_policy = sip_exp_policy; |
1346 | sip[i][j].expect_class_max = SIP_EXPECT_MAX; | |
9fafcd7b PM |
1347 | sip[i][j].me = THIS_MODULE; |
1348 | sip[i][j].help = sip_help; | |
1349 | ||
1350 | tmpname = &sip_names[i][j][0]; | |
1351 | if (ports[i] == SIP_PORT) | |
1352 | sprintf(tmpname, "sip"); | |
1353 | else | |
1354 | sprintf(tmpname, "sip-%u", i); | |
1355 | sip[i][j].name = tmpname; | |
1356 | ||
0d53778e | 1357 | pr_debug("port #%u: %u\n", i, ports[i]); |
9fafcd7b PM |
1358 | |
1359 | ret = nf_conntrack_helper_register(&sip[i][j]); | |
1360 | if (ret) { | |
1361 | printk("nf_ct_sip: failed to register helper " | |
1362 | "for pf: %u port: %u\n", | |
1363 | sip[i][j].tuple.src.l3num, ports[i]); | |
1364 | nf_conntrack_sip_fini(); | |
1365 | return ret; | |
1366 | } | |
1367 | } | |
1368 | } | |
1369 | return 0; | |
1370 | } | |
1371 | ||
1372 | module_init(nf_conntrack_sip_init); | |
1373 | module_exit(nf_conntrack_sip_fini); |