1 .\" $OpenBSD: pf.conf.5,v 1.291 2004/02/04 19:38:30 jmc Exp $
2 .\" $DragonFly: src/usr.sbin/pfctl/pf.conf.5,v 1.9 2007/09/02 19:30:48 swildner Exp $
4 .\" Copyright (c) 2002, Daniel Hartmeier
5 .\" All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
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.
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.
36 .Nd packet filter configuration file
40 packet filter modifies, drops or passes packets according to rules or
41 definitions specified in
44 There are seven types of statements in
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
53 Tables provide a mechanism for increasing the performance and flexibility of
54 rules with large numbers of source or destination addresses.
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.
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
65 .It Cm Packet Filtering
66 Stateful and stateless packet filtering provides rule-based blocking or
74 the types of statements should be grouped and appear in
76 in the order shown above, as this matches the operation of the underlying
77 packet filtering engine.
80 enforces this order (see
88 macros can be defined that will later be expanded in context.
89 Macro names must start with a letter, and may contain letters, digits
91 Macro names may not be reserved words (for example
95 Macros are not expanded inside quotes.
98 .Bd -literal -offset indent
100 all_ifs = \&"{\&" $ext_if lo0 \&"}\&"
101 pass out on $ext_if from any to any keep state
102 pass in on $ext_if proto tcp from any to any port 25 keep state
105 Tables are named structures which can hold a collection of addresses and
107 Lookups against tables in
109 are relatively fast, making a single rule with tables much more efficient,
111 processor usage and memory consumption, than a large number of rules which
112 differ only in IP address (either created explicitly or automatically by rule
115 Tables can be used as the source or destination of filter rules,
119 translation rules such as
123 (see below for details on the various rule types).
124 Tables can also be used for the redirect address of
128 rules and in the routing options of filter rules, but only for
132 Tables can be defined with any of the following
135 As with macros, reserved words may not be used as table names.
136 .Bl -tag -width "manually"
138 Persistent tables can be manually created with the
144 before or after the ruleset has been loaded.
146 Table definitions can be placed directly in this file, and loaded at the
147 same time as other rules are loaded, atomically.
148 Table definitions inside
152 statement, and are especially useful to define non-persistent tables.
153 The contents of a pre-existing table defined without a list of addresses
154 to initialize it is not altered when
157 A table initialized with the empty list,
159 will be cleared on load.
162 Tables may be defined with the following two attributes:
163 .Bl -tag -width persist
167 flag forces the kernel to keep the table even when no rules refer to it.
168 If the flag is not set, the kernel will automatically remove the table
169 when the last rule referring to it is flushed.
173 flag prevents the user from altering the contents of the table once it
177 can be used to add or remove addresses from the table at any time, even
184 .Bd -literal -offset indent
185 table <private> const { 10/8, 172.16/12, 192.168/16 }
186 table <badhosts> persist
187 block on fxp0 from { <private>, <badhosts> } to any
190 creates a table called private, to hold RFC 1918 private network
191 blocks, and a table called badhosts, which is initially empty.
192 A filter rule is set up to block all traffic coming from addresses listed in
194 The private table cannot have its contents changed and the badhosts table
195 will exist even when no active filter rules reference it.
196 Addresses may later be added to the badhosts table, so that traffic from
197 these hosts can be blocked by using
198 .Bd -literal -offset indent
199 # pfctl -t badhosts -Tadd 204.92.77.111
202 A table can also be initialized with an address list specified in one or more
203 external files, using the following syntax:
204 .Bd -literal -offset indent
205 table <spam> persist file \&"/etc/spammers\&" file \&"/etc/openrelays\&"
206 block on fxp0 from <spam> to any
213 list IP addresses, one per line.
214 Any lines beginning with a # are treated as comments and ignored.
215 In addition to being specified by IP address, hosts may also be
216 specified by their hostname.
217 When the resolver is called to add a hostname to a table,
219 resulting IPv4 and IPv6 addresses are placed into the table.
220 IP addresses can also be entered in a table by specifying a valid interface
223 keyword, in which case all addresses assigned to the interface(s) will be
227 may be tuned for various situations using the
233 .Bl -tag -width interval -compact
235 Interval between purging expired states and fragments.
237 Seconds before an unassembled fragment is expired.
239 Length of time to retain a source tracking entry after the last state
243 When a packet matches a stateful connection, the seconds to live for the
244 connection will be updated to that of the
246 which corresponds to the connection state.
247 Each packet which matches this state will reset the TTL.
248 Tuning these values may improve the performance of the
249 firewall at the risk of dropping valid idle connections.
251 .Bl -tag -width xxxx -compact
253 The state after the first packet.
255 The state before the destination host ever sends a packet.
256 .It Ar tcp.established
257 The fully established state.
259 The state after the first FIN has been sent.
261 The state after both FINs have been exchanged and the connection is closed.
262 Some hosts (notably web servers on Solaris) send TCP packets even after closing
268 can prevent blocking of such packets.
270 The state after one endpoint sends an RST.
273 ICMP and UDP are handled in a fashion similar to TCP, but with a much more
274 limited set of states:
276 .Bl -tag -width xxxx -compact
278 The state after the first packet.
280 The state if the source host sends more than one packet but the destination
281 host has never sent one back.
283 The state if both hosts have sent packets.
285 The state after the first packet.
287 The state after an ICMP error came back in response to an ICMP packet.
290 Other protocols are handled similarly to UDP:
292 .Bl -tag -width xxxx -compact
295 .It Ar other.multiple
298 Timeout values can be reduced adaptively as the number of state table
301 .Bl -tag -width xxxx -compact
302 .It Ar adaptive.start
303 When the number of state entries exceeds this value, adaptive scaling
305 All timeout values are scaled linearly with factor
306 (adaptive.end - number of states) / (adaptive.end - adaptive.start).
308 When reaching this number of state entries, all timeout values become
309 zero, effectively purging all state entries immediately.
310 This value is used to define the scale factor, it should not actually
311 be reached (set a lower state limit, see below).
314 These values can be defined both globally and for each rule.
315 When used on a per-rule basis, the values relate to the number of
316 states created by the rule, otherwise to the total number of
320 .Bd -literal -offset indent
321 set timeout tcp.first 120
322 set timeout tcp.established 86400
323 set timeout { adaptive.start 6000, adaptive.end 12000 }
324 set limit states 10000
327 With 9000 state table entries, the timeout values are scaled to 50%
328 (tcp.first 60, tcp.established 43200).
330 .It Ar set loginterface
331 Enable collection of packet and byte count statistics for the given interface.
332 These statistics can be viewed using
333 .Bd -literal -offset indent
339 collects statistics on the interface named dc0:
340 .Bd -literal -offset indent
344 One can disable the loginterface using:
345 .Bd -literal -offset indent
346 set loginterface none
350 Sets hard limits on the memory pools used by the packet filter.
353 for an explanation of memory pools.
356 .Bd -literal -offset indent
357 set limit states 20000
360 sets the maximum number of entries in the memory pool used by state table
361 entries (generated by
365 .Bd -literal -offset indent
366 set limit frags 20000
369 sets the maximum number of entries in the memory pool used for fragment
370 reassembly (generated by
374 .Bd -literal -offset indent
375 set limit src-nodes 2000
378 sets the maximum number of entries in the memory pool used for tracking
379 source IP addresses (generated by the
385 These can be combined:
386 .Bd -literal -offset indent
387 set limit { states 20000, frags 20000, src-nodes 2000 }
390 .It Ar set optimization
391 Optimize the engine for one of the following network environments:
393 .Bl -tag -width xxxx -compact
395 A normal network environment.
396 Suitable for almost all networks.
398 A high-latency environment (such as a satellite connection).
403 Aggressively expire connections.
404 This can greatly reduce the memory usage of the firewall at the cost of
405 dropping idle connections early.
407 Extremely conservative settings.
408 Avoid dropping legitimate connections at the
409 expense of greater memory utilization (possibly much greater on a busy
410 network) and slightly increased processor utilization.
414 .Bd -literal -offset indent
415 set optimization aggressive
418 .It Ar set block-policy
421 option sets the default behaviour for the packet
425 .Bl -tag -width xxxxxxxx -compact
427 Packet is silently dropped.
429 A TCP RST is returned for blocked TCP packets,
430 an ICMP UNREACHABLE is returned for blocked UDP packets,
431 and all other packets are silently dropped.
435 .Bd -literal -offset indent
436 set block-policy return
438 .It Ar set state-policy
441 option sets the default behaviour for states:
443 .Bl -tag -width group-bound -compact
445 States are bound to interface.
447 States are bound to interface group (i.e. ppp)
449 States can match packets on any interfaces (the default).
453 .Bd -literal -offset indent
454 set state-policy if-bound
456 .It Ar set require-order
459 enforces an ordering of the statement types in the ruleset to:
465 Setting this option to
467 disables this enforcement.
468 There may be non-trivial and non-obvious implications to an out of
470 Consider carefully before disabling the order enforcement.
471 .It Ar set fingerprints
472 Load fingerprints of known operating systems from the given filename.
473 By default fingerprints of known operating systems are automatically
478 but can be overridden via this option.
479 Setting this option may leave a small period of time where the fingerprints
480 referenced by the currently active ruleset are inconsistent until the new
481 ruleset finishes loading.
485 .Dl set fingerprints \&"/etc/pf.os.devel\&"
490 to one of the following:
492 .Bl -tag -width xxxxxxxxxxxx -compact
494 Don't generate debug messages.
496 Generate debug messages only for serious errors.
498 Generate debug messages for various errors.
500 Generate debug messages for common conditions.
503 .Sh TRAFFIC NORMALIZATION
504 Traffic normalization is used to sanitize packet content in such
505 a way that there are no ambiguities in packet interpretation on
507 The normalizer does IP fragment reassembly to prevent attacks
508 that confuse intrusion detection systems by sending overlapping
510 Packet normalization is invoked with the
515 has the following options:
520 bit from a matching IP packet.
521 Some operating systems are known to generate fragmented packets with the
524 This is particularly true with NFS.
526 will drop such fragmented
532 Unfortunately some operating systems also generate their
534 packets with a zero IP identification field.
537 bit on packets with a zero IP ID may cause deleterious results if an
538 upstream router later fragments the packet.
541 modifier (see below) is recommended in combination with the
543 modifier to ensure unique IP identifiers.
544 .It Ar min-ttl <number>
545 Enforces a minimum TTL for matching IP packets.
546 .It Ar max-mss <number>
547 Enforces a maximum MSS for matching TCP packets.
549 Replaces the IP identification field with random values to compensate
550 for predictable values generated by many hosts.
551 This option only applies to outgoing packets that are not fragmented
552 after the optional fragment reassembly.
553 .It Ar fragment reassemble
556 rules, fragments can be reassembled by normalization.
557 In this case, fragments are buffered until they form a complete
558 packet, and only the completed packet is passed on to the filter.
559 The advantage is that filter rules have to deal only with complete
560 packets, and can ignore fragments.
561 The drawback of caching fragments is the additional memory cost.
562 But the full reassembly method is the only method that currently works
564 This is the default behavior of a
566 rule if no fragmentation modifier is supplied.
568 The default fragment reassembly method is expensive, hence the option
572 will track the fragments and cache a small range descriptor.
573 Duplicate fragments are dropped and overlaps are cropped.
574 Thus data will only occur once on the wire with ambiguities resolving to
575 the first occurrence.
577 .Ar fragment reassemble
578 modifier, fragments are not buffered, they are passed as soon as they
582 reassembly mechanism does not yet work with NAT.
584 .It Ar fragment drop-ovl
585 This option is similar to the
587 modifier except that all overlapping or duplicate fragments will be
588 dropped, and all further corresponding fragments will be
590 .It Ar reassemble tcp
591 Statefully normalizes TCP connections.
592 .Ar scrub reassemble tcp
593 rules may not have the direction (in/out) specified.
595 performs the following normalizations:
597 .Bl -tag -width timeout -compact
599 Neither side of the connection is allowed to reduce their IP TTL.
600 An attacker may send a packet such that it reaches the firewall, affects
601 the firewall state, and expires before reaching the destination host.
603 will raise the TTL of all packets back up to the highest value seen on
605 .It timeout modulation
606 Modern TCP stacks will send a timestamp on every TCP packet and echo
607 the other endpoint's timestamp back to them.
608 Many operating systems will merely start the timestamp at zero when
609 first booted, and increment it several times a second.
610 The uptime of the host can be deduced by reading the timestamp and multiplying
612 Also observing several different timestamps can be used to count hosts
614 And spoofing TCP packets into a connection requires knowing or guessing
616 Timestamps merely need to be monotonically increasing and not derived off a
621 to modulate the TCP timestamps with a random number.
626 .Bd -literal -offset indent
627 scrub in on $ext_if all fragment reassemble
630 Packets can be assigned to queues for the purpose of bandwidth
632 At least two declarations are required to configure queues, and later
633 any packet filtering rule can reference the defined queues by name.
634 During the filtering component of
638 name is where any packets from
640 rules will be queued, while for
642 rules it specifies where any resulting ICMP or TCP RST
643 packets should be queued.
646 defines the algorithm used to decide which packets get delayed, dropped, or
647 sent out immediately.
653 Class Based Queueing.
655 attached to an interface build a tree, thus each
657 can have further child
659 Each queue can have a
665 mainly controls the time packets take to get sent out, while
667 has primarily effects on throughput.
671 are flat attached to the interface, thus,
673 cannot have further child
679 assigned, ranging from 0 to 15.
686 Hierarchical Fair Service Curve.
688 attached to an interface build a tree, thus each
690 can have further child
692 Each queue can have a
698 mainly controls the time packets take to get sent out, while
700 has primarily effects on throughput.
703 The interfaces on which queueing should be activated are declared using
708 has the following keywords:
711 Queueing is enabled on the named interface.
713 Specifies which queueing scheduler to use.
714 Currently supported values
717 for Class Based Queueing,
719 for Priority Queueing and
721 for the Hierarchical Fair Service Curve scheduler.
722 .It Ar bandwidth <bw>
723 The maximum bitrate for all queues on an
724 interface may be specified using the
727 The value can be specified as an absolute value or as a
728 percentage of the interface bandwidth.
729 When using an absolute value, the suffixes
735 are used to represent bits, kilobits, megabits, and
736 gigabits per second, respectively.
737 The value must not exceed the interface bandwidth.
740 is not specified, the interface bandwidth is used.
741 .It Ar qlimit <limit>
742 The maximum number of packets held in the queue.
744 .It Ar tbrsize <size>
745 Adjusts the size, in bytes, of the token bucket regulator.
746 If not specified, heuristics based on the
747 interface bandwidth are used to determine the size.
749 Defines a list of subqueues to create on an interface.
752 In the following example, the interface dc0
753 should queue up to 5 Mbit/s in four second-level queues using
754 Class Based Queueing.
755 Those four queues will be shown in a later example.
756 .Bd -literal -offset indent
757 altq on dc0 cbq bandwidth 5Mb queue { std, http, mail, ssh }
760 Once interfaces are activated for queueing using the
762 directive, a sequence of
764 directives may be defined.
765 The name associated with a
767 must match a queue defined in the
769 directive (e.g. mail), or, except for the
775 The following keywords can be used:
777 .It Ar on <interface>
778 Specifies the interface the queue operates on.
779 If not given, it operates on all matching interfaces.
780 .It Ar bandwidth <bw>
781 Specifies the maximum bitrate to be processed by the queue.
782 This value must not exceed the value of the parent
784 and can be specified as an absolute value or a percentage of the parent
788 scheduler does not support bandwidth specification.
789 .It Ar priority <level>
790 Between queues a priority level can be set.
795 the range is 0 to 7 and for
797 the range is 0 to 15.
798 The default for all is 1.
800 queues with a higher priority are always served first.
804 queues with a higher priority are preferred in the case of overload.
805 .It Ar qlimit <limit>
806 The maximum number of packets held in the queue.
812 can get additional parameters with
813 .Ar <scheduler> Ns Li (\& Ar <parameters> No ) .
814 Parameters are as follows:
817 Packets not matched by another queue are assigned to this one.
818 Exactly one default queue is required.
820 Enable RED (Random Early Detection) on this queue.
821 RED drops packets with a probability proportional to the average
824 Enables RIO on this queue.
825 RIO is RED with IN/OUT, thus running
826 RED two times more than RIO would achieve the same effect.
827 RIO is currently not supported in the GENERIC kernel.
829 Enables ECN (Explicit Congestion Notification) on this queue.
836 supports an additional option:
839 The queue can borrow bandwidth from the parent.
845 supports some additional options:
848 The minimum required bandwidth for the queue.
849 .It Ar upperlimit <sc>
850 The maximum allowed bandwidth for the queue.
851 .It Ar linkshare <sc>
852 The bandwidth share of a backlogged queue.
855 <sc> is an acronym for
858 The format for service curve specifications is
859 .Ar ( m1 , d , m2 ) .
861 controls the bandwidth assigned to the queue.
865 are optional and can be used to control the initial bandwidth assignment.
868 milliseconds the queue gets the bandwidth given as
870 afterwards the value given in
877 child queues can be specified as in an
879 declaration, thus building a tree of queues using a part of
880 their parent's bandwidth.
882 Packets can be assigned to queues based on filter rules by using the
887 is specified; when a second one is specified it will instead be used for
892 and for TCP ACKs with no data payload.
894 To continue the previous example, the examples below would specify the
896 queues, plus a few child queues.
899 sessions get priority over bulk transfers like
903 The queues may then be referenced by filtering rules (see
907 queue std bandwidth 10% cbq(default)
908 queue http bandwidth 60% priority 2 cbq(borrow red) \e
909 { employees, developers }
910 queue developers bandwidth 75% cbq(borrow)
911 queue employees bandwidth 15%
912 queue mail bandwidth 10% priority 0 cbq(borrow ecn)
913 queue ssh bandwidth 20% cbq(borrow) { ssh_interactive, ssh_bulk }
914 queue ssh_interactive priority 7
915 queue ssh_bulk priority 0
917 block return out on dc0 inet all queue std
918 pass out on dc0 inet proto tcp from $developerhosts to any port 80 \e
919 keep state queue developers
920 pass out on dc0 inet proto tcp from $employeehosts to any port 80 \e
921 keep state queue employees
922 pass out on dc0 inet proto tcp from any to any port 22 \e
923 keep state queue(ssh_bulk, ssh_interactive)
924 pass out on dc0 inet proto tcp from any to any port 25 \e
925 keep state queue mail
928 Translation rules modify either the source or destination address of the
929 packets associated with a stateful connection.
930 A stateful connection is automatically created to track packets matching
931 such a rule as long as they are not blocked by the filtering section of
933 The translation engine modifies the specified address and/or port in the
934 packet, recalculates IP, TCP and UDP checksums as necessary, and passes it to
935 the packet filter for evaluation.
937 Since translation occurs before filtering the filter
938 engine will see packets as they look after any
939 addresses and ports have been translated. Filter rules
940 will therefore have to filter based on the translated
941 address and port number.
942 Packets that match a translation rule are only automatically passed if
945 modifier is given, otherwise they are
952 The state entry created permits
954 to keep track of the original address for traffic associated with that state
955 and correctly direct return traffic for that connection.
957 Various types of translation are possible with pf:
962 rule specifies a bidirectional mapping between an external IP netblock
963 and an internal IP netblock.
967 rule specifies that IP addresses are to be changed as the packet
968 traverses the given interface.
969 This technique allows one or more IP addresses
970 on the translating host to support network traffic for a larger range of
971 machines on an "inside" network.
972 Although in theory any IP address can be used on the inside, it is strongly
973 recommended that one of the address ranges defined by RFC 1918 be used.
976 10.0.0.0 - 10.255.255.255 (all of net 10, i.e., 10/8)
977 172.16.0.0 - 172.31.255.255 (i.e., 172.16/12)
978 192.168.0.0 - 192.168.255.255 (i.e., 192.168/16)
981 The packet is redirected to another destination and possibly a
984 rules can optionally specify port ranges instead of single ports.
985 rdr ... port 2000:2999 -> ... port 4000
986 redirects ports 2000 to 2999 (inclusive) to port 4000.
987 rdr ... port 2000:2999 -> ... port 4000:*
988 redirects port 2000 to 4000, 2001 to 4001, ..., 2999 to 4999.
991 In addition to modifying the address, some translation rules may modify
992 source or destination ports for
996 connections; implicitly in the case of
998 rules and explicitly in the case of
1001 Port numbers are never translated with a
1005 For each packet processed by the translator, the translation rules are
1006 evaluated in sequential order, from first to last.
1007 The first matching rule decides what action is taken.
1011 option prefixed to a translation rule causes packets to remain untranslated,
1012 much in the same way as
1014 works in the packet filter (see below).
1015 If no rule matches the packet it is passed to the filter engine unmodified.
1017 Translation rules apply only to packets that pass through
1018 the specified interface, and if no interface is specified,
1019 translation is applied to packets on all interfaces.
1020 For instance, redirecting port 80 on an external interface to an internal
1021 web server will only work for connections originating from the outside.
1022 Connections to the address of the external interface from local hosts will
1023 not be redirected, since such packets do not actually pass through the
1025 Redirections cannot reflect packets back through the interface they arrive
1026 on, they can only be redirected to hosts connected to different interfaces
1027 or to the firewall itself.
1029 Note that redirecting external incoming connections to the loopback
1031 .Bd -literal -offset indent
1032 rdr on ne3 inet proto tcp to port 8025 -> 127.0.0.1 port 25
1035 will effectively allow an external host to connect to daemons
1036 bound solely to the loopback address, circumventing the traditional
1037 blocking of such connections on a real interface.
1038 Unless this effect is desired, any of the local non-loopback addresses
1039 should be used as redirection target instead, which allows external
1040 connections only to daemons bound to this address or not bound to
1044 .Sx TRANSLATION EXAMPLES
1046 .Sh PACKET FILTERING
1052 packets based on attributes of their layer 3 (see
1062 In addition, packets may also be
1063 assigned to queues for the purpose of bandwidth control.
1065 For each packet processed by the packet filter, the filter rules are
1066 evaluated in sequential order, from first to last.
1067 The last matching rule decides what action is taken.
1069 The following actions can be used in the filter:
1070 .Bl -tag -width xxxx
1072 The packet is blocked.
1073 There are a number of ways in which a
1075 rule can behave when blocking a packet.
1076 The default behaviour is to
1078 packets silently, however this can be overridden or made
1079 explicit either globally, by setting the
1081 option, or on a per-rule basis with one of the following options:
1083 .Bl -tag -width xxxx -compact
1085 The packet is silently dropped.
1087 This applies only to
1089 packets, and issues a TCP RST which closes the
1093 This causes ICMP messages to be returned for packets which match the rule.
1094 By default this is an ICMP UNREACHABLE message, however this
1095 can be overridden by specifying a message as a code or number.
1097 This causes a TCP RST to be returned for
1099 packets and an ICMP UNREACHABLE for UDP and other packets.
1102 Options returning packets have no effect if
1107 The packet is passed.
1110 If no rule matches the packet, the default action is
1113 To block everything by default and only pass packets
1114 that match explicit rules, one uses
1115 .Bd -literal -offset indent
1119 as the first filter rule.
1125 The rule parameters specify the packets to which a rule applies.
1126 A packet always comes in on, or goes out through, one interface.
1127 Most parameters are optional.
1128 If a parameter is specified, the rule only applies to packets with
1129 matching attributes.
1130 Certain parameters can be expressed as lists, in which case
1132 generates all needed rule combinations.
1133 .Bl -tag -width xxxx
1134 .It Ar in No or Ar out
1135 This rule applies to incoming or outgoing packets.
1140 are specified, the rule will match packets in both directions.
1142 In addition to the action specified, a log message is generated.
1143 All packets for that connection are logged, unless the
1148 options are specified, in which case only the
1149 packet that establishes the state is logged.
1156 The logged packets are sent to the
1159 This interface is monitored by the
1161 logging daemon, which dumps the logged packets to the file
1172 rules to force logging of all packets for a connection.
1175 packets are logged to
1178 If a packet matches a rule which has the
1180 option set, this rule
1181 is considered the last matching rule, and evaluation of subsequent rules
1183 .It Ar on <interface>
1184 This rule applies only to packets coming in on, or going out through, this
1185 particular interface.
1186 It is also possible to simply give the interface driver name, like ppp or fxp,
1187 to make the rule match packets flowing through a group of interfaces.
1189 This rule applies only to packets of this address family.
1190 Supported values are
1194 .It Ar proto <protocol>
1195 This rule applies only to packets of this protocol.
1196 Common protocols are
1202 For a list of all the protocol name to number mappings used by
1205 .Pa /etc/protocols .
1207 .Ar from <source> port <source> os <source>
1208 .Ar to <dest> port <dest>
1210 This rule applies only to packets with the specified source and destination
1211 addresses and ports.
1213 Addresses can be specified in CIDR notation (matching netblocks), as
1214 symbolic host names or interface names, or as any of the following keywords:
1216 .Bl -tag -width xxxxxxxxxxxx -compact
1220 Any address which is not currently routable.
1222 Any address that matches the given table.
1225 Interface names can have modifiers appended:
1227 .Bl -tag -width xxxxxxxxxxxx -compact
1229 Translates to the network(s) attached to the interface.
1231 Translates to the interface's broadcast address(es).
1233 Translates to the point to point interface's peer address(es).
1235 Do not include interface aliases.
1238 Host names may also have the
1240 option appended to restrict the name resolution to the first of each
1241 v4 and v6 address found.
1243 Host name resolution and interface to address translation are done at
1245 When the address of an interface (or host name) changes (under DHCP or PPP,
1246 for instance), the ruleset must be reloaded for the change to be reflected
1248 Surrounding the interface name (and optional modifiers) in parentheses
1249 changes this behaviour.
1250 When the interface name is surrounded by parentheses, the rule is
1251 automatically updated whenever the interface changes its address.
1252 The ruleset does not need to be reloaded.
1253 This is especially useful with
1256 Ports can be specified either by number or by name.
1257 For example, port 80 can be specified as
1259 For a list of all port name to number mappings used by
1264 Ports and ranges of ports are specified by using these operators:
1265 .Bd -literal -offset indent
1269 <= (less than or equal)
1271 >= (greater than or equal)
1272 : (range including boundaries)
1273 >< (range excluding boundaries)
1278 are binary operators (they take two arguments).
1281 .It Ar port 2000:2004
1283 .Sq all ports \(>= 2000 and \(<= 2004 ,
1284 hence ports 2000, 2001, 2002, 2003 and 2004.
1285 .It Ar port 2000 >< 2004
1287 .Sq all ports > 2000 and < 2004 ,
1288 hence ports 2001, 2002 and 2003.
1289 .It Ar port 2000 <> 2004
1291 .Sq all ports < 2000 or > 2004 ,
1292 hence ports 1-1999 and 2005-65535.
1295 The operating system of the source host can be specified in the case of TCP
1300 .Sx OPERATING SYSTEM FINGERPRINTING
1301 section for more information.
1303 The host, port and OS specifications are optional, as in the following examples:
1304 .Bd -literal -offset indent
1306 pass in from any to any
1307 pass in proto tcp from any port <= 1024 to any
1308 pass in proto tcp from any to any port 25
1309 pass in proto tcp from 10.0.0.0/8 port > 1024 \e
1310 to ! 10.1.2.3 port != ssh
1311 pass in proto tcp from any os "OpenBSD" flags S/SA
1314 This is equivalent to "from any to any".
1315 .It Ar group <group>
1318 this rule only applies to packets of sockets owned by the specified group.
1320 This rule only applies to packets of sockets owned by the specified user.
1321 For outgoing connections initiated from the firewall, this is the user
1322 that opened the connection.
1323 For incoming connections to the firewall itself, this is the user that
1324 listens on the destination port.
1325 For forwarded connections, where the firewall is not a connection endpoint,
1326 the user and group are
1329 All packets, both outgoing and incoming, of one connection are associated
1330 with the same user and group.
1331 Only TCP and UDP packets can be associated with users; for other protocols
1332 these parameters are ignored.
1334 User and group refer to the effective (as opposed to the real) IDs, in
1335 case the socket is created by a setuid/setgid process.
1336 User and group IDs are stored when a socket is created;
1337 when a process creates a listening socket as root (for instance, by
1338 binding to a privileged port) and subsequently changes to another
1339 user ID (to drop privileges), the credentials will remain root.
1341 User and group IDs can be specified as either numbers or names.
1342 The syntax is similar to the one for ports.
1345 matches packets of forwarded connections.
1347 can only be used with the operators
1351 Other constructs like
1354 Forwarded packets with unknown user and group ID match only rules
1355 that explicitly compare against
1363 does not match forwarded packets.
1364 The following example allows only selected users to open outgoing
1366 .Bd -literal -offset indent
1367 block out proto { tcp, udp } all
1368 pass out proto { tcp, udp } all \e
1369 user { < 1000, dhartmei } keep state
1371 .It Ar flags <a>/<b> | /<b>
1372 This rule only applies to TCP packets that have the flags
1376 Flags not specified in
1379 The flags are: (F)IN, (S)YN, (R)ST, (P)USH, (A)CK, (U)RG, (E)CE, and C(W)R.
1383 The other flags are ignored.
1385 Out of SYN and ACK, exactly SYN may be set.
1386 SYN, SYN+PSH and SYN+RST match, but SYN+ACK, ACK and ACK+RST do not.
1387 This is more restrictive than the previous example.
1389 If the first set is not specified, it defaults to none.
1390 All of SYN, FIN, RST and ACK must be unset.
1392 .It Ar icmp-type <type> code <code>
1393 .It Ar icmp6-type <type> code <code>
1394 This rule only applies to ICMP or ICMPv6 packets with the specified type
1396 This parameter is only valid for rules that cover protocols ICMP or
1398 The protocol and the ICMP type indicator (icmp-type or icmp6-type)
1401 By default, packets which contain IP options are blocked.
1406 rule, packets that pass the filter based on that rule (last matching)
1407 do so even if they contain IP options.
1408 For packets that match state, the rule that initially created the
1412 rule that is used when a packet does not match any rules does not
1414 .It Ar label <string>
1415 Adds a label (name) to the rule, which can be used to identify the rule.
1418 shows per-rule statistics for rules that have labels.
1420 The following macros can be used in labels:
1422 .Bl -tag -width $srcaddr -compact -offset indent
1426 The source IP address.
1428 The destination IP address.
1430 The source port specification.
1432 The destination port specification.
1440 .Bd -literal -offset indent
1441 ips = \&"{ 1.2.3.4, 1.2.3.5 }\&"
1442 pass in proto tcp from any to $ips \e
1443 port > 1023 label \&"$dstaddr:$dstport\&"
1447 .Bd -literal -offset indent
1448 pass in inet proto tcp from any to 1.2.3.4 \e
1449 port > 1023 label \&"1.2.3.4:>1023\&"
1450 pass in inet proto tcp from any to 1.2.3.5 \e
1451 port > 1023 label \&"1.2.3.5:>1023\&"
1454 The macro expansion for the
1456 directive occurs only at configuration file parse time, not during runtime.
1457 .It Ar queue <queue> | ( <queue> , <queue> )
1458 Packets matching this rule will be assigned to the specified queue.
1459 If two queues are given, packets which have a
1463 and TCP ACKs with no data payload will be assigned to the second one.
1469 .Bd -literal -offset indent
1470 pass in proto tcp to port 25 queue mail
1471 pass in proto tcp to port 22 queue(ssh_bulk, ssh_prio)
1474 Packets matching this rule will be tagged with the
1476 The tag acts as an internal marker that can be used to
1477 identify these packets later on.
1478 This can be used, for example, to provide trust between
1479 interfaces and to determine if packets have been
1480 processed by translation rules.
1483 meaning that the packet will be tagged even if the rule
1484 is not the last matching rule.
1485 Further matching rules can replace the tag with a
1486 new one but will not remove a previously applied tag.
1487 A packet is only ever assigned one tag at a time.
1491 keyword must also use
1495 .Ar synproxy state .
1496 Packet tagging can be done during
1501 rules in addition to filter rules.
1502 Tags take the same macros as labels (see above).
1503 .It Ar tagged <string>
1504 Used with filter rules to specify that packets must already
1505 be tagged with the given tag in order to match the rule.
1506 Inverse tag matching can also be done
1514 If a packet matches a rule with a route option set, the packet filter will
1515 route the packet according to the type of route option.
1516 When such a rule creates state, the route option is also applied to all
1517 packets matching the same connection.
1518 .Bl -tag -width xxxx
1522 option does a normal route lookup to find the next hop for the packet.
1526 option routes the packet to the specified interface with an optional address
1530 rule creates state, only packets that pass in the same direction as the
1531 filter rule specifies will be routed in this way.
1532 Packets passing in the opposite direction (replies) are not affected
1533 and are routed normally.
1537 option is similar to
1539 but routes packets that pass in the opposite direction (replies) to the
1540 specified interface.
1541 Opposite direction is only defined in the context of a state entry, and
1543 is useful only in rules that create state.
1544 It can be used on systems with multiple external connections to
1545 route all outgoing packets of a connection through the interface
1546 the incoming connection arrived through (symmetric routing enforcement).
1550 option creates a duplicate of the packet and routes it like
1552 The original packet gets routed as it normally would.
1559 rules, (as well as for the
1564 rule options) for which there is a single redirection address which has a
1565 subnet mask smaller than 32 for IPv4 or 128 for IPv6 (more than one IP
1566 address), a variety of different methods for assigning this address can be
1568 .Bl -tag -width xxxx
1572 option applies the network portion of the redirection address to the address
1573 to be modified (source with
1580 option selects an address at random within the defined block of addresses.
1584 option uses a hash of the source address to determine the redirection address,
1585 ensuring that the redirection address is always the same for a given source.
1586 An optional key can be specified after this keyword either in hex or as a
1589 randomly generates a key for source-hash every time the
1590 ruleset is reloaded.
1594 option loops through the redirection address(es).
1596 When more than one redirection address is specified,
1598 is the only permitted pool type.
1606 from modifying the source port on TCP and UDP packets.
1611 option can be specified to help ensure that multiple connections from the
1612 same source are mapped to the same redirection address.
1613 This option can be used with the
1618 Note that by default these associations are destroyed as soon as there are
1619 no longer states which refer to them; in order to make the mappings last
1620 beyond the lifetime of the states, increase the global options with
1621 .Ar set timeout source-track
1623 .Sx STATEFUL TRACKING OPTIONS
1624 for more ways to control the source tracking.
1625 .Sh STATEFUL INSPECTION
1627 is a stateful packet filter, which means it can track the state of
1629 Instead of passing all traffic to port 25, for instance, it is possible
1630 to pass only the initial packet, and then begin to keep state.
1631 Subsequent traffic will flow because the filter is aware of the connection.
1633 If a packet matches a
1634 .Ar pass ... keep state
1635 rule, the filter creates a state for this connection and automatically
1636 lets pass all subsequent packets of that connection.
1638 Before any rules are evaluated, the filter checks whether the packet
1640 If it does, the packet is passed without evaluation of any rules.
1642 States are removed after the connection is closed or has timed out.
1644 This has several advantages.
1645 Comparing a packet to a state involves checking its sequence numbers.
1646 If the sequence numbers are outside the narrow windows of expected
1647 values, the packet is dropped.
1648 This prevents spoofing attacks, such as when an attacker sends packets with
1649 a fake source address/port but does not know the connection's sequence
1652 Also, looking up states is usually faster than evaluating rules.
1653 If there are 50 rules, all of them are evaluated sequentially in O(n).
1654 Even with 50000 states, only 16 comparisons are needed to match a
1655 state, since states are stored in a binary search tree that allows
1656 searches in O(log2 n).
1659 .Bd -literal -offset indent
1661 pass out proto tcp from any to any flags S/SA keep state
1662 pass in proto tcp from any to any port 25 flags S/SA keep state
1665 This ruleset blocks everything by default.
1666 Only outgoing connections and incoming connections to port 25 are allowed.
1667 The initial packet of each connection has the SYN
1668 flag set, will be passed and creates state.
1669 All further packets of these connections are passed if they match a state.
1671 By default, packets coming in and out of any interface can match a state,
1672 but it is also possible to change that behaviour by assigning states to a
1673 single interface or a group of interfaces.
1675 The default policy is specified by the
1677 global option, but this can be adjusted on a per-rule basis by adding one
1686 For example, if a rule is defined as:
1687 .Bd -literal -offset indent
1688 pass out on ppp from any to 10.12/16 keep state (group-bound)
1691 A state created on ppp0 would match packets an all PPP interfaces,
1692 but not packets flowing through fxp0 or any other interface.
1696 is the more flexible option when the firewall is in a dynamic routing
1698 However, this has some security implications since a state created by one
1699 trusted network could allow potentially hostile packets coming in from other
1704 restricts state creation to the initial SYN
1705 packet of the TCP handshake.
1706 One can also be less restrictive, and allow state creation from
1712 to synchronize to existing connections, for instance
1713 if one flushes the state table.
1715 For UDP, which is stateless by nature,
1717 will create state as well.
1718 UDP packets are matched to states using only host addresses and ports.
1720 ICMP messages fall into two categories: ICMP error messages, which always
1721 refer to a TCP or UDP packet, are matched against the referred to connection.
1722 If one keeps state on a TCP connection, and an ICMP source quench message
1723 referring to this TCP connection arrives, it will be matched to the right
1724 state and get passed.
1728 creates an ICMP state, and
1730 knows how to match ICMP replies to states.
1732 .Bd -literal -offset indent
1733 pass out inet proto icmp all icmp-type echoreq keep state
1736 allows echo requests (such as those created by
1738 out, creates state, and matches incoming echo replies correctly to states.
1741 .Ar nat , binat No and Ar rdr
1742 rules implicitly create state for connections.
1743 .Sh STATE MODULATION
1744 Much of the security derived from TCP is attributable to how well the
1745 initial sequence numbers (ISNs) are chosen.
1746 Some popular stack implementations choose
1748 poor ISNs and thus are normally susceptible to ISN prediction exploits.
1751 rule to a TCP connection,
1753 will create a high quality random sequence number for each connection
1758 directive implicitly keeps state on the rule and is
1759 only applicable to TCP connections.
1762 .Bd -literal -offset indent
1764 pass out proto tcp from any to any modulate state
1765 pass in proto tcp from any to any port 25 flags S/SA modulate state
1768 There are two caveats associated with state modulation:
1771 rule can not be applied to a pre-existing but unmodulated connection.
1772 Such an application would desynchronize TCP's strict
1773 sequencing between the two endpoints.
1780 modifier and the pre-existing connection will be inferred without
1781 the protection conferred by modulation.
1783 The other caveat affects currently modulated states when the state table
1784 is lost (firewall reboot, flushing the state table, etc...).
1786 will not be able to infer a connection again after the state table flushes
1787 the connection's modulator.
1788 When the state is lost, the connection may be left dangling until the
1789 respective endpoints time out the connection.
1790 It is possible on a fast local network for the endpoints to start an ACK
1791 storm while trying to resynchronize after the loss of the modulator.
1796 rules between fast networks is suggested to prevent ACK storms.
1800 passes packets that are part of a
1802 handshake between the endpoints.
1805 option can be used to cause
1807 itself to complete the handshake with the active endpoint, perform a handshake
1808 with the passive endpoint, and then forward packets between the endpoints.
1810 No packets are sent to the passive endpoint before the active endpoint has
1811 completed the handshake, hence so-called SYN floods with spoofed source
1812 addresses will not reach the passive endpoint, as the sender can't complete the
1815 The proxy is transparent to both endpoints, they each see a single
1816 connection from/to the other endpoint.
1818 chooses random initial sequence numbers for both handshakes.
1819 Once the handshakes are completed, the sequence number modulators
1820 (see previous section) are used to translate further packets of the
1837 .Bd -literal -offset indent
1838 pass in proto tcp from any to any port www flags S/SA synproxy state
1840 .Sh STATEFUL TRACKING OPTIONS
1846 support the following options:
1848 .Bl -tag -width xxxx -compact
1850 Limits the number of concurrent states the rule may create.
1851 When this limit is reached, further packets matching the rule that would
1852 create state are dropped, until existing states time out.
1854 Prevent state changes for states created by this rule from appearing on the
1857 .It Ar <timeout> <seconds>
1858 Changes the timeout values used for states created by this rule.
1862 keyword is specified, the number of states per source IP is tracked.
1863 The following limits can be set:
1865 .Bl -tag -width xxxx -compact
1866 .It Ar max-src-nodes
1867 Limits the maximum number of source addresses which can simultaneously
1868 have state table entries.
1869 .It Ar max-src-states
1870 Limits the maximum number of simultaneous state entries that a single
1871 source address can create with this rule.
1873 For a list of all valid timeout names, see
1877 Multiple options can be specified, separated by commas:
1879 pass in proto tcp from any to any \e
1880 port www flags S/SA keep state \e
1881 (max 100, source-track rule, max-src-nodes 75, \e
1882 max-src-states 3, tcp.established 60, tcp.closing 5)
1885 .Sh OPERATING SYSTEM FINGERPRINTING
1886 Passive OS Fingerprinting is a mechanism to inspect nuances of a TCP
1887 connection's initial SYN packet and guess at the host's operating system.
1888 Unfortunately these nuances are easily spoofed by an attacker so the
1889 fingerprint is not useful in making security decisions.
1890 But the fingerprint is typically accurate enough to make policy decisions
1893 The fingerprints may be specified by operating system class, by
1894 version, or by subtype/patchlevel.
1895 The class of an operating system is typically the vendor or genre
1901 The version of the oldest available
1903 release on the main ftp site
1904 would be 2.6 and the fingerprint would be written
1906 .Dl \&"OpenBSD 2.6\&"
1908 The subtype of an operating system is typically used to describe the
1909 patchlevel if that patch led to changes in the TCP stack behavior.
1912 the only subtype is for a fingerprint that was
1915 scrub option and would be specified as
1917 .Dl \&"OpenBSD 3.3 no-df\&"
1919 Fingerprints for most popular operating systems are provided by
1923 is running, a complete list of known operating system fingerprints may
1924 be listed by running:
1928 Filter rules can enforce policy at any level of operating system specification
1929 assuming a fingerprint is present.
1930 Policy could limit traffic to approved operating systems or even ban traffic
1931 from hosts that aren't at the latest service pack.
1935 class can also be used as the fingerprint which will match packets for
1936 which no operating system fingerprint is known.
1939 .Bd -literal -offset indent
1940 pass out proto tcp from any os OpenBSD keep state
1941 block out proto tcp from any os Doors
1942 block out proto tcp from any os "Doors PT"
1943 block out proto tcp from any os "Doors PT SP3"
1944 block out from any os "unknown"
1945 pass on lo0 proto tcp from any os "OpenBSD 3.3 lo0" keep state
1948 Operating system fingerprinting is limited only to the TCP SYN packet.
1949 This means that it will not work on other protocols and will not match
1950 a currently established connection.
1952 Caveat: operating system fingerprints are occasionally wrong.
1953 There are three problems: an attacker can trivially craft his packets to
1954 appear as any operating system he chooses;
1955 an operating system patch could change the stack behavior and no fingerprints
1956 will match it until the database is updated;
1957 and multiple operating systems may have the same fingerprint.
1958 .Sh BLOCKING SPOOFED TRAFFIC
1959 "Spoofing" is the faking of IP addresses, typically for malicious
1963 directive expands to a set of filter rules which will block all
1964 traffic with a source IP from the network(s) directly connected
1965 to the specified interface(s) from entering the system through
1966 any other interface.
1968 For example, the line
1969 .Bd -literal -offset indent
1974 .Bd -literal -offset indent
1975 block drop in on ! lo0 inet from 127.0.0.1/8 to any
1976 block drop in on ! lo0 inet6 from ::1 to any
1979 For non-loopback interfaces, there are additional rules to block incoming
1980 packets with a source IP address identical to the interface's IP(s).
1981 For example, assuming the interface wi0 had an IP address of 10.0.0.1 and a
1982 netmask of 255.255.255.0,
1984 .Bd -literal -offset indent
1985 antispoof for wi0 inet
1989 .Bd -literal -offset indent
1990 block drop in on ! wi0 inet from 10.0.0.0/24 to any
1991 block drop in inet from 10.0.0.1 to any
1994 Caveat: Rules created by the
1996 directive interfere with packets sent over loopback interfaces
1998 One should pass these explicitly.
1999 .Sh FRAGMENT HANDLING
2000 The size of IP datagrams (packets) can be significantly larger than the
2001 maximum transmission unit (MTU) of the network.
2002 In cases when it is necessary or more efficient to send such large packets,
2003 the large packet will be fragmented into many smaller packets that will each
2005 Unfortunately for a firewalling device, only the first logical fragment will
2006 contain the necessary header information for the subprotocol that allows
2008 to filter on things such as TCP ports or to perform NAT.
2012 rules as described in
2013 .Sx TRAFFIC NORMALIZATION
2014 above, there are three options for handling fragments in the packet filter.
2016 One alternative is to filter individual fragments with filter rules.
2019 rule applies to a fragment, it is passed to the filter.
2020 Filter rules with matching IP header parameters decide whether the
2021 fragment is passed or blocked, in the same way as complete packets
2023 Without reassembly, fragments can only be filtered based on IP header
2024 fields (source/destination address, protocol), since subprotocol header
2025 fields are not available (TCP/UDP port numbers, ICMP code/type).
2028 option can be used to restrict filter rules to apply only to
2029 fragments, but not complete packets.
2030 Filter rules without the
2032 option still apply to fragments, if they only specify IP header fields.
2033 For instance, the rule
2034 .Bd -literal -offset indent
2035 pass in proto tcp from any to any port 80
2038 never applies to a fragment, even if the fragment is part of a TCP
2039 packet with destination port 80, because without reassembly this information
2040 is not available for each fragment.
2041 This also means that fragments cannot create new or match existing
2042 state table entries, which makes stateful filtering and address
2043 translation (NAT, redirection) for fragments impossible.
2045 It's also possible to reassemble only certain fragments by specifying
2046 source or destination addresses or protocols as parameters in
2050 In most cases, the benefits of reassembly outweigh the additional
2051 memory cost, and it's recommended to use
2054 all fragments via the
2055 .Ar fragment reassemble
2058 The memory allocated for fragment caching can be limited using
2060 Once this limit is reached, fragments that would have to be cached
2061 are dropped until other entries time out.
2062 The timeout value can also be adjusted.
2064 Currently, only IPv4 fragments are supported and IPv6 fragments
2065 are blocked unconditionally.
2066 .Sh ANCHORS AND NAMED RULESETS
2067 Besides the main ruleset,
2069 can load named rulesets into
2074 contains a list of named rulesets.
2077 has a name which specifies where
2079 can be used to attach sub-rulesets.
2080 A named ruleset contains filter and translation rules, like the
2082 The main ruleset can reference
2085 using the following kinds
2087 .Bl -tag -width xxxx
2088 .It Ar nat-anchor <name>
2091 rules of all named rulesets in the specified
2093 .It Ar rdr-anchor <name>
2096 rules of all named rulesets in the specified
2098 .It Ar binat-anchor <name>
2101 rules of all named rulesets in the specified
2103 .It Ar anchor <name>
2104 Evaluates the filter rules of all named rulesets in the specified
2106 .It Ar load anchor <name>:<ruleset> from <file>
2107 Loads the rules from the specified file into the named
2110 attached to the anchor
2114 When evaluation of the main ruleset reaches an
2118 will proceed to evaluate all rules specified in the
2119 named rulesets attached to that
2122 Matching filter rules in named rulesets with the
2124 option and matching translation rules are final and abort the
2125 evaluation of both the rules in the
2127 and the main ruleset.
2129 Only the main ruleset can contain
2135 contains more than one named ruleset, they are evaluated
2136 in the alphabetical order of their names.
2140 attachment points which do not contain any rules when the main ruleset
2141 is loaded, and later such named rulesets can be manipulated through
2143 without reloading the main ruleset.
2145 .Bd -literal -offset indent
2147 block on $ext_if all
2149 pass out on $ext_if all keep state
2150 pass in on $ext_if proto tcp from any \e
2151 to $ext_if port smtp keep state
2154 blocks all packets on the external interface by default, then evaluates
2157 named "spam", and finally passes all outgoing connections and
2158 incoming connections to port 25.
2159 .Bd -literal -offset indent
2160 # echo \&"block in quick from 1.2.3.4 to any\&" \&| \e
2161 pfctl -a spam:manual -f -
2164 loads a single ruleset containing a single rule into the
2166 which blocks all packets from a specific address.
2168 The named ruleset can also be populated by adding a
2173 .Bd -literal -offset indent
2175 load anchor spam:manual from "/etc/pf-spam.conf"
2182 it will also load all the rules from the file
2183 .Pa /etc/pf-spam.conf
2184 into the named ruleset.
2188 rules can specify the parameter's
2189 direction, interface, address family, protocol and source/destination
2191 using the same syntax as filter rules.
2192 When parameters are used, the
2194 rule is only evaluated for matching packets.
2195 This allows conditional evaluation of named rulesets, like:
2196 .Bd -literal -offset indent
2197 block on $ext_if all
2198 anchor spam proto tcp from any to any port smtp
2199 pass out on $ext_if all keep state
2200 pass in on $ext_if proto tcp from any to $ext_if port smtp keep state
2205 spam are only evaluated for
2207 packets with destination port 25.
2209 .Bd -literal -offset indent
2210 # echo \&"block in quick from 1.2.3.4 to any" \&| \e
2211 pfctl -a spam:manual -f -
2214 will only block connections from 1.2.3.4 to port 25.
2216 .Bl -tag -width "/etc/protocols" -compact
2220 Default location of the ruleset file.
2222 Default location of OS fingerprints.
2223 .It Pa /etc/protocols
2224 Protocol name database.
2225 .It Pa /etc/services
2226 Service name database.
2227 .It Pa /usr/share/examples/pf
2230 .Sh TRANSLATION EXAMPLES
2231 This example maps incoming requests on port 80 to port 8080, on
2232 which a daemon is running (because, for example, it is not run as root,
2233 and therefore lacks permission to bind to port 80).
2235 # use a macro for the interface name, so it can be changed easily
2238 # map daemon on 8080 to appear to be on 80
2239 rdr on $ext_if proto tcp from any to any port 80 -> 127.0.0.1 port 8080
2244 modifier is given, packets matching the translation rule are passed without
2245 inspecting the filter rules:
2247 rdr pass on $ext_if proto tcp from any to any port 80 -> 127.0.0.1 \e
2251 In the example below, vlan12 is configured as 192.168.168.1;
2252 the machine translates all packets coming from 192.168.168.0/24 to 204.92.77.111
2253 when they are going out any interface except vlan12.
2254 This has the net effect of making traffic from the 192.168.168.0/24
2255 network appear as though it is the Internet routable address
2256 204.92.77.111 to nodes behind any interface on the router except
2257 for the nodes on vlan12.
2258 (Thus, 192.168.168.1 can talk to the 192.168.168.0/24 nodes.)
2260 nat on ! vlan12 from 192.168.168.0/24 to any -> 204.92.77.111
2263 In the example below, the machine sits between a fake internal 144.19.74.*
2264 network, and a routable external IP of 204.92.77.100.
2267 rule excludes protocol AH from being translated.
2270 no nat on $ext_if proto ah from 144.19.74.0/24 to any
2271 nat on $ext_if from 144.19.74.0/24 to any -> 204.92.77.100
2274 In the example below, packets bound for one specific server, as well as those
2275 generated by the sysadmins are not proxied; all other connections are.
2278 no rdr on $int_if proto { tcp, udp } from any to $server port 80
2279 no rdr on $int_if proto { tcp, udp } from $sysadmins to any port 80
2280 rdr on $int_if proto { tcp, udp } from any to any port 80 -> 127.0.0.1 \e
2284 This longer example uses both a NAT and a redirection.
2285 The external interface has the address 157.161.48.183.
2286 On the internal interface, we are running
2288 listening for outbound ftp sessions captured to port 8021.
2291 # Translate outgoing packets' source addresses (any protocol).
2292 # In this case, any address but the gateway's external address is mapped.
2293 nat on $ext_if inet from ! ($ext_if) to any -> ($ext_if)
2296 # Map outgoing packets' source port to an assigned proxy port instead of
2297 # an arbitrary port.
2298 # In this case, proxy outgoing isakmp with port 500 on the gateway.
2299 nat on $ext_if inet proto udp from any port = isakmp to any -> ($ext_if) \e
2303 # Translate outgoing packets' source address (any protocol).
2304 # Translate incoming packets' destination address to an internal machine
2306 binat on $ext_if from 10.1.2.150 to any -> ($ext_if)
2309 # Translate incoming packets' destination addresses.
2310 # As an example, redirect a TCP and UDP port to an internal machine.
2311 rdr on $ext_if inet proto tcp from any to ($ext_if) port 8080 \e
2312 -> 10.1.2.151 port 22
2313 rdr on $ext_if inet proto udp from any to ($ext_if) port 8080 \e
2314 -> 10.1.2.151 port 53
2317 # Translate outgoing ftp control connections to send them to localhost
2318 # for proxying with ftp-proxy(8) running on port 8021.
2319 rdr on $int_if proto tcp from any to any port 21 -> 127.0.0.1 port 8021
2322 In this example, a NAT gateway is set up to translate internal addresses
2323 using a pool of public addresses (192.0.2.16/28) and to redirect
2324 incoming web server connections to a group of web servers on the internal
2328 # Translate outgoing packets' source addresses using an address pool.
2329 # A given source address is always translated to the same pool address by
2330 # using the source-hash keyword.
2331 nat on $ext_if inet from any to any -> 192.0.2.16/28 source-hash
2334 # Translate incoming web server connections to a group of web servers on
2335 # the internal network.
2336 rdr on $ext_if proto tcp from any to any port 80 \e
2337 -> { 10.1.2.155, 10.1.2.160, 10.1.2.161 } round-robin
2341 # The external interface is kue0
2342 # (157.161.48.183, the only routable address)
2343 # and the private network is 10.0.0.0/8, for which we are doing NAT.
2345 # use a macro for the interface name, so it can be changed easily
2348 # normalize all incoming traffic
2349 scrub in on $ext_if all fragment reassemble
2351 # block and log everything by default
2352 block return log on $ext_if all
2354 # block anything coming from source we have no back routes for
2355 block in from no-route to any
2357 # block and log outgoing packets that do not have our address as source,
2358 # they are either spoofed or something is misconfigured (NAT disabled,
2359 # for instance), we want to be nice and do not send out garbage.
2360 block out log quick on $ext_if from ! 157.161.48.183 to any
2362 # silently drop broadcasts (cable modem noise)
2363 block in quick on $ext_if from any to 255.255.255.255
2365 # block and log incoming packets from reserved address space and invalid
2366 # addresses, they are either spoofed or misconfigured, we cannot reply to
2367 # them anyway (hence, no return-rst).
2368 block in log quick on $ext_if from { 10.0.0.0/8, 172.16.0.0/12, \e
2369 192.168.0.0/16, 255.255.255.255/32 } to any
2373 # pass out/in certain ICMP queries and keep state (ping)
2374 # state matching is done on host addresses and ICMP id (not type/code),
2375 # so replies (like 0/0 for 8/0) will match queries
2376 # ICMP error messages (which always refer to a TCP/UDP packet) are
2377 # handled by the TCP/UDP states
2378 pass on $ext_if inet proto icmp all icmp-type 8 code 0 keep state
2382 # pass out all UDP connections and keep state
2383 pass out on $ext_if proto udp all keep state
2385 # pass in certain UDP connections and keep state (DNS)
2386 pass in on $ext_if proto udp from any to any port domain keep state
2390 # pass out all TCP connections and modulate state
2391 pass out on $ext_if proto tcp all modulate state
2393 # pass in certain TCP connections and keep state (SSH, SMTP, DNS, IDENT)
2394 pass in on $ext_if proto tcp from any to any port { ssh, smtp, domain, \e
2395 auth } flags S/SA keep state
2397 # pass in data mode connections for ftp-proxy running on this host.
2398 # (see ftp-proxy(8) for details)
2399 pass in on $ext_if proto tcp from any to 157.161.48.183 port >= 49152 \e
2400 flags S/SA keep state
2402 # Do not allow Windows 9x SMTP connections since they are typically
2403 # a viral worm. Alternately we could limit these OSes to 1 connection each.
2404 block in on $ext_if proto tcp from any os {"Windows 95", "Windows 98"} \e
2409 # three interfaces: $int_if, $ext_if, and $wifi_if (wireless). NAT is
2410 # being done on $ext_if for all outgoing packets. tag packets in on
2411 # $int_if and pass those tagged packets out on $ext_if. all other
2412 # outgoing packets (i.e., packets from the wireless network) are only
2413 # permitted to access port 80.
2415 pass in on $int_if from any to any tag INTNET keep state
2416 pass in on $wifi_if from any to any keep state
2418 block out on $ext_if from any to any
2419 pass out quick on $ext_if tagged INTNET keep state
2420 pass out on $ext_if from any to any port 80 keep state
2422 # tag incoming packets as they are redirected to spamd(8). use the tag
2423 # to pass those packets through the packet filter.
2425 rdr on $ext_if inet proto tcp from <spammers> to port smtp \e
2426 tag SPAMD -> 127.0.0.1 port spamd
2429 pass in on $ext_if inet proto tcp tagged SPAMD keep state
2436 line = ( option | pf-rule | nat-rule | binat-rule | rdr-rule |
2437 antispoof-rule | altq-rule | queue-rule | anchor-rule |
2438 trans-anchors | load-anchors | table-rule )
2440 option = "set" ( [ "timeout" ( timeout | "{" timeout-list "}" ) ] |
2441 [ "optimization" [ "default" | "normal" |
2442 "high-latency" | "satellite" |
2443 "aggressive" | "conservative" ] ]
2444 [ "limit" ( limit-item | "{" limit-list "}" ) ] |
2445 [ "loginterface" ( interface-name | "none" ) ] |
2446 [ "block-policy" ( "drop" | "return" ) ] |
2447 [ "state-policy" ( "if-bound" | "group-bound" |
2449 [ "require-order" ( "yes" | "no" ) ]
2450 [ "fingerprints" filename ] |
2451 [ "debug" ( "none" | "urgent" | "misc" | "loud" ) ] )
2453 pf-rule = action [ ( "in" | "out" ) ]
2454 [ "log" | "log-all" ] [ "quick" ]
2455 [ "on" ifspec ] [ route ] [ af ] [ protospec ]
2456 hosts [ filteropt-list ]
2458 filteropt-list = filteropt-list filteropt | filteropt
2459 filteropt = user | group | flags | icmp-type | icmp6-type | tos |
2460 ( "keep" | "modulate" | "synproxy" ) "state"
2461 [ "(" state-opts ")" ] |
2462 "fragment" | "no-df" | "min-ttl" number |
2463 "max-mss" number | "random-id" | "reassemble tcp" |
2464 fragmentation | "allow-opts" |
2465 "label" string | "tag" string | [ ! ] "tagged" string
2466 "queue" ( string | "(" string [ [ "," ] string ] ")" )
2468 nat-rule = [ "no" ] "nat" [ "pass" ] [ "on" ifspec ] [ af ]
2469 [ protospec ] hosts [ "tag" string ]
2470 [ "->" ( redirhost | "{" redirhost-list "}" )
2471 [ portspec ] [ pooltype ] [ "static-port" ] ]
2473 binat-rule = [ "no" ] "binat" [ "pass" ] [ "on" interface-name ]
2474 [ af ] [ "proto" ( proto-name | proto-number ) ]
2475 "from" address [ "/" mask-bits ] "to" ipspec
2477 [ "->" address [ "/" mask-bits ] ]
2479 rdr-rule = [ "no" ] "rdr" [ "pass" ] [ "on" ifspec ] [ af ]
2480 [ protospec ] hosts [ "tag" string ]
2481 [ "->" ( redirhost | "{" redirhost-list "}" )
2482 [ portspec ] [ pooltype ] ]
2484 antispoof-rule = "antispoof" [ "log" ] [ "quick" ]
2485 "for" ( interface-name | "{" interface-list "}" )
2486 [ af ] [ "label" string ]
2488 table-rule = "table" "<" string ">" [ tableopts-list ]
2489 tableopts-list = tableopts-list tableopts | tableopts
2490 tableopts = "persist" | "const" | "file" string |
2491 "{" [ tableaddr-list ] "}"
2492 tableaddr-list = tableaddr-list [ "," ] tableaddr-spec | tableaddr-spec
2493 tableaddr-spec = [ "!" ] tableaddr [ "/" mask-bits ]
2494 tableaddr = hostname | ipv4-dotted-quad | ipv6-coloned-hex |
2495 interface-name | "self"
2497 altq-rule = "altq on" interface-name queueopts-list
2499 queue-rule = "queue" string [ "on" interface-name ] queueopts-list
2502 anchor-rule = "anchor" string [ ( "in" | "out" ) ] [ "on" ifspec ]
2503 [ af ] [ "proto" ] [ protospec ] [ hosts ]
2505 trans-anchors = ( "nat-anchor" | "rdr-anchor" | "binat-anchor" ) string
2506 [ "on" ifspec ] [ af ] [ "proto" ] [ protospec ] [ hosts ]
2508 load-anchor = "load anchor" anchorname:rulesetname "from" filename
2510 queueopts-list = queueopts-list queueopts | queueopts
2511 queueopts = [ "bandwidth" bandwidth-spec ] |
2512 [ "qlimit" number ] | [ "tbrsize" number ] |
2513 [ "priority" number ] | [ schedulers ]
2514 schedulers = ( cbq-def | priq-def | hfsc-def )
2515 bandwidth-spec = "number" ( "b" | "Kb" | "Mb" | "Gb" | "%" )
2517 action = "pass" | "block" [ return ] | "scrub"
2518 return = "drop" | "return" | "return-rst" [ "( ttl" number ")" ] |
2519 "return-icmp" [ "(" icmpcode ["," icmp6code ] ")" ] |
2520 "return-icmp6" [ "(" icmp6code ")" ]
2521 icmpcode = ( icmp-code-name | icmp-code-number )
2522 icmp6code = ( icmp6-code-name | icmp6-code-number )
2524 ifspec = ( [ "!" ] interface-name ) | "{" interface-list "}"
2525 interface-list = [ "!" ] interface-name [ [ "," ] interface-list ]
2526 route = "fastroute" |
2527 ( "route-to" | "reply-to" | "dup-to" )
2528 ( routehost | "{" routehost-list "}" )
2530 af = "inet" | "inet6"
2532 protospec = "proto" ( proto-name | proto-number |
2533 "{" proto-list "}" )
2534 proto-list = ( proto-name | proto-number ) [ [ "," ] proto-list ]
2537 "from" ( "any" | "no-route" | "self" | host |
2538 "{" host-list "}" ) [ port ] [ os ]
2539 "to" ( "any" | "no-route" | "self" | host |
2540 "{" host-list "}" ) [ port ]
2542 ipspec = "any" | host | "{" host-list "}"
2543 host = [ "!" ] ( address [ "/" mask-bits ] | "<" string ">" )
2544 redirhost = address [ "/" mask-bits ]
2545 routehost = ( interface-name [ address [ "/" mask-bits ] ] )
2546 address = ( interface-name | "(" interface-name ")" | hostname |
2547 ipv4-dotted-quad | ipv6-coloned-hex )
2548 host-list = host [ [ "," ] host-list ]
2549 redirhost-list = redirhost [ [ "," ] redirhost-list ]
2550 routehost-list = routehost [ [ "," ] routehost-list ]
2552 port = "port" ( unary-op | binary-op | "{" op-list "}" )
2553 portspec = "port" ( number | name ) [ ":" ( "*" | number | name ) ]
2554 os = "os" ( os-name | "{" os-list "}" )
2555 user = "user" ( unary-op | binary-op | "{" op-list "}" )
2556 group = "group" ( unary-op | binary-op | "{" op-list "}" )
2558 unary-op = [ "=" | "!=" | "<" | "<=" | ">" | ">=" ]
2560 binary-op = number ( "<>" | "><" | ":" ) number
2561 op-list = ( unary-op | binary-op ) [ [ "," ] op-list ]
2563 os-name = operating-system-name
2564 os-list = os-name [ [ "," ] os-list ]
2566 flags = "flags" [ flag-set ] "/" flag-set
2567 flag-set = [ "F" ] [ "S" ] [ "R" ] [ "P" ] [ "A" ] [ "U" ] [ "E" ]
2570 icmp-type = "icmp-type" ( icmp-type-code | "{" icmp-list "}" )
2571 icmp6-type = "icmp6-type" ( icmp-type-code | "{" icmp-list "}" )
2572 icmp-type-code = ( icmp-type-name | icmp-type-number )
2573 [ "code" ( icmp-code-name | icmp-code-number ) ]
2574 icmp-list = icmp-type-code [ [ "," ] icmp-list ]
2576 tos = "tos" ( "lowdelay" | "throughput" | "reliability" |
2579 state-opts = state-opt [ [ "," ] state-opts ]
2580 state-opt = ( "max" number | "no-sync" | timeout |
2581 "source-track" [ ( "rule" | "global" ) ] |
2582 "max-src-nodes" number | "max-src-states" number |
2583 "if-bound" | "group-bound" | "floating" )
2585 fragmentation = [ "fragment reassemble" | "fragment crop" |
2586 "fragment drop-ovl" ]
2588 timeout-list = timeout [ [ "," ] timeout-list ]
2589 timeout = ( "tcp.first" | "tcp.opening" | "tcp.established" |
2590 "tcp.closing" | "tcp.finwait" | "tcp.closed" |
2591 "udp.first" | "udp.single" | "udp.multiple" |
2592 "icmp.first" | "icmp.error" |
2593 "other.first" | "other.single" | "other.multiple" |
2594 "frag" | "interval" | "src.track" |
2595 "adaptive.start" | "adaptive.end" ) number
2597 limit-list = limit-item [ [ "," ] limit-list ]
2598 limit-item = ( "states" | "frags" | "src-nodes" ) number
2600 pooltype = ( "bitmask" | "random" |
2601 "source-hash" [ ( hex-key | string-key ) ] |
2602 "round-robin" ) [ sticky-address ]
2604 subqueue = string | "{" queue-list "}"
2605 queue-list = string [ [ "," ] string ]
2606 cbq-def = "cbq" [ "(" cbq-opt [ [ "," ] cbq-opt ] ")" ]
2607 priq-def = "priq" [ "(" priq-opt [ [ "," ] priq-opt ] ")" ]
2608 hfsc-def = "hfsc" [ "(" hfsc-opt [ [ "," ] hfsc-opt ] ")" ]
2609 cbq-opt = ( "default" | "borrow" | "red" | "ecn" | "rio" )
2610 priq-opt = ( "default" | "red" | "ecn" | "rio" )
2611 hfsc-opt = ( "default" | "red" | "ecn" | "rio" |
2612 linkshare-sc | realtime-sc | upperlimit-sc )
2613 linkshare-sc = "linkshare" sc-spec
2614 realtime-sc = "realtime" sc-spec
2615 upperlimit-sc = "upperlimit" sc-spec
2616 sc-spec = ( bandwidth-spec |
2617 "(" bandwidth-spec number bandwidth-spec ")" )
2638 file format first appeared in