I have a small network with a router, which maintains a connection to Internet, a server and some workstations in a local network.

Network map

Server is meant to be accessed from the Internet, and there are several DNAT entries set in the router iptables, like this:

-A PREROUTING -i ppp0 -p tcp -m multiport --dports 22,25,80,443 -j DNAT --to-destination

External packets come to router via ppp0 interface, and internal ones go from br-lan, which actually includes the switch and WLAN adapter. The problem is, while external access works fine, trying to access the server from inside the LAN by a DNS-resolved external IP (assigned to ppp0) fails.

The only solution I was able to invent is to add static entries to router's /etc/hosts pointing to the internal IP, but as there are no wildcards (and I have at least three top-level domains assigned to that system, not counting tens of subdomains), that's rather crunchy and failure-prone. Can you suggest something better?

I've only found this question, which was not very helpful.

If that's relevant, the router runs OpenWRT 10.03 Kamikaze with dnsmasq.

  • What version of OpenWRT? – Corey S. Nov 23 '10 at 12:44
  • @Corey 10.03 Stable, but that has nothing to do with openwrt itself, isn't it? – whitequark Nov 23 '10 at 16:44

I am surprised that after almost 8 years, nobody has explained how to do this the correct way using the UCI configuration system used by default in OpenWRT.

Steven Monday's answer is correct, yet it is using iptables commands directly, which is a lower layer than the UCI configuration system, and is best left untouched by most OpenWRT users if possible.

The correct way to access internal servers through their public IP/port combos from another internal host in UCI is by enabling the configuration option reflection under each specific DNAT target in the file /etc/config/firewall. This behavior is documented here.

For example:

config redirect option target 'DNAT' option src 'wan' option dest 'lan' option proto 'tcp' option src_dport '44322' option dest_ip '' option dest_port '443' option name 'apache HTTPS server' option reflection '1'

Note: According to the indicated OpenWRT documentation, reflection is enabled by default. In my testing, this was not the case.


I deleted my original answer, because I wasn't fully confident that it was correct. I have since had some time to set up a little virtual network of VMs to simulate the network in question. Here is the set of firewall rules that worked for me (in iptables-save format, for the nat table only):

-A PREROUTING -d -p tcp -m multiport --dports 22,25,80,443 -j DNAT --to-destination
-A POSTROUTING -s -d -p tcp -m multiport --dports 22,25,80,443 -j MASQUERADE

The first POSTROUTING rule is a straightforward way of sharing the internet connection with the LAN. I left it there for completeness.

The PREROUTING rule and the second POSTROUTING rule together establish the appropriate NATs, so that connections to the server via the external IP address can happen, regardless of whether the connections originate from outside or from inside the LAN. When clients on the LAN connect to the server via the external IP address, the server sees the connections as coming from the router's internal IP address (

