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