make sure uci firewall reverts its states when stopped
[openwrt/svn-archive/archive.git] / package / firewall / files / uci_firewall.sh
index 99663c67717d2402c52a13cec601d9a23b07a39a..5063d9e0884f1696b238d9a47c05b79f9bc6212b 100755 (executable)
@@ -229,6 +229,17 @@ fw_redirect() {
                echo "dport may only be used it proto is defined"; return; }
        [ -n "$src_port" -a -z "$proto" ] && { \
                echo "sport may only be used it proto is defined"; return; }
                echo "dport may only be used it proto is defined"; return; }
        [ -n "$src_port" -a -z "$proto" ] && { \
                echo "sport may only be used it proto is defined"; return; }
+
+       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"; }
+
        $IPTABLES -A zone_${src}_prerouting -t nat \
                ${proto:+-p $proto} \
                ${src_ip:+-s $src_ip} \
        $IPTABLES -A zone_${src}_prerouting -t nat \
                ${proto:+-p $proto} \
                ${src_ip:+-s $src_ip} \
@@ -236,6 +247,12 @@ fw_redirect() {
                ${src_dport:+--dport $src_dport} \
                ${src_mac:+-m mac --mac-source $src_mac} \
                -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
                ${src_dport:+--dport $src_dport} \
                ${src_mac:+-m mac --mac-source $src_mac} \
                -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
+
+       dest_port_first=${dest_port%-*}
+       dest_port_last=${dest_port#*-}
+       [ "$dest_port_first" -ne "$dest_port_last" ] && { \
+               dest_port="$dest_port_first:$dest_port_last"; }
+
        $IPTABLES -I zone_${src}_forward 1 \
                ${proto:+-p $proto} \
                -d $dest_ip \
        $IPTABLES -I zone_${src}_forward 1 \
                ${proto:+-p $proto} \
                -d $dest_ip \
@@ -261,6 +278,19 @@ fw_addif() {
        (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/20-firewall)
 }
 
        (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/20-firewall)
 }
 
+fw_custom_chains() {
+       $IPTABLES -N input_rule
+       $IPTABLES -N output_rule
+       $IPTABLES -N forward_rule
+       $IPTABLES -N prerouting_rule -t nat
+       $IPTABLES -N postrouting_rule -t nat
+       $IPTABLES -A INPUT -j input_rule
+       $IPTABLES -A OUTPUT -j output_rule
+       $IPTABLES -A FORWARD -j forward_rule
+       $IPTABLES -A PREROUTING -t nat -j prerouting_rule
+       $IPTABLES -A POSTROUTING -t nat -j postrouting_rule
+}
+
 fw_init() {
        echo "Loading defaults"
        config_foreach fw_defaults defaults
 fw_init() {
        echo "Loading defaults"
        config_foreach fw_defaults defaults
@@ -272,9 +302,11 @@ fw_init() {
        config_foreach fw_forwarding forwarding
        echo "Loading redirects"
        config_foreach fw_redirect redirect
        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
        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
        uci_set_state firewall core "" firewall_state 
        uci_set_state firewall core loaded 1
        unset CONFIG_APPEND
@@ -291,4 +323,5 @@ fw_stop() {
        $IPTABLES -P INPUT ACCEPT
        $IPTABLES -P OUTPUT ACCEPT
        $IPTABLES -P FORWARD ACCEPT
        $IPTABLES -P INPUT ACCEPT
        $IPTABLES -P OUTPUT ACCEPT
        $IPTABLES -P FORWARD ACCEPT
+       uci_revert_state firewall core
 }
 }