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