My host has two network interfaces:
eth0:172.16.125.5
eth1:172.165.8.55
I want to redirect all UDP traffic to eth1 port 1234 to eth0 port 1234, ie:
172.165.8.55:1234 -> 172.16.125.5:1234
After reading reading related posts here, I enable ipv4_forwarding, and then added the following rules to iptables:
iptables -t nat -A PREROUTING -p udp -d 172.165.8.55 --dport 1234 \
-j DNAT --to-destination 172.16.125.5:1234
iptables -A FORWARD -p udp -d 172.16.125.5 --dport 1234 -m state \
--state NEW,ESTABLISHED,RELATED -j ACCEPT
When I send UDP traffic to 172.165.8.55:1234
, output of iptables -t nat -L -n -v
does show the packet counts of the rule in PREROUTING
chain going up. So I think that rule is working.
However output of iptables -L -n -v
shows that packet counts of the rule in FORWARD
chain remains zero. So I don't think that rule is working. Also output tcpdump shows the incoming UDP traffic going to 172.165.8.55:1234
and does not show it being forwarded anywhere.
I then check the route table:
ip route show
172.16.125.0/24 dev eth0 proto kernel scope link src 172.16.125.5
172.165.8.0/24 dev eth1 proto kernel scope link src 172.165.8.55
default via 172.16.125.1 dev eth0
I don't see any route between the two subnets of eth0 and eth1. Is that why the redirecting isn't working? If so what changes do I need to make to the route table?
How about bridging the two network interface? Will that help? If so how do I do that?
EDITED FOR ADDITIONAL INFO: I did some tests with nc as suggested. I am dealing with UDP so I used nc -u:
$ nc -u 172.165.8.55 1234
this is a test
nc: Write error: No route to host
The two windows which I have nc -ul running did not show anything. I did have another window open running tcpdump and it has the following output:
01:45:12.327911 IP 172.165.8.77.42726 > 172.165.8.55.1553: UDP, length 15 E..+7.@.@.mT.. M.. 7........this is a test ...
ADDITIONAL INFO: I think I know whey I am getting "No route to host" from nc:
$ nc -u 172.165.8.55 1234 this is a test nc: Write error: No route to host
My rule in the PREROUTING chain is working and the destination IP is being changed. However, my rule in the FORWARD chain is not working because my host does not know how to route to the new destination IP 172.16.125.5 when the source IP is not in the subnet 172.16.125.5.x. I do not get the "No route to host" error if I remove both rules from iptables.
How do I add a route from all traffic from 172.165.8.x subnet to 172.16.125.x subnet?