xref: /freebsd/sys/net/pfvar.h (revision dbaad75f2834f40bfe74ebe393d2101967052036)
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_PFVAR_H_
36 #define _NET_PFVAR_H_
37 
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/counter.h>
41 #include <sys/cpuset.h>
42 #include <sys/malloc.h>
43 #include <sys/nv.h>
44 #include <sys/refcount.h>
45 #include <sys/sdt.h>
46 #include <sys/sysctl.h>
47 #include <sys/lock.h>
48 #include <sys/rmlock.h>
49 #include <sys/tree.h>
50 #include <vm/uma.h>
51 
52 #include <net/radix.h>
53 #include <netinet/in.h>
54 #ifdef _KERNEL
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 #include <netinet/udp.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/icmp6.h>
60 #endif
61 
62 #include <netpfil/pf/pf.h>
63 #include <netpfil/pf/pf_altq.h>
64 #include <netpfil/pf/pf_mtag.h>
65 
66 #ifdef _KERNEL
67 
68 SYSCTL_DECL(_net_pf);
69 MALLOC_DECLARE(M_PFHASH);
70 
71 SDT_PROVIDER_DECLARE(pf);
72 
73 struct pfi_dynaddr {
74 	TAILQ_ENTRY(pfi_dynaddr)	 entry;
75 	struct pf_addr			 pfid_addr4;
76 	struct pf_addr			 pfid_mask4;
77 	struct pf_addr			 pfid_addr6;
78 	struct pf_addr			 pfid_mask6;
79 	struct pfr_ktable		*pfid_kt;
80 	struct pfi_kkif			*pfid_kif;
81 	int				 pfid_net;	/* mask or 128 */
82 	int				 pfid_acnt4;	/* address count IPv4 */
83 	int				 pfid_acnt6;	/* address count IPv6 */
84 	sa_family_t			 pfid_af;	/* rule af */
85 	u_int8_t			 pfid_iflags;	/* PFI_AFLAG_* */
86 };
87 
88 /*
89  * Address manipulation macros
90  */
91 #define	HTONL(x)	(x) = htonl((__uint32_t)(x))
92 #define	HTONS(x)	(x) = htons((__uint16_t)(x))
93 #define	NTOHL(x)	(x) = ntohl((__uint32_t)(x))
94 #define	NTOHS(x)	(x) = ntohs((__uint16_t)(x))
95 
96 #define	PF_NAME		"pf"
97 
98 #define	PF_HASHROW_ASSERT(h)	mtx_assert(&(h)->lock, MA_OWNED)
99 #define	PF_HASHROW_LOCK(h)	mtx_lock(&(h)->lock)
100 #define	PF_HASHROW_UNLOCK(h)	mtx_unlock(&(h)->lock)
101 
102 #define	PF_STATE_LOCK(s)						\
103 	do {								\
104 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)];	\
105 		PF_HASHROW_LOCK(_ih);					\
106 	} while (0)
107 
108 #define	PF_STATE_UNLOCK(s)						\
109 	do {								\
110 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH((s))];	\
111 		PF_HASHROW_UNLOCK(_ih);					\
112 	} while (0)
113 
114 #ifdef INVARIANTS
115 #define	PF_STATE_LOCK_ASSERT(s)						\
116 	do {								\
117 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)];	\
118 		PF_HASHROW_ASSERT(_ih);					\
119 	} while (0)
120 #else /* !INVARIANTS */
121 #define	PF_STATE_LOCK_ASSERT(s)		do {} while (0)
122 #endif /* INVARIANTS */
123 
124 extern struct mtx_padalign pf_unlnkdrules_mtx;
125 #define	PF_UNLNKDRULES_LOCK()	mtx_lock(&pf_unlnkdrules_mtx)
126 #define	PF_UNLNKDRULES_UNLOCK()	mtx_unlock(&pf_unlnkdrules_mtx)
127 
128 extern struct rmlock pf_rules_lock;
129 #define	PF_RULES_RLOCK_TRACKER	struct rm_priotracker _pf_rules_tracker
130 #define	PF_RULES_RLOCK()	rm_rlock(&pf_rules_lock, &_pf_rules_tracker)
131 #define	PF_RULES_RUNLOCK()	rm_runlock(&pf_rules_lock, &_pf_rules_tracker)
132 #define	PF_RULES_WLOCK()	rm_wlock(&pf_rules_lock)
133 #define	PF_RULES_WUNLOCK()	rm_wunlock(&pf_rules_lock)
134 #define	PF_RULES_WOWNED()	rm_wowned(&pf_rules_lock)
135 #define	PF_RULES_ASSERT()	rm_assert(&pf_rules_lock, RA_LOCKED)
136 #define	PF_RULES_RASSERT()	rm_assert(&pf_rules_lock, RA_RLOCKED)
137 #define	PF_RULES_WASSERT()	rm_assert(&pf_rules_lock, RA_WLOCKED)
138 
139 extern struct mtx_padalign pf_table_stats_lock;
140 #define	PF_TABLE_STATS_LOCK()	mtx_lock(&pf_table_stats_lock)
141 #define	PF_TABLE_STATS_UNLOCK()	mtx_unlock(&pf_table_stats_lock)
142 #define	PF_TABLE_STATS_OWNED()	mtx_owned(&pf_table_stats_lock)
143 #define	PF_TABLE_STATS_ASSERT()	mtx_assert(&pf_rules_lock, MA_OWNED)
144 
145 extern struct sx pf_end_lock;
146 
147 #define	PF_MODVER	1
148 #define	PFLOG_MODVER	1
149 #define	PFSYNC_MODVER	1
150 
151 #define	PFLOG_MINVER	1
152 #define	PFLOG_PREFVER	PFLOG_MODVER
153 #define	PFLOG_MAXVER	1
154 #define	PFSYNC_MINVER	1
155 #define	PFSYNC_PREFVER	PFSYNC_MODVER
156 #define	PFSYNC_MAXVER	1
157 
158 #ifdef INET
159 #ifndef INET6
160 #define	PF_INET_ONLY
161 #endif /* ! INET6 */
162 #endif /* INET */
163 
164 #ifdef INET6
165 #ifndef INET
166 #define	PF_INET6_ONLY
167 #endif /* ! INET */
168 #endif /* INET6 */
169 
170 #ifdef INET
171 #ifdef INET6
172 #define	PF_INET_INET6
173 #endif /* INET6 */
174 #endif /* INET */
175 
176 #else
177 
178 #define	PF_INET_INET6
179 
180 #endif /* _KERNEL */
181 
182 /* Both IPv4 and IPv6 */
183 #ifdef PF_INET_INET6
184 
185 #define PF_AEQ(a, b, c) \
186 	((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
187 	(c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \
188 	(a)->addr32[2] == (b)->addr32[2] && \
189 	(a)->addr32[1] == (b)->addr32[1] && \
190 	(a)->addr32[0] == (b)->addr32[0])) \
191 
192 #define PF_ANEQ(a, b, c) \
193 	((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
194 	(c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \
195 	(a)->addr32[1] != (b)->addr32[1] || \
196 	(a)->addr32[2] != (b)->addr32[2] || \
197 	(a)->addr32[3] != (b)->addr32[3]))) \
198 
199 #define PF_AZERO(a, c) \
200 	((c == AF_INET && !(a)->addr32[0]) || \
201 	(c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \
202 	!(a)->addr32[2] && !(a)->addr32[3] )) \
203 
204 #define PF_MATCHA(n, a, m, b, f) \
205 	pf_match_addr(n, a, m, b, f)
206 
207 #define PF_ACPY(a, b, f) \
208 	pf_addrcpy(a, b, f)
209 
210 #define PF_AINC(a, f) \
211 	pf_addr_inc(a, f)
212 
213 #define PF_POOLMASK(a, b, c, d, f) \
214 	pf_poolmask(a, b, c, d, f)
215 
216 #else
217 
218 /* Just IPv6 */
219 
220 #ifdef PF_INET6_ONLY
221 
222 #define PF_AEQ(a, b, c) \
223 	((a)->addr32[3] == (b)->addr32[3] && \
224 	(a)->addr32[2] == (b)->addr32[2] && \
225 	(a)->addr32[1] == (b)->addr32[1] && \
226 	(a)->addr32[0] == (b)->addr32[0]) \
227 
228 #define PF_ANEQ(a, b, c) \
229 	((a)->addr32[3] != (b)->addr32[3] || \
230 	(a)->addr32[2] != (b)->addr32[2] || \
231 	(a)->addr32[1] != (b)->addr32[1] || \
232 	(a)->addr32[0] != (b)->addr32[0]) \
233 
234 #define PF_AZERO(a, c) \
235 	(!(a)->addr32[0] && \
236 	!(a)->addr32[1] && \
237 	!(a)->addr32[2] && \
238 	!(a)->addr32[3] ) \
239 
240 #define PF_MATCHA(n, a, m, b, f) \
241 	pf_match_addr(n, a, m, b, f)
242 
243 #define PF_ACPY(a, b, f) \
244 	pf_addrcpy(a, b, f)
245 
246 #define PF_AINC(a, f) \
247 	pf_addr_inc(a, f)
248 
249 #define PF_POOLMASK(a, b, c, d, f) \
250 	pf_poolmask(a, b, c, d, f)
251 
252 #else
253 
254 /* Just IPv4 */
255 #ifdef PF_INET_ONLY
256 
257 #define PF_AEQ(a, b, c) \
258 	((a)->addr32[0] == (b)->addr32[0])
259 
260 #define PF_ANEQ(a, b, c) \
261 	((a)->addr32[0] != (b)->addr32[0])
262 
263 #define PF_AZERO(a, c) \
264 	(!(a)->addr32[0])
265 
266 #define PF_MATCHA(n, a, m, b, f) \
267 	pf_match_addr(n, a, m, b, f)
268 
269 #define PF_ACPY(a, b, f) \
270 	(a)->v4.s_addr = (b)->v4.s_addr
271 
272 #define PF_AINC(a, f) \
273 	do { \
274 		(a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
275 	} while (0)
276 
277 #define PF_POOLMASK(a, b, c, d, f) \
278 	do { \
279 		(a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
280 		(((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
281 	} while (0)
282 
283 #endif /* PF_INET_ONLY */
284 #endif /* PF_INET6_ONLY */
285 #endif /* PF_INET_INET6 */
286 
287 /*
288  * XXX callers not FIB-aware in our version of pf yet.
289  * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio.
290  */
291 #define	PF_MISMATCHAW(aw, x, af, neg, ifp, rtid)			\
292 	(								\
293 		(((aw)->type == PF_ADDR_NOROUTE &&			\
294 		    pf_routable((x), (af), NULL, (rtid))) ||		\
295 		(((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&	\
296 		    pf_routable((x), (af), (ifp), (rtid))) ||		\
297 		((aw)->type == PF_ADDR_TABLE &&				\
298 		    !pfr_match_addr((aw)->p.tbl, (x), (af))) ||		\
299 		((aw)->type == PF_ADDR_DYNIFTL &&			\
300 		    !pfi_match_addr((aw)->p.dyn, (x), (af))) ||		\
301 		((aw)->type == PF_ADDR_RANGE &&				\
302 		    !pf_match_addr_range(&(aw)->v.a.addr,		\
303 		    &(aw)->v.a.mask, (x), (af))) ||			\
304 		((aw)->type == PF_ADDR_ADDRMASK &&			\
305 		    !PF_AZERO(&(aw)->v.a.mask, (af)) &&			\
306 		    !PF_MATCHA(0, &(aw)->v.a.addr,			\
307 		    &(aw)->v.a.mask, (x), (af))))) !=			\
308 		(neg)							\
309 	)
310 
311 #define PF_ALGNMNT(off) (((off) % 2) == 0)
312 
313 #ifdef _KERNEL
314 
315 struct pf_kpooladdr {
316 	struct pf_addr_wrap		 addr;
317 	TAILQ_ENTRY(pf_kpooladdr)	 entries;
318 	char				 ifname[IFNAMSIZ];
319 	struct pfi_kkif			*kif;
320 };
321 
322 TAILQ_HEAD(pf_kpalist, pf_kpooladdr);
323 
324 struct pf_kpool {
325 	struct pf_kpalist	 list;
326 	struct pf_kpooladdr	*cur;
327 	struct pf_poolhashkey	 key;
328 	struct pf_addr		 counter;
329 	struct pf_mape_portset	 mape;
330 	int			 tblidx;
331 	u_int16_t		 proxy_port[2];
332 	u_int8_t		 opts;
333 };
334 
335 struct pf_rule_actions {
336 	u_int32_t       qid;
337 	u_int32_t       pqid;
338 };
339 
340 union pf_krule_ptr {
341 	struct pf_krule		*ptr;
342 	u_int32_t		 nr;
343 };
344 
345 struct pf_krule {
346 	struct pf_rule_addr	 src;
347 	struct pf_rule_addr	 dst;
348 	union pf_krule_ptr	 skip[PF_SKIP_COUNT];
349 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
350 	char			 ifname[IFNAMSIZ];
351 	char			 qname[PF_QNAME_SIZE];
352 	char			 pqname[PF_QNAME_SIZE];
353 	char			 tagname[PF_TAG_NAME_SIZE];
354 	char			 match_tagname[PF_TAG_NAME_SIZE];
355 
356 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
357 
358 	TAILQ_ENTRY(pf_krule)	 entries;
359 	struct pf_kpool		 rpool;
360 
361 	counter_u64_t		 evaluations;
362 	counter_u64_t		 packets[2];
363 	counter_u64_t		 bytes[2];
364 
365 	struct pfi_kkif		*kif;
366 	struct pf_kanchor	*anchor;
367 	struct pfr_ktable	*overload_tbl;
368 
369 	pf_osfp_t		 os_fingerprint;
370 
371 	int			 rtableid;
372 	u_int32_t		 timeout[PFTM_MAX];
373 	u_int32_t		 max_states;
374 	u_int32_t		 max_src_nodes;
375 	u_int32_t		 max_src_states;
376 	u_int32_t		 max_src_conn;
377 	struct {
378 		u_int32_t		limit;
379 		u_int32_t		seconds;
380 	}			 max_src_conn_rate;
381 	u_int32_t		 qid;
382 	u_int32_t		 pqid;
383 	u_int32_t		 nr;
384 	u_int32_t		 prob;
385 	uid_t			 cuid;
386 	pid_t			 cpid;
387 
388 	counter_u64_t		 states_cur;
389 	counter_u64_t		 states_tot;
390 	counter_u64_t		 src_nodes;
391 
392 	u_int16_t		 return_icmp;
393 	u_int16_t		 return_icmp6;
394 	u_int16_t		 max_mss;
395 	u_int16_t		 tag;
396 	u_int16_t		 match_tag;
397 	u_int16_t		 scrub_flags;
398 
399 	struct pf_rule_uid	 uid;
400 	struct pf_rule_gid	 gid;
401 
402 	u_int32_t		 rule_flag;
403 	uint32_t		 rule_ref;
404 	u_int8_t		 action;
405 	u_int8_t		 direction;
406 	u_int8_t		 log;
407 	u_int8_t		 logif;
408 	u_int8_t		 quick;
409 	u_int8_t		 ifnot;
410 	u_int8_t		 match_tag_not;
411 	u_int8_t		 natpass;
412 
413 	u_int8_t		 keep_state;
414 	sa_family_t		 af;
415 	u_int8_t		 proto;
416 	u_int8_t		 type;
417 	u_int8_t		 code;
418 	u_int8_t		 flags;
419 	u_int8_t		 flagset;
420 	u_int8_t		 min_ttl;
421 	u_int8_t		 allow_opts;
422 	u_int8_t		 rt;
423 	u_int8_t		 return_ttl;
424 	u_int8_t		 tos;
425 	u_int8_t		 set_tos;
426 	u_int8_t		 anchor_relative;
427 	u_int8_t		 anchor_wildcard;
428 
429 	u_int8_t		 flush;
430 	u_int8_t		 prio;
431 	u_int8_t		 set_prio[2];
432 
433 	struct {
434 		struct pf_addr		addr;
435 		u_int16_t		port;
436 	}			divert;
437 };
438 
439 struct pf_ksrc_node {
440 	LIST_ENTRY(pf_ksrc_node) entry;
441 	struct pf_addr	 addr;
442 	struct pf_addr	 raddr;
443 	union pf_krule_ptr rule;
444 	struct pfi_kkif	*kif;
445 	counter_u64_t	 bytes[2];
446 	counter_u64_t	 packets[2];
447 	u_int32_t	 states;
448 	u_int32_t	 conn;
449 	struct pf_threshold	conn_rate;
450 	u_int32_t	 creation;
451 	u_int32_t	 expire;
452 	sa_family_t	 af;
453 	u_int8_t	 ruletype;
454 };
455 #endif
456 
457 struct pf_state_scrub {
458 	struct timeval	pfss_last;	/* time received last packet	*/
459 	u_int32_t	pfss_tsecr;	/* last echoed timestamp	*/
460 	u_int32_t	pfss_tsval;	/* largest timestamp		*/
461 	u_int32_t	pfss_tsval0;	/* original timestamp		*/
462 	u_int16_t	pfss_flags;
463 #define PFSS_TIMESTAMP	0x0001		/* modulate timestamp		*/
464 #define PFSS_PAWS	0x0010		/* stricter PAWS checks		*/
465 #define PFSS_PAWS_IDLED	0x0020		/* was idle too long.  no PAWS	*/
466 #define PFSS_DATA_TS	0x0040		/* timestamp on data packets	*/
467 #define PFSS_DATA_NOTS	0x0080		/* no timestamp on data packets	*/
468 	u_int8_t	pfss_ttl;	/* stashed TTL			*/
469 	u_int8_t	pad;
470 	u_int32_t	pfss_ts_mod;	/* timestamp modulation		*/
471 };
472 
473 struct pf_state_host {
474 	struct pf_addr	addr;
475 	u_int16_t	port;
476 	u_int16_t	pad;
477 };
478 
479 struct pf_state_peer {
480 	struct pf_state_scrub	*scrub;	/* state is scrubbed		*/
481 	u_int32_t	seqlo;		/* Max sequence number sent	*/
482 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
483 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
484 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
485 	u_int16_t	mss;		/* Maximum segment size option	*/
486 	u_int8_t	state;		/* active state level		*/
487 	u_int8_t	wscale;		/* window scaling factor	*/
488 	u_int8_t	tcp_est;	/* Did we reach TCPS_ESTABLISHED */
489 	u_int8_t	pad[1];
490 };
491 
492 /* Keep synced with struct pf_state_key. */
493 struct pf_state_key_cmp {
494 	struct pf_addr	 addr[2];
495 	u_int16_t	 port[2];
496 	sa_family_t	 af;
497 	u_int8_t	 proto;
498 	u_int8_t	 pad[2];
499 };
500 
501 struct pf_state_key {
502 	struct pf_addr	 addr[2];
503 	u_int16_t	 port[2];
504 	sa_family_t	 af;
505 	u_int8_t	 proto;
506 	u_int8_t	 pad[2];
507 
508 	LIST_ENTRY(pf_state_key) entry;
509 	TAILQ_HEAD(, pf_kstate)	 states[2];
510 };
511 
512 /* Keep synced with struct pf_kstate. */
513 struct pf_state_cmp {
514 	u_int64_t		 id;
515 	u_int32_t		 creatorid;
516 	u_int8_t		 direction;
517 	u_int8_t		 pad[3];
518 };
519 
520 #define	PFSTATE_ALLOWOPTS	0x01
521 #define	PFSTATE_SLOPPY		0x02
522 /*  was	PFSTATE_PFLOW		0x04 */
523 #define	PFSTATE_NOSYNC		0x08
524 #define	PFSTATE_ACK		0x10
525 #define	PFSTATE_SETPRIO		0x0200
526 #define	PFSTATE_SETMASK   (PFSTATE_SETPRIO)
527 
528 struct pf_state_scrub_export {
529 	uint16_t	pfss_flags;
530 	uint8_t		pfss_ttl;	/* stashed TTL		*/
531 #define PF_SCRUB_FLAG_VALID		0x01
532 	uint8_t		scrub_flag;
533 	uint32_t	pfss_ts_mod;	/* timestamp modulation	*/
534 };
535 
536 struct pf_state_key_export {
537 	struct pf_addr	 addr[2];
538 	uint16_t	 port[2];
539 };
540 
541 struct pf_state_peer_export {
542 	struct pf_state_scrub_export	scrub;	/* state is scrubbed	*/
543 	uint32_t	seqlo;		/* Max sequence number sent	*/
544 	uint32_t	seqhi;		/* Max the other end ACKd + win	*/
545 	uint32_t	seqdiff;	/* Sequence number modulator	*/
546 	uint16_t	max_win;	/* largest window (pre scaling)	*/
547 	uint16_t	mss;		/* Maximum segment size option	*/
548 	uint8_t		state;		/* active state level		*/
549 	uint8_t		wscale;		/* window scaling factor	*/
550 	uint8_t		dummy[6];
551 };
552 _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect");
553 
554 struct pf_state_export {
555 	uint64_t	 version;
556 #define	PF_STATE_VERSION	20210706
557 	uint64_t	 id;
558 	char		 ifname[IFNAMSIZ];
559 	char		 orig_ifname[IFNAMSIZ];
560 	struct pf_state_key_export	 key[2];
561 	struct pf_state_peer_export	 src;
562 	struct pf_state_peer_export	 dst;
563 	struct pf_addr	 rt_addr;
564 	uint32_t	 rule;
565 	uint32_t	 anchor;
566 	uint32_t	 nat_rule;
567 	uint32_t	 creation;
568 	uint32_t	 expire;
569 	uint32_t	 spare0;
570 	uint64_t	 packets[2];
571 	uint64_t	 bytes[2];
572 	uint32_t	 creatorid;
573 	uint32_t	 spare1;
574 	sa_family_t	 af;
575 	uint8_t		 proto;
576 	uint8_t		 direction;
577 	uint8_t		 log;
578 	uint8_t		 state_flags;
579 	uint8_t		 timeout;
580 	uint8_t		 sync_flags;
581 	uint8_t		 updates;
582 
583 	uint8_t		 spare[112];
584 };
585 _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect");
586 
587 #ifdef _KERNEL
588 struct pf_kstate {
589 	u_int64_t		 id;
590 	u_int32_t		 creatorid;
591 	u_int8_t		 direction;
592 	u_int8_t		 pad[3];
593 
594 	u_int			 refs;
595 	TAILQ_ENTRY(pf_kstate)	 sync_list;
596 	TAILQ_ENTRY(pf_kstate)	 key_list[2];
597 	LIST_ENTRY(pf_kstate)	 entry;
598 	struct pf_state_peer	 src;
599 	struct pf_state_peer	 dst;
600 	union pf_krule_ptr	 rule;
601 	union pf_krule_ptr	 anchor;
602 	union pf_krule_ptr	 nat_rule;
603 	struct pf_addr		 rt_addr;
604 	struct pf_state_key	*key[2];	/* addresses stack and wire  */
605 	struct pfi_kkif		*kif;
606 	struct pfi_kkif		*orig_kif;	/* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */
607 	struct pfi_kkif		*rt_kif;
608 	struct pf_ksrc_node	*src_node;
609 	struct pf_ksrc_node	*nat_src_node;
610 	u_int64_t		 packets[2];
611 	u_int64_t		 bytes[2];
612 	u_int32_t		 creation;
613 	u_int32_t	 	 expire;
614 	u_int32_t		 pfsync_time;
615 	u_int32_t                qid;
616 	u_int32_t                pqid;
617 	u_int16_t		 tag;
618 	u_int8_t		 log;
619 	u_int8_t		 state_flags;
620 	u_int8_t		 timeout;
621 	u_int8_t		 sync_state; /* PFSYNC_S_x */
622 
623 	/* XXX */
624 	u_int8_t		 sync_updates;
625 	u_int8_t		_tail[3];
626 };
627 
628 /*
629  * Size <= fits 13 objects per page on LP64. Try to not grow the struct beyond that.
630  */
631 _Static_assert(sizeof(struct pf_kstate) <= 312, "pf_kstate size crosses 312 bytes");
632 #endif
633 
634 /*
635  * Unified state structures for pulling states out of the kernel
636  * used by pfsync(4) and the pf(4) ioctl.
637  */
638 struct pfsync_state_scrub {
639 	u_int16_t	pfss_flags;
640 	u_int8_t	pfss_ttl;	/* stashed TTL		*/
641 #define PFSYNC_SCRUB_FLAG_VALID		0x01
642 	u_int8_t	scrub_flag;
643 	u_int32_t	pfss_ts_mod;	/* timestamp modulation	*/
644 } __packed;
645 
646 struct pfsync_state_peer {
647 	struct pfsync_state_scrub scrub;	/* state is scrubbed	*/
648 	u_int32_t	seqlo;		/* Max sequence number sent	*/
649 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
650 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
651 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
652 	u_int16_t	mss;		/* Maximum segment size option	*/
653 	u_int8_t	state;		/* active state level		*/
654 	u_int8_t	wscale;		/* window scaling factor	*/
655 	u_int8_t	pad[6];
656 } __packed;
657 
658 struct pfsync_state_key {
659 	struct pf_addr	 addr[2];
660 	u_int16_t	 port[2];
661 };
662 
663 struct pfsync_state {
664 	u_int64_t	 id;
665 	char		 ifname[IFNAMSIZ];
666 	struct pfsync_state_key	key[2];
667 	struct pfsync_state_peer src;
668 	struct pfsync_state_peer dst;
669 	struct pf_addr	 rt_addr;
670 	u_int32_t	 rule;
671 	u_int32_t	 anchor;
672 	u_int32_t	 nat_rule;
673 	u_int32_t	 creation;
674 	u_int32_t	 expire;
675 	u_int32_t	 packets[2][2];
676 	u_int32_t	 bytes[2][2];
677 	u_int32_t	 creatorid;
678 	sa_family_t	 af;
679 	u_int8_t	 proto;
680 	u_int8_t	 direction;
681 	u_int8_t	 __spare[2];
682 	u_int8_t	 log;
683 	u_int8_t	 state_flags;
684 	u_int8_t	 timeout;
685 	u_int8_t	 sync_flags;
686 	u_int8_t	 updates;
687 } __packed;
688 
689 #ifdef _KERNEL
690 /* pfsync */
691 typedef int		pfsync_state_import_t(struct pfsync_state *, u_int8_t);
692 typedef	void		pfsync_insert_state_t(struct pf_kstate *);
693 typedef	void		pfsync_update_state_t(struct pf_kstate *);
694 typedef	void		pfsync_delete_state_t(struct pf_kstate *);
695 typedef void		pfsync_clear_states_t(u_int32_t, const char *);
696 typedef int		pfsync_defer_t(struct pf_kstate *, struct mbuf *);
697 typedef void		pfsync_detach_ifnet_t(struct ifnet *);
698 
699 VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr);
700 #define V_pfsync_state_import_ptr	VNET(pfsync_state_import_ptr)
701 VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
702 #define V_pfsync_insert_state_ptr	VNET(pfsync_insert_state_ptr)
703 VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr);
704 #define V_pfsync_update_state_ptr	VNET(pfsync_update_state_ptr)
705 VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
706 #define V_pfsync_delete_state_ptr	VNET(pfsync_delete_state_ptr)
707 VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
708 #define V_pfsync_clear_states_ptr	VNET(pfsync_clear_states_ptr)
709 VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr);
710 #define V_pfsync_defer_ptr		VNET(pfsync_defer_ptr)
711 extern pfsync_detach_ifnet_t	*pfsync_detach_ifnet_ptr;
712 
713 void			pfsync_state_export(struct pfsync_state *,
714 			    struct pf_kstate *);
715 void			pf_state_export(struct pf_state_export *,
716 			    struct pf_kstate *);
717 
718 /* pflog */
719 struct pf_kruleset;
720 struct pf_pdesc;
721 typedef int pflog_packet_t(struct pfi_kkif *, struct mbuf *, sa_family_t,
722     u_int8_t, u_int8_t, struct pf_krule *, struct pf_krule *,
723     struct pf_kruleset *, struct pf_pdesc *, int);
724 extern pflog_packet_t		*pflog_packet_ptr;
725 
726 #endif /* _KERNEL */
727 
728 #define	PFSYNC_FLAG_SRCNODE	0x04
729 #define	PFSYNC_FLAG_NATSRCNODE	0x08
730 
731 /* for copies to/from network byte order */
732 /* ioctl interface also uses network byte order */
733 #define pf_state_peer_hton(s,d) do {		\
734 	(d)->seqlo = htonl((s)->seqlo);		\
735 	(d)->seqhi = htonl((s)->seqhi);		\
736 	(d)->seqdiff = htonl((s)->seqdiff);	\
737 	(d)->max_win = htons((s)->max_win);	\
738 	(d)->mss = htons((s)->mss);		\
739 	(d)->state = (s)->state;		\
740 	(d)->wscale = (s)->wscale;		\
741 	if ((s)->scrub) {						\
742 		(d)->scrub.pfss_flags = 				\
743 		    htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP);	\
744 		(d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;		\
745 		(d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
746 		(d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;	\
747 	}								\
748 } while (0)
749 
750 #define pf_state_peer_ntoh(s,d) do {		\
751 	(d)->seqlo = ntohl((s)->seqlo);		\
752 	(d)->seqhi = ntohl((s)->seqhi);		\
753 	(d)->seqdiff = ntohl((s)->seqdiff);	\
754 	(d)->max_win = ntohs((s)->max_win);	\
755 	(d)->mss = ntohs((s)->mss);		\
756 	(d)->state = (s)->state;		\
757 	(d)->wscale = (s)->wscale;		\
758 	if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && 	\
759 	    (d)->scrub != NULL) {					\
760 		(d)->scrub->pfss_flags =				\
761 		    ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP;	\
762 		(d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl;		\
763 		(d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
764 	}								\
765 } while (0)
766 
767 #define pf_state_counter_hton(s,d) do {				\
768 	d[0] = htonl((s>>32)&0xffffffff);			\
769 	d[1] = htonl(s&0xffffffff);				\
770 } while (0)
771 
772 #define pf_state_counter_from_pfsync(s)				\
773 	(((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
774 
775 #define pf_state_counter_ntoh(s,d) do {				\
776 	d = ntohl(s[0]);					\
777 	d = d<<32;						\
778 	d += ntohl(s[1]);					\
779 } while (0)
780 
781 TAILQ_HEAD(pf_krulequeue, pf_krule);
782 
783 struct pf_kanchor;
784 
785 struct pf_kruleset {
786 	struct {
787 		struct pf_krulequeue	 queues[2];
788 		struct {
789 			struct pf_krulequeue	*ptr;
790 			struct pf_krule		**ptr_array;
791 			u_int32_t		 rcount;
792 			u_int32_t		 ticket;
793 			int			 open;
794 		}			 active, inactive;
795 	}			 rules[PF_RULESET_MAX];
796 	struct pf_kanchor	*anchor;
797 	u_int32_t		 tticket;
798 	int			 tables;
799 	int			 topen;
800 };
801 
802 RB_HEAD(pf_kanchor_global, pf_kanchor);
803 RB_HEAD(pf_kanchor_node, pf_kanchor);
804 struct pf_kanchor {
805 	RB_ENTRY(pf_kanchor)	 entry_global;
806 	RB_ENTRY(pf_kanchor)	 entry_node;
807 	struct pf_kanchor	*parent;
808 	struct pf_kanchor_node	 children;
809 	char			 name[PF_ANCHOR_NAME_SIZE];
810 	char			 path[MAXPATHLEN];
811 	struct pf_kruleset	 ruleset;
812 	int			 refcnt;	/* anchor rules */
813 	int			 match;	/* XXX: used for pfctl black magic */
814 };
815 RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare);
816 RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare);
817 
818 #define PF_RESERVED_ANCHOR	"_pf"
819 
820 #define PFR_TFLAG_PERSIST	0x00000001
821 #define PFR_TFLAG_CONST		0x00000002
822 #define PFR_TFLAG_ACTIVE	0x00000004
823 #define PFR_TFLAG_INACTIVE	0x00000008
824 #define PFR_TFLAG_REFERENCED	0x00000010
825 #define PFR_TFLAG_REFDANCHOR	0x00000020
826 #define PFR_TFLAG_COUNTERS	0x00000040
827 /* Adjust masks below when adding flags. */
828 #define PFR_TFLAG_USRMASK	(PFR_TFLAG_PERSIST	| \
829 				 PFR_TFLAG_CONST	| \
830 				 PFR_TFLAG_COUNTERS)
831 #define PFR_TFLAG_SETMASK	(PFR_TFLAG_ACTIVE	| \
832 				 PFR_TFLAG_INACTIVE	| \
833 				 PFR_TFLAG_REFERENCED	| \
834 				 PFR_TFLAG_REFDANCHOR)
835 #define PFR_TFLAG_ALLMASK	(PFR_TFLAG_PERSIST	| \
836 				 PFR_TFLAG_CONST	| \
837 				 PFR_TFLAG_ACTIVE	| \
838 				 PFR_TFLAG_INACTIVE	| \
839 				 PFR_TFLAG_REFERENCED	| \
840 				 PFR_TFLAG_REFDANCHOR	| \
841 				 PFR_TFLAG_COUNTERS)
842 
843 struct pf_kanchor_stackframe;
844 
845 struct pfr_table {
846 	char			 pfrt_anchor[MAXPATHLEN];
847 	char			 pfrt_name[PF_TABLE_NAME_SIZE];
848 	u_int32_t		 pfrt_flags;
849 	u_int8_t		 pfrt_fback;
850 };
851 
852 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
853 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
854 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX };
855 
856 struct pfr_addr {
857 	union {
858 		struct in_addr	 _pfra_ip4addr;
859 		struct in6_addr	 _pfra_ip6addr;
860 	}		 pfra_u;
861 	u_int8_t	 pfra_af;
862 	u_int8_t	 pfra_net;
863 	u_int8_t	 pfra_not;
864 	u_int8_t	 pfra_fback;
865 };
866 #define	pfra_ip4addr	pfra_u._pfra_ip4addr
867 #define	pfra_ip6addr	pfra_u._pfra_ip6addr
868 
869 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
870 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
871 enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX };
872 #define	PFR_NUM_COUNTERS	(PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX)
873 #define PFR_OP_XPASS	PFR_OP_ADDR_MAX
874 
875 struct pfr_astats {
876 	struct pfr_addr	 pfras_a;
877 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
878 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
879 	long		 pfras_tzero;
880 };
881 
882 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
883 
884 struct pfr_tstats {
885 	struct pfr_table pfrts_t;
886 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
887 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
888 	u_int64_t	 pfrts_match;
889 	u_int64_t	 pfrts_nomatch;
890 	long		 pfrts_tzero;
891 	int		 pfrts_cnt;
892 	int		 pfrts_refcnt[PFR_REFCNT_MAX];
893 };
894 
895 #ifdef _KERNEL
896 
897 struct pfr_kstate_counter {
898 	counter_u64_t	pkc_pcpu;
899 	u_int64_t	pkc_zero;
900 };
901 
902 static inline int
903 pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags)
904 {
905 
906 	pfrc->pkc_zero = 0;
907 	pfrc->pkc_pcpu = counter_u64_alloc(flags);
908 	if (pfrc->pkc_pcpu == NULL)
909 		return (ENOMEM);
910 	return (0);
911 }
912 
913 static inline void
914 pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc)
915 {
916 
917 	counter_u64_free(pfrc->pkc_pcpu);
918 }
919 
920 static inline u_int64_t
921 pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc)
922 {
923 	u_int64_t c;
924 
925 	c = counter_u64_fetch(pfrc->pkc_pcpu);
926 	c -= pfrc->pkc_zero;
927 	return (c);
928 }
929 
930 static inline void
931 pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc)
932 {
933 	u_int64_t c;
934 
935 	c = counter_u64_fetch(pfrc->pkc_pcpu);
936 	pfrc->pkc_zero = c;
937 }
938 
939 static inline void
940 pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n)
941 {
942 
943 	counter_u64_add(pfrc->pkc_pcpu, n);
944 }
945 
946 struct pfr_ktstats {
947 	struct pfr_table pfrts_t;
948 	struct pfr_kstate_counter	 pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
949 	struct pfr_kstate_counter	 pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
950 	struct pfr_kstate_counter	 pfrkts_match;
951 	struct pfr_kstate_counter	 pfrkts_nomatch;
952 	long		 pfrkts_tzero;
953 	int		 pfrkts_cnt;
954 	int		 pfrkts_refcnt[PFR_REFCNT_MAX];
955 };
956 
957 #endif /* _KERNEL */
958 
959 #define	pfrts_name	pfrts_t.pfrt_name
960 #define pfrts_flags	pfrts_t.pfrt_flags
961 
962 #ifndef _SOCKADDR_UNION_DEFINED
963 #define	_SOCKADDR_UNION_DEFINED
964 union sockaddr_union {
965 	struct sockaddr		sa;
966 	struct sockaddr_in	sin;
967 	struct sockaddr_in6	sin6;
968 };
969 #endif /* _SOCKADDR_UNION_DEFINED */
970 
971 struct pfr_kcounters {
972 	counter_u64_t		 pfrkc_counters;
973 	long			 pfrkc_tzero;
974 };
975 #define	pfr_kentry_counter(kc, dir, op, t)		\
976 	((kc)->pfrkc_counters +				\
977 	    (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t))
978 
979 #ifdef _KERNEL
980 SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
981 struct pfr_kentry {
982 	struct radix_node	 pfrke_node[2];
983 	union sockaddr_union	 pfrke_sa;
984 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq;
985 	struct pfr_kcounters	 pfrke_counters;
986 	u_int8_t		 pfrke_af;
987 	u_int8_t		 pfrke_net;
988 	u_int8_t		 pfrke_not;
989 	u_int8_t		 pfrke_mark;
990 };
991 
992 SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
993 RB_HEAD(pfr_ktablehead, pfr_ktable);
994 struct pfr_ktable {
995 	struct pfr_ktstats	 pfrkt_kts;
996 	RB_ENTRY(pfr_ktable)	 pfrkt_tree;
997 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq;
998 	struct radix_node_head	*pfrkt_ip4;
999 	struct radix_node_head	*pfrkt_ip6;
1000 	struct pfr_ktable	*pfrkt_shadow;
1001 	struct pfr_ktable	*pfrkt_root;
1002 	struct pf_kruleset	*pfrkt_rs;
1003 	long			 pfrkt_larg;
1004 	int			 pfrkt_nflags;
1005 };
1006 #define pfrkt_t		pfrkt_kts.pfrts_t
1007 #define pfrkt_name	pfrkt_t.pfrt_name
1008 #define pfrkt_anchor	pfrkt_t.pfrt_anchor
1009 #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset
1010 #define pfrkt_flags	pfrkt_t.pfrt_flags
1011 #define pfrkt_cnt	pfrkt_kts.pfrkts_cnt
1012 #define pfrkt_refcnt	pfrkt_kts.pfrkts_refcnt
1013 #define pfrkt_packets	pfrkt_kts.pfrkts_packets
1014 #define pfrkt_bytes	pfrkt_kts.pfrkts_bytes
1015 #define pfrkt_match	pfrkt_kts.pfrkts_match
1016 #define pfrkt_nomatch	pfrkt_kts.pfrkts_nomatch
1017 #define pfrkt_tzero	pfrkt_kts.pfrkts_tzero
1018 #endif
1019 
1020 #ifdef _KERNEL
1021 struct pfi_kkif {
1022 	char				 pfik_name[IFNAMSIZ];
1023 	union {
1024 		RB_ENTRY(pfi_kkif)	 _pfik_tree;
1025 		LIST_ENTRY(pfi_kkif)	 _pfik_list;
1026 	} _pfik_glue;
1027 #define	pfik_tree	_pfik_glue._pfik_tree
1028 #define	pfik_list	_pfik_glue._pfik_list
1029 	counter_u64_t			 pfik_packets[2][2][2];
1030 	counter_u64_t			 pfik_bytes[2][2][2];
1031 	u_int32_t			 pfik_tzero;
1032 	u_int				 pfik_flags;
1033 	struct ifnet			*pfik_ifp;
1034 	struct ifg_group		*pfik_group;
1035 	u_int				 pfik_rulerefs;
1036 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1037 };
1038 #endif
1039 
1040 #define	PFI_IFLAG_REFS		0x0001	/* has state references */
1041 #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */
1042 
1043 #ifdef _KERNEL
1044 struct pf_pdesc {
1045 	struct {
1046 		int	 done;
1047 		uid_t	 uid;
1048 		gid_t	 gid;
1049 	}		 lookup;
1050 	u_int64_t	 tot_len;	/* Make Mickey money */
1051 	union pf_headers {
1052 		struct tcphdr		tcp;
1053 		struct udphdr		udp;
1054 		struct icmp		icmp;
1055 #ifdef INET6
1056 		struct icmp6_hdr	icmp6;
1057 #endif /* INET6 */
1058 		char any[0];
1059 	} hdr;
1060 
1061 	struct pf_krule	*nat_rule;	/* nat/rdr rule applied to packet */
1062 	struct pf_addr	*src;		/* src address */
1063 	struct pf_addr	*dst;		/* dst address */
1064 	u_int16_t *sport;
1065 	u_int16_t *dport;
1066 	struct pf_mtag	*pf_mtag;
1067 	struct pf_rule_actions	act;
1068 
1069 	u_int32_t	 p_len;		/* total length of payload */
1070 
1071 	u_int16_t	*ip_sum;
1072 	u_int16_t	*proto_sum;
1073 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
1074 					 * state code. Easier than tags */
1075 #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
1076 #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
1077 	sa_family_t	 af;
1078 	u_int8_t	 proto;
1079 	u_int8_t	 tos;
1080 	u_int8_t	 dir;		/* direction */
1081 	u_int8_t	 sidx;		/* key index for source */
1082 	u_int8_t	 didx;		/* key index for destination */
1083 };
1084 #endif
1085 
1086 /* flags for RDR options */
1087 #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
1088 #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
1089 
1090 /* UDP state enumeration */
1091 #define PFUDPS_NO_TRAFFIC	0
1092 #define PFUDPS_SINGLE		1
1093 #define PFUDPS_MULTIPLE		2
1094 
1095 #define PFUDPS_NSTATES		3	/* number of state levels */
1096 
1097 #define PFUDPS_NAMES { \
1098 	"NO_TRAFFIC", \
1099 	"SINGLE", \
1100 	"MULTIPLE", \
1101 	NULL \
1102 }
1103 
1104 /* Other protocol state enumeration */
1105 #define PFOTHERS_NO_TRAFFIC	0
1106 #define PFOTHERS_SINGLE		1
1107 #define PFOTHERS_MULTIPLE	2
1108 
1109 #define PFOTHERS_NSTATES	3	/* number of state levels */
1110 
1111 #define PFOTHERS_NAMES { \
1112 	"NO_TRAFFIC", \
1113 	"SINGLE", \
1114 	"MULTIPLE", \
1115 	NULL \
1116 }
1117 
1118 #define ACTION_SET(a, x) \
1119 	do { \
1120 		if ((a) != NULL) \
1121 			*(a) = (x); \
1122 	} while (0)
1123 
1124 #define REASON_SET(a, x) \
1125 	do { \
1126 		if ((a) != NULL) \
1127 			*(a) = (x); \
1128 		if (x < PFRES_MAX) \
1129 			counter_u64_add(V_pf_status.counters[x], 1); \
1130 	} while (0)
1131 
1132 struct pf_kstatus {
1133 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */
1134 	counter_u64_t	lcounters[LCNT_MAX]; /* limit counters */
1135 	counter_u64_t	fcounters[FCNT_MAX]; /* state operation counters */
1136 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */
1137 	uint32_t	states;
1138 	uint32_t	src_nodes;
1139 	uint32_t	running;
1140 	uint32_t	since;
1141 	uint32_t	debug;
1142 	uint32_t	hostid;
1143 	char		ifname[IFNAMSIZ];
1144 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
1145 	bool		keep_counters;
1146 };
1147 
1148 struct pf_divert {
1149 	union {
1150 		struct in_addr	ipv4;
1151 		struct in6_addr	ipv6;
1152 	}		addr;
1153 	u_int16_t	port;
1154 };
1155 
1156 #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
1157 #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
1158 
1159 /*
1160  * Limit the length of the fragment queue traversal.  Remember
1161  * search entry points based on the fragment offset.
1162  */
1163 #define PF_FRAG_ENTRY_POINTS		16
1164 
1165 /*
1166  * The number of entries in the fragment queue must be limited
1167  * to avoid DoS by linear seaching.  Instead of a global limit,
1168  * use a limit per entry point.  For large packets these sum up.
1169  */
1170 #define PF_FRAG_ENTRY_LIMIT		64
1171 
1172 /*
1173  * ioctl parameter structures
1174  */
1175 
1176 struct pfioc_pooladdr {
1177 	u_int32_t		 action;
1178 	u_int32_t		 ticket;
1179 	u_int32_t		 nr;
1180 	u_int32_t		 r_num;
1181 	u_int8_t		 r_action;
1182 	u_int8_t		 r_last;
1183 	u_int8_t		 af;
1184 	char			 anchor[MAXPATHLEN];
1185 	struct pf_pooladdr	 addr;
1186 };
1187 
1188 struct pfioc_rule {
1189 	u_int32_t	 action;
1190 	u_int32_t	 ticket;
1191 	u_int32_t	 pool_ticket;
1192 	u_int32_t	 nr;
1193 	char		 anchor[MAXPATHLEN];
1194 	char		 anchor_call[MAXPATHLEN];
1195 	struct pf_rule	 rule;
1196 };
1197 
1198 struct pfioc_natlook {
1199 	struct pf_addr	 saddr;
1200 	struct pf_addr	 daddr;
1201 	struct pf_addr	 rsaddr;
1202 	struct pf_addr	 rdaddr;
1203 	u_int16_t	 sport;
1204 	u_int16_t	 dport;
1205 	u_int16_t	 rsport;
1206 	u_int16_t	 rdport;
1207 	sa_family_t	 af;
1208 	u_int8_t	 proto;
1209 	u_int8_t	 direction;
1210 };
1211 
1212 struct pfioc_state {
1213 	struct pfsync_state	state;
1214 };
1215 
1216 struct pfioc_src_node_kill {
1217 	sa_family_t psnk_af;
1218 	struct pf_rule_addr psnk_src;
1219 	struct pf_rule_addr psnk_dst;
1220 	u_int		    psnk_killed;
1221 };
1222 
1223 #ifdef _KERNEL
1224 struct pf_kstate_kill {
1225 	struct pf_state_cmp	psk_pfcmp;
1226 	sa_family_t		psk_af;
1227 	int			psk_proto;
1228 	struct pf_rule_addr	psk_src;
1229 	struct pf_rule_addr	psk_dst;
1230 	struct pf_rule_addr	psk_rt_addr;
1231 	char			psk_ifname[IFNAMSIZ];
1232 	char			psk_label[PF_RULE_LABEL_SIZE];
1233 	u_int			psk_killed;
1234 	bool			psk_kill_match;
1235 };
1236 #endif
1237 
1238 struct pfioc_state_kill {
1239 	struct pf_state_cmp	psk_pfcmp;
1240 	sa_family_t		psk_af;
1241 	int			psk_proto;
1242 	struct pf_rule_addr	psk_src;
1243 	struct pf_rule_addr	psk_dst;
1244 	char			psk_ifname[IFNAMSIZ];
1245 	char			psk_label[PF_RULE_LABEL_SIZE];
1246 	u_int			psk_killed;
1247 };
1248 
1249 struct pfioc_states {
1250 	int	ps_len;
1251 	union {
1252 		caddr_t			 psu_buf;
1253 		struct pfsync_state	*psu_states;
1254 	} ps_u;
1255 #define ps_buf		ps_u.psu_buf
1256 #define ps_states	ps_u.psu_states
1257 };
1258 
1259 struct pfioc_states_v2 {
1260 	int		ps_len;
1261 	uint64_t	ps_req_version;
1262 	union {
1263 		caddr_t			 psu_buf;
1264 		struct pf_state_export	*psu_states;
1265 	} ps_u;
1266 #define ps_buf		ps_u.psu_buf
1267 #define ps_states	ps_u.psu_states
1268 };
1269 
1270 struct pfioc_src_nodes {
1271 	int	psn_len;
1272 	union {
1273 		caddr_t		 psu_buf;
1274 		struct pf_src_node	*psu_src_nodes;
1275 	} psn_u;
1276 #define psn_buf		psn_u.psu_buf
1277 #define psn_src_nodes	psn_u.psu_src_nodes
1278 };
1279 
1280 struct pfioc_if {
1281 	char		 ifname[IFNAMSIZ];
1282 };
1283 
1284 struct pfioc_tm {
1285 	int		 timeout;
1286 	int		 seconds;
1287 };
1288 
1289 struct pfioc_limit {
1290 	int		 index;
1291 	unsigned	 limit;
1292 };
1293 
1294 struct pfioc_altq_v0 {
1295 	u_int32_t	 action;
1296 	u_int32_t	 ticket;
1297 	u_int32_t	 nr;
1298 	struct pf_altq_v0 altq;
1299 };
1300 
1301 struct pfioc_altq_v1 {
1302 	u_int32_t	 action;
1303 	u_int32_t	 ticket;
1304 	u_int32_t	 nr;
1305 	/*
1306 	 * Placed here so code that only uses the above parameters can be
1307 	 * written entirely in terms of the v0 or v1 type.
1308 	 */
1309 	u_int32_t	 version;
1310 	struct pf_altq_v1 altq;
1311 };
1312 
1313 /*
1314  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1315  * the latest version of struct pf_altq_vX as it has that struct as a
1316  * member.
1317  */
1318 #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION
1319 
1320 struct pfioc_qstats_v0 {
1321 	u_int32_t	 ticket;
1322 	u_int32_t	 nr;
1323 	void		*buf;
1324 	int		 nbytes;
1325 	u_int8_t	 scheduler;
1326 };
1327 
1328 struct pfioc_qstats_v1 {
1329 	u_int32_t	 ticket;
1330 	u_int32_t	 nr;
1331 	void		*buf;
1332 	int		 nbytes;
1333 	u_int8_t	 scheduler;
1334 	/*
1335 	 * Placed here so code that only uses the above parameters can be
1336 	 * written entirely in terms of the v0 or v1 type.
1337 	 */
1338 	u_int32_t	 version;  /* Requested version of stats struct */
1339 };
1340 
1341 /* Latest version of struct pfioc_qstats_vX */
1342 #define PFIOC_QSTATS_VERSION	1
1343 
1344 struct pfioc_ruleset {
1345 	u_int32_t	 nr;
1346 	char		 path[MAXPATHLEN];
1347 	char		 name[PF_ANCHOR_NAME_SIZE];
1348 };
1349 
1350 #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
1351 #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1352 struct pfioc_trans {
1353 	int		 size;	/* number of elements */
1354 	int		 esize; /* size of each element in bytes */
1355 	struct pfioc_trans_e {
1356 		int		rs_num;
1357 		char		anchor[MAXPATHLEN];
1358 		u_int32_t	ticket;
1359 	}		*array;
1360 };
1361 
1362 #define PFR_FLAG_ATOMIC		0x00000001	/* unused */
1363 #define PFR_FLAG_DUMMY		0x00000002
1364 #define PFR_FLAG_FEEDBACK	0x00000004
1365 #define PFR_FLAG_CLSTATS	0x00000008
1366 #define PFR_FLAG_ADDRSTOO	0x00000010
1367 #define PFR_FLAG_REPLACE	0x00000020
1368 #define PFR_FLAG_ALLRSETS	0x00000040
1369 #define PFR_FLAG_ALLMASK	0x0000007F
1370 #ifdef _KERNEL
1371 #define PFR_FLAG_USERIOCTL	0x10000000
1372 #endif
1373 
1374 struct pfioc_table {
1375 	struct pfr_table	 pfrio_table;
1376 	void			*pfrio_buffer;
1377 	int			 pfrio_esize;
1378 	int			 pfrio_size;
1379 	int			 pfrio_size2;
1380 	int			 pfrio_nadd;
1381 	int			 pfrio_ndel;
1382 	int			 pfrio_nchange;
1383 	int			 pfrio_flags;
1384 	u_int32_t		 pfrio_ticket;
1385 };
1386 #define	pfrio_exists	pfrio_nadd
1387 #define	pfrio_nzero	pfrio_nadd
1388 #define	pfrio_nmatch	pfrio_nadd
1389 #define pfrio_naddr	pfrio_size2
1390 #define pfrio_setflag	pfrio_size2
1391 #define pfrio_clrflag	pfrio_nadd
1392 
1393 struct pfioc_iface {
1394 	char	 pfiio_name[IFNAMSIZ];
1395 	void	*pfiio_buffer;
1396 	int	 pfiio_esize;
1397 	int	 pfiio_size;
1398 	int	 pfiio_nzero;
1399 	int	 pfiio_flags;
1400 };
1401 
1402 /*
1403  * ioctl operations
1404  */
1405 
1406 #define DIOCSTART	_IO  ('D',  1)
1407 #define DIOCSTOP	_IO  ('D',  2)
1408 #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
1409 #define DIOCADDRULENV	_IOWR('D',  4, struct pfioc_nv)
1410 #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
1411 #define DIOCGETRULE	_IOWR('D',  7, struct pfioc_rule)
1412 #define DIOCGETRULENV	_IOWR('D',  7, struct pfioc_nv)
1413 /* XXX cut 8 - 17 */
1414 #define DIOCCLRSTATES	_IOWR('D', 18, struct pfioc_state_kill)
1415 #define DIOCCLRSTATESNV	_IOWR('D', 18, struct pfioc_nv)
1416 #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
1417 #define DIOCGETSTATENV	_IOWR('D', 19, struct pfioc_nv)
1418 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1419 #define DIOCGETSTATUS	_IOWR('D', 21, struct pf_status)
1420 #define DIOCCLRSTATUS	_IO  ('D', 22)
1421 #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
1422 #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
1423 #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1424 #define DIOCGETSTATESNV	_IOWR('D', 25, struct pfioc_nv)
1425 #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
1426 /* XXX cut 26 - 28 */
1427 #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
1428 #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1429 #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1430 #define DIOCCLRRULECTRS	_IO  ('D', 38)
1431 #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
1432 #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
1433 #define DIOCKILLSTATES	_IOWR('D', 41, struct pfioc_state_kill)
1434 #define DIOCKILLSTATESNV	_IOWR('D', 41, struct pfioc_nv)
1435 #define DIOCSTARTALTQ	_IO  ('D', 42)
1436 #define DIOCSTOPALTQ	_IO  ('D', 43)
1437 #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0)
1438 #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1)
1439 #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0)
1440 #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1)
1441 #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0)
1442 #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1)
1443 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1444 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1445 #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0)
1446 #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1)
1447 #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
1448 #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
1449 #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
1450 #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1451 #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
1452 /* XXX cut 55 - 57 */
1453 #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
1454 #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
1455 #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
1456 #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
1457 #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
1458 #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
1459 #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
1460 #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table)
1461 #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
1462 #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
1463 #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
1464 #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
1465 #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
1466 #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
1467 #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table)
1468 #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
1469 #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
1470 #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
1471 #define	DIOCOSFPFLUSH	_IO('D', 78)
1472 #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
1473 #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
1474 #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans)
1475 #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans)
1476 #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans)
1477 #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
1478 #define	DIOCCLRSRCNODES	_IO('D', 85)
1479 #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
1480 #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
1481 #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
1482 #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
1483 #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
1484 #define	DIOCKEEPCOUNTERS	_IOWR('D', 92, struct pfioc_nv)
1485 #define DIOCGETSTATESV2	_IOWR('D', 93, struct pfioc_states_v2)
1486 
1487 struct pf_ifspeed_v0 {
1488 	char			ifname[IFNAMSIZ];
1489 	u_int32_t		baudrate;
1490 };
1491 
1492 struct pf_ifspeed_v1 {
1493 	char			ifname[IFNAMSIZ];
1494 	u_int32_t		baudrate32;
1495 	/* layout identical to struct pf_ifspeed_v0 up to this point */
1496 	u_int64_t		baudrate;
1497 };
1498 
1499 /* Latest version of struct pf_ifspeed_vX */
1500 #define PF_IFSPEED_VERSION	1
1501 
1502 #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0)
1503 #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1)
1504 
1505 /*
1506  * Compatibility and convenience macros
1507  */
1508 #ifndef _KERNEL
1509 #ifdef PFIOC_USE_LATEST
1510 /*
1511  * Maintaining in-tree consumers of the ioctl interface is easier when that
1512  * code can be written in terms old names that refer to the latest interface
1513  * version as that reduces the required changes in the consumers to those
1514  * that are functionally necessary to accommodate a new interface version.
1515  */
1516 #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
1517 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
1518 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
1519 
1520 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
1521 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
1522 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
1523 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
1524 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
1525 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
1526 #else
1527 /*
1528  * When building out-of-tree code that is written for the old interface,
1529  * such as may exist in ports for example, resolve the old struct tags and
1530  * ioctl command names to the v0 versions.
1531  */
1532 #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0)
1533 #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0)
1534 #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0)
1535 
1536 #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0)
1537 #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0)
1538 #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0)
1539 #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0)
1540 #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0)
1541 #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0)
1542 #endif /* PFIOC_USE_LATEST */
1543 #endif /* _KERNEL */
1544 
1545 #ifdef _KERNEL
1546 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node);
1547 struct pf_srchash {
1548 	struct pf_ksrc_node_list		nodes;
1549 	struct mtx			lock;
1550 };
1551 
1552 struct pf_keyhash {
1553 	LIST_HEAD(, pf_state_key)	keys;
1554 	struct mtx			lock;
1555 };
1556 
1557 struct pf_idhash {
1558 	LIST_HEAD(, pf_kstate)		states;
1559 	struct mtx			lock;
1560 };
1561 
1562 extern u_long		pf_hashmask;
1563 extern u_long		pf_srchashmask;
1564 #define	PF_HASHSIZ	(131072)
1565 #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4)
1566 VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
1567 VNET_DECLARE(struct pf_idhash *, pf_idhash);
1568 #define V_pf_keyhash	VNET(pf_keyhash)
1569 #define	V_pf_idhash	VNET(pf_idhash)
1570 VNET_DECLARE(struct pf_srchash *, pf_srchash);
1571 #define	V_pf_srchash	VNET(pf_srchash)
1572 
1573 #define PF_IDHASH(s)	(be64toh((s)->id) % (pf_hashmask + 1))
1574 
1575 VNET_DECLARE(void *, pf_swi_cookie);
1576 #define V_pf_swi_cookie	VNET(pf_swi_cookie)
1577 VNET_DECLARE(struct intr_event *, pf_swi_ie);
1578 #define	V_pf_swi_ie	VNET(pf_swi_ie)
1579 
1580 VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]);
1581 #define	V_pf_stateid	VNET(pf_stateid)
1582 
1583 TAILQ_HEAD(pf_altqqueue, pf_altq);
1584 VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]);
1585 #define	V_pf_altqs			 VNET(pf_altqs)
1586 VNET_DECLARE(struct pf_kpalist,		 pf_pabuf);
1587 #define	V_pf_pabuf			 VNET(pf_pabuf)
1588 
1589 VNET_DECLARE(u_int32_t,			 ticket_altqs_active);
1590 #define	V_ticket_altqs_active		 VNET(ticket_altqs_active)
1591 VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive);
1592 #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive)
1593 VNET_DECLARE(int,			 altqs_inactive_open);
1594 #define	V_altqs_inactive_open		 VNET(altqs_inactive_open)
1595 VNET_DECLARE(u_int32_t,			 ticket_pabuf);
1596 #define	V_ticket_pabuf			 VNET(ticket_pabuf)
1597 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active);
1598 #define	V_pf_altqs_active		 VNET(pf_altqs_active)
1599 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active);
1600 #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active)
1601 VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive);
1602 #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive)
1603 VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
1604 #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive)
1605 
1606 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules);
1607 #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules)
1608 
1609 void				 pf_initialize(void);
1610 void				 pf_mtag_initialize(void);
1611 void				 pf_mtag_cleanup(void);
1612 void				 pf_cleanup(void);
1613 
1614 struct pf_mtag			*pf_get_mtag(struct mbuf *);
1615 
1616 extern void			 pf_calc_skip_steps(struct pf_krulequeue *);
1617 #ifdef ALTQ
1618 extern	void			 pf_altq_ifnet_event(struct ifnet *, int);
1619 #endif
1620 VNET_DECLARE(uma_zone_t,	 pf_state_z);
1621 #define	V_pf_state_z		 VNET(pf_state_z)
1622 VNET_DECLARE(uma_zone_t,	 pf_state_key_z);
1623 #define	V_pf_state_key_z	 VNET(pf_state_key_z)
1624 VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z);
1625 #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z)
1626 
1627 extern void			 pf_purge_thread(void *);
1628 extern void			 pf_unload_vnet_purge(void);
1629 extern void			 pf_intr(void *);
1630 extern void			 pf_purge_expired_src_nodes(void);
1631 
1632 extern int			 pf_unlink_state(struct pf_kstate *, u_int);
1633 #define	PF_ENTER_LOCKED		0x00000001
1634 #define	PF_RETURN_LOCKED	0x00000002
1635 extern int			 pf_state_insert(struct pfi_kkif *,
1636 				    struct pfi_kkif *,
1637 				    struct pf_state_key *,
1638 				    struct pf_state_key *,
1639 				    struct pf_kstate *);
1640 extern struct pf_kstate		*pf_alloc_state(int);
1641 extern void			 pf_free_state(struct pf_kstate *);
1642 
1643 static __inline void
1644 pf_ref_state(struct pf_kstate *s)
1645 {
1646 
1647 	refcount_acquire(&s->refs);
1648 }
1649 
1650 static __inline int
1651 pf_release_state(struct pf_kstate *s)
1652 {
1653 
1654 	if (refcount_release(&s->refs)) {
1655 		pf_free_state(s);
1656 		return (1);
1657 	} else
1658 		return (0);
1659 }
1660 
1661 static __inline int
1662 pf_release_staten(struct pf_kstate *s, u_int n)
1663 {
1664 
1665 	if (refcount_releasen(&s->refs, n)) {
1666 		pf_free_state(s);
1667 		return (1);
1668 	} else
1669 		return (0);
1670 }
1671 
1672 extern struct pf_kstate		*pf_find_state_byid(uint64_t, uint32_t);
1673 extern struct pf_kstate		*pf_find_state_all(struct pf_state_key_cmp *,
1674 				    u_int, int *);
1675 extern bool			pf_find_state_all_exists(struct pf_state_key_cmp *,
1676 				    u_int);
1677 extern struct pf_ksrc_node	*pf_find_src_node(struct pf_addr *,
1678 				    struct pf_krule *, sa_family_t, int);
1679 extern void			 pf_unlink_src_node(struct pf_ksrc_node *);
1680 extern u_int			 pf_free_src_nodes(struct pf_ksrc_node_list *);
1681 extern void			 pf_print_state(struct pf_kstate *);
1682 extern void			 pf_print_flags(u_int8_t);
1683 extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
1684 				    u_int8_t);
1685 extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
1686 				    u_int16_t, u_int16_t, u_int8_t);
1687 
1688 VNET_DECLARE(struct ifnet *,		 sync_ifp);
1689 #define	V_sync_ifp		 	 VNET(sync_ifp);
1690 VNET_DECLARE(struct pf_krule,		 pf_default_rule);
1691 #define	V_pf_default_rule		  VNET(pf_default_rule)
1692 extern void			 pf_addrcpy(struct pf_addr *, struct pf_addr *,
1693 				    u_int8_t);
1694 void				pf_free_rule(struct pf_krule *);
1695 
1696 #ifdef INET
1697 int	pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1698 int	pf_normalize_ip(struct mbuf **, int, struct pfi_kkif *, u_short *,
1699 	    struct pf_pdesc *);
1700 #endif /* INET */
1701 
1702 #ifdef INET6
1703 int	pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
1704 int	pf_normalize_ip6(struct mbuf **, int, struct pfi_kkif *, u_short *,
1705 	    struct pf_pdesc *);
1706 void	pf_poolmask(struct pf_addr *, struct pf_addr*,
1707 	    struct pf_addr *, struct pf_addr *, u_int8_t);
1708 void	pf_addr_inc(struct pf_addr *, sa_family_t);
1709 int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *);
1710 #endif /* INET6 */
1711 
1712 u_int32_t	pf_new_isn(struct pf_kstate *);
1713 void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
1714 	    sa_family_t);
1715 void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
1716 void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
1717 	    u_int8_t);
1718 void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
1719 void	pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t,
1720 	    bool, u_int8_t);
1721 void	pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t,
1722     bool, u_int8_t);
1723 void	pf_send_deferred_syn(struct pf_kstate *);
1724 int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
1725 	    struct pf_addr *, sa_family_t);
1726 int	pf_match_addr_range(struct pf_addr *, struct pf_addr *,
1727 	    struct pf_addr *, sa_family_t);
1728 int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
1729 
1730 void	pf_normalize_init(void);
1731 void	pf_normalize_cleanup(void);
1732 int	pf_normalize_tcp(int, struct pfi_kkif *, struct mbuf *, int, int, void *,
1733 	    struct pf_pdesc *);
1734 void	pf_normalize_tcp_cleanup(struct pf_kstate *);
1735 int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
1736 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
1737 int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
1738 	    u_short *, struct tcphdr *, struct pf_kstate *,
1739 	    struct pf_state_peer *, struct pf_state_peer *, int *);
1740 u_int32_t
1741 	pf_state_expires(const struct pf_kstate *);
1742 void	pf_purge_expired_fragments(void);
1743 void	pf_purge_fragments(uint32_t);
1744 int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *,
1745 	    int);
1746 int	pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *);
1747 struct pf_state_key *pf_alloc_state_key(int);
1748 void	pfr_initialize(void);
1749 void	pfr_cleanup(void);
1750 int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
1751 void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
1752 	    u_int64_t, int, int, int);
1753 int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
1754 void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
1755 struct pfr_ktable *
1756 	pfr_attach_table(struct pf_kruleset *, char *);
1757 void	pfr_detach_table(struct pfr_ktable *);
1758 int	pfr_clr_tables(struct pfr_table *, int *, int);
1759 int	pfr_add_tables(struct pfr_table *, int, int *, int);
1760 int	pfr_del_tables(struct pfr_table *, int, int *, int);
1761 int	pfr_table_count(struct pfr_table *, int);
1762 int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
1763 int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
1764 int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
1765 int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
1766 int	pfr_clr_addrs(struct pfr_table *, int *, int);
1767 int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
1768 int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1769 	    int);
1770 int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1771 	    int);
1772 int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1773 	    int *, int *, int *, int, u_int32_t);
1774 int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
1775 int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
1776 int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
1777 	    int);
1778 int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1779 	    int);
1780 int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
1781 int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
1782 int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
1783 int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
1784 	    int *, u_int32_t, int);
1785 
1786 MALLOC_DECLARE(PFI_MTYPE);
1787 VNET_DECLARE(struct pfi_kkif *,		 pfi_all);
1788 #define	V_pfi_all	 		 VNET(pfi_all)
1789 
1790 void		 pfi_initialize(void);
1791 void		 pfi_initialize_vnet(void);
1792 void		 pfi_cleanup(void);
1793 void		 pfi_cleanup_vnet(void);
1794 void		 pfi_kkif_ref(struct pfi_kkif *);
1795 void		 pfi_kkif_unref(struct pfi_kkif *);
1796 struct pfi_kkif	*pfi_kkif_find(const char *);
1797 struct pfi_kkif	*pfi_kkif_attach(struct pfi_kkif *, const char *);
1798 int		 pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *);
1799 void		 pfi_kkif_purge(void);
1800 int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
1801 		    sa_family_t);
1802 int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
1803 void		 pfi_dynaddr_remove(struct pfi_dynaddr *);
1804 void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
1805 void		 pfi_update_status(const char *, struct pf_status *);
1806 void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
1807 int		 pfi_set_flags(const char *, int);
1808 int		 pfi_clear_flags(const char *, int);
1809 
1810 int		 pf_match_tag(struct mbuf *, struct pf_krule *, int *, int);
1811 int		 pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
1812 int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *,
1813 		    sa_family_t);
1814 void		 pf_qid2qname(u_int32_t, char *);
1815 
1816 VNET_DECLARE(struct pf_kstatus, pf_status);
1817 #define	V_pf_status	VNET(pf_status)
1818 
1819 struct pf_limit {
1820 	uma_zone_t	zone;
1821 	u_int		limit;
1822 };
1823 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
1824 #define	V_pf_limits VNET(pf_limits)
1825 
1826 #endif /* _KERNEL */
1827 
1828 #ifdef _KERNEL
1829 VNET_DECLARE(struct pf_kanchor_global,		 pf_anchors);
1830 #define	V_pf_anchors				 VNET(pf_anchors)
1831 VNET_DECLARE(struct pf_kanchor,			 pf_main_anchor);
1832 #define	V_pf_main_anchor			 VNET(pf_main_anchor)
1833 #define pf_main_ruleset	V_pf_main_anchor.ruleset
1834 
1835 void			 pf_init_kruleset(struct pf_kruleset *);
1836 int			 pf_kanchor_setup(struct pf_krule *,
1837 			    const struct pf_kruleset *, const char *);
1838 int			 pf_kanchor_nvcopyout(const struct pf_kruleset *,
1839 			    const struct pf_krule *, nvlist_t *);
1840 int			 pf_kanchor_copyout(const struct pf_kruleset *,
1841 			    const struct pf_krule *, struct pfioc_rule *);
1842 void			 pf_kanchor_remove(struct pf_krule *);
1843 void			 pf_remove_if_empty_kruleset(struct pf_kruleset *);
1844 struct pf_kruleset	*pf_find_kruleset(const char *);
1845 struct pf_kruleset	*pf_find_or_create_kruleset(const char *);
1846 void			 pf_rs_initialize(void);
1847 
1848 void			 pf_krule_free(struct pf_krule *);
1849 #endif
1850 
1851 /* The fingerprint functions can be linked into userland programs (tcpdump) */
1852 int	pf_osfp_add(struct pf_osfp_ioctl *);
1853 #ifdef _KERNEL
1854 struct pf_osfp_enlist *
1855 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
1856 	    const struct tcphdr *);
1857 #endif /* _KERNEL */
1858 void	pf_osfp_flush(void);
1859 int	pf_osfp_get(struct pf_osfp_ioctl *);
1860 int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
1861 
1862 #ifdef _KERNEL
1863 void			 pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
1864 
1865 void			 pf_step_into_anchor(struct pf_kanchor_stackframe *, int *,
1866 			    struct pf_kruleset **, int, struct pf_krule **,
1867 			    struct pf_krule **, int *);
1868 int			 pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *,
1869 			    struct pf_kruleset **, int, struct pf_krule **,
1870 			    struct pf_krule **, int *);
1871 
1872 int			 pf_map_addr(u_int8_t, struct pf_krule *,
1873 			    struct pf_addr *, struct pf_addr *,
1874 			    struct pf_addr *, struct pf_ksrc_node **);
1875 struct pf_krule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
1876 			    int, int, struct pfi_kkif *, struct pf_ksrc_node **,
1877 			    struct pf_state_key **, struct pf_state_key **,
1878 			    struct pf_addr *, struct pf_addr *,
1879 			    uint16_t, uint16_t, struct pf_kanchor_stackframe *);
1880 
1881 struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
1882 			    struct pf_addr *, u_int16_t, u_int16_t);
1883 struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
1884 
1885 struct pfi_kkif		*pf_kkif_create(int);
1886 void			 pf_kkif_free(struct pfi_kkif *);
1887 void			 pf_kkif_zero(struct pfi_kkif *);
1888 #endif /* _KERNEL */
1889 
1890 #endif /* _NET_PFVAR_H_ */
1891