xref: /dragonfly/etc/rc.firewall (revision 1de703da)
1#!/bin/sh
2# Copyright (c) 1996  Poul-Henning Kamp
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD: src/etc/rc.firewall,v 1.30.2.16 2003/02/10 05:45:06 trhodes Exp $
27# $DragonFly: src/etc/rc.firewall,v 1.2 2003/06/17 04:24:45 dillon Exp $
28#
29
30#
31# Setup system for firewall service.
32#
33
34# Suck in the configuration variables.
35if [ -z "${source_rc_confs_defined}" ]; then
36	if [ -r /etc/defaults/rc.conf ]; then
37		. /etc/defaults/rc.conf
38		source_rc_confs
39	elif [ -r /etc/rc.conf ]; then
40		. /etc/rc.conf
41	fi
42fi
43
44############
45# Define the firewall type in /etc/rc.conf.  Valid values are:
46#   open     - will allow anyone in
47#   client   - will try to protect just this machine
48#   simple   - will try to protect a whole network
49#   closed   - totally disables IP services except via lo0 interface
50#   UNKNOWN  - disables the loading of firewall rules.
51#   filename - will load the rules in the given filename (full path required)
52#
53# For ``client'' and ``simple'' the entries below should be customized
54# appropriately.
55
56############
57#
58# If you don't know enough about packet filtering, we suggest that you
59# take time to read this book:
60#
61#	Building Internet Firewalls, 2nd Edition
62#	Brent Chapman and Elizabeth Zwicky
63#
64#	O'Reilly & Associates, Inc
65#	ISBN 1-56592-871-7
66#	http://www.ora.com/
67#	http://www.oreilly.com/catalog/fire2/
68#
69# For a more advanced treatment of Internet Security read:
70#
71#	Firewalls & Internet Security
72#	Repelling the wily hacker
73#	William R. Cheswick, Steven M. Bellowin
74#
75#	Addison-Wesley
76#	ISBN 0-201-63357-4
77#	http://www.awl.com/
78#	http://www.awlonline.com/product/0%2C2627%2C0201633574%2C00.html
79#
80
81setup_loopback () {
82	############
83	# Only in rare cases do you want to change these rules
84	#
85	${fwcmd} add 100 pass all from any to any via lo0
86	${fwcmd} add 200 deny all from any to 127.0.0.0/8
87	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
88}
89
90if [ -n "${1}" ]; then
91	firewall_type="${1}"
92fi
93
94############
95# Set quiet mode if requested
96#
97case ${firewall_quiet} in
98[Yy][Ee][Ss])
99	fwcmd="/sbin/ipfw -q"
100	;;
101*)
102	fwcmd="/sbin/ipfw"
103	;;
104esac
105
106############
107# Flush out the list before we begin.
108#
109${fwcmd} -f flush
110
111############
112# Network Address Translation.  All packets are passed to natd(8)
113# before they encounter your remaining rules.  The firewall rules
114# will then be run again on each packet after translation by natd
115# starting at the rule number following the divert rule.
116#
117# For ``simple'' firewall type the divert rule should be put to a
118# different place to not interfere with address-checking rules.
119#
120case ${firewall_type} in
121[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
122	case ${natd_enable} in
123	[Yy][Ee][Ss])
124		if [ -n "${natd_interface}" ]; then
125			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
126		fi
127		;;
128	esac
129esac
130
131############
132# If you just configured ipfw in the kernel as a tool to solve network
133# problems or you just want to disallow some particular kinds of traffic
134# then you will want to change the default policy to open.  You can also
135# do this as your only action by setting the firewall_type to ``open''.
136#
137# ${fwcmd} add 65000 pass all from any to any
138
139
140# Prototype setups.
141#
142case ${firewall_type} in
143[Oo][Pp][Ee][Nn])
144	setup_loopback
145	${fwcmd} add 65000 pass all from any to any
146	;;
147
148[Cc][Ll][Ii][Ee][Nn][Tt])
149	############
150	# This is a prototype setup that will protect your system somewhat
151	# against people from outside your own network.
152	############
153
154	# set these to your network and netmask and ip
155	net="192.0.2.0"
156	mask="255.255.255.0"
157	ip="192.0.2.1"
158
159	setup_loopback
160
161	# Allow any traffic to or from my own net.
162	${fwcmd} add pass all from ${ip} to ${net}:${mask}
163	${fwcmd} add pass all from ${net}:${mask} to ${ip}
164
165	# Allow TCP through if setup succeeded
166	${fwcmd} add pass tcp from any to any established
167
168	# Allow IP fragments to pass through
169	${fwcmd} add pass all from any to any frag
170
171	# Allow setup of incoming email
172	${fwcmd} add pass tcp from any to ${ip} 25 setup
173
174	# Allow setup of outgoing TCP connections only
175	${fwcmd} add pass tcp from ${ip} to any setup
176
177	# Disallow setup of all other TCP connections
178	${fwcmd} add deny tcp from any to any setup
179
180	# Allow DNS queries out in the world
181	${fwcmd} add pass udp from ${ip} to any 53 keep-state
182
183	# Allow NTP queries out in the world
184	${fwcmd} add pass udp from ${ip} to any 123 keep-state
185
186	# Everything else is denied by default, unless the
187	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
188	# config file.
189	;;
190
191[Ss][Ii][Mm][Pp][Ll][Ee])
192	############
193	# This is a prototype setup for a simple firewall.  Configure this
194	# machine as a named server and ntp server, and point all the machines
195	# on the inside at this machine for those services.
196	############
197
198	# set these to your outside interface network and netmask and ip
199	oif="ed0"
200	onet="192.0.2.0"
201	omask="255.255.255.240"
202	oip="192.0.2.1"
203
204	# set these to your inside interface network and netmask and ip
205	iif="ed1"
206	inet="192.0.2.16"
207	imask="255.255.255.240"
208	iip="192.0.2.17"
209
210	setup_loopback
211
212	# Stop spoofing
213	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
214	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
215
216	# Stop RFC1918 nets on the outside interface
217	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
218	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
219	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
220
221	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
222	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
223	# on the outside interface
224	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
225	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
226	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
227	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
228	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
229
230	# Network Address Translation.  This rule is placed here deliberately
231	# so that it does not interfere with the surrounding address-checking
232	# rules.  If for example one of your internal LAN machines had its IP
233	# address set to 192.0.2.1 then an incoming packet for it after being
234	# translated by natd(8) would match the `deny' rule above.  Similarly
235	# an outgoing packet originated from it before being translated would
236	# match the `deny' rule below.
237	case ${natd_enable} in
238	[Yy][Ee][Ss])
239		if [ -n "${natd_interface}" ]; then
240			${fwcmd} add divert natd all from any to any via ${natd_interface}
241		fi
242		;;
243	esac
244
245	# Stop RFC1918 nets on the outside interface
246	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
247	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
248	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
249
250	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
251	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
252	# on the outside interface
253	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
254	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
255	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
256	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
257	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
258
259	# Allow TCP through if setup succeeded
260	${fwcmd} add pass tcp from any to any established
261
262	# Allow IP fragments to pass through
263	${fwcmd} add pass all from any to any frag
264
265	# Allow setup of incoming email
266	${fwcmd} add pass tcp from any to ${oip} 25 setup
267
268	# Allow access to our DNS
269	${fwcmd} add pass tcp from any to ${oip} 53 setup
270	${fwcmd} add pass udp from any to ${oip} 53
271	${fwcmd} add pass udp from ${oip} 53 to any
272
273	# Allow access to our WWW
274	${fwcmd} add pass tcp from any to ${oip} 80 setup
275
276	# Reject&Log all setup of incoming connections from the outside
277	${fwcmd} add deny log tcp from any to any in via ${oif} setup
278
279	# Allow setup of any other TCP connection
280	${fwcmd} add pass tcp from any to any setup
281
282	# Allow DNS queries out in the world
283	${fwcmd} add pass udp from ${oip} to any 53 keep-state
284
285	# Allow NTP queries out in the world
286	${fwcmd} add pass udp from ${oip} to any 123 keep-state
287
288	# Everything else is denied by default, unless the
289	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
290	# config file.
291	;;
292
293[Cc][Ll][Oo][Ss][Ee][Dd])
294	setup_loopback
295	;;
296[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
297	;;
298*)
299	if [ -r "${firewall_type}" ]; then
300		${fwcmd} ${firewall_flags} ${firewall_type}
301	fi
302	;;
303esac
304