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 <assert.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <stdlib.h> 47 #include <string.h> 48 49 #include "libpfctl.h" 50 51 const char* PFCTL_SYNCOOKIES_MODE_NAMES[] = { 52 "never", 53 "always", 54 "adaptive" 55 }; 56 57 static int _pfctl_clear_states(int , const struct pfctl_kill *, 58 unsigned int *, uint64_t); 59 60 static int 61 pfctl_do_ioctl(int dev, uint cmd, size_t size, nvlist_t **nvl) 62 { 63 struct pfioc_nv nv; 64 void *data; 65 size_t nvlen; 66 int ret; 67 68 data = nvlist_pack(*nvl, &nvlen); 69 if (nvlen > size) 70 size = nvlen; 71 72 retry: 73 nv.data = malloc(size); 74 memcpy(nv.data, data, nvlen); 75 free(data); 76 77 nv.len = nvlen; 78 nv.size = size; 79 80 ret = ioctl(dev, cmd, &nv); 81 if (ret == -1 && errno == ENOSPC) { 82 size *= 2; 83 free(nv.data); 84 goto retry; 85 } 86 87 nvlist_destroy(*nvl); 88 *nvl = NULL; 89 90 if (ret == 0) { 91 *nvl = nvlist_unpack(nv.data, nv.len, 0); 92 if (*nvl == NULL) { 93 free(nv.data); 94 return (EIO); 95 } 96 } else { 97 ret = errno; 98 } 99 100 free(nv.data); 101 102 return (ret); 103 } 104 105 static void 106 pf_nvuint_8_array(const nvlist_t *nvl, const char *name, size_t maxelems, 107 uint8_t *numbers, size_t *nelems) 108 { 109 const uint64_t *tmp; 110 size_t elems; 111 112 tmp = nvlist_get_number_array(nvl, name, &elems); 113 assert(elems <= maxelems); 114 115 for (size_t i = 0; i < elems; i++) 116 numbers[i] = tmp[i]; 117 118 if (nelems) 119 *nelems = elems; 120 } 121 122 static void 123 pf_nvuint_16_array(const nvlist_t *nvl, const char *name, size_t maxelems, 124 uint16_t *numbers, size_t *nelems) 125 { 126 const uint64_t *tmp; 127 size_t elems; 128 129 tmp = nvlist_get_number_array(nvl, name, &elems); 130 assert(elems <= maxelems); 131 132 for (size_t i = 0; i < elems; i++) 133 numbers[i] = tmp[i]; 134 135 if (nelems) 136 *nelems = elems; 137 } 138 139 static void 140 pf_nvuint_32_array(const nvlist_t *nvl, const char *name, size_t maxelems, 141 uint32_t *numbers, size_t *nelems) 142 { 143 const uint64_t *tmp; 144 size_t elems; 145 146 tmp = nvlist_get_number_array(nvl, name, &elems); 147 assert(elems <= maxelems); 148 149 for (size_t i = 0; i < elems; i++) 150 numbers[i] = tmp[i]; 151 152 if (nelems) 153 *nelems = elems; 154 } 155 156 static void 157 pf_nvuint_64_array(const nvlist_t *nvl, const char *name, size_t maxelems, 158 uint64_t *numbers, size_t *nelems) 159 { 160 const uint64_t *tmp; 161 size_t elems; 162 163 tmp = nvlist_get_number_array(nvl, name, &elems); 164 assert(elems <= maxelems); 165 166 for (size_t i = 0; i < elems; i++) 167 numbers[i] = tmp[i]; 168 169 if (nelems) 170 *nelems = elems; 171 } 172 173 static void 174 _pfctl_get_status_counters(const nvlist_t *nvl, 175 struct pfctl_status_counters *counters) 176 { 177 const uint64_t *ids, *counts; 178 const char *const *names; 179 size_t id_len, counter_len, names_len; 180 181 ids = nvlist_get_number_array(nvl, "ids", &id_len); 182 counts = nvlist_get_number_array(nvl, "counters", &counter_len); 183 names = nvlist_get_string_array(nvl, "names", &names_len); 184 assert(id_len == counter_len); 185 assert(counter_len == names_len); 186 187 TAILQ_INIT(counters); 188 189 for (size_t i = 0; i < id_len; i++) { 190 struct pfctl_status_counter *c; 191 192 c = malloc(sizeof(*c)); 193 194 c->id = ids[i]; 195 c->counter = counts[i]; 196 c->name = strdup(names[i]); 197 198 TAILQ_INSERT_TAIL(counters, c, entry); 199 } 200 } 201 202 struct pfctl_status * 203 pfctl_get_status(int dev) 204 { 205 struct pfctl_status *status; 206 nvlist_t *nvl; 207 size_t len; 208 const void *chksum; 209 210 status = calloc(1, sizeof(*status)); 211 if (status == NULL) 212 return (NULL); 213 214 nvl = nvlist_create(0); 215 216 if (pfctl_do_ioctl(dev, DIOCGETSTATUSNV, 4096, &nvl)) { 217 free(status); 218 return (NULL); 219 } 220 221 status->running = nvlist_get_bool(nvl, "running"); 222 status->since = nvlist_get_number(nvl, "since"); 223 status->debug = nvlist_get_number(nvl, "debug"); 224 status->hostid = ntohl(nvlist_get_number(nvl, "hostid")); 225 status->states = nvlist_get_number(nvl, "states"); 226 status->src_nodes = nvlist_get_number(nvl, "src_nodes"); 227 status->syncookies_active = nvlist_get_bool(nvl, "syncookies_active"); 228 status->reass = nvlist_get_number(nvl, "reass"); 229 230 strlcpy(status->ifname, nvlist_get_string(nvl, "ifname"), 231 IFNAMSIZ); 232 chksum = nvlist_get_binary(nvl, "chksum", &len); 233 assert(len == PF_MD5_DIGEST_LENGTH); 234 memcpy(status->pf_chksum, chksum, len); 235 236 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "counters"), 237 &status->counters); 238 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "lcounters"), 239 &status->lcounters); 240 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "fcounters"), 241 &status->fcounters); 242 _pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"), 243 &status->scounters); 244 245 pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 3, 246 (uint64_t *)status->pcounters, NULL); 247 pf_nvuint_64_array(nvl, "bcounters", 2 * 2, 248 (uint64_t *)status->bcounters, NULL); 249 250 nvlist_destroy(nvl); 251 252 return (status); 253 } 254 255 void 256 pfctl_free_status(struct pfctl_status *status) 257 { 258 struct pfctl_status_counter *c, *tmp; 259 260 TAILQ_FOREACH_SAFE(c, &status->counters, entry, tmp) { 261 free(c->name); 262 free(c); 263 } 264 TAILQ_FOREACH_SAFE(c, &status->lcounters, entry, tmp) { 265 free(c->name); 266 free(c); 267 } 268 TAILQ_FOREACH_SAFE(c, &status->fcounters, entry, tmp) { 269 free(c->name); 270 free(c); 271 } 272 TAILQ_FOREACH_SAFE(c, &status->scounters, entry, tmp) { 273 free(c->name); 274 free(c); 275 } 276 277 free(status); 278 } 279 280 static void 281 pfctl_nv_add_addr(nvlist_t *nvparent, const char *name, 282 const struct pf_addr *addr) 283 { 284 nvlist_t *nvl = nvlist_create(0); 285 286 nvlist_add_binary(nvl, "addr", addr, sizeof(*addr)); 287 288 nvlist_add_nvlist(nvparent, name, nvl); 289 nvlist_destroy(nvl); 290 } 291 292 static void 293 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *addr) 294 { 295 size_t len; 296 const void *data; 297 298 data = nvlist_get_binary(nvl, "addr", &len); 299 assert(len == sizeof(struct pf_addr)); 300 memcpy(addr, data, len); 301 } 302 303 static void 304 pfctl_nv_add_addr_wrap(nvlist_t *nvparent, const char *name, 305 const struct pf_addr_wrap *addr) 306 { 307 nvlist_t *nvl = nvlist_create(0); 308 309 nvlist_add_number(nvl, "type", addr->type); 310 nvlist_add_number(nvl, "iflags", addr->iflags); 311 if (addr->type == PF_ADDR_DYNIFTL) 312 nvlist_add_string(nvl, "ifname", addr->v.ifname); 313 if (addr->type == PF_ADDR_TABLE) 314 nvlist_add_string(nvl, "tblname", addr->v.tblname); 315 pfctl_nv_add_addr(nvl, "addr", &addr->v.a.addr); 316 pfctl_nv_add_addr(nvl, "mask", &addr->v.a.mask); 317 318 nvlist_add_nvlist(nvparent, name, nvl); 319 nvlist_destroy(nvl); 320 } 321 322 static void 323 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr) 324 { 325 bzero(addr, sizeof(*addr)); 326 327 addr->type = nvlist_get_number(nvl, "type"); 328 addr->iflags = nvlist_get_number(nvl, "iflags"); 329 if (addr->type == PF_ADDR_DYNIFTL) { 330 strlcpy(addr->v.ifname, nvlist_get_string(nvl, "ifname"), 331 IFNAMSIZ); 332 addr->p.dyncnt = nvlist_get_number(nvl, "dyncnt"); 333 } 334 if (addr->type == PF_ADDR_TABLE) { 335 strlcpy(addr->v.tblname, nvlist_get_string(nvl, "tblname"), 336 PF_TABLE_NAME_SIZE); 337 addr->p.tblcnt = nvlist_get_number(nvl, "tblcnt"); 338 } 339 340 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &addr->v.a.addr); 341 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"), &addr->v.a.mask); 342 } 343 344 static void 345 pfctl_nv_add_rule_addr(nvlist_t *nvparent, const char *name, 346 const struct pf_rule_addr *addr) 347 { 348 uint64_t ports[2]; 349 nvlist_t *nvl = nvlist_create(0); 350 351 pfctl_nv_add_addr_wrap(nvl, "addr", &addr->addr); 352 ports[0] = addr->port[0]; 353 ports[1] = addr->port[1]; 354 nvlist_add_number_array(nvl, "port", ports, 2); 355 nvlist_add_number(nvl, "neg", addr->neg); 356 nvlist_add_number(nvl, "port_op", addr->port_op); 357 358 nvlist_add_nvlist(nvparent, name, nvl); 359 nvlist_destroy(nvl); 360 } 361 362 static void 363 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr) 364 { 365 pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"), &addr->addr); 366 367 pf_nvuint_16_array(nvl, "port", 2, addr->port, NULL); 368 addr->neg = nvlist_get_number(nvl, "neg"); 369 addr->port_op = nvlist_get_number(nvl, "port_op"); 370 } 371 372 static void 373 pfctl_nv_add_mape(nvlist_t *nvparent, const char *name, 374 const struct pf_mape_portset *mape) 375 { 376 nvlist_t *nvl = nvlist_create(0); 377 378 nvlist_add_number(nvl, "offset", mape->offset); 379 nvlist_add_number(nvl, "psidlen", mape->psidlen); 380 nvlist_add_number(nvl, "psid", mape->psid); 381 nvlist_add_nvlist(nvparent, name, nvl); 382 nvlist_destroy(nvl); 383 } 384 385 static void 386 pfctl_nv_add_pool(nvlist_t *nvparent, const char *name, 387 const struct pfctl_pool *pool) 388 { 389 uint64_t ports[2]; 390 nvlist_t *nvl = nvlist_create(0); 391 392 nvlist_add_binary(nvl, "key", &pool->key, sizeof(pool->key)); 393 pfctl_nv_add_addr(nvl, "counter", &pool->counter); 394 nvlist_add_number(nvl, "tblidx", pool->tblidx); 395 396 ports[0] = pool->proxy_port[0]; 397 ports[1] = pool->proxy_port[1]; 398 nvlist_add_number_array(nvl, "proxy_port", ports, 2); 399 nvlist_add_number(nvl, "opts", pool->opts); 400 pfctl_nv_add_mape(nvl, "mape", &pool->mape); 401 402 nvlist_add_nvlist(nvparent, name, nvl); 403 nvlist_destroy(nvl); 404 } 405 406 static void 407 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape) 408 { 409 mape->offset = nvlist_get_number(nvl, "offset"); 410 mape->psidlen = nvlist_get_number(nvl, "psidlen"); 411 mape->psid = nvlist_get_number(nvl, "psid"); 412 } 413 414 static void 415 pf_nvpool_to_pool(const nvlist_t *nvl, struct pfctl_pool *pool) 416 { 417 size_t len; 418 const void *data; 419 420 data = nvlist_get_binary(nvl, "key", &len); 421 assert(len == sizeof(pool->key)); 422 memcpy(&pool->key, data, len); 423 424 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"), &pool->counter); 425 426 pool->tblidx = nvlist_get_number(nvl, "tblidx"); 427 pf_nvuint_16_array(nvl, "proxy_port", 2, pool->proxy_port, NULL); 428 pool->opts = nvlist_get_number(nvl, "opts"); 429 430 if (nvlist_exists_nvlist(nvl, "mape")) 431 pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"), &pool->mape); 432 } 433 434 static void 435 pfctl_nv_add_uid(nvlist_t *nvparent, const char *name, 436 const struct pf_rule_uid *uid) 437 { 438 uint64_t uids[2]; 439 nvlist_t *nvl = nvlist_create(0); 440 441 uids[0] = uid->uid[0]; 442 uids[1] = uid->uid[1]; 443 nvlist_add_number_array(nvl, "uid", uids, 2); 444 nvlist_add_number(nvl, "op", uid->op); 445 446 nvlist_add_nvlist(nvparent, name, nvl); 447 nvlist_destroy(nvl); 448 } 449 450 static void 451 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid) 452 { 453 pf_nvuint_32_array(nvl, "uid", 2, uid->uid, NULL); 454 uid->op = nvlist_get_number(nvl, "op"); 455 } 456 457 static void 458 pfctl_nv_add_divert(nvlist_t *nvparent, const char *name, 459 const struct pfctl_rule *r) 460 { 461 nvlist_t *nvl = nvlist_create(0); 462 463 pfctl_nv_add_addr(nvl, "addr", &r->divert.addr); 464 nvlist_add_number(nvl, "port", r->divert.port); 465 466 nvlist_add_nvlist(nvparent, name, nvl); 467 nvlist_destroy(nvl); 468 } 469 470 static void 471 pf_nvdivert_to_divert(const nvlist_t *nvl, struct pfctl_rule *rule) 472 { 473 pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"), &rule->divert.addr); 474 rule->divert.port = nvlist_get_number(nvl, "port"); 475 } 476 477 static void 478 pf_nvrule_to_rule(const nvlist_t *nvl, struct pfctl_rule *rule) 479 { 480 const uint64_t *skip; 481 const char *const *labels; 482 size_t skipcount, labelcount; 483 484 rule->nr = nvlist_get_number(nvl, "nr"); 485 486 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"), &rule->src); 487 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"), &rule->dst); 488 489 skip = nvlist_get_number_array(nvl, "skip", &skipcount); 490 assert(skip); 491 assert(skipcount == PF_SKIP_COUNT); 492 for (int i = 0; i < PF_SKIP_COUNT; i++) 493 rule->skip[i].nr = skip[i]; 494 495 labels = nvlist_get_string_array(nvl, "labels", &labelcount); 496 assert(labelcount <= PF_RULE_MAX_LABEL_COUNT); 497 for (size_t i = 0; i < labelcount; i++) 498 strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE); 499 rule->ridentifier = nvlist_get_number(nvl, "ridentifier"); 500 strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ); 501 strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE); 502 strlcpy(rule->pqname, nvlist_get_string(nvl, "pqname"), PF_QNAME_SIZE); 503 strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"), 504 PF_TAG_NAME_SIZE); 505 strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"), 506 PF_TAG_NAME_SIZE); 507 508 strlcpy(rule->overload_tblname, nvlist_get_string(nvl, "overload_tblname"), 509 PF_TABLE_NAME_SIZE); 510 511 pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"), &rule->rpool); 512 513 rule->evaluations = nvlist_get_number(nvl, "evaluations"); 514 pf_nvuint_64_array(nvl, "packets", 2, rule->packets, NULL); 515 pf_nvuint_64_array(nvl, "bytes", 2, rule->bytes, NULL); 516 517 if (nvlist_exists_number(nvl, "timestamp")) { 518 rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp"); 519 } 520 521 rule->os_fingerprint = nvlist_get_number(nvl, "os_fingerprint"); 522 523 rule->rtableid = nvlist_get_number(nvl, "rtableid"); 524 pf_nvuint_32_array(nvl, "timeout", PFTM_MAX, rule->timeout, NULL); 525 rule->max_states = nvlist_get_number(nvl, "max_states"); 526 rule->max_src_nodes = nvlist_get_number(nvl, "max_src_nodes"); 527 rule->max_src_states = nvlist_get_number(nvl, "max_src_states"); 528 rule->max_src_conn = nvlist_get_number(nvl, "max_src_conn"); 529 rule->max_src_conn_rate.limit = 530 nvlist_get_number(nvl, "max_src_conn_rate.limit"); 531 rule->max_src_conn_rate.seconds = 532 nvlist_get_number(nvl, "max_src_conn_rate.seconds"); 533 rule->qid = nvlist_get_number(nvl, "qid"); 534 rule->pqid = nvlist_get_number(nvl, "pqid"); 535 rule->dnpipe = nvlist_get_number(nvl, "dnpipe"); 536 rule->dnrpipe = nvlist_get_number(nvl, "dnrpipe"); 537 rule->free_flags = nvlist_get_number(nvl, "dnflags"); 538 rule->prob = nvlist_get_number(nvl, "prob"); 539 rule->cuid = nvlist_get_number(nvl, "cuid"); 540 rule->cpid = nvlist_get_number(nvl, "cpid"); 541 542 rule->return_icmp = nvlist_get_number(nvl, "return_icmp"); 543 rule->return_icmp6 = nvlist_get_number(nvl, "return_icmp6"); 544 rule->max_mss = nvlist_get_number(nvl, "max_mss"); 545 rule->scrub_flags = nvlist_get_number(nvl, "scrub_flags"); 546 547 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"), &rule->uid); 548 pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "gid"), 549 (struct pf_rule_uid *)&rule->gid); 550 551 rule->rule_flag = nvlist_get_number(nvl, "rule_flag"); 552 rule->action = nvlist_get_number(nvl, "action"); 553 rule->direction = nvlist_get_number(nvl, "direction"); 554 rule->log = nvlist_get_number(nvl, "log"); 555 rule->logif = nvlist_get_number(nvl, "logif"); 556 rule->quick = nvlist_get_number(nvl, "quick"); 557 rule->ifnot = nvlist_get_number(nvl, "ifnot"); 558 rule->match_tag_not = nvlist_get_number(nvl, "match_tag_not"); 559 rule->natpass = nvlist_get_number(nvl, "natpass"); 560 561 rule->keep_state = nvlist_get_number(nvl, "keep_state"); 562 rule->af = nvlist_get_number(nvl, "af"); 563 rule->proto = nvlist_get_number(nvl, "proto"); 564 rule->type = nvlist_get_number(nvl, "type"); 565 rule->code = nvlist_get_number(nvl, "code"); 566 rule->flags = nvlist_get_number(nvl, "flags"); 567 rule->flagset = nvlist_get_number(nvl, "flagset"); 568 rule->min_ttl = nvlist_get_number(nvl, "min_ttl"); 569 rule->allow_opts = nvlist_get_number(nvl, "allow_opts"); 570 rule->rt = nvlist_get_number(nvl, "rt"); 571 rule->return_ttl = nvlist_get_number(nvl, "return_ttl"); 572 rule->tos = nvlist_get_number(nvl, "tos"); 573 rule->set_tos = nvlist_get_number(nvl, "set_tos"); 574 rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative"); 575 rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard"); 576 577 rule->flush = nvlist_get_number(nvl, "flush"); 578 rule->prio = nvlist_get_number(nvl, "prio"); 579 pf_nvuint_8_array(nvl, "set_prio", 2, rule->set_prio, NULL); 580 581 pf_nvdivert_to_divert(nvlist_get_nvlist(nvl, "divert"), rule); 582 583 rule->states_cur = nvlist_get_number(nvl, "states_cur"); 584 rule->states_tot = nvlist_get_number(nvl, "states_tot"); 585 rule->src_nodes = nvlist_get_number(nvl, "src_nodes"); 586 } 587 588 static void 589 pfctl_nveth_addr_to_eth_addr(const nvlist_t *nvl, struct pfctl_eth_addr *addr) 590 { 591 static const u_int8_t EMPTY_MAC[ETHER_ADDR_LEN] = { 0 }; 592 size_t len; 593 const void *data; 594 595 data = nvlist_get_binary(nvl, "addr", &len); 596 assert(len == sizeof(addr->addr)); 597 memcpy(addr->addr, data, sizeof(addr->addr)); 598 599 data = nvlist_get_binary(nvl, "mask", &len); 600 assert(len == sizeof(addr->mask)); 601 memcpy(addr->mask, data, sizeof(addr->mask)); 602 603 addr->neg = nvlist_get_bool(nvl, "neg"); 604 605 /* To make checks for 'is this address set?' easier. */ 606 addr->isset = memcmp(addr->addr, EMPTY_MAC, ETHER_ADDR_LEN) != 0; 607 } 608 609 static nvlist_t * 610 pfctl_eth_addr_to_nveth_addr(const struct pfctl_eth_addr *addr) 611 { 612 nvlist_t *nvl; 613 614 nvl = nvlist_create(0); 615 if (nvl == NULL) 616 return (NULL); 617 618 nvlist_add_bool(nvl, "neg", addr->neg); 619 nvlist_add_binary(nvl, "addr", &addr->addr, ETHER_ADDR_LEN); 620 nvlist_add_binary(nvl, "mask", &addr->mask, ETHER_ADDR_LEN); 621 622 return (nvl); 623 } 624 625 static void 626 pfctl_nveth_rule_to_eth_rule(const nvlist_t *nvl, struct pfctl_eth_rule *rule) 627 { 628 const char *const *labels; 629 size_t labelcount, i; 630 631 rule->nr = nvlist_get_number(nvl, "nr"); 632 rule->quick = nvlist_get_bool(nvl, "quick"); 633 strlcpy(rule->ifname, nvlist_get_string(nvl, "ifname"), IFNAMSIZ); 634 rule->ifnot = nvlist_get_bool(nvl, "ifnot"); 635 rule->direction = nvlist_get_number(nvl, "direction"); 636 rule->proto = nvlist_get_number(nvl, "proto"); 637 strlcpy(rule->match_tagname, nvlist_get_string(nvl, "match_tagname"), 638 PF_TAG_NAME_SIZE); 639 rule->match_tag = nvlist_get_number(nvl, "match_tag"); 640 rule->match_tag_not = nvlist_get_bool(nvl, "match_tag_not"); 641 642 labels = nvlist_get_string_array(nvl, "labels", &labelcount); 643 assert(labelcount <= PF_RULE_MAX_LABEL_COUNT); 644 for (i = 0; i < labelcount; i++) 645 strlcpy(rule->label[i], labels[i], PF_RULE_LABEL_SIZE); 646 rule->ridentifier = nvlist_get_number(nvl, "ridentifier"); 647 648 pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "src"), 649 &rule->src); 650 pfctl_nveth_addr_to_eth_addr(nvlist_get_nvlist(nvl, "dst"), 651 &rule->dst); 652 653 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipsrc"), 654 &rule->ipsrc); 655 pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "ipdst"), 656 &rule->ipdst); 657 658 rule->evaluations = nvlist_get_number(nvl, "evaluations"); 659 rule->packets[0] = nvlist_get_number(nvl, "packets-in"); 660 rule->packets[1] = nvlist_get_number(nvl, "packets-out"); 661 rule->bytes[0] = nvlist_get_number(nvl, "bytes-in"); 662 rule->bytes[1] = nvlist_get_number(nvl, "bytes-out"); 663 664 if (nvlist_exists_number(nvl, "timestamp")) { 665 rule->last_active_timestamp = nvlist_get_number(nvl, "timestamp"); 666 } 667 668 strlcpy(rule->qname, nvlist_get_string(nvl, "qname"), PF_QNAME_SIZE); 669 strlcpy(rule->tagname, nvlist_get_string(nvl, "tagname"), 670 PF_TAG_NAME_SIZE); 671 672 rule->dnpipe = nvlist_get_number(nvl, "dnpipe"); 673 rule->dnflags = nvlist_get_number(nvl, "dnflags"); 674 675 rule->anchor_relative = nvlist_get_number(nvl, "anchor_relative"); 676 rule->anchor_wildcard = nvlist_get_number(nvl, "anchor_wildcard"); 677 678 strlcpy(rule->bridge_to, nvlist_get_string(nvl, "bridge_to"), 679 IFNAMSIZ); 680 681 rule->action = nvlist_get_number(nvl, "action"); 682 } 683 684 int 685 pfctl_get_eth_rulesets_info(int dev, struct pfctl_eth_rulesets_info *ri, 686 const char *path) 687 { 688 nvlist_t *nvl; 689 int ret; 690 691 bzero(ri, sizeof(*ri)); 692 693 nvl = nvlist_create(0); 694 nvlist_add_string(nvl, "path", path); 695 696 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESETS, 256, &nvl)) != 0) 697 return (ret); 698 699 ri->nr = nvlist_get_number(nvl, "nr"); 700 701 nvlist_destroy(nvl); 702 return (0); 703 } 704 705 int 706 pfctl_get_eth_ruleset(int dev, const char *path, int nr, 707 struct pfctl_eth_ruleset_info *ri) 708 { 709 nvlist_t *nvl; 710 int ret; 711 712 bzero(ri, sizeof(*ri)); 713 714 nvl = nvlist_create(0); 715 nvlist_add_string(nvl, "path", path); 716 nvlist_add_number(nvl, "nr", nr); 717 718 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULESET, 1024, &nvl)) != 0) 719 return (ret); 720 721 ri->nr = nvlist_get_number(nvl, "nr"); 722 strlcpy(ri->path, nvlist_get_string(nvl, "path"), MAXPATHLEN); 723 strlcpy(ri->name, nvlist_get_string(nvl, "name"), 724 PF_ANCHOR_NAME_SIZE); 725 726 return (0); 727 } 728 729 int 730 pfctl_get_eth_rules_info(int dev, struct pfctl_eth_rules_info *rules, 731 const char *path) 732 { 733 nvlist_t *nvl; 734 int ret; 735 736 bzero(rules, sizeof(*rules)); 737 738 nvl = nvlist_create(0); 739 nvlist_add_string(nvl, "anchor", path); 740 741 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULES, 1024, &nvl)) != 0) 742 return (ret); 743 744 rules->nr = nvlist_get_number(nvl, "nr"); 745 rules->ticket = nvlist_get_number(nvl, "ticket"); 746 747 nvlist_destroy(nvl); 748 return (0); 749 } 750 751 int 752 pfctl_get_eth_rule(int dev, uint32_t nr, uint32_t ticket, 753 const char *path, struct pfctl_eth_rule *rule, bool clear, 754 char *anchor_call) 755 { 756 nvlist_t *nvl; 757 int ret; 758 759 nvl = nvlist_create(0); 760 761 nvlist_add_string(nvl, "anchor", path); 762 nvlist_add_number(nvl, "ticket", ticket); 763 nvlist_add_number(nvl, "nr", nr); 764 nvlist_add_bool(nvl, "clear", clear); 765 766 if ((ret = pfctl_do_ioctl(dev, DIOCGETETHRULE, 4096, &nvl)) != 0) 767 return (ret); 768 769 pfctl_nveth_rule_to_eth_rule(nvl, rule); 770 771 if (anchor_call) 772 strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"), 773 MAXPATHLEN); 774 775 nvlist_destroy(nvl); 776 return (0); 777 } 778 779 int 780 pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r, const char *anchor, 781 const char *anchor_call, uint32_t ticket) 782 { 783 struct pfioc_nv nv; 784 nvlist_t *nvl, *addr; 785 void *packed; 786 int error = 0; 787 size_t labelcount, size; 788 789 nvl = nvlist_create(0); 790 791 nvlist_add_number(nvl, "ticket", ticket); 792 nvlist_add_string(nvl, "anchor", anchor); 793 nvlist_add_string(nvl, "anchor_call", anchor_call); 794 795 nvlist_add_number(nvl, "nr", r->nr); 796 nvlist_add_bool(nvl, "quick", r->quick); 797 nvlist_add_string(nvl, "ifname", r->ifname); 798 nvlist_add_bool(nvl, "ifnot", r->ifnot); 799 nvlist_add_number(nvl, "direction", r->direction); 800 nvlist_add_number(nvl, "proto", r->proto); 801 nvlist_add_string(nvl, "match_tagname", r->match_tagname); 802 nvlist_add_bool(nvl, "match_tag_not", r->match_tag_not); 803 804 addr = pfctl_eth_addr_to_nveth_addr(&r->src); 805 if (addr == NULL) { 806 nvlist_destroy(nvl); 807 return (ENOMEM); 808 } 809 nvlist_add_nvlist(nvl, "src", addr); 810 nvlist_destroy(addr); 811 812 addr = pfctl_eth_addr_to_nveth_addr(&r->dst); 813 if (addr == NULL) { 814 nvlist_destroy(nvl); 815 return (ENOMEM); 816 } 817 nvlist_add_nvlist(nvl, "dst", addr); 818 nvlist_destroy(addr); 819 820 pfctl_nv_add_rule_addr(nvl, "ipsrc", &r->ipsrc); 821 pfctl_nv_add_rule_addr(nvl, "ipdst", &r->ipdst); 822 823 labelcount = 0; 824 while (r->label[labelcount][0] != 0 && 825 labelcount < PF_RULE_MAX_LABEL_COUNT) { 826 nvlist_append_string_array(nvl, "labels", 827 r->label[labelcount]); 828 labelcount++; 829 } 830 nvlist_add_number(nvl, "ridentifier", r->ridentifier); 831 832 nvlist_add_string(nvl, "qname", r->qname); 833 nvlist_add_string(nvl, "tagname", r->tagname); 834 nvlist_add_number(nvl, "dnpipe", r->dnpipe); 835 nvlist_add_number(nvl, "dnflags", r->dnflags); 836 837 nvlist_add_string(nvl, "bridge_to", r->bridge_to); 838 839 nvlist_add_number(nvl, "action", r->action); 840 841 packed = nvlist_pack(nvl, &size); 842 if (packed == NULL) { 843 nvlist_destroy(nvl); 844 return (ENOMEM); 845 } 846 847 nv.len = size; 848 nv.size = size; 849 nv.data = packed; 850 851 if (ioctl(dev, DIOCADDETHRULE, &nv) != 0) 852 error = errno; 853 854 free(packed); 855 nvlist_destroy(nvl); 856 857 return (error); 858 } 859 860 int 861 pfctl_add_rule(int dev, const struct pfctl_rule *r, const char *anchor, 862 const char *anchor_call, uint32_t ticket, uint32_t pool_ticket) 863 { 864 struct pfioc_nv nv; 865 uint64_t timeouts[PFTM_MAX]; 866 uint64_t set_prio[2]; 867 nvlist_t *nvl, *nvlr; 868 size_t labelcount; 869 int ret; 870 871 nvl = nvlist_create(0); 872 nvlr = nvlist_create(0); 873 874 nvlist_add_number(nvl, "ticket", ticket); 875 nvlist_add_number(nvl, "pool_ticket", pool_ticket); 876 nvlist_add_string(nvl, "anchor", anchor); 877 nvlist_add_string(nvl, "anchor_call", anchor_call); 878 879 nvlist_add_number(nvlr, "nr", r->nr); 880 pfctl_nv_add_rule_addr(nvlr, "src", &r->src); 881 pfctl_nv_add_rule_addr(nvlr, "dst", &r->dst); 882 883 labelcount = 0; 884 while (r->label[labelcount][0] != 0 && 885 labelcount < PF_RULE_MAX_LABEL_COUNT) { 886 nvlist_append_string_array(nvlr, "labels", 887 r->label[labelcount]); 888 labelcount++; 889 } 890 nvlist_add_number(nvlr, "ridentifier", r->ridentifier); 891 892 nvlist_add_string(nvlr, "ifname", r->ifname); 893 nvlist_add_string(nvlr, "qname", r->qname); 894 nvlist_add_string(nvlr, "pqname", r->pqname); 895 nvlist_add_string(nvlr, "tagname", r->tagname); 896 nvlist_add_string(nvlr, "match_tagname", r->match_tagname); 897 nvlist_add_string(nvlr, "overload_tblname", r->overload_tblname); 898 899 pfctl_nv_add_pool(nvlr, "rpool", &r->rpool); 900 901 nvlist_add_number(nvlr, "os_fingerprint", r->os_fingerprint); 902 903 nvlist_add_number(nvlr, "rtableid", r->rtableid); 904 for (int i = 0; i < PFTM_MAX; i++) 905 timeouts[i] = r->timeout[i]; 906 nvlist_add_number_array(nvlr, "timeout", timeouts, PFTM_MAX); 907 nvlist_add_number(nvlr, "max_states", r->max_states); 908 nvlist_add_number(nvlr, "max_src_nodes", r->max_src_nodes); 909 nvlist_add_number(nvlr, "max_src_states", r->max_src_states); 910 nvlist_add_number(nvlr, "max_src_conn", r->max_src_conn); 911 nvlist_add_number(nvlr, "max_src_conn_rate.limit", 912 r->max_src_conn_rate.limit); 913 nvlist_add_number(nvlr, "max_src_conn_rate.seconds", 914 r->max_src_conn_rate.seconds); 915 nvlist_add_number(nvlr, "dnpipe", r->dnpipe); 916 nvlist_add_number(nvlr, "dnrpipe", r->dnrpipe); 917 nvlist_add_number(nvlr, "dnflags", r->free_flags); 918 nvlist_add_number(nvlr, "prob", r->prob); 919 nvlist_add_number(nvlr, "cuid", r->cuid); 920 nvlist_add_number(nvlr, "cpid", r->cpid); 921 922 nvlist_add_number(nvlr, "return_icmp", r->return_icmp); 923 nvlist_add_number(nvlr, "return_icmp6", r->return_icmp6); 924 925 nvlist_add_number(nvlr, "max_mss", r->max_mss); 926 nvlist_add_number(nvlr, "scrub_flags", r->scrub_flags); 927 928 pfctl_nv_add_uid(nvlr, "uid", &r->uid); 929 pfctl_nv_add_uid(nvlr, "gid", (const struct pf_rule_uid *)&r->gid); 930 931 nvlist_add_number(nvlr, "rule_flag", r->rule_flag); 932 nvlist_add_number(nvlr, "action", r->action); 933 nvlist_add_number(nvlr, "direction", r->direction); 934 nvlist_add_number(nvlr, "log", r->log); 935 nvlist_add_number(nvlr, "logif", r->logif); 936 nvlist_add_number(nvlr, "quick", r->quick); 937 nvlist_add_number(nvlr, "ifnot", r->ifnot); 938 nvlist_add_number(nvlr, "match_tag_not", r->match_tag_not); 939 nvlist_add_number(nvlr, "natpass", r->natpass); 940 941 nvlist_add_number(nvlr, "keep_state", r->keep_state); 942 nvlist_add_number(nvlr, "af", r->af); 943 nvlist_add_number(nvlr, "proto", r->proto); 944 nvlist_add_number(nvlr, "type", r->type); 945 nvlist_add_number(nvlr, "code", r->code); 946 nvlist_add_number(nvlr, "flags", r->flags); 947 nvlist_add_number(nvlr, "flagset", r->flagset); 948 nvlist_add_number(nvlr, "min_ttl", r->min_ttl); 949 nvlist_add_number(nvlr, "allow_opts", r->allow_opts); 950 nvlist_add_number(nvlr, "rt", r->rt); 951 nvlist_add_number(nvlr, "return_ttl", r->return_ttl); 952 nvlist_add_number(nvlr, "tos", r->tos); 953 nvlist_add_number(nvlr, "set_tos", r->set_tos); 954 nvlist_add_number(nvlr, "anchor_relative", r->anchor_relative); 955 nvlist_add_number(nvlr, "anchor_wildcard", r->anchor_wildcard); 956 957 nvlist_add_number(nvlr, "flush", r->flush); 958 959 nvlist_add_number(nvlr, "prio", r->prio); 960 set_prio[0] = r->set_prio[0]; 961 set_prio[1] = r->set_prio[1]; 962 nvlist_add_number_array(nvlr, "set_prio", set_prio, 2); 963 964 pfctl_nv_add_divert(nvlr, "divert", r); 965 966 nvlist_add_nvlist(nvl, "rule", nvlr); 967 nvlist_destroy(nvlr); 968 969 /* Now do the call. */ 970 nv.data = nvlist_pack(nvl, &nv.len); 971 nv.size = nv.len; 972 973 ret = ioctl(dev, DIOCADDRULENV, &nv); 974 if (ret == -1) 975 ret = errno; 976 977 free(nv.data); 978 nvlist_destroy(nvl); 979 980 return (ret); 981 } 982 983 int 984 pfctl_get_rules_info(int dev, struct pfctl_rules_info *rules, uint32_t ruleset, 985 const char *path) 986 { 987 struct pfioc_rule pr; 988 int ret; 989 990 bzero(&pr, sizeof(pr)); 991 if (strlcpy(pr.anchor, path, sizeof(pr.anchor)) >= sizeof(pr.anchor)) 992 return (E2BIG); 993 994 pr.rule.action = ruleset; 995 ret = ioctl(dev, DIOCGETRULES, &pr); 996 if (ret != 0) 997 return (ret); 998 999 rules->nr = pr.nr; 1000 rules->ticket = pr.ticket; 1001 1002 return (0); 1003 } 1004 1005 int 1006 pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket, const char *anchor, 1007 uint32_t ruleset, struct pfctl_rule *rule, char *anchor_call) 1008 { 1009 return (pfctl_get_clear_rule(dev, nr, ticket, anchor, ruleset, rule, 1010 anchor_call, false)); 1011 } 1012 1013 int pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket, 1014 const char *anchor, uint32_t ruleset, struct pfctl_rule *rule, 1015 char *anchor_call, bool clear) 1016 { 1017 nvlist_t *nvl; 1018 int ret; 1019 1020 nvl = nvlist_create(0); 1021 if (nvl == 0) 1022 return (ENOMEM); 1023 1024 nvlist_add_number(nvl, "nr", nr); 1025 nvlist_add_number(nvl, "ticket", ticket); 1026 nvlist_add_string(nvl, "anchor", anchor); 1027 nvlist_add_number(nvl, "ruleset", ruleset); 1028 1029 if (clear) 1030 nvlist_add_bool(nvl, "clear_counter", true); 1031 1032 if ((ret = pfctl_do_ioctl(dev, DIOCGETRULENV, 8192, &nvl)) != 0) 1033 return (ret); 1034 1035 pf_nvrule_to_rule(nvlist_get_nvlist(nvl, "rule"), rule); 1036 1037 if (anchor_call) 1038 strlcpy(anchor_call, nvlist_get_string(nvl, "anchor_call"), 1039 MAXPATHLEN); 1040 1041 nvlist_destroy(nvl); 1042 1043 return (0); 1044 } 1045 1046 int 1047 pfctl_set_keepcounters(int dev, bool keep) 1048 { 1049 struct pfioc_nv nv; 1050 nvlist_t *nvl; 1051 int ret; 1052 1053 nvl = nvlist_create(0); 1054 1055 nvlist_add_bool(nvl, "keep_counters", keep); 1056 1057 nv.data = nvlist_pack(nvl, &nv.len); 1058 nv.size = nv.len; 1059 1060 nvlist_destroy(nvl); 1061 1062 ret = ioctl(dev, DIOCKEEPCOUNTERS, &nv); 1063 1064 free(nv.data); 1065 return (ret); 1066 } 1067 1068 static void 1069 pfctl_nv_add_state_cmp(nvlist_t *nvl, const char *name, 1070 const struct pfctl_state_cmp *cmp) 1071 { 1072 nvlist_t *nv; 1073 1074 nv = nvlist_create(0); 1075 1076 nvlist_add_number(nv, "id", cmp->id); 1077 nvlist_add_number(nv, "creatorid", htonl(cmp->creatorid)); 1078 nvlist_add_number(nv, "direction", cmp->direction); 1079 1080 nvlist_add_nvlist(nvl, name, nv); 1081 nvlist_destroy(nv); 1082 } 1083 1084 static void 1085 pf_state_key_export_to_state_key(struct pfctl_state_key *ps, 1086 const struct pf_state_key_export *s) 1087 { 1088 bcopy(s->addr, ps->addr, sizeof(ps->addr[0]) * 2); 1089 ps->port[0] = s->port[0]; 1090 ps->port[1] = s->port[1]; 1091 } 1092 1093 static void 1094 pf_state_peer_export_to_state_peer(struct pfctl_state_peer *ps, 1095 const struct pf_state_peer_export *s) 1096 { 1097 /* Ignore scrub. */ 1098 ps->seqlo = s->seqlo; 1099 ps->seqhi = s->seqhi; 1100 ps->seqdiff = s->seqdiff; 1101 /* Ignore max_win & mss */ 1102 ps->state = s->state; 1103 ps->wscale = s->wscale; 1104 } 1105 1106 static void 1107 pf_state_export_to_state(struct pfctl_state *ps, const struct pf_state_export *s) 1108 { 1109 assert(s->version >= PF_STATE_VERSION); 1110 1111 ps->id = s->id; 1112 strlcpy(ps->ifname, s->ifname, sizeof(ps->ifname)); 1113 strlcpy(ps->orig_ifname, s->orig_ifname, sizeof(ps->orig_ifname)); 1114 strlcpy(ps->rt_ifname, s->rt_ifname, sizeof(ps->rt_ifname)); 1115 pf_state_key_export_to_state_key(&ps->key[0], &s->key[0]); 1116 pf_state_key_export_to_state_key(&ps->key[1], &s->key[1]); 1117 pf_state_peer_export_to_state_peer(&ps->src, &s->src); 1118 pf_state_peer_export_to_state_peer(&ps->dst, &s->dst); 1119 bcopy(&s->rt_addr, &ps->rt_addr, sizeof(ps->rt_addr)); 1120 ps->rule = ntohl(s->rule); 1121 ps->anchor = ntohl(s->anchor); 1122 ps->nat_rule = ntohl(s->nat_rule); 1123 ps->creation = ntohl(s->creation); 1124 ps->expire = ntohl(s->expire); 1125 ps->packets[0] = s->packets[0]; 1126 ps->packets[1] = s->packets[1]; 1127 ps->bytes[0] = s->bytes[0]; 1128 ps->bytes[1] = s->bytes[1]; 1129 ps->creatorid = ntohl(s->creatorid); 1130 ps->key[0].proto = s->proto; 1131 ps->key[1].proto = s->proto; 1132 ps->key[0].af = s->af; 1133 ps->key[1].af = s->af; 1134 ps->direction = s->direction; 1135 ps->state_flags = ntohs(s->state_flags); 1136 ps->sync_flags = ntohs(s->sync_flags); 1137 ps->qid = ntohs(s->qid); 1138 ps->pqid = ntohs(s->pqid); 1139 ps->dnpipe = ntohs(s->dnpipe); 1140 ps->dnrpipe = ntohs(s->dnrpipe); 1141 ps->rtableid = ntohl(s->rtableid); 1142 ps->min_ttl = s->min_ttl; 1143 ps->set_tos = s->set_tos; 1144 ps->max_mss = ntohs(s->max_mss); 1145 ps->rt = s->rt; 1146 ps->set_prio[0] = s->set_prio[0]; 1147 ps->set_prio[1] = s->set_prio[1]; 1148 } 1149 1150 int 1151 pfctl_get_states(int dev, struct pfctl_states *states) 1152 { 1153 struct pfioc_states_v2 ps; 1154 struct pf_state_export *p; 1155 char *inbuf = NULL, *newinbuf = NULL; 1156 unsigned int len = 0; 1157 int i, error; 1158 1159 bzero(&ps, sizeof(ps)); 1160 ps.ps_req_version = PF_STATE_VERSION; 1161 1162 bzero(states, sizeof(*states)); 1163 TAILQ_INIT(&states->states); 1164 1165 for (;;) { 1166 ps.ps_len = len; 1167 if (len) { 1168 newinbuf = realloc(inbuf, len); 1169 if (newinbuf == NULL) 1170 return (ENOMEM); 1171 ps.ps_buf = inbuf = newinbuf; 1172 } 1173 if ((error = ioctl(dev, DIOCGETSTATESV2, &ps)) < 0) { 1174 free(inbuf); 1175 return (error); 1176 } 1177 if (ps.ps_len + sizeof(struct pfioc_states_v2) < len) 1178 break; 1179 if (len == 0 && ps.ps_len == 0) 1180 goto out; 1181 if (len == 0 && ps.ps_len != 0) 1182 len = ps.ps_len; 1183 if (ps.ps_len == 0) 1184 goto out; /* no states */ 1185 len *= 2; 1186 } 1187 p = ps.ps_states; 1188 1189 for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) { 1190 struct pfctl_state *s = malloc(sizeof(*s)); 1191 if (s == NULL) { 1192 pfctl_free_states(states); 1193 error = ENOMEM; 1194 goto out; 1195 } 1196 1197 pf_state_export_to_state(s, p); 1198 TAILQ_INSERT_TAIL(&states->states, s, entry); 1199 } 1200 1201 out: 1202 free(inbuf); 1203 return (error); 1204 } 1205 1206 void 1207 pfctl_free_states(struct pfctl_states *states) 1208 { 1209 struct pfctl_state *s, *tmp; 1210 1211 TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) { 1212 free(s); 1213 } 1214 1215 bzero(states, sizeof(*states)); 1216 } 1217 1218 static int 1219 _pfctl_clear_states(int dev, const struct pfctl_kill *kill, 1220 unsigned int *killed, uint64_t ioctlval) 1221 { 1222 nvlist_t *nvl; 1223 int ret; 1224 1225 nvl = nvlist_create(0); 1226 1227 pfctl_nv_add_state_cmp(nvl, "cmp", &kill->cmp); 1228 nvlist_add_number(nvl, "af", kill->af); 1229 nvlist_add_number(nvl, "proto", kill->proto); 1230 pfctl_nv_add_rule_addr(nvl, "src", &kill->src); 1231 pfctl_nv_add_rule_addr(nvl, "dst", &kill->dst); 1232 pfctl_nv_add_rule_addr(nvl, "rt_addr", &kill->rt_addr); 1233 nvlist_add_string(nvl, "ifname", kill->ifname); 1234 nvlist_add_string(nvl, "label", kill->label); 1235 nvlist_add_bool(nvl, "kill_match", kill->kill_match); 1236 1237 if ((ret = pfctl_do_ioctl(dev, ioctlval, 1024, &nvl)) != 0) 1238 return (ret); 1239 1240 if (killed) 1241 *killed = nvlist_get_number(nvl, "killed"); 1242 1243 nvlist_destroy(nvl); 1244 1245 return (ret); 1246 } 1247 1248 int 1249 pfctl_clear_states(int dev, const struct pfctl_kill *kill, 1250 unsigned int *killed) 1251 { 1252 return (_pfctl_clear_states(dev, kill, killed, DIOCCLRSTATESNV)); 1253 } 1254 1255 int 1256 pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed) 1257 { 1258 return (_pfctl_clear_states(dev, kill, killed, DIOCKILLSTATESNV)); 1259 } 1260 1261 int 1262 pfctl_clear_rules(int dev, const char *anchorname) 1263 { 1264 struct pfioc_trans trans; 1265 struct pfioc_trans_e transe[2]; 1266 int ret; 1267 1268 bzero(&trans, sizeof(trans)); 1269 bzero(&transe, sizeof(transe)); 1270 1271 transe[0].rs_num = PF_RULESET_SCRUB; 1272 if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) 1273 >= sizeof(transe[0].anchor)) 1274 return (E2BIG); 1275 1276 transe[1].rs_num = PF_RULESET_FILTER; 1277 if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) 1278 >= sizeof(transe[1].anchor)) 1279 return (E2BIG); 1280 1281 trans.size = 2; 1282 trans.esize = sizeof(transe[0]); 1283 trans.array = transe; 1284 1285 ret = ioctl(dev, DIOCXBEGIN, &trans); 1286 if (ret != 0) 1287 return (ret); 1288 return ioctl(dev, DIOCXCOMMIT, &trans); 1289 } 1290 1291 int 1292 pfctl_clear_nat(int dev, const char *anchorname) 1293 { 1294 struct pfioc_trans trans; 1295 struct pfioc_trans_e transe[3]; 1296 int ret; 1297 1298 bzero(&trans, sizeof(trans)); 1299 bzero(&transe, sizeof(transe)); 1300 1301 transe[0].rs_num = PF_RULESET_NAT; 1302 if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) 1303 >= sizeof(transe[0].anchor)) 1304 return (E2BIG); 1305 1306 transe[1].rs_num = PF_RULESET_BINAT; 1307 if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) 1308 >= sizeof(transe[0].anchor)) 1309 return (E2BIG); 1310 1311 transe[2].rs_num = PF_RULESET_RDR; 1312 if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor)) 1313 >= sizeof(transe[2].anchor)) 1314 return (E2BIG); 1315 1316 trans.size = 3; 1317 trans.esize = sizeof(transe[0]); 1318 trans.array = transe; 1319 1320 ret = ioctl(dev, DIOCXBEGIN, &trans); 1321 if (ret != 0) 1322 return (ret); 1323 return ioctl(dev, DIOCXCOMMIT, &trans); 1324 } 1325 int 1326 pfctl_clear_eth_rules(int dev, const char *anchorname) 1327 { 1328 struct pfioc_trans trans; 1329 struct pfioc_trans_e transe; 1330 int ret; 1331 1332 bzero(&trans, sizeof(trans)); 1333 bzero(&transe, sizeof(transe)); 1334 1335 transe.rs_num = PF_RULESET_ETH; 1336 if (strlcpy(transe.anchor, anchorname, sizeof(transe.anchor)) 1337 >= sizeof(transe.anchor)) 1338 return (E2BIG); 1339 1340 trans.size = 1; 1341 trans.esize = sizeof(transe); 1342 trans.array = &transe; 1343 1344 ret = ioctl(dev, DIOCXBEGIN, &trans); 1345 if (ret != 0) 1346 return (ret); 1347 return ioctl(dev, DIOCXCOMMIT, &trans); 1348 } 1349 1350 static int 1351 pfctl_get_limit(int dev, const int index, uint *limit) 1352 { 1353 struct pfioc_limit pl; 1354 1355 bzero(&pl, sizeof(pl)); 1356 pl.index = index; 1357 1358 if (ioctl(dev, DIOCGETLIMIT, &pl) == -1) 1359 return (errno); 1360 1361 *limit = pl.limit; 1362 1363 return (0); 1364 } 1365 1366 int 1367 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s) 1368 { 1369 struct pfioc_nv nv; 1370 nvlist_t *nvl; 1371 int ret; 1372 uint state_limit; 1373 uint64_t lim, hi, lo; 1374 1375 ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit); 1376 if (ret != 0) 1377 return (ret); 1378 1379 lim = state_limit; 1380 hi = lim * s->highwater / 100; 1381 lo = lim * s->lowwater / 100; 1382 1383 if (lo == hi) 1384 hi++; 1385 1386 nvl = nvlist_create(0); 1387 1388 nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER); 1389 nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE); 1390 nvlist_add_number(nvl, "highwater", hi); 1391 nvlist_add_number(nvl, "lowwater", lo); 1392 1393 nv.data = nvlist_pack(nvl, &nv.len); 1394 nv.size = nv.len; 1395 nvlist_destroy(nvl); 1396 nvl = NULL; 1397 1398 ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv); 1399 1400 free(nv.data); 1401 return (ret); 1402 } 1403 1404 int 1405 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s) 1406 { 1407 nvlist_t *nvl; 1408 int ret; 1409 uint state_limit; 1410 bool enabled, adaptive; 1411 1412 ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit); 1413 if (ret != 0) 1414 return (ret); 1415 1416 bzero(s, sizeof(*s)); 1417 1418 nvl = nvlist_create(0); 1419 1420 if ((ret = pfctl_do_ioctl(dev, DIOCGETSYNCOOKIES, 256, &nvl)) != 0) 1421 return (errno); 1422 1423 enabled = nvlist_get_bool(nvl, "enabled"); 1424 adaptive = nvlist_get_bool(nvl, "adaptive"); 1425 1426 if (enabled) { 1427 if (adaptive) 1428 s->mode = PFCTL_SYNCOOKIES_ADAPTIVE; 1429 else 1430 s->mode = PFCTL_SYNCOOKIES_ALWAYS; 1431 } else { 1432 s->mode = PFCTL_SYNCOOKIES_NEVER; 1433 } 1434 1435 s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit; 1436 s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit; 1437 1438 nvlist_destroy(nvl); 1439 1440 return (0); 1441 } 1442 1443 int 1444 pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 1445 *addr, int size, int *nadd, int flags) 1446 { 1447 struct pfioc_table io; 1448 1449 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 1450 return (EINVAL); 1451 } 1452 bzero(&io, sizeof io); 1453 io.pfrio_flags = flags; 1454 io.pfrio_table = *tbl; 1455 io.pfrio_buffer = addr; 1456 io.pfrio_esize = sizeof(*addr); 1457 io.pfrio_size = size; 1458 1459 if (ioctl(dev, DIOCRADDADDRS, &io)) 1460 return (errno); 1461 if (nadd != NULL) 1462 *nadd = io.pfrio_nadd; 1463 return (0); 1464 } 1465 1466 int 1467 pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 1468 *addr, int size, int *ndel, int flags) 1469 { 1470 struct pfioc_table io; 1471 1472 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 1473 return (EINVAL); 1474 } 1475 bzero(&io, sizeof io); 1476 io.pfrio_flags = flags; 1477 io.pfrio_table = *tbl; 1478 io.pfrio_buffer = addr; 1479 io.pfrio_esize = sizeof(*addr); 1480 io.pfrio_size = size; 1481 1482 if (ioctl(dev, DIOCRDELADDRS, &io)) 1483 return (errno); 1484 if (ndel != NULL) 1485 *ndel = io.pfrio_ndel; 1486 return (0); 1487 } 1488 1489 int 1490 pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 1491 *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags) 1492 { 1493 struct pfioc_table io; 1494 1495 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 1496 return (EINVAL); 1497 } 1498 bzero(&io, sizeof io); 1499 io.pfrio_flags = flags; 1500 io.pfrio_table = *tbl; 1501 io.pfrio_buffer = addr; 1502 io.pfrio_esize = sizeof(*addr); 1503 io.pfrio_size = size; 1504 io.pfrio_size2 = (size2 != NULL) ? *size2 : 0; 1505 if (ioctl(dev, DIOCRSETADDRS, &io)) 1506 return (-1); 1507 if (nadd != NULL) 1508 *nadd = io.pfrio_nadd; 1509 if (ndel != NULL) 1510 *ndel = io.pfrio_ndel; 1511 if (nchange != NULL) 1512 *nchange = io.pfrio_nchange; 1513 if (size2 != NULL) 1514 *size2 = io.pfrio_size2; 1515 return (0); 1516 } 1517 1518 int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr, 1519 int *size, int flags) 1520 { 1521 struct pfioc_table io; 1522 1523 if (tbl == NULL || size == NULL || *size < 0 || 1524 (*size && addr == NULL)) { 1525 return (EINVAL); 1526 } 1527 bzero(&io, sizeof io); 1528 io.pfrio_flags = flags; 1529 io.pfrio_table = *tbl; 1530 io.pfrio_buffer = addr; 1531 io.pfrio_esize = sizeof(*addr); 1532 io.pfrio_size = *size; 1533 if (ioctl(dev, DIOCRGETADDRS, &io)) 1534 return (-1); 1535 *size = io.pfrio_size; 1536 return (0); 1537 } 1538