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>
18 #include <net/netfilter/nf_conntrack.h>
19 #include <net/netfilter/nf_conntrack_expect.h>
20 #include <net/netfilter/nf_conntrack_helper.h>
21 #include <linux/netfilter/nf_conntrack_sip.h>
26 #define DEBUGP(format, args...)
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
31 MODULE_DESCRIPTION("SIP connection tracking helper");
32 MODULE_ALIAS("ip_conntrack_sip");
35 static unsigned short ports[MAX_PORTS];
37 module_param_array(ports, ushort, &ports_c, 0400);
38 MODULE_PARM_DESC(ports, "port numbers of SIP servers");
40 static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
41 module_param(sip_timeout, uint, 0600);
42 MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
44 unsigned int (*nf_nat_sip_hook)(struct sk_buff **pskb,
45 enum ip_conntrack_info ctinfo,
47 const char **dptr) __read_mostly;
48 EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
50 unsigned int (*nf_nat_sdp_hook)(struct sk_buff **pskb,
51 enum ip_conntrack_info ctinfo,
52 struct nf_conntrack_expect *exp,
53 const char *dptr) __read_mostly;
54 EXPORT_SYMBOL_GPL(nf_nat_sdp_hook);
56 static int digits_len(struct nf_conn *, const char *, const char *, int *);
57 static int epaddr_len(struct nf_conn *, const char *, const char *, int *);
58 static int skp_digits_len(struct nf_conn *, const char *, const char *, int *);
59 static int skp_epaddr_len(struct nf_conn *, const char *, const char *, int *);
61 struct sip_header_nfo {
69 int (*match_len)(struct nf_conn *, const char *,
73 static const struct sip_header_nfo ct_sip_hdrs[] = {
74 [POS_REG_REQ_URI] = { /* SIP REGISTER request URI */
76 .lnlen = sizeof("sip:") - 1,
78 .ln_strlen = sizeof(":") - 1,
79 .match_len = epaddr_len,
81 [POS_REQ_URI] = { /* SIP request URI */
83 .lnlen = sizeof("sip:") - 1,
85 .ln_strlen = sizeof("@") - 1,
86 .match_len = epaddr_len,
88 [POS_FROM] = { /* SIP From header */
90 .lnlen = sizeof("From:") - 1,
92 .snlen = sizeof("\r\nf:") - 1,
94 .ln_strlen = sizeof("sip:") - 1,
95 .match_len = skp_epaddr_len,
97 [POS_TO] = { /* SIP To header */
99 .lnlen = sizeof("To:") - 1,
101 .snlen = sizeof("\r\nt:") - 1,
103 .ln_strlen = sizeof("sip:") - 1,
104 .match_len = skp_epaddr_len
106 [POS_VIA] = { /* SIP Via header */
108 .lnlen = sizeof("Via:") - 1,
110 .snlen = sizeof("\r\nv:") - 1, /* rfc3261 "\r\n" */
112 .ln_strlen = sizeof("UDP ") - 1,
113 .match_len = epaddr_len,
115 [POS_CONTACT] = { /* SIP Contact header */
117 .lnlen = sizeof("Contact:") - 1,
119 .snlen = sizeof("\r\nm:") - 1,
121 .ln_strlen = sizeof("sip:") - 1,
122 .match_len = skp_epaddr_len
124 [POS_CONTENT] = { /* SIP Content length header */
125 .lname = "Content-Length:",
126 .lnlen = sizeof("Content-Length:") - 1,
128 .snlen = sizeof("\r\nl:") - 1,
130 .ln_strlen = sizeof(":") - 1,
131 .match_len = skp_digits_len
133 [POS_MEDIA] = { /* SDP media info */
136 .lnlen = sizeof("\nm=") - 1,
138 .snlen = sizeof("\rm=") - 1,
140 .ln_strlen = sizeof("audio ") - 1,
141 .match_len = digits_len
143 [POS_OWNER_IP4] = { /* SDP owner address*/
146 .lnlen = sizeof("\no=") - 1,
148 .snlen = sizeof("\ro=") - 1,
150 .ln_strlen = sizeof("IN IP4 ") - 1,
151 .match_len = epaddr_len
153 [POS_CONNECTION_IP4] = {/* SDP connection info */
156 .lnlen = sizeof("\nc=") - 1,
158 .snlen = sizeof("\rc=") - 1,
160 .ln_strlen = sizeof("IN IP4 ") - 1,
161 .match_len = epaddr_len
163 [POS_OWNER_IP6] = { /* SDP owner address*/
166 .lnlen = sizeof("\no=") - 1,
168 .snlen = sizeof("\ro=") - 1,
170 .ln_strlen = sizeof("IN IP6 ") - 1,
171 .match_len = epaddr_len
173 [POS_CONNECTION_IP6] = {/* SDP connection info */
176 .lnlen = sizeof("\nc=") - 1,
178 .snlen = sizeof("\rc=") - 1,
180 .ln_strlen = sizeof("IN IP6 ") - 1,
181 .match_len = epaddr_len
183 [POS_SDP_HEADER] = { /* SDP version header */
186 .lnlen = sizeof("\nv=") - 1,
188 .snlen = sizeof("\rv=") - 1,
190 .ln_strlen = sizeof("=") - 1,
191 .match_len = digits_len
195 /* get line lenght until first CR or LF seen. */
196 int ct_sip_lnlen(const char *line, const char *limit)
198 const char *k = line;
200 while ((line <= limit) && (*line == '\r' || *line == '\n'))
203 while (line <= limit) {
204 if (*line == '\r' || *line == '\n')
210 EXPORT_SYMBOL_GPL(ct_sip_lnlen);
212 /* Linear string search, case sensitive. */
213 const char *ct_sip_search(const char *needle, const char *haystack,
214 size_t needle_len, size_t haystack_len,
217 const char *limit = haystack + (haystack_len - needle_len);
219 while (haystack <= limit) {
220 if (case_sensitive) {
221 if (strncmp(haystack, needle, needle_len) == 0)
224 if (strnicmp(haystack, needle, needle_len) == 0)
231 EXPORT_SYMBOL_GPL(ct_sip_search);
233 static int digits_len(struct nf_conn *ct, const char *dptr,
234 const char *limit, int *shift)
237 while (dptr <= limit && isdigit(*dptr)) {
244 /* get digits lenght, skiping blank spaces. */
245 static int skp_digits_len(struct nf_conn *ct, const char *dptr,
246 const char *limit, int *shift)
248 for (; dptr <= limit && *dptr == ' '; dptr++)
251 return digits_len(ct, dptr, limit, shift);
254 static int parse_addr(struct nf_conn *ct, const char *cp, const char **endp,
255 union nf_conntrack_address *addr, const char *limit)
258 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
263 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
266 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
272 if (ret == 0 || end == cp)
279 /* skip ip address. returns its length. */
280 static int epaddr_len(struct nf_conn *ct, const char *dptr,
281 const char *limit, int *shift)
283 union nf_conntrack_address addr;
284 const char *aux = dptr;
286 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
287 DEBUGP("ip: %s parse failed.!\n", dptr);
294 dptr += digits_len(ct, dptr, limit, shift);
299 /* get address length, skiping user info. */
300 static int skp_epaddr_len(struct nf_conn *ct, const char *dptr,
301 const char *limit, int *shift)
305 for (; dptr <= limit && *dptr != '@'; dptr++)
314 return epaddr_len(ct, dptr, limit, shift);
317 /* Returns 0 if not found, -1 error parsing. */
318 int ct_sip_get_info(struct nf_conn *ct,
319 const char *dptr, size_t dlen,
320 unsigned int *matchoff,
321 unsigned int *matchlen,
322 enum sip_header_pos pos)
324 const struct sip_header_nfo *hnfo = &ct_sip_hdrs[pos];
325 const char *limit, *aux, *k = dptr;
328 limit = dptr + (dlen - hnfo->lnlen);
330 while (dptr <= limit) {
331 if ((strncmp(dptr, hnfo->lname, hnfo->lnlen) != 0) &&
332 (strncmp(dptr, hnfo->sname, hnfo->snlen) != 0)) {
336 aux = ct_sip_search(hnfo->ln_str, dptr, hnfo->ln_strlen,
337 ct_sip_lnlen(dptr, limit),
338 hnfo->case_sensitive);
340 DEBUGP("'%s' not found in '%s'.\n", hnfo->ln_str,
344 aux += hnfo->ln_strlen;
346 *matchlen = hnfo->match_len(ct, aux, limit, &shift);
350 *matchoff = (aux - k) + shift;
352 DEBUGP("%s match succeeded! - len: %u\n", hnfo->lname,
356 DEBUGP("%s header not found.\n", hnfo->lname);
359 EXPORT_SYMBOL_GPL(ct_sip_get_info);
361 static int set_expected_rtp(struct sk_buff **pskb,
363 enum ip_conntrack_info ctinfo,
364 union nf_conntrack_address *addr,
368 struct nf_conntrack_expect *exp;
369 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
370 int family = ct->tuplehash[!dir].tuple.src.l3num;
372 typeof(nf_nat_sdp_hook) nf_nat_sdp;
374 exp = nf_conntrack_expect_alloc(ct);
377 nf_conntrack_expect_init(exp, family,
378 &ct->tuplehash[!dir].tuple.src.u3, addr,
379 IPPROTO_UDP, NULL, &port);
381 nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook);
382 if (nf_nat_sdp && ct->status & IPS_NAT_MASK)
383 ret = nf_nat_sdp(pskb, ctinfo, exp, dptr);
385 if (nf_conntrack_expect_related(exp) != 0)
390 nf_conntrack_expect_put(exp);
395 static int sip_help(struct sk_buff **pskb,
396 unsigned int protoff,
398 enum ip_conntrack_info ctinfo)
400 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
401 union nf_conntrack_address addr;
402 unsigned int dataoff, datalen;
405 int matchoff, matchlen;
407 enum sip_header_pos pos;
408 typeof(nf_nat_sip_hook) nf_nat_sip;
411 dataoff = protoff + sizeof(struct udphdr);
412 if (dataoff >= (*pskb)->len)
415 nf_ct_refresh(ct, *pskb, sip_timeout * HZ);
417 if (!skb_is_nonlinear(*pskb))
418 dptr = (*pskb)->data + dataoff;
420 DEBUGP("Copy of skbuff not supported yet.\n");
424 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
425 if (nf_nat_sip && ct->status & IPS_NAT_MASK) {
426 if (!nf_nat_sip(pskb, ctinfo, ct, &dptr)) {
432 datalen = (*pskb)->len - dataoff;
433 if (datalen < sizeof("SIP/2.0 200") - 1)
436 /* RTP info only in some SDP pkts */
437 if (memcmp(dptr, "INVITE", sizeof("INVITE") - 1) != 0 &&
438 memcmp(dptr, "SIP/2.0 200", sizeof("SIP/2.0 200") - 1) != 0) {
441 /* Get address and port from SDP packet. */
442 pos = family == AF_INET ? POS_CONNECTION_IP4 : POS_CONNECTION_IP6;
443 if (ct_sip_get_info(ct, dptr, datalen, &matchoff, &matchlen, pos) > 0) {
445 /* We'll drop only if there are parse problems. */
446 if (!parse_addr(ct, dptr + matchoff, NULL, &addr,
451 if (ct_sip_get_info(ct, dptr, datalen, &matchoff, &matchlen,
454 port = simple_strtoul(dptr + matchoff, NULL, 10);
459 ret = set_expected_rtp(pskb, ct, ctinfo, &addr,
467 static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
468 static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
470 static void nf_conntrack_sip_fini(void)
474 for (i = 0; i < ports_c; i++) {
475 for (j = 0; j < 2; j++) {
476 if (sip[i][j].me == NULL)
478 nf_conntrack_helper_unregister(&sip[i][j]);
483 static int __init nf_conntrack_sip_init(void)
489 ports[ports_c++] = SIP_PORT;
491 for (i = 0; i < ports_c; i++) {
492 memset(&sip[i], 0, sizeof(sip[i]));
494 sip[i][0].tuple.src.l3num = AF_INET;
495 sip[i][1].tuple.src.l3num = AF_INET6;
496 for (j = 0; j < 2; j++) {
497 sip[i][j].tuple.dst.protonum = IPPROTO_UDP;
498 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
499 sip[i][j].mask.src.l3num = 0xFFFF;
500 sip[i][j].mask.src.u.udp.port = htons(0xFFFF);
501 sip[i][j].mask.dst.protonum = 0xFF;
502 sip[i][j].max_expected = 2;
503 sip[i][j].timeout = 3 * 60; /* 3 minutes */
504 sip[i][j].me = THIS_MODULE;
505 sip[i][j].help = sip_help;
507 tmpname = &sip_names[i][j][0];
508 if (ports[i] == SIP_PORT)
509 sprintf(tmpname, "sip");
511 sprintf(tmpname, "sip-%u", i);
512 sip[i][j].name = tmpname;
514 DEBUGP("port #%u: %u\n", i, ports[i]);
516 ret = nf_conntrack_helper_register(&sip[i][j]);
518 printk("nf_ct_sip: failed to register helper "
519 "for pf: %u port: %u\n",
520 sip[i][j].tuple.src.l3num, ports[i]);
521 nf_conntrack_sip_fini();
529 module_init(nf_conntrack_sip_init);
530 module_exit(nf_conntrack_sip_fini);