Skip to contentSkip to conversation

netfilter: conntrack: do not renew timeout while in tcp SYN_SENT state

PATCH In review Florian Westphal 2 participants 4 messages Jun 24, 2021 – Jul 2, 2021

Subsystems (3)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: conntrack: do not renew timeout while in tcp SYN_SENT state

Antonio Ojea reported a problem with a container environment where connection retries prevent expiry of a SYN_SENT conntrack entry.

This in turn prevents a NAT rule from becoming active.

Consider:
client -----> conntrack ---> Host

client sends a SYN, but $Host is unreachable/silent.

In the reported case, $host address doesn't exist at all -- its a 'virtual' ip that is made accessible via dnat/redirect.

The routing table even passes the packet back via the same interface it arrived on.

In the mean time, a NAT rule has been added to the conntrack namespace, but it has no effect until the existing conntrack entry times out.

Unfortunately, in the above scenario, the client retries reconnects faster than the SYN default timeout (60 seconds), i.e. the entry never expires and the 'virtual' ip never becomes active.

First patch adds a test case:
3 namespaces, one sender, one receiver.
sender connects to non-existent/virtual ip.
Then a dnat rule gets added.

The test case succeeds once conntrack tool shows that the nat rule was evaluated.

Second patch prevents timeout refresh for entries stuck in SYN_SENT state.

Without second patch the test case doesn't pass even though syn timeout is set to 10 seconds.

Florian Westphal (2):
selftest: netfilter: add test case for unreplied tcp connections
netfilter: conntrack: do not renew entry stuck in tcp SYN_SENT state

 net/netfilter/nf_conntrack_proto_tcp.c        |  10 ++
 tools/testing/selftests/netfilter/Makefile    |   2 +-
 .../netfilter/conntrack_tcp_unreplied.sh      | 167 ++++++++++++++++++
 3 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/netfilter/conntrack_tcp_unreplied.sh
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/2 selftest: netfilter: add test case for unreplied tcp connections

TCP connections in UNREPLIED state (only SYN seen) can be kept alive indefinitely, as each SYN re-sets the timeout.

This means that even if a peer has closed its socket the entry never times out.

This also prevents re-evaluation of configured NAT rules. Add a test case that sets SYN timeout to 10 seconds, then check that the nat redirection added later eventually takes effect.

This is based off a repro script from Antonio Ojea.

---
 tools/testing/selftests/netfilter/Makefile    |   2 +-
 .../netfilter/conntrack_tcp_unreplied.sh      | 167 ++++++++++++++++++
 2 files changed, 168 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/netfilter/conntrack_tcp_unreplied.sh
tools/testing/selftests/netfilter/Makefile +1 -1
index cd6430b39982..8748199ac109 100644
@@ -5,7 +5,7 @@ TEST_PROGS := nft_trans_stress.sh nft_fib.sh nft_nat.sh bridge_brouter.sh \
 	conntrack_icmp_related.sh nft_flowtable.sh ipvs.sh \
 	nft_concat_range.sh nft_conntrack_helper.sh \
 	nft_queue.sh nft_meta.sh nf_nat_edemux.sh \
-	ipip-conntrack-mtu.sh
+	ipip-conntrack-mtu.sh conntrack_tcp_unreplied.sh
 
 LDLIBS = -lmnl
 TEST_GEN_FILES =  nf-queue
tools/testing/selftests/netfilter/conntrack_tcp_unreplied.sh new file +167 -0

167 lines not on this page · raw message

Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH 2/2 netfilter: conntrack: do not renew entry stuck in tcp SYN_SENT state

In reply to Florian Westphal

Consider:
client -----> conntrack ---> Host

client sends a SYN, but $Host is unreachable/silent. Client eventually gives up and the conntrack entry will time out.

However, if the client is restarted with same addr/port pair, it may prevent the conntrack entry from timing out.

This is noticeable when the existing conntrack entry has no NAT transformation or an outdated one and port reuse happens either on client or due to a NAT middlebox.

This change prevents refresh of the timeout for SYN retransmits, so entry is going away after nf_conntrack_tcp_timeout_syn_sent seconds (default: 60).

Entry will be re-created on next connection attempt, but then nat rules will be evaluated again.

---
 net/netfilter/nf_conntrack_proto_tcp.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
net/netfilter/nf_conntrack_proto_tcp.c +10 -0
index f7e8baf59b51..eb4de92077a8 100644
@@ -1134,6 +1134,16 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 			nf_ct_kill_acct(ct, ctinfo, skb);
 			return NF_ACCEPT;
 		}
+
+		if (index == TCP_SYN_SET && old_state == TCP_CONNTRACK_SYN_SENT) {
+			/* do not renew timeout on SYN retransmit.
+			 *
+			 * Else port reuse by client or NAT middlebox can keep
+			 * entry alive indefinitely (including nat info).
+			 */
+			return NF_ACCEPT;
+		}
+
 		/* ESTABLISHED without SEEN_REPLY, i.e. mid-connection
 		 * pickup with loose=1. Avoid large ESTABLISHED timeout.
 		 */
Signature · 1 line
2.31.1
#3 lore raw
In reply to Florian Westphal
Quoted 29 lines · Florian Westphal

Antonio Ojea reported a problem with a container environment where connection retries prevent expiry of a SYN_SENT conntrack entry.

This in turn prevents a NAT rule from becoming active.

Consider:
client -----> conntrack ---> Host

client sends a SYN, but $Host is unreachable/silent.

In the reported case, $host address doesn't exist at all -- its a 'virtual' ip that is made accessible via dnat/redirect.

The routing table even passes the packet back via the same interface it arrived on.

In the mean time, a NAT rule has been added to the conntrack namespace, but it has no effect until the existing conntrack entry times out.

Unfortunately, in the above scenario, the client retries reconnects faster than the SYN default timeout (60 seconds), i.e. the entry never expires and the 'virtual' ip never becomes active.

First patch adds a test case:
3 namespaces, one sender, one receiver.
sender connects to non-existent/virtual ip.
Then a dnat rule gets added.

Quoted 8 lines

The test case succeeds once conntrack tool shows that the nat rule was evaluated.

Second patch prevents timeout refresh for entries stuck in SYN_SENT state.

Without second patch the test case doesn't pass even though syn timeout is set to 10 seconds.

Series applied, thanks.