LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   IPTables Port forwarding rules works only for some hosts (https://www.linuxquestions.org/questions/linux-networking-3/iptables-port-forwarding-rules-works-only-for-some-hosts-4175435253/)

BushNik 11-02-2012 07:15 AM

IPTables Port forwarding rules does not work in internal network
 
Hi,

I have the following iptables rules on proxy host, which forwards traffic to Server:
iptables -t nat -A PREROUTING -d $host_ip -i eth0 -p tcp -m tcp --dport $host_port -j DNAT --to-destination $server_ip:$server_port
iptables -t nat -A POSTROUTING -p tcp --dport $server_port -d $server_ip -j SNAT --to $host_ip

For one server(call it A), when i try to connect with echo client using proxy host_ip:host_port it works fine and client is connecting to server A, server_ip:server_port.

For other server(call it B), when i try to connect with echo client using proxy host_ip:host_port it does not work and there is a strange behavior.
Connection success rate of echo client is about 1%. About 100 attempts, one is successful. Period of successful attempts is not regular, it can be successful 1 of 300, or 3 of 100.
• Tcpdump logs were collected on proxy host and server. The analysis of these logs showed that the proxy server forwards traffic to server, server sends a response, but proxy server does not receive it (i.e. it is visible in the server dump that the response is sent to proxy, but there’s no corresponding packet in the proxy dump).
• An unexplained correlation was found: when an attempt is done to connect other client to the same echo-server (simultaneously with running echo-client), responses from the echo-server are received successfully by the echo-client.

What can be root cause of this issue? Should we find the problems on server B or on proxy server? Please, help.

BushNik 11-06-2012 12:16 AM

The issue is that iptables port forwarding rules works only for external connections and not for connections in internal network. It's called nat loopback. I have tried to set these rules:
* iptables -t nat -A PREROUTING -d $host_ip -i eth0 -p tcp -m tcp --dport $host_port -j DNAT --to-destination $server_ip:$server_port
* iptables -t nat -A POSTROUTING -s $source_ip -p tcp --dport $server_port -d $server_ip -j SNAT --to $host_ip

Postrouting rules will applies only for one machine, but it does not help to solve the issue, client connection from $source_ip is always freezes at the same place.

goossen 11-19-2012 04:37 PM

If you are using the POSTROUTING rule for the "return" packets you should do it this way (assuming $host_ip is configured as gw of $server_ip):
Code:

iptables -t nat -A POSTROUTING -s $server_ip -p tcp --sport $server_port -j SNAT --to-source $host_ip


All times are GMT -5. The time now is 03:30 PM.