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 iptos Ar spec 931Matches IP packets whose 932.Cm tos 933field contains the comma separated list of 934service types specified in 935.Ar spec . 936The supported IP types of service are: 937.Pp 938.Cm lowdelay 939.Pq Dv IPTOS_LOWDELAY , 940.Cm throughput 941.Pq Dv IPTOS_THROUGHPUT , 942.Cm reliability 943.Pq Dv IPTOS_RELIABILITY , 944.Cm mincost 945.Pq Dv IPTOS_MINCOST , 946.Cm congestion 947.Pq Dv IPTOS_CE . 948The absence of a particular type may be denoted 949with a 950.Ql \&! . 951.It Cm ipttl Ar ttl-list 952Matches IP packets whose time to live is included in 953.Ar ttl-list , 954which is either a single value or a list of values or ranges 955specified in the same way as 956.Ar ports . 957.It Cm ipversion Ar ver 958Matches IP packets whose IP version field is 959.Ar ver . 960.It Cm keep-state 961Upon a match, the firewall will create a dynamic rule, whose 962default behaviour is to match bidirectional traffic between 963source and destination IP/port using the same protocol. 964The rule has a limited lifetime (controlled by a set of 965.Xr sysctl 8 966variables), and the lifetime is refreshed every time a matching 967packet is found. 968.It Cm layer2 969Matches only layer2 packets, i.e. those passed to 970.Nm 971from ether_demux() and ether_output_frame(). 972.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N 973The firewall will only allow 974.Ar N 975connections with the same 976set of parameters as specified in the rule. 977One or more 978of source and destination addresses and ports can be 979specified. 980.It Cm { MAC | mac } Ar dst-mac src-mac 981Match packets with a given 982.Ar dst-mac 983and 984.Ar src-mac 985addresses, specified as the 986.Cm any 987keyword (matching any MAC address), or six groups of hex digits 988separated by colons, 989and optionally followed by a mask indicating how many bits are 990significant, as in 991.Pp 992.Dl "MAC 10:20:30:40:50:60/33 any" 993.Pp 994Note that the order of MAC addresses (destination first, 995source second) is 996the same as on the wire, but the opposite of the one used for 997IP addresses. 998.It Cm mac-type Ar mac-type 999Matches packets whose Ethernet Type field 1000corresponds to one of those specified as argument. 1001.Ar mac-type 1002is specified in the same way as 1003.Cm port numbers 1004(i.e. one or more comma-separated single values or ranges). 1005You can use symbolic names for known values such as 1006.Em vlan , ipv4, ipv6 . 1007Values can be entered as decimal or hexadecimal (if prefixed by 0x), 1008and they are always printed as hexadecimal (unless the 1009.Cm -N 1010option is used, in which case symbolic resolution will be attempted). 1011.It Cm proto Ar protocol 1012Matches packets with the corresponding IPv4 protocol. 1013.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any 1014Matches packets received, transmitted or going through, 1015respectively, the interface specified by exact name 1016.Ns No ( Ar ifX Ns No ), 1017by device name 1018.Ns No ( Ar if Ns Ar * Ns No ), 1019by IP address, or through some interface. 1020.Pp 1021The 1022.Cm via 1023keyword causes the interface to always be checked. 1024If 1025.Cm recv 1026or 1027.Cm xmit 1028is used instead of 1029.Cm via , 1030then only the receive or transmit interface (respectively) 1031is checked. 1032By specifying both, it is possible to match packets based on 1033both receive and transmit interface, e.g.: 1034.Pp 1035.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1" 1036.Pp 1037The 1038.Cm recv 1039interface can be tested on either incoming or outgoing packets, 1040while the 1041.Cm xmit 1042interface can only be tested on outgoing packets. 1043So 1044.Cm out 1045is required (and 1046.Cm in 1047is invalid) whenever 1048.Cm xmit 1049is used. 1050.Pp 1051A packet may not have a receive or transmit interface: packets 1052originating from the local host have no receive interface, 1053while packets destined for the local host have no transmit 1054interface. 1055.It Cm setup 1056Matches TCP packets that have the SYN bit set but no ACK bit. 1057This is the short form of 1058.Dq Li tcpflags\ syn,!ack . 1059.It Cm src-ip Ar ip-address 1060Matches IP packets whose source IP is one of the address(es) 1061specified as argument. 1062.It Cm src-port Ar ports 1063Matches IP packets whose source port is one of the port(s) 1064specified as argument. 1065.It Cm tcpack Ar ack 1066TCP packets only. 1067Match if the TCP header acknowledgment number field is set to 1068.Ar ack . 1069.It Cm tcpflags Ar spec 1070TCP packets only. 1071Match if the TCP header contains the comma separated list of 1072flags specified in 1073.Ar spec . 1074The supported TCP flags are: 1075.Pp 1076.Cm fin , 1077.Cm syn , 1078.Cm rst , 1079.Cm psh , 1080.Cm ack 1081and 1082.Cm urg . 1083The absence of a particular flag may be denoted 1084with a 1085.Ql \&! . 1086A rule which contains a 1087.Cm tcpflags 1088specification can never match a fragmented packet which has 1089a non-zero offset. 1090See the 1091.Cm frag 1092option for details on matching fragmented packets. 1093.It Cm tcpseq Ar seq 1094TCP packets only. 1095Match if the TCP header sequence number field is set to 1096.Ar seq . 1097.It Cm tcpwin Ar win 1098TCP packets only. 1099Match if the TCP header window field is set to 1100.Ar win . 1101.It Cm tcpoptions Ar spec 1102TCP packets only. 1103Match if the TCP header contains the comma separated list of 1104options specified in 1105.Ar spec . 1106The supported TCP options are: 1107.Pp 1108.Cm mss 1109(maximum segment size), 1110.Cm window 1111(tcp window advertisement), 1112.Cm sack 1113(selective ack), 1114.Cm ts 1115(rfc1323 timestamp) and 1116.Cm cc 1117(rfc1644 t/tcp connection count). 1118The absence of a particular option may be denoted 1119with a 1120.Ql \&! . 1121.It Cm uid Ar user 1122Match all TCP or UDP packets sent by or received for a 1123.Ar user . 1124A 1125.Ar user 1126may be matched by name or identification number. 1127.It Cm verrevpath 1128For incoming packets, 1129a routing table lookup is done on the packet's source address. 1130If the interface on which the packet entered the system matches the 1131outgoing interface for the route, 1132the packet matches. 1133If the interfaces do not match up, 1134the packet does not match. 1135All outgoing packets or packets with no incoming interface match. 1136.Pp 1137The name and functionality of the option is intentionally similar to 1138the Cisco IOS command: 1139.Pp 1140.Dl ip verify unicast reverse-path 1141.Pp 1142This option can be used to make anti-spoofing rules. 1143.El 1144.Sh SETS OF RULES 1145Each rule belongs to one of 32 different 1146.Em sets 1147, numbered 0 to 31. 1148Set 31 is reserved for the default rule. 1149.Pp 1150By default, rules are put in set 0, unless you use the 1151.Cm set N 1152attribute when entering a new rule. 1153Sets can be individually and atomically enabled or disabled, 1154so this mechanism permits an easy way to store multiple configurations 1155of the firewall and quickly (and atomically) switch between them. 1156The command to enable/disable sets is 1157.Bd -ragged -offset indent 1158.Nm 1159.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ... 1160.Ed 1161.Pp 1162where multiple 1163.Cm enable 1164or 1165.Cm disable 1166sections can be specified. 1167Command execution is atomic on all the sets specified in the command. 1168By default, all sets are enabled. 1169.Pp 1170When you disable a set, its rules behave as if they do not exist 1171in the firewall configuration, with only one exception: 1172.Bd -ragged -offset indent 1173dynamic rules created from a rule before it had been disabled 1174will still be active until they expire. In order to delete 1175dynamic rules you have to explicitly delete the parent rule 1176which generated them. 1177.Ed 1178.Pp 1179The set number of rules can be changed with the command 1180.Bd -ragged -offset indent 1181.Nm 1182.Cm set move 1183.Brq Cm rule Ar rule-number | old-set 1184.Cm to Ar new-set 1185.Ed 1186.Pp 1187Also, you can atomically swap two rulesets with the command 1188.Bd -ragged -offset indent 1189.Nm 1190.Cm set swap Ar first-set second-set 1191.Ed 1192.Pp 1193See the 1194.Sx EXAMPLES 1195Section on some possible uses of sets of rules. 1196.Sh STATEFUL FIREWALL 1197Stateful operation is a way for the firewall to dynamically 1198create rules for specific flows when packets that 1199match a given pattern are detected. Support for stateful 1200operation comes through the 1201.Cm check-state , keep-state 1202and 1203.Cm limit 1204options of 1205.Nm rules. 1206.Pp 1207Dynamic rules are created when a packet matches a 1208.Cm keep-state 1209or 1210.Cm limit 1211rule, causing the creation of a 1212.Em dynamic 1213rule which will match all and only packets with 1214a given 1215.Em protocol 1216between a 1217.Em src-ip/src-port dst-ip/dst-port 1218pair of addresses ( 1219.Em src 1220and 1221.Em dst 1222are used here only to denote the initial match addresses, but they 1223are completely equivalent afterwards). 1224Dynamic rules will be checked at the first 1225.Cm check-state, keep-state 1226or 1227.Cm limit 1228occurrence, and the action performed upon a match will be the same 1229as in the parent rule. 1230.Pp 1231Note that no additional attributes other than protocol and IP addresses 1232and ports are checked on dynamic rules. 1233.Pp 1234The typical use of dynamic rules is to keep a closed firewall configuration, 1235but let the first TCP SYN packet from the inside network install a 1236dynamic rule for the flow so that packets belonging to that session 1237will be allowed through the firewall: 1238.Pp 1239.Dl "ipfw add check-state" 1240.Dl "ipfw add allow tcp from my-subnet to any setup keep-state" 1241.Dl "ipfw add deny tcp from any to any" 1242.Pp 1243A similar approach can be used for UDP, where an UDP packet coming 1244from the inside will install a dynamic rule to let the response through 1245the firewall: 1246.Pp 1247.Dl "ipfw add check-state" 1248.Dl "ipfw add allow udp from my-subnet to any keep-state" 1249.Dl "ipfw add deny udp from any to any" 1250.Pp 1251Dynamic rules expire after some time, which depends on the status 1252of the flow and the setting of some 1253.Cm sysctl 1254variables. 1255See Section 1256.Sx SYSCTL VARIABLES 1257for more details. 1258For TCP sessions, dynamic rules can be instructed to periodically 1259send keepalive packets to refresh the state of the rule when it is 1260about to expire. 1261.Pp 1262See Section 1263.Sx EXAMPLES 1264for more examples on how to use dynamic rules. 1265.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 1266.Nm 1267is also the user interface for the 1268.Xr dummynet 4 1269traffic shaper. 1270.Pp 1271.Nm dummynet 1272operates by first using the firewall to classify packets and divide them into 1273.Em flows , 1274using any match pattern that can be used in 1275.Nm 1276rules. 1277Depending on local policies, a flow can contain packets for a single 1278TCP connection, or from/to a given host, or entire subnet, or a 1279protocol type, etc. 1280.Pp 1281Packets belonging to the same flow are then passed to either of two 1282different objects, which implement the traffic regulation: 1283.Bl -hang -offset XXXX 1284.It Em pipe 1285A pipe emulates a link with given bandwidth, propagation delay, 1286queue size and packet loss rate. 1287Packets are queued in front of the pipe as they come out from the classifier, 1288and then transferred to the pipe according to the pipe's parameters. 1289.Pp 1290.It Em queue 1291A queue 1292is an abstraction used to implement the WF2Q+ 1293(Worst-case Fair Weighted Fair Queueing) policy, which is 1294an efficient variant of the WFQ policy. 1295.br 1296The queue associates a 1297.Em weight 1298and a reference pipe to each flow, and then all backlogged (i.e., 1299with packets queued) flows linked to the same pipe share the pipe's 1300bandwidth proportionally to their weights. 1301Note that weights are not priorities; a flow with a lower weight 1302is still guaranteed to get its fraction of the bandwidth even if a 1303flow with a higher weight is permanently backlogged. 1304.Pp 1305.El 1306In practice, 1307.Em pipes 1308can be used to set hard limits to the bandwidth that a flow can use, whereas 1309.Em queues 1310can be used to determine how different flow share the available bandwidth. 1311.Pp 1312The 1313.Em pipe 1314and 1315.Em queue 1316configuration commands are the following: 1317.Bd -ragged -offset indent 1318.Cm pipe Ar number Cm config Ar pipe-configuration 1319.Pp 1320.Cm queue Ar number Cm config Ar queue-configuration 1321.Ed 1322.Pp 1323The following parameters can be configured for a pipe: 1324.Pp 1325.Bl -tag -width indent -compact 1326.It Cm bw Ar bandwidth | device 1327Bandwidth, measured in 1328.Sm off 1329.Op Cm K | M 1330.Brq Cm bit/s | Byte/s . 1331.Sm on 1332.Pp 1333A value of 0 (default) means unlimited bandwidth. 1334The unit must immediately follow the number, as in 1335.Pp 1336.Dl "ipfw pipe 1 config bw 300Kbit/s" 1337.Pp 1338If a device name is specified instead of a numeric value, as in 1339.Pp 1340.Dl "ipfw pipe 1 config bw tun0" 1341.Pp 1342then the transmit clock is supplied by the specified device. 1343At the moment only the 1344.Xr tun 4 1345device supports this 1346functionality, for use in conjunction with 1347.Xr ppp 8 . 1348.Pp 1349.It Cm delay Ar ms-delay 1350Propagation delay, measured in milliseconds. 1351The value is rounded to the next multiple of the clock tick 1352(typically 10ms, but it is a good practice to run kernels 1353with 1354.Dq "options HZ=1000" 1355to reduce 1356the granularity to 1ms or less). 1357Default value is 0, meaning no delay. 1358.El 1359.Pp 1360The following parameters can be configured for a queue: 1361.Pp 1362.Bl -tag -width indent -compact 1363.It Cm pipe Ar pipe_nr 1364Connects a queue to the specified pipe. 1365Multiple queues (with the same or different weights) can be connected to 1366the same pipe, which specifies the aggregate rate for the set of queues. 1367.Pp 1368.It Cm weight Ar weight 1369Specifies the weight to be used for flows matching this queue. 1370The weight must be in the range 1..100, and defaults to 1. 1371.El 1372.Pp 1373Finally, the following parameters can be configured for both 1374pipes and queues: 1375.Pp 1376.Bl -tag -width XXXX -compact 1377.Pp 1378.It Cm buckets Ar hash-table-size 1379Specifies the size of the hash table used for storing the 1380various queues. 1381Default value is 64 controlled by the 1382.Xr sysctl 8 1383variable 1384.Em net.inet.ip.dummynet.hash_size , 1385allowed range is 16 to 65536. 1386.Pp 1387.It Cm mask Ar mask-specifier 1388Packets sent to a given pipe or queue by an 1389.Nm 1390rule can be further classified into multiple flows, each of which is then 1391sent to a different 1392.Em dynamic 1393pipe or queue. 1394A flow identifier is constructed by masking the IP addresses, 1395ports and protocol types as specified with the 1396.Cm mask 1397options in the configuration of the pipe or queue. 1398For each different flow identifier, a new pipe or queue is created 1399with the same parameters as the original object, and matching packets 1400are sent to it. 1401.Pp 1402Thus, when 1403.Em dynamic pipes 1404are used, each flow will get the same bandwidth as defined by the pipe, 1405whereas when 1406.Em dynamic queues 1407are used, each flow will share the parent's pipe bandwidth evenly 1408with other flows generated by the same queue (note that other queues 1409with different weights might be connected to the same pipe). 1410.br 1411Available mask specifiers are a combination of one or more of the following: 1412.Pp 1413.Cm dst-ip Ar mask , 1414.Cm src-ip Ar mask , 1415.Cm dst-port Ar mask , 1416.Cm src-port Ar mask , 1417.Cm proto Ar mask 1418or 1419.Cm all , 1420.Pp 1421where the latter means all bits in all fields are significant. 1422.Pp 1423.It Cm noerror 1424When a packet is dropped by a dummynet queue or pipe, the error 1425is normally reported to the caller routine in the kernel, in the 1426same way as it happens when a device queue fills up. Setting this 1427option reports the packet as successfully delivered, which can be 1428needed for some experimental setups where you want to simulate 1429loss or congestion at a remote router. 1430.Pp 1431.It Cm plr Ar packet-loss-rate 1432Packet loss rate. 1433Argument 1434.Ar packet-loss-rate 1435is a floating-point number between 0 and 1, with 0 meaning no 1436loss, 1 meaning 100% loss. 1437The loss rate is internally represented on 31 bits. 1438.Pp 1439.It Cm queue Brq Ar slots | size Ns Cm Kbytes 1440Queue size, in 1441.Ar slots 1442or 1443.Cm KBytes . 1444Default value is 50 slots, which 1445is the typical queue size for Ethernet devices. 1446Note that for slow speed links you should keep the queue 1447size short or your traffic might be affected by a significant 1448queueing delay. 1449E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit 1450or 20s of queue on a 30Kbit/s pipe. 1451Even worse effect can result if you get packets from an 1452interface with a much larger MTU, e.g. the loopback interface 1453with its 16KB packets. 1454.Pp 1455.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p 1456Make use of the RED (Random Early Detection) queue management algorithm. 1457.Ar w_q 1458and 1459.Ar max_p 1460are floating 1461point numbers between 0 and 1 (0 not included), while 1462.Ar min_th 1463and 1464.Ar max_th 1465are integer numbers specifying thresholds for queue management 1466(thresholds are computed in bytes if the queue has been defined 1467in bytes, in slots otherwise). 1468The 1469.Xr dummynet 4 1470also supports the gentle RED variant (gred). 1471Three 1472.Xr sysctl 8 1473variables can be used to control the RED behaviour: 1474.Bl -tag -width indent 1475.It Em net.inet.ip.dummynet.red_lookup_depth 1476specifies the accuracy in computing the average queue 1477when the link is idle (defaults to 256, must be greater than zero) 1478.It Em net.inet.ip.dummynet.red_avg_pkt_size 1479specifies the expected average packet size (defaults to 512, must be 1480greater than zero) 1481.It Em net.inet.ip.dummynet.red_max_pkt_size 1482specifies the expected maximum packet size, only used when queue 1483thresholds are in bytes (defaults to 1500, must be greater than zero). 1484.El 1485.El 1486.Sh CHECKLIST 1487Here are some important points to consider when designing your 1488rules: 1489.Bl -bullet 1490.It 1491Remember that you filter both packets going 1492.Cm in 1493and 1494.Cm out . 1495Most connections need packets going in both directions. 1496.It 1497Remember to test very carefully. 1498It is a good idea to be near the console when doing this. 1499If you cannot be near the console, 1500use an auto-recovery script such as the one in 1501.Pa /usr/share/examples/ipfw/change_rules.sh . 1502.It 1503Don't forget the loopback interface. 1504.El 1505.Sh FINE POINTS 1506.Bl -bullet 1507.It 1508There are circumstances where fragmented datagrams are unconditionally 1509dropped. 1510TCP packets are dropped if they do not contain at least 20 bytes of 1511TCP header, UDP packets are dropped if they do not contain a full 8 1512byte UDP header, and ICMP packets are dropped if they do not contain 15134 bytes of ICMP header, enough to specify the ICMP type, code, and 1514checksum. 1515These packets are simply logged as 1516.Dq pullup failed 1517since there may not be enough good data in the packet to produce a 1518meaningful log entry. 1519.It 1520Another type of packet is unconditionally dropped, a TCP packet with a 1521fragment offset of one. 1522This is a valid packet, but it only has one use, to try 1523to circumvent firewalls. 1524When logging is enabled, these packets are 1525reported as being dropped by rule -1. 1526.It 1527If you are logged in over a network, loading the 1528.Xr kld 4 1529version of 1530.Nm 1531is probably not as straightforward as you would think. 1532I recommend the following command line: 1533.Bd -literal -offset indent 1534kldload ipfw && \e 1535ipfw add 32000 allow ip from any to any 1536.Ed 1537.Pp 1538Along the same lines, doing an 1539.Bd -literal -offset indent 1540ipfw flush 1541.Ed 1542.Pp 1543in similar surroundings is also a bad idea. 1544.It 1545The 1546.Nm 1547filter list may not be modified if the system security level 1548is set to 3 or higher 1549(see 1550.Xr init 8 1551for information on system security levels). 1552.El 1553.Sh PACKET DIVERSION 1554A 1555.Xr divert 4 1556socket bound to the specified port will receive all packets 1557diverted to that port. 1558If no socket is bound to the destination port, or if the kernel 1559wasn't compiled with divert socket support, the packets are 1560dropped. 1561.Sh SYSCTL VARIABLES 1562A set of 1563.Xr sysctl 8 1564variables controls the behaviour of the firewall and 1565associated modules ( 1566.Nm dummynet, bridge 1567). 1568These are shown below together with their default value 1569(but always check with the 1570.Xr sysctl 8 1571command what value is actually in use) and meaning: 1572.Bl -tag -width indent 1573.It Em net.inet.ip.dummynet.expire : No 1 1574Lazily delete dynamic pipes/queue once they have no pending traffic. 1575You can disable this by setting the variable to 0, in which case 1576the pipes/queues will only be deleted when the threshold is reached. 1577.It Em net.inet.ip.dummynet.hash_size : No 64 1578Default size of the hash table used for dynamic pipes/queues. 1579This value is used when no 1580.Cm buckets 1581option is specified when configuring a pipe/queue. 1582.It Em net.inet.ip.dummynet.max_chain_len : No 16 1583Target value for the maximum number of pipes/queues in a hash bucket. 1584The product 1585.Cm max_chain_len*hash_size 1586is used to determine the threshold over which empty pipes/queues 1587will be expired even when 1588.Cm net.inet.ip.dummynet.expire=0 . 1589.It Em net.inet.ip.dummynet.red_lookup_depth : No 256 1590.It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512 1591.It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500 1592Parameters used in the computations of the drop probability 1593for the RED algorithm. 1594.It Em net.inet.ip.fw.autoinc_step : No 100 1595Delta between rule numbers when auto-generating them. 1596The value must be in the range 1..1000. 1597.It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets 1598The current number of buckets in the hash table for dynamic rules 1599(readonly). 1600.It Em net.inet.ip.fw.debug : No 1 1601Controls debugging messages produced by 1602.Nm . 1603.It Em net.inet.ip.fw.dyn_buckets : No 256 1604The number of buckets in the hash table for dynamic rules. 1605Must be a power of 2, up to 65536. 1606It only takes effect when all dynamic rules have expired, so you 1607are advised to use a 1608.Cm flush 1609command to make sure that the hash table is resized. 1610.It Em net.inet.ip.fw.dyn_count : No 3 1611Current number of dynamic rules 1612(read-only). 1613.It Em net.inet.ip.fw.dyn_keepalive : No 1 1614Enables generation of keepalive packets for 1615.Cm keep-state 1616rules on TCP sessions. A keepalive is generated to both 1617sides of the connection every 5 seconds for the last 20 1618seconds of the lifetime of the rule. 1619.It Em net.inet.ip.fw.dyn_max : No 8192 1620Maximum number of dynamic rules. 1621When you hit this limit, no more dynamic rules can be 1622installed until old ones expire. 1623.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300 1624.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20 1625.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1 1626.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1 1627.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5 1628.It Em net.inet.ip.fw.dyn_short_lifetime : No 30 1629These variables control the lifetime, in seconds, of dynamic 1630rules. 1631Upon the initial SYN exchange the lifetime is kept short, 1632then increased after both SYN have been seen, then decreased 1633again during the final FIN exchange or when a RST is received. 1634Both 1635.Em dyn_fin_lifetime 1636and 1637.Em dyn_rst_lifetime 1638must be strictly lower than 5 seconds, the period of 1639repetition of keepalives. The firewall enforces that. 1640.It Em net.inet.ip.fw.enable : No 1 1641Enables the firewall. 1642Setting this variable to 0 lets you run your machine without 1643firewall even if compiled in. 1644.It Em net.inet.ip.fw.one_pass : No 1 1645When set, the packet exiting from the 1646.Xr dummynet 4 1647pipe is not passed though the firewall again. 1648Otherwise, after a pipe action, the packet is 1649reinjected into the firewall at the next rule. 1650.It Em net.inet.ip.fw.verbose : No 1 1651Enables verbose messages. 1652.It Em net.inet.ip.fw.verbose_limit : No 0 1653Limits the number of messages produced by a verbose firewall. 1654.It Em net.link.ether.ipfw : No 0 1655Controls whether layer-2 packets are passed to 1656.Nm . 1657Default is no. 1658.It Em net.link.ether.bridge_ipfw : No 0 1659Controls whether bridged packets are passed to 1660.Nm . 1661Default is no. 1662.El 1663.Sh USING IPFW2 IN FreeBSD-STABLE 1664.Nm ipfw2 1665is standard in 1666.Fx 1667CURRENT, whereas 1668.Fx 1669STABLE still uses 1670.Nm ipfw1 1671unless the kernel is compiled with 1672.Cm options IPFW2 , 1673and 1674.Nm /sbin/ipfw 1675and 1676.Nm /usr/lib/libalias 1677are recompiled with 1678.Cm -DIPFW2 1679and reinstalled (the same effect can be achieved by adding 1680.Cm IPFW2=TRUE 1681to 1682.Nm /etc/make.conf 1683before a buildworld). 1684.Pp 1685.Sh IPFW2 ENHANCEMENTS 1686This Section lists the features that have been introduced in 1687.Nm ipfw2 1688which were not present in 1689.Nm ipfw1 . 1690We list them in order of the potential impact that they can 1691have in writing your rulesets. 1692You might want to consider using these features in order to 1693write your rulesets in a more efficient way. 1694.Bl -tag -width indent 1695.It Handling of non-IPv4 packets 1696.Nm ipfw1 1697will silently accept all non-IPv4 packets (which 1698.Nm ipfw1 1699will only see when 1700.Em net.link.ether.bridge_ipfw=1 Ns 1701). 1702.Nm ipfw2 1703will filter all packets (including non-IPv4 ones) according to the ruleset. 1704To achieve the same behaviour as 1705.Nm ipfw1 1706you can use the following as the very first rule in your ruleset: 1707.Pp 1708.Dl "ipfw add 1 allow layer2 not mac-type ip" 1709.Pp 1710The 1711.Cm layer2 1712option might seem redundant, but it is necessary -- packets 1713passed to the firewall from layer3 will not have a MAC header, 1714so the 1715.Cm mac-type ip 1716pattern will always fail on them, and the 1717.Cm not 1718operator will make this rule into a pass-all. 1719.It Address sets 1720.Nm ipfw1 1721does not supports address sets (those in the form 1722.Ar addr/masklen{num,num,...} 1723). 1724.Pp 1725.It Port specifications 1726.Nm ipfw1 1727only allows one port range when specifying TCP and UDP ports, and 1728is limited to 10 entries instead of the 15 allowed by 1729.Nm ipfw2 . 1730Also, in 1731.Nm ipfw1 1732you can only specify ports when the rule is requesting 1733.Cm tcp 1734or 1735.Cm udp 1736packets. With 1737.Nm ipfw2 1738you can put port specifications in rules matching all packets, 1739and the match will be attempted only on those packets carrying 1740protocols which include port identifiers. 1741.Pp 1742Finally, 1743.Nm ipfw1 1744allowed the first port entry to be specified as 1745.Ar port:mask 1746where 1747.Ar mask 1748can be an arbitrary 16-bit mask. 1749This syntax is of questionable usefulness and it is not 1750supported anymore in 1751.Nm ipfw2 . 1752.It Or-blocks 1753.Nm ipfw1 1754does not support Or-blocks. 1755.It keepalives 1756.Nm ipfw1 1757does not generate keepalives for stateful sessions. 1758As a consequence, it might cause idle sessions to drop because 1759the lifetime of the dynamic rules expires. 1760.It Sets of rules 1761.Nm ipfw1 1762does not implement sets of rules. 1763.It MAC header filtering and Layer-2 firewalling. 1764.Nm ipfw1 1765does not implement filtering on MAC header fields, nor is it 1766invoked on packets from 1767.Cm ether_demux() 1768and 1769.Cm ether_output_frame(). 1770The sysctl variable 1771.Em net.link.ether.ipfw 1772has no effect there. 1773.It Options 1774In 1775.Nm ipfw1 , 1776the following options only accept a single value as an argument: 1777.Pp 1778.Cm ipid, iplen, ipttl 1779.Pp 1780The following options are not implemented by 1781.Nm ipfw1 : 1782.Pp 1783.Cm dst-ip, dst-port, layer2, mac, mac-type, src-ip, src-port. 1784.Pp 1785Additionally, the RELENG_4 version of 1786.Nm ipfw1 1787does not implement the following options: 1788.Pp 1789.Cm ipid, iplen, ipprecedence, iptos, ipttl, 1790.Cm ipversion, tcpack, tcpseq, tcpwin . 1791.It Dummynet options 1792The following option for 1793.Nm dummynet 1794pipes/queues is not supported: 1795.Cm noerror . 1796.El 1797.Sh EXAMPLES 1798There are far too many possible uses of 1799.Nm 1800so this Section will only give a small set of examples. 1801.Pp 1802.Ss BASIC PACKET FILTERING 1803This command adds an entry which denies all tcp packets from 1804.Em cracker.evil.org 1805to the telnet port of 1806.Em wolf.tambov.su 1807from being forwarded by the host: 1808.Pp 1809.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet" 1810.Pp 1811This one disallows any connection from the entire cracker's 1812network to my host: 1813.Pp 1814.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org" 1815.Pp 1816A first and efficient way to limit access (not using dynamic rules) 1817is the use of the following rules: 1818.Pp 1819.Dl "ipfw add allow tcp from any to any established" 1820.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup" 1821.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup" 1822.Dl "..." 1823.Dl "ipfw add deny tcp from any to any" 1824.Pp 1825The first rule will be a quick match for normal TCP packets, 1826but it will not match the initial SYN packet, which will be 1827matched by the 1828.Cm setup 1829rules only for selected source/destination pairs. 1830All other SYN packets will be rejected by the final 1831.Cm deny 1832rule. 1833.Pp 1834If you administer one or more subnets, you can take advantage of the 1835.Nm ipfw2 1836syntax to specify address sets and or-blocks and write extremely 1837compact rulesets which selectively enable services to blocks 1838of clients, as below: 1839.Pp 1840.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q" 1841.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q" 1842.Dl "" 1843.Dl "ipfw add allow ip from ${goodguys} to any" 1844.Dl "ipfw add deny ip from ${badguys} to any" 1845.Dl "... normal policies ..." 1846.Pp 1847The 1848.Nm ipfw1 1849syntax would require a separate rule for each IP in the above 1850example. 1851.Pp 1852The 1853.Cm verrevpath 1854option could be used to do automated anti-spoofing by adding the 1855following to the top of a ruleset: 1856.Pp 1857.Dl "ipfw add deny ip from any to any not verrevpath in" 1858.Pp 1859This rule drops all incoming packets that appear to be coming to the 1860sytem on the wrong interface. For example, a packet with a source 1861address belonging to a host on a protected internal network would be 1862dropped if it tried to enter the system from an external interface. 1863.Ss DYNAMIC RULES 1864In order to protect a site from flood attacks involving fake 1865TCP packets, it is safer to use dynamic rules: 1866.Pp 1867.Dl "ipfw add check-state" 1868.Dl "ipfw add deny tcp from any to any established" 1869.Dl "ipfw add allow tcp from my-net to any setup keep-state" 1870.Pp 1871This will let the firewall install dynamic rules only for 1872those connection which start with a regular SYN packet coming 1873from the inside of our network. 1874Dynamic rules are checked when encountering the first 1875.Cm check-state 1876or 1877.Cm keep-state 1878rule. 1879A 1880.Cm check-state 1881rule should usually be placed near the beginning of the 1882ruleset to minimize the amount of work scanning the ruleset. 1883Your mileage may vary. 1884.Pp 1885To limit the number of connections a user can open 1886you can use the following type of rules: 1887.Pp 1888.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10" 1889.Dl "ipfw add allow tcp from any to me setup limit src-addr 4" 1890.Pp 1891The former (assuming it runs on a gateway) will allow each host 1892on a /24 network to open at most 10 TCP connections. 1893The latter can be placed on a server to make sure that a single 1894client does not use more than 4 simultaneous connections. 1895.Pp 1896.Em BEWARE : 1897stateful rules can be subject to denial-of-service attacks 1898by a SYN-flood which opens a huge number of dynamic rules. 1899The effects of such attacks can be partially limited by 1900acting on a set of 1901.Xr sysctl 8 1902variables which control the operation of the firewall. 1903.Pp 1904Here is a good usage of the 1905.Cm list 1906command to see accounting records and timestamp information: 1907.Pp 1908.Dl ipfw -at list 1909.Pp 1910or in short form without timestamps: 1911.Pp 1912.Dl ipfw -a list 1913.Pp 1914which is equivalent to: 1915.Pp 1916.Dl ipfw show 1917.Pp 1918Next rule diverts all incoming packets from 192.168.2.0/24 1919to divert port 5000: 1920.Pp 1921.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in 1922.Pp 1923.Ss TRAFFIC SHAPING 1924The following rules show some of the applications of 1925.Nm 1926and 1927.Xr dummynet 4 1928for simulations and the like. 1929.Pp 1930This rule drops random incoming packets with a probability 1931of 5%: 1932.Pp 1933.Dl "ipfw add prob 0.05 deny ip from any to any in" 1934.Pp 1935A similar effect can be achieved making use of dummynet pipes: 1936.Pp 1937.Dl "ipfw add pipe 10 ip from any to any" 1938.Dl "ipfw pipe 10 config plr 0.05" 1939.Pp 1940We can use pipes to artificially limit bandwidth, e.g. on a 1941machine acting as a router, if we want to limit traffic from 1942local clients on 192.168.2.0/24 we do: 1943.Pp 1944.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 1945.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 1946.Pp 1947note that we use the 1948.Cm out 1949modifier so that the rule is not used twice. 1950Remember in fact that 1951.Nm 1952rules are checked both on incoming and outgoing packets. 1953.Pp 1954Should we want to simulate a bidirectional link with bandwidth 1955limitations, the correct way is the following: 1956.Pp 1957.Dl "ipfw add pipe 1 ip from any to any out" 1958.Dl "ipfw add pipe 2 ip from any to any in" 1959.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes" 1960.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes" 1961.Pp 1962The above can be very useful, e.g. if you want to see how 1963your fancy Web page will look for a residential user who 1964is connected only through a slow link. 1965You should not use only one pipe for both directions, unless 1966you want to simulate a half-duplex medium (e.g. AppleTalk, 1967Ethernet, IRDA). 1968It is not necessary that both pipes have the same configuration, 1969so we can also simulate asymmetric links. 1970.Pp 1971Should we want to verify network performance with the RED queue 1972management algorithm: 1973.Pp 1974.Dl "ipfw add pipe 1 ip from any to any" 1975.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1" 1976.Pp 1977Another typical application of the traffic shaper is to 1978introduce some delay in the communication. 1979This can significantly affect applications which do a lot of Remote 1980Procedure Calls, and where the round-trip-time of the 1981connection often becomes a limiting factor much more than 1982bandwidth: 1983.Pp 1984.Dl "ipfw add pipe 1 ip from any to any out" 1985.Dl "ipfw add pipe 2 ip from any to any in" 1986.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s" 1987.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s" 1988.Pp 1989Per-flow queueing can be useful for a variety of purposes. 1990A very simple one is counting traffic: 1991.Pp 1992.Dl "ipfw add pipe 1 tcp from any to any" 1993.Dl "ipfw add pipe 1 udp from any to any" 1994.Dl "ipfw add pipe 1 ip from any to any" 1995.Dl "ipfw pipe 1 config mask all" 1996.Pp 1997The above set of rules will create queues (and collect 1998statistics) for all traffic. 1999Because the pipes have no limitations, the only effect is 2000collecting statistics. 2001Note that we need 3 rules, not just the last one, because 2002when 2003.Nm 2004tries to match IP packets it will not consider ports, so we 2005would not see connections on separate ports as different 2006ones. 2007.Pp 2008A more sophisticated example is limiting the outbound traffic 2009on a net with per-host limits, rather than per-network limits: 2010.Pp 2011.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 2012.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in" 2013.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 2014.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 2015.Ss SETS OF RULES 2016To add a set of rules atomically, e.g. set 18: 2017.Pp 2018.Dl "ipfw set disable 18" 2019.Dl "ipfw add NN set 18 ... # repeat as needed" 2020.Dl "ipfw set enable 18" 2021.Pp 2022To delete a set of rules atomically the command is simply: 2023.Pp 2024.Dl "ipfw delete set 18" 2025.Pp 2026To test a ruleset and disable it and regain control if something goes wrong: 2027.Pp 2028.Dl "ipfw set disable 18" 2029.Dl "ipfw add NN set 18 ... # repeat as needed" 2030.Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18" 2031.Pp 2032Here if everything goes well, you press control-C before the "sleep" 2033terminates, and your ruleset will be left active. Otherwise, e.g. if 2034you cannot access your box, the ruleset will be disabled after 2035the sleep terminates thus restoring the previous situation. 2036.Sh SEE ALSO 2037.Xr cpp 1 , 2038.Xr m4 1 , 2039.Xr bridge 4 , 2040.Xr divert 4 , 2041.Xr dummynet 4 , 2042.Xr ip 4 , 2043.Xr ipfirewall 4 , 2044.Xr protocols 5 , 2045.Xr services 5 , 2046.Xr init 8 , 2047.Xr kldload 8 , 2048.Xr reboot 8 , 2049.Xr sysctl 8 , 2050.Xr syslogd 8 2051.Sh BUGS 2052The syntax has grown over the years and sometimes it might be confusing. 2053Unfortunately, backward compatibility prevents cleaning up mistakes 2054made in the definition of the syntax. 2055.Pp 2056.Em !!! WARNING !!! 2057.Pp 2058Misconfiguring the firewall can put your computer in an unusable state, 2059possibly shutting down network services and requiring console access to 2060regain control of it. 2061.Pp 2062Incoming packet fragments diverted by 2063.Cm divert 2064or 2065.Cm tee 2066are reassembled before delivery to the socket. 2067The action used on those packet is the one from the 2068rule which matches the first fragment of the packet. 2069.Pp 2070Packets that match a 2071.Cm tee 2072rule should not be immediately accepted, but should continue 2073going through the rule list. 2074This may be fixed in a later version. 2075.Pp 2076Packets diverted to userland, and then reinserted by a userland process 2077(such as 2078.Xr natd 8 ) 2079will lose various packet attributes, including their source interface. 2080If a packet is reinserted in this manner, later rules may be incorrectly 2081applied, making the order of 2082.Cm divert 2083rules in the rule sequence very important. 2084.Sh AUTHORS 2085.An Ugen J. S. Antsilevich , 2086.An Poul-Henning Kamp , 2087.An Alex Nash , 2088.An Archie Cobbs , 2089.An Luigi Rizzo . 2090.Pp 2091.An -nosplit 2092API based upon code written by 2093.An Daniel Boulet 2094for BSDI. 2095.Pp 2096Work on 2097.Xr dummynet 4 2098traffic shaper supported by Akamba Corp. 2099.Sh HISTORY 2100The 2101.Nm 2102utility first appeared in 2103.Fx 2.0 . 2104.Xr dummynet 4 2105was introduced in 2106.Fx 2.2.8 . 2107Stateful extensions were introduced in 2108.Fx 4.0 . 2109.Nm ipfw2 2110was introduced in Summer 2002. 2111