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