From: Joseph Roback <openwrt-devel@roback.cc>
[openwrt.git] / package / firewall / files / lib / core_interface.sh
index 56ff1a1..c2a5bd7 100644 (file)
@@ -11,7 +11,12 @@ fw_configure_interface() {
                [ "$status" == 1 ] || [ -n "$aliasnet" ] || return 0
        }
 
-       [ -n "$ifname" ] || ifname=$(uci_get_state network "$iface" ifname "$iface")
+       [ -n "$ifname" ] || {
+               ifname=$(uci_get_state network "$iface" ifname)
+               ifname="${ifname%%:*}"
+               [ -z "$ifname" ] && return 0
+       }
+
        [ "$ifname" == "lo" ] && return 0
 
        fw_callback pre interface
@@ -22,22 +27,24 @@ fw_configure_interface() {
                local chain=zone_${zone}
                local ifname=$3
                local subnet=$4
+               local masq_src=$5
+               local masq_dest=$6
 
                local inet onet
                local mode=$(fw_get_family_mode x $zone i)
 
                case "$mode/$subnet" in
                        # Zone supports v6 only or dual, need v6
-                       6/*:*|i/*:*)
-                               inet="{ -s $subnet -d ::/0 }"
-                               onet="{ -s ::/0 -d $subnet }"
+                       G6/*:*|i/*:*)
+                               inet="-s $subnet -d ::/0"
+                               onet="-s ::/0 -d $subnet"
                                mode=6
                        ;;
 
                        # Zone supports v4 only or dual, need v4
-                       4/*.*.*.*|i/*.*.*.*)
-                               inet="{ -s $subnet -d 0.0.0.0/0 }"
-                               onet="{ -s 0.0.0.0/0 -d $subnet }"
+                       G4/*.*.*.*|i/*.*.*.*)
+                               inet="-s $subnet -d 0.0.0.0/0"
+                               onet="-s 0.0.0.0/0 -d $subnet"
                                mode=4
                        ;;
 
@@ -48,34 +55,48 @@ fw_configure_interface() {
                        */*.*) fw_log info "zone $zone does not support IPv4 address family, skipping"; return ;;
                esac
 
-               fw $action $mode f ${chain}_ACCEPT ACCEPT ^ $onet { -o "$ifname" }
-               fw $action $mode f ${chain}_ACCEPT ACCEPT ^ $inet { -i "$ifname" }
-               fw $action $mode f ${chain}_DROP   DROP   ^ $onet { -o "$ifname" }
-               fw $action $mode f ${chain}_DROP   DROP   ^ $inet { -i "$ifname" }
-               fw $action $mode f ${chain}_REJECT reject ^ $onet { -o "$ifname" }
-               fw $action $mode f ${chain}_REJECT reject ^ $inet { -i "$ifname" }
+               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 }
+               fw $action $mode f ${chain}_DROP   DROP   $ { -i "$ifname" $inet }
+               fw $action $mode f ${chain}_REJECT reject $ { -o "$ifname" $onet }
+               fw $action $mode f ${chain}_REJECT reject $ { -i "$ifname" $inet }
+
+               # NB: if MASQUERADING for IPv6 becomes available we'll need a family check here
+               local msrc mdst
+               for msrc in ${masq_src:-0.0.0.0/0}; do
+                       [ "${msrc#!}" != "$msrc" ] && msrc="! -s ${msrc#!}" || msrc="-s $msrc"
+                       for mdst in ${subnet:-${masq_dest:-0.0.0.0/0}}; do
+                               [ "${mdst#!}" != "$mdst" ] && mdst="! -d ${mdst#!}" || mdst="-d $mdst"
+                               fw $action $mode n ${chain}_nat MASQUERADE $ { -o "$ifname" $msrc $mdst }
+                       done
+               done
 
-               fw $action $mode n ${chain}_nat MASQUERADE ^ $onet { -o "$ifname" }
-               fw $action $mode f ${chain}_MSSFIX TCPMSS  ^ $onet { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu }
+               fw $action $mode f ${chain}_MSSFIX TCPMSS  $ { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu $onet }
 
