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