xref: /freebsd/sys/net/pfvar.h (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
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/nv.h>
44 #include <sys/refcount.h>
45 #include <sys/sdt.h>
46 #include <sys/sysctl.h>
47 #include <sys/smp.h>
48 #include <sys/lock.h>
49 #include <sys/rmlock.h>
50 #include <sys/tree.h>
51 #include <sys/seqc.h>
52 #include <vm/uma.h>
53 
54 #include <net/radix.h>
55 #include <netinet/in.h>
56 #ifdef _KERNEL
57 #include <netinet/ip.h>
58 #include <netinet/tcp.h>
59 #include <netinet/udp.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/icmp6.h>
62 #endif
63 
64 #include <netpfil/pf/pf.h>
65 #include <netpfil/pf/pf_altq.h>
66 #include <netpfil/pf/pf_mtag.h>
67 
68 #ifdef _KERNEL
69 
70 #if defined(__arm__)
71 #define PF_WANT_32_TO_64_COUNTER
72 #endif
73 
74 /*
75  * A hybrid of 32-bit and 64-bit counters which can be used on platforms where
76  * counter(9) is very expensive.
77  *
78  * As 32-bit counters are expected to overflow, a periodic job sums them up to
79  * a saved 64-bit state. Fetching the value still walks all CPUs to get the most
80  * current snapshot.
81  */
82 #ifdef PF_WANT_32_TO_64_COUNTER
83 struct pf_counter_u64_pcpu {
84 	u_int32_t current;
85 	u_int32_t snapshot;
86 };
87 
88 struct pf_counter_u64 {
89 	struct pf_counter_u64_pcpu *pfcu64_pcpu;
90 	u_int64_t pfcu64_value;
91 	seqc_t	pfcu64_seqc;
92 };
93 
94 static inline int
95 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags)
96 {
97 
98 	pfcu64->pfcu64_value = 0;
99 	pfcu64->pfcu64_seqc = 0;
100 	pfcu64->pfcu64_pcpu = uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO);
101 	if (__predict_false(pfcu64->pfcu64_pcpu == NULL))
102 		return (ENOMEM);
103 	return (0);
104 }
105 
106 static inline void
107 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64)
108 {
109 
110 	uma_zfree_pcpu(pcpu_zone_8, pfcu64->pfcu64_pcpu);
111 }
112 
113 static inline void
114 pf_counter_u64_critical_enter(void)
115 {
116 
117 	critical_enter();
118 }
119 
120 static inline void
121 pf_counter_u64_critical_exit(void)
122 {
123 
124 	critical_exit();
125 }
126 
127 static inline void
128 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
129 {
130 	struct pf_counter_u64_pcpu *pcpu;
131 	u_int32_t val;
132 
133 	MPASS(curthread->td_critnest > 0);
134 	pcpu = zpcpu_get(pfcu64->pfcu64_pcpu);
135 	val = atomic_load_int(&pcpu->current);
136 	atomic_store_int(&pcpu->current, val + n);
137 }
138 
139 static inline void
140 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n)
141 {
142 
143 	critical_enter();
144 	pf_counter_u64_add_protected(pfcu64, n);
145 	critical_exit();
146 }
147 
148 static inline u_int64_t
149 pf_counter_u64_periodic(struct pf_counter_u64 *pfcu64)
150 {
151 	struct pf_counter_u64_pcpu *pcpu;
152 	u_int64_t sum;
153 	u_int32_t val;
154 	int cpu;
155 
156 	MPASS(curthread->td_critnest > 0);
157 	seqc_write_begin(&pfcu64->pfcu64_seqc);
158 	sum = pfcu64->pfcu64_value;
159 	CPU_FOREACH(cpu) {
160 		pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
161 		val = atomic_load_int(&pcpu->current);
162 		sum += (uint32_t)(val - pcpu->snapshot);
163 		pcpu->snapshot = val;
164 	}
165 	pfcu64->pfcu64_value = sum;
166 	seqc_write_end(&pfcu64->pfcu64_seqc);
167 	return (sum);
168 }
169 
170 static inline u_int64_t
171 pf_counter_u64_fetch(struct pf_counter_u64 *pfcu64)
172 {
173 	struct pf_counter_u64_pcpu *pcpu;
174 	u_int64_t sum;
175 	seqc_t seqc;
176 	int cpu;
177 
178 	for (;;) {
179 		seqc = seqc_read(&pfcu64->pfcu64_seqc);
180 		sum = 0;
181 		CPU_FOREACH(cpu) {
182 			pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
183 			sum += (uint32_t)(atomic_load_int(&pcpu->current) -pcpu->snapshot);
184 		}
185 		sum += pfcu64->pfcu64_value;
186 		if (seqc_consistent(&pfcu64->pfcu64_seqc, seqc))
187 			break;
188 	}
189 	return (sum);
190 }
191 
192 static inline void
193 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64)
194 {
195 	struct pf_counter_u64_pcpu *pcpu;
196 	int cpu;
197 
198 	MPASS(curthread->td_critnest > 0);
199 	seqc_write_begin(&pfcu64->pfcu64_seqc);
200 	CPU_FOREACH(cpu) {
201 		pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
202 		pcpu->snapshot = atomic_load_int(&pcpu->current);
203 	}
204 	pfcu64->pfcu64_value = 0;
205 	seqc_write_end(&pfcu64->pfcu64_seqc);
206 }
207 
208 static inline void
209 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64)
210 {
211 
212 	critical_enter();
213 	pf_counter_u64_zero_protected(pfcu64);
214 	critical_exit();
215 }
216 #else
217 struct pf_counter_u64 {
218 	counter_u64_t counter;
219 };
220 
221 static inline int
222 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags)
223 {
224 
225 	pfcu64->counter = counter_u64_alloc(flags);
226 	if (__predict_false(pfcu64->counter == NULL))
227 		return (ENOMEM);
228 	return (0);
229 }
230 
231 static inline void
232 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64)
233 {
234 
235 	counter_u64_free(pfcu64->counter);
236 }
237 
238 static inline void
239 pf_counter_u64_critical_enter(void)
240 {
241 
242 }
243 
244 static inline void
245 pf_counter_u64_critical_exit(void)
246 {
247 
248 }
249 
250 static inline void
251 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
252 {
253 
254 	counter_u64_add(pfcu64->counter, n);
255 }
256 
257 static inline void
258 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n)
259 {
260 
261 	pf_counter_u64_add_protected(pfcu64, n);
262 }
263 
264 static inline u_int64_t
265 pf_counter_u64_fetch(struct pf_counter_u64 *pfcu64)
266 {
267 
268 	return (counter_u64_fetch(pfcu64->counter));
269 }
270 
271 static inline void
272 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64)
273 {
274 
275 	counter_u64_zero(pfcu64->counter);
276 }
277 
278 static inline void
279 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64)
280 {
281 
282 	pf_counter_u64_zero_protected(pfcu64);
283 }
284 #endif
285 
286 SYSCTL_DECL(_net_pf);
287 MALLOC_DECLARE(M_PFHASH);
288 
289 SDT_PROVIDER_DECLARE(pf);
290 
291 struct pfi_dynaddr {
292 	TAILQ_ENTRY(pfi_dynaddr)	 entry;
293 	struct pf_addr			 pfid_addr4;
294 	struct pf_addr			 pfid_mask4;
295 	struct pf_addr			 pfid_addr6;
296 	struct pf_addr			 pfid_mask6;
297 	struct pfr_ktable		*pfid_kt;
298 	struct pfi_kkif			*pfid_kif;
299 	int				 pfid_net;	/* mask or 128 */
300 	int				 pfid_acnt4;	/* address count IPv4 */
301 	int				 pfid_acnt6;	/* address count IPv6 */
302 	sa_family_t			 pfid_af;	/* rule af */
303 	u_int8_t			 pfid_iflags;	/* PFI_AFLAG_* */
304 };
305 
306 /*
307  * Address manipulation macros
308  */
309 #define	HTONL(x)	(x) = htonl((__uint32_t)(x))
310 #define	HTONS(x)	(x) = htons((__uint16_t)(x))
311 #define	NTOHL(x)	(x) = ntohl((__uint32_t)(x))
312 #define	NTOHS(x)	(x) = ntohs((__uint16_t)(x))
313 
314 #define	PF_NAME		"pf"
315 
316 #define	PF_HASHROW_ASSERT(h)	mtx_assert(&(h)->lock, MA_OWNED)
317 #define	PF_HASHROW_LOCK(h)	mtx_lock(&(h)->lock)
318 #define	PF_HASHROW_UNLOCK(h)	mtx_unlock(&(h)->lock)
319 
320 #ifdef INVARIANTS
321 #define	PF_STATE_LOCK(s)						\
322 	do {								\
323 		struct pf_kstate *_s = (s);				\
324 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
325 		MPASS(_s->lock == &_ih->lock);				\
326 		mtx_lock(_s->lock);					\
327 	} while (0)
328 #define	PF_STATE_UNLOCK(s)						\
329 	do {								\
330 		struct pf_kstate *_s = (s);				\
331 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
332 		MPASS(_s->lock == &_ih->lock);				\
333 		mtx_unlock(_s->lock);					\
334 	} while (0)
335 #else
336 #define	PF_STATE_LOCK(s)	mtx_lock(s->lock)
337 #define	PF_STATE_UNLOCK(s)	mtx_unlock(s->lock)
338 #endif
339 
340 #ifdef INVARIANTS
341 #define	PF_STATE_LOCK_ASSERT(s)						\
342 	do {								\
343 		struct pf_kstate *_s = (s);				\
344 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
345 		MPASS(_s->lock == &_ih->lock);				\
346 		PF_HASHROW_ASSERT(_ih);					\
347 	} while (0)
348 #else /* !INVARIANTS */
349 #define	PF_STATE_LOCK_ASSERT(s)		do {} while (0)
350 #endif /* INVARIANTS */
351 
352 extern struct mtx_padalign pf_unlnkdrules_mtx;
353 #define	PF_UNLNKDRULES_LOCK()	mtx_lock(&pf_unlnkdrules_mtx)
354 #define	PF_UNLNKDRULES_UNLOCK()	mtx_unlock(&pf_unlnkdrules_mtx)
355 
356 extern struct rmlock pf_rules_lock;
357 #define	PF_RULES_RLOCK_TRACKER	struct rm_priotracker _pf_rules_tracker
358 #define	PF_RULES_RLOCK()	rm_rlock(&pf_rules_lock, &_pf_rules_tracker)
359 #define	PF_RULES_RUNLOCK()	rm_runlock(&pf_rules_lock, &_pf_rules_tracker)
360 #define	PF_RULES_WLOCK()	rm_wlock(&pf_rules_lock)
361 #define	PF_RULES_WUNLOCK()	rm_wunlock(&pf_rules_lock)
362 #define	PF_RULES_WOWNED()	rm_wowned(&pf_rules_lock)
363 #define	PF_RULES_ASSERT()	rm_assert(&pf_rules_lock, RA_LOCKED)
364 #define	PF_RULES_RASSERT()	rm_assert(&pf_rules_lock, RA_RLOCKED)
365 #define	PF_RULES_WASSERT()	rm_assert(&pf_rules_lock, RA_WLOCKED)
366 
367 extern struct mtx_padalign pf_table_stats_lock;
368 #define	PF_TABLE_STATS_LOCK()	mtx_lock(&pf_table_stats_lock)
369 #define	PF_TABLE_STATS_UNLOCK()	mtx_unlock(&pf_table_stats_lock)
370 #define	PF_TABLE_STATS_OWNED()	mtx_owned(&pf_table_stats_lock)
371 #define	PF_TABLE_STATS_ASSERT()	mtx_assert(&pf_rules_lock, MA_OWNED)
372 
373 extern struct sx pf_end_lock;
374 
375 #define	PF_MODVER	1
376 #define	PFLOG_MODVER	1
377 #define	PFSYNC_MODVER	1
378 
379 #define	PFLOG_MINVER	1
380 #define	PFLOG_PREFVER	PFLOG_MODVER
381 #define	PFLOG_MAXVER	1
382 #define	PFSYNC_MINVER	1
383 #define	PFSYNC_PREFVER	PFSYNC_MODVER
384 #define	PFSYNC_MAXVER	1
385 
386 #ifdef INET
387 #ifndef INET6
388 #define	PF_INET_ONLY
389 #endif /* ! INET6 */
390 #endif /* INET */
391 
392 #ifdef INET6
393 #ifndef INET
394 #define	PF_INET6_ONLY
395 #endif /* ! INET */
396 #endif /* INET6 */
397 
398 #ifdef INET
399 #ifdef INET6
400 #define	PF_INET_INET6
401 #endif /* INET6 */
402 #endif /* INET */
403 
404 #else
405 
406 #define	PF_INET_INET6
407 
408 #endif /* _KERNEL */
409 
410 /* Both IPv4 and IPv6 */
411 #ifdef PF_INET_INET6
412 
413 #define PF_AEQ(a, b, c) \
414 	((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
415 	(c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \
416 	(a)->addr32[2] == (b)->addr32[2] && \
417 	(a)->addr32[1] == (b)->addr32[1] && \
418 	(a)->addr32[0] == (b)->addr32[0])) \
419 
420 #define PF_ANEQ(a, b, c) \
421 	((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
422 	(c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \
423 	(a)->addr32[1] != (b)->addr32[1] || \
424 	(a)->addr32[2] != (b)->addr32[2] || \
425 	(a)->addr32[3] != (b)->addr32[3]))) \
426 
427 #define PF_AZERO(a, c) \
428 	((c == AF_INET && !(a)->addr32[0]) || \
429 	(c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \
430 	!(a)->addr32[2] && !(a)->addr32[3] )) \
431 
432 #define PF_MATCHA(n, a, m, b, f) \
433 	pf_match_addr(n, a, m, b, f)
434 
435 #define PF_ACPY(a, b, f) \
436 	pf_addrcpy(a, b, f)
437 
438 #define PF_AINC(a, f) \
439 	pf_addr_inc(a, f)
440 
441 #define PF_POOLMASK(a, b, c, d, f) \
442 	pf_poolmask(a, b, c, d, f)
443 
444 #else
445 
446 /* Just IPv6 */
447 
448 #ifdef PF_INET6_ONLY
449 
450 #define PF_AEQ(a, b, c) \
451 	((a)->addr32[3] == (b)->addr32[3] && \
452 	(a)->addr32[2] == (b)->addr32[2] && \
453 	(a)->addr32[1] == (b)->addr32[1] && \
454 	(a)->addr32[0] == (b)->addr32[0]) \
455 
456 #define PF_ANEQ(a, b, c) \
457 	((a)->addr32[3] != (b)->addr32[3] || \
458 	(a)->addr32[2] != (b)->addr32[2] || \
459 	(a)->addr32[1] != (b)->addr32[1] || \
460 	(a)->addr32[0] != (b)->addr32[0]) \
461 
462 #define PF_AZERO(a, c) \
463 	(!(a)->addr32[0] && \
464 	!(a)->addr32[1] && \
465 	!(a)->addr32[2] && \
466 	!(a)->addr32[3] ) \
467 
468 #define PF_MATCHA(n, a, m, b, f) \
469 	pf_match_addr(n, a, m, b, f)
470 
471 #define PF_ACPY(a, b, f) \
472 	pf_addrcpy(a, b, f)
473 
474 #define PF_AINC(a, f) \
475 	pf_addr_inc(a, f)
476 
477 #define PF_POOLMASK(a, b, c, d, f) \
478 	pf_poolmask(a, b, c, d, f)
479 
480 #else
481 
482 /* Just IPv4 */
483 #ifdef PF_INET_ONLY
484 
485 #define PF_AEQ(a, b, c) \
486 	((a)->addr32[0] == (b)->addr32[0])
487 
488 #define PF_ANEQ(a, b, c) \
489 	((a)->addr32[0] != (b)->addr32[0])
490 
491 #define PF_AZERO(a, c) \
492 	(!(a)->addr32[0])
493 
494 #define PF_MATCHA(n, a, m, b, f) \
495 	pf_match_addr(n, a, m, b, f)
496 
497 #define PF_ACPY(a, b, f) \
498 	(a)->v4.s_addr = (b)->v4.s_addr
499 
500 #define PF_AINC(a, f) \
501 	do { \
502 		(a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
503 	} while (0)
504 
505 #define PF_POOLMASK(a, b, c, d, f) \
506 	do { \
507 		(a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
508 		(((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
509 	} while (0)
510 
511 #endif /* PF_INET_ONLY */
512 #endif /* PF_INET6_ONLY */
513 #endif /* PF_INET_INET6 */
514 
515 /*
516  * XXX callers not FIB-aware in our version of pf yet.
517  * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio.
518  */
519 #define	PF_MISMATCHAW(aw, x, af, neg, ifp, rtid)			\
520 	(								\
521 		(((aw)->type == PF_ADDR_NOROUTE &&			\
522 		    pf_routable((x), (af), NULL, (rtid))) ||		\
523 		(((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&	\
524 		    pf_routable((x), (af), (ifp), (rtid))) ||		\
525 		((aw)->type == PF_ADDR_TABLE &&				\
526 		    !pfr_match_addr((aw)->p.tbl, (x), (af))) ||		\
527 		((aw)->type == PF_ADDR_DYNIFTL &&			\
528 		    !pfi_match_addr((aw)->p.dyn, (x), (af))) ||		\
529 		((aw)->type == PF_ADDR_RANGE &&				\
530 		    !pf_match_addr_range(&(aw)->v.a.addr,		\
531 		    &(aw)->v.a.mask, (x), (af))) ||			\
532 		((aw)->type == PF_ADDR_ADDRMASK &&			\
533 		    !PF_AZERO(&(aw)->v.a.mask, (af)) &&			\
534 		    !PF_MATCHA(0, &(aw)->v.a.addr,			\
535 		    &(aw)->v.a.mask, (x), (af))))) !=			\
536 		(neg)							\
537 	)
538 
539 #define PF_ALGNMNT(off) (((off) % 2) == 0)
540 
541 #ifdef _KERNEL
542 
543 struct pf_kpooladdr {
544 	struct pf_addr_wrap		 addr;
545 	TAILQ_ENTRY(pf_kpooladdr)	 entries;
546 	char				 ifname[IFNAMSIZ];
547 	struct pfi_kkif			*kif;
548 };
549 
550 TAILQ_HEAD(pf_kpalist, pf_kpooladdr);
551 
552 struct pf_kpool {
553 	struct pf_kpalist	 list;
554 	struct pf_kpooladdr	*cur;
555 	struct pf_poolhashkey	 key;
556 	struct pf_addr		 counter;
557 	struct pf_mape_portset	 mape;
558 	int			 tblidx;
559 	u_int16_t		 proxy_port[2];
560 	u_int8_t		 opts;
561 };
562 
563 struct pf_rule_actions {
564 	uint16_t	 qid;
565 	uint16_t	 pqid;
566 	uint16_t	 dnpipe;
567 	uint16_t	 dnrpipe;	/* Reverse direction pipe */
568 	uint32_t	 flags;
569 };
570 
571 union pf_krule_ptr {
572 	struct pf_krule		*ptr;
573 	u_int32_t		 nr;
574 };
575 
576 struct pf_krule {
577 	struct pf_rule_addr	 src;
578 	struct pf_rule_addr	 dst;
579 	union pf_krule_ptr	 skip[PF_SKIP_COUNT];
580 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
581 	uint32_t		 ridentifier;
582 	char			 ifname[IFNAMSIZ];
583 	char			 qname[PF_QNAME_SIZE];
584 	char			 pqname[PF_QNAME_SIZE];
585 	char			 tagname[PF_TAG_NAME_SIZE];
586 	char			 match_tagname[PF_TAG_NAME_SIZE];
587 
588 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
589 
590 	TAILQ_ENTRY(pf_krule)	 entries;
591 	struct pf_kpool		 rpool;
592 
593 	struct pf_counter_u64	 evaluations;
594 	struct pf_counter_u64	 packets[2];
595 	struct pf_counter_u64	 bytes[2];
596 
597 	struct pfi_kkif		*kif;
598 	struct pf_kanchor	*anchor;
599 	struct pfr_ktable	*overload_tbl;
600 
601 	pf_osfp_t		 os_fingerprint;
602 
603 	int			 rtableid;
604 	u_int32_t		 timeout[PFTM_MAX];
605 	u_int32_t		 max_states;
606 	u_int32_t		 max_src_nodes;
607 	u_int32_t		 max_src_states;
608 	u_int32_t		 max_src_conn;
609 	struct {
610 		u_int32_t		limit;
611 		u_int32_t		seconds;
612 	}			 max_src_conn_rate;
613 	u_int16_t		 qid;
614 	u_int16_t		 pqid;
615 	u_int16_t		 dnpipe;
616 	u_int16_t		 dnrpipe;
617 	u_int32_t		 free_flags;
618 	u_int32_t		 nr;
619 	u_int32_t		 prob;
620 	uid_t			 cuid;
621 	pid_t			 cpid;
622 
623 	counter_u64_t		 states_cur;
624 	counter_u64_t		 states_tot;
625 	counter_u64_t		 src_nodes;
626 
627 	u_int16_t		 return_icmp;
628 	u_int16_t		 return_icmp6;
629 	u_int16_t		 max_mss;
630 	u_int16_t		 tag;
631 	u_int16_t		 match_tag;
632 	u_int16_t		 scrub_flags;
633 
634 	struct pf_rule_uid	 uid;
635 	struct pf_rule_gid	 gid;
636 
637 	u_int32_t		 rule_flag;
638 	uint32_t		 rule_ref;
639 	u_int8_t		 action;
640 	u_int8_t		 direction;
641 	u_int8_t		 log;
642 	u_int8_t		 logif;
643 	u_int8_t		 quick;
644 	u_int8_t		 ifnot;
645 	u_int8_t		 match_tag_not;
646 	u_int8_t		 natpass;
647 
648 	u_int8_t		 keep_state;
649 	sa_family_t		 af;
650 	u_int8_t		 proto;
651 	u_int8_t		 type;
652 	u_int8_t		 code;
653 	u_int8_t		 flags;
654 	u_int8_t		 flagset;
655 	u_int8_t		 min_ttl;
656 	u_int8_t		 allow_opts;
657 	u_int8_t		 rt;
658 	u_int8_t		 return_ttl;
659 	u_int8_t		 tos;
660 	u_int8_t		 set_tos;
661 	u_int8_t		 anchor_relative;
662 	u_int8_t		 anchor_wildcard;
663 
664 	u_int8_t		 flush;
665 	u_int8_t		 prio;
666 	u_int8_t		 set_prio[2];
667 
668 	struct {
669 		struct pf_addr		addr;
670 		u_int16_t		port;
671 	}			divert;
672 
673 #ifdef PF_WANT_32_TO_64_COUNTER
674 	LIST_ENTRY(pf_krule)	 allrulelist;
675 	bool			 allrulelinked;
676 #endif
677 };
678 
679 struct pf_ksrc_node {
680 	LIST_ENTRY(pf_ksrc_node) entry;
681 	struct pf_addr	 addr;
682 	struct pf_addr	 raddr;
683 	union pf_krule_ptr rule;
684 	struct pfi_kkif	*kif;
685 	counter_u64_t	 bytes[2];
686 	counter_u64_t	 packets[2];
687 	u_int32_t	 states;
688 	u_int32_t	 conn;
689 	struct pf_threshold	conn_rate;
690 	u_int32_t	 creation;
691 	u_int32_t	 expire;
692 	sa_family_t	 af;
693 	u_int8_t	 ruletype;
694 };
695 #endif
696 
697 struct pf_state_scrub {
698 	struct timeval	pfss_last;	/* time received last packet	*/
699 	u_int32_t	pfss_tsecr;	/* last echoed timestamp	*/
700 	u_int32_t	pfss_tsval;	/* largest timestamp		*/
701 	u_int32_t	pfss_tsval0;	/* original timestamp		*/
702 	u_int16_t	pfss_flags;
703 #define PFSS_TIMESTAMP	0x0001		/* modulate timestamp		*/
704 #define PFSS_PAWS	0x0010		/* stricter PAWS checks		*/
705 #define PFSS_PAWS_IDLED	0x0020		/* was idle too long.  no PAWS	*/
706 #define PFSS_DATA_TS	0x0040		/* timestamp on data packets	*/
707 #define PFSS_DATA_NOTS	0x0080		/* no timestamp on data packets	*/
708 	u_int8_t	pfss_ttl;	/* stashed TTL			*/
709 	u_int8_t	pad;
710 	u_int32_t	pfss_ts_mod;	/* timestamp modulation		*/
711 };
712 
713 struct pf_state_host {
714 	struct pf_addr	addr;
715 	u_int16_t	port;
716 	u_int16_t	pad;
717 };
718 
719 struct pf_state_peer {
720 	struct pf_state_scrub	*scrub;	/* state is scrubbed		*/
721 	u_int32_t	seqlo;		/* Max sequence number sent	*/
722 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
723 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
724 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
725 	u_int16_t	mss;		/* Maximum segment size option	*/
726 	u_int8_t	state;		/* active state level		*/
727 	u_int8_t	wscale;		/* window scaling factor	*/
728 	u_int8_t	tcp_est;	/* Did we reach TCPS_ESTABLISHED */
729 	u_int8_t	pad[1];
730 };
731 
732 /* Keep synced with struct pf_state_key. */
733 struct pf_state_key_cmp {
734 	struct pf_addr	 addr[2];
735 	u_int16_t	 port[2];
736 	sa_family_t	 af;
737 	u_int8_t	 proto;
738 	u_int8_t	 pad[2];
739 };
740 
741 struct pf_state_key {
742 	struct pf_addr	 addr[2];
743 	u_int16_t	 port[2];
744 	sa_family_t	 af;
745 	u_int8_t	 proto;
746 	u_int8_t	 pad[2];
747 
748 	LIST_ENTRY(pf_state_key) entry;
749 	TAILQ_HEAD(, pf_kstate)	 states[2];
750 };
751 
752 /* Keep synced with struct pf_kstate. */
753 struct pf_state_cmp {
754 	u_int64_t		 id;
755 	u_int32_t		 creatorid;
756 	u_int8_t		 direction;
757 	u_int8_t		 pad[3];
758 };
759 
760 #define	PFSTATE_ALLOWOPTS	0x01
761 #define	PFSTATE_SLOPPY		0x02
762 /*  was	PFSTATE_PFLOW		0x04 */
763 #define	PFSTATE_NOSYNC		0x08
764 #define	PFSTATE_ACK		0x10
765 #define	PFRULE_DN_IS_PIPE	0x40
766 #define	PFRULE_DN_IS_QUEUE	0x80
767 #define	PFSTATE_SETPRIO		0x0200
768 #define	PFSTATE_SETMASK   (PFSTATE_SETPRIO)
769 
770 struct pf_state_scrub_export {
771 	uint16_t	pfss_flags;
772 	uint8_t		pfss_ttl;	/* stashed TTL		*/
773 #define PF_SCRUB_FLAG_VALID		0x01
774 	uint8_t		scrub_flag;
775 	uint32_t	pfss_ts_mod;	/* timestamp modulation	*/
776 };
777 
778 struct pf_state_key_export {
779 	struct pf_addr	 addr[2];
780 	uint16_t	 port[2];
781 };
782 
783 struct pf_state_peer_export {
784 	struct pf_state_scrub_export	scrub;	/* state is scrubbed	*/
785 	uint32_t	seqlo;		/* Max sequence number sent	*/
786 	uint32_t	seqhi;		/* Max the other end ACKd + win	*/
787 	uint32_t	seqdiff;	/* Sequence number modulator	*/
788 	uint16_t	max_win;	/* largest window (pre scaling)	*/
789 	uint16_t	mss;		/* Maximum segment size option	*/
790 	uint8_t		state;		/* active state level		*/
791 	uint8_t		wscale;		/* window scaling factor	*/
792 	uint8_t		dummy[6];
793 };
794 _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect");
795 
796 struct pf_state_export {
797 	uint64_t	 version;
798 #define	PF_STATE_VERSION	20210706
799 	uint64_t	 id;
800 	char		 ifname[IFNAMSIZ];
801 	char		 orig_ifname[IFNAMSIZ];
802 	struct pf_state_key_export	 key[2];
803 	struct pf_state_peer_export	 src;
804 	struct pf_state_peer_export	 dst;
805 	struct pf_addr	 rt_addr;
806 	uint32_t	 rule;
807 	uint32_t	 anchor;
808 	uint32_t	 nat_rule;
809 	uint32_t	 creation;
810 	uint32_t	 expire;
811 	uint32_t	 spare0;
812 	uint64_t	 packets[2];
813 	uint64_t	 bytes[2];
814 	uint32_t	 creatorid;
815 	uint32_t	 spare1;
816 	sa_family_t	 af;
817 	uint8_t		 proto;
818 	uint8_t		 direction;
819 	uint8_t		 log;
820 	uint8_t		 state_flags;
821 	uint8_t		 timeout;
822 	uint8_t		 sync_flags;
823 	uint8_t		 updates;
824 
825 	uint8_t		 spare[112];
826 };
827 _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect");
828 
829 #ifdef _KERNEL
830 struct pf_kstate {
831 	/*
832 	 * Area shared with pf_state_cmp
833 	 */
834 	u_int64_t		 id;
835 	u_int32_t		 creatorid;
836 	u_int8_t		 direction;
837 	u_int8_t		 pad[3];
838 	/*
839 	 * end of the area
840 	 */
841 
842 	u_int8_t		 state_flags;
843 	u_int8_t		 timeout;
844 	u_int8_t		 sync_state; /* PFSYNC_S_x */
845 	u_int8_t		 sync_updates; /* XXX */
846 	u_int			 refs;
847 	struct mtx		*lock;
848 	TAILQ_ENTRY(pf_kstate)	 sync_list;
849 	TAILQ_ENTRY(pf_kstate)	 key_list[2];
850 	LIST_ENTRY(pf_kstate)	 entry;
851 	struct pf_state_peer	 src;
852 	struct pf_state_peer	 dst;
853 	union pf_krule_ptr	 rule;
854 	union pf_krule_ptr	 anchor;
855 	union pf_krule_ptr	 nat_rule;
856 	struct pf_addr		 rt_addr;
857 	struct pf_state_key	*key[2];	/* addresses stack and wire  */
858 	struct pfi_kkif		*kif;
859 	struct pfi_kkif		*orig_kif;	/* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */
860 	struct pfi_kkif		*rt_kif;
861 	struct pf_ksrc_node	*src_node;
862 	struct pf_ksrc_node	*nat_src_node;
863 	u_int64_t		 packets[2];
864 	u_int64_t		 bytes[2];
865 	u_int32_t		 creation;
866 	u_int32_t	 	 expire;
867 	u_int32_t		 pfsync_time;
868 	u_int16_t                qid;
869 	u_int16_t                pqid;
870 	u_int16_t		 dnpipe;
871 	u_int16_t		 dnrpipe;
872 	u_int16_t		 tag;
873 	u_int8_t		 log;
874 };
875 
876 /*
877  * Size <= fits 13 objects per page on LP64. Try to not grow the struct beyond that.
878  */
879 _Static_assert(sizeof(struct pf_kstate) <= 312, "pf_kstate size crosses 312 bytes");
880 #endif
881 
882 /*
883  * Unified state structures for pulling states out of the kernel
884  * used by pfsync(4) and the pf(4) ioctl.
885  */
886 struct pfsync_state_scrub {
887 	u_int16_t	pfss_flags;
888 	u_int8_t	pfss_ttl;	/* stashed TTL		*/
889 #define PFSYNC_SCRUB_FLAG_VALID		0x01
890 	u_int8_t	scrub_flag;
891 	u_int32_t	pfss_ts_mod;	/* timestamp modulation	*/
892 } __packed;
893 
894 struct pfsync_state_peer {
895 	struct pfsync_state_scrub scrub;	/* state is scrubbed	*/
896 	u_int32_t	seqlo;		/* Max sequence number sent	*/
897 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
898 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
899 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
900 	u_int16_t	mss;		/* Maximum segment size option	*/
901 	u_int8_t	state;		/* active state level		*/
902 	u_int8_t	wscale;		/* window scaling factor	*/
903 	u_int8_t	pad[6];
904 } __packed;
905 
906 struct pfsync_state_key {
907 	struct pf_addr	 addr[2];
908 	u_int16_t	 port[2];
909 };
910 
911 struct pfsync_state {
912 	u_int64_t	 id;
913 	char		 ifname[IFNAMSIZ];
914 	struct pfsync_state_key	key[2];
915 	struct pfsync_state_peer src;
916 	struct pfsync_state_peer dst;
917 	struct pf_addr	 rt_addr;
918 	u_int32_t	 rule;
919 	u_int32_t	 anchor;
920 	u_int32_t	 nat_rule;
921 	u_int32_t	 creation;
922 	u_int32_t	 expire;
923 	u_int32_t	 packets[2][2];
924 	u_int32_t	 bytes[2][2];
925 	u_int32_t	 creatorid;
926 	sa_family_t	 af;
927 	u_int8_t	 proto;
928 	u_int8_t	 direction;
929 	u_int8_t	 __spare[2];
930 	u_int8_t	 log;
931 	u_int8_t	 state_flags;
932 	u_int8_t	 timeout;
933 	u_int8_t	 sync_flags;
934 	u_int8_t	 updates;
935 } __packed;
936 
937 #ifdef _KERNEL
938 /* pfsync */
939 typedef int		pfsync_state_import_t(struct pfsync_state *, u_int8_t);
940 typedef	void		pfsync_insert_state_t(struct pf_kstate *);
941 typedef	void		pfsync_update_state_t(struct pf_kstate *);
942 typedef	void		pfsync_delete_state_t(struct pf_kstate *);
943 typedef void		pfsync_clear_states_t(u_int32_t, const char *);
944 typedef int		pfsync_defer_t(struct pf_kstate *, struct mbuf *);
945 typedef void		pfsync_detach_ifnet_t(struct ifnet *);
946 
947 VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr);
948 #define V_pfsync_state_import_ptr	VNET(pfsync_state_import_ptr)
949 VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
950 #define V_pfsync_insert_state_ptr	VNET(pfsync_insert_state_ptr)
951 VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr);
952 #define V_pfsync_update_state_ptr	VNET(pfsync_update_state_ptr)
953 VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
954 #define V_pfsync_delete_state_ptr	VNET(pfsync_delete_state_ptr)
955 VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
956 #define V_pfsync_clear_states_ptr	VNET(pfsync_clear_states_ptr)
957 VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr);
958 #define V_pfsync_defer_ptr		VNET(pfsync_defer_ptr)
959 extern pfsync_detach_ifnet_t	*pfsync_detach_ifnet_ptr;
960 
961 void			pfsync_state_export(struct pfsync_state *,
962 			    struct pf_kstate *);
963 void			pf_state_export(struct pf_state_export *,
964 			    struct pf_kstate *);
965 
966 /* pflog */
967 struct pf_kruleset;
968 struct pf_pdesc;
969 typedef int pflog_packet_t(struct pfi_kkif *, struct mbuf *, sa_family_t,
970     u_int8_t, u_int8_t, struct pf_krule *, struct pf_krule *,
971     struct pf_kruleset *, struct pf_pdesc *, int);
972 extern pflog_packet_t		*pflog_packet_ptr;
973 
974 #endif /* _KERNEL */
975 
976 #define	PFSYNC_FLAG_SRCNODE	0x04
977 #define	PFSYNC_FLAG_NATSRCNODE	0x08
978 
979 /* for copies to/from network byte order */
980 /* ioctl interface also uses network byte order */
981 #define pf_state_peer_hton(s,d) do {		\
982 	(d)->seqlo = htonl((s)->seqlo);		\
983 	(d)->seqhi = htonl((s)->seqhi);		\
984 	(d)->seqdiff = htonl((s)->seqdiff);	\
985 	(d)->max_win = htons((s)->max_win);	\
986 	(d)->mss = htons((s)->mss);		\
987 	(d)->state = (s)->state;		\
988 	(d)->wscale = (s)->wscale;		\
989 	if ((s)->scrub) {						\
990 		(d)->scrub.pfss_flags = 				\
991 		    htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP);	\
992 		(d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;		\
993 		(d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
994 		(d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;	\
995 	}								\
996 } while (0)
997 
998 #define pf_state_peer_ntoh(s,d) do {		\
999 	(d)->seqlo = ntohl((s)->seqlo);		\
1000 	(d)->seqhi = ntohl((s)->seqhi);		\
1001 	(d)->seqdiff = ntohl((s)->seqdiff);	\
1002 	(d)->max_win = ntohs((s)->max_win);	\
1003 	(d)->mss = ntohs((s)->mss);		\
1004 	(d)->state = (s)->state;		\
1005 	(d)->wscale = (s)->wscale;		\
1006 	if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && 	\
1007 	    (d)->scrub != NULL) {					\
1008 		(d)->scrub->pfss_flags =				\
1009 		    ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP;	\
1010 		(d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl;		\
1011 		(d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
1012 	}								\
1013 } while (0)
1014 
1015 #define pf_state_counter_hton(s,d) do {				\
1016 	d[0] = htonl((s>>32)&0xffffffff);			\
1017 	d[1] = htonl(s&0xffffffff);				\
1018 } while (0)
1019 
1020 #define pf_state_counter_from_pfsync(s)				\
1021 	(((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
1022 
1023 #define pf_state_counter_ntoh(s,d) do {				\
1024 	d = ntohl(s[0]);					\
1025 	d = d<<32;						\
1026 	d += ntohl(s[1]);					\
1027 } while (0)
1028 
1029 TAILQ_HEAD(pf_krulequeue, pf_krule);
1030 
1031 struct pf_kanchor;
1032 
1033 struct pf_kruleset {
1034 	struct {
1035 		struct pf_krulequeue	 queues[2];
1036 		struct {
1037 			struct pf_krulequeue	*ptr;
1038 			struct pf_krule		**ptr_array;
1039 			u_int32_t		 rcount;
1040 			u_int32_t		 ticket;
1041 			int			 open;
1042 		}			 active, inactive;
1043 	}			 rules[PF_RULESET_MAX];
1044 	struct pf_kanchor	*anchor;
1045 	u_int32_t		 tticket;
1046 	int			 tables;
1047 	int			 topen;
1048 };
1049 
1050 RB_HEAD(pf_kanchor_global, pf_kanchor);
1051 RB_HEAD(pf_kanchor_node, pf_kanchor);
1052 struct pf_kanchor {
1053 	RB_ENTRY(pf_kanchor)	 entry_global;
1054 	RB_ENTRY(pf_kanchor)	 entry_node;
1055 	struct pf_kanchor	*parent;
1056 	struct pf_kanchor_node	 children;
1057 	char			 name[PF_ANCHOR_NAME_SIZE];
1058 	char			 path[MAXPATHLEN];
1059 	struct pf_kruleset	 ruleset;
1060 	int			 refcnt;	/* anchor rules */
1061 };
1062 RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare);
1063 RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare);
1064 
1065 #define PF_RESERVED_ANCHOR	"_pf"
1066 
1067 #define PFR_TFLAG_PERSIST	0x00000001
1068 #define PFR_TFLAG_CONST		0x00000002
1069 #define PFR_TFLAG_ACTIVE	0x00000004
1070 #define PFR_TFLAG_INACTIVE	0x00000008
1071 #define PFR_TFLAG_REFERENCED	0x00000010
1072 #define PFR_TFLAG_REFDANCHOR	0x00000020
1073 #define PFR_TFLAG_COUNTERS	0x00000040
1074 /* Adjust masks below when adding flags. */
1075 #define PFR_TFLAG_USRMASK	(PFR_TFLAG_PERSIST	| \
1076 				 PFR_TFLAG_CONST	| \
1077 				 PFR_TFLAG_COUNTERS)
1078 #define PFR_TFLAG_SETMASK	(PFR_TFLAG_ACTIVE	| \
1079 				 PFR_TFLAG_INACTIVE	| \
1080 				 PFR_TFLAG_REFERENCED	| \
1081 				 PFR_TFLAG_REFDANCHOR)
1082 #define PFR_TFLAG_ALLMASK	(PFR_TFLAG_PERSIST	| \
1083 				 PFR_TFLAG_CONST	| \
1084 				 PFR_TFLAG_ACTIVE	| \
1085 				 PFR_TFLAG_INACTIVE	| \
1086 				 PFR_TFLAG_REFERENCED	| \
1087 				 PFR_TFLAG_REFDANCHOR	| \
1088 				 PFR_TFLAG_COUNTERS)
1089 
1090 struct pf_kanchor_stackframe;
1091 
1092 struct pfr_table {
1093 	char			 pfrt_anchor[MAXPATHLEN];
1094 	char			 pfrt_name[PF_TABLE_NAME_SIZE];
1095 	u_int32_t		 pfrt_flags;
1096 	u_int8_t		 pfrt_fback;
1097 };
1098 
1099 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
1100 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
1101 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX };
1102 
1103 struct pfr_addr {
1104 	union {
1105 		struct in_addr	 _pfra_ip4addr;
1106 		struct in6_addr	 _pfra_ip6addr;
1107 	}		 pfra_u;
1108 	u_int8_t	 pfra_af;
1109 	u_int8_t	 pfra_net;
1110 	u_int8_t	 pfra_not;
1111 	u_int8_t	 pfra_fback;
1112 };
1113 #define	pfra_ip4addr	pfra_u._pfra_ip4addr
1114 #define	pfra_ip6addr	pfra_u._pfra_ip6addr
1115 
1116 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
1117 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
1118 enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX };
1119 #define	PFR_NUM_COUNTERS	(PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX)
1120 #define PFR_OP_XPASS	PFR_OP_ADDR_MAX
1121 
1122 struct pfr_astats {
1123 	struct pfr_addr	 pfras_a;
1124 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1125 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1126 	long		 pfras_tzero;
1127 };
1128 
1129 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
1130 
1131 struct pfr_tstats {
1132 	struct pfr_table pfrts_t;
1133 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1134 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1135 	u_int64_t	 pfrts_match;
1136 	u_int64_t	 pfrts_nomatch;
1137 	long		 pfrts_tzero;
1138 	int		 pfrts_cnt;
1139 	int		 pfrts_refcnt[PFR_REFCNT_MAX];
1140 };
1141 
1142 #ifdef _KERNEL
1143 
1144 struct pfr_kstate_counter {
1145 	counter_u64_t	pkc_pcpu;
1146 	u_int64_t	pkc_zero;
1147 };
1148 
1149 static inline int
1150 pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags)
1151 {
1152 
1153 	pfrc->pkc_zero = 0;
1154 	pfrc->pkc_pcpu = counter_u64_alloc(flags);
1155 	if (pfrc->pkc_pcpu == NULL)
1156 		return (ENOMEM);
1157 	return (0);
1158 }
1159 
1160 static inline void
1161 pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc)
1162 {
1163 
1164 	counter_u64_free(pfrc->pkc_pcpu);
1165 }
1166 
1167 static inline u_int64_t
1168 pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc)
1169 {
1170 	u_int64_t c;
1171 
1172 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1173 	c -= pfrc->pkc_zero;
1174 	return (c);
1175 }
1176 
1177 static inline void
1178 pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc)
1179 {
1180 	u_int64_t c;
1181 
1182 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1183 	pfrc->pkc_zero = c;
1184 }
1185 
1186 static inline void
1187 pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n)
1188 {
1189 
1190 	counter_u64_add(pfrc->pkc_pcpu, n);
1191 }
1192 
1193 struct pfr_ktstats {
1194 	struct pfr_table pfrts_t;
1195 	struct pfr_kstate_counter	 pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1196 	struct pfr_kstate_counter	 pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1197 	struct pfr_kstate_counter	 pfrkts_match;
1198 	struct pfr_kstate_counter	 pfrkts_nomatch;
1199 	long		 pfrkts_tzero;
1200 	int		 pfrkts_cnt;
1201 	int		 pfrkts_refcnt[PFR_REFCNT_MAX];
1202 };
1203 
1204 #endif /* _KERNEL */
1205 
1206 #define	pfrts_name	pfrts_t.pfrt_name
1207 #define pfrts_flags	pfrts_t.pfrt_flags
1208 
1209 #ifndef _SOCKADDR_UNION_DEFINED
1210 #define	_SOCKADDR_UNION_DEFINED
1211 union sockaddr_union {
1212 	struct sockaddr		sa;
1213 	struct sockaddr_in	sin;
1214 	struct sockaddr_in6	sin6;
1215 };
1216 #endif /* _SOCKADDR_UNION_DEFINED */
1217 
1218 struct pfr_kcounters {
1219 	counter_u64_t		 pfrkc_counters;
1220 	long			 pfrkc_tzero;
1221 };
1222 #define	pfr_kentry_counter(kc, dir, op, t)		\
1223 	((kc)->pfrkc_counters +				\
1224 	    (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t))
1225 
1226 #ifdef _KERNEL
1227 SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
1228 struct pfr_kentry {
1229 	struct radix_node	 pfrke_node[2];
1230 	union sockaddr_union	 pfrke_sa;
1231 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq;
1232 	struct pfr_kcounters	 pfrke_counters;
1233 	u_int8_t		 pfrke_af;
1234 	u_int8_t		 pfrke_net;
1235 	u_int8_t		 pfrke_not;
1236 	u_int8_t		 pfrke_mark;
1237 };
1238 
1239 SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
1240 RB_HEAD(pfr_ktablehead, pfr_ktable);
1241 struct pfr_ktable {
1242 	struct pfr_ktstats	 pfrkt_kts;
1243 	RB_ENTRY(pfr_ktable)	 pfrkt_tree;
1244 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq;
1245 	struct radix_node_head	*pfrkt_ip4;
1246 	struct radix_node_head	*pfrkt_ip6;
1247 	struct pfr_ktable	*pfrkt_shadow;
1248 	struct pfr_ktable	*pfrkt_root;
1249 	struct pf_kruleset	*pfrkt_rs;
1250 	long			 pfrkt_larg;
1251 	int			 pfrkt_nflags;
1252 };
1253 #define pfrkt_t		pfrkt_kts.pfrts_t
1254 #define pfrkt_name	pfrkt_t.pfrt_name
1255 #define pfrkt_anchor	pfrkt_t.pfrt_anchor
1256 #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset
1257 #define pfrkt_flags	pfrkt_t.pfrt_flags
1258 #define pfrkt_cnt	pfrkt_kts.pfrkts_cnt
1259 #define pfrkt_refcnt	pfrkt_kts.pfrkts_refcnt
1260 #define pfrkt_packets	pfrkt_kts.pfrkts_packets
1261 #define pfrkt_bytes	pfrkt_kts.pfrkts_bytes
1262 #define pfrkt_match	pfrkt_kts.pfrkts_match
1263 #define pfrkt_nomatch	pfrkt_kts.pfrkts_nomatch
1264 #define pfrkt_tzero	pfrkt_kts.pfrkts_tzero
1265 #endif
1266 
1267 #ifdef _KERNEL
1268 struct pfi_kkif {
1269 	char				 pfik_name[IFNAMSIZ];
1270 	union {
1271 		RB_ENTRY(pfi_kkif)	 _pfik_tree;
1272 		LIST_ENTRY(pfi_kkif)	 _pfik_list;
1273 	} _pfik_glue;
1274 #define	pfik_tree	_pfik_glue._pfik_tree
1275 #define	pfik_list	_pfik_glue._pfik_list
1276 	struct pf_counter_u64		 pfik_packets[2][2][2];
1277 	struct pf_counter_u64		 pfik_bytes[2][2][2];
1278 	u_int32_t			 pfik_tzero;
1279 	u_int				 pfik_flags;
1280 	struct ifnet			*pfik_ifp;
1281 	struct ifg_group		*pfik_group;
1282 	u_int				 pfik_rulerefs;
1283 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1284 #ifdef PF_WANT_32_TO_64_COUNTER
1285 	LIST_ENTRY(pfi_kkif)		 pfik_allkiflist;
1286 #endif
1287 };
1288 #endif
1289 
1290 #define	PFI_IFLAG_REFS		0x0001	/* has state references */
1291 #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */
1292 
1293 #ifdef _KERNEL
1294 struct pf_pdesc {
1295 	struct {
1296 		int	 done;
1297 		uid_t	 uid;
1298 		gid_t	 gid;
1299 	}		 lookup;
1300 	u_int64_t	 tot_len;	/* Make Mickey money */
1301 	union pf_headers {
1302 		struct tcphdr		tcp;
1303 		struct udphdr		udp;
1304 		struct icmp		icmp;
1305 #ifdef INET6
1306 		struct icmp6_hdr	icmp6;
1307 #endif /* INET6 */
1308 		char any[0];
1309 	} hdr;
1310 
1311 	struct pf_krule	*nat_rule;	/* nat/rdr rule applied to packet */
1312 	struct pf_addr	*src;		/* src address */
1313 	struct pf_addr	*dst;		/* dst address */
1314 	u_int16_t *sport;
1315 	u_int16_t *dport;
1316 	struct pf_mtag	*pf_mtag;
1317 	struct pf_rule_actions	act;
1318 
1319 	u_int32_t	 p_len;		/* total length of payload */
1320 
1321 	u_int16_t	*ip_sum;
1322 	u_int16_t	*proto_sum;
1323 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
1324 					 * state code. Easier than tags */
1325 #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
1326 #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
1327 	sa_family_t	 af;
1328 	u_int8_t	 proto;
1329 	u_int8_t	 tos;
1330 	u_int8_t	 dir;		/* direction */
1331 	u_int8_t	 sidx;		/* key index for source */
1332 	u_int8_t	 didx;		/* key index for destination */
1333 };
1334 #endif
1335 
1336 /* flags for RDR options */
1337 #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
1338 #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
1339 
1340 /* UDP state enumeration */
1341 #define PFUDPS_NO_TRAFFIC	0
1342 #define PFUDPS_SINGLE		1
1343 #define PFUDPS_MULTIPLE		2
1344 
1345 #define PFUDPS_NSTATES		3	/* number of state levels */
1346 
1347 #define PFUDPS_NAMES { \
1348 	"NO_TRAFFIC", \
1349 	"SINGLE", \
1350 	"MULTIPLE", \
1351 	NULL \
1352 }
1353 
1354 /* Other protocol state enumeration */
1355 #define PFOTHERS_NO_TRAFFIC	0
1356 #define PFOTHERS_SINGLE		1
1357 #define PFOTHERS_MULTIPLE	2
1358 
1359 #define PFOTHERS_NSTATES	3	/* number of state levels */
1360 
1361 #define PFOTHERS_NAMES { \
1362 	"NO_TRAFFIC", \
1363 	"SINGLE", \
1364 	"MULTIPLE", \
1365 	NULL \
1366 }
1367 
1368 #define ACTION_SET(a, x) \
1369 	do { \
1370 		if ((a) != NULL) \
1371 			*(a) = (x); \
1372 	} while (0)
1373 
1374 #define REASON_SET(a, x) \
1375 	do { \
1376 		if ((a) != NULL) \
1377 			*(a) = (x); \
1378 		if (x < PFRES_MAX) \
1379 			counter_u64_add(V_pf_status.counters[x], 1); \
1380 	} while (0)
1381 
1382 enum pf_syncookies_mode {
1383 	PF_SYNCOOKIES_NEVER = 0,
1384 	PF_SYNCOOKIES_ALWAYS = 1,
1385 	PF_SYNCOOKIES_ADAPTIVE = 2,
1386 	PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE
1387 };
1388 
1389 #define	PF_SYNCOOKIES_HIWATPCT	25
1390 #define	PF_SYNCOOKIES_LOWATPCT	(PF_SYNCOOKIES_HIWATPCT / 2)
1391 
1392 #ifdef _KERNEL
1393 struct pf_kstatus {
1394 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */
1395 	counter_u64_t	lcounters[KLCNT_MAX]; /* limit counters */
1396 	struct pf_counter_u64	fcounters[FCNT_MAX]; /* state operation counters */
1397 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */
1398 	uint32_t	states;
1399 	uint32_t	src_nodes;
1400 	uint32_t	running;
1401 	uint32_t	since;
1402 	uint32_t	debug;
1403 	uint32_t	hostid;
1404 	char		ifname[IFNAMSIZ];
1405 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
1406 	bool		keep_counters;
1407 	enum pf_syncookies_mode	syncookies_mode;
1408 	bool		syncookies_active;
1409 	uint64_t	syncookies_inflight[2];
1410 	uint32_t	states_halfopen;
1411 };
1412 #endif
1413 
1414 struct pf_divert {
1415 	union {
1416 		struct in_addr	ipv4;
1417 		struct in6_addr	ipv6;
1418 	}		addr;
1419 	u_int16_t	port;
1420 };
1421 
1422 #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
1423 #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
1424 
1425 /*
1426  * Limit the length of the fragment queue traversal.  Remember
1427  * search entry points based on the fragment offset.
1428  */
1429 #define PF_FRAG_ENTRY_POINTS		16
1430 
1431 /*
1432  * The number of entries in the fragment queue must be limited
1433  * to avoid DoS by linear seaching.  Instead of a global limit,
1434  * use a limit per entry point.  For large packets these sum up.
1435  */
1436 #define PF_FRAG_ENTRY_LIMIT		64
1437 
1438 /*
1439  * ioctl parameter structures
1440  */
1441 
1442 struct pfioc_pooladdr {
1443 	u_int32_t		 action;
1444 	u_int32_t		 ticket;
1445 	u_int32_t		 nr;
1446 	u_int32_t		 r_num;
1447 	u_int8_t		 r_action;
1448 	u_int8_t		 r_last;
1449 	u_int8_t		 af;
1450 	char			 anchor[MAXPATHLEN];
1451 	struct pf_pooladdr	 addr;
1452 };
1453 
1454 struct pfioc_rule {
1455 	u_int32_t	 action;
1456 	u_int32_t	 ticket;
1457 	u_int32_t	 pool_ticket;
1458 	u_int32_t	 nr;
1459 	char		 anchor[MAXPATHLEN];
1460 	char		 anchor_call[MAXPATHLEN];
1461 	struct pf_rule	 rule;
1462 };
1463 
1464 struct pfioc_natlook {
1465 	struct pf_addr	 saddr;
1466 	struct pf_addr	 daddr;
1467 	struct pf_addr	 rsaddr;
1468 	struct pf_addr	 rdaddr;
1469 	u_int16_t	 sport;
1470 	u_int16_t	 dport;
1471 	u_int16_t	 rsport;
1472 	u_int16_t	 rdport;
1473 	sa_family_t	 af;
1474 	u_int8_t	 proto;
1475 	u_int8_t	 direction;
1476 };
1477 
1478 struct pfioc_state {
1479 	struct pfsync_state	state;
1480 };
1481 
1482 struct pfioc_src_node_kill {
1483 	sa_family_t psnk_af;
1484 	struct pf_rule_addr psnk_src;
1485 	struct pf_rule_addr psnk_dst;
1486 	u_int		    psnk_killed;
1487 };
1488 
1489 #ifdef _KERNEL
1490 struct pf_kstate_kill {
1491 	struct pf_state_cmp	psk_pfcmp;
1492 	sa_family_t		psk_af;
1493 	int			psk_proto;
1494 	struct pf_rule_addr	psk_src;
1495 	struct pf_rule_addr	psk_dst;
1496 	struct pf_rule_addr	psk_rt_addr;
1497 	char			psk_ifname[IFNAMSIZ];
1498 	char			psk_label[PF_RULE_LABEL_SIZE];
1499 	u_int			psk_killed;
1500 	bool			psk_kill_match;
1501 };
1502 #endif
1503 
1504 struct pfioc_state_kill {
1505 	struct pf_state_cmp	psk_pfcmp;
1506 	sa_family_t		psk_af;
1507 	int			psk_proto;
1508 	struct pf_rule_addr	psk_src;
1509 	struct pf_rule_addr	psk_dst;
1510 	char			psk_ifname[IFNAMSIZ];
1511 	char			psk_label[PF_RULE_LABEL_SIZE];
1512 	u_int			psk_killed;
1513 };
1514 
1515 struct pfioc_states {
1516 	int	ps_len;
1517 	union {
1518 		caddr_t			 psu_buf;
1519 		struct pfsync_state	*psu_states;
1520 	} ps_u;
1521 #define ps_buf		ps_u.psu_buf
1522 #define ps_states	ps_u.psu_states
1523 };
1524 
1525 struct pfioc_states_v2 {
1526 	int		ps_len;
1527 	uint64_t	ps_req_version;
1528 	union {
1529 		caddr_t			 psu_buf;
1530 		struct pf_state_export	*psu_states;
1531 	} ps_u;
1532 #define ps_buf		ps_u.psu_buf
1533 #define ps_states	ps_u.psu_states
1534 };
1535 
1536 struct pfioc_src_nodes {
1537 	int	psn_len;
1538 	union {
1539 		caddr_t		 psu_buf;
1540 		struct pf_src_node	*psu_src_nodes;
1541 	} psn_u;
1542 #define psn_buf		psn_u.psu_buf
1543 #define psn_src_nodes	psn_u.psu_src_nodes
1544 };
1545 
1546 struct pfioc_if {
1547 	char		 ifname[IFNAMSIZ];
1548 };
1549 
1550 struct pfioc_tm {
1551 	int		 timeout;
1552 	int		 seconds;
1553 };
1554 
1555 struct pfioc_limit {
1556 	int		 index;
1557 	unsigned	 limit;
1558 };
1559 
1560 struct pfioc_altq_v0 {
1561 	u_int32_t	 action;
1562 	u_int32_t	 ticket;
1563 	u_int32_t	 nr;
1564 	struct pf_altq_v0 altq;
1565 };
1566 
1567 struct pfioc_altq_v1 {
1568 	u_int32_t	 action;
1569 	u_int32_t	 ticket;
1570 	u_int32_t	 nr;
1571 	/*
1572 	 * Placed here so code that only uses the above parameters can be
1573 	 * written entirely in terms of the v0 or v1 type.
1574 	 */
1575 	u_int32_t	 version;
1576 	struct pf_altq_v1 altq;
1577 };
1578 
1579 /*
1580  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1581  * the latest version of struct pf_altq_vX as it has that struct as a
1582  * member.
1583  */
1584 #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION
1585 
1586 struct pfioc_qstats_v0 {
1587 	u_int32_t	 ticket;
1588 	u_int32_t	 nr;
1589 	void		*buf;
1590 	int		 nbytes;
1591 	u_int8_t	 scheduler;
1592 };
1593 
1594 struct pfioc_qstats_v1 {
1595 	u_int32_t	 ticket;
1596 	u_int32_t	 nr;
1597 	void		*buf;
1598 	int		 nbytes;
1599 	u_int8_t	 scheduler;
1600 	/*
1601 	 * Placed here so code that only uses the above parameters can be
1602 	 * written entirely in terms of the v0 or v1 type.
1603 	 */
1604 	u_int32_t	 version;  /* Requested version of stats struct */
1605 };
1606 
1607 /* Latest version of struct pfioc_qstats_vX */
1608 #define PFIOC_QSTATS_VERSION	1
1609 
1610 struct pfioc_ruleset {
1611 	u_int32_t	 nr;
1612 	char		 path[MAXPATHLEN];
1613 	char		 name[PF_ANCHOR_NAME_SIZE];
1614 };
1615 
1616 #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
1617 #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1618 struct pfioc_trans {
1619 	int		 size;	/* number of elements */
1620 	int		 esize; /* size of each element in bytes */
1621 	struct pfioc_trans_e {
1622 		int		rs_num;
1623 		char		anchor[MAXPATHLEN];
1624 		u_int32_t	ticket;
1625 	}		*array;
1626 };
1627 
1628 #define PFR_FLAG_ATOMIC		0x00000001	/* unused */
1629 #define PFR_FLAG_DUMMY		0x00000002
1630 #define PFR_FLAG_FEEDBACK	0x00000004
1631 #define PFR_FLAG_CLSTATS	0x00000008
1632 #define PFR_FLAG_ADDRSTOO	0x00000010
1633 #define PFR_FLAG_REPLACE	0x00000020
1634 #define PFR_FLAG_ALLRSETS	0x00000040
1635 #define PFR_FLAG_ALLMASK	0x0000007F
1636 #ifdef _KERNEL
1637 #define PFR_FLAG_USERIOCTL	0x10000000
1638 #endif
1639 
1640 struct pfioc_table {
1641 	struct pfr_table	 pfrio_table;
1642 	void			*pfrio_buffer;
1643 	int			 pfrio_esize;
1644 	int			 pfrio_size;
1645 	int			 pfrio_size2;
1646 	int			 pfrio_nadd;
1647 	int			 pfrio_ndel;
1648 	int			 pfrio_nchange;
1649 	int			 pfrio_flags;
1650 	u_int32_t		 pfrio_ticket;
1651 };
1652 #define	pfrio_exists	pfrio_nadd
1653 #define	pfrio_nzero	pfrio_nadd
1654 #define	pfrio_nmatch	pfrio_nadd
1655 #define pfrio_naddr	pfrio_size2
1656 #define pfrio_setflag	pfrio_size2
1657 #define pfrio_clrflag	pfrio_nadd
1658 
1659 struct pfioc_iface {
1660 	char	 pfiio_name[IFNAMSIZ];
1661 	void	*pfiio_buffer;
1662 	int	 pfiio_esize;
1663 	int	 pfiio_size;
1664 	int	 pfiio_nzero;
1665 	int	 pfiio_flags;
1666 };
1667 
1668 /*
1669  * ioctl operations
1670  */
1671 
1672 #define DIOCSTART	_IO  ('D',  1)
1673 #define DIOCSTOP	_IO  ('D',  2)
1674 #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
1675 #define DIOCADDRULENV	_IOWR('D',  4, struct pfioc_nv)
1676 #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
1677 #define DIOCGETRULE	_IOWR('D',  7, struct pfioc_rule)
1678 #define DIOCGETRULENV	_IOWR('D',  7, struct pfioc_nv)
1679 /* XXX cut 8 - 17 */
1680 #define DIOCCLRSTATES	_IOWR('D', 18, struct pfioc_state_kill)
1681 #define DIOCCLRSTATESNV	_IOWR('D', 18, struct pfioc_nv)
1682 #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
1683 #define DIOCGETSTATENV	_IOWR('D', 19, struct pfioc_nv)
1684 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1685 #define DIOCGETSTATUS	_IOWR('D', 21, struct pf_status)
1686 #define DIOCGETSTATUSNV	_IOWR('D', 21, struct pfioc_nv)
1687 #define DIOCCLRSTATUS	_IO  ('D', 22)
1688 #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
1689 #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
1690 #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1691 #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
1692 /* XXX cut 26 - 28 */
1693 #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
1694 #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1695 #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1696 #define DIOCCLRRULECTRS	_IO  ('D', 38)
1697 #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
1698 #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
1699 #define DIOCKILLSTATES	_IOWR('D', 41, struct pfioc_state_kill)
1700 #define DIOCKILLSTATESNV	_IOWR('D', 41, struct pfioc_nv)
1701 #define DIOCSTARTALTQ	_IO  ('D', 42)
1702 #define DIOCSTOPALTQ	_IO  ('D', 43)
1703 #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0)
1704 #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1)
1705 #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0)
1706 #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1)
1707 #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0)
1708 #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1)
1709 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1710 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1711 #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0)
1712 #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1)
1713 #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
1714 #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
1715 #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
1716 #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1717 #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
1718 /* XXX cut 55 - 57 */
1719 #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
1720 #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
1721 #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
1722 #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
1723 #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
1724 #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
1725 #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
1726 #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table)
1727 #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
1728 #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
1729 #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
1730 #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
1731 #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
1732 #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
1733 #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table)
1734 #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
1735 #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
1736 #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
1737 #define	DIOCOSFPFLUSH	_IO('D', 78)
1738 #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
1739 #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
1740 #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans)
1741 #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans)
1742 #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans)
1743 #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
1744 #define	DIOCCLRSRCNODES	_IO('D', 85)
1745 #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
1746 #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
1747 #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
1748 #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
1749 #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
1750 #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0)
1751 #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1)
1752 #define DIOCGETSTATESV2	_IOWR('D', 93, struct pfioc_states_v2)
1753 #define	DIOCGETSYNCOOKIES	_IOWR('D', 94, struct pfioc_nv)
1754 #define	DIOCSETSYNCOOKIES	_IOWR('D', 95, struct pfioc_nv)
1755 #define	DIOCKEEPCOUNTERS	_IOWR('D', 96, struct pfioc_nv)
1756 #define	DIOCKEEPCOUNTERS_FREEBSD13	_IOWR('D', 92, struct pfioc_nv)
1757 
1758 struct pf_ifspeed_v0 {
1759 	char			ifname[IFNAMSIZ];
1760 	u_int32_t		baudrate;
1761 };
1762 
1763 struct pf_ifspeed_v1 {
1764 	char			ifname[IFNAMSIZ];
1765 	u_int32_t		baudrate32;
1766 	/* layout identical to struct pf_ifspeed_v0 up to this point */
1767 	u_int64_t		baudrate;
1768 };
1769 
1770 /* Latest version of struct pf_ifspeed_vX */
1771 #define PF_IFSPEED_VERSION	1
1772 
1773 /*
1774  * Compatibility and convenience macros
1775  */
1776 #ifndef _KERNEL
1777 #ifdef PFIOC_USE_LATEST
1778 /*
1779  * Maintaining in-tree consumers of the ioctl interface is easier when that
1780  * code can be written in terms old names that refer to the latest interface
1781  * version as that reduces the required changes in the consumers to those
1782  * that are functionally necessary to accommodate a new interface version.
1783  */
1784 #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
1785 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
1786 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
1787 
1788 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
1789 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
1790 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
1791 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
1792 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
1793 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
1794 #else
1795 /*
1796  * When building out-of-tree code that is written for the old interface,
1797  * such as may exist in ports for example, resolve the old struct tags and
1798  * ioctl command names to the v0 versions.
1799  */
1800 #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0)
1801 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0)
1802 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0)
1803 
1804 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0)
1805 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0)
1806 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0)
1807 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0)
1808 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0)
1809 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0)
1810 #endif /* PFIOC_USE_LATEST */
1811 #endif /* _KERNEL */
1812 
1813 #ifdef _KERNEL
1814 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node);
1815 struct pf_srchash {
1816 	struct pf_ksrc_node_list		nodes;
1817 	struct mtx			lock;
1818 };
1819 
1820 struct pf_keyhash {
1821 	LIST_HEAD(, pf_state_key)	keys;
1822 	struct mtx			lock;
1823 };
1824 
1825 struct pf_idhash {
1826 	LIST_HEAD(, pf_kstate)		states;
1827 	struct mtx			lock;
1828 };
1829 
1830 extern u_long		pf_ioctl_maxcount;
1831 extern u_long		pf_hashmask;
1832 extern u_long		pf_srchashmask;
1833 #define	PF_HASHSIZ	(131072)
1834 #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4)
1835 VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
1836 VNET_DECLARE(struct pf_idhash *, pf_idhash);
1837 #define V_pf_keyhash	VNET(pf_keyhash)
1838 #define	V_pf_idhash	VNET(pf_idhash)
1839 VNET_DECLARE(struct pf_srchash *, pf_srchash);
1840 #define	V_pf_srchash	VNET(pf_srchash)
1841 
1842 #define PF_IDHASH(s)	(be64toh((s)->id) % (pf_hashmask + 1))
1843 
1844 VNET_DECLARE(void *, pf_swi_cookie);
1845 #define V_pf_swi_cookie	VNET(pf_swi_cookie)
1846 VNET_DECLARE(struct intr_event *, pf_swi_ie);
1847 #define	V_pf_swi_ie	VNET(pf_swi_ie)
1848 
1849 VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]);
1850 #define	V_pf_stateid	VNET(pf_stateid)
1851 
1852 TAILQ_HEAD(pf_altqqueue, pf_altq);
1853 VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]);
1854 #define	V_pf_altqs			 VNET(pf_altqs)
1855 VNET_DECLARE(struct pf_kpalist,		 pf_pabuf);
1856 #define	V_pf_pabuf			 VNET(pf_pabuf)
1857 
1858 VNET_DECLARE(u_int32_t,			 ticket_altqs_active);
1859 #define	V_ticket_altqs_active		 VNET(ticket_altqs_active)
1860 VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive);
1861 #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive)
1862 VNET_DECLARE(int,			 altqs_inactive_open);
1863 #define	V_altqs_inactive_open		 VNET(altqs_inactive_open)
1864 VNET_DECLARE(u_int32_t,			 ticket_pabuf);
1865 #define	V_ticket_pabuf			 VNET(ticket_pabuf)
1866 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active);
1867 #define	V_pf_altqs_active		 VNET(pf_altqs_active)
1868 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active);
1869 #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active)
1870 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive);
1871 #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive)
1872 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
1873 #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive)
1874 
1875 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules);
1876 #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules)
1877 
1878 #ifdef PF_WANT_32_TO_64_COUNTER
1879 LIST_HEAD(allkiflist_head, pfi_kkif);
1880 VNET_DECLARE(struct allkiflist_head, pf_allkiflist);
1881 #define V_pf_allkiflist     VNET(pf_allkiflist)
1882 VNET_DECLARE(size_t, pf_allkifcount);
1883 #define V_pf_allkifcount     VNET(pf_allkifcount)
1884 VNET_DECLARE(struct pfi_kkif *, pf_kifmarker);
1885 #define V_pf_kifmarker     VNET(pf_kifmarker)
1886 
1887 LIST_HEAD(allrulelist_head, pf_krule);
1888 VNET_DECLARE(struct allrulelist_head, pf_allrulelist);
1889 #define V_pf_allrulelist     VNET(pf_allrulelist)
1890 VNET_DECLARE(size_t, pf_allrulecount);
1891 #define V_pf_allrulecount     VNET(pf_allrulecount)
1892 VNET_DECLARE(struct pf_krule *, pf_rulemarker);
1893 #define V_pf_rulemarker     VNET(pf_rulemarker)
1894 #endif
1895 
1896 void				 pf_initialize(void);
1897 void				 pf_mtag_initialize(void);
1898 void				 pf_mtag_cleanup(void);
1899 void				 pf_cleanup(void);
1900 
1901 struct pf_mtag			*pf_get_mtag(struct mbuf *);
1902 
1903 extern void			 pf_calc_skip_steps(struct pf_krulequeue *);
1904 #ifdef ALTQ
1905 extern	void			 pf_altq_ifnet_event(struct ifnet *, int);
1906 #endif
1907 VNET_DECLARE(uma_zone_t,	 pf_state_z);
1908 #define	V_pf_state_z		 VNET(pf_state_z)
1909 VNET_DECLARE(uma_zone_t,	 pf_state_key_z);
1910 #define	V_pf_state_key_z	 VNET(pf_state_key_z)
1911 VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z);
1912 #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z)
1913 
1914 extern void			 pf_purge_thread(void *);
1915 extern void			 pf_unload_vnet_purge(void);
1916 extern void			 pf_intr(void *);
1917 extern void			 pf_purge_expired_src_nodes(void);
1918 
1919 extern int			 pf_unlink_state(struct pf_kstate *);
1920 extern int			 pf_state_insert(struct pfi_kkif *,
1921 				    struct pfi_kkif *,
1922 				    struct pf_state_key *,
1923 				    struct pf_state_key *,
1924 				    struct pf_kstate *);
1925 extern struct pf_kstate		*pf_alloc_state(int);
1926 extern void			 pf_free_state(struct pf_kstate *);
1927 
1928 static __inline void
1929 pf_ref_state(struct pf_kstate *s)
1930 {
1931 
1932 	refcount_acquire(&s->refs);
1933 }
1934 
1935 static __inline int
1936 pf_release_state(struct pf_kstate *s)
1937 {
1938 
1939 	if (refcount_release(&s->refs)) {
1940 		pf_free_state(s);
1941 		return (1);
1942 	} else
1943 		return (0);
1944 }
1945 
1946 static __inline int
1947 pf_release_staten(struct pf_kstate *s, u_int n)
1948 {
1949 
1950 	if (refcount_releasen(&s->refs, n)) {
1951 		pf_free_state(s);
1952 		return (1);
1953 	} else
1954 		return (0);
1955 }
1956 
1957 extern struct pf_kstate		*pf_find_state_byid(uint64_t, uint32_t);
1958 extern struct pf_kstate		*pf_find_state_all(struct pf_state_key_cmp *,
1959 				    u_int, int *);
1960 extern bool			pf_find_state_all_exists(struct pf_state_key_cmp *,
1961 				    u_int);
1962 extern struct pf_ksrc_node	*pf_find_src_node(struct pf_addr *,
1963 				    struct pf_krule *, sa_family_t, int);
1964 extern void			 pf_unlink_src_node(struct pf_ksrc_node *);
1965 extern u_int			 pf_free_src_nodes(struct pf_ksrc_node_list *);
1966 extern void			 pf_print_state(struct pf_kstate *);
1967 extern void			 pf_print_flags(u_int8_t);
1968 extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
1969 				    u_int8_t);
1970 extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
1971 				    u_int16_t, u_int16_t, u_int8_t);
1972 
1973 VNET_DECLARE(struct ifnet *,		 sync_ifp);
1974 #define	V_sync_ifp		 	 VNET(sync_ifp);
1975 VNET_DECLARE(struct pf_krule,		 pf_default_rule);
1976 #define	V_pf_default_rule		  VNET(pf_default_rule)
1977 extern void			 pf_addrcpy(struct pf_addr *, struct pf_addr *,
1978 				    u_int8_t);
1979 void				pf_free_rule(struct pf_krule *);
1980 
1981 #ifdef INET
1982 int	pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1983 int	pf_normalize_ip(struct mbuf **, int, struct pfi_kkif *, u_short *,
1984 	    struct pf_pdesc *);
1985 #endif /* INET */
1986 
1987 #ifdef INET6
1988 int	pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1989 int	pf_normalize_ip6(struct mbuf **, int, struct pfi_kkif *, u_short *,
1990 	    struct pf_pdesc *);
1991 void	pf_poolmask(struct pf_addr *, struct pf_addr*,
1992 	    struct pf_addr *, struct pf_addr *, u_int8_t);
1993 void	pf_addr_inc(struct pf_addr *, sa_family_t);
1994 int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *);
1995 #endif /* INET6 */
1996 
1997 u_int32_t	pf_new_isn(struct pf_kstate *);
1998 void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
1999 	    sa_family_t);
2000 void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
2001 void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
2002 	    u_int8_t);
2003 void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
2004 void	pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t,
2005 	    bool, u_int8_t);
2006 void	pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t,
2007     bool, u_int8_t);
2008 void	pf_send_deferred_syn(struct pf_kstate *);
2009 int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
2010 	    struct pf_addr *, sa_family_t);
2011 int	pf_match_addr_range(struct pf_addr *, struct pf_addr *,
2012 	    struct pf_addr *, sa_family_t);
2013 int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
2014 
2015 void	pf_normalize_init(void);
2016 void	pf_normalize_cleanup(void);
2017 int	pf_normalize_tcp(int, struct pfi_kkif *, struct mbuf *, int, int, void *,
2018 	    struct pf_pdesc *);
2019 void	pf_normalize_tcp_cleanup(struct pf_kstate *);
2020 int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
2021 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
2022 int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
2023 	    u_short *, struct tcphdr *, struct pf_kstate *,
2024 	    struct pf_state_peer *, struct pf_state_peer *, int *);
2025 u_int32_t
2026 	pf_state_expires(const struct pf_kstate *);
2027 void	pf_purge_expired_fragments(void);
2028 void	pf_purge_fragments(uint32_t);
2029 int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *,
2030 	    int);
2031 int	pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *);
2032 struct pf_state_key *pf_alloc_state_key(int);
2033 void	pfr_initialize(void);
2034 void	pfr_cleanup(void);
2035 int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
2036 void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
2037 	    u_int64_t, int, int, int);
2038 int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
2039 void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
2040 struct pfr_ktable *
2041 	pfr_attach_table(struct pf_kruleset *, char *);
2042 void	pfr_detach_table(struct pfr_ktable *);
2043 int	pfr_clr_tables(struct pfr_table *, int *, int);
2044 int	pfr_add_tables(struct pfr_table *, int, int *, int);
2045 int	pfr_del_tables(struct pfr_table *, int, int *, int);
2046 int	pfr_table_count(struct pfr_table *, int);
2047 int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
2048 int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
2049 int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
2050 int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
2051 int	pfr_clr_addrs(struct pfr_table *, int *, int);
2052 int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
2053 int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2054 	    int);
2055 int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2056 	    int);
2057 int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2058 	    int *, int *, int *, int, u_int32_t);
2059 int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
2060 int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
2061 int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
2062 	    int);
2063 int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2064 	    int);
2065 int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
2066 int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
2067 int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
2068 int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
2069 	    int *, u_int32_t, int);
2070 
2071 MALLOC_DECLARE(PFI_MTYPE);
2072 VNET_DECLARE(struct pfi_kkif *,		 pfi_all);
2073 #define	V_pfi_all	 		 VNET(pfi_all)
2074 
2075 void		 pfi_initialize(void);
2076 void		 pfi_initialize_vnet(void);
2077 void		 pfi_cleanup(void);
2078 void		 pfi_cleanup_vnet(void);
2079 void		 pfi_kkif_ref(struct pfi_kkif *);
2080 void		 pfi_kkif_unref(struct pfi_kkif *);
2081 struct pfi_kkif	*pfi_kkif_find(const char *);
2082 struct pfi_kkif	*pfi_kkif_attach(struct pfi_kkif *, const char *);
2083 int		 pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *);
2084 void		 pfi_kkif_purge(void);
2085 int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
2086 		    sa_family_t);
2087 int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
2088 void		 pfi_dynaddr_remove(struct pfi_dynaddr *);
2089 void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
2090 void		 pfi_update_status(const char *, struct pf_status *);
2091 void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
2092 int		 pfi_set_flags(const char *, int);
2093 int		 pfi_clear_flags(const char *, int);
2094 
2095 int		 pf_match_tag(struct mbuf *, struct pf_krule *, int *, int);
2096 int		 pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
2097 int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *,
2098 		    sa_family_t);
2099 
2100 u_int16_t	 pf_get_mss(struct mbuf *, int, u_int16_t, sa_family_t);
2101 u_int8_t	 pf_get_wscale(struct mbuf *, int, u_int16_t, sa_family_t);
2102 struct mbuf 	*pf_build_tcp(const struct pf_krule *, sa_family_t,
2103 		    const struct pf_addr *, const struct pf_addr *,
2104 		    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
2105 		    u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
2106 		    u_int16_t);
2107 void		 pf_send_tcp(const struct pf_krule *, sa_family_t,
2108 			    const struct pf_addr *, const struct pf_addr *,
2109 			    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
2110 			    u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
2111 			    u_int16_t);
2112 
2113 void			 pf_syncookies_init(void);
2114 void			 pf_syncookies_cleanup(void);
2115 int			 pf_get_syncookies(struct pfioc_nv *);
2116 int			 pf_set_syncookies(struct pfioc_nv *);
2117 int			 pf_synflood_check(struct pf_pdesc *);
2118 void			 pf_syncookie_send(struct mbuf *m, int off,
2119 			    struct pf_pdesc *);
2120 u_int8_t		 pf_syncookie_validate(struct pf_pdesc *);
2121 struct mbuf *		 pf_syncookie_recreate_syn(uint8_t, int,
2122 			    struct pf_pdesc *);
2123 
2124 VNET_DECLARE(struct pf_kstatus, pf_status);
2125 #define	V_pf_status	VNET(pf_status)
2126 
2127 struct pf_limit {
2128 	uma_zone_t	zone;
2129 	u_int		limit;
2130 };
2131 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
2132 #define	V_pf_limits VNET(pf_limits)
2133 
2134 #endif /* _KERNEL */
2135 
2136 #ifdef _KERNEL
2137 VNET_DECLARE(struct pf_kanchor_global,		 pf_anchors);
2138 #define	V_pf_anchors				 VNET(pf_anchors)
2139 VNET_DECLARE(struct pf_kanchor,			 pf_main_anchor);
2140 #define	V_pf_main_anchor			 VNET(pf_main_anchor)
2141 #define pf_main_ruleset	V_pf_main_anchor.ruleset
2142 
2143 void			 pf_init_kruleset(struct pf_kruleset *);
2144 int			 pf_kanchor_setup(struct pf_krule *,
2145 			    const struct pf_kruleset *, const char *);
2146 int			 pf_kanchor_nvcopyout(const struct pf_kruleset *,
2147 			    const struct pf_krule *, nvlist_t *);
2148 int			 pf_kanchor_copyout(const struct pf_kruleset *,
2149 			    const struct pf_krule *, struct pfioc_rule *);
2150 void			 pf_kanchor_remove(struct pf_krule *);
2151 void			 pf_remove_if_empty_kruleset(struct pf_kruleset *);
2152 struct pf_kruleset	*pf_find_kruleset(const char *);
2153 struct pf_kruleset	*pf_find_or_create_kruleset(const char *);
2154 void			 pf_rs_initialize(void);
2155 
2156 void			 pf_krule_free(struct pf_krule *);
2157 #endif
2158 
2159 /* The fingerprint functions can be linked into userland programs (tcpdump) */
2160 int	pf_osfp_add(struct pf_osfp_ioctl *);
2161 #ifdef _KERNEL
2162 struct pf_osfp_enlist *
2163 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
2164 	    const struct tcphdr *);
2165 #endif /* _KERNEL */
2166 void	pf_osfp_flush(void);
2167 int	pf_osfp_get(struct pf_osfp_ioctl *);
2168 int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
2169 
2170 #ifdef _KERNEL
2171 void			 pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
2172 
2173 void			 pf_step_into_anchor(struct pf_kanchor_stackframe *, int *,
2174 			    struct pf_kruleset **, int, struct pf_krule **,
2175 			    struct pf_krule **, int *);
2176 int			 pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *,
2177 			    struct pf_kruleset **, int, struct pf_krule **,
2178 			    struct pf_krule **, int *);
2179 
2180 int			 pf_map_addr(u_int8_t, struct pf_krule *,
2181 			    struct pf_addr *, struct pf_addr *,
2182 			    struct pf_addr *, struct pf_ksrc_node **);
2183 struct pf_krule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
2184 			    int, int, struct pfi_kkif *, struct pf_ksrc_node **,
2185 			    struct pf_state_key **, struct pf_state_key **,
2186 			    struct pf_addr *, struct pf_addr *,
2187 			    uint16_t, uint16_t, struct pf_kanchor_stackframe *);
2188 
2189 struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
2190 			    struct pf_addr *, u_int16_t, u_int16_t);
2191 struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
2192 
2193 struct pfi_kkif		*pf_kkif_create(int);
2194 void			 pf_kkif_free(struct pfi_kkif *);
2195 void			 pf_kkif_zero(struct pfi_kkif *);
2196 #endif /* _KERNEL */
2197 
2198 #endif /* _NET_PFVAR_H_ */
2199