1 /* SIP extension for IP connection tracking.
3 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
4 * based on RR's ip_conntrack_ftp.c and other modules.
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.
11 #include <linux/module.h>
12 #include <linux/ctype.h>
13 #include <linux/skbuff.h>
14 #include <linux/inet.h>
16 #include <linux/udp.h>
17 #include <linux/netfilter.h>
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>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
26 MODULE_DESCRIPTION("SIP connection tracking helper");
27 MODULE_ALIAS("ip_conntrack_sip");
30 static unsigned short ports[MAX_PORTS];
31 static unsigned int ports_c;
32 module_param_array(ports, ushort, &ports_c, 0400);
33 MODULE_PARM_DESC(ports, "port numbers of SIP servers");
35 static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
36 module_param(sip_timeout, uint, 0600);
37 MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
39 unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb,
41 unsigned int *datalen) __read_mostly;
42 EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
44 unsigned int (*nf_nat_sdp_hook)(struct sk_buff *skb,
46 unsigned int *datalen,
47 struct nf_conntrack_expect *exp) __read_mostly;
48 EXPORT_SYMBOL_GPL(nf_nat_sdp_hook);
50 static int string_len(const struct nf_conn *ct, const char *dptr,
51 const char *limit, int *shift)
55 while (dptr < limit && isalpha(*dptr)) {
62 static int digits_len(const struct nf_conn *ct, const char *dptr,
63 const char *limit, int *shift)
66 while (dptr < limit && isdigit(*dptr)) {
73 static int parse_addr(const struct nf_conn *ct, const char *cp,
74 const char **endp, union nf_inet_addr *addr,
78 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
83 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
86 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
92 if (ret == 0 || end == cp)
99 /* skip ip address. returns its length. */
100 static int epaddr_len(const struct nf_conn *ct, const char *dptr,
101 const char *limit, int *shift)
103 union nf_inet_addr addr;
104 const char *aux = dptr;
106 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
107 pr_debug("ip: %s parse failed.!\n", dptr);
114 dptr += digits_len(ct, dptr, limit, shift);
119 /* get address length, skiping user info. */
120 static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
121 const char *limit, int *shift)
123 const char *start = dptr;
126 /* 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. */
129 while (dptr < limit &&
130 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
135 if (dptr < limit && *dptr == '@') {
143 return epaddr_len(ct, dptr, limit, shift);
146 /* Parse a SIP request line of the form:
148 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
150 * and return the offset and length of the address contained in the Request-URI.
152 int ct_sip_parse_request(const struct nf_conn *ct,
153 const char *dptr, unsigned int datalen,
154 unsigned int *matchoff, unsigned int *matchlen,
155 union nf_inet_addr *addr, __be16 *port)
157 const char *start = dptr, *limit = dptr + datalen, *end;
162 /* Skip method and following whitespace */
163 mlen = string_len(ct, dptr, limit, NULL);
171 limit -= strlen("sip:");
172 for (; dptr < limit; dptr++) {
173 if (*dptr == '\r' || *dptr == '\n')
175 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0)
178 if (!skp_epaddr_len(ct, dptr, limit, &shift))
182 if (!parse_addr(ct, dptr, &end, addr, limit))
184 if (end < limit && *end == ':') {
186 p = simple_strtoul(end, (char **)&end, 10);
187 if (p < 1024 || p > 65535)
191 *port = htons(SIP_PORT);
195 *matchoff = dptr - start;
196 *matchlen = end - dptr;
199 EXPORT_SYMBOL_GPL(ct_sip_parse_request);
201 /* SIP header parsing: SIP headers are located at the beginning of a line, but
202 * may span several lines, in which case the continuation lines begin with a
203 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
204 * CRLF, RFC 3261 allows only CRLF, we support both.
206 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
207 * whitespace and the values. Whitespace in this context means any amount of
208 * tabs, spaces and continuation lines, which are treated as a single whitespace
211 * Some headers may appear multiple times. A comma seperated list of values is
212 * equivalent to multiple headers.
214 static const struct sip_header ct_sip_hdrs[] = {
215 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
216 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
217 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
218 [SIP_HDR_VIA] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
219 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
222 static const char *sip_follow_continuation(const char *dptr, const char *limit)
224 /* Walk past newline */
228 /* Skip '\n' in CR LF */
229 if (*(dptr - 1) == '\r' && *dptr == '\n') {
234 /* Continuation line? */
235 if (*dptr != ' ' && *dptr != '\t')
238 /* skip leading whitespace */
239 for (; dptr < limit; dptr++) {
240 if (*dptr != ' ' && *dptr != '\t')
246 static const char *sip_skip_whitespace(const char *dptr, const char *limit)
248 for (; dptr < limit; dptr++) {
251 if (*dptr != '\r' && *dptr != '\n')
253 dptr = sip_follow_continuation(dptr, limit);
260 /* Search within a SIP header value, dealing with continuation lines */
261 static const char *ct_sip_header_search(const char *dptr, const char *limit,
262 const char *needle, unsigned int len)
264 for (limit -= len; dptr < limit; dptr++) {
265 if (*dptr == '\r' || *dptr == '\n') {
266 dptr = sip_follow_continuation(dptr, limit);
272 if (strnicmp(dptr, needle, len) == 0)
278 int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
279 unsigned int dataoff, unsigned int datalen,
280 enum sip_header_types type,
281 unsigned int *matchoff, unsigned int *matchlen)
283 const struct sip_header *hdr = &ct_sip_hdrs[type];
284 const char *start = dptr, *limit = dptr + datalen;
287 for (dptr += dataoff; dptr < limit; dptr++) {
288 /* Find beginning of line */
289 if (*dptr != '\r' && *dptr != '\n')
293 if (*(dptr - 1) == '\r' && *dptr == '\n') {
298 /* Skip continuation lines */
299 if (*dptr == ' ' || *dptr == '\t')
302 /* Find header. Compact headers must be followed by a
303 * non-alphabetic character to avoid mismatches. */
304 if (limit - dptr >= hdr->len &&
305 strnicmp(dptr, hdr->name, hdr->len) == 0)
307 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
308 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
309 !isalpha(*(dptr + hdr->clen + 1)))
314 /* Find and skip colon */
315 dptr = sip_skip_whitespace(dptr, limit);
318 if (*dptr != ':' || ++dptr >= limit)
321 /* Skip whitespace after colon */
322 dptr = sip_skip_whitespace(dptr, limit);
326 *matchoff = dptr - start;
328 dptr = ct_sip_header_search(dptr, limit, hdr->search,
335 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
338 *matchoff = dptr - start + shift;
343 EXPORT_SYMBOL_GPL(ct_sip_get_header);
345 /* Get next header field in a list of comma seperated values */
346 static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
347 unsigned int dataoff, unsigned int datalen,
348 enum sip_header_types type,
349 unsigned int *matchoff, unsigned int *matchlen)
351 const struct sip_header *hdr = &ct_sip_hdrs[type];
352 const char *start = dptr, *limit = dptr + datalen;
357 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
361 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
366 *matchoff = dptr - start;
367 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
374 /* Walk through headers until a parsable one is found or no header of the
375 * given type is left. */
376 static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
377 unsigned int dataoff, unsigned int datalen,
378 enum sip_header_types type, int *in_header,
379 unsigned int *matchoff, unsigned int *matchlen)
383 if (in_header && *in_header) {
385 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
386 type, matchoff, matchlen);
391 dataoff += *matchoff;
397 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
398 type, matchoff, matchlen);
403 dataoff += *matchoff;
411 /* Locate a SIP header, parse the URI and return the offset and length of
412 * the address as well as the address and port themselves. A stream of
413 * headers can be parsed by handing in a non-NULL datalen and in_header
416 int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
417 unsigned int *dataoff, unsigned int datalen,
418 enum sip_header_types type, int *in_header,
419 unsigned int *matchoff, unsigned int *matchlen,
420 union nf_inet_addr *addr, __be16 *port)
422 const char *c, *limit = dptr + datalen;
426 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
427 type, in_header, matchoff, matchlen);
432 if (!parse_addr(ct, dptr + *matchoff, &c, addr, limit))
436 p = simple_strtoul(c, (char **)&c, 10);
437 if (p < 1024 || p > 65535)
441 *port = htons(SIP_PORT);
447 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
449 /* SDP header parsing: a SDP session description contains an ordered set of
450 * headers, starting with a section containing general session parameters,
451 * optionally followed by multiple media descriptions.
453 * SDP headers always start at the beginning of a line. According to RFC 2327:
454 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
455 * be tolerant and also accept records terminated with a single newline
456 * character". We handle both cases.
458 static const struct sip_header ct_sdp_hdrs[] = {
459 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
460 [SDP_HDR_OWNER_IP4] = SDP_HDR("o=", "IN IP4 ", epaddr_len),
461 [SDP_HDR_CONNECTION_IP4] = SDP_HDR("c=", "IN IP4 ", epaddr_len),
462 [SDP_HDR_OWNER_IP6] = SDP_HDR("o=", "IN IP6 ", epaddr_len),
463 [SDP_HDR_CONNECTION_IP6] = SDP_HDR("c=", "IN IP6 ", epaddr_len),
464 [SDP_HDR_MEDIA] = SDP_HDR("m=", "audio ", digits_len),
467 /* Linear string search within SDP header values */
468 static const char *ct_sdp_header_search(const char *dptr, const char *limit,
469 const char *needle, unsigned int len)
471 for (limit -= len; dptr < limit; dptr++) {
472 if (*dptr == '\r' || *dptr == '\n')
474 if (strncmp(dptr, needle, len) == 0)
480 /* Locate a SDP header (optionally a substring within the header value),
481 * optionally stopping at the first occurence of the term header, parse
482 * it and return the offset and length of the data we're interested in.
484 int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
485 unsigned int dataoff, unsigned int datalen,
486 enum sdp_header_types type,
487 enum sdp_header_types term,
488 unsigned int *matchoff, unsigned int *matchlen)
490 const struct sip_header *hdr = &ct_sdp_hdrs[type];
491 const struct sip_header *thdr = &ct_sdp_hdrs[term];
492 const char *start = dptr, *limit = dptr + datalen;
495 for (dptr += dataoff; dptr < limit; dptr++) {
496 /* Find beginning of line */
497 if (*dptr != '\r' && *dptr != '\n')
501 if (*(dptr - 1) == '\r' && *dptr == '\n') {
506 if (term != SDP_HDR_UNSPEC &&
507 limit - dptr >= thdr->len &&
508 strnicmp(dptr, thdr->name, thdr->len) == 0)
510 else if (limit - dptr >= hdr->len &&
511 strnicmp(dptr, hdr->name, hdr->len) == 0)
516 *matchoff = dptr - start;
518 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
525 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
528 *matchoff = dptr - start + shift;
533 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
535 static int set_expected_rtp(struct sk_buff *skb,
536 const char **dptr, unsigned int *datalen,
537 union nf_inet_addr *addr, __be16 port)
539 struct nf_conntrack_expect *exp;
540 enum ip_conntrack_info ctinfo;
541 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
542 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
543 int family = ct->tuplehash[!dir].tuple.src.l3num;
545 typeof(nf_nat_sdp_hook) nf_nat_sdp;
547 exp = nf_ct_expect_alloc(ct);
550 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, family,
551 &ct->tuplehash[!dir].tuple.src.u3, addr,
552 IPPROTO_UDP, NULL, &port);
554 nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook);
555 if (nf_nat_sdp && ct->status & IPS_NAT_MASK)
556 ret = nf_nat_sdp(skb, dptr, datalen, exp);
558 if (nf_ct_expect_related(exp) != 0)
563 nf_ct_expect_put(exp);
568 static int process_sdp(struct sk_buff *skb,
569 const char **dptr, unsigned int *datalen)
571 enum ip_conntrack_info ctinfo;
572 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
573 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
574 unsigned int matchoff, matchlen;
575 union nf_inet_addr addr;
577 enum sdp_header_types type;
579 /* Get address and port from SDP packet. */
580 type = family == AF_INET ? SDP_HDR_CONNECTION_IP4 :
581 SDP_HDR_CONNECTION_IP6;
583 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
584 type, SDP_HDR_UNSPEC,
585 &matchoff, &matchlen) <= 0)
588 /* We'll drop only if there are parse problems. */
589 if (!parse_addr(ct, *dptr + matchoff, NULL, &addr, *dptr + *datalen))
592 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
593 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
594 &matchoff, &matchlen) <= 0)
597 port = simple_strtoul(*dptr + matchoff, NULL, 10);
598 if (port < 1024 || port > 65535)
601 return set_expected_rtp(skb, dptr, datalen, &addr, htons(port));
604 static int sip_help(struct sk_buff *skb,
605 unsigned int protoff,
607 enum ip_conntrack_info ctinfo)
609 unsigned int dataoff, datalen;
611 typeof(nf_nat_sip_hook) nf_nat_sip;
614 dataoff = protoff + sizeof(struct udphdr);
615 if (dataoff >= skb->len)
618 nf_ct_refresh(ct, skb, sip_timeout * HZ);
620 if (!skb_is_nonlinear(skb))
621 dptr = skb->data + dataoff;
623 pr_debug("Copy of skbuff not supported yet.\n");
627 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
628 if (nf_nat_sip && ct->status & IPS_NAT_MASK) {
629 if (!nf_nat_sip(skb, &dptr, &datalen))
633 datalen = skb->len - dataoff;
634 if (datalen < strlen("SIP/2.0 200"))
637 /* RTP info only in some SDP pkts */
638 if (strnicmp(dptr, "INVITE", strlen("INVITE")) != 0 &&
639 strnicmp(dptr, "UPDATE", strlen("UPDATE")) != 0 &&
640 strnicmp(dptr, "SIP/2.0 180", strlen("SIP/2.0 180")) != 0 &&
641 strnicmp(dptr, "SIP/2.0 183", strlen("SIP/2.0 183")) != 0 &&
642 strnicmp(dptr, "SIP/2.0 200", strlen("SIP/2.0 200")) != 0)
645 return process_sdp(skb, &dptr, &datalen);
648 static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
649 static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
651 static const struct nf_conntrack_expect_policy sip_exp_policy = {
656 static void nf_conntrack_sip_fini(void)
660 for (i = 0; i < ports_c; i++) {
661 for (j = 0; j < 2; j++) {
662 if (sip[i][j].me == NULL)
664 nf_conntrack_helper_unregister(&sip[i][j]);
669 static int __init nf_conntrack_sip_init(void)
675 ports[ports_c++] = SIP_PORT;
677 for (i = 0; i < ports_c; i++) {
678 memset(&sip[i], 0, sizeof(sip[i]));
680 sip[i][0].tuple.src.l3num = AF_INET;
681 sip[i][1].tuple.src.l3num = AF_INET6;
682 for (j = 0; j < 2; j++) {
683 sip[i][j].tuple.dst.protonum = IPPROTO_UDP;
684 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
685 sip[i][j].expect_policy = &sip_exp_policy;
686 sip[i][j].me = THIS_MODULE;
687 sip[i][j].help = sip_help;
689 tmpname = &sip_names[i][j][0];
690 if (ports[i] == SIP_PORT)
691 sprintf(tmpname, "sip");
693 sprintf(tmpname, "sip-%u", i);
694 sip[i][j].name = tmpname;
696 pr_debug("port #%u: %u\n", i, ports[i]);
698 ret = nf_conntrack_helper_register(&sip[i][j]);
700 printk("nf_ct_sip: failed to register helper "
701 "for pf: %u port: %u\n",
702 sip[i][j].tuple.src.l3num, ports[i]);
703 nf_conntrack_sip_fini();
711 module_init(nf_conntrack_sip_init);
712 module_exit(nf_conntrack_sip_fini);