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