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