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 * $FreeBSD$ 32 */ 33 34 #include <sys/cdefs.h> 35 36 #include <sys/ioctl.h> 37 #include <sys/nv.h> 38 #include <sys/queue.h> 39 #include <sys/types.h> 40 41 #include <net/if.h> 42 #include <net/pfvar.h> 43 #include <netinet/in.h> 44 45 #include <assert.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "libpfctl.h" 52 53 const char* PFCTL_SYNCOOKIES_MODE_NAMES[] = { 54 "never", 55 "always", 56 "adaptive" 57 }; 58 59 static int _pfctl_clear_states(int , const struct pfctl_kill *, 60 unsigned int *, uint64_t); 61 62 static int 63 pfctl_do_ioctl(int dev, uint cmd, size_t size, nvlist_t **nvl) 64 { 65 struct pfioc_nv nv; 66 void *data; 67 size_t nvlen; 68 int ret; 69 70 data = nvlist_pack(*nvl, &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 pf_state_key_export_to_state_key(&ps->key[0], &s->key[0]); 1115 pf_state_key_export_to_state_key(&ps->key[1], &s->key[1]); 1116 pf_state_peer_export_to_state_peer(&ps->src, &s->src); 1117 pf_state_peer_export_to_state_peer(&ps->dst, &s->dst); 1118 bcopy(&s->rt_addr, &ps->rt_addr, sizeof(ps->rt_addr)); 1119 ps->rule = ntohl(s->rule); 1120 ps->anchor = ntohl(s->anchor); 1121 ps->nat_rule = ntohl(s->nat_rule); 1122 ps->creation = ntohl(s->creation); 1123 ps->expire = ntohl(s->expire); 1124 ps->packets[0] = s->packets[0]; 1125 ps->packets[1] = s->packets[1]; 1126 ps->bytes[0] = s->bytes[0]; 1127 ps->bytes[1] = s->bytes[1]; 1128 ps->creatorid = ntohl(s->creatorid); 1129 ps->key[0].proto = s->proto; 1130 ps->key[1].proto = s->proto; 1131 ps->key[0].af = s->af; 1132 ps->key[1].af = s->af; 1133 ps->direction = s->direction; 1134 ps->state_flags = s->state_flags; 1135 ps->sync_flags = s->sync_flags; 1136 } 1137 1138 int 1139 pfctl_get_states(int dev, struct pfctl_states *states) 1140 { 1141 struct pfioc_states_v2 ps; 1142 struct pf_state_export *p; 1143 char *inbuf = NULL, *newinbuf = NULL; 1144 unsigned int len = 0; 1145 int i, error; 1146 1147 bzero(&ps, sizeof(ps)); 1148 ps.ps_req_version = PF_STATE_VERSION; 1149 1150 bzero(states, sizeof(*states)); 1151 TAILQ_INIT(&states->states); 1152 1153 for (;;) { 1154 ps.ps_len = len; 1155 if (len) { 1156 newinbuf = realloc(inbuf, len); 1157 if (newinbuf == NULL) 1158 return (ENOMEM); 1159 ps.ps_buf = inbuf = newinbuf; 1160 } 1161 if ((error = ioctl(dev, DIOCGETSTATESV2, &ps)) < 0) { 1162 free(inbuf); 1163 return (error); 1164 } 1165 if (ps.ps_len + sizeof(struct pfioc_states_v2) < len) 1166 break; 1167 if (len == 0 && ps.ps_len == 0) 1168 goto out; 1169 if (len == 0 && ps.ps_len != 0) 1170 len = ps.ps_len; 1171 if (ps.ps_len == 0) 1172 goto out; /* no states */ 1173 len *= 2; 1174 } 1175 p = ps.ps_states; 1176 1177 for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) { 1178 struct pfctl_state *s = malloc(sizeof(*s)); 1179 if (s == NULL) { 1180 pfctl_free_states(states); 1181 error = ENOMEM; 1182 goto out; 1183 } 1184 1185 pf_state_export_to_state(s, p); 1186 TAILQ_INSERT_TAIL(&states->states, s, entry); 1187 } 1188 1189 out: 1190 free(inbuf); 1191 return (error); 1192 } 1193 1194 void 1195 pfctl_free_states(struct pfctl_states *states) 1196 { 1197 struct pfctl_state *s, *tmp; 1198 1199 TAILQ_FOREACH_SAFE(s, &states->states, entry, tmp) { 1200 free(s); 1201 } 1202 1203 bzero(states, sizeof(*states)); 1204 } 1205 1206 static int 1207 _pfctl_clear_states(int dev, const struct pfctl_kill *kill, 1208 unsigned int *killed, uint64_t ioctlval) 1209 { 1210 nvlist_t *nvl; 1211 int ret; 1212 1213 nvl = nvlist_create(0); 1214 1215 pfctl_nv_add_state_cmp(nvl, "cmp", &kill->cmp); 1216 nvlist_add_number(nvl, "af", kill->af); 1217 nvlist_add_number(nvl, "proto", kill->proto); 1218 pfctl_nv_add_rule_addr(nvl, "src", &kill->src); 1219 pfctl_nv_add_rule_addr(nvl, "dst", &kill->dst); 1220 pfctl_nv_add_rule_addr(nvl, "rt_addr", &kill->rt_addr); 1221 nvlist_add_string(nvl, "ifname", kill->ifname); 1222 nvlist_add_string(nvl, "label", kill->label); 1223 nvlist_add_bool(nvl, "kill_match", kill->kill_match); 1224 1225 if ((ret = pfctl_do_ioctl(dev, ioctlval, 1024, &nvl)) != 0) 1226 return (ret); 1227 1228 if (killed) 1229 *killed = nvlist_get_number(nvl, "killed"); 1230 1231 nvlist_destroy(nvl); 1232 1233 return (ret); 1234 } 1235 1236 int 1237 pfctl_clear_states(int dev, const struct pfctl_kill *kill, 1238 unsigned int *killed) 1239 { 1240 return (_pfctl_clear_states(dev, kill, killed, DIOCCLRSTATESNV)); 1241 } 1242 1243 int 1244 pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed) 1245 { 1246 return (_pfctl_clear_states(dev, kill, killed, DIOCKILLSTATESNV)); 1247 } 1248 1249 int 1250 pfctl_clear_rules(int dev, const char *anchorname) 1251 { 1252 struct pfioc_trans trans; 1253 struct pfioc_trans_e transe[2]; 1254 int ret; 1255 1256 bzero(&trans, sizeof(trans)); 1257 bzero(&transe, sizeof(transe)); 1258 1259 transe[0].rs_num = PF_RULESET_SCRUB; 1260 if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) 1261 >= sizeof(transe[0].anchor)) 1262 return (E2BIG); 1263 1264 transe[1].rs_num = PF_RULESET_FILTER; 1265 if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) 1266 >= sizeof(transe[1].anchor)) 1267 return (E2BIG); 1268 1269 trans.size = 2; 1270 trans.esize = sizeof(transe[0]); 1271 trans.array = transe; 1272 1273 ret = ioctl(dev, DIOCXBEGIN, &trans); 1274 if (ret != 0) 1275 return (ret); 1276 return ioctl(dev, DIOCXCOMMIT, &trans); 1277 } 1278 1279 int 1280 pfctl_clear_nat(int dev, const char *anchorname) 1281 { 1282 struct pfioc_trans trans; 1283 struct pfioc_trans_e transe[3]; 1284 int ret; 1285 1286 bzero(&trans, sizeof(trans)); 1287 bzero(&transe, sizeof(transe)); 1288 1289 transe[0].rs_num = PF_RULESET_NAT; 1290 if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) 1291 >= sizeof(transe[0].anchor)) 1292 return (E2BIG); 1293 1294 transe[1].rs_num = PF_RULESET_BINAT; 1295 if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) 1296 >= sizeof(transe[0].anchor)) 1297 return (E2BIG); 1298 1299 transe[2].rs_num = PF_RULESET_RDR; 1300 if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor)) 1301 >= sizeof(transe[2].anchor)) 1302 return (E2BIG); 1303 1304 trans.size = 3; 1305 trans.esize = sizeof(transe[0]); 1306 trans.array = transe; 1307 1308 ret = ioctl(dev, DIOCXBEGIN, &trans); 1309 if (ret != 0) 1310 return (ret); 1311 return ioctl(dev, DIOCXCOMMIT, &trans); 1312 } 1313 int 1314 pfctl_clear_eth_rules(int dev, const char *anchorname) 1315 { 1316 struct pfioc_trans trans; 1317 struct pfioc_trans_e transe; 1318 int ret; 1319 1320 bzero(&trans, sizeof(trans)); 1321 bzero(&transe, sizeof(transe)); 1322 1323 transe.rs_num = PF_RULESET_ETH; 1324 if (strlcpy(transe.anchor, anchorname, sizeof(transe.anchor)) 1325 >= sizeof(transe.anchor)) 1326 return (E2BIG); 1327 1328 trans.size = 1; 1329 trans.esize = sizeof(transe); 1330 trans.array = &transe; 1331 1332 ret = ioctl(dev, DIOCXBEGIN, &trans); 1333 if (ret != 0) 1334 return (ret); 1335 return ioctl(dev, DIOCXCOMMIT, &trans); 1336 } 1337 1338 static int 1339 pfctl_get_limit(int dev, const int index, uint *limit) 1340 { 1341 struct pfioc_limit pl; 1342 1343 bzero(&pl, sizeof(pl)); 1344 pl.index = index; 1345 1346 if (ioctl(dev, DIOCGETLIMIT, &pl) == -1) 1347 return (errno); 1348 1349 *limit = pl.limit; 1350 1351 return (0); 1352 } 1353 1354 int 1355 pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s) 1356 { 1357 struct pfioc_nv nv; 1358 nvlist_t *nvl; 1359 int ret; 1360 uint state_limit; 1361 uint64_t lim, hi, lo; 1362 1363 ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit); 1364 if (ret != 0) 1365 return (ret); 1366 1367 lim = state_limit; 1368 hi = lim * s->highwater / 100; 1369 lo = lim * s->lowwater / 100; 1370 1371 if (lo == hi) 1372 hi++; 1373 1374 nvl = nvlist_create(0); 1375 1376 nvlist_add_bool(nvl, "enabled", s->mode != PFCTL_SYNCOOKIES_NEVER); 1377 nvlist_add_bool(nvl, "adaptive", s->mode == PFCTL_SYNCOOKIES_ADAPTIVE); 1378 nvlist_add_number(nvl, "highwater", hi); 1379 nvlist_add_number(nvl, "lowwater", lo); 1380 1381 nv.data = nvlist_pack(nvl, &nv.len); 1382 nv.size = nv.len; 1383 nvlist_destroy(nvl); 1384 nvl = NULL; 1385 1386 ret = ioctl(dev, DIOCSETSYNCOOKIES, &nv); 1387 1388 free(nv.data); 1389 return (ret); 1390 } 1391 1392 int 1393 pfctl_get_syncookies(int dev, struct pfctl_syncookies *s) 1394 { 1395 nvlist_t *nvl; 1396 int ret; 1397 uint state_limit; 1398 bool enabled, adaptive; 1399 1400 ret = pfctl_get_limit(dev, PF_LIMIT_STATES, &state_limit); 1401 if (ret != 0) 1402 return (ret); 1403 1404 bzero(s, sizeof(*s)); 1405 1406 nvl = nvlist_create(0); 1407 1408 if ((ret = pfctl_do_ioctl(dev, DIOCGETSYNCOOKIES, 256, &nvl)) != 0) 1409 return (errno); 1410 1411 enabled = nvlist_get_bool(nvl, "enabled"); 1412 adaptive = nvlist_get_bool(nvl, "adaptive"); 1413 1414 if (enabled) { 1415 if (adaptive) 1416 s->mode = PFCTL_SYNCOOKIES_ADAPTIVE; 1417 else 1418 s->mode = PFCTL_SYNCOOKIES_ALWAYS; 1419 } else { 1420 s->mode = PFCTL_SYNCOOKIES_NEVER; 1421 } 1422 1423 s->highwater = nvlist_get_number(nvl, "highwater") * 100 / state_limit; 1424 s->lowwater = nvlist_get_number(nvl, "lowwater") * 100 / state_limit; 1425 1426 nvlist_destroy(nvl); 1427 1428 return (0); 1429 } 1430 1431 int 1432 pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 1433 *addr, int size, int *nadd, int flags) 1434 { 1435 struct pfioc_table io; 1436 1437 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 1438 return (EINVAL); 1439 } 1440 bzero(&io, sizeof io); 1441 io.pfrio_flags = flags; 1442 io.pfrio_table = *tbl; 1443 io.pfrio_buffer = addr; 1444 io.pfrio_esize = sizeof(*addr); 1445 io.pfrio_size = size; 1446 1447 if (ioctl(dev, DIOCRADDADDRS, &io)) 1448 return (errno); 1449 if (nadd != NULL) 1450 *nadd = io.pfrio_nadd; 1451 return (0); 1452 } 1453 1454 int 1455 pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 1456 *addr, int size, int *ndel, int flags) 1457 { 1458 struct pfioc_table io; 1459 1460 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 1461 return (EINVAL); 1462 } 1463 bzero(&io, sizeof io); 1464 io.pfrio_flags = flags; 1465 io.pfrio_table = *tbl; 1466 io.pfrio_buffer = addr; 1467 io.pfrio_esize = sizeof(*addr); 1468 io.pfrio_size = size; 1469 1470 if (ioctl(dev, DIOCRDELADDRS, &io)) 1471 return (errno); 1472 if (ndel != NULL) 1473 *ndel = io.pfrio_ndel; 1474 return (0); 1475 } 1476 1477 int 1478 pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr 1479 *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags) 1480 { 1481 struct pfioc_table io; 1482 1483 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 1484 return (EINVAL); 1485 } 1486 bzero(&io, sizeof io); 1487 io.pfrio_flags = flags; 1488 io.pfrio_table = *tbl; 1489 io.pfrio_buffer = addr; 1490 io.pfrio_esize = sizeof(*addr); 1491 io.pfrio_size = size; 1492 io.pfrio_size2 = (size2 != NULL) ? *size2 : 0; 1493 if (ioctl(dev, DIOCRSETADDRS, &io)) 1494 return (-1); 1495 if (nadd != NULL) 1496 *nadd = io.pfrio_nadd; 1497 if (ndel != NULL) 1498 *ndel = io.pfrio_ndel; 1499 if (nchange != NULL) 1500 *nchange = io.pfrio_nchange; 1501 if (size2 != NULL) 1502 *size2 = io.pfrio_size2; 1503 return (0); 1504 } 1505 1506 int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr, 1507 int *size, int flags) 1508 { 1509 struct pfioc_table io; 1510 1511 if (tbl == NULL || size == NULL || *size < 0 || 1512 (*size && addr == NULL)) { 1513 return (EINVAL); 1514 } 1515 bzero(&io, sizeof io); 1516 io.pfrio_flags = flags; 1517 io.pfrio_table = *tbl; 1518 io.pfrio_buffer = addr; 1519 io.pfrio_esize = sizeof(*addr); 1520 io.pfrio_size = *size; 1521 if (ioctl(dev, DIOCRGETADDRS, &io)) 1522 return (-1); 1523 *size = io.pfrio_size; 1524 return (0); 1525 } 1526