1.\" 2.\" $FreeBSD$ 3.\" 4.de NOIPFW 5.br 6(\\$1 NOT IN IPFW) 7.br 8.. 9.Dd August 13, 2002 10.Dt IPFW 8 11.Os 12.Sh NAME 13.Nm ipfw 14.Nd IP firewall and traffic shaper control program 15.Sh SYNOPSIS 16.Nm 17.Op Fl q 18.Cm add 19.Ar rule 20.Nm 21.Op Fl adeftNS 22.Brq Cm list | show 23.Op Ar number ... 24.Nm 25.Op Fl f | q 26.Cm flush 27.Nm 28.Op Fl q 29.Brq Cm delete | zero | resetlog 30.Op Cm set 31.Op Ar number ... 32.Pp 33.Nm 34.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ... 35.Nm 36.Cm set move 37.Op Cm rule 38.Ar number Cm to Ar number 39.Nm 40.Cm set swap Ar number number 41.Nm 42.Cm set show 43.Pp 44.Nm 45.Brq Cm pipe | queue 46.Ar number 47.Cm config 48.Ar config-options 49.Nm 50.Op Fl s Op Ar field 51.Brq Cm pipe | queue 52.Brq Cm delete | list | show 53.Op Ar number ... 54.Pp 55.Nm 56.Op Fl q 57.Oo 58.Fl p Ar preproc 59.Oo Fl D 60.Ar macro Ns Op = Ns Ar value 61.Oc 62.Op Fl U Ar macro 63.Oc 64.Ar pathname 65.Sh DESCRIPTION 66The 67.Nm 68utility is the user interface for controlling the 69.Xr ipfw 4 70firewall and the 71.Xr dummynet 4 72traffic shaper in 73.Fx . 74.Pp 75.Em NOTE: 76this manual page refers to the newer version of 77.Nm 78introduced in July 2002, also known as 79.Nm ipfw2 . 80The commands listed here are a superset of the old 81firewall, which we will call 82.Nm ipfw1 83when it is necessary to distinguish between the two. 84.Pp 85.Nm ipfw2 86is standard in 87.Fx 88CURRENT, whereas 89.Fx 90STABLE still uses 91.Nm ipfw1 92unless the kernel is compiled with 93.Cm options IPFW2 , 94and 95.Nm /sbin/ipfw 96and 97.Nm /usr/lib/libalias 98are recompiled with 99.Cm -DIPFW2 100and reinstalled (the same effect can be achieved by adding 101.Cm IPFW2=TRUE 102to 103.Nm /etc/make.conf 104before a buildworld). 105.Pp 106See the 107.Sx IPFW2 ENHANCEMENTS 108Section for a list of features which are not present in 109.Nm ipfw1 . 110This list can also be useful to revise your rules and 111write them more efficiently. 112.Pp 113An 114.Nm 115configuration, or 116.Em ruleset , 117is made of a list of 118.Em rules 119numbered from 1 to 65535. 120Packets are passed to 121.Nm 122in a number of different places in the protocol stack 123(depending on the source and destination of the packet, 124it is possible that 125.Nm 126is invoked multiple times on the same packet). 127The packet passed to the firewall is compared 128against each of the rules in the firewall 129.Em ruleset . 130When a match is found, the action corresponding to the 131matching rule is performed. 132.Pp 133Depending on the action and certain system settings, packets 134can be reinjected into the firewall at some rule after the 135matching one for further processing. 136.Pp 137An 138.Nm 139ruleset always includes a 140.Em default 141rule (numbered 65535) which cannot be modified, 142and matches all packets. 143The action associated with the 144.Em default 145rule can be either 146.Cm deny 147or 148.Cm allow 149depending on how the kernel is configured. 150.Pp 151If the ruleset includes one or more rules with the 152.Cm keep-state 153or 154.Cm limit 155option, then 156.Nm 157assumes a 158.Em stateful 159behaviour, i.e. upon a match it will create dynamic rules matching 160the exact parameters (addresses and ports) of the matching packet. 161.Pp 162These dynamic rules, which have a limited lifetime, are checked 163at the first occurrence of a 164.Cm check-state , 165.Cm keep-state 166or 167.Cm limit 168rule, and are typically used to open the firewall on-demand to 169legitimate traffic only. 170See the 171.Sx STATEFUL FIREWALL 172and 173.Sx EXAMPLES 174Sections below for more information on the stateful behaviour of 175.Nm . 176.Pp 177All rules (including dynamic ones) have a few associated counters: 178a packet count, a byte count, a log count and a timestamp 179indicating the time of the last match. 180Counters can be displayed or reset with 181.Nm 182commands. 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 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 202Also, each rule belongs to one of 32 different 203.Em sets 204, and there are 205.Nm 206commands to atomically manipulate sets, such as enable, 207disable, swap sets, move all rules in a set to another 208one, delete all rules in a set. These can be useful to 209install temporary configurations, or to test them. 210See Section 211.Sx SETS OF RULES 212for more information on 213.Em sets . 214.Pp 215The following options are available: 216.Bl -tag -width indent 217.It Fl a 218While listing, show counter values. 219The 220.Cm show 221command just implies this option. 222.It Fl d 223While listing, show dynamic rules in addition to static ones. 224.It Fl e 225While listing, if the 226.Fl d 227option was specified, also show expired dynamic rules. 228.It Fl f 229Don't ask for confirmation for commands that can cause problems 230if misused, 231.No i.e. Cm flush . 232If there is no tty associated with the process, this is implied. 233.It Fl N 234Try to resolve addresses and service names in output. 235.It Fl q 236While 237.Cm add Ns ing , 238.Cm zero Ns ing , 239.Cm resetlog Ns ging 240or 241.Cm flush Ns ing , 242be quiet about actions 243(implies 244.Fl f ) . 245This is useful for adjusting rules by executing multiple 246.Nm 247commands in a script 248(e.g., 249.Ql sh\ /etc/rc.firewall ) , 250or by processing a file of many 251.Nm 252rules, 253across a remote login session. 254If a 255.Cm flush 256is performed in normal (verbose) mode (with the default kernel 257configuration), it prints a message. 258Because all rules are flushed, the message might not be delivered 259to the login session, causing the remote login session to be closed 260and the remainder of the ruleset is not processed. 261Access to the console would then be required to recover. 262.It Fl S 263While listing rules, show the 264.Em set 265each rule belongs to. 266If this flag is not specified, disabled rules will not be 267listed. 268.It Fl s Op Ar field 269While listing pipes, sort according to one of the four 270counters (total and current packets or bytes). 271.It Fl t 272While listing, show last match timestamp. 273.El 274.Pp 275To ease configuration, rules can be put into a file which is 276processed using 277.Nm 278as shown in the first synopsis line. 279An absolute 280.Ar pathname 281must be used. 282The file will be read line by line and applied as arguments to the 283.Nm 284utility. 285.Pp 286Optionally, a preprocessor can be specified using 287.Fl p Ar preproc 288where 289.Ar pathname 290is to be piped through. 291Useful preprocessors include 292.Xr cpp 1 293and 294.Xr m4 1 . 295If 296.Ar preproc 297doesn't start with a slash 298.Pq Ql / 299as its first character, the usual 300.Ev PATH 301name search is performed. 302Care should be taken with this in environments where not all 303filesystems are mounted (yet) by the time 304.Nm 305is being run (e.g. when they are mounted over NFS). 306Once 307.Fl p 308has been specified, optional 309.Fl D 310and 311.Fl U 312specifications can follow and will be passed on to the preprocessor. 313This allows for flexible configuration files (like conditionalizing 314them on the local hostname) and the use of macros to centralize 315frequently required arguments like IP addresses. 316.Pp 317The 318.Nm 319.Cm pipe 320and 321.Cm queue 322commands are used to configure the traffic shaper, as shown in the 323.Sx TRAFFIC SHAPER CONFIGURATION 324Section below. 325.Sh PACKET FLOW 326.Nm 327can be invoked from multiple places in the protocol stack, 328under control of several system parameters, 329and it is important to understand when this occurs in order to 330design a proper ruleset. The places where 331.Nm 332is invoked are listed below, together with the sysctl variables 333which control its invocation. 334.Bd -literal -offset indent 335 ^ to upper layers V 336 | | 337 +----------->-----------+ 338 ^ V 339 [ip_input] [ip_output] net.inet.ip.fw.enable=1 340 | | 341 ^ V 342[ether_demux] [ether_output_frame] net.link.ether.ipfw=1 343 | | 344 +-->--[bdg_forward]-->--+ net.link.ether.bridge_ipfw=1 345 ^ V 346 | to devices | 347.Ed 348.Pp 349As can be noted from the above picture, the number of 350times the same packet goes through the firewall can 351vary between 0 and 4 depending o packet source and 352destination, and system configuration. 353In each of these places, the packet is passed to 354.Nm 355with all (and only) the fields that belong to that level. 356That is, incoming packets will include the MAC header when 357.Nm 358is invoked from 359.Cm ether_demux() , 360but the same packets will have the MAC header stripped off when 361.Nm 362is invoked from 363.Cm ip_input() . 364.br 365The complete ruleset is always used, 366irrespective of the place where 367.Nm 368is invoked, or the source of the packet. 369If a rule contains some match patterns or actions which are not valid 370for the place of invokation (e.g. trying 371to match a MAC header when 372.Nm 373is called from 374.Cm ip_input() 375) the rule will simply not match. It is thus responsibility of 376the programmer, if necessary, to write a suitable ruleset to 377differentiate among the possible places. 378.Cm skipto 379rules can be useful here, as an example: 380.Bd -literal -offset indent 381# packets from ether_demux or bdg_forward 382ipfw add 10 skipto 1000 all from any to any layer2 in 383# packets from ip_input 384ipfw add 10 skipto 2000 all from any to any not layer2 in 385# packets from ip_output 386ipfw add 10 skipto 3000 all from any to any not layer2 out 387# packets from ether_output_frame 388ipfw add 10 skipto 4000 all from any to any layer2 out 389.Ed 390.Pp 391(yes, at the moment there is no way to differentiate between 392ether_demux and bdg_forward). 393.Sh RULE FORMAT 394The format of 395.Nm 396rules is the following: 397.Bd -ragged -offset indent 398.Op Ar rule_number 399.Op Cm set Ar set_number 400.Op Cm prob Ar match_probability 401.br 402.Ar " " action 403.Op Cm log Op Cm logamount Ar number 404.Ar body 405.Ed 406.Pp 407where the body of the rule specifies which information is used 408for filtering packets, among the following: 409.Pp 410.Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact 411.It Layer-2 header fields 412When available 413.It IPv4 Protocol 414TCP, UDP, ICMP, etc. 415.It Source and dest. addresses and ports 416.It Direction 417See Section 418.Sx PACKET FLOW 419.It Transmit and receive interface 420By name or address 421.It Misc. IP header fields 422Version, type of service, datagram length, identification, 423fragment flag (non-zero IP offset), 424Time To Live 425.It IP options 426.It Misc. TCP header fields 427TCP flags (SYN, FIN, ACK, RST, etc.), 428sequence number, acknowledgment number, 429window 430.It TCP options 431.It ICMP types 432for ICMP packets 433.It User/group ID 434When the packet can be associate to a local socket. 435.El 436.Pp 437Note that some of the above information, e.g. source MAC or IP addresses and 438TCP/UDP ports, could easily be spoofed, so filtering on those fields 439alone might not guarantee the desired results. 440.Bl -tag -width indent 441.It Ar rule_number 442Each rule is associated with a 443.Ar rule_number 444in the range 1..65535, with the latter reserved for the 445.Em default 446rule. 447Rules are checked sequentially by rule number. 448Multiple rules can have the same number, in which case they are 449checked (and listed) according to the order in which they have 450been added. 451If a rule is entered without specifying a number, the kernel will 452assign one in such a way that the rule becomes the last one 453before the 454.Em default 455rule. 456Automatic rule numbers are assigned by incrementing the last 457non-default rule number by the value of the sysctl variable 458.Ar net.inet.ip.fw.autoinc_step 459which defaults to 100. 460If this is not possible (e.g. because we would go beyond the 461maximum allowed rule number), the same number of the last 462non-default value is used instead. 463.It Cm set Ar set_number 464Each rule is associated to a 465.Ar set_number 466in the range 0..31, with the latter reserved for the 467.Em default 468rule. 469Sets can be individually disabled and enabled, so this parameter 470is of fundamental importance for atomic ruleset manipulation. 471It can be also used to simplify deletion of groups of rules. 472If a rule is entered without specifying a set number, 473set 0 will be used. 474.It Cm prob Ar match_probability 475A match is only declared with the specified probability 476(floating point number between 0 and 1). 477This can be useful for a number of applications such as 478random packet drop or 479(in conjunction with 480.Xr dummynet 4 ) 481to simulate the effect of multiple paths leading to out-of-order 482packet delivery. 483.It Cm log Op Cm logamount Ar number 484When a packet matches a rule with the 485.Cm log 486keyword, a message will be 487logged to 488.Xr syslogd 8 489with a 490.Dv LOG_SECURITY 491facility. 492The logging only occurs if the sysctl variable 493.Em net.inet.ip.fw.verbose 494is set to 1 495(which is the default when the kernel is compiled with 496.Dv IPFIREWALL_VERBOSE 497) and the number of packets logged so far for that 498particular rule does not exceed ther 499.Cm logamount 500parameter. 501If no 502.Cm logamount 503is specified, the limit is taken from the sysctl variable 504.Em net.inet.ip.fw.verbose_limit . 505In both cases, a value of 0 506removes the logging limit. 507.Pp 508Once the limit is reached, logging can be re-enabled by 509clearing the logging counter 510or the packet counter for that entry, see the 511.Cm resetlog 512command. 513.Pp 514.El 515.Ss RULE ACTIONS 516A rule can be associated with one of the following actions, which 517will be executed when the packet matches the body of the rule. 518.Bl -tag -width indent 519.It Cm allow | accept | pass | permit 520Allow packets that match rule. 521The search terminates. 522.It Cm check-state 523Checks the packet against the dynamic ruleset. 524If a match is found, execute the action associated with 525the rule which generated this dynamic rule, otherwise 526move to the next rule. 527.br 528.Cm Check-state 529rules do not have a body. 530If no 531.Cm check-state 532rule is found, the dynamic ruleset is checked at the first 533.Cm keep-state 534or 535.Cm limit 536rule. 537.It Cm count 538Update counters for all packets that match rule. 539The search continues with the next rule. 540.It Cm deny | drop 541Discard packets that match this rule. 542The search terminates. 543.It Cm divert Ar port 544Divert packets that match this rule to the 545.Xr divert 4 546socket bound to port 547.Ar port . 548The search terminates. 549.It Cm fwd | forward Ar ipaddr Ns Op , Ns Ar port 550Change the next-hop on matching packets to 551.Ar ipaddr , 552which can be an IP address in dotted quad or a host name. 553The search terminates if this rule matches. 554.Pp 555If 556.Ar ipaddr 557is a local address, then matching packets will be forwarded to 558.Ar port 559(or the port number in the packet if one is not specified in the rule) 560on the local machine. 561.br 562If 563.Ar ipaddr 564is not a local address, then the port number 565(if specified) is ignored, and the packet will be 566forwarded to the remote address, using the route as found in 567the local routing table for that IP. 568.br 569A 570.Ar fwd 571rule will not match layer-2 packets (those received 572on ether_input, ether_output, or bridged). 573.br 574The 575.Cm fwd 576action does not change the contents of the packet at all. 577In particular, the destination address remains unmodified, so 578packets forwarded to another system will usually be rejected by that system 579unless there is a matching rule on that system to capture them. 580For packets forwarded locally, 581the local address of the socket will be 582set to the original destination address of the packet. 583This makes the 584.Xr netstat 1 585entry look rather weird but is intended for 586use with transparent proxy servers. 587.It Cm pipe Ar pipe_nr 588Pass packet to a 589.Xr dummynet 4 590.Dq pipe 591(for bandwidth limitation, delay, etc.). 592See the 593.Sx TRAFFIC SHAPER CONFIGURATION 594Section for further information. 595The search terminates; however, on exit from the pipe and if 596the 597.Xr sysctl 8 598variable 599.Em net.inet.ip.fw.one_pass 600is not set, the packet is passed again to the firewall code 601starting from the next rule. 602.It Cm queue Ar queue_nr 603Pass packet to a 604.Xr dummynet 4 605.Dq queue 606(for bandwidth limitation using WF2Q). 607.It Cm reject 608(Deprecated). 609Synonym for 610.Cm unreach host . 611.It Cm reset 612Discard packets that match this rule, and if the 613packet is a TCP packet, try to send a TCP reset (RST) notice. 614The search terminates. 615.It Cm skipto Ar number 616Skip all subsequent rules numbered less than 617.Ar number . 618The search continues with the first rule numbered 619.Ar number 620or higher. 621.It Cm tee Ar port 622Send a copy of packets matching this rule to the 623.Xr divert 4 624socket bound to port 625.Ar port . 626The search terminates and the original packet is accepted 627(but see Section 628.Sx BUGS 629below). 630.It Cm unreach Ar code 631Discard packets that match this rule, and try to send an ICMP 632unreachable notice with code 633.Ar code , 634where 635.Ar code 636is a number from 0 to 255, or one of these aliases: 637.Cm net , host , protocol , port , 638.Cm needfrag , srcfail , net-unknown , host-unknown , 639.Cm isolated , net-prohib , host-prohib , tosnet , 640.Cm toshost , filter-prohib , host-precedence 641or 642.Cm precedence-cutoff . 643The search terminates. 644.El 645.Ss RULE BODY 646The body of a rule contains zero or more patterns (such as 647specific source and destination addresses or ports, 648protocol options, incoming or outgoing interfaces, etc.) 649that the packet must match in order to be recognised. 650In general, the patterns are connected by (implicit) 651.Em and 652connectives -- i.e. all must match in order for the 653rule to match. 654Individual patterns can be prefixed by the 655.Em not 656keyword to reverse the result of the match, as in 657.Pp 658.Dl "ipfw add 100 allow ip from not 1.2.3.4 to any" 659.Pp 660Additionally, sets of alternative match patterns ( 661.Em or-blocks 662) can be constructed by putting the patterns in 663lists enclosed between parentheses ( ) or braces { }, and 664using 665.Cm or 666connectives as follows: 667.Pp 668.Dl "ipfw add 100 allow ip from { x or not y or z } to any" 669.Pp 670Only one level of parentheses is allowed. 671Beware that most shells have special meanings for parentheses 672or braces, so it is advisable to put a \\ in front of them. 673.Pp 674The body of a rule must in general comprise a source and destination 675addres specifier. 676The keyword 677.Ar any 678can be used in various places to specify that the content of 679a required field is irrelevant. 680.Pp 681The general rule body format is one of the following: 682.Bd -ragged -offset indent 683.Ar proto 684.Cm from Ar src 685.Cm to Ar dst 686.Op Ar options 687.br 688.Cm MAC Ar dst-mac src-mac mac-type 689.Op Cm from Ar src Cm to Ar dst 690.Op Ar options 691.Ed 692.Pp 693where the second format allows you to specify MAC header fields 694instead (or in addition) of the IPv4 header fields. 695.Pp 696Rule fields have the following meaning: 697.Bl -tag -width indent 698.It Ar proto 699An IPv4 protocol specified by number or name (for a complete 700list see 701.Pa /etc/protocols ) . 702The 703.Cm ip 704or 705.Cm all 706keywords mean any protocol will match. 707.It Ar src No and Ar dst : 708A single 709.Ar ip address 710, or an 711.Em or-block 712containing one or more of them, 713optionally followed by 714.Em port numbers. 715.It Ar ip address : 716An address (or set of addresses) specified in one of the following 717ways, optionally preceded by a 718.Cm not 719operator: 720.Bl -tag -width indent 721.It Cm any 722matches any IP address. 723.It Cm me 724matches any IP address configured on an interface in the system. 725The address list is evaluated at the time the packet is 726analysed. 727.It Ar numeric-ip | hostname 728Matches a single IPv4 address, specified as dotted-quad or a hostname. 729Hostnames are resolved at the time the rule is added to the firewall list. 730.It Ar addr Ns / Ns Ar masklen 731Matches all addresses with base 732.Ar addr 733(specified as a dotted quad or a hostname) 734and mask width of 735.Cm masklen 736bits. 737As an example, 1.2.3.4/25 will match 738all IP numbers from 1.2.3.0 to 1.2.3.127 . 739.It Ar addr Ns / Ns Ar masklen Ns Cm { Ns Ar num,num,... Ns Cm } 740Matches all addresses with base address 741.Ar addr 742(specified as a dotted quad or a hostname) 743and whose last byte is in the list between braces { } . 744Note that there must be no spaces between braces, commas and 745numbers. 746The 747.Ar masklen 748field is used to limit the size of the set of addresses, 749and can have any value between 24 and 32. 750.br 751As an example, an address specified as 1.2.3.4/24{128,35,55,89} 752will match the following IP addresses: 753.br 7541.2.3.128 1.2.3.35 1.2.3.55 1.2.3.89 . 755.br 756This format is particularly useful to handle sparse address sets 757within a single rule. Because the matching occurs using a 758bitmask, it takes constant time and dramatically reduces 759the complexity of rulesets. 760.El 761.It port numbers 762With protocols which support port numbers (such as TCP and UDP), optional 763.Cm ports 764may be specified as one or more ports or port ranges, separated 765by commas but no spaces, and an optional 766.Cm not 767operator: 768.Bd -ragged -offset indent 769.Op Cm not 770.Brq Ar port | port Ns \&- Ns Ar port Ns 771.Op , Ns Ar ... 772.Ed 773.Pp 774The 775.Ql \&- 776notation specifies a range of ports (including boundaries). 777.Pp 778Service names (from 779.Pa /etc/services ) 780may be used instead of numeric port values. 781The length of the port list is limited to 14 ports or ranges, 782though you can also use port ranges within an 783.Em or-block 784to build essentially unlimited lists: 785.Pp 786.Dl "ipfw add allow tcp from any { 1-20,30-50 or 500-600 } to any" 787.Pp 788.Pp 789A backslash 790.Pq Ql \e 791can be used to escape the dash 792.Pq Ql - 793character in a service name: 794.Pp 795.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any" 796.Pp 797Fragmented packets which have a non-zero offset (i.e. not the first 798fragment) will never match a rule which has one or more port 799specifications. 800See the 801.Cm frag 802option for details on matching fragmented packets. 803.It dst-mac, src-mac 804Destination and source MAC addresses, specified as 805groups of hex digits separated by commas, and optionally 806followed by a mask indicating how many bits are significant: 807.Pp 808.Dl "ipfw add allow MAC 10:20:30:40:50:60/30 any any 809.Pp 810Note that the order of MAC addresses (destination first, 811source second) is 812the same as on the wire, but the opposite of the one used for 813IP addresses. 814.It mac-type 815The value of the Ethernet Type field, specified in the same way as 816.Cm port numbers 817(i.e. one or more comma-separated single values or ranges). 818You can use symbolic names for known values such as 819.Em vlan , ipv4, ipv6 . 820The values can be enter as decimal or hexadecimal, but they 821are always printed as hexadecimal (unless the 822.Cm -N 823option is used, in which case symbolic resolution will be 824attempted). 825.El 826.Ss RULE OPTIONS 827Additional match patterns can be used within 828rules. Zero or more of these so-called 829.Em options 830can be present in a rule, optionally prefixed by the 831.Cm not 832operand, and possibly grouped into 833.Em or-blocks . 834.Pp 835Note that there is an ambiguity in the syntax: in a rule of 836the form 837.Pp 838.Dl "ipfw add allow ip from any to any { in or layer2 }" 839.Pp 840the or-block could contain either port lists or options. 841To remove the ambiguity, one should specify a destination 842port, which can be done by either using the keyword 843.Cm any 844or an empty or-block 845.Cm { } 846e.g.: 847.Pp 848.Dl "ipfw add allow ip from any to any any { in or layer2 }" 849.Pp 850The following options are available: 851.Bl -tag -width indent 852.It Cm bridged 853Matches only bridged packets. 854.It Cm established 855TCP packets only. 856Match packets that have the RST or ACK bits set. 857.It Cm frag 858Match if the packet is a fragment and this is not the first 859fragment of the datagram. 860.Cm frag 861may not be used in conjunction with either 862.Cm tcpflags 863or TCP/UDP port specifications. 864.It Cm gid Ar group 865Match all TCP or UDP packets sent by or received for a 866.Ar group . 867A 868.Ar group 869may be matched by name or identification number. 870.It Cm icmptypes Ar types 871ICMP packets only. 872Match if the ICMP type is in the list 873.Ar types . 874The list may be specified as any combination of ranges or 875individual types separated by commas. 876The supported ICMP types are: 877.Pp 878echo reply 879.Pq Cm 0 , 880destination unreachable 881.Pq Cm 3 , 882source quench 883.Pq Cm 4 , 884redirect 885.Pq Cm 5 , 886echo request 887.Pq Cm 8 , 888router advertisement 889.Pq Cm 9 , 890router solicitation 891.Pq Cm 10 , 892time-to-live exceeded 893.Pq Cm 11 , 894IP header bad 895.Pq Cm 12 , 896timestamp request 897.Pq Cm 13 , 898timestamp reply 899.Pq Cm 14 , 900information request 901.Pq Cm 15 , 902information reply 903.Pq Cm 16 , 904address mask request 905.Pq Cm 17 906and address mask reply 907.Pq Cm 18 . 908.It Cm in | out 909Only match incoming or outgoing packets, respectively. 910.Cm in 911and 912.Cm out 913are mutually exclusive (in fact, 914.Cm out 915is implemented as 916.Cm not in 917). 918.It Cm ipid Ar id 919Match if the identification of IP datagram is 920.Ar id . 921.It Cm iplen Ar len 922Match if the total length of a packet, including header and data, is 923.Ar len 924bytes. 925.It Cm ipoptions Ar spec 926Match if the IP header contains the comma separated list of 927options specified in 928.Ar spec . 929The supported IP options are: 930.Pp 931.Cm ssrr 932(strict source route), 933.Cm lsrr 934(loose source route), 935.Cm rr 936(record packet route) and 937.Cm ts 938(timestamp). 939The absence of a particular option may be denoted 940with a 941.Ql \&! . 942.It Cm ipprecedence Ar precedence 943Match if the numeric value of IP datagram's precedence is equal to 944.Ar precedence . 945.It Cm iptos Ar spec 946Match if the IP header contains the comma separated list of 947service types specified in 948.Ar spec . 949The supported IP types of service are: 950.Pp 951.Cm lowdelay 952.Pq Dv IPTOS_LOWDELAY , 953.Cm throughput 954.Pq Dv IPTOS_THROUGHPUT , 955.Cm reliability 956.Pq Dv IPTOS_RELIABILITY , 957.Cm mincost 958.Pq Dv IPTOS_MINCOST , 959.Cm congestion 960.Pq Dv IPTOS_CE . 961The absence of a particular type may be denoted 962with a 963.Ql \&! . 964.It Cm ipttl Ar ttl 965Match if the time to live of IP datagram is 966.Ar ttl . 967.It Cm ipversion Ar ver 968Match if the IP header version is 969.Ar ver . 970.It Cm keep-state 971Upon a match, the firewall will create a dynamic rule, whose 972default behaviour is to matching bidirectional traffic between 973source and destination IP/port using the same protocol. 974The rule has a limited lifetime (controlled by a set of 975.Xr sysctl 8 976variables), and the lifetime is refreshed every time a matching 977packet is found. 978.It Cm layer2 979Matches only layer2 packets, i.e. those passed to 980.Nm 981from ether_demux() and ether_output_frame(). 982.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N 983The firewall will only allow 984.Ar N 985connections with the same 986set of parameters as specified in the rule. 987One or more 988of source and destination addresses and ports can be 989specified. 990.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any 991Packet must be received, transmitted or be going through, 992respectively, the interface specified by exact name ( 993.Ar ifX 994), by device name ( 995.Ar if Ns Cm * 996), by IP address, or through some interface. 997.Pp 998The 999.Cm via 1000keyword causes the interface to always be checked. 1001If 1002.Cm recv 1003or 1004.Cm xmit 1005is used instead of 1006.Cm via , 1007then only the receive or transmit interface (respectively) 1008is checked. 1009By specifying both, it is possible to match packets based on 1010both receive and transmit interface, e.g.: 1011.Pp 1012.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1" 1013.Pp 1014The 1015.Cm recv 1016interface can be tested on either incoming or outgoing packets, 1017while the 1018.Cm xmit 1019interface can only be tested on outgoing packets. 1020So 1021.Cm out 1022is required (and 1023.Cm in 1024is invalid) whenever 1025.Cm xmit 1026is used. 1027.Pp 1028A packet may not have a receive or transmit interface: packets 1029originating from the local host have no receive interface, 1030while packets destined for the local host have no transmit 1031interface. 1032.It Cm setup 1033TCP packets only. 1034Match packets that have the SYN bit set but no ACK bit. 1035This is the short form of 1036.Dq Li tcpflags\ syn,!ack . 1037.It Cm tcpack Ar ack 1038TCP packets only. 1039Match if the TCP header acknowledgment number field is set to 1040.Ar ack . 1041.It Cm tcpflags Ar spec 1042TCP packets only. 1043Match if the TCP header contains the comma separated list of 1044flags specified in 1045.Ar spec . 1046The supported TCP flags are: 1047.Pp 1048.Cm fin , 1049.Cm syn , 1050.Cm rst , 1051.Cm psh , 1052.Cm ack 1053and 1054.Cm urg . 1055The absence of a particular flag may be denoted 1056with a 1057.Ql \&! . 1058A rule which contains a 1059.Cm tcpflags 1060specification can never match a fragmented packet which has 1061a non-zero offset. 1062See the 1063.Cm frag 1064option for details on matching fragmented packets. 1065.It Cm tcpseq Ar seq 1066TCP packets only. 1067Match if the TCP header sequence number field is set to 1068.Ar seq . 1069.It Cm tcpwin Ar win 1070TCP packets only. 1071Match if the TCP header window field is set to 1072.Ar win . 1073.It Cm tcpoptions Ar spec 1074TCP packets only. 1075Match if the TCP header contains the comma separated list of 1076options specified in 1077.Ar spec . 1078The supported TCP options are: 1079.Pp 1080.Cm mss 1081(maximum segment size), 1082.Cm window 1083(tcp window advertisement), 1084.Cm sack 1085(selective ack), 1086.Cm ts 1087(rfc1323 timestamp) and 1088.Cm cc 1089(rfc1644 t/tcp connection count). 1090The absence of a particular option may be denoted 1091with a 1092.Ql \&! . 1093.It Cm uid Ar user 1094Match all TCP or UDP packets sent by or received for a 1095.Ar user . 1096A 1097.Ar user 1098may be matched by name or identification number. 1099.El 1100.Sh SETS OF RULES 1101Each rule belongs to one of 32 different 1102.Em sets 1103, numbered 0 to 31. 1104Set 31 is reserved for the default rule. 1105.Pp 1106By default, rules are put in set 0, unless you use the 1107.Cm set N 1108attribute when entering a new rule. 1109Sets can be individually and atomically enabled or disabled, 1110so this mechanism permits an easy way to store multiple configurations 1111of the firewall and quickly (and atomically) switch between them. 1112The command to enable/disable sets is 1113.Pp 1114.Nm 1115.Cm set disable Ar number ... Op Cm enable Ar number ... 1116.Pp 1117where multiple 1118.Cm enable 1119or 1120.Cm disable 1121sections can be specified. 1122Command execution is atomic on all the sets specified in the command. 1123By default, all sets are enabled. 1124.Pp 1125When you disable a set, its rules behave as if they were not existing 1126in the firewall configuration, with only one exception: 1127.Bl -bullet 1128.It 1129dynamic rules created from a rule before it had been disabled 1130will still be active until they expire. In order to delete 1131dynamic rules you have to explicitly delete the parent rule 1132which generated them; 1133.El 1134The set number of rules can be changed with the command 1135.Pp 1136.Nm 1137.Cm set move 1138.Brq Cm rule Ar rule-number | old-set 1139.Cm to Ar new-set 1140.Pp 1141Also, you can atomically swap two rulesets with the command 1142.Pp 1143.Nm 1144.Cm set swap Ar first-set second-set 1145.Pp 1146See the 1147.Sx EXAMPLES 1148Section on some possible uses of sets of rules. 1149.Sh STATEFUL FIREWALL 1150Stateful operation is a way for the firewall to dynamically 1151create rules for specific flows when packets that 1152match a given pattern are detected. Support for stateful 1153operation comes through the 1154.Cm check-state , keep-state 1155and 1156.Cm limit 1157options of 1158.Nm rules. 1159.Pp 1160Dynamic rules are created when a packet matches a 1161.Cm keep-state 1162or 1163.Cm limit 1164rule, causing the creation of a 1165.Em dynamic 1166rule which will match all and only packets with 1167a given 1168.Em protocol 1169between a 1170.Em src-ip/src-port dst-ip/dst-port 1171pair of addresses ( 1172.Em src 1173and 1174.Em dst 1175are used here only to denote the initial match addresses, but they 1176are completely equivalent afterwards). 1177Dynamic rules will be checked at the first 1178.Cm check-state, keep-state 1179or 1180.Cm limit 1181occurrence, and the action performed upon a match will be the same 1182as in the parent rule. 1183.Pp 1184Note that no additional attributes other than protocol and IP addresses 1185and ports are checked on dynamic rules. 1186.Pp 1187The typical use of dynamic rules is to keep a closed firewall configuration, 1188but let the first TCP SYN packet from the inside network install a 1189dynamic rule for the flow so that packets belonging to that session 1190will be allowed through the firewall: 1191.Pp 1192.Dl "ipfw add check-state" 1193.Dl "ipfw add allow tcp from my-subnet to any setup" 1194.Dl "ipfw add deny tcp from any to any" 1195.Pp 1196A similar approach can be used for UDP, where an UDP packet coming 1197from the inside will install a dynamic rule to let the response through 1198the firewall: 1199.Pp 1200.Dl "ipfw add check-state" 1201.Dl "ipfw add allow udp from my-subnet to any" 1202.Dl "ipfw add deny udp from any to any" 1203.Pp 1204Dynamic rules expire after some time, which depends on the status 1205of the flow and the setting of some 1206.Cm sysctl 1207variables. 1208See Section 1209.Sx SYSCTL VARIABLES 1210for more details. 1211For TCP sessions, dynamic rules can be instructed to periodically 1212send keepalive packets to refresh the state of the rule when it is 1213about to expire. 1214.Pp 1215See Section 1216.Sx EXAMPLES 1217for more examples on how to use dynamic rules. 1218.Sh TRAFFIC SHAPER CONFIGURATION 1219.Nm 1220is also the user interface for the 1221.Xr dummynet 4 1222traffic shaper. 1223The shaper operates by dividing packets into 1224.Em flows 1225according to a user-specified mask on different fields 1226of the IP header. 1227Packets belonging to the same flow are then passed to two 1228different objects, named 1229.Em pipe 1230or 1231.Em queue . 1232.Pp 1233A 1234.Em pipe 1235emulates a link with given bandwidth, propagation delay, 1236queue size and packet loss rate. 1237Packets transit through the pipe according to its parameters. 1238.Pp 1239A 1240.Em queue 1241is an abstraction used to implement the WF2Q+ (Worst-case Fair Weighted Fair Queueing) policy. 1242The queue associates to each flow a weight and a reference pipe. 1243Then, all flows linked to the same pipe are scheduled at the 1244rate fixed by the pipe according to the WF2Q+ policy. 1245.Pp 1246The 1247.Nm 1248pipe configuration format is the following: 1249.Bd -ragged -offset indent 1250.Cm pipe Ar number Cm config Ar pipe-configuration 1251.Ed 1252.Pp 1253The 1254.Nm 1255queue configuration format is the following: 1256.Bd -ragged -offset indent 1257.Cm queue Ar number Cm config Ar queue-configuration 1258.Ed 1259.Pp 1260The following parameters can be configured for a pipe: 1261.Pp 1262.Bl -tag -width indent -compact 1263.It Cm bw Ar bandwidth | device 1264Bandwidth, measured in 1265.Sm off 1266.Op Cm K | M 1267.Brq Cm bit/s | Byte/s . 1268.Sm on 1269.Pp 1270A value of 0 (default) means unlimited bandwidth. 1271The unit must follow immediately the number, as in 1272.Pp 1273.Dl "ipfw pipe 1 config bw 300Kbit/s" 1274.Pp 1275If a device name is specified instead of a numeric 1276value, then the transmit clock is supplied by the specified 1277device. 1278At the moment only the 1279.Xr tun 4 1280device supports this 1281functionality, for use in conjunction with 1282.Xr ppp 8 . 1283.Pp 1284.It Cm delay Ar ms-delay 1285Propagation delay, measured in milliseconds. 1286The value is rounded to the next multiple of the clock tick 1287(typically 10ms, but it is a good practice to run kernels 1288with 1289.Dq "options HZ=1000" 1290to reduce 1291the granularity to 1ms or less). 1292Default value is 0, meaning no delay. 1293.El 1294.Pp 1295The following parameters can be configured for a queue: 1296.Pp 1297.Bl -tag -width indent -compact 1298.It Cm pipe Ar pipe_nr 1299Connects a queue to the specified pipe. 1300Multiple queues (usually 1301with different weights) can be connected to the same pipe, which 1302specifies the aggregate rate for the set of queues. 1303.Pp 1304.It Cm weight Ar weight 1305Specifies the weight to be used for flows matching this queue. 1306The weight must be in the range 1..100, and defaults to 1. 1307.El 1308.Pp 1309Finally, the following parameters can be configured for both 1310pipes and queues: 1311.Pp 1312.Bl -tag -width indent -compact 1313.Pp 1314.It Cm buckets Ar hash-table-size 1315Specifies the size of the hash table used for storing the 1316various queues. 1317Default value is 64 controlled by the 1318.Xr sysctl 8 1319variable 1320.Em net.inet.ip.dummynet.hash_size , 1321allowed range is 16 to 1024. 1322.Pp 1323.It Cm mask Ar mask-specifier 1324The 1325.Xr dummynet 4 1326lets you to create per-flow queues. 1327A flow identifier is constructed by masking the IP addresses, 1328ports and protocol types as specified in the pipe configuration. 1329Packets with the same identifier after masking fall into the 1330same queue. 1331Available mask specifiers are a combination of the following: 1332.Cm dst-ip Ar mask , 1333.Cm src-ip Ar mask , 1334.Cm dst-port Ar mask , 1335.Cm src-port Ar mask , 1336.Cm proto Ar mask 1337or 1338.Cm all , 1339where the latter means all bits in all fields are significant. 1340When used within a 1341.Ar pipe 1342configuration, each flow is assigned a rate equal 1343to the rate of the pipe. 1344When used within a 1345.Ar queue 1346configuration, each flow is assigned a weight equal to the 1347weight of the queue, and all flows insisting on the same pipe 1348share bandwidth proportionally to their weight. 1349.Pp 1350.It Cm noerror 1351When a packet is dropped by a dummynet queue or pipe, the error 1352is normally reported to the caller routine in the kernel, in the 1353same way as it happens when a device queue fills up. Setting this 1354option reports the packet as successfully delivered, which can be 1355needed for some experimental setups where you want to simulate 1356loss or congestion at a remote router. 1357.Pp 1358.It Cm plr Ar packet-loss-rate 1359Packet loss rate. 1360Argument 1361.Ar packet-loss-rate 1362is a floating-point number between 0 and 1, with 0 meaning no 1363loss, 1 meaning 100% loss. 1364The loss rate is internally represented on 31 bits. 1365.Pp 1366.It Cm queue Brq Ar slots | size Ns Cm Kbytes 1367Queue size, in 1368.Ar slots 1369or 1370.Cm KBytes . 1371Default value is 50 slots, which 1372is the typical queue size for Ethernet devices. 1373Note that for slow speed links you should keep the queue 1374size short or your traffic might be affected by a significant 1375queueing delay. 1376E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit 1377or 20s of queue on a 30Kbit/s pipe. 1378Even worse effect can result if you get packets from an 1379interface with a much larger MTU, e.g. the loopback interface 1380with its 16KB packets. 1381.Pp 1382.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p 1383Make use of the RED (Random Early Detection) queue management algorithm. 1384.Ar w_q 1385and 1386.Ar max_p 1387are floating 1388point numbers between 0 and 1 (0 not included), while 1389.Ar min_th 1390and 1391.Ar max_th 1392are integer numbers specifying thresholds for queue management 1393(thresholds are computed in bytes if the queue has been defined 1394in bytes, in slots otherwise). 1395The 1396.Xr dummynet 4 1397also supports the gentle RED variant (gred). 1398Three 1399.Xr sysctl 8 1400variables can be used to control the RED behaviour: 1401.Bl -tag -width indent 1402.It Em net.inet.ip.dummynet.red_lookup_depth 1403specifies the accuracy in computing the average queue 1404when the link is idle (defaults to 256, must be greater than zero) 1405.It Em net.inet.ip.dummynet.red_avg_pkt_size 1406specifies the expected average packet size (defaults to 512, must be 1407greater than zero) 1408.It Em net.inet.ip.dummynet.red_max_pkt_size 1409specifies the expected maximum packet size, only used when queue 1410thresholds are in bytes (defaults to 1500, must be greater than zero). 1411.El 1412.El 1413.Sh CHECKLIST 1414Here are some important points to consider when designing your 1415rules: 1416.Bl -bullet 1417.It 1418Remember that you filter both packets going 1419.Cm in 1420and 1421.Cm out . 1422Most connections need packets going in both directions. 1423.It 1424Remember to test very carefully. 1425It is a good idea to be near the console when doing this. 1426If you cannot be near the console, 1427use an auto-recovery script such as the one in 1428.Pa /usr/share/examples/ipfw/change_rules.sh . 1429.It 1430Don't forget the loopback interface. 1431.El 1432.Sh FINE POINTS 1433.Bl -bullet 1434.It 1435There are circumstances where fragmented datagrams are unconditionally 1436dropped. 1437TCP packets are dropped if they do not contain at least 20 bytes of 1438TCP header, UDP packets are dropped if they do not contain a full 8 1439byte UDP header, and ICMP packets are dropped if they do not contain 14404 bytes of ICMP header, enough to specify the ICMP type, code, and 1441checksum. 1442These packets are simply logged as 1443.Dq pullup failed 1444since there may not be enough good data in the packet to produce a 1445meaningful log entry. 1446.It 1447Another type of packet is unconditionally dropped, a TCP packet with a 1448fragment offset of one. 1449This is a valid packet, but it only has one use, to try 1450to circumvent firewalls. 1451When logging is enabled, these packets are 1452reported as being dropped by rule -1. 1453.It 1454If you are logged in over a network, loading the 1455.Xr kld 4 1456version of 1457.Nm 1458is probably not as straightforward as you would think. 1459I recommend the following command line: 1460.Bd -literal -offset indent 1461kldload /modules/ipfw.ko && \e 1462ipfw add 32000 allow ip from any to any 1463.Ed 1464.Pp 1465Along the same lines, doing an 1466.Bd -literal -offset indent 1467ipfw flush 1468.Ed 1469.Pp 1470in similar surroundings is also a bad idea. 1471.It 1472The 1473.Nm 1474filter list may not be modified if the system security level 1475is set to 3 or higher 1476(see 1477.Xr init 8 1478for information on system security levels). 1479.El 1480.Sh PACKET DIVERSION 1481A 1482.Xr divert 4 1483socket bound to the specified port will receive all packets 1484diverted to that port. 1485If no socket is bound to the destination port, or if the kernel 1486wasn't compiled with divert socket support, the packets are 1487dropped. 1488.Sh SYSCTL VARIABLES 1489A set of 1490.Xr sysctl 8 1491variables controls the behaviour of the firewall and 1492associated modules ( 1493.Nm dummynet, bridge 1494). 1495These are shown below together with their default value 1496(but always check with the 1497.Xr sysctl 8 1498command what value is actually in use) and meaning: 1499.Bl -tag -width indent 1500.It Em net.inet.ip.dummynet.expire : No 1 1501Lazily delete dynamic pipes/queue once they have no pending traffic. 1502You can disable this by setting the variable to 0, in which case 1503the pipes/queues will only be deleted when the threshold is reached. 1504.It Em net.inet.ip.dummynet.hash_size : No 64 1505Default size of the hash table used for dynamic pipes/queues. 1506This value is used when no 1507.Cm buckets 1508option is specified when configuring a pipe/queue. 1509.It Em net.inet.ip.dummynet.max_chain_len : No 16 1510Target value for the maximum number of pipes/queues in a hash bucket. 1511The product 1512.Cm max_chain_len*hash_size 1513is used to determine the threshold over which empty pipes/queues 1514will be expired even when 1515.Cm net.inet.ip.dummynet.expire=0 . 1516.It net.inet.ip.dummynet.red_lookup_depth : No 256 1517.It net.inet.ip.dummynet.red_avg_pkt_size : No 512 1518.It net.inet.ip.dummynet.red_max_pkt_size : No 1500 1519Parameters used in the computations of the drop probability 1520for the RED algorithm. 1521.It Em net.inet.ip.fw.autoinc_step : No 100 1522Delta beween rule numbers when auto-generating them. 1523The value must be in the range 1..1000. 1524.It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets 1525The current number of buckets in the hash table for dynamic rules 1526(readonly). 1527.It Em net.inet.ip.fw.debug : No 1 1528Controls debugging messages produced by 1529.Nm . 1530.It Em net.inet.ip.fw.dyn_buckets : No 256 1531The number of buckets in the hash table for dynamic rules. 1532Must be a power of 2, up to 65536. 1533It only takes effect when all dynamic rules have expired, so you 1534are advised to use a 1535.Cm flush 1536command to make sure that the hash table is resized. 1537.It Em net.inet.ip.fw.dyn_count : No 3 1538Current number of dynamic rules 1539(read-only). 1540.It Em net.inet.ip.fw.dyn_keepalive : No 1 1541Enables generation of keepalive packets for 1542.Cm keep-state 1543rules on TCP sessions. A keepalive is generated to both 1544sides of the connection every 5 seconds for the last 20 1545seconds of the lifetime of the rule. 1546.It Em net.inet.ip.fw.dyn_max : No 8192 1547Maximum number of dynamic rules. 1548When you hit this limit, no more dynamic rules can be 1549installed until old ones expire. 1550.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300 1551.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20 1552.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1 1553.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1 1554.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5 1555.It Em net.inet.ip.fw.dyn_short_lifetime : No 30 1556These variables control the lifetime, in seconds, of dynamic 1557rules. 1558Upon the initial SYN exchange the lifetime is kept short, 1559then increased after both SYN have been seen, then decreased 1560again during the final FIN exchange or when a RST is received. 1561Both 1562.Em dyn_fin_lifetime 1563and 1564.Em dyn_rst_lifetime 1565must be strictly lower than 5 seconds, the period of 1566repetition of keepalives. The firewall enforces that. 1567.It Em net.inet.ip.fw.enable : No 1 1568Enables the firewall. 1569Setting this variable to 0 lets you run your machine without 1570firewall even if compiled in. 1571.It Em net.inet.ip.fw.one_pass : No 1 1572When set, the packet exiting from the 1573.Xr dummynet 4 1574pipe is not passed though the firewall again. 1575Otherwise, after a pipe action, the packet is 1576reinjected into the firewall at the next rule. 1577.Pp 1578Note: bridged and layer 2 packets coming out of a pipe 1579are never reinjected in the firewall irrespective of the 1580value of this variable. 1581.It Em net.inet.ip.fw.verbose : No 1 1582Enables verbose messages. 1583.It Em net.inet.ip.fw.verbose_limit : No 0 1584Limits the number of messages produced by a verbose firewall. 1585.It Em net.link.ether.ipfw : No 0 1586Controls whether layer-2 packets are passed to 1587.Nm . 1588Default is no. 1589.It Em net.link.ether.bridge_ipfw : No 0 1590Controls whether bridged packets are passed to 1591.Nm . 1592Default is no. 1593.El 1594.Sh IPFW2 ENHANCEMENTS 1595This Section lists the features that have been introduced in 1596.Nm ipfw2 1597and were not present in 1598.Nm ipfw1 . 1599We list them in order of the potential impact that they can 1600have in writing your rulesets. 1601You might want to consider using these features in order to 1602write your rulesets in a more efficient way. 1603.Bl -tag -width indent 1604.It Address sets 1605.Nm ipfw1 1606does not supports address sets (those in the form 1607.Ar addr/masklen{num,num,...} 1608). 1609.Pp 1610A minor difference between 1611.Nm ipfw1 1612and 1613.Nm ipfw2 1614is that the former allows addresses to be specified as 1615.Ar ipno:mask 1616where the mask can be an arbitrary bitmask instead of 1617a countiguous set of bits. 1618.Nm ipfw2 1619no longer supports this syntax though it would be trivial 1620to reintroduce it as it is supported on the kernel side. 1621.It Port specifications 1622.Nm ipfw1 1623only allows one port range when specifying TCP and UDP ports, and 1624is limited to 10 entries instead of the 15 allowed by 1625.Nm ipfw2 . 1626Also, in 1627.Nm ipfw1 1628you can only specify ports when the rule is requesting 1629.Cm tcp 1630or 1631.Cm udp 1632packets. With 1633.Nm ipfw2 1634you can put port specifications in rules matching all packets, 1635and the match will be attempted only on those packets carrying 1636protocols which include port identifiers. 1637.Pp 1638Finally, 1639.Nm ipfw1 1640allowed the first port entry to be specified as 1641.Ar port:mask 1642where 1643.Ar mask 1644can be an arbitrary 16-bit mask. 1645This syntax is of questionable usefulness and it is not 1646supported anymore in 1647.Nm ipfw2 . 1648.It Or-blocks 1649.Nm ipfw1 1650does not support Or-blocks. 1651.It keepalives 1652.Nm ipfw1 1653does not generate keepalives for stateful sessions. 1654As a consequence, it might cause idle sessions to drop because 1655the lifetime of the dynamic rules expires. 1656.It Sets of rules 1657.Nm ipfw1 1658does not implement sets of rules. 1659.It MAC header filtering and Layer-2 firewalling. 1660.Nm ipfw1 1661does not implement filtering on MAC header fields, nor it is 1662invoked on packets from 1663.Cm ether_demux() 1664and 1665.Cm ether_output_frame(). 1666The sysctl variable 1667.Em net.link.ether.ipfw 1668has no effect there. 1669.It Options 1670The following options are not supported in 1671.Nm ipfw1 1672(RELENG_4) 1673rules: 1674.Pp 1675.Cm layer2, ipid, iplen, ipprecedence, iptos, ipttl, 1676.Cm ipversion, tcpack, tcpseq, tcpwin . 1677.It Dummynet options 1678The following option for 1679.Nm dummynet 1680pipes/queues is not supported: 1681.Cm noerror . 1682.El 1683.Sh EXAMPLES 1684There are far too many possible uses of 1685.Nm 1686so this Section will only give a small set of examples. 1687.Pp 1688.Ss BASIC PACKET FILTERING 1689This command adds an entry which denies all tcp packets from 1690.Em cracker.evil.org 1691to the telnet port of 1692.Em wolf.tambov.su 1693from being forwarded by the host: 1694.Pp 1695.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet" 1696.Pp 1697This one disallows any connection from the entire crackers 1698network to my host: 1699.Pp 1700.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org" 1701.Pp 1702A first and efficient way to limit access (not using dynamic rules) 1703is the use of the following rules: 1704.Pp 1705.Dl "ipfw add allow tcp from any to any established" 1706.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup" 1707.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup" 1708.Dl "..." 1709.Dl "ipfw add deny tcp from any to any" 1710.Pp 1711The first rule will be a quick match for normal TCP packets, 1712but it will not match the initial SYN packet, which will be 1713matched by the 1714.Cm setup 1715rules only for selected source/destination pairs. 1716All other SYN packets will be rejected by the final 1717.Cm deny 1718rule. 1719.Pp 1720If you administer one or more subnets, you can take advantage of the 1721.Nm ipfw2 1722syntax to specify address sets and or-blocks and write extremely 1723compact rulesets which selectively enable services to blocks 1724of clients, as below: 1725.Pp 1726.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q" 1727.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q" 1728.Dl "" 1729.Dl "ipfw add allow ip from ${goodguys} to any" 1730.Dl "ipfw add deny ip from ${badguys} to any" 1731.Dl "... normal policies ..." 1732.Pp 1733The 1734.Nm ipfw1 1735syntax would require a separate rule for each IP in the above 1736example. 1737.Ss DYNAMIC RULES 1738In order to protect a site from flood attacks involving fake 1739TCP packets, it is safer to use dynamic rules: 1740.Pp 1741.Dl "ipfw add check-state" 1742.Dl "ipfw add deny tcp from any to any established" 1743.Dl "ipfw add allow tcp from my-net to any setup keep-state" 1744.Pp 1745This will let the firewall install dynamic rules only for 1746those connection which start with a regular SYN packet coming 1747from the inside of our network. 1748Dynamic rules are checked when encountering the first 1749.Cm check-state 1750or 1751.Cm keep-state 1752rule. 1753A 1754.Cm check-state 1755rule should be usually placed near the beginning of the 1756ruleset to minimize the amount of work scanning the ruleset. 1757Your mileage may vary. 1758.Pp 1759To limit the number of connections a user can open 1760you can use the following type of rules: 1761.Pp 1762.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10" 1763.Dl "ipfw add allow tcp from any to me setup limit src-addr 4" 1764.Pp 1765The former (assuming it runs on a gateway) will allow each host 1766on a /24 network to open at most 10 TCP connections. 1767The latter can be placed on a server to make sure that a single 1768client does not use more than 4 simultaneous connections. 1769.Pp 1770.Em BEWARE : 1771stateful rules can be subject to denial-of-service attacks 1772by a SYN-flood which opens a huge number of dynamic rules. 1773The effects of such attacks can be partially limited by 1774acting on a set of 1775.Xr sysctl 8 1776variables which control the operation of the firewall. 1777.Pp 1778Here is a good usage of the 1779.Cm list 1780command to see accounting records and timestamp information: 1781.Pp 1782.Dl ipfw -at list 1783.Pp 1784or in short form without timestamps: 1785.Pp 1786.Dl ipfw -a list 1787.Pp 1788which is equivalent to: 1789.Pp 1790.Dl ipfw show 1791.Pp 1792Next rule diverts all incoming packets from 192.168.2.0/24 1793to divert port 5000: 1794.Pp 1795.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in 1796.Pp 1797.Ss TRAFFIC SHAPING 1798The following rules show some of the applications of 1799.Nm 1800and 1801.Xr dummynet 4 1802for simulations and the like. 1803.Pp 1804This rule drops random incoming packets with a probability 1805of 5%: 1806.Pp 1807.Dl "ipfw add prob 0.05 deny ip from any to any in" 1808.Pp 1809A similar effect can be achieved making use of dummynet pipes: 1810.Pp 1811.Dl "ipfw add pipe 10 ip from any to any" 1812.Dl "ipfw pipe 10 config plr 0.05" 1813.Pp 1814We can use pipes to artificially limit bandwidth, e.g. on a 1815machine acting as a router, if we want to limit traffic from 1816local clients on 192.168.2.0/24 we do: 1817.Pp 1818.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 1819.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 1820.Pp 1821note that we use the 1822.Cm out 1823modifier so that the rule is not used twice. 1824Remember in fact that 1825.Nm 1826rules are checked both on incoming and outgoing packets. 1827.Pp 1828Should we like to simulate a bidirectional link with bandwidth 1829limitations, the correct way is the following: 1830.Pp 1831.Dl "ipfw add pipe 1 ip from any to any out" 1832.Dl "ipfw add pipe 2 ip from any to any in" 1833.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes" 1834.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes" 1835.Pp 1836The above can be very useful, e.g. if you want to see how 1837your fancy Web page will look for a residential user which 1838is connected only through a slow link. 1839You should not use only one pipe for both directions, unless 1840you want to simulate a half-duplex medium (e.g. AppleTalk, 1841Ethernet, IRDA). 1842It is not necessary that both pipes have the same configuration, 1843so we can also simulate asymmetric links. 1844.Pp 1845Should we like to verify network performance with the RED queue 1846management algorithm: 1847.Pp 1848.Dl "ipfw add pipe 1 ip from any to any" 1849.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1" 1850.Pp 1851Another typical application of the traffic shaper is to 1852introduce some delay in the communication. 1853This can affect a lot applications which do a lot of Remote 1854Procedure Calls, and where the round-trip-time of the 1855connection often becomes a limiting factor much more than 1856bandwidth: 1857.Pp 1858.Dl "ipfw add pipe 1 ip from any to any out" 1859.Dl "ipfw add pipe 2 ip from any to any in" 1860.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s" 1861.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s" 1862.Pp 1863Per-flow queueing can be useful for a variety of purposes. 1864A very simple one is counting traffic: 1865.Pp 1866.Dl "ipfw add pipe 1 tcp from any to any" 1867.Dl "ipfw add pipe 1 udp from any to any" 1868.Dl "ipfw add pipe 1 ip from any to any" 1869.Dl "ipfw pipe 1 config mask all" 1870.Pp 1871The above set of rules will create queues (and collect 1872statistics) for all traffic. 1873Because the pipes have no limitations, the only effect is 1874collecting statistics. 1875Note that we need 3 rules, not just the last one, because 1876when 1877.Nm 1878tries to match IP packets it will not consider ports, so we 1879would not see connections on separate ports as different 1880ones. 1881.Pp 1882A more sophisticated example is limiting the outbound traffic 1883on a net with per-host limits, rather than per-network limits: 1884.Pp 1885.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 1886.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in" 1887.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 1888.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 1889.Ss SETS OF RULES 1890To add a set of rules atomically, e.g. set 18: 1891.Pp 1892.Dl "ipfw disable set 18" 1893.Dl "ipfw add NN set 18 ... # repeat as needed" 1894.Dl "ipfw enable set 18" 1895.Pp 1896To delete a set of rules atomically the command is simply: 1897.Pp 1898.Dl "ipfw delete set 18" 1899.Pp 1900To test a ruleset and disable it and regain control if something goes wrong: 1901.Pp 1902.Dl "ipfw disable set 18" 1903.Dl "ipfw add NN set 18 ... # repeat as needed" 1904.Dl "ipfw enable set 18 ; echo done; sleep 30 && ipfw disable set 18" 1905.Pp 1906Here if everything goes well, you press control-C before the "sleep" 1907terminates, and your ruleset will be left active. Otherwise, e.g. if 1908you cannot access your box, the ruleset will be disabled after 1909the sleep terminates thus restoring the previous situation. 1910.Sh SEE ALSO 1911.Xr cpp 1 , 1912.Xr m4 1 , 1913.Xr bridge 4 , 1914.Xr divert 4 , 1915.Xr dummynet 4 , 1916.Xr ip 4 , 1917.Xr ipfirewall 4 , 1918.Xr protocols 5 , 1919.Xr services 5 , 1920.Xr init 8 , 1921.Xr kldload 8 , 1922.Xr reboot 8 , 1923.Xr sysctl 8 , 1924.Xr syslogd 8 1925.Rs 1926.%A "S. Floyd" 1927.%A "V. Jacobson" 1928.%T "Random Early Detection gateways for Congestion Avoidance" 1929.%D "August 1993" 1930.Re 1931.Rs 1932.%A "B. Braden" 1933.%A "D. Clark" 1934.%A "J. Crowcroft" 1935.%A "B. Davie" 1936.%A "S. Deering" 1937.%A "D. Estrin" 1938.%A "S. Floyd" 1939.%A "V. Jacobson" 1940.%A "G. Minshall" 1941.%A "C. Partridge" 1942.%A "L. Peterson" 1943.%A "K. Ramakrishnan" 1944.%A "S. Shenker" 1945.%A "J. Wroclawski" 1946.%A "L. Zhang" 1947.%T "Recommendations on Queue Management and Congestion Avoidance in the Internet" 1948.%D "April 1998" 1949.%O "RFC 2309" 1950.Re 1951.Sh BUGS 1952The syntax has grown over the years and sometimes it might be confusing. 1953Unfortunately, backward compatibility prevents cleaning up mistakes 1954done in the definition of the syntax. 1955.Pp 1956.Em !!! WARNING !!! 1957.Pp 1958Misconfiguring the firewall can put your computer in an unusable state, 1959possibly shutting down network services and requiring console access to 1960regain control to it. 1961.Pp 1962Incoming packet fragments diverted by 1963.Cm divert 1964or 1965.Cm tee 1966are reassembled before delivery to the socket. 1967The action used on those packet is the one from the 1968rule which matches the first fragment of the packet. 1969.Pp 1970Packets that match a 1971.Cm tee 1972rule should not be immediately accepted, but should continue 1973going through the rule list. 1974This may be fixed in a later version. 1975.Pp 1976Packets diverted to userland, and then reinserted by a userland process 1977(such as 1978.Xr natd 8 ) 1979will lose various packet attributes, including their source interface. 1980If a packet is reinserted in this manner, later rules may be incorrectly 1981applied, making the order of 1982.Cm divert 1983rules in the rule sequence very important. 1984.Sh AUTHORS 1985.An Ugen J. S. Antsilevich , 1986.An Poul-Henning Kamp , 1987.An Alex Nash , 1988.An Archie Cobbs , 1989.An Luigi Rizzo . 1990.Pp 1991.An -nosplit 1992API based upon code written by 1993.An Daniel Boulet 1994for BSDI. 1995.Pp 1996Work on 1997.Xr dummynet 4 1998traffic shaper supported by Akamba Corp. 1999.Sh HISTORY 2000The 2001.Nm 2002utility first appeared in 2003.Fx 2.0 . 2004.Xr dummynet 4 2005was introduced in 2006.Fx 2.2.8 . 2007Stateful extensions were introduced in 2008.Fx 4.0 . 2009.Nm ipfw2 2010was introduced in Summer 2002. 2011