rc.firewall revision 110476
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 110476 2003-02-06 22:00:38Z trhodes $
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:
4529300Sdanny#   open     - will allow anyone in
4629300Sdanny#   client   - will try to protect just this machine
4729300Sdanny#   simple   - will try to protect a whole network
4829300Sdanny#   closed   - totally disables IP services except via lo0 interface
4929300Sdanny#   UNKNOWN  - disables the loading of firewall rules.
5029300Sdanny#   filename - will load the rules in the given filename (full path required)
5115027Sphk#
5251231Ssheldonh# For ``client'' and ``simple'' the entries below should be customized
5329300Sdanny# appropriately.
5415027Sphk
5515027Sphk############
5615027Sphk#
5715027Sphk# If you don't know enough about packet filtering, we suggest that you
5815027Sphk# take time to read this book:
5915027Sphk#
6073023Sdes#	Building Internet Firewalls, 2nd Edition
6115210Sphk#	Brent Chapman and Elizabeth Zwicky
6215210Sphk#
6315210Sphk#	O'Reilly & Associates, Inc
6473023Sdes#	ISBN 1-56592-871-7
6525478Sjkh#	http://www.ora.com/
6673023Sdes#	http://www.oreilly.com/catalog/fire2/
6715210Sphk#
6815210Sphk# For a more advanced treatment of Internet Security read:
6915210Sphk#
7015027Sphk#	Firewalls & Internet Security
7115027Sphk#	Repelling the wily hacker
7215027Sphk#	William R. Cheswick, Steven M. Bellowin
7315027Sphk#
7415027Sphk#	Addison-Wesley
7573023Sdes#	ISBN 0-201-63357-4
7625478Sjkh#	http://www.awl.com/
7773023Sdes#	http://www.awlonline.com/product/0%2C2627%2C0201633574%2C00.html
7815027Sphk#
7915027Sphk
8091019Scjcsetup_loopback () {
8191019Scjc	############
8291019Scjc	# Only in rare cases do you want to change these rules
8391019Scjc	#
8491019Scjc	${fwcmd} add 100 pass all from any to any via lo0
8591019Scjc	${fwcmd} add 200 deny all from any to 127.0.0.0/8
8691019Scjc	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
8791019Scjc}
8891019Scjc
8951231Ssheldonhif [ -n "${1}" ]; then
9051231Ssheldonh	firewall_type="${1}"
9129300Sdannyfi
9229300Sdanny
9315027Sphk############
9429300Sdanny# Set quiet mode if requested
9551231Ssheldonh#
9651231Ssheldonhcase ${firewall_quiet} in
9751231Ssheldonh[Yy][Ee][Ss])
9829300Sdanny	fwcmd="/sbin/ipfw -q"
9951231Ssheldonh	;;
10051231Ssheldonh*)
10129300Sdanny	fwcmd="/sbin/ipfw"
10251231Ssheldonh	;;
10351231Ssheldonhesac
10429300Sdanny
10529300Sdanny############
10616578Salex# Flush out the list before we begin.
10751231Ssheldonh#
10850357Ssheldonh${fwcmd} -f flush
10916578Salex
11016578Salex############
11164244Sru# Network Address Translation.  All packets are passed to natd(8)
11264244Sru# before they encounter your remaining rules.  The firewall rules
11364244Sru# will then be run again on each packet after translation by natd
11464244Sru# starting at the rule number following the divert rule.
11551231Ssheldonh#
11664244Sru# For ``simple'' firewall type the divert rule should be put to a
11764244Sru# different place to not interfere with address-checking rules.
11873842Sobrien#
11964244Srucase ${firewall_type} in
12065257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
12164244Sru	case ${natd_enable} in
12264244Sru	[Yy][Ee][Ss])
12364244Sru		if [ -n "${natd_interface}" ]; then
12464244Sru			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
12564244Sru		fi
12664244Sru		;;
12764244Sru	esac
12851231Ssheldonhesac
12935267Sbrian
13035267Sbrian############
13115027Sphk# If you just configured ipfw in the kernel as a tool to solve network
13215027Sphk# problems or you just want to disallow some particular kinds of traffic
13351805Smpp# then you will want to change the default policy to open.  You can also
13417594Sjkh# do this as your only action by setting the firewall_type to ``open''.
13551231Ssheldonh#
13650357Ssheldonh# ${fwcmd} add 65000 pass all from any to any
13715027Sphk
13815027Sphk
13917594Sjkh# Prototype setups.
14051231Ssheldonh#
14151231Ssheldonhcase ${firewall_type} in
14251231Ssheldonh[Oo][Pp][Ee][Nn])
14391019Scjc	setup_loopback
14450357Ssheldonh	${fwcmd} add 65000 pass all from any to any
14551231Ssheldonh	;;
14654108Sobrien
14751231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
14851231Ssheldonh	############
14951231Ssheldonh	# This is a prototype setup that will protect your system somewhat
15051231Ssheldonh	# against people from outside your own network.
15151231Ssheldonh	############
15229300Sdanny
15351231Ssheldonh	# set these to your network and netmask and ip
15456736Srgrimes	net="192.0.2.0"
15551231Ssheldonh	mask="255.255.255.0"
15656736Srgrimes	ip="192.0.2.1"
15717594Sjkh
15891019Scjc	setup_loopback
15991019Scjc
16051231Ssheldonh	# Allow any traffic to or from my own net.
16151231Ssheldonh	${fwcmd} add pass all from ${ip} to ${net}:${mask}
16251231Ssheldonh	${fwcmd} add pass all from ${net}:${mask} to ${ip}
16315027Sphk
16451231Ssheldonh	# Allow TCP through if setup succeeded
16551231Ssheldonh	${fwcmd} add pass tcp from any to any established
16615027Sphk
16752873Sru	# Allow IP fragments to pass through
16852873Sru	${fwcmd} add pass all from any to any frag
16952873Sru
17051231Ssheldonh	# Allow setup of incoming email
17151231Ssheldonh	${fwcmd} add pass tcp from any to ${ip} 25 setup
17215027Sphk
17351231Ssheldonh	# Allow setup of outgoing TCP connections only
17451231Ssheldonh	${fwcmd} add pass tcp from ${ip} to any setup
17515027Sphk
17651231Ssheldonh	# Disallow setup of all other TCP connections
17751231Ssheldonh	${fwcmd} add deny tcp from any to any setup
17815027Sphk
17951231Ssheldonh	# Allow DNS queries out in the world
18072772Snsayer	${fwcmd} add pass udp from ${ip} to any 53 keep-state
18115027Sphk
18251231Ssheldonh	# Allow NTP queries out in the world
18372772Snsayer	${fwcmd} add pass udp from ${ip} to any 123 keep-state
18415027Sphk
18551231Ssheldonh	# Everything else is denied by default, unless the
18651231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
18751231Ssheldonh	# config file.
18851231Ssheldonh	;;
18915027Sphk
19051231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee])
19151231Ssheldonh	############
19251231Ssheldonh	# This is a prototype setup for a simple firewall.  Configure this
19351231Ssheldonh	# machine as a named server and ntp server, and point all the machines
19451231Ssheldonh	# on the inside at this machine for those services.
19551231Ssheldonh	############
19615027Sphk
19751231Ssheldonh	# set these to your outside interface network and netmask and ip
19851231Ssheldonh	oif="ed0"
19956736Srgrimes	onet="192.0.2.0"
20056736Srgrimes	omask="255.255.255.240"
20156736Srgrimes	oip="192.0.2.1"
20217594Sjkh
20351231Ssheldonh	# set these to your inside interface network and netmask and ip
20451231Ssheldonh	iif="ed1"
20556736Srgrimes	inet="192.0.2.16"
20656736Srgrimes	imask="255.255.255.240"
20756736Srgrimes	iip="192.0.2.17"
20815027Sphk
20991019Scjc	setup_loopback
21091019Scjc
21151231Ssheldonh	# Stop spoofing
21251231Ssheldonh	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
21351231Ssheldonh	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
21415027Sphk
21551231Ssheldonh	# Stop RFC1918 nets on the outside interface
21656736Srgrimes	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
21756736Srgrimes	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
21856736Srgrimes	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
21915027Sphk
22064028Sobrien	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
22164028Sobrien	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
22264028Sobrien	# on the outside interface
22356736Srgrimes	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
22456736Srgrimes	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
22556736Srgrimes	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
22656736Srgrimes	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
22756736Srgrimes	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
22856736Srgrimes
22964244Sru	# Network Address Translation.  This rule is placed here deliberately
23064244Sru	# so that it does not interfere with the surrounding address-checking
23164244Sru	# rules.  If for example one of your internal LAN machines had its IP
23264244Sru	# address set to 192.0.2.1 then an incoming packet for it after being
23364244Sru	# translated by natd(8) would match the `deny' rule above.  Similarly
23464244Sru	# an outgoing packet originated from it before being translated would
23564244Sru	# match the `deny' rule below.
23664244Sru	case ${natd_enable} in
23764244Sru	[Yy][Ee][Ss])
23864244Sru		if [ -n "${natd_interface}" ]; then
23964244Sru			${fwcmd} add divert natd all from any to any via ${natd_interface}
24064244Sru		fi
24164244Sru		;;
24264244Sru	esac
24364244Sru
24464244Sru	# Stop RFC1918 nets on the outside interface
24564244Sru	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
24664244Sru	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
24764244Sru	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
24864244Sru
24964244Sru	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
25064244Sru	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
25164244Sru	# on the outside interface
25264244Sru	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
25364244Sru	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
25464244Sru	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
25564244Sru	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
25664244Sru	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
25764244Sru
25851231Ssheldonh	# Allow TCP through if setup succeeded
25951231Ssheldonh	${fwcmd} add pass tcp from any to any established
26015027Sphk
26152873Sru	# Allow IP fragments to pass through
26252873Sru	${fwcmd} add pass all from any to any frag
26352873Sru
26451231Ssheldonh	# Allow setup of incoming email
26551231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 25 setup
26615027Sphk
26751231Ssheldonh	# Allow access to our DNS
26851231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 53 setup
26952404Sru	${fwcmd} add pass udp from any to ${oip} 53
27052404Sru	${fwcmd} add pass udp from ${oip} 53 to any
27115027Sphk
27251231Ssheldonh	# Allow access to our WWW
27351231Ssheldonh	${fwcmd} add pass tcp from any to ${oip} 80 setup
27415027Sphk
27551231Ssheldonh	# Reject&Log all setup of incoming connections from the outside
27651231Ssheldonh	${fwcmd} add deny log tcp from any to any in via ${oif} setup
27715027Sphk
27851231Ssheldonh	# Allow setup of any other TCP connection
27951231Ssheldonh	${fwcmd} add pass tcp from any to any setup
28015027Sphk
28151231Ssheldonh	# Allow DNS queries out in the world
28272772Snsayer	${fwcmd} add pass udp from ${oip} to any 53 keep-state
28315027Sphk
28451231Ssheldonh	# Allow NTP queries out in the world
28572772Snsayer	${fwcmd} add pass udp from ${oip} to any 123 keep-state
28615027Sphk
28751231Ssheldonh	# Everything else is denied by default, unless the
28851231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
28951231Ssheldonh	# config file.
29051231Ssheldonh	;;
29115027Sphk
29291019Scjc[Cc][Ll][Oo][Ss][Ee][Dd])
29391019Scjc	setup_loopback
29491019Scjc	;;
29551231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
29651231Ssheldonh	;;
29751231Ssheldonh*)
29859669Sbsd	if [ -r "${firewall_type}" ]; then
29957014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
30051231Ssheldonh	fi
30151231Ssheldonh	;;
30251231Ssheldonhesac
303