VPNs
NAT traversal
Getting traffic through NAT: wrapping protocols in UDP, whose ports NAT can map, and punching holes between peers.
NAT traversal is getting traffic through a NAT that would otherwise break it. NAT maps ports, and ESP has none, so IPsec wraps ESP in UDP 4500 (RFC 3948) when IKE detects a NAT on the way. Two peers both behind NAT meet by hole punching, helped by STUN, or through a TURN relay when that fails. Keepalives every 20 seconds or so stop the NAT's mapping expiring.
How NAT traversal works, step by step
Two devices each behind NAT cannot be called from outside: the router drops any packet
that matches no mapping. NAT traversal is how video calls, games and peer-to-peer apps meet anyway. Follow the
NAT traversal lab, where linaLaptop (192.168.60.10) is behind linaRouter and
omarLaptop (192.168.70.10) behind omarRouter, both facing an internet network 198.51.100.0/24 with a
meetingPoint server at 198.51.100.50:
- Learn your public address. linaLaptop sends a UDP datagram from port 4100 to the server, which replies with
the address and port it arrived as:
198.51.100.11 4100, linaRouter's outside address. This is what a STUN server does (RFC 8489): it reports the server-reflexive address. omarLaptop learns it is198.51.100.22 4200. - Swap addresses. Each side passes its answer to the other through the app's own server. But knowing Omar's
address is not enough: a packet Lina sends to
198.51.100.22:4200cold is dropped, because omarRouter has no mapping expecting it. - Punch the hole. Both keep sending to each other. Omar's first datagrams die at linaRouter, but on their way out
they opened a mapping on omarRouter expecting replies from
198.51.100.11:4100. Lina's datagram does the mirror. Once each router has seen an outbound packet, the other's packets match an established mapping and get in. This is hole punching, and it works because these NATs keep endpoint-independent mapping (RFC 4787): one outside port per inside port, whatever the destination. - Keep it open and fall back. A UDP mapping expires when idle, so peers send keepalives every 15 to 25 seconds. When a NAT refuses to cooperate, a TURN relay forwards the media, and ICE (RFC 8445) tries every candidate and keeps the pair that works. IPsec uses the same idea: NAT-T wraps ESP in UDP 4500, in the IPsec tunnel lab.
What it looks like on a Linux machine
Asking the server what you look like from outside, with nc (the lab's server prints plain text; real STUN
is binary):
$ echo "who am I?" | nc -u -w 1 -p 4100 198.51.100.50 3478
198.51.100.11 4100
Lina is seen as her router's address 198.51.100.11, port 4100. On the router, conntrack shows the mapping, the
reply half written as the router expects it:
$ conntrack -L -p udp
udp 17 113 src=192.168.60.10 dst=198.51.100.50 sport=4100 dport=3478 src=198.51.100.50 dst=198.51.100.11 sport=3478 dport=4100 [ASSURED] mark=0 use=1
After both sides send, the hole-punch mapping to Omar appears:
$ conntrack -L -p udp --dport 4200
udp 17 117 src=192.168.60.10 dst=198.51.100.22 sport=4100 dport=4200 src=198.51.100.22 dst=198.51.100.11 sport=4200 dport=4100 [ASSURED] mark=0 use=1
The number after 17 is the seconds left. On Linux, an unanswered UDP flow gets 30, and a flow with traffic both ways
gets 120 after a couple of seconds.
The lab then turns omarRouter into an endpoint-dependent NAT (symmetric NAT) with MASQUERADE --random: Omar gets a
different outside port for each destination, so the port the server reported is already wrong and the punch fails.
What goes wrong, and how you see it
Symmetric NAT defeats the punch. With endpoint-dependent mapping the public port differs per
destination, so the peer sends to a port that was never opened. In conntrack -L -p udp -s 192.168.70.10 on Omar's
router, the entries show different dport= on their reply halves. The fix is a TURN relay; ICE picks it
automatically. See NAT mapping.
You sent once and stopped. A one-sided packet reaches the far router and is dropped, because no mapping expects it; a capture on the peer shows nothing arriving. Both sides must keep sending until one gets through, which is why hole punching needs a signalling step first.
The mapping expired. With no keepalive the UDP entry times out after 30 seconds and the next packet is dropped as new. Media stops; sending a keepalive within the timeout holds the entry open.
IPsec ESP cannot cross NAT. ESP has no ports for NAT to map, so it is dropped. IKE detects NAT and switches to
UDP 4500 (NAT-T); if ip xfrm state shows no encap, forcing UDP encapsulation fixes it.
Questions people ask
What is the difference between STUN and TURN? STUN only tells a peer its public address so the two can talk directly; TURN relays the traffic through a server when a direct path is impossible. TURN always works but costs the relay's bandwidth, so ICE prefers STUN.
Why does hole punching fail on some networks? Symmetric (endpoint-dependent) NAT gives a new port per destination, so the address a peer learns is stale. Some office firewalls and provider NATs behave this way; the answer is a relay.
Is NAT traversal a security hole? No. The peer still had to send out first to open its own mapping; nothing unsolicited gets in. It uses the stateful rule, it does not bypass it.
Read next
- IPsec Encryption at the IP layer: IKE agrees keys between two gateways, ESP carries their traffic encrypted.
- NAT A router rewriting addresses as packets pass, usually to hide many private devices behind one address.
- VPN Packets wrapped, encrypted, in UDP to a VPN server, which unwraps them on the far side.
- UDP hole punching Two peers behind NAT sending to each other at once, so each router takes the other's packets for replies.
- STUN A server that tells a device behind NAT the public address and port its packets arrive from.
- NAT mapping and filtering Whether a NAT keeps one public port per inside port (friendly to peer-to-peer) or picks one per destination.