xref: /freebsd/lib/libpfctl/libpfctl.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
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  * $FreeBSD$
32  */
33 
34 #include <sys/cdefs.h>
35 
36 #include <sys/ioctl.h>
37 #include <sys/nv.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 
41 #include <net/if.h>
42 #include <net/pfvar.h>
43 #include <netinet/in.h>
44 
45 #include <assert.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "libpfctl.h"
52 
53 const char* PFCTL_SYNCOOKIES_MODE_NAMES[] = {
54 	"never",
55 	"always",
56 	"adaptive"
57 };
58 
59 static int	_pfctl_clear_states(int , const struct pfctl_kill *,
60 		    unsigned int *, uint64_t);
61 
62 static void
63 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems,
64     uint8_t *numbers, size_t *nelems)
65 {
66 	const uint64_t *tmp;
67 	size_t elems;
68 
69 	tmp = nvlist_get_number_array(nvl, name, &elems);
70 	assert(elems <= maxelems);
71 
72 	for (size_t i = 0; i < elems; i++)
73 		numbers[i] = tmp[i];
74 
75 	if (nelems)
76 		*nelems = elems;
77 }
78 
79 static void
80 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems,
81     uint16_t *numbers, size_t *nelems)
82 {
83 	const uint64_t *tmp;
84 	size_t elems;
85 
86 	tmp = nvlist_get_number_array(nvl, name, &elems);
87 	assert(elems <= maxelems);
88 
89 	for (size_t i = 0; i < elems; i++)
90 		numbers[i] = tmp[i];
91 
92 	if (nelems)
93 		*nelems = elems;
94 }
95 
96 static void
97 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems,
98     uint32_t *numbers, size_t *nelems)
99 {
100 	const uint64_t *tmp;
101 	size_t elems;
102 
103 	tmp = nvlist_get_number_array(nvl, name, &elems);
104 	assert(elems <= maxelems);
105 
106 	for (size_t i = 0; i < elems; i++)
107 		numbers[i] = tmp[i];
108 
109 	if (nelems)
110 		*nelems = elems;
111 }
112 
113 static void
114 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems,
115     uint64_t *numbers, size_t *nelems)
116 {
117 	const uint64_t *tmp;
118 	size_t elems;
119 
120 	tmp = nvlist_get_number_array(nvl, name, &elems);
121 	assert(elems <= maxelems);
122 
123 	for (size_t i = 0; i < elems; i++)
124 		numbers[i] = tmp[i];
125 
126 	if (nelems)
127 		*nelems = elems;
128 }
129 
130 static void
131 _pfctl_get_status_counters(const nvlist_t *nvl,
132     struct pfctl_status_counters *counters)
133 {
134 	const uint64_t		*ids, *counts;
135 	const char *const	*names;
136 	size_t id_len, counter_len, names_len;
137 
138 	ids = nvlist_get_number_array(nvl, "ids", &id_len);
139 	counts = nvlist_get_number_array(nvl, "counters", &counter_len);
140 	names = nvlist_get_string_array(nvl, "names", &names_len);
141 	assert(id_len == counter_len);
142 	assert(counter_len == names_len);
143 
144 	TAILQ_INIT(counters);
145 
146 	for (size_t i = 0; i < id_len; i++) {
147 		struct pfctl_status_counter *c;
148 
149 		c = malloc(sizeof(*c));
150 
151 		c->id = ids[i];
152 		c->counter = counts[i];
153 		c->name = strdup(names[i]);
154 
155 		TAILQ_INSERT_TAIL(counters, c, entry);
156 	}
157 }
158 
159 struct pfctl_status *
160 pfctl_get_status(int dev)
161 {
162 	struct pfioc_nv	 nv;
163 	struct pfctl_status	*status;
164 	nvlist_t	*nvl;
165 	size_t		 len;
166 	const void	*chksum;
167 
168 	status = calloc(1, sizeof(*status));
169 	if (status == NULL)
170 		return (NULL);
171 
172 	nv.data = malloc(4096);
173 	nv.len = nv.size = 4096;
174 
175 	if (ioctl(dev, DIOCGETSTATUSNV, &nv)) {
176 		free(nv.data);
177 		free(status);
178 		return (NULL);
179 	}
180 
181 	nvl = nvlist_unpack(nv.data, nv.len, 0);
182 	free(nv.data);
183 	if (nvl == NULL) {
184 		free(status);
185 		return (NULL);
186 	}
187 
188 	status->running = nvlist_get_bool(nvl, "running");
189 	status->since = nvlist_get_number(nvl, "since");
190 	status->debug = nvlist_get_number(nvl, "debug");
191 	status->hostid = ntohl(nvlist_get_number(nvl, "hostid"));
192 	status->states = nvlist_get_number(nvl, "states");
193 	status->src_nodes = nvlist_get_number(nvl, "src_nodes");
194 
195 	strlcpy(status->ifname, nvlist_get_string(nvl, "ifname"),
196 	    IFNAMSIZ);
197 	chksum = nvlist_get_binary(nvl, "chksum", &len);
198 	assert(len == PF_MD5_DIGEST_LENGTH);
199 	memcpy(status->pf_chksum, chksum, len);
200 
201 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "counters"),
202 	    &status->counters);
203 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "lcounters"),
204 	    &status->lcounters);
205 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "fcounters"),
206 	    &status->fcounters);
207 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"),
208 	    &status->scounters);
209 
210 	pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 3,
211 	    (uint64_t *)status->pcounters, NULL);
212 	pf_nvuint_64_array(nvl, "bcounters", 2 * 2,
213 	    (uint64_t *)status->bcounters, NULL);
214 
215 	nvlist_destroy(nvl);
216 
217 	return (status);
218 }
219 
220 void
221 pfctl_free_status(struct pfctl_status *status)
222 {
223 	struct pfctl_status_counter *c, *tmp;
224 
225 	TAILQ_FOREACH_SAFE(c, &status->counters, entry, tmp) {
226 		free(c->name);
227 		free(c);
228 	}
229 	TAILQ_FOREACH_SAFE(c, &status->lcounters, entry, tmp) {
230 		free(c->name);
231 		free(c);
232 	}
233 	TAILQ_FOREACH_SAFE(c, &status->fcounters, entry, tmp) {
234 		free(c->name);
235 		free(c);
236 	}
237 	TAILQ_FOREACH_SAFE(c, &status->scounters, entry, tmp) {
238 		free(c->name);
239 		free(c);
240 	}
241 
242 	free(status);
243 }
244 
245 static void
246 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name,
247     const struct pf_addr *addr)
248 {
249 	nvlist_t *nvl = nvlist_create(0);
250 
251 	nvlist_add_binary(nvl, "addr", addr, sizeof(*addr));
252 
253 	nvlist_add_nvlist(nvparent, name, nvl);
254 	nvlist_destroy(nvl);
255 }
256 
257 static void
258 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr)
259 {
260 	size_t len;
261 	const void *data;
262 
263 	data = nvlist_get_binary(nvl, "addr", &len);
264 	assert(len == sizeof(struct pf_addr));
265 	memcpy(addr, data, len);
266 }
267 
268 static void
269 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name,
270     const struct pf_addr_wrap *addr)
271 {
272 	nvlist_t *nvl = nvlist_create(0);
273 
274 	nvlist_add_number(nvl, "type", addr->type);
275 	nvlist_add_number(nvl, "iflags", addr->iflags);
276 	if (addr->type == PF_ADDR_DYNIFTL)
277 		nvlist_add_string(nvl, "ifname", addr->v.ifname);
278 	if (addr->type == PF_ADDR_TABLE)
279 		nvlist_add_string(nvl, "tblname", addr->v.tblname);
280 	pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr);
281 	pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask);
282 
283 	nvlist_add_nvlist(nvparent, name, nvl);
284 	nvlist_destroy(nvl);
285 }
286 
287 static void
288 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
289 {
290 	bzero(addr, sizeof(*addr));
291 
292 	addr->type = nvlist_get_number(nvl, "type");
293 	addr->iflags = nvlist_get_number(nvl, "iflags");
294 	if (addr->type == PF_ADDR_DYNIFTL) {
295 		strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"),
296 		    IFNAMSIZ);
297 		addr->p.dyncnt = nvlist_get_number(nvl, "dyncnt");
298 	}
299 	if (addr->type == PF_ADDR_TABLE) {
300 		strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"),
301 		    PF_TABLE_NAME_SIZE);
302 		addr->p.tblcnt = nvlist_get_number(nvl, "tblcnt");
303 	}
304 
305 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr);
306 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask);
307 }
308 
309 static void
310 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name,
311     const struct pf_rule_addr *addr)
312 {
313 	uint64_t ports[2];
314 	nvlist_t *nvl = nvlist_create(0);
315 
316 	pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr);
317 	ports[0] = addr->port[0];
318 	ports[1] = addr->port[1];
319 	nvlist_add_number_array(nvl, "port", ports, 2);
320 	nvlist_add_number(nvl, "neg", addr->neg);
321 	nvlist_add_number(nvl, "port_op", addr->port_op);
322 
323 	nvlist_add_nvlist(nvparent, name, nvl);
324 	nvlist_destroy(nvl);
325 }
326 
327 static void
328 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
329 {
330 	pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr);
331 
332 	pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL);
333 	addr->neg = nvlist_get_number(nvl, "neg");
334 	addr->port_op = nvlist_get_number(nvl, "port_op");
335 }
336 
337 static void
338 pfctl_nv_add_mape(nvlist_t *nvparent, const char *name,
339     const struct pf_mape_portset *mape)
340 {
341 	nvlist_t *nvl = nvlist_create(0);
342 
343 	nvlist_add_number(nvl, "offset", mape->offset);
344 	nvlist_add_number(nvl, "psidlen", mape->psidlen);
345 	nvlist_add_number(nvl, "psid", mape->psid);
346 	nvlist_add_nvlist(nvparent, name, nvl);
347 	nvlist_destroy(nvl);
348 }
349 
350 static void
351 pfctl_nv_add_pool(nvlist_t *nvparent, const char *name,
352     const struct pfctl_pool *pool)
353 {
354 	uint64_t ports[2];
355 	nvlist_t *nvl = nvlist_create(0);
356 
357 	nvlist_add_binary(nvl, "key", &pool->key, sizeof(pool->key));
358 	pfctl_nv_add_addr(nvl, "counter", &pool->counter);
359 	nvlist_add_number(nvl, "tblidx", pool->tblidx);
360 
361 	ports[0] = pool->proxy_port[0];
362 	ports[1] = pool->proxy_port[1];
363 	nvlist_add_number_array(nvl, "proxy_port", ports, 2);
364 	nvlist_add_number(nvl, "opts", pool->opts);
365 	pfctl_nv_add_mape(nvl, "mape", &pool->mape);
366 
367 	nvlist_add_nvlist(nvparent, name, nvl);
368 	nvlist_destroy(nvl);
369 }
370 
371 static void
372 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape)
373 {
374 	mape->offset = nvlist_get_number(nvl, "offset");
375 	mape->psidlen = nvlist_get_number(nvl, "psidlen");
376 	mape->psid = nvlist_get_number(nvl, "psid");
377 }
378 
379 static void
380 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool)
381 {
382 	size_t len;
383 	const void *data;
384 
385 	data = nvlist_get_binary(nvl, "key", &len);
386 	assert(len == sizeof(pool->key));
387 	memcpy(&pool->key, data, len);
388 
389 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter);
390 
391 	pool->tblidx = nvlist_get_number(nvl, "tblidx");
392 	pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL);
393 	pool->opts = nvlist_get_number(nvl, "opts");
394 
395 	if (nvlist_exists_nvlist(nvl, "mape"))
396 		pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape);
397 }
398 
399 static void
400 pfctl_nv_add_uid(nvlist_t *nvparent, const char *name,
401     const struct pf_rule_uid *uid)
402 {
403 	uint64_t uids[2];
404 	nvlist_t *nvl = nvlist_create(0);
405 
406 	uids[0] = uid->uid[0];
407 	uids[1] = uid->uid[1];
408 	nvlist_add_number_array(nvl, "uid", uids, 2);
409 	nvlist_add_number(nvl, "op", uid->op);
410 
411 	nvlist_add_nvlist(nvparent, name, nvl);
412 	nvlist_destroy(nvl);
413 }
414 
415 static void
416 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
417 {
418 	pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL);
419 	uid->op = nvlist_get_number(nvl, "op");
420 }
421 
422 static void
423 pfctl_nv_add_divert(nvlist_t *nvparent, const char *name,
424     const struct pfctl_rule *r)
425 {
426 	nvlist_t *nvl = nvlist_create(0);
427 
428 	pfctl_nv_add_addr(nvl, "addr", &r->divert.addr);
429 	nvlist_add_number(nvl, "port", r->divert.port);
430 
431 	nvlist_add_nvlist(nvparent, name, nvl);
432 	nvlist_destroy(nvl);
433 }
434 
435 static void
436 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule)
437 {
438 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr);
439 	rule->divert.port = nvlist_get_number(nvl, "port");
440 }
441 
442 static void
443 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule)
444 {
445 	const uint64_t *skip;
446 	const char *const *labels;
447 	size_t skipcount, labelcount;
448 
449 	rule->nr = nvlist_get_number(nvl, "nr");
450 
451 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src);
452 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst);
453 
454 	skip = nvlist_get_number_array(nvl, "skip", &skipcount);
455 	assert(skip);
456 	assert(skipcount == PF_SKIP_COUNT);
457 	for (int i = 0; i < PF_SKIP_COUNT; i++)
458 		rule->skip[i].nr = skip[i];
459 
460 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
461 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
462 	for (size_t i = 0; i < labelcount; i++)
463 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
464 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
465 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
466 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
467 	strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE);
468 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
469 	    PF_TAG_NAME_SIZE);
470 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
471 	    PF_TAG_NAME_SIZE);
472 
473 	strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"),
474 	    PF_TABLE_NAME_SIZE);
475 
476 	pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool);
477 
478 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
479 	pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL);
480 	pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL);
481 
482 	rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint");
483 
484 	rule->rtableid = nvlist_get_number(nvl, "rtableid");
485 	pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL);
486 	rule->max_states = nvlist_get_number(nvl, "max_states");
487 	rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes");
488 	rule->max_src_states = nvlist_get_number(nvl, "max_src_states");
489 	rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn");
490 	rule->max_src_conn_rate.limit =
491 	    nvlist_get_number(nvl, "max_src_conn_rate.limit");
492 	rule->max_src_conn_rate.seconds =
493 	    nvlist_get_number(nvl, "max_src_conn_rate.seconds");
494 	rule->qid = nvlist_get_number(nvl, "qid");
495 	rule->pqid = nvlist_get_number(nvl, "pqid");
496 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
497 	rule->dnrpipe = nvlist_get_number(nvl, "dnrpipe");
498 	rule->free_flags = nvlist_get_number(nvl, "dnflags");
499 	rule->prob = nvlist_get_number(nvl, "prob");
500 	rule->cuid = nvlist_get_number(nvl, "cuid");
501 	rule->cpid = nvlist_get_number(nvl, "cpid");
502 
503 	rule->return_icmp = nvlist_get_number(nvl, "return_icmp");
504 	rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6");
505 	rule->max_mss = nvlist_get_number(nvl, "max_mss");
506 	rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags");
507 
508 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid);
509 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"),
510 	    (struct pf_rule_uid *)&rule->gid);
511 
512 	rule->rule_flag = nvlist_get_number(nvl, "rule_flag");
513 	rule->action = nvlist_get_number(nvl, "action");
514 	rule->direction = nvlist_get_number(nvl, "direction");
515 	rule->log = nvlist_get_number(nvl, "log");
516 	rule->logif = nvlist_get_number(nvl, "logif");
517 	rule->quick = nvlist_get_number(nvl, "quick");
518 	rule->ifnot = nvlist_get_number(nvl, "ifnot");
519 	rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not");
520 	rule->natpass = nvlist_get_number(nvl, "natpass");
521 
522 	rule->keep_state = nvlist_get_number(nvl, "keep_state");
523 	rule->af = nvlist_get_number(nvl, "af");
524 	rule->proto = nvlist_get_number(nvl, "proto");
525 	rule->type = nvlist_get_number(nvl, "type");
526 	rule->code = nvlist_get_number(nvl, "code");
527 	rule->flags = nvlist_get_number(nvl, "flags");
528 	rule->flagset = nvlist_get_number(nvl, "flagset");
529 	rule->min_ttl = nvlist_get_number(nvl, "min_ttl");
530 	rule->allow_opts = nvlist_get_number(nvl, "allow_opts");
531 	rule->rt = nvlist_get_number(nvl, "rt");
532 	rule->return_ttl  = nvlist_get_number(nvl, "return_ttl");
533 	rule->tos = nvlist_get_number(nvl, "tos");
534 	rule->set_tos = nvlist_get_number(nvl, "set_tos");
535 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
536 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
537 
538 	rule->flush = nvlist_get_number(nvl, "flush");
539 	rule->prio = nvlist_get_number(nvl, "prio");
540 	pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL);
541 
542 	pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule);
543 
544 	rule->states_cur = nvlist_get_number(nvl, "states_cur");
545 	rule->states_tot = nvlist_get_number(nvl, "states_tot");
546 	rule->src_nodes = nvlist_get_number(nvl, "src_nodes");
547 }
548 
549 static void
550 pfctl_nveth_addr_to_eth_addr(const nvlist_t *nvl, struct pfctl_eth_addr *addr)
551 {
552 	static const u_int8_t EMPTY_MAC[ETHER_ADDR_LEN] = { 0 };
553 	size_t len;
554 	const void *data;
555 
556 	data = nvlist_get_binary(nvl, "addr", &len);
557 	assert(len == sizeof(addr->addr));
558 	memcpy(addr->addr, data, sizeof(addr->addr));
559 
560 	data = nvlist_get_binary(nvl, "mask", &len);
561 	assert(len == sizeof(addr->mask));
562 	memcpy(addr->mask, data, sizeof(addr->mask));
563 
564 	addr->neg = nvlist_get_bool(nvl, "neg");
565 
566 	/* To make checks for 'is this address set?' easier. */
567 	addr->isset = memcmp(addr->addr, EMPTY_MAC, ETHER_ADDR_LEN) != 0;
568 }
569 
570 static nvlist_t *
571 pfctl_eth_addr_to_nveth_addr(const struct pfctl_eth_addr *addr)
572 {
573 	nvlist_t *nvl;
574 
575 	nvl = nvlist_create(0);
576 	if (nvl == NULL)
577 		return (NULL);
578 
579 	nvlist_add_bool(nvl, "neg", addr->neg);
580 	nvlist_add_binary(nvl, "addr", &addr->addr, ETHER_ADDR_LEN);
581 	nvlist_add_binary(nvl, "mask", &addr->mask, ETHER_ADDR_LEN);
582 
583 	return (nvl);
584 }
585 
586 static void
587 pfctl_nveth_rule_to_eth_rule(const nvlist_t *nvl, struct pfctl_eth_rule *rule)
588 {
589 	rule->nr = nvlist_get_number(nvl, "nr");
590 	rule->quick = nvlist_get_bool(nvl, "quick");
591 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
592 	rule->ifnot = nvlist_get_bool(nvl, "ifnot");
593 	rule->direction = nvlist_get_number(nvl, "direction");
594 	rule->proto = nvlist_get_number(nvl, "proto");
595 
596 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "src"),
597 	    &rule->src);
598 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "dst"),
599 	    &rule->dst);
600 
601 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipsrc"),
602 	    &rule->ipsrc);
603 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipdst"),
604 	    &rule->ipdst);
605 
606 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
607 	rule->packets[0] = nvlist_get_number(nvl, "packets-in");
608 	rule->packets[1] = nvlist_get_number(nvl, "packets-out");
609 	rule->bytes[0] = nvlist_get_number(nvl, "bytes-in");
610 	rule->bytes[1] = nvlist_get_number(nvl, "bytes-out");
611 
612 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
613 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
614 	    PF_TAG_NAME_SIZE);
615 
616 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
617 	rule->dnflags = nvlist_get_number(nvl, "dnflags");
618 
619 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
620 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
621 
622 	rule->action = nvlist_get_number(nvl, "action");
623 }
624 
625 int
626 pfctl_get_eth_rulesets_info(int dev, struct pfctl_eth_rulesets_info *ri,
627     const char *path)
628 {
629 	uint8_t buf[1024];
630 	struct pfioc_nv nv;
631 	nvlist_t *nvl;
632 	void *packed;
633 	size_t len;
634 
635 	bzero(ri, sizeof(*ri));
636 
637 	nvl = nvlist_create(0);
638 	nvlist_add_string(nvl, "path", path);
639 	packed = nvlist_pack(nvl, &len);
640 	memcpy(buf, packed, len);
641 	free(packed);
642 	nvlist_destroy(nvl);
643 
644 	nv.data = buf;
645 	nv.len = len;
646 	nv.size = sizeof(buf);
647 
648 	if (ioctl(dev, DIOCGETETHRULESETS, &nv) != 0)
649 		return (errno);
650 
651 	nvl = nvlist_unpack(buf, nv.len, 0);
652 	if (nvl == NULL)
653 		return (EIO);
654 
655 	ri->nr = nvlist_get_number(nvl, "nr");
656 
657 	nvlist_destroy(nvl);
658 	return (0);
659 }
660 
661 int
662 pfctl_get_eth_ruleset(int dev, const char *path, int nr,
663     struct pfctl_eth_ruleset_info *ri)
664 {
665 	uint8_t buf[1024];
666 	struct pfioc_nv nv;
667 	nvlist_t *nvl;
668 	void *packed;
669 	size_t len;
670 
671 	bzero(ri, sizeof(*ri));
672 
673 	nvl = nvlist_create(0);
674 	nvlist_add_string(nvl, "path", path);
675 	nvlist_add_number(nvl, "nr", nr);
676 	packed = nvlist_pack(nvl, &len);
677 	memcpy(buf, packed, len);
678 	free(packed);
679 	nvlist_destroy(nvl);
680 
681 	nv.data = buf;
682 	nv.len = len;
683 	nv.size = sizeof(buf);
684 
685 	if (ioctl(dev, DIOCGETETHRULESET, &nv) != 0)
686 		return (errno);
687 
688 	nvl = nvlist_unpack(buf, nv.len, 0);
689 	if (nvl == NULL)
690 		return (EIO);
691 
692 	ri->nr = nvlist_get_number(nvl, "nr");
693 	strlcpy(ri->path, nvlist_get_string(nvl, "path"), MAXPATHLEN);
694 	strlcpy(ri->name, nvlist_get_string(nvl, "name"),
695 	    PF_ANCHOR_NAME_SIZE);
696 
697 	return (0);
698 }
699 
700 int
701 pfctl_get_eth_rules_info(int dev, struct pfctl_eth_rules_info *rules,
702     const char *path)
703 {
704 	uint8_t buf[1024];
705 	struct pfioc_nv nv;
706 	nvlist_t *nvl;
707 	void *packed;
708 	size_t len;
709 
710 	bzero(rules, sizeof(*rules));
711 
712 	nvl = nvlist_create(0);
713 	nvlist_add_string(nvl, "anchor", path);
714 	packed = nvlist_pack(nvl, &len);
715 	memcpy(buf, packed, len);
716 	free(packed);
717 	nvlist_destroy(nvl);
718 
719 	nv.data = buf;
720 	nv.len = len;
721 	nv.size = sizeof(buf);
722 
723 	if (ioctl(dev, DIOCGETETHRULES, &nv) != 0)
724 		return (errno);
725 
726 	nvl = nvlist_unpack(buf, nv.len, 0);
727 	if (nvl == NULL)
728 		return (EIO);
729 
730 	rules->nr = nvlist_get_number(nvl, "nr");
731 	rules->ticket = nvlist_get_number(nvl, "ticket");
732 
733 	nvlist_destroy(nvl);
734 	return (0);
735 }
736 
737 int
738 pfctl_get_eth_rule(int dev, uint32_t nr, uint32_t ticket,
739     const char *path, struct pfctl_eth_rule *rule, bool clear,
740     char *anchor_call)
741 {
742 	uint8_t buf[2048];
743 	struct pfioc_nv nv;
744 	nvlist_t *nvl;
745 	void *data;
746 	size_t len;
747 
748 	nvl = nvlist_create(0);
749 
750 	nvlist_add_string(nvl, "anchor", path);
751 	nvlist_add_number(nvl, "ticket", ticket);
752 	nvlist_add_number(nvl, "nr", nr);
753 	nvlist_add_bool(nvl, "clear", clear);
754 
755 	data = nvlist_pack(nvl, &len);
756 	nv.data = buf;
757 	memcpy(buf, data, len);
758 	free(data);
759 	nv.len = len;
760 	nv.size = sizeof(buf);
761 	if (ioctl(dev, DIOCGETETHRULE, &nv)) {
762 		nvlist_destroy(nvl);
763 		return (errno);
764 	}
765 	nvlist_destroy(nvl);
766 
767 	nvl = nvlist_unpack(buf, nv.len, 0);
768 	if (nvl == NULL) {
769 		return (EIO);
770 	}
771 
772 	pfctl_nveth_rule_to_eth_rule(nvl, rule);
773 
774 	if (anchor_call)
775 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
776 		    MAXPATHLEN);
777 
778 	nvlist_destroy(nvl);
779 	return (0);
780 }
781 
782 int
783 pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r, const char *anchor,
784     const char *anchor_call, uint32_t ticket)
785 {
786 	struct pfioc_nv nv;
787 	nvlist_t *nvl, *addr;
788 	void *packed;
789 	int error = 0;
790 	size_t size;
791 
792 	nvl = nvlist_create(0);
793 
794 	nvlist_add_number(nvl, "ticket", ticket);
795 	nvlist_add_string(nvl, "anchor", anchor);
796 	nvlist_add_string(nvl, "anchor_call", anchor_call);
797 
798 	nvlist_add_number(nvl, "nr", r->nr);
799 	nvlist_add_bool(nvl, "quick", r->quick);
800 	nvlist_add_string(nvl, "ifname", r->ifname);
801 	nvlist_add_bool(nvl, "ifnot", r->ifnot);
802 	nvlist_add_number(nvl, "direction", r->direction);
803 	nvlist_add_number(nvl, "proto", r->proto);
804 
805 	addr = pfctl_eth_addr_to_nveth_addr(&r->src);
806 	if (addr == NULL) {
807 		nvlist_destroy(nvl);
808 		return (ENOMEM);
809 	}
810 	nvlist_add_nvlist(nvl, "src", addr);
811 	nvlist_destroy(addr);
812 
813 	addr = pfctl_eth_addr_to_nveth_addr(&r->dst);
814 	if (addr == NULL) {
815 		nvlist_destroy(nvl);
816 		return (ENOMEM);
817 	}
818 	nvlist_add_nvlist(nvl, "dst", addr);
819 	nvlist_destroy(addr);
820 
821 	pfctl_nv_add_rule_addr(nvl, "ipsrc", &r->ipsrc);
822 	pfctl_nv_add_rule_addr(nvl, "ipdst", &r->ipdst);
823 
824 	nvlist_add_string(nvl, "qname", r->qname);
825 	nvlist_add_string(nvl, "tagname", r->tagname);
826 	nvlist_add_number(nvl, "dnpipe", r->dnpipe);
827 	nvlist_add_number(nvl, "dnflags", r->dnflags);
828 
829 	nvlist_add_number(nvl, "action", r->action);
830 
831 	packed = nvlist_pack(nvl, &size);
832 	if (packed == NULL) {
833 		nvlist_destroy(nvl);
834 		return (ENOMEM);
835 	}
836 
837 	nv.len = size;
838 	nv.size = size;
839 	nv.data = packed;
840 
841 	if (ioctl(dev, DIOCADDETHRULE, &nv) != 0)
842 		error = errno;
843 
844 	free(packed);
845 	nvlist_destroy(nvl);
846 
847 	return (error);
848 }
849 
850 int
851 pfctl_add_rule(int dev, const struct pfctl_rule *r, const char *anchor,
852     const char *anchor_call, uint32_t ticket, uint32_t pool_ticket)
853 {
854 	struct pfioc_nv nv;
855 	uint64_t timeouts[PFTM_MAX];
856 	uint64_t set_prio[2];
857 	nvlist_t *nvl, *nvlr;
858 	size_t labelcount;
859 	int ret;
860 
861 	nvl = nvlist_create(0);
862 	nvlr = nvlist_create(0);
863 
864 	nvlist_add_number(nvl, "ticket", ticket);
865 	nvlist_add_number(nvl, "pool_ticket", pool_ticket);
866 	nvlist_add_string(nvl, "anchor", anchor);
867 	nvlist_add_string(nvl, "anchor_call", anchor_call);
868 
869 	nvlist_add_number(nvlr, "nr", r->nr);
870 	pfctl_nv_add_rule_addr(nvlr, "src", &r->src);
871 	pfctl_nv_add_rule_addr(nvlr, "dst", &r->dst);
872 
873 	labelcount = 0;
874 	while (r->label[labelcount][0] != 0 &&
875 	    labelcount < PF_RULE_MAX_LABEL_COUNT) {
876 		nvlist_append_string_array(nvlr, "labels",
877 		    r->label[labelcount]);
878 		labelcount++;
879 	}
880 	nvlist_add_number(nvlr, "ridentifier", r->ridentifier);
881 
882 	nvlist_add_string(nvlr, "ifname", r->ifname);
883 	nvlist_add_string(nvlr, "qname", r->qname);
884 	nvlist_add_string(nvlr, "pqname", r->pqname);
885 	nvlist_add_string(nvlr, "tagname", r->tagname);
886 	nvlist_add_string(nvlr, "match_tagname", r->match_tagname);
887 	nvlist_add_string(nvlr, "overload_tblname", r->overload_tblname);
888 
889 	pfctl_nv_add_pool(nvlr, "rpool", &r->rpool);
890 
891 	nvlist_add_number(nvlr, "os_fingerprint", r->os_fingerprint);
892 
893 	nvlist_add_number(nvlr, "rtableid", r->rtableid);
894 	for (int i = 0; i < PFTM_MAX; i++)
895 		timeouts[i] = r->timeout[i];
896 	nvlist_add_number_array(nvlr, "timeout", timeouts, PFTM_MAX);
897 	nvlist_add_number(nvlr, "max_states", r->max_states);
898 	nvlist_add_number(nvlr, "max_src_nodes", r->max_src_nodes);
899 	nvlist_add_number(nvlr, "max_src_states", r->max_src_states);
900 	nvlist_add_number(nvlr, "max_src_conn", r->max_src_conn);
901 	nvlist_add_number(nvlr, "max_src_conn_rate.limit",
902 	    r->max_src_conn_rate.limit);
903 	nvlist_add_number(nvlr, "max_src_conn_rate.seconds",
904 	    r->max_src_conn_rate.seconds);
905 	nvlist_add_number(nvlr, "dnpipe", r->dnpipe);
906 	nvlist_add_number(nvlr, "dnrpipe", r->dnrpipe);
907 	nvlist_add_number(nvlr, "dnflags", r->free_flags);
908 	nvlist_add_number(nvlr, "prob", r->prob);
909 	nvlist_add_number(nvlr, "cuid", r->cuid);
910 	nvlist_add_number(nvlr, "cpid", r->cpid);
911 
912 	nvlist_add_number(nvlr, "return_icmp", r->return_icmp);
913 	nvlist_add_number(nvlr, "return_icmp6", r->return_icmp6);
914 
915 	nvlist_add_number(nvlr, "max_mss", r->max_mss);
916 	nvlist_add_number(nvlr, "scrub_flags", r->scrub_flags);
917 
918 	pfctl_nv_add_uid(nvlr, "uid", &r->uid);
919 	pfctl_nv_add_uid(nvlr, "gid", (const struct pf_rule_uid *)&r->gid);
920 
921 	nvlist_add_number(nvlr, "rule_flag", r->rule_flag);
922 	nvlist_add_number(nvlr, "action", r->action);
923 	nvlist_add_number(nvlr, "direction", r->direction);
924 	nvlist_add_number(nvlr, "log", r->log);
925 	nvlist_add_number(nvlr, "logif", r->logif);
926 	nvlist_add_number(nvlr, "quick", r->quick);
927 	nvlist_add_number(nvlr, "ifnot", r->ifnot);
928 	nvlist_add_number(nvlr, "match_tag_not", r->match_tag_not);
929 	nvlist_add_number(nvlr, "natpass", r->natpass);
930 
931 	nvlist_add_number(nvlr, "keep_state", r->keep_state);
932 	nvlist_add_number(nvlr, "af", r->af);
933 	nvlist_add_number(nvlr, "proto", r->proto);
934 	nvlist_add_number(nvlr, "type", r->type);
935 	nvlist_add_number(nvlr, "code", r->code);
936 	nvlist_add_number(nvlr, "flags", r->flags);
937 	nvlist_add_number(nvlr, "flagset", r->flagset);
938 	nvlist_add_number(nvlr, "min_ttl", r->min_ttl);
939 	nvlist_add_number(nvlr, "allow_opts", r->allow_opts);
940 	nvlist_add_number(nvlr, "rt", r->rt);
941 	nvlist_add_number(nvlr, "return_ttl", r->return_ttl);
942 	nvlist_add_number(nvlr, "tos", r->tos);
943 	nvlist_add_number(nvlr, "set_tos", r->set_tos);
944 	nvlist_add_number(nvlr, "anchor_relative", r->anchor_relative);
945 	nvlist_add_number(nvlr, "anchor_wildcard", r->anchor_wildcard);
946 
947 	nvlist_add_number(nvlr, "flush", r->flush);
948 
949 	nvlist_add_number(nvlr, "prio", r->prio);
950 	set_prio[0] = r->set_prio[0];
951 	set_prio[1] = r->set_prio[1];
952 	nvlist_add_number_array(nvlr, "set_prio", set_prio, 2);
953 
954 	pfctl_nv_add_divert(nvlr, "divert", r);
955 
956 	nvlist_add_nvlist(nvl, "rule", nvlr);
957 	nvlist_destroy(nvlr);
958 
959 	/* Now do the call. */
960 	nv.data = nvlist_pack(nvl, &nv.len);
961 	nv.size = nv.len;
962 
963 	ret = ioctl(dev, DIOCADDRULENV, &nv);
964 	if (ret == -1)
965 		ret = errno;
966 
967 	free(nv.data);
968 	nvlist_destroy(nvl);
969 
970 	return (ret);
971 }
972 
973 int
974 pfctl_get_rules_info(int dev, struct pfctl_rules_info *rules, uint32_t ruleset,
975     const char *path)
976 {
977 	struct pfioc_rule pr;
978 	int ret;
979 
980 	bzero(&pr, sizeof(pr));
981 	if (strlcpy(pr.anchor, path, sizeof(pr.anchor)) >= sizeof(pr.anchor))
982 		return (E2BIG);
983 
984 	pr.rule.action = ruleset;
985 	ret = ioctl(dev, DIOCGETRULES, &pr);
986 	if (ret != 0)
987 		return (ret);
988 
989 	rules->nr = pr.nr;
990 	rules->ticket = pr.ticket;
991 
992 	return (0);
993 }
994 
995 int
996 pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket, const char *anchor,
997     uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
998 {
999 	return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule,
1000 	    anchor_call, false));
1001 }
1002 
1003 int	pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket,
1004 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
1005 	    char *anchor_call, bool clear)
1006 {
1007 	struct pfioc_nv nv;
1008 	nvlist_t *nvl;
1009 	void *nvlpacked;
1010 	int ret;
1011 
1012 	nvl = nvlist_create(0);
1013 	if (nvl == 0)
1014 		return (ENOMEM);
1015 
1016 	nvlist_add_number(nvl, "nr", nr);
1017 	nvlist_add_number(nvl, "ticket", ticket);
1018 	nvlist_add_string(nvl, "anchor", anchor);
1019 	nvlist_add_number(nvl, "ruleset", ruleset);
1020 
1021 	if (clear)
1022 		nvlist_add_bool(nvl, "clear_counter", true);
1023 
1024 	nvlpacked = nvlist_pack(nvl, &nv.len);
1025 	if (nvlpacked == NULL) {
1026 		nvlist_destroy(nvl);
1027 		return (ENOMEM);
1028 	}
1029 	nv.data = malloc(8182);
1030 	nv.size = 8192;
1031 	assert(nv.len <= nv.size);
1032 	memcpy(nv.data, nvlpacked, nv.len);
1033 	nvlist_destroy(nvl);
1034 	nvl = NULL;
1035 	free(nvlpacked);
1036 
1037 	ret = ioctl(dev, DIOCGETRULENV, &nv);
1038 	if (ret != 0) {
1039 		free(nv.data);
1040 		return (ret);
1041 	}
1042 
1043 	nvl = nvlist_unpack(nv.data, nv.len, 0);
1044 	if (nvl == NULL) {
1045 		free(nv.data);
1046 		return (EIO);
1047 	}
1048 
1049 	pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule);
1050 
1051 	if (anchor_call)
1052 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
1053 		    MAXPATHLEN);
1054 
1055 	free(nv.data);
1056 	nvlist_destroy(nvl);
1057 
1058 	return (0);
1059 }
1060 
1061 int
1062 pfctl_set_keepcounters(int dev, bool keep)
1063 {
1064 	struct pfioc_nv	 nv;
1065 	nvlist_t	*nvl;
1066 	int		 ret;
1067 
1068 	nvl = nvlist_create(0);
1069 
1070 	nvlist_add_bool(nvl, "keep_counters", keep);
1071 
1072 	nv.data = nvlist_pack(nvl, &nv.len);
1073 	nv.size = nv.len;
1074 
1075 	nvlist_destroy(nvl);
1076 
1077 	ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv);
1078 
1079 	free(nv.data);
1080 	return (ret);
1081 }
1082 
1083 static void
1084 pfctl_nv_add_state_cmp(nvlist_t *nvl, const char *name,
1085     const struct pfctl_state_cmp *cmp)
1086 {
1087 	nvlist_t	*nv;
1088 
1089 	nv = nvlist_create(0);
1090 
1091 	nvlist_add_number(nv, "id", cmp->id);
1092 	nvlist_add_number(nv, "creatorid", htonl(cmp->creatorid));
1093 	nvlist_add_number(nv, "direction", cmp->direction);
1094 
1095 	nvlist_add_nvlist(nvl, name, nv);
1096 	nvlist_destroy(nv);
1097 }
1098 
1099 static void
1100 pf_state_key_export_to_state_key(struct pfctl_state_key *ps,
1101     const struct pf_state_key_export *s)
1102 {
1103 	bcopy(s->addr, ps->addr, sizeof(ps->addr[0]) * 2);
1104 	ps->port[0] = s->port[0];
1105 	ps->port[1] = s->port[1];
1106 }
1107 
1108 static void
1109 pf_state_peer_export_to_state_peer(struct pfctl_state_peer *ps,
1110     const struct pf_state_peer_export *s)
1111 {
1112 	/* Ignore scrub. */
1113 	ps->seqlo = s->seqlo;
1114 	ps->seqhi = s->seqhi;
1115 	ps->seqdiff = s->seqdiff;
1116 	/* Ignore max_win & mss */
1117 	ps->state = s->state;
1118 	ps->wscale = s->wscale;
1119 }
1120 
1121 static void
1122 pf_state_export_to_state(struct pfctl_state *ps, const struct pf_state_export *s)
1123 {
1124 	assert(s->version >= PF_STATE_VERSION);
1125 
1126 	ps->id = s->id;
1127 	strlcpy(ps->ifname, s->ifname, sizeof(ps->ifname));
1128 	strlcpy(ps->orig_ifname, s->orig_ifname, sizeof(ps->orig_ifname));
1129 	pf_state_key_export_to_state_key(&ps->key[0], &s->key[0]);
1130 	pf_state_key_export_to_state_key(&ps->key[1], &s->key[1]);
1131 	pf_state_peer_export_to_state_peer(&ps->src, &s->src);
1132 	pf_state_peer_export_to_state_peer(&ps->dst, &s->dst);
1133 	bcopy(&s->rt_addr, &ps->rt_addr, sizeof(ps->rt_addr));
1134 	ps->rule = ntohl(s->rule);
1135 	ps->anchor = ntohl(s->anchor);
1136 	ps->nat_rule = ntohl(s->nat_rule);
1137 	ps->creation = ntohl(s->creation);
1138 	ps->expire = ntohl(s->expire);
1139 	ps->packets[0] = s->packets[0];
1140 	ps->packets[1] = s->packets[1];
1141 	ps->bytes[0] = s->bytes[0];
1142 	ps->bytes[1] = s->bytes[1];
1143 	ps->creatorid = ntohl(s->creatorid);
1144 	ps->key[0].proto = s->proto;
1145 	ps->key[1].proto = s->proto;
1146 	ps->key[0].af = s->af;
1147 	ps->key[1].af = s->af;
1148 	ps->direction = s->direction;
1149 	ps->state_flags = s->state_flags;
1150 	ps->sync_flags = s->sync_flags;
1151 }
1152 
1153 int
1154 pfctl_get_states(int dev, struct pfctl_states *states)
1155 {
1156 	struct pfioc_states_v2 ps;
1157 	struct pf_state_export *p;
1158 	char *inbuf = NULL, *newinbuf = NULL;
1159 	unsigned int len = 0;
1160 	int i, error;
1161 
1162 	bzero(&ps, sizeof(ps));
1163 	ps.ps_req_version = PF_STATE_VERSION;
1164 
1165 	bzero(states, sizeof(*states));
1166 	TAILQ_INIT(&states->states);
1167 
1168 	for (;;) {
1169 		ps.ps_len = len;
1170 		if (len) {
1171 			newinbuf = realloc(inbuf, len);
1172 			if (newinbuf == NULL)
1173 				return (ENOMEM);
1174 			ps.ps_buf = inbuf = newinbuf;
1175 		}
1176 		if ((error = ioctl(dev, DIOCGETSTATESV2, &ps)) < 0) {
1177 			free(inbuf);
1178 			return (error);
1179 		}
1180 		if (ps.ps_len + sizeof(struct pfioc_states_v2) < len)
1181 			break;
1182 		if (len == 0 && ps.ps_len == 0)
1183 			goto out;
1184 		if (len == 0 && ps.ps_len != 0)
1185 			len = ps.ps_len;
1186 		if (ps.ps_len == 0)
1187 			goto out;      /* no states */
1188 		len *= 2;
1189 	}
1190 	p = ps.ps_states;
1191 
1192 	for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
1193 		struct pfctl_state *s = malloc(sizeof(*s));
1194 		if (s == NULL) {
1195 			pfctl_free_states(states);
1196 			error = ENOMEM;
1197 			goto out;
1198 		}
1199 
1200 		pf_state_export_to_state(s, p);
1201 		TAILQ_INSERT_TAIL(&states->states, s, entry);
1202 	}
1203 
1204 out:
1205 	free(inbuf);
1206 	return (error);
1207 }
1208 
1209 void
1210 pfctl_free_states(struct pfctl_states *states)
1211 {
1212 	struct pfctl_state *s, *tmp;
1213 
1214 	TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) {
1215 		free(s);
1216 	}
1217 
1218 	bzero(states, sizeof(*states));
1219 }
1220 
1221 static int
1222 _pfctl_clear_states(int dev, const struct pfctl_kill *kill,
1223     unsigned int *killed, uint64_t ioctlval)
1224 {
1225 	struct pfioc_nv	 nv;
1226 	nvlist_t	*nvl;
1227 	int		 ret;
1228 
1229 	nvl = nvlist_create(0);
1230 
1231 	pfctl_nv_add_state_cmp(nvl, "cmp", &kill->cmp);
1232 	nvlist_add_number(nvl, "af", kill->af);
1233 	nvlist_add_number(nvl, "proto", kill->proto);
1234 	pfctl_nv_add_rule_addr(nvl, "src", &kill->src);
1235 	pfctl_nv_add_rule_addr(nvl, "dst", &kill->dst);
1236 	pfctl_nv_add_rule_addr(nvl, "rt_addr", &kill->rt_addr);
1237 	nvlist_add_string(nvl, "ifname", kill->ifname);
1238 	nvlist_add_string(nvl, "label", kill->label);
1239 	nvlist_add_bool(nvl, "kill_match", kill->kill_match);
1240 
1241 	nv.data = nvlist_pack(nvl, &nv.len);
1242 	nv.size = nv.len;
1243 	nvlist_destroy(nvl);
1244 	nvl = NULL;
1245 
1246 	ret = ioctl(dev, ioctlval, &nv);
1247 	if (ret != 0) {
1248 		free(nv.data);
1249 		return (ret);
1250 	}
1251 
1252 	nvl = nvlist_unpack(nv.data, nv.len, 0);
1253 	if (nvl == NULL) {
1254 		free(nv.data);
1255 		return (EIO);
1256 	}
1257 
1258 	if (killed)
1259 		*killed = nvlist_get_number(nvl, "killed");
1260 
1261 	nvlist_destroy(nvl);
1262 	free(nv.data);
1263 
1264 	return (ret);
1265 }
1266 
1267 int
1268 pfctl_clear_states(int dev, const struct pfctl_kill *kill,
1269     unsigned int *killed)
1270 {
1271 	return (_pfctl_clear_states(dev, kill, killed, DIOCCLRSTATESNV));
1272 }
1273 
1274 int
1275 pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed)
1276 {
1277 	return (_pfctl_clear_states(dev, kill, killed, DIOCKILLSTATESNV));
1278 }
1279 
1280 int
1281 pfctl_clear_rules(int dev, const char *anchorname)
1282 {
1283 	struct pfioc_trans trans;
1284 	struct pfioc_trans_e transe[2];
1285 	int ret;
1286 
1287 	bzero(&trans, sizeof(trans));
1288 	bzero(&transe, sizeof(transe));
1289 
1290 	transe[0].rs_num = PF_RULESET_SCRUB;
1291 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
1292 	    >= sizeof(transe[0].anchor))
1293 		return (E2BIG);
1294 
1295 	transe[1].rs_num = PF_RULESET_FILTER;
1296 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
1297 	    >= sizeof(transe[1].anchor))
1298 		return (E2BIG);
1299 
1300 	trans.size = 2;
1301 	trans.esize = sizeof(transe[0]);
1302 	trans.array = transe;
1303 
1304 	ret = ioctl(dev, DIOCXBEGIN, &trans);
1305 	if (ret != 0)
1306 		return (ret);
1307 	return ioctl(dev, DIOCXCOMMIT, &trans);
1308 }
1309 
1310 int
1311 pfctl_clear_nat(int dev, const char *anchorname)
1312 {
1313 	struct pfioc_trans trans;
1314 	struct pfioc_trans_e transe[3];
1315 	int ret;
1316 
1317 	bzero(&trans, sizeof(trans));
1318 	bzero(&transe, sizeof(transe));
1319 
1320 	transe[0].rs_num = PF_RULESET_NAT;
1321 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
1322 	    >= sizeof(transe[0].anchor))
1323 		return (E2BIG);
1324 
1325 	transe[1].rs_num = PF_RULESET_BINAT;
1326 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
1327 	    >= sizeof(transe[0].anchor))
1328 		return (E2BIG);
1329 
1330 	transe[2].rs_num = PF_RULESET_RDR;
1331 	if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor))
1332 	    >= sizeof(transe[2].anchor))
1333 		return (E2BIG);
1334 
1335 	trans.size = 3;
1336 	trans.esize = sizeof(transe[0]);
1337 	trans.array = transe;
1338 
1339 	ret = ioctl(dev, DIOCXBEGIN, &trans);
1340 	if (ret != 0)
1341 		return (ret);
1342 	return ioctl(dev, DIOCXCOMMIT, &trans);
1343 }
1344 int
1345 pfctl_clear_eth_rules(int dev, const char *anchorname)
1346 {
1347 	struct pfioc_trans trans;
1348 	struct pfioc_trans_e transe;
1349 	int ret;
1350 
1351 	bzero(&trans, sizeof(trans));
1352 	bzero(&transe, sizeof(transe));
1353 
1354 	transe.rs_num = PF_RULESET_ETH;
1355 	if (strlcpy(transe.anchor, anchorname, sizeof(transe.anchor))
1356 	    >= sizeof(transe.anchor))
1357 		return (E2BIG);
1358 
1359 	trans.size = 1;
1360 	trans.esize = sizeof(transe);
1361 	trans.array = &transe;
1362 
1363 	ret = ioctl(dev, DIOCXBEGIN, &trans);
1364 	if (ret != 0)
1365 		return (ret);
1366 	return ioctl(dev, DIOCXCOMMIT, &trans);
1367 }
1368 
1369 static int
1370 pfctl_get_limit(int dev, const int index, uint *limit)
1371 {
1372 	struct pfioc_limit pl;
1373 
1374 	bzero(&pl, sizeof(pl));
1375 	pl.index = index;
1376 
1377 	if (ioctl(dev, DIOCGETLIMIT, &pl) == -1)
1378 		return (errno);
1379 
1380 	*limit = pl.limit;
1381 
1382 	return (0);
1383 }
1384 
1385 int
1386 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s)
1387 {
1388 	struct pfioc_nv	 nv;
1389 	nvlist_t	*nvl;
1390 	int		 ret;
1391 	uint		 state_limit;
1392 
1393 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
1394 	if (ret != 0)
1395 		return (ret);
1396 
1397 	nvl = nvlist_create(0);
1398 
1399 	nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER);
1400 	nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE);
1401 	nvlist_add_number(nvl, "highwater", state_limit * s->highwater / 100);
1402 	nvlist_add_number(nvl, "lowwater", state_limit * s->lowwater / 100);
1403 
1404 	nv.data = nvlist_pack(nvl, &nv.len);
1405 	nv.size = nv.len;
1406 	nvlist_destroy(nvl);
1407 	nvl = NULL;
1408 
1409 	ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv);
1410 
1411 	free(nv.data);
1412 	return (ret);
1413 }
1414 
1415 int
1416 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s)
1417 {
1418 	struct pfioc_nv	 nv;
1419 	nvlist_t	*nvl;
1420 	int		 ret;
1421 	uint		 state_limit;
1422 	bool		 enabled, adaptive;
1423 
1424 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
1425 	if (ret != 0)
1426 		return (ret);
1427 
1428 	bzero(s, sizeof(*s));
1429 
1430 	nv.data = malloc(256);
1431 	nv.len = nv.size = 256;
1432 
1433 	if (ioctl(dev, DIOCGETSYNCOOKIES, &nv)) {
1434 		free(nv.data);
1435 		return (errno);
1436 	}
1437 
1438 	nvl = nvlist_unpack(nv.data, nv.len, 0);
1439 	free(nv.data);
1440 	if (nvl == NULL) {
1441 		return (EIO);
1442 	}
1443 
1444 	enabled = nvlist_get_bool(nvl, "enabled");
1445 	adaptive = nvlist_get_bool(nvl, "adaptive");
1446 
1447 	if (enabled) {
1448 		if (adaptive)
1449 			s->mode = PFCTL_SYNCOOKIES_ADAPTIVE;
1450 		else
1451 			s->mode = PFCTL_SYNCOOKIES_ALWAYS;
1452 	} else {
1453 		s->mode = PFCTL_SYNCOOKIES_NEVER;
1454 	}
1455 
1456 	s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit;
1457 	s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit;
1458 
1459 	nvlist_destroy(nvl);
1460 
1461 	return (0);
1462 }
1463 
1464 int
1465 pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
1466     *addr, int size, int *nadd, int flags)
1467 {
1468 	struct pfioc_table io;
1469 
1470 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
1471 		return (EINVAL);
1472 	}
1473 	bzero(&io, sizeof io);
1474 	io.pfrio_flags = flags;
1475 	io.pfrio_table = *tbl;
1476 	io.pfrio_buffer = addr;
1477 	io.pfrio_esize = sizeof(*addr);
1478 	io.pfrio_size = size;
1479 
1480 	if (ioctl(dev, DIOCRADDADDRS, &io))
1481 		return (errno);
1482 	if (nadd != NULL)
1483 		*nadd = io.pfrio_nadd;
1484 	return (0);
1485 }
1486 
1487 int
1488 pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
1489     *addr, int size, int *ndel, int flags)
1490 {
1491 	struct pfioc_table io;
1492 
1493 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
1494 		return (EINVAL);
1495 	}
1496 	bzero(&io, sizeof io);
1497 	io.pfrio_flags = flags;
1498 	io.pfrio_table = *tbl;
1499 	io.pfrio_buffer = addr;
1500 	io.pfrio_esize = sizeof(*addr);
1501 	io.pfrio_size = size;
1502 
1503 	if (ioctl(dev, DIOCRDELADDRS, &io))
1504 		return (errno);
1505 	if (ndel != NULL)
1506 		*ndel = io.pfrio_ndel;
1507 	return (0);
1508 }
1509 
1510 int
1511 pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
1512     *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags)
1513 {
1514 	struct pfioc_table io;
1515 
1516 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
1517 		return (EINVAL);
1518 	}
1519 	bzero(&io, sizeof io);
1520 	io.pfrio_flags = flags;
1521 	io.pfrio_table = *tbl;
1522 	io.pfrio_buffer = addr;
1523 	io.pfrio_esize = sizeof(*addr);
1524 	io.pfrio_size = size;
1525 	io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
1526 	if (ioctl(dev, DIOCRSETADDRS, &io))
1527 		return (-1);
1528 	if (nadd != NULL)
1529 		*nadd = io.pfrio_nadd;
1530 	if (ndel != NULL)
1531 		*ndel = io.pfrio_ndel;
1532 	if (nchange != NULL)
1533 		*nchange = io.pfrio_nchange;
1534 	if (size2 != NULL)
1535 		*size2 = io.pfrio_size2;
1536 	return (0);
1537 }
1538 
1539 int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr,
1540     int *size, int flags)
1541 {
1542 	struct pfioc_table io;
1543 
1544 	if (tbl == NULL || size == NULL || *size < 0 ||
1545 	    (*size && addr == NULL)) {
1546 		return (EINVAL);
1547 	}
1548 	bzero(&io, sizeof io);
1549 	io.pfrio_flags = flags;
1550 	io.pfrio_table = *tbl;
1551 	io.pfrio_buffer = addr;
1552 	io.pfrio_esize = sizeof(*addr);
1553 	io.pfrio_size = *size;
1554 	if (ioctl(dev, DIOCRGETADDRS, &io))
1555 		return (-1);
1556 	*size = io.pfrio_size;
1557 	return (0);
1558 }
1559