xref: /freebsd/sys/netpfil/pf/pf_ioctl.c (revision 6dced2c6358e467ac1dccd99f6f648d4f71957a8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2002,2003 Henning Brauer
6  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  *    - Redistributions of source code must retain the above copyright
14  *      notice, this list of conditions and the following disclaimer.
15  *    - Redistributions in binary form must reproduce the above
16  *      copyright notice, this list of conditions and the following
17  *      disclaimer in the documentation and/or other materials provided
18  *      with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Effort sponsored in part by the Defense Advanced Research Projects
34  * Agency (DARPA) and Air Force Research Laboratory, Air Force
35  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
36  *
37  *	$OpenBSD: pf_ioctl.c,v 1.213 2009/02/15 21:46:12 mbalmer Exp $
38  */
39 
40 #include <sys/cdefs.h>
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43 #include "opt_bpf.h"
44 #include "opt_pf.h"
45 
46 #include <sys/param.h>
47 #include <sys/_bitset.h>
48 #include <sys/bitset.h>
49 #include <sys/bus.h>
50 #include <sys/conf.h>
51 #include <sys/endian.h>
52 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/hash.h>
55 #include <sys/interrupt.h>
56 #include <sys/jail.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/lock.h>
60 #include <sys/mbuf.h>
61 #include <sys/module.h>
62 #include <sys/nv.h>
63 #include <sys/proc.h>
64 #include <sys/sdt.h>
65 #include <sys/smp.h>
66 #include <sys/socket.h>
67 #include <sys/sysctl.h>
68 #include <sys/md5.h>
69 #include <sys/ucred.h>
70 
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/if_private.h>
74 #include <net/vnet.h>
75 #include <net/route.h>
76 #include <net/pfil.h>
77 #include <net/pfvar.h>
78 #include <net/if_pfsync.h>
79 #include <net/if_pflog.h>
80 
81 #include <netinet/in.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_var.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet/ip_icmp.h>
86 #include <netpfil/pf/pf_nl.h>
87 #include <netpfil/pf/pf_nv.h>
88 
89 #ifdef INET6
90 #include <netinet/ip6.h>
91 #endif /* INET6 */
92 
93 #ifdef ALTQ
94 #include <net/altq/altq.h>
95 #endif
96 
97 SDT_PROBE_DEFINE3(pf, ioctl, ioctl, error, "int", "int", "int");
98 SDT_PROBE_DEFINE3(pf, ioctl, function, error, "char *", "int", "int");
99 SDT_PROBE_DEFINE2(pf, ioctl, addrule, error, "int", "int");
100 SDT_PROBE_DEFINE2(pf, ioctl, nvchk, error, "int", "int");
101 
102 static struct pf_kpool	*pf_get_kpool(const char *, u_int32_t, u_int8_t,
103 			    u_int32_t, u_int8_t, u_int8_t, u_int8_t);
104 
105 static void		 pf_mv_kpool(struct pf_kpalist *, struct pf_kpalist *);
106 static void		 pf_empty_kpool(struct pf_kpalist *);
107 static int		 pfioctl(struct cdev *, u_long, caddr_t, int,
108 			    struct thread *);
109 static int		 pf_begin_eth(uint32_t *, const char *);
110 static void		 pf_rollback_eth_cb(struct epoch_context *);
111 static int		 pf_rollback_eth(uint32_t, const char *);
112 static int		 pf_commit_eth(uint32_t, const char *);
113 static void		 pf_free_eth_rule(struct pf_keth_rule *);
114 #ifdef ALTQ
115 static int		 pf_begin_altq(u_int32_t *);
116 static int		 pf_rollback_altq(u_int32_t);
117 static int		 pf_commit_altq(u_int32_t);
118 static int		 pf_enable_altq(struct pf_altq *);
119 static int		 pf_disable_altq(struct pf_altq *);
120 static uint16_t		 pf_qname2qid(const char *);
121 static void		 pf_qid_unref(uint16_t);
122 #endif /* ALTQ */
123 static int		 pf_begin_rules(u_int32_t *, int, const char *);
124 static int		 pf_rollback_rules(u_int32_t, int, char *);
125 static int		 pf_setup_pfsync_matching(struct pf_kruleset *);
126 static void		 pf_hash_rule_rolling(MD5_CTX *, struct pf_krule *);
127 static void		 pf_hash_rule(struct pf_krule *);
128 static void		 pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
129 static int		 pf_commit_rules(u_int32_t, int, char *);
130 static int		 pf_addr_setup(struct pf_kruleset *,
131 			    struct pf_addr_wrap *, sa_family_t);
132 static void		 pf_addr_copyout(struct pf_addr_wrap *);
133 static void		 pf_src_node_copy(const struct pf_ksrc_node *,
134 			    struct pf_src_node *);
135 #ifdef ALTQ
136 static int		 pf_export_kaltq(struct pf_altq *,
137 			    struct pfioc_altq_v1 *, size_t);
138 static int		 pf_import_kaltq(struct pfioc_altq_v1 *,
139 			    struct pf_altq *, size_t);
140 #endif /* ALTQ */
141 
142 VNET_DEFINE(struct pf_krule,	pf_default_rule);
143 
144 static __inline int             pf_krule_compare(struct pf_krule *,
145 				    struct pf_krule *);
146 
147 RB_GENERATE(pf_krule_global, pf_krule, entry_global, pf_krule_compare);
148 
149 #ifdef ALTQ
150 VNET_DEFINE_STATIC(int,		pf_altq_running);
151 #define	V_pf_altq_running	VNET(pf_altq_running)
152 #endif
153 
154 #define	TAGID_MAX	 50000
155 struct pf_tagname {
156 	TAILQ_ENTRY(pf_tagname)	namehash_entries;
157 	TAILQ_ENTRY(pf_tagname)	taghash_entries;
158 	char			name[PF_TAG_NAME_SIZE];
159 	uint16_t		tag;
160 	int			ref;
161 };
162 
163 struct pf_tagset {
164 	TAILQ_HEAD(, pf_tagname)	*namehash;
165 	TAILQ_HEAD(, pf_tagname)	*taghash;
166 	unsigned int			 mask;
167 	uint32_t			 seed;
168 	BITSET_DEFINE(, TAGID_MAX)	 avail;
169 };
170 
171 VNET_DEFINE(struct pf_tagset, pf_tags);
172 #define	V_pf_tags	VNET(pf_tags)
173 static unsigned int	pf_rule_tag_hashsize;
174 #define	PF_RULE_TAG_HASH_SIZE_DEFAULT	128
175 SYSCTL_UINT(_net_pf, OID_AUTO, rule_tag_hashsize, CTLFLAG_RDTUN,
176     &pf_rule_tag_hashsize, PF_RULE_TAG_HASH_SIZE_DEFAULT,
177     "Size of pf(4) rule tag hashtable");
178 
179 #ifdef ALTQ
180 VNET_DEFINE(struct pf_tagset, pf_qids);
181 #define	V_pf_qids	VNET(pf_qids)
182 static unsigned int	pf_queue_tag_hashsize;
183 #define	PF_QUEUE_TAG_HASH_SIZE_DEFAULT	128
184 SYSCTL_UINT(_net_pf, OID_AUTO, queue_tag_hashsize, CTLFLAG_RDTUN,
185     &pf_queue_tag_hashsize, PF_QUEUE_TAG_HASH_SIZE_DEFAULT,
186     "Size of pf(4) queue tag hashtable");
187 #endif
188 VNET_DEFINE(uma_zone_t,	 pf_tag_z);
189 #define	V_pf_tag_z		 VNET(pf_tag_z)
190 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db");
191 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules");
192 
193 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
194 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
195 #endif
196 
197 VNET_DEFINE_STATIC(bool, pf_filter_local) = false;
198 #define V_pf_filter_local	VNET(pf_filter_local)
199 SYSCTL_BOOL(_net_pf, OID_AUTO, filter_local, CTLFLAG_VNET | CTLFLAG_RW,
200     &VNET_NAME(pf_filter_local), false,
201     "Enable filtering for packets delivered to local network stack");
202 
203 #ifdef PF_DEFAULT_TO_DROP
204 VNET_DEFINE_STATIC(bool, default_to_drop) = true;
205 #else
206 VNET_DEFINE_STATIC(bool, default_to_drop);
207 #endif
208 #define	V_default_to_drop VNET(default_to_drop)
209 SYSCTL_BOOL(_net_pf, OID_AUTO, default_to_drop, CTLFLAG_RDTUN | CTLFLAG_VNET,
210     &VNET_NAME(default_to_drop), false,
211     "Make the default rule drop all packets.");
212 
213 static void		 pf_init_tagset(struct pf_tagset *, unsigned int *,
214 			    unsigned int);
215 static void		 pf_cleanup_tagset(struct pf_tagset *);
216 static uint16_t		 tagname2hashindex(const struct pf_tagset *, const char *);
217 static uint16_t		 tag2hashindex(const struct pf_tagset *, uint16_t);
218 static u_int16_t	 tagname2tag(struct pf_tagset *, const char *);
219 static u_int16_t	 pf_tagname2tag(const char *);
220 static void		 tag_unref(struct pf_tagset *, u_int16_t);
221 
222 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
223 
224 struct cdev *pf_dev;
225 
226 /*
227  * XXX - These are new and need to be checked when moveing to a new version
228  */
229 static void		 pf_clear_all_states(void);
230 static unsigned int	 pf_clear_states(const struct pf_kstate_kill *);
231 static void		 pf_killstates(struct pf_kstate_kill *,
232 			    unsigned int *);
233 static int		 pf_killstates_row(struct pf_kstate_kill *,
234 			    struct pf_idhash *);
235 static int		 pf_killstates_nv(struct pfioc_nv *);
236 static int		 pf_clearstates_nv(struct pfioc_nv *);
237 static int		 pf_getstate(struct pfioc_nv *);
238 static int		 pf_getstatus(struct pfioc_nv *);
239 static int		 pf_clear_tables(void);
240 static void		 pf_clear_srcnodes(struct pf_ksrc_node *);
241 static void		 pf_kill_srcnodes(struct pfioc_src_node_kill *);
242 static int		 pf_keepcounters(struct pfioc_nv *);
243 static void		 pf_tbladdr_copyout(struct pf_addr_wrap *);
244 
245 /*
246  * Wrapper functions for pfil(9) hooks
247  */
248 static pfil_return_t pf_eth_check_in(struct mbuf **m, struct ifnet *ifp,
249     int flags, void *ruleset __unused, struct inpcb *inp);
250 static pfil_return_t pf_eth_check_out(struct mbuf **m, struct ifnet *ifp,
251     int flags, void *ruleset __unused, struct inpcb *inp);
252 #ifdef INET
253 static pfil_return_t pf_check_in(struct mbuf **m, struct ifnet *ifp,
254     int flags, void *ruleset __unused, struct inpcb *inp);
255 static pfil_return_t pf_check_out(struct mbuf **m, struct ifnet *ifp,
256     int flags, void *ruleset __unused, struct inpcb *inp);
257 #endif
258 #ifdef INET6
259 static pfil_return_t pf_check6_in(struct mbuf **m, struct ifnet *ifp,
260     int flags, void *ruleset __unused, struct inpcb *inp);
261 static pfil_return_t pf_check6_out(struct mbuf **m, struct ifnet *ifp,
262     int flags, void *ruleset __unused, struct inpcb *inp);
263 #endif
264 
265 static void		hook_pf_eth(void);
266 static void		hook_pf(void);
267 static void		dehook_pf_eth(void);
268 static void		dehook_pf(void);
269 static int		shutdown_pf(void);
270 static int		pf_load(void);
271 static void		pf_unload(void);
272 
273 static struct cdevsw pf_cdevsw = {
274 	.d_ioctl =	pfioctl,
275 	.d_name =	PF_NAME,
276 	.d_version =	D_VERSION,
277 };
278 
279 VNET_DEFINE_STATIC(bool, pf_pfil_hooked);
280 #define V_pf_pfil_hooked	VNET(pf_pfil_hooked)
281 VNET_DEFINE_STATIC(bool, pf_pfil_eth_hooked);
282 #define V_pf_pfil_eth_hooked	VNET(pf_pfil_eth_hooked)
283 
284 /*
285  * We need a flag that is neither hooked nor running to know when
286  * the VNET is "valid".  We primarily need this to control (global)
287  * external event, e.g., eventhandlers.
288  */
289 VNET_DEFINE(int, pf_vnet_active);
290 #define V_pf_vnet_active	VNET(pf_vnet_active)
291 
292 int pf_end_threads;
293 struct proc *pf_purge_proc;
294 
295 VNET_DEFINE(struct rmlock, pf_rules_lock);
296 VNET_DEFINE_STATIC(struct sx, pf_ioctl_lock);
297 #define	V_pf_ioctl_lock		VNET(pf_ioctl_lock)
298 struct sx			pf_end_lock;
299 
300 /* pfsync */
301 VNET_DEFINE(pfsync_state_import_t *, pfsync_state_import_ptr);
302 VNET_DEFINE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
303 VNET_DEFINE(pfsync_update_state_t *, pfsync_update_state_ptr);
304 VNET_DEFINE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
305 VNET_DEFINE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
306 VNET_DEFINE(pfsync_defer_t *, pfsync_defer_ptr);
307 pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr;
308 
309 /* pflog */
310 pflog_packet_t			*pflog_packet_ptr = NULL;
311 
312 /*
313  * Copy a user-provided string, returning an error if truncation would occur.
314  * Avoid scanning past "sz" bytes in the source string since there's no
315  * guarantee that it's nul-terminated.
316  */
317 static int
318 pf_user_strcpy(char *dst, const char *src, size_t sz)
319 {
320 	if (strnlen(src, sz) == sz)
321 		return (EINVAL);
322 	(void)strlcpy(dst, src, sz);
323 	return (0);
324 }
325 
326 static void
327 pfattach_vnet(void)
328 {
329 	u_int32_t *my_timeout = V_pf_default_rule.timeout;
330 
331 	bzero(&V_pf_status, sizeof(V_pf_status));
332 
333 	pf_initialize();
334 	pfr_initialize();
335 	pfi_initialize_vnet();
336 	pf_normalize_init();
337 	pf_syncookies_init();
338 
339 	V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
340 	V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
341 
342 	RB_INIT(&V_pf_anchors);
343 	pf_init_kruleset(&pf_main_ruleset);
344 
345 	pf_init_keth(V_pf_keth);
346 
347 	/* default rule should never be garbage collected */
348 	V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
349 	V_pf_default_rule.action = V_default_to_drop ? PF_DROP : PF_PASS;
350 	V_pf_default_rule.nr = -1;
351 	V_pf_default_rule.rtableid = -1;
352 
353 	pf_counter_u64_init(&V_pf_default_rule.evaluations, M_WAITOK);
354 	for (int i = 0; i < 2; i++) {
355 		pf_counter_u64_init(&V_pf_default_rule.packets[i], M_WAITOK);
356 		pf_counter_u64_init(&V_pf_default_rule.bytes[i], M_WAITOK);
357 	}
358 	V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK);
359 	V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK);
360 	V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK);
361 
362 	V_pf_default_rule.timestamp = uma_zalloc_pcpu(pf_timestamp_pcpu_zone,
363 	    M_WAITOK | M_ZERO);
364 
365 #ifdef PF_WANT_32_TO_64_COUNTER
366 	V_pf_kifmarker = malloc(sizeof(*V_pf_kifmarker), PFI_MTYPE, M_WAITOK | M_ZERO);
367 	V_pf_rulemarker = malloc(sizeof(*V_pf_rulemarker), M_PFRULE, M_WAITOK | M_ZERO);
368 	PF_RULES_WLOCK();
369 	LIST_INSERT_HEAD(&V_pf_allkiflist, V_pf_kifmarker, pfik_allkiflist);
370 	LIST_INSERT_HEAD(&V_pf_allrulelist, &V_pf_default_rule, allrulelist);
371 	V_pf_allrulecount++;
372 	LIST_INSERT_HEAD(&V_pf_allrulelist, V_pf_rulemarker, allrulelist);
373 	PF_RULES_WUNLOCK();
374 #endif
375 
376 	/* initialize default timeouts */
377 	my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
378 	my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
379 	my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
380 	my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
381 	my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
382 	my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
383 	my_timeout[PFTM_SCTP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
384 	my_timeout[PFTM_SCTP_OPENING] = PFTM_TCP_OPENING_VAL;
385 	my_timeout[PFTM_SCTP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
386 	my_timeout[PFTM_SCTP_CLOSING] = PFTM_TCP_CLOSING_VAL;
387 	my_timeout[PFTM_SCTP_CLOSED] = PFTM_TCP_CLOSED_VAL;
388 	my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
389 	my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
390 	my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
391 	my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
392 	my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
393 	my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
394 	my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
395 	my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
396 	my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
397 	my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
398 	my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
399 	my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
400 	my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
401 	my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
402 
403 	V_pf_status.debug = PF_DEBUG_URGENT;
404 	/*
405 	 * XXX This is different than in OpenBSD where reassembly is enabled by
406 	 * defult. In FreeBSD we expect people to still use scrub rules and
407 	 * switch to the new syntax later. Only when they switch they must
408 	 * explicitly enable reassemle. We could change the default once the
409 	 * scrub rule functionality is hopefully removed some day in future.
410 	 */
411 	V_pf_status.reass = 0;
412 
413 	V_pf_pfil_hooked = false;
414 	V_pf_pfil_eth_hooked = false;
415 
416 	/* XXX do our best to avoid a conflict */
417 	V_pf_status.hostid = arc4random();
418 
419 	for (int i = 0; i < PFRES_MAX; i++)
420 		V_pf_status.counters[i] = counter_u64_alloc(M_WAITOK);
421 	for (int i = 0; i < KLCNT_MAX; i++)
422 		V_pf_status.lcounters[i] = counter_u64_alloc(M_WAITOK);
423 	for (int i = 0; i < FCNT_MAX; i++)
424 		pf_counter_u64_init(&V_pf_status.fcounters[i], M_WAITOK);
425 	for (int i = 0; i < SCNT_MAX; i++)
426 		V_pf_status.scounters[i] = counter_u64_alloc(M_WAITOK);
427 
428 	if (swi_add(&V_pf_swi_ie, "pf send", pf_intr, curvnet, SWI_NET,
429 	    INTR_MPSAFE, &V_pf_swi_cookie) != 0)
430 		/* XXXGL: leaked all above. */
431 		return;
432 }
433 
434 static struct pf_kpool *
435 pf_get_kpool(const char *anchor, u_int32_t ticket, u_int8_t rule_action,
436     u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
437     u_int8_t check_ticket)
438 {
439 	struct pf_kruleset	*ruleset;
440 	struct pf_krule		*rule;
441 	int			 rs_num;
442 
443 	ruleset = pf_find_kruleset(anchor);
444 	if (ruleset == NULL)
445 		return (NULL);
446 	rs_num = pf_get_ruleset_number(rule_action);
447 	if (rs_num >= PF_RULESET_MAX)
448 		return (NULL);
449 	if (active) {
450 		if (check_ticket && ticket !=
451 		    ruleset->rules[rs_num].active.ticket)
452 			return (NULL);
453 		if (r_last)
454 			rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
455 			    pf_krulequeue);
456 		else
457 			rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
458 	} else {
459 		if (check_ticket && ticket !=
460 		    ruleset->rules[rs_num].inactive.ticket)
461 			return (NULL);
462 		if (r_last)
463 			rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
464 			    pf_krulequeue);
465 		else
466 			rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
467 	}
468 	if (!r_last) {
469 		while ((rule != NULL) && (rule->nr != rule_number))
470 			rule = TAILQ_NEXT(rule, entries);
471 	}
472 	if (rule == NULL)
473 		return (NULL);
474 
475 	return (&rule->rpool);
476 }
477 
478 static void
479 pf_mv_kpool(struct pf_kpalist *poola, struct pf_kpalist *poolb)
480 {
481 	struct pf_kpooladdr	*mv_pool_pa;
482 
483 	while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
484 		TAILQ_REMOVE(poola, mv_pool_pa, entries);
485 		TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
486 	}
487 }
488 
489 static void
490 pf_empty_kpool(struct pf_kpalist *poola)
491 {
492 	struct pf_kpooladdr *pa;
493 
494 	while ((pa = TAILQ_FIRST(poola)) != NULL) {
495 		switch (pa->addr.type) {
496 		case PF_ADDR_DYNIFTL:
497 			pfi_dynaddr_remove(pa->addr.p.dyn);
498 			break;
499 		case PF_ADDR_TABLE:
500 			/* XXX: this could be unfinished pooladdr on pabuf */
501 			if (pa->addr.p.tbl != NULL)
502 				pfr_detach_table(pa->addr.p.tbl);
503 			break;
504 		}
505 		if (pa->kif)
506 			pfi_kkif_unref(pa->kif);
507 		TAILQ_REMOVE(poola, pa, entries);
508 		free(pa, M_PFRULE);
509 	}
510 }
511 
512 static void
513 pf_unlink_rule_locked(struct pf_krulequeue *rulequeue, struct pf_krule *rule)
514 {
515 
516 	PF_RULES_WASSERT();
517 	PF_UNLNKDRULES_ASSERT();
518 
519 	TAILQ_REMOVE(rulequeue, rule, entries);
520 
521 	rule->rule_ref |= PFRULE_REFS;
522 	TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
523 }
524 
525 static void
526 pf_unlink_rule(struct pf_krulequeue *rulequeue, struct pf_krule *rule)
527 {
528 
529 	PF_RULES_WASSERT();
530 
531 	PF_UNLNKDRULES_LOCK();
532 	pf_unlink_rule_locked(rulequeue, rule);
533 	PF_UNLNKDRULES_UNLOCK();
534 }
535 
536 static void
537 pf_free_eth_rule(struct pf_keth_rule *rule)
538 {
539 	PF_RULES_WASSERT();
540 
541 	if (rule == NULL)
542 		return;
543 
544 	if (rule->tag)
545 		tag_unref(&V_pf_tags, rule->tag);
546 	if (rule->match_tag)
547 		tag_unref(&V_pf_tags, rule->match_tag);
548 #ifdef ALTQ
549 	pf_qid_unref(rule->qid);
550 #endif
551 
552 	if (rule->bridge_to)
553 		pfi_kkif_unref(rule->bridge_to);
554 	if (rule->kif)
555 		pfi_kkif_unref(rule->kif);
556 
557 	if (rule->ipsrc.addr.type == PF_ADDR_TABLE)
558 		pfr_detach_table(rule->ipsrc.addr.p.tbl);
559 	if (rule->ipdst.addr.type == PF_ADDR_TABLE)
560 		pfr_detach_table(rule->ipdst.addr.p.tbl);
561 
562 	counter_u64_free(rule->evaluations);
563 	for (int i = 0; i < 2; i++) {
564 		counter_u64_free(rule->packets[i]);
565 		counter_u64_free(rule->bytes[i]);
566 	}
567 	uma_zfree_pcpu(pf_timestamp_pcpu_zone, rule->timestamp);
568 	pf_keth_anchor_remove(rule);
569 
570 	free(rule, M_PFRULE);
571 }
572 
573 void
574 pf_free_rule(struct pf_krule *rule)
575 {
576 
577 	PF_RULES_WASSERT();
578 	PF_CONFIG_ASSERT();
579 
580 	if (rule->tag)
581 		tag_unref(&V_pf_tags, rule->tag);
582 	if (rule->match_tag)
583 		tag_unref(&V_pf_tags, rule->match_tag);
584 #ifdef ALTQ
585 	if (rule->pqid != rule->qid)
586 		pf_qid_unref(rule->pqid);
587 	pf_qid_unref(rule->qid);
588 #endif
589 	switch (rule->src.addr.type) {
590 	case PF_ADDR_DYNIFTL:
591 		pfi_dynaddr_remove(rule->src.addr.p.dyn);
592 		break;
593 	case PF_ADDR_TABLE:
594 		pfr_detach_table(rule->src.addr.p.tbl);
595 		break;
596 	}
597 	switch (rule->dst.addr.type) {
598 	case PF_ADDR_DYNIFTL:
599 		pfi_dynaddr_remove(rule->dst.addr.p.dyn);
600 		break;
601 	case PF_ADDR_TABLE:
602 		pfr_detach_table(rule->dst.addr.p.tbl);
603 		break;
604 	}
605 	if (rule->overload_tbl)
606 		pfr_detach_table(rule->overload_tbl);
607 	if (rule->kif)
608 		pfi_kkif_unref(rule->kif);
609 	pf_kanchor_remove(rule);
610 	pf_empty_kpool(&rule->rpool.list);
611 
612 	pf_krule_free(rule);
613 }
614 
615 static void
616 pf_init_tagset(struct pf_tagset *ts, unsigned int *tunable_size,
617     unsigned int default_size)
618 {
619 	unsigned int i;
620 	unsigned int hashsize;
621 
622 	if (*tunable_size == 0 || !powerof2(*tunable_size))
623 		*tunable_size = default_size;
624 
625 	hashsize = *tunable_size;
626 	ts->namehash = mallocarray(hashsize, sizeof(*ts->namehash), M_PFHASH,
627 	    M_WAITOK);
628 	ts->taghash = mallocarray(hashsize, sizeof(*ts->taghash), M_PFHASH,
629 	    M_WAITOK);
630 	ts->mask = hashsize - 1;
631 	ts->seed = arc4random();
632 	for (i = 0; i < hashsize; i++) {
633 		TAILQ_INIT(&ts->namehash[i]);
634 		TAILQ_INIT(&ts->taghash[i]);
635 	}
636 	BIT_FILL(TAGID_MAX, &ts->avail);
637 }
638 
639 static void
640 pf_cleanup_tagset(struct pf_tagset *ts)
641 {
642 	unsigned int i;
643 	unsigned int hashsize;
644 	struct pf_tagname *t, *tmp;
645 
646 	/*
647 	 * Only need to clean up one of the hashes as each tag is hashed
648 	 * into each table.
649 	 */
650 	hashsize = ts->mask + 1;
651 	for (i = 0; i < hashsize; i++)
652 		TAILQ_FOREACH_SAFE(t, &ts->namehash[i], namehash_entries, tmp)
653 			uma_zfree(V_pf_tag_z, t);
654 
655 	free(ts->namehash, M_PFHASH);
656 	free(ts->taghash, M_PFHASH);
657 }
658 
659 static uint16_t
660 tagname2hashindex(const struct pf_tagset *ts, const char *tagname)
661 {
662 	size_t len;
663 
664 	len = strnlen(tagname, PF_TAG_NAME_SIZE - 1);
665 	return (murmur3_32_hash(tagname, len, ts->seed) & ts->mask);
666 }
667 
668 static uint16_t
669 tag2hashindex(const struct pf_tagset *ts, uint16_t tag)
670 {
671 
672 	return (tag & ts->mask);
673 }
674 
675 static u_int16_t
676 tagname2tag(struct pf_tagset *ts, const char *tagname)
677 {
678 	struct pf_tagname	*tag;
679 	u_int32_t		 index;
680 	u_int16_t		 new_tagid;
681 
682 	PF_RULES_WASSERT();
683 
684 	index = tagname2hashindex(ts, tagname);
685 	TAILQ_FOREACH(tag, &ts->namehash[index], namehash_entries)
686 		if (strcmp(tagname, tag->name) == 0) {
687 			tag->ref++;
688 			return (tag->tag);
689 		}
690 
691 	/*
692 	 * new entry
693 	 *
694 	 * to avoid fragmentation, we do a linear search from the beginning
695 	 * and take the first free slot we find.
696 	 */
697 	new_tagid = BIT_FFS(TAGID_MAX, &ts->avail);
698 	/*
699 	 * Tags are 1-based, with valid tags in the range [1..TAGID_MAX].
700 	 * BIT_FFS() returns a 1-based bit number, with 0 indicating no bits
701 	 * set.  It may also return a bit number greater than TAGID_MAX due
702 	 * to rounding of the number of bits in the vector up to a multiple
703 	 * of the vector word size at declaration/allocation time.
704 	 */
705 	if ((new_tagid == 0) || (new_tagid > TAGID_MAX))
706 		return (0);
707 
708 	/* Mark the tag as in use.  Bits are 0-based for BIT_CLR() */
709 	BIT_CLR(TAGID_MAX, new_tagid - 1, &ts->avail);
710 
711 	/* allocate and fill new struct pf_tagname */
712 	tag = uma_zalloc(V_pf_tag_z, M_NOWAIT);
713 	if (tag == NULL)
714 		return (0);
715 	strlcpy(tag->name, tagname, sizeof(tag->name));
716 	tag->tag = new_tagid;
717 	tag->ref = 1;
718 
719 	/* Insert into namehash */
720 	TAILQ_INSERT_TAIL(&ts->namehash[index], tag, namehash_entries);
721 
722 	/* Insert into taghash */
723 	index = tag2hashindex(ts, new_tagid);
724 	TAILQ_INSERT_TAIL(&ts->taghash[index], tag, taghash_entries);
725 
726 	return (tag->tag);
727 }
728 
729 static void
730 tag_unref(struct pf_tagset *ts, u_int16_t tag)
731 {
732 	struct pf_tagname	*t;
733 	uint16_t		 index;
734 
735 	PF_RULES_WASSERT();
736 
737 	index = tag2hashindex(ts, tag);
738 	TAILQ_FOREACH(t, &ts->taghash[index], taghash_entries)
739 		if (tag == t->tag) {
740 			if (--t->ref == 0) {
741 				TAILQ_REMOVE(&ts->taghash[index], t,
742 				    taghash_entries);
743 				index = tagname2hashindex(ts, t->name);
744 				TAILQ_REMOVE(&ts->namehash[index], t,
745 				    namehash_entries);
746 				/* Bits are 0-based for BIT_SET() */
747 				BIT_SET(TAGID_MAX, tag - 1, &ts->avail);
748 				uma_zfree(V_pf_tag_z, t);
749 			}
750 			break;
751 		}
752 }
753 
754 static uint16_t
755 pf_tagname2tag(const char *tagname)
756 {
757 	return (tagname2tag(&V_pf_tags, tagname));
758 }
759 
760 static int
761 pf_begin_eth(uint32_t *ticket, const char *anchor)
762 {
763 	struct pf_keth_rule *rule, *tmp;
764 	struct pf_keth_ruleset *rs;
765 
766 	PF_RULES_WASSERT();
767 
768 	rs = pf_find_or_create_keth_ruleset(anchor);
769 	if (rs == NULL)
770 		return (EINVAL);
771 
772 	/* Purge old inactive rules. */
773 	TAILQ_FOREACH_SAFE(rule, rs->inactive.rules, entries,
774 	    tmp) {
775 		TAILQ_REMOVE(rs->inactive.rules, rule,
776 		    entries);
777 		pf_free_eth_rule(rule);
778 	}
779 
780 	*ticket = ++rs->inactive.ticket;
781 	rs->inactive.open = 1;
782 
783 	return (0);
784 }
785 
786 static void
787 pf_rollback_eth_cb(struct epoch_context *ctx)
788 {
789 	struct pf_keth_ruleset *rs;
790 
791 	rs = __containerof(ctx, struct pf_keth_ruleset, epoch_ctx);
792 
793 	CURVNET_SET(rs->vnet);
794 
795 	PF_RULES_WLOCK();
796 	pf_rollback_eth(rs->inactive.ticket,
797 	    rs->anchor ? rs->anchor->path : "");
798 	PF_RULES_WUNLOCK();
799 
800 	CURVNET_RESTORE();
801 }
802 
803 static int
804 pf_rollback_eth(uint32_t ticket, const char *anchor)
805 {
806 	struct pf_keth_rule *rule, *tmp;
807 	struct pf_keth_ruleset *rs;
808 
809 	PF_RULES_WASSERT();
810 
811 	rs = pf_find_keth_ruleset(anchor);
812 	if (rs == NULL)
813 		return (EINVAL);
814 
815 	if (!rs->inactive.open ||
816 	    ticket != rs->inactive.ticket)
817 		return (0);
818 
819 	/* Purge old inactive rules. */
820 	TAILQ_FOREACH_SAFE(rule, rs->inactive.rules, entries,
821 	    tmp) {
822 		TAILQ_REMOVE(rs->inactive.rules, rule, entries);
823 		pf_free_eth_rule(rule);
824 	}
825 
826 	rs->inactive.open = 0;
827 
828 	pf_remove_if_empty_keth_ruleset(rs);
829 
830 	return (0);
831 }
832 
833 #define	PF_SET_SKIP_STEPS(i)					\
834 	do {							\
835 		while (head[i] != cur) {			\
836 			head[i]->skip[i].ptr = cur;		\
837 			head[i] = TAILQ_NEXT(head[i], entries);	\
838 		}						\
839 	} while (0)
840 
841 static void
842 pf_eth_calc_skip_steps(struct pf_keth_ruleq *rules)
843 {
844 	struct pf_keth_rule *cur, *prev, *head[PFE_SKIP_COUNT];
845 	int i;
846 
847 	cur = TAILQ_FIRST(rules);
848 	prev = cur;
849 	for (i = 0; i < PFE_SKIP_COUNT; ++i)
850 		head[i] = cur;
851 	while (cur != NULL) {
852 		if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
853 			PF_SET_SKIP_STEPS(PFE_SKIP_IFP);
854 		if (cur->direction != prev->direction)
855 			PF_SET_SKIP_STEPS(PFE_SKIP_DIR);
856 		if (cur->proto != prev->proto)
857 			PF_SET_SKIP_STEPS(PFE_SKIP_PROTO);
858 		if (memcmp(&cur->src, &prev->src, sizeof(cur->src)) != 0)
859 			PF_SET_SKIP_STEPS(PFE_SKIP_SRC_ADDR);
860 		if (memcmp(&cur->dst, &prev->dst, sizeof(cur->dst)) != 0)
861 			PF_SET_SKIP_STEPS(PFE_SKIP_DST_ADDR);
862 		if (cur->ipsrc.neg != prev->ipsrc.neg ||
863 		    pf_addr_wrap_neq(&cur->ipsrc.addr, &prev->ipsrc.addr))
864 			PF_SET_SKIP_STEPS(PFE_SKIP_SRC_IP_ADDR);
865 		if (cur->ipdst.neg != prev->ipdst.neg ||
866 		    pf_addr_wrap_neq(&cur->ipdst.addr, &prev->ipdst.addr))
867 			PF_SET_SKIP_STEPS(PFE_SKIP_DST_IP_ADDR);
868 
869 		prev = cur;
870 		cur = TAILQ_NEXT(cur, entries);
871 	}
872 	for (i = 0; i < PFE_SKIP_COUNT; ++i)
873 		PF_SET_SKIP_STEPS(i);
874 }
875 
876 static int
877 pf_commit_eth(uint32_t ticket, const char *anchor)
878 {
879 	struct pf_keth_ruleq *rules;
880 	struct pf_keth_ruleset *rs;
881 
882 	rs = pf_find_keth_ruleset(anchor);
883 	if (rs == NULL) {
884 		return (EINVAL);
885 	}
886 
887 	if (!rs->inactive.open ||
888 	    ticket != rs->inactive.ticket)
889 		return (EBUSY);
890 
891 	PF_RULES_WASSERT();
892 
893 	pf_eth_calc_skip_steps(rs->inactive.rules);
894 
895 	rules = rs->active.rules;
896 	ck_pr_store_ptr(&rs->active.rules, rs->inactive.rules);
897 	rs->inactive.rules = rules;
898 	rs->inactive.ticket = rs->active.ticket;
899 
900 	/* Clean up inactive rules (i.e. previously active rules), only when
901 	 * we're sure they're no longer used. */
902 	NET_EPOCH_CALL(pf_rollback_eth_cb, &rs->epoch_ctx);
903 
904 	return (0);
905 }
906 
907 #ifdef ALTQ
908 static uint16_t
909 pf_qname2qid(const char *qname)
910 {
911 	return (tagname2tag(&V_pf_qids, qname));
912 }
913 
914 static void
915 pf_qid_unref(uint16_t qid)
916 {
917 	tag_unref(&V_pf_qids, qid);
918 }
919 
920 static int
921 pf_begin_altq(u_int32_t *ticket)
922 {
923 	struct pf_altq	*altq, *tmp;
924 	int		 error = 0;
925 
926 	PF_RULES_WASSERT();
927 
928 	/* Purge the old altq lists */
929 	TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) {
930 		if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
931 			/* detach and destroy the discipline */
932 			error = altq_remove(altq);
933 		}
934 		free(altq, M_PFALTQ);
935 	}
936 	TAILQ_INIT(V_pf_altq_ifs_inactive);
937 	TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) {
938 		pf_qid_unref(altq->qid);
939 		free(altq, M_PFALTQ);
940 	}
941 	TAILQ_INIT(V_pf_altqs_inactive);
942 	if (error)
943 		return (error);
944 	*ticket = ++V_ticket_altqs_inactive;
945 	V_altqs_inactive_open = 1;
946 	return (0);
947 }
948 
949 static int
950 pf_rollback_altq(u_int32_t ticket)
951 {
952 	struct pf_altq	*altq, *tmp;
953 	int		 error = 0;
954 
955 	PF_RULES_WASSERT();
956 
957 	if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
958 		return (0);
959 	/* Purge the old altq lists */
960 	TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) {
961 		if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
962 			/* detach and destroy the discipline */
963 			error = altq_remove(altq);
964 		}
965 		free(altq, M_PFALTQ);
966 	}
967 	TAILQ_INIT(V_pf_altq_ifs_inactive);
968 	TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) {
969 		pf_qid_unref(altq->qid);
970 		free(altq, M_PFALTQ);
971 	}
972 	TAILQ_INIT(V_pf_altqs_inactive);
973 	V_altqs_inactive_open = 0;
974 	return (error);
975 }
976 
977 static int
978 pf_commit_altq(u_int32_t ticket)
979 {
980 	struct pf_altqqueue	*old_altqs, *old_altq_ifs;
981 	struct pf_altq		*altq, *tmp;
982 	int			 err, error = 0;
983 
984 	PF_RULES_WASSERT();
985 
986 	if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
987 		return (EBUSY);
988 
989 	/* swap altqs, keep the old. */
990 	old_altqs = V_pf_altqs_active;
991 	old_altq_ifs = V_pf_altq_ifs_active;
992 	V_pf_altqs_active = V_pf_altqs_inactive;
993 	V_pf_altq_ifs_active = V_pf_altq_ifs_inactive;
994 	V_pf_altqs_inactive = old_altqs;
995 	V_pf_altq_ifs_inactive = old_altq_ifs;
996 	V_ticket_altqs_active = V_ticket_altqs_inactive;
997 
998 	/* Attach new disciplines */
999 	TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
1000 		if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
1001 			/* attach the discipline */
1002 			error = altq_pfattach(altq);
1003 			if (error == 0 && V_pf_altq_running)
1004 				error = pf_enable_altq(altq);
1005 			if (error != 0)
1006 				return (error);
1007 		}
1008 	}
1009 
1010 	/* Purge the old altq lists */
1011 	TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) {
1012 		if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
1013 			/* detach and destroy the discipline */
1014 			if (V_pf_altq_running)
1015 				error = pf_disable_altq(altq);
1016 			err = altq_pfdetach(altq);
1017 			if (err != 0 && error == 0)
1018 				error = err;
1019 			err = altq_remove(altq);
1020 			if (err != 0 && error == 0)
1021 				error = err;
1022 		}
1023 		free(altq, M_PFALTQ);
1024 	}
1025 	TAILQ_INIT(V_pf_altq_ifs_inactive);
1026 	TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) {
1027 		pf_qid_unref(altq->qid);
1028 		free(altq, M_PFALTQ);
1029 	}
1030 	TAILQ_INIT(V_pf_altqs_inactive);
1031 
1032 	V_altqs_inactive_open = 0;
1033 	return (error);
1034 }
1035 
1036 static int
1037 pf_enable_altq(struct pf_altq *altq)
1038 {
1039 	struct ifnet		*ifp;
1040 	struct tb_profile	 tb;
1041 	int			 error = 0;
1042 
1043 	if ((ifp = ifunit(altq->ifname)) == NULL)
1044 		return (EINVAL);
1045 
1046 	if (ifp->if_snd.altq_type != ALTQT_NONE)
1047 		error = altq_enable(&ifp->if_snd);
1048 
1049 	/* set tokenbucket regulator */
1050 	if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
1051 		tb.rate = altq->ifbandwidth;
1052 		tb.depth = altq->tbrsize;
1053 		error = tbr_set(&ifp->if_snd, &tb);
1054 	}
1055 
1056 	return (error);
1057 }
1058 
1059 static int
1060 pf_disable_altq(struct pf_altq *altq)
1061 {
1062 	struct ifnet		*ifp;
1063 	struct tb_profile	 tb;
1064 	int			 error;
1065 
1066 	if ((ifp = ifunit(altq->ifname)) == NULL)
1067 		return (EINVAL);
1068 
1069 	/*
1070 	 * when the discipline is no longer referenced, it was overridden
1071 	 * by a new one.  if so, just return.
1072 	 */
1073 	if (altq->altq_disc != ifp->if_snd.altq_disc)
1074 		return (0);
1075 
1076 	error = altq_disable(&ifp->if_snd);
1077 
1078 	if (error == 0) {
1079 		/* clear tokenbucket regulator */
1080 		tb.rate = 0;
1081 		error = tbr_set(&ifp->if_snd, &tb);
1082 	}
1083 
1084 	return (error);
1085 }
1086 
1087 static int
1088 pf_altq_ifnet_event_add(struct ifnet *ifp, int remove, u_int32_t ticket,
1089     struct pf_altq *altq)
1090 {
1091 	struct ifnet	*ifp1;
1092 	int		 error = 0;
1093 
1094 	/* Deactivate the interface in question */
1095 	altq->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
1096 	if ((ifp1 = ifunit(altq->ifname)) == NULL ||
1097 	    (remove && ifp1 == ifp)) {
1098 		altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
1099 	} else {
1100 		error = altq_add(ifp1, altq);
1101 
1102 		if (ticket != V_ticket_altqs_inactive)
1103 			error = EBUSY;
1104 
1105 		if (error)
1106 			free(altq, M_PFALTQ);
1107 	}
1108 
1109 	return (error);
1110 }
1111 
1112 void
1113 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
1114 {
1115 	struct pf_altq	*a1, *a2, *a3;
1116 	u_int32_t	 ticket;
1117 	int		 error = 0;
1118 
1119 	/*
1120 	 * No need to re-evaluate the configuration for events on interfaces
1121 	 * that do not support ALTQ, as it's not possible for such
1122 	 * interfaces to be part of the configuration.
1123 	 */
1124 	if (!ALTQ_IS_READY(&ifp->if_snd))
1125 		return;
1126 
1127 	/* Interrupt userland queue modifications */
1128 	if (V_altqs_inactive_open)
1129 		pf_rollback_altq(V_ticket_altqs_inactive);
1130 
1131 	/* Start new altq ruleset */
1132 	if (pf_begin_altq(&ticket))
1133 		return;
1134 
1135 	/* Copy the current active set */
1136 	TAILQ_FOREACH(a1, V_pf_altq_ifs_active, entries) {
1137 		a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
1138 		if (a2 == NULL) {
1139 			error = ENOMEM;
1140 			break;
1141 		}
1142 		bcopy(a1, a2, sizeof(struct pf_altq));
1143 
1144 		error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2);
1145 		if (error)
1146 			break;
1147 
1148 		TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, a2, entries);
1149 	}
1150 	if (error)
1151 		goto out;
1152 	TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
1153 		a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
1154 		if (a2 == NULL) {
1155 			error = ENOMEM;
1156 			break;
1157 		}
1158 		bcopy(a1, a2, sizeof(struct pf_altq));
1159 
1160 		if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
1161 			error = EBUSY;
1162 			free(a2, M_PFALTQ);
1163 			break;
1164 		}
1165 		a2->altq_disc = NULL;
1166 		TAILQ_FOREACH(a3, V_pf_altq_ifs_inactive, entries) {
1167 			if (strncmp(a3->ifname, a2->ifname,
1168 				IFNAMSIZ) == 0) {
1169 				a2->altq_disc = a3->altq_disc;
1170 				break;
1171 			}
1172 		}
1173 		error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2);
1174 		if (error)
1175 			break;
1176 
1177 		TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
1178 	}
1179 
1180 out:
1181 	if (error != 0)
1182 		pf_rollback_altq(ticket);
1183 	else
1184 		pf_commit_altq(ticket);
1185 }
1186 #endif /* ALTQ */
1187 
1188 static struct pf_krule_global *
1189 pf_rule_tree_alloc(int flags)
1190 {
1191 	struct pf_krule_global *tree;
1192 
1193 	tree = malloc(sizeof(struct pf_krule_global), M_TEMP, flags);
1194 	if (tree == NULL)
1195 		return (NULL);
1196 	RB_INIT(tree);
1197 	return (tree);
1198 }
1199 
1200 static void
1201 pf_rule_tree_free(struct pf_krule_global *tree)
1202 {
1203 
1204 	free(tree, M_TEMP);
1205 }
1206 
1207 static int
1208 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
1209 {
1210 	struct pf_krule_global *tree;
1211 	struct pf_kruleset	*rs;
1212 	struct pf_krule		*rule;
1213 
1214 	PF_RULES_WASSERT();
1215 
1216 	if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1217 		return (EINVAL);
1218 	tree = pf_rule_tree_alloc(M_NOWAIT);
1219 	if (tree == NULL)
1220 		return (ENOMEM);
1221 	rs = pf_find_or_create_kruleset(anchor);
1222 	if (rs == NULL) {
1223 		free(tree, M_TEMP);
1224 		return (EINVAL);
1225 	}
1226 	pf_rule_tree_free(rs->rules[rs_num].inactive.tree);
1227 	rs->rules[rs_num].inactive.tree = tree;
1228 
1229 	while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1230 		pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
1231 		rs->rules[rs_num].inactive.rcount--;
1232 	}
1233 	*ticket = ++rs->rules[rs_num].inactive.ticket;
1234 	rs->rules[rs_num].inactive.open = 1;
1235 	return (0);
1236 }
1237 
1238 static int
1239 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
1240 {
1241 	struct pf_kruleset	*rs;
1242 	struct pf_krule		*rule;
1243 
1244 	PF_RULES_WASSERT();
1245 
1246 	if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1247 		return (EINVAL);
1248 	rs = pf_find_kruleset(anchor);
1249 	if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1250 	    rs->rules[rs_num].inactive.ticket != ticket)
1251 		return (0);
1252 	while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1253 		pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
1254 		rs->rules[rs_num].inactive.rcount--;
1255 	}
1256 	rs->rules[rs_num].inactive.open = 0;
1257 	return (0);
1258 }
1259 
1260 #define PF_MD5_UPD(st, elm)						\
1261 		MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
1262 
1263 #define PF_MD5_UPD_STR(st, elm)						\
1264 		MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
1265 
1266 #define PF_MD5_UPD_HTONL(st, elm, stor) do {				\
1267 		(stor) = htonl((st)->elm);				\
1268 		MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
1269 } while (0)
1270 
1271 #define PF_MD5_UPD_HTONS(st, elm, stor) do {				\
1272 		(stor) = htons((st)->elm);				\
1273 		MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
1274 } while (0)
1275 
1276 static void
1277 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
1278 {
1279 	PF_MD5_UPD(pfr, addr.type);
1280 	switch (pfr->addr.type) {
1281 		case PF_ADDR_DYNIFTL:
1282 			PF_MD5_UPD(pfr, addr.v.ifname);
1283 			PF_MD5_UPD(pfr, addr.iflags);
1284 			break;
1285 		case PF_ADDR_TABLE:
1286 			PF_MD5_UPD(pfr, addr.v.tblname);
1287 			break;
1288 		case PF_ADDR_ADDRMASK:
1289 			/* XXX ignore af? */
1290 			PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
1291 			PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
1292 			break;
1293 	}
1294 
1295 	PF_MD5_UPD(pfr, port[0]);
1296 	PF_MD5_UPD(pfr, port[1]);
1297 	PF_MD5_UPD(pfr, neg);
1298 	PF_MD5_UPD(pfr, port_op);
1299 }
1300 
1301 static void
1302 pf_hash_rule_rolling(MD5_CTX *ctx, struct pf_krule *rule)
1303 {
1304 	u_int16_t x;
1305 	u_int32_t y;
1306 
1307 	pf_hash_rule_addr(ctx, &rule->src);
1308 	pf_hash_rule_addr(ctx, &rule->dst);
1309 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1310 		PF_MD5_UPD_STR(rule, label[i]);
1311 	PF_MD5_UPD_STR(rule, ifname);
1312 	PF_MD5_UPD_STR(rule, match_tagname);
1313 	PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
1314 	PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
1315 	PF_MD5_UPD_HTONL(rule, prob, y);
1316 	PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
1317 	PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
1318 	PF_MD5_UPD(rule, uid.op);
1319 	PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
1320 	PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
1321 	PF_MD5_UPD(rule, gid.op);
1322 	PF_MD5_UPD_HTONL(rule, rule_flag, y);
1323 	PF_MD5_UPD(rule, action);
1324 	PF_MD5_UPD(rule, direction);
1325 	PF_MD5_UPD(rule, af);
1326 	PF_MD5_UPD(rule, quick);
1327 	PF_MD5_UPD(rule, ifnot);
1328 	PF_MD5_UPD(rule, match_tag_not);
1329 	PF_MD5_UPD(rule, natpass);
1330 	PF_MD5_UPD(rule, keep_state);
1331 	PF_MD5_UPD(rule, proto);
1332 	PF_MD5_UPD(rule, type);
1333 	PF_MD5_UPD(rule, code);
1334 	PF_MD5_UPD(rule, flags);
1335 	PF_MD5_UPD(rule, flagset);
1336 	PF_MD5_UPD(rule, allow_opts);
1337 	PF_MD5_UPD(rule, rt);
1338 	PF_MD5_UPD(rule, tos);
1339 	PF_MD5_UPD(rule, scrub_flags);
1340 	PF_MD5_UPD(rule, min_ttl);
1341 	PF_MD5_UPD(rule, set_tos);
1342 	if (rule->anchor != NULL)
1343 		PF_MD5_UPD_STR(rule, anchor->path);
1344 }
1345 
1346 static void
1347 pf_hash_rule(struct pf_krule *rule)
1348 {
1349 	MD5_CTX		ctx;
1350 
1351 	MD5Init(&ctx);
1352 	pf_hash_rule_rolling(&ctx, rule);
1353 	MD5Final(rule->md5sum, &ctx);
1354 }
1355 
1356 static int
1357 pf_krule_compare(struct pf_krule *a, struct pf_krule *b)
1358 {
1359 
1360 	return (memcmp(a->md5sum, b->md5sum, PF_MD5_DIGEST_LENGTH));
1361 }
1362 
1363 static int
1364 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
1365 {
1366 	struct pf_kruleset	*rs;
1367 	struct pf_krule		*rule, **old_array, *old_rule;
1368 	struct pf_krulequeue	*old_rules;
1369 	struct pf_krule_global  *old_tree;
1370 	int			 error;
1371 	u_int32_t		 old_rcount;
1372 
1373 	PF_RULES_WASSERT();
1374 
1375 	if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1376 		return (EINVAL);
1377 	rs = pf_find_kruleset(anchor);
1378 	if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1379 	    ticket != rs->rules[rs_num].inactive.ticket)
1380 		return (EBUSY);
1381 
1382 	/* Calculate checksum for the main ruleset */
1383 	if (rs == &pf_main_ruleset) {
1384 		error = pf_setup_pfsync_matching(rs);
1385 		if (error != 0)
1386 			return (error);
1387 	}
1388 
1389 	/* Swap rules, keep the old. */
1390 	old_rules = rs->rules[rs_num].active.ptr;
1391 	old_rcount = rs->rules[rs_num].active.rcount;
1392 	old_array = rs->rules[rs_num].active.ptr_array;
1393 	old_tree = rs->rules[rs_num].active.tree;
1394 
1395 	rs->rules[rs_num].active.ptr =
1396 	    rs->rules[rs_num].inactive.ptr;
1397 	rs->rules[rs_num].active.ptr_array =
1398 	    rs->rules[rs_num].inactive.ptr_array;
1399 	rs->rules[rs_num].active.tree =
1400 	    rs->rules[rs_num].inactive.tree;
1401 	rs->rules[rs_num].active.rcount =
1402 	    rs->rules[rs_num].inactive.rcount;
1403 
1404 	/* Attempt to preserve counter information. */
1405 	if (V_pf_status.keep_counters && old_tree != NULL) {
1406 		TAILQ_FOREACH(rule, rs->rules[rs_num].active.ptr,
1407 		    entries) {
1408 			old_rule = RB_FIND(pf_krule_global, old_tree, rule);
1409 			if (old_rule == NULL) {
1410 				continue;
1411 			}
1412 			pf_counter_u64_critical_enter();
1413 			pf_counter_u64_add_protected(&rule->evaluations,
1414 			    pf_counter_u64_fetch(&old_rule->evaluations));
1415 			pf_counter_u64_add_protected(&rule->packets[0],
1416 			    pf_counter_u64_fetch(&old_rule->packets[0]));
1417 			pf_counter_u64_add_protected(&rule->packets[1],
1418 			    pf_counter_u64_fetch(&old_rule->packets[1]));
1419 			pf_counter_u64_add_protected(&rule->bytes[0],
1420 			    pf_counter_u64_fetch(&old_rule->bytes[0]));
1421 			pf_counter_u64_add_protected(&rule->bytes[1],
1422 			    pf_counter_u64_fetch(&old_rule->bytes[1]));
1423 			pf_counter_u64_critical_exit();
1424 		}
1425 	}
1426 
1427 	rs->rules[rs_num].inactive.ptr = old_rules;
1428 	rs->rules[rs_num].inactive.ptr_array = old_array;
1429 	rs->rules[rs_num].inactive.tree = NULL; /* important for pf_ioctl_addrule */
1430 	rs->rules[rs_num].inactive.rcount = old_rcount;
1431 
1432 	rs->rules[rs_num].active.ticket =
1433 	    rs->rules[rs_num].inactive.ticket;
1434 	pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
1435 
1436 	/* Purge the old rule list. */
1437 	PF_UNLNKDRULES_LOCK();
1438 	while ((rule = TAILQ_FIRST(old_rules)) != NULL)
1439 		pf_unlink_rule_locked(old_rules, rule);
1440 	PF_UNLNKDRULES_UNLOCK();
1441 	if (rs->rules[rs_num].inactive.ptr_array)
1442 		free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
1443 	rs->rules[rs_num].inactive.ptr_array = NULL;
1444 	rs->rules[rs_num].inactive.rcount = 0;
1445 	rs->rules[rs_num].inactive.open = 0;
1446 	pf_remove_if_empty_kruleset(rs);
1447 	free(old_tree, M_TEMP);
1448 
1449 	return (0);
1450 }
1451 
1452 static int
1453 pf_setup_pfsync_matching(struct pf_kruleset *rs)
1454 {
1455 	MD5_CTX			 ctx;
1456 	struct pf_krule		*rule;
1457 	int			 rs_cnt;
1458 	u_int8_t		 digest[PF_MD5_DIGEST_LENGTH];
1459 
1460 	MD5Init(&ctx);
1461 	for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
1462 		/* XXX PF_RULESET_SCRUB as well? */
1463 		if (rs_cnt == PF_RULESET_SCRUB)
1464 			continue;
1465 
1466 		if (rs->rules[rs_cnt].inactive.ptr_array)
1467 			free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
1468 		rs->rules[rs_cnt].inactive.ptr_array = NULL;
1469 
1470 		if (rs->rules[rs_cnt].inactive.rcount) {
1471 			rs->rules[rs_cnt].inactive.ptr_array =
1472 			    mallocarray(rs->rules[rs_cnt].inactive.rcount,
1473 			    sizeof(struct pf_rule **),
1474 			    M_TEMP, M_NOWAIT);
1475 
1476 			if (!rs->rules[rs_cnt].inactive.ptr_array)
1477 				return (ENOMEM);
1478 		}
1479 
1480 		TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
1481 		    entries) {
1482 			pf_hash_rule_rolling(&ctx, rule);
1483 			(rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
1484 		}
1485 	}
1486 
1487 	MD5Final(digest, &ctx);
1488 	memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
1489 	return (0);
1490 }
1491 
1492 static int
1493 pf_eth_addr_setup(struct pf_keth_ruleset *ruleset, struct pf_addr_wrap *addr)
1494 {
1495 	int error = 0;
1496 
1497 	switch (addr->type) {
1498 	case PF_ADDR_TABLE:
1499 		addr->p.tbl = pfr_eth_attach_table(ruleset, addr->v.tblname);
1500 		if (addr->p.tbl == NULL)
1501 			error = ENOMEM;
1502 		break;
1503 	default:
1504 		error = EINVAL;
1505 	}
1506 
1507 	return (error);
1508 }
1509 
1510 static int
1511 pf_addr_setup(struct pf_kruleset *ruleset, struct pf_addr_wrap *addr,
1512     sa_family_t af)
1513 {
1514 	int error = 0;
1515 
1516 	switch (addr->type) {
1517 	case PF_ADDR_TABLE:
1518 		addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
1519 		if (addr->p.tbl == NULL)
1520 			error = ENOMEM;
1521 		break;
1522 	case PF_ADDR_DYNIFTL:
1523 		error = pfi_dynaddr_setup(addr, af);
1524 		break;
1525 	}
1526 
1527 	return (error);
1528 }
1529 
1530 static void
1531 pf_addr_copyout(struct pf_addr_wrap *addr)
1532 {
1533 
1534 	switch (addr->type) {
1535 	case PF_ADDR_DYNIFTL:
1536 		pfi_dynaddr_copyout(addr);
1537 		break;
1538 	case PF_ADDR_TABLE:
1539 		pf_tbladdr_copyout(addr);
1540 		break;
1541 	}
1542 }
1543 
1544 static void
1545 pf_src_node_copy(const struct pf_ksrc_node *in, struct pf_src_node *out)
1546 {
1547 	int	secs = time_uptime, diff;
1548 
1549 	bzero(out, sizeof(struct pf_src_node));
1550 
1551 	bcopy(&in->addr, &out->addr, sizeof(struct pf_addr));
1552 	bcopy(&in->raddr, &out->raddr, sizeof(struct pf_addr));
1553 
1554 	if (in->rule.ptr != NULL)
1555 		out->rule.nr = in->rule.ptr->nr;
1556 
1557 	for (int i = 0; i < 2; i++) {
1558 		out->bytes[i] = counter_u64_fetch(in->bytes[i]);
1559 		out->packets[i] = counter_u64_fetch(in->packets[i]);
1560 	}
1561 
1562 	out->states = in->states;
1563 	out->conn = in->conn;
1564 	out->af = in->af;
1565 	out->ruletype = in->ruletype;
1566 
1567 	out->creation = secs - in->creation;
1568 	if (out->expire > secs)
1569 		out->expire -= secs;
1570 	else
1571 		out->expire = 0;
1572 
1573 	/* Adjust the connection rate estimate. */
1574 	diff = secs - in->conn_rate.last;
1575 	if (diff >= in->conn_rate.seconds)
1576 		out->conn_rate.count = 0;
1577 	else
1578 		out->conn_rate.count -=
1579 		    in->conn_rate.count * diff /
1580 		    in->conn_rate.seconds;
1581 }
1582 
1583 #ifdef ALTQ
1584 /*
1585  * Handle export of struct pf_kaltq to user binaries that may be using any
1586  * version of struct pf_altq.
1587  */
1588 static int
1589 pf_export_kaltq(struct pf_altq *q, struct pfioc_altq_v1 *pa, size_t ioc_size)
1590 {
1591 	u_int32_t version;
1592 
1593 	if (ioc_size == sizeof(struct pfioc_altq_v0))
1594 		version = 0;
1595 	else
1596 		version = pa->version;
1597 
1598 	if (version > PFIOC_ALTQ_VERSION)
1599 		return (EINVAL);
1600 
1601 #define ASSIGN(x) exported_q->x = q->x
1602 #define COPY(x) \
1603 	bcopy(&q->x, &exported_q->x, min(sizeof(q->x), sizeof(exported_q->x)))
1604 #define SATU16(x) (u_int32_t)uqmin((x), USHRT_MAX)
1605 #define SATU32(x) (u_int32_t)uqmin((x), UINT_MAX)
1606 
1607 	switch (version) {
1608 	case 0: {
1609 		struct pf_altq_v0 *exported_q =
1610 		    &((struct pfioc_altq_v0 *)pa)->altq;
1611 
1612 		COPY(ifname);
1613 
1614 		ASSIGN(scheduler);
1615 		ASSIGN(tbrsize);
1616 		exported_q->tbrsize = SATU16(q->tbrsize);
1617 		exported_q->ifbandwidth = SATU32(q->ifbandwidth);
1618 
1619 		COPY(qname);
1620 		COPY(parent);
1621 		ASSIGN(parent_qid);
1622 		exported_q->bandwidth = SATU32(q->bandwidth);
1623 		ASSIGN(priority);
1624 		ASSIGN(local_flags);
1625 
1626 		ASSIGN(qlimit);
1627 		ASSIGN(flags);
1628 
1629 		if (q->scheduler == ALTQT_HFSC) {
1630 #define ASSIGN_OPT(x) exported_q->pq_u.hfsc_opts.x = q->pq_u.hfsc_opts.x
1631 #define ASSIGN_OPT_SATU32(x) exported_q->pq_u.hfsc_opts.x = \
1632 			    SATU32(q->pq_u.hfsc_opts.x)
1633 
1634 			ASSIGN_OPT_SATU32(rtsc_m1);
1635 			ASSIGN_OPT(rtsc_d);
1636 			ASSIGN_OPT_SATU32(rtsc_m2);
1637 
1638 			ASSIGN_OPT_SATU32(lssc_m1);
1639 			ASSIGN_OPT(lssc_d);
1640 			ASSIGN_OPT_SATU32(lssc_m2);
1641 
1642 			ASSIGN_OPT_SATU32(ulsc_m1);
1643 			ASSIGN_OPT(ulsc_d);
1644 			ASSIGN_OPT_SATU32(ulsc_m2);
1645 
1646 			ASSIGN_OPT(flags);
1647 
1648 #undef ASSIGN_OPT
1649 #undef ASSIGN_OPT_SATU32
1650 		} else
1651 			COPY(pq_u);
1652 
1653 		ASSIGN(qid);
1654 		break;
1655 	}
1656 	case 1:	{
1657 		struct pf_altq_v1 *exported_q =
1658 		    &((struct pfioc_altq_v1 *)pa)->altq;
1659 
1660 		COPY(ifname);
1661 
1662 		ASSIGN(scheduler);
1663 		ASSIGN(tbrsize);
1664 		ASSIGN(ifbandwidth);
1665 
1666 		COPY(qname);
1667 		COPY(parent);
1668 		ASSIGN(parent_qid);
1669 		ASSIGN(bandwidth);
1670 		ASSIGN(priority);
1671 		ASSIGN(local_flags);
1672 
1673 		ASSIGN(qlimit);
1674 		ASSIGN(flags);
1675 		COPY(pq_u);
1676 
1677 		ASSIGN(qid);
1678 		break;
1679 	}
1680 	default:
1681 		panic("%s: unhandled struct pfioc_altq version", __func__);
1682 		break;
1683 	}
1684 
1685 #undef ASSIGN
1686 #undef COPY
1687 #undef SATU16
1688 #undef SATU32
1689 
1690 	return (0);
1691 }
1692 
1693 /*
1694  * Handle import to struct pf_kaltq of struct pf_altq from user binaries
1695  * that may be using any version of it.
1696  */
1697 static int
1698 pf_import_kaltq(struct pfioc_altq_v1 *pa, struct pf_altq *q, size_t ioc_size)
1699 {
1700 	u_int32_t version;
1701 
1702 	if (ioc_size == sizeof(struct pfioc_altq_v0))
1703 		version = 0;
1704 	else
1705 		version = pa->version;
1706 
1707 	if (version > PFIOC_ALTQ_VERSION)
1708 		return (EINVAL);
1709 
1710 #define ASSIGN(x) q->x = imported_q->x
1711 #define COPY(x) \
1712 	bcopy(&imported_q->x, &q->x, min(sizeof(imported_q->x), sizeof(q->x)))
1713 
1714 	switch (version) {
1715 	case 0: {
1716 		struct pf_altq_v0 *imported_q =
1717 		    &((struct pfioc_altq_v0 *)pa)->altq;
1718 
1719 		COPY(ifname);
1720 
1721 		ASSIGN(scheduler);
1722 		ASSIGN(tbrsize); /* 16-bit -> 32-bit */
1723 		ASSIGN(ifbandwidth); /* 32-bit -> 64-bit */
1724 
1725 		COPY(qname);
1726 		COPY(parent);
1727 		ASSIGN(parent_qid);
1728 		ASSIGN(bandwidth); /* 32-bit -> 64-bit */
1729 		ASSIGN(priority);
1730 		ASSIGN(local_flags);
1731 
1732 		ASSIGN(qlimit);
1733 		ASSIGN(flags);
1734 
1735 		if (imported_q->scheduler == ALTQT_HFSC) {
1736 #define ASSIGN_OPT(x) q->pq_u.hfsc_opts.x = imported_q->pq_u.hfsc_opts.x
1737 
1738 			/*
1739 			 * The m1 and m2 parameters are being copied from
1740 			 * 32-bit to 64-bit.
1741 			 */
1742 			ASSIGN_OPT(rtsc_m1);
1743 			ASSIGN_OPT(rtsc_d);
1744 			ASSIGN_OPT(rtsc_m2);
1745 
1746 			ASSIGN_OPT(lssc_m1);
1747 			ASSIGN_OPT(lssc_d);
1748 			ASSIGN_OPT(lssc_m2);
1749 
1750 			ASSIGN_OPT(ulsc_m1);
1751 			ASSIGN_OPT(ulsc_d);
1752 			ASSIGN_OPT(ulsc_m2);
1753 
1754 			ASSIGN_OPT(flags);
1755 
1756 #undef ASSIGN_OPT
1757 		} else
1758 			COPY(pq_u);
1759 
1760 		ASSIGN(qid);
1761 		break;
1762 	}
1763 	case 1: {
1764 		struct pf_altq_v1 *imported_q =
1765 		    &((struct pfioc_altq_v1 *)pa)->altq;
1766 
1767 		COPY(ifname);
1768 
1769 		ASSIGN(scheduler);
1770 		ASSIGN(tbrsize);
1771 		ASSIGN(ifbandwidth);
1772 
1773 		COPY(qname);
1774 		COPY(parent);
1775 		ASSIGN(parent_qid);
1776 		ASSIGN(bandwidth);
1777 		ASSIGN(priority);
1778 		ASSIGN(local_flags);
1779 
1780 		ASSIGN(qlimit);
1781 		ASSIGN(flags);
1782 		COPY(pq_u);
1783 
1784 		ASSIGN(qid);
1785 		break;
1786 	}
1787 	default:
1788 		panic("%s: unhandled struct pfioc_altq version", __func__);
1789 		break;
1790 	}
1791 
1792 #undef ASSIGN
1793 #undef COPY
1794 
1795 	return (0);
1796 }
1797 
1798 static struct pf_altq *
1799 pf_altq_get_nth_active(u_int32_t n)
1800 {
1801 	struct pf_altq		*altq;
1802 	u_int32_t		 nr;
1803 
1804 	nr = 0;
1805 	TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
1806 		if (nr == n)
1807 			return (altq);
1808 		nr++;
1809 	}
1810 
1811 	TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
1812 		if (nr == n)
1813 			return (altq);
1814 		nr++;
1815 	}
1816 
1817 	return (NULL);
1818 }
1819 #endif /* ALTQ */
1820 
1821 struct pf_krule *
1822 pf_krule_alloc(void)
1823 {
1824 	struct pf_krule *rule;
1825 
1826 	rule = malloc(sizeof(struct pf_krule), M_PFRULE, M_WAITOK | M_ZERO);
1827 	mtx_init(&rule->rpool.mtx, "pf_krule_pool", NULL, MTX_DEF);
1828 	rule->timestamp = uma_zalloc_pcpu(pf_timestamp_pcpu_zone,
1829 	    M_WAITOK | M_ZERO);
1830 	return (rule);
1831 }
1832 
1833 void
1834 pf_krule_free(struct pf_krule *rule)
1835 {
1836 #ifdef PF_WANT_32_TO_64_COUNTER
1837 	bool wowned;
1838 #endif
1839 
1840 	if (rule == NULL)
1841 		return;
1842 
1843 #ifdef PF_WANT_32_TO_64_COUNTER
1844 	if (rule->allrulelinked) {
1845 		wowned = PF_RULES_WOWNED();
1846 		if (!wowned)
1847 			PF_RULES_WLOCK();
1848 		LIST_REMOVE(rule, allrulelist);
1849 		V_pf_allrulecount--;
1850 		if (!wowned)
1851 			PF_RULES_WUNLOCK();
1852 	}
1853 #endif
1854 
1855 	pf_counter_u64_deinit(&rule->evaluations);
1856 	for (int i = 0; i < 2; i++) {
1857 		pf_counter_u64_deinit(&rule->packets[i]);
1858 		pf_counter_u64_deinit(&rule->bytes[i]);
1859 	}
1860 	counter_u64_free(rule->states_cur);
1861 	counter_u64_free(rule->states_tot);
1862 	counter_u64_free(rule->src_nodes);
1863 	uma_zfree_pcpu(pf_timestamp_pcpu_zone, rule->timestamp);
1864 
1865 	mtx_destroy(&rule->rpool.mtx);
1866 	free(rule, M_PFRULE);
1867 }
1868 
1869 static void
1870 pf_kpooladdr_to_pooladdr(const struct pf_kpooladdr *kpool,
1871     struct pf_pooladdr *pool)
1872 {
1873 
1874 	bzero(pool, sizeof(*pool));
1875 	bcopy(&kpool->addr, &pool->addr, sizeof(pool->addr));
1876 	strlcpy(pool->ifname, kpool->ifname, sizeof(pool->ifname));
1877 }
1878 
1879 static int
1880 pf_pooladdr_to_kpooladdr(const struct pf_pooladdr *pool,
1881     struct pf_kpooladdr *kpool)
1882 {
1883 	int ret;
1884 
1885 	bzero(kpool, sizeof(*kpool));
1886 	bcopy(&pool->addr, &kpool->addr, sizeof(kpool->addr));
1887 	ret = pf_user_strcpy(kpool->ifname, pool->ifname,
1888 	    sizeof(kpool->ifname));
1889 	return (ret);
1890 }
1891 
1892 static void
1893 pf_pool_to_kpool(const struct pf_pool *pool, struct pf_kpool *kpool)
1894 {
1895 	_Static_assert(sizeof(pool->key) == sizeof(kpool->key), "");
1896 	_Static_assert(sizeof(pool->counter) == sizeof(kpool->counter), "");
1897 
1898 	bcopy(&pool->key, &kpool->key, sizeof(kpool->key));
1899 	bcopy(&pool->counter, &kpool->counter, sizeof(kpool->counter));
1900 
1901 	kpool->tblidx = pool->tblidx;
1902 	kpool->proxy_port[0] = pool->proxy_port[0];
1903 	kpool->proxy_port[1] = pool->proxy_port[1];
1904 	kpool->opts = pool->opts;
1905 }
1906 
1907 static int
1908 pf_rule_to_krule(const struct pf_rule *rule, struct pf_krule *krule)
1909 {
1910 	int ret;
1911 
1912 #ifndef INET
1913 	if (rule->af == AF_INET) {
1914 		return (EAFNOSUPPORT);
1915 	}
1916 #endif /* INET */
1917 #ifndef INET6
1918 	if (rule->af == AF_INET6) {
1919 		return (EAFNOSUPPORT);
1920 	}
1921 #endif /* INET6 */
1922 
1923 	ret = pf_check_rule_addr(&rule->src);
1924 	if (ret != 0)
1925 		return (ret);
1926 	ret = pf_check_rule_addr(&rule->dst);
1927 	if (ret != 0)
1928 		return (ret);
1929 
1930 	bcopy(&rule->src, &krule->src, sizeof(rule->src));
1931 	bcopy(&rule->dst, &krule->dst, sizeof(rule->dst));
1932 
1933 	ret = pf_user_strcpy(krule->label[0], rule->label, sizeof(rule->label));
1934 	if (ret != 0)
1935 		return (ret);
1936 	ret = pf_user_strcpy(krule->ifname, rule->ifname, sizeof(rule->ifname));
1937 	if (ret != 0)
1938 		return (ret);
1939 	ret = pf_user_strcpy(krule->qname, rule->qname, sizeof(rule->qname));
1940 	if (ret != 0)
1941 		return (ret);
1942 	ret = pf_user_strcpy(krule->pqname, rule->pqname, sizeof(rule->pqname));
1943 	if (ret != 0)
1944 		return (ret);
1945 	ret = pf_user_strcpy(krule->tagname, rule->tagname,
1946 	    sizeof(rule->tagname));
1947 	if (ret != 0)
1948 		return (ret);
1949 	ret = pf_user_strcpy(krule->match_tagname, rule->match_tagname,
1950 	    sizeof(rule->match_tagname));
1951 	if (ret != 0)
1952 		return (ret);
1953 	ret = pf_user_strcpy(krule->overload_tblname, rule->overload_tblname,
1954 	    sizeof(rule->overload_tblname));
1955 	if (ret != 0)
1956 		return (ret);
1957 
1958 	pf_pool_to_kpool(&rule->rpool, &krule->rpool);
1959 
1960 	/* Don't allow userspace to set evaluations, packets or bytes. */
1961 	/* kif, anchor, overload_tbl are not copied over. */
1962 
1963 	krule->os_fingerprint = rule->os_fingerprint;
1964 
1965 	krule->rtableid = rule->rtableid;
1966 	bcopy(rule->timeout, krule->timeout, sizeof(krule->timeout));
1967 	krule->max_states = rule->max_states;
1968 	krule->max_src_nodes = rule->max_src_nodes;
1969 	krule->max_src_states = rule->max_src_states;
1970 	krule->max_src_conn = rule->max_src_conn;
1971 	krule->max_src_conn_rate.limit = rule->max_src_conn_rate.limit;
1972 	krule->max_src_conn_rate.seconds = rule->max_src_conn_rate.seconds;
1973 	krule->qid = rule->qid;
1974 	krule->pqid = rule->pqid;
1975 	krule->nr = rule->nr;
1976 	krule->prob = rule->prob;
1977 	krule->cuid = rule->cuid;
1978 	krule->cpid = rule->cpid;
1979 
1980 	krule->return_icmp = rule->return_icmp;
1981 	krule->return_icmp6 = rule->return_icmp6;
1982 	krule->max_mss = rule->max_mss;
1983 	krule->tag = rule->tag;
1984 	krule->match_tag = rule->match_tag;
1985 	krule->scrub_flags = rule->scrub_flags;
1986 
1987 	bcopy(&rule->uid, &krule->uid, sizeof(krule->uid));
1988 	bcopy(&rule->gid, &krule->gid, sizeof(krule->gid));
1989 
1990 	krule->rule_flag = rule->rule_flag;
1991 	krule->action = rule->action;
1992 	krule->direction = rule->direction;
1993 	krule->log = rule->log;
1994 	krule->logif = rule->logif;
1995 	krule->quick = rule->quick;
1996 	krule->ifnot = rule->ifnot;
1997 	krule->match_tag_not = rule->match_tag_not;
1998 	krule->natpass = rule->natpass;
1999 
2000 	krule->keep_state = rule->keep_state;
2001 	krule->af = rule->af;
2002 	krule->proto = rule->proto;
2003 	krule->type = rule->type;
2004 	krule->code = rule->code;
2005 	krule->flags = rule->flags;
2006 	krule->flagset = rule->flagset;
2007 	krule->min_ttl = rule->min_ttl;
2008 	krule->allow_opts = rule->allow_opts;
2009 	krule->rt = rule->rt;
2010 	krule->return_ttl = rule->return_ttl;
2011 	krule->tos = rule->tos;
2012 	krule->set_tos = rule->set_tos;
2013 
2014 	krule->flush = rule->flush;
2015 	krule->prio = rule->prio;
2016 	krule->set_prio[0] = rule->set_prio[0];
2017 	krule->set_prio[1] = rule->set_prio[1];
2018 
2019 	bcopy(&rule->divert, &krule->divert, sizeof(krule->divert));
2020 
2021 	return (0);
2022 }
2023 
2024 int
2025 pf_ioctl_addrule(struct pf_krule *rule, uint32_t ticket,
2026     uint32_t pool_ticket, const char *anchor, const char *anchor_call,
2027     uid_t uid, pid_t pid)
2028 {
2029 	struct pf_kruleset	*ruleset;
2030 	struct pf_krule		*tail;
2031 	struct pf_kpooladdr	*pa;
2032 	struct pfi_kkif		*kif = NULL;
2033 	int			 rs_num;
2034 	int			 error = 0;
2035 
2036 	if ((rule->return_icmp >> 8) > ICMP_MAXTYPE) {
2037 		error = EINVAL;
2038 		goto errout_unlocked;
2039 	}
2040 
2041 #define	ERROUT(x)	ERROUT_FUNCTION(errout, x)
2042 
2043 	if (rule->ifname[0])
2044 		kif = pf_kkif_create(M_WAITOK);
2045 	pf_counter_u64_init(&rule->evaluations, M_WAITOK);
2046 	for (int i = 0; i < 2; i++) {
2047 		pf_counter_u64_init(&rule->packets[i], M_WAITOK);
2048 		pf_counter_u64_init(&rule->bytes[i], M_WAITOK);
2049 	}
2050 	rule->states_cur = counter_u64_alloc(M_WAITOK);
2051 	rule->states_tot = counter_u64_alloc(M_WAITOK);
2052 	rule->src_nodes = counter_u64_alloc(M_WAITOK);
2053 	rule->cuid = uid;
2054 	rule->cpid = pid;
2055 	TAILQ_INIT(&rule->rpool.list);
2056 
2057 	PF_CONFIG_LOCK();
2058 	PF_RULES_WLOCK();
2059 #ifdef PF_WANT_32_TO_64_COUNTER
2060 	LIST_INSERT_HEAD(&V_pf_allrulelist, rule, allrulelist);
2061 	MPASS(!rule->allrulelinked);
2062 	rule->allrulelinked = true;
2063 	V_pf_allrulecount++;
2064 #endif
2065 	ruleset = pf_find_kruleset(anchor);
2066 	if (ruleset == NULL)
2067 		ERROUT(EINVAL);
2068 	rs_num = pf_get_ruleset_number(rule->action);
2069 	if (rs_num >= PF_RULESET_MAX)
2070 		ERROUT(EINVAL);
2071 	if (ticket != ruleset->rules[rs_num].inactive.ticket) {
2072 		DPFPRINTF(PF_DEBUG_MISC,
2073 		    ("ticket: %d != [%d]%d\n", ticket, rs_num,
2074 		    ruleset->rules[rs_num].inactive.ticket));
2075 		ERROUT(EBUSY);
2076 	}
2077 	if (pool_ticket != V_ticket_pabuf) {
2078 		DPFPRINTF(PF_DEBUG_MISC,
2079 		    ("pool_ticket: %d != %d\n", pool_ticket,
2080 		    V_ticket_pabuf));
2081 		ERROUT(EBUSY);
2082 	}
2083 	/*
2084 	 * XXXMJG hack: there is no mechanism to ensure they started the
2085 	 * transaction. Ticket checked above may happen to match by accident,
2086 	 * even if nobody called DIOCXBEGIN, let alone this process.
2087 	 * Partially work around it by checking if the RB tree got allocated,
2088 	 * see pf_begin_rules.
2089 	 */
2090 	if (ruleset->rules[rs_num].inactive.tree == NULL) {
2091 		ERROUT(EINVAL);
2092 	}
2093 
2094 	tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
2095 	    pf_krulequeue);
2096 	if (tail)
2097 		rule->nr = tail->nr + 1;
2098 	else
2099 		rule->nr = 0;
2100 	if (rule->ifname[0]) {
2101 		rule->kif = pfi_kkif_attach(kif, rule->ifname);
2102 		kif = NULL;
2103 		pfi_kkif_ref(rule->kif);
2104 	} else
2105 		rule->kif = NULL;
2106 
2107 	if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
2108 		error = EBUSY;
2109 
2110 #ifdef ALTQ
2111 	/* set queue IDs */
2112 	if (rule->qname[0] != 0) {
2113 		if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
2114 			error = EBUSY;
2115 		else if (rule->pqname[0] != 0) {
2116 			if ((rule->pqid =
2117 			    pf_qname2qid(rule->pqname)) == 0)
2118 				error = EBUSY;
2119 		} else
2120 			rule->pqid = rule->qid;
2121 	}
2122 #endif
2123 	if (rule->tagname[0])
2124 		if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
2125 			error = EBUSY;
2126 	if (rule->match_tagname[0])
2127 		if ((rule->match_tag =
2128 		    pf_tagname2tag(rule->match_tagname)) == 0)
2129 			error = EBUSY;
2130 	if (rule->rt && !rule->direction)
2131 		error = EINVAL;
2132 	if (!rule->log)
2133 		rule->logif = 0;
2134 	if (rule->logif >= PFLOGIFS_MAX)
2135 		error = EINVAL;
2136 	if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
2137 		error = ENOMEM;
2138 	if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
2139 		error = ENOMEM;
2140 	if (pf_kanchor_setup(rule, ruleset, anchor_call))
2141 		error = EINVAL;
2142 	if (rule->scrub_flags & PFSTATE_SETPRIO &&
2143 	    (rule->set_prio[0] > PF_PRIO_MAX ||
2144 	    rule->set_prio[1] > PF_PRIO_MAX))
2145 		error = EINVAL;
2146 	TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
2147 		if (pa->addr.type == PF_ADDR_TABLE) {
2148 			pa->addr.p.tbl = pfr_attach_table(ruleset,
2149 			    pa->addr.v.tblname);
2150 			if (pa->addr.p.tbl == NULL)
2151 				error = ENOMEM;
2152 		}
2153 
2154 	rule->overload_tbl = NULL;
2155 	if (rule->overload_tblname[0]) {
2156 		if ((rule->overload_tbl = pfr_attach_table(ruleset,
2157 		    rule->overload_tblname)) == NULL)
2158 			error = EINVAL;
2159 		else
2160 			rule->overload_tbl->pfrkt_flags |=
2161 			    PFR_TFLAG_ACTIVE;
2162 	}
2163 
2164 	pf_mv_kpool(&V_pf_pabuf, &rule->rpool.list);
2165 	if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
2166 	    (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
2167 	    (rule->rt > PF_NOPFROUTE)) &&
2168 	    (TAILQ_FIRST(&rule->rpool.list) == NULL))
2169 		error = EINVAL;
2170 
2171 	if (error) {
2172 		pf_free_rule(rule);
2173 		rule = NULL;
2174 		ERROUT(error);
2175 	}
2176 
2177 	rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
2178 	TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
2179 	    rule, entries);
2180 	ruleset->rules[rs_num].inactive.rcount++;
2181 
2182 	PF_RULES_WUNLOCK();
2183 	pf_hash_rule(rule);
2184 	if (RB_INSERT(pf_krule_global, ruleset->rules[rs_num].inactive.tree, rule) != NULL) {
2185 		PF_RULES_WLOCK();
2186 		TAILQ_REMOVE(ruleset->rules[rs_num].inactive.ptr, rule, entries);
2187 		ruleset->rules[rs_num].inactive.rcount--;
2188 		pf_free_rule(rule);
2189 		rule = NULL;
2190 		ERROUT(EEXIST);
2191 	}
2192 	PF_CONFIG_UNLOCK();
2193 
2194 	return (0);
2195 
2196 #undef ERROUT
2197 errout:
2198 	PF_RULES_WUNLOCK();
2199 	PF_CONFIG_UNLOCK();
2200 errout_unlocked:
2201 	pf_kkif_free(kif);
2202 	pf_krule_free(rule);
2203 	return (error);
2204 }
2205 
2206 static bool
2207 pf_label_match(const struct pf_krule *rule, const char *label)
2208 {
2209 	int i = 0;
2210 
2211 	while (*rule->label[i]) {
2212 		if (strcmp(rule->label[i], label) == 0)
2213 			return (true);
2214 		i++;
2215 	}
2216 
2217 	return (false);
2218 }
2219 
2220 static unsigned int
2221 pf_kill_matching_state(struct pf_state_key_cmp *key, int dir)
2222 {
2223 	struct pf_kstate *s;
2224 	int more = 0;
2225 
2226 	s = pf_find_state_all(key, dir, &more);
2227 	if (s == NULL)
2228 		return (0);
2229 
2230 	if (more) {
2231 		PF_STATE_UNLOCK(s);
2232 		return (0);
2233 	}
2234 
2235 	pf_unlink_state(s);
2236 	return (1);
2237 }
2238 
2239 static int
2240 pf_killstates_row(struct pf_kstate_kill *psk, struct pf_idhash *ih)
2241 {
2242 	struct pf_kstate	*s;
2243 	struct pf_state_key	*sk;
2244 	struct pf_addr		*srcaddr, *dstaddr;
2245 	struct pf_state_key_cmp	 match_key;
2246 	int			 idx, killed = 0;
2247 	unsigned int		 dir;
2248 	u_int16_t		 srcport, dstport;
2249 	struct pfi_kkif		*kif;
2250 
2251 relock_DIOCKILLSTATES:
2252 	PF_HASHROW_LOCK(ih);
2253 	LIST_FOREACH(s, &ih->states, entry) {
2254 		/* For floating states look at the original kif. */
2255 		kif = s->kif == V_pfi_all ? s->orig_kif : s->kif;
2256 
2257 		sk = s->key[psk->psk_nat ? PF_SK_STACK : PF_SK_WIRE];
2258 		if (s->direction == PF_OUT) {
2259 			srcaddr = &sk->addr[1];
2260 			dstaddr = &sk->addr[0];
2261 			srcport = sk->port[1];
2262 			dstport = sk->port[0];
2263 		} else {
2264 			srcaddr = &sk->addr[0];
2265 			dstaddr = &sk->addr[1];
2266 			srcport = sk->port[0];
2267 			dstport = sk->port[1];
2268 		}
2269 
2270 		if (psk->psk_af && sk->af != psk->psk_af)
2271 			continue;
2272 
2273 		if (psk->psk_proto && psk->psk_proto != sk->proto)
2274 			continue;
2275 
2276 		if (! PF_MATCHA(psk->psk_src.neg, &psk->psk_src.addr.v.a.addr,
2277 		    &psk->psk_src.addr.v.a.mask, srcaddr, sk->af))
2278 			continue;
2279 
2280 		if (! PF_MATCHA(psk->psk_dst.neg, &psk->psk_dst.addr.v.a.addr,
2281 		    &psk->psk_dst.addr.v.a.mask, dstaddr, sk->af))
2282 			continue;
2283 
2284 		if (!  PF_MATCHA(psk->psk_rt_addr.neg,
2285 		    &psk->psk_rt_addr.addr.v.a.addr,
2286 		    &psk->psk_rt_addr.addr.v.a.mask,
2287 		    &s->rt_addr, sk->af))
2288 			continue;
2289 
2290 		if (psk->psk_src.port_op != 0 &&
2291 		    ! pf_match_port(psk->psk_src.port_op,
2292 		    psk->psk_src.port[0], psk->psk_src.port[1], srcport))
2293 			continue;
2294 
2295 		if (psk->psk_dst.port_op != 0 &&
2296 		    ! pf_match_port(psk->psk_dst.port_op,
2297 		    psk->psk_dst.port[0], psk->psk_dst.port[1], dstport))
2298 			continue;
2299 
2300 		if (psk->psk_label[0] &&
2301 		    ! pf_label_match(s->rule.ptr, psk->psk_label))
2302 			continue;
2303 
2304 		if (psk->psk_ifname[0] && strcmp(psk->psk_ifname,
2305 		    kif->pfik_name))
2306 			continue;
2307 
2308 		if (psk->psk_kill_match) {
2309 			/* Create the key to find matching states, with lock
2310 			 * held. */
2311 
2312 			bzero(&match_key, sizeof(match_key));
2313 
2314 			if (s->direction == PF_OUT) {
2315 				dir = PF_IN;
2316 				idx = psk->psk_nat ? PF_SK_WIRE : PF_SK_STACK;
2317 			} else {
2318 				dir = PF_OUT;
2319 				idx = psk->psk_nat ? PF_SK_STACK : PF_SK_WIRE;
2320 			}
2321 
2322 			match_key.af = s->key[idx]->af;
2323 			match_key.proto = s->key[idx]->proto;
2324 			PF_ACPY(&match_key.addr[0],
2325 			    &s->key[idx]->addr[1], match_key.af);
2326 			match_key.port[0] = s->key[idx]->port[1];
2327 			PF_ACPY(&match_key.addr[1],
2328 			    &s->key[idx]->addr[0], match_key.af);
2329 			match_key.port[1] = s->key[idx]->port[0];
2330 		}
2331 
2332 		pf_unlink_state(s);
2333 		killed++;
2334 
2335 		if (psk->psk_kill_match)
2336 			killed += pf_kill_matching_state(&match_key, dir);
2337 
2338 		goto relock_DIOCKILLSTATES;
2339 	}
2340 	PF_HASHROW_UNLOCK(ih);
2341 
2342 	return (killed);
2343 }
2344 
2345 int
2346 pf_start(void)
2347 {
2348 	int error = 0;
2349 
2350 	sx_xlock(&V_pf_ioctl_lock);
2351 	if (V_pf_status.running)
2352 		error = EEXIST;
2353 	else {
2354 		hook_pf();
2355 		if (! TAILQ_EMPTY(V_pf_keth->active.rules))
2356 			hook_pf_eth();
2357 		V_pf_status.running = 1;
2358 		V_pf_status.since = time_second;
2359 		new_unrhdr64(&V_pf_stateid, time_second);
2360 
2361 		DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
2362 	}
2363 	sx_xunlock(&V_pf_ioctl_lock);
2364 
2365 	return (error);
2366 }
2367 
2368 int
2369 pf_stop(void)
2370 {
2371 	int error = 0;
2372 
2373 	sx_xlock(&V_pf_ioctl_lock);
2374 	if (!V_pf_status.running)
2375 		error = ENOENT;
2376 	else {
2377 		V_pf_status.running = 0;
2378 		dehook_pf();
2379 		dehook_pf_eth();
2380 		V_pf_status.since = time_second;
2381 		DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
2382 	}
2383 	sx_xunlock(&V_pf_ioctl_lock);
2384 
2385 	return (error);
2386 }
2387 
2388 static int
2389 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
2390 {
2391 	int			 error = 0;
2392 	PF_RULES_RLOCK_TRACKER;
2393 
2394 #define	ERROUT_IOCTL(target, x)					\
2395     do {								\
2396 	    error = (x);						\
2397 	    SDT_PROBE3(pf, ioctl, ioctl, error, cmd, error, __LINE__);	\
2398 	    goto target;						\
2399     } while (0)
2400 
2401 
2402 	/* XXX keep in sync with switch() below */
2403 	if (securelevel_gt(td->td_ucred, 2))
2404 		switch (cmd) {
2405 		case DIOCGETRULES:
2406 		case DIOCGETRULENV:
2407 		case DIOCGETADDRS:
2408 		case DIOCGETADDR:
2409 		case DIOCGETSTATE:
2410 		case DIOCGETSTATENV:
2411 		case DIOCSETSTATUSIF:
2412 		case DIOCGETSTATUSNV:
2413 		case DIOCCLRSTATUS:
2414 		case DIOCNATLOOK:
2415 		case DIOCSETDEBUG:
2416 #ifdef COMPAT_FREEBSD14
2417 		case DIOCGETSTATES:
2418 		case DIOCGETSTATESV2:
2419 #endif
2420 		case DIOCGETTIMEOUT:
2421 		case DIOCCLRRULECTRS:
2422 		case DIOCGETLIMIT:
2423 		case DIOCGETALTQSV0:
2424 		case DIOCGETALTQSV1:
2425 		case DIOCGETALTQV0:
2426 		case DIOCGETALTQV1:
2427 		case DIOCGETQSTATSV0:
2428 		case DIOCGETQSTATSV1:
2429 		case DIOCGETRULESETS:
2430 		case DIOCGETRULESET:
2431 		case DIOCRGETTABLES:
2432 		case DIOCRGETTSTATS:
2433 		case DIOCRCLRTSTATS:
2434 		case DIOCRCLRADDRS:
2435 		case DIOCRADDADDRS:
2436 		case DIOCRDELADDRS:
2437 		case DIOCRSETADDRS:
2438 		case DIOCRGETADDRS:
2439 		case DIOCRGETASTATS:
2440 		case DIOCRCLRASTATS:
2441 		case DIOCRTSTADDRS:
2442 		case DIOCOSFPGET:
2443 		case DIOCGETSRCNODES:
2444 		case DIOCCLRSRCNODES:
2445 		case DIOCGETSYNCOOKIES:
2446 		case DIOCIGETIFACES:
2447 		case DIOCGIFSPEEDV0:
2448 		case DIOCGIFSPEEDV1:
2449 		case DIOCSETIFFLAG:
2450 		case DIOCCLRIFFLAG:
2451 		case DIOCGETETHRULES:
2452 		case DIOCGETETHRULE:
2453 		case DIOCGETETHRULESETS:
2454 		case DIOCGETETHRULESET:
2455 			break;
2456 		case DIOCRCLRTABLES:
2457 		case DIOCRADDTABLES:
2458 		case DIOCRDELTABLES:
2459 		case DIOCRSETTFLAGS:
2460 			if (((struct pfioc_table *)addr)->pfrio_flags &
2461 			    PFR_FLAG_DUMMY)
2462 				break; /* dummy operation ok */
2463 			return (EPERM);
2464 		default:
2465 			return (EPERM);
2466 		}
2467 
2468 	if (!(flags & FWRITE))
2469 		switch (cmd) {
2470 		case DIOCGETRULES:
2471 		case DIOCGETADDRS:
2472 		case DIOCGETADDR:
2473 		case DIOCGETSTATE:
2474 		case DIOCGETSTATENV:
2475 		case DIOCGETSTATUSNV:
2476 #ifdef COMPAT_FREEBSD14
2477 		case DIOCGETSTATES:
2478 		case DIOCGETSTATESV2:
2479 #endif
2480 		case DIOCGETTIMEOUT:
2481 		case DIOCGETLIMIT:
2482 		case DIOCGETALTQSV0:
2483 		case DIOCGETALTQSV1:
2484 		case DIOCGETALTQV0:
2485 		case DIOCGETALTQV1:
2486 		case DIOCGETQSTATSV0:
2487 		case DIOCGETQSTATSV1:
2488 		case DIOCGETRULESETS:
2489 		case DIOCGETRULESET:
2490 		case DIOCNATLOOK:
2491 		case DIOCRGETTABLES:
2492 		case DIOCRGETTSTATS:
2493 		case DIOCRGETADDRS:
2494 		case DIOCRGETASTATS:
2495 		case DIOCRTSTADDRS:
2496 		case DIOCOSFPGET:
2497 		case DIOCGETSRCNODES:
2498 		case DIOCGETSYNCOOKIES:
2499 		case DIOCIGETIFACES:
2500 		case DIOCGIFSPEEDV1:
2501 		case DIOCGIFSPEEDV0:
2502 		case DIOCGETRULENV:
2503 		case DIOCGETETHRULES:
2504 		case DIOCGETETHRULE:
2505 		case DIOCGETETHRULESETS:
2506 		case DIOCGETETHRULESET:
2507 			break;
2508 		case DIOCRCLRTABLES:
2509 		case DIOCRADDTABLES:
2510 		case DIOCRDELTABLES:
2511 		case DIOCRCLRTSTATS:
2512 		case DIOCRCLRADDRS:
2513 		case DIOCRADDADDRS:
2514 		case DIOCRDELADDRS:
2515 		case DIOCRSETADDRS:
2516 		case DIOCRSETTFLAGS:
2517 			if (((struct pfioc_table *)addr)->pfrio_flags &
2518 			    PFR_FLAG_DUMMY) {
2519 				flags |= FWRITE; /* need write lock for dummy */
2520 				break; /* dummy operation ok */
2521 			}
2522 			return (EACCES);
2523 		default:
2524 			return (EACCES);
2525 		}
2526 
2527 	CURVNET_SET(TD_TO_VNET(td));
2528 
2529 	switch (cmd) {
2530 #ifdef COMPAT_FREEBSD14
2531 	case DIOCSTART:
2532 		error = pf_start();
2533 		break;
2534 
2535 	case DIOCSTOP:
2536 		error = pf_stop();
2537 		break;
2538 #endif
2539 
2540 	case DIOCGETETHRULES: {
2541 		struct pfioc_nv		*nv = (struct pfioc_nv *)addr;
2542 		nvlist_t		*nvl;
2543 		void			*packed;
2544 		struct pf_keth_rule	*tail;
2545 		struct pf_keth_ruleset	*rs;
2546 		u_int32_t		 ticket, nr;
2547 		const char		*anchor = "";
2548 
2549 		nvl = NULL;
2550 		packed = NULL;
2551 
2552 #define	ERROUT(x)	ERROUT_IOCTL(DIOCGETETHRULES_error, x)
2553 
2554 		if (nv->len > pf_ioctl_maxcount)
2555 			ERROUT(ENOMEM);
2556 
2557 		/* Copy the request in */
2558 		packed = malloc(nv->len, M_NVLIST, M_WAITOK);
2559 		if (packed == NULL)
2560 			ERROUT(ENOMEM);
2561 
2562 		error = copyin(nv->data, packed, nv->len);
2563 		if (error)
2564 			ERROUT(error);
2565 
2566 		nvl = nvlist_unpack(packed, nv->len, 0);
2567 		if (nvl == NULL)
2568 			ERROUT(EBADMSG);
2569 
2570 		if (! nvlist_exists_string(nvl, "anchor"))
2571 			ERROUT(EBADMSG);
2572 
2573 		anchor = nvlist_get_string(nvl, "anchor");
2574 
2575 		rs = pf_find_keth_ruleset(anchor);
2576 
2577 		nvlist_destroy(nvl);
2578 		nvl = NULL;
2579 		free(packed, M_NVLIST);
2580 		packed = NULL;
2581 
2582 		if (rs == NULL)
2583 			ERROUT(ENOENT);
2584 
2585 		/* Reply */
2586 		nvl = nvlist_create(0);
2587 		if (nvl == NULL)
2588 			ERROUT(ENOMEM);
2589 
2590 		PF_RULES_RLOCK();
2591 
2592 		ticket = rs->active.ticket;
2593 		tail = TAILQ_LAST(rs->active.rules, pf_keth_ruleq);
2594 		if (tail)
2595 			nr = tail->nr + 1;
2596 		else
2597 			nr = 0;
2598 
2599 		PF_RULES_RUNLOCK();
2600 
2601 		nvlist_add_number(nvl, "ticket", ticket);
2602 		nvlist_add_number(nvl, "nr", nr);
2603 
2604 		packed = nvlist_pack(nvl, &nv->len);
2605 		if (packed == NULL)
2606 			ERROUT(ENOMEM);
2607 
2608 		if (nv->size == 0)
2609 			ERROUT(0);
2610 		else if (nv->size < nv->len)
2611 			ERROUT(ENOSPC);
2612 
2613 		error = copyout(packed, nv->data, nv->len);
2614 
2615 #undef ERROUT
2616 DIOCGETETHRULES_error:
2617 		free(packed, M_NVLIST);
2618 		nvlist_destroy(nvl);
2619 		break;
2620 	}
2621 
2622 	case DIOCGETETHRULE: {
2623 		struct epoch_tracker	 et;
2624 		struct pfioc_nv		*nv = (struct pfioc_nv *)addr;
2625 		nvlist_t		*nvl = NULL;
2626 		void			*nvlpacked = NULL;
2627 		struct pf_keth_rule	*rule = NULL;
2628 		struct pf_keth_ruleset	*rs;
2629 		u_int32_t		 ticket, nr;
2630 		bool			 clear = false;
2631 		const char		*anchor;
2632 
2633 #define ERROUT(x)	ERROUT_IOCTL(DIOCGETETHRULE_error, x)
2634 
2635 		if (nv->len > pf_ioctl_maxcount)
2636 			ERROUT(ENOMEM);
2637 
2638 		nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
2639 		if (nvlpacked == NULL)
2640 			ERROUT(ENOMEM);
2641 
2642 		error = copyin(nv->data, nvlpacked, nv->len);
2643 		if (error)
2644 			ERROUT(error);
2645 
2646 		nvl = nvlist_unpack(nvlpacked, nv->len, 0);
2647 		if (nvl == NULL)
2648 			ERROUT(EBADMSG);
2649 		if (! nvlist_exists_number(nvl, "ticket"))
2650 			ERROUT(EBADMSG);
2651 		ticket = nvlist_get_number(nvl, "ticket");
2652 		if (! nvlist_exists_string(nvl, "anchor"))
2653 			ERROUT(EBADMSG);
2654 		anchor = nvlist_get_string(nvl, "anchor");
2655 
2656 		if (nvlist_exists_bool(nvl, "clear"))
2657 			clear = nvlist_get_bool(nvl, "clear");
2658 
2659 		if (clear && !(flags & FWRITE))
2660 			ERROUT(EACCES);
2661 
2662 		if (! nvlist_exists_number(nvl, "nr"))
2663 			ERROUT(EBADMSG);
2664 		nr = nvlist_get_number(nvl, "nr");
2665 
2666 		PF_RULES_RLOCK();
2667 		rs = pf_find_keth_ruleset(anchor);
2668 		if (rs == NULL) {
2669 			PF_RULES_RUNLOCK();
2670 			ERROUT(ENOENT);
2671 		}
2672 		if (ticket != rs->active.ticket) {
2673 			PF_RULES_RUNLOCK();
2674 			ERROUT(EBUSY);
2675 		}
2676 
2677 		nvlist_destroy(nvl);
2678 		nvl = NULL;
2679 		free(nvlpacked, M_NVLIST);
2680 		nvlpacked = NULL;
2681 
2682 		rule = TAILQ_FIRST(rs->active.rules);
2683 		while ((rule != NULL) && (rule->nr != nr))
2684 			rule = TAILQ_NEXT(rule, entries);
2685 		if (rule == NULL) {
2686 			PF_RULES_RUNLOCK();
2687 			ERROUT(ENOENT);
2688 		}
2689 		/* Make sure rule can't go away. */
2690 		NET_EPOCH_ENTER(et);
2691 		PF_RULES_RUNLOCK();
2692 		nvl = pf_keth_rule_to_nveth_rule(rule);
2693 		if (pf_keth_anchor_nvcopyout(rs, rule, nvl))
2694 			ERROUT(EBUSY);
2695 		NET_EPOCH_EXIT(et);
2696 		if (nvl == NULL)
2697 			ERROUT(ENOMEM);
2698 
2699 		nvlpacked = nvlist_pack(nvl, &nv->len);
2700 		if (nvlpacked == NULL)
2701 			ERROUT(ENOMEM);
2702 
2703 		if (nv->size == 0)
2704 			ERROUT(0);
2705 		else if (nv->size < nv->len)
2706 			ERROUT(ENOSPC);
2707 
2708 		error = copyout(nvlpacked, nv->data, nv->len);
2709 		if (error == 0 && clear) {
2710 			counter_u64_zero(rule->evaluations);
2711 			for (int i = 0; i < 2; i++) {
2712 				counter_u64_zero(rule->packets[i]);
2713 				counter_u64_zero(rule->bytes[i]);
2714 			}
2715 		}
2716 
2717 #undef ERROUT
2718 DIOCGETETHRULE_error:
2719 		free(nvlpacked, M_NVLIST);
2720 		nvlist_destroy(nvl);
2721 		break;
2722 	}
2723 
2724 	case DIOCADDETHRULE: {
2725 		struct pfioc_nv		*nv = (struct pfioc_nv *)addr;
2726 		nvlist_t		*nvl = NULL;
2727 		void			*nvlpacked = NULL;
2728 		struct pf_keth_rule	*rule = NULL, *tail = NULL;
2729 		struct pf_keth_ruleset	*ruleset = NULL;
2730 		struct pfi_kkif		*kif = NULL, *bridge_to_kif = NULL;
2731 		const char		*anchor = "", *anchor_call = "";
2732 
2733 #define ERROUT(x)	ERROUT_IOCTL(DIOCADDETHRULE_error, x)
2734 
2735 		if (nv->len > pf_ioctl_maxcount)
2736 			ERROUT(ENOMEM);
2737 
2738 		nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
2739 		if (nvlpacked == NULL)
2740 			ERROUT(ENOMEM);
2741 
2742 		error = copyin(nv->data, nvlpacked, nv->len);
2743 		if (error)
2744 			ERROUT(error);
2745 
2746 		nvl = nvlist_unpack(nvlpacked, nv->len, 0);
2747 		if (nvl == NULL)
2748 			ERROUT(EBADMSG);
2749 
2750 		if (! nvlist_exists_number(nvl, "ticket"))
2751 			ERROUT(EBADMSG);
2752 
2753 		if (nvlist_exists_string(nvl, "anchor"))
2754 			anchor = nvlist_get_string(nvl, "anchor");
2755 		if (nvlist_exists_string(nvl, "anchor_call"))
2756 			anchor_call = nvlist_get_string(nvl, "anchor_call");
2757 
2758 		ruleset = pf_find_keth_ruleset(anchor);
2759 		if (ruleset == NULL)
2760 			ERROUT(EINVAL);
2761 
2762 		if (nvlist_get_number(nvl, "ticket") !=
2763 		    ruleset->inactive.ticket) {
2764 			DPFPRINTF(PF_DEBUG_MISC,
2765 			    ("ticket: %d != %d\n",
2766 			    (u_int32_t)nvlist_get_number(nvl, "ticket"),
2767 			    ruleset->inactive.ticket));
2768 			ERROUT(EBUSY);
2769 		}
2770 
2771 		rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
2772 		if (rule == NULL)
2773 			ERROUT(ENOMEM);
2774 		rule->timestamp = NULL;
2775 
2776 		error = pf_nveth_rule_to_keth_rule(nvl, rule);
2777 		if (error != 0)
2778 			ERROUT(error);
2779 
2780 		if (rule->ifname[0])
2781 			kif = pf_kkif_create(M_WAITOK);
2782 		if (rule->bridge_to_name[0])
2783 			bridge_to_kif = pf_kkif_create(M_WAITOK);
2784 		rule->evaluations = counter_u64_alloc(M_WAITOK);
2785 		for (int i = 0; i < 2; i++) {
2786 			rule->packets[i] = counter_u64_alloc(M_WAITOK);
2787 			rule->bytes[i] = counter_u64_alloc(M_WAITOK);
2788 		}
2789 		rule->timestamp = uma_zalloc_pcpu(pf_timestamp_pcpu_zone,
2790 		    M_WAITOK | M_ZERO);
2791 
2792 		PF_RULES_WLOCK();
2793 
2794 		if (rule->ifname[0]) {
2795 			rule->kif = pfi_kkif_attach(kif, rule->ifname);
2796 			pfi_kkif_ref(rule->kif);
2797 		} else
2798 			rule->kif = NULL;
2799 		if (rule->bridge_to_name[0]) {
2800 			rule->bridge_to = pfi_kkif_attach(bridge_to_kif,
2801 			    rule->bridge_to_name);
2802 			pfi_kkif_ref(rule->bridge_to);
2803 		} else
2804 			rule->bridge_to = NULL;
2805 
2806 #ifdef ALTQ
2807 		/* set queue IDs */
2808 		if (rule->qname[0] != 0) {
2809 			if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
2810 				error = EBUSY;
2811 			else
2812 				rule->qid = rule->qid;
2813 		}
2814 #endif
2815 		if (rule->tagname[0])
2816 			if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
2817 				error = EBUSY;
2818 		if (rule->match_tagname[0])
2819 			if ((rule->match_tag = pf_tagname2tag(
2820 			    rule->match_tagname)) == 0)
2821 				error = EBUSY;
2822 
2823 		if (error == 0 && rule->ipdst.addr.type == PF_ADDR_TABLE)
2824 			error = pf_eth_addr_setup(ruleset, &rule->ipdst.addr);
2825 		if (error == 0 && rule->ipsrc.addr.type == PF_ADDR_TABLE)
2826 			error = pf_eth_addr_setup(ruleset, &rule->ipsrc.addr);
2827 
2828 		if (error) {
2829 			pf_free_eth_rule(rule);
2830 			PF_RULES_WUNLOCK();
2831 			ERROUT(error);
2832 		}
2833 
2834 		if (pf_keth_anchor_setup(rule, ruleset, anchor_call)) {
2835 			pf_free_eth_rule(rule);
2836 			PF_RULES_WUNLOCK();
2837 			ERROUT(EINVAL);
2838 		}
2839 
2840 		tail = TAILQ_LAST(ruleset->inactive.rules, pf_keth_ruleq);
2841 		if (tail)
2842 			rule->nr = tail->nr + 1;
2843 		else
2844 			rule->nr = 0;
2845 
2846 		TAILQ_INSERT_TAIL(ruleset->inactive.rules, rule, entries);
2847 
2848 		PF_RULES_WUNLOCK();
2849 
2850 #undef ERROUT
2851 DIOCADDETHRULE_error:
2852 		nvlist_destroy(nvl);
2853 		free(nvlpacked, M_NVLIST);
2854 		break;
2855 	}
2856 
2857 	case DIOCGETETHRULESETS: {
2858 		struct epoch_tracker	 et;
2859 		struct pfioc_nv		*nv = (struct pfioc_nv *)addr;
2860 		nvlist_t		*nvl = NULL;
2861 		void			*nvlpacked = NULL;
2862 		struct pf_keth_ruleset	*ruleset;
2863 		struct pf_keth_anchor	*anchor;
2864 		int			 nr = 0;
2865 
2866 #define ERROUT(x)	ERROUT_IOCTL(DIOCGETETHRULESETS_error, x)
2867 
2868 		if (nv->len > pf_ioctl_maxcount)
2869 			ERROUT(ENOMEM);
2870 
2871 		nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
2872 		if (nvlpacked == NULL)
2873 			ERROUT(ENOMEM);
2874 
2875 		error = copyin(nv->data, nvlpacked, nv->len);
2876 		if (error)
2877 			ERROUT(error);
2878 
2879 		nvl = nvlist_unpack(nvlpacked, nv->len, 0);
2880 		if (nvl == NULL)
2881 			ERROUT(EBADMSG);
2882 		if (! nvlist_exists_string(nvl, "path"))
2883 			ERROUT(EBADMSG);
2884 
2885 		NET_EPOCH_ENTER(et);
2886 
2887 		if ((ruleset = pf_find_keth_ruleset(
2888 		    nvlist_get_string(nvl, "path"))) == NULL) {
2889 			NET_EPOCH_EXIT(et);
2890 			ERROUT(ENOENT);
2891 		}
2892 
2893 		if (ruleset->anchor == NULL) {
2894 			RB_FOREACH(anchor, pf_keth_anchor_global, &V_pf_keth_anchors)
2895 				if (anchor->parent == NULL)
2896 					nr++;
2897 		} else {
2898 			RB_FOREACH(anchor, pf_keth_anchor_node,
2899 			    &ruleset->anchor->children)
2900 				nr++;
2901 		}
2902 
2903 		NET_EPOCH_EXIT(et);
2904 
2905 		nvlist_destroy(nvl);
2906 		nvl = NULL;
2907 		free(nvlpacked, M_NVLIST);
2908 		nvlpacked = NULL;
2909 
2910 		nvl = nvlist_create(0);
2911 		if (nvl == NULL)
2912 			ERROUT(ENOMEM);
2913 
2914 		nvlist_add_number(nvl, "nr", nr);
2915 
2916 		nvlpacked = nvlist_pack(nvl, &nv->len);
2917 		if (nvlpacked == NULL)
2918 			ERROUT(ENOMEM);
2919 
2920 		if (nv->size == 0)
2921 			ERROUT(0);
2922 		else if (nv->size < nv->len)
2923 			ERROUT(ENOSPC);
2924 
2925 		error = copyout(nvlpacked, nv->data, nv->len);
2926 
2927 #undef ERROUT
2928 DIOCGETETHRULESETS_error:
2929 		free(nvlpacked, M_NVLIST);
2930 		nvlist_destroy(nvl);
2931 		break;
2932 	}
2933 
2934 	case DIOCGETETHRULESET: {
2935 		struct epoch_tracker	 et;
2936 		struct pfioc_nv		*nv = (struct pfioc_nv *)addr;
2937 		nvlist_t		*nvl = NULL;
2938 		void			*nvlpacked = NULL;
2939 		struct pf_keth_ruleset	*ruleset;
2940 		struct pf_keth_anchor	*anchor;
2941 		int			 nr = 0, req_nr = 0;
2942 		bool			 found = false;
2943 
2944 #define ERROUT(x)	ERROUT_IOCTL(DIOCGETETHRULESET_error, x)
2945 
2946 		if (nv->len > pf_ioctl_maxcount)
2947 			ERROUT(ENOMEM);
2948 
2949 		nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
2950 		if (nvlpacked == NULL)
2951 			ERROUT(ENOMEM);
2952 
2953 		error = copyin(nv->data, nvlpacked, nv->len);
2954 		if (error)
2955 			ERROUT(error);
2956 
2957 		nvl = nvlist_unpack(nvlpacked, nv->len, 0);
2958 		if (nvl == NULL)
2959 			ERROUT(EBADMSG);
2960 		if (! nvlist_exists_string(nvl, "path"))
2961 			ERROUT(EBADMSG);
2962 		if (! nvlist_exists_number(nvl, "nr"))
2963 			ERROUT(EBADMSG);
2964 
2965 		req_nr = nvlist_get_number(nvl, "nr");
2966 
2967 		NET_EPOCH_ENTER(et);
2968 
2969 		if ((ruleset = pf_find_keth_ruleset(
2970 		    nvlist_get_string(nvl, "path"))) == NULL) {
2971 			NET_EPOCH_EXIT(et);
2972 			ERROUT(ENOENT);
2973 		}
2974 
2975 		nvlist_destroy(nvl);
2976 		nvl = NULL;
2977 		free(nvlpacked, M_NVLIST);
2978 		nvlpacked = NULL;
2979 
2980 		nvl = nvlist_create(0);
2981 		if (nvl == NULL) {
2982 			NET_EPOCH_EXIT(et);
2983 			ERROUT(ENOMEM);
2984 		}
2985 
2986 		if (ruleset->anchor == NULL) {
2987 			RB_FOREACH(anchor, pf_keth_anchor_global,
2988 			    &V_pf_keth_anchors) {
2989 				if (anchor->parent == NULL && nr++ == req_nr) {
2990 					found = true;
2991 					break;
2992 				}
2993 			}
2994 		} else {
2995 			RB_FOREACH(anchor, pf_keth_anchor_node,
2996 			     &ruleset->anchor->children) {
2997 				if (nr++ == req_nr) {
2998 					found = true;
2999 					break;
3000 				}
3001 			}
3002 		}
3003 
3004 		NET_EPOCH_EXIT(et);
3005 		if (found) {
3006 			nvlist_add_number(nvl, "nr", nr);
3007 			nvlist_add_string(nvl, "name", anchor->name);
3008 			if (ruleset->anchor)
3009 				nvlist_add_string(nvl, "path",
3010 				    ruleset->anchor->path);
3011 			else
3012 				nvlist_add_string(nvl, "path", "");
3013 		} else {
3014 			ERROUT(EBUSY);
3015 		}
3016 
3017 		nvlpacked = nvlist_pack(nvl, &nv->len);
3018 		if (nvlpacked == NULL)
3019 			ERROUT(ENOMEM);
3020 
3021 		if (nv->size == 0)
3022 			ERROUT(0);
3023 		else if (nv->size < nv->len)
3024 			ERROUT(ENOSPC);
3025 
3026 		error = copyout(nvlpacked, nv->data, nv->len);
3027 
3028 #undef ERROUT
3029 DIOCGETETHRULESET_error:
3030 		free(nvlpacked, M_NVLIST);
3031 		nvlist_destroy(nvl);
3032 		break;
3033 	}
3034 
3035 	case DIOCADDRULENV: {
3036 		struct pfioc_nv	*nv = (struct pfioc_nv *)addr;
3037 		nvlist_t	*nvl = NULL;
3038 		void		*nvlpacked = NULL;
3039 		struct pf_krule	*rule = NULL;
3040 		const char	*anchor = "", *anchor_call = "";
3041 		uint32_t	 ticket = 0, pool_ticket = 0;
3042 
3043 #define	ERROUT(x)	ERROUT_IOCTL(DIOCADDRULENV_error, x)
3044 
3045 		if (nv->len > pf_ioctl_maxcount)
3046 			ERROUT(ENOMEM);
3047 
3048 		nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
3049 		error = copyin(nv->data, nvlpacked, nv->len);
3050 		if (error)
3051 			ERROUT(error);
3052 
3053 		nvl = nvlist_unpack(nvlpacked, nv->len, 0);
3054 		if (nvl == NULL)
3055 			ERROUT(EBADMSG);
3056 
3057 		if (! nvlist_exists_number(nvl, "ticket"))
3058 			ERROUT(EINVAL);
3059 		ticket = nvlist_get_number(nvl, "ticket");
3060 
3061 		if (! nvlist_exists_number(nvl, "pool_ticket"))
3062 			ERROUT(EINVAL);
3063 		pool_ticket = nvlist_get_number(nvl, "pool_ticket");
3064 
3065 		if (! nvlist_exists_nvlist(nvl, "rule"))
3066 			ERROUT(EINVAL);
3067 
3068 		rule = pf_krule_alloc();
3069 		error = pf_nvrule_to_krule(nvlist_get_nvlist(nvl, "rule"),
3070 		    rule);
3071 		if (error)
3072 			ERROUT(error);
3073 
3074 		if (nvlist_exists_string(nvl, "anchor"))
3075 			anchor = nvlist_get_string(nvl, "anchor");
3076 		if (nvlist_exists_string(nvl, "anchor_call"))
3077 			anchor_call = nvlist_get_string(nvl, "anchor_call");
3078 
3079 		if ((error = nvlist_error(nvl)))
3080 			ERROUT(error);
3081 
3082 		/* Frees rule on error */
3083 		error = pf_ioctl_addrule(rule, ticket, pool_ticket, anchor,
3084 		    anchor_call, td->td_ucred->cr_ruid,
3085 		    td->td_proc ? td->td_proc->p_pid : 0);
3086 
3087 		nvlist_destroy(nvl);
3088 		free(nvlpacked, M_NVLIST);
3089 		break;
3090 #undef ERROUT
3091 DIOCADDRULENV_error:
3092 		pf_krule_free(rule);
3093 		nvlist_destroy(nvl);
3094 		free(nvlpacked, M_NVLIST);
3095 
3096 		break;
3097 	}
3098 	case DIOCADDRULE: {
3099 		struct pfioc_rule	*pr = (struct pfioc_rule *)addr;
3100 		struct pf_krule		*rule;
3101 
3102 		rule = pf_krule_alloc();
3103 		error = pf_rule_to_krule(&pr->rule, rule);
3104 		if (error != 0) {
3105 			pf_krule_free(rule);
3106 			break;
3107 		}
3108 
3109 		pr->anchor[sizeof(pr->anchor) - 1] = 0;
3110 
3111 		/* Frees rule on error */
3112 		error = pf_ioctl_addrule(rule, pr->ticket, pr->pool_ticket,
3113 		    pr->anchor, pr->anchor_call, td->td_ucred->cr_ruid,
3114 		    td->td_proc ? td->td_proc->p_pid : 0);
3115 		break;
3116 	}
3117 
3118 	case DIOCGETRULES: {
3119 		struct pfioc_rule	*pr = (struct pfioc_rule *)addr;
3120 		struct pf_kruleset	*ruleset;
3121 		struct pf_krule		*tail;
3122 		int			 rs_num;
3123 
3124 		pr->anchor[sizeof(pr->anchor) - 1] = 0;
3125 
3126 		PF_RULES_WLOCK();
3127 		ruleset = pf_find_kruleset(pr->anchor);
3128 		if (ruleset == NULL) {
3129 			PF_RULES_WUNLOCK();
3130 			error = EINVAL;
3131 			break;
3132 		}
3133 		rs_num = pf_get_ruleset_number(pr->rule.action);
3134 		if (rs_num >= PF_RULESET_MAX) {
3135 			PF_RULES_WUNLOCK();
3136 			error = EINVAL;
3137 			break;
3138 		}
3139 		tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
3140 		    pf_krulequeue);
3141 		if (tail)
3142 			pr->nr = tail->nr + 1;
3143 		else
3144 			pr->nr = 0;
3145 		pr->ticket = ruleset->rules[rs_num].active.ticket;
3146 		PF_RULES_WUNLOCK();
3147 		break;
3148 	}
3149 
3150 	case DIOCGETRULENV: {
3151 		struct pfioc_nv		*nv = (struct pfioc_nv *)addr;
3152 		nvlist_t		*nvrule = NULL;
3153 		nvlist_t		*nvl = NULL;
3154 		struct pf_kruleset	*ruleset;
3155 		struct pf_krule		*rule;
3156 		void			*nvlpacked = NULL;
3157 		int			 rs_num, nr;
3158 		bool			 clear_counter = false;
3159 
3160 #define	ERROUT(x)	ERROUT_IOCTL(DIOCGETRULENV_error, x)
3161 
3162 		if (nv->len > pf_ioctl_maxcount)
3163 			ERROUT(ENOMEM);
3164 
3165 		/* Copy the request in */
3166 		nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
3167 		if (nvlpacked == NULL)
3168 			ERROUT(ENOMEM);
3169 
3170 		error = copyin(nv->data, nvlpacked, nv->len);
3171 		if (error)
3172 			ERROUT(error);
3173 
3174 		nvl = nvlist_unpack(nvlpacked, nv->len, 0);
3175 		if (nvl == NULL)
3176 			ERROUT(EBADMSG);
3177 
3178 		if (! nvlist_exists_string(nvl, "anchor"))
3179 			ERROUT(EBADMSG);
3180 		if (! nvlist_exists_number(nvl, "ruleset"))
3181 			ERROUT(EBADMSG);
3182 		if (! nvlist_exists_number(nvl, "ticket"))
3183 			ERROUT(EBADMSG);
3184 		if (! nvlist_exists_number(nvl, "nr"))
3185 			ERROUT(EBADMSG);
3186 
3187 		if (nvlist_exists_bool(nvl, "clear_counter"))
3188 			clear_counter = nvlist_get_bool(nvl, "clear_counter");
3189 
3190 		if (clear_counter && !(flags & FWRITE))
3191 			ERROUT(EACCES);
3192 
3193 		nr = nvlist_get_number(nvl, "nr");
3194 
3195 		PF_RULES_WLOCK();
3196 		ruleset = pf_find_kruleset(nvlist_get_string(nvl, "anchor"));
3197 		if (ruleset == NULL) {
3198 			PF_RULES_WUNLOCK();
3199 			ERROUT(ENOENT);
3200 		}
3201 
3202 		rs_num = pf_get_ruleset_number(nvlist_get_number(nvl, "ruleset"));
3203 		if (rs_num >= PF_RULESET_MAX) {
3204 			PF_RULES_WUNLOCK();
3205 			ERROUT(EINVAL);
3206 		}
3207 
3208 		if (nvlist_get_number(nvl, "ticket") !=
3209 		    ruleset->rules[rs_num].active.ticket) {
3210 			PF_RULES_WUNLOCK();
3211 			ERROUT(EBUSY);
3212 		}
3213 
3214 		if ((error = nvlist_error(nvl))) {
3215 			PF_RULES_WUNLOCK();
3216 			ERROUT(error);
3217 		}
3218 
3219 		rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
3220 		while ((rule != NULL) && (rule->nr != nr))
3221 			rule = TAILQ_NEXT(rule, entries);
3222 		if (rule == NULL) {
3223 			PF_RULES_WUNLOCK();
3224 			ERROUT(EBUSY);
3225 		}
3226 
3227 		nvrule = pf_krule_to_nvrule(rule);
3228 
3229 		nvlist_destroy(nvl);
3230 		nvl = nvlist_create(0);
3231 		if (nvl == NULL) {
3232 			PF_RULES_WUNLOCK();
3233 			ERROUT(ENOMEM);
3234 		}
3235 		nvlist_add_number(nvl, "nr", nr);
3236 		nvlist_add_nvlist(nvl, "rule", nvrule);
3237 		nvlist_destroy(nvrule);
3238 		nvrule = NULL;
3239 		if (pf_kanchor_nvcopyout(ruleset, rule, nvl)) {
3240 			PF_RULES_WUNLOCK();
3241 			ERROUT(EBUSY);
3242 		}
3243 
3244 		free(nvlpacked, M_NVLIST);
3245 		nvlpacked = nvlist_pack(nvl, &nv->len);
3246 		if (nvlpacked == NULL) {
3247 			PF_RULES_WUNLOCK();
3248 			ERROUT(ENOMEM);
3249 		}
3250 
3251 		if (nv->size == 0) {
3252 			PF_RULES_WUNLOCK();
3253 			ERROUT(0);
3254 		}
3255 		else if (nv->size < nv->len) {
3256 			PF_RULES_WUNLOCK();
3257 			ERROUT(ENOSPC);
3258 		}
3259 
3260 		if (clear_counter) {
3261 			pf_counter_u64_zero(&rule->evaluations);
3262 			for (int i = 0; i < 2; i++) {
3263 				pf_counter_u64_zero(&rule->packets[i]);
3264 				pf_counter_u64_zero(&rule->bytes[i]);
3265 			}
3266 			counter_u64_zero(rule->states_tot);
3267 		}
3268 		PF_RULES_WUNLOCK();
3269 
3270 		error = copyout(nvlpacked, nv->data, nv->len);
3271 
3272 #undef ERROUT
3273 DIOCGETRULENV_error:
3274 		free(nvlpacked, M_NVLIST);
3275 		nvlist_destroy(nvrule);
3276 		nvlist_destroy(nvl);
3277 
3278 		break;
3279 	}
3280 
3281 	case DIOCCHANGERULE: {
3282 		struct pfioc_rule	*pcr = (struct pfioc_rule *)addr;
3283 		struct pf_kruleset	*ruleset;
3284 		struct pf_krule		*oldrule = NULL, *newrule = NULL;
3285 		struct pfi_kkif		*kif = NULL;
3286 		struct pf_kpooladdr	*pa;
3287 		u_int32_t		 nr = 0;
3288 		int			 rs_num;
3289 
3290 		pcr->anchor[sizeof(pcr->anchor) - 1] = 0;
3291 
3292 		if (pcr->action < PF_CHANGE_ADD_HEAD ||
3293 		    pcr->action > PF_CHANGE_GET_TICKET) {
3294 			error = EINVAL;
3295 			break;
3296 		}
3297 		if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
3298 			error = EINVAL;
3299 			break;
3300 		}
3301 
3302 		if (pcr->action != PF_CHANGE_REMOVE) {
3303 			newrule = pf_krule_alloc();
3304 			error = pf_rule_to_krule(&pcr->rule, newrule);
3305 			if (error != 0) {
3306 				pf_krule_free(newrule);
3307 				break;
3308 			}
3309 
3310 			if (newrule->ifname[0])
3311 				kif = pf_kkif_create(M_WAITOK);
3312 			pf_counter_u64_init(&newrule->evaluations, M_WAITOK);
3313 			for (int i = 0; i < 2; i++) {
3314 				pf_counter_u64_init(&newrule->packets[i], M_WAITOK);
3315 				pf_counter_u64_init(&newrule->bytes[i], M_WAITOK);
3316 			}
3317 			newrule->states_cur = counter_u64_alloc(M_WAITOK);
3318 			newrule->states_tot = counter_u64_alloc(M_WAITOK);
3319 			newrule->src_nodes = counter_u64_alloc(M_WAITOK);
3320 			newrule->cuid = td->td_ucred->cr_ruid;
3321 			newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
3322 			TAILQ_INIT(&newrule->rpool.list);
3323 		}
3324 #define	ERROUT(x)	ERROUT_IOCTL(DIOCCHANGERULE_error, x)
3325 
3326 		PF_CONFIG_LOCK();
3327 		PF_RULES_WLOCK();
3328 #ifdef PF_WANT_32_TO_64_COUNTER
3329 		if (newrule != NULL) {
3330 			LIST_INSERT_HEAD(&V_pf_allrulelist, newrule, allrulelist);
3331 			newrule->allrulelinked = true;
3332 			V_pf_allrulecount++;
3333 		}
3334 #endif
3335 
3336 		if (!(pcr->action == PF_CHANGE_REMOVE ||
3337 		    pcr->action == PF_CHANGE_GET_TICKET) &&
3338 		    pcr->pool_ticket != V_ticket_pabuf)
3339 			ERROUT(EBUSY);
3340 
3341 		ruleset = pf_find_kruleset(pcr->anchor);
3342 		if (ruleset == NULL)
3343 			ERROUT(EINVAL);
3344 
3345 		rs_num = pf_get_ruleset_number(pcr->rule.action);
3346 		if (rs_num >= PF_RULESET_MAX)
3347 			ERROUT(EINVAL);
3348 
3349 		/*
3350 		 * XXXMJG: there is no guarantee that the ruleset was
3351 		 * created by the usual route of calling DIOCXBEGIN.
3352 		 * As a result it is possible the rule tree will not
3353 		 * be allocated yet. Hack around it by doing it here.
3354 		 * Note it is fine to let the tree persist in case of
3355 		 * error as it will be freed down the road on future
3356 		 * updates (if need be).
3357 		 */
3358 		if (ruleset->rules[rs_num].active.tree == NULL) {
3359 			ruleset->rules[rs_num].active.tree = pf_rule_tree_alloc(M_NOWAIT);
3360 			if (ruleset->rules[rs_num].active.tree == NULL) {
3361 				ERROUT(ENOMEM);
3362 			}
3363 		}
3364 
3365 		if (pcr->action == PF_CHANGE_GET_TICKET) {
3366 			pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
3367 			ERROUT(0);
3368 		} else if (pcr->ticket !=
3369 			    ruleset->rules[rs_num].active.ticket)
3370 				ERROUT(EINVAL);
3371 
3372 		if (pcr->action != PF_CHANGE_REMOVE) {
3373 			if (newrule->ifname[0]) {
3374 				newrule->kif = pfi_kkif_attach(kif,
3375 				    newrule->ifname);
3376 				kif = NULL;
3377 				pfi_kkif_ref(newrule->kif);
3378 			} else
3379 				newrule->kif = NULL;
3380 
3381 			if (newrule->rtableid > 0 &&
3382 			    newrule->rtableid >= rt_numfibs)
3383 				error = EBUSY;
3384 
3385 #ifdef ALTQ
3386 			/* set queue IDs */
3387 			if (newrule->qname[0] != 0) {
3388 				if ((newrule->qid =
3389 				    pf_qname2qid(newrule->qname)) == 0)
3390 					error = EBUSY;
3391 				else if (newrule->pqname[0] != 0) {
3392 					if ((newrule->pqid =
3393 					    pf_qname2qid(newrule->pqname)) == 0)
3394 						error = EBUSY;
3395 				} else
3396 					newrule->pqid = newrule->qid;
3397 			}
3398 #endif /* ALTQ */
3399 			if (newrule->tagname[0])
3400 				if ((newrule->tag =
3401 				    pf_tagname2tag(newrule->tagname)) == 0)
3402 					error = EBUSY;
3403 			if (newrule->match_tagname[0])
3404 				if ((newrule->match_tag = pf_tagname2tag(
3405 				    newrule->match_tagname)) == 0)
3406 					error = EBUSY;
3407 			if (newrule->rt && !newrule->direction)
3408 				error = EINVAL;
3409 			if (!newrule->log)
3410 				newrule->logif = 0;
3411 			if (newrule->logif >= PFLOGIFS_MAX)
3412 				error = EINVAL;
3413 			if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
3414 				error = ENOMEM;
3415 			if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
3416 				error = ENOMEM;
3417 			if (pf_kanchor_setup(newrule, ruleset, pcr->anchor_call))
3418 				error = EINVAL;
3419 			TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
3420 				if (pa->addr.type == PF_ADDR_TABLE) {
3421 					pa->addr.p.tbl =
3422 					    pfr_attach_table(ruleset,
3423 					    pa->addr.v.tblname);
3424 					if (pa->addr.p.tbl == NULL)
3425 						error = ENOMEM;
3426 				}
3427 
3428 			newrule->overload_tbl = NULL;
3429 			if (newrule->overload_tblname[0]) {
3430 				if ((newrule->overload_tbl = pfr_attach_table(
3431 				    ruleset, newrule->overload_tblname)) ==
3432 				    NULL)
3433 					error = EINVAL;
3434 				else
3435 					newrule->overload_tbl->pfrkt_flags |=
3436 					    PFR_TFLAG_ACTIVE;
3437 			}
3438 
3439 			pf_mv_kpool(&V_pf_pabuf, &newrule->rpool.list);
3440 			if (((((newrule->action == PF_NAT) ||
3441 			    (newrule->action == PF_RDR) ||
3442 			    (newrule->action == PF_BINAT) ||
3443 			    (newrule->rt > PF_NOPFROUTE)) &&
3444 			    !newrule->anchor)) &&
3445 			    (TAILQ_FIRST(&newrule->rpool.list) == NULL))
3446 				error = EINVAL;
3447 
3448 			if (error) {
3449 				pf_free_rule(newrule);
3450 				PF_RULES_WUNLOCK();
3451 				PF_CONFIG_UNLOCK();
3452 				break;
3453 			}
3454 
3455 			newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
3456 		}
3457 		pf_empty_kpool(&V_pf_pabuf);
3458 
3459 		if (pcr->action == PF_CHANGE_ADD_HEAD)
3460 			oldrule = TAILQ_FIRST(
3461 			    ruleset->rules[rs_num].active.ptr);
3462 		else if (pcr->action == PF_CHANGE_ADD_TAIL)
3463 			oldrule = TAILQ_LAST(
3464 			    ruleset->rules[rs_num].active.ptr, pf_krulequeue);
3465 		else {
3466 			oldrule = TAILQ_FIRST(
3467 			    ruleset->rules[rs_num].active.ptr);
3468 			while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
3469 				oldrule = TAILQ_NEXT(oldrule, entries);
3470 			if (oldrule == NULL) {
3471 				if (newrule != NULL)
3472 					pf_free_rule(newrule);
3473 				PF_RULES_WUNLOCK();
3474 				PF_CONFIG_UNLOCK();
3475 				error = EINVAL;
3476 				break;
3477 			}
3478 		}
3479 
3480 		if (pcr->action == PF_CHANGE_REMOVE) {
3481 			pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
3482 			    oldrule);
3483 			RB_REMOVE(pf_krule_global,
3484 			    ruleset->rules[rs_num].active.tree, oldrule);
3485 			ruleset->rules[rs_num].active.rcount--;
3486 		} else {
3487 			pf_hash_rule(newrule);
3488 			if (RB_INSERT(pf_krule_global,
3489 			    ruleset->rules[rs_num].active.tree, newrule) != NULL) {
3490 				pf_free_rule(newrule);
3491 				PF_RULES_WUNLOCK();
3492 				PF_CONFIG_UNLOCK();
3493 				error = EEXIST;
3494 				break;
3495 			}
3496 
3497 			if (oldrule == NULL)
3498 				TAILQ_INSERT_TAIL(
3499 				    ruleset->rules[rs_num].active.ptr,
3500 				    newrule, entries);
3501 			else if (pcr->action == PF_CHANGE_ADD_HEAD ||
3502 			    pcr->action == PF_CHANGE_ADD_BEFORE)
3503 				TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
3504 			else
3505 				TAILQ_INSERT_AFTER(
3506 				    ruleset->rules[rs_num].active.ptr,
3507 				    oldrule, newrule, entries);
3508 			ruleset->rules[rs_num].active.rcount++;
3509 		}
3510 
3511 		nr = 0;
3512 		TAILQ_FOREACH(oldrule,
3513 		    ruleset->rules[rs_num].active.ptr, entries)
3514 			oldrule->nr = nr++;
3515 
3516 		ruleset->rules[rs_num].active.ticket++;
3517 
3518 		pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
3519 		pf_remove_if_empty_kruleset(ruleset);
3520 
3521 		PF_RULES_WUNLOCK();
3522 		PF_CONFIG_UNLOCK();
3523 		break;
3524 
3525 #undef ERROUT
3526 DIOCCHANGERULE_error:
3527 		PF_RULES_WUNLOCK();
3528 		PF_CONFIG_UNLOCK();
3529 		pf_krule_free(newrule);
3530 		pf_kkif_free(kif);
3531 		break;
3532 	}
3533 
3534 	case DIOCCLRSTATESNV: {
3535 		error = pf_clearstates_nv((struct pfioc_nv *)addr);
3536 		break;
3537 	}
3538 
3539 	case DIOCKILLSTATESNV: {
3540 		error = pf_killstates_nv((struct pfioc_nv *)addr);
3541 		break;
3542 	}
3543 
3544 	case DIOCADDSTATE: {
3545 		struct pfioc_state		*ps = (struct pfioc_state *)addr;
3546 		struct pfsync_state_1301	*sp = &ps->state;
3547 
3548 		if (sp->timeout >= PFTM_MAX) {
3549 			error = EINVAL;
3550 			break;
3551 		}
3552 		if (V_pfsync_state_import_ptr != NULL) {
3553 			PF_RULES_RLOCK();
3554 			error = V_pfsync_state_import_ptr(
3555 			    (union pfsync_state_union *)sp, PFSYNC_SI_IOCTL,
3556 			    PFSYNC_MSG_VERSION_1301);
3557 			PF_RULES_RUNLOCK();
3558 		} else
3559 			error = EOPNOTSUPP;
3560 		break;
3561 	}
3562 
3563 	case DIOCGETSTATE: {
3564 		struct pfioc_state	*ps = (struct pfioc_state *)addr;
3565 		struct pf_kstate	*s;
3566 
3567 		s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
3568 		if (s == NULL) {
3569 			error = ENOENT;
3570 			break;
3571 		}
3572 
3573 		pfsync_state_export((union pfsync_state_union*)&ps->state,
3574 		    s, PFSYNC_MSG_VERSION_1301);
3575 		PF_STATE_UNLOCK(s);
3576 		break;
3577 	}
3578 
3579 	case DIOCGETSTATENV: {
3580 		error = pf_getstate((struct pfioc_nv *)addr);
3581 		break;
3582 	}
3583 
3584 #ifdef COMPAT_FREEBSD14
3585 	case DIOCGETSTATES: {
3586 		struct pfioc_states	*ps = (struct pfioc_states *)addr;
3587 		struct pf_kstate	*s;
3588 		struct pfsync_state_1301	*pstore, *p;
3589 		int			 i, nr;
3590 		size_t			 slice_count = 16, count;
3591 		void			*out;
3592 
3593 		if (ps->ps_len <= 0) {
3594 			nr = uma_zone_get_cur(V_pf_state_z);
3595 			ps->ps_len = sizeof(struct pfsync_state_1301) * nr;
3596 			break;
3597 		}
3598 
3599 		out = ps->ps_states;
3600 		pstore = mallocarray(slice_count,
3601 		    sizeof(struct pfsync_state_1301), M_TEMP, M_WAITOK | M_ZERO);
3602 		nr = 0;
3603 
3604 		for (i = 0; i <= pf_hashmask; i++) {
3605 			struct pf_idhash *ih = &V_pf_idhash[i];
3606 
3607 DIOCGETSTATES_retry:
3608 			p = pstore;
3609 
3610 			if (LIST_EMPTY(&ih->states))
3611 				continue;
3612 
3613 			PF_HASHROW_LOCK(ih);
3614 			count = 0;
3615 			LIST_FOREACH(s, &ih->states, entry) {
3616 				if (s->timeout == PFTM_UNLINKED)
3617 					continue;
3618 				count++;
3619 			}
3620 
3621 			if (count > slice_count) {
3622 				PF_HASHROW_UNLOCK(ih);
3623 				free(pstore, M_TEMP);
3624 				slice_count = count * 2;
3625 				pstore = mallocarray(slice_count,
3626 				    sizeof(struct pfsync_state_1301), M_TEMP,
3627 				    M_WAITOK | M_ZERO);
3628 				goto DIOCGETSTATES_retry;
3629 			}
3630 
3631 			if ((nr+count) * sizeof(*p) > ps->ps_len) {
3632 				PF_HASHROW_UNLOCK(ih);
3633 				goto DIOCGETSTATES_full;
3634 			}
3635 
3636 			LIST_FOREACH(s, &ih->states, entry) {
3637 				if (s->timeout == PFTM_UNLINKED)
3638 					continue;
3639 
3640 				pfsync_state_export((union pfsync_state_union*)p,
3641 				    s, PFSYNC_MSG_VERSION_1301);
3642 				p++;
3643 				nr++;
3644 			}
3645 			PF_HASHROW_UNLOCK(ih);
3646 			error = copyout(pstore, out,
3647 			    sizeof(struct pfsync_state_1301) * count);
3648 			if (error)
3649 				break;
3650 			out = ps->ps_states + nr;
3651 		}
3652 DIOCGETSTATES_full:
3653 		ps->ps_len = sizeof(struct pfsync_state_1301) * nr;
3654 		free(pstore, M_TEMP);
3655 
3656 		break;
3657 	}
3658 
3659 	case DIOCGETSTATESV2: {
3660 		struct pfioc_states_v2	*ps = (struct pfioc_states_v2 *)addr;
3661 		struct pf_kstate	*s;
3662 		struct pf_state_export	*pstore, *p;
3663 		int i, nr;
3664 		size_t slice_count = 16, count;
3665 		void *out;
3666 
3667 		if (ps->ps_req_version > PF_STATE_VERSION) {
3668 			error = ENOTSUP;
3669 			break;
3670 		}
3671 
3672 		if (ps->ps_len <= 0) {
3673 			nr = uma_zone_get_cur(V_pf_state_z);
3674 			ps->ps_len = sizeof(struct pf_state_export) * nr;
3675 			break;
3676 		}
3677 
3678 		out = ps->ps_states;
3679 		pstore = mallocarray(slice_count,
3680 		    sizeof(struct pf_state_export), M_TEMP, M_WAITOK | M_ZERO);
3681 		nr = 0;
3682 
3683 		for (i = 0; i <= pf_hashmask; i++) {
3684 			struct pf_idhash *ih = &V_pf_idhash[i];
3685 
3686 DIOCGETSTATESV2_retry:
3687 			p = pstore;
3688 
3689 			if (LIST_EMPTY(&ih->states))
3690 				continue;
3691 
3692 			PF_HASHROW_LOCK(ih);
3693 			count = 0;
3694 			LIST_FOREACH(s, &ih->states, entry) {
3695 				if (s->timeout == PFTM_UNLINKED)
3696 					continue;
3697 				count++;
3698 			}
3699 
3700 			if (count > slice_count) {
3701 				PF_HASHROW_UNLOCK(ih);
3702 				free(pstore, M_TEMP);
3703 				slice_count = count * 2;
3704 				pstore = mallocarray(slice_count,
3705 				    sizeof(struct pf_state_export), M_TEMP,
3706 				    M_WAITOK | M_ZERO);
3707 				goto DIOCGETSTATESV2_retry;
3708 			}
3709 
3710 			if ((nr+count) * sizeof(*p) > ps->ps_len) {
3711 				PF_HASHROW_UNLOCK(ih);
3712 				goto DIOCGETSTATESV2_full;
3713 			}
3714 
3715 			LIST_FOREACH(s, &ih->states, entry) {
3716 				if (s->timeout == PFTM_UNLINKED)
3717 					continue;
3718 
3719 				pf_state_export(p, s);
3720 				p++;
3721 				nr++;
3722 			}
3723 			PF_HASHROW_UNLOCK(ih);
3724 			error = copyout(pstore, out,
3725 			    sizeof(struct pf_state_export) * count);
3726 			if (error)
3727 				break;
3728 			out = ps->ps_states + nr;
3729 		}
3730 DIOCGETSTATESV2_full:
3731 		ps->ps_len = nr * sizeof(struct pf_state_export);
3732 		free(pstore, M_TEMP);
3733 
3734 		break;
3735 	}
3736 #endif
3737 	case DIOCGETSTATUSNV: {
3738 		error = pf_getstatus((struct pfioc_nv *)addr);
3739 		break;
3740 	}
3741 
3742 	case DIOCSETSTATUSIF: {
3743 		struct pfioc_if	*pi = (struct pfioc_if *)addr;
3744 
3745 		if (pi->ifname[0] == 0) {
3746 			bzero(V_pf_status.ifname, IFNAMSIZ);
3747 			break;
3748 		}
3749 		PF_RULES_WLOCK();
3750 		error = pf_user_strcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
3751 		PF_RULES_WUNLOCK();
3752 		break;
3753 	}
3754 
3755 	case DIOCCLRSTATUS: {
3756 		PF_RULES_WLOCK();
3757 		for (int i = 0; i < PFRES_MAX; i++)
3758 			counter_u64_zero(V_pf_status.counters[i]);
3759 		for (int i = 0; i < FCNT_MAX; i++)
3760 			pf_counter_u64_zero(&V_pf_status.fcounters[i]);
3761 		for (int i = 0; i < SCNT_MAX; i++)
3762 			counter_u64_zero(V_pf_status.scounters[i]);
3763 		for (int i = 0; i < KLCNT_MAX; i++)
3764 			counter_u64_zero(V_pf_status.lcounters[i]);
3765 		V_pf_status.since = time_second;
3766 		if (*V_pf_status.ifname)
3767 			pfi_update_status(V_pf_status.ifname, NULL);
3768 		PF_RULES_WUNLOCK();
3769 		break;
3770 	}
3771 
3772 	case DIOCNATLOOK: {
3773 		struct pfioc_natlook	*pnl = (struct pfioc_natlook *)addr;
3774 		struct pf_state_key	*sk;
3775 		struct pf_kstate	*state;
3776 		struct pf_state_key_cmp	 key;
3777 		int			 m = 0, direction = pnl->direction;
3778 		int			 sidx, didx;
3779 
3780 		/* NATLOOK src and dst are reversed, so reverse sidx/didx */
3781 		sidx = (direction == PF_IN) ? 1 : 0;
3782 		didx = (direction == PF_IN) ? 0 : 1;
3783 
3784 		if (!pnl->proto ||
3785 		    PF_AZERO(&pnl->saddr, pnl->af) ||
3786 		    PF_AZERO(&pnl->daddr, pnl->af) ||
3787 		    ((pnl->proto == IPPROTO_TCP ||
3788 		    pnl->proto == IPPROTO_UDP) &&
3789 		    (!pnl->dport || !pnl->sport)))
3790 			error = EINVAL;
3791 		else {
3792 			bzero(&key, sizeof(key));
3793 			key.af = pnl->af;
3794 			key.proto = pnl->proto;
3795 			PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
3796 			key.port[sidx] = pnl->sport;
3797 			PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
3798 			key.port[didx] = pnl->dport;
3799 
3800 			state = pf_find_state_all(&key, direction, &m);
3801 			if (state == NULL) {
3802 				error = ENOENT;
3803 			} else {
3804 				if (m > 1) {
3805 					PF_STATE_UNLOCK(state);
3806 					error = E2BIG;	/* more than one state */
3807 				} else {
3808 					sk = state->key[sidx];
3809 					PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
3810 					pnl->rsport = sk->port[sidx];
3811 					PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
3812 					pnl->rdport = sk->port[didx];
3813 					PF_STATE_UNLOCK(state);
3814 				}
3815 			}
3816 		}
3817 		break;
3818 	}
3819 
3820 	case DIOCSETTIMEOUT: {
3821 		struct pfioc_tm	*pt = (struct pfioc_tm *)addr;
3822 		int		 old;
3823 
3824 		if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
3825 		    pt->seconds < 0) {
3826 			error = EINVAL;
3827 			break;
3828 		}
3829 		PF_RULES_WLOCK();
3830 		old = V_pf_default_rule.timeout[pt->timeout];
3831 		if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
3832 			pt->seconds = 1;
3833 		V_pf_default_rule.timeout[pt->timeout] = pt->seconds;
3834 		if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
3835 			wakeup(pf_purge_thread);
3836 		pt->seconds = old;
3837 		PF_RULES_WUNLOCK();
3838 		break;
3839 	}
3840 
3841 	case DIOCGETTIMEOUT: {
3842 		struct pfioc_tm	*pt = (struct pfioc_tm *)addr;
3843 
3844 		if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
3845 			error = EINVAL;
3846 			break;
3847 		}
3848 		PF_RULES_RLOCK();
3849 		pt->seconds = V_pf_default_rule.timeout[pt->timeout];
3850 		PF_RULES_RUNLOCK();
3851 		break;
3852 	}
3853 
3854 	case DIOCGETLIMIT: {
3855 		struct pfioc_limit	*pl = (struct pfioc_limit *)addr;
3856 
3857 		if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
3858 			error = EINVAL;
3859 			break;
3860 		}
3861 		PF_RULES_RLOCK();
3862 		pl->limit = V_pf_limits[pl->index].limit;
3863 		PF_RULES_RUNLOCK();
3864 		break;
3865 	}
3866 
3867 	case DIOCSETLIMIT: {
3868 		struct pfioc_limit	*pl = (struct pfioc_limit *)addr;
3869 		int			 old_limit;
3870 
3871 		PF_RULES_WLOCK();
3872 		if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
3873 		    V_pf_limits[pl->index].zone == NULL) {
3874 			PF_RULES_WUNLOCK();
3875 			error = EINVAL;
3876 			break;
3877 		}
3878 		uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit);
3879 		old_limit = V_pf_limits[pl->index].limit;
3880 		V_pf_limits[pl->index].limit = pl->limit;
3881 		pl->limit = old_limit;
3882 		PF_RULES_WUNLOCK();
3883 		break;
3884 	}
3885 
3886 	case DIOCSETDEBUG: {
3887 		u_int32_t	*level = (u_int32_t *)addr;
3888 
3889 		PF_RULES_WLOCK();
3890 		V_pf_status.debug = *level;
3891 		PF_RULES_WUNLOCK();
3892 		break;
3893 	}
3894 
3895 	case DIOCCLRRULECTRS: {
3896 		/* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
3897 		struct pf_kruleset	*ruleset = &pf_main_ruleset;
3898 		struct pf_krule		*rule;
3899 
3900 		PF_RULES_WLOCK();
3901 		TAILQ_FOREACH(rule,
3902 		    ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
3903 			pf_counter_u64_zero(&rule->evaluations);
3904 			for (int i = 0; i < 2; i++) {
3905 				pf_counter_u64_zero(&rule->packets[i]);
3906 				pf_counter_u64_zero(&rule->bytes[i]);
3907 			}
3908 		}
3909 		PF_RULES_WUNLOCK();
3910 		break;
3911 	}
3912 
3913 	case DIOCGIFSPEEDV0:
3914 	case DIOCGIFSPEEDV1: {
3915 		struct pf_ifspeed_v1	*psp = (struct pf_ifspeed_v1 *)addr;
3916 		struct pf_ifspeed_v1	ps;
3917 		struct ifnet		*ifp;
3918 
3919 		if (psp->ifname[0] == '\0') {
3920 			error = EINVAL;
3921 			break;
3922 		}
3923 
3924 		error = pf_user_strcpy(ps.ifname, psp->ifname, IFNAMSIZ);
3925 		if (error != 0)
3926 			break;
3927 		ifp = ifunit(ps.ifname);
3928 		if (ifp != NULL) {
3929 			psp->baudrate32 =
3930 			    (u_int32_t)uqmin(ifp->if_baudrate, UINT_MAX);
3931 			if (cmd == DIOCGIFSPEEDV1)
3932 				psp->baudrate = ifp->if_baudrate;
3933 		} else {
3934 			error = EINVAL;
3935 		}
3936 		break;
3937 	}
3938 
3939 #ifdef ALTQ
3940 	case DIOCSTARTALTQ: {
3941 		struct pf_altq		*altq;
3942 
3943 		PF_RULES_WLOCK();
3944 		/* enable all altq interfaces on active list */
3945 		TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
3946 			if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
3947 				error = pf_enable_altq(altq);
3948 				if (error != 0)
3949 					break;
3950 			}
3951 		}
3952 		if (error == 0)
3953 			V_pf_altq_running = 1;
3954 		PF_RULES_WUNLOCK();
3955 		DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
3956 		break;
3957 	}
3958 
3959 	case DIOCSTOPALTQ: {
3960 		struct pf_altq		*altq;
3961 
3962 		PF_RULES_WLOCK();
3963 		/* disable all altq interfaces on active list */
3964 		TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
3965 			if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
3966 				error = pf_disable_altq(altq);
3967 				if (error != 0)
3968 					break;
3969 			}
3970 		}
3971 		if (error == 0)
3972 			V_pf_altq_running = 0;
3973 		PF_RULES_WUNLOCK();
3974 		DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
3975 		break;
3976 	}
3977 
3978 	case DIOCADDALTQV0:
3979 	case DIOCADDALTQV1: {
3980 		struct pfioc_altq_v1	*pa = (struct pfioc_altq_v1 *)addr;
3981 		struct pf_altq		*altq, *a;
3982 		struct ifnet		*ifp;
3983 
3984 		altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK | M_ZERO);
3985 		error = pf_import_kaltq(pa, altq, IOCPARM_LEN(cmd));
3986 		if (error)
3987 			break;
3988 		altq->local_flags = 0;
3989 
3990 		PF_RULES_WLOCK();
3991 		if (pa->ticket != V_ticket_altqs_inactive) {
3992 			PF_RULES_WUNLOCK();
3993 			free(altq, M_PFALTQ);
3994 			error = EBUSY;
3995 			break;
3996 		}
3997 
3998 		/*
3999 		 * if this is for a queue, find the discipline and
4000 		 * copy the necessary fields
4001 		 */
4002 		if (altq->qname[0] != 0) {
4003 			if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
4004 				PF_RULES_WUNLOCK();
4005 				error = EBUSY;
4006 				free(altq, M_PFALTQ);
4007 				break;
4008 			}
4009 			altq->altq_disc = NULL;
4010 			TAILQ_FOREACH(a, V_pf_altq_ifs_inactive, entries) {
4011 				if (strncmp(a->ifname, altq->ifname,
4012 				    IFNAMSIZ) == 0) {
4013 					altq->altq_disc = a->altq_disc;
4014 					break;
4015 				}
4016 			}
4017 		}
4018 
4019 		if ((ifp = ifunit(altq->ifname)) == NULL)
4020 			altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
4021 		else
4022 			error = altq_add(ifp, altq);
4023 
4024 		if (error) {
4025 			PF_RULES_WUNLOCK();
4026 			free(altq, M_PFALTQ);
4027 			break;
4028 		}
4029 
4030 		if (altq->qname[0] != 0)
4031 			TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
4032 		else
4033 			TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, altq, entries);
4034 		/* version error check done on import above */
4035 		pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd));
4036 		PF_RULES_WUNLOCK();
4037 		break;
4038 	}
4039 
4040 	case DIOCGETALTQSV0:
4041 	case DIOCGETALTQSV1: {
4042 		struct pfioc_altq_v1	*pa = (struct pfioc_altq_v1 *)addr;
4043 		struct pf_altq		*altq;
4044 
4045 		PF_RULES_RLOCK();
4046 		pa->nr = 0;
4047 		TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries)
4048 			pa->nr++;
4049 		TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
4050 			pa->nr++;
4051 		pa->ticket = V_ticket_altqs_active;
4052 		PF_RULES_RUNLOCK();
4053 		break;
4054 	}
4055 
4056 	case DIOCGETALTQV0:
4057 	case DIOCGETALTQV1: {
4058 		struct pfioc_altq_v1	*pa = (struct pfioc_altq_v1 *)addr;
4059 		struct pf_altq		*altq;
4060 
4061 		PF_RULES_RLOCK();
4062 		if (pa->ticket != V_ticket_altqs_active) {
4063 			PF_RULES_RUNLOCK();
4064 			error = EBUSY;
4065 			break;
4066 		}
4067 		altq = pf_altq_get_nth_active(pa->nr);
4068 		if (altq == NULL) {
4069 			PF_RULES_RUNLOCK();
4070 			error = EBUSY;
4071 			break;
4072 		}
4073 		pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd));
4074 		PF_RULES_RUNLOCK();
4075 		break;
4076 	}
4077 
4078 	case DIOCCHANGEALTQV0:
4079 	case DIOCCHANGEALTQV1:
4080 		/* CHANGEALTQ not supported yet! */
4081 		error = ENODEV;
4082 		break;
4083 
4084 	case DIOCGETQSTATSV0:
4085 	case DIOCGETQSTATSV1: {
4086 		struct pfioc_qstats_v1	*pq = (struct pfioc_qstats_v1 *)addr;
4087 		struct pf_altq		*altq;
4088 		int			 nbytes;
4089 		u_int32_t		 version;
4090 
4091 		PF_RULES_RLOCK();
4092 		if (pq->ticket != V_ticket_altqs_active) {
4093 			PF_RULES_RUNLOCK();
4094 			error = EBUSY;
4095 			break;
4096 		}
4097 		nbytes = pq->nbytes;
4098 		altq = pf_altq_get_nth_active(pq->nr);
4099 		if (altq == NULL) {
4100 			PF_RULES_RUNLOCK();
4101 			error = EBUSY;
4102 			break;
4103 		}
4104 
4105 		if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
4106 			PF_RULES_RUNLOCK();
4107 			error = ENXIO;
4108 			break;
4109 		}
4110 		PF_RULES_RUNLOCK();
4111 		if (cmd == DIOCGETQSTATSV0)
4112 			version = 0;  /* DIOCGETQSTATSV0 means stats struct v0 */
4113 		else
4114 			version = pq->version;
4115 		error = altq_getqstats(altq, pq->buf, &nbytes, version);
4116 		if (error == 0) {
4117 			pq->scheduler = altq->scheduler;
4118 			pq->nbytes = nbytes;
4119 		}
4120 		break;
4121 	}
4122 #endif /* ALTQ */
4123 
4124 	case DIOCBEGINADDRS: {
4125 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
4126 
4127 		PF_RULES_WLOCK();
4128 		pf_empty_kpool(&V_pf_pabuf);
4129 		pp->ticket = ++V_ticket_pabuf;
4130 		PF_RULES_WUNLOCK();
4131 		break;
4132 	}
4133 
4134 	case DIOCADDADDR: {
4135 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
4136 		struct pf_kpooladdr	*pa;
4137 		struct pfi_kkif		*kif = NULL;
4138 
4139 #ifndef INET
4140 		if (pp->af == AF_INET) {
4141 			error = EAFNOSUPPORT;
4142 			break;
4143 		}
4144 #endif /* INET */
4145 #ifndef INET6
4146 		if (pp->af == AF_INET6) {
4147 			error = EAFNOSUPPORT;
4148 			break;
4149 		}
4150 #endif /* INET6 */
4151 		if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
4152 		    pp->addr.addr.type != PF_ADDR_DYNIFTL &&
4153 		    pp->addr.addr.type != PF_ADDR_TABLE) {
4154 			error = EINVAL;
4155 			break;
4156 		}
4157 		if (pp->addr.addr.p.dyn != NULL) {
4158 			error = EINVAL;
4159 			break;
4160 		}
4161 		pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
4162 		error = pf_pooladdr_to_kpooladdr(&pp->addr, pa);
4163 		if (error != 0)
4164 			break;
4165 		if (pa->ifname[0])
4166 			kif = pf_kkif_create(M_WAITOK);
4167 		PF_RULES_WLOCK();
4168 		if (pp->ticket != V_ticket_pabuf) {
4169 			PF_RULES_WUNLOCK();
4170 			if (pa->ifname[0])
4171 				pf_kkif_free(kif);
4172 			free(pa, M_PFRULE);
4173 			error = EBUSY;
4174 			break;
4175 		}
4176 		if (pa->ifname[0]) {
4177 			pa->kif = pfi_kkif_attach(kif, pa->ifname);
4178 			kif = NULL;
4179 			pfi_kkif_ref(pa->kif);
4180 		} else
4181 			pa->kif = NULL;
4182 		if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
4183 		    pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
4184 			if (pa->ifname[0])
4185 				pfi_kkif_unref(pa->kif);
4186 			PF_RULES_WUNLOCK();
4187 			free(pa, M_PFRULE);
4188 			break;
4189 		}
4190 		TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
4191 		PF_RULES_WUNLOCK();
4192 		break;
4193 	}
4194 
4195 	case DIOCGETADDRS: {
4196 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
4197 		struct pf_kpool		*pool;
4198 		struct pf_kpooladdr	*pa;
4199 
4200 		pp->anchor[sizeof(pp->anchor) - 1] = 0;
4201 		pp->nr = 0;
4202 
4203 		PF_RULES_RLOCK();
4204 		pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action,
4205 		    pp->r_num, 0, 1, 0);
4206 		if (pool == NULL) {
4207 			PF_RULES_RUNLOCK();
4208 			error = EBUSY;
4209 			break;
4210 		}
4211 		TAILQ_FOREACH(pa, &pool->list, entries)
4212 			pp->nr++;
4213 		PF_RULES_RUNLOCK();
4214 		break;
4215 	}
4216 
4217 	case DIOCGETADDR: {
4218 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
4219 		struct pf_kpool		*pool;
4220 		struct pf_kpooladdr	*pa;
4221 		u_int32_t		 nr = 0;
4222 
4223 		pp->anchor[sizeof(pp->anchor) - 1] = 0;
4224 
4225 		PF_RULES_RLOCK();
4226 		pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action,
4227 		    pp->r_num, 0, 1, 1);
4228 		if (pool == NULL) {
4229 			PF_RULES_RUNLOCK();
4230 			error = EBUSY;
4231 			break;
4232 		}
4233 		pa = TAILQ_FIRST(&pool->list);
4234 		while ((pa != NULL) && (nr < pp->nr)) {
4235 			pa = TAILQ_NEXT(pa, entries);
4236 			nr++;
4237 		}
4238 		if (pa == NULL) {
4239 			PF_RULES_RUNLOCK();
4240 			error = EBUSY;
4241 			break;
4242 		}
4243 		pf_kpooladdr_to_pooladdr(pa, &pp->addr);
4244 		pf_addr_copyout(&pp->addr.addr);
4245 		PF_RULES_RUNLOCK();
4246 		break;
4247 	}
4248 
4249 	case DIOCCHANGEADDR: {
4250 		struct pfioc_pooladdr	*pca = (struct pfioc_pooladdr *)addr;
4251 		struct pf_kpool		*pool;
4252 		struct pf_kpooladdr	*oldpa = NULL, *newpa = NULL;
4253 		struct pf_kruleset	*ruleset;
4254 		struct pfi_kkif		*kif = NULL;
4255 
4256 		pca->anchor[sizeof(pca->anchor) - 1] = 0;
4257 
4258 		if (pca->action < PF_CHANGE_ADD_HEAD ||
4259 		    pca->action > PF_CHANGE_REMOVE) {
4260 			error = EINVAL;
4261 			break;
4262 		}
4263 		if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
4264 		    pca->addr.addr.type != PF_ADDR_DYNIFTL &&
4265 		    pca->addr.addr.type != PF_ADDR_TABLE) {
4266 			error = EINVAL;
4267 			break;
4268 		}
4269 		if (pca->addr.addr.p.dyn != NULL) {
4270 			error = EINVAL;
4271 			break;
4272 		}
4273 
4274 		if (pca->action != PF_CHANGE_REMOVE) {
4275 #ifndef INET
4276 			if (pca->af == AF_INET) {
4277 				error = EAFNOSUPPORT;
4278 				break;
4279 			}
4280 #endif /* INET */
4281 #ifndef INET6
4282 			if (pca->af == AF_INET6) {
4283 				error = EAFNOSUPPORT;
4284 				break;
4285 			}
4286 #endif /* INET6 */
4287 			newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
4288 			bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
4289 			if (newpa->ifname[0])
4290 				kif = pf_kkif_create(M_WAITOK);
4291 			newpa->kif = NULL;
4292 		}
4293 #define	ERROUT(x)	ERROUT_IOCTL(DIOCCHANGEADDR_error, x)
4294 		PF_RULES_WLOCK();
4295 		ruleset = pf_find_kruleset(pca->anchor);
4296 		if (ruleset == NULL)
4297 			ERROUT(EBUSY);
4298 
4299 		pool = pf_get_kpool(pca->anchor, pca->ticket, pca->r_action,
4300 		    pca->r_num, pca->r_last, 1, 1);
4301 		if (pool == NULL)
4302 			ERROUT(EBUSY);
4303 
4304 		if (pca->action != PF_CHANGE_REMOVE) {
4305 			if (newpa->ifname[0]) {
4306 				newpa->kif = pfi_kkif_attach(kif, newpa->ifname);
4307 				pfi_kkif_ref(newpa->kif);
4308 				kif = NULL;
4309 			}
4310 
4311 			switch (newpa->addr.type) {
4312 			case PF_ADDR_DYNIFTL:
4313 				error = pfi_dynaddr_setup(&newpa->addr,
4314 				    pca->af);
4315 				break;
4316 			case PF_ADDR_TABLE:
4317 				newpa->addr.p.tbl = pfr_attach_table(ruleset,
4318 				    newpa->addr.v.tblname);
4319 				if (newpa->addr.p.tbl == NULL)
4320 					error = ENOMEM;
4321 				break;
4322 			}
4323 			if (error)
4324 				goto DIOCCHANGEADDR_error;
4325 		}
4326 
4327 		switch (pca->action) {
4328 		case PF_CHANGE_ADD_HEAD:
4329 			oldpa = TAILQ_FIRST(&pool->list);
4330 			break;
4331 		case PF_CHANGE_ADD_TAIL:
4332 			oldpa = TAILQ_LAST(&pool->list, pf_kpalist);
4333 			break;
4334 		default:
4335 			oldpa = TAILQ_FIRST(&pool->list);
4336 			for (int i = 0; oldpa && i < pca->nr; i++)
4337 				oldpa = TAILQ_NEXT(oldpa, entries);
4338 
4339 			if (oldpa == NULL)
4340 				ERROUT(EINVAL);
4341 		}
4342 
4343 		if (pca->action == PF_CHANGE_REMOVE) {
4344 			TAILQ_REMOVE(&pool->list, oldpa, entries);
4345 			switch (oldpa->addr.type) {
4346 			case PF_ADDR_DYNIFTL:
4347 				pfi_dynaddr_remove(oldpa->addr.p.dyn);
4348 				break;
4349 			case PF_ADDR_TABLE:
4350 				pfr_detach_table(oldpa->addr.p.tbl);
4351 				break;
4352 			}
4353 			if (oldpa->kif)
4354 				pfi_kkif_unref(oldpa->kif);
4355 			free(oldpa, M_PFRULE);
4356 		} else {
4357 			if (oldpa == NULL)
4358 				TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
4359 			else if (pca->action == PF_CHANGE_ADD_HEAD ||
4360 			    pca->action == PF_CHANGE_ADD_BEFORE)
4361 				TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
4362 			else
4363 				TAILQ_INSERT_AFTER(&pool->list, oldpa,
4364 				    newpa, entries);
4365 		}
4366 
4367 		pool->cur = TAILQ_FIRST(&pool->list);
4368 		PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af);
4369 		PF_RULES_WUNLOCK();
4370 		break;
4371 
4372 #undef ERROUT
4373 DIOCCHANGEADDR_error:
4374 		if (newpa != NULL) {
4375 			if (newpa->kif)
4376 				pfi_kkif_unref(newpa->kif);
4377 			free(newpa, M_PFRULE);
4378 		}
4379 		PF_RULES_WUNLOCK();
4380 		pf_kkif_free(kif);
4381 		break;
4382 	}
4383 
4384 	case DIOCGETRULESETS: {
4385 		struct pfioc_ruleset	*pr = (struct pfioc_ruleset *)addr;
4386 		struct pf_kruleset	*ruleset;
4387 		struct pf_kanchor	*anchor;
4388 
4389 		pr->path[sizeof(pr->path) - 1] = 0;
4390 
4391 		PF_RULES_RLOCK();
4392 		if ((ruleset = pf_find_kruleset(pr->path)) == NULL) {
4393 			PF_RULES_RUNLOCK();
4394 			error = ENOENT;
4395 			break;
4396 		}
4397 		pr->nr = 0;
4398 		if (ruleset->anchor == NULL) {
4399 			/* XXX kludge for pf_main_ruleset */
4400 			RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors)
4401 				if (anchor->parent == NULL)
4402 					pr->nr++;
4403 		} else {
4404 			RB_FOREACH(anchor, pf_kanchor_node,
4405 			    &ruleset->anchor->children)
4406 				pr->nr++;
4407 		}
4408 		PF_RULES_RUNLOCK();
4409 		break;
4410 	}
4411 
4412 	case DIOCGETRULESET: {
4413 		struct pfioc_ruleset	*pr = (struct pfioc_ruleset *)addr;
4414 		struct pf_kruleset	*ruleset;
4415 		struct pf_kanchor	*anchor;
4416 		u_int32_t		 nr = 0;
4417 
4418 		pr->path[sizeof(pr->path) - 1] = 0;
4419 
4420 		PF_RULES_RLOCK();
4421 		if ((ruleset = pf_find_kruleset(pr->path)) == NULL) {
4422 			PF_RULES_RUNLOCK();
4423 			error = ENOENT;
4424 			break;
4425 		}
4426 		pr->name[0] = 0;
4427 		if (ruleset->anchor == NULL) {
4428 			/* XXX kludge for pf_main_ruleset */
4429 			RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors)
4430 				if (anchor->parent == NULL && nr++ == pr->nr) {
4431 					strlcpy(pr->name, anchor->name,
4432 					    sizeof(pr->name));
4433 					break;
4434 				}
4435 		} else {
4436 			RB_FOREACH(anchor, pf_kanchor_node,
4437 			    &ruleset->anchor->children)
4438 				if (nr++ == pr->nr) {
4439 					strlcpy(pr->name, anchor->name,
4440 					    sizeof(pr->name));
4441 					break;
4442 				}
4443 		}
4444 		if (!pr->name[0])
4445 			error = EBUSY;
4446 		PF_RULES_RUNLOCK();
4447 		break;
4448 	}
4449 
4450 	case DIOCRCLRTABLES: {
4451 		struct pfioc_table *io = (struct pfioc_table *)addr;
4452 
4453 		if (io->pfrio_esize != 0) {
4454 			error = ENODEV;
4455 			break;
4456 		}
4457 		PF_RULES_WLOCK();
4458 		error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
4459 		    io->pfrio_flags | PFR_FLAG_USERIOCTL);
4460 		PF_RULES_WUNLOCK();
4461 		break;
4462 	}
4463 
4464 	case DIOCRADDTABLES: {
4465 		struct pfioc_table *io = (struct pfioc_table *)addr;
4466 		struct pfr_table *pfrts;
4467 		size_t totlen;
4468 
4469 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
4470 			error = ENODEV;
4471 			break;
4472 		}
4473 
4474 		if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount ||
4475 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) {
4476 			error = ENOMEM;
4477 			break;
4478 		}
4479 
4480 		totlen = io->pfrio_size * sizeof(struct pfr_table);
4481 		pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4482 		    M_TEMP, M_WAITOK);
4483 		error = copyin(io->pfrio_buffer, pfrts, totlen);
4484 		if (error) {
4485 			free(pfrts, M_TEMP);
4486 			break;
4487 		}
4488 		PF_RULES_WLOCK();
4489 		error = pfr_add_tables(pfrts, io->pfrio_size,
4490 		    &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4491 		PF_RULES_WUNLOCK();
4492 		free(pfrts, M_TEMP);
4493 		break;
4494 	}
4495 
4496 	case DIOCRDELTABLES: {
4497 		struct pfioc_table *io = (struct pfioc_table *)addr;
4498 		struct pfr_table *pfrts;
4499 		size_t totlen;
4500 
4501 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
4502 			error = ENODEV;
4503 			break;
4504 		}
4505 
4506 		if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount ||
4507 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) {
4508 			error = ENOMEM;
4509 			break;
4510 		}
4511 
4512 		totlen = io->pfrio_size * sizeof(struct pfr_table);
4513 		pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4514 		    M_TEMP, M_WAITOK);
4515 		error = copyin(io->pfrio_buffer, pfrts, totlen);
4516 		if (error) {
4517 			free(pfrts, M_TEMP);
4518 			break;
4519 		}
4520 		PF_RULES_WLOCK();
4521 		error = pfr_del_tables(pfrts, io->pfrio_size,
4522 		    &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4523 		PF_RULES_WUNLOCK();
4524 		free(pfrts, M_TEMP);
4525 		break;
4526 	}
4527 
4528 	case DIOCRGETTABLES: {
4529 		struct pfioc_table *io = (struct pfioc_table *)addr;
4530 		struct pfr_table *pfrts;
4531 		size_t totlen;
4532 		int n;
4533 
4534 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
4535 			error = ENODEV;
4536 			break;
4537 		}
4538 		PF_RULES_RLOCK();
4539 		n = pfr_table_count(&io->pfrio_table, io->pfrio_flags);
4540 		if (n < 0) {
4541 			PF_RULES_RUNLOCK();
4542 			error = EINVAL;
4543 			break;
4544 		}
4545 		io->pfrio_size = min(io->pfrio_size, n);
4546 
4547 		totlen = io->pfrio_size * sizeof(struct pfr_table);
4548 
4549 		pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4550 		    M_TEMP, M_NOWAIT | M_ZERO);
4551 		if (pfrts == NULL) {
4552 			error = ENOMEM;
4553 			PF_RULES_RUNLOCK();
4554 			break;
4555 		}
4556 		error = pfr_get_tables(&io->pfrio_table, pfrts,
4557 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4558 		PF_RULES_RUNLOCK();
4559 		if (error == 0)
4560 			error = copyout(pfrts, io->pfrio_buffer, totlen);
4561 		free(pfrts, M_TEMP);
4562 		break;
4563 	}
4564 
4565 	case DIOCRGETTSTATS: {
4566 		struct pfioc_table *io = (struct pfioc_table *)addr;
4567 		struct pfr_tstats *pfrtstats;
4568 		size_t totlen;
4569 		int n;
4570 
4571 		if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
4572 			error = ENODEV;
4573 			break;
4574 		}
4575 		PF_TABLE_STATS_LOCK();
4576 		PF_RULES_RLOCK();
4577 		n = pfr_table_count(&io->pfrio_table, io->pfrio_flags);
4578 		if (n < 0) {
4579 			PF_RULES_RUNLOCK();
4580 			PF_TABLE_STATS_UNLOCK();
4581 			error = EINVAL;
4582 			break;
4583 		}
4584 		io->pfrio_size = min(io->pfrio_size, n);
4585 
4586 		totlen = io->pfrio_size * sizeof(struct pfr_tstats);
4587 		pfrtstats = mallocarray(io->pfrio_size,
4588 		    sizeof(struct pfr_tstats), M_TEMP, M_NOWAIT | M_ZERO);
4589 		if (pfrtstats == NULL) {
4590 			error = ENOMEM;
4591 			PF_RULES_RUNLOCK();
4592 			PF_TABLE_STATS_UNLOCK();
4593 			break;
4594 		}
4595 		error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
4596 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4597 		PF_RULES_RUNLOCK();
4598 		PF_TABLE_STATS_UNLOCK();
4599 		if (error == 0)
4600 			error = copyout(pfrtstats, io->pfrio_buffer, totlen);
4601 		free(pfrtstats, M_TEMP);
4602 		break;
4603 	}
4604 
4605 	case DIOCRCLRTSTATS: {
4606 		struct pfioc_table *io = (struct pfioc_table *)addr;
4607 		struct pfr_table *pfrts;
4608 		size_t totlen;
4609 
4610 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
4611 			error = ENODEV;
4612 			break;
4613 		}
4614 
4615 		if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount ||
4616 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) {
4617 			/* We used to count tables and use the minimum required
4618 			 * size, so we didn't fail on overly large requests.
4619 			 * Keep doing so. */
4620 			io->pfrio_size = pf_ioctl_maxcount;
4621 			break;
4622 		}
4623 
4624 		totlen = io->pfrio_size * sizeof(struct pfr_table);
4625 		pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4626 		    M_TEMP, M_WAITOK);
4627 		error = copyin(io->pfrio_buffer, pfrts, totlen);
4628 		if (error) {
4629 			free(pfrts, M_TEMP);
4630 			break;
4631 		}
4632 
4633 		PF_TABLE_STATS_LOCK();
4634 		PF_RULES_RLOCK();
4635 		error = pfr_clr_tstats(pfrts, io->pfrio_size,
4636 		    &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4637 		PF_RULES_RUNLOCK();
4638 		PF_TABLE_STATS_UNLOCK();
4639 		free(pfrts, M_TEMP);
4640 		break;
4641 	}
4642 
4643 	case DIOCRSETTFLAGS: {
4644 		struct pfioc_table *io = (struct pfioc_table *)addr;
4645 		struct pfr_table *pfrts;
4646 		size_t totlen;
4647 		int n;
4648 
4649 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
4650 			error = ENODEV;
4651 			break;
4652 		}
4653 
4654 		PF_RULES_RLOCK();
4655 		n = pfr_table_count(&io->pfrio_table, io->pfrio_flags);
4656 		if (n < 0) {
4657 			PF_RULES_RUNLOCK();
4658 			error = EINVAL;
4659 			break;
4660 		}
4661 
4662 		io->pfrio_size = min(io->pfrio_size, n);
4663 		PF_RULES_RUNLOCK();
4664 
4665 		totlen = io->pfrio_size * sizeof(struct pfr_table);
4666 		pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4667 		    M_TEMP, M_WAITOK);
4668 		error = copyin(io->pfrio_buffer, pfrts, totlen);
4669 		if (error) {
4670 			free(pfrts, M_TEMP);
4671 			break;
4672 		}
4673 		PF_RULES_WLOCK();
4674 		error = pfr_set_tflags(pfrts, io->pfrio_size,
4675 		    io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
4676 		    &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4677 		PF_RULES_WUNLOCK();
4678 		free(pfrts, M_TEMP);
4679 		break;
4680 	}
4681 
4682 	case DIOCRCLRADDRS: {
4683 		struct pfioc_table *io = (struct pfioc_table *)addr;
4684 
4685 		if (io->pfrio_esize != 0) {
4686 			error = ENODEV;
4687 			break;
4688 		}
4689 		PF_RULES_WLOCK();
4690 		error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
4691 		    io->pfrio_flags | PFR_FLAG_USERIOCTL);
4692 		PF_RULES_WUNLOCK();
4693 		break;
4694 	}
4695 
4696 	case DIOCRADDADDRS: {
4697 		struct pfioc_table *io = (struct pfioc_table *)addr;
4698 		struct pfr_addr *pfras;
4699 		size_t totlen;
4700 
4701 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4702 			error = ENODEV;
4703 			break;
4704 		}
4705 		if (io->pfrio_size < 0 ||
4706 		    io->pfrio_size > pf_ioctl_maxcount ||
4707 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4708 			error = EINVAL;
4709 			break;
4710 		}
4711 		totlen = io->pfrio_size * sizeof(struct pfr_addr);
4712 		pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4713 		    M_TEMP, M_WAITOK);
4714 		error = copyin(io->pfrio_buffer, pfras, totlen);
4715 		if (error) {
4716 			free(pfras, M_TEMP);
4717 			break;
4718 		}
4719 		PF_RULES_WLOCK();
4720 		error = pfr_add_addrs(&io->pfrio_table, pfras,
4721 		    io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
4722 		    PFR_FLAG_USERIOCTL);
4723 		PF_RULES_WUNLOCK();
4724 		if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
4725 			error = copyout(pfras, io->pfrio_buffer, totlen);
4726 		free(pfras, M_TEMP);
4727 		break;
4728 	}
4729 
4730 	case DIOCRDELADDRS: {
4731 		struct pfioc_table *io = (struct pfioc_table *)addr;
4732 		struct pfr_addr *pfras;
4733 		size_t totlen;
4734 
4735 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4736 			error = ENODEV;
4737 			break;
4738 		}
4739 		if (io->pfrio_size < 0 ||
4740 		    io->pfrio_size > pf_ioctl_maxcount ||
4741 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4742 			error = EINVAL;
4743 			break;
4744 		}
4745 		totlen = io->pfrio_size * sizeof(struct pfr_addr);
4746 		pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4747 		    M_TEMP, M_WAITOK);
4748 		error = copyin(io->pfrio_buffer, pfras, totlen);
4749 		if (error) {
4750 			free(pfras, M_TEMP);
4751 			break;
4752 		}
4753 		PF_RULES_WLOCK();
4754 		error = pfr_del_addrs(&io->pfrio_table, pfras,
4755 		    io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
4756 		    PFR_FLAG_USERIOCTL);
4757 		PF_RULES_WUNLOCK();
4758 		if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
4759 			error = copyout(pfras, io->pfrio_buffer, totlen);
4760 		free(pfras, M_TEMP);
4761 		break;
4762 	}
4763 
4764 	case DIOCRSETADDRS: {
4765 		struct pfioc_table *io = (struct pfioc_table *)addr;
4766 		struct pfr_addr *pfras;
4767 		size_t totlen, count;
4768 
4769 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4770 			error = ENODEV;
4771 			break;
4772 		}
4773 		if (io->pfrio_size < 0 || io->pfrio_size2 < 0) {
4774 			error = EINVAL;
4775 			break;
4776 		}
4777 		count = max(io->pfrio_size, io->pfrio_size2);
4778 		if (count > pf_ioctl_maxcount ||
4779 		    WOULD_OVERFLOW(count, sizeof(struct pfr_addr))) {
4780 			error = EINVAL;
4781 			break;
4782 		}
4783 		totlen = count * sizeof(struct pfr_addr);
4784 		pfras = mallocarray(count, sizeof(struct pfr_addr), M_TEMP,
4785 		    M_WAITOK);
4786 		error = copyin(io->pfrio_buffer, pfras, totlen);
4787 		if (error) {
4788 			free(pfras, M_TEMP);
4789 			break;
4790 		}
4791 		PF_RULES_WLOCK();
4792 		error = pfr_set_addrs(&io->pfrio_table, pfras,
4793 		    io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
4794 		    &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
4795 		    PFR_FLAG_USERIOCTL, 0);
4796 		PF_RULES_WUNLOCK();
4797 		if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
4798 			error = copyout(pfras, io->pfrio_buffer, totlen);
4799 		free(pfras, M_TEMP);
4800 		break;
4801 	}
4802 
4803 	case DIOCRGETADDRS: {
4804 		struct pfioc_table *io = (struct pfioc_table *)addr;
4805 		struct pfr_addr *pfras;
4806 		size_t totlen;
4807 
4808 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4809 			error = ENODEV;
4810 			break;
4811 		}
4812 		if (io->pfrio_size < 0 ||
4813 		    io->pfrio_size > pf_ioctl_maxcount ||
4814 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4815 			error = EINVAL;
4816 			break;
4817 		}
4818 		totlen = io->pfrio_size * sizeof(struct pfr_addr);
4819 		pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4820 		    M_TEMP, M_WAITOK | M_ZERO);
4821 		PF_RULES_RLOCK();
4822 		error = pfr_get_addrs(&io->pfrio_table, pfras,
4823 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4824 		PF_RULES_RUNLOCK();
4825 		if (error == 0)
4826 			error = copyout(pfras, io->pfrio_buffer, totlen);
4827 		free(pfras, M_TEMP);
4828 		break;
4829 	}
4830 
4831 	case DIOCRGETASTATS: {
4832 		struct pfioc_table *io = (struct pfioc_table *)addr;
4833 		struct pfr_astats *pfrastats;
4834 		size_t totlen;
4835 
4836 		if (io->pfrio_esize != sizeof(struct pfr_astats)) {
4837 			error = ENODEV;
4838 			break;
4839 		}
4840 		if (io->pfrio_size < 0 ||
4841 		    io->pfrio_size > pf_ioctl_maxcount ||
4842 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_astats))) {
4843 			error = EINVAL;
4844 			break;
4845 		}
4846 		totlen = io->pfrio_size * sizeof(struct pfr_astats);
4847 		pfrastats = mallocarray(io->pfrio_size,
4848 		    sizeof(struct pfr_astats), M_TEMP, M_WAITOK | M_ZERO);
4849 		PF_RULES_RLOCK();
4850 		error = pfr_get_astats(&io->pfrio_table, pfrastats,
4851 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4852 		PF_RULES_RUNLOCK();
4853 		if (error == 0)
4854 			error = copyout(pfrastats, io->pfrio_buffer, totlen);
4855 		free(pfrastats, M_TEMP);
4856 		break;
4857 	}
4858 
4859 	case DIOCRCLRASTATS: {
4860 		struct pfioc_table *io = (struct pfioc_table *)addr;
4861 		struct pfr_addr *pfras;
4862 		size_t totlen;
4863 
4864 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4865 			error = ENODEV;
4866 			break;
4867 		}
4868 		if (io->pfrio_size < 0 ||
4869 		    io->pfrio_size > pf_ioctl_maxcount ||
4870 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4871 			error = EINVAL;
4872 			break;
4873 		}
4874 		totlen = io->pfrio_size * sizeof(struct pfr_addr);
4875 		pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4876 		    M_TEMP, M_WAITOK);
4877 		error = copyin(io->pfrio_buffer, pfras, totlen);
4878 		if (error) {
4879 			free(pfras, M_TEMP);
4880 			break;
4881 		}
4882 		PF_RULES_WLOCK();
4883 		error = pfr_clr_astats(&io->pfrio_table, pfras,
4884 		    io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
4885 		    PFR_FLAG_USERIOCTL);
4886 		PF_RULES_WUNLOCK();
4887 		if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
4888 			error = copyout(pfras, io->pfrio_buffer, totlen);
4889 		free(pfras, M_TEMP);
4890 		break;
4891 	}
4892 
4893 	case DIOCRTSTADDRS: {
4894 		struct pfioc_table *io = (struct pfioc_table *)addr;
4895 		struct pfr_addr *pfras;
4896 		size_t totlen;
4897 
4898 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4899 			error = ENODEV;
4900 			break;
4901 		}
4902 		if (io->pfrio_size < 0 ||
4903 		    io->pfrio_size > pf_ioctl_maxcount ||
4904 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4905 			error = EINVAL;
4906 			break;
4907 		}
4908 		totlen = io->pfrio_size * sizeof(struct pfr_addr);
4909 		pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4910 		    M_TEMP, M_WAITOK);
4911 		error = copyin(io->pfrio_buffer, pfras, totlen);
4912 		if (error) {
4913 			free(pfras, M_TEMP);
4914 			break;
4915 		}
4916 		PF_RULES_RLOCK();
4917 		error = pfr_tst_addrs(&io->pfrio_table, pfras,
4918 		    io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
4919 		    PFR_FLAG_USERIOCTL);
4920 		PF_RULES_RUNLOCK();
4921 		if (error == 0)
4922 			error = copyout(pfras, io->pfrio_buffer, totlen);
4923 		free(pfras, M_TEMP);
4924 		break;
4925 	}
4926 
4927 	case DIOCRINADEFINE: {
4928 		struct pfioc_table *io = (struct pfioc_table *)addr;
4929 		struct pfr_addr *pfras;
4930 		size_t totlen;
4931 
4932 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4933 			error = ENODEV;
4934 			break;
4935 		}
4936 		if (io->pfrio_size < 0 ||
4937 		    io->pfrio_size > pf_ioctl_maxcount ||
4938 		    WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4939 			error = EINVAL;
4940 			break;
4941 		}
4942 		totlen = io->pfrio_size * sizeof(struct pfr_addr);
4943 		pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4944 		    M_TEMP, M_WAITOK);
4945 		error = copyin(io->pfrio_buffer, pfras, totlen);
4946 		if (error) {
4947 			free(pfras, M_TEMP);
4948 			break;
4949 		}
4950 		PF_RULES_WLOCK();
4951 		error = pfr_ina_define(&io->pfrio_table, pfras,
4952 		    io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
4953 		    io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4954 		PF_RULES_WUNLOCK();
4955 		free(pfras, M_TEMP);
4956 		break;
4957 	}
4958 
4959 	case DIOCOSFPADD: {
4960 		struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
4961 		PF_RULES_WLOCK();
4962 		error = pf_osfp_add(io);
4963 		PF_RULES_WUNLOCK();
4964 		break;
4965 	}
4966 
4967 	case DIOCOSFPGET: {
4968 		struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
4969 		PF_RULES_RLOCK();
4970 		error = pf_osfp_get(io);
4971 		PF_RULES_RUNLOCK();
4972 		break;
4973 	}
4974 
4975 	case DIOCXBEGIN: {
4976 		struct pfioc_trans	*io = (struct pfioc_trans *)addr;
4977 		struct pfioc_trans_e	*ioes, *ioe;
4978 		size_t			 totlen;
4979 		int			 i;
4980 
4981 		if (io->esize != sizeof(*ioe)) {
4982 			error = ENODEV;
4983 			break;
4984 		}
4985 		if (io->size < 0 ||
4986 		    io->size > pf_ioctl_maxcount ||
4987 		    WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) {
4988 			error = EINVAL;
4989 			break;
4990 		}
4991 		totlen = sizeof(struct pfioc_trans_e) * io->size;
4992 		ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
4993 		    M_TEMP, M_WAITOK);
4994 		error = copyin(io->array, ioes, totlen);
4995 		if (error) {
4996 			free(ioes, M_TEMP);
4997 			break;
4998 		}
4999 		/* Ensure there's no more ethernet rules to clean up. */
5000 		NET_EPOCH_DRAIN_CALLBACKS();
5001 		PF_RULES_WLOCK();
5002 		for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5003 			ioe->anchor[sizeof(ioe->anchor) - 1] = '\0';
5004 			switch (ioe->rs_num) {
5005 			case PF_RULESET_ETH:
5006 				if ((error = pf_begin_eth(&ioe->ticket, ioe->anchor))) {
5007 					PF_RULES_WUNLOCK();
5008 					free(ioes, M_TEMP);
5009 					goto fail;
5010 				}
5011 				break;
5012 #ifdef ALTQ
5013 			case PF_RULESET_ALTQ:
5014 				if (ioe->anchor[0]) {
5015 					PF_RULES_WUNLOCK();
5016 					free(ioes, M_TEMP);
5017 					error = EINVAL;
5018 					goto fail;
5019 				}
5020 				if ((error = pf_begin_altq(&ioe->ticket))) {
5021 					PF_RULES_WUNLOCK();
5022 					free(ioes, M_TEMP);
5023 					goto fail;
5024 				}
5025 				break;
5026 #endif /* ALTQ */
5027 			case PF_RULESET_TABLE:
5028 			    {
5029 				struct pfr_table table;
5030 
5031 				bzero(&table, sizeof(table));
5032 				strlcpy(table.pfrt_anchor, ioe->anchor,
5033 				    sizeof(table.pfrt_anchor));
5034 				if ((error = pfr_ina_begin(&table,
5035 				    &ioe->ticket, NULL, 0))) {
5036 					PF_RULES_WUNLOCK();
5037 					free(ioes, M_TEMP);
5038 					goto fail;
5039 				}
5040 				break;
5041 			    }
5042 			default:
5043 				if ((error = pf_begin_rules(&ioe->ticket,
5044 				    ioe->rs_num, ioe->anchor))) {
5045 					PF_RULES_WUNLOCK();
5046 					free(ioes, M_TEMP);
5047 					goto fail;
5048 				}
5049 				break;
5050 			}
5051 		}
5052 		PF_RULES_WUNLOCK();
5053 		error = copyout(ioes, io->array, totlen);
5054 		free(ioes, M_TEMP);
5055 		break;
5056 	}
5057 
5058 	case DIOCXROLLBACK: {
5059 		struct pfioc_trans	*io = (struct pfioc_trans *)addr;
5060 		struct pfioc_trans_e	*ioe, *ioes;
5061 		size_t			 totlen;
5062 		int			 i;
5063 
5064 		if (io->esize != sizeof(*ioe)) {
5065 			error = ENODEV;
5066 			break;
5067 		}
5068 		if (io->size < 0 ||
5069 		    io->size > pf_ioctl_maxcount ||
5070 		    WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) {
5071 			error = EINVAL;
5072 			break;
5073 		}
5074 		totlen = sizeof(struct pfioc_trans_e) * io->size;
5075 		ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
5076 		    M_TEMP, M_WAITOK);
5077 		error = copyin(io->array, ioes, totlen);
5078 		if (error) {
5079 			free(ioes, M_TEMP);
5080 			break;
5081 		}
5082 		PF_RULES_WLOCK();
5083 		for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5084 			ioe->anchor[sizeof(ioe->anchor) - 1] = '\0';
5085 			switch (ioe->rs_num) {
5086 			case PF_RULESET_ETH:
5087 				if ((error = pf_rollback_eth(ioe->ticket,
5088 				    ioe->anchor))) {
5089 					PF_RULES_WUNLOCK();
5090 					free(ioes, M_TEMP);
5091 					goto fail; /* really bad */
5092 				}
5093 				break;
5094 #ifdef ALTQ
5095 			case PF_RULESET_ALTQ:
5096 				if (ioe->anchor[0]) {
5097 					PF_RULES_WUNLOCK();
5098 					free(ioes, M_TEMP);
5099 					error = EINVAL;
5100 					goto fail;
5101 				}
5102 				if ((error = pf_rollback_altq(ioe->ticket))) {
5103 					PF_RULES_WUNLOCK();
5104 					free(ioes, M_TEMP);
5105 					goto fail; /* really bad */
5106 				}
5107 				break;
5108 #endif /* ALTQ */
5109 			case PF_RULESET_TABLE:
5110 			    {
5111 				struct pfr_table table;
5112 
5113 				bzero(&table, sizeof(table));
5114 				strlcpy(table.pfrt_anchor, ioe->anchor,
5115 				    sizeof(table.pfrt_anchor));
5116 				if ((error = pfr_ina_rollback(&table,
5117 				    ioe->ticket, NULL, 0))) {
5118 					PF_RULES_WUNLOCK();
5119 					free(ioes, M_TEMP);
5120 					goto fail; /* really bad */
5121 				}
5122 				break;
5123 			    }
5124 			default:
5125 				if ((error = pf_rollback_rules(ioe->ticket,
5126 				    ioe->rs_num, ioe->anchor))) {
5127 					PF_RULES_WUNLOCK();
5128 					free(ioes, M_TEMP);
5129 					goto fail; /* really bad */
5130 				}
5131 				break;
5132 			}
5133 		}
5134 		PF_RULES_WUNLOCK();
5135 		free(ioes, M_TEMP);
5136 		break;
5137 	}
5138 
5139 	case DIOCXCOMMIT: {
5140 		struct pfioc_trans	*io = (struct pfioc_trans *)addr;
5141 		struct pfioc_trans_e	*ioe, *ioes;
5142 		struct pf_kruleset	*rs;
5143 		struct pf_keth_ruleset	*ers;
5144 		size_t			 totlen;
5145 		int			 i;
5146 
5147 		if (io->esize != sizeof(*ioe)) {
5148 			error = ENODEV;
5149 			break;
5150 		}
5151 
5152 		if (io->size < 0 ||
5153 		    io->size > pf_ioctl_maxcount ||
5154 		    WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) {
5155 			error = EINVAL;
5156 			break;
5157 		}
5158 
5159 		totlen = sizeof(struct pfioc_trans_e) * io->size;
5160 		ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
5161 		    M_TEMP, M_WAITOK);
5162 		error = copyin(io->array, ioes, totlen);
5163 		if (error) {
5164 			free(ioes, M_TEMP);
5165 			break;
5166 		}
5167 		PF_RULES_WLOCK();
5168 		/* First makes sure everything will succeed. */
5169 		for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5170 			ioe->anchor[sizeof(ioe->anchor) - 1] = 0;
5171 			switch (ioe->rs_num) {
5172 			case PF_RULESET_ETH:
5173 				ers = pf_find_keth_ruleset(ioe->anchor);
5174 				if (ers == NULL || ioe->ticket == 0 ||
5175 				    ioe->ticket != ers->inactive.ticket) {
5176 					PF_RULES_WUNLOCK();
5177 					free(ioes, M_TEMP);
5178 					error = EINVAL;
5179 					goto fail;
5180 				}
5181 				break;
5182 #ifdef ALTQ
5183 			case PF_RULESET_ALTQ:
5184 				if (ioe->anchor[0]) {
5185 					PF_RULES_WUNLOCK();
5186 					free(ioes, M_TEMP);
5187 					error = EINVAL;
5188 					goto fail;
5189 				}
5190 				if (!V_altqs_inactive_open || ioe->ticket !=
5191 				    V_ticket_altqs_inactive) {
5192 					PF_RULES_WUNLOCK();
5193 					free(ioes, M_TEMP);
5194 					error = EBUSY;
5195 					goto fail;
5196 				}
5197 				break;
5198 #endif /* ALTQ */
5199 			case PF_RULESET_TABLE:
5200 				rs = pf_find_kruleset(ioe->anchor);
5201 				if (rs == NULL || !rs->topen || ioe->ticket !=
5202 				    rs->tticket) {
5203 					PF_RULES_WUNLOCK();
5204 					free(ioes, M_TEMP);
5205 					error = EBUSY;
5206 					goto fail;
5207 				}
5208 				break;
5209 			default:
5210 				if (ioe->rs_num < 0 || ioe->rs_num >=
5211 				    PF_RULESET_MAX) {
5212 					PF_RULES_WUNLOCK();
5213 					free(ioes, M_TEMP);
5214 					error = EINVAL;
5215 					goto fail;
5216 				}
5217 				rs = pf_find_kruleset(ioe->anchor);
5218 				if (rs == NULL ||
5219 				    !rs->rules[ioe->rs_num].inactive.open ||
5220 				    rs->rules[ioe->rs_num].inactive.ticket !=
5221 				    ioe->ticket) {
5222 					PF_RULES_WUNLOCK();
5223 					free(ioes, M_TEMP);
5224 					error = EBUSY;
5225 					goto fail;
5226 				}
5227 				break;
5228 			}
5229 		}
5230 		/* Now do the commit - no errors should happen here. */
5231 		for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5232 			switch (ioe->rs_num) {
5233 			case PF_RULESET_ETH:
5234 				if ((error = pf_commit_eth(ioe->ticket, ioe->anchor))) {
5235 					PF_RULES_WUNLOCK();
5236 					free(ioes, M_TEMP);
5237 					goto fail; /* really bad */
5238 				}
5239 				break;
5240 #ifdef ALTQ
5241 			case PF_RULESET_ALTQ:
5242 				if ((error = pf_commit_altq(ioe->ticket))) {
5243 					PF_RULES_WUNLOCK();
5244 					free(ioes, M_TEMP);
5245 					goto fail; /* really bad */
5246 				}
5247 				break;
5248 #endif /* ALTQ */
5249 			case PF_RULESET_TABLE:
5250 			    {
5251 				struct pfr_table table;
5252 
5253 				bzero(&table, sizeof(table));
5254 				(void)strlcpy(table.pfrt_anchor, ioe->anchor,
5255 				    sizeof(table.pfrt_anchor));
5256 				if ((error = pfr_ina_commit(&table,
5257 				    ioe->ticket, NULL, NULL, 0))) {
5258 					PF_RULES_WUNLOCK();
5259 					free(ioes, M_TEMP);
5260 					goto fail; /* really bad */
5261 				}
5262 				break;
5263 			    }
5264 			default:
5265 				if ((error = pf_commit_rules(ioe->ticket,
5266 				    ioe->rs_num, ioe->anchor))) {
5267 					PF_RULES_WUNLOCK();
5268 					free(ioes, M_TEMP);
5269 					goto fail; /* really bad */
5270 				}
5271 				break;
5272 			}
5273 		}
5274 		PF_RULES_WUNLOCK();
5275 
5276 		/* Only hook into EtherNet taffic if we've got rules for it. */
5277 		if (! TAILQ_EMPTY(V_pf_keth->active.rules))
5278 			hook_pf_eth();
5279 		else
5280 			dehook_pf_eth();
5281 
5282 		free(ioes, M_TEMP);
5283 		break;
5284 	}
5285 
5286 	case DIOCGETSRCNODES: {
5287 		struct pfioc_src_nodes	*psn = (struct pfioc_src_nodes *)addr;
5288 		struct pf_srchash	*sh;
5289 		struct pf_ksrc_node	*n;
5290 		struct pf_src_node	*p, *pstore;
5291 		uint32_t		 i, nr = 0;
5292 
5293 		for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
5294 				i++, sh++) {
5295 			PF_HASHROW_LOCK(sh);
5296 			LIST_FOREACH(n, &sh->nodes, entry)
5297 				nr++;
5298 			PF_HASHROW_UNLOCK(sh);
5299 		}
5300 
5301 		psn->psn_len = min(psn->psn_len,
5302 		    sizeof(struct pf_src_node) * nr);
5303 
5304 		if (psn->psn_len == 0) {
5305 			psn->psn_len = sizeof(struct pf_src_node) * nr;
5306 			break;
5307 		}
5308 
5309 		nr = 0;
5310 
5311 		p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK | M_ZERO);
5312 		for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
5313 		    i++, sh++) {
5314 		    PF_HASHROW_LOCK(sh);
5315 		    LIST_FOREACH(n, &sh->nodes, entry) {
5316 
5317 			if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
5318 				break;
5319 
5320 			pf_src_node_copy(n, p);
5321 
5322 			p++;
5323 			nr++;
5324 		    }
5325 		    PF_HASHROW_UNLOCK(sh);
5326 		}
5327 		error = copyout(pstore, psn->psn_src_nodes,
5328 		    sizeof(struct pf_src_node) * nr);
5329 		if (error) {
5330 			free(pstore, M_TEMP);
5331 			break;
5332 		}
5333 		psn->psn_len = sizeof(struct pf_src_node) * nr;
5334 		free(pstore, M_TEMP);
5335 		break;
5336 	}
5337 
5338 	case DIOCCLRSRCNODES: {
5339 		pf_clear_srcnodes(NULL);
5340 		pf_purge_expired_src_nodes();
5341 		break;
5342 	}
5343 
5344 	case DIOCKILLSRCNODES:
5345 		pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
5346 		break;
5347 
5348 #ifdef COMPAT_FREEBSD13
5349 	case DIOCKEEPCOUNTERS_FREEBSD13:
5350 #endif
5351 	case DIOCKEEPCOUNTERS:
5352 		error = pf_keepcounters((struct pfioc_nv *)addr);
5353 		break;
5354 
5355 	case DIOCGETSYNCOOKIES:
5356 		error = pf_get_syncookies((struct pfioc_nv *)addr);
5357 		break;
5358 
5359 	case DIOCSETSYNCOOKIES:
5360 		error = pf_set_syncookies((struct pfioc_nv *)addr);
5361 		break;
5362 
5363 	case DIOCSETHOSTID: {
5364 		u_int32_t	*hostid = (u_int32_t *)addr;
5365 
5366 		PF_RULES_WLOCK();
5367 		if (*hostid == 0)
5368 			V_pf_status.hostid = arc4random();
5369 		else
5370 			V_pf_status.hostid = *hostid;
5371 		PF_RULES_WUNLOCK();
5372 		break;
5373 	}
5374 
5375 	case DIOCOSFPFLUSH:
5376 		PF_RULES_WLOCK();
5377 		pf_osfp_flush();
5378 		PF_RULES_WUNLOCK();
5379 		break;
5380 
5381 	case DIOCIGETIFACES: {
5382 		struct pfioc_iface *io = (struct pfioc_iface *)addr;
5383 		struct pfi_kif *ifstore;
5384 		size_t bufsiz;
5385 
5386 		if (io->pfiio_esize != sizeof(struct pfi_kif)) {
5387 			error = ENODEV;
5388 			break;
5389 		}
5390 
5391 		if (io->pfiio_size < 0 ||
5392 		    io->pfiio_size > pf_ioctl_maxcount ||
5393 		    WOULD_OVERFLOW(io->pfiio_size, sizeof(struct pfi_kif))) {
5394 			error = EINVAL;
5395 			break;
5396 		}
5397 
5398 		io->pfiio_name[sizeof(io->pfiio_name) - 1] = '\0';
5399 
5400 		bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
5401 		ifstore = mallocarray(io->pfiio_size, sizeof(struct pfi_kif),
5402 		    M_TEMP, M_WAITOK | M_ZERO);
5403 
5404 		PF_RULES_RLOCK();
5405 		pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
5406 		PF_RULES_RUNLOCK();
5407 		error = copyout(ifstore, io->pfiio_buffer, bufsiz);
5408 		free(ifstore, M_TEMP);
5409 		break;
5410 	}
5411 
5412 	case DIOCSETIFFLAG: {
5413 		struct pfioc_iface *io = (struct pfioc_iface *)addr;
5414 
5415 		io->pfiio_name[sizeof(io->pfiio_name) - 1] = '\0';
5416 
5417 		PF_RULES_WLOCK();
5418 		error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
5419 		PF_RULES_WUNLOCK();
5420 		break;
5421 	}
5422 
5423 	case DIOCCLRIFFLAG: {
5424 		struct pfioc_iface *io = (struct pfioc_iface *)addr;
5425 
5426 		io->pfiio_name[sizeof(io->pfiio_name) - 1] = '\0';
5427 
5428 		PF_RULES_WLOCK();
5429 		error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
5430 		PF_RULES_WUNLOCK();
5431 		break;
5432 	}
5433 
5434 	case DIOCSETREASS: {
5435 		u_int32_t	*reass = (u_int32_t *)addr;
5436 
5437 		V_pf_status.reass = *reass & (PF_REASS_ENABLED|PF_REASS_NODF);
5438 		/* Removal of DF flag without reassembly enabled is not a
5439 		 * valid combination. Disable reassembly in such case. */
5440 		if (!(V_pf_status.reass & PF_REASS_ENABLED))
5441 			V_pf_status.reass = 0;
5442 		break;
5443 	}
5444 
5445 	default:
5446 		error = ENODEV;
5447 		break;
5448 	}
5449 fail:
5450 	CURVNET_RESTORE();
5451 
5452 #undef ERROUT_IOCTL
5453 
5454 	return (error);
5455 }
5456 
5457 void
5458 pfsync_state_export(union pfsync_state_union *sp, struct pf_kstate *st, int msg_version)
5459 {
5460 	bzero(sp, sizeof(union pfsync_state_union));
5461 
5462 	/* copy from state key */
5463 	sp->pfs_1301.key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
5464 	sp->pfs_1301.key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
5465 	sp->pfs_1301.key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
5466 	sp->pfs_1301.key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
5467 	sp->pfs_1301.key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
5468 	sp->pfs_1301.key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
5469 	sp->pfs_1301.key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
5470 	sp->pfs_1301.key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
5471 	sp->pfs_1301.proto = st->key[PF_SK_WIRE]->proto;
5472 	sp->pfs_1301.af = st->key[PF_SK_WIRE]->af;
5473 
5474 	/* copy from state */
5475 	strlcpy(sp->pfs_1301.ifname, st->kif->pfik_name, sizeof(sp->pfs_1301.ifname));
5476 	bcopy(&st->rt_addr, &sp->pfs_1301.rt_addr, sizeof(sp->pfs_1301.rt_addr));
5477 	sp->pfs_1301.creation = htonl(time_uptime - st->creation);
5478 	sp->pfs_1301.expire = pf_state_expires(st);
5479 	if (sp->pfs_1301.expire <= time_uptime)
5480 		sp->pfs_1301.expire = htonl(0);
5481 	else
5482 		sp->pfs_1301.expire = htonl(sp->pfs_1301.expire - time_uptime);
5483 
5484 	sp->pfs_1301.direction = st->direction;
5485 	sp->pfs_1301.log = st->act.log;
5486 	sp->pfs_1301.timeout = st->timeout;
5487 
5488 	switch (msg_version) {
5489 		case PFSYNC_MSG_VERSION_1301:
5490 			sp->pfs_1301.state_flags = st->state_flags;
5491 			break;
5492 		case PFSYNC_MSG_VERSION_1400:
5493 			sp->pfs_1400.state_flags = htons(st->state_flags);
5494 			sp->pfs_1400.qid = htons(st->act.qid);
5495 			sp->pfs_1400.pqid = htons(st->act.pqid);
5496 			sp->pfs_1400.dnpipe = htons(st->act.dnpipe);
5497 			sp->pfs_1400.dnrpipe = htons(st->act.dnrpipe);
5498 			sp->pfs_1400.rtableid = htonl(st->act.rtableid);
5499 			sp->pfs_1400.min_ttl = st->act.min_ttl;
5500 			sp->pfs_1400.set_tos = st->act.set_tos;
5501 			sp->pfs_1400.max_mss = htons(st->act.max_mss);
5502 			sp->pfs_1400.set_prio[0] = st->act.set_prio[0];
5503 			sp->pfs_1400.set_prio[1] = st->act.set_prio[1];
5504 			sp->pfs_1400.rt = st->rt;
5505 			if (st->rt_kif)
5506 				strlcpy(sp->pfs_1400.rt_ifname,
5507 				    st->rt_kif->pfik_name,
5508 				    sizeof(sp->pfs_1400.rt_ifname));
5509 			break;
5510 		default:
5511 			panic("%s: Unsupported pfsync_msg_version %d",
5512 			    __func__, msg_version);
5513 	}
5514 
5515 	if (st->src_node)
5516 		sp->pfs_1301.sync_flags |= PFSYNC_FLAG_SRCNODE;
5517 	if (st->nat_src_node)
5518 		sp->pfs_1301.sync_flags |= PFSYNC_FLAG_NATSRCNODE;
5519 
5520 	sp->pfs_1301.id = st->id;
5521 	sp->pfs_1301.creatorid = st->creatorid;
5522 	pf_state_peer_hton(&st->src, &sp->pfs_1301.src);
5523 	pf_state_peer_hton(&st->dst, &sp->pfs_1301.dst);
5524 
5525 	if (st->rule.ptr == NULL)
5526 		sp->pfs_1301.rule = htonl(-1);
5527 	else
5528 		sp->pfs_1301.rule = htonl(st->rule.ptr->nr);
5529 	if (st->anchor.ptr == NULL)
5530 		sp->pfs_1301.anchor = htonl(-1);
5531 	else
5532 		sp->pfs_1301.anchor = htonl(st->anchor.ptr->nr);
5533 	if (st->nat_rule.ptr == NULL)
5534 		sp->pfs_1301.nat_rule = htonl(-1);
5535 	else
5536 		sp->pfs_1301.nat_rule = htonl(st->nat_rule.ptr->nr);
5537 
5538 	pf_state_counter_hton(st->packets[0], sp->pfs_1301.packets[0]);
5539 	pf_state_counter_hton(st->packets[1], sp->pfs_1301.packets[1]);
5540 	pf_state_counter_hton(st->bytes[0], sp->pfs_1301.bytes[0]);
5541 	pf_state_counter_hton(st->bytes[1], sp->pfs_1301.bytes[1]);
5542 }
5543 
5544 void
5545 pf_state_export(struct pf_state_export *sp, struct pf_kstate *st)
5546 {
5547 	bzero(sp, sizeof(*sp));
5548 
5549 	sp->version = PF_STATE_VERSION;
5550 
5551 	/* copy from state key */
5552 	sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
5553 	sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
5554 	sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
5555 	sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
5556 	sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
5557 	sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
5558 	sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
5559 	sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
5560 	sp->proto = st->key[PF_SK_WIRE]->proto;
5561 	sp->af = st->key[PF_SK_WIRE]->af;
5562 
5563 	/* copy from state */
5564 	strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
5565 	strlcpy(sp->orig_ifname, st->orig_kif->pfik_name,
5566 	    sizeof(sp->orig_ifname));
5567 	bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
5568 	sp->creation = htonl(time_uptime - st->creation);
5569 	sp->expire = pf_state_expires(st);
5570 	if (sp->expire <= time_uptime)
5571 		sp->expire = htonl(0);
5572 	else
5573 		sp->expire = htonl(sp->expire - time_uptime);
5574 
5575 	sp->direction = st->direction;
5576 	sp->log = st->act.log;
5577 	sp->timeout = st->timeout;
5578 	/* 8 bits for the old libpfctl, 16 bits for the new libpfctl */
5579 	sp->state_flags_compat = st->state_flags;
5580 	sp->state_flags = htons(st->state_flags);
5581 	if (st->src_node)
5582 		sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
5583 	if (st->nat_src_node)
5584 		sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
5585 
5586 	sp->id = st->id;
5587 	sp->creatorid = st->creatorid;
5588 	pf_state_peer_hton(&st->src, &sp->src);
5589 	pf_state_peer_hton(&st->dst, &sp->dst);
5590 
5591 	if (st->rule.ptr == NULL)
5592 		sp->rule = htonl(-1);
5593 	else
5594 		sp->rule = htonl(st->rule.ptr->nr);
5595 	if (st->anchor.ptr == NULL)
5596 		sp->anchor = htonl(-1);
5597 	else
5598 		sp->anchor = htonl(st->anchor.ptr->nr);
5599 	if (st->nat_rule.ptr == NULL)
5600 		sp->nat_rule = htonl(-1);
5601 	else
5602 		sp->nat_rule = htonl(st->nat_rule.ptr->nr);
5603 
5604 	sp->packets[0] = st->packets[0];
5605 	sp->packets[1] = st->packets[1];
5606 	sp->bytes[0] = st->bytes[0];
5607 	sp->bytes[1] = st->bytes[1];
5608 
5609 	sp->qid = htons(st->act.qid);
5610 	sp->pqid = htons(st->act.pqid);
5611 	sp->dnpipe = htons(st->act.dnpipe);
5612 	sp->dnrpipe = htons(st->act.dnrpipe);
5613 	sp->rtableid = htonl(st->act.rtableid);
5614 	sp->min_ttl = st->act.min_ttl;
5615 	sp->set_tos = st->act.set_tos;
5616 	sp->max_mss = htons(st->act.max_mss);
5617 	sp->rt = st->rt;
5618 	if (st->rt_kif)
5619 		strlcpy(sp->rt_ifname, st->rt_kif->pfik_name,
5620 		    sizeof(sp->rt_ifname));
5621 	sp->set_prio[0] = st->act.set_prio[0];
5622 	sp->set_prio[1] = st->act.set_prio[1];
5623 
5624 }
5625 
5626 static void
5627 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
5628 {
5629 	struct pfr_ktable *kt;
5630 
5631 	KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
5632 
5633 	kt = aw->p.tbl;
5634 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
5635 		kt = kt->pfrkt_root;
5636 	aw->p.tbl = NULL;
5637 	aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
5638 		kt->pfrkt_cnt : -1;
5639 }
5640 
5641 static int
5642 pf_add_status_counters(nvlist_t *nvl, const char *name, counter_u64_t *counters,
5643     size_t number, char **names)
5644 {
5645 	nvlist_t        *nvc;
5646 
5647 	nvc = nvlist_create(0);
5648 	if (nvc == NULL)
5649 		return (ENOMEM);
5650 
5651 	for (int i = 0; i < number; i++) {
5652 		nvlist_append_number_array(nvc, "counters",
5653 		    counter_u64_fetch(counters[i]));
5654 		nvlist_append_string_array(nvc, "names",
5655 		    names[i]);
5656 		nvlist_append_number_array(nvc, "ids",
5657 		    i);
5658 	}
5659 	nvlist_add_nvlist(nvl, name, nvc);
5660 	nvlist_destroy(nvc);
5661 
5662 	return (0);
5663 }
5664 
5665 static int
5666 pf_getstatus(struct pfioc_nv *nv)
5667 {
5668 	nvlist_t        *nvl = NULL, *nvc = NULL;
5669 	void            *nvlpacked = NULL;
5670 	int              error;
5671 	struct pf_status s;
5672 	char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
5673 	char *pf_lcounter[KLCNT_MAX+1] = KLCNT_NAMES;
5674 	char *pf_fcounter[FCNT_MAX+1] = FCNT_NAMES;
5675 	PF_RULES_RLOCK_TRACKER;
5676 
5677 #define ERROUT(x)      ERROUT_FUNCTION(errout, x)
5678 
5679 	PF_RULES_RLOCK();
5680 
5681 	nvl = nvlist_create(0);
5682 	if (nvl == NULL)
5683 		ERROUT(ENOMEM);
5684 
5685 	nvlist_add_bool(nvl, "running", V_pf_status.running);
5686 	nvlist_add_number(nvl, "since", V_pf_status.since);
5687 	nvlist_add_number(nvl, "debug", V_pf_status.debug);
5688 	nvlist_add_number(nvl, "hostid", V_pf_status.hostid);
5689 	nvlist_add_number(nvl, "states", V_pf_status.states);
5690 	nvlist_add_number(nvl, "src_nodes", V_pf_status.src_nodes);
5691 	nvlist_add_number(nvl, "reass", V_pf_status.reass);
5692 	nvlist_add_bool(nvl, "syncookies_active",
5693 	    V_pf_status.syncookies_active);
5694 	nvlist_add_number(nvl, "halfopen_states", V_pf_status.states_halfopen);
5695 
5696 	/* counters */
5697 	error = pf_add_status_counters(nvl, "counters", V_pf_status.counters,
5698 	    PFRES_MAX, pf_reasons);
5699 	if (error != 0)
5700 		ERROUT(error);
5701 
5702 	/* lcounters */
5703 	error = pf_add_status_counters(nvl, "lcounters", V_pf_status.lcounters,
5704 	    KLCNT_MAX, pf_lcounter);
5705 	if (error != 0)
5706 		ERROUT(error);
5707 
5708 	/* fcounters */
5709 	nvc = nvlist_create(0);
5710 	if (nvc == NULL)
5711 		ERROUT(ENOMEM);
5712 
5713 	for (int i = 0; i < FCNT_MAX; i++) {
5714 		nvlist_append_number_array(nvc, "counters",
5715 		    pf_counter_u64_fetch(&V_pf_status.fcounters[i]));
5716 		nvlist_append_string_array(nvc, "names",
5717 		    pf_fcounter[i]);
5718 		nvlist_append_number_array(nvc, "ids",
5719 		    i);
5720 	}
5721 	nvlist_add_nvlist(nvl, "fcounters", nvc);
5722 	nvlist_destroy(nvc);
5723 	nvc = NULL;
5724 
5725 	/* scounters */
5726 	error = pf_add_status_counters(nvl, "scounters", V_pf_status.scounters,
5727 	    SCNT_MAX, pf_fcounter);
5728 	if (error != 0)
5729 		ERROUT(error);
5730 
5731 	nvlist_add_string(nvl, "ifname", V_pf_status.ifname);
5732 	nvlist_add_binary(nvl, "chksum", V_pf_status.pf_chksum,
5733 	    PF_MD5_DIGEST_LENGTH);
5734 
5735 	pfi_update_status(V_pf_status.ifname, &s);
5736 
5737 	/* pcounters / bcounters */
5738 	for (int i = 0; i < 2; i++) {
5739 		for (int j = 0; j < 2; j++) {
5740 			for (int k = 0; k < 2; k++) {
5741 				nvlist_append_number_array(nvl, "pcounters",
5742 				    s.pcounters[i][j][k]);
5743 			}
5744 			nvlist_append_number_array(nvl, "bcounters",
5745 			    s.bcounters[i][j]);
5746 		}
5747 	}
5748 
5749 	nvlpacked = nvlist_pack(nvl, &nv->len);
5750 	if (nvlpacked == NULL)
5751 		ERROUT(ENOMEM);
5752 
5753 	if (nv->size == 0)
5754 		ERROUT(0);
5755 	else if (nv->size < nv->len)
5756 		ERROUT(ENOSPC);
5757 
5758 	PF_RULES_RUNLOCK();
5759 	error = copyout(nvlpacked, nv->data, nv->len);
5760 	goto done;
5761 
5762 #undef ERROUT
5763 errout:
5764 	PF_RULES_RUNLOCK();
5765 done:
5766 	free(nvlpacked, M_NVLIST);
5767 	nvlist_destroy(nvc);
5768 	nvlist_destroy(nvl);
5769 
5770 	return (error);
5771 }
5772 
5773 /*
5774  * XXX - Check for version mismatch!!!
5775  */
5776 static void
5777 pf_clear_all_states(void)
5778 {
5779 	struct pf_kstate	*s;
5780 	u_int i;
5781 
5782 	for (i = 0; i <= pf_hashmask; i++) {
5783 		struct pf_idhash *ih = &V_pf_idhash[i];
5784 relock:
5785 		PF_HASHROW_LOCK(ih);
5786 		LIST_FOREACH(s, &ih->states, entry) {
5787 			s->timeout = PFTM_PURGE;
5788 			/* Don't send out individual delete messages. */
5789 			s->state_flags |= PFSTATE_NOSYNC;
5790 			pf_unlink_state(s);
5791 			goto relock;
5792 		}
5793 		PF_HASHROW_UNLOCK(ih);
5794 	}
5795 }
5796 
5797 static int
5798 pf_clear_tables(void)
5799 {
5800 	struct pfioc_table io;
5801 	int error;
5802 
5803 	bzero(&io, sizeof(io));
5804 
5805 	error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
5806 	    io.pfrio_flags);
5807 
5808 	return (error);
5809 }
5810 
5811 static void
5812 pf_clear_srcnodes(struct pf_ksrc_node *n)
5813 {
5814 	struct pf_kstate *s;
5815 	int i;
5816 
5817 	for (i = 0; i <= pf_hashmask; i++) {
5818 		struct pf_idhash *ih = &V_pf_idhash[i];
5819 
5820 		PF_HASHROW_LOCK(ih);
5821 		LIST_FOREACH(s, &ih->states, entry) {
5822 			if (n == NULL || n == s->src_node)
5823 				s->src_node = NULL;
5824 			if (n == NULL || n == s->nat_src_node)
5825 				s->nat_src_node = NULL;
5826 		}
5827 		PF_HASHROW_UNLOCK(ih);
5828 	}
5829 
5830 	if (n == NULL) {
5831 		struct pf_srchash *sh;
5832 
5833 		for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
5834 		    i++, sh++) {
5835 			PF_HASHROW_LOCK(sh);
5836 			LIST_FOREACH(n, &sh->nodes, entry) {
5837 				n->expire = 1;
5838 				n->states = 0;
5839 			}
5840 			PF_HASHROW_UNLOCK(sh);
5841 		}
5842 	} else {
5843 		/* XXX: hash slot should already be locked here. */
5844 		n->expire = 1;
5845 		n->states = 0;
5846 	}
5847 }
5848 
5849 static void
5850 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
5851 {
5852 	struct pf_ksrc_node_list	 kill;
5853 
5854 	LIST_INIT(&kill);
5855 	for (int i = 0; i <= pf_srchashmask; i++) {
5856 		struct pf_srchash *sh = &V_pf_srchash[i];
5857 		struct pf_ksrc_node *sn, *tmp;
5858 
5859 		PF_HASHROW_LOCK(sh);
5860 		LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
5861 			if (PF_MATCHA(psnk->psnk_src.neg,
5862 			      &psnk->psnk_src.addr.v.a.addr,
5863 			      &psnk->psnk_src.addr.v.a.mask,
5864 			      &sn->addr, sn->af) &&
5865 			    PF_MATCHA(psnk->psnk_dst.neg,
5866 			      &psnk->psnk_dst.addr.v.a.addr,
5867 			      &psnk->psnk_dst.addr.v.a.mask,
5868 			      &sn->raddr, sn->af)) {
5869 				pf_unlink_src_node(sn);
5870 				LIST_INSERT_HEAD(&kill, sn, entry);
5871 				sn->expire = 1;
5872 			}
5873 		PF_HASHROW_UNLOCK(sh);
5874 	}
5875 
5876 	for (int i = 0; i <= pf_hashmask; i++) {
5877 		struct pf_idhash *ih = &V_pf_idhash[i];
5878 		struct pf_kstate *s;
5879 
5880 		PF_HASHROW_LOCK(ih);
5881 		LIST_FOREACH(s, &ih->states, entry) {
5882 			if (s->src_node && s->src_node->expire == 1)
5883 				s->src_node = NULL;
5884 			if (s->nat_src_node && s->nat_src_node->expire == 1)
5885 				s->nat_src_node = NULL;
5886 		}
5887 		PF_HASHROW_UNLOCK(ih);
5888 	}
5889 
5890 	psnk->psnk_killed = pf_free_src_nodes(&kill);
5891 }
5892 
5893 static int
5894 pf_keepcounters(struct pfioc_nv *nv)
5895 {
5896 	nvlist_t	*nvl = NULL;
5897 	void		*nvlpacked = NULL;
5898 	int		 error = 0;
5899 
5900 #define	ERROUT(x)	ERROUT_FUNCTION(on_error, x)
5901 
5902 	if (nv->len > pf_ioctl_maxcount)
5903 		ERROUT(ENOMEM);
5904 
5905 	nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
5906 	if (nvlpacked == NULL)
5907 		ERROUT(ENOMEM);
5908 
5909 	error = copyin(nv->data, nvlpacked, nv->len);
5910 	if (error)
5911 		ERROUT(error);
5912 
5913 	nvl = nvlist_unpack(nvlpacked, nv->len, 0);
5914 	if (nvl == NULL)
5915 		ERROUT(EBADMSG);
5916 
5917 	if (! nvlist_exists_bool(nvl, "keep_counters"))
5918 		ERROUT(EBADMSG);
5919 
5920 	V_pf_status.keep_counters = nvlist_get_bool(nvl, "keep_counters");
5921 
5922 on_error:
5923 	nvlist_destroy(nvl);
5924 	free(nvlpacked, M_NVLIST);
5925 	return (error);
5926 }
5927 
5928 static unsigned int
5929 pf_clear_states(const struct pf_kstate_kill *kill)
5930 {
5931 	struct pf_state_key_cmp	 match_key;
5932 	struct pf_kstate	*s;
5933 	struct pfi_kkif	*kif;
5934 	int		 idx;
5935 	unsigned int	 killed = 0, dir;
5936 
5937 	for (unsigned int i = 0; i <= pf_hashmask; i++) {
5938 		struct pf_idhash *ih = &V_pf_idhash[i];
5939 
5940 relock_DIOCCLRSTATES:
5941 		PF_HASHROW_LOCK(ih);
5942 		LIST_FOREACH(s, &ih->states, entry) {
5943 			/* For floating states look at the original kif. */
5944 			kif = s->kif == V_pfi_all ? s->orig_kif : s->kif;
5945 
5946 			if (kill->psk_ifname[0] &&
5947 			    strcmp(kill->psk_ifname,
5948 			    kif->pfik_name))
5949 				continue;
5950 
5951 			if (kill->psk_kill_match) {
5952 				bzero(&match_key, sizeof(match_key));
5953 
5954 				if (s->direction == PF_OUT) {
5955 					dir = PF_IN;
5956 					idx = PF_SK_STACK;
5957 				} else {
5958 					dir = PF_OUT;
5959 					idx = PF_SK_WIRE;
5960 				}
5961 
5962 				match_key.af = s->key[idx]->af;
5963 				match_key.proto = s->key[idx]->proto;
5964 				PF_ACPY(&match_key.addr[0],
5965 				    &s->key[idx]->addr[1], match_key.af);
5966 				match_key.port[0] = s->key[idx]->port[1];
5967 				PF_ACPY(&match_key.addr[1],
5968 				    &s->key[idx]->addr[0], match_key.af);
5969 				match_key.port[1] = s->key[idx]->port[0];
5970 			}
5971 
5972 			/*
5973 			 * Don't send out individual
5974 			 * delete messages.
5975 			 */
5976 			s->state_flags |= PFSTATE_NOSYNC;
5977 			pf_unlink_state(s);
5978 			killed++;
5979 
5980 			if (kill->psk_kill_match)
5981 				killed += pf_kill_matching_state(&match_key,
5982 				    dir);
5983 
5984 			goto relock_DIOCCLRSTATES;
5985 		}
5986 		PF_HASHROW_UNLOCK(ih);
5987 	}
5988 
5989 	if (V_pfsync_clear_states_ptr != NULL)
5990 		V_pfsync_clear_states_ptr(V_pf_status.hostid, kill->psk_ifname);
5991 
5992 	return (killed);
5993 }
5994 
5995 static void
5996 pf_killstates(struct pf_kstate_kill *kill, unsigned int *killed)
5997 {
5998 	struct pf_kstate	*s;
5999 
6000 	if (kill->psk_pfcmp.id) {
6001 		if (kill->psk_pfcmp.creatorid == 0)
6002 			kill->psk_pfcmp.creatorid = V_pf_status.hostid;
6003 		if ((s = pf_find_state_byid(kill->psk_pfcmp.id,
6004 		    kill->psk_pfcmp.creatorid))) {
6005 			pf_unlink_state(s);
6006 			*killed = 1;
6007 		}
6008 		return;
6009 	}
6010 
6011 	for (unsigned int i = 0; i <= pf_hashmask; i++)
6012 		*killed += pf_killstates_row(kill, &V_pf_idhash[i]);
6013 
6014 	return;
6015 }
6016 
6017 static int
6018 pf_killstates_nv(struct pfioc_nv *nv)
6019 {
6020 	struct pf_kstate_kill	 kill;
6021 	nvlist_t		*nvl = NULL;
6022 	void			*nvlpacked = NULL;
6023 	int			 error = 0;
6024 	unsigned int		 killed = 0;
6025 
6026 #define ERROUT(x)	ERROUT_FUNCTION(on_error, x)
6027 
6028 	if (nv->len > pf_ioctl_maxcount)
6029 		ERROUT(ENOMEM);
6030 
6031 	nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
6032 	if (nvlpacked == NULL)
6033 		ERROUT(ENOMEM);
6034 
6035 	error = copyin(nv->data, nvlpacked, nv->len);
6036 	if (error)
6037 		ERROUT(error);
6038 
6039 	nvl = nvlist_unpack(nvlpacked, nv->len, 0);
6040 	if (nvl == NULL)
6041 		ERROUT(EBADMSG);
6042 
6043 	error = pf_nvstate_kill_to_kstate_kill(nvl, &kill);
6044 	if (error)
6045 		ERROUT(error);
6046 
6047 	pf_killstates(&kill, &killed);
6048 
6049 	free(nvlpacked, M_NVLIST);
6050 	nvlpacked = NULL;
6051 	nvlist_destroy(nvl);
6052 	nvl = nvlist_create(0);
6053 	if (nvl == NULL)
6054 		ERROUT(ENOMEM);
6055 
6056 	nvlist_add_number(nvl, "killed", killed);
6057 
6058 	nvlpacked = nvlist_pack(nvl, &nv->len);
6059 	if (nvlpacked == NULL)
6060 		ERROUT(ENOMEM);
6061 
6062 	if (nv->size == 0)
6063 		ERROUT(0);
6064 	else if (nv->size < nv->len)
6065 		ERROUT(ENOSPC);
6066 
6067 	error = copyout(nvlpacked, nv->data, nv->len);
6068 
6069 on_error:
6070 	nvlist_destroy(nvl);
6071 	free(nvlpacked, M_NVLIST);
6072 	return (error);
6073 }
6074 
6075 static int
6076 pf_clearstates_nv(struct pfioc_nv *nv)
6077 {
6078 	struct pf_kstate_kill	 kill;
6079 	nvlist_t		*nvl = NULL;
6080 	void			*nvlpacked = NULL;
6081 	int			 error = 0;
6082 	unsigned int		 killed;
6083 
6084 #define ERROUT(x)	ERROUT_FUNCTION(on_error, x)
6085 
6086 	if (nv->len > pf_ioctl_maxcount)
6087 		ERROUT(ENOMEM);
6088 
6089 	nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
6090 	if (nvlpacked == NULL)
6091 		ERROUT(ENOMEM);
6092 
6093 	error = copyin(nv->data, nvlpacked, nv->len);
6094 	if (error)
6095 		ERROUT(error);
6096 
6097 	nvl = nvlist_unpack(nvlpacked, nv->len, 0);
6098 	if (nvl == NULL)
6099 		ERROUT(EBADMSG);
6100 
6101 	error = pf_nvstate_kill_to_kstate_kill(nvl, &kill);
6102 	if (error)
6103 		ERROUT(error);
6104 
6105 	killed = pf_clear_states(&kill);
6106 
6107 	free(nvlpacked, M_NVLIST);
6108 	nvlpacked = NULL;
6109 	nvlist_destroy(nvl);
6110 	nvl = nvlist_create(0);
6111 	if (nvl == NULL)
6112 		ERROUT(ENOMEM);
6113 
6114 	nvlist_add_number(nvl, "killed", killed);
6115 
6116 	nvlpacked = nvlist_pack(nvl, &nv->len);
6117 	if (nvlpacked == NULL)
6118 		ERROUT(ENOMEM);
6119 
6120 	if (nv->size == 0)
6121 		ERROUT(0);
6122 	else if (nv->size < nv->len)
6123 		ERROUT(ENOSPC);
6124 
6125 	error = copyout(nvlpacked, nv->data, nv->len);
6126 
6127 #undef ERROUT
6128 on_error:
6129 	nvlist_destroy(nvl);
6130 	free(nvlpacked, M_NVLIST);
6131 	return (error);
6132 }
6133 
6134 static int
6135 pf_getstate(struct pfioc_nv *nv)
6136 {
6137 	nvlist_t		*nvl = NULL, *nvls;
6138 	void			*nvlpacked = NULL;
6139 	struct pf_kstate	*s = NULL;
6140 	int			 error = 0;
6141 	uint64_t		 id, creatorid;
6142 
6143 #define ERROUT(x)	ERROUT_FUNCTION(errout, x)
6144 
6145 	if (nv->len > pf_ioctl_maxcount)
6146 		ERROUT(ENOMEM);
6147 
6148 	nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
6149 	if (nvlpacked == NULL)
6150 		ERROUT(ENOMEM);
6151 
6152 	error = copyin(nv->data, nvlpacked, nv->len);
6153 	if (error)
6154 		ERROUT(error);
6155 
6156 	nvl = nvlist_unpack(nvlpacked, nv->len, 0);
6157 	if (nvl == NULL)
6158 		ERROUT(EBADMSG);
6159 
6160 	PFNV_CHK(pf_nvuint64(nvl, "id", &id));
6161 	PFNV_CHK(pf_nvuint64(nvl, "creatorid", &creatorid));
6162 
6163 	s = pf_find_state_byid(id, creatorid);
6164 	if (s == NULL)
6165 		ERROUT(ENOENT);
6166 
6167 	free(nvlpacked, M_NVLIST);
6168 	nvlpacked = NULL;
6169 	nvlist_destroy(nvl);
6170 	nvl = nvlist_create(0);
6171 	if (nvl == NULL)
6172 		ERROUT(ENOMEM);
6173 
6174 	nvls = pf_state_to_nvstate(s);
6175 	if (nvls == NULL)
6176 		ERROUT(ENOMEM);
6177 
6178 	nvlist_add_nvlist(nvl, "state", nvls);
6179 	nvlist_destroy(nvls);
6180 
6181 	nvlpacked = nvlist_pack(nvl, &nv->len);
6182 	if (nvlpacked == NULL)
6183 		ERROUT(ENOMEM);
6184 
6185 	if (nv->size == 0)
6186 		ERROUT(0);
6187 	else if (nv->size < nv->len)
6188 		ERROUT(ENOSPC);
6189 
6190 	error = copyout(nvlpacked, nv->data, nv->len);
6191 
6192 #undef ERROUT
6193 errout:
6194 	if (s != NULL)
6195 		PF_STATE_UNLOCK(s);
6196 	free(nvlpacked, M_NVLIST);
6197 	nvlist_destroy(nvl);
6198 	return (error);
6199 }
6200 
6201 /*
6202  * XXX - Check for version mismatch!!!
6203  */
6204 
6205 /*
6206  * Duplicate pfctl -Fa operation to get rid of as much as we can.
6207  */
6208 static int
6209 shutdown_pf(void)
6210 {
6211 	int error = 0;
6212 	u_int32_t t[5];
6213 	char nn = '\0';
6214 
6215 	do {
6216 		if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
6217 		    != 0) {
6218 			DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
6219 			break;
6220 		}
6221 		if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
6222 		    != 0) {
6223 			DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
6224 			break;		/* XXX: rollback? */
6225 		}
6226 		if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
6227 		    != 0) {
6228 			DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
6229 			break;		/* XXX: rollback? */
6230 		}
6231 		if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
6232 		    != 0) {
6233 			DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
6234 			break;		/* XXX: rollback? */
6235 		}
6236 		if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
6237 		    != 0) {
6238 			DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
6239 			break;		/* XXX: rollback? */
6240 		}
6241 
6242 		/* XXX: these should always succeed here */
6243 		pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
6244 		pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
6245 		pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
6246 		pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
6247 		pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
6248 
6249 		if ((error = pf_clear_tables()) != 0)
6250 			break;
6251 
6252 		if ((error = pf_begin_eth(&t[0], &nn)) != 0) {
6253 			DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: eth\n"));
6254 			break;
6255 		}
6256 		pf_commit_eth(t[0], &nn);
6257 
6258 #ifdef ALTQ
6259 		if ((error = pf_begin_altq(&t[0])) != 0) {
6260 			DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
6261 			break;
6262 		}
6263 		pf_commit_altq(t[0]);
6264 #endif
6265 
6266 		pf_clear_all_states();
6267 
6268 		pf_clear_srcnodes(NULL);
6269 
6270 		/* status does not use malloced mem so no need to cleanup */
6271 		/* fingerprints and interfaces have their own cleanup code */
6272 	} while(0);
6273 
6274 	return (error);
6275 }
6276 
6277 static pfil_return_t
6278 pf_check_return(int chk, struct mbuf **m)
6279 {
6280 
6281 	switch (chk) {
6282 	case PF_PASS:
6283 		if (*m == NULL)
6284 			return (PFIL_CONSUMED);
6285 		else
6286 			return (PFIL_PASS);
6287 		break;
6288 	default:
6289 		if (*m != NULL) {
6290 			m_freem(*m);
6291 			*m = NULL;
6292 		}
6293 		return (PFIL_DROPPED);
6294 	}
6295 }
6296 
6297 static pfil_return_t
6298 pf_eth_check_in(struct mbuf **m, struct ifnet *ifp, int flags,
6299     void *ruleset __unused, struct inpcb *inp)
6300 {
6301 	int chk;
6302 
6303 	chk = pf_test_eth(PF_IN, flags, ifp, m, inp);
6304 
6305 	return (pf_check_return(chk, m));
6306 }
6307 
6308 static pfil_return_t
6309 pf_eth_check_out(struct mbuf **m, struct ifnet *ifp, int flags,
6310     void *ruleset __unused, struct inpcb *inp)
6311 {
6312 	int chk;
6313 
6314 	chk = pf_test_eth(PF_OUT, flags, ifp, m, inp);
6315 
6316 	return (pf_check_return(chk, m));
6317 }
6318 
6319 #ifdef INET
6320 static pfil_return_t
6321 pf_check_in(struct mbuf **m, struct ifnet *ifp, int flags,
6322     void *ruleset __unused, struct inpcb *inp)
6323 {
6324 	int chk;
6325 
6326 	chk = pf_test(PF_IN, flags, ifp, m, inp, NULL);
6327 
6328 	return (pf_check_return(chk, m));
6329 }
6330 
6331 static pfil_return_t
6332 pf_check_out(struct mbuf **m, struct ifnet *ifp, int flags,
6333     void *ruleset __unused,  struct inpcb *inp)
6334 {
6335 	int chk;
6336 
6337 	chk = pf_test(PF_OUT, flags, ifp, m, inp, NULL);
6338 
6339 	return (pf_check_return(chk, m));
6340 }
6341 #endif
6342 
6343 #ifdef INET6
6344 static pfil_return_t
6345 pf_check6_in(struct mbuf **m, struct ifnet *ifp, int flags,
6346     void *ruleset __unused,  struct inpcb *inp)
6347 {
6348 	int chk;
6349 
6350 	/*
6351 	 * In case of loopback traffic IPv6 uses the real interface in
6352 	 * order to support scoped addresses. In order to support stateful
6353 	 * filtering we have change this to lo0 as it is the case in IPv4.
6354 	 */
6355 	CURVNET_SET(ifp->if_vnet);
6356 	chk = pf_test6(PF_IN, flags, (*m)->m_flags & M_LOOP ? V_loif : ifp,
6357 	    m, inp, NULL);
6358 	CURVNET_RESTORE();
6359 
6360 	return (pf_check_return(chk, m));
6361 }
6362 
6363 static pfil_return_t
6364 pf_check6_out(struct mbuf **m, struct ifnet *ifp, int flags,
6365     void *ruleset __unused,  struct inpcb *inp)
6366 {
6367 	int chk;
6368 
6369 	CURVNET_SET(ifp->if_vnet);
6370 	chk = pf_test6(PF_OUT, flags, ifp, m, inp, NULL);
6371 	CURVNET_RESTORE();
6372 
6373 	return (pf_check_return(chk, m));
6374 }
6375 #endif /* INET6 */
6376 
6377 VNET_DEFINE_STATIC(pfil_hook_t, pf_eth_in_hook);
6378 VNET_DEFINE_STATIC(pfil_hook_t, pf_eth_out_hook);
6379 #define	V_pf_eth_in_hook	VNET(pf_eth_in_hook)
6380 #define	V_pf_eth_out_hook	VNET(pf_eth_out_hook)
6381 
6382 #ifdef INET
6383 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_in_hook);
6384 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_out_hook);
6385 #define	V_pf_ip4_in_hook	VNET(pf_ip4_in_hook)
6386 #define	V_pf_ip4_out_hook	VNET(pf_ip4_out_hook)
6387 #endif
6388 #ifdef INET6
6389 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_in_hook);
6390 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_out_hook);
6391 #define	V_pf_ip6_in_hook	VNET(pf_ip6_in_hook)
6392 #define	V_pf_ip6_out_hook	VNET(pf_ip6_out_hook)
6393 #endif
6394 
6395 static void
6396 hook_pf_eth(void)
6397 {
6398 	struct pfil_hook_args pha = {
6399 		.pa_version = PFIL_VERSION,
6400 		.pa_modname = "pf",
6401 		.pa_type = PFIL_TYPE_ETHERNET,
6402 	};
6403 	struct pfil_link_args pla = {
6404 		.pa_version = PFIL_VERSION,
6405 	};
6406 	int ret __diagused;
6407 
6408 	if (atomic_load_bool(&V_pf_pfil_eth_hooked))
6409 		return;
6410 
6411 	pha.pa_mbuf_chk = pf_eth_check_in;
6412 	pha.pa_flags = PFIL_IN;
6413 	pha.pa_rulname = "eth-in";
6414 	V_pf_eth_in_hook = pfil_add_hook(&pha);
6415 	pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR;
6416 	pla.pa_head = V_link_pfil_head;
6417 	pla.pa_hook = V_pf_eth_in_hook;
6418 	ret = pfil_link(&pla);
6419 	MPASS(ret == 0);
6420 	pha.pa_mbuf_chk = pf_eth_check_out;
6421 	pha.pa_flags = PFIL_OUT;
6422 	pha.pa_rulname = "eth-out";
6423 	V_pf_eth_out_hook = pfil_add_hook(&pha);
6424 	pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6425 	pla.pa_head = V_link_pfil_head;
6426 	pla.pa_hook = V_pf_eth_out_hook;
6427 	ret = pfil_link(&pla);
6428 	MPASS(ret == 0);
6429 
6430 	atomic_store_bool(&V_pf_pfil_eth_hooked, true);
6431 }
6432 
6433 static void
6434 hook_pf(void)
6435 {
6436 	struct pfil_hook_args pha = {
6437 		.pa_version = PFIL_VERSION,
6438 		.pa_modname = "pf",
6439 	};
6440 	struct pfil_link_args pla = {
6441 		.pa_version = PFIL_VERSION,
6442 	};
6443 	int ret __diagused;
6444 
6445 	if (atomic_load_bool(&V_pf_pfil_hooked))
6446 		return;
6447 
6448 #ifdef INET
6449 	pha.pa_type = PFIL_TYPE_IP4;
6450 	pha.pa_mbuf_chk = pf_check_in;
6451 	pha.pa_flags = PFIL_IN;
6452 	pha.pa_rulname = "default-in";
6453 	V_pf_ip4_in_hook = pfil_add_hook(&pha);
6454 	pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR;
6455 	pla.pa_head = V_inet_pfil_head;
6456 	pla.pa_hook = V_pf_ip4_in_hook;
6457 	ret = pfil_link(&pla);
6458 	MPASS(ret == 0);
6459 	pha.pa_mbuf_chk = pf_check_out;
6460 	pha.pa_flags = PFIL_OUT;
6461 	pha.pa_rulname = "default-out";
6462 	V_pf_ip4_out_hook = pfil_add_hook(&pha);
6463 	pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6464 	pla.pa_head = V_inet_pfil_head;
6465 	pla.pa_hook = V_pf_ip4_out_hook;
6466 	ret = pfil_link(&pla);
6467 	MPASS(ret == 0);
6468 	if (V_pf_filter_local) {
6469 		pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6470 		pla.pa_head = V_inet_local_pfil_head;
6471 		pla.pa_hook = V_pf_ip4_out_hook;
6472 		ret = pfil_link(&pla);
6473 		MPASS(ret == 0);
6474 	}
6475 #endif
6476 #ifdef INET6
6477 	pha.pa_type = PFIL_TYPE_IP6;
6478 	pha.pa_mbuf_chk = pf_check6_in;
6479 	pha.pa_flags = PFIL_IN;
6480 	pha.pa_rulname = "default-in6";
6481 	V_pf_ip6_in_hook = pfil_add_hook(&pha);
6482 	pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR;
6483 	pla.pa_head = V_inet6_pfil_head;
6484 	pla.pa_hook = V_pf_ip6_in_hook;
6485 	ret = pfil_link(&pla);
6486 	MPASS(ret == 0);
6487 	pha.pa_mbuf_chk = pf_check6_out;
6488 	pha.pa_rulname = "default-out6";
6489 	pha.pa_flags = PFIL_OUT;
6490 	V_pf_ip6_out_hook = pfil_add_hook(&pha);
6491 	pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6492 	pla.pa_head = V_inet6_pfil_head;
6493 	pla.pa_hook = V_pf_ip6_out_hook;
6494 	ret = pfil_link(&pla);
6495 	MPASS(ret == 0);
6496 	if (V_pf_filter_local) {
6497 		pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6498 		pla.pa_head = V_inet6_local_pfil_head;
6499 		pla.pa_hook = V_pf_ip6_out_hook;
6500 		ret = pfil_link(&pla);
6501 		MPASS(ret == 0);
6502 	}
6503 #endif
6504 
6505 	atomic_store_bool(&V_pf_pfil_hooked, true);
6506 }
6507 
6508 static void
6509 dehook_pf_eth(void)
6510 {
6511 
6512 	if (!atomic_load_bool(&V_pf_pfil_eth_hooked))
6513 		return;
6514 
6515 	pfil_remove_hook(V_pf_eth_in_hook);
6516 	pfil_remove_hook(V_pf_eth_out_hook);
6517 
6518 	atomic_store_bool(&V_pf_pfil_eth_hooked, false);
6519 }
6520 
6521 static void
6522 dehook_pf(void)
6523 {
6524 
6525 	if (!atomic_load_bool(&V_pf_pfil_hooked))
6526 		return;
6527 
6528 #ifdef INET
6529 	pfil_remove_hook(V_pf_ip4_in_hook);
6530 	pfil_remove_hook(V_pf_ip4_out_hook);
6531 #endif
6532 #ifdef INET6
6533 	pfil_remove_hook(V_pf_ip6_in_hook);
6534 	pfil_remove_hook(V_pf_ip6_out_hook);
6535 #endif
6536 
6537 	atomic_store_bool(&V_pf_pfil_hooked, false);
6538 }
6539 
6540 static void
6541 pf_load_vnet(void)
6542 {
6543 	V_pf_tag_z = uma_zcreate("pf tags", sizeof(struct pf_tagname),
6544 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
6545 
6546 	rm_init_flags(&V_pf_rules_lock, "pf rulesets", RM_RECURSE);
6547 	sx_init(&V_pf_ioctl_lock, "pf ioctl");
6548 
6549 	pf_init_tagset(&V_pf_tags, &pf_rule_tag_hashsize,
6550 	    PF_RULE_TAG_HASH_SIZE_DEFAULT);
6551 #ifdef ALTQ
6552 	pf_init_tagset(&V_pf_qids, &pf_queue_tag_hashsize,
6553 	    PF_QUEUE_TAG_HASH_SIZE_DEFAULT);
6554 #endif
6555 
6556 	V_pf_keth = &V_pf_main_keth_anchor.ruleset;
6557 
6558 	pfattach_vnet();
6559 	V_pf_vnet_active = 1;
6560 }
6561 
6562 static int
6563 pf_load(void)
6564 {
6565 	int error;
6566 
6567 	sx_init(&pf_end_lock, "pf end thread");
6568 
6569 	pf_mtag_initialize();
6570 
6571 	pf_dev = make_dev(&pf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, PF_NAME);
6572 	if (pf_dev == NULL)
6573 		return (ENOMEM);
6574 
6575 	pf_end_threads = 0;
6576 	error = kproc_create(pf_purge_thread, NULL, &pf_purge_proc, 0, 0, "pf purge");
6577 	if (error != 0)
6578 		return (error);
6579 
6580 	pfi_initialize();
6581 
6582 	return (0);
6583 }
6584 
6585 static void
6586 pf_unload_vnet(void)
6587 {
6588 	int ret __diagused;
6589 
6590 	V_pf_vnet_active = 0;
6591 	V_pf_status.running = 0;
6592 	dehook_pf();
6593 	dehook_pf_eth();
6594 
6595 	PF_RULES_WLOCK();
6596 	pf_syncookies_cleanup();
6597 	shutdown_pf();
6598 	PF_RULES_WUNLOCK();
6599 
6600 	/* Make sure we've cleaned up ethernet rules before we continue. */
6601 	NET_EPOCH_DRAIN_CALLBACKS();
6602 
6603 	ret = swi_remove(V_pf_swi_cookie);
6604 	MPASS(ret == 0);
6605 	ret = intr_event_destroy(V_pf_swi_ie);
6606 	MPASS(ret == 0);
6607 
6608 	pf_unload_vnet_purge();
6609 
6610 	pf_normalize_cleanup();
6611 	PF_RULES_WLOCK();
6612 	pfi_cleanup_vnet();
6613 	PF_RULES_WUNLOCK();
6614 	pfr_cleanup();
6615 	pf_osfp_flush();
6616 	pf_cleanup();
6617 	if (IS_DEFAULT_VNET(curvnet))
6618 		pf_mtag_cleanup();
6619 
6620 	pf_cleanup_tagset(&V_pf_tags);
6621 #ifdef ALTQ
6622 	pf_cleanup_tagset(&V_pf_qids);
6623 #endif
6624 	uma_zdestroy(V_pf_tag_z);
6625 
6626 #ifdef PF_WANT_32_TO_64_COUNTER
6627 	PF_RULES_WLOCK();
6628 	LIST_REMOVE(V_pf_kifmarker, pfik_allkiflist);
6629 
6630 	MPASS(LIST_EMPTY(&V_pf_allkiflist));
6631 	MPASS(V_pf_allkifcount == 0);
6632 
6633 	LIST_REMOVE(&V_pf_default_rule, allrulelist);
6634 	V_pf_allrulecount--;
6635 	LIST_REMOVE(V_pf_rulemarker, allrulelist);
6636 
6637 	/*
6638 	 * There are known pf rule leaks when running the test suite.
6639 	 */
6640 #ifdef notyet
6641 	MPASS(LIST_EMPTY(&V_pf_allrulelist));
6642 	MPASS(V_pf_allrulecount == 0);
6643 #endif
6644 
6645 	PF_RULES_WUNLOCK();
6646 
6647 	free(V_pf_kifmarker, PFI_MTYPE);
6648 	free(V_pf_rulemarker, M_PFRULE);
6649 #endif
6650 
6651 	/* Free counters last as we updated them during shutdown. */
6652 	pf_counter_u64_deinit(&V_pf_default_rule.evaluations);
6653 	for (int i = 0; i < 2; i++) {
6654 		pf_counter_u64_deinit(&V_pf_default_rule.packets[i]);
6655 		pf_counter_u64_deinit(&V_pf_default_rule.bytes[i]);
6656 	}
6657 	counter_u64_free(V_pf_default_rule.states_cur);
6658 	counter_u64_free(V_pf_default_rule.states_tot);
6659 	counter_u64_free(V_pf_default_rule.src_nodes);
6660 	uma_zfree_pcpu(pf_timestamp_pcpu_zone, V_pf_default_rule.timestamp);
6661 
6662 	for (int i = 0; i < PFRES_MAX; i++)
6663 		counter_u64_free(V_pf_status.counters[i]);
6664 	for (int i = 0; i < KLCNT_MAX; i++)
6665 		counter_u64_free(V_pf_status.lcounters[i]);
6666 	for (int i = 0; i < FCNT_MAX; i++)
6667 		pf_counter_u64_deinit(&V_pf_status.fcounters[i]);
6668 	for (int i = 0; i < SCNT_MAX; i++)
6669 		counter_u64_free(V_pf_status.scounters[i]);
6670 
6671 	rm_destroy(&V_pf_rules_lock);
6672 	sx_destroy(&V_pf_ioctl_lock);
6673 }
6674 
6675 static void
6676 pf_unload(void)
6677 {
6678 
6679 	sx_xlock(&pf_end_lock);
6680 	pf_end_threads = 1;
6681 	while (pf_end_threads < 2) {
6682 		wakeup_one(pf_purge_thread);
6683 		sx_sleep(pf_purge_proc, &pf_end_lock, 0, "pftmo", 0);
6684 	}
6685 	sx_xunlock(&pf_end_lock);
6686 
6687 	pf_nl_unregister();
6688 
6689 	if (pf_dev != NULL)
6690 		destroy_dev(pf_dev);
6691 
6692 	pfi_cleanup();
6693 
6694 	sx_destroy(&pf_end_lock);
6695 }
6696 
6697 static void
6698 vnet_pf_init(void *unused __unused)
6699 {
6700 
6701 	pf_load_vnet();
6702 }
6703 VNET_SYSINIT(vnet_pf_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
6704     vnet_pf_init, NULL);
6705 
6706 static void
6707 vnet_pf_uninit(const void *unused __unused)
6708 {
6709 
6710 	pf_unload_vnet();
6711 }
6712 SYSUNINIT(pf_unload, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND, pf_unload, NULL);
6713 VNET_SYSUNINIT(vnet_pf_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
6714     vnet_pf_uninit, NULL);
6715 
6716 static int
6717 pf_modevent(module_t mod, int type, void *data)
6718 {
6719 	int error = 0;
6720 
6721 	switch(type) {
6722 	case MOD_LOAD:
6723 		error = pf_load();
6724 		pf_nl_register();
6725 		break;
6726 	case MOD_UNLOAD:
6727 		/* Handled in SYSUNINIT(pf_unload) to ensure it's done after
6728 		 * the vnet_pf_uninit()s */
6729 		break;
6730 	default:
6731 		error = EINVAL;
6732 		break;
6733 	}
6734 
6735 	return (error);
6736 }
6737 
6738 static moduledata_t pf_mod = {
6739 	"pf",
6740 	pf_modevent,
6741 	0
6742 };
6743 
6744 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND);
6745 MODULE_DEPEND(pf, netlink, 1, 1, 1);
6746 MODULE_VERSION(pf, PF_MODVER);
6747