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