rc.firewall revision 66830
133965Sjdp# Copyright (c) 1996  Poul-Henning Kamp
233965Sjdp# All rights reserved.
333965Sjdp#
433965Sjdp# Redistribution and use in source and binary forms, with or without
533965Sjdp# modification, are permitted provided that the following conditions
633965Sjdp# are met:
733965Sjdp# 1. Redistributions of source code must retain the above copyright
833965Sjdp#    notice, this list of conditions and the following disclaimer.
933965Sjdp# 2. Redistributions in binary form must reproduce the above copyright
1033965Sjdp#    notice, this list of conditions and the following disclaimer in the
1133965Sjdp#    documentation and/or other materials provided with the distribution.
1233965Sjdp#
1333965Sjdp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1433965Sjdp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1533965Sjdp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1633965Sjdp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1733965Sjdp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1833965Sjdp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1933965Sjdp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2033965Sjdp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2133965Sjdp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2233965Sjdp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2333965Sjdp# SUCH DAMAGE.
2433965Sjdp#
2533965Sjdp# $FreeBSD: head/etc/rc.firewall 66830 2000-10-08 19:20:36Z obrien $
2633965Sjdp#
2733965Sjdp
2833965Sjdp#
2933965Sjdp# Setup system for firewall service.
3033965Sjdp#
3133965Sjdp
3233965Sjdp# Suck in the configuration variables.
3333965Sjdpif [ -r /etc/defaults/rc.conf ]; then
3433965Sjdp	. /etc/defaults/rc.conf
3533965Sjdp	source_rc_confs
3633965Sjdpelif [ -r /etc/rc.conf ]; then
3733965Sjdp	. /etc/rc.conf
3833965Sjdpfi
3933965Sjdp
4033965Sjdp############
4133965Sjdp# Define the firewall type in /etc/rc.conf.  Valid values are:
4233965Sjdp#   open     - will allow anyone in
4333965Sjdp#   client   - will try to protect just this machine
4433965Sjdp#   simple   - will try to protect a whole network
4533965Sjdp#   closed   - totally disables IP services except via lo0 interface
4633965Sjdp#   UNKNOWN  - disables the loading of firewall rules.
4733965Sjdp#   filename - will load the rules in the given filename (full path required)
4833965Sjdp#
4933965Sjdp# For ``client'' and ``simple'' the entries below should be customized
5033965Sjdp# appropriately.
5133965Sjdp
5233965Sjdp############
5333965Sjdp#
5433965Sjdp# If you don't know enough about packet filtering, we suggest that you
5533965Sjdp# take time to read this book:
5633965Sjdp#
5733965Sjdp#	Building Internet Firewalls
5833965Sjdp#	Brent Chapman and Elizabeth Zwicky
5933965Sjdp#
6033965Sjdp#	O'Reilly & Associates, Inc
6133965Sjdp#	ISBN 1-56592-124-0
6233965Sjdp#	http://www.ora.com/
6333965Sjdp#
6433965Sjdp# For a more advanced treatment of Internet Security read:
6533965Sjdp#
6633965Sjdp#	Firewalls & Internet Security
6733965Sjdp#	Repelling the wily hacker
6833965Sjdp#	William R. Cheswick, Steven M. Bellowin
6933965Sjdp#
7033965Sjdp#	Addison-Wesley
7133965Sjdp#	ISBN 0-201-6337-4
7233965Sjdp#	http://www.awl.com/
7333965Sjdp#
7433965Sjdp
7533965Sjdpif [ -n "${1}" ]; then
7633965Sjdp	firewall_type="${1}"
7733965Sjdpfi
7833965Sjdp
7933965Sjdp############
8033965Sjdp# Set quiet mode if requested
8133965Sjdp#
8233965Sjdpcase ${firewall_quiet} in
8333965Sjdp[Yy][Ee][Ss])
8433965Sjdp	fwcmd="/sbin/ipfw -q"
8533965Sjdp	;;
8633965Sjdp*)
8733965Sjdp	fwcmd="/sbin/ipfw"
8833965Sjdp	;;
8933965Sjdpesac
9033965Sjdp
9133965Sjdp############
9233965Sjdp# Flush out the list before we begin.
9333965Sjdp#
9433965Sjdp${fwcmd} -f flush
9533965Sjdp
9633965Sjdp############
9733965Sjdp# Network Address Translation.  All packets are passed to natd(8)
9833965Sjdp# before they encounter your remaining rules.  The firewall rules
9933965Sjdp# will then be run again on each packet after translation by natd
10033965Sjdp# starting at the rule number following the divert rule.
10133965Sjdp#
10233965Sjdp# For ``simple'' firewall type the divert rule should be put to a
10333965Sjdp# different place to not interfere with address-checking rules.
10433965Sjdp# 
10533965Sjdpcase ${firewall_type} in
10633965Sjdp[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
10733965Sjdp	case ${natd_enable} in
10833965Sjdp	[Yy][Ee][Ss])
10933965Sjdp		if [ -n "${natd_interface}" ]; then
11033965Sjdp			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
11133965Sjdp		fi
11233965Sjdp		;;
11333965Sjdp	esac
11433965Sjdpesac
11533965Sjdp
11633965Sjdp############
11733965Sjdp# If you just configured ipfw in the kernel as a tool to solve network
11833965Sjdp# problems or you just want to disallow some particular kinds of traffic
11933965Sjdp# then you will want to change the default policy to open.  You can also
12033965Sjdp# do this as your only action by setting the firewall_type to ``open''.
12133965Sjdp#
12233965Sjdp# ${fwcmd} add 65000 pass all from any to any
12333965Sjdp
12433965Sjdp############
12533965Sjdp# Only in rare cases do you want to change these rules
12633965Sjdp#
12733965Sjdp${fwcmd} add 100 pass all from any to any via lo0
12833965Sjdp${fwcmd} add 200 deny all from any to 127.0.0.0/8
12933965Sjdp# If you're using 'options BRIDGE', uncomment the following line to pass ARP
13033965Sjdp#${fwcmd} add 300 pass udp from 0.0.0.0 2054 to 0.0.0.0
13133965Sjdp
13233965Sjdp
13333965Sjdp# Prototype setups.
13433965Sjdp#
13533965Sjdpcase ${firewall_type} in
13633965Sjdp[Oo][Pp][Ee][Nn])
13733965Sjdp	${fwcmd} add 65000 pass all from any to any
13833965Sjdp	;;
13933965Sjdp
14033965Sjdp[Cc][Ll][Ii][Ee][Nn][Tt])
14133965Sjdp	############
14233965Sjdp	# This is a prototype setup that will protect your system somewhat
14333965Sjdp	# against people from outside your own network.
14433965Sjdp	############
14533965Sjdp
14633965Sjdp	# set these to your network and netmask and ip
14733965Sjdp	net="192.0.2.0"
14833965Sjdp	mask="255.255.255.0"
14933965Sjdp	ip="192.0.2.1"
15033965Sjdp
15133965Sjdp	# Allow any traffic to or from my own net.
15233965Sjdp	${fwcmd} add pass all from ${ip} to ${net}:${mask}
15333965Sjdp	${fwcmd} add pass all from ${net}:${mask} to ${ip}
15433965Sjdp
15533965Sjdp	# Allow TCP through if setup succeeded
15633965Sjdp	${fwcmd} add pass tcp from any to any established
15733965Sjdp
15833965Sjdp	# Allow IP fragments to pass through
159	${fwcmd} add pass all from any to any frag
160
161	# Allow setup of incoming email
162	${fwcmd} add pass tcp from any to ${ip} 25 setup
163
164	# Allow setup of outgoing TCP connections only
165	${fwcmd} add pass tcp from ${ip} to any setup
166
167	# Disallow setup of all other TCP connections
168	${fwcmd} add deny tcp from any to any setup
169
170	# Allow DNS queries out in the world
171	${fwcmd} add pass udp from any 53 to ${ip}
172	${fwcmd} add pass udp from ${ip} to any 53
173
174	# Allow NTP queries out in the world
175	${fwcmd} add pass udp from any 123 to ${ip}
176	${fwcmd} add pass udp from ${ip} to any 123
177
178	# Everything else is denied by default, unless the
179	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
180	# config file.
181	;;
182
183[Ss][Ii][Mm][Pp][Ll][Ee])
184	############
185	# This is a prototype setup for a simple firewall.  Configure this
186	# machine as a named server and ntp server, and point all the machines
187	# on the inside at this machine for those services.
188	############
189
190	# set these to your outside interface network and netmask and ip
191	oif="ed0"
192	onet="192.0.2.0"
193	omask="255.255.255.240"
194	oip="192.0.2.1"
195
196	# set these to your inside interface network and netmask and ip
197	iif="ed1"
198	inet="192.0.2.16"
199	imask="255.255.255.240"
200	iip="192.0.2.17"
201
202	# Stop spoofing
203	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
204	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
205
206	# Stop RFC1918 nets on the outside interface
207	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
208	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
209	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
210
211	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
212	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
213	# on the outside interface
214	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
215	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
216	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
217	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
218	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
219
220	# Network Address Translation.  This rule is placed here deliberately
221	# so that it does not interfere with the surrounding address-checking
222	# rules.  If for example one of your internal LAN machines had its IP
223	# address set to 192.0.2.1 then an incoming packet for it after being
224	# translated by natd(8) would match the `deny' rule above.  Similarly
225	# an outgoing packet originated from it before being translated would
226	# match the `deny' rule below.
227	case ${natd_enable} in
228	[Yy][Ee][Ss])
229		if [ -n "${natd_interface}" ]; then
230			${fwcmd} add divert natd all from any to any via ${natd_interface}
231		fi
232		;;
233	esac
234
235	# Stop RFC1918 nets on the outside interface
236	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
237	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
238	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
239
240	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
241	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
242	# on the outside interface
243	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
244	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
245	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
246	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
247	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
248
249	# Allow TCP through if setup succeeded
250	${fwcmd} add pass tcp from any to any established
251
252	# Allow IP fragments to pass through
253	${fwcmd} add pass all from any to any frag
254
255	# Allow setup of incoming email
256	${fwcmd} add pass tcp from any to ${oip} 25 setup
257
258	# Allow access to our DNS
259	${fwcmd} add pass tcp from any to ${oip} 53 setup
260	${fwcmd} add pass udp from any to ${oip} 53
261	${fwcmd} add pass udp from ${oip} 53 to any
262
263	# Allow access to our WWW
264	${fwcmd} add pass tcp from any to ${oip} 80 setup
265
266	# Reject&Log all setup of incoming connections from the outside
267	${fwcmd} add deny log tcp from any to any in via ${oif} setup
268
269	# Allow setup of any other TCP connection
270	${fwcmd} add pass tcp from any to any setup
271
272	# Allow DNS queries out in the world
273	${fwcmd} add pass udp from any 53 to ${oip}
274	${fwcmd} add pass udp from ${oip} to any 53
275
276	# Allow NTP queries out in the world
277	${fwcmd} add pass udp from any 123 to ${oip}
278	${fwcmd} add pass udp from ${oip} to any 123
279
280	# Everything else is denied by default, unless the
281	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
282	# config file.
283	;;
284
285[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
286	;;
287*)
288	if [ -r "${firewall_type}" ]; then
289		${fwcmd} ${firewall_flags} ${firewall_type}
290	fi
291	;;
292esac
293