Home | History | Annotate | Line # | Download | only in npf
npf_nat.c revision 1.35
      1  1.35   rmind /*	$NetBSD: npf_nat.c,v 1.35 2014/11/26 21:25:35 rmind Exp $	*/
      2   1.1   rmind 
      3   1.1   rmind /*-
      4  1.25   rmind  * Copyright (c) 2014 Mindaugas Rasiukevicius <rmind at netbsd org>
      5  1.19   rmind  * Copyright (c) 2010-2013 The NetBSD Foundation, Inc.
      6   1.1   rmind  * All rights reserved.
      7   1.1   rmind  *
      8   1.1   rmind  * This material is based upon work partially supported by The
      9   1.1   rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
     10   1.1   rmind  *
     11   1.1   rmind  * Redistribution and use in source and binary forms, with or without
     12   1.1   rmind  * modification, are permitted provided that the following conditions
     13   1.1   rmind  * are met:
     14   1.1   rmind  * 1. Redistributions of source code must retain the above copyright
     15   1.1   rmind  *    notice, this list of conditions and the following disclaimer.
     16   1.1   rmind  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1   rmind  *    notice, this list of conditions and the following disclaimer in the
     18   1.1   rmind  *    documentation and/or other materials provided with the distribution.
     19   1.1   rmind  *
     20   1.1   rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1   rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1   rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1   rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1   rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1   rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1   rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1   rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1   rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1   rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1   rmind  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1   rmind  */
     32   1.1   rmind 
     33   1.1   rmind /*
     34  1.19   rmind  * NPF network address port translation (NAPT) and other forms of NAT.
     35  1.19   rmind  * Described in RFC 2663, RFC 3022, etc.
     36   1.1   rmind  *
     37   1.1   rmind  * Overview
     38   1.1   rmind  *
     39   1.1   rmind  *	There are few mechanisms: NAT policy, port map and translation.
     40   1.1   rmind  *	NAT module has a separate ruleset, where rules contain associated
     41   1.1   rmind  *	NAT policy, thus flexible filter criteria can be used.
     42   1.1   rmind  *
     43   1.2   rmind  * Translation types
     44   1.2   rmind  *
     45   1.2   rmind  *	There are two types of translation: outbound (NPF_NATOUT) and
     46   1.2   rmind  *	inbound (NPF_NATIN).  It should not be confused with connection
     47  1.23   rmind  *	direction.  See npf_nat_which() for the description of how the
     48  1.23   rmind  *	addresses are rewritten.
     49   1.2   rmind  *
     50   1.2   rmind  *	It should be noted that bi-directional NAT is a combined outbound
     51   1.2   rmind  *	and inbound translation, therefore constructed as two policies.
     52   1.2   rmind  *
     53   1.1   rmind  * NAT policies and port maps
     54   1.1   rmind  *
     55   1.2   rmind  *	NAT (translation) policy is applied when a packet matches the rule.
     56   1.2   rmind  *	Apart from filter criteria, NAT policy has a translation IP address
     57   1.1   rmind  *	and associated port map.  Port map is a bitmap used to reserve and
     58   1.1   rmind  *	use unique TCP/UDP ports for translation.  Port maps are unique to
     59   1.1   rmind  *	the IP addresses, therefore multiple NAT policies with the same IP
     60   1.1   rmind  *	will share the same port map.
     61   1.1   rmind  *
     62  1.29   rmind  * Connections, translation entries and their life-cycle
     63   1.1   rmind  *
     64  1.29   rmind  *	NAT module relies on connection tracking module.  Each translated
     65  1.29   rmind  *	connection has an associated translation entry (npf_nat_t), which
     66   1.4   rmind  *	contains information used for backwards stream translation, i.e.
     67   1.4   rmind  *	original IP address with port and translation port, allocated from
     68   1.4   rmind  *	the port map.  Each NAT entry is associated with the policy, which
     69   1.4   rmind  *	contains translation IP address.  Allocated port is returned to the
     70  1.29   rmind  *	port map and NAT entry is destroyed when connection expires.
     71   1.1   rmind  */
     72   1.1   rmind 
     73   1.1   rmind #include <sys/cdefs.h>
     74  1.35   rmind __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.35 2014/11/26 21:25:35 rmind Exp $");
     75   1.1   rmind 
     76   1.1   rmind #include <sys/param.h>
     77  1.11   rmind #include <sys/types.h>
     78   1.1   rmind 
     79   1.1   rmind #include <sys/atomic.h>
     80   1.1   rmind #include <sys/bitops.h>
     81   1.4   rmind #include <sys/condvar.h>
     82   1.1   rmind #include <sys/kmem.h>
     83   1.4   rmind #include <sys/mutex.h>
     84   1.1   rmind #include <sys/pool.h>
     85  1.19   rmind #include <sys/proc.h>
     86   1.8     tls #include <sys/cprng.h>
     87   1.8     tls 
     88   1.1   rmind #include <net/pfil.h>
     89   1.1   rmind #include <netinet/in.h>
     90   1.1   rmind 
     91   1.1   rmind #include "npf_impl.h"
     92  1.29   rmind #include "npf_conn.h"
     93   1.1   rmind 
     94   1.1   rmind /*
     95   1.1   rmind  * NPF portmap structure.
     96   1.1   rmind  */
     97   1.1   rmind typedef struct {
     98   1.4   rmind 	u_int			p_refcnt;
     99   1.4   rmind 	uint32_t		p_bitmap[0];
    100   1.1   rmind } npf_portmap_t;
    101   1.1   rmind 
    102   1.1   rmind /* Portmap range: [ 1024 .. 65535 ] */
    103   1.4   rmind #define	PORTMAP_FIRST		(1024)
    104   1.4   rmind #define	PORTMAP_SIZE		((65536 - PORTMAP_FIRST) / 32)
    105  1.25   rmind #define	PORTMAP_FILLED		((uint32_t)~0U)
    106   1.4   rmind #define	PORTMAP_MASK		(31)
    107   1.4   rmind #define	PORTMAP_SHIFT		(5)
    108   1.4   rmind 
    109   1.4   rmind #define	PORTMAP_MEM_SIZE	\
    110   1.4   rmind     (sizeof(npf_portmap_t) + (PORTMAP_SIZE * sizeof(uint32_t)))
    111   1.1   rmind 
    112  1.12   rmind /*
    113  1.12   rmind  * NAT policy structure.
    114  1.12   rmind  */
    115   1.1   rmind struct npf_natpolicy {
    116  1.31   rmind 	kmutex_t		n_lock;
    117   1.4   rmind 	LIST_HEAD(, npf_nat)	n_nat_list;
    118  1.19   rmind 	volatile u_int		n_refcnt;
    119   1.4   rmind 	npf_portmap_t *		n_portmap;
    120  1.31   rmind 	uint64_t		n_id;
    121  1.31   rmind 
    122  1.31   rmind 	/*
    123  1.31   rmind 	 * Translation type, flags and address.  Optionally, prefix
    124  1.31   rmind 	 * for the NPTv6 and translation port.  Translation algorithm
    125  1.31   rmind 	 * and related data (for NPTv6, the adjustment value).
    126  1.31   rmind 	 *
    127  1.31   rmind 	 * NPF_NP_CMP_START mark starts here.
    128  1.31   rmind 	 */
    129   1.4   rmind 	int			n_type;
    130   1.6   rmind 	u_int			n_flags;
    131  1.31   rmind 	u_int			n_alen;
    132   1.4   rmind 	npf_addr_t		n_taddr;
    133  1.25   rmind 	npf_netmask_t		n_tmask;
    134   1.4   rmind 	in_port_t		n_tport;
    135  1.25   rmind 	u_int			n_algo;
    136  1.25   rmind 	union {
    137  1.25   rmind 		uint16_t	n_npt66_adj;
    138  1.25   rmind 	};
    139   1.1   rmind };
    140   1.1   rmind 
    141   1.4   rmind #define	NPF_NP_CMP_START	offsetof(npf_natpolicy_t, n_type)
    142   1.4   rmind #define	NPF_NP_CMP_SIZE		(sizeof(npf_natpolicy_t) - NPF_NP_CMP_START)
    143   1.4   rmind 
    144  1.12   rmind /*
    145  1.29   rmind  * NAT translation entry for a connection.
    146  1.12   rmind  */
    147   1.1   rmind struct npf_nat {
    148  1.28   rmind 	/* Associated NAT policy. */
    149   1.4   rmind 	npf_natpolicy_t *	nt_natpolicy;
    150  1.28   rmind 
    151  1.28   rmind 	/*
    152  1.28   rmind 	 * Original address and port (for backwards translation).
    153  1.28   rmind 	 * Translation port (for redirects).
    154  1.28   rmind 	 */
    155   1.4   rmind 	npf_addr_t		nt_oaddr;
    156   1.4   rmind 	in_port_t		nt_oport;
    157   1.4   rmind 	in_port_t		nt_tport;
    158  1.28   rmind 
    159   1.1   rmind 	/* ALG (if any) associated with this NAT entry. */
    160   1.4   rmind 	npf_alg_t *		nt_alg;
    161   1.4   rmind 	uintptr_t		nt_alg_arg;
    162  1.28   rmind 
    163  1.28   rmind 	LIST_ENTRY(npf_nat)	nt_entry;
    164  1.29   rmind 	npf_conn_t *		nt_conn;
    165   1.1   rmind };
    166   1.1   rmind 
    167   1.4   rmind static pool_cache_t		nat_cache	__read_mostly;
    168   1.1   rmind 
    169   1.1   rmind /*
    170   1.1   rmind  * npf_nat_sys{init,fini}: initialise/destroy NAT subsystem structures.
    171   1.1   rmind  */
    172   1.1   rmind 
    173   1.1   rmind void
    174   1.1   rmind npf_nat_sysinit(void)
    175   1.1   rmind {
    176   1.1   rmind 	nat_cache = pool_cache_init(sizeof(npf_nat_t), coherency_unit,
    177   1.1   rmind 	    0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
    178   1.1   rmind 	KASSERT(nat_cache != NULL);
    179   1.1   rmind }
    180   1.1   rmind 
    181   1.1   rmind void
    182   1.1   rmind npf_nat_sysfini(void)
    183   1.1   rmind {
    184  1.23   rmind 	/* All NAT policies should already be destroyed. */
    185   1.1   rmind 	pool_cache_destroy(nat_cache);
    186   1.1   rmind }
    187   1.1   rmind 
    188   1.1   rmind /*
    189   1.2   rmind  * npf_nat_newpolicy: create a new NAT policy.
    190   1.1   rmind  *
    191   1.1   rmind  * => Shares portmap if policy is on existing translation address.
    192   1.1   rmind  */
    193   1.1   rmind npf_natpolicy_t *
    194  1.31   rmind npf_nat_newpolicy(prop_dictionary_t natdict, npf_ruleset_t *rset)
    195   1.1   rmind {
    196   1.5   rmind 	npf_natpolicy_t *np;
    197   1.4   rmind 	prop_object_t obj;
    198   1.1   rmind 	npf_portmap_t *pm;
    199   1.1   rmind 
    200   1.1   rmind 	np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
    201   1.4   rmind 
    202  1.33   rmind 	/* The translation type, flags and policy ID. */
    203   1.6   rmind 	prop_dictionary_get_int32(natdict, "type", &np->n_type);
    204   1.6   rmind 	prop_dictionary_get_uint32(natdict, "flags", &np->n_flags);
    205  1.33   rmind 	prop_dictionary_get_uint64(natdict, "nat-policy", &np->n_id);
    206  1.10   rmind 
    207  1.10   rmind 	/* Should be exclusively either inbound or outbound NAT. */
    208  1.10   rmind 	if (((np->n_type == NPF_NATIN) ^ (np->n_type == NPF_NATOUT)) == 0) {
    209  1.25   rmind 		goto err;
    210  1.10   rmind 	}
    211  1.10   rmind 	mutex_init(&np->n_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    212  1.10   rmind 	LIST_INIT(&np->n_nat_list);
    213   1.4   rmind 
    214  1.25   rmind 	/* Translation IP, mask and port (if applicable). */
    215  1.32   rmind 	obj = prop_dictionary_get(natdict, "nat-ip");
    216  1.31   rmind 	np->n_alen = prop_data_size(obj);
    217  1.31   rmind 	if (np->n_alen == 0 || np->n_alen > sizeof(npf_addr_t)) {
    218  1.25   rmind 		goto err;
    219  1.25   rmind 	}
    220  1.31   rmind 	memcpy(&np->n_taddr, prop_data_data_nocopy(obj), np->n_alen);
    221  1.32   rmind 	prop_dictionary_get_uint8(natdict, "nat-mask", &np->n_tmask);
    222  1.32   rmind 	prop_dictionary_get_uint16(natdict, "nat-port", &np->n_tport);
    223   1.4   rmind 
    224  1.32   rmind 	prop_dictionary_get_uint32(natdict, "nat-algo", &np->n_algo);
    225  1.25   rmind 	switch (np->n_algo) {
    226  1.25   rmind 	case NPF_ALGO_NPT66:
    227  1.32   rmind 		prop_dictionary_get_uint16(natdict, "npt66-adj",
    228  1.25   rmind 		    &np->n_npt66_adj);
    229  1.25   rmind 		break;
    230  1.25   rmind 	default:
    231  1.25   rmind 		if (np->n_tmask != NPF_NO_NETMASK)
    232  1.25   rmind 			goto err;
    233  1.25   rmind 		break;
    234  1.25   rmind 	}
    235   1.2   rmind 
    236   1.5   rmind 	/* Determine if port map is needed. */
    237   1.5   rmind 	np->n_portmap = NULL;
    238   1.4   rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) == 0) {
    239   1.5   rmind 		/* No port map. */
    240   1.5   rmind 		return np;
    241   1.2   rmind 	}
    242   1.1   rmind 
    243   1.5   rmind 	/*
    244   1.5   rmind 	 * Inspect NAT policies in the ruleset for port map sharing.
    245   1.5   rmind 	 * Note that npf_ruleset_sharepm() will increase the reference count.
    246   1.5   rmind 	 */
    247  1.31   rmind 	if (!npf_ruleset_sharepm(rset, np)) {
    248   1.1   rmind 		/* Allocate a new port map for the NAT policy. */
    249   1.4   rmind 		pm = kmem_zalloc(PORTMAP_MEM_SIZE, KM_SLEEP);
    250   1.1   rmind 		pm->p_refcnt = 1;
    251   1.1   rmind 		KASSERT((uintptr_t)pm->p_bitmap == (uintptr_t)pm + sizeof(*pm));
    252   1.5   rmind 		np->n_portmap = pm;
    253   1.1   rmind 	} else {
    254   1.5   rmind 		KASSERT(np->n_portmap != NULL);
    255   1.1   rmind 	}
    256   1.1   rmind 	return np;
    257  1.25   rmind err:
    258  1.25   rmind 	kmem_free(np, sizeof(npf_natpolicy_t));
    259  1.25   rmind 	return NULL;
    260   1.1   rmind }
    261   1.1   rmind 
    262  1.32   rmind int
    263  1.32   rmind npf_nat_policyexport(const npf_natpolicy_t *np, prop_dictionary_t natdict)
    264  1.32   rmind {
    265  1.32   rmind 	prop_data_t d;
    266  1.32   rmind 
    267  1.32   rmind 	prop_dictionary_set_int32(natdict, "type", np->n_type);
    268  1.32   rmind 	prop_dictionary_set_uint32(natdict, "flags", np->n_flags);
    269  1.32   rmind 
    270  1.32   rmind 	d = prop_data_create_data(&np->n_taddr, np->n_alen);
    271  1.32   rmind 	prop_dictionary_set_and_rel(natdict, "nat-ip", d);
    272  1.32   rmind 
    273  1.32   rmind 	prop_dictionary_set_uint8(natdict, "nat-mask", np->n_tmask);
    274  1.32   rmind 	prop_dictionary_set_uint16(natdict, "nat-port", np->n_tport);
    275  1.32   rmind 	prop_dictionary_set_uint32(natdict, "nat-algo", np->n_algo);
    276  1.32   rmind 
    277  1.32   rmind 	switch (np->n_algo) {
    278  1.32   rmind 	case NPF_ALGO_NPT66:
    279  1.32   rmind 		prop_dictionary_set_uint16(natdict, "npt66-adj", np->n_npt66_adj);
    280  1.32   rmind 		break;
    281  1.32   rmind 	}
    282  1.32   rmind 	prop_dictionary_set_uint64(natdict, "nat-policy", np->n_id);
    283  1.32   rmind 	return 0;
    284  1.32   rmind }
    285  1.32   rmind 
    286   1.1   rmind /*
    287   1.1   rmind  * npf_nat_freepolicy: free NAT policy and, on last reference, free portmap.
    288   1.1   rmind  *
    289   1.4   rmind  * => Called from npf_rule_free() during the reload via npf_ruleset_destroy().
    290   1.1   rmind  */
    291   1.1   rmind void
    292   1.1   rmind npf_nat_freepolicy(npf_natpolicy_t *np)
    293   1.1   rmind {
    294   1.1   rmind 	npf_portmap_t *pm = np->n_portmap;
    295  1.29   rmind 	npf_conn_t *con;
    296   1.4   rmind 	npf_nat_t *nt;
    297   1.1   rmind 
    298  1.22   rmind 	/*
    299  1.22   rmind 	 * Disassociate all entries from the policy.  At this point,
    300  1.22   rmind 	 * new entries can no longer be created for this policy.
    301  1.22   rmind 	 */
    302  1.28   rmind 	while (np->n_refcnt) {
    303  1.28   rmind 		mutex_enter(&np->n_lock);
    304  1.28   rmind 		LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
    305  1.29   rmind 			con = nt->nt_conn;
    306  1.29   rmind 			KASSERT(con != NULL);
    307  1.29   rmind 			npf_conn_expire(con);
    308  1.28   rmind 		}
    309  1.28   rmind 		mutex_exit(&np->n_lock);
    310   1.4   rmind 
    311  1.28   rmind 		/* Kick the worker - all references should be going away. */
    312  1.28   rmind 		npf_worker_signal();
    313  1.19   rmind 		kpause("npfgcnat", false, 1, NULL);
    314  1.19   rmind 	}
    315  1.22   rmind 	KASSERT(LIST_EMPTY(&np->n_nat_list));
    316  1.35   rmind 	KASSERT(pm == NULL || pm->p_refcnt > 0);
    317  1.19   rmind 
    318   1.4   rmind 	/* Destroy the port map, on last reference. */
    319  1.35   rmind 	if (pm && atomic_dec_uint_nv(&pm->p_refcnt) == 0) {
    320   1.2   rmind 		KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
    321   1.4   rmind 		kmem_free(pm, PORTMAP_MEM_SIZE);
    322   1.1   rmind 	}
    323   1.4   rmind 	mutex_destroy(&np->n_lock);
    324   1.1   rmind 	kmem_free(np, sizeof(npf_natpolicy_t));
    325   1.1   rmind }
    326   1.1   rmind 
    327  1.13   rmind void
    328  1.15   rmind npf_nat_freealg(npf_natpolicy_t *np, npf_alg_t *alg)
    329  1.13   rmind {
    330  1.15   rmind 	npf_nat_t *nt;
    331  1.15   rmind 
    332  1.15   rmind 	mutex_enter(&np->n_lock);
    333  1.15   rmind 	LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
    334  1.31   rmind 		if (nt->nt_alg == alg)
    335  1.31   rmind 			nt->nt_alg = NULL;
    336  1.15   rmind 	}
    337  1.15   rmind 	mutex_exit(&np->n_lock);
    338  1.13   rmind }
    339  1.13   rmind 
    340   1.5   rmind /*
    341  1.31   rmind  * npf_nat_cmppolicy: compare two NAT policies.
    342   1.5   rmind  *
    343   1.5   rmind  * => Return 0 on match, and non-zero otherwise.
    344   1.5   rmind  */
    345   1.4   rmind bool
    346  1.31   rmind npf_nat_cmppolicy(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
    347   1.1   rmind {
    348  1.31   rmind 	const void *np_raw, *mnp_raw;
    349  1.31   rmind 
    350   1.4   rmind 	/*
    351   1.4   rmind 	 * Compare the relevant NAT policy information (in raw form),
    352   1.4   rmind 	 * which is enough for matching criterion.
    353   1.4   rmind 	 */
    354   1.5   rmind 	KASSERT(np && mnp && np != mnp);
    355  1.31   rmind 	np_raw = (const uint8_t *)np + NPF_NP_CMP_START;
    356  1.31   rmind 	mnp_raw = (const uint8_t *)mnp + NPF_NP_CMP_START;
    357  1.31   rmind 	return memcmp(np_raw, mnp_raw, NPF_NP_CMP_SIZE) == 0;
    358   1.1   rmind }
    359   1.1   rmind 
    360   1.5   rmind bool
    361   1.5   rmind npf_nat_sharepm(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
    362   1.5   rmind {
    363   1.5   rmind 	npf_portmap_t *pm, *mpm;
    364   1.5   rmind 
    365   1.5   rmind 	KASSERT(np && mnp && np != mnp);
    366   1.5   rmind 
    367   1.5   rmind 	/* Using port map and having equal translation address? */
    368   1.5   rmind 	if ((np->n_flags & mnp->n_flags & NPF_NAT_PORTMAP) == 0) {
    369   1.5   rmind 		return false;
    370   1.5   rmind 	}
    371  1.31   rmind 	if (np->n_alen != mnp->n_alen) {
    372   1.5   rmind 		return false;
    373   1.5   rmind 	}
    374  1.31   rmind 	if (memcmp(&np->n_taddr, &mnp->n_taddr, np->n_alen) != 0) {
    375   1.5   rmind 		return false;
    376   1.5   rmind 	}
    377   1.5   rmind 	mpm = mnp->n_portmap;
    378  1.35   rmind 	KASSERT(mpm == NULL || mpm->p_refcnt > 0);
    379  1.35   rmind 
    380  1.35   rmind 	/*
    381  1.35   rmind 	 * If NAT policy has an old port map - drop the reference
    382  1.35   rmind 	 * and destroy the port map if it was the last.
    383  1.35   rmind 	 */
    384  1.35   rmind 	if (mpm && atomic_dec_uint_nv(&mpm->p_refcnt) == 0) {
    385  1.35   rmind 		kmem_free(mpm, PORTMAP_MEM_SIZE);
    386   1.5   rmind 	}
    387  1.35   rmind 
    388   1.5   rmind 	/* Share the port map. */
    389   1.5   rmind 	pm = np->n_portmap;
    390  1.35   rmind 	atomic_inc_uint(&pm->p_refcnt);
    391   1.5   rmind 	mnp->n_portmap = pm;
    392   1.5   rmind 	return true;
    393   1.5   rmind }
    394   1.5   rmind 
    395  1.31   rmind void
    396  1.31   rmind npf_nat_setid(npf_natpolicy_t *np, uint64_t id)
    397  1.31   rmind {
    398  1.31   rmind 	np->n_id = id;
    399  1.31   rmind }
    400  1.31   rmind 
    401  1.31   rmind uint64_t
    402  1.31   rmind npf_nat_getid(const npf_natpolicy_t *np)
    403  1.31   rmind {
    404  1.31   rmind 	return np->n_id;
    405  1.31   rmind }
    406  1.31   rmind 
    407   1.1   rmind /*
    408   1.1   rmind  * npf_nat_getport: allocate and return a port in the NAT policy portmap.
    409   1.1   rmind  *
    410   1.1   rmind  * => Returns in network byte-order.
    411   1.1   rmind  * => Zero indicates failure.
    412   1.1   rmind  */
    413   1.1   rmind static in_port_t
    414   1.1   rmind npf_nat_getport(npf_natpolicy_t *np)
    415   1.1   rmind {
    416   1.1   rmind 	npf_portmap_t *pm = np->n_portmap;
    417   1.1   rmind 	u_int n = PORTMAP_SIZE, idx, bit;
    418   1.1   rmind 	uint32_t map, nmap;
    419   1.1   rmind 
    420   1.8     tls 	idx = cprng_fast32() % PORTMAP_SIZE;
    421   1.1   rmind 	for (;;) {
    422   1.1   rmind 		KASSERT(idx < PORTMAP_SIZE);
    423   1.1   rmind 		map = pm->p_bitmap[idx];
    424   1.1   rmind 		if (__predict_false(map == PORTMAP_FILLED)) {
    425   1.1   rmind 			if (n-- == 0) {
    426   1.1   rmind 				/* No space. */
    427   1.1   rmind 				return 0;
    428   1.1   rmind 			}
    429   1.2   rmind 			/* This bitmap is filled, next. */
    430   1.1   rmind 			idx = (idx ? idx : PORTMAP_SIZE) - 1;
    431   1.1   rmind 			continue;
    432   1.1   rmind 		}
    433   1.1   rmind 		bit = ffs32(~map) - 1;
    434   1.1   rmind 		nmap = map | (1 << bit);
    435   1.1   rmind 		if (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map) {
    436   1.1   rmind 			/* Success. */
    437   1.1   rmind 			break;
    438   1.1   rmind 		}
    439   1.1   rmind 	}
    440   1.1   rmind 	return htons(PORTMAP_FIRST + (idx << PORTMAP_SHIFT) + bit);
    441   1.1   rmind }
    442   1.1   rmind 
    443   1.1   rmind /*
    444   1.4   rmind  * npf_nat_takeport: allocate specific port in the NAT policy portmap.
    445   1.4   rmind  */
    446   1.4   rmind static bool
    447   1.4   rmind npf_nat_takeport(npf_natpolicy_t *np, in_port_t port)
    448   1.4   rmind {
    449   1.4   rmind 	npf_portmap_t *pm = np->n_portmap;
    450   1.4   rmind 	uint32_t map, nmap;
    451   1.4   rmind 	u_int idx, bit;
    452   1.4   rmind 
    453   1.4   rmind 	port = ntohs(port) - PORTMAP_FIRST;
    454   1.4   rmind 	idx = port >> PORTMAP_SHIFT;
    455   1.4   rmind 	bit = port & PORTMAP_MASK;
    456   1.4   rmind 	map = pm->p_bitmap[idx];
    457   1.4   rmind 	nmap = map | (1 << bit);
    458   1.4   rmind 	if (map == nmap) {
    459   1.4   rmind 		/* Already taken. */
    460   1.4   rmind 		return false;
    461   1.4   rmind 	}
    462   1.4   rmind 	return atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map;
    463   1.4   rmind }
    464   1.4   rmind 
    465   1.4   rmind /*
    466   1.1   rmind  * npf_nat_putport: return port as available in the NAT policy portmap.
    467   1.1   rmind  *
    468   1.1   rmind  * => Port should be in network byte-order.
    469   1.1   rmind  */
    470   1.1   rmind static void
    471   1.1   rmind npf_nat_putport(npf_natpolicy_t *np, in_port_t port)
    472   1.1   rmind {
    473   1.1   rmind 	npf_portmap_t *pm = np->n_portmap;
    474   1.1   rmind 	uint32_t map, nmap;
    475   1.1   rmind 	u_int idx, bit;
    476   1.1   rmind 
    477   1.1   rmind 	port = ntohs(port) - PORTMAP_FIRST;
    478   1.1   rmind 	idx = port >> PORTMAP_SHIFT;
    479   1.1   rmind 	bit = port & PORTMAP_MASK;
    480   1.1   rmind 	do {
    481   1.1   rmind 		map = pm->p_bitmap[idx];
    482   1.1   rmind 		KASSERT(map | (1 << bit));
    483   1.1   rmind 		nmap = map & ~(1 << bit);
    484   1.1   rmind 	} while (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) != map);
    485   1.1   rmind }
    486   1.1   rmind 
    487   1.1   rmind /*
    488  1.23   rmind  * npf_nat_which: tell which address (source or destination) should be
    489  1.23   rmind  * rewritten given the combination of the NAT type and flow direction.
    490  1.23   rmind  */
    491  1.23   rmind static inline u_int
    492  1.23   rmind npf_nat_which(const int type, bool forw)
    493  1.23   rmind {
    494  1.23   rmind 	/*
    495  1.23   rmind 	 * Outbound NAT rewrites:
    496  1.24   rmind 	 * - Source (NPF_SRC) on "forwards" stream.
    497  1.24   rmind 	 * - Destination (NPF_DST) on "backwards" stream.
    498  1.23   rmind 	 * Inbound NAT is other way round.
    499  1.23   rmind 	 */
    500  1.23   rmind 	if (type == NPF_NATOUT) {
    501  1.23   rmind 		forw = !forw;
    502  1.23   rmind 	} else {
    503  1.23   rmind 		KASSERT(type == NPF_NATIN);
    504  1.23   rmind 	}
    505  1.23   rmind 	CTASSERT(NPF_SRC == 0 && NPF_DST == 1);
    506  1.24   rmind 	KASSERT(forw == NPF_SRC || forw == NPF_DST);
    507  1.23   rmind 	return (u_int)forw;
    508  1.23   rmind }
    509  1.23   rmind 
    510  1.23   rmind /*
    511   1.2   rmind  * npf_nat_inspect: inspect packet against NAT ruleset and return a policy.
    512  1.19   rmind  *
    513  1.19   rmind  * => Acquire a reference on the policy, if found.
    514   1.2   rmind  */
    515   1.2   rmind static npf_natpolicy_t *
    516  1.30   rmind npf_nat_inspect(npf_cache_t *npc, const int di)
    517   1.2   rmind {
    518  1.19   rmind 	int slock = npf_config_read_enter();
    519  1.19   rmind 	npf_ruleset_t *rlset = npf_config_natset();
    520   1.6   rmind 	npf_natpolicy_t *np;
    521   1.2   rmind 	npf_rule_t *rl;
    522   1.2   rmind 
    523  1.30   rmind 	rl = npf_ruleset_inspect(npc, rlset, di, NPF_LAYER_3);
    524   1.6   rmind 	if (rl == NULL) {
    525  1.19   rmind 		npf_config_read_exit(slock);
    526   1.6   rmind 		return NULL;
    527   1.6   rmind 	}
    528   1.6   rmind 	np = npf_rule_getnat(rl);
    529  1.19   rmind 	atomic_inc_uint(&np->n_refcnt);
    530  1.19   rmind 	npf_config_read_exit(slock);
    531   1.6   rmind 	return np;
    532   1.2   rmind }
    533   1.2   rmind 
    534   1.2   rmind /*
    535   1.2   rmind  * npf_nat_create: create a new NAT translation entry.
    536   1.1   rmind  */
    537   1.2   rmind static npf_nat_t *
    538  1.29   rmind npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np, npf_conn_t *con)
    539   1.1   rmind {
    540  1.19   rmind 	const int proto = npc->npc_proto;
    541   1.2   rmind 	npf_nat_t *nt;
    542   1.2   rmind 
    543   1.7  zoltan 	KASSERT(npf_iscached(npc, NPC_IP46));
    544   1.7  zoltan 	KASSERT(npf_iscached(npc, NPC_LAYER4));
    545   1.3   rmind 
    546  1.29   rmind 	/* Construct a new NAT entry and associate it with the connection. */
    547   1.2   rmind 	nt = pool_cache_get(nat_cache, PR_NOWAIT);
    548   1.2   rmind 	if (nt == NULL){
    549   1.2   rmind 		return NULL;
    550   1.2   rmind 	}
    551   1.4   rmind 	npf_stats_inc(NPF_STAT_NAT_CREATE);
    552   1.5   rmind 	nt->nt_natpolicy = np;
    553  1.29   rmind 	nt->nt_conn = con;
    554   1.5   rmind 	nt->nt_alg = NULL;
    555   1.5   rmind 
    556   1.2   rmind 	/* Save the original address which may be rewritten. */
    557   1.2   rmind 	if (np->n_type == NPF_NATOUT) {
    558  1.23   rmind 		/* Outbound NAT: source (think internal) address. */
    559  1.23   rmind 		memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_SRC], npc->npc_alen);
    560   1.2   rmind 	} else {
    561  1.23   rmind 		/* Inbound NAT: destination (think external) address. */
    562   1.2   rmind 		KASSERT(np->n_type == NPF_NATIN);
    563  1.23   rmind 		memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_DST], npc->npc_alen);
    564   1.2   rmind 	}
    565   1.2   rmind 
    566   1.2   rmind 	/*
    567   1.2   rmind 	 * Port translation, if required, and if it is TCP/UDP.
    568   1.2   rmind 	 */
    569   1.2   rmind 	if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
    570   1.2   rmind 	    (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
    571   1.2   rmind 		nt->nt_oport = 0;
    572   1.2   rmind 		nt->nt_tport = 0;
    573  1.12   rmind 		goto out;
    574   1.2   rmind 	}
    575  1.12   rmind 
    576   1.3   rmind 	/* Save the relevant TCP/UDP port. */
    577   1.3   rmind 	if (proto == IPPROTO_TCP) {
    578  1.18   rmind 		const struct tcphdr *th = npc->npc_l4.tcp;
    579   1.3   rmind 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
    580   1.3   rmind 		    th->th_sport : th->th_dport;
    581   1.2   rmind 	} else {
    582  1.18   rmind 		const struct udphdr *uh = npc->npc_l4.udp;
    583   1.3   rmind 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
    584   1.3   rmind 		    uh->uh_sport : uh->uh_dport;
    585   1.2   rmind 	}
    586   1.3   rmind 
    587   1.2   rmind 	/* Get a new port for translation. */
    588   1.2   rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
    589   1.2   rmind 		nt->nt_tport = npf_nat_getport(np);
    590   1.2   rmind 	} else {
    591   1.2   rmind 		nt->nt_tport = np->n_tport;
    592   1.2   rmind 	}
    593  1.12   rmind out:
    594  1.12   rmind 	mutex_enter(&np->n_lock);
    595  1.12   rmind 	LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
    596  1.12   rmind 	mutex_exit(&np->n_lock);
    597   1.2   rmind 	return nt;
    598   1.2   rmind }
    599   1.2   rmind 
    600   1.2   rmind /*
    601  1.24   rmind  * npf_nat_translate: perform translation given the state data.
    602  1.24   rmind  */
    603  1.26   rmind static inline int
    604  1.30   rmind npf_nat_translate(npf_cache_t *npc, npf_nat_t *nt, bool forw)
    605  1.24   rmind {
    606  1.24   rmind 	const npf_natpolicy_t *np = nt->nt_natpolicy;
    607  1.26   rmind 	const u_int which = npf_nat_which(np->n_type, forw);
    608  1.24   rmind 	const npf_addr_t *addr;
    609  1.24   rmind 	in_port_t port;
    610  1.24   rmind 
    611  1.24   rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    612  1.24   rmind 	KASSERT(npf_iscached(npc, NPC_LAYER4));
    613  1.24   rmind 
    614  1.24   rmind 	if (forw) {
    615  1.24   rmind 		/* "Forwards" stream: use translation address/port. */
    616  1.24   rmind 		addr = &np->n_taddr;
    617  1.24   rmind 		port = nt->nt_tport;
    618  1.24   rmind 	} else {
    619  1.24   rmind 		/* "Backwards" stream: use original address/port. */
    620  1.24   rmind 		addr = &nt->nt_oaddr;
    621  1.24   rmind 		port = nt->nt_oport;
    622  1.24   rmind 	}
    623  1.24   rmind 	KASSERT((np->n_flags & NPF_NAT_PORTS) != 0 || port == 0);
    624  1.24   rmind 
    625  1.26   rmind 	/* Execute ALG translation first. */
    626  1.24   rmind 	if ((npc->npc_info & NPC_ALG_EXEC) == 0) {
    627  1.24   rmind 		npc->npc_info |= NPC_ALG_EXEC;
    628  1.30   rmind 		npf_alg_exec(npc, nt, forw);
    629  1.30   rmind 		npf_recache(npc);
    630  1.24   rmind 	}
    631  1.30   rmind 	KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
    632  1.24   rmind 
    633  1.24   rmind 	/* Finally, perform the translation. */
    634  1.26   rmind 	return npf_napt_rwr(npc, which, addr, port);
    635  1.24   rmind }
    636  1.24   rmind 
    637  1.24   rmind /*
    638  1.25   rmind  * npf_nat_algo: perform the translation given the algorithm.
    639  1.25   rmind  */
    640  1.29   rmind static inline int
    641  1.25   rmind npf_nat_algo(npf_cache_t *npc, const npf_natpolicy_t *np, bool forw)
    642  1.25   rmind {
    643  1.26   rmind 	const u_int which = npf_nat_which(np->n_type, forw);
    644  1.25   rmind 	int error;
    645  1.25   rmind 
    646  1.25   rmind 	switch (np->n_algo) {
    647  1.25   rmind 	case NPF_ALGO_NPT66:
    648  1.25   rmind 		error = npf_npt66_rwr(npc, which, &np->n_taddr,
    649  1.25   rmind 		    np->n_tmask, np->n_npt66_adj);
    650  1.25   rmind 		break;
    651  1.25   rmind 	default:
    652  1.26   rmind 		error = npf_napt_rwr(npc, which, &np->n_taddr, np->n_tport);
    653  1.25   rmind 		break;
    654  1.25   rmind 	}
    655  1.25   rmind 
    656  1.25   rmind 	return error;
    657  1.31   rmind }
    658  1.25   rmind 
    659  1.25   rmind /*
    660   1.2   rmind  * npf_do_nat:
    661  1.29   rmind  *	- Inspect packet for a NAT policy, unless a connection with a NAT
    662   1.4   rmind  *	  association already exists.  In such case, determine whether it
    663   1.2   rmind  *	  is a "forwards" or "backwards" stream.
    664   1.4   rmind  *	- Perform translation: rewrite source or destination fields,
    665   1.4   rmind  *	  depending on translation type and direction.
    666  1.29   rmind  *	- Associate a NAT policy with a connection (may establish a new).
    667   1.2   rmind  */
    668   1.2   rmind int
    669  1.30   rmind npf_do_nat(npf_cache_t *npc, npf_conn_t *con, const int di)
    670   1.2   rmind {
    671  1.30   rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    672  1.29   rmind 	npf_conn_t *ncon = NULL;
    673   1.1   rmind 	npf_natpolicy_t *np;
    674   1.1   rmind 	npf_nat_t *nt;
    675   1.1   rmind 	int error;
    676  1.22   rmind 	bool forw;
    677   1.1   rmind 
    678   1.1   rmind 	/* All relevant IPv4 data should be already cached. */
    679   1.3   rmind 	if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
    680   1.1   rmind 		return 0;
    681   1.1   rmind 	}
    682  1.18   rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    683   1.1   rmind 
    684   1.2   rmind 	/*
    685  1.29   rmind 	 * Return the NAT entry associated with the connection, if any.
    686   1.3   rmind 	 * Determines whether the stream is "forwards" or "backwards".
    687  1.29   rmind 	 * Note: no need to lock, since reference on connection is held.
    688   1.2   rmind 	 */
    689  1.29   rmind 	if (con && (nt = npf_conn_retnat(con, di, &forw)) != NULL) {
    690   1.1   rmind 		np = nt->nt_natpolicy;
    691   1.2   rmind 		goto translate;
    692   1.1   rmind 	}
    693   1.1   rmind 
    694   1.6   rmind 	/*
    695  1.29   rmind 	 * Inspect the packet for a NAT policy, if there is no connection.
    696  1.19   rmind 	 * Note: acquires a reference if found.
    697   1.6   rmind 	 */
    698  1.30   rmind 	np = npf_nat_inspect(npc, di);
    699   1.1   rmind 	if (np == NULL) {
    700   1.1   rmind 		/* If packet does not match - done. */
    701   1.1   rmind 		return 0;
    702   1.1   rmind 	}
    703   1.2   rmind 	forw = true;
    704   1.1   rmind 
    705  1.24   rmind 	/* Static NAT - just perform the translation. */
    706  1.24   rmind 	if (np->n_flags & NPF_NAT_STATIC) {
    707  1.24   rmind 		if (nbuf_cksum_barrier(nbuf, di)) {
    708  1.30   rmind 			npf_recache(npc);
    709  1.24   rmind 		}
    710  1.25   rmind 		error = npf_nat_algo(npc, np, forw);
    711  1.24   rmind 		atomic_dec_uint(&np->n_refcnt);
    712  1.24   rmind 		return error;
    713  1.24   rmind 	}
    714  1.24   rmind 
    715   1.4   rmind 	/*
    716  1.29   rmind 	 * If there is no local connection (no "stateful" rule - unusual,
    717  1.29   rmind 	 * but possible configuration), establish one before translation.
    718  1.29   rmind 	 * Note that it is not a "pass" connection, therefore passing of
    719  1.29   rmind 	 * "backwards" stream depends on other, stateless filtering rules.
    720  1.29   rmind 	 */
    721  1.29   rmind 	if (con == NULL) {
    722  1.30   rmind 		ncon = npf_conn_establish(npc, di, true);
    723  1.29   rmind 		if (ncon == NULL) {
    724  1.22   rmind 			atomic_dec_uint(&np->n_refcnt);
    725  1.22   rmind 			return ENOMEM;
    726   1.1   rmind 		}
    727  1.29   rmind 		con = ncon;
    728   1.1   rmind 	}
    729  1.22   rmind 
    730  1.22   rmind 	/*
    731  1.29   rmind 	 * Create a new NAT entry and associate with the connection.
    732  1.22   rmind 	 * We will consume the reference on success (release on error).
    733  1.22   rmind 	 */
    734  1.29   rmind 	nt = npf_nat_create(npc, np, con);
    735  1.22   rmind 	if (nt == NULL) {
    736  1.22   rmind 		atomic_dec_uint(&np->n_refcnt);
    737  1.22   rmind 		error = ENOMEM;
    738  1.22   rmind 		goto out;
    739  1.22   rmind 	}
    740  1.22   rmind 
    741  1.29   rmind 	/* Associate the NAT translation entry with the connection. */
    742  1.29   rmind 	error = npf_conn_setnat(npc, con, nt, np->n_type);
    743   1.2   rmind 	if (error) {
    744  1.22   rmind 		/* Will release the reference. */
    745  1.22   rmind 		npf_nat_destroy(nt);
    746   1.1   rmind 		goto out;
    747   1.1   rmind 	}
    748   1.1   rmind 
    749  1.22   rmind 	/* Determine whether any ALG matches. */
    750  1.30   rmind 	if (npf_alg_match(npc, nt, di)) {
    751  1.22   rmind 		KASSERT(nt->nt_alg != NULL);
    752  1.22   rmind 	}
    753  1.22   rmind 
    754  1.22   rmind translate:
    755  1.23   rmind 	/* May need to process the delayed checksums first (XXX: NetBSD). */
    756  1.23   rmind 	if (nbuf_cksum_barrier(nbuf, di)) {
    757  1.30   rmind 		npf_recache(npc);
    758  1.23   rmind 	}
    759  1.23   rmind 
    760  1.22   rmind 	/* Perform the translation. */
    761  1.30   rmind 	error = npf_nat_translate(npc, nt, forw);
    762   1.1   rmind out:
    763  1.29   rmind 	if (__predict_false(ncon)) {
    764  1.24   rmind 		if (error) {
    765  1.24   rmind 			/* It created for NAT - just expire. */
    766  1.29   rmind 			npf_conn_expire(ncon);
    767  1.24   rmind 		}
    768  1.29   rmind 		npf_conn_release(ncon);
    769   1.1   rmind 	}
    770   1.1   rmind 	return error;
    771   1.1   rmind }
    772   1.1   rmind 
    773   1.1   rmind /*
    774   1.4   rmind  * npf_nat_gettrans: return translation IP address and port.
    775   1.4   rmind  */
    776   1.4   rmind void
    777   1.4   rmind npf_nat_gettrans(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
    778   1.4   rmind {
    779   1.4   rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    780   1.4   rmind 
    781   1.4   rmind 	*addr = &np->n_taddr;
    782   1.4   rmind 	*port = nt->nt_tport;
    783   1.4   rmind }
    784   1.4   rmind 
    785   1.4   rmind /*
    786   1.2   rmind  * npf_nat_getorig: return original IP address and port from translation entry.
    787   1.1   rmind  */
    788   1.1   rmind void
    789   1.3   rmind npf_nat_getorig(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
    790   1.1   rmind {
    791   1.3   rmind 	*addr = &nt->nt_oaddr;
    792   1.2   rmind 	*port = nt->nt_oport;
    793   1.1   rmind }
    794   1.1   rmind 
    795   1.3   rmind /*
    796   1.3   rmind  * npf_nat_setalg: associate an ALG with the NAT entry.
    797   1.3   rmind  */
    798   1.1   rmind void
    799   1.1   rmind npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
    800   1.1   rmind {
    801   1.1   rmind 	nt->nt_alg = alg;
    802   1.1   rmind 	nt->nt_alg_arg = arg;
    803   1.1   rmind }
    804   1.1   rmind 
    805   1.1   rmind /*
    806  1.29   rmind  * npf_nat_destroy: destroy NAT structure (performed on connection expiration).
    807   1.1   rmind  */
    808   1.1   rmind void
    809  1.22   rmind npf_nat_destroy(npf_nat_t *nt)
    810   1.1   rmind {
    811   1.2   rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    812   1.1   rmind 
    813   1.4   rmind 	/* Return any taken port to the portmap. */
    814   1.4   rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
    815   1.1   rmind 		npf_nat_putport(np, nt->nt_tport);
    816   1.1   rmind 	}
    817   1.4   rmind 
    818   1.4   rmind 	mutex_enter(&np->n_lock);
    819   1.4   rmind 	LIST_REMOVE(nt, nt_entry);
    820  1.34   rmind 	KASSERT(np->n_refcnt > 0);
    821  1.19   rmind 	atomic_dec_uint(&np->n_refcnt);
    822   1.4   rmind 	mutex_exit(&np->n_lock);
    823   1.4   rmind 
    824   1.1   rmind 	pool_cache_put(nat_cache, nt);
    825   1.4   rmind 	npf_stats_inc(NPF_STAT_NAT_DESTROY);
    826   1.4   rmind }
    827   1.4   rmind 
    828   1.4   rmind /*
    829  1.31   rmind  * npf_nat_export: serialise the NAT entry with a NAT policy ID.
    830   1.4   rmind  */
    831  1.31   rmind void
    832  1.31   rmind npf_nat_export(prop_dictionary_t condict, npf_nat_t *nt)
    833   1.4   rmind {
    834   1.4   rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    835  1.31   rmind 	prop_dictionary_t natdict;
    836  1.31   rmind 	prop_data_t d;
    837   1.4   rmind 
    838  1.31   rmind 	natdict = prop_dictionary_create();
    839  1.31   rmind 	d = prop_data_create_data(&nt->nt_oaddr, sizeof(npf_addr_t));
    840  1.31   rmind 	prop_dictionary_set_and_rel(natdict, "oaddr", d);
    841  1.31   rmind 	prop_dictionary_set_uint16(natdict, "oport", nt->nt_oport);
    842  1.31   rmind 	prop_dictionary_set_uint16(natdict, "tport", nt->nt_tport);
    843  1.31   rmind 	prop_dictionary_set_uint64(natdict, "nat-policy", np->n_id);
    844  1.31   rmind 	prop_dictionary_set_and_rel(condict, "nat", natdict);
    845   1.4   rmind }
    846   1.4   rmind 
    847   1.4   rmind /*
    848  1.31   rmind  * npf_nat_import: find the NAT policy and unserialise the NAT entry.
    849   1.4   rmind  */
    850   1.4   rmind npf_nat_t *
    851  1.31   rmind npf_nat_import(prop_dictionary_t natdict, npf_ruleset_t *natlist,
    852  1.31   rmind     npf_conn_t *con)
    853   1.4   rmind {
    854   1.4   rmind 	npf_natpolicy_t *np;
    855   1.4   rmind 	npf_nat_t *nt;
    856  1.31   rmind 	uint64_t np_id;
    857  1.31   rmind 	const void *d;
    858   1.4   rmind 
    859  1.31   rmind 	prop_dictionary_get_uint64(natdict, "nat-policy", &np_id);
    860  1.31   rmind 	if ((np = npf_ruleset_findnat(natlist, np_id)) == NULL) {
    861   1.4   rmind 		return NULL;
    862   1.4   rmind 	}
    863  1.31   rmind 	nt = pool_cache_get(nat_cache, PR_WAITOK);
    864  1.31   rmind 	memset(nt, 0, sizeof(npf_nat_t));
    865   1.4   rmind 
    866  1.31   rmind 	prop_object_t obj = prop_dictionary_get(natdict, "oaddr");
    867  1.31   rmind 	if ((d = prop_data_data_nocopy(obj)) == NULL ||
    868  1.31   rmind 	    prop_data_size(obj) != sizeof(npf_addr_t)) {
    869  1.31   rmind 		pool_cache_put(nat_cache, nt);
    870   1.4   rmind 		return NULL;
    871   1.4   rmind 	}
    872  1.31   rmind 	memcpy(&nt->nt_oaddr, d, sizeof(npf_addr_t));
    873  1.31   rmind 	prop_dictionary_get_uint16(natdict, "oport", &nt->nt_oport);
    874  1.31   rmind 	prop_dictionary_get_uint16(natdict, "tport", &nt->nt_tport);
    875   1.4   rmind 
    876   1.4   rmind 	/* Take a specific port from port-map. */
    877  1.31   rmind 	if (!npf_nat_takeport(np, nt->nt_tport)) {
    878  1.31   rmind 		pool_cache_put(nat_cache, nt);
    879   1.4   rmind 		return NULL;
    880   1.4   rmind 	}
    881   1.4   rmind 
    882  1.34   rmind 	/*
    883  1.34   rmind 	 * Associate, take a reference and insert.  Unlocked since
    884  1.34   rmind 	 * the policy is not yet visible.
    885  1.34   rmind 	 */
    886   1.4   rmind 	nt->nt_natpolicy = np;
    887  1.29   rmind 	nt->nt_conn = con;
    888  1.34   rmind 	np->n_refcnt++;
    889  1.34   rmind 	LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
    890   1.4   rmind 	return nt;
    891   1.1   rmind }
    892   1.1   rmind 
    893   1.1   rmind #if defined(DDB) || defined(_NPF_TESTING)
    894   1.1   rmind 
    895   1.1   rmind void
    896  1.14   rmind npf_nat_dump(const npf_nat_t *nt)
    897   1.1   rmind {
    898  1.14   rmind 	const npf_natpolicy_t *np;
    899   1.1   rmind 	struct in_addr ip;
    900   1.1   rmind 
    901   1.4   rmind 	np = nt->nt_natpolicy;
    902   1.4   rmind 	memcpy(&ip, &np->n_taddr, sizeof(ip));
    903   1.4   rmind 	printf("\tNATP(%p): type %d flags 0x%x taddr %s tport %d\n",
    904   1.4   rmind 	    np, np->n_type, np->n_flags, inet_ntoa(ip), np->n_tport);
    905   1.4   rmind 	memcpy(&ip, &nt->nt_oaddr, sizeof(ip));
    906   1.4   rmind 	printf("\tNAT: original address %s oport %d tport %d\n",
    907   1.4   rmind 	    inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
    908   1.4   rmind 	if (nt->nt_alg) {
    909   1.4   rmind 		printf("\tNAT ALG = %p, ARG = %p\n",
    910   1.4   rmind 		    nt->nt_alg, (void *)nt->nt_alg_arg);
    911   1.1   rmind 	}
    912   1.1   rmind }
    913   1.1   rmind 
    914   1.1   rmind #endif
    915