NAT TRAVERSAL

Convincing two firewalls to let packets through

L318 MINNETWORKING
PREDICT

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.

▶ SIMULATION
PEER A
host 192.168.1.10:5000
public (STUN) —
from B: nothing yet
NAT TABLES
NAT-A 203.0.113.5 (EIM mapping, addr+port filtering):
no mappings
NAT-B 198.51.100.7 (EIM mapping, addr+port filtering):
no mappings
PEER B
host 10.0.0.5:5000
public (STUN) —
from A: nothing yet
TURNnot used
packets0 peer packets sent · 0 dropped
lastboth peers behind NAT; nothing mapped yet
✓ the two peers end up connected
SCHEDULE
01[A]A → STUN: binding request; STUN replies with the address it saw
02[B]B → STUN: binding request; learns its public address
03[SIGNAL]signaling server: swap the STUN-learned (server-reflexive) addresses
04[A]A → B's public address (opens NAT-A toward B)
05[B]B → A's public address
06[A]A → B's public address again
07[SIGNAL]report
Start of hole punch: eim nats scenario. 7 steps.
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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a NAT lets in only replies. Hole punching makes both sides send at once so that each peer’s packet looks like a reply to the other’s. It depends on the port STUN saw being the same port used toward the peer. When a NAT breaks that, both peers talk outbound to a relay and pay for it in latency and bandwidth.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: TCP CONGESTION CONTROL →