mainly used in start-up script
When we play with iptables aka firewall we might end up in situation, where we execute rule, which has unforseen impact - lock yourself out. Recovering from this situation is necessity.
How to:
- Enable reboot via SMS.
- Test all commands in shell first before putting them into Start-up script. This way the command will be wiped out, when unit is rebooted.
iptables -t nat -A PREROUTING -s 192.168.1.2 -i eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 192.168.1.1
Below makes sure packets from Eth Devices have correct source IP Address Notice, when specifying a port, protocol needs to be specified as well
iptables -t nat -A POSTROUTING -o wlan0 -s 192.168.1.2 -p udp --dport 16020 -j SNAT --to 10.1.1.7:51889
iptables -t nat -A POSTROUTING -o wlan0 -s 192.168.1.2 -p tcp --dport 21 -j SNAT --to 10.1.1.7:21
iptables -t nat -A POSTROUTING -o wlan0 -s 192.168.1.3 -j SNAT --to 10.1.1.9
# Packets destined for IP 10.1.1.7 will be forwaded to 192.168.1.2 UDP,TCP
# Packets destined for IP 10.1.1.9 will be forwaded to 192.168.1.3 UDP,TCP
# Does work with ping (ICMP) correctly
iptables -t nat -A PREROUTING -i wlan0 -d 10.1.1.7 -j DNAT --to-destination 192.168.1.2
iptables -t nat -A PREROUTING -i wlan0 -d 10.1.1.9 -j DNAT --to-destination 192.168.1.3
Does NOT work with ping (ICMP) correctly, does not handle ICMP protocol WLAN IP reply on a ping without
iptables -t nat -A PREROUTING -p tcp -i wlan0 -d 10.1.1.7 -j DNAT --to-destination 192.168.1.2
iptables -t nat -A PREROUTING -p udp -i wlan0 -d 10.1.1.7 -j DNAT --to-destination 192.168.1.2
iptables -t nat -A OUTPUT -p udp --dport 162 -j DNAT --to-destination 192.168.1.33:1162
ip addr add 10.1.1.7/24 dev wlan0
ip addr add 10.1.1.9/24 dev wlan0
ip add list dev wlan0
iptables -t nat -A POSTROUTING -o eth1 -j SNAT --to 192.168.20.1
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.1.22 -p tcp --dport 443 -j SNAT --to 192.168.20.1:443
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.1.22 -p icmp -j SNAT --to 192.168.20.1
All supported packets leaving eth1 which have source IP 192.168.1.22 will change source IP to 192.168.20.1
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.1.22 -p all -j SNAT --to 192.168.20.1
usage with WIFI dual mode where WiFi can be AP and STA at the same time add to start-up script
# assuming wlan1 is STA interface
ip=$(ip -o addr show up primary scope global wlan1 |
while read -r num dev fam addr rest; do echo ${addr%/*}; done)
echo $ip
# all packets leaving wlan1 will change source IP to STA interface IP
iptables -t nat -A POSTROUTING -o wlan1 -j SNAT --to $ip
Order is important as the DROP
will end up after allowing communication with NTP server. For that reason we need to INSERT
the rules. If used APPEND
the order of commands have to be reversed to ensure DROP
is the last.
iptables -I FORWARD -i eth0 -o usb0 -j DROP
iptables -I FORWARD -d pool.ntp.org -i eth0 -o usb0 -j ACCEPT
Useful when you have a device behind the router and want to limit it's ability to use/exploit resources on the router.
Use with caution as you can lock yourself out.
# Block port 80 (http) only on ETH1 interface
iptables -t filter -A INPUT -i eth1 -p tcp --dport 80 -j DROP
# Block port 443 (https) only on ETH1 interface
iptables -t filter -A INPUT -i eth1 -p tcp --dport 80 -j DROP
# Block port 22 (ssh) only on ETH1 interface
iptables -t filter-A INPUT -i eth1 -p tcp --dport 22 -j DROP
# Block ping (icmp) on ETH1 interface unit does not response to ping
iptables -t filter -A INPUT -i eth1 -p icmp -j DROP
The iptables
table needs to be specified for listing. EG. nat
, mangle
.
iptables -t nat -L -n -v