Home | History | Annotate | Line # | Download | only in npf
npf_nat.c revision 1.2
      1  1.2  rmind /*	$NetBSD: npf_nat.c,v 1.2 2010/09/16 04:53:27 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*-
      4  1.1  rmind  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1  rmind  * All rights reserved.
      6  1.1  rmind  *
      7  1.1  rmind  * This material is based upon work partially supported by The
      8  1.1  rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  1.1  rmind  *
     10  1.1  rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1  rmind  * modification, are permitted provided that the following conditions
     12  1.1  rmind  * are met:
     13  1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1  rmind  *
     19  1.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  rmind  */
     31  1.1  rmind 
     32  1.1  rmind /*
     33  1.1  rmind  * NPF network address port translation (NAPT).
     34  1.1  rmind  * Described in RFC 2663, RFC 3022.  Commonly just "NAT".
     35  1.1  rmind  *
     36  1.1  rmind  * Overview
     37  1.1  rmind  *
     38  1.1  rmind  *	There are few mechanisms: NAT policy, port map and translation.
     39  1.1  rmind  *	NAT module has a separate ruleset, where rules contain associated
     40  1.1  rmind  *	NAT policy, thus flexible filter criteria can be used.
     41  1.1  rmind  *
     42  1.2  rmind  * Translation types
     43  1.2  rmind  *
     44  1.2  rmind  *	There are two types of translation: outbound (NPF_NATOUT) and
     45  1.2  rmind  *	inbound (NPF_NATIN).  It should not be confused with connection
     46  1.2  rmind  *	direction.
     47  1.2  rmind  *
     48  1.2  rmind  *	Outbound NAT rewrites:
     49  1.2  rmind  *	- Source on "forwards" stream.
     50  1.2  rmind  *	- Destination on "backwards" stream.
     51  1.2  rmind  *	Inbound NAT rewrites:
     52  1.2  rmind  *	- Destination on "forwards" stream.
     53  1.2  rmind  *	- Source on "backwards" stream.
     54  1.2  rmind  *
     55  1.2  rmind  *	It should be noted that bi-directional NAT is a combined outbound
     56  1.2  rmind  *	and inbound translation, therefore constructed as two policies.
     57  1.2  rmind  *
     58  1.1  rmind  * NAT policies and port maps
     59  1.1  rmind  *
     60  1.2  rmind  *	NAT (translation) policy is applied when a packet matches the rule.
     61  1.2  rmind  *	Apart from filter criteria, NAT policy has a translation IP address
     62  1.1  rmind  *	and associated port map.  Port map is a bitmap used to reserve and
     63  1.1  rmind  *	use unique TCP/UDP ports for translation.  Port maps are unique to
     64  1.1  rmind  *	the IP addresses, therefore multiple NAT policies with the same IP
     65  1.1  rmind  *	will share the same port map.
     66  1.1  rmind  *
     67  1.1  rmind  * NAT sessions and translation entries
     68  1.1  rmind  *
     69  1.1  rmind  *	NAT module relies on session management module.  Each "NAT" session
     70  1.2  rmind  *	has an associated translation entry (npf_nat_t).  It contains saved
     71  1.1  rmind  *	i.e. original IP address with port and translation port, allocated
     72  1.1  rmind  *	from the port map.  Each NAT translation entry is associated with
     73  1.1  rmind  *	the policy, which contains translation IP address.  Allocated port
     74  1.1  rmind  *	is returned to the port map and translation entry destroyed when
     75  1.1  rmind  *	"NAT" session expires.
     76  1.1  rmind  */
     77  1.1  rmind 
     78  1.1  rmind #ifdef _KERNEL
     79  1.1  rmind #include <sys/cdefs.h>
     80  1.2  rmind __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.2 2010/09/16 04:53:27 rmind Exp $");
     81  1.1  rmind 
     82  1.1  rmind #include <sys/param.h>
     83  1.1  rmind #include <sys/kernel.h>
     84  1.1  rmind #endif
     85  1.1  rmind 
     86  1.1  rmind #include <sys/atomic.h>
     87  1.1  rmind #include <sys/bitops.h>
     88  1.1  rmind #include <sys/kmem.h>
     89  1.1  rmind #include <sys/pool.h>
     90  1.1  rmind #include <net/pfil.h>
     91  1.1  rmind #include <netinet/in.h>
     92  1.1  rmind 
     93  1.1  rmind #include "npf_impl.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.1  rmind 	u_int				p_refcnt;
    100  1.1  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.1  rmind #define	PORTMAP_FIRST			(1024)
    105  1.1  rmind #define	PORTMAP_SIZE			((65536 - PORTMAP_FIRST) / 32)
    106  1.1  rmind #define	PORTMAP_FILLED			((uint32_t)~0)
    107  1.1  rmind #define	PORTMAP_MASK			(31)
    108  1.1  rmind #define	PORTMAP_SHIFT			(5)
    109  1.1  rmind 
    110  1.1  rmind /* NAT policy structure. */
    111  1.1  rmind struct npf_natpolicy {
    112  1.1  rmind 	LIST_ENTRY(npf_natpolicy)	n_entry;
    113  1.2  rmind 	int				n_type;
    114  1.2  rmind 	int				n_flags;
    115  1.2  rmind 	in_addr_t			n_taddr;
    116  1.2  rmind 	in_port_t			n_tport;
    117  1.1  rmind 	npf_portmap_t *			n_portmap;
    118  1.1  rmind };
    119  1.1  rmind 
    120  1.1  rmind /* NAT translation entry for a session. */
    121  1.1  rmind struct npf_nat {
    122  1.1  rmind 	npf_natpolicy_t *		nt_natpolicy;
    123  1.2  rmind 	/* Original address and port (for backwards translation). */
    124  1.2  rmind 	in_addr_t			nt_oaddr;
    125  1.2  rmind 	in_port_t			nt_oport;
    126  1.2  rmind 	/* Translation port (for redirects). */
    127  1.1  rmind 	in_port_t			nt_tport;
    128  1.1  rmind 	/* ALG (if any) associated with this NAT entry. */
    129  1.1  rmind 	npf_alg_t *			nt_alg;
    130  1.1  rmind 	uintptr_t			nt_alg_arg;
    131  1.1  rmind };
    132  1.1  rmind 
    133  1.2  rmind static npf_ruleset_t *			nat_ruleset	__read_mostly;
    134  1.2  rmind static LIST_HEAD(, npf_natpolicy)	nat_policy_list	__read_mostly;
    135  1.2  rmind static pool_cache_t			nat_cache	__read_mostly;
    136  1.1  rmind 
    137  1.1  rmind /*
    138  1.1  rmind  * npf_nat_sys{init,fini}: initialise/destroy NAT subsystem structures.
    139  1.1  rmind  */
    140  1.1  rmind 
    141  1.1  rmind void
    142  1.1  rmind npf_nat_sysinit(void)
    143  1.1  rmind {
    144  1.1  rmind 
    145  1.1  rmind 	nat_cache = pool_cache_init(sizeof(npf_nat_t), coherency_unit,
    146  1.1  rmind 	    0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
    147  1.1  rmind 	KASSERT(nat_cache != NULL);
    148  1.1  rmind 	nat_ruleset = npf_ruleset_create();
    149  1.1  rmind 	LIST_INIT(&nat_policy_list);
    150  1.1  rmind }
    151  1.1  rmind 
    152  1.1  rmind void
    153  1.1  rmind npf_nat_sysfini(void)
    154  1.1  rmind {
    155  1.1  rmind 
    156  1.1  rmind 	/* Flush NAT policies. */
    157  1.1  rmind 	npf_nat_reload(NULL);
    158  1.1  rmind 	KASSERT(LIST_EMPTY(&nat_policy_list));
    159  1.1  rmind 	pool_cache_destroy(nat_cache);
    160  1.1  rmind }
    161  1.1  rmind 
    162  1.1  rmind /*
    163  1.2  rmind  * npf_nat_newpolicy: create a new NAT policy.
    164  1.1  rmind  *
    165  1.1  rmind  * => Shares portmap if policy is on existing translation address.
    166  1.1  rmind  * => XXX: serialise at upper layer.
    167  1.1  rmind  */
    168  1.1  rmind npf_natpolicy_t *
    169  1.2  rmind npf_nat_newpolicy(int type, int flags, in_addr_t taddr, in_port_t tport)
    170  1.1  rmind {
    171  1.1  rmind 	npf_natpolicy_t *np, *it;
    172  1.1  rmind 	npf_portmap_t *pm;
    173  1.1  rmind 
    174  1.1  rmind 	np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
    175  1.1  rmind 	if (np == NULL) {
    176  1.1  rmind 		return NULL;
    177  1.1  rmind 	}
    178  1.2  rmind 	KASSERT(type == NPF_NATIN || type == NPF_NATOUT);
    179  1.2  rmind 	np->n_type = type;
    180  1.2  rmind 	np->n_flags = flags;
    181  1.2  rmind 	np->n_taddr = taddr;
    182  1.2  rmind 	np->n_tport = tport;
    183  1.2  rmind 
    184  1.2  rmind 	pm = NULL;
    185  1.2  rmind 	if ((flags & NPF_NAT_PORTMAP) == 0) {
    186  1.2  rmind 		goto nopm;
    187  1.2  rmind 	}
    188  1.1  rmind 
    189  1.1  rmind 	/* Search for a NAT policy using the same translation address. */
    190  1.1  rmind 	LIST_FOREACH(it, &nat_policy_list, n_entry) {
    191  1.2  rmind 		if (it->n_taddr != np->n_taddr)
    192  1.1  rmind 			continue;
    193  1.1  rmind 		pm = it->n_portmap;
    194  1.1  rmind 		break;
    195  1.1  rmind 	}
    196  1.1  rmind 	if (pm == NULL) {
    197  1.1  rmind 		/* Allocate a new port map for the NAT policy. */
    198  1.1  rmind 		pm = kmem_zalloc(sizeof(npf_portmap_t) +
    199  1.1  rmind 		    (PORTMAP_SIZE * sizeof(uint32_t)), KM_SLEEP);
    200  1.1  rmind 		if (pm == NULL) {
    201  1.1  rmind 			kmem_free(np, sizeof(npf_natpolicy_t));
    202  1.1  rmind 			return NULL;
    203  1.1  rmind 		}
    204  1.1  rmind 		pm->p_refcnt = 1;
    205  1.1  rmind 		KASSERT((uintptr_t)pm->p_bitmap == (uintptr_t)pm + sizeof(*pm));
    206  1.1  rmind 	} else {
    207  1.1  rmind 		/* Share the port map. */
    208  1.1  rmind 		pm->p_refcnt++;
    209  1.1  rmind 	}
    210  1.2  rmind nopm:
    211  1.1  rmind 	np->n_portmap = pm;
    212  1.1  rmind 	/*
    213  1.1  rmind 	 * Note: old policies with new might co-exist in the list,
    214  1.1  rmind 	 * while reload is in progress, but that is not an issue.
    215  1.1  rmind 	 */
    216  1.1  rmind 	LIST_INSERT_HEAD(&nat_policy_list, np, n_entry);
    217  1.1  rmind 	return np;
    218  1.1  rmind }
    219  1.1  rmind 
    220  1.1  rmind /*
    221  1.1  rmind  * npf_nat_freepolicy: free NAT policy and, on last reference, free portmap.
    222  1.1  rmind  *
    223  1.1  rmind  * => Called from npf_rule_free() during the reload via npf_nat_reload().
    224  1.1  rmind  */
    225  1.1  rmind void
    226  1.1  rmind npf_nat_freepolicy(npf_natpolicy_t *np)
    227  1.1  rmind {
    228  1.1  rmind 	npf_portmap_t *pm = np->n_portmap;
    229  1.1  rmind 
    230  1.1  rmind 	LIST_REMOVE(np, n_entry);
    231  1.2  rmind 	if (pm && --pm->p_refcnt == 0) {
    232  1.2  rmind 		KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
    233  1.1  rmind 		kmem_free(pm, sizeof(npf_portmap_t) +
    234  1.1  rmind 		    (PORTMAP_SIZE * sizeof(uint32_t)));
    235  1.1  rmind 	}
    236  1.1  rmind 	kmem_free(np, sizeof(npf_natpolicy_t));
    237  1.1  rmind }
    238  1.1  rmind 
    239  1.1  rmind /*
    240  1.1  rmind  * npf_nat_reload: activate new ruleset of NAT policies and destroy old.
    241  1.1  rmind  *
    242  1.1  rmind  * => Destruction of ruleset will perform npf_nat_freepolicy() for each policy.
    243  1.1  rmind  */
    244  1.1  rmind void
    245  1.1  rmind npf_nat_reload(npf_ruleset_t *nset)
    246  1.1  rmind {
    247  1.1  rmind 	npf_ruleset_t *oldnset;
    248  1.1  rmind 
    249  1.1  rmind 	oldnset = atomic_swap_ptr(&nat_ruleset, nset);
    250  1.1  rmind 	if (oldnset) {
    251  1.1  rmind 		npf_ruleset_destroy(oldnset);
    252  1.1  rmind 	}
    253  1.1  rmind }
    254  1.1  rmind 
    255  1.1  rmind /*
    256  1.1  rmind  * npf_nat_getport: allocate and return a port in the NAT policy portmap.
    257  1.1  rmind  *
    258  1.1  rmind  * => Returns in network byte-order.
    259  1.1  rmind  * => Zero indicates failure.
    260  1.1  rmind  */
    261  1.1  rmind static in_port_t
    262  1.1  rmind npf_nat_getport(npf_natpolicy_t *np)
    263  1.1  rmind {
    264  1.1  rmind 	npf_portmap_t *pm = np->n_portmap;
    265  1.1  rmind 	u_int n = PORTMAP_SIZE, idx, bit;
    266  1.1  rmind 	uint32_t map, nmap;
    267  1.1  rmind 
    268  1.1  rmind 	idx = arc4random() % PORTMAP_SIZE;
    269  1.1  rmind 	for (;;) {
    270  1.1  rmind 		KASSERT(idx < PORTMAP_SIZE);
    271  1.1  rmind 		map = pm->p_bitmap[idx];
    272  1.1  rmind 		if (__predict_false(map == PORTMAP_FILLED)) {
    273  1.1  rmind 			if (n-- == 0) {
    274  1.1  rmind 				/* No space. */
    275  1.1  rmind 				return 0;
    276  1.1  rmind 			}
    277  1.2  rmind 			/* This bitmap is filled, next. */
    278  1.1  rmind 			idx = (idx ? idx : PORTMAP_SIZE) - 1;
    279  1.1  rmind 			continue;
    280  1.1  rmind 		}
    281  1.1  rmind 		bit = ffs32(~map) - 1;
    282  1.1  rmind 		nmap = map | (1 << bit);
    283  1.1  rmind 		if (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map) {
    284  1.1  rmind 			/* Success. */
    285  1.1  rmind 			break;
    286  1.1  rmind 		}
    287  1.1  rmind 	}
    288  1.1  rmind 	return htons(PORTMAP_FIRST + (idx << PORTMAP_SHIFT) + bit);
    289  1.1  rmind }
    290  1.1  rmind 
    291  1.1  rmind /*
    292  1.1  rmind  * npf_nat_putport: return port as available in the NAT policy portmap.
    293  1.1  rmind  *
    294  1.1  rmind  * => Port should be in network byte-order.
    295  1.1  rmind  */
    296  1.1  rmind static void
    297  1.1  rmind npf_nat_putport(npf_natpolicy_t *np, in_port_t port)
    298  1.1  rmind {
    299  1.1  rmind 	npf_portmap_t *pm = np->n_portmap;
    300  1.1  rmind 	uint32_t map, nmap;
    301  1.1  rmind 	u_int idx, bit;
    302  1.1  rmind 
    303  1.1  rmind 	port = ntohs(port) - PORTMAP_FIRST;
    304  1.1  rmind 	idx = port >> PORTMAP_SHIFT;
    305  1.1  rmind 	bit = port & PORTMAP_MASK;
    306  1.1  rmind 	do {
    307  1.1  rmind 		map = pm->p_bitmap[idx];
    308  1.1  rmind 		KASSERT(map | (1 << bit));
    309  1.1  rmind 		nmap = map & ~(1 << bit);
    310  1.1  rmind 	} while (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) != map);
    311  1.1  rmind }
    312  1.1  rmind 
    313  1.1  rmind /*
    314  1.2  rmind  * npf_nat_inspect: inspect packet against NAT ruleset and return a policy.
    315  1.2  rmind  */
    316  1.2  rmind static npf_natpolicy_t *
    317  1.2  rmind npf_nat_inspect(npf_cache_t *npc, nbuf_t *nbuf, struct ifnet *ifp, const int di)
    318  1.2  rmind {
    319  1.2  rmind 	npf_rule_t *rl;
    320  1.2  rmind 
    321  1.2  rmind 	rl = npf_ruleset_match(nat_ruleset, npc, nbuf, ifp, di, NPF_LAYER_3);
    322  1.2  rmind 
    323  1.2  rmind 	return rl ? npf_rule_getnat(rl) : NULL;
    324  1.2  rmind }
    325  1.2  rmind 
    326  1.2  rmind /*
    327  1.2  rmind  * npf_nat_create: create a new NAT translation entry.
    328  1.1  rmind  */
    329  1.2  rmind static npf_nat_t *
    330  1.2  rmind npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np)
    331  1.1  rmind {
    332  1.1  rmind 	const int proto = npc->npc_proto;
    333  1.2  rmind 	npf_nat_t *nt;
    334  1.2  rmind 
    335  1.2  rmind 	/* New NAT association. */
    336  1.2  rmind 	nt = pool_cache_get(nat_cache, PR_NOWAIT);
    337  1.2  rmind 	if (nt == NULL){
    338  1.2  rmind 		return NULL;
    339  1.2  rmind 	}
    340  1.2  rmind 	nt->nt_natpolicy = np;
    341  1.2  rmind 	nt->nt_alg = NULL;
    342  1.2  rmind 
    343  1.2  rmind 	/* Save the original address which may be rewritten. */
    344  1.2  rmind 	if (np->n_type == NPF_NATOUT) {
    345  1.2  rmind 		/* Source (local) for Outbound NAT. */
    346  1.2  rmind 		nt->nt_oaddr = npc->npc_srcip;
    347  1.2  rmind 	} else {
    348  1.2  rmind 		/* Destination (external) for Inbound NAT. */
    349  1.2  rmind 		KASSERT(np->n_type == NPF_NATIN);
    350  1.2  rmind 		nt->nt_oaddr = npc->npc_dstip;
    351  1.2  rmind 	}
    352  1.2  rmind 
    353  1.2  rmind 	/*
    354  1.2  rmind 	 * Port translation, if required, and if it is TCP/UDP.
    355  1.2  rmind 	 */
    356  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
    357  1.2  rmind 	    (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
    358  1.2  rmind 		nt->nt_oport = 0;
    359  1.2  rmind 		nt->nt_tport = 0;
    360  1.2  rmind 		return nt;
    361  1.2  rmind 	}
    362  1.2  rmind 	/* Save a relevant TCP/UDP port. */
    363  1.2  rmind 	KASSERT(npf_iscached(npc, NPC_PORTS));
    364  1.2  rmind 	if (np->n_type == NPF_NATOUT) {
    365  1.2  rmind 		nt->nt_oport = npc->npc_sport;
    366  1.2  rmind 	} else {
    367  1.2  rmind 		nt->nt_oport = npc->npc_dport;
    368  1.2  rmind 	}
    369  1.2  rmind 	/* Get a new port for translation. */
    370  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
    371  1.2  rmind 		nt->nt_tport = npf_nat_getport(np);
    372  1.2  rmind 	} else {
    373  1.2  rmind 		nt->nt_tport = np->n_tport;
    374  1.2  rmind 	}
    375  1.2  rmind 	return nt;
    376  1.2  rmind }
    377  1.2  rmind 
    378  1.2  rmind /*
    379  1.2  rmind  * npf_nat_translate: perform address and/or port translation.
    380  1.2  rmind  */
    381  1.2  rmind static int
    382  1.2  rmind npf_nat_translate(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt,
    383  1.2  rmind     const bool forw, const int di)
    384  1.2  rmind {
    385  1.2  rmind 	const npf_natpolicy_t *np = nt->nt_natpolicy;
    386  1.1  rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    387  1.2  rmind 	in_addr_t addr;
    388  1.2  rmind 	in_port_t port;
    389  1.2  rmind 
    390  1.2  rmind 	KASSERT(npf_iscached(npc, NPC_IP46 | NPC_ADDRS));
    391  1.2  rmind 
    392  1.2  rmind 	if (forw) {
    393  1.2  rmind 		/* "Forwards" stream: use translation address/port. */
    394  1.2  rmind 		KASSERT(
    395  1.2  rmind 		    (np->n_type == NPF_NATIN && di == PFIL_IN) ^
    396  1.2  rmind 		    (np->n_type == NPF_NATOUT && di == PFIL_OUT)
    397  1.2  rmind 		);
    398  1.2  rmind 		addr = np->n_taddr;
    399  1.2  rmind 		port = nt->nt_tport;
    400  1.2  rmind 	} else {
    401  1.2  rmind 		/* "Backwards" stream: use original address/port. */
    402  1.2  rmind 		KASSERT(
    403  1.2  rmind 		    (np->n_type == NPF_NATIN && di == PFIL_OUT) ^
    404  1.2  rmind 		    (np->n_type == NPF_NATOUT && di == PFIL_IN)
    405  1.2  rmind 		);
    406  1.2  rmind 		addr = nt->nt_oaddr;
    407  1.2  rmind 		port = nt->nt_oport;
    408  1.2  rmind 	}
    409  1.2  rmind 
    410  1.2  rmind 	/* Execute ALG hooks first. */
    411  1.2  rmind 	npf_alg_exec(npc, nbuf, nt, di);
    412  1.2  rmind 
    413  1.2  rmind 	/*
    414  1.2  rmind 	 * Address translation: rewrite source/destination address, depending
    415  1.2  rmind 	 * on direction (PFIL_OUT - for source, PFIL_IN - for destination).
    416  1.2  rmind 	 * Note: cache will be used in npf_rwrport(), update only in the end.
    417  1.2  rmind 	 */
    418  1.2  rmind 	if (!npf_rwrip(npc, nbuf, n_ptr, di, addr)) {
    419  1.2  rmind 		return EINVAL;
    420  1.2  rmind 	}
    421  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTS) == 0) {
    422  1.2  rmind 		/* Cache new address. */
    423  1.2  rmind 		if (di == PFIL_OUT) {
    424  1.2  rmind 			npc->npc_srcip = addr;
    425  1.2  rmind 		} else {
    426  1.2  rmind 			npc->npc_dstip = addr;
    427  1.2  rmind 		}
    428  1.2  rmind 		return 0;
    429  1.2  rmind 	}
    430  1.2  rmind 	switch (npc->npc_proto) {
    431  1.2  rmind 	case IPPROTO_TCP:
    432  1.2  rmind 	case IPPROTO_UDP:
    433  1.2  rmind 		KASSERT(npf_iscached(npc, NPC_PORTS));
    434  1.2  rmind 		/* Rewrite source/destination port. */
    435  1.2  rmind 		if (!npf_rwrport(npc, nbuf, n_ptr, di, port, addr)) {
    436  1.2  rmind 			return EINVAL;
    437  1.2  rmind 		}
    438  1.2  rmind 		break;
    439  1.2  rmind 	case IPPROTO_ICMP:
    440  1.2  rmind 		/* None. */
    441  1.2  rmind 		break;
    442  1.2  rmind 	default:
    443  1.2  rmind 		return ENOTSUP;
    444  1.2  rmind 	}
    445  1.2  rmind 	/* Cache new address and port. */
    446  1.2  rmind 	if (di == PFIL_OUT) {
    447  1.2  rmind 		npc->npc_srcip = addr;
    448  1.2  rmind 		npc->npc_sport = port;
    449  1.2  rmind 	} else {
    450  1.2  rmind 		npc->npc_dstip = addr;
    451  1.2  rmind 		npc->npc_dport = port;
    452  1.2  rmind 	}
    453  1.2  rmind 	return 0;
    454  1.2  rmind }
    455  1.2  rmind 
    456  1.2  rmind /*
    457  1.2  rmind  * npf_do_nat:
    458  1.2  rmind  *	- Inspect packet for a NAT policy, unless a session with a NAT
    459  1.2  rmind  *	  association already exists.  In such case, determine whether is
    460  1.2  rmind  *	  is a "forwards" or "backwards" stream.
    461  1.2  rmind  *	- Perform translation: rewrite source address if "forwards" stream
    462  1.2  rmind  *	  and destination address if "backwards".
    463  1.2  rmind  *	- Establish sessions or, if already exists, associate a NAT policy.
    464  1.2  rmind  */
    465  1.2  rmind int
    466  1.2  rmind npf_do_nat(npf_cache_t *npc, npf_session_t *se, nbuf_t *nbuf,
    467  1.2  rmind     struct ifnet *ifp, const int di)
    468  1.2  rmind {
    469  1.2  rmind 	npf_session_t *nse = NULL;
    470  1.1  rmind 	npf_natpolicy_t *np;
    471  1.1  rmind 	npf_nat_t *nt;
    472  1.1  rmind 	int error;
    473  1.2  rmind 	bool forw, new;
    474  1.1  rmind 
    475  1.1  rmind 	/* All relevant IPv4 data should be already cached. */
    476  1.1  rmind 	if (!npf_iscached(npc, NPC_IP46 | NPC_ADDRS)) {
    477  1.1  rmind 		return 0;
    478  1.1  rmind 	}
    479  1.1  rmind 
    480  1.2  rmind 	/*
    481  1.2  rmind 	 * Return the NAT entry associated with the session, if any.
    482  1.2  rmind 	 * Assumptions:
    483  1.2  rmind 	 * - If associated via linked session, then "forwards" stream.
    484  1.2  rmind 	 * - If associated directly, then "backwards" stream.
    485  1.2  rmind 	 */
    486  1.2  rmind 	if (se && (nt = npf_session_retnat(se, di, &forw)) != NULL) {
    487  1.1  rmind 		np = nt->nt_natpolicy;
    488  1.1  rmind 		new = false;
    489  1.2  rmind 		goto translate;
    490  1.1  rmind 	}
    491  1.1  rmind 
    492  1.2  rmind 	/* Inspect the packet for a NAT policy, if there is no session. */
    493  1.2  rmind 	np = npf_nat_inspect(npc, nbuf, ifp, di);
    494  1.1  rmind 	if (np == NULL) {
    495  1.1  rmind 		/* If packet does not match - done. */
    496  1.1  rmind 		return 0;
    497  1.1  rmind 	}
    498  1.2  rmind 	forw = true;
    499  1.1  rmind 
    500  1.2  rmind 	/* Create a new NAT translation entry. */
    501  1.2  rmind 	nt = npf_nat_create(npc, np);
    502  1.2  rmind 	if (nt == NULL) {
    503  1.1  rmind 		return ENOMEM;
    504  1.1  rmind 	}
    505  1.1  rmind 	new = true;
    506  1.1  rmind 
    507  1.2  rmind 	/*
    508  1.2  rmind 	 * If there is no local session (no "keep state" rule - unusual, but
    509  1.2  rmind 	 * possible configuration), establish one before translation.  Note
    510  1.2  rmind 	 * that it is not a "pass" session, therefore passing of "backwards"
    511  1.2  rmind 	 * stream depends on other, stateless filtering rules.
    512  1.2  rmind 	 */
    513  1.1  rmind 	if (se == NULL) {
    514  1.2  rmind 		nse = npf_session_establish(npc, NULL, di);
    515  1.1  rmind 		if (nse == NULL) {
    516  1.1  rmind 			error = ENOMEM;
    517  1.1  rmind 			goto out;
    518  1.1  rmind 		}
    519  1.1  rmind 		se = nse;
    520  1.1  rmind 	}
    521  1.2  rmind translate:
    522  1.2  rmind 	/* Perform the translation. */
    523  1.2  rmind 	error = npf_nat_translate(npc, nbuf, nt, forw, di);
    524  1.2  rmind 	if (error) {
    525  1.1  rmind 		goto out;
    526  1.1  rmind 	}
    527  1.1  rmind 
    528  1.1  rmind 	if (__predict_false(new)) {
    529  1.1  rmind 		npf_session_t *natse;
    530  1.1  rmind 		/*
    531  1.1  rmind 		 * Establish a new NAT session using translated address and
    532  1.1  rmind 		 * associate NAT translation data with this session.
    533  1.1  rmind 		 *
    534  1.1  rmind 		 * Note: packet now has a translated address in the cache.
    535  1.1  rmind 		 */
    536  1.2  rmind 		natse = npf_session_establish(npc, nt, di);
    537  1.1  rmind 		if (natse == NULL) {
    538  1.1  rmind 			error = ENOMEM;
    539  1.1  rmind 			goto out;
    540  1.1  rmind 		}
    541  1.1  rmind 		/*
    542  1.1  rmind 		 * Link local session with NAT session, if no link already.
    543  1.1  rmind 		 */
    544  1.1  rmind 		npf_session_link(se, natse);
    545  1.1  rmind 		npf_session_release(natse);
    546  1.1  rmind out:
    547  1.1  rmind 		if (error) {
    548  1.1  rmind 			if (nse != NULL) {
    549  1.2  rmind 				/* XXX: Expire it?? */
    550  1.1  rmind 			}
    551  1.1  rmind 			/* Will free the structure and return the port. */
    552  1.1  rmind 			npf_nat_expire(nt);
    553  1.1  rmind 		}
    554  1.1  rmind 		if (nse != NULL) {
    555  1.1  rmind 			npf_session_release(nse);
    556  1.1  rmind 		}
    557  1.1  rmind 	}
    558  1.1  rmind 	return error;
    559  1.1  rmind }
    560  1.1  rmind 
    561  1.1  rmind /*
    562  1.2  rmind  * npf_nat_getorig: return original IP address and port from translation entry.
    563  1.1  rmind  */
    564  1.1  rmind void
    565  1.2  rmind npf_nat_getorig(npf_nat_t *nt, in_addr_t *addr, in_port_t *port)
    566  1.1  rmind {
    567  1.1  rmind 
    568  1.2  rmind 	*addr = nt->nt_oaddr;
    569  1.2  rmind 	*port = nt->nt_oport;
    570  1.1  rmind }
    571  1.1  rmind 
    572  1.1  rmind void
    573  1.1  rmind npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
    574  1.1  rmind {
    575  1.1  rmind 
    576  1.1  rmind 	nt->nt_alg = alg;
    577  1.1  rmind 	nt->nt_alg_arg = arg;
    578  1.1  rmind }
    579  1.1  rmind 
    580  1.1  rmind /*
    581  1.1  rmind  * npf_nat_expire: free NAT-related data structures on session expiration.
    582  1.1  rmind  */
    583  1.1  rmind void
    584  1.1  rmind npf_nat_expire(npf_nat_t *nt)
    585  1.1  rmind {
    586  1.2  rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    587  1.1  rmind 
    588  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
    589  1.2  rmind 		KASSERT(nt->nt_tport != 0);
    590  1.1  rmind 		npf_nat_putport(np, nt->nt_tport);
    591  1.1  rmind 	}
    592  1.1  rmind 	pool_cache_put(nat_cache, nt);
    593  1.1  rmind }
    594  1.1  rmind 
    595  1.1  rmind #if defined(DDB) || defined(_NPF_TESTING)
    596  1.1  rmind 
    597  1.1  rmind void
    598  1.1  rmind npf_nat_dump(npf_nat_t *nt)
    599  1.1  rmind {
    600  1.1  rmind 	npf_natpolicy_t *np;
    601  1.1  rmind 	struct in_addr ip;
    602  1.1  rmind 
    603  1.1  rmind 	if (nt) {
    604  1.1  rmind 		np = nt->nt_natpolicy;
    605  1.1  rmind 		goto skip;
    606  1.1  rmind 	}
    607  1.1  rmind 	LIST_FOREACH(np, &nat_policy_list, n_entry) {
    608  1.1  rmind skip:
    609  1.2  rmind 		ip.s_addr = np->n_taddr;
    610  1.2  rmind 		printf("\tNAT policy: type = %d, flags = %d, taddr = %s\n",
    611  1.2  rmind 		    np->n_type, np->n_flags, inet_ntoa(ip));
    612  1.1  rmind 		if (nt == NULL) {
    613  1.1  rmind 			continue;
    614  1.1  rmind 		}
    615  1.2  rmind 		ip.s_addr = nt->nt_oaddr;
    616  1.2  rmind 		printf("\tNAT: original address %s, oport %d, tport = %d\n",
    617  1.2  rmind 		    inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
    618  1.1  rmind 		if (nt->nt_alg) {
    619  1.1  rmind 			printf("\tNAT ALG = %p, ARG = %p\n",
    620  1.1  rmind 			    nt->nt_alg, (void *)nt->nt_alg_arg);
    621  1.1  rmind 		}
    622  1.1  rmind 		return;
    623  1.1  rmind 	}
    624  1.1  rmind }
    625  1.1  rmind 
    626  1.1  rmind #endif
    627