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
This commit is contained in:
Jo-Philipp Wich 2010-09-28 10:42:56 +00:00
parent 2fdc8aea04
commit 6a335579b8
5 changed files with 42 additions and 28 deletions

View File

@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=firewall PKG_NAME:=firewall
PKG_VERSION:=2 PKG_VERSION:=2
PKG_RELEASE:=17 PKG_RELEASE:=18
include $(INCLUDE_DIR)/package.mk include $(INCLUDE_DIR)/package.mk

View File

@ -245,9 +245,9 @@ fw_load_zone() {
if [ "$zone_masq" == 1 ]; then if [ "$zone_masq" == 1 ]; then
local msrc mdst local msrc mdst
for msrc in ${zone_masq_src:-0.0.0.0/0}; do 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 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 } fw add $mode n ${chain}_nat MASQUERADE $ { $msrc $mdst }
done done
done done

View File

@ -41,7 +41,7 @@ fw_load_redirect() {
nataddr="$redirect_dest_ip" nataddr="$redirect_dest_ip"
fw_get_port_range natports "$redirect_dest_port" "-" 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" ":" fw_get_port_range srcdports "$redirect_src_dport" ":"
list_contains FW_CONNTRACK_ZONES $redirect_src || \ list_contains FW_CONNTRACK_ZONES $redirect_src || \
@ -60,7 +60,7 @@ fw_load_redirect() {
nataddr="$redirect_src_dip" nataddr="$redirect_src_dip"
fw_get_port_range natports "$redirect_src_dport" "-" 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" ":" fw_get_port_range srcdports "$redirect_dest_port" ":"
list_contains FW_CONNTRACK_ZONES $redirect_dest || \ list_contains FW_CONNTRACK_ZONES $redirect_dest || \
@ -74,21 +74,24 @@ fw_load_redirect() {
local mode local mode
fw_get_family_mode mode ${redirect_family:-x} ${redirect_src:-$redirect_dest} I 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 local srcports
fw_get_port_range srcports "$redirect_src_port" ":" 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 local destports
fw_get_port_range destports "${redirect_dest_port:-$redirect_src_dport}" ":" fw_get_port_range destports "${redirect_dest_port:-$redirect_src_dport}" ":"
[ "$redirect_proto" == "tcpudp" ] && redirect_proto="tcp udp" [ "$redirect_proto" == "tcpudp" ] && redirect_proto="tcp udp"
for redirect_proto in $redirect_proto; do for redirect_proto in $redirect_proto; do
fw add $mode n $natchain $redirect_target ^ { $redirect_src_ip $redirect_dest_ip } { \ fw add $mode n $natchain $redirect_target ^ { $redirect_src_ip $redirect_dest_ip } { \
$srcaddr $srcdaddr \
${redirect_proto:+-p $redirect_proto} \ ${redirect_proto:+-p $redirect_proto} \
${srcaddr:+-s $srcaddr} \
${srcports:+--sport $srcports} \ ${srcports:+--sport $srcports} \
${srcdaddr:+-d $srcdaddr} \
${srcdports:+--dport $srcdports} \ ${srcdports:+--dport $srcdports} \
${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \ ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
$natopt $nataddr${natports:+:$natports} \ $natopt $nataddr${natports:+:$natports} \
@ -96,10 +99,9 @@ fw_load_redirect() {
[ -n "$destaddr" ] && \ [ -n "$destaddr" ] && \
fw add $mode f ${fwdchain:-forward} ACCEPT ^ { $redirect_src_ip $redirect_dest_ip } { \ fw add $mode f ${fwdchain:-forward} ACCEPT ^ { $redirect_src_ip $redirect_dest_ip } { \
$srcaddr $destaddr \
${redirect_proto:+-p $redirect_proto} \ ${redirect_proto:+-p $redirect_proto} \
${srcaddr:+-s $srcaddr} \
${srcports:+--sport $srcports} \ ${srcports:+--sport $srcports} \
${destaddr:+-d $destaddr} \
${destports:+--dport $destports} \ ${destports:+--dport $destports} \
${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \ ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
} }

View File

@ -24,42 +24,45 @@ fw_config_get_rule() {
fw_load_rule() { fw_load_rule() {
fw_config_get_rule "$1" 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_callback pre rule
fw_get_port_range rule_src_port $rule_src_port fw_get_port_range rule_src_port $rule_src_port
fw_get_port_range rule_dest_port $rule_dest_port fw_get_port_range rule_dest_port $rule_dest_port
local table=f
local chain=input local chain=input
[ -n "$rule_src" ] && { if [ "$rule_target" == "NOTRACK" ]; then
[ -z "$rule_dest" ] && { table=r
chain=zone_${rule_src} chain="zone_${rule_src}_notrack"
} || { elif [ -n "$rule_src" ]; then
chain=zone_${rule_src}_forward chain="zone_${rule_src}${rule_dest:+_forward}"
} fi
}
local target=$rule_target local target="${rule_target:-REJECT}"
[ -z "$target" ] && { [ -n "$dest" ] && target="zone_${rule_dest}_${target}"
target=REJECT
}
[ -n "$dest" ] && {
target=zone_${rule_dest}_${target}
}
local mode local mode
fw_get_family_mode mode ${rule_family:-x} $rule_src I 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 local rule_pos
eval 'rule_pos=$((++FW__RULE_COUNT_'$mode'_'$chain'))' eval 'rule_pos=$((++FW__RULE_COUNT_'$mode'_'$chain'))'
[ "$rule_proto" == "tcpudp" ] && rule_proto="tcp udp" [ "$rule_proto" == "tcpudp" ] && rule_proto="tcp udp"
for rule_proto in $rule_proto; do 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_proto:+-p $rule_proto} \
${rule_src_ip:+-s $rule_src_ip/$rule_src_ip_prefixlen} \
${rule_src_port:+--sport $rule_src_port} \ ${rule_src_port:+--sport $rule_src_port} \
${rule_src_mac:+-m mac --mac-source $rule_src_mac} \ ${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_dest_port:+--dport $rule_dest_port} \
${rule_icmp_type:+--icmp-type $rule_icmp_type} \ ${rule_icmp_type:+--icmp-type $rule_icmp_type} \
} }

View File

@ -218,3 +218,12 @@ fw_get_family_mode() {
esac 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}"
}