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