Interestingly, it turns out that there are a couple of variations of the second POSTROUTING rule that also work. If the target is changed to -j SNAT --to-source, the effect is (not surprisingly) the same as the MASQUERADE: the server sees connections from local LAN clients as originating from the router's internal IP address. On the other hand, if the target is changed to -j SNAT --to-source, then the NATs still work, but this time the server sees connections from local LAN clients as originating from the router's external IP address (

Finally, note that your original PREROUTING/DNAT rule with -i ppp0 does not work, because the rule never matches packets coming from the LAN clients (since those don't enter the router via the ppp0 interface). It would be possible to make it work by adding a second PREROUTING rule just for the internal LAN clients, but it would be inelegant (IMO) and would still need to refer explicitly to the external IP address.

Now, even after having laid out a "hairpin NAT" (or "NAT loopback", or "NAT reflection", or whatever one prefers to call it) solution in full detail, I still believe that a split-horizon DNS solution---with external clients resolving to the external IP and internal clients resolving to the internal IP---would be the more advisable route to take. Why? Because more people understand how DNS works than understand how NAT works, and a big part of building good systems is choosing to use parts that are maintainable. A DNS setup is more likely to be understood, and thus correctly maintained, than an arcane NAT setup (IMO, of course).

  • This works perfectly, thank you a lot! I agree that DNS setup is better, but you cannot forward different ports on same external IP to different machines on LAN with it. Anyway, I'm the only one who will ever maintain this setup, so it's fine. – whitequark Nov 24 '10 at 6:25
  • I'm glad to hear this worked for you! – Steven Monday Nov 24 '10 at 6:35
  • What is "IMO"? International Meteor Organization www.imo.net? – Jonathan Komar Apr 2 '17 at 20:39
  • @macmadness86 IMO == In My Opinion – Steven Monday Apr 15 '17 at 0:04

A common solution is to point your internal hosts at a local DNS server that returns the correct "internal" address for these hostnames.

Another solution -- and we're using this where I work on our Cisco firewalls -- is to rewrite DNS responses on the firewall that correspond to these addresses. I don't think there are tools for Linux that do this right now.

You should be able to configure the routing on your gateway to do the right thing. You may need to configure the servers to be aware of their externally mappped ip address (e.g., by assigning it to a dummy interface). With this configuration, communication from one internal system to another internal system -- using it's "external" address -- would go through the router.

  • Hmm. So are you suggesting adding the external IP to servers' interfaces and then configuring router so it will forward all packets to the external IP coming from inside the LAN to that server? Interesting, I'll test it soon. – whitequark Nov 23 '10 at 17:30
  • Can you suggest the configuration? I tried this: ip rule add to dev br-lan table 10; ip route add via dev br-lan table 10, and it isn't working. – whitequark Nov 24 '10 at 5:40
  • What's in routing table 10? On the internal servers, you probably want them to have both a local 192.168.x.x address (for communicating locally) and the public address (as an alias) on their primary interface. – larsks Nov 24 '10 at 14:06

What you are asking to do is called NAT Loopback and it requires that you add a SNAT rule so that packets originating from your LAN to your Server will go back through the router:

-A POSTROUTING -p tcp -s -d -m multiport --dports 22,25,80,443 -j SNAT --to-source
  • Sadly, that does not work. I've originally missed the -i ppp0 option in my rule in question, as that was handled by other chain; this rule would prevent routing of packets coming from LAN (and if I'd enable it, packets will go from wrong source and will be rejected). – whitequark Nov 23 '10 at 18:11
  • Have you tried it? It will only affect packets from your LAN going to your server IP on those very specific ports. – SiegeX Nov 23 '10 at 18:43
  • Yes, I did. (And I tried changing the first rule, too). E.g. dig sends a packet to, and then gets an unexpected reply from, with or without your rule. – whitequark Nov 24 '10 at 4:34

larsks comment about hosting an internal version of the namespace\domain is generally the way I've handled this issue in the past. Of course, you need a DNS server internally in order to do this.

  • Yeah, I've wrote that I am using dnsmasq. Any ideas on setting up automatic substitution? – whitequark Nov 23 '10 at 17:47
  • I know nothing about OpenWRT and Kamikaze, but based on what I'm reading - what if you added the following to your /etc/dnsmasq.conf "cname=ext-hostname.domain.com,int-hostname.domain.com" – CurtM Nov 23 '10 at 17:58
  • Well, as far as I was able to determine, dnsmasq's cname does not support masks, and thus is not applicable for me due to subdomain count. – whitequark Nov 24 '10 at 5:08

I came up with the following solution to allow my guest network to connect to ports that were forwarded from my wan to lan network. This script is placed in my "Network -> Firewall -> Custom Rules" section:

# lookup the public IP (DDNS resolves to this)
wanip=$(ip route get | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')

# Guest network to LAN
# srcname is the guest network name, this needs to match for iptables
# CIDR notation of guest and lan networks
# router (openwrt) IP on lan network
# host on lan network where ports are normally forwarded
# ports to forward as a list or range

iptables -t nat -A prerouting_${srcname}_rule -s ${srcnet} -d ${wanip}/32 -p tcp -m tcp -m multiport --dports ${tcpports} -m comment --comment "${srcname} NAT reflection TCP DNAT" -j DNAT --to-destination ${tgthost}
iptables -t nat -A postrouting_${srcname}_rule -s ${srcnet} -d ${tgthost}/32 -p tcp -m tcp -m multiport --dports ${tcpports} -m comment --comment "${srcname} NAT reflection TCP SNAT" -j SNAT --to-source ${tgtrouter}
iptables -t nat -A prerouting_${srcname}_rule -s ${srcnet} -d ${wanip}/32 -p udp -m udp -m multiport --dports ${udpports} -m comment --comment "${srcname} NAT reflection UDP DNAT" -j DNAT --to-destination ${tgthost}
iptables -t nat -A postrouting_${srcname}_rule -s ${srcnet} -d ${tgthost}/32 -p udp -m udp -m multiport --dports ${udpports} -m comment --comment "${srcname} NAT reflection UDP SNAT" -j SNAT --to-source ${tgtrouter}

NAT reflection is setup for connections within the LAN network to itself, but not to other networks if you have created multiple interfaces to isolate traffic. I tried configuring a forwarding rule from the web interface, but that sends all traffic to a port from the guest network to that LAN host. The above only intercepts requests to the WAN IP instead of all network traffic.

An internal DNS could be used instead of this, but only if all port forwards only go to a single host. If you have multiple hosts where you forward different ports, you can repeat the rules for different ports to different tgthost IP's and ports.

  • In current kernels there is conntrack match module. And all you need to solve the issue is use the single rule like that: iptables -t nat -A POSTROUTING --dst <lan-net> ! --src <lan-gw-ip> -m conntrack --ctstate DNAT --ctorigdst <wan-gw-ip> -j MASQUERADE – Anton Danilov May 13 at 20:51
  • @AntonDanilov nice, I like that. The rules I used were based off of reflection NAT rules already in OpenWRT for connections from the same subnet. Not sure if they had any other reasons for that other than possibly being written before conntrack was available. – BMitch May 13 at 21:31

You want to use dnsmasq to override the real DNS entry for your server to use the INTERNAL IP address.

If your server is www.example.com and it's IP address is, from the OUTSIDE, it should look like:

Name:    www.example.com

But on the INSIDE of your network, you need www.example.com to point to so that you can access using the same name as you would on the outside, so nslookup should return this on your LAN:

Name:    www.example.com

Why have those packets "leave" your LAN to just come right back in? I've never had much success in doing that. But at the same time, I never had a reason to do it either. I have seen this same operation done in many other configurations such as with Endian firewalls, or larger more complex network hidden behind Cisco ASA devices as well as a variety of different DNS servers.

So long as the Internet knows how to get to your server and your internal hosts know how to get to your server (by means of overriding the DNS entries at your local DNS servers), that is the important thing.

  • Well, dnsmasq has alias directive which can rewrite external IP to internal 'on the fly', but what if I want e.g. have router:22 be the server's SSH, and router:2222 be the router's SSH? Externally, this would work fine, but on internal access the rewrite will break port 2222 access. – whitequark Nov 24 '10 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.