Sub topic 4
How the NAT device remembers connections
This chapter assumes you know IPv4 addressing, what a default gateway does, and how TCP and UDP use port numbers. It also assumes you can run commands in a Linux shell.
Why a NAT device needs memory
A NAT device rewrites addresses and ports on packets. When a reply comes back, it must undo that rewrite. The reply only carries the public address and port. It says nothing about which private host started the exchange. The device therefore stores each translation in a table.
On Linux this table is the connection tracking table, called conntrack. The kernel creates an entry when the first packet of a new flow arrives. Every later packet in that flow, in both directions, is matched against the entry.
What an entry contains
Each entry holds two tuples. A tuple is the set of fields that identify a flow: protocol, source address, source port, destination address, and destination port.
- The original tuple is the flow as the sender sent it.
- The reply tuple is the flow as the reply is expected to arrive.
When NAT rewrites the source, the reply tuple differs from the original tuple. The difference between the two is the translation.
| Field | Original tuple | Reply tuple |
|---|---|---|
| Source | 192.168.1.20:51000 | 198.51.100.7:443 |
| Destination | 198.51.100.7:443 | 203.0.113.5:40001 |
Here the LAN host 192.168.1.20 contacts a web server. The gateway's public address is 203.0.113.5. The gateway picked source port 40001 for the outside leg. The reply from the server will arrive addressed to 203.0.113.5:40001, and the table maps it back to 192.168.1.20:51000.
Following one connection
Looking at the table
The conntrack tool lists current entries. Each line shows the original tuple first and the reply tuple second.
# Inspect the kernel's connection tracking table (NAT mappings)
sudo conntrack -L -p tcpA translated entry looks like this:
tcp 6 431999 ESTABLISHED src=192.168.1.20 dst=198.51.100.7 sport=51000 dport=443 src=198.51.100.7 dst=203.0.113.5 sport=443 dport=40001 [ASSURED]The first src, dst, sport, and dport group is the original tuple. The second group is the reply tuple. In the reply group, the destination 203.0.113.5 and port 40001 are the translated values. The number 431999 is the seconds left before the entry expires.
Entry state and timeouts
The table cannot grow forever, so entries expire.
- For TCP, the kernel follows the connection state. An established connection keeps its entry for a long time. A closed connection is removed quickly.
- UDP has no connection state. The kernel creates an entry on the first packet and keeps it for a short idle timeout. Traffic in either direction refreshes it.
If a mapping expires while the application still believes the connection is open, the next packet creates a new entry or is dropped. This is why idle sessions through NAT can silently break. Applications work around it with keepalive packets.
The table also has a size limit. A busy gateway that hits it drops new connections and logs nf_conntrack: table full, dropping packet in the kernel log.
Common errors
- Reading only the first half of a
conntrackline and missing that the reply tuple holds the translation. - Expecting an unsolicited packet to reach a private host. With no entry, the gateway has nothing to map it to, so it is dropped or handled by a port forwarding rule.
- Assuming a long-idle UDP flow still has a mapping.
Exercise
A conntrack entry reads src=10.0.0.5 dst=93.184.216.34 sport=44000 dport=80 src=93.184.216.34 dst=203.0.113.9 sport=80 dport=50123. Which host started the connection, and which public address and port did the gateway use for it?
Show answer
Host 10.0.0.5 started it, from port 44000. The first group is the original tuple. The gateway used 203.0.113.9 with source port 50123, shown as the destination in the reply tuple.
Exercise
Why can a UDP mapping expire during a quiet period even though both applications are still running?
Show answer
UDP has no connection state, so the kernel relies on an idle timeout. If no packets pass in either direction before the timeout, the entry is removed. Keepalive packets refresh it.