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