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