jem.m4: Remove SMB for untrusted hosts.
[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.
31for t in mangle filter; do
32 for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do
0291d6d5
MW
33 run ip46tables -t $t -P $i DROP 2>/dev/null || :
34 run ip46tables -t $t -F $i 2>/dev/null || :
bfdc045d 35 done
0291d6d5
MW
36 run ip46tables -t $t -F
37 run ip46tables -t $t -X
bfdc045d
MW
38done
39
40m4_divert(32)m4_dnl
41###--------------------------------------------------------------------------
42### Set safe IP options.
43
44## Set forwarding options. Apparently setting ip_forward clobbers other
45## settings, so put this first.
46setopt ip_forward $forward
47setdevopt forwarding $forward
48
49## Set dynamic port allocation.
50setopt ip_local_port_range $open_port_min $open_port_max
51
52## Deploy SYN-cookies if necessary.
53setopt tcp_syncookies 1
54
c8d422ec
MW
55## Allow broadcast and multicast ping, because it's a useful diagnostic tool.
56setopt icmp_echo_ignore_broadcasts 0
57
429f4314
MW
58## Turn off iptables filtering for bridges. We'll use ebtables if we need
59## to; but right now the model is that we do filtering at the borders, and
60## are tolerant of things which are local.
6d47692a
MW
61if [ -x /sbin/brctl ]; then
62 modprobe bridge || :
63 if [ -d /proc/sys/net/bridge ]; then
64 for filter in arptables iptables ip6tables; do
65 run sysctl -q net.bridge.bridge-nf-call-$filter=0
66 done
67 fi
68fi
429f4314 69
bfdc045d 70## Turn on the reverse-path filter, and log weird things.
a0ade6f1
MW
71setdevopt rp_filter $rp_filter
72setdevopt log_martians $log_martians
bfdc045d
MW
73
74## Turn off things which can mess with our routing decisions.
75setdevopt accept_source_route 0
76setdevopt accept_redirects 0
77
78## If we're maent to stop the firewall, then now is the time to do it.
79$exit_after_clearing
80
81m4_divert(34)m4_dnl
82###--------------------------------------------------------------------------
83### Establish error chains.
84
0291d6d5
MW
85errorchain forbidden REJECT
86## Generic `not allowed' chain.
bfdc045d 87
0291d6d5
MW
88errorchain tcp-fragment REJECT
89## Chain for logging fragmented TCP segements.
bfdc045d
MW
90
91errorchain bad-tcp REJECT -p tcp --reject-with tcp-reset
92## Bad TCP segments (e.g., for unknown connections). Sends a TCP reset.
93
94errorchain mangle:bad-source-address DROP
0291d6d5 95errorchain bad-source-address DROP
bfdc045d
MW
96## Packet arrived on wrong interface for its source address. Drops the
97## packet, since there's nowhere sensible to send an error.
98
0291d6d5
MW
99errorchain bad-destination-address REJECT
100## Packet arrived on non-loopback interface with loopback destination.
f381cc0a 101
bfdc045d
MW
102errorchain interesting ACCEPT
103## Not an error, just log interesting packets.
104
105m4_divert(36)m4_dnl
106###--------------------------------------------------------------------------
f543dab7 107### Standard filtering.
bfdc045d 108
f381cc0a 109## Don't clobber local traffic
0291d6d5 110run ip46tables -A INPUT -i lo -j ACCEPT
bfdc045d 111
f381cc0a
MW
112## We really shouldn't see packets destined for localhost on any interface
113## other than the loopback.
114run iptables -A INPUT -g bad-destination-address \
115 -d 127.0.0.0/8
0291d6d5
MW
116run ip6tables -A INPUT -g bad-destination-address \
117 -d ::1
118
119## We shouldn't be asked to forward things with link-local addresses.
120run iptables -A FORWARD -g bad-source-address \
121 -s 169.254.0.0/16
122run iptables -A FORWARD -g bad-destination-address \
123 -d 169.254.0.0/16
124run ip6tables -A FORWARD -g bad-source-address \
125 -s fe80::/10
126run ip6tables -A FORWARD -g bad-destination-address \
127 -d fe80::/10
f381cc0a 128
429f4314
MW
129## Also, don't forward link-local broadcast or multicast.
130run iptables -A FORWARD -g bad-destination-address \
131 -d 255.255.255.255
132run iptables -A FORWARD -g bad-destination-address \
133 -m addrtype --dst-type BROADCAST
134run iptables -A FORWARD -g bad-destination-address \
135 -d 224.0.0.0/24
136for x in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
137 run ip6tables -A FORWARD -g bad-destination-address \
138 -d fe${x}2::/16
139done
140
f543dab7
MW
141## Add a hook for fail2ban.
142clearchain fail2ban
143run ip46tables -A INPUT -j fail2ban
144
bfdc045d
MW
145m4_divert(90)m4_dnl
146###--------------------------------------------------------------------------
147### Finishing touches.
148
149m4_divert(94)m4_dnl
150## Locally generated packets are all OK.
c1c877c2 151run ip46tables -P OUTPUT ACCEPT
bfdc045d
MW
152
153## Other incoming things are forbidden.
154for chain in INPUT FORWARD; do
c1c877c2 155 run ip46tables -A $chain -g forbidden
bfdc045d
MW
156done
157
158m4_divert(-1)
159###----- That's all, folks --------------------------------------------------