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