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