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