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