PF(4) NetBSD Kernel Interfaces Manual PF(4) NNAAMMEE ppff - packet filter SSYYNNOOPPSSIISS ppsseeuuddoo--ddeevviiccee ppff 11 DDEESSCCRRIIPPTTIIOONN Packet filtering takes place in the kernel. A pseudo-device, _/_d_e_v_/_p_f, allows userland processes to control the behavior of the packet filter through an ioctl(2) interface. There are commands to enable and disable the filter, load rulesets, add and remove individual rules or retrieve state table entries and statistics. The most commonly used functions are covered by pfctl(8). Manipulations like loading a ruleset that involve more than a single ioctl call require a so-called ticket, which prevents the occurrence of multiple concurrent manipulations. Fields of ioctl parameter structures that refer to packet data (like addresses and ports) are generally expected in network byte-order. FFIILLEESS /dev/pf packet filtering device. IIOOCCTTLL IINNTTEERRFFAACCEE pf supports the following ioctl(2) commands: DIOCSTART Starts the packet filter. DIOCSTOP Stops the packet filter. DIOCSTARTALTQ Starts the ALTQ bandwidth control system. DIOCSTOPALTQ Stops the ALTQ bandwidth control system. DIOCBEGINADDRS _u___i_n_t_3_2___t Clears the buffer address pool and returns a ticket for subse- quent DIOCADDADDR, DIOCADDRULE and DIOCCHANGERULE calls. DIOCADDADDR _s_t_r_u_c_t _p_f_i_o_c___p_o_o_l_a_d_d_r struct pfioc_pooladdr { u_int32_t action; u_int32_t ticket; u_int32_t nr; u_int32_t r_num; u_int8_t r_action; u_int8_t r_last; u_int8_t af; char anchor[PF_ANCHOR_NAME_SIZE]; char ruleset[PF_RULESET_NAME_SIZE]; struct pf_pooladdr addr; }; Adds pool address _a_d_d_r to the buffer address pool to be used in the following DIOCADDRULE or DIOCCHANGERULE call. All other mem- bers of the structure are ignored. DIOCBEGINRULES _u___i_n_t_3_2___t Clears the inactive ruleset for the type of rule indicated by _r_u_l_e_._a_c_t_i_o_n and returns a ticket for subsequent DIOCADDRULE and DIOCCOMMITRULES calls. DIOCADDRULE _s_t_r_u_c_t _p_f_i_o_c___r_u_l_e struct pfioc_rule { u_int32_t action; u_int32_t ticket; u_int32_t pool_ticket; u_int32_t nr; char anchor[PF_ANCHOR_NAME_SIZE]; char ruleset[PF_RULESET_NAME_SIZE]; struct pf_rule rule; }; Adds _r_u_l_e at the end of the inactive ruleset. Requires _t_i_c_k_e_t obtained through preceding DIOCBEGINRULES call, and _p_o_o_l___t_i_c_k_e_t obtained through DIOCBEGINADDRS call. DIOCADDADDR must also be called if any pool addresses are required. The optional _a_n_c_h_o_r and _r_u_l_e_s_e_t names indicate the anchor and ruleset in which to append the rule. _n_r and _a_c_t_i_o_n are ignored. DIOCCOMMITRULES _u___i_n_t_3_2___t Switch inactive to active filter ruleset. Requires _t_i_c_k_e_t. DIOCBEGINALTQS _u___i_n_t_3_2___t Clears the inactive list of queues and returns a ticket for sub- sequent DIOCADDALTQ and DIOCCOMMITALTQS calls. DIOCADDALTQ _s_t_r_u_c_t _p_f_i_o_c___a_l_t_q Adds struct pfioc_altq { u_int32_t ticket; u_int32_t nr; struct pf_altq altq; }; DIOCCOMMITALTQS _u___i_n_t_3_2___t Switch inactive to active list of queues. Requires _t_i_c_k_e_t. DIOCGETRULES _s_t_r_u_c_t _p_f_i_o_c___r_u_l_e Returns _t_i_c_k_e_t for subsequent DIOCGETRULE calls and _n_r of rules in the active ruleset. DIOCGETRULE _s_t_r_u_c_t _p_f_i_o_c___r_u_l_e Returns _r_u_l_e number _n_r using _t_i_c_k_e_t obtained through a preceding DIOCGETRULES call. DIOCGETADDRS _s_t_r_u_c_t _p_f_i_o_c___p_o_o_l_a_d_d_r Returns _t_i_c_k_e_t for subsequent DIOCGETADDR calls and _n_r of pool addresses in the rule specified with _r___a_c_t_i_o_n, _r___n_u_m, _a_n_c_h_o_r and _r_u_l_e_s_e_t. DIOCGETADDR _s_t_r_u_c_t _p_f_i_o_c___p_o_o_l_a_d_d_r Returns pool address _a_d_d_r number _n_r from the rule specified with _r___a_c_t_i_o_n, _r___n_u_m, _a_n_c_h_o_r and _r_u_l_e_s_e_t using _t_i_c_k_e_t obtained through a preceding DIOCGETADDRS call. DIOCGETALTQS _s_t_r_u_c_t _p_f_i_o_c___a_l_t_q Returns _t_i_c_k_e_t for subsequent DIOCGETALTQ calls and _n_r of queues in the active list. DIOCGETALTQ _s_t_r_u_c_t _p_f_i_o_c___a_l_t_q Returns _a_l_t_q number _n_r using _t_i_c_k_e_t obtained through a preceding DIOCGETALTQS call. DIOCGETQSTATS _s_t_r_u_c_t _p_f_i_o_c___q_s_t_a_t_s Returns statistics on a queue. struct pfioc_qstats { u_int32_t ticket; u_int32_t nr; void *buf; int nbytes; u_int8_t scheduler; }; A pointer to a buffer of statistics _b_u_f of length _n_b_y_t_e_s for the queue specified by _n_r. DIOCCLRSTATES Clears the state table. DIOCADDSTATE _s_t_r_u_c_t _p_f_i_o_c___s_t_a_t_e Adds a state entry. DIOCGETSTATE _s_t_r_u_c_t _p_f_i_o_c___s_t_a_t_e struct pfioc_state { u_int32_t nr; struct pf_state state; }; Extracts the entry with the specified number from the state ta- ble. DIOCKILLSTATES _s_t_r_u_c_t _p_f_i_o_c___s_t_a_t_e___k_i_l_l Removes matching entries from the state table. Returns the num- ber of killed states in psk_af. struct pfioc_state_kill { int psk_af; int psk_proto; struct pf_rule_addr psk_src; struct pf_rule_addr psk_dst; }; DIOCSETSTATUSIF _s_t_r_u_c_t _p_f_i_o_c___i_f struct pfioc_if { char ifname[IFNAMSIZ]; }; Specifies the interface for which statistics are accumulated. DIOCGETSTATUS _s_t_r_u_c_t _p_f___s_t_a_t_u_s struct pf_status { u_int64_t counters[PFRES_MAX]; u_int64_t fcounters[FCNT_MAX]; u_int64_t pcounters[2][2][3]; u_int64_t bcounters[2][2]; u_int32_t running; u_int32_t states; u_int32_t since; u_int32_t debug; }; Gets the internal packet filter statistics. DIOCCLRSTATUS Clears the internal packet filter statistics. DIOCNATLOOK _s_t_r_u_c_t _p_f_i_o_c___n_a_t_l_o_o_k Looks up a state table entry by source and destination addresses and ports. struct pfioc_natlook { struct pf_addr saddr; struct pf_addr daddr; struct pf_addr rsaddr; struct pf_addr rdaddr; u_int16_t sport; u_int16_t dport; u_int16_t rsport; u_int16_t rdport; u_int8_t af; u_int8_t proto; u_int8_t direction; }; DIOCSETDEBUG _u___i_n_t_3_2___t Sets the debug level. enum { PF_DEBUG_NONE=0, PF_DEBUG_URGENT=1, PF_DEBUG_MISC=2 }; DIOCGETSTATES _s_t_r_u_c_t _p_f_i_o_c___s_t_a_t_e_s struct pfioc_states { int ps_len; union { caddr_t psu_buf; struct pf_state *psu_states; } ps_u; #define ps_buf ps_u.psu_buf #define ps_states ps_u.psu_states }; DIOCCHANGERULE _s_t_r_u_c_t _p_f_i_o_c___r_u_l_e Adds or removes the _r_u_l_e in the ruleset specified by _r_u_l_e_._a_c_t_i_o_n. enum { PF_CHANGE_ADD_HEAD=1, PF_CHANGE_ADD_TAIL=2, PF_CHANGE_ADD_BEFORE=3, PF_CHANGE_ADD_AFTER=4, PF_CHANGE_REMOVE=5, PF_CHANGE_GET_TICKET=6 }; The type of operation to be performed is indicated by _a_c_t_i_o_n. _t_i_c_k_e_t must be set to the value obtained with PF_CHANGE_GET_TICKET for all actions except PF_CHANGE_GET_TICKET. _p_o_o_l___t_i_c_k_e_t must be set to the value obtained with the DIOCBEGI- NADDRS call for all actions except PF_CHANGE_REMOVE and PF_CHANGE_GET_TICKET. _a_n_c_h_o_r and _r_u_l_e_s_e_t indicate which anchor and ruleset the opera- tion applies to. _n_r indicates the rule number against which PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER or PF_CHANGE_REMOVE actions are applied. DIOCCHANGEADDR _s_t_r_u_c_t _p_f_i_o_c___a_d_d_r Adds or removes a pool address _a_d_d_r from a rule specified with _r___a_c_t_i_o_n, _r___n_u_m, _a_n_c_h_o_r and _r_u_l_e_s_e_t. DIOCSETTIMEOUT _s_t_r_u_c_t _p_f_i_o_c___t_m struct pfioc_tm { int timeout; int seconds; }; DIOCGETTIMEOUT _s_t_r_u_c_t _p_f_i_o_c___t_m DIOCCLRRULECTRS Clear per-rule statistics. DIOCSETLIMIT _s_t_r_u_c_t _p_f_i_o_c___l_i_m_i_t Sets hard limits on the memory pools used by the packet filter. struct pfioc_limit { int index; unsigned limit; }; DIOCGETLIMIT _s_t_r_u_c_t _p_f_i_o_c___l_i_m_i_t DIOCRCLRTABLES _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Clear all tables. All the IOCTLs that manipulate radix tables use the same structure described below. For DIOCRCLRTABLES, pfrio_ndel contains on exit the number of tables deleted. struct pfioc_table { struct pfr_table pfrio_table; void *pfrio_buffer; int pfrio_size; int pfrio_size2; int pfrio_nadd; int pfrio_ndel; int pfrio_nchange; int pfrio_flags; int pfrio_ticket; }; #define pfrio_exists pfrio_nadd #define pfrio_nzero pfrio_nadd #define pfrio_nmatch pfrio_nadd #define pfrio_naddr pfrio_size2 #define pfrio_setflag pfrio_size2 #define pfrio_clrflag pfrio_nadd DIOCRADDTABLES _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Creates one or more tables. On entry, pfrio_buffer[pfrio_size] contains a table of pfr_table structures. On exit, pfrio_nadd contains the number of tables effectively created. struct pfr_table { char pfrt_name[PF_TABLE_NAME_SIZE]; u_int32_t pfrt_flags; }; DIOCRDELTABLES _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Deletes one or more tables. On entry, pfrio_buffer[pfrio_size] contains a table of pfr_table structures. On exit, pfrio_nadd contains the number of tables effectively deleted. DIOCRGETTABLES _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Get the list of all tables. On entry, pfrio_buffer[pfrio_size] contains a valid writeable buffer for pfr_table structures. On exit, pfrio_size contains the number of tables written into the buffer. If the buffer is too small, the kernel does not store anything but just returns the required buffer size, without error. DIOCRGETTSTATS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Like DIOCRGETTABLES, but returns an array of pfr_tstats structures. struct pfr_tstats { struct pfr_table pfrts_t; u_int64_t pfrts_packets [PFR_DIR_MAX][PFR_OP_TABLE_MAX]; u_int64_t pfrts_bytes [PFR_DIR_MAX][PFR_OP_TABLE_MAX]; u_int64_t pfrts_match; u_int64_t pfrts_nomatch; long pfrts_tzero; int pfrts_cnt; int pfrts_refcnt; }; #define pfrts_name pfrts_t.pfrt_name #define pfrts_flags pfrts_t.pfrt_flags DIOCRCLRTSTATS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Clears the statistics of one or more tables. On entry, pfrio_buffer[pfrio_size] contains a table of pfr_table struc- tures. On exit, pfrio_nzero contains the number of tables effec- tively cleared. DIOCRCLRADDRS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Clear all addresses in a table. On entry, pfrio_table contains the table to clear. On exit, pfrio_ndel contains the number of addresses removed. DIOCRADDADDRS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Add one or more addresses to a table. On entry, pfrio_table con- tains the table id and pfrio_buffer[pfrio_size] contains the list of pfr_addr structures to add. On exit, pfrio_nadd contains the number of addresses effectively added. struct pfr_addr { union { struct in_addr _pfra_ip4addr; struct in6_addr _pfra_ip6addr; } pfra_u; u_int8_t pfra_af; u_int8_t pfra_net; u_int8_t pfra_not; u_int8_t pfra_fback; }; #define pfra_ip4addr pfra_u._pfra_ip4addr #define pfra_ip6addr pfra_u._pfra_ip6addr DIOCRDELADDRS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Delete one or more addresses from a table. On entry, pfrio_table contains the table id and pfrio_buffer[pfrio_size] contains the list of pfr_addr structures to delete. On exit, pfrio_ndel con- tains the number of addresses effectively deleted. DIOCRSETADDRS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Replace the content of a table by a new address list. This is the most complicated command, which uses all the structure mem- bers. On entry, pfrio_table contains the table id and pfrio_buffer[pfrio_size] contains the new list of pfr_addr struc- tures. In addition to that, if size2 is nonzero, pfrio_buffer[pfrio_size..pfrio_size2] must be a writeable buffer, into which the kernel can copy the addresses that have been deleted during the replace operation. On exit, pfrio_ndel, pfrio_nadd and pfrio_nchange contain the number of addresses deleted, added and changed by the kernel. If pfrio_size2 was set on entry, pfrio_size2 will point to the size of the buffer used, exactly like DIOCRGETADDRS. DIOCRGETADDRS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Get all the addresses of a table. On entry, pfrio_table contains the table id and pfrio_buffer[pfrio_size] contains a valid write- able buffer for pfr_addr structures. On exit, pfrio_size con- tains the number of addresses written into the buffer. If the buffer was too small, the kernel does not store anything but just return the required buffer size, without returning an error. DIOCRGETASTATS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Like DIOCRGETADDRS, but returns an array of pfr_astats structures. struct pfr_astats { struct pfr_addr pfras_a; u_int64_t pfras_packets [PFR_DIR_MAX][PFR_OP_ADDR_MAX]; u_int64_t pfras_bytes [PFR_DIR_MAX][PFR_OP_ADDR_MAX]; long pfras_tzero; }; DIOCRCLRASTATS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Clears the statistics of one or more addresses. On entry, pfrio_table contains the table id and pfrio_buffer[pfrio_size] contains a table of pfr_addr structures to clear. On exit, pfrio_nzero contains the number of addresses effectively cleared. DIOCRTSTADDRS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Test if the given addresses match a table. On entry, pfrio_table contains the table id and pfrio_buffer[pfrio_size] contains a ta- ble of pfr_addr structures to test. On exit, the kernel updates the pfr_addr table by setting the pfra_fback member appropri- ately. DIOCRSETTFLAGS _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Change the _c_o_n_s_t or _p_e_r_s_i_s_t flag of a table. On entry, pfrio_buffer[pfrio_size] contains a table of pfr_table struc- tures, and pfrio_setflag contains the flags to add, while pfrio_clrflag contains the flags to remove. On exit, pfrio_nchange and pfrio_ndel contain the number of tables altered or deleted by the kernel. Yes, tables can be deleted if one removes the _p_e_r_s_i_s_t flag of an unreferenced table. DIOCRINABEGIN _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Starts a transaction with the inactive set of tables. Cleans up any leftover from a previously aborted transaction, and returns a new ticket. On exit, pfrio_ndel contains the number of leftover table deleted, and pfrio_ticket contains a valid ticket to use for the following two IOCTLs. DIOCRINACOMMIT _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Commit the inactive set of tables into the active set. While copying the addresses, do a best effort to keep statistics for addresses present before and after the commit. On entry, io->pfrio_ticket takes a valid ticket. On exit, io->pfrio_nadd and io->pfrio_nchange contain the number of tables added and altered by the commit operation. DIOCRINADEFINE _s_t_r_u_c_t _p_f_i_o_c___t_a_b_l_e Defines a table in the inactive set. On entry, pfrio_table con- tains the table id and pfrio_buffer[pfrio_size] contains the list of pfr_addr structures to put in the table. A valid ticket must also be supplied to pfrio_ticket. On exit, pfrio_nadd contains 0 if the table was already defined in the inactive list, or 1 if a new table has been created. pfrio_naddr contains the number of addresses effectively put in the table. EEXXAAMMPPLLEESS The following example demonstrates how to use the DIOCNATLOOK command to find the internal host/port of a NATed connection. #include #include #include #include #include #include #include #include u_int32_t read_address(const char *s) { int a, b, c, d; sscanf(s, "%i.%i.%i.%i", &a, &b, &c, &d); return htonl(a << 24 | b << 16 | c << 8 | d); } void print_address(u_int32_t a) { a = ntohl(a); printf("%d.%d.%d.%d", a >> 24 & 255, a >> 16 & 255, a >> 8 & 255, a & 255); } int main(int argc, char *argv[]) { struct pfioc_natlook nl; int dev; if (argc != 5) { printf("%s \n", argv[0]); return 1; } dev = open("/dev/pf", O_RDWR); if (dev == -1) err(1, "open(\"/dev/pf\") failed"); memset(&nl, 0, sizeof(struct pfioc_natlook)); nl.saddr.v4.s_addr = read_address(argv[1]); nl.sport = htons(atoi(argv[2])); nl.daddr.v4.s_addr = read_address(argv[3]); nl.dport = htons(atoi(argv[4])); nl.af = AF_INET; nl.proto = IPPROTO_TCP; nl.direction = PF_IN; if (ioctl(dev, DIOCNATLOOK, &nl)) err(1, "DIOCNATLOOK"); printf("internal host "); print_address(nl.rsaddr.v4.s_addr); printf(":%u\n", ntohs(nl.rsport)); return 0; } SSEEEE AALLSSOO ioctl(2), bridge(4), pflog(4), pfsync(4), pfctl(8) HHIISSTTOORRYY The ppff packet filtering mechanism first appeared in OpenBSD 3.0. NetBSD 2.0_BETA June 24, 2001 NetBSD 2.0_BETA