1.\" 2.\" $FreeBSD$ 3.\" 4.Dd February 16, 2000 5.Dt IPFW 8 6.Os FreeBSD 7.Sh NAME 8.Nm ipfw 9.Nd controlling utility for IP firewall and traffic shaper 10.Sh SYNOPSIS 11.Nm ipfw 12.Op Fl q 13.Oo 14.Fl p Ar preproc 15.Oo Fl D 16.Sm off 17.Ar macro 18.Op = Ar value 19.Sm on 20.Oc 21.Op Fl U Ar macro 22.Oc 23.Ar file 24.Nm ipfw 25.Op Fl f | q 26.Cm flush 27.Nm ipfw 28.Op Fl q 29.Es \&{ \&} 30.En Cm zero | resetlog | delete 31.Op Ar number ... 32.Nm ipfw 33.Op Fl s Op Ar field 34.Op Fl aftN 35.Es \&{ \&} 36.En Cm list | show 37.Op Ar number ... 38.Nm ipfw 39.Op Fl q 40.Cm add 41.Op Ar number 42.Ar rule-body 43.Nm ipfw 44.Cm pipe 45.Ar number 46.Cm config 47.Ar pipe-config-options 48.Nm ipfw 49.Cm pipe 50.Es \&{ \&} 51.En Cm delete | list | show 52.Op Ar number ... 53.Sh DESCRIPTION 54.Nm 55is the user interface for controlling the 56.Xr ipfirewall 4 57and the 58.Xr dummynet 4 59traffic shaper in 60.Fx . 61.Pp 62Each incoming or outgoing packet is passed through the 63.Nm 64rules. 65If host is acting as a gateway, packets forwarded by 66the gateway are processed by 67.Nm 68twice. 69In case a host is acting as a bridge, packets forwarded by 70the bridge are processed by 71.Nm 72once. 73.Pp 74A firewall configuration is made of a list of numbered rules, 75which is scanned for each packet until a match is found and 76the relevant action is performed. 77Depending on the action and certain system settings, packets 78can be reinjected into the firewall at the rule after the 79matching one for further processing. 80All rules apply to all interfaces, so it is responsibility 81of the system administrator to write the ruleset in such a 82way as to minimize the number of checks. 83.Pp 84A configuration always includes a 85.Em DEFAULT 86rule (numbered 65535) which cannot be modified by the programmer 87and always matches packets. 88The action associated with the default rule can be either 89.Cm deny 90or 91.Cm allow 92depending on how the kernel is configured. 93.Pp 94If the ruleset includes one or more rules with the 95.Cm keep-state 96option, then 97.Nm 98assumes a 99.Em stateful 100behaviour, i.e. upon a match will create dynamic rules matching 101the exact parameters (addresses and ports) of the matching packet. 102.Pp 103These dynamic rules, which have a limited lifetime, are checked 104at the first occurrence of a 105.Cm check-state 106or 107.Cm keep-state 108rule, and are typically used to open the firewall on-demand to 109legitimate traffic only. 110See the 111.Sx RULE FORMAT 112and 113.Sx EXAMPLES 114sections below for more information on the stateful behaviour of 115.Nm ipfw . 116.Pp 117All rules (including dynamic ones) have a few associated counters: 118a packet count, a byte count, a log count and a timestamp 119indicating the time of the last match. 120Counters can be displayed or reset with 121.Nm 122commands. 123.Pp 124Rules can be added with the 125.Cm add 126command; deleted individually with the 127.Cm delete 128command, and globally with the 129.Cm flush 130command; displayed, optionally with the content of the 131counters, using the 132.Cm show 133and 134.Cm list 135commands. 136Finally, counters can be reset with the 137.Cm zero 138and 139.Cm resetlog 140commands. 141.Pp 142The following options are available: 143.Bl -tag -width indent 144.It Fl a 145While listing, show counter values. 146See also the 147.Cm show 148command. 149.It Fl f 150Don't ask for confirmation for commands that can cause problems 151if misused, 152.No i.e. Cm flush . 153.Em Note , 154if there is no tty associated with the process, this is implied. 155.It Fl q 156While 157.Cm add Ns ing , 158.Cm zero Ns ing , 159.Cm resetlog Ns ging 160or 161.Cm flush Ns ing , 162be quiet about actions 163.Po 164implies 165.Fl f 166.Pc . 167This is useful for adjusting rules by executing multiple 168.Nm 169commands in a script 170.Po 171e.g., 172.Ql sh\ /etc/rc.firewall 173.Pc , 174or by processing a file of many 175.Nm 176rules, 177across a remote login session. 178If a 179.Cm flush 180is performed in normal (verbose) mode (with the default kernel 181configuration), it prints a message. 182Because all rules are flushed, the message cannot be delivered 183to the login session. 184This causes the remote login session to be closed and the 185remainder of the ruleset is not processed. 186Access to the console is required to recover. 187.It Fl t 188While listing, show last match timestamp. 189.It Fl N 190Try to resolve addresses and service names in output. 191.It Fl s Op Ar field 192While listing pipes, sort according to one of the four 193counters (total and current packets or bytes). 194.El 195.Pp 196To ease configuration, rules can be put into a file which is 197processed using 198.Nm 199as shown in the first synopsis line. 200The 201.Ar file 202will be read line by line and applied as arguments to the 203.Nm 204utility. 205.Pp 206Optionally, a preprocessor can be specified using 207.Fl p Ar preproc 208where 209.Ar file 210is to be piped through. 211Useful preprocessors include 212.Xr cpp 1 213and 214.Xr m4 1 . 215If 216.Ar preproc 217doesn't start with a slash 218.Pq Ql / 219as its first character, the usual 220.Ev PATH 221name search is performed. 222Care should be taken with this in environments where not all 223filesystems are mounted (yet) by the time 224.Nm 225is being run (e.g. when they are mounted over NFS). 226Once 227.Fl p 228has been specified, optional 229.Fl D 230and 231.Fl U 232specifications can follow and will be passed on to the preprocessor. 233This allows for flexible configuration files (like conditionalizing 234them on the local hostname) and the use of macros to centralize 235frequently required arguments like IP addresses. 236.Pp 237The 238.Nm 239.Cm pipe 240commands are used to configure the traffic shaper, as shown in the 241.Sx TRAFFIC SHAPER CONFIGURATION 242section below. 243.Sh RULE FORMAT 244The 245.Nm 246rule format is the following: 247.Bd -ragged 248.Op Cm prob Ar match_probability 249.Ar action 250.Op Cm log Op Cm logamount Ar number 251.Ar proto 252.Cm from Ar src 253.Cm to Ar dst 254.Op Ar interface-spec 255.Op Ar options 256.Ed 257.Pp 258Each packet can be filtered based on the following information that is 259associated with it: 260.Pp 261.Bl -tag -width "Source and destination IP address" -offset indent -compact 262.It Transmit and receive interface 263(by name or address) 264.It Direction 265(incoming or outgoing) 266.It Source and destination IP address 267(possibly masked) 268.It Protocol 269(TCP, UDP, ICMP, etc.) 270.It Source and destination port 271(lists, ranges or masks) 272.It TCP flags 273.It IP fragment flag 274.It IP options 275.It ICMP types 276.It User/group ID of the socket associated with the packet 277.El 278.Pp 279Note that it may be dangerous to filter on the source IP 280address or source TCP/UDP port because either or both could 281easily be spoofed. 282.Bl -tag -width indent 283.It Cm prob Ar match_probability 284A match is only declared with the specified probability 285(floating point number between 0 and 1). 286This can be useful for a number of applications such as 287random packet drop or 288.Po 289in conjunction with 290.Xr dummynet 4 291.Pc 292to simulate the effect of multiple paths leading to out-of-order 293packet delivery. 294.It Ar action : 295.Bl -tag -width indent 296.It Cm allow 297Allow packets that match rule. 298The search terminates. 299Aliases are 300.Cm pass , 301.Cm permit 302and 303.Cm accept . 304.It Cm deny 305Discard packets that match this rule. 306The search terminates. 307.Cm drop 308is an alias for 309.Cm deny . 310.It Cm reject 311.Pq Deprecated . 312Discard packets that match this rule, and try to send an ICMP 313host unreachable notice. 314The search terminates. 315.It Cm unreach Ar code 316Discard packets that match this rule, and try to send an ICMP 317unreachable notice with code 318.Ar code , 319where 320.Ar code 321is a number from 0 to 255, or one of these aliases: 322.Cm net , host , protocol , port , 323.Cm needfrag , srcfail , net-unknown , host-unknown , 324.Cm isolated , net-prohib , host-prohib , tosnet , 325.Cm toshost , filter-prohib , host-precedence 326or 327.Cm precedence-cutoff . 328The search terminates. 329.It Cm reset 330TCP packets only. 331Discard packets that match this rule, and try to send a TCP 332reset (RST) notice. 333The search terminates. 334.It Cm count 335Update counters for all packets that match rule. 336The search continues with the next rule. 337.It Cm check-state 338Checks the packet against the dynamic ruleset. 339If a match is found then the search terminates, otherwise 340we move to the next rule. 341If no 342.Cm check-state 343rule is found, the dynamic ruleset is checked at the first 344.Cm keep-state 345rule. 346.It Cm divert Ar port 347Divert packets that match this rule to the 348.Xr divert 4 349socket bound to port 350.Ar port . 351The search terminates. 352.It Cm tee Ar port 353Send a copy of packets matching this rule to the 354.Xr divert 4 355socket bound to port 356.Ar port . 357The search terminates and the original packet is accepted 358.Po 359but see section 360.Sx BUGS 361below 362.Pc . 363.It Cm fwd Ar ipaddr Ns Xo 364.Op , Ns Ar port 365.Xc 366Change the next-hop on matching packets to 367.Ar ipaddr , 368which can be an IP address in dotted quad or a host name. 369If 370.Ar ipaddr 371is not a directly-reachable address, the route as found in 372the local routing table for that IP is used instead. 373If 374.Ar ipaddr 375is a local address, then on a packet entering the system 376from a remote host it will be diverted to 377.Ar port 378on the local machine, keeping the local address of the socket 379set to the original IP address the packet was destined for. 380This is intended for use with transparent proxy servers. 381If the IP is not a local address then the port number 382(if specified) is ignored and the rule only applies to packets 383leaving the system. 384This will also map addresses to local ports when packets are 385generated locally. 386The search terminates if this rule matches. 387If the port number is not given then the port number in the 388packet is used, so that a packet for an external machine port 389Y would be forwarded to local port Y. 390The kernel must have been compiled with the 391.Dv IPFIREWALL_FORWARD 392option. 393.It Cm pipe Ar pipe_nr 394Pass packet to a 395.Xr dummynet 4 396.Dq pipe 397(for bandwidth limitation, delay, etc.). 398See the 399.Xr dummynet 4 400manpage for further information. 401The search terminates; however, on exit from the pipe and if 402the 403.Xr sysctl 8 404variable 405.Em net.inet.ip.fw.one_pass 406is not set, the packet is passed again to the firewall code 407starting from the next rule. 408.It Cm skipto Ar number 409Skip all subsequent rules numbered less than 410.Ar number . 411The search continues with the first rule numbered 412.Ar number 413or higher. 414.El 415.It Cm log Op Cm logamount Ar number 416If the kernel was compiled with 417.Dv IPFIREWALL_VERBOSE , 418then when a packet matches a rule with the 419.Cm log 420keyword a message will be printed on the console. 421If the kernel was compiled with the 422.Dv IPFIREWALL_VERBOSE_LIMIT 423option, then by default logging will cease after the number 424of packets specified by the option are received for that 425particular chain entry, and 426.Em net.inet.ip.fw.verbose_limit 427will be set to that number. 428However, if 429.Cm logamount Ar number 430is used, that 431.Ar number 432will be the logging limit rather than 433.Em net.inet.ip.fw.verbose_limit , 434where the value 435.Dq 0 436removes the logging limit. 437Logging may then be re-enabled by clearing the logging counter 438or the packet counter for that entry. 439.Pp 440Console logging and the log limit are adjustable dynamically 441through the 442.Xr sysctl 8 443interface in the MIB base of 444.Em net.inet.ip.fw . 445.It Ar proto 446An IP protocol specified by number or name (for a complete 447list see 448.Pa /etc/protocols ) . 449The 450.Cm ip 451or 452.Cm all 453keywords mean any protocol will match. 454.It Ar src No and Ar dst : 455.Aq Ar address Ns / Ns Ar mask 456.Op Ar ports 457.Pp 458The 459.Aq Ar address Ns / Ns Ar mask 460may be specified as: 461.Bl -tag -width indent 462.It Ar ipno 463An IP number of the form 1.2.3.4. 464Only this exact IP number will match the rule. 465.It Ar ipno Ns / Ns Ar bits 466An IP number with a mask width of the form 1.2.3.4/24. 467In this case all IP numbers from 1.2.3.0 to 1.2.3.255 will match. 468.It Ar ipno Ns : Ns Ar mask 469An IP number with a mask of the form 1.2.3.4:255.255.240.0. 470In this case all IP numbers from 1.2.0.0 to 1.2.15.255 will match. 471.El 472.Pp 473The sense of the match can be inverted by preceding an address with the 474.Cm not 475modifier, causing all other addresses to be matched instead. 476This does not affect the selection of port numbers. 477.Pp 478With the TCP and UDP protocols, optional 479.Em ports 480may be specified as: 481.Bd -ragged -offset indent 482.Sm off 483.Eo \&{ 484.Ar port | 485.Ar port No \&- Ar port | 486.Ar port : mask 487.Ec \&} Op , Ar port Op , Ar ... 488.Sm on 489.Ed 490.Pp 491The 492.Ql \&- 493notation specifies a range of ports (including boundaries). 494.Pp 495The 496.Ql \&: 497notation specifies a port and a mask, a match is declared if 498the port number in the packet matches the one in the rule, 499limited to the bits which are set in the mask. 500.Pp 501Service names (from 502.Pa /etc/services ) 503may be used instead of numeric port values. 504A range may only be specified as the first value, and the 505length of the port list is limited to 506.Dv IP_FW_MAX_PORTS 507ports (as defined in 508.Pa /usr/src/sys/netinet/ip_fw.h ) . 509A backslash 510.Pq Ql \e 511can be used to escape the dash 512.Pq Ql - 513character in a service name: 514.Pp 515.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any" 516.Pp 517Fragmented packets which have a non-zero offset (i.e. not the first 518fragment) will never match a rule which has one or more port 519specifications. 520See the 521.Cm frag 522option for details on matching fragmented packets. 523.It Ar interface-spec 524Some combinations of the following specifiers are allowed: 525.Bl -tag -width "via ipno" 526.It Cm in 527Only match incoming packets. 528.It Cm out 529Only match outgoing packets. 530.It Cm via Ar ifX 531Packet must be going through interface 532.Ar ifX . 533.It Cm via Ar if Ns Cm * 534Packet must be going through interface 535.Ar ifX , 536where 537.Ar X 538is any unit number. 539.It Cm via any 540Packet must be going through 541.Em some 542interface. 543.It Cm via Ar ipno 544Packet must be going through the interface having IP address 545.Ar ipno . 546.El 547.Pp 548The 549.Cm via 550keyword causes the interface to always be checked. 551If 552.Cm recv 553or 554.Cm xmit 555is used instead of 556.Cm via , 557then the only receive or transmit interface (respectively) 558is checked. 559By specifying both, it is possible to match packets based on 560both receive and transmit interface, e.g.: 561.Pp 562.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1" 563.Pp 564The 565.Cm recv 566interface can be tested on either incoming or outgoing packets, 567while the 568.Cm xmit 569interface can only be tested on outgoing packets. 570So 571.Cm out 572is required (and 573.Cm in 574is invalid) whenever 575.Cm xmit 576is used. 577Specifying 578.Cm via 579together with 580.Cm xmit 581or 582.Cm recv 583is invalid. 584.Pp 585A packet may not have a receive or transmit interface: packets 586originating from the local host have no receive interface, 587while packets destined for the local host have no transmit 588interface. 589.It Ar options : 590.Bl -tag -width indent 591.It Cm keep-state Op Ar method 592Upon a match, the firewall will create a dynamic rule, whose 593default behaviour is to matching bidirectional traffic between 594source and destination IP/port using the same protocol. 595The rule has a limited lifetime (controlled by a set of 596.Xr sysctl 8 597variables), and the lifetime is refreshed every time a matching 598packet is found. 599.Pp 600The actual behaviour can be modified by specifying a different 601.Ar method , 602although at the moment only the default one is specified. 603.It Cm bridged 604Matches only bridged packets. 605This can be useful for multicast or broadcast traffic, which 606would otherwise pass through the firewall twice: once during 607bridging, and a second time when the packet is delivered to 608the local stack. 609.Pp 610Apart from a small performance penalty, this would be a problem 611when using 612.Em pipes 613because the same packet would be accounted for twice in terms 614of bandwidth, queue occupation, and also counters. 615.It Cm frag 616Match if the packet is a fragment and this is not the first 617fragment of the datagram. 618.Cm frag 619may not be used in conjunction with either 620.Cm tcpflags 621or TCP/UDP port specifications. 622.It Cm ipoptions Ar spec 623Match if the IP header contains the comma separated list of 624options specified in 625.Ar spec . 626The supported IP options are: 627.Pp 628.Cm ssrr 629(strict source route), 630.Cm lsrr 631(loose source route), 632.Cm rr 633(record packet route) and 634.Cm ts 635(timestamp). 636The absence of a particular option may be denoted 637with a 638.Ql ! . 639.It Cm established 640TCP packets only. 641Match packets that have the RST or ACK bits set. 642.It Cm setup 643TCP packets only. 644Match packets that have the SYN bit set but no ACK bit. 645.It Cm tcpflags Ar spec 646TCP packets only. 647Match if the TCP header contains the comma separated list of 648flags specified in 649.Ar spec . 650The supported TCP flags are: 651.Pp 652.Cm fin , 653.Cm syn , 654.Cm rst , 655.Cm psh , 656.Cm ack 657and 658.Cm urg . 659The absence of a particular flag may be denoted 660with a 661.Ql ! . 662A rule which contains a 663.Cm tcpflags 664specification can never match a fragmented packet which has 665a non-zero offset. 666See the 667.Cm frag 668option for details on matching fragmented packets. 669.It Cm icmptypes Ar types 670ICMP packets only. 671Match if the ICMP type is in the list 672.Ar types . 673The list may be specified as any combination of ranges or 674individual types separated by commas. 675The supported ICMP types are: 676.Pp 677echo reply 678.Pq Cm 0 , 679destination unreachable 680.Pq Cm 3 , 681source quench 682.Pq Cm 4 , 683redirect 684.Pq Cm 5 , 685echo request 686.Pq Cm 8 , 687router advertisement 688.Pq Cm 9 , 689router solicitation 690.Pq Cm 10 , 691time-to-live exceeded 692.Pq Cm 11 , 693IP header bad 694.Pq Cm 12 , 695timestamp request 696.Pq Cm 13 , 697timestamp reply 698.Pq Cm 14 , 699information request 700.Pq Cm 15 , 701information reply 702.Pq Cm 16 , 703address mask request 704.Pq Cm 17 705and address mask reply 706.Pq Cm 18 . 707.It Cm uid Ar user 708Match all TCP or UDP packets sent by or received for a 709.Ar user . 710A 711.Ar user 712may be matched by name or identification number. 713.It Cm gid Ar group 714Match all TCP or UDP packets sent by or received for a 715.Ar group . 716A 717.Ar group 718may be matched by name or identification number. 719.El 720.El 721.Sh TRAFFIC SHAPER CONFIGURATION 722The 723.Nm 724utility is also the user interface for the 725.Xr dummynet 4 726traffic shaper. 727The shaper operates by passing packets to objects called 728.Em pipes , 729which emulate a link with given bandwidth, propagation delay, 730queue size and packet loss rate. 731The 732.Nm 733pipe configuration format is the following: 734.Bd -ragged 735.Cm pipe Ar number Cm config 736.Op Cm bw Ar bandwidth 737.Oo 738.Cm queue 739.Es \&{ \&} 740.En Ar slots | size 741.Oc 742.Op Cm delay Ar ms-delay 743.Op Cm plr Ar loss-probability 744.Op Cm mask Ar mask-specifier 745.Op Cm buckets Ar hash-table-size 746.Ed 747.Pp 748The following parameters can be configured for a pipe: 749.Bl -tag -width indent 750.It Cm bw Ar bandwidth 751Bandwidth, measured in 752.Sm off 753.Oo 754.Cm K | M 755.Oc Eo \&{ 756.Cm bit/s | Byte/s 757.Ec \&} . 758.Sm on 759.Pp 760A value of 0 (default) means unlimited bandwidth. 761The unit must follow immediately the number, as in 762.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 763.It Cm delay Ar ms-delay 764Propagation delay, measured in milliseconds. 765The value is rounded to the next multiple of the clock tick 766(typically 10ms, but it is a good practice to run kernels 767with 768.Dq "options HZ=1000" 769to reduce 770the granularity to 1ms or less). 771Default value is 0, meaning no delay. 772.It Cm queue Xo 773.Es \&{ \&} 774.En Ar slots | size Ns Cm Kbytes 775.Xc 776Queue size, in 777.Ar slots 778or 779.Cm KBytes . 780Default value is 50 slots, which 781is the typical queue size for Ethernet devices. 782Note that for slow speed links you should keep the queue 783size short or your traffic might be affected by a significant 784queueing delay. 785E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit 786or 20s of queue on a 30Kbit/s pipe. 787Even worse effect can result if you get packets from an 788interface with a much larger MTU, e.g. the loopback interface 789with its 16KB packets. 790.It Cm plr Ar packet-loss-rate 791Packet loss rate. 792Argument 793.Ar packet-loss-rate 794is a floating-point number between 0 and 1, with 0 meaning no 795loss, 1 meaning 100% loss. 796The loss rate is internally represented on 31 bits. 797.It Cm mask Ar mask-specifier 798The 799.Xr dummynet 4 800allows you to generate per-flow queues using a single pipe 801specification. 802A flow identifier is constructed by masking the IP addresses, 803ports and protocol types as specified in the pipe configuration. 804Packets with the same identifier after masking fall into the 805same queue. 806Available mask specifiers are a combination of the following: 807.Cm dst-ip Ar mask , 808.Cm src-ip Ar mask , 809.Cm dst-port Ar mask , 810.Cm src-port Ar mask , 811.Cm proto Ar mask 812or 813.Cm all , 814where the latter means all bits in all fields are significant. 815.It Cm buckets Ar hash-table-size 816Specifies the size of the hash table used for storing the 817various queues. 818Default value is 64 controlled by the 819.Xr sysctl 8 820variable 821.Em net.inet.ip.dummynet.hash_size , 822allowed range is 16 to 1024. 823.El 824.Sh CHECKLIST 825Here are some important points to consider when designing your 826rules: 827.Bl -bullet 828.It 829Remember that you filter both packets going 830.Cm in 831and 832.Cm out . 833Most connections need packets going in both directions. 834.It 835Remember to test very carefully. 836It is a good idea to be near the console when doing this. 837.It 838Don't forget the loopback interface. 839.El 840.Sh FINE POINTS 841There is one kind of packet that the firewall will always 842discard, that is an IP fragment with a fragment offset of 843one. 844This is a valid packet, but it only has one use, to try 845to circumvent firewalls. 846.Pp 847If you are logged in over a network, loading the 848.Xr kld 4 849version of 850.Nm 851is probably not as straightforward as you would think. 852I recommend the following command line: 853.Bd -literal -offset indent 854kldload /modules/ipfw.ko && \e 855ipfw add 32000 allow ip from any to any 856.Ed 857.Pp 858Along the same lines, doing an 859.Bd -literal -offset indent 860ipfw flush 861.Ed 862.Pp 863in similar surroundings is also a bad idea. 864.Pp 865The 866.Nm 867filter list may not be modified if the system security level 868is set to 3 or higher 869.Po 870see 871.Xr init 8 872for information on system security levels 873.Pc . 874.Sh PACKET DIVERSION 875A 876.Xr divert 4 877socket bound to the specified port will receive all packets 878diverted to that port. 879If no socket is bound to the destination port, or if the kernel 880wasn't compiled with divert socket support, the packets are 881dropped. 882.Sh SYSCTL VARIABLES 883A set of 884.Xr sysctl 8 885variables controls the behaviour of the firewall. 886These are shown below together with their default value and 887meaning: 888.Bl -tag -width indent 889.It Em net.inet.ip.fw.debug : No 1 890Controls debugging messages produced by 891.Nm ipfw . 892.It Em net.inet.ip.fw.one_pass : No 1 893When set, permits only one pass through the firewall. 894Otherwise, after a pipe or divert action, the packet is 895reinjected in the firewall starting from the next rule. 896.It Em net.inet.ip.fw.verbose : No 1 897Enables verbose messages. 898.It Em net.inet.ip.fw.enable : No 1 899Enables the firewall. 900Setting this variable to 0 lets you run your machine without 901firewall even if compiled in. 902.It Em net.inet.ip.fw.verbose_limit : No 0 903Limits the number of messages produced by a verbose firewall. 904.It Em net.inet.ip.fw.dyn_buckets : No 256 905.It Em net.inet.ip.fw.curr_dyn_buckets : No 256 906The configured and current size of the hash table used to 907hold dynamic rules. 908This must be a power of 2. 909The table can only be resized when empty, so in order to 910resize it on the fly you will probably have to 911.Cm flush 912and reload the ruleset. 913.It Em net.inet.ip.fw.dyn_count : No 3 914Current number of dynamic rules 915.Pq read-only . 916.It Em net.inet.ip.fw.dyn_max : No 1000 917Maximum number of dynamic rules. 918When you hit this limit, no more dynamic rules can be 919installed until old ones expire. 920.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300 921.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20 922.It Em net.inet.ip.fw.dyn_fin_lifetime : No 20 923.It Em net.inet.ip.fw.dyn_rst_lifetime : No 5 924.It Em net.inet.ip.fw.dyn_short_lifetime : No 30 925These variables control the lifetime, in seconds, of dynamic 926rules. 927Upon the initial SYN exchange the lifetime is kept short, 928then increased after both SYN have been seen, then decreased 929again during the final FIN exchange or when a RST 930.El 931.Sh EXAMPLES 932This command adds an entry which denies all tcp packets from 933.Em cracker.evil.org 934to the telnet port of 935.Em wolf.tambov.su 936from being forwarded by the host: 937.Pp 938.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet" 939.Pp 940This one disallows any connection from the entire crackers 941network to my host: 942.Pp 943.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org" 944.Pp 945A first and efficient way to limit access (not using dynamic rules) 946is the use of the following rules: 947.Pp 948.Dl "ipfw add allow tcp from any to any established" 949.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup" 950.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup" 951.Dl "..." 952.Dl "ipfw add deny tcp from any to any" 953.Pp 954The first rule will be a quick match for normal TCP packets, 955but it will not match the initial SYN packet, which will be 956matched by the 957.Cm setup 958rules only for selected source/destination pairs. 959All other SYN packets will be rejected by the final 960.Cm deny 961rule. 962.Pp 963In order to protect a site from flood attacks involving fake 964TCP packets, it is safer to use dynamic rules: 965.Pp 966.Dl "ipfw add check-state" 967.Dl "ipfw add deny tcp from any to any established" 968.Dl "ipfw add allow tcp from my-net to any setup keep-state" 969.Pp 970This will let the firewall install dynamic rules only for 971those connection which start with a regular SYN packet coming 972from the inside of our network. 973Dynamic rules are checked when encountering the first 974.Cm check-state 975or 976.Cm keep-state 977rule. 978A 979.Cm check-state 980rule should be usually placed near the beginning of the 981ruleset to minimize the amount of work scanning the ruleset. 982Your mileage may vary. 983.Pp 984.Em BEWARE : 985stateful rules can be subject to denial-of-service attacks 986by a SYN-flood which opens a huge number of dynamic rules. 987The effects of such attacks can be partially limited by 988acting on a set of 989.Xr sysctl 8 990variables which control the operation of the firewall. 991.Pp 992Here is a good usage of the 993.Cm list 994command to see accounting records and timestamp information: 995.Pp 996.Dl ipfw -at list 997.Pp 998or in short form without timestamps: 999.Pp 1000.Dl ipfw -a list 1001.Pp 1002Next rule diverts all incoming packets from 192.168.2.0/24 1003to divert port 5000: 1004.Pp 1005.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in 1006.Pp 1007The following rules show some of the applications of 1008.Nm 1009and 1010.Xr dummynet 4 1011for simulations and the like. 1012.Pp 1013This rule drops random incoming packets with a probability 1014of 5%: 1015.Pp 1016.Dl "ipfw add prob 0.05 deny ip from any to any in" 1017.Pp 1018A similar effect can be achieved making use of dummynet pipes: 1019.Pp 1020.Dl "ipfw add pipe 10 ip from any to any" 1021.Dl "ipfw pipe 10 config plr 0.05" 1022.Pp 1023We can use pipes to artificially limit bandwidth, e.g. on a 1024machine acting as a router, if we want to limit traffic from 1025local clients on 192.168.2.0/24 we do: 1026.Pp 1027.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 1028.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes" 1029.Pp 1030note that we use the 1031.Cm out 1032modifier so that the rule is not used twice. 1033Remember in fact that 1034.Nm 1035rules are checked both on incoming and outgoing packets. 1036.Pp 1037Should we like to simulate a bidirectional link with bandwidth 1038limitations, the correct way is the following: 1039.Pp 1040.Dl "ipfw add pipe 1 ip from any to any out" 1041.Dl "ipfw add pipe 2 ip from any to any in" 1042.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes" 1043.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes" 1044.Pp 1045The above can be very useful, e.g. if you want to see how 1046your fancy Web page will look for a residential user which 1047is connected only through a slow link. 1048You should not use only one pipe for both directions, unless 1049you want to simulate a half-duplex medium (e.g. AppleTalk, 1050Ethernet, IRDA). 1051It is not necessary that both pipes have the same configuration, 1052so we can also simulate asymmetric links. 1053.Pp 1054Another typical application of the traffic shaper is to 1055introduce some delay in the communication. 1056This can affect a lot applications which do a lot of Remote 1057Procedure Calls, and where the round-trip-time of the 1058connection often becomes a limiting factor much more than 1059bandwidth: 1060.Pp 1061.Dl "ipfw add pipe 1 ip from any to any out" 1062.Dl "ipfw add pipe 2 ip from any to any in" 1063.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s" 1064.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s" 1065.Pp 1066Per-flow queueing can be useful for a variety of purposes. 1067A very simple one is counting traffic: 1068.Pp 1069.Dl "ipfw add pipe 1 tcp from any to any" 1070.Dl "ipfw add pipe 1 udp from any to any" 1071.Dl "ipfw add pipe 1 ip from any to any" 1072.Dl "ipfw pipe 1 config mask all" 1073.Pp 1074The above set of rules will create queues (and collect 1075statistics) for all traffic. 1076Because the pipes have no limitations, the only effect is 1077collecting statistics. 1078Note that we need 3 rules, not just the last one, because 1079when 1080.Nm 1081tries to match IP packets it will not consider ports, so we 1082would not see connections on separate ports as different 1083ones. 1084.Pp 1085A more sophisticated example is limiting the outbound traffic 1086on a net with per-host limits, rather than per-network limits: 1087.Pp 1088.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out" 1089.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in" 1090.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 1091.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes" 1092.Sh SEE ALSO 1093.Xr cpp 1 , 1094.Xr m4 1 , 1095.Xr bridge 4 , 1096.Xr divert 4 , 1097.Xr dummynet 4 , 1098.Xr ip 4 , 1099.Xr ipfirewall 4 , 1100.Xr protocols 5 , 1101.Xr services 5 , 1102.Xr init 8 , 1103.Xr kldload 8 , 1104.Xr reboot 8 , 1105.Xr sysctl 8 , 1106.Xr syslogd 8 . 1107.Sh BUGS 1108.Pp 1109The syntax has grown over the years and it is not very clean. 1110.Pp 1111.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!! 1112.Pp 1113This program can put your computer in rather unusable state. 1114When using it for the first time, work on the console of the 1115computer, and do 1116.Em NOT 1117do anything you don't understand. 1118.Pp 1119When manipulating/adding chain entries, service and protocol names 1120are not accepted. 1121.Pp 1122Incoming packet fragments diverted by 1123.Cm divert 1124or 1125.Cm tee 1126are reassembled before delivery to the socket. 1127.Pp 1128Packets that match a 1129.Cm tee 1130rule should not be immediately accepted, but should continue 1131going through the rule list. 1132This may be fixed in a later version. 1133.Sh AUTHORS 1134.An Ugen J. S. Antsilevich , 1135.An Poul-Henning Kamp , 1136.An Alex Nash , 1137.An Archie Cobbs , 1138.An Luigi Rizzo . 1139.Pp 1140API based upon code written by 1141Daniel Boulet 1142for BSDI. 1143.Pp 1144Work on 1145.Xr dummynet 4 1146traffic shaper supported by Akamba Corp. 1147.Sh HISTORY 1148The 1149.Nm 1150utility first appeared in 1151.Fx 2.0 . 1152.Xr dummynet 4 1153was introduced in 1154.Fx 2.2.8 . 1155Stateful extensions were introduced in 1156.Fx 4.0 . 1157