fireall: - support negations for src_ip, dest_ip, src_dip options in rules and redire...
authorJo-Philipp Wich <jow@openwrt.org>
Tue, 28 Sep 2010 10:42:56 +0000 (10:42 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Tue, 28 Sep 2010 10:42:56 +0000 (10:42 +0000)
SVN-Revision: 23141

package/firewall/Makefile
package/firewall/files/lib/core_init.sh
package/firewall/files/lib/core_redirect.sh
package/firewall/files/lib/core_rule.sh
package/firewall/files/lib/fw.sh

index 511fbe9aeb63ca3a7709d03f6ccf034d1b057e2f..82ae04757f66654bf811cb91a0f44b0484094f08 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 PKG_NAME:=firewall
 
 PKG_VERSION:=2
-PKG_RELEASE:=17
+PKG_RELEASE:=18
 
 include $(INCLUDE_DIR)/package.mk
 
index 56e19b7b86e198a3d79dfe0418ec13b2c57b1550..c7e41e7ff7dada3ad8015d741c8e894240e78376 100644 (file)
@@ -245,9 +245,9 @@ fw_load_zone() {
        if [ "$zone_masq" == 1 ]; then
                local msrc mdst
                for msrc in ${zone_masq_src:-0.0.0.0/0}; do
-                       [ "${msrc#!}" != "$msrc" ] && msrc="! -s ${msrc#!}" || msrc="-s $msrc"
+                       fw_get_negation msrc '-s' "$msrc"
                        for mdst in ${zone_masq_dest:-0.0.0.0/0}; do
-                               [ "${mdst#!}" != "$mdst" ] && mdst="! -d ${mdst#!}" || mdst="-d $mdst"
+                               fw_get_negation mdst '-d' "$mdst"
                                fw add $mode n ${chain}_nat MASQUERADE $ { $msrc $mdst }
                        done
                done
index 3a37bb7ee6f5d73413e77cd0f77c8511105fd658..9654e7177c58e12b9a96560fe6284c06d299a9b5 100644 (file)
@@ -41,7 +41,7 @@ fw_load_redirect() {
                nataddr="$redirect_dest_ip"
                fw_get_port_range natports "$redirect_dest_port" "-"
 
-               srcdaddr="${redirect_src_dip:+$redirect_src_dip/$redirect_src_dip_prefixlen}"
+               fw_get_negation srcdaddr '-d' "${redirect_src_dip:+$redirect_src_dip/$redirect_src_dip_prefixlen}"
                fw_get_port_range srcdports "$redirect_src_dport" ":"
 
                list_contains FW_CONNTRACK_ZONES $redirect_src || \
@@ -60,7 +60,7 @@ fw_load_redirect() {
                nataddr="$redirect_src_dip"
                fw_get_port_range natports "$redirect_src_dport" "-"
 
-               srcdaddr="${redirect_dest_ip:+$redirect_dest_ip/$redirect_dest_ip_prefixlen}"
+               fw_get_negation srcdaddr '-d' "${redirect_dest_ip:+$redirect_dest_ip/$redirect_dest_ip_prefixlen}"
                fw_get_port_range srcdports "$redirect_dest_port" ":"
 
                list_contains FW_CONNTRACK_ZONES $redirect_dest || \
@@ -74,21 +74,24 @@ fw_load_redirect() {
        local mode
        fw_get_family_mode mode ${redirect_family:-x} ${redirect_src:-$redirect_dest} I
 
-       local srcaddr="${redirect_src_ip:+$redirect_src_ip/$redirect_src_ip_prefixlen}"
+       local srcaddr
+       fw_get_negation srcaddr '-s' "${redirect_src_ip:+$redirect_src_ip/$redirect_src_ip_prefixlen}"
+
        local srcports
        fw_get_port_range srcports "$redirect_src_port" ":"
 
-       local destaddr="${redirect_dest_ip:+$redirect_dest_ip/$redirect_dest_ip_prefixlen}"
+       local destaddr
+       fw_get_negation destaddr '-d' "${redirect_dest_ip:+$redirect_dest_ip/$redirect_dest_ip_prefixlen}"
+
        local destports
        fw_get_port_range destports "${redirect_dest_port:-$redirect_src_dport}" ":"
 
        [ "$redirect_proto" == "tcpudp" ] && redirect_proto="tcp udp"
        for redirect_proto in $redirect_proto; do
                fw add $mode n $natchain $redirect_target ^ { $redirect_src_ip $redirect_dest_ip } { \
+                       $srcaddr $srcdaddr \
                        ${redirect_proto:+-p $redirect_proto} \
-                       ${srcaddr:+-s $srcaddr} \
                        ${srcports:+--sport $srcports} \
-                       ${srcdaddr:+-d $srcdaddr} \
                        ${srcdports:+--dport $srcdports} \
                        ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
                        $natopt $nataddr${natports:+:$natports} \
@@ -96,10 +99,9 @@ fw_load_redirect() {
 
                [ -n "$destaddr" ] && \
                fw add $mode f ${fwdchain:-forward} ACCEPT ^ { $redirect_src_ip $redirect_dest_ip } { \
+                       $srcaddr $destaddr \
                        ${redirect_proto:+-p $redirect_proto} \
-                       ${srcaddr:+-s $srcaddr} \
                        ${srcports:+--sport $srcports} \
-                       ${destaddr:+-d $destaddr} \
                        ${destports:+--dport $destports} \
                        ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
                }
index 1e4afe5aff498e181c0d2ca07af09e8bdb611abd..dbaf1102e96c2cc35276c0fab4de142e64f8143c 100644 (file)
@@ -24,42 +24,45 @@ fw_config_get_rule() {
 fw_load_rule() {
        fw_config_get_rule "$1"
 
+       [ "$rule_target" != "NOTRACK" ] || [ -n "$rule_src" ] || {
+               fw_log error "NOTRACK rule ${rule_name}: needs src, skipping"
+               return 0
+       }
+
        fw_callback pre rule
 
        fw_get_port_range rule_src_port $rule_src_port
        fw_get_port_range rule_dest_port $rule_dest_port
 
+       local table=f
        local chain=input
-       [ -n "$rule_src" ] && {
-               [ -z "$rule_dest" ] && {
-                       chain=zone_${rule_src}
-               } || {
-                       chain=zone_${rule_src}_forward
-               }
-       }
+       if [ "$rule_target" == "NOTRACK" ]; then
+               table=r
+               chain="zone_${rule_src}_notrack"
+       elif [ -n "$rule_src" ]; then
+               chain="zone_${rule_src}${rule_dest:+_forward}"
+       fi
 
-       local target=$rule_target
-       [ -z "$target" ] && {
-               target=REJECT
-       }
-       [ -n "$dest" ] && {
-               target=zone_${rule_dest}_${target}
-       }
+       local target="${rule_target:-REJECT}"
+       [ -n "$dest" ] && target="zone_${rule_dest}_${target}"
 
        local mode
        fw_get_family_mode mode ${rule_family:-x} $rule_src I
 
+       local src_spec dest_spec
+       fw_get_negation src_spec '-s' "${rule_src_ip:+$rule_src_ip/$rule_src_ip_prefixlen}"
+       fw_get_negation dest_spec '-d' "${rule_dest_ip:+$rule_dest_ip/$rule_dest_ip_prefixlen}"
+
        local rule_pos
        eval 'rule_pos=$((++FW__RULE_COUNT_'$mode'_'$chain'))'
 
        [ "$rule_proto" == "tcpudp" ] && rule_proto="tcp udp"
        for rule_proto in $rule_proto; do
-               fw add $mode f $chain $target $rule_pos { $rule_src_ip $rule_dest_ip } { \
+               fw add $mode $table $chain $target $rule_pos { $rule_src_ip $rule_dest_ip } { \
+                       $src_spec $dest_spec \
                        ${rule_proto:+-p $rule_proto} \
-                       ${rule_src_ip:+-s $rule_src_ip/$rule_src_ip_prefixlen} \
                        ${rule_src_port:+--sport $rule_src_port} \
                        ${rule_src_mac:+-m mac --mac-source $rule_src_mac} \
-                       ${rule_dest_ip:+-d $rule_dest_ip/$rule_dest_ip_prefixlen} \
                        ${rule_dest_port:+--dport $rule_dest_port} \
                        ${rule_icmp_type:+--icmp-type $rule_icmp_type} \
                }
index 3549f8aa4c1e4980c5e71e70dcc64f963e0db710..16a39b6a66fbe730700734640aadb6545e71fac4 100644 (file)
@@ -218,3 +218,12 @@ fw_get_family_mode() {
        esac
 }
 
+fw_get_negation() {
+       local _var="$1"
+       local _flag="$2"
+       local _ipaddr="$3"
+
+       [ "${_ipaddr#!}" != "$_ipaddr" ] && \
+               export -n -- "$_var=! $_flag ${_ipaddr#!}" || \
+               export -n -- "$_var=${_ipaddr:+$_flag $_ipaddr}"
+}