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