Sub topic 9
Hairpin NAT: reaching your own forwarded service from inside
This chapter assumes you know how a port forward works (destination NAT on the gateway), how masquerading rewrites source addresses, and how to read basic iptables rules.
The problem
You forward 203.0.113.5:8080 to a web server at 192.168.1.10:80. From the internet, it works. From a laptop on the same LAN, http://203.0.113.5:8080/ hangs or fails.
The cause is that the LAN client's packet reaches the gateway, gets its destination rewritten by DNAT, and is then sent back out the same LAN interface to a host on the same subnet. The packet "hairpins": it enters and leaves through the same side of the gateway.
Why the connection fails
Suppose the client is 192.168.1.20. Only the destination is rewritten:
- The client sends
192.168.1.20:50000 -> 203.0.113.5:8080. - The gateway applies DNAT and forwards
192.168.1.20:50000 -> 192.168.1.10:80. - The server sees a client on its own subnet. It replies directly to
192.168.1.20, and the reply skips the gateway. - The client receives a packet from
192.168.1.10:80. It was waiting for a reply from203.0.113.5:8080, so it drops the packet.
The gateway never sees the reply, so it cannot undo the DNAT translation. The client's TCP handshake never completes.
The fix: also rewrite the source
The gateway must force the reply to come back through it. It does this by applying source NAT to hairpinned traffic, so the server sees the gateway's LAN address as the client.
# Hairpin NAT: let LAN clients use the public address 203.0.113.5:8080
sudo iptables -t nat -A PREROUTING -d 203.0.113.5 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -d 192.168.1.10 -p tcp --dport 80 -j MASQUERADEThe first rule matches on the destination address 203.0.113.5 instead of the incoming interface. LAN traffic does not arrive on eth0, so an -i eth0 match would never fire for it. The second rule matches LAN clients talking to the server after DNAT and masquerades them.
Now the flow works:
- The client sends
192.168.1.20:50000 -> 203.0.113.5:8080. - The gateway rewrites it to
192.168.1.1:51000 -> 192.168.1.10:80. - The server replies to
192.168.1.1:51000, which is the gateway. - The gateway reverses both translations using its connection tracking entry and sends
203.0.113.5:8080 -> 192.168.1.20:50000to the client.
Trade-offs
Hairpin NAT has a cost. The server logs show the gateway's address for every LAN client, so per-client logging and access control on the server stop working for internal traffic.
An alternative is split-horizon DNS. Internal clients resolve the service's name to 192.168.1.10 directly, so no hairpin is needed and the server sees real client addresses. This only works when users connect by hostname rather than a literal public IP. It also needs the port to match, because the internal address uses port 80 and not 8080.
Checking the result
Run conntrack -L -p tcp on the gateway. A working hairpin connection shows an original tuple with the public destination and a reply tuple with the gateway's LAN address as the peer.
Exercise
A LAN client connects to the forwarded public address and hangs, although outside clients succeed. Packet captures on the client show a SYN-ACK arriving from 192.168.1.10 instead of 203.0.113.5. What is happening, and what rule fixes it?
Show answer
The gateway applies DNAT but not source NAT, so the server replies directly to the client and bypasses the gateway. The client drops the SYN-ACK because its source address is not the one it contacted. Add a POSTROUTING MASQUERADE (or SNAT) rule for LAN-sourced traffic going to 192.168.1.10:80, so replies return through the gateway and are translated back.