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