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