rc.firewall revision 66830
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 66830 2000-10-08 19:20:36Z 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#
5715210Sphk#	Building Internet Firewalls
5815210Sphk#	Brent Chapman and Elizabeth Zwicky
5915210Sphk#
6015210Sphk#	O'Reilly & Associates, Inc
6115210Sphk#	ISBN 1-56592-124-0
6225478Sjkh#	http://www.ora.com/
6315210Sphk#
6415210Sphk# For a more advanced treatment of Internet Security read:
6515210Sphk#
6615027Sphk#	Firewalls & Internet Security
6715027Sphk#	Repelling the wily hacker
6815027Sphk#	William R. Cheswick, Steven M. Bellowin
6915027Sphk#
7015027Sphk#	Addison-Wesley
7115027Sphk#	ISBN 0-201-6337-4
7225478Sjkh#	http://www.awl.com/
7315027Sphk#
7415027Sphk
7551231Ssheldonhif [ -n "${1}" ]; then
7651231Ssheldonh	firewall_type="${1}"
7729300Sdannyfi
7829300Sdanny
7915027Sphk############
8029300Sdanny# Set quiet mode if requested
8151231Ssheldonh#
8251231Ssheldonhcase ${firewall_quiet} in
8351231Ssheldonh[Yy][Ee][Ss])
8429300Sdanny	fwcmd="/sbin/ipfw -q"
8551231Ssheldonh	;;
8651231Ssheldonh*)
8729300Sdanny	fwcmd="/sbin/ipfw"
8851231Ssheldonh	;;
8951231Ssheldonhesac
9029300Sdanny
9129300Sdanny############
9216578Salex# Flush out the list before we begin.
9351231Ssheldonh#
9450357Ssheldonh${fwcmd} -f flush
9516578Salex
9616578Salex############
9764244Sru# Network Address Translation.  All packets are passed to natd(8)
9864244Sru# before they encounter your remaining rules.  The firewall rules
9964244Sru# will then be run again on each packet after translation by natd
10064244Sru# starting at the rule number following the divert rule.
10151231Ssheldonh#
10264244Sru# For ``simple'' firewall type the divert rule should be put to a
10364244Sru# different place to not interfere with address-checking rules.
10464244Sru# 
10564244Srucase ${firewall_type} in
10665257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
10764244Sru	case ${natd_enable} in
10864244Sru	[Yy][Ee][Ss])
10964244Sru		if [ -n "${natd_interface}" ]; then
11064244Sru			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
11164244Sru		fi
11264244Sru		;;
11364244Sru	esac
11451231Ssheldonhesac
11535267Sbrian
11635267Sbrian############
11715027Sphk# If you just configured ipfw in the kernel as a tool to solve network
11815027Sphk# problems or you just want to disallow some particular kinds of traffic
11951805Smpp# then you will want to change the default policy to open.  You can also
12017594Sjkh# do this as your only action by setting the firewall_type to ``open''.
12151231Ssheldonh#
12250357Ssheldonh# ${fwcmd} add 65000 pass all from any to any
12315027Sphk
12415027Sphk############
12530617Sdanny# Only in rare cases do you want to change these rules
12651231Ssheldonh#
12750357Ssheldonh${fwcmd} add 100 pass all from any to any via lo0
12850357Ssheldonh${fwcmd} add 200 deny all from any to 127.0.0.0/8
12952449Snsayer# If you're using 'options BRIDGE', uncomment the following line to pass ARP
13052449Snsayer#${fwcmd} add 300 pass udp from 0.0.0.0 2054 to 0.0.0.0
13115027Sphk
13215027Sphk
13317594Sjkh# Prototype setups.
13451231Ssheldonh#
13551231Ssheldonhcase ${firewall_type} in
13651231Ssheldonh[Oo][Pp][Ee][Nn])
13750357Ssheldonh	${fwcmd} add 65000 pass all from any to any
13851231Ssheldonh	;;
13954108Sobrien
14051231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
14151231Ssheldonh	############
14251231Ssheldonh	# This is a prototype setup that will protect your system somewhat
14351231Ssheldonh	# against people from outside your own network.
14451231Ssheldonh	############
14529300Sdanny
14651231Ssheldonh	# set these to your network and netmask and ip
14756736Srgrimes	net="192.0.2.0"
14851231Ssheldonh	mask="255.255.255.0"
14956736Srgrimes	ip="192.0.2.1"
15017594Sjkh
15151231Ssheldonh	# Allow any traffic to or from my own net.
15251231Ssheldonh	${fwcmd} add pass all from ${ip} to ${net}:${mask}
15351231Ssheldonh	${fwcmd} add pass all from ${net}:${mask} to ${ip}
15415027Sphk
15551231Ssheldonh	# Allow TCP through if setup succeeded
15651231Ssheldonh	${fwcmd} add pass tcp from any to any established
15715027Sphk
15852873Sru	# Allow IP fragments to pass through
15952873Sru	${fwcmd} add pass all from any to any frag
16052873Sru
16151231Ssheldonh	# Allow setup of incoming email
16251231Ssheldonh	${fwcmd} add pass tcp from any to ${ip} 25 setup
16315027Sphk
16451231Ssheldonh	# Allow setup of outgoing TCP connections only
16551231Ssheldonh	${fwcmd} add pass tcp from ${ip} to any setup
16615027Sphk
16751231Ssheldonh	# Disallow setup of all other TCP connections
16851231Ssheldonh	${fwcmd} add deny tcp from any to any setup
16915027Sphk
17051231Ssheldonh	# Allow DNS queries out in the world
17151231Ssheldonh	${fwcmd} add pass udp from any 53 to ${ip}
17251231Ssheldonh	${fwcmd} add pass udp from ${ip} to any 53
17315027Sphk
17451231Ssheldonh	# Allow NTP queries out in the world
17551231Ssheldonh	${fwcmd} add pass udp from any 123 to ${ip}
17651231Ssheldonh	${fwcmd} add pass udp from ${ip} to any 123
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
27351231Ssheldonh	${fwcmd} add pass udp from any 53 to ${oip}
27451231Ssheldonh	${fwcmd} add pass udp from ${oip} to any 53
27515027Sphk
27651231Ssheldonh	# Allow NTP queries out in the world
27751231Ssheldonh	${fwcmd} add pass udp from any 123 to ${oip}
27851231Ssheldonh	${fwcmd} add pass udp from ${oip} to any 123
27915027Sphk
28051231Ssheldonh	# Everything else is denied by default, unless the
28151231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
28251231Ssheldonh	# config file.
28351231Ssheldonh	;;
28415027Sphk
28551231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
28651231Ssheldonh	;;
28751231Ssheldonh*)
28859669Sbsd	if [ -r "${firewall_type}" ]; then
28957014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
29051231Ssheldonh	fi
29151231Ssheldonh	;;
29251231Ssheldonhesac
293