xref: /freebsd/sbin/pfctl/pfctl.8 (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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 November 25, 2024
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
107The
108.Nm
109utility provides several commands.
110The options are as follows:
111.Bl -tag -width Ds
112.It Fl A
113Load only the queue rules present in the rule file.
114Other rules and options are ignored.
115.It Fl a Ar anchor
116Apply flags
117.Fl f ,
118.Fl F ,
119and
120.Fl s
121only to the rules in the specified
122.Ar anchor .
123In addition to the main ruleset,
124.Nm
125can load and manipulate additional rulesets by name,
126called anchors.
127The main ruleset is the default anchor.
128.Pp
129Anchors are referenced by name and may be nested,
130with the various components of the anchor path separated by
131.Sq /
132characters, similar to how file system hierarchies are laid out.
133The last component of the anchor path is where ruleset operations are
134performed.
135.Pp
136Evaluation of
137.Ar anchor
138rules from the main ruleset is described in
139.Xr pf.conf 5 .
140.Pp
141For example, the following will show all filter rules (see the
142.Fl s
143flag below) inside the anchor
144.Dq authpf/smith(1234) ,
145which would have been created for user
146.Dq smith
147by
148.Xr authpf 8 ,
149PID 1234:
150.Bd -literal -offset indent
151# pfctl -a "authpf/smith(1234)" -s rules
152.Ed
153.Pp
154Private tables can also be put inside anchors, either by having table
155statements in the
156.Xr pf.conf 5
157file that is loaded in the anchor, or by using regular table commands, as in:
158.Bd -literal -offset indent
159# pfctl -a foo/bar -t mytable -T add 1.2.3.4 5.6.7.8
160.Ed
161.Pp
162When a rule referring to a table is loaded in an anchor, the rule will use the
163private table if one is defined, and then fall back to the table defined in the
164main ruleset, if there is one.
165This is similar to C rules for variable scope.
166It is possible to create distinct tables with the same name in the global
167ruleset and in an anchor, but this is often bad design and a warning will be
168issued in that case.
169.Pp
170By default, recursive inline printing of anchors applies only to unnamed
171anchors specified inline in the ruleset.
172If the anchor name is terminated with a
173.Sq *
174character, the
175.Fl s
176flag will recursively print all anchors in a brace delimited block.
177For example the following will print the
178.Dq authpf
179ruleset recursively:
180.Bd -literal -offset indent
181# pfctl -a 'authpf/*' -sr
182.Ed
183.Pp
184To print the main ruleset recursively, specify only
185.Sq *
186as the anchor name:
187.Bd -literal -offset indent
188# pfctl -a '*' -sr
189.Ed
190.It Fl D Ar macro Ns = Ns Ar value
191Define
192.Ar macro
193to be set to
194.Ar value
195on the command line.
196Overrides the definition of
197.Ar macro
198in the ruleset.
199.It Fl d
200Disable the packet filter.
201.It Fl e
202Enable the packet filter.
203.It Fl F Ar modifier
204Flush the filter parameters specified by
205.Ar modifier
206(may be abbreviated):
207.Pp
208.Bl -tag -width xxxxxxxxxxxx -compact
209.It Fl F Cm nat
210Flush the NAT rules.
211.It Fl F Cm queue
212Flush the queue rules.
213.It Fl F Cm ethernet
214Flush the Ethernet filter rules.
215.It Fl F Cm rules
216Flush the filter rules.
217.It Fl F Cm states
218Flush the state table (NAT and filter).
219.It Fl F Cm Sources
220Flush the source tracking table.
221.It Fl F Cm info
222Flush the filter information (statistics that are not bound to rules).
223.It Fl F Cm Tables
224Flush the tables.
225.It Fl F Cm osfp
226Flush the passive operating system fingerprints.
227.It Fl F Cm all
228Flush all of the above.
229.El
230.It Fl f Ar file
231Load the rules contained in
232.Ar file .
233This
234.Ar file
235may contain macros, tables, options, and normalization, queueing,
236translation, and filtering rules.
237With the exception of macros and tables, the statements must appear in that
238order.
239.It Fl g
240Include output helpful for debugging.
241.It Fl h
242Help.
243.It Fl i Ar interface
244Restrict the operation to the given
245.Ar interface .
246.It Fl K Ar host | network
247Kill all of the source tracking entries originating from the specified
248.Ar host
249or
250.Ar network .
251A second
252.Fl K Ar host
253or
254.Fl K Ar network
255option may be specified, which will kill all the source tracking
256entries from the first host/network to the second.
257.It Xo
258.Fl k
259.Ar host | network | label | id | gateway | nat
260.Xc
261Kill all of the state entries matching the specified
262.Ar host ,
263.Ar network ,
264.Ar label ,
265.Ar id ,
266.Ar gateway,
267or
268.Ar nat.
269.Pp
270For example, to kill all of the state entries originating from
271.Dq host :
272.Pp
273.Dl # pfctl -k host
274.Pp
275A second
276.Fl k Ar host
277or
278.Fl k Ar network
279option may be specified, which will kill all the state entries
280from the first host/network to the second.
281To kill all of the state entries from
282.Dq host1
283to
284.Dq host2 :
285.Pp
286.Dl # pfctl -k host1 -k host2
287.Pp
288To kill all states originating from 192.168.1.0/24 to 172.16.0.0/16:
289.Pp
290.Dl # pfctl -k 192.168.1.0/24 -k 172.16.0.0/16
291.Pp
292A network prefix length of 0 can be used as a wildcard.
293To kill all states with the target
294.Dq host2 :
295.Pp
296.Dl # pfctl -k 0.0.0.0/0 -k host2
297.Pp
298It is also possible to kill states by rule label or state ID.
299In this mode the first
300.Fl k
301argument is used to specify the type
302of the second argument.
303The following command would kill all states that have been created
304from rules carrying the label
305.Dq foobar :
306.Pp
307.Dl # pfctl -k label -k foobar
308.Pp
309To kill one specific state by its unique state ID
310(as shown by pfctl -s state -vv),
311use the
312.Ar id
313modifier and as a second argument the state ID and optional creator ID.
314To kill a state with ID 4823e84500000003 use:
315.Pp
316.Dl # pfctl -k id -k 4823e84500000003
317.Pp
318To kill a state with ID 4823e84500000018 created from a backup
319firewall with hostid 00000002 use:
320.Pp
321.Dl # pfctl -k id -k 4823e84500000018/2
322.Pp
323It is also possible to kill states created from a rule with the route-to/reply-to
324parameter set to route the connection through a particular gateway.
325Note that rules routing via the default routing table (not via a route-to
326rule) will have their rt_addr set as 0.0.0.0 or ::.
327To kill all states using a gateway of 192.168.0.1 use:
328.Pp
329.Dl # pfctl -k gateway -k 192.168.0.1
330.Pp
331A network prefix length can also be specified.
332To kill all states using a gateway in 192.168.0.0/24:
333.Pp
334.Dl # pfctl -k gateway -k 192.168.0.0/24
335.Pp
336States can also be killed based on their pre-NAT address:
337.Pp
338.Dl # pfctl -k nat -k 192.168.0.1
339.Pp
340.It Fl M
341Kill matching states in the opposite direction (on other interfaces) when
342killing states.
343This applies to states killed using the -k option and also will apply to the
344flush command when flushing states.
345This is useful when an interface is specified when flushing states.
346Example:
347.Pp
348.Dl # pfctl -M -i interface -Fs
349.Pp
350.It Fl m
351Merge in explicitly given options without resetting those
352which are omitted.
353Allows single options to be modified without disturbing the others:
354.Bd -literal -offset indent
355# echo "set loginterface fxp0" | pfctl -mf -
356.Ed
357.It Fl N
358Load only the NAT rules present in the rule file.
359Other rules and options are ignored.
360.It Fl n
361Do not actually load rules, just parse them.
362.It Fl O
363Load only the options present in the rule file.
364Other rules and options are ignored.
365.It Fl o Ar level
366Control the ruleset optimizer, overriding any rule file settings.
367.Pp
368.Bl -tag -width xxxxxxxxxxxx -compact
369.It Fl o Cm none
370Disable the ruleset optimizer.
371.It Fl o Cm basic
372Enable basic ruleset optimizations.
373This is the default behaviour.
374.It Fl o Cm profile
375Enable basic ruleset optimizations with profiling.
376.El
377For further information on the ruleset optimizer, see
378.Xr pf.conf 5 .
379.It Fl P
380Do not perform service name lookup for port specific rules,
381instead display the ports numerically.
382.It Fl p Ar device
383Use the device file
384.Ar device
385instead of the default
386.Pa /dev/pf .
387.It Fl q
388Only print errors and warnings.
389.It Fl R
390Load only the filter rules present in the rule file.
391Other rules and options are ignored.
392.It Fl r
393Perform reverse DNS lookups on states when displaying them.
394.It Fl s Ar modifier
395Show the filter parameters specified by
396.Ar modifier
397(may be abbreviated):
398.Pp
399.Bl -tag -width xxxxxxxxxxxxx -compact
400.It Fl s Cm nat
401Show the currently loaded NAT rules.
402.It Fl s Cm queue
403Show the currently loaded queue rules.
404When used together with
405.Fl v ,
406per-queue statistics are also shown.
407When used together with
408.Fl v v ,
409.Nm
410will loop and show updated queue statistics every five seconds, including
411measured bandwidth and packets per second.
412.It Fl s Cm ether
413Show the currently loaded Ethernet rules.
414When used together with
415.Fl v ,
416the per-rule statistics (number of evaluations,
417packets, and bytes) are also shown.
418.It Fl s Cm rules
419Show the currently loaded filter rules.
420When used together with
421.Fl v ,
422the per-rule statistics (number of evaluations,
423packets, and bytes) are also shown.
424Note that the
425.Dq skip step
426optimization done automatically by the kernel
427will skip evaluation of rules where possible.
428Packets passed statefully are counted in the rule that created the state
429(even though the rule is not evaluated more than once for the entire
430connection).
431.It Fl s Cm Anchors
432Show the currently loaded anchors directly attached to the main ruleset.
433If
434.Fl a Ar anchor
435is specified as well, the anchors loaded directly below the given
436.Ar anchor
437are shown instead.
438If
439.Fl v
440is specified, all anchors attached under the target anchor will be
441displayed recursively.
442.It Fl s Cm states
443Show the contents of the state table.
444.It Fl s Cm Sources
445Show the contents of the source tracking table.
446.It Fl s Cm info
447Show filter information (statistics and counters).
448When used together with
449.Fl v ,
450source tracking statistics are also shown.
451.It Fl s Cm Running
452Show the running status and provide a non-zero exit status when disabled.
453.It Fl s Cm labels
454Show per-rule statistics (label, evaluations, packets total, bytes total,
455packets in, bytes in, packets out, bytes out, state creations) of
456filter rules with labels, useful for accounting.
457.It Fl s Cm timeouts
458Show the current global timeouts.
459.It Fl s Cm memory
460Show the current pool memory hard limits.
461.It Fl s Cm Tables
462Show the list of tables.
463.It Fl s Cm osfp
464Show the list of operating system fingerprints.
465.It Fl s Cm Interfaces
466Show the list of interfaces and interface drivers available to PF.
467When used together with
468.Fl v ,
469it additionally lists which interfaces have skip rules activated.
470When used together with
471.Fl vv ,
472interface statistics are also shown.
473.Fl i
474can be used to select an interface or a group of interfaces.
475.It Fl s Cm all
476Show all of the above, except for the lists of interfaces and operating
477system fingerprints.
478.El
479.It Fl T Ar command Op Ar address ...
480Specify the
481.Ar command
482(may be abbreviated) to apply to the table.
483Commands include:
484.Pp
485.Bl -tag -width xxxxxxxxxxxx -compact
486.It Fl T Cm kill
487Kill a table.
488.It Fl T Cm flush
489Flush all addresses of a table.
490.It Fl T Cm add
491Add one or more addresses in a table.
492Automatically create a nonexisting table.
493.It Fl T Cm delete
494Delete one or more addresses from a table.
495.It Fl T Cm expire Ar number
496Delete addresses which had their statistics cleared more than
497.Ar number
498seconds ago.
499For entries which have never had their statistics cleared,
500.Ar number
501refers to the time they were added to the table.
502.It Fl T Cm replace
503Replace the addresses of the table.
504Automatically create a nonexisting table.
505.It Fl T Cm show
506Show the content (addresses) of a table.
507.It Fl T Cm test
508Test if the given addresses match a table.
509.It Fl T Cm zero Op Ar address ...
510Clear all the statistics of a table, or only for specified addresses.
511.It Fl T Cm reset
512Clear statistics only for addresses with non-zero statistics. Addresses
513with counter values at zero and their
514.Dq Cleared
515timestamp are left untouched.
516.It Fl T Cm load
517Load only the table definitions from
518.Xr pf.conf 5 .
519This is used in conjunction with the
520.Fl f
521flag, as in:
522.Bd -literal -offset indent
523# pfctl -Tl -f pf.conf
524.Ed
525.El
526.Pp
527For the
528.Cm add ,
529.Cm delete ,
530.Cm replace ,
531and
532.Cm test
533commands, the list of addresses can be specified either directly on the command
534line and/or in an unformatted text file, using the
535.Fl f
536flag.
537Comments starting with a
538.Sq #
539or
540.Sq \;
541are allowed in the text file.
542With these commands, the
543.Fl v
544flag can also be used once or twice, in which case
545.Nm
546will print the
547detailed result of the operation for each individual address, prefixed by
548one of the following letters:
549.Pp
550.Bl -tag -width XXX -compact
551.It A
552The address/network has been added.
553.It C
554The address/network has been changed (negated).
555.It D
556The address/network has been deleted.
557.It M
558The address matches
559.Po
560.Cm test
561operation only
562.Pc .
563.It X
564The address/network is duplicated and therefore ignored.
565.It Y
566The address/network cannot be added/deleted due to conflicting
567.Sq \&!
568attributes.
569.It Z
570The address/network has been cleared (statistics).
571.El
572.Pp
573Each table can maintain a set of counters that can be retrieved using the
574.Fl v
575flag of
576.Nm .
577For example, the following commands define a wide open firewall which will keep
578track of packets going to or coming from the
579.Ox
580FTP server.
581The following commands configure the firewall and send 10 pings to the FTP
582server:
583.Bd -literal -offset indent
584# printf "table <test> counters { ftp.openbsd.org }\en \e
585    pass out to <test>\en" | pfctl -f-
586# ping -qc10 ftp.openbsd.org
587.Ed
588.Pp
589We can now use the table
590.Cm show
591command to output, for each address and packet direction, the number of packets
592and bytes that are being passed or blocked by rules referencing the table.
593The time at which the current accounting started is also shown with the
594.Dq Cleared
595line.
596.Bd -literal -offset indent
597# pfctl -t test -vTshow
598   129.128.5.191
599    Cleared:     Thu Feb 13 18:55:18 2003
600    In/Block:    [ Packets: 0        Bytes: 0        ]
601    In/Pass:     [ Packets: 10       Bytes: 840      ]
602    Out/Block:   [ Packets: 0        Bytes: 0        ]
603    Out/Pass:    [ Packets: 10       Bytes: 840      ]
604.Ed
605.Pp
606Similarly, it is possible to view global information about the tables
607by using the
608.Fl v
609modifier twice and the
610.Fl s
611.Cm Tables
612command.
613This will display the number of addresses on each table,
614the number of rules which reference the table, and the global
615packet statistics for the whole table:
616.Bd -literal -offset indent
617# pfctl -vvsTables
618--a-r-C test
619    Addresses:   1
620    Cleared:     Thu Feb 13 18:55:18 2003
621    References:  [ Anchors: 0        Rules: 1        ]
622    Evaluations: [ NoMatch: 3496     Match: 1        ]
623    In/Block:    [ Packets: 0        Bytes: 0        ]
624    In/Pass:     [ Packets: 10       Bytes: 840      ]
625    In/XPass:    [ Packets: 0        Bytes: 0        ]
626    Out/Block:   [ Packets: 0        Bytes: 0        ]
627    Out/Pass:    [ Packets: 10       Bytes: 840      ]
628    Out/XPass:   [ Packets: 0        Bytes: 0        ]
629.Ed
630.Pp
631As we can see here, only one packet \- the initial ping request \- matched the
632table, but all packets passing as the result of the state are correctly
633accounted for.
634Reloading the table(s) or ruleset will not affect packet accounting in any way.
635The two
636.Dq XPass
637counters are incremented instead of the
638.Dq Pass
639counters when a
640.Dq stateful
641packet is passed but does not match the table anymore.
642This will happen in our example if someone flushes the table while the
643.Xr ping 8
644command is running.
645.Pp
646When used with a single
647.Fl v ,
648.Nm
649will only display the first line containing the table flags and name.
650The flags are defined as follows:
651.Pp
652.Bl -tag -width XXX -compact
653.It c
654For constant tables, which cannot be altered outside
655.Xr pf.conf 5 .
656.It p
657For persistent tables, which do not get automatically killed when no rules
658refer to them.
659.It a
660For tables which are part of the
661.Em active
662tableset.
663Tables without this flag do not really exist, cannot contain addresses, and are
664only listed if the
665.Fl g
666flag is given.
667.It i
668For tables which are part of the
669.Em inactive
670tableset.
671This flag can only be witnessed briefly during the loading of
672.Xr pf.conf 5 .
673.It r
674For tables which are referenced (used) by rules.
675.It h
676This flag is set when a table in the main ruleset is hidden by one or more
677tables of the same name from anchors attached below it.
678.It C
679This flag is set when per-address counters are enabled on the table.
680.El
681.It Fl t Ar table
682Specify the name of the table.
683.It Fl v
684Produce more verbose output.
685A second use of
686.Fl v
687will produce even more verbose output including ruleset warnings.
688See the previous section for its effect on table commands.
689.It Fl x Ar level
690Set the debug
691.Ar level
692(may be abbreviated) to one of the following:
693.Pp
694.Bl -tag -width xxxxxxxxxxxx -compact
695.It Fl x Cm none
696Do not generate debug messages.
697.It Fl x Cm urgent
698Generate debug messages only for serious errors.
699.It Fl x Cm misc
700Generate debug messages for various errors.
701.It Fl x Cm loud
702Generate debug messages for common conditions.
703.El
704.It Fl z
705Clear per-rule statistics.
706.El
707.Sh FILES
708.Bl -tag -width "/etc/pf.conf" -compact
709.It Pa /etc/pf.conf
710Packet filter rules file.
711.It Pa /etc/pf.os
712Passive operating system fingerprint database.
713.El
714.Sh SEE ALSO
715.Xr pf 4 ,
716.Xr pf.conf 5 ,
717.Xr pf.os 5 ,
718.Xr rc.conf 5 ,
719.Xr services 5 ,
720.Xr sysctl.conf 5 ,
721.Xr authpf 8 ,
722.Xr ftp-proxy 8 ,
723.Xr rc 8 ,
724.Xr sysctl 8
725.Sh HISTORY
726The
727.Nm
728program and the
729.Xr pf 4
730filter mechanism appeared in
731.Ox 3.0 .
732They first appeared in
733.Fx 5.3
734ported from the version in
735.Ox 3.5
736