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