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
{
67 int (*match_len
)(const char *, const char *, int *);
70 static struct sip_header_nfo ct_sip_hdrs
[] = {
71 [POS_REQ_HEADER
] = { /* SIP Requests headers */
73 .lnlen
= sizeof("sip:") - 1,
75 .snlen
= sizeof("sip:") - 1, /* yes, i know.. ;) */
77 .ln_strlen
= sizeof("@") - 1,
78 .match_len
= epaddr_len
80 [POS_VIA
] = { /* SIP Via header */
82 .lnlen
= sizeof("Via:") - 1,
84 .snlen
= sizeof("\r\nv:") - 1, /* rfc3261 "\r\n" */
86 .ln_strlen
= sizeof("UDP ") - 1,
87 .match_len
= epaddr_len
,
89 [POS_CONTACT
] = { /* SIP Contact header */
91 .lnlen
= sizeof("Contact:") - 1,
93 .snlen
= sizeof("\r\nm:") - 1,
95 .ln_strlen
= sizeof("sip:") - 1,
96 .match_len
= skp_epaddr_len
98 [POS_CONTENT
] = { /* SIP Content length header */
99 .lname
= "Content-Length:",
100 .lnlen
= sizeof("Content-Length:") - 1,
102 .snlen
= sizeof("\r\nl:") - 1,
104 .ln_strlen
= sizeof(":") - 1,
105 .match_len
= skp_digits_len
107 [POS_MEDIA
] = { /* SDP media info */
109 .lnlen
= sizeof("\nm=") - 1,
111 .snlen
= sizeof("\rm=") - 1,
113 .ln_strlen
= sizeof("audio ") - 1,
114 .match_len
= digits_len
116 [POS_OWNER
] = { /* SDP owner address*/
118 .lnlen
= sizeof("\no=") - 1,
120 .snlen
= sizeof("\ro=") - 1,
122 .ln_strlen
= sizeof("IN IP4 ") - 1,
123 .match_len
= epaddr_len
125 [POS_CONNECTION
] = { /* SDP connection info */
127 .lnlen
= sizeof("\nc=") - 1,
129 .snlen
= sizeof("\rc=") - 1,
131 .ln_strlen
= sizeof("IN IP4 ") - 1,
132 .match_len
= epaddr_len
134 [POS_SDP_HEADER
] = { /* SDP version header */
136 .lnlen
= sizeof("\nv=") - 1,
138 .snlen
= sizeof("\rv=") - 1,
140 .ln_strlen
= sizeof("=") - 1,
141 .match_len
= digits_len
145 /* get line lenght until first CR or LF seen. */
146 int ct_sip_lnlen(const char *line
, const char *limit
)
148 const char *k
= line
;
150 while ((line
<= limit
) && (*line
== '\r' || *line
== '\n'))
153 while (line
<= limit
) {
154 if (*line
== '\r' || *line
== '\n')
160 EXPORT_SYMBOL_GPL(ct_sip_lnlen
);
162 /* Linear string search, case sensitive. */
163 const char *ct_sip_search(const char *needle
, const char *haystack
,
164 size_t needle_len
, size_t haystack_len
)
166 const char *limit
= haystack
+ (haystack_len
- needle_len
);
168 while (haystack
<= limit
) {
169 if (memcmp(haystack
, needle
, needle_len
) == 0)
175 EXPORT_SYMBOL_GPL(ct_sip_search
);
177 static int digits_len(const char *dptr
, const char *limit
, int *shift
)
180 while (dptr
<= limit
&& isdigit(*dptr
)) {
187 /* get digits lenght, skiping blank spaces. */
188 static int skp_digits_len(const char *dptr
, const char *limit
, int *shift
)
190 for (; dptr
<= limit
&& *dptr
== ' '; dptr
++)
193 return digits_len(dptr
, limit
, shift
);
196 /* Simple ipaddr parser.. */
197 static int parse_ipaddr(const char *cp
, const char **endp
,
198 __be32
*ipaddr
, const char *limit
)
200 unsigned long int val
;
203 for (i
= 0, *ipaddr
= 0; cp
<= limit
&& i
< 4; i
++) {
208 val
= simple_strtoul(cp
, (char **)&cp
, 10);
212 ((u_int8_t
*)ipaddr
)[i
] = val
;
228 /* skip ip address. returns it lenght. */
229 static int epaddr_len(const char *dptr
, const char *limit
, int *shift
)
231 const char *aux
= dptr
;
234 if (parse_ipaddr(dptr
, &dptr
, &ip
, limit
) < 0) {
235 DEBUGP("ip: %s parse failed.!\n", dptr
);
242 dptr
+= digits_len(dptr
, limit
, shift
);
247 /* get address length, skiping user info. */
248 static int skp_epaddr_len(const char *dptr
, const char *limit
, int *shift
)
252 for (; dptr
<= limit
&& *dptr
!= '@'; dptr
++)
261 return epaddr_len(dptr
, limit
, shift
);
264 /* Returns 0 if not found, -1 error parsing. */
265 int ct_sip_get_info(const char *dptr
, size_t dlen
,
266 unsigned int *matchoff
,
267 unsigned int *matchlen
,
268 enum sip_header_pos pos
)
270 struct sip_header_nfo
*hnfo
= &ct_sip_hdrs
[pos
];
271 const char *limit
, *aux
, *k
= dptr
;
274 limit
= dptr
+ (dlen
- hnfo
->lnlen
);
276 while (dptr
<= limit
) {
277 if ((strncmp(dptr
, hnfo
->lname
, hnfo
->lnlen
) != 0) &&
278 (strncmp(dptr
, hnfo
->sname
, hnfo
->snlen
) != 0)) {
282 aux
= ct_sip_search(hnfo
->ln_str
, dptr
, hnfo
->ln_strlen
,
283 ct_sip_lnlen(dptr
, limit
));
285 DEBUGP("'%s' not found in '%s'.\n", hnfo
->ln_str
,
289 aux
+= hnfo
->ln_strlen
;
291 *matchlen
= hnfo
->match_len(aux
, limit
, &shift
);
295 *matchoff
= (aux
- k
) + shift
;
297 DEBUGP("%s match succeeded! - len: %u\n", hnfo
->lname
,
301 DEBUGP("%s header not found.\n", hnfo
->lname
);
304 EXPORT_SYMBOL_GPL(ct_sip_get_info
);
306 static int set_expected_rtp(struct sk_buff
**pskb
,
307 struct ip_conntrack
*ct
,
308 enum ip_conntrack_info ctinfo
,
309 __be32 ipaddr
, u_int16_t port
,
312 struct ip_conntrack_expect
*exp
;
313 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
315 typeof(ip_nat_sdp_hook
) ip_nat_sdp
;
317 exp
= ip_conntrack_expect_alloc(ct
);
321 exp
->tuple
.src
.ip
= ct
->tuplehash
[!dir
].tuple
.src
.ip
;
322 exp
->tuple
.src
.u
.udp
.port
= 0;
323 exp
->tuple
.dst
.ip
= ipaddr
;
324 exp
->tuple
.dst
.u
.udp
.port
= htons(port
);
325 exp
->tuple
.dst
.protonum
= IPPROTO_UDP
;
327 exp
->mask
.src
.ip
= htonl(0xFFFFFFFF);
328 exp
->mask
.src
.u
.udp
.port
= 0;
329 exp
->mask
.dst
.ip
= htonl(0xFFFFFFFF);
330 exp
->mask
.dst
.u
.udp
.port
= htons(0xFFFF);
331 exp
->mask
.dst
.protonum
= 0xFF;
333 exp
->expectfn
= NULL
;
336 ip_nat_sdp
= rcu_dereference(ip_nat_sdp_hook
);
338 ret
= ip_nat_sdp(pskb
, ctinfo
, exp
, dptr
);
340 if (ip_conntrack_expect_related(exp
) != 0)
345 ip_conntrack_expect_put(exp
);
350 static int sip_help(struct sk_buff
**pskb
,
351 struct ip_conntrack
*ct
,
352 enum ip_conntrack_info ctinfo
)
354 unsigned int dataoff
, datalen
;
357 int matchoff
, matchlen
;
360 typeof(ip_nat_sip_hook
) ip_nat_sip
;
363 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
364 if (dataoff
>= (*pskb
)->len
) {
365 DEBUGP("skb->len = %u\n", (*pskb
)->len
);
369 ip_ct_refresh(ct
, *pskb
, sip_timeout
* HZ
);
371 if (!skb_is_nonlinear(*pskb
))
372 dptr
= (*pskb
)->data
+ dataoff
;
374 DEBUGP("Copy of skbuff not supported yet.\n");
378 ip_nat_sip
= rcu_dereference(ip_nat_sip_hook
);
380 if (!ip_nat_sip(pskb
, ctinfo
, ct
, &dptr
)) {
386 /* After this point NAT, could have mangled skb, so
387 we need to recalculate payload lenght. */
388 datalen
= (*pskb
)->len
- dataoff
;
390 if (datalen
< (sizeof("SIP/2.0 200") - 1))
393 /* RTP info only in some SDP pkts */
394 if (memcmp(dptr
, "INVITE", sizeof("INVITE") - 1) != 0 &&
395 memcmp(dptr
, "SIP/2.0 200", sizeof("SIP/2.0 200") - 1) != 0) {
398 /* Get ip and port address from SDP packet. */
399 if (ct_sip_get_info(dptr
, datalen
, &matchoff
, &matchlen
,
400 POS_CONNECTION
) > 0) {
402 /* We'll drop only if there are parse problems. */
403 if (parse_ipaddr(dptr
+ matchoff
, NULL
, &ipaddr
,
404 dptr
+ datalen
) < 0) {
408 if (ct_sip_get_info(dptr
, datalen
, &matchoff
, &matchlen
,
411 port
= simple_strtoul(dptr
+ matchoff
, NULL
, 10);
416 ret
= set_expected_rtp(pskb
, ct
, ctinfo
,
424 static struct ip_conntrack_helper sip
[MAX_PORTS
];
425 static char sip_names
[MAX_PORTS
][10];
427 static void fini(void)
430 for (i
= 0; i
< ports_c
; i
++) {
431 DEBUGP("unregistering helper for port %d\n", ports
[i
]);
432 ip_conntrack_helper_unregister(&sip
[i
]);
436 static int __init
init(void)
442 ports
[ports_c
++] = SIP_PORT
;
444 for (i
= 0; i
< ports_c
; i
++) {
445 /* Create helper structure */
446 memset(&sip
[i
], 0, sizeof(struct ip_conntrack_helper
));
448 sip
[i
].tuple
.dst
.protonum
= IPPROTO_UDP
;
449 sip
[i
].tuple
.src
.u
.udp
.port
= htons(ports
[i
]);
450 sip
[i
].mask
.src
.u
.udp
.port
= htons(0xFFFF);
451 sip
[i
].mask
.dst
.protonum
= 0xFF;
452 sip
[i
].max_expected
= 2;
453 sip
[i
].timeout
= 3 * 60; /* 3 minutes */
454 sip
[i
].me
= THIS_MODULE
;
455 sip
[i
].help
= sip_help
;
457 tmpname
= &sip_names
[i
][0];
458 if (ports
[i
] == SIP_PORT
)
459 sprintf(tmpname
, "sip");
461 sprintf(tmpname
, "sip-%d", i
);
462 sip
[i
].name
= tmpname
;
464 DEBUGP("port #%d: %d\n", i
, ports
[i
]);
466 ret
= ip_conntrack_helper_register(&sip
[i
]);
468 printk("ERROR registering helper for port %d\n",