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