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