December 5, 2026

Hairpin NAT Explained: Why a Port Forward Works From Outside but Not From Your Own Wi-Fi

A router with an ethernet cable looping out of one port and back into another

A port forward gets set up for a home server, a friend on another network confirms it loads, and then the same address typed on a laptop sitting three meters from the router times out. Nothing is broken in the usual sense. The router is being asked to do something different from what a port forward does, a trick called hairpinning, NAT loopback or NAT reflection depending on the vendor, and when it is missing or half-configured the connection fails in a very specific way. A small test network built from Linux network namespaces, with a laptop, a NAS, a router and an “internet” host, shows every stage of that failure packet by packet.

Video: MikroTik on YouTube

The setup and the symptom

Four machines, one router

The test network used addresses from the documentation ranges. The router had a LAN address of 192.168.1.1 and a public address of 203.0.113.2. Behind it sat a NAS at 192.168.1.10 running a tiny web server on port 8000, and a laptop at 192.168.1.20. Outside, an “internet” host at 203.0.113.50 stood in for a phone on mobile data. The web server replied with one line naming the address the request appeared to come from, which turns out to be the most useful diagnostic in the whole exercise.

The router did ordinary home-router things: outbound traffic was masqueraded behind 203.0.113.2, and one port forward (a DNAT rule) sent TCP port 8000 arriving on the WAN interface to the NAS.

What worked and what did not

From the internet host, curl http://203.0.113.2:8000/ returned hello from nas, you came from 203.0.113.50. The forward worked. From the laptop, the private address 192.168.1.10:8000 also worked. The public address from the laptop failed immediately with curl exit code 7, “connection refused.”

That refusal is the first clue. The forward rule only matched packets arriving on the WAN interface, so a packet from the laptop to 203.0.113.2 arrived on the LAN side, matched nothing, and was treated as a connection to the router itself. The router had nothing listening on port 8000 and said so. Many consumer routers behave the same way, which is why the earlier x9m8 guide to static addresses and DHCP reservations is often step one before any of this: the forward needs a stable target, but a stable target alone does not make the public address reachable from inside.

A computer case with a blue light inside of it

Half a fix: redirecting LAN traffic

Adding the obvious rule

The intuitive repair is a second DNAT rule: if a packet from the LAN is addressed to 203.0.113.2 port 8000, rewrite the destination to 192.168.1.10. With that rule added, the laptop’s request no longer got refused. It hung for four seconds and timed out with exit code 28.

Watching it fail

A packet capture on the laptop and the NAS at the same moment showed exactly why. On the laptop’s interface:

192.168.1.20.54752 > 203.0.113.2.8000: Flags [S]
192.168.1.10.8000 > 192.168.1.20.54752: Flags [S.]
192.168.1.20.54752 > 192.168.1.10.8000: Flags [R]

The laptop sent its connection request (SYN) to the public address. The router rewrote the destination and passed it to the NAS. The NAS saw a request from 192.168.1.20, a machine on its own subnet, so it answered directly over the local network without involving the router. The laptop then received a SYN-ACK from 192.168.1.10, an address it had never contacted, and answered with a reset. It retried, the same thing happened, and curl eventually gave up.

The router’s connection-tracking table recorded the half-open attempt as SYN_SENT and [UNREPLIED]: it rewrote the outgoing half of the conversation and never saw the return half at all.

MikroTik’s RouterOS documentation describes the identical failure in its NAT manual: the server “does not send the reply back to the router but sends it back directly” to the client, which then discards it because it expected the reply from the public address. MikroTik NAT documentation

The full hairpin

Rewriting the source too

The fix is to make the NAS answer through the router. That means rewriting the source address of hairpinned packets as well as the destination, so the NAS thinks the request came from the router. MikroTik’s example adds a masquerade rule matching LAN traffic headed for the server, and the equivalent Linux rule in the test was:

iptables -t nat -A POSTROUTING -o r0 -s 192.168.1.0/24 \
  -d 192.168.1.10 -p tcp --dport 8000 -j MASQUERADE

With both rules in place, the laptop’s request to the public address returned hello from nas, you came from 192.168.1.1. The connection-tracking entry now showed the full round trip, laptop to public address one way, NAS to router the other, marked ASSURED.

Two kinds of hairpin

The standards documents spell out what a well-behaved NAT is supposed to do here. RFC 4787, the IETF’s behavioral requirements for NAT handling of UDP, published in 2007 as BCP 127, defines hairpinning as a NAT that lets “two endpoints on the internal side of the NAT” reach each other using external addresses. Its REQ-9 says a NAT must support it, and that the hairpinned packet must carry the external source address and port. RFC 4787 RFC 5382, the 2008 companion for TCP (BCP 142), carries the same requirement as REQ-8. RFC 5382

The masquerade rule above is the other flavor: it uses the router’s internal address. Swapping it for a rule that rewrites the source to the public address, -j SNAT --to-source 203.0.113.2, produced hello from nas, you came from 203.0.113.2, which is what the RFCs ask for. In the test both versions delivered the page. The difference shows up in the server’s view of the world, which matters more than it first appears.

