1.\" 2.\" $FreeBSD$ 3.\" 4.Dd March 9, 2012 5.Dt IPFW 8 6.Os 7.Sh NAME 8.Nm ipfw 9.Nd User interface for firewall, traffic shaper, packet scheduler, 10in-kernel NAT. 11.Sh SYNOPSIS 12.Ss FIREWALL CONFIGURATION 13.Nm 14.Op Fl cq 15.Cm add 16.Ar rule 17.Nm 18.Op Fl acdefnNStT 19.Op Cm set Ar N 20.Brq Cm list | show 21.Op Ar rule | first-last ... 22.Nm 23.Op Fl f | q 24.Op Cm set Ar N 25.Cm flush 26.Nm 27.Op Fl q 28.Op Cm set Ar N 29.Brq Cm delete | zero | resetlog 30.Op Ar number ... 31.Pp 32.Nm 33.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ... 34.Nm 35.Cm set move 36.Op Cm rule 37.Ar number Cm to Ar number 38.Nm 39.Cm set swap Ar number number 40.Nm 41.Cm set show 42.Ss SYSCTL SHORTCUTS 43.Nm 44.Cm enable 45.Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive 46.Nm 47.Cm disable 48.Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive 49.Ss LOOKUP TABLES 50.Nm 51.Cm table Ar number Cm add Ar addr Ns Oo / Ns Ar masklen Oc Op Ar value 52.Nm 53.Cm table Ar number Cm delete Ar addr Ns Op / Ns Ar masklen 54.Nm 55.Cm table 56.Brq Ar number | all 57.Cm flush 58.Nm 59.Cm table 60.Brq Ar number | all 61.Cm list 62.Ss DUMMYNET CONFIGURATION (TRAFFIC SHAPER AND PACKET SCHEDULER) 63.Nm 64.Brq Cm pipe | queue | sched 65.Ar number 66.Cm config 67.Ar config-options 68.Nm 69.Op Fl s Op Ar field 70.Brq Cm pipe | queue | sched 71.Brq Cm delete | list | show 72.Op Ar number ... 73.Ss IN-KERNEL NAT 74.Nm 75.Op Fl q 76.Cm nat 77.Ar number 78.Cm config 79.Ar config-options 80.Pp 81.Nm 82.Op Fl cfnNqS 83.Oo 84.Fl p Ar preproc 85.Oo 86.Ar preproc-flags 87.Oc 88.Oc 89.Ar pathname 90.Sh DESCRIPTION 91The 92.Nm 93utility is the user interface for controlling the 94.Xr ipfw 4 95firewall, the 96.Xr dummynet 4 97traffic shaper/packet scheduler, and the 98in-kernel NAT services. 99.Pp 100A firewall configuration, or 101.Em ruleset , 102is made of a list of 103.Em rules 104numbered from 1 to 65535. 105Packets are passed to the firewall 106from a number of different places in the protocol stack 107(depending on the source and destination of the packet, 108it is possible for the firewall to be 109invoked multiple times on the same packet). 110The packet passed to the firewall is compared 111against each of the rules in the 112.Em ruleset , 113in rule-number order 114(multiple rules with the same number are permitted, in which case 115they are processed in order of insertion). 116When a match is found, the action corresponding to the 117matching rule is performed. 118.Pp 119Depending on the action and certain system settings, packets 120can be reinjected into the firewall at some rule after the 121matching one for further processing. 122.Pp 123A ruleset always includes a 124.Em default 125rule (numbered 65535) which cannot be modified or deleted, 126and matches all packets. 127The action associated with the 128.Em default 129rule can be either 130.Cm deny 131or 132.Cm allow 133depending on how the kernel is configured. 134.Pp 135If the ruleset includes one or more rules with the 136.Cm keep-state 137or 138.Cm limit 139option, 140the firewall will have a 141.Em stateful 142behaviour, i.e., upon a match it will create 143.Em dynamic rules , 144i.e. rules that match packets with the same 5-tuple 145(protocol, source and destination addresses and ports) 146as the packet which caused their creation. 147Dynamic rules, which have a limited lifetime, are checked 148at the first occurrence of a 149.Cm check-state , 150.Cm keep-state 151or 152.Cm limit 153rule, and are typically used to open the firewall on-demand to 154legitimate traffic only. 155See the 156.Sx STATEFUL FIREWALL 157and 158.Sx EXAMPLES 159Sections below for more information on the stateful behaviour of 160.Nm . 161.Pp 162All rules (including dynamic ones) have a few associated counters: 163a packet count, a byte count, a log count and a timestamp 164indicating the time of the last match. 165Counters can be displayed or reset with 166.Nm 167commands. 168.Pp 169Each rule belongs to one of 32 different 170.Em sets 171, and there are 172.Nm 173commands to atomically manipulate sets, such as enable, 174disable, swap sets, move all rules in a set to another 175one, delete all rules in a set. 176These can be useful to 177install temporary configurations, or to test them. 178See Section 179.Sx SETS OF RULES 180for more information on 181.Em sets . 182.Pp 183Rules can be added with the 184.Cm add 185command; deleted individually or in groups with the 186.Cm delete 187command, and globally (except those in set 31) with the 188.Cm flush 189command; displayed, optionally with the content of the 190counters, using the 191.Cm show 192and 193.Cm list 194commands. 195Finally, counters can be reset with the 196.Cm zero 197and 198.Cm resetlog 199commands. 200.Pp 201.Ss COMMAND OPTIONS 202The following general options are available when invoking 203.Nm : 204.Bl -tag -width indent 205.It Fl a 206Show counter values when listing rules. 207The 208.Cm show 209command implies this option. 210.It Fl b 211Only show the action and the comment, not the body of a rule. 212Implies 213.Fl c . 214.It Fl c 215When entering or showing rules, print them in compact form, 216i.e., omitting the "ip from any to any" string 217when this does not carry any additional information. 218.It Fl d 219When listing, show dynamic rules in addition to static ones. 220.It Fl e 221When listing and 222.Fl d 223is specified, also show expired dynamic rules. 224.It Fl f 225Do not ask for confirmation for commands that can cause problems 226if misused, 227.No i.e. Cm flush . 228If there is no tty associated with the process, this is implied. 229.It Fl i 230When listing a table (see the 231.Sx LOOKUP TABLES 232section below for more information on lookup tables), format values 233as IP addresses. By default, values are shown as integers. 234.It Fl n 235Only check syntax of the command strings, without actually passing 236them to the kernel. 237.It Fl N 238Try to resolve addresses and service names in output. 239.It Fl q 240Be quiet when executing the 241.Cm add , 242.Cm nat , 243.Cm zero , 244.Cm resetlog 245or 246.Cm flush 247commands; 248(implies 249.Fl f ) . 250This is useful when updating rulesets by executing multiple 251.Nm 252commands in a script 253(e.g., 254.Ql sh\ /etc/rc.firewall ) , 255or by processing a file with many 256.Nm 257rules across a remote login session. 258It also stops a table add or delete 259from failing if the entry already exists or is not present. 260.Pp 261The reason why this option may be important is that 262for some of these actions, 263.Nm 264may print a message; if the action results in blocking the 265traffic to the remote client, 266the remote login session will be closed 267and the rest of the ruleset will not be processed. 268Access to the console would then be required to recover. 269.It Fl S 270When listing rules, show the 271.Em set 272each rule belongs to. 273If this flag is not specified, disabled rules will not be 274listed. 275.It Fl s Op Ar field 276When listing pipes, sort according to one of the four 277counters (total or current packets or bytes). 278.It Fl t 279When listing, show last match timestamp converted with ctime(). 280.It Fl T 281When listing, show last match timestamp as seconds from the epoch. 282This form can be more convenient for postprocessing by scripts. 283.El 284.Ss LIST OF RULES AND PREPROCESSING 285To ease configuration, rules can be put into a file which is 286processed using 287.Nm 288as shown in the last synopsis line. 289An absolute 290.Ar pathname 291must be used. 292The file will be read line by line and applied as arguments to the 293.Nm 294utility. 295.Pp 296Optionally, a preprocessor can be specified using 297.Fl p Ar preproc 298where 299.Ar pathname 300is to be piped through. 301Useful preprocessors include 302.Xr cpp 1 303and 304.Xr m4 1 . 305If 306.Ar preproc 307does not start with a slash 308.Pq Ql / 309as its first character, the usual 310.Ev PATH 311name search is performed. 312Care should be taken with this in environments where not all 313file systems are mounted (yet) by the time 314.Nm 315is being run (e.g.\& when they are mounted over NFS). 316Once 317.Fl p 318has been specified, any additional arguments are passed on to the preprocessor 319for interpretation. 320This allows for flexible configuration files (like conditionalizing 321them on the local hostname) and the use of macros to centralize 322frequently required arguments like IP addresses. 323.Ss TRAFFIC SHAPER CONFIGURATION 324The 325.Nm 326.Cm pipe , queue 327and 328.Cm sched 329commands are used to configure the traffic shaper and packet scheduler. 330See the 331.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 332Section below for details. 333.Pp 334If the world and the kernel get out of sync the 335.Nm 336ABI may break, preventing you from being able to add any rules. 337This can 338adversely effect the booting process. 339You can use 340.Nm 341.Cm disable 342.Cm firewall 343to temporarily disable the firewall to regain access to the network, 344allowing you to fix the problem. 345.Sh PACKET FLOW 346A packet is checked against the active ruleset in multiple places 347in the protocol stack, under control of several sysctl variables. 348These places and variables are shown below, and it is important to 349have this picture in mind in order to design a correct ruleset. 350.Bd -literal -offset indent 351 ^ to upper layers V 352 | | 353 +----------->-----------+ 354 ^ V 355 [ip(6)_input] [ip(6)_output] net.inet(6).ip(6).fw.enable=1 356 | | 357 ^ V 358 [ether_demux] [ether_output_frame] net.link.ether.ipfw=1 359 | | 360 +-->--[bdg_forward]-->--+ net.link.bridge.ipfw=1 361 ^ V 362 | to devices | 363.Ed 364.Pp 365The number of 366times the same packet goes through the firewall can 367vary between 0 and 4 depending on packet source and 368destination, and system configuration. 369.Pp 370Note that as packets flow through the stack, headers can be 371stripped or added to it, and so they may or may not be available 372for inspection. 373E.g., incoming packets will include the MAC header when 374.Nm 375is invoked from 376.Cm ether_demux() , 377but the same packets will have the MAC header stripped off when 378.Nm 379is invoked from 380.Cm ip_input() 381or 382.Cm ip6_input() . 383.Pp 384Also note that each packet is always checked against the complete ruleset, 385irrespective of the place where the check occurs, or the source of the packet. 386If a rule contains some match patterns or actions which are not valid 387for the place of invocation (e.g.\& trying to match a MAC header within 388.Cm ip_input 389or 390.Cm ip6_input ), 391the match pattern will not match, but a 392.Cm not 393operator in front of such patterns 394.Em will 395cause the pattern to 396.Em always 397match on those packets. 398It is thus the responsibility of 399the programmer, if necessary, to write a suitable ruleset to 400differentiate among the possible places. 401.Cm skipto 402rules can be useful here, as an example: 403.Bd -literal -offset indent 404# packets from ether_demux or bdg_forward 405ipfw add 10 skipto 1000 all from any to any layer2 in 406# packets from ip_input 407ipfw add 10 skipto 2000 all from any to any not layer2 in 408# packets from ip_output 409ipfw add 10 skipto 3000 all from any to any not layer2 out 410# packets from ether_output_frame 411ipfw add 10 skipto 4000 all from any to any layer2 out 412.Ed 413.Pp 414(yes, at the moment there is no way to differentiate between 415ether_demux and bdg_forward). 416.Sh SYNTAX 417In general, each keyword or argument must be provided as 418a separate command line argument, with no leading or trailing 419spaces. 420Keywords are case-sensitive, whereas arguments may 421or may not be case-sensitive depending on their nature 422(e.g.\& uid's are, hostnames are not). 423.Pp 424Some arguments (e.g. port or address lists) are comma-separated 425lists of values. 426In this case, spaces after commas ',' are allowed to make 427the line more readable. 428You can also put the entire 429command (including flags) into a single argument. 430E.g., the following forms are equivalent: 431.Bd -literal -offset indent 432ipfw -q add deny src-ip 10.0.0.0/24,127.0.0.1/8 433ipfw -q add deny src-ip 10.0.0.0/24, 127.0.0.1/8 434ipfw "-q add deny src-ip 10.0.0.0/24, 127.0.0.1/8" 435.Ed 436.Sh RULE FORMAT 437The format of firewall rules is the following: 438.Bd -ragged -offset indent 439.Bk -words 440.Op Ar rule_number 441.Op Cm set Ar set_number 442.Op Cm prob Ar match_probability 443.Ar action 444.Op Cm log Op Cm logamount Ar number 445.Op Cm altq Ar queue 446.Oo 447.Bro Cm tag | untag 448.Brc Ar number 449.Oc 450.Ar body 451.Ek 452.Ed 453.Pp 454where the body of the rule specifies which information is used 455for filtering packets, among the following: 456.Pp 457.Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact 458.It Layer-2 header fields 459When available 460.It IPv4 and IPv6 Protocol 461TCP, UDP, ICMP, etc. 462.It Source and dest. addresses and ports 463.It Direction 464See Section 465.Sx PACKET FLOW 466.It Transmit and receive interface 467By name or address 468.It Misc. IP header fields 469Version, type of service, datagram length, identification, 470fragment flag (non-zero IP offset), 471Time To Live 472.It IP options 473.It IPv6 Extension headers 474Fragmentation, Hop-by-Hop options, 475Routing Headers, Source routing rthdr0, Mobile IPv6 rthdr2, IPSec options. 476.It IPv6 Flow-ID 477.It Misc. TCP header fields 478TCP flags (SYN, FIN, ACK, RST, etc.), 479sequence number, acknowledgment number, 480window 481.It TCP options 482.It ICMP types 483for ICMP packets 484.It ICMP6 types 485for ICMP6 packets 486.It User/group ID 487When the packet can be associated with a local socket. 488.It Divert status 489Whether a packet came from a divert socket (e.g., 490.Xr natd 8 ) . 491.It Fib annotation state 492Whether a packet has been tagged for using a specific FIB (routing table) 493in future forwarding decisions. 494.El 495.Pp 496Note that some of the above information, e.g.\& source MAC or IP addresses and 497TCP/UDP ports, can be easily spoofed, so filtering on those fields 498alone might not guarantee the desired results. 499.Bl -tag -width indent 500.It Ar rule_number 501Each rule is associated with a 502.Ar rule_number 503in the range 1..65535, with the latter reserved for the 504.Em default 505rule. 506Rules are checked sequentially by rule number. 507Multiple rules can have the same number, in which case they are 508checked (and listed) according to the order in which they have 509been added. 510If a rule is entered without specifying a number, the kernel will 511assign one in such a way that the rule becomes the last one 512before the 513.Em default 514rule. 515Automatic rule numbers are assigned by incrementing the last 516non-default rule number by the value of the sysctl variable 517.Ar net.inet.ip.fw.autoinc_step 518which defaults to 100. 519If this is not possible (e.g.\& because we would go beyond the 520maximum allowed rule number), the number of the last 521non-default value is used instead. 522.It Cm set Ar set_number 523Each rule is associated with a 524.Ar set_number 525in the range 0..31. 526Sets can be individually disabled and enabled, so this parameter 527is of fundamental importance for atomic ruleset manipulation. 528It can be also used to simplify deletion of groups of rules. 529If a rule is entered without specifying a set number, 530set 0 will be used. 531.br 532Set 31 is special in that it cannot be disabled, 533and rules in set 31 are not deleted by the 534.Nm ipfw flush 535command (but you can delete them with the 536.Nm ipfw delete set 31 537command). 538Set 31 is also used for the 539.Em default 540rule. 541.It Cm prob Ar match_probability 542A match is only declared with the specified probability 543(floating point number between 0 and 1). 544This can be useful for a number of applications such as 545random packet drop or 546(in conjunction with 547.Nm dummynet ) 548to simulate the effect of multiple paths leading to out-of-order 549packet delivery. 550.Pp 551Note: this condition is checked before any other condition, including 552ones such as keep-state or check-state which might have side effects. 553.It Cm log Op Cm logamount Ar number 554Packets matching a rule with the 555.Cm log 556keyword will be made available for logging in two ways: 557if the sysctl variable 558.Va net.inet.ip.fw.verbose 559is set to 0 (default), one can use 560.Xr bpf 4 561attached to the 562.Li ipfw0 563pseudo interface. There is no overhead if no 564.Xr bpf 4 565is attached to the pseudo interface. 566.Pp 567If 568.Va net.inet.ip.fw.verbose 569is set to 1, packets will be logged to 570.Xr syslogd 8 571with a 572.Dv LOG_SECURITY 573facility up to a maximum of 574.Cm logamount 575packets. 576If no 577.Cm logamount 578is specified, the limit is taken from the sysctl variable 579.Va net.inet.ip.fw.verbose_limit . 580In both cases, a value of 0 means unlimited logging. 581.Pp 582Once the limit is reached, logging can be re-enabled by 583clearing the logging counter or the packet counter for that entry, see the 584.Cm resetlog 585command. 586.Pp 587Note: logging is done after all other packet matching conditions 588have been successfully verified, and before performing the final 589action (accept, deny, etc.) on the packet. 590.It Cm tag Ar number 591When a packet matches a rule with the 592.Cm tag 593keyword, the numeric tag for the given 594.Ar number 595in the range 1..65534 will be attached to the packet. 596The tag acts as an internal marker (it is not sent out over 597the wire) that can be used to identify these packets later on. 598This can be used, for example, to provide trust between interfaces 599and to start doing policy-based filtering. 600A packet can have multiple tags at the same time. 601Tags are "sticky", meaning once a tag is applied to a packet by a 602matching rule it exists until explicit removal. 603Tags are kept with the packet everywhere within the kernel, but are 604lost when packet leaves the kernel, for example, on transmitting 605packet out to the network or sending packet to a 606.Xr divert 4 607socket. 608.Pp 609To check for previously applied tags, use the 610.Cm tagged 611rule option. 612To delete previously applied tag, use the 613.Cm untag 614keyword. 615.Pp 616Note: since tags are kept with the packet everywhere in kernelspace, 617they can be set and unset anywhere in the kernel network subsystem 618(using the 619.Xr mbuf_tags 9 620facility), not only by means of the 621.Xr ipfw 4 622.Cm tag 623and 624.Cm untag 625keywords. 626For example, there can be a specialized 627.Xr netgraph 4 628node doing traffic analyzing and tagging for later inspecting 629in firewall. 630.It Cm untag Ar number 631When a packet matches a rule with the 632.Cm untag 633keyword, the tag with the number 634.Ar number 635is searched among the tags attached to this packet and, 636if found, removed from it. 637Other tags bound to packet, if present, are left untouched. 638.It Cm altq Ar queue 639When a packet matches a rule with the 640.Cm altq 641keyword, the ALTQ identifier for the given 642.Ar queue 643(see 644.Xr altq 4 ) 645will be attached. 646Note that this ALTQ tag is only meaningful for packets going "out" of IPFW, 647and not being rejected or going to divert sockets. 648Note that if there is insufficient memory at the time the packet is 649processed, it will not be tagged, so it is wise to make your ALTQ 650"default" queue policy account for this. 651If multiple 652.Cm altq 653rules match a single packet, only the first one adds the ALTQ classification 654tag. 655In doing so, traffic may be shaped by using 656.Cm count Cm altq Ar queue 657rules for classification early in the ruleset, then later applying 658the filtering decision. 659For example, 660.Cm check-state 661and 662.Cm keep-state 663rules may come later and provide the actual filtering decisions in 664addition to the fallback ALTQ tag. 665.Pp 666You must run 667.Xr pfctl 8 668to set up the queues before IPFW will be able to look them up by name, 669and if the ALTQ disciplines are rearranged, the rules in containing the 670queue identifiers in the kernel will likely have gone stale and need 671to be reloaded. 672Stale queue identifiers will probably result in misclassification. 673.Pp 674All system ALTQ processing can be turned on or off via 675.Nm 676.Cm enable Ar altq 677and 678.Nm 679.Cm disable Ar altq . 680The usage of 681.Va net.inet.ip.fw.one_pass 682is irrelevant to ALTQ traffic shaping, as the actual rule action is followed 683always after adding an ALTQ tag. 684.El 685.Ss RULE ACTIONS 686A rule can be associated with one of the following actions, which 687will be executed when the packet matches the body of the rule. 688.Bl -tag -width indent 689.It Cm allow | accept | pass | permit 690Allow packets that match rule. 691The search terminates. 692.It Cm check-state 693Checks the packet against the dynamic ruleset. 694If a match is found, execute the action associated with 695the rule which generated this dynamic rule, otherwise 696move to the next rule. 697.br 698.Cm Check-state 699rules do not have a body. 700If no 701.Cm check-state 702rule is found, the dynamic ruleset is checked at the first 703.Cm keep-state 704or 705.Cm limit 706rule. 707.It Cm count 708Update counters for all packets that match rule. 709The search continues with the next rule. 710.It Cm deny | drop 711Discard packets that match this rule. 712The search terminates. 713.It Cm divert Ar port 714Divert packets that match this rule to the 715.Xr divert 4 716socket bound to port 717.Ar port . 718The search terminates. 719.It Cm fwd | forward Ar ipaddr | tablearg Ns Op , Ns Ar port 720Change the next-hop on matching packets to 721.Ar ipaddr , 722which can be an IP address or a host name. 723For IPv4, the next hop can also be supplied by the last table 724looked up for the packet by using the 725.Cm tablearg 726keyword instead of an explicit address. 727The search terminates if this rule matches. 728.Pp 729If 730.Ar ipaddr 731is a local address, then matching packets will be forwarded to 732.Ar port 733(or the port number in the packet if one is not specified in the rule) 734on the local machine. 735.br 736If 737.Ar ipaddr 738is not a local address, then the port number 739(if specified) is ignored, and the packet will be 740forwarded to the remote address, using the route as found in 741the local routing table for that IP. 742.br 743A 744.Ar fwd 745rule will not match layer-2 packets (those received 746on ether_input, ether_output, or bridged). 747.br 748The 749.Cm fwd 750action does not change the contents of the packet at all. 751In particular, the destination address remains unmodified, so 752packets forwarded to another system will usually be rejected by that system 753unless there is a matching rule on that system to capture them. 754For packets forwarded locally, 755the local address of the socket will be 756set to the original destination address of the packet. 757This makes the 758.Xr netstat 1 759entry look rather weird but is intended for 760use with transparent proxy servers. 761.Pp 762To enable 763.Cm fwd 764a custom kernel needs to be compiled with the option 765.Cd "options IPFIREWALL_FORWARD" . 766.It Cm nat Ar nat_nr | tablearg 767Pass packet to a 768nat instance 769(for network address translation, address redirect, etc.): 770see the 771.Sx NETWORK ADDRESS TRANSLATION (NAT) 772Section for further information. 773.It Cm pipe Ar pipe_nr 774Pass packet to a 775.Nm dummynet 776.Dq pipe 777(for bandwidth limitation, delay, etc.). 778See the 779.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 780Section for further information. 781The search terminates; however, on exit from the pipe and if 782the 783.Xr sysctl 8 784variable 785.Va net.inet.ip.fw.one_pass 786is not set, the packet is passed again to the firewall code 787starting from the next rule. 788.It Cm queue Ar queue_nr 789Pass packet to a 790.Nm dummynet 791.Dq queue 792(for bandwidth limitation using WF2Q+). 793.It Cm reject 794(Deprecated). 795Synonym for 796.Cm unreach host . 797.It Cm reset 798Discard packets that match this rule, and if the 799packet is a TCP packet, try to send a TCP reset (RST) notice. 800The search terminates. 801.It Cm reset6 802Discard packets that match this rule, and if the 803packet is a TCP packet, try to send a TCP reset (RST) notice. 804The search terminates. 805.It Cm skipto Ar number | tablearg 806Skip all subsequent rules numbered less than 807.Ar number . 808The search continues with the first rule numbered 809.Ar number 810or higher. 811It is possible to use the 812.Cm tablearg 813keyword with a skipto for a 814.Em computed 815skipto, but care should be used, as no destination caching 816is possible in this case so the rules are always walked to find it, 817starting from the 818.Cm skipto . 819.It Cm call Ar number | tablearg 820The current rule number is saved in the internal stack and 821ruleset processing continues with the first rule numbered 822.Ar number 823or higher. 824If later a rule with the 825.Cm return 826action is encountered, the processing returns to the first rule 827with number of this 828.Cm call 829rule plus one or higher 830(the same behaviour as with packets returning from 831.Xr divert 4 832socket after a 833.Cm divert 834action). 835This could be used to make somewhat like an assembly language 836.Dq subroutine 837calls to rules with common checks for different interfaces, etc. 838.Pp 839Rule with any number could be called, not just forward jumps as with 840.Cm skipto . 841So, to prevent endless loops in case of mistakes, both 842.Cm call 843and 844.Cm return 845actions don't do any jumps and simply go to the next rule if memory 846can't be allocated or stack overflowed/undeflowed. 847.Pp 848Internally stack for rule numbers is implemented using 849.Xr mbuf_tags 9 850facility and currently has size of 16 entries. 851As mbuf tags are lost when packet leaves the kernel, 852.Cm divert 853should not be used in subroutines to avoid endless loops 854and other undesired effects. 855.It Cm return 856Takes rule number saved to internal stack by the last 857.Cm call 858action and returns ruleset processing to the first rule 859with number greater than number of corresponding 860.Cm call 861rule. See description of the 862.Cm call 863action for more details. 864.Pp 865Note that 866.Cm return 867rules usually end a 868.Dq subroutine 869and thus are unconditional, but 870.Nm 871command-line utility currently requires every action except 872.Cm check-state 873to have body. 874While it is sometimes useful to return only on some packets, 875usually you want to print just 876.Dq return 877for readability. 878A workaround for this is to use new syntax and 879.Fl c 880switch: 881.Bd -literal -offset indent 882# Add a rule without actual body 883ipfw add 2999 return via any 884 885# List rules without "from any to any" part 886ipfw -c list 887.Ed 888.Pp 889This cosmetic annoyance may be fixed in future releases. 890.It Cm tee Ar port 891Send a copy of packets matching this rule to the 892.Xr divert 4 893socket bound to port 894.Ar port . 895The search continues with the next rule. 896.It Cm unreach Ar code 897Discard packets that match this rule, and try to send an ICMP 898unreachable notice with code 899.Ar code , 900where 901.Ar code 902is a number from 0 to 255, or one of these aliases: 903.Cm net , host , protocol , port , 904.Cm needfrag , srcfail , net-unknown , host-unknown , 905.Cm isolated , net-prohib , host-prohib , tosnet , 906.Cm toshost , filter-prohib , host-precedence 907or 908.Cm precedence-cutoff . 909The search terminates. 910.It Cm unreach6 Ar code 911Discard packets that match this rule, and try to send an ICMPv6 912unreachable notice with code 913.Ar code , 914where 915.Ar code 916is a number from 0, 1, 3 or 4, or one of these aliases: 917.Cm no-route, admin-prohib, address 918or 919.Cm port . 920The search terminates. 921.It Cm netgraph Ar cookie 922Divert packet into netgraph with given 923.Ar cookie . 924The search terminates. 925If packet is later returned from netgraph it is either 926accepted or continues with the next rule, depending on 927.Va net.inet.ip.fw.one_pass 928sysctl variable. 929.It Cm ngtee Ar cookie 930A copy of packet is diverted into netgraph, original 931packet continues with the next rule. 932See 933.Xr ng_ipfw 4 934for more information on 935.Cm netgraph 936and 937.Cm ngtee 938actions. 939.It Cm setfib Ar fibnum | tablearg 940The packet is tagged so as to use the FIB (routing table) 941.Ar fibnum 942in any subsequent forwarding decisions. 943Initially this is limited to the values 0 through 15, see 944.Xr setfib 1 . 945Processing continues at the next rule. 946It is possible to use the 947.Cm tablearg 948keyword with a setfib. If tablearg value is not within compiled FIB range packet fib is set to 0. 949.It Cm reass 950Queue and reassemble ip fragments. 951If the packet is not fragmented, counters are updated and processing continues with the next rule. 952If the packet is the last logical fragment, the packet is reassembled and, if 953.Va net.inet.ip.fw.one_pass 954is set to 0, processing continues with the next rule, else packet is allowed to pass and search terminates. 955If the packet is a fragment in the middle, it is consumed and processing stops immediately. 956.Pp 957Fragments handling can be tuned via 958.Va net.inet.ip.maxfragpackets 959and 960.Va net.inet.ip.maxfragsperpacket 961which limit, respectively, the maximum number of processable fragments (default: 800) and 962the maximum number of fragments per packet (default: 16). 963.Pp 964NOTA BENE: since fragments do not contain port numbers, they should be avoided with the 965.Nm reass 966rule. 967Alternatively, direction-based (like 968.Nm in 969/ 970.Nm out 971) and source-based (like 972.Nm via 973) match patterns can be used to select fragments. 974.Pp 975Usually a simple rule like: 976.Bd -literal -offset indent 977# reassemble incoming fragments 978ipfw add reass all from any to any in 979.Ed 980.Pp 981is all you need at the beginning of your ruleset. 982.El 983.Ss RULE BODY 984The body of a rule contains zero or more patterns (such as 985specific source and destination addresses or ports, 986protocol options, incoming or outgoing interfaces, etc.) 987that the packet must match in order to be recognised. 988In general, the patterns are connected by (implicit) 989.Cm and 990operators -- i.e., all must match in order for the 991rule to match. 992Individual patterns can be prefixed by the 993.Cm not 994operator to reverse the result of the match, as in 995.Pp 996.Dl "ipfw add 100 allow ip from not 1.2.3.4 to any" 997.Pp 998Additionally, sets of alternative match patterns 999.Pq Em or-blocks 1000can be constructed by putting the patterns in 1001lists enclosed between parentheses ( ) or braces { }, and 1002using the 1003.Cm or 1004operator as follows: 1005.Pp 1006.Dl "ipfw add 100 allow ip from { x or not y or z } to any" 1007.Pp 1008Only one level of parentheses is allowed. 1009Beware that most shells have special meanings for parentheses 1010or braces, so it is advisable to put a backslash \\ in front of them 1011to prevent such interpretations. 1012.Pp 1013The body of a rule must in general include a source and destination 1014address specifier. 1015The keyword 1016.Ar any 1017can be used in various places to specify that the content of 1018a required field is irrelevant. 1019.Pp 1020The rule body has the following format: 1021.Bd -ragged -offset indent 1022.Op Ar proto Cm from Ar src Cm to Ar dst 1023.Op Ar options 1024.Ed 1025.Pp 1026The first part (proto from src to dst) is for backward 1027compatibility with earlier versions of 1028.Fx . 1029In modern 1030.Fx 1031any match pattern (including MAC headers, IP protocols, 1032addresses and ports) can be specified in the 1033.Ar options 1034section. 1035.Pp 1036Rule fields have the following meaning: 1037.Bl -tag -width indent 1038.It Ar proto : protocol | Cm { Ar protocol Cm or ... } 1039.It Ar protocol : Oo Cm not Oc Ar protocol-name | protocol-number 1040An IP protocol specified by number or name 1041(for a complete list see 1042.Pa /etc/protocols ) , 1043or one of the following keywords: 1044.Bl -tag -width indent 1045.It Cm ip4 | ipv4 1046Matches IPv4 packets. 1047.It Cm ip6 | ipv6 1048Matches IPv6 packets. 1049.It Cm ip | all 1050Matches any packet. 1051.El 1052.Pp 1053The 1054.Cm ipv6 1055in 1056.Cm proto 1057option will be treated as inner protocol. 1058And, the 1059.Cm ipv4 1060is not available in 1061.Cm proto 1062option. 1063.Pp 1064The 1065.Cm { Ar protocol Cm or ... } 1066format (an 1067.Em or-block ) 1068is provided for convenience only but its use is deprecated. 1069.It Ar src No and Ar dst : Bro Cm addr | Cm { Ar addr Cm or ... } Brc Op Oo Cm not Oc Ar ports 1070An address (or a list, see below) 1071optionally followed by 1072.Ar ports 1073specifiers. 1074.Pp 1075The second format 1076.Em ( or-block 1077with multiple addresses) is provided for convenience only and 1078its use is discouraged. 1079.It Ar addr : Oo Cm not Oc Bro 1080.Cm any | me | me6 | 1081.Cm table Ns Pq Ar number Ns Op , Ns Ar value 1082.Ar | addr-list | addr-set 1083.Brc 1084.Bl -tag -width indent 1085.It Cm any 1086matches any IP address. 1087.It Cm me 1088matches any IP address configured on an interface in the system. 1089.It Cm me6 1090matches any IPv6 address configured on an interface in the system. 1091The address list is evaluated at the time the packet is 1092analysed. 1093.It Cm table Ns Pq Ar number Ns Op , Ns Ar value 1094Matches any IPv4 address for which an entry exists in the lookup table 1095.Ar number . 1096If an optional 32-bit unsigned 1097.Ar value 1098is also specified, an entry will match only if it has this value. 1099See the 1100.Sx LOOKUP TABLES 1101section below for more information on lookup tables. 1102.El 1103.It Ar addr-list : ip-addr Ns Op Ns , Ns Ar addr-list 1104.It Ar ip-addr : 1105A host or subnet address specified in one of the following ways: 1106.Bl -tag -width indent 1107.It Ar numeric-ip | hostname 1108Matches a single IPv4 address, specified as dotted-quad or a hostname. 1109Hostnames are resolved at the time the rule is added to the firewall list. 1110.It Ar addr Ns / Ns Ar masklen 1111Matches all addresses with base 1112.Ar addr 1113(specified as an IP address, a network number, or a hostname) 1114and mask width of 1115.Cm masklen 1116bits. 1117As an example, 1.2.3.4/25 or 1.2.3.0/25 will match 1118all IP numbers from 1.2.3.0 to 1.2.3.127 . 1119.It Ar addr Ns : Ns Ar mask 1120Matches all addresses with base 1121.Ar addr 1122(specified as an IP address, a network number, or a hostname) 1123and the mask of 1124.Ar mask , 1125specified as a dotted quad. 1126As an example, 1.2.3.4:255.0.255.0 or 1.0.3.0:255.0.255.0 will match 11271.*.3.*. 1128This form is advised only for non-contiguous 1129masks. 1130It is better to resort to the 1131.Ar addr Ns / Ns Ar masklen 1132format for contiguous masks, which is more compact and less 1133error-prone. 1134.El 1135.It Ar addr-set : addr Ns Oo Ns / Ns Ar masklen Oc Ns Cm { Ns Ar list Ns Cm } 1136.It Ar list : Bro Ar num | num-num Brc Ns Op Ns , Ns Ar list 1137Matches all addresses with base address 1138.Ar addr 1139(specified as an IP address, a network number, or a hostname) 1140and whose last byte is in the list between braces { } . 1141Note that there must be no spaces between braces and 1142numbers (spaces after commas are allowed). 1143Elements of the list can be specified as single entries 1144or ranges. 1145The 1146.Ar masklen 1147field is used to limit the size of the set of addresses, 1148and can have any value between 24 and 32. 1149If not specified, 1150it will be assumed as 24. 1151.br 1152This format is particularly useful to handle sparse address sets 1153within a single rule. 1154Because the matching occurs using a 1155bitmask, it takes constant time and dramatically reduces 1156the complexity of rulesets. 1157.br 1158As an example, an address specified as 1.2.3.4/24{128,35-55,89} 1159or 1.2.3.0/24{128,35-55,89} 1160will match the following IP addresses: 1161.br 11621.2.3.128, 1.2.3.35 to 1.2.3.55, 1.2.3.89 . 1163.It Ar addr6-list : ip6-addr Ns Op Ns , Ns Ar addr6-list 1164.It Ar ip6-addr : 1165A host or subnet specified one of the following ways: 1166.Bl -tag -width indent 1167.It Ar numeric-ip | hostname 1168Matches a single IPv6 address as allowed by 1169.Xr inet_pton 3 1170or a hostname. 1171Hostnames are resolved at the time the rule is added to the firewall 1172list. 1173.It Ar addr Ns / Ns Ar masklen 1174Matches all IPv6 addresses with base 1175.Ar addr 1176(specified as allowed by 1177.Xr inet_pton 1178or a hostname) 1179and mask width of 1180.Cm masklen 1181bits. 1182.El 1183.Pp 1184No support for sets of IPv6 addresses is provided because IPv6 addresses 1185are typically random past the initial prefix. 1186.It Ar ports : Bro Ar port | port Ns \&- Ns Ar port Ns Brc Ns Op , Ns Ar ports 1187For protocols which support port numbers (such as TCP and UDP), optional 1188.Cm ports 1189may be specified as one or more ports or port ranges, separated 1190by commas but no spaces, and an optional 1191.Cm not 1192operator. 1193The 1194.Ql \&- 1195notation specifies a range of ports (including boundaries). 1196.Pp 1197Service names (from 1198.Pa /etc/services ) 1199may be used instead of numeric port values. 1200The length of the port list is limited to 30 ports or ranges, 1201though one can specify larger ranges by using an 1202.Em or-block 1203in the 1204.Cm options 1205section of the rule. 1206.Pp 1207A backslash 1208.Pq Ql \e 1209can be used to escape the dash 1210.Pq Ql - 1211character in a service name (from a shell, the backslash must be 1212typed twice to avoid the shell itself interpreting it as an escape 1213character). 1214.Pp 1215.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any" 1216.Pp 1217Fragmented packets which have a non-zero offset (i.e., not the first 1218fragment) will never match a rule which has one or more port 1219specifications. 1220See the 1221.Cm frag 1222option for details on matching fragmented packets. 1223.El 1224.Ss RULE OPTIONS (MATCH PATTERNS) 1225Additional match patterns can be used within 1226rules. 1227Zero or more of these so-called 1228.Em options 1229can be present in a rule, optionally prefixed by the 1230.Cm not 1231operand, and possibly grouped into 1232.Em or-blocks . 1233.Pp 1234The following match patterns can be used (listed in alphabetical order): 1235.Bl -tag -width indent 1236.It Cm // this is a comment. 1237Inserts the specified text as a comment in the rule. 1238Everything following // is considered as a comment and stored in the rule. 1239You can have comment-only rules, which are listed as having a 1240.Cm count 1241action followed by the comment. 1242.It Cm bridged 1243Alias for 1244.Cm layer2 . 1245.It Cm diverted 1246Matches only packets generated by a divert socket. 1247.It Cm diverted-loopback 1248Matches only packets coming from a divert socket back into the IP stack 1249input for delivery. 1250.It Cm diverted-output 1251Matches only packets going from a divert socket back outward to the IP 1252stack output for delivery. 1253.It Cm dst-ip Ar ip-address 1254Matches IPv4 packets whose destination IP is one of the address(es) 1255specified as argument. 1256.It Bro Cm dst-ip6 | dst-ipv6 Brc Ar ip6-address 1257Matches IPv6 packets whose destination IP is one of the address(es) 1258specified as argument. 1259.It Cm dst-port Ar ports 1260Matches IP packets whose destination port is one of the port(s) 1261specified as argument. 1262.It Cm established 1263Matches TCP packets that have the RST or ACK bits set. 1264.It Cm ext6hdr Ar header 1265Matches IPv6 packets containing the extended header given by 1266.Ar header . 1267Supported headers are: 1268.Pp 1269Fragment, 1270.Pq Cm frag , 1271Hop-to-hop options 1272.Pq Cm hopopt , 1273any type of Routing Header 1274.Pq Cm route , 1275Source routing Routing Header Type 0 1276.Pq Cm rthdr0 , 1277Mobile IPv6 Routing Header Type 2 1278.Pq Cm rthdr2 , 1279Destination options 1280.Pq Cm dstopt , 1281IPSec authentication headers 1282.Pq Cm ah , 1283and IPsec encapsulated security payload headers 1284.Pq Cm esp . 1285.It Cm fib Ar fibnum 1286Matches a packet that has been tagged to use 1287the given FIB (routing table) number. 1288.It Cm flow-id Ar labels 1289Matches IPv6 packets containing any of the flow labels given in 1290.Ar labels . 1291.Ar labels 1292is a comma separated list of numeric flow labels. 1293.It Cm frag 1294Matches packets that are fragments and not the first 1295fragment of an IP datagram. 1296Note that these packets will not have 1297the next protocol header (e.g.\& TCP, UDP) so options that look into 1298these headers cannot match. 1299.It Cm gid Ar group 1300Matches all TCP or UDP packets sent by or received for a 1301.Ar group . 1302A 1303.Ar group 1304may be specified by name or number. 1305.It Cm jail Ar prisonID 1306Matches all TCP or UDP packets sent by or received for the 1307jail whos prison ID is 1308.Ar prisonID . 1309.It Cm icmptypes Ar types 1310Matches ICMP packets whose ICMP type is in the list 1311.Ar types . 1312The list may be specified as any combination of 1313individual types (numeric) separated by commas. 1314.Em Ranges are not allowed . 1315The supported ICMP types are: 1316.Pp 1317echo reply 1318.Pq Cm 0 , 1319destination unreachable 1320.Pq Cm 3 , 1321source quench 1322.Pq Cm 4 , 1323redirect 1324.Pq Cm 5 , 1325echo request 1326.Pq Cm 8 , 1327router advertisement 1328.Pq Cm 9 , 1329router solicitation 1330.Pq Cm 10 , 1331time-to-live exceeded 1332.Pq Cm 11 , 1333IP header bad 1334.Pq Cm 12 , 1335timestamp request 1336.Pq Cm 13 , 1337timestamp reply 1338.Pq Cm 14 , 1339information request 1340.Pq Cm 15 , 1341information reply 1342.Pq Cm 16 , 1343address mask request 1344.Pq Cm 17 1345and address mask reply 1346.Pq Cm 18 . 1347.It Cm icmp6types Ar types 1348Matches ICMP6 packets whose ICMP6 type is in the list of 1349.Ar types . 1350The list may be specified as any combination of 1351individual types (numeric) separated by commas. 1352.Em Ranges are not allowed . 1353.It Cm in | out 1354Matches incoming or outgoing packets, respectively. 1355.Cm in 1356and 1357.Cm out 1358are mutually exclusive (in fact, 1359.Cm out 1360is implemented as 1361.Cm not in Ns No ). 1362.It Cm ipid Ar id-list 1363Matches IPv4 packets whose 1364.Cm ip_id 1365field has value included in 1366.Ar id-list , 1367which is either a single value or a list of values or ranges 1368specified in the same way as 1369.Ar ports . 1370.It Cm iplen Ar len-list 1371Matches IP packets whose total length, including header and data, is 1372in the set 1373.Ar len-list , 1374which is either a single value or a list of values or ranges 1375specified in the same way as 1376.Ar ports . 1377.It Cm ipoptions Ar spec 1378Matches packets whose IPv4 header contains the comma separated list of 1379options specified in 1380.Ar spec . 1381The supported IP options are: 1382.Pp 1383.Cm ssrr 1384(strict source route), 1385.Cm lsrr 1386(loose source route), 1387.Cm rr 1388(record packet route) and 1389.Cm ts 1390(timestamp). 1391The absence of a particular option may be denoted 1392with a 1393.Ql \&! . 1394.It Cm ipprecedence Ar precedence 1395Matches IPv4 packets whose precedence field is equal to 1396.Ar precedence . 1397.It Cm ipsec 1398Matches packets that have IPSEC history associated with them 1399(i.e., the packet comes encapsulated in IPSEC, the kernel 1400has IPSEC support and IPSEC_FILTERTUNNEL option, and can correctly 1401decapsulate it). 1402.Pp 1403Note that specifying 1404.Cm ipsec 1405is different from specifying 1406.Cm proto Ar ipsec 1407as the latter will only look at the specific IP protocol field, 1408irrespective of IPSEC kernel support and the validity of the IPSEC data. 1409.Pp 1410Further note that this flag is silently ignored in kernels without 1411IPSEC support. 1412It does not affect rule processing when given and the 1413rules are handled as if with no 1414.Cm ipsec 1415flag. 1416.It Cm iptos Ar spec 1417Matches IPv4 packets whose 1418.Cm tos 1419field contains the comma separated list of 1420service types specified in 1421.Ar spec . 1422The supported IP types of service are: 1423.Pp 1424.Cm lowdelay 1425.Pq Dv IPTOS_LOWDELAY , 1426.Cm throughput 1427.Pq Dv IPTOS_THROUGHPUT , 1428.Cm reliability 1429.Pq Dv IPTOS_RELIABILITY , 1430.Cm mincost 1431.Pq Dv IPTOS_MINCOST , 1432.Cm congestion 1433.Pq Dv IPTOS_ECN_CE . 1434The absence of a particular type may be denoted 1435with a 1436.Ql \&! . 1437.It Cm ipttl Ar ttl-list 1438Matches IPv4 packets whose time to live is included in 1439.Ar ttl-list , 1440which is either a single value or a list of values or ranges 1441specified in the same way as 1442.Ar ports . 1443.It Cm ipversion Ar ver 1444Matches IP packets whose IP version field is 1445.Ar ver . 1446.It Cm keep-state 1447Upon a match, the firewall will create a dynamic rule, whose 1448default behaviour is to match bidirectional traffic between 1449source and destination IP/port using the same protocol. 1450The rule has a limited lifetime (controlled by a set of 1451.Xr sysctl 8 1452variables), and the lifetime is refreshed every time a matching 1453packet is found. 1454.It Cm layer2 1455Matches only layer2 packets, i.e., those passed to 1456.Nm 1457from ether_demux() and ether_output_frame(). 1458.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N 1459The firewall will only allow 1460.Ar N 1461connections with the same 1462set of parameters as specified in the rule. 1463One or more 1464of source and destination addresses and ports can be 1465specified. 1466Currently, 1467only IPv4 flows are supported. 1468.It Cm lookup Bro Cm dst-ip | dst-port | src-ip | src-port | uid | jail Brc Ar N 1469Search an entry in lookup table 1470.Ar N 1471that matches the field specified as argument. 1472If not found, the match fails. 1473Otherwise, the match succeeds and 1474.Cm tablearg 1475is set to the value extracted from the table. 1476.Pp 1477This option can be useful to quickly dispatch traffic based on 1478certain packet fields. 1479See the 1480.Sx LOOKUP TABLES 1481section below for more information on lookup tables. 1482.It Cm { MAC | mac } Ar dst-mac src-mac 1483Match packets with a given 1484.Ar dst-mac 1485and 1486.Ar src-mac 1487addresses, specified as the 1488.Cm any 1489keyword (matching any MAC address), or six groups of hex digits 1490separated by colons, 1491and optionally followed by a mask indicating the significant bits. 1492The mask may be specified using either of the following methods: 1493.Bl -enum -width indent 1494.It 1495A slash 1496.Pq / 1497followed by the number of significant bits. 1498For example, an address with 33 significant bits could be specified as: 1499.Pp 1500.Dl "MAC 10:20:30:40:50:60/33 any" 1501.Pp 1502.It 1503An ampersand 1504.Pq & 1505followed by a bitmask specified as six groups of hex digits separated 1506by colons. 1507For example, an address in which the last 16 bits are significant could 1508be specified as: 1509.Pp 1510.Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any" 1511.Pp 1512Note that the ampersand character has a special meaning in many shells 1513and should generally be escaped. 1514.Pp 1515.El 1516Note that the order of MAC addresses (destination first, 1517source second) is 1518the same as on the wire, but the opposite of the one used for 1519IP addresses. 1520.It Cm mac-type Ar mac-type 1521Matches packets whose Ethernet Type field 1522corresponds to one of those specified as argument. 1523.Ar mac-type 1524is specified in the same way as 1525.Cm port numbers 1526(i.e., one or more comma-separated single values or ranges). 1527You can use symbolic names for known values such as 1528.Em vlan , ipv4, ipv6 . 1529Values can be entered as decimal or hexadecimal (if prefixed by 0x), 1530and they are always printed as hexadecimal (unless the 1531.Cm -N 1532option is used, in which case symbolic resolution will be attempted). 1533.It Cm proto Ar protocol 1534Matches packets with the corresponding IP protocol. 1535.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar table Ns Pq Ar number Ns Op , Ns Ar value | Ar ipno | Ar any 1536Matches packets received, transmitted or going through, 1537respectively, the interface specified by exact name 1538.Ns No ( Ar ifX Ns No ), 1539by device name 1540.Ns No ( Ar if Ns Ar * Ns No ), 1541by IP address, or through some interface. 1542.Pp 1543The 1544.Cm via 1545keyword causes the interface to always be checked. 1546If 1547.Cm recv 1548or 1549.Cm xmit 1550is used instead of 1551.Cm via , 1552then only the receive or transmit interface (respectively) 1553is checked. 1554By specifying both, it is possible to match packets based on 1555both receive and transmit interface, e.g.: 1556.Pp 1557.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1" 1558.Pp 1559The 1560.Cm recv 1561interface can be tested on either incoming or outgoing packets, 1562while the 1563.Cm xmit 1564interface can only be tested on outgoing packets. 1565So 1566.Cm out 1567is required (and 1568.Cm in 1569is invalid) whenever 1570.Cm xmit 1571is used. 1572.Pp 1573A packet might not have a receive or transmit interface: packets 1574originating from the local host have no receive interface, 1575while packets destined for the local host have no transmit 1576interface. 1577.It Cm setup 1578Matches TCP packets that have the SYN bit set but no ACK bit. 1579This is the short form of 1580.Dq Li tcpflags\ syn,!ack . 1581.It Cm sockarg 1582Matches packets that are associated to a local socket and 1583for which the SO_USER_COOKIE socket option has been set 1584to a non-zero value. As a side effect, the value of the 1585option is made available as 1586.Cm tablearg 1587value, which in turn can be used as 1588.Cm skipto 1589or 1590.Cm pipe 1591number. 1592.It Cm src-ip Ar ip-address 1593Matches IPv4 packets whose source IP is one of the address(es) 1594specified as an argument. 1595.It Cm src-ip6 Ar ip6-address 1596Matches IPv6 packets whose source IP is one of the address(es) 1597specified as an argument. 1598.It Cm src-port Ar ports 1599Matches IP packets whose source port is one of the port(s) 1600specified as argument. 1601.It Cm tagged Ar tag-list 1602Matches packets whose tags are included in 1603.Ar tag-list , 1604which is either a single value or a list of values or ranges 1605specified in the same way as 1606.Ar ports . 1607Tags can be applied to the packet using 1608.Cm tag 1609rule action parameter (see it's description for details on tags). 1610.It Cm tcpack Ar ack 1611TCP packets only. 1612Match if the TCP header acknowledgment number field is set to 1613.Ar ack . 1614.It Cm tcpdatalen Ar tcpdatalen-list 1615Matches TCP packets whose length of TCP data is 1616.Ar tcpdatalen-list , 1617which is either a single value or a list of values or ranges 1618specified in the same way as 1619.Ar ports . 1620.It Cm tcpflags Ar spec 1621TCP packets only. 1622Match if the TCP header contains the comma separated list of 1623flags specified in 1624.Ar spec . 1625The supported TCP flags are: 1626.Pp 1627.Cm fin , 1628.Cm syn , 1629.Cm rst , 1630.Cm psh , 1631.Cm ack 1632and 1633.Cm urg . 1634The absence of a particular flag may be denoted 1635with a 1636.Ql \&! . 1637A rule which contains a 1638.Cm tcpflags 1639specification can never match a fragmented packet which has 1640a non-zero offset. 1641See the 1642.Cm frag 1643option for details on matching fragmented packets. 1644.It Cm tcpseq Ar seq 1645TCP packets only. 1646Match if the TCP header sequence number field is set to 1647.Ar seq . 1648.It Cm tcpwin Ar tcpwin-list 1649Matches TCP packets whose header window field is set to 1650.Ar tcpwin-list , 1651which is either a single value or a list of values or ranges 1652specified in the same way as 1653.Ar ports . 1654.It Cm tcpoptions Ar spec 1655TCP packets only. 1656Match if the TCP header contains the comma separated list of 1657options specified in 1658.Ar spec . 1659The supported TCP options are: 1660.Pp 1661.Cm mss 1662(maximum segment size), 1663.Cm window 1664(tcp window advertisement), 1665.Cm sack 1666(selective ack), 1667.Cm ts 1668(rfc1323 timestamp) and 1669.Cm cc 1670(rfc1644 t/tcp connection count). 1671The absence of a particular option may be denoted 1672with a 1673.Ql \&! . 1674.It Cm uid Ar user 1675Match all TCP or UDP packets sent by or received for a 1676.Ar user . 1677A 1678.Ar user 1679may be matched by name or identification number. 1680.It Cm verrevpath 1681For incoming packets, 1682a routing table lookup is done on the packet's source address. 1683If the interface on which the packet entered the system matches the 1684outgoing interface for the route, 1685the packet matches. 1686If the interfaces do not match up, 1687the packet does not match. 1688All outgoing packets or packets with no incoming interface match. 1689.Pp 1690The name and functionality of the option is intentionally similar to 1691the Cisco IOS command: 1692.Pp 1693.Dl ip verify unicast reverse-path 1694.Pp 1695This option can be used to make anti-spoofing rules to reject all 1696packets with source addresses not from this interface. 1697See also the option 1698.Cm antispoof . 1699.It Cm versrcreach 1700For incoming packets, 1701a routing table lookup is done on the packet's source address. 1702If a route to the source address exists, but not the default route 1703or a blackhole/reject route, the packet matches. 1704Otherwise, the packet does not match. 1705All outgoing packets match. 1706.Pp 1707The name and functionality of the option is intentionally similar to 1708the Cisco IOS command: 1709.Pp 1710.Dl ip verify unicast source reachable-via any 1711.Pp 1712This option can be used to make anti-spoofing rules to reject all 1713packets whose source address is unreachable. 1714.It Cm antispoof 1715For incoming packets, the packet's source address is checked if it 1716belongs to a directly connected network. 1717If the network is directly connected, then the interface the packet 1718came on in is compared to the interface the network is connected to. 1719When incoming interface and directly connected interface are not the 1720same, the packet does not match. 1721Otherwise, the packet does match. 1722All outgoing packets match. 1723.Pp 1724This option can be used to make anti-spoofing rules to reject all 1725packets that pretend to be from a directly connected network but do 1726not come in through that interface. 1727This option is similar to but more restricted than 1728.Cm verrevpath 1729because it engages only on packets with source addresses of directly 1730connected networks instead of all source addresses. 1731.El 1732.Sh LOOKUP TABLES 1733Lookup tables are useful to handle large sparse sets of 1734addresses or other search keys (e.g. ports, jail IDs, interface names). 1735In the rest of this section we will use the term ``address''. 1736There may be up to 4096 different lookup tables, numbered 0 to 4095. 1737.Pp 1738Each entry is represented by an 1739.Ar addr Ns Op / Ns Ar masklen 1740and will match all addresses with base 1741.Ar addr 1742(specified as an IPv4/IPv6 address, a hostname or an unsigned integer) 1743and mask width of 1744.Ar masklen 1745bits. 1746If 1747.Ar masklen 1748is not specified, it defaults to 32 for IPv4 and 128 for IPv6. 1749When looking up an IP address in a table, the most specific 1750entry will match. 1751Associated with each entry is a 32-bit unsigned 1752.Ar value , 1753which can optionally be checked by a rule matching code. 1754When adding an entry, if 1755.Ar value 1756is not specified, it defaults to 0. 1757.Pp 1758An entry can be added to a table 1759.Pq Cm add , 1760or removed from a table 1761.Pq Cm delete . 1762A table can be examined 1763.Pq Cm list 1764or flushed 1765.Pq Cm flush . 1766.Pp 1767Internally, each table is stored in a Radix tree, the same way as 1768the routing table (see 1769.Xr route 4 ) . 1770.Pp 1771Lookup tables currently support only ports, jail IDs, IPv4/IPv6 addresses 1772and interface names. Wildcards is not supported for interface names. 1773.Pp 1774The 1775.Cm tablearg 1776feature provides the ability to use a value, looked up in the table, as 1777the argument for a rule action, action parameter or rule option. 1778This can significantly reduce number of rules in some configurations. 1779If two tables are used in a rule, the result of the second (destination) 1780is used. 1781The 1782.Cm tablearg 1783argument can be used with the following actions: 1784.Cm nat, pipe , queue, divert, tee, netgraph, ngtee, fwd, skipto, setfib, 1785action parameters: 1786.Cm tag, untag, 1787rule options: 1788.Cm limit, tagged. 1789.Pp 1790When used with 1791.Cm fwd 1792it is possible to supply table entries with values 1793that are in the form of IP addresses or hostnames. 1794See the 1795.Sx EXAMPLES 1796Section for example usage of tables and the tablearg keyword. 1797.Pp 1798When used with the 1799.Cm skipto 1800action, the user should be aware that the code will walk the ruleset 1801up to a rule equal to, or past, the given number, and should therefore try keep the 1802ruleset compact between the skipto and the target rules. 1803.Sh SETS OF RULES 1804Each rule belongs to one of 32 different 1805.Em sets 1806, numbered 0 to 31. 1807Set 31 is reserved for the default rule. 1808.Pp 1809By default, rules are put in set 0, unless you use the 1810.Cm set N 1811attribute when entering a new rule. 1812Sets can be individually and atomically enabled or disabled, 1813so this mechanism permits an easy way to store multiple configurations 1814of the firewall and quickly (and atomically) switch between them. 1815The command to enable/disable sets is 1816.Bd -ragged -offset indent 1817.Nm 1818.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ... 1819.Ed 1820.Pp 1821where multiple 1822.Cm enable 1823or 1824.Cm disable 1825sections can be specified. 1826Command execution is atomic on all the sets specified in the command. 1827By default, all sets are enabled. 1828.Pp 1829When you disable a set, its rules behave as if they do not exist 1830in the firewall configuration, with only one exception: 1831.Bd -ragged -offset indent 1832dynamic rules created from a rule before it had been disabled 1833will still be active until they expire. 1834In order to delete 1835dynamic rules you have to explicitly delete the parent rule 1836which generated them. 1837.Ed 1838.Pp 1839The set number of rules can be changed with the command 1840.Bd -ragged -offset indent 1841.Nm 1842.Cm set move 1843.Brq Cm rule Ar rule-number | old-set 1844.Cm to Ar new-set 1845.Ed 1846.Pp 1847Also, you can atomically swap two rulesets with the command 1848.Bd -ragged -offset indent 1849.Nm 1850.Cm set swap Ar first-set second-set 1851.Ed 1852.Pp 1853See the 1854.Sx EXAMPLES 1855Section on some possible uses of sets of rules. 1856.Sh STATEFUL FIREWALL 1857Stateful operation is a way for the firewall to dynamically 1858create rules for specific flows when packets that 1859match a given pattern are detected. 1860Support for stateful 1861operation comes through the 1862.Cm check-state , keep-state 1863and 1864.Cm limit 1865options of 1866.Nm rules . 1867.Pp 1868Dynamic rules are created when a packet matches a 1869.Cm keep-state 1870or 1871.Cm limit 1872rule, causing the creation of a 1873.Em dynamic 1874rule which will match all and only packets with 1875a given 1876.Em protocol 1877between a 1878.Em src-ip/src-port dst-ip/dst-port 1879pair of addresses 1880.Em ( src 1881and 1882.Em dst 1883are used here only to denote the initial match addresses, but they 1884are completely equivalent afterwards). 1885Dynamic rules will be checked at the first 1886.Cm check-state, keep-state 1887or 1888.Cm limit 1889occurrence, and the action performed upon a match will be the same 1890as in the parent rule. 1891.Pp 1892Note that no additional attributes other than protocol and IP addresses 1893and ports are checked on dynamic rules. 1894.Pp 1895The typical use of dynamic rules is to keep a closed firewall configuration, 1896but let the first TCP SYN packet from the inside network install a 1897dynamic rule for the flow so that packets belonging to that session 1898will be allowed through the firewall: 1899.Pp 1900.Dl "ipfw add check-state" 1901.Dl "ipfw add allow tcp from my-subnet to any setup keep-state" 1902.Dl "ipfw add deny tcp from any to any" 1903.Pp 1904A similar approach can be used for UDP, where an UDP packet coming 1905from the inside will install a dynamic rule to let the response through 1906the firewall: 1907.Pp 1908.Dl "ipfw add check-state" 1909.Dl "ipfw add allow udp from my-subnet to any keep-state" 1910.Dl "ipfw add deny udp from any to any" 1911.Pp 1912Dynamic rules expire after some time, which depends on the status 1913of the flow and the setting of some 1914.Cm sysctl 1915variables. 1916See Section 1917.Sx SYSCTL VARIABLES 1918for more details. 1919For TCP sessions, dynamic rules can be instructed to periodically 1920send keepalive packets to refresh the state of the rule when it is 1921about to expire. 1922.Pp 1923See Section 1924.Sx EXAMPLES 1925for more examples on how to use dynamic rules. 1926.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 1927.Nm 1928is also the user interface for the 1929.Nm dummynet 1930traffic shaper, packet scheduler and network emulator, a subsystem that 1931can artificially queue, delay or drop packets 1932emulating the behaviour of certain network links 1933or queueing systems. 1934.Pp 1935.Nm dummynet 1936operates by first using the firewall to select packets 1937using any match pattern that can be used in 1938.Nm 1939rules. 1940Matching packets are then passed to either of two 1941different objects, which implement the traffic regulation: 1942.Bl -hang -offset XXXX 1943.It Em pipe 1944A 1945.Em pipe 1946emulates a 1947.Em link 1948with given bandwidth and propagation delay, 1949driven by a FIFO scheduler and a single queue with programmable 1950queue size and packet loss rate. 1951Packets are appended to the queue as they come out from 1952.Nm ipfw , 1953and then transferred in FIFO order to the link at the desired rate. 1954.It Em queue 1955A 1956.Em queue 1957is an abstraction used to implement packet scheduling 1958using one of several packet scheduling algorithms. 1959Packets sent to a 1960.Em queue 1961are first grouped into flows according to a mask on the 5-tuple. 1962Flows are then passed to the scheduler associated to the 1963.Em queue , 1964and each flow uses scheduling parameters (weight and others) 1965as configured in the 1966.Em queue 1967itself. 1968A scheduler in turn is connected to an emulated link, 1969and arbitrates the link's bandwidth among backlogged flows according to 1970weights and to the features of the scheduling algorithm in use. 1971.El 1972.Pp 1973In practice, 1974.Em pipes 1975can be used to set hard limits to the bandwidth that a flow can use, whereas 1976.Em queues 1977can be used to determine how different flows share the available bandwidth. 1978.Pp 1979A graphical representation of the binding of queues, 1980flows, schedulers and links is below. 1981.Bd -literal -offset indent 1982 (flow_mask|sched_mask) sched_mask 1983 +---------+ weight Wx +-------------+ 1984 | |->-[flow]-->--| |-+ 1985 -->--| QUEUE x | ... | | | 1986 | |->-[flow]-->--| SCHEDuler N | | 1987 +---------+ | | | 1988 ... | +--[LINK N]-->-- 1989 +---------+ weight Wy | | +--[LINK N]-->-- 1990 | |->-[flow]-->--| | | 1991 -->--| QUEUE y | ... | | | 1992 | |->-[flow]-->--| | | 1993 +---------+ +-------------+ | 1994 +-------------+ 1995.Ed 1996It is important to understand the role of the SCHED_MASK 1997and FLOW_MASK, which are configured through the commands 1998.Dl "ipfw sched N config mask SCHED_MASK ..." 1999and 2000.Dl "ipfw queue X config mask FLOW_MASK ..." . 2001.Pp 2002The SCHED_MASK is used to assign flows to one or more 2003scheduler instances, one for each 2004value of the packet's 5-tuple after applying SCHED_MASK. 2005As an example, using ``src-ip 0xffffff00'' creates one instance 2006for each /24 destination subnet. 2007.Pp 2008The FLOW_MASK, together with the SCHED_MASK, is used to split 2009packets into flows. As an example, using 2010``src-ip 0x000000ff'' 2011together with the previous SCHED_MASK makes a flow for 2012each individual source address. In turn, flows for each /24 2013subnet will be sent to the same scheduler instance. 2014.Pp 2015The above diagram holds even for the 2016.Em pipe 2017case, with the only restriction that a 2018.Em pipe 2019only supports a SCHED_MASK, and forces the use of a FIFO 2020scheduler (these are for backward compatibility reasons; 2021in fact, internally, a 2022.Nm dummynet's 2023pipe is implemented exactly as above). 2024.Pp 2025There are two modes of 2026.Nm dummynet 2027operation: 2028.Dq normal 2029and 2030.Dq fast . 2031The 2032.Dq normal 2033mode tries to emulate a real link: the 2034.Nm dummynet 2035scheduler ensures that the packet will not leave the pipe faster than it 2036would on the real link with a given bandwidth. 2037The 2038.Dq fast 2039mode allows certain packets to bypass the 2040.Nm dummynet 2041scheduler (if packet flow does not exceed pipe's bandwidth). 2042This is the reason why the 2043.Dq fast 2044mode requires less CPU cycles per packet (on average) and packet latency 2045can be significantly lower in comparison to a real link with the same 2046bandwidth. 2047The default mode is 2048.Dq normal . 2049The 2050.Dq fast 2051mode can be enabled by setting the 2052.Va net.inet.ip.dummynet.io_fast 2053.Xr sysctl 8 2054variable to a non-zero value. 2055.Pp 2056.Ss PIPE, QUEUE AND SCHEDULER CONFIGURATION 2057The 2058.Em pipe , 2059.Em queue 2060and 2061.Em scheduler 2062configuration commands are the following: 2063.Bd -ragged -offset indent 2064.Cm pipe Ar number Cm config Ar pipe-configuration 2065.Pp 2066.Cm queue Ar number Cm config Ar queue-configuration 2067.Pp 2068.Cm sched Ar number Cm config Ar sched-configuration 2069.Ed 2070.Pp 2071The following parameters can be configured for a pipe: 2072.Pp 2073.Bl -tag -width indent -compact 2074.It Cm bw Ar bandwidth | device 2075Bandwidth, measured in 2076.Sm off 2077.Op Cm K | M 2078.Brq Cm bit/s | Byte/s . 2079.Sm on 2080.Pp 2081A value of 0 (default) means unlimited bandwidth. 2082The unit must immediately follow the number, as in 2083.Pp 2084.Dl "ipfw pipe 1 config bw 300Kbit/s" 2085.Pp 2086If a device name is specified instead of a numeric value, as in 2087.Pp 2088.Dl "ipfw pipe 1 config bw tun0" 2089.Pp 2090then the transmit clock is supplied by the specified device. 2091At the moment only the 2092.Xr tun 4 2093device supports this 2094functionality, for use in conjunction with 2095.Xr ppp 8 . 2096.Pp 2097.It Cm delay Ar ms-delay 2098Propagation delay, measured in milliseconds. 2099The value is rounded to the next multiple of the clock tick 2100(typically 10ms, but it is a good practice to run kernels 2101with 2102.Dq "options HZ=1000" 2103to reduce 2104the granularity to 1ms or less). 2105The default value is 0, meaning no delay. 2106.Pp 2107.It Cm burst Ar size 2108If the data to be sent exceeds the pipe's bandwidth limit 2109(and the pipe was previously idle), up to 2110.Ar size 2111bytes of data are allowed to bypass the 2112.Nm dummynet 2113scheduler, and will be sent as fast as the physical link allows. 2114Any additional data will be transmitted at the rate specified 2115by the 2116.Nm pipe 2117bandwidth. 2118The burst size depends on how long the pipe has been idle; 2119the effective burst size is calculated as follows: 2120MAX( 2121.Ar size 2122, 2123.Nm bw 2124* pipe_idle_time). 2125.Pp 2126.It Cm profile Ar filename 2127A file specifying the additional overhead incurred in the transmission 2128of a packet on the link. 2129.Pp 2130Some link types introduce extra delays in the transmission 2131of a packet, e.g. because of MAC level framing, contention on 2132the use of the channel, MAC level retransmissions and so on. 2133From our point of view, the channel is effectively unavailable 2134for this extra time, which is constant or variable depending 2135on the link type. Additionally, packets may be dropped after this 2136time (e.g. on a wireless link after too many retransmissions). 2137We can model the additional delay with an empirical curve 2138that represents its distribution. 2139.Bd -literal -offset indent 2140 cumulative probability 2141 1.0 ^ 2142 | 2143 L +-- loss-level x 2144 | ****** 2145 | * 2146 | ***** 2147 | * 2148 | ** 2149 | * 2150 +-------*-------------------> 2151 delay 2152.Ed 2153The empirical curve may have both vertical and horizontal lines. 2154Vertical lines represent constant delay for a range of 2155probabilities. 2156Horizontal lines correspond to a discontinuity in the delay 2157distribution: the pipe will use the largest delay for a 2158given probability. 2159.Pp 2160The file format is the following, with whitespace acting as 2161a separator and '#' indicating the beginning a comment: 2162.Bl -tag -width indent 2163.It Cm name Ar identifier 2164optional name (listed by "ipfw pipe show") 2165to identify the delay distribution; 2166.It Cm bw Ar value 2167the bandwidth used for the pipe. 2168If not specified here, it must be present 2169explicitly as a configuration parameter for the pipe; 2170.It Cm loss-level Ar L 2171the probability above which packets are lost. 2172(0.0 <= L <= 1.0, default 1.0 i.e. no loss); 2173.It Cm samples Ar N 2174the number of samples used in the internal 2175representation of the curve (2..1024; default 100); 2176.It Cm "delay prob" | "prob delay" 2177One of these two lines is mandatory and defines 2178the format of the following lines with data points. 2179.It Ar XXX Ar YYY 21802 or more lines representing points in the curve, 2181with either delay or probability first, according 2182to the chosen format. 2183The unit for delay is milliseconds. 2184Data points do not need to be sorted. 2185Also, the number of actual lines can be different 2186from the value of the "samples" parameter: 2187.Nm 2188utility will sort and interpolate 2189the curve as needed. 2190.El 2191.Pp 2192Example of a profile file: 2193.Bd -literal -offset indent 2194name bla_bla_bla 2195samples 100 2196loss-level 0.86 2197prob delay 21980 200 # minimum overhead is 200ms 21990.5 200 22000.5 300 22010.8 1000 22020.9 1300 22031 1300 2204#configuration file end 2205.Ed 2206.El 2207.Pp 2208The following parameters can be configured for a queue: 2209.Pp 2210.Bl -tag -width indent -compact 2211.It Cm pipe Ar pipe_nr 2212Connects a queue to the specified pipe. 2213Multiple queues (with the same or different weights) can be connected to 2214the same pipe, which specifies the aggregate rate for the set of queues. 2215.Pp 2216.It Cm weight Ar weight 2217Specifies the weight to be used for flows matching this queue. 2218The weight must be in the range 1..100, and defaults to 1. 2219.El 2220.Pp 2221The following parameters can be configured for a scheduler: 2222.Pp 2223.Bl -tag -width indent -compact 2224.It Cm type Ar {fifo | wf2qp | rr | qfq} 2225specifies the scheduling algorithm to use. 2226.Bl -tag -width indent -compact 2227.It cm fifo 2228is just a FIFO scheduler (which means that all packets 2229are stored in the same queue as they arrive to the scheduler). 2230FIFO has O(1) per-packet time complexity, with very low 2231constants (estimate 60-80ns on a 2GHz desktop machine) 2232but gives no service guarantees. 2233.It Cm wf2qp 2234implements the WF2Q+ algorithm, which is a Weighted Fair Queueing 2235algorithm which permits flows to share bandwidth according to 2236their weights. Note that weights are not priorities; even a flow 2237with a minuscule weight will never starve. 2238WF2Q+ has O(log N) per-packet processing cost, where N is the number 2239of flows, and is the default algorithm used by previous versions 2240dummynet's queues. 2241.It Cm rr 2242implements the Deficit Round Robin algorithm, which has O(1) processing 2243costs (roughly, 100-150ns per packet) 2244and permits bandwidth allocation according to weights, but 2245with poor service guarantees. 2246.It Cm qfq 2247implements the QFQ algorithm, which is a very fast variant of 2248WF2Q+, with similar service guarantees and O(1) processing 2249costs (roughly, 200-250ns per packet). 2250.El 2251.El 2252.Pp 2253In addition to the type, all parameters allowed for a pipe can also 2254be specified for a scheduler. 2255.Pp 2256Finally, the following parameters can be configured for both 2257pipes and queues: 2258.Pp 2259.Bl -tag -width XXXX -compact 2260.It Cm buckets Ar hash-table-size 2261Specifies the size of the hash table used for storing the 2262various queues. 2263Default value is 64 controlled by the 2264.Xr sysctl 8 2265variable 2266.Va net.inet.ip.dummynet.hash_size , 2267allowed range is 16 to 65536. 2268.Pp 2269.It Cm mask Ar mask-specifier 2270Packets sent to a given pipe or queue by an 2271.Nm 2272rule can be further classified into multiple flows, each of which is then 2273sent to a different 2274.Em dynamic 2275pipe or queue. 2276A flow identifier is constructed by masking the IP addresses, 2277ports and protocol types as specified with the 2278.Cm mask 2279options in the configuration of the pipe or queue. 2280For each different flow identifier, a new pipe or queue is created 2281with the same parameters as the original object, and matching packets 2282are sent to it. 2283.Pp 2284Thus, when 2285.Em dynamic pipes 2286are used, each flow will get the same bandwidth as defined by the pipe, 2287whereas when 2288.Em dynamic queues 2289are used, each flow will share the parent's pipe bandwidth evenly 2290with other flows generated by the same queue (note that other queues 2291with different weights might be connected to the same pipe). 2292.br 2293Available mask specifiers are a combination of one or more of the following: 2294.Pp 2295.Cm dst-ip Ar mask , 2296.Cm dst-ip6 Ar mask , 2297.Cm src-ip Ar mask , 2298.Cm src-ip6 Ar mask , 2299.Cm dst-port Ar mask , 2300.Cm src-port Ar mask , 2301.Cm flow-id Ar mask , 2302.Cm proto Ar mask 2303or 2304.Cm all , 2305.Pp 2306where the latter means all bits in all fields are significant. 2307.Pp 2308.It Cm noerror 2309When a packet is dropped by a 2310.Nm dummynet 2311queue or pipe, the error 2312is normally reported to the caller routine in the kernel, in the 2313same way as it happens when a device queue fills up. 2314Setting this 2315option reports the packet as successfully delivered, which can be 2316needed for some experimental setups where you want to simulate 2317loss or congestion at a remote router. 2318.Pp 2319.It Cm plr Ar packet-loss-rate 2320Packet loss rate. 2321Argument 2322.Ar packet-loss-rate 2323is a floating-point number between 0 and 1, with 0 meaning no 2324loss, 1 meaning 100% loss. 2325The loss rate is internally represented on 31 bits. 2326.Pp 2327.It Cm queue Brq Ar slots | size Ns Cm Kbytes 2328Queue size, in 2329.Ar slots 2330or 2331.Cm KBytes . 2332Default value is 50 slots, which 2333is the typical queue size for Ethernet devices. 2334Note that for slow speed links you should keep the queue 2335size short or your traffic might be affected by a significant 2336queueing delay. 2337E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit 2338or 20s of queue on a 30Kbit/s pipe. 2339Even worse effects can result if you get packets from an 2340interface with a much larger MTU, e.g.\& the loopback interface 2341with its 16KB packets. 2342The 2343.Xr sysctl 8 2344variables 2345.Em net.inet.ip.dummynet.pipe_byte_limit 2346and 2347.Em net.inet.ip.dummynet.pipe_slot_limit 2348control the maximum lengths that can be specified. 2349.Pp 2350.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p 2351Make use of the RED (Random Early Detection) queue management algorithm. 2352.Ar w_q 2353and 2354.Ar max_p 2355are floating 2356point numbers between 0 and 1 (0 not included), while 2357.Ar min_th 2358and 2359.Ar max_th 2360are integer numbers specifying thresholds for queue management 2361(thresholds are computed in bytes if the queue has been defined 2362in bytes, in slots otherwise). 2363The 2364.Nm dummynet 2365also supports the gentle RED variant (gred). 2366Three 2367.Xr sysctl 8 2368variables can be used to control the RED behaviour: 2369.Bl -tag -width indent 2370.It Va net.inet.ip.dummynet.red_lookup_depth 2371specifies the accuracy in computing the average queue 2372when the link is idle (defaults to 256, must be greater than zero) 2373.It Va net.inet.ip.dummynet.red_avg_pkt_size 2374specifies the expected average packet size (defaults to 512, must be 2375greater than zero) 2376.It Va net.inet.ip.dummynet.red_max_pkt_size 2377specifies the expected maximum packet size, only used when queue 2378thresholds are in bytes (defaults to 1500, must be greater than zero). 2379.El 2380.El 2381.Pp 2382When used with IPv6 data, 2383.Nm dummynet 2384currently has several limitations. 2385Information necessary to route link-local packets to an 2386interface is not available after processing by 2387.Nm dummynet 2388so those packets are dropped in the output path. 2389Care should be taken to ensure that link-local packets are not passed to 2390.Nm dummynet . 2391.Sh CHECKLIST 2392Here are some important points to consider when designing your 2393rules: 2394.Bl -bullet 2395.It 2396Remember that you filter both packets going 2397.Cm in 2398and 2399.Cm out . 2400Most connections need packets going in both directions. 2401.It 2402Remember to test very carefully. 2403It is a good idea to be near the console when doing this. 2404If you cannot be near the console, 2405use an auto-recovery script such as the one in 2406.Pa /usr/share/examples/ipfw/change_rules.sh . 2407.It 2408Do not forget the loopback interface. 2409.El 2410.Sh FINE POINTS 2411.Bl -bullet 2412.It 2413There are circumstances where fragmented datagrams are unconditionally 2414dropped. 2415TCP packets are dropped if they do not contain at least 20 bytes of 2416TCP header, UDP packets are dropped if they do not contain a full 8 2417byte UDP header, and ICMP packets are dropped if they do not contain 24184 bytes of ICMP header, enough to specify the ICMP type, code, and 2419checksum. 2420These packets are simply logged as 2421.Dq pullup failed 2422since there may not be enough good data in the packet to produce a 2423meaningful log entry. 2424.It 2425Another type of packet is unconditionally dropped, a TCP packet with a 2426fragment offset of one. 2427This is a valid packet, but it only has one use, to try 2428to circumvent firewalls. 2429When logging is enabled, these packets are 2430reported as being dropped by rule -1. 2431.It 2432If you are logged in over a network, loading the 2433.Xr kld 4 2434version of 2435.Nm 2436is probably not as straightforward as you would think. 2437The following command line is recommended: 2438.Bd -literal -offset indent 2439kldload ipfw && \e 2440ipfw add 32000 allow ip from any to any 2441.Ed 2442.Pp 2443Along the same lines, doing an 2444.Bd -literal -offset indent 2445ipfw flush 2446.Ed 2447.Pp 2448in similar surroundings is also a bad idea. 2449.It 2450The 2451.Nm 2452filter list may not be modified if the system security level 2453is set to 3 or higher 2454(see 2455.Xr init 8 2456for information on system security levels). 2457.El 2458.Sh PACKET DIVERSION 2459A 2460.Xr divert 4 2461socket bound to the specified port will receive all packets 2462diverted to that port. 2463If no socket is bound to the destination port, or if the divert module is 2464not loaded, or if the kernel was not compiled with divert socket support, 2465the packets are dropped. 2466.Sh NETWORK ADDRESS TRANSLATION (NAT) 2467.Nm 2468support in-kernel NAT using the kernel version of 2469.Xr libalias 3 . 2470.Pp 2471The nat configuration command is the following: 2472.Bd -ragged -offset indent 2473.Bk -words 2474.Cm nat 2475.Ar nat_number 2476.Cm config 2477.Ar nat-configuration 2478.Ek 2479.Ed 2480.Pp 2481The following parameters can be configured: 2482.Bl -tag -width indent 2483.It Cm ip Ar ip_address 2484Define an ip address to use for aliasing. 2485.It Cm if Ar nic 2486Use ip address of NIC for aliasing, dynamically changing 2487it if NIC's ip address changes. 2488.It Cm log 2489Enable logging on this nat instance. 2490.It Cm deny_in 2491Deny any incoming connection from outside world. 2492.It Cm same_ports 2493Try to leave the alias port numbers unchanged from 2494the actual local port numbers. 2495.It Cm unreg_only 2496Traffic on the local network not originating from an 2497unregistered address spaces will be ignored. 2498.It Cm reset 2499Reset table of the packet aliasing engine on address change. 2500.It Cm reverse 2501Reverse the way libalias handles aliasing. 2502.It Cm proxy_only 2503Obey transparent proxy rules only, packet aliasing is not performed. 2504.It Cm skip_global 2505Skip instance in case of global state lookup (see below). 2506.El 2507.Pp 2508Some specials value can be supplied instead of 2509.Va nat_number: 2510.Bl -tag -width indent 2511.It Cm global 2512Looks up translation state in all configured nat instances. 2513If an entry is found, packet is aliased according to that entry. 2514If no entry was found in any of the instances, packet is passed unchanged, 2515and no new entry will be created. 2516See section 2517.Sx MULTIPLE INSTANCES 2518in 2519.Xr natd 8 2520for more information. 2521.It Cm tablearg 2522Uses argument supplied in lookup table. See 2523.Sx LOOKUP TABLES 2524section below for more information on lookup tables. 2525.El 2526.Pp 2527To let the packet continue after being (de)aliased, set the sysctl variable 2528.Va net.inet.ip.fw.one_pass 2529to 0. 2530For more information about aliasing modes, refer to 2531.Xr libalias 3 . 2532See Section 2533.Sx EXAMPLES 2534for some examples about nat usage. 2535.Ss REDIRECT AND LSNAT SUPPORT IN IPFW 2536Redirect and LSNAT support follow closely the syntax used in 2537.Xr natd 8 . 2538See Section 2539.Sx EXAMPLES 2540for some examples on how to do redirect and lsnat. 2541.Ss SCTP NAT SUPPORT 2542SCTP nat can be configured in a similar manner to TCP through the 2543.Nm 2544command line tool. 2545The main difference is that 2546.Nm sctp nat 2547does not do port translation. 2548Since the local and global side ports will be the same, 2549there is no need to specify both. 2550Ports are redirected as follows: 2551.Bd -ragged -offset indent 2552.Bk -words 2553.Cm nat 2554.Ar nat_number 2555.Cm config if 2556.Ar nic 2557.Cm redirect_port sctp 2558.Ar ip_address [,addr_list] {[port | port-port] [,ports]} 2559.Ek 2560.Ed 2561.Pp 2562Most 2563.Nm sctp nat 2564configuration can be done in real-time through the 2565.Xr sysctl 8 2566interface. 2567All may be changed dynamically, though the hash_table size will only 2568change for new 2569.Nm nat 2570instances. 2571See 2572.Sx SYSCTL VARIABLES 2573for more info. 2574.Sh LOADER TUNABLES 2575Tunables can be set in 2576.Xr loader 8 2577prompt, 2578.Xr loader.conf 5 2579or 2580.Xr kenv 1 2581before ipfw module gets loaded. 2582.Bl -tag -width indent 2583.It Va net.inet.ip.fw.default_to_accept: No 0 2584Defines ipfw last rule behavior. This value overrides 2585.Cd "options IPFW_DEFAULT_TO_(ACCEPT|DENY)" 2586from kernel configuration file. 2587.It Va net.inet.ip.fw.tables_max: No 128 2588Defines number of tables available in ipfw. Number cannot exceed 65534. 2589.El 2590.Sh SYSCTL VARIABLES 2591A set of 2592.Xr sysctl 8 2593variables controls the behaviour of the firewall and 2594associated modules 2595.Pq Nm dummynet , bridge , sctp nat . 2596These are shown below together with their default value 2597(but always check with the 2598.Xr sysctl 8 2599command what value is actually in use) and meaning: 2600.Bl -tag -width indent 2601.It Va net.inet.ip.alias.sctp.accept_global_ootb_addip: No 0 2602Defines how the 2603.Nm nat 2604responds to receipt of global OOTB ASCONF-AddIP: 2605.Bl -tag -width indent 2606.It Cm 0 2607No response (unless a partially matching association exists - 2608ports and vtags match but global address does not) 2609.It Cm 1 2610.Nm nat 2611will accept and process all OOTB global AddIP messages. 2612.El 2613.Pp 2614Option 1 should never be selected as this forms a security risk. 2615An attacker can 2616establish multiple fake associations by sending AddIP messages. 2617.It Va net.inet.ip.alias.sctp.chunk_proc_limit: No 5 2618Defines the maximum number of chunks in an SCTP packet that will be parsed for a 2619packet that matches an existing association. 2620This value is enforced to be greater or equal than 2621.Cm net.inet.ip.alias.sctp.initialising_chunk_proc_limit . 2622A high value is 2623a DoS risk yet setting too low a value may result in important control chunks in 2624the packet not being located and parsed. 2625.It Va net.inet.ip.alias.sctp.error_on_ootb: No 1 2626Defines when the 2627.Nm nat 2628responds to any Out-of-the-Blue (OOTB) packets with ErrorM packets. 2629An OOTB packet is a packet that arrives with no existing association 2630registered in the 2631.Nm nat 2632and is not an INIT or ASCONF-AddIP packet: 2633.Bl -tag -width indent 2634.It Cm 0 2635ErrorM is never sent in response to OOTB packets. 2636.It Cm 1 2637ErrorM is only sent to OOTB packets received on the local side. 2638.It Cm 2 2639ErrorM is sent to the local side and on the global side ONLY if there is a 2640partial match (ports and vtags match but the source global IP does not). 2641This value is only useful if the 2642.Nm nat 2643is tracking global IP addresses. 2644.It Cm 3 2645ErrorM is sent in response to all OOTB packets on both the local and global side 2646(DoS risk). 2647.El 2648.Pp 2649At the moment the default is 0, since the ErrorM packet is not yet 2650supported by most SCTP stacks. 2651When it is supported, and if not tracking 2652global addresses, we recommend setting this value to 1 to allow 2653multi-homed local hosts to function with the 2654.Nm nat . 2655To track global addresses, we recommend setting this value to 2 to 2656allow global hosts to be informed when they need to (re)send an 2657ASCONF-AddIP. 2658Value 3 should never be chosen (except for debugging) as the 2659.Nm nat 2660will respond to all OOTB global packets (a DoS risk). 2661.It Va net.inet.ip.alias.sctp.hashtable_size: No 2003 2662Size of hash tables used for 2663.Nm nat 2664lookups (100 < prime_number > 1000001). 2665This value sets the 2666.Nm hash table 2667size for any future created 2668.Nm nat 2669instance and therefore must be set prior to creating a 2670.Nm nat 2671instance. 2672The table sizes may be changed to suit specific needs. 2673If there will be few 2674concurrent associations, and memory is scarce, you may make these smaller. 2675If there will be many thousands (or millions) of concurrent associations, you 2676should make these larger. 2677A prime number is best for the table size. 2678The sysctl 2679update function will adjust your input value to the next highest prime number. 2680.It Va net.inet.ip.alias.sctp.holddown_time: No 0 2681Hold association in table for this many seconds after receiving a 2682SHUTDOWN-COMPLETE. 2683This allows endpoints to correct shutdown gracefully if a 2684shutdown_complete is lost and retransmissions are required. 2685.It Va net.inet.ip.alias.sctp.init_timer: No 15 2686Timeout value while waiting for (INIT-ACK|AddIP-ACK). 2687This value cannot be 0. 2688.It Va net.inet.ip.alias.sctp.initialising_chunk_proc_limit: No 2 2689Defines the maximum number of chunks in an SCTP packet that will be parsed when 2690no existing association exists that matches that packet. 2691Ideally this packet 2692will only be an INIT or ASCONF-AddIP packet. 2693A higher value may become a DoS 2694risk as malformed packets can consume processing resources. 2695.It Va net.inet.ip.alias.sctp.param_proc_limit: No 25 2696Defines the maximum number of parameters within a chunk that will be parsed in a 2697packet. 2698As for other similar sysctl variables, larger values pose a DoS risk. 2699.It Va net.inet.ip.alias.sctp.log_level: No 0 2700Level of detail in the system log messages (0 \- minimal, 1 \- event, 27012 \- info, 3 \- detail, 4 \- debug, 5 \- max debug). May be a good 2702option in high loss environments. 2703.It Va net.inet.ip.alias.sctp.shutdown_time: No 15 2704Timeout value while waiting for SHUTDOWN-COMPLETE. 2705This value cannot be 0. 2706.It Va net.inet.ip.alias.sctp.track_global_addresses: No 0 2707Enables/disables global IP address tracking within the 2708.Nm nat 2709and places an 2710upper limit on the number of addresses tracked for each association: 2711.Bl -tag -width indent 2712.It Cm 0 2713Global tracking is disabled 2714.It Cm >1 2715Enables tracking, the maximum number of addresses tracked for each 2716association is limited to this value 2717.El 2718.Pp 2719This variable is fully dynamic, the new value will be adopted for all newly 2720arriving associations, existing associations are treated as they were previously. 2721Global tracking will decrease the number of collisions within the 2722.Nm nat 2723at a cost 2724of increased processing load, memory usage, complexity, and possible 2725.Nm nat 2726state 2727problems in complex networks with multiple 2728.Nm nats . 2729We recommend not tracking 2730global IP addresses, this will still result in a fully functional 2731.Nm nat . 2732.It Va net.inet.ip.alias.sctp.up_timer: No 300 2733Timeout value to keep an association up with no traffic. 2734This value cannot be 0. 2735.It Va net.inet.ip.dummynet.expire : No 1 2736Lazily delete dynamic pipes/queue once they have no pending traffic. 2737You can disable this by setting the variable to 0, in which case 2738the pipes/queues will only be deleted when the threshold is reached. 2739.It Va net.inet.ip.dummynet.hash_size : No 64 2740Default size of the hash table used for dynamic pipes/queues. 2741This value is used when no 2742.Cm buckets 2743option is specified when configuring a pipe/queue. 2744.It Va net.inet.ip.dummynet.io_fast : No 0 2745If set to a non-zero value, 2746the 2747.Dq fast 2748mode of 2749.Nm dummynet 2750operation (see above) is enabled. 2751.It Va net.inet.ip.dummynet.io_pkt 2752Number of packets passed to 2753.Nm dummynet . 2754.It Va net.inet.ip.dummynet.io_pkt_drop 2755Number of packets dropped by 2756.Nm dummynet . 2757.It Va net.inet.ip.dummynet.io_pkt_fast 2758Number of packets bypassed by the 2759.Nm dummynet 2760scheduler. 2761.It Va net.inet.ip.dummynet.max_chain_len : No 16 2762Target value for the maximum number of pipes/queues in a hash bucket. 2763The product 2764.Cm max_chain_len*hash_size 2765is used to determine the threshold over which empty pipes/queues 2766will be expired even when 2767.Cm net.inet.ip.dummynet.expire=0 . 2768.It Va net.inet.ip.dummynet.red_lookup_depth : No 256 2769.It Va net.inet.ip.dummynet.red_avg_pkt_size : No 512 2770.It Va net.inet.ip.dummynet.red_max_pkt_size : No 1500 2771Parameters used in the computations of the drop probability 2772for the RED algorithm. 2773.It Va net.inet.ip.dummynet.pipe_byte_limit : No 1048576 2774.It Va net.inet.ip.dummynet.pipe_slot_limit : No 100 2775The maximum queue size that can be specified in bytes or packets. 2776These limits prevent accidental exhaustion of resources such as mbufs. 2777If you raise these limits, 2778you should make sure the system is configured so that sufficient resources 2779are available. 2780.It Va net.inet.ip.fw.autoinc_step : No 100 2781Delta between rule numbers when auto-generating them. 2782The value must be in the range 1..1000. 2783.It Va net.inet.ip.fw.curr_dyn_buckets : Va net.inet.ip.fw.dyn_buckets 2784The current number of buckets in the hash table for dynamic rules 2785(readonly). 2786.It Va net.inet.ip.fw.debug : No 1 2787Controls debugging messages produced by 2788.Nm . 2789.It Va net.inet.ip.fw.default_rule : No 65535 2790The default rule number (read-only). 2791By the design of 2792.Nm , the default rule is the last one, so its number 2793can also serve as the highest number allowed for a rule. 2794.It Va net.inet.ip.fw.dyn_buckets : No 256 2795The number of buckets in the hash table for dynamic rules. 2796Must be a power of 2, up to 65536. 2797It only takes effect when all dynamic rules have expired, so you 2798are advised to use a 2799.Cm flush 2800command to make sure that the hash table is resized. 2801.It Va net.inet.ip.fw.dyn_count : No 3 2802Current number of dynamic rules 2803(read-only). 2804.It Va net.inet.ip.fw.dyn_keepalive : No 1 2805Enables generation of keepalive packets for 2806.Cm keep-state 2807rules on TCP sessions. 2808A keepalive is generated to both 2809sides of the connection every 5 seconds for the last 20 2810seconds of the lifetime of the rule. 2811.It Va net.inet.ip.fw.dyn_max : No 8192 2812Maximum number of dynamic rules. 2813When you hit this limit, no more dynamic rules can be 2814installed until old ones expire. 2815.It Va net.inet.ip.fw.dyn_ack_lifetime : No 300 2816.It Va net.inet.ip.fw.dyn_syn_lifetime : No 20 2817.It Va net.inet.ip.fw.dyn_fin_lifetime : No 1 2818.It Va net.inet.ip.fw.dyn_rst_lifetime : No 1 2819.It Va net.inet.ip.fw.dyn_udp_lifetime : No 5 2820.It Va net.inet.ip.fw.dyn_short_lifetime : No 30 2821These variables control the lifetime, in seconds, of dynamic 2822rules. 2823Upon the initial SYN exchange the lifetime is kept short, 2824then increased after both SYN have been seen, then decreased 2825again during the final FIN exchange or when a RST is received. 2826Both 2827.Em dyn_fin_lifetime 2828and 2829.Em dyn_rst_lifetime 2830must be strictly lower than 5 seconds, the period of 2831repetition of keepalives. 2832The firewall enforces that. 2833.It Va net.inet.ip.fw.enable : No 1 2834Enables the firewall. 2835Setting this variable to 0 lets you run your machine without 2836firewall even if compiled in. 2837.It Va net.inet6.ip6.fw.enable : No 1 2838provides the same functionality as above for the IPv6 case. 2839.It Va net.inet.ip.fw.one_pass : No 1 2840When set, the packet exiting from the 2841.Nm dummynet 2842pipe or from 2843.Xr ng_ipfw 4 2844node is not passed though the firewall again. 2845Otherwise, after an action, the packet is 2846reinjected into the firewall at the next rule. 2847.It Va net.inet.ip.fw.tables_max : No 128 2848Maximum number of tables. 2849.It Va net.inet.ip.fw.verbose : No 1 2850Enables verbose messages. 2851.It Va net.inet.ip.fw.verbose_limit : No 0 2852Limits the number of messages produced by a verbose firewall. 2853.It Va net.inet6.ip6.fw.deny_unknown_exthdrs : No 1 2854If enabled packets with unknown IPv6 Extension Headers will be denied. 2855.It Va net.link.ether.ipfw : No 0 2856Controls whether layer-2 packets are passed to 2857.Nm . 2858Default is no. 2859.It Va net.link.bridge.ipfw : No 0 2860Controls whether bridged packets are passed to 2861.Nm . 2862Default is no. 2863.El 2864.Sh EXAMPLES 2865There are far too many possible uses of 2866.Nm 2867so this Section will only give a small set of examples. 2868.Pp 2869.Ss BASIC PACKET FILTERING 2870This command adds an entry which denies all tcp packets from 2871.Em cracker.evil.org 2872to the telnet port of 2873.Em wolf.tambov.su 2874from being forwarded by the host: 2875.Pp 2876.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet" 2877.Pp 2878This one disallows any connection from the entire cracker's 2879network to my host: 2880.Pp 2881.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org" 2882.Pp 2883A first and efficient way to limit access (not using dynamic rules) 2884is the use of the following rules: 2885.Pp 2886.Dl "ipfw add allow tcp from any to any established" 2887.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup" 2888.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup" 2889.Dl "..." 2890.Dl "ipfw add deny tcp from any to any" 2891.Pp 2892The first rule will be a quick match for normal TCP packets, 2893but it will not match the initial SYN packet, which will be 2894matched by the 2895.Cm setup 2896rules only for selected source/destination pairs. 2897All other SYN packets will be rejected by the final 2898.Cm deny 2899rule. 2900.Pp 2901If you administer one or more subnets, you can take advantage 2902of the address sets and or-blocks and write extremely 2903compact rulesets which selectively enable services to blocks 2904of clients, as below: 2905.Pp 2906.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q" 2907.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q" 2908.Dl "" 2909.Dl "ipfw add allow ip from ${goodguys} to any" 2910.Dl "ipfw add deny ip from ${badguys} to any" 2911.Dl "... normal policies ..." 2912.Pp 2913The 2914.Cm verrevpath 2915option could be used to do automated anti-spoofing by adding the 2916following to the top of a ruleset: 2917.Pp 2918.Dl "ipfw add deny ip from any to any not verrevpath in" 2919.Pp 2920This rule drops all incoming packets that appear to be coming to the 2921system on the wrong interface. 2922For example, a packet with a source 2923address belonging to a host on a protected internal network would be 2924dropped if it tried to enter the system from an external interface. 2925.Pp 2926The 2927.Cm antispoof 2928option could be used to do similar but more restricted anti-spoofing 2929by adding the following to the top of a ruleset: 2930.Pp 2931.Dl "ipfw add deny ip from any to any not antispoof in" 2932.Pp 2933This rule drops all incoming packets that appear to be coming from another 2934directly connected system but on the wrong interface. 2935For example, a packet with a source address of 2936.Li 192.168.0.0/24 , 2937configured on 2938.Li fxp0 , 2939but coming in on 2940.Li fxp1 2941would be dropped. 2942.Ss DYNAMIC RULES 2943In order to protect a site from flood attacks involving fake 2944TCP packets, it is safer to use dynamic rules: 2945.Pp 2946.Dl "ipfw add check-state" 2947.Dl "ipfw add deny tcp from any to any established" 2948.Dl "ipfw add allow tcp from my-net to any setup keep-state" 2949.Pp 2950This will let the firewall install dynamic rules only for 2951those connection which start with a regular SYN packet coming 2952from the inside of our network. 2953Dynamic rules are checked when encountering the first 2954.Cm check-state 2955or 2956.Cm keep-state 2957rule. 2958A 2959.Cm check-state 2960rule should usually be placed near the beginning of the 2961ruleset to minimize the amount of work scanning the ruleset. 2962Your mileage may vary. 2963.Pp 2964To limit the number of connections a user can open 2965you can use the following type of rules: 2966.Pp 2967.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10" 2968.Dl "ipfw add allow tcp from any to me setup limit src-addr 4" 2969.Pp 2970The former (assuming it runs on a gateway) will allow each host 2971on a /24 network to open at most 10 TCP connections. 2972The latter can be placed on a server to make sure that a single 2973client does not use more than 4 simultaneous connections. 2974.Pp 2975.Em BEWARE : 2976stateful rules can be subject to denial-of-service attacks 2977by a SYN-flood which opens a huge number of dynamic rules. 2978The effects of such attacks can be partially limited by 2979acting on a set of 2980.Xr sysctl 8 2981variables which control the operation of the firewall. 2982.Pp 2983Here is a good usage of the 2984.Cm list 2985command to see accounting records and timestamp information: 2986.Pp 2987.Dl ipfw -at list 2988.Pp 2989or in short form without timestamps: 2990.Pp 2991.Dl ipfw -a list 2992.Pp 2993which is equivalent to: 2994.Pp 2995.Dl ipfw show 2996.Pp 2997Next rule diverts all incoming packets from 192.168.2.0/24 2998to divert port 5000: 2999.Pp 3000.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in 3001.Ss TRAFFIC SHAPING 3002The following rules show some of the applications of 3003.Nm 3004and 3005.Nm dummynet 3006for simulations and the like. 3007.Pp 3008This rule drops random incoming packets with a probability 3009of 5%: 3010.Pp 3011.Dl "ipfw add prob 0.05 deny ip from any to any in" 3012.Pp 3013A similar effect can be achieved making use of 3014.Nm dummynet 3015pipes: 3016.Pp 3017.Dl "ipfw add pipe 10 ip from any to any" 3018.Dl "ipfw pipe 10 config plr 0.05" 3019.Pp 3020We can use pipes to artificially limit bandwidth, e.g.\& on a 3021machine acting as a router, if we want to limit traffic from 3022local clients on 192.168.2.0/24 we do: 3023.Pp 3024.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 3025.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 3026.Pp 3027note that we use the 3028.Cm out 3029modifier so that the rule is not used twice. 3030Remember in fact that 3031.Nm 3032rules are checked both on incoming and outgoing packets. 3033.Pp 3034Should we want to simulate a bidirectional link with bandwidth 3035limitations, the correct way is the following: 3036.Pp 3037.Dl "ipfw add pipe 1 ip from any to any out" 3038.Dl "ipfw add pipe 2 ip from any to any in" 3039.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes" 3040.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes" 3041.Pp 3042The above can be very useful, e.g.\& if you want to see how 3043your fancy Web page will look for a residential user who 3044is connected only through a slow link. 3045You should not use only one pipe for both directions, unless 3046you want to simulate a half-duplex medium (e.g.\& AppleTalk, 3047Ethernet, IRDA). 3048It is not necessary that both pipes have the same configuration, 3049so we can also simulate asymmetric links. 3050.Pp 3051Should we want to verify network performance with the RED queue 3052management algorithm: 3053.Pp 3054.Dl "ipfw add pipe 1 ip from any to any" 3055.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1" 3056.Pp 3057Another typical application of the traffic shaper is to 3058introduce some delay in the communication. 3059This can significantly affect applications which do a lot of Remote 3060Procedure Calls, and where the round-trip-time of the 3061connection often becomes a limiting factor much more than 3062bandwidth: 3063.Pp 3064.Dl "ipfw add pipe 1 ip from any to any out" 3065.Dl "ipfw add pipe 2 ip from any to any in" 3066.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s" 3067.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s" 3068.Pp 3069Per-flow queueing can be useful for a variety of purposes. 3070A very simple one is counting traffic: 3071.Pp 3072.Dl "ipfw add pipe 1 tcp from any to any" 3073.Dl "ipfw add pipe 1 udp from any to any" 3074.Dl "ipfw add pipe 1 ip from any to any" 3075.Dl "ipfw pipe 1 config mask all" 3076.Pp 3077The above set of rules will create queues (and collect 3078statistics) for all traffic. 3079Because the pipes have no limitations, the only effect is 3080collecting statistics. 3081Note that we need 3 rules, not just the last one, because 3082when 3083.Nm 3084tries to match IP packets it will not consider ports, so we 3085would not see connections on separate ports as different 3086ones. 3087.Pp 3088A more sophisticated example is limiting the outbound traffic 3089on a net with per-host limits, rather than per-network limits: 3090.Pp 3091.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 3092.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in" 3093.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 3094.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 3095.Ss LOOKUP TABLES 3096In the following example, we need to create several traffic bandwidth 3097classes and we need different hosts/networks to fall into different classes. 3098We create one pipe for each class and configure them accordingly. 3099Then we create a single table and fill it with IP subnets and addresses. 3100For each subnet/host we set the argument equal to the number of the pipe 3101that it should use. 3102Then we classify traffic using a single rule: 3103.Pp 3104.Dl "ipfw pipe 1 config bw 1000Kbyte/s" 3105.Dl "ipfw pipe 4 config bw 4000Kbyte/s" 3106.Dl "..." 3107.Dl "ipfw table 1 add 192.168.2.0/24 1" 3108.Dl "ipfw table 1 add 192.168.0.0/27 4" 3109.Dl "ipfw table 1 add 192.168.0.2 1" 3110.Dl "..." 3111.Dl "ipfw add pipe tablearg ip from table(1) to any" 3112.Pp 3113Using the 3114.Cm fwd 3115action, the table entries may include hostnames and IP addresses. 3116.Pp 3117.Dl "ipfw table 1 add 192.168.2.0/24 10.23.2.1" 3118.Dl "ipfw table 1 add 192.168.0.0/27 router1.dmz" 3119.Dl "..." 3120.Dl "ipfw add 100 fwd tablearg ip from any to table(1)" 3121.Pp 3122In the following example per-interface firewall is created: 3123.Pp 3124.Dl "ipfw table 10 add vlan20 12000" 3125.Dl "ipfw table 10 add vlan30 13000" 3126.Dl "ipfw table 20 add vlan20 22000" 3127.Dl "ipfw table 20 add vlan30 23000" 3128.Dl ".." 3129.Dl "ipfw add 100 ipfw skipto tablearg ip from any to any recv 'table(10)' in" 3130.Dl "ipfw add 200 ipfw skipto tablearg ip from any to any xmit 'table(10)' out" 3131.Ss SETS OF RULES 3132To add a set of rules atomically, e.g.\& set 18: 3133.Pp 3134.Dl "ipfw set disable 18" 3135.Dl "ipfw add NN set 18 ... # repeat as needed" 3136.Dl "ipfw set enable 18" 3137.Pp 3138To delete a set of rules atomically the command is simply: 3139.Pp 3140.Dl "ipfw delete set 18" 3141.Pp 3142To test a ruleset and disable it and regain control if something goes wrong: 3143.Pp 3144.Dl "ipfw set disable 18" 3145.Dl "ipfw add NN set 18 ... # repeat as needed" 3146.Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18" 3147.Pp 3148Here if everything goes well, you press control-C before the "sleep" 3149terminates, and your ruleset will be left active. 3150Otherwise, e.g.\& if 3151you cannot access your box, the ruleset will be disabled after 3152the sleep terminates thus restoring the previous situation. 3153.Pp 3154To show rules of the specific set: 3155.Pp 3156.Dl "ipfw set 18 show" 3157.Pp 3158To show rules of the disabled set: 3159.Pp 3160.Dl "ipfw -S set 18 show" 3161.Pp 3162To clear a specific rule counters of the specific set: 3163.Pp 3164.Dl "ipfw set 18 zero NN" 3165.Pp 3166To delete a specific rule of the specific set: 3167.Pp 3168.Dl "ipfw set 18 delete NN" 3169.Ss NAT, REDIRECT AND LSNAT 3170First redirect all the traffic to nat instance 123: 3171.Pp 3172.Dl "ipfw add nat 123 all from any to any" 3173.Pp 3174Then to configure nat instance 123 to alias all the outgoing traffic with ip 3175192.168.0.123, blocking all incoming connections, trying to keep 3176same ports on both sides, clearing aliasing table on address change 3177and keeping a log of traffic/link statistics: 3178.Pp 3179.Dl "ipfw nat 123 config ip 192.168.0.123 log deny_in reset same_ports" 3180.Pp 3181Or to change address of instance 123, aliasing table will be cleared (see 3182reset option): 3183.Pp 3184.Dl "ipfw nat 123 config ip 10.0.0.1" 3185.Pp 3186To see configuration of nat instance 123: 3187.Pp 3188.Dl "ipfw nat 123 show config" 3189.Pp 3190To show logs of all the instances in range 111-999: 3191.Pp 3192.Dl "ipfw nat 111-999 show" 3193.Pp 3194To see configurations of all instances: 3195.Pp 3196.Dl "ipfw nat show config" 3197.Pp 3198Or a redirect rule with mixed modes could looks like: 3199.Pp 3200.Dl "ipfw nat 123 config redirect_addr 10.0.0.1 10.0.0.66" 3201.Dl " redirect_port tcp 192.168.0.1:80 500" 3202.Dl " redirect_proto udp 192.168.1.43 192.168.1.1" 3203.Dl " redirect_addr 192.168.0.10,192.168.0.11" 3204.Dl " 10.0.0.100 # LSNAT" 3205.Dl " redirect_port tcp 192.168.0.1:80,192.168.0.10:22" 3206.Dl " 500 # LSNAT" 3207.Pp 3208or it could be split in: 3209.Pp 3210.Dl "ipfw nat 1 config redirect_addr 10.0.0.1 10.0.0.66" 3211.Dl "ipfw nat 2 config redirect_port tcp 192.168.0.1:80 500" 3212.Dl "ipfw nat 3 config redirect_proto udp 192.168.1.43 192.168.1.1" 3213.Dl "ipfw nat 4 config redirect_addr 192.168.0.10,192.168.0.11,192.168.0.12" 3214.Dl " 10.0.0.100" 3215.Dl "ipfw nat 5 config redirect_port tcp" 3216.Dl " 192.168.0.1:80,192.168.0.10:22,192.168.0.20:25 500" 3217.Sh SEE ALSO 3218.Xr cpp 1 , 3219.Xr m4 1 , 3220.Xr altq 4 , 3221.Xr divert 4 , 3222.Xr dummynet 4 , 3223.Xr if_bridge 4 , 3224.Xr ip 4 , 3225.Xr ipfirewall 4 , 3226.Xr ng_ipfw 4 , 3227.Xr protocols 5 , 3228.Xr services 5 , 3229.Xr init 8 , 3230.Xr kldload 8 , 3231.Xr reboot 8 , 3232.Xr sysctl 8 , 3233.Xr syslogd 8 3234.Sh HISTORY 3235The 3236.Nm 3237utility first appeared in 3238.Fx 2.0 . 3239.Nm dummynet 3240was introduced in 3241.Fx 2.2.8 . 3242Stateful extensions were introduced in 3243.Fx 4.0 . 3244.Nm ipfw2 3245was introduced in Summer 2002. 3246.Sh AUTHORS 3247.An Ugen J. S. Antsilevich , 3248.An Poul-Henning Kamp , 3249.An Alex Nash , 3250.An Archie Cobbs , 3251.An Luigi Rizzo . 3252.Pp 3253.An -nosplit 3254API based upon code written by 3255.An Daniel Boulet 3256for BSDI. 3257.Pp 3258Dummynet has been introduced by Luigi Rizzo in 1997-1998. 3259.Pp 3260Some early work (1999-2000) on the 3261.Nm dummynet 3262traffic shaper supported by Akamba Corp. 3263.Pp 3264The ipfw core (ipfw2) has been completely redesigned and 3265reimplemented by Luigi Rizzo in summer 2002. Further 3266actions and 3267options have been added by various developer over the years. 3268.Pp 3269.An -nosplit 3270In-kernel NAT support written by 3271.An Paolo Pisati Aq piso@FreeBSD.org 3272as part of a Summer of Code 2005 project. 3273.Pp 3274SCTP 3275.Nm nat 3276support has been developed by 3277.An The Centre for Advanced Internet Architectures (CAIA) Aq http://www.caia.swin.edu.au . 3278The primary developers and maintainers are David Hayes and Jason But. 3279For further information visit: 3280.Aq http://www.caia.swin.edu.au/urp/SONATA 3281.Pp 3282Delay profiles have been developed by Alessandro Cerri and 3283Luigi Rizzo, supported by the 3284European Commission within Projects Onelab and Onelab2. 3285.Sh BUGS 3286The syntax has grown over the years and sometimes it might be confusing. 3287Unfortunately, backward compatibility prevents cleaning up mistakes 3288made in the definition of the syntax. 3289.Pp 3290.Em !!! WARNING !!! 3291.Pp 3292Misconfiguring the firewall can put your computer in an unusable state, 3293possibly shutting down network services and requiring console access to 3294regain control of it. 3295.Pp 3296Incoming packet fragments diverted by 3297.Cm divert 3298are reassembled before delivery to the socket. 3299The action used on those packet is the one from the 3300rule which matches the first fragment of the packet. 3301.Pp 3302Packets diverted to userland, and then reinserted by a userland process 3303may lose various packet attributes. 3304The packet source interface name 3305will be preserved if it is shorter than 8 bytes and the userland process 3306saves and reuses the sockaddr_in 3307(as does 3308.Xr natd 8 ) ; 3309otherwise, it may be lost. 3310If a packet is reinserted in this manner, later rules may be incorrectly 3311applied, making the order of 3312.Cm divert 3313rules in the rule sequence very important. 3314.Pp 3315Dummynet drops all packets with IPv6 link-local addresses. 3316.Pp 3317Rules using 3318.Cm uid 3319or 3320.Cm gid 3321may not behave as expected. 3322In particular, incoming SYN packets may 3323have no uid or gid associated with them since they do not yet belong 3324to a TCP connection, and the uid/gid associated with a packet may not 3325be as expected if the associated process calls 3326.Xr setuid 2 3327or similar system calls. 3328.Pp 3329Rule syntax is subject to the command line environment and some patterns 3330may need to be escaped with the backslash character 3331or quoted appropriately. 3332.Pp 3333Due to the architecture of 3334.Xr libalias 3 , 3335ipfw nat is not compatible with the TCP segmentation offloading (TSO). 3336Thus, to reliably nat your network traffic, please disable TSO 3337on your NICs using 3338.Xr ifconfig 8 . 3339.Pp 3340ICMP error messages are not implicitly matched by dynamic rules 3341for the respective conversations. 3342To avoid failures of network error detection and path MTU discovery, 3343ICMP error messages may need to be allowed explicitly through static 3344rules. 3345.Pp 3346Rules using 3347.Cm call 3348and 3349.Cm return 3350actions may lead to confusing behaviour if ruleset has mistakes, 3351and/or interaction with other subsystems (netgraph, dummynet, etc.) is used. 3352One possible case for this is packet leaving 3353.Nm 3354in subroutine on the input pass, while later on output encountering unpaired 3355.Cm return 3356first. 3357As the call stack is kept intact after input pass, packet will suddenly 3358return to the rule number used on input pass, not on output one. 3359Order of processing should be checked carefully to avoid such mistakes. 3360