xref: /freebsd/lib/libpfctl/libpfctl.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #include <sys/cdefs.h>
35 
36 #include <sys/ioctl.h>
37 #include <sys/nv.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 
41 #include <net/if.h>
42 #include <net/pfvar.h>
43 #include <netinet/in.h>
44 
45 #include <assert.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "libpfctl.h"
52 
53 const char* PFCTL_SYNCOOKIES_MODE_NAMES[] = {
54 	"never",
55 	"always",
56 	"adaptive"
57 };
58 
59 static int	_pfctl_clear_states(int , const struct pfctl_kill *,
60 		    unsigned int *, uint64_t);
61 
62 static void
63 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems,
64     uint8_t *numbers, size_t *nelems)
65 {
66 	const uint64_t *tmp;
67 	size_t elems;
68 
69 	tmp = nvlist_get_number_array(nvl, name, &elems);
70 	assert(elems <= maxelems);
71 
72 	for (size_t i = 0; i < elems; i++)
73 		numbers[i] = tmp[i];
74 
75 	if (nelems)
76 		*nelems = elems;
77 }
78 
79 static void
80 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems,
81     uint16_t *numbers, size_t *nelems)
82 {
83 	const uint64_t *tmp;
84 	size_t elems;
85 
86 	tmp = nvlist_get_number_array(nvl, name, &elems);
87 	assert(elems <= maxelems);
88 
89 	for (size_t i = 0; i < elems; i++)
90 		numbers[i] = tmp[i];
91 
92 	if (nelems)
93 		*nelems = elems;
94 }
95 
96 static void
97 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems,
98     uint32_t *numbers, size_t *nelems)
99 {
100 	const uint64_t *tmp;
101 	size_t elems;
102 
103 	tmp = nvlist_get_number_array(nvl, name, &elems);
104 	assert(elems <= maxelems);
105 
106 	for (size_t i = 0; i < elems; i++)
107 		numbers[i] = tmp[i];
108 
109 	if (nelems)
110 		*nelems = elems;
111 }
112 
113 static void
114 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems,
115     uint64_t *numbers, size_t *nelems)
116 {
117 	const uint64_t *tmp;
118 	size_t elems;
119 
120 	tmp = nvlist_get_number_array(nvl, name, &elems);
121 	assert(elems <= maxelems);
122 
123 	for (size_t i = 0; i < elems; i++)
124 		numbers[i] = tmp[i];
125 
126 	if (nelems)
127 		*nelems = elems;
128 }
129 
130 static void
131 _pfctl_get_status_counters(const nvlist_t *nvl,
132     struct pfctl_status_counters *counters)
133 {
134 	const uint64_t		*ids, *counts;
135 	const char *const	*names;
136 	size_t id_len, counter_len, names_len;
137 
138 	ids = nvlist_get_number_array(nvl, "ids", &id_len);
139 	counts = nvlist_get_number_array(nvl, "counters", &counter_len);
140 	names = nvlist_get_string_array(nvl, "names", &names_len);
141 	assert(id_len == counter_len);
142 	assert(counter_len == names_len);
143 
144 	TAILQ_INIT(counters);
145 
146 	for (size_t i = 0; i < id_len; i++) {
147 		struct pfctl_status_counter *c;
148 
149 		c = malloc(sizeof(*c));
150 
151 		c->id = ids[i];
152 		c->counter = counts[i];
153 		c->name = strdup(names[i]);
154 
155 		TAILQ_INSERT_TAIL(counters, c, entry);
156 	}
157 }
158 
159 struct pfctl_status *
160 pfctl_get_status(int dev)
161 {
162 	struct pfioc_nv	 nv;
163 	struct pfctl_status	*status;
164 	nvlist_t	*nvl;
165 	size_t		 len;
166 	const void	*chksum;
167 
168 	status = calloc(1, sizeof(*status));
169 	if (status == NULL)
170 		return (NULL);
171 
172 	nv.data = malloc(4096);
173 	nv.len = nv.size = 4096;
174 
175 	if (ioctl(dev, DIOCGETSTATUSNV, &nv)) {
176 		free(nv.data);
177 		free(status);
178 		return (NULL);
179 	}
180 
181 	nvl = nvlist_unpack(nv.data, nv.len, 0);
182 	free(nv.data);
183 	if (nvl == NULL) {
184 		free(status);
185 		return (NULL);
186 	}
187 
188 	status->running = nvlist_get_bool(nvl, "running");
189 	status->since = nvlist_get_number(nvl, "since");
190 	status->debug = nvlist_get_number(nvl, "debug");
191 	status->hostid = nvlist_get_number(nvl, "hostid");
192 	status->states = nvlist_get_number(nvl, "states");
193 	status->src_nodes = nvlist_get_number(nvl, "src_nodes");
194 
195 	strlcpy(status->ifname, nvlist_get_string(nvl, "ifname"),
196 	    IFNAMSIZ);
197 	chksum = nvlist_get_binary(nvl, "chksum", &len);
198 	assert(len == PF_MD5_DIGEST_LENGTH);
199 	memcpy(status->pf_chksum, chksum, len);
200 
201 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "counters"),
202 	    &status->counters);
203 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "lcounters"),
204 	    &status->lcounters);
205 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "fcounters"),
206 	    &status->fcounters);
207 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"),
208 	    &status->scounters);
209 
210 	pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 3,
211 	    (uint64_t *)status->pcounters, NULL);
212 	pf_nvuint_64_array(nvl, "bcounters", 2 * 2,
213 	    (uint64_t *)status->bcounters, NULL);
214 
215 	nvlist_destroy(nvl);
216 
217 	return (status);
218 }
219 
220 void
221 pfctl_free_status(struct pfctl_status *status)
222 {
223 	struct pfctl_status_counter *c, *tmp;
224 
225 	TAILQ_FOREACH_SAFE(c, &status->counters, entry, tmp) {
226 		free(c->name);
227 		free(c);
228 	}
229 	TAILQ_FOREACH_SAFE(c, &status->lcounters, entry, tmp) {
230 		free(c->name);
231 		free(c);
232 	}
233 	TAILQ_FOREACH_SAFE(c, &status->fcounters, entry, tmp) {
234 		free(c->name);
235 		free(c);
236 	}
237 	TAILQ_FOREACH_SAFE(c, &status->scounters, entry, tmp) {
238 		free(c->name);
239 		free(c);
240 	}
241 
242 	free(status);
243 }
244 
245 static void
246 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name,
247     const struct pf_addr *addr)
248 {
249 	nvlist_t *nvl = nvlist_create(0);
250 
251 	nvlist_add_binary(nvl, "addr", addr, sizeof(*addr));
252 
253 	nvlist_add_nvlist(nvparent, name, nvl);
254 	nvlist_destroy(nvl);
255 }
256 
257 static void
258 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr)
259 {
260 	size_t len;
261 	const void *data;
262 
263 	data = nvlist_get_binary(nvl, "addr", &len);
264 	assert(len == sizeof(struct pf_addr));
265 	memcpy(addr, data, len);
266 }
267 
268 static void
269 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name,
270     const struct pf_addr_wrap *addr)
271 {
272 	nvlist_t *nvl = nvlist_create(0);
273 
274 	nvlist_add_number(nvl, "type", addr->type);
275 	nvlist_add_number(nvl, "iflags", addr->iflags);
276 	if (addr->type == PF_ADDR_DYNIFTL)
277 		nvlist_add_string(nvl, "ifname", addr->v.ifname);
278 	if (addr->type == PF_ADDR_TABLE)
279 		nvlist_add_string(nvl, "tblname", addr->v.tblname);
280 	pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr);
281 	pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask);
282 
283 	nvlist_add_nvlist(nvparent, name, nvl);
284 	nvlist_destroy(nvl);
285 }
286 
287 static void
288 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
289 {
290 	bzero(addr, sizeof(*addr));
291 
292 	addr->type = nvlist_get_number(nvl, "type");
293 	addr->iflags = nvlist_get_number(nvl, "iflags");
294 	if (addr->type == PF_ADDR_DYNIFTL) {
295 		strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"),
296 		    IFNAMSIZ);
297 		addr->p.dyncnt = nvlist_get_number(nvl, "dyncnt");
298 	}
299 	if (addr->type == PF_ADDR_TABLE) {
300 		strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"),
301 		    PF_TABLE_NAME_SIZE);
302 		addr->p.tblcnt = nvlist_get_number(nvl, "tblcnt");
303 	}
304 
305 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr);
306 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask);
307 }
308 
309 static void
310 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name,
311     const struct pf_rule_addr *addr)
312 {
313 	uint64_t ports[2];
314 	nvlist_t *nvl = nvlist_create(0);
315 
316 	pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr);
317 	ports[0] = addr->port[0];
318 	ports[1] = addr->port[1];
319 	nvlist_add_number_array(nvl, "port", ports, 2);
320 	nvlist_add_number(nvl, "neg", addr->neg);
321 	nvlist_add_number(nvl, "port_op", addr->port_op);
322 
323 	nvlist_add_nvlist(nvparent, name, nvl);
324 	nvlist_destroy(nvl);
325 }
326 
327 static void
328 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
329 {
330 	pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr);
331 
332 	pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL);
333 	addr->neg = nvlist_get_number(nvl, "neg");
334 	addr->port_op = nvlist_get_number(nvl, "port_op");
335 }
336 
337 static void
338 pfctl_nv_add_mape(nvlist_t *nvparent, const char *name,
339     const struct pf_mape_portset *mape)
340 {
341 	nvlist_t *nvl = nvlist_create(0);
342 
343 	nvlist_add_number(nvl, "offset", mape->offset);
344 	nvlist_add_number(nvl, "psidlen", mape->psidlen);
345 	nvlist_add_number(nvl, "psid", mape->psid);
346 	nvlist_add_nvlist(nvparent, name, nvl);
347 	nvlist_destroy(nvl);
348 }
349 
350 static void
351 pfctl_nv_add_pool(nvlist_t *nvparent, const char *name,
352     const struct pfctl_pool *pool)
353 {
354 	uint64_t ports[2];
355 	nvlist_t *nvl = nvlist_create(0);
356 
357 	nvlist_add_binary(nvl, "key", &pool->key, sizeof(pool->key));
358 	pfctl_nv_add_addr(nvl, "counter", &pool->counter);
359 	nvlist_add_number(nvl, "tblidx", pool->tblidx);
360 
361 	ports[0] = pool->proxy_port[0];
362 	ports[1] = pool->proxy_port[1];
363 	nvlist_add_number_array(nvl, "proxy_port", ports, 2);
364 	nvlist_add_number(nvl, "opts", pool->opts);
365 	pfctl_nv_add_mape(nvl, "mape", &pool->mape);
366 
367 	nvlist_add_nvlist(nvparent, name, nvl);
368 	nvlist_destroy(nvl);
369 }
370 
371 static void
372 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape)
373 {
374 	mape->offset = nvlist_get_number(nvl, "offset");
375 	mape->psidlen = nvlist_get_number(nvl, "psidlen");
376 	mape->psid = nvlist_get_number(nvl, "psid");
377 }
378 
379 static void
380 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool)
381 {
382 	size_t len;
383 	const void *data;
384 
385 	data = nvlist_get_binary(nvl, "key", &len);
386 	assert(len == sizeof(pool->key));
387 	memcpy(&pool->key, data, len);
388 
389 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter);
390 
391 	pool->tblidx = nvlist_get_number(nvl, "tblidx");
392 	pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL);
393 	pool->opts = nvlist_get_number(nvl, "opts");
394 
395 	if (nvlist_exists_nvlist(nvl, "mape"))
396 		pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape);
397 }
398 
399 static void
400 pfctl_nv_add_uid(nvlist_t *nvparent, const char *name,
401     const struct pf_rule_uid *uid)
402 {
403 	uint64_t uids[2];
404 	nvlist_t *nvl = nvlist_create(0);
405 
406 	uids[0] = uid->uid[0];
407 	uids[1] = uid->uid[1];
408 	nvlist_add_number_array(nvl, "uid", uids, 2);
409 	nvlist_add_number(nvl, "op", uid->op);
410 
411 	nvlist_add_nvlist(nvparent, name, nvl);
412 	nvlist_destroy(nvl);
413 }
414 
415 static void
416 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
417 {
418 	pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL);
419 	uid->op = nvlist_get_number(nvl, "op");
420 }
421 
422 static void
423 pfctl_nv_add_divert(nvlist_t *nvparent, const char *name,
424     const struct pfctl_rule *r)
425 {
426 	nvlist_t *nvl = nvlist_create(0);
427 
428 	pfctl_nv_add_addr(nvl, "addr", &r->divert.addr);
429 	nvlist_add_number(nvl, "port", r->divert.port);
430 
431 	nvlist_add_nvlist(nvparent, name, nvl);
432 	nvlist_destroy(nvl);
433 }
434 
435 static void
436 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule)
437 {
438 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr);
439 	rule->divert.port = nvlist_get_number(nvl, "port");
440 }
441 
442 static void
443 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule)
444 {
445 	const uint64_t *skip;
446 	const char *const *labels;
447 	size_t skipcount, labelcount;
448 
449 	rule->nr = nvlist_get_number(nvl, "nr");
450 
451 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src);
452 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst);
453 
454 	skip = nvlist_get_number_array(nvl, "skip", &skipcount);
455 	assert(skip);
456 	assert(skipcount == PF_SKIP_COUNT);
457 	for (int i = 0; i < PF_SKIP_COUNT; i++)
458 		rule->skip[i].nr = skip[i];
459 
460 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
461 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
462 	for (size_t i = 0; i < labelcount; i++)
463 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
464 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
465 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
466 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
467 	strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE);
468 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
469 	    PF_TAG_NAME_SIZE);
470 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
471 	    PF_TAG_NAME_SIZE);
472 
473 	strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"),
474 	    PF_TABLE_NAME_SIZE);
475 
476 	pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool);
477 
478 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
479 	pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL);
480 	pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL);
481 
482 	rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint");
483 
484 	rule->rtableid = nvlist_get_number(nvl, "rtableid");
485 	pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL);
486 	rule->max_states = nvlist_get_number(nvl, "max_states");
487 	rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes");
488 	rule->max_src_states = nvlist_get_number(nvl, "max_src_states");
489 	rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn");
490 	rule->max_src_conn_rate.limit =
491 	    nvlist_get_number(nvl, "max_src_conn_rate.limit");
492 	rule->max_src_conn_rate.seconds =
493 	    nvlist_get_number(nvl, "max_src_conn_rate.seconds");
494 	rule->qid = nvlist_get_number(nvl, "qid");
495 	rule->pqid = nvlist_get_number(nvl, "pqid");
496 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
497 	rule->dnrpipe = nvlist_get_number(nvl, "dnrpipe");
498 	rule->free_flags = nvlist_get_number(nvl, "dnflags");
499 	rule->prob = nvlist_get_number(nvl, "prob");
500 	rule->cuid = nvlist_get_number(nvl, "cuid");
501 	rule->cpid = nvlist_get_number(nvl, "cpid");
502 
503 	rule->return_icmp = nvlist_get_number(nvl, "return_icmp");
504 	rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6");
505 	rule->max_mss = nvlist_get_number(nvl, "max_mss");
506 	rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags");
507 
508 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid);
509 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"),
510 	    (struct pf_rule_uid *)&rule->gid);
511 
512 	rule->rule_flag = nvlist_get_number(nvl, "rule_flag");
513 	rule->action = nvlist_get_number(nvl, "action");
514 	rule->direction = nvlist_get_number(nvl, "direction");
515 	rule->log = nvlist_get_number(nvl, "log");
516 	rule->logif = nvlist_get_number(nvl, "logif");
517 	rule->quick = nvlist_get_number(nvl, "quick");
518 	rule->ifnot = nvlist_get_number(nvl, "ifnot");
519 	rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not");
520 	rule->natpass = nvlist_get_number(nvl, "natpass");
521 
522 	rule->keep_state = nvlist_get_number(nvl, "keep_state");
523 	rule->af = nvlist_get_number(nvl, "af");
524 	rule->proto = nvlist_get_number(nvl, "proto");
525 	rule->type = nvlist_get_number(nvl, "type");
526 	rule->code = nvlist_get_number(nvl, "code");
527 	rule->flags = nvlist_get_number(nvl, "flags");
528 	rule->flagset = nvlist_get_number(nvl, "flagset");
529 	rule->min_ttl = nvlist_get_number(nvl, "min_ttl");
530 	rule->allow_opts = nvlist_get_number(nvl, "allow_opts");
531 	rule->rt = nvlist_get_number(nvl, "rt");
532 	rule->return_ttl  = nvlist_get_number(nvl, "return_ttl");
533 	rule->tos = nvlist_get_number(nvl, "tos");
534 	rule->set_tos = nvlist_get_number(nvl, "set_tos");
535 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
536 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
537 
538 	rule->flush = nvlist_get_number(nvl, "flush");
539 	rule->prio = nvlist_get_number(nvl, "prio");
540 	pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL);
541 
542 	pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule);
543 
544 	rule->states_cur = nvlist_get_number(nvl, "states_cur");
545 	rule->states_tot = nvlist_get_number(nvl, "states_tot");
546 	rule->src_nodes = nvlist_get_number(nvl, "src_nodes");
547 }
548 
549 int
550 pfctl_add_rule(int dev, const struct pfctl_rule *r, const char *anchor,
551     const char *anchor_call, uint32_t ticket, uint32_t pool_ticket)
552 {
553 	struct pfioc_nv nv;
554 	uint64_t timeouts[PFTM_MAX];
555 	uint64_t set_prio[2];
556 	nvlist_t *nvl, *nvlr;
557 	size_t labelcount;
558 	int ret;
559 
560 	nvl = nvlist_create(0);
561 	nvlr = nvlist_create(0);
562 
563 	nvlist_add_number(nvl, "ticket", ticket);
564 	nvlist_add_number(nvl, "pool_ticket", pool_ticket);
565 	nvlist_add_string(nvl, "anchor", anchor);
566 	nvlist_add_string(nvl, "anchor_call", anchor_call);
567 
568 	nvlist_add_number(nvlr, "nr", r->nr);
569 	pfctl_nv_add_rule_addr(nvlr, "src", &r->src);
570 	pfctl_nv_add_rule_addr(nvlr, "dst", &r->dst);
571 
572 	labelcount = 0;
573 	while (r->label[labelcount][0] != 0 &&
574 	    labelcount < PF_RULE_MAX_LABEL_COUNT) {
575 		nvlist_append_string_array(nvlr, "labels",
576 		    r->label[labelcount]);
577 		labelcount++;
578 	}
579 	nvlist_add_number(nvlr, "ridentifier", r->ridentifier);
580 
581 	nvlist_add_string(nvlr, "ifname", r->ifname);
582 	nvlist_add_string(nvlr, "qname", r->qname);
583 	nvlist_add_string(nvlr, "pqname", r->pqname);
584 	nvlist_add_string(nvlr, "tagname", r->tagname);
585 	nvlist_add_string(nvlr, "match_tagname", r->match_tagname);
586 	nvlist_add_string(nvlr, "overload_tblname", r->overload_tblname);
587 
588 	pfctl_nv_add_pool(nvlr, "rpool", &r->rpool);
589 
590 	nvlist_add_number(nvlr, "os_fingerprint", r->os_fingerprint);
591 
592 	nvlist_add_number(nvlr, "rtableid", r->rtableid);
593 	for (int i = 0; i < PFTM_MAX; i++)
594 		timeouts[i] = r->timeout[i];
595 	nvlist_add_number_array(nvlr, "timeout", timeouts, PFTM_MAX);
596 	nvlist_add_number(nvlr, "max_states", r->max_states);
597 	nvlist_add_number(nvlr, "max_src_nodes", r->max_src_nodes);
598 	nvlist_add_number(nvlr, "max_src_states", r->max_src_states);
599 	nvlist_add_number(nvlr, "max_src_conn", r->max_src_conn);
600 	nvlist_add_number(nvlr, "max_src_conn_rate.limit",
601 	    r->max_src_conn_rate.limit);
602 	nvlist_add_number(nvlr, "max_src_conn_rate.seconds",
603 	    r->max_src_conn_rate.seconds);
604 	nvlist_add_number(nvlr, "dnpipe", r->dnpipe);
605 	nvlist_add_number(nvlr, "dnrpipe", r->dnrpipe);
606 	nvlist_add_number(nvlr, "dnflags", r->free_flags);
607 	nvlist_add_number(nvlr, "prob", r->prob);
608 	nvlist_add_number(nvlr, "cuid", r->cuid);
609 	nvlist_add_number(nvlr, "cpid", r->cpid);
610 
611 	nvlist_add_number(nvlr, "return_icmp", r->return_icmp);
612 	nvlist_add_number(nvlr, "return_icmp6", r->return_icmp6);
613 
614 	nvlist_add_number(nvlr, "max_mss", r->max_mss);
615 	nvlist_add_number(nvlr, "scrub_flags", r->scrub_flags);
616 
617 	pfctl_nv_add_uid(nvlr, "uid", &r->uid);
618 	pfctl_nv_add_uid(nvlr, "gid", (const struct pf_rule_uid *)&r->gid);
619 
620 	nvlist_add_number(nvlr, "rule_flag", r->rule_flag);
621 	nvlist_add_number(nvlr, "action", r->action);
622 	nvlist_add_number(nvlr, "direction", r->direction);
623 	nvlist_add_number(nvlr, "log", r->log);
624 	nvlist_add_number(nvlr, "logif", r->logif);
625 	nvlist_add_number(nvlr, "quick", r->quick);
626 	nvlist_add_number(nvlr, "ifnot", r->ifnot);
627 	nvlist_add_number(nvlr, "match_tag_not", r->match_tag_not);
628 	nvlist_add_number(nvlr, "natpass", r->natpass);
629 
630 	nvlist_add_number(nvlr, "keep_state", r->keep_state);
631 	nvlist_add_number(nvlr, "af", r->af);
632 	nvlist_add_number(nvlr, "proto", r->proto);
633 	nvlist_add_number(nvlr, "type", r->type);
634 	nvlist_add_number(nvlr, "code", r->code);
635 	nvlist_add_number(nvlr, "flags", r->flags);
636 	nvlist_add_number(nvlr, "flagset", r->flagset);
637 	nvlist_add_number(nvlr, "min_ttl", r->min_ttl);
638 	nvlist_add_number(nvlr, "allow_opts", r->allow_opts);
639 	nvlist_add_number(nvlr, "rt", r->rt);
640 	nvlist_add_number(nvlr, "return_ttl", r->return_ttl);
641 	nvlist_add_number(nvlr, "tos", r->tos);
642 	nvlist_add_number(nvlr, "set_tos", r->set_tos);
643 	nvlist_add_number(nvlr, "anchor_relative", r->anchor_relative);
644 	nvlist_add_number(nvlr, "anchor_wildcard", r->anchor_wildcard);
645 
646 	nvlist_add_number(nvlr, "flush", r->flush);
647 
648 	nvlist_add_number(nvlr, "prio", r->prio);
649 	set_prio[0] = r->set_prio[0];
650 	set_prio[1] = r->set_prio[1];
651 	nvlist_add_number_array(nvlr, "set_prio", set_prio, 2);
652 
653 	pfctl_nv_add_divert(nvlr, "divert", r);
654 
655 	nvlist_add_nvlist(nvl, "rule", nvlr);
656 	nvlist_destroy(nvlr);
657 
658 	/* Now do the call. */
659 	nv.data = nvlist_pack(nvl, &nv.len);
660 	nv.size = nv.len;
661 
662 	ret = ioctl(dev, DIOCADDRULENV, &nv);
663 
664 	free(nv.data);
665 	nvlist_destroy(nvl);
666 
667 	return (ret);
668 }
669 
670 int
671 pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket, const char *anchor,
672     uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
673 {
674 	return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule,
675 	    anchor_call, false));
676 }
677 
678 int	pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket,
679 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
680 	    char *anchor_call, bool clear)
681 {
682 	struct pfioc_nv nv;
683 	nvlist_t *nvl;
684 	void *nvlpacked;
685 	int ret;
686 
687 	nvl = nvlist_create(0);
688 	if (nvl == 0)
689 		return (ENOMEM);
690 
691 	nvlist_add_number(nvl, "nr", nr);
692 	nvlist_add_number(nvl, "ticket", ticket);
693 	nvlist_add_string(nvl, "anchor", anchor);
694 	nvlist_add_number(nvl, "ruleset", ruleset);
695 
696 	if (clear)
697 		nvlist_add_bool(nvl, "clear_counter", true);
698 
699 	nvlpacked = nvlist_pack(nvl, &nv.len);
700 	if (nvlpacked == NULL) {
701 		nvlist_destroy(nvl);
702 		return (ENOMEM);
703 	}
704 	nv.data = malloc(8182);
705 	nv.size = 8192;
706 	assert(nv.len <= nv.size);
707 	memcpy(nv.data, nvlpacked, nv.len);
708 	nvlist_destroy(nvl);
709 	nvl = NULL;
710 	free(nvlpacked);
711 
712 	ret = ioctl(dev, DIOCGETRULENV, &nv);
713 	if (ret != 0) {
714 		free(nv.data);
715 		return (ret);
716 	}
717 
718 	nvl = nvlist_unpack(nv.data, nv.len, 0);
719 	if (nvl == NULL) {
720 		free(nv.data);
721 		return (EIO);
722 	}
723 
724 	pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule);
725 
726 	if (anchor_call)
727 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
728 		    MAXPATHLEN);
729 
730 	free(nv.data);
731 	nvlist_destroy(nvl);
732 
733 	return (0);
734 }
735 
736 int
737 pfctl_set_keepcounters(int dev, bool keep)
738 {
739 	struct pfioc_nv	 nv;
740 	nvlist_t	*nvl;
741 	int		 ret;
742 
743 	nvl = nvlist_create(0);
744 
745 	nvlist_add_bool(nvl, "keep_counters", keep);
746 
747 	nv.data = nvlist_pack(nvl, &nv.len);
748 	nv.size = nv.len;
749 
750 	nvlist_destroy(nvl);
751 
752 	ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv);
753 
754 	free(nv.data);
755 	return (ret);
756 }
757 
758 static void
759 pfctl_nv_add_state_cmp(nvlist_t *nvl, const char *name,
760     const struct pfctl_state_cmp *cmp)
761 {
762 	nvlist_t	*nv;
763 
764 	nv = nvlist_create(0);
765 
766 	nvlist_add_number(nv, "id", cmp->id);
767 	nvlist_add_number(nv, "creatorid", cmp->creatorid);
768 	nvlist_add_number(nv, "direction", cmp->direction);
769 
770 	nvlist_add_nvlist(nvl, name, nv);
771 	nvlist_destroy(nv);
772 }
773 
774 static void
775 pf_state_key_export_to_state_key(struct pfctl_state_key *ps,
776     const struct pf_state_key_export *s)
777 {
778 	bcopy(s->addr, ps->addr, sizeof(ps->addr[0]) * 2);
779 	ps->port[0] = s->port[0];
780 	ps->port[1] = s->port[1];
781 }
782 
783 static void
784 pf_state_peer_export_to_state_peer(struct pfctl_state_peer *ps,
785     const struct pf_state_peer_export *s)
786 {
787 	/* Ignore scrub. */
788 	ps->seqlo = s->seqlo;
789 	ps->seqhi = s->seqhi;
790 	ps->seqdiff = s->seqdiff;
791 	/* Ignore max_win & mss */
792 	ps->state = s->state;
793 	ps->wscale = s->wscale;
794 }
795 
796 static void
797 pf_state_export_to_state(struct pfctl_state *ps, const struct pf_state_export *s)
798 {
799 	assert(s->version >= PF_STATE_VERSION);
800 
801 	ps->id = s->id;
802 	strlcpy(ps->ifname, s->ifname, sizeof(ps->ifname));
803 	strlcpy(ps->orig_ifname, s->orig_ifname, sizeof(ps->orig_ifname));
804 	pf_state_key_export_to_state_key(&ps->key[0], &s->key[0]);
805 	pf_state_key_export_to_state_key(&ps->key[1], &s->key[1]);
806 	pf_state_peer_export_to_state_peer(&ps->src, &s->src);
807 	pf_state_peer_export_to_state_peer(&ps->dst, &s->dst);
808 	bcopy(&s->rt_addr, &ps->rt_addr, sizeof(ps->rt_addr));
809 	ps->rule = ntohl(s->rule);
810 	ps->anchor = ntohl(s->anchor);
811 	ps->nat_rule = ntohl(s->nat_rule);
812 	ps->creation = ntohl(s->creation);
813 	ps->expire = ntohl(s->expire);
814 	ps->packets[0] = s->packets[0];
815 	ps->packets[1] = s->packets[1];
816 	ps->bytes[0] = s->bytes[0];
817 	ps->bytes[1] = s->bytes[1];
818 	ps->creatorid = s->creatorid;
819 	ps->key[0].proto = s->proto;
820 	ps->key[1].proto = s->proto;
821 	ps->key[0].af = s->af;
822 	ps->key[1].af = s->af;
823 	ps->direction = s->direction;
824 	ps->state_flags = s->state_flags;
825 	ps->sync_flags = s->sync_flags;
826 }
827 
828 int
829 pfctl_get_states(int dev, struct pfctl_states *states)
830 {
831 	struct pfioc_states_v2 ps;
832 	struct pf_state_export *p;
833 	char *inbuf = NULL, *newinbuf = NULL;
834 	unsigned int len = 0;
835 	int i, error;
836 
837 	bzero(&ps, sizeof(ps));
838 	ps.ps_req_version = PF_STATE_VERSION;
839 
840 	bzero(states, sizeof(*states));
841 	TAILQ_INIT(&states->states);
842 
843 	for (;;) {
844 		ps.ps_len = len;
845 		if (len) {
846 			newinbuf = realloc(inbuf, len);
847 			if (newinbuf == NULL)
848 				return (ENOMEM);
849 			ps.ps_buf = inbuf = newinbuf;
850 		}
851 		if ((error = ioctl(dev, DIOCGETSTATESV2, &ps)) < 0) {
852 			free(inbuf);
853 			return (error);
854 		}
855 		if (ps.ps_len + sizeof(struct pfioc_states_v2) < len)
856 			break;
857 		if (len == 0 && ps.ps_len == 0)
858 			goto out;
859 		if (len == 0 && ps.ps_len != 0)
860 			len = ps.ps_len;
861 		if (ps.ps_len == 0)
862 			goto out;      /* no states */
863 		len *= 2;
864 	}
865 	p = ps.ps_states;
866 
867 	for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
868 		struct pfctl_state *s = malloc(sizeof(*s));
869 		if (s == NULL) {
870 			pfctl_free_states(states);
871 			error = ENOMEM;
872 			goto out;
873 		}
874 
875 		pf_state_export_to_state(s, p);
876 		TAILQ_INSERT_TAIL(&states->states, s, entry);
877 	}
878 
879 out:
880 	free(inbuf);
881 	return (error);
882 }
883 
884 void
885 pfctl_free_states(struct pfctl_states *states)
886 {
887 	struct pfctl_state *s, *tmp;
888 
889 	TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) {
890 		free(s);
891 	}
892 
893 	bzero(states, sizeof(*states));
894 }
895 
896 static int
897 _pfctl_clear_states(int dev, const struct pfctl_kill *kill,
898     unsigned int *killed, uint64_t ioctlval)
899 {
900 	struct pfioc_nv	 nv;
901 	nvlist_t	*nvl;
902 	int		 ret;
903 
904 	nvl = nvlist_create(0);
905 
906 	pfctl_nv_add_state_cmp(nvl, "cmp", &kill->cmp);
907 	nvlist_add_number(nvl, "af", kill->af);
908 	nvlist_add_number(nvl, "proto", kill->proto);
909 	pfctl_nv_add_rule_addr(nvl, "src", &kill->src);
910 	pfctl_nv_add_rule_addr(nvl, "dst", &kill->dst);
911 	pfctl_nv_add_rule_addr(nvl, "rt_addr", &kill->rt_addr);
912 	nvlist_add_string(nvl, "ifname", kill->ifname);
913 	nvlist_add_string(nvl, "label", kill->label);
914 	nvlist_add_bool(nvl, "kill_match", kill->kill_match);
915 
916 	nv.data = nvlist_pack(nvl, &nv.len);
917 	nv.size = nv.len;
918 	nvlist_destroy(nvl);
919 	nvl = NULL;
920 
921 	ret = ioctl(dev, ioctlval, &nv);
922 	if (ret != 0) {
923 		free(nv.data);
924 		return (ret);
925 	}
926 
927 	nvl = nvlist_unpack(nv.data, nv.len, 0);
928 	if (nvl == NULL) {
929 		free(nv.data);
930 		return (EIO);
931 	}
932 
933 	if (killed)
934 		*killed = nvlist_get_number(nvl, "killed");
935 
936 	nvlist_destroy(nvl);
937 	free(nv.data);
938 
939 	return (ret);
940 }
941 
942 int
943 pfctl_clear_states(int dev, const struct pfctl_kill *kill,
944     unsigned int *killed)
945 {
946 	return (_pfctl_clear_states(dev, kill, killed, DIOCCLRSTATESNV));
947 }
948 
949 int
950 pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed)
951 {
952 	return (_pfctl_clear_states(dev, kill, killed, DIOCKILLSTATESNV));
953 }
954 
955 static int
956 pfctl_get_limit(int dev, const int index, uint *limit)
957 {
958 	struct pfioc_limit pl;
959 
960 	bzero(&pl, sizeof(pl));
961 	pl.index = index;
962 
963 	if (ioctl(dev, DIOCGETLIMIT, &pl) == -1)
964 		return (errno);
965 
966 	*limit = pl.limit;
967 
968 	return (0);
969 }
970 
971 int
972 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s)
973 {
974 	struct pfioc_nv	 nv;
975 	nvlist_t	*nvl;
976 	int		 ret;
977 	uint		 state_limit;
978 
979 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
980 	if (ret != 0)
981 		return (ret);
982 
983 	nvl = nvlist_create(0);
984 
985 	nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER);
986 	nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE);
987 	nvlist_add_number(nvl, "highwater", state_limit * s->highwater / 100);
988 	nvlist_add_number(nvl, "lowwater", state_limit * s->lowwater / 100);
989 
990 	nv.data = nvlist_pack(nvl, &nv.len);
991 	nv.size = nv.len;
992 	nvlist_destroy(nvl);
993 	nvl = NULL;
994 
995 	ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv);
996 
997 	free(nv.data);
998 	return (ret);
999 }
1000 
1001 int
1002 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s)
1003 {
1004 	struct pfioc_nv	 nv;
1005 	nvlist_t	*nvl;
1006 	int		 ret;
1007 	uint		 state_limit;
1008 	bool		 enabled, adaptive;
1009 
1010 	ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
1011 	if (ret != 0)
1012 		return (ret);
1013 
1014 	bzero(s, sizeof(*s));
1015 
1016 	nv.data = malloc(256);
1017 	nv.len = nv.size = 256;
1018 
1019 	if (ioctl(dev, DIOCGETSYNCOOKIES, &nv)) {
1020 		free(nv.data);
1021 		return (errno);
1022 	}
1023 
1024 	nvl = nvlist_unpack(nv.data, nv.len, 0);
1025 	free(nv.data);
1026 	if (nvl == NULL) {
1027 		return (EIO);
1028 	}
1029 
1030 	enabled = nvlist_get_bool(nvl, "enabled");
1031 	adaptive = nvlist_get_bool(nvl, "adaptive");
1032 
1033 	if (enabled) {
1034 		if (adaptive)
1035 			s->mode = PFCTL_SYNCOOKIES_ADAPTIVE;
1036 		else
1037 			s->mode = PFCTL_SYNCOOKIES_ALWAYS;
1038 	} else {
1039 		s->mode = PFCTL_SYNCOOKIES_NEVER;
1040 	}
1041 
1042 	s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit;
1043 	s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit;
1044 
1045 	nvlist_destroy(nvl);
1046 
1047 	return (0);
1048 }
1049