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