1 # Copyright (C) 2009-2010 OpenWrt.org
3 fw_configure_interface
() {
9 [ "$action" == "add" ] && {
10 local status
=$
(uci_get_state network
"$iface" up
0)
11 [ "$status" == 1 ] ||
[ -n "$aliasnet" ] ||
return 0
15 ifname
=$
(uci_get_state network
"$iface" ifname
"$iface")
16 ifname
="${ifname%%:*}"
19 [ "$ifname" == "lo" ] && return 0
21 fw_callback pre interface
26 local chain
=zone_
${zone}
31 local mode
=$
(fw_get_family_mode x
$zone i
)
33 case "$mode/$subnet" in
34 # Zone supports v6 only or dual, need v6
36 inet
="{ -s $subnet -d ::/0 }"
37 onet
="{ -s ::/0 -d $subnet }"
41 # Zone supports v4 only or dual, need v4
43 inet
="{ -s $subnet -d 0.0.0.0/0 }"
44 onet
="{ -s 0.0.0.0/0 -d $subnet }"
48 # Need v6 while zone is v4
49 */*:*) fw_log info
"zone $zone does not support IPv6 address family, skipping"; return ;;
51 # Need v4 while zone is v6
52 */*.
*) fw_log info
"zone $zone does not support IPv4 address family, skipping"; return ;;
55 fw
$action $mode f
${chain}_ACCEPT ACCEPT ^
$onet { -o "$ifname" }
56 fw
$action $mode f
${chain}_ACCEPT ACCEPT ^
$inet { -i "$ifname" }
57 fw
$action $mode f
${chain}_DROP DROP ^
$onet { -o "$ifname" }
58 fw
$action $mode f
${chain}_DROP DROP ^
$inet { -i "$ifname" }
59 fw
$action $mode f
${chain}_REJECT reject ^
$onet { -o "$ifname" }
60 fw
$action $mode f
${chain}_REJECT reject ^
$inet { -i "$ifname" }
62 fw
$action $mode n
${chain}_nat MASQUERADE ^
$onet { -o "$ifname" }
63 fw
$action $mode f
${chain}_MSSFIX TCPMSS ^
$onet { -o "$ifname" -p tcp
--tcp-flags SYN
,RST SYN
--clamp-mss-to-pmtu }
65 fw
$action $mode f input
${chain} $
$inet { -i "$ifname" }
66 fw
$action $mode f forward
${chain}_forward $
$inet { -i "$ifname" }
67 fw
$action $mode n PREROUTING
${chain}_prerouting ^
$inet { -i "$ifname" }
68 fw
$action $mode r PREROUTING
${chain}_notrack ^
$inet { -i "$ifname" }
71 local old_zones old_ifname old_subnets
72 config_get old_zones core
"${iface}_zone"
73 [ -n "$old_zones" ] && {
74 config_get old_ifname core
"${iface}_ifname"
75 config_get old_subnets core
"${iface}_subnets"
78 for z
in $old_zones; do
80 for n
in ${old_subnets:-""}; do
81 fw_log info
"removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
82 fw__do_rules del
$z $old_ifname $n
85 [ -n "$old_subnets" ] || ACTION
=remove ZONE
="$z" INTERFACE
="$iface" DEVICE
="$ifname" /sbin
/hotplug-call firewall
89 config_get old_aliases core
"${iface}_aliases"
92 for a
in $old_aliases; do
93 fw_configure_interface
"$a" del
"$old_ifname"
96 uci_revert_state firewall core
"${iface}_zone"
97 uci_revert_state firewall core
"${iface}_ifname"
98 uci_revert_state firewall core
"${iface}_subnets"
99 uci_revert_state firewall core
"${iface}_aliases"
102 [ "$action" == del
] && return
106 fw_config_get_zone
"$1"
107 list_contains zone_network
"$iface" ||
return
109 fw_log info
"adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name"
110 fw__do_rules add
${zone_name} "$ifname" $aliasnet
111 append new_zones
$zone_name
113 [ -n "$aliasnet" ] || ACTION
=add ZONE
="$zone_name" INTERFACE
="$iface" DEVICE
="$ifname" /sbin
/hotplug-call firewall
115 config_foreach load_zone zone
117 [ -z "$aliasnet" ] && {
119 config_get aliases
"$iface" aliases
122 for a
in $aliases; do
123 local ipaddr netmask ip6addr
124 config_get ipaddr
"$a" ipaddr
125 config_get netmask
"$a" netmask
126 config_get ip6addr
"$a" ip6addr
128 [ -n "$ipaddr" ] && fw_configure_interface
"$a" add
"" "$ipaddr${netmask:+/$netmask}"
129 [ -n "$ip6addr" ] && fw_configure_interface
"$a" add
"" "$ip6addr"
132 fw_sysctl_interface
$ifname
133 fw_callback post interface
135 uci_set_state firewall core
"${iface}_aliases" "$aliases"
138 config_get subnets core
"${iface}_subnets"
139 append subnets
"$aliasnet"
141 config_set core
"${iface}_subnets" "$subnets"
142 uci_set_state firewall core
"${iface}_subnets" "$subnets"
145 uci_set_state firewall core
"${iface}_zone" "$new_zones"
146 uci_set_state firewall core
"${iface}_ifname" "$ifname"
149 fw_sysctl_interface
() {
152 sysctl
-w net.ipv4.conf.
${ifname}.accept_redirects
=$FW_ACCEPT_REDIRECTS
153 sysctl
-w net.ipv6.conf.
${ifname}.accept_redirects
=$FW_ACCEPT_REDIRECTS
154 sysctl
-w net.ipv4.conf.
${ifname}.accept_source_route
=$FW_ACCEPT_SRC_ROUTE
155 sysctl
-w net.ipv6.conf.
${ifname}.accept_source_route
=$FW_ACCEPT_SRC_ROUTE
156 } >/dev
/null
2>/dev
/null