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