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