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