xref: /freebsd/share/man/man4/pf.4 (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1.\"	$OpenBSD: pf.4,v 1.62 2008/09/10 14:57:37 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. 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.
16.\"
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
27.\" SUCH DAMAGE.
28.\"
29.Dd August 28, 2023
30.Dt PF 4
31.Os
32.Sh NAME
33.Nm pf
34.Nd packet filter
35.Sh SYNOPSIS
36.Cd "device pf"
37.Cd "options PF_DEFAULT_TO_DROP"
38.Sh DESCRIPTION
39Packet filtering takes place in the kernel.
40A pseudo-device,
41.Pa /dev/pf ,
42allows userland processes to control the
43behavior of the packet filter through an
44.Xr ioctl 2
45interface.
46There are commands to enable and disable the filter, load rulesets,
47add and remove individual rules or state table entries,
48and retrieve statistics.
49The most commonly used functions are covered by
50.Xr pfctl 8 .
51.Pp
52Manipulations like loading a ruleset that involve more than a single
53.Xr ioctl 2
54call require a so-called
55.Em ticket ,
56which prevents the occurrence of
57multiple concurrent manipulations.
58.Pp
59Fields of
60.Xr ioctl 2
61parameter structures that refer to packet data (like
62addresses and ports) are generally expected in network byte-order.
63.Pp
64Rules and address tables are contained in so-called
65.Em anchors .
66When servicing an
67.Xr ioctl 2
68request, if the anchor field of the argument structure is empty,
69the kernel will use the default anchor (i.e., the main ruleset)
70in operations.
71Anchors are specified by name and may be nested, with components
72separated by
73.Sq /
74characters, similar to how file system hierarchies are laid out.
75The final component of the anchor path is the anchor under which
76operations will be performed.
77.Sh SYSCTL VARIABLES AND LOADER TUNABLES
78The following
79.Xr loader 8
80tunables are available.
81.Bl -tag -width indent
82.It Va net.pf.states_hashsize
83Size of hash tables that store states.
84Should be power of 2.
85Default value is 131072.
86.It Va net.pf.source_nodes_hashsize
87Size of hash table that store source nodes.
88Should be power of 2.
89Default value is 32768.
90.El
91.Pp
92Read only
93.Xr sysctl 8
94variables with matching names are provided to obtain current values
95at runtime.
96.Sh KERNEL OPTIONS
97The following options in the kernel configuration file are related to
98.Nm
99operation:
100.Pp
101.Bl -tag -width ".Dv PF_DEFAULT_TO_DROP" -compact
102.It Dv PF_DEFAULT_TO_DROP
103Change default policy to drop by default
104.El
105.Sh IOCTL INTERFACE
106.Nm
107supports the following
108.Xr ioctl 2
109commands, available through
110.Aq Pa net/pfvar.h :
111.Bl -tag -width xxxxxx
112.It Dv DIOCSTART
113Start the packet filter.
114.It Dv DIOCSTOP
115Stop the packet filter.
116.It Dv DIOCSTARTALTQ
117Start the ALTQ bandwidth control system (see
118.Xr altq 9 ) .
119.It Dv DIOCSTOPALTQ
120Stop the ALTQ bandwidth control system.
121.It Dv DIOCBEGINADDRS Fa "struct pfioc_pooladdr *pp"
122.Bd -literal
123struct pfioc_pooladdr {
124	u_int32_t		action;
125	u_int32_t		ticket;
126	u_int32_t		nr;
127	u_int32_t		r_num;
128	u_int8_t		r_action;
129	u_int8_t		r_last;
130	u_int8_t		af;
131	char			anchor[MAXPATHLEN];
132	struct pf_pooladdr	addr;
133};
134.Ed
135.Pp
136Clear the buffer address pool and get a
137.Va ticket
138for subsequent
139.Dv DIOCADDADDR ,
140.Dv DIOCADDRULE ,
141and
142.Dv DIOCCHANGERULE
143calls.
144.It Dv DIOCADDADDR Fa "struct pfioc_pooladdr *pp"
145.Pp
146Add the pool address
147.Va addr
148to the buffer address pool to be used in the following
149.Dv DIOCADDRULE
150or
151.Dv DIOCCHANGERULE
152call.
153All other members of the structure are ignored.
154.It Dv DIOCADDRULE Fa "struct pfioc_rule *pr"
155.Bd -literal
156struct pfioc_rule {
157	u_int32_t	action;
158	u_int32_t	ticket;
159	u_int32_t	pool_ticket;
160	u_int32_t	nr;
161	char		anchor[MAXPATHLEN];
162	char		anchor_call[MAXPATHLEN];
163	struct pf_rule	rule;
164};
165.Ed
166.Pp
167Add
168.Va rule
169at the end of the inactive ruleset.
170This call requires a
171.Va ticket
172obtained through a preceding
173.Dv DIOCXBEGIN
174call and a
175.Va pool_ticket
176obtained through a
177.Dv DIOCBEGINADDRS
178call.
179.Dv DIOCADDADDR
180must also be called if any pool addresses are required.
181The optional
182.Va anchor
183name indicates the anchor in which to append the rule.
184.Va nr
185and
186.Va action
187are ignored.
188.It Dv DIOCADDALTQ Fa "struct pfioc_altq *pa"
189Add an ALTQ discipline or queue.
190.Bd -literal
191struct pfioc_altq {
192	u_int32_t	action;
193	u_int32_t	ticket;
194	u_int32_t	nr;
195	struct pf_altq  altq;
196};
197.Ed
198.It Dv DIOCGETRULES Fa "struct pfioc_rule *pr"
199Get a
200.Va ticket
201for subsequent
202.Dv DIOCGETRULE
203calls and the number
204.Va nr
205of rules in the active ruleset.
206.It Dv DIOCGETRULE Fa "struct pfioc_rule *pr"
207Get a
208.Va rule
209by its number
210.Va nr
211using the
212.Va ticket
213obtained through a preceding
214.Dv DIOCGETRULES
215call.
216If
217.Va action
218is set to
219.Dv PF_GET_CLR_CNTR ,
220the per-rule statistics on the requested rule are cleared.
221.It Dv DIOCGETADDRS Fa "struct pfioc_pooladdr *pp"
222Get a
223.Va ticket
224for subsequent
225.Dv DIOCGETADDR
226calls and the number
227.Va nr
228of pool addresses in the rule specified with
229.Va r_action ,
230.Va r_num ,
231and
232.Va anchor .
233.It Dv DIOCGETADDR Fa "struct pfioc_pooladdr *pp"
234Get the pool address
235.Va addr
236by its number
237.Va nr
238from the rule specified with
239.Va r_action ,
240.Va r_num ,
241and
242.Va anchor
243using the
244.Va ticket
245obtained through a preceding
246.Dv DIOCGETADDRS
247call.
248.It Dv DIOCGETALTQS Fa "struct pfioc_altq *pa"
249Get a
250.Va ticket
251for subsequent
252.Dv DIOCGETALTQ
253calls and the number
254.Va nr
255of queues in the active list.
256.It Dv DIOCGETALTQ Fa "struct pfioc_altq *pa"
257Get the queueing discipline
258.Va altq
259by its number
260.Va nr
261using the
262.Va ticket
263obtained through a preceding
264.Dv DIOCGETALTQS
265call.
266.It Dv DIOCGETQSTATS Fa "struct pfioc_qstats *pq"
267Get the statistics on a queue.
268.Bd -literal
269struct pfioc_qstats {
270	u_int32_t	 ticket;
271	u_int32_t	 nr;
272	void		*buf;
273	int		 nbytes;
274	u_int8_t	 scheduler;
275};
276.Ed
277.Pp
278This call fills in a pointer to the buffer of statistics
279.Va buf ,
280of length
281.Va nbytes ,
282for the queue specified by
283.Va nr .
284.It Dv DIOCGETRULESETS Fa "struct pfioc_ruleset *pr"
285.Bd -literal
286struct pfioc_ruleset {
287	u_int32_t	 nr;
288	char		 path[MAXPATHLEN];
289	char		 name[PF_ANCHOR_NAME_SIZE];
290};
291.Ed
292.Pp
293Get the number
294.Va nr
295of rulesets (i.e., anchors) directly attached to the anchor named by
296.Va path
297for use in subsequent
298.Dv DIOCGETRULESET
299calls.
300Nested anchors, since they are not directly attached to the given
301anchor, will not be included.
302This ioctl returns
303.Er ENOENT
304if the parent anchor given at
305.Va path
306does not exist.
307.It Dv DIOCGETRULESET Fa "struct pfioc_ruleset *pr"
308Get a ruleset (i.e., an anchor)
309.Va name
310by its number
311.Va nr
312from the given anchor
313.Va path ,
314the maximum number of which can be obtained from a preceding
315.Dv DIOCGETRULESETS
316call.
317This ioctl returns
318.Er ENOENT
319if the parent anchor given by
320.Va path
321does not exist or
322.Er EBUSY
323if the index passed in by
324.Va nr
325is greater than the number of anchors.
326.It Dv DIOCADDSTATE Fa "struct pfioc_state *ps"
327Add a state entry.
328.Bd -literal
329struct pfioc_state {
330	struct pfsync_state	state;
331};
332.Ed
333.It Dv DIOCGETSTATENV Fa "struct pfioc_nv *nv"
334Extract the entry identified by the
335.Va id
336and
337.Va creatorid
338fields of the
339.Va state
340nvlist from the state table.
341.It Dv DIOCKILLSTATESNV Fa "struct pfioc_nv nv"
342Remove matching entries from the state table.
343This ioctl returns the number of killed states in
344.Va "killed" .
345.Bd -literal
346nvlist pf_state_cmp {
347	number			id;
348	number			creatorid;
349	number			direction;
350};
351
352nvlist pf_kill {
353	nvlist pf_state_cmp	cmp;
354	number			af;
355	number			proto;
356	nvlist pf_rule_addr	src;
357	nvlist pf_rule_addr	dst;
358	string			ifname[IFNAMSIZ];
359	string			label[PF_RULE_LABEL_SIZE];
360};
361.Ed
362.It Dv DIOCCLRSTATESNV Fa "struct pfioc_nv nv"
363Clear all states.
364It works like
365.Dv DIOCKILLSTATESNV ,
366but ignores the
367.Va af ,
368.Va proto ,
369.Va src ,
370and
371.Va dst
372fields of the
373.Vt pf_kill
374nvlist.
375.It Dv DIOCSETSTATUSIF Fa "struct pfioc_if *pi"
376Specify the interface for which statistics are accumulated.
377.Bd -literal
378struct pfioc_if {
379	char		 ifname[IFNAMSIZ];
380};
381.Ed
382.It Dv DIOCGETSTATUS Fa "struct pf_status *s"
383Get the internal packet filter statistics.
384.Bd -literal
385struct pf_status {
386	u_int64_t	counters[PFRES_MAX];
387	u_int64_t	lcounters[LCNT_MAX];
388	u_int64_t	fcounters[FCNT_MAX];
389	u_int64_t	scounters[SCNT_MAX];
390	u_int64_t	pcounters[2][2][3];
391	u_int64_t	bcounters[2][2];
392	u_int32_t	running;
393	u_int32_t	states;
394	u_int32_t	src_nodes;
395	u_int32_t	since;
396	u_int32_t	debug;
397	u_int32_t	hostid;
398	char		ifname[IFNAMSIZ];
399	u_int8_t	pf_chksum[MD5_DIGEST_LENGTH];
400};
401.Ed
402.It Dv DIOCCLRSTATUS
403Clear the internal packet filter statistics.
404.It Dv DIOCNATLOOK Fa "struct pfioc_natlook *pnl"
405Look up a state table entry by source and destination addresses and ports.
406.Bd -literal
407struct pfioc_natlook {
408	struct pf_addr	 saddr;
409	struct pf_addr	 daddr;
410	struct pf_addr	 rsaddr;
411	struct pf_addr	 rdaddr;
412	u_int16_t	 sport;
413	u_int16_t	 dport;
414	u_int16_t	 rsport;
415	u_int16_t	 rdport;
416	sa_family_t	 af;
417	u_int8_t	 proto;
418	u_int8_t	 direction;
419};
420.Ed
421.It Dv DIOCSETDEBUG Fa "u_int32_t *level"
422Set the debug level.
423.Bd -literal
424enum	{ PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC,
425	  PF_DEBUG_NOISY };
426.Ed
427.It Dv DIOCGETSTATESNV Fa "struct pfioc_nv *nv"
428Get state table entries.
429.Bd -literal
430nvlist pf_state_key {
431	nvlist pf_addr	addr[2];
432	number		port[2];
433	number		af;
434	number		proto;
435};
436
437nvlist pf_state_scrub {
438	bool	timestamp;
439	number	ttl;
440	number	ts_mod;
441};
442
443nvlist pf_state_peer {
444	nvlist pf_state_scrub	scrub;
445	number			seqlo;
446	number			seqhi;
447	number			seqdiff;
448	number			max_win;
449	number			mss;
450	number			state;
451	number			wscale;
452};
453
454nvlist pf_state {
455	number			id;
456	string			ifname;
457	nvlist pf_state_key	stack_key;
458	nvlist pf_state_key	wire_key;
459	nvlist pf_state_peer	src;
460	nvlist pf_state_peer	dst;
461	nvlist pf_addr		rt_addr;
462	number			rule;
463	number			anchor;
464	number			nat_rule;
465	number			expire;
466	number			packets[2];
467	number			bytes[2];
468	number			creatorid;
469	number			direction;
470	number			log;
471	number			state_flags;
472	number			timeout;
473	number			sync_flags;
474};
475
476nvlist pf_states {
477	number		count;
478	nvlist pf_state	states[];
479};
480.Ed
481.Pp
482If
483.Va pfioc_nv.size
484is insufficiently large, as many states as possible that can fit into this
485size will be copied into the supplied buffer.
486.It Dv DIOCCHANGERULE Fa "struct pfioc_rule *pcr"
487Add or remove the
488.Va rule
489in the ruleset specified by
490.Va rule.action .
491.Pp
492The type of operation to be performed is indicated by
493.Va action ,
494which can be any of the following:
495.Bd -literal
496enum	{ PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
497	  PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
498	  PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
499.Ed
500.Pp
501.Va ticket
502must be set to the value obtained with
503.Dv PF_CHANGE_GET_TICKET
504for all actions except
505.Dv PF_CHANGE_GET_TICKET .
506.Va pool_ticket
507must be set to the value obtained with the
508.Dv DIOCBEGINADDRS
509call for all actions except
510.Dv PF_CHANGE_REMOVE
511and
512.Dv PF_CHANGE_GET_TICKET .
513.Va anchor
514indicates to which anchor the operation applies.
515.Va nr
516indicates the rule number against which
517.Dv PF_CHANGE_ADD_BEFORE ,
518.Dv PF_CHANGE_ADD_AFTER ,
519or
520.Dv PF_CHANGE_REMOVE
521actions are applied.
522.\" It Dv DIOCCHANGEALTQ Fa "struct pfioc_altq *pcr"
523.It Dv DIOCCHANGEADDR Fa "struct pfioc_pooladdr *pca"
524Add or remove the pool address
525.Va addr
526from the rule specified by
527.Va r_action ,
528.Va r_num ,
529and
530.Va anchor .
531.It Dv DIOCSETTIMEOUT Fa "struct pfioc_tm *pt"
532.Bd -literal
533struct pfioc_tm {
534	int		 timeout;
535	int		 seconds;
536};
537.Ed
538.Pp
539Set the state timeout of
540.Va timeout
541to
542.Va seconds .
543The old value will be placed into
544.Va seconds .
545For possible values of
546.Va timeout ,
547consult the
548.Dv PFTM_*
549values in
550.Aq Pa net/pfvar.h .
551.It Dv DIOCGETTIMEOUT Fa "struct pfioc_tm *pt"
552Get the state timeout of
553.Va timeout .
554The value will be placed into the
555.Va seconds
556field.
557.It Dv DIOCCLRRULECTRS
558Clear per-rule statistics.
559.It Dv DIOCSETLIMIT Fa "struct pfioc_limit *pl"
560Set the hard limits on the memory pools used by the packet filter.
561.Bd -literal
562struct pfioc_limit {
563	int		index;
564	unsigned	limit;
565};
566
567enum	{ PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
568	  PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
569.Ed
570.It Dv DIOCGETLIMIT Fa "struct pfioc_limit *pl"
571Get the hard
572.Va limit
573for the memory pool indicated by
574.Va index .
575.It Dv DIOCRCLRTABLES Fa "struct pfioc_table *io"
576Clear all tables.
577All the ioctls that manipulate radix tables
578use the same structure described below.
579For
580.Dv DIOCRCLRTABLES ,
581.Va pfrio_ndel
582contains on exit the number of tables deleted.
583.Bd -literal
584struct pfioc_table {
585	struct pfr_table	 pfrio_table;
586	void			*pfrio_buffer;
587	int			 pfrio_esize;
588	int			 pfrio_size;
589	int			 pfrio_size2;
590	int			 pfrio_nadd;
591	int			 pfrio_ndel;
592	int			 pfrio_nchange;
593	int			 pfrio_flags;
594	u_int32_t		 pfrio_ticket;
595};
596#define pfrio_exists    pfrio_nadd
597#define pfrio_nzero     pfrio_nadd
598#define pfrio_nmatch    pfrio_nadd
599#define pfrio_naddr     pfrio_size2
600#define pfrio_setflag   pfrio_size2
601#define pfrio_clrflag   pfrio_nadd
602.Ed
603.It Dv DIOCRADDTABLES Fa "struct pfioc_table *io"
604Create one or more tables.
605On entry,
606.Va pfrio_buffer
607must point to an array of
608.Vt struct pfr_table
609containing at least
610.Vt pfrio_size
611elements.
612.Vt pfrio_esize
613must be the size of
614.Vt struct pfr_table .
615On exit,
616.Va pfrio_nadd
617contains the number of tables effectively created.
618.Bd -literal
619struct pfr_table {
620	char		pfrt_anchor[MAXPATHLEN];
621	char		pfrt_name[PF_TABLE_NAME_SIZE];
622	u_int32_t	pfrt_flags;
623	u_int8_t	pfrt_fback;
624};
625.Ed
626.It Dv DIOCRDELTABLES Fa "struct pfioc_table *io"
627Delete one or more tables.
628On entry,
629.Va pfrio_buffer
630must point to an array of
631.Vt struct pfr_table
632containing at least
633.Vt pfrio_size
634elements.
635.Vt pfrio_esize
636must be the size of
637.Vt struct pfr_table .
638On exit,
639.Va pfrio_ndel
640contains the number of tables effectively deleted.
641.It Dv DIOCRGETTABLES Fa "struct pfioc_table *io"
642Get the list of all tables.
643On entry,
644.Va pfrio_buffer[pfrio_size]
645contains a valid writeable buffer for
646.Vt pfr_table
647structures.
648On exit,
649.Va pfrio_size
650contains the number of tables written into the buffer.
651If the buffer is too small, the kernel does not store anything but just
652returns the required buffer size, without error.
653.It Dv DIOCRGETTSTATS Fa "struct pfioc_table *io"
654This call is like
655.Dv DIOCRGETTABLES
656but is used to get an array of
657.Vt pfr_tstats
658structures.
659.Bd -literal
660struct pfr_tstats {
661	struct pfr_table pfrts_t;
662	u_int64_t	 pfrts_packets
663			     [PFR_DIR_MAX][PFR_OP_TABLE_MAX];
664	u_int64_t	 pfrts_bytes
665			     [PFR_DIR_MAX][PFR_OP_TABLE_MAX];
666	u_int64_t	 pfrts_match;
667	u_int64_t	 pfrts_nomatch;
668	long		 pfrts_tzero;
669	int		 pfrts_cnt;
670	int		 pfrts_refcnt[PFR_REFCNT_MAX];
671};
672#define pfrts_name	 pfrts_t.pfrt_name
673#define pfrts_flags	 pfrts_t.pfrt_flags
674.Ed
675.It Dv DIOCRCLRTSTATS Fa "struct pfioc_table *io"
676Clear the statistics of one or more tables.
677On entry,
678.Va pfrio_buffer
679must point to an array of
680.Vt struct pfr_table
681containing at least
682.Vt pfrio_size
683elements.
684.Vt pfrio_esize
685must be the size of
686.Vt struct pfr_table .
687On exit,
688.Va pfrio_nzero
689contains the number of tables effectively cleared.
690.It Dv DIOCRCLRADDRS Fa "struct pfioc_table *io"
691Clear all addresses in a table.
692On entry,
693.Va pfrio_table
694contains the table to clear.
695On exit,
696.Va pfrio_ndel
697contains the number of addresses removed.
698.It Dv DIOCRADDADDRS Fa "struct pfioc_table *io"
699Add one or more addresses to a table.
700On entry,
701.Va pfrio_table
702contains the table ID and
703.Va pfrio_buffer
704must point to an array of
705.Vt struct pfr_addr
706containing at least
707.Vt pfrio_size
708elements to add to the table.
709.Vt pfrio_esize
710must be the size of
711.Vt struct pfr_addr .
712On exit,
713.Va pfrio_nadd
714contains the number of addresses effectively added.
715.Bd -literal
716struct pfr_addr {
717	union {
718		struct in_addr	 _pfra_ip4addr;
719		struct in6_addr	 _pfra_ip6addr;
720	}		 pfra_u;
721	u_int8_t	 pfra_af;
722	u_int8_t	 pfra_net;
723	u_int8_t	 pfra_not;
724	u_int8_t	 pfra_fback;
725};
726#define pfra_ip4addr    pfra_u._pfra_ip4addr
727#define pfra_ip6addr    pfra_u._pfra_ip6addr
728.Ed
729.It Dv DIOCRDELADDRS Fa "struct pfioc_table *io"
730Delete one or more addresses from a table.
731On entry,
732.Va pfrio_table
733contains the table ID and
734.Va pfrio_buffer
735must point to an array of
736.Vt struct pfr_addr
737containing at least
738.Vt pfrio_size
739elements to delete from the table.
740.Vt pfrio_esize
741must be the size of
742.Vt struct pfr_addr .
743On exit,
744.Va pfrio_ndel
745contains the number of addresses effectively deleted.
746.It Dv DIOCRSETADDRS Fa "struct pfioc_table *io"
747Replace the content of a table by a new address list.
748This is the most complicated command, which uses all the structure members.
749.Pp
750On entry,
751.Va pfrio_table
752contains the table ID and
753.Va pfrio_buffer
754must point to an array of
755.Vt struct pfr_addr
756containing at least
757.Vt pfrio_size
758elements which become the new contents of the table.
759.Vt pfrio_esize
760must be the size of
761.Vt struct pfr_addr .
762Additionally, if
763.Va pfrio_size2
764is non-zero,
765.Va pfrio_buffer[pfrio_size..pfrio_size2]
766must be a writeable buffer, into which the kernel can copy the
767addresses that have been deleted during the replace operation.
768On exit,
769.Va pfrio_ndel ,
770.Va pfrio_nadd ,
771and
772.Va pfrio_nchange
773contain the number of addresses deleted, added, and changed by the
774kernel.
775If
776.Va pfrio_size2
777was set on entry,
778.Va pfrio_size2
779will point to the size of the buffer used, exactly like
780.Dv DIOCRGETADDRS .
781.It Dv DIOCRGETADDRS Fa "struct pfioc_table *io"
782Get all the addresses of a table.
783On entry,
784.Va pfrio_table
785contains the table ID and
786.Va pfrio_buffer[pfrio_size]
787contains a valid writeable buffer for
788.Vt pfr_addr
789structures.
790On exit,
791.Va pfrio_size
792contains the number of addresses written into the buffer.
793If the buffer was too small, the kernel does not store anything but just
794returns the required buffer size, without returning an error.
795.It Dv DIOCRGETASTATS Fa "struct pfioc_table *io"
796This call is like
797.Dv DIOCRGETADDRS
798but is used to get an array of
799.Vt pfr_astats
800structures.
801.Bd -literal
802struct pfr_astats {
803	struct pfr_addr	 pfras_a;
804	u_int64_t	 pfras_packets
805			     [PFR_DIR_MAX][PFR_OP_ADDR_MAX];
806	u_int64_t	 pfras_bytes
807			     [PFR_DIR_MAX][PFR_OP_ADDR_MAX];
808	long		 pfras_tzero;
809};
810.Ed
811.It Dv DIOCRCLRASTATS Fa "struct pfioc_table *io"
812Clear the statistics of one or more addresses.
813On entry,
814.Va pfrio_table
815contains the table ID and
816.Va pfrio_buffer
817must point to an array of
818.Vt struct pfr_addr
819containing at least
820.Vt pfrio_size
821elements to be cleared from the table.
822.Vt pfrio_esize
823must be the size of
824.Vt struct pfr_addr .
825On exit,
826.Va pfrio_nzero
827contains the number of addresses effectively cleared.
828.It Dv DIOCRTSTADDRS Fa "struct pfioc_table *io"
829Test if the given addresses match a table.
830On entry,
831.Va pfrio_table
832contains the table ID and
833.Va pfrio_buffer
834must point to an array of
835.Vt struct pfr_addr
836containing at least
837.Vt pfrio_size
838elements, each of which will be tested for a match in the table.
839.Vt pfrio_esize
840must be the size of
841.Vt struct pfr_addr .
842On exit, the kernel updates the
843.Vt pfr_addr
844array by setting the
845.Va pfra_fback
846member appropriately.
847.It Dv DIOCRSETTFLAGS Fa "struct pfioc_table *io"
848Change the
849.Dv PFR_TFLAG_CONST
850or
851.Dv PFR_TFLAG_PERSIST
852flags of a table.
853On entry,
854.Va pfrio_buffer
855must point to an array of
856.Vt struct pfr_table
857containing at least
858.Vt pfrio_size
859elements.
860.Va pfrio_esize
861must be the size of
862.Vt struct pfr_table .
863.Va pfrio_setflag
864must contain the flags to add, while
865.Va pfrio_clrflag
866must contain the flags to remove.
867On exit,
868.Va pfrio_nchange
869and
870.Va pfrio_ndel
871contain the number of tables altered or deleted by the kernel.
872Yes, tables can be deleted if one removes the
873.Dv PFR_TFLAG_PERSIST
874flag of an unreferenced table.
875.It Dv DIOCRINADEFINE Fa "struct pfioc_table *io"
876Defines a table in the inactive set.
877On entry,
878.Va pfrio_table
879contains the table ID and
880.Va pfrio_buffer[pfrio_size]
881contains an array of
882.Vt pfr_addr
883structures to put in the table.
884A valid ticket must also be supplied to
885.Va pfrio_ticket .
886On exit,
887.Va pfrio_nadd
888contains 0 if the table was already defined in the inactive list
889or 1 if a new table has been created.
890.Va pfrio_naddr
891contains the number of addresses effectively put in the table.
892.It Dv DIOCXBEGIN Fa "struct pfioc_trans *io"
893.Bd -literal
894struct pfioc_trans {
895	int		 size;	/* number of elements */
896	int		 esize;	/* size of each element in bytes */
897	struct pfioc_trans_e {
898		int		rs_num;
899		char		anchor[MAXPATHLEN];
900		u_int32_t	ticket;
901	}		*array;
902};
903.Ed
904.Pp
905Clear all the inactive rulesets specified in the
906.Vt pfioc_trans_e
907array.
908For each ruleset, a ticket is returned for subsequent "add rule" ioctls,
909as well as for the
910.Dv DIOCXCOMMIT
911and
912.Dv DIOCXROLLBACK
913calls.
914.Pp
915Ruleset types, identified by
916.Va rs_num ,
917include the following:
918.Pp
919.Bl -tag -width PF_RULESET_FILTER -offset ind -compact
920.It Dv PF_RULESET_SCRUB
921Scrub (packet normalization) rules.
922.It Dv PF_RULESET_FILTER
923Filter rules.
924.It Dv PF_RULESET_NAT
925NAT (Network Address Translation) rules.
926.It Dv PF_RULESET_BINAT
927Bidirectional NAT rules.
928.It Dv PF_RULESET_RDR
929Redirect rules.
930.It Dv PF_RULESET_ALTQ
931ALTQ disciplines.
932.It Dv PF_RULESET_TABLE
933Address tables.
934.El
935.It Dv DIOCXCOMMIT Fa "struct pfioc_trans *io"
936Atomically switch a vector of inactive rulesets to the active rulesets.
937This call is implemented as a standard two-phase commit, which will either
938fail for all rulesets or completely succeed.
939All tickets need to be valid.
940This ioctl returns
941.Er EBUSY
942if another process is concurrently updating some of the same rulesets.
943.It Dv DIOCXROLLBACK Fa "struct pfioc_trans *io"
944Clean up the kernel by undoing all changes that have taken place on the
945inactive rulesets since the last
946.Dv DIOCXBEGIN .
947.Dv DIOCXROLLBACK
948will silently ignore rulesets for which the ticket is invalid.
949.It Dv DIOCSETHOSTID Fa "u_int32_t *hostid"
950Set the host ID, which is used by
951.Xr pfsync 4
952to identify which host created state table entries.
953.It Dv DIOCOSFPFLUSH
954Flush the passive OS fingerprint table.
955.It Dv DIOCOSFPADD Fa "struct pf_osfp_ioctl *io"
956.Bd -literal
957struct pf_osfp_ioctl {
958	struct pf_osfp_entry {
959		SLIST_ENTRY(pf_osfp_entry) fp_entry;
960		pf_osfp_t		fp_os;
961		char			fp_class_nm[PF_OSFP_LEN];
962		char			fp_version_nm[PF_OSFP_LEN];
963		char			fp_subtype_nm[PF_OSFP_LEN];
964	} 			fp_os;
965	pf_tcpopts_t		fp_tcpopts;
966	u_int16_t		fp_wsize;
967	u_int16_t		fp_psize;
968	u_int16_t		fp_mss;
969	u_int16_t		fp_flags;
970	u_int8_t		fp_optcnt;
971	u_int8_t		fp_wscale;
972	u_int8_t		fp_ttl;
973	int			fp_getnum;
974};
975.Ed
976.Pp
977Add a passive OS fingerprint to the table.
978Set
979.Va fp_os.fp_os
980to the packed fingerprint,
981.Va fp_os.fp_class_nm
982to the name of the class (Linux, Windows, etc),
983.Va fp_os.fp_version_nm
984to the name of the version (NT, 95, 98), and
985.Va fp_os.fp_subtype_nm
986to the name of the subtype or patchlevel.
987The members
988.Va fp_mss ,
989.Va fp_wsize ,
990.Va fp_psize ,
991.Va fp_ttl ,
992.Va fp_optcnt ,
993and
994.Va fp_wscale
995are set to the TCP MSS, the TCP window size, the IP length, the IP TTL,
996the number of TCP options, and the TCP window scaling constant of the
997TCP SYN packet, respectively.
998.Pp
999The
1000.Va fp_flags
1001member is filled according to the
1002.Aq Pa net/pfvar.h
1003include file
1004.Dv PF_OSFP_*
1005defines.
1006The
1007.Va fp_tcpopts
1008member contains packed TCP options.
1009Each option uses
1010.Dv PF_OSFP_TCPOPT_BITS
1011bits in the packed value.
1012Options include any of
1013.Dv PF_OSFP_TCPOPT_NOP ,
1014.Dv PF_OSFP_TCPOPT_SACK ,
1015.Dv PF_OSFP_TCPOPT_WSCALE ,
1016.Dv PF_OSFP_TCPOPT_MSS ,
1017or
1018.Dv PF_OSFP_TCPOPT_TS .
1019.Pp
1020The
1021.Va fp_getnum
1022member is not used with this ioctl.
1023.Pp
1024The structure's slack space must be zeroed for correct operation;
1025.Xr memset 3
1026the whole structure to zero before filling and sending to the kernel.
1027.It Dv DIOCOSFPGET Fa "struct pf_osfp_ioctl *io"
1028Get the passive OS fingerprint number
1029.Va fp_getnum
1030from the kernel's fingerprint list.
1031The rest of the structure members will come back filled.
1032Get the whole list by repeatedly incrementing the
1033.Va fp_getnum
1034number until the ioctl returns
1035.Er EBUSY .
1036.It Dv DIOCGETSRCNODES Fa "struct pfioc_src_nodes *psn"
1037.Bd -literal
1038struct pfioc_src_nodes {
1039	int	psn_len;
1040	union {
1041		caddr_t		psu_buf;
1042		struct pf_src_node	*psu_src_nodes;
1043	} psn_u;
1044#define psn_buf		psn_u.psu_buf
1045#define psn_src_nodes	psn_u.psu_src_nodes
1046};
1047.Ed
1048.Pp
1049Get the list of source nodes kept by sticky addresses and source
1050tracking.
1051The ioctl must be called once with
1052.Va psn_len
1053set to 0.
1054If the ioctl returns without error,
1055.Va psn_len
1056will be set to the size of the buffer required to hold all the
1057.Va pf_src_node
1058structures held in the table.
1059A buffer of this size should then be allocated, and a pointer to this buffer
1060placed in
1061.Va psn_buf .
1062The ioctl must then be called again to fill this buffer with the actual
1063source node data.
1064After that call,
1065.Va psn_len
1066will be set to the length of the buffer actually used.
1067.It Dv DIOCCLRSRCNODES
1068Clear the tree of source tracking nodes.
1069.It Dv DIOCIGETIFACES Fa "struct pfioc_iface *io"
1070Get the list of interfaces and interface drivers known to
1071.Nm .
1072All the ioctls that manipulate interfaces
1073use the same structure described below:
1074.Bd -literal
1075struct pfioc_iface {
1076	char			 pfiio_name[IFNAMSIZ];
1077	void			*pfiio_buffer;
1078	int			 pfiio_esize;
1079	int			 pfiio_size;
1080	int			 pfiio_nzero;
1081	int			 pfiio_flags;
1082};
1083.Ed
1084.Pp
1085If not empty,
1086.Va pfiio_name
1087can be used to restrict the search to a specific interface or driver.
1088.Va pfiio_buffer[pfiio_size]
1089is the user-supplied buffer for returning the data.
1090On entry,
1091.Va pfiio_size
1092contains the number of
1093.Vt pfi_kif
1094entries that can fit into the buffer.
1095The kernel will replace this value by the real number of entries it wants
1096to return.
1097.Va pfiio_esize
1098should be set to
1099.Li sizeof(struct pfi_kif) .
1100.Pp
1101The data is returned in the
1102.Vt pfi_kif
1103structure described below:
1104.Bd -literal
1105struct pfi_kif {
1106	char				 pfik_name[IFNAMSIZ];
1107	union {
1108		RB_ENTRY(pfi_kif)	 pfik_tree;
1109		LIST_ENTRY(pfi_kif)	 pfik_list;
1110	};
1111	u_int64_t			 pfik_packets[2][2][2];
1112	u_int64_t			 pfik_bytes[2][2][2];
1113	u_int32_t			 pfik_tzero;
1114	u_int				 pfik_flags;
1115	struct ifnet			*pfik_ifp;
1116	struct ifg_group		*pfik_group;
1117	u_int				 pfik_rulerefs;
1118	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1119};
1120.Ed
1121.It Dv DIOCSETIFFLAG Fa "struct pfioc_iface *io"
1122Set the user settable flags (described above) of the
1123.Nm
1124internal interface description.
1125The filtering process is the same as for
1126.Dv DIOCIGETIFACES .
1127.Bd -literal
1128#define PFI_IFLAG_SKIP	0x0100	/* skip filtering on interface */
1129.Ed
1130.It Dv DIOCCLRIFFLAG Fa "struct pfioc_iface *io"
1131Works as
1132.Dv DIOCSETIFFLAG
1133above but clears the flags.
1134.It Dv DIOCKILLSRCNODES Fa "struct pfioc_iface *io"
1135Explicitly remove source tracking nodes.
1136.El
1137.Sh FILES
1138.Bl -tag -width /dev/pf -compact
1139.It Pa /dev/pf
1140packet filtering device.
1141.El
1142.Sh EXAMPLES
1143The following example demonstrates how to use the
1144.Dv DIOCNATLOOK
1145command to find the internal host/port of a NATed connection:
1146.Bd -literal
1147#include <sys/types.h>
1148#include <sys/socket.h>
1149#include <sys/ioctl.h>
1150#include <sys/fcntl.h>
1151#include <net/if.h>
1152#include <netinet/in.h>
1153#include <net/pfvar.h>
1154#include <err.h>
1155#include <stdio.h>
1156#include <stdlib.h>
1157
1158u_int32_t
1159read_address(const char *s)
1160{
1161	int a, b, c, d;
1162
1163	sscanf(s, "%i.%i.%i.%i", &a, &b, &c, &d);
1164	return htonl(a << 24 | b << 16 | c << 8 | d);
1165}
1166
1167void
1168print_address(u_int32_t a)
1169{
1170	a = ntohl(a);
1171	printf("%d.%d.%d.%d", a >> 24 & 255, a >> 16 & 255,
1172	    a >> 8 & 255, a & 255);
1173}
1174
1175int
1176main(int argc, char *argv[])
1177{
1178	struct pfioc_natlook nl;
1179	int dev;
1180
1181	if (argc != 5) {
1182		printf("%s <gwy addr> <gwy port> <ext addr> <ext port>\\n",
1183		    argv[0]);
1184		return 1;
1185	}
1186
1187	dev = open("/dev/pf", O_RDWR);
1188	if (dev == -1)
1189		err(1, "open(\\"/dev/pf\\") failed");
1190
1191	memset(&nl, 0, sizeof(struct pfioc_natlook));
1192	nl.saddr.v4.s_addr	= read_address(argv[1]);
1193	nl.sport		= htons(atoi(argv[2]));
1194	nl.daddr.v4.s_addr	= read_address(argv[3]);
1195	nl.dport		= htons(atoi(argv[4]));
1196	nl.af			= AF_INET;
1197	nl.proto		= IPPROTO_TCP;
1198	nl.direction		= PF_IN;
1199
1200	if (ioctl(dev, DIOCNATLOOK, &nl))
1201		err(1, "DIOCNATLOOK");
1202
1203	printf("internal host ");
1204	print_address(nl.rsaddr.v4.s_addr);
1205	printf(":%u\\n", ntohs(nl.rsport));
1206	return 0;
1207}
1208.Ed
1209.Sh SEE ALSO
1210.Xr ioctl 2 ,
1211.Xr altq 4 ,
1212.Xr if_bridge 4 ,
1213.Xr pflog 4 ,
1214.Xr pfsync 4 ,
1215.Xr pfctl 8 ,
1216.Xr altq 9
1217.Sh HISTORY
1218The
1219.Nm
1220packet filtering mechanism first appeared in
1221.Ox 3.0
1222and then
1223.Fx 5.2 .
1224.Pp
1225This implementation is derived from
1226.Ox 4.5 .
1227It has been heavily modified to be capable of running in multithreaded
1228.Fx
1229kernel and scale its performance on multiple CPUs.
1230