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