rc.firewall revision 165648
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 165648 2006-12-29 21:59:17Z piso $
2766830Sobrien#
2815027Sphk
2966830Sobrien#
3066830Sobrien# Setup system for 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#
7215027Sphk#	Firewalls & Internet Security
7315027Sphk#	Repelling the wily hacker
7415027Sphk#	William R. Cheswick, Steven M. Bellowin
7515027Sphk#
7615027Sphk#	Addison-Wesley
7773023Sdes#	ISBN 0-201-63357-4
7825478Sjkh#	http://www.awl.com/
7973023Sdes#	http://www.awlonline.com/product/0%2C2627%2C0201633574%2C00.html
8015027Sphk#
8115027Sphk
8291019Scjcsetup_loopback () {
8391019Scjc	############
8491019Scjc	# Only in rare cases do you want to change these rules
8591019Scjc	#
8691019Scjc	${fwcmd} add 100 pass all from any to any via lo0
8791019Scjc	${fwcmd} add 200 deny all from any to 127.0.0.0/8
8891019Scjc	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
8991019Scjc}
9091019Scjc
9151231Ssheldonhif [ -n "${1}" ]; then
9251231Ssheldonh	firewall_type="${1}"
9329300Sdannyfi
9429300Sdanny
9515027Sphk############
9629300Sdanny# Set quiet mode if requested
9751231Ssheldonh#
9851231Ssheldonhcase ${firewall_quiet} in
9951231Ssheldonh[Yy][Ee][Ss])
10029300Sdanny	fwcmd="/sbin/ipfw -q"
10151231Ssheldonh	;;
10251231Ssheldonh*)
10329300Sdanny	fwcmd="/sbin/ipfw"
10451231Ssheldonh	;;
10551231Ssheldonhesac
10629300Sdanny
10729300Sdanny############
10816578Salex# Flush out the list before we begin.
10951231Ssheldonh#
11050357Ssheldonh${fwcmd} -f flush
11116578Salex
112163749Sphksetup_loopback
113163749Sphk
11416578Salex############
11564244Sru# Network Address Translation.  All packets are passed to natd(8)
11664244Sru# before they encounter your remaining rules.  The firewall rules
11764244Sru# will then be run again on each packet after translation by natd
11864244Sru# starting at the rule number following the divert rule.
11951231Ssheldonh#
12064244Sru# For ``simple'' firewall type the divert rule should be put to a
12164244Sru# different place to not interfere with address-checking rules.
12273842Sobrien#
12364244Srucase ${firewall_type} in
12465257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
12564244Sru	case ${natd_enable} in
12664244Sru	[Yy][Ee][Ss])
12764244Sru		if [ -n "${natd_interface}" ]; then
128152562Sume			${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
12964244Sru		fi
13064244Sru		;;
13164244Sru	esac
132165648Spiso	case ${firewall_nat_enable} in
133165648Spiso	[Yy][Ee][Ss])
134165648Spiso		if [ -n "${firewall_nat_interface}" ]; then
135165648Spiso			${fwcmd} nat 123 config if ${firewall_nat_interface} log
136165648Spiso			${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
137165648Spiso		fi
138165648Spiso		;;
139165648Spiso	esac
14051231Ssheldonhesac
14135267Sbrian
14235267Sbrian############
14315027Sphk# If you just configured ipfw in the kernel as a tool to solve network
14415027Sphk# problems or you just want to disallow some particular kinds of traffic
14551805Smpp# then you will want to change the default policy to open.  You can also
14617594Sjkh# do this as your only action by setting the firewall_type to ``open''.
14751231Ssheldonh#
14850357Ssheldonh# ${fwcmd} add 65000 pass all from any to any
14915027Sphk
15015027Sphk
15117594Sjkh# Prototype setups.
15251231Ssheldonh#
15351231Ssheldonhcase ${firewall_type} in
15451231Ssheldonh[Oo][Pp][Ee][Nn])
15550357Ssheldonh	${fwcmd} add 65000 pass all from any to any
15651231Ssheldonh	;;
15754108Sobrien
15851231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
15951231Ssheldonh	############
16051231Ssheldonh	# This is a prototype setup that will protect your system somewhat
16151231Ssheldonh	# against people from outside your own network.
16251231Ssheldonh	############
16329300Sdanny
16451231Ssheldonh	# set these to your network and netmask and ip
16556736Srgrimes	net="192.0.2.0"
16651231Ssheldonh	mask="255.255.255.0"
16756736Srgrimes	ip="192.0.2.1"
16817594Sjkh
16951231Ssheldonh	# Allow any traffic to or from my own net.
17051231Ssheldonh	${fwcmd} add pass all from ${ip} to ${net}:${mask}
17151231Ssheldonh	${fwcmd} add pass all from ${net}:${mask} to ${ip}
17215027Sphk
17351231Ssheldonh	# Allow TCP through if setup succeeded
17451231Ssheldonh	${fwcmd} add pass tcp from any to any established
17515027Sphk
17652873Sru	# Allow IP fragments to pass through
17752873Sru	${fwcmd} add pass all from any to any frag
17852873Sru
17951231Ssheldonh	# Allow setup of incoming email
180163749Sphk	${fwcmd} add pass tcp from any to me 25 setup
18115027Sphk
18251231Ssheldonh	# Allow setup of outgoing TCP connections only
183163749Sphk	${fwcmd} add pass tcp from me to any setup
18415027Sphk
18551231Ssheldonh	# Disallow setup of all other TCP connections
18651231Ssheldonh	${fwcmd} add deny tcp from any to any setup
18715027Sphk
18851231Ssheldonh	# Allow DNS queries out in the world
189163749Sphk	${fwcmd} add pass udp from me to any 53 keep-state
19015027Sphk
19151231Ssheldonh	# Allow NTP queries out in the world
192163749Sphk	${fwcmd} add pass udp from me to any 123 keep-state
19315027Sphk
19451231Ssheldonh	# Everything else is denied by default, unless the
19551231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
19651231Ssheldonh	# config file.
19751231Ssheldonh	;;
19815027Sphk
19951231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee])
20051231Ssheldonh	############
20151231Ssheldonh	# This is a prototype setup for a simple firewall.  Configure this
202121881Sru	# machine as a DNS and NTP server, and point all the machines
20351231Ssheldonh	# on the inside at this machine for those services.
20451231Ssheldonh	############
20515027Sphk
20651231Ssheldonh	# set these to your outside interface network and netmask and ip
20751231Ssheldonh	oif="ed0"
20856736Srgrimes	onet="192.0.2.0"
20956736Srgrimes	omask="255.255.255.240"
21056736Srgrimes	oip="192.0.2.1"
21117594Sjkh
21251231Ssheldonh	# set these to your inside interface network and netmask and ip
21351231Ssheldonh	iif="ed1"
21456736Srgrimes	inet="192.0.2.16"
21556736Srgrimes	imask="255.255.255.240"
21656736Srgrimes	iip="192.0.2.17"
21715027Sphk
21851231Ssheldonh	# Stop spoofing
21951231Ssheldonh	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
22051231Ssheldonh	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
22115027Sphk
22251231Ssheldonh	# Stop RFC1918 nets on the outside interface
22356736Srgrimes	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
22456736Srgrimes	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
22556736Srgrimes	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
22615027Sphk
22764028Sobrien	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
22864028Sobrien	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
22964028Sobrien	# on the outside interface
23056736Srgrimes	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
23156736Srgrimes	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
23256736Srgrimes	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
23356736Srgrimes	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
23456736Srgrimes	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
23556736Srgrimes
23664244Sru	# Network Address Translation.  This rule is placed here deliberately
23764244Sru	# so that it does not interfere with the surrounding address-checking
23864244Sru	# rules.  If for example one of your internal LAN machines had its IP
23964244Sru	# address set to 192.0.2.1 then an incoming packet for it after being
24064244Sru	# translated by natd(8) would match the `deny' rule above.  Similarly
24164244Sru	# an outgoing packet originated from it before being translated would
24264244Sru	# match the `deny' rule below.
24364244Sru	case ${natd_enable} in
24464244Sru	[Yy][Ee][Ss])
24564244Sru		if [ -n "${natd_interface}" ]; then
24664244Sru			${fwcmd} add divert natd all from any to any via ${natd_interface}
24764244Sru		fi
24864244Sru		;;
24964244Sru	esac
25064244Sru
25164244Sru	# Stop RFC1918 nets on the outside interface
25264244Sru	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
25364244Sru	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
25464244Sru	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
25564244Sru
25664244Sru	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
25764244Sru	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
25864244Sru	# on the outside interface
25964244Sru	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
26064244Sru	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
26164244Sru	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
26264244Sru	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
26364244Sru	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
26464244Sru
26551231Ssheldonh	# Allow TCP through if setup succeeded
26651231Ssheldonh	${fwcmd} add pass tcp from any to any established
26715027Sphk
26852873Sru	# Allow IP fragments to pass through
26952873Sru	${fwcmd} add pass all from any to any frag
27052873Sru
27151231Ssheldonh	# Allow setup of incoming email
27251231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 25 setup
27315027Sphk
27451231Ssheldonh	# Allow access to our DNS
27551231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 53 setup
27652404Sru	${fwcmd} add pass udp from any to ${oip} 53
27752404Sru	${fwcmd} add pass udp from ${oip} 53 to any
27815027Sphk
27951231Ssheldonh	# Allow access to our WWW
28051231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 80 setup
28115027Sphk
28251231Ssheldonh	# Reject&Log all setup of incoming connections from the outside
28351231Ssheldonh	${fwcmd} add deny log tcp from any to any in via ${oif} setup
28415027Sphk
28551231Ssheldonh	# Allow setup of any other TCP connection
28651231Ssheldonh	${fwcmd} add pass tcp from any to any setup
28715027Sphk
28851231Ssheldonh	# Allow DNS queries out in the world
28972772Snsayer	${fwcmd} add pass udp from ${oip} to any 53 keep-state
29015027Sphk
29151231Ssheldonh	# Allow NTP queries out in the world
29272772Snsayer	${fwcmd} add pass udp from ${oip} to any 123 keep-state
29315027Sphk
29451231Ssheldonh	# Everything else is denied by default, unless the
29551231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
29651231Ssheldonh	# config file.
29751231Ssheldonh	;;
29815027Sphk
299163749Sphk[Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
300163749Sphk	# Configuration:
301163749Sphk	#  firewall_myservices:		List of TCP ports on which this host
302163749Sphk	#			 	 offers services.
303163749Sphk	#  firewall_allowservices:	List of IPs which has access to
304163749Sphk	#				 $firewall_myservices.
305163749Sphk	#  firewall_trusted:		List of IPs which has full access 
306163749Sphk	#				 to this host. Be very carefull 
307163749Sphk	#				 when setting this. This option can
308163749Sphk	#				 seriously degrade the level of 
309163749Sphk	#				 protection provided by the firewall.
310163749Sphk	#  firewall_logdeny:		Boolean (YES/NO) specifying if the
311163749Sphk	#				 default denied packets should be
312163749Sphk	#				 logged (in /var/log/security).
313163749Sphk	#  firewall_nologports:		List of TCP/UDP ports for which
314163749Sphk	#				 denied incomming packets are not
315163749Sphk	#				 logged.
316163749Sphk	
317163749Sphk	# Allow packets for which a state has been built.
318163749Sphk	${fwcmd} add check-state
319163749Sphk
320163749Sphk	# For services permitted below.
321163749Sphk	${fwcmd} add pass tcp  from me to any established
322163749Sphk
323163749Sphk	# Allow any connection out, adding state for each.
324163749Sphk	${fwcmd} add pass tcp  from me to any setup keep-state
325163749Sphk	${fwcmd} add pass udp  from me to any       keep-state
326163749Sphk	${fwcmd} add pass icmp from me to any       keep-state
327163749Sphk
328163749Sphk	# Allow DHCP.
329163749Sphk	${fwcmd} add pass udp  from 0.0.0.0 68 to 255.255.255.255 67 out
330163749Sphk	${fwcmd} add pass udp  from any 67     to me 68 in
331163749Sphk	${fwcmd} add pass udp  from any 67     to 255.255.255.255 68 in
332163749Sphk	# Some servers will ping the IP while trying to decide if it's 
333163749Sphk	# still in use.
334163749Sphk	${fwcmd} add pass icmp from any to any icmptype 8
335163749Sphk
336163749Sphk	# Allow "mandatory" ICMP in.
337163749Sphk	${fwcmd} add pass icmp from any to any icmptype 3,4,11
338163749Sphk	
339163749Sphk	# Add permits for this workstations published services below
340163749Sphk	# Only IPs and nets in firewall_allowservices is allowed in.
341163749Sphk	# If you really wish to let anyone use services on your 
342163749Sphk	# workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
343163749Sphk	#
344163749Sphk	# Note: We don't use keep-state as that would allow DoS of
345163749Sphk	#       our statetable. 
346163749Sphk	#       You can add 'keep-state' to the lines for slightly
347163749Sphk	#       better performance if you fell that DoS of your
348163749Sphk	#       workstation won't be a problem.
349163749Sphk	#
350163749Sphk	for i in ${firewall_allowservices} ; do
351163749Sphk	  for j in ${firewall_myservices} ; do
352163749Sphk	    ${fwcmd} add pass tcp from $i to me $j
353163749Sphk	  done
354163749Sphk	done
355163749Sphk
356163749Sphk	# Allow all connections from trusted IPs.
357163749Sphk	# Playing with the content of firewall_trusted could seriously
358163749Sphk	# degrade the level of protection provided by the firewall.
359163749Sphk	for i in ${firewall_trusted} ; do
360163749Sphk	  ${fwcmd} add pass ip from $i to me
361163749Sphk	done
362163749Sphk	
363163749Sphk	${fwcmd} add 65000 count ip from any to any
364163749Sphk
365163749Sphk	# Drop packets to ports where we don't want logging
366163749Sphk	for i in ${firewall_nologports} ; do
367163749Sphk	  ${fwcmd} add deny { tcp or udp } from any to any $i in
368163749Sphk	done
369163749Sphk
370163749Sphk	# Broadcasts and muticasts
371163749Sphk	${fwcmd} add deny ip  from any to 255.255.255.255
372163749Sphk	${fwcmd} add deny ip  from any to 224.0.0.0/24 in	# XXX
373163749Sphk
374163749Sphk	# Noise from routers
375163749Sphk	${fwcmd} add deny udp from any to any 520 in
376163749Sphk
377163749Sphk	# Noise from webbrowsing.
378163749Sphk	# The statefull filter is a bit agressive, and will cause some
379163749Sphk	#  connection teardowns to be logged.
380163749Sphk	${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
381163749Sphk
382163749Sphk	# Deny and (if wanted) log the rest unconditionally.
383163749Sphk	log=""
384163749Sphk	if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
385163749Sphk	  log="log logamount 500"	# The default of 100 is too low.
386163749Sphk	  sysctl net.inet.ip.fw.verbose=1 >/dev/null
387163749Sphk	fi
388163749Sphk	${fwcmd} add deny $log ip from any to any
389163749Sphk	;;
390163749Sphk
39191019Scjc[Cc][Ll][Oo][Ss][Ee][Dd])
392163749Sphk	${fwcmd} add 65000 deny ip from any to any
39391019Scjc	;;
39451231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
39551231Ssheldonh	;;
39651231Ssheldonh*)
39759669Sbsd	if [ -r "${firewall_type}" ]; then
39857014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
39951231Ssheldonh	fi
40051231Ssheldonh	;;
40151231Ssheldonhesac
402