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