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