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