1 # Copyright (C) 2009-2011 OpenWrt.org
7 local val
="$(uci_get_state firewall core $var)"
8 local e1
; for e1
in $item; do
9 local e2
; for e2
in $val; do
10 [ "$e1" = "$e2" ] && e1
=""
12 val
="${val:+$val${e1:+ }}$e1"
15 uci_toggle_state firewall core
$var "$val"
23 local val
="$(uci_get_state firewall core $var)"
24 local e1
; for e1
in $val; do
25 local e2
; for e2
in $item; do
26 [ "$e1" = "$e2" ] && e1
=""
28 rest
="${rest:+$rest${e1:+ }}$e1"
31 uci_toggle_state firewall core
$var "$val"
34 fw_configure_interface
() {
40 [ "$action" == "add" ] && {
41 local status
=$
(uci_get_state network
"$iface" up
0)
42 [ "$status" == 1 ] ||
[ -n "$aliasnet" ] ||
return 0
46 ifname
=$
(uci_get_state network
"$iface" ifname
)
47 ifname
="${ifname%%:*}"
48 [ -z "$ifname" ] && return 0
51 [ "$ifname" == "lo" ] && return 0
53 fw_callback pre interface
58 local chain
=zone_
${zone}
63 fw_get_family_mode mode x
$zone i
65 case "$mode/$subnet" in
66 # Zone supports v6 only or dual, need v6
68 inet
="-s $subnet -d ::/0"
69 onet
="-s ::/0 -d $subnet"
73 # Zone supports v4 only or dual, need v4
75 inet
="-s $subnet -d 0.0.0.0/0"
76 onet
="-s 0.0.0.0/0 -d $subnet"
80 # Need v6 while zone is v4
81 */*:*) fw_log info
"zone $zone does not support IPv6 address family, skipping"; return ;;
83 # Need v4 while zone is v6
84 */*.
*) fw_log info
"zone $zone does not support IPv4 address family, skipping"; return ;;
87 *) mode
="${mode#G}" ;;
90 lock
/var
/run
/firewall-interface.lock
92 fw
$action $mode f
${chain}_ACCEPT ACCEPT $
{ -o "$ifname" $onet }
93 fw
$action $mode f
${chain}_ACCEPT ACCEPT $
{ -i "$ifname" $inet }
94 fw
$action $mode f
${chain}_DROP DROP $
{ -o "$ifname" $onet }
95 fw
$action $mode f
${chain}_DROP DROP $
{ -i "$ifname" $inet }
96 fw
$action $mode f
${chain}_REJECT reject $
{ -o "$ifname" $onet }
97 fw
$action $mode f
${chain}_REJECT reject $
{ -i "$ifname" $inet }
99 [ "$(uci_get_state firewall core "${zone}_tcpmss
")" == 1 ] && \
100 fw
$action $mode m
${chain}_MSSFIX TCPMSS $ \
101 { -o "$ifname" -p tcp
--tcp-flags SYN
,RST SYN
--clamp-mss-to-pmtu $onet }
103 fw
$action $mode f input
${chain} $
{ -i "$ifname" $inet }
104 fw
$action $mode f forward
${chain}_forward $
{ -i "$ifname" $inet }
105 fw
$action $mode n PREROUTING
${chain}_prerouting $
{ -i "$ifname" $inet }
106 fw
$action $mode r PREROUTING
${chain}_notrack $
{ -i "$ifname" $inet }
107 fw
$action $mode n POSTROUTING
${chain}_nat $
{ -o "$ifname" $onet }
109 lock
-u /var
/run
/firewall-interface.lock
112 local old_zones old_ifname old_subnets
113 config_get old_zones core
"${iface}_zone"
114 [ -n "$old_zones" ] && {
115 config_get old_ifname core
"${iface}_ifname"
116 config_get old_subnets core
"${iface}_subnets"
119 for z
in $old_zones; do
121 for n
in ${old_subnets:-""}; do
122 fw_log info
"removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
123 fw__do_rules del
$z $old_ifname $n
126 [ -n "$old_subnets" ] ||
{
127 fw__uci_state_del
"${z}_networks" "$iface"
128 env
-i ACTION
=remove ZONE
="$z" INTERFACE
="$iface" DEVICE
="$ifname" /sbin
/hotplug-call firewall
133 config_get old_aliases core
"${iface}_aliases"
136 for a
in $old_aliases; do
137 fw_configure_interface
"$a" del
"$old_ifname"
140 uci_revert_state firewall core
"${iface}_zone"
141 uci_revert_state firewall core
"${iface}_ifname"
142 uci_revert_state firewall core
"${iface}_subnets"
143 uci_revert_state firewall core
"${iface}_aliases"
146 [ "$action" == del
] && return
148 [ -z "$aliasnet" ] && {
150 config_get aliases
"$iface" aliases
153 for a
in $aliases; do
154 local ipaddr netmask ip6addr
155 config_get ipaddr
"$a" ipaddr
156 config_get netmask
"$a" netmask
157 config_get ip6addr
"$a" ip6addr
159 [ -n "$ipaddr" ] && fw_configure_interface
"$a" add
"" "$ipaddr${netmask:+/$netmask}"
160 [ -n "$ip6addr" ] && fw_configure_interface
"$a" add
"" "$ip6addr"
163 fw_sysctl_interface
$ifname
164 fw_callback post interface
166 uci_toggle_state firewall core
"${iface}_aliases" "$aliases"
169 config_get subnets core
"${iface}_subnets"
170 append subnets
"$aliasnet"
172 config_set core
"${iface}_subnets" "$subnets"
173 uci_toggle_state firewall core
"${iface}_subnets" "$subnets"
178 fw_config_get_zone
"$1"
179 list_contains zone_network
"$iface" ||
return
181 fw_log info
"adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name"
182 fw__do_rules add
${zone_name} "$ifname" "$aliasnet"
183 append new_zones
$zone_name
185 [ -n "$aliasnet" ] ||
{
186 fw__uci_state_add
"${zone_name}_networks" "${zone_network}"
187 env
-i ACTION
=add ZONE
="$zone_name" INTERFACE
="$iface" DEVICE
="$ifname" /sbin
/hotplug-call firewall
190 config_foreach load_zone zone
192 uci_toggle_state firewall core
"${iface}_zone" "$new_zones"
193 uci_toggle_state firewall core
"${iface}_ifname" "$ifname"
196 fw_sysctl_interface
() {
199 sysctl
-w net.ipv4.conf.
${ifname}.accept_redirects
=$FW_ACCEPT_REDIRECTS
200 sysctl
-w net.ipv6.conf.
${ifname}.accept_redirects
=$FW_ACCEPT_REDIRECTS
201 sysctl
-w net.ipv4.conf.
${ifname}.accept_source_route
=$FW_ACCEPT_SRC_ROUTE
202 sysctl
-w net.ipv6.conf.
${ifname}.accept_source_route
=$FW_ACCEPT_SRC_ROUTE
203 } >/dev
/null
2>/dev
/null