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