npf_tableset.c revision 1.32 1 /*-
2 * Copyright (c) 2009-2018 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This material is based upon work partially supported by The
6 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * NPF tableset module.
32 *
33 * Notes
34 *
35 * The tableset is an array of tables. After the creation, the array
36 * is immutable. The caller is responsible to synchronise the access
37 * to the tableset.
38 */
39
40 #ifdef _KERNEL
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.32 2019/06/20 17:12:37 christos Exp $");
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46
47 #include <sys/atomic.h>
48 #include <sys/cdbr.h>
49 #include <sys/kmem.h>
50 #include <sys/pool.h>
51 #include <sys/queue.h>
52 #include <sys/mutex.h>
53 #include <sys/thmap.h>
54
55 #include "lpm.h"
56 #endif
57
58 #include "npf_impl.h"
59
60 typedef struct npf_tblent {
61 LIST_ENTRY(npf_tblent) te_listent;
62 uint16_t te_preflen;
63 uint16_t te_alen;
64 npf_addr_t te_addr;
65 } npf_tblent_t;
66
67 #define NPF_ADDRLEN2IDX(alen) ((alen) >> 4)
68 #define NPF_ADDR_SLOTS (2)
69
70 struct npf_table {
71 /*
72 * The storage type can be: a) hashmap b) LPM c) cdb.
73 * There are separate trees for IPv4 and IPv6.
74 */
75 union {
76 struct {
77 thmap_t * t_map;
78 LIST_HEAD(, npf_tblent) t_gc;
79 };
80 lpm_t * t_lpm;
81 struct {
82 void * t_blob;
83 size_t t_bsize;
84 struct cdbr * t_cdb;
85 };
86 struct {
87 npf_tblent_t ** t_elements[NPF_ADDR_SLOTS];
88 unsigned t_allocated[NPF_ADDR_SLOTS];
89 unsigned t_used[NPF_ADDR_SLOTS];
90 };
91 } /* C11 */;
92 LIST_HEAD(, npf_tblent) t_list;
93 unsigned t_nitems;
94
95 /*
96 * Table ID, type and lock. The ID may change during the
97 * config reload, it is protected by the npf_config_lock.
98 */
99 int t_type;
100 unsigned t_id;
101 kmutex_t t_lock;
102
103 /* Reference count and table name. */
104 unsigned t_refcnt;
105 char t_name[NPF_TABLE_MAXNAMELEN];
106 };
107
108 struct npf_tableset {
109 unsigned ts_nitems;
110 npf_table_t * ts_map[];
111 };
112
113 #define NPF_TABLESET_SIZE(n) \
114 (offsetof(npf_tableset_t, ts_map[n]) * sizeof(npf_table_t *))
115
116 #define NPF_IFADDR_STEP 4
117
118 static pool_cache_t tblent_cache __read_mostly;
119
120 /*
121 * npf_table_sysinit: initialise tableset structures.
122 */
123 void
124 npf_tableset_sysinit(void)
125 {
126 tblent_cache = pool_cache_init(sizeof(npf_tblent_t), 0,
127 0, 0, "npftblpl", NULL, IPL_NONE, NULL, NULL, NULL);
128 }
129
130 void
131 npf_tableset_sysfini(void)
132 {
133 pool_cache_destroy(tblent_cache);
134 }
135
136 npf_tableset_t *
137 npf_tableset_create(u_int nitems)
138 {
139 npf_tableset_t *ts = kmem_zalloc(NPF_TABLESET_SIZE(nitems), KM_SLEEP);
140 ts->ts_nitems = nitems;
141 return ts;
142 }
143
144 void
145 npf_tableset_destroy(npf_tableset_t *ts)
146 {
147 /*
148 * Destroy all tables (no references should be held, since the
149 * ruleset should be destroyed before).
150 */
151 for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
152 npf_table_t *t = ts->ts_map[tid];
153
154 if (t && atomic_dec_uint_nv(&t->t_refcnt) == 0) {
155 npf_table_destroy(t);
156 }
157 }
158 kmem_free(ts, NPF_TABLESET_SIZE(ts->ts_nitems));
159 }
160
161 /*
162 * npf_tableset_insert: insert the table into the specified tableset.
163 *
164 * => Returns 0 on success. Fails and returns error if ID is already used.
165 */
166 int
167 npf_tableset_insert(npf_tableset_t *ts, npf_table_t *t)
168 {
169 const u_int tid = t->t_id;
170 int error;
171
172 KASSERT((u_int)tid < ts->ts_nitems);
173
174 if (ts->ts_map[tid] == NULL) {
175 atomic_inc_uint(&t->t_refcnt);
176 ts->ts_map[tid] = t;
177 error = 0;
178 } else {
179 error = EEXIST;
180 }
181 return error;
182 }
183
184 npf_table_t *
185 npf_tableset_swap(npf_tableset_t *ts, npf_table_t *newt)
186 {
187 const u_int tid = newt->t_id;
188 npf_table_t *oldt = ts->ts_map[tid];
189
190 KASSERT(tid < ts->ts_nitems);
191 KASSERT(oldt->t_id == newt->t_id);
192
193 newt->t_refcnt = oldt->t_refcnt;
194 oldt->t_refcnt = 0;
195
196 return atomic_swap_ptr(&ts->ts_map[tid], newt);
197 }
198
199 /*
200 * npf_tableset_getbyname: look for a table in the set given the name.
201 */
202 npf_table_t *
203 npf_tableset_getbyname(npf_tableset_t *ts, const char *name)
204 {
205 npf_table_t *t;
206
207 for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
208 if ((t = ts->ts_map[tid]) == NULL)
209 continue;
210 if (strcmp(name, t->t_name) == 0)
211 return t;
212 }
213 return NULL;
214 }
215
216 npf_table_t *
217 npf_tableset_getbyid(npf_tableset_t *ts, u_int tid)
218 {
219 if (__predict_true(tid < ts->ts_nitems)) {
220 return ts->ts_map[tid];
221 }
222 return NULL;
223 }
224
225 /*
226 * npf_tableset_reload: iterate all tables and if the new table is of the
227 * same type and has no items, then we preserve the old one and its entries.
228 *
229 * => The caller is responsible for providing synchronisation.
230 */
231 void
232 npf_tableset_reload(npf_t *npf, npf_tableset_t *nts, npf_tableset_t *ots)
233 {
234 for (u_int tid = 0; tid < nts->ts_nitems; tid++) {
235 npf_table_t *t, *ot;
236
237 if ((t = nts->ts_map[tid]) == NULL) {
238 continue;
239 }
240
241 /* If our table has entries, just load it. */
242 if (t->t_nitems) {
243 continue;
244 }
245
246 /* Look for a currently existing table with such name. */
247 ot = npf_tableset_getbyname(ots, t->t_name);
248 if (ot == NULL) {
249 /* Not found: we have a new table. */
250 continue;
251 }
252
253 /* Found. Did the type change? */
254 if (t->t_type != ot->t_type) {
255 /* Yes, load the new. */
256 continue;
257 }
258
259 /*
260 * Preserve the current table. Acquire a reference since
261 * we are keeping it in the old table set. Update its ID.
262 */
263 atomic_inc_uint(&ot->t_refcnt);
264 nts->ts_map[tid] = ot;
265
266 KASSERT(npf_config_locked_p(npf));
267 ot->t_id = tid;
268
269 /* Destroy the new table (we hold the only reference). */
270 t->t_refcnt--;
271 npf_table_destroy(t);
272 }
273 }
274
275 int
276 npf_tableset_export(npf_t *npf, const npf_tableset_t *ts, nvlist_t *npf_dict)
277 {
278 const npf_table_t *t;
279
280 KASSERT(npf_config_locked_p(npf));
281
282 for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
283 nvlist_t *table;
284
285 if ((t = ts->ts_map[tid]) == NULL) {
286 continue;
287 }
288 table = nvlist_create(0);
289 nvlist_add_string(table, "name", t->t_name);
290 nvlist_add_number(table, "type", t->t_type);
291 nvlist_add_number(table, "id", tid);
292
293 nvlist_append_nvlist_array(npf_dict, "tables", table);
294 nvlist_destroy(table);
295 }
296 return 0;
297 }
298
299 /*
300 * Few helper routines.
301 */
302
303 static void
304 table_ipset_flush(npf_table_t *t)
305 {
306 npf_tblent_t *ent;
307
308 while ((ent = LIST_FIRST(&t->t_list)) != NULL) {
309 thmap_del(t->t_map, &ent->te_addr, ent->te_alen);
310 LIST_REMOVE(ent, te_listent);
311 pool_cache_put(tblent_cache, ent);
312 }
313 t->t_nitems = 0;
314 }
315
316 static void
317 table_tree_flush(npf_table_t *t)
318 {
319 npf_tblent_t *ent;
320
321 while ((ent = LIST_FIRST(&t->t_list)) != NULL) {
322 LIST_REMOVE(ent, te_listent);
323 pool_cache_put(tblent_cache, ent);
324 }
325 lpm_clear(t->t_lpm, NULL, NULL);
326 t->t_nitems = 0;
327 }
328
329 static void
330 table_ifaddr_flush(npf_table_t *t)
331 {
332 npf_tblent_t *ent;
333
334 for (unsigned i = 0; i < NPF_ADDR_SLOTS; i++) {
335 size_t len;
336
337 if (!t->t_allocated[i]) {
338 KASSERT(t->t_elements[i] == NULL);
339 continue;
340 }
341 len = t->t_allocated[i] * sizeof(npf_tblent_t *);
342 kmem_free(t->t_elements[i], len);
343 t->t_elements[i] = NULL;
344 t->t_allocated[i] = 0;
345 t->t_used[i] = 0;
346 }
347 while ((ent = LIST_FIRST(&t->t_list)) != NULL) {
348 LIST_REMOVE(ent, te_listent);
349 pool_cache_put(tblent_cache, ent);
350 }
351 t->t_nitems = 0;
352 }
353
354 /*
355 * npf_table_create: create table with a specified ID.
356 */
357 npf_table_t *
358 npf_table_create(const char *name, u_int tid, int type,
359 const void *blob, size_t size)
360 {
361 npf_table_t *t;
362
363 t = kmem_zalloc(sizeof(npf_table_t), KM_SLEEP);
364 strlcpy(t->t_name, name, NPF_TABLE_MAXNAMELEN);
365
366 switch (type) {
367 case NPF_TABLE_LPM:
368 t->t_lpm = lpm_create(KM_NOSLEEP);
369 if (t->t_lpm == NULL) {
370 goto out;
371 }
372 LIST_INIT(&t->t_list);
373 break;
374 case NPF_TABLE_IPSET:
375 t->t_map = thmap_create(0, NULL, THMAP_NOCOPY);
376 if (t->t_map == NULL) {
377 goto out;
378 }
379 break;
380 case NPF_TABLE_CONST:
381 t->t_blob = kmem_alloc(size, KM_SLEEP);
382 if (t->t_blob == NULL) {
383 goto out;
384 }
385 memcpy(t->t_blob, blob, size);
386 t->t_bsize = size;
387
388 t->t_cdb = cdbr_open_mem(t->t_blob, size,
389 CDBR_DEFAULT, NULL, NULL);
390 if (t->t_cdb == NULL) {
391 kmem_free(t->t_blob, t->t_bsize);
392 goto out;
393 }
394 t->t_nitems = cdbr_entries(t->t_cdb);
395 break;
396 case NPF_TABLE_IFADDR:
397 break;
398 default:
399 KASSERT(false);
400 }
401 mutex_init(&t->t_lock, MUTEX_DEFAULT, IPL_NET);
402 t->t_type = type;
403 t->t_id = tid;
404 return t;
405 out:
406 kmem_free(t, sizeof(npf_table_t));
407 return NULL;
408 }
409
410 /*
411 * npf_table_destroy: free all table entries and table itself.
412 */
413 void
414 npf_table_destroy(npf_table_t *t)
415 {
416 KASSERT(t->t_refcnt == 0);
417
418 switch (t->t_type) {
419 case NPF_TABLE_IPSET:
420 table_ipset_flush(t);
421 npf_table_gc(NULL, t);
422 thmap_destroy(t->t_map);
423 break;
424 case NPF_TABLE_LPM:
425 table_tree_flush(t);
426 lpm_destroy(t->t_lpm);
427 break;
428 case NPF_TABLE_CONST:
429 cdbr_close(t->t_cdb);
430 kmem_free(t->t_blob, t->t_bsize);
431 break;
432 case NPF_TABLE_IFADDR:
433 table_ifaddr_flush(t);
434 break;
435 default:
436 KASSERT(false);
437 }
438 mutex_destroy(&t->t_lock);
439 kmem_free(t, sizeof(npf_table_t));
440 }
441
442 u_int
443 npf_table_getid(npf_table_t *t)
444 {
445 return t->t_id;
446 }
447
448 /*
449 * npf_table_check: validate the name, ID and type.
450 */
451 int
452 npf_table_check(npf_tableset_t *ts, const char *name, uint64_t tid, uint64_t type)
453 {
454 if (tid >= ts->ts_nitems) {
455 return EINVAL;
456 }
457 if (ts->ts_map[tid] != NULL) {
458 return EEXIST;
459 }
460 switch (type) {
461 case NPF_TABLE_LPM:
462 case NPF_TABLE_IPSET:
463 case NPF_TABLE_CONST:
464 case NPF_TABLE_IFADDR:
465 break;
466 default:
467 return EINVAL;
468 }
469 if (strlen(name) >= NPF_TABLE_MAXNAMELEN) {
470 return ENAMETOOLONG;
471 }
472 if (npf_tableset_getbyname(ts, name)) {
473 return EEXIST;
474 }
475 return 0;
476 }
477
478 static int
479 table_cidr_check(int alen, const npf_addr_t *addr, npf_netmask_t mask)
480 {
481 switch (alen) {
482 case sizeof(struct in_addr):
483 if (__predict_false(mask > 32 && mask != NPF_NO_NETMASK)) {
484 return EINVAL;
485 }
486 break;
487 case sizeof(struct in6_addr):
488 if (__predict_false(mask > 128 && mask != NPF_NO_NETMASK)) {
489 return EINVAL;
490 }
491 break;
492 default:
493 return EINVAL;
494 }
495 return 0;
496 }
497
498 static int
499 table_ifaddr_insert(npf_table_t *t, const int alen, npf_tblent_t *ent)
500 {
501 const unsigned aidx = NPF_ADDRLEN2IDX(alen);
502 const unsigned allocated = t->t_allocated[aidx];
503 const unsigned used = t->t_used[aidx];
504
505 /*
506 * No need to check for duplicates.
507 */
508 if (allocated <= used) {
509 npf_tblent_t **old_elements = t->t_elements[aidx];
510 npf_tblent_t **elements;
511 size_t toalloc, newsize;
512
513 toalloc = roundup2(allocated + 1, NPF_IFADDR_STEP);
514 newsize = toalloc * sizeof(npf_tblent_t *);
515
516 elements = kmem_zalloc(newsize, KM_NOSLEEP);
517 if (elements == NULL) {
518 return ENOMEM;
519 }
520 for (unsigned i = 0; i < used; i++) {
521 elements[i] = old_elements[i];
522 }
523 if (allocated) {
524 const size_t len = allocated * sizeof(npf_tblent_t *);
525 KASSERT(old_elements != NULL);
526 kmem_free(old_elements, len);
527 }
528 t->t_elements[aidx] = elements;
529 t->t_allocated[aidx] = toalloc;
530 }
531 t->t_elements[aidx][used] = ent;
532 t->t_used[aidx]++;
533 return 0;
534 }
535
536 /*
537 * npf_table_insert: add an IP CIDR entry into the table.
538 */
539 int
540 npf_table_insert(npf_table_t *t, const int alen,
541 const npf_addr_t *addr, const npf_netmask_t mask)
542 {
543 npf_tblent_t *ent;
544 int error;
545
546 error = table_cidr_check(alen, addr, mask);
547 if (error) {
548 return error;
549 }
550 ent = pool_cache_get(tblent_cache, PR_WAITOK);
551 memcpy(&ent->te_addr, addr, alen);
552 ent->te_alen = alen;
553 ent->te_preflen = 0;
554
555 /*
556 * Insert the entry. Return an error on duplicate.
557 */
558 mutex_enter(&t->t_lock);
559 switch (t->t_type) {
560 case NPF_TABLE_IPSET:
561 /*
562 * Hashmap supports only IPs.
563 *
564 * Note: the key must be already persistent, since we
565 * use THMAP_NOCOPY.
566 */
567 if (mask != NPF_NO_NETMASK) {
568 error = EINVAL;
569 break;
570 }
571 if (thmap_put(t->t_map, &ent->te_addr, alen, ent) == ent) {
572 LIST_INSERT_HEAD(&t->t_list, ent, te_listent);
573 t->t_nitems++;
574 } else {
575 error = EEXIST;
576 }
577 break;
578 case NPF_TABLE_LPM: {
579 const unsigned preflen =
580 (mask == NPF_NO_NETMASK) ? (alen * 8) : mask;
581 ent->te_preflen = preflen;
582
583 if (lpm_lookup(t->t_lpm, addr, alen) == NULL &&
584 lpm_insert(t->t_lpm, addr, alen, preflen, ent) == 0) {
585 LIST_INSERT_HEAD(&t->t_list, ent, te_listent);
586 t->t_nitems++;
587 error = 0;
588 } else {
589 error = EEXIST;
590 }
591 break;
592 }
593 case NPF_TABLE_CONST:
594 error = EINVAL;
595 break;
596 case NPF_TABLE_IFADDR:
597 if ((error = table_ifaddr_insert(t, alen, ent)) != 0) {
598 break;
599 }
600 LIST_INSERT_HEAD(&t->t_list, ent, te_listent);
601 t->t_nitems++;
602 break;
603 default:
604 KASSERT(false);
605 }
606 mutex_exit(&t->t_lock);
607
608 if (error) {
609 pool_cache_put(tblent_cache, ent);
610 }
611 return error;
612 }
613
614 /*
615 * npf_table_remove: remove the IP CIDR entry from the table.
616 */
617 int
618 npf_table_remove(npf_table_t *t, const int alen,
619 const npf_addr_t *addr, const npf_netmask_t mask)
620 {
621 npf_tblent_t *ent = NULL;
622 int error;
623
624 error = table_cidr_check(alen, addr, mask);
625 if (error) {
626 return error;
627 }
628
629 mutex_enter(&t->t_lock);
630 switch (t->t_type) {
631 case NPF_TABLE_IPSET:
632 ent = thmap_del(t->t_map, addr, alen);
633 if (__predict_true(ent != NULL)) {
634 LIST_REMOVE(ent, te_listent);
635 LIST_INSERT_HEAD(&t->t_gc, ent, te_listent);
636 ent = NULL; // to be G/C'ed
637 t->t_nitems--;
638 } else {
639 error = ENOENT;
640 }
641 break;
642 case NPF_TABLE_LPM:
643 ent = lpm_lookup(t->t_lpm, addr, alen);
644 if (__predict_true(ent != NULL)) {
645 LIST_REMOVE(ent, te_listent);
646 lpm_remove(t->t_lpm, &ent->te_addr,
647 ent->te_alen, ent->te_preflen);
648 t->t_nitems--;
649 } else {
650 error = ENOENT;
651 }
652 break;
653 case NPF_TABLE_CONST:
654 case NPF_TABLE_IFADDR:
655 error = EINVAL;
656 break;
657 default:
658 KASSERT(false);
659 ent = NULL;
660 }
661 mutex_exit(&t->t_lock);
662
663 if (ent) {
664 pool_cache_put(tblent_cache, ent);
665 }
666 return error;
667 }
668
669 /*
670 * npf_table_lookup: find the table according to ID, lookup and match
671 * the contents with the specified IP address.
672 */
673 int
674 npf_table_lookup(npf_table_t *t, const int alen, const npf_addr_t *addr)
675 {
676 const void *data;
677 size_t dlen;
678 bool found;
679 int error;
680
681 error = table_cidr_check(alen, addr, NPF_NO_NETMASK);
682 if (error) {
683 return error;
684 }
685
686 switch (t->t_type) {
687 case NPF_TABLE_IPSET:
688 found = thmap_get(t->t_map, addr, alen) != NULL;
689 break;
690 case NPF_TABLE_LPM:
691 mutex_enter(&t->t_lock);
692 found = lpm_lookup(t->t_lpm, addr, alen) != NULL;
693 mutex_exit(&t->t_lock);
694 break;
695 case NPF_TABLE_CONST:
696 if (cdbr_find(t->t_cdb, addr, alen, &data, &dlen) == 0) {
697 found = dlen == (unsigned)alen &&
698 memcmp(addr, data, dlen) == 0;
699 } else {
700 found = false;
701 }
702 break;
703 case NPF_TABLE_IFADDR: {
704 const unsigned aidx = NPF_ADDRLEN2IDX(alen);
705
706 found = false;
707 for (unsigned i = 0; i < t->t_used[aidx]; i++) {
708 const npf_tblent_t *elm = t->t_elements[aidx][i];
709
710 KASSERT(elm->te_alen == alen);
711
712 if (memcmp(&elm->te_addr, addr, alen) == 0) {
713 found = true;
714 break;
715 }
716 }
717 break;
718 }
719 default:
720 KASSERT(false);
721 found = false;
722 }
723
724 return found ? 0 : ENOENT;
725 }
726
727 npf_addr_t *
728 npf_table_getsome(npf_table_t *t, const int alen, unsigned idx)
729 {
730 const unsigned aidx = NPF_ADDRLEN2IDX(alen);
731 npf_tblent_t *elm;
732 unsigned nitems;
733
734 KASSERT(t->t_type == NPF_TABLE_IFADDR);
735 KASSERT(aidx < NPF_ADDR_SLOTS);
736
737 nitems = t->t_used[aidx];
738 if (nitems == 0) {
739 return NULL;
740 }
741
742 /*
743 * No need to acquire the lock, since the table is immutable.
744 */
745 elm = t->t_elements[aidx][idx % nitems];
746 return &elm->te_addr;
747 }
748
749 static int
750 table_ent_copyout(const npf_addr_t *addr, const int alen, npf_netmask_t mask,
751 void *ubuf, size_t len, size_t *off)
752 {
753 void *ubufp = (uint8_t *)ubuf + *off;
754 npf_ioctl_ent_t uent;
755
756 if ((*off += sizeof(npf_ioctl_ent_t)) > len) {
757 return ENOMEM;
758 }
759 uent.alen = alen;
760 memcpy(&uent.addr, addr, sizeof(npf_addr_t));
761 uent.mask = mask;
762
763 return copyout(&uent, ubufp, sizeof(npf_ioctl_ent_t));
764 }
765
766 static int
767 table_generic_list(const npf_table_t *t, void *ubuf, size_t len)
768 {
769 npf_tblent_t *ent;
770 size_t off = 0;
771 int error = 0;
772
773 LIST_FOREACH(ent, &t->t_list, te_listent) {
774 error = table_ent_copyout(&ent->te_addr,
775 ent->te_alen, ent->te_preflen, ubuf, len, &off);
776 if (error)
777 break;
778 }
779 return error;
780 }
781
782 static int
783 table_cdb_list(npf_table_t *t, void *ubuf, size_t len)
784 {
785 size_t off = 0, dlen;
786 const void *data;
787 int error = 0;
788
789 for (size_t i = 0; i < t->t_nitems; i++) {
790 if (cdbr_get(t->t_cdb, i, &data, &dlen) != 0) {
791 return EINVAL;
792 }
793 error = table_ent_copyout(data, dlen, 0, ubuf, len, &off);
794 if (error)
795 break;
796 }
797 return error;
798 }
799
800 /*
801 * npf_table_list: copy a list of all table entries into a userspace buffer.
802 */
803 int
804 npf_table_list(npf_table_t *t, void *ubuf, size_t len)
805 {
806 int error = 0;
807
808 mutex_enter(&t->t_lock);
809 switch (t->t_type) {
810 case NPF_TABLE_IPSET:
811 error = table_generic_list(t, ubuf, len);
812 break;
813 case NPF_TABLE_LPM:
814 error = table_generic_list(t, ubuf, len);
815 break;
816 case NPF_TABLE_CONST:
817 error = table_cdb_list(t, ubuf, len);
818 break;
819 case NPF_TABLE_IFADDR:
820 error = table_generic_list(t, ubuf, len);
821 break;
822 default:
823 KASSERT(false);
824 }
825 mutex_exit(&t->t_lock);
826
827 return error;
828 }
829
830 /*
831 * npf_table_flush: remove all table entries.
832 */
833 int
834 npf_table_flush(npf_table_t *t)
835 {
836 int error = 0;
837
838 mutex_enter(&t->t_lock);
839 switch (t->t_type) {
840 case NPF_TABLE_IPSET:
841 table_ipset_flush(t);
842 break;
843 case NPF_TABLE_LPM:
844 table_tree_flush(t);
845 break;
846 case NPF_TABLE_CONST:
847 case NPF_TABLE_IFADDR:
848 error = EINVAL;
849 break;
850 default:
851 KASSERT(false);
852 }
853 mutex_exit(&t->t_lock);
854 return error;
855 }
856
857 void
858 npf_table_gc(npf_t *npf, npf_table_t *t)
859 {
860 npf_tblent_t *ent;
861 void *ref;
862
863 if (t->t_type != NPF_TABLE_IPSET || LIST_EMPTY(&t->t_gc)) {
864 return;
865 }
866
867 ref = thmap_stage_gc(t->t_map);
868 if (npf) {
869 npf_config_locked_p(npf);
870 npf_config_sync(npf);
871 }
872 thmap_gc(t->t_map, ref);
873
874 while ((ent = LIST_FIRST(&t->t_gc)) != NULL) {
875 LIST_REMOVE(ent, te_listent);
876 pool_cache_put(tblent_cache, ent);
877 }
878 }
879