rc.firewall revision 201930
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 201930 2010-01-09 19:16:27Z ume $
2766830Sobrien#
2815027Sphk
2966830Sobrien#
30168384Srwatson# Setup system for ipfw(4) 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#
72175244Smaxim#	Firewalls and Internet Security: Repelling the Wily Hacker, 2nd Edition
73175244Smaxim#	William R. Cheswick, Steven M. Bellowin, Aviel D. Rubin
7415027Sphk#
75175244Smaxim#	Addison-Wesley / Prentice Hall
76175244Smaxim#	ISBN 0-201-63466-X
77175244Smaxim#	http://www.pearsonhighered.com/
78175244Smaxim#	http://www.pearsonhighered.com/educator/academic/product/0,3110,020163466X,00.html
7915027Sphk#
8015027Sphk
8191019Scjcsetup_loopback () {
8291019Scjc	############
8391019Scjc	# Only in rare cases do you want to change these rules
8491019Scjc	#
8591019Scjc	${fwcmd} add 100 pass all from any to any via lo0
8691019Scjc	${fwcmd} add 200 deny all from any to 127.0.0.0/8
8791019Scjc	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
88200028Sume	if [ $ipv6_available -eq 0 ]; then
89200028Sume		${fwcmd} add 400 deny all from any to ::1
90200028Sume		${fwcmd} add 500 deny all from ::1 to any
91200028Sume	fi
9291019Scjc}
9391019Scjc
94200028Sumesetup_ipv6_mandatory () {
95200028Sume	[ $ipv6_available -eq 0 ] || return 0
96200028Sume
97200028Sume	############
98200028Sume	# Only in rare cases do you want to change these rules
99200028Sume	#
100200028Sume	# ND
101200028Sume	#
102200028Sume	# DAD
103200028Sume	${fwcmd} add pass ipv6-icmp from :: to ff02::/16
104200028Sume	# RS, RA, NS, NA, redirect...
105200028Sume	${fwcmd} add pass ipv6-icmp from fe80::/10 to fe80::/10
106200028Sume	${fwcmd} add pass ipv6-icmp from fe80::/10 to ff02::/16
107200028Sume
108200028Sume	# Allow ICMPv6 destination unreach
109200028Sume	${fwcmd} add pass ipv6-icmp from any to any icmp6types 1
110200028Sume
111200028Sume	# Allow NS/NA/toobig (don't filter it out)
112200028Sume	${fwcmd} add pass ipv6-icmp from any to any icmp6types 2,135,136
113200028Sume}
114200028Sume
11551231Ssheldonhif [ -n "${1}" ]; then
11651231Ssheldonh	firewall_type="${1}"
11729300Sdannyfi
11829300Sdanny
119200028Sume. /etc/rc.subr
120200028Sume. /etc/network.subr
121200028Sumeafexists inet6
122200028Sumeipv6_available=$?
123200028Sume
12415027Sphk############
12529300Sdanny# Set quiet mode if requested
12651231Ssheldonh#
12751231Ssheldonhcase ${firewall_quiet} in
12851231Ssheldonh[Yy][Ee][Ss])
12929300Sdanny	fwcmd="/sbin/ipfw -q"
13051231Ssheldonh	;;
13151231Ssheldonh*)
13229300Sdanny	fwcmd="/sbin/ipfw"
13351231Ssheldonh	;;
13451231Ssheldonhesac
13529300Sdanny
13629300Sdanny############
13716578Salex# Flush out the list before we begin.
13851231Ssheldonh#
13950357Ssheldonh${fwcmd} -f flush
14016578Salex
141163749Sphksetup_loopback
142200028Sumesetup_ipv6_mandatory
143163749Sphk
14416578Salex############
14564244Sru# Network Address Translation.  All packets are passed to natd(8)
14664244Sru# before they encounter your remaining rules.  The firewall rules
14764244Sru# will then be run again on each packet after translation by natd
14864244Sru# starting at the rule number following the divert rule.
14951231Ssheldonh#
15064244Sru# For ``simple'' firewall type the divert rule should be put to a
15164244Sru# different place to not interfere with address-checking rules.
15273842Sobrien#
15364244Srucase ${firewall_type} in
15465257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
15564244Sru	case ${natd_enable} in
15664244Sru	[Yy][Ee][Ss])
15764244Sru		if [ -n "${natd_interface}" ]; then
158152562Sume			${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
15964244Sru		fi
16064244Sru		;;
16164244Sru	esac
162165648Spiso	case ${firewall_nat_enable} in
163165648Spiso	[Yy][Ee][Ss])
164165648Spiso		if [ -n "${firewall_nat_interface}" ]; then
165175522Srafan			if echo "${firewall_nat_interface}" | \
166175522Srafan				grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then
167175522Srafan				firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}"
168175522Srafan			else
169175522Srafan				firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}"
170175522Srafan			fi
171175522Srafan			${fwcmd} nat 123 config log ${firewall_nat_flags}
172165648Spiso			${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
173165648Spiso		fi
174165648Spiso		;;
175165648Spiso	esac
17651231Ssheldonhesac
17735267Sbrian
17835267Sbrian############
17915027Sphk# If you just configured ipfw in the kernel as a tool to solve network
18015027Sphk# problems or you just want to disallow some particular kinds of traffic
18151805Smpp# then you will want to change the default policy to open.  You can also
18217594Sjkh# do this as your only action by setting the firewall_type to ``open''.
18351231Ssheldonh#
18450357Ssheldonh# ${fwcmd} add 65000 pass all from any to any
18515027Sphk
18615027Sphk
18717594Sjkh# Prototype setups.
18851231Ssheldonh#
18951231Ssheldonhcase ${firewall_type} in
19051231Ssheldonh[Oo][Pp][Ee][Nn])
19150357Ssheldonh	${fwcmd} add 65000 pass all from any to any
19251231Ssheldonh	;;
19354108Sobrien
19451231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
19551231Ssheldonh	############
19651231Ssheldonh	# This is a prototype setup that will protect your system somewhat
19751231Ssheldonh	# against people from outside your own network.
198181762Sjhb	#
199181762Sjhb	# Configuration:
200200028Sume	#  firewall_client_net:		Network address of local IPv4 network.
201200028Sume	#  firewall_client_net_ipv6:	Network address of local IPv6 network.
20251231Ssheldonh	############
20329300Sdanny
204181762Sjhb	# set this to your local network
205181762Sjhb	net="$firewall_client_net"
206200028Sume	net6="$firewall_client_net_ipv6"
20717594Sjkh
208179598Skeramida	# Allow limited broadcast traffic from my own net.
209181761Sjhb	${fwcmd} add pass all from ${net} to 255.255.255.255
210179598Skeramida
21151231Ssheldonh	# Allow any traffic to or from my own net.
212181761Sjhb	${fwcmd} add pass all from me to ${net}
213181761Sjhb	${fwcmd} add pass all from ${net} to me
214200028Sume	if [ -n "$net6" ]; then
215200028Sume		${fwcmd} add pass all from me6 to ${net6}
216200028Sume		${fwcmd} add pass all from ${net6} to me6
217200028Sume	fi
21815027Sphk
219200028Sume	if [ -n "$net6" ]; then
220200028Sume		# Allow any link-local multicast traffic
221200028Sume		${fwcmd} add pass all from fe80::/10 to ff02::/16
222200028Sume		${fwcmd} add pass all from ${net6} to ff02::/16
223201930Sume		# Allow DHCPv6
224201930Sume		${fwcmd} add pass udp from fe80::/10 to me6 546
225200028Sume	fi
226200028Sume
22751231Ssheldonh	# Allow TCP through if setup succeeded
22851231Ssheldonh	${fwcmd} add pass tcp from any to any established
22915027Sphk
23052873Sru	# Allow IP fragments to pass through
23152873Sru	${fwcmd} add pass all from any to any frag
23252873Sru
23351231Ssheldonh	# Allow setup of incoming email
234163749Sphk	${fwcmd} add pass tcp from any to me 25 setup
235201193Sume	if [ -n "$net6" ]; then
236201193Sume		${fwcmd} add pass tcp from any to me6 25 setup
237201193Sume	fi
23815027Sphk
23951231Ssheldonh	# Allow setup of outgoing TCP connections only
240163749Sphk	${fwcmd} add pass tcp from me to any setup
241201193Sume	if [ -n "$net6" ]; then
242201193Sume		${fwcmd} add pass tcp from me6 to any setup
243201193Sume	fi
24415027Sphk
24551231Ssheldonh	# Disallow setup of all other TCP connections
24651231Ssheldonh	${fwcmd} add deny tcp from any to any setup
24715027Sphk
24851231Ssheldonh	# Allow DNS queries out in the world
249163749Sphk	${fwcmd} add pass udp from me to any 53 keep-state
250201193Sume	if [ -n "$net6" ]; then
251201193Sume		${fwcmd} add pass udp from me6 to any 53 keep-state
252201193Sume	fi
25315027Sphk
25451231Ssheldonh	# Allow NTP queries out in the world
255163749Sphk	${fwcmd} add pass udp from me to any 123 keep-state
256201193Sume	if [ -n "$net6" ]; then
257201193Sume		${fwcmd} add pass udp from me6 to any 123 keep-state
258201193Sume	fi
25915027Sphk
26051231Ssheldonh	# Everything else is denied by default, unless the
26151231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
26251231Ssheldonh	# config file.
26351231Ssheldonh	;;
26415027Sphk
26551231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee])
26651231Ssheldonh	############
26751231Ssheldonh	# This is a prototype setup for a simple firewall.  Configure this
268121881Sru	# machine as a DNS and NTP server, and point all the machines
26951231Ssheldonh	# on the inside at this machine for those services.
270181762Sjhb	#
271181762Sjhb	# Configuration:
272200028Sume	#  firewall_simple_iif:		Inside IPv4 network interface.
273200028Sume	#  firewall_simple_inet:	Inside IPv4 network address.
274200028Sume	#  firewall_simple_oif:		Outside IPv4 network interface.
275200028Sume	#  firewall_simple_onet:	Outside IPv4 network address.
276200028Sume	#  firewall_simple_iif_ipv6:	Inside IPv6 network interface.
277200028Sume	#  firewall_simple_inet_ipv6:	Inside IPv6 network prefix.
278200028Sume	#  firewall_simple_oif_ipv6:	Outside IPv6 network interface.
279200028Sume	#  firewall_simple_onet_ipv6:	Outside IPv6 network prefix.
28051231Ssheldonh	############
28115027Sphk
282181761Sjhb	# set these to your outside interface network
283181762Sjhb	oif="$firewall_simple_oif"
284181762Sjhb	onet="$firewall_simple_onet"
285200028Sume	oif6="${firewall_simple_oif_ipv6:-$firewall_simple_oif}"
286200028Sume	onet6="$firewall_simple_onet_ipv6"
28717594Sjkh
288181761Sjhb	# set these to your inside interface network
289181762Sjhb	iif="$firewall_simple_iif"
290181762Sjhb	inet="$firewall_simple_inet"
291200028Sume	iif6="${firewall_simple_iif_ipv6:-$firewall_simple_iif}"
292200028Sume	inet6="$firewall_simple_inet_ipv6"
29315027Sphk
29451231Ssheldonh	# Stop spoofing
295181761Sjhb	${fwcmd} add deny all from ${inet} to any in via ${oif}
296181761Sjhb	${fwcmd} add deny all from ${onet} to any in via ${iif}
297200028Sume	if [ -n "$inet6" ]; then
298200028Sume		${fwcmd} add deny all from ${inet6} to any in via ${oif6}
299200028Sume		if [ -n "$onet6" ]; then
300200028Sume			${fwcmd} add deny all from ${onet6} to any in \
301200028Sume			    via ${iif6}
302200028Sume		fi
303200028Sume	fi
30415027Sphk
30551231Ssheldonh	# Stop RFC1918 nets on the outside interface
30656736Srgrimes	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
30756736Srgrimes	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
30856736Srgrimes	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
30915027Sphk
31064028Sobrien	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
31164028Sobrien	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
31264028Sobrien	# on the outside interface
31356736Srgrimes	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
31456736Srgrimes	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
31556736Srgrimes	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
31656736Srgrimes	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
31756736Srgrimes	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
31856736Srgrimes
31964244Sru	# Network Address Translation.  This rule is placed here deliberately
32064244Sru	# so that it does not interfere with the surrounding address-checking
32164244Sru	# rules.  If for example one of your internal LAN machines had its IP
32264244Sru	# address set to 192.0.2.1 then an incoming packet for it after being
32364244Sru	# translated by natd(8) would match the `deny' rule above.  Similarly
32464244Sru	# an outgoing packet originated from it before being translated would
32564244Sru	# match the `deny' rule below.
32664244Sru	case ${natd_enable} in
32764244Sru	[Yy][Ee][Ss])
32864244Sru		if [ -n "${natd_interface}" ]; then
329200028Sume			${fwcmd} add divert natd ip4 from any to any via ${natd_interface}
33064244Sru		fi
33164244Sru		;;
33264244Sru	esac
33364244Sru
33464244Sru	# Stop RFC1918 nets on the outside interface
33564244Sru	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
33664244Sru	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
33764244Sru	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
33864244Sru
33964244Sru	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
34064244Sru	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
34164244Sru	# on the outside interface
34264244Sru	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
34364244Sru	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
34464244Sru	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
34564244Sru	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
34664244Sru	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
34764244Sru
348200028Sume	if [ -n "$inet6" ]; then
349200028Sume		# Stop unique local unicast address on the outside interface
350200028Sume		${fwcmd} add deny all from fc00::/7 to any via ${oif6}
351200028Sume		${fwcmd} add deny all from any to fc00::/7 via ${oif6}
352200028Sume
353200028Sume		# Stop site-local on the outside interface
354200028Sume		${fwcmd} add deny all from fec0::/10 to any via ${oif6}
355200028Sume		${fwcmd} add deny all from any to fec0::/10 via ${oif6}
356200028Sume
357200028Sume		# Disallow "internal" addresses to appear on the wire.
358200028Sume		${fwcmd} add deny all from ::ffff:0.0.0.0/96 to any \
359200028Sume		    via ${oif6}
360200028Sume		${fwcmd} add deny all from any to ::ffff:0.0.0.0/96 \
361200028Sume		    via ${oif6}
362200028Sume
363200028Sume		# Disallow packets to malicious IPv4 compatible prefix.
364200028Sume		${fwcmd} add deny all from ::224.0.0.0/100 to any via ${oif6}
365200028Sume		${fwcmd} add deny all from any to ::224.0.0.0/100 via ${oif6}
366200028Sume		${fwcmd} add deny all from ::127.0.0.0/104 to any via ${oif6}
367200028Sume		${fwcmd} add deny all from any to ::127.0.0.0/104 via ${oif6}
368200028Sume		${fwcmd} add deny all from ::0.0.0.0/104 to any via ${oif6}
369200028Sume		${fwcmd} add deny all from any to ::0.0.0.0/104 via ${oif6}
370200028Sume		${fwcmd} add deny all from ::255.0.0.0/104 to any via ${oif6}
371200028Sume		${fwcmd} add deny all from any to ::255.0.0.0/104 via ${oif6}
372200028Sume
373200028Sume		${fwcmd} add deny all from ::0.0.0.0/96 to any via ${oif6}
374200028Sume		${fwcmd} add deny all from any to ::0.0.0.0/96 via ${oif6}
375200028Sume
376200028Sume		# Disallow packets to malicious 6to4 prefix.
377200028Sume		${fwcmd} add deny all from 2002:e000::/20 to any via ${oif6}
378200028Sume		${fwcmd} add deny all from any to 2002:e000::/20 via ${oif6}
379200028Sume		${fwcmd} add deny all from 2002:7f00::/24 to any via ${oif6}
380200028Sume		${fwcmd} add deny all from any to 2002:7f00::/24 via ${oif6}
381200028Sume		${fwcmd} add deny all from 2002:0000::/24 to any via ${oif6}
382200028Sume		${fwcmd} add deny all from any to 2002:0000::/24 via ${oif6}
383200028Sume		${fwcmd} add deny all from 2002:ff00::/24 to any via ${oif6}
384200028Sume		${fwcmd} add deny all from any to 2002:ff00::/24 via ${oif6}
385200028Sume
386200028Sume		${fwcmd} add deny all from 2002:0a00::/24 to any via ${oif6}
387200028Sume		${fwcmd} add deny all from any to 2002:0a00::/24 via ${oif6}
388200028Sume		${fwcmd} add deny all from 2002:ac10::/28 to any via ${oif6}
389200028Sume		${fwcmd} add deny all from any to 2002:ac10::/28 via ${oif6}
390200028Sume		${fwcmd} add deny all from 2002:c0a8::/32 to any via ${oif6}
391200028Sume		${fwcmd} add deny all from any to 2002:c0a8::/32 via ${oif6}
392200028Sume
393200028Sume		${fwcmd} add deny all from ff05::/16 to any via ${oif6}
394200028Sume		${fwcmd} add deny all from any to ff05::/16 via ${oif6}
395200028Sume	fi
396200028Sume
39751231Ssheldonh	# Allow TCP through if setup succeeded
39851231Ssheldonh	${fwcmd} add pass tcp from any to any established
39915027Sphk
40052873Sru	# Allow IP fragments to pass through
40152873Sru	${fwcmd} add pass all from any to any frag
40252873Sru
40351231Ssheldonh	# Allow setup of incoming email
404181760Sjhb	${fwcmd} add pass tcp from any to me 25 setup
405201193Sume	if [ -n "$inet6" ]; then
406201193Sume		${fwcmd} add pass tcp from any to me6 25 setup
407201193Sume	fi
40815027Sphk
40951231Ssheldonh	# Allow access to our DNS
410181760Sjhb	${fwcmd} add pass tcp from any to me 53 setup
411181760Sjhb	${fwcmd} add pass udp from any to me 53
412181760Sjhb	${fwcmd} add pass udp from me 53 to any
413201193Sume	if [ -n "$inet6" ]; then
414201193Sume		${fwcmd} add pass tcp from any to me6 53 setup
415201193Sume		${fwcmd} add pass udp from any to me6 53
416201193Sume		${fwcmd} add pass udp from me6 53 to any
417201193Sume	fi
41815027Sphk
41951231Ssheldonh	# Allow access to our WWW
420181760Sjhb	${fwcmd} add pass tcp from any to me 80 setup
421201193Sume	if [ -n "$inet6" ]; then
422201193Sume		${fwcmd} add pass tcp from any to me6 80 setup
423201193Sume	fi
42415027Sphk
42551231Ssheldonh	# Reject&Log all setup of incoming connections from the outside
426200028Sume	${fwcmd} add deny log ip4 from any to any in via ${oif} setup proto tcp
427200028Sume	if [ -n "$inet6" ]; then
428200028Sume		${fwcmd} add deny log ip6 from any to any in via ${oif6} \
429200028Sume		    setup proto tcp
430200028Sume	fi
43115027Sphk
43251231Ssheldonh	# Allow setup of any other TCP connection
43351231Ssheldonh	${fwcmd} add pass tcp from any to any setup
43415027Sphk
43551231Ssheldonh	# Allow DNS queries out in the world
436181760Sjhb	${fwcmd} add pass udp from me to any 53 keep-state
437201193Sume	if [ -n "$inet6" ]; then
438201193Sume		${fwcmd} add pass udp from me6 to any 53 keep-state
439201193Sume	fi
44015027Sphk
44151231Ssheldonh	# Allow NTP queries out in the world
442181760Sjhb	${fwcmd} add pass udp from me to any 123 keep-state
443201193Sume	if [ -n "$inet6" ]; then
444201193Sume		${fwcmd} add pass udp from me6 to any 123 keep-state
445201193Sume	fi
44615027Sphk
44751231Ssheldonh	# Everything else is denied by default, unless the
44851231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
44951231Ssheldonh	# config file.
45051231Ssheldonh	;;
45115027Sphk
452163749Sphk[Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
453163749Sphk	# Configuration:
454163749Sphk	#  firewall_myservices:		List of TCP ports on which this host
455163749Sphk	#			 	 offers services.
456163749Sphk	#  firewall_allowservices:	List of IPs which has access to
457163749Sphk	#				 $firewall_myservices.
458200028Sume	#  firewall_trusted:		List of IPv4s which has full access 
459163749Sphk	#				 to this host. Be very carefull 
460163749Sphk	#				 when setting this. This option can
461163749Sphk	#				 seriously degrade the level of 
462163749Sphk	#				 protection provided by the firewall.
463163749Sphk	#  firewall_logdeny:		Boolean (YES/NO) specifying if the
464163749Sphk	#				 default denied packets should be
465163749Sphk	#				 logged (in /var/log/security).
466163749Sphk	#  firewall_nologports:		List of TCP/UDP ports for which
467163749Sphk	#				 denied incomming packets are not
468163749Sphk	#				 logged.
469200028Sume	#  firewall_trusted_ipv6:	List of IPv6s which has full access 
470200028Sume	#				 to this host. Be very carefull 
471200028Sume	#				 when setting this. This option can
472200028Sume	#				 seriously degrade the level of 
473200028Sume	#				 protection provided by the firewall.
474200028Sume
475163749Sphk	# Allow packets for which a state has been built.
476163749Sphk	${fwcmd} add check-state
477163749Sphk
478163749Sphk	# For services permitted below.
479163749Sphk	${fwcmd} add pass tcp  from me to any established
480200028Sume	if [ $ipv6_available -eq 0 ]; then
481200028Sume		${fwcmd} add pass tcp from me6 to any established
482200028Sume	fi
483163749Sphk
484163749Sphk	# Allow any connection out, adding state for each.
485163749Sphk	${fwcmd} add pass tcp  from me to any setup keep-state
486163749Sphk	${fwcmd} add pass udp  from me to any       keep-state
487163749Sphk	${fwcmd} add pass icmp from me to any       keep-state
488200028Sume	if [ $ipv6_available -eq 0 ]; then
489200028Sume		${fwcmd} add pass tcp from me6 to any setup keep-state
490200028Sume		${fwcmd} add pass udp from me6 to any keep-state
491200028Sume		${fwcmd} add pass ipv6-icmp from me6 to any keep-state
492200028Sume	fi
493163749Sphk
494163749Sphk	# Allow DHCP.
495163749Sphk	${fwcmd} add pass udp  from 0.0.0.0 68 to 255.255.255.255 67 out
496163749Sphk	${fwcmd} add pass udp  from any 67     to me 68 in
497163749Sphk	${fwcmd} add pass udp  from any 67     to 255.255.255.255 68 in
498200028Sume	if [ $ipv6_available -eq 0 ]; then
499200028Sume		${fwcmd} add pass udp from fe80::/10 to me6 546 in
500200028Sume	fi
501163749Sphk	# Some servers will ping the IP while trying to decide if it's 
502163749Sphk	# still in use.
503163749Sphk	${fwcmd} add pass icmp from any to any icmptype 8
504200028Sume	if [ $ipv6_available -eq 0 ]; then
505200028Sume		${fwcmd} add pass ipv6-icmp from any to any icmp6type 128,129
506200028Sume	fi
507163749Sphk
508163749Sphk	# Allow "mandatory" ICMP in.
509163749Sphk	${fwcmd} add pass icmp from any to any icmptype 3,4,11
510201752Sume	if [ $ipv6_available -eq 0 ]; then
511201752Sume		${fwcmd} add pass ipv6-icmp from any to any icmp6type 3
512201752Sume	fi
513201752Sume
514163749Sphk	# Add permits for this workstations published services below
515163749Sphk	# Only IPs and nets in firewall_allowservices is allowed in.
516163749Sphk	# If you really wish to let anyone use services on your 
517163749Sphk	# workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
518163749Sphk	#
519163749Sphk	# Note: We don't use keep-state as that would allow DoS of
520163749Sphk	#       our statetable. 
521163749Sphk	#       You can add 'keep-state' to the lines for slightly
522163749Sphk	#       better performance if you fell that DoS of your
523163749Sphk	#       workstation won't be a problem.
524163749Sphk	#
525163749Sphk	for i in ${firewall_allowservices} ; do
526163749Sphk	  for j in ${firewall_myservices} ; do
527163749Sphk	    ${fwcmd} add pass tcp from $i to me $j
528200028Sume	    if [ $ipv6_available -eq 0 ]; then
529200028Sume	      ${fwcmd} add pass tcp from $i to me6 $j
530200028Sume	    fi
531163749Sphk	  done
532163749Sphk	done
533163749Sphk
534163749Sphk	# Allow all connections from trusted IPs.
535163749Sphk	# Playing with the content of firewall_trusted could seriously
536163749Sphk	# degrade the level of protection provided by the firewall.
537163749Sphk	for i in ${firewall_trusted} ; do
538163749Sphk	  ${fwcmd} add pass ip from $i to me
539163749Sphk	done
540200028Sume	for i in ${firewall_trusted_ipv6} ; do
541200028Sume	  ${fwcmd} add pass all from $i to me6
542200028Sume	done
543200028Sume
544163749Sphk	${fwcmd} add 65000 count ip from any to any
545163749Sphk
546163749Sphk	# Drop packets to ports where we don't want logging
547163749Sphk	for i in ${firewall_nologports} ; do
548163749Sphk	  ${fwcmd} add deny { tcp or udp } from any to any $i in
549163749Sphk	done
550163749Sphk
551163749Sphk	# Broadcasts and muticasts
552163749Sphk	${fwcmd} add deny ip  from any to 255.255.255.255
553163749Sphk	${fwcmd} add deny ip  from any to 224.0.0.0/24 in	# XXX
554163749Sphk
555163749Sphk	# Noise from routers
556163749Sphk	${fwcmd} add deny udp from any to any 520 in
557163749Sphk
558163749Sphk	# Noise from webbrowsing.
559163749Sphk	# The statefull filter is a bit agressive, and will cause some
560163749Sphk	#  connection teardowns to be logged.
561163749Sphk	${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
562163749Sphk
563163749Sphk	# Deny and (if wanted) log the rest unconditionally.
564163749Sphk	log=""
565163749Sphk	if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
566163749Sphk	  log="log logamount 500"	# The default of 100 is too low.
567163749Sphk	  sysctl net.inet.ip.fw.verbose=1 >/dev/null
568163749Sphk	fi
569163749Sphk	${fwcmd} add deny $log ip from any to any
570163749Sphk	;;
571163749Sphk
57291019Scjc[Cc][Ll][Oo][Ss][Ee][Dd])
573163749Sphk	${fwcmd} add 65000 deny ip from any to any
57491019Scjc	;;
57551231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
57651231Ssheldonh	;;
57751231Ssheldonh*)
57859669Sbsd	if [ -r "${firewall_type}" ]; then
57957014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
58051231Ssheldonh	fi
58151231Ssheldonh	;;
58251231Ssheldonhesac
583