1 .\" $OpenBSD: pf.4,v 1.58 2007/02/09 11:39:06 henning Exp $
3 .\" Copyright (C) 2001, Kjell Wooding. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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. Neither the name of the project nor the names of its contributors
14 .\" may be used to endorse or promote products derived from this software
15 .\" without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 Packet filtering takes place in the kernel.
41 allows userland processes to control the
42 behavior of the packet filter through an
45 There are commands to enable and disable the filter, load rulesets,
46 add and remove individual rules or state table entries,
47 and retrieve statistics.
48 The most commonly used functions are covered by
51 Manipulations like loading a ruleset that involve more than a single
53 call require a so-called
55 which prevents the occurrence of
56 multiple concurrent manipulations.
60 parameter structures that refer to packet data (like
61 addresses and ports) are generally expected in network byte-order.
63 Rules and address tables are contained in so-called
67 request, if the anchor field of the argument structure is empty,
68 the kernel will use the default anchor (i.e., the main ruleset)
70 Anchors are specified by name and may be nested, with components
73 characters, similar to how file system hierarchies are laid out.
74 The final component of the anchor path is the anchor under which
75 operations will be performed.
78 supports the following
80 commands, available through
82 .Bl -tag -width xxxxxx
84 Start the packet filter.
86 Stop the packet filter.
88 Start the ALTQ bandwidth control system (see
91 Stop the ALTQ bandwidth control system.
92 .It Dv DIOCBEGINADDRS Fa "struct pfioc_pooladdr *pp"
94 struct pfioc_pooladdr {
102 char anchor[MAXPATHLEN];
103 struct pf_pooladdr addr;
107 Clear the buffer address pool and get a
115 .It Dv DIOCADDADDR Fa "struct pfioc_pooladdr *pp"
119 to the buffer address pool to be used in the following
124 All other members of the structure are ignored.
125 .It Dv DIOCADDRULE Fa "struct pfioc_rule *pr"
130 u_int32_t pool_ticket;
132 char anchor[MAXPATHLEN];
133 char anchor_call[MAXPATHLEN];
140 at the end of the inactive ruleset.
143 obtained through a preceding
151 must also be called if any pool addresses are required.
154 name indicates the anchor in which to append the rule.
159 .It Dv DIOCADDALTQ Fa "struct pfioc_altq *pa"
160 Add an ALTQ discipline or queue.
169 .It Dv DIOCGETRULES Fa "struct pfioc_rule *pr"
176 of rules in the active ruleset.
177 .It Dv DIOCGETRULE Fa "struct pfioc_rule *pr"
184 obtained through a preceding
190 .Dv PF_GET_CLR_CNTR ,
191 the per-rule statistics on the requested rule are cleared.
192 .It Dv DIOCGETADDRS Fa "struct pfioc_pooladdr *pp"
199 of pool addresses in the rule specified with
204 .It Dv DIOCGETADDR Fa "struct pfioc_pooladdr *pp"
209 from the rule specified with
216 obtained through a preceding
219 .It Dv DIOCGETALTQS Fa "struct pfioc_altq *pa"
226 of queues in the active list.
227 .It Dv DIOCGETALTQ Fa "struct pfioc_altq *pa"
228 Get the queueing discipline
234 obtained through a preceding
237 .It Dv DIOCGETQSTATS Fa "struct pfioc_qstats *pq"
238 Get the statistics on a queue.
240 struct pfioc_qstats {
249 This call fills in a pointer to the buffer of statistics
253 for the queue specified by
255 .It Dv DIOCGETRULESETS Fa "struct pfioc_ruleset *pr"
257 struct pfioc_ruleset {
259 char path[MAXPATHLEN];
260 char name[PF_ANCHOR_NAME_SIZE];
266 of rulesets (i.e., anchors) directly attached to the anchor named by
268 for use in subsequent
271 Nested anchors, since they are not directly attached to the given
272 anchor, will not be included.
275 if the given anchor does not exist.
276 .It Dv DIOCGETRULESET Fa "struct pfioc_ruleset *pr"
277 Get a ruleset (i.e., an anchor)
281 from the given anchor
283 the maximum number of which can be obtained from a preceding
288 if the given anchor does not exist or
290 if another process is concurrently updating a ruleset.
291 .It Dv DIOCADDSTATE Fa "struct pfioc_state *ps"
296 struct pf_state state;
299 .It Dv DIOCGETSTATE Fa "struct pfioc_state *ps"
300 Extract the entry with the specified number
302 from the state table.
303 .It Dv DIOCKILLSTATES Fa "struct pfioc_state_kill *psk"
304 Remove matching entries from the state table.
305 This ioctl returns the number of killed states in
308 struct pfioc_state_kill {
311 struct pf_rule_addr psk_src;
312 struct pf_rule_addr psk_dst;
313 char psk_ifname[IFNAMSIZ];
316 .It Dv DIOCCLRSTATES Fa "struct pfioc_state_kill *psk"
329 .It Dv DIOCSETSTATUSIF Fa "struct pfioc_if *pi"
330 Specify the interface for which statistics are accumulated.
333 char ifname[IFNAMSIZ];
336 .It Dv DIOCGETSTATUS Fa "struct pf_status *s"
337 Get the internal packet filter statistics.
340 u_int64_t counters[PFRES_MAX];
341 u_int64_t lcounters[LCNT_MAX];
342 u_int64_t fcounters[FCNT_MAX];
343 u_int64_t scounters[SCNT_MAX];
344 u_int64_t pcounters[2][2][3];
345 u_int64_t bcounters[2][2];
351 char ifname[IFNAMSIZ];
352 u_int8_t pf_chksum[MD5_DIGEST_LENGTH];
356 Clear the internal packet filter statistics.
357 .It Dv DIOCNATLOOK Fa "struct pfioc_natlook *pnl"
358 Look up a state table entry by source and destination addresses and ports.
360 struct pfioc_natlook {
361 struct pf_addr saddr;
362 struct pf_addr daddr;
363 struct pf_addr rsaddr;
364 struct pf_addr rdaddr;
374 .It Dv DIOCSETDEBUG Fa "u_int32_t *level"
377 enum { PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC,
380 .It Dv DIOCGETSTATES Fa "struct pfioc_states *ps"
381 Get state table entries.
383 struct pfioc_states {
387 struct pf_state *psu_states;
389 #define ps_buf ps_u.psu_buf
390 #define ps_states ps_u.psu_states
396 is non-zero on entry, as many states as possible that can fit into this
397 size will be copied into the supplied buffer
401 is always set to the total size required to hold all state table entries
403 .Li sizeof(struct pf_state) * nr ) .
404 .It Dv DIOCCHANGERULE Fa "struct pfioc_rule *pcr"
407 in the ruleset specified by
410 The type of operation to be performed is indicated by
412 which can be any of the following:
414 enum { PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
415 PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
416 PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
420 must be set to the value obtained with
421 .Dv PF_CHANGE_GET_TICKET
422 for all actions except
423 .Dv PF_CHANGE_GET_TICKET .
425 must be set to the value obtained with the
427 call for all actions except
430 .Dv PF_CHANGE_GET_TICKET .
432 indicates to which anchor the operation applies.
434 indicates the rule number against which
435 .Dv PF_CHANGE_ADD_BEFORE ,
436 .Dv PF_CHANGE_ADD_AFTER ,
440 .\" It Dv DIOCCHANGEALTQ Fa "struct pfioc_altq *pcr"
441 .It Dv DIOCCHANGEADDR Fa "struct pfioc_pooladdr *pca"
442 Add or remove the pool address
444 from the rule specified by
449 .It Dv DIOCSETTIMEOUT Fa "struct pfioc_tm *pt"
457 Set the state timeout of
461 The old value will be placed into
463 For possible values of
469 .It Dv DIOCGETTIMEOUT Fa "struct pfioc_tm *pt"
470 Get the state timeout of
472 The value will be placed into the
475 .It Dv DIOCCLRRULECTRS
476 Clear per-rule statistics.
477 .It Dv DIOCSETLIMIT Fa "struct pfioc_limit *pl"
478 Set the hard limits on the memory pools used by the packet filter.
485 enum { PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
486 PF_LIMIT_TABLES, PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
488 .It Dv DIOCGETLIMIT Fa "struct pfioc_limit *pl"
491 for the memory pool indicated by
493 .It Dv DIOCRCLRTABLES Fa "struct pfioc_table *io"
495 All the ioctls that manipulate radix tables
496 use the same structure described below.
500 contains on exit the number of tables deleted.
503 struct pfr_table pfrio_table;
512 u_int32_t pfrio_ticket;
514 #define pfrio_exists pfrio_nadd
515 #define pfrio_nzero pfrio_nadd
516 #define pfrio_nmatch pfrio_nadd
517 #define pfrio_naddr pfrio_size2
518 #define pfrio_setflag pfrio_size2
519 #define pfrio_clrflag pfrio_nadd
521 .It Dv DIOCRADDTABLES Fa "struct pfioc_table *io"
522 Create one or more tables.
525 must point to an array of
532 .Vt struct pfr_table .
535 contains the number of tables effectively created.
538 char pfrt_anchor[MAXPATHLEN];
539 char pfrt_name[PF_TABLE_NAME_SIZE];
540 u_int32_t pfrt_flags;
544 .It Dv DIOCRDELTABLES Fa "struct pfioc_table *io"
545 Delete one or more tables.
548 must point to an array of
555 .Vt struct pfr_table .
558 contains the number of tables effectively deleted.
559 .It Dv DIOCRGETTABLES Fa "struct pfioc_table *io"
560 Get the list of all tables.
562 .Va pfrio_buffer[pfrio_size]
563 contains a valid writeable buffer for
568 contains the number of tables written into the buffer.
569 If the buffer is too small, the kernel does not store anything but just
570 returns the required buffer size, without error.
571 .It Dv DIOCRGETTSTATS Fa "struct pfioc_table *io"
574 but is used to get an array of
579 struct pfr_table pfrts_t;
580 u_int64_t pfrts_packets
581 [PFR_DIR_MAX][PFR_OP_TABLE_MAX];
582 u_int64_t pfrts_bytes
583 [PFR_DIR_MAX][PFR_OP_TABLE_MAX];
584 u_int64_t pfrts_match;
585 u_int64_t pfrts_nomatch;
588 int pfrts_refcnt[PFR_REFCNT_MAX];
590 #define pfrts_name pfrts_t.pfrt_name
591 #define pfrts_flags pfrts_t.pfrt_flags
593 .It Dv DIOCRCLRTSTATS Fa "struct pfioc_table *io"
594 Clear the statistics of one or more tables.
597 must point to an array of
604 .Vt struct pfr_table .
607 contains the number of tables effectively cleared.
608 .It Dv DIOCRCLRADDRS Fa "struct pfioc_table *io"
609 Clear all addresses in a table.
612 contains the table to clear.
615 contains the number of addresses removed.
616 .It Dv DIOCRADDADDRS Fa "struct pfioc_table *io"
617 Add one or more addresses to a table.
620 contains the table ID and
622 must point to an array of
626 elements to add to the table.
629 .Vt struct pfr_addr .
632 contains the number of addresses effectively added.
636 struct in_addr _pfra_ip4addr;
637 struct in6_addr _pfra_ip6addr;
644 #define pfra_ip4addr pfra_u._pfra_ip4addr
645 #define pfra_ip6addr pfra_u._pfra_ip6addr
647 .It Dv DIOCRDELADDRS Fa "struct pfioc_table *io"
648 Delete one or more addresses from a table.
651 contains the table ID and
653 must point to an array of
657 elements to delete from the table.
660 .Vt struct pfr_addr .
663 contains the number of addresses effectively deleted.
664 .It Dv DIOCRSETADDRS Fa "struct pfioc_table *io"
665 Replace the content of a table by a new address list.
666 This is the most complicated command, which uses all the structure members.
670 contains the table ID and
672 must point to an array of
676 elements which become the new contents of the table.
679 .Vt struct pfr_addr .
683 .Va pfrio_buffer[pfrio_size..pfrio_size2]
684 must be a writeable buffer, into which the kernel can copy the
685 addresses that have been deleted during the replace operation.
691 contain the number of addresses deleted, added, and changed by the
697 will point to the size of the buffer used, exactly like
699 .It Dv DIOCRGETADDRS Fa "struct pfioc_table *io"
700 Get all the addresses of a table.
703 contains the table ID and
704 .Va pfrio_buffer[pfrio_size]
705 contains a valid writeable buffer for
710 contains the number of addresses written into the buffer.
711 If the buffer was too small, the kernel does not store anything but just
712 returns the required buffer size, without returning an error.
713 .It Dv DIOCRGETASTATS Fa "struct pfioc_table *io"
716 but is used to get an array of
721 struct pfr_addr pfras_a;
722 u_int64_t pfras_packets
723 [PFR_DIR_MAX][PFR_OP_ADDR_MAX];
724 u_int64_t pfras_bytes
725 [PFR_DIR_MAX][PFR_OP_ADDR_MAX];
729 .It Dv DIOCRCLRASTATS Fa "struct pfioc_table *io"
730 Clear the statistics of one or more addresses.
733 contains the table ID and
735 must point to an array of
739 elements to be cleared from the table.
742 .Vt struct pfr_addr .
745 contains the number of addresses effectively cleared.
746 .It Dv DIOCRTSTADDRS Fa "struct pfioc_table *io"
747 Test if the given addresses match a table.
750 contains the table ID and
752 must point to an array of
756 elements, each of which will be tested for a match in the table.
759 .Vt struct pfr_addr .
760 On exit, the kernel updates the
764 member appropriately.
765 .It Dv DIOCRSETTFLAGS Fa "struct pfioc_table *io"
769 .Dv PFR_TFLAG_PERSIST
773 must point to an array of
780 .Vt struct pfr_table .
782 must contain the flags to add, while
784 must contain the flags to remove.
789 contain the number of tables altered or deleted by the kernel.
790 Yes, tables can be deleted if one removes the
791 .Dv PFR_TFLAG_PERSIST
792 flag of an unreferenced table.
793 .It Dv DIOCRINADEFINE Fa "struct pfioc_table *io"
794 Defines a table in the inactive set.
797 contains the table ID and
798 .Va pfrio_buffer[pfrio_size]
801 structures to put in the table.
802 A valid ticket must also be supplied to
806 contains 0 if the table was already defined in the inactive list
807 or 1 if a new table has been created.
809 contains the number of addresses effectively put in the table.
810 .It Dv DIOCXBEGIN Fa "struct pfioc_trans *io"
813 int size; /* number of elements */
814 int esize; /* size of each element in bytes */
815 struct pfioc_trans_e {
817 char anchor[MAXPATHLEN];
823 Clear all the inactive rulesets specified in the
826 For each ruleset, a ticket is returned for subsequent "add rule" ioctls,
833 Ruleset types, identified by
835 include the following:
837 .Bl -tag -width PF_RULESET_FILTER -offset ind -compact
838 .It Dv PF_RULESET_SCRUB
839 Scrub (packet normalization) rules.
840 .It Dv PF_RULESET_FILTER
842 .It Dv PF_RULESET_NAT
843 NAT (Network Address Translation) rules.
844 .It Dv PF_RULESET_BINAT
845 Bidirectional NAT rules.
846 .It Dv PF_RULESET_RDR
848 .It Dv PF_RULESET_ALTQ
850 .It Dv PF_RULESET_TABLE
853 .It Dv DIOCXCOMMIT Fa "struct pfioc_trans *io"
854 Atomically switch a vector of inactive rulesets to the active rulesets.
855 This call is implemented as a standard two-phase commit, which will either
856 fail for all rulesets or completely succeed.
857 All tickets need to be valid.
860 if another process is concurrently updating some of the same rulesets.
861 .It Dv DIOCXROLLBACK Fa "struct pfioc_trans *io"
862 Clean up the kernel by undoing all changes that have taken place on the
863 inactive rulesets since the last
866 will silently ignore rulesets for which the ticket is invalid.
867 .It Dv DIOCSETHOSTID Fa "u_int32_t *hostid"
868 Set the host ID, which is used by
870 to identify which host created state table entries.
872 Flush the passive OS fingerprint table.
873 .It Dv DIOCOSFPADD Fa "struct pf_osfp_ioctl *io"
875 struct pf_osfp_ioctl {
876 struct pf_osfp_entry {
877 SLIST_ENTRY(pf_osfp_entry) fp_entry;
879 char fp_class_nm[PF_OSFP_LEN];
880 char fp_version_nm[PF_OSFP_LEN];
881 char fp_subtype_nm[PF_OSFP_LEN];
883 pf_tcpopts_t fp_tcpopts;
895 Add a passive OS fingerprint to the table.
898 to the packed fingerprint,
899 .Va fp_os.fp_class_nm
900 to the name of the class (Linux, Windows, etc),
901 .Va fp_os.fp_version_nm
902 to the name of the version (NT, 95, 98), and
903 .Va fp_os.fp_subtype_nm
904 to the name of the subtype or patchlevel.
913 are set to the TCP MSS, the TCP window size, the IP length, the IP TTL,
914 the number of TCP options, and the TCP window scaling constant of the
915 TCP SYN packet, respectively.
919 member is filled according to the
926 member contains packed TCP options.
928 .Dv PF_OSFP_TCPOPT_BITS
929 bits in the packed value.
930 Options include any of
931 .Dv PF_OSFP_TCPOPT_NOP ,
932 .Dv PF_OSFP_TCPOPT_SACK ,
933 .Dv PF_OSFP_TCPOPT_WSCALE ,
934 .Dv PF_OSFP_TCPOPT_MSS ,
936 .Dv PF_OSFP_TCPOPT_TS .
940 member is not used with this ioctl.
942 The structure's slack space must be zeroed for correct operation;
944 the whole structure to zero before filling and sending to the kernel.
945 .It Dv DIOCOSFPGET Fa "struct pf_osfp_ioctl *io"
946 Get the passive OS fingerprint number
948 from the kernel's fingerprint list.
949 The rest of the structure members will come back filled.
950 Get the whole list by repeatedly incrementing the
952 number until the ioctl returns
954 .It Dv DIOCGETSRCNODES Fa "struct pfioc_src_nodes *psn"
956 struct pfioc_src_nodes {
960 struct pf_src_node *psu_src_nodes;
962 #define psn_buf psn_u.psu_buf
963 #define psn_src_nodes psn_u.psu_src_nodes
967 Get the list of source nodes kept by sticky addresses and source
969 The ioctl must be called once with
972 If the ioctl returns without error,
974 will be set to the size of the buffer required to hold all the
976 structures held in the table.
977 A buffer of this size should then be allocated, and a pointer to this buffer
980 The ioctl must then be called again to fill this buffer with the actual
984 will be set to the length of the buffer actually used.
985 .It Dv DIOCCLRSRCNODES
986 Clear the tree of source tracking nodes.
987 .It Dv DIOCIGETIFACES Fa "struct pfioc_iface *io"
988 Get the list of interfaces and interface drivers known to
990 All the ioctls that manipulate interfaces
991 use the same structure described below:
994 char pfiio_name[IFNAMSIZ];
1005 can be used to restrict the search to a specific interface or driver.
1006 .Va pfiio_buffer[pfiio_size]
1007 is the user-supplied buffer for returning the data.
1010 contains the number of
1012 entries that can fit into the buffer.
1013 The kernel will replace this value by the real number of entries it wants
1017 .Li sizeof(struct pfi_kif) .
1019 The data is returned in the
1021 structure described below:
1024 RB_ENTRY(pfi_kif) pfik_tree;
1025 char pfik_name[IFNAMSIZ];
1026 u_int64_t pfik_packets[2][2][2];
1027 u_int64_t pfik_bytes[2][2][2];
1028 u_int32_t pfik_tzero;
1030 struct pf_state_tree_lan_ext pfik_lan_ext;
1031 struct pf_state_tree_ext_gwy pfik_ext_gwy;
1032 TAILQ_ENTRY(pfi_kif) pfik_w_states;
1033 void *pfik_ah_cookie;
1034 struct ifnet *pfik_ifp;
1035 struct ifg_group *pfik_group;
1038 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs;
1041 .It Dv DIOCSETIFFLAG Fa "struct pfioc_iface *io"
1042 Set the user setable flags (described above) of the
1044 internal interface description.
1045 The filtering process is the same as for
1046 .Dv DIOCIGETIFACES .
1048 #define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */
1050 .It Dv DIOCCLRIFFLAG Fa "struct pfioc_iface *io"
1053 above but clears the flags.
1056 .Bl -tag -width /dev/pf -compact
1058 packet filtering device.
1061 The following example demonstrates how to use the
1063 command to find the internal host/port of a NATed connection:
1065 #include <sys/types.h>
1066 #include <sys/socket.h>
1067 #include <sys/ioctl.h>
1068 #include <sys/fcntl.h>
1070 #include <netinet/in.h>
1071 #include <net/pfvar.h>
1077 read_address(const char *s)
1081 sscanf(s, "%i.%i.%i.%i", &a, &b, &c, &d);
1082 return htonl(a << 24 | b << 16 | c << 8 | d);
1086 print_address(u_int32_t a)
1089 printf("%d.%d.%d.%d", a >> 24 & 255, a >> 16 & 255,
1090 a >> 8 & 255, a & 255);
1094 main(int argc, char *argv[])
1096 struct pfioc_natlook nl;
1100 printf("%s <gwy addr> <gwy port> <ext addr> <ext port>\\n",
1105 dev = open("/dev/pf", O_RDWR);
1107 err(1, "open(\\"/dev/pf\\") failed");
1109 memset(&nl, 0, sizeof(struct pfioc_natlook));
1110 nl.saddr.v4.s_addr = read_address(argv[1]);
1111 nl.sport = htons(atoi(argv[2]));
1112 nl.daddr.v4.s_addr = read_address(argv[3]);
1113 nl.dport = htons(atoi(argv[4]));
1115 nl.proto = IPPROTO_TCP;
1116 nl.direction = PF_IN;
1118 if (ioctl(dev, DIOCNATLOOK, &nl))
1119 err(1, "DIOCNATLOOK");
1121 printf("internal host ");
1122 print_address(nl.rsaddr.v4.s_addr);
1123 printf(":%u\\n", ntohs(nl.rsport));
1137 packet filtering mechanism first appeared in
1139 and was imported into
1141 by Devon H. O'Dell and Simon Schubert.