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