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