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