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