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