chiark / gitweb /
local.mk: Introduce new target for testing.
[firewall] / bookends.m4
CommitLineData
bfdc045d
MW
1### -*-m4-*-
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
33 run iptables -t $t -P $i DROP 2>/dev/null || :
34 run iptables -t $t -F $i 2>/dev/null || :
35 done
36 run iptables -t $t -F
37 run iptables -t $t -X
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
55## Turn on the reverse-path filter, and log weird things.
56setdevopt rp_filter 1
57setdevopt log_martians 1
58
59## Turn off things which can mess with our routing decisions.
60setdevopt accept_source_route 0
61setdevopt accept_redirects 0
62
63## If we're maent to stop the firewall, then now is the time to do it.
64$exit_after_clearing
65
66m4_divert(34)m4_dnl
67###--------------------------------------------------------------------------
68### Establish error chains.
69
70errorchain forbidden REJECT --reject-with icmp-host-prohibited
71## Generic `not allowed' chain. Rejects with ICMP host-prohibited.
72
73errorchain tcp-fragment REJECT --reject-with icmp-host-prohibited
74## Chain for logging fragmented TCP segements. Rejects with ICMP
75## host-prohibited.
76
77errorchain bad-tcp REJECT -p tcp --reject-with tcp-reset
78## Bad TCP segments (e.g., for unknown connections). Sends a TCP reset.
79
80errorchain mangle: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.
83
f381cc0a
MW
84errorchain bad-destination-address REJECT --reject-with icmp-host-prohibited
85## Packet arrived on non-loopback interface with loopback destination. Sends
86## a rude note back.
87
bfdc045d
MW
88errorchain interesting ACCEPT
89## Not an error, just log interesting packets.
90
91m4_divert(36)m4_dnl
92###--------------------------------------------------------------------------
f381cc0a 93### Standard loopback stuff.
bfdc045d 94
f381cc0a 95## Don't clobber local traffic
bfdc045d
MW
96run iptables -A INPUT -i lo -j ACCEPT
97
f381cc0a
MW
98## We really shouldn't see packets destined for localhost on any interface
99## other than the loopback.
100run iptables -A INPUT -g bad-destination-address \
101 -d 127.0.0.0/8
102
bfdc045d
MW
103m4_divert(90)m4_dnl
104###--------------------------------------------------------------------------
105### Finishing touches.
106
107m4_divert(94)m4_dnl
108## Locally generated packets are all OK.
109run iptables -P OUTPUT ACCEPT
110
111## Other incoming things are forbidden.
112for chain in INPUT FORWARD; do
113 run iptables -A $chain -g forbidden
114done
115
116m4_divert(-1)
117###----- That's all, folks --------------------------------------------------