1.\" 2.\" $FreeBSD$ 3.\" 4.Dd May 4, 2007 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, 459Routing Headers, Source routing rthdr0, Mobile IPv6 rthdr2, 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 , 1128any type of Routing Header 1129.Pq Cm route , 1130Source routing Routing Header Type 0 1131.Pq Cm rthdr0 , 1132Mobile IPv6 Routing Header Type 2 1133.Pq Cm rthdr2 , 1134Destination options 1135.Pq Cm dstopt , 1136IPSec authentication headers 1137.Pq Cm ah , 1138and IPSec encapsulated security payload headers 1139.Pq Cm esp . 1140.It Cm flow-id Ar labels 1141Matches IPv6 packets containing any of the flow labels given in 1142.Ar labels . 1143.Ar labels 1144is a comma seperate list of numeric flow labels. 1145.It Cm frag 1146Matches packets that are fragments and not the first 1147fragment of an IP datagram. 1148Note that these packets will not have 1149the next protocol header (e.g.\& TCP, UDP) so options that look into 1150these headers cannot match. 1151.It Cm gid Ar group 1152Matches all TCP or UDP packets sent by or received for a 1153.Ar group . 1154A 1155.Ar group 1156may be specified by name or number. 1157This option should be used only if debug.mpsafenet=0 to avoid possible 1158deadlocks due to layering violations in its implementation. 1159.It Cm jail Ar prisonID 1160Matches all TCP or UDP packets sent by or received for the 1161jail whos prison ID is 1162.Ar prisonID . 1163This option should be used only if debug.mpsafenet=0 to avoid possible 1164deadlocks due to layering violations in its implementation. 1165.It Cm icmptypes Ar types 1166Matches ICMP packets whose ICMP type is in the list 1167.Ar types . 1168The list may be specified as any combination of 1169individual types (numeric) separated by commas. 1170.Em Ranges are not allowed. 1171The supported ICMP types are: 1172.Pp 1173echo reply 1174.Pq Cm 0 , 1175destination unreachable 1176.Pq Cm 3 , 1177source quench 1178.Pq Cm 4 , 1179redirect 1180.Pq Cm 5 , 1181echo request 1182.Pq Cm 8 , 1183router advertisement 1184.Pq Cm 9 , 1185router solicitation 1186.Pq Cm 10 , 1187time-to-live exceeded 1188.Pq Cm 11 , 1189IP header bad 1190.Pq Cm 12 , 1191timestamp request 1192.Pq Cm 13 , 1193timestamp reply 1194.Pq Cm 14 , 1195information request 1196.Pq Cm 15 , 1197information reply 1198.Pq Cm 16 , 1199address mask request 1200.Pq Cm 17 1201and address mask reply 1202.Pq Cm 18 . 1203.It Cm icmp6types Ar types 1204Matches ICMP6 packets whose ICMP6 type is in the list of 1205.Ar types . 1206The list may be specified as any combination of 1207individual types (numeric) separated by commas. 1208.Em Ranges are not allowed. 1209.It Cm in | out 1210Matches incoming or outgoing packets, respectively. 1211.Cm in 1212and 1213.Cm out 1214are mutually exclusive (in fact, 1215.Cm out 1216is implemented as 1217.Cm not in Ns No ). 1218.It Cm ipid Ar id-list 1219Matches IPv4 packets whose 1220.Cm ip_id 1221field has value included in 1222.Ar id-list , 1223which is either a single value or a list of values or ranges 1224specified in the same way as 1225.Ar ports . 1226.It Cm iplen Ar len-list 1227Matches IP packets whose total length, including header and data, is 1228in the set 1229.Ar len-list , 1230which is either a single value or a list of values or ranges 1231specified in the same way as 1232.Ar ports . 1233.It Cm ipoptions Ar spec 1234Matches packets whose IPv4 header contains the comma separated list of 1235options specified in 1236.Ar spec . 1237The supported IP options are: 1238.Pp 1239.Cm ssrr 1240(strict source route), 1241.Cm lsrr 1242(loose source route), 1243.Cm rr 1244(record packet route) and 1245.Cm ts 1246(timestamp). 1247The absence of a particular option may be denoted 1248with a 1249.Ql \&! . 1250.It Cm ipprecedence Ar precedence 1251Matches IPv4 packets whose precedence field is equal to 1252.Ar precedence . 1253.It Cm ipsec 1254Matches packets that have IPSEC history associated with them 1255(i.e., the packet comes encapsulated in IPSEC, the kernel 1256has IPSEC support and IPSEC_FILTERGIF option, and can correctly 1257decapsulate it). 1258.Pp 1259Note that specifying 1260.Cm ipsec 1261is different from specifying 1262.Cm proto Ar ipsec 1263as the latter will only look at the specific IP protocol field, 1264irrespective of IPSEC kernel support and the validity of the IPSEC data. 1265.Pp 1266Further note that this flag is silently ignored in kernels without 1267IPSEC support. 1268It does not affect rule processing when given and the 1269rules are handled as if with no 1270.Cm ipsec 1271flag. 1272.It Cm iptos Ar spec 1273Matches IPv4 packets whose 1274.Cm tos 1275field contains the comma separated list of 1276service types specified in 1277.Ar spec . 1278The supported IP types of service are: 1279.Pp 1280.Cm lowdelay 1281.Pq Dv IPTOS_LOWDELAY , 1282.Cm throughput 1283.Pq Dv IPTOS_THROUGHPUT , 1284.Cm reliability 1285.Pq Dv IPTOS_RELIABILITY , 1286.Cm mincost 1287.Pq Dv IPTOS_MINCOST , 1288.Cm congestion 1289.Pq Dv IPTOS_CE . 1290The absence of a particular type may be denoted 1291with a 1292.Ql \&! . 1293.It Cm ipttl Ar ttl-list 1294Matches IPv4 packets whose time to live is included in 1295.Ar ttl-list , 1296which is either a single value or a list of values or ranges 1297specified in the same way as 1298.Ar ports . 1299.It Cm ipversion Ar ver 1300Matches IP packets whose IP version field is 1301.Ar ver . 1302.It Cm keep-state 1303Upon a match, the firewall will create a dynamic rule, whose 1304default behaviour is to match bidirectional traffic between 1305source and destination IP/port using the same protocol. 1306The rule has a limited lifetime (controlled by a set of 1307.Xr sysctl 8 1308variables), and the lifetime is refreshed every time a matching 1309packet is found. 1310.It Cm layer2 1311Matches only layer2 packets, i.e., those passed to 1312.Nm 1313from ether_demux() and ether_output_frame(). 1314.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N 1315The firewall will only allow 1316.Ar N 1317connections with the same 1318set of parameters as specified in the rule. 1319One or more 1320of source and destination addresses and ports can be 1321specified. 1322Currently, 1323only IPv4 flows are supported. 1324.It Cm { MAC | mac } Ar dst-mac src-mac 1325Match packets with a given 1326.Ar dst-mac 1327and 1328.Ar src-mac 1329addresses, specified as the 1330.Cm any 1331keyword (matching any MAC address), or six groups of hex digits 1332separated by colons, 1333and optionally followed by a mask indicating the significant bits. 1334The mask may be specified using either of the following methods: 1335.Bl -enum -width indent 1336.It 1337A slash 1338.Pq / 1339followed by the number of significant bits. 1340For example, an address with 33 significant bits could be specified as: 1341.Pp 1342.Dl "MAC 10:20:30:40:50:60/33 any" 1343.Pp 1344.It 1345An ampersand 1346.Pq & 1347followed by a bitmask specified as six groups of hex digits separated 1348by colons. 1349For example, an address in which the last 16 bits are significant could 1350be specified as: 1351.Pp 1352.Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any" 1353.Pp 1354Note that the ampersand character has a special meaning in many shells 1355and should generally be escaped. 1356.Pp 1357.El 1358Note that the order of MAC addresses (destination first, 1359source second) is 1360the same as on the wire, but the opposite of the one used for 1361IP addresses. 1362.It Cm mac-type Ar mac-type 1363Matches packets whose Ethernet Type field 1364corresponds to one of those specified as argument. 1365.Ar mac-type 1366is specified in the same way as 1367.Cm port numbers 1368(i.e., one or more comma-separated single values or ranges). 1369You can use symbolic names for known values such as 1370.Em vlan , ipv4, ipv6 . 1371Values can be entered as decimal or hexadecimal (if prefixed by 0x), 1372and they are always printed as hexadecimal (unless the 1373.Cm -N 1374option is used, in which case symbolic resolution will be attempted). 1375.It Cm proto Ar protocol 1376Matches packets with the corresponding IP protocol. 1377.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any 1378Matches packets received, transmitted or going through, 1379respectively, the interface specified by exact name 1380.Ns No ( Ar ifX Ns No ), 1381by device name 1382.Ns No ( Ar if Ns Ar * Ns No ), 1383by IP address, or through some interface. 1384.Pp 1385The 1386.Cm via 1387keyword causes the interface to always be checked. 1388If 1389.Cm recv 1390or 1391.Cm xmit 1392is used instead of 1393.Cm via , 1394then only the receive or transmit interface (respectively) 1395is checked. 1396By specifying both, it is possible to match packets based on 1397both receive and transmit interface, e.g.: 1398.Pp 1399.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1" 1400.Pp 1401The 1402.Cm recv 1403interface can be tested on either incoming or outgoing packets, 1404while the 1405.Cm xmit 1406interface can only be tested on outgoing packets. 1407So 1408.Cm out 1409is required (and 1410.Cm in 1411is invalid) whenever 1412.Cm xmit 1413is used. 1414.Pp 1415A packet may not have a receive or transmit interface: packets 1416originating from the local host have no receive interface, 1417while packets destined for the local host have no transmit 1418interface. 1419.It Cm setup 1420Matches TCP packets that have the SYN bit set but no ACK bit. 1421This is the short form of 1422.Dq Li tcpflags\ syn,!ack . 1423.It Cm src-ip Ar ip-address 1424Matches IPv4 packets whose source IP is one of the address(es) 1425specified as an argument. 1426.It Cm src-ip6 Ar ip6-address 1427Matches IPv6 packets whose source IP is one of the address(es) 1428specified as an argument. 1429.It Cm src-port Ar ports 1430Matches IP packets whose source port is one of the port(s) 1431specified as argument. 1432.It Cm tagged Ar tag-list 1433Matches packets whose tags are included in 1434.Ar tag-list , 1435which is either a single value or a list of values or ranges 1436specified in the same way as 1437.Ar ports . 1438Tags can be applied to the packet using 1439.Cm tag 1440rule action parameter (see it's description for details on tags). 1441.It Cm tcpack Ar ack 1442TCP packets only. 1443Match if the TCP header acknowledgment number field is set to 1444.Ar ack . 1445.It Cm tcpdatalen Ar tcpdatalen-list 1446Matches TCP packets whose length of TCP data is 1447.Ar tcpdatalen-list , 1448which is either a single value or a list of values or ranges 1449specified in the same way as 1450.Ar ports . 1451.It Cm tcpflags Ar spec 1452TCP packets only. 1453Match if the TCP header contains the comma separated list of 1454flags specified in 1455.Ar spec . 1456The supported TCP flags are: 1457.Pp 1458.Cm fin , 1459.Cm syn , 1460.Cm rst , 1461.Cm psh , 1462.Cm ack 1463and 1464.Cm urg . 1465The absence of a particular flag may be denoted 1466with a 1467.Ql \&! . 1468A rule which contains a 1469.Cm tcpflags 1470specification can never match a fragmented packet which has 1471a non-zero offset. 1472See the 1473.Cm frag 1474option for details on matching fragmented packets. 1475.It Cm tcpseq Ar seq 1476TCP packets only. 1477Match if the TCP header sequence number field is set to 1478.Ar seq . 1479.It Cm tcpwin Ar win 1480TCP packets only. 1481Match if the TCP header window field is set to 1482.Ar win . 1483.It Cm tcpoptions Ar spec 1484TCP packets only. 1485Match if the TCP header contains the comma separated list of 1486options specified in 1487.Ar spec . 1488The supported TCP options are: 1489.Pp 1490.Cm mss 1491(maximum segment size), 1492.Cm window 1493(tcp window advertisement), 1494.Cm sack 1495(selective ack), 1496.Cm ts 1497(rfc1323 timestamp) and 1498.Cm cc 1499(rfc1644 t/tcp connection count). 1500The absence of a particular option may be denoted 1501with a 1502.Ql \&! . 1503.It Cm uid Ar user 1504Match all TCP or UDP packets sent by or received for a 1505.Ar user . 1506A 1507.Ar user 1508may be matched by name or identification number. 1509This option should be used only if debug.mpsafenet=0 to avoid possible 1510deadlocks due to layering violations in its implementation. 1511.It Cm verrevpath 1512For incoming packets, 1513a routing table lookup is done on the packet's source address. 1514If the interface on which the packet entered the system matches the 1515outgoing interface for the route, 1516the packet matches. 1517If the interfaces do not match up, 1518the packet does not match. 1519All outgoing packets or packets with no incoming interface match. 1520.Pp 1521The name and functionality of the option is intentionally similar to 1522the Cisco IOS command: 1523.Pp 1524.Dl ip verify unicast reverse-path 1525.Pp 1526This option can be used to make anti-spoofing rules to reject all 1527packets with source addresses not from this interface. 1528See also the option 1529.Cm antispoof . 1530.It Cm versrcreach 1531For incoming packets, 1532a routing table lookup is done on the packet's source address. 1533If a route to the source address exists, but not the default route 1534or a blackhole/reject route, the packet matches. 1535Otherwise, the packet does not match. 1536All outgoing packets match. 1537.Pp 1538The name and functionality of the option is intentionally similar to 1539the Cisco IOS command: 1540.Pp 1541.Dl ip verify unicast source reachable-via any 1542.Pp 1543This option can be used to make anti-spoofing rules to reject all 1544packets whose source address is unreachable. 1545.It Cm antispoof 1546For incoming packets, the packet's source address is checked if it 1547belongs to a directly connected network. 1548If the network is directly connected, then the interface the packet 1549came on in is compared to the interface the network is connected to. 1550When incoming interface and directly connected interface are not the 1551same, the packet does not match. 1552Otherwise, the packet does match. 1553All outgoing packets match. 1554.Pp 1555This option can be used to make anti-spoofing rules to reject all 1556packets that pretend to be from a directly connected network but do 1557not come in through that interface. 1558This option is similar to but more restricted than 1559.Cm verrevpath 1560because it engages only on packets with source addresses of directly 1561connected networks instead of all source addresses. 1562.El 1563.Sh LOOKUP TABLES 1564Lookup tables are useful to handle large sparse address sets, 1565typically from a hundred to several thousands of entries. 1566There may be up to 128 different lookup tables, numbered 0 to 127. 1567.Pp 1568Each entry is represented by an 1569.Ar addr Ns Op / Ns Ar masklen 1570and will match all addresses with base 1571.Ar addr 1572(specified as an IP address or a hostname) 1573and mask width of 1574.Ar masklen 1575bits. 1576If 1577.Ar masklen 1578is not specified, it defaults to 32. 1579When looking up an IP address in a table, the most specific 1580entry will match. 1581Associated with each entry is a 32-bit unsigned 1582.Ar value , 1583which can optionally be checked by a rule matching code. 1584When adding an entry, if 1585.Ar value 1586is not specified, it defaults to 0. 1587.Pp 1588An entry can be added to a table 1589.Pq Cm add , 1590removed from a table 1591.Pq Cm delete , 1592a table can be examined 1593.Pq Cm list 1594or flushed 1595.Pq Cm flush . 1596.Pp 1597Internally, each table is stored in a Radix tree, the same way as 1598the routing table (see 1599.Xr route 4 ) . 1600.Pp 1601Lookup tables currently support IPv4 addresses only. 1602.Pp 1603The 1604.Cm tablearg 1605feature provides the ability to use a value, looked up in the table, as 1606the argument for a rule action, action parameter or rule option. 1607This can significantly reduce number of rules in some configurations. 1608The 1609.Cm tablearg 1610argument can be used with the following actions: 1611.Cm pipe , queue, divert, tee, netgraph, ngtee, fwd 1612action parameters: 1613.Cm tag, untag, 1614rule options: 1615.Cm limit, tagged. 1616.Pp 1617When used with 1618.Cm fwd 1619it is possible to supply table entries with values 1620that are in the form of IP addresses or hostnames. 1621See the 1622.Sx EXAMPLES 1623Section for example usage of tables and the tablearg keyword. 1624.Sh SETS OF RULES 1625Each rule belongs to one of 32 different 1626.Em sets 1627, numbered 0 to 31. 1628Set 31 is reserved for the default rule. 1629.Pp 1630By default, rules are put in set 0, unless you use the 1631.Cm set N 1632attribute when entering a new rule. 1633Sets can be individually and atomically enabled or disabled, 1634so this mechanism permits an easy way to store multiple configurations 1635of the firewall and quickly (and atomically) switch between them. 1636The command to enable/disable sets is 1637.Bd -ragged -offset indent 1638.Nm 1639.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ... 1640.Ed 1641.Pp 1642where multiple 1643.Cm enable 1644or 1645.Cm disable 1646sections can be specified. 1647Command execution is atomic on all the sets specified in the command. 1648By default, all sets are enabled. 1649.Pp 1650When you disable a set, its rules behave as if they do not exist 1651in the firewall configuration, with only one exception: 1652.Bd -ragged -offset indent 1653dynamic rules created from a rule before it had been disabled 1654will still be active until they expire. 1655In order to delete 1656dynamic rules you have to explicitly delete the parent rule 1657which generated them. 1658.Ed 1659.Pp 1660The set number of rules can be changed with the command 1661.Bd -ragged -offset indent 1662.Nm 1663.Cm set move 1664.Brq Cm rule Ar rule-number | old-set 1665.Cm to Ar new-set 1666.Ed 1667.Pp 1668Also, you can atomically swap two rulesets with the command 1669.Bd -ragged -offset indent 1670.Nm 1671.Cm set swap Ar first-set second-set 1672.Ed 1673.Pp 1674See the 1675.Sx EXAMPLES 1676Section on some possible uses of sets of rules. 1677.Sh STATEFUL FIREWALL 1678Stateful operation is a way for the firewall to dynamically 1679create rules for specific flows when packets that 1680match a given pattern are detected. 1681Support for stateful 1682operation comes through the 1683.Cm check-state , keep-state 1684and 1685.Cm limit 1686options of 1687.Nm rules . 1688.Pp 1689Dynamic rules are created when a packet matches a 1690.Cm keep-state 1691or 1692.Cm limit 1693rule, causing the creation of a 1694.Em dynamic 1695rule which will match all and only packets with 1696a given 1697.Em protocol 1698between a 1699.Em src-ip/src-port dst-ip/dst-port 1700pair of addresses 1701.Em ( src 1702and 1703.Em dst 1704are used here only to denote the initial match addresses, but they 1705are completely equivalent afterwards). 1706Dynamic rules will be checked at the first 1707.Cm check-state, keep-state 1708or 1709.Cm limit 1710occurrence, and the action performed upon a match will be the same 1711as in the parent rule. 1712.Pp 1713Note that no additional attributes other than protocol and IP addresses 1714and ports are checked on dynamic rules. 1715.Pp 1716The typical use of dynamic rules is to keep a closed firewall configuration, 1717but let the first TCP SYN packet from the inside network install a 1718dynamic rule for the flow so that packets belonging to that session 1719will be allowed through the firewall: 1720.Pp 1721.Dl "ipfw add check-state" 1722.Dl "ipfw add allow tcp from my-subnet to any setup keep-state" 1723.Dl "ipfw add deny tcp from any to any" 1724.Pp 1725A similar approach can be used for UDP, where an UDP packet coming 1726from the inside will install a dynamic rule to let the response through 1727the firewall: 1728.Pp 1729.Dl "ipfw add check-state" 1730.Dl "ipfw add allow udp from my-subnet to any keep-state" 1731.Dl "ipfw add deny udp from any to any" 1732.Pp 1733Dynamic rules expire after some time, which depends on the status 1734of the flow and the setting of some 1735.Cm sysctl 1736variables. 1737See Section 1738.Sx SYSCTL VARIABLES 1739for more details. 1740For TCP sessions, dynamic rules can be instructed to periodically 1741send keepalive packets to refresh the state of the rule when it is 1742about to expire. 1743.Pp 1744See Section 1745.Sx EXAMPLES 1746for more examples on how to use dynamic rules. 1747.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION 1748.Nm 1749is also the user interface for the 1750.Xr dummynet 4 1751traffic shaper. 1752.Pp 1753.Nm dummynet 1754operates by first using the firewall to classify packets and divide them into 1755.Em flows , 1756using any match pattern that can be used in 1757.Nm 1758rules. 1759Depending on local policies, a flow can contain packets for a single 1760TCP connection, or from/to a given host, or entire subnet, or a 1761protocol type, etc. 1762.Pp 1763Packets belonging to the same flow are then passed to either of two 1764different objects, which implement the traffic regulation: 1765.Bl -hang -offset XXXX 1766.It Em pipe 1767A pipe emulates a link with given bandwidth, propagation delay, 1768queue size and packet loss rate. 1769Packets are queued in front of the pipe as they come out from the classifier, 1770and then transferred to the pipe according to the pipe's parameters. 1771.Pp 1772.It Em queue 1773A queue 1774is an abstraction used to implement the WF2Q+ 1775(Worst-case Fair Weighted Fair Queueing) policy, which is 1776an efficient variant of the WFQ policy. 1777.br 1778The queue associates a 1779.Em weight 1780and a reference pipe to each flow, and then all backlogged (i.e., 1781with packets queued) flows linked to the same pipe share the pipe's 1782bandwidth proportionally to their weights. 1783Note that weights are not priorities; a flow with a lower weight 1784is still guaranteed to get its fraction of the bandwidth even if a 1785flow with a higher weight is permanently backlogged. 1786.Pp 1787.El 1788In practice, 1789.Em pipes 1790can be used to set hard limits to the bandwidth that a flow can use, whereas 1791.Em queues 1792can be used to determine how different flow share the available bandwidth. 1793.Pp 1794The 1795.Em pipe 1796and 1797.Em queue 1798configuration commands are the following: 1799.Bd -ragged -offset indent 1800.Cm pipe Ar number Cm config Ar pipe-configuration 1801.Pp 1802.Cm queue Ar number Cm config Ar queue-configuration 1803.Ed 1804.Pp 1805The following parameters can be configured for a pipe: 1806.Pp 1807.Bl -tag -width indent -compact 1808.It Cm bw Ar bandwidth | device 1809Bandwidth, measured in 1810.Sm off 1811.Op Cm K | M 1812.Brq Cm bit/s | Byte/s . 1813.Sm on 1814.Pp 1815A value of 0 (default) means unlimited bandwidth. 1816The unit must immediately follow the number, as in 1817.Pp 1818.Dl "ipfw pipe 1 config bw 300Kbit/s" 1819.Pp 1820If a device name is specified instead of a numeric value, as in 1821.Pp 1822.Dl "ipfw pipe 1 config bw tun0" 1823.Pp 1824then the transmit clock is supplied by the specified device. 1825At the moment only the 1826.Xr tun 4 1827device supports this 1828functionality, for use in conjunction with 1829.Xr ppp 8 . 1830.Pp 1831.It Cm delay Ar ms-delay 1832Propagation delay, measured in milliseconds. 1833The value is rounded to the next multiple of the clock tick 1834(typically 10ms, but it is a good practice to run kernels 1835with 1836.Dq "options HZ=1000" 1837to reduce 1838the granularity to 1ms or less). 1839Default value is 0, meaning no delay. 1840.El 1841.Pp 1842The following parameters can be configured for a queue: 1843.Pp 1844.Bl -tag -width indent -compact 1845.It Cm pipe Ar pipe_nr 1846Connects a queue to the specified pipe. 1847Multiple queues (with the same or different weights) can be connected to 1848the same pipe, which specifies the aggregate rate for the set of queues. 1849.Pp 1850.It Cm weight Ar weight 1851Specifies the weight to be used for flows matching this queue. 1852The weight must be in the range 1..100, and defaults to 1. 1853.El 1854.Pp 1855Finally, the following parameters can be configured for both 1856pipes and queues: 1857.Pp 1858.Bl -tag -width XXXX -compact 1859.Pp 1860.It Cm buckets Ar hash-table-size 1861Specifies the size of the hash table used for storing the 1862various queues. 1863Default value is 64 controlled by the 1864.Xr sysctl 8 1865variable 1866.Em net.inet.ip.dummynet.hash_size , 1867allowed range is 16 to 65536. 1868.Pp 1869.It Cm mask Ar mask-specifier 1870Packets sent to a given pipe or queue by an 1871.Nm 1872rule can be further classified into multiple flows, each of which is then 1873sent to a different 1874.Em dynamic 1875pipe or queue. 1876A flow identifier is constructed by masking the IP addresses, 1877ports and protocol types as specified with the 1878.Cm mask 1879options in the configuration of the pipe or queue. 1880For each different flow identifier, a new pipe or queue is created 1881with the same parameters as the original object, and matching packets 1882are sent to it. 1883.Pp 1884Thus, when 1885.Em dynamic pipes 1886are used, each flow will get the same bandwidth as defined by the pipe, 1887whereas when 1888.Em dynamic queues 1889are used, each flow will share the parent's pipe bandwidth evenly 1890with other flows generated by the same queue (note that other queues 1891with different weights might be connected to the same pipe). 1892.br 1893Available mask specifiers are a combination of one or more of the following: 1894.Pp 1895.Cm dst-ip Ar mask , 1896.Cm dst-ip6 Ar mask , 1897.Cm src-ip Ar mask , 1898.Cm src-ip6 Ar mask , 1899.Cm dst-port Ar mask , 1900.Cm src-port Ar mask , 1901.Cm flow-id Ar mask , 1902.Cm proto Ar mask 1903or 1904.Cm all , 1905.Pp 1906where the latter means all bits in all fields are significant. 1907.Pp 1908.It Cm noerror 1909When a packet is dropped by a dummynet queue or pipe, the error 1910is normally reported to the caller routine in the kernel, in the 1911same way as it happens when a device queue fills up. 1912Setting this 1913option reports the packet as successfully delivered, which can be 1914needed for some experimental setups where you want to simulate 1915loss or congestion at a remote router. 1916.Pp 1917.It Cm plr Ar packet-loss-rate 1918Packet loss rate. 1919Argument 1920.Ar packet-loss-rate 1921is a floating-point number between 0 and 1, with 0 meaning no 1922loss, 1 meaning 100% loss. 1923The loss rate is internally represented on 31 bits. 1924.Pp 1925.It Cm queue Brq Ar slots | size Ns Cm Kbytes 1926Queue size, in 1927.Ar slots 1928or 1929.Cm KBytes . 1930Default value is 50 slots, which 1931is the typical queue size for Ethernet devices. 1932Note that for slow speed links you should keep the queue 1933size short or your traffic might be affected by a significant 1934queueing delay. 1935E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit 1936or 20s of queue on a 30Kbit/s pipe. 1937Even worse effects can result if you get packets from an 1938interface with a much larger MTU, e.g.\& the loopback interface 1939with its 16KB packets. 1940.Pp 1941.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p 1942Make use of the RED (Random Early Detection) queue management algorithm. 1943.Ar w_q 1944and 1945.Ar max_p 1946are floating 1947point numbers between 0 and 1 (0 not included), while 1948.Ar min_th 1949and 1950.Ar max_th 1951are integer numbers specifying thresholds for queue management 1952(thresholds are computed in bytes if the queue has been defined 1953in bytes, in slots otherwise). 1954The 1955.Xr dummynet 4 1956also supports the gentle RED variant (gred). 1957Three 1958.Xr sysctl 8 1959variables can be used to control the RED behaviour: 1960.Bl -tag -width indent 1961.It Em net.inet.ip.dummynet.red_lookup_depth 1962specifies the accuracy in computing the average queue 1963when the link is idle (defaults to 256, must be greater than zero) 1964.It Em net.inet.ip.dummynet.red_avg_pkt_size 1965specifies the expected average packet size (defaults to 512, must be 1966greater than zero) 1967.It Em net.inet.ip.dummynet.red_max_pkt_size 1968specifies the expected maximum packet size, only used when queue 1969thresholds are in bytes (defaults to 1500, must be greater than zero). 1970.El 1971.El 1972.Pp 1973When used with IPv6 data, dummynet currently has several limitations. 1974First, debug.mpsafenet=0 must be set. 1975Second, the information necessicary to route link-local packets to an 1976interface is not avalable after processing by dummynet so those packets 1977are dropped in the output path. 1978Care should be taken to insure that link-local packets are not passed to 1979dummynet. 1980.Sh CHECKLIST 1981Here are some important points to consider when designing your 1982rules: 1983.Bl -bullet 1984.It 1985Remember that you filter both packets going 1986.Cm in 1987and 1988.Cm out . 1989Most connections need packets going in both directions. 1990.It 1991Remember to test very carefully. 1992It is a good idea to be near the console when doing this. 1993If you cannot be near the console, 1994use an auto-recovery script such as the one in 1995.Pa /usr/share/examples/ipfw/change_rules.sh . 1996.It 1997Do not forget the loopback interface. 1998.El 1999.Sh FINE POINTS 2000.Bl -bullet 2001.It 2002There are circumstances where fragmented datagrams are unconditionally 2003dropped. 2004TCP packets are dropped if they do not contain at least 20 bytes of 2005TCP header, UDP packets are dropped if they do not contain a full 8 2006byte UDP header, and ICMP packets are dropped if they do not contain 20074 bytes of ICMP header, enough to specify the ICMP type, code, and 2008checksum. 2009These packets are simply logged as 2010.Dq pullup failed 2011since there may not be enough good data in the packet to produce a 2012meaningful log entry. 2013.It 2014Another type of packet is unconditionally dropped, a TCP packet with a 2015fragment offset of one. 2016This is a valid packet, but it only has one use, to try 2017to circumvent firewalls. 2018When logging is enabled, these packets are 2019reported as being dropped by rule -1. 2020.It 2021If you are logged in over a network, loading the 2022.Xr kld 4 2023version of 2024.Nm 2025is probably not as straightforward as you would think. 2026I recommend the following command line: 2027.Bd -literal -offset indent 2028kldload ipfw && \e 2029ipfw add 32000 allow ip from any to any 2030.Ed 2031.Pp 2032Along the same lines, doing an 2033.Bd -literal -offset indent 2034ipfw flush 2035.Ed 2036.Pp 2037in similar surroundings is also a bad idea. 2038.It 2039The 2040.Nm 2041filter list may not be modified if the system security level 2042is set to 3 or higher 2043(see 2044.Xr init 8 2045for information on system security levels). 2046.El 2047.Sh PACKET DIVERSION 2048A 2049.Xr divert 4 2050socket bound to the specified port will receive all packets 2051diverted to that port. 2052If no socket is bound to the destination port, or if the divert module is 2053not loaded, or if the kernel was not compiled with divert socket support, 2054the packets are dropped. 2055.Sh NETWORK ADDRESS TRANSLATION (NAT) 2056The nat configuration command is the following: 2057.Bd -ragged -offset indent 2058.Bk -words 2059.Cm nat 2060.Ar nat_number 2061.Cm config 2062.Ar nat-configuration 2063.Ek 2064.Ed 2065.Pp 2066. 2067The following parameters can be configured: 2068.Bl -tag -width indent 2069.It Cm ip Ar ip_address 2070Define an ip address to use for aliasing. 2071.It Cm if Ar nic 2072Use ip addres of NIC for aliasing, dynamically changing 2073it if NIC's ip address change. 2074.It Cm log 2075Enable logging on this nat instance. 2076.It Cm deny_in 2077Deny any incoming connection from outside world. 2078.It Cm same_ports 2079Try to leave the alias port numbers unchanged from 2080the actual local port numbers. 2081.It Cm unreg_only 2082Traffic on the local network not originating from an 2083unregistered address spaces will be ignored. 2084.It Cm reset 2085Reset table of the packet aliasing engine on address change. 2086.It Cm reverse 2087Reverse the way libalias handles aliasing. 2088.It Cm proxy_only 2089Obey transparent proxy rules only, packet aliasing is not performed. 2090.El 2091.Pp 2092To let the packet continue after being (de)aliased, set the sysctl variable 2093.Em net.inet.ip.fw.one_pass 2094to 0. 2095For more information about aliasing modes, refer to 2096.Xr libalias 3 2097. 2098See Section 2099.Sx EXAMPLES 2100for some examples about nat usage. 2101.Sh REDIRECT AND LSNAT SUPPORT IN IPFW 2102Redirect and LSNAT support follow closely the syntax used in 2103.Xr natd 8 2104. 2105See Section 2106.Sx EXAMPLES 2107for some examples on how to do redirect and lsnat. 2108.Sh SYSCTL VARIABLES 2109A set of 2110.Xr sysctl 8 2111variables controls the behaviour of the firewall and 2112associated modules 2113.Pq Nm dummynet , bridge . 2114These are shown below together with their default value 2115(but always check with the 2116.Xr sysctl 8 2117command what value is actually in use) and meaning: 2118.Bl -tag -width indent 2119.It Em net.inet.ip.dummynet.expire : No 1 2120Lazily delete dynamic pipes/queue once they have no pending traffic. 2121You can disable this by setting the variable to 0, in which case 2122the pipes/queues will only be deleted when the threshold is reached. 2123.It Em net.inet.ip.dummynet.hash_size : No 64 2124Default size of the hash table used for dynamic pipes/queues. 2125This value is used when no 2126.Cm buckets 2127option is specified when configuring a pipe/queue. 2128.It Em net.inet.ip.dummynet.max_chain_len : No 16 2129Target value for the maximum number of pipes/queues in a hash bucket. 2130The product 2131.Cm max_chain_len*hash_size 2132is used to determine the threshold over which empty pipes/queues 2133will be expired even when 2134.Cm net.inet.ip.dummynet.expire=0 . 2135.It Em net.inet.ip.dummynet.red_lookup_depth : No 256 2136.It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512 2137.It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500 2138Parameters used in the computations of the drop probability 2139for the RED algorithm. 2140.It Em net.inet.ip.fw.autoinc_step : No 100 2141Delta between rule numbers when auto-generating them. 2142The value must be in the range 1..1000. 2143.It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets 2144The current number of buckets in the hash table for dynamic rules 2145(readonly). 2146.It Em net.inet.ip.fw.debug : No 1 2147Controls debugging messages produced by 2148.Nm . 2149.It Em net.inet.ip.fw.dyn_buckets : No 256 2150The number of buckets in the hash table for dynamic rules. 2151Must be a power of 2, up to 65536. 2152It only takes effect when all dynamic rules have expired, so you 2153are advised to use a 2154.Cm flush 2155command to make sure that the hash table is resized. 2156.It Em net.inet.ip.fw.dyn_count : No 3 2157Current number of dynamic rules 2158(read-only). 2159.It Em net.inet.ip.fw.dyn_keepalive : No 1 2160Enables generation of keepalive packets for 2161.Cm keep-state 2162rules on TCP sessions. 2163A keepalive is generated to both 2164sides of the connection every 5 seconds for the last 20 2165seconds of the lifetime of the rule. 2166.It Em net.inet.ip.fw.dyn_max : No 8192 2167Maximum number of dynamic rules. 2168When you hit this limit, no more dynamic rules can be 2169installed until old ones expire. 2170.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300 2171.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20 2172.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1 2173.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1 2174.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5 2175.It Em net.inet.ip.fw.dyn_short_lifetime : No 30 2176These variables control the lifetime, in seconds, of dynamic 2177rules. 2178Upon the initial SYN exchange the lifetime is kept short, 2179then increased after both SYN have been seen, then decreased 2180again during the final FIN exchange or when a RST is received. 2181Both 2182.Em dyn_fin_lifetime 2183and 2184.Em dyn_rst_lifetime 2185must be strictly lower than 5 seconds, the period of 2186repetition of keepalives. 2187The firewall enforces that. 2188.It Em net.inet.ip.fw.enable : No 1 2189Enables the firewall. 2190Setting this variable to 0 lets you run your machine without 2191firewall even if compiled in. 2192.It Em net.inet6.ip6.fw.enable : No 1 2193provides the same functionality as above for the IPv6 case. 2194.It Em net.inet.ip.fw.one_pass : No 1 2195When set, the packet exiting from the 2196.Xr dummynet 4 2197pipe or from 2198.Xr ng_ipfw 4 2199node is not passed though the firewall again. 2200Otherwise, after an action, the packet is 2201reinjected into the firewall at the next rule. 2202.It Em net.inet.ip.fw.verbose : No 1 2203Enables verbose messages. 2204.It Em net.inet.ip.fw.verbose_limit : No 0 2205Limits the number of messages produced by a verbose firewall. 2206.It Em net.inet6.ip6.fw.deny_unknown_exthdrs : No 1 2207If enabled packets with unknown IPv6 Extension Headers will be denied. 2208.It Em net.link.ether.ipfw : No 0 2209Controls whether layer-2 packets are passed to 2210.Nm . 2211Default is no. 2212.It Em net.link.bridge.ipfw : No 0 2213Controls whether bridged packets are passed to 2214.Nm . 2215Default is no. 2216.El 2217.Pp 2218.Sh EXAMPLES 2219There are far too many possible uses of 2220.Nm 2221so this Section will only give a small set of examples. 2222.Pp 2223.Ss BASIC PACKET FILTERING 2224This command adds an entry which denies all tcp packets from 2225.Em cracker.evil.org 2226to the telnet port of 2227.Em wolf.tambov.su 2228from being forwarded by the host: 2229.Pp 2230.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet" 2231.Pp 2232This one disallows any connection from the entire cracker's 2233network to my host: 2234.Pp 2235.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org" 2236.Pp 2237A first and efficient way to limit access (not using dynamic rules) 2238is the use of the following rules: 2239.Pp 2240.Dl "ipfw add allow tcp from any to any established" 2241.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup" 2242.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup" 2243.Dl "..." 2244.Dl "ipfw add deny tcp from any to any" 2245.Pp 2246The first rule will be a quick match for normal TCP packets, 2247but it will not match the initial SYN packet, which will be 2248matched by the 2249.Cm setup 2250rules only for selected source/destination pairs. 2251All other SYN packets will be rejected by the final 2252.Cm deny 2253rule. 2254.Pp 2255If you administer one or more subnets, you can take advantage 2256of the address sets and or-blocks and write extremely 2257compact rulesets which selectively enable services to blocks 2258of clients, as below: 2259.Pp 2260.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q" 2261.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q" 2262.Dl "" 2263.Dl "ipfw add allow ip from ${goodguys} to any" 2264.Dl "ipfw add deny ip from ${badguys} to any" 2265.Dl "... normal policies ..." 2266.Pp 2267The 2268.Cm verrevpath 2269option could be used to do automated anti-spoofing by adding the 2270following to the top of a ruleset: 2271.Pp 2272.Dl "ipfw add deny ip from any to any not verrevpath in" 2273.Pp 2274This rule drops all incoming packets that appear to be coming to the 2275system on the wrong interface. 2276For example, a packet with a source 2277address belonging to a host on a protected internal network would be 2278dropped if it tried to enter the system from an external interface. 2279.Pp 2280The 2281.Cm antispoof 2282option could be used to do similar but more restricted anti-spoofing 2283by adding the following to the top of a ruleset: 2284.Pp 2285.Dl "ipfw add deny ip from any to any not antispoof in" 2286.Pp 2287This rule drops all incoming packets that appear to be coming from another 2288directly connected system but on the wrong interface. 2289For example, a packet with a source address of 2290.Li 192.168.0.0/24 2291, configured on 2292.Li fxp0 2293, but coming in on 2294.Li fxp1 2295would be dropped. 2296.Ss DYNAMIC RULES 2297In order to protect a site from flood attacks involving fake 2298TCP packets, it is safer to use dynamic rules: 2299.Pp 2300.Dl "ipfw add check-state" 2301.Dl "ipfw add deny tcp from any to any established" 2302.Dl "ipfw add allow tcp from my-net to any setup keep-state" 2303.Pp 2304This will let the firewall install dynamic rules only for 2305those connection which start with a regular SYN packet coming 2306from the inside of our network. 2307Dynamic rules are checked when encountering the first 2308.Cm check-state 2309or 2310.Cm keep-state 2311rule. 2312A 2313.Cm check-state 2314rule should usually be placed near the beginning of the 2315ruleset to minimize the amount of work scanning the ruleset. 2316Your mileage may vary. 2317.Pp 2318To limit the number of connections a user can open 2319you can use the following type of rules: 2320.Pp 2321.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10" 2322.Dl "ipfw add allow tcp from any to me setup limit src-addr 4" 2323.Pp 2324The former (assuming it runs on a gateway) will allow each host 2325on a /24 network to open at most 10 TCP connections. 2326The latter can be placed on a server to make sure that a single 2327client does not use more than 4 simultaneous connections. 2328.Pp 2329.Em BEWARE : 2330stateful rules can be subject to denial-of-service attacks 2331by a SYN-flood which opens a huge number of dynamic rules. 2332The effects of such attacks can be partially limited by 2333acting on a set of 2334.Xr sysctl 8 2335variables which control the operation of the firewall. 2336.Pp 2337Here is a good usage of the 2338.Cm list 2339command to see accounting records and timestamp information: 2340.Pp 2341.Dl ipfw -at list 2342.Pp 2343or in short form without timestamps: 2344.Pp 2345.Dl ipfw -a list 2346.Pp 2347which is equivalent to: 2348.Pp 2349.Dl ipfw show 2350.Pp 2351Next rule diverts all incoming packets from 192.168.2.0/24 2352to divert port 5000: 2353.Pp 2354.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in 2355.Pp 2356.Ss TRAFFIC SHAPING 2357The following rules show some of the applications of 2358.Nm 2359and 2360.Xr dummynet 4 2361for simulations and the like. 2362.Pp 2363This rule drops random incoming packets with a probability 2364of 5%: 2365.Pp 2366.Dl "ipfw add prob 0.05 deny ip from any to any in" 2367.Pp 2368A similar effect can be achieved making use of dummynet pipes: 2369.Pp 2370.Dl "ipfw add pipe 10 ip from any to any" 2371.Dl "ipfw pipe 10 config plr 0.05" 2372.Pp 2373We can use pipes to artificially limit bandwidth, e.g.\& on a 2374machine acting as a router, if we want to limit traffic from 2375local clients on 192.168.2.0/24 we do: 2376.Pp 2377.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 2378.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 2379.Pp 2380note that we use the 2381.Cm out 2382modifier so that the rule is not used twice. 2383Remember in fact that 2384.Nm 2385rules are checked both on incoming and outgoing packets. 2386.Pp 2387Should we want to simulate a bidirectional link with bandwidth 2388limitations, the correct way is the following: 2389.Pp 2390.Dl "ipfw add pipe 1 ip from any to any out" 2391.Dl "ipfw add pipe 2 ip from any to any in" 2392.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes" 2393.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes" 2394.Pp 2395The above can be very useful, e.g.\& if you want to see how 2396your fancy Web page will look for a residential user who 2397is connected only through a slow link. 2398You should not use only one pipe for both directions, unless 2399you want to simulate a half-duplex medium (e.g.\& AppleTalk, 2400Ethernet, IRDA). 2401It is not necessary that both pipes have the same configuration, 2402so we can also simulate asymmetric links. 2403.Pp 2404Should we want to verify network performance with the RED queue 2405management algorithm: 2406.Pp 2407.Dl "ipfw add pipe 1 ip from any to any" 2408.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1" 2409.Pp 2410Another typical application of the traffic shaper is to 2411introduce some delay in the communication. 2412This can significantly affect applications which do a lot of Remote 2413Procedure Calls, and where the round-trip-time of the 2414connection often becomes a limiting factor much more than 2415bandwidth: 2416.Pp 2417.Dl "ipfw add pipe 1 ip from any to any out" 2418.Dl "ipfw add pipe 2 ip from any to any in" 2419.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s" 2420.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s" 2421.Pp 2422Per-flow queueing can be useful for a variety of purposes. 2423A very simple one is counting traffic: 2424.Pp 2425.Dl "ipfw add pipe 1 tcp from any to any" 2426.Dl "ipfw add pipe 1 udp from any to any" 2427.Dl "ipfw add pipe 1 ip from any to any" 2428.Dl "ipfw pipe 1 config mask all" 2429.Pp 2430The above set of rules will create queues (and collect 2431statistics) for all traffic. 2432Because the pipes have no limitations, the only effect is 2433collecting statistics. 2434Note that we need 3 rules, not just the last one, because 2435when 2436.Nm 2437tries to match IP packets it will not consider ports, so we 2438would not see connections on separate ports as different 2439ones. 2440.Pp 2441A more sophisticated example is limiting the outbound traffic 2442on a net with per-host limits, rather than per-network limits: 2443.Pp 2444.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 2445.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in" 2446.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 2447.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 2448.Ss LOOKUP TABLES 2449In the following example, we need to create several traffic bandwidth 2450classes and we need different hosts/networks to fall into different classes. 2451We create one pipe for each class and configure them accordingly. 2452Then we create a single table and fill it with IP subnets and addresses. 2453For each subnet/host we set the argument equal to the number of the pipe 2454that it should use. 2455Then we classify traffic using a single rule: 2456.Pp 2457.Dl "ipfw pipe 1 config bw 1000Kbyte/s" 2458.Dl "ipfw pipe 4 config bw 4000Kbyte/s" 2459.Dl "..." 2460.Dl "ipfw table 1 add 192.168.2.0/24 1" 2461.Dl "ipfw table 1 add 192.168.0.0/27 4" 2462.Dl "ipfw table 1 add 192.168.0.2 1" 2463.Dl "..." 2464.Dl "ipfw pipe tablearg ip from table(1) to any" 2465.Pp 2466Using the 2467.Cm fwd 2468action, the table entries may include hostnames and IP addresses. 2469.Pp 2470.Dl "ipfw table 1 add 192.168.2.0/24 10.23.2.1" 2471.Dl "ipfw table 1 add 192.168.0.0/27 router1.dmz" 2472.Dl "..." 2473.Dl "ipfw add 100 fwd tablearg ip from any to table(1)" 2474.Ss SETS OF RULES 2475To add a set of rules atomically, e.g.\& set 18: 2476.Pp 2477.Dl "ipfw set disable 18" 2478.Dl "ipfw add NN set 18 ... # repeat as needed" 2479.Dl "ipfw set enable 18" 2480.Pp 2481To delete a set of rules atomically the command is simply: 2482.Pp 2483.Dl "ipfw delete set 18" 2484.Pp 2485To test a ruleset and disable it and regain control if something goes wrong: 2486.Pp 2487.Dl "ipfw set disable 18" 2488.Dl "ipfw add NN set 18 ... # repeat as needed" 2489.Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18" 2490.Pp 2491Here if everything goes well, you press control-C before the "sleep" 2492terminates, and your ruleset will be left active. 2493Otherwise, e.g.\& if 2494you cannot access your box, the ruleset will be disabled after 2495the sleep terminates thus restoring the previous situation. 2496.Ss NAT, REDIRECT AND LSNAT 2497First redirect all the traffic to nat instance 123: 2498.Pp 2499.Dl "ipfw add nat 123 all from any to any" 2500.Pp 2501Then to configure nat instance 123 to alias all the outgoing traffic with ip 2502192.168.0.123, blocking all incoming connections, trying to keep 2503same ports on both sides, clearing aliasing table on address change 2504and keeping a log of traffic/link statistics: 2505.Pp 2506.Dl "ipfw nat 123 config ip 192.168.0.123 log deny_in reset same_ports" 2507.Pp 2508Or to change address of instance 123, aliasing table will be cleared (see 2509reset option): 2510.Pp 2511.Dl "ipfw nat 123 config ip 10.0.0.1" 2512.Pp 2513To see configuration of nat instance 123: 2514.Pp 2515.Dl "ipfw nat 123 show config" 2516.Pp 2517To show logs of all the instances in range 111-999: 2518.Pp 2519.Dl "ipfw nat 111-999 show" 2520.Pp 2521To see configurations of all instances: 2522.Pp 2523.Dl "ipfw nat show config" 2524.Pp 2525Or a redirect rule with mixed modes could looks like: 2526.Pp 2527.Dl "ipfw nat 123 config redirect_addr 10.0.0.1 10.0.0.66" 2528.Dl " redirect_port tcp 192.168.0.1:80 500" 2529.Dl " redirect_proto udp 192.168.1.43 192.168.1.1" 2530.Dl " redirect_addr 192.168.0.10,192.168.0.11" 2531.Dl " 10.0.0.100 # LSNAT" 2532.Dl " redirect_port tcp 192.168.0.1:80,192.168.0.10:22" 2533.Dl " 500 # LSNAT" 2534.Pp 2535or it could be splitted in: 2536.Pp 2537.Dl "ipfw nat 1 config redirect_addr 10.0.0.1 10.0.0.66" 2538.Dl "ipfw nat 2 config redirect_port tcp 192.168.0.1:80 500" 2539.Dl "ipfw nat 3 config redirect_proto udp 192.168.1.43 192.168.1.1" 2540.Dl "ipfw nat 4 config redirect_addr 192.168.0.10,192.168.0.11,192.168.0.12" 2541.Dl " 10.0.0.100" 2542.Dl "ipfw nat 5 config redirect_port tcp" 2543.Dl " 192.168.0.1:80,192.168.0.10:22,192.168.0.20:25 500" 2544.Pp 2545.Sh SEE ALSO 2546.Xr cpp 1 , 2547.Xr m4 1 , 2548.Xr altq 4 , 2549.Xr divert 4 , 2550.Xr dummynet 4 , 2551.Xr if_bridge 4 , 2552.Xr ip 4 , 2553.Xr ipfirewall 4 , 2554.Xr ng_ipfw 4 , 2555.Xr protocols 5 , 2556.Xr services 5 , 2557.Xr init 8 , 2558.Xr kldload 8 , 2559.Xr reboot 8 , 2560.Xr sysctl 8 , 2561.Xr syslogd 8 2562.Sh HISTORY 2563The 2564.Nm 2565utility first appeared in 2566.Fx 2.0 . 2567.Xr dummynet 4 2568was introduced in 2569.Fx 2.2.8 . 2570Stateful extensions were introduced in 2571.Fx 4.0 . 2572.Nm ipfw2 2573was introduced in Summer 2002. 2574.Sh AUTHORS 2575.An Ugen J. S. Antsilevich , 2576.An Poul-Henning Kamp , 2577.An Alex Nash , 2578.An Archie Cobbs , 2579.An Luigi Rizzo . 2580.Pp 2581.An -nosplit 2582API based upon code written by 2583.An Daniel Boulet 2584for BSDI. 2585.Pp 2586.An -nosplit 2587In-kernel NAT support written by 2588.An Paolo Pisati Aq piso@FreeBSD.org 2589as part of a Summer of Code 2005 project. 2590.Pp 2591Work on 2592.Xr dummynet 4 2593traffic shaper supported by Akamba Corp. 2594.Sh BUGS 2595Use of dummynet with IPv6 requires that debug.mpsafenet be set to 0. 2596.Pp 2597The syntax has grown over the years and sometimes it might be confusing. 2598Unfortunately, backward compatibility prevents cleaning up mistakes 2599made in the definition of the syntax. 2600.Pp 2601.Em !!! WARNING !!! 2602.Pp 2603Misconfiguring the firewall can put your computer in an unusable state, 2604possibly shutting down network services and requiring console access to 2605regain control of it. 2606.Pp 2607Incoming packet fragments diverted by 2608.Cm divert 2609are reassembled before delivery to the socket. 2610The action used on those packet is the one from the 2611rule which matches the first fragment of the packet. 2612.Pp 2613Packets diverted to userland, and then reinserted by a userland process 2614may lose various packet attributes. 2615The packet source interface name 2616will be preserved if it is shorter than 8 bytes and the userland process 2617saves and reuses the sockaddr_in 2618(as does 2619.Xr natd 8 ) ; 2620otherwise, it may be lost. 2621If a packet is reinserted in this manner, later rules may be incorrectly 2622applied, making the order of 2623.Cm divert 2624rules in the rule sequence very important. 2625.Pp 2626Dummynet drops all packets with IPv6 link-local addresses. 2627.Pp 2628Rules using 2629.Cm uid 2630or 2631.Cm gid 2632may not behave as expected. 2633In particular, incoming SYN packets may 2634have no uid or gid associated with them since they do not yet belong 2635to a TCP connection, and the uid/gid associated with a packet may not 2636be as expected if the associated process calls 2637.Xr setuid 2 2638or similar system calls. 2639.Pp 2640Rules which use uid, gid or jail based matching should be used only 2641if debug.mpsafenet=0 to avoid possible deadlocks due to layering 2642violations in its implementation. 2643.Pp 2644Rule syntax is subject to the command line environment and some patterns 2645may need to be escaped with the backslash character 2646or quoted appropriately. 2647.Pp 2648Due to the architecture of 2649.Xr libalias 3 , 2650ipfw nat is not compatible with the tcp segmentation offloading 2651(TSO). Thus, to reliably nat your network traffic, please disable TSO 2652on your NICs using 2653.Xr ifconfig 8 . 2654