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