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: stable/11/etc/rc.firewall 331728 2018-03-29 04:51:07Z araujo $
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
49229783Suqs#   workstation - will try to protect just this machine using stateful
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
81238416Skevlosetup_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
94238416Skevlosetup_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
108229783Suqs	# Allow ICMPv6 destination unreachable
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
115331728Saraujo. /etc/rc.subr
116331728Saraujo. /etc/network.subr
117331728Saraujo
11851231Ssheldonhif [ -n "${1}" ]; then
11951231Ssheldonh	firewall_type="${1}"
12029300Sdannyfi
121331728Saraujoif [ -z "${firewall_rc_config_load}" ]; then
122331728Saraujo        load_rc_config ipfw
123331728Saraujoelse
124331728Saraujo        for i in ${firewall_rc_config_load}; do
125331728Saraujo                load_rc_config $i
126331728Saraujo        done
127331728Saraujofi
12829300Sdanny
129200028Sumeafexists inet6
130200028Sumeipv6_available=$?
131200028Sume
13215027Sphk############
13329300Sdanny# Set quiet mode if requested
13451231Ssheldonh#
13551231Ssheldonhcase ${firewall_quiet} in
13651231Ssheldonh[Yy][Ee][Ss])
13729300Sdanny	fwcmd="/sbin/ipfw -q"
13851231Ssheldonh	;;
13951231Ssheldonh*)
14029300Sdanny	fwcmd="/sbin/ipfw"
14151231Ssheldonh	;;
14251231Ssheldonhesac
14329300Sdanny
14429300Sdanny############
14516578Salex# Flush out the list before we begin.
14651231Ssheldonh#
14750357Ssheldonh${fwcmd} -f flush
14816578Salex
149163749Sphksetup_loopback
150200028Sumesetup_ipv6_mandatory
151163749Sphk
15216578Salex############
15364244Sru# Network Address Translation.  All packets are passed to natd(8)
15464244Sru# before they encounter your remaining rules.  The firewall rules
15564244Sru# will then be run again on each packet after translation by natd
15664244Sru# starting at the rule number following the divert rule.
15751231Ssheldonh#
15864244Sru# For ``simple'' firewall type the divert rule should be put to a
15964244Sru# different place to not interfere with address-checking rules.
16073842Sobrien#
16164244Srucase ${firewall_type} in
16265257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
16364244Sru	case ${natd_enable} in
16464244Sru	[Yy][Ee][Ss])
16564244Sru		if [ -n "${natd_interface}" ]; then
166152562Sume			${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
16764244Sru		fi
16864244Sru		;;
16964244Sru	esac
170165648Spiso	case ${firewall_nat_enable} in
171165648Spiso	[Yy][Ee][Ss])
172165648Spiso		if [ -n "${firewall_nat_interface}" ]; then
173175522Srafan			if echo "${firewall_nat_interface}" | \
174175522Srafan				grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then
175175522Srafan				firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}"
176175522Srafan			else
177175522Srafan				firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}"
178175522Srafan			fi
179175522Srafan			${fwcmd} nat 123 config log ${firewall_nat_flags}
180165648Spiso			${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
181165648Spiso		fi
182165648Spiso		;;
183165648Spiso	esac
18451231Ssheldonhesac
18535267Sbrian
18635267Sbrian############
18715027Sphk# If you just configured ipfw in the kernel as a tool to solve network
18815027Sphk# problems or you just want to disallow some particular kinds of traffic
18951805Smpp# then you will want to change the default policy to open.  You can also
19017594Sjkh# do this as your only action by setting the firewall_type to ``open''.
19151231Ssheldonh#
19250357Ssheldonh# ${fwcmd} add 65000 pass all from any to any
19315027Sphk
19415027Sphk
19517594Sjkh# Prototype setups.
19651231Ssheldonh#
19751231Ssheldonhcase ${firewall_type} in
19851231Ssheldonh[Oo][Pp][Ee][Nn])
19950357Ssheldonh	${fwcmd} add 65000 pass all from any to any
20051231Ssheldonh	;;
20154108Sobrien
20251231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt])
20351231Ssheldonh	############
20451231Ssheldonh	# This is a prototype setup that will protect your system somewhat
20551231Ssheldonh	# against people from outside your own network.
206181762Sjhb	#
207181762Sjhb	# Configuration:
208200028Sume	#  firewall_client_net:		Network address of local IPv4 network.
209200028Sume	#  firewall_client_net_ipv6:	Network address of local IPv6 network.
21051231Ssheldonh	############
21129300Sdanny
212181762Sjhb	# set this to your local network
213181762Sjhb	net="$firewall_client_net"
214200028Sume	net6="$firewall_client_net_ipv6"
21517594Sjkh
216179598Skeramida	# Allow limited broadcast traffic from my own net.
217181761Sjhb	${fwcmd} add pass all from ${net} to 255.255.255.255
218179598Skeramida
21951231Ssheldonh	# Allow any traffic to or from my own net.
220181761Sjhb	${fwcmd} add pass all from me to ${net}
221181761Sjhb	${fwcmd} add pass all from ${net} to me
222200028Sume	if [ -n "$net6" ]; then
223202460Sume		${fwcmd} add pass all from me to ${net6}
224202460Sume		${fwcmd} add pass all from ${net6} to me
225200028Sume	fi
22615027Sphk
227200028Sume	if [ -n "$net6" ]; then
228200028Sume		# Allow any link-local multicast traffic
229200028Sume		${fwcmd} add pass all from fe80::/10 to ff02::/16
230200028Sume		${fwcmd} add pass all from ${net6} to ff02::/16
231201930Sume		# Allow DHCPv6
232202460Sume		${fwcmd} add pass udp from fe80::/10 to me 546
233200028Sume	fi
234200028Sume
23551231Ssheldonh	# Allow TCP through if setup succeeded
23651231Ssheldonh	${fwcmd} add pass tcp from any to any established
23715027Sphk
23852873Sru	# Allow IP fragments to pass through
23952873Sru	${fwcmd} add pass all from any to any frag
24052873Sru
24151231Ssheldonh	# Allow setup of incoming email
242163749Sphk	${fwcmd} add pass tcp from any to me 25 setup
24315027Sphk
24451231Ssheldonh	# Allow setup of outgoing TCP connections only
245163749Sphk	${fwcmd} add pass tcp from me to any setup
24615027Sphk
24751231Ssheldonh	# Disallow setup of all other TCP connections
24851231Ssheldonh	${fwcmd} add deny tcp from any to any setup
24915027Sphk
25051231Ssheldonh	# Allow DNS queries out in the world
251163749Sphk	${fwcmd} add pass udp from me to any 53 keep-state
25215027Sphk
25351231Ssheldonh	# Allow NTP queries out in the world
254163749Sphk	${fwcmd} add pass udp from me to any 123 keep-state
25515027Sphk
25651231Ssheldonh	# Everything else is denied by default, unless the
25751231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
25851231Ssheldonh	# config file.
25951231Ssheldonh	;;
26015027Sphk
26151231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee])
26251231Ssheldonh	############
26351231Ssheldonh	# This is a prototype setup for a simple firewall.  Configure this
264121881Sru	# machine as a DNS and NTP server, and point all the machines
26551231Ssheldonh	# on the inside at this machine for those services.
266181762Sjhb	#
267181762Sjhb	# Configuration:
268200028Sume	#  firewall_simple_iif:		Inside IPv4 network interface.
269200028Sume	#  firewall_simple_inet:	Inside IPv4 network address.
270200028Sume	#  firewall_simple_oif:		Outside IPv4 network interface.
271200028Sume	#  firewall_simple_onet:	Outside IPv4 network address.
272200028Sume	#  firewall_simple_iif_ipv6:	Inside IPv6 network interface.
273200028Sume	#  firewall_simple_inet_ipv6:	Inside IPv6 network prefix.
274200028Sume	#  firewall_simple_oif_ipv6:	Outside IPv6 network interface.
275200028Sume	#  firewall_simple_onet_ipv6:	Outside IPv6 network prefix.
27651231Ssheldonh	############
277284691Sjulian	BAD_ADDR_TBL=13
27815027Sphk
279181761Sjhb	# set these to your outside interface network
280181762Sjhb	oif="$firewall_simple_oif"
281181762Sjhb	onet="$firewall_simple_onet"
282200028Sume	oif6="${firewall_simple_oif_ipv6:-$firewall_simple_oif}"
283200028Sume	onet6="$firewall_simple_onet_ipv6"
28417594Sjkh
285181761Sjhb	# set these to your inside interface network
286181762Sjhb	iif="$firewall_simple_iif"
287181762Sjhb	inet="$firewall_simple_inet"
288200028Sume	iif6="${firewall_simple_iif_ipv6:-$firewall_simple_iif}"
289200028Sume	inet6="$firewall_simple_inet_ipv6"
29015027Sphk
29151231Ssheldonh	# Stop spoofing
292181761Sjhb	${fwcmd} add deny all from ${inet} to any in via ${oif}
293181761Sjhb	${fwcmd} add deny all from ${onet} to any in via ${iif}
294200028Sume	if [ -n "$inet6" ]; then
295200028Sume		${fwcmd} add deny all from ${inet6} to any in via ${oif6}
296200028Sume		if [ -n "$onet6" ]; then
297200028Sume			${fwcmd} add deny all from ${onet6} to any in \
298200028Sume			    via ${iif6}
299200028Sume		fi
300200028Sume	fi
30115027Sphk
302284920Sjulian	# Define stuff we should never send out or receive in.
30351231Ssheldonh	# Stop RFC1918 nets on the outside interface
304284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} flush
305284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 10.0.0.0/8
306284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 172.16.0.0/12
307284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 192.168.0.0/16
30815027Sphk
309284920Sjulian	# And stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
31064028Sobrien	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
31164028Sobrien	# on the outside interface
312284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 0.0.0.0/8
313284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 169.254.0.0/16
314284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 192.0.2.0/24
315284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 224.0.0.0/4
316284691Sjulian	${fwcmd} table ${BAD_ADDR_TBL} add 240.0.0.0/4
31756736Srgrimes
318284691Sjulian	${fwcmd} add deny all from any to "table($BAD_ADDR_TBL)" via ${oif}
319284691Sjulian
32064244Sru	# Network Address Translation.  This rule is placed here deliberately
32164244Sru	# so that it does not interfere with the surrounding address-checking
32264244Sru	# rules.  If for example one of your internal LAN machines had its IP
32364244Sru	# address set to 192.0.2.1 then an incoming packet for it after being
32464244Sru	# translated by natd(8) would match the `deny' rule above.  Similarly
32564244Sru	# an outgoing packet originated from it before being translated would
32664244Sru	# match the `deny' rule below.
32764244Sru	case ${natd_enable} in
32864244Sru	[Yy][Ee][Ss])
32964244Sru		if [ -n "${natd_interface}" ]; then
330200028Sume			${fwcmd} add divert natd ip4 from any to any via ${natd_interface}
33164244Sru		fi
33264244Sru		;;
33364244Sru	esac
33464244Sru
335284691Sjulian	${fwcmd} add deny all from "table($BAD_ADDR_TBL)" to any via ${oif}
336200028Sume	if [ -n "$inet6" ]; then
337200028Sume		# Stop unique local unicast address on the outside interface
338200028Sume		${fwcmd} add deny all from fc00::/7 to any via ${oif6}
339200028Sume		${fwcmd} add deny all from any to fc00::/7 via ${oif6}
340200028Sume
341200028Sume		# Stop site-local on the outside interface
342200028Sume		${fwcmd} add deny all from fec0::/10 to any via ${oif6}
343200028Sume		${fwcmd} add deny all from any to fec0::/10 via ${oif6}
344200028Sume
345200028Sume		# Disallow "internal" addresses to appear on the wire.
346200028Sume		${fwcmd} add deny all from ::ffff:0.0.0.0/96 to any \
347200028Sume		    via ${oif6}
348200028Sume		${fwcmd} add deny all from any to ::ffff:0.0.0.0/96 \
349200028Sume		    via ${oif6}
350200028Sume
351200028Sume		# Disallow packets to malicious IPv4 compatible prefix.
352200028Sume		${fwcmd} add deny all from ::224.0.0.0/100 to any via ${oif6}
353200028Sume		${fwcmd} add deny all from any to ::224.0.0.0/100 via ${oif6}
354200028Sume		${fwcmd} add deny all from ::127.0.0.0/104 to any via ${oif6}
355200028Sume		${fwcmd} add deny all from any to ::127.0.0.0/104 via ${oif6}
356200028Sume		${fwcmd} add deny all from ::0.0.0.0/104 to any via ${oif6}
357200028Sume		${fwcmd} add deny all from any to ::0.0.0.0/104 via ${oif6}
358200028Sume		${fwcmd} add deny all from ::255.0.0.0/104 to any via ${oif6}
359200028Sume		${fwcmd} add deny all from any to ::255.0.0.0/104 via ${oif6}
360200028Sume
361200028Sume		${fwcmd} add deny all from ::0.0.0.0/96 to any via ${oif6}
362200028Sume		${fwcmd} add deny all from any to ::0.0.0.0/96 via ${oif6}
363200028Sume
364200028Sume		# Disallow packets to malicious 6to4 prefix.
365200028Sume		${fwcmd} add deny all from 2002:e000::/20 to any via ${oif6}
366200028Sume		${fwcmd} add deny all from any to 2002:e000::/20 via ${oif6}
367200028Sume		${fwcmd} add deny all from 2002:7f00::/24 to any via ${oif6}
368200028Sume		${fwcmd} add deny all from any to 2002:7f00::/24 via ${oif6}
369200028Sume		${fwcmd} add deny all from 2002:0000::/24 to any via ${oif6}
370200028Sume		${fwcmd} add deny all from any to 2002:0000::/24 via ${oif6}
371200028Sume		${fwcmd} add deny all from 2002:ff00::/24 to any via ${oif6}
372200028Sume		${fwcmd} add deny all from any to 2002:ff00::/24 via ${oif6}
373200028Sume
374200028Sume		${fwcmd} add deny all from 2002:0a00::/24 to any via ${oif6}
375200028Sume		${fwcmd} add deny all from any to 2002:0a00::/24 via ${oif6}
376200028Sume		${fwcmd} add deny all from 2002:ac10::/28 to any via ${oif6}
377200028Sume		${fwcmd} add deny all from any to 2002:ac10::/28 via ${oif6}
378200028Sume		${fwcmd} add deny all from 2002:c0a8::/32 to any via ${oif6}
379200028Sume		${fwcmd} add deny all from any to 2002:c0a8::/32 via ${oif6}
380200028Sume
381200028Sume		${fwcmd} add deny all from ff05::/16 to any via ${oif6}
382200028Sume		${fwcmd} add deny all from any to ff05::/16 via ${oif6}
383200028Sume	fi
384200028Sume
38551231Ssheldonh	# Allow TCP through if setup succeeded
38651231Ssheldonh	${fwcmd} add pass tcp from any to any established
38715027Sphk
38852873Sru	# Allow IP fragments to pass through
38952873Sru	${fwcmd} add pass all from any to any frag
39052873Sru
39151231Ssheldonh	# Allow setup of incoming email
392181760Sjhb	${fwcmd} add pass tcp from any to me 25 setup
39315027Sphk
39451231Ssheldonh	# Allow access to our DNS
395181760Sjhb	${fwcmd} add pass tcp from any to me 53 setup
396181760Sjhb	${fwcmd} add pass udp from any to me 53
397181760Sjhb	${fwcmd} add pass udp from me 53 to any
39815027Sphk
39951231Ssheldonh	# Allow access to our WWW
400181760Sjhb	${fwcmd} add pass tcp from any to me 80 setup
40115027Sphk
40251231Ssheldonh	# Reject&Log all setup of incoming connections from the outside
403200028Sume	${fwcmd} add deny log ip4 from any to any in via ${oif} setup proto tcp
404200028Sume	if [ -n "$inet6" ]; then
405200028Sume		${fwcmd} add deny log ip6 from any to any in via ${oif6} \
406200028Sume		    setup proto tcp
407200028Sume	fi
40815027Sphk
40951231Ssheldonh	# Allow setup of any other TCP connection
41051231Ssheldonh	${fwcmd} add pass tcp from any to any setup
41115027Sphk
41251231Ssheldonh	# Allow DNS queries out in the world
413181760Sjhb	${fwcmd} add pass udp from me to any 53 keep-state
41415027Sphk
41551231Ssheldonh	# Allow NTP queries out in the world
416181760Sjhb	${fwcmd} add pass udp from me to any 123 keep-state
41715027Sphk
41851231Ssheldonh	# Everything else is denied by default, unless the
41951231Ssheldonh	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
42051231Ssheldonh	# config file.
42151231Ssheldonh	;;
42215027Sphk
423163749Sphk[Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
424163749Sphk	# Configuration:
425273201Shrs	#  firewall_myservices:		List of ports/protocols on which this
426273201Shrs	#				 host offers services.
427206399Sume	#  firewall_allowservices:	List of IPv4 and/or IPv6 addresses
428206479Sume	#				 that have access to
429163749Sphk	#				 $firewall_myservices.
430206399Sume	#  firewall_trusted:		List of IPv4 and/or IPv6 addresses
431206479Sume	#				 that have full access to this host.
432206479Sume	#				 Be very careful when setting this.
433206399Sume	#				 This option can seriously degrade
434206399Sume	#				 the level of protection provided by
435206399Sume	#				 the firewall.
436163749Sphk	#  firewall_logdeny:		Boolean (YES/NO) specifying if the
437163749Sphk	#				 default denied packets should be
438163749Sphk	#				 logged (in /var/log/security).
439163749Sphk	#  firewall_nologports:		List of TCP/UDP ports for which
440229783Suqs	#				 denied incoming packets are not
441163749Sphk	#				 logged.
442200028Sume
443163749Sphk	# Allow packets for which a state has been built.
444163749Sphk	${fwcmd} add check-state
445163749Sphk
446163749Sphk	# For services permitted below.
447163749Sphk	${fwcmd} add pass tcp  from me to any established
448163749Sphk
449163749Sphk	# Allow any connection out, adding state for each.
450163749Sphk	${fwcmd} add pass tcp  from me to any setup keep-state
451163749Sphk	${fwcmd} add pass udp  from me to any       keep-state
452163749Sphk	${fwcmd} add pass icmp from me to any       keep-state
453200028Sume	if [ $ipv6_available -eq 0 ]; then
454202460Sume		${fwcmd} add pass ipv6-icmp from me to any keep-state
455200028Sume	fi
456163749Sphk
457163749Sphk	# Allow DHCP.
458163749Sphk	${fwcmd} add pass udp  from 0.0.0.0 68 to 255.255.255.255 67 out
459163749Sphk	${fwcmd} add pass udp  from any 67     to me 68 in
460163749Sphk	${fwcmd} add pass udp  from any 67     to 255.255.255.255 68 in
461200028Sume	if [ $ipv6_available -eq 0 ]; then
462202460Sume		${fwcmd} add pass udp from fe80::/10 to me 546 in
463200028Sume	fi
464208060Sdougb	# Some servers will ping the IP while trying to decide if it's
465163749Sphk	# still in use.
466163749Sphk	${fwcmd} add pass icmp from any to any icmptype 8
467200028Sume	if [ $ipv6_available -eq 0 ]; then
468200028Sume		${fwcmd} add pass ipv6-icmp from any to any icmp6type 128,129
469200028Sume	fi
470163749Sphk
471163749Sphk	# Allow "mandatory" ICMP in.
472163749Sphk	${fwcmd} add pass icmp from any to any icmptype 3,4,11
473201752Sume	if [ $ipv6_available -eq 0 ]; then
474201752Sume		${fwcmd} add pass ipv6-icmp from any to any icmp6type 3
475201752Sume	fi
476201752Sume
477163749Sphk	# Add permits for this workstations published services below
478163749Sphk	# Only IPs and nets in firewall_allowservices is allowed in.
479208060Sdougb	# If you really wish to let anyone use services on your
480163749Sphk	# workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
481163749Sphk	#
482163749Sphk	# Note: We don't use keep-state as that would allow DoS of
483208060Sdougb	#       our statetable.
484163749Sphk	#       You can add 'keep-state' to the lines for slightly
485163749Sphk	#       better performance if you fell that DoS of your
486163749Sphk	#       workstation won't be a problem.
487163749Sphk	#
488163749Sphk	for i in ${firewall_allowservices} ; do
489163749Sphk	  for j in ${firewall_myservices} ; do
490273201Shrs	    case $j in
491273201Shrs	    [0-9A-Za-z]*/[Pp][Rr][Oo][Tt][Oo])
492273201Shrs	      ${fwcmd} add pass ${j%/[Pp][Rr][Oo][Tt][Oo]} from $i to me
493273201Shrs	    ;;
494273201Shrs	    [0-9A-Za-z]*/[Tt][Cc][Pp])
495273201Shrs	      ${fwcmd} add pass tcp from $i to me ${j%/[Tt][Cc][Pp]}
496273201Shrs	    ;;
497273201Shrs	    [0-9A-Za-z]*/[Uu][Dd][Pp])
498273201Shrs	      ${fwcmd} add pass udp from $i to me ${j%/[Uu][Dd][Pp]}
499273201Shrs	    ;;
500273201Shrs	    *[0-9A-Za-z])
501273301Shrs	      echo "Consider using ${j}/tcp in firewall_myservices." \
502273301Shrs	        > /dev/stderr
503273201Shrs	      ${fwcmd} add pass tcp from $i to me $j
504273201Shrs	    ;;
505273201Shrs	    *)
506273201Shrs	      echo "Invalid port in firewall_myservices: $j" > /dev/stderr
507273201Shrs	    ;;
508273201Shrs	    esac
509163749Sphk	  done
510163749Sphk	done
511163749Sphk
512163749Sphk	# Allow all connections from trusted IPs.
513163749Sphk	# Playing with the content of firewall_trusted could seriously
514163749Sphk	# degrade the level of protection provided by the firewall.
515163749Sphk	for i in ${firewall_trusted} ; do
516163749Sphk	  ${fwcmd} add pass ip from $i to me
517163749Sphk	done
518200028Sume
519163749Sphk	${fwcmd} add 65000 count ip from any to any
520163749Sphk
521163749Sphk	# Drop packets to ports where we don't want logging
522163749Sphk	for i in ${firewall_nologports} ; do
523163749Sphk	  ${fwcmd} add deny { tcp or udp } from any to any $i in
524163749Sphk	done
525163749Sphk
526229783Suqs	# Broadcasts and multicasts
527163749Sphk	${fwcmd} add deny ip  from any to 255.255.255.255
528163749Sphk	${fwcmd} add deny ip  from any to 224.0.0.0/24 in	# XXX
529163749Sphk
530163749Sphk	# Noise from routers
531163749Sphk	${fwcmd} add deny udp from any to any 520 in
532163749Sphk
533163749Sphk	# Noise from webbrowsing.
534229783Suqs	# The stateful filter is a bit aggressive, and will cause some
535163749Sphk	#  connection teardowns to be logged.
536163749Sphk	${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
537163749Sphk
538163749Sphk	# Deny and (if wanted) log the rest unconditionally.
539163749Sphk	log=""
540163749Sphk	if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
541163749Sphk	  log="log logamount 500"	# The default of 100 is too low.
542163749Sphk	  sysctl net.inet.ip.fw.verbose=1 >/dev/null
543163749Sphk	fi
544163749Sphk	${fwcmd} add deny $log ip from any to any
545163749Sphk	;;
546163749Sphk
54791019Scjc[Cc][Ll][Oo][Ss][Ee][Dd])
548163749Sphk	${fwcmd} add 65000 deny ip from any to any
54991019Scjc	;;
55051231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
55151231Ssheldonh	;;
55251231Ssheldonh*)
55359669Sbsd	if [ -r "${firewall_type}" ]; then
55457014Spaul		${fwcmd} ${firewall_flags} ${firewall_type}
55551231Ssheldonh	fi
55651231Ssheldonh	;;
55751231Ssheldonhesac
558