npf_tableset.c revision 1.17 1 1.17 rmind /* $NetBSD: npf_tableset.c,v 1.17 2013/02/09 03:35:32 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.9 rmind * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.4 rmind * NPF tableset module.
34 1.1 rmind *
35 1.15 rmind * Notes
36 1.15 rmind *
37 1.15 rmind * The tableset is an array of tables. After the creation, the array
38 1.15 rmind * is immutable. The caller is responsible to synchronise the access
39 1.15 rmind * to the tableset. The table can either be a hash or a tree. Its
40 1.15 rmind * entries are protected by a read-write lock.
41 1.1 rmind */
42 1.1 rmind
43 1.1 rmind #include <sys/cdefs.h>
44 1.17 rmind __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.17 2013/02/09 03:35:32 rmind Exp $");
45 1.1 rmind
46 1.1 rmind #include <sys/param.h>
47 1.10 rmind #include <sys/types.h>
48 1.1 rmind
49 1.1 rmind #include <sys/atomic.h>
50 1.1 rmind #include <sys/hash.h>
51 1.1 rmind #include <sys/kmem.h>
52 1.1 rmind #include <sys/pool.h>
53 1.1 rmind #include <sys/queue.h>
54 1.1 rmind #include <sys/rwlock.h>
55 1.1 rmind #include <sys/systm.h>
56 1.1 rmind #include <sys/types.h>
57 1.1 rmind
58 1.1 rmind #include "npf_impl.h"
59 1.1 rmind
60 1.13 rmind /*
61 1.14 rmind * Table structures.
62 1.13 rmind */
63 1.13 rmind
64 1.15 rmind typedef struct npf_tblent {
65 1.1 rmind union {
66 1.13 rmind LIST_ENTRY(npf_tblent) hashq;
67 1.13 rmind pt_node_t node;
68 1.1 rmind } te_entry;
69 1.13 rmind int te_alen;
70 1.13 rmind npf_addr_t te_addr;
71 1.15 rmind } npf_tblent_t;
72 1.1 rmind
73 1.1 rmind LIST_HEAD(npf_hashl, npf_tblent);
74 1.1 rmind
75 1.1 rmind struct npf_table {
76 1.13 rmind char t_name[16];
77 1.1 rmind /* Lock and reference count. */
78 1.13 rmind krwlock_t t_lock;
79 1.13 rmind u_int t_refcnt;
80 1.15 rmind /* Total number of items. */
81 1.15 rmind u_int t_nitems;
82 1.1 rmind /* Table ID. */
83 1.13 rmind u_int t_id;
84 1.14 rmind /* The storage type can be: a) hash b) tree. */
85 1.13 rmind int t_type;
86 1.13 rmind struct npf_hashl * t_hashl;
87 1.13 rmind u_long t_hashmask;
88 1.15 rmind /* Separate trees for IPv4 and IPv6. */
89 1.13 rmind pt_tree_t t_tree[2];
90 1.1 rmind };
91 1.1 rmind
92 1.13 rmind #define NPF_ADDRLEN2TREE(alen) ((alen) >> 4)
93 1.13 rmind
94 1.13 rmind static pool_cache_t tblent_cache __read_mostly;
95 1.1 rmind
96 1.1 rmind /*
97 1.1 rmind * npf_table_sysinit: initialise tableset structures.
98 1.1 rmind */
99 1.4 rmind void
100 1.1 rmind npf_tableset_sysinit(void)
101 1.1 rmind {
102 1.1 rmind
103 1.1 rmind tblent_cache = pool_cache_init(sizeof(npf_tblent_t), coherency_unit,
104 1.14 rmind 0, 0, "npftblpl", NULL, IPL_NONE, NULL, NULL, NULL);
105 1.1 rmind }
106 1.1 rmind
107 1.1 rmind void
108 1.1 rmind npf_tableset_sysfini(void)
109 1.1 rmind {
110 1.1 rmind
111 1.1 rmind pool_cache_destroy(tblent_cache);
112 1.1 rmind }
113 1.1 rmind
114 1.1 rmind npf_tableset_t *
115 1.1 rmind npf_tableset_create(void)
116 1.1 rmind {
117 1.1 rmind const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
118 1.1 rmind
119 1.1 rmind return kmem_zalloc(sz, KM_SLEEP);
120 1.1 rmind }
121 1.1 rmind
122 1.1 rmind void
123 1.1 rmind npf_tableset_destroy(npf_tableset_t *tblset)
124 1.1 rmind {
125 1.1 rmind const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
126 1.1 rmind npf_table_t *t;
127 1.1 rmind u_int tid;
128 1.1 rmind
129 1.1 rmind /*
130 1.1 rmind * Destroy all tables (no references should be held, as ruleset
131 1.1 rmind * should be destroyed before).
132 1.1 rmind */
133 1.1 rmind for (tid = 0; tid < NPF_TABLE_SLOTS; tid++) {
134 1.1 rmind t = tblset[tid];
135 1.17 rmind if (t && atomic_dec_uint_nv(&t->t_refcnt) == 0) {
136 1.1 rmind npf_table_destroy(t);
137 1.1 rmind }
138 1.1 rmind }
139 1.1 rmind kmem_free(tblset, sz);
140 1.1 rmind }
141 1.1 rmind
142 1.1 rmind /*
143 1.1 rmind * npf_tableset_insert: insert the table into the specified tableset.
144 1.1 rmind *
145 1.13 rmind * => Returns 0 on success. Fails and returns error if ID is already used.
146 1.1 rmind */
147 1.1 rmind int
148 1.1 rmind npf_tableset_insert(npf_tableset_t *tblset, npf_table_t *t)
149 1.1 rmind {
150 1.1 rmind const u_int tid = t->t_id;
151 1.1 rmind int error;
152 1.1 rmind
153 1.1 rmind KASSERT((u_int)tid < NPF_TABLE_SLOTS);
154 1.1 rmind
155 1.1 rmind if (tblset[tid] == NULL) {
156 1.17 rmind atomic_inc_uint(&t->t_refcnt);
157 1.1 rmind tblset[tid] = t;
158 1.1 rmind error = 0;
159 1.1 rmind } else {
160 1.1 rmind error = EEXIST;
161 1.1 rmind }
162 1.1 rmind return error;
163 1.1 rmind }
164 1.1 rmind
165 1.1 rmind /*
166 1.15 rmind * npf_tableset_reload: iterate all tables and if the new table is of the
167 1.15 rmind * same type and has no items, then we preserve the old one and its entries.
168 1.15 rmind *
169 1.15 rmind * => The caller is responsible for providing synchronisation.
170 1.15 rmind */
171 1.15 rmind void
172 1.15 rmind npf_tableset_reload(npf_tableset_t *ntset, npf_tableset_t *otset)
173 1.15 rmind {
174 1.15 rmind for (int i = 0; i < NPF_TABLE_SLOTS; i++) {
175 1.15 rmind npf_table_t *t = ntset[i], *ot = otset[i];
176 1.15 rmind
177 1.15 rmind if (t == NULL || ot == NULL) {
178 1.15 rmind continue;
179 1.15 rmind }
180 1.15 rmind if (t->t_nitems || t->t_type != ot->t_type) {
181 1.15 rmind continue;
182 1.15 rmind }
183 1.17 rmind
184 1.17 rmind /*
185 1.17 rmind * Acquire a reference since the table has to be kept
186 1.17 rmind * in the old tableset.
187 1.17 rmind */
188 1.17 rmind atomic_inc_uint(&ot->t_refcnt);
189 1.15 rmind ntset[i] = ot;
190 1.17 rmind
191 1.17 rmind /* Only reference, never been visible. */
192 1.17 rmind t->t_refcnt--;
193 1.15 rmind npf_table_destroy(t);
194 1.15 rmind }
195 1.15 rmind }
196 1.15 rmind
197 1.15 rmind /*
198 1.13 rmind * Few helper routines.
199 1.1 rmind */
200 1.1 rmind
201 1.13 rmind static npf_tblent_t *
202 1.13 rmind table_hash_lookup(const npf_table_t *t, const npf_addr_t *addr,
203 1.13 rmind const int alen, struct npf_hashl **rhtbl)
204 1.1 rmind {
205 1.13 rmind const uint32_t hidx = hash32_buf(addr, alen, HASH32_BUF_INIT);
206 1.13 rmind struct npf_hashl *htbl = &t->t_hashl[hidx & t->t_hashmask];
207 1.13 rmind npf_tblent_t *ent;
208 1.1 rmind
209 1.13 rmind /*
210 1.13 rmind * Lookup the hash table and check for duplicates.
211 1.13 rmind * Note: mask is ignored for the hash storage.
212 1.13 rmind */
213 1.13 rmind LIST_FOREACH(ent, htbl, te_entry.hashq) {
214 1.13 rmind if (ent->te_alen != alen) {
215 1.13 rmind continue;
216 1.13 rmind }
217 1.13 rmind if (memcmp(&ent->te_addr, addr, alen) == 0) {
218 1.13 rmind break;
219 1.13 rmind }
220 1.13 rmind }
221 1.13 rmind *rhtbl = htbl;
222 1.13 rmind return ent;
223 1.1 rmind }
224 1.1 rmind
225 1.13 rmind static void
226 1.13 rmind table_tree_destroy(pt_tree_t *tree)
227 1.1 rmind {
228 1.13 rmind npf_tblent_t *ent;
229 1.1 rmind
230 1.13 rmind while ((ent = ptree_iterate(tree, NULL, PT_ASCENDING)) != NULL) {
231 1.13 rmind ptree_remove_node(tree, ent);
232 1.13 rmind pool_cache_put(tblent_cache, ent);
233 1.13 rmind }
234 1.1 rmind }
235 1.1 rmind
236 1.1 rmind /*
237 1.1 rmind * npf_table_create: create table with a specified ID.
238 1.1 rmind */
239 1.1 rmind npf_table_t *
240 1.1 rmind npf_table_create(u_int tid, int type, size_t hsize)
241 1.1 rmind {
242 1.1 rmind npf_table_t *t;
243 1.1 rmind
244 1.1 rmind KASSERT((u_int)tid < NPF_TABLE_SLOTS);
245 1.1 rmind
246 1.1 rmind t = kmem_zalloc(sizeof(npf_table_t), KM_SLEEP);
247 1.1 rmind switch (type) {
248 1.9 rmind case NPF_TABLE_TREE:
249 1.13 rmind ptree_init(&t->t_tree[0], &npf_table_ptree_ops,
250 1.13 rmind (void *)(sizeof(struct in_addr) / sizeof(uint32_t)),
251 1.13 rmind offsetof(npf_tblent_t, te_entry.node),
252 1.13 rmind offsetof(npf_tblent_t, te_addr));
253 1.13 rmind ptree_init(&t->t_tree[1], &npf_table_ptree_ops,
254 1.13 rmind (void *)(sizeof(struct in6_addr) / sizeof(uint32_t)),
255 1.13 rmind offsetof(npf_tblent_t, te_entry.node),
256 1.13 rmind offsetof(npf_tblent_t, te_addr));
257 1.1 rmind break;
258 1.1 rmind case NPF_TABLE_HASH:
259 1.1 rmind t->t_hashl = hashinit(hsize, HASH_LIST, true, &t->t_hashmask);
260 1.1 rmind if (t->t_hashl == NULL) {
261 1.1 rmind kmem_free(t, sizeof(npf_table_t));
262 1.1 rmind return NULL;
263 1.1 rmind }
264 1.1 rmind break;
265 1.1 rmind default:
266 1.1 rmind KASSERT(false);
267 1.1 rmind }
268 1.1 rmind rw_init(&t->t_lock);
269 1.1 rmind t->t_type = type;
270 1.1 rmind t->t_id = tid;
271 1.15 rmind
272 1.1 rmind return t;
273 1.1 rmind }
274 1.1 rmind
275 1.1 rmind /*
276 1.1 rmind * npf_table_destroy: free all table entries and table itself.
277 1.1 rmind */
278 1.1 rmind void
279 1.1 rmind npf_table_destroy(npf_table_t *t)
280 1.1 rmind {
281 1.17 rmind KASSERT(t->t_refcnt == 0);
282 1.1 rmind
283 1.1 rmind switch (t->t_type) {
284 1.15 rmind case NPF_TABLE_HASH:
285 1.13 rmind for (unsigned n = 0; n <= t->t_hashmask; n++) {
286 1.13 rmind npf_tblent_t *ent;
287 1.13 rmind
288 1.13 rmind while ((ent = LIST_FIRST(&t->t_hashl[n])) != NULL) {
289 1.13 rmind LIST_REMOVE(ent, te_entry.hashq);
290 1.13 rmind pool_cache_put(tblent_cache, ent);
291 1.1 rmind }
292 1.1 rmind }
293 1.1 rmind hashdone(t->t_hashl, HASH_LIST, t->t_hashmask);
294 1.1 rmind break;
295 1.15 rmind case NPF_TABLE_TREE:
296 1.13 rmind table_tree_destroy(&t->t_tree[0]);
297 1.13 rmind table_tree_destroy(&t->t_tree[1]);
298 1.1 rmind break;
299 1.1 rmind default:
300 1.1 rmind KASSERT(false);
301 1.1 rmind }
302 1.1 rmind rw_destroy(&t->t_lock);
303 1.1 rmind kmem_free(t, sizeof(npf_table_t));
304 1.1 rmind }
305 1.1 rmind
306 1.1 rmind /*
307 1.1 rmind * npf_table_check: validate ID and type.
308 1.13 rmind */
309 1.1 rmind int
310 1.13 rmind npf_table_check(const npf_tableset_t *tset, u_int tid, int type)
311 1.1 rmind {
312 1.1 rmind
313 1.1 rmind if ((u_int)tid >= NPF_TABLE_SLOTS) {
314 1.1 rmind return EINVAL;
315 1.1 rmind }
316 1.1 rmind if (tset[tid] != NULL) {
317 1.1 rmind return EEXIST;
318 1.1 rmind }
319 1.9 rmind if (type != NPF_TABLE_TREE && type != NPF_TABLE_HASH) {
320 1.1 rmind return EINVAL;
321 1.1 rmind }
322 1.1 rmind return 0;
323 1.1 rmind }
324 1.1 rmind
325 1.13 rmind static int
326 1.15 rmind table_cidr_check(const u_int aidx, const npf_addr_t *addr,
327 1.13 rmind const npf_netmask_t mask)
328 1.13 rmind {
329 1.13 rmind
330 1.13 rmind if (mask > NPF_MAX_NETMASK && mask != NPF_NO_NETMASK) {
331 1.13 rmind return EINVAL;
332 1.13 rmind }
333 1.13 rmind if (aidx > 1) {
334 1.13 rmind return EINVAL;
335 1.13 rmind }
336 1.13 rmind
337 1.13 rmind /*
338 1.13 rmind * For IPv4 (aidx = 0) - 32 and for IPv6 (aidx = 1) - 128.
339 1.13 rmind * If it is a host - shall use NPF_NO_NETMASK.
340 1.13 rmind */
341 1.13 rmind if (mask >= (aidx ? 128 : 32) && mask != NPF_NO_NETMASK) {
342 1.13 rmind return EINVAL;
343 1.13 rmind }
344 1.13 rmind return 0;
345 1.13 rmind }
346 1.13 rmind
347 1.1 rmind /*
348 1.13 rmind * npf_table_insert: add an IP CIDR entry into the table.
349 1.1 rmind */
350 1.1 rmind int
351 1.13 rmind npf_table_insert(npf_tableset_t *tset, u_int tid, const int alen,
352 1.6 zoltan const npf_addr_t *addr, const npf_netmask_t mask)
353 1.1 rmind {
354 1.13 rmind const u_int aidx = NPF_ADDRLEN2TREE(alen);
355 1.13 rmind npf_tblent_t *ent;
356 1.1 rmind npf_table_t *t;
357 1.13 rmind int error;
358 1.1 rmind
359 1.15 rmind if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
360 1.15 rmind return EINVAL;
361 1.15 rmind }
362 1.15 rmind
363 1.15 rmind error = table_cidr_check(aidx, addr, mask);
364 1.13 rmind if (error) {
365 1.13 rmind return error;
366 1.8 rmind }
367 1.12 rmind ent = pool_cache_get(tblent_cache, PR_WAITOK);
368 1.13 rmind memcpy(&ent->te_addr, addr, alen);
369 1.13 rmind ent->te_alen = alen;
370 1.1 rmind
371 1.13 rmind /*
372 1.13 rmind * Insert the entry. Return an error on duplicate.
373 1.13 rmind */
374 1.15 rmind rw_enter(&t->t_lock, RW_WRITER);
375 1.1 rmind switch (t->t_type) {
376 1.13 rmind case NPF_TABLE_HASH: {
377 1.13 rmind struct npf_hashl *htbl;
378 1.13 rmind
379 1.13 rmind /*
380 1.13 rmind * Hash tables by the concept support only IPs.
381 1.13 rmind */
382 1.13 rmind if (mask != NPF_NO_NETMASK) {
383 1.13 rmind error = EINVAL;
384 1.13 rmind break;
385 1.1 rmind }
386 1.13 rmind if (!table_hash_lookup(t, addr, alen, &htbl)) {
387 1.12 rmind LIST_INSERT_HEAD(htbl, ent, te_entry.hashq);
388 1.15 rmind t->t_nitems++;
389 1.1 rmind } else {
390 1.1 rmind error = EEXIST;
391 1.1 rmind }
392 1.1 rmind break;
393 1.13 rmind }
394 1.13 rmind case NPF_TABLE_TREE: {
395 1.13 rmind pt_tree_t *tree = &t->t_tree[aidx];
396 1.13 rmind bool ok;
397 1.13 rmind
398 1.13 rmind /*
399 1.13 rmind * If no mask specified, use maximum mask.
400 1.13 rmind */
401 1.15 rmind ok = (mask != NPF_NO_NETMASK) ?
402 1.15 rmind ptree_insert_mask_node(tree, ent, mask) :
403 1.15 rmind ptree_insert_node(tree, ent);
404 1.15 rmind if (ok) {
405 1.15 rmind t->t_nitems++;
406 1.15 rmind error = 0;
407 1.13 rmind } else {
408 1.15 rmind error = EEXIST;
409 1.1 rmind }
410 1.1 rmind break;
411 1.13 rmind }
412 1.1 rmind default:
413 1.1 rmind KASSERT(false);
414 1.1 rmind }
415 1.15 rmind rw_exit(&t->t_lock);
416 1.1 rmind
417 1.8 rmind if (error) {
418 1.12 rmind pool_cache_put(tblent_cache, ent);
419 1.1 rmind }
420 1.1 rmind return error;
421 1.1 rmind }
422 1.1 rmind
423 1.1 rmind /*
424 1.13 rmind * npf_table_remove: remove the IP CIDR entry from the table.
425 1.1 rmind */
426 1.1 rmind int
427 1.13 rmind npf_table_remove(npf_tableset_t *tset, u_int tid, const int alen,
428 1.6 zoltan const npf_addr_t *addr, const npf_netmask_t mask)
429 1.1 rmind {
430 1.13 rmind const u_int aidx = NPF_ADDRLEN2TREE(alen);
431 1.12 rmind npf_tblent_t *ent;
432 1.1 rmind npf_table_t *t;
433 1.13 rmind int error;
434 1.1 rmind
435 1.15 rmind error = table_cidr_check(aidx, addr, mask);
436 1.13 rmind if (error) {
437 1.13 rmind return error;
438 1.8 rmind }
439 1.15 rmind
440 1.15 rmind if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
441 1.1 rmind return EINVAL;
442 1.1 rmind }
443 1.12 rmind
444 1.15 rmind rw_enter(&t->t_lock, RW_WRITER);
445 1.13 rmind switch (t->t_type) {
446 1.13 rmind case NPF_TABLE_HASH: {
447 1.13 rmind struct npf_hashl *htbl;
448 1.8 rmind
449 1.13 rmind ent = table_hash_lookup(t, addr, alen, &htbl);
450 1.12 rmind if (__predict_true(ent != NULL)) {
451 1.12 rmind LIST_REMOVE(ent, te_entry.hashq);
452 1.15 rmind t->t_nitems--;
453 1.1 rmind }
454 1.1 rmind break;
455 1.13 rmind }
456 1.13 rmind case NPF_TABLE_TREE: {
457 1.13 rmind pt_tree_t *tree = &t->t_tree[aidx];
458 1.13 rmind
459 1.13 rmind ent = ptree_find_node(tree, addr);
460 1.12 rmind if (__predict_true(ent != NULL)) {
461 1.13 rmind ptree_remove_node(tree, ent);
462 1.15 rmind t->t_nitems--;
463 1.1 rmind }
464 1.1 rmind break;
465 1.13 rmind }
466 1.1 rmind default:
467 1.1 rmind KASSERT(false);
468 1.13 rmind ent = NULL;
469 1.1 rmind }
470 1.15 rmind rw_exit(&t->t_lock);
471 1.1 rmind
472 1.12 rmind if (ent == NULL) {
473 1.8 rmind return ENOENT;
474 1.1 rmind }
475 1.12 rmind pool_cache_put(tblent_cache, ent);
476 1.8 rmind return 0;
477 1.1 rmind }
478 1.1 rmind
479 1.1 rmind /*
480 1.13 rmind * npf_table_lookup: find the table according to ID, lookup and match
481 1.13 rmind * the contents with the specified IP address.
482 1.1 rmind */
483 1.1 rmind int
484 1.13 rmind npf_table_lookup(npf_tableset_t *tset, u_int tid,
485 1.13 rmind const int alen, const npf_addr_t *addr)
486 1.1 rmind {
487 1.13 rmind const u_int aidx = NPF_ADDRLEN2TREE(alen);
488 1.13 rmind npf_tblent_t *ent;
489 1.1 rmind npf_table_t *t;
490 1.1 rmind
491 1.13 rmind if (__predict_false(aidx > 1)) {
492 1.13 rmind return EINVAL;
493 1.13 rmind }
494 1.13 rmind
495 1.15 rmind if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
496 1.1 rmind return EINVAL;
497 1.1 rmind }
498 1.15 rmind
499 1.15 rmind rw_enter(&t->t_lock, RW_READER);
500 1.1 rmind switch (t->t_type) {
501 1.13 rmind case NPF_TABLE_HASH: {
502 1.13 rmind struct npf_hashl *htbl;
503 1.13 rmind ent = table_hash_lookup(t, addr, alen, &htbl);
504 1.1 rmind break;
505 1.13 rmind }
506 1.13 rmind case NPF_TABLE_TREE: {
507 1.13 rmind ent = ptree_find_node(&t->t_tree[aidx], addr);
508 1.1 rmind break;
509 1.13 rmind }
510 1.1 rmind default:
511 1.1 rmind KASSERT(false);
512 1.13 rmind ent = NULL;
513 1.1 rmind }
514 1.15 rmind rw_exit(&t->t_lock);
515 1.1 rmind
516 1.13 rmind return ent ? 0 : ENOENT;
517 1.1 rmind }
518 1.15 rmind
519 1.15 rmind static int
520 1.15 rmind table_ent_copyout(npf_tblent_t *ent, npf_netmask_t mask,
521 1.15 rmind void *ubuf, size_t len, size_t *off)
522 1.15 rmind {
523 1.15 rmind void *ubufp = (uint8_t *)ubuf + *off;
524 1.15 rmind npf_ioctl_ent_t uent;
525 1.15 rmind
526 1.15 rmind if ((*off += sizeof(npf_ioctl_ent_t)) > len) {
527 1.15 rmind return ENOMEM;
528 1.15 rmind }
529 1.15 rmind uent.alen = ent->te_alen;
530 1.15 rmind memcpy(&uent.addr, &ent->te_addr, sizeof(npf_addr_t));
531 1.15 rmind uent.mask = mask;
532 1.15 rmind
533 1.15 rmind return copyout(&uent, ubufp, sizeof(npf_ioctl_ent_t));
534 1.15 rmind }
535 1.15 rmind
536 1.15 rmind static int
537 1.15 rmind table_tree_list(pt_tree_t *tree, npf_netmask_t maxmask, void *ubuf,
538 1.15 rmind size_t len, size_t *off)
539 1.15 rmind {
540 1.15 rmind npf_tblent_t *ent = NULL;
541 1.15 rmind int error = 0;
542 1.15 rmind
543 1.15 rmind while ((ent = ptree_iterate(tree, ent, PT_ASCENDING)) != NULL) {
544 1.15 rmind pt_bitlen_t blen;
545 1.15 rmind
546 1.15 rmind if (!ptree_mask_node_p(tree, ent, &blen)) {
547 1.15 rmind blen = maxmask;
548 1.15 rmind }
549 1.15 rmind error = table_ent_copyout(ent, blen, ubuf, len, off);
550 1.15 rmind if (error)
551 1.15 rmind break;
552 1.15 rmind }
553 1.15 rmind return error;
554 1.15 rmind }
555 1.15 rmind
556 1.15 rmind /*
557 1.15 rmind * npf_table_list: copy a list of all table entries into a userspace buffer.
558 1.15 rmind */
559 1.15 rmind int
560 1.15 rmind npf_table_list(npf_tableset_t *tset, u_int tid, void *ubuf, size_t len)
561 1.15 rmind {
562 1.15 rmind npf_table_t *t;
563 1.15 rmind size_t off = 0;
564 1.15 rmind int error = 0;
565 1.15 rmind
566 1.15 rmind if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
567 1.15 rmind return EINVAL;
568 1.15 rmind }
569 1.15 rmind
570 1.15 rmind rw_enter(&t->t_lock, RW_READER);
571 1.15 rmind switch (t->t_type) {
572 1.15 rmind case NPF_TABLE_HASH:
573 1.15 rmind for (unsigned n = 0; n <= t->t_hashmask; n++) {
574 1.15 rmind npf_tblent_t *ent;
575 1.15 rmind
576 1.15 rmind LIST_FOREACH(ent, &t->t_hashl[n], te_entry.hashq)
577 1.15 rmind if ((error = table_ent_copyout(ent, 0, ubuf,
578 1.15 rmind len, &off)) != 0)
579 1.15 rmind break;
580 1.15 rmind }
581 1.15 rmind break;
582 1.15 rmind case NPF_TABLE_TREE:
583 1.15 rmind error = table_tree_list(&t->t_tree[0], 32, ubuf, len, &off);
584 1.15 rmind if (error)
585 1.15 rmind break;
586 1.15 rmind error = table_tree_list(&t->t_tree[1], 128, ubuf, len, &off);
587 1.16 rmind break;
588 1.15 rmind default:
589 1.15 rmind KASSERT(false);
590 1.15 rmind }
591 1.15 rmind rw_exit(&t->t_lock);
592 1.15 rmind
593 1.15 rmind return error;
594 1.15 rmind }
595