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