My server (on which bellow iptables
rules are loaded) has the IP 192.168.3.110
.
There is another computer in my LAN with IP 192.168.3.106
.
I'm trying to redirect requests to my server on port 80 to 192.168.3.106
.
I have the following iptables
file which is loaded in my CentOS 7 server:
*nat
:PREROUTING DROP
:INPUT DROP
:OUTPUT DROP
:POSTROUTING DROP
-A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
-A PREROUTING -p tcp -m state --state NEW --dport 22 -j ACCEPT
-A PREROUTING --dst 192.168.3.110 -p tcp --dport 80 -j DNAT --to-destination 192.168.3.106
-A PREROUTING -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
-A OUTPUT --dst 192.168.3.110 -p tcp -m state --state NEW --dport 80 -j DNAT --to-destination 192.168.3.106
-A OUTPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A POSTROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
-A POSTROUTING -p tcp -m state --state NEW --dport 22 -j ACCEPT
-A POSTROUTING --src 192.168.3.0/24 --dst 192.168.3.106 -p tcp --dport 80 -j SNAT --to-source 192.168.3.110
-A POSTROUTING -o lo -j ACCEPT
COMMIT
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -p tcp -m state --state NEW --dport 80 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A OUTPUT -p tcp --sport 22 -j ACCEPT
-A OUTPUT -p tcp -j ACCEPT
-A OUTPUT -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
COMMIT
Basically I'm doing the following (at least that's how I understand it):
enable ssh-ing
enable TCP on port 80
DNAT and SNAT packets so that I achieve the desired behavior.
Question: I don't understand why, when I make a call from my server to itself, i.e. 192.168.3.110
, I get a response.
That's how I understand that things should work in this case:
curl http://192.168.3.110
- don't forget that I execute this from my server, with IP192.168.3.110
- packet is going to OUTPUT chain from nat table, where it is DNATed
- packet is going to POSTROUTING chain from nat table, where it is SNATed
- my Apache from
192.168.3.106
, is answering to my request. - packet is reaching PREROUTING chain from nat table, where it should be DNATed
- packet is forwarded and thrown somewhere.
All seems to work as expected, except 5 and 6. With other words, I receive the response from server. Can anybody explain me where is my logic broken?