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 * 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 98 pfctl_close(struct pfctl_handle *h) 99 { 100 close(h->fd); 101 snl_free(&h->ss); 102 free(h); 103 } 104 105 static int 106 pfctl_do_ioctl(int dev, uint cmd, size_t size, nvlist_t **nvl) 107 { 108 struct pfioc_nv nv; 109 void *data; 110 size_t nvlen; 111 int ret; 112 113 data = nvlist_pack(*nvl, &nvlen); 114 if (nvlen > size) 115 size = nvlen; 116 117 retry: 118 nv.data = malloc(size); 119 if (nv.data == NULL) { 120 ret = ENOMEM; 121 goto out; 122 } 123 124 memcpy(nv.data, data, nvlen); 125 126 nv.len = nvlen; 127 nv.size = size; 128 129 ret = ioctl(dev, cmd, &nv); 130 if (ret == -1 && errno == ENOSPC) { 131 size *= 2; 132 free(nv.data); 133 goto retry; 134 } 135 136 nvlist_destroy(*nvl); 137 *nvl = NULL; 138 139 if (ret == 0) { 140 *nvl = nvlist_unpack(nv.data, nv.len, 0); 141 if (*nvl == NULL) { 142 ret = EIO; 143 goto out; 144 } 145 } else { 146 ret = errno; 147 } 148 149 out: 150 free(data); 151 free(nv.data); 152 153 return (ret); 154 } 155 156 static void 157 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems, 158 uint8_t *numbers, size_t *nelems) 159 { 160 const uint64_t *tmp; 161 size_t elems; 162 163 tmp = nvlist_get_number_array(nvl, name, &elems); 164 assert(elems <= maxelems); 165 166 for (size_t i = 0; i < elems; i++) 167 numbers[i] = tmp[i]; 168 169 if (nelems) 170 *nelems = elems; 171 } 172 173 static void 174 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems, 175 uint16_t *numbers, size_t *nelems) 176 { 177 const uint64_t *tmp; 178 size_t elems; 179 180 tmp = nvlist_get_number_array(nvl, name, &elems); 181 assert(elems <= maxelems); 182 183 for (size_t i = 0; i < elems; i++) 184 numbers[i] = tmp[i]; 185 186 if (nelems) 187 *nelems = elems; 188 } 189 190 static void 191 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems, 192 uint32_t *numbers, size_t *nelems) 193 { 194 const uint64_t *tmp; 195 size_t elems; 196 197 tmp = nvlist_get_number_array(nvl, name, &elems); 198 199 for (size_t i = 0; i < elems && i < maxelems; i++) 200 numbers[i] = tmp[i]; 201 202 if (nelems) 203 *nelems = elems; 204 } 205 206 static void 207 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems, 208 uint64_t *numbers, size_t *nelems) 209 { 210 const uint64_t *tmp; 211 size_t elems; 212 213 tmp = nvlist_get_number_array(nvl, name, &elems); 214 assert(elems <= maxelems); 215 216 for (size_t i = 0; i < elems; i++) 217 numbers[i] = tmp[i]; 218 219 if (nelems) 220 *nelems = elems; 221 } 222 223 int 224 pfctl_startstop(struct pfctl_handle *h, int start) 225 { 226 struct snl_errmsg_data e = {}; 227 struct snl_writer nw; 228 struct nlmsghdr *hdr; 229 uint32_t seq_id; 230 int family_id; 231 232 family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME); 233 if (family_id == 0) 234 return (ENOTSUP); 235 236 snl_init_writer(&h->ss, &nw); 237 hdr = snl_create_genl_msg_request(&nw, family_id, 238 start ? PFNL_CMD_START : PFNL_CMD_STOP); 239 240 hdr = snl_finalize_msg(&nw); 241 if (hdr == NULL) 242 return (ENOMEM); 243 seq_id = hdr->nlmsg_seq; 244 245 snl_send_message(&h->ss, hdr); 246 247 while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) { 248 } 249 250 return (e.error); 251 } 252 253 static void 254 _pfctl_get_status_counters(const nvlist_t *nvl, 255 struct pfctl_status_counters *counters) 256 { 257 const uint64_t *ids, *counts; 258 const char *const *names; 259 size_t id_len, counter_len, names_len; 260 261 ids = nvlist_get_number_array(nvl, "ids", &id_len); 262 counts = nvlist_get_number_array(nvl, "counters", &counter_len); 263 names = nvlist_get_string_array(nvl, "names", &names_len); 264 assert(id_len == counter_len); 265 assert(counter_len == names_len); 266 267 TAILQ_INIT(counters); 268 269 for (size_t i = 0; i < id_len; i++) { 270 struct pfctl_status_counter *c; 271 272 c = malloc(sizeof(*c)); 273 if (c == NULL) 274 continue; 275 276 c->id = ids[i]; 277 c->counter = counts[i]; 278 c->name = strdup(names[i]); 279 280 TAILQ_INSERT_TAIL(counters, c, entry); 281 } 282 } 283 284 struct pfctl_status * 285 pfctl_get_status(int dev) 286 { 287 struct pfctl_status *status; 288 nvlist_t *nvl; 289 size_t len; 290 const void *chksum; 291 292 status = calloc(1, sizeof(*status)); 293 if (status == NULL) 294 return (NULL); 295 296 nvl = nvlist_create(0); 297 298 if (pfctl_do_ioctl(dev, DIOCGETSTATUSNV, 4096, &nvl)) { 299 nvlist_destroy(nvl); 300 free(status); 301 return (NULL); 302 } 303 304 status->running = nvlist_get_bool(nvl, "running"); 305 status->since = nvlist_get_number(nvl, "since"); 306 status->debug = nvlist_get_number(nvl, "debug"); 307 status->hostid = ntohl(nvlist_get_number(nvl, "hostid")); 308 status->states = nvlist_get_number(nvl, "states"); 309 status->src_nodes = nvlist_get_number(nvl, "src_nodes"); 310 status->syncookies_active = nvlist_get_bool(nvl, "syncookies_active"); 311 status->reass = nvlist_get_number(nvl, "reass"); 312 313 strlcpy(status->ifname, nvlist_get_string(nvl, "ifname"), 314 IFNAMSIZ); 315 chksum = nvlist_get_binary(nvl, "chksum", &len); 316 assert(len == PF_MD5_DIGEST_LENGTH); 317 memcpy(status->pf_chksum, chksum, len); 318 319 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "counters"), 320 &status->counters); 321 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "lcounters"), 322 &status->lcounters); 323 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "fcounters"), 324 &status->fcounters); 325 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"), 326 &status->scounters); 327 328 pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 3, 329 (uint64_t *)status->pcounters, NULL); 330 pf_nvuint_64_array(nvl, "bcounters", 2 * 2, 331 (uint64_t *)status->bcounters, NULL); 332 333 nvlist_destroy(nvl); 334 335 return (status); 336 } 337 338 static uint64_t 339 _pfctl_status_counter(struct pfctl_status_counters *counters, uint64_t id) 340 { 341 struct pfctl_status_counter *c; 342 343 TAILQ_FOREACH(c, counters, entry) { 344 if (c->id == id) 345 return (c->counter); 346 } 347 348 return (0); 349 } 350 351 uint64_t 352 pfctl_status_counter(struct pfctl_status *status, int id) 353 { 354 return (_pfctl_status_counter(&status->counters, id)); 355 } 356 357 uint64_t 358 pfctl_status_lcounter(struct pfctl_status *status, int id) 359 { 360 return (_pfctl_status_counter(&status->lcounters, id)); 361 } 362 363 uint64_t 364 pfctl_status_fcounter(struct pfctl_status *status, int id) 365 { 366 return (_pfctl_status_counter(&status->fcounters, id)); 367 } 368 369 uint64_t 370 pfctl_status_scounter(struct pfctl_status *status, int id) 371 { 372 return (_pfctl_status_counter(&status->scounters, id)); 373 } 374 375 void 376 pfctl_free_status(struct pfctl_status *status) 377 { 378 struct pfctl_status_counter *c, *tmp; 379 380 if (status == NULL) 381 return; 382 383 TAILQ_FOREACH_SAFE(c, &status->counters, entry, tmp) { 384 free(c->name); 385 free(c); 386 } 387 TAILQ_FOREACH_SAFE(c, &status->lcounters, entry, tmp) { 388 free(c->name); 389 free(c); 390 } 391 TAILQ_FOREACH_SAFE(c, &status->fcounters, entry, tmp) { 392 free(c->name); 393 free(c); 394 } 395 TAILQ_FOREACH_SAFE(c, &status->scounters, entry, tmp) { 396 free(c->name); 397 free(c); 398 } 399 400 free(status); 401 } 402 403 static void 404 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name, 405 const struct pf_addr *addr) 406 { 407 nvlist_t *nvl = nvlist_create(0); 408 409 nvlist_add_binary(nvl, "addr", addr, sizeof(*addr)); 410 411 nvlist_add_nvlist(nvparent, name, nvl); 412 nvlist_destroy(nvl); 413 } 414 415 static void 416 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr) 417 { 418 size_t len; 419 const void *data; 420 421 data = nvlist_get_binary(nvl, "addr", &len); 422 assert(len == sizeof(struct pf_addr)); 423 memcpy(addr, data, len); 424 } 425 426 static void 427 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name, 428 const struct pf_addr_wrap *addr) 429 { 430 nvlist_t *nvl = nvlist_create(0); 431 432 nvlist_add_number(nvl, "type", addr->type); 433 nvlist_add_number(nvl, "iflags", addr->iflags); 434 if (addr->type == PF_ADDR_DYNIFTL) 435 nvlist_add_string(nvl, "ifname", addr->v.ifname); 436 if (addr->type == PF_ADDR_TABLE) 437 nvlist_add_string(nvl, "tblname", addr->v.tblname); 438 pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr); 439 pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask); 440 441 nvlist_add_nvlist(nvparent, name, nvl); 442 nvlist_destroy(nvl); 443 } 444 445 static void 446 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr) 447 { 448 bzero(addr, sizeof(*addr)); 449 450 addr->type = nvlist_get_number(nvl, "type"); 451 addr->iflags = nvlist_get_number(nvl, "iflags"); 452 if (addr->type == PF_ADDR_DYNIFTL) { 453 strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"), 454 IFNAMSIZ); 455 addr->p.dyncnt = nvlist_get_number(nvl, "dyncnt"); 456 } 457 if (addr->type == PF_ADDR_TABLE) { 458 strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"), 459 PF_TABLE_NAME_SIZE); 460 addr->p.tblcnt = nvlist_get_number(nvl, "tblcnt"); 461 } 462 463 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr); 464 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask); 465 } 466 467 static void 468 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name, 469 const struct pf_rule_addr *addr) 470 { 471 uint64_t ports[2]; 472 nvlist_t *nvl = nvlist_create(0); 473 474 pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr); 475 ports[0] = addr->port[0]; 476 ports[1] = addr->port[1]; 477 nvlist_add_number_array(nvl, "port", ports, 2); 478 nvlist_add_number(nvl, "neg", addr->neg); 479 nvlist_add_number(nvl, "port_op", addr->port_op); 480 481 nvlist_add_nvlist(nvparent, name, nvl); 482 nvlist_destroy(nvl); 483 } 484 485 static void 486 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr) 487 { 488 pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr); 489 490 pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL); 491 addr->neg = nvlist_get_number(nvl, "neg"); 492 addr->port_op = nvlist_get_number(nvl, "port_op"); 493 } 494 495 static void 496 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape) 497 { 498 mape->offset = nvlist_get_number(nvl, "offset"); 499 mape->psidlen = nvlist_get_number(nvl, "psidlen"); 500 mape->psid = nvlist_get_number(nvl, "psid"); 501 } 502 503 static void 504 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool) 505 { 506 size_t len; 507 const void *data; 508 509 data = nvlist_get_binary(nvl, "key", &len); 510 assert(len == sizeof(pool->key)); 511 memcpy(&pool->key, data, len); 512 513 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter); 514 515 pool->tblidx = nvlist_get_number(nvl, "tblidx"); 516 pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL); 517 pool->opts = nvlist_get_number(nvl, "opts"); 518 519 if (nvlist_exists_nvlist(nvl, "mape")) 520 pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape); 521 } 522 523 static void 524 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid) 525 { 526 pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL); 527 uid->op = nvlist_get_number(nvl, "op"); 528 } 529 530 static void 531 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule) 532 { 533 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr); 534 rule->divert.port = nvlist_get_number(nvl, "port"); 535 } 536 537 static void 538 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule) 539 { 540 const uint64_t *skip; 541 const char *const *labels; 542 size_t skipcount, labelcount; 543 544 rule->nr = nvlist_get_number(nvl, "nr"); 545 546 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src); 547 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst); 548 549 skip = nvlist_get_number_array(nvl, "skip", &skipcount); 550 assert(skip); 551 assert(skipcount == PF_SKIP_COUNT); 552 for (int i = 0; i < PF_SKIP_COUNT; i++) 553 rule->skip[i].nr = skip[i]; 554 555 labels = nvlist_get_string_array(nvl, "labels", &labelcount); 556 assert(labelcount <= PF_RULE_MAX_LABEL_COUNT); 557 for (size_t i = 0; i < labelcount; i++) 558 strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE); 559 rule->ridentifier = nvlist_get_number(nvl, "ridentifier"); 560 strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ); 561 strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE); 562 strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE); 563 strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"), 564 PF_TAG_NAME_SIZE); 565 strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"), 566 PF_TAG_NAME_SIZE); 567 568 strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"), 569 PF_TABLE_NAME_SIZE); 570 571 pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool); 572 573 rule->evaluations = nvlist_get_number(nvl, "evaluations"); 574 pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL); 575 pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL); 576 577 if (nvlist_exists_number(nvl, "timestamp")) { 578 rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp"); 579 } 580 581 rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint"); 582 583 rule->rtableid = nvlist_get_number(nvl, "rtableid"); 584 pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL); 585 rule->max_states = nvlist_get_number(nvl, "max_states"); 586 rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes"); 587 rule->max_src_states = nvlist_get_number(nvl, "max_src_states"); 588 rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn"); 589 rule->max_src_conn_rate.limit = 590 nvlist_get_number(nvl, "max_src_conn_rate.limit"); 591 rule->max_src_conn_rate.seconds = 592 nvlist_get_number(nvl, "max_src_conn_rate.seconds"); 593 rule->qid = nvlist_get_number(nvl, "qid"); 594 rule->pqid = nvlist_get_number(nvl, "pqid"); 595 rule->dnpipe = nvlist_get_number(nvl, "dnpipe"); 596 rule->dnrpipe = nvlist_get_number(nvl, "dnrpipe"); 597 rule->free_flags = nvlist_get_number(nvl, "dnflags"); 598 rule->prob = nvlist_get_number(nvl, "prob"); 599 rule->cuid = nvlist_get_number(nvl, "cuid"); 600 rule->cpid = nvlist_get_number(nvl, "cpid"); 601 602 rule->return_icmp = nvlist_get_number(nvl, "return_icmp"); 603 rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6"); 604 rule->max_mss = nvlist_get_number(nvl, "max_mss"); 605 rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags"); 606 607 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid); 608 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"), 609 (struct pf_rule_uid *)&rule->gid); 610 611 rule->rule_flag = nvlist_get_number(nvl, "rule_flag"); 612 rule->action = nvlist_get_number(nvl, "action"); 613 rule->direction = nvlist_get_number(nvl, "direction"); 614 rule->log = nvlist_get_number(nvl, "log"); 615 rule->logif = nvlist_get_number(nvl, "logif"); 616 rule->quick = nvlist_get_number(nvl, "quick"); 617 rule->ifnot = nvlist_get_number(nvl, "ifnot"); 618 rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not"); 619 rule->natpass = nvlist_get_number(nvl, "natpass"); 620 621 rule->keep_state = nvlist_get_number(nvl, "keep_state"); 622 rule->af = nvlist_get_number(nvl, "af"); 623 rule->proto = nvlist_get_number(nvl, "proto"); 624 rule->type = nvlist_get_number(nvl, "type"); 625 rule->code = nvlist_get_number(nvl, "code"); 626 rule->flags = nvlist_get_number(nvl, "flags"); 627 rule->flagset = nvlist_get_number(nvl, "flagset"); 628 rule->min_ttl = nvlist_get_number(nvl, "min_ttl"); 629 rule->allow_opts = nvlist_get_number(nvl, "allow_opts"); 630 rule->rt = nvlist_get_number(nvl, "rt"); 631 rule->return_ttl = nvlist_get_number(nvl, "return_ttl"); 632 rule->tos = nvlist_get_number(nvl, "tos"); 633 rule->set_tos = nvlist_get_number(nvl, "set_tos"); 634 rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative"); 635 rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard"); 636 637 rule->flush = nvlist_get_number(nvl, "flush"); 638 rule->prio = nvlist_get_number(nvl, "prio"); 639 pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL); 640 641 pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule); 642 643 rule->states_cur = nvlist_get_number(nvl, "states_cur"); 644 rule->states_tot = nvlist_get_number(nvl, "states_tot"); 645 rule->src_nodes = nvlist_get_number(nvl, "src_nodes"); 646 } 647 648 static void 649 pfctl_nveth_addr_to_eth_addr(const nvlist_t *nvl, struct pfctl_eth_addr *addr) 650 { 651 static const u_int8_t EMPTY_MAC[ETHER_ADDR_LEN] = { 0 }; 652 size_t len; 653 const void *data; 654 655 data = nvlist_get_binary(nvl, "addr", &len); 656 assert(len == sizeof(addr->addr)); 657 memcpy(addr->addr, data, sizeof(addr->addr)); 658 659 data = nvlist_get_binary(nvl, "mask", &len); 660 assert(len == sizeof(addr->mask)); 661 memcpy(addr->mask, data, sizeof(addr->mask)); 662 663 addr->neg = nvlist_get_bool(nvl, "neg"); 664 665 /* To make checks for 'is this address set?' easier. */ 666 addr->isset = memcmp(addr->addr, EMPTY_MAC, ETHER_ADDR_LEN) != 0; 667 } 668 669 static nvlist_t * 670 pfctl_eth_addr_to_nveth_addr(const struct pfctl_eth_addr *addr) 671 { 672 nvlist_t *nvl; 673 674 nvl = nvlist_create(0); 675 if (nvl == NULL) 676 return (NULL); 677 678 nvlist_add_bool(nvl, "neg", addr->neg); 679 nvlist_add_binary(nvl, "addr", &addr->addr, ETHER_ADDR_LEN); 680 nvlist_add_binary(nvl, "mask", &addr->mask, ETHER_ADDR_LEN); 681 682 return (nvl); 683 } 684 685 static void 686 pfctl_nveth_rule_to_eth_rule(const nvlist_t *nvl, struct pfctl_eth_rule *rule) 687 { 688 const char *const *labels; 689 size_t labelcount, i; 690 691 rule->nr = nvlist_get_number(nvl, "nr"); 692 rule->quick = nvlist_get_bool(nvl, "quick"); 693 strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ); 694 rule->ifnot = nvlist_get_bool(nvl, "ifnot"); 695 rule->direction = nvlist_get_number(nvl, "direction"); 696 rule->proto = nvlist_get_number(nvl, "proto"); 697 strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"), 698 PF_TAG_NAME_SIZE); 699 rule->match_tag = nvlist_get_number(nvl, "match_tag"); 700 rule->match_tag_not = nvlist_get_bool(nvl, "match_tag_not"); 701 702 labels = nvlist_get_string_array(nvl, "labels", &labelcount); 703 assert(labelcount <= PF_RULE_MAX_LABEL_COUNT); 704 for (i = 0; i < labelcount; i++) 705 strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE); 706 rule->ridentifier = nvlist_get_number(nvl, "ridentifier"); 707 708 pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "src"), 709 &rule->src); 710 pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "dst"), 711 &rule->dst); 712 713 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipsrc"), 714 &rule->ipsrc); 715 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipdst"), 716 &rule->ipdst); 717 718 rule->evaluations = nvlist_get_number(nvl, "evaluations"); 719 rule->packets[0] = nvlist_get_number(nvl, "packets-in"); 720 rule->packets[1] = nvlist_get_number(nvl, "packets-out"); 721 rule->bytes[0] = nvlist_get_number(nvl, "bytes-in"); 722 rule->bytes[1] = nvlist_get_number(nvl, "bytes-out"); 723 724 if (nvlist_exists_number(nvl, "timestamp")) { 725 rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp"); 726 } 727 728 strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE); 729 strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"), 730 PF_TAG_NAME_SIZE); 731 732 rule->dnpipe = nvlist_get_number(nvl, "dnpipe"); 733 rule->dnflags = nvlist_get_number(nvl, "dnflags"); 734 735 rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative"); 736 rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard"); 737 738 strlcpy(rule->bridge_to, nvlist_get_string(nvl, "bridge_to"), 739 IFNAMSIZ); 740 741 rule->action = nvlist_get_number(nvl, "action"); 742 } 743 744 int 745 pfctl_get_eth_rulesets_info(int dev, struct pfctl_eth_rulesets_info *ri, 746 const char *path) 747 { 748 nvlist_t *nvl; 749 int ret; 750 751 bzero(ri, sizeof(*ri)); 752 753 nvl = nvlist_create(0); 754 nvlist_add_string(nvl, "path", path); 755 756 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESETS, 256, &nvl)) != 0) 757 goto out; 758 759 ri->nr = nvlist_get_number(nvl, "nr"); 760 761 out: 762 nvlist_destroy(nvl); 763 return (ret); 764 } 765 766 int 767 pfctl_get_eth_ruleset(int dev, const char *path, int nr, 768 struct pfctl_eth_ruleset_info *ri) 769 { 770 nvlist_t *nvl; 771 int ret; 772 773 bzero(ri, sizeof(*ri)); 774 775 nvl = nvlist_create(0); 776 nvlist_add_string(nvl, "path", path); 777 nvlist_add_number(nvl, "nr", nr); 778 779 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESET, 1024, &nvl)) != 0) 780 goto out; 781 782 ri->nr = nvlist_get_number(nvl, "nr"); 783 strlcpy(ri->path, nvlist_get_string(nvl, "path"), MAXPATHLEN); 784 strlcpy(ri->name, nvlist_get_string(nvl, "name"), 785 PF_ANCHOR_NAME_SIZE); 786 787 out: 788 nvlist_destroy(nvl); 789 return (ret); 790 } 791 792 int 793 pfctl_get_eth_rules_info(int dev, struct pfctl_eth_rules_info *rules, 794 const char *path) 795 { 796 nvlist_t *nvl; 797 int ret; 798 799 bzero(rules, sizeof(*rules)); 800 801 nvl = nvlist_create(0); 802 nvlist_add_string(nvl, "anchor", path); 803 804 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULES, 1024, &nvl)) != 0) 805 goto out; 806 807 rules->nr = nvlist_get_number(nvl, "nr"); 808 rules->ticket = nvlist_get_number(nvl, "ticket"); 809 810 out: 811 nvlist_destroy(nvl); 812 return (ret); 813 } 814 815 int 816 pfctl_get_eth_rule(int dev, uint32_t nr, uint32_t ticket, 817 const char *path, struct pfctl_eth_rule *rule, bool clear, 818 char *anchor_call) 819 { 820 nvlist_t *nvl; 821 int ret; 822 823 nvl = nvlist_create(0); 824 825 nvlist_add_string(nvl, "anchor", path); 826 nvlist_add_number(nvl, "ticket", ticket); 827 nvlist_add_number(nvl, "nr", nr); 828 nvlist_add_bool(nvl, "clear", clear); 829 830 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULE, 4096, &nvl)) != 0) 831 goto out; 832 833 pfctl_nveth_rule_to_eth_rule(nvl, rule); 834 835 if (anchor_call) 836 strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"), 837 MAXPATHLEN); 838 839 out: 840 nvlist_destroy(nvl); 841 return (ret); 842 } 843 844 int 845 pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r, const char *anchor, 846 const char *anchor_call, uint32_t ticket) 847 { 848 struct pfioc_nv nv; 849 nvlist_t *nvl, *addr; 850 void *packed; 851 int error = 0; 852 size_t labelcount, size; 853 854 nvl = nvlist_create(0); 855 856 nvlist_add_number(nvl, "ticket", ticket); 857 nvlist_add_string(nvl, "anchor", anchor); 858 nvlist_add_string(nvl, "anchor_call", anchor_call); 859 860 nvlist_add_number(nvl, "nr", r->nr); 861 nvlist_add_bool(nvl, "quick", r->quick); 862 nvlist_add_string(nvl, "ifname", r->ifname); 863 nvlist_add_bool(nvl, "ifnot", r->ifnot); 864 nvlist_add_number(nvl, "direction", r->direction); 865 nvlist_add_number(nvl, "proto", r->proto); 866 nvlist_add_string(nvl, "match_tagname", r->match_tagname); 867 nvlist_add_bool(nvl, "match_tag_not", r->match_tag_not); 868 869 addr = pfctl_eth_addr_to_nveth_addr(&r->src); 870 if (addr == NULL) { 871 nvlist_destroy(nvl); 872 return (ENOMEM); 873 } 874 nvlist_add_nvlist(nvl, "src", addr); 875 nvlist_destroy(addr); 876 877 addr = pfctl_eth_addr_to_nveth_addr(&r->dst); 878 if (addr == NULL) { 879 nvlist_destroy(nvl); 880 return (ENOMEM); 881 } 882 nvlist_add_nvlist(nvl, "dst", addr); 883 nvlist_destroy(addr); 884 885 pfctl_nv_add_rule_addr(nvl, "ipsrc", &r->ipsrc); 886 pfctl_nv_add_rule_addr(nvl, "ipdst", &r->ipdst); 887 888 labelcount = 0; 889 while (labelcount < PF_RULE_MAX_LABEL_COUNT && 890 r->label[labelcount][0] != 0) { 891 nvlist_append_string_array(nvl, "labels", 892 r->label[labelcount]); 893 labelcount++; 894 } 895 nvlist_add_number(nvl, "ridentifier", r->ridentifier); 896 897 nvlist_add_string(nvl, "qname", r->qname); 898 nvlist_add_string(nvl, "tagname", r->tagname); 899 nvlist_add_number(nvl, "dnpipe", r->dnpipe); 900 nvlist_add_number(nvl, "dnflags", r->dnflags); 901 902 nvlist_add_string(nvl, "bridge_to", r->bridge_to); 903 904 nvlist_add_number(nvl, "action", r->action); 905 906 packed = nvlist_pack(nvl, &size); 907 if (packed == NULL) { 908 nvlist_destroy(nvl); 909 return (ENOMEM); 910 } 911 912 nv.len = size; 913 nv.size = size; 914 nv.data = packed; 915 916 if (ioctl(dev, DIOCADDETHRULE, &nv) != 0) 917 error = errno; 918 919 free(packed); 920 nvlist_destroy(nvl); 921 922 return (error); 923 } 924 925 static void 926 snl_add_msg_attr_addr_wrap(struct snl_writer *nw, uint32_t type, const struct pf_addr_wrap *addr) 927 { 928 int off; 929 930 off = snl_add_msg_attr_nested(nw, type); 931 932 snl_add_msg_attr_ip6(nw, PF_AT_ADDR, &addr->v.a.addr.v6); 933 snl_add_msg_attr_ip6(nw, PF_AT_MASK, &addr->v.a.mask.v6); 934 935 if (addr->type == PF_ADDR_DYNIFTL) 936 snl_add_msg_attr_string(nw, PF_AT_IFNAME, addr->v.ifname); 937 if (addr->type == PF_ADDR_TABLE) 938 snl_add_msg_attr_string(nw, PF_AT_TABLENAME, addr->v.tblname); 939 snl_add_msg_attr_u8(nw, PF_AT_TYPE, addr->type); 940 snl_add_msg_attr_u8(nw, PF_AT_IFLAGS, addr->iflags); 941 942 snl_end_attr_nested(nw, off); 943 } 944 945 static void 946 snl_add_msg_attr_rule_addr(struct snl_writer *nw, uint32_t type, const struct pf_rule_addr *addr) 947 { 948 int off; 949 950 off = snl_add_msg_attr_nested(nw, type); 951 952 snl_add_msg_attr_addr_wrap(nw, PF_RAT_ADDR, &addr->addr); 953 snl_add_msg_attr_u16(nw, PF_RAT_SRC_PORT, addr->port[0]); 954 snl_add_msg_attr_u16(nw, PF_RAT_DST_PORT, addr->port[1]); 955 snl_add_msg_attr_u8(nw, PF_RAT_NEG, addr->neg); 956 snl_add_msg_attr_u8(nw, PF_RAT_OP, addr->port_op); 957 958 snl_end_attr_nested(nw, off); 959 } 960 961 static void 962 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]) 963 { 964 int off, i = 0; 965 966 off = snl_add_msg_attr_nested(nw, type); 967 968 while (i < PF_RULE_MAX_LABEL_COUNT && 969 labels[i][0] != 0) { 970 snl_add_msg_attr_string(nw, PF_LT_LABEL, labels[i]); 971 i++; 972 } 973 974 snl_end_attr_nested(nw, off); 975 } 976 977 static void 978 snl_add_msg_attr_mape(struct snl_writer *nw, uint32_t type, const struct pf_mape_portset *me) 979 { 980 int off; 981 982 off = snl_add_msg_attr_nested(nw, type); 983 984 snl_add_msg_attr_u8(nw, PF_MET_OFFSET, me->offset); 985 snl_add_msg_attr_u8(nw, PF_MET_PSID_LEN, me->psidlen); 986 snl_add_msg_attr_u16(nw, PF_MET_PSID, me->psid); 987 988 snl_end_attr_nested(nw, off); 989 } 990 991 static void 992 snl_add_msg_attr_rpool(struct snl_writer *nw, uint32_t type, const struct pfctl_pool *pool) 993 { 994 int off; 995 996 off = snl_add_msg_attr_nested(nw, type); 997 998 snl_add_msg_attr(nw, PF_PT_KEY, sizeof(pool->key), &pool->key); 999 snl_add_msg_attr_ip6(nw, PF_PT_COUNTER, &pool->counter.v6); 1000 snl_add_msg_attr_u32(nw, PF_PT_TBLIDX, pool->tblidx); 1001 snl_add_msg_attr_u16(nw, PF_PT_PROXY_SRC_PORT, pool->proxy_port[0]); 1002 snl_add_msg_attr_u16(nw, PF_PT_PROXY_DST_PORT, pool->proxy_port[1]); 1003 snl_add_msg_attr_u8(nw, PF_PT_OPTS, pool->opts); 1004 snl_add_msg_attr_mape(nw, PF_PT_MAPE, &pool->mape); 1005 1006 snl_end_attr_nested(nw, off); 1007 } 1008 1009 static void 1010 snl_add_msg_attr_timeouts(struct snl_writer *nw, uint32_t type, const uint32_t *timeouts) 1011 { 1012 int off; 1013 1014 off = snl_add_msg_attr_nested(nw, type); 1015 1016 for (int i = 0; i < PFTM_MAX; i++) 1017 snl_add_msg_attr_u32(nw, PF_TT_TIMEOUT, timeouts[i]); 1018 1019 snl_end_attr_nested(nw, off); 1020 } 1021 1022 static void 1023 snl_add_msg_attr_uid(struct snl_writer *nw, uint32_t type, const struct pf_rule_uid *uid) 1024 { 1025 int off; 1026 1027 off = snl_add_msg_attr_nested(nw, type); 1028 1029 snl_add_msg_attr_u32(nw, PF_RUT_UID_LOW, uid->uid[0]); 1030 snl_add_msg_attr_u32(nw, PF_RUT_UID_HIGH, uid->uid[1]); 1031 snl_add_msg_attr_u8(nw, PF_RUT_OP, uid->op); 1032 1033 snl_end_attr_nested(nw, off); 1034 } 1035 1036 static void 1037 snl_add_msg_attr_pf_rule(struct snl_writer *nw, uint32_t type, const struct pfctl_rule *r) 1038 { 1039 int off; 1040 1041 off = snl_add_msg_attr_nested(nw, type); 1042 1043 snl_add_msg_attr_rule_addr(nw, PF_RT_SRC, &r->src); 1044 snl_add_msg_attr_rule_addr(nw, PF_RT_DST, &r->dst); 1045 snl_add_msg_attr_rule_labels(nw, PF_RT_LABELS, r->label); 1046 snl_add_msg_attr_u32(nw, PF_RT_RIDENTIFIER, r->ridentifier); 1047 snl_add_msg_attr_string(nw, PF_RT_IFNAME, r->ifname); 1048 snl_add_msg_attr_string(nw, PF_RT_QNAME, r->qname); 1049 snl_add_msg_attr_string(nw, PF_RT_PQNAME, r->pqname); 1050 snl_add_msg_attr_string(nw, PF_RT_TAGNAME, r->tagname); 1051 snl_add_msg_attr_string(nw, PF_RT_MATCH_TAGNAME, r->match_tagname); 1052 snl_add_msg_attr_string(nw, PF_RT_OVERLOAD_TBLNAME, r->overload_tblname); 1053 snl_add_msg_attr_rpool(nw, PF_RT_RPOOL, &r->rpool); 1054 snl_add_msg_attr_u32(nw, PF_RT_OS_FINGERPRINT, r->os_fingerprint); 1055 snl_add_msg_attr_u32(nw, PF_RT_RTABLEID, r->rtableid); 1056 snl_add_msg_attr_timeouts(nw, PF_RT_TIMEOUT, r->timeout); 1057 snl_add_msg_attr_u32(nw, PF_RT_MAX_STATES, r->max_states); 1058 snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_NODES, r->max_src_nodes); 1059 snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_STATES, r->max_src_states); 1060 snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_LIMIT, r->max_src_conn_rate.limit); 1061 snl_add_msg_attr_u32(nw, PF_RT_MAX_SRC_CONN_RATE_SECS, r->max_src_conn_rate.seconds); 1062 1063 snl_add_msg_attr_u16(nw, PF_RT_DNPIPE, r->dnpipe); 1064 snl_add_msg_attr_u16(nw, PF_RT_DNRPIPE, r->dnrpipe); 1065 snl_add_msg_attr_u32(nw, PF_RT_DNFLAGS, r->free_flags); 1066 1067 snl_add_msg_attr_u32(nw, PF_RT_NR, r->nr); 1068 snl_add_msg_attr_u32(nw, PF_RT_PROB, r->prob); 1069 snl_add_msg_attr_u32(nw, PF_RT_CUID, r->cuid); 1070 snl_add_msg_attr_u32(nw, PF_RT_CPID, r->cpid); 1071 1072 snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP, r->return_icmp); 1073 snl_add_msg_attr_u16(nw, PF_RT_RETURN_ICMP6, r->return_icmp6); 1074 snl_add_msg_attr_u16(nw, PF_RT_MAX_MSS, r->max_mss); 1075 snl_add_msg_attr_u16(nw, PF_RT_SCRUB_FLAGS, r->scrub_flags); 1076 1077 snl_add_msg_attr_uid(nw, PF_RT_UID, &r->uid); 1078 snl_add_msg_attr_uid(nw, PF_RT_GID, (const struct pf_rule_uid *)&r->gid); 1079 1080 snl_add_msg_attr_u32(nw, PF_RT_RULE_FLAG, r->rule_flag); 1081 snl_add_msg_attr_u8(nw, PF_RT_ACTION, r->action); 1082 snl_add_msg_attr_u8(nw, PF_RT_DIRECTION, r->direction); 1083 snl_add_msg_attr_u8(nw, PF_RT_LOG, r->log); 1084 snl_add_msg_attr_u8(nw, PF_RT_LOGIF, r->logif); 1085 snl_add_msg_attr_u8(nw, PF_RT_QUICK, r->quick); 1086 snl_add_msg_attr_u8(nw, PF_RT_IF_NOT, r->ifnot); 1087 snl_add_msg_attr_u8(nw, PF_RT_MATCH_TAG_NOT, r->match_tag_not); 1088 snl_add_msg_attr_u8(nw, PF_RT_NATPASS, r->natpass); 1089 snl_add_msg_attr_u8(nw, PF_RT_KEEP_STATE, r->keep_state); 1090 snl_add_msg_attr_u8(nw, PF_RT_AF, r->af); 1091 snl_add_msg_attr_u8(nw, PF_RT_PROTO, r->proto); 1092 snl_add_msg_attr_u8(nw, PF_RT_TYPE, r->type); 1093 snl_add_msg_attr_u8(nw, PF_RT_CODE, r->code); 1094 snl_add_msg_attr_u8(nw, PF_RT_FLAGS, r->flags); 1095 snl_add_msg_attr_u8(nw, PF_RT_FLAGSET, r->flagset); 1096 snl_add_msg_attr_u8(nw, PF_RT_MIN_TTL, r->min_ttl); 1097 snl_add_msg_attr_u8(nw, PF_RT_ALLOW_OPTS, r->allow_opts); 1098 snl_add_msg_attr_u8(nw, PF_RT_RT, r->rt); 1099 snl_add_msg_attr_u8(nw, PF_RT_RETURN_TTL, r->return_ttl); 1100 snl_add_msg_attr_u8(nw, PF_RT_TOS, r->tos); 1101 snl_add_msg_attr_u8(nw, PF_RT_SET_TOS, r->set_tos); 1102 1103 snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_RELATIVE, r->anchor_relative); 1104 snl_add_msg_attr_u8(nw, PF_RT_ANCHOR_WILDCARD, r->anchor_wildcard); 1105 snl_add_msg_attr_u8(nw, PF_RT_FLUSH, r->flush); 1106 snl_add_msg_attr_u8(nw, PF_RT_PRIO, r->prio); 1107 snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO, r->set_prio[0]); 1108 snl_add_msg_attr_u8(nw, PF_RT_SET_PRIO_REPLY, r->set_prio[1]); 1109 1110 snl_add_msg_attr_ip6(nw, PF_RT_DIVERT_ADDRESS, &r->divert.addr.v6); 1111 snl_add_msg_attr_u16(nw, PF_RT_DIVERT_PORT, r->divert.port); 1112 1113 snl_end_attr_nested(nw, off); 1114 } 1115 1116 int 1117 pfctl_add_rule(int dev __unused, const struct pfctl_rule *r, const char *anchor, 1118 const char *anchor_call, uint32_t ticket, uint32_t pool_ticket) 1119 { 1120 struct pfctl_handle *h; 1121 int ret; 1122 1123 h = pfctl_open(PF_DEVICE); 1124 if (h == NULL) 1125 return (ENODEV); 1126 1127 ret = pfctl_add_rule_h(h, r, anchor, anchor_call, ticket, pool_ticket); 1128 1129 pfctl_close(h); 1130 1131 return (ret); 1132 } 1133 1134 int 1135 pfctl_add_rule_h(struct pfctl_handle *h, const struct pfctl_rule *r, 1136 const char *anchor, const char *anchor_call, uint32_t ticket, 1137 uint32_t pool_ticket) 1138 { 1139 struct snl_writer nw; 1140 struct snl_errmsg_data e = {}; 1141 struct nlmsghdr *hdr; 1142 uint32_t seq_id; 1143 int family_id; 1144 1145 family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME); 1146 if (family_id == 0) 1147 return (ENOTSUP); 1148 1149 snl_init_writer(&h->ss, &nw); 1150 hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_ADDRULE); 1151 hdr->nlmsg_flags |= NLM_F_DUMP; 1152 snl_add_msg_attr_u32(&nw, PF_ART_TICKET, ticket); 1153 snl_add_msg_attr_u32(&nw, PF_ART_POOL_TICKET, pool_ticket); 1154 snl_add_msg_attr_string(&nw, PF_ART_ANCHOR, anchor); 1155 snl_add_msg_attr_string(&nw, PF_ART_ANCHOR_CALL, anchor_call); 1156 1157 snl_add_msg_attr_pf_rule(&nw, PF_ART_RULE, r); 1158 1159 if ((hdr = snl_finalize_msg(&nw)) == NULL) 1160 return (ENXIO); 1161 1162 seq_id = hdr->nlmsg_seq; 1163 1164 if (! snl_send_message(&h->ss, hdr)) 1165 return (ENXIO); 1166 1167 while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) { 1168 } 1169 1170 return (e.error); 1171 } 1172 1173 #define _IN(_field) offsetof(struct genlmsghdr, _field) 1174 #define _OUT(_field) offsetof(struct pfctl_rules_info, _field) 1175 static struct snl_attr_parser ap_getrules[] = { 1176 { .type = PF_GR_NR, .off = _OUT(nr), .cb = snl_attr_get_uint32 }, 1177 { .type = PF_GR_TICKET, .off = _OUT(ticket), .cb = snl_attr_get_uint32 }, 1178 }; 1179 static struct snl_field_parser fp_getrules[] = { 1180 }; 1181 #undef _IN 1182 #undef _OUT 1183 SNL_DECLARE_PARSER(getrules_parser, struct genlmsghdr, fp_getrules, ap_getrules); 1184 1185 int 1186 pfctl_get_rules_info(int dev __unused, struct pfctl_rules_info *rules, uint32_t ruleset, 1187 const char *path) 1188 { 1189 struct snl_state ss = {}; 1190 struct snl_errmsg_data e = {}; 1191 struct nlmsghdr *hdr; 1192 struct snl_writer nw; 1193 uint32_t seq_id; 1194 int family_id; 1195 1196 snl_init(&ss, NETLINK_GENERIC); 1197 family_id = snl_get_genl_family(&ss, PFNL_FAMILY_NAME); 1198 if (family_id == 0) 1199 return (ENOTSUP); 1200 1201 snl_init_writer(&ss, &nw); 1202 hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETRULES); 1203 hdr->nlmsg_flags |= NLM_F_DUMP; 1204 1205 snl_add_msg_attr_string(&nw, PF_GR_ANCHOR, path); 1206 snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset); 1207 1208 hdr = snl_finalize_msg(&nw); 1209 if (hdr == NULL) 1210 return (ENOMEM); 1211 1212 seq_id = hdr->nlmsg_seq; 1213 if (! snl_send_message(&ss, hdr)) 1214 return (ENXIO); 1215 1216 while ((hdr = snl_read_reply_multi(&ss, seq_id, &e)) != NULL) { 1217 if (! snl_parse_nlmsg(&ss, hdr, &getrules_parser, rules)) 1218 continue; 1219 } 1220 1221 return (e.error); 1222 } 1223 1224 int 1225 pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket, const char *anchor, 1226 uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call) 1227 { 1228 return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule, 1229 anchor_call, false)); 1230 } 1231 1232 #define _OUT(_field) offsetof(struct pf_addr_wrap, _field) 1233 static const struct snl_attr_parser ap_addr_wrap[] = { 1234 { .type = PF_AT_ADDR, .off = _OUT(v.a.addr), .cb = snl_attr_get_in6_addr }, 1235 { .type = PF_AT_MASK, .off = _OUT(v.a.mask), .cb = snl_attr_get_in6_addr }, 1236 { .type = PF_AT_IFNAME, .off = _OUT(v.ifname), .arg = (void *)IFNAMSIZ,.cb = snl_attr_copy_string }, 1237 { .type = PF_AT_TABLENAME, .off = _OUT(v.tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = snl_attr_copy_string }, 1238 { .type = PF_AT_TYPE, .off = _OUT(type), .cb = snl_attr_get_uint8 }, 1239 { .type = PF_AT_IFLAGS, .off = _OUT(iflags), .cb = snl_attr_get_uint8 }, 1240 { .type = PF_AT_TBLCNT, .off = _OUT(p.tblcnt), .cb = snl_attr_get_uint32 }, 1241 { .type = PF_AT_DYNCNT, .off = _OUT(p.dyncnt), .cb = snl_attr_get_uint32 }, 1242 }; 1243 SNL_DECLARE_ATTR_PARSER(addr_wrap_parser, ap_addr_wrap); 1244 #undef _OUT 1245 1246 #define _OUT(_field) offsetof(struct pf_rule_addr, _field) 1247 static struct snl_attr_parser ap_rule_addr[] = { 1248 { .type = PF_RAT_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = snl_attr_get_nested }, 1249 { .type = PF_RAT_SRC_PORT, .off = _OUT(port[0]), .cb = snl_attr_get_uint16 }, 1250 { .type = PF_RAT_DST_PORT, .off = _OUT(port[1]), .cb = snl_attr_get_uint16 }, 1251 { .type = PF_RAT_NEG, .off = _OUT(neg), .cb = snl_attr_get_uint8 }, 1252 { .type = PF_RAT_OP, .off = _OUT(port_op), .cb = snl_attr_get_uint8 }, 1253 }; 1254 #undef _OUT 1255 SNL_DECLARE_ATTR_PARSER(rule_addr_parser, ap_rule_addr); 1256 1257 struct snl_parsed_labels 1258 { 1259 char labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 1260 uint32_t i; 1261 }; 1262 1263 static bool 1264 snl_attr_get_pf_rule_labels(struct snl_state *ss, struct nlattr *nla, 1265 const void *arg __unused, void *target) 1266 { 1267 struct snl_parsed_labels *l = (struct snl_parsed_labels *)target; 1268 bool ret; 1269 1270 if (l->i >= PF_RULE_MAX_LABEL_COUNT) 1271 return (E2BIG); 1272 1273 ret = snl_attr_copy_string(ss, nla, (void *)PF_RULE_LABEL_SIZE, 1274 l->labels[l->i]); 1275 if (ret) 1276 l->i++; 1277 1278 return (ret); 1279 } 1280 1281 #define _OUT(_field) offsetof(struct nl_parsed_labels, _field) 1282 static const struct snl_attr_parser ap_labels[] = { 1283 { .type = PF_LT_LABEL, .off = 0, .cb = snl_attr_get_pf_rule_labels }, 1284 }; 1285 SNL_DECLARE_ATTR_PARSER(rule_labels_parser, ap_labels); 1286 #undef _OUT 1287 1288 static bool 1289 snl_attr_get_nested_pf_rule_labels(struct snl_state *ss, struct nlattr *nla, 1290 const void *arg __unused, void *target) 1291 { 1292 struct snl_parsed_labels parsed_labels = { }; 1293 bool error; 1294 1295 /* Assumes target points to the beginning of the structure */ 1296 error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &rule_labels_parser, &parsed_labels); 1297 if (! error) 1298 return (error); 1299 1300 memcpy(target, parsed_labels.labels, sizeof(parsed_labels.labels)); 1301 1302 return (true); 1303 } 1304 1305 #define _OUT(_field) offsetof(struct pf_mape_portset, _field) 1306 static const struct snl_attr_parser ap_mape_portset[] = { 1307 { .type = PF_MET_OFFSET, .off = _OUT(offset), .cb = snl_attr_get_uint8 }, 1308 { .type = PF_MET_PSID_LEN, .off = _OUT(psidlen), .cb = snl_attr_get_uint8 }, 1309 {. type = PF_MET_PSID, .off = _OUT(psid), .cb = snl_attr_get_uint16 }, 1310 }; 1311 SNL_DECLARE_ATTR_PARSER(mape_portset_parser, ap_mape_portset); 1312 #undef _OUT 1313 1314 #define _OUT(_field) offsetof(struct pfctl_pool, _field) 1315 static const struct snl_attr_parser ap_pool[] = { 1316 { .type = PF_PT_KEY, .off = _OUT(key), .arg = (void *)sizeof(struct pf_poolhashkey), .cb = snl_attr_get_bytes }, 1317 { .type = PF_PT_COUNTER, .off = _OUT(counter), .cb = snl_attr_get_in6_addr }, 1318 { .type = PF_PT_TBLIDX, .off = _OUT(tblidx), .cb = snl_attr_get_uint32 }, 1319 { .type = PF_PT_PROXY_SRC_PORT, .off = _OUT(proxy_port[0]), .cb = snl_attr_get_uint16 }, 1320 { .type = PF_PT_PROXY_DST_PORT, .off = _OUT(proxy_port[1]), .cb = snl_attr_get_uint16 }, 1321 { .type = PF_PT_OPTS, .off = _OUT(opts), .cb = snl_attr_get_uint8 }, 1322 { .type = PF_PT_MAPE, .off = _OUT(mape), .arg = &mape_portset_parser, .cb = snl_attr_get_nested }, 1323 }; 1324 SNL_DECLARE_ATTR_PARSER(pool_parser, ap_pool); 1325 #undef _OUT 1326 1327 struct nl_parsed_timeouts 1328 { 1329 uint32_t timeouts[PFTM_MAX]; 1330 uint32_t i; 1331 }; 1332 1333 static bool 1334 snl_attr_get_pf_timeout(struct snl_state *ss, struct nlattr *nla, 1335 const void *arg __unused, void *target) 1336 { 1337 struct nl_parsed_timeouts *t = (struct nl_parsed_timeouts *)target; 1338 bool ret; 1339 1340 if (t->i >= PFTM_MAX) 1341 return (E2BIG); 1342 1343 ret = snl_attr_get_uint32(ss, nla, NULL, &t->timeouts[t->i]); 1344 if (ret) 1345 t->i++; 1346 1347 return (ret); 1348 } 1349 1350 #define _OUT(_field) offsetof(struct nl_parsed_timeout, _field) 1351 static const struct snl_attr_parser ap_timeouts[] = { 1352 { .type = PF_TT_TIMEOUT, .off = 0, .cb = snl_attr_get_pf_timeout }, 1353 }; 1354 SNL_DECLARE_ATTR_PARSER(timeout_parser, ap_timeouts); 1355 #undef _OUT 1356 1357 static bool 1358 snl_attr_get_nested_timeouts(struct snl_state *ss, struct nlattr *nla, 1359 const void *arg __unused, void *target) 1360 { 1361 struct nl_parsed_timeouts parsed_timeouts = { }; 1362 bool error; 1363 1364 /* Assumes target points to the beginning of the structure */ 1365 error = snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), &timeout_parser, &parsed_timeouts); 1366 if (! error) 1367 return (error); 1368 1369 memcpy(target, parsed_timeouts.timeouts, sizeof(parsed_timeouts.timeouts)); 1370 1371 return (true); 1372 } 1373 1374 #define _OUT(_field) offsetof(struct pf_rule_uid, _field) 1375 static const struct snl_attr_parser ap_rule_uid[] = { 1376 { .type = PF_RUT_UID_LOW, .off = _OUT(uid[0]), .cb = snl_attr_get_uint32 }, 1377 { .type = PF_RUT_UID_HIGH, .off = _OUT(uid[1]), .cb = snl_attr_get_uint32 }, 1378 { .type = PF_RUT_OP, .off = _OUT(op), .cb = snl_attr_get_uint8 }, 1379 }; 1380 SNL_DECLARE_ATTR_PARSER(rule_uid_parser, ap_rule_uid); 1381 #undef _OUT 1382 1383 struct pfctl_nl_get_rule { 1384 struct pfctl_rule r; 1385 char anchor_call[MAXPATHLEN]; 1386 }; 1387 #define _OUT(_field) offsetof(struct pfctl_nl_get_rule, _field) 1388 static struct snl_attr_parser ap_getrule[] = { 1389 { .type = PF_RT_SRC, .off = _OUT(r.src), .arg = &rule_addr_parser,.cb = snl_attr_get_nested }, 1390 { .type = PF_RT_DST, .off = _OUT(r.dst), .arg = &rule_addr_parser,.cb = snl_attr_get_nested }, 1391 { .type = PF_RT_RIDENTIFIER, .off = _OUT(r.ridentifier), .cb = snl_attr_get_uint32 }, 1392 { .type = PF_RT_LABELS, .off = _OUT(r.label), .arg = &rule_labels_parser,.cb = snl_attr_get_nested_pf_rule_labels }, 1393 { .type = PF_RT_IFNAME, .off = _OUT(r.ifname), .arg = (void *)IFNAMSIZ, .cb = snl_attr_copy_string }, 1394 { .type = PF_RT_QNAME, .off = _OUT(r.qname), .arg = (void *)PF_QNAME_SIZE, .cb = snl_attr_copy_string }, 1395 { .type = PF_RT_PQNAME, .off = _OUT(r.pqname), .arg = (void *)PF_QNAME_SIZE, .cb = snl_attr_copy_string }, 1396 { .type = PF_RT_TAGNAME, .off = _OUT(r.tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = snl_attr_copy_string }, 1397 { .type = PF_RT_MATCH_TAGNAME, .off = _OUT(r.match_tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = snl_attr_copy_string }, 1398 { .type = PF_RT_OVERLOAD_TBLNAME, .off = _OUT(r.overload_tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = snl_attr_copy_string }, 1399 { .type = PF_RT_RPOOL, .off = _OUT(r.rpool), .arg = &pool_parser, .cb = snl_attr_get_nested }, 1400 { .type = PF_RT_OS_FINGERPRINT, .off = _OUT(r.os_fingerprint), .cb = snl_attr_get_uint32 }, 1401 { .type = PF_RT_RTABLEID, .off = _OUT(r.rtableid), .cb = snl_attr_get_uint32 }, 1402 { .type = PF_RT_TIMEOUT, .off = _OUT(r.timeout), .arg = &timeout_parser, .cb = snl_attr_get_nested_timeouts }, 1403 { .type = PF_RT_MAX_STATES, .off = _OUT(r.max_states), .cb = snl_attr_get_uint32 }, 1404 { .type = PF_RT_MAX_SRC_NODES, .off = _OUT(r.max_src_nodes), .cb = snl_attr_get_uint32 }, 1405 { .type = PF_RT_MAX_SRC_STATES, .off = _OUT(r.max_src_states), .cb = snl_attr_get_uint32 }, 1406 { .type = PF_RT_MAX_SRC_CONN_RATE_LIMIT, .off = _OUT(r.max_src_conn_rate.limit), .cb = snl_attr_get_uint32 }, 1407 { .type = PF_RT_MAX_SRC_CONN_RATE_SECS, .off = _OUT(r.max_src_conn_rate.seconds), .cb = snl_attr_get_uint32 }, 1408 { .type = PF_RT_DNPIPE, .off = _OUT(r.dnpipe), .cb = snl_attr_get_uint16 }, 1409 { .type = PF_RT_DNRPIPE, .off = _OUT(r.dnrpipe), .cb = snl_attr_get_uint16 }, 1410 { .type = PF_RT_DNFLAGS, .off = _OUT(r.free_flags), .cb = snl_attr_get_uint32 }, 1411 { .type = PF_RT_NR, .off = _OUT(r.nr), .cb = snl_attr_get_uint32 }, 1412 { .type = PF_RT_PROB, .off = _OUT(r.prob), .cb = snl_attr_get_uint32 }, 1413 { .type = PF_RT_CUID, .off = _OUT(r.cuid), .cb = snl_attr_get_uint32 }, 1414 {. type = PF_RT_CPID, .off = _OUT(r.cpid), .cb = snl_attr_get_uint32 }, 1415 { .type = PF_RT_RETURN_ICMP, .off = _OUT(r.return_icmp), .cb = snl_attr_get_uint16 }, 1416 { .type = PF_RT_RETURN_ICMP6, .off = _OUT(r.return_icmp6), .cb = snl_attr_get_uint16 }, 1417 { .type = PF_RT_MAX_MSS, .off = _OUT(r.max_mss), .cb = snl_attr_get_uint16 }, 1418 { .type = PF_RT_SCRUB_FLAGS, .off = _OUT(r.scrub_flags), .cb = snl_attr_get_uint16 }, 1419 { .type = PF_RT_UID, .off = _OUT(r.uid), .arg = &rule_uid_parser, .cb = snl_attr_get_nested }, 1420 { .type = PF_RT_GID, .off = _OUT(r.gid), .arg = &rule_uid_parser, .cb = snl_attr_get_nested }, 1421 { .type = PF_RT_RULE_FLAG, .off = _OUT(r.rule_flag), .cb = snl_attr_get_uint32 }, 1422 { .type = PF_RT_ACTION, .off = _OUT(r.action), .cb = snl_attr_get_uint8 }, 1423 { .type = PF_RT_DIRECTION, .off = _OUT(r.direction), .cb = snl_attr_get_uint8 }, 1424 { .type = PF_RT_LOG, .off = _OUT(r.log), .cb = snl_attr_get_uint8 }, 1425 { .type = PF_RT_LOGIF, .off = _OUT(r.logif), .cb = snl_attr_get_uint8 }, 1426 { .type = PF_RT_QUICK, .off = _OUT(r.quick), .cb = snl_attr_get_uint8 }, 1427 { .type = PF_RT_IF_NOT, .off = _OUT(r.ifnot), .cb = snl_attr_get_uint8 }, 1428 { .type = PF_RT_MATCH_TAG_NOT, .off = _OUT(r.match_tag_not), .cb = snl_attr_get_uint8 }, 1429 { .type = PF_RT_NATPASS, .off = _OUT(r.natpass), .cb = snl_attr_get_uint8 }, 1430 { .type = PF_RT_KEEP_STATE, .off = _OUT(r.keep_state), .cb = snl_attr_get_uint8 }, 1431 { .type = PF_RT_AF, .off = _OUT(r.af), .cb = snl_attr_get_uint8 }, 1432 { .type = PF_RT_PROTO, .off = _OUT(r.proto), .cb = snl_attr_get_uint8 }, 1433 { .type = PF_RT_TYPE, .off = _OUT(r.type), .cb = snl_attr_get_uint8 }, 1434 { .type = PF_RT_CODE, .off = _OUT(r.code), .cb = snl_attr_get_uint8 }, 1435 { .type = PF_RT_FLAGS, .off = _OUT(r.flags), .cb = snl_attr_get_uint8 }, 1436 { .type = PF_RT_FLAGSET, .off = _OUT(r.flagset), .cb = snl_attr_get_uint8 }, 1437 { .type = PF_RT_MIN_TTL, .off = _OUT(r.min_ttl), .cb = snl_attr_get_uint8 }, 1438 { .type = PF_RT_ALLOW_OPTS, .off = _OUT(r.allow_opts), .cb = snl_attr_get_uint8 }, 1439 { .type = PF_RT_RT, .off = _OUT(r.rt), .cb = snl_attr_get_uint8 }, 1440 { .type = PF_RT_RETURN_TTL, .off = _OUT(r.return_ttl), .cb = snl_attr_get_uint8 }, 1441 { .type = PF_RT_TOS, .off = _OUT(r.tos), .cb = snl_attr_get_uint8 }, 1442 { .type = PF_RT_SET_TOS, .off = _OUT(r.set_tos), .cb = snl_attr_get_uint8 }, 1443 { .type = PF_RT_ANCHOR_RELATIVE, .off = _OUT(r.anchor_relative), .cb = snl_attr_get_uint8 }, 1444 { .type = PF_RT_ANCHOR_WILDCARD, .off = _OUT(r.anchor_wildcard), .cb = snl_attr_get_uint8 }, 1445 { .type = PF_RT_FLUSH, .off = _OUT(r.flush), .cb = snl_attr_get_uint8 }, 1446 { .type = PF_RT_PRIO, .off = _OUT(r.prio), .cb = snl_attr_get_uint8 }, 1447 { .type = PF_RT_SET_PRIO, .off = _OUT(r.set_prio[0]), .cb = snl_attr_get_uint8 }, 1448 { .type = PF_RT_SET_PRIO_REPLY, .off = _OUT(r.set_prio[1]), .cb = snl_attr_get_uint8 }, 1449 { .type = PF_RT_DIVERT_ADDRESS, .off = _OUT(r.divert.addr), .cb = snl_attr_get_in6_addr }, 1450 { .type = PF_RT_DIVERT_PORT, .off = _OUT(r.divert.port), .cb = snl_attr_get_uint16 }, 1451 { .type = PF_RT_PACKETS_IN, .off = _OUT(r.packets[0]), .cb = snl_attr_get_uint64 }, 1452 { .type = PF_RT_PACKETS_OUT, .off = _OUT(r.packets[1]), .cb = snl_attr_get_uint64 }, 1453 { .type = PF_RT_BYTES_IN, .off = _OUT(r.bytes[0]), .cb = snl_attr_get_uint64 }, 1454 { .type = PF_RT_BYTES_OUT, .off = _OUT(r.bytes[1]), .cb = snl_attr_get_uint64 }, 1455 { .type = PF_RT_EVALUATIONS, .off = _OUT(r.evaluations), .cb = snl_attr_get_uint64 }, 1456 { .type = PF_RT_TIMESTAMP, .off = _OUT(r.last_active_timestamp), .cb = snl_attr_get_uint64 }, 1457 { .type = PF_RT_STATES_CUR, .off = _OUT(r.states_cur), .cb = snl_attr_get_uint64 }, 1458 { .type = PF_RT_STATES_TOTAL, .off = _OUT(r.states_tot), .cb = snl_attr_get_uint64 }, 1459 { .type = PF_RT_SRC_NODES, .off = _OUT(r.src_nodes), .cb = snl_attr_get_uint64 }, 1460 { .type = PF_RT_ANCHOR_CALL, .off = _OUT(anchor_call), .arg = (void*)MAXPATHLEN, .cb = snl_attr_copy_string }, 1461 }; 1462 static struct snl_field_parser fp_getrule[] = {}; 1463 #undef _OUT 1464 SNL_DECLARE_PARSER(getrule_parser, struct genlmsghdr, fp_getrule, ap_getrule); 1465 1466 int 1467 pfctl_get_clear_rule_h(struct pfctl_handle *h, uint32_t nr, uint32_t ticket, 1468 const char *anchor, uint32_t ruleset, struct pfctl_rule *rule, 1469 char *anchor_call, bool clear) 1470 { 1471 struct pfctl_nl_get_rule attrs = {}; 1472 struct snl_errmsg_data e = {}; 1473 struct nlmsghdr *hdr; 1474 struct snl_writer nw; 1475 uint32_t seq_id; 1476 int family_id; 1477 1478 family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME); 1479 if (family_id == 0) 1480 return (ENOTSUP); 1481 1482 snl_init_writer(&h->ss, &nw); 1483 hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETRULE); 1484 hdr->nlmsg_flags |= NLM_F_DUMP; 1485 1486 snl_add_msg_attr_string(&nw, PF_GR_ANCHOR, anchor); 1487 snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset); 1488 snl_add_msg_attr_u32(&nw, PF_GR_NR, nr); 1489 snl_add_msg_attr_u32(&nw, PF_GR_TICKET, ticket); 1490 snl_add_msg_attr_u8(&nw, PF_GR_CLEAR, clear); 1491 1492 hdr = snl_finalize_msg(&nw); 1493 if (hdr == NULL) 1494 return (ENOMEM); 1495 1496 seq_id = hdr->nlmsg_seq; 1497 if (! snl_send_message(&h->ss, hdr)) 1498 return (ENXIO); 1499 1500 while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) { 1501 if (! snl_parse_nlmsg(&h->ss, hdr, &getrule_parser, &attrs)) 1502 continue; 1503 } 1504 1505 memcpy(rule, &attrs.r, sizeof(attrs.r)); 1506 strlcpy(anchor_call, attrs.anchor_call, MAXPATHLEN); 1507 1508 return (e.error); 1509 } 1510 1511 int 1512 pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket, 1513 const char *anchor, uint32_t ruleset, struct pfctl_rule *rule, 1514 char *anchor_call, bool clear) 1515 { 1516 nvlist_t *nvl; 1517 int ret; 1518 1519 nvl = nvlist_create(0); 1520 if (nvl == 0) 1521 return (ENOMEM); 1522 1523 nvlist_add_number(nvl, "nr", nr); 1524 nvlist_add_number(nvl, "ticket", ticket); 1525 nvlist_add_string(nvl, "anchor", anchor); 1526 nvlist_add_number(nvl, "ruleset", ruleset); 1527 1528 if (clear) 1529 nvlist_add_bool(nvl, "clear_counter", true); 1530 1531 if ((ret = pfctl_do_ioctl(dev, DIOCGETRULENV, 8192, &nvl)) != 0) 1532 goto out; 1533 1534 pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule); 1535 1536 if (anchor_call) 1537 strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"), 1538 MAXPATHLEN); 1539 1540 out: 1541 nvlist_destroy(nvl); 1542 return (ret); 1543 } 1544 1545 int 1546 pfctl_set_keepcounters(int dev, bool keep) 1547 { 1548 struct pfioc_nv nv; 1549 nvlist_t *nvl; 1550 int ret; 1551 1552 nvl = nvlist_create(0); 1553 1554 nvlist_add_bool(nvl, "keep_counters", keep); 1555 1556 nv.data = nvlist_pack(nvl, &nv.len); 1557 nv.size = nv.len; 1558 1559 nvlist_destroy(nvl); 1560 1561 ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv); 1562 1563 free(nv.data); 1564 return (ret); 1565 } 1566 1567 struct pfctl_creator { 1568 uint32_t id; 1569 }; 1570 #define _IN(_field) offsetof(struct genlmsghdr, _field) 1571 #define _OUT(_field) offsetof(struct pfctl_creator, _field) 1572 static struct snl_attr_parser ap_creators[] = { 1573 { .type = PF_ST_CREATORID, .off = _OUT(id), .cb = snl_attr_get_uint32 }, 1574 }; 1575 static struct snl_field_parser fp_creators[] = { 1576 }; 1577 #undef _IN 1578 #undef _OUT 1579 SNL_DECLARE_PARSER(creator_parser, struct genlmsghdr, fp_creators, ap_creators); 1580 1581 static int 1582 pfctl_get_creators_nl(struct snl_state *ss, uint32_t *creators, size_t *len) 1583 { 1584 1585 int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME); 1586 size_t i = 0; 1587 1588 struct nlmsghdr *hdr; 1589 struct snl_writer nw; 1590 1591 if (family_id == 0) 1592 return (ENOTSUP); 1593 1594 snl_init_writer(ss, &nw); 1595 hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETCREATORS); 1596 hdr->nlmsg_flags |= NLM_F_DUMP; 1597 hdr = snl_finalize_msg(&nw); 1598 if (hdr == NULL) 1599 return (ENOMEM); 1600 uint32_t seq_id = hdr->nlmsg_seq; 1601 1602 snl_send_message(ss, hdr); 1603 1604 struct snl_errmsg_data e = {}; 1605 while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) { 1606 struct pfctl_creator c; 1607 bzero(&c, sizeof(c)); 1608 1609 if (!snl_parse_nlmsg(ss, hdr, &creator_parser, &c)) 1610 continue; 1611 1612 creators[i] = c.id; 1613 i++; 1614 if (i > *len) 1615 return (E2BIG); 1616 } 1617 1618 *len = i; 1619 1620 return (0); 1621 } 1622 1623 int 1624 pfctl_get_creatorids(struct pfctl_handle *h, uint32_t *creators, size_t *len) 1625 { 1626 int error; 1627 1628 error = pfctl_get_creators_nl(&h->ss, creators, len); 1629 1630 return (error); 1631 } 1632 1633 static inline bool 1634 snl_attr_get_pfaddr(struct snl_state *ss __unused, struct nlattr *nla, 1635 const void *arg __unused, void *target) 1636 { 1637 memcpy(target, NLA_DATA(nla), NLA_DATA_LEN(nla)); 1638 return (true); 1639 } 1640 1641 static inline bool 1642 snl_attr_store_ifname(struct snl_state *ss __unused, struct nlattr *nla, 1643 const void *arg __unused, void *target) 1644 { 1645 size_t maxlen = NLA_DATA_LEN(nla); 1646 1647 if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) { 1648 strlcpy(target, (char *)NLA_DATA(nla), maxlen); 1649 return (true); 1650 } 1651 return (false); 1652 } 1653 1654 #define _OUT(_field) offsetof(struct pfctl_state_peer, _field) 1655 static const struct snl_attr_parser nla_p_speer[] = { 1656 { .type = PF_STP_SEQLO, .off = _OUT(seqlo), .cb = snl_attr_get_uint32 }, 1657 { .type = PF_STP_SEQHI, .off = _OUT(seqhi), .cb = snl_attr_get_uint32 }, 1658 { .type = PF_STP_SEQDIFF, .off = _OUT(seqdiff), .cb = snl_attr_get_uint32 }, 1659 { .type = PF_STP_STATE, .off = _OUT(state), .cb = snl_attr_get_uint8 }, 1660 { .type = PF_STP_WSCALE, .off = _OUT(wscale), .cb = snl_attr_get_uint8 }, 1661 }; 1662 SNL_DECLARE_ATTR_PARSER(speer_parser, nla_p_speer); 1663 #undef _OUT 1664 1665 #define _OUT(_field) offsetof(struct pf_state_key_export, _field) 1666 static const struct snl_attr_parser nla_p_skey[] = { 1667 { .type = PF_STK_ADDR0, .off = _OUT(addr[0]), .cb = snl_attr_get_pfaddr }, 1668 { .type = PF_STK_ADDR1, .off = _OUT(addr[1]), .cb = snl_attr_get_pfaddr }, 1669 { .type = PF_STK_PORT0, .off = _OUT(port[0]), .cb = snl_attr_get_uint16 }, 1670 { .type = PF_STK_PORT1, .off = _OUT(port[1]), .cb = snl_attr_get_uint16 }, 1671 }; 1672 SNL_DECLARE_ATTR_PARSER(skey_parser, nla_p_skey); 1673 #undef _OUT 1674 1675 #define _IN(_field) offsetof(struct genlmsghdr, _field) 1676 #define _OUT(_field) offsetof(struct pfctl_state, _field) 1677 static struct snl_attr_parser ap_state[] = { 1678 { .type = PF_ST_ID, .off = _OUT(id), .cb = snl_attr_get_uint64 }, 1679 { .type = PF_ST_CREATORID, .off = _OUT(creatorid), .cb = snl_attr_get_uint32 }, 1680 { .type = PF_ST_IFNAME, .off = _OUT(ifname), .cb = snl_attr_store_ifname }, 1681 { .type = PF_ST_ORIG_IFNAME, .off = _OUT(orig_ifname), .cb = snl_attr_store_ifname }, 1682 { .type = PF_ST_KEY_WIRE, .off = _OUT(key[0]), .arg = &skey_parser, .cb = snl_attr_get_nested }, 1683 { .type = PF_ST_KEY_STACK, .off = _OUT(key[1]), .arg = &skey_parser, .cb = snl_attr_get_nested }, 1684 { .type = PF_ST_PEER_SRC, .off = _OUT(src), .arg = &speer_parser, .cb = snl_attr_get_nested }, 1685 { .type = PF_ST_PEER_DST, .off = _OUT(dst), .arg = &speer_parser, .cb = snl_attr_get_nested }, 1686 { .type = PF_ST_RT_ADDR, .off = _OUT(rt_addr), .cb = snl_attr_get_pfaddr }, 1687 { .type = PF_ST_RULE, .off = _OUT(rule), .cb = snl_attr_get_uint32 }, 1688 { .type = PF_ST_ANCHOR, .off = _OUT(anchor), .cb = snl_attr_get_uint32 }, 1689 { .type = PF_ST_NAT_RULE, .off = _OUT(nat_rule), .cb = snl_attr_get_uint32 }, 1690 { .type = PF_ST_CREATION, .off = _OUT(creation), .cb = snl_attr_get_uint32 }, 1691 { .type = PF_ST_EXPIRE, .off = _OUT(expire), .cb = snl_attr_get_uint32 }, 1692 { .type = PF_ST_PACKETS0, .off = _OUT(packets[0]), .cb = snl_attr_get_uint64 }, 1693 { .type = PF_ST_PACKETS1, .off = _OUT(packets[1]), .cb = snl_attr_get_uint64 }, 1694 { .type = PF_ST_BYTES0, .off = _OUT(bytes[0]), .cb = snl_attr_get_uint64 }, 1695 { .type = PF_ST_BYTES1, .off = _OUT(bytes[1]), .cb = snl_attr_get_uint64 }, 1696 { .type = PF_ST_AF, .off = _OUT(key[0].af), .cb = snl_attr_get_uint8 }, 1697 { .type = PF_ST_PROTO, .off = _OUT(key[0].proto), .cb = snl_attr_get_uint8 }, 1698 { .type = PF_ST_DIRECTION, .off = _OUT(direction), .cb = snl_attr_get_uint8 }, 1699 { .type = PF_ST_LOG, .off = _OUT(log), .cb = snl_attr_get_uint8 }, 1700 { .type = PF_ST_STATE_FLAGS, .off = _OUT(state_flags), .cb = snl_attr_get_uint16 }, 1701 { .type = PF_ST_SYNC_FLAGS, .off = _OUT(sync_flags), .cb = snl_attr_get_uint8 }, 1702 { .type = PF_ST_RTABLEID, .off = _OUT(rtableid), .cb = snl_attr_get_int32 }, 1703 { .type = PF_ST_MIN_TTL, .off = _OUT(min_ttl), .cb = snl_attr_get_uint8 }, 1704 { .type = PF_ST_MAX_MSS, .off = _OUT(max_mss), .cb = snl_attr_get_uint16 }, 1705 { .type = PF_ST_DNPIPE, .off = _OUT(dnpipe), .cb = snl_attr_get_uint16 }, 1706 { .type = PF_ST_DNRPIPE, .off = _OUT(dnrpipe), .cb = snl_attr_get_uint16 }, 1707 { .type = PF_ST_RT, .off = _OUT(rt), .cb = snl_attr_get_uint8 }, 1708 { .type = PF_ST_RT_IFNAME, .off = _OUT(rt_ifname), .cb = snl_attr_store_ifname }, 1709 }; 1710 static struct snl_field_parser fp_state[] = { 1711 }; 1712 #undef _IN 1713 #undef _OUT 1714 SNL_DECLARE_PARSER(state_parser, struct genlmsghdr, fp_state, ap_state); 1715 1716 static const struct snl_hdr_parser *all_parsers[] = { 1717 &state_parser, &skey_parser, &speer_parser, 1718 &creator_parser, &getrules_parser 1719 }; 1720 1721 static int 1722 pfctl_get_states_nl(struct pfctl_state_filter *filter, struct snl_state *ss, pfctl_get_state_fn f, void *arg) 1723 { 1724 SNL_VERIFY_PARSERS(all_parsers); 1725 int family_id = snl_get_genl_family(ss, PFNL_FAMILY_NAME); 1726 int ret; 1727 1728 struct nlmsghdr *hdr; 1729 struct snl_writer nw; 1730 1731 if (family_id == 0) 1732 return (ENOTSUP); 1733 1734 snl_init_writer(ss, &nw); 1735 hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETSTATES); 1736 hdr->nlmsg_flags |= NLM_F_DUMP; 1737 snl_add_msg_attr_string(&nw, PF_ST_IFNAME, filter->ifname); 1738 snl_add_msg_attr_u16(&nw, PF_ST_PROTO, filter->proto); 1739 snl_add_msg_attr_u8(&nw, PF_ST_AF, filter->af); 1740 snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_ADDR, &filter->addr.v6); 1741 snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_MASK, &filter->mask.v6); 1742 1743 hdr = snl_finalize_msg(&nw); 1744 if (hdr == NULL) 1745 return (ENOMEM); 1746 1747 uint32_t seq_id = hdr->nlmsg_seq; 1748 1749 snl_send_message(ss, hdr); 1750 1751 struct snl_errmsg_data e = {}; 1752 while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) { 1753 struct pfctl_state s; 1754 bzero(&s, sizeof(s)); 1755 if (!snl_parse_nlmsg(ss, hdr, &state_parser, &s)) 1756 continue; 1757 1758 s.key[1].af = s.key[0].af; 1759 s.key[1].proto = s.key[0].proto; 1760 1761 ret = f(&s, arg); 1762 if (ret != 0) 1763 return (ret); 1764 } 1765 1766 return (0); 1767 } 1768 1769 int 1770 pfctl_get_states_iter(pfctl_get_state_fn f, void *arg) 1771 { 1772 struct pfctl_state_filter filter = {}; 1773 return (pfctl_get_filtered_states_iter(&filter, f, arg)); 1774 } 1775 1776 int 1777 pfctl_get_filtered_states_iter(struct pfctl_state_filter *filter, pfctl_get_state_fn f, void *arg) 1778 { 1779 struct snl_state ss = {}; 1780 int error; 1781 1782 snl_init(&ss, NETLINK_GENERIC); 1783 error = pfctl_get_states_nl(filter, &ss, f, arg); 1784 snl_free(&ss); 1785 1786 return (error); 1787 } 1788 1789 static int 1790 pfctl_append_states(struct pfctl_state *s, void *arg) 1791 { 1792 struct pfctl_state *new; 1793 struct pfctl_states *states = (struct pfctl_states *)arg; 1794 1795 new = malloc(sizeof(*s)); 1796 if (new == NULL) 1797 return (ENOMEM); 1798 1799 memcpy(new, s, sizeof(*s)); 1800 1801 TAILQ_INSERT_TAIL(&states->states, new, entry); 1802 1803 return (0); 1804 } 1805 1806 int 1807 pfctl_get_states(int dev __unused, struct pfctl_states *states) 1808 { 1809 int ret; 1810 1811 bzero(states, sizeof(*states)); 1812 TAILQ_INIT(&states->states); 1813 1814 ret = pfctl_get_states_iter(pfctl_append_states, states); 1815 if (ret != 0) { 1816 pfctl_free_states(states); 1817 return (ret); 1818 } 1819 1820 return (0); 1821 } 1822 1823 void 1824 pfctl_free_states(struct pfctl_states *states) 1825 { 1826 struct pfctl_state *s, *tmp; 1827 1828 TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) { 1829 free(s); 1830 } 1831 1832 bzero(states, sizeof(*states)); 1833 } 1834 1835 struct pfctl_nl_clear_states { 1836 uint32_t killed; 1837 }; 1838 #define _OUT(_field) offsetof(struct pfctl_nl_clear_states, _field) 1839 static struct snl_attr_parser ap_clear_states[] = { 1840 { .type = PF_CS_KILLED, .off = _OUT(killed), .cb = snl_attr_get_uint32 }, 1841 }; 1842 static struct snl_field_parser fp_clear_states[] = {}; 1843 #undef _OUT 1844 SNL_DECLARE_PARSER(clear_states_parser, struct genlmsghdr, fp_clear_states, ap_clear_states); 1845 1846 static int 1847 _pfctl_clear_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill, 1848 unsigned int *killed, int cmd) 1849 { 1850 struct snl_writer nw; 1851 struct snl_errmsg_data e = {}; 1852 struct pfctl_nl_clear_states attrs = {}; 1853 struct nlmsghdr *hdr; 1854 uint32_t seq_id; 1855 int family_id; 1856 1857 family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME); 1858 if (family_id == 0) 1859 return (ENOTSUP); 1860 1861 snl_init_writer(&h->ss, &nw); 1862 hdr = snl_create_genl_msg_request(&nw, family_id, cmd); 1863 hdr->nlmsg_flags |= NLM_F_DUMP; 1864 1865 snl_add_msg_attr_u64(&nw, PF_CS_CMP_ID, kill->cmp.id); 1866 snl_add_msg_attr_u32(&nw, PF_CS_CMP_CREATORID, htonl(kill->cmp.creatorid)); 1867 snl_add_msg_attr_u8(&nw, PF_CS_CMP_DIR, kill->cmp.direction); 1868 snl_add_msg_attr_u8(&nw, PF_CS_AF, kill->af); 1869 snl_add_msg_attr_u8(&nw, PF_CS_PROTO, kill->proto); 1870 snl_add_msg_attr_rule_addr(&nw, PF_CS_SRC, &kill->src); 1871 snl_add_msg_attr_rule_addr(&nw, PF_CS_DST, &kill->dst); 1872 snl_add_msg_attr_rule_addr(&nw, PF_CS_RT_ADDR, &kill->rt_addr); 1873 snl_add_msg_attr_string(&nw, PF_CS_IFNAME, kill->ifname); 1874 snl_add_msg_attr_string(&nw, PF_CS_LABEL, kill->label); 1875 snl_add_msg_attr_bool(&nw, PF_CS_KILL_MATCH, kill->kill_match); 1876 snl_add_msg_attr_bool(&nw, PF_CS_NAT, kill->nat); 1877 1878 if ((hdr = snl_finalize_msg(&nw)) == NULL) 1879 return (ENXIO); 1880 1881 seq_id = hdr->nlmsg_seq; 1882 1883 if (! snl_send_message(&h->ss, hdr)) 1884 return (ENXIO); 1885 1886 while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) { 1887 if (! snl_parse_nlmsg(&h->ss, hdr, &clear_states_parser, &attrs)) 1888 continue; 1889 } 1890 1891 if (killed) 1892 *killed = attrs.killed; 1893 1894 return (e.error); 1895 } 1896 1897 int 1898 pfctl_clear_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill, 1899 unsigned int *killed) 1900 { 1901 return(_pfctl_clear_states_h(h, kill, killed, PFNL_CMD_CLRSTATES)); 1902 } 1903 1904 int 1905 pfctl_kill_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill, 1906 unsigned int *killed) 1907 { 1908 return(_pfctl_clear_states_h(h, kill, killed, PFNL_CMD_KILLSTATES)); 1909 } 1910 1911 static int 1912 _pfctl_clear_states(int dev __unused, const struct pfctl_kill *kill, 1913 unsigned int *killed, uint64_t cmd) 1914 { 1915 struct pfctl_handle *h; 1916 int ret; 1917 1918 h = pfctl_open(PF_DEVICE); 1919 if (h == NULL) 1920 return (ENODEV); 1921 1922 ret = _pfctl_clear_states_h(h, kill, killed, cmd); 1923 pfctl_close(h); 1924 1925 return (ret); 1926 } 1927 1928 int 1929 pfctl_clear_states(int dev __unused, const struct pfctl_kill *kill, 1930 unsigned int *killed) 1931 { 1932 return (_pfctl_clear_states(dev, kill, killed, PFNL_CMD_CLRSTATES)); 1933 } 1934 1935 int 1936 pfctl_kill_states(int dev __unused, const struct pfctl_kill *kill, unsigned int *killed) 1937 { 1938 return (_pfctl_clear_states(dev, kill, killed, PFNL_CMD_KILLSTATES)); 1939 } 1940 1941 int 1942 pfctl_clear_rules(int dev, const char *anchorname) 1943 { 1944 struct pfioc_trans trans; 1945 struct pfioc_trans_e transe[2]; 1946 int ret; 1947 1948 bzero(&trans, sizeof(trans)); 1949 bzero(&transe, sizeof(transe)); 1950 1951 transe[0].rs_num = PF_RULESET_SCRUB; 1952 if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) 1953 >= sizeof(transe[0].anchor)) 1954 return (E2BIG); 1955 1956 transe[1].rs_num = PF_RULESET_FILTER; 1957 if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) 1958 >= sizeof(transe[1].anchor)) 1959 return (E2BIG); 1960 1961 trans.size = 2; 1962 trans.esize = sizeof(transe[0]); 1963 trans.array = transe; 1964 1965 ret = ioctl(dev, DIOCXBEGIN, &trans); 1966 if (ret != 0) 1967 return (ret); 1968 return ioctl(dev, DIOCXCOMMIT, &trans); 1969 } 1970 1971 int 1972 pfctl_clear_nat(int dev, const char *anchorname) 1973 { 1974 struct pfioc_trans trans; 1975 struct pfioc_trans_e transe[3]; 1976 int ret; 1977 1978 bzero(&trans, sizeof(trans)); 1979 bzero(&transe, sizeof(transe)); 1980 1981 transe[0].rs_num = PF_RULESET_NAT; 1982 if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) 1983 >= sizeof(transe[0].anchor)) 1984 return (E2BIG); 1985 1986 transe[1].rs_num = PF_RULESET_BINAT; 1987 if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) 1988 >= sizeof(transe[0].anchor)) 1989 return (E2BIG); 1990 1991 transe[2].rs_num = PF_RULESET_RDR; 1992 if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor)) 1993 >= sizeof(transe[2].anchor)) 1994 return (E2BIG); 1995 1996 trans.size = 3; 1997 trans.esize = sizeof(transe[0]); 1998 trans.array = transe; 1999 2000 ret = ioctl(dev, DIOCXBEGIN, &trans); 2001 if (ret != 0) 2002 return (ret); 2003 return ioctl(dev, DIOCXCOMMIT, &trans); 2004 } 2005 int 2006 pfctl_clear_eth_rules(int dev, const char *anchorname) 2007 { 2008 struct pfioc_trans trans; 2009 struct pfioc_trans_e transe; 2010 int ret; 2011 2012 bzero(&trans, sizeof(trans)); 2013 bzero(&transe, sizeof(transe)); 2014 2015 transe.rs_num = PF_RULESET_ETH; 2016 if (strlcpy(transe.anchor, anchorname, sizeof(transe.anchor)) 2017 >= sizeof(transe.anchor)) 2018 return (E2BIG); 2019 2020 trans.size = 1; 2021 trans.esize = sizeof(transe); 2022 trans.array = &transe; 2023 2024 ret = ioctl(dev, DIOCXBEGIN, &trans); 2025 if (ret != 0) 2026 return (ret); 2027 return ioctl(dev, DIOCXCOMMIT, &trans); 2028 } 2029 2030 static int 2031 pfctl_get_limit(int dev, const int index, uint *limit) 2032 { 2033 struct pfioc_limit pl; 2034 2035 bzero(&pl, sizeof(pl)); 2036 pl.index = index; 2037 2038 if (ioctl(dev, DIOCGETLIMIT, &pl) == -1) 2039 return (errno); 2040 2041 *limit = pl.limit; 2042 2043 return (0); 2044 } 2045 2046 int 2047 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s) 2048 { 2049 struct pfioc_nv nv; 2050 nvlist_t *nvl; 2051 int ret; 2052 uint state_limit; 2053 uint64_t lim, hi, lo; 2054 2055 ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit); 2056 if (ret != 0) 2057 return (ret); 2058 2059 lim = state_limit; 2060 hi = lim * s->highwater / 100; 2061 lo = lim * s->lowwater / 100; 2062 2063 if (lo == hi) 2064 hi++; 2065 2066 nvl = nvlist_create(0); 2067 2068 nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER); 2069 nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE); 2070 nvlist_add_number(nvl, "highwater", hi); 2071 nvlist_add_number(nvl, "lowwater", lo); 2072 2073 nv.data = nvlist_pack(nvl, &nv.len); 2074 nv.size = nv.len; 2075 nvlist_destroy(nvl); 2076 nvl = NULL; 2077 2078 ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv); 2079 2080 free(nv.data); 2081 return (ret); 2082 } 2083 2084 int 2085 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s) 2086 { 2087 nvlist_t *nvl; 2088 int ret; 2089 uint state_limit; 2090 bool enabled, adaptive; 2091 2092 ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit); 2093 if (ret != 0) 2094 return (ret); 2095 2096 bzero(s, sizeof(*s)); 2097 2098 nvl = nvlist_create(0); 2099 2100 if ((ret = pfctl_do_ioctl(dev, DIOCGETSYNCOOKIES, 256, &nvl)) != 0) { 2101 ret = errno; 2102 goto out; 2103 } 2104 2105 enabled = nvlist_get_bool(nvl, "enabled"); 2106 adaptive = nvlist_get_bool(nvl, "adaptive"); 2107 2108 if (enabled) { 2109 if (adaptive) 2110 s->mode = PFCTL_SYNCOOKIES_ADAPTIVE; 2111 else 2112 s->mode = PFCTL_SYNCOOKIES_ALWAYS; 2113 } else { 2114 s->mode = PFCTL_SYNCOOKIES_NEVER; 2115 } 2116 2117 s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit; 2118 s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit; 2119 s->halfopen_states = nvlist_get_number(nvl, "halfopen_states"); 2120 2121 out: 2122 nvlist_destroy(nvl); 2123 return (ret); 2124 } 2125 2126 int 2127 pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 2128 *addr, int size, int *nadd, int flags) 2129 { 2130 struct pfioc_table io; 2131 2132 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 2133 return (EINVAL); 2134 } 2135 bzero(&io, sizeof io); 2136 io.pfrio_flags = flags; 2137 io.pfrio_table = *tbl; 2138 io.pfrio_buffer = addr; 2139 io.pfrio_esize = sizeof(*addr); 2140 io.pfrio_size = size; 2141 2142 if (ioctl(dev, DIOCRADDADDRS, &io)) 2143 return (errno); 2144 if (nadd != NULL) 2145 *nadd = io.pfrio_nadd; 2146 return (0); 2147 } 2148 2149 int 2150 pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 2151 *addr, int size, int *ndel, int flags) 2152 { 2153 struct pfioc_table io; 2154 2155 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 2156 return (EINVAL); 2157 } 2158 bzero(&io, sizeof io); 2159 io.pfrio_flags = flags; 2160 io.pfrio_table = *tbl; 2161 io.pfrio_buffer = addr; 2162 io.pfrio_esize = sizeof(*addr); 2163 io.pfrio_size = size; 2164 2165 if (ioctl(dev, DIOCRDELADDRS, &io)) 2166 return (errno); 2167 if (ndel != NULL) 2168 *ndel = io.pfrio_ndel; 2169 return (0); 2170 } 2171 2172 int 2173 pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 2174 *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags) 2175 { 2176 struct pfioc_table io; 2177 2178 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 2179 return (EINVAL); 2180 } 2181 bzero(&io, sizeof io); 2182 io.pfrio_flags = flags; 2183 io.pfrio_table = *tbl; 2184 io.pfrio_buffer = addr; 2185 io.pfrio_esize = sizeof(*addr); 2186 io.pfrio_size = size; 2187 io.pfrio_size2 = (size2 != NULL) ? *size2 : 0; 2188 if (ioctl(dev, DIOCRSETADDRS, &io)) 2189 return (-1); 2190 if (nadd != NULL) 2191 *nadd = io.pfrio_nadd; 2192 if (ndel != NULL) 2193 *ndel = io.pfrio_ndel; 2194 if (nchange != NULL) 2195 *nchange = io.pfrio_nchange; 2196 if (size2 != NULL) 2197 *size2 = io.pfrio_size2; 2198 return (0); 2199 } 2200 2201 int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr, 2202 int *size, int flags) 2203 { 2204 struct pfioc_table io; 2205 2206 if (tbl == NULL || size == NULL || *size < 0 || 2207 (*size && addr == NULL)) { 2208 return (EINVAL); 2209 } 2210 bzero(&io, sizeof io); 2211 io.pfrio_flags = flags; 2212 io.pfrio_table = *tbl; 2213 io.pfrio_buffer = addr; 2214 io.pfrio_esize = sizeof(*addr); 2215 io.pfrio_size = *size; 2216 if (ioctl(dev, DIOCRGETADDRS, &io)) 2217 return (-1); 2218 *size = io.pfrio_size; 2219 return (0); 2220 } 2221 2222 int 2223 pfctl_set_statusif(struct pfctl_handle *h, const char *ifname) 2224 { 2225 struct snl_writer nw; 2226 struct snl_errmsg_data e = {}; 2227 struct nlmsghdr *hdr; 2228 uint32_t seq_id; 2229 int family_id; 2230 2231 family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME); 2232 if (family_id == 0) 2233 return (ENOTSUP); 2234 2235 snl_init_writer(&h->ss, &nw); 2236 hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_SET_STATUSIF); 2237 2238 snl_add_msg_attr_string(&nw, PF_SS_IFNAME, ifname); 2239 2240 if ((hdr = snl_finalize_msg(&nw)) == NULL) 2241 return (ENXIO); 2242 2243 seq_id = hdr->nlmsg_seq; 2244 2245 if (! snl_send_message(&h->ss, hdr)) 2246 return (ENXIO); 2247 2248 while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) { 2249 } 2250 2251 return (e.error); 2252 } 2253