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