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: projects/ipfw/sys/netpfil/ipfw/ip_fw_table.c 270407 2014-08-23 12:41:39Z melifaro $");
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, void *key,
62     uint32_t kopt);
63 static int cmp_table_value(struct named_object *no, void *key, uint32_t kopt);
64 
65 static int list_table_values(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
66     struct sockopt_data *sd);
67 
68 static struct ipfw_sopt_handler	scodes[] = {
69 	{ IP_FW_TABLE_VLIST,	0,	HDIR_GET,	list_table_values },
70 };
71 
72 #define	CHAIN_TO_VI(chain)	(CHAIN_TO_TCFG(chain)->valhash)
73 
74 struct table_val_link
75 {
76 	struct named_object	no;
77 	struct table_value	*pval;	/* Pointer to real table value */
78 };
79 #define	VALDATA_START_SIZE	64	/* Allocate 64-items array by default */
80 
81 struct vdump_args {
82 	struct ip_fw_chain *ch;
83 	struct sockopt_data *sd;
84 	struct table_value *pval;
85 	int error;
86 };
87 
88 
89 static uint32_t
90 hash_table_value(struct namedobj_instance *ni, 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, 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(dscp, IPFW_VTYPE_DSCP);
118 	_MCPY(nh4, IPFW_VTYPE_NH4);
119 	_MCPY(nh6, 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 void
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 
161 }
162 
163 /*
164  * Grows value storage shared among all tables.
165  * Drops/reacquires UH locks.
166  * Notifies other running adds on @ch shared storage resize.
167  * Note function does not guarantee that free space
168  * will be available after invocation, so one caller needs
169  * to roll cycle himself.
170  *
171  * Returns 0 if case of no errors.
172  */
173 static int
174 resize_shared_value_storage(struct ip_fw_chain *ch)
175 {
176 	struct tables_config *tcfg;
177 	struct namedobj_instance *vi;
178 	struct table_value *pval, *valuestate, *old_valuestate;
179 	void *new_idx;
180 	struct vdump_args da;
181 	int new_blocks;
182 	int val_size, val_size_old;
183 
184 	IPFW_UH_WLOCK_ASSERT(ch);
185 
186 	valuestate = NULL;
187 	new_idx = NULL;
188 
189 	pval = (struct table_value *)ch->valuestate;
190 	vi = CHAIN_TO_VI(ch);
191 	tcfg = CHAIN_TO_TCFG(ch);
192 
193 	val_size = tcfg->val_size * 2;
194 
195 	if (val_size == (1 << 30))
196 		return (ENOSPC);
197 
198 	IPFW_UH_WUNLOCK(ch);
199 
200 	valuestate = malloc(sizeof(struct table_value) * val_size, M_IPFW,
201 	    M_WAITOK | M_ZERO);
202 	ipfw_objhash_bitmap_alloc(val_size, (void *)&new_idx,
203 	    &new_blocks);
204 
205 	IPFW_UH_WLOCK(ch);
206 
207 	/*
208 	 * Check if we still need to resize
209 	 */
210 	if (tcfg->val_size >= val_size)
211 		goto done;
212 
213 	/* Update pointers and notify everyone we're changing @ch */
214 	pval = (struct table_value *)ch->valuestate;
215 	rollback_toperation_state(ch, ch);
216 
217 	/* Good. Let's merge */
218 	memcpy(valuestate, pval, sizeof(struct table_value) * tcfg->val_size);
219 	ipfw_objhash_bitmap_merge(CHAIN_TO_VI(ch), &new_idx, &new_blocks);
220 
221 	IPFW_WLOCK(ch);
222 	/* Change pointers */
223 	old_valuestate = ch->valuestate;
224 	ch->valuestate = valuestate;
225 	valuestate = old_valuestate;
226 	ipfw_objhash_bitmap_swap(CHAIN_TO_VI(ch), &new_idx, &new_blocks);
227 
228 	val_size_old = tcfg->val_size;
229 	tcfg->val_size = val_size;
230 	val_size = val_size_old;
231 	IPFW_WUNLOCK(ch);
232 	/* Update pointers to reflect resize */
233 	memset(&da, 0, sizeof(da));
234 	da.pval = (struct table_value *)ch->valuestate;
235 	ipfw_objhash_foreach(vi, update_tvalue, &da);
236 
237 done:
238 	free(valuestate, M_IPFW);
239 	ipfw_objhash_bitmap_free(new_idx, new_blocks);
240 
241 	return (0);
242 }
243 
244 /*
245  * Drops reference for table value with index @kidx, stored in @pval and
246  * @vi. Frees value if it has no references.
247  */
248 static void
249 unref_table_value(struct namedobj_instance *vi, struct table_value *pval,
250     uint32_t kidx)
251 {
252 	struct table_val_link *ptvl;
253 
254 	if (pval[kidx].refcnt > 1) {
255 		pval[kidx].refcnt--;
256 		return;
257 	}
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 	memset(&fa, 0, sizeof(fa));
311 	fa.ch = ch;
312 	fa.ta = ta;
313 	fa.astate = astate;
314 	fa.ti = ti;
315 
316 	ta->foreach(astate, ti, unref_table_value_cb, &fa);
317 }
318 
319 /*
320  * Table operation state handler.
321  * Called when we are going to change something in @tc which
322  * may lead to inconsistencies in on-going table data addition.
323  *
324  * Here we rollback all already committed state (table values, currently)
325  * and set "modified" field to non-zero value to indicate
326  * that we need to restart original operation.
327  */
328 void
329 rollback_table_values(struct tableop_state *ts)
330 {
331 	struct ip_fw_chain *ch;
332 	struct table_value *pval;
333 	struct tentry_info *ptei;
334 	struct namedobj_instance *vi;
335 	int i;
336 
337 	ch = ts->ch;
338 
339 	IPFW_UH_WLOCK_ASSERT(ch);
340 
341 	/* Get current table value pointer */
342 	get_value_ptrs(ch, ts->tc, ts->vshared, &pval, &vi);
343 
344 	for (i = 0; i < ts->count; i++) {
345 		ptei = &ts->tei[i];
346 
347 		if (ptei->value == 0)
348 			continue;
349 
350 		unref_table_value(vi, pval, ptei->value);
351 	}
352 }
353 
354 /*
355  * Allocate new value index in either shared or per-table array.
356  * Function may drop/reacquire UH lock.
357  *
358  * Returns 0 on success.
359  */
360 static int
361 alloc_table_vidx(struct ip_fw_chain *ch, struct tableop_state *ts,
362     struct namedobj_instance *vi, uint16_t *pvidx)
363 {
364 	int error, vlimit;
365 	uint16_t vidx;
366 
367 	IPFW_UH_WLOCK_ASSERT(ch);
368 
369 	error = ipfw_objhash_alloc_idx(vi, &vidx);
370 	if (error != 0) {
371 
372 		/*
373 		 * We need to resize array. This involves
374 		 * lock/unlock, so we need to check "modified"
375 		 * state.
376 		 */
377 		ts->opstate.func(ts->tc, &ts->opstate);
378 		error = resize_shared_value_storage(ch);
379 		return (error); /* ts->modified should be set, we will restart */
380 	}
381 
382 	vlimit = ts->ta->vlimit;
383 	if (vlimit != 0 && vidx >= vlimit) {
384 
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 		 * TODO: do not rollback state if
392 		 * atomicity is not required.
393 		 */
394 		if (ts->vshared != 0) {
395 			/* shared -> per-table  */
396 			return (ENOSPC); /* TODO: proper error */
397 		}
398 
399 		/* per-table. Fail for now. */
400 		return (ENOSPC); /* TODO: proper error */
401 	}
402 
403 	*pvidx = vidx;
404 	return (0);
405 }
406 
407 /*
408  * Drops value reference for unused values (updates, deletes, partially
409  * successful adds or rollbacks).
410  */
411 void
412 ipfw_garbage_table_values(struct ip_fw_chain *ch, struct table_config *tc,
413     struct tentry_info *tei, uint32_t count, int rollback)
414 {
415 	int i;
416 	struct tentry_info *ptei;
417 	struct table_value *pval;
418 	struct namedobj_instance *vi;
419 
420 	/*
421 	 * We have two slightly different ADD cases here:
422 	 * either (1) we are successful / partially successful,
423 	 * in that case we need
424 	 * * to ignore ADDED entries values
425 	 * * rollback every other values (either UPDATED since
426 	 *   old value has been stored there, or some failure like
427 	 *   EXISTS or LIMIT 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 			/*
448 			 * We may be deleting non-existing record.
449 			 * Skip.
450 			 */
451 			continue;
452 		}
453 
454 		if ((ptei->flags & TEI_FLAGS_ADDED) != 0 && rollback == 0) {
455 			ptei->value = 0;
456 			continue;
457 		}
458 
459 		unref_table_value(vi, pval, ptei->value);
460 		ptei->value = 0;
461 	}
462 }
463 
464 /*
465  * Main function used to link values of entries going to be added,
466  * to the index. Since we may perform many UH locks drops/acquires,
467  * handle changes by checking tablestate "modified" field.
468  *
469  * Success: return 0.
470  */
471 int
472 ipfw_link_table_values(struct ip_fw_chain *ch, struct tableop_state *ts)
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 	tc = ts->tc;
494 	tei = ts->tei;
495 	count = ts->count;
496 	for (i = 0; i < count; i++) {
497 		ptei = &tei[i];
498 		ptei->value = 0; /* Ensure value is always 0 in the beginnig */
499 		mask_table_value(ptei->pvalue, &tval, ts->vmask);
500 		ptv = (struct table_val_link *)ipfw_objhash_lookup_name(vi, 0,
501 		    (char *)&tval);
502 		if (ptv == NULL)
503 			continue;
504 		/* Deal with vlimit later */
505 		if (vlimit > 0 && vlimit <= ptv->no.kidx)
506 			continue;
507 
508 		/* Value found. Bump refcount */
509 		ptv->pval->refcnt++;
510 		ptei->value = ptv->no.kidx;
511 		found++;
512 	}
513 
514 	if (ts->count == found) {
515 		/* We've found all values , no need ts create new ones */
516 		return (0);
517 	}
518 
519 	/*
520 	 * we have added some state here, let's attach operation
521 	 * state ts the list ts be able ts rollback if necessary.
522 	 */
523 	add_toperation_state(ch, ts);
524 	/* Ensure table won't disappear */
525 	tc_ref(tc);
526 	IPFW_UH_WUNLOCK(ch);
527 
528 	/*
529 	 * Stage 2: allocate objects for non-existing values.
530 	 */
531 	for (i = 0; i < count; i++) {
532 		ptei = &tei[i];
533 		if (ptei->value != 0)
534 			continue;
535 		if (ptei->ptv != NULL)
536 			continue;
537 		ptei->ptv = malloc(sizeof(struct table_val_link), M_IPFW,
538 		    M_WAITOK | M_ZERO);
539 	}
540 
541 	/*
542 	 * Stage 3: allocate index numbers for new values
543 	 * and link them to index.
544 	 */
545 	IPFW_UH_WLOCK(ch);
546 	tc_unref(tc);
547 	del_toperation_state(ch, ts);
548 	if (ts->modified != 0)
549 		return (0);
550 
551 	KASSERT(pval == ch->tablestate, ("resize_storage() notify failure"));
552 
553 	/* Let's try to link values */
554 	for (i = 0; i < count; i++) {
555 		ptei = &tei[i];
556 		if (ptei->value != 0)
557 			continue;
558 
559 		/* Check if record has appeared */
560 		mask_table_value(ptei->pvalue, &tval, ts->vmask);
561 		ptv = (struct table_val_link *)ipfw_objhash_lookup_name(vi, 0,
562 		    (char *)&tval);
563 		if (ptv != NULL) {
564 			ptv->pval->refcnt++;
565 			ptei->value = ptv->no.kidx;
566 			continue;
567 		}
568 
569 		/* May perform UH unlock/lock */
570 		error = alloc_table_vidx(ch, ts, vi, &vidx);
571 		if (error != 0) {
572 			ts->opstate.func(ts->tc, &ts->opstate);
573 			return (error);
574 		}
575 		/* value storage resize has happened, return */
576 		if (ts->modified != 0)
577 			return (0);
578 
579 		/* Finally, we have allocated valid index, let's add entry */
580 		ptei->value = vidx;
581 		ptv = (struct table_val_link *)ptei->ptv;
582 		ptei->ptv = NULL;
583 
584 		ptv->no.kidx = vidx;
585 		ptv->no.name = (char *)&pval[vidx];
586 		ptv->pval = &pval[vidx];
587 		memcpy(ptv->pval, &tval, sizeof(struct table_value));
588 		pval[vidx].refcnt = 1;
589 		ipfw_objhash_add(vi, &ptv->no);
590 	}
591 
592 	return (0);
593 }
594 
595 /*
596  * Compability function used to import data from old
597  * IP_FW_TABLE_ADD / IP_FW_TABLE_XADD opcodes.
598  */
599 void
600 ipfw_import_table_value_legacy(uint32_t value, struct table_value *v)
601 {
602 
603 	memset(v, 0, sizeof(*v));
604 	v->tag = value;
605 	v->pipe = value;
606 	v->divert = value;
607 	v->skipto = value;
608 	v->netgraph = value;
609 	v->fib = value;
610 	v->nat = value;
611 	v->nh4 = value; /* host format */
612 	v->dscp = value;
613 	v->limit = value;
614 }
615 
616 /*
617  * Export data to legacy table dumps opcodes.
618  */
619 uint32_t
620 ipfw_export_table_value_legacy(struct table_value *v)
621 {
622 
623 	/*
624 	 * TODO: provide more compatibility depending on
625 	 * vmask value.
626 	 */
627 	return (v->tag);
628 }
629 
630 /*
631  * Imports table value from current userland format.
632  * Saves value in kernel format to the same place.
633  */
634 void
635 ipfw_import_table_value_v1(ipfw_table_value *iv)
636 {
637 	struct table_value v;
638 
639 	memset(&v, 0, sizeof(v));
640 	v.tag = iv->tag;
641 	v.pipe = iv->pipe;
642 	v.divert = iv->divert;
643 	v.skipto = iv->skipto;
644 	v.netgraph = iv->netgraph;
645 	v.fib = iv->fib;
646 	v.nat = iv->nat;
647 	v.dscp = iv->dscp;
648 	v.nh4 = iv->nh4;
649 	v.nh6 = iv->nh6;
650 	v.limit = iv->limit;
651 
652 	memcpy(iv, &v, sizeof(ipfw_table_value));
653 }
654 
655 /*
656  * Export real table value @v to current userland format.
657  * Note that @v and @piv may point to the same memory.
658  */
659 void
660 ipfw_export_table_value_v1(struct table_value *v, ipfw_table_value *piv)
661 {
662 	ipfw_table_value iv;
663 
664 	memset(&iv, 0, sizeof(iv));
665 	iv.tag = v->tag;
666 	iv.pipe = v->pipe;
667 	iv.divert = v->divert;
668 	iv.skipto = v->skipto;
669 	iv.netgraph = v->netgraph;
670 	iv.fib = v->fib;
671 	iv.nat = v->nat;
672 	iv.dscp = v->dscp;
673 	iv.limit = v->limit;
674 	iv.nh4 = v->nh4;
675 	iv.nh6 = v->nh6;
676 
677 	memcpy(piv, &iv, sizeof(iv));
678 }
679 
680 /*
681  * Exports real value data into ipfw_table_value structure.
682  * Utilizes "spare1" field to store kernel index.
683  */
684 static void
685 dump_tvalue(struct namedobj_instance *ni, struct named_object *no, void *arg)
686 {
687 	struct vdump_args *da;
688 	struct table_val_link *ptv;
689 	struct table_value *v;
690 
691 	da = (struct vdump_args *)arg;
692 	ptv = (struct table_val_link *)no;
693 
694 	v = (struct table_value *)ipfw_get_sopt_space(da->sd, sizeof(*v));
695 	/* Out of memory, returning */
696 	if (v == NULL) {
697 		da->error = ENOMEM;
698 		return;
699 	}
700 
701 	memcpy(v, ptv->pval, sizeof(*v));
702 	v->spare1 = ptv->no.kidx;
703 }
704 
705 /*
706  * Dumps all shared/table value data
707  * Data layout (v1)(current):
708  * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
709  * Reply: [ ipfw_obj_lheader ipfw_table_value x N ]
710  *
711  * Returns 0 on success
712  */
713 static int
714 list_table_values(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
715     struct sockopt_data *sd)
716 {
717 	struct _ipfw_obj_lheader *olh;
718 	struct namedobj_instance *vi;
719 	struct vdump_args da;
720 	uint32_t count, size;
721 
722 	olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
723 	if (olh == NULL)
724 		return (EINVAL);
725 	if (sd->valsize < olh->size)
726 		return (EINVAL);
727 
728 	IPFW_UH_RLOCK(ch);
729 	vi = CHAIN_TO_VI(ch);
730 
731 	count = ipfw_objhash_count(vi);
732 	size = count * sizeof(ipfw_table_value) + sizeof(ipfw_obj_lheader);
733 
734 	/* Fill in header regadless of buffer size */
735 	olh->count = count;
736 	olh->objsize = sizeof(ipfw_table_value);
737 
738 	if (size > olh->size) {
739 		olh->size = size;
740 		IPFW_UH_RUNLOCK(ch);
741 		return (ENOMEM);
742 	}
743 	olh->size = size;
744 
745 	/*
746 	 * Do the actual value dump
747 	 */
748 	memset(&da, 0, sizeof(da));
749 	da.ch = ch;
750 	da.sd = sd;
751 	ipfw_objhash_foreach(vi, dump_tvalue, &da);
752 
753 	IPFW_UH_RUNLOCK(ch);
754 
755 	return (0);
756 }
757 
758 void
759 ipfw_table_value_init(struct ip_fw_chain *ch, int first)
760 {
761 	struct tables_config *tcfg;
762 
763 	ch->valuestate = malloc(VALDATA_START_SIZE * sizeof(struct table_value),
764 	    M_IPFW, M_WAITOK | M_ZERO);
765 
766 	tcfg = ch->tblcfg;
767 
768 	tcfg->val_size = VALDATA_START_SIZE;
769 	tcfg->valhash = ipfw_objhash_create(tcfg->val_size);
770 	ipfw_objhash_set_funcs(tcfg->valhash, hash_table_value,
771 	    cmp_table_value);
772 
773 	IPFW_ADD_SOPT_HANDLER(first, scodes);
774 }
775 
776 static void
777 destroy_value(struct namedobj_instance *ni, struct named_object *no,
778     void *arg)
779 {
780 
781 	free(no, M_IPFW);
782 }
783 
784 void
785 ipfw_table_value_destroy(struct ip_fw_chain *ch, int last)
786 {
787 
788 	IPFW_DEL_SOPT_HANDLER(last, scodes);
789 
790 	free(ch->valuestate, M_IPFW);
791 	ipfw_objhash_foreach(CHAIN_TO_VI(ch), destroy_value, ch);
792 	ipfw_objhash_destroy(CHAIN_TO_VI(ch));
793 }
794 
795