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