1 /* $NetBSD: npf.c,v 1.12 2012/08/15 18:44:56 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 2010-2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This material is based upon work partially supported by The 8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.12 2012/08/15 18:44:56 rmind Exp $"); 34 35 #include <sys/types.h> 36 #include <netinet/in_systm.h> 37 #include <netinet/in.h> 38 #include <net/if.h> 39 #include <prop/proplib.h> 40 41 #include <stdlib.h> 42 #include <string.h> 43 #include <assert.h> 44 #include <errno.h> 45 #include <err.h> 46 47 #define _NPF_PRIVATE 48 #include "npf.h" 49 50 struct nl_config { 51 /* Rules, translations, tables, procedures. */ 52 prop_dictionary_t ncf_dict; 53 prop_array_t ncf_rules_list; 54 prop_array_t ncf_rproc_list; 55 prop_array_t ncf_table_list; 56 prop_array_t ncf_nat_list; 57 /* Priority counters. */ 58 pri_t ncf_rule_pri; 59 pri_t ncf_nat_pri; 60 /* Debug information. */ 61 prop_dictionary_t ncf_debug; 62 /* Error report. */ 63 prop_dictionary_t ncf_err; 64 /* Custom file to externalise property-list. */ 65 const char * ncf_plist; 66 bool ncf_flush; 67 }; 68 69 struct nl_rule { 70 prop_dictionary_t nrl_dict; 71 }; 72 73 struct nl_rproc { 74 prop_dictionary_t nrp_dict; 75 }; 76 77 struct nl_table { 78 prop_dictionary_t ntl_dict; 79 }; 80 81 /* 82 * CONFIGURATION INTERFACE. 83 */ 84 85 nl_config_t * 86 npf_config_create(void) 87 { 88 nl_config_t *ncf; 89 90 ncf = calloc(1, sizeof(*ncf)); 91 if (ncf == NULL) { 92 return NULL; 93 } 94 ncf->ncf_rules_list = prop_array_create(); 95 ncf->ncf_rproc_list = prop_array_create(); 96 ncf->ncf_table_list = prop_array_create(); 97 ncf->ncf_nat_list = prop_array_create(); 98 99 ncf->ncf_rule_pri = 1; 100 ncf->ncf_nat_pri = 1; 101 102 ncf->ncf_plist = NULL; 103 ncf->ncf_flush = false; 104 105 return ncf; 106 } 107 108 int 109 npf_config_submit(nl_config_t *ncf, int fd) 110 { 111 const char *plist = ncf->ncf_plist; 112 prop_dictionary_t npf_dict; 113 int error = 0; 114 115 npf_dict = prop_dictionary_create(); 116 if (npf_dict == NULL) { 117 return ENOMEM; 118 } 119 if (ncf->ncf_debug) { 120 prop_dictionary_set(npf_dict, "debug", ncf->ncf_debug); 121 } 122 prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list); 123 prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list); 124 prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list); 125 prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list); 126 prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush); 127 128 if (plist) { 129 if (!prop_dictionary_externalize_to_file(npf_dict, plist)) { 130 error = errno; 131 } 132 prop_object_release(npf_dict); 133 return error; 134 } 135 136 error = prop_dictionary_sendrecv_ioctl(npf_dict, fd, 137 IOC_NPF_RELOAD, &ncf->ncf_err); 138 if (error) { 139 prop_object_release(npf_dict); 140 assert(ncf->ncf_err == NULL); 141 return error; 142 } 143 144 prop_dictionary_get_int32(ncf->ncf_err, "errno", &error); 145 prop_object_release(npf_dict); 146 return error; 147 } 148 149 nl_config_t * 150 npf_config_retrieve(int fd, bool *active, bool *loaded) 151 { 152 prop_dictionary_t npf_dict; 153 nl_config_t *ncf; 154 int error; 155 156 error = prop_dictionary_recv_ioctl(fd, IOC_NPF_GETCONF, &npf_dict); 157 if (error) { 158 return NULL; 159 } 160 ncf = calloc(1, sizeof(*ncf)); 161 if (ncf == NULL) { 162 prop_object_release(npf_dict); 163 return NULL; 164 } 165 ncf->ncf_dict = npf_dict; 166 ncf->ncf_rules_list = prop_dictionary_get(npf_dict, "rules"); 167 ncf->ncf_rproc_list = prop_dictionary_get(npf_dict, "rprocs"); 168 ncf->ncf_table_list = prop_dictionary_get(npf_dict, "tables"); 169 ncf->ncf_nat_list = prop_dictionary_get(npf_dict, "translation"); 170 171 prop_dictionary_get_bool(npf_dict, "active", active); 172 *loaded = (ncf->ncf_rules_list != NULL); 173 return ncf; 174 } 175 176 int 177 npf_config_flush(int fd) 178 { 179 nl_config_t *ncf; 180 int error; 181 182 ncf = npf_config_create(); 183 if (ncf == NULL) { 184 return ENOMEM; 185 } 186 ncf->ncf_flush = true; 187 error = npf_config_submit(ncf, fd); 188 npf_config_destroy(ncf); 189 return error; 190 } 191 192 void 193 _npf_config_error(nl_config_t *ncf, nl_error_t *ne) 194 { 195 memset(ne, 0, sizeof(*ne)); 196 prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id); 197 prop_dictionary_get_cstring(ncf->ncf_err, 198 "source-file", &ne->ne_source_file); 199 prop_dictionary_get_uint32(ncf->ncf_err, 200 "source-line", &ne->ne_source_line); 201 prop_dictionary_get_int32(ncf->ncf_err, 202 "ncode-error", &ne->ne_ncode_error); 203 prop_dictionary_get_int32(ncf->ncf_err, 204 "ncode-errat", &ne->ne_ncode_errat); 205 } 206 207 void 208 npf_config_destroy(nl_config_t *ncf) 209 { 210 211 if (ncf->ncf_dict == NULL) { 212 prop_object_release(ncf->ncf_rules_list); 213 prop_object_release(ncf->ncf_rproc_list); 214 prop_object_release(ncf->ncf_table_list); 215 prop_object_release(ncf->ncf_nat_list); 216 } else { 217 prop_object_release(ncf->ncf_dict); 218 } 219 if (ncf->ncf_err) { 220 prop_object_release(ncf->ncf_err); 221 } 222 if (ncf->ncf_debug) { 223 prop_object_release(ncf->ncf_debug); 224 } 225 free(ncf); 226 } 227 228 void 229 _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file) 230 { 231 232 ncf->ncf_plist = plist_file; 233 } 234 235 static bool 236 _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name) 237 { 238 prop_dictionary_t dict; 239 prop_object_iterator_t it; 240 241 it = prop_array_iterator(array); 242 while ((dict = prop_object_iterator_next(it)) != NULL) { 243 const char *lname; 244 prop_dictionary_get_cstring_nocopy(dict, key, &lname); 245 if (strcmp(name, lname) == 0) 246 break; 247 } 248 prop_object_iterator_release(it); 249 return dict ? true : false; 250 } 251 252 /* 253 * RULE INTERFACE. 254 */ 255 256 nl_rule_t * 257 npf_rule_create(const char *name, uint32_t attr, u_int if_idx) 258 { 259 prop_dictionary_t rldict; 260 nl_rule_t *rl; 261 262 rl = malloc(sizeof(*rl)); 263 if (rl == NULL) { 264 return NULL; 265 } 266 rldict = prop_dictionary_create(); 267 if (rldict == NULL) { 268 free(rl); 269 return NULL; 270 } 271 if (name) { 272 prop_dictionary_set_cstring(rldict, "name", name); 273 } 274 prop_dictionary_set_uint32(rldict, "attributes", attr); 275 276 if (if_idx) { 277 prop_dictionary_set_uint32(rldict, "interface", if_idx); 278 } 279 rl->nrl_dict = rldict; 280 return rl; 281 } 282 283 int 284 npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t sz) 285 { 286 prop_dictionary_t rldict = rl->nrl_dict; 287 prop_data_t cdata; 288 289 if (type != NPF_CODE_NCODE) { 290 return ENOTSUP; 291 } 292 cdata = prop_data_create_data(code, sz); 293 if (cdata == NULL) { 294 return ENOMEM; 295 } 296 prop_dictionary_set(rldict, "ncode", cdata); 297 prop_object_release(cdata); 298 return 0; 299 } 300 301 int 302 npf_rule_setproc(nl_config_t *ncf, nl_rule_t *rl, const char *name) 303 { 304 prop_dictionary_t rldict = rl->nrl_dict; 305 306 if (!npf_rproc_exists_p(ncf, name)) { 307 return ENOENT; 308 } 309 prop_dictionary_set_cstring(rldict, "rproc", name); 310 return 0; 311 } 312 313 bool 314 npf_rule_exists_p(nl_config_t *ncf, const char *name) 315 { 316 317 return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name); 318 } 319 320 int 321 npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl, pri_t pri) 322 { 323 prop_dictionary_t rldict = rl->nrl_dict; 324 prop_array_t rlset; 325 326 if (pri == NPF_PRI_NEXT) { 327 pri = ncf->ncf_rule_pri++; 328 } else if (ncf) { 329 ncf->ncf_rule_pri = pri + 1; 330 } 331 prop_dictionary_set_int32(rldict, "priority", pri); 332 333 if (parent) { 334 prop_dictionary_t pdict = parent->nrl_dict; 335 rlset = prop_dictionary_get(pdict, "subrules"); 336 if (rlset == NULL) { 337 rlset = prop_array_create(); 338 prop_dictionary_set(pdict, "subrules", rlset); 339 prop_object_release(rlset); 340 } 341 } else { 342 rlset = ncf->ncf_rules_list; 343 } 344 prop_array_add(rlset, rldict); 345 return 0; 346 } 347 348 static int 349 _npf_rule_foreach1(prop_array_t rules, unsigned nlevel, nl_rule_callback_t func) 350 { 351 prop_dictionary_t rldict; 352 prop_object_iterator_t it; 353 354 if (!rules || prop_object_type(rules) != PROP_TYPE_ARRAY) { 355 return ENOENT; 356 } 357 it = prop_array_iterator(rules); 358 if (it == NULL) { 359 return ENOMEM; 360 } 361 while ((rldict = prop_object_iterator_next(it)) != NULL) { 362 prop_array_t subrules; 363 nl_rule_t nrl; 364 365 nrl.nrl_dict = rldict; 366 (*func)(&nrl, nlevel); 367 368 subrules = prop_dictionary_get(rldict, "subrules"); 369 (void)_npf_rule_foreach1(subrules, nlevel + 1, func); 370 } 371 prop_object_iterator_release(it); 372 return 0; 373 } 374 375 int 376 _npf_rule_foreach(nl_config_t *ncf, nl_rule_callback_t func) 377 { 378 379 return _npf_rule_foreach1(ncf->ncf_rules_list, 0, func); 380 } 381 382 pri_t 383 _npf_rule_getinfo(nl_rule_t *nrl, const char **rname, uint32_t *attr, 384 u_int *if_idx) 385 { 386 prop_dictionary_t rldict = nrl->nrl_dict; 387 pri_t prio; 388 389 prop_dictionary_get_cstring_nocopy(rldict, "name", rname); 390 prop_dictionary_get_uint32(rldict, "attributes", attr); 391 prop_dictionary_get_int32(rldict, "priority", &prio); 392 prop_dictionary_get_uint32(rldict, "interface", if_idx); 393 return prio; 394 } 395 396 const void * 397 _npf_rule_ncode(nl_rule_t *nrl, size_t *size) 398 { 399 prop_dictionary_t rldict = nrl->nrl_dict; 400 prop_object_t obj = prop_dictionary_get(rldict, "ncode"); 401 *size = prop_data_size(obj); 402 return prop_data_data_nocopy(obj); 403 } 404 405 const char * 406 _npf_rule_rproc(nl_rule_t *nrl) 407 { 408 prop_dictionary_t rldict = nrl->nrl_dict; 409 const char *rpname = NULL; 410 411 prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rpname); 412 return rpname; 413 } 414 415 void 416 npf_rule_destroy(nl_rule_t *rl) 417 { 418 419 prop_object_release(rl->nrl_dict); 420 free(rl); 421 } 422 423 /* 424 * RULE PROCEDURE INTERFACE. 425 */ 426 427 nl_rproc_t * 428 npf_rproc_create(const char *name) 429 { 430 prop_dictionary_t rpdict; 431 nl_rproc_t *nrp; 432 433 nrp = malloc(sizeof(nl_rproc_t)); 434 if (nrp == NULL) { 435 return NULL; 436 } 437 rpdict = prop_dictionary_create(); 438 if (rpdict == NULL) { 439 free(nrp); 440 return NULL; 441 } 442 prop_dictionary_set_cstring(rpdict, "name", name); 443 nrp->nrp_dict = rpdict; 444 return nrp; 445 } 446 447 bool 448 npf_rproc_exists_p(nl_config_t *ncf, const char *name) 449 { 450 451 return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name); 452 } 453 454 int 455 _npf_rproc_setnorm(nl_rproc_t *rp, bool rnd, bool no_df, u_int minttl, 456 u_int maxmss) 457 { 458 prop_dictionary_t rpdict = rp->nrp_dict; 459 uint32_t fl = 0; 460 461 prop_dictionary_set_bool(rpdict, "randomize-id", rnd); 462 prop_dictionary_set_bool(rpdict, "no-df", no_df); 463 prop_dictionary_set_uint32(rpdict, "min-ttl", minttl); 464 prop_dictionary_set_uint32(rpdict, "max-mss", maxmss); 465 466 prop_dictionary_get_uint32(rpdict, "flags", &fl); 467 prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_NORMALIZE); 468 return 0; 469 } 470 471 int 472 _npf_rproc_setlog(nl_rproc_t *rp, u_int if_idx) 473 { 474 prop_dictionary_t rpdict = rp->nrp_dict; 475 uint32_t fl = 0; 476 477 prop_dictionary_set_uint32(rpdict, "log-interface", if_idx); 478 479 prop_dictionary_get_uint32(rpdict, "flags", &fl); 480 prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_LOG); 481 return 0; 482 } 483 484 int 485 npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp) 486 { 487 prop_dictionary_t rpdict = rp->nrp_dict; 488 const char *name; 489 490 if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) { 491 return EINVAL; 492 } 493 if (npf_rproc_exists_p(ncf, name)) { 494 return EEXIST; 495 } 496 prop_array_add(ncf->ncf_rproc_list, rpdict); 497 return 0; 498 } 499 500 /* 501 * TRANSLATION INTERFACE. 502 */ 503 504 nl_nat_t * 505 npf_nat_create(int type, u_int flags, u_int if_idx, 506 npf_addr_t *addr, int af, in_port_t port) 507 { 508 nl_rule_t *rl; 509 prop_dictionary_t rldict; 510 prop_data_t addrdat; 511 uint32_t attr; 512 size_t sz; 513 514 if (af == AF_INET) { 515 sz = sizeof(struct in_addr); 516 } else if (af == AF_INET6) { 517 sz = sizeof(struct in6_addr); 518 } else { 519 return NULL; 520 } 521 522 attr = NPF_RULE_PASS | NPF_RULE_FINAL | 523 (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN); 524 525 /* Create a rule for NAT policy. Next, will add translation data. */ 526 rl = npf_rule_create(NULL, attr, if_idx); 527 if (rl == NULL) { 528 return NULL; 529 } 530 rldict = rl->nrl_dict; 531 532 /* Translation type and flags. */ 533 prop_dictionary_set_int32(rldict, "type", type); 534 prop_dictionary_set_uint32(rldict, "flags", flags); 535 536 /* Translation IP. */ 537 addrdat = prop_data_create_data(addr, sz); 538 if (addrdat == NULL) { 539 npf_rule_destroy(rl); 540 return NULL; 541 } 542 prop_dictionary_set(rldict, "translation-ip", addrdat); 543 prop_object_release(addrdat); 544 545 /* Translation port (for redirect case). */ 546 prop_dictionary_set_uint16(rldict, "translation-port", port); 547 548 return (nl_nat_t *)rl; 549 } 550 551 int 552 npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri) 553 { 554 prop_dictionary_t rldict = nt->nrl_dict; 555 556 if (pri == NPF_PRI_NEXT) { 557 pri = ncf->ncf_nat_pri++; 558 } else { 559 ncf->ncf_nat_pri = pri + 1; 560 } 561 prop_dictionary_set_int32(rldict, "priority", pri); 562 prop_array_add(ncf->ncf_nat_list, rldict); 563 return 0; 564 } 565 566 int 567 _npf_nat_foreach(nl_config_t *ncf, nl_rule_callback_t func) 568 { 569 570 return _npf_rule_foreach1(ncf->ncf_nat_list, 0, func); 571 } 572 573 void 574 _npf_nat_getinfo(nl_nat_t *nt, int *type, u_int *flags, npf_addr_t *addr, 575 size_t *alen, in_port_t *port) 576 { 577 prop_dictionary_t rldict = nt->nrl_dict; 578 579 prop_dictionary_get_int32(rldict, "type", type); 580 prop_dictionary_get_uint32(rldict, "flags", flags); 581 582 prop_object_t obj = prop_dictionary_get(rldict, "translation-ip"); 583 *alen = prop_data_size(obj); 584 memcpy(addr, prop_data_data_nocopy(obj), *alen); 585 586 prop_dictionary_get_uint16(rldict, "translation-port", port); 587 } 588 589 /* 590 * TABLE INTERFACE. 591 */ 592 593 nl_table_t * 594 npf_table_create(u_int id, int type) 595 { 596 prop_dictionary_t tldict; 597 prop_array_t tblents; 598 nl_table_t *tl; 599 600 tl = malloc(sizeof(*tl)); 601 if (tl == NULL) { 602 return NULL; 603 } 604 tldict = prop_dictionary_create(); 605 if (tldict == NULL) { 606 free(tl); 607 return NULL; 608 } 609 prop_dictionary_set_uint32(tldict, "id", id); 610 prop_dictionary_set_int32(tldict, "type", type); 611 612 tblents = prop_array_create(); 613 if (tblents == NULL) { 614 prop_object_release(tldict); 615 free(tl); 616 return NULL; 617 } 618 prop_dictionary_set(tldict, "entries", tblents); 619 prop_object_release(tblents); 620 621 tl->ntl_dict = tldict; 622 return tl; 623 } 624 625 int 626 npf_table_add_entry(nl_table_t *tl, const int alen, 627 const npf_addr_t *addr, const npf_netmask_t mask) 628 { 629 prop_dictionary_t tldict = tl->ntl_dict, entdict; 630 prop_array_t tblents; 631 prop_data_t addrdata; 632 633 /* Create the table entry. */ 634 entdict = prop_dictionary_create(); 635 if (entdict == NULL) { 636 return ENOMEM; 637 } 638 addrdata = prop_data_create_data(addr, alen); 639 prop_dictionary_set(entdict, "addr", addrdata); 640 prop_dictionary_set_uint8(entdict, "mask", mask); 641 prop_object_release(addrdata); 642 643 /* Insert the entry. */ 644 tblents = prop_dictionary_get(tldict, "entries"); 645 prop_array_add(tblents, entdict); 646 prop_object_release(entdict); 647 return 0; 648 } 649 650 bool 651 npf_table_exists_p(nl_config_t *ncf, u_int tid) 652 { 653 prop_dictionary_t tldict; 654 prop_object_iterator_t it; 655 656 it = prop_array_iterator(ncf->ncf_table_list); 657 while ((tldict = prop_object_iterator_next(it)) != NULL) { 658 u_int i; 659 if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i) 660 break; 661 } 662 prop_object_iterator_release(it); 663 return tldict ? true : false; 664 } 665 666 int 667 npf_table_insert(nl_config_t *ncf, nl_table_t *tl) 668 { 669 prop_dictionary_t tldict = tl->ntl_dict; 670 u_int tid; 671 672 if (!prop_dictionary_get_uint32(tldict, "id", &tid)) { 673 return EINVAL; 674 } 675 if (npf_table_exists_p(ncf, tid)) { 676 return EEXIST; 677 } 678 prop_array_add(ncf->ncf_table_list, tldict); 679 return 0; 680 } 681 682 void 683 npf_table_destroy(nl_table_t *tl) 684 { 685 686 prop_object_release(tl->ntl_dict); 687 free(tl); 688 } 689 690 void 691 _npf_table_foreach(nl_config_t *ncf, nl_table_callback_t func) 692 { 693 prop_dictionary_t tldict; 694 prop_object_iterator_t it; 695 696 it = prop_array_iterator(ncf->ncf_table_list); 697 while ((tldict = prop_object_iterator_next(it)) != NULL) { 698 u_int id; 699 int type; 700 701 prop_dictionary_get_uint32(tldict, "id", &id); 702 prop_dictionary_get_int32(tldict, "type", &type); 703 (*func)(id, type); 704 } 705 prop_object_iterator_release(it); 706 } 707 708 /* 709 * MISC. 710 */ 711 712 int 713 npf_update_rule(int fd, const char *rname __unused, nl_rule_t *rl) 714 { 715 prop_dictionary_t rldict = rl->nrl_dict, errdict = NULL; 716 int error; 717 718 error = prop_dictionary_sendrecv_ioctl(rldict, fd, 719 IOC_NPF_UPDATE_RULE, &errdict); 720 if (errdict) { 721 prop_object_release(errdict); 722 } 723 return error; 724 } 725 726 int 727 npf_sessions_recv(int fd, const char *fpath) 728 { 729 prop_dictionary_t sdict; 730 int error; 731 732 error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict); 733 if (error) { 734 return error; 735 } 736 if (!prop_dictionary_externalize_to_file(sdict, fpath)) { 737 error = errno; 738 } 739 prop_object_release(sdict); 740 return error; 741 } 742 743 int 744 npf_sessions_send(int fd, const char *fpath) 745 { 746 prop_dictionary_t sdict; 747 int error; 748 749 if (fpath) { 750 sdict = prop_dictionary_internalize_from_file(fpath); 751 if (sdict == NULL) { 752 return errno; 753 } 754 } else { 755 /* Empty: will flush the sessions. */ 756 prop_array_t selist = prop_array_create(); 757 sdict = prop_dictionary_create(); 758 prop_dictionary_set(sdict, "session-list", selist); 759 prop_object_release(selist); 760 } 761 error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD); 762 prop_object_release(sdict); 763 return error; 764 } 765 766 static prop_dictionary_t 767 _npf_debug_initonce(nl_config_t *ncf) 768 { 769 if (!ncf->ncf_debug) { 770 prop_array_t iflist = prop_array_create(); 771 ncf->ncf_debug = prop_dictionary_create(); 772 prop_dictionary_set(ncf->ncf_debug, "interfaces", iflist); 773 prop_object_release(iflist); 774 } 775 return ncf->ncf_debug; 776 } 777 778 void 779 _npf_debug_addif(nl_config_t *ncf, struct ifaddrs *ifa, u_int if_idx) 780 { 781 prop_dictionary_t ifdict, dbg = _npf_debug_initonce(ncf); 782 prop_array_t iflist = prop_dictionary_get(dbg, "interfaces"); 783 784 if (_npf_prop_array_lookup(iflist, "name", ifa->ifa_name)) { 785 return; 786 } 787 788 ifdict = prop_dictionary_create(); 789 prop_dictionary_set_cstring(ifdict, "name", ifa->ifa_name); 790 prop_dictionary_set_uint32(ifdict, "flags", ifa->ifa_flags); 791 if (!if_idx) { 792 if_idx = if_nametoindex(ifa->ifa_name); 793 } 794 prop_dictionary_set_uint32(ifdict, "idx", if_idx); 795 796 const struct sockaddr *sa = ifa->ifa_addr; 797 npf_addr_t addr; 798 size_t alen = 0; 799 800 switch (sa ? sa->sa_family : -1) { 801 case AF_INET: { 802 const struct sockaddr_in *sin = (const void *)sa; 803 alen = sizeof(sin->sin_addr); 804 memcpy(&addr, &sin->sin_addr, alen); 805 break; 806 } 807 case AF_INET6: { 808 const struct sockaddr_in6 *sin6 = (const void *)sa; 809 alen = sizeof(sin6->sin6_addr); 810 memcpy(&addr, &sin6->sin6_addr, alen); 811 break; 812 } 813 default: 814 break; 815 } 816 817 if (alen) { 818 prop_data_t addrdata = prop_data_create_data(&addr, alen); 819 prop_dictionary_set(ifdict, "addr", addrdata); 820 prop_object_release(addrdata); 821 } 822 prop_array_add(iflist, ifdict); 823 prop_object_release(ifdict); 824 } 825