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,
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 *skb,
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(const struct nf_conn *, const char *, const char *, int *);
52 static int epaddr_len(const struct nf_conn *, const char *, const char *, int *);
53 static int skp_digits_len(const struct nf_conn *, const char *, const char *, int *);
54 static int skp_epaddr_len(const struct nf_conn *, const char *, const char *, int *);
56 struct sip_header_nfo {
64 int (*match_len)(const 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 length 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(const struct nf_conn *ct, const char *dptr,
229 const char *limit, int *shift)
232 while (dptr <= limit && isdigit(*dptr)) {
239 /* get digits length, skipping blank spaces. */
240 static int skp_digits_len(const 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(const struct nf_conn *ct, const char *cp,
250 const char **endp, union nf_inet_addr *addr,
254 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
259 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
262 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
268 if (ret == 0 || end == cp)
275 /* skip ip address. returns its length. */
276 static int epaddr_len(const struct nf_conn *ct, const char *dptr,
277 const char *limit, int *shift)
279 union nf_inet_addr addr;
280 const char *aux = dptr;
282 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
283 pr_debug("ip: %s parse failed.!\n", dptr);
290 dptr += digits_len(ct, dptr, limit, shift);
295 /* get address length, skiping user info. */
296 static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
297 const char *limit, int *shift)
299 const char *start = dptr;
302 /* Search for @, but stop at the end of the line.
303 * We are inside a sip: URI, so we don't need to worry about
304 * continuation lines. */
305 while (dptr <= limit &&
306 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
311 if (dptr <= limit && *dptr == '@') {
319 return epaddr_len(ct, dptr, limit, shift);
322 /* Returns 0 if not found, -1 error parsing. */
323 int ct_sip_get_info(const struct nf_conn *ct,
324 const char *dptr, size_t dlen,
325 unsigned int *matchoff,
326 unsigned int *matchlen,
327 enum sip_header_pos pos)
329 const struct sip_header_nfo *hnfo = &ct_sip_hdrs[pos];
330 const char *limit, *aux, *k = dptr;
333 limit = dptr + (dlen - hnfo->lnlen);
335 while (dptr <= limit) {
336 if ((strncmp(dptr, hnfo->lname, hnfo->lnlen) != 0) &&
337 (hnfo->sname == NULL ||
338 strncmp(dptr, hnfo->sname, hnfo->snlen) != 0)) {
342 aux = ct_sip_search(hnfo->ln_str, dptr, hnfo->ln_strlen,
343 ct_sip_lnlen(dptr, limit),
344 hnfo->case_sensitive);
346 pr_debug("'%s' not found in '%s'.\n", hnfo->ln_str,
350 aux += hnfo->ln_strlen;
352 *matchlen = hnfo->match_len(ct, aux, limit, &shift);
356 *matchoff = (aux - k) + shift;
358 pr_debug("%s match succeeded! - len: %u\n", hnfo->lname,
362 pr_debug("%s header not found.\n", hnfo->lname);
365 EXPORT_SYMBOL_GPL(ct_sip_get_info);
367 static int set_expected_rtp(struct sk_buff *skb,
369 enum ip_conntrack_info ctinfo,
370 union nf_inet_addr *addr,
374 struct nf_conntrack_expect *exp;
375 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
376 int family = ct->tuplehash[!dir].tuple.src.l3num;
378 typeof(nf_nat_sdp_hook) nf_nat_sdp;
380 exp = nf_ct_expect_alloc(ct);
383 nf_ct_expect_init(exp, family,
384 &ct->tuplehash[!dir].tuple.src.u3, addr,
385 IPPROTO_UDP, NULL, &port);
387 nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook);
388 if (nf_nat_sdp && ct->status & IPS_NAT_MASK)
389 ret = nf_nat_sdp(skb, ctinfo, exp, dptr);
391 if (nf_ct_expect_related(exp) != 0)
396 nf_ct_expect_put(exp);
401 static int sip_help(struct sk_buff *skb,
402 unsigned int protoff,
404 enum ip_conntrack_info ctinfo)
406 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
407 union nf_inet_addr addr;
408 unsigned int dataoff, datalen;
411 unsigned int matchoff, matchlen;
413 enum sip_header_pos pos;
414 typeof(nf_nat_sip_hook) nf_nat_sip;
417 dataoff = protoff + sizeof(struct udphdr);
418 if (dataoff >= skb->len)
421 nf_ct_refresh(ct, skb, sip_timeout * HZ);
423 if (!skb_is_nonlinear(skb))
424 dptr = skb->data + dataoff;
426 pr_debug("Copy of skbuff not supported yet.\n");
430 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
431 if (nf_nat_sip && ct->status & IPS_NAT_MASK) {
432 if (!nf_nat_sip(skb, ctinfo, ct, &dptr)) {
438 datalen = skb->len - dataoff;
439 if (datalen < sizeof("SIP/2.0 200") - 1)
442 /* RTP info only in some SDP pkts */
443 if (memcmp(dptr, "INVITE", sizeof("INVITE") - 1) != 0 &&
444 memcmp(dptr, "UPDATE", sizeof("UPDATE") - 1) != 0 &&
445 memcmp(dptr, "SIP/2.0 180", sizeof("SIP/2.0 180") - 1) != 0 &&
446 memcmp(dptr, "SIP/2.0 183", sizeof("SIP/2.0 183") - 1) != 0 &&
447 memcmp(dptr, "SIP/2.0 200", sizeof("SIP/2.0 200") - 1) != 0) {
450 /* Get address and port from SDP packet. */
451 pos = family == AF_INET ? POS_CONNECTION_IP4 : POS_CONNECTION_IP6;
452 if (ct_sip_get_info(ct, dptr, datalen, &matchoff, &matchlen, pos) > 0) {
454 /* We'll drop only if there are parse problems. */
455 if (!parse_addr(ct, dptr + matchoff, NULL, &addr,
460 if (ct_sip_get_info(ct, dptr, datalen, &matchoff, &matchlen,
463 port = simple_strtoul(dptr + matchoff, NULL, 10);
468 ret = set_expected_rtp(skb, ct, ctinfo, &addr,
476 static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
477 static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
479 static void nf_conntrack_sip_fini(void)
483 for (i = 0; i < ports_c; i++) {
484 for (j = 0; j < 2; j++) {
485 if (sip[i][j].me == NULL)
487 nf_conntrack_helper_unregister(&sip[i][j]);
492 static int __init nf_conntrack_sip_init(void)
498 ports[ports_c++] = SIP_PORT;
500 for (i = 0; i < ports_c; i++) {
501 memset(&sip[i], 0, sizeof(sip[i]));
503 sip[i][0].tuple.src.l3num = AF_INET;
504 sip[i][1].tuple.src.l3num = AF_INET6;
505 for (j = 0; j < 2; j++) {
506 sip[i][j].tuple.dst.protonum = IPPROTO_UDP;
507 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
508 sip[i][j].max_expected = 2;
509 sip[i][j].timeout = 3 * 60; /* 3 minutes */
510 sip[i][j].me = THIS_MODULE;
511 sip[i][j].help = sip_help;
513 tmpname = &sip_names[i][j][0];
514 if (ports[i] == SIP_PORT)
515 sprintf(tmpname, "sip");
517 sprintf(tmpname, "sip-%u", i);
518 sip[i][j].name = tmpname;
520 pr_debug("port #%u: %u\n", i, ports[i]);
522 ret = nf_conntrack_helper_register(&sip[i][j]);
524 printk("nf_ct_sip: failed to register helper "
525 "for pf: %u port: %u\n",
526 sip[i][j].tuple.src.l3num, ports[i]);
527 nf_conntrack_sip_fini();
535 module_init(nf_conntrack_sip_init);
536 module_exit(nf_conntrack_sip_fini);