1.Dd July 20, 1996 2.Dt IPFW 8 SMM 3.Os FreeBSD 4.Sh NAME 5.Nm ipfw 6.Nd controlling utility for IP firewall 7.Sh SYNOPSIS 8.Nm ipfw 9.Op Fl q 10.Oo 11.Fl p Ar preproc 12.Op Fl D Ar macro Ns Op Ns =value 13.Op Fl U Ar macro 14.Oc 15file 16.Nm ipfw 17.Oo 18.Fl f 19| 20.Fl q 21.Oc 22flush 23.Nm ipfw 24.Oo 25.Fl q 26.Oc 27zero 28.Op Ar number ... 29.Nm ipfw 30delete 31.Ar number ... 32.Nm ipfw 33.Op Fl aftN 34list 35.Op Ar number ... 36.Nm ipfw 37.Oo 38.Fl ftN 39.Oc 40show 41.Op Ar number ... 42.Nm ipfw 43.Oo 44.Fl q 45.Oc 46add 47.Op Ar number 48.Ar action 49.Op log 50.Ar proto 51from 52.Ar src 53to 54.Ar dst 55.Op via Ar name | ipno 56.Op Ar options 57.Sh DESCRIPTION 58If used as shown in the first synopsis line, the 59.Ar file 60will be read line by line and applied as arguments to the 61.Nm 62command. 63.Pp 64Optionally, a preprocessor can be specified using 65.Fl p Ar preproc 66where 67.Ar file 68is to be piped through. Useful preprocessors include 69.Xr cpp 1 70and 71.Xr m4 1 . 72If 73.Ar preproc 74doesn't start with a slash as its first character, the usual 75.Ev PATH 76name search is performed. Care should be taken with this in environments 77where not all filesystems are mounted (yet) by the time 78.Nm 79is being run (e. g. since they are mounted over NFS). Once 80.Fl p 81has been specified, optional 82.Fl D 83and 84.Fl U 85specifcations can follow and will be passed on to the preprocessor. 86This allows for flexible configuration files (like conditionalizing 87them on the local hostname) and the use of macros to centralize 88frequently required arguments like IP addresses. 89.Pp 90The 91.Nm 92code works by going through the rule-list for each packet, 93until a match is found. 94All rules have two associated counters, a packet count and 95a byte count. 96These counters are updated when a packet matches the rule. 97.Pp 98The rules are ordered by a ``line-number'' from 1 to 65534 that is used 99to order and delete rules. Rules are tried in increasing order, and the 100first rule that matches a packet applies. 101Multiple rules may share the same number and apply in 102the order in which they were added. 103.Pp 104If a rule is added without a number, it is numbered 100 higher 105than the previous rule. If the highest defined rule number is 106greater than 65534, new rules are appended to the last rule. 107.Pp 108The delete operation deletes the first rule with number 109.Ar number , 110if any. 111.Pp 112The list command prints out the current rule set. 113.Pp 114The show command is equivalent to `ipfw -a list'. 115.Pp 116The zero operation zeroes the counters associated with rule number 117.Ar number . 118.Pp 119The flush operation removes all rules. 120.Pp 121Any command beginning with a '#', or being all blank, is ignored. 122.Pp 123One rule is always present: 124.Bd -literal -offset center 12565535 deny all from any to any 126.Ed 127.Pp 128This rule is the default policy, i.e., don't allow anything at all. 129Your job in setting up rules is to modify this policy to match your 130needs. 131.Pp 132However, if the kernel option 133.Dq IPFIREWALL_DEFAULT_TO_ACCEPT 134is active, the rule is instead: 135.Bd -literal -offset center 13665535 allow all from any to any 137.Ed 138.Pp 139This variation lets everything pass through. This option should only be 140activated in particular circumstances, such as if you use the firewall 141system as an on-demand denial-of-service filter that is normally wide open. 142.Pp 143The following options are available: 144.Bl -tag -width flag 145.It Fl a 146While listing, show counter values. See also ``show'' command. 147.It Fl f 148Don't ask for confirmation for commands that can cause problems if misused 149(ie; flush). 150.Ar Note , 151if there is no tty associated with the process, this is implied. 152.It Fl q 153While adding, zeroing or flushing, be quiet about actions (implies '-f'). 154This is useful for adjusting rules by executing multiple ipfw commands in a 155script (e.g. sh /etc/rc.firewall), or by processing a file of many ipfw rules, 156across a remote login session. If a flush is performed in normal 157(verbose) mode (with the default kernel configuration), it prints a message. 158Because all rules are flushed, the 159message cannot be delivered to the login session, the login session is 160closed and the remainder of the ruleset is not processed. Access to the 161console is required to recover. 162.It Fl t 163While listing, show last match timestamp. 164.It Fl N 165Try to resolve addresses and service names in output. 166.El 167.Pp 168.Ar action : 169.Bl -hang -offset flag -width 1234567890123456 170.It Ar allow 171Allow packets that match rule. 172The search terminates. Aliases are 173.Ar pass , 174.Ar permit , 175and 176.Ar accept . 177.It Ar deny 178Discard packets that match this rule. 179The search terminates. 180.Ar Drop 181is an alias for 182.Ar deny . 183.It Ar reject 184(Deprecated.) Discard packets that match this rule, and try to send an ICMP 185host unreachable notice. 186The search terminates. 187.It Ar unreach code 188Discard packets that match this rule, and try to send an ICMP 189unreachable notice with code 190.Ar code , 191where 192.Ar code 193is a number from zero to 255, or one of these aliases: 194.Ar net , 195.Ar host , 196.Ar protocol , 197.Ar port , 198.Ar needfrag , 199.Ar srcfail , 200.Ar net-unknown , 201.Ar host-unknown , 202.Ar isolated , 203.Ar net-prohib , 204.Ar host-prohib , 205.Ar tosnet , 206.Ar toshost , 207.Ar filter-prohib , 208.Ar host-precedence , 209or 210.Ar precedence-cutoff . 211The search terminates. 212.It Ar reset 213TCP packets only. Discard packets that match this rule, 214and try to send a TCP reset (RST) notice. 215The search terminates. 216.It Ar count 217Update counters for all packets that match rule. 218The search continues with the next rule. 219.It Ar divert port 220Divert packets that match this rule to the 221.Xr divert 4 222socket bound to port 223.Ar port . 224The search terminates. 225.It Ar tee port 226Send a copy of packets matching this rule to the 227.Xr divert 4 228socket bound to port 229.Ar port . 230The search continues with the next rule. This feature is not yet implemeted. 231.It Ar fwd ipaddr Op ,port 232Change the next-hop on matching packets to 233.Ar ipaddr , 234which can be an IP address in dotted quad or a host name. 235If 236.Ar ipaddr 237is not a directly-reachable address, the route 238as found in the local routing table for that IP is used 239instead. 240If 241.Ar ipaddr 242is a local address, then on a packet entering the system from a remote 243host it will be diverted to 244.Ar port 245on the local machine, keeping the local address of the socket set 246to the original IP address the packet was destined for. This is intended 247for use with transparent proxy servers. If the IP is not 248a local address then the port number (if specified) is ignored and 249the rule only applies to packets leaving the system. This will 250also map addresses to local ports when packets are generated locally. 251The search terminates if this rule matches. If the port number is not 252given then the port number in the packet is used, so that a packet for 253an external machine port Y would be forwarded to local port Y. The kernel 254must have been compiled with optiions IPFIREWALL_FORWARD. 255.It Ar skipto number 256Skip all subsequent rules numbered less than 257.Ar number . 258The search continues with the first rule numbered 259.Ar number 260or higher. 261.El 262.Pp 263If a packet matches more than one 264.Ar divert 265and/or 266.Ar tee 267rule, all but the last are ignored. 268.Pp 269If the kernel was compiled with 270.Dv IPFIREWALL_VERBOSE , 271then when a packet matches a rule with the ``log'' 272keyword a message will be printed on the console. 273If the kernel was compiled with the 274.Dv IPFIREWALL_VERBOSE_LIMIT 275option, then logging will cease after the number of packets 276specified by the option are received for that particular 277chain entry. Logging may then be re-enabled by clearing 278the packet counter for that entry. 279.Pp 280Console logging and the log limit are adjustable dynamically 281through the 282.Xr sysctl 8 283interface. 284.Pp 285.Ar proto : 286.Bl -hang -offset flag -width 1234567890123456 287.It Ar ip 288All packets match. The alias 289.Ar all 290has the same effect. 291.It Ar tcp 292Only TCP packets match. 293.It Ar udp 294Only UDP packets match. 295.It Ar icmp 296Only ICMP packets match. 297.It Ar <number|name> 298Only packets for the specified protocol matches (see 299.Pa /etc/protocols 300for a complete list). 301.El 302.Pp 303.Ar src 304and 305.Ar dst : 306.Bl -hang -offset flag 307.It Ar <address/mask> 308.Op Ar ports 309.El 310.Pp 311The 312.Em <address/mask> 313may be specified as: 314.Bl -hang -offset flag -width 1234567890123456 315.It Ar ipno 316An ipnumber of the form 1.2.3.4. 317Only this exact ip number match the rule. 318.It Ar ipno/bits 319An ipnumber with a mask width of the form 1.2.3.4/24. 320In this case all ip numbers from 1.2.3.0 to 1.2.3.255 will match. 321.It Ar ipno:mask 322An ipnumber with a mask width of the form 1.2.3.4:255.255.240.0. 323In this case all ip numbers from 1.2.0.0 to 1.2.15.255 will match. 324.El 325.Pp 326The sense of the match can be inverted by preceding an address with the 327``not'' modifier, causing all other addresses to be matched instead. This 328does not affect the selection of port numbers. 329.Pp 330With the TCP and UDP protocols, optional 331.Em ports 332may be specified as: 333.Pp 334.Bl -hang -offset flag 335.It Ns {port|port-port} Ns Op ,port Ns Op ,... 336.El 337.Pp 338Service names (from 339.Pa /etc/services ) 340may be used instead of numeric port values. 341A range may only be specified as the first value, 342and the length of the port list is limited to 343.Dv IP_FW_MAX_PORTS 344(as defined in 345.Pa /usr/src/sys/netinet/ip_fw.h ) 346ports. 347.Pp 348Fragmented packets which have a non-zero offset (i.e. not the first 349fragment) will never match a rule which has one or more port 350specifications. See the 351.Ar frag 352option for details on matching fragmented packets. 353.Pp 354Rules can apply to packets when they are incoming, or outgoing, or both. 355The 356.Ar in 357keyword indicates the rule should only match incoming packets. 358The 359.Ar out 360keyword indicates the rule should only match outgoing packets. 361.Pp 362To match packets going through a certain interface, specify 363the interface using 364.Ar via : 365.Bl -hang -offset flag -width 1234567890123456 366.It Ar via ifX 367Packet must be going through interface 368.Ar ifX. 369.It Ar via if* 370Packet must be going through interface 371.Ar ifX , 372where X is any unit number. 373.It Ar via any 374Packet must be going through 375.Em some 376interface. 377.It Ar via ipno 378Packet must be going through the interface having IP address 379.Ar ipno . 380.El 381.Pp 382The 383.Ar via 384keyword causes the interface to always be checked. 385If 386.Ar recv 387or 388.Ar xmit 389is used instead of 390.Ar via , 391then the only receive or transmit interface (respectively) is checked. 392By specifying both, it is possible to match packets based on both receive 393and transmit interface, e.g.: 394.Pp 395.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1" 396.Pp 397The 398.Ar recv 399interface can be tested on either incoming or outgoing packets, while the 400.Ar xmit 401interface can only be tested on outgoing packets. So 402.Ar out 403is required (and 404.Ar in 405invalid) whenver 406.Ar xmit 407is used. Specifying 408.Ar via 409together with 410.Ar xmit 411or 412.Ar recv 413is invalid. 414.Pp 415A packet may not have a receive or transmit interface: packets originating 416from the local host have no receive interface. while packets destined for 417the local host have no transmit interface. 418.Pp 419Additional 420.Ar options : 421.Bl -hang -offset flag -width 1234567890123456 422.It frag 423Matches if the packet is a fragment and this is not the first fragment 424of the datagram. 425.Ar frag 426may not be used in conjunction with either 427.Ar tcpflags 428or TCP/UDP port specifications. 429.It in 430Matches if this packet was on the way in. 431.It out 432Matches if this packet was on the way out. 433.It ipoptions Ar spec 434Matches if the IP header contains the comma separated list of 435options specified in 436.Ar spec . 437The supported IP options are: 438.Ar ssrr 439(strict source route), 440.Ar lsrr 441(loose source route), 442.Ar rr 443(record packet route), and 444.Ar ts 445(timestamp). 446The absence of a particular option may be denoted 447with a ``!''. 448.It established 449Matches packets that have the RST or ACK bits set. 450TCP packets only. 451.It setup 452Matches packets that have the SYN bit set but no ACK bit. 453TCP packets only. 454.It tcpflags Ar spec 455Matches if the TCP header contains the comma separated list of 456flags specified in 457.Ar spec . 458The supported TCP flags are: 459.Ar fin , 460.Ar syn , 461.Ar rst , 462.Ar psh , 463.Ar ack , 464and 465.Ar urg . 466The absence of a particular flag may be denoted 467with a ``!''. 468A rule which contains a 469.Ar tcpflags 470specification can never match a fragmented packet which has 471a non-zero offset. See the 472.Ar frag 473option for details on matching fragmented packets. 474.It icmptypes Ar types 475Matches if the ICMP type is in the list 476.Ar types . 477The list may be specified as any combination of ranges 478or individual types separated by commas. 479.El 480.Sh CHECKLIST 481Here are some important points to consider when designing your 482rules: 483.Bl -bullet -hang -offset flag 484.It 485Remember that you filter both packets going in and out. 486Most connections need packets going in both directions. 487.It 488Remember to test very carefully. 489It is a good idea to be near the console when doing this. 490.It 491Don't forget the loopback interface. 492.El 493.Sh FINE POINTS 494There is one kind of packet that the firewall will always discard, 495that is an IP fragment with a fragment offset of one. 496This is a valid packet, but it only has one use, to try to circumvent 497firewalls. 498.Pp 499If you are logged in over a network, loading the LKM version of 500.Nm 501is probably not as straightforward as you would think. 502I recommend this command line: 503.Bd -literal -offset center 504modload /lkm/ipfw_mod.o && \e 505ipfw add 32000 allow all from any to any 506.Ed 507.Pp 508Along the same lines, doing an 509.Bd -literal -offset center 510ipfw flush 511.Ed 512.Pp 513in similar surroundings is also a bad idea. 514.Pp 515The IP filter list may not be modified if the system security level 516is set to 3 or higher (see 517.Xr init 8 518for information on system security levels). 519.Sh PACKET DIVERSION 520A divert socket bound to the specified port will receive all packets diverted 521to that port; see 522.Xr divert 4 . 523If no socket is bound to the destination port, or if the kernel 524wasn't compiled with divert socket support, diverted packets are dropped. 525.Sh EXAMPLES 526This command adds an entry which denies all tcp packets from 527.Em cracker.evil.org 528to the telnet port of 529.Em wolf.tambov.su 530from being forwarded by the host: 531.Pp 532.Dl ipfw add deny tcp from cracker.evil.org to wolf.tambov.su 23 533.Pp 534This one disallows any connection from the entire crackers network to 535my host: 536.Pp 537.Dl ipfw addf deny all from 123.45.67.0/24 to my.host.org 538.Pp 539Here is a good usage of the list command to see accounting records 540and timestamp information: 541.Pp 542.Dl ipfw -at l 543.Pp 544or in short form without timestamps: 545.Pp 546.Dl ipfw -a l 547.Pp 548This rule diverts all incoming packets from 192.168.2.0/24 to divert port 5000: 549.Pp 550.Dl ipfw divert 5000 all from 192.168.2.0/24 to any in 551.Sh SEE ALSO 552.Xr cpp 1 , 553.Xr m4 1 , 554.Xr divert 4 , 555.Xr ip 4 , 556.Xr ipfirewall 4 , 557.Xr protocols 5 , 558.Xr services 5 , 559.Xr init 8 , 560.Xr reboot 8 , 561.Xr sysctl 8 , 562.Xr syslogd 8 563.Sh BUGS 564.Pp 565.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!! 566.Pp 567This program can put your computer in rather unusable state. When 568using it for the first time, work on the console of the computer, and 569do 570.Em NOT 571do anything you don't understand. 572.Pp 573When manipulating/adding chain entries, service and protocol names are 574not accepted. 575.Pp 576Incoming packet fragments diverted by 577.Ar divert 578are reassembled before delivery to the socket, whereas fragments diverted via 579.Ar tee 580are not. 581.Pp 582Port aliases containing dashes cannot be first in a list. 583.Pp 584The ``tee'' action is unimplemented. 585.Sh AUTHORS 586.An Ugen J. S. Antsilevich , 587.An Poul-Henning Kamp , 588.An Alex Nash , 589.An Archie Cobbs . 590API based upon code written by 591.An Daniel Boulet 592for BSDI. 593.Sh HISTORY 594.Nm 595first appeared in 596.Fx 2.0 . 597