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