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