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>
16 #include <linux/udp.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
21 #include <linux/netfilter_ipv4/ip_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");
34 static unsigned short ports
[MAX_PORTS
];
36 module_param_array(ports
, ushort
, &ports_c
, 0400);
37 MODULE_PARM_DESC(ports
, "port numbers of sip servers");
39 static unsigned int sip_timeout
= SIP_TIMEOUT
;
40 module_param(sip_timeout
, uint
, 0600);
41 MODULE_PARM_DESC(sip_timeout
, "timeout for the master SIP session");
43 unsigned int (*ip_nat_sip_hook
)(struct sk_buff
**pskb
,
44 enum ip_conntrack_info ctinfo
,
45 struct ip_conntrack
*ct
,
47 EXPORT_SYMBOL_GPL(ip_nat_sip_hook
);
49 unsigned int (*ip_nat_sdp_hook
)(struct sk_buff
**pskb
,
50 enum ip_conntrack_info ctinfo
,
51 struct ip_conntrack_expect
*exp
,
53 EXPORT_SYMBOL_GPL(ip_nat_sdp_hook
);
55 static int digits_len(const char *dptr
, const char *limit
, int *shift
);
56 static int epaddr_len(const char *dptr
, const char *limit
, int *shift
);
57 static int skp_digits_len(const char *dptr
, const char *limit
, int *shift
);
58 static int skp_epaddr_len(const char *dptr
, const char *limit
, int *shift
);
60 struct sip_header_nfo
{
68 int (*match_len
)(const char *, const char *, int *);
71 static struct sip_header_nfo ct_sip_hdrs
[] = {
72 [POS_REG_REQ_URI
] = { /* SIP REGISTER request URI */
74 .lnlen
= sizeof("sip:") - 1,
76 .ln_strlen
= sizeof(":") - 1,
77 .match_len
= epaddr_len
79 [POS_REQ_URI
] = { /* SIP request URI */
81 .lnlen
= sizeof("sip:") - 1,
83 .ln_strlen
= sizeof("@") - 1,
84 .match_len
= epaddr_len
86 [POS_FROM
] = { /* SIP From header */
88 .lnlen
= sizeof("From:") - 1,
90 .snlen
= sizeof("\r\nf:") - 1,
92 .ln_strlen
= sizeof("sip:") - 1,
93 .match_len
= skp_epaddr_len
,
95 [POS_TO
] = { /* SIP To header */
97 .lnlen
= sizeof("To:") - 1,
99 .snlen
= sizeof("\r\nt:") - 1,
101 .ln_strlen
= sizeof("sip:") - 1,
102 .match_len
= skp_epaddr_len
,
104 [POS_VIA
] = { /* SIP Via header */
106 .lnlen
= sizeof("Via:") - 1,
108 .snlen
= sizeof("\r\nv:") - 1, /* rfc3261 "\r\n" */
110 .ln_strlen
= sizeof("UDP ") - 1,
111 .match_len
= epaddr_len
,
113 [POS_CONTACT
] = { /* SIP Contact header */
115 .lnlen
= sizeof("Contact:") - 1,
117 .snlen
= sizeof("\r\nm:") - 1,
119 .ln_strlen
= sizeof("sip:") - 1,
120 .match_len
= skp_epaddr_len
122 [POS_CONTENT
] = { /* SIP Content length header */
123 .lname
= "Content-Length:",
124 .lnlen
= sizeof("Content-Length:") - 1,
126 .snlen
= sizeof("\r\nl:") - 1,
128 .ln_strlen
= sizeof(":") - 1,
129 .match_len
= skp_digits_len
131 [POS_MEDIA
] = { /* SDP media info */
134 .lnlen
= sizeof("\nm=") - 1,
136 .snlen
= sizeof("\rm=") - 1,
138 .ln_strlen
= sizeof("audio ") - 1,
139 .match_len
= digits_len
141 [POS_OWNER
] = { /* SDP owner address*/
144 .lnlen
= sizeof("\no=") - 1,
146 .snlen
= sizeof("\ro=") - 1,
148 .ln_strlen
= sizeof("IN IP4 ") - 1,
149 .match_len
= epaddr_len
151 [POS_CONNECTION
] = { /* SDP connection info */
154 .lnlen
= sizeof("\nc=") - 1,
156 .snlen
= sizeof("\rc=") - 1,
158 .ln_strlen
= sizeof("IN IP4 ") - 1,
159 .match_len
= epaddr_len
161 [POS_SDP_HEADER
] = { /* SDP version header */
164 .lnlen
= sizeof("\nv=") - 1,
166 .snlen
= sizeof("\rv=") - 1,
168 .ln_strlen
= sizeof("=") - 1,
169 .match_len
= digits_len
173 /* get line lenght until first CR or LF seen. */
174 int ct_sip_lnlen(const char *line
, const char *limit
)
176 const char *k
= line
;
178 while ((line
<= limit
) && (*line
== '\r' || *line
== '\n'))
181 while (line
<= limit
) {
182 if (*line
== '\r' || *line
== '\n')
188 EXPORT_SYMBOL_GPL(ct_sip_lnlen
);
190 /* Linear string search, case sensitive. */
191 const char *ct_sip_search(const char *needle
, const char *haystack
,
192 size_t needle_len
, size_t haystack_len
,
195 const char *limit
= haystack
+ (haystack_len
- needle_len
);
197 while (haystack
<= limit
) {
198 if (case_sensitive
) {
199 if (strncmp(haystack
, needle
, needle_len
) == 0)
202 if (strnicmp(haystack
, needle
, needle_len
) == 0)
209 EXPORT_SYMBOL_GPL(ct_sip_search
);
211 static int digits_len(const char *dptr
, const char *limit
, int *shift
)
214 while (dptr
<= limit
&& isdigit(*dptr
)) {
221 /* get digits lenght, skiping blank spaces. */
222 static int skp_digits_len(const char *dptr
, const char *limit
, int *shift
)
224 for (; dptr
<= limit
&& *dptr
== ' '; dptr
++)
227 return digits_len(dptr
, limit
, shift
);
230 /* Simple ipaddr parser.. */
231 static int parse_ipaddr(const char *cp
, const char **endp
,
232 __be32
*ipaddr
, const char *limit
)
234 unsigned long int val
;
237 for (i
= 0, *ipaddr
= 0; cp
<= limit
&& i
< 4; i
++) {
242 val
= simple_strtoul(cp
, (char **)&cp
, 10);
246 ((u_int8_t
*)ipaddr
)[i
] = val
;
262 /* skip ip address. returns it lenght. */
263 static int epaddr_len(const char *dptr
, const char *limit
, int *shift
)
265 const char *aux
= dptr
;
268 if (parse_ipaddr(dptr
, &dptr
, &ip
, limit
) < 0) {
269 DEBUGP("ip: %s parse failed.!\n", dptr
);
276 dptr
+= digits_len(dptr
, limit
, shift
);
281 /* get address length, skiping user info. */
282 static int skp_epaddr_len(const char *dptr
, const char *limit
, int *shift
)
286 for (; dptr
<= limit
&& *dptr
!= '@'; dptr
++)
295 return epaddr_len(dptr
, limit
, shift
);
298 /* Returns 0 if not found, -1 error parsing. */
299 int ct_sip_get_info(const char *dptr
, size_t dlen
,
300 unsigned int *matchoff
,
301 unsigned int *matchlen
,
302 enum sip_header_pos pos
)
304 struct sip_header_nfo
*hnfo
= &ct_sip_hdrs
[pos
];
305 const char *limit
, *aux
, *k
= dptr
;
308 limit
= dptr
+ (dlen
- hnfo
->lnlen
);
310 while (dptr
<= limit
) {
311 if ((strncmp(dptr
, hnfo
->lname
, hnfo
->lnlen
) != 0) &&
312 (hnfo
->sname
== NULL
||
313 strncmp(dptr
, hnfo
->sname
, hnfo
->snlen
) != 0)) {
317 aux
= ct_sip_search(hnfo
->ln_str
, dptr
, hnfo
->ln_strlen
,
318 ct_sip_lnlen(dptr
, limit
),
319 hnfo
->case_sensitive
);
321 DEBUGP("'%s' not found in '%s'.\n", hnfo
->ln_str
,
325 aux
+= hnfo
->ln_strlen
;
327 *matchlen
= hnfo
->match_len(aux
, limit
, &shift
);
331 *matchoff
= (aux
- k
) + shift
;
333 DEBUGP("%s match succeeded! - len: %u\n", hnfo
->lname
,
337 DEBUGP("%s header not found.\n", hnfo
->lname
);
340 EXPORT_SYMBOL_GPL(ct_sip_get_info
);
342 static int set_expected_rtp(struct sk_buff
**pskb
,
343 struct ip_conntrack
*ct
,
344 enum ip_conntrack_info ctinfo
,
345 __be32 ipaddr
, u_int16_t port
,
348 struct ip_conntrack_expect
*exp
;
349 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
351 typeof(ip_nat_sdp_hook
) ip_nat_sdp
;
353 exp
= ip_conntrack_expect_alloc(ct
);
357 exp
->tuple
.src
.ip
= ct
->tuplehash
[!dir
].tuple
.src
.ip
;
358 exp
->tuple
.src
.u
.udp
.port
= 0;
359 exp
->tuple
.dst
.ip
= ipaddr
;
360 exp
->tuple
.dst
.u
.udp
.port
= htons(port
);
361 exp
->tuple
.dst
.protonum
= IPPROTO_UDP
;
363 exp
->mask
.src
.ip
= htonl(0xFFFFFFFF);
364 exp
->mask
.src
.u
.udp
.port
= 0;
365 exp
->mask
.dst
.ip
= htonl(0xFFFFFFFF);
366 exp
->mask
.dst
.u
.udp
.port
= htons(0xFFFF);
367 exp
->mask
.dst
.protonum
= 0xFF;
369 exp
->expectfn
= NULL
;
372 ip_nat_sdp
= rcu_dereference(ip_nat_sdp_hook
);
374 ret
= ip_nat_sdp(pskb
, ctinfo
, exp
, dptr
);
376 if (ip_conntrack_expect_related(exp
) != 0)
381 ip_conntrack_expect_put(exp
);
386 static int sip_help(struct sk_buff
**pskb
,
387 struct ip_conntrack
*ct
,
388 enum ip_conntrack_info ctinfo
)
390 unsigned int dataoff
, datalen
;
393 int matchoff
, matchlen
;
396 typeof(ip_nat_sip_hook
) ip_nat_sip
;
399 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
400 if (dataoff
>= (*pskb
)->len
) {
401 DEBUGP("skb->len = %u\n", (*pskb
)->len
);
405 ip_ct_refresh(ct
, *pskb
, sip_timeout
* HZ
);
407 if (!skb_is_nonlinear(*pskb
))
408 dptr
= (*pskb
)->data
+ dataoff
;
410 DEBUGP("Copy of skbuff not supported yet.\n");
414 ip_nat_sip
= rcu_dereference(ip_nat_sip_hook
);
416 if (!ip_nat_sip(pskb
, ctinfo
, ct
, &dptr
)) {
422 /* After this point NAT, could have mangled skb, so
423 we need to recalculate payload lenght. */
424 datalen
= (*pskb
)->len
- dataoff
;
426 if (datalen
< (sizeof("SIP/2.0 200") - 1))
429 /* RTP info only in some SDP pkts */
430 if (memcmp(dptr
, "INVITE", sizeof("INVITE") - 1) != 0 &&
431 memcmp(dptr
, "SIP/2.0 200", sizeof("SIP/2.0 200") - 1) != 0) {
434 /* Get ip and port address from SDP packet. */
435 if (ct_sip_get_info(dptr
, datalen
, &matchoff
, &matchlen
,
436 POS_CONNECTION
) > 0) {
438 /* We'll drop only if there are parse problems. */
439 if (parse_ipaddr(dptr
+ matchoff
, NULL
, &ipaddr
,
440 dptr
+ datalen
) < 0) {
444 if (ct_sip_get_info(dptr
, datalen
, &matchoff
, &matchlen
,
447 port
= simple_strtoul(dptr
+ matchoff
, NULL
, 10);
452 ret
= set_expected_rtp(pskb
, ct
, ctinfo
,
460 static struct ip_conntrack_helper sip
[MAX_PORTS
];
461 static char sip_names
[MAX_PORTS
][10];
463 static void fini(void)
466 for (i
= 0; i
< ports_c
; i
++) {
467 DEBUGP("unregistering helper for port %d\n", ports
[i
]);
468 ip_conntrack_helper_unregister(&sip
[i
]);
472 static int __init
init(void)
478 ports
[ports_c
++] = SIP_PORT
;
480 for (i
= 0; i
< ports_c
; i
++) {
481 /* Create helper structure */
482 memset(&sip
[i
], 0, sizeof(struct ip_conntrack_helper
));
484 sip
[i
].tuple
.dst
.protonum
= IPPROTO_UDP
;
485 sip
[i
].tuple
.src
.u
.udp
.port
= htons(ports
[i
]);
486 sip
[i
].mask
.src
.u
.udp
.port
= htons(0xFFFF);
487 sip
[i
].mask
.dst
.protonum
= 0xFF;
488 sip
[i
].max_expected
= 2;
489 sip
[i
].timeout
= 3 * 60; /* 3 minutes */
490 sip
[i
].me
= THIS_MODULE
;
491 sip
[i
].help
= sip_help
;
493 tmpname
= &sip_names
[i
][0];
494 if (ports
[i
] == SIP_PORT
)
495 sprintf(tmpname
, "sip");
497 sprintf(tmpname
, "sip-%d", i
);
498 sip
[i
].name
= tmpname
;
500 DEBUGP("port #%d: %d\n", i
, ports
[i
]);
502 ret
= ip_conntrack_helper_register(&sip
[i
]);
504 printk("ERROR registering helper for port %d\n",