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.
31 for t in mangle filter; do
32 for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do
33 run ip46tables -t $t -P $i DROP 2>/dev/null || :
34 run ip46tables -t $t -F $i 2>/dev/null || :
36 run ip46tables -t $t -F
37 run ip46tables -t $t -X
41 ###--------------------------------------------------------------------------
42 ### Set safe IP options.
44 ## Set forwarding options. Apparently setting ip_forward clobbers other
45 ## settings, so put this first.
46 setopt ip_forward $forward
47 setdevopt forwarding $forward
49 ## Set dynamic port allocation.
50 setopt ip_local_port_range $open_port_min $open_port_max
52 ## Deploy SYN-cookies if necessary.
53 setopt tcp_syncookies 1
55 ## Turn on the reverse-path filter, and log weird things.
57 setdevopt log_martians 1
59 ## Turn off things which can mess with our routing decisions.
60 setdevopt accept_source_route 0
61 setdevopt accept_redirects 0
63 ## If we're maent to stop the firewall, then now is the time to do it.
67 ###--------------------------------------------------------------------------
68 ### Establish error chains.
70 errorchain forbidden REJECT
71 ## Generic `not allowed' chain.
73 errorchain tcp-fragment REJECT
74 ## Chain for logging fragmented TCP segements.
76 errorchain bad-tcp REJECT -p tcp --reject-with tcp-reset
77 ## Bad TCP segments (e.g., for unknown connections). Sends a TCP reset.
79 errorchain mangle:bad-source-address DROP
80 errorchain bad-source-address DROP
81 ## Packet arrived on wrong interface for its source address. Drops the
82 ## packet, since there's nowhere sensible to send an error.
84 errorchain bad-destination-address REJECT
85 ## Packet arrived on non-loopback interface with loopback destination.
87 errorchain interesting ACCEPT
88 ## Not an error, just log interesting packets.
91 ###--------------------------------------------------------------------------
92 ### Standard loopback stuff.
94 ## Don't clobber local traffic
95 run ip46tables -A INPUT -i lo -j ACCEPT
97 ## We really shouldn't see packets destined for localhost on any interface
98 ## other than the loopback.
99 run iptables -A INPUT -g bad-destination-address \
101 run ip6tables -A INPUT -g bad-destination-address \
104 ## We shouldn't be asked to forward things with link-local addresses.
105 run iptables -A FORWARD -g bad-source-address \
107 run iptables -A FORWARD -g bad-destination-address \
109 run ip6tables -A FORWARD -g bad-source-address \
111 run ip6tables -A FORWARD -g bad-destination-address \
115 ###--------------------------------------------------------------------------
116 ### Finishing touches.
119 ## Locally generated packets are all OK.
120 run iptables -P OUTPUT ACCEPT
122 ## Other incoming things are forbidden.
123 for chain in INPUT FORWARD; do
124 run iptables -A $chain -g forbidden
128 ###----- That's all, folks --------------------------------------------------