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)
156 const char *start = dptr, *limit = dptr + datalen;
160 /* Skip method and following whitespace */
161 mlen = string_len(ct, dptr, limit, NULL);
169 limit -= strlen("sip:");
170 for (; dptr < limit; dptr++) {
171 if (*dptr == '\r' || *dptr == '\n')
173 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0)
176 *matchlen = skp_epaddr_len(ct, dptr, limit, &shift);
179 *matchoff = dptr - start + shift;
182 EXPORT_SYMBOL_GPL(ct_sip_parse_request);
184 /* 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.
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
194 static 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),
202 static const char *sip_follow_continuation(const char *dptr, const char *limit)
204 /* Walk past newline */
208 /* Skip '\n' in CR LF */
209 if (*(dptr - 1) == '\r' && *dptr == '\n') {
214 /* Continuation line? */
215 if (*dptr != ' ' && *dptr != '\t')
218 /* skip leading whitespace */
219 for (; dptr < limit; dptr++) {
220 if (*dptr != ' ' && *dptr != '\t')
226 static const char *sip_skip_whitespace(const char *dptr, const char *limit)
228 for (; dptr < limit; dptr++) {
231 if (*dptr != '\r' && *dptr != '\n')
233 dptr = sip_follow_continuation(dptr, limit);
240 /* Search within a SIP header value, dealing with continuation lines */
241 static const char *ct_sip_header_search(const char *dptr, const char *limit,
242 const char *needle, unsigned int len)
244 for (limit -= len; dptr < limit; dptr++) {
245 if (*dptr == '\r' || *dptr == '\n') {
246 dptr = sip_follow_continuation(dptr, limit);
252 if (strnicmp(dptr, needle, len) == 0)
258 int 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)
263 const struct sip_header *hdr = &ct_sip_hdrs[type];
264 const char *start = dptr, *limit = dptr + datalen;
267 for (dptr += dataoff; dptr < limit; dptr++) {
268 /* Find beginning of line */
269 if (*dptr != '\r' && *dptr != '\n')
273 if (*(dptr - 1) == '\r' && *dptr == '\n') {
278 /* Skip continuation lines */
279 if (*dptr == ' ' || *dptr == '\t')
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)
287 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
288 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
289 !isalpha(*(dptr + hdr->clen + 1)))
294 /* Find and skip colon */
295 dptr = sip_skip_whitespace(dptr, limit);
298 if (*dptr != ':' || ++dptr >= limit)
301 /* Skip whitespace after colon */
302 dptr = sip_skip_whitespace(dptr, limit);
306 *matchoff = dptr - start;
308 dptr = ct_sip_header_search(dptr, limit, hdr->search,
315 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
318 *matchoff = dptr - start + shift;
323 EXPORT_SYMBOL_GPL(ct_sip_get_header);
325 /* 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.
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.
334 static 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),
343 /* Linear string search within SDP header values */
344 static const char *ct_sdp_header_search(const char *dptr, const char *limit,
345 const char *needle, unsigned int len)
347 for (limit -= len; dptr < limit; dptr++) {
348 if (*dptr == '\r' || *dptr == '\n')
350 if (strncmp(dptr, needle, len) == 0)
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.
360 int 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)
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;
371 for (dptr += dataoff; dptr < limit; dptr++) {
372 /* Find beginning of line */
373 if (*dptr != '\r' && *dptr != '\n')
377 if (*(dptr - 1) == '\r' && *dptr == '\n') {
382 if (term != SDP_HDR_UNSPEC &&
383 limit - dptr >= thdr->len &&
384 strnicmp(dptr, thdr->name, thdr->len) == 0)
386 else if (limit - dptr >= hdr->len &&
387 strnicmp(dptr, hdr->name, hdr->len) == 0)
392 *matchoff = dptr - start;
394 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
401 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
404 *matchoff = dptr - start + shift;
409 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
411 static int set_expected_rtp(struct sk_buff *skb,
412 const char **dptr, unsigned int *datalen,
413 union nf_inet_addr *addr, __be16 port)
415 struct nf_conntrack_expect *exp;
416 enum ip_conntrack_info ctinfo;
417 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
418 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
419 int family = ct->tuplehash[!dir].tuple.src.l3num;
421 typeof(nf_nat_sdp_hook) nf_nat_sdp;
423 exp = nf_ct_expect_alloc(ct);
426 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, family,
427 &ct->tuplehash[!dir].tuple.src.u3, addr,
428 IPPROTO_UDP, NULL, &port);
430 nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook);
431 if (nf_nat_sdp && ct->status & IPS_NAT_MASK)
432 ret = nf_nat_sdp(skb, dptr, datalen, exp);
434 if (nf_ct_expect_related(exp) != 0)
439 nf_ct_expect_put(exp);
444 static int sip_help(struct sk_buff *skb,
445 unsigned int protoff,
447 enum ip_conntrack_info ctinfo)
449 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
450 union nf_inet_addr addr;
451 unsigned int dataoff, datalen;
454 unsigned int matchoff, matchlen;
456 enum sdp_header_types type;
457 typeof(nf_nat_sip_hook) nf_nat_sip;
460 dataoff = protoff + sizeof(struct udphdr);
461 if (dataoff >= skb->len)
464 nf_ct_refresh(ct, skb, sip_timeout * HZ);
466 if (!skb_is_nonlinear(skb))
467 dptr = skb->data + dataoff;
469 pr_debug("Copy of skbuff not supported yet.\n");
473 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
474 if (nf_nat_sip && ct->status & IPS_NAT_MASK) {
475 if (!nf_nat_sip(skb, &dptr, &datalen)) {
481 datalen = skb->len - dataoff;
482 if (datalen < strlen("SIP/2.0 200"))
485 /* RTP info only in some SDP pkts */
486 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) {
493 /* Get address and port from SDP packet. */
494 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) {
499 /* We'll drop only if there are parse problems. */
500 if (!parse_addr(ct, dptr + matchoff, NULL, &addr,
505 if (ct_sip_get_sdp_header(ct, dptr, 0, datalen,
506 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
507 &matchoff, &matchlen) > 0) {
509 port = simple_strtoul(dptr + matchoff, NULL, 10);
514 ret = set_expected_rtp(skb, &dptr, &datalen,
522 static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
523 static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
525 static const struct nf_conntrack_expect_policy sip_exp_policy = {
530 static void nf_conntrack_sip_fini(void)
534 for (i = 0; i < ports_c; i++) {
535 for (j = 0; j < 2; j++) {
536 if (sip[i][j].me == NULL)
538 nf_conntrack_helper_unregister(&sip[i][j]);
543 static int __init nf_conntrack_sip_init(void)
549 ports[ports_c++] = SIP_PORT;
551 for (i = 0; i < ports_c; i++) {
552 memset(&sip[i], 0, sizeof(sip[i]));
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]);
559 sip[i][j].expect_policy = &sip_exp_policy;
560 sip[i][j].me = THIS_MODULE;
561 sip[i][j].help = sip_help;
563 tmpname = &sip_names[i][j][0];
564 if (ports[i] == SIP_PORT)
565 sprintf(tmpname, "sip");
567 sprintf(tmpname, "sip-%u", i);
568 sip[i][j].name = tmpname;
570 pr_debug("port #%u: %u\n", i, ports[i]);
572 ret = nf_conntrack_helper_register(&sip[i][j]);
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();
585 module_init(nf_conntrack_sip_init);
586 module_exit(nf_conntrack_sip_fini);