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