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