2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
36 #include <sys/ioctl.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
42 #include <net/pfvar.h>
43 #include <netinet/in.h>
53 static int _pfctl_clear_states(int , const struct pfctl_kill *,
54 unsigned int *, uint64_t);
57 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems,
58 u_int8_t *numbers, size_t *nelems)
63 tmp = nvlist_get_number_array(nvl, name, &elems);
64 assert(elems <= maxelems);
66 for (size_t i = 0; i < elems; i++)
74 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems,
75 u_int16_t *numbers, size_t *nelems)
80 tmp = nvlist_get_number_array(nvl, name, &elems);
81 assert(elems <= maxelems);
83 for (size_t i = 0; i < elems; i++)
91 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems,
92 u_int32_t *numbers, size_t *nelems)
97 tmp = nvlist_get_number_array(nvl, name, &elems);
98 assert(elems <= maxelems);
100 for (size_t i = 0; i < elems; i++)
108 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems,
109 u_int64_t *numbers, size_t *nelems)
114 tmp = nvlist_get_number_array(nvl, name, &elems);
115 assert(elems <= maxelems);
117 for (size_t i = 0; i < elems; i++)
125 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name,
126 const struct pf_addr *addr)
128 nvlist_t *nvl = nvlist_create(0);
130 nvlist_add_binary(nvl, "addr", addr, sizeof(*addr));
132 nvlist_add_nvlist(nvparent, name, nvl);
137 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr)
142 data = nvlist_get_binary(nvl, "addr", &len);
143 assert(len == sizeof(struct pf_addr));
144 memcpy(addr, data, len);
148 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name,
149 const struct pf_addr_wrap *addr)
151 nvlist_t *nvl = nvlist_create(0);
153 nvlist_add_number(nvl, "type", addr->type);
154 nvlist_add_number(nvl, "iflags", addr->iflags);
155 if (addr->type == PF_ADDR_DYNIFTL)
156 nvlist_add_string(nvl, "ifname", addr->v.ifname);
157 if (addr->type == PF_ADDR_TABLE)
158 nvlist_add_string(nvl, "tblname", addr->v.tblname);
159 pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr);
160 pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask);
162 nvlist_add_nvlist(nvparent, name, nvl);
167 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
169 addr->type = nvlist_get_number(nvl, "type");
170 addr->iflags = nvlist_get_number(nvl, "iflags");
171 if (addr->type == PF_ADDR_DYNIFTL)
172 strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"),
174 if (addr->type == PF_ADDR_TABLE)
175 strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"),
178 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr);
179 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask);
183 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name,
184 const struct pf_rule_addr *addr)
187 nvlist_t *nvl = nvlist_create(0);
189 pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr);
190 ports[0] = addr->port[0];
191 ports[1] = addr->port[1];
192 nvlist_add_number_array(nvl, "port", ports, 2);
193 nvlist_add_number(nvl, "neg", addr->neg);
194 nvlist_add_number(nvl, "port_op", addr->port_op);
196 nvlist_add_nvlist(nvparent, name, nvl);
201 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
203 pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr);
205 pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL);
206 addr->neg = nvlist_get_number(nvl, "neg");
207 addr->port_op = nvlist_get_number(nvl, "port_op");
211 pfctl_nv_add_mape(nvlist_t *nvparent, const char *name,
212 const struct pf_mape_portset *mape)
214 nvlist_t *nvl = nvlist_create(0);
216 nvlist_add_number(nvl, "offset", mape->offset);
217 nvlist_add_number(nvl, "psidlen", mape->psidlen);
218 nvlist_add_number(nvl, "psid", mape->psid);
219 nvlist_add_nvlist(nvparent, name, nvl);
224 pfctl_nv_add_pool(nvlist_t *nvparent, const char *name,
225 const struct pfctl_pool *pool)
228 nvlist_t *nvl = nvlist_create(0);
230 nvlist_add_binary(nvl, "key", &pool->key, sizeof(pool->key));
231 pfctl_nv_add_addr(nvl, "counter", &pool->counter);
232 nvlist_add_number(nvl, "tblidx", pool->tblidx);
234 ports[0] = pool->proxy_port[0];
235 ports[1] = pool->proxy_port[1];
236 nvlist_add_number_array(nvl, "proxy_port", ports, 2);
237 nvlist_add_number(nvl, "opts", pool->opts);
238 pfctl_nv_add_mape(nvl, "mape", &pool->mape);
240 nvlist_add_nvlist(nvparent, name, nvl);
245 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape)
247 mape->offset = nvlist_get_number(nvl, "offset");
248 mape->psidlen = nvlist_get_number(nvl, "psidlen");
249 mape->psid = nvlist_get_number(nvl, "psid");
253 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool)
258 data = nvlist_get_binary(nvl, "key", &len);
259 assert(len == sizeof(pool->key));
260 memcpy(&pool->key, data, len);
262 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter);
264 pool->tblidx = nvlist_get_number(nvl, "tblidx");
265 pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL);
266 pool->opts = nvlist_get_number(nvl, "opts");
268 if (nvlist_exists_nvlist(nvl, "mape"))
269 pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape);
273 pfctl_nv_add_uid(nvlist_t *nvparent, const char *name,
274 const struct pf_rule_uid *uid)
277 nvlist_t *nvl = nvlist_create(0);
279 uids[0] = uid->uid[0];
280 uids[1] = uid->uid[1];
281 nvlist_add_number_array(nvl, "uid", uids, 2);
282 nvlist_add_number(nvl, "op", uid->op);
284 nvlist_add_nvlist(nvparent, name, nvl);
289 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
291 pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL);
292 uid->op = nvlist_get_number(nvl, "op");
296 pfctl_nv_add_divert(nvlist_t *nvparent, const char *name,
297 const struct pfctl_rule *r)
299 nvlist_t *nvl = nvlist_create(0);
301 pfctl_nv_add_addr(nvl, "addr", &r->divert.addr);
302 nvlist_add_number(nvl, "port", r->divert.port);
304 nvlist_add_nvlist(nvparent, name, nvl);
309 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule)
311 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr);
312 rule->divert.port = nvlist_get_number(nvl, "port");
316 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule)
318 const uint64_t *skip;
319 const char *const *labels;
320 size_t skipcount, labelcount;
322 rule->nr = nvlist_get_number(nvl, "nr");
324 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src);
325 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst);
327 skip = nvlist_get_number_array(nvl, "skip", &skipcount);
329 assert(skipcount == PF_SKIP_COUNT);
330 for (int i = 0; i < PF_SKIP_COUNT; i++)
331 rule->skip[i].nr = skip[i];
333 labels = nvlist_get_string_array(nvl, "labels", &labelcount);
334 assert(labelcount <= PF_RULE_MAX_LABEL_COUNT);
335 for (size_t i = 0; i < labelcount; i++)
336 strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE);
337 strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
338 strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
339 strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE);
340 strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
342 strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
345 strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"),
348 pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool);
350 rule->evaluations = nvlist_get_number(nvl, "evaluations");
351 pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL);
352 pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL);
354 rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint");
356 rule->rtableid = nvlist_get_number(nvl, "rtableid");
357 pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL);
358 rule->max_states = nvlist_get_number(nvl, "max_states");
359 rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes");
360 rule->max_src_states = nvlist_get_number(nvl, "max_src_states");
361 rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn");
362 rule->max_src_conn_rate.limit =
363 nvlist_get_number(nvl, "max_src_conn_rate.limit");
364 rule->max_src_conn_rate.seconds =
365 nvlist_get_number(nvl, "max_src_conn_rate.seconds");
366 rule->qid = nvlist_get_number(nvl, "qid");
367 rule->pqid = nvlist_get_number(nvl, "pqid");
368 rule->prob = nvlist_get_number(nvl, "prob");
369 rule->cuid = nvlist_get_number(nvl, "cuid");
370 rule->cpid = nvlist_get_number(nvl, "cpid");
372 rule->return_icmp = nvlist_get_number(nvl, "return_icmp");
373 rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6");
374 rule->max_mss = nvlist_get_number(nvl, "max_mss");
375 rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags");
377 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid);
378 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"),
379 (struct pf_rule_uid *)&rule->gid);
381 rule->rule_flag = nvlist_get_number(nvl, "rule_flag");
382 rule->action = nvlist_get_number(nvl, "action");
383 rule->direction = nvlist_get_number(nvl, "direction");
384 rule->log = nvlist_get_number(nvl, "log");
385 rule->logif = nvlist_get_number(nvl, "logif");
386 rule->quick = nvlist_get_number(nvl, "quick");
387 rule->ifnot = nvlist_get_number(nvl, "ifnot");
388 rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not");
389 rule->natpass = nvlist_get_number(nvl, "natpass");
391 rule->keep_state = nvlist_get_number(nvl, "keep_state");
392 rule->af = nvlist_get_number(nvl, "af");
393 rule->proto = nvlist_get_number(nvl, "proto");
394 rule->type = nvlist_get_number(nvl, "type");
395 rule->code = nvlist_get_number(nvl, "code");
396 rule->flags = nvlist_get_number(nvl, "flags");
397 rule->flagset = nvlist_get_number(nvl, "flagset");
398 rule->min_ttl = nvlist_get_number(nvl, "min_ttl");
399 rule->allow_opts = nvlist_get_number(nvl, "allow_opts");
400 rule->rt = nvlist_get_number(nvl, "rt");
401 rule->return_ttl = nvlist_get_number(nvl, "return_ttl");
402 rule->tos = nvlist_get_number(nvl, "tos");
403 rule->set_tos = nvlist_get_number(nvl, "set_tos");
404 rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
405 rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
407 rule->flush = nvlist_get_number(nvl, "flush");
408 rule->prio = nvlist_get_number(nvl, "prio");
409 pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL);
411 pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule);
413 rule->states_cur = nvlist_get_number(nvl, "states_cur");
414 rule->states_tot = nvlist_get_number(nvl, "states_tot");
415 rule->src_nodes = nvlist_get_number(nvl, "src_nodes");
419 pfctl_add_rule(int dev, const struct pfctl_rule *r, const char *anchor,
420 const char *anchor_call, u_int32_t ticket, u_int32_t pool_ticket)
423 u_int64_t timeouts[PFTM_MAX];
424 u_int64_t set_prio[2];
425 nvlist_t *nvl, *nvlr;
429 nvl = nvlist_create(0);
430 nvlr = nvlist_create(0);
432 nvlist_add_number(nvl, "ticket", ticket);
433 nvlist_add_number(nvl, "pool_ticket", pool_ticket);
434 nvlist_add_string(nvl, "anchor", anchor);
435 nvlist_add_string(nvl, "anchor_call", anchor_call);
437 nvlist_add_number(nvlr, "nr", r->nr);
438 pfctl_nv_add_rule_addr(nvlr, "src", &r->src);
439 pfctl_nv_add_rule_addr(nvlr, "dst", &r->dst);
442 while (r->label[labelcount][0] != 0 &&
443 labelcount < PF_RULE_MAX_LABEL_COUNT) {
444 nvlist_append_string_array(nvlr, "labels",
445 r->label[labelcount]);
449 nvlist_add_string(nvlr, "ifname", r->ifname);
450 nvlist_add_string(nvlr, "qname", r->qname);
451 nvlist_add_string(nvlr, "pqname", r->pqname);
452 nvlist_add_string(nvlr, "tagname", r->tagname);
453 nvlist_add_string(nvlr, "match_tagname", r->match_tagname);
454 nvlist_add_string(nvlr, "overload_tblname", r->overload_tblname);
456 pfctl_nv_add_pool(nvlr, "rpool", &r->rpool);
458 nvlist_add_number(nvlr, "os_fingerprint", r->os_fingerprint);
460 nvlist_add_number(nvlr, "rtableid", r->rtableid);
461 for (int i = 0; i < PFTM_MAX; i++)
462 timeouts[i] = r->timeout[i];
463 nvlist_add_number_array(nvlr, "timeout", timeouts, PFTM_MAX);
464 nvlist_add_number(nvlr, "max_states", r->max_states);
465 nvlist_add_number(nvlr, "max_src_nodes", r->max_src_nodes);
466 nvlist_add_number(nvlr, "max_src_states", r->max_src_states);
467 nvlist_add_number(nvlr, "max_src_conn", r->max_src_conn);
468 nvlist_add_number(nvlr, "max_src_conn_rate.limit",
469 r->max_src_conn_rate.limit);
470 nvlist_add_number(nvlr, "max_src_conn_rate.seconds",
471 r->max_src_conn_rate.seconds);
472 nvlist_add_number(nvlr, "prob", r->prob);
473 nvlist_add_number(nvlr, "cuid", r->cuid);
474 nvlist_add_number(nvlr, "cpid", r->cpid);
476 nvlist_add_number(nvlr, "return_icmp", r->return_icmp);
477 nvlist_add_number(nvlr, "return_icmp6", r->return_icmp6);
479 nvlist_add_number(nvlr, "max_mss", r->max_mss);
480 nvlist_add_number(nvlr, "scrub_flags", r->scrub_flags);
482 pfctl_nv_add_uid(nvlr, "uid", &r->uid);
483 pfctl_nv_add_uid(nvlr, "gid", (const struct pf_rule_uid *)&r->gid);
485 nvlist_add_number(nvlr, "rule_flag", r->rule_flag);
486 nvlist_add_number(nvlr, "action", r->action);
487 nvlist_add_number(nvlr, "direction", r->direction);
488 nvlist_add_number(nvlr, "log", r->log);
489 nvlist_add_number(nvlr, "logif", r->logif);
490 nvlist_add_number(nvlr, "quick", r->quick);
491 nvlist_add_number(nvlr, "ifnot", r->ifnot);
492 nvlist_add_number(nvlr, "match_tag_not", r->match_tag_not);
493 nvlist_add_number(nvlr, "natpass", r->natpass);
495 nvlist_add_number(nvlr, "keep_state", r->keep_state);
496 nvlist_add_number(nvlr, "af", r->af);
497 nvlist_add_number(nvlr, "proto", r->proto);
498 nvlist_add_number(nvlr, "type", r->type);
499 nvlist_add_number(nvlr, "code", r->code);
500 nvlist_add_number(nvlr, "flags", r->flags);
501 nvlist_add_number(nvlr, "flagset", r->flagset);
502 nvlist_add_number(nvlr, "min_ttl", r->min_ttl);
503 nvlist_add_number(nvlr, "allow_opts", r->allow_opts);
504 nvlist_add_number(nvlr, "rt", r->rt);
505 nvlist_add_number(nvlr, "return_ttl", r->return_ttl);
506 nvlist_add_number(nvlr, "tos", r->tos);
507 nvlist_add_number(nvlr, "set_tos", r->set_tos);
508 nvlist_add_number(nvlr, "anchor_relative", r->anchor_relative);
509 nvlist_add_number(nvlr, "anchor_wildcard", r->anchor_wildcard);
511 nvlist_add_number(nvlr, "flush", r->flush);
513 nvlist_add_number(nvlr, "prio", r->prio);
514 set_prio[0] = r->set_prio[0];
515 set_prio[1] = r->set_prio[1];
516 nvlist_add_number_array(nvlr, "set_prio", set_prio, 2);
518 pfctl_nv_add_divert(nvlr, "divert", r);
520 nvlist_add_nvlist(nvl, "rule", nvlr);
521 nvlist_destroy(nvlr);
523 /* Now do the call. */
524 nv.data = nvlist_pack(nvl, &nv.len);
527 ret = ioctl(dev, DIOCADDRULENV, &nv);
536 pfctl_get_rule(int dev, u_int32_t nr, u_int32_t ticket, const char *anchor,
537 u_int32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
539 return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule,
540 anchor_call, false));
543 int pfctl_get_clear_rule(int dev, u_int32_t nr, u_int32_t ticket,
544 const char *anchor, u_int32_t ruleset, struct pfctl_rule *rule,
545 char *anchor_call, bool clear)
552 nvl = nvlist_create(0);
556 nvlist_add_number(nvl, "nr", nr);
557 nvlist_add_number(nvl, "ticket", ticket);
558 nvlist_add_string(nvl, "anchor", anchor);
559 nvlist_add_number(nvl, "ruleset", ruleset);
562 nvlist_add_bool(nvl, "clear_counter", true);
564 nvlpacked = nvlist_pack(nvl, &nv.len);
565 if (nvlpacked == NULL) {
569 nv.data = malloc(8182);
571 assert(nv.len <= nv.size);
572 memcpy(nv.data, nvlpacked, nv.len);
577 ret = ioctl(dev, DIOCGETRULENV, &nv);
583 nvl = nvlist_unpack(nv.data, nv.len, 0);
589 pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule);
592 strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),
602 pfctl_set_keepcounters(int dev, bool keep)
608 nvl = nvlist_create(0);
610 nvlist_add_bool(nvl, "keep_counters", keep);
612 nv.data = nvlist_pack(nvl, &nv.len);
617 ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv);
624 pfctl_nv_add_state_cmp(nvlist_t *nvl, const char *name,
625 const struct pfctl_state_cmp *cmp)
629 nv = nvlist_create(0);
631 nvlist_add_number(nv, "id", cmp->id);
632 nvlist_add_number(nv, "creatorid", cmp->creatorid);
633 nvlist_add_number(nv, "direction", cmp->direction);
635 nvlist_add_nvlist(nvl, name, nv);
640 pf_nvstate_scrub_to_state_scrub(const nvlist_t *nvl,
641 struct pfctl_state_scrub *scrub)
643 bzero(scrub, sizeof(*scrub));
645 scrub->timestamp = nvlist_get_bool(nvl, "timestamp");
646 scrub->ttl = nvlist_get_number(nvl, "ttl");
647 scrub->ts_mod = nvlist_get_number(nvl, "ts_mod");
651 pf_nvstate_peer_to_state_peer(const nvlist_t *nvl,
652 struct pfctl_state_peer *peer)
654 bzero(peer, sizeof(*peer));
656 if (nvlist_exists_nvlist(nvl, "scrub")) {
657 peer->scrub = malloc(sizeof(*peer->scrub));
658 pf_nvstate_scrub_to_state_scrub(
659 nvlist_get_nvlist(nvl, "scrub"),
663 peer->seqlo = nvlist_get_number(nvl, "seqlo");
664 peer->seqhi = nvlist_get_number(nvl, "seqhi");
665 peer->seqdiff = nvlist_get_number(nvl, "seqdiff");
666 peer->max_win = nvlist_get_number(nvl, "max_win");
667 peer->mss = nvlist_get_number(nvl, "mss");
668 peer->state = nvlist_get_number(nvl, "state");
669 peer->wscale = nvlist_get_number(nvl, "wscale");
673 pf_nvstate_key_to_state_key(const nvlist_t *nvl, struct pfctl_state_key *key)
675 const nvlist_t * const *tmp;
678 bzero(key, sizeof(*key));
680 tmp = nvlist_get_nvlist_array(nvl, "addr", &count);
683 for (int i = 0; i < 2; i++)
684 pf_nvaddr_to_addr(tmp[i], &key->addr[i]);
686 pf_nvuint_16_array(nvl, "port", 2, key->port, NULL);
688 key->af = nvlist_get_number(nvl, "af");
689 key->proto = nvlist_get_number(nvl, "proto");
693 pf_nvstate_to_state(const nvlist_t *nvl, struct pfctl_state *s)
695 bzero(s, sizeof(*s));
697 s->id = nvlist_get_number(nvl, "id");
698 s->creatorid = nvlist_get_number(nvl, "creatorid");
699 s->direction = nvlist_get_number(nvl, "direction");
701 pf_nvstate_peer_to_state_peer(nvlist_get_nvlist(nvl, "src"), &s->src);
702 pf_nvstate_peer_to_state_peer(nvlist_get_nvlist(nvl, "dst"), &s->dst);
704 pf_nvstate_key_to_state_key(nvlist_get_nvlist(nvl, "stack_key"),
706 pf_nvstate_key_to_state_key(nvlist_get_nvlist(nvl, "wire_key"),
709 strlcpy(s->ifname, nvlist_get_string(nvl, "ifname"),
711 strlcpy(s->orig_ifname, nvlist_get_string(nvl, "orig_ifname"),
712 sizeof(s->orig_ifname));
714 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "rt_addr"), &s->rt_addr);
715 s->rule = nvlist_get_number(nvl, "rule");
716 s->anchor = nvlist_get_number(nvl, "anchor");
717 s->nat_rule = nvlist_get_number(nvl, "nat_rule");
718 s->creation = nvlist_get_number(nvl, "creation");
719 s->expire = nvlist_get_number(nvl, "expire");
721 pf_nvuint_64_array(nvl, "packets", 2, s->packets, NULL);
722 pf_nvuint_64_array(nvl, "bytes", 2, s->bytes, NULL);
724 s->log = nvlist_get_number(nvl, "log");
725 s->state_flags = nvlist_get_number(nvl, "state_flags");
726 s->timeout = nvlist_get_number(nvl, "timeout");
727 s->sync_flags = nvlist_get_number(nvl, "sync_flags");
731 pfctl_get_states(int dev, struct pfctl_states *states)
734 nvlist_t *nvl = NULL;
735 const nvlist_t * const *slist;
739 bzero(states, sizeof(*states));
740 TAILQ_INIT(&states->states);
742 /* Just enough to get a number, and we'll grow from there. */
743 nv.data = malloc(64);
744 nv.len = nv.size = 64;
747 if (ioctl(dev, DIOCGETSTATESNV, &nv)) {
752 nvl = nvlist_unpack(nv.data, nv.len, 0);
758 states->count = nvlist_get_number(nvl, "count");
760 /* Are there any states? */
761 if (states->count == 0)
764 if (nvlist_exists_nvlist_array(nvl, "states"))
765 slist = nvlist_get_nvlist_array(nvl, "states", &found_count);
769 if (found_count < states->count) {
770 size_t new_size = nv.size +
771 (nv.size * states->count / (found_count + 1) * 2);
773 /* Our buffer is too small. Estimate what we need based
774 * on how many states fit in the previous allocation
775 * and how many states there are. Doubled for margin.
777 nv.data = realloc(nv.data, new_size);
780 if (nv.data == NULL) {
787 for (size_t i = 0; i < found_count; i++) {
788 struct pfctl_state *s = malloc(sizeof(*s));
790 pfctl_free_states(states);
795 pf_nvstate_to_state(slist[i], s);
796 TAILQ_INSERT_TAIL(&states->states, s, entry);
809 pfctl_free_states(struct pfctl_states *states)
811 struct pfctl_state *s, *tmp;
813 TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) {
817 bzero(states, sizeof(*states));
821 _pfctl_clear_states(int dev, const struct pfctl_kill *kill,
822 unsigned int *killed, uint64_t ioctlval)
828 nvl = nvlist_create(0);
830 pfctl_nv_add_state_cmp(nvl, "cmp", &kill->cmp);
831 nvlist_add_number(nvl, "af", kill->af);
832 nvlist_add_number(nvl, "proto", kill->proto);
833 pfctl_nv_add_rule_addr(nvl, "src", &kill->src);
834 pfctl_nv_add_rule_addr(nvl, "dst", &kill->dst);
835 pfctl_nv_add_rule_addr(nvl, "rt_addr", &kill->rt_addr);
836 nvlist_add_string(nvl, "ifname", kill->ifname);
837 nvlist_add_string(nvl, "label", kill->label);
838 nvlist_add_bool(nvl, "kill_match", kill->kill_match);
840 nv.data = nvlist_pack(nvl, &nv.len);
845 ret = ioctl(dev, ioctlval, &nv);
851 nvl = nvlist_unpack(nv.data, nv.len, 0);
858 *killed = nvlist_get_number(nvl, "killed");
867 pfctl_clear_states(int dev, const struct pfctl_kill *kill,
868 unsigned int *killed)
870 return (_pfctl_clear_states(dev, kill, killed, DIOCCLRSTATESNV));
874 pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed)
876 return (_pfctl_clear_states(dev, kill, killed, DIOCKILLSTATESNV));