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