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