xref: /freebsd/lib/libpfctl/libpfctl.c (revision aa48259f337100e79933d660fec8856371f761ed)
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 int
106 pfctl_fd(struct pfctl_handle *h)
107 {
108 	return (h->fd);
109 }
110 
111 static int
112 pfctl_do_ioctl(int dev, uint cmd, size_t size, nvlist_t **nvl)
113 {
114 	struct pfioc_nv nv;
115 	void *data;
116 	size_t nvlen;
117 	int ret;
118 
119 	data = nvlist_pack(*nvl, &nvlen);
120 	if (nvlen > size)
121 		size = nvlen;
122 
123 retry:
124 	nv.data = malloc(size);
125 	if (nv.data == NULL) {
126 		ret = ENOMEM;
127 		goto out;
128 	}
129 
130 	memcpy(nv.data, data, nvlen);
131 
132 	nv.len = nvlen;
133 	nv.size = size;
134 
135 	ret = ioctl(dev, cmd, &nv);
136 	if (ret == -1 && errno == ENOSPC) {
137 		size *= 2;
138 		free(nv.data);
139 		goto retry;
140 	}
141 
142 	nvlist_destroy(*nvl);
143 	*nvl = NULL;
144 
145 	if (ret == 0) {
146 		*nvl = nvlist_unpack(nv.data, nv.len, 0);
147 		if (*nvl == NULL) {
148 			ret = EIO;
149 			goto out;
150 		}
151 	} else {
152 		ret = errno;
153 	}
154 
155 out:
156 	free(data);
157 	free(nv.data);
158 
159 	return (ret);
160 }
161 
162 static void
163 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems,
164     uint8_t *numbers, size_t *nelems)
165 {
166 	const uint64_t *tmp;
167 	size_t elems;
168 
169 	tmp = nvlist_get_number_array(nvl, name, &elems);
170 	assert(elems <= maxelems);
171 
172 	for (size_t i = 0; i < elems; i++)
173 		numbers[i] = tmp[i];
174 
175 	if (nelems)
176 		*nelems = elems;
177 }
178 
179 static void
180 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems,
181     uint16_t *numbers, size_t *nelems)
182 {
183 	const uint64_t *tmp;
184 	size_t elems;
185 
186 	tmp = nvlist_get_number_array(nvl, name, &elems);
187 	assert(elems <= maxelems);
188 
189 	for (size_t i = 0; i < elems; i++)
190 		numbers[i] = tmp[i];
191 
192 	if (nelems)
193 		*nelems = elems;
194 }
195 
196 static void
197 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems,
198     uint32_t *numbers, size_t *nelems)
199 {
200 	const uint64_t *tmp;
201 	size_t elems;
202 
203 	tmp = nvlist_get_number_array(nvl, name, &elems);
204 
205 	for (size_t i = 0; i < elems && i < maxelems; i++)
206 		numbers[i] = tmp[i];
207 
208 	if (nelems)
209 		*nelems = elems;
210 }
211 
212 static void
213 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems,
214     uint64_t *numbers, size_t *nelems)
215 {
216 	const uint64_t *tmp;
217 	size_t elems;
218 
219 	tmp = nvlist_get_number_array(nvl, name, &elems);
220 	assert(elems <= maxelems);
221 
222 	for (size_t i = 0; i < elems; i++)
223 		numbers[i] = tmp[i];
224 
225 	if (nelems)
226 		*nelems = elems;
227 }
228 
229 int
230 pfctl_startstop(struct pfctl_handle *h, int start)
231 {
232 	struct snl_errmsg_data e = {};
233 	struct snl_writer nw;
234 	struct nlmsghdr *hdr;
235 	uint32_t seq_id;
236 	int family_id;
237 
238 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
239 	if (family_id == 0)
240 		return (ENOTSUP);
241 
242 	snl_init_writer(&h->ss, &nw);
243 	hdr = snl_create_genl_msg_request(&nw, family_id,
244 	    start ? PFNL_CMD_START : PFNL_CMD_STOP);
245 
246 	hdr = snl_finalize_msg(&nw);
247 	if (hdr == NULL)
248 		return (ENOMEM);
249 	seq_id = hdr->nlmsg_seq;
250 
251 	snl_send_message(&h->ss, hdr);
252 
253 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
254 	}
255 
256 	return (e.error);
257 }
258 
259 static void
260 _pfctl_get_status_counters(const nvlist_t *nvl,
261     struct pfctl_status_counters *counters)
262 {
263 	const uint64_t		*ids, *counts;
264 	const char *const	*names;
265 	size_t id_len, counter_len, names_len;
266 
267 	ids = nvlist_get_number_array(nvl, "ids", &id_len);
268 	counts = nvlist_get_number_array(nvl, "counters", &counter_len);
269 	names = nvlist_get_string_array(nvl, "names", &names_len);
270 	assert(id_len == counter_len);
271 	assert(counter_len == names_len);
272 
273 	TAILQ_INIT(counters);
274 
275 	for (size_t i = 0; i < id_len; i++) {
276 		struct pfctl_status_counter *c;
277 
278 		c = malloc(sizeof(*c));
279 		if (c == NULL)
280 			continue;
281 
282 		c->id = ids[i];
283 		c->counter = counts[i];
284 		c->name = strdup(names[i]);
285 
286 		TAILQ_INSERT_TAIL(counters, c, entry);
287 	}
288 }
289 
290 #define	_OUT(_field)	offsetof(struct pfctl_status_counter, _field)
291 static const struct snl_attr_parser ap_counter[] = {
292 	{ .type = PF_C_COUNTER, .off = _OUT(counter), .cb = snl_attr_get_uint64 },
293 	{ .type = PF_C_NAME, .off = _OUT(name), .cb = snl_attr_get_string },
294 	{ .type = PF_C_ID, .off = _OUT(id), .cb = snl_attr_get_uint32 },
295 };
296 SNL_DECLARE_ATTR_PARSER(counter_parser, ap_counter);
297 #undef _OUT
298 
299 static bool
300 snl_attr_get_counters(struct snl_state *ss, struct nlattr *nla,
301     const void *arg __unused, void *target)
302 {
303 	struct pfctl_status_counter counter = {};
304 	struct pfctl_status_counter *c;
305 	bool error;
306 
307 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &counter_parser, &counter);
308 	if (! error)
309 		return (error);
310 
311 	c = malloc(sizeof(*c));
312 	if (c == NULL)
313 		return (false);
314 
315 	c->id = counter.id;
316 	c->counter = counter.counter;
317 	c->name = strdup(counter.name);
318 
319 	TAILQ_INSERT_TAIL((struct pfctl_status_counters *)target, c, entry);
320 
321 	return (error);
322 }
323 
324 struct snl_uint64_array {
325 	uint64_t *array;
326 	size_t count;
327 	size_t max;
328 };
329 static bool
330 snl_attr_get_uint64_element(struct snl_state *ss, struct nlattr *nla,
331     const void *arg, void *target)
332 {
333 	bool error;
334 	uint64_t value;
335 	struct snl_uint64_array *t = (struct snl_uint64_array *)target;
336 
337 	if (t->count >= t->max)
338 		return (false);
339 
340 	error = snl_attr_get_uint64(ss, nla, arg, &value);
341 	if (! error)
342 		return (error);
343 
344 	t->array[t->count++] = value;
345 
346 	return (true);
347 }
348 
349 static const struct snl_attr_parser ap_array[] = {
350 	{ .cb = snl_attr_get_uint64_element },
351 };
352 SNL_DECLARE_ATTR_PARSER(array_parser, ap_array);
353 static bool
354 snl_attr_get_uint64_array(struct snl_state *ss, struct nlattr *nla,
355     const void *arg, void *target)
356 {
357 	struct snl_uint64_array a = {
358 		.array = target,
359 		.count = 0,
360 		.max = (size_t)arg,
361 	};
362 	bool error;
363 
364 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &array_parser, &a);
365 	if (! error)
366 		return (error);
367 
368 	return (true);
369 }
370 
371 #define	_OUT(_field)	offsetof(struct pfctl_status, _field)
372 static const struct snl_attr_parser ap_getstatus[] = {
373 	{ .type = PF_GS_IFNAME, .off = _OUT(ifname), .arg_u32 = IFNAMSIZ, .cb = snl_attr_copy_string },
374 	{ .type = PF_GS_RUNNING, .off = _OUT(running), .cb = snl_attr_get_bool },
375 	{ .type = PF_GS_SINCE, .off = _OUT(since), .cb = snl_attr_get_uint32 },
376 	{ .type = PF_GS_DEBUG, .off = _OUT(debug), .cb = snl_attr_get_uint32 },
377 	{ .type = PF_GS_HOSTID, .off = _OUT(hostid), .cb = snl_attr_get_uint32 },
378 	{ .type = PF_GS_STATES, .off = _OUT(states), .cb = snl_attr_get_uint32 },
379 	{ .type = PF_GS_SRC_NODES, .off = _OUT(src_nodes), .cb = snl_attr_get_uint32 },
380 	{ .type = PF_GS_REASSEMBLE, .off = _OUT(reass), .cb = snl_attr_get_uint32 },
381 	{ .type = PF_GS_SYNCOOKIES_ACTIVE, .off = _OUT(syncookies_active), .cb = snl_attr_get_uint32 },
382 	{ .type = PF_GS_COUNTERS, .off = _OUT(counters), .cb = snl_attr_get_counters },
383 	{ .type = PF_GS_LCOUNTERS, .off = _OUT(lcounters), .cb = snl_attr_get_counters },
384 	{ .type = PF_GS_FCOUNTERS, .off = _OUT(fcounters), .cb = snl_attr_get_counters },
385 	{ .type = PF_GS_SCOUNTERS, .off = _OUT(scounters), .cb = snl_attr_get_counters },
386 	{ .type = PF_GS_CHKSUM, .off = _OUT(pf_chksum), .arg_u32 = PF_MD5_DIGEST_LENGTH, .cb = snl_attr_get_bytes },
387 	{ .type = PF_GS_BCOUNTERS, .off = _OUT(bcounters), .arg_u32 = 2 * 2, .cb = snl_attr_get_uint64_array },
388 	{ .type = PF_GS_PCOUNTERS, .off = _OUT(pcounters), .arg_u32 = 2 * 2 * 2, .cb = snl_attr_get_uint64_array },
389 };
390 static struct snl_field_parser fp_getstatus[] = {};
391 SNL_DECLARE_PARSER(getstatus_parser, struct genlmsghdr, fp_getstatus, ap_getstatus);
392 #undef _OUT
393 
394 struct pfctl_status *
395 pfctl_get_status_h(struct pfctl_handle *h __unused)
396 {
397 	struct pfctl_status	*status;
398 	struct snl_errmsg_data e = {};
399 	struct nlmsghdr *hdr;
400 	struct snl_writer nw;
401 	uint32_t seq_id;
402 	int family_id;
403 
404 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
405 	if (family_id == 0)
406 		return (NULL);
407 
408 	snl_init_writer(&h->ss, &nw);
409 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_STATUS);
410 	hdr->nlmsg_flags |= NLM_F_DUMP;
411 
412 	hdr = snl_finalize_msg(&nw);
413 	if (hdr == NULL) {
414 		return (NULL);
415 	}
416 
417 	seq_id = hdr->nlmsg_seq;
418 	if (! snl_send_message(&h->ss, hdr))
419 		return (NULL);
420 
421 	status = calloc(1, sizeof(*status));
422 	if (status == NULL)
423 		return (NULL);
424 	TAILQ_INIT(&status->counters);
425 	TAILQ_INIT(&status->lcounters);
426 	TAILQ_INIT(&status->fcounters);
427 	TAILQ_INIT(&status->scounters);
428 
429 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
430 		if (! snl_parse_nlmsg(&h->ss, hdr, &getstatus_parser, status))
431 			continue;
432 	}
433 
434 	return (status);
435 }
436 
437 struct pfctl_status *
438 pfctl_get_status(int dev)
439 {
440 	struct pfctl_status	*status;
441 	nvlist_t	*nvl;
442 	size_t		 len;
443 	const void	*chksum;
444 
445 	status = calloc(1, sizeof(*status));
446 	if (status == NULL)
447 		return (NULL);
448 
449 	nvl = nvlist_create(0);
450 
451 	if (pfctl_do_ioctl(dev, DIOCGETSTATUSNV, 4096, &nvl)) {
452 		nvlist_destroy(nvl);
453 		free(status);
454 		return (NULL);
455 	}
456 
457 	status->running = nvlist_get_bool(nvl, "running");
458 	status->since = nvlist_get_number(nvl, "since");
459 	status->debug = nvlist_get_number(nvl, "debug");
460 	status->hostid = ntohl(nvlist_get_number(nvl, "hostid"));
461 	status->states = nvlist_get_number(nvl, "states");
462 	status->src_nodes = nvlist_get_number(nvl, "src_nodes");
463 	status->syncookies_active = nvlist_get_bool(nvl, "syncookies_active");
464 	status->reass = nvlist_get_number(nvl, "reass");
465 
466 	strlcpy(status->ifname, nvlist_get_string(nvl, "ifname"),
467 	    IFNAMSIZ);
468 	chksum = nvlist_get_binary(nvl, "chksum", &len);
469 	assert(len == PF_MD5_DIGEST_LENGTH);
470 	memcpy(status->pf_chksum, chksum, len);
471 
472 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "counters"),
473 	    &status->counters);
474 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "lcounters"),
475 	    &status->lcounters);
476 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "fcounters"),
477 	    &status->fcounters);
478 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"),
479 	    &status->scounters);
480 
481 	pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 2,
482 	    (uint64_t *)status->pcounters, NULL);
483 	pf_nvuint_64_array(nvl, "bcounters", 2 * 2,
484 	    (uint64_t *)status->bcounters, NULL);
485 
486 	nvlist_destroy(nvl);
487 
488 	return (status);
489 }
490 
491 static uint64_t
492 _pfctl_status_counter(struct pfctl_status_counters *counters, uint64_t id)
493 {
494 	struct pfctl_status_counter *c;
495 
496 	TAILQ_FOREACH(c, counters, entry) {
497 		if (c->id == id)
498 			return (c->counter);
499 	}
500 
501 	return (0);
502 }
503 
504 uint64_t
505 pfctl_status_counter(struct pfctl_status *status, int id)
506 {
507 	return (_pfctl_status_counter(&status->counters, id));
508 }
509 
510 uint64_t
511 pfctl_status_lcounter(struct pfctl_status *status, int id)
512 {
513 	return (_pfctl_status_counter(&status->lcounters, id));
514 }
515 
516 uint64_t
517 pfctl_status_fcounter(struct pfctl_status *status, int id)
518 {
519 	return (_pfctl_status_counter(&status->fcounters, id));
520 }
521 
522 uint64_t
523 pfctl_status_scounter(struct pfctl_status *status, int id)
524 {
525 	return (_pfctl_status_counter(&status->scounters, id));
526 }
527 
528 void
529 pfctl_free_status(struct pfctl_status *status)
530 {
531 	struct pfctl_status_counter *c, *tmp;
532 
533 	if (status == NULL)
534 		return;
535 
536 	TAILQ_FOREACH_SAFE(c, &status->counters, entry, tmp) {
537 		free(c->name);
538 		free(c);
539 	}
540 	TAILQ_FOREACH_SAFE(c, &status->lcounters, entry, tmp) {
541 		free(c->name);
542 		free(c);
543 	}
544 	TAILQ_FOREACH_SAFE(c, &status->fcounters, entry, tmp) {
545 		free(c->name);
546 		free(c);
547 	}
548 	TAILQ_FOREACH_SAFE(c, &status->scounters, entry, tmp) {
549 		free(c->name);
550 		free(c);
551 	}
552 
553 	free(status);
554 }
555 
556 static void
557 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name,
558     const struct pf_addr *addr)
559 {
560 	nvlist_t *nvl = nvlist_create(0);
561 
562 	nvlist_add_binary(nvl, "addr", addr, sizeof(*addr));
563 
564 	nvlist_add_nvlist(nvparent, name, nvl);
565 	nvlist_destroy(nvl);
566 }
567 
568 static void
569 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr)
570 {
571 	size_t len;
572 	const void *data;
573 
574 	data = nvlist_get_binary(nvl, "addr", &len);
575 	assert(len == sizeof(struct pf_addr));
576 	memcpy(addr, data, len);
577 }
578 
579 static void
580 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name,
581     const struct pf_addr_wrap *addr)
582 {
583 	nvlist_t *nvl = nvlist_create(0);
584 
585 	nvlist_add_number(nvl, "type", addr->type);
586 	nvlist_add_number(nvl, "iflags", addr->iflags);
587 	if (addr->type == PF_ADDR_DYNIFTL)
588 		nvlist_add_string(nvl, "ifname", addr->v.ifname);
589 	if (addr->type == PF_ADDR_TABLE)
590 		nvlist_add_string(nvl, "tblname", addr->v.tblname);
591 	pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr);
592 	pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask);
593 
594 	nvlist_add_nvlist(nvparent, name, nvl);
595 	nvlist_destroy(nvl);
596 }
597 
598 static void
599 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
600 {
601 	bzero(addr, sizeof(*addr));
602 
603 	addr->type = nvlist_get_number(nvl, "type");
604 	addr->iflags = nvlist_get_number(nvl, "iflags");
605 	if (addr->type == PF_ADDR_DYNIFTL) {
606 		strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"),
607 		    IFNAMSIZ);
608 		addr->p.dyncnt = nvlist_get_number(nvl, "dyncnt");
609 	}
610 	if (addr->type == PF_ADDR_TABLE) {
611 		strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"),
612 		    PF_TABLE_NAME_SIZE);
613 		addr->p.tblcnt = nvlist_get_number(nvl, "tblcnt");
614 	}
615 
616 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr);
617 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask);
618 }
619 
620 static void
621 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name,
622     const struct pf_rule_addr *addr)
623 {
624 	uint64_t ports[2];
625 	nvlist_t *nvl = nvlist_create(0);
626 
627 	pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr);
628 	ports[0] = addr->port[0];
629 	ports[1] = addr->port[1];
630 	nvlist_add_number_array(nvl, "port", ports, 2);
631 	nvlist_add_number(nvl, "neg", addr->neg);
632 	nvlist_add_number(nvl, "port_op", addr->port_op);
633 
634 	nvlist_add_nvlist(nvparent, name, nvl);
635 	nvlist_destroy(nvl);
636 }
637 
638 static void
639 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
640 {
641 	pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr);
642 
643 	pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL);
644 	addr->neg = nvlist_get_number(nvl, "neg");
645 	addr->port_op = nvlist_get_number(nvl, "port_op");
646 }
647 
648 static void
649 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape)
650 {
651 	mape->offset = nvlist_get_number(nvl, "offset");
652 	mape->psidlen = nvlist_get_number(nvl, "psidlen");
653 	mape->psid = nvlist_get_number(nvl, "psid");
654 }
655 
656 static void
657 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool)
658 {
659 	size_t len;
660 	const void *data;
661 
662 	data = nvlist_get_binary(nvl, "key", &len);
663 	assert(len == sizeof(pool->key));
664 	memcpy(&pool->key, data, len);
665 
666 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter);
667 
668 	pool->tblidx = nvlist_get_number(nvl, "tblidx");
669 	pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL);
670 	pool->opts = nvlist_get_number(nvl, "opts");
671 
672 	if (nvlist_exists_nvlist(nvl, "mape"))
673 		pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape);
674 }
675 
676 static void
677 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
678 {
679 	pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL);
680 	uid->op = nvlist_get_number(nvl, "op");
681 }
682 
683 static void
684 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule)
685 {
686 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr);
687 	rule->divert.port = nvlist_get_number(nvl, "port");
688 }
689 
690 static void
691 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule)
692 {
693 	const uint64_t *skip;
694 	const char *const *labels;
695 	size_t skipcount, labelcount;
696 
697 	rule->nr = nvlist_get_number(nvl, "nr");
698 
699 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src);
700 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst);
701 
702 	skip = nvlist_get_number_array(nvl, "skip", &skipcount);
703 	assert(skip);
704 	assert(skipcount == PF_SKIP_COUNT);
705 	for (int i = 0; i < PF_SKIP_COUNT; i++)
706 		rule->skip[i].nr = skip[i];
707 
708 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
709 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
710 	for (size_t i = 0; i < labelcount; i++)
711 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
712 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
713 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
714 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
715 	strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE);
716 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
717 	    PF_TAG_NAME_SIZE);
718 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
719 	    PF_TAG_NAME_SIZE);
720 
721 	strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"),
722 	    PF_TABLE_NAME_SIZE);
723 
724 	pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool);
725 
726 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
727 	pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL);
728 	pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL);
729 
730 	if (nvlist_exists_number(nvl, "timestamp")) {
731 		rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp");
732 	}
733 
734 	rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint");
735 
736 	rule->rtableid = nvlist_get_number(nvl, "rtableid");
737 	pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL);
738 	rule->max_states = nvlist_get_number(nvl, "max_states");
739 	rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes");
740 	rule->max_src_states = nvlist_get_number(nvl, "max_src_states");
741 	rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn");
742 	rule->max_src_conn_rate.limit =
743 	    nvlist_get_number(nvl, "max_src_conn_rate.limit");
744 	rule->max_src_conn_rate.seconds =
745 	    nvlist_get_number(nvl, "max_src_conn_rate.seconds");
746 	rule->qid = nvlist_get_number(nvl, "qid");
747 	rule->pqid = nvlist_get_number(nvl, "pqid");
748 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
749 	rule->dnrpipe = nvlist_get_number(nvl, "dnrpipe");
750 	rule->free_flags = nvlist_get_number(nvl, "dnflags");
751 	rule->prob = nvlist_get_number(nvl, "prob");
752 	rule->cuid = nvlist_get_number(nvl, "cuid");
753 	rule->cpid = nvlist_get_number(nvl, "cpid");
754 
755 	rule->return_icmp = nvlist_get_number(nvl, "return_icmp");
756 	rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6");
757 	rule->max_mss = nvlist_get_number(nvl, "max_mss");
758 	rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags");
759 
760 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid);
761 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"),
762 	    (struct pf_rule_uid *)&rule->gid);
763 
764 	rule->rule_flag = nvlist_get_number(nvl, "rule_flag");
765 	rule->action = nvlist_get_number(nvl, "action");
766 	rule->direction = nvlist_get_number(nvl, "direction");
767 	rule->log = nvlist_get_number(nvl, "log");
768 	rule->logif = nvlist_get_number(nvl, "logif");
769 	rule->quick = nvlist_get_number(nvl, "quick");
770 	rule->ifnot = nvlist_get_number(nvl, "ifnot");
771 	rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not");
772 	rule->natpass = nvlist_get_number(nvl, "natpass");
773 
774 	rule->keep_state = nvlist_get_number(nvl, "keep_state");
775 	rule->af = nvlist_get_number(nvl, "af");
776 	rule->proto = nvlist_get_number(nvl, "proto");
777 	rule->type = nvlist_get_number(nvl, "type");
778 	rule->code = nvlist_get_number(nvl, "code");
779 	rule->flags = nvlist_get_number(nvl, "flags");
780 	rule->flagset = nvlist_get_number(nvl, "flagset");
781 	rule->min_ttl = nvlist_get_number(nvl, "min_ttl");
782 	rule->allow_opts = nvlist_get_number(nvl, "allow_opts");
783 	rule->rt = nvlist_get_number(nvl, "rt");
784 	rule->return_ttl  = nvlist_get_number(nvl, "return_ttl");
785 	rule->tos = nvlist_get_number(nvl, "tos");
786 	rule->set_tos = nvlist_get_number(nvl, "set_tos");
787 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
788 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
789 
790 	rule->flush = nvlist_get_number(nvl, "flush");
791 	rule->prio = nvlist_get_number(nvl, "prio");
792 	pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL);
793 
794 	pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule);
795 
796 	rule->states_cur = nvlist_get_number(nvl, "states_cur");
797 	rule->states_tot = nvlist_get_number(nvl, "states_tot");
798 	rule->src_nodes = nvlist_get_number(nvl, "src_nodes");
799 }
800 
801 static void
802 pfctl_nveth_addr_to_eth_addr(const nvlist_t *nvl, struct pfctl_eth_addr *addr)
803 {
804 	static const u_int8_t EMPTY_MAC[ETHER_ADDR_LEN] = { 0 };
805 	size_t len;
806 	const void *data;
807 
808 	data = nvlist_get_binary(nvl, "addr", &len);
809 	assert(len == sizeof(addr->addr));
810 	memcpy(addr->addr, data, sizeof(addr->addr));
811 
812 	data = nvlist_get_binary(nvl, "mask", &len);
813 	assert(len == sizeof(addr->mask));
814 	memcpy(addr->mask, data, sizeof(addr->mask));
815 
816 	addr->neg = nvlist_get_bool(nvl, "neg");
817 
818 	/* To make checks for 'is this address set?' easier. */
819 	addr->isset = memcmp(addr->addr, EMPTY_MAC, ETHER_ADDR_LEN) != 0;
820 }
821 
822 static nvlist_t *
823 pfctl_eth_addr_to_nveth_addr(const struct pfctl_eth_addr *addr)
824 {
825 	nvlist_t *nvl;
826 
827 	nvl = nvlist_create(0);
828 	if (nvl == NULL)
829 		return (NULL);
830 
831 	nvlist_add_bool(nvl, "neg", addr->neg);
832 	nvlist_add_binary(nvl, "addr", &addr->addr, ETHER_ADDR_LEN);
833 	nvlist_add_binary(nvl, "mask", &addr->mask, ETHER_ADDR_LEN);
834 
835 	return (nvl);
836 }
837 
838 static void
839 pfctl_nveth_rule_to_eth_rule(const nvlist_t *nvl, struct pfctl_eth_rule *rule)
840 {
841 	const char *const *labels;
842 	size_t labelcount, i;
843 
844 	rule->nr = nvlist_get_number(nvl, "nr");
845 	rule->quick = nvlist_get_bool(nvl, "quick");
846 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
847 	rule->ifnot = nvlist_get_bool(nvl, "ifnot");
848 	rule->direction = nvlist_get_number(nvl, "direction");
849 	rule->proto = nvlist_get_number(nvl, "proto");
850 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
851 	    PF_TAG_NAME_SIZE);
852 	rule->match_tag = nvlist_get_number(nvl, "match_tag");
853 	rule->match_tag_not = nvlist_get_bool(nvl, "match_tag_not");
854 
855 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
856 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
857 	for (i = 0; i < labelcount; i++)
858 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
859 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
860 
861 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "src"),
862 	    &rule->src);
863 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "dst"),
864 	    &rule->dst);
865 
866 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipsrc"),
867 	    &rule->ipsrc);
868 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipdst"),
869 	    &rule->ipdst);
870 
871 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
872 	rule->packets[0] = nvlist_get_number(nvl, "packets-in");
873 	rule->packets[1] = nvlist_get_number(nvl, "packets-out");
874 	rule->bytes[0] = nvlist_get_number(nvl, "bytes-in");
875 	rule->bytes[1] = nvlist_get_number(nvl, "bytes-out");
876 
877 	if (nvlist_exists_number(nvl, "timestamp")) {
878 		rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp");
879 	}
880 
881 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
882 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
883 	    PF_TAG_NAME_SIZE);
884 
885 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
886 	rule->dnflags = nvlist_get_number(nvl, "dnflags");
887 
888 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
889 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
890 
891 	strlcpy(rule->bridge_to, nvlist_get_string(nvl, "bridge_to"),
892 	    IFNAMSIZ);
893 
894 	rule->action = nvlist_get_number(nvl, "action");
895 }
896 
897 int
898 pfctl_get_eth_rulesets_info(int dev, struct pfctl_eth_rulesets_info *ri,
899     const char *path)
900 {
901 	nvlist_t *nvl;
902 	int ret;
903 
904 	bzero(ri, sizeof(*ri));
905 
906 	nvl = nvlist_create(0);
907 	nvlist_add_string(nvl, "path", path);
908 
909 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESETS, 256, &nvl)) != 0)
910 		goto out;
911 
912 	ri->nr = nvlist_get_number(nvl, "nr");
913 
914 out:
915 	nvlist_destroy(nvl);
916 	return (ret);
917 }
918 
919 int
920 pfctl_get_eth_ruleset(int dev, const char *path, int nr,
921     struct pfctl_eth_ruleset_info *ri)
922 {
923 	nvlist_t *nvl;
924 	int ret;
925 
926 	bzero(ri, sizeof(*ri));
927 
928 	nvl = nvlist_create(0);
929 	nvlist_add_string(nvl, "path", path);
930 	nvlist_add_number(nvl, "nr", nr);
931 
932 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESET, 1024, &nvl)) != 0)
933 		goto out;
934 
935 	ri->nr = nvlist_get_number(nvl, "nr");
936 	strlcpy(ri->path, nvlist_get_string(nvl, "path"), MAXPATHLEN);
937 	strlcpy(ri->name, nvlist_get_string(nvl, "name"),
938 	    PF_ANCHOR_NAME_SIZE);
939 
940 out:
941 	nvlist_destroy(nvl);
942 	return (ret);
943 }
944 
945 int
946 pfctl_get_eth_rules_info(int dev, struct pfctl_eth_rules_info *rules,
947     const char *path)
948 {
949 	nvlist_t *nvl;
950 	int ret;
951 
952 	bzero(rules, sizeof(*rules));
953 
954 	nvl = nvlist_create(0);
955 	nvlist_add_string(nvl, "anchor", path);
956 
957 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULES, 1024, &nvl)) != 0)
958 		goto out;
959 
960 	rules->nr = nvlist_get_number(nvl, "nr");
961 	rules->ticket = nvlist_get_number(nvl, "ticket");
962 
963 out:
964 	nvlist_destroy(nvl);
965 	return (ret);
966 }
967 
968 int
969 pfctl_get_eth_rule(int dev, uint32_t nr, uint32_t ticket,
970     const char *path, struct pfctl_eth_rule *rule, bool clear,
971     char *anchor_call)
972 {
973 	nvlist_t *nvl;
974 	int ret;
975 
976 	nvl = nvlist_create(0);
977 
978 	nvlist_add_string(nvl, "anchor", path);
979 	nvlist_add_number(nvl, "ticket", ticket);
980 	nvlist_add_number(nvl, "nr", nr);
981 	nvlist_add_bool(nvl, "clear", clear);
982 
983 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULE, 4096, &nvl)) != 0)
984 		goto out;
985 
986 	pfctl_nveth_rule_to_eth_rule(nvl, rule);
987 
988 	if (anchor_call)
989 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
990 		    MAXPATHLEN);
991 
992 out:
993 	nvlist_destroy(nvl);
994 	return (ret);
995 }
996 
997 int
998 pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r, const char *anchor,
999     const char *anchor_call, uint32_t ticket)
1000 {
1001 	struct pfioc_nv nv;
1002 	nvlist_t *nvl, *addr;
1003 	void *packed;
1004 	int error = 0;
1005 	size_t labelcount, size;
1006 
1007 	nvl = nvlist_create(0);
1008 
1009 	nvlist_add_number(nvl, "ticket", ticket);
1010 	nvlist_add_string(nvl, "anchor", anchor);
1011 	nvlist_add_string(nvl, "anchor_call", anchor_call);
1012 
1013 	nvlist_add_number(nvl, "nr", r->nr);
1014 	nvlist_add_bool(nvl, "quick", r->quick);
1015 	nvlist_add_string(nvl, "ifname", r->ifname);
1016 	nvlist_add_bool(nvl, "ifnot", r->ifnot);
1017 	nvlist_add_number(nvl, "direction", r->direction);
1018 	nvlist_add_number(nvl, "proto", r->proto);
1019 	nvlist_add_string(nvl, "match_tagname", r->match_tagname);
1020 	nvlist_add_bool(nvl, "match_tag_not", r->match_tag_not);
1021 
1022 	addr = pfctl_eth_addr_to_nveth_addr(&r->src);
1023 	if (addr == NULL) {
1024 		nvlist_destroy(nvl);
1025 		return (ENOMEM);
1026 	}
1027 	nvlist_add_nvlist(nvl, "src", addr);
1028 	nvlist_destroy(addr);
1029 
1030 	addr = pfctl_eth_addr_to_nveth_addr(&r->dst);
1031 	if (addr == NULL) {
1032 		nvlist_destroy(nvl);
1033 		return (ENOMEM);
1034 	}
1035 	nvlist_add_nvlist(nvl, "dst", addr);
1036 	nvlist_destroy(addr);
1037 
1038 	pfctl_nv_add_rule_addr(nvl, "ipsrc", &r->ipsrc);
1039 	pfctl_nv_add_rule_addr(nvl, "ipdst", &r->ipdst);
1040 
1041 	labelcount = 0;
1042 	while (labelcount < PF_RULE_MAX_LABEL_COUNT &&
1043 	    r->label[labelcount][0] != 0) {
1044 		nvlist_append_string_array(nvl, "labels",
1045 		    r->label[labelcount]);
1046 		labelcount++;
1047 	}
1048 	nvlist_add_number(nvl, "ridentifier", r->ridentifier);
1049 
1050 	nvlist_add_string(nvl, "qname", r->qname);
1051 	nvlist_add_string(nvl, "tagname", r->tagname);
1052 	nvlist_add_number(nvl, "dnpipe", r->dnpipe);
1053 	nvlist_add_number(nvl, "dnflags", r->dnflags);
1054 
1055 	nvlist_add_string(nvl, "bridge_to", r->bridge_to);
1056 
1057 	nvlist_add_number(nvl, "action", r->action);
1058 
1059 	packed = nvlist_pack(nvl, &size);
1060 	if (packed == NULL) {
1061 		nvlist_destroy(nvl);
1062 		return (ENOMEM);
1063 	}
1064 
1065 	nv.len = size;
1066 	nv.size = size;
1067 	nv.data = packed;
1068 
1069 	if (ioctl(dev, DIOCADDETHRULE, &nv) != 0)
1070 		error = errno;
1071 
1072 	free(packed);
1073 	nvlist_destroy(nvl);
1074 
1075 	return (error);
1076 }
1077 
1078 static void
1079 snl_add_msg_attr_addr_wrap(struct snl_writer *nw, uint32_t type, const struct pf_addr_wrap *addr)
1080 {
1081 	int off;
1082 
1083 	off = snl_add_msg_attr_nested(nw, type);
1084 
1085 	snl_add_msg_attr_ip6(nw, PF_AT_ADDR, &addr->v.a.addr.v6);
1086 	snl_add_msg_attr_ip6(nw, PF_AT_MASK, &addr->v.a.mask.v6);
1087 
1088 	if (addr->type == PF_ADDR_DYNIFTL)
1089 		snl_add_msg_attr_string(nw, PF_AT_IFNAME, addr->v.ifname);
1090 	if (addr->type == PF_ADDR_TABLE)
1091 		snl_add_msg_attr_string(nw, PF_AT_TABLENAME, addr->v.tblname);
1092 	snl_add_msg_attr_u8(nw, PF_AT_TYPE, addr->type);
1093 	snl_add_msg_attr_u8(nw, PF_AT_IFLAGS, addr->iflags);
1094 
1095 	snl_end_attr_nested(nw, off);
1096 }
1097 
1098 static void
1099 snl_add_msg_attr_rule_addr(struct snl_writer *nw, uint32_t type, const struct pf_rule_addr *addr)
1100 {
1101 	int off;
1102 
1103 	off = snl_add_msg_attr_nested(nw, type);
1104 
1105 	snl_add_msg_attr_addr_wrap(nw, PF_RAT_ADDR, &addr->addr);
1106 	snl_add_msg_attr_u16(nw, PF_RAT_SRC_PORT, addr->port[0]);
1107 	snl_add_msg_attr_u16(nw, PF_RAT_DST_PORT, addr->port[1]);
1108 	snl_add_msg_attr_u8(nw, PF_RAT_NEG, addr->neg);
1109 	snl_add_msg_attr_u8(nw, PF_RAT_OP, addr->port_op);
1110 
1111 	snl_end_attr_nested(nw, off);
1112 }
1113 
1114 static void
1115 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])
1116 {
1117 	int off, i = 0;
1118 
1119 	off = snl_add_msg_attr_nested(nw, type);
1120 
1121 	while (i < PF_RULE_MAX_LABEL_COUNT &&
1122 	    labels[i][0] != 0) {
1123 		snl_add_msg_attr_string(nw, PF_LT_LABEL, labels[i]);
1124 		i++;
1125 	}
1126 
1127 	snl_end_attr_nested(nw, off);
1128 }
1129 
1130 static void
1131 snl_add_msg_attr_mape(struct snl_writer *nw, uint32_t type, const struct pf_mape_portset *me)
1132 {
1133 	int off;
1134 
1135 	off = snl_add_msg_attr_nested(nw, type);
1136 
1137 	snl_add_msg_attr_u8(nw, PF_MET_OFFSET, me->offset);
1138 	snl_add_msg_attr_u8(nw, PF_MET_PSID_LEN, me->psidlen);
1139 	snl_add_msg_attr_u16(nw, PF_MET_PSID, me->psid);
1140 
1141 	snl_end_attr_nested(nw, off);
1142 }
1143 
1144 static void
1145 snl_add_msg_attr_rpool(struct snl_writer *nw, uint32_t type, const struct pfctl_pool *pool)
1146 {
1147 	int off;
1148 
1149 	off = snl_add_msg_attr_nested(nw, type);
1150 
1151 	snl_add_msg_attr(nw, PF_PT_KEY, sizeof(pool->key), &pool->key);
1152 	snl_add_msg_attr_ip6(nw, PF_PT_COUNTER, &pool->counter.v6);
1153 	snl_add_msg_attr_u32(nw, PF_PT_TBLIDX, pool->tblidx);
1154 	snl_add_msg_attr_u16(nw, PF_PT_PROXY_SRC_PORT, pool->proxy_port[0]);
1155 	snl_add_msg_attr_u16(nw, PF_PT_PROXY_DST_PORT, pool->proxy_port[1]);
1156 	snl_add_msg_attr_u8(nw, PF_PT_OPTS, pool->opts);
1157 	snl_add_msg_attr_mape(nw, PF_PT_MAPE, &pool->mape);
1158 
1159 	snl_end_attr_nested(nw, off);
1160 }
1161 
1162 static void
1163 snl_add_msg_attr_timeouts(struct snl_writer *nw, uint32_t type, const uint32_t *timeouts)
1164 {
1165 	int off;
1166 
1167 	off = snl_add_msg_attr_nested(nw, type);
1168 
1169 	for (int i = 0; i < PFTM_MAX; i++)
1170 		snl_add_msg_attr_u32(nw, PF_TT_TIMEOUT, timeouts[i]);
1171 
1172 	snl_end_attr_nested(nw, off);
1173 }
1174 
1175 static void
1176 snl_add_msg_attr_uid(struct snl_writer *nw, uint32_t type, const struct pf_rule_uid *uid)
1177 {
1178 	int off;
1179 
1180 	off = snl_add_msg_attr_nested(nw, type);
1181 
1182 	snl_add_msg_attr_u32(nw, PF_RUT_UID_LOW, uid->uid[0]);
1183 	snl_add_msg_attr_u32(nw, PF_RUT_UID_HIGH, uid->uid[1]);
1184 	snl_add_msg_attr_u8(nw, PF_RUT_OP, uid->op);
1185 
1186 	snl_end_attr_nested(nw, off);
1187 }
1188 
1189 static void
1190 snl_add_msg_attr_pf_rule(struct snl_writer *nw, uint32_t type, const struct pfctl_rule *r)
1191 {
1192 	int off;
1193 
1194 	off = snl_add_msg_attr_nested(nw, type);
1195 
1196 	snl_add_msg_attr_rule_addr(nw, PF_RT_SRC, &r->src);
1197 	snl_add_msg_attr_rule_addr(nw, PF_RT_DST, &r->dst);
1198 	snl_add_msg_attr_rule_labels(nw, PF_RT_LABELS, r->label);
1199 	snl_add_msg_attr_u32(nw, PF_RT_RIDENTIFIER, r->ridentifier);
1200 	snl_add_msg_attr_string(nw, PF_RT_IFNAME, r->ifname);
1201 	snl_add_msg_attr_string(nw, PF_RT_QNAME, r->qname);
1202 	snl_add_msg_attr_string(nw, PF_RT_PQNAME, r->pqname);
1203 	snl_add_msg_attr_string(nw, PF_RT_TAGNAME, r->tagname);
1204 	snl_add_msg_attr_string(nw, PF_RT_MATCH_TAGNAME, r->match_tagname);
1205 	snl_add_msg_attr_string(nw, PF_RT_OVERLOAD_TBLNAME, r->overload_tblname);
1206 	snl_add_msg_attr_rpool(nw, PF_RT_RPOOL, &r->rpool);
1207 	snl_add_msg_attr_u32(nw, PF_RT_OS_FINGERPRINT, r->os_fingerprint);
1208 	snl_add_msg_attr_u32(nw, PF_RT_RTABLEID, r->rtableid);
1209 	snl_add_msg_attr_timeouts(nw, PF_RT_TIMEOUT, r->timeout);
1210 	snl_add_msg_attr_u32(nw, PF_RT_MAX_STATES, r->max_states);
1211 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_NODES, r->max_src_nodes);
1212 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_STATES, r->max_src_states);
1213 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_LIMIT, r->max_src_conn_rate.limit);
1214 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_SECS, r->max_src_conn_rate.seconds);
1215 
1216 	snl_add_msg_attr_u16(nw, PF_RT_DNPIPE, r->dnpipe);
1217 	snl_add_msg_attr_u16(nw, PF_RT_DNRPIPE, r->dnrpipe);
1218 	snl_add_msg_attr_u32(nw, PF_RT_DNFLAGS, r->free_flags);
1219 
1220 	snl_add_msg_attr_u32(nw, PF_RT_NR, r->nr);
1221 	snl_add_msg_attr_u32(nw, PF_RT_PROB, r->prob);
1222 	snl_add_msg_attr_u32(nw, PF_RT_CUID, r->cuid);
1223 	snl_add_msg_attr_u32(nw, PF_RT_CPID, r->cpid);
1224 
1225 	snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP, r->return_icmp);
1226 	snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP6, r->return_icmp6);
1227 	snl_add_msg_attr_u16(nw, PF_RT_MAX_MSS, r->max_mss);
1228 	snl_add_msg_attr_u16(nw, PF_RT_SCRUB_FLAGS, r->scrub_flags);
1229 
1230 	snl_add_msg_attr_uid(nw, PF_RT_UID, &r->uid);
1231 	snl_add_msg_attr_uid(nw, PF_RT_GID, (const struct pf_rule_uid *)&r->gid);
1232 
1233 	snl_add_msg_attr_u32(nw, PF_RT_RULE_FLAG, r->rule_flag);
1234 	snl_add_msg_attr_u8(nw, PF_RT_ACTION, r->action);
1235 	snl_add_msg_attr_u8(nw, PF_RT_DIRECTION, r->direction);
1236 	snl_add_msg_attr_u8(nw, PF_RT_LOG, r->log);
1237 	snl_add_msg_attr_u8(nw, PF_RT_LOGIF, r->logif);
1238 	snl_add_msg_attr_u8(nw, PF_RT_QUICK, r->quick);
1239 	snl_add_msg_attr_u8(nw, PF_RT_IF_NOT, r->ifnot);
1240 	snl_add_msg_attr_u8(nw, PF_RT_MATCH_TAG_NOT, r->match_tag_not);
1241 	snl_add_msg_attr_u8(nw, PF_RT_NATPASS, r->natpass);
1242 	snl_add_msg_attr_u8(nw, PF_RT_KEEP_STATE, r->keep_state);
1243 	snl_add_msg_attr_u8(nw, PF_RT_AF, r->af);
1244 	snl_add_msg_attr_u8(nw, PF_RT_PROTO, r->proto);
1245 	snl_add_msg_attr_u8(nw, PF_RT_TYPE, r->type);
1246 	snl_add_msg_attr_u8(nw, PF_RT_CODE, r->code);
1247 	snl_add_msg_attr_u8(nw, PF_RT_FLAGS, r->flags);
1248 	snl_add_msg_attr_u8(nw, PF_RT_FLAGSET, r->flagset);
1249 	snl_add_msg_attr_u8(nw, PF_RT_MIN_TTL, r->min_ttl);
1250 	snl_add_msg_attr_u8(nw, PF_RT_ALLOW_OPTS, r->allow_opts);
1251 	snl_add_msg_attr_u8(nw, PF_RT_RT, r->rt);
1252 	snl_add_msg_attr_u8(nw, PF_RT_RETURN_TTL, r->return_ttl);
1253 	snl_add_msg_attr_u8(nw, PF_RT_TOS, r->tos);
1254 	snl_add_msg_attr_u8(nw, PF_RT_SET_TOS, r->set_tos);
1255 
1256 	snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_RELATIVE, r->anchor_relative);
1257 	snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_WILDCARD, r->anchor_wildcard);
1258 	snl_add_msg_attr_u8(nw, PF_RT_FLUSH, r->flush);
1259 	snl_add_msg_attr_u8(nw, PF_RT_PRIO, r->prio);
1260 	snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO, r->set_prio[0]);
1261 	snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO_REPLY, r->set_prio[1]);
1262 
1263 	snl_add_msg_attr_ip6(nw, PF_RT_DIVERT_ADDRESS, &r->divert.addr.v6);
1264 	snl_add_msg_attr_u16(nw, PF_RT_DIVERT_PORT, r->divert.port);
1265 
1266 	snl_end_attr_nested(nw, off);
1267 }
1268 
1269 int
1270 pfctl_add_rule(int dev __unused, const struct pfctl_rule *r, const char *anchor,
1271     const char *anchor_call, uint32_t ticket, uint32_t pool_ticket)
1272 {
1273 	struct pfctl_handle *h;
1274 	int ret;
1275 
1276 	h = pfctl_open(PF_DEVICE);
1277 	if (h == NULL)
1278 		return (ENODEV);
1279 
1280 	ret = pfctl_add_rule_h(h, r, anchor, anchor_call, ticket, pool_ticket);
1281 
1282 	pfctl_close(h);
1283 
1284 	return (ret);
1285 }
1286 
1287 int
1288 pfctl_add_rule_h(struct pfctl_handle *h, const struct pfctl_rule *r,
1289 	    const char *anchor, const char *anchor_call, uint32_t ticket,
1290 	    uint32_t pool_ticket)
1291 {
1292 	struct snl_writer nw;
1293 	struct snl_errmsg_data e = {};
1294 	struct nlmsghdr *hdr;
1295 	uint32_t seq_id;
1296 	int family_id;
1297 
1298 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
1299 	if (family_id == 0)
1300 		return (ENOTSUP);
1301 
1302 	snl_init_writer(&h->ss, &nw);
1303 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_ADDRULE);
1304 	hdr->nlmsg_flags |= NLM_F_DUMP;
1305 	snl_add_msg_attr_u32(&nw, PF_ART_TICKET, ticket);
1306 	snl_add_msg_attr_u32(&nw, PF_ART_POOL_TICKET, pool_ticket);
1307 	snl_add_msg_attr_string(&nw, PF_ART_ANCHOR, anchor);
1308 	snl_add_msg_attr_string(&nw, PF_ART_ANCHOR_CALL, anchor_call);
1309 
1310 	snl_add_msg_attr_pf_rule(&nw, PF_ART_RULE, r);
1311 
1312 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
1313 		return (ENXIO);
1314 
1315 	seq_id = hdr->nlmsg_seq;
1316 
1317 	if (! snl_send_message(&h->ss, hdr))
1318 		return (ENXIO);
1319 
1320 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
1321 	}
1322 
1323 	return (e.error);
1324 }
1325 
1326 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1327 #define	_OUT(_field)	offsetof(struct pfctl_rules_info, _field)
1328 static struct snl_attr_parser ap_getrules[] = {
1329 	{ .type = PF_GR_NR, .off = _OUT(nr), .cb = snl_attr_get_uint32 },
1330 	{ .type = PF_GR_TICKET, .off = _OUT(ticket), .cb = snl_attr_get_uint32 },
1331 };
1332 static struct snl_field_parser fp_getrules[] = {
1333 };
1334 #undef _IN
1335 #undef _OUT
1336 SNL_DECLARE_PARSER(getrules_parser, struct genlmsghdr, fp_getrules, ap_getrules);
1337 
1338 int
1339 pfctl_get_rules_info(int dev __unused, struct pfctl_rules_info *rules, uint32_t ruleset,
1340     const char *path)
1341 {
1342 	struct snl_state ss = {};
1343 	struct snl_errmsg_data e = {};
1344 	struct nlmsghdr *hdr;
1345 	struct snl_writer nw;
1346 	uint32_t seq_id;
1347 	int family_id;
1348 
1349 	snl_init(&ss, NETLINK_GENERIC);
1350 	family_id = snl_get_genl_family(&ss, PFNL_FAMILY_NAME);
1351 	if (family_id == 0)
1352 		return (ENOTSUP);
1353 
1354 	snl_init_writer(&ss, &nw);
1355 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETRULES);
1356 	hdr->nlmsg_flags |= NLM_F_DUMP;
1357 
1358 	snl_add_msg_attr_string(&nw, PF_GR_ANCHOR, path);
1359 	snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset);
1360 
1361 	hdr = snl_finalize_msg(&nw);
1362 	if (hdr == NULL)
1363 		return (ENOMEM);
1364 
1365 	seq_id = hdr->nlmsg_seq;
1366 	if (! snl_send_message(&ss, hdr))
1367 		return (ENXIO);
1368 
1369 	while ((hdr = snl_read_reply_multi(&ss, seq_id, &e)) != NULL) {
1370 		if (! snl_parse_nlmsg(&ss, hdr, &getrules_parser, rules))
1371 			continue;
1372 	}
1373 
1374 	return (e.error);
1375 }
1376 
1377 int
1378 pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket, const char *anchor,
1379     uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
1380 {
1381 	return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule,
1382 	    anchor_call, false));
1383 }
1384 
1385 #define _OUT(_field)	offsetof(struct pf_addr_wrap, _field)
1386 static const struct snl_attr_parser ap_addr_wrap[] = {
1387 	{ .type = PF_AT_ADDR, .off = _OUT(v.a.addr), .cb = snl_attr_get_in6_addr },
1388 	{ .type = PF_AT_MASK, .off = _OUT(v.a.mask), .cb = snl_attr_get_in6_addr },
1389 	{ .type = PF_AT_IFNAME, .off = _OUT(v.ifname), .arg = (void *)IFNAMSIZ,.cb = snl_attr_copy_string },
1390 	{ .type = PF_AT_TABLENAME, .off = _OUT(v.tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = snl_attr_copy_string },
1391 	{ .type = PF_AT_TYPE, .off = _OUT(type), .cb = snl_attr_get_uint8 },
1392 	{ .type = PF_AT_IFLAGS, .off = _OUT(iflags), .cb = snl_attr_get_uint8 },
1393 	{ .type = PF_AT_TBLCNT, .off = _OUT(p.tblcnt), .cb = snl_attr_get_uint32 },
1394 	{ .type = PF_AT_DYNCNT, .off = _OUT(p.dyncnt), .cb = snl_attr_get_uint32 },
1395 };
1396 SNL_DECLARE_ATTR_PARSER(addr_wrap_parser, ap_addr_wrap);
1397 #undef _OUT
1398 
1399 #define _OUT(_field)	offsetof(struct pf_rule_addr, _field)
1400 static struct snl_attr_parser ap_rule_addr[] = {
1401 	{ .type = PF_RAT_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = snl_attr_get_nested },
1402 	{ .type = PF_RAT_SRC_PORT, .off = _OUT(port[0]), .cb = snl_attr_get_uint16 },
1403 	{ .type = PF_RAT_DST_PORT, .off = _OUT(port[1]), .cb = snl_attr_get_uint16 },
1404 	{ .type = PF_RAT_NEG, .off = _OUT(neg), .cb = snl_attr_get_uint8 },
1405 	{ .type = PF_RAT_OP, .off = _OUT(port_op), .cb = snl_attr_get_uint8 },
1406 };
1407 #undef _OUT
1408 SNL_DECLARE_ATTR_PARSER(rule_addr_parser, ap_rule_addr);
1409 
1410 struct snl_parsed_labels
1411 {
1412 	char		labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
1413 	uint32_t	i;
1414 };
1415 
1416 static bool
1417 snl_attr_get_pf_rule_labels(struct snl_state *ss, struct nlattr *nla,
1418     const void *arg __unused, void *target)
1419 {
1420 	struct snl_parsed_labels *l = (struct snl_parsed_labels *)target;
1421 	bool ret;
1422 
1423 	if (l->i >= PF_RULE_MAX_LABEL_COUNT)
1424 		return (E2BIG);
1425 
1426 	ret = snl_attr_copy_string(ss, nla, (void *)PF_RULE_LABEL_SIZE,
1427 	    l->labels[l->i]);
1428 	if (ret)
1429 		l->i++;
1430 
1431 	return (ret);
1432 }
1433 
1434 #define _OUT(_field)	offsetof(struct nl_parsed_labels, _field)
1435 static const struct snl_attr_parser ap_labels[] = {
1436 	{ .type = PF_LT_LABEL, .off = 0, .cb = snl_attr_get_pf_rule_labels },
1437 };
1438 SNL_DECLARE_ATTR_PARSER(rule_labels_parser, ap_labels);
1439 #undef _OUT
1440 
1441 static bool
1442 snl_attr_get_nested_pf_rule_labels(struct snl_state *ss, struct nlattr *nla,
1443     const void *arg __unused, void *target)
1444 {
1445 	struct snl_parsed_labels parsed_labels = { };
1446 	bool error;
1447 
1448 	/* Assumes target points to the beginning of the structure */
1449 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &rule_labels_parser, &parsed_labels);
1450 	if (! error)
1451 		return (error);
1452 
1453 	memcpy(target, parsed_labels.labels, sizeof(parsed_labels.labels));
1454 
1455 	return (true);
1456 }
1457 
1458 #define _OUT(_field)	offsetof(struct pf_mape_portset, _field)
1459 static const struct snl_attr_parser ap_mape_portset[] = {
1460 	{ .type = PF_MET_OFFSET, .off = _OUT(offset), .cb = snl_attr_get_uint8 },
1461 	{ .type = PF_MET_PSID_LEN, .off = _OUT(psidlen), .cb = snl_attr_get_uint8 },
1462 	{. type = PF_MET_PSID, .off = _OUT(psid), .cb = snl_attr_get_uint16 },
1463 };
1464 SNL_DECLARE_ATTR_PARSER(mape_portset_parser, ap_mape_portset);
1465 #undef _OUT
1466 
1467 #define _OUT(_field)	offsetof(struct pfctl_pool, _field)
1468 static const struct snl_attr_parser ap_pool[] = {
1469 	{ .type = PF_PT_KEY, .off = _OUT(key), .arg = (void *)sizeof(struct pf_poolhashkey), .cb = snl_attr_get_bytes },
1470 	{ .type = PF_PT_COUNTER, .off = _OUT(counter), .cb = snl_attr_get_in6_addr },
1471 	{ .type = PF_PT_TBLIDX, .off = _OUT(tblidx), .cb = snl_attr_get_uint32 },
1472 	{ .type = PF_PT_PROXY_SRC_PORT, .off = _OUT(proxy_port[0]), .cb = snl_attr_get_uint16 },
1473 	{ .type = PF_PT_PROXY_DST_PORT, .off = _OUT(proxy_port[1]), .cb = snl_attr_get_uint16 },
1474 	{ .type = PF_PT_OPTS, .off = _OUT(opts), .cb = snl_attr_get_uint8 },
1475 	{ .type = PF_PT_MAPE, .off = _OUT(mape), .arg = &mape_portset_parser, .cb = snl_attr_get_nested },
1476 };
1477 SNL_DECLARE_ATTR_PARSER(pool_parser, ap_pool);
1478 #undef _OUT
1479 
1480 struct nl_parsed_timeouts
1481 {
1482 	uint32_t	timeouts[PFTM_MAX];
1483 	uint32_t	i;
1484 };
1485 
1486 static bool
1487 snl_attr_get_pf_timeout(struct snl_state *ss, struct nlattr *nla,
1488     const void *arg __unused, void *target)
1489 {
1490 	struct nl_parsed_timeouts *t = (struct nl_parsed_timeouts *)target;
1491 	bool ret;
1492 
1493 	if (t->i >= PFTM_MAX)
1494 		return (E2BIG);
1495 
1496 	ret = snl_attr_get_uint32(ss, nla, NULL, &t->timeouts[t->i]);
1497 	if (ret)
1498 		t->i++;
1499 
1500 	return (ret);
1501 }
1502 
1503 #define _OUT(_field)	offsetof(struct nl_parsed_timeout, _field)
1504 static const struct snl_attr_parser ap_timeouts[] = {
1505 	{ .type = PF_TT_TIMEOUT, .off = 0, .cb = snl_attr_get_pf_timeout },
1506 };
1507 SNL_DECLARE_ATTR_PARSER(timeout_parser, ap_timeouts);
1508 #undef _OUT
1509 
1510 static bool
1511 snl_attr_get_nested_timeouts(struct snl_state *ss, struct nlattr *nla,
1512     const void *arg __unused, void *target)
1513 {
1514 	struct nl_parsed_timeouts parsed_timeouts = { };
1515 	bool error;
1516 
1517 	/* Assumes target points to the beginning of the structure */
1518 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &timeout_parser, &parsed_timeouts);
1519 	if (! error)
1520 		return (error);
1521 
1522 	memcpy(target, parsed_timeouts.timeouts, sizeof(parsed_timeouts.timeouts));
1523 
1524 	return (true);
1525 }
1526 
1527 #define _OUT(_field)	offsetof(struct pf_rule_uid, _field)
1528 static const struct snl_attr_parser ap_rule_uid[] = {
1529 	{ .type = PF_RUT_UID_LOW, .off = _OUT(uid[0]), .cb = snl_attr_get_uint32 },
1530 	{ .type = PF_RUT_UID_HIGH, .off = _OUT(uid[1]), .cb = snl_attr_get_uint32 },
1531 	{ .type = PF_RUT_OP, .off = _OUT(op), .cb = snl_attr_get_uint8 },
1532 };
1533 SNL_DECLARE_ATTR_PARSER(rule_uid_parser, ap_rule_uid);
1534 #undef _OUT
1535 
1536 struct pfctl_nl_get_rule {
1537 	struct pfctl_rule r;
1538 	char anchor_call[MAXPATHLEN];
1539 };
1540 #define	_OUT(_field)	offsetof(struct pfctl_nl_get_rule, _field)
1541 static struct snl_attr_parser ap_getrule[] = {
1542 	{ .type = PF_RT_SRC, .off = _OUT(r.src), .arg = &rule_addr_parser,.cb = snl_attr_get_nested },
1543 	{ .type = PF_RT_DST, .off = _OUT(r.dst), .arg = &rule_addr_parser,.cb = snl_attr_get_nested },
1544 	{ .type = PF_RT_RIDENTIFIER, .off = _OUT(r.ridentifier), .cb = snl_attr_get_uint32 },
1545 	{ .type = PF_RT_LABELS, .off = _OUT(r.label), .arg = &rule_labels_parser,.cb = snl_attr_get_nested_pf_rule_labels },
1546 	{ .type = PF_RT_IFNAME, .off = _OUT(r.ifname), .arg = (void *)IFNAMSIZ, .cb = snl_attr_copy_string },
1547 	{ .type = PF_RT_QNAME, .off = _OUT(r.qname), .arg = (void *)PF_QNAME_SIZE, .cb = snl_attr_copy_string },
1548 	{ .type = PF_RT_PQNAME, .off = _OUT(r.pqname), .arg = (void *)PF_QNAME_SIZE, .cb = snl_attr_copy_string },
1549 	{ .type = PF_RT_TAGNAME, .off = _OUT(r.tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = snl_attr_copy_string },
1550 	{ .type = PF_RT_MATCH_TAGNAME, .off = _OUT(r.match_tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = snl_attr_copy_string },
1551 	{ .type = PF_RT_OVERLOAD_TBLNAME, .off = _OUT(r.overload_tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = snl_attr_copy_string },
1552 	{ .type = PF_RT_RPOOL, .off = _OUT(r.rpool), .arg = &pool_parser, .cb = snl_attr_get_nested },
1553 	{ .type = PF_RT_OS_FINGERPRINT, .off = _OUT(r.os_fingerprint), .cb = snl_attr_get_uint32 },
1554 	{ .type = PF_RT_RTABLEID, .off = _OUT(r.rtableid), .cb = snl_attr_get_uint32 },
1555 	{ .type = PF_RT_TIMEOUT, .off = _OUT(r.timeout), .arg = &timeout_parser, .cb = snl_attr_get_nested_timeouts },
1556 	{ .type = PF_RT_MAX_STATES, .off = _OUT(r.max_states), .cb = snl_attr_get_uint32 },
1557 	{ .type = PF_RT_MAX_SRC_NODES, .off = _OUT(r.max_src_nodes), .cb = snl_attr_get_uint32 },
1558 	{ .type = PF_RT_MAX_SRC_STATES, .off = _OUT(r.max_src_states), .cb = snl_attr_get_uint32 },
1559 	{ .type = PF_RT_MAX_SRC_CONN_RATE_LIMIT, .off = _OUT(r.max_src_conn_rate.limit), .cb = snl_attr_get_uint32 },
1560 	{ .type = PF_RT_MAX_SRC_CONN_RATE_SECS, .off = _OUT(r.max_src_conn_rate.seconds), .cb = snl_attr_get_uint32 },
1561 	{ .type = PF_RT_DNPIPE, .off = _OUT(r.dnpipe), .cb = snl_attr_get_uint16 },
1562 	{ .type = PF_RT_DNRPIPE, .off = _OUT(r.dnrpipe), .cb = snl_attr_get_uint16 },
1563 	{ .type = PF_RT_DNFLAGS, .off = _OUT(r.free_flags), .cb = snl_attr_get_uint32 },
1564 	{ .type = PF_RT_NR, .off = _OUT(r.nr), .cb = snl_attr_get_uint32 },
1565 	{ .type = PF_RT_PROB, .off = _OUT(r.prob), .cb = snl_attr_get_uint32 },
1566 	{ .type = PF_RT_CUID, .off = _OUT(r.cuid), .cb = snl_attr_get_uint32 },
1567 	{. type = PF_RT_CPID, .off = _OUT(r.cpid), .cb = snl_attr_get_uint32 },
1568 	{ .type = PF_RT_RETURN_ICMP, .off = _OUT(r.return_icmp), .cb = snl_attr_get_uint16 },
1569 	{ .type = PF_RT_RETURN_ICMP6, .off = _OUT(r.return_icmp6), .cb = snl_attr_get_uint16 },
1570 	{ .type = PF_RT_MAX_MSS, .off = _OUT(r.max_mss), .cb = snl_attr_get_uint16 },
1571 	{ .type = PF_RT_SCRUB_FLAGS, .off = _OUT(r.scrub_flags), .cb = snl_attr_get_uint16 },
1572 	{ .type = PF_RT_UID, .off = _OUT(r.uid), .arg = &rule_uid_parser, .cb = snl_attr_get_nested },
1573 	{ .type = PF_RT_GID, .off = _OUT(r.gid), .arg = &rule_uid_parser, .cb = snl_attr_get_nested },
1574 	{ .type = PF_RT_RULE_FLAG, .off = _OUT(r.rule_flag), .cb = snl_attr_get_uint32 },
1575 	{ .type = PF_RT_ACTION, .off = _OUT(r.action), .cb = snl_attr_get_uint8 },
1576 	{ .type = PF_RT_DIRECTION, .off = _OUT(r.direction), .cb = snl_attr_get_uint8 },
1577 	{ .type = PF_RT_LOG, .off = _OUT(r.log), .cb = snl_attr_get_uint8 },
1578 	{ .type = PF_RT_LOGIF, .off = _OUT(r.logif), .cb = snl_attr_get_uint8 },
1579 	{ .type = PF_RT_QUICK, .off = _OUT(r.quick), .cb = snl_attr_get_uint8 },
1580 	{ .type = PF_RT_IF_NOT, .off = _OUT(r.ifnot), .cb = snl_attr_get_uint8 },
1581 	{ .type = PF_RT_MATCH_TAG_NOT, .off = _OUT(r.match_tag_not), .cb = snl_attr_get_uint8 },
1582 	{ .type = PF_RT_NATPASS, .off = _OUT(r.natpass), .cb = snl_attr_get_uint8 },
1583 	{ .type = PF_RT_KEEP_STATE, .off = _OUT(r.keep_state), .cb = snl_attr_get_uint8 },
1584 	{ .type = PF_RT_AF, .off = _OUT(r.af), .cb = snl_attr_get_uint8 },
1585 	{ .type = PF_RT_PROTO, .off = _OUT(r.proto), .cb = snl_attr_get_uint8 },
1586 	{ .type = PF_RT_TYPE, .off = _OUT(r.type), .cb = snl_attr_get_uint8 },
1587 	{ .type = PF_RT_CODE, .off = _OUT(r.code), .cb = snl_attr_get_uint8 },
1588 	{ .type = PF_RT_FLAGS, .off = _OUT(r.flags), .cb = snl_attr_get_uint8 },
1589 	{ .type = PF_RT_FLAGSET, .off = _OUT(r.flagset), .cb = snl_attr_get_uint8 },
1590 	{ .type = PF_RT_MIN_TTL, .off = _OUT(r.min_ttl), .cb = snl_attr_get_uint8 },
1591 	{ .type = PF_RT_ALLOW_OPTS, .off = _OUT(r.allow_opts), .cb = snl_attr_get_uint8 },
1592 	{ .type = PF_RT_RT, .off = _OUT(r.rt), .cb = snl_attr_get_uint8 },
1593 	{ .type = PF_RT_RETURN_TTL, .off = _OUT(r.return_ttl), .cb = snl_attr_get_uint8 },
1594 	{ .type = PF_RT_TOS, .off = _OUT(r.tos), .cb = snl_attr_get_uint8 },
1595 	{ .type = PF_RT_SET_TOS, .off = _OUT(r.set_tos), .cb = snl_attr_get_uint8 },
1596 	{ .type = PF_RT_ANCHOR_RELATIVE, .off = _OUT(r.anchor_relative), .cb = snl_attr_get_uint8 },
1597 	{ .type = PF_RT_ANCHOR_WILDCARD, .off = _OUT(r.anchor_wildcard), .cb = snl_attr_get_uint8 },
1598 	{ .type = PF_RT_FLUSH, .off = _OUT(r.flush), .cb = snl_attr_get_uint8 },
1599 	{ .type = PF_RT_PRIO, .off = _OUT(r.prio), .cb = snl_attr_get_uint8 },
1600 	{ .type = PF_RT_SET_PRIO, .off = _OUT(r.set_prio[0]), .cb = snl_attr_get_uint8 },
1601 	{ .type = PF_RT_SET_PRIO_REPLY, .off = _OUT(r.set_prio[1]), .cb = snl_attr_get_uint8 },
1602 	{ .type = PF_RT_DIVERT_ADDRESS, .off = _OUT(r.divert.addr), .cb = snl_attr_get_in6_addr },
1603 	{ .type = PF_RT_DIVERT_PORT, .off = _OUT(r.divert.port), .cb = snl_attr_get_uint16 },
1604 	{ .type = PF_RT_PACKETS_IN, .off = _OUT(r.packets[0]), .cb = snl_attr_get_uint64 },
1605 	{ .type = PF_RT_PACKETS_OUT, .off = _OUT(r.packets[1]), .cb = snl_attr_get_uint64 },
1606 	{ .type = PF_RT_BYTES_IN, .off = _OUT(r.bytes[0]), .cb = snl_attr_get_uint64 },
1607 	{ .type = PF_RT_BYTES_OUT, .off = _OUT(r.bytes[1]), .cb = snl_attr_get_uint64 },
1608 	{ .type = PF_RT_EVALUATIONS, .off = _OUT(r.evaluations), .cb = snl_attr_get_uint64 },
1609 	{ .type = PF_RT_TIMESTAMP, .off = _OUT(r.last_active_timestamp), .cb = snl_attr_get_uint64 },
1610 	{ .type = PF_RT_STATES_CUR, .off = _OUT(r.states_cur), .cb = snl_attr_get_uint64 },
1611 	{ .type = PF_RT_STATES_TOTAL, .off = _OUT(r.states_tot), .cb = snl_attr_get_uint64 },
1612 	{ .type = PF_RT_SRC_NODES, .off = _OUT(r.src_nodes), .cb = snl_attr_get_uint64 },
1613 	{ .type = PF_RT_ANCHOR_CALL, .off = _OUT(anchor_call), .arg = (void*)MAXPATHLEN, .cb = snl_attr_copy_string },
1614 };
1615 static struct snl_field_parser fp_getrule[] = {};
1616 #undef _OUT
1617 SNL_DECLARE_PARSER(getrule_parser, struct genlmsghdr, fp_getrule, ap_getrule);
1618 
1619 int
1620 pfctl_get_clear_rule_h(struct pfctl_handle *h, uint32_t nr, uint32_t ticket,
1621     const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
1622     char *anchor_call, bool clear)
1623 {
1624 	struct pfctl_nl_get_rule attrs = {};
1625 	struct snl_errmsg_data e = {};
1626 	struct nlmsghdr *hdr;
1627 	struct snl_writer nw;
1628 	uint32_t seq_id;
1629 	int family_id;
1630 
1631 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
1632 	if (family_id == 0)
1633 		return (ENOTSUP);
1634 
1635 	snl_init_writer(&h->ss, &nw);
1636 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETRULE);
1637 	hdr->nlmsg_flags |= NLM_F_DUMP;
1638 
1639 	snl_add_msg_attr_string(&nw, PF_GR_ANCHOR, anchor);
1640 	snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset);
1641 	snl_add_msg_attr_u32(&nw, PF_GR_NR, nr);
1642 	snl_add_msg_attr_u32(&nw, PF_GR_TICKET, ticket);
1643 	snl_add_msg_attr_u8(&nw, PF_GR_CLEAR, clear);
1644 
1645 	hdr = snl_finalize_msg(&nw);
1646 	if (hdr == NULL)
1647 		return (ENOMEM);
1648 
1649 	seq_id = hdr->nlmsg_seq;
1650 	if (! snl_send_message(&h->ss, hdr))
1651 		return (ENXIO);
1652 
1653 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
1654 		if (! snl_parse_nlmsg(&h->ss, hdr, &getrule_parser, &attrs))
1655 			continue;
1656 	}
1657 
1658 	memcpy(rule, &attrs.r, sizeof(attrs.r));
1659 	strlcpy(anchor_call, attrs.anchor_call, MAXPATHLEN);
1660 
1661 	return (e.error);
1662 }
1663 
1664 int
1665 pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket,
1666 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
1667 	    char *anchor_call, bool clear)
1668 {
1669 	nvlist_t *nvl;
1670 	int ret;
1671 
1672 	nvl = nvlist_create(0);
1673 	if (nvl == 0)
1674 		return (ENOMEM);
1675 
1676 	nvlist_add_number(nvl, "nr", nr);
1677 	nvlist_add_number(nvl, "ticket", ticket);
1678 	nvlist_add_string(nvl, "anchor", anchor);
1679 	nvlist_add_number(nvl, "ruleset", ruleset);
1680 
1681 	if (clear)
1682 		nvlist_add_bool(nvl, "clear_counter", true);
1683 
1684 	if ((ret = pfctl_do_ioctl(dev, DIOCGETRULENV, 8192, &nvl)) != 0)
1685 		goto out;
1686 
1687 	pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule);
1688 
1689 	if (anchor_call)
1690 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
1691 		    MAXPATHLEN);
1692 
1693 out:
1694 	nvlist_destroy(nvl);
1695 	return (ret);
1696 }
1697 
1698 int
1699 pfctl_set_keepcounters(int dev, bool keep)
1700 {
1701 	struct pfioc_nv	 nv;
1702 	nvlist_t	*nvl;
1703 	int		 ret;
1704 
1705 	nvl = nvlist_create(0);
1706 
1707 	nvlist_add_bool(nvl, "keep_counters", keep);
1708 
1709 	nv.data = nvlist_pack(nvl, &nv.len);
1710 	nv.size = nv.len;
1711 
1712 	nvlist_destroy(nvl);
1713 
1714 	ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv);
1715 
1716 	free(nv.data);
1717 	return (ret);
1718 }
1719 
1720 struct pfctl_creator {
1721 	uint32_t id;
1722 };
1723 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1724 #define	_OUT(_field)	offsetof(struct pfctl_creator, _field)
1725 static struct snl_attr_parser ap_creators[] = {
1726 	{ .type = PF_ST_CREATORID, .off = _OUT(id), .cb = snl_attr_get_uint32 },
1727 };
1728 static struct snl_field_parser fp_creators[] = {
1729 };
1730 #undef _IN
1731 #undef _OUT
1732 SNL_DECLARE_PARSER(creator_parser, struct genlmsghdr, fp_creators, ap_creators);
1733 
1734 static int
1735 pfctl_get_creators_nl(struct snl_state *ss, uint32_t *creators, size_t *len)
1736 {
1737 
1738 	int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME);
1739 	size_t i = 0;
1740 
1741 	struct nlmsghdr *hdr;
1742 	struct snl_writer nw;
1743 
1744 	if (family_id == 0)
1745 		return (ENOTSUP);
1746 
1747 	snl_init_writer(ss, &nw);
1748 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETCREATORS);
1749 	hdr->nlmsg_flags |= NLM_F_DUMP;
1750 	hdr = snl_finalize_msg(&nw);
1751 	if (hdr == NULL)
1752 		return (ENOMEM);
1753 	uint32_t seq_id = hdr->nlmsg_seq;
1754 
1755 	snl_send_message(ss, hdr);
1756 
1757 	struct snl_errmsg_data e = {};
1758 	while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) {
1759 		struct pfctl_creator c;
1760 		bzero(&c, sizeof(c));
1761 
1762 		if (!snl_parse_nlmsg(ss, hdr, &creator_parser, &c))
1763 			continue;
1764 
1765 		creators[i] = c.id;
1766 		i++;
1767 		if (i > *len)
1768 			return (E2BIG);
1769 	}
1770 
1771 	*len = i;
1772 
1773 	return (0);
1774 }
1775 
1776 int
1777 pfctl_get_creatorids(struct pfctl_handle *h, uint32_t *creators, size_t *len)
1778 {
1779 	int error;
1780 
1781 	error = pfctl_get_creators_nl(&h->ss, creators, len);
1782 
1783 	return (error);
1784 }
1785 
1786 static inline bool
1787 snl_attr_get_pfaddr(struct snl_state *ss __unused, struct nlattr *nla,
1788     const void *arg __unused, void *target)
1789 {
1790 	memcpy(target, NLA_DATA(nla), NLA_DATA_LEN(nla));
1791 	return (true);
1792 }
1793 
1794 static inline bool
1795 snl_attr_store_ifname(struct snl_state *ss __unused, struct nlattr *nla,
1796     const void *arg __unused, void *target)
1797 {
1798 	size_t maxlen = NLA_DATA_LEN(nla);
1799 
1800 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
1801 		strlcpy(target, (char *)NLA_DATA(nla), maxlen);
1802 		return (true);
1803 	}
1804 	return (false);
1805 }
1806 
1807 #define	_OUT(_field)	offsetof(struct pfctl_state_peer, _field)
1808 static const struct snl_attr_parser nla_p_speer[] = {
1809 	{ .type = PF_STP_SEQLO, .off = _OUT(seqlo), .cb = snl_attr_get_uint32 },
1810 	{ .type = PF_STP_SEQHI, .off = _OUT(seqhi), .cb = snl_attr_get_uint32 },
1811 	{ .type = PF_STP_SEQDIFF, .off = _OUT(seqdiff), .cb = snl_attr_get_uint32 },
1812 	{ .type = PF_STP_STATE, .off = _OUT(state), .cb = snl_attr_get_uint8 },
1813 	{ .type = PF_STP_WSCALE, .off = _OUT(wscale), .cb = snl_attr_get_uint8 },
1814 };
1815 SNL_DECLARE_ATTR_PARSER(speer_parser, nla_p_speer);
1816 #undef _OUT
1817 
1818 #define	_OUT(_field)	offsetof(struct pf_state_key_export, _field)
1819 static const struct snl_attr_parser nla_p_skey[] = {
1820 	{ .type = PF_STK_ADDR0, .off = _OUT(addr[0]), .cb = snl_attr_get_pfaddr },
1821 	{ .type = PF_STK_ADDR1, .off = _OUT(addr[1]), .cb = snl_attr_get_pfaddr },
1822 	{ .type = PF_STK_PORT0, .off = _OUT(port[0]), .cb = snl_attr_get_uint16 },
1823 	{ .type = PF_STK_PORT1, .off = _OUT(port[1]), .cb = snl_attr_get_uint16 },
1824 };
1825 SNL_DECLARE_ATTR_PARSER(skey_parser, nla_p_skey);
1826 #undef _OUT
1827 
1828 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1829 #define	_OUT(_field)	offsetof(struct pfctl_state, _field)
1830 static struct snl_attr_parser ap_state[] = {
1831 	{ .type = PF_ST_ID, .off = _OUT(id), .cb = snl_attr_get_uint64 },
1832 	{ .type = PF_ST_CREATORID, .off = _OUT(creatorid), .cb = snl_attr_get_uint32 },
1833 	{ .type = PF_ST_IFNAME, .off = _OUT(ifname), .cb = snl_attr_store_ifname },
1834 	{ .type = PF_ST_ORIG_IFNAME, .off = _OUT(orig_ifname), .cb = snl_attr_store_ifname },
1835 	{ .type = PF_ST_KEY_WIRE, .off = _OUT(key[0]), .arg = &skey_parser, .cb = snl_attr_get_nested },
1836 	{ .type = PF_ST_KEY_STACK, .off = _OUT(key[1]), .arg = &skey_parser, .cb = snl_attr_get_nested },
1837 	{ .type = PF_ST_PEER_SRC, .off = _OUT(src), .arg = &speer_parser, .cb = snl_attr_get_nested },
1838 	{ .type = PF_ST_PEER_DST, .off = _OUT(dst), .arg = &speer_parser, .cb = snl_attr_get_nested },
1839 	{ .type = PF_ST_RT_ADDR, .off = _OUT(rt_addr), .cb = snl_attr_get_pfaddr },
1840 	{ .type = PF_ST_RULE, .off = _OUT(rule), .cb = snl_attr_get_uint32 },
1841 	{ .type = PF_ST_ANCHOR, .off = _OUT(anchor), .cb = snl_attr_get_uint32 },
1842 	{ .type = PF_ST_NAT_RULE, .off = _OUT(nat_rule), .cb = snl_attr_get_uint32 },
1843 	{ .type = PF_ST_CREATION, .off = _OUT(creation), .cb = snl_attr_get_uint32 },
1844 	{ .type = PF_ST_EXPIRE, .off = _OUT(expire), .cb = snl_attr_get_uint32 },
1845 	{ .type = PF_ST_PACKETS0, .off = _OUT(packets[0]), .cb = snl_attr_get_uint64 },
1846 	{ .type = PF_ST_PACKETS1, .off = _OUT(packets[1]), .cb = snl_attr_get_uint64 },
1847 	{ .type = PF_ST_BYTES0, .off = _OUT(bytes[0]), .cb = snl_attr_get_uint64 },
1848 	{ .type = PF_ST_BYTES1, .off = _OUT(bytes[1]), .cb = snl_attr_get_uint64 },
1849 	{ .type = PF_ST_AF, .off = _OUT(key[0].af), .cb = snl_attr_get_uint8 },
1850 	{ .type = PF_ST_PROTO, .off = _OUT(key[0].proto), .cb = snl_attr_get_uint8 },
1851 	{ .type = PF_ST_DIRECTION, .off = _OUT(direction), .cb = snl_attr_get_uint8 },
1852 	{ .type = PF_ST_LOG, .off = _OUT(log), .cb = snl_attr_get_uint8 },
1853 	{ .type = PF_ST_STATE_FLAGS, .off = _OUT(state_flags), .cb = snl_attr_get_uint16 },
1854 	{ .type = PF_ST_SYNC_FLAGS, .off = _OUT(sync_flags), .cb = snl_attr_get_uint8 },
1855 	{ .type = PF_ST_RTABLEID, .off = _OUT(rtableid), .cb = snl_attr_get_int32 },
1856 	{ .type = PF_ST_MIN_TTL, .off = _OUT(min_ttl), .cb = snl_attr_get_uint8 },
1857 	{ .type = PF_ST_MAX_MSS, .off = _OUT(max_mss), .cb = snl_attr_get_uint16 },
1858 	{ .type = PF_ST_DNPIPE, .off = _OUT(dnpipe), .cb = snl_attr_get_uint16 },
1859 	{ .type = PF_ST_DNRPIPE, .off = _OUT(dnrpipe), .cb = snl_attr_get_uint16 },
1860 	{ .type = PF_ST_RT, .off = _OUT(rt), .cb = snl_attr_get_uint8 },
1861 	{ .type = PF_ST_RT_IFNAME, .off = _OUT(rt_ifname), .cb = snl_attr_store_ifname },
1862 };
1863 static struct snl_field_parser fp_state[] = {
1864 };
1865 #undef _IN
1866 #undef _OUT
1867 SNL_DECLARE_PARSER(state_parser, struct genlmsghdr, fp_state, ap_state);
1868 
1869 static const struct snl_hdr_parser *all_parsers[] = {
1870 	&state_parser, &skey_parser, &speer_parser,
1871 	&creator_parser, &getrules_parser
1872 };
1873 
1874 static int
1875 pfctl_get_states_nl(struct pfctl_state_filter *filter, struct snl_state *ss, pfctl_get_state_fn f, void *arg)
1876 {
1877 	SNL_VERIFY_PARSERS(all_parsers);
1878 	int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME);
1879 	int ret;
1880 
1881 	struct nlmsghdr *hdr;
1882 	struct snl_writer nw;
1883 
1884 	if (family_id == 0)
1885 		return (ENOTSUP);
1886 
1887 	snl_init_writer(ss, &nw);
1888 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETSTATES);
1889 	hdr->nlmsg_flags |= NLM_F_DUMP;
1890 	snl_add_msg_attr_string(&nw, PF_ST_IFNAME, filter->ifname);
1891 	snl_add_msg_attr_u16(&nw, PF_ST_PROTO, filter->proto);
1892 	snl_add_msg_attr_u8(&nw, PF_ST_AF, filter->af);
1893 	snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_ADDR, &filter->addr.v6);
1894 	snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_MASK, &filter->mask.v6);
1895 
1896 	hdr = snl_finalize_msg(&nw);
1897 	if (hdr == NULL)
1898 		return (ENOMEM);
1899 
1900 	uint32_t seq_id = hdr->nlmsg_seq;
1901 
1902 	snl_send_message(ss, hdr);
1903 
1904 	struct snl_errmsg_data e = {};
1905 	while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) {
1906 		struct pfctl_state s;
1907 		bzero(&s, sizeof(s));
1908 		if (!snl_parse_nlmsg(ss, hdr, &state_parser, &s))
1909 			continue;
1910 
1911 		s.key[1].af = s.key[0].af;
1912 		s.key[1].proto = s.key[0].proto;
1913 
1914 		ret = f(&s, arg);
1915 		if (ret != 0)
1916 			return (ret);
1917 	}
1918 
1919 	return (0);
1920 }
1921 
1922 int
1923 pfctl_get_states_iter(pfctl_get_state_fn f, void *arg)
1924 {
1925 	struct pfctl_state_filter filter = {};
1926 	return (pfctl_get_filtered_states_iter(&filter, f, arg));
1927 }
1928 
1929 int
1930 pfctl_get_filtered_states_iter(struct pfctl_state_filter *filter, pfctl_get_state_fn f, void *arg)
1931 {
1932 	struct snl_state ss = {};
1933 	int error;
1934 
1935 	snl_init(&ss, NETLINK_GENERIC);
1936 	error = pfctl_get_states_nl(filter, &ss, f, arg);
1937 	snl_free(&ss);
1938 
1939 	return (error);
1940 }
1941 
1942 static int
1943 pfctl_append_states(struct pfctl_state *s, void *arg)
1944 {
1945 	struct pfctl_state *new;
1946 	struct pfctl_states *states = (struct pfctl_states *)arg;
1947 
1948 	new = malloc(sizeof(*s));
1949 	if (new == NULL)
1950 		return (ENOMEM);
1951 
1952 	memcpy(new, s, sizeof(*s));
1953 
1954 	TAILQ_INSERT_TAIL(&states->states, new, entry);
1955 
1956 	return (0);
1957 }
1958 
1959 int
1960 pfctl_get_states(int dev __unused, struct pfctl_states *states)
1961 {
1962 	int ret;
1963 
1964 	bzero(states, sizeof(*states));
1965 	TAILQ_INIT(&states->states);
1966 
1967 	ret = pfctl_get_states_iter(pfctl_append_states, states);
1968 	if (ret != 0) {
1969 		pfctl_free_states(states);
1970 		return (ret);
1971 	}
1972 
1973 	return (0);
1974 }
1975 
1976 void
1977 pfctl_free_states(struct pfctl_states *states)
1978 {
1979 	struct pfctl_state *s, *tmp;
1980 
1981 	TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) {
1982 		free(s);
1983 	}
1984 
1985 	bzero(states, sizeof(*states));
1986 }
1987 
1988 struct pfctl_nl_clear_states {
1989 	uint32_t killed;
1990 };
1991 #define	_OUT(_field)	offsetof(struct pfctl_nl_clear_states, _field)
1992 static struct snl_attr_parser ap_clear_states[] = {
1993 	{ .type = PF_CS_KILLED, .off = _OUT(killed), .cb = snl_attr_get_uint32 },
1994 };
1995 static struct snl_field_parser fp_clear_states[] = {};
1996 #undef _OUT
1997 SNL_DECLARE_PARSER(clear_states_parser, struct genlmsghdr, fp_clear_states, ap_clear_states);
1998 
1999 static int
2000 _pfctl_clear_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
2001     unsigned int *killed, int cmd)
2002 {
2003 	struct snl_writer nw;
2004 	struct snl_errmsg_data e = {};
2005 	struct pfctl_nl_clear_states attrs = {};
2006 	struct nlmsghdr *hdr;
2007 	uint32_t seq_id;
2008 	int family_id;
2009 
2010 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2011 	if (family_id == 0)
2012 		return (ENOTSUP);
2013 
2014 	snl_init_writer(&h->ss, &nw);
2015 	hdr = snl_create_genl_msg_request(&nw, family_id, cmd);
2016 	hdr->nlmsg_flags |= NLM_F_DUMP;
2017 
2018 	snl_add_msg_attr_u64(&nw, PF_CS_CMP_ID, kill->cmp.id);
2019 	snl_add_msg_attr_u32(&nw, PF_CS_CMP_CREATORID, htonl(kill->cmp.creatorid));
2020 	snl_add_msg_attr_u8(&nw, PF_CS_CMP_DIR, kill->cmp.direction);
2021 	snl_add_msg_attr_u8(&nw, PF_CS_AF, kill->af);
2022 	snl_add_msg_attr_u8(&nw, PF_CS_PROTO, kill->proto);
2023 	snl_add_msg_attr_rule_addr(&nw, PF_CS_SRC, &kill->src);
2024 	snl_add_msg_attr_rule_addr(&nw, PF_CS_DST, &kill->dst);
2025 	snl_add_msg_attr_rule_addr(&nw, PF_CS_RT_ADDR, &kill->rt_addr);
2026 	snl_add_msg_attr_string(&nw, PF_CS_IFNAME, kill->ifname);
2027 	snl_add_msg_attr_string(&nw, PF_CS_LABEL, kill->label);
2028 	snl_add_msg_attr_bool(&nw, PF_CS_KILL_MATCH, kill->kill_match);
2029 	snl_add_msg_attr_bool(&nw, PF_CS_NAT, kill->nat);
2030 
2031 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2032 		return (ENXIO);
2033 
2034 	seq_id = hdr->nlmsg_seq;
2035 
2036 	if (! snl_send_message(&h->ss, hdr))
2037 		return (ENXIO);
2038 
2039 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2040 		if (! snl_parse_nlmsg(&h->ss, hdr, &clear_states_parser, &attrs))
2041 			continue;
2042 	}
2043 
2044 	if (killed)
2045 		*killed = attrs.killed;
2046 
2047 	return (e.error);
2048 }
2049 
2050 int
2051 pfctl_clear_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
2052     unsigned int *killed)
2053 {
2054 	return(_pfctl_clear_states_h(h, kill, killed, PFNL_CMD_CLRSTATES));
2055 }
2056 
2057 int
2058 pfctl_kill_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
2059     unsigned int *killed)
2060 {
2061 	return(_pfctl_clear_states_h(h, kill, killed, PFNL_CMD_KILLSTATES));
2062 }
2063 
2064 static int
2065 _pfctl_clear_states(int dev __unused, const struct pfctl_kill *kill,
2066     unsigned int *killed, uint64_t cmd)
2067 {
2068 	struct pfctl_handle *h;
2069 	int ret;
2070 
2071 	h = pfctl_open(PF_DEVICE);
2072 	if (h == NULL)
2073 		return (ENODEV);
2074 
2075 	ret = _pfctl_clear_states_h(h, kill, killed, cmd);
2076 	pfctl_close(h);
2077 
2078 	return (ret);
2079 }
2080 
2081 int
2082 pfctl_clear_states(int dev __unused, const struct pfctl_kill *kill,
2083     unsigned int *killed)
2084 {
2085 	return (_pfctl_clear_states(dev, kill, killed, PFNL_CMD_CLRSTATES));
2086 }
2087 
2088 int
2089 pfctl_kill_states(int dev __unused, const struct pfctl_kill *kill, unsigned int *killed)
2090 {
2091 	return (_pfctl_clear_states(dev, kill, killed, PFNL_CMD_KILLSTATES));
2092 }
2093 
2094 int
2095 pfctl_clear_rules(int dev, const char *anchorname)
2096 {
2097 	struct pfioc_trans trans;
2098 	struct pfioc_trans_e transe[2];
2099 	int ret;
2100 
2101 	bzero(&trans, sizeof(trans));
2102 	bzero(&transe, sizeof(transe));
2103 
2104 	transe[0].rs_num = PF_RULESET_SCRUB;
2105 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
2106 	    >= sizeof(transe[0].anchor))
2107 		return (E2BIG);
2108 
2109 	transe[1].rs_num = PF_RULESET_FILTER;
2110 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
2111 	    >= sizeof(transe[1].anchor))
2112 		return (E2BIG);
2113 
2114 	trans.size = 2;
2115 	trans.esize = sizeof(transe[0]);
2116 	trans.array = transe;
2117 
2118 	ret = ioctl(dev, DIOCXBEGIN, &trans);
2119 	if (ret != 0)
2120 		return (ret);
2121 	return ioctl(dev, DIOCXCOMMIT, &trans);
2122 }
2123 
2124 int
2125 pfctl_clear_nat(int dev, const char *anchorname)
2126 {
2127 	struct pfioc_trans trans;
2128 	struct pfioc_trans_e transe[3];
2129 	int ret;
2130 
2131 	bzero(&trans, sizeof(trans));
2132 	bzero(&transe, sizeof(transe));
2133 
2134 	transe[0].rs_num = PF_RULESET_NAT;
2135 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
2136 	    >= sizeof(transe[0].anchor))
2137 		return (E2BIG);
2138 
2139 	transe[1].rs_num = PF_RULESET_BINAT;
2140 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
2141 	    >= sizeof(transe[0].anchor))
2142 		return (E2BIG);
2143 
2144 	transe[2].rs_num = PF_RULESET_RDR;
2145 	if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor))
2146 	    >= sizeof(transe[2].anchor))
2147 		return (E2BIG);
2148 
2149 	trans.size = 3;
2150 	trans.esize = sizeof(transe[0]);
2151 	trans.array = transe;
2152 
2153 	ret = ioctl(dev, DIOCXBEGIN, &trans);
2154 	if (ret != 0)
2155 		return (ret);
2156 	return ioctl(dev, DIOCXCOMMIT, &trans);
2157 }
2158 int
2159 pfctl_clear_eth_rules(int dev, const char *anchorname)
2160 {
2161 	struct pfioc_trans trans;
2162 	struct pfioc_trans_e transe;
2163 	int ret;
2164 
2165 	bzero(&trans, sizeof(trans));
2166 	bzero(&transe, sizeof(transe));
2167 
2168 	transe.rs_num = PF_RULESET_ETH;
2169 	if (strlcpy(transe.anchor, anchorname, sizeof(transe.anchor))
2170 	    >= sizeof(transe.anchor))
2171 		return (E2BIG);
2172 
2173 	trans.size = 1;
2174 	trans.esize = sizeof(transe);
2175 	trans.array = &transe;
2176 
2177 	ret = ioctl(dev, DIOCXBEGIN, &trans);
2178 	if (ret != 0)
2179 		return (ret);
2180 	return ioctl(dev, DIOCXCOMMIT, &trans);
2181 }
2182 
2183 static int
2184 pfctl_get_limit(int dev, const int index, uint *limit)
2185 {
2186 	struct pfioc_limit pl;
2187 
2188 	bzero(&pl, sizeof(pl));
2189 	pl.index = index;
2190 
2191 	if (ioctl(dev, DIOCGETLIMIT, &pl) == -1)
2192 		return (errno);
2193 
2194 	*limit = pl.limit;
2195 
2196 	return (0);
2197 }
2198 
2199 int
2200 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s)
2201 {
2202 	struct pfioc_nv	 nv;
2203 	nvlist_t	*nvl;
2204 	int		 ret;
2205 	uint		 state_limit;
2206 	uint64_t	 lim, hi, lo;
2207 
2208 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
2209 	if (ret != 0)
2210 		return (ret);
2211 
2212 	lim = state_limit;
2213 	hi = lim * s->highwater / 100;
2214 	lo = lim * s->lowwater / 100;
2215 
2216 	if (lo == hi)
2217 		hi++;
2218 
2219 	nvl = nvlist_create(0);
2220 
2221 	nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER);
2222 	nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE);
2223 	nvlist_add_number(nvl, "highwater", hi);
2224 	nvlist_add_number(nvl, "lowwater", lo);
2225 
2226 	nv.data = nvlist_pack(nvl, &nv.len);
2227 	nv.size = nv.len;
2228 	nvlist_destroy(nvl);
2229 	nvl = NULL;
2230 
2231 	ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv);
2232 
2233 	free(nv.data);
2234 	return (ret);
2235 }
2236 
2237 int
2238 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s)
2239 {
2240 	nvlist_t	*nvl;
2241 	int		 ret;
2242 	uint		 state_limit;
2243 	bool		 enabled, adaptive;
2244 
2245 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
2246 	if (ret != 0)
2247 		return (ret);
2248 
2249 	bzero(s, sizeof(*s));
2250 
2251 	nvl = nvlist_create(0);
2252 
2253 	if ((ret = pfctl_do_ioctl(dev, DIOCGETSYNCOOKIES, 256, &nvl)) != 0) {
2254 		ret = errno;
2255 		goto out;
2256 	}
2257 
2258 	enabled = nvlist_get_bool(nvl, "enabled");
2259 	adaptive = nvlist_get_bool(nvl, "adaptive");
2260 
2261 	if (enabled) {
2262 		if (adaptive)
2263 			s->mode = PFCTL_SYNCOOKIES_ADAPTIVE;
2264 		else
2265 			s->mode = PFCTL_SYNCOOKIES_ALWAYS;
2266 	} else {
2267 		s->mode = PFCTL_SYNCOOKIES_NEVER;
2268 	}
2269 
2270 	s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit;
2271 	s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit;
2272 	s->halfopen_states = nvlist_get_number(nvl, "halfopen_states");
2273 
2274 out:
2275 	nvlist_destroy(nvl);
2276 	return (ret);
2277 }
2278 
2279 int
2280 pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
2281     *addr, int size, int *nadd, int flags)
2282 {
2283 	struct pfioc_table io;
2284 
2285 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
2286 		return (EINVAL);
2287 	}
2288 	bzero(&io, sizeof io);
2289 	io.pfrio_flags = flags;
2290 	io.pfrio_table = *tbl;
2291 	io.pfrio_buffer = addr;
2292 	io.pfrio_esize = sizeof(*addr);
2293 	io.pfrio_size = size;
2294 
2295 	if (ioctl(dev, DIOCRADDADDRS, &io))
2296 		return (errno);
2297 	if (nadd != NULL)
2298 		*nadd = io.pfrio_nadd;
2299 	return (0);
2300 }
2301 
2302 int
2303 pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
2304     *addr, int size, int *ndel, int flags)
2305 {
2306 	struct pfioc_table io;
2307 
2308 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
2309 		return (EINVAL);
2310 	}
2311 	bzero(&io, sizeof io);
2312 	io.pfrio_flags = flags;
2313 	io.pfrio_table = *tbl;
2314 	io.pfrio_buffer = addr;
2315 	io.pfrio_esize = sizeof(*addr);
2316 	io.pfrio_size = size;
2317 
2318 	if (ioctl(dev, DIOCRDELADDRS, &io))
2319 		return (errno);
2320 	if (ndel != NULL)
2321 		*ndel = io.pfrio_ndel;
2322 	return (0);
2323 }
2324 
2325 int
2326 pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
2327     *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags)
2328 {
2329 	struct pfioc_table io;
2330 
2331 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
2332 		return (EINVAL);
2333 	}
2334 	bzero(&io, sizeof io);
2335 	io.pfrio_flags = flags;
2336 	io.pfrio_table = *tbl;
2337 	io.pfrio_buffer = addr;
2338 	io.pfrio_esize = sizeof(*addr);
2339 	io.pfrio_size = size;
2340 	io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
2341 	if (ioctl(dev, DIOCRSETADDRS, &io))
2342 		return (-1);
2343 	if (nadd != NULL)
2344 		*nadd = io.pfrio_nadd;
2345 	if (ndel != NULL)
2346 		*ndel = io.pfrio_ndel;
2347 	if (nchange != NULL)
2348 		*nchange = io.pfrio_nchange;
2349 	if (size2 != NULL)
2350 		*size2 = io.pfrio_size2;
2351 	return (0);
2352 }
2353 
2354 int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr,
2355     int *size, int flags)
2356 {
2357 	struct pfioc_table io;
2358 
2359 	if (tbl == NULL || size == NULL || *size < 0 ||
2360 	    (*size && addr == NULL)) {
2361 		return (EINVAL);
2362 	}
2363 	bzero(&io, sizeof io);
2364 	io.pfrio_flags = flags;
2365 	io.pfrio_table = *tbl;
2366 	io.pfrio_buffer = addr;
2367 	io.pfrio_esize = sizeof(*addr);
2368 	io.pfrio_size = *size;
2369 	if (ioctl(dev, DIOCRGETADDRS, &io))
2370 		return (-1);
2371 	*size = io.pfrio_size;
2372 	return (0);
2373 }
2374 
2375 int
2376 pfctl_set_statusif(struct pfctl_handle *h, const char *ifname)
2377 {
2378 	struct snl_writer nw;
2379 	struct snl_errmsg_data e = {};
2380 	struct nlmsghdr *hdr;
2381 	uint32_t seq_id;
2382 	int family_id;
2383 
2384 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2385 	if (family_id == 0)
2386 		return (ENOTSUP);
2387 
2388 	snl_init_writer(&h->ss, &nw);
2389 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_SET_STATUSIF);
2390 
2391 	snl_add_msg_attr_string(&nw, PF_SS_IFNAME, ifname);
2392 
2393 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2394 		return (ENXIO);
2395 
2396 	seq_id = hdr->nlmsg_seq;
2397 
2398 	if (! snl_send_message(&h->ss, hdr))
2399 		return (ENXIO);
2400 
2401 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2402 	}
2403 
2404 	return (e.error);
2405 }
2406