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