xref: /freebsd/lib/libpfctl/libpfctl.c (revision 0972294ef034d92f59857b8312dd2e1e3a7adc9c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 #include <sys/ioctl.h>
35 #include <sys/nv.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 
39 #include <net/if.h>
40 #include <net/pfvar.h>
41 #include <netinet/in.h>
42 
43 #include <netpfil/pf/pf_nl.h>
44 #include <netlink/netlink.h>
45 #include <netlink/netlink_generic.h>
46 #include <netlink/netlink_snl.h>
47 #include <netlink/netlink_snl_generic.h>
48 #include <netlink/netlink_snl_route.h>
49 
50 #include <assert.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "libpfctl.h"
58 
59 struct pfctl_handle {
60 	int fd;
61 	struct snl_state ss;
62 };
63 
64 const char* PFCTL_SYNCOOKIES_MODE_NAMES[] = {
65 	"never",
66 	"always",
67 	"adaptive"
68 };
69 
70 static int	_pfctl_clear_states(int , const struct pfctl_kill *,
71 		    unsigned int *, uint64_t);
72 
73 struct pfctl_handle *
pfctl_open(const char * pf_device)74 pfctl_open(const char *pf_device)
75 {
76 	struct pfctl_handle *h;
77 
78 	h = calloc(1, sizeof(struct pfctl_handle));
79 	h->fd = -1;
80 
81 	h->fd = open(pf_device, O_RDWR);
82 	if (h->fd < 0)
83 		goto error;
84 
85 	if (!snl_init(&h->ss, NETLINK_GENERIC))
86 		goto error;
87 
88 	return (h);
89 error:
90 	close(h->fd);
91 	snl_free(&h->ss);
92 	free(h);
93 
94 	return (NULL);
95 }
96 
97 void
pfctl_close(struct pfctl_handle * h)98 pfctl_close(struct pfctl_handle *h)
99 {
100 	close(h->fd);
101 	snl_free(&h->ss);
102 	free(h);
103 }
104 
105 int
pfctl_fd(struct pfctl_handle * h)106 pfctl_fd(struct pfctl_handle *h)
107 {
108 	return (h->fd);
109 }
110 
111 static int
pfctl_do_netlink_cmd(struct pfctl_handle * h,uint cmd)112 pfctl_do_netlink_cmd(struct pfctl_handle *h, uint cmd)
113 {
114 	struct snl_errmsg_data e = {};
115 	struct snl_writer nw;
116 	struct nlmsghdr *hdr;
117 	uint32_t seq_id;
118 	int family_id;
119 
120 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
121 	if (family_id == 0)
122 		return (ENOTSUP);
123 
124 	snl_init_writer(&h->ss, &nw);
125 	hdr = snl_create_genl_msg_request(&nw, family_id, cmd);
126 
127 	hdr = snl_finalize_msg(&nw);
128 	if (hdr == NULL)
129 		return (ENOMEM);
130 	seq_id = hdr->nlmsg_seq;
131 
132 	snl_send_message(&h->ss, hdr);
133 
134 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
135 	}
136 
137 	return (e.error);
138 }
139 
140 static int
pfctl_do_ioctl(int dev,uint cmd,size_t size,nvlist_t ** nvl)141 pfctl_do_ioctl(int dev, uint cmd, size_t size, nvlist_t **nvl)
142 {
143 	struct pfioc_nv nv;
144 	void *data;
145 	size_t nvlen;
146 	int ret;
147 
148 	data = nvlist_pack(*nvl, &nvlen);
149 	if (nvlen > size)
150 		size = nvlen;
151 
152 retry:
153 	nv.data = malloc(size);
154 	if (nv.data == NULL) {
155 		ret = ENOMEM;
156 		goto out;
157 	}
158 
159 	memcpy(nv.data, data, nvlen);
160 
161 	nv.len = nvlen;
162 	nv.size = size;
163 
164 	ret = ioctl(dev, cmd, &nv);
165 	if (ret == -1 && errno == ENOSPC) {
166 		size *= 2;
167 		free(nv.data);
168 		goto retry;
169 	}
170 
171 	nvlist_destroy(*nvl);
172 	*nvl = NULL;
173 
174 	if (ret == 0) {
175 		*nvl = nvlist_unpack(nv.data, nv.len, 0);
176 		if (*nvl == NULL) {
177 			ret = EIO;
178 			goto out;
179 		}
180 	} else {
181 		ret = errno;
182 	}
183 
184 out:
185 	free(data);
186 	free(nv.data);
187 
188 	return (ret);
189 }
190 
191 static void
pf_nvuint_8_array(const nvlist_t * nvl,const char * name,size_t maxelems,uint8_t * numbers,size_t * nelems)192 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems,
193     uint8_t *numbers, size_t *nelems)
194 {
195 	const uint64_t *tmp;
196 	size_t elems;
197 
198 	tmp = nvlist_get_number_array(nvl, name, &elems);
199 	assert(elems <= maxelems);
200 
201 	for (size_t i = 0; i < elems; i++)
202 		numbers[i] = tmp[i];
203 
204 	if (nelems)
205 		*nelems = elems;
206 }
207 
208 static void
pf_nvuint_16_array(const nvlist_t * nvl,const char * name,size_t maxelems,uint16_t * numbers,size_t * nelems)209 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems,
210     uint16_t *numbers, size_t *nelems)
211 {
212 	const uint64_t *tmp;
213 	size_t elems;
214 
215 	tmp = nvlist_get_number_array(nvl, name, &elems);
216 	assert(elems <= maxelems);
217 
218 	for (size_t i = 0; i < elems; i++)
219 		numbers[i] = tmp[i];
220 
221 	if (nelems)
222 		*nelems = elems;
223 }
224 
225 static void
pf_nvuint_32_array(const nvlist_t * nvl,const char * name,size_t maxelems,uint32_t * numbers,size_t * nelems)226 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems,
227     uint32_t *numbers, size_t *nelems)
228 {
229 	const uint64_t *tmp;
230 	size_t elems;
231 
232 	tmp = nvlist_get_number_array(nvl, name, &elems);
233 
234 	for (size_t i = 0; i < elems && i < maxelems; i++)
235 		numbers[i] = tmp[i];
236 
237 	if (nelems)
238 		*nelems = elems;
239 }
240 
241 static void
pf_nvuint_64_array(const nvlist_t * nvl,const char * name,size_t maxelems,uint64_t * numbers,size_t * nelems)242 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems,
243     uint64_t *numbers, size_t *nelems)
244 {
245 	const uint64_t *tmp;
246 	size_t elems;
247 
248 	tmp = nvlist_get_number_array(nvl, name, &elems);
249 	assert(elems <= maxelems);
250 
251 	for (size_t i = 0; i < elems; i++)
252 		numbers[i] = tmp[i];
253 
254 	if (nelems)
255 		*nelems = elems;
256 }
257 
258 int
pfctl_startstop(struct pfctl_handle * h,int start)259 pfctl_startstop(struct pfctl_handle *h, int start)
260 {
261 	return (pfctl_do_netlink_cmd(h, start ? PFNL_CMD_START : PFNL_CMD_STOP));
262 }
263 
264 static void
_pfctl_get_status_counters(const nvlist_t * nvl,struct pfctl_status_counters * counters)265 _pfctl_get_status_counters(const nvlist_t *nvl,
266     struct pfctl_status_counters *counters)
267 {
268 	const uint64_t		*ids, *counts;
269 	const char *const	*names;
270 	size_t id_len, counter_len, names_len;
271 
272 	ids = nvlist_get_number_array(nvl, "ids", &id_len);
273 	counts = nvlist_get_number_array(nvl, "counters", &counter_len);
274 	names = nvlist_get_string_array(nvl, "names", &names_len);
275 	assert(id_len == counter_len);
276 	assert(counter_len == names_len);
277 
278 	TAILQ_INIT(counters);
279 
280 	for (size_t i = 0; i < id_len; i++) {
281 		struct pfctl_status_counter *c;
282 
283 		c = malloc(sizeof(*c));
284 		if (c == NULL)
285 			continue;
286 
287 		c->id = ids[i];
288 		c->counter = counts[i];
289 		c->name = strdup(names[i]);
290 
291 		TAILQ_INSERT_TAIL(counters, c, entry);
292 	}
293 }
294 
295 #define	_OUT(_field)	offsetof(struct pfctl_status_counter, _field)
296 static const struct snl_attr_parser ap_counter[] = {
297 	{ .type = PF_C_COUNTER, .off = _OUT(counter), .cb = snl_attr_get_uint64 },
298 	{ .type = PF_C_NAME, .off = _OUT(name), .cb = snl_attr_get_string },
299 	{ .type = PF_C_ID, .off = _OUT(id), .cb = snl_attr_get_uint32 },
300 };
301 SNL_DECLARE_ATTR_PARSER(counter_parser, ap_counter);
302 #undef _OUT
303 
304 static bool
snl_attr_get_counters(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)305 snl_attr_get_counters(struct snl_state *ss, struct nlattr *nla,
306     const void *arg __unused, void *target)
307 {
308 	struct pfctl_status_counter counter = {};
309 	struct pfctl_status_counter *c;
310 	bool error;
311 
312 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &counter_parser, &counter);
313 	if (! error)
314 		return (error);
315 
316 	c = malloc(sizeof(*c));
317 	if (c == NULL)
318 		return (false);
319 
320 	c->id = counter.id;
321 	c->counter = counter.counter;
322 	c->name = strdup(counter.name);
323 
324 	TAILQ_INSERT_TAIL((struct pfctl_status_counters *)target, c, entry);
325 
326 	return (error);
327 }
328 
329 struct snl_uint64_array {
330 	uint64_t *array;
331 	size_t count;
332 	size_t max;
333 };
334 static bool
snl_attr_get_uint64_element(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)335 snl_attr_get_uint64_element(struct snl_state *ss, struct nlattr *nla,
336     const void *arg, void *target)
337 {
338 	bool error;
339 	uint64_t value;
340 	struct snl_uint64_array *t = (struct snl_uint64_array *)target;
341 
342 	if (t->count >= t->max)
343 		return (false);
344 
345 	error = snl_attr_get_uint64(ss, nla, arg, &value);
346 	if (! error)
347 		return (error);
348 
349 	t->array[t->count++] = value;
350 
351 	return (true);
352 }
353 
354 static const struct snl_attr_parser ap_array[] = {
355 	{ .cb = snl_attr_get_uint64_element },
356 };
357 SNL_DECLARE_ATTR_PARSER(array_parser, ap_array);
358 static bool
snl_attr_get_uint64_array(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)359 snl_attr_get_uint64_array(struct snl_state *ss, struct nlattr *nla,
360     const void *arg, void *target)
361 {
362 	struct snl_uint64_array a = {
363 		.array = target,
364 		.count = 0,
365 		.max = (size_t)arg,
366 	};
367 	bool error;
368 
369 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &array_parser, &a);
370 	if (! error)
371 		return (error);
372 
373 	return (true);
374 }
375 
376 #define	_OUT(_field)	offsetof(struct pfctl_status, _field)
377 static const struct snl_attr_parser ap_getstatus[] = {
378 	{ .type = PF_GS_IFNAME, .off = _OUT(ifname), .arg_u32 = IFNAMSIZ, .cb = snl_attr_copy_string },
379 	{ .type = PF_GS_RUNNING, .off = _OUT(running), .cb = snl_attr_get_bool },
380 	{ .type = PF_GS_SINCE, .off = _OUT(since), .cb = snl_attr_get_uint32 },
381 	{ .type = PF_GS_DEBUG, .off = _OUT(debug), .cb = snl_attr_get_uint32 },
382 	{ .type = PF_GS_HOSTID, .off = _OUT(hostid), .cb = snl_attr_get_uint32 },
383 	{ .type = PF_GS_STATES, .off = _OUT(states), .cb = snl_attr_get_uint32 },
384 	{ .type = PF_GS_SRC_NODES, .off = _OUT(src_nodes), .cb = snl_attr_get_uint32 },
385 	{ .type = PF_GS_REASSEMBLE, .off = _OUT(reass), .cb = snl_attr_get_uint32 },
386 	{ .type = PF_GS_SYNCOOKIES_ACTIVE, .off = _OUT(syncookies_active), .cb = snl_attr_get_uint32 },
387 	{ .type = PF_GS_COUNTERS, .off = _OUT(counters), .cb = snl_attr_get_counters },
388 	{ .type = PF_GS_LCOUNTERS, .off = _OUT(lcounters), .cb = snl_attr_get_counters },
389 	{ .type = PF_GS_FCOUNTERS, .off = _OUT(fcounters), .cb = snl_attr_get_counters },
390 	{ .type = PF_GS_SCOUNTERS, .off = _OUT(scounters), .cb = snl_attr_get_counters },
391 	{ .type = PF_GS_CHKSUM, .off = _OUT(pf_chksum), .arg_u32 = PF_MD5_DIGEST_LENGTH, .cb = snl_attr_get_bytes },
392 	{ .type = PF_GS_BCOUNTERS, .off = _OUT(bcounters), .arg_u32 = 2 * 2, .cb = snl_attr_get_uint64_array },
393 	{ .type = PF_GS_PCOUNTERS, .off = _OUT(pcounters), .arg_u32 = 2 * 2 * 2, .cb = snl_attr_get_uint64_array },
394 };
395 SNL_DECLARE_PARSER(getstatus_parser, struct genlmsghdr, snl_f_p_empty, ap_getstatus);
396 #undef _OUT
397 
398 struct pfctl_status *
pfctl_get_status_h(struct pfctl_handle * h __unused)399 pfctl_get_status_h(struct pfctl_handle *h __unused)
400 {
401 	struct pfctl_status	*status;
402 	struct snl_errmsg_data e = {};
403 	struct nlmsghdr *hdr;
404 	struct snl_writer nw;
405 	uint32_t seq_id;
406 	int family_id;
407 
408 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
409 	if (family_id == 0)
410 		return (NULL);
411 
412 	snl_init_writer(&h->ss, &nw);
413 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_STATUS);
414 	hdr->nlmsg_flags |= NLM_F_DUMP;
415 
416 	hdr = snl_finalize_msg(&nw);
417 	if (hdr == NULL) {
418 		return (NULL);
419 	}
420 
421 	seq_id = hdr->nlmsg_seq;
422 	if (! snl_send_message(&h->ss, hdr))
423 		return (NULL);
424 
425 	status = calloc(1, sizeof(*status));
426 	if (status == NULL)
427 		return (NULL);
428 	TAILQ_INIT(&status->counters);
429 	TAILQ_INIT(&status->lcounters);
430 	TAILQ_INIT(&status->fcounters);
431 	TAILQ_INIT(&status->scounters);
432 
433 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
434 		if (! snl_parse_nlmsg(&h->ss, hdr, &getstatus_parser, status))
435 			continue;
436 	}
437 
438 	return (status);
439 }
440 
441 struct pfctl_status *
pfctl_get_status(int dev)442 pfctl_get_status(int dev)
443 {
444 	struct pfctl_status	*status;
445 	nvlist_t	*nvl;
446 	size_t		 len;
447 	const void	*chksum;
448 
449 	status = calloc(1, sizeof(*status));
450 	if (status == NULL)
451 		return (NULL);
452 
453 	nvl = nvlist_create(0);
454 
455 	if (pfctl_do_ioctl(dev, DIOCGETSTATUSNV, 4096, &nvl)) {
456 		nvlist_destroy(nvl);
457 		free(status);
458 		return (NULL);
459 	}
460 
461 	status->running = nvlist_get_bool(nvl, "running");
462 	status->since = nvlist_get_number(nvl, "since");
463 	status->debug = nvlist_get_number(nvl, "debug");
464 	status->hostid = ntohl(nvlist_get_number(nvl, "hostid"));
465 	status->states = nvlist_get_number(nvl, "states");
466 	status->src_nodes = nvlist_get_number(nvl, "src_nodes");
467 	status->syncookies_active = nvlist_get_bool(nvl, "syncookies_active");
468 	status->reass = nvlist_get_number(nvl, "reass");
469 
470 	strlcpy(status->ifname, nvlist_get_string(nvl, "ifname"),
471 	    IFNAMSIZ);
472 	chksum = nvlist_get_binary(nvl, "chksum", &len);
473 	assert(len == PF_MD5_DIGEST_LENGTH);
474 	memcpy(status->pf_chksum, chksum, len);
475 
476 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "counters"),
477 	    &status->counters);
478 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "lcounters"),
479 	    &status->lcounters);
480 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "fcounters"),
481 	    &status->fcounters);
482 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"),
483 	    &status->scounters);
484 
485 	pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 2,
486 	    (uint64_t *)status->pcounters, NULL);
487 	pf_nvuint_64_array(nvl, "bcounters", 2 * 2,
488 	    (uint64_t *)status->bcounters, NULL);
489 
490 	nvlist_destroy(nvl);
491 
492 	return (status);
493 }
494 int
pfctl_clear_status(struct pfctl_handle * h)495 pfctl_clear_status(struct pfctl_handle *h)
496 {
497 	return (pfctl_do_netlink_cmd(h, PFNL_CMD_CLEAR_STATUS));
498 }
499 
500 static uint64_t
_pfctl_status_counter(struct pfctl_status_counters * counters,uint64_t id)501 _pfctl_status_counter(struct pfctl_status_counters *counters, uint64_t id)
502 {
503 	struct pfctl_status_counter *c;
504 
505 	TAILQ_FOREACH(c, counters, entry) {
506 		if (c->id == id)
507 			return (c->counter);
508 	}
509 
510 	return (0);
511 }
512 
513 uint64_t
pfctl_status_counter(struct pfctl_status * status,int id)514 pfctl_status_counter(struct pfctl_status *status, int id)
515 {
516 	return (_pfctl_status_counter(&status->counters, id));
517 }
518 
519 uint64_t
pfctl_status_lcounter(struct pfctl_status * status,int id)520 pfctl_status_lcounter(struct pfctl_status *status, int id)
521 {
522 	return (_pfctl_status_counter(&status->lcounters, id));
523 }
524 
525 uint64_t
pfctl_status_fcounter(struct pfctl_status * status,int id)526 pfctl_status_fcounter(struct pfctl_status *status, int id)
527 {
528 	return (_pfctl_status_counter(&status->fcounters, id));
529 }
530 
531 uint64_t
pfctl_status_scounter(struct pfctl_status * status,int id)532 pfctl_status_scounter(struct pfctl_status *status, int id)
533 {
534 	return (_pfctl_status_counter(&status->scounters, id));
535 }
536 
537 void
pfctl_free_status(struct pfctl_status * status)538 pfctl_free_status(struct pfctl_status *status)
539 {
540 	struct pfctl_status_counter *c, *tmp;
541 
542 	if (status == NULL)
543 		return;
544 
545 	TAILQ_FOREACH_SAFE(c, &status->counters, entry, tmp) {
546 		free(c->name);
547 		free(c);
548 	}
549 	TAILQ_FOREACH_SAFE(c, &status->lcounters, entry, tmp) {
550 		free(c->name);
551 		free(c);
552 	}
553 	TAILQ_FOREACH_SAFE(c, &status->fcounters, entry, tmp) {
554 		free(c->name);
555 		free(c);
556 	}
557 	TAILQ_FOREACH_SAFE(c, &status->scounters, entry, tmp) {
558 		free(c->name);
559 		free(c);
560 	}
561 
562 	free(status);
563 }
564 
565 static void
pfctl_nv_add_addr(nvlist_t * nvparent,const char * name,const struct pf_addr * addr)566 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name,
567     const struct pf_addr *addr)
568 {
569 	nvlist_t *nvl = nvlist_create(0);
570 
571 	nvlist_add_binary(nvl, "addr", addr, sizeof(*addr));
572 
573 	nvlist_add_nvlist(nvparent, name, nvl);
574 	nvlist_destroy(nvl);
575 }
576 
577 static void
pf_nvaddr_to_addr(const nvlist_t * nvl,struct pf_addr * addr)578 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr)
579 {
580 	size_t len;
581 	const void *data;
582 
583 	data = nvlist_get_binary(nvl, "addr", &len);
584 	assert(len == sizeof(struct pf_addr));
585 	memcpy(addr, data, len);
586 }
587 
588 static void
pfctl_nv_add_addr_wrap(nvlist_t * nvparent,const char * name,const struct pf_addr_wrap * addr)589 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name,
590     const struct pf_addr_wrap *addr)
591 {
592 	nvlist_t *nvl = nvlist_create(0);
593 
594 	nvlist_add_number(nvl, "type", addr->type);
595 	nvlist_add_number(nvl, "iflags", addr->iflags);
596 	if (addr->type == PF_ADDR_DYNIFTL)
597 		nvlist_add_string(nvl, "ifname", addr->v.ifname);
598 	if (addr->type == PF_ADDR_TABLE)
599 		nvlist_add_string(nvl, "tblname", addr->v.tblname);
600 	pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr);
601 	pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask);
602 
603 	nvlist_add_nvlist(nvparent, name, nvl);
604 	nvlist_destroy(nvl);
605 }
606 
607 static void
pf_nvaddr_wrap_to_addr_wrap(const nvlist_t * nvl,struct pf_addr_wrap * addr)608 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
609 {
610 	bzero(addr, sizeof(*addr));
611 
612 	addr->type = nvlist_get_number(nvl, "type");
613 	addr->iflags = nvlist_get_number(nvl, "iflags");
614 	if (addr->type == PF_ADDR_DYNIFTL) {
615 		strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"),
616 		    IFNAMSIZ);
617 		addr->p.dyncnt = nvlist_get_number(nvl, "dyncnt");
618 	}
619 	if (addr->type == PF_ADDR_TABLE) {
620 		strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"),
621 		    PF_TABLE_NAME_SIZE);
622 		addr->p.tblcnt = nvlist_get_number(nvl, "tblcnt");
623 	}
624 
625 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr);
626 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask);
627 }
628 
629 static void
pfctl_nv_add_rule_addr(nvlist_t * nvparent,const char * name,const struct pf_rule_addr * addr)630 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name,
631     const struct pf_rule_addr *addr)
632 {
633 	uint64_t ports[2];
634 	nvlist_t *nvl = nvlist_create(0);
635 
636 	pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr);
637 	ports[0] = addr->port[0];
638 	ports[1] = addr->port[1];
639 	nvlist_add_number_array(nvl, "port", ports, 2);
640 	nvlist_add_number(nvl, "neg", addr->neg);
641 	nvlist_add_number(nvl, "port_op", addr->port_op);
642 
643 	nvlist_add_nvlist(nvparent, name, nvl);
644 	nvlist_destroy(nvl);
645 }
646 
647 static void
pf_nvrule_addr_to_rule_addr(const nvlist_t * nvl,struct pf_rule_addr * addr)648 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
649 {
650 	pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr);
651 
652 	pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL);
653 	addr->neg = nvlist_get_number(nvl, "neg");
654 	addr->port_op = nvlist_get_number(nvl, "port_op");
655 }
656 
657 static void
pf_nvmape_to_mape(const nvlist_t * nvl,struct pf_mape_portset * mape)658 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape)
659 {
660 	mape->offset = nvlist_get_number(nvl, "offset");
661 	mape->psidlen = nvlist_get_number(nvl, "psidlen");
662 	mape->psid = nvlist_get_number(nvl, "psid");
663 }
664 
665 static void
pf_nvpool_to_pool(const nvlist_t * nvl,struct pfctl_pool * pool)666 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool)
667 {
668 	size_t len;
669 	const void *data;
670 
671 	data = nvlist_get_binary(nvl, "key", &len);
672 	assert(len == sizeof(pool->key));
673 	memcpy(&pool->key, data, len);
674 
675 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter);
676 
677 	pool->tblidx = nvlist_get_number(nvl, "tblidx");
678 	pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL);
679 	pool->opts = nvlist_get_number(nvl, "opts");
680 
681 	if (nvlist_exists_nvlist(nvl, "mape"))
682 		pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape);
683 }
684 
685 static void
pf_nvrule_uid_to_rule_uid(const nvlist_t * nvl,struct pf_rule_uid * uid)686 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
687 {
688 	pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL);
689 	uid->op = nvlist_get_number(nvl, "op");
690 }
691 
692 static void
pf_nvdivert_to_divert(const nvlist_t * nvl,struct pfctl_rule * rule)693 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule)
694 {
695 	pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr);
696 	rule->divert.port = nvlist_get_number(nvl, "port");
697 }
698 
699 static void
pf_nvrule_to_rule(const nvlist_t * nvl,struct pfctl_rule * rule)700 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule)
701 {
702 	const uint64_t *skip;
703 	const char *const *labels;
704 	size_t skipcount, labelcount;
705 
706 	rule->nr = nvlist_get_number(nvl, "nr");
707 
708 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src);
709 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst);
710 
711 	skip = nvlist_get_number_array(nvl, "skip", &skipcount);
712 	assert(skip);
713 	assert(skipcount == PF_SKIP_COUNT);
714 	for (int i = 0; i < PF_SKIP_COUNT; i++)
715 		rule->skip[i].nr = skip[i];
716 
717 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
718 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
719 	for (size_t i = 0; i < labelcount; i++)
720 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
721 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
722 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
723 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
724 	strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE);
725 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
726 	    PF_TAG_NAME_SIZE);
727 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
728 	    PF_TAG_NAME_SIZE);
729 
730 	strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"),
731 	    PF_TABLE_NAME_SIZE);
732 
733 	pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rdr);
734 
735 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
736 	pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL);
737 	pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL);
738 
739 	if (nvlist_exists_number(nvl, "timestamp")) {
740 		rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp");
741 	}
742 
743 	rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint");
744 
745 	rule->rtableid = nvlist_get_number(nvl, "rtableid");
746 	pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL);
747 	rule->max_states = nvlist_get_number(nvl, "max_states");
748 	rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes");
749 	rule->max_src_states = nvlist_get_number(nvl, "max_src_states");
750 	rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn");
751 	rule->max_src_conn_rate.limit =
752 	    nvlist_get_number(nvl, "max_src_conn_rate.limit");
753 	rule->max_src_conn_rate.seconds =
754 	    nvlist_get_number(nvl, "max_src_conn_rate.seconds");
755 	rule->qid = nvlist_get_number(nvl, "qid");
756 	rule->pqid = nvlist_get_number(nvl, "pqid");
757 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
758 	rule->dnrpipe = nvlist_get_number(nvl, "dnrpipe");
759 	rule->free_flags = nvlist_get_number(nvl, "dnflags");
760 	rule->prob = nvlist_get_number(nvl, "prob");
761 	rule->cuid = nvlist_get_number(nvl, "cuid");
762 	rule->cpid = nvlist_get_number(nvl, "cpid");
763 
764 	rule->return_icmp = nvlist_get_number(nvl, "return_icmp");
765 	rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6");
766 	rule->max_mss = nvlist_get_number(nvl, "max_mss");
767 	rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags");
768 
769 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid);
770 	pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"),
771 	    (struct pf_rule_uid *)&rule->gid);
772 
773 	rule->rule_flag = nvlist_get_number(nvl, "rule_flag");
774 	rule->action = nvlist_get_number(nvl, "action");
775 	rule->direction = nvlist_get_number(nvl, "direction");
776 	rule->log = nvlist_get_number(nvl, "log");
777 	rule->logif = nvlist_get_number(nvl, "logif");
778 	rule->quick = nvlist_get_number(nvl, "quick");
779 	rule->ifnot = nvlist_get_number(nvl, "ifnot");
780 	rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not");
781 	rule->natpass = nvlist_get_number(nvl, "natpass");
782 
783 	rule->keep_state = nvlist_get_number(nvl, "keep_state");
784 	rule->af = nvlist_get_number(nvl, "af");
785 	rule->proto = nvlist_get_number(nvl, "proto");
786 	rule->type = nvlist_get_number(nvl, "type");
787 	rule->code = nvlist_get_number(nvl, "code");
788 	rule->flags = nvlist_get_number(nvl, "flags");
789 	rule->flagset = nvlist_get_number(nvl, "flagset");
790 	rule->min_ttl = nvlist_get_number(nvl, "min_ttl");
791 	rule->allow_opts = nvlist_get_number(nvl, "allow_opts");
792 	rule->rt = nvlist_get_number(nvl, "rt");
793 	rule->return_ttl  = nvlist_get_number(nvl, "return_ttl");
794 	rule->tos = nvlist_get_number(nvl, "tos");
795 	rule->set_tos = nvlist_get_number(nvl, "set_tos");
796 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
797 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
798 
799 	rule->flush = nvlist_get_number(nvl, "flush");
800 	rule->prio = nvlist_get_number(nvl, "prio");
801 	pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL);
802 
803 	pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule);
804 
805 	rule->states_cur = nvlist_get_number(nvl, "states_cur");
806 	rule->states_tot = nvlist_get_number(nvl, "states_tot");
807 	rule->src_nodes = nvlist_get_number(nvl, "src_nodes");
808 }
809 
810 static void
pfctl_nveth_addr_to_eth_addr(const nvlist_t * nvl,struct pfctl_eth_addr * addr)811 pfctl_nveth_addr_to_eth_addr(const nvlist_t *nvl, struct pfctl_eth_addr *addr)
812 {
813 	static const u_int8_t EMPTY_MAC[ETHER_ADDR_LEN] = { 0 };
814 	size_t len;
815 	const void *data;
816 
817 	data = nvlist_get_binary(nvl, "addr", &len);
818 	assert(len == sizeof(addr->addr));
819 	memcpy(addr->addr, data, sizeof(addr->addr));
820 
821 	data = nvlist_get_binary(nvl, "mask", &len);
822 	assert(len == sizeof(addr->mask));
823 	memcpy(addr->mask, data, sizeof(addr->mask));
824 
825 	addr->neg = nvlist_get_bool(nvl, "neg");
826 
827 	/* To make checks for 'is this address set?' easier. */
828 	addr->isset = memcmp(addr->addr, EMPTY_MAC, ETHER_ADDR_LEN) != 0;
829 }
830 
831 static nvlist_t *
pfctl_eth_addr_to_nveth_addr(const struct pfctl_eth_addr * addr)832 pfctl_eth_addr_to_nveth_addr(const struct pfctl_eth_addr *addr)
833 {
834 	nvlist_t *nvl;
835 
836 	nvl = nvlist_create(0);
837 	if (nvl == NULL)
838 		return (NULL);
839 
840 	nvlist_add_bool(nvl, "neg", addr->neg);
841 	nvlist_add_binary(nvl, "addr", &addr->addr, ETHER_ADDR_LEN);
842 	nvlist_add_binary(nvl, "mask", &addr->mask, ETHER_ADDR_LEN);
843 
844 	return (nvl);
845 }
846 
847 static void
pfctl_nveth_rule_to_eth_rule(const nvlist_t * nvl,struct pfctl_eth_rule * rule)848 pfctl_nveth_rule_to_eth_rule(const nvlist_t *nvl, struct pfctl_eth_rule *rule)
849 {
850 	const char *const *labels;
851 	size_t labelcount, i;
852 
853 	rule->nr = nvlist_get_number(nvl, "nr");
854 	rule->quick = nvlist_get_bool(nvl, "quick");
855 	strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
856 	rule->ifnot = nvlist_get_bool(nvl, "ifnot");
857 	rule->direction = nvlist_get_number(nvl, "direction");
858 	rule->proto = nvlist_get_number(nvl, "proto");
859 	strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
860 	    PF_TAG_NAME_SIZE);
861 	rule->match_tag = nvlist_get_number(nvl, "match_tag");
862 	rule->match_tag_not = nvlist_get_bool(nvl, "match_tag_not");
863 
864 	labels = nvlist_get_string_array(nvl, "labels", &labelcount);
865 	assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
866 	for (i = 0; i < labelcount; i++)
867 		strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
868 	rule->ridentifier = nvlist_get_number(nvl, "ridentifier");
869 
870 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "src"),
871 	    &rule->src);
872 	pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "dst"),
873 	    &rule->dst);
874 
875 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipsrc"),
876 	    &rule->ipsrc);
877 	pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipdst"),
878 	    &rule->ipdst);
879 
880 	rule->evaluations = nvlist_get_number(nvl, "evaluations");
881 	rule->packets[0] = nvlist_get_number(nvl, "packets-in");
882 	rule->packets[1] = nvlist_get_number(nvl, "packets-out");
883 	rule->bytes[0] = nvlist_get_number(nvl, "bytes-in");
884 	rule->bytes[1] = nvlist_get_number(nvl, "bytes-out");
885 
886 	if (nvlist_exists_number(nvl, "timestamp")) {
887 		rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp");
888 	}
889 
890 	strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
891 	strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
892 	    PF_TAG_NAME_SIZE);
893 
894 	rule->dnpipe = nvlist_get_number(nvl, "dnpipe");
895 	rule->dnflags = nvlist_get_number(nvl, "dnflags");
896 
897 	rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
898 	rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
899 
900 	strlcpy(rule->bridge_to, nvlist_get_string(nvl, "bridge_to"),
901 	    IFNAMSIZ);
902 
903 	rule->action = nvlist_get_number(nvl, "action");
904 }
905 
906 int
pfctl_get_eth_rulesets_info(int dev,struct pfctl_eth_rulesets_info * ri,const char * path)907 pfctl_get_eth_rulesets_info(int dev, struct pfctl_eth_rulesets_info *ri,
908     const char *path)
909 {
910 	nvlist_t *nvl;
911 	int ret;
912 
913 	bzero(ri, sizeof(*ri));
914 
915 	nvl = nvlist_create(0);
916 	nvlist_add_string(nvl, "path", path);
917 
918 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESETS, 256, &nvl)) != 0)
919 		goto out;
920 
921 	ri->nr = nvlist_get_number(nvl, "nr");
922 
923 out:
924 	nvlist_destroy(nvl);
925 	return (ret);
926 }
927 
928 int
pfctl_get_eth_ruleset(int dev,const char * path,int nr,struct pfctl_eth_ruleset_info * ri)929 pfctl_get_eth_ruleset(int dev, const char *path, int nr,
930     struct pfctl_eth_ruleset_info *ri)
931 {
932 	nvlist_t *nvl;
933 	int ret;
934 
935 	bzero(ri, sizeof(*ri));
936 
937 	nvl = nvlist_create(0);
938 	nvlist_add_string(nvl, "path", path);
939 	nvlist_add_number(nvl, "nr", nr);
940 
941 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESET, 1024, &nvl)) != 0)
942 		goto out;
943 
944 	ri->nr = nvlist_get_number(nvl, "nr");
945 	strlcpy(ri->path, nvlist_get_string(nvl, "path"), MAXPATHLEN);
946 	strlcpy(ri->name, nvlist_get_string(nvl, "name"),
947 	    PF_ANCHOR_NAME_SIZE);
948 
949 out:
950 	nvlist_destroy(nvl);
951 	return (ret);
952 }
953 
954 int
pfctl_get_eth_rules_info(int dev,struct pfctl_eth_rules_info * rules,const char * path)955 pfctl_get_eth_rules_info(int dev, struct pfctl_eth_rules_info *rules,
956     const char *path)
957 {
958 	nvlist_t *nvl;
959 	int ret;
960 
961 	bzero(rules, sizeof(*rules));
962 
963 	nvl = nvlist_create(0);
964 	nvlist_add_string(nvl, "anchor", path);
965 
966 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULES, 1024, &nvl)) != 0)
967 		goto out;
968 
969 	rules->nr = nvlist_get_number(nvl, "nr");
970 	rules->ticket = nvlist_get_number(nvl, "ticket");
971 
972 out:
973 	nvlist_destroy(nvl);
974 	return (ret);
975 }
976 
977 int
pfctl_get_eth_rule(int dev,uint32_t nr,uint32_t ticket,const char * path,struct pfctl_eth_rule * rule,bool clear,char * anchor_call)978 pfctl_get_eth_rule(int dev, uint32_t nr, uint32_t ticket,
979     const char *path, struct pfctl_eth_rule *rule, bool clear,
980     char *anchor_call)
981 {
982 	nvlist_t *nvl;
983 	int ret;
984 
985 	nvl = nvlist_create(0);
986 
987 	nvlist_add_string(nvl, "anchor", path);
988 	nvlist_add_number(nvl, "ticket", ticket);
989 	nvlist_add_number(nvl, "nr", nr);
990 	nvlist_add_bool(nvl, "clear", clear);
991 
992 	if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULE, 4096, &nvl)) != 0)
993 		goto out;
994 
995 	pfctl_nveth_rule_to_eth_rule(nvl, rule);
996 
997 	if (anchor_call)
998 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
999 		    MAXPATHLEN);
1000 
1001 out:
1002 	nvlist_destroy(nvl);
1003 	return (ret);
1004 }
1005 
1006 int
pfctl_add_eth_rule(int dev,const struct pfctl_eth_rule * r,const char * anchor,const char * anchor_call,uint32_t ticket)1007 pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r, const char *anchor,
1008     const char *anchor_call, uint32_t ticket)
1009 {
1010 	struct pfioc_nv nv;
1011 	nvlist_t *nvl, *addr;
1012 	void *packed;
1013 	int error = 0;
1014 	size_t labelcount, size;
1015 
1016 	nvl = nvlist_create(0);
1017 
1018 	nvlist_add_number(nvl, "ticket", ticket);
1019 	nvlist_add_string(nvl, "anchor", anchor);
1020 	nvlist_add_string(nvl, "anchor_call", anchor_call);
1021 
1022 	nvlist_add_number(nvl, "nr", r->nr);
1023 	nvlist_add_bool(nvl, "quick", r->quick);
1024 	nvlist_add_string(nvl, "ifname", r->ifname);
1025 	nvlist_add_bool(nvl, "ifnot", r->ifnot);
1026 	nvlist_add_number(nvl, "direction", r->direction);
1027 	nvlist_add_number(nvl, "proto", r->proto);
1028 	nvlist_add_string(nvl, "match_tagname", r->match_tagname);
1029 	nvlist_add_bool(nvl, "match_tag_not", r->match_tag_not);
1030 
1031 	addr = pfctl_eth_addr_to_nveth_addr(&r->src);
1032 	if (addr == NULL) {
1033 		nvlist_destroy(nvl);
1034 		return (ENOMEM);
1035 	}
1036 	nvlist_add_nvlist(nvl, "src", addr);
1037 	nvlist_destroy(addr);
1038 
1039 	addr = pfctl_eth_addr_to_nveth_addr(&r->dst);
1040 	if (addr == NULL) {
1041 		nvlist_destroy(nvl);
1042 		return (ENOMEM);
1043 	}
1044 	nvlist_add_nvlist(nvl, "dst", addr);
1045 	nvlist_destroy(addr);
1046 
1047 	pfctl_nv_add_rule_addr(nvl, "ipsrc", &r->ipsrc);
1048 	pfctl_nv_add_rule_addr(nvl, "ipdst", &r->ipdst);
1049 
1050 	labelcount = 0;
1051 	while (labelcount < PF_RULE_MAX_LABEL_COUNT &&
1052 	    r->label[labelcount][0] != 0) {
1053 		nvlist_append_string_array(nvl, "labels",
1054 		    r->label[labelcount]);
1055 		labelcount++;
1056 	}
1057 	nvlist_add_number(nvl, "ridentifier", r->ridentifier);
1058 
1059 	nvlist_add_string(nvl, "qname", r->qname);
1060 	nvlist_add_string(nvl, "tagname", r->tagname);
1061 	nvlist_add_number(nvl, "dnpipe", r->dnpipe);
1062 	nvlist_add_number(nvl, "dnflags", r->dnflags);
1063 
1064 	nvlist_add_string(nvl, "bridge_to", r->bridge_to);
1065 
1066 	nvlist_add_number(nvl, "action", r->action);
1067 
1068 	packed = nvlist_pack(nvl, &size);
1069 	if (packed == NULL) {
1070 		nvlist_destroy(nvl);
1071 		return (ENOMEM);
1072 	}
1073 
1074 	nv.len = size;
1075 	nv.size = size;
1076 	nv.data = packed;
1077 
1078 	if (ioctl(dev, DIOCADDETHRULE, &nv) != 0)
1079 		error = errno;
1080 
1081 	free(packed);
1082 	nvlist_destroy(nvl);
1083 
1084 	return (error);
1085 }
1086 
1087 static void
snl_add_msg_attr_addr_wrap(struct snl_writer * nw,uint32_t type,const struct pf_addr_wrap * addr)1088 snl_add_msg_attr_addr_wrap(struct snl_writer *nw, uint32_t type, const struct pf_addr_wrap *addr)
1089 {
1090 	int off;
1091 
1092 	off = snl_add_msg_attr_nested(nw, type);
1093 
1094 	snl_add_msg_attr_ip6(nw, PF_AT_ADDR, &addr->v.a.addr.v6);
1095 	snl_add_msg_attr_ip6(nw, PF_AT_MASK, &addr->v.a.mask.v6);
1096 
1097 	if (addr->type == PF_ADDR_DYNIFTL)
1098 		snl_add_msg_attr_string(nw, PF_AT_IFNAME, addr->v.ifname);
1099 	if (addr->type == PF_ADDR_TABLE)
1100 		snl_add_msg_attr_string(nw, PF_AT_TABLENAME, addr->v.tblname);
1101 	snl_add_msg_attr_u8(nw, PF_AT_TYPE, addr->type);
1102 	snl_add_msg_attr_u8(nw, PF_AT_IFLAGS, addr->iflags);
1103 
1104 	snl_end_attr_nested(nw, off);
1105 }
1106 
1107 static void
snl_add_msg_attr_pool_addr(struct snl_writer * nw,uint32_t type,const struct pf_pooladdr * pa)1108 snl_add_msg_attr_pool_addr(struct snl_writer *nw, uint32_t type, const struct pf_pooladdr *pa)
1109 {
1110 	int off;
1111 
1112 	off = snl_add_msg_attr_nested(nw, type);
1113 
1114 	snl_add_msg_attr_string(nw, PF_PA_IFNAME, pa->ifname);
1115 	snl_add_msg_attr_addr_wrap(nw, PF_PA_ADDR, &pa->addr);
1116 
1117 	snl_end_attr_nested(nw, off);
1118 }
1119 
1120 static void
snl_add_msg_attr_rule_addr(struct snl_writer * nw,uint32_t type,const struct pf_rule_addr * addr)1121 snl_add_msg_attr_rule_addr(struct snl_writer *nw, uint32_t type, const struct pf_rule_addr *addr)
1122 {
1123 	int off;
1124 
1125 	off = snl_add_msg_attr_nested(nw, type);
1126 
1127 	snl_add_msg_attr_addr_wrap(nw, PF_RAT_ADDR, &addr->addr);
1128 	snl_add_msg_attr_u16(nw, PF_RAT_SRC_PORT, addr->port[0]);
1129 	snl_add_msg_attr_u16(nw, PF_RAT_DST_PORT, addr->port[1]);
1130 	snl_add_msg_attr_u8(nw, PF_RAT_NEG, addr->neg);
1131 	snl_add_msg_attr_u8(nw, PF_RAT_OP, addr->port_op);
1132 
1133 	snl_end_attr_nested(nw, off);
1134 }
1135 
1136 static void
snl_add_msg_attr_rule_labels(struct snl_writer * nw,uint32_t type,const char labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE])1137 snl_add_msg_attr_rule_labels(struct snl_writer *nw, uint32_t type, const char labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE])
1138 {
1139 	int off, i = 0;
1140 
1141 	off = snl_add_msg_attr_nested(nw, type);
1142 
1143 	while (i < PF_RULE_MAX_LABEL_COUNT &&
1144 	    labels[i][0] != 0) {
1145 		snl_add_msg_attr_string(nw, PF_LT_LABEL, labels[i]);
1146 		i++;
1147 	}
1148 
1149 	snl_end_attr_nested(nw, off);
1150 }
1151 
1152 static void
snl_add_msg_attr_mape(struct snl_writer * nw,uint32_t type,const struct pf_mape_portset * me)1153 snl_add_msg_attr_mape(struct snl_writer *nw, uint32_t type, const struct pf_mape_portset *me)
1154 {
1155 	int off;
1156 
1157 	off = snl_add_msg_attr_nested(nw, type);
1158 
1159 	snl_add_msg_attr_u8(nw, PF_MET_OFFSET, me->offset);
1160 	snl_add_msg_attr_u8(nw, PF_MET_PSID_LEN, me->psidlen);
1161 	snl_add_msg_attr_u16(nw, PF_MET_PSID, me->psid);
1162 
1163 	snl_end_attr_nested(nw, off);
1164 }
1165 
1166 static void
snl_add_msg_attr_rpool(struct snl_writer * nw,uint32_t type,const struct pfctl_pool * pool)1167 snl_add_msg_attr_rpool(struct snl_writer *nw, uint32_t type, const struct pfctl_pool *pool)
1168 {
1169 	int off;
1170 
1171 	off = snl_add_msg_attr_nested(nw, type);
1172 
1173 	snl_add_msg_attr(nw, PF_PT_KEY, sizeof(pool->key), &pool->key);
1174 	snl_add_msg_attr_ip6(nw, PF_PT_COUNTER, &pool->counter.v6);
1175 	snl_add_msg_attr_u32(nw, PF_PT_TBLIDX, pool->tblidx);
1176 	snl_add_msg_attr_u16(nw, PF_PT_PROXY_SRC_PORT, pool->proxy_port[0]);
1177 	snl_add_msg_attr_u16(nw, PF_PT_PROXY_DST_PORT, pool->proxy_port[1]);
1178 	snl_add_msg_attr_u8(nw, PF_PT_OPTS, pool->opts);
1179 	snl_add_msg_attr_mape(nw, PF_PT_MAPE, &pool->mape);
1180 
1181 	snl_end_attr_nested(nw, off);
1182 }
1183 
1184 static void
snl_add_msg_attr_timeouts(struct snl_writer * nw,uint32_t type,const uint32_t * timeouts)1185 snl_add_msg_attr_timeouts(struct snl_writer *nw, uint32_t type, const uint32_t *timeouts)
1186 {
1187 	int off;
1188 
1189 	off = snl_add_msg_attr_nested(nw, type);
1190 
1191 	for (int i = 0; i < PFTM_MAX; i++)
1192 		snl_add_msg_attr_u32(nw, PF_TT_TIMEOUT, timeouts[i]);
1193 
1194 	snl_end_attr_nested(nw, off);
1195 }
1196 
1197 static void
snl_add_msg_attr_uid(struct snl_writer * nw,uint32_t type,const struct pf_rule_uid * uid)1198 snl_add_msg_attr_uid(struct snl_writer *nw, uint32_t type, const struct pf_rule_uid *uid)
1199 {
1200 	int off;
1201 
1202 	off = snl_add_msg_attr_nested(nw, type);
1203 
1204 	snl_add_msg_attr_u32(nw, PF_RUT_UID_LOW, uid->uid[0]);
1205 	snl_add_msg_attr_u32(nw, PF_RUT_UID_HIGH, uid->uid[1]);
1206 	snl_add_msg_attr_u8(nw, PF_RUT_OP, uid->op);
1207 
1208 	snl_end_attr_nested(nw, off);
1209 }
1210 
1211 static void
snl_add_msg_attr_pf_rule(struct snl_writer * nw,uint32_t type,const struct pfctl_rule * r)1212 snl_add_msg_attr_pf_rule(struct snl_writer *nw, uint32_t type, const struct pfctl_rule *r)
1213 {
1214 	int off;
1215 
1216 	off = snl_add_msg_attr_nested(nw, type);
1217 
1218 	snl_add_msg_attr_rule_addr(nw, PF_RT_SRC, &r->src);
1219 	snl_add_msg_attr_rule_addr(nw, PF_RT_DST, &r->dst);
1220 	snl_add_msg_attr_rule_labels(nw, PF_RT_LABELS, r->label);
1221 	snl_add_msg_attr_u32(nw, PF_RT_RIDENTIFIER, r->ridentifier);
1222 	snl_add_msg_attr_string(nw, PF_RT_IFNAME, r->ifname);
1223 	snl_add_msg_attr_string(nw, PF_RT_QNAME, r->qname);
1224 	snl_add_msg_attr_string(nw, PF_RT_PQNAME, r->pqname);
1225 	snl_add_msg_attr_string(nw, PF_RT_TAGNAME, r->tagname);
1226 	snl_add_msg_attr_string(nw, PF_RT_MATCH_TAGNAME, r->match_tagname);
1227 	snl_add_msg_attr_string(nw, PF_RT_OVERLOAD_TBLNAME, r->overload_tblname);
1228 	snl_add_msg_attr_rpool(nw, PF_RT_RPOOL_RDR, &r->rdr);
1229 	snl_add_msg_attr_rpool(nw, PF_RT_RPOOL_NAT, &r->nat);
1230 	snl_add_msg_attr_rpool(nw, PF_RT_RPOOL_RT, &r->route);
1231 	snl_add_msg_attr_u32(nw, PF_RT_OS_FINGERPRINT, r->os_fingerprint);
1232 	snl_add_msg_attr_u32(nw, PF_RT_RTABLEID, r->rtableid);
1233 	snl_add_msg_attr_timeouts(nw, PF_RT_TIMEOUT, r->timeout);
1234 	snl_add_msg_attr_u32(nw, PF_RT_MAX_STATES, r->max_states);
1235 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_NODES, r->max_src_nodes);
1236 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_STATES, r->max_src_states);
1237 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN, r->max_src_conn);
1238 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_LIMIT, r->max_src_conn_rate.limit);
1239 	snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_SECS, r->max_src_conn_rate.seconds);
1240 
1241 	snl_add_msg_attr_u16(nw, PF_RT_DNPIPE, r->dnpipe);
1242 	snl_add_msg_attr_u16(nw, PF_RT_DNRPIPE, r->dnrpipe);
1243 	snl_add_msg_attr_u32(nw, PF_RT_DNFLAGS, r->free_flags);
1244 
1245 	snl_add_msg_attr_u32(nw, PF_RT_NR, r->nr);
1246 	snl_add_msg_attr_u32(nw, PF_RT_PROB, r->prob);
1247 	snl_add_msg_attr_u32(nw, PF_RT_CUID, r->cuid);
1248 	snl_add_msg_attr_u32(nw, PF_RT_CPID, r->cpid);
1249 
1250 	snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP, r->return_icmp);
1251 	snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP6, r->return_icmp6);
1252 	snl_add_msg_attr_u16(nw, PF_RT_MAX_MSS, r->max_mss);
1253 	snl_add_msg_attr_u16(nw, PF_RT_SCRUB_FLAGS, r->scrub_flags);
1254 
1255 	snl_add_msg_attr_uid(nw, PF_RT_UID, &r->uid);
1256 	snl_add_msg_attr_uid(nw, PF_RT_GID, (const struct pf_rule_uid *)&r->gid);
1257 	snl_add_msg_attr_string(nw, PF_RT_RCV_IFNAME, r->rcv_ifname);
1258 
1259 	snl_add_msg_attr_u32(nw, PF_RT_RULE_FLAG, r->rule_flag);
1260 	snl_add_msg_attr_u8(nw, PF_RT_ACTION, r->action);
1261 	snl_add_msg_attr_u8(nw, PF_RT_DIRECTION, r->direction);
1262 	snl_add_msg_attr_u8(nw, PF_RT_LOG, r->log);
1263 	snl_add_msg_attr_u8(nw, PF_RT_LOGIF, r->logif);
1264 	snl_add_msg_attr_u8(nw, PF_RT_QUICK, r->quick);
1265 	snl_add_msg_attr_u8(nw, PF_RT_IF_NOT, r->ifnot);
1266 	snl_add_msg_attr_u8(nw, PF_RT_MATCH_TAG_NOT, r->match_tag_not);
1267 	snl_add_msg_attr_u8(nw, PF_RT_NATPASS, r->natpass);
1268 	snl_add_msg_attr_u8(nw, PF_RT_KEEP_STATE, r->keep_state);
1269 	snl_add_msg_attr_u8(nw, PF_RT_AF, r->af);
1270 	snl_add_msg_attr_u8(nw, PF_RT_PROTO, r->proto);
1271 	snl_add_msg_attr_u8(nw, PF_RT_TYPE, r->type);
1272 	snl_add_msg_attr_u8(nw, PF_RT_CODE, r->code);
1273 	snl_add_msg_attr_u8(nw, PF_RT_FLAGS, r->flags);
1274 	snl_add_msg_attr_u8(nw, PF_RT_FLAGSET, r->flagset);
1275 	snl_add_msg_attr_u8(nw, PF_RT_MIN_TTL, r->min_ttl);
1276 	snl_add_msg_attr_u8(nw, PF_RT_ALLOW_OPTS, r->allow_opts);
1277 	snl_add_msg_attr_u8(nw, PF_RT_RT, r->rt);
1278 	snl_add_msg_attr_u8(nw, PF_RT_RETURN_TTL, r->return_ttl);
1279 	snl_add_msg_attr_u8(nw, PF_RT_TOS, r->tos);
1280 	snl_add_msg_attr_u8(nw, PF_RT_SET_TOS, r->set_tos);
1281 
1282 	snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_RELATIVE, r->anchor_relative);
1283 	snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_WILDCARD, r->anchor_wildcard);
1284 	snl_add_msg_attr_u8(nw, PF_RT_FLUSH, r->flush);
1285 	snl_add_msg_attr_u8(nw, PF_RT_PRIO, r->prio);
1286 	snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO, r->set_prio[0]);
1287 	snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO_REPLY, r->set_prio[1]);
1288 	snl_add_msg_attr_u8(nw, PF_RT_NAF, r->naf);
1289 
1290 	snl_add_msg_attr_ip6(nw, PF_RT_DIVERT_ADDRESS, &r->divert.addr.v6);
1291 	snl_add_msg_attr_u16(nw, PF_RT_DIVERT_PORT, r->divert.port);
1292 
1293 	snl_end_attr_nested(nw, off);
1294 }
1295 
1296 int
pfctl_add_rule(int dev __unused,const struct pfctl_rule * r,const char * anchor,const char * anchor_call,uint32_t ticket,uint32_t pool_ticket)1297 pfctl_add_rule(int dev __unused, const struct pfctl_rule *r, const char *anchor,
1298     const char *anchor_call, uint32_t ticket, uint32_t pool_ticket)
1299 {
1300 	struct pfctl_handle *h;
1301 	int ret;
1302 
1303 	h = pfctl_open(PF_DEVICE);
1304 	if (h == NULL)
1305 		return (ENODEV);
1306 
1307 	ret = pfctl_add_rule_h(h, r, anchor, anchor_call, ticket, pool_ticket);
1308 
1309 	pfctl_close(h);
1310 
1311 	return (ret);
1312 }
1313 
1314 int
pfctl_add_rule_h(struct pfctl_handle * h,const struct pfctl_rule * r,const char * anchor,const char * anchor_call,uint32_t ticket,uint32_t pool_ticket)1315 pfctl_add_rule_h(struct pfctl_handle *h, const struct pfctl_rule *r,
1316 	    const char *anchor, const char *anchor_call, uint32_t ticket,
1317 	    uint32_t pool_ticket)
1318 {
1319 	struct snl_writer nw;
1320 	struct snl_errmsg_data e = {};
1321 	struct nlmsghdr *hdr;
1322 	uint32_t seq_id;
1323 	int family_id;
1324 
1325 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
1326 	if (family_id == 0)
1327 		return (ENOTSUP);
1328 
1329 	snl_init_writer(&h->ss, &nw);
1330 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_ADDRULE);
1331 	hdr->nlmsg_flags |= NLM_F_DUMP;
1332 	snl_add_msg_attr_u32(&nw, PF_ART_TICKET, ticket);
1333 	snl_add_msg_attr_u32(&nw, PF_ART_POOL_TICKET, pool_ticket);
1334 	snl_add_msg_attr_string(&nw, PF_ART_ANCHOR, anchor);
1335 	snl_add_msg_attr_string(&nw, PF_ART_ANCHOR_CALL, anchor_call);
1336 
1337 	snl_add_msg_attr_pf_rule(&nw, PF_ART_RULE, r);
1338 
1339 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
1340 		return (ENXIO);
1341 
1342 	seq_id = hdr->nlmsg_seq;
1343 
1344 	if (! snl_send_message(&h->ss, hdr))
1345 		return (ENXIO);
1346 
1347 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
1348 	}
1349 
1350 	return (e.error);
1351 }
1352 
1353 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1354 #define	_OUT(_field)	offsetof(struct pfctl_rules_info, _field)
1355 static struct snl_attr_parser ap_getrules[] = {
1356 	{ .type = PF_GR_NR, .off = _OUT(nr), .cb = snl_attr_get_uint32 },
1357 	{ .type = PF_GR_TICKET, .off = _OUT(ticket), .cb = snl_attr_get_uint32 },
1358 };
1359 #undef _IN
1360 #undef _OUT
1361 SNL_DECLARE_PARSER(getrules_parser, struct genlmsghdr, snl_f_p_empty, ap_getrules);
1362 
1363 int
pfctl_get_rules_info_h(struct pfctl_handle * h,struct pfctl_rules_info * rules,uint32_t ruleset,const char * path)1364 pfctl_get_rules_info_h(struct pfctl_handle *h, struct pfctl_rules_info *rules, uint32_t ruleset,
1365     const char *path)
1366 {
1367 	struct snl_errmsg_data e = {};
1368 	struct nlmsghdr *hdr;
1369 	struct snl_writer nw;
1370 	uint32_t seq_id;
1371 	int family_id;
1372 
1373 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
1374 	if (family_id == 0)
1375 		return (ENOTSUP);
1376 
1377 	snl_init_writer(&h->ss, &nw);
1378 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETRULES);
1379 	hdr->nlmsg_flags |= NLM_F_DUMP;
1380 
1381 	snl_add_msg_attr_string(&nw, PF_GR_ANCHOR, path);
1382 	snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset);
1383 
1384 	hdr = snl_finalize_msg(&nw);
1385 	if (hdr == NULL)
1386 		return (ENOMEM);
1387 
1388 	seq_id = hdr->nlmsg_seq;
1389 	if (! snl_send_message(&h->ss, hdr))
1390 		return (ENXIO);
1391 
1392 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
1393 		if (! snl_parse_nlmsg(&h->ss, hdr, &getrules_parser, rules))
1394 			continue;
1395 	}
1396 
1397 	return (e.error);
1398 }
1399 
1400 int
pfctl_get_rules_info(int dev __unused,struct pfctl_rules_info * rules,uint32_t ruleset,const char * path)1401 pfctl_get_rules_info(int dev __unused, struct pfctl_rules_info *rules, uint32_t ruleset,
1402     const char *path)
1403 {
1404 	struct pfctl_handle *h;
1405 	int error;
1406 
1407 	h = pfctl_open(PF_DEVICE);
1408 	if (h == NULL)
1409 		return (ENOTSUP);
1410 	error = pfctl_get_rules_info_h(h, rules, ruleset, path);
1411 	pfctl_close(h);
1412 
1413 	return (error);
1414 }
1415 
1416 int
pfctl_get_rule_h(struct pfctl_handle * h,uint32_t nr,uint32_t ticket,const char * anchor,uint32_t ruleset,struct pfctl_rule * rule,char * anchor_call)1417 pfctl_get_rule_h(struct pfctl_handle *h, uint32_t nr, uint32_t ticket, const char *anchor,
1418     uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
1419 {
1420 	return (pfctl_get_clear_rule_h(h, nr, ticket, anchor, ruleset, rule,
1421 	    anchor_call, false));
1422 }
1423 
1424 int
pfctl_get_rule(int dev,uint32_t nr,uint32_t ticket,const char * anchor,uint32_t ruleset,struct pfctl_rule * rule,char * anchor_call)1425 pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket, const char *anchor,
1426     uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
1427 {
1428 	return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule,
1429 	    anchor_call, false));
1430 }
1431 
1432 #define _OUT(_field)	offsetof(struct pf_addr_wrap, _field)
1433 static const struct snl_attr_parser ap_addr_wrap[] = {
1434 	{ .type = PF_AT_ADDR, .off = _OUT(v.a.addr), .cb = snl_attr_get_in6_addr },
1435 	{ .type = PF_AT_MASK, .off = _OUT(v.a.mask), .cb = snl_attr_get_in6_addr },
1436 	{ .type = PF_AT_IFNAME, .off = _OUT(v.ifname), .arg = (void *)IFNAMSIZ,.cb = snl_attr_copy_string },
1437 	{ .type = PF_AT_TABLENAME, .off = _OUT(v.tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = snl_attr_copy_string },
1438 	{ .type = PF_AT_TYPE, .off = _OUT(type), .cb = snl_attr_get_uint8 },
1439 	{ .type = PF_AT_IFLAGS, .off = _OUT(iflags), .cb = snl_attr_get_uint8 },
1440 	{ .type = PF_AT_TBLCNT, .off = _OUT(p.tblcnt), .cb = snl_attr_get_uint32 },
1441 	{ .type = PF_AT_DYNCNT, .off = _OUT(p.dyncnt), .cb = snl_attr_get_uint32 },
1442 };
1443 SNL_DECLARE_ATTR_PARSER(addr_wrap_parser, ap_addr_wrap);
1444 #undef _OUT
1445 
1446 #define _OUT(_field)	offsetof(struct pf_rule_addr, _field)
1447 static struct snl_attr_parser ap_rule_addr[] = {
1448 	{ .type = PF_RAT_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = snl_attr_get_nested },
1449 	{ .type = PF_RAT_SRC_PORT, .off = _OUT(port[0]), .cb = snl_attr_get_uint16 },
1450 	{ .type = PF_RAT_DST_PORT, .off = _OUT(port[1]), .cb = snl_attr_get_uint16 },
1451 	{ .type = PF_RAT_NEG, .off = _OUT(neg), .cb = snl_attr_get_uint8 },
1452 	{ .type = PF_RAT_OP, .off = _OUT(port_op), .cb = snl_attr_get_uint8 },
1453 };
1454 #undef _OUT
1455 SNL_DECLARE_ATTR_PARSER(rule_addr_parser, ap_rule_addr);
1456 
1457 struct snl_parsed_labels
1458 {
1459 	char		labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
1460 	uint32_t	i;
1461 };
1462 
1463 static bool
snl_attr_get_pf_rule_labels(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)1464 snl_attr_get_pf_rule_labels(struct snl_state *ss, struct nlattr *nla,
1465     const void *arg __unused, void *target)
1466 {
1467 	struct snl_parsed_labels *l = (struct snl_parsed_labels *)target;
1468 	bool ret;
1469 
1470 	if (l->i >= PF_RULE_MAX_LABEL_COUNT)
1471 		return (E2BIG);
1472 
1473 	ret = snl_attr_copy_string(ss, nla, (void *)PF_RULE_LABEL_SIZE,
1474 	    l->labels[l->i]);
1475 	if (ret)
1476 		l->i++;
1477 
1478 	return (ret);
1479 }
1480 
1481 #define _OUT(_field)	offsetof(struct nl_parsed_labels, _field)
1482 static const struct snl_attr_parser ap_labels[] = {
1483 	{ .type = PF_LT_LABEL, .off = 0, .cb = snl_attr_get_pf_rule_labels },
1484 };
1485 SNL_DECLARE_ATTR_PARSER(rule_labels_parser, ap_labels);
1486 #undef _OUT
1487 
1488 static bool
snl_attr_get_nested_pf_rule_labels(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)1489 snl_attr_get_nested_pf_rule_labels(struct snl_state *ss, struct nlattr *nla,
1490     const void *arg __unused, void *target)
1491 {
1492 	struct snl_parsed_labels parsed_labels = { };
1493 	bool error;
1494 
1495 	/* Assumes target points to the beginning of the structure */
1496 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &rule_labels_parser, &parsed_labels);
1497 	if (! error)
1498 		return (error);
1499 
1500 	memcpy(target, parsed_labels.labels, sizeof(parsed_labels.labels));
1501 
1502 	return (true);
1503 }
1504 
1505 #define _OUT(_field)	offsetof(struct pf_mape_portset, _field)
1506 static const struct snl_attr_parser ap_mape_portset[] = {
1507 	{ .type = PF_MET_OFFSET, .off = _OUT(offset), .cb = snl_attr_get_uint8 },
1508 	{ .type = PF_MET_PSID_LEN, .off = _OUT(psidlen), .cb = snl_attr_get_uint8 },
1509 	{. type = PF_MET_PSID, .off = _OUT(psid), .cb = snl_attr_get_uint16 },
1510 };
1511 SNL_DECLARE_ATTR_PARSER(mape_portset_parser, ap_mape_portset);
1512 #undef _OUT
1513 
1514 #define _OUT(_field)	offsetof(struct pfctl_pool, _field)
1515 static const struct snl_attr_parser ap_pool[] = {
1516 	{ .type = PF_PT_KEY, .off = _OUT(key), .arg = (void *)sizeof(struct pf_poolhashkey), .cb = snl_attr_get_bytes },
1517 	{ .type = PF_PT_COUNTER, .off = _OUT(counter), .cb = snl_attr_get_in6_addr },
1518 	{ .type = PF_PT_TBLIDX, .off = _OUT(tblidx), .cb = snl_attr_get_uint32 },
1519 	{ .type = PF_PT_PROXY_SRC_PORT, .off = _OUT(proxy_port[0]), .cb = snl_attr_get_uint16 },
1520 	{ .type = PF_PT_PROXY_DST_PORT, .off = _OUT(proxy_port[1]), .cb = snl_attr_get_uint16 },
1521 	{ .type = PF_PT_OPTS, .off = _OUT(opts), .cb = snl_attr_get_uint8 },
1522 	{ .type = PF_PT_MAPE, .off = _OUT(mape), .arg = &mape_portset_parser, .cb = snl_attr_get_nested },
1523 };
1524 SNL_DECLARE_ATTR_PARSER(pool_parser, ap_pool);
1525 #undef _OUT
1526 
1527 struct nl_parsed_timeouts
1528 {
1529 	uint32_t	timeouts[PFTM_MAX];
1530 	uint32_t	i;
1531 };
1532 
1533 static bool
snl_attr_get_pf_timeout(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)1534 snl_attr_get_pf_timeout(struct snl_state *ss, struct nlattr *nla,
1535     const void *arg __unused, void *target)
1536 {
1537 	struct nl_parsed_timeouts *t = (struct nl_parsed_timeouts *)target;
1538 	bool ret;
1539 
1540 	if (t->i >= PFTM_MAX)
1541 		return (E2BIG);
1542 
1543 	ret = snl_attr_get_uint32(ss, nla, NULL, &t->timeouts[t->i]);
1544 	if (ret)
1545 		t->i++;
1546 
1547 	return (ret);
1548 }
1549 
1550 #define _OUT(_field)	offsetof(struct nl_parsed_timeout, _field)
1551 static const struct snl_attr_parser ap_timeouts[] = {
1552 	{ .type = PF_TT_TIMEOUT, .off = 0, .cb = snl_attr_get_pf_timeout },
1553 };
1554 SNL_DECLARE_ATTR_PARSER(timeout_parser, ap_timeouts);
1555 #undef _OUT
1556 
1557 static bool
snl_attr_get_nested_timeouts(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)1558 snl_attr_get_nested_timeouts(struct snl_state *ss, struct nlattr *nla,
1559     const void *arg __unused, void *target)
1560 {
1561 	struct nl_parsed_timeouts parsed_timeouts = { };
1562 	bool error;
1563 
1564 	/* Assumes target points to the beginning of the structure */
1565 	error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &timeout_parser, &parsed_timeouts);
1566 	if (! error)
1567 		return (error);
1568 
1569 	memcpy(target, parsed_timeouts.timeouts, sizeof(parsed_timeouts.timeouts));
1570 
1571 	return (true);
1572 }
1573 
1574 #define _OUT(_field)	offsetof(struct pf_rule_uid, _field)
1575 static const struct snl_attr_parser ap_rule_uid[] = {
1576 	{ .type = PF_RUT_UID_LOW, .off = _OUT(uid[0]), .cb = snl_attr_get_uint32 },
1577 	{ .type = PF_RUT_UID_HIGH, .off = _OUT(uid[1]), .cb = snl_attr_get_uint32 },
1578 	{ .type = PF_RUT_OP, .off = _OUT(op), .cb = snl_attr_get_uint8 },
1579 };
1580 SNL_DECLARE_ATTR_PARSER(rule_uid_parser, ap_rule_uid);
1581 #undef _OUT
1582 
1583 struct pfctl_nl_get_rule {
1584 	struct pfctl_rule r;
1585 	char anchor_call[MAXPATHLEN];
1586 };
1587 #define	_OUT(_field)	offsetof(struct pfctl_nl_get_rule, _field)
1588 static struct snl_attr_parser ap_getrule[] = {
1589 	{ .type = PF_RT_SRC, .off = _OUT(r.src), .arg = &rule_addr_parser,.cb = snl_attr_get_nested },
1590 	{ .type = PF_RT_DST, .off = _OUT(r.dst), .arg = &rule_addr_parser,.cb = snl_attr_get_nested },
1591 	{ .type = PF_RT_RIDENTIFIER, .off = _OUT(r.ridentifier), .cb = snl_attr_get_uint32 },
1592 	{ .type = PF_RT_LABELS, .off = _OUT(r.label), .arg = &rule_labels_parser,.cb = snl_attr_get_nested_pf_rule_labels },
1593 	{ .type = PF_RT_IFNAME, .off = _OUT(r.ifname), .arg = (void *)IFNAMSIZ, .cb = snl_attr_copy_string },
1594 	{ .type = PF_RT_QNAME, .off = _OUT(r.qname), .arg = (void *)PF_QNAME_SIZE, .cb = snl_attr_copy_string },
1595 	{ .type = PF_RT_PQNAME, .off = _OUT(r.pqname), .arg = (void *)PF_QNAME_SIZE, .cb = snl_attr_copy_string },
1596 	{ .type = PF_RT_TAGNAME, .off = _OUT(r.tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = snl_attr_copy_string },
1597 	{ .type = PF_RT_MATCH_TAGNAME, .off = _OUT(r.match_tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = snl_attr_copy_string },
1598 	{ .type = PF_RT_OVERLOAD_TBLNAME, .off = _OUT(r.overload_tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = snl_attr_copy_string },
1599 	{ .type = PF_RT_RPOOL_RDR, .off = _OUT(r.rdr), .arg = &pool_parser, .cb = snl_attr_get_nested },
1600 	{ .type = PF_RT_OS_FINGERPRINT, .off = _OUT(r.os_fingerprint), .cb = snl_attr_get_uint32 },
1601 	{ .type = PF_RT_RTABLEID, .off = _OUT(r.rtableid), .cb = snl_attr_get_uint32 },
1602 	{ .type = PF_RT_TIMEOUT, .off = _OUT(r.timeout), .arg = &timeout_parser, .cb = snl_attr_get_nested_timeouts },
1603 	{ .type = PF_RT_MAX_STATES, .off = _OUT(r.max_states), .cb = snl_attr_get_uint32 },
1604 	{ .type = PF_RT_MAX_SRC_NODES, .off = _OUT(r.max_src_nodes), .cb = snl_attr_get_uint32 },
1605 	{ .type = PF_RT_MAX_SRC_STATES, .off = _OUT(r.max_src_states), .cb = snl_attr_get_uint32 },
1606 	{ .type = PF_RT_MAX_SRC_CONN_RATE_LIMIT, .off = _OUT(r.max_src_conn_rate.limit), .cb = snl_attr_get_uint32 },
1607 	{ .type = PF_RT_MAX_SRC_CONN_RATE_SECS, .off = _OUT(r.max_src_conn_rate.seconds), .cb = snl_attr_get_uint32 },
1608 	{ .type = PF_RT_DNPIPE, .off = _OUT(r.dnpipe), .cb = snl_attr_get_uint16 },
1609 	{ .type = PF_RT_DNRPIPE, .off = _OUT(r.dnrpipe), .cb = snl_attr_get_uint16 },
1610 	{ .type = PF_RT_DNFLAGS, .off = _OUT(r.free_flags), .cb = snl_attr_get_uint32 },
1611 	{ .type = PF_RT_NR, .off = _OUT(r.nr), .cb = snl_attr_get_uint32 },
1612 	{ .type = PF_RT_PROB, .off = _OUT(r.prob), .cb = snl_attr_get_uint32 },
1613 	{ .type = PF_RT_CUID, .off = _OUT(r.cuid), .cb = snl_attr_get_uint32 },
1614 	{. type = PF_RT_CPID, .off = _OUT(r.cpid), .cb = snl_attr_get_uint32 },
1615 	{ .type = PF_RT_RETURN_ICMP, .off = _OUT(r.return_icmp), .cb = snl_attr_get_uint16 },
1616 	{ .type = PF_RT_RETURN_ICMP6, .off = _OUT(r.return_icmp6), .cb = snl_attr_get_uint16 },
1617 	{ .type = PF_RT_MAX_MSS, .off = _OUT(r.max_mss), .cb = snl_attr_get_uint16 },
1618 	{ .type = PF_RT_SCRUB_FLAGS, .off = _OUT(r.scrub_flags), .cb = snl_attr_get_uint16 },
1619 	{ .type = PF_RT_UID, .off = _OUT(r.uid), .arg = &rule_uid_parser, .cb = snl_attr_get_nested },
1620 	{ .type = PF_RT_GID, .off = _OUT(r.gid), .arg = &rule_uid_parser, .cb = snl_attr_get_nested },
1621 	{ .type = PF_RT_RULE_FLAG, .off = _OUT(r.rule_flag), .cb = snl_attr_get_uint32 },
1622 	{ .type = PF_RT_ACTION, .off = _OUT(r.action), .cb = snl_attr_get_uint8 },
1623 	{ .type = PF_RT_DIRECTION, .off = _OUT(r.direction), .cb = snl_attr_get_uint8 },
1624 	{ .type = PF_RT_LOG, .off = _OUT(r.log), .cb = snl_attr_get_uint8 },
1625 	{ .type = PF_RT_LOGIF, .off = _OUT(r.logif), .cb = snl_attr_get_uint8 },
1626 	{ .type = PF_RT_QUICK, .off = _OUT(r.quick), .cb = snl_attr_get_uint8 },
1627 	{ .type = PF_RT_IF_NOT, .off = _OUT(r.ifnot), .cb = snl_attr_get_uint8 },
1628 	{ .type = PF_RT_MATCH_TAG_NOT, .off = _OUT(r.match_tag_not), .cb = snl_attr_get_uint8 },
1629 	{ .type = PF_RT_NATPASS, .off = _OUT(r.natpass), .cb = snl_attr_get_uint8 },
1630 	{ .type = PF_RT_KEEP_STATE, .off = _OUT(r.keep_state), .cb = snl_attr_get_uint8 },
1631 	{ .type = PF_RT_AF, .off = _OUT(r.af), .cb = snl_attr_get_uint8 },
1632 	{ .type = PF_RT_PROTO, .off = _OUT(r.proto), .cb = snl_attr_get_uint8 },
1633 	{ .type = PF_RT_TYPE, .off = _OUT(r.type), .cb = snl_attr_get_uint8 },
1634 	{ .type = PF_RT_CODE, .off = _OUT(r.code), .cb = snl_attr_get_uint8 },
1635 	{ .type = PF_RT_FLAGS, .off = _OUT(r.flags), .cb = snl_attr_get_uint8 },
1636 	{ .type = PF_RT_FLAGSET, .off = _OUT(r.flagset), .cb = snl_attr_get_uint8 },
1637 	{ .type = PF_RT_MIN_TTL, .off = _OUT(r.min_ttl), .cb = snl_attr_get_uint8 },
1638 	{ .type = PF_RT_ALLOW_OPTS, .off = _OUT(r.allow_opts), .cb = snl_attr_get_uint8 },
1639 	{ .type = PF_RT_RT, .off = _OUT(r.rt), .cb = snl_attr_get_uint8 },
1640 	{ .type = PF_RT_RETURN_TTL, .off = _OUT(r.return_ttl), .cb = snl_attr_get_uint8 },
1641 	{ .type = PF_RT_TOS, .off = _OUT(r.tos), .cb = snl_attr_get_uint8 },
1642 	{ .type = PF_RT_SET_TOS, .off = _OUT(r.set_tos), .cb = snl_attr_get_uint8 },
1643 	{ .type = PF_RT_ANCHOR_RELATIVE, .off = _OUT(r.anchor_relative), .cb = snl_attr_get_uint8 },
1644 	{ .type = PF_RT_ANCHOR_WILDCARD, .off = _OUT(r.anchor_wildcard), .cb = snl_attr_get_uint8 },
1645 	{ .type = PF_RT_FLUSH, .off = _OUT(r.flush), .cb = snl_attr_get_uint8 },
1646 	{ .type = PF_RT_PRIO, .off = _OUT(r.prio), .cb = snl_attr_get_uint8 },
1647 	{ .type = PF_RT_SET_PRIO, .off = _OUT(r.set_prio[0]), .cb = snl_attr_get_uint8 },
1648 	{ .type = PF_RT_SET_PRIO_REPLY, .off = _OUT(r.set_prio[1]), .cb = snl_attr_get_uint8 },
1649 	{ .type = PF_RT_DIVERT_ADDRESS, .off = _OUT(r.divert.addr), .cb = snl_attr_get_in6_addr },
1650 	{ .type = PF_RT_DIVERT_PORT, .off = _OUT(r.divert.port), .cb = snl_attr_get_uint16 },
1651 	{ .type = PF_RT_PACKETS_IN, .off = _OUT(r.packets[0]), .cb = snl_attr_get_uint64 },
1652 	{ .type = PF_RT_PACKETS_OUT, .off = _OUT(r.packets[1]), .cb = snl_attr_get_uint64 },
1653 	{ .type = PF_RT_BYTES_IN, .off = _OUT(r.bytes[0]), .cb = snl_attr_get_uint64 },
1654 	{ .type = PF_RT_BYTES_OUT, .off = _OUT(r.bytes[1]), .cb = snl_attr_get_uint64 },
1655 	{ .type = PF_RT_EVALUATIONS, .off = _OUT(r.evaluations), .cb = snl_attr_get_uint64 },
1656 	{ .type = PF_RT_TIMESTAMP, .off = _OUT(r.last_active_timestamp), .cb = snl_attr_get_uint64 },
1657 	{ .type = PF_RT_STATES_CUR, .off = _OUT(r.states_cur), .cb = snl_attr_get_uint64 },
1658 	{ .type = PF_RT_STATES_TOTAL, .off = _OUT(r.states_tot), .cb = snl_attr_get_uint64 },
1659 	{ .type = PF_RT_SRC_NODES, .off = _OUT(r.src_nodes), .cb = snl_attr_get_uint64 },
1660 	{ .type = PF_RT_ANCHOR_CALL, .off = _OUT(anchor_call), .arg = (void*)MAXPATHLEN, .cb = snl_attr_copy_string },
1661 	{ .type = PF_RT_RCV_IFNAME, .off = _OUT(r.rcv_ifname), .arg = (void*)IFNAMSIZ, .cb = snl_attr_copy_string },
1662 	{ .type = PF_RT_MAX_SRC_CONN, .off = _OUT(r.max_src_conn), .cb = snl_attr_get_uint32 },
1663 	{ .type = PF_RT_RPOOL_NAT, .off = _OUT(r.nat), .arg = &pool_parser, .cb = snl_attr_get_nested },
1664 	{ .type = PF_RT_NAF, .off = _OUT(r.naf), .cb = snl_attr_get_uint8 },
1665 	{ .type = PF_RT_RPOOL_RT, .off = _OUT(r.route), .arg = &pool_parser, .cb = snl_attr_get_nested },
1666 };
1667 #undef _OUT
1668 SNL_DECLARE_PARSER(getrule_parser, struct genlmsghdr, snl_f_p_empty, ap_getrule);
1669 
1670 int
pfctl_get_clear_rule_h(struct pfctl_handle * h,uint32_t nr,uint32_t ticket,const char * anchor,uint32_t ruleset,struct pfctl_rule * rule,char * anchor_call,bool clear)1671 pfctl_get_clear_rule_h(struct pfctl_handle *h, uint32_t nr, uint32_t ticket,
1672     const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
1673     char *anchor_call, bool clear)
1674 {
1675 	struct pfctl_nl_get_rule attrs = {};
1676 	struct snl_errmsg_data e = {};
1677 	struct nlmsghdr *hdr;
1678 	struct snl_writer nw;
1679 	uint32_t seq_id;
1680 	int family_id;
1681 
1682 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
1683 	if (family_id == 0)
1684 		return (ENOTSUP);
1685 
1686 	snl_init_writer(&h->ss, &nw);
1687 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETRULE);
1688 	hdr->nlmsg_flags |= NLM_F_DUMP;
1689 
1690 	snl_add_msg_attr_string(&nw, PF_GR_ANCHOR, anchor);
1691 	snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset);
1692 	snl_add_msg_attr_u32(&nw, PF_GR_NR, nr);
1693 	snl_add_msg_attr_u32(&nw, PF_GR_TICKET, ticket);
1694 	snl_add_msg_attr_u8(&nw, PF_GR_CLEAR, clear);
1695 
1696 	hdr = snl_finalize_msg(&nw);
1697 	if (hdr == NULL)
1698 		return (ENOMEM);
1699 
1700 	seq_id = hdr->nlmsg_seq;
1701 	if (! snl_send_message(&h->ss, hdr))
1702 		return (ENXIO);
1703 
1704 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
1705 		if (! snl_parse_nlmsg(&h->ss, hdr, &getrule_parser, &attrs))
1706 			continue;
1707 	}
1708 
1709 	memcpy(rule, &attrs.r, sizeof(attrs.r));
1710 	strlcpy(anchor_call, attrs.anchor_call, MAXPATHLEN);
1711 
1712 	return (e.error);
1713 }
1714 
1715 int
pfctl_get_clear_rule(int dev,uint32_t nr,uint32_t ticket,const char * anchor,uint32_t ruleset,struct pfctl_rule * rule,char * anchor_call,bool clear)1716 pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket,
1717 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
1718 	    char *anchor_call, bool clear)
1719 {
1720 	nvlist_t *nvl;
1721 	int ret;
1722 
1723 	nvl = nvlist_create(0);
1724 	if (nvl == 0)
1725 		return (ENOMEM);
1726 
1727 	nvlist_add_number(nvl, "nr", nr);
1728 	nvlist_add_number(nvl, "ticket", ticket);
1729 	nvlist_add_string(nvl, "anchor", anchor);
1730 	nvlist_add_number(nvl, "ruleset", ruleset);
1731 
1732 	if (clear)
1733 		nvlist_add_bool(nvl, "clear_counter", true);
1734 
1735 	if ((ret = pfctl_do_ioctl(dev, DIOCGETRULENV, 8192, &nvl)) != 0)
1736 		goto out;
1737 
1738 	pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule);
1739 
1740 	if (anchor_call)
1741 		strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
1742 		    MAXPATHLEN);
1743 
1744 out:
1745 	nvlist_destroy(nvl);
1746 	return (ret);
1747 }
1748 
1749 int
pfctl_set_keepcounters(int dev,bool keep)1750 pfctl_set_keepcounters(int dev, bool keep)
1751 {
1752 	struct pfioc_nv	 nv;
1753 	nvlist_t	*nvl;
1754 	int		 ret;
1755 
1756 	nvl = nvlist_create(0);
1757 
1758 	nvlist_add_bool(nvl, "keep_counters", keep);
1759 
1760 	nv.data = nvlist_pack(nvl, &nv.len);
1761 	nv.size = nv.len;
1762 
1763 	nvlist_destroy(nvl);
1764 
1765 	ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv);
1766 
1767 	free(nv.data);
1768 	return (ret);
1769 }
1770 
1771 struct pfctl_creator {
1772 	uint32_t id;
1773 };
1774 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1775 #define	_OUT(_field)	offsetof(struct pfctl_creator, _field)
1776 static struct snl_attr_parser ap_creators[] = {
1777 	{ .type = PF_ST_CREATORID, .off = _OUT(id), .cb = snl_attr_get_uint32 },
1778 };
1779 #undef _IN
1780 #undef _OUT
1781 SNL_DECLARE_PARSER(creator_parser, struct genlmsghdr, snl_f_p_empty, ap_creators);
1782 
1783 static int
pfctl_get_creators_nl(struct snl_state * ss,uint32_t * creators,size_t * len)1784 pfctl_get_creators_nl(struct snl_state *ss, uint32_t *creators, size_t *len)
1785 {
1786 
1787 	int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME);
1788 	size_t i = 0;
1789 
1790 	struct nlmsghdr *hdr;
1791 	struct snl_writer nw;
1792 
1793 	if (family_id == 0)
1794 		return (ENOTSUP);
1795 
1796 	snl_init_writer(ss, &nw);
1797 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETCREATORS);
1798 	hdr->nlmsg_flags |= NLM_F_DUMP;
1799 	hdr = snl_finalize_msg(&nw);
1800 	if (hdr == NULL)
1801 		return (ENOMEM);
1802 	uint32_t seq_id = hdr->nlmsg_seq;
1803 
1804 	snl_send_message(ss, hdr);
1805 
1806 	struct snl_errmsg_data e = {};
1807 	while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) {
1808 		struct pfctl_creator c;
1809 		bzero(&c, sizeof(c));
1810 
1811 		if (!snl_parse_nlmsg(ss, hdr, &creator_parser, &c))
1812 			continue;
1813 
1814 		creators[i] = c.id;
1815 		i++;
1816 		if (i > *len)
1817 			return (E2BIG);
1818 	}
1819 
1820 	*len = i;
1821 
1822 	return (0);
1823 }
1824 
1825 int
pfctl_get_creatorids(struct pfctl_handle * h,uint32_t * creators,size_t * len)1826 pfctl_get_creatorids(struct pfctl_handle *h, uint32_t *creators, size_t *len)
1827 {
1828 	int error;
1829 
1830 	error = pfctl_get_creators_nl(&h->ss, creators, len);
1831 
1832 	return (error);
1833 }
1834 
1835 static inline bool
snl_attr_get_pfaddr(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)1836 snl_attr_get_pfaddr(struct snl_state *ss __unused, struct nlattr *nla,
1837     const void *arg __unused, void *target)
1838 {
1839 	memcpy(target, NLA_DATA(nla), NLA_DATA_LEN(nla));
1840 	return (true);
1841 }
1842 
1843 static inline bool
snl_attr_store_ifname(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)1844 snl_attr_store_ifname(struct snl_state *ss __unused, struct nlattr *nla,
1845     const void *arg __unused, void *target)
1846 {
1847 	size_t maxlen = NLA_DATA_LEN(nla);
1848 
1849 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
1850 		strlcpy(target, (char *)NLA_DATA(nla), maxlen);
1851 		return (true);
1852 	}
1853 	return (false);
1854 }
1855 
1856 #define	_OUT(_field)	offsetof(struct pfctl_state_peer, _field)
1857 static const struct snl_attr_parser nla_p_speer[] = {
1858 	{ .type = PF_STP_SEQLO, .off = _OUT(seqlo), .cb = snl_attr_get_uint32 },
1859 	{ .type = PF_STP_SEQHI, .off = _OUT(seqhi), .cb = snl_attr_get_uint32 },
1860 	{ .type = PF_STP_SEQDIFF, .off = _OUT(seqdiff), .cb = snl_attr_get_uint32 },
1861 	{ .type = PF_STP_STATE, .off = _OUT(state), .cb = snl_attr_get_uint8 },
1862 	{ .type = PF_STP_WSCALE, .off = _OUT(wscale), .cb = snl_attr_get_uint8 },
1863 };
1864 SNL_DECLARE_ATTR_PARSER(speer_parser, nla_p_speer);
1865 #undef _OUT
1866 
1867 #define	_OUT(_field)	offsetof(struct pfctl_state_key, _field)
1868 static const struct snl_attr_parser nla_p_skey[] = {
1869 	{ .type = PF_STK_ADDR0, .off = _OUT(addr[0]), .cb = snl_attr_get_pfaddr },
1870 	{ .type = PF_STK_ADDR1, .off = _OUT(addr[1]), .cb = snl_attr_get_pfaddr },
1871 	{ .type = PF_STK_PORT0, .off = _OUT(port[0]), .cb = snl_attr_get_uint16 },
1872 	{ .type = PF_STK_PORT1, .off = _OUT(port[1]), .cb = snl_attr_get_uint16 },
1873 	{ .type = PF_STK_AF, .off = _OUT(af), .cb = snl_attr_get_uint8 },
1874 	{ .type = PF_STK_PROTO, .off = _OUT(proto), .cb = snl_attr_get_uint16 },
1875 };
1876 SNL_DECLARE_ATTR_PARSER(skey_parser, nla_p_skey);
1877 #undef _OUT
1878 
1879 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1880 #define	_OUT(_field)	offsetof(struct pfctl_state, _field)
1881 static struct snl_attr_parser ap_state[] = {
1882 	{ .type = PF_ST_ID, .off = _OUT(id), .cb = snl_attr_get_uint64 },
1883 	{ .type = PF_ST_CREATORID, .off = _OUT(creatorid), .cb = snl_attr_get_uint32 },
1884 	{ .type = PF_ST_IFNAME, .off = _OUT(ifname), .cb = snl_attr_store_ifname },
1885 	{ .type = PF_ST_ORIG_IFNAME, .off = _OUT(orig_ifname), .cb = snl_attr_store_ifname },
1886 	{ .type = PF_ST_KEY_WIRE, .off = _OUT(key[0]), .arg = &skey_parser, .cb = snl_attr_get_nested },
1887 	{ .type = PF_ST_KEY_STACK, .off = _OUT(key[1]), .arg = &skey_parser, .cb = snl_attr_get_nested },
1888 	{ .type = PF_ST_PEER_SRC, .off = _OUT(src), .arg = &speer_parser, .cb = snl_attr_get_nested },
1889 	{ .type = PF_ST_PEER_DST, .off = _OUT(dst), .arg = &speer_parser, .cb = snl_attr_get_nested },
1890 	{ .type = PF_ST_RT_ADDR, .off = _OUT(rt_addr), .cb = snl_attr_get_pfaddr },
1891 	{ .type = PF_ST_RULE, .off = _OUT(rule), .cb = snl_attr_get_uint32 },
1892 	{ .type = PF_ST_ANCHOR, .off = _OUT(anchor), .cb = snl_attr_get_uint32 },
1893 	{ .type = PF_ST_NAT_RULE, .off = _OUT(nat_rule), .cb = snl_attr_get_uint32 },
1894 	{ .type = PF_ST_CREATION, .off = _OUT(creation), .cb = snl_attr_get_uint32 },
1895 	{ .type = PF_ST_EXPIRE, .off = _OUT(expire), .cb = snl_attr_get_uint32 },
1896 	{ .type = PF_ST_PACKETS0, .off = _OUT(packets[0]), .cb = snl_attr_get_uint64 },
1897 	{ .type = PF_ST_PACKETS1, .off = _OUT(packets[1]), .cb = snl_attr_get_uint64 },
1898 	{ .type = PF_ST_BYTES0, .off = _OUT(bytes[0]), .cb = snl_attr_get_uint64 },
1899 	{ .type = PF_ST_BYTES1, .off = _OUT(bytes[1]), .cb = snl_attr_get_uint64 },
1900 	{ .type = PF_ST_DIRECTION, .off = _OUT(direction), .cb = snl_attr_get_uint8 },
1901 	{ .type = PF_ST_LOG, .off = _OUT(log), .cb = snl_attr_get_uint8 },
1902 	{ .type = PF_ST_STATE_FLAGS, .off = _OUT(state_flags), .cb = snl_attr_get_uint16 },
1903 	{ .type = PF_ST_SYNC_FLAGS, .off = _OUT(sync_flags), .cb = snl_attr_get_uint8 },
1904 	{ .type = PF_ST_RTABLEID, .off = _OUT(rtableid), .cb = snl_attr_get_int32 },
1905 	{ .type = PF_ST_MIN_TTL, .off = _OUT(min_ttl), .cb = snl_attr_get_uint8 },
1906 	{ .type = PF_ST_MAX_MSS, .off = _OUT(max_mss), .cb = snl_attr_get_uint16 },
1907 	{ .type = PF_ST_DNPIPE, .off = _OUT(dnpipe), .cb = snl_attr_get_uint16 },
1908 	{ .type = PF_ST_DNRPIPE, .off = _OUT(dnrpipe), .cb = snl_attr_get_uint16 },
1909 	{ .type = PF_ST_RT, .off = _OUT(rt), .cb = snl_attr_get_uint8 },
1910 	{ .type = PF_ST_RT_IFNAME, .off = _OUT(rt_ifname), .cb = snl_attr_store_ifname },
1911 };
1912 #undef _IN
1913 #undef _OUT
1914 SNL_DECLARE_PARSER(state_parser, struct genlmsghdr, snl_f_p_empty, ap_state);
1915 
1916 static const struct snl_hdr_parser *all_parsers[] = {
1917 	&state_parser, &skey_parser, &speer_parser,
1918 	&creator_parser, &getrules_parser
1919 };
1920 
1921 static int
pfctl_get_states_nl(struct pfctl_state_filter * filter,struct snl_state * ss,pfctl_get_state_fn f,void * arg)1922 pfctl_get_states_nl(struct pfctl_state_filter *filter, struct snl_state *ss, pfctl_get_state_fn f, void *arg)
1923 {
1924 	SNL_VERIFY_PARSERS(all_parsers);
1925 	int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME);
1926 	int ret;
1927 
1928 	struct nlmsghdr *hdr;
1929 	struct snl_writer nw;
1930 
1931 	if (family_id == 0)
1932 		return (ENOTSUP);
1933 
1934 	snl_init_writer(ss, &nw);
1935 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETSTATES);
1936 	hdr->nlmsg_flags |= NLM_F_DUMP;
1937 	snl_add_msg_attr_string(&nw, PF_ST_IFNAME, filter->ifname);
1938 	snl_add_msg_attr_u16(&nw, PF_ST_PROTO, filter->proto);
1939 	snl_add_msg_attr_u8(&nw, PF_ST_AF, filter->af);
1940 	snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_ADDR, &filter->addr.v6);
1941 	snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_MASK, &filter->mask.v6);
1942 
1943 	hdr = snl_finalize_msg(&nw);
1944 	if (hdr == NULL)
1945 		return (ENOMEM);
1946 
1947 	uint32_t seq_id = hdr->nlmsg_seq;
1948 
1949 	snl_send_message(ss, hdr);
1950 
1951 	struct snl_errmsg_data e = {};
1952 	while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) {
1953 		struct pfctl_state s;
1954 		bzero(&s, sizeof(s));
1955 		if (!snl_parse_nlmsg(ss, hdr, &state_parser, &s))
1956 			continue;
1957 
1958 		ret = f(&s, arg);
1959 		if (ret != 0)
1960 			return (ret);
1961 	}
1962 
1963 	return (0);
1964 }
1965 
1966 int
pfctl_get_states_iter(pfctl_get_state_fn f,void * arg)1967 pfctl_get_states_iter(pfctl_get_state_fn f, void *arg)
1968 {
1969 	struct pfctl_state_filter filter = {};
1970 	return (pfctl_get_filtered_states_iter(&filter, f, arg));
1971 }
1972 
1973 int
pfctl_get_filtered_states_iter(struct pfctl_state_filter * filter,pfctl_get_state_fn f,void * arg)1974 pfctl_get_filtered_states_iter(struct pfctl_state_filter *filter, pfctl_get_state_fn f, void *arg)
1975 {
1976 	struct snl_state ss = {};
1977 	int error;
1978 
1979 	snl_init(&ss, NETLINK_GENERIC);
1980 	error = pfctl_get_states_nl(filter, &ss, f, arg);
1981 	snl_free(&ss);
1982 
1983 	return (error);
1984 }
1985 
1986 static int
pfctl_append_states(struct pfctl_state * s,void * arg)1987 pfctl_append_states(struct pfctl_state *s, void *arg)
1988 {
1989 	struct pfctl_state *new;
1990 	struct pfctl_states *states = (struct pfctl_states *)arg;
1991 
1992 	new = malloc(sizeof(*s));
1993 	if (new == NULL)
1994 		return (ENOMEM);
1995 
1996 	memcpy(new, s, sizeof(*s));
1997 
1998 	TAILQ_INSERT_TAIL(&states->states, new, entry);
1999 
2000 	return (0);
2001 }
2002 
2003 int
pfctl_get_states(int dev __unused,struct pfctl_states * states)2004 pfctl_get_states(int dev __unused, struct pfctl_states *states)
2005 {
2006 	int ret;
2007 
2008 	bzero(states, sizeof(*states));
2009 	TAILQ_INIT(&states->states);
2010 
2011 	ret = pfctl_get_states_iter(pfctl_append_states, states);
2012 	if (ret != 0) {
2013 		pfctl_free_states(states);
2014 		return (ret);
2015 	}
2016 
2017 	return (0);
2018 }
2019 
2020 void
pfctl_free_states(struct pfctl_states * states)2021 pfctl_free_states(struct pfctl_states *states)
2022 {
2023 	struct pfctl_state *s, *tmp;
2024 
2025 	TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) {
2026 		free(s);
2027 	}
2028 
2029 	bzero(states, sizeof(*states));
2030 }
2031 
2032 struct pfctl_nl_clear_states {
2033 	uint32_t killed;
2034 };
2035 #define	_OUT(_field)	offsetof(struct pfctl_nl_clear_states, _field)
2036 static struct snl_attr_parser ap_clear_states[] = {
2037 	{ .type = PF_CS_KILLED, .off = _OUT(killed), .cb = snl_attr_get_uint32 },
2038 };
2039 #undef _OUT
2040 SNL_DECLARE_PARSER(clear_states_parser, struct genlmsghdr, snl_f_p_empty, ap_clear_states);
2041 
2042 static int
_pfctl_clear_states_h(struct pfctl_handle * h,const struct pfctl_kill * kill,unsigned int * killed,int cmd)2043 _pfctl_clear_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
2044     unsigned int *killed, int cmd)
2045 {
2046 	struct snl_writer nw;
2047 	struct snl_errmsg_data e = {};
2048 	struct pfctl_nl_clear_states attrs = {};
2049 	struct nlmsghdr *hdr;
2050 	uint32_t seq_id;
2051 	int family_id;
2052 
2053 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2054 	if (family_id == 0)
2055 		return (ENOTSUP);
2056 
2057 	snl_init_writer(&h->ss, &nw);
2058 	hdr = snl_create_genl_msg_request(&nw, family_id, cmd);
2059 	hdr->nlmsg_flags |= NLM_F_DUMP;
2060 
2061 	snl_add_msg_attr_u64(&nw, PF_CS_CMP_ID, kill->cmp.id);
2062 	snl_add_msg_attr_u32(&nw, PF_CS_CMP_CREATORID, htonl(kill->cmp.creatorid));
2063 	snl_add_msg_attr_u8(&nw, PF_CS_CMP_DIR, kill->cmp.direction);
2064 	snl_add_msg_attr_u8(&nw, PF_CS_AF, kill->af);
2065 	snl_add_msg_attr_u8(&nw, PF_CS_PROTO, kill->proto);
2066 	snl_add_msg_attr_rule_addr(&nw, PF_CS_SRC, &kill->src);
2067 	snl_add_msg_attr_rule_addr(&nw, PF_CS_DST, &kill->dst);
2068 	snl_add_msg_attr_rule_addr(&nw, PF_CS_RT_ADDR, &kill->rt_addr);
2069 	snl_add_msg_attr_string(&nw, PF_CS_IFNAME, kill->ifname);
2070 	snl_add_msg_attr_string(&nw, PF_CS_LABEL, kill->label);
2071 	snl_add_msg_attr_bool(&nw, PF_CS_KILL_MATCH, kill->kill_match);
2072 	snl_add_msg_attr_bool(&nw, PF_CS_NAT, kill->nat);
2073 
2074 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2075 		return (ENXIO);
2076 
2077 	seq_id = hdr->nlmsg_seq;
2078 
2079 	if (! snl_send_message(&h->ss, hdr))
2080 		return (ENXIO);
2081 
2082 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2083 		if (! snl_parse_nlmsg(&h->ss, hdr, &clear_states_parser, &attrs))
2084 			continue;
2085 	}
2086 
2087 	if (killed)
2088 		*killed = attrs.killed;
2089 
2090 	return (e.error);
2091 }
2092 
2093 int
pfctl_clear_states_h(struct pfctl_handle * h,const struct pfctl_kill * kill,unsigned int * killed)2094 pfctl_clear_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
2095     unsigned int *killed)
2096 {
2097 	return(_pfctl_clear_states_h(h, kill, killed, PFNL_CMD_CLRSTATES));
2098 }
2099 
2100 int
pfctl_kill_states_h(struct pfctl_handle * h,const struct pfctl_kill * kill,unsigned int * killed)2101 pfctl_kill_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
2102     unsigned int *killed)
2103 {
2104 	return(_pfctl_clear_states_h(h, kill, killed, PFNL_CMD_KILLSTATES));
2105 }
2106 
2107 static int
_pfctl_clear_states(int dev __unused,const struct pfctl_kill * kill,unsigned int * killed,uint64_t cmd)2108 _pfctl_clear_states(int dev __unused, const struct pfctl_kill *kill,
2109     unsigned int *killed, uint64_t cmd)
2110 {
2111 	struct pfctl_handle *h;
2112 	int ret;
2113 
2114 	h = pfctl_open(PF_DEVICE);
2115 	if (h == NULL)
2116 		return (ENODEV);
2117 
2118 	ret = _pfctl_clear_states_h(h, kill, killed, cmd);
2119 	pfctl_close(h);
2120 
2121 	return (ret);
2122 }
2123 
2124 int
pfctl_clear_states(int dev __unused,const struct pfctl_kill * kill,unsigned int * killed)2125 pfctl_clear_states(int dev __unused, const struct pfctl_kill *kill,
2126     unsigned int *killed)
2127 {
2128 	return (_pfctl_clear_states(dev, kill, killed, PFNL_CMD_CLRSTATES));
2129 }
2130 
2131 int
pfctl_kill_states(int dev __unused,const struct pfctl_kill * kill,unsigned int * killed)2132 pfctl_kill_states(int dev __unused, const struct pfctl_kill *kill, unsigned int *killed)
2133 {
2134 	return (_pfctl_clear_states(dev, kill, killed, PFNL_CMD_KILLSTATES));
2135 }
2136 
2137 int
pfctl_clear_rules(int dev,const char * anchorname)2138 pfctl_clear_rules(int dev, const char *anchorname)
2139 {
2140 	struct pfioc_trans trans;
2141 	struct pfioc_trans_e transe[2];
2142 	int ret;
2143 
2144 	bzero(&trans, sizeof(trans));
2145 	bzero(&transe, sizeof(transe));
2146 
2147 	transe[0].rs_num = PF_RULESET_SCRUB;
2148 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
2149 	    >= sizeof(transe[0].anchor))
2150 		return (E2BIG);
2151 
2152 	transe[1].rs_num = PF_RULESET_FILTER;
2153 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
2154 	    >= sizeof(transe[1].anchor))
2155 		return (E2BIG);
2156 
2157 	trans.size = 2;
2158 	trans.esize = sizeof(transe[0]);
2159 	trans.array = transe;
2160 
2161 	ret = ioctl(dev, DIOCXBEGIN, &trans);
2162 	if (ret != 0)
2163 		return (errno);
2164 	ret = ioctl(dev, DIOCXCOMMIT, &trans);
2165 	if (ret != 0)
2166 		return (errno);
2167 
2168 	return (0);
2169 }
2170 
2171 int
pfctl_clear_nat(int dev,const char * anchorname)2172 pfctl_clear_nat(int dev, const char *anchorname)
2173 {
2174 	struct pfioc_trans trans;
2175 	struct pfioc_trans_e transe[3];
2176 	int ret;
2177 
2178 	bzero(&trans, sizeof(trans));
2179 	bzero(&transe, sizeof(transe));
2180 
2181 	transe[0].rs_num = PF_RULESET_NAT;
2182 	if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor))
2183 	    >= sizeof(transe[0].anchor))
2184 		return (E2BIG);
2185 
2186 	transe[1].rs_num = PF_RULESET_BINAT;
2187 	if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor))
2188 	    >= sizeof(transe[0].anchor))
2189 		return (E2BIG);
2190 
2191 	transe[2].rs_num = PF_RULESET_RDR;
2192 	if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor))
2193 	    >= sizeof(transe[2].anchor))
2194 		return (E2BIG);
2195 
2196 	trans.size = 3;
2197 	trans.esize = sizeof(transe[0]);
2198 	trans.array = transe;
2199 
2200 	ret = ioctl(dev, DIOCXBEGIN, &trans);
2201 	if (ret != 0)
2202 		return (errno);
2203 	ret = ioctl(dev, DIOCXCOMMIT, &trans);
2204 	if (ret != 0)
2205 		return (errno);
2206 
2207 	return (0);
2208 }
2209 
2210 int
pfctl_clear_eth_rules(int dev,const char * anchorname)2211 pfctl_clear_eth_rules(int dev, const char *anchorname)
2212 {
2213 	struct pfioc_trans trans;
2214 	struct pfioc_trans_e transe;
2215 	int ret;
2216 
2217 	bzero(&trans, sizeof(trans));
2218 	bzero(&transe, sizeof(transe));
2219 
2220 	transe.rs_num = PF_RULESET_ETH;
2221 	if (strlcpy(transe.anchor, anchorname, sizeof(transe.anchor))
2222 	    >= sizeof(transe.anchor))
2223 		return (E2BIG);
2224 
2225 	trans.size = 1;
2226 	trans.esize = sizeof(transe);
2227 	trans.array = &transe;
2228 
2229 	ret = ioctl(dev, DIOCXBEGIN, &trans);
2230 	if (ret != 0)
2231 		return (errno);
2232 	ret = ioctl(dev, DIOCXCOMMIT, &trans);
2233 	if (ret != 0)
2234 		return (errno);
2235 
2236 	return (0);
2237 }
2238 
2239 static int
_pfctl_get_limit(int dev,const int index,uint * limit)2240 _pfctl_get_limit(int dev, const int index, uint *limit)
2241 {
2242 	struct pfioc_limit pl;
2243 
2244 	bzero(&pl, sizeof(pl));
2245 	pl.index = index;
2246 
2247 	if (ioctl(dev, DIOCGETLIMIT, &pl) == -1)
2248 		return (errno);
2249 
2250 	*limit = pl.limit;
2251 
2252 	return (0);
2253 }
2254 
2255 int
pfctl_set_syncookies(int dev,const struct pfctl_syncookies * s)2256 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s)
2257 {
2258 	struct pfioc_nv	 nv;
2259 	nvlist_t	*nvl;
2260 	int		 ret;
2261 	uint		 state_limit;
2262 	uint64_t	 lim, hi, lo;
2263 
2264 	ret = _pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
2265 	if (ret != 0)
2266 		return (ret);
2267 
2268 	lim = state_limit;
2269 	hi = lim * s->highwater / 100;
2270 	lo = lim * s->lowwater / 100;
2271 
2272 	if (lo == hi)
2273 		hi++;
2274 
2275 	nvl = nvlist_create(0);
2276 
2277 	nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER);
2278 	nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE);
2279 	nvlist_add_number(nvl, "highwater", hi);
2280 	nvlist_add_number(nvl, "lowwater", lo);
2281 
2282 	nv.data = nvlist_pack(nvl, &nv.len);
2283 	nv.size = nv.len;
2284 	nvlist_destroy(nvl);
2285 	nvl = NULL;
2286 
2287 	ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv);
2288 
2289 	free(nv.data);
2290 	if (ret != 0)
2291 		return (errno);
2292 
2293 	return (0);
2294 }
2295 
2296 int
pfctl_get_syncookies(int dev,struct pfctl_syncookies * s)2297 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s)
2298 {
2299 	nvlist_t	*nvl;
2300 	int		 ret;
2301 	uint		 state_limit;
2302 	bool		 enabled, adaptive;
2303 
2304 	ret = _pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit);
2305 	if (ret != 0)
2306 		return (ret);
2307 
2308 	bzero(s, sizeof(*s));
2309 
2310 	nvl = nvlist_create(0);
2311 
2312 	if ((ret = pfctl_do_ioctl(dev, DIOCGETSYNCOOKIES, 256, &nvl)) != 0) {
2313 		ret = errno;
2314 		goto out;
2315 	}
2316 
2317 	enabled = nvlist_get_bool(nvl, "enabled");
2318 	adaptive = nvlist_get_bool(nvl, "adaptive");
2319 
2320 	if (enabled) {
2321 		if (adaptive)
2322 			s->mode = PFCTL_SYNCOOKIES_ADAPTIVE;
2323 		else
2324 			s->mode = PFCTL_SYNCOOKIES_ALWAYS;
2325 	} else {
2326 		s->mode = PFCTL_SYNCOOKIES_NEVER;
2327 	}
2328 
2329 	s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit;
2330 	s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit;
2331 	s->halfopen_states = nvlist_get_number(nvl, "halfopen_states");
2332 
2333 out:
2334 	nvlist_destroy(nvl);
2335 	return (ret);
2336 }
2337 
2338 int
pfctl_table_add_addrs(int dev,struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nadd,int flags)2339 pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
2340     *addr, int size, int *nadd, int flags)
2341 {
2342 	struct pfioc_table io;
2343 
2344 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
2345 		return (EINVAL);
2346 	}
2347 	bzero(&io, sizeof io);
2348 	io.pfrio_flags = flags;
2349 	io.pfrio_table = *tbl;
2350 	io.pfrio_buffer = addr;
2351 	io.pfrio_esize = sizeof(*addr);
2352 	io.pfrio_size = size;
2353 
2354 	if (ioctl(dev, DIOCRADDADDRS, &io))
2355 		return (errno);
2356 	if (nadd != NULL)
2357 		*nadd = io.pfrio_nadd;
2358 	return (0);
2359 }
2360 
2361 int
pfctl_table_del_addrs(int dev,struct pfr_table * tbl,struct pfr_addr * addr,int size,int * ndel,int flags)2362 pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
2363     *addr, int size, int *ndel, int flags)
2364 {
2365 	struct pfioc_table io;
2366 
2367 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
2368 		return (EINVAL);
2369 	}
2370 	bzero(&io, sizeof io);
2371 	io.pfrio_flags = flags;
2372 	io.pfrio_table = *tbl;
2373 	io.pfrio_buffer = addr;
2374 	io.pfrio_esize = sizeof(*addr);
2375 	io.pfrio_size = size;
2376 
2377 	if (ioctl(dev, DIOCRDELADDRS, &io))
2378 		return (errno);
2379 	if (ndel != NULL)
2380 		*ndel = io.pfrio_ndel;
2381 	return (0);
2382 }
2383 
2384 int
pfctl_table_set_addrs(int dev,struct pfr_table * tbl,struct pfr_addr * addr,int size,int * size2,int * nadd,int * ndel,int * nchange,int flags)2385 pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
2386     *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags)
2387 {
2388 	struct pfioc_table io;
2389 
2390 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
2391 		return (EINVAL);
2392 	}
2393 	bzero(&io, sizeof io);
2394 	io.pfrio_flags = flags;
2395 	io.pfrio_table = *tbl;
2396 	io.pfrio_buffer = addr;
2397 	io.pfrio_esize = sizeof(*addr);
2398 	io.pfrio_size = size;
2399 	io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
2400 	if (ioctl(dev, DIOCRSETADDRS, &io))
2401 		return (errno);
2402 	if (nadd != NULL)
2403 		*nadd = io.pfrio_nadd;
2404 	if (ndel != NULL)
2405 		*ndel = io.pfrio_ndel;
2406 	if (nchange != NULL)
2407 		*nchange = io.pfrio_nchange;
2408 	if (size2 != NULL)
2409 		*size2 = io.pfrio_size2;
2410 	return (0);
2411 }
2412 
pfctl_table_get_addrs(int dev,struct pfr_table * tbl,struct pfr_addr * addr,int * size,int flags)2413 int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr,
2414     int *size, int flags)
2415 {
2416 	struct pfioc_table io;
2417 
2418 	if (tbl == NULL || size == NULL || *size < 0 ||
2419 	    (*size && addr == NULL)) {
2420 		return (EINVAL);
2421 	}
2422 	bzero(&io, sizeof io);
2423 	io.pfrio_flags = flags;
2424 	io.pfrio_table = *tbl;
2425 	io.pfrio_buffer = addr;
2426 	io.pfrio_esize = sizeof(*addr);
2427 	io.pfrio_size = *size;
2428 	if (ioctl(dev, DIOCRGETADDRS, &io))
2429 		return (errno);
2430 	*size = io.pfrio_size;
2431 	return (0);
2432 }
2433 
2434 int
pfctl_set_statusif(struct pfctl_handle * h,const char * ifname)2435 pfctl_set_statusif(struct pfctl_handle *h, const char *ifname)
2436 {
2437 	struct snl_writer nw;
2438 	struct snl_errmsg_data e = {};
2439 	struct nlmsghdr *hdr;
2440 	uint32_t seq_id;
2441 	int family_id;
2442 
2443 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2444 	if (family_id == 0)
2445 		return (ENOTSUP);
2446 
2447 	snl_init_writer(&h->ss, &nw);
2448 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_SET_STATUSIF);
2449 
2450 	snl_add_msg_attr_string(&nw, PF_SS_IFNAME, ifname);
2451 
2452 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2453 		return (ENXIO);
2454 
2455 	seq_id = hdr->nlmsg_seq;
2456 
2457 	if (! snl_send_message(&h->ss, hdr))
2458 		return (ENXIO);
2459 
2460 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2461 	}
2462 
2463 	return (e.error);
2464 }
2465 
2466 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
2467 #define	_OUT(_field)	offsetof(struct pfctl_natlook, _field)
2468 static struct snl_attr_parser ap_natlook[] = {
2469 	{ .type = PF_NL_SRC_ADDR, .off = _OUT(saddr), .cb = snl_attr_get_in6_addr },
2470 	{ .type = PF_NL_DST_ADDR, .off = _OUT(daddr), .cb = snl_attr_get_in6_addr },
2471 	{ .type = PF_NL_SRC_PORT, .off = _OUT(sport), .cb = snl_attr_get_uint16 },
2472 	{ .type = PF_NL_DST_PORT, .off = _OUT(dport), .cb = snl_attr_get_uint16 },
2473 };
2474 #undef _IN
2475 #undef _OUT
2476 SNL_DECLARE_PARSER(natlook_parser, struct genlmsghdr, snl_f_p_empty, ap_natlook);
2477 
2478 int
pfctl_natlook(struct pfctl_handle * h,const struct pfctl_natlook_key * k,struct pfctl_natlook * r)2479 pfctl_natlook(struct pfctl_handle *h, const struct pfctl_natlook_key *k,
2480     struct pfctl_natlook *r)
2481 {
2482 	struct snl_writer nw;
2483 	struct snl_errmsg_data e = {};
2484 	struct nlmsghdr *hdr;
2485 	uint32_t seq_id;
2486 	int family_id;
2487 
2488 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2489 	if (family_id == 0)
2490 		return (ENOTSUP);
2491 
2492 	snl_init_writer(&h->ss, &nw);
2493 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_NATLOOK);
2494 	hdr->nlmsg_flags |= NLM_F_DUMP;
2495 
2496 	snl_add_msg_attr_u8(&nw, PF_NL_AF, k->af);
2497 	snl_add_msg_attr_u8(&nw, PF_NL_DIRECTION, k->direction);
2498 	snl_add_msg_attr_u8(&nw, PF_NL_PROTO, k->proto);
2499 	snl_add_msg_attr_ip6(&nw, PF_NL_SRC_ADDR, &k->saddr.v6);
2500 	snl_add_msg_attr_ip6(&nw, PF_NL_DST_ADDR, &k->daddr.v6);
2501 	snl_add_msg_attr_u16(&nw, PF_NL_SRC_PORT, k->sport);
2502 	snl_add_msg_attr_u16(&nw, PF_NL_DST_PORT, k->dport);
2503 
2504 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2505 		return (ENXIO);
2506 
2507 	seq_id = hdr->nlmsg_seq;
2508 
2509 	if (! snl_send_message(&h->ss, hdr))
2510 		return (ENXIO);
2511 
2512 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2513 		if (! snl_parse_nlmsg(&h->ss, hdr, &natlook_parser, r))
2514 			continue;
2515 	}
2516 
2517 	return (e.error);
2518 }
2519 
2520 int
pfctl_set_debug(struct pfctl_handle * h,uint32_t level)2521 pfctl_set_debug(struct pfctl_handle *h, uint32_t level)
2522 {
2523 	struct snl_writer nw;
2524 	struct snl_errmsg_data e = {};
2525 	struct nlmsghdr *hdr;
2526 	uint32_t seq_id;
2527 	int family_id;
2528 
2529 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2530 	if (family_id == 0)
2531 		return (ENOTSUP);
2532 
2533 	snl_init_writer(&h->ss, &nw);
2534 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_SET_DEBUG);
2535 
2536 	snl_add_msg_attr_u32(&nw, PF_SD_LEVEL, level);
2537 
2538 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2539 		return (ENXIO);
2540 
2541 	seq_id = hdr->nlmsg_seq;
2542 
2543 	if (! snl_send_message(&h->ss, hdr))
2544 		return (ENXIO);
2545 
2546 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2547 	}
2548 
2549 	return (e.error);
2550 }
2551 
2552 int
pfctl_set_timeout(struct pfctl_handle * h,uint32_t timeout,uint32_t seconds)2553 pfctl_set_timeout(struct pfctl_handle *h, uint32_t timeout, uint32_t seconds)
2554 {
2555 	struct snl_writer nw;
2556 	struct snl_errmsg_data e = {};
2557 	struct nlmsghdr *hdr;
2558 	uint32_t seq_id;
2559 	int family_id;
2560 
2561 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2562 	if (family_id == 0)
2563 		return (ENOTSUP);
2564 
2565 	snl_init_writer(&h->ss, &nw);
2566 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_SET_TIMEOUT);
2567 
2568 	snl_add_msg_attr_u32(&nw, PF_TO_TIMEOUT, timeout);
2569 	snl_add_msg_attr_u32(&nw, PF_TO_SECONDS, seconds);
2570 
2571 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2572 		return (ENXIO);
2573 
2574 	seq_id = hdr->nlmsg_seq;
2575 
2576 	if (! snl_send_message(&h->ss, hdr))
2577 		return (ENXIO);
2578 
2579 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2580 	}
2581 
2582 	return (e.error);
2583 }
2584 
2585 struct pfctl_nl_timeout {
2586 	uint32_t seconds;
2587 };
2588 #define	_OUT(_field)	offsetof(struct pfctl_nl_timeout, _field)
2589 static struct snl_attr_parser ap_get_timeout[] = {
2590 	{ .type = PF_TO_SECONDS, .off = _OUT(seconds), .cb = snl_attr_get_uint32 },
2591 };
2592 #undef _OUT
2593 SNL_DECLARE_PARSER(get_timeout_parser, struct genlmsghdr, snl_f_p_empty, ap_get_timeout);
2594 
2595 int
pfctl_get_timeout(struct pfctl_handle * h,uint32_t timeout,uint32_t * seconds)2596 pfctl_get_timeout(struct pfctl_handle *h, uint32_t timeout, uint32_t *seconds)
2597 {
2598 	struct snl_writer nw;
2599 	struct pfctl_nl_timeout to = {};
2600 	struct snl_errmsg_data e = {};
2601 	struct nlmsghdr *hdr;
2602 	uint32_t seq_id;
2603 	int family_id;
2604 
2605 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2606 	if (family_id == 0)
2607 		return (ENOTSUP);
2608 
2609 	snl_init_writer(&h->ss, &nw);
2610 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_TIMEOUT);
2611 	hdr->nlmsg_flags |= NLM_F_DUMP;
2612 
2613 	snl_add_msg_attr_u32(&nw, PF_TO_TIMEOUT, timeout);
2614 
2615 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2616 		return (ENXIO);
2617 
2618 	seq_id = hdr->nlmsg_seq;
2619 
2620 	if (! snl_send_message(&h->ss, hdr))
2621 		return (ENXIO);
2622 
2623 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2624 		if (! snl_parse_nlmsg(&h->ss, hdr, &get_timeout_parser, &to))
2625 			continue;
2626 	}
2627 
2628 	if (seconds != NULL)
2629 		*seconds = to.seconds;
2630 
2631 	return (e.error);
2632 }
2633 
2634 int
pfctl_set_limit(struct pfctl_handle * h,const int index,const uint limit)2635 pfctl_set_limit(struct pfctl_handle *h, const int index, const uint limit)
2636 {
2637 	struct snl_writer nw;
2638 	struct snl_errmsg_data e = {};
2639 	struct nlmsghdr *hdr;
2640 	uint32_t seq_id;
2641 	int family_id;
2642 
2643 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2644 	if (family_id == 0)
2645 		return (ENOTSUP);
2646 
2647 	snl_init_writer(&h->ss, &nw);
2648 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_SET_LIMIT);
2649 
2650 	snl_add_msg_attr_u32(&nw, PF_LI_INDEX, index);
2651 	snl_add_msg_attr_u32(&nw, PF_LI_LIMIT, limit);
2652 
2653 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2654 		return (ENXIO);
2655 
2656 	seq_id = hdr->nlmsg_seq;
2657 
2658 	if (! snl_send_message(&h->ss, hdr))
2659 		return (ENXIO);
2660 
2661 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2662 	}
2663 
2664 	return (e.error);
2665 }
2666 
2667 struct pfctl_nl_limit {
2668 	unsigned int limit;
2669 };
2670 #define	_OUT(_field)	offsetof(struct pfctl_nl_limit, _field)
2671 static struct snl_attr_parser ap_get_limit[] = {
2672 	{ .type = PF_LI_LIMIT, .off = _OUT(limit), .cb = snl_attr_get_uint32 },
2673 };
2674 #undef _OUT
2675 SNL_DECLARE_PARSER(get_limit_parser, struct genlmsghdr, snl_f_p_empty, ap_get_limit);
2676 
2677 int
pfctl_get_limit(struct pfctl_handle * h,const int index,uint * limit)2678 pfctl_get_limit(struct pfctl_handle *h, const int index, uint *limit)
2679 {
2680 	struct snl_writer nw;
2681 	struct pfctl_nl_limit li = {};
2682 	struct snl_errmsg_data e = {};
2683 	struct nlmsghdr *hdr;
2684 	uint32_t seq_id;
2685 	int family_id;
2686 
2687 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2688 	if (family_id == 0)
2689 		return (ENOTSUP);
2690 
2691 	snl_init_writer(&h->ss, &nw);
2692 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_LIMIT);
2693 	hdr->nlmsg_flags |= NLM_F_DUMP;
2694 
2695 	snl_add_msg_attr_u32(&nw, PF_LI_INDEX, index);
2696 
2697 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2698 		return (ENXIO);
2699 
2700 	seq_id = hdr->nlmsg_seq;
2701 
2702 	if (! snl_send_message(&h->ss, hdr))
2703 		return (ENXIO);
2704 
2705 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2706 		if (! snl_parse_nlmsg(&h->ss, hdr, &get_limit_parser, &li))
2707 			continue;
2708 	}
2709 
2710 	if (limit != NULL)
2711 		*limit = li.limit;
2712 
2713 	return (e.error);
2714 }
2715 
2716 struct pfctl_nl_begin_addrs {
2717 	uint32_t ticket;
2718 };
2719 #define	_OUT(_field)	offsetof(struct pfctl_nl_begin_addrs, _field)
2720 static struct snl_attr_parser ap_begin_addrs[] = {
2721 	{ .type = PF_BA_TICKET, .off = _OUT(ticket), .cb = snl_attr_get_uint32 },
2722 };
2723 #undef _OUT
2724 SNL_DECLARE_PARSER(begin_addrs_parser, struct genlmsghdr, snl_f_p_empty, ap_begin_addrs);
2725 
2726 int
pfctl_begin_addrs(struct pfctl_handle * h,uint32_t * ticket)2727 pfctl_begin_addrs(struct pfctl_handle *h, uint32_t *ticket)
2728 {
2729 	struct snl_writer nw;
2730 	struct pfctl_nl_begin_addrs attrs = {};
2731 	struct snl_errmsg_data e = {};
2732 	struct nlmsghdr *hdr;
2733 	uint32_t seq_id;
2734 	int family_id;
2735 
2736 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2737 	if (family_id == 0)
2738 		return (ENOTSUP);
2739 
2740 	snl_init_writer(&h->ss, &nw);
2741 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_BEGIN_ADDRS);
2742 	hdr->nlmsg_flags |= NLM_F_DUMP;
2743 
2744 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2745 		return (ENXIO);
2746 
2747 	seq_id = hdr->nlmsg_seq;
2748 
2749 	if (! snl_send_message(&h->ss, hdr))
2750 		return (ENXIO);
2751 
2752 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2753 		if (! snl_parse_nlmsg(&h->ss, hdr, &begin_addrs_parser, &attrs))
2754 			continue;
2755 	}
2756 
2757 	if (ticket != NULL)
2758 		*ticket = attrs.ticket;
2759 
2760 	return (e.error);
2761 }
2762 
2763 int
pfctl_add_addr(struct pfctl_handle * h,const struct pfioc_pooladdr * pa,int which)2764 pfctl_add_addr(struct pfctl_handle *h, const struct pfioc_pooladdr *pa, int which)
2765 {
2766 	struct snl_writer nw;
2767 	struct snl_errmsg_data e = {};
2768 	struct nlmsghdr *hdr;
2769 	uint32_t seq_id;
2770 	int family_id;
2771 
2772 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2773 	if (family_id == 0)
2774 		return (ENOTSUP);
2775 
2776 	snl_init_writer(&h->ss, &nw);
2777 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_ADD_ADDR);
2778 
2779 	snl_add_msg_attr_u32(&nw, PF_AA_ACTION, pa->action);
2780 	snl_add_msg_attr_u32(&nw, PF_AA_TICKET, pa->ticket);
2781 	snl_add_msg_attr_u32(&nw, PF_AA_NR, pa->nr);
2782 	snl_add_msg_attr_u32(&nw, PF_AA_R_NUM, pa->r_num);
2783 	snl_add_msg_attr_u8(&nw, PF_AA_R_ACTION, pa->r_action);
2784 	snl_add_msg_attr_u8(&nw, PF_AA_R_LAST, pa->r_last);
2785 	snl_add_msg_attr_u8(&nw, PF_AA_AF, pa->af);
2786 	snl_add_msg_attr_string(&nw, PF_AA_ANCHOR, pa->anchor);
2787 	snl_add_msg_attr_pool_addr(&nw, PF_AA_ADDR, &pa->addr);
2788 	snl_add_msg_attr_u32(&nw, PF_AA_WHICH, which);
2789 
2790 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2791 		return (ENXIO);
2792 
2793 	seq_id = hdr->nlmsg_seq;
2794 
2795 	if (! snl_send_message(&h->ss, hdr))
2796 		return (ENXIO);
2797 
2798 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2799 	}
2800 
2801 	return (e.error);
2802 }
2803 
2804 static const struct snl_attr_parser ap_get_addrs[] = {
2805 	{ .type = PF_AA_NR, .off = 0, .cb = snl_attr_get_uint32 },
2806 };
2807 SNL_DECLARE_PARSER(get_addrs_parser, struct genlmsghdr, snl_f_p_empty, ap_get_addrs);
2808 
2809 int
pfctl_get_addrs(struct pfctl_handle * h,uint32_t ticket,uint32_t r_num,uint8_t r_action,const char * anchor,uint32_t * nr,int which)2810 pfctl_get_addrs(struct pfctl_handle *h, uint32_t ticket, uint32_t r_num,
2811     uint8_t r_action, const char *anchor, uint32_t *nr, int which)
2812 {
2813 	struct snl_writer nw;
2814 	struct snl_errmsg_data e = {};
2815 	struct nlmsghdr *hdr;
2816 	uint32_t seq_id;
2817 	int family_id;
2818 
2819 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2820 	if (family_id == 0)
2821 		return (ENOTSUP);
2822 
2823 	snl_init_writer(&h->ss, &nw);
2824 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_ADDRS);
2825 
2826 	snl_add_msg_attr_u32(&nw, PF_AA_TICKET, ticket);
2827 	snl_add_msg_attr_u32(&nw, PF_AA_R_NUM, r_num);
2828 	snl_add_msg_attr_u8(&nw, PF_AA_R_ACTION, r_action);
2829 	snl_add_msg_attr_string(&nw, PF_AA_ANCHOR, anchor);
2830 	snl_add_msg_attr_u32(&nw, PF_AA_WHICH, which);
2831 
2832 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2833 		return (ENXIO);
2834 
2835 	seq_id = hdr->nlmsg_seq;
2836 
2837 	if (! snl_send_message(&h->ss, hdr))
2838 		return (ENXIO);
2839 
2840 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2841 		if (! snl_parse_nlmsg(&h->ss, hdr, &get_addrs_parser, nr))
2842 			continue;
2843 	}
2844 
2845 	return (e.error);
2846 }
2847 
2848 #define _OUT(_field)	offsetof(struct pf_pooladdr, _field)
2849 static const struct snl_attr_parser ap_pool_addr[] = {
2850 	{ .type = PF_PA_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = snl_attr_get_nested },
2851 	{ .type = PF_PA_IFNAME, .off = _OUT(ifname), .arg_u32 = IFNAMSIZ, .cb = snl_attr_copy_string },
2852 };
2853 SNL_DECLARE_ATTR_PARSER(pool_addr_parser, ap_pool_addr);
2854 #undef _OUT
2855 
2856 #define _OUT(_field)	offsetof(struct pfioc_pooladdr, _field)
2857 static const struct snl_attr_parser ap_get_addr[] = {
2858 	{ .type = PF_AA_ACTION, .off = _OUT(action), .cb = snl_attr_get_uint32 },
2859 	{ .type = PF_AA_TICKET, .off = _OUT(ticket), .cb = snl_attr_get_uint32 },
2860 	{ .type = PF_AA_NR, .off = _OUT(nr), .cb = snl_attr_get_uint32 },
2861 	{ .type = PF_AA_R_NUM, .off = _OUT(r_num), .cb = snl_attr_get_uint32 },
2862 	{ .type = PF_AA_R_ACTION, .off = _OUT(r_action), .cb = snl_attr_get_uint8 },
2863 	{ .type = PF_AA_R_LAST, .off = _OUT(r_last), .cb = snl_attr_get_uint8 },
2864 	{ .type = PF_AA_AF, .off = _OUT(af), .cb = snl_attr_get_uint8 },
2865 	{ .type = PF_AA_ANCHOR, .off = _OUT(anchor), .arg_u32 = MAXPATHLEN, .cb = snl_attr_copy_string },
2866 	{ .type = PF_AA_ADDR, .off = _OUT(addr), .arg = &pool_addr_parser, .cb = snl_attr_get_nested },
2867 };
2868 SNL_DECLARE_PARSER(get_addr_parser, struct genlmsghdr, snl_f_p_empty, ap_get_addr);
2869 #undef _OUT
2870 
2871 int
pfctl_get_addr(struct pfctl_handle * h,uint32_t ticket,uint32_t r_num,uint8_t r_action,const char * anchor,uint32_t nr,struct pfioc_pooladdr * pa,int which)2872 pfctl_get_addr(struct pfctl_handle *h, uint32_t ticket, uint32_t r_num,
2873     uint8_t r_action, const char *anchor, uint32_t nr, struct pfioc_pooladdr *pa,
2874     int which)
2875 {
2876 	struct snl_writer nw;
2877 	struct snl_errmsg_data e = {};
2878 	struct nlmsghdr *hdr;
2879 	uint32_t seq_id;
2880 	int family_id;
2881 
2882 	family_id =snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2883 	if (family_id == 0)
2884 		return (ENOTSUP);
2885 
2886 	snl_init_writer(&h->ss, &nw);
2887 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_ADDR);
2888 
2889 	snl_add_msg_attr_u32(&nw, PF_AA_TICKET, ticket);
2890 	snl_add_msg_attr_u32(&nw, PF_AA_R_NUM, r_num);
2891 	snl_add_msg_attr_u8(&nw, PF_AA_R_ACTION, r_action);
2892 	snl_add_msg_attr_string(&nw, PF_AA_ANCHOR, anchor);
2893 	snl_add_msg_attr_u32(&nw, PF_AA_NR, nr);
2894 	snl_add_msg_attr_u32(&nw, PF_AA_WHICH, which);
2895 
2896 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2897 		return (ENXIO);
2898 
2899 	seq_id = hdr->nlmsg_seq;
2900 
2901 	if (! snl_send_message(&h->ss, hdr))
2902 		return (ENXIO);
2903 
2904 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2905 		if (! snl_parse_nlmsg(&h->ss, hdr, &get_addr_parser, pa))
2906 			continue;
2907 	}
2908 
2909 	return (e.error);
2910 }
2911 
2912 #define _OUT(_field)	offsetof(struct pfioc_ruleset, _field)
2913 static const struct snl_attr_parser ap_ruleset[] = {
2914 	{ .type = PF_RS_NR, .off = _OUT(nr), .cb = snl_attr_get_uint32 },
2915 	{ .type = PF_RS_NAME, .off = _OUT(name), .arg = (void *)PF_ANCHOR_NAME_SIZE, .cb = snl_attr_copy_string },
2916 };
2917 SNL_DECLARE_PARSER(ruleset_parser, struct genlmsghdr, snl_f_p_empty, ap_ruleset);
2918 #undef _OUT
2919 
2920 int
pfctl_get_rulesets(struct pfctl_handle * h,const char * path,uint32_t * nr)2921 pfctl_get_rulesets(struct pfctl_handle *h, const char *path, uint32_t *nr)
2922 {
2923 	struct snl_writer nw;
2924 	struct snl_errmsg_data e = {};
2925 	struct nlmsghdr *hdr;
2926 	struct pfioc_ruleset rs = {};
2927 	uint32_t seq_id;
2928 	int family_id;
2929 
2930 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2931 	if (family_id == 0)
2932 		return (ENOTSUP);
2933 
2934 	snl_init_writer(&h->ss, &nw);
2935 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_RULESETS);
2936 
2937 	snl_add_msg_attr_string(&nw, PF_RS_PATH, path);
2938 
2939 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2940 		return (ENXIO);
2941 
2942 	seq_id = hdr->nlmsg_seq;
2943 
2944 	if (! snl_send_message(&h->ss, hdr))
2945 		return (ENXIO);
2946 
2947 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2948 		if (! snl_parse_nlmsg(&h->ss, hdr, &ruleset_parser, &rs))
2949 			continue;
2950 	}
2951 
2952 	*nr = rs.nr;
2953 
2954 	return (e.error);
2955 }
2956 
2957 int
pfctl_get_ruleset(struct pfctl_handle * h,const char * path,uint32_t nr,struct pfioc_ruleset * rs)2958 pfctl_get_ruleset(struct pfctl_handle *h, const char *path, uint32_t nr, struct pfioc_ruleset *rs)
2959 {
2960 	struct snl_writer nw;
2961 	struct snl_errmsg_data e = {};
2962 	struct nlmsghdr *hdr;
2963 	uint32_t seq_id;
2964 	int family_id;
2965 
2966 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
2967 	if (family_id == 0)
2968 		return (ENOTSUP);
2969 
2970 	snl_init_writer(&h->ss, &nw);
2971 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_RULESET);
2972 
2973 	snl_add_msg_attr_string(&nw, PF_RS_PATH, path);
2974 	snl_add_msg_attr_u32(&nw, PF_RS_NR, nr);
2975 
2976 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
2977 		return (ENXIO);
2978 
2979 	seq_id = hdr->nlmsg_seq;
2980 
2981 	if (! snl_send_message(&h->ss, hdr))
2982 		return (ENXIO);
2983 
2984 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
2985 		if (! snl_parse_nlmsg(&h->ss, hdr, &ruleset_parser, rs))
2986 			continue;
2987 	}
2988 
2989 	return (e.error);
2990 }
2991 
2992 #define	_OUT(_field)	offsetof(struct pfctl_threshold, _field)
2993 static const struct snl_attr_parser ap_pfctl_threshold[] = {
2994 	{ .type = PF_TH_LIMIT, .off = _OUT(limit), .cb = snl_attr_get_uint32 },
2995 	{ .type = PF_TH_SECONDS, .off = _OUT(seconds), .cb = snl_attr_get_uint32 },
2996 	{ .type = PF_TH_COUNT, .off = _OUT(count), .cb = snl_attr_get_uint32 },
2997 	{ .type = PF_TH_LAST, .off = _OUT(last), .cb = snl_attr_get_uint32 },
2998 };
2999 SNL_DECLARE_ATTR_PARSER(pfctl_threshold_parser, ap_pfctl_threshold);
3000 #undef _OUT
3001 
3002 #define	_OUT(_field)	offsetof(struct pfctl_src_node, _field)
3003 static struct snl_attr_parser ap_srcnode[] = {
3004 	{ .type = PF_SN_ADDR, .off = _OUT(addr), .cb = snl_attr_get_in6_addr },
3005 	{ .type = PF_SN_RADDR, .off = _OUT(raddr), .cb = snl_attr_get_in6_addr },
3006 	{ .type = PF_SN_RULE_NR, .off = _OUT(rule), .cb = snl_attr_get_uint32 },
3007 	{ .type = PF_SN_BYTES_IN, .off = _OUT(bytes[0]), .cb = snl_attr_get_uint64 },
3008 	{ .type = PF_SN_BYTES_OUT, .off = _OUT(bytes[1]), .cb = snl_attr_get_uint64 },
3009 	{ .type = PF_SN_PACKETS_IN, .off = _OUT(packets[0]), .cb = snl_attr_get_uint64 },
3010 	{ .type = PF_SN_PACKETS_OUT, .off = _OUT(packets[1]), .cb = snl_attr_get_uint64 },
3011 	{ .type = PF_SN_STATES, .off = _OUT(states), .cb = snl_attr_get_uint32 },
3012 	{ .type = PF_SN_CONNECTIONS, .off = _OUT(conn), .cb = snl_attr_get_uint32 },
3013 	{ .type = PF_SN_AF, .off = _OUT(af), .cb = snl_attr_get_uint8 },
3014 	{ .type = PF_SN_RULE_TYPE, .off = _OUT(ruletype), .cb = snl_attr_get_uint8 },
3015 	{ .type = PF_SN_CREATION, .off = _OUT(creation), .cb = snl_attr_get_uint64 },
3016 	{ .type = PF_SN_EXPIRE, .off = _OUT(expire), .cb = snl_attr_get_uint64 },
3017 	{ .type = PF_SN_CONNECTION_RATE, .off = _OUT(conn_rate), .arg = &pfctl_threshold_parser, .cb = snl_attr_get_nested },
3018 	{ .type = PF_SN_NAF, .off = _OUT(naf), .cb = snl_attr_get_uint8 },
3019 };
3020 #undef _OUT
3021 SNL_DECLARE_PARSER(srcnode_parser, struct genlmsghdr, snl_f_p_empty, ap_srcnode);
3022 
3023 int
pfctl_get_srcnodes(struct pfctl_handle * h,pfctl_get_srcnode_fn fn,void * arg)3024 pfctl_get_srcnodes(struct pfctl_handle *h, pfctl_get_srcnode_fn fn, void *arg)
3025 {
3026 	struct snl_writer nw;
3027 	struct pfctl_src_node sn;
3028 	struct snl_errmsg_data e = {};
3029 	struct nlmsghdr *hdr;
3030 	uint32_t seq_id;
3031 	int family_id;
3032 	int ret;
3033 
3034 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
3035 	if (family_id == 0)
3036 		return (ENOTSUP);
3037 
3038 	snl_init_writer(&h->ss, &nw);
3039 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_SRCNODES);
3040 
3041 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
3042 		return (ENXIO);
3043 
3044 	seq_id = hdr->nlmsg_seq;
3045 
3046 	if (!snl_send_message(&h->ss, hdr))
3047 		return (ENXIO);
3048 
3049 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
3050 		if (!snl_parse_nlmsg(&h->ss, hdr, &srcnode_parser, &sn))
3051 			continue;
3052 
3053 		ret = fn(&sn, arg);
3054 		if (ret != 0)
3055 			return (ret);
3056 	}
3057 
3058 	return (e.error);
3059 }
3060 
3061 static struct snl_attr_parser ap_ndel[] = {
3062 	{ .type = PF_T_NBR_DELETED, .off = 0, .cb = snl_attr_get_uint32 },
3063 };
3064 SNL_DECLARE_PARSER(ndel_parser, struct genlmsghdr, snl_f_p_empty, ap_ndel);
3065 
3066 int
pfctl_clear_tables(struct pfctl_handle * h,struct pfr_table * filter,int * ndel,int flags)3067 pfctl_clear_tables(struct pfctl_handle *h, struct pfr_table *filter,
3068     int *ndel, int flags)
3069 {
3070 	struct snl_writer nw;
3071 	struct snl_errmsg_data e = {};
3072 	struct nlmsghdr *hdr;
3073 	uint32_t seq_id;
3074 	int family_id;
3075 
3076 	family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME);
3077 	if (family_id == 0)
3078 		return (ENOTSUP);
3079 
3080 	snl_init_writer(&h->ss, &nw);
3081 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_CLEAR_TABLES);
3082 
3083 	snl_add_msg_attr_string(&nw, PF_T_ANCHOR, filter->pfrt_anchor);
3084 	snl_add_msg_attr_string(&nw, PF_T_NAME, filter->pfrt_name);
3085 	snl_add_msg_attr_u32(&nw, PF_T_TABLE_FLAGS, filter->pfrt_flags);
3086 	snl_add_msg_attr_u32(&nw, PF_T_FLAGS, flags);
3087 
3088 	if ((hdr = snl_finalize_msg(&nw)) == NULL)
3089 		return (ENXIO);
3090 
3091 	seq_id = hdr->nlmsg_seq;
3092 
3093 	if (!snl_send_message(&h->ss, hdr))
3094 		return (ENXIO);
3095 
3096 	while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
3097 		if (!snl_parse_nlmsg(&h->ss, hdr, &ndel_parser, ndel))
3098 			continue;
3099 	}
3100 
3101 	return (e.error);
3102 }
3103