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