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