1.\" $NetBSD: pf.conf.5,v 1.4 2005/07/01 12:43:50 peter Exp $ 2.\" $OpenBSD: pf.conf.5,v 1.326 2005/03/01 18:10:44 jmc Exp $ 3.\" 4.\" Copyright (c) 2002, Daniel Hartmeier 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 11.\" - Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" - Redistributions in binary form must reproduce the above 14.\" copyright notice, this list of conditions and the following 15.\" disclaimer in the documentation and/or other materials provided 16.\" with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28.\" ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd November 19, 2002 32.Dt PF.CONF 5 33.Os 34.Sh NAME 35.Nm pf.conf 36.Nd packet filter configuration file 37.Sh DESCRIPTION 38The 39.Xr pf 4 40packet filter modifies, drops or passes packets according to rules or 41definitions specified in 42.Nm pf.conf . 43.Sh STATEMENT ORDER 44There are seven types of statements in 45.Nm pf.conf : 46.Bl -tag -width xxxx 47.It Cm Macros 48User-defined variables may be defined and used later, simplifying 49the configuration file. 50Macros must be defined before they are referenced in 51.Nm pf.conf . 52.It Cm Tables 53Tables provide a mechanism for increasing the performance and flexibility of 54rules with large numbers of source or destination addresses. 55.It Cm Options 56Options tune the behaviour of the packet filtering engine. 57.It Cm Traffic Normalization Li (e.g. Em scrub ) 58Traffic normalization protects internal machines against inconsistencies 59in Internet protocols and implementations. 60.It Cm Queueing 61Queueing provides rule-based bandwidth control. 62.It Cm Translation Li (Various forms of NAT) 63Translation rules specify how addresses are to be mapped or redirected to 64other addresses. 65.It Cm Packet Filtering 66Stateful and stateless packet filtering provides rule-based blocking or 67passing of packets. 68.El 69.Pp 70With the exception of 71.Cm macros 72and 73.Cm tables , 74the types of statements should be grouped and appear in 75.Nm pf.conf 76in the order shown above, as this matches the operation of the underlying 77packet filtering engine. 78By default 79.Xr pfctl 8 80enforces this order (see 81.Ar set require-order 82below). 83.Sh MACROS 84Much like 85.Xr cpp 1 86or 87.Xr m4 1 , 88macros can be defined that will later be expanded in context. 89Macro names must start with a letter, and may contain letters, digits 90and underscores. 91Macro names may not be reserved words (for example 92.Ar pass , 93.Ar in , 94.Ar out ) . 95Macros are not expanded inside quotes. 96.Pp 97For example, 98.Bd -literal -offset indent 99ext_if = \&"kue0\&" 100all_ifs = \&"{\&" $ext_if lo0 \&"}\&" 101pass out on $ext_if from any to any keep state 102pass in on $ext_if proto tcp from any to any port 25 keep state 103.Ed 104.Sh TABLES 105Tables are named structures which can hold a collection of addresses and 106networks. 107Lookups against tables in 108.Xr pf 4 109are relatively fast, making a single rule with tables much more efficient, 110in terms of 111processor usage and memory consumption, than a large number of rules which 112differ only in IP address (either created explicitly or automatically by rule 113expansion). 114.Pp 115Tables can be used as the source or destination of filter rules, 116.Ar scrub 117rules 118or 119translation rules such as 120.Ar nat 121or 122.Ar rdr 123(see below for details on the various rule types). 124Tables can also be used for the redirect address of 125.Ar nat 126and 127.Ar rdr 128rules and in the routing options of filter rules, but only for 129.Ar round-robin 130pools. 131.Pp 132Tables can be defined with any of the following 133.Xr pfctl 8 134mechanisms. 135As with macros, reserved words may not be used as table names. 136.Bl -tag -width "manually" 137.It Ar manually 138Persistent tables can be manually created with the 139.Ar add 140or 141.Ar replace 142option of 143.Xr pfctl 8 , 144before or after the ruleset has been loaded. 145.It Pa pf.conf 146Table definitions can be placed directly in this file, and loaded at the 147same time as other rules are loaded, atomically. 148Table definitions inside 149.Nm pf.conf 150use the 151.Ar table 152statement, and are especially useful to define non-persistent tables. 153The contents of a pre-existing table defined without a list of addresses 154to initialize it is not altered when 155.Nm pf.conf 156is loaded. 157A table initialized with the empty list, 158.Li { } , 159will be cleared on load. 160.El 161.Pp 162Tables may be defined with the following two attributes: 163.Bl -tag -width persist 164.It Ar persist 165The 166.Ar persist 167flag forces the kernel to keep the table even when no rules refer to it. 168If the flag is not set, the kernel will automatically remove the table 169when the last rule referring to it is flushed. 170.It Ar const 171The 172.Ar const 173flag prevents the user from altering the contents of the table once it 174has been created. 175Without that flag, 176.Xr pfctl 8 177can be used to add or remove addresses from the table at any time, even 178when running with 179.Xr securelevel 7 180= 2. 181.El 182.Pp 183For example, 184.Bd -literal -offset indent 185table <private> const { 10/8, 172.16/12, 192.168/16 } 186table <badhosts> persist 187block on fxp0 from { <private>, <badhosts> } to any 188.Ed 189.Pp 190creates a table called private, to hold RFC 1918 private network 191blocks, and a table called badhosts, which is initially empty. 192A filter rule is set up to block all traffic coming from addresses listed in 193either table. 194The private table cannot have its contents changed and the badhosts table 195will exist even when no active filter rules reference it. 196Addresses may later be added to the badhosts table, so that traffic from 197these hosts can be blocked by using 198.Bd -literal -offset indent 199# pfctl -t badhosts -Tadd 204.92.77.111 200.Ed 201.Pp 202A table can also be initialized with an address list specified in one or more 203external files, using the following syntax: 204.Bd -literal -offset indent 205table <spam> persist file \&"/etc/spammers\&" file \&"/etc/openrelays\&" 206block on fxp0 from <spam> to any 207.Ed 208.Pp 209The files 210.Pa /etc/spammers 211and 212.Pa /etc/openrelays 213list IP addresses, one per line. 214Any lines beginning with a # are treated as comments and ignored. 215In addition to being specified by IP address, hosts may also be 216specified by their hostname. 217When the resolver is called to add a hostname to a table, 218.Em all 219resulting IPv4 and IPv6 addresses are placed into the table. 220IP addresses can also be entered in a table by specifying a valid interface 221name or the 222.Em self 223keyword, in which case all addresses assigned to the interface(s) will be 224added to the table. 225.Sh OPTIONS 226.Xr pf 4 227may be tuned for various situations using the 228.Ar set 229command. 230.Bl -tag -width xxxx 231.It Ar set timeout 232.Pp 233.Bl -tag -width "src.track" -compact 234.It Ar interval 235Interval between purging expired states and fragments. 236.It Ar frag 237Seconds before an unassembled fragment is expired. 238.It Ar src.track 239Length of time to retain a source tracking entry after the last state 240expires. 241.El 242.Pp 243When a packet matches a stateful connection, the seconds to live for the 244connection will be updated to that of the 245.Ar proto.modifier 246which corresponds to the connection state. 247Each packet which matches this state will reset the TTL. 248Tuning these values may improve the performance of the 249firewall at the risk of dropping valid idle connections. 250.Pp 251.Bl -tag -width xxxx -compact 252.It Ar tcp.first 253The state after the first packet. 254.It Ar tcp.opening 255The state before the destination host ever sends a packet. 256.It Ar tcp.established 257The fully established state. 258.It Ar tcp.closing 259The state after the first FIN has been sent. 260.It Ar tcp.finwait 261The state after both FINs have been exchanged and the connection is closed. 262Some hosts (notably web servers on Solaris) send TCP packets even after closing 263the connection. 264Increasing 265.Ar tcp.finwait 266(and possibly 267.Ar tcp.closing ) 268can prevent blocking of such packets. 269.It Ar tcp.closed 270The state after one endpoint sends an RST. 271.El 272.Pp 273ICMP and UDP are handled in a fashion similar to TCP, but with a much more 274limited set of states: 275.Pp 276.Bl -tag -width xxxx -compact 277.It Ar udp.first 278The state after the first packet. 279.It Ar udp.single 280The state if the source host sends more than one packet but the destination 281host has never sent one back. 282.It Ar udp.multiple 283The state if both hosts have sent packets. 284.It Ar icmp.first 285The state after the first packet. 286.It Ar icmp.error 287The state after an ICMP error came back in response to an ICMP packet. 288.El 289.Pp 290Other protocols are handled similarly to UDP: 291.Pp 292.Bl -tag -width xxxx -compact 293.It Ar other.first 294.It Ar other.single 295.It Ar other.multiple 296.El 297.Pp 298Timeout values can be reduced adaptively as the number of state table 299entries grows. 300.Pp 301.Bl -tag -width xxxx -compact 302.It Ar adaptive.start 303When the number of state entries exceeds this value, adaptive scaling 304begins. 305All timeout values are scaled linearly with factor 306(adaptive.end - number of states) / (adaptive.end - adaptive.start). 307.It Ar adaptive.end 308When reaching this number of state entries, all timeout values become 309zero, effectively purging all state entries immediately. 310This value is used to define the scale factor, it should not actually 311be reached (set a lower state limit, see below). 312.El 313.Pp 314These values can be defined both globally and for each rule. 315When used on a per-rule basis, the values relate to the number of 316states created by the rule, otherwise to the total number of 317states. 318.Pp 319For example: 320.Bd -literal -offset indent 321set timeout tcp.first 120 322set timeout tcp.established 86400 323set timeout { adaptive.start 6000, adaptive.end 12000 } 324set limit states 10000 325.Ed 326.Pp 327With 9000 state table entries, the timeout values are scaled to 50% 328(tcp.first 60, tcp.established 43200). 329.Pp 330.It Ar set loginterface 331Enable collection of packet and byte count statistics for the given interface. 332These statistics can be viewed using 333.Bd -literal -offset indent 334# pfctl -s info 335.Ed 336.Pp 337In this example 338.Xr pf 4 339collects statistics on the interface named dc0: 340.Bd -literal -offset indent 341set loginterface dc0 342.Ed 343.Pp 344One can disable the loginterface using: 345.Bd -literal -offset indent 346set loginterface none 347.Ed 348.Pp 349.It Ar set limit 350Sets hard limits on the memory pools used by the packet filter. 351See 352.Xr pool 9 353for an explanation of memory pools. 354.Pp 355For example, 356.Bd -literal -offset indent 357set limit states 20000 358.Ed 359.Pp 360sets the maximum number of entries in the memory pool used by state table 361entries (generated by 362.Ar keep state 363rules) to 20000. 364Using 365.Bd -literal -offset indent 366set limit frags 20000 367.Ed 368.Pp 369sets the maximum number of entries in the memory pool used for fragment 370reassembly (generated by 371.Ar scrub 372rules) to 20000. 373Finally, 374.Bd -literal -offset indent 375set limit src-nodes 2000 376.Ed 377.Pp 378sets the maximum number of entries in the memory pool used for tracking 379source IP addresses (generated by the 380.Ar sticky-address 381and 382.Ar source-track 383options) to 2000. 384.Pp 385These can be combined: 386.Bd -literal -offset indent 387set limit { states 20000, frags 20000, src-nodes 2000 } 388.Ed 389.Pp 390.It Ar set optimization 391Optimize the engine for one of the following network environments: 392.Pp 393.Bl -tag -width xxxx -compact 394.It Ar normal 395A normal network environment. 396Suitable for almost all networks. 397.It Ar high-latency 398A high-latency environment (such as a satellite connection). 399.It Ar satellite 400Alias for 401.Ar high-latency . 402.It Ar aggressive 403Aggressively expire connections. 404This can greatly reduce the memory usage of the firewall at the cost of 405dropping idle connections early. 406.It Ar conservative 407Extremely conservative settings. 408Avoid dropping legitimate connections at the 409expense of greater memory utilization (possibly much greater on a busy 410network) and slightly increased processor utilization. 411.El 412.Pp 413For example: 414.Bd -literal -offset indent 415set optimization aggressive 416.Ed 417.Pp 418.It Ar set block-policy 419The 420.Ar block-policy 421option sets the default behaviour for the packet 422.Ar block 423action: 424.Pp 425.Bl -tag -width xxxxxxxx -compact 426.It Ar drop 427Packet is silently dropped. 428.It Ar return 429A TCP RST is returned for blocked TCP packets, 430an ICMP UNREACHABLE is returned for blocked UDP packets, 431and all other packets are silently dropped. 432.El 433.Pp 434For example: 435.Bd -literal -offset indent 436set block-policy return 437.Ed 438.It Ar set state-policy 439The 440.Ar state-policy 441option sets the default behaviour for states: 442.Pp 443.Bl -tag -width group-bound -compact 444.It Ar if-bound 445States are bound to interface. 446.It Ar group-bound 447States are bound to interface group (i.e. ppp) 448.It Ar floating 449States can match packets on any interfaces (the default). 450.El 451.Pp 452For example: 453.Bd -literal -offset indent 454set state-policy if-bound 455.Ed 456.It Ar set require-order 457By default 458.Xr pfctl 8 459enforces an ordering of the statement types in the ruleset to: 460.Em options , 461.Em normalization , 462.Em queueing , 463.Em translation , 464.Em filtering . 465Setting this option to 466.Ar no 467disables this enforcement. 468There may be non-trivial and non-obvious implications to an out of 469order ruleset. 470Consider carefully before disabling the order enforcement. 471.It Ar set fingerprints 472Load fingerprints of known operating systems from the given filename. 473By default fingerprints of known operating systems are automatically 474loaded from 475.Xr pf.os 5 476in 477.Pa /etc 478but can be overridden via this option. 479Setting this option may leave a small period of time where the fingerprints 480referenced by the currently active ruleset are inconsistent until the new 481ruleset finishes loading. 482.Pp 483For example: 484.Pp 485.Dl set fingerprints \&"/etc/pf.os.devel\&" 486.Pp 487.It Ar set skip on <ifspec> 488List interfaces for which packets should not be filtered. 489Packets passing in or out on such interfaces are passed as if pf was 490disabled, i.e. pf does not process them in any way. 491This can be useful on loopback and other virtual interfaces, when 492packet filtering is not desired and can have unexpected effects. 493For example: 494.Pp 495.Dl set skip on lo0 496.Pp 497.It Ar set debug 498Set the debug 499.Ar level 500to one of the following: 501.Pp 502.Bl -tag -width xxxxxxxxxxxx -compact 503.It Ar none 504Don't generate debug messages. 505.It Ar urgent 506Generate debug messages only for serious errors. 507.It Ar misc 508Generate debug messages for various errors. 509.It Ar loud 510Generate debug messages for common conditions. 511.El 512.El 513.Sh TRAFFIC NORMALIZATION 514Traffic normalization is used to sanitize packet content in such 515a way that there are no ambiguities in packet interpretation on 516the receiving side. 517The normalizer does IP fragment reassembly to prevent attacks 518that confuse intrusion detection systems by sending overlapping 519IP fragments. 520Packet normalization is invoked with the 521.Ar scrub 522directive. 523.Pp 524.Ar scrub 525has the following options: 526.Bl -tag -width xxxx 527.It Ar no-df 528Clears the 529.Ar dont-fragment 530bit from a matching IP packet. 531Some operating systems are known to generate fragmented packets with the 532.Ar dont-fragment 533bit set. 534This is particularly true with NFS. 535.Ar Scrub 536will drop such fragmented 537.Ar dont-fragment 538packets unless 539.Ar no-df 540is specified. 541.Pp 542Unfortunately some operating systems also generate their 543.Ar dont-fragment 544packets with a zero IP identification field. 545Clearing the 546.Ar dont-fragment 547bit on packets with a zero IP ID may cause deleterious results if an 548upstream router later fragments the packet. 549Using the 550.Ar random-id 551modifier (see below) is recommended in combination with the 552.Ar no-df 553modifier to ensure unique IP identifiers. 554.It Ar min-ttl <number> 555Enforces a minimum TTL for matching IP packets. 556.It Ar max-mss <number> 557Enforces a maximum MSS for matching TCP packets. 558.It Ar random-id 559Replaces the IP identification field with random values to compensate 560for predictable values generated by many hosts. 561This option only applies to packets that are not fragmented 562after the optional fragment reassembly. 563.It Ar fragment reassemble 564Using 565.Ar scrub 566rules, fragments can be reassembled by normalization. 567In this case, fragments are buffered until they form a complete 568packet, and only the completed packet is passed on to the filter. 569The advantage is that filter rules have to deal only with complete 570packets, and can ignore fragments. 571The drawback of caching fragments is the additional memory cost. 572But the full reassembly method is the only method that currently works 573with NAT. 574This is the default behavior of a 575.Ar scrub 576rule if no fragmentation modifier is supplied. 577.It Ar fragment crop 578The default fragment reassembly method is expensive, hence the option 579to crop is provided. 580In this case, 581.Xr pf 4 582will track the fragments and cache a small range descriptor. 583Duplicate fragments are dropped and overlaps are cropped. 584Thus data will only occur once on the wire with ambiguities resolving to 585the first occurrence. 586Unlike the 587.Ar fragment reassemble 588modifier, fragments are not buffered, they are passed as soon as they 589are received. 590The 591.Ar fragment crop 592reassembly mechanism does not yet work with NAT. 593.Pp 594.It Ar fragment drop-ovl 595This option is similar to the 596.Ar fragment crop 597modifier except that all overlapping or duplicate fragments will be 598dropped, and all further corresponding fragments will be 599dropped as well. 600.It Ar reassemble tcp 601Statefully normalizes TCP connections. 602.Ar scrub reassemble tcp 603rules may not have the direction (in/out) specified. 604.Ar reassemble tcp 605performs the following normalizations: 606.Pp 607.Bl -tag -width timeout -compact 608.It ttl 609Neither side of the connection is allowed to reduce their IP TTL. 610An attacker may send a packet such that it reaches the firewall, affects 611the firewall state, and expires before reaching the destination host. 612.Ar reassemble tcp 613will raise the TTL of all packets back up to the highest value seen on 614the connection. 615.It timestamp modulation 616Modern TCP stacks will send a timestamp on every TCP packet and echo 617the other endpoint's timestamp back to them. 618Many operating systems will merely start the timestamp at zero when 619first booted, and increment it several times a second. 620The uptime of the host can be deduced by reading the timestamp and multiplying 621by a constant. 622Also observing several different timestamps can be used to count hosts 623behind a NAT device. 624And spoofing TCP packets into a connection requires knowing or guessing 625valid timestamps. 626Timestamps merely need to be monotonically increasing and not derived off a 627guessable base time. 628.Ar reassemble tcp 629will cause 630.Ar scrub 631to modulate the TCP timestamps with a random number. 632.It extended PAWS checks 633There is a problem with TCP on long fat pipes, in that a packet might get 634delayed for longer than it takes the connection to wrap its 32-bit sequence 635space. 636In such an occurrence, the old packet would be indistinguishable from a 637new packet and would be accepted as such. 638The solution to this is called PAWS: Protection Against Wrapped Sequence 639numbers. 640It protects against it by making sure the timestamp on each packet does 641not go backwards. 642.Ar reassemble tcp 643also makes sure the timestamp on the packet does not go forward more 644than the RFC allows. 645By doing this, 646.Xr pf 4 647artificially extends the security of TCP sequence numbers by 10 to 18 648bits when the host uses appropriately randomized timestamps, since a 649blind attacker would have to guess the timestamp as well. 650.El 651.El 652.Pp 653For example, 654.Bd -literal -offset indent 655scrub in on $ext_if all fragment reassemble 656.Ed 657.Pp 658The 659.Ar no 660option prefixed to a scrub rule causes matching packets to remain unscrubbed, 661much in the same way as 662.Ar drop quick 663works in the packet filter (see below). 664This mechanism should be used when it is necessary to exclude specific packets 665from broader scrub rules. 666.Sh QUEUEING 667Packets can be assigned to queues for the purpose of bandwidth 668control. 669At least two declarations are required to configure queues, and later 670any packet filtering rule can reference the defined queues by name. 671During the filtering component of 672.Nm pf.conf , 673the last referenced 674.Ar queue 675name is where any packets from 676.Ar pass 677rules will be queued, while for 678.Ar block 679rules it specifies where any resulting ICMP or TCP RST 680packets should be queued. 681The 682.Ar scheduler 683defines the algorithm used to decide which packets get delayed, dropped, or 684sent out immediately. 685There are three 686.Ar schedulers 687currently supported. 688.Bl -tag -width xxxx 689.It Ar cbq 690Class Based Queueing. 691.Ar Queues 692attached to an interface build a tree, thus each 693.Ar queue 694can have further child 695.Ar queues . 696Each queue can have a 697.Ar priority 698and a 699.Ar bandwidth 700assigned. 701.Ar Priority 702mainly controls the time packets take to get sent out, while 703.Ar bandwidth 704has primarily effects on throughput. 705.Ar cbq 706achieves both partitioning and sharing of link bandwidth 707by hierarchically structured classes. 708Each class has its own 709.Ar queue 710and is assigned its share of 711.Ar bandwidth . 712A child class can borrow bandwidth from its parent class 713as long as excess bandwidth is available 714(see the option 715.Ar borrow , 716below). 717.It Ar priq 718Priority Queueing. 719.Ar Queues 720are flat attached to the interface, thus, 721.Ar queues 722cannot have further child 723.Ar queues . 724Each 725.Ar queue 726has a unique 727.Ar priority 728assigned, ranging from 0 to 15. 729Packets in the 730.Ar queue 731with the highest 732.Ar priority 733are processed first. 734.It Ar hfsc 735Hierarchical Fair Service Curve. 736.Ar Queues 737attached to an interface build a tree, thus each 738.Ar queue 739can have further child 740.Ar queues . 741Each queue can have a 742.Ar priority 743and a 744.Ar bandwidth 745assigned. 746.Ar Priority 747mainly controls the time packets take to get sent out, while 748.Ar bandwidth 749has primarily effects on throughput. 750.Ar hfsc 751supports both link-sharing and guaranteed real-time services. 752It employs a service curve based QoS model, 753and its unique feature is an ability to decouple 754.Ar delay 755and 756.Ar bandwidth 757allocation. 758.El 759.Pp 760The interfaces on which queueing should be activated are declared using 761the 762.Ar altq on 763declaration. 764.Ar altq on 765has the following keywords: 766.Bl -tag -width xxxx 767.It Ar <interface> 768Queueing is enabled on the named interface. 769.It Ar <scheduler> 770Specifies which queueing scheduler to use. 771Currently supported values 772are 773.Ar cbq 774for Class Based Queueing, 775.Ar priq 776for Priority Queueing and 777.Ar hfsc 778for the Hierarchical Fair Service Curve scheduler. 779.It Ar bandwidth <bw> 780The maximum bitrate for all queues on an 781interface may be specified using the 782.Ar bandwidth 783keyword. 784The value can be specified as an absolute value or as a 785percentage of the interface bandwidth. 786When using an absolute value, the suffixes 787.Ar b , 788.Ar Kb , 789.Ar Mb , 790and 791.Ar Gb 792are used to represent bits, kilobits, megabits, and 793gigabits per second, respectively. 794The value must not exceed the interface bandwidth. 795If 796.Ar bandwidth 797is not specified, the interface bandwidth is used. 798.It Ar qlimit <limit> 799The maximum number of packets held in the queue. 800The default is 50. 801.It Ar tbrsize <size> 802Adjusts the size, in bytes, of the token bucket regulator. 803If not specified, heuristics based on the 804interface bandwidth are used to determine the size. 805.It Ar queue <list> 806Defines a list of subqueues to create on an interface. 807.El 808.Pp 809In the following example, the interface dc0 810should queue up to 5 Mbit/s in four second-level queues using 811Class Based Queueing. 812Those four queues will be shown in a later example. 813.Bd -literal -offset indent 814altq on dc0 cbq bandwidth 5Mb queue { std, http, mail, ssh } 815.Ed 816.Pp 817Once interfaces are activated for queueing using the 818.Ar altq 819directive, a sequence of 820.Ar queue 821directives may be defined. 822The name associated with a 823.Ar queue 824must match a queue defined in the 825.Ar altq 826directive (e.g. mail), or, except for the 827.Ar priq 828.Ar scheduler , 829in a parent 830.Ar queue 831declaration. 832The following keywords can be used: 833.Bl -tag -width xxxx 834.It Ar on <interface> 835Specifies the interface the queue operates on. 836If not given, it operates on all matching interfaces. 837.It Ar bandwidth <bw> 838Specifies the maximum bitrate to be processed by the queue. 839This value must not exceed the value of the parent 840.Ar queue 841and can be specified as an absolute value or a percentage of the parent 842queue's bandwidth. 843If not specified, defaults to 100% of the parent queue's bandwidth. 844The 845.Ar priq 846scheduler does not support bandwidth specification. 847.It Ar priority <level> 848Between queues a priority level can be set. 849For 850.Ar cbq 851and 852.Ar hfsc , 853the range is 0 to 7 and for 854.Ar priq , 855the range is 0 to 15. 856The default for all is 1. 857.Ar Priq 858queues with a higher priority are always served first. 859.Ar Cbq 860and 861.Ar Hfsc 862queues with a higher priority are preferred in the case of overload. 863.It Ar qlimit <limit> 864The maximum number of packets held in the queue. 865The default is 50. 866.El 867.Pp 868The 869.Ar scheduler 870can get additional parameters with 871.Ar <scheduler> Ns Li (\& Ar <parameters> No ) . 872Parameters are as follows: 873.Bl -tag -width Fl 874.It Ar default 875Packets not matched by another queue are assigned to this one. 876Exactly one default queue is required. 877.It Ar red 878Enable RED (Random Early Detection) on this queue. 879RED drops packets with a probability proportional to the average 880queue length. 881.It Ar rio 882Enables RIO on this queue. 883RIO is RED with IN/OUT, thus running 884RED two times more than RIO would achieve the same effect. 885RIO is currently not supported in the GENERIC kernel. 886.It Ar ecn 887Enables ECN (Explicit Congestion Notification) on this queue. 888ECN implies RED. 889.El 890.Pp 891The 892.Ar cbq 893.Ar scheduler 894supports an additional option: 895.Bl -tag -width Fl 896.It Ar borrow 897The queue can borrow bandwidth from the parent. 898.El 899.Pp 900The 901.Ar hfsc 902.Ar scheduler 903supports some additional options: 904.Bl -tag -width Fl 905.It Ar realtime <sc> 906The minimum required bandwidth for the queue. 907.It Ar upperlimit <sc> 908The maximum allowed bandwidth for the queue. 909.It Ar linkshare <sc> 910The bandwidth share of a backlogged queue. 911.El 912.Pp 913<sc> is an acronym for 914.Ar service curve . 915.Pp 916The format for service curve specifications is 917.Ar ( m1 , d , m2 ) . 918.Ar m2 919controls the bandwidth assigned to the queue. 920.Ar m1 921and 922.Ar d 923are optional and can be used to control the initial bandwidth assignment. 924For the first 925.Ar d 926milliseconds the queue gets the bandwidth given as 927.Ar m1 , 928afterwards the value given in 929.Ar m2 . 930.Pp 931Furthermore, with 932.Ar cbq 933and 934.Ar hfsc , 935child queues can be specified as in an 936.Ar altq 937declaration, thus building a tree of queues using a part of 938their parent's bandwidth. 939.Pp 940Packets can be assigned to queues based on filter rules by using the 941.Ar queue 942keyword. 943Normally only one 944.Ar queue 945is specified; when a second one is specified it will instead be used for 946packets which have a 947.Em TOS 948of 949.Em lowdelay 950and for TCP ACKs with no data payload. 951.Pp 952To continue the previous example, the examples below would specify the 953four referenced 954queues, plus a few child queues. 955Interactive 956.Xr ssh 1 957sessions get priority over bulk transfers like 958.Xr scp 1 959and 960.Xr sftp 1 . 961The queues may then be referenced by filtering rules (see 962.Sx PACKET FILTERING 963below). 964.Bd -literal 965queue std bandwidth 10% cbq(default) 966queue http bandwidth 60% priority 2 cbq(borrow red) \e 967 { employees, developers } 968queue developers bandwidth 75% cbq(borrow) 969queue employees bandwidth 15% 970queue mail bandwidth 10% priority 0 cbq(borrow ecn) 971queue ssh bandwidth 20% cbq(borrow) { ssh_interactive, ssh_bulk } 972queue ssh_interactive bandwidth 50% priority 7 cbq(borrow) 973queue ssh_bulk bandwidth 50% priority 0 cbq(borrow) 974 975block return out on dc0 inet all queue std 976pass out on dc0 inet proto tcp from $developerhosts to any port 80 \e 977 keep state queue developers 978pass out on dc0 inet proto tcp from $employeehosts to any port 80 \e 979 keep state queue employees 980pass out on dc0 inet proto tcp from any to any port 22 \e 981 keep state queue(ssh_bulk, ssh_interactive) 982pass out on dc0 inet proto tcp from any to any port 25 \e 983 keep state queue mail 984.Ed 985.Sh TRANSLATION 986Translation rules modify either the source or destination address of the 987packets associated with a stateful connection. 988A stateful connection is automatically created to track packets matching 989such a rule as long as they are not blocked by the filtering section of 990.Nm pf.conf . 991The translation engine modifies the specified address and/or port in the 992packet, recalculates IP, TCP and UDP checksums as necessary, and passes it to 993the packet filter for evaluation. 994.Pp 995Since translation occurs before filtering the filter 996engine will see packets as they look after any 997addresses and ports have been translated. 998Filter rules will therefore have to filter based on the translated 999address and port number. 1000Packets that match a translation rule are only automatically passed if 1001the 1002.Ar pass 1003modifier is given, otherwise they are 1004still subject to 1005.Ar block 1006and 1007.Ar pass 1008rules. 1009.Pp 1010The state entry created permits 1011.Xr pf 4 1012to keep track of the original address for traffic associated with that state 1013and correctly direct return traffic for that connection. 1014.Pp 1015Various types of translation are possible with pf: 1016.Bl -tag -width xxxx 1017.It Ar binat 1018A 1019.Ar binat 1020rule specifies a bidirectional mapping between an external IP netblock 1021and an internal IP netblock. 1022.It Ar nat 1023A 1024.Ar nat 1025rule specifies that IP addresses are to be changed as the packet 1026traverses the given interface. 1027This technique allows one or more IP addresses 1028on the translating host to support network traffic for a larger range of 1029machines on an "inside" network. 1030Although in theory any IP address can be used on the inside, it is strongly 1031recommended that one of the address ranges defined by RFC 1918 be used. 1032These netblocks are: 1033.Bd -literal 103410.0.0.0 - 10.255.255.255 (all of net 10, i.e., 10/8) 1035172.16.0.0 - 172.31.255.255 (i.e., 172.16/12) 1036192.168.0.0 - 192.168.255.255 (i.e., 192.168/16) 1037.Ed 1038.It Pa rdr 1039The packet is redirected to another destination and possibly a 1040different port. 1041.Ar rdr 1042rules can optionally specify port ranges instead of single ports. 1043rdr ... port 2000:2999 -> ... port 4000 1044redirects ports 2000 to 2999 (inclusive) to port 4000. 1045rdr ... port 2000:2999 -> ... port 4000:* 1046redirects port 2000 to 4000, 2001 to 4001, ..., 2999 to 4999. 1047.El 1048.Pp 1049In addition to modifying the address, some translation rules may modify 1050source or destination ports for 1051.Xr tcp 4 1052or 1053.Xr udp 4 1054connections; implicitly in the case of 1055.Ar nat 1056rules and explicitly in the case of 1057.Ar rdr 1058rules. 1059Port numbers are never translated with a 1060.Ar binat 1061rule. 1062.Pp 1063For each packet processed by the translator, the translation rules are 1064evaluated in sequential order, from first to last. 1065The first matching rule decides what action is taken. 1066.Pp 1067The 1068.Ar no 1069option prefixed to a translation rule causes packets to remain untranslated, 1070much in the same way as 1071.Ar drop quick 1072works in the packet filter (see below). 1073If no rule matches the packet it is passed to the filter engine unmodified. 1074.Pp 1075Translation rules apply only to packets that pass through 1076the specified interface, and if no interface is specified, 1077translation is applied to packets on all interfaces. 1078For instance, redirecting port 80 on an external interface to an internal 1079web server will only work for connections originating from the outside. 1080Connections to the address of the external interface from local hosts will 1081not be redirected, since such packets do not actually pass through the 1082external interface. 1083Redirections cannot reflect packets back through the interface they arrive 1084on, they can only be redirected to hosts connected to different interfaces 1085or to the firewall itself. 1086.Pp 1087Note that redirecting external incoming connections to the loopback 1088address, as in 1089.Bd -literal -offset indent 1090rdr on ne3 inet proto tcp to port 8025 -> 127.0.0.1 port 25 1091.Ed 1092.Pp 1093will effectively allow an external host to connect to daemons 1094bound solely to the loopback address, circumventing the traditional 1095blocking of such connections on a real interface. 1096Unless this effect is desired, any of the local non-loopback addresses 1097should be used as redirection target instead, which allows external 1098connections only to daemons bound to this address or not bound to 1099any address. 1100.Pp 1101See 1102.Sx TRANSLATION EXAMPLES 1103below. 1104.Sh PACKET FILTERING 1105.Xr pf 4 1106has the ability to 1107.Ar block 1108and 1109.Ar pass 1110packets based on attributes of their layer 3 (see 1111.Xr ip 4 1112and 1113.Xr ip6 4 ) 1114and layer 4 (see 1115.Xr icmp 4 , 1116.Xr icmp6 4 , 1117.Xr tcp 4 , 1118.Xr udp 4 ) 1119headers. 1120In addition, packets may also be 1121assigned to queues for the purpose of bandwidth control. 1122.Pp 1123For each packet processed by the packet filter, the filter rules are 1124evaluated in sequential order, from first to last. 1125The last matching rule decides what action is taken. 1126.Pp 1127The following actions can be used in the filter: 1128.Bl -tag -width xxxx 1129.It Ar block 1130The packet is blocked. 1131There are a number of ways in which a 1132.Ar block 1133rule can behave when blocking a packet. 1134The default behaviour is to 1135.Ar drop 1136packets silently, however this can be overridden or made 1137explicit either globally, by setting the 1138.Ar block-policy 1139option, or on a per-rule basis with one of the following options: 1140.Pp 1141.Bl -tag -width xxxx -compact 1142.It Ar drop 1143The packet is silently dropped. 1144.It Ar return-rst 1145This applies only to 1146.Xr tcp 4 1147packets, and issues a TCP RST which closes the 1148connection. 1149.It Ar return-icmp 1150.It Ar return-icmp6 1151This causes ICMP messages to be returned for packets which match the rule. 1152By default this is an ICMP UNREACHABLE message, however this 1153can be overridden by specifying a message as a code or number. 1154.It Ar return 1155This causes a TCP RST to be returned for 1156.Xr tcp 4 1157packets and an ICMP UNREACHABLE for UDP and other packets. 1158.El 1159.Pp 1160Options returning ICMP packets currently have no effect if 1161.Xr pf 4 1162operates on a 1163.Xr bridge 4 , 1164as the code to support this feature has not yet been implemented. 1165.It Ar pass 1166The packet is passed. 1167.El 1168.Pp 1169If no rule matches the packet, the default action is 1170.Ar pass . 1171.Pp 1172To block everything by default and only pass packets 1173that match explicit rules, one uses 1174.Bd -literal -offset indent 1175block all 1176.Ed 1177.Pp 1178as the first filter rule. 1179.Pp 1180See 1181.Sx FILTER EXAMPLES 1182below. 1183.Sh PARAMETERS 1184The rule parameters specify the packets to which a rule applies. 1185A packet always comes in on, or goes out through, one interface. 1186Most parameters are optional. 1187If a parameter is specified, the rule only applies to packets with 1188matching attributes. 1189Certain parameters can be expressed as lists, in which case 1190.Xr pfctl 8 1191generates all needed rule combinations. 1192.Bl -tag -width xxxx 1193.It Ar in No or Ar out 1194This rule applies to incoming or outgoing packets. 1195If neither 1196.Ar in 1197nor 1198.Ar out 1199are specified, the rule will match packets in both directions. 1200.It Ar log 1201In addition to the action specified, a log message is generated. 1202All packets for that connection are logged, unless the 1203.Ar keep state , 1204.Ar modulate state 1205or 1206.Ar synproxy state 1207options are specified, in which case only the 1208packet that establishes the state is logged. 1209(See 1210.Ar keep state , 1211.Ar modulate state 1212and 1213.Ar synproxy state 1214below). 1215The logged packets are sent to the 1216.Xr pflog 4 1217interface. 1218This interface is monitored by the 1219.Xr pflogd 8 1220logging daemon, which dumps the logged packets to the file 1221.Pa /var/log/pflog 1222in 1223.Xr pcap 3 1224binary format. 1225.It Ar log-all 1226Used with 1227.Ar keep state , 1228.Ar modulate state 1229or 1230.Ar synproxy state 1231rules to force logging of all packets for a connection. 1232As with 1233.Ar log , 1234packets are logged to 1235.Xr pflog 4 . 1236.It Ar quick 1237If a packet matches a rule which has the 1238.Ar quick 1239option set, this rule 1240is considered the last matching rule, and evaluation of subsequent rules 1241is skipped. 1242.It Ar on <interface> 1243This rule applies only to packets coming in on, or going out through, this 1244particular interface. 1245It is also possible to simply give the interface driver name, like ppp or fxp, 1246to make the rule match packets flowing through a group of interfaces. 1247.It Ar <af> 1248This rule applies only to packets of this address family. 1249Supported values are 1250.Ar inet 1251and 1252.Ar inet6 . 1253.It Ar proto <protocol> 1254This rule applies only to packets of this protocol. 1255Common protocols are 1256.Xr icmp 4 , 1257.Xr icmp6 4 , 1258.Xr tcp 4 , 1259and 1260.Xr udp 4 . 1261For a list of all the protocol name to number mappings used by 1262.Xr pfctl 8 , 1263see the file 1264.Em /etc/protocols . 1265.It Xo 1266.Ar from <source> port <source> os <source> 1267.Ar to <dest> port <dest> 1268.Xc 1269This rule applies only to packets with the specified source and destination 1270addresses and ports. 1271.Pp 1272Addresses can be specified in CIDR notation (matching netblocks), as 1273symbolic host names or interface names, or as any of the following keywords: 1274.Pp 1275.Bl -tag -width xxxxxxxxxxxxxx -compact 1276.It Ar any 1277Any address. 1278.It Ar route <label> 1279Any address whose associated route has label 1280.Ar <label> . 1281See 1282.Xr route 4 1283and 1284.Xr route 8 . 1285.It Ar no-route 1286Any address which is not currently routable. 1287.It Ar <table> 1288Any address that matches the given table. 1289.El 1290.Pp 1291Interface names can have modifiers appended: 1292.Pp 1293.Bl -tag -width xxxxxxxxxxxx -compact 1294.It Ar :network 1295Translates to the network(s) attached to the interface. 1296.It Ar :broadcast 1297Translates to the interface's broadcast address(es). 1298.It Ar :peer 1299Translates to the point to point interface's peer address(es). 1300.It Ar :0 1301Do not include interface aliases. 1302.El 1303.Pp 1304Host names may also have the 1305.Ar :0 1306option appended to restrict the name resolution to the first of each 1307v4 and v6 address found. 1308.Pp 1309Host name resolution and interface to address translation are done at 1310ruleset load-time. 1311When the address of an interface (or host name) changes (under DHCP or PPP, 1312for instance), the ruleset must be reloaded for the change to be reflected 1313in the kernel. 1314Surrounding the interface name (and optional modifiers) in parentheses 1315changes this behaviour. 1316When the interface name is surrounded by parentheses, the rule is 1317automatically updated whenever the interface changes its address. 1318The ruleset does not need to be reloaded. 1319This is especially useful with 1320.Ar nat . 1321.Pp 1322Ports can be specified either by number or by name. 1323For example, port 80 can be specified as 1324.Em www . 1325For a list of all port name to number mappings used by 1326.Xr pfctl 8 , 1327see the file 1328.Pa /etc/services . 1329.Pp 1330Ports and ranges of ports are specified by using these operators: 1331.Bd -literal -offset indent 1332= (equal) 1333!= (unequal) 1334< (less than) 1335<= (less than or equal) 1336> (greater than) 1337>= (greater than or equal) 1338: (range including boundaries) 1339>< (range excluding boundaries) 1340<> (except range) 1341.Ed 1342.Pp 1343><, <> and : 1344are binary operators (they take two arguments). 1345For instance: 1346.Bl -tag -width Fl 1347.It Ar port 2000:2004 1348means 1349.Sq all ports >= 2000 and <= 2004 , 1350hence ports 2000, 2001, 2002, 2003 and 2004. 1351.It Ar port 2000 >< 2004 1352means 1353.Sq all ports > 2000 and < 2004 , 1354hence ports 2001, 2002 and 2003. 1355.It Ar port 2000 <> 2004 1356means 1357.Sq all ports < 2000 or > 2004 , 1358hence ports 1-1999 and 2005-65535. 1359.El 1360.Pp 1361The operating system of the source host can be specified in the case of TCP 1362rules with the 1363.Ar OS 1364modifier. 1365See the 1366.Sx OPERATING SYSTEM FINGERPRINTING 1367section for more information. 1368.Pp 1369The host, port and OS specifications are optional, as in the following examples: 1370.Bd -literal -offset indent 1371pass in all 1372pass in from any to any 1373pass in proto tcp from any port <= 1024 to any 1374pass in proto tcp from any to any port 25 1375pass in proto tcp from 10.0.0.0/8 port > 1024 \e 1376 to ! 10.1.2.3 port != ssh 1377pass in proto tcp from any os "OpenBSD" flags S/SA 1378pass in proto tcp from route "DTAG" 1379.Ed 1380.It Ar all 1381This is equivalent to "from any to any". 1382.It Ar group <group> 1383Similar to 1384.Ar user , 1385this rule only applies to packets of sockets owned by the specified group. 1386.It Ar user <user> 1387This rule only applies to packets of sockets owned by the specified user. 1388For outgoing connections initiated from the firewall, this is the user 1389that opened the connection. 1390For incoming connections to the firewall itself, this is the user that 1391listens on the destination port. 1392For forwarded connections, where the firewall is not a connection endpoint, 1393the user and group are 1394.Em unknown . 1395.Pp 1396All packets, both outgoing and incoming, of one connection are associated 1397with the same user and group. 1398Only TCP and UDP packets can be associated with users; for other protocols 1399these parameters are ignored. 1400.Pp 1401User and group refer to the effective (as opposed to the real) IDs, in 1402case the socket is created by a setuid/setgid process. 1403User and group IDs are stored when a socket is created; 1404when a process creates a listening socket as root (for instance, by 1405binding to a privileged port) and subsequently changes to another 1406user ID (to drop privileges), the credentials will remain root. 1407.Pp 1408User and group IDs can be specified as either numbers or names. 1409The syntax is similar to the one for ports. 1410The value 1411.Em unknown 1412matches packets of forwarded connections. 1413.Em unknown 1414can only be used with the operators 1415.Cm = 1416and 1417.Cm != . 1418Other constructs like 1419.Cm user >= unknown 1420are invalid. 1421Forwarded packets with unknown user and group ID match only rules 1422that explicitly compare against 1423.Em unknown 1424with the operators 1425.Cm = 1426or 1427.Cm != . 1428For instance 1429.Cm user >= 0 1430does not match forwarded packets. 1431The following example allows only selected users to open outgoing 1432connections: 1433.Bd -literal -offset indent 1434block out proto { tcp, udp } all 1435pass out proto { tcp, udp } all \e 1436 user { < 1000, dhartmei } keep state 1437.Ed 1438.It Ar flags <a>/<b> | /<b> 1439This rule only applies to TCP packets that have the flags 1440.Ar <a> 1441set out of set 1442.Ar <b> . 1443Flags not specified in 1444.Ar <b> 1445are ignored. 1446The flags are: (F)IN, (S)YN, (R)ST, (P)USH, (A)CK, (U)RG, (E)CE, and C(W)R. 1447.Bl -tag -width Fl 1448.It Ar flags S/S 1449Flag SYN is set. 1450The other flags are ignored. 1451.It Ar flags S/SA 1452Out of SYN and ACK, exactly SYN may be set. 1453SYN, SYN+PSH and SYN+RST match, but SYN+ACK, ACK and ACK+RST do not. 1454This is more restrictive than the previous example. 1455.It Ar flags /SFRA 1456If the first set is not specified, it defaults to none. 1457All of SYN, FIN, RST and ACK must be unset. 1458.El 1459.It Ar icmp-type <type> code <code> 1460.It Ar icmp6-type <type> code <code> 1461This rule only applies to ICMP or ICMPv6 packets with the specified type 1462and code. 1463Text names for ICMP types and codes are listed in 1464.Xr icmp 4 1465and 1466.Xr icmp6 4 . 1467This parameter is only valid for rules that cover protocols ICMP or 1468ICMP6. 1469The protocol and the ICMP type indicator 1470.Po 1471.Ar icmp-type 1472or 1473.Ar icmp6-type 1474.Pc 1475must match. 1476.It Ar allow-opts 1477By default, packets which contain IP options are blocked. 1478When 1479.Ar allow-opts 1480is specified for a 1481.Ar pass 1482rule, packets that pass the filter based on that rule (last matching) 1483do so even if they contain IP options. 1484For packets that match state, the rule that initially created the 1485state is used. 1486The implicit 1487.Ar pass 1488rule that is used when a packet does not match any rules does not 1489allow IP options. 1490.It Ar label <string> 1491Adds a label (name) to the rule, which can be used to identify the rule. 1492For instance, 1493pfctl -s labels 1494shows per-rule statistics for rules that have labels. 1495.Pp 1496The following macros can be used in labels: 1497.Pp 1498.Bl -tag -width $srcaddr -compact -offset indent 1499.It Ar $if 1500The interface. 1501.It Ar $srcaddr 1502The source IP address. 1503.It Ar $dstaddr 1504The destination IP address. 1505.It Ar $srcport 1506The source port specification. 1507.It Ar $dstport 1508The destination port specification. 1509.It Ar $proto 1510The protocol name. 1511.It Ar $nr 1512The rule number. 1513.El 1514.Pp 1515For example: 1516.Bd -literal -offset indent 1517ips = \&"{ 1.2.3.4, 1.2.3.5 }\&" 1518pass in proto tcp from any to $ips \e 1519 port > 1023 label \&"$dstaddr:$dstport\&" 1520.Ed 1521.Pp 1522expands to 1523.Bd -literal -offset indent 1524pass in inet proto tcp from any to 1.2.3.4 \e 1525 port > 1023 label \&"1.2.3.4:>1023\&" 1526pass in inet proto tcp from any to 1.2.3.5 \e 1527 port > 1023 label \&"1.2.3.5:>1023\&" 1528.Ed 1529.Pp 1530The macro expansion for the 1531.Ar label 1532directive occurs only at configuration file parse time, not during runtime. 1533.It Ar queue <queue> | ( <queue> , <queue> ) 1534Packets matching this rule will be assigned to the specified queue. 1535If two queues are given, packets which have a 1536.Em tos 1537of 1538.Em lowdelay 1539and TCP ACKs with no data payload will be assigned to the second one. 1540See 1541.Sx QUEUEING 1542for setup details. 1543.Pp 1544For example: 1545.Bd -literal -offset indent 1546pass in proto tcp to port 25 queue mail 1547pass in proto tcp to port 22 queue(ssh_bulk, ssh_prio) 1548.Ed 1549.It Ar tag <string> 1550Packets matching this rule will be tagged with the 1551specified string. 1552The tag acts as an internal marker that can be used to 1553identify these packets later on. 1554This can be used, for example, to provide trust between 1555interfaces and to determine if packets have been 1556processed by translation rules. 1557Tags are 1558.Qq sticky , 1559meaning that the packet will be tagged even if the rule 1560is not the last matching rule. 1561Further matching rules can replace the tag with a 1562new one but will not remove a previously applied tag. 1563A packet is only ever assigned one tag at a time. 1564.Ar pass 1565rules that use the 1566.Ar tag 1567keyword must also use 1568.Ar keep state , 1569.Ar modulate state 1570or 1571.Ar synproxy state . 1572Packet tagging can be done during 1573.Ar nat , 1574.Ar rdr , 1575or 1576.Ar binat 1577rules in addition to filter rules. 1578Tags take the same macros as labels (see above). 1579.It Ar tagged <string> 1580Used with filter or translation rules to specify that packets must already 1581be tagged with the given tag in order to match the rule. 1582Inverse tag matching can also be done 1583by specifying the 1584.Cm !\& 1585operator before the 1586.Ar tagged 1587keyword. 1588.It Ar probability <number> 1589A probability attribute can be attached to a rule, with a value set between 15900 and 1, bounds not included. 1591In that case, the rule will be honoured using the given probability value 1592only. 1593For example, the following rule will drop 20% of incoming ICMP packets: 1594.Bd -literal -offset indent 1595block in proto icmp probability 20% 1596.Ed 1597.El 1598.Sh ROUTING 1599If a packet matches a rule with a route option set, the packet filter will 1600route the packet according to the type of route option. 1601When such a rule creates state, the route option is also applied to all 1602packets matching the same connection. 1603.Bl -tag -width xxxx 1604.It Ar fastroute 1605The 1606.Ar fastroute 1607option does a normal route lookup to find the next hop for the packet. 1608.It Ar route-to 1609The 1610.Ar route-to 1611option routes the packet to the specified interface with an optional address 1612for the next hop. 1613When a 1614.Ar route-to 1615rule creates state, only packets that pass in the same direction as the 1616filter rule specifies will be routed in this way. 1617Packets passing in the opposite direction (replies) are not affected 1618and are routed normally. 1619.It Ar reply-to 1620The 1621.Ar reply-to 1622option is similar to 1623.Ar route-to , 1624but routes packets that pass in the opposite direction (replies) to the 1625specified interface. 1626Opposite direction is only defined in the context of a state entry, and 1627.Ar reply-to 1628is useful only in rules that create state. 1629It can be used on systems with multiple external connections to 1630route all outgoing packets of a connection through the interface 1631the incoming connection arrived through (symmetric routing enforcement). 1632.It Ar dup-to 1633The 1634.Ar dup-to 1635option creates a duplicate of the packet and routes it like 1636.Ar route-to . 1637The original packet gets routed as it normally would. 1638.El 1639.Sh POOL OPTIONS 1640For 1641.Ar nat 1642and 1643.Ar rdr 1644rules, (as well as for the 1645.Ar route-to , 1646.Ar reply-to 1647and 1648.Ar dup-to 1649rule options) for which there is a single redirection address which has a 1650subnet mask smaller than 32 for IPv4 or 128 for IPv6 (more than one IP 1651address), a variety of different methods for assigning this address can be 1652used: 1653.Bl -tag -width xxxx 1654.It Ar bitmask 1655The 1656.Ar bitmask 1657option applies the network portion of the redirection address to the address 1658to be modified (source with 1659.Ar nat , 1660destination with 1661.Ar rdr ) . 1662.It Ar random 1663The 1664.Ar random 1665option selects an address at random within the defined block of addresses. 1666.It Ar source-hash 1667The 1668.Ar source-hash 1669option uses a hash of the source address to determine the redirection address, 1670ensuring that the redirection address is always the same for a given source. 1671An optional key can be specified after this keyword either in hex or as a 1672string; by default 1673.Xr pfctl 8 1674randomly generates a key for source-hash every time the 1675ruleset is reloaded. 1676.It Ar round-robin 1677The 1678.Ar round-robin 1679option loops through the redirection address(es). 1680.Pp 1681When more than one redirection address is specified, 1682.Ar round-robin 1683is the only permitted pool type. 1684.It Ar static-port 1685With 1686.Ar nat 1687rules, the 1688.Ar static-port 1689option prevents 1690.Xr pf 4 1691from modifying the source port on TCP and UDP packets. 1692.El 1693.Pp 1694Additionally, the 1695.Ar sticky-address 1696option can be specified to help ensure that multiple connections from the 1697same source are mapped to the same redirection address. 1698This option can be used with the 1699.Ar random 1700and 1701.Ar round-robin 1702pool options. 1703Note that by default these associations are destroyed as soon as there are 1704no longer states which refer to them; in order to make the mappings last 1705beyond the lifetime of the states, increase the global options with 1706.Ar set timeout source-track 1707See 1708.Sx STATEFUL TRACKING OPTIONS 1709for more ways to control the source tracking. 1710.Sh STATEFUL INSPECTION 1711.Xr pf 4 1712is a stateful packet filter, which means it can track the state of 1713a connection. 1714Instead of passing all traffic to port 25, for instance, it is possible 1715to pass only the initial packet, and then begin to keep state. 1716Subsequent traffic will flow because the filter is aware of the connection. 1717.Pp 1718If a packet matches a 1719.Ar pass ... keep state 1720rule, the filter creates a state for this connection and automatically 1721lets pass all subsequent packets of that connection. 1722.Pp 1723Before any rules are evaluated, the filter checks whether the packet 1724matches any state. 1725If it does, the packet is passed without evaluation of any rules. 1726.Pp 1727States are removed after the connection is closed or has timed out. 1728.Pp 1729This has several advantages. 1730Comparing a packet to a state involves checking its sequence numbers. 1731If the sequence numbers are outside the narrow windows of expected 1732values, the packet is dropped. 1733This prevents spoofing attacks, such as when an attacker sends packets with 1734a fake source address/port but does not know the connection's sequence 1735numbers. 1736.Pp 1737Also, looking up states is usually faster than evaluating rules. 1738If there are 50 rules, all of them are evaluated sequentially in O(n). 1739Even with 50000 states, only 16 comparisons are needed to match a 1740state, since states are stored in a binary search tree that allows 1741searches in O(log2 n). 1742.Pp 1743For instance: 1744.Bd -literal -offset indent 1745block all 1746pass out proto tcp from any to any flags S/SA keep state 1747pass in proto tcp from any to any port 25 flags S/SA keep state 1748.Ed 1749.Pp 1750This ruleset blocks everything by default. 1751Only outgoing connections and incoming connections to port 25 are allowed. 1752The initial packet of each connection has the SYN 1753flag set, will be passed and creates state. 1754All further packets of these connections are passed if they match a state. 1755.Pp 1756By default, packets coming in and out of any interface can match a state, 1757but it is also possible to change that behaviour by assigning states to a 1758single interface or a group of interfaces. 1759.Pp 1760The default policy is specified by the 1761.Ar state-policy 1762global option, but this can be adjusted on a per-rule basis by adding one 1763of the 1764.Ar if-bound , 1765.Ar group-bound 1766or 1767.Ar floating 1768keywords to the 1769.Ar keep state 1770option. 1771For example, if a rule is defined as: 1772.Bd -literal -offset indent 1773pass out on ppp from any to 10.12/16 keep state (group-bound) 1774.Ed 1775.Pp 1776A state created on ppp0 would match packets an all PPP interfaces, 1777but not packets flowing through fxp0 or any other interface. 1778.Pp 1779Keeping rules 1780.Ar floating 1781is the more flexible option when the firewall is in a dynamic routing 1782environment. 1783However, this has some security implications since a state created by one 1784trusted network could allow potentially hostile packets coming in from other 1785interfaces. 1786.Pp 1787Specifying 1788.Ar flags S/SA 1789restricts state creation to the initial SYN 1790packet of the TCP handshake. 1791One can also be less restrictive, and allow state creation from 1792intermediate 1793.Pq non-SYN 1794packets. 1795This will cause 1796.Xr pf 4 1797to synchronize to existing connections, for instance 1798if one flushes the state table. 1799.Pp 1800For UDP, which is stateless by nature, 1801.Ar keep state 1802will create state as well. 1803UDP packets are matched to states using only host addresses and ports. 1804.Pp 1805ICMP messages fall into two categories: ICMP error messages, which always 1806refer to a TCP or UDP packet, are matched against the referred to connection. 1807If one keeps state on a TCP connection, and an ICMP source quench message 1808referring to this TCP connection arrives, it will be matched to the right 1809state and get passed. 1810.Pp 1811For ICMP queries, 1812.Ar keep state 1813creates an ICMP state, and 1814.Xr pf 4 1815knows how to match ICMP replies to states. 1816For example, 1817.Bd -literal -offset indent 1818pass out inet proto icmp all icmp-type echoreq keep state 1819.Ed 1820.Pp 1821allows echo requests (such as those created by 1822.Xr ping 8 ) 1823out, creates state, and matches incoming echo replies correctly to states. 1824.Pp 1825Note: 1826.Ar nat , binat No and Ar rdr 1827rules implicitly create state for connections. 1828.Sh STATE MODULATION 1829Much of the security derived from TCP is attributable to how well the 1830initial sequence numbers (ISNs) are chosen. 1831Some popular stack implementations choose 1832.Em very 1833poor ISNs and thus are normally susceptible to ISN prediction exploits. 1834By applying a 1835.Ar modulate state 1836rule to a TCP connection, 1837.Xr pf 4 1838will create a high quality random sequence number for each connection 1839endpoint. 1840.Pp 1841The 1842.Ar modulate state 1843directive implicitly keeps state on the rule and is 1844only applicable to TCP connections. 1845.Pp 1846For instance: 1847.Bd -literal -offset indent 1848block all 1849pass out proto tcp from any to any modulate state 1850pass in proto tcp from any to any port 25 flags S/SA modulate state 1851.Ed 1852.Pp 1853There are two caveats associated with state modulation: 1854A 1855.Ar modulate state 1856rule can not be applied to a pre-existing but unmodulated connection. 1857Such an application would desynchronize TCP's strict 1858sequencing between the two endpoints. 1859Instead, 1860.Xr pf 4 1861will treat the 1862.Ar modulate state 1863modifier as a 1864.Ar keep state 1865modifier and the pre-existing connection will be inferred without 1866the protection conferred by modulation. 1867.Pp 1868The other caveat affects currently modulated states when the state table 1869is lost (firewall reboot, flushing the state table, etc...). 1870.Xr pf 4 1871will not be able to infer a connection again after the state table flushes 1872the connection's modulator. 1873When the state is lost, the connection may be left dangling until the 1874respective endpoints time out the connection. 1875It is possible on a fast local network for the endpoints to start an ACK 1876storm while trying to resynchronize after the loss of the modulator. 1877Using a 1878.Ar flags S/SA 1879modifier on 1880.Ar modulate state 1881rules between fast networks is suggested to prevent ACK storms. 1882.Sh SYN PROXY 1883By default, 1884.Xr pf 4 1885passes packets that are part of a 1886.Xr tcp 4 1887handshake between the endpoints. 1888The 1889.Ar synproxy state 1890option can be used to cause 1891.Xr pf 4 1892itself to complete the handshake with the active endpoint, perform a handshake 1893with the passive endpoint, and then forward packets between the endpoints. 1894.Pp 1895No packets are sent to the passive endpoint before the active endpoint has 1896completed the handshake, hence so-called SYN floods with spoofed source 1897addresses will not reach the passive endpoint, as the sender can't complete the 1898handshake. 1899.Pp 1900The proxy is transparent to both endpoints, they each see a single 1901connection from/to the other endpoint. 1902.Xr pf 4 1903chooses random initial sequence numbers for both handshakes. 1904Once the handshakes are completed, the sequence number modulators 1905(see previous section) are used to translate further packets of the 1906connection. 1907Hence, 1908.Ar synproxy state 1909includes 1910.Ar modulate state 1911and 1912.Ar keep state . 1913.Pp 1914Rules with 1915.Ar synproxy 1916will not work if 1917.Xr pf 4 1918operates on a 1919.Xr bridge 4 . 1920.Pp 1921Example: 1922.Bd -literal -offset indent 1923pass in proto tcp from any to any port www flags S/SA synproxy state 1924.Ed 1925.Sh STATEFUL TRACKING OPTIONS 1926All three of 1927.Ar keep state , 1928.Ar modulate state 1929and 1930.Ar synproxy state 1931support the following options: 1932.Pp 1933.Bl -tag -width xxxx -compact 1934.It Ar max <number> 1935Limits the number of concurrent states the rule may create. 1936When this limit is reached, further packets matching the rule that would 1937create state are dropped, until existing states time out. 1938.It Ar no-sync 1939Prevent state changes for states created by this rule from appearing on the 1940.Xr pfsync 4 1941interface. 1942.It Ar <timeout> <seconds> 1943Changes the timeout values used for states created by this rule. 1944For a list of all valid timeout names, see 1945.Sx OPTIONS 1946above. 1947.El 1948.Pp 1949Multiple options can be specified, separated by commas: 1950.Bd -literal -offset indent 1951pass in proto tcp from any to any \e 1952 port www flags S/SA keep state \e 1953 (max 100, source-track rule, max-src-nodes 75, \e 1954 max-src-states 3, tcp.established 60, tcp.closing 5) 1955.Ed 1956.Pp 1957When the 1958.Ar source-track 1959keyword is specified, the number of states per source IP is tracked. 1960.Pp 1961.Bl -tag -width xxxx -compact 1962.It Ar source-track rule 1963The maximum number of states created by this rule is limited by the rule's 1964.Ar max-src-nodes 1965and 1966.Ar max-src-state 1967options. 1968Only state entries created by this particular rule count toward the rule's 1969limits. 1970.It Ar source-track global 1971The number of states created by all rules that use this option is limited. 1972Each rule can specify different 1973.Ar max-src-nodes 1974and 1975.Ar max-src-states 1976options, however state entries created by any participating rule count towards 1977each individual rule's limits. 1978.El 1979.Pp 1980The following limits can be set: 1981.Pp 1982.Bl -tag -width xxxx -compact 1983.It Ar max-src-nodes <number> 1984Limits the maximum number of source addresses which can simultaneously 1985have state table entries. 1986.It Ar max-src-states <number> 1987Limits the maximum number of simultaneous state entries that a single 1988source address can create with this rule. 1989.El 1990.Pp 1991For stateful TCP connections, limits on established connections (connections 1992which have completed the TCP 3-way handshake) can also be enforced 1993per source IP. 1994.Pp 1995.Bl -tag -width xxxx -compact 1996.It Ar max-src-conn <number> 1997Limits the maximum number of simultaneous TCP connections which have 1998completed the 3-way handshake that a single host can make. 1999.It Ar max-src-conn-rate <number> / <seconds> 2000Limit the rate of new connections over a time interval. 2001The connection rate is an approximation calculated as a moving average. 2002.El 2003.Pp 2004Because the 3-way handshake ensures that the source address is not being 2005spoofed, more aggressive action can be taken based on these limits. 2006With the 2007.Ar overload <table> 2008state option, source IP addresses which hit either of the limits on 2009established connections will be added to the named table. 2010This table can be used in the ruleset to block further activity from 2011the offending host, redirect it to a tarpit process, or restrict its 2012bandwidth. 2013.Pp 2014The optional 2015.Ar flush 2016keyword kills all states created by the matching rule which originate 2017from the host which exceeds these limits. 2018The 2019.Ar global 2020modifier to the flush command kills all states originating from the 2021offending host, regardless of which rule created the state. 2022.Pp 2023For example, the following rules will protect the webserver against 2024hosts making more than 100 connections in 10 seconds. 2025Any host which connects faster than this rate will have its address added 2026to the <bad_hosts> table and have all states originating from it flushed. 2027Any new packets arriving from this host will be dropped unconditionally 2028by the block rule. 2029.Bd -literal -offset indent 2030block quick from <bad_hosts> 2031pass in on $ext_if proto tcp to $webserver port www flags S/SA keep state \e 2032 (max-src-conn-rate 100/10, overload <bad_hosts> flush global) 2033.Ed 2034.Sh OPERATING SYSTEM FINGERPRINTING 2035Passive OS Fingerprinting is a mechanism to inspect nuances of a TCP 2036connection's initial SYN packet and guess at the host's operating system. 2037Unfortunately these nuances are easily spoofed by an attacker so the 2038fingerprint is not useful in making security decisions. 2039But the fingerprint is typically accurate enough to make policy decisions 2040upon. 2041.Pp 2042The fingerprints may be specified by operating system class, by 2043version, or by subtype/patchlevel. 2044The class of an operating system is typically the vendor or genre 2045and would be OpenBSD for the 2046.Xr pf 4 2047firewall itself. 2048The version of the oldest available OpenBSD release on the main ftp site 2049would be 2.6 and the fingerprint would be written 2050.Pp 2051.Dl \&"OpenBSD 2.6\&" 2052.Pp 2053The subtype of an operating system is typically used to describe the 2054patchlevel if that patch led to changes in the TCP stack behavior. 2055In the case of OpenBSD, the only subtype is for a fingerprint that was 2056normalized by the 2057.Ar no-df 2058scrub option and would be specified as 2059.Pp 2060.Dl \&"OpenBSD 3.3 no-df\&" 2061.Pp 2062Fingerprints for most popular operating systems are provided by 2063.Xr pf.os 5 . 2064Once 2065.Xr pf 4 2066is running, a complete list of known operating system fingerprints may 2067be listed by running: 2068.Pp 2069.Dl # pfctl -so 2070.Pp 2071Filter rules can enforce policy at any level of operating system specification 2072assuming a fingerprint is present. 2073Policy could limit traffic to approved operating systems or even ban traffic 2074from hosts that aren't at the latest service pack. 2075.Pp 2076The 2077.Ar unknown 2078class can also be used as the fingerprint which will match packets for 2079which no operating system fingerprint is known. 2080.Pp 2081Examples: 2082.Bd -literal -offset indent 2083pass out proto tcp from any os OpenBSD keep state 2084block out proto tcp from any os Doors 2085block out proto tcp from any os "Doors PT" 2086block out proto tcp from any os "Doors PT SP3" 2087block out from any os "unknown" 2088pass on lo0 proto tcp from any os "OpenBSD 3.3 lo0" keep state 2089.Ed 2090.Pp 2091Operating system fingerprinting is limited only to the TCP SYN packet. 2092This means that it will not work on other protocols and will not match 2093a currently established connection. 2094.Pp 2095Caveat: operating system fingerprints are occasionally wrong. 2096There are three problems: an attacker can trivially craft his packets to 2097appear as any operating system he chooses; 2098an operating system patch could change the stack behavior and no fingerprints 2099will match it until the database is updated; 2100and multiple operating systems may have the same fingerprint. 2101.Sh BLOCKING SPOOFED TRAFFIC 2102"Spoofing" is the faking of IP addresses, typically for malicious 2103purposes. 2104The 2105.Ar antispoof 2106directive expands to a set of filter rules which will block all 2107traffic with a source IP from the network(s) directly connected 2108to the specified interface(s) from entering the system through 2109any other interface. 2110.Pp 2111For example, the line 2112.Bd -literal -offset indent 2113antispoof for lo0 2114.Ed 2115.Pp 2116expands to 2117.Bd -literal -offset indent 2118block drop in on ! lo0 inet from 127.0.0.1/8 to any 2119block drop in on ! lo0 inet6 from ::1 to any 2120.Ed 2121.Pp 2122For non-loopback interfaces, there are additional rules to block incoming 2123packets with a source IP address identical to the interface's IP(s). 2124For example, assuming the interface wi0 had an IP address of 10.0.0.1 and a 2125netmask of 255.255.255.0, 2126the line 2127.Bd -literal -offset indent 2128antispoof for wi0 inet 2129.Ed 2130.Pp 2131expands to 2132.Bd -literal -offset indent 2133block drop in on ! wi0 inet from 10.0.0.0/24 to any 2134block drop in inet from 10.0.0.1 to any 2135.Ed 2136.Pp 2137Caveat: Rules created by the 2138.Ar antispoof 2139directive interfere with packets sent over loopback interfaces 2140to local addresses. 2141One should pass these explicitly. 2142.Sh FRAGMENT HANDLING 2143The size of IP datagrams (packets) can be significantly larger than the 2144maximum transmission unit (MTU) of the network. 2145In cases when it is necessary or more efficient to send such large packets, 2146the large packet will be fragmented into many smaller packets that will each 2147fit onto the wire. 2148Unfortunately for a firewalling device, only the first logical fragment will 2149contain the necessary header information for the subprotocol that allows 2150.Xr pf 4 2151to filter on things such as TCP ports or to perform NAT. 2152.Pp 2153Besides the use of 2154.Ar scrub 2155rules as described in 2156.Sx TRAFFIC NORMALIZATION 2157above, there are three options for handling fragments in the packet filter. 2158.Pp 2159One alternative is to filter individual fragments with filter rules. 2160If no 2161.Ar scrub 2162rule applies to a fragment, it is passed to the filter. 2163Filter rules with matching IP header parameters decide whether the 2164fragment is passed or blocked, in the same way as complete packets 2165are filtered. 2166Without reassembly, fragments can only be filtered based on IP header 2167fields (source/destination address, protocol), since subprotocol header 2168fields are not available (TCP/UDP port numbers, ICMP code/type). 2169The 2170.Ar fragment 2171option can be used to restrict filter rules to apply only to 2172fragments, but not complete packets. 2173Filter rules without the 2174.Ar fragment 2175option still apply to fragments, if they only specify IP header fields. 2176For instance, the rule 2177.Bd -literal -offset indent 2178pass in proto tcp from any to any port 80 2179.Ed 2180.Pp 2181never applies to a fragment, even if the fragment is part of a TCP 2182packet with destination port 80, because without reassembly this information 2183is not available for each fragment. 2184This also means that fragments cannot create new or match existing 2185state table entries, which makes stateful filtering and address 2186translation (NAT, redirection) for fragments impossible. 2187.Pp 2188It's also possible to reassemble only certain fragments by specifying 2189source or destination addresses or protocols as parameters in 2190.Ar scrub 2191rules. 2192.Pp 2193In most cases, the benefits of reassembly outweigh the additional 2194memory cost, and it's recommended to use 2195.Ar scrub 2196rules to reassemble 2197all fragments via the 2198.Ar fragment reassemble 2199modifier. 2200.Pp 2201The memory allocated for fragment caching can be limited using 2202.Xr pfctl 8 . 2203Once this limit is reached, fragments that would have to be cached 2204are dropped until other entries time out. 2205The timeout value can also be adjusted. 2206.Pp 2207Currently, only IPv4 fragments are supported and IPv6 fragments 2208are blocked unconditionally. 2209.Sh ANCHORS 2210Besides the main ruleset, 2211.Xr pfctl 8 2212can load rulesets into 2213.Ar anchor 2214attachment points. 2215An 2216.Ar anchor 2217is a container that can hold rules, address tables, and other anchors. 2218.Pp 2219An 2220.Ar anchor 2221has a name which specifies the path where 2222.Xr pfctl 8 2223can be used to access the anchor to perform operations on it, such as 2224attaching child anchors to it or loading rules into it. 2225Anchors may be nested, with components separated by 2226.Sq / 2227characters, similar to how file system hierarchies are laid out. 2228The main ruleset is actually the default anchor, so filter and 2229translation rules, for example, may also be contained in any anchor. 2230.Pp 2231An anchor can reference another 2232.Ar anchor 2233attachment point 2234using the following kinds 2235of rules: 2236.Bl -tag -width xxxx 2237.It Ar nat-anchor <name> 2238Evaluates the 2239.Ar nat 2240rules in the specified 2241.Ar anchor . 2242.It Ar rdr-anchor <name> 2243Evaluates the 2244.Ar rdr 2245rules in the specified 2246.Ar anchor . 2247.It Ar binat-anchor <name> 2248Evaluates the 2249.Ar binat 2250rules in the specified 2251.Ar anchor . 2252.It Ar anchor <name> 2253Evaluates the filter rules in the specified 2254.Ar anchor . 2255.It Ar load anchor <name> from <file> 2256Loads the rules from the specified file into the 2257anchor 2258.Ar name . 2259.El 2260.Pp 2261When evaluation of the main ruleset reaches an 2262.Ar anchor 2263rule, 2264.Xr pf 4 2265will proceed to evaluate all rules specified in that anchor. 2266.Pp 2267Matching filter and translation rules in anchors with the 2268.Ar quick 2269option are final and abort the evaluation of the rules in other 2270anchors 2271and the main ruleset. 2272.Pp 2273.Ar anchor 2274rules are evaluated relative to the anchor in which they are contained. 2275For example, all 2276.Ar anchor 2277rules specified in the main ruleset will reference anchor 2278attachment points underneath the main ruleset, and 2279.Ar anchor 2280rules specified in a file loaded from a 2281.Ar load anchor 2282rule will be attached under that anchor point. 2283.Pp 2284Rules may be contained in 2285.Ar anchor 2286attachment points which do not contain any rules when the main ruleset 2287is loaded, and later such anchors can be manipulated through 2288.Xr pfctl 8 2289without reloading the main ruleset or other anchors. 2290For example, 2291.Bd -literal -offset indent 2292ext_if = \&"kue0\&" 2293block on $ext_if all 2294anchor spam 2295pass out on $ext_if all keep state 2296pass in on $ext_if proto tcp from any \e 2297 to $ext_if port smtp keep state 2298.Ed 2299.Pp 2300blocks all packets on the external interface by default, then evaluates 2301all rules in the 2302.Ar anchor 2303named "spam", and finally passes all outgoing connections and 2304incoming connections to port 25. 2305.Bd -literal -offset indent 2306# echo \&"block in quick from 1.2.3.4 to any\&" \&| \e 2307 pfctl -a spam -f - 2308.Ed 2309.Pp 2310This loads a single rule into the 2311.Ar anchor , 2312which blocks all packets from a specific address. 2313.Pp 2314The anchor can also be populated by adding a 2315.Ar load anchor 2316rule after the 2317.Ar anchor 2318rule: 2319.Bd -literal -offset indent 2320anchor spam 2321load anchor spam from "/etc/pf-spam.conf" 2322.Ed 2323.Pp 2324When 2325.Xr pfctl 8 2326loads 2327.Nm pf.conf , 2328it will also load all the rules from the file 2329.Pa /etc/pf-spam.conf 2330into the anchor. 2331.Pp 2332Optionally, 2333.Ar anchor 2334rules can specify the parameter's 2335direction, interface, address family, protocol and source/destination 2336address/port 2337using the same syntax as filter rules. 2338When parameters are used, the 2339.Ar anchor 2340rule is only evaluated for matching packets. 2341This allows conditional evaluation of anchors, like: 2342.Bd -literal -offset indent 2343block on $ext_if all 2344anchor spam proto tcp from any to any port smtp 2345pass out on $ext_if all keep state 2346pass in on $ext_if proto tcp from any to $ext_if port smtp keep state 2347.Ed 2348.Pp 2349The rules inside 2350.Ar anchor 2351spam are only evaluated for 2352.Ar tcp 2353packets with destination port 25. 2354Hence, 2355.Bd -literal -offset indent 2356# echo \&"block in quick from 1.2.3.4 to any" \&| \e 2357 pfctl -a spam -f - 2358.Ed 2359.Pp 2360will only block connections from 1.2.3.4 to port 25. 2361.Pp 2362Anchors may end with the asterisk 2363.Pq Sq * 2364character, which signifies that all anchors attached at that point 2365should be evaluated in the alphabetical ordering of their anchor name. 2366For example, 2367.Bd -literal -offset indent 2368anchor "spam/*" 2369.Ed 2370.Pp 2371will evaluate each rule in each anchor attached to the 2372.Li spam 2373anchor. 2374Note that it will only evaluate anchors that are directly attached to the 2375.Li spam 2376anchor, and will not descend to evaluate anchors recursively. 2377.Pp 2378Since anchors are evaluated relative to the anchor in which they are 2379contained, there is a mechanism for accessing the parent and ancestor 2380anchors of a given anchor. 2381Similar to file system path name resolution, if the sequence 2382.Dq .. 2383appears as an anchor path component, the parent anchor of the current 2384anchor in the path evaluation at that point will become the new current 2385anchor. 2386As an example, consider the following: 2387.Bd -literal -offset indent 2388# echo ' anchor "spam/allowed" ' | pfctl -f - 2389# echo -e ' anchor "../banned" \en pass' | \e 2390 pfctl -a spam/allowed -f - 2391.Ed 2392.Pp 2393Evaluation of the main ruleset will lead into the 2394.Li spam/allowed 2395anchor, which will evaluate the rules in the 2396.Li spam/banned 2397anchor, if any, before finally evaluating the 2398.Ar pass 2399rule. 2400.Pp 2401Since the parser specification for anchor names is a string, any 2402reference to an anchor name containing solidus 2403.Pq Sq / 2404characters will require double quote 2405.Pq Sq \&" 2406characters around the anchor name. 2407.Sh TRANSLATION EXAMPLES 2408This example maps incoming requests on port 80 to port 8080, on 2409which a daemon is running (because, for example, it is not run as root, 2410and therefore lacks permission to bind to port 80). 2411.Bd -literal 2412# use a macro for the interface name, so it can be changed easily 2413ext_if = \&"ne3\&" 2414 2415# map daemon on 8080 to appear to be on 80 2416rdr on $ext_if proto tcp from any to any port 80 -> 127.0.0.1 port 8080 2417.Ed 2418.Pp 2419If the 2420.Ar pass 2421modifier is given, packets matching the translation rule are passed without 2422inspecting the filter rules: 2423.Bd -literal 2424rdr pass on $ext_if proto tcp from any to any port 80 -> 127.0.0.1 \e 2425 port 8080 2426.Ed 2427.Pp 2428In the example below, vlan12 is configured as 192.168.168.1; 2429the machine translates all packets coming from 192.168.168.0/24 to 204.92.77.111 2430when they are going out any interface except vlan12. 2431This has the net effect of making traffic from the 192.168.168.0/24 2432network appear as though it is the Internet routable address 2433204.92.77.111 to nodes behind any interface on the router except 2434for the nodes on vlan12. 2435(Thus, 192.168.168.1 can talk to the 192.168.168.0/24 nodes.) 2436.Bd -literal 2437nat on ! vlan12 from 192.168.168.0/24 to any -> 204.92.77.111 2438.Ed 2439.Pp 2440In the example below, the machine sits between a fake internal 144.19.74.* 2441network, and a routable external IP of 204.92.77.100. 2442The 2443.Ar no nat 2444rule excludes protocol AH from being translated. 2445.Bd -literal 2446# NO NAT 2447no nat on $ext_if proto ah from 144.19.74.0/24 to any 2448nat on $ext_if from 144.19.74.0/24 to any -> 204.92.77.100 2449.Ed 2450.Pp 2451In the example below, packets bound for one specific server, as well as those 2452generated by the sysadmins are not proxied; all other connections are. 2453.Bd -literal 2454# NO RDR 2455no rdr on $int_if proto { tcp, udp } from any to $server port 80 2456no rdr on $int_if proto { tcp, udp } from $sysadmins to any port 80 2457rdr on $int_if proto { tcp, udp } from any to any port 80 -> 127.0.0.1 \e 2458 port 80 2459.Ed 2460.Pp 2461This longer example uses both a NAT and a redirection. 2462The external interface has the address 157.161.48.183. 2463On the internal interface, we are running 2464.Xr ftp-proxy 8 , 2465listening for outbound ftp sessions captured to port 8021. 2466.Bd -literal 2467# NAT 2468# Translate outgoing packets' source addresses (any protocol). 2469# In this case, any address but the gateway's external address is mapped. 2470nat on $ext_if inet from ! ($ext_if) to any -> ($ext_if) 2471 2472# NAT PROXYING 2473# Map outgoing packets' source port to an assigned proxy port instead of 2474# an arbitrary port. 2475# In this case, proxy outgoing isakmp with port 500 on the gateway. 2476nat on $ext_if inet proto udp from any port = isakmp to any -> ($ext_if) \e 2477 port 500 2478 2479# BINAT 2480# Translate outgoing packets' source address (any protocol). 2481# Translate incoming packets' destination address to an internal machine 2482# (bidirectional). 2483binat on $ext_if from 10.1.2.150 to any -> $ext_if 2484 2485# RDR 2486# Translate incoming packets' destination addresses. 2487# As an example, redirect a TCP and UDP port to an internal machine. 2488rdr on $ext_if inet proto tcp from any to ($ext_if) port 8080 \e 2489 -> 10.1.2.151 port 22 2490rdr on $ext_if inet proto udp from any to ($ext_if) port 8080 \e 2491 -> 10.1.2.151 port 53 2492 2493# RDR 2494# Translate outgoing ftp control connections to send them to localhost 2495# for proxying with ftp-proxy(8) running on port 8021. 2496rdr on $int_if proto tcp from any to any port 21 -> 127.0.0.1 port 8021 2497.Ed 2498.Pp 2499In this example, a NAT gateway is set up to translate internal addresses 2500using a pool of public addresses (192.0.2.16/28) and to redirect 2501incoming web server connections to a group of web servers on the internal 2502network. 2503.Bd -literal 2504# NAT LOAD BALANCE 2505# Translate outgoing packets' source addresses using an address pool. 2506# A given source address is always translated to the same pool address by 2507# using the source-hash keyword. 2508nat on $ext_if inet from any to any -> 192.0.2.16/28 source-hash 2509 2510# RDR ROUND ROBIN 2511# Translate incoming web server connections to a group of web servers on 2512# the internal network. 2513rdr on $ext_if proto tcp from any to any port 80 \e 2514 -> { 10.1.2.155, 10.1.2.160, 10.1.2.161 } round-robin 2515.Ed 2516.Sh FILTER EXAMPLES 2517.Bd -literal 2518# The external interface is kue0 2519# (157.161.48.183, the only routable address) 2520# and the private network is 10.0.0.0/8, for which we are doing NAT. 2521 2522# use a macro for the interface name, so it can be changed easily 2523ext_if = \&"kue0\&" 2524 2525# normalize all incoming traffic 2526scrub in on $ext_if all fragment reassemble 2527 2528# block and log everything by default 2529block return log on $ext_if all 2530 2531# block anything coming from source we have no back routes for 2532block in from no-route to any 2533 2534# block and log outgoing packets that do not have our address as source, 2535# they are either spoofed or something is misconfigured (NAT disabled, 2536# for instance), we want to be nice and do not send out garbage. 2537block out log quick on $ext_if from ! 157.161.48.183 to any 2538 2539# silently drop broadcasts (cable modem noise) 2540block in quick on $ext_if from any to 255.255.255.255 2541 2542# block and log incoming packets from reserved address space and invalid 2543# addresses, they are either spoofed or misconfigured, we cannot reply to 2544# them anyway (hence, no return-rst). 2545block in log quick on $ext_if from { 10.0.0.0/8, 172.16.0.0/12, \e 2546 192.168.0.0/16, 255.255.255.255/32 } to any 2547 2548# ICMP 2549 2550# pass out/in certain ICMP queries and keep state (ping) 2551# state matching is done on host addresses and ICMP id (not type/code), 2552# so replies (like 0/0 for 8/0) will match queries 2553# ICMP error messages (which always refer to a TCP/UDP packet) are 2554# handled by the TCP/UDP states 2555pass on $ext_if inet proto icmp all icmp-type 8 code 0 keep state 2556 2557# UDP 2558 2559# pass out all UDP connections and keep state 2560pass out on $ext_if proto udp all keep state 2561 2562# pass in certain UDP connections and keep state (DNS) 2563pass in on $ext_if proto udp from any to any port domain keep state 2564 2565# TCP 2566 2567# pass out all TCP connections and modulate state 2568pass out on $ext_if proto tcp all modulate state 2569 2570# pass in certain TCP connections and keep state (SSH, SMTP, DNS, IDENT) 2571pass in on $ext_if proto tcp from any to any port { ssh, smtp, domain, \e 2572 auth } flags S/SA keep state 2573 2574# pass in data mode connections for ftp-proxy running on this host. 2575# (see ftp-proxy(8) for details) 2576pass in on $ext_if proto tcp from any to 157.161.48.183 port >= 49152 \e 2577 flags S/SA keep state 2578 2579# Do not allow Windows 9x SMTP connections since they are typically 2580# a viral worm. Alternately we could limit these OSes to 1 connection each. 2581block in on $ext_if proto tcp from any os {"Windows 95", "Windows 98"} \e 2582 to any port smtp 2583 2584# Packet Tagging 2585 2586# three interfaces: $int_if, $ext_if, and $wifi_if (wireless). NAT is 2587# being done on $ext_if for all outgoing packets. tag packets in on 2588# $int_if and pass those tagged packets out on $ext_if. all other 2589# outgoing packets (i.e., packets from the wireless network) are only 2590# permitted to access port 80. 2591 2592pass in on $int_if from any to any tag INTNET keep state 2593pass in on $wifi_if from any to any keep state 2594 2595block out on $ext_if from any to any 2596pass out quick on $ext_if tagged INTNET keep state 2597pass out on $ext_if proto tcp from any to any port 80 keep state 2598 2599# tag incoming packets as they are redirected to spamd(8). use the tag 2600# to pass those packets through the packet filter. 2601 2602rdr on $ext_if inet proto tcp from <spammers> to port smtp \e 2603 tag SPAMD -> 127.0.0.1 port spamd 2604 2605block in on $ext_if 2606pass in on $ext_if inet proto tcp tagged SPAMD keep state 2607.Ed 2608.Sh GRAMMAR 2609Syntax for 2610.Nm 2611in BNF: 2612.Bd -literal 2613line = ( option | pf-rule | nat-rule | binat-rule | rdr-rule | 2614 antispoof-rule | altq-rule | queue-rule | anchor-rule | 2615 trans-anchors | load-anchors | table-rule ) 2616 2617option = "set" ( [ "timeout" ( timeout | "{" timeout-list "}" ) ] | 2618 [ "optimization" [ "default" | "normal" | 2619 "high-latency" | "satellite" | 2620 "aggressive" | "conservative" ] ] 2621 [ "limit" ( limit-item | "{" limit-list "}" ) ] | 2622 [ "loginterface" ( interface-name | "none" ) ] | 2623 [ "block-policy" ( "drop" | "return" ) ] | 2624 [ "state-policy" ( "if-bound" | "group-bound" | 2625 "floating" ) ] 2626 [ "require-order" ( "yes" | "no" ) ] 2627 [ "fingerprints" filename ] | 2628 [ "debug" ( "none" | "urgent" | "misc" | "loud" ) ] ) 2629 2630pf-rule = action [ ( "in" | "out" ) ] 2631 [ "log" | "log-all" ] [ "quick" ] 2632 [ "on" ifspec ] [ route ] [ af ] [ protospec ] 2633 hosts [ filteropt-list ] 2634 2635filteropt-list = filteropt-list filteropt | filteropt 2636filteropt = user | group | flags | icmp-type | icmp6-type | tos | 2637 ( "keep" | "modulate" | "synproxy" ) "state" 2638 [ "(" state-opts ")" ] | 2639 "fragment" | "no-df" | "min-ttl" number | 2640 "max-mss" number | "random-id" | "reassemble tcp" | 2641 fragmentation | "allow-opts" | 2642 "label" string | "tag" string | [ ! ] "tagged" string 2643 "queue" ( string | "(" string [ [ "," ] string ] ")" ) | 2644 "probability" number"%" 2645 2646nat-rule = [ "no" ] "nat" [ "pass" ] [ "on" ifspec ] [ af ] 2647 [ protospec ] hosts [ "tag" string ] [ "tagged" string ] 2648 [ "->" ( redirhost | "{" redirhost-list "}" ) 2649 [ portspec ] [ pooltype ] [ "static-port" ] ] 2650 2651binat-rule = [ "no" ] "binat" [ "pass" ] [ "on" interface-name ] 2652 [ af ] [ "proto" ( proto-name | proto-number ) ] 2653 "from" address [ "/" mask-bits ] "to" ipspec 2654 [ "tag" string ] [ "tagged" string ] 2655 [ "->" address [ "/" mask-bits ] ] 2656 2657rdr-rule = [ "no" ] "rdr" [ "pass" ] [ "on" ifspec ] [ af ] 2658 [ protospec ] hosts [ "tag" string ] [ "tagged" string ] 2659 [ "->" ( redirhost | "{" redirhost-list "}" ) 2660 [ portspec ] [ pooltype ] ] 2661 2662antispoof-rule = "antispoof" [ "log" ] [ "quick" ] 2663 "for" ( interface-name | "{" interface-list "}" ) 2664 [ af ] [ "label" string ] 2665 2666table-rule = "table" "<" string ">" [ tableopts-list ] 2667tableopts-list = tableopts-list tableopts | tableopts 2668tableopts = "persist" | "const" | "file" string | 2669 "{" [ tableaddr-list ] "}" 2670tableaddr-list = tableaddr-list [ "," ] tableaddr-spec | tableaddr-spec 2671tableaddr-spec = [ "!" ] tableaddr [ "/" mask-bits ] 2672tableaddr = hostname | ipv4-dotted-quad | ipv6-coloned-hex | 2673 interface-name | "self" 2674 2675altq-rule = "altq on" interface-name queueopts-list 2676 "queue" subqueue 2677queue-rule = "queue" string [ "on" interface-name ] queueopts-list 2678 subqueue 2679 2680anchor-rule = "anchor" string [ ( "in" | "out" ) ] [ "on" ifspec ] 2681 [ af ] [ "proto" ] [ protospec ] [ hosts ] 2682 2683trans-anchors = ( "nat-anchor" | "rdr-anchor" | "binat-anchor" ) string 2684 [ "on" ifspec ] [ af ] [ "proto" ] [ protospec ] [ hosts ] 2685 2686load-anchor = "load anchor" string "from" filename 2687 2688queueopts-list = queueopts-list queueopts | queueopts 2689queueopts = [ "bandwidth" bandwidth-spec ] | 2690 [ "qlimit" number ] | [ "tbrsize" number ] | 2691 [ "priority" number ] | [ schedulers ] 2692schedulers = ( cbq-def | priq-def | hfsc-def ) 2693bandwidth-spec = "number" ( "b" | "Kb" | "Mb" | "Gb" | "%" ) 2694 2695action = "pass" | "block" [ return ] | [ "no" ] "scrub" 2696return = "drop" | "return" | "return-rst" [ "( ttl" number ")" ] | 2697 "return-icmp" [ "(" icmpcode ["," icmp6code ] ")" ] | 2698 "return-icmp6" [ "(" icmp6code ")" ] 2699icmpcode = ( icmp-code-name | icmp-code-number ) 2700icmp6code = ( icmp6-code-name | icmp6-code-number ) 2701 2702ifspec = ( [ "!" ] interface-name ) | "{" interface-list "}" 2703interface-list = [ "!" ] interface-name [ [ "," ] interface-list ] 2704route = "fastroute" | 2705 ( "route-to" | "reply-to" | "dup-to" ) 2706 ( routehost | "{" routehost-list "}" ) 2707 [ pooltype ] 2708af = "inet" | "inet6" 2709 2710protospec = "proto" ( proto-name | proto-number | 2711 "{" proto-list "}" ) 2712proto-list = ( proto-name | proto-number ) [ [ "," ] proto-list ] 2713 2714hosts = "all" | 2715 "from" ( "any" | "no-route" | "self" | host | 2716 "{" host-list "}" | "route" string ) [ port ] [ os ] 2717 "to" ( "any" | "no-route" | "self" | host | 2718 "{" host-list "}" | "route" string ) [ port ] 2719 2720ipspec = "any" | host | "{" host-list "}" 2721host = [ "!" ] ( address [ "/" mask-bits ] | "<" string ">" ) 2722redirhost = address [ "/" mask-bits ] 2723routehost = ( interface-name [ address [ "/" mask-bits ] ] ) 2724address = ( interface-name | "(" interface-name ")" | hostname | 2725 ipv4-dotted-quad | ipv6-coloned-hex ) 2726host-list = host [ [ "," ] host-list ] 2727redirhost-list = redirhost [ [ "," ] redirhost-list ] 2728routehost-list = routehost [ [ "," ] routehost-list ] 2729 2730port = "port" ( unary-op | binary-op | "{" op-list "}" ) 2731portspec = "port" ( number | name ) [ ":" ( "*" | number | name ) ] 2732os = "os" ( os-name | "{" os-list "}" ) 2733user = "user" ( unary-op | binary-op | "{" op-list "}" ) 2734group = "group" ( unary-op | binary-op | "{" op-list "}" ) 2735 2736unary-op = [ "=" | "!=" | "<" | "<=" | ">" | ">=" ] 2737 ( name | number ) 2738binary-op = number ( "<>" | "><" | ":" ) number 2739op-list = ( unary-op | binary-op ) [ [ "," ] op-list ] 2740 2741os-name = operating-system-name 2742os-list = os-name [ [ "," ] os-list ] 2743 2744flags = "flags" [ flag-set ] "/" flag-set 2745flag-set = [ "F" ] [ "S" ] [ "R" ] [ "P" ] [ "A" ] [ "U" ] [ "E" ] 2746 [ "W" ] 2747 2748icmp-type = "icmp-type" ( icmp-type-code | "{" icmp-list "}" ) 2749icmp6-type = "icmp6-type" ( icmp-type-code | "{" icmp-list "}" ) 2750icmp-type-code = ( icmp-type-name | icmp-type-number ) 2751 [ "code" ( icmp-code-name | icmp-code-number ) ] 2752icmp-list = icmp-type-code [ [ "," ] icmp-list ] 2753 2754tos = "tos" ( "lowdelay" | "throughput" | "reliability" | 2755 [ "0x" ] number ) 2756 2757state-opts = state-opt [ [ "," ] state-opts ] 2758state-opt = ( "max" number | "no-sync" | timeout | 2759 "source-track" [ ( "rule" | "global" ) ] | 2760 "max-src-nodes" number | "max-src-states" number | 2761 "max-src-conn" number | 2762 "max-src-conn-rate" number "/" number | 2763 "overload" "<" string ">" [ "flush" ] | 2764 "if-bound" | "group-bound" | "floating" ) 2765 2766fragmentation = [ "fragment reassemble" | "fragment crop" | 2767 "fragment drop-ovl" ] 2768 2769timeout-list = timeout [ [ "," ] timeout-list ] 2770timeout = ( "tcp.first" | "tcp.opening" | "tcp.established" | 2771 "tcp.closing" | "tcp.finwait" | "tcp.closed" | 2772 "udp.first" | "udp.single" | "udp.multiple" | 2773 "icmp.first" | "icmp.error" | 2774 "other.first" | "other.single" | "other.multiple" | 2775 "frag" | "interval" | "src.track" | 2776 "adaptive.start" | "adaptive.end" ) number 2777 2778limit-list = limit-item [ [ "," ] limit-list ] 2779limit-item = ( "states" | "frags" | "src-nodes" ) number 2780 2781pooltype = ( "bitmask" | "random" | 2782 "source-hash" [ ( hex-key | string-key ) ] | 2783 "round-robin" ) [ sticky-address ] 2784 2785subqueue = string | "{" queue-list "}" 2786queue-list = string [ [ "," ] string ] 2787cbq-def = "cbq" [ "(" cbq-opt [ [ "," ] cbq-opt ] ")" ] 2788priq-def = "priq" [ "(" priq-opt [ [ "," ] priq-opt ] ")" ] 2789hfsc-def = "hfsc" [ "(" hfsc-opt [ [ "," ] hfsc-opt ] ")" ] 2790cbq-opt = ( "default" | "borrow" | "red" | "ecn" | "rio" ) 2791priq-opt = ( "default" | "red" | "ecn" | "rio" ) 2792hfsc-opt = ( "default" | "red" | "ecn" | "rio" | 2793 linkshare-sc | realtime-sc | upperlimit-sc ) 2794linkshare-sc = "linkshare" sc-spec 2795realtime-sc = "realtime" sc-spec 2796upperlimit-sc = "upperlimit" sc-spec 2797sc-spec = ( bandwidth-spec | 2798 "(" bandwidth-spec number bandwidth-spec ")" ) 2799.Ed 2800.Sh FILES 2801.Bl -tag -width "/etc/protocols" -compact 2802.It Pa /etc/hosts 2803Host name database. 2804.It Pa /etc/pf.conf 2805Default location of the ruleset file. 2806.It Pa /etc/pf.os 2807Default location of OS fingerprints. 2808.It Pa /etc/protocols 2809Protocol name database. 2810.It Pa /etc/services 2811Service name database. 2812.It Pa /usr/share/examples/pf 2813Example rulesets. 2814.El 2815.Sh SEE ALSO 2816.Xr icmp 4 , 2817.Xr icmp6 4 , 2818.Xr ip 4 , 2819.Xr ip6 4 , 2820.Xr pf 4 , 2821.Xr pfsync 4 , 2822.Xr route 4 , 2823.Xr tcp 4 , 2824.Xr udp 4 , 2825.Xr hosts 5 , 2826.Xr pf.os 5 , 2827.Xr protocols 5 , 2828.Xr services 5 , 2829.Xr ftp-proxy 8 , 2830.Xr pfctl 8 , 2831.Xr pflogd 8 , 2832.Xr route 8 2833.Sh HISTORY 2834The 2835.Nm 2836file format first appeared in 2837.Ox 3.0 . 2838