npf_tableset.c revision 1.5.2.2 1 1.5.2.2 rmind /* $NetBSD: npf_tableset.c,v 1.5.2.2 2011/03/05 20:55:56 rmind Exp $ */
2 1.5.2.2 rmind
3 1.5.2.2 rmind /*-
4 1.5.2.2 rmind * Copyright (c) 2009-2010 The NetBSD Foundation, Inc.
5 1.5.2.2 rmind * All rights reserved.
6 1.5.2.2 rmind *
7 1.5.2.2 rmind * This material is based upon work partially supported by The
8 1.5.2.2 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.5.2.2 rmind *
10 1.5.2.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.5.2.2 rmind * modification, are permitted provided that the following conditions
12 1.5.2.2 rmind * are met:
13 1.5.2.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.5.2.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.5.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.5.2.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.5.2.2 rmind * documentation and/or other materials provided with the distribution.
18 1.5.2.2 rmind *
19 1.5.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.5.2.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.5.2.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.5.2.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.5.2.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.5.2.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.5.2.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.5.2.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.5.2.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.5.2.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.5.2.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.5.2.2 rmind */
31 1.5.2.2 rmind
32 1.5.2.2 rmind /*
33 1.5.2.2 rmind * NPF tableset module.
34 1.5.2.2 rmind *
35 1.5.2.2 rmind * TODO:
36 1.5.2.2 rmind * - Currently, code is modeled to handle IPv4 CIDR blocks.
37 1.5.2.2 rmind * - Dynamic hash growing/shrinking (i.e. re-hash functionality), maybe?
38 1.5.2.2 rmind * - Dynamic array resize.
39 1.5.2.2 rmind */
40 1.5.2.2 rmind
41 1.5.2.2 rmind #include <sys/cdefs.h>
42 1.5.2.2 rmind __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.5.2.2 2011/03/05 20:55:56 rmind Exp $");
43 1.5.2.2 rmind
44 1.5.2.2 rmind #include <sys/param.h>
45 1.5.2.2 rmind #include <sys/kernel.h>
46 1.5.2.2 rmind
47 1.5.2.2 rmind #include <sys/atomic.h>
48 1.5.2.2 rmind #include <sys/hash.h>
49 1.5.2.2 rmind #include <sys/kmem.h>
50 1.5.2.2 rmind #include <sys/pool.h>
51 1.5.2.2 rmind #include <sys/queue.h>
52 1.5.2.2 rmind #include <sys/rwlock.h>
53 1.5.2.2 rmind #include <sys/systm.h>
54 1.5.2.2 rmind #include <sys/types.h>
55 1.5.2.2 rmind
56 1.5.2.2 rmind #include "npf_impl.h"
57 1.5.2.2 rmind
58 1.5.2.2 rmind /* Table entry structure. */
59 1.5.2.2 rmind struct npf_tblent {
60 1.5.2.2 rmind /* Hash/tree entry. */
61 1.5.2.2 rmind union {
62 1.5.2.2 rmind LIST_ENTRY(npf_tblent) hashq;
63 1.5.2.2 rmind rb_node_t rbnode;
64 1.5.2.2 rmind } te_entry;
65 1.5.2.2 rmind /* IPv4 CIDR block. */
66 1.5.2.2 rmind in_addr_t te_addr;
67 1.5.2.2 rmind in_addr_t te_mask;
68 1.5.2.2 rmind };
69 1.5.2.2 rmind
70 1.5.2.2 rmind LIST_HEAD(npf_hashl, npf_tblent);
71 1.5.2.2 rmind
72 1.5.2.2 rmind /* Table structure. */
73 1.5.2.2 rmind struct npf_table {
74 1.5.2.2 rmind char t_name[16];
75 1.5.2.2 rmind /* Lock and reference count. */
76 1.5.2.2 rmind krwlock_t t_lock;
77 1.5.2.2 rmind u_int t_refcnt;
78 1.5.2.2 rmind /* Table ID. */
79 1.5.2.2 rmind u_int t_id;
80 1.5.2.2 rmind /* The storage type can be: 1. Hash 2. RB-tree. */
81 1.5.2.2 rmind int t_type;
82 1.5.2.2 rmind struct npf_hashl * t_hashl;
83 1.5.2.2 rmind u_long t_hashmask;
84 1.5.2.2 rmind rb_tree_t t_rbtree;
85 1.5.2.2 rmind };
86 1.5.2.2 rmind
87 1.5.2.2 rmind static pool_cache_t tblent_cache __read_mostly;
88 1.5.2.2 rmind
89 1.5.2.2 rmind /*
90 1.5.2.2 rmind * npf_table_sysinit: initialise tableset structures.
91 1.5.2.2 rmind */
92 1.5.2.2 rmind void
93 1.5.2.2 rmind npf_tableset_sysinit(void)
94 1.5.2.2 rmind {
95 1.5.2.2 rmind
96 1.5.2.2 rmind tblent_cache = pool_cache_init(sizeof(npf_tblent_t), coherency_unit,
97 1.5.2.2 rmind 0, 0, "npftenpl", NULL, IPL_NONE, NULL, NULL, NULL);
98 1.5.2.2 rmind }
99 1.5.2.2 rmind
100 1.5.2.2 rmind void
101 1.5.2.2 rmind npf_tableset_sysfini(void)
102 1.5.2.2 rmind {
103 1.5.2.2 rmind
104 1.5.2.2 rmind pool_cache_destroy(tblent_cache);
105 1.5.2.2 rmind }
106 1.5.2.2 rmind
107 1.5.2.2 rmind npf_tableset_t *
108 1.5.2.2 rmind npf_tableset_create(void)
109 1.5.2.2 rmind {
110 1.5.2.2 rmind const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
111 1.5.2.2 rmind
112 1.5.2.2 rmind return kmem_zalloc(sz, KM_SLEEP);
113 1.5.2.2 rmind }
114 1.5.2.2 rmind
115 1.5.2.2 rmind void
116 1.5.2.2 rmind npf_tableset_destroy(npf_tableset_t *tblset)
117 1.5.2.2 rmind {
118 1.5.2.2 rmind const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
119 1.5.2.2 rmind npf_table_t *t;
120 1.5.2.2 rmind u_int tid;
121 1.5.2.2 rmind
122 1.5.2.2 rmind /*
123 1.5.2.2 rmind * Destroy all tables (no references should be held, as ruleset
124 1.5.2.2 rmind * should be destroyed before).
125 1.5.2.2 rmind */
126 1.5.2.2 rmind for (tid = 0; tid < NPF_TABLE_SLOTS; tid++) {
127 1.5.2.2 rmind t = tblset[tid];
128 1.5.2.2 rmind if (t != NULL) {
129 1.5.2.2 rmind npf_table_destroy(t);
130 1.5.2.2 rmind }
131 1.5.2.2 rmind }
132 1.5.2.2 rmind kmem_free(tblset, sz);
133 1.5.2.2 rmind }
134 1.5.2.2 rmind
135 1.5.2.2 rmind /*
136 1.5.2.2 rmind * npf_tableset_insert: insert the table into the specified tableset.
137 1.5.2.2 rmind *
138 1.5.2.2 rmind * => Returns 0 on success, fails and returns errno if ID is already used.
139 1.5.2.2 rmind */
140 1.5.2.2 rmind int
141 1.5.2.2 rmind npf_tableset_insert(npf_tableset_t *tblset, npf_table_t *t)
142 1.5.2.2 rmind {
143 1.5.2.2 rmind const u_int tid = t->t_id;
144 1.5.2.2 rmind int error;
145 1.5.2.2 rmind
146 1.5.2.2 rmind KASSERT((u_int)tid < NPF_TABLE_SLOTS);
147 1.5.2.2 rmind
148 1.5.2.2 rmind if (tblset[tid] == NULL) {
149 1.5.2.2 rmind tblset[tid] = t;
150 1.5.2.2 rmind error = 0;
151 1.5.2.2 rmind } else {
152 1.5.2.2 rmind error = EEXIST;
153 1.5.2.2 rmind }
154 1.5.2.2 rmind return error;
155 1.5.2.2 rmind }
156 1.5.2.2 rmind
157 1.5.2.2 rmind /*
158 1.5.2.2 rmind * Red-black tree storage.
159 1.5.2.2 rmind */
160 1.5.2.2 rmind
161 1.5.2.2 rmind static signed int
162 1.5.2.2 rmind table_rbtree_cmp_nodes(void *ctx, const void *n1, const void *n2)
163 1.5.2.2 rmind {
164 1.5.2.2 rmind const npf_tblent_t * const te1 = n1;
165 1.5.2.2 rmind const npf_tblent_t * const te2 = n2;
166 1.5.2.2 rmind const in_addr_t x = te1->te_addr & te1->te_mask;
167 1.5.2.2 rmind const in_addr_t y = te2->te_addr & te2->te_mask;
168 1.5.2.2 rmind
169 1.5.2.2 rmind if (x < y)
170 1.5.2.2 rmind return -1;
171 1.5.2.2 rmind if (x > y)
172 1.5.2.2 rmind return 1;
173 1.5.2.2 rmind return 0;
174 1.5.2.2 rmind }
175 1.5.2.2 rmind
176 1.5.2.2 rmind static signed int
177 1.5.2.2 rmind table_rbtree_cmp_key(void *ctx, const void *n1, const void *key)
178 1.5.2.2 rmind {
179 1.5.2.2 rmind const npf_tblent_t * const te = n1;
180 1.5.2.2 rmind const in_addr_t x = te->te_addr & te->te_mask;
181 1.5.2.2 rmind const in_addr_t y = *(const in_addr_t *)key;
182 1.5.2.2 rmind
183 1.5.2.2 rmind if (x < y)
184 1.5.2.2 rmind return -1;
185 1.5.2.2 rmind if (x > y)
186 1.5.2.2 rmind return 1;
187 1.5.2.2 rmind return 0;
188 1.5.2.2 rmind }
189 1.5.2.2 rmind
190 1.5.2.2 rmind static const rb_tree_ops_t table_rbtree_ops = {
191 1.5.2.2 rmind .rbto_compare_nodes = table_rbtree_cmp_nodes,
192 1.5.2.2 rmind .rbto_compare_key = table_rbtree_cmp_key,
193 1.5.2.2 rmind .rbto_node_offset = offsetof(npf_tblent_t, te_entry.rbnode),
194 1.5.2.2 rmind .rbto_context = NULL
195 1.5.2.2 rmind };
196 1.5.2.2 rmind
197 1.5.2.2 rmind /*
198 1.5.2.2 rmind * Hash helper routine.
199 1.5.2.2 rmind */
200 1.5.2.2 rmind
201 1.5.2.2 rmind static inline struct npf_hashl *
202 1.5.2.2 rmind table_hash_bucket(npf_table_t *t, void *buf, size_t sz)
203 1.5.2.2 rmind {
204 1.5.2.2 rmind const uint32_t hidx = hash32_buf(buf, sz, HASH32_BUF_INIT);
205 1.5.2.2 rmind
206 1.5.2.2 rmind return &t->t_hashl[hidx & t->t_hashmask];
207 1.5.2.2 rmind }
208 1.5.2.2 rmind
209 1.5.2.2 rmind /*
210 1.5.2.2 rmind * npf_table_create: create table with a specified ID.
211 1.5.2.2 rmind */
212 1.5.2.2 rmind npf_table_t *
213 1.5.2.2 rmind npf_table_create(u_int tid, int type, size_t hsize)
214 1.5.2.2 rmind {
215 1.5.2.2 rmind npf_table_t *t;
216 1.5.2.2 rmind
217 1.5.2.2 rmind KASSERT((u_int)tid < NPF_TABLE_SLOTS);
218 1.5.2.2 rmind
219 1.5.2.2 rmind t = kmem_zalloc(sizeof(npf_table_t), KM_SLEEP);
220 1.5.2.2 rmind switch (type) {
221 1.5.2.2 rmind case NPF_TABLE_RBTREE:
222 1.5.2.2 rmind rb_tree_init(&t->t_rbtree, &table_rbtree_ops);
223 1.5.2.2 rmind break;
224 1.5.2.2 rmind case NPF_TABLE_HASH:
225 1.5.2.2 rmind t->t_hashl = hashinit(hsize, HASH_LIST, true, &t->t_hashmask);
226 1.5.2.2 rmind if (t->t_hashl == NULL) {
227 1.5.2.2 rmind kmem_free(t, sizeof(npf_table_t));
228 1.5.2.2 rmind return NULL;
229 1.5.2.2 rmind }
230 1.5.2.2 rmind break;
231 1.5.2.2 rmind default:
232 1.5.2.2 rmind KASSERT(false);
233 1.5.2.2 rmind }
234 1.5.2.2 rmind rw_init(&t->t_lock);
235 1.5.2.2 rmind t->t_type = type;
236 1.5.2.2 rmind t->t_refcnt = 1;
237 1.5.2.2 rmind t->t_id = tid;
238 1.5.2.2 rmind return t;
239 1.5.2.2 rmind }
240 1.5.2.2 rmind
241 1.5.2.2 rmind /*
242 1.5.2.2 rmind * npf_table_destroy: free all table entries and table itself.
243 1.5.2.2 rmind */
244 1.5.2.2 rmind void
245 1.5.2.2 rmind npf_table_destroy(npf_table_t *t)
246 1.5.2.2 rmind {
247 1.5.2.2 rmind npf_tblent_t *e;
248 1.5.2.2 rmind u_int n;
249 1.5.2.2 rmind
250 1.5.2.2 rmind switch (t->t_type) {
251 1.5.2.2 rmind case NPF_TABLE_HASH:
252 1.5.2.2 rmind for (n = 0; n <= t->t_hashmask; n++) {
253 1.5.2.2 rmind while ((e = LIST_FIRST(&t->t_hashl[n])) != NULL) {
254 1.5.2.2 rmind LIST_REMOVE(e, te_entry.hashq);
255 1.5.2.2 rmind pool_cache_put(tblent_cache, e);
256 1.5.2.2 rmind }
257 1.5.2.2 rmind }
258 1.5.2.2 rmind hashdone(t->t_hashl, HASH_LIST, t->t_hashmask);
259 1.5.2.2 rmind break;
260 1.5.2.2 rmind case NPF_TABLE_RBTREE:
261 1.5.2.2 rmind while ((e = rb_tree_iterate(&t->t_rbtree, NULL,
262 1.5.2.2 rmind RB_DIR_LEFT)) != NULL) {
263 1.5.2.2 rmind rb_tree_remove_node(&t->t_rbtree, e);
264 1.5.2.2 rmind pool_cache_put(tblent_cache, e);
265 1.5.2.2 rmind }
266 1.5.2.2 rmind break;
267 1.5.2.2 rmind default:
268 1.5.2.2 rmind KASSERT(false);
269 1.5.2.2 rmind }
270 1.5.2.2 rmind rw_destroy(&t->t_lock);
271 1.5.2.2 rmind kmem_free(t, sizeof(npf_table_t));
272 1.5.2.2 rmind }
273 1.5.2.2 rmind
274 1.5.2.2 rmind /*
275 1.5.2.2 rmind * npf_table_ref: holds the reference on table.
276 1.5.2.2 rmind *
277 1.5.2.2 rmind * => Table must be locked.
278 1.5.2.2 rmind */
279 1.5.2.2 rmind void
280 1.5.2.2 rmind npf_table_ref(npf_table_t *t)
281 1.5.2.2 rmind {
282 1.5.2.2 rmind
283 1.5.2.2 rmind KASSERT(rw_lock_held(&t->t_lock));
284 1.5.2.2 rmind atomic_inc_uint(&t->t_refcnt);
285 1.5.2.2 rmind }
286 1.5.2.2 rmind
287 1.5.2.2 rmind /*
288 1.5.2.2 rmind * npf_table_unref: drop reference from the table and destroy the table if
289 1.5.2.2 rmind * it is the last reference.
290 1.5.2.2 rmind */
291 1.5.2.2 rmind void
292 1.5.2.2 rmind npf_table_unref(npf_table_t *t)
293 1.5.2.2 rmind {
294 1.5.2.2 rmind
295 1.5.2.2 rmind if (atomic_dec_uint_nv(&t->t_refcnt) != 0) {
296 1.5.2.2 rmind return;
297 1.5.2.2 rmind }
298 1.5.2.2 rmind npf_table_destroy(t);
299 1.5.2.2 rmind }
300 1.5.2.2 rmind
301 1.5.2.2 rmind /*
302 1.5.2.2 rmind * npf_table_get: find the table according to ID and "get it" by locking it.
303 1.5.2.2 rmind */
304 1.5.2.2 rmind npf_table_t *
305 1.5.2.2 rmind npf_table_get(npf_tableset_t *tset, u_int tid)
306 1.5.2.2 rmind {
307 1.5.2.2 rmind npf_tableset_t *rtset;
308 1.5.2.2 rmind npf_table_t *t;
309 1.5.2.2 rmind
310 1.5.2.2 rmind if ((u_int)tid >= NPF_TABLE_SLOTS) {
311 1.5.2.2 rmind return NULL;
312 1.5.2.2 rmind }
313 1.5.2.2 rmind rtset = tset ? tset : npf_core_tableset();
314 1.5.2.2 rmind t = rtset[tid];
315 1.5.2.2 rmind if (t != NULL) {
316 1.5.2.2 rmind rw_enter(&t->t_lock, RW_READER);
317 1.5.2.2 rmind }
318 1.5.2.2 rmind return t;
319 1.5.2.2 rmind }
320 1.5.2.2 rmind
321 1.5.2.2 rmind /*
322 1.5.2.2 rmind * npf_table_put: "put table back" by unlocking it.
323 1.5.2.2 rmind */
324 1.5.2.2 rmind void
325 1.5.2.2 rmind npf_table_put(npf_table_t *t)
326 1.5.2.2 rmind {
327 1.5.2.2 rmind
328 1.5.2.2 rmind rw_exit(&t->t_lock);
329 1.5.2.2 rmind }
330 1.5.2.2 rmind
331 1.5.2.2 rmind /*
332 1.5.2.2 rmind * npf_table_check: validate ID and type.
333 1.5.2.2 rmind * */
334 1.5.2.2 rmind int
335 1.5.2.2 rmind npf_table_check(npf_tableset_t *tset, u_int tid, int type)
336 1.5.2.2 rmind {
337 1.5.2.2 rmind
338 1.5.2.2 rmind if ((u_int)tid >= NPF_TABLE_SLOTS) {
339 1.5.2.2 rmind return EINVAL;
340 1.5.2.2 rmind }
341 1.5.2.2 rmind if (tset[tid] != NULL) {
342 1.5.2.2 rmind return EEXIST;
343 1.5.2.2 rmind }
344 1.5.2.2 rmind if (type != NPF_TABLE_RBTREE && type != NPF_TABLE_HASH) {
345 1.5.2.2 rmind return EINVAL;
346 1.5.2.2 rmind }
347 1.5.2.2 rmind return 0;
348 1.5.2.2 rmind }
349 1.5.2.2 rmind
350 1.5.2.2 rmind /*
351 1.5.2.2 rmind * npf_table_add_v4cidr: add an IPv4 CIDR into the table.
352 1.5.2.2 rmind */
353 1.5.2.2 rmind int
354 1.5.2.2 rmind npf_table_add_v4cidr(npf_tableset_t *tset, u_int tid,
355 1.5.2.2 rmind in_addr_t addr, in_addr_t mask)
356 1.5.2.2 rmind {
357 1.5.2.2 rmind struct npf_hashl *htbl;
358 1.5.2.2 rmind npf_tblent_t *e, *it;
359 1.5.2.2 rmind npf_table_t *t;
360 1.5.2.2 rmind in_addr_t val;
361 1.5.2.2 rmind int error = 0;
362 1.5.2.2 rmind
363 1.5.2.2 rmind /* Allocate and setup entry. */
364 1.5.2.2 rmind e = pool_cache_get(tblent_cache, PR_WAITOK);
365 1.5.2.2 rmind e->te_addr = addr;
366 1.5.2.2 rmind e->te_mask = mask;
367 1.5.2.2 rmind
368 1.5.2.2 rmind /* Locks the table. */
369 1.5.2.2 rmind t = npf_table_get(tset, tid);
370 1.5.2.2 rmind if (__predict_false(t == NULL)) {
371 1.5.2.2 rmind pool_cache_put(tblent_cache, e);
372 1.5.2.2 rmind return EINVAL;
373 1.5.2.2 rmind }
374 1.5.2.2 rmind switch (t->t_type) {
375 1.5.2.2 rmind case NPF_TABLE_HASH:
376 1.5.2.2 rmind /* Generate hash value from: address & mask. */
377 1.5.2.2 rmind val = addr & mask;
378 1.5.2.2 rmind htbl = table_hash_bucket(t, &val, sizeof(in_addr_t));
379 1.5.2.2 rmind /* Lookup to check for duplicates. */
380 1.5.2.2 rmind LIST_FOREACH(it, htbl, te_entry.hashq) {
381 1.5.2.2 rmind if (it->te_addr == addr && it->te_mask == mask)
382 1.5.2.2 rmind break;
383 1.5.2.2 rmind }
384 1.5.2.2 rmind /* If no duplicate - insert entry. */
385 1.5.2.2 rmind if (__predict_true(it == NULL)) {
386 1.5.2.2 rmind LIST_INSERT_HEAD(htbl, e, te_entry.hashq);
387 1.5.2.2 rmind } else {
388 1.5.2.2 rmind error = EEXIST;
389 1.5.2.2 rmind }
390 1.5.2.2 rmind break;
391 1.5.2.2 rmind case NPF_TABLE_RBTREE:
392 1.5.2.2 rmind /* Insert entry. Returns false, if duplicate. */
393 1.5.2.2 rmind if (rb_tree_insert_node(&t->t_rbtree, e) != e) {
394 1.5.2.2 rmind error = EEXIST;
395 1.5.2.2 rmind }
396 1.5.2.2 rmind break;
397 1.5.2.2 rmind default:
398 1.5.2.2 rmind KASSERT(false);
399 1.5.2.2 rmind }
400 1.5.2.2 rmind npf_table_put(t);
401 1.5.2.2 rmind
402 1.5.2.2 rmind if (__predict_false(error)) {
403 1.5.2.2 rmind pool_cache_put(tblent_cache, e);
404 1.5.2.2 rmind }
405 1.5.2.2 rmind return error;
406 1.5.2.2 rmind }
407 1.5.2.2 rmind
408 1.5.2.2 rmind /*
409 1.5.2.2 rmind * npf_table_rem_v4cidr: remove an IPv4 CIDR from the table.
410 1.5.2.2 rmind */
411 1.5.2.2 rmind int
412 1.5.2.2 rmind npf_table_rem_v4cidr(npf_tableset_t *tset, u_int tid,
413 1.5.2.2 rmind in_addr_t addr, in_addr_t mask)
414 1.5.2.2 rmind {
415 1.5.2.2 rmind struct npf_hashl *htbl;
416 1.5.2.2 rmind npf_tblent_t *e;
417 1.5.2.2 rmind npf_table_t *t;
418 1.5.2.2 rmind in_addr_t val;
419 1.5.2.2 rmind int error;
420 1.5.2.2 rmind
421 1.5.2.2 rmind e = NULL;
422 1.5.2.2 rmind
423 1.5.2.2 rmind /* Locks the table. */
424 1.5.2.2 rmind t = npf_table_get(tset, tid);
425 1.5.2.2 rmind if (__predict_false(t == NULL)) {
426 1.5.2.2 rmind return EINVAL;
427 1.5.2.2 rmind }
428 1.5.2.2 rmind /* Lookup & remove. */
429 1.5.2.2 rmind switch (t->t_type) {
430 1.5.2.2 rmind case NPF_TABLE_HASH:
431 1.5.2.2 rmind /* Generate hash value from: (address & mask). */
432 1.5.2.2 rmind val = addr & mask;
433 1.5.2.2 rmind htbl = table_hash_bucket(t, &val, sizeof(in_addr_t));
434 1.5.2.2 rmind LIST_FOREACH(e, htbl, te_entry.hashq) {
435 1.5.2.2 rmind if (e->te_addr == addr && e->te_mask == mask)
436 1.5.2.2 rmind break;
437 1.5.2.2 rmind }
438 1.5.2.2 rmind if (__predict_true(e != NULL)) {
439 1.5.2.2 rmind LIST_REMOVE(e, te_entry.hashq);
440 1.5.2.2 rmind } else {
441 1.5.2.2 rmind error = ESRCH;
442 1.5.2.2 rmind }
443 1.5.2.2 rmind break;
444 1.5.2.2 rmind case NPF_TABLE_RBTREE:
445 1.5.2.2 rmind /* Key: (address & mask). */
446 1.5.2.2 rmind val = addr & mask;
447 1.5.2.2 rmind e = rb_tree_find_node(&t->t_rbtree, &val);
448 1.5.2.2 rmind if (__predict_true(e != NULL)) {
449 1.5.2.2 rmind rb_tree_remove_node(&t->t_rbtree, e);
450 1.5.2.2 rmind } else {
451 1.5.2.2 rmind error = ESRCH;
452 1.5.2.2 rmind }
453 1.5.2.2 rmind break;
454 1.5.2.2 rmind default:
455 1.5.2.2 rmind KASSERT(false);
456 1.5.2.2 rmind }
457 1.5.2.2 rmind npf_table_put(t);
458 1.5.2.2 rmind
459 1.5.2.2 rmind /* Free table the entry. */
460 1.5.2.2 rmind if (__predict_true(e != NULL)) {
461 1.5.2.2 rmind pool_cache_put(tblent_cache, e);
462 1.5.2.2 rmind }
463 1.5.2.2 rmind return e ? 0 : -1;
464 1.5.2.2 rmind }
465 1.5.2.2 rmind
466 1.5.2.2 rmind /*
467 1.5.2.2 rmind * npf_table_match_v4addr: find the table according to ID, lookup and
468 1.5.2.2 rmind * match the contents with specified IPv4 address.
469 1.5.2.2 rmind */
470 1.5.2.2 rmind int
471 1.5.2.2 rmind npf_table_match_v4addr(u_int tid, in_addr_t ip4addr)
472 1.5.2.2 rmind {
473 1.5.2.2 rmind struct npf_hashl *htbl;
474 1.5.2.2 rmind npf_tblent_t *e = NULL;
475 1.5.2.2 rmind npf_table_t *t;
476 1.5.2.2 rmind
477 1.5.2.2 rmind /* Locks the table. */
478 1.5.2.2 rmind t = npf_table_get(NULL, tid);
479 1.5.2.2 rmind if (__predict_false(t == NULL)) {
480 1.5.2.2 rmind return EINVAL;
481 1.5.2.2 rmind }
482 1.5.2.2 rmind switch (t->t_type) {
483 1.5.2.2 rmind case NPF_TABLE_HASH:
484 1.5.2.2 rmind htbl = table_hash_bucket(t, &ip4addr, sizeof(in_addr_t));
485 1.5.2.2 rmind LIST_FOREACH(e, htbl, te_entry.hashq) {
486 1.5.2.2 rmind if ((ip4addr & e->te_mask) == e->te_addr) {
487 1.5.2.2 rmind break;
488 1.5.2.2 rmind }
489 1.5.2.2 rmind }
490 1.5.2.2 rmind break;
491 1.5.2.2 rmind case NPF_TABLE_RBTREE:
492 1.5.2.2 rmind e = rb_tree_find_node(&t->t_rbtree, &ip4addr);
493 1.5.2.2 rmind KASSERT((ip4addr & e->te_mask) == e->te_addr);
494 1.5.2.2 rmind break;
495 1.5.2.2 rmind default:
496 1.5.2.2 rmind KASSERT(false);
497 1.5.2.2 rmind }
498 1.5.2.2 rmind npf_table_put(t);
499 1.5.2.2 rmind
500 1.5.2.2 rmind return e ? 0 : -1;
501 1.5.2.2 rmind }
502