1.\" 2.\" $FreeBSD$ 3.\" 4.Dd May 31, 2001 5.Dt IPFW 8 6.Os 7.Sh NAME 8.Nm ipfw 9.Nd IP firewall and traffic shaper control program 10.Sh SYNOPSIS 11.Nm 12.Op Fl q 13.Oo 14.Fl p Ar preproc 15.Oo Fl D 16.Ar macro Ns Op = Ns Ar value 17.Oc 18.Op Fl U Ar macro 19.Oc 20.Ar pathname 21.Nm 22.Op Fl f | q 23.Cm flush 24.Nm 25.Op Fl q 26.Brq Cm zero | resetlog | delete 27.Op Ar number ... 28.Nm 29.Op Fl s Op Ar field 30.Op Fl adeftN 31.Brq Cm list | show 32.Op Ar number ... 33.Nm 34.Op Fl q 35.Cm add 36.Op Ar number 37.Ar rule-body 38.Nm 39.Cm pipe 40.Ar number 41.Cm config 42.Ar pipe-config-options 43.Nm 44.Cm pipe 45.Brq Cm delete | list | show 46.Op Ar number ... 47.Nm 48.Cm queue 49.Ar number 50.Cm config 51.Ar queue-config-options 52.Nm 53.Cm queue 54.Brq Cm delete | list | show 55.Op Ar number ... 56.Sh DESCRIPTION 57.Nm 58is the user interface for controlling the 59.Xr ipfirewall 4 60and the 61.Xr dummynet 4 62traffic shaper in 63.Fx . 64.Pp 65Each incoming or outgoing packet is passed through the 66.Nm 67rules. 68The number of times a packet is processed by 69.Nm 70varies \(em basically, 71.Nm 72is invoked every time the kernel functions 73.Fn ip_input , 74.Fn ip_output 75and 76.Fn bdg_forward 77are invoked. 78This means that packets are processed once for connections having 79only one endpoint on the local host, twice for connections with 80both endpoints on the local host, or for packet routed by the host 81(acting as a gateway), and once for packets bridged by the host 82(acting as a bridge). 83.Pp 84A firewall configuration is made of a list of numbered rules, 85which is scanned for each packet until a match is found and 86the relevant action is performed. 87Depending on the action and certain system settings, packets 88can be reinjected into the firewall at the rule after the 89matching one for further processing. 90All rules apply to all interfaces, so it is responsibility 91of the system administrator to write the ruleset in such a 92way as to minimize the number of checks. 93.Pp 94A configuration always includes a 95.Em DEFAULT 96rule (numbered 65535) which cannot be modified, 97and matches all packets. 98The action associated with the default rule can be either 99.Cm deny 100or 101.Cm allow 102depending on how the kernel is configured. 103.Pp 104If the ruleset includes one or more rules with the 105.Cm keep-state 106or 107.Cm limit 108option, then 109.Nm 110assumes a 111.Em stateful 112behaviour, i.e. upon a match it will create dynamic rules matching 113the exact parameters (addresses and ports) of the matching packet. 114.Pp 115These dynamic rules, which have a limited lifetime, are checked 116at the first occurrence of a 117.Cm check-state 118or 119.Cm keep-state 120rule, and are typically used to open the firewall on-demand to 121legitimate traffic only. 122See the 123.Sx RULE FORMAT 124and 125.Sx EXAMPLES 126sections below for more information on the stateful behaviour of 127.Nm . 128.Pp 129All rules (including dynamic ones) have a few associated counters: 130a packet count, a byte count, a log count and a timestamp 131indicating the time of the last match. 132Counters can be displayed or reset with 133.Nm 134commands. 135.Pp 136Rules can be added with the 137.Cm add 138command; deleted individually with the 139.Cm delete 140command, and globally with the 141.Cm flush 142command; displayed, optionally with the content of the 143counters, using the 144.Cm show 145and 146.Cm list 147commands. 148Finally, counters can be reset with the 149.Cm zero 150and 151.Cm resetlog 152commands. 153.Pp 154The following options are available: 155.Bl -tag -width indent 156.It Fl a 157While listing, show counter values. 158See also the 159.Cm show 160command. 161.It Fl d 162While listing, show dynamic rules in addition to static ones. 163.It Fl e 164While listing, if the 165.Fl d 166option was specified, also show expired dynamic rules. 167.It Fl f 168Don't ask for confirmation for commands that can cause problems 169if misused, 170.No i.e. Cm flush . 171.Em Note , 172if there is no tty associated with the process, this is implied. 173.It Fl q 174While 175.Cm add Ns ing , 176.Cm zero Ns ing , 177.Cm resetlog Ns ging 178or 179.Cm flush Ns ing , 180be quiet about actions 181(implies 182.Fl f ) . 183This is useful for adjusting rules by executing multiple 184.Nm 185commands in a script 186(e.g., 187.Ql sh\ /etc/rc.firewall ) , 188or by processing a file of many 189.Nm 190rules, 191across a remote login session. 192If a 193.Cm flush 194is performed in normal (verbose) mode (with the default kernel 195configuration), it prints a message. 196Because all rules are flushed, the message cannot be delivered 197to the login session. 198This causes the remote login session to be closed and the 199remainder of the ruleset is not processed. 200Access to the console is required to recover. 201.It Fl t 202While listing, show last match timestamp. 203.It Fl N 204Try to resolve addresses and service names in output. 205.It Fl s Op Ar field 206While listing pipes, sort according to one of the four 207counters (total and current packets or bytes). 208.El 209.Pp 210To ease configuration, rules can be put into a file which is 211processed using 212.Nm 213as shown in the first synopsis line. 214An absolute 215.Ar pathname 216must be used. 217The file 218will be read line by line and applied as arguments to the 219.Nm 220utility. 221.Pp 222Optionally, a preprocessor can be specified using 223.Fl p Ar preproc 224where 225.Ar pathname 226is to be piped through. 227Useful preprocessors include 228.Xr cpp 1 229and 230.Xr m4 1 . 231If 232.Ar preproc 233doesn't start with a slash 234.Pq Ql / 235as its first character, the usual 236.Ev PATH 237name search is performed. 238Care should be taken with this in environments where not all 239filesystems are mounted (yet) by the time 240.Nm 241is being run (e.g. when they are mounted over NFS). 242Once 243.Fl p 244has been specified, optional 245.Fl D 246and 247.Fl U 248specifications can follow and will be passed on to the preprocessor. 249This allows for flexible configuration files (like conditionalizing 250them on the local hostname) and the use of macros to centralize 251frequently required arguments like IP addresses. 252.Pp 253The 254.Nm 255.Cm pipe 256commands are used to configure the traffic shaper, as shown in the 257.Sx TRAFFIC SHAPER CONFIGURATION 258section below. 259.Sh RULE FORMAT 260The 261.Nm 262rule format is the following: 263.Bd -ragged 264.Op Cm prob Ar match_probability 265.Ar action 266.Op Cm log Op Cm logamount Ar number 267.Ar proto 268.Cm from Ar src 269.Cm to Ar dst 270.Op Ar interface-spec 271.Op Ar options 272.Ed 273.Pp 274Each packet can be filtered based on the following information that is 275associated with it: 276.Pp 277.Bl -tag -width "Source and destination IP address" -offset indent -compact 278.It Protocol 279(TCP, UDP, ICMP, etc.) 280.It Source and destination IP address 281(possibly masked) 282.It Source and destination port 283(lists, ranges or masks) 284.It Direction 285(incoming or outgoing) 286.It Transmit and receive interface 287(by name or address) 288.It IP version 289.It IP type of service 290.It IP datagram length 291.It IP identification 292.It IP fragment flag 293(non-zero IP offset) 294.It IP time to live 295.It IP options 296.It TCP sequence number 297.It TCP acknowledgment number 298.It TCP flags 299(SYN, FIN, ACK, RST, etc.) 300.It TCP window 301.It TCP options 302.It ICMP types 303(for ICMP packets) 304.It User/group ID of the socket associated with the packet 305.El 306.Pp 307Note that it may be dangerous to filter on the source IP 308address or source TCP/UDP port because either or both could 309easily be spoofed. 310.Bl -tag -width indent 311.It Cm prob Ar match_probability 312A match is only declared with the specified probability 313(floating point number between 0 and 1). 314This can be useful for a number of applications such as 315random packet drop or 316(in conjunction with 317.Xr dummynet 4 ) 318to simulate the effect of multiple paths leading to out-of-order 319packet delivery. 320.It Ar action : 321.Bl -tag -width indent 322.It Cm allow 323Allow packets that match rule. 324The search terminates. 325Aliases are 326.Cm pass , 327.Cm permit 328and 329.Cm accept . 330.It Cm deny 331Discard packets that match this rule. 332The search terminates. 333.Cm drop 334is an alias for 335.Cm deny . 336.It Cm reject 337(Deprecated). 338Discard packets that match this rule, and try to send an ICMP 339host unreachable notice. 340The search terminates. 341.It Cm unreach Ar code 342Discard packets that match this rule, and try to send an ICMP 343unreachable notice with code 344.Ar code , 345where 346.Ar code 347is a number from 0 to 255, or one of these aliases: 348.Cm net , host , protocol , port , 349.Cm needfrag , srcfail , net-unknown , host-unknown , 350.Cm isolated , net-prohib , host-prohib , tosnet , 351.Cm toshost , filter-prohib , host-precedence 352or 353.Cm precedence-cutoff . 354The search terminates. 355.It Cm reset 356TCP packets only. 357Discard packets that match this rule, and try to send a TCP 358reset (RST) notice. 359The search terminates. 360.It Cm count 361Update counters for all packets that match rule. 362The search continues with the next rule. 363.It Cm check-state 364Checks the packet against the dynamic ruleset. 365If a match is found then the search terminates, otherwise 366we move to the next rule. 367If no 368.Cm check-state 369rule is found, the dynamic ruleset is checked at the first 370.Cm keep-state 371rule. 372.It Cm divert Ar port 373Divert packets that match this rule to the 374.Xr divert 4 375socket bound to port 376.Ar port . 377The search terminates. 378.It Cm tee Ar port 379Send a copy of packets matching this rule to the 380.Xr divert 4 381socket bound to port 382.Ar port . 383The search terminates and the original packet is accepted 384(but see section 385.Sx BUGS 386below). 387.It Cm fwd Ar ipaddr Ns Op , Ns Ar port 388Change the next-hop on matching packets to 389.Ar ipaddr , 390which can be an IP address in dotted quad or a host name. 391If 392.Ar ipaddr 393is not a directly-reachable address, the route as found in 394the local routing table for that IP is used instead. 395If 396.Ar ipaddr 397is a local address, then on a packet matching a fwd rule, 398it will be diverted to 399.Ar port 400on the local machine, keeping the local address of the socket 401set to the original IP address the packet was destined for. 402This makes the 403.Xr netstat 1 404entry look rather wierd but is intended for 405use with transparent proxy servers. 406If the IP is not a local address then the port number 407(if specified) is ignored. 408This will also map addresses when packets are 409generated locally. 410The search terminates if this rule matches. 411If the port number is not given then the port number in the 412packet is used, so that a packet for an external machine port 413Y would be forwarded to local port Y. 414The kernel must have been compiled with the 415.Dv IPFIREWALL_FORWARD 416option. Bridging interferes with forwarding of packets not destined 417to the local system as they bypass ip_input() and ip_output() where 418forwarding is implimented. The 419.Ar fwd 420action does not change the contents of the packet at all so 421packets forwarded to another system will usually be rejected by that system 422unless there is a matching rule on that system to capture them. 423.It Cm pipe Ar pipe_nr 424Pass packet to a 425.Xr dummynet 4 426.Dq pipe 427(for bandwidth limitation, delay, etc.). 428See the 429.Sx TRAFFIC SHAPER CONFIGURATION 430section for further information. 431The search terminates; however, on exit from the pipe and if 432the 433.Xr sysctl 8 434variable 435.Em net.inet.ip.fw.one_pass 436is not set, the packet is passed again to the firewall code 437starting from the next rule. 438.It Cm queue Ar queue_nr 439Pass packet to a 440.Xr dummynet 4 441.Dq queue 442(for bandwidth limitation using WF2Q). 443.It Cm skipto Ar number 444Skip all subsequent rules numbered less than 445.Ar number . 446The search continues with the first rule numbered 447.Ar number 448or higher. 449.El 450.It Cm log Op Cm logamount Ar number 451If the kernel was compiled with 452.Dv IPFIREWALL_VERBOSE , 453then when a packet matches a rule with the 454.Cm log 455keyword a message will be 456logged to 457.Xr syslogd 8 458with a 459.Dv LOG_SECURITY 460facility. 461.Em Note : 462by default, they are appended to the 463.Pa /var/log/security 464file (see 465.Xr syslog.conf 5 ) . 466If the kernel was compiled with the 467.Dv IPFIREWALL_VERBOSE_LIMIT 468option, then by default logging will cease after the number 469of packets specified by the option are received for that 470particular chain entry, and 471.Em net.inet.ip.fw.verbose_limit 472will be set to that number. 473However, if 474.Cm logamount Ar number 475is used, that 476.Ar number 477will be the logging limit rather than 478.Em net.inet.ip.fw.verbose_limit , 479where the value 480.Dq 0 481removes the logging limit. 482Logging may then be re-enabled by clearing the logging counter 483or the packet counter for that entry. 484.Pp 485Console logging and the log limit are adjustable dynamically 486through the 487.Xr sysctl 8 488interface in the MIB base of 489.Em net.inet.ip.fw . 490.It Ar proto 491An IP protocol specified by number or name (for a complete 492list see 493.Pa /etc/protocols ) . 494The 495.Cm ip 496or 497.Cm all 498keywords mean any protocol will match. 499.It Ar src No and Ar dst : 500.Cm any | me | Op Cm not 501.Aq Ar address Ns / Ns Ar mask 502.Op Ar ports 503.Pp 504Specifying 505.Cm any 506makes the rule match any IP address. 507.Pp 508Specifying 509.Cm me 510makes the rule match any IP address configured on an interface in the system. 511.Pp 512The 513.Aq Ar address Ns / Ns Ar mask 514may be specified as: 515.Bl -tag -width "ipno/bits" 516.It Ar ipno 517An IP number of the form 1.2.3.4. 518Only this exact IP number will match the rule. 519.It Ar ipno Ns / Ns Ar bits 520An IP number with a mask width of the form 1.2.3.4/24. 521In this case all IP numbers from 1.2.3.0 to 1.2.3.255 will match. 522.It Ar ipno Ns : Ns Ar mask 523An IP number with a mask of the form 1.2.3.4:255.255.240.0. 524In this case all IP numbers from 1.2.0.0 to 1.2.15.255 will match. 525.El 526.Pp 527The sense of the match can be inverted by preceding an address with the 528.Cm not 529modifier, causing all other addresses to be matched instead. 530This does not affect the selection of port numbers. 531.Pp 532With the TCP and UDP protocols, optional 533.Em ports 534may be specified as: 535.Bd -ragged -offset indent 536.Sm off 537.Brq Ar port | port No \&- Ar port | port : mask 538.Op , Ar port Op , Ar ... 539.Sm on 540.Ed 541.Pp 542The 543.Ql \&- 544notation specifies a range of ports (including boundaries). 545.Pp 546The 547.Ql \&: 548notation specifies a port and a mask, a match is declared if 549the port number in the packet matches the one in the rule, 550limited to the bits which are set in the mask. 551.Pp 552Service names (from 553.Pa /etc/services ) 554may be used instead of numeric port values. 555A range may only be specified as the first value, and the 556length of the port list is limited to 557.Dv IP_FW_MAX_PORTS 558ports (as defined in 559.Pa /usr/src/sys/netinet/ip_fw.h ) . 560A backslash 561.Pq Ql \e 562can be used to escape the dash 563.Pq Ql - 564character in a service name: 565.Pp 566.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any" 567.Pp 568Fragmented packets which have a non-zero offset (i.e. not the first 569fragment) will never match a rule which has one or more port 570specifications. 571See the 572.Cm frag 573option for details on matching fragmented packets. 574.It Ar interface-spec 575Some combinations of the following specifiers are allowed: 576.Bl -tag -width "via ipno" 577.It Cm in 578Only match incoming packets. 579.It Cm out 580Only match outgoing packets. 581.It Cm via Ar ifX 582Packet must be going through interface 583.Ar ifX . 584.It Cm via Ar if Ns Cm * 585Packet must be going through interface 586.Ar ifX , 587where 588.Ar X 589is any unit number. 590.It Cm via any 591Packet must be going through 592.Em some 593interface. 594.It Cm via Ar ipno 595Packet must be going through the interface having IP address 596.Ar ipno . 597.El 598.Pp 599The 600.Cm via 601keyword causes the interface to always be checked. 602If 603.Cm recv 604or 605.Cm xmit 606is used instead of 607.Cm via , 608then only the receive or transmit interface (respectively) 609is checked. 610By specifying both, it is possible to match packets based on 611both receive and transmit interface, e.g.: 612.Pp 613.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1" 614.Pp 615The 616.Cm recv 617interface can be tested on either incoming or outgoing packets, 618while the 619.Cm xmit 620interface can only be tested on outgoing packets. 621So 622.Cm out 623is required (and 624.Cm in 625is invalid) whenever 626.Cm xmit 627is used. 628Specifying 629.Cm via 630together with 631.Cm xmit 632or 633.Cm recv 634is invalid. 635.Pp 636A packet may not have a receive or transmit interface: packets 637originating from the local host have no receive interface, 638while packets destined for the local host have no transmit 639interface. 640.It Ar options : 641.Bl -tag -width indent 642.It Cm keep-state 643Upon a match, the firewall will create a dynamic rule, whose 644default behaviour is to matching bidirectional traffic between 645source and destination IP/port using the same protocol. 646The rule has a limited lifetime (controlled by a set of 647.Xr sysctl 8 648variables), and the lifetime is refreshed every time a matching 649packet is found. 650.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N 651The firewall will only allow 652.Ar N 653connections with the same 654set of parameters as specified in the rule. 655One or more 656of source and destination addresses and ports can be 657specified. 658.It Cm bridged 659Matches only bridged packets. 660This can be useful for multicast or broadcast traffic, which 661would otherwise pass through the firewall twice: once during 662bridging, and a second time when the packet is delivered to 663the local stack. 664.Pp 665Apart from a small performance penalty, this would be a problem 666when using 667.Em pipes 668because the same packet would be accounted for twice in terms 669of bandwidth, queue occupation, and also counters. 670.It Cm ipversion Ar ver 671Match if the IP header version is 672.Ar ver . 673.It Cm ipprecedence Ar precedence 674Match if the numeric value of IP datagram's precedence is equal to 675.Ar precedence . 676.It Cm iptos Ar spec 677Match if the IP header contains the comma separated list of 678service types specified in 679.Ar spec . 680The supported IP types of service are: 681.Pp 682.Cm lowdelay 683.Pq Dv IPTOS_LOWDELAY , 684.Cm throughput 685.Pq Dv IPTOS_THROUGHPUT , 686.Cm reliability 687.Pq Dv IPTOS_RELIABILITY , 688.Cm mincost 689.Pq Dv IPTOS_MINCOST , 690.Cm congestion 691.Pq Dv IPTOS_CE . 692The absence of a particular type may be denoted 693with a 694.Ql \&! . 695.It Cm iplen Ar len 696Match if the total length of a packet, including header and data, is 697.Ar len 698bytes. 699.It Cm ipid Ar id 700Match if the identification of IP datagram is 701.Ar id . 702.It Cm frag 703Match if the packet is a fragment and this is not the first 704fragment of the datagram. 705.Cm frag 706may not be used in conjunction with either 707.Cm tcpflags 708or TCP/UDP port specifications. 709.It Cm ipttl Ar ttl 710Match if the time to live of IP datagram is 711.Ar ttl . 712.It Cm ipoptions Ar spec 713Match if the IP header contains the comma separated list of 714options specified in 715.Ar spec . 716The supported IP options are: 717.Pp 718.Cm ssrr 719(strict source route), 720.Cm lsrr 721(loose source route), 722.Cm rr 723(record packet route) and 724.Cm ts 725(timestamp). 726The absence of a particular option may be denoted 727with a 728.Ql \&! . 729.It Cm tcpseq Ar seq 730TCP packets only. 731Match if the TCP header sequence number field is set to 732.Ar seq . 733.It Cm tcpack Ar ack 734TCP packets only. 735Match if the TCP header acknowledgment number field is set to 736.Ar ack . 737.It Cm tcpflags Ar spec 738TCP packets only. 739Match if the TCP header contains the comma separated list of 740flags specified in 741.Ar spec . 742The supported TCP flags are: 743.Pp 744.Cm fin , 745.Cm syn , 746.Cm rst , 747.Cm psh , 748.Cm ack 749and 750.Cm urg . 751The absence of a particular flag may be denoted 752with a 753.Ql \&! . 754A rule which contains a 755.Cm tcpflags 756specification can never match a fragmented packet which has 757a non-zero offset. 758See the 759.Cm frag 760option for details on matching fragmented packets. 761.It Cm established 762TCP packets only. 763Match packets that have the RST or ACK bits set. 764.It Cm setup 765TCP packets only. 766Match packets that have the SYN bit set but no ACK bit. 767This is the short form of 768.Dq Li tcpflags\ syn,!ack . 769.It Cm tcpwin Ar win 770TCP packets only. 771Match if the TCP header window field is set to 772.Ar win . 773.It Cm tcpoptions Ar spec 774TCP packets only. 775Match if the TCP header contains the comma separated list of 776options specified in 777.Ar spec . 778The supported TCP options are: 779.Pp 780.Cm mss 781(maximum segment size), 782.Cm window 783(tcp window advertisement), 784.Cm sack 785(selective ack), 786.Cm ts 787(rfc1323 timestamp) and 788.Cm cc 789(rfc1644 t/tcp connection count). 790The absence of a particular option may be denoted 791with a 792.Ql \&! . 793.It Cm icmptypes Ar types 794ICMP packets only. 795Match if the ICMP type is in the list 796.Ar types . 797The list may be specified as any combination of ranges or 798individual types separated by commas. 799The supported ICMP types are: 800.Pp 801echo reply 802.Pq Cm 0 , 803destination unreachable 804.Pq Cm 3 , 805source quench 806.Pq Cm 4 , 807redirect 808.Pq Cm 5 , 809echo request 810.Pq Cm 8 , 811router advertisement 812.Pq Cm 9 , 813router solicitation 814.Pq Cm 10 , 815time-to-live exceeded 816.Pq Cm 11 , 817IP header bad 818.Pq Cm 12 , 819timestamp request 820.Pq Cm 13 , 821timestamp reply 822.Pq Cm 14 , 823information request 824.Pq Cm 15 , 825information reply 826.Pq Cm 16 , 827address mask request 828.Pq Cm 17 829and address mask reply 830.Pq Cm 18 . 831.It Cm uid Ar user 832Match all TCP or UDP packets sent by or received for a 833.Ar user . 834A 835.Ar user 836may be matched by name or identification number. 837.It Cm gid Ar group 838Match all TCP or UDP packets sent by or received for a 839.Ar group . 840A 841.Ar group 842may be matched by name or identification number. 843.El 844.El 845.Sh TRAFFIC SHAPER CONFIGURATION 846The 847.Nm 848utility is also the user interface for the 849.Xr dummynet 4 850traffic shaper. 851The shaper operates by dividing packets into 852.Em flows 853according to a user-specified mask on different fields 854of the IP header. 855Packets belonging to the same flow are then passed to two 856different objects, named 857.Em pipe 858or 859.Em queue . 860.Pp 861A 862.Em pipe 863emulates a link with given bandwidth, propagation delay, 864queue size and packet loss rate. 865Packets transit through the pipe according to its parameters. 866.Pp 867A 868.Em queue 869is an abstraction used to implement the WF2Q+ (Worst-case Fair Weighted Fair Queueing) policy. 870The queue associates to each flow a weight and a reference pipe. 871Then, all flows linked to the same pipe are scheduled at the 872rate fixed by the pipe according to the WF2Q+ policy. 873.Pp 874The 875.Nm 876pipe configuration format is the following: 877.Bd -ragged 878.Cm pipe Ar number Cm config 879.Op Cm bw Ar bandwidth | device 880.Op Cm delay Ar ms-delay 881.Oo 882.Cm queue 883.Brq Ar slots | size 884.Oc 885.Op Cm plr Ar loss-probability 886.Op Cm mask Ar mask-specifier 887.Op Cm buckets Ar hash-table-size 888.Oo 889.Cm red | gred 890.Sm off 891.Ar w_q No / Ar min_th No / Ar max_th No / Ar max_p 892.Sm on 893.Oc 894.Ed 895.Pp 896The 897.Nm 898queue configuration format is the following: 899.Bd -ragged 900.Cm queue Ar number Cm config 901.Op Cm pipe Ar pipe_nr 902.Op Cm weight Ar weight 903.Oo 904.Cm queue 905.Brq Ar slots | size 906.Oc 907.Op Cm plr Ar loss-probability 908.Op Cm mask Ar mask-specifier 909.Op Cm buckets Ar hash-table-size 910.Oo 911.Cm red | gred 912.Sm off 913.Ar w_q No / Ar min_th No / Ar max_th No / Ar max_p 914.Sm on 915.Oc 916.Ed 917.Pp 918The following parameters can be configured for a pipe: 919.Bl -tag -width indent 920.It Cm bw Ar bandwidth | device 921Bandwidth, measured in 922.Sm off 923.Op Cm K | M 924.Brq Cm bit/s | Byte/s . 925.Sm on 926.Pp 927A value of 0 (default) means unlimited bandwidth. 928The unit must follow immediately the number, as in 929.Pp 930.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 931.Pp 932If a device name is specified instead of a numeric 933value, then the transmit clock is supplied by the specified 934device. 935At the moment only the 936.Xr tun 4 937device supports this 938functionality, for use in conjunction with 939.Xr ppp 8 . 940.It Cm delay Ar ms-delay 941Propagation delay, measured in milliseconds. 942The value is rounded to the next multiple of the clock tick 943(typically 10ms, but it is a good practice to run kernels 944with 945.Dq "options HZ=1000" 946to reduce 947the granularity to 1ms or less). 948Default value is 0, meaning no delay. 949.It Cm queue Brq Ar slots | size Ns Cm Kbytes 950Queue size, in 951.Ar slots 952or 953.Cm KBytes . 954Default value is 50 slots, which 955is the typical queue size for Ethernet devices. 956Note that for slow speed links you should keep the queue 957size short or your traffic might be affected by a significant 958queueing delay. 959E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit 960or 20s of queue on a 30Kbit/s pipe. 961Even worse effect can result if you get packets from an 962interface with a much larger MTU, e.g. the loopback interface 963with its 16KB packets. 964.It Cm plr Ar packet-loss-rate 965Packet loss rate. 966Argument 967.Ar packet-loss-rate 968is a floating-point number between 0 and 1, with 0 meaning no 969loss, 1 meaning 100% loss. 970The loss rate is internally represented on 31 bits. 971.It Cm mask Ar mask-specifier 972The 973.Xr dummynet 4 974lets you to create per-flow queues. 975A flow identifier is constructed by masking the IP addresses, 976ports and protocol types as specified in the pipe configuration. 977Packets with the same identifier after masking fall into the 978same queue. 979Available mask specifiers are a combination of the following: 980.Cm dst-ip Ar mask , 981.Cm src-ip Ar mask , 982.Cm dst-port Ar mask , 983.Cm src-port Ar mask , 984.Cm proto Ar mask 985or 986.Cm all , 987where the latter means all bits in all fields are significant. 988When used within a 989.Ar pipe 990configuration, each flow is assigned a rate equal 991to the rate of the pipe. 992When used within a 993.Ar queue 994configuration, each flow is assigned a weight equal to the 995weight of the queue, and all flows insisting on the same pipe 996share bandwidth proportionally to their weight. 997.It Cm buckets Ar hash-table-size 998Specifies the size of the hash table used for storing the 999various queues. 1000Default value is 64 controlled by the 1001.Xr sysctl 8 1002variable 1003.Em net.inet.ip.dummynet.hash_size , 1004allowed range is 16 to 1024. 1005.It Cm pipe Ar pipe_nr 1006Connects a queue to the specified pipe. 1007Multiple queues (usually 1008with different weights) can be connected to the same pipe, which 1009specifies the aggregate rate for the set of queues. 1010.It Cm weight Ar weight 1011Specifies the weight to be used for flows matching this queue. 1012The weight must be in the range 1..100, and defaults to 1. 1013.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p 1014Make use of the RED (Random Early Detection) queue management algorithm. 1015.Ar w_q 1016and 1017.Ar max_p 1018are floating 1019point numbers between 0 and 1 (0 not included), while 1020.Ar min_th 1021and 1022.Ar max_th 1023are integer numbers specifying thresholds for queue management 1024(thresholds are computed in bytes if the queue has been defined 1025in bytes, in slots otherwise). 1026The 1027.Xr dummynet 4 1028also supports the gentle RED variant (gred). 1029Three 1030.Xr sysctl 8 1031variables can be used to control the RED behaviour: 1032.Bl -tag -width indent 1033.It Em net.inet.ip.dummynet.red_lookup_depth 1034specifies the accuracy in computing the average queue 1035when the link is idle (defaults to 256, must be greater than zero) 1036.It Em net.inet.ip.dummynet.red_avg_pkt_size 1037specifies the expected average packet size (defaults to 512, must be 1038greater than zero) 1039.It Em net.inet.ip.dummynet.red_max_pkt_size 1040specifies the expected maximum packet size, only used when queue 1041thresholds are in bytes (defaults to 1500, must be greater than zero). 1042.El 1043.El 1044.Sh CHECKLIST 1045Here are some important points to consider when designing your 1046rules: 1047.Bl -bullet 1048.It 1049Remember that you filter both packets going 1050.Cm in 1051and 1052.Cm out . 1053Most connections need packets going in both directions. 1054.It 1055Remember to test very carefully. 1056It is a good idea to be near the console when doing this. 1057If you cannot be near the console, 1058use an auto-recovery script such as the one in 1059.Pa /usr/share/examples/ipfw/change_rules.sh . 1060.It 1061Don't forget the loopback interface. 1062.El 1063.Sh FINE POINTS 1064.Bl -bullet 1065.It 1066There is one kind of packet that the firewall will always 1067discard, that is a TCP packet's fragment with a fragment offset of 1068one. 1069This is a valid packet, but it only has one use, to try 1070to circumvent firewalls. 1071When logging is enabled, these packets are 1072reported as being dropped by rule -1. 1073.It 1074If you are logged in over a network, loading the 1075.Xr kld 4 1076version of 1077.Nm 1078is probably not as straightforward as you would think. 1079I recommend the following command line: 1080.Bd -literal -offset indent 1081kldload /modules/ipfw.ko && \e 1082ipfw add 32000 allow ip from any to any 1083.Ed 1084.Pp 1085Along the same lines, doing an 1086.Bd -literal -offset indent 1087ipfw flush 1088.Ed 1089.Pp 1090in similar surroundings is also a bad idea. 1091.It 1092The 1093.Nm 1094filter list may not be modified if the system security level 1095is set to 3 or higher 1096(see 1097.Xr init 8 1098for information on system security levels). 1099.El 1100.Sh PACKET DIVERSION 1101A 1102.Xr divert 4 1103socket bound to the specified port will receive all packets 1104diverted to that port. 1105If no socket is bound to the destination port, or if the kernel 1106wasn't compiled with divert socket support, the packets are 1107dropped. 1108.Sh SYSCTL VARIABLES 1109A set of 1110.Xr sysctl 8 1111variables controls the behaviour of the firewall. 1112These are shown below together with their default value 1113(but always check with the 1114.Xr sysctl 8 1115command what value is actually in use) and meaning: 1116.Bl -tag -width indent 1117.It Em net.inet.ip.fw.debug : No 1 1118Controls debugging messages produced by 1119.Nm . 1120.It Em net.inet.ip.fw.one_pass : No 1 1121When set, the packet exiting from the 1122.Xr dummynet 4 1123pipe is not passed though the firewall again. 1124Otherwise, after a pipe action, the packet is 1125reinjected into the firewall at the next rule. 1126.It Em net.inet.ip.fw.verbose : No 1 1127Enables verbose messages. 1128.It Em net.inet.ip.fw.enable : No 1 1129Enables the firewall. 1130Setting this variable to 0 lets you run your machine without 1131firewall even if compiled in. 1132.It Em net.inet.ip.fw.verbose_limit : No 0 1133Limits the number of messages produced by a verbose firewall. 1134.It Em net.inet.ip.fw.dyn_buckets : No 256 1135.It Em net.inet.ip.fw.curr_dyn_buckets : No 256 1136The configured and current size of the hash table used to 1137hold dynamic rules. 1138This must be a power of 2. 1139The table can only be resized when empty, so in order to 1140resize it on the fly you will probably have to 1141.Cm flush 1142and reload the ruleset. 1143.It Em net.inet.ip.fw.dyn_count : No 3 1144Current number of dynamic rules 1145(read-only). 1146.It Em net.inet.ip.fw.dyn_max : No 1000 1147Maximum number of dynamic rules. 1148When you hit this limit, no more dynamic rules can be 1149installed until old ones expire. 1150.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300 1151.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20 1152.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1 1153.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1 1154.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5 1155.It Em net.inet.ip.fw.dyn_short_lifetime : No 30 1156These variables control the lifetime, in seconds, of dynamic 1157rules. 1158Upon the initial SYN exchange the lifetime is kept short, 1159then increased after both SYN have been seen, then decreased 1160again during the final FIN exchange or when a RST 1161.El 1162.Sh EXAMPLES 1163This command adds an entry which denies all tcp packets from 1164.Em cracker.evil.org 1165to the telnet port of 1166.Em wolf.tambov.su 1167from being forwarded by the host: 1168.Pp 1169.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet" 1170.Pp 1171This one disallows any connection from the entire crackers 1172network to my host: 1173.Pp 1174.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org" 1175.Pp 1176A first and efficient way to limit access (not using dynamic rules) 1177is the use of the following rules: 1178.Pp 1179.Dl "ipfw add allow tcp from any to any established" 1180.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup" 1181.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup" 1182.Dl "..." 1183.Dl "ipfw add deny tcp from any to any" 1184.Pp 1185The first rule will be a quick match for normal TCP packets, 1186but it will not match the initial SYN packet, which will be 1187matched by the 1188.Cm setup 1189rules only for selected source/destination pairs. 1190All other SYN packets will be rejected by the final 1191.Cm deny 1192rule. 1193.Pp 1194In order to protect a site from flood attacks involving fake 1195TCP packets, it is safer to use dynamic rules: 1196.Pp 1197.Dl "ipfw add check-state" 1198.Dl "ipfw add deny tcp from any to any established" 1199.Dl "ipfw add allow tcp from my-net to any setup keep-state" 1200.Pp 1201This will let the firewall install dynamic rules only for 1202those connection which start with a regular SYN packet coming 1203from the inside of our network. 1204Dynamic rules are checked when encountering the first 1205.Cm check-state 1206or 1207.Cm keep-state 1208rule. 1209A 1210.Cm check-state 1211rule should be usually placed near the beginning of the 1212ruleset to minimize the amount of work scanning the ruleset. 1213Your mileage may vary. 1214.Pp 1215To limit the number of connections a user can open 1216you can use the following type of rules: 1217.Pp 1218.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10" 1219.Dl "ipfw add allow tcp from any to me setup limit src-addr 4" 1220.Pp 1221The former (assuming it runs on a gateway) will allow each host 1222on a /24 network to open at most 10 TCP connections. 1223The latter can be placed on a server to make sure that a single 1224client does not use more than 4 simultaneous connections. 1225.Pp 1226.Em BEWARE : 1227stateful rules can be subject to denial-of-service attacks 1228by a SYN-flood which opens a huge number of dynamic rules. 1229The effects of such attacks can be partially limited by 1230acting on a set of 1231.Xr sysctl 8 1232variables which control the operation of the firewall. 1233.Pp 1234Here is a good usage of the 1235.Cm list 1236command to see accounting records and timestamp information: 1237.Pp 1238.Dl ipfw -at list 1239.Pp 1240or in short form without timestamps: 1241.Pp 1242.Dl ipfw -a list 1243.Pp 1244Next rule diverts all incoming packets from 192.168.2.0/24 1245to divert port 5000: 1246.Pp 1247.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in 1248.Pp 1249The following rules show some of the applications of 1250.Nm 1251and 1252.Xr dummynet 4 1253for simulations and the like. 1254.Pp 1255This rule drops random incoming packets with a probability 1256of 5%: 1257.Pp 1258.Dl "ipfw add prob 0.05 deny ip from any to any in" 1259.Pp 1260A similar effect can be achieved making use of dummynet pipes: 1261.Pp 1262.Dl "ipfw add pipe 10 ip from any to any" 1263.Dl "ipfw pipe 10 config plr 0.05" 1264.Pp 1265We can use pipes to artificially limit bandwidth, e.g. on a 1266machine acting as a router, if we want to limit traffic from 1267local clients on 192.168.2.0/24 we do: 1268.Pp 1269.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 1270.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 1271.Pp 1272note that we use the 1273.Cm out 1274modifier so that the rule is not used twice. 1275Remember in fact that 1276.Nm 1277rules are checked both on incoming and outgoing packets. 1278.Pp 1279Should we like to simulate a bidirectional link with bandwidth 1280limitations, the correct way is the following: 1281.Pp 1282.Dl "ipfw add pipe 1 ip from any to any out" 1283.Dl "ipfw add pipe 2 ip from any to any in" 1284.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes" 1285.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes" 1286.Pp 1287The above can be very useful, e.g. if you want to see how 1288your fancy Web page will look for a residential user which 1289is connected only through a slow link. 1290You should not use only one pipe for both directions, unless 1291you want to simulate a half-duplex medium (e.g. AppleTalk, 1292Ethernet, IRDA). 1293It is not necessary that both pipes have the same configuration, 1294so we can also simulate asymmetric links. 1295.Pp 1296Should we like to verify network performance with the RED queue 1297management algorithm: 1298.Pp 1299.Dl "ipfw add pipe 1 ip from any to any" 1300.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1" 1301.Pp 1302Another typical application of the traffic shaper is to 1303introduce some delay in the communication. 1304This can affect a lot applications which do a lot of Remote 1305Procedure Calls, and where the round-trip-time of the 1306connection often becomes a limiting factor much more than 1307bandwidth: 1308.Pp 1309.Dl "ipfw add pipe 1 ip from any to any out" 1310.Dl "ipfw add pipe 2 ip from any to any in" 1311.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s" 1312.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s" 1313.Pp 1314Per-flow queueing can be useful for a variety of purposes. 1315A very simple one is counting traffic: 1316.Pp 1317.Dl "ipfw add pipe 1 tcp from any to any" 1318.Dl "ipfw add pipe 1 udp from any to any" 1319.Dl "ipfw add pipe 1 ip from any to any" 1320.Dl "ipfw pipe 1 config mask all" 1321.Pp 1322The above set of rules will create queues (and collect 1323statistics) for all traffic. 1324Because the pipes have no limitations, the only effect is 1325collecting statistics. 1326Note that we need 3 rules, not just the last one, because 1327when 1328.Nm 1329tries to match IP packets it will not consider ports, so we 1330would not see connections on separate ports as different 1331ones. 1332.Pp 1333A more sophisticated example is limiting the outbound traffic 1334on a net with per-host limits, rather than per-network limits: 1335.Pp 1336.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 1337.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in" 1338.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 1339.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 1340.Sh SEE ALSO 1341.Xr cpp 1 , 1342.Xr m4 1 , 1343.Xr bridge 4 , 1344.Xr divert 4 , 1345.Xr dummynet 4 , 1346.Xr ip 4 , 1347.Xr ipfirewall 4 , 1348.Xr protocols 5 , 1349.Xr services 5 , 1350.Xr init 8 , 1351.Xr kldload 8 , 1352.Xr reboot 8 , 1353.Xr sysctl 8 , 1354.Xr syslogd 8 1355.Sh BUGS 1356The syntax has grown over the years and it is not very clean. 1357.Pp 1358.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!! 1359.Pp 1360This program can put your computer in rather unusable state. 1361When using it for the first time, work on the console of the 1362computer, and do 1363.Em NOT 1364do anything you don't understand. 1365.Pp 1366When manipulating/adding chain entries, service and protocol names 1367are not accepted. 1368.Pp 1369Incoming packet fragments diverted by 1370.Cm divert 1371or 1372.Cm tee 1373are reassembled before delivery to the socket. 1374.Pp 1375Packets that match a 1376.Cm tee 1377rule should not be immediately accepted, but should continue 1378going through the rule list. 1379This may be fixed in a later version. 1380.Sh AUTHORS 1381.An Ugen J. S. Antsilevich , 1382.An Poul-Henning Kamp , 1383.An Alex Nash , 1384.An Archie Cobbs , 1385.An Luigi Rizzo . 1386.Pp 1387.An -nosplit 1388API based upon code written by 1389.An Daniel Boulet 1390for BSDI. 1391.Pp 1392Work on 1393.Xr dummynet 4 1394traffic shaper supported by Akamba Corp. 1395.Sh HISTORY 1396The 1397.Nm 1398utility first appeared in 1399.Fx 2.0 . 1400.Xr dummynet 4 1401was introduced in 1402.Fx 2.2.8 . 1403Stateful extensions were introduced in 1404.Fx 4.0 . 1405