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