I need forward packets from one server ("as a proxy") to another with keeping the original IP address of clients. Topology is: X.X.X.X - public IP1 ("proxy server") Y.Y.Y.Y - public IP2 (host for virtualization) 172.16.0.2 - private IP (virutal machine with web server)
some public IP ---> X.X.X.X ---> Y.Y.Y.Y ---> 172.16.0.2 (web server)
On systems with X.X.X.X and Y.Y.Y.Y forwarding is enabled.
Traffic from some public IP via X.X.X.X is on X.X.X.X routed to Y.Y.Y.Y but it never gets there. I captured it with tcpdump.
If I use masquerade on "proxy server" it works OK, but the original ip address is not preserved.
If I use DNAT for port 80 only form Y.Y.Y.Y to 172.16.0.2 - iptables -t nat -A PREROUTING -d Y.Y.Y.Y/32 -i venet0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 172.16.0.2:80
and I try Y.Y.Y.Y:80 it works ok. Problem is the most probably on "proxy server" with public IP X.X.X.X.
"Proxy server" host IP X.X.X.X
It has only one interface with connect to the internet - eth0.
iptables rule: (forwarding in filter table is allowed)
iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 443 -j DNAT --to-destination Y.Y.Y.Y:443
Route table: It uses main table with:
default via X.X.X.1 dev eth0 onlink
Host IP Y.Y.Y.Y
It has only one interface with connect to the internet - venet0. For VM is used Qemu and interface br0.
iptables rule: (forwarding in filter table is allowed)
iptables -t nat -A PREROUTING -d Y.Y.Y.Y/32 -i venet0 -p tcp -m tcp --dport 443 -j DNAT --to-destination 172.16.0.2:443
iptables -t nat -A POSTROUTING -s 172.16.0.0/24 ! -o br0 -j MASQUERADE
Route table: It uses main table with:
default via 255.255.255.254 dev venet0
Host IP 172.16.0.2
It has only one interface with connect to the internet - ens6 Route table: It uses main table with:
default via 172.16.0.1 dev ens6 proto static
iptables rules according to gapsf answer:
X.X.X.X iptables rules:
iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 443 -j DNAT --to-destination Y.Y.Y.Y
iptables -t nat -A POSTROUTING -s Y.Y.Y.Y/32 -o eth0 -p tcp -m tcp --sport 443 -j SNAT --to-source X.X.X.X
iptables -A FORWARD -d Y.Y.Y.Y/32 -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A FORWARD -s Y.Y.Y.Y/32 -p tcp -m tcp --sport 443 -j ACCEPT
Y.Y.Y.Y iptables rules:
iptables -t nat -A PREROUTING -i venet0 -p tcp -m tcp --dport 443 -j DNAT --to-destination 172.16.0.2:443
iptables -t nat -A POSTROUTING -s 172.16.0.2/32 -o venet0 -p tcp -m tcp --sport 443 -j SNAT --to-source Y.Y.Y.Y
iptables -A FORWARD -d 172.16.0.2/32 -i venet0 -o br0 -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A FORWARD -s 172.16.0.2/32 -i br0 -o venet0 -p tcp -m tcp --sport 443 -j ACCEPT
172.16.0.2 iptables rules:
iptables -A INPUT -i ens6 -p tcp -m multiport --dports 80,443 -j ACCEPT
iptables -A OUTPUT -o ens6 -p tcp -m tcp --sport 443 -j ACCEPT
Could you help me please - where is the problem?
iptables
. Just set up routing (network A needs to know how to get to network B, and B needs to know how to get to A, etc.)