How NAT uses state, and why it breaks assumptions
By the end of this module you will be able to:
- Explain what NAT changes, why it requires state, and how return traffic is matched
- Describe the end-to-end principle and how NAT breaks it
- Explain NAT traversal techniques (STUN, TURN, ICE) and why peer-to-peer applications need them
- Say why NAT is not a firewall, and what an IPv6 network uses instead to reach the same default-deny position
Real-world problem · Ongoing
Carrier-grade NAT: when your ISP gives you a shared IP address you cannot control
Carrier-grade NAT (CGN or CGNAT) is performed by an Internet Service Provider (ISP) or mobile carrier rather than a home router. The ISP assigns a single public IP address to multiple customers simultaneously. All traffic from those customers reaches the internet from the same source IP, with different numbers distinguishing each customer's connections.
RFC 6888 documents the requirements and implications of CGNAT. The practical consequences for developers include broken IP geolocation (the source IP is the ISP's NAT device in a data centre, not the customer's location), broken rate limiting by IP (one IP serves thousands of customers), and broken services that assume a persistent 1:1 mapping between user and IP address.
For end users, CGNAT makes inbound connections impossible, breaks certain gaming services, and can make port forwarding unavailable. addresses this problem by providing enough addresses for every device to have its own public address, removing the need for NAT entirely. But IPv4 CGNAT is standard on most mobile networks and many residential broadband connections globally.
A developer reports that their application's IP geolocation feature shows wrong results for customers on certain mobile carriers. All those customers show the same IP address, and it is located in a data centre, not their city. What is happening?
NAT is stateful translation, not universal reachability
The direct attempt is blocked because neither peer knows anything but a private address, so STUN has to supply the public mapping before hole punching can work, and where symmetric NAT defeats that the TURN relay carries every byte in both directions.
Two peers behind NAT cannot connect by guessing each other's address. STUN reveals the reflexive address, hole-punching opens the path, TURN relays when nothing else works, ICE chooses.
16.1 Why NAT exists: the IPv4 address exhaustion problem
IPv4 uses 32-bit addresses, providing approximately 4.3 billion unique addresses. That sounded sufficient in 1981. By the early 1990s, it was clear it would not be enough for the growing internet. NAT (Network Address Translation) was one solution: allow many devices to share a single public IP address.
RFC 3022 defines traditional NAT. The idea is straightforward. An organisation with one public IP address can have hundreds of internal devices using private address ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 as defined in RFC 1918). The NAT device rewrites source addresses on outgoing packets and reverses the process for incoming traffic.
The rewriting creates an obligation: the NAT device must track every active translation so it knows where to send the return traffic. This is the state requirement. NAT is not stateless translation. It is translation plus a per-connection mapping table.
That mapping is created and read differently depending on which way the packet is travelling and on whether ports are rewritten alongside addresses. Those two questions are what separate the three forms of NAT you will meet by name.
16.2 SNAT, DNAT, and port address translation
SNAT (Source NAT) rewrites the source address of outgoing packets. A device at 192.168.1.5 sends a packet; the NAT device rewrites the source to 203.0.113.1 (the public IP) before forwarding it. Return traffic to 203.0.113.1 is rewritten back to 192.168.1.5.
DNAT (Destination NAT) rewrites the destination address of incoming packets. Traffic arriving for 203.0.113.1:80 is rewritten to 192.168.1.10:80 to reach a web server on the internal network. Port forwarding is DNAT. Load balancers often use DNAT to distribute traffic across backend servers.
, also called NAPT (Network Address Port Translation), allows many internal addresses to share one public IP by using different source port numbers to distinguish connections. This is what home routers do. Device A at 192.168.1.2 and Device B at 192.168.1.3 both reach the internet from the same public IP, but their connections use different source port numbers so the NAT device can tell return traffic apart.
SNAT, DNAT and PAT all depend on the same record. Nothing in a returning packet says which internal host it belongs to, so unless the device wrote down the translation when the connection started, the reply has nowhere to go.
PAT works because the NAT owns a state table
Laptop and Phone both chose source port 53144, so the only thing telling their return traffic apart is the public port the gateway assigned, 62000 against 62001, and a reply can be delivered only while that row is still in the table.
PAT works because the gateway remembers which translated port belongs to which internal host:port. Without that state table, return traffic has nowhere to go.
16.3 The NAT state table
That record is the . For every active connection through the NAT device, an entry holds the original source IP and port, the translated source IP and port, and the destination IP and port. Return traffic is matched against this table: incoming packets with a destination matching a translated entry are rewritten back to the original source.
Each entry has a timer. When no matching traffic is seen for the timeout period, the entry is removed. The timeout varies by protocol. TCP connections have longer timeouts than UDP flows. This creates intermittent failures: a long-lived TCP connection that is idle for longer than the NAT timeout will have its state entry removed. When the connection becomes active again, return traffic has no entry to match and is dropped.
This is why applications that maintain long-lived idle connections, such as SSH sessions, sometimes drop silently after a period of inactivity. The connection appears open on both ends but the NAT state has expired. TCP keepalives or application-level heartbeats prevent this by sending periodic small packets to refresh the NAT entry.
Keepalives treat the symptom. The deeper consequence of holding the mapping is that an inside device has no address the outside world can use on its own initiative, and that changes what the network as a whole promises.
When NAT state expires, the return path disappears
Only traffic refreshes the gateway timer, and silence alone removes the NAT table entry while both endpoints still believe the connection is open. The next reply matches no row and is dropped without notice, so only a keep-alive inside the idle window holds the return path open.
A NAT entry has a timer. When the connection goes idle past the timeout, the entry is removed. The next reply packet matches nothing and is silently dropped.
16.4 How NAT breaks the end-to-end principle
The end-to-end principle (described in the original internet architecture work by Saltzer, Reed, and Clark in 1984) holds that intelligence should be placed at the endpoints of a network, not in the middle. Each device should be directly addressable and reachable. The network should move packets, not make decisions about them.
NAT violates this. An internal device's address is not globally routable. It cannot receive unsolicited connections. Two devices behind different NATs cannot directly communicate without one initiating to the other's public address, or without a third-party relay.
Peer-to-peer applications (video calls, file sharing, gaming) break under NAT because both peers may be behind NAT and neither can directly receive a connection from the other. This is the NAT traversal problem.
16.5 NAT traversal: STUN, TURN, and ICE
STUN (Session Traversal Utilities for NAT), defined in RFC 8489, which obsoleted the earlier RFC 5389, is a protocol that allows a client to discover its public IP address and port as seen from outside its NAT. The client sends a request to a public STUN server; the response tells the client what source IP and port the server observed. The client can then share this "reflexive" address with peers.
TURN (Traversal Using Relays around NAT) is a fallback when direct connection is impossible. When both peers are behind NATs that block hole-punching, a TURN server relays all traffic between them. TURN works in every NAT configuration but adds latency and bandwidth cost.
ICE (Interactive Connectivity Establishment), defined in RFC 8445, is the framework that coordinates candidate gathering and connectivity checks. An ICE agent gathers multiple candidate addresses (local, STUN-discovered, TURN relay), shares them with the peer, then tests each pair to find the best working path. WebRTC, which powers most web-based video calling, uses ICE.
One case catches people out. When two devices behind the same NAT reach each other using the public address, their traffic has to leave for the NAT, turn around and come straight back in. That is , and RFC 4787 requires a NAT to support it. A device that does not breaks peer-to-peer applications only between users on the same office or home network, which is why the fault report tends to arrive as "it works with everyone except the person next to me".
16.6 Why NAT is not a firewall
NAT is address sharing. An unsolicited inbound packet is dropped because there is no state entry telling the device which internal host it belongs to, not because a policy examined it and decided against it. The drop is a consequence of how translation works.
RFC 4787 is where that behaviour is specified, and it is a behavioural document rather than a protective one. It sets requirements for how mappings and filtering behave for unicast UDP so that applications can predict them, and it is explicit that the mapping choices it defines make no difference to the security properties of the NAT, which are settled by which packets the device allows in. Citing it as evidence that NAT protects you inverts what it says.
What NAT withholds is as important as what it does. It authenticates nobody, it carries no policy you can read or review, and it inspects nothing inside the connections it does permit, including the outbound connection that malware on an inside host opens for itself. On a typical home gateway the same box also runs a stateful firewall, and that firewall is doing the work people credit to NAT.
IPv6 reaches the same default-deny position with no translation at all: every device holds a globally routable address, and a stateful firewall at the edge admits inbound packets belonging to a connection that started inside and drops everything else. The security property is identical, the addresses stay end-to-end, and the policy is now something an operator can read, log against and change deliberately.
Common misconception
“NAT is a firewall.”
NAT is address sharing. It drops unsolicited inbound traffic because no translation state exists for it, which is a side effect of keeping state rather than a security policy. RFC 4787 defines how that behaviour must work, not what it protects, and it says plainly that its mapping choices make no difference to the security properties of the device. NAT gives you no authentication, no access-control policy and no view of what a permitted connection carries, so a network that relies on it as its security boundary has no boundary it can describe.
Two devices behind the same NAT both connect to the same web server on port 443. How does the NAT device distinguish the return traffic?
An SSH session to a remote server stops responding after 30 minutes of inactivity. The connection appears still open on both sides. What is the most likely cause?
Core distinctions
- NAT rewrites IP addresses (and ports in PAT) in packets and maintains a state table to match return traffic. State entries expire when idle.
- SNAT rewrites source addresses on outbound traffic. DNAT rewrites destination addresses for inbound traffic. PAT allows many devices to share one IP using different port numbers.
- NAT breaks the end-to-end principle. Internal devices cannot receive unsolicited inbound connections. Peer-to-peer applications require NAT traversal techniques (STUN, TURN, ICE), and same-network peers additionally need the NAT to hairpin.
- NAT is not a firewall. Unsolicited inbound traffic is dropped for want of a state entry, not by policy. RFC 4787 specifies that behaviour, not protection.
- An IPv6 network reaches the same default-deny position with a stateful firewall and no translation, on addresses that stay end-to-end.
Standards and sources cited in this module
RFC 3022, Traditional IP Network Address Translator (Traditional NAT)
Section 2, Terminology; Section 4, Traditional NAT
Defines traditional NAT types and the stateful mapping requirement. Quoted in Section 16.2 for the uni-directional session design.
RFC 4787 (BCP 127), NAT Behavioral Requirements for Unicast UDP
Section 4, Mapping behaviour; Section 5, Filtering behaviour; Section 6, Hairpinning
The behavioural specification for NAT, and the source for Section 16.6: it defines mapping, filtering and hairpinning requirements and states that those mapping choices do not determine the security properties of the device.
RFC 8489, Session Traversal Utilities for NAT (STUN)
The current STUN specification, which obsoletes RFC 5389. Defines STUN and the reflexive address discovery mechanism used in the NAT traversal description.
RFC 6888, Common Requirements for Carrier-Grade NATs (CGNs)
Section 3, Requirements; Section 4, Logging Requirements
Defines CGNAT requirements and documents operational problems. Used in the opening case study for the geolocation and rate limiting consequences.
RFC 8445, Interactive Connectivity Establishment (ICE)
Section 2, Overview; Section 5, Gathering Candidates
Defines the ICE framework used in WebRTC. Referenced in Section 16.5 for the candidate gathering and connectivity check description.
NAT keeps IPv4 working by sharing addresses and remembering who started each conversation. It reads nothing, protects nothing and authenticates nobody, which leaves two questions open for the rest of this stage: how a network retires the address shortage that made translation necessary, and what has to be applied to the data itself once the addressing is settled.
Module 18 of 45 · Applied stage