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