1 /* $NetBSD: npf_ctl.c,v 1.47 2017/01/29 00:15:54 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2009-2014 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 /* 33 * NPF device control. 34 * 35 * Implementation of (re)loading, construction of tables and rules. 36 * NPF proplib(9) dictionary consumer. 37 */ 38 39 #ifdef _KERNEL 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.47 2017/01/29 00:15:54 christos Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/conf.h> 45 #include <sys/kmem.h> 46 #include <net/bpf.h> 47 48 #include <prop/proplib.h> 49 #endif 50 51 #include "npf_impl.h" 52 #include "npf_conn.h" 53 54 #define NPF_ERR_DEBUG(e) \ 55 prop_dictionary_set_cstring_nocopy((e), "source-file", __FILE__); \ 56 prop_dictionary_set_uint32((e), "source-line", __LINE__); 57 58 #ifdef _KERNEL 59 /* 60 * npfctl_switch: enable or disable packet inspection. 61 */ 62 int 63 npfctl_switch(void *data) 64 { 65 const bool onoff = *(int *)data ? true : false; 66 int error; 67 68 if (onoff) { 69 /* Enable: add pfil hooks. */ 70 error = npf_pfil_register(false); 71 } else { 72 /* Disable: remove pfil hooks. */ 73 npf_pfil_unregister(false); 74 error = 0; 75 } 76 return error; 77 } 78 #endif 79 80 static int __noinline 81 npf_mk_table_entries(npf_table_t *t, prop_array_t entries) 82 { 83 prop_object_iterator_t eit; 84 prop_dictionary_t ent; 85 int error = 0; 86 87 if (prop_object_type(entries) != PROP_TYPE_ARRAY) { 88 return EINVAL; 89 } 90 eit = prop_array_iterator(entries); 91 while ((ent = prop_object_iterator_next(eit)) != NULL) { 92 const npf_addr_t *addr; 93 npf_netmask_t mask; 94 int alen; 95 96 /* Get address and mask. Add a table entry. */ 97 prop_object_t obj = prop_dictionary_get(ent, "addr"); 98 addr = (const npf_addr_t *)prop_data_data_nocopy(obj); 99 prop_dictionary_get_uint8(ent, "mask", &mask); 100 alen = prop_data_size(obj); 101 102 error = npf_table_insert(t, alen, addr, mask); 103 if (error) 104 break; 105 } 106 prop_object_iterator_release(eit); 107 return error; 108 } 109 110 static int __noinline 111 npf_mk_tables(npf_t *npf, npf_tableset_t *tblset, prop_array_t tables, 112 prop_dictionary_t errdict) 113 { 114 prop_object_iterator_t it; 115 prop_dictionary_t tbldict; 116 int error = 0; 117 118 /* Tables - array. */ 119 if (prop_object_type(tables) != PROP_TYPE_ARRAY) { 120 NPF_ERR_DEBUG(errdict); 121 return EINVAL; 122 } 123 124 it = prop_array_iterator(tables); 125 while ((tbldict = prop_object_iterator_next(it)) != NULL) { 126 const char *name; 127 npf_table_t *t; 128 uint64_t tid; 129 int type; 130 131 /* Table - dictionary. */ 132 if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) { 133 NPF_ERR_DEBUG(errdict); 134 error = EINVAL; 135 break; 136 } 137 138 /* Table name, ID and type. Validate them. */ 139 if (!prop_dictionary_get_cstring_nocopy(tbldict, "name", &name)) { 140 NPF_ERR_DEBUG(errdict); 141 error = EINVAL; 142 break; 143 } 144 prop_dictionary_get_uint64(tbldict, "id", &tid); 145 prop_dictionary_get_int32(tbldict, "type", &type); 146 error = npf_table_check(tblset, name, (u_int)tid, type); 147 if (error) { 148 NPF_ERR_DEBUG(errdict); 149 break; 150 } 151 152 /* Get the entries or binary data. */ 153 prop_array_t ents = prop_dictionary_get(tbldict, "entries"); 154 prop_object_t obj = prop_dictionary_get(tbldict, "data"); 155 void *blob = prop_data_data(obj); 156 size_t size = prop_data_size(obj); 157 158 if (type == NPF_TABLE_CDB && (blob == NULL || size == 0)) { 159 NPF_ERR_DEBUG(errdict); 160 error = EINVAL; 161 break; 162 } 163 164 /* Create and insert the table. */ 165 t = npf_table_create(name, (u_int)tid, type, blob, size); 166 if (t == NULL) { 167 NPF_ERR_DEBUG(errdict); 168 error = ENOMEM; 169 break; 170 } 171 error = npf_tableset_insert(tblset, t); 172 KASSERT(error == 0); 173 174 if (ents && (error = npf_mk_table_entries(t, ents)) != 0) { 175 NPF_ERR_DEBUG(errdict); 176 break; 177 } 178 } 179 prop_object_iterator_release(it); 180 /* 181 * Note: in a case of error, caller will free the tableset. 182 */ 183 return error; 184 } 185 186 static npf_rproc_t * 187 npf_mk_singlerproc(npf_t *npf, prop_dictionary_t rpdict) 188 { 189 prop_object_iterator_t it; 190 prop_dictionary_t extdict; 191 prop_array_t extlist; 192 npf_rproc_t *rp; 193 194 extlist = prop_dictionary_get(rpdict, "extcalls"); 195 if (prop_object_type(extlist) != PROP_TYPE_ARRAY) { 196 return NULL; 197 } 198 199 rp = npf_rproc_create(rpdict); 200 if (rp == NULL) { 201 return NULL; 202 } 203 204 it = prop_array_iterator(extlist); 205 while ((extdict = prop_object_iterator_next(it)) != NULL) { 206 const char *name; 207 208 if (!prop_dictionary_get_cstring_nocopy(extdict, "name", 209 &name) || npf_ext_construct(npf, name, rp, extdict)) { 210 npf_rproc_release(rp); 211 rp = NULL; 212 break; 213 } 214 } 215 prop_object_iterator_release(it); 216 return rp; 217 } 218 219 static int __noinline 220 npf_mk_rprocs(npf_t *npf, npf_rprocset_t *rpset, prop_array_t rprocs, 221 prop_dictionary_t errdict) 222 { 223 prop_object_iterator_t it; 224 prop_dictionary_t rpdict; 225 int error = 0; 226 227 it = prop_array_iterator(rprocs); 228 while ((rpdict = prop_object_iterator_next(it)) != NULL) { 229 npf_rproc_t *rp; 230 231 if ((rp = npf_mk_singlerproc(npf, rpdict)) == NULL) { 232 NPF_ERR_DEBUG(errdict); 233 error = EINVAL; 234 break; 235 } 236 npf_rprocset_insert(rpset, rp); 237 } 238 prop_object_iterator_release(it); 239 return error; 240 } 241 242 static npf_alg_t * 243 npf_mk_singlealg(npf_t *npf, prop_dictionary_t aldict) 244 { 245 const char *name; 246 247 if (!prop_dictionary_get_cstring_nocopy(aldict, "name", &name)) 248 return NULL; 249 return npf_alg_construct(npf, name); 250 } 251 252 static int __noinline 253 npf_mk_algs(npf_t *npf, prop_array_t alglist, prop_dictionary_t errdict) 254 { 255 prop_object_iterator_t it; 256 prop_dictionary_t nadict; 257 int error = 0; 258 259 it = prop_array_iterator(alglist); 260 while ((nadict = prop_object_iterator_next(it)) != NULL) { 261 if (npf_mk_singlealg(npf, nadict) == NULL) { 262 NPF_ERR_DEBUG(errdict); 263 error = EINVAL; 264 break; 265 } 266 } 267 prop_object_iterator_release(it); 268 return error; 269 } 270 271 static int __noinline 272 npf_mk_code(prop_object_t obj, int type, void **code, size_t *csize, 273 prop_dictionary_t errdict) 274 { 275 const void *cptr; 276 size_t clen; 277 void *bc; 278 279 if (type != NPF_CODE_BPF) { 280 return ENOTSUP; 281 } 282 cptr = prop_data_data_nocopy(obj); 283 if (cptr == NULL || (clen = prop_data_size(obj)) == 0) { 284 NPF_ERR_DEBUG(errdict); 285 return EINVAL; 286 } 287 if (!npf_bpf_validate(cptr, clen)) { 288 NPF_ERR_DEBUG(errdict); 289 return EINVAL; 290 } 291 bc = kmem_alloc(clen, KM_SLEEP); 292 memcpy(bc, cptr, clen); 293 294 *code = bc; 295 *csize = clen; 296 return 0; 297 } 298 299 static int __noinline 300 npf_mk_singlerule(npf_t *npf, prop_dictionary_t rldict, npf_rprocset_t *rpset, 301 npf_rule_t **rlret, prop_dictionary_t errdict) 302 { 303 npf_rule_t *rl; 304 const char *rname; 305 prop_object_t obj; 306 int p, error = 0; 307 308 /* Rule - dictionary. */ 309 if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) { 310 NPF_ERR_DEBUG(errdict); 311 return EINVAL; 312 } 313 if ((rl = npf_rule_alloc(npf, rldict)) == NULL) { 314 NPF_ERR_DEBUG(errdict); 315 return EINVAL; 316 } 317 318 /* Assign rule procedure, if any. */ 319 if (prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rname)) { 320 npf_rproc_t *rp; 321 322 if (rpset == NULL) { 323 error = EINVAL; 324 goto err; 325 } 326 if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) { 327 NPF_ERR_DEBUG(errdict); 328 error = EINVAL; 329 goto err; 330 } 331 npf_rule_setrproc(rl, rp); 332 } 333 334 /* Filter code (binary data). */ 335 if ((obj = prop_dictionary_get(rldict, "code")) != NULL) { 336 int type; 337 size_t len; 338 void *code; 339 340 prop_dictionary_get_int32(rldict, "code-type", &type); 341 error = npf_mk_code(obj, type, &code, &len, errdict); 342 if (error) { 343 goto err; 344 } 345 npf_rule_setcode(rl, type, code, len); 346 } 347 348 *rlret = rl; 349 return 0; 350 err: 351 npf_rule_free(rl); 352 prop_dictionary_get_int32(rldict, "prio", &p); /* XXX */ 353 prop_dictionary_set_int64(errdict, "id", p); 354 return error; 355 } 356 357 static int __noinline 358 npf_mk_rules(npf_t *npf, npf_ruleset_t *rlset, prop_array_t rules, 359 npf_rprocset_t *rpset, prop_dictionary_t errdict) 360 { 361 prop_object_iterator_t it; 362 prop_dictionary_t rldict; 363 int error; 364 365 if (prop_object_type(rules) != PROP_TYPE_ARRAY) { 366 NPF_ERR_DEBUG(errdict); 367 return EINVAL; 368 } 369 370 error = 0; 371 it = prop_array_iterator(rules); 372 while ((rldict = prop_object_iterator_next(it)) != NULL) { 373 npf_rule_t *rl = NULL; 374 375 /* Generate a single rule. */ 376 error = npf_mk_singlerule(npf, rldict, rpset, &rl, errdict); 377 if (error) { 378 break; 379 } 380 npf_ruleset_insert(rlset, rl); 381 } 382 prop_object_iterator_release(it); 383 /* 384 * Note: in a case of error, caller will free the ruleset. 385 */ 386 return error; 387 } 388 389 static int __noinline 390 npf_mk_natlist(npf_t *npf, npf_ruleset_t *nset, prop_array_t natlist, 391 prop_dictionary_t errdict) 392 { 393 prop_object_iterator_t it; 394 prop_dictionary_t natdict; 395 int error; 396 397 /* NAT policies - array. */ 398 if (prop_object_type(natlist) != PROP_TYPE_ARRAY) { 399 NPF_ERR_DEBUG(errdict); 400 return EINVAL; 401 } 402 403 error = 0; 404 it = prop_array_iterator(natlist); 405 while ((natdict = prop_object_iterator_next(it)) != NULL) { 406 npf_rule_t *rl = NULL; 407 npf_natpolicy_t *np; 408 409 /* NAT policy - dictionary. */ 410 if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) { 411 NPF_ERR_DEBUG(errdict); 412 error = EINVAL; 413 break; 414 } 415 416 /* 417 * NAT policies are standard rules, plus additional 418 * information for translation. Make a rule. 419 */ 420 error = npf_mk_singlerule(npf, natdict, NULL, &rl, errdict); 421 if (error) { 422 break; 423 } 424 npf_ruleset_insert(nset, rl); 425 426 /* If rule is named, it is a group with NAT policies. */ 427 if (prop_dictionary_get(natdict, "name") && 428 prop_dictionary_get(natdict, "subrules")) { 429 continue; 430 } 431 432 /* Allocate a new NAT policy and assign to the rule. */ 433 np = npf_nat_newpolicy(npf, natdict, nset); 434 if (np == NULL) { 435 NPF_ERR_DEBUG(errdict); 436 error = ENOMEM; 437 break; 438 } 439 npf_rule_setnat(rl, np); 440 } 441 prop_object_iterator_release(it); 442 /* 443 * Note: in a case of error, caller will free entire NAT ruleset 444 * with assigned NAT policies. 445 */ 446 return error; 447 } 448 449 /* 450 * npf_mk_connlist: import a list of connections and load them. 451 */ 452 static int __noinline 453 npf_mk_connlist(npf_t *npf, prop_array_t conlist, npf_ruleset_t *natlist, 454 npf_conndb_t **conndb, prop_dictionary_t errdict) 455 { 456 prop_dictionary_t condict; 457 prop_object_iterator_t it; 458 npf_conndb_t *cd; 459 int error = 0; 460 461 /* Connection list - array */ 462 if (prop_object_type(conlist) != PROP_TYPE_ARRAY) { 463 NPF_ERR_DEBUG(errdict); 464 return EINVAL; 465 } 466 467 /* Create a connection database. */ 468 cd = npf_conndb_create(); 469 it = prop_array_iterator(conlist); 470 while ((condict = prop_object_iterator_next(it)) != NULL) { 471 /* Connection - dictionary. */ 472 if (prop_object_type(condict) != PROP_TYPE_DICTIONARY) { 473 NPF_ERR_DEBUG(errdict); 474 error = EINVAL; 475 break; 476 } 477 /* Construct and insert the connection. */ 478 error = npf_conn_import(npf, cd, condict, natlist); 479 if (error) { 480 NPF_ERR_DEBUG(errdict); 481 break; 482 } 483 } 484 prop_object_iterator_release(it); 485 if (error) { 486 npf_conn_gc(npf, cd, true, false); 487 npf_conndb_destroy(cd); 488 } else { 489 *conndb = cd; 490 } 491 return error; 492 } 493 494 /* 495 * npfctl_load: store passed data i.e. update settings, create passed 496 * tables, rules and atomically activate all them. 497 */ 498 int 499 npfctl_load(npf_t *npf, u_long cmd, void *data) 500 { 501 struct plistref *pref = data; 502 prop_dictionary_t npf_dict, errdict; 503 prop_array_t alglist, natlist, tables, rprocs, rules, conlist; 504 npf_tableset_t *tblset = NULL; 505 npf_rprocset_t *rpset = NULL; 506 npf_ruleset_t *rlset = NULL; 507 npf_ruleset_t *nset = NULL; 508 npf_conndb_t *conndb = NULL; 509 uint32_t ver = 0; 510 size_t nitems; 511 bool flush; 512 int error; 513 514 /* Retrieve the dictionary. */ 515 #if !defined(_NPF_TESTING) && !defined(_NPF_STANDALONE) 516 error = prop_dictionary_copyin_ioctl_size(pref, cmd, &npf_dict, 517 4 * 1024 * 1024); 518 if (error) 519 return error; 520 #else 521 npf_dict = (prop_dictionary_t)pref; 522 #endif 523 524 /* Dictionary for error reporting and version check. */ 525 errdict = prop_dictionary_create(); 526 prop_dictionary_get_uint32(npf_dict, "version", &ver); 527 if (ver != NPF_VERSION) { 528 error = EPROGMISMATCH; 529 goto fail; 530 } 531 532 /* ALGs. */ 533 alglist = prop_dictionary_get(npf_dict, "algs"); 534 error = npf_mk_algs(npf, alglist, errdict); 535 if (error) { 536 goto fail; 537 } 538 539 /* NAT policies. */ 540 natlist = prop_dictionary_get(npf_dict, "nat"); 541 if ((nitems = prop_array_count(natlist)) > NPF_MAX_RULES) { 542 error = E2BIG; 543 goto fail; 544 } 545 546 nset = npf_ruleset_create(nitems); 547 error = npf_mk_natlist(npf, nset, natlist, errdict); 548 if (error) { 549 goto fail; 550 } 551 552 /* Tables. */ 553 tables = prop_dictionary_get(npf_dict, "tables"); 554 if ((nitems = prop_array_count(tables)) > NPF_MAX_TABLES) { 555 error = E2BIG; 556 goto fail; 557 } 558 tblset = npf_tableset_create(nitems); 559 error = npf_mk_tables(npf, tblset, tables, errdict); 560 if (error) { 561 goto fail; 562 } 563 564 /* Rule procedures. */ 565 rprocs = prop_dictionary_get(npf_dict, "rprocs"); 566 if ((nitems = prop_array_count(rprocs)) > NPF_MAX_RPROCS) { 567 error = E2BIG; 568 goto fail; 569 } 570 rpset = npf_rprocset_create(); 571 error = npf_mk_rprocs(npf, rpset, rprocs, errdict); 572 if (error) { 573 goto fail; 574 } 575 576 /* Rules. */ 577 rules = prop_dictionary_get(npf_dict, "rules"); 578 if ((nitems = prop_array_count(rules)) > NPF_MAX_RULES) { 579 error = E2BIG; 580 goto fail; 581 } 582 583 rlset = npf_ruleset_create(nitems); 584 error = npf_mk_rules(npf, rlset, rules, rpset, errdict); 585 if (error) { 586 goto fail; 587 } 588 589 /* Connections (if loading any). */ 590 if ((conlist = prop_dictionary_get(npf_dict, "conn-list")) != NULL) { 591 error = npf_mk_connlist(npf, conlist, nset, &conndb, errdict); 592 if (error) { 593 goto fail; 594 } 595 } 596 597 flush = false; 598 prop_dictionary_get_bool(npf_dict, "flush", &flush); 599 600 /* 601 * Finally - perform the load. 602 */ 603 npf_config_load(npf, rlset, tblset, nset, rpset, conndb, flush); 604 605 /* Done. Since data is consumed now, we shall not destroy it. */ 606 tblset = NULL; 607 rpset = NULL; 608 rlset = NULL; 609 nset = NULL; 610 fail: 611 /* 612 * Note: destroy rulesets first, to drop references to the tableset. 613 */ 614 if (nset) { 615 npf_ruleset_destroy(nset); 616 } 617 if (rlset) { 618 npf_ruleset_destroy(rlset); 619 } 620 if (rpset) { 621 npf_rprocset_destroy(rpset); 622 } 623 if (tblset) { 624 npf_tableset_destroy(tblset); 625 } 626 prop_object_release(npf_dict); 627 628 /* Error report. */ 629 #if !defined(_NPF_TESTING) && !defined(_NPF_STANDALONE) 630 prop_dictionary_set_int32(errdict, "errno", error); 631 prop_dictionary_copyout_ioctl(pref, cmd, errdict); 632 prop_object_release(errdict); 633 error = 0; 634 #endif 635 return error; 636 } 637 638 /* 639 * npfctl_save: export the config dictionary as it was submitted, 640 * including the current snapshot of the connections. Additionally, 641 * indicate whether the ruleset is currently active. 642 */ 643 int 644 npfctl_save(npf_t *npf, u_long cmd, void *data) 645 { 646 struct plistref *pref = data; 647 prop_array_t rulelist, natlist, tables, rprocs, conlist; 648 prop_dictionary_t npf_dict = NULL; 649 int error; 650 651 rulelist = prop_array_create(); 652 natlist = prop_array_create(); 653 tables = prop_array_create(); 654 rprocs = prop_array_create(); 655 conlist = prop_array_create(); 656 657 /* 658 * Serialise the connections and NAT policies. 659 */ 660 npf_config_enter(npf); 661 error = npf_conndb_export(npf, conlist); 662 if (error) { 663 goto out; 664 } 665 error = npf_ruleset_export(npf, npf_config_ruleset(npf), rulelist); 666 if (error) { 667 goto out; 668 } 669 error = npf_ruleset_export(npf, npf_config_natset(npf), natlist); 670 if (error) { 671 goto out; 672 } 673 error = npf_tableset_export(npf, npf_config_tableset(npf), tables); 674 if (error) { 675 goto out; 676 } 677 error = npf_rprocset_export(npf_config_rprocs(npf), rprocs); 678 if (error) { 679 goto out; 680 } 681 prop_array_t alglist = npf_alg_export(npf); 682 683 npf_dict = prop_dictionary_create(); 684 prop_dictionary_set_uint32(npf_dict, "version", NPF_VERSION); 685 prop_dictionary_set_and_rel(npf_dict, "algs", alglist); 686 prop_dictionary_set_and_rel(npf_dict, "rules", rulelist); 687 prop_dictionary_set_and_rel(npf_dict, "nat", natlist); 688 prop_dictionary_set_and_rel(npf_dict, "tables", tables); 689 prop_dictionary_set_and_rel(npf_dict, "rprocs", rprocs); 690 prop_dictionary_set_and_rel(npf_dict, "conn-list", conlist); 691 #if !defined(_NPF_STANDALONE) 692 prop_dictionary_set_bool(npf_dict, "active", npf_pfil_registered_p()); 693 error = prop_dictionary_copyout_ioctl(pref, cmd, npf_dict); 694 #else 695 prop_dictionary_set_bool(npf_dict, "active", true); 696 /* Userspace: just copy the pointer of the dictionary. */ 697 CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *)); 698 memcpy(data, npf_dict, sizeof(void *)); 699 #endif 700 out: 701 npf_config_exit(npf); 702 703 if (!npf_dict) { 704 prop_object_release(rulelist); 705 prop_object_release(natlist); 706 prop_object_release(tables); 707 prop_object_release(rprocs); 708 prop_object_release(conlist); 709 } else { 710 #if !defined(_NPF_STANDALONE) 711 prop_object_release(npf_dict); 712 #endif 713 } 714 return error; 715 } 716 717 /* 718 * npfctl_conn_lookup: lookup a connection in the list of connections 719 */ 720 int 721 npfctl_conn_lookup(npf_t *npf, u_long cmd, void *data) 722 { 723 struct plistref *pref = data; 724 prop_dictionary_t conn_data, conn_result; 725 int error; 726 727 #if !defined(_NPF_STANDALONE) 728 error = prop_dictionary_copyin_ioctl(pref, cmd, &conn_data); 729 if (error) { 730 return error; 731 } 732 #else 733 conn_data = (prop_dictionary_t)pref; 734 #endif 735 error = npf_conn_find(npf, conn_data, &conn_result); 736 if (error) { 737 goto out; 738 } 739 #if !defined(_NPF_STANDALONE) 740 prop_dictionary_copyout_ioctl(pref, cmd, conn_result); 741 prop_object_release(conn_result); 742 #else 743 CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *)); 744 memcpy(data, conn_result, sizeof(void *)); 745 #endif 746 out: 747 prop_object_release(conn_data); 748 return error; 749 } 750 751 /* 752 * npfctl_rule: add or remove dynamic rules in the specified ruleset. 753 */ 754 int 755 npfctl_rule(npf_t *npf, u_long cmd, void *data) 756 { 757 struct plistref *pref = data; 758 prop_dictionary_t npf_rule, retdict = NULL; 759 npf_ruleset_t *rlset; 760 npf_rule_t *rl = NULL; 761 const char *ruleset_name; 762 uint32_t rcmd = 0; 763 int error = 0; 764 765 #if !defined(_NPF_STANDALONE) 766 error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_rule); 767 if (error) { 768 return error; 769 } 770 #else 771 npf_rule = (prop_dictionary_t)pref; 772 #endif 773 prop_dictionary_get_uint32(npf_rule, "command", &rcmd); 774 if (!prop_dictionary_get_cstring_nocopy(npf_rule, 775 "ruleset-name", &ruleset_name)) { 776 error = EINVAL; 777 goto out; 778 } 779 780 if (rcmd == NPF_CMD_RULE_ADD) { 781 retdict = prop_dictionary_create(); 782 if (npf_mk_singlerule(npf, npf_rule, NULL, &rl, retdict) != 0) { 783 error = EINVAL; 784 goto out; 785 } 786 } 787 788 npf_config_enter(npf); 789 rlset = npf_config_ruleset(npf); 790 791 switch (rcmd) { 792 case NPF_CMD_RULE_ADD: { 793 if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) { 794 /* Success. */ 795 uint64_t id = npf_rule_getid(rl); 796 prop_dictionary_set_uint64(retdict, "id", id); 797 rl = NULL; 798 } 799 break; 800 } 801 case NPF_CMD_RULE_REMOVE: { 802 uint64_t id; 803 804 if (!prop_dictionary_get_uint64(npf_rule, "id", &id)) { 805 error = EINVAL; 806 break; 807 } 808 error = npf_ruleset_remove(rlset, ruleset_name, id); 809 break; 810 } 811 case NPF_CMD_RULE_REMKEY: { 812 prop_object_t obj = prop_dictionary_get(npf_rule, "key"); 813 const void *key = prop_data_data_nocopy(obj); 814 size_t len = prop_data_size(obj); 815 816 if (len == 0 || len > NPF_RULE_MAXKEYLEN) { 817 error = EINVAL; 818 break; 819 } 820 error = npf_ruleset_remkey(rlset, ruleset_name, key, len); 821 break; 822 } 823 case NPF_CMD_RULE_LIST: { 824 retdict = npf_ruleset_list(npf, rlset, ruleset_name); 825 if (!retdict) { 826 error = ESRCH; 827 } 828 break; 829 } 830 case NPF_CMD_RULE_FLUSH: { 831 error = npf_ruleset_flush(rlset, ruleset_name); 832 break; 833 } 834 default: 835 error = EINVAL; 836 break; 837 } 838 839 /* Destroy any removed rules. */ 840 if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) { 841 npf_config_sync(npf); 842 npf_ruleset_gc(rlset); 843 } 844 npf_config_exit(npf); 845 846 if (rl) { 847 KASSERT(error); 848 npf_rule_free(rl); 849 } 850 out: 851 if (retdict) { 852 prop_object_release(npf_rule); 853 #if !defined(_NPF_STANDALONE) 854 prop_dictionary_copyout_ioctl(pref, cmd, retdict); 855 prop_object_release(retdict); 856 #else 857 CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *)); 858 memcpy(data, npf_rule, sizeof(void *)); 859 #endif 860 } 861 return error; 862 } 863 864 /* 865 * npfctl_table: add, remove or query entries in the specified table. 866 * 867 * For maximum performance, interface is avoiding proplib(3)'s overhead. 868 */ 869 int 870 npfctl_table(npf_t *npf, void *data) 871 { 872 const npf_ioctl_table_t *nct = data; 873 char tname[NPF_TABLE_MAXNAMELEN]; 874 npf_tableset_t *ts; 875 npf_table_t *t; 876 int s, error; 877 878 error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL); 879 if (error) { 880 return error; 881 } 882 883 s = npf_config_read_enter(); /* XXX */ 884 ts = npf_config_tableset(npf); 885 if ((t = npf_tableset_getbyname(ts, tname)) == NULL) { 886 npf_config_read_exit(s); 887 return EINVAL; 888 } 889 890 switch (nct->nct_cmd) { 891 case NPF_CMD_TABLE_LOOKUP: 892 error = npf_table_lookup(t, nct->nct_data.ent.alen, 893 &nct->nct_data.ent.addr); 894 break; 895 case NPF_CMD_TABLE_ADD: 896 error = npf_table_insert(t, nct->nct_data.ent.alen, 897 &nct->nct_data.ent.addr, nct->nct_data.ent.mask); 898 break; 899 case NPF_CMD_TABLE_REMOVE: 900 error = npf_table_remove(t, nct->nct_data.ent.alen, 901 &nct->nct_data.ent.addr, nct->nct_data.ent.mask); 902 break; 903 case NPF_CMD_TABLE_LIST: 904 error = npf_table_list(t, nct->nct_data.buf.buf, 905 nct->nct_data.buf.len); 906 break; 907 case NPF_CMD_TABLE_FLUSH: 908 error = npf_table_flush(t); 909 break; 910 default: 911 error = EINVAL; 912 break; 913 } 914 npf_config_read_exit(s); 915 916 return error; 917 } 918