xref: /freebsd/libexec/rc/rc.firewall (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
1#!/bin/sh -
2# Copyright (c) 1996  Poul-Henning Kamp
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27#
28
29#
30# Setup system for ipfw(4) firewall service.
31#
32
33# Suck in the configuration variables.
34if [ -z "${source_rc_confs_defined}" ]; then
35	if [ -r /etc/defaults/rc.conf ]; then
36		. /etc/defaults/rc.conf
37		source_rc_confs
38	elif [ -r /etc/rc.conf ]; then
39		. /etc/rc.conf
40	fi
41fi
42
43############
44# Define the firewall type in /etc/rc.conf.  Valid values are:
45#   open        - will allow anyone in
46#   client      - will try to protect just this machine
47#   simple      - will try to protect a whole network
48#   closed      - totally disables IP services except via lo0 interface
49#   workstation - will try to protect just this machine using stateful
50#		  firewalling. See below for rc.conf variables used
51#   UNKNOWN     - disables the loading of firewall rules.
52#   filename    - will load the rules in the given filename (full path required)
53#
54# For ``client'' and ``simple'' the entries below should be customized
55# appropriately.
56
57############
58#
59# If you don't know enough about packet filtering, we suggest that you
60# take time to read this book:
61#
62#	Building Internet Firewalls, 2nd Edition
63#	Brent Chapman and Elizabeth Zwicky
64#
65#	O'Reilly & Associates, Inc
66#	ISBN 1-56592-871-7
67#	http://www.ora.com/
68#	http://www.oreilly.com/catalog/fire2/
69#
70# For a more advanced treatment of Internet Security read:
71#
72#	Firewalls and Internet Security: Repelling the Wily Hacker, 2nd Edition
73#	William R. Cheswick, Steven M. Bellowin, Aviel D. Rubin
74#
75#	Addison-Wesley / Prentice Hall
76#	ISBN 0-201-63466-X
77#	http://www.pearsonhighered.com/
78#	http://www.pearsonhighered.com/educator/academic/product/0,3110,020163466X,00.html
79#
80
81setup_loopback() {
82	############
83	# Only in rare cases do you want to change these rules
84	#
85	${fwcmd} add 100 pass all from any to any via lo0
86	${fwcmd} add 200 deny all from any to 127.0.0.0/8
87	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
88	if [ $ipv6_available -eq 0 ]; then
89		${fwcmd} add 400 deny all from any to ::1
90		${fwcmd} add 500 deny all from ::1 to any
91	fi
92}
93
94setup_ipv6_mandatory() {
95	[ $ipv6_available -eq 0 ] || return 0
96
97	############
98	# Only in rare cases do you want to change these rules
99	#
100	# ND
101	#
102	# DAD
103	${fwcmd} add pass ipv6-icmp from :: to ff02::/16
104	# RS, RA, NS, NA, redirect...
105	${fwcmd} add pass ipv6-icmp from fe80::/10 to fe80::/10
106	${fwcmd} add pass ipv6-icmp from fe80::/10 to ff02::/16
107
108	# Allow ICMPv6 destination unreachable
109	${fwcmd} add pass ipv6-icmp from any to any icmp6types 1
110
111	# Allow NS/NA/toobig (don't filter it out)
112	${fwcmd} add pass ipv6-icmp from any to any icmp6types 2,135,136
113}
114
115. /etc/rc.subr
116. /etc/network.subr
117
118if [ -n "${1}" ]; then
119	firewall_type="${1}"
120fi
121if [ -z "${firewall_rc_config_load}" ]; then
122        load_rc_config ipfw
123else
124        for i in ${firewall_rc_config_load}; do
125                load_rc_config $i
126        done
127fi
128
129afexists inet6
130ipv6_available=$?
131
132############
133# Set quiet mode if requested
134#
135case ${firewall_quiet} in
136[Yy][Ee][Ss])
137	fwcmd="/sbin/ipfw -q"
138	;;
139*)
140	fwcmd="/sbin/ipfw"
141	;;
142esac
143
144############
145# Flush out the list before we begin.
146#
147${fwcmd} -f flush
148
149setup_loopback
150setup_ipv6_mandatory
151
152############
153# Network Address Translation.  All packets are passed to natd(8)
154# before they encounter your remaining rules.  The firewall rules
155# will then be run again on each packet after translation by natd
156# starting at the rule number following the divert rule.
157#
158# For ``simple'' firewall type the divert rule should be put to a
159# different place to not interfere with address-checking rules.
160#
161case ${firewall_type} in
162[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
163	case ${natd_enable} in
164	[Yy][Ee][Ss])
165		if [ -n "${natd_interface}" ]; then
166			${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
167		fi
168		;;
169	esac
170	case ${firewall_nat_enable} in
171	[Yy][Ee][Ss])
172		if [ -n "${firewall_nat_interface}" ]; then
173			if echo "${firewall_nat_interface}" | \
174				grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then
175				firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}"
176			else
177				firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}"
178			fi
179			${fwcmd} nat 123 config log ${firewall_nat_flags}
180			${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
181		fi
182		;;
183	esac
184esac
185
186############
187# If you just configured ipfw in the kernel as a tool to solve network
188# problems or you just want to disallow some particular kinds of traffic
189# then you will want to change the default policy to open.  You can also
190# do this as your only action by setting the firewall_type to ``open''.
191#
192# ${fwcmd} add 65000 pass all from any to any
193
194
195# Prototype setups.
196#
197case ${firewall_type} in
198[Oo][Pp][Ee][Nn])
199	${fwcmd} add 65000 pass all from any to any
200	;;
201
202[Cc][Ll][Ii][Ee][Nn][Tt])
203	############
204	# This is a prototype setup that will protect your system somewhat
205	# against people from outside your own network.
206	#
207	# Configuration:
208	#  firewall_client_net:		Network address of local IPv4 network.
209	#  firewall_client_net_ipv6:	Network address of local IPv6 network.
210	############
211
212	# set this to your local network
213	net="$firewall_client_net"
214	net6="$firewall_client_net_ipv6"
215
216	# Allow limited broadcast traffic from my own net.
217	${fwcmd} add pass all from ${net} to 255.255.255.255
218
219	# Allow any traffic to or from my own net.
220	${fwcmd} add pass all from me to ${net}
221	${fwcmd} add pass all from ${net} to me
222	if [ -n "$net6" ]; then
223		${fwcmd} add pass all from me to ${net6}
224		${fwcmd} add pass all from ${net6} to me
225		# Allow any link-local multicast traffic
226		${fwcmd} add pass all from fe80::/10 to ff02::/16
227		${fwcmd} add pass all from ${net6} to ff02::/16
228		# Allow DHCPv6
229		${fwcmd} add pass udp from fe80::/10 to me 546
230	fi
231
232	# Allow TCP through if setup succeeded
233	${fwcmd} add pass tcp from any to any established
234
235	# Allow IP fragments to pass through
236	${fwcmd} add pass all from any to any frag
237
238	# Allow setup of incoming email
239	${fwcmd} add pass tcp from any to me 25 setup
240
241	# Allow setup of outgoing TCP connections only
242	${fwcmd} add pass tcp from me to any setup
243
244	# Disallow setup of all other TCP connections
245	${fwcmd} add deny tcp from any to any setup
246
247	# Allow DNS queries out in the world
248	${fwcmd} add pass udp from me to any 53 keep-state
249
250	# Allow NTP queries out in the world
251	${fwcmd} add pass udp from me to any 123 keep-state
252
253	# Everything else is denied by default, unless the
254	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
255	# config file.
256	;;
257
258[Ss][Ii][Mm][Pp][Ll][Ee])
259	############
260	# This is a prototype setup for a simple firewall.  Configure this
261	# machine as a DNS and NTP server, and point all the machines
262	# on the inside at this machine for those services.
263	#
264	# Configuration:
265	#  firewall_simple_iif:		Inside IPv4 network interface.
266	#  firewall_simple_inet:	Inside IPv4 network address.
267	#  firewall_simple_oif:		Outside IPv4 network interface.
268	#  firewall_simple_onet:	Outside IPv4 network address.
269	#  firewall_simple_iif_ipv6:	Inside IPv6 network interface.
270	#  firewall_simple_inet_ipv6:	Inside IPv6 network prefix.
271	#  firewall_simple_oif_ipv6:	Outside IPv6 network interface.
272	#  firewall_simple_onet_ipv6:	Outside IPv6 network prefix.
273	############
274	BAD_ADDR_TBL=13
275
276	# set these to your outside interface network
277	oif="$firewall_simple_oif"
278	onet="$firewall_simple_onet"
279	oif6="${firewall_simple_oif_ipv6:-$firewall_simple_oif}"
280	onet6="$firewall_simple_onet_ipv6"
281
282	# set these to your inside interface network
283	iif="$firewall_simple_iif"
284	inet="$firewall_simple_inet"
285	iif6="${firewall_simple_iif_ipv6:-$firewall_simple_iif}"
286	inet6="$firewall_simple_inet_ipv6"
287
288	# Stop spoofing
289	${fwcmd} add deny all from ${inet} to any in via ${oif}
290	${fwcmd} add deny all from ${onet} to any in via ${iif}
291	if [ -n "$inet6" ]; then
292		${fwcmd} add deny all from ${inet6} to any in via ${oif6}
293		if [ -n "$onet6" ]; then
294			${fwcmd} add deny all from ${onet6} to any in \
295			    via ${iif6}
296		fi
297	fi
298
299	# Define stuff we should never send out or receive in.
300	# Stop RFC1918 nets on the outside interface
301	${fwcmd} table ${BAD_ADDR_TBL} flush
302	${fwcmd} table ${BAD_ADDR_TBL} add 10.0.0.0/8
303	${fwcmd} table ${BAD_ADDR_TBL} add 172.16.0.0/12
304	${fwcmd} table ${BAD_ADDR_TBL} add 192.168.0.0/16
305
306	# And stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
307	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
308	# on the outside interface
309	${fwcmd} table ${BAD_ADDR_TBL} add 0.0.0.0/8
310	${fwcmd} table ${BAD_ADDR_TBL} add 169.254.0.0/16
311	${fwcmd} table ${BAD_ADDR_TBL} add 192.0.2.0/24
312	${fwcmd} table ${BAD_ADDR_TBL} add 224.0.0.0/4
313	${fwcmd} table ${BAD_ADDR_TBL} add 240.0.0.0/4
314
315	${fwcmd} add deny all from any to "table($BAD_ADDR_TBL)" via ${oif}
316
317	# Network Address Translation.  This rule is placed here deliberately
318	# so that it does not interfere with the surrounding address-checking
319	# rules.  If for example one of your internal LAN machines had its IP
320	# address set to 192.0.2.1 then an incoming packet for it after being
321	# translated by natd(8) would match the `deny' rule above.  Similarly
322	# an outgoing packet originated from it before being translated would
323	# match the `deny' rule below.
324	case ${natd_enable} in
325	[Yy][Ee][Ss])
326		if [ -n "${natd_interface}" ]; then
327			${fwcmd} add divert natd ip4 from any to any via ${natd_interface}
328		fi
329		;;
330	esac
331
332	${fwcmd} add deny all from "table($BAD_ADDR_TBL)" to any via ${oif}
333	if [ -n "$inet6" ]; then
334		# Stop unique local unicast address on the outside interface
335		${fwcmd} add deny all from fc00::/7 to any via ${oif6}
336		${fwcmd} add deny all from any to fc00::/7 via ${oif6}
337
338		# Stop site-local on the outside interface
339		${fwcmd} add deny all from fec0::/10 to any via ${oif6}
340		${fwcmd} add deny all from any to fec0::/10 via ${oif6}
341
342		# Disallow "internal" addresses to appear on the wire.
343		${fwcmd} add deny all from ::ffff:0.0.0.0/96 to any \
344		    via ${oif6}
345		${fwcmd} add deny all from any to ::ffff:0.0.0.0/96 \
346		    via ${oif6}
347
348		# Disallow packets to malicious IPv4 compatible prefix.
349		${fwcmd} add deny all from ::224.0.0.0/100 to any via ${oif6}
350		${fwcmd} add deny all from any to ::224.0.0.0/100 via ${oif6}
351		${fwcmd} add deny all from ::127.0.0.0/104 to any via ${oif6}
352		${fwcmd} add deny all from any to ::127.0.0.0/104 via ${oif6}
353		${fwcmd} add deny all from ::0.0.0.0/104 to any via ${oif6}
354		${fwcmd} add deny all from any to ::0.0.0.0/104 via ${oif6}
355		${fwcmd} add deny all from ::255.0.0.0/104 to any via ${oif6}
356		${fwcmd} add deny all from any to ::255.0.0.0/104 via ${oif6}
357
358		${fwcmd} add deny all from ::0.0.0.0/96 to any via ${oif6}
359		${fwcmd} add deny all from any to ::0.0.0.0/96 via ${oif6}
360
361		# Disallow packets to malicious 6to4 prefix.
362		${fwcmd} add deny all from 2002:e000::/20 to any via ${oif6}
363		${fwcmd} add deny all from any to 2002:e000::/20 via ${oif6}
364		${fwcmd} add deny all from 2002:7f00::/24 to any via ${oif6}
365		${fwcmd} add deny all from any to 2002:7f00::/24 via ${oif6}
366		${fwcmd} add deny all from 2002:0000::/24 to any via ${oif6}
367		${fwcmd} add deny all from any to 2002:0000::/24 via ${oif6}
368		${fwcmd} add deny all from 2002:ff00::/24 to any via ${oif6}
369		${fwcmd} add deny all from any to 2002:ff00::/24 via ${oif6}
370
371		${fwcmd} add deny all from 2002:0a00::/24 to any via ${oif6}
372		${fwcmd} add deny all from any to 2002:0a00::/24 via ${oif6}
373		${fwcmd} add deny all from 2002:ac10::/28 to any via ${oif6}
374		${fwcmd} add deny all from any to 2002:ac10::/28 via ${oif6}
375		${fwcmd} add deny all from 2002:c0a8::/32 to any via ${oif6}
376		${fwcmd} add deny all from any to 2002:c0a8::/32 via ${oif6}
377
378		${fwcmd} add deny all from ff05::/16 to any via ${oif6}
379		${fwcmd} add deny all from any to ff05::/16 via ${oif6}
380	fi
381
382	# Allow TCP through if setup succeeded
383	${fwcmd} add pass tcp from any to any established
384
385	# Allow IP fragments to pass through
386	${fwcmd} add pass all from any to any frag
387
388	# Allow setup of incoming email
389	${fwcmd} add pass tcp from any to me 25 setup
390
391	# Allow access to our DNS
392	${fwcmd} add pass tcp from any to me 53 setup
393	${fwcmd} add pass udp from any to me 53
394	${fwcmd} add pass udp from me 53 to any
395
396	# Allow access to our WWW
397	${fwcmd} add pass tcp from any to me 80 setup
398
399	# Reject&Log all setup of incoming connections from the outside
400	${fwcmd} add deny log ip4 from any to any in via ${oif} setup proto tcp
401	if [ -n "$inet6" ]; then
402		${fwcmd} add deny log ip6 from any to any in via ${oif6} \
403		    setup proto tcp
404	fi
405
406	# Allow setup of any other TCP connection
407	${fwcmd} add pass tcp from any to any setup
408
409	# Allow DNS queries out in the world
410	${fwcmd} add pass udp from me to any 53 keep-state
411
412	# Allow NTP queries out in the world
413	${fwcmd} add pass udp from me to any 123 keep-state
414
415	# Everything else is denied by default, unless the
416	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
417	# config file.
418	;;
419
420[Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
421	# Configuration:
422	#  firewall_myservices:		List of ports/protocols on which this
423	#				 host offers services.
424	#  firewall_allowservices:	List of IPv4 and/or IPv6 addresses
425	#				 that have access to
426	#				 $firewall_myservices.
427	#  firewall_trusted:		List of IPv4 and/or IPv6 addresses
428	#				 that have full access to this host.
429	#				 Be very careful when setting this.
430	#				 This option can seriously degrade
431	#				 the level of protection provided by
432	#				 the firewall.
433	#  firewall_logdeny:		Boolean (YES/NO) specifying if the
434	#				 default denied packets should be
435	#				 logged (in /var/log/security).
436	#  firewall_nologports:		List of TCP/UDP ports for which
437	#				 denied incoming packets are not
438	#				 logged.
439
440	# Allow packets for which a state has been built.
441	${fwcmd} add check-state
442
443	# For services permitted below.
444	${fwcmd} add pass tcp  from me to any established
445
446	# Allow any connection out, adding state for each.
447	${fwcmd} add pass tcp  from me to any setup keep-state
448	${fwcmd} add pass udp  from me to any       keep-state
449	${fwcmd} add pass icmp from me to any       keep-state
450	if [ $ipv6_available -eq 0 ]; then
451		${fwcmd} add pass ipv6-icmp from me to any keep-state
452	fi
453
454	# Allow DHCP.
455	${fwcmd} add pass udp  from 0.0.0.0 68 to 255.255.255.255 67 out
456	${fwcmd} add pass udp  from any 67     to me 68 in
457	${fwcmd} add pass udp  from any 67     to 255.255.255.255 68 in
458	if [ $ipv6_available -eq 0 ]; then
459		${fwcmd} add pass udp from fe80::/10 to me 546 in
460	fi
461	# Some servers will ping the IP while trying to decide if it's
462	# still in use.
463	${fwcmd} add pass icmp from any to any icmptype 8
464	if [ $ipv6_available -eq 0 ]; then
465		${fwcmd} add pass ipv6-icmp from any to any icmp6type 128,129
466	fi
467
468	# Allow "mandatory" ICMP in.
469	${fwcmd} add pass icmp from any to any icmptype 3,4,11
470	if [ $ipv6_available -eq 0 ]; then
471		${fwcmd} add pass ipv6-icmp from any to any icmp6type 3
472	fi
473
474	# Add permits for this workstations published services below
475	# Only IPs and nets in firewall_allowservices is allowed in.
476	# If you really wish to let anyone use services on your
477	# workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
478	#
479	# Note: We don't use keep-state as that would allow DoS of
480	#       our statetable.
481	#       You can add 'keep-state' to the lines for slightly
482	#       better performance if you fell that DoS of your
483	#       workstation won't be a problem.
484	#
485	for i in ${firewall_allowservices} ; do
486	  for j in ${firewall_myservices} ; do
487	    case $j in
488	    [0-9A-Za-z]*/[Pp][Rr][Oo][Tt][Oo])
489	      ${fwcmd} add pass ${j%/[Pp][Rr][Oo][Tt][Oo]} from $i to me
490	    ;;
491	    [0-9A-Za-z]*/[Tt][Cc][Pp])
492	      ${fwcmd} add pass tcp from $i to me ${j%/[Tt][Cc][Pp]}
493	    ;;
494	    [0-9A-Za-z]*/[Uu][Dd][Pp])
495	      ${fwcmd} add pass udp from $i to me ${j%/[Uu][Dd][Pp]}
496	    ;;
497	    *[0-9A-Za-z])
498	      echo "Consider using ${j}/tcp in firewall_myservices." \
499	        > /dev/stderr
500	      ${fwcmd} add pass tcp from $i to me $j
501	    ;;
502	    *)
503	      echo "Invalid port in firewall_myservices: $j" > /dev/stderr
504	    ;;
505	    esac
506	  done
507	done
508
509	# Allow all connections from trusted IPs.
510	# Playing with the content of firewall_trusted could seriously
511	# degrade the level of protection provided by the firewall.
512	for i in ${firewall_trusted} ; do
513	  ${fwcmd} add pass ip from $i to me
514	done
515
516	${fwcmd} add 65000 count ip from any to any
517
518	# Drop packets to ports where we don't want logging
519	for i in ${firewall_nologports} ; do
520	  ${fwcmd} add deny { tcp or udp } from any to any $i in
521	done
522
523	# Broadcasts and multicasts
524	${fwcmd} add deny ip  from any to 255.255.255.255
525	${fwcmd} add deny ip  from any to 224.0.0.0/24 in	# XXX
526
527	# Noise from routers
528	${fwcmd} add deny udp from any to any 520 in
529
530	# Noise from webbrowsing.
531	# The stateful filter is a bit aggressive, and will cause some
532	#  connection teardowns to be logged.
533	${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
534
535	# Deny and (if wanted) log the rest unconditionally.
536	log=""
537	if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
538	  log="log logamount 500"	# The default of 100 is too low.
539	  sysctl net.inet.ip.fw.verbose=1 >/dev/null
540	fi
541	${fwcmd} add deny $log ip from any to any
542	;;
543
544[Cc][Ll][Oo][Ss][Ee][Dd])
545	${fwcmd} add 65000 deny ip from any to any
546	;;
547[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
548	;;
549*)
550	if [ -r "${firewall_type}" ]; then
551		${fwcmd} ${firewall_flags} ${firewall_type}
552	fi
553	;;
554esac
555