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