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