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