rc.firewall revision 73023
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 73023 2001-02-25 11:44:51Z des $
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.
10664244Sru# 
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
13152449Snsayer# If you're using 'options BRIDGE', uncomment the following line to pass ARP
13252449Snsayer#${fwcmd} add 300 pass udp from 0.0.0.0 2054 to 0.0.0.0
13315027Sphk
13415027Sphk
13517594Sjkh# Prototype setups.
13651231Ssheldonh#
13751231Ssheldonhcase ${firewall_type} in
13851231Ssheldonh[Oo][Pp][Ee][Nn])
13950357Ssheldonh	${fwcmd} add 65000 pass all from any to any
14051231Ssheldonh	;;
14154108Sobrien
14251231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
14351231Ssheldonh	############
14451231Ssheldonh	# This is a prototype setup that will protect your system somewhat
14551231Ssheldonh	# against people from outside your own network.
14651231Ssheldonh	############
14729300Sdanny
14851231Ssheldonh	# set these to your network and netmask and ip
14956736Srgrimes	net="192.0.2.0"
15051231Ssheldonh	mask="255.255.255.0"
15156736Srgrimes	ip="192.0.2.1"
15217594Sjkh
15351231Ssheldonh	# Allow any traffic to or from my own net.
15451231Ssheldonh	${fwcmd} add pass all from ${ip} to ${net}:${mask}
15551231Ssheldonh	${fwcmd} add pass all from ${net}:${mask} to ${ip}
15615027Sphk
15751231Ssheldonh	# Allow TCP through if setup succeeded
15851231Ssheldonh	${fwcmd} add pass tcp from any to any established
15915027Sphk
16052873Sru	# Allow IP fragments to pass through
16152873Sru	${fwcmd} add pass all from any to any frag
16252873Sru
16351231Ssheldonh	# Allow setup of incoming email
16451231Ssheldonh	${fwcmd} add pass tcp from any to ${ip} 25 setup
16515027Sphk
16651231Ssheldonh	# Allow setup of outgoing TCP connections only
16751231Ssheldonh	${fwcmd} add pass tcp from ${ip} to any setup
16815027Sphk
16951231Ssheldonh	# Disallow setup of all other TCP connections
17051231Ssheldonh	${fwcmd} add deny tcp from any to any setup
17115027Sphk
17251231Ssheldonh	# Allow DNS queries out in the world
17372772Snsayer	${fwcmd} add pass udp from ${ip} to any 53 keep-state
17415027Sphk
17551231Ssheldonh	# Allow NTP queries out in the world
17672772Snsayer	${fwcmd} add pass udp from ${ip} to any 123 keep-state
17715027Sphk
17851231Ssheldonh	# Everything else is denied by default, unless the
17951231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
18051231Ssheldonh	# config file.
18151231Ssheldonh	;;
18215027Sphk
18351231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee])
18451231Ssheldonh	############
18551231Ssheldonh	# This is a prototype setup for a simple firewall.  Configure this
18651231Ssheldonh	# machine as a named server and ntp server, and point all the machines
18751231Ssheldonh	# on the inside at this machine for those services.
18851231Ssheldonh	############
18915027Sphk
19051231Ssheldonh	# set these to your outside interface network and netmask and ip
19151231Ssheldonh	oif="ed0"
19256736Srgrimes	onet="192.0.2.0"
19356736Srgrimes	omask="255.255.255.240"
19456736Srgrimes	oip="192.0.2.1"
19517594Sjkh
19651231Ssheldonh	# set these to your inside interface network and netmask and ip
19751231Ssheldonh	iif="ed1"
19856736Srgrimes	inet="192.0.2.16"
19956736Srgrimes	imask="255.255.255.240"
20056736Srgrimes	iip="192.0.2.17"
20115027Sphk
20251231Ssheldonh	# Stop spoofing
20351231Ssheldonh	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
20451231Ssheldonh	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
20515027Sphk
20651231Ssheldonh	# Stop RFC1918 nets on the outside interface
20756736Srgrimes	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
20856736Srgrimes	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
20956736Srgrimes	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
21015027Sphk
21164028Sobrien	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
21264028Sobrien	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
21364028Sobrien	# on the outside interface
21456736Srgrimes	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
21556736Srgrimes	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
21656736Srgrimes	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
21756736Srgrimes	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
21856736Srgrimes	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
21956736Srgrimes
22064244Sru	# Network Address Translation.  This rule is placed here deliberately
22164244Sru	# so that it does not interfere with the surrounding address-checking
22264244Sru	# rules.  If for example one of your internal LAN machines had its IP
22364244Sru	# address set to 192.0.2.1 then an incoming packet for it after being
22464244Sru	# translated by natd(8) would match the `deny' rule above.  Similarly
22564244Sru	# an outgoing packet originated from it before being translated would
22664244Sru	# match the `deny' rule below.
22764244Sru	case ${natd_enable} in
22864244Sru	[Yy][Ee][Ss])
22964244Sru		if [ -n "${natd_interface}" ]; then
23064244Sru			${fwcmd} add divert natd all from any to any via ${natd_interface}
23164244Sru		fi
23264244Sru		;;
23364244Sru	esac
23464244Sru
23564244Sru	# Stop RFC1918 nets on the outside interface
23664244Sru	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
23764244Sru	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
23864244Sru	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
23964244Sru
24064244Sru	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
24164244Sru	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
24264244Sru	# on the outside interface
24364244Sru	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
24464244Sru	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
24564244Sru	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
24664244Sru	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
24764244Sru	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
24864244Sru
24951231Ssheldonh	# Allow TCP through if setup succeeded
25051231Ssheldonh	${fwcmd} add pass tcp from any to any established
25115027Sphk
25252873Sru	# Allow IP fragments to pass through
25352873Sru	${fwcmd} add pass all from any to any frag
25452873Sru
25551231Ssheldonh	# Allow setup of incoming email
25651231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 25 setup
25715027Sphk
25851231Ssheldonh	# Allow access to our DNS
25951231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 53 setup
26052404Sru	${fwcmd} add pass udp from any to ${oip} 53
26152404Sru	${fwcmd} add pass udp from ${oip} 53 to any
26215027Sphk
26351231Ssheldonh	# Allow access to our WWW
26451231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 80 setup
26515027Sphk
26651231Ssheldonh	# Reject&Log all setup of incoming connections from the outside
26751231Ssheldonh	${fwcmd} add deny log tcp from any to any in via ${oif} setup
26815027Sphk
26951231Ssheldonh	# Allow setup of any other TCP connection
27051231Ssheldonh	${fwcmd} add pass tcp from any to any setup
27115027Sphk
27251231Ssheldonh	# Allow DNS queries out in the world
27372772Snsayer	${fwcmd} add pass udp from ${oip} to any 53 keep-state
27415027Sphk
27551231Ssheldonh	# Allow NTP queries out in the world
27672772Snsayer	${fwcmd} add pass udp from ${oip} to any 123 keep-state
27715027Sphk
27851231Ssheldonh	# Everything else is denied by default, unless the
27951231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
28051231Ssheldonh	# config file.
28151231Ssheldonh	;;
28215027Sphk
28351231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
28451231Ssheldonh	;;
28551231Ssheldonh*)
28659669Sbsd	if [ -r "${firewall_type}" ]; then
28757014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
28851231Ssheldonh	fi
28951231Ssheldonh	;;
29051231Ssheldonhesac
291