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];
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 **pskb,
40 enum ip_conntrack_info ctinfo,
42 const char **dptr) __read_mostly;
43 EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
45 unsigned int (*nf_nat_sdp_hook)(struct sk_buff **pskb,
46 enum ip_conntrack_info ctinfo,
47 struct nf_conntrack_expect *exp,
48 const char *dptr) __read_mostly;
49 EXPORT_SYMBOL_GPL(nf_nat_sdp_hook);
51 static int digits_len(struct nf_conn *, const char *, const char *, int *);
52 static int epaddr_len(struct nf_conn *, const char *, const char *, int *);
53 static int skp_digits_len(struct nf_conn *, const char *, const char *, int *);
54 static int skp_epaddr_len(struct nf_conn *, const char *, const char *, int *);
56 struct sip_header_nfo {
64 int (*match_len)(struct nf_conn *, const char *,
68 static const struct sip_header_nfo ct_sip_hdrs[] = {
69 [POS_REG_REQ_URI] = { /* SIP REGISTER request URI */
71 .lnlen = sizeof("sip:") - 1,
73 .ln_strlen = sizeof(":") - 1,
74 .match_len = epaddr_len,
76 [POS_REQ_URI] = { /* SIP request URI */
78 .lnlen = sizeof("sip:") - 1,
80 .ln_strlen = sizeof("@") - 1,
81 .match_len = epaddr_len,
83 [POS_FROM] = { /* SIP From header */
85 .lnlen = sizeof("From:") - 1,
87 .snlen = sizeof("\r\nf:") - 1,
89 .ln_strlen = sizeof("sip:") - 1,
90 .match_len = skp_epaddr_len,
92 [POS_TO] = { /* SIP To header */
94 .lnlen = sizeof("To:") - 1,
96 .snlen = sizeof("\r\nt:") - 1,
98 .ln_strlen = sizeof("sip:") - 1,
99 .match_len = skp_epaddr_len
101 [POS_VIA] = { /* SIP Via header */
103 .lnlen = sizeof("Via:") - 1,
105 .snlen = sizeof("\r\nv:") - 1, /* rfc3261 "\r\n" */
107 .ln_strlen = sizeof("UDP ") - 1,
108 .match_len = epaddr_len,
110 [POS_CONTACT] = { /* SIP Contact header */
112 .lnlen = sizeof("Contact:") - 1,
114 .snlen = sizeof("\r\nm:") - 1,
116 .ln_strlen = sizeof("sip:") - 1,
117 .match_len = skp_epaddr_len
119 [POS_CONTENT] = { /* SIP Content length header */
120 .lname = "Content-Length:",
121 .lnlen = sizeof("Content-Length:") - 1,
123 .snlen = sizeof("\r\nl:") - 1,
125 .ln_strlen = sizeof(":") - 1,
126 .match_len = skp_digits_len
128 [POS_MEDIA] = { /* SDP media info */
131 .lnlen = sizeof("\nm=") - 1,
133 .snlen = sizeof("\rm=") - 1,
135 .ln_strlen = sizeof("audio ") - 1,
136 .match_len = digits_len
138 [POS_OWNER_IP4] = { /* SDP owner address*/
141 .lnlen = sizeof("\no=") - 1,
143 .snlen = sizeof("\ro=") - 1,
145 .ln_strlen = sizeof("IN IP4 ") - 1,
146 .match_len = epaddr_len
148 [POS_CONNECTION_IP4] = {/* SDP connection info */
151 .lnlen = sizeof("\nc=") - 1,
153 .snlen = sizeof("\rc=") - 1,
155 .ln_strlen = sizeof("IN IP4 ") - 1,
156 .match_len = epaddr_len
158 [POS_OWNER_IP6] = { /* SDP owner address*/
161 .lnlen = sizeof("\no=") - 1,
163 .snlen = sizeof("\ro=") - 1,
165 .ln_strlen = sizeof("IN IP6 ") - 1,
166 .match_len = epaddr_len
168 [POS_CONNECTION_IP6] = {/* SDP connection info */
171 .lnlen = sizeof("\nc=") - 1,
173 .snlen = sizeof("\rc=") - 1,
175 .ln_strlen = sizeof("IN IP6 ") - 1,
176 .match_len = epaddr_len
178 [POS_SDP_HEADER] = { /* SDP version header */
181 .lnlen = sizeof("\nv=") - 1,
183 .snlen = sizeof("\rv=") - 1,
185 .ln_strlen = sizeof("=") - 1,
186 .match_len = digits_len
190 /* get line lenght until first CR or LF seen. */
191 int ct_sip_lnlen(const char *line, const char *limit)
193 const char *k = line;
195 while ((line <= limit) && (*line == '\r' || *line == '\n'))
198 while (line <= limit) {
199 if (*line == '\r' || *line == '\n')
205 EXPORT_SYMBOL_GPL(ct_sip_lnlen);
207 /* Linear string search, case sensitive. */
208 const char *ct_sip_search(const char *needle, const char *haystack,
209 size_t needle_len, size_t haystack_len,
212 const char *limit = haystack + (haystack_len - needle_len);
214 while (haystack <= limit) {
215 if (case_sensitive) {
216 if (strncmp(haystack, needle, needle_len) == 0)
219 if (strnicmp(haystack, needle, needle_len) == 0)
226 EXPORT_SYMBOL_GPL(ct_sip_search);
228 static int digits_len(struct nf_conn *ct, const char *dptr,
229 const char *limit, int *shift)
232 while (dptr <= limit && isdigit(*dptr)) {
239 /* get digits lenght, skiping blank spaces. */
240 static int skp_digits_len(struct nf_conn *ct, const char *dptr,
241 const char *limit, int *shift)
243 for (; dptr <= limit && *dptr == ' '; dptr++)
246 return digits_len(ct, dptr, limit, shift);
249 static int parse_addr(struct nf_conn *ct, const char *cp, const char **endp,
250 union nf_conntrack_address *addr, const char *limit)
253 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
258 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
261 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
267 if (ret == 0 || end == cp)
274 /* skip ip address. returns its length. */
275 static int epaddr_len(struct nf_conn *ct, const char *dptr,
276 const char *limit, int *shift)
278 union nf_conntrack_address addr;
279 const char *aux = dptr;
281 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
282 pr_debug("ip: %s parse failed.!\n", dptr);
289 dptr += digits_len(ct, dptr, limit, shift);
294 /* get address length, skiping user info. */
295 static int skp_epaddr_len(struct nf_conn *ct, const char *dptr,
296 const char *limit, int *shift)
298 const char *start = dptr;
301 /* Search for @, but stop at the end of the line.
302 * We are inside a sip: URI, so we don't need to worry about
303 * continuation lines. */
304 while (dptr <= limit &&
305 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
310 if (dptr <= limit && *dptr == '@') {
318 return epaddr_len(ct, dptr, limit, shift);
321 /* Returns 0 if not found, -1 error parsing. */
322 int ct_sip_get_info(struct nf_conn *ct,
323 const char *dptr, size_t dlen,
324 unsigned int *matchoff,
325 unsigned int *matchlen,
326 enum sip_header_pos pos)
328 const struct sip_header_nfo *hnfo = &ct_sip_hdrs[pos];
329 const char *limit, *aux, *k = dptr;
332 limit = dptr + (dlen - hnfo->lnlen);
334 while (dptr <= limit) {
335 if ((strncmp(dptr, hnfo->lname, hnfo->lnlen) != 0) &&
336 (hnfo->sname == NULL ||
337 strncmp(dptr, hnfo->sname, hnfo->snlen) != 0)) {
341 aux = ct_sip_search(hnfo->ln_str, dptr, hnfo->ln_strlen,
342 ct_sip_lnlen(dptr, limit),
343 hnfo->case_sensitive);
345 pr_debug("'%s' not found in '%s'.\n", hnfo->ln_str,
349 aux += hnfo->ln_strlen;
351 *matchlen = hnfo->match_len(ct, aux, limit, &shift);
355 *matchoff = (aux - k) + shift;
357 pr_debug("%s match succeeded! - len: %u\n", hnfo->lname,
361 pr_debug("%s header not found.\n", hnfo->lname);
364 EXPORT_SYMBOL_GPL(ct_sip_get_info);
366 static int set_expected_rtp(struct sk_buff **pskb,
368 enum ip_conntrack_info ctinfo,
369 union nf_conntrack_address *addr,
373 struct nf_conntrack_expect *exp;
374 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
375 int family = ct->tuplehash[!dir].tuple.src.l3num;
377 typeof(nf_nat_sdp_hook) nf_nat_sdp;
379 exp = nf_ct_expect_alloc(ct);
382 nf_ct_expect_init(exp, family,
383 &ct->tuplehash[!dir].tuple.src.u3, addr,
384 IPPROTO_UDP, NULL, &port);
386 nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook);
387 if (nf_nat_sdp && ct->status & IPS_NAT_MASK)
388 ret = nf_nat_sdp(pskb, ctinfo, exp, dptr);
390 if (nf_ct_expect_related(exp) != 0)
395 nf_ct_expect_put(exp);
400 static int sip_help(struct sk_buff **pskb,
401 unsigned int protoff,
403 enum ip_conntrack_info ctinfo)
405 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
406 union nf_conntrack_address addr;
407 unsigned int dataoff, datalen;
410 int matchoff, matchlen;
412 enum sip_header_pos pos;
413 typeof(nf_nat_sip_hook) nf_nat_sip;
416 dataoff = protoff + sizeof(struct udphdr);
417 if (dataoff >= (*pskb)->len)
420 nf_ct_refresh(ct, *pskb, sip_timeout * HZ);
422 if (!skb_is_nonlinear(*pskb))
423 dptr = (*pskb)->data + dataoff;
425 pr_debug("Copy of skbuff not supported yet.\n");
429 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
430 if (nf_nat_sip && ct->status & IPS_NAT_MASK) {
431 if (!nf_nat_sip(pskb, ctinfo, ct, &dptr)) {
437 datalen = (*pskb)->len - dataoff;
438 if (datalen < sizeof("SIP/2.0 200") - 1)
441 /* RTP info only in some SDP pkts */
442 if (memcmp(dptr, "INVITE", sizeof("INVITE") - 1) != 0 &&
443 memcmp(dptr, "UPDATE", sizeof("UPDATE") - 1) != 0 &&
444 memcmp(dptr, "SIP/2.0 180", sizeof("SIP/2.0 180") - 1) != 0 &&
445 memcmp(dptr, "SIP/2.0 183", sizeof("SIP/2.0 183") - 1) != 0 &&
446 memcmp(dptr, "SIP/2.0 200", sizeof("SIP/2.0 200") - 1) != 0) {
449 /* Get address and port from SDP packet. */
450 pos = family == AF_INET ? POS_CONNECTION_IP4 : POS_CONNECTION_IP6;
451 if (ct_sip_get_info(ct, dptr, datalen, &matchoff, &matchlen, pos) > 0) {
453 /* We'll drop only if there are parse problems. */
454 if (!parse_addr(ct, dptr + matchoff, NULL, &addr,
459 if (ct_sip_get_info(ct, dptr, datalen, &matchoff, &matchlen,
462 port = simple_strtoul(dptr + matchoff, NULL, 10);
467 ret = set_expected_rtp(pskb, ct, ctinfo, &addr,
475 static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
476 static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
478 static void nf_conntrack_sip_fini(void)
482 for (i = 0; i < ports_c; i++) {
483 for (j = 0; j < 2; j++) {
484 if (sip[i][j].me == NULL)
486 nf_conntrack_helper_unregister(&sip[i][j]);
491 static int __init nf_conntrack_sip_init(void)
497 ports[ports_c++] = SIP_PORT;
499 for (i = 0; i < ports_c; i++) {
500 memset(&sip[i], 0, sizeof(sip[i]));
502 sip[i][0].tuple.src.l3num = AF_INET;
503 sip[i][1].tuple.src.l3num = AF_INET6;
504 for (j = 0; j < 2; j++) {
505 sip[i][j].tuple.dst.protonum = IPPROTO_UDP;
506 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
507 sip[i][j].max_expected = 2;
508 sip[i][j].timeout = 3 * 60; /* 3 minutes */
509 sip[i][j].me = THIS_MODULE;
510 sip[i][j].help = sip_help;
512 tmpname = &sip_names[i][j][0];
513 if (ports[i] == SIP_PORT)
514 sprintf(tmpname, "sip");
516 sprintf(tmpname, "sip-%u", i);
517 sip[i][j].name = tmpname;
519 pr_debug("port #%u: %u\n", i, ports[i]);
521 ret = nf_conntrack_helper_register(&sip[i][j]);
523 printk("nf_ct_sip: failed to register helper "
524 "for pf: %u port: %u\n",
525 sip[i][j].tuple.src.l3num, ports[i]);
526 nf_conntrack_sip_fini();
534 module_init(nf_conntrack_sip_init);
535 module_exit(nf_conntrack_sip_fini);