3 ### Initialization and finishing touches for firewall scripts
5 ### (c) 2008 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
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.
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.
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.
25 ###--------------------------------------------------------------------------
26 ### Clear existing firewall rules.
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.
32 ## We don't control some of the chains, so we should preserve them. This
33 ## introduces a whole bunch of problems.
35 ## Chains we're meant to preserve
36 preserve_chains="filter:fail2ban filter:fail2ban-* $preserve_chains"
38 ## Take the various IP versions in turn.
41 if [ "$FW_NOACT" ]; then break; fi
43 for table in $(cat /proc/net/${ip}_tables_names); do
45 ## Step 1: clear out the builtin chains.
46 ${ip}tables -nL -t $table |
47 sed -n '/^Chain \([^ ]\+\) (policy .*$/ s//\1/p ' |
53 run ${ip}tables -t $table -P $chain $policy
54 run ${ip}tables -t $table -F $chain
57 ## Step 2: clear out user chains. Unfortunately, we can only clear
58 ## chains which have no references to them, so work through picking off
59 ## unreferenced chains which aren't meant to be preserved until there are
63 ${ip}tables -nL -t $table |
64 sed -n '/^Chain \([^ ]\+\) (0 references)$/ s//\1/p ' \
65 >/var/run/firewall-chains.tmp
68 for pat in $preserve_chains; do
69 case "$table:$chain" in $pat) match=t ;; esac
73 run ${ip}tables -t $table -F $chain
74 run ${ip}tables -t $table -X $chain
78 done </var/run/firewall-chains.tmp
79 case $progress in nil) break ;; esac
82 ## Step 3: report on uncleared user chains. This means that there's a
84 ${ip}tables -nL -t $table |
85 sed -n '/^Chain \([^ ]\+\) (\([1-9][0-9]*\) references)$/ s//\1 \2/p ' \
86 >/var/run/firewall-chains.tmp
87 while read chain refs; do
89 for pat in $preserve_chains; do
90 case "$table:$chain" in $pat) match=t ;; esac
94 echo >&2 "$0: can't clear referenced $ip chain \`$table:$chain'"
98 done </var/run/firewall-chains.tmp
101 rm -f /var/run/firewall-chains.tmp
102 case $unref in t) exit 1 ;; esac
105 ###--------------------------------------------------------------------------
106 ### Set safe IP options.
108 ## Set forwarding options. Apparently setting ip_forward clobbers other
109 ## settings, so put this first.
110 case $host_type_<::>FWHOST in
114 setopt ip_forward $forward
115 setdevopt forwarding $forward
117 0) inchains="INPUT" ;;
118 1) inchains="INPUT FORWARD" ;;
121 ## Set dynamic port allocation.
122 setopt ip_local_port_range $open_port_min $open_port_max
124 ## Deploy SYN-cookies if necessary.
125 setopt tcp_syncookies 1
127 ## Allow broadcast and multicast ping, because it's a useful diagnostic tool.
128 setopt icmp_echo_ignore_broadcasts 0
130 ## Turn off iptables filtering for bridges. We'll use ebtables if we need
131 ## to; but right now the model is that we do filtering at the borders, and
132 ## are tolerant of things which are local.
133 if [ -x /sbin/brctl ]; then
135 if [ -d /proc/sys/net/bridge ]; then
136 for filter in arptables iptables ip6tables; do
137 run sysctl -q net.bridge.bridge-nf-call-$filter=0
142 ## Turn off the reverse-path filter. It's basically useless: the filter does
143 ## nothing at all for single-homed hosts; and multi-homed hosts tend to have
144 ## routing aysmmetries if there's any kind of cycle.
145 setdevopt rp_filter 0
146 setdevopt log_martians 0
148 ## Turn off things which can mess with our routing decisions.
149 setdevopt accept_source_route 0
150 setdevopt accept_redirects 0
152 ## If we're maent to stop the firewall, then now is the time to do it.
156 ###--------------------------------------------------------------------------
157 ### Establish error chains.
159 errorchain forbidden REJECT
160 ## Generic `not allowed' chain.
162 errorchain tcp-fragment REJECT
163 ## Chain for logging fragmented TCP segements.
165 errorchain bad-tcp REJECT -p tcp --reject-with tcp-reset
166 ## Bad TCP segments (e.g., for unknown connections). Sends a TCP reset.
168 errorchain mangle:bad-source-address DROP
169 errorchain bad-source-address DROP
170 ## Packet arrived on wrong interface for its source address. Drops the
171 ## packet, since there's nowhere sensible to send an error.
173 errorchain bad-destination-address REJECT
174 ## Packet arrived on non-loopback interface with loopback destination.
176 errorchain interesting ACCEPT
177 ## Not an error, just log interesting packets.
180 ###--------------------------------------------------------------------------
181 ### Standard filtering.
183 ## Don't clobber local traffic
184 run ip46tables -A INPUT -i lo -j ACCEPT
186 ## We really shouldn't see packets destined for localhost on any interface
187 ## other than the loopback.
188 run iptables -A INPUT -g bad-destination-address \
190 run ip6tables -A INPUT -g bad-destination-address \
193 ## We shouldn't be asked to forward things with link-local addresses.
196 run iptables -A FORWARD -g bad-source-address \
198 run iptables -A FORWARD -g bad-destination-address \
200 run ip6tables -A FORWARD -g bad-source-address \
202 run ip6tables -A FORWARD -g bad-destination-address \
207 ## Also, don't forward link-local broadcast or multicast.
210 run iptables -A FORWARD -g bad-destination-address \
212 run iptables -A FORWARD -g bad-destination-address \
213 -m addrtype --dst-type BROADCAST
214 run iptables -A FORWARD -g bad-destination-address \
216 clearchain check-fwd-multi
217 for x in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
218 run ip6tables -A check-fwd-multi -g bad-destination-address \
221 run ip6tables -A FORWARD -j check-fwd-multi -d ff00::/8
225 ## Add a hook for fail2ban.
227 run ip46tables -A INPUT -j fail2ban
230 ###--------------------------------------------------------------------------
231 ### Finishing touches.
234 ## Locally generated packets are all OK.
235 run ip46tables -P OUTPUT ACCEPT
237 ## Other incoming things are forbidden.
238 for chain in INPUT FORWARD; do
239 run ip46tables -A $chain -g forbidden
242 ## Allow stuff through unknown tables.
244 for table in $(cat /proc/net/${ip}_tables_names); do
245 case $table in mangle | filter) continue ;; esac
246 ${ip}tables -nL -t $table |
247 sed -n '/^Chain \([^ ]\+\) (policy .*$/ s//\1/p ' |
249 run ${ip}tables -t $table -P $chain ACCEPT
254 ## Dump the resulting configuration.
255 if [ "$FW_DEBUG" ]; then
257 for table in mangle filter; do
258 echo "----- $ip $table -----"
260 ${ip}tables -t $table -nvL
267 ###----- That's all, folks --------------------------------------------------