NAT && TIME_WAIT TCP connections
Subsystems (2)
Hi,
I have a proposal regarding connection tracking and NAT. Imagine a
scenario, where you must SNAT TCP traffic not only to a specific IP range, but also to a specific port range. The extreme case of the scenario is of course one IP with only one port, for example:
iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -p tcp -j SNAT \ --to-source 0.2.0.1:1234
However, such a setup maximizes the number of connections to which NAT
can be applied, and you must wait for the existing connections to get deleted (timeout, etc.) before another connection can be created. However, in case of TCP, when the SNAT range is a scarce resource, IP:port pairs could be reused for connections where the connection is already in a 'half-died' state (for example, TCP's TIME_WAIT). The theory of operation is the following: a protocol helper marks the conntrack entry MAY_BE_DELETED if it thinks that it's in a state where new packages cannot be received. Then, if ip_nat_setup_info() finds that while trying to allocate a new IP/port pair from the given range, a clashing conntrack entry has this flag, it deletes the old one, so the allocation can succeed.
While the upper example may look a bit extreme, such problems occur
much more often when using the TPROXY patch and a transparent SQUID proxy. The attached patch helped a lot in these cases (and after modifying ip_conntrack_proto_tcp.c accordingly, to mark TPROXY-ed TCP connections 'deletable' when they reach the TIME_WAIT state).
Any comments? (I don't like the idea of deleting conntrack entries inip_nat_setup_info(), however, I don't have a better idea.)
Signature · 2 lines
Regards,
Krisztian KOVACSinclude/linux/netfilter_ipv4/ip_conntrack.h +5 -1
@@ -46,6 +46,10 @@/* Connection is confirmed: originating packet has left box */ IPS_CONFIRMED_BIT =3 ,IPS_CONFIRMED = (1 <<IPS_CONFIRMED_BIT ), + +/* May delete conntrack if its tuple is needed for NAT */ +IPS_MAY_DELETE_BIT =5 , + IPS_MAY_DELETE = (1 << IPS_MAY_DELETE_BIT), };#include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
@@ -219,7 +223,7 @@/* Is this tuple taken? (ignoring any belonging to the given conntrack). */ -extern int +extern struct ip_conntrack_tuple_hash *ip_conntrack_tuple_taken (const struct ip_conntrack_tuple *tuple,const struct ip_conntrack *ignored_conntrack);
net/ipv4/netfilter/ip_conntrack_core.c +2 -2
@@ -479,7 +479,7 @@/* Returns true if a connection correspondings to the tuple (required for NAT). */ -int +struct ip_conntrack_tuple_hash *ip_conntrack_tuple_taken (const struct ip_conntrack_tuple *tuple,const struct ip_conntrack *ignored_conntrack) {
@@ -489,7 +489,7 @@ h =__ip_conntrack_find (tuple, ignored_conntrack);READ_UNLOCK (&ip_conntrack_lock); -return h !=NULL ; +return h; }/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
net/ipv4/netfilter/ip_nat_core.c +42 -1
57 lines not on this page · raw message
Quoted 8 lines · Kovacs Krisztian
While the upper example may look a bit extreme, such problems occur
much more often when using the TPROXY patch and a transparent SQUID proxy. The attached patch helped a lot in these cases (and after modifyingip_conntrack_proto_tcp.caccordingly, to mark TPROXY-ed TCP connections 'deletable' when they reach theTIME_WAITstate).Any comments? (I don't like the idea of deleting conntrack entries in
ip_nat_setup_info(), however, I don't have a better idea.)
It's questionable whether we should optimize for minimization of tuple allocations at the cost of run time performance :(
I'm really not sure if it's worth the effort.
Quoted 2 lines
Regards,
Krisztian KOVACS
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie