{Spam?} [PATCH 2.4] TCP window tracking: core implementation
Subsystems (3)
Hello,
This is the core tcp window tracking patch, updated for 2.4.32 and enhanced with some 2.6.x fixes in the tcp state verifier. Notably the following two flag combinations are allowed as well:
TCP_SYN|TCP_PSH
TCP_SYN|TCP_ACK|TCP_PSH
NOTE: This is the work of the excellent netfilter hacker Jozsef Kadlecsik, so all credit goes to him for the 2.4.x implementation. I just rediffed and backported some minor things from 2.6.x.
A former version of this patch has been in production on a dozen nodes for about 8 months now, and besides unmotivated DROPs invoked by "non-RFC-comformant" applications, it works reasonably well. The added TCP flag combinations in this patch should resolve most of the observed issues we've gathered from various customers over the past half year.
We'd hoped to overcome remaining "broken" applications by using the NOTRACK flag to a filter rule. This would allow one to have a general stateful packet filter with a few stateless rules for "broken" applications; a feature most commercial firewall suites don't offer. Unfortunately there is still an issue with regard to SMP and rmmod'ing ip_conntrack while having NOTRACK rules loaded and network traffic hitting NOTRACK and conntrack. Otherwise it works flawlessly. Patch will follow shortly.
Best regards,
Roberto Nibali, ratz
Signature · 6 lines
------------------------------------------------------------- addr://Kasinostrasse 30, CH-5001 Aarau tel://++41 62 823 9355 http://www.terreactive.com fax://++41 62 823 9356 ------------------------------------------------------------- terreActive AG Wir sichern Ihren Erfolg -------------------------------------------------------------
include/linux/netfilter_ipv4/ip_conntrack.h +5 -0
@@ -246,6 +246,11 @@ unsigned long extra_jiffies);/* These are for NAT. Icky. */ +/* Update TCP window tracking data after NAT successfully mangled the packet */ +extern int ip_conntrack_tcp_update (struct sk_buff *skb, +struct ip_conntrack *conntrack, +int dir); +/* Call me when a conntrack is destroyed. */ extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
include/linux/netfilter_ipv4/ip_conntrack_tcp.h +31 -9
@@ -4,25 +4,47 @@enum tcp_conntrack {TCP_CONNTRACK_NONE , -TCP_CONNTRACK_ESTABLISHED ,TCP_CONNTRACK_SYN_SENT ,TCP_CONNTRACK_SYN_RECV , +TCP_CONNTRACK_ESTABLISHED ,TCP_CONNTRACK_FIN_WAIT , -TCP_CONNTRACK_TIME_WAIT , -TCP_CONNTRACK_CLOSE ,TCP_CONNTRACK_CLOSE_WAIT ,TCP_CONNTRACK_LAST_ACK , +TCP_CONNTRACK_TIME_WAIT , +TCP_CONNTRACK_CLOSE ,TCP_CONNTRACK_LISTEN , -TCP_CONNTRACK_MAX +TCP_CONNTRACK_MAX , +TCP_CONNTRACK_IGNORE +}; + +/* Window scaling is advertised by the sender */ +#define IP_CT_TCP_FLAG_WINDOW_SCALE 0x01 + +/* SACK is permitted by the sender */ +#define IP_CT_TCP_FLAG_SACK_PERM 0x02 + +/* This sender sent FIN first */ +#define IP_CT_TCP_FLAG_CLOSE_INIT 0x03 + +struct ip_ct_tcp_state { +u_int32_t td_end;/* max of seq + len */ +u_int32_t td_maxend;/* max of ack + max(win, 1) */ +u_int32_t td_maxwin;/* max(win) */ +u_int8_t td_scale;/* window scale factor */ +u_int8_t loose;/* used when connection picked up from the middle */ +u_int8_t flags;/* per direction options */ };struct ip_ct_tcp { -enum tcp_conntrack state; - -/* Poor man's window tracking: sequence number of valid ACK -handshake completion packet */ -u_int32_t handshake_ack; +struct ip_ct_tcp_state seen[2 ];/* connection parameters per direction */ +u_int8_t state;/* state of the connection (enum tcp_conntrack) */ +/* For detecting stale connections */ +u_int8_t last_dir;/* Direction of the last packet (enum ip_conntrack_dir) */ +u_int8_t retrans;/* Number of retransmitted packets */ +u_int8_t last_index;/* Index of the last packet */ +u_int32_t last_seq;/* Last sequence number seen in dir */ +u_int32_t last_end;/* Last seq + len */ };#endif /* _IP_CONNTRACK_TCP_H */
include/linux/sysctl.h +5 -0
@@ -396,6 +396,11 @@NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT =12 ,NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT =13 ,NET_IPV4_NF_CONNTRACK_BUCKETS =14 , +NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS =15 , +NET_IPV4_NF_CONNTRACK_TCP_LOG_INVALID =16 , +NET_IPV4_NF_CONNTRACK_TCP_LOOSE =17 , +NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL =18 , + NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS=19, };/* /proc/sys/net/ipv6 */
net/ipv4/netfilter/Makefile +4 -0
@@ -31,6 +31,10 @@ # connection tracking obj-$(CONFIG_IP_NF_CONNTRACK) += ip_conntrack.o +ifdef CONFIG_IP_NF_NAT_NEEDED + export-objs += ip_conntrack_proto_tcp.o +endif + # connection tracking helpers obj-$(CONFIG_IP_NF_AMANDA) += ip_conntrack_amanda.o ifdef CONFIG_IP_NF_AMANDA
net/ipv4/netfilter/ip_conntrack_core.c +4 -3
@@ -874,11 +874,12 @@IP_NF_ASSERT ((*pskb)->nfct); ret = proto->packet (ct, (*pskb)->nh.iph, (*pskb)->len, ctinfo); -if (ret ==-1 ) { -/* Invalid */ +if (ret <0 ) { +/* Invalid: inverse of the return code tells +* to the netfilter core what to do. */ nf_conntrack_put ((*pskb)->nfct); (*pskb)->nfct =NULL ; -return NF_ACCEPT ; +return -ret; }if (ret !=NF_DROP && ct->helper) {
net/ipv4/netfilter/ip_conntrack_ftp.c +2 -20
@@ -4,7 +4,6 @@#include <linux/netfilter.h> #include <linux/ip.h> #include <linux/ctype.h> -#include <net/checksum.h> #include <net/tcp.h> #include <linux/netfilter_ipv4/lockhelp.h>
@@ -233,11 +232,10 @@struct ip_conntrack *ct, enum ip_conntrack_info ctinfo) { - /* tcplen not negative guaranteed by ip_conntrack_tcp.c */ + /* datalen not negative guaranteed by ip_conntrack_proto_tcp.c */ struct tcphdr *tcph = (void *)iph + iph->ihl * 4;const char *data = (const char *)tcph + tcph->doff *4 ; -unsigned int tcplen = len - iph->ihl *4 ; -unsigned int datalen = tcplen - tcph->doff *4 ; +unsigned int datalen = len - iph->ihl *4 - tcph->doff *4 ;u_int32_t old_seq_aft_nl;int old_seq_aft_nl_set;u_int32_t array[6 ] = {0 };
@@ -257,22 +255,6 @@return NF_ACCEPT ; } -/* Not whole TCP header? */ -if (tcplen <sizeof (struct tcphdr ) || tcplen < tcph->doff*4 ) { -DEBUGP ("ftp: tcplen = %u\n" , (unsigned )tcplen); -return NF_ACCEPT ; - } - -/* Checksum invalid? Ignore. */ -/* FIXME: Source route IP option packets --RR */ -if (tcp_v4_check (tcph, tcplen, iph->saddr, iph->daddr, -csum_partial ((char *)tcph, tcplen,0 ))) { -DEBUGP ("ftp_help: bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n" , - tcph, tcplen,NIPQUAD (iph->saddr), -NIPQUAD (iph->daddr)); -return NF_ACCEPT ; - } -LOCK_BH (&ip_ftp_lock); old_seq_aft_nl_set = ct_ftp_info->seq_aft_nl_set[dir]; old_seq_aft_nl = ct_ftp_info->seq_aft_nl[dir];
net/ipv4/netfilter/ip_conntrack_irc.c +2 -20
@@ -26,7 +26,6 @@#include <linux/module.h> #include <linux/netfilter.h> #include <linux/ip.h> -#include <net/checksum.h> #include <net/tcp.h> #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
@@ -107,13 +106,12 @@static int help (const struct iphdr *iph,size_t len,struct ip_conntrack *ct,enum ip_conntrack_info ctinfo) { -/* tcplen not negative guarenteed by ip_conntrack_tcp.c */ +/* datalen not negative guarenteed by ip_conntrack_proto_tcp.c */ struct tcphdr *tcph = (void *) iph + iph->ihl *4 ;const char *data = (const char *) tcph + tcph->doff *4 ;const char *_data = data;char *data_limit; -u_int32_t tcplen = len - iph->ihl *4 ; -u_int32_t datalen = tcplen - tcph->doff *4 ; +u_int32_t datalen = len - iph->ihl *4 - tcph->doff *4 ;int dir =CTINFO2DIR (ctinfo);struct ip_conntrack_expect expect, *exp = &expect;struct ip_ct_irc_expect *exp_irc_info = &exp->help.exp_irc_info;
@@ -136,22 +134,6 @@return NF_ACCEPT ; } -/* Not whole TCP header? */ -if (tcplen <sizeof (struct tcphdr ) || tcplen < tcph->doff *4 ) { -DEBUGP ("tcplen = %u\n" , (unsigned ) tcplen); -return NF_ACCEPT ; - } - -/* Checksum invalid? Ignore. */ -/* FIXME: Source route IP option packets --RR */ -if (tcp_v4_check (tcph, tcplen, iph->saddr, iph->daddr, -csum_partial ((char *) tcph, tcplen,0 ))) { -DEBUGP ("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n" , - tcph, tcplen,NIPQUAD (iph->saddr), -NIPQUAD (iph->daddr)); -return NF_ACCEPT ; - } - data_limit = (char *) data + datalen;/* strlen("\1DCC SEND t AAAAAAAA P\1\n")=24
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +932 -99
1,138 lines not on this page · raw message
net/ipv4/netfilter/ip_conntrack_standalone.c +20 -0
@@ -259,6 +259,11 @@extern unsigned long ip_ct_tcp_timeout_last_ack;extern unsigned long ip_ct_tcp_timeout_time_wait;extern unsigned long ip_ct_tcp_timeout_close; +extern unsigned long ip_ct_tcp_timeout_max_retrans; +extern int ip_ct_tcp_log_invalid; +extern int ip_ct_tcp_loose; +extern int ip_ct_tcp_be_liberal; +extern int ip_ct_tcp_max_retrans;/* From ip_conntrack_proto_udp.c */ extern unsigned long ip_ct_udp_timeout;
@@ -315,6 +320,21 @@ {NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT ,"ip_conntrack_generic_timeout" , &ip_ct_generic_timeout,sizeof (unsigned int ),0644 ,NULL , &proc_dointvec_jiffies}, + {NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS, "ip_conntrack_tcp_timeout_max_retrans", + &ip_ct_tcp_timeout_max_retrans,sizeof (unsigned int ),0644 ,NULL , + &proc_dointvec_jiffies}, + {NET_IPV4_NF_CONNTRACK_TCP_LOG_INVALID, "ip_conntrack_tcp_log_invalid", + &ip_ct_tcp_log_invalid,sizeof (unsigned int ),0644 ,NULL , + &proc_dointvec}, + {NET_IPV4_NF_CONNTRACK_TCP_LOOSE, "ip_conntrack_tcp_loose", + &ip_ct_tcp_loose,sizeof (unsigned int ),0644 ,NULL , + &proc_dointvec}, + {NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL, "ip_conntrack_tcp_be_liberal", + &ip_ct_tcp_be_liberal,sizeof (unsigned int ),0644 ,NULL , + &proc_dointvec}, + {NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS, "ip_conntrack_tcp_max_retrans", + &ip_ct_tcp_max_retrans,sizeof (unsigned int ),0644 ,NULL , + &proc_dointvec}, {0} };
net/ipv4/netfilter/ip_nat_helper.c +2 -0
@@ -451,6 +451,8 @@ tcph->ack_seq = newack;ip_nat_sack_adjust (skb, ct, ctinfo); + +ip_conntrack_tcp_update (skb, ct, dir);return 0 ; }
Re: [PATCH 2.4] TCP window tracking: core implementation
Hi Roberto,
Quoted 9 lines · Roberto Nibali
Hello,
This is the core tcp window tracking patch, updated for 2.4.32 and enhanced with some 2.6.x fixes in the tcp state verifier. Notably the following two flag combinations are allowed as well:
TCP_SYN|TCP_PSH
TCP_SYN|TCP_ACK|TCP_PSH
Quoted 7 lines
NOTE: This is the work of the excellent netfilter hacker Jozsef Kadlecsik, so all credit goes to him for the 2.4.x implementation. I just rediffed and backported some minor things from 2.6.x.
A former version of this patch has been in production on a dozen nodes for about 8 months now, and besides unmotivated DROPs invoked by "non-RFC-comformant" applications, it works reasonably well.
Seconded !
I've had it in production for 18 months now, handling between 1.2 and 1.8 billions of sessions a month, and it still works like a charm.
Quoted 4 lines
The added
TCP flag combinations in this patch should resolve most of the observed issues we've gathered from various customers over the past half year.
Quoted 8 lines
We'd hoped to overcome remaining "broken" applications by using the NOTRACK flag to a filter rule. This would allow one to have a general stateful packet filter with a few stateless rules for "broken" applications; a feature most commercial firewall suites don't offer. Unfortunately there is still an issue with regard to SMP and rmmod'ing
ip_conntrackwhile having NOTRACK rules loaded and network traffic hitting NOTRACK and conntrack. Otherwise it works flawlessly. Patch will follow shortly.
another possibility would be to add something like a "reason" code to the INVALID state, so that we could tell which types of invalids we still want to let go and which ones we definitely want to block.
Cheers,
Willy
Re: [PATCH 2.4] TCP window tracking: core implementation
Salut Willy,
Quoted 7 lines
Quoted 3 lines
A former version of this patch has been in production on a dozen nodes for about 8 months now, and besides unmotivated DROPs invoked by "non-RFC-comformant" applications, it works reasonably well.
Seconded !
I've had it in production for 18 months now, handling between 1.2 and 1.8 billions of sessions a month, and it still works like a charm.
Could you batch my patchset for your next -hf series, please? I'll try to get you a tcpdump with erroneous behaviour regarding window tracking, so you can verify this as well.
Quoted 9 lines
Quoted 8 lines
We'd hoped to overcome remaining "broken" applications by using the NOTRACK flag to a filter rule. This would allow one to have a general stateful packet filter with a few stateless rules for "broken" applications; a feature most commercial firewall suites don't offer. Unfortunately there is still an issue with regard to SMP and rmmod'ing
ip_conntrackwhile having NOTRACK rules loaded and network traffic hitting NOTRACK and conntrack. Otherwise it works flawlessly. Patch will follow shortly.
Quoted 3 lines
another possibility would be to add something like a "reason" code to the INVALID state, so that we could tell which types of invalids we still want to let go and which ones we definitely want to block.
This is of course possible but could potentially lead to a lot of exception code. I reckon that it's safe enough to fallback to classic packet filtering when dealing with non-RFC conform TCP clients or applications. Meanwhile I've found the problem with the NOTRACK hanging and will propose a fix in another thread I've started.
Best regards,
Roberto Nibali, ratz
Signature · 6 lines
------------------------------------------------------------- addr://Kasinostrasse 30, CH-5001 Aarau tel://++41 62 823 9355 http://www.terreactive.com fax://++41 62 823 9356 ------------------------------------------------------------- terreActive AG Wir sichern Ihren Erfolg -------------------------------------------------------------
Re: [PATCH 2.4] TCP window tracking: core implementation
Hi,
Quoted 3 lines · Roberto Nibali
Could you batch my patchset for your next -hf series, please? I'll try to get you a tcpdump with erroneous behaviour regarding window tracking, so you can verify this as well.
I'd be interested in the recording as well!
Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics
H-1525 Budapest 114, POB. 49, Hungary
Re: [PATCH 2.4] TCP window tracking: core implementation
In reply to Roberto NibaliHi Roberto,
Quoted 6 lines · Roberto Nibali
Salut Willy,
Quoted 4 lines
Quoted 3 lines
A former version of this patch has been in production on a dozen nodes for about 8 months now, and besides unmotivated DROPs invoked by "non-RFC-comformant" applications, it works reasonably well.
Quoted 7 lines
Quoted 3 lines
Seconded !
I've had it in production for 18 months now, handling between 1.2 and 1.8 billions of sessions a month, and it still works like a charm.Could you batch my patchset for your next -hf series, please? I'll try to get you a tcpdump with erroneous behaviour regarding window tracking, so you can verify this as well.
No, and I'm sorry to insist in this direction : the -hf tree is only a *selection* of patches from next mainline version(s). It's targetted at the users who cannot risk an upgrade to the next -pre-something, but who still need security or stability fixes. Under no circumstance should I add new features to this tree.
I'm currently thinking about another tree (something like 2.4-enterprise) which would host those patches absolutely needed for people with higher expectations than "normal" users. Nflog, window-tracking, epoll, and jiffies64 immediately come to mind, but possibly a small bunch of others too. And I hope to count you among the patch contributors ;-)
Quoted 13 lines
Quoted 9 lines
Quoted 8 lines
We'd hoped to overcome remaining "broken" applications by using the NOTRACK flag to a filter rule. This would allow one to have a general stateful packet filter with a few stateless rules for "broken" applications; a feature most commercial firewall suites don't offer. Unfortunately there is still an issue with regard to SMP and rmmod'ing
ip_conntrackwhile having NOTRACK rules loaded and network traffic hitting NOTRACK and conntrack. Otherwise it works flawlessly. Patch will follow shortly.Quoted 3 lines
another possibility would be to add something like a "reason" code to the INVALID state, so that we could tell which types of invalids we still want to let go and which ones we definitely want to block.
Quoted 5 lines
This is of course possible but could potentially lead to a lot of exception code. I reckon that it's safe enough to fallback to classic packet filtering when dealing with non-RFC conform TCP clients or applications. Meanwhile I've found the problem with the NOTRACK hanging and will propose a fix in another thread I've started.
OK, I don't have much idea about how to proceed with the NOTRACK yet because in the past I could not get it to work along with window-tracking. So of course I'm interested :-)
Cheers,
Willy
Re: [PATCH 2.4] TCP window tracking: core implementation
Hello Willy,
Quoted 6 lines
Quoted 3 lines
Could you batch my patchset for your next -hf series, please? I'll try to get you a tcpdump with erroneous behaviour regarding window tracking, so you can verify this as well.
No, and I'm sorry to insist in this direction : the -hf tree is only a *selection* of patches from next mainline version(s). It's targetted at
Stupid me, I didn't mean -hf series, I meant the experimental 2.4.x kernel series we're going to start.
Quoted 3 lines
the users who cannot risk an upgrade to the next -pre-something, but who still need security or stability fixes. Under no circumstance should I add new features to this tree.
Of course. Although tcp window tracking could be seen as a security fix.
Quoted 3 lines
I'm currently thinking about another tree (something like 2.4-enterprise) which would host those patches absolutely needed for people with higher expectations than "normal" users. Nflog, window-tracking, epoll, and
I'm probably going to drop the nflog stuff, since it is not really used in 2.4.x. Also the tcp window tracking can happily live without it.
Quoted 2 lines
jiffies64 immediately come to mind, but possibly a small bunch of others too. And I hope to count you among the patch contributors ;-)
Sure thing.
Quoted 9 lines
Quoted 5 lines
This is of course possible but could potentially lead to a lot of exception code. I reckon that it's safe enough to fallback to classic packet filtering when dealing with non-RFC conform TCP clients or applications. Meanwhile I've found the problem with the NOTRACK hanging and will propose a fix in another thread I've started.
OK, I don't have much idea about how to proceed with the NOTRACK yet because in the past I could not get it to work along with window-tracking. So of course I'm interested :-)
It's not supposed to work with window tracking other than not influencing it. I use the NOTRACK feature to simulate the old ipchains or ipfwadm behaviour. So when using NOTRACK you need to map the TCP states into to filter rules yourself. While window tracking (looking at TCP) is an n-tuple (n>7) check on the skb, NOTRACK is a simple 5 to 7-tuple check with no memory constraints:
<srcIP, srcPORT, destIP, destPORT, proto, (TCPflags, interface)>
I'll send you the semantics off-list.
Best regards,
Roberto Nibali, ratz
Signature · 1 line
echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq'|dc
Re: [PATCH 2.4] TCP window tracking: core implementation
Quoted 11 lines · Roberto Nibali
Hello Willy,
Quoted 6 lines
Quoted 3 lines
Could you batch my patchset for your next -hf series, please? I'll try to get you a tcpdump with erroneous behaviour regarding window tracking, so you can verify this as well.
No, and I'm sorry to insist in this direction : the -hf tree is only a *selection* of patches from next mainline version(s). It's targetted at
Stupid me, I didn't mean -hf series, I meant the experimental 2.4.x kernel series we're going to start.
OK, I'm reassured.
Quoted 5 lines
Quoted 3 lines
the users who cannot risk an upgrade to the next -pre-something, but who still need security or stability fixes. Under no circumstance should I add new features to this tree.
Of course. Although tcp window tracking could be seen as a security fix.
I knew you would say this, I left the window open on purpose :-)
Quoted 6 lines
Quoted 3 lines
I'm currently thinking about another tree (something like 2.4-enterprise) which would host those patches absolutely needed for people with higher expectations than "normal" users. Nflog, window-tracking, epoll, and
I'm probably going to drop the nflog stuff, since it is not really used in 2.4.x. Also the tcp window tracking can happily live without it.
OK, I did not know. I've long believed that it really relied on it. Maybe it's only for the INVALID logs ? Most often, /dev/null wants to eat them.
Quoted 4 lines
Quoted 2 lines
jiffies64 immediately come to mind, but possibly a small bunch of others too. And I hope to count you among the patch contributors ;-)
Sure thing.
cool.
Quoted 18 lines
Quoted 10 lines
Quoted 5 lines
This is of course possible but could potentially lead to a lot of exception code. I reckon that it's safe enough to fallback to classic packet filtering when dealing with non-RFC conform TCP clients or applications. Meanwhile I've found the problem with the NOTRACK hanging and will propose a fix in another thread I've started.
OK, I don't have much idea about how to proceed with the NOTRACK yet because in the past I could not get it to work along with window-tracking.
So of course I'm interested :-)It's not supposed to work with window tracking other than not influencing it. I use the NOTRACK feature to simulate the old ipchains or ipfwadm behaviour. So when using NOTRACK you need to map the TCP states into to filter rules yourself. While window tracking (looking at TCP) is an n-tuple (n>7) check on the skb, NOTRACK is a simple 5 to 7-tuple check with no memory constraints:
Quoted 3 lines
<srcIP, srcPORT, destIP, destPORT, proto, (TCPflags, interface)>
I'll send you the semantics off-list.
OK thanks, now I understand what you do.
Quoted 2 lines
Best regards,
Roberto Nibali, ratz
Cheers,
Willy