rc.firewall revision 181761
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: head/etc/rc.firewall 181761 2008-08-15 19:14:25Z jhb $
27#
28
29#
30# Setup system for ipfw(4) firewall service.
31#
32
33# Suck in the configuration variables.
34if [ -z "${source_rc_confs_defined}" ]; then
35	if [ -r /etc/defaults/rc.conf ]; then
36		. /etc/defaults/rc.conf
37		source_rc_confs
38	elif [ -r /etc/rc.conf ]; then
39		. /etc/rc.conf
40	fi
41fi
42
43############
44# Define the firewall type in /etc/rc.conf.  Valid values are:
45#   open        - will allow anyone in
46#   client      - will try to protect just this machine
47#   simple      - will try to protect a whole network
48#   closed      - totally disables IP services except via lo0 interface
49#   workstation - will try to protect just this machine using statefull
50#		  firewalling. See below for rc.conf variables used
51#   UNKNOWN     - disables the loading of firewall rules.
52#   filename    - will load the rules in the given filename (full path required)
53#
54# For ``client'' and ``simple'' the entries below should be customized
55# appropriately.
56
57############
58#
59# If you don't know enough about packet filtering, we suggest that you
60# take time to read this book:
61#
62#	Building Internet Firewalls, 2nd Edition
63#	Brent Chapman and Elizabeth Zwicky
64#
65#	O'Reilly & Associates, Inc
66#	ISBN 1-56592-871-7
67#	http://www.ora.com/
68#	http://www.oreilly.com/catalog/fire2/
69#
70# For a more advanced treatment of Internet Security read:
71#
72#	Firewalls and Internet Security: Repelling the Wily Hacker, 2nd Edition
73#	William R. Cheswick, Steven M. Bellowin, Aviel D. Rubin
74#
75#	Addison-Wesley / Prentice Hall
76#	ISBN 0-201-63466-X
77#	http://www.pearsonhighered.com/
78#	http://www.pearsonhighered.com/educator/academic/product/0,3110,020163466X,00.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
111setup_loopback
112
113############
114# Network Address Translation.  All packets are passed to natd(8)
115# before they encounter your remaining rules.  The firewall rules
116# will then be run again on each packet after translation by natd
117# starting at the rule number following the divert rule.
118#
119# For ``simple'' firewall type the divert rule should be put to a
120# different place to not interfere with address-checking rules.
121#
122case ${firewall_type} in
123[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
124	case ${natd_enable} in
125	[Yy][Ee][Ss])
126		if [ -n "${natd_interface}" ]; then
127			${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
128		fi
129		;;
130	esac
131	case ${firewall_nat_enable} in
132	[Yy][Ee][Ss])
133		if [ -n "${firewall_nat_interface}" ]; then
134			if echo "${firewall_nat_interface}" | \
135				grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then
136				firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}"
137			else
138				firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}"
139			fi
140			${fwcmd} nat 123 config log ${firewall_nat_flags}
141			${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
142		fi
143		;;
144	esac
145esac
146
147############
148# If you just configured ipfw in the kernel as a tool to solve network
149# problems or you just want to disallow some particular kinds of traffic
150# then you will want to change the default policy to open.  You can also
151# do this as your only action by setting the firewall_type to ``open''.
152#
153# ${fwcmd} add 65000 pass all from any to any
154
155
156# Prototype setups.
157#
158case ${firewall_type} in
159[Oo][Pp][Ee][Nn])
160	${fwcmd} add 65000 pass all from any to any
161	;;
162
163[Cc][Ll][Ii][Ee][Nn][Tt])
164	############
165	# This is a prototype setup that will protect your system somewhat
166	# against people from outside your own network.
167	############
168
169	# set these to your network
170	net="192.0.2.0/24"
171
172	# Allow limited broadcast traffic from my own net.
173	${fwcmd} add pass all from ${net} to 255.255.255.255
174
175	# Allow any traffic to or from my own net.
176	${fwcmd} add pass all from me to ${net}
177	${fwcmd} add pass all from ${net} to me
178
179	# Allow TCP through if setup succeeded
180	${fwcmd} add pass tcp from any to any established
181
182	# Allow IP fragments to pass through
183	${fwcmd} add pass all from any to any frag
184
185	# Allow setup of incoming email
186	${fwcmd} add pass tcp from any to me 25 setup
187
188	# Allow setup of outgoing TCP connections only
189	${fwcmd} add pass tcp from me to any setup
190
191	# Disallow setup of all other TCP connections
192	${fwcmd} add deny tcp from any to any setup
193
194	# Allow DNS queries out in the world
195	${fwcmd} add pass udp from me to any 53 keep-state
196
197	# Allow NTP queries out in the world
198	${fwcmd} add pass udp from me to any 123 keep-state
199
200	# Everything else is denied by default, unless the
201	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
202	# config file.
203	;;
204
205[Ss][Ii][Mm][Pp][Ll][Ee])
206	############
207	# This is a prototype setup for a simple firewall.  Configure this
208	# machine as a DNS and NTP server, and point all the machines
209	# on the inside at this machine for those services.
210	############
211
212	# set these to your outside interface network
213	oif="ed0"
214	onet="192.0.2.0/28"
215
216	# set these to your inside interface network
217	iif="ed1"
218	inet="192.0.2.16/28"
219
220	# Stop spoofing
221	${fwcmd} add deny all from ${inet} to any in via ${oif}
222	${fwcmd} add deny all from ${onet} to any in via ${iif}
223
224	# Stop RFC1918 nets on the outside interface
225	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
226	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
227	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
228
229	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
230	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
231	# on the outside interface
232	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
233	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
234	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
235	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
236	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
237
238	# Network Address Translation.  This rule is placed here deliberately
239	# so that it does not interfere with the surrounding address-checking
240	# rules.  If for example one of your internal LAN machines had its IP
241	# address set to 192.0.2.1 then an incoming packet for it after being
242	# translated by natd(8) would match the `deny' rule above.  Similarly
243	# an outgoing packet originated from it before being translated would
244	# match the `deny' rule below.
245	case ${natd_enable} in
246	[Yy][Ee][Ss])
247		if [ -n "${natd_interface}" ]; then
248			${fwcmd} add divert natd all from any to any via ${natd_interface}
249		fi
250		;;
251	esac
252
253	# Stop RFC1918 nets on the outside interface
254	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
255	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
256	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
257
258	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
259	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
260	# on the outside interface
261	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
262	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
263	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
264	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
265	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
266
267	# Allow TCP through if setup succeeded
268	${fwcmd} add pass tcp from any to any established
269
270	# Allow IP fragments to pass through
271	${fwcmd} add pass all from any to any frag
272
273	# Allow setup of incoming email
274	${fwcmd} add pass tcp from any to me 25 setup
275
276	# Allow access to our DNS
277	${fwcmd} add pass tcp from any to me 53 setup
278	${fwcmd} add pass udp from any to me 53
279	${fwcmd} add pass udp from me 53 to any
280
281	# Allow access to our WWW
282	${fwcmd} add pass tcp from any to me 80 setup
283
284	# Reject&Log all setup of incoming connections from the outside
285	${fwcmd} add deny log tcp from any to any in via ${oif} setup
286
287	# Allow setup of any other TCP connection
288	${fwcmd} add pass tcp from any to any setup
289
290	# Allow DNS queries out in the world
291	${fwcmd} add pass udp from me to any 53 keep-state
292
293	# Allow NTP queries out in the world
294	${fwcmd} add pass udp from me to any 123 keep-state
295
296	# Everything else is denied by default, unless the
297	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
298	# config file.
299	;;
300
301[Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
302	# Configuration:
303	#  firewall_myservices:		List of TCP ports on which this host
304	#			 	 offers services.
305	#  firewall_allowservices:	List of IPs which has access to
306	#				 $firewall_myservices.
307	#  firewall_trusted:		List of IPs which has full access 
308	#				 to this host. Be very carefull 
309	#				 when setting this. This option can
310	#				 seriously degrade the level of 
311	#				 protection provided by the firewall.
312	#  firewall_logdeny:		Boolean (YES/NO) specifying if the
313	#				 default denied packets should be
314	#				 logged (in /var/log/security).
315	#  firewall_nologports:		List of TCP/UDP ports for which
316	#				 denied incomming packets are not
317	#				 logged.
318	
319	# Allow packets for which a state has been built.
320	${fwcmd} add check-state
321
322	# For services permitted below.
323	${fwcmd} add pass tcp  from me to any established
324
325	# Allow any connection out, adding state for each.
326	${fwcmd} add pass tcp  from me to any setup keep-state
327	${fwcmd} add pass udp  from me to any       keep-state
328	${fwcmd} add pass icmp from me to any       keep-state
329
330	# Allow DHCP.
331	${fwcmd} add pass udp  from 0.0.0.0 68 to 255.255.255.255 67 out
332	${fwcmd} add pass udp  from any 67     to me 68 in
333	${fwcmd} add pass udp  from any 67     to 255.255.255.255 68 in
334	# Some servers will ping the IP while trying to decide if it's 
335	# still in use.
336	${fwcmd} add pass icmp from any to any icmptype 8
337
338	# Allow "mandatory" ICMP in.
339	${fwcmd} add pass icmp from any to any icmptype 3,4,11
340	
341	# Add permits for this workstations published services below
342	# Only IPs and nets in firewall_allowservices is allowed in.
343	# If you really wish to let anyone use services on your 
344	# workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
345	#
346	# Note: We don't use keep-state as that would allow DoS of
347	#       our statetable. 
348	#       You can add 'keep-state' to the lines for slightly
349	#       better performance if you fell that DoS of your
350	#       workstation won't be a problem.
351	#
352	for i in ${firewall_allowservices} ; do
353	  for j in ${firewall_myservices} ; do
354	    ${fwcmd} add pass tcp from $i to me $j
355	  done
356	done
357
358	# Allow all connections from trusted IPs.
359	# Playing with the content of firewall_trusted could seriously
360	# degrade the level of protection provided by the firewall.
361	for i in ${firewall_trusted} ; do
362	  ${fwcmd} add pass ip from $i to me
363	done
364	
365	${fwcmd} add 65000 count ip from any to any
366
367	# Drop packets to ports where we don't want logging
368	for i in ${firewall_nologports} ; do
369	  ${fwcmd} add deny { tcp or udp } from any to any $i in
370	done
371
372	# Broadcasts and muticasts
373	${fwcmd} add deny ip  from any to 255.255.255.255
374	${fwcmd} add deny ip  from any to 224.0.0.0/24 in	# XXX
375
376	# Noise from routers
377	${fwcmd} add deny udp from any to any 520 in
378
379	# Noise from webbrowsing.
380	# The statefull filter is a bit agressive, and will cause some
381	#  connection teardowns to be logged.
382	${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
383
384	# Deny and (if wanted) log the rest unconditionally.
385	log=""
386	if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
387	  log="log logamount 500"	# The default of 100 is too low.
388	  sysctl net.inet.ip.fw.verbose=1 >/dev/null
389	fi
390	${fwcmd} add deny $log ip from any to any
391	;;
392
393[Cc][Ll][Oo][Ss][Ee][Dd])
394	${fwcmd} add 65000 deny ip from any to any
395	;;
396[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
397	;;
398*)
399	if [ -r "${firewall_type}" ]; then
400		${fwcmd} ${firewall_flags} ${firewall_type}
401	fi
402	;;
403esac
404