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