blob: 0e7d423324c399b3c5012dc312fd75f470b78cb5 [file] [log] [blame]
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001/* 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 McHardyf49e1aa2008-03-25 20:27:05 -07005 * (C) 2007 United Security Providers
6 * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
Patrick McHardy9fafcd72006-12-02 22:09:57 -08007 *
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 McHardyf5b321b2010-02-11 12:26:19 +010019#include <linux/tcp.h>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080020#include <linux/netfilter.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080021
22#include <net/netfilter/nf_conntrack.h>
Patrick McHardy9467ee32008-03-25 20:24:04 -070023#include <net/netfilter/nf_conntrack_core.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080024#include <net/netfilter/nf_conntrack_expect.h>
25#include <net/netfilter/nf_conntrack_helper.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010026#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080027#include <linux/netfilter/nf_conntrack_sip.h>
28
Patrick McHardy9fafcd72006-12-02 22:09:57 -080029MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
31MODULE_DESCRIPTION("SIP connection tracking helper");
32MODULE_ALIAS("ip_conntrack_sip");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010033MODULE_ALIAS_NFCT_HELPER("sip");
Patrick McHardy9fafcd72006-12-02 22:09:57 -080034
35#define MAX_PORTS 8
36static unsigned short ports[MAX_PORTS];
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -080037static unsigned int ports_c;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080038module_param_array(ports, ushort, &ports_c, 0400);
39MODULE_PARM_DESC(ports, "port numbers of SIP servers");
40
41static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
42module_param(sip_timeout, uint, 0600);
43MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
44
Patrick McHardy0f32a402008-03-25 20:25:13 -070045static int sip_direct_signalling __read_mostly = 1;
46module_param(sip_direct_signalling, int, 0600);
47MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
48 "only (default 1)");
49
Patrick McHardyd901a932008-03-25 20:25:32 -070050static int sip_direct_media __read_mostly = 1;
51module_param(sip_direct_media, int, 0600);
52MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
53 "endpoints only (default 1)");
54
Patrick McHardy051966c2012-08-26 19:14:04 +020055unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, unsigned int protoff,
56 unsigned int dataoff, const char **dptr,
Patrick McHardy2a6cfb22008-03-25 20:16:54 -070057 unsigned int *datalen) __read_mostly;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080058EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
59
Patrick McHardy9a664822012-08-26 19:14:25 +020060void (*nf_nat_sip_seq_adjust_hook)(struct sk_buff *skb, unsigned int protoff,
61 s16 off) __read_mostly;
Patrick McHardy48f8ac22010-02-11 12:29:38 +010062EXPORT_SYMBOL_GPL(nf_nat_sip_seq_adjust_hook);
63
Patrick McHardy0f32a402008-03-25 20:25:13 -070064unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb,
Patrick McHardy051966c2012-08-26 19:14:04 +020065 unsigned int protoff,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010066 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -070067 const char **dptr,
68 unsigned int *datalen,
69 struct nf_conntrack_expect *exp,
70 unsigned int matchoff,
71 unsigned int matchlen) __read_mostly;
72EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook);
73
Patrick McHardy051966c2012-08-26 19:14:04 +020074unsigned int (*nf_nat_sdp_addr_hook)(struct sk_buff *skb, unsigned int protoff,
75 unsigned int dataoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -070076 const char **dptr,
Patrick McHardy4ab9e642008-03-25 20:26:08 -070077 unsigned int *datalen,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010078 unsigned int sdpoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -070079 enum sdp_header_types type,
80 enum sdp_header_types term,
81 const union nf_inet_addr *addr)
82 __read_mostly;
83EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook);
84
Patrick McHardy051966c2012-08-26 19:14:04 +020085unsigned int (*nf_nat_sdp_port_hook)(struct sk_buff *skb, unsigned int protoff,
86 unsigned int dataoff,
Patrick McHardyc7f485a2008-03-25 20:26:43 -070087 const char **dptr,
88 unsigned int *datalen,
89 unsigned int matchoff,
90 unsigned int matchlen,
91 u_int16_t port) __read_mostly;
92EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook);
93
Patrick McHardy4ab9e642008-03-25 20:26:08 -070094unsigned int (*nf_nat_sdp_session_hook)(struct sk_buff *skb,
Patrick McHardy051966c2012-08-26 19:14:04 +020095 unsigned int protoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -070096 unsigned int dataoff,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010097 const char **dptr,
Patrick McHardy4ab9e642008-03-25 20:26:08 -070098 unsigned int *datalen,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010099 unsigned int sdpoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700100 const union nf_inet_addr *addr)
101 __read_mostly;
102EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook);
103
Patrick McHardy051966c2012-08-26 19:14:04 +0200104unsigned int (*nf_nat_sdp_media_hook)(struct sk_buff *skb, unsigned int protoff,
105 unsigned int dataoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700106 const char **dptr,
107 unsigned int *datalen,
108 struct nf_conntrack_expect *rtp_exp,
109 struct nf_conntrack_expect *rtcp_exp,
110 unsigned int mediaoff,
111 unsigned int medialen,
112 union nf_inet_addr *rtp_addr)
113 __read_mostly;
114EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800115
Patrick McHardyac367742008-03-25 20:18:40 -0700116static int string_len(const struct nf_conn *ct, const char *dptr,
117 const char *limit, int *shift)
118{
119 int len = 0;
120
121 while (dptr < limit && isalpha(*dptr)) {
122 dptr++;
123 len++;
124 }
125 return len;
126}
127
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800128static int digits_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800129 const char *limit, int *shift)
130{
131 int len = 0;
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700132 while (dptr < limit && isdigit(*dptr)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800133 dptr++;
134 len++;
135 }
136 return len;
137}
138
Simon Horman001985b2010-08-22 21:37:51 +0900139static int iswordc(const char c)
140{
141 if (isalnum(c) || c == '!' || c == '"' || c == '%' ||
142 (c >= '(' && c <= '/') || c == ':' || c == '<' || c == '>' ||
143 c == '?' || (c >= '[' && c <= ']') || c == '_' || c == '`' ||
144 c == '{' || c == '}' || c == '~')
145 return 1;
146 return 0;
147}
148
149static int word_len(const char *dptr, const char *limit)
150{
151 int len = 0;
152 while (dptr < limit && iswordc(*dptr)) {
153 dptr++;
154 len++;
155 }
156 return len;
157}
158
159static int callid_len(const struct nf_conn *ct, const char *dptr,
160 const char *limit, int *shift)
161{
162 int len, domain_len;
163
164 len = word_len(dptr, limit);
165 dptr += len;
166 if (!len || dptr == limit || *dptr != '@')
167 return len;
168 dptr++;
169 len++;
170
171 domain_len = word_len(dptr, limit);
172 if (!domain_len)
173 return 0;
174 return len + domain_len;
175}
176
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700177/* get media type + port length */
178static int media_len(const struct nf_conn *ct, const char *dptr,
179 const char *limit, int *shift)
180{
181 int len = string_len(ct, dptr, limit, shift);
182
183 dptr += len;
184 if (dptr >= limit || *dptr != ' ')
185 return 0;
186 len++;
187 dptr++;
188
189 return len + digits_len(ct, dptr, limit, shift);
190}
191
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000192static int sip_parse_addr(const struct nf_conn *ct, const char *cp,
193 const char **endp, union nf_inet_addr *addr,
194 const char *limit, bool delim)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800195{
196 const char *end;
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000197 int ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800198
Simon Horman5adbb9f2010-08-22 21:37:51 +0900199 if (!ct)
200 return 0;
201
Patrick McHardyfa913dd2008-04-14 11:15:45 +0200202 memset(addr, 0, sizeof(*addr));
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200203 switch (nf_ct_l3num(ct)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800204 case AF_INET:
205 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000206 if (ret == 0)
207 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800208 break;
209 case AF_INET6:
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000210 if (cp < limit && *cp == '[')
211 cp++;
212 else if (delim)
213 return 0;
214
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800215 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000216 if (ret == 0)
217 return 0;
218
219 if (end < limit && *end == ']')
220 end++;
221 else if (delim)
222 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800223 break;
224 default:
225 BUG();
226 }
227
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800228 if (endp)
229 *endp = end;
230 return 1;
231}
232
233/* skip ip address. returns its length. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800234static int epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800235 const char *limit, int *shift)
236{
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800237 union nf_inet_addr addr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800238 const char *aux = dptr;
239
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000240 if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit, true)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700241 pr_debug("ip: %s parse failed.!\n", dptr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800242 return 0;
243 }
244
245 /* Port number */
246 if (*dptr == ':') {
247 dptr++;
248 dptr += digits_len(ct, dptr, limit, shift);
249 }
250 return dptr - aux;
251}
252
253/* get address length, skiping user info. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800254static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800255 const char *limit, int *shift)
256{
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700257 const char *start = dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800258 int s = *shift;
259
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800260 /* Search for @, but stop at the end of the line.
261 * We are inside a sip: URI, so we don't need to worry about
262 * continuation lines. */
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700263 while (dptr < limit &&
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800264 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800265 (*shift)++;
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800266 dptr++;
267 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800268
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700269 if (dptr < limit && *dptr == '@') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800270 dptr++;
271 (*shift)++;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700272 } else {
273 dptr = start;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800274 *shift = s;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700275 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800276
277 return epaddr_len(ct, dptr, limit, shift);
278}
279
Patrick McHardyac367742008-03-25 20:18:40 -0700280/* Parse a SIP request line of the form:
281 *
282 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
283 *
284 * and return the offset and length of the address contained in the Request-URI.
285 */
286int ct_sip_parse_request(const struct nf_conn *ct,
287 const char *dptr, unsigned int datalen,
Patrick McHardy624f8b72008-03-25 20:19:30 -0700288 unsigned int *matchoff, unsigned int *matchlen,
289 union nf_inet_addr *addr, __be16 *port)
Patrick McHardyac367742008-03-25 20:18:40 -0700290{
Patrick McHardy624f8b72008-03-25 20:19:30 -0700291 const char *start = dptr, *limit = dptr + datalen, *end;
Patrick McHardyac367742008-03-25 20:18:40 -0700292 unsigned int mlen;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700293 unsigned int p;
Patrick McHardyac367742008-03-25 20:18:40 -0700294 int shift = 0;
295
296 /* Skip method and following whitespace */
297 mlen = string_len(ct, dptr, limit, NULL);
298 if (!mlen)
299 return 0;
300 dptr += mlen;
301 if (++dptr >= limit)
302 return 0;
303
304 /* Find SIP URI */
Patrick McHardy54101f42010-02-11 12:23:12 +0100305 for (; dptr < limit - strlen("sip:"); dptr++) {
Patrick McHardyac367742008-03-25 20:18:40 -0700306 if (*dptr == '\r' || *dptr == '\n')
307 return -1;
Patrick McHardy54101f42010-02-11 12:23:12 +0100308 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0) {
309 dptr += strlen("sip:");
Patrick McHardyac367742008-03-25 20:18:40 -0700310 break;
Patrick McHardy54101f42010-02-11 12:23:12 +0100311 }
Patrick McHardyac367742008-03-25 20:18:40 -0700312 }
Patrick McHardy624f8b72008-03-25 20:19:30 -0700313 if (!skp_epaddr_len(ct, dptr, limit, &shift))
Patrick McHardyac367742008-03-25 20:18:40 -0700314 return 0;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700315 dptr += shift;
316
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000317 if (!sip_parse_addr(ct, dptr, &end, addr, limit, true))
Patrick McHardy624f8b72008-03-25 20:19:30 -0700318 return -1;
319 if (end < limit && *end == ':') {
320 end++;
321 p = simple_strtoul(end, (char **)&end, 10);
322 if (p < 1024 || p > 65535)
323 return -1;
324 *port = htons(p);
325 } else
326 *port = htons(SIP_PORT);
327
328 if (end == dptr)
329 return 0;
330 *matchoff = dptr - start;
331 *matchlen = end - dptr;
Patrick McHardyac367742008-03-25 20:18:40 -0700332 return 1;
333}
334EXPORT_SYMBOL_GPL(ct_sip_parse_request);
335
Patrick McHardyea45f122008-03-25 20:18:57 -0700336/* SIP header parsing: SIP headers are located at the beginning of a line, but
337 * may span several lines, in which case the continuation lines begin with a
338 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
339 * CRLF, RFC 3261 allows only CRLF, we support both.
340 *
341 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
342 * whitespace and the values. Whitespace in this context means any amount of
343 * tabs, spaces and continuation lines, which are treated as a single whitespace
344 * character.
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700345 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800346 * Some headers may appear multiple times. A comma separated list of values is
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700347 * equivalent to multiple headers.
Patrick McHardyea45f122008-03-25 20:18:57 -0700348 */
349static const struct sip_header ct_sip_hdrs[] = {
Patrick McHardy30f33e62008-03-25 20:22:20 -0700350 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700351 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
352 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
353 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100354 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
355 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
Patrick McHardy0f32a402008-03-25 20:25:13 -0700356 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700357 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
Simon Horman001985b2010-08-22 21:37:51 +0900358 [SIP_HDR_CALL_ID] = SIP_HDR("Call-Id", "i", NULL, callid_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700359};
360
361static const char *sip_follow_continuation(const char *dptr, const char *limit)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800362{
Patrick McHardyea45f122008-03-25 20:18:57 -0700363 /* Walk past newline */
364 if (++dptr >= limit)
365 return NULL;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800366
Patrick McHardyea45f122008-03-25 20:18:57 -0700367 /* Skip '\n' in CR LF */
368 if (*(dptr - 1) == '\r' && *dptr == '\n') {
369 if (++dptr >= limit)
370 return NULL;
371 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800372
Patrick McHardyea45f122008-03-25 20:18:57 -0700373 /* Continuation line? */
374 if (*dptr != ' ' && *dptr != '\t')
375 return NULL;
376
377 /* skip leading whitespace */
378 for (; dptr < limit; dptr++) {
379 if (*dptr != ' ' && *dptr != '\t')
380 break;
381 }
382 return dptr;
383}
384
385static const char *sip_skip_whitespace(const char *dptr, const char *limit)
386{
387 for (; dptr < limit; dptr++) {
388 if (*dptr == ' ')
389 continue;
390 if (*dptr != '\r' && *dptr != '\n')
391 break;
392 dptr = sip_follow_continuation(dptr, limit);
393 if (dptr == NULL)
394 return NULL;
395 }
396 return dptr;
397}
398
399/* Search within a SIP header value, dealing with continuation lines */
400static const char *ct_sip_header_search(const char *dptr, const char *limit,
401 const char *needle, unsigned int len)
402{
403 for (limit -= len; dptr < limit; dptr++) {
404 if (*dptr == '\r' || *dptr == '\n') {
405 dptr = sip_follow_continuation(dptr, limit);
406 if (dptr == NULL)
407 break;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800408 continue;
409 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800410
Patrick McHardyea45f122008-03-25 20:18:57 -0700411 if (strnicmp(dptr, needle, len) == 0)
412 return dptr;
413 }
414 return NULL;
415}
416
417int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
418 unsigned int dataoff, unsigned int datalen,
419 enum sip_header_types type,
420 unsigned int *matchoff, unsigned int *matchlen)
421{
422 const struct sip_header *hdr = &ct_sip_hdrs[type];
423 const char *start = dptr, *limit = dptr + datalen;
424 int shift = 0;
425
426 for (dptr += dataoff; dptr < limit; dptr++) {
427 /* Find beginning of line */
428 if (*dptr != '\r' && *dptr != '\n')
429 continue;
430 if (++dptr >= limit)
431 break;
432 if (*(dptr - 1) == '\r' && *dptr == '\n') {
433 if (++dptr >= limit)
434 break;
435 }
436
437 /* Skip continuation lines */
438 if (*dptr == ' ' || *dptr == '\t')
439 continue;
440
441 /* Find header. Compact headers must be followed by a
442 * non-alphabetic character to avoid mismatches. */
443 if (limit - dptr >= hdr->len &&
444 strnicmp(dptr, hdr->name, hdr->len) == 0)
445 dptr += hdr->len;
446 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
447 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
Patrick McHardy135d0182010-01-19 19:06:59 +0100448 !isalpha(*(dptr + hdr->clen)))
Patrick McHardyea45f122008-03-25 20:18:57 -0700449 dptr += hdr->clen;
450 else
451 continue;
452
453 /* Find and skip colon */
454 dptr = sip_skip_whitespace(dptr, limit);
455 if (dptr == NULL)
456 break;
457 if (*dptr != ':' || ++dptr >= limit)
458 break;
459
460 /* Skip whitespace after colon */
461 dptr = sip_skip_whitespace(dptr, limit);
462 if (dptr == NULL)
463 break;
464
465 *matchoff = dptr - start;
466 if (hdr->search) {
467 dptr = ct_sip_header_search(dptr, limit, hdr->search,
468 hdr->slen);
469 if (!dptr)
470 return -1;
471 dptr += hdr->slen;
472 }
473
474 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800475 if (!*matchlen)
476 return -1;
Patrick McHardyea45f122008-03-25 20:18:57 -0700477 *matchoff = dptr - start + shift;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800478 return 1;
479 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800480 return 0;
481}
Patrick McHardyea45f122008-03-25 20:18:57 -0700482EXPORT_SYMBOL_GPL(ct_sip_get_header);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800483
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800484/* Get next header field in a list of comma separated values */
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700485static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
486 unsigned int dataoff, unsigned int datalen,
487 enum sip_header_types type,
488 unsigned int *matchoff, unsigned int *matchlen)
489{
490 const struct sip_header *hdr = &ct_sip_hdrs[type];
491 const char *start = dptr, *limit = dptr + datalen;
492 int shift = 0;
493
494 dptr += dataoff;
495
496 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
497 if (!dptr)
498 return 0;
499
500 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
501 if (!dptr)
502 return 0;
503 dptr += hdr->slen;
504
505 *matchoff = dptr - start;
506 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
507 if (!*matchlen)
508 return -1;
509 *matchoff += shift;
510 return 1;
511}
512
513/* Walk through headers until a parsable one is found or no header of the
514 * given type is left. */
515static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
516 unsigned int dataoff, unsigned int datalen,
517 enum sip_header_types type, int *in_header,
518 unsigned int *matchoff, unsigned int *matchlen)
519{
520 int ret;
521
522 if (in_header && *in_header) {
523 while (1) {
524 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
525 type, matchoff, matchlen);
526 if (ret > 0)
527 return ret;
528 if (ret == 0)
529 break;
530 dataoff += *matchoff;
531 }
532 *in_header = 0;
533 }
534
535 while (1) {
536 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
537 type, matchoff, matchlen);
538 if (ret > 0)
539 break;
540 if (ret == 0)
541 return ret;
542 dataoff += *matchoff;
543 }
544
545 if (in_header)
546 *in_header = 1;
547 return 1;
548}
549
550/* Locate a SIP header, parse the URI and return the offset and length of
551 * the address as well as the address and port themselves. A stream of
552 * headers can be parsed by handing in a non-NULL datalen and in_header
553 * pointer.
554 */
555int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
556 unsigned int *dataoff, unsigned int datalen,
557 enum sip_header_types type, int *in_header,
558 unsigned int *matchoff, unsigned int *matchlen,
559 union nf_inet_addr *addr, __be16 *port)
560{
561 const char *c, *limit = dptr + datalen;
562 unsigned int p;
563 int ret;
564
565 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
566 type, in_header, matchoff, matchlen);
567 WARN_ON(ret < 0);
568 if (ret == 0)
569 return ret;
570
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000571 if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true))
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700572 return -1;
573 if (*c == ':') {
574 c++;
575 p = simple_strtoul(c, (char **)&c, 10);
576 if (p < 1024 || p > 65535)
577 return -1;
578 *port = htons(p);
579 } else
580 *port = htons(SIP_PORT);
581
582 if (dataoff)
583 *dataoff = c - dptr;
584 return 1;
585}
586EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
587
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100588static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
589 unsigned int dataoff, unsigned int datalen,
590 const char *name,
591 unsigned int *matchoff, unsigned int *matchlen)
592{
593 const char *limit = dptr + datalen;
594 const char *start;
595 const char *end;
596
597 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
598 if (!limit)
599 limit = dptr + datalen;
600
601 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
602 if (!start)
603 return 0;
604 start += strlen(name);
605
606 end = ct_sip_header_search(start, limit, ";", strlen(";"));
607 if (!end)
608 end = limit;
609
610 *matchoff = start - dptr;
611 *matchlen = end - start;
612 return 1;
613}
614
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700615/* Parse address from header parameter and return address, offset and length */
616int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
617 unsigned int dataoff, unsigned int datalen,
618 const char *name,
619 unsigned int *matchoff, unsigned int *matchlen,
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000620 union nf_inet_addr *addr, bool delim)
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700621{
622 const char *limit = dptr + datalen;
623 const char *start, *end;
624
625 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
626 if (!limit)
627 limit = dptr + datalen;
628
629 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
630 if (!start)
631 return 0;
632
633 start += strlen(name);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000634 if (!sip_parse_addr(ct, start, &end, addr, limit, delim))
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700635 return 0;
636 *matchoff = start - dptr;
637 *matchlen = end - start;
638 return 1;
639}
640EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
641
642/* Parse numerical header parameter and return value, offset and length */
643int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
644 unsigned int dataoff, unsigned int datalen,
645 const char *name,
646 unsigned int *matchoff, unsigned int *matchlen,
647 unsigned int *val)
648{
649 const char *limit = dptr + datalen;
650 const char *start;
651 char *end;
652
653 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
654 if (!limit)
655 limit = dptr + datalen;
656
657 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
658 if (!start)
659 return 0;
660
661 start += strlen(name);
662 *val = simple_strtoul(start, &end, 0);
663 if (start == end)
664 return 0;
665 if (matchoff && matchlen) {
666 *matchoff = start - dptr;
667 *matchlen = end - start;
668 }
669 return 1;
670}
671EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
672
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100673static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
674 unsigned int dataoff, unsigned int datalen,
675 u8 *proto)
676{
677 unsigned int matchoff, matchlen;
678
679 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
680 &matchoff, &matchlen)) {
681 if (!strnicmp(dptr + matchoff, "TCP", strlen("TCP")))
682 *proto = IPPROTO_TCP;
683 else if (!strnicmp(dptr + matchoff, "UDP", strlen("UDP")))
684 *proto = IPPROTO_UDP;
685 else
686 return 0;
687
688 if (*proto != nf_ct_protonum(ct))
689 return 0;
690 } else
691 *proto = nf_ct_protonum(ct);
692
693 return 1;
694}
695
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000696static int sdp_parse_addr(const struct nf_conn *ct, const char *cp,
697 const char **endp, union nf_inet_addr *addr,
698 const char *limit)
699{
700 const char *end;
701 int ret;
702
703 memset(addr, 0, sizeof(*addr));
704 switch (nf_ct_l3num(ct)) {
705 case AF_INET:
706 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
707 break;
708 case AF_INET6:
709 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
710 break;
711 default:
712 BUG();
713 }
714
715 if (ret == 0)
716 return 0;
717 if (endp)
718 *endp = end;
719 return 1;
720}
721
722/* skip ip address. returns its length. */
723static int sdp_addr_len(const struct nf_conn *ct, const char *dptr,
724 const char *limit, int *shift)
725{
726 union nf_inet_addr addr;
727 const char *aux = dptr;
728
729 if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) {
730 pr_debug("ip: %s parse failed.!\n", dptr);
731 return 0;
732 }
733
734 return dptr - aux;
735}
736
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700737/* SDP header parsing: a SDP session description contains an ordered set of
738 * headers, starting with a section containing general session parameters,
739 * optionally followed by multiple media descriptions.
740 *
741 * SDP headers always start at the beginning of a line. According to RFC 2327:
742 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
743 * be tolerant and also accept records terminated with a single newline
744 * character". We handle both cases.
745 */
Patrick McHardy9a664822012-08-26 19:14:25 +0200746static const struct sip_header ct_sdp_hdrs_v4[] = {
747 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
748 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len),
749 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len),
750 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
751};
752
753static const struct sip_header ct_sdp_hdrs_v6[] = {
754 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
755 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len),
756 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len),
757 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700758};
759
760/* Linear string search within SDP header values */
761static const char *ct_sdp_header_search(const char *dptr, const char *limit,
762 const char *needle, unsigned int len)
763{
764 for (limit -= len; dptr < limit; dptr++) {
765 if (*dptr == '\r' || *dptr == '\n')
766 break;
767 if (strncmp(dptr, needle, len) == 0)
768 return dptr;
769 }
770 return NULL;
771}
772
773/* Locate a SDP header (optionally a substring within the header value),
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300774 * optionally stopping at the first occurrence of the term header, parse
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700775 * it and return the offset and length of the data we're interested in.
776 */
777int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
778 unsigned int dataoff, unsigned int datalen,
779 enum sdp_header_types type,
780 enum sdp_header_types term,
781 unsigned int *matchoff, unsigned int *matchlen)
782{
Patrick McHardy9a664822012-08-26 19:14:25 +0200783 const struct sip_header *hdrs, *hdr, *thdr;
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700784 const char *start = dptr, *limit = dptr + datalen;
785 int shift = 0;
786
Patrick McHardy9a664822012-08-26 19:14:25 +0200787 hdrs = nf_ct_l3num(ct) == NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6;
788 hdr = &hdrs[type];
789 thdr = &hdrs[term];
790
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700791 for (dptr += dataoff; dptr < limit; dptr++) {
792 /* Find beginning of line */
793 if (*dptr != '\r' && *dptr != '\n')
794 continue;
795 if (++dptr >= limit)
796 break;
797 if (*(dptr - 1) == '\r' && *dptr == '\n') {
798 if (++dptr >= limit)
799 break;
800 }
801
802 if (term != SDP_HDR_UNSPEC &&
803 limit - dptr >= thdr->len &&
804 strnicmp(dptr, thdr->name, thdr->len) == 0)
805 break;
806 else if (limit - dptr >= hdr->len &&
807 strnicmp(dptr, hdr->name, hdr->len) == 0)
808 dptr += hdr->len;
809 else
810 continue;
811
812 *matchoff = dptr - start;
813 if (hdr->search) {
814 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
815 hdr->slen);
816 if (!dptr)
817 return -1;
818 dptr += hdr->slen;
819 }
820
821 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
822 if (!*matchlen)
823 return -1;
824 *matchoff = dptr - start + shift;
825 return 1;
826 }
827 return 0;
828}
829EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
830
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700831static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
832 unsigned int dataoff, unsigned int datalen,
833 enum sdp_header_types type,
834 enum sdp_header_types term,
835 unsigned int *matchoff, unsigned int *matchlen,
836 union nf_inet_addr *addr)
837{
838 int ret;
839
840 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
841 matchoff, matchlen);
842 if (ret <= 0)
843 return ret;
844
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000845 if (!sdp_parse_addr(ct, dptr + *matchoff, NULL, addr,
846 dptr + *matchoff + *matchlen))
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700847 return -1;
848 return 1;
849}
850
Patrick McHardy0f32a402008-03-25 20:25:13 -0700851static int refresh_signalling_expectation(struct nf_conn *ct,
852 union nf_inet_addr *addr,
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100853 u8 proto, __be16 port,
Patrick McHardy0f32a402008-03-25 20:25:13 -0700854 unsigned int expires)
855{
856 struct nf_conn_help *help = nfct_help(ct);
857 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800858 struct hlist_node *next;
Patrick McHardy0f32a402008-03-25 20:25:13 -0700859 int found = 0;
860
861 spin_lock_bh(&nf_conntrack_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800862 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -0700863 if (exp->class != SIP_EXPECT_SIGNALLING ||
864 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100865 exp->tuple.dst.protonum != proto ||
Patrick McHardy0f32a402008-03-25 20:25:13 -0700866 exp->tuple.dst.u.udp.port != port)
867 continue;
868 if (!del_timer(&exp->timeout))
869 continue;
870 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
871 exp->timeout.expires = jiffies + expires * HZ;
872 add_timer(&exp->timeout);
873 found = 1;
874 break;
875 }
876 spin_unlock_bh(&nf_conntrack_lock);
877 return found;
878}
879
880static void flush_expectations(struct nf_conn *ct, bool media)
Patrick McHardy9467ee32008-03-25 20:24:04 -0700881{
882 struct nf_conn_help *help = nfct_help(ct);
883 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800884 struct hlist_node *next;
Patrick McHardy9467ee32008-03-25 20:24:04 -0700885
886 spin_lock_bh(&nf_conntrack_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800887 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -0700888 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
889 continue;
Patrick McHardy9467ee32008-03-25 20:24:04 -0700890 if (!del_timer(&exp->timeout))
891 continue;
892 nf_ct_unlink_expect(exp);
893 nf_ct_expect_put(exp);
Patrick McHardy0f32a402008-03-25 20:25:13 -0700894 if (!media)
895 break;
Patrick McHardy9467ee32008-03-25 20:24:04 -0700896 }
897 spin_unlock_bh(&nf_conntrack_lock);
898}
899
Patrick McHardy051966c2012-08-26 19:14:04 +0200900static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
901 unsigned int dataoff,
Patrick McHardya9c1d352008-03-25 20:25:49 -0700902 const char **dptr, unsigned int *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700903 union nf_inet_addr *daddr, __be16 port,
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700904 enum sip_expectation_classes class,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700905 unsigned int mediaoff, unsigned int medialen)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800906{
Patrick McHardya9c1d352008-03-25 20:25:49 -0700907 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
Patrick McHardy212440a2008-03-25 20:17:13 -0700908 enum ip_conntrack_info ctinfo;
909 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyana5c3a802008-10-08 11:35:09 +0200910 struct net *net = nf_ct_net(ct);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800911 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardyd901a932008-03-25 20:25:32 -0700912 union nf_inet_addr *saddr;
913 struct nf_conntrack_tuple tuple;
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700914 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
Patrick McHardya9c1d352008-03-25 20:25:49 -0700915 u_int16_t base_port;
916 __be16 rtp_port, rtcp_port;
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700917 typeof(nf_nat_sdp_port_hook) nf_nat_sdp_port;
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700918 typeof(nf_nat_sdp_media_hook) nf_nat_sdp_media;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800919
Patrick McHardyd901a932008-03-25 20:25:32 -0700920 saddr = NULL;
921 if (sip_direct_media) {
922 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
923 return NF_ACCEPT;
924 saddr = &ct->tuplehash[!dir].tuple.src.u3;
925 }
926
927 /* We need to check whether the registration exists before attempting
928 * to register it since we can see the same media description multiple
929 * times on different connections in case multiple endpoints receive
930 * the same call.
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700931 *
932 * RTP optimization: if we find a matching media channel expectation
933 * and both the expectation and this connection are SNATed, we assume
934 * both sides can reach each other directly and use the final
935 * destination address from the expectation. We still need to keep
936 * the NATed expectations for media that might arrive from the
937 * outside, and additionally need to expect the direct RTP stream
938 * in case it passes through us even without NAT.
Patrick McHardyd901a932008-03-25 20:25:32 -0700939 */
940 memset(&tuple, 0, sizeof(tuple));
941 if (saddr)
942 tuple.src.u3 = *saddr;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200943 tuple.src.l3num = nf_ct_l3num(ct);
Patrick McHardyd901a932008-03-25 20:25:32 -0700944 tuple.dst.protonum = IPPROTO_UDP;
945 tuple.dst.u3 = *daddr;
946 tuple.dst.u.udp.port = port;
947
948 rcu_read_lock();
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700949 do {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100950 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
Patrick McHardyd901a932008-03-25 20:25:32 -0700951
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700952 if (!exp || exp->master == ct ||
953 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
954 exp->class != class)
955 break;
Patrick McHardye1f9a462008-04-19 17:53:52 -0700956#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardy9a664822012-08-26 19:14:25 +0200957 if (!direct_rtp &&
958 (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) ||
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700959 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
960 ct->status & IPS_NAT_MASK) {
Patrick McHardy9a664822012-08-26 19:14:25 +0200961 *daddr = exp->saved_addr;
962 tuple.dst.u3 = exp->saved_addr;
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700963 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
964 direct_rtp = 1;
965 } else
Patrick McHardye1f9a462008-04-19 17:53:52 -0700966#endif
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700967 skip_expect = 1;
968 } while (!skip_expect);
969 rcu_read_unlock();
Patrick McHardyd901a932008-03-25 20:25:32 -0700970
Patrick McHardya9c1d352008-03-25 20:25:49 -0700971 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
972 rtp_port = htons(base_port);
973 rtcp_port = htons(base_port + 1);
974
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700975 if (direct_rtp) {
976 nf_nat_sdp_port = rcu_dereference(nf_nat_sdp_port_hook);
977 if (nf_nat_sdp_port &&
Patrick McHardy051966c2012-08-26 19:14:04 +0200978 !nf_nat_sdp_port(skb, protoff, dataoff, dptr, datalen,
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700979 mediaoff, medialen, ntohs(rtp_port)))
980 goto err1;
981 }
982
983 if (skip_expect)
984 return NF_ACCEPT;
985
Patrick McHardya9c1d352008-03-25 20:25:49 -0700986 rtp_exp = nf_ct_expect_alloc(ct);
987 if (rtp_exp == NULL)
988 goto err1;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200989 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -0700990 IPPROTO_UDP, NULL, &rtp_port);
991
992 rtcp_exp = nf_ct_expect_alloc(ct);
993 if (rtcp_exp == NULL)
994 goto err2;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200995 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -0700996 IPPROTO_UDP, NULL, &rtcp_port);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800997
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700998 nf_nat_sdp_media = rcu_dereference(nf_nat_sdp_media_hook);
Patrick McHardy9a664822012-08-26 19:14:25 +0200999 if (nf_nat_sdp_media && ct->status & IPS_NAT_MASK && !direct_rtp)
Patrick McHardy051966c2012-08-26 19:14:04 +02001000 ret = nf_nat_sdp_media(skb, protoff, dataoff, dptr, datalen,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001001 rtp_exp, rtcp_exp,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001002 mediaoff, medialen, daddr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001003 else {
Patrick McHardya9c1d352008-03-25 20:25:49 -07001004 if (nf_ct_expect_related(rtp_exp) == 0) {
1005 if (nf_ct_expect_related(rtcp_exp) != 0)
1006 nf_ct_unexpect_related(rtp_exp);
1007 else
1008 ret = NF_ACCEPT;
1009 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001010 }
Patrick McHardya9c1d352008-03-25 20:25:49 -07001011 nf_ct_expect_put(rtcp_exp);
1012err2:
1013 nf_ct_expect_put(rtp_exp);
1014err1:
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001015 return ret;
1016}
1017
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001018static const struct sdp_media_type sdp_media_types[] = {
1019 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
1020 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
Patrick McHardy9d288df2010-02-11 12:30:21 +01001021 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE),
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001022};
1023
1024static const struct sdp_media_type *sdp_media_type(const char *dptr,
1025 unsigned int matchoff,
1026 unsigned int matchlen)
1027{
1028 const struct sdp_media_type *t;
1029 unsigned int i;
1030
1031 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
1032 t = &sdp_media_types[i];
1033 if (matchlen < t->len ||
1034 strncmp(dptr + matchoff, t->name, t->len))
1035 continue;
1036 return t;
1037 }
1038 return NULL;
1039}
1040
Patrick McHardy051966c2012-08-26 19:14:04 +02001041static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1042 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001043 const char **dptr, unsigned int *datalen,
1044 unsigned int cseq)
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001045{
1046 enum ip_conntrack_info ctinfo;
1047 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001048 unsigned int matchoff, matchlen;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001049 unsigned int mediaoff, medialen;
1050 unsigned int sdpoff;
1051 unsigned int caddr_len, maddr_len;
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001052 unsigned int i;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001053 union nf_inet_addr caddr, maddr, rtp_addr;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001054 unsigned int port;
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001055 const struct sdp_media_type *t;
1056 int ret = NF_ACCEPT;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001057 typeof(nf_nat_sdp_addr_hook) nf_nat_sdp_addr;
1058 typeof(nf_nat_sdp_session_hook) nf_nat_sdp_session;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001059
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001060 nf_nat_sdp_addr = rcu_dereference(nf_nat_sdp_addr_hook);
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001061
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001062 /* Find beginning of session description */
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001063 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001064 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001065 &matchoff, &matchlen) <= 0)
1066 return NF_ACCEPT;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001067 sdpoff = matchoff;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001068
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001069 /* The connection information is contained in the session description
1070 * and/or once per media description. The first media description marks
1071 * the end of the session description. */
1072 caddr_len = 0;
1073 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001074 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001075 &matchoff, &matchlen, &caddr) > 0)
1076 caddr_len = matchlen;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001077
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001078 mediaoff = sdpoff;
1079 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
1080 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
1081 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
1082 &mediaoff, &medialen) <= 0)
1083 break;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001084
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001085 /* Get media type and port number. A media port value of zero
1086 * indicates an inactive stream. */
1087 t = sdp_media_type(*dptr, mediaoff, medialen);
1088 if (!t) {
1089 mediaoff += medialen;
1090 continue;
1091 }
1092 mediaoff += t->len;
1093 medialen -= t->len;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001094
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001095 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
1096 if (port == 0)
1097 continue;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001098 if (port < 1024 || port > 65535) {
1099 nf_ct_helper_log(skb, ct, "wrong port %u", port);
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001100 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001101 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001102
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001103 /* The media description overrides the session description. */
1104 maddr_len = 0;
1105 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001106 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001107 &matchoff, &matchlen, &maddr) > 0) {
1108 maddr_len = matchlen;
1109 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
1110 } else if (caddr_len)
1111 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001112 else {
1113 nf_ct_helper_log(skb, ct, "cannot parse SDP message");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001114 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001115 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001116
Patrick McHardy051966c2012-08-26 19:14:04 +02001117 ret = set_expected_rtp_rtcp(skb, protoff, dataoff,
1118 dptr, datalen,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001119 &rtp_addr, htons(port), t->class,
1120 mediaoff, medialen);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001121 if (ret != NF_ACCEPT) {
1122 nf_ct_helper_log(skb, ct,
1123 "cannot add expectation for voice");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001124 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001125 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001126
1127 /* Update media connection address if present */
Patrick McHardy9a664822012-08-26 19:14:25 +02001128 if (maddr_len && nf_nat_sdp_addr && ct->status & IPS_NAT_MASK) {
Patrick McHardy051966c2012-08-26 19:14:04 +02001129 ret = nf_nat_sdp_addr(skb, protoff, dataoff,
Patrick McHardy9a664822012-08-26 19:14:25 +02001130 dptr, datalen, mediaoff,
1131 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001132 &rtp_addr);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001133 if (ret != NF_ACCEPT) {
1134 nf_ct_helper_log(skb, ct, "cannot mangle SDP");
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001135 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001136 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001137 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001138 i++;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001139 }
1140
1141 /* Update session connection and owner addresses */
1142 nf_nat_sdp_session = rcu_dereference(nf_nat_sdp_session_hook);
Patrick McHardy9a664822012-08-26 19:14:25 +02001143 if (nf_nat_sdp_session && ct->status & IPS_NAT_MASK)
Patrick McHardy051966c2012-08-26 19:14:04 +02001144 ret = nf_nat_sdp_session(skb, protoff, dataoff,
1145 dptr, datalen, sdpoff, &rtp_addr);
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001146
1147 return ret;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001148}
Patrick McHardy051966c2012-08-26 19:14:04 +02001149static int process_invite_response(struct sk_buff *skb, unsigned int protoff,
1150 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001151 const char **dptr, unsigned int *datalen,
1152 unsigned int cseq, unsigned int code)
1153{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001154 enum ip_conntrack_info ctinfo;
1155 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001156 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001157
Patrick McHardy30f33e62008-03-25 20:22:20 -07001158 if ((code >= 100 && code <= 199) ||
1159 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001160 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001161 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001162 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001163 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001164}
1165
Patrick McHardy051966c2012-08-26 19:14:04 +02001166static int process_update_response(struct sk_buff *skb, unsigned int protoff,
1167 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001168 const char **dptr, unsigned int *datalen,
1169 unsigned int cseq, unsigned int code)
1170{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001171 enum ip_conntrack_info ctinfo;
1172 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001173 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001174
Patrick McHardy30f33e62008-03-25 20:22:20 -07001175 if ((code >= 100 && code <= 199) ||
1176 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001177 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001178 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001179 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001180 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001181}
1182
Patrick McHardy051966c2012-08-26 19:14:04 +02001183static int process_prack_response(struct sk_buff *skb, unsigned int protoff,
1184 unsigned int dataoff,
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001185 const char **dptr, unsigned int *datalen,
1186 unsigned int cseq, unsigned int code)
1187{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001188 enum ip_conntrack_info ctinfo;
1189 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001190 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001191
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001192 if ((code >= 100 && code <= 199) ||
1193 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001194 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001195 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001196 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001197 return NF_ACCEPT;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001198}
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001199
Patrick McHardy051966c2012-08-26 19:14:04 +02001200static int process_invite_request(struct sk_buff *skb, unsigned int protoff,
1201 unsigned int dataoff,
Patrick McHardy9d288df2010-02-11 12:30:21 +01001202 const char **dptr, unsigned int *datalen,
1203 unsigned int cseq)
1204{
1205 enum ip_conntrack_info ctinfo;
1206 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001207 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001208 unsigned int ret;
1209
1210 flush_expectations(ct, true);
Patrick McHardy051966c2012-08-26 19:14:04 +02001211 ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001212 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001213 ct_sip_info->invite_cseq = cseq;
Patrick McHardy9d288df2010-02-11 12:30:21 +01001214 return ret;
1215}
1216
Patrick McHardy051966c2012-08-26 19:14:04 +02001217static int process_bye_request(struct sk_buff *skb, unsigned int protoff,
1218 unsigned int dataoff,
Patrick McHardy9467ee32008-03-25 20:24:04 -07001219 const char **dptr, unsigned int *datalen,
1220 unsigned int cseq)
1221{
1222 enum ip_conntrack_info ctinfo;
1223 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1224
Patrick McHardy0f32a402008-03-25 20:25:13 -07001225 flush_expectations(ct, true);
1226 return NF_ACCEPT;
1227}
1228
1229/* Parse a REGISTER request and create a permanent expectation for incoming
1230 * signalling connections. The expectation is marked inactive and is activated
1231 * when receiving a response indicating success from the registrar.
1232 */
Patrick McHardy051966c2012-08-26 19:14:04 +02001233static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1234 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001235 const char **dptr, unsigned int *datalen,
1236 unsigned int cseq)
1237{
1238 enum ip_conntrack_info ctinfo;
1239 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001240 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001241 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001242 unsigned int matchoff, matchlen;
1243 struct nf_conntrack_expect *exp;
1244 union nf_inet_addr *saddr, daddr;
1245 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001246 u8 proto;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001247 unsigned int expires = 0;
1248 int ret;
1249 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect;
1250
1251 /* Expected connections can not register again. */
1252 if (ct->status & IPS_EXPECTED)
1253 return NF_ACCEPT;
1254
1255 /* We must check the expiration time: a value of zero signals the
1256 * registrar to release the binding. We'll remove our expectation
1257 * when receiving the new bindings in the response, but we don't
1258 * want to create new ones.
1259 *
1260 * The expiration time may be contained in Expires: header, the
1261 * Contact: header parameters or the URI parameters.
1262 */
1263 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1264 &matchoff, &matchlen) > 0)
1265 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1266
1267 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1268 SIP_HDR_CONTACT, NULL,
1269 &matchoff, &matchlen, &daddr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001270 if (ret < 0) {
1271 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001272 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001273 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001274 return NF_ACCEPT;
1275
1276 /* We don't support third-party registrations */
1277 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1278 return NF_ACCEPT;
1279
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001280 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1281 &proto) == 0)
1282 return NF_ACCEPT;
1283
Patrick McHardy0f32a402008-03-25 20:25:13 -07001284 if (ct_sip_parse_numerical_param(ct, *dptr,
1285 matchoff + matchlen, *datalen,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001286 "expires=", NULL, NULL, &expires) < 0) {
1287 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001288 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001289 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001290
1291 if (expires == 0) {
1292 ret = NF_ACCEPT;
1293 goto store_cseq;
1294 }
1295
1296 exp = nf_ct_expect_alloc(ct);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001297 if (!exp) {
1298 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001299 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001300 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001301
1302 saddr = NULL;
1303 if (sip_direct_signalling)
1304 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1305
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001306 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001307 saddr, &daddr, proto, NULL, &port);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001308 exp->timeout.expires = sip_timeout * HZ;
1309 exp->helper = nfct_help(ct)->helper;
1310 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1311
1312 nf_nat_sip_expect = rcu_dereference(nf_nat_sip_expect_hook);
Patrick McHardy9a664822012-08-26 19:14:25 +02001313 if (nf_nat_sip_expect && ct->status & IPS_NAT_MASK)
Patrick McHardy051966c2012-08-26 19:14:04 +02001314 ret = nf_nat_sip_expect(skb, protoff, dataoff, dptr, datalen,
1315 exp, matchoff, matchlen);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001316 else {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001317 if (nf_ct_expect_related(exp) != 0) {
1318 nf_ct_helper_log(skb, ct, "cannot add expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001319 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001320 } else
Patrick McHardy0f32a402008-03-25 20:25:13 -07001321 ret = NF_ACCEPT;
1322 }
1323 nf_ct_expect_put(exp);
1324
1325store_cseq:
1326 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001327 ct_sip_info->register_cseq = cseq;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001328 return ret;
1329}
1330
Patrick McHardy051966c2012-08-26 19:14:04 +02001331static int process_register_response(struct sk_buff *skb, unsigned int protoff,
1332 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001333 const char **dptr, unsigned int *datalen,
1334 unsigned int cseq, unsigned int code)
1335{
1336 enum ip_conntrack_info ctinfo;
1337 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001338 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001339 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1340 union nf_inet_addr addr;
1341 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001342 u8 proto;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001343 unsigned int matchoff, matchlen, coff = 0;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001344 unsigned int expires = 0;
1345 int in_contact = 0, ret;
1346
1347 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1348 * they have received a final response from the registrar for the
1349 * previous one or the previous REGISTER request has timed out".
1350 *
1351 * However, some servers fail to detect retransmissions and send late
1352 * responses, so we store the sequence number of the last valid
1353 * request and compare it here.
1354 */
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001355 if (ct_sip_info->register_cseq != cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001356 return NF_ACCEPT;
1357
1358 if (code >= 100 && code <= 199)
1359 return NF_ACCEPT;
1360 if (code < 200 || code > 299)
1361 goto flush;
1362
1363 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1364 &matchoff, &matchlen) > 0)
1365 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1366
1367 while (1) {
1368 unsigned int c_expires = expires;
1369
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001370 ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001371 SIP_HDR_CONTACT, &in_contact,
1372 &matchoff, &matchlen,
1373 &addr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001374 if (ret < 0) {
1375 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001376 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001377 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001378 break;
1379
1380 /* We don't support third-party registrations */
1381 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1382 continue;
1383
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001384 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1385 *datalen, &proto) == 0)
1386 continue;
1387
Patrick McHardy0f32a402008-03-25 20:25:13 -07001388 ret = ct_sip_parse_numerical_param(ct, *dptr,
1389 matchoff + matchlen,
1390 *datalen, "expires=",
1391 NULL, NULL, &c_expires);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001392 if (ret < 0) {
1393 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001394 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001395 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001396 if (c_expires == 0)
1397 break;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001398 if (refresh_signalling_expectation(ct, &addr, proto, port,
1399 c_expires))
Patrick McHardy0f32a402008-03-25 20:25:13 -07001400 return NF_ACCEPT;
1401 }
1402
1403flush:
1404 flush_expectations(ct, false);
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001405 return NF_ACCEPT;
1406}
1407
Patrick McHardy30f33e62008-03-25 20:22:20 -07001408static const struct sip_handler sip_handlers[] = {
Patrick McHardy9d288df2010-02-11 12:30:21 +01001409 SIP_HANDLER("INVITE", process_invite_request, process_invite_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001410 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001411 SIP_HANDLER("ACK", process_sdp, NULL),
1412 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
Patrick McHardy9467ee32008-03-25 20:24:04 -07001413 SIP_HANDLER("BYE", process_bye_request, NULL),
Patrick McHardy0f32a402008-03-25 20:25:13 -07001414 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001415};
1416
Patrick McHardy051966c2012-08-26 19:14:04 +02001417static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
1418 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001419 const char **dptr, unsigned int *datalen)
1420{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001421 enum ip_conntrack_info ctinfo;
1422 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001423 unsigned int matchoff, matchlen, matchend;
1424 unsigned int code, cseq, i;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001425
1426 if (*datalen < strlen("SIP/2.0 200"))
1427 return NF_ACCEPT;
1428 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001429 if (!code) {
1430 nf_ct_helper_log(skb, ct, "cannot get code");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001431 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001432 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001433
1434 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001435 &matchoff, &matchlen) <= 0) {
1436 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001437 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001438 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001439 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001440 if (!cseq) {
1441 nf_ct_helper_log(skb, ct, "cannot get cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001442 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001443 }
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001444 matchend = matchoff + matchlen + 1;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001445
1446 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001447 const struct sip_handler *handler;
1448
Patrick McHardy30f33e62008-03-25 20:22:20 -07001449 handler = &sip_handlers[i];
1450 if (handler->response == NULL)
1451 continue;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001452 if (*datalen < matchend + handler->len ||
1453 strnicmp(*dptr + matchend, handler->method, handler->len))
Patrick McHardy30f33e62008-03-25 20:22:20 -07001454 continue;
Patrick McHardy051966c2012-08-26 19:14:04 +02001455 return handler->response(skb, protoff, dataoff, dptr, datalen,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001456 cseq, code);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001457 }
1458 return NF_ACCEPT;
1459}
1460
Patrick McHardy051966c2012-08-26 19:14:04 +02001461static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
1462 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001463 const char **dptr, unsigned int *datalen)
1464{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001465 enum ip_conntrack_info ctinfo;
1466 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Kevin Cernekee72665072012-12-17 18:33:58 +00001467 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1468 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001469 unsigned int matchoff, matchlen;
1470 unsigned int cseq, i;
Kevin Cernekee72665072012-12-17 18:33:58 +00001471 union nf_inet_addr addr;
1472 __be16 port;
1473
1474 /* Many Cisco IP phones use a high source port for SIP requests, but
1475 * listen for the response on port 5060. If we are the local
1476 * router for one of these phones, save the port number from the
1477 * Via: header so that nf_nat_sip can redirect the responses to
1478 * the correct port.
1479 */
1480 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1481 SIP_HDR_VIA_UDP, NULL, &matchoff,
1482 &matchlen, &addr, &port) > 0 &&
1483 port != ct->tuplehash[dir].tuple.src.u.udp.port &&
1484 nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3))
1485 ct_sip_info->forced_dport = port;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001486
1487 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001488 const struct sip_handler *handler;
1489
Patrick McHardy30f33e62008-03-25 20:22:20 -07001490 handler = &sip_handlers[i];
1491 if (handler->request == NULL)
1492 continue;
1493 if (*datalen < handler->len ||
1494 strnicmp(*dptr, handler->method, handler->len))
1495 continue;
1496
1497 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001498 &matchoff, &matchlen) <= 0) {
1499 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001500 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001501 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001502 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001503 if (!cseq) {
1504 nf_ct_helper_log(skb, ct, "cannot get cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001505 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001506 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001507
Patrick McHardy051966c2012-08-26 19:14:04 +02001508 return handler->request(skb, protoff, dataoff, dptr, datalen,
1509 cseq);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001510 }
1511 return NF_ACCEPT;
1512}
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001513
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001514static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
Patrick McHardy051966c2012-08-26 19:14:04 +02001515 unsigned int protoff, unsigned int dataoff,
1516 const char **dptr, unsigned int *datalen)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001517{
1518 typeof(nf_nat_sip_hook) nf_nat_sip;
1519 int ret;
1520
1521 if (strnicmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
Patrick McHardy051966c2012-08-26 19:14:04 +02001522 ret = process_sip_request(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001523 else
Patrick McHardy051966c2012-08-26 19:14:04 +02001524 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001525
Patrick McHardy9a664822012-08-26 19:14:25 +02001526 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001527 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
Patrick McHardy051966c2012-08-26 19:14:04 +02001528 if (nf_nat_sip && !nf_nat_sip(skb, protoff, dataoff,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001529 dptr, datalen)) {
1530 nf_ct_helper_log(skb, ct, "cannot NAT SIP message");
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001531 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001532 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001533 }
1534
1535 return ret;
1536}
1537
1538static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1539 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1540{
1541 struct tcphdr *th, _tcph;
1542 unsigned int dataoff, datalen;
1543 unsigned int matchoff, matchlen, clen;
1544 unsigned int msglen, origlen;
1545 const char *dptr, *end;
1546 s16 diff, tdiff = 0;
Simon Horman78748962010-09-21 21:17:30 +00001547 int ret = NF_ACCEPT;
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001548 bool term;
Patrick McHardy48f8ac22010-02-11 12:29:38 +01001549 typeof(nf_nat_sip_seq_adjust_hook) nf_nat_sip_seq_adjust;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001550
1551 if (ctinfo != IP_CT_ESTABLISHED &&
Eric Dumazetfb048832011-05-19 15:44:27 +02001552 ctinfo != IP_CT_ESTABLISHED_REPLY)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001553 return NF_ACCEPT;
1554
1555 /* No Data ? */
1556 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1557 if (th == NULL)
1558 return NF_ACCEPT;
1559 dataoff = protoff + th->doff * 4;
1560 if (dataoff >= skb->len)
1561 return NF_ACCEPT;
1562
1563 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1564
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02001565 if (unlikely(skb_linearize(skb)))
1566 return NF_DROP;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001567
1568 dptr = skb->data + dataoff;
1569 datalen = skb->len - dataoff;
1570 if (datalen < strlen("SIP/2.0 200"))
1571 return NF_ACCEPT;
1572
1573 while (1) {
1574 if (ct_sip_get_header(ct, dptr, 0, datalen,
1575 SIP_HDR_CONTENT_LENGTH,
1576 &matchoff, &matchlen) <= 0)
1577 break;
1578
1579 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1580 if (dptr + matchoff == end)
1581 break;
1582
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001583 term = false;
1584 for (; end + strlen("\r\n\r\n") <= dptr + datalen; end++) {
1585 if (end[0] == '\r' && end[1] == '\n' &&
1586 end[2] == '\r' && end[3] == '\n') {
1587 term = true;
1588 break;
1589 }
1590 }
1591 if (!term)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001592 break;
1593 end += strlen("\r\n\r\n") + clen;
1594
1595 msglen = origlen = end - dptr;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001596 if (msglen > datalen) {
1597 nf_ct_helper_log(skb, ct, "incomplete/bad SIP message");
Patrick McHardy274ea0e2011-05-16 14:42:26 +02001598 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001599 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001600
Patrick McHardy051966c2012-08-26 19:14:04 +02001601 ret = process_sip_msg(skb, ct, protoff, dataoff,
1602 &dptr, &msglen);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001603 /* process_sip_* functions report why this packet is dropped */
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001604 if (ret != NF_ACCEPT)
1605 break;
1606 diff = msglen - origlen;
1607 tdiff += diff;
1608
1609 dataoff += msglen;
1610 dptr += msglen;
1611 datalen = datalen + diff - msglen;
1612 }
1613
Patrick McHardy9a664822012-08-26 19:14:25 +02001614 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
Patrick McHardy48f8ac22010-02-11 12:29:38 +01001615 nf_nat_sip_seq_adjust = rcu_dereference(nf_nat_sip_seq_adjust_hook);
1616 if (nf_nat_sip_seq_adjust)
Patrick McHardy9a664822012-08-26 19:14:25 +02001617 nf_nat_sip_seq_adjust(skb, protoff, tdiff);
Patrick McHardy48f8ac22010-02-11 12:29:38 +01001618 }
1619
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001620 return ret;
1621}
1622
1623static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
1624 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001625{
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001626 unsigned int dataoff, datalen;
1627 const char *dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001628
1629 /* No Data ? */
1630 dataoff = protoff + sizeof(struct udphdr);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001631 if (dataoff >= skb->len)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001632 return NF_ACCEPT;
1633
Herbert Xu3db05fe2007-10-15 00:53:15 -07001634 nf_ct_refresh(ct, skb, sip_timeout * HZ);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001635
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02001636 if (unlikely(skb_linearize(skb)))
1637 return NF_DROP;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001638
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001639 dptr = skb->data + dataoff;
Herbert Xu3db05fe2007-10-15 00:53:15 -07001640 datalen = skb->len - dataoff;
Patrick McHardy779382e2008-03-25 20:17:36 -07001641 if (datalen < strlen("SIP/2.0 200"))
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001642 return NF_ACCEPT;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001643
Patrick McHardy051966c2012-08-26 19:14:04 +02001644 return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001645}
1646
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001647static struct nf_conntrack_helper sip[MAX_PORTS][4] __read_mostly;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001648
Patrick McHardy0f32a402008-03-25 20:25:13 -07001649static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1650 [SIP_EXPECT_SIGNALLING] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01001651 .name = "signalling",
Patrick McHardy0f32a402008-03-25 20:25:13 -07001652 .max_expected = 1,
1653 .timeout = 3 * 60,
1654 },
1655 [SIP_EXPECT_AUDIO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01001656 .name = "audio",
Patrick McHardya9c1d352008-03-25 20:25:49 -07001657 .max_expected = 2 * IP_CT_DIR_MAX,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001658 .timeout = 3 * 60,
1659 },
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001660 [SIP_EXPECT_VIDEO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01001661 .name = "video",
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001662 .max_expected = 2 * IP_CT_DIR_MAX,
1663 .timeout = 3 * 60,
1664 },
Patrick McHardy9d288df2010-02-11 12:30:21 +01001665 [SIP_EXPECT_IMAGE] = {
1666 .name = "image",
1667 .max_expected = IP_CT_DIR_MAX,
1668 .timeout = 3 * 60,
1669 },
Patrick McHardy6002f2662008-03-25 20:09:15 -07001670};
1671
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001672static void nf_conntrack_sip_fini(void)
1673{
1674 int i, j;
1675
1676 for (i = 0; i < ports_c; i++) {
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001677 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001678 if (sip[i][j].me == NULL)
1679 continue;
1680 nf_conntrack_helper_unregister(&sip[i][j]);
1681 }
1682 }
1683}
1684
1685static int __init nf_conntrack_sip_init(void)
1686{
1687 int i, j, ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001688
1689 if (ports_c == 0)
1690 ports[ports_c++] = SIP_PORT;
1691
1692 for (i = 0; i < ports_c; i++) {
1693 memset(&sip[i], 0, sizeof(sip[i]));
1694
1695 sip[i][0].tuple.src.l3num = AF_INET;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001696 sip[i][0].tuple.dst.protonum = IPPROTO_UDP;
1697 sip[i][0].help = sip_help_udp;
1698 sip[i][1].tuple.src.l3num = AF_INET;
1699 sip[i][1].tuple.dst.protonum = IPPROTO_TCP;
1700 sip[i][1].help = sip_help_tcp;
1701
1702 sip[i][2].tuple.src.l3num = AF_INET6;
1703 sip[i][2].tuple.dst.protonum = IPPROTO_UDP;
1704 sip[i][2].help = sip_help_udp;
1705 sip[i][3].tuple.src.l3num = AF_INET6;
1706 sip[i][3].tuple.dst.protonum = IPPROTO_TCP;
1707 sip[i][3].help = sip_help_tcp;
1708
1709 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001710 sip[i][j].data_len = sizeof(struct nf_ct_sip_master);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001711 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001712 sip[i][j].expect_policy = sip_exp_policy;
1713 sip[i][j].expect_class_max = SIP_EXPECT_MAX;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001714 sip[i][j].me = THIS_MODULE;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001715
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001716 if (ports[i] == SIP_PORT)
Patrick McHardye9324b22012-08-09 10:08:45 +00001717 sprintf(sip[i][j].name, "sip");
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001718 else
Patrick McHardye9324b22012-08-09 10:08:45 +00001719 sprintf(sip[i][j].name, "sip-%u", i);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001720
Patrick McHardy0d537782007-07-07 22:39:38 -07001721 pr_debug("port #%u: %u\n", i, ports[i]);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001722
1723 ret = nf_conntrack_helper_register(&sip[i][j]);
1724 if (ret) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +02001725 printk(KERN_ERR "nf_ct_sip: failed to register"
1726 " helper for pf: %u port: %u\n",
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001727 sip[i][j].tuple.src.l3num, ports[i]);
1728 nf_conntrack_sip_fini();
1729 return ret;
1730 }
1731 }
1732 }
1733 return 0;
1734}
1735
1736module_init(nf_conntrack_sip_init);
1737module_exit(nf_conntrack_sip_fini);