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