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