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