Sub topic 9
Tunnels through NAT
This chapter assumes you know IP addressing and subnets, how a routing table and default route work, and the difference between TCP and UDP. It also assumes you have read the earlier chapters on encapsulation and on IPsec (ESP and IKE). Here you will see why Network Address Translation (NAT) breaks some tunnels, how NAT traversal fixes that, and how to spot the common failures.
Why NAT and tunnels clash
Network Address Translation (NAT) lets many hosts with private addresses share one public IPv4 address. The most common form is port address translation (PAT, often just called NAT). A NAT device rewrites the source address and source port of each outgoing packet. It records the rewrite in a translation table, and it uses that table to reverse the rewrite on replies.
A NAT device can only build and use table entries from fields it understands. For TCP and UDP, those fields are the addresses and the port numbers. A typical entry looks like this:
| Protocol | Inside address:port | Outside (translated) address:port | Remote address:port |
|---|---|---|---|
| UDP | 192.168.1.20:51820 | 203.0.113.5:40001 | 198.51.100.1:51820 |
Three properties of this table cause most tunnel problems:
- Entries are created by outbound traffic. An unsolicited packet from the internet matches no entry and is dropped.
- Entries expire. If a flow is idle, the entry is removed. UDP timeouts are often 30 to 120 seconds on consumer routers.
- Entries need port numbers. A protocol with no ports cannot be told apart from other flows of the same protocol.
Which tunnel protocols NAT breaks
Different tunnel types fail in different ways.
- GRE is IP protocol 47. It has no ports. A NAT device can translate the outer addresses, but many cannot track more than one GRE flow to the same destination. Some drop GRE entirely. GRE also carries no security, so it is rarely used across the public internet without IPsec.
- IPsec ESP is IP protocol 50. It has no ports either. ESP identifies a flow by a Security Parameter Index (SPI), and the NAT device does not look at it.
- IPsec AH (protocol 51) authenticates the outer IP header, including the addresses. NAT changes those addresses, so the integrity check fails. AH and NAT cannot work together.
- IPsec ESP in tunnel or transport mode has a second problem. In transport mode with TCP or UDP inside, the checksum covers a pseudo-header with the IP addresses. NAT changes the addresses, so the checksum is wrong unless the receiver fixes it.
- IKE uses UDP port 500, which NAT handles well. The trouble is that IKE can include the peer's IP address inside its payloads. If NAT changed the address, the two sides disagree about it.
- WireGuard, OpenVPN over UDP, and SSH use UDP or TCP with ports. NAT handles them as ordinary flows. Their only NAT concern is keeping the mapping alive.
NAT traversal for IPsec (NAT-T)
NAT traversal (NAT-T) is a set of extensions that lets IPsec work through NAT. It has two parts.
- Detection during IKE. Each peer sends hashes of its own IP address and port in NAT-D (NAT Discovery) payloads. The receiver hashes the address and port it actually saw. If the hashes differ, a NAT device sits on the path. The peers learn this, and which side is behind it.
- UDP encapsulation of ESP. When NAT is detected, the peers move IKE from UDP port 500 to UDP port 4500. They also wrap each ESP packet inside a UDP header on port 4500. The NAT device now sees ordinary UDP with ports, so it can build a normal table entry.
On port 4500 the receiver must tell IKE messages from ESP packets. IKE messages start with four zero bytes (a "non-ESP marker"). ESP packets start with the SPI, which is never zero.
The wrapped packet looks like this:
The extra UDP header adds 8 bytes. This adds to the tunnel overhead, so the effective MTU drops further. Chapter material on MTU applies directly: when NAT-T is active, lower the tunnel MTU or enable MSS clamping accordingly.
Keepalives and mapping timeouts
The NAT table entry exists only while traffic flows. A tunnel can be up, with valid keys, yet carry no traffic for minutes. After the NAT timeout, the entry disappears. Two things then go wrong:
- Packets from the remote side no longer match any entry and are dropped.
- Packets from the inside create a new entry, often with a different external port. The remote peer must learn the new port.
The fix is a keepalive: a small packet sent often enough to refresh the entry. IPsec NAT-T sends a one-byte UDP packet (value 0xFF) about every 20 seconds. WireGuard has an optional setting for the same purpose, shown in the client configuration below.
[Interface]
PrivateKey = <client-private-key>
Address = 10.8.0.2/24
DNS = 10.8.0.1
[Peer]
PublicKey = <server-public-key>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25PersistentKeepalive = 25 makes the client send an empty authenticated packet every 25 seconds. Choose a value below the shortest NAT timeout on the path. Without it, WireGuard sends nothing when idle, so a client behind NAT can become unreachable from the server until the client sends something. A peer with a public address does not need it.
Keepalives cost a little battery and data on mobile devices. For that reason WireGuard leaves them off by default.
Who can start the conversation
NAT is asymmetric. A host behind NAT can reach a public server, but the public server cannot start a connection to the host. This matters for tunnels.
- Remote access clients (laptops, phones) sit behind NAT and connect out to a VPN server with a public address. This works well, because the client always initiates.
- Site-to-site tunnels where both ends are behind NAT cannot start with either side as a plain server. Common solutions are a port forward on one NAT device (for example UDP 51820 or UDP 500 and 4500), a public relay, or a design where both sides connect out to a third host.
- Traffic from the server to a client works only while the mapping is alive. This is why a server may be unable to reach a client until the client's keepalive refreshes the mapping.
When a peer's address or port changes because of NAT rebinding, protocols react differently. WireGuard updates the peer's endpoint to the source address of the last valid authenticated packet, so roaming works without renegotiation. IPsec with NAT-T uses the new address after it receives a valid packet from it, or it re-runs IKE (as defined by MOBIKE if both sides support it).
Double NAT and carrier-grade NAT
A home router behind an ISP's carrier-grade NAT (CGNAT) gives you two translation layers. Outbound tunnels from a client still work. Inbound port forwards do not, because you do not control the outer NAT. Addresses in the range 100.64.0.0/10 on your router's WAN side are a sign of CGNAT.
Also avoid overlapping private ranges. If your home network and the remote network both use 192.168.1.0/24, the client cannot tell which side an address belongs to. That is a routing problem rather than a NAT problem, but it often appears at the same time.
Checking for NAT problems
Start by seeing whether packets leave and whether replies return. On a Linux client or server, watch the tunnel port:
sudo tcpdump -ni any 'udp port 51820 or udp port 4500 or udp port 500'Read the output this way:
- Packets leave, nothing returns: a firewall or NAT drops the replies, or the server cannot route back. Check the server's firewall and port forward.
- Nothing leaves: the problem is local routing, or the client has no route to the server's address.
- Works for a minute, then stalls until you send traffic: the NAT mapping expired. Add or shorten the keepalive.
- IKE completes but data does not flow (IPsec): ESP is probably blocked or NAT-T was not negotiated. Look for UDP 4500 traffic. If you see only protocol 50 packets, force UDP encapsulation in the configuration.
- The remote peer shows a different source port than expected: this is normal NAT behaviour and confirms the mapping.
To see whether a Linux NAT device holds a mapping, use conntrack:
sudo conntrack -L -p udp --dport 51820The output shows the translated addresses and the remaining timeout. If the tunnel stalls, the entry is missing or its timeout is short.
Summary of NAT behaviour by protocol
| Protocol | Transport | Works behind NAT? | What helps |
|---|---|---|---|
| GRE | IP protocol 47 | Often not | Run it inside IPsec with NAT-T, or use a UDP-based tunnel |
| IPsec ESP | IP protocol 50 | Not without NAT-T | NAT-T on UDP 4500 |
| IPsec AH | IP protocol 51 | No | Use ESP instead |
| IKE | UDP 500 then 4500 | Yes, with NAT-T | NAT-D detection |
| WireGuard | UDP | Yes | PersistentKeepalive |
| OpenVPN | UDP or TCP | Yes | Keepalive directive |
| SSH forwarding | TCP | Yes | ServerAliveInterval |
Exercise
A laptop behind a home router connects to a WireGuard server. The tunnel works for a few minutes. After the laptop is idle, pings from the server to the laptop's tunnel address fail, but pings from the laptop to the server still work and then fix the problem for a while. What is the cause, and what single setting fixes it?
Show answer
The home router's NAT mapping for the UDP flow expired during idle time, so packets from the server no longer match a table entry and are dropped. A packet from the laptop creates a fresh mapping, which is why laptop-initiated traffic restores the path. Setting PersistentKeepalive = 25 in the laptop's [Peer] section refreshes the mapping before it expires.
Exercise
Why does IPsec ESP fail through a typical NAT device, and what does NAT-T change to fix it?
Show answer
ESP is IP protocol 50 and has no port numbers, so a PAT device cannot build a per-flow table entry for it, and several clients behind one address cannot be told apart. NAT-T detects the NAT with NAT-D hashes during IKE, then moves IKE to UDP port 4500 and wraps ESP packets in a UDP header on port 4500. The NAT device sees ordinary UDP with ports and translates it normally.
Exercise
You capture traffic on a VPN server and see only IPsec protocol 50 packets arriving from a client behind NAT, and the tunnel carries no data. What should you check, and what does the extra UDP header add to the overhead?
Show answer
Check whether NAT-T was negotiated and whether UDP 4500 is allowed through the server's firewall and any port forward. Force UDP encapsulation if the peer allows it. The UDP header adds 8 bytes per packet, so lower the tunnel MTU or clamp the TCP MSS to avoid fragmentation.