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