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