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