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