xref: /freebsd/sys/net/pfvar.h (revision 3b2324c3a800d7599f348c408f01908d0cef05a0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel Hartmeier
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *	$OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $
32  *	$FreeBSD$
33  */
34 
35 #ifndef _NET_PFVAR_H_
36 #define _NET_PFVAR_H_
37 
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/counter.h>
41 #include <sys/cpuset.h>
42 #include <sys/malloc.h>
43 #include <sys/refcount.h>
44 #include <sys/sysctl.h>
45 #include <sys/lock.h>
46 #include <sys/rmlock.h>
47 #include <sys/tree.h>
48 #include <vm/uma.h>
49 
50 #include <net/radix.h>
51 #include <netinet/in.h>
52 
53 #include <netpfil/pf/pf.h>
54 #include <netpfil/pf/pf_altq.h>
55 #include <netpfil/pf/pf_mtag.h>
56 
57 struct pf_addr {
58 	union {
59 		struct in_addr		v4;
60 		struct in6_addr		v6;
61 		u_int8_t		addr8[16];
62 		u_int16_t		addr16[8];
63 		u_int32_t		addr32[4];
64 	} pfa;		    /* 128-bit address */
65 #define v4	pfa.v4
66 #define v6	pfa.v6
67 #define addr8	pfa.addr8
68 #define addr16	pfa.addr16
69 #define addr32	pfa.addr32
70 };
71 
72 #define PFI_AFLAG_NETWORK	0x01
73 #define PFI_AFLAG_BROADCAST	0x02
74 #define PFI_AFLAG_PEER		0x04
75 #define PFI_AFLAG_MODEMASK	0x07
76 #define PFI_AFLAG_NOALIAS	0x08
77 
78 struct pf_addr_wrap {
79 	union {
80 		struct {
81 			struct pf_addr		 addr;
82 			struct pf_addr		 mask;
83 		}			 a;
84 		char			 ifname[IFNAMSIZ];
85 		char			 tblname[PF_TABLE_NAME_SIZE];
86 	}			 v;
87 	union {
88 		struct pfi_dynaddr	*dyn;
89 		struct pfr_ktable	*tbl;
90 		int			 dyncnt;
91 		int			 tblcnt;
92 	}			 p;
93 	u_int8_t		 type;		/* PF_ADDR_* */
94 	u_int8_t		 iflags;	/* PFI_AFLAG_* */
95 };
96 
97 #ifdef _KERNEL
98 
99 SYSCTL_DECL(_net_pf);
100 MALLOC_DECLARE(M_PFHASH);
101 
102 struct pfi_dynaddr {
103 	TAILQ_ENTRY(pfi_dynaddr)	 entry;
104 	struct pf_addr			 pfid_addr4;
105 	struct pf_addr			 pfid_mask4;
106 	struct pf_addr			 pfid_addr6;
107 	struct pf_addr			 pfid_mask6;
108 	struct pfr_ktable		*pfid_kt;
109 	struct pfi_kif			*pfid_kif;
110 	int				 pfid_net;	/* mask or 128 */
111 	int				 pfid_acnt4;	/* address count IPv4 */
112 	int				 pfid_acnt6;	/* address count IPv6 */
113 	sa_family_t			 pfid_af;	/* rule af */
114 	u_int8_t			 pfid_iflags;	/* PFI_AFLAG_* */
115 };
116 
117 /*
118  * Address manipulation macros
119  */
120 #define	HTONL(x)	(x) = htonl((__uint32_t)(x))
121 #define	HTONS(x)	(x) = htons((__uint16_t)(x))
122 #define	NTOHL(x)	(x) = ntohl((__uint32_t)(x))
123 #define	NTOHS(x)	(x) = ntohs((__uint16_t)(x))
124 
125 #define	PF_NAME		"pf"
126 
127 #define	PF_HASHROW_ASSERT(h)	mtx_assert(&(h)->lock, MA_OWNED)
128 #define	PF_HASHROW_LOCK(h)	mtx_lock(&(h)->lock)
129 #define	PF_HASHROW_UNLOCK(h)	mtx_unlock(&(h)->lock)
130 
131 #define	PF_STATE_LOCK(s)						\
132 	do {								\
133 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)];	\
134 		PF_HASHROW_LOCK(_ih);					\
135 	} while (0)
136 
137 #define	PF_STATE_UNLOCK(s)						\
138 	do {								\
139 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH((s))];	\
140 		PF_HASHROW_UNLOCK(_ih);					\
141 	} while (0)
142 
143 #ifdef INVARIANTS
144 #define	PF_STATE_LOCK_ASSERT(s)						\
145 	do {								\
146 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)];	\
147 		PF_HASHROW_ASSERT(_ih);					\
148 	} while (0)
149 #else /* !INVARIANTS */
150 #define	PF_STATE_LOCK_ASSERT(s)		do {} while (0)
151 #endif /* INVARIANTS */
152 
153 extern struct mtx pf_unlnkdrules_mtx;
154 #define	PF_UNLNKDRULES_LOCK()	mtx_lock(&pf_unlnkdrules_mtx)
155 #define	PF_UNLNKDRULES_UNLOCK()	mtx_unlock(&pf_unlnkdrules_mtx)
156 
157 extern struct rmlock pf_rules_lock;
158 #define	PF_RULES_RLOCK_TRACKER	struct rm_priotracker _pf_rules_tracker
159 #define	PF_RULES_RLOCK()	rm_rlock(&pf_rules_lock, &_pf_rules_tracker)
160 #define	PF_RULES_RUNLOCK()	rm_runlock(&pf_rules_lock, &_pf_rules_tracker)
161 #define	PF_RULES_WLOCK()	rm_wlock(&pf_rules_lock)
162 #define	PF_RULES_WUNLOCK()	rm_wunlock(&pf_rules_lock)
163 #define	PF_RULES_ASSERT()	rm_assert(&pf_rules_lock, RA_LOCKED)
164 #define	PF_RULES_RASSERT()	rm_assert(&pf_rules_lock, RA_RLOCKED)
165 #define	PF_RULES_WASSERT()	rm_assert(&pf_rules_lock, RA_WLOCKED)
166 
167 extern struct sx pf_end_lock;
168 
169 #define	PF_MODVER	1
170 #define	PFLOG_MODVER	1
171 #define	PFSYNC_MODVER	1
172 
173 #define	PFLOG_MINVER	1
174 #define	PFLOG_PREFVER	PFLOG_MODVER
175 #define	PFLOG_MAXVER	1
176 #define	PFSYNC_MINVER	1
177 #define	PFSYNC_PREFVER	PFSYNC_MODVER
178 #define	PFSYNC_MAXVER	1
179 
180 #ifdef INET
181 #ifndef INET6
182 #define	PF_INET_ONLY
183 #endif /* ! INET6 */
184 #endif /* INET */
185 
186 #ifdef INET6
187 #ifndef INET
188 #define	PF_INET6_ONLY
189 #endif /* ! INET */
190 #endif /* INET6 */
191 
192 #ifdef INET
193 #ifdef INET6
194 #define	PF_INET_INET6
195 #endif /* INET6 */
196 #endif /* INET */
197 
198 #else
199 
200 #define	PF_INET_INET6
201 
202 #endif /* _KERNEL */
203 
204 /* Both IPv4 and IPv6 */
205 #ifdef PF_INET_INET6
206 
207 #define PF_AEQ(a, b, c) \
208 	((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
209 	(c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \
210 	(a)->addr32[2] == (b)->addr32[2] && \
211 	(a)->addr32[1] == (b)->addr32[1] && \
212 	(a)->addr32[0] == (b)->addr32[0])) \
213 
214 #define PF_ANEQ(a, b, c) \
215 	((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
216 	(c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \
217 	(a)->addr32[1] != (b)->addr32[1] || \
218 	(a)->addr32[2] != (b)->addr32[2] || \
219 	(a)->addr32[3] != (b)->addr32[3]))) \
220 
221 #define PF_AZERO(a, c) \
222 	((c == AF_INET && !(a)->addr32[0]) || \
223 	(c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \
224 	!(a)->addr32[2] && !(a)->addr32[3] )) \
225 
226 #define PF_MATCHA(n, a, m, b, f) \
227 	pf_match_addr(n, a, m, b, f)
228 
229 #define PF_ACPY(a, b, f) \
230 	pf_addrcpy(a, b, f)
231 
232 #define PF_AINC(a, f) \
233 	pf_addr_inc(a, f)
234 
235 #define PF_POOLMASK(a, b, c, d, f) \
236 	pf_poolmask(a, b, c, d, f)
237 
238 #else
239 
240 /* Just IPv6 */
241 
242 #ifdef PF_INET6_ONLY
243 
244 #define PF_AEQ(a, b, c) \
245 	((a)->addr32[3] == (b)->addr32[3] && \
246 	(a)->addr32[2] == (b)->addr32[2] && \
247 	(a)->addr32[1] == (b)->addr32[1] && \
248 	(a)->addr32[0] == (b)->addr32[0]) \
249 
250 #define PF_ANEQ(a, b, c) \
251 	((a)->addr32[3] != (b)->addr32[3] || \
252 	(a)->addr32[2] != (b)->addr32[2] || \
253 	(a)->addr32[1] != (b)->addr32[1] || \
254 	(a)->addr32[0] != (b)->addr32[0]) \
255 
256 #define PF_AZERO(a, c) \
257 	(!(a)->addr32[0] && \
258 	!(a)->addr32[1] && \
259 	!(a)->addr32[2] && \
260 	!(a)->addr32[3] ) \
261 
262 #define PF_MATCHA(n, a, m, b, f) \
263 	pf_match_addr(n, a, m, b, f)
264 
265 #define PF_ACPY(a, b, f) \
266 	pf_addrcpy(a, b, f)
267 
268 #define PF_AINC(a, f) \
269 	pf_addr_inc(a, f)
270 
271 #define PF_POOLMASK(a, b, c, d, f) \
272 	pf_poolmask(a, b, c, d, f)
273 
274 #else
275 
276 /* Just IPv4 */
277 #ifdef PF_INET_ONLY
278 
279 #define PF_AEQ(a, b, c) \
280 	((a)->addr32[0] == (b)->addr32[0])
281 
282 #define PF_ANEQ(a, b, c) \
283 	((a)->addr32[0] != (b)->addr32[0])
284 
285 #define PF_AZERO(a, c) \
286 	(!(a)->addr32[0])
287 
288 #define PF_MATCHA(n, a, m, b, f) \
289 	pf_match_addr(n, a, m, b, f)
290 
291 #define PF_ACPY(a, b, f) \
292 	(a)->v4.s_addr = (b)->v4.s_addr
293 
294 #define PF_AINC(a, f) \
295 	do { \
296 		(a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
297 	} while (0)
298 
299 #define PF_POOLMASK(a, b, c, d, f) \
300 	do { \
301 		(a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
302 		(((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
303 	} while (0)
304 
305 #endif /* PF_INET_ONLY */
306 #endif /* PF_INET6_ONLY */
307 #endif /* PF_INET_INET6 */
308 
309 /*
310  * XXX callers not FIB-aware in our version of pf yet.
311  * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio.
312  */
313 #define	PF_MISMATCHAW(aw, x, af, neg, ifp, rtid)			\
314 	(								\
315 		(((aw)->type == PF_ADDR_NOROUTE &&			\
316 		    pf_routable((x), (af), NULL, (rtid))) ||		\
317 		(((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&	\
318 		    pf_routable((x), (af), (ifp), (rtid))) ||		\
319 		((aw)->type == PF_ADDR_TABLE &&				\
320 		    !pfr_match_addr((aw)->p.tbl, (x), (af))) ||		\
321 		((aw)->type == PF_ADDR_DYNIFTL &&			\
322 		    !pfi_match_addr((aw)->p.dyn, (x), (af))) ||		\
323 		((aw)->type == PF_ADDR_RANGE &&				\
324 		    !pf_match_addr_range(&(aw)->v.a.addr,		\
325 		    &(aw)->v.a.mask, (x), (af))) ||			\
326 		((aw)->type == PF_ADDR_ADDRMASK &&			\
327 		    !PF_AZERO(&(aw)->v.a.mask, (af)) &&			\
328 		    !PF_MATCHA(0, &(aw)->v.a.addr,			\
329 		    &(aw)->v.a.mask, (x), (af))))) !=			\
330 		(neg)							\
331 	)
332 
333 
334 struct pf_rule_uid {
335 	uid_t		 uid[2];
336 	u_int8_t	 op;
337 };
338 
339 struct pf_rule_gid {
340 	uid_t		 gid[2];
341 	u_int8_t	 op;
342 };
343 
344 struct pf_rule_addr {
345 	struct pf_addr_wrap	 addr;
346 	u_int16_t		 port[2];
347 	u_int8_t		 neg;
348 	u_int8_t		 port_op;
349 };
350 
351 struct pf_pooladdr {
352 	struct pf_addr_wrap		 addr;
353 	TAILQ_ENTRY(pf_pooladdr)	 entries;
354 	char				 ifname[IFNAMSIZ];
355 	struct pfi_kif			*kif;
356 };
357 
358 TAILQ_HEAD(pf_palist, pf_pooladdr);
359 
360 struct pf_poolhashkey {
361 	union {
362 		u_int8_t		key8[16];
363 		u_int16_t		key16[8];
364 		u_int32_t		key32[4];
365 	} pfk;		    /* 128-bit hash key */
366 #define key8	pfk.key8
367 #define key16	pfk.key16
368 #define key32	pfk.key32
369 };
370 
371 struct pf_pool {
372 	struct pf_palist	 list;
373 	struct pf_pooladdr	*cur;
374 	struct pf_poolhashkey	 key;
375 	struct pf_addr		 counter;
376 	int			 tblidx;
377 	u_int16_t		 proxy_port[2];
378 	u_int8_t		 opts;
379 };
380 
381 
382 /* A packed Operating System description for fingerprinting */
383 typedef u_int32_t pf_osfp_t;
384 #define PF_OSFP_ANY	((pf_osfp_t)0)
385 #define PF_OSFP_UNKNOWN	((pf_osfp_t)-1)
386 #define PF_OSFP_NOMATCH	((pf_osfp_t)-2)
387 
388 struct pf_osfp_entry {
389 	SLIST_ENTRY(pf_osfp_entry) fp_entry;
390 	pf_osfp_t		fp_os;
391 	int			fp_enflags;
392 #define PF_OSFP_EXPANDED	0x001		/* expanded entry */
393 #define PF_OSFP_GENERIC		0x002		/* generic signature */
394 #define PF_OSFP_NODETAIL	0x004		/* no p0f details */
395 #define PF_OSFP_LEN	32
396 	char			fp_class_nm[PF_OSFP_LEN];
397 	char			fp_version_nm[PF_OSFP_LEN];
398 	char			fp_subtype_nm[PF_OSFP_LEN];
399 };
400 #define PF_OSFP_ENTRY_EQ(a, b) \
401     ((a)->fp_os == (b)->fp_os && \
402     memcmp((a)->fp_class_nm, (b)->fp_class_nm, PF_OSFP_LEN) == 0 && \
403     memcmp((a)->fp_version_nm, (b)->fp_version_nm, PF_OSFP_LEN) == 0 && \
404     memcmp((a)->fp_subtype_nm, (b)->fp_subtype_nm, PF_OSFP_LEN) == 0)
405 
406 /* handle pf_osfp_t packing */
407 #define _FP_RESERVED_BIT	1  /* For the special negative #defines */
408 #define _FP_UNUSED_BITS		1
409 #define _FP_CLASS_BITS		10 /* OS Class (Windows, Linux) */
410 #define _FP_VERSION_BITS	10 /* OS version (95, 98, NT, 2.4.54, 3.2) */
411 #define _FP_SUBTYPE_BITS	10 /* patch level (NT SP4, SP3, ECN patch) */
412 #define PF_OSFP_UNPACK(osfp, class, version, subtype) do { \
413 	(class) = ((osfp) >> (_FP_VERSION_BITS+_FP_SUBTYPE_BITS)) & \
414 	    ((1 << _FP_CLASS_BITS) - 1); \
415 	(version) = ((osfp) >> _FP_SUBTYPE_BITS) & \
416 	    ((1 << _FP_VERSION_BITS) - 1);\
417 	(subtype) = (osfp) & ((1 << _FP_SUBTYPE_BITS) - 1); \
418 } while(0)
419 #define PF_OSFP_PACK(osfp, class, version, subtype) do { \
420 	(osfp) = ((class) & ((1 << _FP_CLASS_BITS) - 1)) << (_FP_VERSION_BITS \
421 	    + _FP_SUBTYPE_BITS); \
422 	(osfp) |= ((version) & ((1 << _FP_VERSION_BITS) - 1)) << \
423 	    _FP_SUBTYPE_BITS; \
424 	(osfp) |= (subtype) & ((1 << _FP_SUBTYPE_BITS) - 1); \
425 } while(0)
426 
427 /* the fingerprint of an OSes TCP SYN packet */
428 typedef u_int64_t	pf_tcpopts_t;
429 struct pf_os_fingerprint {
430 	SLIST_HEAD(pf_osfp_enlist, pf_osfp_entry) fp_oses; /* list of matches */
431 	pf_tcpopts_t		fp_tcpopts;	/* packed TCP options */
432 	u_int16_t		fp_wsize;	/* TCP window size */
433 	u_int16_t		fp_psize;	/* ip->ip_len */
434 	u_int16_t		fp_mss;		/* TCP MSS */
435 	u_int16_t		fp_flags;
436 #define PF_OSFP_WSIZE_MOD	0x0001		/* Window modulus */
437 #define PF_OSFP_WSIZE_DC	0x0002		/* Window don't care */
438 #define PF_OSFP_WSIZE_MSS	0x0004		/* Window multiple of MSS */
439 #define PF_OSFP_WSIZE_MTU	0x0008		/* Window multiple of MTU */
440 #define PF_OSFP_PSIZE_MOD	0x0010		/* packet size modulus */
441 #define PF_OSFP_PSIZE_DC	0x0020		/* packet size don't care */
442 #define PF_OSFP_WSCALE		0x0040		/* TCP window scaling */
443 #define PF_OSFP_WSCALE_MOD	0x0080		/* TCP window scale modulus */
444 #define PF_OSFP_WSCALE_DC	0x0100		/* TCP window scale dont-care */
445 #define PF_OSFP_MSS		0x0200		/* TCP MSS */
446 #define PF_OSFP_MSS_MOD		0x0400		/* TCP MSS modulus */
447 #define PF_OSFP_MSS_DC		0x0800		/* TCP MSS dont-care */
448 #define PF_OSFP_DF		0x1000		/* IPv4 don't fragment bit */
449 #define PF_OSFP_TS0		0x2000		/* Zero timestamp */
450 #define PF_OSFP_INET6		0x4000		/* IPv6 */
451 	u_int8_t		fp_optcnt;	/* TCP option count */
452 	u_int8_t		fp_wscale;	/* TCP window scaling */
453 	u_int8_t		fp_ttl;		/* IPv4 TTL */
454 #define PF_OSFP_MAXTTL_OFFSET	40
455 /* TCP options packing */
456 #define PF_OSFP_TCPOPT_NOP	0x0		/* TCP NOP option */
457 #define PF_OSFP_TCPOPT_WSCALE	0x1		/* TCP window scaling option */
458 #define PF_OSFP_TCPOPT_MSS	0x2		/* TCP max segment size opt */
459 #define PF_OSFP_TCPOPT_SACK	0x3		/* TCP SACK OK option */
460 #define PF_OSFP_TCPOPT_TS	0x4		/* TCP timestamp option */
461 #define PF_OSFP_TCPOPT_BITS	3		/* bits used by each option */
462 #define PF_OSFP_MAX_OPTS \
463     (sizeof(((struct pf_os_fingerprint *)0)->fp_tcpopts) * 8) \
464     / PF_OSFP_TCPOPT_BITS
465 
466 	SLIST_ENTRY(pf_os_fingerprint)	fp_next;
467 };
468 
469 struct pf_osfp_ioctl {
470 	struct pf_osfp_entry	fp_os;
471 	pf_tcpopts_t		fp_tcpopts;	/* packed TCP options */
472 	u_int16_t		fp_wsize;	/* TCP window size */
473 	u_int16_t		fp_psize;	/* ip->ip_len */
474 	u_int16_t		fp_mss;		/* TCP MSS */
475 	u_int16_t		fp_flags;
476 	u_int8_t		fp_optcnt;	/* TCP option count */
477 	u_int8_t		fp_wscale;	/* TCP window scaling */
478 	u_int8_t		fp_ttl;		/* IPv4 TTL */
479 
480 	int			fp_getnum;	/* DIOCOSFPGET number */
481 };
482 
483 
484 union pf_rule_ptr {
485 	struct pf_rule		*ptr;
486 	u_int32_t		 nr;
487 };
488 
489 #define	PF_ANCHOR_NAME_SIZE	 64
490 
491 struct pf_rule {
492 	struct pf_rule_addr	 src;
493 	struct pf_rule_addr	 dst;
494 #define PF_SKIP_IFP		0
495 #define PF_SKIP_DIR		1
496 #define PF_SKIP_AF		2
497 #define PF_SKIP_PROTO		3
498 #define PF_SKIP_SRC_ADDR	4
499 #define PF_SKIP_SRC_PORT	5
500 #define PF_SKIP_DST_ADDR	6
501 #define PF_SKIP_DST_PORT	7
502 #define PF_SKIP_COUNT		8
503 	union pf_rule_ptr	 skip[PF_SKIP_COUNT];
504 #define PF_RULE_LABEL_SIZE	 64
505 	char			 label[PF_RULE_LABEL_SIZE];
506 	char			 ifname[IFNAMSIZ];
507 	char			 qname[PF_QNAME_SIZE];
508 	char			 pqname[PF_QNAME_SIZE];
509 #define	PF_TAG_NAME_SIZE	 64
510 	char			 tagname[PF_TAG_NAME_SIZE];
511 	char			 match_tagname[PF_TAG_NAME_SIZE];
512 
513 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
514 
515 	TAILQ_ENTRY(pf_rule)	 entries;
516 	struct pf_pool		 rpool;
517 
518 	u_int64_t		 evaluations;
519 	u_int64_t		 packets[2];
520 	u_int64_t		 bytes[2];
521 
522 	struct pfi_kif		*kif;
523 	struct pf_anchor	*anchor;
524 	struct pfr_ktable	*overload_tbl;
525 
526 	pf_osfp_t		 os_fingerprint;
527 
528 	int			 rtableid;
529 	u_int32_t		 timeout[PFTM_MAX];
530 	u_int32_t		 max_states;
531 	u_int32_t		 max_src_nodes;
532 	u_int32_t		 max_src_states;
533 	u_int32_t		 max_src_conn;
534 	struct {
535 		u_int32_t		limit;
536 		u_int32_t		seconds;
537 	}			 max_src_conn_rate;
538 	u_int32_t		 qid;
539 	u_int32_t		 pqid;
540 	u_int32_t		 rt_listid;
541 	u_int32_t		 nr;
542 	u_int32_t		 prob;
543 	uid_t			 cuid;
544 	pid_t			 cpid;
545 
546 	counter_u64_t		 states_cur;
547 	counter_u64_t		 states_tot;
548 	counter_u64_t		 src_nodes;
549 
550 	u_int16_t		 return_icmp;
551 	u_int16_t		 return_icmp6;
552 	u_int16_t		 max_mss;
553 	u_int16_t		 tag;
554 	u_int16_t		 match_tag;
555 	u_int16_t		 scrub_flags;
556 
557 	struct pf_rule_uid	 uid;
558 	struct pf_rule_gid	 gid;
559 
560 	u_int32_t		 rule_flag;
561 	u_int8_t		 action;
562 	u_int8_t		 direction;
563 	u_int8_t		 log;
564 	u_int8_t		 logif;
565 	u_int8_t		 quick;
566 	u_int8_t		 ifnot;
567 	u_int8_t		 match_tag_not;
568 	u_int8_t		 natpass;
569 
570 #define PF_STATE_NORMAL		0x1
571 #define PF_STATE_MODULATE	0x2
572 #define PF_STATE_SYNPROXY	0x3
573 	u_int8_t		 keep_state;
574 	sa_family_t		 af;
575 	u_int8_t		 proto;
576 	u_int8_t		 type;
577 	u_int8_t		 code;
578 	u_int8_t		 flags;
579 	u_int8_t		 flagset;
580 	u_int8_t		 min_ttl;
581 	u_int8_t		 allow_opts;
582 	u_int8_t		 rt;
583 	u_int8_t		 return_ttl;
584 	u_int8_t		 tos;
585 	u_int8_t		 set_tos;
586 	u_int8_t		 anchor_relative;
587 	u_int8_t		 anchor_wildcard;
588 
589 #define PF_FLUSH		0x01
590 #define PF_FLUSH_GLOBAL		0x02
591 	u_int8_t		 flush;
592 #define PF_PRIO_ZERO		0xff		/* match "prio 0" packets */
593 #define PF_PRIO_MAX		7
594 	u_int8_t		 prio;
595 	u_int8_t		 set_prio[2];
596 
597 	struct {
598 		struct pf_addr		addr;
599 		u_int16_t		port;
600 	}			divert;
601 
602 	uint64_t		 u_states_cur;
603 	uint64_t		 u_states_tot;
604 	uint64_t		 u_src_nodes;
605 };
606 
607 /* rule flags */
608 #define	PFRULE_DROP		0x0000
609 #define	PFRULE_RETURNRST	0x0001
610 #define	PFRULE_FRAGMENT		0x0002
611 #define	PFRULE_RETURNICMP	0x0004
612 #define	PFRULE_RETURN		0x0008
613 #define	PFRULE_NOSYNC		0x0010
614 #define PFRULE_SRCTRACK		0x0020  /* track source states */
615 #define PFRULE_RULESRCTRACK	0x0040  /* per rule */
616 #define	PFRULE_REFS		0x0080	/* rule has references */
617 
618 /* scrub flags */
619 #define	PFRULE_NODF		0x0100
620 #define PFRULE_RANDOMID		0x0800
621 #define PFRULE_REASSEMBLE_TCP	0x1000
622 #define PFRULE_SET_TOS		0x2000
623 
624 /* rule flags again */
625 #define PFRULE_IFBOUND		0x00010000	/* if-bound */
626 #define PFRULE_STATESLOPPY	0x00020000	/* sloppy state tracking */
627 
628 #define PFSTATE_HIWAT		100000	/* default state table size */
629 #define PFSTATE_ADAPT_START	60000	/* default adaptive timeout start */
630 #define PFSTATE_ADAPT_END	120000	/* default adaptive timeout end */
631 
632 
633 struct pf_threshold {
634 	u_int32_t	limit;
635 #define	PF_THRESHOLD_MULT	1000
636 #define PF_THRESHOLD_MAX	0xffffffff / PF_THRESHOLD_MULT
637 	u_int32_t	seconds;
638 	u_int32_t	count;
639 	u_int32_t	last;
640 };
641 
642 struct pf_src_node {
643 	LIST_ENTRY(pf_src_node) entry;
644 	struct pf_addr	 addr;
645 	struct pf_addr	 raddr;
646 	union pf_rule_ptr rule;
647 	struct pfi_kif	*kif;
648 	u_int64_t	 bytes[2];
649 	u_int64_t	 packets[2];
650 	u_int32_t	 states;
651 	u_int32_t	 conn;
652 	struct pf_threshold	conn_rate;
653 	u_int32_t	 creation;
654 	u_int32_t	 expire;
655 	sa_family_t	 af;
656 	u_int8_t	 ruletype;
657 };
658 
659 #define PFSNODE_HIWAT		10000	/* default source node table size */
660 
661 struct pf_state_scrub {
662 	struct timeval	pfss_last;	/* time received last packet	*/
663 	u_int32_t	pfss_tsecr;	/* last echoed timestamp	*/
664 	u_int32_t	pfss_tsval;	/* largest timestamp		*/
665 	u_int32_t	pfss_tsval0;	/* original timestamp		*/
666 	u_int16_t	pfss_flags;
667 #define PFSS_TIMESTAMP	0x0001		/* modulate timestamp		*/
668 #define PFSS_PAWS	0x0010		/* stricter PAWS checks		*/
669 #define PFSS_PAWS_IDLED	0x0020		/* was idle too long.  no PAWS	*/
670 #define PFSS_DATA_TS	0x0040		/* timestamp on data packets	*/
671 #define PFSS_DATA_NOTS	0x0080		/* no timestamp on data packets	*/
672 	u_int8_t	pfss_ttl;	/* stashed TTL			*/
673 	u_int8_t	pad;
674 	u_int32_t	pfss_ts_mod;	/* timestamp modulation		*/
675 };
676 
677 struct pf_state_host {
678 	struct pf_addr	addr;
679 	u_int16_t	port;
680 	u_int16_t	pad;
681 };
682 
683 struct pf_state_peer {
684 	struct pf_state_scrub	*scrub;	/* state is scrubbed		*/
685 	u_int32_t	seqlo;		/* Max sequence number sent	*/
686 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
687 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
688 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
689 	u_int16_t	mss;		/* Maximum segment size option	*/
690 	u_int8_t	state;		/* active state level		*/
691 	u_int8_t	wscale;		/* window scaling factor	*/
692 	u_int8_t	tcp_est;	/* Did we reach TCPS_ESTABLISHED */
693 	u_int8_t	pad[1];
694 };
695 
696 /* Keep synced with struct pf_state_key. */
697 struct pf_state_key_cmp {
698 	struct pf_addr	 addr[2];
699 	u_int16_t	 port[2];
700 	sa_family_t	 af;
701 	u_int8_t	 proto;
702 	u_int8_t	 pad[2];
703 };
704 
705 struct pf_state_key {
706 	struct pf_addr	 addr[2];
707 	u_int16_t	 port[2];
708 	sa_family_t	 af;
709 	u_int8_t	 proto;
710 	u_int8_t	 pad[2];
711 
712 	LIST_ENTRY(pf_state_key) entry;
713 	TAILQ_HEAD(, pf_state)	 states[2];
714 };
715 
716 /* Keep synced with struct pf_state. */
717 struct pf_state_cmp {
718 	u_int64_t		 id;
719 	u_int32_t		 creatorid;
720 	u_int8_t		 direction;
721 	u_int8_t		 pad[3];
722 };
723 
724 struct pf_state {
725 	u_int64_t		 id;
726 	u_int32_t		 creatorid;
727 	u_int8_t		 direction;
728 	u_int8_t		 pad[3];
729 
730 	u_int			 refs;
731 	TAILQ_ENTRY(pf_state)	 sync_list;
732 	TAILQ_ENTRY(pf_state)	 key_list[2];
733 	LIST_ENTRY(pf_state)	 entry;
734 	struct pf_state_peer	 src;
735 	struct pf_state_peer	 dst;
736 	union pf_rule_ptr	 rule;
737 	union pf_rule_ptr	 anchor;
738 	union pf_rule_ptr	 nat_rule;
739 	struct pf_addr		 rt_addr;
740 	struct pf_state_key	*key[2];	/* addresses stack and wire  */
741 	struct pfi_kif		*kif;
742 	struct pfi_kif		*rt_kif;
743 	struct pf_src_node	*src_node;
744 	struct pf_src_node	*nat_src_node;
745 	u_int64_t		 packets[2];
746 	u_int64_t		 bytes[2];
747 	u_int32_t		 creation;
748 	u_int32_t	 	 expire;
749 	u_int32_t		 pfsync_time;
750 	u_int16_t		 tag;
751 	u_int8_t		 log;
752 	u_int8_t		 state_flags;
753 #define	PFSTATE_ALLOWOPTS	0x01
754 #define	PFSTATE_SLOPPY		0x02
755 /*  was	PFSTATE_PFLOW		0x04 */
756 #define	PFSTATE_NOSYNC		0x08
757 #define	PFSTATE_ACK		0x10
758 #define	PFSTATE_SETPRIO		0x0200
759 #define	PFSTATE_SETMASK   (PFSTATE_SETPRIO)
760 	u_int8_t		 timeout;
761 	u_int8_t		 sync_state; /* PFSYNC_S_x */
762 
763 	/* XXX */
764 	u_int8_t		 sync_updates;
765 	u_int8_t		_tail[3];
766 };
767 
768 /*
769  * Unified state structures for pulling states out of the kernel
770  * used by pfsync(4) and the pf(4) ioctl.
771  */
772 struct pfsync_state_scrub {
773 	u_int16_t	pfss_flags;
774 	u_int8_t	pfss_ttl;	/* stashed TTL		*/
775 #define PFSYNC_SCRUB_FLAG_VALID		0x01
776 	u_int8_t	scrub_flag;
777 	u_int32_t	pfss_ts_mod;	/* timestamp modulation	*/
778 } __packed;
779 
780 struct pfsync_state_peer {
781 	struct pfsync_state_scrub scrub;	/* state is scrubbed	*/
782 	u_int32_t	seqlo;		/* Max sequence number sent	*/
783 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
784 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
785 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
786 	u_int16_t	mss;		/* Maximum segment size option	*/
787 	u_int8_t	state;		/* active state level		*/
788 	u_int8_t	wscale;		/* window scaling factor	*/
789 	u_int8_t	pad[6];
790 } __packed;
791 
792 struct pfsync_state_key {
793 	struct pf_addr	 addr[2];
794 	u_int16_t	 port[2];
795 };
796 
797 struct pfsync_state {
798 	u_int64_t	 id;
799 	char		 ifname[IFNAMSIZ];
800 	struct pfsync_state_key	key[2];
801 	struct pfsync_state_peer src;
802 	struct pfsync_state_peer dst;
803 	struct pf_addr	 rt_addr;
804 	u_int32_t	 rule;
805 	u_int32_t	 anchor;
806 	u_int32_t	 nat_rule;
807 	u_int32_t	 creation;
808 	u_int32_t	 expire;
809 	u_int32_t	 packets[2][2];
810 	u_int32_t	 bytes[2][2];
811 	u_int32_t	 creatorid;
812 	sa_family_t	 af;
813 	u_int8_t	 proto;
814 	u_int8_t	 direction;
815 	u_int8_t	 __spare[2];
816 	u_int8_t	 log;
817 	u_int8_t	 state_flags;
818 	u_int8_t	 timeout;
819 	u_int8_t	 sync_flags;
820 	u_int8_t	 updates;
821 } __packed;
822 
823 #ifdef _KERNEL
824 /* pfsync */
825 typedef int		pfsync_state_import_t(struct pfsync_state *, u_int8_t);
826 typedef	void		pfsync_insert_state_t(struct pf_state *);
827 typedef	void		pfsync_update_state_t(struct pf_state *);
828 typedef	void		pfsync_delete_state_t(struct pf_state *);
829 typedef void		pfsync_clear_states_t(u_int32_t, const char *);
830 typedef int		pfsync_defer_t(struct pf_state *, struct mbuf *);
831 typedef void		pfsync_detach_ifnet_t(struct ifnet *);
832 
833 VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr);
834 #define V_pfsync_state_import_ptr	VNET(pfsync_state_import_ptr)
835 VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
836 #define V_pfsync_insert_state_ptr	VNET(pfsync_insert_state_ptr)
837 VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr);
838 #define V_pfsync_update_state_ptr	VNET(pfsync_update_state_ptr)
839 VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
840 #define V_pfsync_delete_state_ptr	VNET(pfsync_delete_state_ptr)
841 VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
842 #define V_pfsync_clear_states_ptr	VNET(pfsync_clear_states_ptr)
843 VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr);
844 #define V_pfsync_defer_ptr		VNET(pfsync_defer_ptr)
845 extern pfsync_detach_ifnet_t	*pfsync_detach_ifnet_ptr;
846 
847 void			pfsync_state_export(struct pfsync_state *,
848 			    struct pf_state *);
849 
850 /* pflog */
851 struct pf_ruleset;
852 struct pf_pdesc;
853 typedef int pflog_packet_t(struct pfi_kif *, struct mbuf *, sa_family_t,
854     u_int8_t, u_int8_t, struct pf_rule *, struct pf_rule *,
855     struct pf_ruleset *, struct pf_pdesc *, int);
856 extern pflog_packet_t		*pflog_packet_ptr;
857 
858 #endif /* _KERNEL */
859 
860 #define	PFSYNC_FLAG_SRCNODE	0x04
861 #define	PFSYNC_FLAG_NATSRCNODE	0x08
862 
863 /* for copies to/from network byte order */
864 /* ioctl interface also uses network byte order */
865 #define pf_state_peer_hton(s,d) do {		\
866 	(d)->seqlo = htonl((s)->seqlo);		\
867 	(d)->seqhi = htonl((s)->seqhi);		\
868 	(d)->seqdiff = htonl((s)->seqdiff);	\
869 	(d)->max_win = htons((s)->max_win);	\
870 	(d)->mss = htons((s)->mss);		\
871 	(d)->state = (s)->state;		\
872 	(d)->wscale = (s)->wscale;		\
873 	if ((s)->scrub) {						\
874 		(d)->scrub.pfss_flags = 				\
875 		    htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP);	\
876 		(d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;		\
877 		(d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
878 		(d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;	\
879 	}								\
880 } while (0)
881 
882 #define pf_state_peer_ntoh(s,d) do {		\
883 	(d)->seqlo = ntohl((s)->seqlo);		\
884 	(d)->seqhi = ntohl((s)->seqhi);		\
885 	(d)->seqdiff = ntohl((s)->seqdiff);	\
886 	(d)->max_win = ntohs((s)->max_win);	\
887 	(d)->mss = ntohs((s)->mss);		\
888 	(d)->state = (s)->state;		\
889 	(d)->wscale = (s)->wscale;		\
890 	if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && 	\
891 	    (d)->scrub != NULL) {					\
892 		(d)->scrub->pfss_flags =				\
893 		    ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP;	\
894 		(d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl;		\
895 		(d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
896 	}								\
897 } while (0)
898 
899 #define pf_state_counter_hton(s,d) do {				\
900 	d[0] = htonl((s>>32)&0xffffffff);			\
901 	d[1] = htonl(s&0xffffffff);				\
902 } while (0)
903 
904 #define pf_state_counter_from_pfsync(s)				\
905 	(((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
906 
907 #define pf_state_counter_ntoh(s,d) do {				\
908 	d = ntohl(s[0]);					\
909 	d = d<<32;						\
910 	d += ntohl(s[1]);					\
911 } while (0)
912 
913 TAILQ_HEAD(pf_rulequeue, pf_rule);
914 
915 struct pf_anchor;
916 
917 struct pf_ruleset {
918 	struct {
919 		struct pf_rulequeue	 queues[2];
920 		struct {
921 			struct pf_rulequeue	*ptr;
922 			struct pf_rule		**ptr_array;
923 			u_int32_t		 rcount;
924 			u_int32_t		 ticket;
925 			int			 open;
926 		}			 active, inactive;
927 	}			 rules[PF_RULESET_MAX];
928 	struct pf_anchor	*anchor;
929 	u_int32_t		 tticket;
930 	int			 tables;
931 	int			 topen;
932 };
933 
934 RB_HEAD(pf_anchor_global, pf_anchor);
935 RB_HEAD(pf_anchor_node, pf_anchor);
936 struct pf_anchor {
937 	RB_ENTRY(pf_anchor)	 entry_global;
938 	RB_ENTRY(pf_anchor)	 entry_node;
939 	struct pf_anchor	*parent;
940 	struct pf_anchor_node	 children;
941 	char			 name[PF_ANCHOR_NAME_SIZE];
942 	char			 path[MAXPATHLEN];
943 	struct pf_ruleset	 ruleset;
944 	int			 refcnt;	/* anchor rules */
945 	int			 match;	/* XXX: used for pfctl black magic */
946 };
947 RB_PROTOTYPE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
948 RB_PROTOTYPE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
949 
950 #define PF_RESERVED_ANCHOR	"_pf"
951 
952 #define PFR_TFLAG_PERSIST	0x00000001
953 #define PFR_TFLAG_CONST		0x00000002
954 #define PFR_TFLAG_ACTIVE	0x00000004
955 #define PFR_TFLAG_INACTIVE	0x00000008
956 #define PFR_TFLAG_REFERENCED	0x00000010
957 #define PFR_TFLAG_REFDANCHOR	0x00000020
958 #define PFR_TFLAG_COUNTERS	0x00000040
959 /* Adjust masks below when adding flags. */
960 #define PFR_TFLAG_USRMASK	(PFR_TFLAG_PERSIST	| \
961 				 PFR_TFLAG_CONST	| \
962 				 PFR_TFLAG_COUNTERS)
963 #define PFR_TFLAG_SETMASK	(PFR_TFLAG_ACTIVE	| \
964 				 PFR_TFLAG_INACTIVE	| \
965 				 PFR_TFLAG_REFERENCED	| \
966 				 PFR_TFLAG_REFDANCHOR)
967 #define PFR_TFLAG_ALLMASK	(PFR_TFLAG_PERSIST	| \
968 				 PFR_TFLAG_CONST	| \
969 				 PFR_TFLAG_ACTIVE	| \
970 				 PFR_TFLAG_INACTIVE	| \
971 				 PFR_TFLAG_REFERENCED	| \
972 				 PFR_TFLAG_REFDANCHOR	| \
973 				 PFR_TFLAG_COUNTERS)
974 
975 struct pf_anchor_stackframe;
976 
977 struct pfr_table {
978 	char			 pfrt_anchor[MAXPATHLEN];
979 	char			 pfrt_name[PF_TABLE_NAME_SIZE];
980 	u_int32_t		 pfrt_flags;
981 	u_int8_t		 pfrt_fback;
982 };
983 
984 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
985 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
986 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX };
987 
988 struct pfr_addr {
989 	union {
990 		struct in_addr	 _pfra_ip4addr;
991 		struct in6_addr	 _pfra_ip6addr;
992 	}		 pfra_u;
993 	u_int8_t	 pfra_af;
994 	u_int8_t	 pfra_net;
995 	u_int8_t	 pfra_not;
996 	u_int8_t	 pfra_fback;
997 };
998 #define	pfra_ip4addr	pfra_u._pfra_ip4addr
999 #define	pfra_ip6addr	pfra_u._pfra_ip6addr
1000 
1001 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
1002 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
1003 #define PFR_OP_XPASS	PFR_OP_ADDR_MAX
1004 
1005 struct pfr_astats {
1006 	struct pfr_addr	 pfras_a;
1007 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1008 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1009 	long		 pfras_tzero;
1010 };
1011 
1012 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
1013 
1014 struct pfr_tstats {
1015 	struct pfr_table pfrts_t;
1016 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1017 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1018 	u_int64_t	 pfrts_match;
1019 	u_int64_t	 pfrts_nomatch;
1020 	long		 pfrts_tzero;
1021 	int		 pfrts_cnt;
1022 	int		 pfrts_refcnt[PFR_REFCNT_MAX];
1023 };
1024 
1025 struct pfr_ktstats {
1026 	struct pfr_table pfrts_t;
1027 	counter_u64_t	 pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1028 	counter_u64_t	 pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1029 	counter_u64_t	 pfrkts_match;
1030 	counter_u64_t	 pfrkts_nomatch;
1031 	long		 pfrkts_tzero;
1032 	int		 pfrkts_cnt;
1033 	int		 pfrkts_refcnt[PFR_REFCNT_MAX];
1034 };
1035 #define	pfrts_name	pfrts_t.pfrt_name
1036 #define pfrts_flags	pfrts_t.pfrt_flags
1037 
1038 #ifndef _SOCKADDR_UNION_DEFINED
1039 #define	_SOCKADDR_UNION_DEFINED
1040 union sockaddr_union {
1041 	struct sockaddr		sa;
1042 	struct sockaddr_in	sin;
1043 	struct sockaddr_in6	sin6;
1044 };
1045 #endif /* _SOCKADDR_UNION_DEFINED */
1046 
1047 struct pfr_kcounters {
1048 	counter_u64_t		 pfrkc_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1049 	counter_u64_t		 pfrkc_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1050 	long			 pfrkc_tzero;
1051 };
1052 
1053 SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
1054 struct pfr_kentry {
1055 	struct radix_node	 pfrke_node[2];
1056 	union sockaddr_union	 pfrke_sa;
1057 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq;
1058 	struct pfr_kcounters	 pfrke_counters;
1059 	u_int8_t		 pfrke_af;
1060 	u_int8_t		 pfrke_net;
1061 	u_int8_t		 pfrke_not;
1062 	u_int8_t		 pfrke_mark;
1063 };
1064 
1065 SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
1066 RB_HEAD(pfr_ktablehead, pfr_ktable);
1067 struct pfr_ktable {
1068 	struct pfr_ktstats	 pfrkt_kts;
1069 	RB_ENTRY(pfr_ktable)	 pfrkt_tree;
1070 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq;
1071 	struct radix_node_head	*pfrkt_ip4;
1072 	struct radix_node_head	*pfrkt_ip6;
1073 	struct pfr_ktable	*pfrkt_shadow;
1074 	struct pfr_ktable	*pfrkt_root;
1075 	struct pf_ruleset	*pfrkt_rs;
1076 	long			 pfrkt_larg;
1077 	int			 pfrkt_nflags;
1078 };
1079 #define pfrkt_t		pfrkt_kts.pfrts_t
1080 #define pfrkt_name	pfrkt_t.pfrt_name
1081 #define pfrkt_anchor	pfrkt_t.pfrt_anchor
1082 #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset
1083 #define pfrkt_flags	pfrkt_t.pfrt_flags
1084 #define pfrkt_cnt	pfrkt_kts.pfrkts_cnt
1085 #define pfrkt_refcnt	pfrkt_kts.pfrkts_refcnt
1086 #define pfrkt_packets	pfrkt_kts.pfrkts_packets
1087 #define pfrkt_bytes	pfrkt_kts.pfrkts_bytes
1088 #define pfrkt_match	pfrkt_kts.pfrkts_match
1089 #define pfrkt_nomatch	pfrkt_kts.pfrkts_nomatch
1090 #define pfrkt_tzero	pfrkt_kts.pfrkts_tzero
1091 
1092 /* keep synced with pfi_kif, used in RB_FIND */
1093 struct pfi_kif_cmp {
1094 	char				 pfik_name[IFNAMSIZ];
1095 };
1096 
1097 struct pfi_kif {
1098 	char				 pfik_name[IFNAMSIZ];
1099 	union {
1100 		RB_ENTRY(pfi_kif)	 _pfik_tree;
1101 		LIST_ENTRY(pfi_kif)	 _pfik_list;
1102 	} _pfik_glue;
1103 #define	pfik_tree	_pfik_glue._pfik_tree
1104 #define	pfik_list	_pfik_glue._pfik_list
1105 	u_int64_t			 pfik_packets[2][2][2];
1106 	u_int64_t			 pfik_bytes[2][2][2];
1107 	u_int32_t			 pfik_tzero;
1108 	u_int				 pfik_flags;
1109 	struct ifnet			*pfik_ifp;
1110 	struct ifg_group		*pfik_group;
1111 	u_int				 pfik_rulerefs;
1112 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1113 };
1114 
1115 #define	PFI_IFLAG_REFS		0x0001	/* has state references */
1116 #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */
1117 
1118 struct pf_pdesc {
1119 	struct {
1120 		int	 done;
1121 		uid_t	 uid;
1122 		gid_t	 gid;
1123 	}		 lookup;
1124 	u_int64_t	 tot_len;	/* Make Mickey money */
1125 	union {
1126 		struct tcphdr		*tcp;
1127 		struct udphdr		*udp;
1128 		struct icmp		*icmp;
1129 #ifdef INET6
1130 		struct icmp6_hdr	*icmp6;
1131 #endif /* INET6 */
1132 		void			*any;
1133 	} hdr;
1134 
1135 	struct pf_rule	*nat_rule;	/* nat/rdr rule applied to packet */
1136 	struct pf_addr	*src;		/* src address */
1137 	struct pf_addr	*dst;		/* dst address */
1138 	u_int16_t *sport;
1139 	u_int16_t *dport;
1140 	struct pf_mtag	*pf_mtag;
1141 
1142 	u_int32_t	 p_len;		/* total length of payload */
1143 
1144 	u_int16_t	*ip_sum;
1145 	u_int16_t	*proto_sum;
1146 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
1147 					 * state code. Easier than tags */
1148 #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
1149 #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
1150 	sa_family_t	 af;
1151 	u_int8_t	 proto;
1152 	u_int8_t	 tos;
1153 	u_int8_t	 dir;		/* direction */
1154 	u_int8_t	 sidx;		/* key index for source */
1155 	u_int8_t	 didx;		/* key index for destination */
1156 };
1157 
1158 /* flags for RDR options */
1159 #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
1160 #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
1161 
1162 /* UDP state enumeration */
1163 #define PFUDPS_NO_TRAFFIC	0
1164 #define PFUDPS_SINGLE		1
1165 #define PFUDPS_MULTIPLE		2
1166 
1167 #define PFUDPS_NSTATES		3	/* number of state levels */
1168 
1169 #define PFUDPS_NAMES { \
1170 	"NO_TRAFFIC", \
1171 	"SINGLE", \
1172 	"MULTIPLE", \
1173 	NULL \
1174 }
1175 
1176 /* Other protocol state enumeration */
1177 #define PFOTHERS_NO_TRAFFIC	0
1178 #define PFOTHERS_SINGLE		1
1179 #define PFOTHERS_MULTIPLE	2
1180 
1181 #define PFOTHERS_NSTATES	3	/* number of state levels */
1182 
1183 #define PFOTHERS_NAMES { \
1184 	"NO_TRAFFIC", \
1185 	"SINGLE", \
1186 	"MULTIPLE", \
1187 	NULL \
1188 }
1189 
1190 #define ACTION_SET(a, x) \
1191 	do { \
1192 		if ((a) != NULL) \
1193 			*(a) = (x); \
1194 	} while (0)
1195 
1196 #define REASON_SET(a, x) \
1197 	do { \
1198 		if ((a) != NULL) \
1199 			*(a) = (x); \
1200 		if (x < PFRES_MAX) \
1201 			counter_u64_add(V_pf_status.counters[x], 1); \
1202 	} while (0)
1203 
1204 struct pf_kstatus {
1205 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */
1206 	counter_u64_t	lcounters[LCNT_MAX]; /* limit counters */
1207 	counter_u64_t	fcounters[FCNT_MAX]; /* state operation counters */
1208 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */
1209 	uint32_t	states;
1210 	uint32_t	src_nodes;
1211 	uint32_t	running;
1212 	uint32_t	since;
1213 	uint32_t	debug;
1214 	uint32_t	hostid;
1215 	char		ifname[IFNAMSIZ];
1216 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
1217 };
1218 
1219 struct pf_divert {
1220 	union {
1221 		struct in_addr	ipv4;
1222 		struct in6_addr	ipv6;
1223 	}		addr;
1224 	u_int16_t	port;
1225 };
1226 
1227 #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
1228 #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
1229 
1230 /*
1231  * Limit the length of the fragment queue traversal.  Remember
1232  * search entry points based on the fragment offset.
1233  */
1234 #define PF_FRAG_ENTRY_POINTS		16
1235 
1236 /*
1237  * The number of entries in the fragment queue must be limited
1238  * to avoid DoS by linear seaching.  Instead of a global limit,
1239  * use a limit per entry point.  For large packets these sum up.
1240  */
1241 #define PF_FRAG_ENTRY_LIMIT		64
1242 
1243 /*
1244  * ioctl parameter structures
1245  */
1246 
1247 struct pfioc_pooladdr {
1248 	u_int32_t		 action;
1249 	u_int32_t		 ticket;
1250 	u_int32_t		 nr;
1251 	u_int32_t		 r_num;
1252 	u_int8_t		 r_action;
1253 	u_int8_t		 r_last;
1254 	u_int8_t		 af;
1255 	char			 anchor[MAXPATHLEN];
1256 	struct pf_pooladdr	 addr;
1257 };
1258 
1259 struct pfioc_rule {
1260 	u_int32_t	 action;
1261 	u_int32_t	 ticket;
1262 	u_int32_t	 pool_ticket;
1263 	u_int32_t	 nr;
1264 	char		 anchor[MAXPATHLEN];
1265 	char		 anchor_call[MAXPATHLEN];
1266 	struct pf_rule	 rule;
1267 };
1268 
1269 struct pfioc_natlook {
1270 	struct pf_addr	 saddr;
1271 	struct pf_addr	 daddr;
1272 	struct pf_addr	 rsaddr;
1273 	struct pf_addr	 rdaddr;
1274 	u_int16_t	 sport;
1275 	u_int16_t	 dport;
1276 	u_int16_t	 rsport;
1277 	u_int16_t	 rdport;
1278 	sa_family_t	 af;
1279 	u_int8_t	 proto;
1280 	u_int8_t	 direction;
1281 };
1282 
1283 struct pfioc_state {
1284 	struct pfsync_state	state;
1285 };
1286 
1287 struct pfioc_src_node_kill {
1288 	sa_family_t psnk_af;
1289 	struct pf_rule_addr psnk_src;
1290 	struct pf_rule_addr psnk_dst;
1291 	u_int		    psnk_killed;
1292 };
1293 
1294 struct pfioc_state_kill {
1295 	struct pf_state_cmp	psk_pfcmp;
1296 	sa_family_t		psk_af;
1297 	int			psk_proto;
1298 	struct pf_rule_addr	psk_src;
1299 	struct pf_rule_addr	psk_dst;
1300 	char			psk_ifname[IFNAMSIZ];
1301 	char			psk_label[PF_RULE_LABEL_SIZE];
1302 	u_int			psk_killed;
1303 };
1304 
1305 struct pfioc_states {
1306 	int	ps_len;
1307 	union {
1308 		caddr_t			 psu_buf;
1309 		struct pfsync_state	*psu_states;
1310 	} ps_u;
1311 #define ps_buf		ps_u.psu_buf
1312 #define ps_states	ps_u.psu_states
1313 };
1314 
1315 struct pfioc_src_nodes {
1316 	int	psn_len;
1317 	union {
1318 		caddr_t		 psu_buf;
1319 		struct pf_src_node	*psu_src_nodes;
1320 	} psn_u;
1321 #define psn_buf		psn_u.psu_buf
1322 #define psn_src_nodes	psn_u.psu_src_nodes
1323 };
1324 
1325 struct pfioc_if {
1326 	char		 ifname[IFNAMSIZ];
1327 };
1328 
1329 struct pfioc_tm {
1330 	int		 timeout;
1331 	int		 seconds;
1332 };
1333 
1334 struct pfioc_limit {
1335 	int		 index;
1336 	unsigned	 limit;
1337 };
1338 
1339 struct pfioc_altq_v0 {
1340 	u_int32_t	 action;
1341 	u_int32_t	 ticket;
1342 	u_int32_t	 nr;
1343 	struct pf_altq_v0 altq;
1344 };
1345 
1346 struct pfioc_altq_v1 {
1347 	u_int32_t	 action;
1348 	u_int32_t	 ticket;
1349 	u_int32_t	 nr;
1350 	/*
1351 	 * Placed here so code that only uses the above parameters can be
1352 	 * written entirely in terms of the v0 or v1 type.
1353 	 */
1354 	u_int32_t	 version;
1355 	struct pf_altq_v1 altq;
1356 };
1357 
1358 /*
1359  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1360  * the latest version of struct pf_altq_vX as it has that struct as a
1361  * member.
1362  */
1363 #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION
1364 
1365 struct pfioc_qstats_v0 {
1366 	u_int32_t	 ticket;
1367 	u_int32_t	 nr;
1368 	void		*buf;
1369 	int		 nbytes;
1370 	u_int8_t	 scheduler;
1371 };
1372 
1373 struct pfioc_qstats_v1 {
1374 	u_int32_t	 ticket;
1375 	u_int32_t	 nr;
1376 	void		*buf;
1377 	int		 nbytes;
1378 	u_int8_t	 scheduler;
1379 	/*
1380 	 * Placed here so code that only uses the above parameters can be
1381 	 * written entirely in terms of the v0 or v1 type.
1382 	 */
1383 	u_int32_t	 version;  /* Requested version of stats struct */
1384 };
1385 
1386 /* Latest version of struct pfioc_qstats_vX */
1387 #define PFIOC_QSTATS_VERSION	1
1388 
1389 struct pfioc_ruleset {
1390 	u_int32_t	 nr;
1391 	char		 path[MAXPATHLEN];
1392 	char		 name[PF_ANCHOR_NAME_SIZE];
1393 };
1394 
1395 #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
1396 #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1397 struct pfioc_trans {
1398 	int		 size;	/* number of elements */
1399 	int		 esize; /* size of each element in bytes */
1400 	struct pfioc_trans_e {
1401 		int		rs_num;
1402 		char		anchor[MAXPATHLEN];
1403 		u_int32_t	ticket;
1404 	}		*array;
1405 };
1406 
1407 #define PFR_FLAG_ATOMIC		0x00000001	/* unused */
1408 #define PFR_FLAG_DUMMY		0x00000002
1409 #define PFR_FLAG_FEEDBACK	0x00000004
1410 #define PFR_FLAG_CLSTATS	0x00000008
1411 #define PFR_FLAG_ADDRSTOO	0x00000010
1412 #define PFR_FLAG_REPLACE	0x00000020
1413 #define PFR_FLAG_ALLRSETS	0x00000040
1414 #define PFR_FLAG_ALLMASK	0x0000007F
1415 #ifdef _KERNEL
1416 #define PFR_FLAG_USERIOCTL	0x10000000
1417 #endif
1418 
1419 struct pfioc_table {
1420 	struct pfr_table	 pfrio_table;
1421 	void			*pfrio_buffer;
1422 	int			 pfrio_esize;
1423 	int			 pfrio_size;
1424 	int			 pfrio_size2;
1425 	int			 pfrio_nadd;
1426 	int			 pfrio_ndel;
1427 	int			 pfrio_nchange;
1428 	int			 pfrio_flags;
1429 	u_int32_t		 pfrio_ticket;
1430 };
1431 #define	pfrio_exists	pfrio_nadd
1432 #define	pfrio_nzero	pfrio_nadd
1433 #define	pfrio_nmatch	pfrio_nadd
1434 #define pfrio_naddr	pfrio_size2
1435 #define pfrio_setflag	pfrio_size2
1436 #define pfrio_clrflag	pfrio_nadd
1437 
1438 struct pfioc_iface {
1439 	char	 pfiio_name[IFNAMSIZ];
1440 	void	*pfiio_buffer;
1441 	int	 pfiio_esize;
1442 	int	 pfiio_size;
1443 	int	 pfiio_nzero;
1444 	int	 pfiio_flags;
1445 };
1446 
1447 
1448 /*
1449  * ioctl operations
1450  */
1451 
1452 #define DIOCSTART	_IO  ('D',  1)
1453 #define DIOCSTOP	_IO  ('D',  2)
1454 #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
1455 #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
1456 #define DIOCGETRULE	_IOWR('D',  7, struct pfioc_rule)
1457 /* XXX cut 8 - 17 */
1458 #define DIOCCLRSTATES	_IOWR('D', 18, struct pfioc_state_kill)
1459 #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
1460 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1461 #define DIOCGETSTATUS	_IOWR('D', 21, struct pf_status)
1462 #define DIOCCLRSTATUS	_IO  ('D', 22)
1463 #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
1464 #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
1465 #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1466 #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
1467 /* XXX cut 26 - 28 */
1468 #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
1469 #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1470 #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1471 #define DIOCCLRRULECTRS	_IO  ('D', 38)
1472 #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
1473 #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
1474 #define DIOCKILLSTATES	_IOWR('D', 41, struct pfioc_state_kill)
1475 #define DIOCSTARTALTQ	_IO  ('D', 42)
1476 #define DIOCSTOPALTQ	_IO  ('D', 43)
1477 #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0)
1478 #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1)
1479 #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0)
1480 #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1)
1481 #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0)
1482 #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1)
1483 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1484 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1485 #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0)
1486 #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1)
1487 #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
1488 #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
1489 #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
1490 #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1491 #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
1492 /* XXX cut 55 - 57 */
1493 #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
1494 #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
1495 #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
1496 #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
1497 #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
1498 #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
1499 #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
1500 #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table)
1501 #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
1502 #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
1503 #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
1504 #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
1505 #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
1506 #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
1507 #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table)
1508 #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
1509 #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
1510 #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
1511 #define	DIOCOSFPFLUSH	_IO('D', 78)
1512 #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
1513 #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
1514 #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans)
1515 #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans)
1516 #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans)
1517 #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
1518 #define	DIOCCLRSRCNODES	_IO('D', 85)
1519 #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
1520 #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
1521 #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
1522 #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
1523 #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
1524 struct pf_ifspeed_v0 {
1525 	char			ifname[IFNAMSIZ];
1526 	u_int32_t		baudrate;
1527 };
1528 
1529 struct pf_ifspeed_v1 {
1530 	char			ifname[IFNAMSIZ];
1531 	u_int32_t		baudrate32;
1532 	/* layout identical to struct pf_ifspeed_v0 up to this point */
1533 	u_int64_t		baudrate;
1534 };
1535 
1536 /* Latest version of struct pf_ifspeed_vX */
1537 #define PF_IFSPEED_VERSION	1
1538 
1539 #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0)
1540 #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1)
1541 
1542 /*
1543  * Compatibility and convenience macros
1544  */
1545 #ifndef _KERNEL
1546 #ifdef PFIOC_USE_LATEST
1547 /*
1548  * Maintaining in-tree consumers of the ioctl interface is easier when that
1549  * code can be written in terms old names that refer to the latest interface
1550  * version as that reduces the required changes in the consumers to those
1551  * that are functionally necessary to accommodate a new interface version.
1552  */
1553 #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
1554 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
1555 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
1556 
1557 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
1558 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
1559 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
1560 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
1561 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
1562 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
1563 #else
1564 /*
1565  * When building out-of-tree code that is written for the old interface,
1566  * such as may exist in ports for example, resolve the old struct tags and
1567  * ioctl command names to the v0 versions.
1568  */
1569 #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0)
1570 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0)
1571 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0)
1572 
1573 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0)
1574 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0)
1575 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0)
1576 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0)
1577 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0)
1578 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0)
1579 #endif /* PFIOC_USE_LATEST */
1580 #endif /* _KERNEL */
1581 
1582 #ifdef _KERNEL
1583 LIST_HEAD(pf_src_node_list, pf_src_node);
1584 struct pf_srchash {
1585 	struct pf_src_node_list		nodes;
1586 	struct mtx			lock;
1587 };
1588 
1589 struct pf_keyhash {
1590 	LIST_HEAD(, pf_state_key)	keys;
1591 	struct mtx			lock;
1592 };
1593 
1594 struct pf_idhash {
1595 	LIST_HEAD(, pf_state)		states;
1596 	struct mtx			lock;
1597 };
1598 
1599 extern u_long		pf_hashmask;
1600 extern u_long		pf_srchashmask;
1601 #define	PF_HASHSIZ	(131072)
1602 #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4)
1603 VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
1604 VNET_DECLARE(struct pf_idhash *, pf_idhash);
1605 #define V_pf_keyhash	VNET(pf_keyhash)
1606 #define	V_pf_idhash	VNET(pf_idhash)
1607 VNET_DECLARE(struct pf_srchash *, pf_srchash);
1608 #define	V_pf_srchash	VNET(pf_srchash)
1609 
1610 #define PF_IDHASH(s)	(be64toh((s)->id) % (pf_hashmask + 1))
1611 
1612 VNET_DECLARE(void *, pf_swi_cookie);
1613 #define V_pf_swi_cookie	VNET(pf_swi_cookie)
1614 
1615 VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]);
1616 #define	V_pf_stateid	VNET(pf_stateid)
1617 
1618 TAILQ_HEAD(pf_altqqueue, pf_altq);
1619 VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]);
1620 #define	V_pf_altqs			 VNET(pf_altqs)
1621 VNET_DECLARE(struct pf_palist,		 pf_pabuf);
1622 #define	V_pf_pabuf			 VNET(pf_pabuf)
1623 
1624 VNET_DECLARE(u_int32_t,			 ticket_altqs_active);
1625 #define	V_ticket_altqs_active		 VNET(ticket_altqs_active)
1626 VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive);
1627 #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive)
1628 VNET_DECLARE(int,			 altqs_inactive_open);
1629 #define	V_altqs_inactive_open		 VNET(altqs_inactive_open)
1630 VNET_DECLARE(u_int32_t,			 ticket_pabuf);
1631 #define	V_ticket_pabuf			 VNET(ticket_pabuf)
1632 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active);
1633 #define	V_pf_altqs_active		 VNET(pf_altqs_active)
1634 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active);
1635 #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active)
1636 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive);
1637 #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive)
1638 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
1639 #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive)
1640 
1641 VNET_DECLARE(struct pf_rulequeue, pf_unlinked_rules);
1642 #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules)
1643 
1644 void				 pf_initialize(void);
1645 void				 pf_mtag_initialize(void);
1646 void				 pf_mtag_cleanup(void);
1647 void				 pf_cleanup(void);
1648 
1649 struct pf_mtag			*pf_get_mtag(struct mbuf *);
1650 
1651 extern void			 pf_calc_skip_steps(struct pf_rulequeue *);
1652 #ifdef ALTQ
1653 extern	void			 pf_altq_ifnet_event(struct ifnet *, int);
1654 #endif
1655 VNET_DECLARE(uma_zone_t,	 pf_state_z);
1656 #define	V_pf_state_z		 VNET(pf_state_z)
1657 VNET_DECLARE(uma_zone_t,	 pf_state_key_z);
1658 #define	V_pf_state_key_z	 VNET(pf_state_key_z)
1659 VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z);
1660 #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z)
1661 
1662 extern void			 pf_purge_thread(void *);
1663 extern void			 pf_unload_vnet_purge(void);
1664 extern void			 pf_intr(void *);
1665 extern void			 pf_purge_expired_src_nodes(void);
1666 
1667 extern int			 pf_unlink_state(struct pf_state *, u_int);
1668 #define	PF_ENTER_LOCKED		0x00000001
1669 #define	PF_RETURN_LOCKED	0x00000002
1670 extern int			 pf_state_insert(struct pfi_kif *,
1671 				    struct pf_state_key *,
1672 				    struct pf_state_key *,
1673 				    struct pf_state *);
1674 extern void			 pf_free_state(struct pf_state *);
1675 
1676 static __inline void
1677 pf_ref_state(struct pf_state *s)
1678 {
1679 
1680 	refcount_acquire(&s->refs);
1681 }
1682 
1683 static __inline int
1684 pf_release_state(struct pf_state *s)
1685 {
1686 
1687 	if (refcount_release(&s->refs)) {
1688 		pf_free_state(s);
1689 		return (1);
1690 	} else
1691 		return (0);
1692 }
1693 
1694 extern struct pf_state		*pf_find_state_byid(uint64_t, uint32_t);
1695 extern struct pf_state		*pf_find_state_all(struct pf_state_key_cmp *,
1696 				    u_int, int *);
1697 extern struct pf_src_node	*pf_find_src_node(struct pf_addr *,
1698 				    struct pf_rule *, sa_family_t, int);
1699 extern void			 pf_unlink_src_node(struct pf_src_node *);
1700 extern u_int			 pf_free_src_nodes(struct pf_src_node_list *);
1701 extern void			 pf_print_state(struct pf_state *);
1702 extern void			 pf_print_flags(u_int8_t);
1703 extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
1704 				    u_int8_t);
1705 extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
1706 				    u_int16_t, u_int16_t, u_int8_t);
1707 
1708 VNET_DECLARE(struct ifnet *,		 sync_ifp);
1709 #define	V_sync_ifp		 	 VNET(sync_ifp);
1710 VNET_DECLARE(struct pf_rule,		 pf_default_rule);
1711 #define	V_pf_default_rule		  VNET(pf_default_rule)
1712 extern void			 pf_addrcpy(struct pf_addr *, struct pf_addr *,
1713 				    u_int8_t);
1714 void				pf_free_rule(struct pf_rule *);
1715 
1716 #ifdef INET
1717 int	pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1718 int	pf_normalize_ip(struct mbuf **, int, struct pfi_kif *, u_short *,
1719 	    struct pf_pdesc *);
1720 #endif /* INET */
1721 
1722 #ifdef INET6
1723 int	pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1724 int	pf_normalize_ip6(struct mbuf **, int, struct pfi_kif *, u_short *,
1725 	    struct pf_pdesc *);
1726 void	pf_poolmask(struct pf_addr *, struct pf_addr*,
1727 	    struct pf_addr *, struct pf_addr *, u_int8_t);
1728 void	pf_addr_inc(struct pf_addr *, sa_family_t);
1729 int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *);
1730 #endif /* INET6 */
1731 
1732 u_int32_t	pf_new_isn(struct pf_state *);
1733 void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
1734 	    sa_family_t);
1735 void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
1736 void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
1737 	    u_int8_t);
1738 void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
1739 void	pf_send_deferred_syn(struct pf_state *);
1740 int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
1741 	    struct pf_addr *, sa_family_t);
1742 int	pf_match_addr_range(struct pf_addr *, struct pf_addr *,
1743 	    struct pf_addr *, sa_family_t);
1744 int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
1745 
1746 void	pf_normalize_init(void);
1747 void	pf_normalize_cleanup(void);
1748 int	pf_normalize_tcp(int, struct pfi_kif *, struct mbuf *, int, int, void *,
1749 	    struct pf_pdesc *);
1750 void	pf_normalize_tcp_cleanup(struct pf_state *);
1751 int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
1752 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
1753 int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
1754 	    u_short *, struct tcphdr *, struct pf_state *,
1755 	    struct pf_state_peer *, struct pf_state_peer *, int *);
1756 u_int32_t
1757 	pf_state_expires(const struct pf_state *);
1758 void	pf_purge_expired_fragments(void);
1759 void	pf_purge_fragments(uint32_t);
1760 int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *,
1761 	    int);
1762 int	pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *);
1763 struct pf_state_key *pf_alloc_state_key(int);
1764 void	pfr_initialize(void);
1765 void	pfr_cleanup(void);
1766 int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
1767 void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
1768 	    u_int64_t, int, int, int);
1769 int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
1770 void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
1771 struct pfr_ktable *
1772 	pfr_attach_table(struct pf_ruleset *, char *);
1773 void	pfr_detach_table(struct pfr_ktable *);
1774 int	pfr_clr_tables(struct pfr_table *, int *, int);
1775 int	pfr_add_tables(struct pfr_table *, int, int *, int);
1776 int	pfr_del_tables(struct pfr_table *, int, int *, int);
1777 int	pfr_table_count(struct pfr_table *, int);
1778 int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
1779 int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
1780 int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
1781 int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
1782 int	pfr_clr_addrs(struct pfr_table *, int *, int);
1783 int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
1784 int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1785 	    int);
1786 int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1787 	    int);
1788 int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1789 	    int *, int *, int *, int, u_int32_t);
1790 int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
1791 int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
1792 int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
1793 	    int);
1794 int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1795 	    int);
1796 int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
1797 int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
1798 int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
1799 int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
1800 	    int *, u_int32_t, int);
1801 
1802 MALLOC_DECLARE(PFI_MTYPE);
1803 VNET_DECLARE(struct pfi_kif *,		 pfi_all);
1804 #define	V_pfi_all	 		 VNET(pfi_all)
1805 
1806 void		 pfi_initialize(void);
1807 void		 pfi_initialize_vnet(void);
1808 void		 pfi_cleanup(void);
1809 void		 pfi_cleanup_vnet(void);
1810 void		 pfi_kif_ref(struct pfi_kif *);
1811 void		 pfi_kif_unref(struct pfi_kif *);
1812 struct pfi_kif	*pfi_kif_find(const char *);
1813 struct pfi_kif	*pfi_kif_attach(struct pfi_kif *, const char *);
1814 int		 pfi_kif_match(struct pfi_kif *, struct pfi_kif *);
1815 void		 pfi_kif_purge(void);
1816 int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
1817 		    sa_family_t);
1818 int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
1819 void		 pfi_dynaddr_remove(struct pfi_dynaddr *);
1820 void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
1821 void		 pfi_update_status(const char *, struct pf_status *);
1822 void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
1823 int		 pfi_set_flags(const char *, int);
1824 int		 pfi_clear_flags(const char *, int);
1825 
1826 int		 pf_match_tag(struct mbuf *, struct pf_rule *, int *, int);
1827 int		 pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
1828 int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *,
1829 		    sa_family_t);
1830 void		 pf_qid2qname(u_int32_t, char *);
1831 
1832 VNET_DECLARE(struct pf_kstatus, pf_status);
1833 #define	V_pf_status	VNET(pf_status)
1834 
1835 struct pf_limit {
1836 	uma_zone_t	zone;
1837 	u_int		limit;
1838 };
1839 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
1840 #define	V_pf_limits VNET(pf_limits)
1841 
1842 #endif /* _KERNEL */
1843 
1844 #ifdef _KERNEL
1845 VNET_DECLARE(struct pf_anchor_global,		 pf_anchors);
1846 #define	V_pf_anchors				 VNET(pf_anchors)
1847 VNET_DECLARE(struct pf_anchor,			 pf_main_anchor);
1848 #define	V_pf_main_anchor			 VNET(pf_main_anchor)
1849 #define pf_main_ruleset	V_pf_main_anchor.ruleset
1850 #endif
1851 
1852 /* these ruleset functions can be linked into userland programs (pfctl) */
1853 int			 pf_get_ruleset_number(u_int8_t);
1854 void			 pf_init_ruleset(struct pf_ruleset *);
1855 int			 pf_anchor_setup(struct pf_rule *,
1856 			    const struct pf_ruleset *, const char *);
1857 int			 pf_anchor_copyout(const struct pf_ruleset *,
1858 			    const struct pf_rule *, struct pfioc_rule *);
1859 void			 pf_anchor_remove(struct pf_rule *);
1860 void			 pf_remove_if_empty_ruleset(struct pf_ruleset *);
1861 struct pf_ruleset	*pf_find_ruleset(const char *);
1862 struct pf_ruleset	*pf_find_or_create_ruleset(const char *);
1863 void			 pf_rs_initialize(void);
1864 
1865 /* The fingerprint functions can be linked into userland programs (tcpdump) */
1866 int	pf_osfp_add(struct pf_osfp_ioctl *);
1867 #ifdef _KERNEL
1868 struct pf_osfp_enlist *
1869 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
1870 	    const struct tcphdr *);
1871 #endif /* _KERNEL */
1872 void	pf_osfp_flush(void);
1873 int	pf_osfp_get(struct pf_osfp_ioctl *);
1874 int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
1875 
1876 #ifdef _KERNEL
1877 void			 pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
1878 
1879 void			 pf_step_into_anchor(struct pf_anchor_stackframe *, int *,
1880 			    struct pf_ruleset **, int, struct pf_rule **,
1881 			    struct pf_rule **, int *);
1882 int			 pf_step_out_of_anchor(struct pf_anchor_stackframe *, int *,
1883 			    struct pf_ruleset **, int, struct pf_rule **,
1884 			    struct pf_rule **, int *);
1885 
1886 int			 pf_map_addr(u_int8_t, struct pf_rule *,
1887 			    struct pf_addr *, struct pf_addr *,
1888 			    struct pf_addr *, struct pf_src_node **);
1889 struct pf_rule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
1890 			    int, int, struct pfi_kif *, struct pf_src_node **,
1891 			    struct pf_state_key **, struct pf_state_key **,
1892 			    struct pf_addr *, struct pf_addr *,
1893 			    uint16_t, uint16_t, struct pf_anchor_stackframe *);
1894 
1895 struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
1896 			    struct pf_addr *, u_int16_t, u_int16_t);
1897 struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
1898 #endif /* _KERNEL */
1899 
1900 #endif /* _NET_PFVAR_H_ */
1901