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