Sub topic 9
Tunnels through NAT
This chapter assumes you know IP addressing, how a routing table works, the difference between TCP and UDP, and the basics of tunnelling and IPsec's ESP and IKE. It covers why NAT breaks some tunnels and how the common fixes work.
Why NAT is a problem for tunnels
Network address translation (NAT) lets many private hosts share one public address. A NAT device rewrites the source address and port of outgoing packets. It records the mapping in a table so replies can be translated back.
This works well for TCP and UDP, because both have port numbers the device can rewrite. Tunnel protocols often lack them:
- GRE and ESP are separate IP protocols (47 and 50). They have no ports.
- Many simple NATs can only track a few such flows, or none. Two hosts behind one NAT often cannot both use ESP to the same server.
- Authenticated packets resist rewriting. IPsec's integrity check covers fields that a NAT would change, so a translated packet can fail verification.
Inbound traffic is the other difficulty. A NAT only creates a mapping when an inside host sends a packet first. A packet arriving from outside with no mapping is dropped.
NAT traversal in IPsec
IPsec solves this with NAT traversal (NAT-T). It has three parts:
- IKE detects NAT. Each peer sends hashes of its own address and port. If the received hashes do not match what the peer sees, a NAT is in the path.
- The peers switch from UDP port 500 to UDP port 4500 for IKE.
- ESP packets are wrapped in a UDP header on port 4500. The NAT now sees ordinary UDP and can map it.
The extra UDP header costs 8 bytes. That reduces the usable MTU, which matters when you size tunnel packets.
Keepalives and mapping timeouts
A NAT mapping expires when no traffic uses it. UDP timeouts are often 30 to 120 seconds. When the mapping disappears, the NAT drops packets that the remote side sends in. The tunnel looks up but nothing passes until the inside host sends again.
The fix is to send a small packet at regular intervals. IPsec NAT-T sends keepalive packets (commonly every 20 seconds). WireGuard does this with an option you saw earlier:
[Peer]
PublicKey = <server-public-key>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25PersistentKeepalive = 25 sends a packet every 25 seconds. It is needed on the peer that sits behind NAT and must be reachable. A server with a public address does not need it.
Which side initiates
Because inbound packets need a mapping, the peer behind NAT should be the one that starts the connection. After the first packet, the NAT forwards replies. With keepalives, the server can also send to the client at any time.
If both peers are behind NAT, neither has a public endpoint. Direct connection then needs hole punching, where both sides send to each other's mapped address at the same time. Without it, you need a relay on a public host.
Other tunnel types
- TLS-based VPNs such as OpenVPN usually use one UDP port or TCP port 443. NAT handles both without extra features.
- SSH tunnels are plain TCP, so NAT is rarely a problem.
- GRE has no NAT traversal. Behind NAT it usually needs to be carried inside another tunnel, such as IPsec with NAT-T or a UDP encapsulation.
Symptoms to recognise
| Symptom | Likely cause |
|---|---|
| IKE completes but no data flows | ESP blocked or not encapsulated in UDP |
| Tunnel works, then stops after idle time | NAT mapping expired, no keepalive |
| Works for one client, fails for a second | NAT cannot map two ESP flows to one server |
| Server cannot reach client, client reaches server | Inbound traffic has no NAT mapping |
Exercise
A laptop behind a home router connects to a WireGuard server with a public address. The tunnel works, but after ten idle minutes the server's pings to the laptop fail until the laptop sends something. What is wrong, and what is the fix?
Show answer
The router's NAT mapping for the UDP flow expired, so the server's packets have no mapping to follow. Add PersistentKeepalive = 25 to the laptop's peer section so it sends a packet before the mapping times out.