[package] firewall: protect iptables invocations with locks in interface ops, it...
[openwrt/svn-archive/archive.git] / package / firewall / files / lib / core_interface.sh
1 # Copyright (C) 2009-2010 OpenWrt.org
2
3 fw__uci_state_add() {
4 local var="$1"
5 local item="$2"
6
7 local val="$(uci_get_state firewall core $var)"
8 uci_set_state firewall core $var "${val:+$val }$item"
9 }
10
11 fw__uci_state_del() {
12 local var="$1"
13 local item="$2"
14
15 local val=" $(uci_get_state firewall core $var) "
16 val="${val// $item / }"
17 val="${val# }"
18 val="${val% }"
19 uci_set_state firewall core $var "$val"
20 }
21
22 fw_configure_interface() {
23 local iface=$1
24 local action=$2
25 local ifname=$3
26 local aliasnet=$4
27
28 [ "$action" == "add" ] && {
29 local status=$(uci_get_state network "$iface" up 0)
30 [ "$status" == 1 ] || [ -n "$aliasnet" ] || return 0
31 }
32
33 [ -n "$ifname" ] || {
34 ifname=$(uci_get_state network "$iface" ifname)
35 ifname="${ifname%%:*}"
36 [ -z "$ifname" ] && return 0
37 }
38
39 [ "$ifname" == "lo" ] && return 0
40
41 fw_callback pre interface
42
43 fw__do_rules() {
44 local action=$1
45 local zone=$2
46 local chain=zone_${zone}
47 local ifname=$3
48 local subnet=$4
49
50 local inet onet mode
51 fw_get_family_mode mode x $zone i
52
53 case "$mode/$subnet" in
54 # Zone supports v6 only or dual, need v6
55 G6/*:*|i/*:*)
56 inet="-s $subnet -d ::/0"
57 onet="-s ::/0 -d $subnet"
58 mode=6
59 ;;
60
61 # Zone supports v4 only or dual, need v4
62 G4/*.*.*.*|i/*.*.*.*)
63 inet="-s $subnet -d 0.0.0.0/0"
64 onet="-s 0.0.0.0/0 -d $subnet"
65 mode=4
66 ;;
67
68 # Need v6 while zone is v4
69 */*:*) fw_log info "zone $zone does not support IPv6 address family, skipping"; return ;;
70
71 # Need v4 while zone is v6
72 */*.*) fw_log info "zone $zone does not support IPv4 address family, skipping"; return ;;
73 esac
74
75 lock /var/run/firewall-interface.lock
76
77 fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -o "$ifname" $onet }
78 fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -i "$ifname" $inet }
79 fw $action $mode f ${chain}_DROP DROP $ { -o "$ifname" $onet }
80 fw $action $mode f ${chain}_DROP DROP $ { -i "$ifname" $inet }
81 fw $action $mode f ${chain}_REJECT reject $ { -o "$ifname" $onet }
82 fw $action $mode f ${chain}_REJECT reject $ { -i "$ifname" $inet }
83
84 fw $action $mode f ${chain}_MSSFIX TCPMSS $ { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu $onet }
85
86 fw $action $mode f input ${chain} $ { -i "$ifname" $inet }
87 fw $action $mode f forward ${chain}_forward $ { -i "$ifname" $inet }
88 fw $action $mode n PREROUTING ${chain}_prerouting $ { -i "$ifname" $inet }
89 fw $action $mode r PREROUTING ${chain}_notrack $ { -i "$ifname" $inet }
90 fw $action $mode n POSTROUTING ${chain}_nat $ { -o "$ifname" $onet }
91
92 lock -u /var/run/firewall-interface.lock
93 }
94
95 local old_zones old_ifname old_subnets
96 config_get old_zones core "${iface}_zone"
97 [ -n "$old_zones" ] && {
98 config_get old_ifname core "${iface}_ifname"
99 config_get old_subnets core "${iface}_subnets"
100
101 local z
102 for z in $old_zones; do
103 local n
104 for n in ${old_subnets:-""}; do
105 fw_log info "removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
106 fw__do_rules del $z $old_ifname $n
107 done
108
109 [ -n "$old_subnets" ] || {
110 fw__uci_state_del "${z}_networks" "$iface"
111 env -i ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
112 }
113 done
114
115 local old_aliases
116 config_get old_aliases core "${iface}_aliases"
117
118 local a
119 for a in $old_aliases; do
120 fw_configure_interface "$a" del "$old_ifname"
121 done
122
123 uci_revert_state firewall core "${iface}_zone"
124 uci_revert_state firewall core "${iface}_ifname"
125 uci_revert_state firewall core "${iface}_subnets"
126 uci_revert_state firewall core "${iface}_aliases"
127 }
128
129 [ "$action" == del ] && return
130
131 [ -z "$aliasnet" ] && {
132 local aliases
133 config_get aliases "$iface" aliases
134
135 local a
136 for a in $aliases; do
137 local ipaddr netmask ip6addr
138 config_get ipaddr "$a" ipaddr
139 config_get netmask "$a" netmask
140 config_get ip6addr "$a" ip6addr
141
142 [ -n "$ipaddr" ] && fw_configure_interface "$a" add "" "$ipaddr${netmask:+/$netmask}"
143 [ -n "$ip6addr" ] && fw_configure_interface "$a" add "" "$ip6addr"
144 done
145
146 fw_sysctl_interface $ifname
147 fw_callback post interface
148
149 uci_set_state firewall core "${iface}_aliases" "$aliases"
150 } || {
151 local subnets=
152 config_get subnets core "${iface}_subnets"
153 append subnets "$aliasnet"
154
155 config_set core "${iface}_subnets" "$subnets"
156 uci_set_state firewall core "${iface}_subnets" "$subnets"
157 }
158
159 local new_zones=
160 load_zone() {
161 fw_config_get_zone "$1"
162 list_contains zone_network "$iface" || return
163
164 fw_log info "adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name"
165 fw__do_rules add ${zone_name} "$ifname" "$aliasnet"
166 append new_zones $zone_name
167
168 [ -n "$aliasnet" ] || {
169 fw__uci_state_add "${zone_name}_networks" "${zone_network}"
170 env -i ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
171 }
172 }
173 config_foreach load_zone zone
174
175 uci_set_state firewall core "${iface}_zone" "$new_zones"
176 uci_set_state firewall core "${iface}_ifname" "$ifname"
177 }
178
179 fw_sysctl_interface() {
180 local ifname=$1
181 {
182 sysctl -w net.ipv4.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
183 sysctl -w net.ipv6.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
184 sysctl -w net.ipv4.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
185 sysctl -w net.ipv6.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
186 } >/dev/null 2>/dev/null
187 }
188