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