SIP/SDP conntrack and NAT helper
PATCH 1/1 SIP/SDP conntrack and NAT helper
Hi,
these patches adds conntrack and NAT support for SIP/SDP protocol.
Currently it only support audio sessions, and it's status is "works for me!"
There's some work to be done, but some help with testing, or some feedback about this would be great meanwhile.
These patches are against lastest revision of pmo-ng, and only works in new > 2.6.11 nat framework.
Regards,
Signature · 1 line
Christian Hentschel <[email protected]>
linux-2.6.11/include/linux/netfilter_ipv4/ip_conntrack_sip.h +78 -0
@@ -0,0 +1,78 @@ +#ifndef __IP_CONNTRACK_SIP_H__ +#define __IP_CONNTRACK_SIP_H__ +/* SIP tracking. */ + +#ifdef __KERNEL__ + +#define SIP_PORT 5060 +#define SIP_TIMEOUT 3600 + +#define POS_VIA 0 +#define POS_CONTACT 1 +#define POS_CONTENT 2 +#define POS_MEDIA 3 +#define POS_OWNER 4 +#define POS_CONECTION 5 +#define POS_REQ_HEADER 6 +#define POS_SDP_HEADER 7 + +struct header_nfo { +const char *lname; +size_t lnlen; +const char *sname; +size_t snlen; +const char *ln_str; +size_t ln_strlen; +int (*match_len)(const char *,const char *,int *); + +}; + +extern unsigned int (*ip_nat_sip_hook)(struct sk_buff **pskb, +enum ip_conntrack_info ctinfo, +struct ip_conntrack *ct, +char **dptr); + +/* For NAT to hook in when on expect. */ +extern unsigned int (*ip_nat_sdp_hook)(struct sk_buff **pskb, +enum ip_conntrack_info ctinfo, +struct ip_conntrack_expect *exp, +char *dptr); + +extern int get_info (char *dptr,int dlen,int *matchoff,int *matchlen, +struct header_nfo *hnfo); + + +#endif /* __KERNEL__ */ + +/* Linear string search, case sensitive. */ +static __inline__char *str_search (const char *needle,char *haystack, +int needle_len,int haystack_len) +{ +char *limit = haystack + (haystack_len - needle_len); + +while (haystack <= limit) { +if (memcmp (haystack, needle, needle_len) ==0 ) +return haystack; + haystack++; + } +return NULL ; +} + +/* get line lenght until first CR or LF seen. */ +static __inline__int get_lnlen (char *line,char *limit) +{ +char *k = line; + +while ((line <= limit) && (*line =='\r' || *line =='\n' )) + line++; + +while (line <= limit) { +if (*line =='\r' || *line =='\n' ) +break ; + line++; + } +return line - k; +} + + +#endif /* __IP_CONNTRACK_SIP_H__ */
linux-2.6.11/net/ipv4/netfilter/Makefile.ladd +6 -0
@@ -0,0 +1,6 @@ +obj-$(CONFIG_IP_NF_CONNTRACK) += ip_conntrack.o + +# SIP support +obj-$(CONFIG_IP_NF_SIP) += ip_conntrack_sip.o +obj-$(CONFIG_IP_NF_NAT_SIP) += ip_nat_sip.o +
linux-2.6.11/net/ipv4/netfilter/Kconfig.ladd +18 -0
@@ -0,0 +1,18 @@ +config IP_NF_NAT_SIP + tristate + depends on IP_NF_CONNTRACK!=n && IP_NF_NAT!=n + default IP_NF_NAT if IP_NF_SIP=y + default m if IP_NF_SIP=m + +config IP_NF_SIP + tristate 'SIP support' + depends on IP_NF_CONNTRACK + help + SIP is an application-layer control protocol that can establish, + modify, and terminate multimedia sessions (conferences) such as + Internet telephony calls. With the ip_conntrack_sip and + the ip_nat_sip modules you can support the protocol on a connection + tracking/NATing firewall. + + If you want to compile it as a module, say 'M' here and read + Documentation/modules.txt. If unsure, say 'N'.
linux-2.6.11/net/ipv4/netfilter/ip_conntrack_sip.c +404 -0
404 lines not on this page · raw message
linux-2.6.11/net/ipv4/netfilter/ip_nat_sip.c +253 -0
253 lines not on this page · raw message
help +7 -0
@@ -0,0 +1,7 @@ +This adds CONFIG_IP_NF_SIP: SIP support module for netfilter +connection tracking and NAT. + +The SIP conntrack/NAT modules support the connection tracking/NATing of +the data streams requested on the dynamic RTP/RTCP ports, as well as mangling +of SIP requests/responses. +
info +5 -0
@@ -0,0 +1,5 @@ +Title: SIP connection tracking and NAT helper +Author: Christian Hentschel <[email protected]> +Status: Alpha +Repository: extra +Requires: linux-2.6 >= 2.6.11
Hi Christian,
Quoted 1 line
these patches adds conntrack and NAT support for SIP/SDP protocol.
Here's some critique based on a dumb scan through your patch. I'm not competent to comment on the code content, right now. So this mail only covers superficial, stylistic things.
First, there is user namespace pollution in include/linux/netfilter_ipv4/ip_conntrack_sip.h
where you define two static inline functions str_search and get_lnlen.
Second, in the same file and elsewhere, get_info seems inappropriately named for a kernel symbol. Also struct header_nfo.
+struct header_nfo headers[] = {
Ick! Sure that no other in-kernel code will ever declare a globally visible symbol named 'headers'? What if Linus finds a use for this nice name? :)
[...] Could you stick to some common prefix for all visible symbols and macros in include files / cross dependancies, please?
Hope this helps you along somehow.
I'll see how I can help in testing. Needs thinking. I don't use SIP regularly.
And please don't get this wrong. This mail is meant well :)
best regards
Patrick
Hi Patrick,
Your feedback is very wellcome :). Here's the patch with the things u
pointed fixed.
Regards,
Quoted 14 lines · Patrick Schaaf
two static inline functions
str_searchandget_lnlen.Second, in the same file and elsewhere,
get_infoseems inappropriately named for a kernel symbol. Also structheader_nfo.+struct
header_nfoheaders[] = {
Ick! Sure that no other in-kernel code will ever declare a globally visible
symbol named 'headers'? What if Linus finds a use for this nice name? :)[...] Could you stick to some common prefix for all visible symbols and macros
in include files / cross dependancies, please?
Signature · 1 line
Christian Hentschel <[email protected]>
linux-2.6.11/include/linux/netfilter_ipv4/ip_conntrack_sip.h +75 -0
@@ -0,0 +1,75 @@ +#ifndef __IP_CONNTRACK_SIP_H__ +#define __IP_CONNTRACK_SIP_H__ +/* SIP tracking. */ + +#ifdef __KERNEL__ + +#define SIP_PORT 5060 +#define SIP_TIMEOUT 3600 + +#define POS_VIA 0 +#define POS_CONTACT 1 +#define POS_CONTENT 2 +#define POS_MEDIA 3 +#define POS_OWNER 4 +#define POS_CONECTION 5 +#define POS_REQ_HEADER 6 +#define POS_SDP_HEADER 7 + +struct sip_header_nfo { +const char *lname; +size_t lnlen; +const char *sname; +size_t snlen; +const char *ln_str; +size_t ln_strlen; +int (*match_len)(const char *,const char *,int *); + +}; + +extern unsigned int (*ip_nat_sip_hook)(struct sk_buff **pskb, +enum ip_conntrack_info ctinfo, +struct ip_conntrack *ct, +char **dptr); + +/* For NAT to hook in when on expect. */ +extern unsigned int (*ip_nat_sdp_hook)(struct sk_buff **pskb, +enum ip_conntrack_info ctinfo, +struct ip_conntrack_expect *exp, +char *dptr); + +extern int ct_sip_get_info (char *dptr,int dlen,int *matchoff, +int *matchlen,struct sip_header_nfo *hnfo); + +/* get line lenght until first CR or LF seen. */ +static __inline__int ct_sip_lnlen (char *line,char *limit) +{ +char *k = line; + +while ((line <= limit) && (*line =='\r' || *line =='\n' )) + line++; + +while (line <= limit) { +if (*line =='\r' || *line =='\n' ) +break ; + line++; + } +return line - k; +} + +/* Linear string search, case sensitive. */ +static __inline__char *ct_sip_search (const char *needle,char *haystack, +int needle_len,int haystack_len) +{ +char *limit = haystack + (haystack_len - needle_len); + +while (haystack <= limit) { +if (memcmp (haystack, needle, needle_len) ==0 ) +return haystack; + haystack++; + } +return NULL ; +} +#endif /* __KERNEL__ */ + +#endif /* __IP_CONNTRACK_SIP_H__ */
linux-2.6.11/net/ipv4/netfilter/Makefile.ladd +6 -0
@@ -0,0 +1,6 @@ +obj-$(CONFIG_IP_NF_CONNTRACK) += ip_conntrack.o + +# SIP support +obj-$(CONFIG_IP_NF_SIP) += ip_conntrack_sip.o +obj-$(CONFIG_IP_NF_NAT_SIP) += ip_nat_sip.o +
linux-2.6.11/net/ipv4/netfilter/Kconfig.ladd +18 -0
@@ -0,0 +1,18 @@ +config IP_NF_NAT_SIP + tristate + depends on IP_NF_CONNTRACK!=n && IP_NF_NAT!=n + default IP_NF_NAT if IP_NF_SIP=y + default m if IP_NF_SIP=m + +config IP_NF_SIP + tristate 'SIP support' + depends on IP_NF_CONNTRACK + help + SIP is an application-layer control protocol that can establish, + modify, and terminate multimedia sessions (conferences) such as + Internet telephony calls. With the ip_conntrack_sip and + the ip_nat_sip modules you can support the protocol on a connection + tracking/NATing firewall. + + If you want to compile it as a module, say 'M' here and read + Documentation/modules.txt. If unsure, say 'N'.
linux-2.6.11/net/ipv4/netfilter/ip_conntrack_sip.c +405 -0
405 lines not on this page · raw message
linux-2.6.11/net/ipv4/netfilter/ip_nat_sip.c +253 -0
253 lines not on this page · raw message
help +7 -0
@@ -0,0 +1,7 @@ +This adds CONFIG_IP_NF_SIP: SIP support module for netfilter +connection tracking and NAT. + +The SIP conntrack/NAT modules support the connection tracking/NATing of +the data streams requested on the dynamic RTP/RTCP ports, as well as mangling +of SIP requests/responses. +
info +5 -0
@@ -0,0 +1,5 @@ +Title: SIP connection tracking and NAT helper +Author: Christian Hentschel <[email protected]> +Status: Alpha +Repository: extra +Requires: linux-2.6 >= 2.6.11
Hi Christian,
good to see someone is working on the "other" VoIP protocol (I'm doing H.323) - I just read your code. Although I havn't yet fully understood everything, I have some advices for you:
* haystack should be const, too:
static __inline__ char *ct_sip_search(const char *needle,
char *haystack,
* in digits_len, 'shift' is unused.
* parse_ipaddr(): don't cast (const char*) to (char*) - endp should be "const char**". same in epaddr_len.
* in ip_nat_sip.c, you should not parse the packet again - this has already been done in in_conntrack_sip.c. sip_help() should pass the offsets it found over to ip_nat_sip() - see the other conntrack helpers, they all have "unsigned int matchoff, unsigned int matchlen" as parameter
* never use "signed int" for lengths. Although it may be unlikely that 'len' can overflow in digits_len(), it is a matter of good style not to use signed integers for this stuff. Yesterday, Paul Starzetz released an advisory for a local hole in the Linux kernel regarding signed integers. Last year, there were a bunch of vulnerabilities due to signed integers...
* I'm not sure, but I believe there is an off-by-one error in your code, regarding the 'limit' parameter: in ct_sip_get_info you set 'limit' to 'dptr + (dlen - hnfo->lnlen)', i.e. *limit is an invalid dereference. In contrast to this, in the called functions, you compare 'dptr <= limit'.
* again, I'm not sure: in ct_sip_get_info(), you use strncmp() to work with dptr, implying that dptr is a null-terminated string. You should check the length of dptr, not that of hnfo->lname, which _is_ a null terminated string. I believe, hnfo->lnlen and hnfo->snlen are superfluous for this reason.
* code style: you should correct the indent in parameter lists:
static int sip_help(struct sk_buff **pskb,
struct ip_conntrack *ct,
change to:
static int sip_help(struct sk_buff **pskb,
struct ip_conntrack *ct,
code style again:
if (ip_nat_sip_hook) {
if (!ip_nat_sip_hook(pskb, ctinfo, ct, &dptr)) {
/* ... */
}
}to:
if (ip_nat_sip_hook &&
!ip_nat_sip_hook(pskb, ctinfo, ct, &dptr)) {
/* ... */
}Max
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 4 lines · Christian Hentschel
Quoted 3 lines
*
parse_ipaddr(): don't cast (const char*) to (char*) - endp should be "const char**". same inepaddr_len.Why?
Casting const to non-const is bad code style. If you're declaring something const, there's a reason, therefore it should stay const. If you have to deconst something, there is something really wrong in your code. I pointed out to you what's wrong: there are functions which want non-const parameters, where const suffices. Change that, remove the cast, everything ok.
Quoted 9 lines
Quoted 7 lines
* in
ip_nat_sip.c, you should not parse the packet again - this has already been done inin_conntrack_sip.c.sip_help()should pass the offsets it found over toip_nat_sip()- see the other conntrack helpers, they all have "unsigned int matchoff, unsigned int matchlen" as parameterI think it's kind of impossible with SIP. But if u have an idea, please let me know.
I havn't read the code that much, and I don't know SIP. But I'll look at it later.
Quoted 7 lines
Quoted 5 lines
* again, I'm not sure: in
ct_sip_get_info(), you usestrncmp()to work with dptr, implying that dptr is a null-terminated string. You should check the length of dptr, not that ofhnfo->lname, which _is_ a null terminated string. I believe,hnfo->lnlenandhnfo->snlenare superfluous for this reason.You r wrong. dptr is not null terminated.
Exactly that's my point - dptr is *not* null terminated, that is why you must not use it with strncmp(), unless you give strncmp() the correct limit, which you do not.
if ((strncmp(dptr, hnfo->lname, hnfo->lnlen) != 0) &&
What if length_of(dptr) happens to be smaller than hnfo->lnlen? strncmp() will violate dptr's boundary => kernel crashing.
Quoted 3 lines
Thanks Max, i'll post some patches fixing and changeing some things after i get the code into pom-ng. Some help with testing would be highly appreciated. I'll try to add some nfsim testsuite too.
The netfilter people are quite busy, don't wait for them.
Max
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 9 lines · Christian Hentschel
Quoted 3 lines
What if
length_of(dptr)happens to be smaller thanhnfo->lnlen?strncmp()will violate dptr's boundary => kernel crashing.Again, you are wrong.
Look at thewhile()2 lines before and you will see that there's where the limit is.
Astrncmp()here is the same as callingmemcmp(), may be u got confused because of this.
I see. The 'limit' variable guarantees that length_of(dptr) is always equal or bigger than hnfo->lnlen, although you do not explicitly check the same for hnfo->snlen. In ct_sip_hdrs, hnfo->lnlen>=nfo->snlen is always true, so this is not a real problem here.
However, this is dangerous when someone changes ct_sip_hdrs later and does not notice that ct_sip_get_info() may produce a buffer overflow in this case (the assumption is not documented there). I'd rather produce code without this danger and lose some performance (which is so small you can't even measure it).
But I insist that ct_sip_get_info() is bugged - e.h. a "\r\nl:" at the end of the packet is never found - that would exceed 'limit', which is so small because "Content-Length:" is such a big string.
btw. why don't you use memcmp() here? In strncmp(), the known size of one of the strings as parameter is redundant. You'd better pass limit-dptr (rest length of dptr) here, and set 'limit' to dptr+dlen. This would be safe and would parse correctly. In addition, you could remove hnfo->lnlen and hnfo->snlen (which looks ugly in the initializers anyway).
Max
Quoted 11 lines · Christian Hentschel
Hi,
these patches adds conntrack and NAT support for SIP/SDP protocol.
Currently it only support audio sessions, and it's status is "works for me!"There's some work to be done, but some help with testing, or some feedback about this would be great meanwhile.
These patches are against lastest revision of pmo-ng, and only works in new > 2.6.11 nat framework.
If it is still of interest, I have found a couple of cases where the current sip_conntrack appears to fall short. I am still using the 2.6.10 kernel, and the older sip_conntrack code, but I compared it to the latest version I could find of sip_copnntrack for 2.6.11, and the same should apply.
In our network, we use SNOM 190 phones. When they register, they negotiate to listen on a dynamic port number (NOT 5060) - The conntrack code is only looking for packets with a SOURCE port of 5060, so when the SNOM 190 initiates the INVITE, the destination port is 5060, but the source port is not. Of course conntrack does not see this, and the RTP packets are not allowed.
This currently only seems to happen with either an IAX forwarded call, or a transferred call. I am still experimenting.
I am sure there is an "elegant" way to cater for this, but my kludged attempts were a bit of a failure :)
Kind regards,
Steve
Hi,
Quoted 6 lines
In our network, we use SNOM 190 phones. When they register, they negotiate to listen on a dynamic port number (NOT 5060) - The conntrack code is only looking for packets with a SOURCE port of 5060, so when the SNOM 190 initiates the INVITE, the destination port is 5060, but the source port is not. Of course conntrack does not see this, and the RTP packets are not allowed.
mmm.. it should be looking for dst 5060. Anyways, since i don't have those snom, can u send me some captures of this situation?. ethereal would be great.
Please, use the lastest sip conntrack patch though, 2.6.10 was an early code.
Best regards
Signature · 3 lines
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian W. Kernighan