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