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