Sub topic 13
Getting through NAT: UPnP, STUN, hole punching, and relays
This chapter assumes you know how a NAT device keeps a translation table, the difference between source NAT and port forwarding, and the basics of UDP and TCP ports. Here you will see how two hosts that both sit behind NAT can still reach each other.
The problem
A host behind NAT has no public address and no inbound mapping. A NAT device creates a mapping only when an inside host sends traffic out. A peer on the internet therefore cannot start a connection to it. Manual port forwarding fixes this, but ordinary users cannot be asked to edit router rules. Four techniques cover the gap.
UPnP and NAT-PMP/PCP
Universal Plug and Play (UPnP) lets an application ask the router to create a port forward for it. Its Internet Gateway Device (IGD) protocol runs over the LAN. NAT-PMP and its successor PCP do the same job with a simpler protocol. The router creates the mapping, and the application can then read the external address and port.
Common errors:
- The feature is disabled on the router, which is the default on many security-conscious devices.
- The router sits behind another NAT (double NAT or CGNAT), so the mapping lands on a private address.
- Any program on the LAN can open ports, which is why many administrators turn UPnP off.
STUN: learning your public address
Session Traversal Utilities for NAT (STUN) is a small protocol. A client sends a request to a public STUN server. The server replies with the source IP address and port it saw, which is the client's mapped address on the NAT. The client can then share that address with a peer through a signaling channel, such as a chat server.
STUN reveals the mapping. It does not create a path by itself. It also only helps when the NAT reuses one mapping for every destination, as endpoint-independent mapping does. With a NAT that picks a new port per destination, the address STUN reports is useless to the peer.
UDP hole punching
Hole punching uses the fact that outbound packets create state. Both peers learn their mapped addresses (through STUN) and exchange them through a rendezvous server. Then both send UDP packets to each other at the same time.
- Peer A sends a packet to B's mapped address. A's NAT creates a mapping. B's NAT may drop the packet, since it has no matching entry.
- Peer B sends a packet to A's mapped address. B's NAT creates a mapping, and A's earlier mapping now accepts the traffic.
- Packets flow in both directions without any configuration on either router.
TCP hole punching also exists, using simultaneous open, but it is less reliable. Most real systems punch UDP.
Relays: the fallback
Hole punching fails when both sides use NATs that change the port for each destination, which is common with carrier-grade NAT and some enterprise firewalls. Then traffic goes through a relay, a public server that forwards packets between the peers. TURN (Traversal Using Relays around NAT) is the standard protocol for this. A relay always works, but it costs bandwidth and adds latency.
How ICE combines them
Interactive Connectivity Establishment (ICE) is the procedure that WebRTC uses. It gathers candidate addresses: local addresses, the STUN-mapped address, and a TURN relay address. It tests pairs of candidates and picks the best one that works. The usual order of preference is direct, then hole-punched, then relayed.
| Technique | Needs router support | Needs a public server | Works behind CGNAT |
|---|---|---|---|
| UPnP / PCP | Yes | No | No |
| STUN + hole punching | No | Yes (STUN, rendezvous) | Sometimes |
| TURN relay | No | Yes (relay) | Yes |
Exercise
Two peers both sit behind NATs. STUN shows each peer a different mapped port for every destination it contacts, and hole punching fails. Which technique still connects them, and what is the cost?
Show answer
A TURN relay. The NATs use destination-dependent mapping, so the address STUN reports for the server is not the address the peer would see. A relay is a public host that both peers can reach with outbound traffic, so it works. The cost is relay bandwidth and extra latency, because all traffic passes through the relay.