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