Fixed a typo in the firewall scripts
[openwrt/svn-archive/archive.git] / package / firewall / files / uci_firewall.sh
index 88e6976dace280f1e10603f6a019f7f8999e767c..22731af98e6f6e9b82e8b593772ed87529020322 100755 (executable)
@@ -49,10 +49,14 @@ create_zone() {
        $IPTABLES -A OUTPUT -j zone_$1_$4
        $IPTABLES -N zone_$1_nat -t nat
        $IPTABLES -N zone_$1_prerouting -t nat
-       [ "$6" == "1" ] && $IPTABLES -t nat -A POSTROUTING -j zone_$2_nat
+       [ "$6" == "1" ] && $IPTABLES -t nat -A POSTROUTING -j zone_$1_nat
 }
 
 addif() {
+       local dev
+       config_get dev core $2
+       [ -n "$dev" -a "$dev" != "$1" ] && delif "$dev" "$2"
+       [ -n "$dev" -a "$dev" == "$1" ] && return
        logger "adding $1 to firewall zone $2"
        $IPTABLES -A INPUT -i $1 -j zone_$2
        $IPTABLES -I zone_$2_ACCEPT 1 -o $1 -j ACCEPT
@@ -64,6 +68,7 @@ addif() {
        $IPTABLES -I zone_$2_nat 1 -t nat -o $1 -j MASQUERADE 
        $IPTABLES -I PREROUTING 1 -t nat -i $1 -j zone_$2_prerouting 
        $IPTABLES -A FORWARD -i $1 -j zone_$2_forward
+       uci_set_state firewall core "$2" "$1"
 }
 
 delif() {
@@ -78,6 +83,7 @@ delif() {
        $IPTABLES -D zone_$2_nat -t nat -o $1 -j MASQUERADE 
        $IPTABLES -D PREROUTING -t nat -i $1 -j zone_$2_prerouting 
        $IPTABLES -D FORWARD -i $1 -j zone_$2_forward
+       uci_revert_state firewall core "$2"
 }
 
 load_synflood() {
@@ -106,6 +112,9 @@ fw_defaults() {
        do
                echo 0 > $f
        done                                                                   
+       
+       uci_revert_state firewall core
+       uci_set_state firewall core "" firewall_state 
 
        $IPTABLES -F
        $IPTABLES -t nat -F
@@ -163,6 +172,7 @@ fw_rule() {
        local dest_port
        local proto
        local target
+       local ruleset
 
        config_get src $1 src
        config_get src_ip $1 src_ip
@@ -178,16 +188,24 @@ fw_rule() {
        [ -z "$target" ] && target=DROP
        [ -n "$src" ] && ZONE=zone_$src || ZONE=INPUT
        [ -n "$dest" ] && TARGET=zone_${dest}_$target || TARGET=$target
-       [ -n "$dest_port" -a -z "$proto" ] && { \
-               echo "dport may only be used it proto is defined"; return; }
-       $IPTABLES -I $ZONE 1 \
-               ${proto:+-p $proto} \
-               ${src_ip:+-s $src_ip} \
-               ${src_port:+--sport $src_port} \
-               ${src_mac:+-m mac --mac-source $src_mac} \
-               ${dest_ip:+-d $dest_ip} \
-               ${dest_port:+--dport $dest_port} \
-               -j $TARGET 
+       add_rule() {
+               $IPTABLES -I $ZONE 1 \
+                       ${proto:+-p $proto} \
+                       ${src_ip:+-s $src_ip} \
+                       ${src_port:+--sport $src_port} \
+                       ${src_mac:+-m mac --mac-source $src_mac} \
+                       ${dest_ip:+-d $dest_ip} \
+                       ${dest_port:+--dport $dest_port} \
+                       -j $TARGET 
+       }
+       [ "$proto" == "tcpudp" -o -z "$proto" ] && {
+               proto=tcp
+               add_rule
+               proto=udp
+               add_rule
+               return
+       }
+       add_rule
 }
 
 fw_forwarding() {
@@ -209,8 +227,8 @@ fw_redirect() {
        local src_dport
        local src_mac
        local dest_ip
-       local dest_port
-       local protocol
+       local dest_port dest_port2
+       local proto
        
        config_get src $1 src
        config_get src_ip $1 src_ip
@@ -219,26 +237,52 @@ fw_redirect() {
        config_get src_mac $1 src_mac
        config_get dest_ip $1 dest_ip
        config_get dest_port $1 dest_port
-       config_get protocol $1 protocol
+       config_get proto $1 proto
        [ -z "$src" -o -z "$dest_ip" ] && { \
                echo "redirect needs src and dest_ip"; return ; }
-       [ -n "$dest_port" -a -z "$protocol" ] && { \
-               echo "dport may only be used it proto is defined"; return; }
-       $IPTABLES -A zone_${src}_prerouting -t nat \
-               ${protocol:+-p $protocol} \
-               ${src_ip:+-s $srcdip} \
-               ${src_port:+--sport $src_port} \
-               ${src_dport:+--dport $src_dport} \
-               ${src_mac:+-m mac --mac-source $src_mac} \
-               -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
-       $IPTABLES -I zone_${src}_forward 1 \
-               ${protocol:+-p $protocol} \
-               -d $dest_ip \
-               ${src_ip:+-s $srcdip} \
-               ${src_port:+--sport $src_port} \
-               ${dest_port:+--dport $dest_port} \
-               ${src_mac:+-m mac --mac-source $src_mac} \
-               -j ACCEPT 
+       
+       src_port_first=${src_port%-*}
+       src_port_last=${src_port#*-}
+       [ "$src_port_first" -ne "$src_port_last" ] && { \
+               src_port="$src_port_first:$src_port_last"; }
+
+       src_dport_first=${src_dport%-*}
+       src_dport_last=${src_dport#*-}
+       [ "$src_dport_first" -ne "$src_dport_last" ] && { \
+               src_dport="$src_dport_first:$src_dport_last"; }
+
+       dest_port2=$dest_port
+       dest_port_first=${dest_port2%-*}
+       dest_port_last=${dest_port2#*-}
+       [ "$dest_port_first" -ne "$dest_port_last" ] && { \
+               dest_port2="$dest_port_first:$dest_port_last"; }
+
+       add_rule() {
+               $IPTABLES -A zone_${src}_prerouting -t nat \
+                       ${proto:+-p $proto} \
+                       ${src_ip:+-s $src_ip} \
+                       ${src_port:+--sport $src_port} \
+                       ${src_dport:+--dport $src_dport} \
+                       ${src_mac:+-m mac --mac-source $src_mac} \
+                       -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
+
+               $IPTABLES -I zone_${src}_forward 1 \
+                       ${proto:+-p $proto} \
+                       -d $dest_ip \
+                       ${src_ip:+-s $src_ip} \
+                       ${src_port:+--sport $src_port} \
+                       ${dest_port2:+--dport $dest_port2} \
+                       ${src_mac:+-m mac --mac-source $src_mac} \
+                       -j ACCEPT 
+       }
+       [ "$proto" == "tcpudp" -o -z "$proto" ] && {
+               proto=tcp
+               add_rule
+               proto=udp
+               add_rule
+               return
+       }
+       add_rule
 }
 
 fw_include() {
@@ -256,6 +300,26 @@ fw_addif() {
        (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/20-firewall)
 }
 
+fw_custom_chains() {
+       $IPTABLES -N input_rule
+       $IPTABLES -N output_rule
+       $IPTABLES -N forwarding_rule
+       $IPTABLES -N prerouting_rule -t nat
+       $IPTABLES -N postrouting_rule -t nat
+       $IPTABLES -N input_wan
+       $IPTABLES -N forwarding_wan
+       $IPTABLES -N prerouting_wan -t nat
+                       
+       $IPTABLES -A INPUT -j input_rule
+       $IPTABLES -A OUTPUT -j output_rule
+       $IPTABLES -A FORWARD -j forwarding_rule
+       $IPTABLES -A PREROUTING -t nat -j prerouting_rule
+       $IPTABLES -A POSTROUTING -t nat -j postrouting_rule
+       $IPTABLES -A zone_wan -j input_wan
+       $IPTABLES -A zone_wan_forward -j forwarding_wan
+       $IPTABLES -A zone_wan_prerouting -t nat -j prerouting_wan
+}
+
 fw_init() {
        echo "Loading defaults"
        config_foreach fw_defaults defaults
@@ -267,10 +331,10 @@ fw_init() {
        config_foreach fw_forwarding forwarding
        echo "Loading redirects"
        config_foreach fw_redirect redirect
+       echo "Adding custom chains"
+       fw_custom_chains
        echo "Loading includes"
        config_foreach fw_include include
-       
-       uci_set_state firewall core "" firewall_state 
        uci_set_state firewall core loaded 1
        unset CONFIG_APPEND
        config_load network
@@ -286,4 +350,5 @@ fw_stop() {
        $IPTABLES -P INPUT ACCEPT
        $IPTABLES -P OUTPUT ACCEPT
        $IPTABLES -P FORWARD ACCEPT
+       uci_revert_state firewall core
 }