Accessing the DNAT'ted webserver from inside the LANCan't access domain internally via public IP addressweb server behind NAT cannot be accessed by the same network using NAT router IP or domainCannot access my webserver using its public addressHow to mimic “DMZ Host” or “Default Host” NAT funcionality with iptables?Route/IPtables QuestionCan't access a URL with custom port from inside the LAN, but can from outsideBlock a machine from accessing the internetIs this iptables NAT exploitable from the external side?Linux routing - Access external IP from LANIPtables NAT puzzler: Why is traffic from the firewall host being NATed even though it's not coming from one of the prescribed source addresses?Setting up Squid Transparent Proxy on a machine separate from the routerExcept some IPs from rule to not allow internet through openwrt + openvpn when vpn is disconnectediptables rules for allowing specific DNS requests from external to internal networkMikrotik - NAT behind NAT
Specific use case of to_address
How can I end combat quickly when the outcome is inevitable?
Can I utilise a baking stone to make crepes?
Increase speed altering column on large table to NON NULL
My boss want to get rid of me - what should I do?
Origin of "boor"
Excel division by 0 error when trying to average results of formulas
Scientist couple raises alien baby
Why is there always a fire truck present before refuelling?
Does putting salt first make it easier for attacker to bruteforce the hash?
Are polynomials with the same roots identical?
Why did Intel abandon unified CPU cache?
Explain the ending of Black Mirror's "Smithereens"
Are there any normal animals in Pokemon universe?
Why was this person allowed to become Grand Maester?
Tabular with "display math" vertical size
Why am I Seeing A Weird "Notch" on the Data Line For Some Logical 1s?
Does Assassinate grant two attacks?
What does 思ってやっている mean?
Why can my keyboard only digest 6 keypresses at a time?
What's the difference between the Add and Linear Dodge blend modes in After Effects?
Russian word for a male zebra
Bb13b9 confusion
Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?
Accessing the DNAT'ted webserver from inside the LAN
Can't access domain internally via public IP addressweb server behind NAT cannot be accessed by the same network using NAT router IP or domainCannot access my webserver using its public addressHow to mimic “DMZ Host” or “Default Host” NAT funcionality with iptables?Route/IPtables QuestionCan't access a URL with custom port from inside the LAN, but can from outsideBlock a machine from accessing the internetIs this iptables NAT exploitable from the external side?Linux routing - Access external IP from LANIPtables NAT puzzler: Why is traffic from the firewall host being NATed even though it's not coming from one of the prescribed source addresses?Setting up Squid Transparent Proxy on a machine separate from the routerExcept some IPs from rule to not allow internet through openwrt + openvpn when vpn is disconnectediptables rules for allowing specific DNS requests from external to internal networkMikrotik - NAT behind NAT
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a small network with a router, which maintains a connection to Internet, a server and some workstations in a local network.

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 192.168.2.10
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.
linux iptables nat
add a comment |
I have a small network with a router, which maintains a connection to Internet, a server and some workstations in a local network.

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 192.168.2.10
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.
linux iptables nat
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
add a comment |
I have a small network with a router, which maintains a connection to Internet, a server and some workstations in a local network.

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 192.168.2.10
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.
linux iptables nat
I have a small network with a router, which maintains a connection to Internet, a server and some workstations in a local network.

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 192.168.2.10
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.
linux iptables nat
linux iptables nat
edited Apr 13 '17 at 12:14
Community♦
1
1
asked Nov 23 '10 at 8:20
whitequarkwhitequark
3901211
3901211
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
add a comment |
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
What version of OpenWRT?
– Corey S.
Nov 23 '10 at 12:44
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
@Corey 10.03 Stable, but that has nothing to do with openwrt itself, isn't it?
– whitequark
Nov 23 '10 at 16:44
add a comment |
7 Answers
7
active
oldest
votes
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 '192.168.5.22'
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.
add a comment |
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 89.179.245.232/32 -p tcp -m multiport --dports 22,25,80,443 -j DNAT --to-destination 192.168.2.10
-A POSTROUTING -s 192.168.2.0/24 -o ppp0 -j MASQUERADE
-A POSTROUTING -s 192.168.2.0/24 -d 192.168.2.10/32 -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 (192.168.2.1).
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 192.168.2.1, 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 89.179.245.232, 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 (89.179.245.232).
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
add a comment |
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 89.179.245.232 dev br-lan table 10; ip route add 89.179.245.232 via 192.168.2.10 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
add a comment |
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 192.168.2.0/24 -d 192.168.2.10 -m multiport --dports 22,25,80,443 -j SNAT --to-source 89.179.245.232
Sadly, that does not work. I've originally missed the-i ppp0option 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 192.168.2.1#53, and then gets an unexpected reply from 192.168.2.10#53, with or without your rule.
– whitequark
Nov 24 '10 at 4:34
add a comment |
larsks comment about hosting an internal version of the namespacedomain 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'scnamedoes not support masks, and thus is not applicable for me due to subdomain count.
– whitequark
Nov 24 '10 at 5:08
add a comment |
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 8.8.8.8 | awk -F"src " 'NR==1split($2,a," ");print a[1]')
# Guest network to LAN
# srcname is the guest network name, this needs to match for iptables
srcname=guest
# CIDR notation of guest and lan networks
srcnet=192.168.15.0/24
tgtnet=192.168.10.0/24
# router (openwrt) IP on lan network
tgtrouter=192.168.10.1
# host on lan network where ports are normally forwarded
tgthost=192.168.10.5
# ports to forward as a list or range
tcpports=8080,9090
udpports=12345
prechain=prerouting_$srcname_rule
postchain=postrouting_$srcname_rule
# reset the tables to prevent duplicate rules
iptables -t nat -F $prechain
iptables -t nat -F $postchain
iptables -t nat -A $prechain -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 $postchain -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 $prechain -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 $postchain -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
To support reboots, I needed to run the following from the ssh command line on openwrt (otherwise, I believe there is a race condition where some rules were added and then flushed during the reboot):
uci set firewall.@include[0].reload="1"
uci commit firewall
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 isconntrackmatch 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
add a comment |
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 1.2.3.4, from the OUTSIDE, it should look like:
Name: www.example.com
Address: 1.2.3.4
But on the INSIDE of your network, you need www.example.com to point to 192.168.2.10 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
Address: 192.168.2.10
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 hasaliasdirective 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
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f205040%2faccessing-the-dnatted-webserver-from-inside-the-lan%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '192.168.5.22'
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.
add a comment |
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 '192.168.5.22'
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.
add a comment |
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 '192.168.5.22'
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 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 '192.168.5.22'
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.
answered Oct 22 '18 at 14:33
vittorio88vittorio88
462
462
add a comment |
add a comment |
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 89.179.245.232/32 -p tcp -m multiport --dports 22,25,80,443 -j DNAT --to-destination 192.168.2.10
-A POSTROUTING -s 192.168.2.0/24 -o ppp0 -j MASQUERADE
-A POSTROUTING -s 192.168.2.0/24 -d 192.168.2.10/32 -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 (192.168.2.1).
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 192.168.2.1, 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 89.179.245.232, 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 (89.179.245.232).
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
add a comment |
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 89.179.245.232/32 -p tcp -m multiport --dports 22,25,80,443 -j DNAT --to-destination 192.168.2.10
-A POSTROUTING -s 192.168.2.0/24 -o ppp0 -j MASQUERADE
-A POSTROUTING -s 192.168.2.0/24 -d 192.168.2.10/32 -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 (192.168.2.1).
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 192.168.2.1, 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 89.179.245.232, 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 (89.179.245.232).
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
add a comment |
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 89.179.245.232/32 -p tcp -m multiport --dports 22,25,80,443 -j DNAT --to-destination 192.168.2.10
-A POSTROUTING -s 192.168.2.0/24 -o ppp0 -j MASQUERADE
-A POSTROUTING -s 192.168.2.0/24 -d 192.168.2.10/32 -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 (192.168.2.1).
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 192.168.2.1, 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 89.179.245.232, 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 (89.179.245.232).
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).
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 89.179.245.232/32 -p tcp -m multiport --dports 22,25,80,443 -j DNAT --to-destination 192.168.2.10
-A POSTROUTING -s 192.168.2.0/24 -o ppp0 -j MASQUERADE
-A POSTROUTING -s 192.168.2.0/24 -d 192.168.2.10/32 -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 (192.168.2.1).
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 192.168.2.1, 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 89.179.245.232, 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 (89.179.245.232).
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).
answered Nov 24 '10 at 6:12
Steven MondaySteven Monday
10.8k22840
10.8k22840
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
add a comment |
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
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
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
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
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
@macmadness86 IMO == In My Opinion
– Steven Monday
Apr 15 '17 at 0:04
add a comment |
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 89.179.245.232 dev br-lan table 10; ip route add 89.179.245.232 via 192.168.2.10 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
add a comment |
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 89.179.245.232 dev br-lan table 10; ip route add 89.179.245.232 via 192.168.2.10 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
add a comment |
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.
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.
answered Nov 23 '10 at 17:24
larskslarsks
35.3k594147
35.3k594147
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 89.179.245.232 dev br-lan table 10; ip route add 89.179.245.232 via 192.168.2.10 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
add a comment |
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 89.179.245.232 dev br-lan table 10; ip route add 89.179.245.232 via 192.168.2.10 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
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
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 89.179.245.232 dev br-lan table 10; ip route add 89.179.245.232 via 192.168.2.10 dev br-lan table 10, and it isn't working.– whitequark
Nov 24 '10 at 5:40
Can you suggest the configuration? I tried this:
ip rule add to 89.179.245.232 dev br-lan table 10; ip route add 89.179.245.232 via 192.168.2.10 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'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
add a comment |
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 192.168.2.0/24 -d 192.168.2.10 -m multiport --dports 22,25,80,443 -j SNAT --to-source 89.179.245.232
Sadly, that does not work. I've originally missed the-i ppp0option 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 192.168.2.1#53, and then gets an unexpected reply from 192.168.2.10#53, with or without your rule.
– whitequark
Nov 24 '10 at 4:34
add a comment |
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 192.168.2.0/24 -d 192.168.2.10 -m multiport --dports 22,25,80,443 -j SNAT --to-source 89.179.245.232
Sadly, that does not work. I've originally missed the-i ppp0option 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 192.168.2.1#53, and then gets an unexpected reply from 192.168.2.10#53, with or without your rule.
– whitequark
Nov 24 '10 at 4:34
add a comment |
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 192.168.2.0/24 -d 192.168.2.10 -m multiport --dports 22,25,80,443 -j SNAT --to-source 89.179.245.232
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 192.168.2.0/24 -d 192.168.2.10 -m multiport --dports 22,25,80,443 -j SNAT --to-source 89.179.245.232
answered Nov 23 '10 at 18:00
SiegeXSiegeX
435616
435616
Sadly, that does not work. I've originally missed the-i ppp0option 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 192.168.2.1#53, and then gets an unexpected reply from 192.168.2.10#53, with or without your rule.
– whitequark
Nov 24 '10 at 4:34
add a comment |
Sadly, that does not work. I've originally missed the-i ppp0option 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 192.168.2.1#53, and then gets an unexpected reply from 192.168.2.10#53, with or without your rule.
– whitequark
Nov 24 '10 at 4:34
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
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
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 192.168.2.1#53, and then gets an unexpected reply from 192.168.2.10#53, with or without your rule.
– whitequark
Nov 24 '10 at 4:34
Yes, I did. (And I tried changing the first rule, too). E.g. dig sends a packet to 192.168.2.1#53, and then gets an unexpected reply from 192.168.2.10#53, with or without your rule.
– whitequark
Nov 24 '10 at 4:34
add a comment |
larsks comment about hosting an internal version of the namespacedomain 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'scnamedoes not support masks, and thus is not applicable for me due to subdomain count.
– whitequark
Nov 24 '10 at 5:08
add a comment |
larsks comment about hosting an internal version of the namespacedomain 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'scnamedoes not support masks, and thus is not applicable for me due to subdomain count.
– whitequark
Nov 24 '10 at 5:08
add a comment |
larsks comment about hosting an internal version of the namespacedomain 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.
larsks comment about hosting an internal version of the namespacedomain 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.
answered Nov 23 '10 at 17:46
CurtMCurtM
2,26511210
2,26511210
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'scnamedoes not support masks, and thus is not applicable for me due to subdomain count.
– whitequark
Nov 24 '10 at 5:08
add a comment |
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'scnamedoes not support masks, and thus is not applicable for me due to subdomain count.
– whitequark
Nov 24 '10 at 5:08
Yeah, I've wrote that I am using dnsmasq. Any ideas on setting up automatic substitution?
– whitequark
Nov 23 '10 at 17:47
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
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
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
add a comment |
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 8.8.8.8 | awk -F"src " 'NR==1split($2,a," ");print a[1]')
# Guest network to LAN
# srcname is the guest network name, this needs to match for iptables
srcname=guest
# CIDR notation of guest and lan networks
srcnet=192.168.15.0/24
tgtnet=192.168.10.0/24
# router (openwrt) IP on lan network
tgtrouter=192.168.10.1
# host on lan network where ports are normally forwarded
tgthost=192.168.10.5
# ports to forward as a list or range
tcpports=8080,9090
udpports=12345
prechain=prerouting_$srcname_rule
postchain=postrouting_$srcname_rule
# reset the tables to prevent duplicate rules
iptables -t nat -F $prechain
iptables -t nat -F $postchain
iptables -t nat -A $prechain -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 $postchain -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 $prechain -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 $postchain -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
To support reboots, I needed to run the following from the ssh command line on openwrt (otherwise, I believe there is a race condition where some rules were added and then flushed during the reboot):
uci set firewall.@include[0].reload="1"
uci commit firewall
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 isconntrackmatch 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
add a comment |
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 8.8.8.8 | awk -F"src " 'NR==1split($2,a," ");print a[1]')
# Guest network to LAN
# srcname is the guest network name, this needs to match for iptables
srcname=guest
# CIDR notation of guest and lan networks
srcnet=192.168.15.0/24
tgtnet=192.168.10.0/24
# router (openwrt) IP on lan network
tgtrouter=192.168.10.1
# host on lan network where ports are normally forwarded
tgthost=192.168.10.5
# ports to forward as a list or range
tcpports=8080,9090
udpports=12345
prechain=prerouting_$srcname_rule
postchain=postrouting_$srcname_rule
# reset the tables to prevent duplicate rules
iptables -t nat -F $prechain
iptables -t nat -F $postchain
iptables -t nat -A $prechain -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 $postchain -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 $prechain -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 $postchain -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
To support reboots, I needed to run the following from the ssh command line on openwrt (otherwise, I believe there is a race condition where some rules were added and then flushed during the reboot):
uci set firewall.@include[0].reload="1"
uci commit firewall
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 isconntrackmatch 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
add a comment |
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 8.8.8.8 | awk -F"src " 'NR==1split($2,a," ");print a[1]')
# Guest network to LAN
# srcname is the guest network name, this needs to match for iptables
srcname=guest
# CIDR notation of guest and lan networks
srcnet=192.168.15.0/24
tgtnet=192.168.10.0/24
# router (openwrt) IP on lan network
tgtrouter=192.168.10.1
# host on lan network where ports are normally forwarded
tgthost=192.168.10.5
# ports to forward as a list or range
tcpports=8080,9090
udpports=12345
prechain=prerouting_$srcname_rule
postchain=postrouting_$srcname_rule
# reset the tables to prevent duplicate rules
iptables -t nat -F $prechain
iptables -t nat -F $postchain
iptables -t nat -A $prechain -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 $postchain -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 $prechain -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 $postchain -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
To support reboots, I needed to run the following from the ssh command line on openwrt (otherwise, I believe there is a race condition where some rules were added and then flushed during the reboot):
uci set firewall.@include[0].reload="1"
uci commit firewall
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.
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 8.8.8.8 | awk -F"src " 'NR==1split($2,a," ");print a[1]')
# Guest network to LAN
# srcname is the guest network name, this needs to match for iptables
srcname=guest
# CIDR notation of guest and lan networks
srcnet=192.168.15.0/24
tgtnet=192.168.10.0/24
# router (openwrt) IP on lan network
tgtrouter=192.168.10.1
# host on lan network where ports are normally forwarded
tgthost=192.168.10.5
# ports to forward as a list or range
tcpports=8080,9090
udpports=12345
prechain=prerouting_$srcname_rule
postchain=postrouting_$srcname_rule
# reset the tables to prevent duplicate rules
iptables -t nat -F $prechain
iptables -t nat -F $postchain
iptables -t nat -A $prechain -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 $postchain -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 $prechain -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 $postchain -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
To support reboots, I needed to run the following from the ssh command line on openwrt (otherwise, I believe there is a race condition where some rules were added and then flushed during the reboot):
uci set firewall.@include[0].reload="1"
uci commit firewall
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.
edited May 24 at 13:31
answered May 13 at 18:56
BMitchBMitch
1,715815
1,715815
In current kernels there isconntrackmatch 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
add a comment |
In current kernels there isconntrackmatch 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
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
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
@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
add a comment |
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 1.2.3.4, from the OUTSIDE, it should look like:
Name: www.example.com
Address: 1.2.3.4
But on the INSIDE of your network, you need www.example.com to point to 192.168.2.10 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
Address: 192.168.2.10
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 hasaliasdirective 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
add a comment |
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 1.2.3.4, from the OUTSIDE, it should look like:
Name: www.example.com
Address: 1.2.3.4
But on the INSIDE of your network, you need www.example.com to point to 192.168.2.10 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
Address: 192.168.2.10
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 hasaliasdirective 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
add a comment |
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 1.2.3.4, from the OUTSIDE, it should look like:
Name: www.example.com
Address: 1.2.3.4
But on the INSIDE of your network, you need www.example.com to point to 192.168.2.10 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
Address: 192.168.2.10
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.
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 1.2.3.4, from the OUTSIDE, it should look like:
Name: www.example.com
Address: 1.2.3.4
But on the INSIDE of your network, you need www.example.com to point to 192.168.2.10 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
Address: 192.168.2.10
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.
answered Nov 23 '10 at 22:01
Corey S.Corey S.
1,8121621
1,8121621
Well, dnsmasq hasaliasdirective 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
add a comment |
Well, dnsmasq hasaliasdirective 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
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
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
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f205040%2faccessing-the-dnatted-webserver-from-inside-the-lan%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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