Fixed a typo in the firewall scripts
[openwrt/svn-archive/archive.git] / package / firewall / files / uci_firewall.sh
1 #!/bin/sh
2 # Copyright (C) 2008 John Crispin <blogic@openwrt.org>
3
4 . /etc/functions.sh
5
6 IPTABLES="echo iptables"
7 IPTABLES=iptables
8
9 config_clear
10 include /lib/network
11 scan_interfaces
12
13 CONFIG_APPEND=1
14 config_load firewall
15
16 config fw_zones
17 ZONE_LIST=$CONFIG_SECTION
18
19 DEF_INPUT=DROP
20 DEF_OUTPUT=DROP
21 DEF_FORWARD=DROP
22
23 load_policy() {
24 config_get input $1 input
25 config_get output $1 output
26 config_get forward $1 forward
27
28 [ -z "$input" ] && input=$DEF_INPUT
29 [ -z "$output" ] && output=$DEF_OUTPUT
30 [ -z "$forward" ] && forward=$DEF_FORWARD
31 }
32
33 create_zone() {
34 local exists
35
36 [ "$1" == "loopback" ] && return
37
38 config_get exists $ZONE_LIST $1
39 [ -n "$exists" ] && return
40 config_set $ZONE_LIST $1 1
41
42 $IPTABLES -N zone_$1
43 $IPTABLES -N zone_$1_ACCEPT
44 $IPTABLES -N zone_$1_DROP
45 $IPTABLES -N zone_$1_REJECT
46 $IPTABLES -N zone_$1_forward
47 $IPTABLES -A zone_$1_forward -j zone_$1_$5
48 $IPTABLES -A zone_$1 -j zone_$1_$3
49 $IPTABLES -A OUTPUT -j zone_$1_$4
50 $IPTABLES -N zone_$1_nat -t nat
51 $IPTABLES -N zone_$1_prerouting -t nat
52 [ "$6" == "1" ] && $IPTABLES -t nat -A POSTROUTING -j zone_$1_nat
53 }
54
55 addif() {
56 local dev
57 config_get dev core $2
58 [ -n "$dev" -a "$dev" != "$1" ] && delif "$dev" "$2"
59 [ -n "$dev" -a "$dev" == "$1" ] && return
60 logger "adding $1 to firewall zone $2"
61 $IPTABLES -A INPUT -i $1 -j zone_$2
62 $IPTABLES -I zone_$2_ACCEPT 1 -o $1 -j ACCEPT
63 $IPTABLES -I zone_$2_DROP 1 -o $1 -j DROP
64 $IPTABLES -I zone_$2_REJECT 1 -o $1 -j REJECT
65 $IPTABLES -I zone_$2_ACCEPT 1 -i $1 -j ACCEPT
66 $IPTABLES -I zone_$2_DROP 1 -i $1 -j DROP
67 $IPTABLES -I zone_$2_REJECT 1 -i $1 -j REJECT
68 $IPTABLES -I zone_$2_nat 1 -t nat -o $1 -j MASQUERADE
69 $IPTABLES -I PREROUTING 1 -t nat -i $1 -j zone_$2_prerouting
70 $IPTABLES -A FORWARD -i $1 -j zone_$2_forward
71 uci_set_state firewall core "$2" "$1"
72 }
73
74 delif() {
75 logger "removing $1 from firewall zone $2"
76 $IPTABLES -D INPUT -i $1 -j zone_$2
77 $IPTABLES -D zone_$2_ACCEPT -o $1 -j ACCEPT
78 $IPTABLES -D zone_$2_DROP -o $1 -j DROP
79 $IPTABLES -D zone_$2_REJECT -o $1 -j REJECT
80 $IPTABLES -D zone_$2_ACCEPT -i $1 -j ACCEPT
81 $IPTABLES -D zone_$2_DROP -i $1 -j DROP
82 $IPTABLES -D zone_$2_REJECT -i $1 -j REJECT
83 $IPTABLES -D zone_$2_nat -t nat -o $1 -j MASQUERADE
84 $IPTABLES -D PREROUTING -t nat -i $1 -j zone_$2_prerouting
85 $IPTABLES -D FORWARD -i $1 -j zone_$2_forward
86 uci_revert_state firewall core "$2"
87 }
88
89 load_synflood() {
90 echo "Loading synflood protection"
91 $IPTABLES -N SYN_FLOOD
92 $IPTABLES -A SYN_FLOOD -p tcp --syn -m limit --limit ${1}/second --limit-burst $2 -j RETURN
93 $IPTABLES -A SYN_FLOOD -p ! tcp -j RETURN
94 $IPTABLES -A SYN_FLOOD -p tcp ! --syn -j RETURN
95 $IPTABLES -A SYN_FLOOD -j LOG --log-prefix "syn_flood: "
96 $IPTABLES -A SYN_FLOOD -j DROP
97 $IPTABLES -A INPUT -p tcp --syn -j SYN_FLOOD
98 }
99
100 fw_defaults() {
101 load_policy $1
102 DEF_INPUT=$input
103 DEF_OUTPUT=$output
104 DEF_FORWARD=$forward
105
106 echo 1 > /proc/sys/net/ipv4/tcp_syncookies
107 for f in /proc/sys/net/ipv4/conf/*/accept_redirects
108 do
109 echo 0 > $f
110 done
111 for f in /proc/sys/net/ipv4/conf/*/accept_source_route
112 do
113 echo 0 > $f
114 done
115
116 uci_revert_state firewall core
117 uci_set_state firewall core "" firewall_state
118
119 $IPTABLES -F
120 $IPTABLES -t nat -F
121 $IPTABLES -t mangle -F
122 $IPTABLES -X -t nat
123 $IPTABLES -X
124
125 $IPTABLES -P INPUT $input
126 $IPTABLES -A INPUT -m state --state INVALID -j DROP
127 $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
128
129 $IPTABLES -P OUTPUT $output
130 $IPTABLES -A OUTPUT -m state --state INVALID -j DROP
131 $IPTABLES -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
132
133 $IPTABLES -P FORWARD $forward
134 $IPTABLES -A FORWARD -m state --state INVALID -j DROP
135 $IPTABLES -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
136 $IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
137
138 $IPTABLES -A INPUT -i lo -j ACCEPT
139 $IPTABLES -A OUTPUT -o lo -j ACCEPT
140
141 config_get syn_flood $1 syn_flood
142 config_get syn_rate $1 syn_rate
143 config_get syn_burst $1 syn_burst
144
145 [ -z "$syn_rate" ] && syn_rate=25
146 [ -z "$syn_burst" ] && syn_burst=50
147 [ "$syn_flood" == "1" ] && load_synflood $syn_rate $syn_burst
148 }
149
150 fw_zone() {
151 local name
152 local network
153 local masq
154
155 config_get name $1 name
156 config_get network $1 network
157 config_get masq $1 masq
158 load_policy $1
159
160 [ -z "$network" ] && network=$name
161 create_zone "$name" "$network" "$input" "$output" "$forward" "$masq"
162 }
163
164 fw_rule() {
165 local src
166 local src_ip
167 local src_mac
168 local src_port
169 local src_mac
170 local dest
171 local dest_ip
172 local dest_port
173 local proto
174 local target
175 local ruleset
176
177 config_get src $1 src
178 config_get src_ip $1 src_ip
179 config_get src_mac $1 src_mac
180 config_get src_port $1 src_port
181 config_get dest $1 dest
182 config_get dest_ip $1 dest_ip
183 config_get dest_port $1 dest_port
184 config_get proto $1 proto
185 config_get target $1 target
186 config_get ruleset $1 ruleset
187
188 [ -z "$target" ] && target=DROP
189 [ -n "$src" ] && ZONE=zone_$src || ZONE=INPUT
190 [ -n "$dest" ] && TARGET=zone_${dest}_$target || TARGET=$target
191 add_rule() {
192 $IPTABLES -I $ZONE 1 \
193 ${proto:+-p $proto} \
194 ${src_ip:+-s $src_ip} \
195 ${src_port:+--sport $src_port} \
196 ${src_mac:+-m mac --mac-source $src_mac} \
197 ${dest_ip:+-d $dest_ip} \
198 ${dest_port:+--dport $dest_port} \
199 -j $TARGET
200 }
201 [ "$proto" == "tcpudp" -o -z "$proto" ] && {
202 proto=tcp
203 add_rule
204 proto=udp
205 add_rule
206 return
207 }
208 add_rule
209 }
210
211 fw_forwarding() {
212 local src
213 local dest
214 local masq
215
216 config_get src $1 src
217 config_get dest $1 dest
218 [ -n "$src" ] && z_src=zone_${src}_forward || z_src=FORWARD
219 [ -n "$dest" ] && z_dest=zone_${dest}_ACCEPT || z_dest=ACCEPT
220 $IPTABLES -I $z_src 1 -j $z_dest
221 }
222
223 fw_redirect() {
224 local src
225 local src_ip
226 local src_port
227 local src_dport
228 local src_mac
229 local dest_ip
230 local dest_port dest_port2
231 local proto
232
233 config_get src $1 src
234 config_get src_ip $1 src_ip
235 config_get src_port $1 src_port
236 config_get src_dport $1 src_dport
237 config_get src_mac $1 src_mac
238 config_get dest_ip $1 dest_ip
239 config_get dest_port $1 dest_port
240 config_get proto $1 proto
241 [ -z "$src" -o -z "$dest_ip" ] && { \
242 echo "redirect needs src and dest_ip"; return ; }
243
244 src_port_first=${src_port%-*}
245 src_port_last=${src_port#*-}
246 [ "$src_port_first" -ne "$src_port_last" ] && { \
247 src_port="$src_port_first:$src_port_last"; }
248
249 src_dport_first=${src_dport%-*}
250 src_dport_last=${src_dport#*-}
251 [ "$src_dport_first" -ne "$src_dport_last" ] && { \
252 src_dport="$src_dport_first:$src_dport_last"; }
253
254 dest_port2=$dest_port
255 dest_port_first=${dest_port2%-*}
256 dest_port_last=${dest_port2#*-}
257 [ "$dest_port_first" -ne "$dest_port_last" ] && { \
258 dest_port2="$dest_port_first:$dest_port_last"; }
259
260 add_rule() {
261 $IPTABLES -A zone_${src}_prerouting -t nat \
262 ${proto:+-p $proto} \
263 ${src_ip:+-s $src_ip} \
264 ${src_port:+--sport $src_port} \
265 ${src_dport:+--dport $src_dport} \
266 ${src_mac:+-m mac --mac-source $src_mac} \
267 -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
268
269 $IPTABLES -I zone_${src}_forward 1 \
270 ${proto:+-p $proto} \
271 -d $dest_ip \
272 ${src_ip:+-s $src_ip} \
273 ${src_port:+--sport $src_port} \
274 ${dest_port2:+--dport $dest_port2} \
275 ${src_mac:+-m mac --mac-source $src_mac} \
276 -j ACCEPT
277 }
278 [ "$proto" == "tcpudp" -o -z "$proto" ] && {
279 proto=tcp
280 add_rule
281 proto=udp
282 add_rule
283 return
284 }
285 add_rule
286 }
287
288 fw_include() {
289 local path
290 config_get path $1 path
291 [ -e $path ] && . $path
292 }
293
294 fw_addif() {
295 local up
296 local ifname
297 config_get up $1 up
298 config_get ifname $1 ifname
299 [ -n "$up" ] || return 0
300 (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/20-firewall)
301 }
302
303 fw_custom_chains() {
304 $IPTABLES -N input_rule
305 $IPTABLES -N output_rule
306 $IPTABLES -N forwarding_rule
307 $IPTABLES -N prerouting_rule -t nat
308 $IPTABLES -N postrouting_rule -t nat
309 $IPTABLES -N input_wan
310 $IPTABLES -N forwarding_wan
311 $IPTABLES -N prerouting_wan -t nat
312
313 $IPTABLES -A INPUT -j input_rule
314 $IPTABLES -A OUTPUT -j output_rule
315 $IPTABLES -A FORWARD -j forwarding_rule
316 $IPTABLES -A PREROUTING -t nat -j prerouting_rule
317 $IPTABLES -A POSTROUTING -t nat -j postrouting_rule
318 $IPTABLES -A zone_wan -j input_wan
319 $IPTABLES -A zone_wan_forward -j forwarding_wan
320 $IPTABLES -A zone_wan_prerouting -t nat -j prerouting_wan
321 }
322
323 fw_init() {
324 echo "Loading defaults"
325 config_foreach fw_defaults defaults
326 echo "Loading zones"
327 config_foreach fw_zone zone
328 echo "Loading rules"
329 config_foreach fw_rule rule
330 echo "Loading forwarding"
331 config_foreach fw_forwarding forwarding
332 echo "Loading redirects"
333 config_foreach fw_redirect redirect
334 echo "Adding custom chains"
335 fw_custom_chains
336 echo "Loading includes"
337 config_foreach fw_include include
338 uci_set_state firewall core loaded 1
339 unset CONFIG_APPEND
340 config_load network
341 config_foreach fw_addif interface
342 }
343
344 fw_stop() {
345 $IPTABLES -F
346 $IPTABLES -t nat -F
347 $IPTABLES -t mangle -F
348 $IPTABLES -X -t nat
349 $IPTABLES -X
350 $IPTABLES -P INPUT ACCEPT
351 $IPTABLES -P OUTPUT ACCEPT
352 $IPTABLES -P FORWARD ACCEPT
353 uci_revert_state firewall core
354 }