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 int ct_sip_get_info(const char *dptr
, size_t dlen
,
56 unsigned int *matchoff
,
57 unsigned int *matchlen
,
58 struct sip_header_nfo
*hnfo
);
59 EXPORT_SYMBOL_GPL(ct_sip_get_info
);
62 static int digits_len(const char *dptr
, const char *limit
, int *shift
);
63 static int epaddr_len(const char *dptr
, const char *limit
, int *shift
);
64 static int skp_digits_len(const char *dptr
, const char *limit
, int *shift
);
65 static int skp_epaddr_len(const char *dptr
, const char *limit
, int *shift
);
67 struct sip_header_nfo ct_sip_hdrs
[] = {
70 .lnlen
= sizeof("Via:") - 1,
72 .snlen
= sizeof("\r\nv:") - 1, /* rfc3261 "\r\n" */
74 .ln_strlen
= sizeof("UDP ") - 1,
75 .match_len
= epaddr_len
,
77 { /* Contact header */
79 .lnlen
= sizeof("Contact:") - 1,
81 .snlen
= sizeof("\r\nm:") - 1,
83 .ln_strlen
= sizeof("sip:") - 1,
84 .match_len
= skp_epaddr_len
86 { /* Content length header */
87 .lname
= "Content-Length:",
88 .lnlen
= sizeof("Content-Length:") - 1,
90 .snlen
= sizeof("\r\nl:") - 1,
92 .ln_strlen
= sizeof(":") - 1,
93 .match_len
= skp_digits_len
95 { /* SDP media info */
97 .lnlen
= sizeof("\nm=") - 1,
99 .snlen
= sizeof("\rm=") - 1,
101 .ln_strlen
= sizeof("audio ") - 1,
102 .match_len
= digits_len
104 { /* SDP owner address*/
106 .lnlen
= sizeof("\no=") - 1,
108 .snlen
= sizeof("\ro=") - 1,
110 .ln_strlen
= sizeof("IN IP4 ") - 1,
111 .match_len
= epaddr_len
113 { /* SDP connection info */
115 .lnlen
= sizeof("\nc=") - 1,
117 .snlen
= sizeof("\rc=") - 1,
119 .ln_strlen
= sizeof("IN IP4 ") - 1,
120 .match_len
= epaddr_len
122 { /* Requests headers */
124 .lnlen
= sizeof("sip:") - 1,
126 .snlen
= sizeof("sip:") - 1, /* yes, i know.. ;) */
128 .ln_strlen
= sizeof("@") - 1,
129 .match_len
= epaddr_len
131 { /* SDP version header */
133 .lnlen
= sizeof("\nv=") - 1,
135 .snlen
= sizeof("\rv=") - 1,
137 .ln_strlen
= sizeof("=") - 1,
138 .match_len
= digits_len
141 EXPORT_SYMBOL_GPL(ct_sip_hdrs
);
143 /* get line lenght until first CR or LF seen. */
144 int ct_sip_lnlen(const char *line
, const char *limit
)
146 const char *k
= line
;
148 while ((line
<= limit
) && (*line
== '\r' || *line
== '\n'))
151 while (line
<= limit
) {
152 if (*line
== '\r' || *line
== '\n')
158 EXPORT_SYMBOL_GPL(ct_sip_lnlen
);
160 /* Linear string search, case sensitive. */
161 const char *ct_sip_search(const char *needle
, const char *haystack
,
162 size_t needle_len
, size_t haystack_len
)
164 const char *limit
= haystack
+ (haystack_len
- needle_len
);
166 while (haystack
<= limit
) {
167 if (memcmp(haystack
, needle
, needle_len
) == 0)
173 EXPORT_SYMBOL_GPL(ct_sip_search
);
175 static int digits_len(const char *dptr
, const char *limit
, int *shift
)
178 while (dptr
<= limit
&& isdigit(*dptr
)) {
185 /* get digits lenght, skiping blank spaces. */
186 static int skp_digits_len(const char *dptr
, const char *limit
, int *shift
)
188 for (; dptr
<= limit
&& *dptr
== ' '; dptr
++)
191 return digits_len(dptr
, limit
, shift
);
194 /* Simple ipaddr parser.. */
195 static int parse_ipaddr(const char *cp
, const char **endp
,
196 u_int32_t
*ipaddr
, const char *limit
)
198 unsigned long int val
;
201 for (i
= 0, *ipaddr
= 0; cp
<= limit
&& i
< 4; i
++) {
206 val
= simple_strtoul(cp
, (char **)&cp
, 10);
210 ((u_int8_t
*)ipaddr
)[i
] = val
;
226 /* skip ip address. returns it lenght. */
227 static int epaddr_len(const char *dptr
, const char *limit
, int *shift
)
229 const char *aux
= dptr
;
232 if (parse_ipaddr(dptr
, &dptr
, &ip
, limit
) < 0) {
233 DEBUGP("ip: %s parse failed.!\n", dptr
);
240 dptr
+= digits_len(dptr
, limit
, shift
);
245 /* get address length, skiping user info. */
246 static int skp_epaddr_len(const char *dptr
, const char *limit
, int *shift
)
250 for (; dptr
<= limit
&& *dptr
!= '@'; dptr
++)
259 return epaddr_len(dptr
, limit
, shift
);
262 /* Returns 0 if not found, -1 error parsing. */
263 int ct_sip_get_info(const char *dptr
, size_t dlen
,
264 unsigned int *matchoff
,
265 unsigned int *matchlen
,
266 struct sip_header_nfo
*hnfo
)
268 const char *limit
, *aux
, *k
= dptr
;
271 limit
= dptr
+ (dlen
- hnfo
->lnlen
);
273 while (dptr
<= limit
) {
274 if ((strncmp(dptr
, hnfo
->lname
, hnfo
->lnlen
) != 0) &&
275 (strncmp(dptr
, hnfo
->sname
, hnfo
->snlen
) != 0)) {
279 aux
= ct_sip_search(hnfo
->ln_str
, dptr
, hnfo
->ln_strlen
,
280 ct_sip_lnlen(dptr
, limit
));
282 DEBUGP("'%s' not found in '%s'.\n", hnfo
->ln_str
,
286 aux
+= hnfo
->ln_strlen
;
288 *matchlen
= hnfo
->match_len(aux
, limit
, &shift
);
292 *matchoff
= (aux
- k
) + shift
;
294 DEBUGP("%s match succeeded! - len: %u\n", hnfo
->lname
,
298 DEBUGP("%s header not found.\n", hnfo
->lname
);
302 static int set_expected_rtp(struct sk_buff
**pskb
,
303 struct ip_conntrack
*ct
,
304 enum ip_conntrack_info ctinfo
,
305 u_int32_t ipaddr
, u_int16_t port
,
308 struct ip_conntrack_expect
*exp
;
309 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
312 exp
= ip_conntrack_expect_alloc(ct
);
316 exp
->tuple
.src
.ip
= ct
->tuplehash
[!dir
].tuple
.src
.ip
;
317 exp
->tuple
.src
.u
.udp
.port
= 0;
318 exp
->tuple
.dst
.ip
= ipaddr
;
319 exp
->tuple
.dst
.u
.udp
.port
= htons(port
);
320 exp
->tuple
.dst
.protonum
= IPPROTO_UDP
;
322 exp
->mask
.src
.ip
= 0xFFFFFFFF;
323 exp
->mask
.src
.u
.udp
.port
= 0;
324 exp
->mask
.dst
.ip
= 0xFFFFFFFF;
325 exp
->mask
.dst
.u
.udp
.port
= 0xFFFF;
326 exp
->mask
.dst
.protonum
= 0xFF;
328 exp
->expectfn
= NULL
;
332 ret
= ip_nat_sdp_hook(pskb
, ctinfo
, exp
, dptr
);
334 if (ip_conntrack_expect_related(exp
) != 0)
339 ip_conntrack_expect_put(exp
);
344 static int sip_help(struct sk_buff
**pskb
,
345 struct ip_conntrack
*ct
,
346 enum ip_conntrack_info ctinfo
)
348 unsigned int dataoff
, datalen
;
351 int matchoff
, matchlen
;
356 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
357 if (dataoff
>= (*pskb
)->len
) {
358 DEBUGP("skb->len = %u\n", (*pskb
)->len
);
362 ip_ct_refresh(ct
, *pskb
, sip_timeout
* HZ
);
364 if (!skb_is_nonlinear(*pskb
))
365 dptr
= (*pskb
)->data
+ dataoff
;
367 DEBUGP("Copy of skbuff not supported yet.\n");
371 if (ip_nat_sip_hook
) {
372 if (!ip_nat_sip_hook(pskb
, ctinfo
, ct
, &dptr
)) {
378 /* After this point NAT, could have mangled skb, so
379 we need to recalculate payload lenght. */
380 datalen
= (*pskb
)->len
- dataoff
;
382 if (datalen
< (sizeof("SIP/2.0 200") - 1))
385 /* RTP info only in some SDP pkts */
386 if (memcmp(dptr
, "INVITE", sizeof("INVITE") - 1) != 0 &&
387 memcmp(dptr
, "SIP/2.0 200", sizeof("SIP/2.0 200") - 1) != 0) {
390 /* Get ip and port address from SDP packet. */
391 if (ct_sip_get_info(dptr
, datalen
, &matchoff
, &matchlen
,
392 &ct_sip_hdrs
[POS_CONNECTION
]) > 0) {
394 /* We'll drop only if there are parse problems. */
395 if (parse_ipaddr(dptr
+ matchoff
, NULL
, &ipaddr
,
396 dptr
+ datalen
) < 0) {
400 if (ct_sip_get_info(dptr
, datalen
, &matchoff
, &matchlen
,
401 &ct_sip_hdrs
[POS_MEDIA
]) > 0) {
403 port
= simple_strtoul(dptr
+ matchoff
, NULL
, 10);
408 ret
= set_expected_rtp(pskb
, ct
, ctinfo
,
416 static struct ip_conntrack_helper sip
[MAX_PORTS
];
417 static char sip_names
[MAX_PORTS
][10];
419 static void fini(void)
422 for (i
= 0; i
< ports_c
; i
++) {
423 DEBUGP("unregistering helper for port %d\n", ports
[i
]);
424 ip_conntrack_helper_unregister(&sip
[i
]);
428 static int __init
init(void)
434 ports
[ports_c
++] = SIP_PORT
;
436 for (i
= 0; i
< ports_c
; i
++) {
437 /* Create helper structure */
438 memset(&sip
[i
], 0, sizeof(struct ip_conntrack_helper
));
440 sip
[i
].tuple
.dst
.protonum
= IPPROTO_UDP
;
441 sip
[i
].tuple
.src
.u
.udp
.port
= htons(ports
[i
]);
442 sip
[i
].mask
.src
.u
.udp
.port
= 0xFFFF;
443 sip
[i
].mask
.dst
.protonum
= 0xFF;
444 sip
[i
].max_expected
= 2;
445 sip
[i
].timeout
= 3 * 60; /* 3 minutes */
446 sip
[i
].me
= THIS_MODULE
;
447 sip
[i
].help
= sip_help
;
449 tmpname
= &sip_names
[i
][0];
450 if (ports
[i
] == SIP_PORT
)
451 sprintf(tmpname
, "sip");
453 sprintf(tmpname
, "sip-%d", i
);
454 sip
[i
].name
= tmpname
;
456 DEBUGP("port #%d: %d\n", i
, ports
[i
]);
458 ret
= ip_conntrack_helper_register(&sip
[i
]);
460 printk("ERROR registering helper for port %d\n",