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>
54 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems,
55 u_int8_t *numbers, size_t *nelems)
60 tmp = nvlist_get_number_array(nvl, name, &elems);
61 assert(elems <= maxelems);
63 for (size_t i = 0; i < elems; i++)
71 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems,
72 u_int16_t *numbers, size_t *nelems)
77 tmp = nvlist_get_number_array(nvl, name, &elems);
78 assert(elems <= maxelems);
80 for (size_t i = 0; i < elems; i++)
88 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems,
89 u_int32_t *numbers, size_t *nelems)
94 tmp = nvlist_get_number_array(nvl, name, &elems);
95 assert(elems <= maxelems);
97 for (size_t i = 0; i < elems; i++)
105 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems,
106 u_int64_t *numbers, size_t *nelems)
111 tmp = nvlist_get_number_array(nvl, name, &elems);
112 assert(elems <= maxelems);
114 for (size_t i = 0; i < elems; i++)
122 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name,
123 const struct pf_addr *addr)
125 nvlist_t *nvl = nvlist_create(0);
127 nvlist_add_binary(nvl, "addr", addr, sizeof(*addr));
129 nvlist_add_nvlist(nvparent, name, nvl);
133 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr)
138 data = nvlist_get_binary(nvl, "addr", &len);
139 assert(len == sizeof(struct pf_addr));
140 memcpy(addr, data, len);
144 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name,
145 const struct pf_addr_wrap *addr)
147 nvlist_t *nvl = nvlist_create(0);
149 nvlist_add_number(nvl, "type", addr->type);
150 nvlist_add_number(nvl, "iflags", addr->iflags);
151 nvlist_add_string(nvl, "ifname", addr->v.ifname);
152 nvlist_add_string(nvl, "tblname", addr->v.tblname);
153 pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr);
154 pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask);
156 nvlist_add_nvlist(nvparent, name, nvl);
160 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
162 addr->type = nvlist_get_number(nvl, "type");
163 addr->iflags = nvlist_get_number(nvl, "iflags");
164 strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
165 strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"),
168 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr);
169 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask);
173 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name,
174 const struct pf_rule_addr *addr)
177 nvlist_t *nvl = nvlist_create(0);
179 pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr);
180 ports[0] = addr->port[0];
181 ports[1] = addr->port[1];
182 nvlist_add_number_array(nvl, "port", ports, 2);
183 nvlist_add_number(nvl, "neg", addr->neg);
184 nvlist_add_number(nvl, "port_op", addr->port_op);
186 nvlist_add_nvlist(nvparent, name, nvl);
190 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
192 pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr);
194 pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL);
195 addr->neg = nvlist_get_number(nvl, "neg");
196 addr->port_op = nvlist_get_number(nvl, "port_op");
200 pfctl_nv_add_pool(nvlist_t *nvparent, const char *name,
201 const struct pf_pool *pool)
204 nvlist_t *nvl = nvlist_create(0);
206 nvlist_add_binary(nvl, "key", &pool->key, sizeof(pool->key));
207 pfctl_nv_add_addr(nvl, "counter", &pool->counter);
208 nvlist_add_number(nvl, "tblidx", pool->tblidx);
210 ports[0] = pool->proxy_port[0];
211 ports[1] = pool->proxy_port[1];
212 nvlist_add_number_array(nvl, "proxy_port", ports, 2);
213 nvlist_add_number(nvl, "opts", pool->opts);
215 nvlist_add_nvlist(nvparent, name, nvl);
219 pf_nvpool_to_pool(const nvlist_t *nvl, struct pf_pool *pool)
224 data = nvlist_get_binary(nvl, "key", &len);
225 assert(len == sizeof(pool->key));
226 memcpy(&pool->key, data, len);
228 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter);
230 pool->tblidx = nvlist_get_number(nvl, "tblidx");
231 pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL);
232 pool->opts = nvlist_get_number(nvl, "opts");
236 pfctl_nv_add_uid(nvlist_t *nvparent, const char *name,
237 const struct pf_rule_uid *uid)
240 nvlist_t *nvl = nvlist_create(0);
242 uids[0] = uid->uid[0];
243 uids[1] = uid->uid[1];
244 nvlist_add_number_array(nvl, "uid", uids, 2);
245 nvlist_add_number(nvl, "op", uid->op);
247 nvlist_add_nvlist(nvparent, name, nvl);
251 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
253 pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL);
254 uid->op = nvlist_get_number(nvl, "op");
258 pfctl_nv_add_divert(nvlist_t *nvparent, const char *name,
259 const struct pfctl_rule *r)
261 nvlist_t *nvl = nvlist_create(0);
263 pfctl_nv_add_addr(nvl, "addr", &r->divert.addr);
264 nvlist_add_number(nvl, "port", r->divert.port);
266 nvlist_add_nvlist(nvparent, name, nvl);
270 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule)
272 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr);
273 rule->divert.port = nvlist_get_number(nvl, "port");
277 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule)
279 const uint64_t *skip;
282 rule->nr = nvlist_get_number(nvl, "nr");
284 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src);
285 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst);
287 skip = nvlist_get_number_array(nvl, "skip", &skipcount);
289 assert(skipcount == PF_SKIP_COUNT);
290 for (int i = 0; i < PF_SKIP_COUNT; i++)
291 rule->skip[i].nr = skip[i];
293 strlcpy(rule->label, nvlist_get_string(nvl, "label"), PF_RULE_LABEL_SIZE);
294 strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ);
295 strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE);
296 strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE);
297 strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"),
299 strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"),
302 strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"),
305 pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool);
307 rule->evaluations = nvlist_get_number(nvl, "evaluations");
308 pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL);
309 pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL);
311 rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint");
313 rule->rtableid = nvlist_get_number(nvl, "rtableid");
314 pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL);
315 rule->max_states = nvlist_get_number(nvl, "max_states");
316 rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes");
317 rule->max_src_states = nvlist_get_number(nvl, "max_src_states");
318 rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn");
319 rule->max_src_conn_rate.limit =
320 nvlist_get_number(nvl, "max_src_conn_rate.limit");
321 rule->max_src_conn_rate.seconds =
322 nvlist_get_number(nvl, "max_src_conn_rate.seconds");
323 rule->qid = nvlist_get_number(nvl, "qid");
324 rule->pqid = nvlist_get_number(nvl, "pqid");
325 rule->prob = nvlist_get_number(nvl, "prob");
326 rule->cuid = nvlist_get_number(nvl, "cuid");
327 rule->cpid = nvlist_get_number(nvl, "cpid");
329 rule->return_icmp = nvlist_get_number(nvl, "return_icmp");
330 rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6");
331 rule->max_mss = nvlist_get_number(nvl, "max_mss");
332 rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags");
334 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid);
335 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"),
336 (struct pf_rule_uid *)&rule->gid);
338 rule->rule_flag = nvlist_get_number(nvl, "rule_flag");
339 rule->action = nvlist_get_number(nvl, "action");
340 rule->direction = nvlist_get_number(nvl, "direction");
341 rule->log = nvlist_get_number(nvl, "log");
342 rule->logif = nvlist_get_number(nvl, "logif");
343 rule->quick = nvlist_get_number(nvl, "quick");
344 rule->ifnot = nvlist_get_number(nvl, "ifnot");
345 rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not");
346 rule->natpass = nvlist_get_number(nvl, "natpass");
348 rule->keep_state = nvlist_get_number(nvl, "keep_state");
349 rule->af = nvlist_get_number(nvl, "af");
350 rule->proto = nvlist_get_number(nvl, "proto");
351 rule->type = nvlist_get_number(nvl, "type");
352 rule->code = nvlist_get_number(nvl, "code");
353 rule->flags = nvlist_get_number(nvl, "flags");
354 rule->flagset = nvlist_get_number(nvl, "flagset");
355 rule->min_ttl = nvlist_get_number(nvl, "min_ttl");
356 rule->allow_opts = nvlist_get_number(nvl, "allow_opts");
357 rule->rt = nvlist_get_number(nvl, "rt");
358 rule->return_ttl = nvlist_get_number(nvl, "return_ttl");
359 rule->tos = nvlist_get_number(nvl, "tos");
360 rule->set_tos = nvlist_get_number(nvl, "set_tos");
361 rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative");
362 rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard");
364 rule->flush = nvlist_get_number(nvl, "flush");
365 rule->prio = nvlist_get_number(nvl, "prio");
366 pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL);
368 pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule);
370 rule->states_cur = nvlist_get_number(nvl, "states_cur");
371 rule->states_tot = nvlist_get_number(nvl, "states_tot");
372 rule->src_nodes = nvlist_get_number(nvl, "src_nodes");
376 pfctl_add_rule(int dev, const struct pfctl_rule *r, const char *anchor,
377 const char *anchor_call, u_int32_t ticket, u_int32_t pool_ticket)
380 u_int64_t timeouts[PFTM_MAX];
381 u_int64_t set_prio[2];
382 nvlist_t *nvl, *nvlr;
385 nvl = nvlist_create(0);
386 nvlr = nvlist_create(0);
388 nvlist_add_number(nvl, "ticket", ticket);
389 nvlist_add_number(nvl, "pool_ticket", pool_ticket);
390 nvlist_add_string(nvl, "anchor", anchor);
391 nvlist_add_string(nvl, "anchor_call", anchor_call);
393 nvlist_add_number(nvlr, "nr", r->nr);
394 pfctl_nv_add_rule_addr(nvlr, "src", &r->src);
395 pfctl_nv_add_rule_addr(nvlr, "dst", &r->dst);
397 nvlist_add_string(nvlr, "label", r->label);
398 nvlist_add_string(nvlr, "ifname", r->ifname);
399 nvlist_add_string(nvlr, "qname", r->qname);
400 nvlist_add_string(nvlr, "pqname", r->pqname);
401 nvlist_add_string(nvlr, "tagname", r->tagname);
402 nvlist_add_string(nvlr, "match_tagname", r->match_tagname);
403 nvlist_add_string(nvlr, "overload_tblname", r->overload_tblname);
405 pfctl_nv_add_pool(nvlr, "rpool", &r->rpool);
407 nvlist_add_number(nvlr, "os_fingerprint", r->os_fingerprint);
409 nvlist_add_number(nvlr, "rtableid", r->rtableid);
410 for (int i = 0; i < PFTM_MAX; i++)
411 timeouts[i] = r->timeout[i];
412 nvlist_add_number_array(nvlr, "timeout", timeouts, PFTM_MAX);
413 nvlist_add_number(nvlr, "max_states", r->max_states);
414 nvlist_add_number(nvlr, "max_src_nodes", r->max_src_nodes);
415 nvlist_add_number(nvlr, "max_src_states", r->max_src_states);
416 nvlist_add_number(nvlr, "max_src_conn", r->max_src_conn);
417 nvlist_add_number(nvlr, "max_src_conn_rate.limit",
418 r->max_src_conn_rate.limit);
419 nvlist_add_number(nvlr, "max_src_conn_rate.seconds",
420 r->max_src_conn_rate.seconds);
421 nvlist_add_number(nvlr, "prob", r->prob);
422 nvlist_add_number(nvlr, "cuid", r->cuid);
423 nvlist_add_number(nvlr, "cpid", r->cpid);
425 nvlist_add_number(nvlr, "return_icmp", r->return_icmp);
426 nvlist_add_number(nvlr, "return_icmp6", r->return_icmp6);
428 nvlist_add_number(nvlr, "max_mss", r->max_mss);
429 nvlist_add_number(nvlr, "scrub_flags", r->scrub_flags);
431 pfctl_nv_add_uid(nvlr, "uid", &r->uid);
432 pfctl_nv_add_uid(nvlr, "gid", (const struct pf_rule_uid *)&r->gid);
434 nvlist_add_number(nvlr, "rule_flag", r->rule_flag);
435 nvlist_add_number(nvlr, "action", r->action);
436 nvlist_add_number(nvlr, "direction", r->direction);
437 nvlist_add_number(nvlr, "log", r->log);
438 nvlist_add_number(nvlr, "logif", r->logif);
439 nvlist_add_number(nvlr, "quick", r->quick);
440 nvlist_add_number(nvlr, "ifnot", r->ifnot);
441 nvlist_add_number(nvlr, "match_tag_not", r->match_tag_not);
442 nvlist_add_number(nvlr, "natpass", r->natpass);
444 nvlist_add_number(nvlr, "keep_state", r->keep_state);
445 nvlist_add_number(nvlr, "af", r->af);
446 nvlist_add_number(nvlr, "proto", r->proto);
447 nvlist_add_number(nvlr, "type", r->type);
448 nvlist_add_number(nvlr, "code", r->code);
449 nvlist_add_number(nvlr, "flags", r->flags);
450 nvlist_add_number(nvlr, "flagset", r->flagset);
451 nvlist_add_number(nvlr, "min_ttl", r->min_ttl);
452 nvlist_add_number(nvlr, "allow_opts", r->allow_opts);
453 nvlist_add_number(nvlr, "rt", r->rt);
454 nvlist_add_number(nvlr, "return_ttl", r->return_ttl);
455 nvlist_add_number(nvlr, "tos", r->tos);
456 nvlist_add_number(nvlr, "set_tos", r->set_tos);
457 nvlist_add_number(nvlr, "anchor_relative", r->anchor_relative);
458 nvlist_add_number(nvlr, "anchor_wildcard", r->anchor_wildcard);
460 nvlist_add_number(nvlr, "flush", r->flush);
462 nvlist_add_number(nvlr, "prio", r->prio);
463 set_prio[0] = r->set_prio[0];
464 set_prio[1] = r->set_prio[1];
465 nvlist_add_number_array(nvlr, "set_prio", set_prio, 2);
467 pfctl_nv_add_divert(nvlr, "divert", r);
469 nvlist_add_nvlist(nvl, "rule", nvlr);
471 /* Now do the call. */
472 nv.data = nvlist_pack(nvl, &nv.len);
475 ret = ioctl(dev, DIOCADDRULENV, &nv);
484 pfctl_get_rule(int dev, u_int32_t nr, u_int32_t ticket, const char *anchor,
485 u_int32_t ruleset, struct pfctl_rule *rule, char *anchor_call)
492 nvl = nvlist_create(0);
496 nvlist_add_number(nvl, "nr", nr);
497 nvlist_add_number(nvl, "ticket", ticket);
498 nvlist_add_string(nvl, "anchor", anchor);
499 nvlist_add_number(nvl, "ruleset", ruleset);
501 nvlpacked = nvlist_pack(nvl, &nv.len);
502 if (nvlpacked == NULL) {
506 nv.data = malloc(8182);
508 assert(nv.len <= nv.size);
509 memcpy(nv.data, nvlpacked, nv.len);
514 ret = ioctl(dev, DIOCGETRULENV, &nv);
520 nvl = nvlist_unpack(nv.data, nv.len, 0);
526 pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule);
529 strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"),