1.\" 2.\" $FreeBSD$ 3.\" 4.Dd July 20, 1996 5.Dt IPFW 8 6.Os FreeBSD 7.Sh NAME 8.Nm ipfw 9.Nd controlling utility for IP firewall 10.Sh SYNOPSIS 11.Nm ipfw 12.Op Fl q 13.Oo 14.Fl p Ar preproc 15.Op Fl D Ar macro Ns Op Ns =value 16.Op Fl U Ar macro 17.Oc 18.Ar file 19.Nm ipfw 20.Oo 21.Fl f 22| 23.Fl q 24.Oc 25flush 26.Nm ipfw 27.Oo 28.Fl q 29.Oc 30zero 31.Op Ar number ... 32.Nm ipfw 33.Oo 34.Fl q 35.Oc 36resetlog 37.Op Ar number ... 38.Nm ipfw 39delete 40.Ar number ... 41.Nm ipfw 42.Op Fl aftN 43list 44.Op Ar number ... 45.Nm ipfw 46.Oo 47.Fl ftN 48.Oc 49show 50.Op Ar number ... 51.Nm ipfw 52.Oo 53.Fl q 54.Oc 55add 56.Op Ar number 57.Op prob match_probability 58.Ar action 59.Op log Op Ar logamount Ar number 60.Ar proto 61from 62.Ar src 63to 64.Ar dst 65.Op via Ar name | ipno 66.Op Ar options 67.Sh DESCRIPTION 68If used as shown in the first synopsis line, the 69.Ar file 70will be read line by line and applied as arguments to the 71.Nm 72command. 73.Pp 74Optionally, a preprocessor can be specified using 75.Fl p Ar preproc 76where 77.Ar file 78is to be piped through. Useful preprocessors include 79.Xr cpp 1 80and 81.Xr m4 1 . 82If 83.Ar preproc 84doesn't start with a slash as its first character, the usual 85.Ev PATH 86name search is performed. Care should be taken with this in environments 87where not all filesystems are mounted (yet) by the time 88.Nm 89is being run (e. g. since they are mounted over NFS). Once 90.Fl p 91has been specified, optional 92.Fl D 93and 94.Fl U 95specifications can follow and will be passed on to the preprocessor. 96This allows for flexible configuration files (like conditionalizing 97them on the local hostname) and the use of macros to centralize 98frequently required arguments like IP addresses. 99.Pp 100Each packet that has been received or is about to be sent goes through 101the 102.Nm 103rules. In the case of a host acting as a gateway, packets that are 104forwarded by the host are processed by 105.Nm 106twice 107.Po 108once when entering, and once when leaving 109.Pc . 110Each packet can be filtered based on the following information that is 111associated with it: 112.Pp 113.Bl -tag -offset indent -compact -width xxxx 114.It Receive Interface Pq Ar recv 115Interface over which the packet was received 116.It Transmit Interface Pq Ar xmit 117Interface over which the packet would be transmitted 118.It Incoming Pq Ar in 119Packet was just received 120.It Outgoing Pq Ar out 121Packet would be transmitted 122.It Source IP Address 123Sender's IP Address 124.It Destination IP Address 125Target's IP Address 126.It Protocol 127IP protocol, including but not limited to IP 128.Pq Ar ip , 129UDP 130.Pq Ar udp , 131TCP 132.Pq Ar tcp , 133or 134ICMP 135.Pq Ar icmp 136.It Source Port 137Sender's UDP or TCP port 138.It Destination Port 139Target's UDP or TCP port 140.It Connection Setup Flag Pq Ar setup 141This packet is a request to setup a TCP connection 142.It Connection Established Flag Pq Ar established 143This packet is part of an established TCP connection 144.It All TCP Flags Pq Ar tcpflags 145One or more of the TCP flags: close connection 146.Pq Ar fin , 147open connection 148.Pq Ar syn , 149reset connection 150.Pq Ar rst , 151push 152.Pq Ar psh , 153acknowledgment 154.Pq Ar ack , 155and 156urgent 157.Pq Ar urg 158.It Fragment Flag Pq Ar frag 159This packet is a fragment of an IP packet 160.It IP Options Pq Ar ipoptions 161One or more of the IP options: strict source route 162.Pq Ar ssrr , 163loose source route 164.Pq Ar lsrr , 165record route 166.Pq Ar rr , 167and timestamp 168.Pq Ar ts 169.It ICMP Types Pq Ar icmptypes 170One or more of the ICMP types: echo reply 171.Pq Ar 0 , 172destination unreachable 173.Pq Ar 3 , 174source quench 175.Pq Ar 4 , 176redirect 177.Pq Ar 5 , 178echo request 179.Pq Ar 8 , 180router advertisement 181.Pq Ar 9 , 182router solicitation 183.Pq Ar 10 , 184time-to-live exceeded 185.Pq Ar 11 , 186IP header bad 187.Pq Ar 12 , 188timestamp request 189.Pq Ar 13 , 190timestamp reply 191.Pq Ar 14 , 192information request 193.Pq Ar 15 , 194information reply 195.Pq Ar 16 , 196address mask request 197.Pq Ar 17 , 198and address mask reply 199.Pq Ar 18 200.El 201.Pp 202Note that may be dangerous to filter on the source IP address or 203source TCP/UDP port because either or both could easily be spoofed. 204.Pp 205The 206.Nm 207code works by going through the rule-list for each packet 208until a match is found. 209All rules have two associated counters, a packet count and 210a byte count. 211These counters are updated when a packet matches the rule. 212.Pp 213If a rule has the optional 214.Dq prob match_probability 215specifier, where the match_probability is a floating point number 216between 0 and 1, a match is only declared with the specified 217probability. This can be useful for a number of applications 218such as random packet drop or (in conjunction with 219.Xr dummynet 4 220) to simulate the effect of multiple paths leading to out-of-order 221packet delivery. 222.Pp 223The rules are ordered by a 224.Dq line-number 225from 1 to 65534 that is used 226to order and delete rules. Rules are tried in increasing order, and the 227first rule that matches a packet applies. 228Multiple rules may share the same number and apply in 229the order in which they were added. 230.Pp 231If a rule is added without a number, it is numbered 100 higher than the highest 232defined rule number, unless the highest defined rule number is 65435 or 233greater, in which case new rules are given that same number. 234.Pp 235The delete operation deletes the first rule with number 236.Ar number , 237if any. 238.Pp 239The list command prints out the current rule set. 240.Pp 241The show command is equivalent to 242.Sq ipfw -a list . 243.Pp 244The zero operation zeroes the counters associated with rule number 245.Ar number . 246.Pp 247The resetlog operation resets the logging counters associated with 248rule number 249.Ar number . 250.Pp 251The flush operation removes all rules. 252.Pp 253Any command beginning with a 254.Sq # , 255or being all blank, is ignored. 256.Pp 257One rule is always present: 258.Bd -literal -offset center 25965535 deny all from any to any 260.Ed 261.Pp 262This rule is the default policy, i.e., don't allow anything at all. 263Your job in setting up rules is to modify this policy to match your 264needs. 265.Pp 266However, if the kernel option 267.Dq IPFIREWALL_DEFAULT_TO_ACCEPT 268is active, the rule is instead: 269.Bd -literal -offset center 27065535 allow all from any to any 271.Ed 272.Pp 273This variation lets everything pass through. This option should only be 274activated in particular circumstances, such as if you use the firewall 275system as an on-demand denial-of-service filter that is normally wide open. 276.Pp 277The following options are available: 278.Bl -tag -width indent 279.It Fl a 280While listing, show counter values. See also 281.Dq show 282command. 283.It Fl f 284Don't ask for confirmation for commands that can cause problems if misused 285(i.e. flush). 286.Ar Note , 287if there is no tty associated with the process, this is implied. 288.It Fl q 289While adding, zeroing, resetlogging or flushing, be quiet about actions (implies 290.Fl f Ns ). 291This is useful for adjusting rules by executing multiple 292.Nm 293commands in a script 294.Po 295e.g., 296.Sq sh /etc/rc.firewall 297.Pc , 298or by processing a file of many 299.Nm 300rules, 301across a remote login session. If a flush is performed in normal 302(verbose) mode (with the default kernel configuration), it prints a message. 303Because all rules are flushed, the 304message cannot be delivered to the login session. This causes the 305remote login session to be closed and the remainder of the ruleset is 306not processed. Access to the console is required to recover. 307.It Fl t 308While listing, show last match timestamp. 309.It Fl N 310Try to resolve addresses and service names in output. 311.El 312.Pp 313.Ar action : 314.Bl -hang -offset flag -width 1234567890123456 315.It Ar allow 316Allow packets that match rule. 317The search terminates. Aliases are 318.Ar pass , 319.Ar permit , 320and 321.Ar accept . 322.It Ar deny 323Discard packets that match this rule. 324The search terminates. 325.Ar Drop 326is an alias for 327.Ar deny . 328.It Ar reject 329(Deprecated.) Discard packets that match this rule, and try to send an ICMP 330host unreachable notice. 331The search terminates. 332.It Ar unreach code 333Discard packets that match this rule, and try to send an ICMP 334unreachable notice with code 335.Ar code , 336where 337.Ar code 338is a number from zero to 255, or one of these aliases: 339.Ar net , 340.Ar host , 341.Ar protocol , 342.Ar port , 343.Ar needfrag , 344.Ar srcfail , 345.Ar net-unknown , 346.Ar host-unknown , 347.Ar isolated , 348.Ar net-prohib , 349.Ar host-prohib , 350.Ar tosnet , 351.Ar toshost , 352.Ar filter-prohib , 353.Ar host-precedence , 354or 355.Ar precedence-cutoff . 356The search terminates. 357.It Ar reset 358TCP packets only. Discard packets that match this rule, 359and try to send a TCP reset 360.Pq RST 361notice. 362The search terminates. 363.It Ar count 364Update counters for all packets that match rule. 365The search continues with the next rule. 366.It Ar divert port 367Divert packets that match this rule to the 368.Xr divert 4 369socket bound to port 370.Ar port . 371The search terminates. 372.It Ar tee port 373Send a copy of packets matching this rule to the 374.Xr divert 4 375socket bound to port 376.Ar port . 377The search terminates and the original packet is accepted 378(but see BUGS below). 379.It Ar fwd ipaddr Op ,port 380Change the next-hop on matching packets to 381.Ar ipaddr , 382which can be an IP address in dotted quad or a host name. 383If 384.Ar ipaddr 385is not a directly-reachable address, the route 386as found in the local routing table for that IP is used 387instead. 388If 389.Ar ipaddr 390is a local address, then on a packet entering the system from a remote 391host it will be diverted to 392.Ar port 393on the local machine, keeping the local address of the socket set 394to the original IP address the packet was destined for. This is intended 395for use with transparent proxy servers. If the IP is not 396a local address then the port number (if specified) is ignored and 397the rule only applies to packets leaving the system. This will 398also map addresses to local ports when packets are generated locally. 399The search terminates if this rule matches. If the port number is not 400given then the port number in the packet is used, so that a packet for 401an external machine port Y would be forwarded to local port Y. The kernel 402must have been compiled with options IPFIREWALL_FORWARD. 403.It Ar pipe pipe_nr 404Pass packet to a 405.Xr dummynet 4 406``pipe'' (for bandwidth limitation, delay etc.). See the 407.Xr dummynet 4 408manpage for further information. The search terminates; however, 409on exit from the pipe and if the sysctl variable 410net.inet.ip.fw.one_pass is not set, the packet is passed again to 411the firewall code starting from the next rule. 412.It Ar skipto number 413Skip all subsequent rules numbered less than 414.Ar number . 415The search continues with the first rule numbered 416.Ar number 417or higher. 418.It Ar uid user 419Match all TCP or UDP packets sent by or received for a 420.Ar user . 421A 422.Ar user 423may be matched by name or identification number. 424.It Ar gid group 425Match all TCP or UDP packets sent by or received for a 426.Ar group . 427A 428.Ar group 429may be matched by name or identification number. 430.El 431.Pp 432If the kernel was compiled with 433.Dv IPFIREWALL_VERBOSE , 434then when a packet matches a rule with the 435.Ar log 436keyword or a clear/resetlog is performed, a message will be logged to 437.Xr syslogd 8 , 438or, if that fails, to the console. If the kernel was compiled with the 439.Dv IPFIREWALL_VERBOSE_LIMIT 440option, then by default logging will cease after the number 441of packets specified by the option are received for that 442particular chain entry. However, if 443.Ar logamount Ar number 444is used, that 445.Ar number 446will be the default logging limit rather than 447.Dv IPFIREWALL_VERBOSE_LIMIT . 448When this limit is reached, the limit and rule number will be logged. 449Logging may then be re-enabled by clearing the logging counter 450or the packet counter for that entry. 451.Pp 452The 453.Xr syslogd 8 454logging and the default log limit are adjustable dynamically through the 455.Xr sysctl 8 456interface in the MIB base of 457.Dv net.inet.ip.fw . 458.Pp 459.Ar proto : 460.Bl -hang -offset flag -width 1234567890123456 461.It Ar ip 462All packets match. The alias 463.Ar all 464has the same effect. 465.It Ar tcp 466Only TCP packets match. 467.It Ar udp 468Only UDP packets match. 469.It Ar icmp 470Only ICMP packets match. 471.It Ar <number|name> 472Only packets for the specified protocol matches (see 473.Pa /etc/protocols 474for a complete list). 475.El 476.Pp 477.Ar src 478and 479.Ar dst : 480.Bl -hang -offset flag 481.It Ar <address/mask> 482.Op Ar ports 483.El 484.Pp 485The 486.Em <address/mask> 487may be specified as: 488.Bl -hang -offset flag -width 1234567890123456 489.It Ar ipno 490An ipnumber of the form 1.2.3.4. 491Only this exact ip number match the rule. 492.It Ar ipno/bits 493An ipnumber with a mask width of the form 1.2.3.4/24. 494In this case all ip numbers from 1.2.3.0 to 1.2.3.255 will match. 495.It Ar ipno:mask 496An ipnumber with a mask width of the form 1.2.3.4:255.255.240.0. 497In this case all ip numbers from 1.2.0.0 to 1.2.15.255 will match. 498.El 499.Pp 500The sense of the match can be inverted by preceding an address with the 501.Dq not 502modifier, causing all other addresses to be matched instead. This 503does not affect the selection of port numbers. 504.Pp 505With the TCP and UDP protocols, optional 506.Em ports 507may be specified as: 508.Pp 509.Bl -hang -offset flag 510.It Ns {port|port-port} Ns Op ,port Ns Op ,... 511.El 512.Pp 513Service names (from 514.Pa /etc/services ) 515may be used instead of numeric port values. 516A range may only be specified as the first value, 517and the length of the port list is limited to 518.Dv IP_FW_MAX_PORTS 519(as defined in 520.Pa /usr/src/sys/netinet/ip_fw.h ) 521ports. 522A 523.Ql \e 524can be used to escape the 525.Ql - 526character in a service name: 527.Pp 528.Dl ipfw add count tcp from any ftp\e\e-data-ftp to any 529.Pp 530Fragmented packets which have a non-zero offset (i.e. not the first 531fragment) will never match a rule which has one or more port 532specifications. See the 533.Ar frag 534option for details on matching fragmented packets. 535.Pp 536Rules can apply to packets when they are incoming, or outgoing, or both. 537The 538.Ar in 539keyword indicates the rule should only match incoming packets. 540The 541.Ar out 542keyword indicates the rule should only match outgoing packets. 543.Pp 544To match packets going through a certain interface, specify 545the interface using 546.Ar via : 547.Bl -hang -offset flag -width 1234567890123456 548.It Ar via ifX 549Packet must be going through interface 550.Ar ifX. 551.It Ar via if* 552Packet must be going through interface 553.Ar ifX , 554where X is any unit number. 555.It Ar via any 556Packet must be going through 557.Em some 558interface. 559.It Ar via ipno 560Packet must be going through the interface having IP address 561.Ar ipno . 562.El 563.Pp 564The 565.Ar via 566keyword causes the interface to always be checked. 567If 568.Ar recv 569or 570.Ar xmit 571is used instead of 572.Ar via , 573then the only receive or transmit interface (respectively) is checked. 574By specifying both, it is possible to match packets based on both receive 575and transmit interface, e.g.: 576.Pp 577.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1" 578.Pp 579The 580.Ar recv 581interface can be tested on either incoming or outgoing packets, while the 582.Ar xmit 583interface can only be tested on outgoing packets. So 584.Ar out 585is required (and 586.Ar in 587invalid) whenever 588.Ar xmit 589is used. Specifying 590.Ar via 591together with 592.Ar xmit 593or 594.Ar recv 595is invalid. 596.Pp 597A packet may not have a receive or transmit interface: packets originating 598from the local host have no receive interface. while packets destined for 599the local host have no transmit interface. 600.Pp 601Additional 602.Ar options : 603.Bl -hang -offset flag -width 1234567890123456 604.It frag 605Match if the packet is a fragment and this is not the first fragment 606of the datagram. 607.Ar frag 608may not be used in conjunction with either 609.Ar tcpflags 610or TCP/UDP port specifications. 611.It in 612Match if this packet was on the way in. 613.It out 614Match if this packet was on the way out. 615.It ipoptions Ar spec 616Match if the IP header contains the comma separated list of 617options specified in 618.Ar spec . 619The supported IP options are: 620.Ar ssrr 621(strict source route), 622.Ar lsrr 623(loose source route), 624.Ar rr 625(record packet route), and 626.Ar ts 627(timestamp). 628The absence of a particular option may be denoted 629with a 630.Dq ! . 631.It established 632Match packets that have the RST or ACK bits set. 633TCP packets only. 634.It setup 635Match packets that have the SYN bit set but no ACK bit. 636TCP packets only. 637.It tcpflags Ar spec 638Match if the TCP header contains the comma separated list of 639flags specified in 640.Ar spec . 641The supported TCP flags are: 642.Ar fin , 643.Ar syn , 644.Ar rst , 645.Ar psh , 646.Ar ack , 647and 648.Ar urg . 649The absence of a particular flag may be denoted 650with a 651.Dq ! . 652A rule which contains a 653.Ar tcpflags 654specification can never match a fragmented packet which has 655a non-zero offset. See the 656.Ar frag 657option for details on matching fragmented packets. 658.It icmptypes Ar types 659Match if the ICMP type is in the list 660.Ar types . 661The list may be specified as any combination of ranges 662or individual types separated by commas. 663.El 664.Sh CHECKLIST 665Here are some important points to consider when designing your 666rules: 667.Bl -bullet -hang -offset flag 668.It 669Remember that you filter both packets going in and out. 670Most connections need packets going in both directions. 671.It 672Remember to test very carefully. 673It is a good idea to be near the console when doing this. 674.It 675Don't forget the loopback interface. 676.El 677.Sh FINE POINTS 678There is one kind of packet that the firewall will always discard, 679that is an IP fragment with a fragment offset of one. 680This is a valid packet, but it only has one use, to try to circumvent 681firewalls. 682.Pp 683If you are logged in over a network, loading the KLD version of 684.Nm 685is probably not as straightforward as you would think. 686I recommend this command line: 687.Bd -literal -offset center 688kldload /modules/ipfw.ko && \e 689ipfw add 32000 allow all from any to any 690.Ed 691.Pp 692Along the same lines, doing an 693.Bd -literal -offset center 694ipfw flush 695.Ed 696.Pp 697in similar surroundings is also a bad idea. 698.Pp 699The IP filter list may not be modified if the system security level 700is set to 3 or higher 701.Po 702see 703.Xr init 8 704for information on system security levels 705.Pc . 706.Sh PACKET DIVERSION 707A divert socket bound to the specified port will receive all packets diverted 708or tee'd to that port; see 709.Xr divert 4 . 710If no socket is bound to the destination port, or if the kernel 711wasn't compiled with divert socket support, the packets are dropped. 712.Sh EXAMPLES 713This command adds an entry which denies all tcp packets from 714.Em cracker.evil.org 715to the telnet port of 716.Em wolf.tambov.su 717from being forwarded by the host: 718.Pp 719.Dl ipfw add deny tcp from cracker.evil.org to wolf.tambov.su 23 720.Pp 721This one disallows any connection from the entire crackers network to 722my host: 723.Pp 724.Dl ipfw add deny all from 123.45.67.0/24 to my.host.org 725.Pp 726Here is a good usage of the 727.Ar list 728command to see accounting records 729and timestamp information: 730.Pp 731.Dl ipfw -at l 732.Pp 733or in short form without timestamps: 734.Pp 735.Dl ipfw -a l 736.Pp 737This rule diverts all incoming packets from 192.168.2.0/24 to divert port 5000: 738.Pp 739.Dl ipfw divert 5000 all from 192.168.2.0/24 to any in 740.Sh SEE ALSO 741.Xr cpp 1 , 742.Xr m4 1 , 743.Xr divert 4 , 744.Xr dummynet 4 , 745.Xr ip 4 , 746.Xr ipfirewall 4 , 747.Xr protocols 5 , 748.Xr services 5 , 749.Xr init 8 , 750.Xr kldload 8 , 751.Xr reboot 8 , 752.Xr sysctl 8 , 753.Xr syslogd 8 . 754.Sh BUGS 755.Pp 756.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!! 757.Pp 758This program can put your computer in rather unusable state. When 759using it for the first time, work on the console of the computer, and 760do 761.Em NOT 762do anything you don't understand. 763.Pp 764When manipulating/adding chain entries, service and protocol names are 765not accepted. 766.Pp 767Incoming packet fragments diverted by 768.Ar divert 769or 770.Ar tee 771are reassembled before delivery to the socket. 772.Pp 773Packets that match a 774.Ar tee 775rule should not be immediately accepted, but should continue 776going through the rule list. This may be fixed in a later version. 777.Sh AUTHORS 778.An Ugen J. S. Antsilevich , 779.An Poul-Henning Kamp , 780.An Alex Nash , 781.An Archie Cobbs . 782API based upon code written by 783.An Daniel Boulet 784for BSDI. 785.Sh HISTORY 786.Nm Ipfw 787first appeared in 788.Fx 2.0 . 789