A close up of a stopwatch on a car

What the server sees afterwards

Every LAN visitor looks the same

With either form of hairpin, the NAS no longer knew which machine on the LAN was talking to it. Every request arrived from 192.168.1.1, or from 203.0.113.2 in the RFC-style version. For a media server or a photo library that may not matter. For anything that keeps access logs, applies per-address rate limits, or trusts “local” clients more than remote ones, it changes behavior.

Local traffic can look external

The RFC-preferred external source has its own twist. A service that treats requests from 192.168.0.0/16 as trusted would see LAN clients arriving from the public address and treat them as outsiders. The internal-source version has the reverse problem: every client, including ones that started out on the LAN by accident, shows up as the router. Neither version preserves the real client address, because preserving it is precisely what broke the half-fix.

The NAS calling itself

One more case worked once the full hairpin was in place: the NAS requesting its own public address got an answer, appearing to come from 203.0.113.2. That matters for self-hosted apps that check their own public URL during setup, which otherwise fail on a network without loopback even though the server is healthy.

How consumer and prosumer routers handle it

OpenWrt

OpenWrt turns reflection on by default. Its firewall configuration reference lists a reflection option on port-forward (redirect) sections with a default of 1, described as activating NAT reflection for that redirect, and a reflection_src option defaulting to internal, which selects which interface’s address is used as the source of reflected packets. OpenWrt firewall configuration In other words, OpenWrt’s default behaves like the masquerade version in the test, not the RFC’s external-source version, unless the option is changed.

pfSense

Netgate’s pfSense documentation calls this NAT reflection, described as “the ability to access external services from the internal network.” It offers a “NAT + Proxy” mode that uses a helper program and works only for TCP, a “Pure NAT” mode that uses only firewall rules and handles all port-forward protocols, and a Disable setting that still allows reflection per rule. pfSense NAT reflection The documentation also says plainly which approach it prefers, discussed below.

MikroTik and others

RouterOS has no switch for it; the administrator adds the source NAT rule by hand, as in the manual’s example. When a home router simply refuses connections to its own public address from inside, that is usually a router with no loopback support at all, and no rule entered on the device will add it.

A blue suitcase sitting on top of a wooden floor

The usually better answer: split DNS

Skip the round trip

The pfSense documentation states that “the best practice alternative to NAT reflection is deploying a split DNS infrastructure.” The idea is simple. Outside the house, nas.example.com resolves to the public address and the port forward handles it. Inside the house, a local DNS server answers the same name with 192.168.1.10, so the laptop connects straight to the NAS and the router never touches the traffic. pfSense notes one limitation: the internal and external port numbers must match, because DNS can change the address but not the port.

In the test network this is case B from the start: the laptop reaching 192.168.1.10 directly worked first time, and the NAS logged the laptop’s real address, 192.168.1.20.

Doing it at home

Any local DNS server can provide the internal answer. The dnsmasq manual describes its --address=/domain/ip option this way: it will “return for any host in the given domains” the specified address, and queries for those names are never forwarded upstream. dnsmasq manual A single line such as address=/nas.example.com/192.168.1.10 covers it. Networks already running a filtering resolver, like the setup in the x9m8 guide to Pi-hole DNS blocking, have a natural place for that override.

Where split DNS falls short

Split DNS only works when clients use a name. A link that contains the raw public IP, or an app that has the address hard-coded, still needs hairpinning. Devices that ignore the network’s DNS server, for example by using DNS-over-HTTPS to a public resolver, will get the public answer and hit the same timeout. A laptop that moves between home and elsewhere may also keep a cached answer from the other network for a short while. The common home answer is to run both: split DNS for everything that uses a name, and reflection switched on as a fallback.

One more catch with hand-written rules

The hairpin rule in the test matched the destination 203.0.113.2 explicitly. On a home connection whose public address changes, a hand-written rule like that silently stops matching after the next address change, and the timeout returns with no configuration change to explain it. When hairpinning stops working after a modem restart or an outage, checking that the rule still names the current public address is the first thing to look at.

Sources

  • MikroTik RouterOS documentation, NAT (Hairpin NAT section): https://help.mikrotik.com/docs/spaces/ROS/pages/3211299/NAT
  • RFC 4787, Network Address Translation (NAT) Behavioral Requirements for Unicast UDP (BCP 127), section 6: https://www.rfc-editor.org/rfc/rfc4787.html
  • RFC 5382, NAT Behavioral Requirements for TCP (BCP 142), section 7.2: https://www.rfc-editor.org/rfc/rfc5382.html
  • OpenWrt, Firewall configuration /etc/config/firewall (Redirects options): https://openwrt.org/docs/guide-user/firewall/firewall_configuration
  • Netgate pfSense documentation, NAT Reflection: https://docs.netgate.com/pfsense/en/latest/nat/reflection.html
  • dnsmasq manual page: https://thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html

910 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *