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