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