xref: /freebsd/lib/libpfctl/libpfctl.c (revision 78ae60b447ebf420dd5cebfec30480866fd5cef4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 #include <sys/ioctl.h>
35 #include <sys/nv.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 
39 #include <net/if.h>
40 #include <net/pfvar.h>
41 #include <netinet/in.h>
42 
43 #include <netpfil/pf/pf_nl.h>
44 #include <netlink/netlink.h>
45 #include <netlink/netlink_generic.h>
46 #include <netlink/netlink_snl.h>
47 #include <netlink/netlink_snl_generic.h>
48 #include <netlink/netlink_snl_route.h>
49 
50 #include <assert.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "libpfctl.h"
58 
59 struct pfctl_handle {
60 	int fd;
61 	struct snl_state ss;
62 };
63 
64 const char* PFCTL_SYNCOOKIES_MODE_NAMES[] = {
65 	"never",
66 	"always",
67 	"adaptive"
68 };
69 
70 static int	_pfctl_clear_states(int , const struct pfctl_kill *,
71 		    unsigned int *, uint64_t);
72 
73 struct pfctl_handle *
74 pfctl_open(const char *pf_device)
75 {
76 	struct pfctl_handle *h;
77 
78 	h = calloc(1, sizeof(struct pfctl_handle));
79 	h->fd = -1;
80 
81 	h->fd = open(pf_device, O_RDWR);
82 	if (h->fd < 0)
83 		goto error;
84 
85 	if (!snl_init(&h->ss, NETLINK_GENERIC))
86 		goto error;
87 
88 	return (h);
89 error:
90 	close(h->fd);
91 	snl_free(&h->ss);
92 	free(h);
93 
94 	return (NULL);
95 }
96 
97 void
98 pfctl_close(struct pfctl_handle *h)
99 {
100 	close(h->fd);
101 	snl_free(&h->ss);
102 	free(h);
103 }
104 
105 static int
106 pfctl_do_ioctl(int dev, uint cmd, size_t size, nvlist_t **nvl)
107 {
108 	struct pfioc_nv nv;
109 	void *data;
110 	size_t nvlen;
111 	int ret;
112 
113 	data = nvlist_pack(*nvl, &nvlen);
114 	if (nvlen > size)
115 		size = nvlen;
116 
117 retry:
118 	nv.data = malloc(size);
119 	if (nv.data == NULL) {
120 		ret = ENOMEM;
121 		goto out;
122 	}
123 
124 	memcpy(nv.data, data, nvlen);
125 
126 	nv.len = nvlen;
127 	nv.size = size;
128 
129 	ret = ioctl(dev, cmd, &nv);
130 	if (ret == -1 && errno == ENOSPC) {
131 		size *= 2;
132 		free(nv.data);
133 		goto retry;
134 	}
135 
136 	nvlist_destroy(*nvl);
137 	*nvl = NULL;
138 
139 	if (ret == 0) {
140 		*nvl = nvlist_unpack(nv.data, nv.len, 0);
141 		if (*nvl == NULL) {
142 			ret = EIO;
143 			goto out;
144 		}
145 	} else {
146 		ret = errno;
147 	}
148 
149 out:
150 	free(data);
151 	free(nv.data);
152 
153 	return (ret);
154 }
155 
156 static void
157 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems,
158     uint8_t *numbers, size_t *nelems)
159 {
160 	const uint64_t *tmp;
161 	size_t elems;
162 
163 	tmp = nvlist_get_number_array(nvl, name, &elems);
164 	assert(elems <= maxelems);
165 
166 	for (size_t i = 0; i < elems; i++)
167 		numbers[i] = tmp[i];
168 
169 	if (nelems)
170 		*nelems = elems;
171 }
172 
173 static void
174 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems,
175     uint16_t *numbers, size_t *nelems)
176 {
177 	const uint64_t *tmp;
178 	size_t elems;
179 
180 	tmp = nvlist_get_number_array(nvl, name, &elems);
181 	assert(elems <= maxelems);
182 
183 	for (size_t i = 0; i < elems; i++)
184 		numbers[i] = tmp[i];
185 
186 	if (nelems)
187 		*nelems = elems;
188 }
189 
190 static void
191 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems,
192     uint32_t *numbers, size_t *nelems)
193 {
194 	const uint64_t *tmp;
195 	size_t elems;
196 
197 	tmp = nvlist_get_number_array(nvl, name, &elems);
198 
199 	for (size_t i = 0; i < elems && i < maxelems; i++)
200 		numbers[i] = tmp[i];
201 
202 	if (nelems)
203 		*nelems = elems;
204 }
205 
206 static void
207 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems,
208     uint64_t *numbers, size_t *nelems)
209 {
210 	const uint64_t *tmp;
211 	size_t elems;
212 
213 	tmp = nvlist_get_number_array(nvl, name, &elems);
214 	assert(elems <= maxelems);
215 
216 	for (size_t i = 0; i < elems; i++)
217 		numbers[i] = tmp[i];
218 
219 	if (nelems)
220 		*nelems = elems;
221 }
222 
223 int
224 pfctl_startstop(struct pfctl_handle *h, int start)
225 {
226 	struct snl_errmsg_data e = {};
227 	struct snl_writer nw;
228 	struct nlmsghdr *hdr;
229 	uint32_t seq_id;
230 	int family_id;
231 
232 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
233 	if (family_id == 0)
234 		return (ENOTSUP);
235 
236 	snl_init_writer(&h->ss, &nw);
237 	hdr = snl_create_genl_msg_request(&nw, family_id,
238 	    start ? PFNL_CMD_START : PFNL_CMD_STOP);
239 
240 	hdr = snl_finalize_msg(&nw);
241 	if (hdr == NULL)
242 		return (ENOMEM);
243 	seq_id = hdr->nlmsg_seq;
244 
245 	snl_send_message(&h->ss, hdr);
246 
247 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
248 	}
249 
250 	return (e.error);
251 }
252 
253 static void
254 _pfctl_get_status_counters(const nvlist_t *nvl,
255     struct pfctl_status_counters *counters)
256 {
257 	const uint64_t		*ids, *counts;
258 	const char *const	*names;
259 	size_t id_len, counter_len, names_len;
260 
261 	ids = nvlist_get_number_array(nvl, "ids", &id_len);
262 	counts = nvlist_get_number_array(nvl, "counters", &counter_len);
263 	names = nvlist_get_string_array(nvl, "names", &names_len);
264 	assert(id_len == counter_len);
265 	assert(counter_len == names_len);
266 
267 	TAILQ_INIT(counters);
268 
269 	for (size_t i = 0; i < id_len; i++) {
270 		struct pfctl_status_counter *c;
271 
272 		c = malloc(sizeof(*c));
273 		if (c == NULL)
274 			continue;
275 
276 		c->id = ids[i];
277 		c->counter = counts[i];
278 		c->name = strdup(names[i]);
279 
280 		TAILQ_INSERT_TAIL(counters, c, entry);
281 	}
282 }
283 
284 struct pfctl_status *
285 pfctl_get_status(int dev)
286 {
287 	struct pfctl_status	*status;
288 	nvlist_t	*nvl;
289 	size_t		 len;
290 	const void	*chksum;
291 
292 	status = calloc(1, sizeof(*status));
293 	if (status == NULL)
294 		return (NULL);
295 
296 	nvl = nvlist_create(0);
297 
298 	if (pfctl_do_ioctl(dev, DIOCGETSTATUSNV, 4096, &nvl)) {
299 		nvlist_destroy(nvl);
300 		free(status);
301 		return (NULL);
302 	}
303 
304 	status->running = nvlist_get_bool(nvl, "running");
305 	status->since = nvlist_get_number(nvl, "since");
306 	status->debug = nvlist_get_number(nvl, "debug");
307 	status->hostid = ntohl(nvlist_get_number(nvl, "hostid"));
308 	status->states = nvlist_get_number(nvl, "states");
309 	status->src_nodes = nvlist_get_number(nvl, "src_nodes");
310 	status->syncookies_active = nvlist_get_bool(nvl, "syncookies_active");
311 	status->reass = nvlist_get_number(nvl, "reass");
312 
313 	strlcpy(status->ifname, nvlist_get_string(nvl, "ifname"),
314 	    IFNAMSIZ);
315 	chksum = nvlist_get_binary(nvl, "chksum", &len);
316 	assert(len == PF_MD5_DIGEST_LENGTH);
317 	memcpy(status->pf_chksum, chksum, len);
318 
319 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "counters"),
320 	    &status->counters);
321 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "lcounters"),
322 	    &status->lcounters);
323 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "fcounters"),
324 	    &status->fcounters);
325 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"),
326 	    &status->scounters);
327 
328 	pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 3,
329 	    (uint64_t *)status->pcounters, NULL);
330 	pf_nvuint_64_array(nvl, "bcounters", 2 * 2,
331 	    (uint64_t *)status->bcounters, NULL);
332 
333 	nvlist_destroy(nvl);
334 
335 	return (status);
336 }
337 
338 static uint64_t
339 _pfctl_status_counter(struct pfctl_status_counters *counters, uint64_t id)
340 {
341 	struct pfctl_status_counter *c;
342 
343 	TAILQ_FOREACH(c, counters, entry) {
344 		if (c->id == id)
345 			return (c->counter);
346 	}
347 
348 	return (0);
349 }
350 
351 uint64_t
352 pfctl_status_counter(struct pfctl_status *status, int id)
353 {
354 	return (_pfctl_status_counter(&status->counters, id));
355 }
356 
357 uint64_t
358 pfctl_status_lcounter(struct pfctl_status *status, int id)
359 {
360 	return (_pfctl_status_counter(&status->lcounters, id));
361 }
362 
363 uint64_t
364 pfctl_status_fcounter(struct pfctl_status *status, int id)
365 {
366 	return (_pfctl_status_counter(&status->fcounters, id));
367 }
368 
369 uint64_t
370 pfctl_status_scounter(struct pfctl_status *status, int id)
371 {
372 	return (_pfctl_status_counter(&status->scounters, id));
373 }
374 
375 void
376 pfctl_free_status(struct pfctl_status *status)
377 {
378 	struct pfctl_status_counter *c, *tmp;
379 
380 	if (status == NULL)
381 		return;
382 
383 	TAILQ_FOREACH_SAFE(c, &status->counters, entry, tmp) {
384 		free(c->name);
385 		free(c);
386 	}
387 	TAILQ_FOREACH_SAFE(c, &status->lcounters, entry, tmp) {
388 		free(c->name);
389 		free(c);
390 	}
391 	TAILQ_FOREACH_SAFE(c, &status->fcounters, entry, tmp) {
392 		free(c->name);
393 		free(c);
394 	}
395 	TAILQ_FOREACH_SAFE(c, &status->scounters, entry, tmp) {
396 		free(c->name);
397 		free(c);
398 	}
399 
400 	free(status);
401 }
402 
403 static void
404 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name,
405     const struct pf_addr *addr)
406 {
407 	nvlist_t *nvl = nvlist_create(0);
408 
409 	nvlist_add_binary(nvl, "addr", addr, sizeof(*addr));
410 
411 	nvlist_add_nvlist(nvparent, name, nvl);
412 	nvlist_destroy(nvl);
413 }
414 
415 static void
416 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr)
417 {
418 	size_t len;
419 	const void *data;
420 
421 	data = nvlist_get_binary(nvl, "addr", &len);
422 	assert(len == sizeof(struct pf_addr));
423 	memcpy(addr, data, len);
424 }
425 
426 static void
427 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name,
428     const struct pf_addr_wrap *addr)
429 {
430 	nvlist_t *nvl = nvlist_create(0);
431 
432 	nvlist_add_number(nvl, "type", addr->type);
433 	nvlist_add_number(nvl, "iflags", addr->iflags);
434 	if (addr->type == PF_ADDR_DYNIFTL)
435 		nvlist_add_string(nvl, "ifname", addr->v.ifname);
436 	if (addr->type == PF_ADDR_TABLE)
437 		nvlist_add_string(nvl, "tblname", addr->v.tblname);
438 	pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr);
439 	pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask);
440 
441 	nvlist_add_nvlist(nvparent, name, nvl);
442 	nvlist_destroy(nvl);
443 }
444 
445 static void
446 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
447 {
448 	bzero(addr, sizeof(*addr));
449 
450 	addr->type = nvlist_get_number(nvl, "type");
451 	addr->iflags = nvlist_get_number(nvl, "iflags");
452 	if (addr->type == PF_ADDR_DYNIFTL) {
453 		strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"),
454 		    IFNAMSIZ);
455 		addr->p.dyncnt = nvlist_get_number(nvl, "dyncnt");
456 	}
457 	if (addr->type == PF_ADDR_TABLE) {
458 		strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"),
459 		    PF_TABLE_NAME_SIZE);
460 		addr->p.tblcnt = nvlist_get_number(nvl, "tblcnt");
461 	}
462 
463 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr);
464 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask);
465 }
466 
467 static void
468 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name,
469     const struct pf_rule_addr *addr)
470 {
471 	uint64_t ports[2];
472 	nvlist_t *nvl = nvlist_create(0);
473 
474 	pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr);
475 	ports[0] = addr->port[0];
476 	ports[1] = addr->port[1];
477 	nvlist_add_number_array(nvl, "port", ports, 2);
478 	nvlist_add_number(nvl, "neg", addr->neg);
479 	nvlist_add_number(nvl, "port_op", addr->port_op);
480 
481 	nvlist_add_nvlist(nvparent, name, nvl);
482 	nvlist_destroy(nvl);
483 }
484 
485 static void
486 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
487 {
488 	pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr);
489 
490 	pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL);
491 	addr->neg = nvlist_get_number(nvl, "neg");
492 	addr->port_op = nvlist_get_number(nvl, "port_op");
493 }
494 
495 static void
496 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape)
497 {
498 	mape->offset = nvlist_get_number(nvl, "offset");
499 	mape->psidlen = nvlist_get_number(nvl, "psidlen");
500 	mape->psid = nvlist_get_number(nvl, "psid");
501 }
502 
503 static void
504 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool)
505 {
506 	size_t len;
507 	const void *data;
508 
509 	data = nvlist_get_binary(nvl, "key", &len);
510 	assert(len == sizeof(pool->key));
511 	memcpy(&pool->key, data, len);
512 
513 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter);
514 
515 	pool->tblidx = nvlist_get_number(nvl, "tblidx");
516 	pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL);
517 	pool->opts = nvlist_get_number(nvl, "opts");
518 
519 	if (nvlist_exists_nvlist(nvl, "mape"))
520 		pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape);
521 }
522 
523 static void
524 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
525 {
526 	pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL);
527 	uid->op = nvlist_get_number(nvl, "op");
528 }
529 
530 static void
531 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule)
532 {
533 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr);
534 	rule->divert.port = nvlist_get_number(nvl, "port");
535 }
536 
537 static void
538 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule)
539 {
540 	const uint64_t *skip;
541 	const char *const *labels;
542 	size_t skipcount, labelcount;
543 
544 	rule->nr = nvlist_get_number(nvl, "nr");
545 
546 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src);
547 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst);
548 
549 	skip = nvlist_get_number_array(nvl, "skip", &skipcount);
550 	assert(skip);
551 	assert(skipcount == PF_SKIP_COUNT);
552 	for (int i = 0; i < PF_SKIP_COUNT; i++)
553 		rule->skip[i].nr = skip[i];
554 
555 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
556 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
557 	for (size_t i = 0; i < labelcount; i++)
558 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
559 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
560 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
561 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
562 	strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE);
563 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
564 	    PF_TAG_NAME_SIZE);
565 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
566 	    PF_TAG_NAME_SIZE);
567 
568 	strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"),
569 	    PF_TABLE_NAME_SIZE);
570 
571 	pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool);
572 
573 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
574 	pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL);
575 	pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL);
576 
577 	if (nvlist_exists_number(nvl, "timestamp")) {
578 		rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp");
579 	}
580 
581 	rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint");
582 
583 	rule->rtableid = nvlist_get_number(nvl, "rtableid");
584 	pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL);
585 	rule->max_states = nvlist_get_number(nvl, "max_states");
586 	rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes");
587 	rule->max_src_states = nvlist_get_number(nvl, "max_src_states");
588 	rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn");
589 	rule->max_src_conn_rate.limit =
590 	    nvlist_get_number(nvl, "max_src_conn_rate.limit");
591 	rule->max_src_conn_rate.seconds =
592 	    nvlist_get_number(nvl, "max_src_conn_rate.seconds");
593 	rule->qid = nvlist_get_number(nvl, "qid");
594 	rule->pqid = nvlist_get_number(nvl, "pqid");
595 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
596 	rule->dnrpipe = nvlist_get_number(nvl, "dnrpipe");
597 	rule->free_flags = nvlist_get_number(nvl, "dnflags");
598 	rule->prob = nvlist_get_number(nvl, "prob");
599 	rule->cuid = nvlist_get_number(nvl, "cuid");
600 	rule->cpid = nvlist_get_number(nvl, "cpid");
601 
602 	rule->return_icmp = nvlist_get_number(nvl, "return_icmp");
603 	rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6");
604 	rule->max_mss = nvlist_get_number(nvl, "max_mss");
605 	rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags");
606 
607 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid);
608 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"),
609 	    (struct pf_rule_uid *)&rule->gid);
610 
611 	rule->rule_flag = nvlist_get_number(nvl, "rule_flag");
612 	rule->action = nvlist_get_number(nvl, "action");
613 	rule->direction = nvlist_get_number(nvl, "direction");
614 	rule->log = nvlist_get_number(nvl, "log");
615 	rule->logif = nvlist_get_number(nvl, "logif");
616 	rule->quick = nvlist_get_number(nvl, "quick");
617 	rule->ifnot = nvlist_get_number(nvl, "ifnot");
618 	rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not");
619 	rule->natpass = nvlist_get_number(nvl, "natpass");
620 
621 	rule->keep_state = nvlist_get_number(nvl, "keep_state");
622 	rule->af = nvlist_get_number(nvl, "af");
623 	rule->proto = nvlist_get_number(nvl, "proto");
624 	rule->type = nvlist_get_number(nvl, "type");
625 	rule->code = nvlist_get_number(nvl, "code");
626 	rule->flags = nvlist_get_number(nvl, "flags");
627 	rule->flagset = nvlist_get_number(nvl, "flagset");
628 	rule->min_ttl = nvlist_get_number(nvl, "min_ttl");
629 	rule->allow_opts = nvlist_get_number(nvl, "allow_opts");
630 	rule->rt = nvlist_get_number(nvl, "rt");
631 	rule->return_ttl  = nvlist_get_number(nvl, "return_ttl");
632 	rule->tos = nvlist_get_number(nvl, "tos");
633 	rule->set_tos = nvlist_get_number(nvl, "set_tos");
634 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
635 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
636 
637 	rule->flush = nvlist_get_number(nvl, "flush");
638 	rule->prio = nvlist_get_number(nvl, "prio");
639 	pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL);
640 
641 	pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule);
642 
643 	rule->states_cur = nvlist_get_number(nvl, "states_cur");
644 	rule->states_tot = nvlist_get_number(nvl, "states_tot");
645 	rule->src_nodes = nvlist_get_number(nvl, "src_nodes");
646 }
647 
648 static void
649 pfctl_nveth_addr_to_eth_addr(const nvlist_t *nvl, struct pfctl_eth_addr *addr)
650 {
651 	static const u_int8_t EMPTY_MAC[ETHER_ADDR_LEN] = { 0 };
652 	size_t len;
653 	const void *data;
654 
655 	data = nvlist_get_binary(nvl, "addr", &len);
656 	assert(len == sizeof(addr->addr));
657 	memcpy(addr->addr, data, sizeof(addr->addr));
658 
659 	data = nvlist_get_binary(nvl, "mask", &len);
660 	assert(len == sizeof(addr->mask));
661 	memcpy(addr->mask, data, sizeof(addr->mask));
662 
663 	addr->neg = nvlist_get_bool(nvl, "neg");
664 
665 	/* To make checks for 'is this address set?' easier. */
666 	addr->isset = memcmp(addr->addr, EMPTY_MAC, ETHER_ADDR_LEN) != 0;
667 }
668 
669 static nvlist_t *
670 pfctl_eth_addr_to_nveth_addr(const struct pfctl_eth_addr *addr)
671 {
672 	nvlist_t *nvl;
673 
674 	nvl = nvlist_create(0);
675 	if (nvl == NULL)
676 		return (NULL);
677 
678 	nvlist_add_bool(nvl, "neg", addr->neg);
679 	nvlist_add_binary(nvl, "addr", &addr->addr, ETHER_ADDR_LEN);
680 	nvlist_add_binary(nvl, "mask", &addr->mask, ETHER_ADDR_LEN);
681 
682 	return (nvl);
683 }
684 
685 static void
686 pfctl_nveth_rule_to_eth_rule(const nvlist_t *nvl, struct pfctl_eth_rule *rule)
687 {
688 	const char *const *labels;
689 	size_t labelcount, i;
690 
691 	rule->nr = nvlist_get_number(nvl, "nr");
692 	rule->quick = nvlist_get_bool(nvl, "quick");
693 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
694 	rule->ifnot = nvlist_get_bool(nvl, "ifnot");
695 	rule->direction = nvlist_get_number(nvl, "direction");
696 	rule->proto = nvlist_get_number(nvl, "proto");
697 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
698 	    PF_TAG_NAME_SIZE);
699 	rule->match_tag = nvlist_get_number(nvl, "match_tag");
700 	rule->match_tag_not = nvlist_get_bool(nvl, "match_tag_not");
701 
702 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
703 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
704 	for (i = 0; i < labelcount; i++)
705 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
706 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
707 
708 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "src"),
709 	    &rule->src);
710 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "dst"),
711 	    &rule->dst);
712 
713 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipsrc"),
714 	    &rule->ipsrc);
715 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipdst"),
716 	    &rule->ipdst);
717 
718 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
719 	rule->packets[0] = nvlist_get_number(nvl, "packets-in");
720 	rule->packets[1] = nvlist_get_number(nvl, "packets-out");
721 	rule->bytes[0] = nvlist_get_number(nvl, "bytes-in");
722 	rule->bytes[1] = nvlist_get_number(nvl, "bytes-out");
723 
724 	if (nvlist_exists_number(nvl, "timestamp")) {
725 		rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp");
726 	}
727 
728 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
729 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
730 	    PF_TAG_NAME_SIZE);
731 
732 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
733 	rule->dnflags = nvlist_get_number(nvl, "dnflags");
734 
735 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
736 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
737 
738 	strlcpy(rule->bridge_to, nvlist_get_string(nvl, "bridge_to"),
739 	    IFNAMSIZ);
740 
741 	rule->action = nvlist_get_number(nvl, "action");
742 }
743 
744 int
745 pfctl_get_eth_rulesets_info(int dev, struct pfctl_eth_rulesets_info *ri,
746     const char *path)
747 {
748 	nvlist_t *nvl;
749 	int ret;
750 
751 	bzero(ri, sizeof(*ri));
752 
753 	nvl = nvlist_create(0);
754 	nvlist_add_string(nvl, "path", path);
755 
756 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESETS, 256, &nvl)) != 0)
757 		goto out;
758 
759 	ri->nr = nvlist_get_number(nvl, "nr");
760 
761 out:
762 	nvlist_destroy(nvl);
763 	return (ret);
764 }
765 
766 int
767 pfctl_get_eth_ruleset(int dev, const char *path, int nr,
768     struct pfctl_eth_ruleset_info *ri)
769 {
770 	nvlist_t *nvl;
771 	int ret;
772 
773 	bzero(ri, sizeof(*ri));
774 
775 	nvl = nvlist_create(0);
776 	nvlist_add_string(nvl, "path", path);
777 	nvlist_add_number(nvl, "nr", nr);
778 
779 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESET, 1024, &nvl)) != 0)
780 		goto out;
781 
782 	ri->nr = nvlist_get_number(nvl, "nr");
783 	strlcpy(ri->path, nvlist_get_string(nvl, "path"), MAXPATHLEN);
784 	strlcpy(ri->name, nvlist_get_string(nvl, "name"),
785 	    PF_ANCHOR_NAME_SIZE);
786 
787 out:
788 	nvlist_destroy(nvl);
789 	return (ret);
790 }
791 
792 int
793 pfctl_get_eth_rules_info(int dev, struct pfctl_eth_rules_info *rules,
794     const char *path)
795 {
796 	nvlist_t *nvl;
797 	int ret;
798 
799 	bzero(rules, sizeof(*rules));
800 
801 	nvl = nvlist_create(0);
802 	nvlist_add_string(nvl, "anchor", path);
803 
804 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULES, 1024, &nvl)) != 0)
805 		goto out;
806 
807 	rules->nr = nvlist_get_number(nvl, "nr");
808 	rules->ticket = nvlist_get_number(nvl, "ticket");
809 
810 out:
811 	nvlist_destroy(nvl);
812 	return (ret);
813 }
814 
815 int
816 pfctl_get_eth_rule(int dev, uint32_t nr, uint32_t ticket,
817     const char *path, struct pfctl_eth_rule *rule, bool clear,
818     char *anchor_call)
819 {
820 	nvlist_t *nvl;
821 	int ret;
822 
823 	nvl = nvlist_create(0);
824 
825 	nvlist_add_string(nvl, "anchor", path);
826 	nvlist_add_number(nvl, "ticket", ticket);
827 	nvlist_add_number(nvl, "nr", nr);
828 	nvlist_add_bool(nvl, "clear", clear);
829 
830 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULE, 4096, &nvl)) != 0)
831 		goto out;
832 
833 	pfctl_nveth_rule_to_eth_rule(nvl, rule);
834 
835 	if (anchor_call)
836 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
837 		    MAXPATHLEN);
838 
839 out:
840 	nvlist_destroy(nvl);
841 	return (ret);
842 }
843 
844 int
845 pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r, const char *anchor,
846     const char *anchor_call, uint32_t ticket)
847 {
848 	struct pfioc_nv nv;
849 	nvlist_t *nvl, *addr;
850 	void *packed;
851 	int error = 0;
852 	size_t labelcount, size;
853 
854 	nvl = nvlist_create(0);
855 
856 	nvlist_add_number(nvl, "ticket", ticket);
857 	nvlist_add_string(nvl, "anchor", anchor);
858 	nvlist_add_string(nvl, "anchor_call", anchor_call);
859 
860 	nvlist_add_number(nvl, "nr", r->nr);
861 	nvlist_add_bool(nvl, "quick", r->quick);
862 	nvlist_add_string(nvl, "ifname", r->ifname);
863 	nvlist_add_bool(nvl, "ifnot", r->ifnot);
864 	nvlist_add_number(nvl, "direction", r->direction);
865 	nvlist_add_number(nvl, "proto", r->proto);
866 	nvlist_add_string(nvl, "match_tagname", r->match_tagname);
867 	nvlist_add_bool(nvl, "match_tag_not", r->match_tag_not);
868 
869 	addr = pfctl_eth_addr_to_nveth_addr(&r->src);
870 	if (addr == NULL) {
871 		nvlist_destroy(nvl);
872 		return (ENOMEM);
873 	}
874 	nvlist_add_nvlist(nvl, "src", addr);
875 	nvlist_destroy(addr);
876 
877 	addr = pfctl_eth_addr_to_nveth_addr(&r->dst);
878 	if (addr == NULL) {
879 		nvlist_destroy(nvl);
880 		return (ENOMEM);
881 	}
882 	nvlist_add_nvlist(nvl, "dst", addr);
883 	nvlist_destroy(addr);
884 
885 	pfctl_nv_add_rule_addr(nvl, "ipsrc", &r->ipsrc);
886 	pfctl_nv_add_rule_addr(nvl, "ipdst", &r->ipdst);
887 
888 	labelcount = 0;
889 	while (labelcount < PF_RULE_MAX_LABEL_COUNT &&
890 	    r->label[labelcount][0] != 0) {
891 		nvlist_append_string_array(nvl, "labels",
892 		    r->label[labelcount]);
893 		labelcount++;
894 	}
895 	nvlist_add_number(nvl, "ridentifier", r->ridentifier);
896 
897 	nvlist_add_string(nvl, "qname", r->qname);
898 	nvlist_add_string(nvl, "tagname", r->tagname);
899 	nvlist_add_number(nvl, "dnpipe", r->dnpipe);
900 	nvlist_add_number(nvl, "dnflags", r->dnflags);
901 
902 	nvlist_add_string(nvl, "bridge_to", r->bridge_to);
903 
904 	nvlist_add_number(nvl, "action", r->action);
905 
906 	packed = nvlist_pack(nvl, &size);
907 	if (packed == NULL) {
908 		nvlist_destroy(nvl);
909 		return (ENOMEM);
910 	}
911 
912 	nv.len = size;
913 	nv.size = size;
914 	nv.data = packed;
915 
916 	if (ioctl(dev, DIOCADDETHRULE, &nv) != 0)
917 		error = errno;
918 
919 	free(packed);
920 	nvlist_destroy(nvl);
921 
922 	return (error);
923 }
924 
925 static void
926 snl_add_msg_attr_addr_wrap(struct snl_writer *nw, uint32_t type, const struct pf_addr_wrap *addr)
927 {
928 	int off;
929 
930 	off = snl_add_msg_attr_nested(nw, type);
931 
932 	snl_add_msg_attr_ip6(nw, PF_AT_ADDR, &addr->v.a.addr.v6);
933 	snl_add_msg_attr_ip6(nw, PF_AT_MASK, &addr->v.a.mask.v6);
934 
935 	if (addr->type == PF_ADDR_DYNIFTL)
936 		snl_add_msg_attr_string(nw, PF_AT_IFNAME, addr->v.ifname);
937 	if (addr->type == PF_ADDR_TABLE)
938 		snl_add_msg_attr_string(nw, PF_AT_TABLENAME, addr->v.tblname);
939 	snl_add_msg_attr_u8(nw, PF_AT_TYPE, addr->type);
940 	snl_add_msg_attr_u8(nw, PF_AT_IFLAGS, addr->iflags);
941 
942 	snl_end_attr_nested(nw, off);
943 }
944 
945 static void
946 snl_add_msg_attr_rule_addr(struct snl_writer *nw, uint32_t type, const struct pf_rule_addr *addr)
947 {
948 	int off;
949 
950 	off = snl_add_msg_attr_nested(nw, type);
951 
952 	snl_add_msg_attr_addr_wrap(nw, PF_RAT_ADDR, &addr->addr);
953 	snl_add_msg_attr_u16(nw, PF_RAT_SRC_PORT, addr->port[0]);
954 	snl_add_msg_attr_u16(nw, PF_RAT_DST_PORT, addr->port[1]);
955 	snl_add_msg_attr_u8(nw, PF_RAT_NEG, addr->neg);
956 	snl_add_msg_attr_u8(nw, PF_RAT_OP, addr->port_op);
957 
958 	snl_end_attr_nested(nw, off);
959 }
960 
961 static void
962 snl_add_msg_attr_rule_labels(struct snl_writer *nw, uint32_t type, const char labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE])
963 {
964 	int off, i = 0;
965 
966 	off = snl_add_msg_attr_nested(nw, type);
967 
968 	while (labels[i][0] != 0 &&
969 	    i < PF_RULE_MAX_LABEL_COUNT) {
970 		snl_add_msg_attr_string(nw, PF_LT_LABEL, labels[i]);
971 		i++;
972 	}
973 
974 	snl_end_attr_nested(nw, off);
975 }
976 
977 static void
978 snl_add_msg_attr_mape(struct snl_writer *nw, uint32_t type, const struct pf_mape_portset *me)
979 {
980 	int off;
981 
982 	off = snl_add_msg_attr_nested(nw, type);
983 
984 	snl_add_msg_attr_u8(nw, PF_MET_OFFSET, me->offset);
985 	snl_add_msg_attr_u8(nw, PF_MET_PSID_LEN, me->psidlen);
986 	snl_add_msg_attr_u16(nw, PF_MET_PSID, me->psid);
987 
988 	snl_end_attr_nested(nw, off);
989 }
990 
991 static void
992 snl_add_msg_attr_rpool(struct snl_writer *nw, uint32_t type, const struct pfctl_pool *pool)
993 {
994 	int off;
995 
996 	off = snl_add_msg_attr_nested(nw, type);
997 
998 	snl_add_msg_attr(nw, PF_PT_KEY, sizeof(pool->key), &pool->key);
999 	snl_add_msg_attr_ip6(nw, PF_PT_COUNTER, &pool->counter.v6);
1000 	snl_add_msg_attr_u32(nw, PF_PT_TBLIDX, pool->tblidx);
1001 	snl_add_msg_attr_u16(nw, PF_PT_PROXY_SRC_PORT, pool->proxy_port[0]);
1002 	snl_add_msg_attr_u16(nw, PF_PT_PROXY_DST_PORT, pool->proxy_port[1]);
1003 	snl_add_msg_attr_u8(nw, PF_PT_OPTS, pool->opts);
1004 	snl_add_msg_attr_mape(nw, PF_PT_MAPE, &pool->mape);
1005 
1006 	snl_end_attr_nested(nw, off);
1007 }
1008 
1009 static void
1010 snl_add_msg_attr_timeouts(struct snl_writer *nw, uint32_t type, const uint32_t *timeouts)
1011 {
1012 	int off;
1013 
1014 	off = snl_add_msg_attr_nested(nw, type);
1015 
1016 	for (int i = 0; i < PFTM_MAX; i++)
1017 		snl_add_msg_attr_u32(nw, PF_TT_TIMEOUT, timeouts[i]);
1018 
1019 	snl_end_attr_nested(nw, off);
1020 }
1021 
1022 static void
1023 snl_add_msg_attr_uid(struct snl_writer *nw, uint32_t type, const struct pf_rule_uid *uid)
1024 {
1025 	int off;
1026 
1027 	off = snl_add_msg_attr_nested(nw, type);
1028 
1029 	snl_add_msg_attr_u32(nw, PF_RUT_UID_LOW, uid->uid[0]);
1030 	snl_add_msg_attr_u32(nw, PF_RUT_UID_HIGH, uid->uid[1]);
1031 	snl_add_msg_attr_u8(nw, PF_RUT_OP, uid->op);
1032 
1033 	snl_end_attr_nested(nw, off);
1034 }
1035 
1036 static void
1037 snl_add_msg_attr_pf_rule(struct snl_writer *nw, uint32_t type, const struct pfctl_rule *r)
1038 {
1039 	int off;
1040 
1041 	off = snl_add_msg_attr_nested(nw, type);
1042 
1043 	snl_add_msg_attr_rule_addr(nw, PF_RT_SRC, &r->src);
1044 	snl_add_msg_attr_rule_addr(nw, PF_RT_DST, &r->dst);
1045 	snl_add_msg_attr_rule_labels(nw, PF_RT_LABELS, r->label);
1046 	snl_add_msg_attr_u32(nw, PF_RT_RIDENTIFIER, r->ridentifier);
1047 	snl_add_msg_attr_string(nw, PF_RT_IFNAME, r->ifname);
1048 	snl_add_msg_attr_string(nw, PF_RT_QNAME, r->qname);
1049 	snl_add_msg_attr_string(nw, PF_RT_PQNAME, r->pqname);
1050 	snl_add_msg_attr_string(nw, PF_RT_TAGNAME, r->tagname);
1051 	snl_add_msg_attr_string(nw, PF_RT_MATCH_TAGNAME, r->match_tagname);
1052 	snl_add_msg_attr_string(nw, PF_RT_OVERLOAD_TBLNAME, r->overload_tblname);
1053 	snl_add_msg_attr_rpool(nw, PF_RT_RPOOL, &r->rpool);
1054 	snl_add_msg_attr_u32(nw, PF_RT_OS_FINGERPRINT, r->os_fingerprint);
1055 	snl_add_msg_attr_u32(nw, PF_RT_RTABLEID, r->rtableid);
1056 	snl_add_msg_attr_timeouts(nw, PF_RT_TIMEOUT, r->timeout);
1057 	snl_add_msg_attr_u32(nw, PF_RT_MAX_STATES, r->max_states);
1058 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_NODES, r->max_src_nodes);
1059 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_STATES, r->max_src_states);
1060 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_LIMIT, r->max_src_conn_rate.limit);
1061 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_SECS, r->max_src_conn_rate.seconds);
1062 
1063 	snl_add_msg_attr_u16(nw, PF_RT_DNPIPE, r->dnpipe);
1064 	snl_add_msg_attr_u16(nw, PF_RT_DNRPIPE, r->dnrpipe);
1065 	snl_add_msg_attr_u32(nw, PF_RT_DNFLAGS, r->free_flags);
1066 
1067 	snl_add_msg_attr_u32(nw, PF_RT_NR, r->nr);
1068 	snl_add_msg_attr_u32(nw, PF_RT_PROB, r->prob);
1069 	snl_add_msg_attr_u32(nw, PF_RT_CUID, r->cuid);
1070 	snl_add_msg_attr_u32(nw, PF_RT_CPID, r->cpid);
1071 
1072 	snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP, r->return_icmp);
1073 	snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP6, r->return_icmp6);
1074 	snl_add_msg_attr_u16(nw, PF_RT_MAX_MSS, r->max_mss);
1075 	snl_add_msg_attr_u16(nw, PF_RT_SCRUB_FLAGS, r->scrub_flags);
1076 
1077 	snl_add_msg_attr_uid(nw, PF_RT_UID, &r->uid);
1078 	snl_add_msg_attr_uid(nw, PF_RT_GID, (const struct pf_rule_uid *)&r->gid);
1079 
1080 	snl_add_msg_attr_u32(nw, PF_RT_RULE_FLAG, r->rule_flag);
1081 	snl_add_msg_attr_u8(nw, PF_RT_ACTION, r->action);
1082 	snl_add_msg_attr_u8(nw, PF_RT_DIRECTION, r->direction);
1083 	snl_add_msg_attr_u8(nw, PF_RT_LOG, r->log);
1084 	snl_add_msg_attr_u8(nw, PF_RT_LOGIF, r->logif);
1085 	snl_add_msg_attr_u8(nw, PF_RT_QUICK, r->quick);
1086 	snl_add_msg_attr_u8(nw, PF_RT_IF_NOT, r->ifnot);
1087 	snl_add_msg_attr_u8(nw, PF_RT_MATCH_TAG_NOT, r->match_tag_not);
1088 	snl_add_msg_attr_u8(nw, PF_RT_NATPASS, r->natpass);
1089 	snl_add_msg_attr_u8(nw, PF_RT_KEEP_STATE, r->keep_state);
1090 	snl_add_msg_attr_u8(nw, PF_RT_AF, r->af);
1091 	snl_add_msg_attr_u8(nw, PF_RT_PROTO, r->proto);
1092 	snl_add_msg_attr_u8(nw, PF_RT_TYPE, r->type);
1093 	snl_add_msg_attr_u8(nw, PF_RT_CODE, r->code);
1094 	snl_add_msg_attr_u8(nw, PF_RT_FLAGS, r->flags);
1095 	snl_add_msg_attr_u8(nw, PF_RT_FLAGSET, r->flagset);
1096 	snl_add_msg_attr_u8(nw, PF_RT_MIN_TTL, r->min_ttl);
1097 	snl_add_msg_attr_u8(nw, PF_RT_ALLOW_OPTS, r->allow_opts);
1098 	snl_add_msg_attr_u8(nw, PF_RT_RT, r->rt);
1099 	snl_add_msg_attr_u8(nw, PF_RT_RETURN_TTL, r->return_ttl);
1100 	snl_add_msg_attr_u8(nw, PF_RT_TOS, r->tos);
1101 	snl_add_msg_attr_u8(nw, PF_RT_SET_TOS, r->set_tos);
1102 
1103 	snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_RELATIVE, r->anchor_relative);
1104 	snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_WILDCARD, r->anchor_wildcard);
1105 	snl_add_msg_attr_u8(nw, PF_RT_FLUSH, r->flush);
1106 	snl_add_msg_attr_u8(nw, PF_RT_PRIO, r->prio);
1107 	snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO, r->set_prio[0]);
1108 	snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO_REPLY, r->set_prio[1]);
1109 
1110 	snl_add_msg_attr_ip6(nw, PF_RT_DIVERT_ADDRESS, &r->divert.addr.v6);
1111 	snl_add_msg_attr_u16(nw, PF_RT_DIVERT_PORT, r->divert.port);
1112 
1113 	snl_end_attr_nested(nw, off);
1114 }
1115 
1116 int
1117 pfctl_add_rule(int dev __unused, const struct pfctl_rule *r, const char *anchor,
1118     const char *anchor_call, uint32_t ticket, uint32_t pool_ticket)
1119 {
1120 	struct pfctl_handle *h;
1121 	int ret;
1122 
1123 	h = pfctl_open(PF_DEVICE);
1124 	if (h == NULL)
1125 		return (ENODEV);
1126 
1127 	ret = pfctl_add_rule_h(h, r, anchor, anchor_call, ticket, pool_ticket);
1128 
1129 	pfctl_close(h);
1130 
1131 	return (ret);
1132 }
1133 
1134 int
1135 pfctl_add_rule_h(struct pfctl_handle *h, const struct pfctl_rule *r,
1136 	    const char *anchor, const char *anchor_call, uint32_t ticket,
1137 	    uint32_t pool_ticket)
1138 {
1139 	struct snl_writer nw;
1140 	struct snl_errmsg_data e = {};
1141 	struct nlmsghdr *hdr;
1142 	uint32_t seq_id;
1143 	int family_id;
1144 
1145 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
1146 	if (family_id == 0)
1147 		return (ENOTSUP);
1148 
1149 	snl_init_writer(&h->ss, &nw);
1150 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_ADDRULE);
1151 	hdr->nlmsg_flags |= NLM_F_DUMP;
1152 	snl_add_msg_attr_u32(&nw, PF_ART_TICKET, ticket);
1153 	snl_add_msg_attr_u32(&nw, PF_ART_POOL_TICKET, pool_ticket);
1154 	snl_add_msg_attr_string(&nw, PF_ART_ANCHOR, anchor);
1155 	snl_add_msg_attr_string(&nw, PF_ART_ANCHOR_CALL, anchor_call);
1156 
1157 	snl_add_msg_attr_pf_rule(&nw, PF_ART_RULE, r);
1158 
1159 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
1160 		return (ENXIO);
1161 
1162 	seq_id = hdr->nlmsg_seq;
1163 
1164 	if (! snl_send_message(&h->ss, hdr))
1165 		return (ENXIO);
1166 
1167 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
1168 	}
1169 
1170 	return (e.error);
1171 }
1172 
1173 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1174 #define	_OUT(_field)	offsetof(struct pfctl_rules_info, _field)
1175 static struct snl_attr_parser ap_getrules[] = {
1176 	{ .type = PF_GR_NR, .off = _OUT(nr), .cb = snl_attr_get_uint32 },
1177 	{ .type = PF_GR_TICKET, .off = _OUT(ticket), .cb = snl_attr_get_uint32 },
1178 };
1179 static struct snl_field_parser fp_getrules[] = {
1180 };
1181 #undef _IN
1182 #undef _OUT
1183 SNL_DECLARE_PARSER(getrules_parser, struct genlmsghdr, fp_getrules, ap_getrules);
1184 
1185 int
1186 pfctl_get_rules_info(int dev __unused, struct pfctl_rules_info *rules, uint32_t ruleset,
1187     const char *path)
1188 {
1189 	struct snl_state ss = {};
1190 	struct snl_errmsg_data e = {};
1191 	struct nlmsghdr *hdr;
1192 	struct snl_writer nw;
1193 	uint32_t seq_id;
1194 	int family_id;
1195 
1196 	snl_init(&ss, NETLINK_GENERIC);
1197 	family_id = snl_get_genl_family(&ss, PFNL_FAMILY_NAME);
1198 	if (family_id == 0)
1199 		return (ENOTSUP);
1200 
1201 	snl_init_writer(&ss, &nw);
1202 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETRULES);
1203 	hdr->nlmsg_flags |= NLM_F_DUMP;
1204 
1205 	snl_add_msg_attr_string(&nw, PF_GR_ANCHOR, path);
1206 	snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset);
1207 
1208 	hdr = snl_finalize_msg(&nw);
1209 	if (hdr == NULL)
1210 		return (ENOMEM);
1211 
1212 	seq_id = hdr->nlmsg_seq;
1213 	if (! snl_send_message(&ss, hdr))
1214 		return (ENXIO);
1215 
1216 	while ((hdr = snl_read_reply_multi(&ss, seq_id, &e)) != NULL) {
1217 		if (! snl_parse_nlmsg(&ss, hdr, &getrules_parser, rules))
1218 			continue;
1219 	}
1220 
1221 	return (e.error);
1222 }
1223 
1224 int
1225 pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket, const char *anchor,
1226     uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
1227 {
1228 	return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule,
1229 	    anchor_call, false));
1230 }
1231 
1232 int	pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket,
1233 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
1234 	    char *anchor_call, bool clear)
1235 {
1236 	nvlist_t *nvl;
1237 	int ret;
1238 
1239 	nvl = nvlist_create(0);
1240 	if (nvl == 0)
1241 		return (ENOMEM);
1242 
1243 	nvlist_add_number(nvl, "nr", nr);
1244 	nvlist_add_number(nvl, "ticket", ticket);
1245 	nvlist_add_string(nvl, "anchor", anchor);
1246 	nvlist_add_number(nvl, "ruleset", ruleset);
1247 
1248 	if (clear)
1249 		nvlist_add_bool(nvl, "clear_counter", true);
1250 
1251 	if ((ret = pfctl_do_ioctl(dev, DIOCGETRULENV, 8192, &nvl)) != 0)
1252 		goto out;
1253 
1254 	pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule);
1255 
1256 	if (anchor_call)
1257 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
1258 		    MAXPATHLEN);
1259 
1260 out:
1261 	nvlist_destroy(nvl);
1262 	return (ret);
1263 }
1264 
1265 int
1266 pfctl_set_keepcounters(int dev, bool keep)
1267 {
1268 	struct pfioc_nv	 nv;
1269 	nvlist_t	*nvl;
1270 	int		 ret;
1271 
1272 	nvl = nvlist_create(0);
1273 
1274 	nvlist_add_bool(nvl, "keep_counters", keep);
1275 
1276 	nv.data = nvlist_pack(nvl, &nv.len);
1277 	nv.size = nv.len;
1278 
1279 	nvlist_destroy(nvl);
1280 
1281 	ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv);
1282 
1283 	free(nv.data);
1284 	return (ret);
1285 }
1286 
1287 struct pfctl_creator {
1288 	uint32_t id;
1289 };
1290 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1291 #define	_OUT(_field)	offsetof(struct pfctl_creator, _field)
1292 static struct snl_attr_parser ap_creators[] = {
1293 	{ .type = PF_ST_CREATORID, .off = _OUT(id), .cb = snl_attr_get_uint32 },
1294 };
1295 static struct snl_field_parser fp_creators[] = {
1296 };
1297 #undef _IN
1298 #undef _OUT
1299 SNL_DECLARE_PARSER(creator_parser, struct genlmsghdr, fp_creators, ap_creators);
1300 
1301 static int
1302 pfctl_get_creators_nl(struct snl_state *ss, uint32_t *creators, size_t *len)
1303 {
1304 
1305 	int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME);
1306 	size_t i = 0;
1307 
1308 	struct nlmsghdr *hdr;
1309 	struct snl_writer nw;
1310 
1311 	if (family_id == 0)
1312 		return (ENOTSUP);
1313 
1314 	snl_init_writer(ss, &nw);
1315 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETCREATORS);
1316 	hdr->nlmsg_flags |= NLM_F_DUMP;
1317 	hdr = snl_finalize_msg(&nw);
1318 	if (hdr == NULL)
1319 		return (ENOMEM);
1320 	uint32_t seq_id = hdr->nlmsg_seq;
1321 
1322 	snl_send_message(ss, hdr);
1323 
1324 	struct snl_errmsg_data e = {};
1325 	while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) {
1326 		struct pfctl_creator c;
1327 		bzero(&c, sizeof(c));
1328 
1329 		if (!snl_parse_nlmsg(ss, hdr, &creator_parser, &c))
1330 			continue;
1331 
1332 		creators[i] = c.id;
1333 		i++;
1334 		if (i > *len)
1335 			return (E2BIG);
1336 	}
1337 
1338 	*len = i;
1339 
1340 	return (0);
1341 }
1342 
1343 int
1344 pfctl_get_creatorids(struct pfctl_handle *h, uint32_t *creators, size_t *len)
1345 {
1346 	int error;
1347 
1348 	error = pfctl_get_creators_nl(&h->ss, creators, len);
1349 
1350 	return (error);
1351 }
1352 
1353 static void
1354 pfctl_nv_add_state_cmp(nvlist_t *nvl, const char *name,
1355     const struct pfctl_state_cmp *cmp)
1356 {
1357 	nvlist_t	*nv;
1358 
1359 	nv = nvlist_create(0);
1360 
1361 	nvlist_add_number(nv, "id", cmp->id);
1362 	nvlist_add_number(nv, "creatorid", htonl(cmp->creatorid));
1363 	nvlist_add_number(nv, "direction", cmp->direction);
1364 
1365 	nvlist_add_nvlist(nvl, name, nv);
1366 	nvlist_destroy(nv);
1367 }
1368 
1369 static inline bool
1370 snl_attr_get_pfaddr(struct snl_state *ss __unused, struct nlattr *nla,
1371     const void *arg __unused, void *target)
1372 {
1373 	memcpy(target, NLA_DATA(nla), NLA_DATA_LEN(nla));
1374 	return (true);
1375 }
1376 
1377 static inline bool
1378 snl_attr_store_ifname(struct snl_state *ss __unused, struct nlattr *nla,
1379     const void *arg __unused, void *target)
1380 {
1381 	size_t maxlen = NLA_DATA_LEN(nla);
1382 
1383 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
1384 		strlcpy(target, (char *)NLA_DATA(nla), maxlen);
1385 		return (true);
1386 	}
1387 	return (false);
1388 }
1389 
1390 #define	_OUT(_field)	offsetof(struct pfctl_state_peer, _field)
1391 static const struct snl_attr_parser nla_p_speer[] = {
1392 	{ .type = PF_STP_SEQLO, .off = _OUT(seqlo), .cb = snl_attr_get_uint32 },
1393 	{ .type = PF_STP_SEQHI, .off = _OUT(seqhi), .cb = snl_attr_get_uint32 },
1394 	{ .type = PF_STP_SEQDIFF, .off = _OUT(seqdiff), .cb = snl_attr_get_uint32 },
1395 	{ .type = PF_STP_STATE, .off = _OUT(state), .cb = snl_attr_get_uint8 },
1396 	{ .type = PF_STP_WSCALE, .off = _OUT(wscale), .cb = snl_attr_get_uint8 },
1397 };
1398 SNL_DECLARE_ATTR_PARSER(speer_parser, nla_p_speer);
1399 #undef _OUT
1400 
1401 #define	_OUT(_field)	offsetof(struct pf_state_key_export, _field)
1402 static const struct snl_attr_parser nla_p_skey[] = {
1403 	{ .type = PF_STK_ADDR0, .off = _OUT(addr[0]), .cb = snl_attr_get_pfaddr },
1404 	{ .type = PF_STK_ADDR1, .off = _OUT(addr[1]), .cb = snl_attr_get_pfaddr },
1405 	{ .type = PF_STK_PORT0, .off = _OUT(port[0]), .cb = snl_attr_get_uint16 },
1406 	{ .type = PF_STK_PORT1, .off = _OUT(port[1]), .cb = snl_attr_get_uint16 },
1407 };
1408 SNL_DECLARE_ATTR_PARSER(skey_parser, nla_p_skey);
1409 #undef _OUT
1410 
1411 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1412 #define	_OUT(_field)	offsetof(struct pfctl_state, _field)
1413 static struct snl_attr_parser ap_state[] = {
1414 	{ .type = PF_ST_ID, .off = _OUT(id), .cb = snl_attr_get_uint64 },
1415 	{ .type = PF_ST_CREATORID, .off = _OUT(creatorid), .cb = snl_attr_get_uint32 },
1416 	{ .type = PF_ST_IFNAME, .off = _OUT(ifname), .cb = snl_attr_store_ifname },
1417 	{ .type = PF_ST_ORIG_IFNAME, .off = _OUT(orig_ifname), .cb = snl_attr_store_ifname },
1418 	{ .type = PF_ST_KEY_WIRE, .off = _OUT(key[0]), .arg = &skey_parser, .cb = snl_attr_get_nested },
1419 	{ .type = PF_ST_KEY_STACK, .off = _OUT(key[1]), .arg = &skey_parser, .cb = snl_attr_get_nested },
1420 	{ .type = PF_ST_PEER_SRC, .off = _OUT(src), .arg = &speer_parser, .cb = snl_attr_get_nested },
1421 	{ .type = PF_ST_PEER_DST, .off = _OUT(dst), .arg = &speer_parser, .cb = snl_attr_get_nested },
1422 	{ .type = PF_ST_RT_ADDR, .off = _OUT(rt_addr), .cb = snl_attr_get_pfaddr },
1423 	{ .type = PF_ST_RULE, .off = _OUT(rule), .cb = snl_attr_get_uint32 },
1424 	{ .type = PF_ST_ANCHOR, .off = _OUT(anchor), .cb = snl_attr_get_uint32 },
1425 	{ .type = PF_ST_NAT_RULE, .off = _OUT(nat_rule), .cb = snl_attr_get_uint32 },
1426 	{ .type = PF_ST_CREATION, .off = _OUT(creation), .cb = snl_attr_get_uint32 },
1427 	{ .type = PF_ST_EXPIRE, .off = _OUT(expire), .cb = snl_attr_get_uint32 },
1428 	{ .type = PF_ST_PACKETS0, .off = _OUT(packets[0]), .cb = snl_attr_get_uint64 },
1429 	{ .type = PF_ST_PACKETS1, .off = _OUT(packets[1]), .cb = snl_attr_get_uint64 },
1430 	{ .type = PF_ST_BYTES0, .off = _OUT(bytes[0]), .cb = snl_attr_get_uint64 },
1431 	{ .type = PF_ST_BYTES1, .off = _OUT(bytes[1]), .cb = snl_attr_get_uint64 },
1432 	{ .type = PF_ST_AF, .off = _OUT(key[0].af), .cb = snl_attr_get_uint8 },
1433 	{ .type = PF_ST_PROTO, .off = _OUT(key[0].proto), .cb = snl_attr_get_uint8 },
1434 	{ .type = PF_ST_DIRECTION, .off = _OUT(direction), .cb = snl_attr_get_uint8 },
1435 	{ .type = PF_ST_LOG, .off = _OUT(log), .cb = snl_attr_get_uint8 },
1436 	{ .type = PF_ST_STATE_FLAGS, .off = _OUT(state_flags), .cb = snl_attr_get_uint16 },
1437 	{ .type = PF_ST_SYNC_FLAGS, .off = _OUT(sync_flags), .cb = snl_attr_get_uint8 },
1438 	{ .type = PF_ST_RTABLEID, .off = _OUT(rtableid), .cb = snl_attr_get_int32 },
1439 	{ .type = PF_ST_MIN_TTL, .off = _OUT(min_ttl), .cb = snl_attr_get_uint8 },
1440 	{ .type = PF_ST_MAX_MSS, .off = _OUT(max_mss), .cb = snl_attr_get_uint16 },
1441 	{ .type = PF_ST_DNPIPE, .off = _OUT(dnpipe), .cb = snl_attr_get_uint16 },
1442 	{ .type = PF_ST_DNRPIPE, .off = _OUT(dnrpipe), .cb = snl_attr_get_uint16 },
1443 	{ .type = PF_ST_RT, .off = _OUT(rt), .cb = snl_attr_get_uint8 },
1444 	{ .type = PF_ST_RT_IFNAME, .off = _OUT(rt_ifname), .cb = snl_attr_store_ifname },
1445 };
1446 static struct snl_field_parser fp_state[] = {
1447 };
1448 #undef _IN
1449 #undef _OUT
1450 SNL_DECLARE_PARSER(state_parser, struct genlmsghdr, fp_state, ap_state);
1451 
1452 static const struct snl_hdr_parser *all_parsers[] = {
1453 	&state_parser, &skey_parser, &speer_parser,
1454 	&creator_parser, &getrules_parser
1455 };
1456 
1457 static int
1458 pfctl_get_states_nl(struct pfctl_state_filter *filter, struct snl_state *ss, pfctl_get_state_fn f, void *arg)
1459 {
1460 	SNL_VERIFY_PARSERS(all_parsers);
1461 	int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME);
1462 	int ret;
1463 
1464 	struct nlmsghdr *hdr;
1465 	struct snl_writer nw;
1466 
1467 	if (family_id == 0)
1468 		return (ENOTSUP);
1469 
1470 	snl_init_writer(ss, &nw);
1471 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETSTATES);
1472 	hdr->nlmsg_flags |= NLM_F_DUMP;
1473 	snl_add_msg_attr_string(&nw, PF_ST_IFNAME, filter->ifname);
1474 	snl_add_msg_attr_u16(&nw, PF_ST_PROTO, filter->proto);
1475 	snl_add_msg_attr_u8(&nw, PF_ST_AF, filter->af);
1476 	snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_ADDR, &filter->addr.v6);
1477 	snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_MASK, &filter->mask.v6);
1478 
1479 	hdr = snl_finalize_msg(&nw);
1480 	if (hdr == NULL)
1481 		return (ENOMEM);
1482 
1483 	uint32_t seq_id = hdr->nlmsg_seq;
1484 
1485 	snl_send_message(ss, hdr);
1486 
1487 	struct snl_errmsg_data e = {};
1488 	while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) {
1489 		struct pfctl_state s;
1490 		bzero(&s, sizeof(s));
1491 		if (!snl_parse_nlmsg(ss, hdr, &state_parser, &s))
1492 			continue;
1493 
1494 		s.key[1].af = s.key[0].af;
1495 		s.key[1].proto = s.key[0].proto;
1496 
1497 		ret = f(&s, arg);
1498 		if (ret != 0)
1499 			return (ret);
1500 	}
1501 
1502 	return (0);
1503 }
1504 
1505 int
1506 pfctl_get_states_iter(pfctl_get_state_fn f, void *arg)
1507 {
1508 	struct pfctl_state_filter filter = {};
1509 	return (pfctl_get_filtered_states_iter(&filter, f, arg));
1510 }
1511 
1512 int
1513 pfctl_get_filtered_states_iter(struct pfctl_state_filter *filter, pfctl_get_state_fn f, void *arg)
1514 {
1515 	struct snl_state ss = {};
1516 	int error;
1517 
1518 	snl_init(&ss, NETLINK_GENERIC);
1519 	error = pfctl_get_states_nl(filter, &ss, f, arg);
1520 	snl_free(&ss);
1521 
1522 	return (error);
1523 }
1524 
1525 static int
1526 pfctl_append_states(struct pfctl_state *s, void *arg)
1527 {
1528 	struct pfctl_state *new;
1529 	struct pfctl_states *states = (struct pfctl_states *)arg;
1530 
1531 	new = malloc(sizeof(*s));
1532 	if (new == NULL)
1533 		return (ENOMEM);
1534 
1535 	memcpy(new, s, sizeof(*s));
1536 
1537 	TAILQ_INSERT_TAIL(&states->states, new, entry);
1538 
1539 	return (0);
1540 }
1541 
1542 int
1543 pfctl_get_states(int dev __unused, struct pfctl_states *states)
1544 {
1545 	int ret;
1546 
1547 	bzero(states, sizeof(*states));
1548 	TAILQ_INIT(&states->states);
1549 
1550 	ret = pfctl_get_states_iter(pfctl_append_states, states);
1551 	if (ret != 0) {
1552 		pfctl_free_states(states);
1553 		return (ret);
1554 	}
1555 
1556 	return (0);
1557 }
1558 
1559 void
1560 pfctl_free_states(struct pfctl_states *states)
1561 {
1562 	struct pfctl_state *s, *tmp;
1563 
1564 	TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) {
1565 		free(s);
1566 	}
1567 
1568 	bzero(states, sizeof(*states));
1569 }
1570 
1571 static int
1572 _pfctl_clear_states(int dev, const struct pfctl_kill *kill,
1573     unsigned int *killed, uint64_t ioctlval)
1574 {
1575 	nvlist_t	*nvl;
1576 	int		 ret;
1577 
1578 	nvl = nvlist_create(0);
1579 
1580 	pfctl_nv_add_state_cmp(nvl, "cmp", &kill->cmp);
1581 	nvlist_add_number(nvl, "af", kill->af);
1582 	nvlist_add_number(nvl, "proto", kill->proto);
1583 	pfctl_nv_add_rule_addr(nvl, "src", &kill->src);
1584 	pfctl_nv_add_rule_addr(nvl, "dst", &kill->dst);
1585 	pfctl_nv_add_rule_addr(nvl, "rt_addr", &kill->rt_addr);
1586 	nvlist_add_string(nvl, "ifname", kill->ifname);
1587 	nvlist_add_string(nvl, "label", kill->label);
1588 	nvlist_add_bool(nvl, "kill_match", kill->kill_match);
1589 	nvlist_add_bool(nvl, "nat", kill->nat);
1590 
1591 	if ((ret = pfctl_do_ioctl(dev, ioctlval, 1024, &nvl)) != 0)
1592 		goto out;
1593 
1594 	if (killed)
1595 		*killed = nvlist_get_number(nvl, "killed");
1596 
1597 out:
1598 	nvlist_destroy(nvl);
1599 	return (ret);
1600 }
1601 
1602 int
1603 pfctl_clear_states(int dev, const struct pfctl_kill *kill,
1604     unsigned int *killed)
1605 {
1606 	return (_pfctl_clear_states(dev, kill, killed, DIOCCLRSTATESNV));
1607 }
1608 
1609 int
1610 pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed)
1611 {
1612 	return (_pfctl_clear_states(dev, kill, killed, DIOCKILLSTATESNV));
1613 }
1614 
1615 int
1616 pfctl_clear_rules(int dev, const char *anchorname)
1617 {
1618 	struct pfioc_trans trans;
1619 	struct pfioc_trans_e transe[2];
1620 	int ret;
1621 
1622 	bzero(&trans, sizeof(trans));
1623 	bzero(&transe, sizeof(transe));
1624 
1625 	transe[0].rs_num = PF_RULESET_SCRUB;
1626 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
1627 	    >= sizeof(transe[0].anchor))
1628 		return (E2BIG);
1629 
1630 	transe[1].rs_num = PF_RULESET_FILTER;
1631 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
1632 	    >= sizeof(transe[1].anchor))
1633 		return (E2BIG);
1634 
1635 	trans.size = 2;
1636 	trans.esize = sizeof(transe[0]);
1637 	trans.array = transe;
1638 
1639 	ret = ioctl(dev, DIOCXBEGIN, &trans);
1640 	if (ret != 0)
1641 		return (ret);
1642 	return ioctl(dev, DIOCXCOMMIT, &trans);
1643 }
1644 
1645 int
1646 pfctl_clear_nat(int dev, const char *anchorname)
1647 {
1648 	struct pfioc_trans trans;
1649 	struct pfioc_trans_e transe[3];
1650 	int ret;
1651 
1652 	bzero(&trans, sizeof(trans));
1653 	bzero(&transe, sizeof(transe));
1654 
1655 	transe[0].rs_num = PF_RULESET_NAT;
1656 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
1657 	    >= sizeof(transe[0].anchor))
1658 		return (E2BIG);
1659 
1660 	transe[1].rs_num = PF_RULESET_BINAT;
1661 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
1662 	    >= sizeof(transe[0].anchor))
1663 		return (E2BIG);
1664 
1665 	transe[2].rs_num = PF_RULESET_RDR;
1666 	if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor))
1667 	    >= sizeof(transe[2].anchor))
1668 		return (E2BIG);
1669 
1670 	trans.size = 3;
1671 	trans.esize = sizeof(transe[0]);
1672 	trans.array = transe;
1673 
1674 	ret = ioctl(dev, DIOCXBEGIN, &trans);
1675 	if (ret != 0)
1676 		return (ret);
1677 	return ioctl(dev, DIOCXCOMMIT, &trans);
1678 }
1679 int
1680 pfctl_clear_eth_rules(int dev, const char *anchorname)
1681 {
1682 	struct pfioc_trans trans;
1683 	struct pfioc_trans_e transe;
1684 	int ret;
1685 
1686 	bzero(&trans, sizeof(trans));
1687 	bzero(&transe, sizeof(transe));
1688 
1689 	transe.rs_num = PF_RULESET_ETH;
1690 	if (strlcpy(transe.anchor, anchorname, sizeof(transe.anchor))
1691 	    >= sizeof(transe.anchor))
1692 		return (E2BIG);
1693 
1694 	trans.size = 1;
1695 	trans.esize = sizeof(transe);
1696 	trans.array = &transe;
1697 
1698 	ret = ioctl(dev, DIOCXBEGIN, &trans);
1699 	if (ret != 0)
1700 		return (ret);
1701 	return ioctl(dev, DIOCXCOMMIT, &trans);
1702 }
1703 
1704 static int
1705 pfctl_get_limit(int dev, const int index, uint *limit)
1706 {
1707 	struct pfioc_limit pl;
1708 
1709 	bzero(&pl, sizeof(pl));
1710 	pl.index = index;
1711 
1712 	if (ioctl(dev, DIOCGETLIMIT, &pl) == -1)
1713 		return (errno);
1714 
1715 	*limit = pl.limit;
1716 
1717 	return (0);
1718 }
1719 
1720 int
1721 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s)
1722 {
1723 	struct pfioc_nv	 nv;
1724 	nvlist_t	*nvl;
1725 	int		 ret;
1726 	uint		 state_limit;
1727 	uint64_t	 lim, hi, lo;
1728 
1729 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
1730 	if (ret != 0)
1731 		return (ret);
1732 
1733 	lim = state_limit;
1734 	hi = lim * s->highwater / 100;
1735 	lo = lim * s->lowwater / 100;
1736 
1737 	if (lo == hi)
1738 		hi++;
1739 
1740 	nvl = nvlist_create(0);
1741 
1742 	nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER);
1743 	nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE);
1744 	nvlist_add_number(nvl, "highwater", hi);
1745 	nvlist_add_number(nvl, "lowwater", lo);
1746 
1747 	nv.data = nvlist_pack(nvl, &nv.len);
1748 	nv.size = nv.len;
1749 	nvlist_destroy(nvl);
1750 	nvl = NULL;
1751 
1752 	ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv);
1753 
1754 	free(nv.data);
1755 	return (ret);
1756 }
1757 
1758 int
1759 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s)
1760 {
1761 	nvlist_t	*nvl;
1762 	int		 ret;
1763 	uint		 state_limit;
1764 	bool		 enabled, adaptive;
1765 
1766 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
1767 	if (ret != 0)
1768 		return (ret);
1769 
1770 	bzero(s, sizeof(*s));
1771 
1772 	nvl = nvlist_create(0);
1773 
1774 	if ((ret = pfctl_do_ioctl(dev, DIOCGETSYNCOOKIES, 256, &nvl)) != 0) {
1775 		ret = errno;
1776 		goto out;
1777 	}
1778 
1779 	enabled = nvlist_get_bool(nvl, "enabled");
1780 	adaptive = nvlist_get_bool(nvl, "adaptive");
1781 
1782 	if (enabled) {
1783 		if (adaptive)
1784 			s->mode = PFCTL_SYNCOOKIES_ADAPTIVE;
1785 		else
1786 			s->mode = PFCTL_SYNCOOKIES_ALWAYS;
1787 	} else {
1788 		s->mode = PFCTL_SYNCOOKIES_NEVER;
1789 	}
1790 
1791 	s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit;
1792 	s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit;
1793 	s->halfopen_states = nvlist_get_number(nvl, "halfopen_states");
1794 
1795 out:
1796 	nvlist_destroy(nvl);
1797 	return (ret);
1798 }
1799 
1800 int
1801 pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
1802     *addr, int size, int *nadd, int flags)
1803 {
1804 	struct pfioc_table io;
1805 
1806 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
1807 		return (EINVAL);
1808 	}
1809 	bzero(&io, sizeof io);
1810 	io.pfrio_flags = flags;
1811 	io.pfrio_table = *tbl;
1812 	io.pfrio_buffer = addr;
1813 	io.pfrio_esize = sizeof(*addr);
1814 	io.pfrio_size = size;
1815 
1816 	if (ioctl(dev, DIOCRADDADDRS, &io))
1817 		return (errno);
1818 	if (nadd != NULL)
1819 		*nadd = io.pfrio_nadd;
1820 	return (0);
1821 }
1822 
1823 int
1824 pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
1825     *addr, int size, int *ndel, int flags)
1826 {
1827 	struct pfioc_table io;
1828 
1829 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
1830 		return (EINVAL);
1831 	}
1832 	bzero(&io, sizeof io);
1833 	io.pfrio_flags = flags;
1834 	io.pfrio_table = *tbl;
1835 	io.pfrio_buffer = addr;
1836 	io.pfrio_esize = sizeof(*addr);
1837 	io.pfrio_size = size;
1838 
1839 	if (ioctl(dev, DIOCRDELADDRS, &io))
1840 		return (errno);
1841 	if (ndel != NULL)
1842 		*ndel = io.pfrio_ndel;
1843 	return (0);
1844 }
1845 
1846 int
1847 pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
1848     *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags)
1849 {
1850 	struct pfioc_table io;
1851 
1852 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
1853 		return (EINVAL);
1854 	}
1855 	bzero(&io, sizeof io);
1856 	io.pfrio_flags = flags;
1857 	io.pfrio_table = *tbl;
1858 	io.pfrio_buffer = addr;
1859 	io.pfrio_esize = sizeof(*addr);
1860 	io.pfrio_size = size;
1861 	io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
1862 	if (ioctl(dev, DIOCRSETADDRS, &io))
1863 		return (-1);
1864 	if (nadd != NULL)
1865 		*nadd = io.pfrio_nadd;
1866 	if (ndel != NULL)
1867 		*ndel = io.pfrio_ndel;
1868 	if (nchange != NULL)
1869 		*nchange = io.pfrio_nchange;
1870 	if (size2 != NULL)
1871 		*size2 = io.pfrio_size2;
1872 	return (0);
1873 }
1874 
1875 int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr,
1876     int *size, int flags)
1877 {
1878 	struct pfioc_table io;
1879 
1880 	if (tbl == NULL || size == NULL || *size < 0 ||
1881 	    (*size && addr == NULL)) {
1882 		return (EINVAL);
1883 	}
1884 	bzero(&io, sizeof io);
1885 	io.pfrio_flags = flags;
1886 	io.pfrio_table = *tbl;
1887 	io.pfrio_buffer = addr;
1888 	io.pfrio_esize = sizeof(*addr);
1889 	io.pfrio_size = *size;
1890 	if (ioctl(dev, DIOCRGETADDRS, &io))
1891 		return (-1);
1892 	*size = io.pfrio_size;
1893 	return (0);
1894 }
1895