xref: /freebsd/sbin/pfctl/pfctl.8 (revision ddafb584b8aea1e648ddcba2d69d55da41c792de)
1.\" $OpenBSD: pfctl.8,v 1.138 2008/06/10 20:55:02 mcbride Exp $
2.\"
3.\" Copyright (c) 2001 Kjell Wooding.  All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\" 3. The name of the author may not be used to endorse or promote products
14.\"    derived from this software without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26.\"
27.Dd February 10, 2025
28.Dt PFCTL 8
29.Os
30.Sh NAME
31.Nm pfctl
32.Nd control the packet filter (PF) device
33.Sh SYNOPSIS
34.Nm pfctl
35.Bk -words
36.Op Fl AdeghMmNnOPqRrvz
37.Op Fl a Ar anchor
38.Oo Fl D Ar macro Ns =
39.Ar value Oc
40.Op Fl F Ar modifier
41.Op Fl f Ar file
42.Op Fl i Ar interface
43.Op Fl K Ar host | network
44.Xo
45.Oo Fl k
46.Ar host | network | label | id | gateway | nat
47.Oc Xc
48.Op Fl o Ar level
49.Op Fl p Ar device
50.Op Fl s Ar modifier
51.Xo
52.Oo Fl t Ar table
53.Fl T Ar command
54.Op Ar address ...
55.Oc Xc
56.Op Fl x Ar level
57.Ek
58.Sh DESCRIPTION
59The
60.Nm
61utility communicates with the packet filter device using the
62ioctl interface described in
63.Xr pf 4 .
64It allows ruleset and parameter configuration and retrieval of status
65information from the packet filter.
66.Pp
67Packet filtering restricts the types of packets that pass through
68network interfaces entering or leaving the host based on filter
69rules as described in
70.Xr pf.conf 5 .
71The packet filter can also replace addresses and ports of packets.
72Replacing source addresses and ports of outgoing packets is called
73NAT (Network Address Translation) and is used to connect an internal
74network (usually reserved address space) to an external one (the
75Internet) by making all connections to external hosts appear to
76come from the gateway.
77Replacing destination addresses and ports of incoming packets
78is used to redirect connections to different hosts and/or ports.
79A combination of both translations, bidirectional NAT, is also
80supported.
81Translation rules are described in
82.Xr pf.conf 5 .
83.Pp
84When the variable
85.Va pf
86is set to
87.Dv YES
88in
89.Xr rc.conf 5 ,
90the rule file specified with the variable
91.Va pf_rules
92is loaded automatically by the
93.Xr rc 8
94scripts and the packet filter is enabled.
95.Pp
96The packet filter does not itself forward packets between interfaces.
97Forwarding can be enabled by setting the
98.Xr sysctl 8
99variables
100.Em net.inet.ip.forwarding
101and/or
102.Em net.inet6.ip6.forwarding
103to 1.
104Set them permanently in
105.Xr sysctl.conf 5 .
106.Pp
107At least one option must be specified.
108The options are as follows:
109.Bl -tag -width Ds
110.It Fl A
111Load only the queue rules present in the rule file.
112Other rules and options are ignored.
113.It Fl a Ar anchor
114Apply flags
115.Fl f ,
116.Fl F ,
117and
118.Fl s
119only to the rules in the specified
120.Ar anchor .
121In addition to the main ruleset,
122.Nm
123can load and manipulate additional rulesets by name,
124called anchors.
125The main ruleset is the default anchor.
126.Pp
127Anchors are referenced by name and may be nested,
128with the various components of the anchor path separated by
129.Sq /
130characters, similar to how file system hierarchies are laid out.
131The last component of the anchor path is where ruleset operations are
132performed.
133.Pp
134Evaluation of
135.Ar anchor
136rules from the main ruleset is described in
137.Xr pf.conf 5 .
138.Pp
139For example, the following will show all filter rules (see the
140.Fl s
141flag below) inside the anchor
142.Dq authpf/smith(1234) ,
143which would have been created for user
144.Dq smith
145by
146.Xr authpf 8 ,
147PID 1234:
148.Bd -literal -offset indent
149# pfctl -a "authpf/smith(1234)" -s rules
150.Ed
151.Pp
152Private tables can also be put inside anchors, either by having table
153statements in the
154.Xr pf.conf 5
155file that is loaded in the anchor, or by using regular table commands, as in:
156.Bd -literal -offset indent
157# pfctl -a foo/bar -t mytable -T add 1.2.3.4 5.6.7.8
158.Ed
159.Pp
160When a rule referring to a table is loaded in an anchor, the rule will use the
161private table if one is defined, and then fall back to the table defined in the
162main ruleset, if there is one.
163This is similar to C rules for variable scope.
164It is possible to create distinct tables with the same name in the global
165ruleset and in an anchor, but this is often bad design and a warning will be
166issued in that case.
167.Pp
168By default, recursive inline printing of anchors applies only to unnamed
169anchors specified inline in the ruleset.
170If the anchor name is terminated with a
171.Sq *
172character, the
173.Fl s
174flag will recursively print all anchors in a brace delimited block.
175For example the following will print the
176.Dq authpf
177ruleset recursively:
178.Bd -literal -offset indent
179# pfctl -a 'authpf/*' -sr
180.Ed
181.Pp
182To print the main ruleset recursively, specify only
183.Sq *
184as the anchor name:
185.Bd -literal -offset indent
186# pfctl -a '*' -sr
187.Ed
188.It Fl D Ar macro Ns = Ns Ar value
189Define
190.Ar macro
191to be set to
192.Ar value
193on the command line.
194Overrides the definition of
195.Ar macro
196in the ruleset.
197.It Fl d
198Disable the packet filter.
199.It Fl e
200Enable the packet filter.
201.It Fl F Ar modifier
202Flush the filter parameters specified by
203.Ar modifier
204(may be abbreviated):
205.Pp
206.Bl -tag -width xxxxxxxxxxxx -compact
207.It Fl F Cm nat
208Flush the NAT rules.
209.It Fl F Cm queue
210Flush the queue rules.
211.It Fl F Cm ethernet
212Flush the Ethernet filter rules.
213.It Fl F Cm rules
214Flush the filter rules.
215.It Fl F Cm states
216Flush the state table (NAT and filter).
217.It Fl F Cm Sources
218Flush the source tracking table.
219.It Fl F Cm info
220Flush the filter information (statistics that are not bound to rules).
221.It Fl F Cm Tables
222Flush the tables.
223.It Fl F Cm osfp
224Flush the passive operating system fingerprints.
225.It Fl F Cm all
226Flush all of the above.
227.El
228.It Fl f Ar file
229Load the rules contained in
230.Ar file .
231This
232.Ar file
233may contain macros, tables, options, and normalization, queueing,
234translation, and filtering rules.
235With the exception of macros and tables, the statements must appear in that
236order.
237.It Fl g
238Include output helpful for debugging.
239.It Fl h
240Help.
241.It Fl i Ar interface
242Restrict the operation to the given
243.Ar interface .
244.It Fl K Ar host | network
245Kill all of the source tracking entries originating from the specified
246.Ar host
247or
248.Ar network .
249A second
250.Fl K Ar host
251or
252.Fl K Ar network
253option may be specified, which will kill all the source tracking
254entries from the first host/network to the second.
255.It Xo
256.Fl k
257.Ar host | network | label | id | gateway | nat
258.Xc
259Kill all of the state entries matching the specified
260.Ar host ,
261.Ar network ,
262.Ar label ,
263.Ar id ,
264.Ar gateway,
265or
266.Ar nat.
267.Pp
268For example, to kill all of the state entries originating from
269.Dq host :
270.Pp
271.Dl # pfctl -k host
272.Pp
273A second
274.Fl k Ar host
275or
276.Fl k Ar network
277option may be specified, which will kill all the state entries
278from the first host/network to the second.
279To kill all of the state entries from
280.Dq host1
281to
282.Dq host2 :
283.Pp
284.Dl # pfctl -k host1 -k host2
285.Pp
286To kill all states originating from 192.168.1.0/24 to 172.16.0.0/16:
287.Pp
288.Dl # pfctl -k 192.168.1.0/24 -k 172.16.0.0/16
289.Pp
290A network prefix length of 0 can be used as a wildcard.
291To kill all states with the target
292.Dq host2 :
293.Pp
294.Dl # pfctl -k 0.0.0.0/0 -k host2
295.Pp
296It is also possible to kill states by rule label or state ID.
297In this mode the first
298.Fl k
299argument is used to specify the type
300of the second argument.
301The following command would kill all states that have been created
302from rules carrying the label
303.Dq foobar :
304.Pp
305.Dl # pfctl -k label -k foobar
306.Pp
307To kill one specific state by its unique state ID
308(as shown by pfctl -s state -vv),
309use the
310.Ar id
311modifier and as a second argument the state ID and optional creator ID.
312To kill a state with ID 4823e84500000003 use:
313.Pp
314.Dl # pfctl -k id -k 4823e84500000003
315.Pp
316To kill a state with ID 4823e84500000018 created from a backup
317firewall with hostid 00000002 use:
318.Pp
319.Dl # pfctl -k id -k 4823e84500000018/2
320.Pp
321It is also possible to kill states created from a rule with the route-to/reply-to
322parameter set to route the connection through a particular gateway.
323Note that rules routing via the default routing table (not via a route-to
324rule) will have their rt_addr set as 0.0.0.0 or ::.
325To kill all states using a gateway of 192.168.0.1 use:
326.Pp
327.Dl # pfctl -k gateway -k 192.168.0.1
328.Pp
329A network prefix length can also be specified.
330To kill all states using a gateway in 192.168.0.0/24:
331.Pp
332.Dl # pfctl -k gateway -k 192.168.0.0/24
333.Pp
334States can also be killed based on their pre-NAT address:
335.Pp
336.Dl # pfctl -k nat -k 192.168.0.1
337.Pp
338.It Fl M
339Kill matching states in the opposite direction (on other interfaces) when
340killing states.
341This applies to states killed using the -k option and also will apply to the
342flush command when flushing states.
343This is useful when an interface is specified when flushing states.
344Example:
345.Pp
346.Dl # pfctl -M -i interface -Fs
347.Pp
348.It Fl m
349Merge in explicitly given options without resetting those
350which are omitted.
351Allows single options to be modified without disturbing the others:
352.Bd -literal -offset indent
353# echo "set loginterface fxp0" | pfctl -mf -
354.Ed
355.It Fl N
356Load only the NAT rules present in the rule file.
357Other rules and options are ignored.
358.It Fl n
359Do not actually load rules, just parse them.
360.It Fl O
361Load only the options present in the rule file.
362Other rules and options are ignored.
363.It Fl o Ar level
364Control the ruleset optimizer, overriding any rule file settings.
365.Pp
366.Bl -tag -width xxxxxxxxxxxx -compact
367.It Fl o Cm none
368Disable the ruleset optimizer.
369.It Fl o Cm basic
370Enable basic ruleset optimizations.
371This is the default behaviour.
372.It Fl o Cm profile
373Enable basic ruleset optimizations with profiling.
374.El
375For further information on the ruleset optimizer, see
376.Xr pf.conf 5 .
377.It Fl P
378Do not perform service name lookup for port specific rules,
379instead display the ports numerically.
380.It Fl p Ar device
381Use the device file
382.Ar device
383instead of the default
384.Pa /dev/pf .
385.It Fl q
386Only print errors and warnings.
387.It Fl R
388Load only the filter rules present in the rule file.
389Other rules and options are ignored.
390.It Fl r
391Perform reverse DNS lookups on states when displaying them.
392.It Fl s Ar modifier
393Show the filter parameters specified by
394.Ar modifier
395(may be abbreviated):
396.Pp
397.Bl -tag -width xxxxxxxxxxxxx -compact
398.It Fl s Cm nat
399Show the currently loaded NAT rules.
400.It Fl s Cm queue
401Show the currently loaded queue rules.
402When used together with
403.Fl v ,
404per-queue statistics are also shown.
405When used together with
406.Fl v v ,
407.Nm
408will loop and show updated queue statistics every five seconds, including
409measured bandwidth and packets per second.
410.It Fl s Cm ether
411Show the currently loaded Ethernet rules.
412When used together with
413.Fl v ,
414the per-rule statistics (number of evaluations,
415packets, and bytes) are also shown.
416.It Fl s Cm rules
417Show the currently loaded filter rules.
418When used together with
419.Fl v ,
420the per-rule statistics (number of evaluations,
421packets, and bytes) are also shown.
422Note that the
423.Dq skip step
424optimization done automatically by the kernel
425will skip evaluation of rules where possible.
426Packets passed statefully are counted in the rule that created the state
427(even though the rule is not evaluated more than once for the entire
428connection).
429.It Fl s Cm Anchors
430Show the currently loaded anchors directly attached to the main ruleset.
431If
432.Fl a Ar anchor
433is specified as well, the anchors loaded directly below the given
434.Ar anchor
435are shown instead.
436If
437.Fl v
438is specified, all anchors attached under the target anchor will be
439displayed recursively.
440.It Fl s Cm states
441Show the contents of the state table.
442.It Fl s Cm Sources
443Show the contents of the source tracking table.
444.It Fl s Cm info
445Show filter information (statistics and counters).
446When used together with
447.Fl v ,
448source tracking statistics are also shown.
449.It Fl s Cm Running
450Show the running status and provide a non-zero exit status when disabled.
451.It Fl s Cm labels
452Show per-rule statistics (label, evaluations, packets total, bytes total,
453packets in, bytes in, packets out, bytes out, state creations) of
454filter rules with labels, useful for accounting.
455.It Fl s Cm timeouts
456Show the current global timeouts.
457.It Fl s Cm memory
458Show the current pool memory hard limits.
459.It Fl s Cm Tables
460Show the list of tables.
461.It Fl s Cm osfp
462Show the list of operating system fingerprints.
463.It Fl s Cm Interfaces
464Show the list of interfaces and interface drivers available to PF.
465When used together with
466.Fl v ,
467it additionally lists which interfaces have skip rules activated.
468When used together with
469.Fl vv ,
470interface statistics are also shown.
471.Fl i
472can be used to select an interface or a group of interfaces.
473.It Fl s Cm all
474Show all of the above, except for the lists of interfaces and operating
475system fingerprints.
476.El
477.Pp
478Counters shown with
479.Fl s Cm info
480are:
481.Pp
482.Bl -tag -width xxxxxxxxxxxxxx -compact
483.It match
484explicit rule match
485.It bad-offset
486currently unused
487.It fragment
488invalid fragments dropped
489.It short
490short packets dropped
491.It normalize
492dropped by normalizer: illegal packets
493.It memory
494memory could not be allocated
495.It bad-timestamp
496bad TCP timestamp; RFC 1323
497.It congestion
498network interface queue congested
499.It ip-option
500bad IP/IPv6 options
501.It proto-cksum
502invalid protocol checksum
503.It state-mismatch
504packet was associated with a state entry, but sequence numbers did not match
505.It state-insert
506state insertion failure
507.It state-limit
508configured state limit was reached
509.It src-limit
510source node/connection limit
511.It synproxy
512dropped by synproxy
513.It map-failed
514address mapping failed
515.It translate
516no free ports in translation port range
517.El
518.It Fl T Ar command Op Ar address ...
519Specify the
520.Ar command
521(may be abbreviated) to apply to the table.
522Commands include:
523.Pp
524.Bl -tag -width xxxxxxxxxxxx -compact
525.It Fl T Cm kill
526Kill a table.
527.It Fl T Cm flush
528Flush all addresses of a table.
529.It Fl T Cm add
530Add one or more addresses in a table.
531Automatically create a nonexisting table.
532.It Fl T Cm delete
533Delete one or more addresses from a table.
534.It Fl T Cm expire Ar number
535Delete addresses which had their statistics cleared more than
536.Ar number
537seconds ago.
538For entries which have never had their statistics cleared,
539.Ar number
540refers to the time they were added to the table.
541.It Fl T Cm replace
542Replace the addresses of the table.
543Automatically create a nonexisting table.
544.It Fl T Cm show
545Show the content (addresses) of a table.
546.It Fl T Cm test
547Test if the given addresses match a table.
548.It Fl T Cm zero Op Ar address ...
549Clear all the statistics of a table, or only for specified addresses.
550.It Fl T Cm reset
551Clear statistics only for addresses with non-zero statistics. Addresses
552with counter values at zero and their
553.Dq Cleared
554timestamp are left untouched.
555.It Fl T Cm load
556Load only the table definitions from
557.Xr pf.conf 5 .
558This is used in conjunction with the
559.Fl f
560flag, as in:
561.Bd -literal -offset indent
562# pfctl -Tl -f pf.conf
563.Ed
564.El
565.Pp
566For the
567.Cm add ,
568.Cm delete ,
569.Cm replace ,
570and
571.Cm test
572commands, the list of addresses can be specified either directly on the command
573line and/or in an unformatted text file, using the
574.Fl f
575flag.
576Comments starting with a
577.Sq #
578or
579.Sq \;
580are allowed in the text file.
581With these commands, the
582.Fl v
583flag can also be used once or twice, in which case
584.Nm
585will print the
586detailed result of the operation for each individual address, prefixed by
587one of the following letters:
588.Pp
589.Bl -tag -width XXX -compact
590.It A
591The address/network has been added.
592.It C
593The address/network has been changed (negated).
594.It D
595The address/network has been deleted.
596.It M
597The address matches
598.Po
599.Cm test
600operation only
601.Pc .
602.It X
603The address/network is duplicated and therefore ignored.
604.It Y
605The address/network cannot be added/deleted due to conflicting
606.Sq \&!
607attributes.
608.It Z
609The address/network has been cleared (statistics).
610.El
611.Pp
612Each table can maintain a set of counters that can be retrieved using the
613.Fl v
614flag of
615.Nm .
616For example, the following commands define a wide open firewall which will keep
617track of packets going to or coming from the
618.Ox
619FTP server.
620The following commands configure the firewall and send 10 pings to the FTP
621server:
622.Bd -literal -offset indent
623# printf "table <test> counters { ftp.openbsd.org }\en \e
624    pass out to <test>\en" | pfctl -f-
625# ping -qc10 ftp.openbsd.org
626.Ed
627.Pp
628We can now use the table
629.Cm show
630command to output, for each address and packet direction, the number of packets
631and bytes that are being passed or blocked by rules referencing the table.
632The time at which the current accounting started is also shown with the
633.Dq Cleared
634line.
635.Bd -literal -offset indent
636# pfctl -t test -vTshow
637   129.128.5.191
638    Cleared:     Thu Feb 13 18:55:18 2003
639    In/Block:    [ Packets: 0        Bytes: 0        ]
640    In/Pass:     [ Packets: 10       Bytes: 840      ]
641    Out/Block:   [ Packets: 0        Bytes: 0        ]
642    Out/Pass:    [ Packets: 10       Bytes: 840      ]
643.Ed
644.Pp
645Similarly, it is possible to view global information about the tables
646by using the
647.Fl v
648modifier twice and the
649.Fl s
650.Cm Tables
651command.
652This will display the number of addresses on each table,
653the number of rules which reference the table, and the global
654packet statistics for the whole table:
655.Bd -literal -offset indent
656# pfctl -vvsTables
657--a-r-C test
658    Addresses:   1
659    Cleared:     Thu Feb 13 18:55:18 2003
660    References:  [ Anchors: 0        Rules: 1        ]
661    Evaluations: [ NoMatch: 3496     Match: 1        ]
662    In/Block:    [ Packets: 0        Bytes: 0        ]
663    In/Pass:     [ Packets: 10       Bytes: 840      ]
664    In/XPass:    [ Packets: 0        Bytes: 0        ]
665    Out/Block:   [ Packets: 0        Bytes: 0        ]
666    Out/Pass:    [ Packets: 10       Bytes: 840      ]
667    Out/XPass:   [ Packets: 0        Bytes: 0        ]
668.Ed
669.Pp
670As we can see here, only one packet \- the initial ping request \- matched the
671table, but all packets passing as the result of the state are correctly
672accounted for.
673Reloading the table(s) or ruleset will not affect packet accounting in any way.
674The two
675.Dq XPass
676counters are incremented instead of the
677.Dq Pass
678counters when a
679.Dq stateful
680packet is passed but does not match the table anymore.
681This will happen in our example if someone flushes the table while the
682.Xr ping 8
683command is running.
684.Pp
685When used with a single
686.Fl v ,
687.Nm
688will only display the first line containing the table flags and name.
689The flags are defined as follows:
690.Pp
691.Bl -tag -width XXX -compact
692.It c
693For constant tables, which cannot be altered outside
694.Xr pf.conf 5 .
695.It p
696For persistent tables, which do not get automatically killed when no rules
697refer to them.
698.It a
699For tables which are part of the
700.Em active
701tableset.
702Tables without this flag do not really exist, cannot contain addresses, and are
703only listed if the
704.Fl g
705flag is given.
706.It i
707For tables which are part of the
708.Em inactive
709tableset.
710This flag can only be witnessed briefly during the loading of
711.Xr pf.conf 5 .
712.It r
713For tables which are referenced (used) by rules.
714.It h
715This flag is set when a table in the main ruleset is hidden by one or more
716tables of the same name from anchors attached below it.
717.It C
718This flag is set when per-address counters are enabled on the table.
719.El
720.It Fl t Ar table
721Specify the name of the table.
722.It Fl v
723Produce more verbose output.
724A second use of
725.Fl v
726will produce even more verbose output including ruleset warnings.
727See the previous section for its effect on table commands.
728.It Fl x Ar level
729Set the debug
730.Ar level
731(may be abbreviated) to one of the following:
732.Pp
733.Bl -tag -width xxxxxxxxxxxx -compact
734.It Fl x Cm none
735Do not generate debug messages.
736.It Fl x Cm urgent
737Generate debug messages only for serious errors.
738.It Fl x Cm misc
739Generate debug messages for various errors.
740.It Fl x Cm loud
741Generate debug messages for common conditions.
742.El
743.It Fl z
744Clear per-rule statistics.
745.El
746.Sh FILES
747.Bl -tag -width "/etc/pf.conf" -compact
748.It Pa /etc/pf.conf
749Packet filter rules file.
750.It Pa /etc/pf.os
751Passive operating system fingerprint database.
752.El
753.Sh SEE ALSO
754.Xr pf 4 ,
755.Xr pf.conf 5 ,
756.Xr pf.os 5 ,
757.Xr rc.conf 5 ,
758.Xr services 5 ,
759.Xr sysctl.conf 5 ,
760.Xr authpf 8 ,
761.Xr ftp-proxy 8 ,
762.Xr rc 8 ,
763.Xr sysctl 8
764.Sh HISTORY
765The
766.Nm
767program and the
768.Xr pf 4
769filter mechanism appeared in
770.Ox 3.0 .
771They first appeared in
772.Fx 5.3
773ported from the version in
774.Ox 3.5
775