From 6a335579b8e63886c2beed4aee72ab2f335a7d2e Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 28 Sep 2010 10:42:56 +0000 Subject: [PATCH] fireall: - support negations for src_ip, dest_ip, src_dip options in rules and redirects - add NOTRACK target to rule sections, allows to define fine grained notrack rules SVN-Revision: 23141 --- package/firewall/Makefile | 2 +- package/firewall/files/lib/core_init.sh | 4 +-- package/firewall/files/lib/core_redirect.sh | 18 +++++----- package/firewall/files/lib/core_rule.sh | 37 +++++++++++---------- package/firewall/files/lib/fw.sh | 9 +++++ 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/package/firewall/Makefile b/package/firewall/Makefile index 511fbe9aeb..82ae04757f 100644 --- a/package/firewall/Makefile +++ b/package/firewall/Makefile @@ -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 diff --git a/package/firewall/files/lib/core_init.sh b/package/firewall/files/lib/core_init.sh index 56e19b7b86..c7e41e7ff7 100644 --- a/package/firewall/files/lib/core_init.sh +++ b/package/firewall/files/lib/core_init.sh @@ -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 diff --git a/package/firewall/files/lib/core_redirect.sh b/package/firewall/files/lib/core_redirect.sh index 3a37bb7ee6..9654e7177c 100644 --- a/package/firewall/files/lib/core_redirect.sh +++ b/package/firewall/files/lib/core_redirect.sh @@ -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} \ } diff --git a/package/firewall/files/lib/core_rule.sh b/package/firewall/files/lib/core_rule.sh index 1e4afe5aff..dbaf1102e9 100644 --- a/package/firewall/files/lib/core_rule.sh +++ b/package/firewall/files/lib/core_rule.sh @@ -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} \ } diff --git a/package/firewall/files/lib/fw.sh b/package/firewall/files/lib/fw.sh index 3549f8aa4c..16a39b6a66 100644 --- a/package/firewall/files/lib/fw.sh +++ b/package/firewall/files/lib/fw.sh @@ -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}" +} -- 2.30.2