Sub topic 9
Hairpin NAT: reaching your own forwarded service from inside
This chapter assumes you know how destination NAT (port forwarding) and source NAT (masquerading) rewrite packets, and that you have read the earlier chapters on the translation table and on Linux NAT configuration. You should be comfortable with iptables or nft and with reading conntrack output.
The problem: a forward that works from outside and fails from inside
Suppose a gateway has the public address 203.0.113.5 and the LAN 192.168.1.0/24. A web server at 192.168.1.10 listens on port 80. The gateway forwards its public port 8080 to that server. People on the internet can open http://203.0.113.5:8080/. A laptop at 192.168.1.20 on the LAN types the same URL and the connection hangs.
The cause is not a broken forward. The laptop is sending a packet to the public address, and the gateway is the only device that knows that address maps to the internal server. The trouble starts with how the reply travels.
Hairpin NAT, also called NAT loopback or NAT reflection, is the set of rules that makes this case work. The name describes the packet's path. It enters the gateway on the LAN side, is translated, and leaves through the same LAN side, like a hairpin turn.
Why the connection fails
Follow the packets without any hairpin rule, using only the port forward from the previous chapter.
- The laptop (192.168.1.20) sends a SYN to 203.0.113.5:8080.
- The packet reaches the gateway, because the laptop's default route points there.
- The
PREROUTINGDNAT rule rewrites the destination to 192.168.1.10:80. - The gateway routes the packet out of its LAN interface to the server. The source address is still 192.168.1.20.
- The server sees a request from 192.168.1.20, which is on its own subnet. It replies directly to the laptop and does not send the reply through the gateway.
- The laptop receives a SYN-ACK from 192.168.1.10:80. It was waiting for an answer from 203.0.113.5:8080. The 5-tuple does not match any connection it opened, so it discards the packet (or answers with a reset).
The gateway's translation table has an entry for the forward, but the reply never passes back through it, so the destination NAT is never reversed. The second point of failure is asymmetry: the request went through the gateway and the reply took a different path.
The fix: translate the source as well
The fix is to make the server believe the request came from the gateway. If the gateway also rewrites the source address to its own LAN address, the server replies to the gateway. The gateway then reverses both translations using its connection tracking entry and sends the reply to the laptop with the source 203.0.113.5:8080, which is what the laptop expects.
On Linux with iptables, this takes two rules:
# 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 is the DNAT rule. It matches on the destination address (-d 203.0.113.5) and not on the incoming interface (-i eth0). The earlier port forward matched -i eth0, which restricts it to packets arriving from the WAN side. A packet from the LAN arrives on a different interface, so an interface-based rule never matches it. Matching on the public destination address works for both directions.
The second rule is the source NAT. It matches only traffic from the LAN that is headed to the internal server's real port after the DNAT has been applied. Because POSTROUTING runs after the destination has been rewritten, the match uses the translated destination (192.168.1.10, port 80), not the public one. MASQUERADE rewrites the source to the address of the outgoing interface, which here is the gateway's LAN address.
What the server sees now
The server's access log shows every hairpinned request coming from the gateway's LAN address (for example 192.168.1.1), not from the real client. This is the main cost of the technique. Per-client logging, rate limiting, and access control on the server lose the real client address for LAN users.
The same idea in nftables
The hairpin rules map to nft directly. This assumes the ip nat table and its prerouting and postrouting chains from the previous chapter already exist.
# DNAT by public destination address (works for WAN and LAN clients)
sudo nft add rule ip nat prerouting ip daddr 203.0.113.5 tcp dport 8080 dnat to 192.168.1.10:80
# Masquerade LAN-to-server traffic so replies return through the gateway
sudo nft add rule ip nat postrouting ip saddr 192.168.1.0/24 ip daddr 192.168.1.10 tcp dport 80 masqueradeThe logic is the same: match the public address on the way in, then rewrite the source on the way out so the reply is forced back through the gateway.
Check that it works
Run the test from a LAN host, using the public address, and watch the table on the gateway:
# On the gateway: list tracked TCP connections and look for the hairpinned flow
sudo conntrack -L -p tcpA hairpinned connection shows two tuples. The original direction is the laptop to 203.0.113.5:8080. The reply direction is 192.168.1.10:80 back to the gateway's LAN address, not to the laptop. That reply tuple is what shows the source was rewritten. If the reply tuple names 192.168.1.20 as the destination, the source NAT rule did not match and the connection will hang.
Edge cases and gotchas
- Rule order. Both
PREROUTINGandPOSTROUTINGrules in thenattable are evaluated for the first packet of a connection only. Later packets follow the stored mapping. If an earlier rule in the same chain already accepted or translated the packet, the hairpin rule is never reached. - Client and server on the same host. If the client is the server itself, the packet may never reach the gateway at all. A connection from 192.168.1.10 to 203.0.113.5:8080 goes to the gateway, is translated back to 192.168.1.10:80, and returns to the same machine. The server then sees a source address equal to its own. The
-s 192.168.1.0/24match in the masquerade rule covers this case, because the gateway rewrites the source to its own address. - Narrow source match. The masquerade rule only matches traffic to the server on port 80. Do not widen it to all LAN traffic. A broad rule rewrites every LAN-to-LAN packet and hides real client addresses everywhere.
- Forwarding policy. Hairpin traffic enters and leaves on the same interface and passes through the
FORWARDchain. If the chain policy isDROP, add an accept rule for LAN-to-server traffic on port 80, or the packets are dropped after the NAT step. - Multiple servers. Each forwarded service needs its own matching pair of DNAT and masquerade rules.
- Reverse path filtering. Setting
rp_filterstrictly on the LAN interface can drop packets that arrive and leave on the same interface. The default loose or strict modes usually work, but checknet.ipv4.conf.all.rp_filterif the hairpin still fails.
Alternatives to hairpin NAT
Hairpin NAT is not the only way to let LAN users reach the service by name.
| Approach | How it works | Trade-off |
|---|---|---|
| Hairpin NAT | Gateway rewrites destination and source for LAN clients | Server logs show the gateway address, not the client |
| Split-horizon DNS | Internal DNS resolves the hostname to 192.168.1.10; external DNS resolves it to 203.0.113.5 | Needs a DNS server you control; clients that hard-code the public IP still fail |
| Direct internal address | Users type the private address | Easy to set up, but users must know two addresses |
Split-horizon DNS is the cleaner choice when you control the resolver and clients use hostnames. The server sees real client addresses and traffic does not pass through the gateway at all. Hairpin NAT is the right choice when clients use a fixed public address or when you cannot change DNS.
Summary of the mechanism
A forwarded service fails from inside the LAN because the server replies directly to a client it shares a subnet with. The reply then carries the wrong source and the client rejects it. Hairpin NAT adds a source rewrite so the reply returns through the gateway, where the translation table reverses both rewrites. The price is that the server loses sight of the real client address.
Exercise
A LAN client at 192.168.1.20 connects to 203.0.113.5:8080, which forwards to 192.168.1.10:80. The connection hangs. The DNAT rule uses -i eth0, where eth0 is the WAN interface. Name two problems that must be fixed, and say what the server's log will show afterward.
Show answer
First, the DNAT rule matches only packets arriving on eth0, so LAN packets never match. Change it to match the destination address (-d 203.0.113.5). Second, even with DNAT applied, the server replies directly to the client because both are on the same subnet, and the client drops that reply. Add a POSTROUTING MASQUERADE rule for traffic from 192.168.1.0/24 to 192.168.1.10 port 80 so the reply goes back through the gateway. Afterward the server's log shows the gateway's LAN address as the client, not 192.168.1.20.
Exercise
In the conntrack -L output for a hairpinned flow, which detail tells you the source NAT rule is working?
Show answer
The reply tuple. It should list the server (192.168.1.10:80) answering to the gateway's LAN address. If the reply tuple names the real client, 192.168.1.20, as the destination, the source was not rewritten and the reply bypasses the gateway.