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