xref: /netbsd-src/dist/pf/sbin/pfctl/pfctl.8 (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1.\" $NetBSD: pfctl.8,v 1.4 2004/11/14 11:26:48 yamt Exp $
2.\" $OpenBSD: pfctl.8,v 1.115 2004/08/26 16:14:58 jaredy Exp $
3.\"
4.\" Copyright (c) 2001 Kjell Wooding.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. The name of the author may not be used to endorse or promote products
15.\"    derived from this software without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.Dd November 20, 2002
29.Dt PFCTL 8
30.Os
31.Sh NAME
32.Nm pfctl
33.Nd "control the packet filter (PF) and network address translation (NAT) device"
34.Sh SYNOPSIS
35.Nm pfctl
36.Bk -words
37.Op Fl AdeghNnOoqRrvz
38.Op Fl a Ar anchor
39.Xo
40.Oo Fl D
41.Ar macro Ns = Ns Ar value Oc
42.Xc
43.Op Fl F Ar modifier
44.Op Fl f Ar file
45.Op Fl i Ar interface
46.Op Fl k Ar host
47.Op Fl p Ar device
48.Op Fl s Ar modifier
49.Oo Xo
50.Fl t Ar table
51.Fl T Ar command
52.Op Ar address ... Oc
53.Xc
54.Op Fl x Ar level
55.Ek
56.Sh DESCRIPTION
57The
58.Nm
59utility communicates with the packet filter device using the
60ioctl interface described in
61.Xr pf 4 .
62It allows ruleset and parameter configuration and retrieval of status
63information from the packet filter.
64.Pp
65Packet filtering restricts the types of packets that pass through
66network interfaces entering or leaving the host based on filter
67rules as described in
68.Xr pf.conf 5 .
69The packet filter can also replace addresses and ports of packets.
70Replacing source addresses and ports of outgoing packets is called
71NAT (Network Address Translation) and is used to connect an internal
72network (usually reserved address space) to an external one (the
73Internet) by making all connections to external hosts appear to
74come from the gateway.
75Replacing destination addresses and ports of incoming packets
76is used to redirect connections to different hosts and/or ports.
77A combination of both translations, bidirectional NAT, is also
78supported.
79Translation rules are described in
80.Xr pf.conf 5 .
81.Pp
82When the variable
83.Va pf
84is set to
85.Dv YES
86in
87.Xr rc.conf.local 8 ,
88the rule file specified with the variable
89.Va pf_rules
90is loaded automatically by the
91.Xr rc 8
92scripts and the packet filter is enabled.
93.Pp
94The packet filter does not itself forward packets between interfaces.
95Forwarding can be enabled by setting the
96.Xr sysctl 8
97variables
98.Em net.inet.ip.forwarding
99and/or
100.Em net.inet6.ip6.forwarding
101to 1.
102Set them permanently in
103.Xr sysctl.conf 5 .
104.Pp
105The
106.Nm
107utility provides several commands.
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.Li authpf/smith ,
143which would have been created for user smith by
144.Xr authpf 8 :
145.Bd -literal -offset indent
146# pfctl -a authpf/smith -s rules
147.Ed
148.Pp
149Private tables can also be put inside anchors, either by having table
150statements in the
151.Xr pf.conf 5
152file that is loaded in the anchor, or by using regular table commands, as in:
153.Bd -literal -offset indent
154# pfctl -a foo/bar -t mytable -T add 1.2.3.4 5.6.7.8
155.Ed
156.Pp
157When a rule referring to a table is loaded in an anchor, the rule will use the
158private table if one is defined, and then fallback to the table defined in the
159main ruleset, if there is one.
160This is similar to C rules for variable scope.
161It is possible to create distinct tables with the same name in the global
162ruleset and in an anchor, but this is often bad design and a warning will be
163issued in that case.
164.It Fl D Ar macro Ns = Ns Ar value
165Define
166.Ar macro
167to be set to
168.Ar value
169on the command line.
170Overrides the definition of
171.Ar macro
172in the ruleset.
173.It Fl d
174Disable the packet filter.
175.It Fl e
176Enable the packet filter.
177.It Fl F Ar modifier
178Flush the filter parameters specified by
179.Ar modifier
180(may be abbreviated):
181.Pp
182.Bl -tag -width xxxxxxxxxxxx -compact
183.It Fl F Cm nat
184Flush the NAT rules.
185.It Fl F Cm queue
186Flush the queue rules.
187.It Fl F Cm rules
188Flush the filter rules.
189.It Fl F Cm state
190Flush the state table (NAT and filter).
191.It Fl F Cm Sources
192Flush the source tracking table.
193.It Fl F Cm info
194Flush the filter information (statistics that are not bound to rules).
195.It Fl F Cm Tables
196Flush the tables.
197.It Fl F Cm osfp
198Flush the passive operating system fingerprints.
199.It Fl F Cm all
200Flush all of the above.
201.El
202.It Fl f Ar file
203Load the rules contained in
204.Ar file .
205This
206.Ar file
207may contain macros, tables, options, and normalization, queueing,
208translation, and filtering rules.
209With the exception of macros and tables, the statements must appear in that
210order.
211.It Fl g
212Include output helpful for debugging.
213.It Fl h
214Help.
215.It Fl i Ar interface
216Restrict the operation to the given
217.Ar interface .
218.It Fl k Ar host
219Kill all of the state entries originating from the specified
220.Ar host .
221A second
222.Fl k Ar host
223option may be specified, which will kill all the state entries
224from the first
225.Ar host
226to the second
227.Ar host .
228For example, to kill all of the state entries originating from
229.Li host :
230.Bd -literal -offset indent
231# pfctl -k host
232.Ed
233.Pp
234To kill all of the state entries from
235.Li host1
236to
237.Li host2 :
238.Bd -literal -offset indent
239# pfctl -k host1 -k host2
240.Ed
241.It Fl N
242Load only the NAT rules present in the rule file.
243Other rules and options are ignored.
244.It Fl n
245Do not actually load rules, just parse them.
246.It Fl O
247Load only the options present in the rule file.
248Other rules and options are ignored.
249.It Fl o
250Enable the ruleset optimizer.
251The ruleset optimizer attempts to improve rulesets by removing rule
252duplication and making better use of rule ordering.
253Specifically, it does four things:
254.Pp
255.Bl -enum -compact
256.It
257remove duplicate rules
258.It
259remove rules that are a subset of another rule
260.It
261combine multiple rules into a table when advantageous
262.It
263re-order the rules to improve evaluation performance
264.El
265.Pp
266A second
267.Fl o
268may be specified to use the currently loaded ruleset as a feedback profile
269to tailor the optimization of the
270.Ar quick
271rules to the actual network behavior.
272.Pp
273It is important to note that the ruleset optimizer will modify the ruleset
274to improve performance.
275A side effect of the ruleset modification is that per-rule accounting
276statistics will have different meanings than before.
277If per-rule accounting is important for billing purposes or whatnot, either
278the ruleset optimizer should not be used or a
279.Ar label
280field should be added to all of the accounting rules to act as optimization
281barriers.
282.It Fl p Ar device
283Use the device file
284.Ar device
285instead of the default
286.Pa /dev/pf .
287.It Fl q
288Only print errors and warnings.
289.It Fl R
290Load only the filter rules present in the rule file.
291Other rules and options are ignored.
292.It Fl r
293Perform reverse DNS lookups on states when displaying them.
294.It Fl s Ar modifier
295Show the filter parameters specified by
296.Ar modifier
297(may be abbreviated):
298.Pp
299.Bl -tag -width xxxxxxxxxxxxx -compact
300.It Fl s Cm nat
301Show the currently loaded NAT rules.
302.It Fl s Cm queue
303Show the currently loaded queue rules.
304When used together with
305.Fl v ,
306per-queue statistics are also shown.
307When used together with
308.Fl v v ,
309.Nm
310will loop and show updated queue statistics every five seconds, including
311measured bandwidth and packets per second.
312.It Fl s Cm rules
313Show the currently loaded filter rules.
314When used together with
315.Fl v ,
316the per-rule statistics (number of evaluations,
317packets and bytes) are also shown.
318Note that the
319.Dq skip step
320optimization done automatically by the kernel
321will skip evaluation of rules where possible.
322Packets passed statefully are counted in the rule that created the state
323(even though the rule isn't evaluated more than once for the entire
324connection).
325.It Fl s Cm Anchors
326Show the currently loaded anchors directly attached to the main ruleset.
327If
328.Fl a Ar anchor
329is specified as well, the anchors loaded directly below the given
330.Ar anchor
331are shown instead.
332If
333.Fl v
334is specified, all anchors attached under the target anchor will be
335displayed recursively.
336.It Fl s Cm state
337Show the contents of the state table.
338.It Fl s Cm Sources
339Show the contents of the source tracking table.
340.It Fl s Cm info
341Show filter information (statistics and counters).
342When used together with
343.Fl v ,
344source tracking statistics are also shown.
345.It Fl s Cm labels
346Show per-rule statistics (label, evaluations, packets, bytes) of
347filter rules with labels, useful for accounting.
348.It Fl s Cm timeouts
349Show the current global timeouts.
350.It Fl s Cm memory
351Show the current pool memory hard limits.
352.It Fl s Cm Tables
353Show the list of tables.
354.It Fl s Cm osfp
355Show the list of operating system fingerprints.
356.It Fl s Cm Interfaces
357Show the list of interfaces and interface drivers available to PF.
358When used together with a double
359.Fl v ,
360interface statistics are also shown.
361.Fl i
362can be used to select an interface or a group of interfaces.
363.It Fl s Cm all
364Show all of the above, except for the lists of interfaces and operating
365system fingerprints.
366.El
367.It Fl T Ar command Op Ar address ...
368Specify the
369.Ar command
370(may be abbreviated) to apply to the table.
371Commands include:
372.Pp
373.Bl -tag -width xxxxxxxxxxxx -compact
374.It Fl T Cm kill
375Kill a table.
376.It Fl T Cm flush
377Flush all addresses of a table.
378.It Fl T Cm add
379Add one or more addresses in a table.
380Automatically create a nonexisting table.
381.It Fl T Cm delete
382Delete one or more addresses from a table.
383.It Fl T Cm replace
384Replace the addresses of the table.
385Automatically create a nonexisting table.
386.It Fl T Cm show
387Show the content (addresses) of a table.
388.It Fl T Cm test
389Test if the given addresses match a table.
390.It Fl T Cm zero
391Clear all the statistics of a table.
392.It Fl T Cm load
393Load only the table definitions from
394.Xr pf.conf 5 .
395This is used in conjunction with the
396.Fl f
397flag, as in:
398.Bd -literal -offset indent
399# pfctl -Tl -f pf.conf
400.Ed
401.El
402.Pp
403For the
404.Cm add ,
405.Cm delete ,
406.Cm replace ,
407and
408.Cm test
409commands, the list of addresses can be specified either directly on the command
410line and/or in an unformatted text file, using the
411.Fl f
412flag.
413Comments starting with a
414.Sq #
415are allowed in the text file.
416With these commands, the
417.Fl v
418flag can also be used once or twice, in which case
419.Nm
420will print the
421detailed result of the operation for each individual address, prefixed by
422one of the following letters:
423.Pp
424.Bl -tag -width XXX -compact
425.It A
426The address/network has been added.
427.It C
428The address/network has been changed (negated).
429.It D
430The address/network has been deleted.
431.It M
432The address matches
433.Po
434.Cm test
435operation only
436.Pc .
437.It X
438The address/network is duplicated and therefore ignored.
439.It Y
440The address/network cannot be added/deleted due to conflicting
441.Sq \&!
442attributes.
443.It Z
444The address/network has been cleared (statistics).
445.El
446.Pp
447Each table maintains a set of counters that can be retrieved using the
448.Fl v
449flag of
450.Nm .
451For example, the following commands define a wide open firewall which will keep
452track of packets going to or coming from the
453.Ox
454FTP server.
455The following commands configure the firewall and send 10 pings to the FTP
456server:
457.Bd -literal -offset indent
458# printf "table \*[Lt]test\*[Gt] { ftp.NetBSD.org }\en \e
459    pass out to \*[Lt]test\*[Gt] keep state\en" | pfctl -f-
460# ping -qc10 ftp.NetBSD.org
461.Ed
462.Pp
463We can now use the table
464.Cm show
465command to output, for each address and packet direction, the number of packets
466and bytes that are being passed or blocked by rules referencing the table.
467The time at which the current accounting started is also shown with the
468.Dq Cleared
469line.
470.Bd -literal -offset indent
471# pfctl -t test -vTshow
472   129.128.5.191
473    Cleared:     Thu Feb 13 18:55:18 2003
474    In/Block:    [ Packets: 0        Bytes: 0        ]
475    In/Pass:     [ Packets: 10       Bytes: 840      ]
476    Out/Block:   [ Packets: 0        Bytes: 0        ]
477    Out/Pass:    [ Packets: 10       Bytes: 840      ]
478.Ed
479.Pp
480Similarly, it is possible to view global information about the tables
481by using the
482.Fl v
483modifier twice and the
484.Fl s
485.Cm Tables
486command.
487This will display the number of addresses on each table,
488the number of rules which reference the table, and the global
489packet statistics for the whole table:
490.Bd -literal -offset indent
491# pfctl -vvsTables
492--a-r-  test
493    Addresses:   1
494    Cleared:     Thu Feb 13 18:55:18 2003
495    References:  [ Anchors: 0        Rules: 1        ]
496    Evaluations: [ NoMatch: 3496     Match: 1        ]
497    In/Block:    [ Packets: 0        Bytes: 0        ]
498    In/Pass:     [ Packets: 10       Bytes: 840      ]
499    In/XPass:    [ Packets: 0        Bytes: 0        ]
500    Out/Block:   [ Packets: 0        Bytes: 0        ]
501    Out/Pass:    [ Packets: 10       Bytes: 840      ]
502    Out/XPass:   [ Packets: 0        Bytes: 0        ]
503.Ed
504.Pp
505As we can see here, only one packet \- the initial ping request \- matched the
506table, but all packets passing as the result of the state are correctly
507accounted for.
508Reloading the table(s) or ruleset will not affect packet accounting in any way.
509The two
510.Dq XPass
511counters are incremented instead of the
512.Dq Pass
513counters when a
514.Dq stateful
515packet is passed but doesn't match the table anymore.
516This will happen in our example if someone flushes the table while the
517.Xr ping 8
518command is running.
519.Pp
520When used with a single
521.Fl v ,
522.Nm
523will only display the first line containing the table flags and name.
524The flags are defined as follows:
525.Pp
526.Bl -tag -width XXX -compact
527.It c
528For constant tables, which cannot be altered outside
529.Xr pf.conf 5 .
530.It p
531For persistent tables, which don't get automatically killed when no rules
532refer to them.
533.It a
534For tables which are part of the
535.Em active
536tableset.
537Tables without this flag do not really exist, cannot contain addresses, and are
538only listed if the
539.Fl g
540flag is given.
541.It i
542For tables which are part of the
543.Em inactive
544tableset.
545This flag can only be witnessed briefly during the loading of
546.Xr pf.conf 5 .
547.It r
548For tables which are referenced (used) by rules.
549.It h
550This flag is set when a table in the main ruleset is hidden by one or more
551tables of the same name from anchors attached below it.
552.El
553.It Fl t Ar table
554Specify the name of the table.
555.It Fl v
556Produce more verbose output.
557A second use of
558.Fl v
559will produce even more verbose output including ruleset warnings.
560See the previous section for its effect on table commands.
561.It Fl x Ar level
562Set the debug
563.Ar level
564(may be abbreviated) to one of the following:
565.Pp
566.Bl -tag -width xxxxxxxxxxxx -compact
567.It Fl x Cm none
568Don't generate debug messages.
569.It Fl x Cm urgent
570Generate debug messages only for serious errors.
571.It Fl x Cm misc
572Generate debug messages for various errors.
573.It Fl x Cm loud
574Generate debug messages for common conditions.
575.El
576.It Fl z
577Clear per-rule statistics.
578.El
579.Sh FILES
580.Bl -tag -width "/etc/pf.conf" -compact
581.It Pa /etc/pf.conf
582Packet filter rules file.
583.It Pa /etc/pf.os
584Passive operating system fingerprint database.
585.El
586.Sh SEE ALSO
587.Xr pf 4 ,
588.Xr pf.conf 5 ,
589.Xr pf.os 5 ,
590.Xr sysctl.conf 5 ,
591.Xr authpf 8 ,
592.Xr ftp-proxy 8 ,
593.Xr rc 8 ,
594.Xr rc.conf 8 ,
595.Xr sysctl 8
596.Sh HISTORY
597The
598.Nm
599program and the
600.Xr pf 4
601filter mechanism first appeared in
602.Ox 3.0 .
603