xref: /netbsd/sys/dist/pf/net/pfvar.h (revision e90ddff9)
1 /*	$NetBSD: pfvar.h,v 1.23 2020/03/05 07:46:36 riastradh Exp $	*/
2 /*	$OpenBSD: pfvar.h,v 1.254 2007/07/13 09:17:48 markus Exp $ */
3 
4 /*
5  * Copyright (c) 2001 Daniel Hartmeier
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *    - Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *    - Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef _NET_PFVAR_H_
35 #define _NET_PFVAR_H_
36 
37 #ifdef _KERNEL_OPT
38 #include "opt_inet.h"
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/queue.h>
44 #include <sys/tree.h>
45 #include <sys/rwlock.h>
46 
47 #include <net/radix.h>
48 #ifndef __NetBSD__
49 #include <net/route.h>
50 #include <netinet/ip_ipsp.h>
51 #else
52 /* files above include netinet/in.h so include it too for compatibility */
53 #include <netinet/in.h>
54 #endif /* __NetBSD__ */
55 #include <netinet/tcp_fsm.h>
56 
57 #if defined(__NetBSD__) && defined(_KERNEL)
58 #include <net/if_compat.h>
59 #include <net/pf_mtag.h>
60 #endif /* __NetBSD__ && _KERNEL */
61 
62 struct ip;
63 struct ip6_hdr;
64 
65 #define	PF_TCPS_PROXY_SRC	((TCP_NSTATES)+0)
66 #define	PF_TCPS_PROXY_DST	((TCP_NSTATES)+1)
67 
68 #define	PF_MD5_DIGEST_LENGTH	16
69 #ifdef MD5_DIGEST_LENGTH
70 #if PF_MD5_DIGEST_LENGTH != MD5_DIGEST_LENGTH
71 #error
72 #endif
73 #endif
74 
75 enum	{ PF_INOUT, PF_IN, PF_OUT };
76 enum	{ PF_LAN_EXT, PF_EXT_GWY, PF_ID };
77 enum	{ PF_PASS, PF_DROP, PF_SCRUB, PF_NOSCRUB, PF_NAT, PF_NONAT,
78 	  PF_BINAT, PF_NOBINAT, PF_RDR, PF_NORDR, PF_SYNPROXY_DROP };
79 enum	{ PF_RULESET_SCRUB, PF_RULESET_FILTER, PF_RULESET_NAT,
80 	  PF_RULESET_BINAT, PF_RULESET_RDR, PF_RULESET_MAX };
81 enum	{ PF_OP_NONE, PF_OP_IRG, PF_OP_EQ, PF_OP_NE, PF_OP_LT,
82 	  PF_OP_LE, PF_OP_GT, PF_OP_GE, PF_OP_XRG, PF_OP_RRG };
83 enum	{ PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC, PF_DEBUG_NOISY };
84 enum	{ PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
85 	  PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
86 	  PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
87 enum	{ PF_GET_NONE, PF_GET_CLR_CNTR };
88 
89 /*
90  * Note about PFTM_*: real indices into pf_rule.timeout[] come before
91  * PFTM_MAX, special cases afterwards. See pf_state_expires().
92  */
93 enum	{ PFTM_TCP_FIRST_PACKET, PFTM_TCP_OPENING, PFTM_TCP_ESTABLISHED,
94 	  PFTM_TCP_CLOSING, PFTM_TCP_FIN_WAIT, PFTM_TCP_CLOSED,
95 	  PFTM_UDP_FIRST_PACKET, PFTM_UDP_SINGLE, PFTM_UDP_MULTIPLE,
96 	  PFTM_ICMP_FIRST_PACKET, PFTM_ICMP_ERROR_REPLY,
97 	  PFTM_OTHER_FIRST_PACKET, PFTM_OTHER_SINGLE,
98 	  PFTM_OTHER_MULTIPLE, PFTM_FRAG, PFTM_INTERVAL,
99 	  PFTM_ADAPTIVE_START, PFTM_ADAPTIVE_END, PFTM_SRC_NODE,
100 	  PFTM_TS_DIFF, PFTM_MAX, PFTM_PURGE, PFTM_UNLINKED,
101 	  PFTM_UNTIL_PACKET };
102 
103 /* PFTM default values */
104 #define PFTM_TCP_FIRST_PACKET_VAL	120	/* First TCP packet */
105 #define PFTM_TCP_OPENING_VAL		30	/* No response yet */
106 #define PFTM_TCP_ESTABLISHED_VAL	24*60*60/* Established */
107 #define PFTM_TCP_CLOSING_VAL		15 * 60	/* Half closed */
108 #define PFTM_TCP_FIN_WAIT_VAL		45	/* Got both FINs */
109 #define PFTM_TCP_CLOSED_VAL		90	/* Got a RST */
110 #define PFTM_UDP_FIRST_PACKET_VAL	60	/* First UDP packet */
111 #define PFTM_UDP_SINGLE_VAL		30	/* Unidirectional */
112 #define PFTM_UDP_MULTIPLE_VAL		60	/* Bidirectional */
113 #define PFTM_ICMP_FIRST_PACKET_VAL	20	/* First ICMP packet */
114 #define PFTM_ICMP_ERROR_REPLY_VAL	10	/* Got error response */
115 #define PFTM_OTHER_FIRST_PACKET_VAL	60	/* First packet */
116 #define PFTM_OTHER_SINGLE_VAL		30	/* Unidirectional */
117 #define PFTM_OTHER_MULTIPLE_VAL		60	/* Bidirectional */
118 #define PFTM_FRAG_VAL			30	/* Fragment expire */
119 #define PFTM_INTERVAL_VAL		10	/* Expire interval */
120 #define PFTM_SRC_NODE_VAL		0	/* Source tracking */
121 #define PFTM_TS_DIFF_VAL		30	/* Allowed TS diff */
122 
123 enum	{ PF_NOPFROUTE, PF_FASTROUTE, PF_ROUTETO, PF_DUPTO, PF_REPLYTO };
124 enum	{ PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
125 	  PF_LIMIT_TABLES, PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
126 #define PF_POOL_IDMASK		0x0f
127 enum	{ PF_POOL_NONE, PF_POOL_BITMASK, PF_POOL_RANDOM,
128 	  PF_POOL_SRCHASH, PF_POOL_ROUNDROBIN };
129 enum	{ PF_ADDR_ADDRMASK, PF_ADDR_NOROUTE, PF_ADDR_DYNIFTL,
130 	  PF_ADDR_TABLE, PF_ADDR_RTLABEL, PF_ADDR_URPFFAILED };
131 #define PF_POOL_TYPEMASK	0x0f
132 #define PF_POOL_STICKYADDR	0x20
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 
140 struct pf_addr {
141 	union {
142 		struct in_addr		v4;
143 		struct in6_addr		v6;
144 		u_int8_t		addr8[16];
145 		u_int16_t		addr16[8];
146 		u_int32_t		addr32[4];
147 	} pfa;		    /* 128-bit address */
148 #define v4	pfa.v4
149 #define v6	pfa.v6
150 #define addr8	pfa.addr8
151 #define addr16	pfa.addr16
152 #define addr32	pfa.addr32
153 };
154 
155 #define	PF_TABLE_NAME_SIZE	 32
156 
157 #define PFI_AFLAG_NETWORK	0x01
158 #define PFI_AFLAG_BROADCAST	0x02
159 #define PFI_AFLAG_PEER		0x04
160 #define PFI_AFLAG_MODEMASK	0x07
161 #define PFI_AFLAG_NOALIAS	0x08
162 
163 #ifndef RTLABEL_LEN
164 #define RTLABEL_LEN	32
165 #endif
166 
167 struct pf_addr_wrap {
168 	union {
169 		struct {
170 			struct pf_addr		 addr;
171 			struct pf_addr		 mask;
172 		}			 a;
173 		char			 ifname[IFNAMSIZ];
174 		char			 tblname[PF_TABLE_NAME_SIZE];
175 		char			 rtlabelname[RTLABEL_LEN];
176 		u_int32_t		 rtlabel;
177 	}			 v;
178 	union {
179 		struct pfi_dynaddr	*dyn;
180 		struct pfr_ktable	*tbl;
181 		int			 dyncnt;
182 		int			 tblcnt;
183 	}			 p;
184 	u_int8_t		 type;		/* PF_ADDR_* */
185 	u_int8_t		 iflags;	/* PFI_AFLAG_* */
186 };
187 
188 #ifdef _KERNEL
189 
190 struct pfi_dynaddr {
191 	TAILQ_ENTRY(pfi_dynaddr)	 entry;
192 	struct pf_addr			 pfid_addr4;
193 	struct pf_addr			 pfid_mask4;
194 	struct pf_addr			 pfid_addr6;
195 	struct pf_addr			 pfid_mask6;
196 	struct pfr_ktable		*pfid_kt;
197 	struct pfi_kif			*pfid_kif;
198 	void				*pfid_hook_cookie;
199 	int				 pfid_net;	/* mask or 128 */
200 	int				 pfid_acnt4;	/* address count IPv4 */
201 	int				 pfid_acnt6;	/* address count IPv6 */
202 	sa_family_t			 pfid_af;	/* rule af */
203 	u_int8_t			 pfid_iflags;	/* PFI_AFLAG_* */
204 };
205 
206 /*
207  * Address manipulation macros
208  */
209 
210 #ifdef INET
211 #ifndef INET6
212 #define PF_INET_ONLY
213 #endif /* ! INET6 */
214 #endif /* INET */
215 
216 #ifdef INET6
217 #ifndef INET
218 #define PF_INET6_ONLY
219 #endif /* ! INET */
220 #endif /* INET6 */
221 
222 #ifdef INET
223 #ifdef INET6
224 #define PF_INET_INET6
225 #endif /* INET6 */
226 #endif /* INET */
227 
228 #else
229 
230 #define PF_INET_INET6
231 
232 #endif /* _KERNEL */
233 
234 /* Both IPv4 and IPv6 */
235 #ifdef PF_INET_INET6
236 
237 #define PF_AEQ(a, b, c) \
238 	((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
239 	((a)->addr32[3] == (b)->addr32[3] && \
240 	(a)->addr32[2] == (b)->addr32[2] && \
241 	(a)->addr32[1] == (b)->addr32[1] && \
242 	(a)->addr32[0] == (b)->addr32[0])) \
243 
244 #define PF_ANEQ(a, b, c) \
245 	((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
246 	((a)->addr32[3] != (b)->addr32[3] || \
247 	(a)->addr32[2] != (b)->addr32[2] || \
248 	(a)->addr32[1] != (b)->addr32[1] || \
249 	(a)->addr32[0] != (b)->addr32[0])) \
250 
251 #define PF_AZERO(a, c) \
252 	((c == AF_INET && !(a)->addr32[0]) || \
253 	(!(a)->addr32[0] && !(a)->addr32[1] && \
254 	!(a)->addr32[2] && !(a)->addr32[3] )) \
255 
256 #define PF_MATCHA(n, a, m, b, f) \
257 	pf_match_addr(n, a, m, b, f)
258 
259 #define PF_ACPY(a, b, f) \
260 	pf_addrcpy(a, b, f)
261 
262 #define PF_AINC(a, f) \
263 	pf_addr_inc(a, f)
264 
265 #define PF_POOLMASK(a, b, c, d, f) \
266 	pf_poolmask(a, b, c, d, f)
267 
268 #else
269 
270 /* Just IPv6 */
271 
272 #ifdef PF_INET6_ONLY
273 
274 #define PF_AEQ(a, b, c) \
275 	((a)->addr32[3] == (b)->addr32[3] && \
276 	(a)->addr32[2] == (b)->addr32[2] && \
277 	(a)->addr32[1] == (b)->addr32[1] && \
278 	(a)->addr32[0] == (b)->addr32[0]) \
279 
280 #define PF_ANEQ(a, b, c) \
281 	((a)->addr32[3] != (b)->addr32[3] || \
282 	(a)->addr32[2] != (b)->addr32[2] || \
283 	(a)->addr32[1] != (b)->addr32[1] || \
284 	(a)->addr32[0] != (b)->addr32[0]) \
285 
286 #define PF_AZERO(a, c) \
287 	(!(a)->addr32[0] && \
288 	!(a)->addr32[1] && \
289 	!(a)->addr32[2] && \
290 	!(a)->addr32[3] ) \
291 
292 #define PF_MATCHA(n, a, m, b, f) \
293 	pf_match_addr(n, a, m, b, f)
294 
295 #define PF_ACPY(a, b, f) \
296 	pf_addrcpy(a, b, f)
297 
298 #define PF_AINC(a, f) \
299 	pf_addr_inc(a, f)
300 
301 #define PF_POOLMASK(a, b, c, d, f) \
302 	pf_poolmask(a, b, c, d, f)
303 
304 #else
305 
306 /* Just IPv4 */
307 #ifdef PF_INET_ONLY
308 
309 #define PF_AEQ(a, b, c) \
310 	((a)->addr32[0] == (b)->addr32[0])
311 
312 #define PF_ANEQ(a, b, c) \
313 	((a)->addr32[0] != (b)->addr32[0])
314 
315 #define PF_AZERO(a, c) \
316 	(!(a)->addr32[0])
317 
318 #define PF_MATCHA(n, a, m, b, f) \
319 	pf_match_addr(n, a, m, b, f)
320 
321 #define PF_ACPY(a, b, f) \
322 	(a)->v4.s_addr = (b)->v4.s_addr
323 
324 #define PF_AINC(a, f) \
325 	do { \
326 		(a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
327 	} while (0)
328 
329 #define PF_POOLMASK(a, b, c, d, f) \
330 	do { \
331 		(a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
332 		(((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
333 	} while (0)
334 
335 #endif /* PF_INET_ONLY */
336 #endif /* PF_INET6_ONLY */
337 #endif /* PF_INET_INET6 */
338 
339 #define	PF_MISMATCHAW(aw, x, af, neg, ifp)				\
340 	(								\
341 		(((aw)->type == PF_ADDR_NOROUTE &&			\
342 		    pf_routable((x), (af), NULL)) ||			\
343 		(((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&	\
344 		    pf_routable((x), (af), (ifp))) ||			\
345 		((aw)->type == PF_ADDR_RTLABEL &&			\
346 		    !pf_rtlabel_match((x), (af), (aw))) ||		\
347 		((aw)->type == PF_ADDR_TABLE &&				\
348 		    !pfr_match_addr((aw)->p.tbl, (x), (af))) ||		\
349 		((aw)->type == PF_ADDR_DYNIFTL &&			\
350 		    !pfi_match_addr((aw)->p.dyn, (x), (af))) || 	\
351 		((aw)->type == PF_ADDR_ADDRMASK &&			\
352 		    !PF_AZERO(&(aw)->v.a.mask, (af)) &&			\
353 		    !PF_MATCHA(0, &(aw)->v.a.addr,			\
354 		    &(aw)->v.a.mask, (x), (af))))) !=			\
355 		(neg)							\
356 	)
357 
358 
359 struct pf_rule_uid {
360 	uid_t		 uid[2];
361 	u_int8_t	 op;
362 };
363 
364 struct pf_rule_gid {
365 	uid_t		 gid[2];
366 	u_int8_t	 op;
367 };
368 
369 struct pf_rule_addr {
370 	struct pf_addr_wrap	 addr;
371 	u_int16_t		 port[2];
372 	u_int8_t		 neg;
373 	u_int8_t		 port_op;
374 };
375 
376 struct pf_pooladdr {
377 	struct pf_addr_wrap		 addr;
378 	TAILQ_ENTRY(pf_pooladdr)	 entries;
379 	char				 ifname[IFNAMSIZ];
380 	struct pfi_kif			*kif;
381 };
382 
383 TAILQ_HEAD(pf_palist, pf_pooladdr);
384 
385 struct pf_poolhashkey {
386 	union {
387 		u_int8_t		key8[16];
388 		u_int16_t		key16[8];
389 		u_int32_t		key32[4];
390 	} pfk;		    /* 128-bit hash key */
391 #define key8	pfk.key8
392 #define key16	pfk.key16
393 #define key32	pfk.key32
394 };
395 
396 struct pf_pool {
397 	struct pf_palist	 list;
398 	struct pf_pooladdr	*cur;
399 	struct pf_poolhashkey	 key;
400 	struct pf_addr		 counter;
401 	int			 tblidx;
402 	u_int16_t		 proxy_port[2];
403 	u_int8_t		 port_op;
404 	u_int8_t		 opts;
405 };
406 
407 
408 /* A packed Operating System description for fingerprinting */
409 typedef u_int32_t pf_osfp_t;
410 #define PF_OSFP_ANY	((pf_osfp_t)0)
411 #define PF_OSFP_UNKNOWN	((pf_osfp_t)-1)
412 #define PF_OSFP_NOMATCH	((pf_osfp_t)-2)
413 
414 struct pf_osfp_entry {
415 	SLIST_ENTRY(pf_osfp_entry) fp_entry;
416 	pf_osfp_t		fp_os;
417 	int			fp_enflags;
418 #define PF_OSFP_EXPANDED	0x001		/* expanded entry */
419 #define PF_OSFP_GENERIC		0x002		/* generic signature */
420 #define PF_OSFP_NODETAIL	0x004		/* no p0f details */
421 #define PF_OSFP_LEN	32
422 	char			fp_class_nm[PF_OSFP_LEN];
423 	char			fp_version_nm[PF_OSFP_LEN];
424 	char			fp_subtype_nm[PF_OSFP_LEN];
425 };
426 #define PF_OSFP_ENTRY_EQ(a, b) \
427     ((a)->fp_os == (b)->fp_os && \
428     memcmp((a)->fp_class_nm, (b)->fp_class_nm, PF_OSFP_LEN) == 0 && \
429     memcmp((a)->fp_version_nm, (b)->fp_version_nm, PF_OSFP_LEN) == 0 && \
430     memcmp((a)->fp_subtype_nm, (b)->fp_subtype_nm, PF_OSFP_LEN) == 0)
431 
432 /* handle pf_osfp_t packing */
433 #define _FP_RESERVED_BIT	1  /* For the special negative #defines */
434 #define _FP_UNUSED_BITS		1
435 #define _FP_CLASS_BITS		10 /* OS Class (Windows, Linux) */
436 #define _FP_VERSION_BITS	10 /* OS version (95, 98, NT, 2.4.54, 3.2) */
437 #define _FP_SUBTYPE_BITS	10 /* patch level (NT SP4, SP3, ECN patch) */
438 #define PF_OSFP_UNPACK(osfp, class, version, subtype) do { \
439 	(class) = ((osfp) >> (_FP_VERSION_BITS+_FP_SUBTYPE_BITS)) & \
440 	    ((1 << _FP_CLASS_BITS) - 1); \
441 	(version) = ((osfp) >> _FP_SUBTYPE_BITS) & \
442 	    ((1 << _FP_VERSION_BITS) - 1);\
443 	(subtype) = (osfp) & ((1 << _FP_SUBTYPE_BITS) - 1); \
444 } while(0)
445 #define PF_OSFP_PACK(osfp, class, version, subtype) do { \
446 	(osfp) = ((class) & ((1 << _FP_CLASS_BITS) - 1)) << (_FP_VERSION_BITS \
447 	    + _FP_SUBTYPE_BITS); \
448 	(osfp) |= ((version) & ((1 << _FP_VERSION_BITS) - 1)) << \
449 	    _FP_SUBTYPE_BITS; \
450 	(osfp) |= (subtype) & ((1 << _FP_SUBTYPE_BITS) - 1); \
451 } while(0)
452 
453 /* the fingerprint of an OSes TCP SYN packet */
454 typedef u_int64_t	pf_tcpopts_t;
455 struct pf_os_fingerprint {
456 	SLIST_HEAD(pf_osfp_enlist, pf_osfp_entry) fp_oses; /* list of matches */
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 #define PF_OSFP_WSIZE_MOD	0x0001		/* Window modulus */
463 #define PF_OSFP_WSIZE_DC	0x0002		/* Window don't care */
464 #define PF_OSFP_WSIZE_MSS	0x0004		/* Window multiple of MSS */
465 #define PF_OSFP_WSIZE_MTU	0x0008		/* Window multiple of MTU */
466 #define PF_OSFP_PSIZE_MOD	0x0010		/* packet size modulus */
467 #define PF_OSFP_PSIZE_DC	0x0020		/* packet size don't care */
468 #define PF_OSFP_WSCALE		0x0040		/* TCP window scaling */
469 #define PF_OSFP_WSCALE_MOD	0x0080		/* TCP window scale modulus */
470 #define PF_OSFP_WSCALE_DC	0x0100		/* TCP window scale dont-care */
471 #define PF_OSFP_MSS		0x0200		/* TCP MSS */
472 #define PF_OSFP_MSS_MOD		0x0400		/* TCP MSS modulus */
473 #define PF_OSFP_MSS_DC		0x0800		/* TCP MSS dont-care */
474 #define PF_OSFP_DF		0x1000		/* IPv4 don't fragment bit */
475 #define PF_OSFP_TS0		0x2000		/* Zero timestamp */
476 #define PF_OSFP_INET6		0x4000		/* IPv6 */
477 	u_int8_t		fp_optcnt;	/* TCP option count */
478 	u_int8_t		fp_wscale;	/* TCP window scaling */
479 	u_int8_t		fp_ttl;		/* IPv4 TTL */
480 #define PF_OSFP_MAXTTL_OFFSET	40
481 /* TCP options packing */
482 #define PF_OSFP_TCPOPT_NOP	0x0		/* TCP NOP option */
483 #define PF_OSFP_TCPOPT_WSCALE	0x1		/* TCP window scaling option */
484 #define PF_OSFP_TCPOPT_MSS	0x2		/* TCP max segment size opt */
485 #define PF_OSFP_TCPOPT_SACK	0x3		/* TCP SACK OK option */
486 #define PF_OSFP_TCPOPT_TS	0x4		/* TCP timestamp option */
487 #define PF_OSFP_TCPOPT_BITS	3		/* bits used by each option */
488 #define PF_OSFP_MAX_OPTS \
489     (sizeof(((struct pf_os_fingerprint *)0)->fp_tcpopts) * 8) \
490     / PF_OSFP_TCPOPT_BITS
491 
492 	SLIST_ENTRY(pf_os_fingerprint)	fp_next;
493 };
494 
495 struct pf_osfp_ioctl {
496 	struct pf_osfp_entry	fp_os;
497 	pf_tcpopts_t		fp_tcpopts;	/* packed TCP options */
498 	u_int16_t		fp_wsize;	/* TCP window size */
499 	u_int16_t		fp_psize;	/* ip->ip_len */
500 	u_int16_t		fp_mss;		/* TCP MSS */
501 	u_int16_t		fp_flags;
502 	u_int8_t		fp_optcnt;	/* TCP option count */
503 	u_int8_t		fp_wscale;	/* TCP window scaling */
504 	u_int8_t		fp_ttl;		/* IPv4 TTL */
505 
506 	int			fp_getnum;	/* DIOCOSFPGET number */
507 };
508 
509 
510 union pf_rule_ptr {
511 	struct pf_rule		*ptr;
512 	u_int32_t		 nr;
513 };
514 
515 #define	PF_ANCHOR_NAME_SIZE	 64
516 
517 struct pf_rule {
518 	struct pf_rule_addr	 src;
519 	struct pf_rule_addr	 dst;
520 #define PF_SKIP_IFP		0
521 #define PF_SKIP_DIR		1
522 #define PF_SKIP_AF		2
523 #define PF_SKIP_PROTO		3
524 #define PF_SKIP_SRC_ADDR	4
525 #define PF_SKIP_SRC_PORT	5
526 #define PF_SKIP_DST_ADDR	6
527 #define PF_SKIP_DST_PORT	7
528 #define PF_SKIP_COUNT		8
529 	union pf_rule_ptr	 skip[PF_SKIP_COUNT];
530 #define PF_RULE_LABEL_SIZE	 64
531 	char			 label[PF_RULE_LABEL_SIZE];
532 #define PF_QNAME_SIZE		 64
533 	char			 ifname[IFNAMSIZ];
534 	char			 qname[PF_QNAME_SIZE];
535 	char			 pqname[PF_QNAME_SIZE];
536 #define	PF_TAG_NAME_SIZE	 64
537 	char			 tagname[PF_TAG_NAME_SIZE];
538 	char			 match_tagname[PF_TAG_NAME_SIZE];
539 
540 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
541 
542 	TAILQ_ENTRY(pf_rule)	 entries;
543 	struct pf_pool		 rpool;
544 
545 	u_int64_t		 evaluations;
546 	u_int64_t		 packets[2];
547 	u_int64_t		 bytes[2];
548 
549 	struct pfi_kif		*kif;
550 	struct pf_anchor	*anchor;
551 	struct pfr_ktable	*overload_tbl;
552 
553 	pf_osfp_t		 os_fingerprint;
554 
555 	int			 rtableid;
556 	u_int32_t		 timeout[PFTM_MAX];
557 	u_int32_t		 states;
558 	u_int32_t		 max_states;
559 	u_int32_t		 src_nodes;
560 	u_int32_t		 max_src_nodes;
561 	u_int32_t		 max_src_states;
562 	u_int32_t		 max_src_conn;
563 	struct {
564 		u_int32_t		limit;
565 		u_int32_t		seconds;
566 	}			 max_src_conn_rate;
567 	u_int32_t		 qid;
568 	u_int32_t		 pqid;
569 	u_int32_t		 rt_listid;
570 	u_int32_t		 nr;
571 	u_int32_t		 prob;
572 	uid_t			 cuid;
573 	pid_t			 cpid;
574 
575 	u_int16_t		 return_icmp;
576 	u_int16_t		 return_icmp6;
577 	u_int16_t		 max_mss;
578 	u_int16_t		 tag;
579 	u_int16_t		 match_tag;
580 
581 	struct pf_rule_uid	 uid;
582 	struct pf_rule_gid	 gid;
583 
584 	u_int32_t		 rule_flag;
585 	u_int8_t		 action;
586 	u_int8_t		 direction;
587 	u_int8_t		 log;
588 	u_int8_t		 logif;
589 	u_int8_t		 quick;
590 	u_int8_t		 ifnot;
591 	u_int8_t		 match_tag_not;
592 	u_int8_t		 natpass;
593 
594 #define PF_STATE_NORMAL		0x1
595 #define PF_STATE_MODULATE	0x2
596 #define PF_STATE_SYNPROXY	0x3
597 	u_int8_t		 keep_state;
598 	sa_family_t		 af;
599 	u_int8_t		 proto;
600 	u_int8_t		 type;
601 	u_int8_t		 code;
602 	u_int8_t		 flags;
603 	u_int8_t		 flagset;
604 	u_int8_t		 min_ttl;
605 	u_int8_t		 allow_opts;
606 	u_int8_t		 rt;
607 	u_int8_t		 return_ttl;
608 	u_int8_t		 tos;
609 	u_int8_t		 anchor_relative;
610 	u_int8_t		 anchor_wildcard;
611 
612 #define PF_FLUSH		0x01
613 #define PF_FLUSH_GLOBAL		0x02
614 	u_int8_t		 flush;
615 };
616 
617 /* rule flags */
618 #define	PFRULE_DROP		0x0000
619 #define	PFRULE_RETURNRST	0x0001
620 #define	PFRULE_FRAGMENT		0x0002
621 #define	PFRULE_RETURNICMP	0x0004
622 #define	PFRULE_RETURN		0x0008
623 #define	PFRULE_NOSYNC		0x0010
624 #define PFRULE_SRCTRACK		0x0020  /* track source states */
625 #define PFRULE_RULESRCTRACK	0x0040  /* per rule */
626 
627 /* scrub flags */
628 #define	PFRULE_NODF		0x0100
629 #define	PFRULE_FRAGCROP		0x0200	/* non-buffering frag cache */
630 #define	PFRULE_FRAGDROP		0x0400	/* drop funny fragments */
631 #define PFRULE_RANDOMID		0x0800
632 #define PFRULE_REASSEMBLE_TCP	0x1000
633 
634 /* rule flags again */
635 #define PFRULE_IFBOUND		0x00010000	/* if-bound */
636 
637 #define PFSTATE_HIWAT		10000	/* default state table size */
638 #define PFSTATE_ADAPT_START	6000	/* default adaptive timeout start */
639 #define PFSTATE_ADAPT_END	12000	/* default adaptive timeout end */
640 
641 
642 struct pf_threshold {
643 	u_int32_t	limit;
644 #define	PF_THRESHOLD_MULT	1000
645 #define PF_THRESHOLD_MAX	0xffffffff / PF_THRESHOLD_MULT
646 	u_int32_t	seconds;
647 	u_int32_t	count;
648 	u_int32_t	last;
649 };
650 
651 struct pf_src_node {
652 	RB_ENTRY(pf_src_node) entry;
653 	struct pf_addr	 addr;
654 	struct pf_addr	 raddr;
655 	union pf_rule_ptr rule;
656 	struct pfi_kif	*kif;
657 	u_int64_t	 bytes[2];
658 	u_int64_t	 packets[2];
659 	u_int32_t	 states;
660 	u_int32_t	 conn;
661 	struct pf_threshold	conn_rate;
662 	u_int32_t	 creation;
663 	u_int32_t	 expire;
664 	sa_family_t	 af;
665 	u_int8_t	 ruletype;
666 };
667 
668 #define PFSNODE_HIWAT		10000	/* default source node table size */
669 
670 struct pf_state_scrub {
671 	struct timeval	pfss_last;	/* time received last packet	*/
672 	u_int32_t	pfss_tsecr;	/* last echoed timestamp	*/
673 	u_int32_t	pfss_tsval;	/* largest timestamp		*/
674 	u_int32_t	pfss_tsval0;	/* original timestamp		*/
675 	u_int16_t	pfss_flags;
676 #define PFSS_TIMESTAMP	0x0001		/* modulate timestamp		*/
677 #define PFSS_PAWS	0x0010		/* stricter PAWS checks		*/
678 #define PFSS_PAWS_IDLED	0x0020		/* was idle too long.  no PAWS	*/
679 #define PFSS_DATA_TS	0x0040		/* timestamp on data packets	*/
680 #define PFSS_DATA_NOTS	0x0080		/* no timestamp on data packets	*/
681 	u_int8_t	pfss_ttl;	/* stashed TTL			*/
682 	u_int8_t	pad;
683 	u_int32_t	pfss_ts_mod;	/* timestamp modulation		*/
684 };
685 
686 struct pf_state_host {
687 	struct pf_addr	addr;
688 	u_int16_t	port;
689 	u_int16_t	pad;
690 };
691 
692 struct pf_state_peer {
693 	u_int32_t	seqlo;		/* Max sequence number sent	*/
694 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
695 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
696 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
697 	u_int8_t	state;		/* active state level		*/
698 	u_int8_t	wscale;		/* window scaling factor	*/
699 	u_int16_t	mss;		/* Maximum segment size option	*/
700 	u_int8_t	tcp_est;	/* Did we reach TCPS_ESTABLISHED */
701 	struct pf_state_scrub	*scrub;	/* state is scrubbed		*/
702 	u_int8_t	pad[3];
703 };
704 
705 TAILQ_HEAD(pf_state_queue, pf_state);
706 
707 /* keep synced with struct pf_state_key, used in RB_FIND */
708 struct pf_state_key_cmp {
709 	struct pf_state_host lan;
710 	struct pf_state_host gwy;
711 	struct pf_state_host ext;
712 	sa_family_t	 af;
713 	u_int8_t	 proto;
714 	u_int8_t	 direction;
715 	u_int8_t	 pad;
716 };
717 
718 TAILQ_HEAD(pf_statelist, pf_state);
719 
720 struct pf_state_key {
721 	struct pf_state_host lan;
722 	struct pf_state_host gwy;
723 	struct pf_state_host ext;
724 	sa_family_t	 af;
725 	u_int8_t	 proto;
726 	u_int8_t	 direction;
727 	u_int8_t	 pad;
728 
729 	RB_ENTRY(pf_state_key)	 entry_lan_ext;
730 	RB_ENTRY(pf_state_key)	 entry_ext_gwy;
731 	struct pf_statelist	 states;
732 	u_short		 refcnt;	/* same size as if_index */
733 };
734 
735 
736 /* keep synced with struct pf_state, used in RB_FIND */
737 struct pf_state_cmp {
738 	u_int64_t	 id;
739 	u_int32_t	 creatorid;
740 	u_int32_t	 pad;
741 };
742 
743 struct pf_state {
744 	u_int64_t		 id;
745 	u_int32_t		 creatorid;
746 	u_int32_t		 pad;
747 
748 	TAILQ_ENTRY(pf_state)	 entry_list;
749 	TAILQ_ENTRY(pf_state)	 next;
750 	RB_ENTRY(pf_state)	 entry_id;
751 	struct pf_state_peer	 src;
752 	struct pf_state_peer	 dst;
753 	union pf_rule_ptr	 rule;
754 	union pf_rule_ptr	 anchor;
755 	union pf_rule_ptr	 nat_rule;
756 	struct pf_addr		 rt_addr;
757 	struct pf_state_key	*state_key;
758 	struct pfi_kif		*kif;
759 	struct pfi_kif		*rt_kif;
760 	struct pf_src_node	*src_node;
761 	struct pf_src_node	*nat_src_node;
762 	u_int64_t		 packets[2];
763 	u_int64_t		 bytes[2];
764 	u_int32_t		 creation;
765 	u_int32_t		 expire;
766 	u_int32_t		 pfsync_time;
767 	u_int16_t		 tag;
768 	u_int8_t		 log;
769 	u_int8_t		 allow_opts;
770 	u_int8_t		 timeout;
771 	u_int8_t		 sync_flags;
772 #define	PFSTATE_NOSYNC	 0x01
773 #define	PFSTATE_FROMSYNC 0x02
774 #define	PFSTATE_STALE	 0x04
775 };
776 
777 /*
778  * Unified state structures for pulling states out of the kernel
779  * used by pfsync(4) and the pf(4) ioctl.
780  */
781 struct pfsync_state_scrub {
782 	u_int16_t	pfss_flags;
783 	u_int8_t	pfss_ttl;	/* stashed TTL		*/
784 #define PFSYNC_SCRUB_FLAG_VALID 	0x01
785 	u_int8_t	scrub_flag;
786 	u_int32_t	pfss_ts_mod;	/* timestamp modulation	*/
787 } __packed;
788 
789 struct pfsync_state_host {
790 	struct pf_addr	addr;
791 	u_int16_t	port;
792 	u_int16_t	pad[3];
793 } __packed;
794 
795 struct pfsync_state_peer {
796 	struct pfsync_state_scrub scrub;	/* state is scrubbed	*/
797 	u_int32_t	seqlo;		/* Max sequence number sent	*/
798 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
799 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
800 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
801 	u_int16_t	mss;		/* Maximum segment size option	*/
802 	u_int8_t	state;		/* active state level		*/
803 	u_int8_t	wscale;		/* window scaling factor	*/
804 	u_int8_t	pad[6];
805 } __packed;
806 
807 struct pfsync_state {
808 	u_int32_t	 id[2];
809 	char		 ifname[IFNAMSIZ];
810 	struct pfsync_state_host lan;
811 	struct pfsync_state_host gwy;
812 	struct pfsync_state_host ext;
813 	struct pfsync_state_peer src;
814 	struct pfsync_state_peer dst;
815 	struct pf_addr	 rt_addr;
816 	u_int32_t	 rule;
817 	u_int32_t	 anchor;
818 	u_int32_t	 nat_rule;
819 	u_int32_t	 creation;
820 	u_int32_t	 expire;
821 	u_int32_t	 packets[2][2];
822 	u_int32_t	 bytes[2][2];
823 	u_int32_t	 creatorid;
824 	sa_family_t	 af;
825 	u_int8_t	 proto;
826 	u_int8_t	 direction;
827 	u_int8_t	 log;
828 	u_int8_t	 allow_opts;
829 	u_int8_t	 timeout;
830 	u_int8_t	 sync_flags;
831 	u_int8_t	 updates;
832 } __packed;
833 
834 #define PFSYNC_FLAG_COMPRESS 	0x01
835 #define PFSYNC_FLAG_STALE	0x02
836 #define PFSYNC_FLAG_SRCNODE	0x04
837 #define PFSYNC_FLAG_NATSRCNODE	0x08
838 
839 /* for copies to/from userland via pf_ioctl() */
840 #define pf_state_peer_to_pfsync(s,d) do {	\
841 	(d)->seqlo = (s)->seqlo;		\
842 	(d)->seqhi = (s)->seqhi;		\
843 	(d)->seqdiff = (s)->seqdiff;		\
844 	(d)->max_win = (s)->max_win;		\
845 	(d)->mss = (s)->mss;			\
846 	(d)->state = (s)->state;		\
847 	(d)->wscale = (s)->wscale;		\
848 	if ((s)->scrub) {						\
849 		(d)->scrub.pfss_flags = 				\
850 		    (s)->scrub->pfss_flags & PFSS_TIMESTAMP;		\
851 		(d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;		\
852 		(d)->scrub.pfss_ts_mod = (s)->scrub->pfss_ts_mod;	\
853 		(d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;	\
854 	}								\
855 } while (0)
856 
857 #define pf_state_peer_from_pfsync(s,d) do {	\
858 	(d)->seqlo = (s)->seqlo;		\
859 	(d)->seqhi = (s)->seqhi;		\
860 	(d)->seqdiff = (s)->seqdiff;		\
861 	(d)->max_win = (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 = (s)->scrub.pfss_ts_mod;	\
871 	}								\
872 } while (0)
873 
874 #define pf_state_counter_to_pfsync(s,d) do {			\
875 	d[0] = (s>>32)&0xffffffff;				\
876 	d[1] = 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 
883 
884 TAILQ_HEAD(pf_rulequeue, pf_rule);
885 
886 struct pf_anchor;
887 
888 struct pf_ruleset {
889 	struct {
890 		struct pf_rulequeue	 queues[2];
891 		struct {
892 			struct pf_rulequeue	*ptr;
893 			struct pf_rule		**ptr_array;
894 			u_int32_t		 rcount;
895 			u_int32_t		 ticket;
896 			int			 open;
897 		}			 active, inactive;
898 	}			 rules[PF_RULESET_MAX];
899 	struct pf_anchor	*anchor;
900 	u_int32_t		 tticket;
901 	int			 tables;
902 	int			 topen;
903 };
904 
905 RB_HEAD(pf_anchor_global, pf_anchor);
906 RB_HEAD(pf_anchor_node, pf_anchor);
907 struct pf_anchor {
908 	RB_ENTRY(pf_anchor)	 entry_global;
909 	RB_ENTRY(pf_anchor)	 entry_node;
910 	struct pf_anchor	*parent;
911 	struct pf_anchor_node	 children;
912 	char			 name[PF_ANCHOR_NAME_SIZE];
913 	char			 path[MAXPATHLEN];
914 	struct pf_ruleset	 ruleset;
915 	int			 refcnt;	/* anchor rules */
916 	int			 match;
917 };
918 RB_PROTOTYPE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
919 RB_PROTOTYPE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
920 
921 #define PF_RESERVED_ANCHOR	"_pf"
922 
923 #define PFR_TFLAG_PERSIST	0x00000001
924 #define PFR_TFLAG_CONST		0x00000002
925 #define PFR_TFLAG_ACTIVE	0x00000004
926 #define PFR_TFLAG_INACTIVE	0x00000008
927 #define PFR_TFLAG_REFERENCED	0x00000010
928 #define PFR_TFLAG_REFDANCHOR	0x00000020
929 #define PFR_TFLAG_USRMASK	0x00000003
930 #define PFR_TFLAG_SETMASK	0x0000003C
931 #define PFR_TFLAG_ALLMASK	0x0000003F
932 
933 struct pfr_table {
934 	char			 pfrt_anchor[MAXPATHLEN];
935 	char			 pfrt_name[PF_TABLE_NAME_SIZE];
936 	u_int32_t		 pfrt_flags;
937 	u_int8_t		 pfrt_fback;
938 };
939 
940 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
941 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
942 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_MAX };
943 
944 struct pfr_addr {
945 	union {
946 		struct in_addr	 _pfra_ip4addr;
947 		struct in6_addr	 _pfra_ip6addr;
948 	}		 pfra_u;
949 	u_int8_t	 pfra_af;
950 	u_int8_t	 pfra_net;
951 	u_int8_t	 pfra_not;
952 	u_int8_t	 pfra_fback;
953 };
954 #define	pfra_ip4addr	pfra_u._pfra_ip4addr
955 #define	pfra_ip6addr	pfra_u._pfra_ip6addr
956 
957 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
958 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
959 #define PFR_OP_XPASS	PFR_OP_ADDR_MAX
960 
961 struct pfr_astats {
962 	struct pfr_addr	 pfras_a;
963 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
964 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
965 	long		 pfras_tzero;
966 };
967 
968 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
969 
970 struct pfr_tstats {
971 	struct pfr_table pfrts_t;
972 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
973 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
974 	u_int64_t	 pfrts_match;
975 	u_int64_t	 pfrts_nomatch;
976 	long		 pfrts_tzero;
977 	int		 pfrts_cnt;
978 	int		 pfrts_refcnt[PFR_REFCNT_MAX];
979 };
980 #define	pfrts_name	pfrts_t.pfrt_name
981 #define pfrts_flags	pfrts_t.pfrt_flags
982 
983 #ifndef _SOCKADDR_UNION_DEFINED
984 #define _SOCKADDR_UNION_DEFINED
985 union sockaddr_union {
986 	struct sockaddr		sa;
987 	struct sockaddr_in	sin;
988 	struct sockaddr_in6	sin6;
989 };
990 #endif /* _SOCKADDR_UNION_DEFINED */
991 
992 SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
993 struct pfr_kentry {
994 	struct radix_node	 pfrke_node[2];
995 	union sockaddr_union	 pfrke_sa;
996 	u_int64_t		 pfrke_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
997 	u_int64_t		 pfrke_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
998 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq;
999 	long			 pfrke_tzero;
1000 	u_int8_t		 pfrke_af;
1001 	u_int8_t		 pfrke_net;
1002 	u_int8_t		 pfrke_not;
1003 	u_int8_t		 pfrke_mark;
1004 	u_int8_t		 pfrke_intrpool;
1005 };
1006 
1007 SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
1008 RB_HEAD(pfr_ktablehead, pfr_ktable);
1009 struct pfr_ktable {
1010 	struct pfr_tstats	 pfrkt_ts;
1011 	RB_ENTRY(pfr_ktable)	 pfrkt_tree;
1012 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq;
1013 	struct radix_node_head	*pfrkt_ip4;
1014 	struct radix_node_head	*pfrkt_ip6;
1015 	struct pfr_ktable	*pfrkt_shadow;
1016 	struct pfr_ktable	*pfrkt_root;
1017 	struct pf_ruleset	*pfrkt_rs;
1018 	long			 pfrkt_larg;
1019 	int			 pfrkt_nflags;
1020 };
1021 #define pfrkt_t		pfrkt_ts.pfrts_t
1022 #define pfrkt_name	pfrkt_t.pfrt_name
1023 #define pfrkt_anchor	pfrkt_t.pfrt_anchor
1024 #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset
1025 #define pfrkt_flags	pfrkt_t.pfrt_flags
1026 #define pfrkt_cnt	pfrkt_ts.pfrts_cnt
1027 #define pfrkt_refcnt	pfrkt_ts.pfrts_refcnt
1028 #define pfrkt_packets	pfrkt_ts.pfrts_packets
1029 #define pfrkt_bytes	pfrkt_ts.pfrts_bytes
1030 #define pfrkt_match	pfrkt_ts.pfrts_match
1031 #define pfrkt_nomatch	pfrkt_ts.pfrts_nomatch
1032 #define pfrkt_tzero	pfrkt_ts.pfrts_tzero
1033 
1034 RB_HEAD(pf_state_tree_lan_ext, pf_state_key);
1035 RB_PROTOTYPE(pf_state_tree_lan_ext, pf_state_key,
1036     entry_lan_ext, pf_state_compare_lan_ext);
1037 
1038 RB_HEAD(pf_state_tree_ext_gwy, pf_state_key);
1039 RB_PROTOTYPE(pf_state_tree_ext_gwy, pf_state_key,
1040     entry_ext_gwy, pf_state_compare_ext_gwy);
1041 
1042 RB_HEAD(pfi_ifhead, pfi_kif);
1043 
1044 /* state tables */
1045 extern struct pf_state_tree_lan_ext	 pf_statetbl_lan_ext;
1046 extern struct pf_state_tree_ext_gwy	 pf_statetbl_ext_gwy;
1047 
1048 /* keep synced with pfi_kif, used in RB_FIND */
1049 struct pfi_kif_cmp {
1050 	char				 pfik_name[IFNAMSIZ];
1051 };
1052 
1053 struct pfi_kif {
1054 	char				 pfik_name[IFNAMSIZ];
1055 	RB_ENTRY(pfi_kif)		 pfik_tree;
1056 	u_int64_t			 pfik_packets[2][2][2];
1057 	u_int64_t			 pfik_bytes[2][2][2];
1058 	u_int32_t			 pfik_tzero;
1059 	int				 pfik_flags;
1060 	void				*pfik_ah_cookie;
1061 	struct ifnet			*pfik_ifp;
1062 	struct ifg_group		*pfik_group;
1063 	int				 pfik_states;
1064 	int				 pfik_rules;
1065 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1066 };
1067 
1068 enum pfi_kif_refs {
1069 	PFI_KIF_REF_NONE,
1070 	PFI_KIF_REF_STATE,
1071 	PFI_KIF_REF_RULE
1072 };
1073 
1074 #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */
1075 
1076 struct pf_pdesc {
1077 	struct {
1078 		int	 done;
1079 		uid_t	 uid;
1080 		gid_t	 gid;
1081 		pid_t	 pid;
1082 	}		 lookup;
1083 	u_int64_t	 tot_len;	/* Make Mickey money */
1084 	union {
1085 		struct tcphdr		*tcp;
1086 		struct udphdr		*udp;
1087 		struct icmp		*icmp;
1088 #ifdef INET6
1089 		struct icmp6_hdr	*icmp6;
1090 #endif /* INET6 */
1091 		void			*any;
1092 	} hdr;
1093 	struct pf_addr	 baddr;		/* address before translation */
1094 	struct pf_addr	 naddr;		/* address after translation */
1095 	struct pf_rule	*nat_rule;	/* nat/rdr rule applied to packet */
1096 	struct pf_addr	*src;
1097 	struct pf_addr	*dst;
1098 	struct ether_header
1099 			*eh;
1100 	u_int16_t	*ip_sum;
1101 	u_int32_t	 p_len;		/* total length of payload */
1102 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
1103 					 * state code. Easier than tags */
1104 #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
1105 #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
1106 	sa_family_t	 af;
1107 	u_int8_t	 proto;
1108 	u_int8_t	 tos;
1109 };
1110 
1111 /* flags for RDR options */
1112 #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
1113 #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
1114 
1115 /* Reasons code for passing/dropping a packet */
1116 #define PFRES_MATCH	0		/* Explicit match of a rule */
1117 #define PFRES_BADOFF	1		/* Bad offset for pull_hdr */
1118 #define PFRES_FRAG	2		/* Dropping following fragment */
1119 #define PFRES_SHORT	3		/* Dropping short packet */
1120 #define PFRES_NORM	4		/* Dropping by normalizer */
1121 #define PFRES_MEMORY	5		/* Dropped due to lacking mem */
1122 #define PFRES_TS	6		/* Bad TCP Timestamp (RFC1323) */
1123 #define PFRES_CONGEST	7		/* Congestion (of IP queue) */
1124 #define PFRES_IPOPTIONS 8		/* IP option */
1125 #define PFRES_PROTCKSUM 9		/* Protocol checksum invalid */
1126 #define PFRES_BADSTATE	10		/* State mismatch */
1127 #define PFRES_STATEINS	11		/* State insertion failure */
1128 #define PFRES_MAXSTATES	12		/* State limit */
1129 #define PFRES_SRCLIMIT	13		/* Source node/conn limit */
1130 #define PFRES_SYNPROXY	14		/* SYN proxy */
1131 #define PFRES_STATELOCKED 15    /* state table locked */
1132 #define PFRES_MAX	16		/* total+1 */
1133 
1134 #define PFRES_NAMES { \
1135 	"match", \
1136 	"bad-offset", \
1137 	"fragment", \
1138 	"short", \
1139 	"normalize", \
1140 	"memory", \
1141 	"bad-timestamp", \
1142 	"congestion", \
1143 	"ip-option", \
1144 	"proto-cksum", \
1145 	"state-mismatch", \
1146 	"state-insert", \
1147 	"state-limit", \
1148 	"src-limit", \
1149 	"synproxy", \
1150 	"state-locked", \
1151 	NULL \
1152 }
1153 
1154 /* Counters for other things we want to keep track of */
1155 #define LCNT_STATES		0	/* states */
1156 #define LCNT_SRCSTATES		1	/* max-src-states */
1157 #define LCNT_SRCNODES		2	/* max-src-nodes */
1158 #define LCNT_SRCCONN		3	/* max-src-conn */
1159 #define LCNT_SRCCONNRATE	4	/* max-src-conn-rate */
1160 #define LCNT_OVERLOAD_TABLE	5	/* entry added to overload table */
1161 #define LCNT_OVERLOAD_FLUSH	6	/* state entries flushed */
1162 #define LCNT_MAX		7	/* total+1 */
1163 
1164 #define LCNT_NAMES { \
1165 	"max states per rule", \
1166 	"max-src-states", \
1167 	"max-src-nodes", \
1168 	"max-src-conn", \
1169 	"max-src-conn-rate", \
1170 	"overload table insertion", \
1171 	"overload flush states", \
1172 	NULL \
1173 }
1174 
1175 /* UDP state enumeration */
1176 #define PFUDPS_NO_TRAFFIC	0
1177 #define PFUDPS_SINGLE		1
1178 #define PFUDPS_MULTIPLE		2
1179 
1180 #define PFUDPS_NSTATES		3	/* number of state levels */
1181 
1182 #define PFUDPS_NAMES { \
1183 	"NO_TRAFFIC", \
1184 	"SINGLE", \
1185 	"MULTIPLE", \
1186 	NULL \
1187 }
1188 
1189 /* Other protocol state enumeration */
1190 #define PFOTHERS_NO_TRAFFIC	0
1191 #define PFOTHERS_SINGLE		1
1192 #define PFOTHERS_MULTIPLE	2
1193 
1194 #define PFOTHERS_NSTATES	3	/* number of state levels */
1195 
1196 #define PFOTHERS_NAMES { \
1197 	"NO_TRAFFIC", \
1198 	"SINGLE", \
1199 	"MULTIPLE", \
1200 	NULL \
1201 }
1202 
1203 #define FCNT_STATE_SEARCH	0
1204 #define FCNT_STATE_INSERT	1
1205 #define FCNT_STATE_REMOVALS	2
1206 #define FCNT_MAX		3
1207 
1208 #define SCNT_SRC_NODE_SEARCH	0
1209 #define SCNT_SRC_NODE_INSERT	1
1210 #define SCNT_SRC_NODE_REMOVALS	2
1211 #define SCNT_MAX		3
1212 
1213 #define ACTION_SET(a, x) \
1214 	do { \
1215 		if ((a) != NULL) \
1216 			*(a) = (x); \
1217 	} while (0)
1218 
1219 #define REASON_SET(a, x) \
1220 	do { \
1221 		if ((a) != NULL) \
1222 			*(a) = (x); \
1223 		if (x < PFRES_MAX) \
1224 			pf_status.counters[x]++; \
1225 	} while (0)
1226 #define REASON_SET_NOPTR(a, x) \
1227 	do { \
1228 		*(a) = (x); \
1229 		if (x < PFRES_MAX) \
1230 			pf_status.counters[x]++; \
1231 	} while (0)
1232 
1233 struct pf_status {
1234 	u_int64_t	counters[PFRES_MAX];
1235 	u_int64_t	lcounters[LCNT_MAX];	/* limit counters */
1236 	u_int64_t	fcounters[FCNT_MAX];
1237 	u_int64_t	scounters[SCNT_MAX];
1238 	u_int64_t	pcounters[2][2][3];
1239 	u_int64_t	bcounters[2][2];
1240 	u_int64_t	stateid;
1241 	u_int32_t	running;
1242 	u_int32_t	states;
1243 	u_int32_t	src_nodes;
1244 	u_int32_t	since;
1245 	u_int32_t	debug;
1246 	u_int32_t	hostid;
1247 	char		ifname[IFNAMSIZ];
1248 	u_int8_t	pf_chksum[PF_MD5_DIGEST_LENGTH];
1249 };
1250 
1251 struct cbq_opts {
1252 	u_int		minburst;
1253 	u_int		maxburst;
1254 	u_int		pktsize;
1255 	u_int		maxpktsize;
1256 	u_int		ns_per_byte;
1257 	u_int		maxidle;
1258 	int		minidle;
1259 	u_int		offtime;
1260 	int		flags;
1261 };
1262 
1263 struct priq_opts {
1264 	int		flags;
1265 };
1266 
1267 struct hfsc_opts {
1268 	/* real-time service curve */
1269 	u_int		rtsc_m1;	/* slope of the 1st segment in bps */
1270 	u_int		rtsc_d;		/* the x-projection of m1 in msec */
1271 	u_int		rtsc_m2;	/* slope of the 2nd segment in bps */
1272 	/* link-sharing service curve */
1273 	u_int		lssc_m1;
1274 	u_int		lssc_d;
1275 	u_int		lssc_m2;
1276 	/* upper-limit service curve */
1277 	u_int		ulsc_m1;
1278 	u_int		ulsc_d;
1279 	u_int		ulsc_m2;
1280 	int		flags;
1281 };
1282 
1283 struct pf_altq {
1284 	char			 ifname[IFNAMSIZ];
1285 
1286 	void			*altq_disc;	/* discipline-specific state */
1287 	TAILQ_ENTRY(pf_altq)	 entries;
1288 
1289 	/* scheduler spec */
1290 	u_int8_t		 scheduler;	/* scheduler type */
1291 	u_int16_t		 tbrsize;	/* tokenbucket regulator size */
1292 	u_int32_t		 ifbandwidth;	/* interface bandwidth */
1293 
1294 	/* queue spec */
1295 	char			 qname[PF_QNAME_SIZE];	/* queue name */
1296 	char			 parent[PF_QNAME_SIZE];	/* parent name */
1297 	u_int32_t		 parent_qid;	/* parent queue id */
1298 	u_int32_t		 bandwidth;	/* queue bandwidth */
1299 	u_int8_t		 priority;	/* priority */
1300 	u_int16_t		 qlimit;	/* queue size limit */
1301 	u_int16_t		 flags;		/* misc flags */
1302 	union {
1303 		struct cbq_opts		 cbq_opts;
1304 		struct priq_opts	 priq_opts;
1305 		struct hfsc_opts	 hfsc_opts;
1306 	} pq_u;
1307 
1308 	u_int32_t		 qid;		/* return value */
1309 };
1310 
1311 struct pf_tag {
1312 	u_int16_t	tag;		/* tag id */
1313 };
1314 
1315 struct pf_tagname {
1316 	TAILQ_ENTRY(pf_tagname)	entries;
1317 	char			name[PF_TAG_NAME_SIZE];
1318 	u_int16_t		tag;
1319 	int			ref;
1320 };
1321 
1322 #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
1323 #define PFFRAG_FRAG_HIWAT	1000	/* Number of fragmented packets */
1324 #define PFFRAG_FRCENT_HIWAT	50000	/* Number of fragment cache entries */
1325 #define PFFRAG_FRCACHE_HIWAT	10000	/* Number of fragment descriptors */
1326 
1327 #define PFR_KTABLE_HIWAT	1000	/* Number of tables */
1328 #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
1329 #define PFR_KENTRY_HIWAT_SMALL	100000	/* Number of table entries (tiny hosts) */
1330 
1331 /*
1332  * ioctl parameter structures
1333  */
1334 
1335 struct pfioc_pooladdr {
1336 	u_int32_t		 action;
1337 	u_int32_t		 ticket;
1338 	u_int32_t		 nr;
1339 	u_int32_t		 r_num;
1340 	u_int8_t		 r_action;
1341 	u_int8_t		 r_last;
1342 	u_int8_t		 af;
1343 	char			 anchor[MAXPATHLEN];
1344 	struct pf_pooladdr	 addr;
1345 };
1346 
1347 struct pfioc_rule {
1348 	u_int32_t	 action;
1349 	u_int32_t	 ticket;
1350 	u_int32_t	 pool_ticket;
1351 	u_int32_t	 nr;
1352 	char		 anchor[MAXPATHLEN];
1353 	char		 anchor_call[MAXPATHLEN];
1354 	struct pf_rule	 rule;
1355 };
1356 
1357 struct pfioc_natlook {
1358 	struct pf_addr	 saddr;
1359 	struct pf_addr	 daddr;
1360 	struct pf_addr	 rsaddr;
1361 	struct pf_addr	 rdaddr;
1362 	u_int16_t	 sport;
1363 	u_int16_t	 dport;
1364 	u_int16_t	 rsport;
1365 	u_int16_t	 rdport;
1366 	sa_family_t	 af;
1367 	u_int8_t	 proto;
1368 	u_int8_t	 direction;
1369 };
1370 
1371 struct pfioc_state {
1372 	u_int32_t 	 nr;
1373 	void		*state;
1374 };
1375 
1376 struct pfioc_src_node_kill {
1377 	/* XXX returns the number of src nodes killed in psnk_af */
1378 	sa_family_t psnk_af;
1379 	struct pf_rule_addr psnk_src;
1380 	struct pf_rule_addr psnk_dst;
1381 };
1382 
1383 struct pfioc_state_kill {
1384 	/* XXX returns the number of states killed in psk_af */
1385 	sa_family_t		psk_af;
1386 	int			psk_proto;
1387 	struct pf_rule_addr	psk_src;
1388 	struct pf_rule_addr	psk_dst;
1389 	char			psk_ifname[IFNAMSIZ];
1390 };
1391 
1392 struct pfioc_states {
1393 	int	ps_len;
1394 	union {
1395 		void			*psu_buf;
1396 		struct pfsync_state	*psu_states;
1397 	} ps_u;
1398 #define ps_buf		ps_u.psu_buf
1399 #define ps_states	ps_u.psu_states
1400 };
1401 
1402 struct pfioc_src_nodes {
1403 	int	psn_len;
1404 	union {
1405 		void			*psu_buf;
1406 		struct pf_src_node	*psu_src_nodes;
1407 	} psn_u;
1408 #define psn_buf		psn_u.psu_buf
1409 #define psn_src_nodes	psn_u.psu_src_nodes
1410 };
1411 
1412 struct pfioc_if {
1413 	char		 ifname[IFNAMSIZ];
1414 };
1415 
1416 struct pfioc_tm {
1417 	int		 timeout;
1418 	int		 seconds;
1419 };
1420 
1421 struct pfioc_limit {
1422 	int		 index;
1423 	unsigned	 limit;
1424 };
1425 
1426 struct pfioc_altq {
1427 	u_int32_t	 action;
1428 	u_int32_t	 ticket;
1429 	u_int32_t	 nr;
1430 	struct pf_altq	 altq;
1431 };
1432 
1433 struct pfioc_qstats {
1434 	u_int32_t	 ticket;
1435 	u_int32_t	 nr;
1436 	void		*buf;
1437 	int		 nbytes;
1438 	u_int8_t	 scheduler;
1439 };
1440 
1441 struct pfioc_ruleset {
1442 	u_int32_t	 nr;
1443 	char		 path[MAXPATHLEN];
1444 	char		 name[PF_ANCHOR_NAME_SIZE];
1445 };
1446 
1447 #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
1448 #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1449 struct pfioc_trans {
1450 	int		 size;	/* number of elements */
1451 	int		 esize; /* size of each element in bytes */
1452 	struct pfioc_trans_e {
1453 		int		rs_num;
1454 		char		anchor[MAXPATHLEN];
1455 		u_int32_t	ticket;
1456 	}		*array;
1457 };
1458 
1459 #define PFR_FLAG_ATOMIC		0x00000001
1460 #define PFR_FLAG_DUMMY		0x00000002
1461 #define PFR_FLAG_FEEDBACK	0x00000004
1462 #define PFR_FLAG_CLSTATS	0x00000008
1463 #define PFR_FLAG_ADDRSTOO	0x00000010
1464 #define PFR_FLAG_REPLACE	0x00000020
1465 #define PFR_FLAG_ALLRSETS	0x00000040
1466 #define PFR_FLAG_ALLMASK	0x0000007F
1467 #ifdef _KERNEL
1468 #define PFR_FLAG_USERIOCTL	0x10000000
1469 #endif
1470 
1471 struct pfioc_table {
1472 	struct pfr_table	 pfrio_table;
1473 	void			*pfrio_buffer;
1474 	int			 pfrio_esize;
1475 	int			 pfrio_size;
1476 	int			 pfrio_size2;
1477 	int			 pfrio_nadd;
1478 	int			 pfrio_ndel;
1479 	int			 pfrio_nchange;
1480 	int			 pfrio_flags;
1481 	u_int32_t		 pfrio_ticket;
1482 };
1483 #define	pfrio_exists	pfrio_nadd
1484 #define	pfrio_nzero	pfrio_nadd
1485 #define	pfrio_nmatch	pfrio_nadd
1486 #define pfrio_naddr	pfrio_size2
1487 #define pfrio_setflag	pfrio_size2
1488 #define pfrio_clrflag	pfrio_nadd
1489 
1490 struct pfioc_iface {
1491 	char	 pfiio_name[IFNAMSIZ];
1492 	void	*pfiio_buffer;
1493 	int	 pfiio_esize;
1494 	int	 pfiio_size;
1495 	int	 pfiio_nzero;
1496 	int	 pfiio_flags;
1497 };
1498 
1499 
1500 /*
1501  * ioctl operations
1502  */
1503 
1504 #define DIOCSTART	_IO  ('D',  1)
1505 #define DIOCSTOP	_IO  ('D',  2)
1506 #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
1507 #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
1508 #define DIOCGETRULE	_IOWR('D',  7, struct pfioc_rule)
1509 #define DIOCSETLCK  _IOWR('D', 8, uint32_t)
1510 /* XXX cut 9 - 17 */
1511 #define DIOCCLRSTATES	_IOWR('D', 18, struct pfioc_state_kill)
1512 #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
1513 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1514 #define DIOCGETSTATUS	_IOWR('D', 21, struct pf_status)
1515 #define DIOCCLRSTATUS	_IO  ('D', 22)
1516 #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
1517 #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
1518 #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1519 #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
1520 /* XXX cut 26 - 28 */
1521 #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
1522 #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1523 #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1524 #define DIOCCLRRULECTRS	_IO  ('D', 38)
1525 #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
1526 #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
1527 #define DIOCKILLSTATES	_IOWR('D', 41, struct pfioc_state_kill)
1528 #define DIOCSTARTALTQ	_IO  ('D', 42)
1529 #define DIOCSTOPALTQ	_IO  ('D', 43)
1530 #define DIOCADDALTQ	_IOWR('D', 45, struct pfioc_altq)
1531 #define DIOCGETALTQS	_IOWR('D', 47, struct pfioc_altq)
1532 #define DIOCGETALTQ	_IOWR('D', 48, struct pfioc_altq)
1533 #define DIOCCHANGEALTQ	_IOWR('D', 49, struct pfioc_altq)
1534 #define DIOCGETQSTATS	_IOWR('D', 50, struct pfioc_qstats)
1535 #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
1536 #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
1537 #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
1538 #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1539 #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
1540 #define DIOCADDSTATES   _IOWR('D', 56, struct pfioc_states)
1541 /* XXX cut 57 - 57 */
1542 #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
1543 #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
1544 #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
1545 #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
1546 #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
1547 #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
1548 #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
1549 #define DIOCRCLRTSTATS  _IOWR('D', 65, struct pfioc_table)
1550 #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
1551 #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
1552 #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
1553 #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
1554 #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
1555 #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
1556 #define DIOCRCLRASTATS  _IOWR('D', 72, struct pfioc_table)
1557 #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
1558 #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
1559 #define DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
1560 #define DIOCOSFPFLUSH	_IO('D', 78)
1561 #define DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
1562 #define DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
1563 #define DIOCXBEGIN      _IOWR('D', 81, struct pfioc_trans)
1564 #define DIOCXCOMMIT     _IOWR('D', 82, struct pfioc_trans)
1565 #define DIOCXROLLBACK   _IOWR('D', 83, struct pfioc_trans)
1566 #define DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
1567 #define DIOCCLRSRCNODES	_IO('D', 85)
1568 #define DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
1569 #define DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
1570 #define DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
1571 #define DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
1572 #define DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
1573 
1574 #ifdef _KERNEL
1575 RB_HEAD(pf_src_tree, pf_src_node);
1576 RB_PROTOTYPE(pf_src_tree, pf_src_node, entry, pf_src_compare);
1577 extern struct pf_src_tree tree_src_tracking;
1578 
1579 RB_HEAD(pf_state_tree_id, pf_state);
1580 RB_PROTOTYPE(pf_state_tree_id, pf_state,
1581     entry_id, pf_state_compare_id);
1582 extern struct pf_state_tree_id tree_id;
1583 extern struct pf_state_queue state_list;
1584 
1585 TAILQ_HEAD(pf_poolqueue, pf_pool);
1586 extern struct pf_poolqueue		  pf_pools[2];
1587 TAILQ_HEAD(pf_altqqueue, pf_altq);
1588 extern struct pf_altqqueue		  pf_altqs[2];
1589 extern struct pf_palist			  pf_pabuf;
1590 
1591 extern u_int32_t		 ticket_altqs_active;
1592 extern u_int32_t		 ticket_altqs_inactive;
1593 extern int			 altqs_inactive_open;
1594 extern u_int32_t		 ticket_pabuf;
1595 extern struct pf_altqqueue	*pf_altqs_active;
1596 extern struct pf_altqqueue	*pf_altqs_inactive;
1597 extern struct pf_poolqueue	*pf_pools_active;
1598 extern struct pf_poolqueue	*pf_pools_inactive;
1599 extern int			 pf_tbladdr_setup(struct pf_ruleset *,
1600 				    struct pf_addr_wrap *);
1601 extern void			 pf_tbladdr_remove(struct pf_addr_wrap *);
1602 extern void			 pf_tbladdr_copyout(struct pf_addr_wrap *);
1603 extern void			 pf_calc_skip_steps(struct pf_rulequeue *);
1604 extern struct pool		 pf_src_tree_pl, pf_rule_pl;
1605 extern struct pool		 pf_state_pl, pf_state_key_pl, pf_altq_pl,
1606 				    pf_pooladdr_pl;
1607 extern struct pool		 pf_state_scrub_pl;
1608 extern void			 pf_purge_thread(void *);
1609 extern void			 pf_purge_expired_src_nodes(int);
1610 extern void			 pf_purge_expired_states(u_int32_t);
1611 extern void			 pf_unlink_state(struct pf_state *);
1612 extern void			 pf_free_state(struct pf_state *);
1613 extern int			 pf_insert_state(struct pfi_kif *,
1614 				    struct pf_state *);
1615 extern int			 pf_insert_src_node(struct pf_src_node **,
1616 				    struct pf_rule *, struct pf_addr *,
1617 				    sa_family_t);
1618 void				 pf_src_tree_remove_state(struct pf_state *);
1619 extern struct pf_state		*pf_find_state_byid(struct pf_state_cmp *);
1620 extern struct pf_state		*pf_find_state_all(struct pf_state_key_cmp *,
1621 				    u_int8_t, int *);
1622 extern void			 pf_print_state(struct pf_state *);
1623 extern void			 pf_print_flags(u_int8_t);
1624 extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
1625 				    u_int8_t);
1626 
1627 extern struct ifnet		*sync_ifp;
1628 extern struct pf_rule		 pf_default_rule;
1629 extern void			 pf_addrcpy(struct pf_addr *,
1630 				    const struct pf_addr *, u_int8_t);
1631 void				 pf_rm_rule(struct pf_rulequeue *,
1632 				    struct pf_rule *);
1633 
1634 #ifdef INET
1635 int	pf_test(int, struct ifnet *, struct mbuf **, struct ether_header *);
1636 #endif /* INET */
1637 
1638 #ifdef INET6
1639 int	pf_test6(int, struct ifnet *, struct mbuf **, struct ether_header *);
1640 void	pf_poolmask(struct pf_addr *, struct pf_addr*,
1641 	    struct pf_addr *, const struct pf_addr *, u_int8_t);
1642 void	pf_addr_inc(struct pf_addr *, sa_family_t);
1643 #endif /* INET6 */
1644 
1645 void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
1646 	    sa_family_t);
1647 void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
1648 int	pflog_packet(struct pfi_kif *, struct mbuf *, sa_family_t, u_int8_t,
1649 	    u_int8_t, struct pf_rule *, struct pf_rule *, struct pf_ruleset *,
1650 	    struct pf_pdesc *);
1651 int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
1652 	    struct pf_addr *, sa_family_t);
1653 int	pf_match(u_int8_t, u_int32_t, u_int32_t, u_int32_t);
1654 int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
1655 int	pf_match_uid(u_int8_t, uid_t, uid_t, uid_t);
1656 int	pf_match_gid(u_int8_t, gid_t, gid_t, gid_t);
1657 
1658 void	pf_normalize_init(void);
1659 #ifdef _MODULE
1660 void	pf_normalize_destroy(void);
1661 #endif /* _MODULE */
1662 int	pf_normalize_ip(struct mbuf **, int, struct pfi_kif *, u_short *,
1663 	    struct pf_pdesc *);
1664 int	pf_normalize_ip6(struct mbuf **, int, struct pfi_kif *, u_short *,
1665 	    struct pf_pdesc *);
1666 int	pf_normalize_tcp(int, struct pfi_kif *, struct mbuf *, int, int, void *,
1667 	    struct pf_pdesc *);
1668 void	pf_normalize_tcp_cleanup(struct pf_state *);
1669 int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
1670 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
1671 int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
1672 	    u_short *, struct tcphdr *, struct pf_state *,
1673 	    struct pf_state_peer *, struct pf_state_peer *, int *);
1674 u_int32_t
1675 	pf_state_expires(const struct pf_state *);
1676 void	pf_purge_expired_fragments(void);
1677 int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *);
1678 int	pf_rtlabel_match(struct pf_addr *, sa_family_t, struct pf_addr_wrap *);
1679 int	pf_socket_lookup(int, struct pf_pdesc *);
1680 struct pf_state_key *
1681 	pf_alloc_state_key(struct pf_state *);
1682 void	pfr_initialize(void);
1683 #ifdef _MODULE
1684 void	pfr_destroy(void);
1685 #endif /* _MODULE */
1686 int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
1687 void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
1688 	    u_int64_t, int, int, int);
1689 int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *,
1690 	    struct pf_addr **, struct pf_addr **, sa_family_t);
1691 void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
1692 struct pfr_ktable *
1693 	pfr_attach_table(struct pf_ruleset *, char *);
1694 void	pfr_detach_table(struct pfr_ktable *);
1695 int	pfr_clr_tables(struct pfr_table *, int *, int);
1696 int	pfr_add_tables(struct pfr_table *, int, int *, int);
1697 int	pfr_del_tables(struct pfr_table *, int, int *, int);
1698 int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
1699 int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
1700 int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
1701 int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
1702 int	pfr_clr_addrs(struct pfr_table *, int *, int);
1703 int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
1704 int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1705 	    int);
1706 int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1707 	    int);
1708 int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1709 	    int *, int *, int *, int, u_int32_t);
1710 int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
1711 int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
1712 int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
1713 	    int);
1714 int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1715 	    int);
1716 int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
1717 int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
1718 int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
1719 int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
1720 	    int *, u_int32_t, int);
1721 
1722 extern struct pfi_kif		*pfi_all;
1723 
1724 void		 pfi_initialize(void);
1725 #ifdef _MODULE
1726 void		 pfi_destroy(void);
1727 #endif /* _MODULE */
1728 struct pfi_kif	*pfi_kif_get(const char *);
1729 void		 pfi_kif_ref(struct pfi_kif *, enum pfi_kif_refs);
1730 void		 pfi_kif_unref(struct pfi_kif *, enum pfi_kif_refs);
1731 int		 pfi_kif_match(struct pfi_kif *, struct pfi_kif *);
1732 void		 pfi_attach_ifnet(struct ifnet *);
1733 void		 pfi_detach_ifnet(struct ifnet *);
1734 void		 pfi_attach_ifgroup(struct ifg_group *);
1735 void		 pfi_detach_ifgroup(struct ifg_group *);
1736 void		 pfi_group_change(const char *);
1737 int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
1738 		    sa_family_t);
1739 int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
1740 void		 pfi_dynaddr_remove(struct pf_addr_wrap *);
1741 void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
1742 void		 pfi_fill_oldstatus(struct pf_status *);
1743 int		 pfi_clr_istats(const char *);
1744 int		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
1745 int		 pfi_set_flags(const char *, int);
1746 int		 pfi_clear_flags(const char *, int);
1747 
1748 u_int16_t	 pf_tagname2tag(char *);
1749 void		 pf_tag2tagname(u_int16_t, char *);
1750 void		 pf_tag_ref(u_int16_t);
1751 void		 pf_tag_unref(u_int16_t);
1752 int		 pf_tag_packet(struct mbuf *, int, int);
1753 u_int32_t	 pf_qname2qid(char *);
1754 void		 pf_qid2qname(u_int32_t, char *);
1755 void		 pf_qid_unref(u_int32_t);
1756 
1757 extern struct pf_status	pf_status;
1758 extern struct pool	pf_frent_pl, pf_frag_pl;
1759 #ifdef __NetBSD__
1760 extern krwlock_t	pf_consistency_lock;
1761 #else
1762 extern struct rwlock	pf_consistency_lock;
1763 #endif /* !__NetBSD__ */
1764 
1765 struct pf_pool_limit {
1766 	void		*pp;
1767 	unsigned	 limit;
1768 };
1769 extern struct pf_pool_limit	pf_pool_limits[PF_LIMIT_MAX];
1770 
1771 /*
1772  * misc compatibility definitions
1773  */
1774 #if !defined(PRIu32)
1775 #define	PRIu32	"u"	/* XXX */
1776 #endif
1777 
1778 #if !defined(NO_PID)
1779 #define NO_PID		(PID_MAX + 1)
1780 #endif
1781 
1782 #if defined(__NetBSD__)
1783 /* rwlock compatibility definitions */
1784 #define rw_enter_write(a)	rw_enter((a), (RW_WRITER))
1785 #define rw_exit_write(a)	rw_exit((a))
1786 #define rw_enter_read(a)	rw_enter((a), (RW_READER))
1787 #define rw_exit_read(a)		rw_exit((a))
1788 #endif /* __NetBSD__ */
1789 
1790 #endif /* _KERNEL */
1791 
1792 extern struct pf_anchor_global  pf_anchors;
1793 extern struct pf_anchor        pf_main_anchor;
1794 #define pf_main_ruleset	pf_main_anchor.ruleset
1795 
1796 /* these ruleset functions can be linked into userland programs (pfctl) */
1797 int			 pf_get_ruleset_number(u_int8_t);
1798 void			 pf_init_ruleset(struct pf_ruleset *);
1799 int			 pf_anchor_setup(struct pf_rule *,
1800 			    const struct pf_ruleset *, const char *);
1801 int			 pf_anchor_copyout(const struct pf_ruleset *,
1802 			    const struct pf_rule *, struct pfioc_rule *);
1803 void			 pf_anchor_remove(struct pf_rule *);
1804 void			 pf_remove_if_empty_ruleset(struct pf_ruleset *);
1805 struct pf_anchor	*pf_find_anchor(const char *);
1806 struct pf_ruleset	*pf_find_ruleset(const char *);
1807 struct pf_ruleset	*pf_find_or_create_ruleset(const char *);
1808 void			 pf_rs_initialize(void);
1809 
1810 #ifdef _KERNEL
1811 int			 pf_anchor_copyout(const struct pf_ruleset *,
1812 			    const struct pf_rule *, struct pfioc_rule *);
1813 void			 pf_anchor_remove(struct pf_rule *);
1814 
1815 #endif /* _KERNEL */
1816 
1817 /* The fingerprint functions can be linked into userland programs (tcpdump) */
1818 int	pf_osfp_add(struct pf_osfp_ioctl *);
1819 #ifdef _KERNEL
1820 struct pf_osfp_enlist *
1821 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
1822 	    const struct tcphdr *);
1823 #endif /* _KERNEL */
1824 struct pf_osfp_enlist *
1825 	pf_osfp_fingerprint_hdr(const struct ip *, const struct ip6_hdr *,
1826 	    const struct tcphdr *);
1827 void	pf_osfp_flush(void);
1828 int	pf_osfp_get(struct pf_osfp_ioctl *);
1829 void	pf_osfp_initialize(void);
1830 #ifdef _MODULE
1831 void	pf_osfp_destroy(void);
1832 #endif /* _MODULE */
1833 int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
1834 struct pf_os_fingerprint *
1835 	pf_osfp_validate(void);
1836 
1837 
1838 #endif /* _NET_PFVAR_H_ */
1839