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