← Lab /Systems

Conntrack can rewrite soft-unicast ports, but TCP_FASTOPEN sockets prevent collisions

Using TCP_FASTOPEN (or TCP_REPAIR) sockets reserves soft-unicast 5-tuples, preventing Netfilter conntrack from silently rewriting source ports, and that disabling early demux incurs only slight CPU overhead.

Systems Cloudflare Research 29 Oct 2025 Method Deployed evidence

Published as “So long, and thanks for all the fish: how to escape the Linux networking stack”

Why it matters

Enables reliable soft-unicast address allocation at scale without silent port rewrites.

Summary

Cloudflare discovered that Netfilter conntrack can rewrite source ports of soft-unicast connections, breaking the allocated port slice. They solved this by creating TCP sockets with TCP_FASTOPEN_CONNECT (or TCP_REPAIR) which reserve the 5-tuple and block other processes from binding the same address, eliminating silent rewrites. To forward packets via a TUN device they had to disable the kernel's early demux feature, which they measured caused only a slight increase in CPU softirq usage. The combined changes are now used in production for the fish service, albeit limited to handling ICMP traffic.

What they found 3

  1. TCP_FASTOPEN sockets block other processes from binding the same address, preventing port collisions.

    “Binding a “connected” socket that nevertheless corresponds to no actual socket has one important feature: if other processes attempt to bind to the same addresses as the socket, they will fail to do so.”

  2. Conntrack can silently rewrite source ports outside the soft-unicast slice, causing connection failures.

    “Crucially for soft-unicast, it means conntrack may rewrite our connection to have a source port outside of the port slice assigned to our machine.”

  3. Disabling early demux results in only a slight CPU usage increase, mainly during off-peak hours.

    “The observed difference is slight, and mostly appears to reduce efficiency during off-peak hours.”

Caveats 4

  • The TCP_FASTOPEN approach adds extra resource usage and potentially increased latency compared to pure packet rewriting.
  • Disabling early demux may regress performance on existing workloads, though the measured impact was small.
  • The netlink-based conntrack manipulation was deemed too fragile and inefficient for production.
  • The solution relies on kernel features (TCP_FASTOPEN, early demux) that may change in future Linux versions.

What’s new here 4

  • Identified that conntrack may rewrite soft-unicast source ports outside the allocated range.
  • Demonstrated that TCP_FASTOPEN_CONNECT/TCP_REPAIR sockets reserve 5-tuples and prevent other bindings.
  • Showed that disabling net.ipv4.tcp_early_demux allows forwarded packets to bypass local socket lookup.
  • Deployed the solution in Cloudflare's fish service with minimal performance impact.