xref: /freebsd/sys/net/pfvar.h (revision a0409676120c1e558d0ade943019934e0f15118d)
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 #ifdef _KERNEL
897 struct pf_pdesc {
898 	struct {
899 		int	 done;
900 		uid_t	 uid;
901 		gid_t	 gid;
902 	}		 lookup;
903 	u_int64_t	 tot_len;	/* Make Mickey money */
904 	union {
905 		struct tcphdr		*tcp;
906 		struct udphdr		*udp;
907 		struct icmp		*icmp;
908 #ifdef INET6
909 		struct icmp6_hdr	*icmp6;
910 #endif /* INET6 */
911 		void			*any;
912 	} hdr;
913 
914 	struct pf_krule	*nat_rule;	/* nat/rdr rule applied to packet */
915 	struct pf_addr	*src;		/* src address */
916 	struct pf_addr	*dst;		/* dst address */
917 	u_int16_t *sport;
918 	u_int16_t *dport;
919 	struct pf_mtag	*pf_mtag;
920 
921 	u_int32_t	 p_len;		/* total length of payload */
922 
923 	u_int16_t	*ip_sum;
924 	u_int16_t	*proto_sum;
925 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
926 					 * state code. Easier than tags */
927 #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
928 #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
929 	sa_family_t	 af;
930 	u_int8_t	 proto;
931 	u_int8_t	 tos;
932 	u_int8_t	 dir;		/* direction */
933 	u_int8_t	 sidx;		/* key index for source */
934 	u_int8_t	 didx;		/* key index for destination */
935 };
936 #endif
937 
938 /* flags for RDR options */
939 #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
940 #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
941 
942 /* UDP state enumeration */
943 #define PFUDPS_NO_TRAFFIC	0
944 #define PFUDPS_SINGLE		1
945 #define PFUDPS_MULTIPLE		2
946 
947 #define PFUDPS_NSTATES		3	/* number of state levels */
948 
949 #define PFUDPS_NAMES { \
950 	"NO_TRAFFIC", \
951 	"SINGLE", \
952 	"MULTIPLE", \
953 	NULL \
954 }
955 
956 /* Other protocol state enumeration */
957 #define PFOTHERS_NO_TRAFFIC	0
958 #define PFOTHERS_SINGLE		1
959 #define PFOTHERS_MULTIPLE	2
960 
961 #define PFOTHERS_NSTATES	3	/* number of state levels */
962 
963 #define PFOTHERS_NAMES { \
964 	"NO_TRAFFIC", \
965 	"SINGLE", \
966 	"MULTIPLE", \
967 	NULL \
968 }
969 
970 #define ACTION_SET(a, x) \
971 	do { \
972 		if ((a) != NULL) \
973 			*(a) = (x); \
974 	} while (0)
975 
976 #define REASON_SET(a, x) \
977 	do { \
978 		if ((a) != NULL) \
979 			*(a) = (x); \
980 		if (x < PFRES_MAX) \
981 			counter_u64_add(V_pf_status.counters[x], 1); \
982 	} while (0)
983 
984 struct pf_kstatus {
985 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */
986 	counter_u64_t	lcounters[LCNT_MAX]; /* limit counters */
987 	counter_u64_t	fcounters[FCNT_MAX]; /* state operation counters */
988 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */
989 	uint32_t	states;
990 	uint32_t	src_nodes;
991 	uint32_t	running;
992 	uint32_t	since;
993 	uint32_t	debug;
994 	uint32_t	hostid;
995 	char		ifname[IFNAMSIZ];
996 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
997 };
998 
999 struct pf_divert {
1000 	union {
1001 		struct in_addr	ipv4;
1002 		struct in6_addr	ipv6;
1003 	}		addr;
1004 	u_int16_t	port;
1005 };
1006 
1007 #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
1008 #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
1009 
1010 /*
1011  * Limit the length of the fragment queue traversal.  Remember
1012  * search entry points based on the fragment offset.
1013  */
1014 #define PF_FRAG_ENTRY_POINTS		16
1015 
1016 /*
1017  * The number of entries in the fragment queue must be limited
1018  * to avoid DoS by linear seaching.  Instead of a global limit,
1019  * use a limit per entry point.  For large packets these sum up.
1020  */
1021 #define PF_FRAG_ENTRY_LIMIT		64
1022 
1023 /*
1024  * ioctl parameter structures
1025  */
1026 
1027 struct pfioc_pooladdr {
1028 	u_int32_t		 action;
1029 	u_int32_t		 ticket;
1030 	u_int32_t		 nr;
1031 	u_int32_t		 r_num;
1032 	u_int8_t		 r_action;
1033 	u_int8_t		 r_last;
1034 	u_int8_t		 af;
1035 	char			 anchor[MAXPATHLEN];
1036 	struct pf_pooladdr	 addr;
1037 };
1038 
1039 struct pfioc_rule {
1040 	u_int32_t	 action;
1041 	u_int32_t	 ticket;
1042 	u_int32_t	 pool_ticket;
1043 	u_int32_t	 nr;
1044 	char		 anchor[MAXPATHLEN];
1045 	char		 anchor_call[MAXPATHLEN];
1046 	struct pf_rule	 rule;
1047 };
1048 
1049 struct pfioc_natlook {
1050 	struct pf_addr	 saddr;
1051 	struct pf_addr	 daddr;
1052 	struct pf_addr	 rsaddr;
1053 	struct pf_addr	 rdaddr;
1054 	u_int16_t	 sport;
1055 	u_int16_t	 dport;
1056 	u_int16_t	 rsport;
1057 	u_int16_t	 rdport;
1058 	sa_family_t	 af;
1059 	u_int8_t	 proto;
1060 	u_int8_t	 direction;
1061 };
1062 
1063 struct pfioc_state {
1064 	struct pfsync_state	state;
1065 };
1066 
1067 struct pfioc_src_node_kill {
1068 	sa_family_t psnk_af;
1069 	struct pf_rule_addr psnk_src;
1070 	struct pf_rule_addr psnk_dst;
1071 	u_int		    psnk_killed;
1072 };
1073 
1074 struct pfioc_state_kill {
1075 	struct pf_state_cmp	psk_pfcmp;
1076 	sa_family_t		psk_af;
1077 	int			psk_proto;
1078 	struct pf_rule_addr	psk_src;
1079 	struct pf_rule_addr	psk_dst;
1080 	char			psk_ifname[IFNAMSIZ];
1081 	char			psk_label[PF_RULE_LABEL_SIZE];
1082 	u_int			psk_killed;
1083 };
1084 
1085 struct pfioc_states {
1086 	int	ps_len;
1087 	union {
1088 		caddr_t			 psu_buf;
1089 		struct pfsync_state	*psu_states;
1090 	} ps_u;
1091 #define ps_buf		ps_u.psu_buf
1092 #define ps_states	ps_u.psu_states
1093 };
1094 
1095 struct pfioc_src_nodes {
1096 	int	psn_len;
1097 	union {
1098 		caddr_t		 psu_buf;
1099 		struct pf_src_node	*psu_src_nodes;
1100 	} psn_u;
1101 #define psn_buf		psn_u.psu_buf
1102 #define psn_src_nodes	psn_u.psu_src_nodes
1103 };
1104 
1105 struct pfioc_if {
1106 	char		 ifname[IFNAMSIZ];
1107 };
1108 
1109 struct pfioc_tm {
1110 	int		 timeout;
1111 	int		 seconds;
1112 };
1113 
1114 struct pfioc_limit {
1115 	int		 index;
1116 	unsigned	 limit;
1117 };
1118 
1119 struct pfioc_altq_v0 {
1120 	u_int32_t	 action;
1121 	u_int32_t	 ticket;
1122 	u_int32_t	 nr;
1123 	struct pf_altq_v0 altq;
1124 };
1125 
1126 struct pfioc_altq_v1 {
1127 	u_int32_t	 action;
1128 	u_int32_t	 ticket;
1129 	u_int32_t	 nr;
1130 	/*
1131 	 * Placed here so code that only uses the above parameters can be
1132 	 * written entirely in terms of the v0 or v1 type.
1133 	 */
1134 	u_int32_t	 version;
1135 	struct pf_altq_v1 altq;
1136 };
1137 
1138 /*
1139  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1140  * the latest version of struct pf_altq_vX as it has that struct as a
1141  * member.
1142  */
1143 #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION
1144 
1145 struct pfioc_qstats_v0 {
1146 	u_int32_t	 ticket;
1147 	u_int32_t	 nr;
1148 	void		*buf;
1149 	int		 nbytes;
1150 	u_int8_t	 scheduler;
1151 };
1152 
1153 struct pfioc_qstats_v1 {
1154 	u_int32_t	 ticket;
1155 	u_int32_t	 nr;
1156 	void		*buf;
1157 	int		 nbytes;
1158 	u_int8_t	 scheduler;
1159 	/*
1160 	 * Placed here so code that only uses the above parameters can be
1161 	 * written entirely in terms of the v0 or v1 type.
1162 	 */
1163 	u_int32_t	 version;  /* Requested version of stats struct */
1164 };
1165 
1166 /* Latest version of struct pfioc_qstats_vX */
1167 #define PFIOC_QSTATS_VERSION	1
1168 
1169 struct pfioc_ruleset {
1170 	u_int32_t	 nr;
1171 	char		 path[MAXPATHLEN];
1172 	char		 name[PF_ANCHOR_NAME_SIZE];
1173 };
1174 
1175 #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
1176 #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1177 struct pfioc_trans {
1178 	int		 size;	/* number of elements */
1179 	int		 esize; /* size of each element in bytes */
1180 	struct pfioc_trans_e {
1181 		int		rs_num;
1182 		char		anchor[MAXPATHLEN];
1183 		u_int32_t	ticket;
1184 	}		*array;
1185 };
1186 
1187 #define PFR_FLAG_ATOMIC		0x00000001	/* unused */
1188 #define PFR_FLAG_DUMMY		0x00000002
1189 #define PFR_FLAG_FEEDBACK	0x00000004
1190 #define PFR_FLAG_CLSTATS	0x00000008
1191 #define PFR_FLAG_ADDRSTOO	0x00000010
1192 #define PFR_FLAG_REPLACE	0x00000020
1193 #define PFR_FLAG_ALLRSETS	0x00000040
1194 #define PFR_FLAG_ALLMASK	0x0000007F
1195 #ifdef _KERNEL
1196 #define PFR_FLAG_USERIOCTL	0x10000000
1197 #endif
1198 
1199 struct pfioc_table {
1200 	struct pfr_table	 pfrio_table;
1201 	void			*pfrio_buffer;
1202 	int			 pfrio_esize;
1203 	int			 pfrio_size;
1204 	int			 pfrio_size2;
1205 	int			 pfrio_nadd;
1206 	int			 pfrio_ndel;
1207 	int			 pfrio_nchange;
1208 	int			 pfrio_flags;
1209 	u_int32_t		 pfrio_ticket;
1210 };
1211 #define	pfrio_exists	pfrio_nadd
1212 #define	pfrio_nzero	pfrio_nadd
1213 #define	pfrio_nmatch	pfrio_nadd
1214 #define pfrio_naddr	pfrio_size2
1215 #define pfrio_setflag	pfrio_size2
1216 #define pfrio_clrflag	pfrio_nadd
1217 
1218 struct pfioc_iface {
1219 	char	 pfiio_name[IFNAMSIZ];
1220 	void	*pfiio_buffer;
1221 	int	 pfiio_esize;
1222 	int	 pfiio_size;
1223 	int	 pfiio_nzero;
1224 	int	 pfiio_flags;
1225 };
1226 
1227 /*
1228  * ioctl operations
1229  */
1230 
1231 #define DIOCSTART	_IO  ('D',  1)
1232 #define DIOCSTOP	_IO  ('D',  2)
1233 #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
1234 #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
1235 #define DIOCGETRULE	_IOWR('D',  7, struct pfioc_rule)
1236 /* XXX cut 8 - 17 */
1237 #define DIOCCLRSTATES	_IOWR('D', 18, struct pfioc_state_kill)
1238 #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
1239 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1240 #define DIOCGETSTATUS	_IOWR('D', 21, struct pf_status)
1241 #define DIOCCLRSTATUS	_IO  ('D', 22)
1242 #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
1243 #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
1244 #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1245 #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
1246 /* XXX cut 26 - 28 */
1247 #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
1248 #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1249 #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1250 #define DIOCCLRRULECTRS	_IO  ('D', 38)
1251 #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
1252 #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
1253 #define DIOCKILLSTATES	_IOWR('D', 41, struct pfioc_state_kill)
1254 #define DIOCSTARTALTQ	_IO  ('D', 42)
1255 #define DIOCSTOPALTQ	_IO  ('D', 43)
1256 #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0)
1257 #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1)
1258 #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0)
1259 #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1)
1260 #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0)
1261 #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1)
1262 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1263 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1264 #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0)
1265 #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1)
1266 #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
1267 #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
1268 #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
1269 #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1270 #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
1271 /* XXX cut 55 - 57 */
1272 #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
1273 #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
1274 #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
1275 #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
1276 #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
1277 #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
1278 #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
1279 #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table)
1280 #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
1281 #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
1282 #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
1283 #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
1284 #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
1285 #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
1286 #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table)
1287 #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
1288 #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
1289 #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
1290 #define	DIOCOSFPFLUSH	_IO('D', 78)
1291 #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
1292 #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
1293 #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans)
1294 #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans)
1295 #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans)
1296 #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
1297 #define	DIOCCLRSRCNODES	_IO('D', 85)
1298 #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
1299 #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
1300 #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
1301 #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
1302 #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
1303 struct pf_ifspeed_v0 {
1304 	char			ifname[IFNAMSIZ];
1305 	u_int32_t		baudrate;
1306 };
1307 
1308 struct pf_ifspeed_v1 {
1309 	char			ifname[IFNAMSIZ];
1310 	u_int32_t		baudrate32;
1311 	/* layout identical to struct pf_ifspeed_v0 up to this point */
1312 	u_int64_t		baudrate;
1313 };
1314 
1315 /* Latest version of struct pf_ifspeed_vX */
1316 #define PF_IFSPEED_VERSION	1
1317 
1318 #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0)
1319 #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1)
1320 
1321 /*
1322  * Compatibility and convenience macros
1323  */
1324 #ifndef _KERNEL
1325 #ifdef PFIOC_USE_LATEST
1326 /*
1327  * Maintaining in-tree consumers of the ioctl interface is easier when that
1328  * code can be written in terms old names that refer to the latest interface
1329  * version as that reduces the required changes in the consumers to those
1330  * that are functionally necessary to accommodate a new interface version.
1331  */
1332 #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
1333 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
1334 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
1335 
1336 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
1337 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
1338 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
1339 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
1340 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
1341 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
1342 #else
1343 /*
1344  * When building out-of-tree code that is written for the old interface,
1345  * such as may exist in ports for example, resolve the old struct tags and
1346  * ioctl command names to the v0 versions.
1347  */
1348 #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0)
1349 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0)
1350 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0)
1351 
1352 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0)
1353 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0)
1354 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0)
1355 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0)
1356 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0)
1357 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0)
1358 #endif /* PFIOC_USE_LATEST */
1359 #endif /* _KERNEL */
1360 
1361 #ifdef _KERNEL
1362 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node);
1363 struct pf_srchash {
1364 	struct pf_ksrc_node_list		nodes;
1365 	struct mtx			lock;
1366 };
1367 
1368 struct pf_keyhash {
1369 	LIST_HEAD(, pf_state_key)	keys;
1370 	struct mtx			lock;
1371 };
1372 
1373 struct pf_idhash {
1374 	LIST_HEAD(, pf_state)		states;
1375 	struct mtx			lock;
1376 };
1377 
1378 extern u_long		pf_hashmask;
1379 extern u_long		pf_srchashmask;
1380 #define	PF_HASHSIZ	(131072)
1381 #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4)
1382 VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
1383 VNET_DECLARE(struct pf_idhash *, pf_idhash);
1384 #define V_pf_keyhash	VNET(pf_keyhash)
1385 #define	V_pf_idhash	VNET(pf_idhash)
1386 VNET_DECLARE(struct pf_srchash *, pf_srchash);
1387 #define	V_pf_srchash	VNET(pf_srchash)
1388 
1389 #define PF_IDHASH(s)	(be64toh((s)->id) % (pf_hashmask + 1))
1390 
1391 VNET_DECLARE(void *, pf_swi_cookie);
1392 #define V_pf_swi_cookie	VNET(pf_swi_cookie)
1393 VNET_DECLARE(struct intr_event *, pf_swi_ie);
1394 #define	V_pf_swi_ie	VNET(pf_swi_ie)
1395 
1396 VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]);
1397 #define	V_pf_stateid	VNET(pf_stateid)
1398 
1399 TAILQ_HEAD(pf_altqqueue, pf_altq);
1400 VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]);
1401 #define	V_pf_altqs			 VNET(pf_altqs)
1402 VNET_DECLARE(struct pf_kpalist,		 pf_pabuf);
1403 #define	V_pf_pabuf			 VNET(pf_pabuf)
1404 
1405 VNET_DECLARE(u_int32_t,			 ticket_altqs_active);
1406 #define	V_ticket_altqs_active		 VNET(ticket_altqs_active)
1407 VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive);
1408 #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive)
1409 VNET_DECLARE(int,			 altqs_inactive_open);
1410 #define	V_altqs_inactive_open		 VNET(altqs_inactive_open)
1411 VNET_DECLARE(u_int32_t,			 ticket_pabuf);
1412 #define	V_ticket_pabuf			 VNET(ticket_pabuf)
1413 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active);
1414 #define	V_pf_altqs_active		 VNET(pf_altqs_active)
1415 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active);
1416 #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active)
1417 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive);
1418 #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive)
1419 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
1420 #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive)
1421 
1422 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules);
1423 #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules)
1424 
1425 void				 pf_initialize(void);
1426 void				 pf_mtag_initialize(void);
1427 void				 pf_mtag_cleanup(void);
1428 void				 pf_cleanup(void);
1429 
1430 struct pf_mtag			*pf_get_mtag(struct mbuf *);
1431 
1432 extern void			 pf_calc_skip_steps(struct pf_krulequeue *);
1433 #ifdef ALTQ
1434 extern	void			 pf_altq_ifnet_event(struct ifnet *, int);
1435 #endif
1436 VNET_DECLARE(uma_zone_t,	 pf_state_z);
1437 #define	V_pf_state_z		 VNET(pf_state_z)
1438 VNET_DECLARE(uma_zone_t,	 pf_state_key_z);
1439 #define	V_pf_state_key_z	 VNET(pf_state_key_z)
1440 VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z);
1441 #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z)
1442 
1443 extern void			 pf_purge_thread(void *);
1444 extern void			 pf_unload_vnet_purge(void);
1445 extern void			 pf_intr(void *);
1446 extern void			 pf_purge_expired_src_nodes(void);
1447 
1448 extern int			 pf_unlink_state(struct pf_state *, u_int);
1449 #define	PF_ENTER_LOCKED		0x00000001
1450 #define	PF_RETURN_LOCKED	0x00000002
1451 extern int			 pf_state_insert(struct pfi_kkif *,
1452 				    struct pf_state_key *,
1453 				    struct pf_state_key *,
1454 				    struct pf_state *);
1455 extern void			 pf_free_state(struct pf_state *);
1456 
1457 static __inline void
1458 pf_ref_state(struct pf_state *s)
1459 {
1460 
1461 	refcount_acquire(&s->refs);
1462 }
1463 
1464 static __inline int
1465 pf_release_state(struct pf_state *s)
1466 {
1467 
1468 	if (refcount_release(&s->refs)) {
1469 		pf_free_state(s);
1470 		return (1);
1471 	} else
1472 		return (0);
1473 }
1474 
1475 extern struct pf_state		*pf_find_state_byid(uint64_t, uint32_t);
1476 extern struct pf_state		*pf_find_state_all(struct pf_state_key_cmp *,
1477 				    u_int, int *);
1478 extern struct pf_ksrc_node	*pf_find_src_node(struct pf_addr *,
1479 				    struct pf_krule *, sa_family_t, int);
1480 extern void			 pf_unlink_src_node(struct pf_ksrc_node *);
1481 extern u_int			 pf_free_src_nodes(struct pf_ksrc_node_list *);
1482 extern void			 pf_print_state(struct pf_state *);
1483 extern void			 pf_print_flags(u_int8_t);
1484 extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
1485 				    u_int8_t);
1486 extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
1487 				    u_int16_t, u_int16_t, u_int8_t);
1488 
1489 VNET_DECLARE(struct ifnet *,		 sync_ifp);
1490 #define	V_sync_ifp		 	 VNET(sync_ifp);
1491 VNET_DECLARE(struct pf_krule,		 pf_default_rule);
1492 #define	V_pf_default_rule		  VNET(pf_default_rule)
1493 extern void			 pf_addrcpy(struct pf_addr *, struct pf_addr *,
1494 				    u_int8_t);
1495 void				pf_free_rule(struct pf_krule *);
1496 
1497 #ifdef INET
1498 int	pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1499 int	pf_normalize_ip(struct mbuf **, int, struct pfi_kkif *, u_short *,
1500 	    struct pf_pdesc *);
1501 #endif /* INET */
1502 
1503 #ifdef INET6
1504 int	pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1505 int	pf_normalize_ip6(struct mbuf **, int, struct pfi_kkif *, u_short *,
1506 	    struct pf_pdesc *);
1507 void	pf_poolmask(struct pf_addr *, struct pf_addr*,
1508 	    struct pf_addr *, struct pf_addr *, u_int8_t);
1509 void	pf_addr_inc(struct pf_addr *, sa_family_t);
1510 int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *);
1511 #endif /* INET6 */
1512 
1513 u_int32_t	pf_new_isn(struct pf_state *);
1514 void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
1515 	    sa_family_t);
1516 void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
1517 void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
1518 	    u_int8_t);
1519 void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
1520 void	pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t,
1521 	    bool, u_int8_t);
1522 void	pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t,
1523     bool, u_int8_t);
1524 void	pf_send_deferred_syn(struct pf_state *);
1525 int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
1526 	    struct pf_addr *, sa_family_t);
1527 int	pf_match_addr_range(struct pf_addr *, struct pf_addr *,
1528 	    struct pf_addr *, sa_family_t);
1529 int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
1530 
1531 void	pf_normalize_init(void);
1532 void	pf_normalize_cleanup(void);
1533 int	pf_normalize_tcp(int, struct pfi_kkif *, struct mbuf *, int, int, void *,
1534 	    struct pf_pdesc *);
1535 void	pf_normalize_tcp_cleanup(struct pf_state *);
1536 int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
1537 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
1538 int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
1539 	    u_short *, struct tcphdr *, struct pf_state *,
1540 	    struct pf_state_peer *, struct pf_state_peer *, int *);
1541 u_int32_t
1542 	pf_state_expires(const struct pf_state *);
1543 void	pf_purge_expired_fragments(void);
1544 void	pf_purge_fragments(uint32_t);
1545 int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *,
1546 	    int);
1547 int	pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *);
1548 struct pf_state_key *pf_alloc_state_key(int);
1549 void	pfr_initialize(void);
1550 void	pfr_cleanup(void);
1551 int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
1552 void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
1553 	    u_int64_t, int, int, int);
1554 int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
1555 void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
1556 struct pfr_ktable *
1557 	pfr_attach_table(struct pf_kruleset *, char *);
1558 void	pfr_detach_table(struct pfr_ktable *);
1559 int	pfr_clr_tables(struct pfr_table *, int *, int);
1560 int	pfr_add_tables(struct pfr_table *, int, int *, int);
1561 int	pfr_del_tables(struct pfr_table *, int, int *, int);
1562 int	pfr_table_count(struct pfr_table *, int);
1563 int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
1564 int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
1565 int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
1566 int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
1567 int	pfr_clr_addrs(struct pfr_table *, int *, int);
1568 int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
1569 int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1570 	    int);
1571 int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1572 	    int);
1573 int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1574 	    int *, int *, int *, int, u_int32_t);
1575 int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
1576 int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
1577 int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
1578 	    int);
1579 int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1580 	    int);
1581 int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
1582 int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
1583 int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
1584 int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
1585 	    int *, u_int32_t, int);
1586 
1587 MALLOC_DECLARE(PFI_MTYPE);
1588 VNET_DECLARE(struct pfi_kkif *,		 pfi_all);
1589 #define	V_pfi_all	 		 VNET(pfi_all)
1590 
1591 void		 pfi_initialize(void);
1592 void		 pfi_initialize_vnet(void);
1593 void		 pfi_cleanup(void);
1594 void		 pfi_cleanup_vnet(void);
1595 void		 pfi_kkif_ref(struct pfi_kkif *);
1596 void		 pfi_kkif_unref(struct pfi_kkif *);
1597 struct pfi_kkif	*pfi_kkif_find(const char *);
1598 struct pfi_kkif	*pfi_kkif_attach(struct pfi_kkif *, const char *);
1599 int		 pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *);
1600 void		 pfi_kkif_purge(void);
1601 int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
1602 		    sa_family_t);
1603 int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
1604 void		 pfi_dynaddr_remove(struct pfi_dynaddr *);
1605 void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
1606 void		 pfi_update_status(const char *, struct pf_status *);
1607 void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
1608 int		 pfi_set_flags(const char *, int);
1609 int		 pfi_clear_flags(const char *, int);
1610 
1611 int		 pf_match_tag(struct mbuf *, struct pf_krule *, int *, int);
1612 int		 pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
1613 int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *,
1614 		    sa_family_t);
1615 void		 pf_qid2qname(u_int32_t, char *);
1616 
1617 VNET_DECLARE(struct pf_kstatus, pf_status);
1618 #define	V_pf_status	VNET(pf_status)
1619 
1620 struct pf_limit {
1621 	uma_zone_t	zone;
1622 	u_int		limit;
1623 };
1624 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
1625 #define	V_pf_limits VNET(pf_limits)
1626 
1627 #endif /* _KERNEL */
1628 
1629 #ifdef _KERNEL
1630 VNET_DECLARE(struct pf_kanchor_global,		 pf_anchors);
1631 #define	V_pf_anchors				 VNET(pf_anchors)
1632 VNET_DECLARE(struct pf_kanchor,			 pf_main_anchor);
1633 #define	V_pf_main_anchor			 VNET(pf_main_anchor)
1634 #define pf_main_ruleset	V_pf_main_anchor.ruleset
1635 
1636 void			 pf_init_kruleset(struct pf_kruleset *);
1637 int			 pf_kanchor_setup(struct pf_krule *,
1638 			    const struct pf_kruleset *, const char *);
1639 int			 pf_kanchor_copyout(const struct pf_kruleset *,
1640 			    const struct pf_krule *, struct pfioc_rule *);
1641 void			 pf_kanchor_remove(struct pf_krule *);
1642 void			 pf_remove_if_empty_kruleset(struct pf_kruleset *);
1643 struct pf_kruleset	*pf_find_kruleset(const char *);
1644 struct pf_kruleset	*pf_find_or_create_kruleset(const char *);
1645 void			 pf_rs_initialize(void);
1646 
1647 void			 pf_krule_free(struct pf_krule *);
1648 #endif
1649 
1650 /* The fingerprint functions can be linked into userland programs (tcpdump) */
1651 int	pf_osfp_add(struct pf_osfp_ioctl *);
1652 #ifdef _KERNEL
1653 struct pf_osfp_enlist *
1654 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
1655 	    const struct tcphdr *);
1656 #endif /* _KERNEL */
1657 void	pf_osfp_flush(void);
1658 int	pf_osfp_get(struct pf_osfp_ioctl *);
1659 int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
1660 
1661 #ifdef _KERNEL
1662 void			 pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
1663 
1664 void			 pf_step_into_anchor(struct pf_kanchor_stackframe *, int *,
1665 			    struct pf_kruleset **, int, struct pf_krule **,
1666 			    struct pf_krule **, int *);
1667 int			 pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *,
1668 			    struct pf_kruleset **, int, struct pf_krule **,
1669 			    struct pf_krule **, int *);
1670 
1671 int			 pf_map_addr(u_int8_t, struct pf_krule *,
1672 			    struct pf_addr *, struct pf_addr *,
1673 			    struct pf_addr *, struct pf_ksrc_node **);
1674 struct pf_krule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
1675 			    int, int, struct pfi_kkif *, struct pf_ksrc_node **,
1676 			    struct pf_state_key **, struct pf_state_key **,
1677 			    struct pf_addr *, struct pf_addr *,
1678 			    uint16_t, uint16_t, struct pf_kanchor_stackframe *);
1679 
1680 struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
1681 			    struct pf_addr *, u_int16_t, u_int16_t);
1682 struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
1683 
1684 struct pfi_kkif		*pf_kkif_create(int);
1685 void			 pf_kkif_free(struct pfi_kkif *);
1686 void			 pf_kkif_zero(struct pfi_kkif *);
1687 #endif /* _KERNEL */
1688 
1689 #endif /* _NET_PFVAR_H_ */
1690