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