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