The accepted solution works when the destination host and the gateway are on the same subnet (like is in your case, both are on eth0
192.168.1.0/24).
Below is a generic solution for when the gateway, source and destination are all on different subnets.
1) Enable IP forwarding:
sysctl net.ipv4.conf.eth0.forwarding=1
sysctl net.ipv6.conf.eth0.forwarding=1
//note: if forwarding to/from localhost
, also set sysctl net.ipv4.conf.eth0.route_localnet=1
2) Add 2 iptables rules to forward a specific TCP port:
To rewrite the destination IP of the packet (and back in the reply packet):
iptables -A PREROUTING -t nat -p tcp -i ppp0 --dport 8001 -j DNAT --to-destination 192.168.1.200:8080
To rewrite the source IP of the packet to the IP of the gateway (and back in the reply packet):
iptables -A POSTROUTING -t nat -p tcp -d 192.168.1.200 --dport 8080 -j MASQUERADE
3) If you don't have a default ACCEPT
firewall rule, allow traffic to the destination:
iptables -A FORWARD -p tcp -d 192.168.1.200 --dport 8080 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
4) Test the new setup. If it works, make sure the changes persist across reboots:
cat <<EOF > /etc/sysctl.d/99-forwarding.conf
sysctl net.ipv4.conf.eth0.forwarding=1
sysctl net.ipv6.conf.eth0.forwarding=1
EOF
iptables-save > /etc/network/iptables.up.rules
echo '#!/bin/sh' > /etc/network/if-pre-up.d/iptables
echo "`which iptables-restore` < /etc/network/iptables.up.rules" >> /etc/network/if-pre-up.d/iptables
chmod +x /etc/network/if-pre-up.d/iptables