Merge branch 'master' into emergency
[firewall] / bookends.m4
CommitLineData
775bd287 1### -*-sh-*-
bfdc045d
MW
2###
3### Initialization and finishing touches for firewall scripts
4###
5### (c) 2008 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This program is free software; you can redistribute it and/or modify
11### it under the terms of the GNU General Public License as published by
12### the Free Software Foundation; either version 2 of the License, or
13### (at your option) any later version.
14###
15### This program is distributed in the hope that it will be useful,
16### but WITHOUT ANY WARRANTY; without even the implied warranty of
17### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18### GNU General Public License for more details.
19###
20### You should have received a copy of the GNU General Public License
21### along with this program; if not, write to the Free Software Foundation,
22### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24m4_divert(30)m4_dnl
25###--------------------------------------------------------------------------
26### Clear existing firewall rules.
27
28## The main chains: set policy to drop, and then clear the rules. For a
29## while, incoming packets will be silently dropped, but we should have got
30## everything going before anyone actually hits a timeout.
42dae9ae
MW
31##
32## We don't control some of the chains, so we should preserve them. This
33## introduces a whole bunch of problems.
34
35## Chains we're meant to preserve
36preserve_chains="filter:fail2ban filter:fail2ban-* $preserve_chains"
37
38## Take the various IP versions in turn.
39unref=nil
40for ip in ip ip6; do
41 for table in $(cat /proc/net/${ip}_tables_names); do
42
43 ## Step 1: clear out the builtin chains.
44 ${ip}tables -nL -t $table |
45 sed -n '/^Chain \([^ ]\+\) (policy .*$/ s//\1/p ' |
46 while read chain; do
47 case $table in
48 nat) policy=ACCEPT ;;
49 *) policy=DROP ;;
50 esac
51 run ${ip}tables -t $table -P $chain $policy
52 run ${ip}tables -t $table -F $chain
53 done
54
55 ## Step 2: clear out user chains. Unfortunately, we can only clear
56 ## chains which have no references to them, so work through picking off
57 ## unreferenced chains which aren't meant to be preserved until there are
58 ## none left.
59 while :; do
60 progress=nil
61 ${ip}tables -nL -t $table |
62 sed -n '/^Chain \([^ ]\+\) (0 references)$/ s//\1/p ' \
63 >/var/run/firewall-chains.tmp
64 while read chain; do
65 match=nil
66 for pat in $preserve_chains; do
67 case "$table:$chain" in $pat) match=t ;; esac
68 done
69 case $match in
70 nil)
71 run ${ip}tables -t $table -F $chain
72 run ${ip}tables -t $table -X $chain
73 progress=t
74 ;;
75 esac
76 done </var/run/firewall-chains.tmp
77 case $progress in nil) break ;; esac
78 done
79
80 ## Step 3: report on uncleared user chains. This means that there's a
81 ## serious problem.
82 ${ip}tables -nL -t $table |
83 sed -n '/^Chain \([^ ]\+\) (\([1-9][0-9]*\) references)$/ s//\1 \2/p ' \
84 >/var/run/firewall-chains.tmp
85 while read chain refs; do
86 match=nil
87 for pat in $preserve_chains; do
88 case "$table:$chain" in $pat) match=t ;; esac
89 done
90 case $match in
91 nil)
92 echo >&2 "$0: can't clear referenced $ip chain \`$table:$chain'"
93 unref=t
94 ;;
95 esac
96 done </var/run/firewall-chains.tmp
bfdc045d 97 done
bfdc045d 98done
42dae9ae
MW
99rm -f /var/run/firewall-chains.tmp
100case $unref in t) exit 1 ;; esac
bfdc045d
MW
101
102m4_divert(32)m4_dnl
103###--------------------------------------------------------------------------
104### Set safe IP options.
105
106## Set forwarding options. Apparently setting ip_forward clobbers other
107## settings, so put this first.
78af294c 108case $host_type_<::>FWHOST in
dc005150
MW
109 router) forward=1 host=0 ;;
110 server) forward=0 host=0 ;;
111 client) forward=0 host=1 ;;
78af294c 112esac
bfdc045d
MW
113setopt ip_forward $forward
114setdevopt forwarding $forward
dc005150
MW
115for i in \
116 accept_ra accept_ra_defrtr accept_ra_pinfo accept_ra_info_max_plen
117do
118 setdevopt $i $host
119done
f0033e07
MW
120case $forward in
121 0) inchains="INPUT" ;;
122 1) inchains="INPUT FORWARD" ;;
123esac
bfdc045d
MW
124
125## Set dynamic port allocation.
126setopt ip_local_port_range $open_port_min $open_port_max
127
128## Deploy SYN-cookies if necessary.
129setopt tcp_syncookies 1
130
c8d422ec
MW
131## Allow broadcast and multicast ping, because it's a useful diagnostic tool.
132setopt icmp_echo_ignore_broadcasts 0
133
429f4314
MW
134## Turn off iptables filtering for bridges. We'll use ebtables if we need
135## to; but right now the model is that we do filtering at the borders, and
136## are tolerant of things which are local.
6d47692a
MW
137if [ -x /sbin/brctl ]; then
138 modprobe bridge || :
139 if [ -d /proc/sys/net/bridge ]; then
140 for filter in arptables iptables ip6tables; do
141 run sysctl -q net.bridge.bridge-nf-call-$filter=0
142 done
143 fi
144fi
429f4314 145
78af294c
MW
146## Turn off the reverse-path filter. It's basically useless: the filter does
147## nothing at all for single-homed hosts; and multi-homed hosts tend to have
148## routing aysmmetries if there's any kind of cycle.
149setdevopt rp_filter 0
150setdevopt log_martians 0
bfdc045d
MW
151
152## Turn off things which can mess with our routing decisions.
153setdevopt accept_source_route 0
154setdevopt accept_redirects 0
155
156## If we're maent to stop the firewall, then now is the time to do it.
157$exit_after_clearing
158
159m4_divert(34)m4_dnl
160###--------------------------------------------------------------------------
161### Establish error chains.
162
0291d6d5
MW
163errorchain forbidden REJECT
164## Generic `not allowed' chain.
bfdc045d 165
0291d6d5
MW
166errorchain tcp-fragment REJECT
167## Chain for logging fragmented TCP segements.
bfdc045d
MW
168
169errorchain bad-tcp REJECT -p tcp --reject-with tcp-reset
170## Bad TCP segments (e.g., for unknown connections). Sends a TCP reset.
171
172errorchain mangle:bad-source-address DROP
0291d6d5 173errorchain bad-source-address DROP
bfdc045d
MW
174## Packet arrived on wrong interface for its source address. Drops the
175## packet, since there's nowhere sensible to send an error.
176
0291d6d5
MW
177errorchain bad-destination-address REJECT
178## Packet arrived on non-loopback interface with loopback destination.
f381cc0a 179
bfdc045d
MW
180errorchain interesting ACCEPT
181## Not an error, just log interesting packets.
182
a4d8cae3 183m4_divert(50)m4_dnl
bfdc045d 184###--------------------------------------------------------------------------
f543dab7 185### Standard filtering.
bfdc045d 186
f381cc0a 187## Don't clobber local traffic
0291d6d5 188run ip46tables -A INPUT -i lo -j ACCEPT
bfdc045d 189
f381cc0a
MW
190## We really shouldn't see packets destined for localhost on any interface
191## other than the loopback.
192run iptables -A INPUT -g bad-destination-address \
193 -d 127.0.0.0/8
0291d6d5
MW
194run ip6tables -A INPUT -g bad-destination-address \
195 -d ::1
196
197## We shouldn't be asked to forward things with link-local addresses.
78af294c
MW
198case $forward in
199 1)
200 run iptables -A FORWARD -g bad-source-address \
201 -s 169.254.0.0/16
202 run iptables -A FORWARD -g bad-destination-address \
203 -d 169.254.0.0/16
204 run ip6tables -A FORWARD -g bad-source-address \
205 -s fe80::/10
206 run ip6tables -A FORWARD -g bad-destination-address \
207 -d fe80::/10
208 ;;
209esac
f381cc0a 210
429f4314 211## Also, don't forward link-local broadcast or multicast.
78af294c
MW
212case $forward in
213 1)
214 run iptables -A FORWARD -g bad-destination-address \
215 -d 255.255.255.255
216 run iptables -A FORWARD -g bad-destination-address \
217 -m addrtype --dst-type BROADCAST
218 run iptables -A FORWARD -g bad-destination-address \
219 -d 224.0.0.0/24
e6d64b67 220 clearchain check-fwd-multi
78af294c 221 for x in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
e6d64b67
MW
222 run ip6tables -A check-fwd-multi -g bad-destination-address \
223 -d ff${x}2::/16
78af294c 224 done
e6d64b67 225 ip6tables -A FORWARD -j check-fwd-multi -d ff00::/8
78af294c
MW
226 ;;
227esac
429f4314 228
f543dab7
MW
229## Add a hook for fail2ban.
230clearchain fail2ban
231run ip46tables -A INPUT -j fail2ban
232
bfdc045d
MW
233m4_divert(90)m4_dnl
234###--------------------------------------------------------------------------
235### Finishing touches.
236
237m4_divert(94)m4_dnl
238## Locally generated packets are all OK.
c1c877c2 239run ip46tables -P OUTPUT ACCEPT
bfdc045d
MW
240
241## Other incoming things are forbidden.
242for chain in INPUT FORWARD; do
c1c877c2 243 run ip46tables -A $chain -g forbidden
bfdc045d
MW
244done
245
86c975b5
MW
246## Allow stuff through unknown tables.
247for ip in ip ip6; do
248 for table in $(cat /proc/net/${ip}_tables_names); do
249 case $table in mangle | filter) continue ;; esac
250 ${ip}tables -nL -t $table |
251 sed -n '/^Chain \([^ ]\+\) (policy .*$/ s//\1/p ' |
252 while read chain; do
253 run ${ip}tables -t $table -P $chain ACCEPT
254 done
255 done
256done
257
d8852323
MW
258## Dump the resulting configuration.
259if [ "$FW_DEBUG" ]; then
260 for ip in ip ip6; do
261 for table in mangle filter; do
262 echo "----- $ip $table -----"
263 echo
264 ${ip}tables -t $table -nvL
265 echo
266 done
267 done
268fi
269
bfdc045d
MW
270m4_divert(-1)
271###----- That's all, folks --------------------------------------------------