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