1.\" 2.\" $FreeBSD$ 3.\" 4.Dd December 1, 2003 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 cq 13.Cm add 14.Ar rule 15.Nm 16.Op Fl acdefnNStT 17.Brq Cm list | show 18.Op Ar rule | first-last ... 19.Nm 20.Op Fl f | q 21.Cm flush 22.Nm 23.Op Fl q 24.Brq Cm delete | zero | resetlog 25.Op Cm set 26.Op Ar number ... 27.Nm 28.Cm enable 29.Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive 30.Nm 31.Cm disable 32.Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive 33.Pp 34.Nm 35.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ... 36.Nm 37.Cm set move 38.Op Cm rule 39.Ar number Cm to Ar number 40.Nm 41.Cm set swap Ar number number 42.Nm 43.Cm set show 44.Pp 45.Nm 46.Brq Cm pipe | queue 47.Ar number 48.Cm config 49.Ar config-options 50.Nm 51.Op Fl s Op Ar field 52.Brq Cm pipe | queue 53.Brq Cm delete | list | show 54.Op Ar number ... 55.Pp 56.Nm 57.Op Fl cfnNqS 58.Oo 59.Fl p Ar preproc 60.Oo 61.Ar preproc-flags 62.Oc 63.Oc 64.Ar pathname 65.Sh DESCRIPTION 66The 67.Nm 68utility is the user interface for controlling the 69.Xr ipfw 4 70firewall and the 71.Xr dummynet 4 72traffic shaper in 73.Fx . 74.Pp 75.Bd -ragged -offset XXXX 76.Em NOTE: 77this manual page documents the newer version of 78.Nm 79introduced in 80.Fx 81CURRENT in July 2002, also known as 82.Nm ipfw2 . 83.Nm ipfw2 84is a superset of the old firewall, 85.Nm ipfw1 . 86The differences between the two are listed in Section 87.Sx IPFW2 ENHANCEMENTS , 88which you are encouraged to read to revise older rulesets and possibly 89write them more efficiently. 90See Section 91.Sx USING IPFW2 IN FreeBSD-STABLE 92for instructions on how to run 93.Nm ipfw2 94on 95.Fx 96STABLE. 97.Ed 98.Pp 99An 100.Nm 101configuration, or 102.Em ruleset , 103is made of a list of 104.Em rules 105numbered from 1 to 65535. 106Packets are passed to 107.Nm 108from a number of different places in the protocol stack 109(depending on the source and destination of the packet, 110it is possible that 111.Nm 112is invoked multiple times on the same packet). 113The packet passed to the firewall is compared 114against each of the rules in the firewall 115.Em ruleset . 116When a match is found, the action corresponding to the 117matching rule is performed. 118.Pp 119Depending on the action and certain system settings, packets 120can be reinjected into the firewall at some rule after the 121matching one for further processing. 122.Pp 123An 124.Nm 125ruleset always includes a 126.Em default 127rule (numbered 65535) which cannot be modified or deleted, 128and matches all packets. 129The action associated with the 130.Em default 131rule can be either 132.Cm deny 133or 134.Cm allow 135depending on how the kernel is configured. 136.Pp 137If the ruleset includes one or more rules with the 138.Cm keep-state 139or 140.Cm limit 141option, then 142.Nm 143assumes a 144.Em stateful 145behaviour, i.e. upon a match it will create dynamic rules matching 146the exact parameters (addresses and ports) of the matching packet. 147.Pp 148These dynamic rules, which have a limited lifetime, are checked 149at the first occurrence of a 150.Cm check-state , 151.Cm keep-state 152or 153.Cm limit 154rule, and are typically used to open the firewall on-demand to 155legitimate traffic only. 156See the 157.Sx STATEFUL FIREWALL 158and 159.Sx EXAMPLES 160Sections below for more information on the stateful behaviour of 161.Nm . 162.Pp 163All rules (including dynamic ones) have a few associated counters: 164a packet count, a byte count, a log count and a timestamp 165indicating the time of the last match. 166Counters can be displayed or reset with 167.Nm 168commands. 169.Pp 170Rules can be added with the 171.Cm add 172command; deleted individually or in groups with the 173.Cm delete 174command, and globally (except those in set 31) with the 175.Cm flush 176command; displayed, optionally with the content of the 177counters, using the 178.Cm show 179and 180.Cm list 181commands. 182Finally, counters can be reset with the 183.Cm zero 184and 185.Cm resetlog 186commands. 187.Pp 188Also, each rule belongs to one of 32 different 189.Em sets 190, and there are 191.Nm 192commands to atomically manipulate sets, such as enable, 193disable, swap sets, move all rules in a set to another 194one, delete all rules in a set. These can be useful to 195install temporary configurations, or to test them. 196See Section 197.Sx SETS OF RULES 198for more information on 199.Em sets . 200.Pp 201The following options are available: 202.Bl -tag -width indent 203.It Fl a 204While listing, show counter values. 205The 206.Cm show 207command just implies this option. 208.It Fl b 209Only show the action and the comment, not the body of a rule. 210Implies 211.Fl c . 212.It Fl c 213When entering or showing rules, print them in compact form, 214i.e. without the optional "ip from any to any" string 215when this does not carry any additional information. 216.It Fl d 217While listing, show dynamic rules in addition to static ones. 218.It Fl e 219While listing, if the 220.Fl d 221option was specified, also show expired dynamic rules. 222.It Fl f 223Don't ask for confirmation for commands that can cause problems 224if misused, 225.No i.e. Cm flush . 226If there is no tty associated with the process, this is implied. 227.It Fl n 228Only check syntax of the command strings, without actually passing 229them to the kernel. 230.It Fl N 231Try to resolve addresses and service names in output. 232.It Fl q 233While 234.Cm add Ns ing , 235.Cm zero Ns ing , 236.Cm resetlog Ns ging 237or 238.Cm flush Ns ing , 239be quiet about actions 240(implies 241.Fl f ) . 242This is useful for adjusting rules by executing multiple 243.Nm 244commands in a script 245(e.g., 246.Ql sh\ /etc/rc.firewall ) , 247or by processing a file of many 248.Nm 249rules across a remote login session. 250If a 251.Cm flush 252is performed in normal (verbose) mode (with the default kernel 253configuration), it prints a message. 254Because all rules are flushed, the message might not be delivered 255to the login session, causing the remote login session to be closed 256and the remainder of the ruleset to not be processed. 257Access to the console would then be required to recover. 258.It Fl S 259While listing rules, show the 260.Em set 261each rule belongs to. 262If this flag is not specified, disabled rules will not be 263listed. 264.It Fl s Op Ar field 265While listing pipes, sort according to one of the four 266counters (total or current packets or bytes). 267.It Fl t 268While listing, show last match timestamp (converted with ctime()). 269.It Fl T 270While listing, show last match timestamp (as seconds from the epoch). 271This form can be more convenient for postprocessing by scripts. 272.El 273.Pp 274To ease configuration, rules can be put into a file which is 275processed using 276.Nm 277as shown in the last synopsis line. 278An absolute 279.Ar pathname 280must be used. 281The file will be read line by line and applied as arguments to the 282.Nm 283utility. 284.Pp 285Optionally, a preprocessor can be specified using 286.Fl p Ar preproc 287where 288.Ar pathname 289is to be piped through. 290Useful preprocessors include 291.Xr cpp 1 292and 293.Xr m4 1 . 294If 295.Ar preproc 296doesn't start with a slash 297.Pq Ql / 298as its first character, the usual 299.Ev PATH 300name search is performed. 301Care should be taken with this in environments where not all 302file systems are mounted (yet) by the time 303.Nm 304is being run (e.g. when they are mounted over NFS). 305Once 306.Fl p 307has been specified, any additional arguments as passed on to the preprocessor 308for interpretation. 309This allows for flexible configuration files (like conditionalizing 310them on the local hostname) and the use of macros to centralize 311frequently required arguments like IP addresses. 312.Pp 313The 314.Nm 315.Cm pipe 316and 317.Cm queue 318commands are used to configure the traffic shaper, as shown in the 319.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 320Section below. 321.Pp 322If the world and the kernel get out of sync the 323.Nm 324ABI may break, preventing you from being able to add any rules. This can 325adversely effect the booting process. You can use 326.Nm 327.Cm disable 328.Cm firewall 329to temporarily disable the firewall to regain access to the network, 330allowing you to fix the problem. 331.Sh PACKET FLOW 332A packet is checked against the active ruleset in multiple places 333in the protocol stack, under control of several sysctl variables. 334These places and variables are shown below, and it is important to 335have this picture in mind in order to design a correct ruleset. 336.Bd -literal -offset indent 337 ^ to upper layers V 338 | | 339 +----------->-----------+ 340 ^ V 341 [ip_input] [ip_output] net.inet.ip.fw.enable=1 342 | | 343 ^ V 344[ether_demux] [ether_output_frame] net.link.ether.ipfw=1 345 | | 346 +-->--[bdg_forward]-->--+ net.link.ether.bridge_ipfw=1 347 ^ V 348 | to devices | 349.Ed 350.Pp 351As can be noted from the above picture, the number of 352times the same packet goes through the firewall can 353vary between 0 and 4 depending on packet source and 354destination, and system configuration. 355.Pp 356Note that as packets flow through the stack, headers can be 357stripped or added to it, and so they may or may not be available 358for inspection. 359E.g., incoming packets will include the MAC header when 360.Nm 361is invoked from 362.Cm ether_demux() , 363but the same packets will have the MAC header stripped off when 364.Nm 365is invoked from 366.Cm ip_input() . 367.Pp 368Also note that each packet is always checked against the complete ruleset, 369irrespective of the place where the check occurs, or the source of the packet. 370If a rule contains some match patterns or actions which are not valid 371for the place of invocation (e.g. trying to match a MAC header within 372.Cm ip_input() 373), the match pattern will not match, but a 374.Cm not 375operator in front of such patterns 376.Em will 377cause the pattern to 378.Em always 379match on those packets. 380It is thus the responsibility of 381the programmer, if necessary, to write a suitable ruleset to 382differentiate among the possible places. 383.Cm skipto 384rules can be useful here, as an example: 385.Bd -literal -offset indent 386# packets from ether_demux or bdg_forward 387ipfw add 10 skipto 1000 all from any to any layer2 in 388# packets from ip_input 389ipfw add 10 skipto 2000 all from any to any not layer2 in 390# packets from ip_output 391ipfw add 10 skipto 3000 all from any to any not layer2 out 392# packets from ether_output_frame 393ipfw add 10 skipto 4000 all from any to any layer2 out 394.Ed 395.Pp 396(yes, at the moment there is no way to differentiate between 397ether_demux and bdg_forward). 398.Sh SYNTAX 399In general, each keyword or argument must be provided as 400a separate command line argument, with no leading or trailing 401spaces. Keywords are case-sensitive, whereas arguments may 402or may not be case-sensitive depending on their nature 403(e.g. uid's are, hostnames are not). 404.Pp 405In 406.Nm ipfw2 407you can introduce spaces after commas ',' to make 408the line more readable. You can also put the entire 409command (including flags) into a single argument. 410E.g. the following forms are equivalent: 411.Bd -literal -offset indent 412ipfw -q add deny src-ip 10.0.0.0/24,127.0.0.1/8 413ipfw -q add deny src-ip 10.0.0.0/24, 127.0.0.1/8 414ipfw "-q add deny src-ip 10.0.0.0/24, 127.0.0.1/8" 415.Ed 416.Sh RULE FORMAT 417The format of 418.Nm 419rules is the following: 420.Bd -ragged -offset indent 421.Op Ar rule_number 422.Op Cm set Ar set_number 423.Op Cm prob Ar match_probability 424.br 425.Ar " " action 426.Op Cm log Op Cm logamount Ar number 427.Ar body 428.Ed 429.Pp 430where the body of the rule specifies which information is used 431for filtering packets, among the following: 432.Pp 433.Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact 434.It Layer-2 header fields 435When available 436.It IPv4 Protocol 437TCP, UDP, ICMP, etc. 438.It Source and dest. addresses and ports 439.It Direction 440See Section 441.Sx PACKET FLOW 442.It Transmit and receive interface 443By name or address 444.It Misc. IP header fields 445Version, type of service, datagram length, identification, 446fragment flag (non-zero IP offset), 447Time To Live 448.It IP options 449.It Misc. TCP header fields 450TCP flags (SYN, FIN, ACK, RST, etc.), 451sequence number, acknowledgment number, 452window 453.It TCP options 454.It ICMP types 455for ICMP packets 456.It User/group ID 457When the packet can be associated with a local socket. 458.El 459.Pp 460Note that some of the above information, e.g. source MAC or IP addresses and 461TCP/UDP ports, could easily be spoofed, so filtering on those fields 462alone might not guarantee the desired results. 463.Bl -tag -width indent 464.It Ar rule_number 465Each rule is associated with a 466.Ar rule_number 467in the range 1..65535, with the latter reserved for the 468.Em default 469rule. 470Rules are checked sequentially by rule number. 471Multiple rules can have the same number, in which case they are 472checked (and listed) according to the order in which they have 473been added. 474If a rule is entered without specifying a number, the kernel will 475assign one in such a way that the rule becomes the last one 476before the 477.Em default 478rule. 479Automatic rule numbers are assigned by incrementing the last 480non-default rule number by the value of the sysctl variable 481.Ar net.inet.ip.fw.autoinc_step 482which defaults to 100. 483If this is not possible (e.g. because we would go beyond the 484maximum allowed rule number), the number of the last 485non-default value is used instead. 486.It Cm set Ar set_number 487Each rule is associated with a 488.Ar set_number 489in the range 0..31. 490Sets can be individually disabled and enabled, so this parameter 491is of fundamental importance for atomic ruleset manipulation. 492It can be also used to simplify deletion of groups of rules. 493If a rule is entered without specifying a set number, 494set 0 will be used. 495.br 496Set 31 is special in that it cannot be disabled, 497and rules in set 31 are not deleted by the 498.Nm ipfw flush 499command (but you can delete them with the 500.Nm ipfw delete set 31 501command). 502Set 31 is also used for the 503.Em default 504rule. 505.It Cm prob Ar match_probability 506A match is only declared with the specified probability 507(floating point number between 0 and 1). 508This can be useful for a number of applications such as 509random packet drop or 510(in conjunction with 511.Xr dummynet 4 ) 512to simulate the effect of multiple paths leading to out-of-order 513packet delivery. 514.Pp 515Note: this condition is checked before any other condition, including 516ones such as keep-state or check-state which might have side effects. 517.It Cm log Op Cm logamount Ar number 518When a packet matches a rule with the 519.Cm log 520keyword, a message will be 521logged to 522.Xr syslogd 8 523with a 524.Dv LOG_SECURITY 525facility. 526The logging only occurs if the sysctl variable 527.Em net.inet.ip.fw.verbose 528is set to 1 529(which is the default when the kernel is compiled with 530.Dv IPFIREWALL_VERBOSE 531) and the number of packets logged so far for that 532particular rule does not exceed the 533.Cm logamount 534parameter. 535If no 536.Cm logamount 537is specified, the limit is taken from the sysctl variable 538.Em net.inet.ip.fw.verbose_limit . 539In both cases, a value of 0 removes the logging limit. 540.Pp 541Once the limit is reached, logging can be re-enabled by 542clearing the logging counter or the packet counter for that entry, see the 543.Cm resetlog 544command. 545.Pp 546Note: logging is done after all other packet matching conditions 547have been successfully verified, and before performing the final 548action (accept, deny, etc.) on the packet. 549.El 550.Ss RULE ACTIONS 551A rule can be associated with one of the following actions, which 552will be executed when the packet matches the body of the rule. 553.Bl -tag -width indent 554.It Cm allow | accept | pass | permit 555Allow packets that match rule. 556The search terminates. 557.It Cm check-state 558Checks the packet against the dynamic ruleset. 559If a match is found, execute the action associated with 560the rule which generated this dynamic rule, otherwise 561move to the next rule. 562.br 563.Cm Check-state 564rules do not have a body. 565If no 566.Cm check-state 567rule is found, the dynamic ruleset is checked at the first 568.Cm keep-state 569or 570.Cm limit 571rule. 572.It Cm count 573Update counters for all packets that match rule. 574The search continues with the next rule. 575.It Cm deny | drop 576Discard packets that match this rule. 577The search terminates. 578.It Cm divert Ar port 579Divert packets that match this rule to the 580.Xr divert 4 581socket bound to port 582.Ar port . 583The search terminates. 584.It Cm fwd | forward Ar ipaddr Ns Op , Ns Ar port 585Change the next-hop on matching packets to 586.Ar ipaddr , 587which can be an IP address in dotted quad format or a host name. 588The search terminates if this rule matches. 589.Pp 590If 591.Ar ipaddr 592is a local address, then matching packets will be forwarded to 593.Ar port 594(or the port number in the packet if one is not specified in the rule) 595on the local machine. 596.br 597If 598.Ar ipaddr 599is not a local address, then the port number 600(if specified) is ignored, and the packet will be 601forwarded to the remote address, using the route as found in 602the local routing table for that IP. 603.br 604A 605.Ar fwd 606rule will not match layer-2 packets (those received 607on ether_input, ether_output, or bridged). 608.br 609The 610.Cm fwd 611action does not change the contents of the packet at all. 612In particular, the destination address remains unmodified, so 613packets forwarded to another system will usually be rejected by that system 614unless there is a matching rule on that system to capture them. 615For packets forwarded locally, 616the local address of the socket will be 617set to the original destination address of the packet. 618This makes the 619.Xr netstat 1 620entry look rather weird but is intended for 621use with transparent proxy servers. 622.It Cm pipe Ar pipe_nr 623Pass packet to a 624.Xr dummynet 4 625.Dq pipe 626(for bandwidth limitation, delay, etc.). 627See the 628.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 629Section for further information. 630The search terminates; however, on exit from the pipe and if 631the 632.Xr sysctl 8 633variable 634.Em net.inet.ip.fw.one_pass 635is not set, the packet is passed again to the firewall code 636starting from the next rule. 637.It Cm queue Ar queue_nr 638Pass packet to a 639.Xr dummynet 4 640.Dq queue 641(for bandwidth limitation using WF2Q+). 642.It Cm reject 643(Deprecated). 644Synonym for 645.Cm unreach host . 646.It Cm reset 647Discard packets that match this rule, and if the 648packet is a TCP packet, try to send a TCP reset (RST) notice. 649The search terminates. 650.It Cm skipto Ar number 651Skip all subsequent rules numbered less than 652.Ar number . 653The search continues with the first rule numbered 654.Ar number 655or higher. 656.It Cm tee Ar port 657Send a copy of packets matching this rule to the 658.Xr divert 4 659socket bound to port 660.Ar port . 661The search terminates and the original packet is accepted 662(but see Section 663.Sx BUGS 664below). 665.It Cm unreach Ar code 666Discard packets that match this rule, and try to send an ICMP 667unreachable notice with code 668.Ar code , 669where 670.Ar code 671is a number from 0 to 255, or one of these aliases: 672.Cm net , host , protocol , port , 673.Cm needfrag , srcfail , net-unknown , host-unknown , 674.Cm isolated , net-prohib , host-prohib , tosnet , 675.Cm toshost , filter-prohib , host-precedence 676or 677.Cm precedence-cutoff . 678The search terminates. 679.El 680.Ss RULE BODY 681The body of a rule contains zero or more patterns (such as 682specific source and destination addresses or ports, 683protocol options, incoming or outgoing interfaces, etc.) 684that the packet must match in order to be recognised. 685In general, the patterns are connected by (implicit) 686.Cm and 687operators -- i.e. all must match in order for the 688rule to match. 689Individual patterns can be prefixed by the 690.Cm not 691operator to reverse the result of the match, as in 692.Pp 693.Dl "ipfw add 100 allow ip from not 1.2.3.4 to any" 694.Pp 695Additionally, sets of alternative match patterns ( 696.Em or-blocks 697) can be constructed by putting the patterns in 698lists enclosed between parentheses ( ) or braces { }, and 699using the 700.Cm or 701operator as follows: 702.Pp 703.Dl "ipfw add 100 allow ip from { x or not y or z } to any" 704.Pp 705Only one level of parentheses is allowed. 706Beware that most shells have special meanings for parentheses 707or braces, so it is advisable to put a backslash \\ in front of them 708to prevent such interpretations. 709.Pp 710The body of a rule must in general include a source and destination 711address specifier. 712The keyword 713.Ar any 714can be used in various places to specify that the content of 715a required field is irrelevant. 716.Pp 717The rule body has the following format: 718.Bd -ragged -offset indent 719.Op Ar proto Cm from Ar src Cm to Ar dst 720.Op Ar options 721.Ed 722.Pp 723The first part (proto from src to dst) is for backward 724compatibility with 725.Nm ipfw1 . 726In 727.Nm ipfw2 728any match pattern (including MAC headers, IPv4 protocols, 729addresses and ports) can be specified in the 730.Ar options 731section. 732.Pp 733Rule fields have the following meaning: 734.Bl -tag -width indent 735.It Ar proto : protocol | Cm { Ar protocol Cm or ... } 736.It Ar protocol : Oo Cm not Oc Ar protocol-name | protocol-number 737An IPv4 protocol specified by number or name 738(for a complete list see 739.Pa /etc/protocols ) . 740The 741.Cm ip 742or 743.Cm all 744keywords mean any protocol will match. 745.Pp 746The 747.Cm { Ar protocol Cm or ... } 748format (an 749.Em or-block ) 750is provided for convenience only but its use is deprecated. 751.It Ar src No and Ar dst : Bro Cm addr | Cm { Ar addr Cm or ... } Brc Op Oo Cm not Oc Ar ports 752An address (or a list, see below) 753optionally followed by 754.Ar ports 755specifiers. 756.Pp 757The second format ( 758.Em or-block 759with multiple addresses) is provided for convenience only and 760its use is discouraged. 761.It Ar addr : Oo Cm not Oc Brq Cm any | me | Ar addr-list | Ar addr-set 762.It Cm any 763matches any IP address. 764.It Cm me 765matches any IP address configured on an interface in the system. 766The address list is evaluated at the time the packet is 767analysed. 768.It Ar addr-list : ip-addr Ns Op Ns , Ns Ar addr-list 769.It Ar ip-addr : 770A host or subnet address specified in one of the following ways: 771.Bl -tag -width indent 772.It Ar numeric-ip | hostname 773Matches a single IPv4 address, specified as dotted-quad or a hostname. 774Hostnames are resolved at the time the rule is added to the firewall list. 775.It Ar addr Ns / Ns Ar masklen 776Matches all addresses with base 777.Ar addr 778(specified as a dotted quad or a hostname) 779and mask width of 780.Cm masklen 781bits. 782As an example, 1.2.3.4/25 will match 783all IP numbers from 1.2.3.0 to 1.2.3.127 . 784.It Ar addr Ns : Ns Ar mask 785Matches all addresses with base 786.Ar addr 787(specified as a dotted quad or a hostname) 788and the mask of 789.Ar mask , 790specified as a dotted quad. 791As an example, 1.2.3.4/255.0.255.0 will match 7921.*.3.*. 793We suggest to use this form only for non-contiguous 794masks, and resort to the 795.Ar addr Ns / Ns Ar masklen 796format for contiguous masks, which is more compact and less 797error-prone. 798.El 799.It Ar addr-set : addr Ns Oo Ns / Ns Ar masklen Oc Ns Cm { Ns Ar list Ns Cm } 800.It Ar list : Bro Ar num | num-num Brc Ns Op Ns , Ns Ar list 801Matches all addresses with base address 802.Ar addr 803(specified as a dotted quad or a hostname) 804and whose last byte is in the list between braces { } . 805Note that there must be no spaces between braces and 806numbers (spaces after commas are allowed). 807Elements of the list can be specified as single entries 808or ranges. 809The 810.Ar masklen 811field is used to limit the size of the set of addresses, 812and can have any value between 24 and 32. If not specified, 813it will be assumed as 24. 814.br 815This format is particularly useful to handle sparse address sets 816within a single rule. Because the matching occurs using a 817bitmask, it takes constant time and dramatically reduces 818the complexity of rulesets. 819.br 820As an example, an address specified as 1.2.3.4/24{128,35-55,89} 821will match the following IP addresses: 822.br 8231.2.3.128, 1.2.3.35 to 1.2.3.55, 1.2.3.89 . 824.It Ar ports : Bro Ar port | port Ns \&- Ns Ar port Ns Brc Ns Op , Ns Ar ports 825For protocols which support port numbers (such as TCP and UDP), optional 826.Cm ports 827may be specified as one or more ports or port ranges, separated 828by commas but no spaces, and an optional 829.Cm not 830operator. 831The 832.Ql \&- 833notation specifies a range of ports (including boundaries). 834.Pp 835Service names (from 836.Pa /etc/services ) 837may be used instead of numeric port values. 838The length of the port list is limited to 30 ports or ranges, 839though one can specify larger ranges by using an 840.Em or-block 841in the 842.Cm options 843section of the rule. 844.Pp 845A backslash 846.Pq Ql \e 847can be used to escape the dash 848.Pq Ql - 849character in a service name (from a shell, the backslash must be 850typed twice to avoid the shell itself interpreting it as an escape 851character). 852.Pp 853.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any" 854.Pp 855Fragmented packets which have a non-zero offset (i.e. not the first 856fragment) will never match a rule which has one or more port 857specifications. 858See the 859.Cm frag 860option for details on matching fragmented packets. 861.El 862.Ss RULE OPTIONS (MATCH PATTERNS) 863Additional match patterns can be used within 864rules. Zero or more of these so-called 865.Em options 866can be present in a rule, optionally prefixed by the 867.Cm not 868operand, and possibly grouped into 869.Em or-blocks . 870.Pp 871The following match patterns can be used (listed in alphabetical order): 872.Bl -tag -width indent 873.It Cm // this is a comment. 874Inserts the specified text as a comment in the rule. 875Everything following // is considered as a comment and stored in the rule. 876You can have comment-only rules, which are listed as having a 877.Cm count 878action followed by the comment. 879.It Cm bridged 880Matches only bridged packets. 881.It Cm dst-ip Ar ip-address 882Matches IP packets whose destination IP is one of the address(es) 883specified as argument. 884.It Cm dst-port Ar ports 885Matches IP packets whose destination port is one of the port(s) 886specified as argument. 887.It Cm established 888Matches TCP packets that have the RST or ACK bits set. 889.It Cm frag 890Matches packets that are fragments and not the first 891fragment of an IP datagram. Note that these packets will not have 892the next protocol header (e.g. TCP, UDP) so options that look into 893these headers cannot match. 894.It Cm gid Ar group 895Matches all TCP or UDP packets sent by or received for a 896.Ar group . 897A 898.Ar group 899may be specified by name or number. 900.It Cm icmptypes Ar types 901Matches ICMP packets whose ICMP type is in the list 902.Ar types . 903The list may be specified as any combination of 904individual types (numeric) separated by commas. 905.Em Ranges are not allowed. 906The supported ICMP types are: 907.Pp 908echo reply 909.Pq Cm 0 , 910destination unreachable 911.Pq Cm 3 , 912source quench 913.Pq Cm 4 , 914redirect 915.Pq Cm 5 , 916echo request 917.Pq Cm 8 , 918router advertisement 919.Pq Cm 9 , 920router solicitation 921.Pq Cm 10 , 922time-to-live exceeded 923.Pq Cm 11 , 924IP header bad 925.Pq Cm 12 , 926timestamp request 927.Pq Cm 13 , 928timestamp reply 929.Pq Cm 14 , 930information request 931.Pq Cm 15 , 932information reply 933.Pq Cm 16 , 934address mask request 935.Pq Cm 17 936and address mask reply 937.Pq Cm 18 . 938.It Cm in | out 939Matches incoming or outgoing packets, respectively. 940.Cm in 941and 942.Cm out 943are mutually exclusive (in fact, 944.Cm out 945is implemented as 946.Cm not in Ns No ). 947.It Cm ipid Ar id-list 948Matches IP packets whose 949.Cm ip_id 950field has value included in 951.Ar id-list , 952which is either a single value or a list of values or ranges 953specified in the same way as 954.Ar ports . 955.It Cm iplen Ar len-list 956Matches IP packets whose total length, including header and data, is 957in the set 958.Ar len-list , 959which is either a single value or a list of values or ranges 960specified in the same way as 961.Ar ports . 962.It Cm ipoptions Ar spec 963Matches packets whose IP header contains the comma separated list of 964options specified in 965.Ar spec . 966The supported IP options are: 967.Pp 968.Cm ssrr 969(strict source route), 970.Cm lsrr 971(loose source route), 972.Cm rr 973(record packet route) and 974.Cm ts 975(timestamp). 976The absence of a particular option may be denoted 977with a 978.Ql \&! . 979.It Cm ipprecedence Ar precedence 980Matches IP packets whose precedence field is equal to 981.Ar precedence . 982.It Cm ipsec 983Matches packets that have IPSEC history associated with them 984(i.e. the packet comes encapsulated in IPSEC, the kernel 985has IPSEC support and IPSEC_FILTERGIF option, and can correctly 986decapsulate it). 987.Pp 988Note that specifying 989.Cm ipsec 990is different from specifying 991.Cm proto Ar ipsec 992as the latter will only look at the specific IP protocol field, 993irrespective of IPSEC kernel support and the validity of the IPSEC data. 994.Pp 995Further note that this flag is silently ignored in kernels without 996IPSEC support. 997It does not affect rule processing when given and the 998rules are handled as if with no 999.Cm ipsec 1000flag. 1001.It Cm iptos Ar spec 1002Matches IP packets whose 1003.Cm tos 1004field contains the comma separated list of 1005service types specified in 1006.Ar spec . 1007The supported IP types of service are: 1008.Pp 1009.Cm lowdelay 1010.Pq Dv IPTOS_LOWDELAY , 1011.Cm throughput 1012.Pq Dv IPTOS_THROUGHPUT , 1013.Cm reliability 1014.Pq Dv IPTOS_RELIABILITY , 1015.Cm mincost 1016.Pq Dv IPTOS_MINCOST , 1017.Cm congestion 1018.Pq Dv IPTOS_CE . 1019The absence of a particular type may be denoted 1020with a 1021.Ql \&! . 1022.It Cm ipttl Ar ttl-list 1023Matches IP packets whose time to live is included in 1024.Ar ttl-list , 1025which is either a single value or a list of values or ranges 1026specified in the same way as 1027.Ar ports . 1028.It Cm ipversion Ar ver 1029Matches IP packets whose IP version field is 1030.Ar ver . 1031.It Cm keep-state 1032Upon a match, the firewall will create a dynamic rule, whose 1033default behaviour is to match bidirectional traffic between 1034source and destination IP/port using the same protocol. 1035The rule has a limited lifetime (controlled by a set of 1036.Xr sysctl 8 1037variables), and the lifetime is refreshed every time a matching 1038packet is found. 1039.It Cm layer2 1040Matches only layer2 packets, i.e. those passed to 1041.Nm 1042from ether_demux() and ether_output_frame(). 1043.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N 1044The firewall will only allow 1045.Ar N 1046connections with the same 1047set of parameters as specified in the rule. 1048One or more 1049of source and destination addresses and ports can be 1050specified. 1051.It Cm { MAC | mac } Ar dst-mac src-mac 1052Match packets with a given 1053.Ar dst-mac 1054and 1055.Ar src-mac 1056addresses, specified as the 1057.Cm any 1058keyword (matching any MAC address), or six groups of hex digits 1059separated by colons, 1060and optionally followed by a mask indicating the significant bits. 1061The mask may be specified using either of the following methods: 1062.Bl -enum -width indent 1063.It 1064A slash 1065.Pq / 1066followed by the number of significant bits. 1067For example, an address with 33 significant bits could be specified as: 1068.Pp 1069.Dl "MAC 10:20:30:40:50:60/33 any" 1070.Pp 1071.It 1072An ampersand 1073.Pq & 1074followed by a bitmask specified as six groups of hex digits separated 1075by colons. 1076For example, an address in which the last 16 bits are significant could 1077be specified as: 1078.Pp 1079.Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any" 1080.Pp 1081Note that the ampersand character has a special meaning in many shells 1082and should generally be escaped. 1083.Pp 1084.El 1085Note that the order of MAC addresses (destination first, 1086source second) is 1087the same as on the wire, but the opposite of the one used for 1088IP addresses. 1089.It Cm mac-type Ar mac-type 1090Matches packets whose Ethernet Type field 1091corresponds to one of those specified as argument. 1092.Ar mac-type 1093is specified in the same way as 1094.Cm port numbers 1095(i.e. one or more comma-separated single values or ranges). 1096You can use symbolic names for known values such as 1097.Em vlan , ipv4, ipv6 . 1098Values can be entered as decimal or hexadecimal (if prefixed by 0x), 1099and they are always printed as hexadecimal (unless the 1100.Cm -N 1101option is used, in which case symbolic resolution will be attempted). 1102.It Cm proto Ar protocol 1103Matches packets with the corresponding IPv4 protocol. 1104.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any 1105Matches packets received, transmitted or going through, 1106respectively, the interface specified by exact name 1107.Ns No ( Ar ifX Ns No ), 1108by device name 1109.Ns No ( Ar if Ns Ar * Ns No ), 1110by IP address, or through some interface. 1111.Pp 1112The 1113.Cm via 1114keyword causes the interface to always be checked. 1115If 1116.Cm recv 1117or 1118.Cm xmit 1119is used instead of 1120.Cm via , 1121then only the receive or transmit interface (respectively) 1122is checked. 1123By specifying both, it is possible to match packets based on 1124both receive and transmit interface, e.g.: 1125.Pp 1126.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1" 1127.Pp 1128The 1129.Cm recv 1130interface can be tested on either incoming or outgoing packets, 1131while the 1132.Cm xmit 1133interface can only be tested on outgoing packets. 1134So 1135.Cm out 1136is required (and 1137.Cm in 1138is invalid) whenever 1139.Cm xmit 1140is used. 1141.Pp 1142A packet may not have a receive or transmit interface: packets 1143originating from the local host have no receive interface, 1144while packets destined for the local host have no transmit 1145interface. 1146.It Cm setup 1147Matches TCP packets that have the SYN bit set but no ACK bit. 1148This is the short form of 1149.Dq Li tcpflags\ syn,!ack . 1150.It Cm src-ip Ar ip-address 1151Matches IP packets whose source IP is one of the address(es) 1152specified as argument. 1153.It Cm src-port Ar ports 1154Matches IP packets whose source port is one of the port(s) 1155specified as argument. 1156.It Cm tcpack Ar ack 1157TCP packets only. 1158Match if the TCP header acknowledgment number field is set to 1159.Ar ack . 1160.It Cm tcpflags Ar spec 1161TCP packets only. 1162Match if the TCP header contains the comma separated list of 1163flags specified in 1164.Ar spec . 1165The supported TCP flags are: 1166.Pp 1167.Cm fin , 1168.Cm syn , 1169.Cm rst , 1170.Cm psh , 1171.Cm ack 1172and 1173.Cm urg . 1174The absence of a particular flag may be denoted 1175with a 1176.Ql \&! . 1177A rule which contains a 1178.Cm tcpflags 1179specification can never match a fragmented packet which has 1180a non-zero offset. 1181See the 1182.Cm frag 1183option for details on matching fragmented packets. 1184.It Cm tcpseq Ar seq 1185TCP packets only. 1186Match if the TCP header sequence number field is set to 1187.Ar seq . 1188.It Cm tcpwin Ar win 1189TCP packets only. 1190Match if the TCP header window field is set to 1191.Ar win . 1192.It Cm tcpoptions Ar spec 1193TCP packets only. 1194Match if the TCP header contains the comma separated list of 1195options specified in 1196.Ar spec . 1197The supported TCP options are: 1198.Pp 1199.Cm mss 1200(maximum segment size), 1201.Cm window 1202(tcp window advertisement), 1203.Cm sack 1204(selective ack), 1205.Cm ts 1206(rfc1323 timestamp) and 1207.Cm cc 1208(rfc1644 t/tcp connection count). 1209The absence of a particular option may be denoted 1210with a 1211.Ql \&! . 1212.It Cm uid Ar user 1213Match all TCP or UDP packets sent by or received for a 1214.Ar user . 1215A 1216.Ar user 1217may be matched by name or identification number. 1218.It Cm verrevpath 1219For incoming packets, 1220a routing table lookup is done on the packet's source address. 1221If the interface on which the packet entered the system matches the 1222outgoing interface for the route, 1223the packet matches. 1224If the interfaces do not match up, 1225the packet does not match. 1226All outgoing packets or packets with no incoming interface match. 1227.Pp 1228The name and functionality of the option is intentionally similar to 1229the Cisco IOS command: 1230.Pp 1231.Dl ip verify unicast reverse-path 1232.Pp 1233This option can be used to make anti-spoofing rules. 1234.El 1235.Sh SETS OF RULES 1236Each rule belongs to one of 32 different 1237.Em sets 1238, numbered 0 to 31. 1239Set 31 is reserved for the default rule. 1240.Pp 1241By default, rules are put in set 0, unless you use the 1242.Cm set N 1243attribute when entering a new rule. 1244Sets can be individually and atomically enabled or disabled, 1245so this mechanism permits an easy way to store multiple configurations 1246of the firewall and quickly (and atomically) switch between them. 1247The command to enable/disable sets is 1248.Bd -ragged -offset indent 1249.Nm 1250.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ... 1251.Ed 1252.Pp 1253where multiple 1254.Cm enable 1255or 1256.Cm disable 1257sections can be specified. 1258Command execution is atomic on all the sets specified in the command. 1259By default, all sets are enabled. 1260.Pp 1261When you disable a set, its rules behave as if they do not exist 1262in the firewall configuration, with only one exception: 1263.Bd -ragged -offset indent 1264dynamic rules created from a rule before it had been disabled 1265will still be active until they expire. In order to delete 1266dynamic rules you have to explicitly delete the parent rule 1267which generated them. 1268.Ed 1269.Pp 1270The set number of rules can be changed with the command 1271.Bd -ragged -offset indent 1272.Nm 1273.Cm set move 1274.Brq Cm rule Ar rule-number | old-set 1275.Cm to Ar new-set 1276.Ed 1277.Pp 1278Also, you can atomically swap two rulesets with the command 1279.Bd -ragged -offset indent 1280.Nm 1281.Cm set swap Ar first-set second-set 1282.Ed 1283.Pp 1284See the 1285.Sx EXAMPLES 1286Section on some possible uses of sets of rules. 1287.Sh STATEFUL FIREWALL 1288Stateful operation is a way for the firewall to dynamically 1289create rules for specific flows when packets that 1290match a given pattern are detected. Support for stateful 1291operation comes through the 1292.Cm check-state , keep-state 1293and 1294.Cm limit 1295options of 1296.Nm rules. 1297.Pp 1298Dynamic rules are created when a packet matches a 1299.Cm keep-state 1300or 1301.Cm limit 1302rule, causing the creation of a 1303.Em dynamic 1304rule which will match all and only packets with 1305a given 1306.Em protocol 1307between a 1308.Em src-ip/src-port dst-ip/dst-port 1309pair of addresses ( 1310.Em src 1311and 1312.Em dst 1313are used here only to denote the initial match addresses, but they 1314are completely equivalent afterwards). 1315Dynamic rules will be checked at the first 1316.Cm check-state, keep-state 1317or 1318.Cm limit 1319occurrence, and the action performed upon a match will be the same 1320as in the parent rule. 1321.Pp 1322Note that no additional attributes other than protocol and IP addresses 1323and ports are checked on dynamic rules. 1324.Pp 1325The typical use of dynamic rules is to keep a closed firewall configuration, 1326but let the first TCP SYN packet from the inside network install a 1327dynamic rule for the flow so that packets belonging to that session 1328will be allowed through the firewall: 1329.Pp 1330.Dl "ipfw add check-state" 1331.Dl "ipfw add allow tcp from my-subnet to any setup keep-state" 1332.Dl "ipfw add deny tcp from any to any" 1333.Pp 1334A similar approach can be used for UDP, where an UDP packet coming 1335from the inside will install a dynamic rule to let the response through 1336the firewall: 1337.Pp 1338.Dl "ipfw add check-state" 1339.Dl "ipfw add allow udp from my-subnet to any keep-state" 1340.Dl "ipfw add deny udp from any to any" 1341.Pp 1342Dynamic rules expire after some time, which depends on the status 1343of the flow and the setting of some 1344.Cm sysctl 1345variables. 1346See Section 1347.Sx SYSCTL VARIABLES 1348for more details. 1349For TCP sessions, dynamic rules can be instructed to periodically 1350send keepalive packets to refresh the state of the rule when it is 1351about to expire. 1352.Pp 1353See Section 1354.Sx EXAMPLES 1355for more examples on how to use dynamic rules. 1356.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 1357.Nm 1358is also the user interface for the 1359.Xr dummynet 4 1360traffic shaper. 1361.Pp 1362.Nm dummynet 1363operates by first using the firewall to classify packets and divide them into 1364.Em flows , 1365using any match pattern that can be used in 1366.Nm 1367rules. 1368Depending on local policies, a flow can contain packets for a single 1369TCP connection, or from/to a given host, or entire subnet, or a 1370protocol type, etc. 1371.Pp 1372Packets belonging to the same flow are then passed to either of two 1373different objects, which implement the traffic regulation: 1374.Bl -hang -offset XXXX 1375.It Em pipe 1376A pipe emulates a link with given bandwidth, propagation delay, 1377queue size and packet loss rate. 1378Packets are queued in front of the pipe as they come out from the classifier, 1379and then transferred to the pipe according to the pipe's parameters. 1380.Pp 1381.It Em queue 1382A queue 1383is an abstraction used to implement the WF2Q+ 1384(Worst-case Fair Weighted Fair Queueing) policy, which is 1385an efficient variant of the WFQ policy. 1386.br 1387The queue associates a 1388.Em weight 1389and a reference pipe to each flow, and then all backlogged (i.e., 1390with packets queued) flows linked to the same pipe share the pipe's 1391bandwidth proportionally to their weights. 1392Note that weights are not priorities; a flow with a lower weight 1393is still guaranteed to get its fraction of the bandwidth even if a 1394flow with a higher weight is permanently backlogged. 1395.Pp 1396.El 1397In practice, 1398.Em pipes 1399can be used to set hard limits to the bandwidth that a flow can use, whereas 1400.Em queues 1401can be used to determine how different flow share the available bandwidth. 1402.Pp 1403The 1404.Em pipe 1405and 1406.Em queue 1407configuration commands are the following: 1408.Bd -ragged -offset indent 1409.Cm pipe Ar number Cm config Ar pipe-configuration 1410.Pp 1411.Cm queue Ar number Cm config Ar queue-configuration 1412.Ed 1413.Pp 1414The following parameters can be configured for a pipe: 1415.Pp 1416.Bl -tag -width indent -compact 1417.It Cm bw Ar bandwidth | device 1418Bandwidth, measured in 1419.Sm off 1420.Op Cm K | M 1421.Brq Cm bit/s | Byte/s . 1422.Sm on 1423.Pp 1424A value of 0 (default) means unlimited bandwidth. 1425The unit must immediately follow the number, as in 1426.Pp 1427.Dl "ipfw pipe 1 config bw 300Kbit/s" 1428.Pp 1429If a device name is specified instead of a numeric value, as in 1430.Pp 1431.Dl "ipfw pipe 1 config bw tun0" 1432.Pp 1433then the transmit clock is supplied by the specified device. 1434At the moment only the 1435.Xr tun 4 1436device supports this 1437functionality, for use in conjunction with 1438.Xr ppp 8 . 1439.Pp 1440.It Cm delay Ar ms-delay 1441Propagation delay, measured in milliseconds. 1442The value is rounded to the next multiple of the clock tick 1443(typically 10ms, but it is a good practice to run kernels 1444with 1445.Dq "options HZ=1000" 1446to reduce 1447the granularity to 1ms or less). 1448Default value is 0, meaning no delay. 1449.El 1450.Pp 1451The following parameters can be configured for a queue: 1452.Pp 1453.Bl -tag -width indent -compact 1454.It Cm pipe Ar pipe_nr 1455Connects a queue to the specified pipe. 1456Multiple queues (with the same or different weights) can be connected to 1457the same pipe, which specifies the aggregate rate for the set of queues. 1458.Pp 1459.It Cm weight Ar weight 1460Specifies the weight to be used for flows matching this queue. 1461The weight must be in the range 1..100, and defaults to 1. 1462.El 1463.Pp 1464Finally, the following parameters can be configured for both 1465pipes and queues: 1466.Pp 1467.Bl -tag -width XXXX -compact 1468.Pp 1469.It Cm buckets Ar hash-table-size 1470Specifies the size of the hash table used for storing the 1471various queues. 1472Default value is 64 controlled by the 1473.Xr sysctl 8 1474variable 1475.Em net.inet.ip.dummynet.hash_size , 1476allowed range is 16 to 65536. 1477.Pp 1478.It Cm mask Ar mask-specifier 1479Packets sent to a given pipe or queue by an 1480.Nm 1481rule can be further classified into multiple flows, each of which is then 1482sent to a different 1483.Em dynamic 1484pipe or queue. 1485A flow identifier is constructed by masking the IP addresses, 1486ports and protocol types as specified with the 1487.Cm mask 1488options in the configuration of the pipe or queue. 1489For each different flow identifier, a new pipe or queue is created 1490with the same parameters as the original object, and matching packets 1491are sent to it. 1492.Pp 1493Thus, when 1494.Em dynamic pipes 1495are used, each flow will get the same bandwidth as defined by the pipe, 1496whereas when 1497.Em dynamic queues 1498are used, each flow will share the parent's pipe bandwidth evenly 1499with other flows generated by the same queue (note that other queues 1500with different weights might be connected to the same pipe). 1501.br 1502Available mask specifiers are a combination of one or more of the following: 1503.Pp 1504.Cm dst-ip Ar mask , 1505.Cm src-ip Ar mask , 1506.Cm dst-port Ar mask , 1507.Cm src-port Ar mask , 1508.Cm proto Ar mask 1509or 1510.Cm all , 1511.Pp 1512where the latter means all bits in all fields are significant. 1513.Pp 1514.It Cm noerror 1515When a packet is dropped by a dummynet queue or pipe, the error 1516is normally reported to the caller routine in the kernel, in the 1517same way as it happens when a device queue fills up. Setting this 1518option reports the packet as successfully delivered, which can be 1519needed for some experimental setups where you want to simulate 1520loss or congestion at a remote router. 1521.Pp 1522.It Cm plr Ar packet-loss-rate 1523Packet loss rate. 1524Argument 1525.Ar packet-loss-rate 1526is a floating-point number between 0 and 1, with 0 meaning no 1527loss, 1 meaning 100% loss. 1528The loss rate is internally represented on 31 bits. 1529.Pp 1530.It Cm queue Brq Ar slots | size Ns Cm Kbytes 1531Queue size, in 1532.Ar slots 1533or 1534.Cm KBytes . 1535Default value is 50 slots, which 1536is the typical queue size for Ethernet devices. 1537Note that for slow speed links you should keep the queue 1538size short or your traffic might be affected by a significant 1539queueing delay. 1540E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit 1541or 20s of queue on a 30Kbit/s pipe. 1542Even worse effect can result if you get packets from an 1543interface with a much larger MTU, e.g. the loopback interface 1544with its 16KB packets. 1545.Pp 1546.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p 1547Make use of the RED (Random Early Detection) queue management algorithm. 1548.Ar w_q 1549and 1550.Ar max_p 1551are floating 1552point numbers between 0 and 1 (0 not included), while 1553.Ar min_th 1554and 1555.Ar max_th 1556are integer numbers specifying thresholds for queue management 1557(thresholds are computed in bytes if the queue has been defined 1558in bytes, in slots otherwise). 1559The 1560.Xr dummynet 4 1561also supports the gentle RED variant (gred). 1562Three 1563.Xr sysctl 8 1564variables can be used to control the RED behaviour: 1565.Bl -tag -width indent 1566.It Em net.inet.ip.dummynet.red_lookup_depth 1567specifies the accuracy in computing the average queue 1568when the link is idle (defaults to 256, must be greater than zero) 1569.It Em net.inet.ip.dummynet.red_avg_pkt_size 1570specifies the expected average packet size (defaults to 512, must be 1571greater than zero) 1572.It Em net.inet.ip.dummynet.red_max_pkt_size 1573specifies the expected maximum packet size, only used when queue 1574thresholds are in bytes (defaults to 1500, must be greater than zero). 1575.El 1576.El 1577.Sh CHECKLIST 1578Here are some important points to consider when designing your 1579rules: 1580.Bl -bullet 1581.It 1582Remember that you filter both packets going 1583.Cm in 1584and 1585.Cm out . 1586Most connections need packets going in both directions. 1587.It 1588Remember to test very carefully. 1589It is a good idea to be near the console when doing this. 1590If you cannot be near the console, 1591use an auto-recovery script such as the one in 1592.Pa /usr/share/examples/ipfw/change_rules.sh . 1593.It 1594Don't forget the loopback interface. 1595.El 1596.Sh FINE POINTS 1597.Bl -bullet 1598.It 1599There are circumstances where fragmented datagrams are unconditionally 1600dropped. 1601TCP packets are dropped if they do not contain at least 20 bytes of 1602TCP header, UDP packets are dropped if they do not contain a full 8 1603byte UDP header, and ICMP packets are dropped if they do not contain 16044 bytes of ICMP header, enough to specify the ICMP type, code, and 1605checksum. 1606These packets are simply logged as 1607.Dq pullup failed 1608since there may not be enough good data in the packet to produce a 1609meaningful log entry. 1610.It 1611Another type of packet is unconditionally dropped, a TCP packet with a 1612fragment offset of one. 1613This is a valid packet, but it only has one use, to try 1614to circumvent firewalls. 1615When logging is enabled, these packets are 1616reported as being dropped by rule -1. 1617.It 1618If you are logged in over a network, loading the 1619.Xr kld 4 1620version of 1621.Nm 1622is probably not as straightforward as you would think. 1623I recommend the following command line: 1624.Bd -literal -offset indent 1625kldload ipfw && \e 1626ipfw add 32000 allow ip from any to any 1627.Ed 1628.Pp 1629Along the same lines, doing an 1630.Bd -literal -offset indent 1631ipfw flush 1632.Ed 1633.Pp 1634in similar surroundings is also a bad idea. 1635.It 1636The 1637.Nm 1638filter list may not be modified if the system security level 1639is set to 3 or higher 1640(see 1641.Xr init 8 1642for information on system security levels). 1643.El 1644.Sh PACKET DIVERSION 1645A 1646.Xr divert 4 1647socket bound to the specified port will receive all packets 1648diverted to that port. 1649If no socket is bound to the destination port, or if the kernel 1650wasn't compiled with divert socket support, the packets are 1651dropped. 1652.Sh SYSCTL VARIABLES 1653A set of 1654.Xr sysctl 8 1655variables controls the behaviour of the firewall and 1656associated modules ( 1657.Nm dummynet, bridge 1658). 1659These are shown below together with their default value 1660(but always check with the 1661.Xr sysctl 8 1662command what value is actually in use) and meaning: 1663.Bl -tag -width indent 1664.It Em net.inet.ip.dummynet.expire : No 1 1665Lazily delete dynamic pipes/queue once they have no pending traffic. 1666You can disable this by setting the variable to 0, in which case 1667the pipes/queues will only be deleted when the threshold is reached. 1668.It Em net.inet.ip.dummynet.hash_size : No 64 1669Default size of the hash table used for dynamic pipes/queues. 1670This value is used when no 1671.Cm buckets 1672option is specified when configuring a pipe/queue. 1673.It Em net.inet.ip.dummynet.max_chain_len : No 16 1674Target value for the maximum number of pipes/queues in a hash bucket. 1675The product 1676.Cm max_chain_len*hash_size 1677is used to determine the threshold over which empty pipes/queues 1678will be expired even when 1679.Cm net.inet.ip.dummynet.expire=0 . 1680.It Em net.inet.ip.dummynet.red_lookup_depth : No 256 1681.It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512 1682.It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500 1683Parameters used in the computations of the drop probability 1684for the RED algorithm. 1685.It Em net.inet.ip.fw.autoinc_step : No 100 1686Delta between rule numbers when auto-generating them. 1687The value must be in the range 1..1000. 1688This variable is only present in 1689.Nm ipfw2 , 1690the delta is hardwired to 100 in 1691.Nm ipfw1 . 1692.It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets 1693The current number of buckets in the hash table for dynamic rules 1694(readonly). 1695.It Em net.inet.ip.fw.debug : No 1 1696Controls debugging messages produced by 1697.Nm . 1698.It Em net.inet.ip.fw.dyn_buckets : No 256 1699The number of buckets in the hash table for dynamic rules. 1700Must be a power of 2, up to 65536. 1701It only takes effect when all dynamic rules have expired, so you 1702are advised to use a 1703.Cm flush 1704command to make sure that the hash table is resized. 1705.It Em net.inet.ip.fw.dyn_count : No 3 1706Current number of dynamic rules 1707(read-only). 1708.It Em net.inet.ip.fw.dyn_keepalive : No 1 1709Enables generation of keepalive packets for 1710.Cm keep-state 1711rules on TCP sessions. A keepalive is generated to both 1712sides of the connection every 5 seconds for the last 20 1713seconds of the lifetime of the rule. 1714.It Em net.inet.ip.fw.dyn_max : No 8192 1715Maximum number of dynamic rules. 1716When you hit this limit, no more dynamic rules can be 1717installed until old ones expire. 1718.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300 1719.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20 1720.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1 1721.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1 1722.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5 1723.It Em net.inet.ip.fw.dyn_short_lifetime : No 30 1724These variables control the lifetime, in seconds, of dynamic 1725rules. 1726Upon the initial SYN exchange the lifetime is kept short, 1727then increased after both SYN have been seen, then decreased 1728again during the final FIN exchange or when a RST is received. 1729Both 1730.Em dyn_fin_lifetime 1731and 1732.Em dyn_rst_lifetime 1733must be strictly lower than 5 seconds, the period of 1734repetition of keepalives. The firewall enforces that. 1735.It Em net.inet.ip.fw.enable : No 1 1736Enables the firewall. 1737Setting this variable to 0 lets you run your machine without 1738firewall even if compiled in. 1739.It Em net.inet.ip.fw.one_pass : No 1 1740When set, the packet exiting from the 1741.Xr dummynet 4 1742pipe is not passed though the firewall again. 1743Otherwise, after a pipe action, the packet is 1744reinjected into the firewall at the next rule. 1745.It Em net.inet.ip.fw.verbose : No 1 1746Enables verbose messages. 1747.It Em net.inet.ip.fw.verbose_limit : No 0 1748Limits the number of messages produced by a verbose firewall. 1749.It Em net.link.ether.ipfw : No 0 1750Controls whether layer-2 packets are passed to 1751.Nm . 1752Default is no. 1753.It Em net.link.ether.bridge_ipfw : No 0 1754Controls whether bridged packets are passed to 1755.Nm . 1756Default is no. 1757.El 1758.Sh USING IPFW2 IN FreeBSD-STABLE 1759.Nm ipfw2 1760is standard in 1761.Fx 1762CURRENT, whereas 1763.Fx 1764STABLE still uses 1765.Nm ipfw1 1766unless the kernel is compiled with 1767.Cm options IPFW2 , 1768and 1769.Nm /sbin/ipfw 1770and 1771.Nm /usr/lib/libalias 1772are recompiled with 1773.Cm -DIPFW2 1774and reinstalled (the same effect can be achieved by adding 1775.Cm IPFW2=TRUE 1776to 1777.Nm /etc/make.conf 1778before a buildworld). 1779.Pp 1780.Sh IPFW2 ENHANCEMENTS 1781This Section lists the features that have been introduced in 1782.Nm ipfw2 1783which were not present in 1784.Nm ipfw1 . 1785We list them in order of the potential impact that they can 1786have in writing your rulesets. 1787You might want to consider using these features in order to 1788write your rulesets in a more efficient way. 1789.Bl -tag -width indent 1790.It Syntax and flags 1791.Nm ipfw1 1792does not support the -n flag (only test syntax), 1793nor it allows spaces after commas or supports all 1794rule fields in a single argument. 1795.It Handling of non-IPv4 packets 1796.Nm ipfw1 1797will silently accept all non-IPv4 packets (which 1798.Nm ipfw1 1799will only see when 1800.Em net.link.ether.bridge_ipfw=1 Ns 1801). 1802.Nm ipfw2 1803will filter all packets (including non-IPv4 ones) according to the ruleset. 1804To achieve the same behaviour as 1805.Nm ipfw1 1806you can use the following as the very first rule in your ruleset: 1807.Pp 1808.Dl "ipfw add 1 allow layer2 not mac-type ip" 1809.Pp 1810The 1811.Cm layer2 1812option might seem redundant, but it is necessary -- packets 1813passed to the firewall from layer3 will not have a MAC header, 1814so the 1815.Cm mac-type ip 1816pattern will always fail on them, and the 1817.Cm not 1818operator will make this rule into a pass-all. 1819.It Addresses 1820.Nm ipfw1 1821does not supports address sets or lists of addresses. 1822.Pp 1823.It Port specifications 1824.Nm ipfw1 1825only allows one port range when specifying TCP and UDP ports, and 1826is limited to 10 entries instead of the 15 allowed by 1827.Nm ipfw2 . 1828Also, in 1829.Nm ipfw1 1830you can only specify ports when the rule is requesting 1831.Cm tcp 1832or 1833.Cm udp 1834packets. With 1835.Nm ipfw2 1836you can put port specifications in rules matching all packets, 1837and the match will be attempted only on those packets carrying 1838protocols which include port identifiers. 1839.Pp 1840Finally, 1841.Nm ipfw1 1842allowed the first port entry to be specified as 1843.Ar port:mask 1844where 1845.Ar mask 1846can be an arbitrary 16-bit mask. 1847This syntax is of questionable usefulness and it is not 1848supported anymore in 1849.Nm ipfw2 . 1850.It Or-blocks 1851.Nm ipfw1 1852does not support Or-blocks. 1853.It keepalives 1854.Nm ipfw1 1855does not generate keepalives for stateful sessions. 1856As a consequence, it might cause idle sessions to drop because 1857the lifetime of the dynamic rules expires. 1858.It Sets of rules 1859.Nm ipfw1 1860does not implement sets of rules. 1861.It MAC header filtering and Layer-2 firewalling. 1862.Nm ipfw1 1863does not implement filtering on MAC header fields, nor is it 1864invoked on packets from 1865.Cm ether_demux() 1866and 1867.Cm ether_output_frame(). 1868The sysctl variable 1869.Em net.link.ether.ipfw 1870has no effect there. 1871.It Options 1872In 1873.Nm ipfw1 , 1874the following options only accept a single value as an argument: 1875.Pp 1876.Cm ipid, iplen, ipttl 1877.Pp 1878The following options are not implemented by 1879.Nm ipfw1 : 1880.Pp 1881.Cm dst-ip, dst-port, layer2, mac, mac-type, src-ip, src-port. 1882.Pp 1883Additionally, the RELENG_4 version of 1884.Nm ipfw1 1885does not implement the following options: 1886.Pp 1887.Cm ipid, iplen, ipprecedence, iptos, ipttl, 1888.Cm ipversion, tcpack, tcpseq, tcpwin . 1889.It Dummynet options 1890The following option for 1891.Nm dummynet 1892pipes/queues is not supported: 1893.Cm noerror . 1894.El 1895.Sh EXAMPLES 1896There are far too many possible uses of 1897.Nm 1898so this Section will only give a small set of examples. 1899.Pp 1900.Ss BASIC PACKET FILTERING 1901This command adds an entry which denies all tcp packets from 1902.Em cracker.evil.org 1903to the telnet port of 1904.Em wolf.tambov.su 1905from being forwarded by the host: 1906.Pp 1907.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet" 1908.Pp 1909This one disallows any connection from the entire cracker's 1910network to my host: 1911.Pp 1912.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org" 1913.Pp 1914A first and efficient way to limit access (not using dynamic rules) 1915is the use of the following rules: 1916.Pp 1917.Dl "ipfw add allow tcp from any to any established" 1918.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup" 1919.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup" 1920.Dl "..." 1921.Dl "ipfw add deny tcp from any to any" 1922.Pp 1923The first rule will be a quick match for normal TCP packets, 1924but it will not match the initial SYN packet, which will be 1925matched by the 1926.Cm setup 1927rules only for selected source/destination pairs. 1928All other SYN packets will be rejected by the final 1929.Cm deny 1930rule. 1931.Pp 1932If you administer one or more subnets, you can take advantage of the 1933.Nm ipfw2 1934syntax to specify address sets and or-blocks and write extremely 1935compact rulesets which selectively enable services to blocks 1936of clients, as below: 1937.Pp 1938.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q" 1939.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q" 1940.Dl "" 1941.Dl "ipfw add allow ip from ${goodguys} to any" 1942.Dl "ipfw add deny ip from ${badguys} to any" 1943.Dl "... normal policies ..." 1944.Pp 1945The 1946.Nm ipfw1 1947syntax would require a separate rule for each IP in the above 1948example. 1949.Pp 1950The 1951.Cm verrevpath 1952option could be used to do automated anti-spoofing by adding the 1953following to the top of a ruleset: 1954.Pp 1955.Dl "ipfw add deny ip from any to any not verrevpath in" 1956.Pp 1957This rule drops all incoming packets that appear to be coming to the 1958system on the wrong interface. For example, a packet with a source 1959address belonging to a host on a protected internal network would be 1960dropped if it tried to enter the system from an external interface. 1961.Ss DYNAMIC RULES 1962In order to protect a site from flood attacks involving fake 1963TCP packets, it is safer to use dynamic rules: 1964.Pp 1965.Dl "ipfw add check-state" 1966.Dl "ipfw add deny tcp from any to any established" 1967.Dl "ipfw add allow tcp from my-net to any setup keep-state" 1968.Pp 1969This will let the firewall install dynamic rules only for 1970those connection which start with a regular SYN packet coming 1971from the inside of our network. 1972Dynamic rules are checked when encountering the first 1973.Cm check-state 1974or 1975.Cm keep-state 1976rule. 1977A 1978.Cm check-state 1979rule should usually be placed near the beginning of the 1980ruleset to minimize the amount of work scanning the ruleset. 1981Your mileage may vary. 1982.Pp 1983To limit the number of connections a user can open 1984you can use the following type of rules: 1985.Pp 1986.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10" 1987.Dl "ipfw add allow tcp from any to me setup limit src-addr 4" 1988.Pp 1989The former (assuming it runs on a gateway) will allow each host 1990on a /24 network to open at most 10 TCP connections. 1991The latter can be placed on a server to make sure that a single 1992client does not use more than 4 simultaneous connections. 1993.Pp 1994.Em BEWARE : 1995stateful rules can be subject to denial-of-service attacks 1996by a SYN-flood which opens a huge number of dynamic rules. 1997The effects of such attacks can be partially limited by 1998acting on a set of 1999.Xr sysctl 8 2000variables which control the operation of the firewall. 2001.Pp 2002Here is a good usage of the 2003.Cm list 2004command to see accounting records and timestamp information: 2005.Pp 2006.Dl ipfw -at list 2007.Pp 2008or in short form without timestamps: 2009.Pp 2010.Dl ipfw -a list 2011.Pp 2012which is equivalent to: 2013.Pp 2014.Dl ipfw show 2015.Pp 2016Next rule diverts all incoming packets from 192.168.2.0/24 2017to divert port 5000: 2018.Pp 2019.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in 2020.Pp 2021.Ss TRAFFIC SHAPING 2022The following rules show some of the applications of 2023.Nm 2024and 2025.Xr dummynet 4 2026for simulations and the like. 2027.Pp 2028This rule drops random incoming packets with a probability 2029of 5%: 2030.Pp 2031.Dl "ipfw add prob 0.05 deny ip from any to any in" 2032.Pp 2033A similar effect can be achieved making use of dummynet pipes: 2034.Pp 2035.Dl "ipfw add pipe 10 ip from any to any" 2036.Dl "ipfw pipe 10 config plr 0.05" 2037.Pp 2038We can use pipes to artificially limit bandwidth, e.g. on a 2039machine acting as a router, if we want to limit traffic from 2040local clients on 192.168.2.0/24 we do: 2041.Pp 2042.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 2043.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 2044.Pp 2045note that we use the 2046.Cm out 2047modifier so that the rule is not used twice. 2048Remember in fact that 2049.Nm 2050rules are checked both on incoming and outgoing packets. 2051.Pp 2052Should we want to simulate a bidirectional link with bandwidth 2053limitations, the correct way is the following: 2054.Pp 2055.Dl "ipfw add pipe 1 ip from any to any out" 2056.Dl "ipfw add pipe 2 ip from any to any in" 2057.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes" 2058.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes" 2059.Pp 2060The above can be very useful, e.g. if you want to see how 2061your fancy Web page will look for a residential user who 2062is connected only through a slow link. 2063You should not use only one pipe for both directions, unless 2064you want to simulate a half-duplex medium (e.g. AppleTalk, 2065Ethernet, IRDA). 2066It is not necessary that both pipes have the same configuration, 2067so we can also simulate asymmetric links. 2068.Pp 2069Should we want to verify network performance with the RED queue 2070management algorithm: 2071.Pp 2072.Dl "ipfw add pipe 1 ip from any to any" 2073.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1" 2074.Pp 2075Another typical application of the traffic shaper is to 2076introduce some delay in the communication. 2077This can significantly affect applications which do a lot of Remote 2078Procedure Calls, and where the round-trip-time of the 2079connection often becomes a limiting factor much more than 2080bandwidth: 2081.Pp 2082.Dl "ipfw add pipe 1 ip from any to any out" 2083.Dl "ipfw add pipe 2 ip from any to any in" 2084.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s" 2085.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s" 2086.Pp 2087Per-flow queueing can be useful for a variety of purposes. 2088A very simple one is counting traffic: 2089.Pp 2090.Dl "ipfw add pipe 1 tcp from any to any" 2091.Dl "ipfw add pipe 1 udp from any to any" 2092.Dl "ipfw add pipe 1 ip from any to any" 2093.Dl "ipfw pipe 1 config mask all" 2094.Pp 2095The above set of rules will create queues (and collect 2096statistics) for all traffic. 2097Because the pipes have no limitations, the only effect is 2098collecting statistics. 2099Note that we need 3 rules, not just the last one, because 2100when 2101.Nm 2102tries to match IP packets it will not consider ports, so we 2103would not see connections on separate ports as different 2104ones. 2105.Pp 2106A more sophisticated example is limiting the outbound traffic 2107on a net with per-host limits, rather than per-network limits: 2108.Pp 2109.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 2110.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in" 2111.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 2112.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 2113.Ss SETS OF RULES 2114To add a set of rules atomically, e.g. set 18: 2115.Pp 2116.Dl "ipfw set disable 18" 2117.Dl "ipfw add NN set 18 ... # repeat as needed" 2118.Dl "ipfw set enable 18" 2119.Pp 2120To delete a set of rules atomically the command is simply: 2121.Pp 2122.Dl "ipfw delete set 18" 2123.Pp 2124To test a ruleset and disable it and regain control if something goes wrong: 2125.Pp 2126.Dl "ipfw set disable 18" 2127.Dl "ipfw add NN set 18 ... # repeat as needed" 2128.Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18" 2129.Pp 2130Here if everything goes well, you press control-C before the "sleep" 2131terminates, and your ruleset will be left active. Otherwise, e.g. if 2132you cannot access your box, the ruleset will be disabled after 2133the sleep terminates thus restoring the previous situation. 2134.Sh SEE ALSO 2135.Xr cpp 1 , 2136.Xr m4 1 , 2137.Xr bridge 4 , 2138.Xr divert 4 , 2139.Xr dummynet 4 , 2140.Xr ip 4 , 2141.Xr ipfirewall 4 , 2142.Xr protocols 5 , 2143.Xr services 5 , 2144.Xr init 8 , 2145.Xr kldload 8 , 2146.Xr reboot 8 , 2147.Xr sysctl 8 , 2148.Xr syslogd 8 2149.Sh BUGS 2150The syntax has grown over the years and sometimes it might be confusing. 2151Unfortunately, backward compatibility prevents cleaning up mistakes 2152made in the definition of the syntax. 2153.Pp 2154.Em !!! WARNING !!! 2155.Pp 2156Misconfiguring the firewall can put your computer in an unusable state, 2157possibly shutting down network services and requiring console access to 2158regain control of it. 2159.Pp 2160Incoming packet fragments diverted by 2161.Cm divert 2162or 2163.Cm tee 2164are reassembled before delivery to the socket. 2165The action used on those packet is the one from the 2166rule which matches the first fragment of the packet. 2167.Pp 2168Packets that match a 2169.Cm tee 2170rule should not be immediately accepted, but should continue 2171going through the rule list. 2172This may be fixed in a later version. 2173.Pp 2174Packets diverted to userland, and then reinserted by a userland process 2175may lose various packet attributes. 2176The packet source interface name 2177will be preserved if it is shorter than 8 bytes and the userland process 2178saves and reuses the sockaddr_in 2179(as does 2180.Xr natd 8 ) ; 2181otherwise, it may be lost. 2182If a packet is reinserted in this manner, later rules may be incorrectly 2183applied, making the order of 2184.Cm divert 2185rules in the rule sequence very important. 2186.Sh AUTHORS 2187.An Ugen J. S. Antsilevich , 2188.An Poul-Henning Kamp , 2189.An Alex Nash , 2190.An Archie Cobbs , 2191.An Luigi Rizzo . 2192.Pp 2193.An -nosplit 2194API based upon code written by 2195.An Daniel Boulet 2196for BSDI. 2197.Pp 2198Work on 2199.Xr dummynet 4 2200traffic shaper supported by Akamba Corp. 2201.Sh HISTORY 2202The 2203.Nm 2204utility first appeared in 2205.Fx 2.0 . 2206.Xr dummynet 4 2207was introduced in 2208.Fx 2.2.8 . 2209Stateful extensions were introduced in 2210.Fx 4.0 . 2211.Nm ipfw2 2212was introduced in Summer 2002. 2213