rc.firewall revision 181761
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 181761 2008-08-15 19:14:25Z jhb $ 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 8891019Scjc} 8991019Scjc 9051231Ssheldonhif [ -n "${1}" ]; then 9151231Ssheldonh firewall_type="${1}" 9229300Sdannyfi 9329300Sdanny 9415027Sphk############ 9529300Sdanny# Set quiet mode if requested 9651231Ssheldonh# 9751231Ssheldonhcase ${firewall_quiet} in 9851231Ssheldonh[Yy][Ee][Ss]) 9929300Sdanny fwcmd="/sbin/ipfw -q" 10051231Ssheldonh ;; 10151231Ssheldonh*) 10229300Sdanny fwcmd="/sbin/ipfw" 10351231Ssheldonh ;; 10451231Ssheldonhesac 10529300Sdanny 10629300Sdanny############ 10716578Salex# Flush out the list before we begin. 10851231Ssheldonh# 10950357Ssheldonh${fwcmd} -f flush 11016578Salex 111163749Sphksetup_loopback 112163749Sphk 11316578Salex############ 11464244Sru# Network Address Translation. All packets are passed to natd(8) 11564244Sru# before they encounter your remaining rules. The firewall rules 11664244Sru# will then be run again on each packet after translation by natd 11764244Sru# starting at the rule number following the divert rule. 11851231Ssheldonh# 11964244Sru# For ``simple'' firewall type the divert rule should be put to a 12064244Sru# different place to not interfere with address-checking rules. 12173842Sobrien# 12264244Srucase ${firewall_type} in 12365257Sru[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt]) 12464244Sru case ${natd_enable} in 12564244Sru [Yy][Ee][Ss]) 12664244Sru if [ -n "${natd_interface}" ]; then 127152562Sume ${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface} 12864244Sru fi 12964244Sru ;; 13064244Sru esac 131165648Spiso case ${firewall_nat_enable} in 132165648Spiso [Yy][Ee][Ss]) 133165648Spiso if [ -n "${firewall_nat_interface}" ]; then 134175522Srafan if echo "${firewall_nat_interface}" | \ 135175522Srafan grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then 136175522Srafan firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}" 137175522Srafan else 138175522Srafan firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}" 139175522Srafan fi 140175522Srafan ${fwcmd} nat 123 config log ${firewall_nat_flags} 141165648Spiso ${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface} 142165648Spiso fi 143165648Spiso ;; 144165648Spiso esac 14551231Ssheldonhesac 14635267Sbrian 14735267Sbrian############ 14815027Sphk# If you just configured ipfw in the kernel as a tool to solve network 14915027Sphk# problems or you just want to disallow some particular kinds of traffic 15051805Smpp# then you will want to change the default policy to open. You can also 15117594Sjkh# do this as your only action by setting the firewall_type to ``open''. 15251231Ssheldonh# 15350357Ssheldonh# ${fwcmd} add 65000 pass all from any to any 15415027Sphk 15515027Sphk 15617594Sjkh# Prototype setups. 15751231Ssheldonh# 15851231Ssheldonhcase ${firewall_type} in 15951231Ssheldonh[Oo][Pp][Ee][Nn]) 16050357Ssheldonh ${fwcmd} add 65000 pass all from any to any 16151231Ssheldonh ;; 16254108Sobrien 16351231Ssheldonh[Cc][Ll][Ii][Ee][Nn][Tt]) 16451231Ssheldonh ############ 16551231Ssheldonh # This is a prototype setup that will protect your system somewhat 16651231Ssheldonh # against people from outside your own network. 16751231Ssheldonh ############ 16829300Sdanny 169181761Sjhb # set these to your network 170181761Sjhb net="192.0.2.0/24" 17117594Sjkh 172179598Skeramida # Allow limited broadcast traffic from my own net. 173181761Sjhb ${fwcmd} add pass all from ${net} to 255.255.255.255 174179598Skeramida 17551231Ssheldonh # Allow any traffic to or from my own net. 176181761Sjhb ${fwcmd} add pass all from me to ${net} 177181761Sjhb ${fwcmd} add pass all from ${net} to me 17815027Sphk 17951231Ssheldonh # Allow TCP through if setup succeeded 18051231Ssheldonh ${fwcmd} add pass tcp from any to any established 18115027Sphk 18252873Sru # Allow IP fragments to pass through 18352873Sru ${fwcmd} add pass all from any to any frag 18452873Sru 18551231Ssheldonh # Allow setup of incoming email 186163749Sphk ${fwcmd} add pass tcp from any to me 25 setup 18715027Sphk 18851231Ssheldonh # Allow setup of outgoing TCP connections only 189163749Sphk ${fwcmd} add pass tcp from me to any setup 19015027Sphk 19151231Ssheldonh # Disallow setup of all other TCP connections 19251231Ssheldonh ${fwcmd} add deny tcp from any to any setup 19315027Sphk 19451231Ssheldonh # Allow DNS queries out in the world 195163749Sphk ${fwcmd} add pass udp from me to any 53 keep-state 19615027Sphk 19751231Ssheldonh # Allow NTP queries out in the world 198163749Sphk ${fwcmd} add pass udp from me to any 123 keep-state 19915027Sphk 20051231Ssheldonh # Everything else is denied by default, unless the 20151231Ssheldonh # IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel 20251231Ssheldonh # config file. 20351231Ssheldonh ;; 20415027Sphk 20551231Ssheldonh[Ss][Ii][Mm][Pp][Ll][Ee]) 20651231Ssheldonh ############ 20751231Ssheldonh # This is a prototype setup for a simple firewall. Configure this 208121881Sru # machine as a DNS and NTP server, and point all the machines 20951231Ssheldonh # on the inside at this machine for those services. 21051231Ssheldonh ############ 21115027Sphk 212181761Sjhb # set these to your outside interface network 21351231Ssheldonh oif="ed0" 214181761Sjhb onet="192.0.2.0/28" 21517594Sjkh 216181761Sjhb # set these to your inside interface network 21751231Ssheldonh iif="ed1" 218181761Sjhb inet="192.0.2.16/28" 21915027Sphk 22051231Ssheldonh # Stop spoofing 221181761Sjhb ${fwcmd} add deny all from ${inet} to any in via ${oif} 222181761Sjhb ${fwcmd} add deny all from ${onet} to any in via ${iif} 22315027Sphk 22451231Ssheldonh # Stop RFC1918 nets on the outside interface 22556736Srgrimes ${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif} 22656736Srgrimes ${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif} 22756736Srgrimes ${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif} 22815027Sphk 22964028Sobrien # Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1, 23064028Sobrien # DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E) 23164028Sobrien # on the outside interface 23256736Srgrimes ${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif} 23356736Srgrimes ${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif} 23456736Srgrimes ${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif} 23556736Srgrimes ${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif} 23656736Srgrimes ${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif} 23756736Srgrimes 23864244Sru # Network Address Translation. This rule is placed here deliberately 23964244Sru # so that it does not interfere with the surrounding address-checking 24064244Sru # rules. If for example one of your internal LAN machines had its IP 24164244Sru # address set to 192.0.2.1 then an incoming packet for it after being 24264244Sru # translated by natd(8) would match the `deny' rule above. Similarly 24364244Sru # an outgoing packet originated from it before being translated would 24464244Sru # match the `deny' rule below. 24564244Sru case ${natd_enable} in 24664244Sru [Yy][Ee][Ss]) 24764244Sru if [ -n "${natd_interface}" ]; then 24864244Sru ${fwcmd} add divert natd all from any to any via ${natd_interface} 24964244Sru fi 25064244Sru ;; 25164244Sru esac 25264244Sru 25364244Sru # Stop RFC1918 nets on the outside interface 25464244Sru ${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif} 25564244Sru ${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif} 25664244Sru ${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif} 25764244Sru 25864244Sru # Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1, 25964244Sru # DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E) 26064244Sru # on the outside interface 26164244Sru ${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif} 26264244Sru ${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif} 26364244Sru ${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif} 26464244Sru ${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif} 26564244Sru ${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif} 26664244Sru 26751231Ssheldonh # Allow TCP through if setup succeeded 26851231Ssheldonh ${fwcmd} add pass tcp from any to any established 26915027Sphk 27052873Sru # Allow IP fragments to pass through 27152873Sru ${fwcmd} add pass all from any to any frag 27252873Sru 27351231Ssheldonh # Allow setup of incoming email 274181760Sjhb ${fwcmd} add pass tcp from any to me 25 setup 27515027Sphk 27651231Ssheldonh # Allow access to our DNS 277181760Sjhb ${fwcmd} add pass tcp from any to me 53 setup 278181760Sjhb ${fwcmd} add pass udp from any to me 53 279181760Sjhb ${fwcmd} add pass udp from me 53 to any 28015027Sphk 28151231Ssheldonh # Allow access to our WWW 282181760Sjhb ${fwcmd} add pass tcp from any to me 80 setup 28315027Sphk 28451231Ssheldonh # Reject&Log all setup of incoming connections from the outside 28551231Ssheldonh ${fwcmd} add deny log tcp from any to any in via ${oif} setup 28615027Sphk 28751231Ssheldonh # Allow setup of any other TCP connection 28851231Ssheldonh ${fwcmd} add pass tcp from any to any setup 28915027Sphk 29051231Ssheldonh # Allow DNS queries out in the world 291181760Sjhb ${fwcmd} add pass udp from me to any 53 keep-state 29215027Sphk 29351231Ssheldonh # Allow NTP queries out in the world 294181760Sjhb ${fwcmd} add pass udp from me to any 123 keep-state 29515027Sphk 29651231Ssheldonh # Everything else is denied by default, unless the 29751231Ssheldonh # IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel 29851231Ssheldonh # config file. 29951231Ssheldonh ;; 30015027Sphk 301163749Sphk[Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn]) 302163749Sphk # Configuration: 303163749Sphk # firewall_myservices: List of TCP ports on which this host 304163749Sphk # offers services. 305163749Sphk # firewall_allowservices: List of IPs which has access to 306163749Sphk # $firewall_myservices. 307163749Sphk # firewall_trusted: List of IPs which has full access 308163749Sphk # to this host. Be very carefull 309163749Sphk # when setting this. This option can 310163749Sphk # seriously degrade the level of 311163749Sphk # protection provided by the firewall. 312163749Sphk # firewall_logdeny: Boolean (YES/NO) specifying if the 313163749Sphk # default denied packets should be 314163749Sphk # logged (in /var/log/security). 315163749Sphk # firewall_nologports: List of TCP/UDP ports for which 316163749Sphk # denied incomming packets are not 317163749Sphk # logged. 318163749Sphk 319163749Sphk # Allow packets for which a state has been built. 320163749Sphk ${fwcmd} add check-state 321163749Sphk 322163749Sphk # For services permitted below. 323163749Sphk ${fwcmd} add pass tcp from me to any established 324163749Sphk 325163749Sphk # Allow any connection out, adding state for each. 326163749Sphk ${fwcmd} add pass tcp from me to any setup keep-state 327163749Sphk ${fwcmd} add pass udp from me to any keep-state 328163749Sphk ${fwcmd} add pass icmp from me to any keep-state 329163749Sphk 330163749Sphk # Allow DHCP. 331163749Sphk ${fwcmd} add pass udp from 0.0.0.0 68 to 255.255.255.255 67 out 332163749Sphk ${fwcmd} add pass udp from any 67 to me 68 in 333163749Sphk ${fwcmd} add pass udp from any 67 to 255.255.255.255 68 in 334163749Sphk # Some servers will ping the IP while trying to decide if it's 335163749Sphk # still in use. 336163749Sphk ${fwcmd} add pass icmp from any to any icmptype 8 337163749Sphk 338163749Sphk # Allow "mandatory" ICMP in. 339163749Sphk ${fwcmd} add pass icmp from any to any icmptype 3,4,11 340163749Sphk 341163749Sphk # Add permits for this workstations published services below 342163749Sphk # Only IPs and nets in firewall_allowservices is allowed in. 343163749Sphk # If you really wish to let anyone use services on your 344163749Sphk # workstation, then set "firewall_allowservices='any'" in /etc/rc.conf 345163749Sphk # 346163749Sphk # Note: We don't use keep-state as that would allow DoS of 347163749Sphk # our statetable. 348163749Sphk # You can add 'keep-state' to the lines for slightly 349163749Sphk # better performance if you fell that DoS of your 350163749Sphk # workstation won't be a problem. 351163749Sphk # 352163749Sphk for i in ${firewall_allowservices} ; do 353163749Sphk for j in ${firewall_myservices} ; do 354163749Sphk ${fwcmd} add pass tcp from $i to me $j 355163749Sphk done 356163749Sphk done 357163749Sphk 358163749Sphk # Allow all connections from trusted IPs. 359163749Sphk # Playing with the content of firewall_trusted could seriously 360163749Sphk # degrade the level of protection provided by the firewall. 361163749Sphk for i in ${firewall_trusted} ; do 362163749Sphk ${fwcmd} add pass ip from $i to me 363163749Sphk done 364163749Sphk 365163749Sphk ${fwcmd} add 65000 count ip from any to any 366163749Sphk 367163749Sphk # Drop packets to ports where we don't want logging 368163749Sphk for i in ${firewall_nologports} ; do 369163749Sphk ${fwcmd} add deny { tcp or udp } from any to any $i in 370163749Sphk done 371163749Sphk 372163749Sphk # Broadcasts and muticasts 373163749Sphk ${fwcmd} add deny ip from any to 255.255.255.255 374163749Sphk ${fwcmd} add deny ip from any to 224.0.0.0/24 in # XXX 375163749Sphk 376163749Sphk # Noise from routers 377163749Sphk ${fwcmd} add deny udp from any to any 520 in 378163749Sphk 379163749Sphk # Noise from webbrowsing. 380163749Sphk # The statefull filter is a bit agressive, and will cause some 381163749Sphk # connection teardowns to be logged. 382163749Sphk ${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in 383163749Sphk 384163749Sphk # Deny and (if wanted) log the rest unconditionally. 385163749Sphk log="" 386163749Sphk if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then 387163749Sphk log="log logamount 500" # The default of 100 is too low. 388163749Sphk sysctl net.inet.ip.fw.verbose=1 >/dev/null 389163749Sphk fi 390163749Sphk ${fwcmd} add deny $log ip from any to any 391163749Sphk ;; 392163749Sphk 39391019Scjc[Cc][Ll][Oo][Ss][Ee][Dd]) 394163749Sphk ${fwcmd} add 65000 deny ip from any to any 39591019Scjc ;; 39651231Ssheldonh[Uu][Nn][Kk][Nn][Oo][Ww][Nn]) 39751231Ssheldonh ;; 39851231Ssheldonh*) 39959669Sbsd if [ -r "${firewall_type}" ]; then 40057014Spaul ${fwcmd} ${firewall_flags} ${firewall_type} 40151231Ssheldonh fi 40251231Ssheldonh ;; 40351231Ssheldonhesac 404