I have been searching the internet for an answer al morning, but I cant get it to work. If you can help me, I would be grateful.
my Setup: server: eth1: 192.168.6.2 (connected to the internet -> WAN) eth0: 192.168.0.1 (LAN)
local computer: running a service on port 8848 working on IP number 192.168.0.3
Everything is shut tight with IPtables so that people in the building can not access the local LAN (192.168.0.x) and the LAN uses the internet via eth1:
### Set Variables
IPTABLES='/sbin/iptables -v'
WAN='eth1'
LAN='eth0'
#EXTERNAL_INTERFACE=WAN
#EXTERNAL_IP=WAN_IP
WAN_IP=$( ifconfig $WAN | grep 'inet[^6]' | sed 's/[a-zA-Z:]//g' | awk '{print $1}' )
LAN_IP=$( ifconfig $LAN | grep 'inet[^6]' | sed 's/[a-zA-Z:]//g' | awk '{print $1}' )
echo '########################################################## NAT config WAN <=> LAN #'
$IPTABLES -t nat --append POSTROUTING --out-interface $WAN --jump MASQUERADE
$IPTABLES --append FORWARD --in-interface $WAN --out-interface $LAN -m state --state RELATED,ESTABLISHED --jump ACCEPT
$IPTABLES --append FORWARD --in-interface $LAN --out-interface $WAN --jump ACCEPT
echo '############################### Allow unlimited traffic on the loopback interface #'
$IPTABLES --append INPUT --in-interface lo --jump ACCEPT
$IPTABLES --append OUTPUT --out-interface lo --jump ACCEPT
$IPTABLES --append INPUT --in-interface $LAN --jump ACCEPT
$IPTABLES --append OUTPUT --out-interface $LAN --jump ACCEPT
echo '################################################ Allow unlimited outbound traffic #'
# Previously initiated and accepted exchanges bypass rule checking
$IPTABLES --append INPUT -m state --state ESTABLISHED,RELATED --jump ACCEPT
$IPTABLES --append OUTPUT -m state --state NEW,ESTABLISHED,RELATED --jump ACCEPT
The server (LAN 192.168.0.1 and WAN 192.168.6.2) runs Apache, opening that one up for WAN works fine (tested) and I can access websites connecting to 192.168.6.2 via an IP number in the range 192.168.6.x:
$IPTABLES --append INPUT --proto tcp --source 0/0 --dport 80 -m state --state NEW --jump ACCEPT
so I opened port 8848 first:
$IPTABLES --append INPUT --proto tcp --source 0/0 --dport 8848 -m state --state NEW --jump ACCEPT
Then I get stuck.. I have tried everything, but I can't find the solution to to access the service on port 8848 on 192.168.0.3 via 192.168.6.2 (commands containing FORWARD, PREROUTING, POSTROUTING, MASQUERADE, DNAT nat)
Can you help me?
[edit] To complete the whole bash script, after setting the LAN and WAN variable I clean up with:
echo '####################################################################### clear all #'
$IPTABLES --flush
$IPTABLES --delete-chain
for TABLE in filter nat mangle; do
$IPTABLES --table $TABLE --flush # delete the table's rules
$IPTABLES --table $TABLE --delete-chain # delete the table's chains
$IPTABLES --table $TABLE --zero # zero the table's counters
done
And I end my bash script with:
echo '############################################################ Set default policies #'
$IPTABLES --policy INPUT DROP
$IPTABLES --policy OUTPUT DROP
$IPTABLES --policy FORWARD ACCEPT
echo '########################### Have these rules take effect when iptables is started #'
/sbin/service iptables save
/sbin/service iptables restart`
[ANSWER / SOLUTION]
With thanks to this page I have finaly got it working robustly:
echo '################################################################# Port forwarding #'
FROM_PORT='8848'
TO_PORT='8848'
TO_IP='192.168.0.3'
$IPTABLES -t nat -A PREROUTING -p tcp -d $WAN_IP --dport $FROM_PORT -j DNAT --to-destination $TO_IP:$TO_PORT
$IPTABLES -t nat -A POSTROUTING -p tcp -d $TO_IP --dport $TO_PORT -j SNAT --to-source $WAN_IP