rc.firewall revision 181760
1110476Strhodes#!/bin/sh -
266830Sobrien# Copyright (c) 1996  Poul-Henning Kamp
366830Sobrien# All rights reserved.
466830Sobrien#
566830Sobrien# Redistribution and use in source and binary forms, with or without
666830Sobrien# modification, are permitted provided that the following conditions
766830Sobrien# are met:
866830Sobrien# 1. Redistributions of source code must retain the above copyright
966830Sobrien#    notice, this list of conditions and the following disclaimer.
1066830Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1166830Sobrien#    notice, this list of conditions and the following disclaimer in the
1266830Sobrien#    documentation and/or other materials provided with the distribution.
1366830Sobrien#
1466830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1566830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1666830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1766830Sobrien# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1866830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1966830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2066830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2166830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2266830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2366830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2466830Sobrien# SUCH DAMAGE.
2566830Sobrien#
2650472Speter# $FreeBSD: head/etc/rc.firewall 181760 2008-08-15 18:58:15Z jhb $
2766830Sobrien#
2815027Sphk
2966830Sobrien#
30168384Srwatson# Setup system for ipfw(4) firewall service.
3166830Sobrien#
3266830Sobrien
3343849Sjkh# Suck in the configuration variables.
3481618Sddif [ -z "${source_rc_confs_defined}" ]; then
3581618Sdd	if [ -r /etc/defaults/rc.conf ]; then
3681618Sdd		. /etc/defaults/rc.conf
3781618Sdd		source_rc_confs
3881618Sdd	elif [ -r /etc/rc.conf ]; then
3981618Sdd		. /etc/rc.conf
4081618Sdd	fi
4133203Sadamfi
4233203Sadam
4315027Sphk############
4429300Sdanny# Define the firewall type in /etc/rc.conf.  Valid values are:
45163749Sphk#   open        - will allow anyone in
46163749Sphk#   client      - will try to protect just this machine
47163749Sphk#   simple      - will try to protect a whole network
48163749Sphk#   closed      - totally disables IP services except via lo0 interface
49163749Sphk#   workstation - will try to protect just this machine using statefull
50163749Sphk#		  firewalling. See below for rc.conf variables used
51163749Sphk#   UNKNOWN     - disables the loading of firewall rules.
52163749Sphk#   filename    - will load the rules in the given filename (full path required)
5315027Sphk#
5451231Ssheldonh# For ``client'' and ``simple'' the entries below should be customized
5529300Sdanny# appropriately.
5615027Sphk
5715027Sphk############
5815027Sphk#
5915027Sphk# If you don't know enough about packet filtering, we suggest that you
6015027Sphk# take time to read this book:
6115027Sphk#
6273023Sdes#	Building Internet Firewalls, 2nd Edition
6315210Sphk#	Brent Chapman and Elizabeth Zwicky
6415210Sphk#
6515210Sphk#	O'Reilly & Associates, Inc
6673023Sdes#	ISBN 1-56592-871-7
6725478Sjkh#	http://www.ora.com/
6873023Sdes#	http://www.oreilly.com/catalog/fire2/
6915210Sphk#
7015210Sphk# For a more advanced treatment of Internet Security read:
7115210Sphk#
72175244Smaxim#	Firewalls and Internet Security: Repelling the Wily Hacker, 2nd Edition
73175244Smaxim#	William R. Cheswick, Steven M. Bellowin, Aviel D. Rubin
7415027Sphk#
75175244Smaxim#	Addison-Wesley / Prentice Hall
76175244Smaxim#	ISBN 0-201-63466-X
77175244Smaxim#	http://www.pearsonhighered.com/
78175244Smaxim#	http://www.pearsonhighered.com/educator/academic/product/0,3110,020163466X,00.html
7915027Sphk#
8015027Sphk
8191019Scjcsetup_loopback () {
8291019Scjc	############
8391019Scjc	# Only in rare cases do you want to change these rules
8491019Scjc	#
8591019Scjc	${fwcmd} add 100 pass all from any to any via lo0
8691019Scjc	${fwcmd} add 200 deny all from any to 127.0.0.0/8
8791019Scjc	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
8891019Scjc}
8991019Scjc
9051231Ssheldonhif [ -n "${1}" ]; then
9151231Ssheldonh	firewall_type="${1}"
9229300Sdannyfi
9329300Sdanny
9415027Sphk############
9529300Sdanny# Set quiet mode if requested
9651231Ssheldonh#
9751231Ssheldonhcase ${firewall_quiet} in
9851231Ssheldonh[Yy][Ee][Ss])
9929300Sdanny	fwcmd="/sbin/ipfw -q"
10051231Ssheldonh	;;
10151231Ssheldonh*)
10229300Sdanny	fwcmd="/sbin/ipfw"
10351231Ssheldonh	;;
10451231Ssheldonhesac
10529300Sdanny
10629300Sdanny############
10716578Salex# Flush out the list before we begin.
10851231Ssheldonh#
10950357Ssheldonh${fwcmd} -f flush
11016578Salex
111163749Sphksetup_loopback
112163749Sphk
11316578Salex############
11464244Sru# Network Address Translation.  All packets are passed to natd(8)
11564244Sru# before they encounter your remaining rules.  The firewall rules
11664244Sru# will then be run again on each packet after translation by natd
11764244Sru# starting at the rule number following the divert rule.
11851231Ssheldonh#
11964244Sru# For ``simple'' firewall type the divert rule should be put to a
12064244Sru# different place to not interfere with address-checking rules.
12173842Sobrien#
12264244Srucase ${firewall_type} in
12365257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
12464244Sru	case ${natd_enable} in
12564244Sru	[Yy][Ee][Ss])
12664244Sru		if [ -n "${natd_interface}" ]; then
127152562Sume			${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
12864244Sru		fi
12964244Sru		;;
13064244Sru	esac
131165648Spiso	case ${firewall_nat_enable} in
132165648Spiso	[Yy][Ee][Ss])
133165648Spiso		if [ -n "${firewall_nat_interface}" ]; then
134175522Srafan			if echo "${firewall_nat_interface}" | \
135175522Srafan				grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then
136175522Srafan				firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}"
137175522Srafan			else
138175522Srafan				firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}"
139175522Srafan			fi
140175522Srafan			${fwcmd} nat 123 config log ${firewall_nat_flags}
141165648Spiso			${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
142165648Spiso		fi
143165648Spiso		;;
144165648Spiso	esac
14551231Ssheldonhesac
14635267Sbrian
14735267Sbrian############
14815027Sphk# If you just configured ipfw in the kernel as a tool to solve network
14915027Sphk# problems or you just want to disallow some particular kinds of traffic
15051805Smpp# then you will want to change the default policy to open.  You can also
15117594Sjkh# do this as your only action by setting the firewall_type to ``open''.
15251231Ssheldonh#
15350357Ssheldonh# ${fwcmd} add 65000 pass all from any to any
15415027Sphk
15515027Sphk
15617594Sjkh# Prototype setups.
15751231Ssheldonh#
15851231Ssheldonhcase ${firewall_type} in
15951231Ssheldonh[Oo][Pp][Ee][Nn])
16050357Ssheldonh	${fwcmd} add 65000 pass all from any to any
16151231Ssheldonh	;;
16254108Sobrien
16351231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
16451231Ssheldonh	############
16551231Ssheldonh	# This is a prototype setup that will protect your system somewhat
16651231Ssheldonh	# against people from outside your own network.
16751231Ssheldonh	############
16829300Sdanny
16951231Ssheldonh	# set these to your network and netmask and ip
17056736Srgrimes	net="192.0.2.0"
17151231Ssheldonh	mask="255.255.255.0"
17217594Sjkh
173179598Skeramida	# Allow limited broadcast traffic from my own net.
174179598Skeramida	${fwcmd} add pass all from ${net}:${mask} to 255.255.255.255
175179598Skeramida
17651231Ssheldonh	# Allow any traffic to or from my own net.
177181760Sjhb	${fwcmd} add pass all from me to ${net}:${mask}
178181760Sjhb	${fwcmd} add pass all from ${net}:${mask} to me
17915027Sphk
18051231Ssheldonh	# Allow TCP through if setup succeeded
18151231Ssheldonh	${fwcmd} add pass tcp from any to any established
18215027Sphk
18352873Sru	# Allow IP fragments to pass through
18452873Sru	${fwcmd} add pass all from any to any frag
18552873Sru
18651231Ssheldonh	# Allow setup of incoming email
187163749Sphk	${fwcmd} add pass tcp from any to me 25 setup
18815027Sphk
18951231Ssheldonh	# Allow setup of outgoing TCP connections only
190163749Sphk	${fwcmd} add pass tcp from me to any setup
19115027Sphk
19251231Ssheldonh	# Disallow setup of all other TCP connections
19351231Ssheldonh	${fwcmd} add deny tcp from any to any setup
19415027Sphk
19551231Ssheldonh	# Allow DNS queries out in the world
196163749Sphk	${fwcmd} add pass udp from me to any 53 keep-state
19715027Sphk
19851231Ssheldonh	# Allow NTP queries out in the world
199163749Sphk	${fwcmd} add pass udp from me to any 123 keep-state
20015027Sphk
20151231Ssheldonh	# Everything else is denied by default, unless the
20251231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
20351231Ssheldonh	# config file.
20451231Ssheldonh	;;
20515027Sphk
20651231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee])
20751231Ssheldonh	############
20851231Ssheldonh	# This is a prototype setup for a simple firewall.  Configure this
209121881Sru	# machine as a DNS and NTP server, and point all the machines
21051231Ssheldonh	# on the inside at this machine for those services.
21151231Ssheldonh	############
21215027Sphk
21351231Ssheldonh	# set these to your outside interface network and netmask and ip
21451231Ssheldonh	oif="ed0"
21556736Srgrimes	onet="192.0.2.0"
21656736Srgrimes	omask="255.255.255.240"
21717594Sjkh
21851231Ssheldonh	# set these to your inside interface network and netmask and ip
21951231Ssheldonh	iif="ed1"
22056736Srgrimes	inet="192.0.2.16"
22156736Srgrimes	imask="255.255.255.240"
22215027Sphk
22351231Ssheldonh	# Stop spoofing
22451231Ssheldonh	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
22551231Ssheldonh	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
22615027Sphk
22751231Ssheldonh	# Stop RFC1918 nets on the outside interface
22856736Srgrimes	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
22956736Srgrimes	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
23056736Srgrimes	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
23115027Sphk
23264028Sobrien	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
23364028Sobrien	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
23464028Sobrien	# on the outside interface
23556736Srgrimes	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
23656736Srgrimes	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
23756736Srgrimes	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
23856736Srgrimes	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
23956736Srgrimes	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
24056736Srgrimes
24164244Sru	# Network Address Translation.  This rule is placed here deliberately
24264244Sru	# so that it does not interfere with the surrounding address-checking
24364244Sru	# rules.  If for example one of your internal LAN machines had its IP
24464244Sru	# address set to 192.0.2.1 then an incoming packet for it after being
24564244Sru	# translated by natd(8) would match the `deny' rule above.  Similarly
24664244Sru	# an outgoing packet originated from it before being translated would
24764244Sru	# match the `deny' rule below.
24864244Sru	case ${natd_enable} in
24964244Sru	[Yy][Ee][Ss])
25064244Sru		if [ -n "${natd_interface}" ]; then
25164244Sru			${fwcmd} add divert natd all from any to any via ${natd_interface}
25264244Sru		fi
25364244Sru		;;
25464244Sru	esac
25564244Sru
25664244Sru	# Stop RFC1918 nets on the outside interface
25764244Sru	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
25864244Sru	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
25964244Sru	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
26064244Sru
26164244Sru	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
26264244Sru	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
26364244Sru	# on the outside interface
26464244Sru	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
26564244Sru	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
26664244Sru	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
26764244Sru	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
26864244Sru	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
26964244Sru
27051231Ssheldonh	# Allow TCP through if setup succeeded
27151231Ssheldonh	${fwcmd} add pass tcp from any to any established
27215027Sphk
27352873Sru	# Allow IP fragments to pass through
27452873Sru	${fwcmd} add pass all from any to any frag
27552873Sru
27651231Ssheldonh	# Allow setup of incoming email
277181760Sjhb	${fwcmd} add pass tcp from any to me 25 setup
27815027Sphk
27951231Ssheldonh	# Allow access to our DNS
280181760Sjhb	${fwcmd} add pass tcp from any to me 53 setup
281181760Sjhb	${fwcmd} add pass udp from any to me 53
282181760Sjhb	${fwcmd} add pass udp from me 53 to any
28315027Sphk
28451231Ssheldonh	# Allow access to our WWW
285181760Sjhb	${fwcmd} add pass tcp from any to me 80 setup
28615027Sphk
28751231Ssheldonh	# Reject&Log all setup of incoming connections from the outside
28851231Ssheldonh	${fwcmd} add deny log tcp from any to any in via ${oif} setup
28915027Sphk
29051231Ssheldonh	# Allow setup of any other TCP connection
29151231Ssheldonh	${fwcmd} add pass tcp from any to any setup
29215027Sphk
29351231Ssheldonh	# Allow DNS queries out in the world
294181760Sjhb	${fwcmd} add pass udp from me to any 53 keep-state
29515027Sphk
29651231Ssheldonh	# Allow NTP queries out in the world
297181760Sjhb	${fwcmd} add pass udp from me to any 123 keep-state
29815027Sphk
29951231Ssheldonh	# Everything else is denied by default, unless the
30051231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
30151231Ssheldonh	# config file.
30251231Ssheldonh	;;
30315027Sphk
304163749Sphk[Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
305163749Sphk	# Configuration:
306163749Sphk	#  firewall_myservices:		List of TCP ports on which this host
307163749Sphk	#			 	 offers services.
308163749Sphk	#  firewall_allowservices:	List of IPs which has access to
309163749Sphk	#				 $firewall_myservices.
310163749Sphk	#  firewall_trusted:		List of IPs which has full access 
311163749Sphk	#				 to this host. Be very carefull 
312163749Sphk	#				 when setting this. This option can
313163749Sphk	#				 seriously degrade the level of 
314163749Sphk	#				 protection provided by the firewall.
315163749Sphk	#  firewall_logdeny:		Boolean (YES/NO) specifying if the
316163749Sphk	#				 default denied packets should be
317163749Sphk	#				 logged (in /var/log/security).
318163749Sphk	#  firewall_nologports:		List of TCP/UDP ports for which
319163749Sphk	#				 denied incomming packets are not
320163749Sphk	#				 logged.
321163749Sphk	
322163749Sphk	# Allow packets for which a state has been built.
323163749Sphk	${fwcmd} add check-state
324163749Sphk
325163749Sphk	# For services permitted below.
326163749Sphk	${fwcmd} add pass tcp  from me to any established
327163749Sphk
328163749Sphk	# Allow any connection out, adding state for each.
329163749Sphk	${fwcmd} add pass tcp  from me to any setup keep-state
330163749Sphk	${fwcmd} add pass udp  from me to any       keep-state
331163749Sphk	${fwcmd} add pass icmp from me to any       keep-state
332163749Sphk
333163749Sphk	# Allow DHCP.
334163749Sphk	${fwcmd} add pass udp  from 0.0.0.0 68 to 255.255.255.255 67 out
335163749Sphk	${fwcmd} add pass udp  from any 67     to me 68 in
336163749Sphk	${fwcmd} add pass udp  from any 67     to 255.255.255.255 68 in
337163749Sphk	# Some servers will ping the IP while trying to decide if it's 
338163749Sphk	# still in use.
339163749Sphk	${fwcmd} add pass icmp from any to any icmptype 8
340163749Sphk
341163749Sphk	# Allow "mandatory" ICMP in.
342163749Sphk	${fwcmd} add pass icmp from any to any icmptype 3,4,11
343163749Sphk	
344163749Sphk	# Add permits for this workstations published services below
345163749Sphk	# Only IPs and nets in firewall_allowservices is allowed in.
346163749Sphk	# If you really wish to let anyone use services on your 
347163749Sphk	# workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
348163749Sphk	#
349163749Sphk	# Note: We don't use keep-state as that would allow DoS of
350163749Sphk	#       our statetable. 
351163749Sphk	#       You can add 'keep-state' to the lines for slightly
352163749Sphk	#       better performance if you fell that DoS of your
353163749Sphk	#       workstation won't be a problem.
354163749Sphk	#
355163749Sphk	for i in ${firewall_allowservices} ; do
356163749Sphk	  for j in ${firewall_myservices} ; do
357163749Sphk	    ${fwcmd} add pass tcp from $i to me $j
358163749Sphk	  done
359163749Sphk	done
360163749Sphk
361163749Sphk	# Allow all connections from trusted IPs.
362163749Sphk	# Playing with the content of firewall_trusted could seriously
363163749Sphk	# degrade the level of protection provided by the firewall.
364163749Sphk	for i in ${firewall_trusted} ; do
365163749Sphk	  ${fwcmd} add pass ip from $i to me
366163749Sphk	done
367163749Sphk	
368163749Sphk	${fwcmd} add 65000 count ip from any to any
369163749Sphk
370163749Sphk	# Drop packets to ports where we don't want logging
371163749Sphk	for i in ${firewall_nologports} ; do
372163749Sphk	  ${fwcmd} add deny { tcp or udp } from any to any $i in
373163749Sphk	done
374163749Sphk
375163749Sphk	# Broadcasts and muticasts
376163749Sphk	${fwcmd} add deny ip  from any to 255.255.255.255
377163749Sphk	${fwcmd} add deny ip  from any to 224.0.0.0/24 in	# XXX
378163749Sphk
379163749Sphk	# Noise from routers
380163749Sphk	${fwcmd} add deny udp from any to any 520 in
381163749Sphk
382163749Sphk	# Noise from webbrowsing.
383163749Sphk	# The statefull filter is a bit agressive, and will cause some
384163749Sphk	#  connection teardowns to be logged.
385163749Sphk	${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
386163749Sphk
387163749Sphk	# Deny and (if wanted) log the rest unconditionally.
388163749Sphk	log=""
389163749Sphk	if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
390163749Sphk	  log="log logamount 500"	# The default of 100 is too low.
391163749Sphk	  sysctl net.inet.ip.fw.verbose=1 >/dev/null
392163749Sphk	fi
393163749Sphk	${fwcmd} add deny $log ip from any to any
394163749Sphk	;;
395163749Sphk
39691019Scjc[Cc][Ll][Oo][Ss][Ee][Dd])
397163749Sphk	${fwcmd} add 65000 deny ip from any to any
39891019Scjc	;;
39951231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
40051231Ssheldonh	;;
40151231Ssheldonh*)
40259669Sbsd	if [ -r "${firewall_type}" ]; then
40357014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
40451231Ssheldonh	fi
40551231Ssheldonh	;;
40651231Ssheldonhesac
407