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