npf_tableset.c revision 1.25 1 1.25 christos /* $NetBSD: npf_tableset.c,v 1.25 2016/12/26 23:05:06 christos Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.24 christos * Copyright (c) 2009-2016 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.25 christos #ifdef _KERNEL
44 1.1 rmind #include <sys/cdefs.h>
45 1.25 christos __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.25 2016/12/26 23:05:06 christos Exp $");
46 1.1 rmind
47 1.1 rmind #include <sys/param.h>
48 1.10 rmind #include <sys/types.h>
49 1.1 rmind
50 1.1 rmind #include <sys/atomic.h>
51 1.1 rmind #include <sys/hash.h>
52 1.21 rmind #include <sys/cdbr.h>
53 1.1 rmind #include <sys/kmem.h>
54 1.21 rmind #include <sys/malloc.h>
55 1.1 rmind #include <sys/pool.h>
56 1.1 rmind #include <sys/queue.h>
57 1.1 rmind #include <sys/rwlock.h>
58 1.1 rmind #include <sys/systm.h>
59 1.1 rmind #include <sys/types.h>
60 1.1 rmind
61 1.25 christos #include "lpm.h"
62 1.25 christos #endif
63 1.25 christos
64 1.1 rmind #include "npf_impl.h"
65 1.1 rmind
66 1.15 rmind typedef struct npf_tblent {
67 1.24 christos LIST_ENTRY(npf_tblent) te_listent;
68 1.24 christos uint16_t te_preflen;
69 1.24 christos uint16_t 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.19 rmind /*
77 1.21 rmind * The storage type can be: a) hash b) tree c) cdb.
78 1.19 rmind * There are separate trees for IPv4 and IPv6.
79 1.19 rmind */
80 1.21 rmind union {
81 1.21 rmind struct {
82 1.21 rmind struct npf_hashl *t_hashl;
83 1.21 rmind u_long t_hashmask;
84 1.21 rmind };
85 1.21 rmind struct {
86 1.24 christos lpm_t * t_lpm;
87 1.24 christos LIST_HEAD(, npf_tblent) t_list;
88 1.21 rmind };
89 1.21 rmind struct {
90 1.21 rmind void * t_blob;
91 1.21 rmind size_t t_bsize;
92 1.21 rmind struct cdbr * t_cdb;
93 1.21 rmind };
94 1.21 rmind } /* C11 */;
95 1.19 rmind
96 1.19 rmind /*
97 1.19 rmind * Table ID, type and lock. The ID may change during the
98 1.19 rmind * config reload, it is protected by the npf_config_lock.
99 1.19 rmind */
100 1.19 rmind int t_type;
101 1.19 rmind u_int t_id;
102 1.19 rmind krwlock_t t_lock;
103 1.19 rmind
104 1.19 rmind /* The number of items, reference count and table name. */
105 1.19 rmind u_int t_nitems;
106 1.19 rmind u_int t_refcnt;
107 1.19 rmind char t_name[NPF_TABLE_MAXNAMELEN];
108 1.19 rmind };
109 1.19 rmind
110 1.19 rmind struct npf_tableset {
111 1.19 rmind u_int ts_nitems;
112 1.19 rmind npf_table_t * ts_map[];
113 1.1 rmind };
114 1.1 rmind
115 1.19 rmind #define NPF_TABLESET_SIZE(n) \
116 1.19 rmind (offsetof(npf_tableset_t, ts_map[n]) * sizeof(npf_table_t *))
117 1.19 rmind
118 1.13 rmind #define NPF_ADDRLEN2TREE(alen) ((alen) >> 4)
119 1.13 rmind
120 1.13 rmind static pool_cache_t tblent_cache __read_mostly;
121 1.1 rmind
122 1.1 rmind /*
123 1.1 rmind * npf_table_sysinit: initialise tableset structures.
124 1.1 rmind */
125 1.4 rmind void
126 1.1 rmind npf_tableset_sysinit(void)
127 1.1 rmind {
128 1.1 rmind tblent_cache = pool_cache_init(sizeof(npf_tblent_t), coherency_unit,
129 1.14 rmind 0, 0, "npftblpl", NULL, IPL_NONE, NULL, NULL, NULL);
130 1.1 rmind }
131 1.1 rmind
132 1.1 rmind void
133 1.1 rmind npf_tableset_sysfini(void)
134 1.1 rmind {
135 1.1 rmind pool_cache_destroy(tblent_cache);
136 1.1 rmind }
137 1.1 rmind
138 1.1 rmind npf_tableset_t *
139 1.19 rmind npf_tableset_create(u_int nitems)
140 1.1 rmind {
141 1.19 rmind npf_tableset_t *ts = kmem_zalloc(NPF_TABLESET_SIZE(nitems), KM_SLEEP);
142 1.19 rmind ts->ts_nitems = nitems;
143 1.19 rmind return ts;
144 1.1 rmind }
145 1.1 rmind
146 1.1 rmind void
147 1.19 rmind npf_tableset_destroy(npf_tableset_t *ts)
148 1.1 rmind {
149 1.1 rmind /*
150 1.19 rmind * Destroy all tables (no references should be held, since the
151 1.19 rmind * ruleset should be destroyed before).
152 1.1 rmind */
153 1.19 rmind for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
154 1.19 rmind npf_table_t *t = ts->ts_map[tid];
155 1.19 rmind
156 1.17 rmind if (t && atomic_dec_uint_nv(&t->t_refcnt) == 0) {
157 1.1 rmind npf_table_destroy(t);
158 1.1 rmind }
159 1.1 rmind }
160 1.19 rmind kmem_free(ts, NPF_TABLESET_SIZE(ts->ts_nitems));
161 1.1 rmind }
162 1.1 rmind
163 1.1 rmind /*
164 1.1 rmind * npf_tableset_insert: insert the table into the specified tableset.
165 1.1 rmind *
166 1.13 rmind * => Returns 0 on success. Fails and returns error if ID is already used.
167 1.1 rmind */
168 1.1 rmind int
169 1.19 rmind npf_tableset_insert(npf_tableset_t *ts, npf_table_t *t)
170 1.1 rmind {
171 1.1 rmind const u_int tid = t->t_id;
172 1.1 rmind int error;
173 1.1 rmind
174 1.19 rmind KASSERT((u_int)tid < ts->ts_nitems);
175 1.1 rmind
176 1.19 rmind if (ts->ts_map[tid] == NULL) {
177 1.17 rmind atomic_inc_uint(&t->t_refcnt);
178 1.19 rmind ts->ts_map[tid] = t;
179 1.1 rmind error = 0;
180 1.1 rmind } else {
181 1.1 rmind error = EEXIST;
182 1.1 rmind }
183 1.1 rmind return error;
184 1.1 rmind }
185 1.1 rmind
186 1.1 rmind /*
187 1.19 rmind * npf_tableset_getbyname: look for a table in the set given the name.
188 1.19 rmind */
189 1.19 rmind npf_table_t *
190 1.19 rmind npf_tableset_getbyname(npf_tableset_t *ts, const char *name)
191 1.19 rmind {
192 1.19 rmind npf_table_t *t;
193 1.19 rmind
194 1.19 rmind for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
195 1.19 rmind if ((t = ts->ts_map[tid]) == NULL)
196 1.19 rmind continue;
197 1.19 rmind if (strcmp(name, t->t_name) == 0)
198 1.19 rmind return t;
199 1.19 rmind }
200 1.19 rmind return NULL;
201 1.19 rmind }
202 1.19 rmind
203 1.19 rmind npf_table_t *
204 1.19 rmind npf_tableset_getbyid(npf_tableset_t *ts, u_int tid)
205 1.19 rmind {
206 1.19 rmind if (__predict_true(tid < ts->ts_nitems)) {
207 1.19 rmind return ts->ts_map[tid];
208 1.19 rmind }
209 1.19 rmind return NULL;
210 1.19 rmind }
211 1.19 rmind
212 1.19 rmind /*
213 1.15 rmind * npf_tableset_reload: iterate all tables and if the new table is of the
214 1.15 rmind * same type and has no items, then we preserve the old one and its entries.
215 1.15 rmind *
216 1.15 rmind * => The caller is responsible for providing synchronisation.
217 1.15 rmind */
218 1.15 rmind void
219 1.25 christos npf_tableset_reload(npf_t *npf, npf_tableset_t *nts, npf_tableset_t *ots)
220 1.15 rmind {
221 1.19 rmind for (u_int tid = 0; tid < nts->ts_nitems; tid++) {
222 1.19 rmind npf_table_t *t, *ot;
223 1.19 rmind
224 1.19 rmind if ((t = nts->ts_map[tid]) == NULL) {
225 1.19 rmind continue;
226 1.19 rmind }
227 1.15 rmind
228 1.19 rmind /* If our table has entries, just load it. */
229 1.19 rmind if (t->t_nitems) {
230 1.15 rmind continue;
231 1.15 rmind }
232 1.19 rmind
233 1.19 rmind /* Look for a currently existing table with such name. */
234 1.19 rmind ot = npf_tableset_getbyname(ots, t->t_name);
235 1.19 rmind if (ot == NULL) {
236 1.19 rmind /* Not found: we have a new table. */
237 1.19 rmind continue;
238 1.19 rmind }
239 1.19 rmind
240 1.19 rmind /* Found. Did the type change? */
241 1.19 rmind if (t->t_type != ot->t_type) {
242 1.19 rmind /* Yes, load the new. */
243 1.15 rmind continue;
244 1.15 rmind }
245 1.17 rmind
246 1.17 rmind /*
247 1.19 rmind * Preserve the current table. Acquire a reference since
248 1.19 rmind * we are keeping it in the old table set. Update its ID.
249 1.17 rmind */
250 1.17 rmind atomic_inc_uint(&ot->t_refcnt);
251 1.19 rmind nts->ts_map[tid] = ot;
252 1.19 rmind
253 1.25 christos KASSERT(npf_config_locked_p(npf));
254 1.19 rmind ot->t_id = tid;
255 1.17 rmind
256 1.21 rmind /* Destroy the new table (we hold the only reference). */
257 1.17 rmind t->t_refcnt--;
258 1.15 rmind npf_table_destroy(t);
259 1.15 rmind }
260 1.15 rmind }
261 1.15 rmind
262 1.22 rmind int
263 1.25 christos npf_tableset_export(npf_t *npf, const npf_tableset_t *ts, prop_array_t tables)
264 1.20 rmind {
265 1.20 rmind const npf_table_t *t;
266 1.20 rmind
267 1.25 christos KASSERT(npf_config_locked_p(npf));
268 1.20 rmind
269 1.20 rmind for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
270 1.20 rmind if ((t = ts->ts_map[tid]) == NULL) {
271 1.20 rmind continue;
272 1.20 rmind }
273 1.20 rmind prop_dictionary_t tdict = prop_dictionary_create();
274 1.20 rmind prop_dictionary_set_cstring(tdict, "name", t->t_name);
275 1.20 rmind prop_dictionary_set_uint32(tdict, "type", t->t_type);
276 1.20 rmind prop_dictionary_set_uint32(tdict, "id", tid);
277 1.20 rmind
278 1.20 rmind prop_array_add(tables, tdict);
279 1.20 rmind prop_object_release(tdict);
280 1.20 rmind }
281 1.22 rmind return 0;
282 1.20 rmind }
283 1.20 rmind
284 1.15 rmind /*
285 1.13 rmind * Few helper routines.
286 1.1 rmind */
287 1.1 rmind
288 1.13 rmind static npf_tblent_t *
289 1.13 rmind table_hash_lookup(const npf_table_t *t, const npf_addr_t *addr,
290 1.13 rmind const int alen, struct npf_hashl **rhtbl)
291 1.1 rmind {
292 1.13 rmind const uint32_t hidx = hash32_buf(addr, alen, HASH32_BUF_INIT);
293 1.13 rmind struct npf_hashl *htbl = &t->t_hashl[hidx & t->t_hashmask];
294 1.13 rmind npf_tblent_t *ent;
295 1.1 rmind
296 1.13 rmind /*
297 1.13 rmind * Lookup the hash table and check for duplicates.
298 1.13 rmind * Note: mask is ignored for the hash storage.
299 1.13 rmind */
300 1.24 christos LIST_FOREACH(ent, htbl, te_listent) {
301 1.13 rmind if (ent->te_alen != alen) {
302 1.13 rmind continue;
303 1.13 rmind }
304 1.13 rmind if (memcmp(&ent->te_addr, addr, alen) == 0) {
305 1.13 rmind break;
306 1.13 rmind }
307 1.13 rmind }
308 1.13 rmind *rhtbl = htbl;
309 1.13 rmind return ent;
310 1.1 rmind }
311 1.1 rmind
312 1.13 rmind static void
313 1.24 christos table_hash_flush(npf_table_t *t)
314 1.18 rmind {
315 1.18 rmind for (unsigned n = 0; n <= t->t_hashmask; n++) {
316 1.18 rmind npf_tblent_t *ent;
317 1.18 rmind
318 1.18 rmind while ((ent = LIST_FIRST(&t->t_hashl[n])) != NULL) {
319 1.24 christos LIST_REMOVE(ent, te_listent);
320 1.18 rmind pool_cache_put(tblent_cache, ent);
321 1.18 rmind }
322 1.18 rmind }
323 1.18 rmind }
324 1.18 rmind
325 1.18 rmind static void
326 1.24 christos table_tree_flush(npf_table_t *t)
327 1.1 rmind {
328 1.13 rmind npf_tblent_t *ent;
329 1.1 rmind
330 1.24 christos while ((ent = LIST_FIRST(&t->t_list)) != NULL) {
331 1.24 christos LIST_REMOVE(ent, te_listent);
332 1.13 rmind pool_cache_put(tblent_cache, ent);
333 1.13 rmind }
334 1.24 christos lpm_clear(t->t_lpm, NULL, NULL);
335 1.1 rmind }
336 1.1 rmind
337 1.1 rmind /*
338 1.1 rmind * npf_table_create: create table with a specified ID.
339 1.1 rmind */
340 1.1 rmind npf_table_t *
341 1.21 rmind npf_table_create(const char *name, u_int tid, int type,
342 1.21 rmind void *blob, size_t size)
343 1.1 rmind {
344 1.1 rmind npf_table_t *t;
345 1.1 rmind
346 1.25 christos t = kmem_zalloc(sizeof(npf_table_t), KM_SLEEP);
347 1.19 rmind strlcpy(t->t_name, name, NPF_TABLE_MAXNAMELEN);
348 1.1 rmind
349 1.1 rmind switch (type) {
350 1.9 rmind case NPF_TABLE_TREE:
351 1.25 christos if ((t->t_lpm = lpm_create()) == NULL) {
352 1.24 christos goto out;
353 1.25 christos }
354 1.24 christos LIST_INIT(&t->t_list);
355 1.1 rmind break;
356 1.1 rmind case NPF_TABLE_HASH:
357 1.21 rmind t->t_hashl = hashinit(1024, HASH_LIST, true, &t->t_hashmask);
358 1.25 christos if (t->t_hashl == NULL) {
359 1.24 christos goto out;
360 1.25 christos }
361 1.1 rmind break;
362 1.21 rmind case NPF_TABLE_CDB:
363 1.21 rmind t->t_blob = blob;
364 1.21 rmind t->t_bsize = size;
365 1.21 rmind t->t_cdb = cdbr_open_mem(blob, size, CDBR_DEFAULT, NULL, NULL);
366 1.21 rmind if (t->t_cdb == NULL) {
367 1.21 rmind free(blob, M_TEMP);
368 1.24 christos goto out;
369 1.21 rmind }
370 1.21 rmind t->t_nitems = cdbr_entries(t->t_cdb);
371 1.21 rmind break;
372 1.1 rmind default:
373 1.1 rmind KASSERT(false);
374 1.1 rmind }
375 1.1 rmind rw_init(&t->t_lock);
376 1.1 rmind t->t_type = type;
377 1.1 rmind t->t_id = tid;
378 1.1 rmind return t;
379 1.24 christos out:
380 1.25 christos kmem_free(t, sizeof(npf_table_t));
381 1.24 christos return NULL;
382 1.1 rmind }
383 1.1 rmind
384 1.1 rmind /*
385 1.1 rmind * npf_table_destroy: free all table entries and table itself.
386 1.1 rmind */
387 1.1 rmind void
388 1.1 rmind npf_table_destroy(npf_table_t *t)
389 1.1 rmind {
390 1.17 rmind KASSERT(t->t_refcnt == 0);
391 1.1 rmind
392 1.1 rmind switch (t->t_type) {
393 1.15 rmind case NPF_TABLE_HASH:
394 1.24 christos table_hash_flush(t);
395 1.1 rmind hashdone(t->t_hashl, HASH_LIST, t->t_hashmask);
396 1.1 rmind break;
397 1.15 rmind case NPF_TABLE_TREE:
398 1.24 christos table_tree_flush(t);
399 1.24 christos lpm_destroy(t->t_lpm);
400 1.1 rmind break;
401 1.21 rmind case NPF_TABLE_CDB:
402 1.21 rmind cdbr_close(t->t_cdb);
403 1.21 rmind free(t->t_blob, M_TEMP);
404 1.21 rmind break;
405 1.1 rmind default:
406 1.1 rmind KASSERT(false);
407 1.1 rmind }
408 1.1 rmind rw_destroy(&t->t_lock);
409 1.25 christos kmem_free(t, sizeof(npf_table_t));
410 1.1 rmind }
411 1.1 rmind
412 1.1 rmind /*
413 1.19 rmind * npf_table_check: validate the name, ID and type.
414 1.13 rmind */
415 1.1 rmind int
416 1.19 rmind npf_table_check(npf_tableset_t *ts, const char *name, u_int tid, int type)
417 1.1 rmind {
418 1.19 rmind if ((u_int)tid >= ts->ts_nitems) {
419 1.1 rmind return EINVAL;
420 1.1 rmind }
421 1.19 rmind if (ts->ts_map[tid] != NULL) {
422 1.1 rmind return EEXIST;
423 1.1 rmind }
424 1.21 rmind switch (type) {
425 1.21 rmind case NPF_TABLE_TREE:
426 1.21 rmind case NPF_TABLE_HASH:
427 1.21 rmind case NPF_TABLE_CDB:
428 1.21 rmind break;
429 1.21 rmind default:
430 1.1 rmind return EINVAL;
431 1.1 rmind }
432 1.19 rmind if (strlen(name) >= NPF_TABLE_MAXNAMELEN) {
433 1.19 rmind return ENAMETOOLONG;
434 1.19 rmind }
435 1.19 rmind if (npf_tableset_getbyname(ts, name)) {
436 1.20 rmind return EEXIST;
437 1.19 rmind }
438 1.1 rmind return 0;
439 1.1 rmind }
440 1.1 rmind
441 1.13 rmind static int
442 1.15 rmind table_cidr_check(const u_int aidx, const npf_addr_t *addr,
443 1.13 rmind const npf_netmask_t mask)
444 1.13 rmind {
445 1.19 rmind if (aidx > 1) {
446 1.13 rmind return EINVAL;
447 1.13 rmind }
448 1.19 rmind if (mask > NPF_MAX_NETMASK && mask != NPF_NO_NETMASK) {
449 1.13 rmind return EINVAL;
450 1.13 rmind }
451 1.13 rmind
452 1.13 rmind /*
453 1.13 rmind * For IPv4 (aidx = 0) - 32 and for IPv6 (aidx = 1) - 128.
454 1.13 rmind * If it is a host - shall use NPF_NO_NETMASK.
455 1.13 rmind */
456 1.23 christos if (mask > (aidx ? 128 : 32) && mask != NPF_NO_NETMASK) {
457 1.13 rmind return EINVAL;
458 1.13 rmind }
459 1.13 rmind return 0;
460 1.13 rmind }
461 1.13 rmind
462 1.1 rmind /*
463 1.13 rmind * npf_table_insert: add an IP CIDR entry into the table.
464 1.1 rmind */
465 1.1 rmind int
466 1.19 rmind npf_table_insert(npf_table_t *t, const int alen,
467 1.6 zoltan const npf_addr_t *addr, const npf_netmask_t mask)
468 1.1 rmind {
469 1.13 rmind const u_int aidx = NPF_ADDRLEN2TREE(alen);
470 1.13 rmind npf_tblent_t *ent;
471 1.13 rmind int error;
472 1.1 rmind
473 1.15 rmind error = table_cidr_check(aidx, addr, mask);
474 1.13 rmind if (error) {
475 1.13 rmind return error;
476 1.8 rmind }
477 1.12 rmind ent = pool_cache_get(tblent_cache, PR_WAITOK);
478 1.13 rmind memcpy(&ent->te_addr, addr, alen);
479 1.13 rmind ent->te_alen = alen;
480 1.1 rmind
481 1.13 rmind /*
482 1.13 rmind * Insert the entry. Return an error on duplicate.
483 1.13 rmind */
484 1.15 rmind rw_enter(&t->t_lock, RW_WRITER);
485 1.1 rmind switch (t->t_type) {
486 1.13 rmind case NPF_TABLE_HASH: {
487 1.13 rmind struct npf_hashl *htbl;
488 1.13 rmind
489 1.13 rmind /*
490 1.13 rmind * Hash tables by the concept support only IPs.
491 1.13 rmind */
492 1.13 rmind if (mask != NPF_NO_NETMASK) {
493 1.13 rmind error = EINVAL;
494 1.13 rmind break;
495 1.1 rmind }
496 1.13 rmind if (!table_hash_lookup(t, addr, alen, &htbl)) {
497 1.24 christos LIST_INSERT_HEAD(htbl, ent, te_listent);
498 1.15 rmind t->t_nitems++;
499 1.1 rmind } else {
500 1.1 rmind error = EEXIST;
501 1.1 rmind }
502 1.1 rmind break;
503 1.13 rmind }
504 1.13 rmind case NPF_TABLE_TREE: {
505 1.24 christos const unsigned preflen =
506 1.24 christos (mask == NPF_NO_NETMASK) ? (alen * 8) : mask;
507 1.24 christos if (lpm_lookup(t->t_lpm, addr, alen) == NULL &&
508 1.24 christos lpm_insert(t->t_lpm, addr, alen, preflen, ent) == 0) {
509 1.24 christos LIST_INSERT_HEAD(&t->t_list, ent, te_listent);
510 1.24 christos ent->te_preflen = preflen;
511 1.15 rmind t->t_nitems++;
512 1.15 rmind error = 0;
513 1.13 rmind } else {
514 1.15 rmind error = EEXIST;
515 1.1 rmind }
516 1.1 rmind break;
517 1.13 rmind }
518 1.21 rmind case NPF_TABLE_CDB:
519 1.21 rmind error = EINVAL;
520 1.21 rmind break;
521 1.1 rmind default:
522 1.1 rmind KASSERT(false);
523 1.1 rmind }
524 1.15 rmind rw_exit(&t->t_lock);
525 1.1 rmind
526 1.8 rmind if (error) {
527 1.12 rmind pool_cache_put(tblent_cache, ent);
528 1.1 rmind }
529 1.1 rmind return error;
530 1.1 rmind }
531 1.1 rmind
532 1.1 rmind /*
533 1.13 rmind * npf_table_remove: remove the IP CIDR entry from the table.
534 1.1 rmind */
535 1.1 rmind int
536 1.19 rmind npf_table_remove(npf_table_t *t, const int alen,
537 1.6 zoltan const npf_addr_t *addr, const npf_netmask_t mask)
538 1.1 rmind {
539 1.13 rmind const u_int aidx = NPF_ADDRLEN2TREE(alen);
540 1.21 rmind npf_tblent_t *ent = NULL;
541 1.21 rmind int error = ENOENT;
542 1.1 rmind
543 1.15 rmind error = table_cidr_check(aidx, addr, mask);
544 1.13 rmind if (error) {
545 1.13 rmind return error;
546 1.8 rmind }
547 1.15 rmind
548 1.15 rmind rw_enter(&t->t_lock, RW_WRITER);
549 1.13 rmind switch (t->t_type) {
550 1.13 rmind case NPF_TABLE_HASH: {
551 1.13 rmind struct npf_hashl *htbl;
552 1.8 rmind
553 1.13 rmind ent = table_hash_lookup(t, addr, alen, &htbl);
554 1.12 rmind if (__predict_true(ent != NULL)) {
555 1.24 christos LIST_REMOVE(ent, te_listent);
556 1.15 rmind t->t_nitems--;
557 1.1 rmind }
558 1.1 rmind break;
559 1.13 rmind }
560 1.13 rmind case NPF_TABLE_TREE: {
561 1.24 christos ent = lpm_lookup(t->t_lpm, addr, alen);
562 1.12 rmind if (__predict_true(ent != NULL)) {
563 1.24 christos LIST_REMOVE(ent, te_listent);
564 1.24 christos lpm_remove(t->t_lpm, &ent->te_addr,
565 1.24 christos ent->te_alen, ent->te_preflen);
566 1.15 rmind t->t_nitems--;
567 1.1 rmind }
568 1.1 rmind break;
569 1.13 rmind }
570 1.21 rmind case NPF_TABLE_CDB:
571 1.21 rmind error = EINVAL;
572 1.21 rmind break;
573 1.1 rmind default:
574 1.1 rmind KASSERT(false);
575 1.13 rmind ent = NULL;
576 1.1 rmind }
577 1.15 rmind rw_exit(&t->t_lock);
578 1.1 rmind
579 1.21 rmind if (ent) {
580 1.21 rmind pool_cache_put(tblent_cache, ent);
581 1.1 rmind }
582 1.21 rmind return error;
583 1.1 rmind }
584 1.1 rmind
585 1.1 rmind /*
586 1.13 rmind * npf_table_lookup: find the table according to ID, lookup and match
587 1.13 rmind * the contents with the specified IP address.
588 1.1 rmind */
589 1.1 rmind int
590 1.19 rmind npf_table_lookup(npf_table_t *t, const int alen, const npf_addr_t *addr)
591 1.1 rmind {
592 1.13 rmind const u_int aidx = NPF_ADDRLEN2TREE(alen);
593 1.21 rmind struct npf_hashl *htbl;
594 1.21 rmind const void *data;
595 1.21 rmind size_t dlen;
596 1.21 rmind bool found;
597 1.1 rmind
598 1.13 rmind if (__predict_false(aidx > 1)) {
599 1.13 rmind return EINVAL;
600 1.13 rmind }
601 1.13 rmind
602 1.1 rmind switch (t->t_type) {
603 1.21 rmind case NPF_TABLE_HASH:
604 1.21 rmind rw_enter(&t->t_lock, RW_READER);
605 1.21 rmind found = table_hash_lookup(t, addr, alen, &htbl) != NULL;
606 1.21 rmind rw_exit(&t->t_lock);
607 1.1 rmind break;
608 1.21 rmind case NPF_TABLE_TREE:
609 1.21 rmind rw_enter(&t->t_lock, RW_READER);
610 1.24 christos found = lpm_lookup(t->t_lpm, addr, alen) != NULL;
611 1.21 rmind rw_exit(&t->t_lock);
612 1.21 rmind break;
613 1.21 rmind case NPF_TABLE_CDB:
614 1.21 rmind if (cdbr_find(t->t_cdb, addr, alen, &data, &dlen) == 0) {
615 1.25 christos found = dlen == (u_int)alen &&
616 1.25 christos memcmp(addr, data, dlen) == 0;
617 1.21 rmind } else {
618 1.21 rmind found = false;
619 1.21 rmind }
620 1.1 rmind break;
621 1.1 rmind default:
622 1.1 rmind KASSERT(false);
623 1.21 rmind found = false;
624 1.1 rmind }
625 1.1 rmind
626 1.21 rmind return found ? 0 : ENOENT;
627 1.1 rmind }
628 1.15 rmind
629 1.15 rmind static int
630 1.21 rmind table_ent_copyout(const npf_addr_t *addr, const int alen, npf_netmask_t mask,
631 1.15 rmind void *ubuf, size_t len, size_t *off)
632 1.15 rmind {
633 1.15 rmind void *ubufp = (uint8_t *)ubuf + *off;
634 1.15 rmind npf_ioctl_ent_t uent;
635 1.15 rmind
636 1.15 rmind if ((*off += sizeof(npf_ioctl_ent_t)) > len) {
637 1.15 rmind return ENOMEM;
638 1.15 rmind }
639 1.21 rmind uent.alen = alen;
640 1.21 rmind memcpy(&uent.addr, addr, sizeof(npf_addr_t));
641 1.15 rmind uent.mask = mask;
642 1.15 rmind
643 1.15 rmind return copyout(&uent, ubufp, sizeof(npf_ioctl_ent_t));
644 1.15 rmind }
645 1.15 rmind
646 1.15 rmind static int
647 1.21 rmind table_hash_list(const npf_table_t *t, void *ubuf, size_t len)
648 1.21 rmind {
649 1.21 rmind size_t off = 0;
650 1.21 rmind int error = 0;
651 1.21 rmind
652 1.21 rmind for (unsigned n = 0; n <= t->t_hashmask; n++) {
653 1.21 rmind npf_tblent_t *ent;
654 1.21 rmind
655 1.24 christos LIST_FOREACH(ent, &t->t_hashl[n], te_listent) {
656 1.21 rmind error = table_ent_copyout(&ent->te_addr,
657 1.21 rmind ent->te_alen, 0, ubuf, len, &off);
658 1.21 rmind if (error)
659 1.21 rmind break;
660 1.21 rmind }
661 1.21 rmind }
662 1.21 rmind return error;
663 1.21 rmind }
664 1.21 rmind
665 1.21 rmind static int
666 1.24 christos table_tree_list(const npf_table_t *t, void *ubuf, size_t len)
667 1.15 rmind {
668 1.24 christos npf_tblent_t *ent;
669 1.24 christos size_t off = 0;
670 1.15 rmind int error = 0;
671 1.15 rmind
672 1.24 christos LIST_FOREACH(ent, &t->t_list, te_listent) {
673 1.24 christos error = table_ent_copyout(&ent->te_addr,
674 1.24 christos ent->te_alen, 0, ubuf, len, &off);
675 1.21 rmind if (error)
676 1.21 rmind break;
677 1.21 rmind }
678 1.21 rmind return error;
679 1.21 rmind }
680 1.21 rmind
681 1.21 rmind static int
682 1.21 rmind table_cdb_list(npf_table_t *t, void *ubuf, size_t len)
683 1.21 rmind {
684 1.21 rmind size_t off = 0, dlen;
685 1.21 rmind const void *data;
686 1.21 rmind int error = 0;
687 1.21 rmind
688 1.21 rmind for (size_t i = 0; i < t->t_nitems; i++) {
689 1.21 rmind if (cdbr_get(t->t_cdb, i, &data, &dlen) != 0) {
690 1.21 rmind return EINVAL;
691 1.21 rmind }
692 1.21 rmind error = table_ent_copyout(data, dlen, 0, ubuf, len, &off);
693 1.15 rmind if (error)
694 1.15 rmind break;
695 1.15 rmind }
696 1.15 rmind return error;
697 1.15 rmind }
698 1.15 rmind
699 1.15 rmind /*
700 1.15 rmind * npf_table_list: copy a list of all table entries into a userspace buffer.
701 1.15 rmind */
702 1.15 rmind int
703 1.19 rmind npf_table_list(npf_table_t *t, void *ubuf, size_t len)
704 1.15 rmind {
705 1.15 rmind int error = 0;
706 1.15 rmind
707 1.15 rmind rw_enter(&t->t_lock, RW_READER);
708 1.15 rmind switch (t->t_type) {
709 1.15 rmind case NPF_TABLE_HASH:
710 1.21 rmind error = table_hash_list(t, ubuf, len);
711 1.15 rmind break;
712 1.15 rmind case NPF_TABLE_TREE:
713 1.24 christos error = table_tree_list(t, ubuf, len);
714 1.16 rmind break;
715 1.21 rmind case NPF_TABLE_CDB:
716 1.21 rmind error = table_cdb_list(t, ubuf, len);
717 1.21 rmind break;
718 1.15 rmind default:
719 1.15 rmind KASSERT(false);
720 1.15 rmind }
721 1.15 rmind rw_exit(&t->t_lock);
722 1.15 rmind
723 1.15 rmind return error;
724 1.15 rmind }
725 1.18 rmind
726 1.18 rmind /*
727 1.18 rmind * npf_table_flush: remove all table entries.
728 1.18 rmind */
729 1.18 rmind int
730 1.19 rmind npf_table_flush(npf_table_t *t)
731 1.18 rmind {
732 1.21 rmind int error = 0;
733 1.21 rmind
734 1.18 rmind rw_enter(&t->t_lock, RW_WRITER);
735 1.18 rmind switch (t->t_type) {
736 1.18 rmind case NPF_TABLE_HASH:
737 1.24 christos table_hash_flush(t);
738 1.18 rmind t->t_nitems = 0;
739 1.18 rmind break;
740 1.18 rmind case NPF_TABLE_TREE:
741 1.24 christos table_tree_flush(t);
742 1.18 rmind t->t_nitems = 0;
743 1.18 rmind break;
744 1.21 rmind case NPF_TABLE_CDB:
745 1.21 rmind error = EINVAL;
746 1.21 rmind break;
747 1.18 rmind default:
748 1.18 rmind KASSERT(false);
749 1.18 rmind }
750 1.18 rmind rw_exit(&t->t_lock);
751 1.21 rmind return error;
752 1.18 rmind }
753