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