X-Git-Url: https://git.rohieb.name/openwrt.git/blobdiff_plain/94c817eadb20a09468bef10f0e43036ab2012c89..d4ce92dbb7142a3465885fda6d7d4801eb98b74e:/package/firewall/files/lib/core_interface.sh diff --git a/package/firewall/files/lib/core_interface.sh b/package/firewall/files/lib/core_interface.sh index 69ddc343a..3d6718431 100644 --- a/package/firewall/files/lib/core_interface.sh +++ b/package/firewall/files/lib/core_interface.sh @@ -1,4 +1,35 @@ -# Copyright (C) 2009-2010 OpenWrt.org +# Copyright (C) 2009-2012 OpenWrt.org + +fw__uci_state_add() { + local var="$1" + local item="$2" + + local val="$(uci_get_state firewall core $var)" + local e1; for e1 in $item; do + local e2; for e2 in $val; do + [ "$e1" = "$e2" ] && e1="" + done + val="${val:+$val${e1:+ }}$e1" + done + + uci_toggle_state firewall core $var "$val" +} + +fw__uci_state_del() { + local var="$1" + local item="$2" + + local rest="" + local val="$(uci_get_state firewall core $var)" + local e1; for e1 in $val; do + local e2; for e2 in $item; do + [ "$e1" = "$e2" ] && e1="" + done + rest="${rest:+$rest${e1:+ }}$e1" + done + + uci_toggle_state firewall core $var "$rest" +} fw_configure_interface() { local iface=$1 @@ -12,8 +43,9 @@ fw_configure_interface() { } [ -n "$ifname" ] || { - ifname=$(uci_get_state network "$iface" ifname "$iface") + ifname=$(uci_get_state network "$iface" ifname) ifname="${ifname%%:*}" + [ -z "$ifname" ] && return 0 } [ "$ifname" == "lo" ] && return 0 @@ -27,8 +59,8 @@ fw_configure_interface() { local ifname=$3 local subnet=$4 - local inet onet - local mode=$(fw_get_family_mode x $zone i) + local inet onet mode + fw_get_family_mode mode x $zone i case "$mode/$subnet" in # Zone supports v6 only or dual, need v6 @@ -50,8 +82,13 @@ fw_configure_interface() { # Need v4 while zone is v6 */*.*) fw_log info "zone $zone does not support IPv4 address family, skipping"; return ;; + + # Strip prefix + *) mode="${mode#G}" ;; esac + lock /var/run/firewall-interface.lock + fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -o "$ifname" $onet } fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -i "$ifname" $inet } fw $action $mode f ${chain}_DROP DROP $ { -o "$ifname" $onet } @@ -59,13 +96,17 @@ fw_configure_interface() { fw $action $mode f ${chain}_REJECT reject $ { -o "$ifname" $onet } fw $action $mode f ${chain}_REJECT reject $ { -i "$ifname" $inet } - fw $action $mode n ${chain}_nat MASQUERADE $ { -o "$ifname" $onet } - fw $action $mode f ${chain}_MSSFIX TCPMSS $ { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu $onet } + [ "$(uci_get_state firewall core "${zone}_tcpmss")" == 1 ] && \ + fw $action $mode m ${chain}_MSSFIX TCPMSS $ \ + { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu $onet } fw $action $mode f input ${chain} $ { -i "$ifname" $inet } fw $action $mode f forward ${chain}_forward $ { -i "$ifname" $inet } fw $action $mode n PREROUTING ${chain}_prerouting $ { -i "$ifname" $inet } fw $action $mode r PREROUTING ${chain}_notrack $ { -i "$ifname" $inet } + fw $action $mode n POSTROUTING ${chain}_nat $ { -o "$ifname" $onet } + + lock -u /var/run/firewall-interface.lock } local old_zones old_ifname old_subnets @@ -82,7 +123,10 @@ fw_configure_interface() { fw__do_rules del $z $old_ifname $n done - [ -n "$old_subnets" ] || ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall + [ -n "$old_subnets" ] || { + fw__uci_state_del "${z}_networks" "$iface" + env -i ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall + } done local old_aliases @@ -119,14 +163,14 @@ fw_configure_interface() { fw_sysctl_interface $ifname fw_callback post interface - uci_set_state firewall core "${iface}_aliases" "$aliases" + uci_toggle_state firewall core "${iface}_aliases" "$aliases" } || { local subnets= config_get subnets core "${iface}_subnets" append subnets "$aliasnet" config_set core "${iface}_subnets" "$subnets" - uci_set_state firewall core "${iface}_subnets" "$subnets" + uci_toggle_state firewall core "${iface}_subnets" "$subnets" } local new_zones= @@ -135,15 +179,18 @@ fw_configure_interface() { list_contains zone_network "$iface" || return fw_log info "adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name" - fw__do_rules add ${zone_name} "$ifname" $aliasnet + fw__do_rules add ${zone_name} "$ifname" "$aliasnet" append new_zones $zone_name - [ -n "$aliasnet" ] || ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall + [ -n "$aliasnet" ] || { + fw__uci_state_add "${zone_name}_networks" "${zone_network}" + env -i ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall + } } config_foreach load_zone zone - uci_set_state firewall core "${iface}_zone" "$new_zones" - uci_set_state firewall core "${iface}_ifname" "$ifname" + uci_toggle_state firewall core "${iface}_zone" "$new_zones" + uci_toggle_state firewall core "${iface}_ifname" "$ifname" } fw_sysctl_interface() {