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