npf_portmap.c revision 1.2 1 1.1 rmind /*-
2 1.1 rmind * Copyright (c) 2019 Mindaugas Rasiukevicius <rmind at noxt eu>
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * Redistribution and use in source and binary forms, with or without
6 1.1 rmind * modification, are permitted provided that the following conditions
7 1.1 rmind * are met:
8 1.1 rmind * 1. Redistributions of source code must retain the above copyright
9 1.1 rmind * notice, this list of conditions and the following disclaimer.
10 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 rmind * notice, this list of conditions and the following disclaimer in the
12 1.1 rmind * documentation and/or other materials provided with the distribution.
13 1.1 rmind *
14 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 1.1 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 1.1 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 1.1 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 1.1 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 1.1 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 1.1 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 1.1 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 1.1 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 1.1 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 1.1 rmind * SUCH DAMAGE.
25 1.1 rmind */
26 1.1 rmind
27 1.1 rmind /*
28 1.1 rmind * NPF port map mechanism.
29 1.1 rmind *
30 1.1 rmind * The port map is a bitmap used to track TCP/UDP ports used for
31 1.1 rmind * translation. Port maps are per IP addresses, therefore multiple
32 1.1 rmind * NAT policies operating on the same IP address will share the
33 1.1 rmind * same port map.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #ifdef _KERNEL
37 1.1 rmind #include <sys/cdefs.h>
38 1.2 rmind __KERNEL_RCSID(0, "$NetBSD: npf_portmap.c,v 1.2 2019/07/23 08:25:52 rmind Exp $");
39 1.1 rmind
40 1.1 rmind #include <sys/param.h>
41 1.1 rmind #include <sys/types.h>
42 1.1 rmind
43 1.1 rmind #include <sys/atomic.h>
44 1.1 rmind #include <sys/bitops.h>
45 1.1 rmind #include <sys/kmem.h>
46 1.1 rmind #include <sys/mutex.h>
47 1.1 rmind #include <sys/cprng.h>
48 1.1 rmind #include <sys/thmap.h>
49 1.1 rmind #endif
50 1.1 rmind
51 1.1 rmind #include "npf_impl.h"
52 1.1 rmind
53 1.1 rmind /*
54 1.1 rmind * Port map uses two-level bitmaps with compression to efficiently
55 1.1 rmind * represent the maximum of 65536 (2^16) values.
56 1.1 rmind *
57 1.1 rmind * Level 0: 64 chunks each representing 1048 bits in two modes:
58 1.1 rmind *
59 1.1 rmind * a) If PORTMAP_L1_TAG, then up to 5 values are packed in the
60 1.1 rmind * 64-bit integer using 12 bits for each value, starting from the
61 1.1 rmind * most significant bits. The four 4 least significant bits are
62 1.1 rmind * unused or reserved for pointer tagging.
63 1.1 rmind *
64 1.1 rmind * b) If there are more than 5 values, then PORTMAP_L1_TAG is set
65 1.1 rmind * and the value serves as a pointer to the second level bitmap.
66 1.1 rmind *
67 1.1 rmind * Level 1: 16 chunks each representing 64 bits in plain uint64_t.
68 1.1 rmind */
69 1.1 rmind
70 1.1 rmind #define PORTMAP_MAX_BITS (65536U)
71 1.1 rmind #define PORTMAP_MASK (PORTMAP_MAX_BITS - 1)
72 1.1 rmind
73 1.1 rmind #define PORTMAP_L0_SHIFT (10) // or 11
74 1.1 rmind #define PORTMAP_L0_MASK ((1U << PORTMAP_L0_SHIFT) - 1)
75 1.1 rmind #define PORTMAP_L0_WORDS (PORTMAP_MAX_BITS >> PORTMAP_L0_SHIFT)
76 1.1 rmind
77 1.1 rmind #define PORTMAP_L1_SHIFT (6)
78 1.1 rmind #define PORTMAP_L1_MASK ((1U << PORTMAP_L1_SHIFT) - 1)
79 1.1 rmind #define PORTMAP_L1_WORDS \
80 1.1 rmind ((PORTMAP_MAX_BITS / PORTMAP_L0_WORDS) >> PORTMAP_L1_SHIFT)
81 1.1 rmind
82 1.1 rmind #define PORTMAP_L1_TAG (UINT64_C(1)) // use level 1
83 1.1 rmind #define PORTMAP_L1_GET(p) ((void *)((uintptr_t)(p) & ~(uintptr_t)3))
84 1.1 rmind
85 1.1 rmind CTASSERT(sizeof(uint64_t) >= sizeof(uintptr_t));
86 1.1 rmind
87 1.1 rmind typedef struct {
88 1.1 rmind volatile uint64_t bits1[PORTMAP_L1_WORDS];
89 1.1 rmind } bitmap_l1_t;
90 1.1 rmind
91 1.1 rmind typedef struct bitmap {
92 1.1 rmind npf_addr_t addr;
93 1.1 rmind volatile uint64_t bits0[PORTMAP_L0_WORDS];
94 1.1 rmind LIST_ENTRY(bitmap) entry;
95 1.1 rmind unsigned addr_len;
96 1.1 rmind } bitmap_t;
97 1.1 rmind
98 1.1 rmind struct npf_portmap {
99 1.1 rmind thmap_t * addr_map;
100 1.1 rmind LIST_HEAD(, bitmap) bitmap_list;
101 1.1 rmind kmutex_t list_lock;
102 1.1 rmind };
103 1.1 rmind
104 1.1 rmind typedef struct {
105 1.1 rmind int min_port;
106 1.1 rmind int max_port;
107 1.1 rmind } npf_portmap_params_t;
108 1.1 rmind
109 1.2 rmind static kmutex_t portmap_lock;
110 1.2 rmind
111 1.1 rmind void
112 1.1 rmind npf_portmap_init(npf_t *npf)
113 1.1 rmind {
114 1.1 rmind npf_portmap_params_t *params = npf_param_allocgroup(npf,
115 1.1 rmind NPF_PARAMS_PORTMAP, sizeof(npf_portmap_params_t));
116 1.1 rmind npf_param_t param_map[] = {
117 1.1 rmind {
118 1.1 rmind "portmap.min_port",
119 1.1 rmind ¶ms->min_port,
120 1.1 rmind .default_val = 1024,
121 1.1 rmind .min = 1024, .max = 65535
122 1.1 rmind },
123 1.1 rmind {
124 1.1 rmind "portmap.max_port",
125 1.1 rmind ¶ms->max_port,
126 1.1 rmind .default_val = 65535,
127 1.1 rmind .min = 1024, .max = 65535
128 1.1 rmind }
129 1.1 rmind };
130 1.1 rmind npf_param_register(npf, param_map, __arraycount(param_map));
131 1.1 rmind
132 1.1 rmind npf->portmap = kmem_zalloc(sizeof(npf_portmap_t), KM_SLEEP);
133 1.1 rmind mutex_init(&npf->portmap->list_lock, MUTEX_DEFAULT, IPL_SOFTNET);
134 1.1 rmind npf->portmap->addr_map = thmap_create(0, NULL, THMAP_NOCOPY);
135 1.2 rmind
136 1.2 rmind mutex_init(&portmap_lock, MUTEX_DEFAULT, IPL_SOFTNET);
137 1.1 rmind }
138 1.1 rmind
139 1.1 rmind void
140 1.1 rmind npf_portmap_fini(npf_t *npf)
141 1.1 rmind {
142 1.1 rmind const size_t len = sizeof(npf_portmap_params_t);
143 1.1 rmind npf_portmap_t *pm = npf->portmap;
144 1.1 rmind
145 1.1 rmind npf_param_freegroup(npf, NPF_PARAMS_PORTMAP, len);
146 1.1 rmind
147 1.1 rmind npf_portmap_flush(npf);
148 1.1 rmind KASSERT(LIST_EMPTY(&pm->bitmap_list));
149 1.1 rmind
150 1.1 rmind thmap_destroy(pm->addr_map);
151 1.1 rmind mutex_destroy(&pm->list_lock);
152 1.1 rmind kmem_free(pm, sizeof(npf_portmap_t));
153 1.2 rmind
154 1.2 rmind mutex_destroy(&portmap_lock);
155 1.1 rmind }
156 1.1 rmind
157 1.1 rmind /////////////////////////////////////////////////////////////////////////
158 1.1 rmind
159 1.2 rmind #if defined(_LP64)
160 1.2 rmind #define __npf_atomic_cas_64 atomic_cas_64
161 1.2 rmind #else
162 1.2 rmind static uint64_t
163 1.2 rmind __npf_atomic_cas_64(volatile uint64_t *ptr, uint64_t old, uint64_t new)
164 1.2 rmind {
165 1.2 rmind uint64_t prev;
166 1.2 rmind
167 1.2 rmind mutex_enter(&portmap_lock);
168 1.2 rmind prev = *ptr;
169 1.2 rmind if (prev == old) {
170 1.2 rmind *ptr = new;
171 1.2 rmind }
172 1.2 rmind mutex_exit(&portmap_lock);
173 1.2 rmind
174 1.2 rmind return prev;
175 1.2 rmind }
176 1.2 rmind #endif
177 1.2 rmind
178 1.1 rmind /*
179 1.1 rmind * bitmap_word_isset: test whether the bit value is in the packed array.
180 1.1 rmind *
181 1.1 rmind * => Return true if any value equals the bit number value.
182 1.1 rmind *
183 1.1 rmind * Packed array: 60 MSB bits, 5 values, 12 bits each.
184 1.1 rmind *
185 1.1 rmind * Reference: "Bit Twiddling Hacks" by S.E. Anderson, Stanford.
186 1.1 rmind * Based on the hasvalue() and haszero() ideas. Since values are
187 1.1 rmind * represented by upper 60 bits, we shift right by 4.
188 1.1 rmind */
189 1.1 rmind static bool
190 1.1 rmind bitmap_word_isset(uint64_t x, unsigned bit)
191 1.1 rmind {
192 1.1 rmind uint64_t m, r;
193 1.1 rmind
194 1.1 rmind bit++;
195 1.1 rmind KASSERT((x & PORTMAP_L1_TAG) == 0);
196 1.1 rmind KASSERT(bit <= (PORTMAP_L0_MASK + 1));
197 1.1 rmind
198 1.1 rmind m = (x >> 4) ^ (UINT64_C(0x1001001001001) * bit);
199 1.1 rmind r = (m - UINT64_C(0x1001001001001)) & (~m & UINT64_C(0x800800800800800));
200 1.1 rmind return r != 0;
201 1.1 rmind }
202 1.1 rmind
203 1.1 rmind /*
204 1.1 rmind * bitmap_word_cax: compare-and-xor on packed array elements.
205 1.1 rmind */
206 1.1 rmind static uint64_t
207 1.1 rmind bitmap_word_cax(uint64_t x, int exp, int bit)
208 1.1 rmind {
209 1.1 rmind unsigned e = exp + 1;
210 1.1 rmind
211 1.1 rmind /*
212 1.1 rmind * We need to distinguish "no value" from zero. Just add one,
213 1.1 rmind * since we use 12 bits to represent 11 bit values.
214 1.1 rmind */
215 1.1 rmind bit++;
216 1.1 rmind KASSERT((unsigned)bit <= (PORTMAP_L0_MASK + 1));
217 1.1 rmind KASSERT((x & PORTMAP_L1_TAG) == 0);
218 1.1 rmind
219 1.1 rmind if (((x >> 52) & 0xfff) == e)
220 1.1 rmind return x ^ ((uint64_t)bit << 52);
221 1.1 rmind if (((x >> 40) & 0xfff) == e)
222 1.1 rmind return x ^ ((uint64_t)bit << 40);
223 1.1 rmind if (((x >> 28) & 0xfff) == e)
224 1.1 rmind return x ^ ((uint64_t)bit << 28);
225 1.1 rmind if (((x >> 16) & 0xfff) == e)
226 1.1 rmind return x ^ ((uint64_t)bit << 16);
227 1.1 rmind if (((x >> 4) & 0xfff) == e)
228 1.1 rmind return x ^ ((uint64_t)bit << 4);
229 1.1 rmind return 0;
230 1.1 rmind }
231 1.1 rmind
232 1.1 rmind static unsigned
233 1.1 rmind bitmap_word_unpack(uint64_t x, unsigned bitvals[static 5])
234 1.1 rmind {
235 1.1 rmind unsigned n = 0;
236 1.1 rmind uint64_t v;
237 1.1 rmind
238 1.1 rmind KASSERT((x & PORTMAP_L1_TAG) == 0);
239 1.1 rmind
240 1.1 rmind if ((v = ((x >> 52)) & 0xfff) != 0)
241 1.1 rmind bitvals[n++] = v - 1;
242 1.1 rmind if ((v = ((x >> 40)) & 0xfff) != 0)
243 1.1 rmind bitvals[n++] = v - 1;
244 1.1 rmind if ((v = ((x >> 28)) & 0xfff) != 0)
245 1.1 rmind bitvals[n++] = v - 1;
246 1.1 rmind if ((v = ((x >> 16)) & 0xfff) != 0)
247 1.1 rmind bitvals[n++] = v - 1;
248 1.1 rmind if ((v = ((x >> 4)) & 0xfff) != 0)
249 1.1 rmind bitvals[n++] = v - 1;
250 1.1 rmind return n;
251 1.1 rmind }
252 1.1 rmind
253 1.1 rmind #if 0
254 1.1 rmind static bool
255 1.1 rmind bitmap_isset(const bitmap_t *bm, unsigned bit)
256 1.1 rmind {
257 1.1 rmind unsigned i, chunk_bit;
258 1.1 rmind uint64_t bval, b;
259 1.1 rmind bitmap_l1_t *bm1;
260 1.1 rmind
261 1.1 rmind KASSERT(bit < PORTMAP_MAX_BITS);
262 1.1 rmind i = bit >> PORTMAP_L0_SHIFT;
263 1.1 rmind bval = bm->bits0[i];
264 1.1 rmind
265 1.1 rmind /*
266 1.1 rmind * Empty check. Note: we can test the whole word against zero,
267 1.1 rmind * since zero bit values in the packed array result in bits set.
268 1.1 rmind */
269 1.1 rmind if (bval == 0)
270 1.1 rmind return false;
271 1.1 rmind
272 1.1 rmind /* Level 0 check. */
273 1.1 rmind chunk_bit = bit & PORTMAP_L0_MASK;
274 1.1 rmind if ((bval & PORTMAP_L1_TAG) == 0)
275 1.1 rmind return bitmap_word_isset(bval, chunk_bit);
276 1.1 rmind
277 1.1 rmind /* Level 1 check. */
278 1.1 rmind bm1 = PORTMAP_L1_GET(bval);
279 1.1 rmind KASSERT(bm1 != NULL);
280 1.1 rmind i = chunk_bit >> PORTMAP_L1_SHIFT;
281 1.1 rmind b = UINT64_C(1) << (chunk_bit & PORTMAP_L1_MASK);
282 1.1 rmind return (bm1->bits1[i] & b) != 0;
283 1.1 rmind }
284 1.1 rmind #endif
285 1.1 rmind
286 1.1 rmind static bool
287 1.1 rmind bitmap_set(bitmap_t *bm, unsigned bit)
288 1.1 rmind {
289 1.1 rmind unsigned i, chunk_bit;
290 1.1 rmind uint64_t bval, b, oval, nval;
291 1.1 rmind bitmap_l1_t *bm1;
292 1.1 rmind again:
293 1.1 rmind KASSERT(bit < PORTMAP_MAX_BITS);
294 1.1 rmind i = bit >> PORTMAP_L0_SHIFT;
295 1.1 rmind chunk_bit = bit & PORTMAP_L0_MASK;
296 1.1 rmind bval = bm->bits0[i]; // atomic fetch
297 1.1 rmind
298 1.1 rmind if ((bval & PORTMAP_L1_TAG) == 0) {
299 1.1 rmind unsigned n = 0, bitvals[5];
300 1.1 rmind uint64_t bm1p;
301 1.1 rmind
302 1.1 rmind if (bitmap_word_isset(bval, chunk_bit)) {
303 1.1 rmind return false;
304 1.1 rmind }
305 1.1 rmind
306 1.1 rmind /*
307 1.1 rmind * Look for a zero-slot and put a value there.
308 1.1 rmind */
309 1.1 rmind if ((nval = bitmap_word_cax(bval, -1, chunk_bit)) != 0) {
310 1.1 rmind KASSERT((nval & PORTMAP_L1_TAG) == 0);
311 1.2 rmind if (__npf_atomic_cas_64(&bm->bits0[i], bval, nval) != bval) {
312 1.1 rmind goto again;
313 1.1 rmind }
314 1.1 rmind return true;
315 1.1 rmind }
316 1.1 rmind
317 1.1 rmind /*
318 1.1 rmind * Full: allocate L1 block and copy over the current
319 1.1 rmind * values into the level.
320 1.1 rmind */
321 1.1 rmind bm1 = kmem_intr_zalloc(sizeof(bitmap_l1_t), KM_NOSLEEP);
322 1.1 rmind if (bm1 == NULL) {
323 1.1 rmind return false; // error
324 1.1 rmind }
325 1.1 rmind n = bitmap_word_unpack(bval, bitvals);
326 1.1 rmind while (n--) {
327 1.1 rmind const unsigned v = bitvals[n];
328 1.1 rmind const unsigned off = v >> PORTMAP_L1_SHIFT;
329 1.1 rmind
330 1.1 rmind KASSERT(v <= PORTMAP_L0_MASK);
331 1.1 rmind KASSERT(off < (sizeof(uint64_t) * CHAR_BIT));
332 1.1 rmind bm1->bits1[off] |= UINT64_C(1) << (v & PORTMAP_L1_MASK);
333 1.1 rmind }
334 1.1 rmind
335 1.1 rmind /*
336 1.1 rmind * Attempt to set the L1 structure. Note: there is no
337 1.1 rmind * ABA problem since the we compare the actual values.
338 1.1 rmind * Note: CAS serves as a memory barrier.
339 1.1 rmind */
340 1.1 rmind bm1p = (uintptr_t)bm1;
341 1.1 rmind KASSERT((bm1p & PORTMAP_L1_TAG) == 0);
342 1.1 rmind bm1p |= PORTMAP_L1_TAG;
343 1.2 rmind if (__npf_atomic_cas_64(&bm->bits0[i], bval, bm1p) != bval) {
344 1.1 rmind kmem_intr_free(bm1, sizeof(bitmap_l1_t));
345 1.1 rmind goto again;
346 1.1 rmind }
347 1.1 rmind bval = bm1p;
348 1.1 rmind }
349 1.1 rmind
350 1.1 rmind bm1 = PORTMAP_L1_GET(bval);
351 1.1 rmind KASSERT(bm1 != NULL);
352 1.1 rmind i = chunk_bit >> PORTMAP_L1_SHIFT;
353 1.1 rmind b = UINT64_C(1) << (chunk_bit & PORTMAP_L1_MASK);
354 1.1 rmind
355 1.1 rmind oval = bm1->bits1[i]; // atomic fetch
356 1.1 rmind if (oval & b) {
357 1.1 rmind return false;
358 1.1 rmind }
359 1.1 rmind nval = oval | b;
360 1.2 rmind if (__npf_atomic_cas_64(&bm1->bits1[i], oval, nval) != oval) {
361 1.1 rmind goto again;
362 1.1 rmind }
363 1.1 rmind return true;
364 1.1 rmind }
365 1.1 rmind
366 1.1 rmind static bool
367 1.1 rmind bitmap_clr(bitmap_t *bm, unsigned bit)
368 1.1 rmind {
369 1.1 rmind unsigned i, chunk_bit;
370 1.1 rmind uint64_t bval, b, oval, nval;
371 1.1 rmind bitmap_l1_t *bm1;
372 1.1 rmind again:
373 1.1 rmind KASSERT(bit < PORTMAP_MAX_BITS);
374 1.1 rmind i = bit >> PORTMAP_L0_SHIFT;
375 1.1 rmind chunk_bit = bit & PORTMAP_L0_MASK;
376 1.1 rmind bval = bm->bits0[i];
377 1.1 rmind
378 1.1 rmind if ((bval & PORTMAP_L1_TAG) == 0) {
379 1.1 rmind if (!bitmap_word_isset(bval, chunk_bit)) {
380 1.1 rmind return false;
381 1.1 rmind }
382 1.1 rmind nval = bitmap_word_cax(bval, chunk_bit, chunk_bit);
383 1.1 rmind KASSERT((nval & PORTMAP_L1_TAG) == 0);
384 1.2 rmind if (__npf_atomic_cas_64(&bm->bits0[i], bval, nval) != bval) {
385 1.1 rmind goto again;
386 1.1 rmind }
387 1.1 rmind return true;
388 1.1 rmind }
389 1.1 rmind
390 1.1 rmind bm1 = PORTMAP_L1_GET(bval);
391 1.1 rmind KASSERT(bm1 != NULL);
392 1.1 rmind i = chunk_bit >> PORTMAP_L1_SHIFT;
393 1.1 rmind b = UINT64_C(1) << (chunk_bit & PORTMAP_L1_MASK);
394 1.1 rmind
395 1.1 rmind oval = bm1->bits1[i]; // atomic fetch
396 1.1 rmind if ((oval & b) == 0) {
397 1.1 rmind return false;
398 1.1 rmind }
399 1.1 rmind nval = oval & ~b;
400 1.2 rmind if (__npf_atomic_cas_64(&bm1->bits1[i], oval, nval) != oval) {
401 1.1 rmind goto again;
402 1.1 rmind }
403 1.1 rmind return true;
404 1.1 rmind }
405 1.1 rmind
406 1.1 rmind /////////////////////////////////////////////////////////////////////////
407 1.1 rmind
408 1.1 rmind static bitmap_t *
409 1.1 rmind npf_portmap_autoget(npf_t *npf, unsigned alen, const npf_addr_t *addr)
410 1.1 rmind {
411 1.1 rmind npf_portmap_t *pm = npf->portmap;
412 1.1 rmind bitmap_t *bm;
413 1.1 rmind
414 1.1 rmind KASSERT(pm && pm->addr_map);
415 1.1 rmind KASSERT(alen && alen <= sizeof(npf_addr_t));
416 1.1 rmind
417 1.1 rmind /* Lookup the port map for this address. */
418 1.1 rmind bm = thmap_get(pm->addr_map, addr, alen);
419 1.1 rmind if (bm == NULL) {
420 1.1 rmind void *ret;
421 1.1 rmind
422 1.1 rmind /*
423 1.1 rmind * Allocate a new port map for this address and
424 1.1 rmind * attempt to insert it.
425 1.1 rmind */
426 1.1 rmind bm = kmem_intr_zalloc(sizeof(bitmap_t), KM_NOSLEEP);
427 1.1 rmind if (bm == NULL) {
428 1.1 rmind return NULL;
429 1.1 rmind }
430 1.1 rmind memcpy(&bm->addr, addr, alen);
431 1.1 rmind bm->addr_len = alen;
432 1.1 rmind
433 1.1 rmind int s = splsoftnet();
434 1.1 rmind ret = thmap_put(pm->addr_map, &bm->addr, alen, bm);
435 1.1 rmind splx(s);
436 1.1 rmind
437 1.1 rmind if (ret == bm) {
438 1.1 rmind /* Success: insert the bitmap into the list. */
439 1.1 rmind mutex_enter(&pm->list_lock);
440 1.1 rmind LIST_INSERT_HEAD(&pm->bitmap_list, bm, entry);
441 1.1 rmind mutex_exit(&pm->list_lock);
442 1.1 rmind } else {
443 1.1 rmind /* Race: use an existing bitmap. */
444 1.1 rmind kmem_free(bm, sizeof(bitmap_t));
445 1.1 rmind bm = ret;
446 1.1 rmind }
447 1.1 rmind }
448 1.1 rmind return bm;
449 1.1 rmind }
450 1.1 rmind
451 1.1 rmind
452 1.1 rmind /*
453 1.1 rmind * npf_portmap_flush: free all bitmaps and remove all addresses.
454 1.1 rmind *
455 1.1 rmind * => Concurrent calls to this routine are not allowed; therefore no
456 1.1 rmind * need to acquire locks.
457 1.1 rmind */
458 1.1 rmind void
459 1.1 rmind npf_portmap_flush(npf_t *npf)
460 1.1 rmind {
461 1.1 rmind npf_portmap_t *pm = npf->portmap;
462 1.1 rmind bitmap_t *bm;
463 1.1 rmind
464 1.1 rmind KASSERT(npf_config_locked_p(npf));
465 1.1 rmind
466 1.1 rmind while ((bm = LIST_FIRST(&pm->bitmap_list)) != NULL) {
467 1.1 rmind for (unsigned i = 0; i < PORTMAP_L0_WORDS; i++) {
468 1.1 rmind uintptr_t bm1 = bm->bits0[i];
469 1.1 rmind
470 1.1 rmind if (bm1 & PORTMAP_L1_TAG) {
471 1.1 rmind bitmap_l1_t *bm1p = PORTMAP_L1_GET(bm1);
472 1.1 rmind kmem_intr_free(bm1p, sizeof(bitmap_l1_t));
473 1.1 rmind }
474 1.1 rmind bm->bits0[i] = UINT64_C(0);
475 1.1 rmind }
476 1.1 rmind LIST_REMOVE(bm, entry);
477 1.1 rmind thmap_del(pm->addr_map, &bm->addr, bm->addr_len);
478 1.1 rmind kmem_intr_free(bm, sizeof(bitmap_t));
479 1.1 rmind }
480 1.1 rmind /* Note: the caller ensures there are no active references. */
481 1.1 rmind thmap_gc(pm->addr_map, thmap_stage_gc(pm->addr_map));
482 1.1 rmind }
483 1.1 rmind
484 1.1 rmind /*
485 1.1 rmind * npf_portmap_get: allocate and return a port from the given portmap.
486 1.1 rmind *
487 1.1 rmind * => Returns the port value in network byte-order.
488 1.1 rmind * => Zero indicates a failure.
489 1.1 rmind */
490 1.1 rmind in_port_t
491 1.1 rmind npf_portmap_get(npf_t *npf, int alen, const npf_addr_t *addr)
492 1.1 rmind {
493 1.1 rmind const npf_portmap_params_t *params = npf->params[NPF_PARAMS_PORTMAP];
494 1.1 rmind const unsigned port_delta = params->max_port - params->min_port;
495 1.1 rmind unsigned bit, target;
496 1.1 rmind bitmap_t *bm;
497 1.1 rmind
498 1.1 rmind bm = npf_portmap_autoget(npf, alen, addr);
499 1.1 rmind if (bm == NULL) {
500 1.1 rmind /* No memory. */
501 1.1 rmind return 0;
502 1.1 rmind }
503 1.1 rmind
504 1.1 rmind /* Randomly select a port. */
505 1.1 rmind target = params->min_port + (cprng_fast32() % port_delta);
506 1.1 rmind bit = target;
507 1.1 rmind next:
508 1.1 rmind if (bitmap_set(bm, bit)) {
509 1.1 rmind /* Success. */
510 1.1 rmind return htons(bit);
511 1.1 rmind }
512 1.1 rmind bit = params->min_port + ((bit + 1) % port_delta);
513 1.1 rmind if (target != bit) {
514 1.1 rmind /* Next.. */
515 1.1 rmind goto next;
516 1.1 rmind }
517 1.1 rmind /* No space. */
518 1.1 rmind return 0;
519 1.1 rmind }
520 1.1 rmind
521 1.1 rmind /*
522 1.1 rmind * npf_portmap_take: allocate a specific port in the portmap.
523 1.1 rmind */
524 1.1 rmind bool
525 1.1 rmind npf_portmap_take(npf_t *npf, int alen, const npf_addr_t *addr, in_port_t port)
526 1.1 rmind {
527 1.1 rmind const npf_portmap_params_t *params = npf->params[NPF_PARAMS_PORTMAP];
528 1.1 rmind bitmap_t *bm = npf_portmap_autoget(npf, alen, addr);
529 1.1 rmind
530 1.1 rmind port = ntohs(port);
531 1.1 rmind if (!bm || port < params->min_port || port > params->max_port) {
532 1.1 rmind /* Out of memory / invalid port. */
533 1.1 rmind return false;
534 1.1 rmind }
535 1.1 rmind return bitmap_set(bm, port);
536 1.1 rmind }
537 1.1 rmind
538 1.1 rmind /*
539 1.1 rmind * npf_portmap_put: release the port, making it available in the portmap.
540 1.1 rmind *
541 1.1 rmind * => The port value should be in network byte-order.
542 1.1 rmind */
543 1.1 rmind void
544 1.1 rmind npf_portmap_put(npf_t *npf, int alen, const npf_addr_t *addr, in_port_t port)
545 1.1 rmind {
546 1.1 rmind bitmap_t *bm;
547 1.1 rmind
548 1.1 rmind bm = npf_portmap_autoget(npf, alen, addr);
549 1.1 rmind if (bm) {
550 1.1 rmind port = ntohs(port);
551 1.1 rmind bitmap_clr(bm, port);
552 1.1 rmind }
553 1.1 rmind }
554