Home | History | Annotate | Line # | Download | only in npf
npf_nat.c revision 1.45
      1   1.1     rmind /*-
      2  1.45     rmind  * Copyright (c) 2014-2018 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.45     rmind  *	a translation IP address or a table and an associated port map.
     55  1.45     rmind  *	The port map is a bitmap used to reserve and use unique TCP/UDP
     56  1.45     rmind  *	ports for translation.  Port maps are unique to the IP addresses,
     57  1.45     rmind  *	therefore multiple NAT policies with the same IP will share the
     58  1.45     rmind  *	same port map.  However, if NAT policy is using the translation
     59  1.45     rmind  *	table, then the port map is shared per table.
     60   1.1     rmind  *
     61  1.29     rmind  * Connections, translation entries and their life-cycle
     62   1.1     rmind  *
     63  1.45     rmind  *	NAT relies on the connection tracking module.  Each translated
     64  1.45     rmind  *	connection has an associated translation entry (npf_nat_t) which
     65   1.4     rmind  *	contains information used for backwards stream translation, i.e.
     66  1.45     rmind  *	the original IP address with port and translation port, allocated
     67  1.45     rmind  *	from the port map.  Each NAT entry is associated with the policy,
     68  1.45     rmind  *	which contains translation IP address.  Allocated port is returned
     69  1.45     rmind  *	to the port map and NAT entry is destroyed when connection expires.
     70   1.1     rmind  */
     71   1.1     rmind 
     72  1.41  christos #ifdef _KERNEL
     73   1.1     rmind #include <sys/cdefs.h>
     74  1.45     rmind __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.45 2019/01/19 21:19:32 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.41  christos #endif
     91   1.1     rmind 
     92   1.1     rmind #include "npf_impl.h"
     93  1.29     rmind #include "npf_conn.h"
     94   1.1     rmind 
     95   1.1     rmind /*
     96   1.1     rmind  * NPF portmap structure.
     97   1.1     rmind  */
     98   1.1     rmind typedef struct {
     99   1.4     rmind 	u_int			p_refcnt;
    100   1.4     rmind 	uint32_t		p_bitmap[0];
    101   1.1     rmind } npf_portmap_t;
    102   1.1     rmind 
    103   1.1     rmind /* Portmap range: [ 1024 .. 65535 ] */
    104   1.4     rmind #define	PORTMAP_FIRST		(1024)
    105   1.4     rmind #define	PORTMAP_SIZE		((65536 - PORTMAP_FIRST) / 32)
    106  1.25     rmind #define	PORTMAP_FILLED		((uint32_t)~0U)
    107   1.4     rmind #define	PORTMAP_MASK		(31)
    108   1.4     rmind #define	PORTMAP_SHIFT		(5)
    109   1.4     rmind 
    110   1.4     rmind #define	PORTMAP_MEM_SIZE	\
    111   1.4     rmind     (sizeof(npf_portmap_t) + (PORTMAP_SIZE * sizeof(uint32_t)))
    112   1.1     rmind 
    113  1.12     rmind /*
    114  1.12     rmind  * NAT policy structure.
    115  1.12     rmind  */
    116   1.1     rmind struct npf_natpolicy {
    117  1.41  christos 	npf_t *			n_npfctx;
    118  1.31     rmind 	kmutex_t		n_lock;
    119   1.4     rmind 	LIST_HEAD(, npf_nat)	n_nat_list;
    120  1.19     rmind 	volatile u_int		n_refcnt;
    121   1.4     rmind 	npf_portmap_t *		n_portmap;
    122  1.31     rmind 	uint64_t		n_id;
    123  1.31     rmind 
    124  1.31     rmind 	/*
    125  1.45     rmind 	 * Translation type, flags, address or table and the port.
    126  1.45     rmind 	 * Additionally, there may be translation algorithm and any
    127  1.45     rmind 	 * auxiliary data, e.g. NPTv6 adjustment value.
    128  1.31     rmind 	 *
    129  1.31     rmind 	 * NPF_NP_CMP_START mark starts here.
    130  1.31     rmind 	 */
    131   1.4     rmind 	int			n_type;
    132  1.45     rmind 	unsigned		n_flags;
    133  1.45     rmind 	unsigned		n_alen;
    134  1.45     rmind 
    135   1.4     rmind 	npf_addr_t		n_taddr;
    136  1.25     rmind 	npf_netmask_t		n_tmask;
    137   1.4     rmind 	in_port_t		n_tport;
    138  1.45     rmind 	unsigned		n_tid;
    139  1.45     rmind 
    140  1.45     rmind 	unsigned		n_algo;
    141  1.25     rmind 	union {
    142  1.45     rmind 		unsigned	n_rr_idx;
    143  1.25     rmind 		uint16_t	n_npt66_adj;
    144  1.25     rmind 	};
    145   1.1     rmind };
    146   1.1     rmind 
    147  1.45     rmind /*
    148  1.45     rmind  * Private flags - must be in the NPF_NAT_PRIVMASK range.
    149  1.45     rmind  */
    150  1.45     rmind #define	NPF_NAT_USETABLE	(0x01000000 & NPF_NAT_PRIVMASK)
    151  1.45     rmind 
    152   1.4     rmind #define	NPF_NP_CMP_START	offsetof(npf_natpolicy_t, n_type)
    153   1.4     rmind #define	NPF_NP_CMP_SIZE		(sizeof(npf_natpolicy_t) - NPF_NP_CMP_START)
    154   1.4     rmind 
    155  1.12     rmind /*
    156  1.29     rmind  * NAT translation entry for a connection.
    157  1.12     rmind  */
    158   1.1     rmind struct npf_nat {
    159  1.28     rmind 	/* Associated NAT policy. */
    160   1.4     rmind 	npf_natpolicy_t *	nt_natpolicy;
    161  1.28     rmind 
    162  1.28     rmind 	/*
    163  1.45     rmind 	 * Translation address as well as the original address which is
    164  1.45     rmind 	 * used for backwards translation.  The same for ports.
    165  1.28     rmind 	 */
    166  1.45     rmind 	npf_addr_t		nt_taddr;
    167   1.4     rmind 	npf_addr_t		nt_oaddr;
    168  1.45     rmind 
    169   1.4     rmind 	in_port_t		nt_oport;
    170   1.4     rmind 	in_port_t		nt_tport;
    171  1.28     rmind 
    172   1.1     rmind 	/* ALG (if any) associated with this NAT entry. */
    173   1.4     rmind 	npf_alg_t *		nt_alg;
    174   1.4     rmind 	uintptr_t		nt_alg_arg;
    175  1.28     rmind 
    176  1.28     rmind 	LIST_ENTRY(npf_nat)	nt_entry;
    177  1.29     rmind 	npf_conn_t *		nt_conn;
    178   1.1     rmind };
    179   1.1     rmind 
    180   1.4     rmind static pool_cache_t		nat_cache	__read_mostly;
    181   1.1     rmind 
    182   1.1     rmind /*
    183   1.1     rmind  * npf_nat_sys{init,fini}: initialise/destroy NAT subsystem structures.
    184   1.1     rmind  */
    185   1.1     rmind 
    186   1.1     rmind void
    187   1.1     rmind npf_nat_sysinit(void)
    188   1.1     rmind {
    189  1.45     rmind 	nat_cache = pool_cache_init(sizeof(npf_nat_t), 0,
    190   1.1     rmind 	    0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
    191   1.1     rmind 	KASSERT(nat_cache != NULL);
    192   1.1     rmind }
    193   1.1     rmind 
    194   1.1     rmind void
    195   1.1     rmind npf_nat_sysfini(void)
    196   1.1     rmind {
    197  1.23     rmind 	/* All NAT policies should already be destroyed. */
    198   1.1     rmind 	pool_cache_destroy(nat_cache);
    199   1.1     rmind }
    200   1.1     rmind 
    201   1.1     rmind /*
    202   1.2     rmind  * npf_nat_newpolicy: create a new NAT policy.
    203   1.1     rmind  *
    204   1.1     rmind  * => Shares portmap if policy is on existing translation address.
    205   1.1     rmind  */
    206   1.1     rmind npf_natpolicy_t *
    207  1.44     rmind npf_nat_newpolicy(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *rset)
    208   1.1     rmind {
    209   1.5     rmind 	npf_natpolicy_t *np;
    210   1.1     rmind 	npf_portmap_t *pm;
    211  1.44     rmind 	const void *addr;
    212  1.44     rmind 	size_t len;
    213   1.1     rmind 
    214   1.1     rmind 	np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
    215  1.41  christos 	np->n_npfctx = npf;
    216   1.4     rmind 
    217  1.33     rmind 	/* The translation type, flags and policy ID. */
    218  1.44     rmind 	np->n_type = dnvlist_get_number(nat, "type", 0);
    219  1.45     rmind 	np->n_flags = dnvlist_get_number(nat, "flags", 0) & ~NPF_NAT_PRIVMASK;
    220  1.44     rmind 	np->n_id = dnvlist_get_number(nat, "nat-policy", 0);
    221  1.10     rmind 
    222  1.10     rmind 	/* Should be exclusively either inbound or outbound NAT. */
    223  1.10     rmind 	if (((np->n_type == NPF_NATIN) ^ (np->n_type == NPF_NATOUT)) == 0) {
    224  1.25     rmind 		goto err;
    225  1.10     rmind 	}
    226  1.10     rmind 	mutex_init(&np->n_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    227  1.10     rmind 	LIST_INIT(&np->n_nat_list);
    228   1.4     rmind 
    229  1.45     rmind 	/*
    230  1.45     rmind 	 * Translation IP, mask and port (if applicable).
    231  1.45     rmind 	 * Alternatively, there may be a translation table ID.
    232  1.45     rmind 	 */
    233  1.45     rmind 	if (nvlist_exists_number(nat, "nat-table-id")) {
    234  1.45     rmind 		if (np->n_flags & NPF_NAT_STATIC) {
    235  1.45     rmind 			goto err;
    236  1.45     rmind 		}
    237  1.45     rmind 		np->n_tid = nvlist_get_number(nat, "nat-table-id");
    238  1.45     rmind 		np->n_tmask = NPF_NO_NETMASK;
    239  1.45     rmind 		np->n_flags |= NPF_NAT_USETABLE;
    240  1.45     rmind 	} else {
    241  1.45     rmind 		addr = dnvlist_get_binary(nat, "nat-ip", &len, NULL, 0);
    242  1.45     rmind 		if (!addr || len == 0 || len > sizeof(npf_addr_t)) {
    243  1.45     rmind 			goto err;
    244  1.45     rmind 		}
    245  1.45     rmind 		memcpy(&np->n_taddr, addr, len);
    246  1.45     rmind 		np->n_alen = len;
    247  1.45     rmind 		np->n_tmask = dnvlist_get_number(nat, "nat-mask", 0);
    248  1.25     rmind 	}
    249  1.44     rmind 	np->n_tport = dnvlist_get_number(nat, "nat-port", 0);
    250   1.4     rmind 
    251  1.45     rmind 	/*
    252  1.45     rmind 	 * NAT algorithm.
    253  1.45     rmind 	 */
    254  1.44     rmind 	np->n_algo = dnvlist_get_number(nat, "nat-algo", 0);
    255  1.25     rmind 	switch (np->n_algo) {
    256  1.25     rmind 	case NPF_ALGO_NPT66:
    257  1.44     rmind 		np->n_npt66_adj = dnvlist_get_number(nat, "npt66-adj", 0);
    258  1.25     rmind 		break;
    259  1.45     rmind 	case NPF_ALGO_NETMAP:
    260  1.45     rmind 		break;
    261  1.45     rmind 	case NPF_ALGO_IPHASH:
    262  1.45     rmind 	case NPF_ALGO_RR:
    263  1.25     rmind 	default:
    264  1.25     rmind 		if (np->n_tmask != NPF_NO_NETMASK)
    265  1.25     rmind 			goto err;
    266  1.25     rmind 		break;
    267  1.25     rmind 	}
    268   1.2     rmind 
    269   1.5     rmind 	/* Determine if port map is needed. */
    270   1.5     rmind 	np->n_portmap = NULL;
    271   1.4     rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) == 0) {
    272   1.5     rmind 		/* No port map. */
    273   1.5     rmind 		return np;
    274   1.2     rmind 	}
    275   1.1     rmind 
    276   1.5     rmind 	/*
    277   1.5     rmind 	 * Inspect NAT policies in the ruleset for port map sharing.
    278   1.5     rmind 	 * Note that npf_ruleset_sharepm() will increase the reference count.
    279   1.5     rmind 	 */
    280  1.31     rmind 	if (!npf_ruleset_sharepm(rset, np)) {
    281   1.1     rmind 		/* Allocate a new port map for the NAT policy. */
    282   1.4     rmind 		pm = kmem_zalloc(PORTMAP_MEM_SIZE, KM_SLEEP);
    283   1.1     rmind 		pm->p_refcnt = 1;
    284   1.1     rmind 		KASSERT((uintptr_t)pm->p_bitmap == (uintptr_t)pm + sizeof(*pm));
    285   1.5     rmind 		np->n_portmap = pm;
    286   1.1     rmind 	} else {
    287   1.5     rmind 		KASSERT(np->n_portmap != NULL);
    288  1.36     rmind 		KASSERT(np->n_portmap->p_refcnt > 0);
    289   1.1     rmind 	}
    290   1.1     rmind 	return np;
    291  1.25     rmind err:
    292  1.39  christos 	mutex_destroy(&np->n_lock);
    293  1.25     rmind 	kmem_free(np, sizeof(npf_natpolicy_t));
    294  1.25     rmind 	return NULL;
    295   1.1     rmind }
    296   1.1     rmind 
    297  1.32     rmind int
    298  1.44     rmind npf_nat_policyexport(const npf_natpolicy_t *np, nvlist_t *nat)
    299  1.32     rmind {
    300  1.45     rmind 	nvlist_add_number(nat, "nat-policy", np->n_id);
    301  1.44     rmind 	nvlist_add_number(nat, "type", np->n_type);
    302  1.44     rmind 	nvlist_add_number(nat, "flags", np->n_flags);
    303  1.32     rmind 
    304  1.45     rmind 	if (np->n_flags & NPF_NAT_USETABLE) {
    305  1.45     rmind 		nvlist_add_number(nat, "nat-table-id", np->n_tid);
    306  1.45     rmind 	} else {
    307  1.45     rmind 		nvlist_add_binary(nat, "nat-ip", &np->n_taddr, np->n_alen);
    308  1.45     rmind 		nvlist_add_number(nat, "nat-mask", np->n_tmask);
    309  1.45     rmind 	}
    310  1.44     rmind 	nvlist_add_number(nat, "nat-port", np->n_tport);
    311  1.44     rmind 	nvlist_add_number(nat, "nat-algo", np->n_algo);
    312  1.32     rmind 
    313  1.32     rmind 	switch (np->n_algo) {
    314  1.32     rmind 	case NPF_ALGO_NPT66:
    315  1.44     rmind 		nvlist_add_number(nat, "npt66-adj", np->n_npt66_adj);
    316  1.32     rmind 		break;
    317  1.32     rmind 	}
    318  1.32     rmind 	return 0;
    319  1.32     rmind }
    320  1.32     rmind 
    321   1.1     rmind /*
    322   1.1     rmind  * npf_nat_freepolicy: free NAT policy and, on last reference, free portmap.
    323   1.1     rmind  *
    324   1.4     rmind  * => Called from npf_rule_free() during the reload via npf_ruleset_destroy().
    325   1.1     rmind  */
    326   1.1     rmind void
    327   1.1     rmind npf_nat_freepolicy(npf_natpolicy_t *np)
    328   1.1     rmind {
    329   1.1     rmind 	npf_portmap_t *pm = np->n_portmap;
    330  1.29     rmind 	npf_conn_t *con;
    331   1.4     rmind 	npf_nat_t *nt;
    332   1.1     rmind 
    333  1.22     rmind 	/*
    334  1.22     rmind 	 * Disassociate all entries from the policy.  At this point,
    335  1.22     rmind 	 * new entries can no longer be created for this policy.
    336  1.22     rmind 	 */
    337  1.28     rmind 	while (np->n_refcnt) {
    338  1.28     rmind 		mutex_enter(&np->n_lock);
    339  1.28     rmind 		LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
    340  1.29     rmind 			con = nt->nt_conn;
    341  1.29     rmind 			KASSERT(con != NULL);
    342  1.29     rmind 			npf_conn_expire(con);
    343  1.28     rmind 		}
    344  1.28     rmind 		mutex_exit(&np->n_lock);
    345   1.4     rmind 
    346  1.28     rmind 		/* Kick the worker - all references should be going away. */
    347  1.41  christos 		npf_worker_signal(np->n_npfctx);
    348  1.19     rmind 		kpause("npfgcnat", false, 1, NULL);
    349  1.19     rmind 	}
    350  1.22     rmind 	KASSERT(LIST_EMPTY(&np->n_nat_list));
    351  1.35     rmind 	KASSERT(pm == NULL || pm->p_refcnt > 0);
    352  1.19     rmind 
    353   1.4     rmind 	/* Destroy the port map, on last reference. */
    354  1.35     rmind 	if (pm && atomic_dec_uint_nv(&pm->p_refcnt) == 0) {
    355   1.2     rmind 		KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
    356   1.4     rmind 		kmem_free(pm, PORTMAP_MEM_SIZE);
    357   1.1     rmind 	}
    358   1.4     rmind 	mutex_destroy(&np->n_lock);
    359   1.1     rmind 	kmem_free(np, sizeof(npf_natpolicy_t));
    360   1.1     rmind }
    361   1.1     rmind 
    362  1.13     rmind void
    363  1.15     rmind npf_nat_freealg(npf_natpolicy_t *np, npf_alg_t *alg)
    364  1.13     rmind {
    365  1.15     rmind 	npf_nat_t *nt;
    366  1.15     rmind 
    367  1.15     rmind 	mutex_enter(&np->n_lock);
    368  1.15     rmind 	LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
    369  1.31     rmind 		if (nt->nt_alg == alg)
    370  1.31     rmind 			nt->nt_alg = NULL;
    371  1.15     rmind 	}
    372  1.15     rmind 	mutex_exit(&np->n_lock);
    373  1.13     rmind }
    374  1.13     rmind 
    375   1.5     rmind /*
    376  1.31     rmind  * npf_nat_cmppolicy: compare two NAT policies.
    377   1.5     rmind  *
    378   1.5     rmind  * => Return 0 on match, and non-zero otherwise.
    379   1.5     rmind  */
    380   1.4     rmind bool
    381  1.31     rmind npf_nat_cmppolicy(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
    382   1.1     rmind {
    383  1.31     rmind 	const void *np_raw, *mnp_raw;
    384  1.31     rmind 
    385   1.4     rmind 	/*
    386   1.4     rmind 	 * Compare the relevant NAT policy information (in raw form),
    387   1.4     rmind 	 * which is enough for matching criterion.
    388   1.4     rmind 	 */
    389   1.5     rmind 	KASSERT(np && mnp && np != mnp);
    390  1.31     rmind 	np_raw = (const uint8_t *)np + NPF_NP_CMP_START;
    391  1.31     rmind 	mnp_raw = (const uint8_t *)mnp + NPF_NP_CMP_START;
    392  1.31     rmind 	return memcmp(np_raw, mnp_raw, NPF_NP_CMP_SIZE) == 0;
    393   1.1     rmind }
    394   1.1     rmind 
    395   1.5     rmind bool
    396   1.5     rmind npf_nat_sharepm(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
    397   1.5     rmind {
    398   1.5     rmind 	npf_portmap_t *pm, *mpm;
    399   1.5     rmind 
    400   1.5     rmind 	KASSERT(np && mnp && np != mnp);
    401  1.37     rmind 	KASSERT(LIST_EMPTY(&mnp->n_nat_list));
    402  1.37     rmind 	KASSERT(mnp->n_refcnt == 0);
    403   1.5     rmind 
    404   1.5     rmind 	/* Using port map and having equal translation address? */
    405   1.5     rmind 	if ((np->n_flags & mnp->n_flags & NPF_NAT_PORTMAP) == 0) {
    406   1.5     rmind 		return false;
    407   1.5     rmind 	}
    408  1.31     rmind 	if (np->n_alen != mnp->n_alen) {
    409   1.5     rmind 		return false;
    410   1.5     rmind 	}
    411  1.45     rmind 	if ((np->n_flags & NPF_NAT_USETABLE) != 0 && np->n_tid != mnp->n_tid) {
    412  1.45     rmind 		return false;
    413  1.45     rmind 	}
    414  1.45     rmind 	if (np->n_alen && memcmp(&np->n_taddr, &mnp->n_taddr, np->n_alen)) {
    415   1.5     rmind 		return false;
    416   1.5     rmind 	}
    417   1.5     rmind 	mpm = mnp->n_portmap;
    418  1.35     rmind 	KASSERT(mpm == NULL || mpm->p_refcnt > 0);
    419  1.35     rmind 
    420  1.35     rmind 	/*
    421  1.35     rmind 	 * If NAT policy has an old port map - drop the reference
    422  1.35     rmind 	 * and destroy the port map if it was the last.
    423  1.35     rmind 	 */
    424  1.35     rmind 	if (mpm && atomic_dec_uint_nv(&mpm->p_refcnt) == 0) {
    425  1.35     rmind 		kmem_free(mpm, PORTMAP_MEM_SIZE);
    426   1.5     rmind 	}
    427  1.35     rmind 
    428   1.5     rmind 	/* Share the port map. */
    429   1.5     rmind 	pm = np->n_portmap;
    430  1.35     rmind 	atomic_inc_uint(&pm->p_refcnt);
    431   1.5     rmind 	mnp->n_portmap = pm;
    432   1.5     rmind 	return true;
    433   1.5     rmind }
    434   1.5     rmind 
    435  1.31     rmind void
    436  1.31     rmind npf_nat_setid(npf_natpolicy_t *np, uint64_t id)
    437  1.31     rmind {
    438  1.31     rmind 	np->n_id = id;
    439  1.31     rmind }
    440  1.31     rmind 
    441  1.31     rmind uint64_t
    442  1.31     rmind npf_nat_getid(const npf_natpolicy_t *np)
    443  1.31     rmind {
    444  1.31     rmind 	return np->n_id;
    445  1.31     rmind }
    446  1.31     rmind 
    447   1.1     rmind /*
    448   1.1     rmind  * npf_nat_getport: allocate and return a port in the NAT policy portmap.
    449   1.1     rmind  *
    450   1.1     rmind  * => Returns in network byte-order.
    451   1.1     rmind  * => Zero indicates failure.
    452   1.1     rmind  */
    453   1.1     rmind static in_port_t
    454   1.1     rmind npf_nat_getport(npf_natpolicy_t *np)
    455   1.1     rmind {
    456   1.1     rmind 	npf_portmap_t *pm = np->n_portmap;
    457   1.1     rmind 	u_int n = PORTMAP_SIZE, idx, bit;
    458   1.1     rmind 	uint32_t map, nmap;
    459   1.1     rmind 
    460  1.36     rmind 	KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
    461  1.36     rmind 	KASSERT(pm->p_refcnt > 0);
    462  1.36     rmind 
    463   1.8       tls 	idx = cprng_fast32() % PORTMAP_SIZE;
    464   1.1     rmind 	for (;;) {
    465   1.1     rmind 		KASSERT(idx < PORTMAP_SIZE);
    466   1.1     rmind 		map = pm->p_bitmap[idx];
    467   1.1     rmind 		if (__predict_false(map == PORTMAP_FILLED)) {
    468   1.1     rmind 			if (n-- == 0) {
    469   1.1     rmind 				/* No space. */
    470   1.1     rmind 				return 0;
    471   1.1     rmind 			}
    472   1.2     rmind 			/* This bitmap is filled, next. */
    473   1.1     rmind 			idx = (idx ? idx : PORTMAP_SIZE) - 1;
    474   1.1     rmind 			continue;
    475   1.1     rmind 		}
    476   1.1     rmind 		bit = ffs32(~map) - 1;
    477   1.1     rmind 		nmap = map | (1 << bit);
    478   1.1     rmind 		if (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map) {
    479   1.1     rmind 			/* Success. */
    480   1.1     rmind 			break;
    481   1.1     rmind 		}
    482   1.1     rmind 	}
    483   1.1     rmind 	return htons(PORTMAP_FIRST + (idx << PORTMAP_SHIFT) + bit);
    484   1.1     rmind }
    485   1.1     rmind 
    486   1.1     rmind /*
    487   1.4     rmind  * npf_nat_takeport: allocate specific port in the NAT policy portmap.
    488   1.4     rmind  */
    489   1.4     rmind static bool
    490   1.4     rmind npf_nat_takeport(npf_natpolicy_t *np, in_port_t port)
    491   1.4     rmind {
    492   1.4     rmind 	npf_portmap_t *pm = np->n_portmap;
    493   1.4     rmind 	uint32_t map, nmap;
    494   1.4     rmind 	u_int idx, bit;
    495   1.4     rmind 
    496  1.36     rmind 	KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
    497  1.36     rmind 	KASSERT(pm->p_refcnt > 0);
    498  1.36     rmind 
    499   1.4     rmind 	port = ntohs(port) - PORTMAP_FIRST;
    500   1.4     rmind 	idx = port >> PORTMAP_SHIFT;
    501   1.4     rmind 	bit = port & PORTMAP_MASK;
    502   1.4     rmind 	map = pm->p_bitmap[idx];
    503   1.4     rmind 	nmap = map | (1 << bit);
    504   1.4     rmind 	if (map == nmap) {
    505   1.4     rmind 		/* Already taken. */
    506   1.4     rmind 		return false;
    507   1.4     rmind 	}
    508   1.4     rmind 	return atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map;
    509   1.4     rmind }
    510   1.4     rmind 
    511   1.4     rmind /*
    512   1.1     rmind  * npf_nat_putport: return port as available in the NAT policy portmap.
    513   1.1     rmind  *
    514   1.1     rmind  * => Port should be in network byte-order.
    515   1.1     rmind  */
    516   1.1     rmind static void
    517   1.1     rmind npf_nat_putport(npf_natpolicy_t *np, in_port_t port)
    518   1.1     rmind {
    519   1.1     rmind 	npf_portmap_t *pm = np->n_portmap;
    520   1.1     rmind 	uint32_t map, nmap;
    521   1.1     rmind 	u_int idx, bit;
    522   1.1     rmind 
    523  1.36     rmind 	KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
    524  1.36     rmind 	KASSERT(pm->p_refcnt > 0);
    525  1.36     rmind 
    526   1.1     rmind 	port = ntohs(port) - PORTMAP_FIRST;
    527   1.1     rmind 	idx = port >> PORTMAP_SHIFT;
    528   1.1     rmind 	bit = port & PORTMAP_MASK;
    529   1.1     rmind 	do {
    530   1.1     rmind 		map = pm->p_bitmap[idx];
    531   1.1     rmind 		KASSERT(map | (1 << bit));
    532   1.1     rmind 		nmap = map & ~(1 << bit);
    533   1.1     rmind 	} while (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) != map);
    534   1.1     rmind }
    535   1.1     rmind 
    536   1.1     rmind /*
    537  1.23     rmind  * npf_nat_which: tell which address (source or destination) should be
    538  1.23     rmind  * rewritten given the combination of the NAT type and flow direction.
    539  1.23     rmind  */
    540  1.23     rmind static inline u_int
    541  1.23     rmind npf_nat_which(const int type, bool forw)
    542  1.23     rmind {
    543  1.23     rmind 	/*
    544  1.23     rmind 	 * Outbound NAT rewrites:
    545  1.24     rmind 	 * - Source (NPF_SRC) on "forwards" stream.
    546  1.24     rmind 	 * - Destination (NPF_DST) on "backwards" stream.
    547  1.23     rmind 	 * Inbound NAT is other way round.
    548  1.23     rmind 	 */
    549  1.23     rmind 	if (type == NPF_NATOUT) {
    550  1.23     rmind 		forw = !forw;
    551  1.23     rmind 	} else {
    552  1.23     rmind 		KASSERT(type == NPF_NATIN);
    553  1.23     rmind 	}
    554  1.23     rmind 	CTASSERT(NPF_SRC == 0 && NPF_DST == 1);
    555  1.24     rmind 	KASSERT(forw == NPF_SRC || forw == NPF_DST);
    556  1.23     rmind 	return (u_int)forw;
    557  1.23     rmind }
    558  1.23     rmind 
    559  1.23     rmind /*
    560   1.2     rmind  * npf_nat_inspect: inspect packet against NAT ruleset and return a policy.
    561  1.19     rmind  *
    562  1.19     rmind  * => Acquire a reference on the policy, if found.
    563   1.2     rmind  */
    564   1.2     rmind static npf_natpolicy_t *
    565  1.30     rmind npf_nat_inspect(npf_cache_t *npc, const int di)
    566   1.2     rmind {
    567  1.19     rmind 	int slock = npf_config_read_enter();
    568  1.41  christos 	npf_ruleset_t *rlset = npf_config_natset(npc->npc_ctx);
    569   1.6     rmind 	npf_natpolicy_t *np;
    570   1.2     rmind 	npf_rule_t *rl;
    571   1.2     rmind 
    572  1.30     rmind 	rl = npf_ruleset_inspect(npc, rlset, di, NPF_LAYER_3);
    573   1.6     rmind 	if (rl == NULL) {
    574  1.19     rmind 		npf_config_read_exit(slock);
    575   1.6     rmind 		return NULL;
    576   1.6     rmind 	}
    577   1.6     rmind 	np = npf_rule_getnat(rl);
    578  1.19     rmind 	atomic_inc_uint(&np->n_refcnt);
    579  1.19     rmind 	npf_config_read_exit(slock);
    580   1.6     rmind 	return np;
    581   1.2     rmind }
    582   1.2     rmind 
    583   1.2     rmind /*
    584   1.2     rmind  * npf_nat_create: create a new NAT translation entry.
    585   1.1     rmind  */
    586   1.2     rmind static npf_nat_t *
    587  1.29     rmind npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np, npf_conn_t *con)
    588   1.1     rmind {
    589  1.19     rmind 	const int proto = npc->npc_proto;
    590  1.45     rmind 	const unsigned alen = npc->npc_alen;
    591  1.45     rmind 	npf_addr_t *taddr;
    592   1.2     rmind 	npf_nat_t *nt;
    593   1.2     rmind 
    594   1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_IP46));
    595   1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_LAYER4));
    596   1.3     rmind 
    597  1.45     rmind 	if (np->n_flags & NPF_NAT_USETABLE) {
    598  1.45     rmind 		npf_tableset_t *ts = npf_config_tableset(np->n_npfctx);
    599  1.45     rmind 		npf_table_t *t = npf_tableset_getbyid(ts, np->n_tid);
    600  1.45     rmind 		unsigned idx;
    601  1.45     rmind 
    602  1.45     rmind 		/*
    603  1.45     rmind 		 * Dynamically select the translation IP address.
    604  1.45     rmind 		 */
    605  1.45     rmind 		switch (np->n_algo) {
    606  1.45     rmind 		case NPF_ALGO_RR:
    607  1.45     rmind 			idx = atomic_inc_uint_nv(&np->n_rr_idx);
    608  1.45     rmind 			break;
    609  1.45     rmind 		case NPF_ALGO_IPHASH:
    610  1.45     rmind 		default:
    611  1.45     rmind 			idx = npf_addr_mix(alen,
    612  1.45     rmind 			    npc->npc_ips[NPF_SRC], npc->npc_ips[NPF_DST]);
    613  1.45     rmind 			break;
    614  1.45     rmind 		}
    615  1.45     rmind 		taddr = npf_table_getsome(t, alen, idx);
    616  1.45     rmind 		if (taddr == NULL) {
    617  1.45     rmind 			return NULL;
    618  1.45     rmind 		}
    619  1.45     rmind 	} else {
    620  1.45     rmind 		/* Static IP address. */
    621  1.45     rmind 		taddr = &np->n_taddr;
    622  1.45     rmind 	}
    623  1.45     rmind 
    624  1.29     rmind 	/* Construct a new NAT entry and associate it with the connection. */
    625   1.2     rmind 	nt = pool_cache_get(nat_cache, PR_NOWAIT);
    626   1.2     rmind 	if (nt == NULL){
    627   1.2     rmind 		return NULL;
    628   1.2     rmind 	}
    629  1.41  christos 	npf_stats_inc(npc->npc_ctx, NPF_STAT_NAT_CREATE);
    630   1.5     rmind 	nt->nt_natpolicy = np;
    631  1.29     rmind 	nt->nt_conn = con;
    632   1.5     rmind 	nt->nt_alg = NULL;
    633   1.5     rmind 
    634  1.45     rmind 	/* Set the translation address. */
    635  1.45     rmind 	memcpy(&nt->nt_taddr, taddr, alen);
    636  1.45     rmind 
    637   1.2     rmind 	/* Save the original address which may be rewritten. */
    638   1.2     rmind 	if (np->n_type == NPF_NATOUT) {
    639  1.23     rmind 		/* Outbound NAT: source (think internal) address. */
    640  1.45     rmind 		memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_SRC], alen);
    641   1.2     rmind 	} else {
    642  1.23     rmind 		/* Inbound NAT: destination (think external) address. */
    643   1.2     rmind 		KASSERT(np->n_type == NPF_NATIN);
    644  1.45     rmind 		memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_DST], alen);
    645   1.2     rmind 	}
    646   1.2     rmind 
    647   1.2     rmind 	/*
    648   1.2     rmind 	 * Port translation, if required, and if it is TCP/UDP.
    649   1.2     rmind 	 */
    650   1.2     rmind 	if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
    651   1.2     rmind 	    (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
    652   1.2     rmind 		nt->nt_oport = 0;
    653   1.2     rmind 		nt->nt_tport = 0;
    654  1.12     rmind 		goto out;
    655   1.2     rmind 	}
    656  1.12     rmind 
    657   1.3     rmind 	/* Save the relevant TCP/UDP port. */
    658   1.3     rmind 	if (proto == IPPROTO_TCP) {
    659  1.18     rmind 		const struct tcphdr *th = npc->npc_l4.tcp;
    660   1.3     rmind 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
    661   1.3     rmind 		    th->th_sport : th->th_dport;
    662   1.2     rmind 	} else {
    663  1.18     rmind 		const struct udphdr *uh = npc->npc_l4.udp;
    664   1.3     rmind 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
    665   1.3     rmind 		    uh->uh_sport : uh->uh_dport;
    666   1.2     rmind 	}
    667   1.3     rmind 
    668   1.2     rmind 	/* Get a new port for translation. */
    669   1.2     rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
    670   1.2     rmind 		nt->nt_tport = npf_nat_getport(np);
    671   1.2     rmind 	} else {
    672   1.2     rmind 		nt->nt_tport = np->n_tport;
    673   1.2     rmind 	}
    674  1.12     rmind out:
    675  1.12     rmind 	mutex_enter(&np->n_lock);
    676  1.12     rmind 	LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
    677  1.12     rmind 	mutex_exit(&np->n_lock);
    678   1.2     rmind 	return nt;
    679   1.2     rmind }
    680   1.2     rmind 
    681   1.2     rmind /*
    682  1.24     rmind  * npf_nat_translate: perform translation given the state data.
    683  1.24     rmind  */
    684  1.26     rmind static inline int
    685  1.30     rmind npf_nat_translate(npf_cache_t *npc, npf_nat_t *nt, bool forw)
    686  1.24     rmind {
    687  1.24     rmind 	const npf_natpolicy_t *np = nt->nt_natpolicy;
    688  1.26     rmind 	const u_int which = npf_nat_which(np->n_type, forw);
    689  1.24     rmind 	const npf_addr_t *addr;
    690  1.24     rmind 	in_port_t port;
    691  1.24     rmind 
    692  1.24     rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    693  1.24     rmind 	KASSERT(npf_iscached(npc, NPC_LAYER4));
    694  1.24     rmind 
    695  1.24     rmind 	if (forw) {
    696  1.24     rmind 		/* "Forwards" stream: use translation address/port. */
    697  1.45     rmind 		addr = &nt->nt_taddr;
    698  1.24     rmind 		port = nt->nt_tport;
    699  1.24     rmind 	} else {
    700  1.24     rmind 		/* "Backwards" stream: use original address/port. */
    701  1.24     rmind 		addr = &nt->nt_oaddr;
    702  1.24     rmind 		port = nt->nt_oport;
    703  1.24     rmind 	}
    704  1.24     rmind 	KASSERT((np->n_flags & NPF_NAT_PORTS) != 0 || port == 0);
    705  1.24     rmind 
    706  1.26     rmind 	/* Execute ALG translation first. */
    707  1.24     rmind 	if ((npc->npc_info & NPC_ALG_EXEC) == 0) {
    708  1.24     rmind 		npc->npc_info |= NPC_ALG_EXEC;
    709  1.30     rmind 		npf_alg_exec(npc, nt, forw);
    710  1.30     rmind 		npf_recache(npc);
    711  1.24     rmind 	}
    712  1.30     rmind 	KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
    713  1.24     rmind 
    714  1.24     rmind 	/* Finally, perform the translation. */
    715  1.26     rmind 	return npf_napt_rwr(npc, which, addr, port);
    716  1.24     rmind }
    717  1.24     rmind 
    718  1.24     rmind /*
    719  1.25     rmind  * npf_nat_algo: perform the translation given the algorithm.
    720  1.25     rmind  */
    721  1.29     rmind static inline int
    722  1.25     rmind npf_nat_algo(npf_cache_t *npc, const npf_natpolicy_t *np, bool forw)
    723  1.25     rmind {
    724  1.26     rmind 	const u_int which = npf_nat_which(np->n_type, forw);
    725  1.45     rmind 	const npf_addr_t *taddr, *orig_addr;
    726  1.45     rmind 	npf_addr_t addr;
    727  1.45     rmind 
    728  1.45     rmind 	KASSERT(np->n_flags & NPF_NAT_STATIC);
    729  1.25     rmind 
    730  1.25     rmind 	switch (np->n_algo) {
    731  1.45     rmind 	case NPF_ALGO_NETMAP:
    732  1.45     rmind 		/*
    733  1.45     rmind 		 * NETMAP:
    734  1.45     rmind 		 *
    735  1.45     rmind 		 *	addr = net-addr | (orig-addr & ~mask)
    736  1.45     rmind 		 */
    737  1.45     rmind 		orig_addr = npc->npc_ips[which];
    738  1.45     rmind 		npf_addr_mask(&np->n_taddr, np->n_tmask, npc->npc_alen, &addr);
    739  1.45     rmind 		npf_addr_bitor(orig_addr, np->n_tmask, npc->npc_alen, &addr);
    740  1.45     rmind 		taddr = &addr;
    741  1.45     rmind 		break;
    742  1.25     rmind 	case NPF_ALGO_NPT66:
    743  1.45     rmind 		return npf_npt66_rwr(npc, which, &np->n_taddr,
    744  1.25     rmind 		    np->n_tmask, np->n_npt66_adj);
    745  1.25     rmind 	default:
    746  1.45     rmind 		taddr = &np->n_taddr;
    747  1.25     rmind 		break;
    748  1.25     rmind 	}
    749  1.45     rmind 	return npf_napt_rwr(npc, which, taddr, np->n_tport);
    750  1.31     rmind }
    751  1.25     rmind 
    752  1.25     rmind /*
    753   1.2     rmind  * npf_do_nat:
    754  1.45     rmind  *
    755  1.29     rmind  *	- Inspect packet for a NAT policy, unless a connection with a NAT
    756   1.4     rmind  *	  association already exists.  In such case, determine whether it
    757   1.2     rmind  *	  is a "forwards" or "backwards" stream.
    758   1.4     rmind  *	- Perform translation: rewrite source or destination fields,
    759   1.4     rmind  *	  depending on translation type and direction.
    760  1.29     rmind  *	- Associate a NAT policy with a connection (may establish a new).
    761   1.2     rmind  */
    762   1.2     rmind int
    763  1.30     rmind npf_do_nat(npf_cache_t *npc, npf_conn_t *con, const int di)
    764   1.2     rmind {
    765  1.30     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    766  1.29     rmind 	npf_conn_t *ncon = NULL;
    767   1.1     rmind 	npf_natpolicy_t *np;
    768   1.1     rmind 	npf_nat_t *nt;
    769   1.1     rmind 	int error;
    770  1.22     rmind 	bool forw;
    771   1.1     rmind 
    772  1.43      maxv 	/* All relevant data should be already cached. */
    773   1.3     rmind 	if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
    774   1.1     rmind 		return 0;
    775   1.1     rmind 	}
    776  1.18     rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    777   1.1     rmind 
    778   1.2     rmind 	/*
    779  1.29     rmind 	 * Return the NAT entry associated with the connection, if any.
    780   1.3     rmind 	 * Determines whether the stream is "forwards" or "backwards".
    781  1.29     rmind 	 * Note: no need to lock, since reference on connection is held.
    782   1.2     rmind 	 */
    783  1.36     rmind 	if (con && (nt = npf_conn_getnat(con, di, &forw)) != NULL) {
    784   1.1     rmind 		np = nt->nt_natpolicy;
    785   1.2     rmind 		goto translate;
    786   1.1     rmind 	}
    787   1.1     rmind 
    788   1.6     rmind 	/*
    789  1.29     rmind 	 * Inspect the packet for a NAT policy, if there is no connection.
    790  1.19     rmind 	 * Note: acquires a reference if found.
    791   1.6     rmind 	 */
    792  1.30     rmind 	np = npf_nat_inspect(npc, di);
    793   1.1     rmind 	if (np == NULL) {
    794   1.1     rmind 		/* If packet does not match - done. */
    795   1.1     rmind 		return 0;
    796   1.1     rmind 	}
    797   1.2     rmind 	forw = true;
    798   1.1     rmind 
    799  1.24     rmind 	/* Static NAT - just perform the translation. */
    800  1.24     rmind 	if (np->n_flags & NPF_NAT_STATIC) {
    801  1.24     rmind 		if (nbuf_cksum_barrier(nbuf, di)) {
    802  1.30     rmind 			npf_recache(npc);
    803  1.24     rmind 		}
    804  1.25     rmind 		error = npf_nat_algo(npc, np, forw);
    805  1.24     rmind 		atomic_dec_uint(&np->n_refcnt);
    806  1.24     rmind 		return error;
    807  1.24     rmind 	}
    808  1.24     rmind 
    809   1.4     rmind 	/*
    810  1.29     rmind 	 * If there is no local connection (no "stateful" rule - unusual,
    811  1.29     rmind 	 * but possible configuration), establish one before translation.
    812  1.29     rmind 	 * Note that it is not a "pass" connection, therefore passing of
    813  1.29     rmind 	 * "backwards" stream depends on other, stateless filtering rules.
    814  1.29     rmind 	 */
    815  1.29     rmind 	if (con == NULL) {
    816  1.30     rmind 		ncon = npf_conn_establish(npc, di, true);
    817  1.29     rmind 		if (ncon == NULL) {
    818  1.22     rmind 			atomic_dec_uint(&np->n_refcnt);
    819  1.22     rmind 			return ENOMEM;
    820   1.1     rmind 		}
    821  1.29     rmind 		con = ncon;
    822   1.1     rmind 	}
    823  1.22     rmind 
    824  1.22     rmind 	/*
    825  1.29     rmind 	 * Create a new NAT entry and associate with the connection.
    826  1.22     rmind 	 * We will consume the reference on success (release on error).
    827  1.22     rmind 	 */
    828  1.29     rmind 	nt = npf_nat_create(npc, np, con);
    829  1.22     rmind 	if (nt == NULL) {
    830  1.22     rmind 		atomic_dec_uint(&np->n_refcnt);
    831  1.22     rmind 		error = ENOMEM;
    832  1.22     rmind 		goto out;
    833  1.22     rmind 	}
    834  1.22     rmind 
    835  1.29     rmind 	/* Associate the NAT translation entry with the connection. */
    836  1.29     rmind 	error = npf_conn_setnat(npc, con, nt, np->n_type);
    837   1.2     rmind 	if (error) {
    838  1.22     rmind 		/* Will release the reference. */
    839  1.22     rmind 		npf_nat_destroy(nt);
    840   1.1     rmind 		goto out;
    841   1.1     rmind 	}
    842   1.1     rmind 
    843  1.22     rmind 	/* Determine whether any ALG matches. */
    844  1.30     rmind 	if (npf_alg_match(npc, nt, di)) {
    845  1.22     rmind 		KASSERT(nt->nt_alg != NULL);
    846  1.22     rmind 	}
    847  1.22     rmind 
    848  1.22     rmind translate:
    849  1.23     rmind 	/* May need to process the delayed checksums first (XXX: NetBSD). */
    850  1.23     rmind 	if (nbuf_cksum_barrier(nbuf, di)) {
    851  1.30     rmind 		npf_recache(npc);
    852  1.23     rmind 	}
    853  1.23     rmind 
    854  1.22     rmind 	/* Perform the translation. */
    855  1.30     rmind 	error = npf_nat_translate(npc, nt, forw);
    856   1.1     rmind out:
    857  1.29     rmind 	if (__predict_false(ncon)) {
    858  1.24     rmind 		if (error) {
    859  1.24     rmind 			/* It created for NAT - just expire. */
    860  1.29     rmind 			npf_conn_expire(ncon);
    861  1.24     rmind 		}
    862  1.29     rmind 		npf_conn_release(ncon);
    863   1.1     rmind 	}
    864   1.1     rmind 	return error;
    865   1.1     rmind }
    866   1.1     rmind 
    867   1.1     rmind /*
    868   1.4     rmind  * npf_nat_gettrans: return translation IP address and port.
    869   1.4     rmind  */
    870   1.4     rmind void
    871   1.4     rmind npf_nat_gettrans(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
    872   1.4     rmind {
    873  1.45     rmind 	*addr = &nt->nt_taddr;
    874   1.4     rmind 	*port = nt->nt_tport;
    875   1.4     rmind }
    876   1.4     rmind 
    877   1.4     rmind /*
    878   1.2     rmind  * npf_nat_getorig: return original IP address and port from translation entry.
    879   1.1     rmind  */
    880   1.1     rmind void
    881   1.3     rmind npf_nat_getorig(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
    882   1.1     rmind {
    883   1.3     rmind 	*addr = &nt->nt_oaddr;
    884   1.2     rmind 	*port = nt->nt_oport;
    885   1.1     rmind }
    886   1.1     rmind 
    887   1.3     rmind /*
    888   1.3     rmind  * npf_nat_setalg: associate an ALG with the NAT entry.
    889   1.3     rmind  */
    890   1.1     rmind void
    891   1.1     rmind npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
    892   1.1     rmind {
    893   1.1     rmind 	nt->nt_alg = alg;
    894   1.1     rmind 	nt->nt_alg_arg = arg;
    895   1.1     rmind }
    896   1.1     rmind 
    897   1.1     rmind /*
    898  1.29     rmind  * npf_nat_destroy: destroy NAT structure (performed on connection expiration).
    899   1.1     rmind  */
    900   1.1     rmind void
    901  1.22     rmind npf_nat_destroy(npf_nat_t *nt)
    902   1.1     rmind {
    903   1.2     rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    904   1.1     rmind 
    905   1.4     rmind 	/* Return any taken port to the portmap. */
    906   1.4     rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
    907   1.1     rmind 		npf_nat_putport(np, nt->nt_tport);
    908   1.1     rmind 	}
    909  1.41  christos 	npf_stats_inc(np->n_npfctx, NPF_STAT_NAT_DESTROY);
    910   1.4     rmind 
    911   1.4     rmind 	mutex_enter(&np->n_lock);
    912   1.4     rmind 	LIST_REMOVE(nt, nt_entry);
    913  1.34     rmind 	KASSERT(np->n_refcnt > 0);
    914  1.19     rmind 	atomic_dec_uint(&np->n_refcnt);
    915   1.4     rmind 	mutex_exit(&np->n_lock);
    916   1.1     rmind 	pool_cache_put(nat_cache, nt);
    917   1.4     rmind }
    918   1.4     rmind 
    919   1.4     rmind /*
    920  1.31     rmind  * npf_nat_export: serialise the NAT entry with a NAT policy ID.
    921   1.4     rmind  */
    922  1.31     rmind void
    923  1.44     rmind npf_nat_export(nvlist_t *condict, npf_nat_t *nt)
    924   1.4     rmind {
    925   1.4     rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    926  1.44     rmind 	nvlist_t *nat;
    927   1.4     rmind 
    928  1.44     rmind 	nat = nvlist_create(0);
    929  1.44     rmind 	nvlist_add_binary(nat, "oaddr", &nt->nt_oaddr, sizeof(npf_addr_t));
    930  1.44     rmind 	nvlist_add_number(nat, "oport", nt->nt_oport);
    931  1.44     rmind 	nvlist_add_number(nat, "tport", nt->nt_tport);
    932  1.44     rmind 	nvlist_add_number(nat, "nat-policy", np->n_id);
    933  1.44     rmind 	nvlist_move_nvlist(condict, "nat", nat);
    934   1.4     rmind }
    935   1.4     rmind 
    936   1.4     rmind /*
    937  1.31     rmind  * npf_nat_import: find the NAT policy and unserialise the NAT entry.
    938   1.4     rmind  */
    939   1.4     rmind npf_nat_t *
    940  1.44     rmind npf_nat_import(npf_t *npf, const nvlist_t *nat,
    941  1.41  christos     npf_ruleset_t *natlist, npf_conn_t *con)
    942   1.4     rmind {
    943   1.4     rmind 	npf_natpolicy_t *np;
    944   1.4     rmind 	npf_nat_t *nt;
    945  1.44     rmind 	const void *oaddr;
    946  1.31     rmind 	uint64_t np_id;
    947  1.44     rmind 	size_t len;
    948   1.4     rmind 
    949  1.44     rmind 	np_id = dnvlist_get_number(nat, "nat-policy", UINT64_MAX);
    950  1.31     rmind 	if ((np = npf_ruleset_findnat(natlist, np_id)) == NULL) {
    951   1.4     rmind 		return NULL;
    952   1.4     rmind 	}
    953  1.31     rmind 	nt = pool_cache_get(nat_cache, PR_WAITOK);
    954  1.31     rmind 	memset(nt, 0, sizeof(npf_nat_t));
    955   1.4     rmind 
    956  1.44     rmind 	oaddr = dnvlist_get_binary(nat, "oaddr", &len, NULL, 0);
    957  1.44     rmind 	if (!oaddr || len != sizeof(npf_addr_t)) {
    958  1.31     rmind 		pool_cache_put(nat_cache, nt);
    959   1.4     rmind 		return NULL;
    960   1.4     rmind 	}
    961  1.44     rmind 	memcpy(&nt->nt_oaddr, oaddr, sizeof(npf_addr_t));
    962  1.44     rmind 	nt->nt_oport = dnvlist_get_number(nat, "oport", 0);
    963  1.44     rmind 	nt->nt_tport = dnvlist_get_number(nat, "tport", 0);
    964   1.4     rmind 
    965   1.4     rmind 	/* Take a specific port from port-map. */
    966  1.42  christos 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport &&
    967  1.36     rmind 	    !npf_nat_takeport(np, nt->nt_tport)) {
    968  1.31     rmind 		pool_cache_put(nat_cache, nt);
    969   1.4     rmind 		return NULL;
    970   1.4     rmind 	}
    971  1.41  christos 	npf_stats_inc(npf, NPF_STAT_NAT_CREATE);
    972   1.4     rmind 
    973  1.34     rmind 	/*
    974  1.34     rmind 	 * Associate, take a reference and insert.  Unlocked since
    975  1.34     rmind 	 * the policy is not yet visible.
    976  1.34     rmind 	 */
    977   1.4     rmind 	nt->nt_natpolicy = np;
    978  1.29     rmind 	nt->nt_conn = con;
    979  1.34     rmind 	np->n_refcnt++;
    980  1.34     rmind 	LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
    981   1.4     rmind 	return nt;
    982   1.1     rmind }
    983   1.1     rmind 
    984   1.1     rmind #if defined(DDB) || defined(_NPF_TESTING)
    985   1.1     rmind 
    986   1.1     rmind void
    987  1.14     rmind npf_nat_dump(const npf_nat_t *nt)
    988   1.1     rmind {
    989  1.14     rmind 	const npf_natpolicy_t *np;
    990   1.1     rmind 	struct in_addr ip;
    991   1.1     rmind 
    992   1.4     rmind 	np = nt->nt_natpolicy;
    993  1.45     rmind 	memcpy(&ip, &nt->nt_taddr, sizeof(ip));
    994  1.38     rmind 	printf("\tNATP(%p): type %d flags 0x%x taddr %s tport %d\n", np,
    995  1.38     rmind 	    np->n_type, np->n_flags, inet_ntoa(ip), ntohs(np->n_tport));
    996   1.4     rmind 	memcpy(&ip, &nt->nt_oaddr, sizeof(ip));
    997   1.4     rmind 	printf("\tNAT: original address %s oport %d tport %d\n",
    998   1.4     rmind 	    inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
    999   1.4     rmind 	if (nt->nt_alg) {
   1000   1.4     rmind 		printf("\tNAT ALG = %p, ARG = %p\n",
   1001   1.4     rmind 		    nt->nt_alg, (void *)nt->nt_alg_arg);
   1002   1.1     rmind 	}
   1003   1.1     rmind }
   1004   1.1     rmind 
   1005   1.1     rmind #endif
   1006