2 # Copyright (c) 1996 Poul-Henning Kamp
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
30 # Setup system for ipfw(4) firewall service.
33 # Suck in the configuration variables.
34 if [ -z "${source_rc_confs_defined}" ]; then
35 if [ -r /etc/defaults/rc.conf ]; then
36 . /etc/defaults/rc.conf
38 elif [ -r /etc/rc.conf ]; then
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 statefull
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)
54 # For ``client'' and ``simple'' the entries below should be customized
59 # If you don't know enough about packet filtering, we suggest that you
60 # take time to read this book:
62 # Building Internet Firewalls, 2nd Edition
63 # Brent Chapman and Elizabeth Zwicky
65 # O'Reilly & Associates, Inc
68 # http://www.oreilly.com/catalog/fire2/
70 # For a more advanced treatment of Internet Security read:
72 # Firewalls and Internet Security: Repelling the Wily Hacker, 2nd Edition
73 # William R. Cheswick, Steven M. Bellowin, Aviel D. Rubin
75 # Addison-Wesley / Prentice Hall
77 # http://www.pearsonhighered.com/
78 # http://www.pearsonhighered.com/educator/academic/product/0,3110,020163466X,00.html
83 # Only in rare cases do you want to change these rules
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
94 setup_ipv6_mandatory () {
95 [ $ipv6_available -eq 0 ] || return 0
98 # Only in rare cases do you want to change these rules
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
108 # Allow ICMPv6 destination unreach
109 ${fwcmd} add pass ipv6-icmp from any to any icmp6types 1
111 # Allow NS/NA/toobig (don't filter it out)
112 ${fwcmd} add pass ipv6-icmp from any to any icmp6types 2,135,136
115 if [ -n "${1}" ]; then
125 # Set quiet mode if requested
127 case ${firewall_quiet} in
129 fwcmd="/sbin/ipfw -q"
137 # Flush out the list before we begin.
145 # Network Address Translation. All packets are passed to natd(8)
146 # before they encounter your remaining rules. The firewall rules
147 # will then be run again on each packet after translation by natd
148 # starting at the rule number following the divert rule.
150 # For ``simple'' firewall type the divert rule should be put to a
151 # different place to not interfere with address-checking rules.
153 case ${firewall_type} in
154 [Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
155 case ${natd_enable} in
157 if [ -n "${natd_interface}" ]; then
158 ${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
162 case ${firewall_nat_enable} in
164 if [ -n "${firewall_nat_interface}" ]; then
165 if echo "${firewall_nat_interface}" | \
166 grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then
167 firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}"
169 firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}"
171 ${fwcmd} nat 123 config log ${firewall_nat_flags}
172 ${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
179 # If you just configured ipfw in the kernel as a tool to solve network
180 # problems or you just want to disallow some particular kinds of traffic
181 # then you will want to change the default policy to open. You can also
182 # do this as your only action by setting the firewall_type to ``open''.
184 # ${fwcmd} add 65000 pass all from any to any
189 case ${firewall_type} in
191 ${fwcmd} add 65000 pass all from any to any
194 [Cc][Ll][Ii][Ee][Nn][Tt])
196 # This is a prototype setup that will protect your system somewhat
197 # against people from outside your own network.
200 # firewall_client_net: Network address of local IPv4 network.
201 # firewall_client_net_ipv6: Network address of local IPv6 network.
204 # set this to your local network
205 net="$firewall_client_net"
206 net6="$firewall_client_net_ipv6"
208 # Allow limited broadcast traffic from my own net.
209 ${fwcmd} add pass all from ${net} to 255.255.255.255
211 # Allow any traffic to or from my own net.
212 ${fwcmd} add pass all from me to ${net}
213 ${fwcmd} add pass all from ${net} to me
214 if [ -n "$net6" ]; then
215 ${fwcmd} add pass all from me6 to ${net6}
216 ${fwcmd} add pass all from ${net6} to me6
219 if [ -n "$net6" ]; then
220 # Allow any link-local multicast traffic
221 ${fwcmd} add pass all from fe80::/10 to ff02::/16
222 ${fwcmd} add pass all from ${net6} to ff02::/16
224 ${fwcmd} add pass udp from fe80::/10 to me6 546
227 # Allow TCP through if setup succeeded
228 ${fwcmd} add pass tcp from any to any established
230 # Allow IP fragments to pass through
231 ${fwcmd} add pass all from any to any frag
233 # Allow setup of incoming email
234 ${fwcmd} add pass tcp from any to me 25 setup
235 if [ -n "$net6" ]; then
236 ${fwcmd} add pass tcp from any to me6 25 setup
239 # Allow setup of outgoing TCP connections only
240 ${fwcmd} add pass tcp from me to any setup
241 if [ -n "$net6" ]; then
242 ${fwcmd} add pass tcp from me6 to any setup
245 # Disallow setup of all other TCP connections
246 ${fwcmd} add deny tcp from any to any setup
248 # Allow DNS queries out in the world
249 ${fwcmd} add pass udp from me to any 53 keep-state
250 if [ -n "$net6" ]; then
251 ${fwcmd} add pass udp from me6 to any 53 keep-state
254 # Allow NTP queries out in the world
255 ${fwcmd} add pass udp from me to any 123 keep-state
256 if [ -n "$net6" ]; then
257 ${fwcmd} add pass udp from me6 to any 123 keep-state
260 # Everything else is denied by default, unless the
261 # IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
265 [Ss][Ii][Mm][Pp][Ll][Ee])
267 # This is a prototype setup for a simple firewall. Configure this
268 # machine as a DNS and NTP server, and point all the machines
269 # on the inside at this machine for those services.
272 # firewall_simple_iif: Inside IPv4 network interface.
273 # firewall_simple_inet: Inside IPv4 network address.
274 # firewall_simple_oif: Outside IPv4 network interface.
275 # firewall_simple_onet: Outside IPv4 network address.
276 # firewall_simple_iif_ipv6: Inside IPv6 network interface.
277 # firewall_simple_inet_ipv6: Inside IPv6 network prefix.
278 # firewall_simple_oif_ipv6: Outside IPv6 network interface.
279 # firewall_simple_onet_ipv6: Outside IPv6 network prefix.
282 # set these to your outside interface network
283 oif="$firewall_simple_oif"
284 onet="$firewall_simple_onet"
285 oif6="${firewall_simple_oif_ipv6:-$firewall_simple_oif}"
286 onet6="$firewall_simple_onet_ipv6"
288 # set these to your inside interface network
289 iif="$firewall_simple_iif"
290 inet="$firewall_simple_inet"
291 iif6="${firewall_simple_iif_ipv6:-$firewall_simple_iif}"
292 inet6="$firewall_simple_inet_ipv6"
295 ${fwcmd} add deny all from ${inet} to any in via ${oif}
296 ${fwcmd} add deny all from ${onet} to any in via ${iif}
297 if [ -n "$inet6" ]; then
298 ${fwcmd} add deny all from ${inet6} to any in via ${oif6}
299 if [ -n "$onet6" ]; then
300 ${fwcmd} add deny all from ${onet6} to any in \
305 # Stop RFC1918 nets on the outside interface
306 ${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
307 ${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
308 ${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
310 # Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
311 # DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
312 # on the outside interface
313 ${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
314 ${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
315 ${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
316 ${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
317 ${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
319 # Network Address Translation. This rule is placed here deliberately
320 # so that it does not interfere with the surrounding address-checking
321 # rules. If for example one of your internal LAN machines had its IP
322 # address set to 192.0.2.1 then an incoming packet for it after being
323 # translated by natd(8) would match the `deny' rule above. Similarly
324 # an outgoing packet originated from it before being translated would
325 # match the `deny' rule below.
326 case ${natd_enable} in
328 if [ -n "${natd_interface}" ]; then
329 ${fwcmd} add divert natd ip4 from any to any via ${natd_interface}
334 # Stop RFC1918 nets on the outside interface
335 ${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
336 ${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
337 ${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
339 # Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
340 # DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
341 # on the outside interface
342 ${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
343 ${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
344 ${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
345 ${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
346 ${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
348 if [ -n "$inet6" ]; then
349 # Stop unique local unicast address on the outside interface
350 ${fwcmd} add deny all from fc00::/7 to any via ${oif6}
351 ${fwcmd} add deny all from any to fc00::/7 via ${oif6}
353 # Stop site-local on the outside interface
354 ${fwcmd} add deny all from fec0::/10 to any via ${oif6}
355 ${fwcmd} add deny all from any to fec0::/10 via ${oif6}
357 # Disallow "internal" addresses to appear on the wire.
358 ${fwcmd} add deny all from ::ffff:0.0.0.0/96 to any \
360 ${fwcmd} add deny all from any to ::ffff:0.0.0.0/96 \
363 # Disallow packets to malicious IPv4 compatible prefix.
364 ${fwcmd} add deny all from ::224.0.0.0/100 to any via ${oif6}
365 ${fwcmd} add deny all from any to ::224.0.0.0/100 via ${oif6}
366 ${fwcmd} add deny all from ::127.0.0.0/104 to any via ${oif6}
367 ${fwcmd} add deny all from any to ::127.0.0.0/104 via ${oif6}
368 ${fwcmd} add deny all from ::0.0.0.0/104 to any via ${oif6}
369 ${fwcmd} add deny all from any to ::0.0.0.0/104 via ${oif6}
370 ${fwcmd} add deny all from ::255.0.0.0/104 to any via ${oif6}
371 ${fwcmd} add deny all from any to ::255.0.0.0/104 via ${oif6}
373 ${fwcmd} add deny all from ::0.0.0.0/96 to any via ${oif6}
374 ${fwcmd} add deny all from any to ::0.0.0.0/96 via ${oif6}
376 # Disallow packets to malicious 6to4 prefix.
377 ${fwcmd} add deny all from 2002:e000::/20 to any via ${oif6}
378 ${fwcmd} add deny all from any to 2002:e000::/20 via ${oif6}
379 ${fwcmd} add deny all from 2002:7f00::/24 to any via ${oif6}
380 ${fwcmd} add deny all from any to 2002:7f00::/24 via ${oif6}
381 ${fwcmd} add deny all from 2002:0000::/24 to any via ${oif6}
382 ${fwcmd} add deny all from any to 2002:0000::/24 via ${oif6}
383 ${fwcmd} add deny all from 2002:ff00::/24 to any via ${oif6}
384 ${fwcmd} add deny all from any to 2002:ff00::/24 via ${oif6}
386 ${fwcmd} add deny all from 2002:0a00::/24 to any via ${oif6}
387 ${fwcmd} add deny all from any to 2002:0a00::/24 via ${oif6}
388 ${fwcmd} add deny all from 2002:ac10::/28 to any via ${oif6}
389 ${fwcmd} add deny all from any to 2002:ac10::/28 via ${oif6}
390 ${fwcmd} add deny all from 2002:c0a8::/32 to any via ${oif6}
391 ${fwcmd} add deny all from any to 2002:c0a8::/32 via ${oif6}
393 ${fwcmd} add deny all from ff05::/16 to any via ${oif6}
394 ${fwcmd} add deny all from any to ff05::/16 via ${oif6}
397 # Allow TCP through if setup succeeded
398 ${fwcmd} add pass tcp from any to any established
400 # Allow IP fragments to pass through
401 ${fwcmd} add pass all from any to any frag
403 # Allow setup of incoming email
404 ${fwcmd} add pass tcp from any to me 25 setup
405 if [ -n "$inet6" ]; then
406 ${fwcmd} add pass tcp from any to me6 25 setup
409 # Allow access to our DNS
410 ${fwcmd} add pass tcp from any to me 53 setup
411 ${fwcmd} add pass udp from any to me 53
412 ${fwcmd} add pass udp from me 53 to any
413 if [ -n "$inet6" ]; then
414 ${fwcmd} add pass tcp from any to me6 53 setup
415 ${fwcmd} add pass udp from any to me6 53
416 ${fwcmd} add pass udp from me6 53 to any
419 # Allow access to our WWW
420 ${fwcmd} add pass tcp from any to me 80 setup
421 if [ -n "$inet6" ]; then
422 ${fwcmd} add pass tcp from any to me6 80 setup
425 # Reject&Log all setup of incoming connections from the outside
426 ${fwcmd} add deny log ip4 from any to any in via ${oif} setup proto tcp
427 if [ -n "$inet6" ]; then
428 ${fwcmd} add deny log ip6 from any to any in via ${oif6} \
432 # Allow setup of any other TCP connection
433 ${fwcmd} add pass tcp from any to any setup
435 # Allow DNS queries out in the world
436 ${fwcmd} add pass udp from me to any 53 keep-state
437 if [ -n "$inet6" ]; then
438 ${fwcmd} add pass udp from me6 to any 53 keep-state
441 # Allow NTP queries out in the world
442 ${fwcmd} add pass udp from me to any 123 keep-state
443 if [ -n "$inet6" ]; then
444 ${fwcmd} add pass udp from me6 to any 123 keep-state
447 # Everything else is denied by default, unless the
448 # IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
452 [Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
454 # firewall_myservices: List of TCP ports on which this host
456 # firewall_allowservices: List of IPs which has access to
457 # $firewall_myservices.
458 # firewall_trusted: List of IPv4s which has full access
459 # to this host. Be very carefull
460 # when setting this. This option can
461 # seriously degrade the level of
462 # protection provided by the firewall.
463 # firewall_logdeny: Boolean (YES/NO) specifying if the
464 # default denied packets should be
465 # logged (in /var/log/security).
466 # firewall_nologports: List of TCP/UDP ports for which
467 # denied incomming packets are not
469 # firewall_trusted_ipv6: List of IPv6s which has full access
470 # to this host. Be very carefull
471 # when setting this. This option can
472 # seriously degrade the level of
473 # protection provided by the firewall.
475 # Allow packets for which a state has been built.
476 ${fwcmd} add check-state
478 # For services permitted below.
479 ${fwcmd} add pass tcp from me to any established
480 if [ $ipv6_available -eq 0 ]; then
481 ${fwcmd} add pass tcp from me6 to any established
484 # Allow any connection out, adding state for each.
485 ${fwcmd} add pass tcp from me to any setup keep-state
486 ${fwcmd} add pass udp from me to any keep-state
487 ${fwcmd} add pass icmp from me to any keep-state
488 if [ $ipv6_available -eq 0 ]; then
489 ${fwcmd} add pass tcp from me6 to any setup keep-state
490 ${fwcmd} add pass udp from me6 to any keep-state
491 ${fwcmd} add pass ipv6-icmp from me6 to any keep-state
495 ${fwcmd} add pass udp from 0.0.0.0 68 to 255.255.255.255 67 out
496 ${fwcmd} add pass udp from any 67 to me 68 in
497 ${fwcmd} add pass udp from any 67 to 255.255.255.255 68 in
498 if [ $ipv6_available -eq 0 ]; then
499 ${fwcmd} add pass udp from fe80::/10 to me6 546 in
501 # Some servers will ping the IP while trying to decide if it's
503 ${fwcmd} add pass icmp from any to any icmptype 8
504 if [ $ipv6_available -eq 0 ]; then
505 ${fwcmd} add pass ipv6-icmp from any to any icmp6type 128,129
508 # Allow "mandatory" ICMP in.
509 ${fwcmd} add pass icmp from any to any icmptype 3,4,11
510 if [ $ipv6_available -eq 0 ]; then
511 ${fwcmd} add pass ipv6-icmp from any to any icmp6type 3
514 # Add permits for this workstations published services below
515 # Only IPs and nets in firewall_allowservices is allowed in.
516 # If you really wish to let anyone use services on your
517 # workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
519 # Note: We don't use keep-state as that would allow DoS of
521 # You can add 'keep-state' to the lines for slightly
522 # better performance if you fell that DoS of your
523 # workstation won't be a problem.
525 for i in ${firewall_allowservices} ; do
526 for j in ${firewall_myservices} ; do
527 ${fwcmd} add pass tcp from $i to me $j
528 if [ $ipv6_available -eq 0 ]; then
529 ${fwcmd} add pass tcp from $i to me6 $j
534 # Allow all connections from trusted IPs.
535 # Playing with the content of firewall_trusted could seriously
536 # degrade the level of protection provided by the firewall.
537 for i in ${firewall_trusted} ; do
538 ${fwcmd} add pass ip from $i to me
540 for i in ${firewall_trusted_ipv6} ; do
541 ${fwcmd} add pass all from $i to me6
544 ${fwcmd} add 65000 count ip from any to any
546 # Drop packets to ports where we don't want logging
547 for i in ${firewall_nologports} ; do
548 ${fwcmd} add deny { tcp or udp } from any to any $i in
551 # Broadcasts and muticasts
552 ${fwcmd} add deny ip from any to 255.255.255.255
553 ${fwcmd} add deny ip from any to 224.0.0.0/24 in # XXX
556 ${fwcmd} add deny udp from any to any 520 in
558 # Noise from webbrowsing.
559 # The statefull filter is a bit agressive, and will cause some
560 # connection teardowns to be logged.
561 ${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
563 # Deny and (if wanted) log the rest unconditionally.
565 if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
566 log="log logamount 500" # The default of 100 is too low.
567 sysctl net.inet.ip.fw.verbose=1 >/dev/null
569 ${fwcmd} add deny $log ip from any to any
572 [Cc][Ll][Oo][Ss][Ee][Dd])
573 ${fwcmd} add 65000 deny ip from any to any
575 [Uu][Nn][Kk][Nn][Oo][Ww][Nn])
578 if [ -r "${firewall_type}" ]; then
579 ${fwcmd} ${firewall_flags} ${firewall_type}