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