xref: /freebsd/sys/netpfil/pf/pf.h (revision 21b492ed51aa6ff8008a8aa83333b1de30288a15)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel Hartmeier
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *	$OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $
32  *	$FreeBSD$
33  */
34 
35 #ifndef	_NET_PF_H_
36 #define	_NET_PF_H_
37 
38 #include <sys/tree.h>
39 
40 #define	PF_TCPS_PROXY_SRC	((TCP_NSTATES)+0)
41 #define	PF_TCPS_PROXY_DST	((TCP_NSTATES)+1)
42 
43 #define	PF_MD5_DIGEST_LENGTH	16
44 #ifdef MD5_DIGEST_LENGTH
45 #if PF_MD5_DIGEST_LENGTH != MD5_DIGEST_LENGTH
46 #error
47 #endif
48 #endif
49 
50 enum	{ PF_INOUT, PF_IN, PF_OUT };
51 enum	{ PF_PASS, PF_DROP, PF_SCRUB, PF_NOSCRUB, PF_NAT, PF_NONAT,
52 	  PF_BINAT, PF_NOBINAT, PF_RDR, PF_NORDR, PF_SYNPROXY_DROP, PF_DEFER,
53 	  PF_MATCH };
54 enum	{ PF_RULESET_SCRUB, PF_RULESET_FILTER, PF_RULESET_NAT,
55 	  PF_RULESET_BINAT, PF_RULESET_RDR, PF_RULESET_MAX };
56 enum	{ PF_OP_NONE, PF_OP_IRG, PF_OP_EQ, PF_OP_NE, PF_OP_LT,
57 	  PF_OP_LE, PF_OP_GT, PF_OP_GE, PF_OP_XRG, PF_OP_RRG };
58 enum	{ PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC, PF_DEBUG_NOISY };
59 enum	{ PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
60 	  PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
61 	  PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
62 enum	{ PF_GET_NONE, PF_GET_CLR_CNTR };
63 enum	{ PF_SK_WIRE, PF_SK_STACK, PF_SK_BOTH };
64 
65 /*
66  * Note about PFTM_*: real indices into pf_rule.timeout[] come before
67  * PFTM_MAX, special cases afterwards. See pf_state_expires().
68  */
69 enum	{ PFTM_TCP_FIRST_PACKET, PFTM_TCP_OPENING, PFTM_TCP_ESTABLISHED,
70 	  PFTM_TCP_CLOSING, PFTM_TCP_FIN_WAIT, PFTM_TCP_CLOSED,
71 	  PFTM_UDP_FIRST_PACKET, PFTM_UDP_SINGLE, PFTM_UDP_MULTIPLE,
72 	  PFTM_ICMP_FIRST_PACKET, PFTM_ICMP_ERROR_REPLY,
73 	  PFTM_OTHER_FIRST_PACKET, PFTM_OTHER_SINGLE,
74 	  PFTM_OTHER_MULTIPLE, PFTM_FRAG, PFTM_INTERVAL,
75 	  PFTM_ADAPTIVE_START, PFTM_ADAPTIVE_END, PFTM_SRC_NODE,
76 	  PFTM_TS_DIFF, PFTM_MAX, PFTM_PURGE, PFTM_UNLINKED };
77 
78 /* PFTM default values */
79 #define PFTM_TCP_FIRST_PACKET_VAL	120	/* First TCP packet */
80 #define PFTM_TCP_OPENING_VAL		30	/* No response yet */
81 #define PFTM_TCP_ESTABLISHED_VAL	24*60*60/* Established */
82 #define PFTM_TCP_CLOSING_VAL		15 * 60	/* Half closed */
83 #define PFTM_TCP_FIN_WAIT_VAL		45	/* Got both FINs */
84 #define PFTM_TCP_CLOSED_VAL		90	/* Got a RST */
85 #define PFTM_UDP_FIRST_PACKET_VAL	60	/* First UDP packet */
86 #define PFTM_UDP_SINGLE_VAL		30	/* Unidirectional */
87 #define PFTM_UDP_MULTIPLE_VAL		60	/* Bidirectional */
88 #define PFTM_ICMP_FIRST_PACKET_VAL	20	/* First ICMP packet */
89 #define PFTM_ICMP_ERROR_REPLY_VAL	10	/* Got error response */
90 #define PFTM_OTHER_FIRST_PACKET_VAL	60	/* First packet */
91 #define PFTM_OTHER_SINGLE_VAL		30	/* Unidirectional */
92 #define PFTM_OTHER_MULTIPLE_VAL		60	/* Bidirectional */
93 #define PFTM_FRAG_VAL			30	/* Fragment expire */
94 #define PFTM_INTERVAL_VAL		10	/* Expire interval */
95 #define PFTM_SRC_NODE_VAL		0	/* Source tracking */
96 #define PFTM_TS_DIFF_VAL		30	/* Allowed TS diff */
97 
98 enum	{ PF_NOPFROUTE, PF_FASTROUTE, PF_ROUTETO, PF_DUPTO, PF_REPLYTO };
99 enum	{ PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
100 	  PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
101 #define PF_POOL_IDMASK		0x0f
102 enum	{ PF_POOL_NONE, PF_POOL_BITMASK, PF_POOL_RANDOM,
103 	  PF_POOL_SRCHASH, PF_POOL_ROUNDROBIN };
104 enum	{ PF_ADDR_ADDRMASK, PF_ADDR_NOROUTE, PF_ADDR_DYNIFTL,
105 	  PF_ADDR_TABLE, PF_ADDR_URPFFAILED,
106 	  PF_ADDR_RANGE };
107 #define PF_POOL_TYPEMASK	0x0f
108 #define PF_POOL_STICKYADDR	0x20
109 #define	PF_WSCALE_FLAG		0x80
110 #define	PF_WSCALE_MASK		0x0f
111 
112 #define	PF_LOG			0x01
113 #define	PF_LOG_ALL		0x02
114 #define	PF_LOG_SOCKET_LOOKUP	0x04
115 
116 /* Reasons code for passing/dropping a packet */
117 #define PFRES_MATCH	0		/* Explicit match of a rule */
118 #define PFRES_BADOFF	1		/* Bad offset for pull_hdr */
119 #define PFRES_FRAG	2		/* Dropping following fragment */
120 #define PFRES_SHORT	3		/* Dropping short packet */
121 #define PFRES_NORM	4		/* Dropping by normalizer */
122 #define PFRES_MEMORY	5		/* Dropped due to lacking mem */
123 #define PFRES_TS	6		/* Bad TCP Timestamp (RFC1323) */
124 #define PFRES_CONGEST	7		/* Congestion (of ipintrq) */
125 #define PFRES_IPOPTIONS 8		/* IP option */
126 #define PFRES_PROTCKSUM 9		/* Protocol checksum invalid */
127 #define PFRES_BADSTATE	10		/* State mismatch */
128 #define PFRES_STATEINS	11		/* State insertion failure */
129 #define PFRES_MAXSTATES	12		/* State limit */
130 #define PFRES_SRCLIMIT	13		/* Source node/conn limit */
131 #define PFRES_SYNPROXY	14		/* SYN proxy */
132 #define PFRES_MAPFAILED	15		/* pf_map_addr() failed */
133 #define PFRES_MAX	16		/* total+1 */
134 
135 #define PFRES_NAMES { \
136 	"match", \
137 	"bad-offset", \
138 	"fragment", \
139 	"short", \
140 	"normalize", \
141 	"memory", \
142 	"bad-timestamp", \
143 	"congestion", \
144 	"ip-option", \
145 	"proto-cksum", \
146 	"state-mismatch", \
147 	"state-insert", \
148 	"state-limit", \
149 	"src-limit", \
150 	"synproxy", \
151 	"map-failed", \
152 	NULL \
153 }
154 
155 /* Counters for other things we want to keep track of */
156 #define LCNT_STATES		0	/* states */
157 #define LCNT_SRCSTATES		1	/* max-src-states */
158 #define LCNT_SRCNODES		2	/* max-src-nodes */
159 #define LCNT_SRCCONN		3	/* max-src-conn */
160 #define LCNT_SRCCONNRATE	4	/* max-src-conn-rate */
161 #define LCNT_OVERLOAD_TABLE	5	/* entry added to overload table */
162 #define LCNT_OVERLOAD_FLUSH	6	/* state entries flushed */
163 #define LCNT_MAX		7	/* total+1 */
164 
165 #define LCNT_NAMES { \
166 	"max states per rule", \
167 	"max-src-states", \
168 	"max-src-nodes", \
169 	"max-src-conn", \
170 	"max-src-conn-rate", \
171 	"overload table insertion", \
172 	"overload flush states", \
173 	NULL \
174 }
175 
176 /* state operation counters */
177 #define FCNT_STATE_SEARCH	0
178 #define FCNT_STATE_INSERT	1
179 #define FCNT_STATE_REMOVALS	2
180 #define FCNT_MAX		3
181 
182 #ifdef _KERNEL
183 #define FCNT_NAMES { \
184 	"searches", \
185 	"inserts", \
186 	"removals", \
187 	NULL \
188 }
189 #endif
190 
191 /* src_node operation counters */
192 #define SCNT_SRC_NODE_SEARCH	0
193 #define SCNT_SRC_NODE_INSERT	1
194 #define SCNT_SRC_NODE_REMOVALS	2
195 #define SCNT_MAX		3
196 
197 #define	PF_TABLE_NAME_SIZE	32
198 #define	PF_QNAME_SIZE		64
199 
200 struct pfioc_nv {
201 	void            *data;
202 	size_t           len;   /* The length of the nvlist data. */
203 	size_t           size;  /* The total size of the data buffer. */
204 };
205 
206 struct pf_rule;
207 
208 /* keep synced with pfi_kif, used in RB_FIND */
209 struct pfi_kif_cmp {
210 	char				 pfik_name[IFNAMSIZ];
211 };
212 
213 struct pfi_kif {
214 	char				 pfik_name[IFNAMSIZ];
215 	union {
216 		RB_ENTRY(pfi_kif)	 _pfik_tree;
217 		LIST_ENTRY(pfi_kif)	 _pfik_list;
218 	} _pfik_glue;
219 #define	pfik_tree	_pfik_glue._pfik_tree
220 #define	pfik_list	_pfik_glue._pfik_list
221 	u_int64_t			 pfik_packets[2][2][2];
222 	u_int64_t			 pfik_bytes[2][2][2];
223 	u_int32_t			 pfik_tzero;
224 	u_int				 pfik_flags;
225 	struct ifnet			*pfik_ifp;
226 	struct ifg_group		*pfik_group;
227 	u_int				 pfik_rulerefs;
228 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
229 };
230 
231 struct pf_status {
232 	uint64_t	counters[PFRES_MAX];
233 	uint64_t	lcounters[LCNT_MAX];
234 	uint64_t	fcounters[FCNT_MAX];
235 	uint64_t	scounters[SCNT_MAX];
236 	uint64_t	pcounters[2][2][3];
237 	uint64_t	bcounters[2][2];
238 	uint32_t	running;
239 	uint32_t	states;
240 	uint32_t	src_nodes;
241 	uint32_t	since;
242 	uint32_t	debug;
243 	uint32_t	hostid;
244 	char		ifname[IFNAMSIZ];
245 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
246 };
247 
248 struct pf_addr {
249 	union {
250 		struct in_addr		v4;
251 		struct in6_addr		v6;
252 		u_int8_t		addr8[16];
253 		u_int16_t		addr16[8];
254 		u_int32_t		addr32[4];
255 	} pfa;		    /* 128-bit address */
256 #define v4	pfa.v4
257 #define v6	pfa.v6
258 #define addr8	pfa.addr8
259 #define addr16	pfa.addr16
260 #define addr32	pfa.addr32
261 };
262 
263 #define PFI_AFLAG_NETWORK	0x01
264 #define PFI_AFLAG_BROADCAST	0x02
265 #define PFI_AFLAG_PEER		0x04
266 #define PFI_AFLAG_MODEMASK	0x07
267 #define PFI_AFLAG_NOALIAS	0x08
268 
269 struct pf_addr_wrap {
270 	union {
271 		struct {
272 			struct pf_addr		 addr;
273 			struct pf_addr		 mask;
274 		}			 a;
275 		char			 ifname[IFNAMSIZ];
276 		char			 tblname[PF_TABLE_NAME_SIZE];
277 	}			 v;
278 	union {
279 		struct pfi_dynaddr	*dyn;
280 		struct pfr_ktable	*tbl;
281 		int			 dyncnt;
282 		int			 tblcnt;
283 	}			 p;
284 	u_int8_t		 type;		/* PF_ADDR_* */
285 	u_int8_t		 iflags;	/* PFI_AFLAG_* */
286 };
287 
288 union pf_rule_ptr {
289 	struct pf_rule		*ptr;
290 	u_int32_t		 nr;
291 };
292 
293 struct pf_rule_uid {
294 	uid_t		 uid[2];
295 	u_int8_t	 op;
296 };
297 
298 struct pf_rule_gid {
299 	uid_t		 gid[2];
300 	u_int8_t	 op;
301 };
302 
303 struct pf_rule_addr {
304 	struct pf_addr_wrap	 addr;
305 	u_int16_t		 port[2];
306 	u_int8_t		 neg;
307 	u_int8_t		 port_op;
308 };
309 
310 struct pf_pooladdr {
311 	struct pf_addr_wrap		 addr;
312 	TAILQ_ENTRY(pf_pooladdr)	 entries;
313 	char				 ifname[IFNAMSIZ];
314 	struct pfi_kif			*kif;
315 };
316 
317 TAILQ_HEAD(pf_palist, pf_pooladdr);
318 
319 struct pf_poolhashkey {
320 	union {
321 		u_int8_t		key8[16];
322 		u_int16_t		key16[8];
323 		u_int32_t		key32[4];
324 	} pfk;		    /* 128-bit hash key */
325 #define key8	pfk.key8
326 #define key16	pfk.key16
327 #define key32	pfk.key32
328 };
329 
330 struct pf_mape_portset {
331 	u_int8_t		offset;
332 	u_int8_t		psidlen;
333 	u_int16_t		psid;
334 };
335 
336 struct pf_pool {
337 	struct pf_palist	 list;
338 	struct pf_pooladdr	*cur;
339 	struct pf_poolhashkey	 key;
340 	struct pf_addr		 counter;
341 	int			 tblidx;
342 	u_int16_t		 proxy_port[2];
343 	u_int8_t		 opts;
344 };
345 
346 /* A packed Operating System description for fingerprinting */
347 typedef u_int32_t pf_osfp_t;
348 #define PF_OSFP_ANY	((pf_osfp_t)0)
349 #define PF_OSFP_UNKNOWN	((pf_osfp_t)-1)
350 #define PF_OSFP_NOMATCH	((pf_osfp_t)-2)
351 
352 struct pf_osfp_entry {
353 	SLIST_ENTRY(pf_osfp_entry) fp_entry;
354 	pf_osfp_t		fp_os;
355 	int			fp_enflags;
356 #define PF_OSFP_EXPANDED	0x001		/* expanded entry */
357 #define PF_OSFP_GENERIC		0x002		/* generic signature */
358 #define PF_OSFP_NODETAIL	0x004		/* no p0f details */
359 #define PF_OSFP_LEN	32
360 	char			fp_class_nm[PF_OSFP_LEN];
361 	char			fp_version_nm[PF_OSFP_LEN];
362 	char			fp_subtype_nm[PF_OSFP_LEN];
363 };
364 #define PF_OSFP_ENTRY_EQ(a, b) \
365     ((a)->fp_os == (b)->fp_os && \
366     memcmp((a)->fp_class_nm, (b)->fp_class_nm, PF_OSFP_LEN) == 0 && \
367     memcmp((a)->fp_version_nm, (b)->fp_version_nm, PF_OSFP_LEN) == 0 && \
368     memcmp((a)->fp_subtype_nm, (b)->fp_subtype_nm, PF_OSFP_LEN) == 0)
369 
370 /* handle pf_osfp_t packing */
371 #define _FP_RESERVED_BIT	1  /* For the special negative #defines */
372 #define _FP_UNUSED_BITS		1
373 #define _FP_CLASS_BITS		10 /* OS Class (Windows, Linux) */
374 #define _FP_VERSION_BITS	10 /* OS version (95, 98, NT, 2.4.54, 3.2) */
375 #define _FP_SUBTYPE_BITS	10 /* patch level (NT SP4, SP3, ECN patch) */
376 #define PF_OSFP_UNPACK(osfp, class, version, subtype) do { \
377 	(class) = ((osfp) >> (_FP_VERSION_BITS+_FP_SUBTYPE_BITS)) & \
378 	    ((1 << _FP_CLASS_BITS) - 1); \
379 	(version) = ((osfp) >> _FP_SUBTYPE_BITS) & \
380 	    ((1 << _FP_VERSION_BITS) - 1);\
381 	(subtype) = (osfp) & ((1 << _FP_SUBTYPE_BITS) - 1); \
382 } while(0)
383 #define PF_OSFP_PACK(osfp, class, version, subtype) do { \
384 	(osfp) = ((class) & ((1 << _FP_CLASS_BITS) - 1)) << (_FP_VERSION_BITS \
385 	    + _FP_SUBTYPE_BITS); \
386 	(osfp) |= ((version) & ((1 << _FP_VERSION_BITS) - 1)) << \
387 	    _FP_SUBTYPE_BITS; \
388 	(osfp) |= (subtype) & ((1 << _FP_SUBTYPE_BITS) - 1); \
389 } while(0)
390 
391 /* the fingerprint of an OSes TCP SYN packet */
392 typedef u_int64_t	pf_tcpopts_t;
393 struct pf_os_fingerprint {
394 	SLIST_HEAD(pf_osfp_enlist, pf_osfp_entry) fp_oses; /* list of matches */
395 	pf_tcpopts_t		fp_tcpopts;	/* packed TCP options */
396 	u_int16_t		fp_wsize;	/* TCP window size */
397 	u_int16_t		fp_psize;	/* ip->ip_len */
398 	u_int16_t		fp_mss;		/* TCP MSS */
399 	u_int16_t		fp_flags;
400 #define PF_OSFP_WSIZE_MOD	0x0001		/* Window modulus */
401 #define PF_OSFP_WSIZE_DC	0x0002		/* Window don't care */
402 #define PF_OSFP_WSIZE_MSS	0x0004		/* Window multiple of MSS */
403 #define PF_OSFP_WSIZE_MTU	0x0008		/* Window multiple of MTU */
404 #define PF_OSFP_PSIZE_MOD	0x0010		/* packet size modulus */
405 #define PF_OSFP_PSIZE_DC	0x0020		/* packet size don't care */
406 #define PF_OSFP_WSCALE		0x0040		/* TCP window scaling */
407 #define PF_OSFP_WSCALE_MOD	0x0080		/* TCP window scale modulus */
408 #define PF_OSFP_WSCALE_DC	0x0100		/* TCP window scale dont-care */
409 #define PF_OSFP_MSS		0x0200		/* TCP MSS */
410 #define PF_OSFP_MSS_MOD		0x0400		/* TCP MSS modulus */
411 #define PF_OSFP_MSS_DC		0x0800		/* TCP MSS dont-care */
412 #define PF_OSFP_DF		0x1000		/* IPv4 don't fragment bit */
413 #define PF_OSFP_TS0		0x2000		/* Zero timestamp */
414 #define PF_OSFP_INET6		0x4000		/* IPv6 */
415 	u_int8_t		fp_optcnt;	/* TCP option count */
416 	u_int8_t		fp_wscale;	/* TCP window scaling */
417 	u_int8_t		fp_ttl;		/* IPv4 TTL */
418 #define PF_OSFP_MAXTTL_OFFSET	40
419 /* TCP options packing */
420 #define PF_OSFP_TCPOPT_NOP	0x0		/* TCP NOP option */
421 #define PF_OSFP_TCPOPT_WSCALE	0x1		/* TCP window scaling option */
422 #define PF_OSFP_TCPOPT_MSS	0x2		/* TCP max segment size opt */
423 #define PF_OSFP_TCPOPT_SACK	0x3		/* TCP SACK OK option */
424 #define PF_OSFP_TCPOPT_TS	0x4		/* TCP timestamp option */
425 #define PF_OSFP_TCPOPT_BITS	3		/* bits used by each option */
426 #define PF_OSFP_MAX_OPTS \
427     (sizeof(((struct pf_os_fingerprint *)0)->fp_tcpopts) * 8) \
428     / PF_OSFP_TCPOPT_BITS
429 
430 	SLIST_ENTRY(pf_os_fingerprint)	fp_next;
431 };
432 
433 struct pf_osfp_ioctl {
434 	struct pf_osfp_entry	fp_os;
435 	pf_tcpopts_t		fp_tcpopts;	/* packed TCP options */
436 	u_int16_t		fp_wsize;	/* TCP window size */
437 	u_int16_t		fp_psize;	/* ip->ip_len */
438 	u_int16_t		fp_mss;		/* TCP MSS */
439 	u_int16_t		fp_flags;
440 	u_int8_t		fp_optcnt;	/* TCP option count */
441 	u_int8_t		fp_wscale;	/* TCP window scaling */
442 	u_int8_t		fp_ttl;		/* IPv4 TTL */
443 
444 	int			fp_getnum;	/* DIOCOSFPGET number */
445 };
446 
447 #define	PF_ANCHOR_NAME_SIZE	 64
448 
449 struct pf_rule {
450 	struct pf_rule_addr	 src;
451 	struct pf_rule_addr	 dst;
452 #define PF_SKIP_IFP		0
453 #define PF_SKIP_DIR		1
454 #define PF_SKIP_AF		2
455 #define PF_SKIP_PROTO		3
456 #define PF_SKIP_SRC_ADDR	4
457 #define PF_SKIP_SRC_PORT	5
458 #define PF_SKIP_DST_ADDR	6
459 #define PF_SKIP_DST_PORT	7
460 #define PF_SKIP_COUNT		8
461 	union pf_rule_ptr	 skip[PF_SKIP_COUNT];
462 #define PF_RULE_LABEL_SIZE	 64
463 #define PF_RULE_MAX_LABEL_COUNT	 5
464 	char			 label[PF_RULE_LABEL_SIZE];
465 	char			 ifname[IFNAMSIZ];
466 	char			 qname[PF_QNAME_SIZE];
467 	char			 pqname[PF_QNAME_SIZE];
468 #define	PF_TAG_NAME_SIZE	 64
469 	char			 tagname[PF_TAG_NAME_SIZE];
470 	char			 match_tagname[PF_TAG_NAME_SIZE];
471 
472 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
473 
474 	TAILQ_ENTRY(pf_rule)	 entries;
475 	struct pf_pool		 rpool;
476 
477 	u_int64_t		 evaluations;
478 	u_int64_t		 packets[2];
479 	u_int64_t		 bytes[2];
480 
481 	struct pfi_kif		*kif;
482 	struct pf_anchor	*anchor;
483 	struct pfr_ktable	*overload_tbl;
484 
485 	pf_osfp_t		 os_fingerprint;
486 
487 	int			 rtableid;
488 	u_int32_t		 timeout[PFTM_MAX];
489 	u_int32_t		 max_states;
490 	u_int32_t		 max_src_nodes;
491 	u_int32_t		 max_src_states;
492 	u_int32_t		 max_src_conn;
493 	struct {
494 		u_int32_t		limit;
495 		u_int32_t		seconds;
496 	}			 max_src_conn_rate;
497 	u_int32_t		 qid;
498 	u_int32_t		 pqid;
499 	u_int32_t		 rt_listid;
500 	u_int32_t		 nr;
501 	u_int32_t		 prob;
502 	uid_t			 cuid;
503 	pid_t			 cpid;
504 
505 	counter_u64_t		 states_cur;
506 	counter_u64_t		 states_tot;
507 	counter_u64_t		 src_nodes;
508 
509 	u_int16_t		 return_icmp;
510 	u_int16_t		 return_icmp6;
511 	u_int16_t		 max_mss;
512 	u_int16_t		 tag;
513 	u_int16_t		 match_tag;
514 	u_int16_t		 scrub_flags;
515 
516 	struct pf_rule_uid	 uid;
517 	struct pf_rule_gid	 gid;
518 
519 	u_int32_t		 rule_flag;
520 	u_int8_t		 action;
521 	u_int8_t		 direction;
522 	u_int8_t		 log;
523 	u_int8_t		 logif;
524 	u_int8_t		 quick;
525 	u_int8_t		 ifnot;
526 	u_int8_t		 match_tag_not;
527 	u_int8_t		 natpass;
528 
529 #define PF_STATE_NORMAL		0x1
530 #define PF_STATE_MODULATE	0x2
531 #define PF_STATE_SYNPROXY	0x3
532 	u_int8_t		 keep_state;
533 	sa_family_t		 af;
534 	u_int8_t		 proto;
535 	u_int8_t		 type;
536 	u_int8_t		 code;
537 	u_int8_t		 flags;
538 	u_int8_t		 flagset;
539 	u_int8_t		 min_ttl;
540 	u_int8_t		 allow_opts;
541 	u_int8_t		 rt;
542 	u_int8_t		 return_ttl;
543 	u_int8_t		 tos;
544 	u_int8_t		 set_tos;
545 	u_int8_t		 anchor_relative;
546 	u_int8_t		 anchor_wildcard;
547 
548 #define PF_FLUSH		0x01
549 #define PF_FLUSH_GLOBAL		0x02
550 	u_int8_t		 flush;
551 #define PF_PRIO_ZERO		0xff		/* match "prio 0" packets */
552 #define PF_PRIO_MAX		7
553 	u_int8_t		 prio;
554 	u_int8_t		 set_prio[2];
555 
556 	struct {
557 		struct pf_addr		addr;
558 		u_int16_t		port;
559 	}			divert;
560 
561 	uint64_t		 u_states_cur;
562 	uint64_t		 u_states_tot;
563 	uint64_t		 u_src_nodes;
564 };
565 
566 /* rule flags */
567 #define	PFRULE_DROP		0x0000
568 #define	PFRULE_RETURNRST	0x0001
569 #define	PFRULE_FRAGMENT		0x0002
570 #define	PFRULE_RETURNICMP	0x0004
571 #define	PFRULE_RETURN		0x0008
572 #define	PFRULE_NOSYNC		0x0010
573 #define PFRULE_SRCTRACK		0x0020  /* track source states */
574 #define PFRULE_RULESRCTRACK	0x0040  /* per rule */
575 
576 #ifdef _KERNEL
577 #define	PFRULE_REFS		0x0080	/* rule has references */
578 #endif
579 
580 /* scrub flags */
581 #define	PFRULE_NODF		0x0100
582 #define PFRULE_RANDOMID		0x0800
583 #define PFRULE_REASSEMBLE_TCP	0x1000
584 #define PFRULE_SET_TOS		0x2000
585 
586 /* rule flags again */
587 #define PFRULE_IFBOUND		0x00010000	/* if-bound */
588 #define PFRULE_STATESLOPPY	0x00020000	/* sloppy state tracking */
589 
590 #define PFSTATE_HIWAT		100000	/* default state table size */
591 #define PFSTATE_ADAPT_START	60000	/* default adaptive timeout start */
592 #define PFSTATE_ADAPT_END	120000	/* default adaptive timeout end */
593 
594 
595 struct pf_threshold {
596 	u_int32_t	limit;
597 #define	PF_THRESHOLD_MULT	1000
598 #define PF_THRESHOLD_MAX	0xffffffff / PF_THRESHOLD_MULT
599 	u_int32_t	seconds;
600 	u_int32_t	count;
601 	u_int32_t	last;
602 };
603 
604 struct pf_src_node {
605 	LIST_ENTRY(pf_src_node) entry;
606 	struct pf_addr	 addr;
607 	struct pf_addr	 raddr;
608 	union pf_rule_ptr rule;
609 	struct pfi_kif	*kif;
610 	u_int64_t	 bytes[2];
611 	u_int64_t	 packets[2];
612 	u_int32_t	 states;
613 	u_int32_t	 conn;
614 	struct pf_threshold	conn_rate;
615 	u_int32_t	 creation;
616 	u_int32_t	 expire;
617 	sa_family_t	 af;
618 	u_int8_t	 ruletype;
619 };
620 
621 #define PFSNODE_HIWAT		10000	/* default source node table size */
622 
623 TAILQ_HEAD(pf_rulequeue, pf_rule);
624 
625 struct pf_anchor;
626 
627 struct pf_ruleset {
628 	struct {
629 		struct pf_rulequeue	 queues[2];
630 		struct {
631 			struct pf_rulequeue	*ptr;
632 			struct pf_rule		**ptr_array;
633 			u_int32_t		 rcount;
634 			u_int32_t		 ticket;
635 			int			 open;
636 		}			 active, inactive;
637 	}			 rules[PF_RULESET_MAX];
638 	struct pf_anchor	*anchor;
639 	u_int32_t		 tticket;
640 	int			 tables;
641 	int			 topen;
642 };
643 
644 RB_HEAD(pf_anchor_global, pf_anchor);
645 RB_HEAD(pf_anchor_node, pf_anchor);
646 struct pf_anchor {
647 	RB_ENTRY(pf_anchor)	 entry_global;
648 	RB_ENTRY(pf_anchor)	 entry_node;
649 	struct pf_anchor	*parent;
650 	struct pf_anchor_node	 children;
651 	char			 name[PF_ANCHOR_NAME_SIZE];
652 	char			 path[MAXPATHLEN];
653 	struct pf_ruleset	 ruleset;
654 	int			 refcnt;	/* anchor rules */
655 	int			 match;	/* XXX: used for pfctl black magic */
656 };
657 RB_PROTOTYPE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
658 RB_PROTOTYPE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
659 
660 int	 pf_get_ruleset_number(u_int8_t);
661 
662 #endif	/* _NET_PF_H_ */
663