netfilter 00/07: SIP TCP & T.38 support
Subsystems (2)
The attached patches add TCP and T.38 FAX support to the SIP conntrack helper.
The patches are still work-in-progress, but they already work fine in all my tests and I know some people have been waiting for these features, so here's a snapshot of my current tree for anyone interested in testing.
They're based on the nf-next-2.6.git tree, but they also apply to the 2.6.32 release (might not compile though :).
Please report any test results.
include/linux/netfilter/nf_conntrack_sip.h | 19 ++- include/net/netfilter/nf_conntrack.h | 2 +- include/net/netfilter/nf_conntrack_expect.h | 1 + include/net/netfilter/nf_nat_helper.h | 32 ++- net/ipv4/netfilter/nf_nat_helper.c | 39 ++-- net/ipv4/netfilter/nf_nat_sip.c | 158 ++++++++----- net/netfilter/nf_conntrack_expect.c | 9 + net/netfilter/nf_conntrack_sip.c | 333 +++++++++++++++++++++------ 8 files changed, 435 insertions(+), 158 deletions(-)
Patrick McHardy (7):
netfilter: nf_conntrack: show helper and class in /proc/net/nf_conntrack_expect
netfilter: nf_conntrack_sip: fix ct_sip_parse_request() REGISTER request parsing
netfilter: nf_conntrack_sip: pass data offset to NAT functions
netfilter: nf_conntrack_sip: add TCP support
netfilter: nf_nat: support mangling a single TCP packet multiple times
netfilter: nf_nat_sip: add TCP support
netfilter: nf_conntrack_sip: add T.38 FAX support
netfilter: nf_conntrack: show helper and class in /proc/net/nf_conntrack_expect
commit 8a96ae7fda648925254899ad997d5fe3984d3cf6
Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 17:38:16 2010 +0100
netfilter: nf_conntrack: show helper and class in /proc/net/nf_conntrack_expect
Make the output a bit more informative by showing the helper an expectation belongs to and the expectation class.
include/net/netfilter/nf_conntrack_expect.h +1 -0
@@ -66,6 +66,7 @@ static inline struct net *nf_ct_exp_net(struct nf_conntrack_expect *exp)struct nf_conntrack_expect_policy {unsigned int max_expected;unsigned int timeout; +const char *name; };#define NF_CT_EXPECT_CLASS_DEFAULT 0
net/netfilter/nf_conntrack_expect.c +9 -0
@@ -500,6 +500,7 @@ static void exp_seq_stop(struct seq_file *seq, void *v)static int exp_seq_show (struct seq_file *s,void *v) {struct nf_conntrack_expect *expect; +struct nf_conntrack_helper *helper;struct hlist_node *n = v;char *delim ="" ;
@@ -525,6 +526,14 @@ static int exp_seq_show(struct seq_file *s, void *v)if (expect->flags &NF_CT_EXPECT_INACTIVE )seq_printf (s,"%sINACTIVE" , delim); + helper =rcu_dereference (nfct_help (expect->master)->helper); +if (helper) { +seq_printf (s,"%s%s" , expect->flags ?" " :"" , helper->name); +if (helper->expect_policy[expect->class].name) +seq_printf (s,"/%s" , + helper->expect_policy[expect->class].name); + } +return seq_putc (s,'\n' ); }
net/netfilter/nf_conntrack_sip.c +3 -0
@@ -1305,14 +1305,17 @@ static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX +1 ] = { [SIP_EXPECT_SIGNALLING ] = { + .name ="signalling" , .max_expected =1 , .timeout =3 *60 , }, [SIP_EXPECT_AUDIO ] = { + .name ="audio" , .max_expected =2 *IP_CT_DIR_MAX , .timeout =3 *60 , }, [SIP_EXPECT_VIDEO ] = { + .name ="video" , .max_expected =2 *IP_CT_DIR_MAX , .timeout =3 *60 , },
netfilter: nf_conntrack_sip: fix ct_sip_parse_request() REGISTER request parsing
In reply to Patrick McHardycommit 854e013100d88995a6e25896bd426c303c888682
Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 18:23:05 2010 +0100
netfilter: nf_conntrack_sip: fix ct_sip_parse_request() REGISTER request parsing
When requests are parsed, the "sip:" part of the SIP URI should be skipped.
Usually this doesn't matter because address parsing skips forward until after the username part, but in case REGISTER requests it doesn't contain a username and the address can not be parsed.
net/netfilter/nf_conntrack_sip.c +4 -3
@@ -236,12 +236,13 @@ int ct_sip_parse_request(const struct nf_conn *ct,return 0 ;/* Find SIP URI */ - limit -=strlen ("sip:" ); -for (; dptr < limit; dptr++) { +for (; dptr < limit -strlen ("sip:" ); dptr++) {if (*dptr =='\r' || *dptr =='\n' )return -1 ; -if (strnicmp (dptr,"sip:" ,strlen ("sip:" )) ==0 ) +if (strnicmp (dptr,"sip:" ,strlen ("sip:" )) ==0 ) { + dptr +=strlen ("sip:" );break ; + } }if (!skp_epaddr_len (ct, dptr, limit, &shift))return 0 ;
netfilter: nf_conntrack_sip: pass data offset to NAT functions
In reply to Patrick McHardycommit 020393edbcb02e9e7d852d101f6d3871799bfc94
Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 18:32:28 2010 +0100
netfilter: nf_conntrack_sip: pass data offset to NAT functions
When using TCP multiple SIP messages might be present in a single packet. A following patch will parse them by setting the dptr to the beginning of each message. The NAT helper needs to reload the dptr value after mangling the packet however, so it needs to know the offset of the message to the beginning of the packet.
include/linux/netfilter/nf_conntrack_sip.h +10 -4
@@ -34,10 +34,10 @@ struct sdp_media_type {struct sip_handler {const char *method;unsigned int len; -int (*request)(struct sk_buff *skb, +int (*request)(struct sk_buff *skb,unsigned int dataoff,const char **dptr,unsigned int *datalen,unsigned int cseq); -int (*response)(struct sk_buff *skb, +int (*response)(struct sk_buff *skb,unsigned int dataoff,const char **dptr,unsigned int *datalen,unsigned int cseq,unsigned int code); };
@@ -100,33 +100,39 @@ enum sdp_header_types { };extern unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, +unsigned int dataoff,const char **dptr,unsigned int *datalen);extern unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb, +unsigned int dataoff,const char **dptr,unsigned int *datalen,struct nf_conntrack_expect *exp,unsigned int matchoff,unsigned int matchlen);extern unsigned int (*nf_nat_sdp_addr_hook)(struct sk_buff *skb, -const char **dptr,unsigned int dataoff, +const char **dptr,unsigned int *datalen, +unsigned int sdpoff,enum sdp_header_types type,enum sdp_header_types term,const union nf_inet_addr *addr);extern unsigned int (*nf_nat_sdp_port_hook)(struct sk_buff *skb, +unsigned int dataoff,const char **dptr,unsigned int *datalen,unsigned int matchoff,unsigned int matchlen,u_int16_t port);extern unsigned int (*nf_nat_sdp_session_hook)(struct sk_buff *skb, -const char **dptr,unsigned int dataoff, +const char **dptr,unsigned int *datalen, +unsigned int sdpoff,const union nf_inet_addr *addr); extern unsigned int (*nf_nat_sdp_media_hook)(struct sk_buff *skb, + unsigned int dataoff, const char **dptr, unsigned int *datalen, struct nf_conntrack_expect *rtp_exp,
net/ipv4/netfilter/nf_nat_sip.c +51 -50
269 lines not on this page · raw message
net/netfilter/nf_conntrack_sip.c +44 -38
273 lines not on this page · raw message
netfilter: nf_conntrack_sip: add TCP support
In reply to Patrick McHardycommit c6f223918d264c46704c81bb0430915dc5812858
Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 20:12:30 2010 +0100
netfilter: nf_conntrack_sip: add TCP support
SIP over TCP is similar to UDP, except that messages are delimited by Content-Length: headers and multiple messages may appear in one packet.
include/linux/netfilter/nf_conntrack_sip.h +2 -1
@@ -84,7 +84,8 @@ enum sip_header_types {SIP_HDR_FROM ,SIP_HDR_TO ,SIP_HDR_CONTACT , -SIP_HDR_VIA , +SIP_HDR_VIA_UDP , +SIP_HDR_VIA_TCP ,SIP_HDR_EXPIRES , SIP_HDR_CONTENT_LENGTH, };
net/ipv4/netfilter/nf_nat_sip.c +1 -1
@@ -122,7 +122,7 @@ static unsigned int ip_nat_sip(struct sk_buff *skb, unsigned int dataoff,/* Translate topmost Via header and parameters */ if (ct_sip_parse_header_uri (ct, *dptr,NULL , *datalen, -SIP_HDR_VIA ,NULL , &matchoff, &matchlen, +SIP_HDR_VIA_UDP ,NULL , &matchoff, &matchlen, &addr, &port) >0 ) {unsigned int matchend, poff, plen, buflen, n;char buffer[sizeof ("nnn.nnn.nnn.nnn:nnnnn" )];
net/netfilter/nf_conntrack_sip.c +182 -29
329 lines not on this page · raw message
netfilter: nf_nat: support mangling a single TCP packet multiple times
In reply to Patrick McHardycommit ce005af4d5545746ecbf64ddd23e62f52c67bd2b
Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 20:12:30 2010 +0100
netfilter: nf_nat: support mangling a single TCP packet multiple times
nf_nat_mangle_tcp_packet() can currently only handle a single mangling per window because it only maintains two sequence adjustment positions:
the one before the last adjustment and the one after.
This patch makes sequence number adjustment tracking in nf_nat_mangle_tcp_packet() optional and allows a helper to manually update the offsets after the packet has been fully handled.
include/net/netfilter/nf_nat_helper.h +25 -7
@@ -7,13 +7,27 @@struct sk_buff ;/* These return true or false. */ -extern int nf_nat_mangle_tcp_packet (struct sk_buff *skb, -struct nf_conn *ct, -enum ip_conntrack_info ctinfo, -unsigned int match_offset, -unsigned int match_len, -const char *rep_buffer, -unsigned int rep_len); +extern int __nf_nat_mangle_tcp_packet (struct sk_buff *skb, +struct nf_conn *ct, +enum ip_conntrack_info ctinfo, +unsigned int match_offset, +unsigned int match_len, +const char *rep_buffer, +unsigned int rep_len,bool adjust); + +static inline int nf_nat_mangle_tcp_packet (struct sk_buff *skb, +struct nf_conn *ct, +enum ip_conntrack_info ctinfo, +unsigned int match_offset, +unsigned int match_len, +const char *rep_buffer, +unsigned int rep_len) +{ +return __nf_nat_mangle_tcp_packet (skb, ct, ctinfo, + match_offset, match_len, + rep_buffer, rep_len, true); +} + extern int nf_nat_mangle_udp_packet(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info ctinfo,
@@ -21,6 +35,10 @@ extern int nf_nat_mangle_udp_packet(struct sk_buff *skb,unsigned int match_len, const char *rep_buffer, unsigned int rep_len); + +extern void nf_nat_set_seq_adjust (struct nf_conn *ct, +enum ip_conntrack_info ctinfo, +__be32 seq,s16 off);extern int nf_nat_seq_adjust (struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info ctinfo);
net/ipv4/netfilter/nf_nat_helper.c +23 -16
@@ -141,6 +141,17 @@ static int enlarge_skb(struct sk_buff *skb, unsigned int extra)return 1 ; } +void nf_nat_set_seq_adjust (struct nf_conn *ct,enum ip_conntrack_info ctinfo, +__be32 seq,s16 off) +{ +if (!off) +return ; +set_bit (IPS_SEQ_ADJUST_BIT , &ct->status); +adjust_tcp_sequence (ntohl (seq), off, ct, ctinfo); +nf_conntrack_event_cache (IPCT_NATSEQADJ , ct); +} +EXPORT_SYMBOL_GPL (nf_nat_set_seq_adjust); +/* Generic function for mangling variable-length address changes inside * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX * command in FTP).
@@ -149,14 +160,13 @@ static int enlarge_skb(struct sk_buff *skb, unsigned int extra)* skb enlargement, ... * * */ -int -nf_nat_mangle_tcp_packet (struct sk_buff *skb, -struct nf_conn *ct, -enum ip_conntrack_info ctinfo, -unsigned int match_offset, -unsigned int match_len, -const char *rep_buffer, -unsigned int rep_len) +int __nf_nat_mangle_tcp_packet (struct sk_buff *skb, +struct nf_conn *ct, +enum ip_conntrack_info ctinfo, +unsigned int match_offset, +unsigned int match_len, +const char *rep_buffer, +unsigned int rep_len,bool adjust) {struct rtable *rt =skb_rtable (skb);struct iphdr *iph;
@@ -202,16 +212,13 @@ nf_nat_mangle_tcp_packet(struct sk_buff *skb,inet_proto_csum_replace2 (&tcph->check, skb,htons (oldlen),htons (datalen),1 ); -if (rep_len != match_len) { -set_bit (IPS_SEQ_ADJUST_BIT , &ct->status); -adjust_tcp_sequence (ntohl (tcph->seq), - (int )rep_len - (int )match_len, - ct, ctinfo); -nf_conntrack_event_cache (IPCT_NATSEQADJ , ct); - } +if (adjust && rep_len != match_len) +nf_nat_set_seq_adjust (ct, ctinfo, tcph->seq, + (int )rep_len - (int )match_len); +return 1 ; } -EXPORT_SYMBOL (nf_nat_mangle_tcp_packet); +EXPORT_SYMBOL (__nf_nat_mangle_tcp_packet);/* Generic function for mangling variable-length address changes inside * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
netfilter: nf_nat_sip: add TCP support
In reply to Patrick McHardycommit ef7649ffe38affb3c32a1aa3a22645b697247e64
Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 20:12:30 2010 +0100
netfilter: nf_nat_sip: add TCP support
include/linux/netfilter/nf_conntrack_sip.h +1 -0
@@ -104,6 +104,7 @@ extern unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb,unsigned int dataoff, const char **dptr, unsigned int *datalen); +extern void (*nf_nat_sip_seq_adjust_hook)(struct sk_buff *skb,s16 off); extern unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb, unsigned int dataoff, const char **dptr,
net/ipv4/netfilter/nf_nat_sip.c +49 -8
119 lines not on this page · raw message
net/netfilter/nf_conntrack_sip.c +10 -0
@@ -56,6 +56,9 @@ unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, unsigned int dataoff, unsigned int *datalen) __read_mostly;EXPORT_SYMBOL_GPL (nf_nat_sip_hook); +void (*nf_nat_sip_seq_adjust_hook)(struct sk_buff *skb,s16 off)__read_mostly ; +EXPORT_SYMBOL_GPL (nf_nat_sip_seq_adjust_hook); + unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb, unsigned int dataoff, const char **dptr,
@@ -1360,6 +1363,7 @@ static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,const char *dptr, *end;s16 diff, tdiff =0 ;int ret; +typeof (nf_nat_sip_seq_adjust_hook) nf_nat_sip_seq_adjust;if (ctinfo !=IP_CT_ESTABLISHED && ctinfo !=IP_CT_ESTABLISHED +IP_CT_IS_REPLY )
@@ -1421,6 +1425,12 @@ static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff, dataoff, dptr, msglen, clen, diff, datalen); } +if (ret ==NF_ACCEPT && ct->status &IPS_NAT_MASK ) { + nf_nat_sip_seq_adjust =rcu_dereference (nf_nat_sip_seq_adjust_hook); +if (nf_nat_sip_seq_adjust) +nf_nat_sip_seq_adjust (skb, tdiff); + } +return ret; }
netfilter: nf_conntrack_sip: add T.38 FAX support
In reply to Patrick McHardycommit ff1e37680b0c9ee1454cfeee6f0f97452dd7daf5
Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 20:12:30 2010 +0100
netfilter: nf_conntrack_sip: add T.38 FAX support
include/linux/netfilter/nf_conntrack_sip.h +1 -0
@@ -14,6 +14,7 @@ enum sip_expectation_classes {SIP_EXPECT_SIGNALLING ,SIP_EXPECT_AUDIO ,SIP_EXPECT_VIDEO , +SIP_EXPECT_IMAGE , __SIP_EXPECT_MAX };#define SIP_EXPECT_MAX (__SIP_EXPECT_MAX - 1)
include/net/netfilter/nf_conntrack.h +1 -1
@@ -70,7 +70,7 @@ union nf_conntrack_help {struct nf_conntrack_helper ;/* Must be kept in sync with the classes defined by helpers */ -#define NF_CT_MAX_EXPECT_CLASSES 3 +#define NF_CT_MAX_EXPECT_CLASSES 4/* nf_conn feature for connections that have a helper */ struct nf_conn_help {
net/netfilter/nf_conntrack_sip.c +23 -5
@@ -907,6 +907,7 @@ err1:static const struct sdp_media_type sdp_media_types[] = {SDP_MEDIA_TYPE ("audio " ,SIP_EXPECT_AUDIO ),SDP_MEDIA_TYPE ("video " ,SIP_EXPECT_VIDEO ), +SDP_MEDIA_TYPE ("image " ,SIP_EXPECT_IMAGE ), }; static const struct sdp_media_type *sdp_media_type(const char *dptr,
@@ -932,7 +933,6 @@ static int process_sdp(struct sk_buff *skb, unsigned int dataoff, {enum ip_conntrack_info ctinfo;struct nf_conn *ct =nf_ct_get (skb, &ctinfo); -struct nf_conn_help *help =nfct_help (ct);unsigned int matchoff, matchlen;unsigned int mediaoff, medialen;unsigned int sdpoff;
@@ -1024,9 +1024,6 @@ static int process_sdp(struct sk_buff *skb, unsigned int dataoff, ret =nf_nat_sdp_session (skb, dataoff, dptr, datalen, sdpoff, &rtp_addr); -if (ret ==NF_ACCEPT && i >0 ) - help->help.ct_sip_info.invite_cseq = cseq; -return ret; } static int process_invite_response(struct sk_buff *skb, unsigned int dataoff,
@@ -1077,6 +1074,22 @@ static int process_prack_response(struct sk_buff *skb, unsigned int dataoff,return NF_ACCEPT ; } +static int process_invite_request (struct sk_buff *skb,unsigned int dataoff, +const char **dptr,unsigned int *datalen, +unsigned int cseq) +{ +enum ip_conntrack_info ctinfo; +struct nf_conn *ct =nf_ct_get (skb, &ctinfo); +struct nf_conn_help *help =nfct_help (ct); +unsigned int ret; + +flush_expectations (ct, true); + ret =process_sdp (skb, dataoff, dptr, datalen, cseq); +if (ret ==NF_ACCEPT ) + help->help.ct_sip_info.invite_cseq = cseq; +return ret; +} +static int process_bye_request (struct sk_buff *skb,unsigned int dataoff,const char **dptr,unsigned int *datalen,unsigned int cseq)
@@ -1257,7 +1270,7 @@ flush: }static const struct sip_handler sip_handlers[] = { -SIP_HANDLER ("INVITE" , process_sdp, process_invite_response), +SIP_HANDLER ("INVITE" , process_invite_request, process_invite_response),SIP_HANDLER ("UPDATE" , process_sdp, process_update_response),SIP_HANDLER ("ACK" , process_sdp,NULL ),SIP_HANDLER ("PRACK" , process_sdp, process_prack_response),
@@ -1479,6 +1492,11 @@ static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1 .max_expected = 2 * IP_CT_DIR_MAX, .timeout = 3 * 60, }, + [SIP_EXPECT_IMAGE] = { + .name = "image", + .max_expected = IP_CT_DIR_MAX, + .timeout = 3 * 60, + }, };static void nf_conntrack_sip_fini (void )
Re: netfilter: nf_conntrack: show helper and class in /proc/net/nf_conntrack_expect
In reply to Patrick McHardyQuoted 10 lines · Patrick McHardy
commit
8a96ae7fda648925254899ad997d5fe3984d3cf6Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 17:38:16 2010 +0100netfilter:
nf_conntrack: show helper and class in/proc/net/nf_conntrack_expectMake the output a bit more informative by showing the helper an expectation belongs to and the expectation class.
Signed-off-by Patrick McHardy <[email protected]>
Should not we extend on conntrack(8) instead?
Re: netfilter: nf_conntrack: show helper and class in /proc/net/nf_conntrack_expect
Quoted 6 lines · Jan Engelhardt
Quoted 4 lines · Patrick McHardy
commit
8a96ae7fda648925254899ad997d5fe3984d3cf6Author: Patrick McHardy <[email protected]> Date: Wed Jan 20 17:38:16 2010 +0100
Quoted 8 lines
Quoted 6 lines
netfilter:
nf_conntrack: show helper and class in/proc/net/nf_conntrack_expectMake the output a bit more informative by showing the helper an expectation belongs to and the expectation class.
Signed-off-by Patrick McHardy <[email protected]>Should not we extend on conntrack(8) instead?
Yeah, I've been planning on doing that as well. Especially the expectation support is kind of lacking - it has a lot more information than those shown. But I think having this available through /proc is still useful.
Thank you, Patrick :)
Quoted 9 lines · Patrick McHardy
The attached patches add TCP and T.38 FAX support to the SIP conntrack helper.
The patches are still work-in-progress, but they already work fine in all my tests and I know some people have been waiting for these features, so here's a snapshot of my current tree for anyone interested in testing.
Leave me a note when heading to Munich next time; the beer will be on my bill ;)
Quoted 3 lines
They're based on the nf-next-2.6.git tree, but they also apply to the 2.6.32 release (might not compile though :).
I'll give it a try within the next days ...
Quoted 2 lines
Please report any test results.
... and leave you a message with my results.
Quoted 23 lines
include/linux/netfilter/nf_conntrack_sip.h | 19 ++- include/net/netfilter/nf_conntrack.h | 2 +- include/net/netfilter/nf_conntrack_expect.h | 1 + include/net/netfilter/nf_nat_helper.h | 32 ++- net/ipv4/netfilter/nf_nat_helper.c | 39 ++-- net/ipv4/netfilter/nf_nat_sip.c | 158 ++++++++----- net/netfilter/nf_conntrack_expect.c | 9 + net/netfilter/nf_conntrack_sip.c | 333 +++++++++++++++++++++------ 8 files changed, 435 insertions(+), 158 deletions(-)Patrick McHardy (7):
netfilter:nf_conntrack: show helper and class in/proc/net/nf_conntrack_expect
netfilter:nf_conntrack_sip: fixct_sip_parse_request()REGISTER
request parsing
netfilter:nf_conntrack_sip: pass data offset to NAT functions
netfilter:nf_conntrack_sip: add TCP support
netfilter:nf_nat: support mangling a single TCP packet multiple
times
netfilter:nf_nat_sip: add TCP support
netfilter:nf_conntrack_sip: add T.38 FAX support