Temat: Iptables - skrypt z openwrt.org
Znalazłem skrypt do ustawiania firewalla na stronie openwrt.org
# This file is interpreted as shell script.
# Put your custom iptables rules here, they will
# be executed with each firewall (re-)start.
# Internal uci firewall chains are flushed and recreated on reload, so
# put custom rules into the root chains e.g. INPUT or FORWARD or into the
# special user chains, e.g. input_wan_rule or postrouting_lan_rule.
/etc/firewall.ipsec
VPN Firewall Script
Finally we have a look at the script. It injects all the additionally required settings according to /etc/config/ipsec into the OpenWrt firewall. Save it as /etc/ipsec/firewall.sh and put a calling line into /etc/firewall.user so it gets loaded automatically. REMARK: This script only enables VPN firewall rules that have been set in the LUCI web interface. There is no guarantee that manually implemented rules in /etc/config/firewall will work!
#!/bin/sh
#/etc/ipsec/firewall.sh - version 2
. /etc/functions.sh
GetZone() {
config_get zone "$1" zone vpn
}
GetTunnel() {
local remote_subnet
local local_subnet
local local_nat
config_get remote_subnet "$1" remote_subnet
config_get local_subnet "$1" local_subnet
config_get local_nat "$1" local_nat ""
iptables -A zone_${zone}_ACCEPT -d $remote_subnet -j ACCEPT
iptables -A zone_${zone}_ACCEPT -s $remote_subnet -j ACCEPT
iptables -A zone_${zone}_REJECT -d $remote_subnet -j reject
iptables -A zone_${zone}_REJECT -s $remote_subnet -j reject
iptables -A zone_${zone}_INPUT -s $remote_subnet -j zone_${zone}
iptables -A zone_${zone}_FORWARD -s $remote_subnet -j zone_${zone}_forward
if [ "$local_nat" == "" ]; then
iptables -t nat -A zone_${zone}_nat -d $remote_subnet -j ACCEPT
else
iptables -t nat -A zone_${zone}_nat -d $remote_subnet \
-s $local_subnet -j NETMAP --to $local_nat
iptables -t nat -A prerouting_${zone} -s $remote_subnet \
-d $local_nat -j NETMAP --to $local_subnet
fi
}
GetRemote() {
local enabled
local gateway
config_get_bool enabled "$1" enabled 0
config_get gateway "$1" gateway
[[ "$enabled" == "0" ]] && return
config_list_foreach "$1" tunnel GetTunnel
}
GetDevice() {
. /lib/functions/network.sh
local interface="$1"
network_get_device listen "$interface"
# open IPsec endpoint
if [ "$listen" == "" ]; then
iptables -A zone_${zone}_gateway -p esp -j ACCEPT
iptables -A zone_${zone}_gateway -p udp --dport 500 -j ACCEPT
iptables -A zone_${zone}_gateway -p udp --dport 4500 -j ACCEPT
if [ $has_ip6tables -eq 1 ]; then
ip6tables -A zone_${zone}_gateway -p esp -j ACCEPT
ip6tables -A zone_${zone}_gateway -p udp --dport 500 -j ACCEPT
ip6tables -A zone_${zone}_gateway -p udp --dport 4500 -j ACCEPT
fi
else
iptables -A zone_${zone}_gateway -i $listen -p esp -j ACCEPT
iptables -A zone_${zone}_gateway -i $listen -p udp --dport 500 -j ACCEPT
iptables -A zone_${zone}_gateway -i $listen -p udp --dport 4500 -j ACCEPT
if [ $has_ip6tables -eq 1 ]; then
ip6tables -A zone_${zone}_gateway -i $listen -p esp -j ACCEPT
ip6tables -A zone_${zone}_gateway -i $listen -p udp --dport 500 -j ACCEPT
ip6tables -A zone_${zone}_gateway -i $listen -p udp --dport 4500 -j ACCEPT
fi
fi
}
GetInterface() {
config_list_foreach "$1" listen GetDevice
}
zone=vpn
config_load ipsec
config_foreach GetZone ipsec
if [ -x /usr/sbin/ip6tables ]; then
has_ip6tables=1
else
has_ip6tables=0
fi
iptables -F zone_${zone}_ACCEPT
if [ $has_ip6tables -eq 1 ]; then
ip6tables -F zone_${zone}_ACCEPT
fi
iptables -N zone_${zone}_gateway
iptables -I input -j zone_${zone}_gateway
if [ $has_ip6tables -eq 1 ]; then
ip6tables -N zone_${zone}_gateway
ip6tables -I input -j zone_${zone}_gateway
fi
config_foreach GetInterface ipsec
iptables -t nat -F zone_${zone}_nat
iptables -t nat -I POSTROUTING 2 -j zone_${zone}_nat
iptables -t nat -I PREROUTING 2 -j zone_${zone}_prerouting
# sort VPN rules to top of forward zones and insert VPN reject marker afterwards
ForwardZones=`iptables -S | awk '/.N.*zone.*_forward/{print $2}' | grep -v ${zone}`
for ForwardZone in $ForwardZones ; do
echo "iptables -F $ForwardZone" > /tmp/fwrebuild
iptables -S $ForwardZone | grep zone_${zone}_ACCEPT | \
grep -v "^-N" | awk '{ print "iptables " $0}' >> /tmp/fwrebuild
echo "iptables -A $ForwardZone -j zone_${zone}_REJECT" >> /tmp/fwrebuild
iptables -S $ForwardZone | grep -v zone_${zone}_ACCEPT | \
grep -v "^-N" | awk '{ print "iptables " $0}' >> /tmp/fwrebuild
chmod +x /tmp/fwrebuild
/tmp/fwrebuild
rm /tmp/fwrebuild
done
# link zone_vpn via zone_vpn_INPUT
iptables -N zone_${zone}_INPUT
iptables -I input -j zone_${zone}_INPUT
# link zone_vpn_forward via zone_vpn_FORWARD
iptables -N zone_${zone}_FORWARD
iptables -I forward -j zone_${zone}_FORWARD
config_foreach GetRemote remoteNiestety nie działa.. po wywołaniu skryptu otrzymuję:
root@router:/# /etc/ipsec/firewall.sh
iptables: No chain/target/match by that name.
ip6tables: No chain/target/match by that name.
iptables: Chain already exists.
ip6tables: Chain already exists.
iptables: No chain/target/match by that name.
iptables v1.4.21: Couldn't load target `zone_vpn_nat':No such file or directory
Try `iptables -h' or 'iptables --help' for more information.
iptables v1.4.21: Couldn't load target `zone_vpn_REJECT':No such file or directory
Try `iptables -h' or 'iptables --help' for more information.
iptables v1.4.21: Couldn't load target `zone_vpn_REJECT':No such file or directory
Try `iptables -h' or 'iptables --help' for more information.
iptables: No chain/target/match by that name.
iptables: No chain/target/match by that name.
iptables: No chain/target/match by that name.
iptables: No chain/target/match by that name.
iptables v1.4.21: Couldn't load target `zone_vpn':No such file or directory
Try `iptables -h' or 'iptables --help' for more information.
iptables: No chain/target/match by that name.Zgodnie z instrukcja stworzyłam strefę vpn w iptables i nie przydzieliłem jej żadnego interfacu.
Generalnie tunel mi działa, ale na ręcznie dodanych regułach iptables, a zamarzyło mi się, żeby tego nie robić ręcznie, ale
niestey nie wiem dlaczego zwraca mi błędy np.:
Couldn't load target `zone_vpn_REJECT':No such file or directory
Czy to jest związane z ewolucją iptables, czy coś innego?>
nie znalazłem na razie rozwiązania tego problemu, fajnie by było mieć takie narzędzie do przyspieszenia prac na VPN.
Jeszcze link do tego tutoriala:
https://wiki.openwrt.org/doc/howto/vpn.ipsec.firewall
Pozdrawiam