rc.firewall revision 73842
166830Sobrien# Copyright (c) 1996  Poul-Henning Kamp
266830Sobrien# All rights reserved.
366830Sobrien#
466830Sobrien# Redistribution and use in source and binary forms, with or without
566830Sobrien# modification, are permitted provided that the following conditions
666830Sobrien# are met:
766830Sobrien# 1. Redistributions of source code must retain the above copyright
866830Sobrien#    notice, this list of conditions and the following disclaimer.
966830Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1066830Sobrien#    notice, this list of conditions and the following disclaimer in the
1166830Sobrien#    documentation and/or other materials provided with the distribution.
1266830Sobrien#
1366830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1466830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1566830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1666830Sobrien# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1766830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1866830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1966830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2066830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2166830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2266830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2366830Sobrien# SUCH DAMAGE.
2466830Sobrien#
2550472Speter# $FreeBSD: head/etc/rc.firewall 73842 2001-03-06 02:15:38Z obrien $
2666830Sobrien#
2715027Sphk
2866830Sobrien#
2966830Sobrien# Setup system for firewall service.
3066830Sobrien#
3166830Sobrien
3243849Sjkh# Suck in the configuration variables.
3351231Ssheldonhif [ -r /etc/defaults/rc.conf ]; then
3443849Sjkh	. /etc/defaults/rc.conf
3559674Ssheldonh	source_rc_confs
3651231Ssheldonhelif [ -r /etc/rc.conf ]; then
3733203Sadam	. /etc/rc.conf
3833203Sadamfi
3933203Sadam
4015027Sphk############
4129300Sdanny# Define the firewall type in /etc/rc.conf.  Valid values are:
4229300Sdanny#   open     - will allow anyone in
4329300Sdanny#   client   - will try to protect just this machine
4429300Sdanny#   simple   - will try to protect a whole network
4529300Sdanny#   closed   - totally disables IP services except via lo0 interface
4629300Sdanny#   UNKNOWN  - disables the loading of firewall rules.
4729300Sdanny#   filename - will load the rules in the given filename (full path required)
4815027Sphk#
4951231Ssheldonh# For ``client'' and ``simple'' the entries below should be customized
5029300Sdanny# appropriately.
5115027Sphk
5215027Sphk############
5315027Sphk#
5415027Sphk# If you don't know enough about packet filtering, we suggest that you
5515027Sphk# take time to read this book:
5615027Sphk#
5773023Sdes#	Building Internet Firewalls, 2nd Edition
5815210Sphk#	Brent Chapman and Elizabeth Zwicky
5915210Sphk#
6015210Sphk#	O'Reilly & Associates, Inc
6173023Sdes#	ISBN 1-56592-871-7
6225478Sjkh#	http://www.ora.com/
6373023Sdes#	http://www.oreilly.com/catalog/fire2/
6415210Sphk#
6515210Sphk# For a more advanced treatment of Internet Security read:
6615210Sphk#
6715027Sphk#	Firewalls & Internet Security
6815027Sphk#	Repelling the wily hacker
6915027Sphk#	William R. Cheswick, Steven M. Bellowin
7015027Sphk#
7115027Sphk#	Addison-Wesley
7273023Sdes#	ISBN 0-201-63357-4
7325478Sjkh#	http://www.awl.com/
7473023Sdes#	http://www.awlonline.com/product/0%2C2627%2C0201633574%2C00.html
7515027Sphk#
7615027Sphk
7751231Ssheldonhif [ -n "${1}" ]; then
7851231Ssheldonh	firewall_type="${1}"
7929300Sdannyfi
8029300Sdanny
8115027Sphk############
8229300Sdanny# Set quiet mode if requested
8351231Ssheldonh#
8451231Ssheldonhcase ${firewall_quiet} in
8551231Ssheldonh[Yy][Ee][Ss])
8629300Sdanny	fwcmd="/sbin/ipfw -q"
8751231Ssheldonh	;;
8851231Ssheldonh*)
8929300Sdanny	fwcmd="/sbin/ipfw"
9051231Ssheldonh	;;
9151231Ssheldonhesac
9229300Sdanny
9329300Sdanny############
9416578Salex# Flush out the list before we begin.
9551231Ssheldonh#
9650357Ssheldonh${fwcmd} -f flush
9716578Salex
9816578Salex############
9964244Sru# Network Address Translation.  All packets are passed to natd(8)
10064244Sru# before they encounter your remaining rules.  The firewall rules
10164244Sru# will then be run again on each packet after translation by natd
10264244Sru# starting at the rule number following the divert rule.
10351231Ssheldonh#
10464244Sru# For ``simple'' firewall type the divert rule should be put to a
10564244Sru# different place to not interfere with address-checking rules.
10673842Sobrien#
10764244Srucase ${firewall_type} in
10865257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
10964244Sru	case ${natd_enable} in
11064244Sru	[Yy][Ee][Ss])
11164244Sru		if [ -n "${natd_interface}" ]; then
11264244Sru			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
11364244Sru		fi
11464244Sru		;;
11564244Sru	esac
11651231Ssheldonhesac
11735267Sbrian
11835267Sbrian############
11915027Sphk# If you just configured ipfw in the kernel as a tool to solve network
12015027Sphk# problems or you just want to disallow some particular kinds of traffic
12151805Smpp# then you will want to change the default policy to open.  You can also
12217594Sjkh# do this as your only action by setting the firewall_type to ``open''.
12351231Ssheldonh#
12450357Ssheldonh# ${fwcmd} add 65000 pass all from any to any
12515027Sphk
12615027Sphk############
12730617Sdanny# Only in rare cases do you want to change these rules
12851231Ssheldonh#
12950357Ssheldonh${fwcmd} add 100 pass all from any to any via lo0
13050357Ssheldonh${fwcmd} add 200 deny all from any to 127.0.0.0/8
13173785Sobrien${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
13252449Snsayer# If you're using 'options BRIDGE', uncomment the following line to pass ARP
13373785Sobrien#${fwcmd} add 400 pass udp from 0.0.0.0 2054 to 0.0.0.0
13415027Sphk
13515027Sphk
13617594Sjkh# Prototype setups.
13751231Ssheldonh#
13851231Ssheldonhcase ${firewall_type} in
13951231Ssheldonh[Oo][Pp][Ee][Nn])
14050357Ssheldonh	${fwcmd} add 65000 pass all from any to any
14151231Ssheldonh	;;
14254108Sobrien
14351231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
14451231Ssheldonh	############
14551231Ssheldonh	# This is a prototype setup that will protect your system somewhat
14651231Ssheldonh	# against people from outside your own network.
14751231Ssheldonh	############
14829300Sdanny
14951231Ssheldonh	# set these to your network and netmask and ip
15056736Srgrimes	net="192.0.2.0"
15151231Ssheldonh	mask="255.255.255.0"
15256736Srgrimes	ip="192.0.2.1"
15317594Sjkh
15451231Ssheldonh	# Allow any traffic to or from my own net.
15551231Ssheldonh	${fwcmd} add pass all from ${ip} to ${net}:${mask}
15651231Ssheldonh	${fwcmd} add pass all from ${net}:${mask} to ${ip}
15715027Sphk
15851231Ssheldonh	# Allow TCP through if setup succeeded
15951231Ssheldonh	${fwcmd} add pass tcp from any to any established
16015027Sphk
16152873Sru	# Allow IP fragments to pass through
16252873Sru	${fwcmd} add pass all from any to any frag
16352873Sru
16451231Ssheldonh	# Allow setup of incoming email
16551231Ssheldonh	${fwcmd} add pass tcp from any to ${ip} 25 setup
16615027Sphk
16751231Ssheldonh	# Allow setup of outgoing TCP connections only
16851231Ssheldonh	${fwcmd} add pass tcp from ${ip} to any setup
16915027Sphk
17051231Ssheldonh	# Disallow setup of all other TCP connections
17151231Ssheldonh	${fwcmd} add deny tcp from any to any setup
17215027Sphk
17351231Ssheldonh	# Allow DNS queries out in the world
17472772Snsayer	${fwcmd} add pass udp from ${ip} to any 53 keep-state
17515027Sphk
17651231Ssheldonh	# Allow NTP queries out in the world
17772772Snsayer	${fwcmd} add pass udp from ${ip} to any 123 keep-state
17815027Sphk
17951231Ssheldonh	# Everything else is denied by default, unless the
18051231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
18151231Ssheldonh	# config file.
18251231Ssheldonh	;;
18315027Sphk
18451231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee])
18551231Ssheldonh	############
18651231Ssheldonh	# This is a prototype setup for a simple firewall.  Configure this
18751231Ssheldonh	# machine as a named server and ntp server, and point all the machines
18851231Ssheldonh	# on the inside at this machine for those services.
18951231Ssheldonh	############
19015027Sphk
19151231Ssheldonh	# set these to your outside interface network and netmask and ip
19251231Ssheldonh	oif="ed0"
19356736Srgrimes	onet="192.0.2.0"
19456736Srgrimes	omask="255.255.255.240"
19556736Srgrimes	oip="192.0.2.1"
19617594Sjkh
19751231Ssheldonh	# set these to your inside interface network and netmask and ip
19851231Ssheldonh	iif="ed1"
19956736Srgrimes	inet="192.0.2.16"
20056736Srgrimes	imask="255.255.255.240"
20156736Srgrimes	iip="192.0.2.17"
20215027Sphk
20351231Ssheldonh	# Stop spoofing
20451231Ssheldonh	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
20551231Ssheldonh	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
20615027Sphk
20751231Ssheldonh	# Stop RFC1918 nets on the outside interface
20856736Srgrimes	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
20956736Srgrimes	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
21056736Srgrimes	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
21115027Sphk
21264028Sobrien	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
21364028Sobrien	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
21464028Sobrien	# on the outside interface
21556736Srgrimes	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
21656736Srgrimes	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
21756736Srgrimes	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
21856736Srgrimes	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
21956736Srgrimes	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
22056736Srgrimes
22164244Sru	# Network Address Translation.  This rule is placed here deliberately
22264244Sru	# so that it does not interfere with the surrounding address-checking
22364244Sru	# rules.  If for example one of your internal LAN machines had its IP
22464244Sru	# address set to 192.0.2.1 then an incoming packet for it after being
22564244Sru	# translated by natd(8) would match the `deny' rule above.  Similarly
22664244Sru	# an outgoing packet originated from it before being translated would
22764244Sru	# match the `deny' rule below.
22864244Sru	case ${natd_enable} in
22964244Sru	[Yy][Ee][Ss])
23064244Sru		if [ -n "${natd_interface}" ]; then
23164244Sru			${fwcmd} add divert natd all from any to any via ${natd_interface}
23264244Sru		fi
23364244Sru		;;
23464244Sru	esac
23564244Sru
23664244Sru	# Stop RFC1918 nets on the outside interface
23764244Sru	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
23864244Sru	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
23964244Sru	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
24064244Sru
24164244Sru	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
24264244Sru	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
24364244Sru	# on the outside interface
24464244Sru	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
24564244Sru	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
24664244Sru	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
24764244Sru	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
24864244Sru	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
24964244Sru
25051231Ssheldonh	# Allow TCP through if setup succeeded
25151231Ssheldonh	${fwcmd} add pass tcp from any to any established
25215027Sphk
25352873Sru	# Allow IP fragments to pass through
25452873Sru	${fwcmd} add pass all from any to any frag
25552873Sru
25651231Ssheldonh	# Allow setup of incoming email
25751231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 25 setup
25815027Sphk
25951231Ssheldonh	# Allow access to our DNS
26051231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 53 setup
26152404Sru	${fwcmd} add pass udp from any to ${oip} 53
26252404Sru	${fwcmd} add pass udp from ${oip} 53 to any
26315027Sphk
26451231Ssheldonh	# Allow access to our WWW
26551231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 80 setup
26615027Sphk
26751231Ssheldonh	# Reject&Log all setup of incoming connections from the outside
26851231Ssheldonh	${fwcmd} add deny log tcp from any to any in via ${oif} setup
26915027Sphk
27051231Ssheldonh	# Allow setup of any other TCP connection
27151231Ssheldonh	${fwcmd} add pass tcp from any to any setup
27215027Sphk
27351231Ssheldonh	# Allow DNS queries out in the world
27472772Snsayer	${fwcmd} add pass udp from ${oip} to any 53 keep-state
27515027Sphk
27651231Ssheldonh	# Allow NTP queries out in the world
27772772Snsayer	${fwcmd} add pass udp from ${oip} to any 123 keep-state
27815027Sphk
27951231Ssheldonh	# Everything else is denied by default, unless the
28051231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
28151231Ssheldonh	# config file.
28251231Ssheldonh	;;
28315027Sphk
28451231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
28551231Ssheldonh	;;
28651231Ssheldonh*)
28759669Sbsd	if [ -r "${firewall_type}" ]; then
28857014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
28951231Ssheldonh	fi
29051231Ssheldonh	;;
29151231Ssheldonhesac
292