NAT TRAVERSAL
Convincing two firewalls to let packets through
A and B sit behind different NATs. Each NAT keeps one public port per host whatever it sends to, but lets in only packets from an address and port that host has already sent to. A and B have learned each other's public address and port. A sends first. What happens to that first packet?
A NAT shares one public IP between many hosts. An outbound packet creates a mapping: an external port that leads back to the host and accepts replies only from the address it sent to. Unsolicited inbound packets are dropped.
To connect two peers behind NATs, each asks a STUN server for its public address and they swap addresses through a signaling server. Then both send. A’s first packet is dropped by NAT-B, but it opens NAT-A, so B’s packet gets in. That opens NAT-B for A’s next packet. This works when a NAT reuses one port for every destination. A symmetric NAT picks a new port toward the peer. If the other NAT also filters by port, as in the sim, it drops those packets and you fall back to a TURN relay. If the other NAT filters by address only, the packet gets in and ICE learns the new port as a peer-reflexive candidate. Symmetric NATs on both sides almost always need TURN.
ICE, simplified (RFC 8445)
1. gather candidates
host 192.168.1.10:5000 your own interface
srflx 203.0.113.5:40000 STUN: how the NAT maps you
relay 192.0.2.9:49152 TURN: an address on the relay
2. swap candidates over signaling
3. both sides send checks to every pair, preferring host > srflx > relay
- the first packet into a NAT is usually dropped: no mapping lists the sender
- it still opens YOUR NAT, so the other side's packet gets in
4. symmetric NAT: the port toward the peer differs from the port STUN saw.
If the peer's NAT filters by port, it never matches -> fall back to relay.
If it filters by address only, the packet gets in and ICE learns a
peer-reflexive candidate. Symmetric on both sides: almost always relay.
TURN permissions are per IP (no port), and the relay sends from a fixed
address the peer has already sent to, so both NATs see replies. Watch the NAT TABLES and the invariant strip. HOLE PUNCH: EIM NATs drops A’s first packet, then both directions arrive. SYMMETRIC NAT-B sends B’s packet from :50001, a port NAT-A has never heard of, so all three packets are dropped and the peers never connect. TURN FALLBACK keeps the same symmetric NAT and connects through the relay.
Hole punching works between these two NATs (endpoint-independent mapping, address-and-port filtering). Inject one change to NAT-B at a point where the peers end up not connected.
GOAL: A and B are not connected at the end
Write connect so two peers always end up connected by the cheapest path that works: host (same LAN), then srflx (hole punched), then relay. Return the path name. The grader checks that a packet was delivered in both directions on the path you return, across four NAT setups.
WebRTC (video calls, screen sharing), game networking, VPN meshes like Tailscale and WireGuard-based tools, and VoIP. You configure STUN and TURN servers in RTCPeerConnection's iceServers, and you budget TURN bandwidth for the share of users behind symmetric or carrier-grade NAT.
“How do two clients behind NAT connect directly?” Explain mappings and filtering, what STUN returns, why both sides send and the first packet is dropped, when a symmetric NAT breaks the punch, and how ICE orders host, server-reflexive and relay candidates with TURN as the fallback.
USED IN
Chapters that build on this lesson.
- An addressing planNetwork Engineer
- Network philosophies: simple, observable, and every change with a way backNetwork Engineer
- Why network engineers exist when the cloud hides the networkNetwork Engineer