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