rc.firewall revision 81618
1284990Scy# Copyright (c) 1996  Poul-Henning Kamp
2284990Scy# All rights reserved.
3284990Scy#
4284990Scy# Redistribution and use in source and binary forms, with or without
5284990Scy# modification, are permitted provided that the following conditions
6284990Scy# are met:
7284990Scy# 1. Redistributions of source code must retain the above copyright
8284990Scy#    notice, this list of conditions and the following disclaimer.
9284990Scy# 2. Redistributions in binary form must reproduce the above copyright
10284990Scy#    notice, this list of conditions and the following disclaimer in the
11284990Scy#    documentation and/or other materials provided with the distribution.
12284990Scy#
13284990Scy# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14284990Scy# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15284990Scy# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16284990Scy# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17284990Scy# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18284990Scy# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19284990Scy# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20284990Scy# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21284990Scy# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22284990Scy# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23284990Scy# SUCH DAMAGE.
24284990Scy#
25290000Sglebius# $FreeBSD: head/etc/rc.firewall 81618 2001-08-14 05:50:19Z dd $
26290000Sglebius#
27290000Sglebius
28290000Sglebius#
29294904Sdelphij# Setup system for firewall service.
30284990Scy#
31284990Scy
32284990Scy# Suck in the configuration variables.
33284990Scyif [ -z "${source_rc_confs_defined}" ]; then
34290000Sglebius	if [ -r /etc/defaults/rc.conf ]; then
35290000Sglebius		. /etc/defaults/rc.conf
36290000Sglebius		source_rc_confs
37290000Sglebius	elif [ -r /etc/rc.conf ]; then
38290000Sglebius		. /etc/rc.conf
39290000Sglebius	fi
40294904Sdelphijfi
41284990Scy
42284990Scy############
43284990Scy# Define the firewall type in /etc/rc.conf.  Valid values are:
44290000Sglebius#   open     - will allow anyone in
45290000Sglebius#   client   - will try to protect just this machine
46284990Scy#   simple   - will try to protect a whole network
47284990Scy#   closed   - totally disables IP services except via lo0 interface
48284990Scy#   UNKNOWN  - disables the loading of firewall rules.
49284990Scy#   filename - will load the rules in the given filename (full path required)
50284990Scy#
51290000Sglebius# For ``client'' and ``simple'' the entries below should be customized
52284990Scy# appropriately.
53284990Scy
54284990Scy############
55284990Scy#
56284990Scy# If you don't know enough about packet filtering, we suggest that you
57284990Scy# take time to read this book:
58284990Scy#
59294904Sdelphij#	Building Internet Firewalls, 2nd Edition
60294904Sdelphij#	Brent Chapman and Elizabeth Zwicky
61294904Sdelphij#
62294904Sdelphij#	O'Reilly & Associates, Inc
63294904Sdelphij#	ISBN 1-56592-871-7
64294904Sdelphij#	http://www.ora.com/
65294904Sdelphij#	http://www.oreilly.com/catalog/fire2/
66284990Scy#
67284990Scy# For a more advanced treatment of Internet Security read:
68284990Scy#
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
79if [ -n "${1}" ]; then
80	firewall_type="${1}"
81fi
82
83############
84# Set quiet mode if requested
85#
86case ${firewall_quiet} in
87[Yy][Ee][Ss])
88	fwcmd="/sbin/ipfw -q"
89	;;
90*)
91	fwcmd="/sbin/ipfw"
92	;;
93esac
94
95############
96# Flush out the list before we begin.
97#
98${fwcmd} -f flush
99
100############
101# Network Address Translation.  All packets are passed to natd(8)
102# before they encounter your remaining rules.  The firewall rules
103# will then be run again on each packet after translation by natd
104# starting at the rule number following the divert rule.
105#
106# For ``simple'' firewall type the divert rule should be put to a
107# different place to not interfere with address-checking rules.
108#
109case ${firewall_type} in
110[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
111	case ${natd_enable} in
112	[Yy][Ee][Ss])
113		if [ -n "${natd_interface}" ]; then
114			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
115		fi
116		;;
117	esac
118esac
119
120############
121# If you just configured ipfw in the kernel as a tool to solve network
122# problems or you just want to disallow some particular kinds of traffic
123# then you will want to change the default policy to open.  You can also
124# do this as your only action by setting the firewall_type to ``open''.
125#
126# ${fwcmd} add 65000 pass all from any to any
127
128############
129# Only in rare cases do you want to change these rules
130#
131${fwcmd} add 100 pass all from any to any via lo0
132${fwcmd} add 200 deny all from any to 127.0.0.0/8
133${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
134# If you're using 'options BRIDGE', uncomment the following line to pass ARP
135#${fwcmd} add 400 pass udp from 0.0.0.0 2054 to 0.0.0.0
136
137
138# Prototype setups.
139#
140case ${firewall_type} in
141[Oo][Pp][Ee][Nn])
142	${fwcmd} add 65000 pass all from any to any
143	;;
144
145[Cc][Ll][Ii][Ee][Nn][Tt])
146	############
147	# This is a prototype setup that will protect your system somewhat
148	# against people from outside your own network.
149	############
150
151	# set these to your network and netmask and ip
152	net="192.0.2.0"
153	mask="255.255.255.0"
154	ip="192.0.2.1"
155
156	# Allow any traffic to or from my own net.
157	${fwcmd} add pass all from ${ip} to ${net}:${mask}
158	${fwcmd} add pass all from ${net}:${mask} to ${ip}
159
160	# Allow TCP through if setup succeeded
161	${fwcmd} add pass tcp from any to any established
162
163	# Allow IP fragments to pass through
164	${fwcmd} add pass all from any to any frag
165
166	# Allow setup of incoming email
167	${fwcmd} add pass tcp from any to ${ip} 25 setup
168
169	# Allow setup of outgoing TCP connections only
170	${fwcmd} add pass tcp from ${ip} to any setup
171
172	# Disallow setup of all other TCP connections
173	${fwcmd} add deny tcp from any to any setup
174
175	# Allow DNS queries out in the world
176	${fwcmd} add pass udp from ${ip} to any 53 keep-state
177
178	# Allow NTP queries out in the world
179	${fwcmd} add pass udp from ${ip} to any 123 keep-state
180
181	# Everything else is denied by default, unless the
182	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
183	# config file.
184	;;
185
186[Ss][Ii][Mm][Pp][Ll][Ee])
187	############
188	# This is a prototype setup for a simple firewall.  Configure this
189	# machine as a named server and ntp server, and point all the machines
190	# on the inside at this machine for those services.
191	############
192
193	# set these to your outside interface network and netmask and ip
194	oif="ed0"
195	onet="192.0.2.0"
196	omask="255.255.255.240"
197	oip="192.0.2.1"
198
199	# set these to your inside interface network and netmask and ip
200	iif="ed1"
201	inet="192.0.2.16"
202	imask="255.255.255.240"
203	iip="192.0.2.17"
204
205	# Stop spoofing
206	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
207	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
208
209	# Stop RFC1918 nets on the outside interface
210	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
211	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
212	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
213
214	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
215	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
216	# on the outside interface
217	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
218	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
219	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
220	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
221	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
222
223	# Network Address Translation.  This rule is placed here deliberately
224	# so that it does not interfere with the surrounding address-checking
225	# rules.  If for example one of your internal LAN machines had its IP
226	# address set to 192.0.2.1 then an incoming packet for it after being
227	# translated by natd(8) would match the `deny' rule above.  Similarly
228	# an outgoing packet originated from it before being translated would
229	# match the `deny' rule below.
230	case ${natd_enable} in
231	[Yy][Ee][Ss])
232		if [ -n "${natd_interface}" ]; then
233			${fwcmd} add divert natd all from any to any via ${natd_interface}
234		fi
235		;;
236	esac
237
238	# Stop RFC1918 nets on the outside interface
239	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
240	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
241	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
242
243	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
244	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
245	# on the outside interface
246	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
247	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
248	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
249	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
250	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
251
252	# Allow TCP through if setup succeeded
253	${fwcmd} add pass tcp from any to any established
254
255	# Allow IP fragments to pass through
256	${fwcmd} add pass all from any to any frag
257
258	# Allow setup of incoming email
259	${fwcmd} add pass tcp from any to ${oip} 25 setup
260
261	# Allow access to our DNS
262	${fwcmd} add pass tcp from any to ${oip} 53 setup
263	${fwcmd} add pass udp from any to ${oip} 53
264	${fwcmd} add pass udp from ${oip} 53 to any
265
266	# Allow access to our WWW
267	${fwcmd} add pass tcp from any to ${oip} 80 setup
268
269	# Reject&Log all setup of incoming connections from the outside
270	${fwcmd} add deny log tcp from any to any in via ${oif} setup
271
272	# Allow setup of any other TCP connection
273	${fwcmd} add pass tcp from any to any setup
274
275	# Allow DNS queries out in the world
276	${fwcmd} add pass udp from ${oip} to any 53 keep-state
277
278	# Allow NTP queries out in the world
279	${fwcmd} add pass udp from ${oip} to any 123 keep-state
280
281	# Everything else is denied by default, unless the
282	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
283	# config file.
284	;;
285
286[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
287	;;
288*)
289	if [ -r "${firewall_type}" ]; then
290		${fwcmd} ${firewall_flags} ${firewall_type}
291	fi
292	;;
293esac
294