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