[ -n "$src" -a -z "$dest" ] && ZONE=zone_$src
[ -n "$src" -a -n "$dest" ] && ZONE=zone_${src}_forward
[ -n "$dest" ] && TARGET=zone_${dest}_$target
+
+ eval 'RULE_COUNT=$((++RULE_COUNT_'$ZONE'))'
+
add_rule() {
- $IPTABLES -A $ZONE \
+ $IPTABLES -I $ZONE $RULE_COUNT \
${proto:+-p $proto} \
${icmp_type:+--icmp-type $icmp_type} \
${src_ip:+-s $src_ip} \
config_get src $1 src
config_get src_ip $1 src_ip
+ config_get src_dip $1 src_dip
config_get src_port $1 src_port
config_get src_dport $1 src_dport
config_get src_mac $1 src_mac
src_port_first=${src_port%-*}
src_port_last=${src_port#*-}
- [ "$src_port_first" -ne "$src_port_last" ] && { \
+ [ "$src_port_first" != "$src_port_last" ] && { \
src_port="$src_port_first:$src_port_last"; }
src_dport_first=${src_dport%-*}
src_dport_last=${src_dport#*-}
- [ "$src_dport_first" -ne "$src_dport_last" ] && { \
+ [ "$src_dport_first" != "$src_dport_last" ] && { \
src_dport="$src_dport_first:$src_dport_last"; }
dest_port2=${dest_port:-$src_dport}
dest_port_first=${dest_port2%-*}
dest_port_last=${dest_port2#*-}
- [ "$dest_port_first" -ne "$dest_port_last" ] && { \
+ [ "$dest_port_first" != "$dest_port_last" ] && { \
dest_port2="$dest_port_first:$dest_port_last"; }
add_rule() {
$IPTABLES -A zone_${src}_prerouting -t nat \
${proto:+-p $proto} \
${src_ip:+-s $src_ip} \
+ ${src_dip:+-d $src_dip} \
${src_port:+--sport $src_port} \
${src_dport:+--dport $src_dport} \
${src_mac:+-m mac --mac-source $src_mac} \
[ -e $path ] && . $path
}
+get_interface_zones() {
+ local interface="$2"
+ local name
+ local network
+ config_get name $1 name
+ config_get network $1 network
+ [ -z "$network" ] && network=$name
+ for n in $network; do
+ [ "$n" = "$interface" ] && append add_zone "$name"
+ done
+}
+
+fw_event() {
+ local action="$1"
+ local interface="$2"
+ local ifname="$(sh -c ". /etc/functions.sh; include /lib/network; scan_interfaces; config_get "$interface" ifname")"
+ add_zone=
+ local up
+
+ [ -z "$ifname" ] && return 0
+ config_foreach get_interface_zones zone "$interface"
+ [ -z "$add_zone" ] && return 0
+
+ case "$action" in
+ ifup)
+ for z in $add_zone; do
+ local loaded
+ config_get loaded core loaded
+ [ -n "$loaded" ] && addif "$interface" "$ifname" "$z"
+ done
+ ;;
+ ifdown)
+ config_get up "$interface" up
+
+ for z in $ZONE; do
+ [ "$up" == "1" ] && delif "$interface" "$ifname" "$z"
+ done
+ ;;
+ esac
+}
+
fw_addif() {
local up
local ifname
config_get up $1 up
- config_get ifname $1 ifname
[ -n "$up" ] || return 0
- (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/20-firewall)
+ fw_event ifup "$1"
}
fw_custom_chains() {
echo "Loading zone defaults"
config_foreach fw_zone_defaults zone
uci_set_state firewall core loaded 1
+ config_set core loaded 1
config_foreach fw_check_notrack zone
- unset CONFIG_APPEND
- config_load network
- config_foreach fw_addif interface
+ INTERFACES="$(sh -c '
+ . /etc/functions.sh; config_load network
+ echo_up() { local up; config_get_bool up "$1" up 0; [ $up = 1 ] && echo "$1"; }
+ config_foreach echo_up interface
+ ')"
+ for interface in $INTERFACES; do
+ fw_event ifup "$interface"
+ done
}
fw_stop() {