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