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