1.\" Copyright (c) 2001, Matthew Dillon. Terms and conditions are those of 2.\" the BSD Copyright as specified in the file "/usr/src/COPYRIGHT" in 3.\" the source tree. 4.\" 5.\" $FreeBSD$ 6.\" 7.Dd May 26, 2001 8.Dt FIREWALL 7 9.Os 10.Sh NAME 11.Nm firewall 12.Nd simple firewalls under FreeBSD 13.Sh FIREWALL BASICS 14A Firewall is most commonly used to protect an internal network 15from an outside network by preventing the outside network from 16making arbitrary connections into the internal network. 17Firewalls 18are also used to prevent outside entities from spoofing internal 19IP addresses and to isolate services such as NFS or SMBFS (Windows 20file sharing) within LAN segments. 21.Pp 22The 23.Fx 24firewalling system also has the capability to limit bandwidth using 25.Xr dummynet 4 . 26This feature can be useful when you need to guarantee a certain 27amount of bandwidth for a critical purpose. 28For example, if you 29are doing video conferencing over the Internet via your 30office T1 (1.5 MBits/s), you may wish to bandwidth-limit all other 31T1 traffic to 1 MBit/s in order to reserve at least 0.5 MBits 32for your video conferencing connections. 33Similarly if you are 34running a popular web or ftp site from a colocation facility 35you might want to limit bandwidth to prevent excessive bandwidth 36charges from your provider. 37.Pp 38Finally, 39.Fx 40firewalls may be used to divert packets or change the next-hop 41address for packets to help route them to the correct destination. 42Packet diversion is most often used to support NAT (network 43address translation), which allows an internal network using 44a private IP space to make connections to the outside for browsing 45or other purposes. 46.Pp 47Constructing a firewall may appear to be trivial, but most people 48get them wrong. 49The most common mistake is to create an exclusive 50firewall rather than an inclusive firewall. 51An exclusive firewall 52allows all packets through except for those matching a set of rules. 53An inclusive firewall allows only packets matching the ruleset 54through. 55Inclusive firewalls are much, much safer than exclusive 56firewalls but a tad more difficult to build properly. 57The 58second most common mistake is to blackhole everything except the 59particular port you want to let through. 60TCP/IP needs to be able 61to get certain types of ICMP errors to function properly - for 62example, to implement MTU discovery. 63Also, a number of common 64system daemons make reverse connections to the 65.Sy auth 66service in an attempt to authenticate the user making a connection. 67Auth is rather dangerous but the proper implementation is to return 68a TCP reset for the connection attempt rather than simply blackholing 69the packet. 70We cover these and other quirks involved with constructing 71a firewall in the sample firewall section below. 72.Sh IPFW KERNEL CONFIGURATION 73You do not need to create a custom kernel to use the IP firewalling features. 74If you enable firewalling in your 75.Em /etc/rc.conf 76(see below), the ipfw kernel module will be loaded automatically 77when necessary. 78However, 79if you are paranoid you can compile IPFW directly into the 80.Fx 81kernel by using the 82.Sy IPFIREWALL 83option set. 84If compiled in the kernel, ipfw denies all 85packets by default, which means that, if you do not load in 86a permissive ruleset via 87.Em /etc/rc.conf , 88rebooting into your new kernel will take the network offline. 89This can prevent you from being able to access your system if you 90are not sitting at the console. 91It is also quite common to 92update a kernel to a new release and reboot before updating 93the binaries. 94This can result in an incompatibility between 95the 96.Xr ipfw 8 97program and the kernel which prevents it from running in the 98boot sequence, also resulting in an inaccessible machine. 99Because of these problems the 100.Sy IPFIREWALL_DEFAULT_TO_ACCEPT 101kernel option is also available which changes the default firewall 102to pass through all packets. 103Note, however, that using this option 104may open a small window of opportunity during booting where your 105firewall passes all packets. 106Still, it's a good option to use 107while getting up to speed with 108.Fx 109firewalling. 110Get rid of it once you understand how it all works 111to close the loophole, though. 112There is a third option called 113.Sy IPDIVERT 114which allows you to use the firewall to divert packets to a user program 115and is necessary if you wish to use 116.Xr natd 8 117to give private internal networks access to the outside world. 118If you want to be able to limit the bandwidth used by certain types of 119traffic, the 120.Sy DUMMYNET 121option must be used to enable 122.Em ipfw pipe 123rules. 124.Sh SAMPLE IPFW-BASED FIREWALL 125Here is an example ipfw-based firewall taken from a machine with three 126interface cards. 127fxp0 is connected to the 'exposed' LAN. 128Machines 129on this LAN are dual-homed with both internal 10.\& IP addresses and 130Internet-routed IP addresses. 131In our example, 192.100.5.x represents 132the Internet-routed IP block while 10.x.x.x represents the internal 133networks. 134While it isn't relevant to the example, 10.0.1.x is 135assigned as the internal address block for the LAN on fxp0, 10.0.2.x 136for the LAN on fxp1, and 10.0.3.x for the LAN on fxp2. 137.Pp 138In this example we want to isolate all three LANs from the Internet 139as well as isolate them from each other, and we want to give all 140internal addresses access to the Internet through a NAT gateway running 141on this machine. 142To make the NAT gateway work, the firewall machine 143is given two Internet-exposed addresses on fxp0 in addition to an 144internal 10.\& address on fxp0: one exposed address (not shown) 145represents the machine's official address, and the second exposed 146address (192.100.5.5 in our example) represents the NAT gateway 147rendezvous IP. 148We make the example more complex by giving the machines 149on the exposed LAN internal 10.0.0.x addresses as well as exposed 150addresses. 151The idea here is that you can bind internal services 152to internal addresses even on exposed machines and still protect 153those services from the Internet. 154The only services you run on 155exposed IP addresses would be the ones you wish to expose to the 156Internet. 157.Pp 158It is important to note that the 10.0.0.x network in our example 159is not protected by our firewall. 160You must make sure that your 161Internet router protects this network from outside spoofing. 162Also, in our example, we pretty much give the exposed hosts free 163reign on our internal network when operating services through 164internal IP addresses (10.0.0.x). 165This is somewhat of security 166risk: what if an exposed host is compromised? 167To remove the 168risk and force everything coming in via LAN0 to go through 169the firewall, remove rules 01010 and 01011. 170.Pp 171Finally, note that the use of internal addresses represents a 172big piece of our firewall protection mechanism. 173With proper 174spoofing safeguards in place, nothing outside can directly 175access an internal (LAN1 or LAN2) host. 176.Bd -literal 177# /etc/rc.conf 178# 179firewall_enable="YES" 180firewall_type="/etc/ipfw.conf" 181 182# temporary port binding range let 183# through the firewall. 184# 185# NOTE: heavily loaded services running through the firewall may require 186# a larger port range for local-size binding. 4000-10000 or 4000-30000 187# might be a better choice. 188ip_portrange_first=4000 189ip_portrange_last=5000 190\&... 191.Ed 192.Pp 193.Bd -literal 194# /etc/ipfw.conf 195# 196# FIREWALL: the firewall machine / nat gateway 197# LAN0 10.0.0.X and 192.100.5.X (dual homed) 198# LAN1 10.0.1.X 199# LAN2 10.0.2.X 200# sw: ethernet switch (unmanaged) 201# 202# 192.100.5.x represents IP addresses exposed to the Internet 203# (i.e. Internet routeable). 10.x.x.x represent internal IPs 204# (not exposed) 205# 206# [LAN1] 207# ^ 208# | 209# FIREWALL -->[LAN2] 210# | 211# [LAN0] 212# | 213# +--> exposed host A 214# +--> exposed host B 215# +--> exposed host C 216# | 217# INTERNET (secondary firewall) 218# ROUTER 219# | 220# [Internet] 221# 222# NOT SHOWN: The INTERNET ROUTER must contain rules to disallow 223# all packets with source IP addresses in the 10. block in order 224# to protect the dual-homed 10.0.0.x block. Exposed hosts are 225# not otherwise protected in this example - they should only bind 226# exposed services to exposed IPs but can safely bind internal 227# services to internal IPs. 228# 229# The NAT gateway works by taking packets sent from internal 230# IP addresses to external IP addresses and routing them to natd, which 231# is listening on port 8668. This is handled by rule 00300. Data coming 232# back to natd from the outside world must also be routed to natd using 233# rule 00301. To make the example interesting, we note that we do 234# NOT have to run internal requests to exposed hosts through natd 235# (rule 00290) because those exposed hosts know about our 236# 10. network. This can reduce the load on natd. Also note that we 237# of course do not have to route internal<->internal traffic through 238# natd since those hosts know how to route our 10. internal network. 239# The natd command we run from /etc/rc.local is shown below. See 240# also the in-kernel version of natd, ipnat. 241# 242# natd -s -u -a 208.161.114.67 243# 244# 245add 00290 skipto 1000 ip from 10.0.0.0/8 to 192.100.5.0/24 246add 00300 divert 8668 ip from 10.0.0.0/8 to not 10.0.0.0/8 247add 00301 divert 8668 ip from not 10.0.0.0/8 to 192.100.5.5 248 249# Short cut the rules to avoid running high bandwidths through 250# the entire rule set. Allow established tcp connections through, 251# and shortcut all outgoing packets under the assumption that 252# we need only firewall incoming packets. 253# 254# Allowing established tcp connections through creates a small 255# hole but may be necessary to avoid overloading your firewall. 256# If you are worried, you can move the rule to after the spoof 257# checks. 258# 259add 01000 allow tcp from any to any established 260add 01001 allow all from any to any out via fxp0 261add 01001 allow all from any to any out via fxp1 262add 01001 allow all from any to any out via fxp2 263 264# Spoof protection. This depends on how well you trust your 265# internal networks. Packets received via fxp1 MUST come from 266# 10.0.1.x. Packets received via fxp2 MUST come from 10.0.2.x. 267# Packets received via fxp0 cannot come from the LAN1 or LAN2 268# blocks. We can't protect 10.0.0.x here, the Internet router 269# must do that for us. 270# 271add 01500 deny all from not 10.0.1.0/24 in via fxp1 272add 01500 deny all from not 10.0.2.0/24 in via fxp2 273add 01501 deny all from 10.0.1.0/24 in via fxp0 274add 01501 deny all from 10.0.2.0/24 in via fxp0 275 276# In this example rule set there are no restrictions between 277# internal hosts, even those on the exposed LAN (as long as 278# they use an internal IP address). This represents a 279# potential security hole (what if an exposed host is 280# compromised?). If you want full restrictions to apply 281# between the three LANs, firewalling them off from each 282# other for added security, remove these two rules. 283# 284# If you want to isolate LAN1 and LAN2, but still want 285# to give exposed hosts free reign with each other, get 286# rid of rule 01010 and keep rule 01011. 287# 288# (commented out, uncomment for less restrictive firewall) 289#add 01010 allow all from 10.0.0.0/8 to 10.0.0.0/8 290#add 01011 allow all from 192.100.5.0/24 to 192.100.5.0/24 291# 292 293# SPECIFIC SERVICES ALLOWED FROM SPECIFIC LANS 294# 295# If using a more restrictive firewall, allow specific LANs 296# access to specific services running on the firewall itself. 297# In this case we assume LAN1 needs access to filesharing running 298# on the firewall. If using a less restrictive firewall 299# (allowing rule 01010), you don't need these rules. 300# 301add 01012 allow tcp from 10.0.1.0/8 to 10.0.1.1 139 302add 01012 allow udp from 10.0.1.0/8 to 10.0.1.1 137,138 303 304# GENERAL SERVICES ALLOWED TO CROSS INTERNAL AND EXPOSED LANS 305# 306# We allow specific UDP services through: DNS lookups, ntalk, and ntp. 307# Note that internal services are protected by virtue of having 308# spoof-proof internal IP addresses (10. net), so these rules 309# really only apply to services bound to exposed IPs. We have 310# to allow UDP fragments or larger fragmented UDP packets will 311# not survive the firewall. 312# 313# If we want to expose high-numbered temporary service ports 314# for things like DNS lookup responses we can use a port range, 315# in this example 4000-65535, and we set to /etc/rc.conf variables 316# on all exposed machines to make sure they bind temporary ports 317# to the exposed port range (see rc.conf example above) 318# 319add 02000 allow udp from any to any 4000-65535,domain,ntalk,ntp 320add 02500 allow udp from any to any frag 321 322# Allow similar services for TCP. Again, these only apply to 323# services bound to exposed addresses. NOTE: we allow 'auth' 324# through but do not actually run an identd server on any exposed 325# port. This allows the machine being authed to respond with a 326# TCP RESET. Throwing the packet away would result in delays 327# when connecting to remote services that do reverse ident lookups. 328# 329# Note that we do not allow tcp fragments through, and that we do 330# not allow fragments in general (except for UDP fragments). We 331# expect the TCP mtu discovery protocol to work properly so there 332# should be no TCP fragments. 333# 334add 03000 allow tcp from any to any http,https 335add 03000 allow tcp from any to any 4000-65535,ssh,smtp,domain,ntalk 336add 03000 allow tcp from any to any auth,pop3,ftp,ftp-data 337 338# It is important to allow certain ICMP types through, here is a list 339# of general ICMP types. Note that it is important to let ICMP type 3 340# through. 341# 342# 0 Echo Reply 343# 3 Destination Unreachable (used by TCP MTU discovery, aka 344# packet-too-big) 345# 4 Source Quench (typically not allowed) 346# 5 Redirect (typically not allowed - can be dangerous!) 347# 8 Echo 348# 11 Time Exceeded 349# 12 Parameter Problem 350# 13 Timestamp 351# 14 Timestamp Reply 352# 353# Sometimes people need to allow ICMP REDIRECT packets, which is 354# type 5, but if you allow it make sure that your Internet router 355# disallows it. 356 357add 04000 allow icmp from any to any icmptypes 0,3,8,11,12,13,14 358 359# log any remaining fragments that get through. Might be useful, 360# otherwise don't bother. Have a final deny rule as a safety to 361# guarantee that your firewall is inclusive no matter how the kernel 362# is configured. 363# 364add 05000 deny log ip from any to any frag 365add 06000 deny all from any to any 366.Ed 367.Sh PORT BINDING INTERNAL AND EXTERNAL SERVICES 368We've mentioned multi-homing hosts and binding services to internal or 369external addresses but we haven't really explained it. 370When you have a 371host with multiple IP addresses assigned to it, you can bind services run 372on that host to specific IPs or interfaces rather than all IPs. 373Take 374the firewall machine for example: with three interfaces 375and two exposed IP addresses 376on one of those interfaces, the firewall machine is known by 5 different 377IP addresses (10.0.0.1, 10.0.1.1, 10.0.2.1, 192.100.5.5, and say 378192.100.5.1). 379If the firewall is providing file sharing services to the 380windows LAN segment (say it is LAN1), you can use samba's 'bind interfaces' 381directive to specifically bind it to just the LAN1 IP address. 382That 383way the file sharing services will not be made available to other LAN 384segments. 385The same goes for NFS. 386If LAN2 has your UNIX engineering 387workstations, you can tell nfsd to bind specifically to 10.0.2.1. 388You 389can specify how to bind virtually every service on the machine and you 390can use a light 391.Xr jail 8 392to indirectly bind services that do not otherwise give you the option. 393.Sh SEE ALSO 394.Xr ipnat 1 , 395.Xr dummynet 4 , 396.Xr ipnat 5 , 397.Xr rc.conf 5 , 398.Xr smb.conf 5 Pq Pa ports/net/samba , 399.Xr samba 7 Pq Pa ports/net/samba , 400.Xr config 8 , 401.Xr ipfw 8 , 402.Xr jail 8 , 403.Xr natd 8 , 404.Xr nfsd 8 405.Sh ADDITIONAL READING 406.Xr ipf 5 , 407.Xr ipf 8 , 408.Xr ipfstat 8 409.Sh HISTORY 410The 411.Nm 412manual page was originally written by 413.An Matthew Dillon 414and first appeared 415in 416.Fx 4.3 , 417May 2001. 418