Skip to content

Lesson 6 of 9 · 24m

Conntrack, NAT, and the Life of a Flow

Read connection-tracking states, NAT translations, timeouts, and asymmetric failures to explain why one packet works and the flow does not.

Ping succeeds, TCP connects, and then large transfers stall. Or the first request works but replies disappear. These are flow-lifecycle problems, and conntrack is where Linux keeps score. The connection tracker is not just a firewall convenience; NAT depends on it, helpers depend on it, and plenty of production weirdness is nothing more than state aging out at the wrong time.

Read a conntrack entry like a timeline

sudo conntrack -L -p tcp | head

A typical NAT entry compresses both directions:

tcp 6 431999 ESTABLISHED src=10.8.0.2 dst=203.0.113.20 sport=52444 dport=443     src=203.0.113.20 dst=198.51.100.9 sport=443 dport=52444 [ASSURED] mark=0 use=1

The first tuple is the original direction. The second is the expected reply direction after translation. If the public reply address does not match what the remote service should send to, translation or routing is wrong. [ASSURED] means traffic has been observed in both directions.

States are observations, not application truth

NEW, ESTABLISHED, RELATED, and INVALID are netfilter classifications. TCP can be ESTABLISHED in conntrack while the application is hung. UDP has no real connection, so its state is a timeout-backed guess. ICMP errors can be RELATED when they match an existing flow.

ct state invalid counter drop
ct state established,related counter accept

Dropping INVALID is standard, but during diagnosis you need to know whether the kernel is classifying traffic that way. Asymmetric routing is a frequent cause: the host sees only one direction and cannot build the state it expects.

NAT failure modes are directional

Destination NAT failure usually means the packet never reaches the intended socket. Source NAT failure usually means the request leaves but the reply has no return path. Port exhaustion is nastier: thousands of concurrent flows compete for the tuple space of one public address.

sudo conntrack -C
sysctl net.netfilter.nf_conntrack_max
sudo conntrack -L -p udp --dport 53 | wc -l

A full conntrack table turns into intermittent connection failure, not a clean outage. UDP-heavy workloads and scanning behavior can fill it quickly.

Helpers are compatibility debt

FTP, SIP, and some older protocols embed addresses inside application payloads. Conntrack helpers inspect and rewrite those flows. On modern hosts, automatic helper assignment is usually disabled or tightly controlled for good reason. If an old protocol works without NAT and fails with NAT, ask whether it depends on helper behavior before redesigning the firewall around it.

Debugging the flow lifecycle

Capture both sides of NAT when possible:

sudo tcpdump -ni wg0 host 203.0.113.20
sudo tcpdump -ni eth0 host 203.0.113.20

The same flow should appear with private addressing on the inside interface and translated addressing on the outside. If one side is absent, the disappearance point is between those captures: routing, netfilter, qdisc, or interface transmission.

Field notes

  • Conntrack entries can outlive the process that created them, especially for UDP.
  • Docker restart, interface flaps, and network namespace deletion can invalidate assumptions while leaving confusing stale entries briefly visible.
  • Do not raise nf_conntrack_max blindly; inspect memory use and the actual flow population first.
  • TCP retransmissions can be a symptom of a failing middlebox even when local conntrack looks perfect.
  • When testing NAT, use a fresh flow each time; existing state can make a broken rule look correct.

Was this lesson helpful?

Sign in to rate