npf_portmap.c revision 1.4 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.4 rmind __KERNEL_RCSID(0, "$NetBSD: npf_portmap.c,v 1.4 2019/08/11 20:26:34 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.4 rmind #define NPF_PORTMAP_MINPORT 1024
99 1.4 rmind #define NPF_PORTMAP_MAXPORT 65535
100 1.4 rmind
101 1.1 rmind struct npf_portmap {
102 1.1 rmind thmap_t * addr_map;
103 1.1 rmind LIST_HEAD(, bitmap) bitmap_list;
104 1.1 rmind kmutex_t list_lock;
105 1.4 rmind int min_port;
106 1.4 rmind int max_port;
107 1.1 rmind };
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.4 rmind npf_portmap_t *pm = npf_portmap_create(
115 1.4 rmind NPF_PORTMAP_MINPORT, NPF_PORTMAP_MAXPORT);
116 1.1 rmind npf_param_t param_map[] = {
117 1.1 rmind {
118 1.1 rmind "portmap.min_port",
119 1.4 rmind &pm->min_port,
120 1.4 rmind .default_val = NPF_PORTMAP_MINPORT,
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.4 rmind &pm->max_port,
126 1.4 rmind .default_val = NPF_PORTMAP_MAXPORT,
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.2 rmind mutex_init(&portmap_lock, MUTEX_DEFAULT, IPL_SOFTNET);
132 1.4 rmind npf->portmap = pm;
133 1.1 rmind }
134 1.1 rmind
135 1.1 rmind void
136 1.1 rmind npf_portmap_fini(npf_t *npf)
137 1.1 rmind {
138 1.4 rmind npf_portmap_destroy(npf->portmap);
139 1.4 rmind mutex_destroy(&portmap_lock);
140 1.4 rmind npf->portmap = NULL; // diagnostic
141 1.4 rmind }
142 1.4 rmind
143 1.4 rmind npf_portmap_t *
144 1.4 rmind npf_portmap_create(int min_port, int max_port)
145 1.4 rmind {
146 1.4 rmind npf_portmap_t *pm;
147 1.1 rmind
148 1.4 rmind pm = kmem_zalloc(sizeof(npf_portmap_t), KM_SLEEP);
149 1.4 rmind mutex_init(&pm->list_lock, MUTEX_DEFAULT, IPL_SOFTNET);
150 1.4 rmind pm->addr_map = thmap_create(0, NULL, THMAP_NOCOPY);
151 1.4 rmind pm->min_port = min_port;
152 1.4 rmind pm->max_port = max_port;
153 1.4 rmind return pm;
154 1.4 rmind }
155 1.1 rmind
156 1.4 rmind void
157 1.4 rmind npf_portmap_destroy(npf_portmap_t *pm)
158 1.4 rmind {
159 1.4 rmind npf_portmap_flush(pm);
160 1.1 rmind KASSERT(LIST_EMPTY(&pm->bitmap_list));
161 1.1 rmind
162 1.1 rmind thmap_destroy(pm->addr_map);
163 1.1 rmind mutex_destroy(&pm->list_lock);
164 1.1 rmind kmem_free(pm, sizeof(npf_portmap_t));
165 1.1 rmind }
166 1.1 rmind
167 1.1 rmind /////////////////////////////////////////////////////////////////////////
168 1.1 rmind
169 1.2 rmind #if defined(_LP64)
170 1.2 rmind #define __npf_atomic_cas_64 atomic_cas_64
171 1.2 rmind #else
172 1.2 rmind static uint64_t
173 1.2 rmind __npf_atomic_cas_64(volatile uint64_t *ptr, uint64_t old, uint64_t new)
174 1.2 rmind {
175 1.2 rmind uint64_t prev;
176 1.2 rmind
177 1.2 rmind mutex_enter(&portmap_lock);
178 1.2 rmind prev = *ptr;
179 1.2 rmind if (prev == old) {
180 1.2 rmind *ptr = new;
181 1.2 rmind }
182 1.2 rmind mutex_exit(&portmap_lock);
183 1.2 rmind
184 1.2 rmind return prev;
185 1.2 rmind }
186 1.2 rmind #endif
187 1.2 rmind
188 1.1 rmind /*
189 1.1 rmind * bitmap_word_isset: test whether the bit value is in the packed array.
190 1.1 rmind *
191 1.1 rmind * => Return true if any value equals the bit number value.
192 1.1 rmind *
193 1.1 rmind * Packed array: 60 MSB bits, 5 values, 12 bits each.
194 1.1 rmind *
195 1.1 rmind * Reference: "Bit Twiddling Hacks" by S.E. Anderson, Stanford.
196 1.1 rmind * Based on the hasvalue() and haszero() ideas. Since values are
197 1.1 rmind * represented by upper 60 bits, we shift right by 4.
198 1.1 rmind */
199 1.1 rmind static bool
200 1.1 rmind bitmap_word_isset(uint64_t x, unsigned bit)
201 1.1 rmind {
202 1.1 rmind uint64_t m, r;
203 1.1 rmind
204 1.1 rmind bit++;
205 1.1 rmind KASSERT((x & PORTMAP_L1_TAG) == 0);
206 1.1 rmind KASSERT(bit <= (PORTMAP_L0_MASK + 1));
207 1.1 rmind
208 1.1 rmind m = (x >> 4) ^ (UINT64_C(0x1001001001001) * bit);
209 1.1 rmind r = (m - UINT64_C(0x1001001001001)) & (~m & UINT64_C(0x800800800800800));
210 1.1 rmind return r != 0;
211 1.1 rmind }
212 1.1 rmind
213 1.1 rmind /*
214 1.1 rmind * bitmap_word_cax: compare-and-xor on packed array elements.
215 1.1 rmind */
216 1.1 rmind static uint64_t
217 1.1 rmind bitmap_word_cax(uint64_t x, int exp, int bit)
218 1.1 rmind {
219 1.1 rmind unsigned e = exp + 1;
220 1.1 rmind
221 1.1 rmind /*
222 1.1 rmind * We need to distinguish "no value" from zero. Just add one,
223 1.1 rmind * since we use 12 bits to represent 11 bit values.
224 1.1 rmind */
225 1.1 rmind bit++;
226 1.1 rmind KASSERT((unsigned)bit <= (PORTMAP_L0_MASK + 1));
227 1.1 rmind KASSERT((x & PORTMAP_L1_TAG) == 0);
228 1.1 rmind
229 1.1 rmind if (((x >> 52) & 0xfff) == e)
230 1.1 rmind return x ^ ((uint64_t)bit << 52);
231 1.1 rmind if (((x >> 40) & 0xfff) == e)
232 1.1 rmind return x ^ ((uint64_t)bit << 40);
233 1.1 rmind if (((x >> 28) & 0xfff) == e)
234 1.1 rmind return x ^ ((uint64_t)bit << 28);
235 1.1 rmind if (((x >> 16) & 0xfff) == e)
236 1.1 rmind return x ^ ((uint64_t)bit << 16);
237 1.1 rmind if (((x >> 4) & 0xfff) == e)
238 1.1 rmind return x ^ ((uint64_t)bit << 4);
239 1.1 rmind return 0;
240 1.1 rmind }
241 1.1 rmind
242 1.1 rmind static unsigned
243 1.1 rmind bitmap_word_unpack(uint64_t x, unsigned bitvals[static 5])
244 1.1 rmind {
245 1.1 rmind unsigned n = 0;
246 1.1 rmind uint64_t v;
247 1.1 rmind
248 1.1 rmind KASSERT((x & PORTMAP_L1_TAG) == 0);
249 1.1 rmind
250 1.1 rmind if ((v = ((x >> 52)) & 0xfff) != 0)
251 1.1 rmind bitvals[n++] = v - 1;
252 1.1 rmind if ((v = ((x >> 40)) & 0xfff) != 0)
253 1.1 rmind bitvals[n++] = v - 1;
254 1.1 rmind if ((v = ((x >> 28)) & 0xfff) != 0)
255 1.1 rmind bitvals[n++] = v - 1;
256 1.1 rmind if ((v = ((x >> 16)) & 0xfff) != 0)
257 1.1 rmind bitvals[n++] = v - 1;
258 1.1 rmind if ((v = ((x >> 4)) & 0xfff) != 0)
259 1.1 rmind bitvals[n++] = v - 1;
260 1.1 rmind return n;
261 1.1 rmind }
262 1.1 rmind
263 1.1 rmind #if 0
264 1.1 rmind static bool
265 1.1 rmind bitmap_isset(const bitmap_t *bm, unsigned bit)
266 1.1 rmind {
267 1.1 rmind unsigned i, chunk_bit;
268 1.1 rmind uint64_t bval, b;
269 1.1 rmind bitmap_l1_t *bm1;
270 1.1 rmind
271 1.1 rmind KASSERT(bit < PORTMAP_MAX_BITS);
272 1.1 rmind i = bit >> PORTMAP_L0_SHIFT;
273 1.1 rmind bval = bm->bits0[i];
274 1.1 rmind
275 1.1 rmind /*
276 1.1 rmind * Empty check. Note: we can test the whole word against zero,
277 1.1 rmind * since zero bit values in the packed array result in bits set.
278 1.1 rmind */
279 1.1 rmind if (bval == 0)
280 1.1 rmind return false;
281 1.1 rmind
282 1.1 rmind /* Level 0 check. */
283 1.1 rmind chunk_bit = bit & PORTMAP_L0_MASK;
284 1.1 rmind if ((bval & PORTMAP_L1_TAG) == 0)
285 1.1 rmind return bitmap_word_isset(bval, chunk_bit);
286 1.1 rmind
287 1.1 rmind /* Level 1 check. */
288 1.1 rmind bm1 = PORTMAP_L1_GET(bval);
289 1.1 rmind KASSERT(bm1 != NULL);
290 1.1 rmind i = chunk_bit >> PORTMAP_L1_SHIFT;
291 1.1 rmind b = UINT64_C(1) << (chunk_bit & PORTMAP_L1_MASK);
292 1.1 rmind return (bm1->bits1[i] & b) != 0;
293 1.1 rmind }
294 1.1 rmind #endif
295 1.1 rmind
296 1.1 rmind static bool
297 1.1 rmind bitmap_set(bitmap_t *bm, unsigned bit)
298 1.1 rmind {
299 1.1 rmind unsigned i, chunk_bit;
300 1.1 rmind uint64_t bval, b, oval, nval;
301 1.1 rmind bitmap_l1_t *bm1;
302 1.1 rmind again:
303 1.1 rmind KASSERT(bit < PORTMAP_MAX_BITS);
304 1.1 rmind i = bit >> PORTMAP_L0_SHIFT;
305 1.1 rmind chunk_bit = bit & PORTMAP_L0_MASK;
306 1.1 rmind bval = bm->bits0[i]; // atomic fetch
307 1.1 rmind
308 1.1 rmind if ((bval & PORTMAP_L1_TAG) == 0) {
309 1.1 rmind unsigned n = 0, bitvals[5];
310 1.1 rmind uint64_t bm1p;
311 1.1 rmind
312 1.1 rmind if (bitmap_word_isset(bval, chunk_bit)) {
313 1.1 rmind return false;
314 1.1 rmind }
315 1.1 rmind
316 1.1 rmind /*
317 1.1 rmind * Look for a zero-slot and put a value there.
318 1.1 rmind */
319 1.1 rmind if ((nval = bitmap_word_cax(bval, -1, chunk_bit)) != 0) {
320 1.1 rmind KASSERT((nval & PORTMAP_L1_TAG) == 0);
321 1.2 rmind if (__npf_atomic_cas_64(&bm->bits0[i], bval, nval) != bval) {
322 1.1 rmind goto again;
323 1.1 rmind }
324 1.1 rmind return true;
325 1.1 rmind }
326 1.1 rmind
327 1.1 rmind /*
328 1.1 rmind * Full: allocate L1 block and copy over the current
329 1.1 rmind * values into the level.
330 1.1 rmind */
331 1.1 rmind bm1 = kmem_intr_zalloc(sizeof(bitmap_l1_t), KM_NOSLEEP);
332 1.1 rmind if (bm1 == NULL) {
333 1.1 rmind return false; // error
334 1.1 rmind }
335 1.1 rmind n = bitmap_word_unpack(bval, bitvals);
336 1.1 rmind while (n--) {
337 1.1 rmind const unsigned v = bitvals[n];
338 1.1 rmind const unsigned off = v >> PORTMAP_L1_SHIFT;
339 1.1 rmind
340 1.1 rmind KASSERT(v <= PORTMAP_L0_MASK);
341 1.1 rmind KASSERT(off < (sizeof(uint64_t) * CHAR_BIT));
342 1.1 rmind bm1->bits1[off] |= UINT64_C(1) << (v & PORTMAP_L1_MASK);
343 1.1 rmind }
344 1.1 rmind
345 1.1 rmind /*
346 1.1 rmind * Attempt to set the L1 structure. Note: there is no
347 1.1 rmind * ABA problem since the we compare the actual values.
348 1.1 rmind * Note: CAS serves as a memory barrier.
349 1.1 rmind */
350 1.1 rmind bm1p = (uintptr_t)bm1;
351 1.1 rmind KASSERT((bm1p & PORTMAP_L1_TAG) == 0);
352 1.1 rmind bm1p |= PORTMAP_L1_TAG;
353 1.2 rmind if (__npf_atomic_cas_64(&bm->bits0[i], bval, bm1p) != bval) {
354 1.1 rmind kmem_intr_free(bm1, sizeof(bitmap_l1_t));
355 1.1 rmind goto again;
356 1.1 rmind }
357 1.1 rmind bval = bm1p;
358 1.1 rmind }
359 1.1 rmind
360 1.1 rmind bm1 = PORTMAP_L1_GET(bval);
361 1.1 rmind KASSERT(bm1 != NULL);
362 1.1 rmind i = chunk_bit >> PORTMAP_L1_SHIFT;
363 1.1 rmind b = UINT64_C(1) << (chunk_bit & PORTMAP_L1_MASK);
364 1.1 rmind
365 1.1 rmind oval = bm1->bits1[i]; // atomic fetch
366 1.1 rmind if (oval & b) {
367 1.1 rmind return false;
368 1.1 rmind }
369 1.1 rmind nval = oval | b;
370 1.2 rmind if (__npf_atomic_cas_64(&bm1->bits1[i], oval, nval) != oval) {
371 1.1 rmind goto again;
372 1.1 rmind }
373 1.1 rmind return true;
374 1.1 rmind }
375 1.1 rmind
376 1.1 rmind static bool
377 1.1 rmind bitmap_clr(bitmap_t *bm, unsigned bit)
378 1.1 rmind {
379 1.1 rmind unsigned i, chunk_bit;
380 1.1 rmind uint64_t bval, b, oval, nval;
381 1.1 rmind bitmap_l1_t *bm1;
382 1.1 rmind again:
383 1.1 rmind KASSERT(bit < PORTMAP_MAX_BITS);
384 1.1 rmind i = bit >> PORTMAP_L0_SHIFT;
385 1.1 rmind chunk_bit = bit & PORTMAP_L0_MASK;
386 1.1 rmind bval = bm->bits0[i];
387 1.1 rmind
388 1.1 rmind if ((bval & PORTMAP_L1_TAG) == 0) {
389 1.1 rmind if (!bitmap_word_isset(bval, chunk_bit)) {
390 1.1 rmind return false;
391 1.1 rmind }
392 1.1 rmind nval = bitmap_word_cax(bval, chunk_bit, chunk_bit);
393 1.1 rmind KASSERT((nval & PORTMAP_L1_TAG) == 0);
394 1.2 rmind if (__npf_atomic_cas_64(&bm->bits0[i], bval, nval) != bval) {
395 1.1 rmind goto again;
396 1.1 rmind }
397 1.1 rmind return true;
398 1.1 rmind }
399 1.1 rmind
400 1.1 rmind bm1 = PORTMAP_L1_GET(bval);
401 1.1 rmind KASSERT(bm1 != NULL);
402 1.1 rmind i = chunk_bit >> PORTMAP_L1_SHIFT;
403 1.1 rmind b = UINT64_C(1) << (chunk_bit & PORTMAP_L1_MASK);
404 1.1 rmind
405 1.1 rmind oval = bm1->bits1[i]; // atomic fetch
406 1.1 rmind if ((oval & b) == 0) {
407 1.1 rmind return false;
408 1.1 rmind }
409 1.1 rmind nval = oval & ~b;
410 1.2 rmind if (__npf_atomic_cas_64(&bm1->bits1[i], oval, nval) != oval) {
411 1.1 rmind goto again;
412 1.1 rmind }
413 1.1 rmind return true;
414 1.1 rmind }
415 1.1 rmind
416 1.1 rmind /////////////////////////////////////////////////////////////////////////
417 1.1 rmind
418 1.1 rmind static bitmap_t *
419 1.4 rmind npf_portmap_autoget(npf_portmap_t *pm, unsigned alen, const npf_addr_t *addr)
420 1.1 rmind {
421 1.1 rmind bitmap_t *bm;
422 1.1 rmind
423 1.1 rmind KASSERT(pm && pm->addr_map);
424 1.1 rmind KASSERT(alen && alen <= sizeof(npf_addr_t));
425 1.1 rmind
426 1.1 rmind /* Lookup the port map for this address. */
427 1.1 rmind bm = thmap_get(pm->addr_map, addr, alen);
428 1.1 rmind if (bm == NULL) {
429 1.1 rmind void *ret;
430 1.1 rmind
431 1.1 rmind /*
432 1.1 rmind * Allocate a new port map for this address and
433 1.1 rmind * attempt to insert it.
434 1.1 rmind */
435 1.1 rmind bm = kmem_intr_zalloc(sizeof(bitmap_t), KM_NOSLEEP);
436 1.1 rmind if (bm == NULL) {
437 1.1 rmind return NULL;
438 1.1 rmind }
439 1.1 rmind memcpy(&bm->addr, addr, alen);
440 1.1 rmind bm->addr_len = alen;
441 1.1 rmind
442 1.1 rmind int s = splsoftnet();
443 1.1 rmind ret = thmap_put(pm->addr_map, &bm->addr, alen, bm);
444 1.1 rmind splx(s);
445 1.1 rmind
446 1.1 rmind if (ret == bm) {
447 1.1 rmind /* Success: insert the bitmap into the list. */
448 1.1 rmind mutex_enter(&pm->list_lock);
449 1.1 rmind LIST_INSERT_HEAD(&pm->bitmap_list, bm, entry);
450 1.1 rmind mutex_exit(&pm->list_lock);
451 1.1 rmind } else {
452 1.1 rmind /* Race: use an existing bitmap. */
453 1.1 rmind kmem_free(bm, sizeof(bitmap_t));
454 1.1 rmind bm = ret;
455 1.1 rmind }
456 1.1 rmind }
457 1.1 rmind return bm;
458 1.1 rmind }
459 1.1 rmind
460 1.1 rmind
461 1.1 rmind /*
462 1.1 rmind * npf_portmap_flush: free all bitmaps and remove all addresses.
463 1.1 rmind *
464 1.1 rmind * => Concurrent calls to this routine are not allowed; therefore no
465 1.1 rmind * need to acquire locks.
466 1.1 rmind */
467 1.1 rmind void
468 1.4 rmind npf_portmap_flush(npf_portmap_t *pm)
469 1.1 rmind {
470 1.1 rmind bitmap_t *bm;
471 1.1 rmind
472 1.1 rmind while ((bm = LIST_FIRST(&pm->bitmap_list)) != NULL) {
473 1.1 rmind for (unsigned i = 0; i < PORTMAP_L0_WORDS; i++) {
474 1.1 rmind uintptr_t bm1 = bm->bits0[i];
475 1.1 rmind
476 1.1 rmind if (bm1 & PORTMAP_L1_TAG) {
477 1.1 rmind bitmap_l1_t *bm1p = PORTMAP_L1_GET(bm1);
478 1.1 rmind kmem_intr_free(bm1p, sizeof(bitmap_l1_t));
479 1.1 rmind }
480 1.1 rmind bm->bits0[i] = UINT64_C(0);
481 1.1 rmind }
482 1.1 rmind LIST_REMOVE(bm, entry);
483 1.1 rmind thmap_del(pm->addr_map, &bm->addr, bm->addr_len);
484 1.1 rmind kmem_intr_free(bm, sizeof(bitmap_t));
485 1.1 rmind }
486 1.1 rmind /* Note: the caller ensures there are no active references. */
487 1.1 rmind thmap_gc(pm->addr_map, thmap_stage_gc(pm->addr_map));
488 1.1 rmind }
489 1.1 rmind
490 1.1 rmind /*
491 1.1 rmind * npf_portmap_get: allocate and return a port from the given portmap.
492 1.1 rmind *
493 1.1 rmind * => Returns the port value in network byte-order.
494 1.1 rmind * => Zero indicates a failure.
495 1.1 rmind */
496 1.1 rmind in_port_t
497 1.4 rmind npf_portmap_get(npf_portmap_t *pm, int alen, const npf_addr_t *addr)
498 1.1 rmind {
499 1.4 rmind const unsigned port_delta = pm->max_port - pm->min_port;
500 1.1 rmind unsigned bit, target;
501 1.1 rmind bitmap_t *bm;
502 1.1 rmind
503 1.4 rmind bm = npf_portmap_autoget(pm, alen, addr);
504 1.1 rmind if (bm == NULL) {
505 1.1 rmind /* No memory. */
506 1.1 rmind return 0;
507 1.1 rmind }
508 1.1 rmind
509 1.1 rmind /* Randomly select a port. */
510 1.4 rmind target = pm->min_port + (cprng_fast32() % port_delta);
511 1.1 rmind bit = target;
512 1.1 rmind next:
513 1.1 rmind if (bitmap_set(bm, bit)) {
514 1.1 rmind /* Success. */
515 1.1 rmind return htons(bit);
516 1.1 rmind }
517 1.4 rmind bit = pm->min_port + ((bit + 1) % port_delta);
518 1.1 rmind if (target != bit) {
519 1.1 rmind /* Next.. */
520 1.1 rmind goto next;
521 1.1 rmind }
522 1.1 rmind /* No space. */
523 1.1 rmind return 0;
524 1.1 rmind }
525 1.1 rmind
526 1.1 rmind /*
527 1.1 rmind * npf_portmap_take: allocate a specific port in the portmap.
528 1.1 rmind */
529 1.1 rmind bool
530 1.4 rmind npf_portmap_take(npf_portmap_t *pm, int alen,
531 1.4 rmind const npf_addr_t *addr, in_port_t port)
532 1.1 rmind {
533 1.4 rmind bitmap_t *bm = npf_portmap_autoget(pm, alen, addr);
534 1.1 rmind
535 1.1 rmind port = ntohs(port);
536 1.4 rmind if (!bm || port < pm->min_port || port > pm->max_port) {
537 1.1 rmind /* Out of memory / invalid port. */
538 1.1 rmind return false;
539 1.1 rmind }
540 1.1 rmind return bitmap_set(bm, port);
541 1.1 rmind }
542 1.1 rmind
543 1.1 rmind /*
544 1.1 rmind * npf_portmap_put: release the port, making it available in the portmap.
545 1.1 rmind *
546 1.1 rmind * => The port value should be in network byte-order.
547 1.1 rmind */
548 1.1 rmind void
549 1.4 rmind npf_portmap_put(npf_portmap_t *pm, int alen,
550 1.4 rmind const npf_addr_t *addr, in_port_t port)
551 1.1 rmind {
552 1.1 rmind bitmap_t *bm;
553 1.1 rmind
554 1.4 rmind bm = npf_portmap_autoget(pm, alen, addr);
555 1.1 rmind if (bm) {
556 1.1 rmind port = ntohs(port);
557 1.1 rmind bitmap_clr(bm, port);
558 1.1 rmind }
559 1.1 rmind }
560