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 fw
$action $mode f
${chain}_MSSFIX TCPMSS $
{ -o "$ifname" -p tcp
--tcp-flags SYN
,RST SYN
--clamp-mss-to-pmtu $onet }
101 fw
$action $mode f input
${chain} $
{ -i "$ifname" $inet }
102 fw
$action $mode f forward
${chain}_forward $
{ -i "$ifname" $inet }
103 fw
$action $mode n PREROUTING
${chain}_prerouting $
{ -i "$ifname" $inet }
104 fw
$action $mode r PREROUTING
${chain}_notrack $
{ -i "$ifname" $inet }
105 fw
$action $mode n POSTROUTING
${chain}_nat $
{ -o "$ifname" $onet }
107 lock
-u /var
/run
/firewall-interface.lock
110 local old_zones old_ifname old_subnets
111 config_get old_zones core
"${iface}_zone"
112 [ -n "$old_zones" ] && {
113 config_get old_ifname core
"${iface}_ifname"
114 config_get old_subnets core
"${iface}_subnets"
117 for z
in $old_zones; do
119 for n
in ${old_subnets:-""}; do
120 fw_log info
"removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
121 fw__do_rules del
$z $old_ifname $n
124 [ -n "$old_subnets" ] ||
{
125 fw__uci_state_del
"${z}_networks" "$iface"
126 env
-i ACTION
=remove ZONE
="$z" INTERFACE
="$iface" DEVICE
="$ifname" /sbin
/hotplug-call firewall
131 config_get old_aliases core
"${iface}_aliases"
134 for a
in $old_aliases; do
135 fw_configure_interface
"$a" del
"$old_ifname"
138 uci_revert_state firewall core
"${iface}_zone"
139 uci_revert_state firewall core
"${iface}_ifname"
140 uci_revert_state firewall core
"${iface}_subnets"
141 uci_revert_state firewall core
"${iface}_aliases"
144 [ "$action" == del
] && return
146 [ -z "$aliasnet" ] && {
148 config_get aliases
"$iface" aliases
151 for a
in $aliases; do
152 local ipaddr netmask ip6addr
153 config_get ipaddr
"$a" ipaddr
154 config_get netmask
"$a" netmask
155 config_get ip6addr
"$a" ip6addr
157 [ -n "$ipaddr" ] && fw_configure_interface
"$a" add
"" "$ipaddr${netmask:+/$netmask}"
158 [ -n "$ip6addr" ] && fw_configure_interface
"$a" add
"" "$ip6addr"
161 fw_sysctl_interface
$ifname
162 fw_callback post interface
164 uci_toggle_state firewall core
"${iface}_aliases" "$aliases"
167 config_get subnets core
"${iface}_subnets"
168 append subnets
"$aliasnet"
170 config_set core
"${iface}_subnets" "$subnets"
171 uci_toggle_state firewall core
"${iface}_subnets" "$subnets"
176 fw_config_get_zone
"$1"
177 list_contains zone_network
"$iface" ||
return
179 fw_log info
"adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name"
180 fw__do_rules add
${zone_name} "$ifname" "$aliasnet"
181 append new_zones
$zone_name
183 [ -n "$aliasnet" ] ||
{
184 fw__uci_state_add
"${zone_name}_networks" "${zone_network}"
185 env
-i ACTION
=add ZONE
="$zone_name" INTERFACE
="$iface" DEVICE
="$ifname" /sbin
/hotplug-call firewall
188 config_foreach load_zone zone
190 uci_toggle_state firewall core
"${iface}_zone" "$new_zones"
191 uci_toggle_state firewall core
"${iface}_ifname" "$ifname"
194 fw_sysctl_interface
() {
197 sysctl
-w net.ipv4.conf.
${ifname}.accept_redirects
=$FW_ACCEPT_REDIRECTS
198 sysctl
-w net.ipv6.conf.
${ifname}.accept_redirects
=$FW_ACCEPT_REDIRECTS
199 sysctl
-w net.ipv4.conf.
${ifname}.accept_source_route
=$FW_ACCEPT_SRC_ROUTE
200 sysctl
-w net.ipv6.conf.
${ifname}.accept_source_route
=$FW_ACCEPT_SRC_ROUTE
201 } >/dev
/null
2>/dev
/null