blob: cbc91598acee4759d12d71bb718f35df6077c74a [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.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/ctype.h>
13#include <linux/skbuff.h>
14#include <linux/inet.h>
15#include <linux/in.h>
16#include <linux/udp.h>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080017#include <linux/netfilter.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080018
19#include <net/netfilter/nf_conntrack.h>
20#include <net/netfilter/nf_conntrack_expect.h>
21#include <net/netfilter/nf_conntrack_helper.h>
22#include <linux/netfilter/nf_conntrack_sip.h>
23
Patrick McHardy9fafcd72006-12-02 22:09:57 -080024MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
26MODULE_DESCRIPTION("SIP connection tracking helper");
27MODULE_ALIAS("ip_conntrack_sip");
28
29#define MAX_PORTS 8
30static unsigned short ports[MAX_PORTS];
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -080031static unsigned int ports_c;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080032module_param_array(ports, ushort, &ports_c, 0400);
33MODULE_PARM_DESC(ports, "port numbers of SIP servers");
34
35static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
36module_param(sip_timeout, uint, 0600);
37MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
38
Herbert Xu3db05fe2007-10-15 00:53:15 -070039unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb,
Patrick McHardy2a6cfb22008-03-25 20:16:54 -070040 const char **dptr,
41 unsigned int *datalen) __read_mostly;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080042EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
43
Herbert Xu3db05fe2007-10-15 00:53:15 -070044unsigned int (*nf_nat_sdp_hook)(struct sk_buff *skb,
Patrick McHardy2a6cfb22008-03-25 20:16:54 -070045 const char **dptr,
Patrick McHardy212440a2008-03-25 20:17:13 -070046 unsigned int *datalen,
47 struct nf_conntrack_expect *exp) __read_mostly;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080048EXPORT_SYMBOL_GPL(nf_nat_sdp_hook);
49
Patrick McHardyac367742008-03-25 20:18:40 -070050static int string_len(const struct nf_conn *ct, const char *dptr,
51 const char *limit, int *shift)
52{
53 int len = 0;
54
55 while (dptr < limit && isalpha(*dptr)) {
56 dptr++;
57 len++;
58 }
59 return len;
60}
61
Jan Engelhardt13f7d632008-01-31 04:50:25 -080062static int digits_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -080063 const char *limit, int *shift)
64{
65 int len = 0;
Patrick McHardyb1ec4882008-03-25 20:10:11 -070066 while (dptr < limit && isdigit(*dptr)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -080067 dptr++;
68 len++;
69 }
70 return len;
71}
72
Jan Engelhardt13f7d632008-01-31 04:50:25 -080073static int parse_addr(const struct nf_conn *ct, const char *cp,
74 const char **endp, union nf_inet_addr *addr,
75 const char *limit)
Patrick McHardy9fafcd72006-12-02 22:09:57 -080076{
77 const char *end;
78 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
79 int ret = 0;
80
81 switch (family) {
82 case AF_INET:
83 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
84 break;
85 case AF_INET6:
86 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
87 break;
88 default:
89 BUG();
90 }
91
92 if (ret == 0 || end == cp)
93 return 0;
94 if (endp)
95 *endp = end;
96 return 1;
97}
98
99/* skip ip address. returns its length. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800100static int epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800101 const char *limit, int *shift)
102{
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800103 union nf_inet_addr addr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800104 const char *aux = dptr;
105
106 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700107 pr_debug("ip: %s parse failed.!\n", dptr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800108 return 0;
109 }
110
111 /* Port number */
112 if (*dptr == ':') {
113 dptr++;
114 dptr += digits_len(ct, dptr, limit, shift);
115 }
116 return dptr - aux;
117}
118
119/* get address length, skiping user info. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800120static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800121 const char *limit, int *shift)
122{
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700123 const char *start = dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800124 int s = *shift;
125
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800126 /* Search for @, but stop at the end of the line.
127 * We are inside a sip: URI, so we don't need to worry about
128 * continuation lines. */
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700129 while (dptr < limit &&
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800130 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800131 (*shift)++;
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800132 dptr++;
133 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800134
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700135 if (dptr < limit && *dptr == '@') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800136 dptr++;
137 (*shift)++;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700138 } else {
139 dptr = start;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800140 *shift = s;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700141 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800142
143 return epaddr_len(ct, dptr, limit, shift);
144}
145
Patrick McHardyac367742008-03-25 20:18:40 -0700146/* Parse a SIP request line of the form:
147 *
148 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
149 *
150 * and return the offset and length of the address contained in the Request-URI.
151 */
152int ct_sip_parse_request(const struct nf_conn *ct,
153 const char *dptr, unsigned int datalen,
154 unsigned int *matchoff, unsigned int *matchlen)
155{
156 const char *start = dptr, *limit = dptr + datalen;
157 unsigned int mlen;
158 int shift = 0;
159
160 /* Skip method and following whitespace */
161 mlen = string_len(ct, dptr, limit, NULL);
162 if (!mlen)
163 return 0;
164 dptr += mlen;
165 if (++dptr >= limit)
166 return 0;
167
168 /* Find SIP URI */
169 limit -= strlen("sip:");
170 for (; dptr < limit; dptr++) {
171 if (*dptr == '\r' || *dptr == '\n')
172 return -1;
173 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0)
174 break;
175 }
176 *matchlen = skp_epaddr_len(ct, dptr, limit, &shift);
177 if (!*matchlen)
178 return 0;
179 *matchoff = dptr - start + shift;
180 return 1;
181}
182EXPORT_SYMBOL_GPL(ct_sip_parse_request);
183
Patrick McHardyea45f122008-03-25 20:18:57 -0700184/* SIP header parsing: SIP headers are located at the beginning of a line, but
185 * may span several lines, in which case the continuation lines begin with a
186 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
187 * CRLF, RFC 3261 allows only CRLF, we support both.
188 *
189 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
190 * whitespace and the values. Whitespace in this context means any amount of
191 * tabs, spaces and continuation lines, which are treated as a single whitespace
192 * character.
193 */
194static const struct sip_header ct_sip_hdrs[] = {
195 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
196 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
197 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
198 [SIP_HDR_VIA] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
199 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
200};
201
202static const char *sip_follow_continuation(const char *dptr, const char *limit)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800203{
Patrick McHardyea45f122008-03-25 20:18:57 -0700204 /* Walk past newline */
205 if (++dptr >= limit)
206 return NULL;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800207
Patrick McHardyea45f122008-03-25 20:18:57 -0700208 /* Skip '\n' in CR LF */
209 if (*(dptr - 1) == '\r' && *dptr == '\n') {
210 if (++dptr >= limit)
211 return NULL;
212 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800213
Patrick McHardyea45f122008-03-25 20:18:57 -0700214 /* Continuation line? */
215 if (*dptr != ' ' && *dptr != '\t')
216 return NULL;
217
218 /* skip leading whitespace */
219 for (; dptr < limit; dptr++) {
220 if (*dptr != ' ' && *dptr != '\t')
221 break;
222 }
223 return dptr;
224}
225
226static const char *sip_skip_whitespace(const char *dptr, const char *limit)
227{
228 for (; dptr < limit; dptr++) {
229 if (*dptr == ' ')
230 continue;
231 if (*dptr != '\r' && *dptr != '\n')
232 break;
233 dptr = sip_follow_continuation(dptr, limit);
234 if (dptr == NULL)
235 return NULL;
236 }
237 return dptr;
238}
239
240/* Search within a SIP header value, dealing with continuation lines */
241static const char *ct_sip_header_search(const char *dptr, const char *limit,
242 const char *needle, unsigned int len)
243{
244 for (limit -= len; dptr < limit; dptr++) {
245 if (*dptr == '\r' || *dptr == '\n') {
246 dptr = sip_follow_continuation(dptr, limit);
247 if (dptr == NULL)
248 break;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800249 continue;
250 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800251
Patrick McHardyea45f122008-03-25 20:18:57 -0700252 if (strnicmp(dptr, needle, len) == 0)
253 return dptr;
254 }
255 return NULL;
256}
257
258int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
259 unsigned int dataoff, unsigned int datalen,
260 enum sip_header_types type,
261 unsigned int *matchoff, unsigned int *matchlen)
262{
263 const struct sip_header *hdr = &ct_sip_hdrs[type];
264 const char *start = dptr, *limit = dptr + datalen;
265 int shift = 0;
266
267 for (dptr += dataoff; dptr < limit; dptr++) {
268 /* Find beginning of line */
269 if (*dptr != '\r' && *dptr != '\n')
270 continue;
271 if (++dptr >= limit)
272 break;
273 if (*(dptr - 1) == '\r' && *dptr == '\n') {
274 if (++dptr >= limit)
275 break;
276 }
277
278 /* Skip continuation lines */
279 if (*dptr == ' ' || *dptr == '\t')
280 continue;
281
282 /* Find header. Compact headers must be followed by a
283 * non-alphabetic character to avoid mismatches. */
284 if (limit - dptr >= hdr->len &&
285 strnicmp(dptr, hdr->name, hdr->len) == 0)
286 dptr += hdr->len;
287 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
288 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
289 !isalpha(*(dptr + hdr->clen + 1)))
290 dptr += hdr->clen;
291 else
292 continue;
293
294 /* Find and skip colon */
295 dptr = sip_skip_whitespace(dptr, limit);
296 if (dptr == NULL)
297 break;
298 if (*dptr != ':' || ++dptr >= limit)
299 break;
300
301 /* Skip whitespace after colon */
302 dptr = sip_skip_whitespace(dptr, limit);
303 if (dptr == NULL)
304 break;
305
306 *matchoff = dptr - start;
307 if (hdr->search) {
308 dptr = ct_sip_header_search(dptr, limit, hdr->search,
309 hdr->slen);
310 if (!dptr)
311 return -1;
312 dptr += hdr->slen;
313 }
314
315 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800316 if (!*matchlen)
317 return -1;
Patrick McHardyea45f122008-03-25 20:18:57 -0700318 *matchoff = dptr - start + shift;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800319 return 1;
320 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800321 return 0;
322}
Patrick McHardyea45f122008-03-25 20:18:57 -0700323EXPORT_SYMBOL_GPL(ct_sip_get_header);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800324
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700325/* SDP header parsing: a SDP session description contains an ordered set of
326 * headers, starting with a section containing general session parameters,
327 * optionally followed by multiple media descriptions.
328 *
329 * SDP headers always start at the beginning of a line. According to RFC 2327:
330 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
331 * be tolerant and also accept records terminated with a single newline
332 * character". We handle both cases.
333 */
334static const struct sip_header ct_sdp_hdrs[] = {
335 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
336 [SDP_HDR_OWNER_IP4] = SDP_HDR("o=", "IN IP4 ", epaddr_len),
337 [SDP_HDR_CONNECTION_IP4] = SDP_HDR("c=", "IN IP4 ", epaddr_len),
338 [SDP_HDR_OWNER_IP6] = SDP_HDR("o=", "IN IP6 ", epaddr_len),
339 [SDP_HDR_CONNECTION_IP6] = SDP_HDR("c=", "IN IP6 ", epaddr_len),
340 [SDP_HDR_MEDIA] = SDP_HDR("m=", "audio ", digits_len),
341};
342
343/* Linear string search within SDP header values */
344static const char *ct_sdp_header_search(const char *dptr, const char *limit,
345 const char *needle, unsigned int len)
346{
347 for (limit -= len; dptr < limit; dptr++) {
348 if (*dptr == '\r' || *dptr == '\n')
349 break;
350 if (strncmp(dptr, needle, len) == 0)
351 return dptr;
352 }
353 return NULL;
354}
355
356/* Locate a SDP header (optionally a substring within the header value),
357 * optionally stopping at the first occurence of the term header, parse
358 * it and return the offset and length of the data we're interested in.
359 */
360int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
361 unsigned int dataoff, unsigned int datalen,
362 enum sdp_header_types type,
363 enum sdp_header_types term,
364 unsigned int *matchoff, unsigned int *matchlen)
365{
366 const struct sip_header *hdr = &ct_sdp_hdrs[type];
367 const struct sip_header *thdr = &ct_sdp_hdrs[term];
368 const char *start = dptr, *limit = dptr + datalen;
369 int shift = 0;
370
371 for (dptr += dataoff; dptr < limit; dptr++) {
372 /* Find beginning of line */
373 if (*dptr != '\r' && *dptr != '\n')
374 continue;
375 if (++dptr >= limit)
376 break;
377 if (*(dptr - 1) == '\r' && *dptr == '\n') {
378 if (++dptr >= limit)
379 break;
380 }
381
382 if (term != SDP_HDR_UNSPEC &&
383 limit - dptr >= thdr->len &&
384 strnicmp(dptr, thdr->name, thdr->len) == 0)
385 break;
386 else if (limit - dptr >= hdr->len &&
387 strnicmp(dptr, hdr->name, hdr->len) == 0)
388 dptr += hdr->len;
389 else
390 continue;
391
392 *matchoff = dptr - start;
393 if (hdr->search) {
394 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
395 hdr->slen);
396 if (!dptr)
397 return -1;
398 dptr += hdr->slen;
399 }
400
401 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
402 if (!*matchlen)
403 return -1;
404 *matchoff = dptr - start + shift;
405 return 1;
406 }
407 return 0;
408}
409EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
410
Herbert Xu3db05fe2007-10-15 00:53:15 -0700411static int set_expected_rtp(struct sk_buff *skb,
Patrick McHardy212440a2008-03-25 20:17:13 -0700412 const char **dptr, unsigned int *datalen,
413 union nf_inet_addr *addr, __be16 port)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800414{
415 struct nf_conntrack_expect *exp;
Patrick McHardy212440a2008-03-25 20:17:13 -0700416 enum ip_conntrack_info ctinfo;
417 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800418 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
419 int family = ct->tuplehash[!dir].tuple.src.l3num;
420 int ret;
421 typeof(nf_nat_sdp_hook) nf_nat_sdp;
422
Patrick McHardy68236452007-07-07 22:30:49 -0700423 exp = nf_ct_expect_alloc(ct);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800424 if (exp == NULL)
425 return NF_DROP;
Patrick McHardy6002f262008-03-25 20:09:15 -0700426 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, family,
Patrick McHardy68236452007-07-07 22:30:49 -0700427 &ct->tuplehash[!dir].tuple.src.u3, addr,
428 IPPROTO_UDP, NULL, &port);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800429
430 nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook);
431 if (nf_nat_sdp && ct->status & IPS_NAT_MASK)
Patrick McHardy212440a2008-03-25 20:17:13 -0700432 ret = nf_nat_sdp(skb, dptr, datalen, exp);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800433 else {
Patrick McHardy68236452007-07-07 22:30:49 -0700434 if (nf_ct_expect_related(exp) != 0)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800435 ret = NF_DROP;
436 else
437 ret = NF_ACCEPT;
438 }
Patrick McHardy68236452007-07-07 22:30:49 -0700439 nf_ct_expect_put(exp);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800440
441 return ret;
442}
443
Herbert Xu3db05fe2007-10-15 00:53:15 -0700444static int sip_help(struct sk_buff *skb,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800445 unsigned int protoff,
446 struct nf_conn *ct,
447 enum ip_conntrack_info ctinfo)
448{
449 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800450 union nf_inet_addr addr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800451 unsigned int dataoff, datalen;
452 const char *dptr;
453 int ret = NF_ACCEPT;
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800454 unsigned int matchoff, matchlen;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800455 u_int16_t port;
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700456 enum sdp_header_types type;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800457 typeof(nf_nat_sip_hook) nf_nat_sip;
458
459 /* No Data ? */
460 dataoff = protoff + sizeof(struct udphdr);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700461 if (dataoff >= skb->len)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800462 return NF_ACCEPT;
463
Herbert Xu3db05fe2007-10-15 00:53:15 -0700464 nf_ct_refresh(ct, skb, sip_timeout * HZ);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800465
Herbert Xu3db05fe2007-10-15 00:53:15 -0700466 if (!skb_is_nonlinear(skb))
467 dptr = skb->data + dataoff;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800468 else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700469 pr_debug("Copy of skbuff not supported yet.\n");
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800470 goto out;
471 }
472
473 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
474 if (nf_nat_sip && ct->status & IPS_NAT_MASK) {
Patrick McHardy212440a2008-03-25 20:17:13 -0700475 if (!nf_nat_sip(skb, &dptr, &datalen)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800476 ret = NF_DROP;
477 goto out;
478 }
479 }
480
Herbert Xu3db05fe2007-10-15 00:53:15 -0700481 datalen = skb->len - dataoff;
Patrick McHardy779382e2008-03-25 20:17:36 -0700482 if (datalen < strlen("SIP/2.0 200"))
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800483 goto out;
484
485 /* RTP info only in some SDP pkts */
Patrick McHardy779382e2008-03-25 20:17:36 -0700486 if (strnicmp(dptr, "INVITE", strlen("INVITE")) != 0 &&
487 strnicmp(dptr, "UPDATE", strlen("UPDATE")) != 0 &&
488 strnicmp(dptr, "SIP/2.0 180", strlen("SIP/2.0 180")) != 0 &&
489 strnicmp(dptr, "SIP/2.0 183", strlen("SIP/2.0 183")) != 0 &&
490 strnicmp(dptr, "SIP/2.0 200", strlen("SIP/2.0 200")) != 0) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800491 goto out;
492 }
493 /* Get address and port from SDP packet. */
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700494 type = family == AF_INET ? SDP_HDR_CONNECTION_IP4 :
495 SDP_HDR_CONNECTION_IP6;
496 if (ct_sip_get_sdp_header(ct, dptr, 0, datalen, type, SDP_HDR_UNSPEC,
497 &matchoff, &matchlen) > 0) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800498
499 /* We'll drop only if there are parse problems. */
500 if (!parse_addr(ct, dptr + matchoff, NULL, &addr,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800501 dptr + datalen)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800502 ret = NF_DROP;
503 goto out;
504 }
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700505 if (ct_sip_get_sdp_header(ct, dptr, 0, datalen,
506 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
507 &matchoff, &matchlen) > 0) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800508
509 port = simple_strtoul(dptr + matchoff, NULL, 10);
510 if (port < 1024) {
511 ret = NF_DROP;
512 goto out;
513 }
Patrick McHardy212440a2008-03-25 20:17:13 -0700514 ret = set_expected_rtp(skb, &dptr, &datalen,
515 &addr, htons(port));
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800516 }
517 }
518out:
519 return ret;
520}
521
522static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
523static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
524
Patrick McHardy6002f262008-03-25 20:09:15 -0700525static const struct nf_conntrack_expect_policy sip_exp_policy = {
526 .max_expected = 2,
527 .timeout = 3 * 60,
528};
529
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800530static void nf_conntrack_sip_fini(void)
531{
532 int i, j;
533
534 for (i = 0; i < ports_c; i++) {
535 for (j = 0; j < 2; j++) {
536 if (sip[i][j].me == NULL)
537 continue;
538 nf_conntrack_helper_unregister(&sip[i][j]);
539 }
540 }
541}
542
543static int __init nf_conntrack_sip_init(void)
544{
545 int i, j, ret;
546 char *tmpname;
547
548 if (ports_c == 0)
549 ports[ports_c++] = SIP_PORT;
550
551 for (i = 0; i < ports_c; i++) {
552 memset(&sip[i], 0, sizeof(sip[i]));
553
554 sip[i][0].tuple.src.l3num = AF_INET;
555 sip[i][1].tuple.src.l3num = AF_INET6;
556 for (j = 0; j < 2; j++) {
557 sip[i][j].tuple.dst.protonum = IPPROTO_UDP;
558 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
Patrick McHardy6002f262008-03-25 20:09:15 -0700559 sip[i][j].expect_policy = &sip_exp_policy;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800560 sip[i][j].me = THIS_MODULE;
561 sip[i][j].help = sip_help;
562
563 tmpname = &sip_names[i][j][0];
564 if (ports[i] == SIP_PORT)
565 sprintf(tmpname, "sip");
566 else
567 sprintf(tmpname, "sip-%u", i);
568 sip[i][j].name = tmpname;
569
Patrick McHardy0d537782007-07-07 22:39:38 -0700570 pr_debug("port #%u: %u\n", i, ports[i]);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800571
572 ret = nf_conntrack_helper_register(&sip[i][j]);
573 if (ret) {
574 printk("nf_ct_sip: failed to register helper "
575 "for pf: %u port: %u\n",
576 sip[i][j].tuple.src.l3num, ports[i]);
577 nf_conntrack_sip_fini();
578 return ret;
579 }
580 }
581 }
582 return 0;
583}
584
585module_init(nf_conntrack_sip_init);
586module_exit(nf_conntrack_sip_fini);