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