rc.firewall revision 72772
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 72772 2001-02-20 19:54:31Z nsayer $
26#
27
28#
29# Setup system for firewall service.
30#
31
32# Suck in the configuration variables.
33if [ -r /etc/defaults/rc.conf ]; then
34	. /etc/defaults/rc.conf
35	source_rc_confs
36elif [ -r /etc/rc.conf ]; then
37	. /etc/rc.conf
38fi
39
40############
41# Define the firewall type in /etc/rc.conf.  Valid values are:
42#   open     - will allow anyone in
43#   client   - will try to protect just this machine
44#   simple   - will try to protect a whole network
45#   closed   - totally disables IP services except via lo0 interface
46#   UNKNOWN  - disables the loading of firewall rules.
47#   filename - will load the rules in the given filename (full path required)
48#
49# For ``client'' and ``simple'' the entries below should be customized
50# appropriately.
51
52############
53#
54# If you don't know enough about packet filtering, we suggest that you
55# take time to read this book:
56#
57#	Building Internet Firewalls
58#	Brent Chapman and Elizabeth Zwicky
59#
60#	O'Reilly & Associates, Inc
61#	ISBN 1-56592-124-0
62#	http://www.ora.com/
63#
64# For a more advanced treatment of Internet Security read:
65#
66#	Firewalls & Internet Security
67#	Repelling the wily hacker
68#	William R. Cheswick, Steven M. Bellowin
69#
70#	Addison-Wesley
71#	ISBN 0-201-6337-4
72#	http://www.awl.com/
73#
74
75if [ -n "${1}" ]; then
76	firewall_type="${1}"
77fi
78
79############
80# Set quiet mode if requested
81#
82case ${firewall_quiet} in
83[Yy][Ee][Ss])
84	fwcmd="/sbin/ipfw -q"
85	;;
86*)
87	fwcmd="/sbin/ipfw"
88	;;
89esac
90
91############
92# Flush out the list before we begin.
93#
94${fwcmd} -f flush
95
96############
97# Network Address Translation.  All packets are passed to natd(8)
98# before they encounter your remaining rules.  The firewall rules
99# will then be run again on each packet after translation by natd
100# starting at the rule number following the divert rule.
101#
102# For ``simple'' firewall type the divert rule should be put to a
103# different place to not interfere with address-checking rules.
104# 
105case ${firewall_type} in
106[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
107	case ${natd_enable} in
108	[Yy][Ee][Ss])
109		if [ -n "${natd_interface}" ]; then
110			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
111		fi
112		;;
113	esac
114esac
115
116############
117# If you just configured ipfw in the kernel as a tool to solve network
118# problems or you just want to disallow some particular kinds of traffic
119# then you will want to change the default policy to open.  You can also
120# do this as your only action by setting the firewall_type to ``open''.
121#
122# ${fwcmd} add 65000 pass all from any to any
123
124############
125# Only in rare cases do you want to change these rules
126#
127${fwcmd} add 100 pass all from any to any via lo0
128${fwcmd} add 200 deny all from any to 127.0.0.0/8
129# If you're using 'options BRIDGE', uncomment the following line to pass ARP
130#${fwcmd} add 300 pass udp from 0.0.0.0 2054 to 0.0.0.0
131
132
133# Prototype setups.
134#
135case ${firewall_type} in
136[Oo][Pp][Ee][Nn])
137	${fwcmd} add 65000 pass all from any to any
138	;;
139
140[Cc][Ll][Ii][Ee][Nn][Tt])
141	############
142	# This is a prototype setup that will protect your system somewhat
143	# against people from outside your own network.
144	############
145
146	# set these to your network and netmask and ip
147	net="192.0.2.0"
148	mask="255.255.255.0"
149	ip="192.0.2.1"
150
151	# Allow any traffic to or from my own net.
152	${fwcmd} add pass all from ${ip} to ${net}:${mask}
153	${fwcmd} add pass all from ${net}:${mask} to ${ip}
154
155	# Allow TCP through if setup succeeded
156	${fwcmd} add pass tcp from any to any established
157
158	# 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 ${ip} to any 53 keep-state
172
173	# Allow NTP queries out in the world
174	${fwcmd} add pass udp from ${ip} to any 123 keep-state
175
176	# Everything else is denied by default, unless the
177	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
178	# config file.
179	;;
180
181[Ss][Ii][Mm][Pp][Ll][Ee])
182	############
183	# This is a prototype setup for a simple firewall.  Configure this
184	# machine as a named server and ntp server, and point all the machines
185	# on the inside at this machine for those services.
186	############
187
188	# set these to your outside interface network and netmask and ip
189	oif="ed0"
190	onet="192.0.2.0"
191	omask="255.255.255.240"
192	oip="192.0.2.1"
193
194	# set these to your inside interface network and netmask and ip
195	iif="ed1"
196	inet="192.0.2.16"
197	imask="255.255.255.240"
198	iip="192.0.2.17"
199
200	# Stop spoofing
201	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
202	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
203
204	# Stop RFC1918 nets on the outside interface
205	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
206	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
207	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
208
209	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
210	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
211	# on the outside interface
212	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
213	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
214	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
215	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
216	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
217
218	# Network Address Translation.  This rule is placed here deliberately
219	# so that it does not interfere with the surrounding address-checking
220	# rules.  If for example one of your internal LAN machines had its IP
221	# address set to 192.0.2.1 then an incoming packet for it after being
222	# translated by natd(8) would match the `deny' rule above.  Similarly
223	# an outgoing packet originated from it before being translated would
224	# match the `deny' rule below.
225	case ${natd_enable} in
226	[Yy][Ee][Ss])
227		if [ -n "${natd_interface}" ]; then
228			${fwcmd} add divert natd all from any to any via ${natd_interface}
229		fi
230		;;
231	esac
232
233	# Stop RFC1918 nets on the outside interface
234	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
235	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
236	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
237
238	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
239	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
240	# on the outside interface
241	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
242	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
243	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
244	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
245	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
246
247	# Allow TCP through if setup succeeded
248	${fwcmd} add pass tcp from any to any established
249
250	# Allow IP fragments to pass through
251	${fwcmd} add pass all from any to any frag
252
253	# Allow setup of incoming email
254	${fwcmd} add pass tcp from any to ${oip} 25 setup
255
256	# Allow access to our DNS
257	${fwcmd} add pass tcp from any to ${oip} 53 setup
258	${fwcmd} add pass udp from any to ${oip} 53
259	${fwcmd} add pass udp from ${oip} 53 to any
260
261	# Allow access to our WWW
262	${fwcmd} add pass tcp from any to ${oip} 80 setup
263
264	# Reject&Log all setup of incoming connections from the outside
265	${fwcmd} add deny log tcp from any to any in via ${oif} setup
266
267	# Allow setup of any other TCP connection
268	${fwcmd} add pass tcp from any to any setup
269
270	# Allow DNS queries out in the world
271	${fwcmd} add pass udp from ${oip} to any 53 keep-state
272
273	# Allow NTP queries out in the world
274	${fwcmd} add pass udp from ${oip} to any 123 keep-state
275
276	# Everything else is denied by default, unless the
277	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
278	# config file.
279	;;
280
281[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
282	;;
283*)
284	if [ -r "${firewall_type}" ]; then
285		${fwcmd} ${firewall_flags} ${firewall_type}
286	fi
287	;;
288esac
289