1 /*- 2 * Copyright (c) 2014 Yandex LLC 3 * Copyright (c) 2014 Alexander V. Chernikov 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 /* 29 * Multi-field value support for ipfw tables. 30 * 31 * This file contains necessary functions to convert 32 * large multi-field values into u32 indices suitable to be fed 33 * to various table algorithms. Other machinery like proper refcounting, 34 * internal structures resizing are also kept here. 35 */ 36 37 #include "opt_ipfw.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/malloc.h> 42 #include <sys/kernel.h> 43 #include <sys/hash.h> 44 #include <sys/lock.h> 45 #include <sys/rwlock.h> 46 #include <sys/rmlock.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/queue.h> 50 #include <net/if.h> /* ip_fw.h requires IFNAMSIZ */ 51 52 #include <netinet/in.h> 53 #include <netinet/ip_var.h> /* struct ipfw_rule_ref */ 54 #include <netinet/ip_fw.h> 55 56 #include <netpfil/ipfw/ip_fw_private.h> 57 #include <netpfil/ipfw/ip_fw_table.h> 58 59 static uint32_t hash_table_value(struct namedobj_instance *ni, const void *key, 60 uint32_t kopt); 61 static int cmp_table_value(struct named_object *no, const void *key, 62 uint32_t kopt); 63 64 static int list_table_values(struct ip_fw_chain *ch, ip_fw3_opheader *op3, 65 struct sockopt_data *sd); 66 67 static struct ipfw_sopt_handler scodes[] = { 68 { IP_FW_TABLE_VLIST, 0, HDIR_GET, list_table_values }, 69 }; 70 71 #define CHAIN_TO_VI(chain) (CHAIN_TO_TCFG(chain)->valhash) 72 73 struct table_val_link 74 { 75 struct named_object no; 76 struct table_value *pval; /* Pointer to real table value */ 77 }; 78 #define VALDATA_START_SIZE 64 /* Allocate 64-items array by default */ 79 80 struct vdump_args { 81 struct ip_fw_chain *ch; 82 struct sockopt_data *sd; 83 struct table_value *pval; 84 int error; 85 }; 86 87 static uint32_t 88 hash_table_value(struct namedobj_instance *ni, const void *key, uint32_t kopt) 89 { 90 91 return (hash32_buf(key, 56, 0)); 92 } 93 94 static int 95 cmp_table_value(struct named_object *no, const void *key, uint32_t kopt) 96 { 97 98 return (memcmp(((struct table_val_link *)no)->pval, key, 56)); 99 } 100 101 static void 102 mask_table_value(struct table_value *src, struct table_value *dst, 103 uint32_t mask) 104 { 105 #define _MCPY(f, b) if ((mask & (b)) != 0) { dst->f = src->f; } 106 107 memset(dst, 0, sizeof(*dst)); 108 _MCPY(tag, IPFW_VTYPE_TAG); 109 _MCPY(pipe, IPFW_VTYPE_PIPE); 110 _MCPY(divert, IPFW_VTYPE_DIVERT); 111 _MCPY(skipto, IPFW_VTYPE_SKIPTO); 112 _MCPY(netgraph, IPFW_VTYPE_NETGRAPH); 113 _MCPY(fib, IPFW_VTYPE_FIB); 114 _MCPY(nat, IPFW_VTYPE_NAT); 115 _MCPY(mark, IPFW_VTYPE_MARK); 116 _MCPY(dscp, IPFW_VTYPE_DSCP); 117 _MCPY(nh4, IPFW_VTYPE_NH4); 118 _MCPY(nh6, IPFW_VTYPE_NH6); 119 _MCPY(zoneid, IPFW_VTYPE_NH6); 120 #undef _MCPY 121 } 122 123 static void 124 get_value_ptrs(struct ip_fw_chain *ch, struct table_config *tc, int vshared, 125 struct table_value **ptv, struct namedobj_instance **pvi) 126 { 127 struct table_value *pval; 128 struct namedobj_instance *vi; 129 130 if (vshared != 0) { 131 pval = (struct table_value *)ch->valuestate; 132 vi = CHAIN_TO_VI(ch); 133 } else { 134 pval = NULL; 135 vi = NULL; 136 //pval = (struct table_value *)&tc->ti.data; 137 } 138 139 if (ptv != NULL) 140 *ptv = pval; 141 if (pvi != NULL) 142 *pvi = vi; 143 } 144 145 /* 146 * Update pointers to real vaues after @pval change. 147 */ 148 static int 149 update_tvalue(struct namedobj_instance *ni, struct named_object *no, void *arg) 150 { 151 struct vdump_args *da; 152 struct table_val_link *ptv; 153 struct table_value *pval; 154 155 da = (struct vdump_args *)arg; 156 ptv = (struct table_val_link *)no; 157 158 pval = da->pval; 159 ptv->pval = &pval[ptv->no.kidx]; 160 ptv->no.name = (char *)&pval[ptv->no.kidx]; 161 return (0); 162 } 163 164 /* 165 * Grows value storage shared among all tables. 166 * Drops/reacquires UH locks. 167 * Notifies other running adds on @ch shared storage resize. 168 * Note function does not guarantee that free space 169 * will be available after invocation, so one caller needs 170 * to roll cycle himself. 171 * 172 * Returns 0 if case of no errors. 173 */ 174 static int 175 resize_shared_value_storage(struct ip_fw_chain *ch) 176 { 177 struct tables_config *tcfg; 178 struct namedobj_instance *vi; 179 struct table_value *pval, *valuestate, *old_valuestate; 180 void *new_idx; 181 struct vdump_args da; 182 int new_blocks; 183 int val_size, val_size_old; 184 185 IPFW_UH_WLOCK_ASSERT(ch); 186 187 valuestate = NULL; 188 new_idx = NULL; 189 190 pval = (struct table_value *)ch->valuestate; 191 vi = CHAIN_TO_VI(ch); 192 tcfg = CHAIN_TO_TCFG(ch); 193 194 val_size = tcfg->val_size * 2; 195 196 if (val_size == (1 << 30)) 197 return (ENOSPC); 198 199 IPFW_UH_WUNLOCK(ch); 200 201 valuestate = malloc(sizeof(struct table_value) * val_size, M_IPFW, 202 M_WAITOK | M_ZERO); 203 ipfw_objhash_bitmap_alloc(val_size, (void *)&new_idx, 204 &new_blocks); 205 206 IPFW_UH_WLOCK(ch); 207 208 /* 209 * Check if we still need to resize 210 */ 211 if (tcfg->val_size >= val_size) 212 goto done; 213 214 /* Update pointers and notify everyone we're changing @ch */ 215 pval = (struct table_value *)ch->valuestate; 216 rollback_toperation_state(ch, ch); 217 218 /* Good. Let's merge */ 219 memcpy(valuestate, pval, sizeof(struct table_value) * tcfg->val_size); 220 ipfw_objhash_bitmap_merge(CHAIN_TO_VI(ch), &new_idx, &new_blocks); 221 222 IPFW_WLOCK(ch); 223 /* Change pointers */ 224 old_valuestate = ch->valuestate; 225 ch->valuestate = valuestate; 226 valuestate = old_valuestate; 227 ipfw_objhash_bitmap_swap(CHAIN_TO_VI(ch), &new_idx, &new_blocks); 228 229 val_size_old = tcfg->val_size; 230 tcfg->val_size = val_size; 231 val_size = val_size_old; 232 IPFW_WUNLOCK(ch); 233 /* Update pointers to reflect resize */ 234 memset(&da, 0, sizeof(da)); 235 da.pval = (struct table_value *)ch->valuestate; 236 ipfw_objhash_foreach(vi, update_tvalue, &da); 237 238 done: 239 free(valuestate, M_IPFW); 240 ipfw_objhash_bitmap_free(new_idx, new_blocks); 241 242 return (0); 243 } 244 245 /* 246 * Drops reference for table value with index @kidx, stored in @pval and 247 * @vi. Frees value if it has no references. 248 */ 249 static void 250 unref_table_value(struct namedobj_instance *vi, struct table_value *pval, 251 uint32_t kidx) 252 { 253 struct table_val_link *ptvl; 254 255 KASSERT(pval[kidx].refcnt > 0, ("Refcount is 0 on kidx %d", kidx)); 256 if (--pval[kidx].refcnt > 0) 257 return; 258 259 /* Last reference, delete item */ 260 ptvl = (struct table_val_link *)ipfw_objhash_lookup_kidx(vi, kidx); 261 KASSERT(ptvl != NULL, ("lookup on value kidx %d failed", kidx)); 262 ipfw_objhash_del(vi, &ptvl->no); 263 ipfw_objhash_free_idx(vi, kidx); 264 free(ptvl, M_IPFW); 265 } 266 267 struct flush_args { 268 struct ip_fw_chain *ch; 269 struct table_algo *ta; 270 struct table_info *ti; 271 void *astate; 272 ipfw_obj_tentry tent; 273 }; 274 275 static int 276 unref_table_value_cb(void *e, void *arg) 277 { 278 struct flush_args *fa; 279 struct ip_fw_chain *ch; 280 struct table_algo *ta; 281 ipfw_obj_tentry *tent; 282 int error; 283 284 fa = (struct flush_args *)arg; 285 286 ta = fa->ta; 287 memset(&fa->tent, 0, sizeof(fa->tent)); 288 tent = &fa->tent; 289 error = ta->dump_tentry(fa->astate, fa->ti, e, tent); 290 if (error != 0) 291 return (error); 292 293 ch = fa->ch; 294 295 unref_table_value(CHAIN_TO_VI(ch), 296 (struct table_value *)ch->valuestate, tent->v.kidx); 297 298 return (0); 299 } 300 301 /* 302 * Drop references for each value used in @tc. 303 */ 304 void 305 ipfw_unref_table_values(struct ip_fw_chain *ch, struct table_config *tc, 306 struct table_algo *ta, void *astate, struct table_info *ti) 307 { 308 struct flush_args fa; 309 310 IPFW_UH_WLOCK_ASSERT(ch); 311 312 memset(&fa, 0, sizeof(fa)); 313 fa.ch = ch; 314 fa.ta = ta; 315 fa.astate = astate; 316 fa.ti = ti; 317 318 ta->foreach(astate, ti, unref_table_value_cb, &fa); 319 } 320 321 /* 322 * Table operation state handler. 323 * Called when we are going to change something in @tc which 324 * may lead to inconsistencies in on-going table data addition. 325 * 326 * Here we rollback all already committed state (table values, currently) 327 * and set "modified" field to non-zero value to indicate 328 * that we need to restart original operation. 329 */ 330 void 331 rollback_table_values(struct tableop_state *ts) 332 { 333 struct ip_fw_chain *ch; 334 struct table_value *pval; 335 struct tentry_info *ptei; 336 struct namedobj_instance *vi; 337 int i; 338 339 ch = ts->ch; 340 341 IPFW_UH_WLOCK_ASSERT(ch); 342 343 /* Get current table value pointer */ 344 get_value_ptrs(ch, ts->tc, ts->vshared, &pval, &vi); 345 346 for (i = 0; i < ts->count; i++) { 347 ptei = &ts->tei[i]; 348 349 if (ptei->value == 0) 350 continue; 351 352 unref_table_value(vi, pval, ptei->value); 353 } 354 } 355 356 /* 357 * Allocate new value index in either shared or per-table array. 358 * Function may drop/reacquire UH lock. 359 * 360 * Returns 0 on success. 361 */ 362 static int 363 alloc_table_vidx(struct ip_fw_chain *ch, struct tableop_state *ts, 364 struct namedobj_instance *vi, uint16_t *pvidx, uint8_t flags) 365 { 366 int error, vlimit; 367 uint16_t vidx; 368 369 IPFW_UH_WLOCK_ASSERT(ch); 370 371 error = ipfw_objhash_alloc_idx(vi, &vidx); 372 if (error != 0) { 373 /* 374 * We need to resize array. This involves 375 * lock/unlock, so we need to check "modified" 376 * state. 377 */ 378 ts->opstate.func(ts->tc, &ts->opstate); 379 error = resize_shared_value_storage(ch); 380 return (error); /* ts->modified should be set, we will restart */ 381 } 382 383 vlimit = ts->ta->vlimit; 384 if (vlimit != 0 && vidx >= vlimit && !(flags & IPFW_CTF_ATOMIC)) { 385 /* 386 * Algorithm is not able to store given index. 387 * We have to rollback state, start using 388 * per-table value array or return error 389 * if we're already using it. 390 */ 391 if (ts->vshared != 0) { 392 /* shared -> per-table */ 393 return (ENOSPC); /* TODO: proper error */ 394 } 395 396 /* per-table. Fail for now. */ 397 return (ENOSPC); /* TODO: proper error */ 398 } 399 400 *pvidx = vidx; 401 return (0); 402 } 403 404 /* 405 * Drops value reference for unused values (updates, deletes, partially 406 * successful adds or rollbacks). 407 */ 408 void 409 ipfw_garbage_table_values(struct ip_fw_chain *ch, struct table_config *tc, 410 struct tentry_info *tei, uint32_t count, int rollback) 411 { 412 int i; 413 struct tentry_info *ptei; 414 struct table_value *pval; 415 struct namedobj_instance *vi; 416 417 /* 418 * We have two slightly different ADD cases here: 419 * either (1) we are successful / partially successful, 420 * in that case we need 421 * * to ignore ADDED entries values 422 * * rollback every other values if atomicity is not 423 * * required (either UPDATED since old value has been 424 * stored there, or some failure like EXISTS or LIMIT 425 * or simply "ignored" case. 426 * 427 * (2): atomic rollback of partially successful operation 428 * in that case we simply need to unref all entries. 429 * 430 * DELETE case is simpler: no atomic support there, so 431 * we simply unref all non-zero values. 432 */ 433 434 /* 435 * Get current table value pointers. 436 * XXX: Properly read vshared 437 */ 438 get_value_ptrs(ch, tc, 1, &pval, &vi); 439 440 for (i = 0; i < count; i++) { 441 ptei = &tei[i]; 442 443 if (ptei->value == 0) { 444 /* 445 * We may be deleting non-existing record. 446 * Skip. 447 */ 448 continue; 449 } 450 451 if ((ptei->flags & TEI_FLAGS_ADDED) != 0 && rollback == 0) { 452 ptei->value = 0; 453 continue; 454 } 455 456 unref_table_value(vi, pval, ptei->value); 457 ptei->value = 0; 458 } 459 } 460 461 /* 462 * Main function used to link values of entries going to be added, 463 * to the index. Since we may perform many UH locks drops/acquires, 464 * handle changes by checking tablestate "modified" field. 465 * 466 * Success: return 0. 467 */ 468 int 469 ipfw_link_table_values(struct ip_fw_chain *ch, struct tableop_state *ts, 470 uint8_t flags) 471 { 472 int error, i, found; 473 struct namedobj_instance *vi; 474 struct table_config *tc; 475 struct tentry_info *tei, *ptei; 476 uint32_t count, vlimit; 477 uint16_t vidx; 478 struct table_val_link *ptv; 479 struct table_value tval, *pval; 480 481 /* 482 * Stage 1: reference all existing values and 483 * save their indices. 484 */ 485 IPFW_UH_WLOCK_ASSERT(ch); 486 get_value_ptrs(ch, ts->tc, ts->vshared, &pval, &vi); 487 488 error = 0; 489 found = 0; 490 vlimit = ts->ta->vlimit; 491 vidx = 0; 492 tc = ts->tc; 493 tei = ts->tei; 494 count = ts->count; 495 for (i = 0; i < count; i++) { 496 ptei = &tei[i]; 497 ptei->value = 0; /* Ensure value is always 0 in the beginning */ 498 mask_table_value(ptei->pvalue, &tval, ts->vmask); 499 ptv = (struct table_val_link *)ipfw_objhash_lookup_name(vi, 0, 500 (char *)&tval); 501 if (ptv == NULL) 502 continue; 503 /* Deal with vlimit later */ 504 if (vlimit > 0 && vlimit <= ptv->no.kidx) 505 continue; 506 507 /* Value found. Bump refcount */ 508 ptv->pval->refcnt++; 509 ptei->value = ptv->no.kidx; 510 found++; 511 } 512 513 if (ts->count == found) { 514 /* We've found all values , no need ts create new ones */ 515 return (0); 516 } 517 518 /* 519 * we have added some state here, let's attach operation 520 * state ts the list ts be able ts rollback if necessary. 521 */ 522 add_toperation_state(ch, ts); 523 /* Ensure table won't disappear */ 524 tc_ref(tc); 525 IPFW_UH_WUNLOCK(ch); 526 527 /* 528 * Stage 2: allocate objects for non-existing values. 529 */ 530 for (i = 0; i < count; i++) { 531 ptei = &tei[i]; 532 if (ptei->value != 0) 533 continue; 534 if (ptei->ptv != NULL) 535 continue; 536 ptei->ptv = malloc(sizeof(struct table_val_link), M_IPFW, 537 M_WAITOK | M_ZERO); 538 } 539 540 /* 541 * Stage 3: allocate index numbers for new values 542 * and link them to index. 543 */ 544 IPFW_UH_WLOCK(ch); 545 tc_unref(tc); 546 del_toperation_state(ch, ts); 547 if (ts->modified != 0) { 548 /* 549 * In general, we should free all state/indexes here 550 * and return. However, we keep allocated state instead 551 * to ensure we achieve some progress on each restart. 552 */ 553 return (0); 554 } 555 556 KASSERT(pval == ch->valuestate, ("resize_storage() notify failure")); 557 558 /* Let's try to link values */ 559 for (i = 0; i < count; i++) { 560 ptei = &tei[i]; 561 562 /* Check if record has appeared */ 563 mask_table_value(ptei->pvalue, &tval, ts->vmask); 564 ptv = (struct table_val_link *)ipfw_objhash_lookup_name(vi, 0, 565 (char *)&tval); 566 if (ptv != NULL) { 567 ptv->pval->refcnt++; 568 ptei->value = ptv->no.kidx; 569 continue; 570 } 571 572 /* May perform UH unlock/lock */ 573 error = alloc_table_vidx(ch, ts, vi, &vidx, flags); 574 if (error != 0) { 575 ts->opstate.func(ts->tc, &ts->opstate); 576 return (error); 577 } 578 /* value storage resize has happened, return */ 579 if (ts->modified != 0) 580 return (0); 581 582 /* Finally, we have allocated valid index, let's add entry */ 583 ptei->value = vidx; 584 ptv = (struct table_val_link *)ptei->ptv; 585 ptei->ptv = NULL; 586 587 ptv->no.kidx = vidx; 588 ptv->no.name = (char *)&pval[vidx]; 589 ptv->pval = &pval[vidx]; 590 memcpy(ptv->pval, &tval, sizeof(struct table_value)); 591 pval[vidx].refcnt = 1; 592 ipfw_objhash_add(vi, &ptv->no); 593 } 594 595 return (0); 596 } 597 598 /* 599 * Compatibility function used to import data from old 600 * IP_FW_TABLE_ADD / IP_FW_TABLE_XADD opcodes. 601 */ 602 void 603 ipfw_import_table_value_legacy(uint32_t value, struct table_value *v) 604 { 605 606 memset(v, 0, sizeof(*v)); 607 v->tag = value; 608 v->pipe = value; 609 v->divert = value; 610 v->skipto = value; 611 v->netgraph = value; 612 v->fib = value; 613 v->nat = value; 614 v->nh4 = value; /* host format */ 615 v->dscp = value; 616 v->limit = value; 617 v->mark = value; 618 } 619 620 /* 621 * Export data to legacy table dumps opcodes. 622 */ 623 uint32_t 624 ipfw_export_table_value_legacy(struct table_value *v) 625 { 626 627 /* 628 * TODO: provide more compatibility depending on 629 * vmask value. 630 */ 631 return (v->tag); 632 } 633 634 /* 635 * Imports table value from current userland format. 636 * Saves value in kernel format to the same place. 637 */ 638 void 639 ipfw_import_table_value_v1(ipfw_table_value *iv) 640 { 641 struct table_value v; 642 643 memset(&v, 0, sizeof(v)); 644 v.tag = iv->tag; 645 v.pipe = iv->pipe; 646 v.divert = iv->divert; 647 v.skipto = iv->skipto; 648 v.netgraph = iv->netgraph; 649 v.fib = iv->fib; 650 v.nat = iv->nat; 651 v.dscp = iv->dscp; 652 v.nh4 = iv->nh4; 653 v.nh6 = iv->nh6; 654 v.limit = iv->limit; 655 v.zoneid = iv->zoneid; 656 v.mark = iv->mark; 657 658 memcpy(iv, &v, sizeof(ipfw_table_value)); 659 } 660 661 /* 662 * Export real table value @v to current userland format. 663 * Note that @v and @piv may point to the same memory. 664 */ 665 void 666 ipfw_export_table_value_v1(struct table_value *v, ipfw_table_value *piv) 667 { 668 ipfw_table_value iv; 669 670 memset(&iv, 0, sizeof(iv)); 671 iv.tag = v->tag; 672 iv.pipe = v->pipe; 673 iv.divert = v->divert; 674 iv.skipto = v->skipto; 675 iv.netgraph = v->netgraph; 676 iv.fib = v->fib; 677 iv.nat = v->nat; 678 iv.dscp = v->dscp; 679 iv.limit = v->limit; 680 iv.nh4 = v->nh4; 681 iv.nh6 = v->nh6; 682 iv.zoneid = v->zoneid; 683 iv.mark = v->mark; 684 685 memcpy(piv, &iv, sizeof(iv)); 686 } 687 688 /* 689 * Exports real value data into ipfw_table_value structure including refcnt. 690 */ 691 static int 692 dump_tvalue(struct namedobj_instance *ni, struct named_object *no, void *arg) 693 { 694 struct vdump_args *da; 695 struct table_val_link *ptv; 696 ipfw_table_value *v; 697 698 da = (struct vdump_args *)arg; 699 ptv = (struct table_val_link *)no; 700 701 v = (ipfw_table_value *)ipfw_get_sopt_space(da->sd, sizeof(*v)); 702 /* Out of memory, returning */ 703 if (v == NULL) { 704 da->error = ENOMEM; 705 return (ENOMEM); 706 } 707 708 ipfw_export_table_value_v1(ptv->pval, v); 709 v->refcnt = ptv->pval->refcnt; 710 v->kidx = ptv->no.kidx; 711 return (0); 712 } 713 714 /* 715 * Dumps all shared/table value data 716 * Data layout (v1)(current): 717 * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size 718 * Reply: [ ipfw_obj_lheader ipfw_table_value x N ] 719 * 720 * Returns 0 on success 721 */ 722 static int 723 list_table_values(struct ip_fw_chain *ch, ip_fw3_opheader *op3, 724 struct sockopt_data *sd) 725 { 726 struct _ipfw_obj_lheader *olh; 727 struct namedobj_instance *vi; 728 struct vdump_args da; 729 uint32_t count, size; 730 731 olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh)); 732 if (olh == NULL) 733 return (EINVAL); 734 if (sd->valsize < olh->size) 735 return (EINVAL); 736 737 IPFW_UH_RLOCK(ch); 738 vi = CHAIN_TO_VI(ch); 739 740 count = ipfw_objhash_count(vi); 741 size = count * sizeof(ipfw_table_value) + sizeof(ipfw_obj_lheader); 742 743 /* Fill in header regadless of buffer size */ 744 olh->count = count; 745 olh->objsize = sizeof(ipfw_table_value); 746 747 if (size > olh->size) { 748 olh->size = size; 749 IPFW_UH_RUNLOCK(ch); 750 return (ENOMEM); 751 } 752 olh->size = size; 753 754 /* 755 * Do the actual value dump 756 */ 757 memset(&da, 0, sizeof(da)); 758 da.ch = ch; 759 da.sd = sd; 760 ipfw_objhash_foreach(vi, dump_tvalue, &da); 761 762 IPFW_UH_RUNLOCK(ch); 763 764 return (0); 765 } 766 767 void 768 ipfw_table_value_init(struct ip_fw_chain *ch, int first) 769 { 770 struct tables_config *tcfg; 771 772 ch->valuestate = malloc(VALDATA_START_SIZE * sizeof(struct table_value), 773 M_IPFW, M_WAITOK | M_ZERO); 774 775 tcfg = ch->tblcfg; 776 777 tcfg->val_size = VALDATA_START_SIZE; 778 tcfg->valhash = ipfw_objhash_create(tcfg->val_size); 779 ipfw_objhash_set_funcs(tcfg->valhash, hash_table_value, 780 cmp_table_value); 781 782 IPFW_ADD_SOPT_HANDLER(first, scodes); 783 } 784 785 static int 786 destroy_value(struct namedobj_instance *ni, struct named_object *no, 787 void *arg) 788 { 789 790 free(no, M_IPFW); 791 return (0); 792 } 793 794 void 795 ipfw_table_value_destroy(struct ip_fw_chain *ch, int last) 796 { 797 798 IPFW_DEL_SOPT_HANDLER(last, scodes); 799 800 free(ch->valuestate, M_IPFW); 801 ipfw_objhash_foreach(CHAIN_TO_VI(ch), destroy_value, ch); 802 ipfw_objhash_destroy(CHAIN_TO_VI(ch)); 803 } 804