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