-               fw $action $mode f input   ${chain}         $ $inet { -i "$ifname" }
-               fw $action $mode f forward ${chain}_forward $ $inet { -i "$ifname" }
-               fw $action $mode n PREROUTING ${chain}_prerouting ^ $inet { -i "$ifname" }
-               fw $action $mode r PREROUTING ${chain}_notrack    ^ $inet { -i "$ifname" }
+               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 }
        }
 
-       local old_zones old_ifname old_subnet
+       local old_zones old_ifname old_subnets old_masq_src old_masq_dest
        config_get old_zones core "${iface}_zone"
        [ -n "$old_zones" ] && {
                config_get old_ifname core "${iface}_ifname"
-               config_get old_subnet core "${iface}_subnet"
+               config_get old_subnets core "${iface}_subnets"
+               config_get old_masq_src core "${iface}_masq_src"
+               config_get old_masq_dest core "${iface}_masq_dest"
 
                local z
                for z in $old_zones; do
-                       fw_log info "removing $iface ($old_ifname${old_subnet:+ alias $old_subnet}) from zone $z"
-                       fw__do_rules del $z $old_ifname $old_subnet
+                       local n
+                       for n in ${old_subnets:-""}; do
+                               fw_log info "removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
+                               fw__do_rules del $z $old_ifname $n "$old_masq_src" "$old_masq_dest"
+                       done
 
-                       [ -n "$old_subnet" ] || ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
+                       [ -n "$old_subnets" ] || ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
                done
 
                local old_aliases
@@ -88,25 +109,14 @@ fw_configure_interface() {
 
                uci_revert_state firewall core "${iface}_zone"
                uci_revert_state firewall core "${iface}_ifname"
-               uci_revert_state firewall core "${iface}_subnet"
+               uci_revert_state firewall core "${iface}_subnets"
                uci_revert_state firewall core "${iface}_aliases"
+               uci_revert_state firewall core "${iface}_masq_src"
+               uci_revert_state firewall core "${iface}_masq_dest"
        }
 
        [ "$action" == del ] && return
 
-       local new_zones=
-       load_zone() {
-               fw_config_get_zone "$1"
-               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
-               append new_zones $zone_name
-
-               [ -n "$aliasnet" ] || ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
-       }
-       config_foreach load_zone zone
-
        [ -z "$aliasnet" ] && {
                local aliases
                config_get aliases "$iface" aliases
@@ -118,19 +128,44 @@ fw_configure_interface() {
                        config_get netmask "$a" netmask
                        config_get ip6addr "$a" ip6addr
 
-                       [ -n "$ipaddr" ] && fw_configure_interface "$a" add "$ifname" "$ipaddr${netmask:+/$netmask}"
-                       [ -n "$ip6addr" ] && fw_configure_interface "$a" add "$ifname" "$ip6addr"
+                       [ -n "$ipaddr" ] && fw_configure_interface "$a" add "" "$ipaddr${netmask:+/$netmask}"
+                       [ -n "$ip6addr" ] && fw_configure_interface "$a" add "" "$ip6addr"
                done
 
                fw_sysctl_interface $ifname
                fw_callback post interface
 
                uci_set_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"
+       }
+
+       local new_zones=
+       local new_masq_src=
+       local new_masq_dest=
+       load_zone() {
+               fw_config_get_zone "$1"
+               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" "$zone_masq_src" "$zone_masq_dest"
+               append new_zones $zone_name
+               append new_masq_src "$zone_masq_src"
+               append new_masq_dest "$zone_masq_dest"
+
+               [ -n "$aliasnet" ] || 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_set_state firewall core "${iface}_subnet" "$aliasnet"
+       uci_set_state firewall core "${iface}_masq_src" "$new_masq_src"
+       uci_set_state firewall core "${iface}_masq_dest" "$new_masq_dest"
 }
 
 fw_sysctl_interface() {
This page took 0.026765 seconds and 4 git commands to generate.