Home | History | Annotate | Line # | Download | only in npf
npf_nat.c revision 1.3
      1  1.3  rmind /*	$NetBSD: npf_nat.c,v 1.3 2010/11/11 06:30:39 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 #include <sys/cdefs.h>
     79  1.3  rmind __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.3 2010/11/11 06:30:39 rmind Exp $");
     80  1.1  rmind 
     81  1.1  rmind #include <sys/param.h>
     82  1.1  rmind #include <sys/kernel.h>
     83  1.1  rmind 
     84  1.1  rmind #include <sys/atomic.h>
     85  1.1  rmind #include <sys/bitops.h>
     86  1.1  rmind #include <sys/kmem.h>
     87  1.1  rmind #include <sys/pool.h>
     88  1.1  rmind #include <net/pfil.h>
     89  1.1  rmind #include <netinet/in.h>
     90  1.1  rmind 
     91  1.1  rmind #include "npf_impl.h"
     92  1.1  rmind 
     93  1.1  rmind /*
     94  1.1  rmind  * NPF portmap structure.
     95  1.1  rmind  */
     96  1.1  rmind typedef struct {
     97  1.1  rmind 	u_int				p_refcnt;
     98  1.1  rmind 	uint32_t			p_bitmap[0];
     99  1.1  rmind } npf_portmap_t;
    100  1.1  rmind 
    101  1.1  rmind /* Portmap range: [ 1024 .. 65535 ] */
    102  1.1  rmind #define	PORTMAP_FIRST			(1024)
    103  1.1  rmind #define	PORTMAP_SIZE			((65536 - PORTMAP_FIRST) / 32)
    104  1.1  rmind #define	PORTMAP_FILLED			((uint32_t)~0)
    105  1.1  rmind #define	PORTMAP_MASK			(31)
    106  1.1  rmind #define	PORTMAP_SHIFT			(5)
    107  1.1  rmind 
    108  1.1  rmind /* NAT policy structure. */
    109  1.1  rmind struct npf_natpolicy {
    110  1.1  rmind 	LIST_ENTRY(npf_natpolicy)	n_entry;
    111  1.2  rmind 	int				n_type;
    112  1.2  rmind 	int				n_flags;
    113  1.3  rmind 	npf_portmap_t *			n_portmap;
    114  1.3  rmind 	size_t				n_addr_sz;
    115  1.3  rmind 	npf_addr_t			n_taddr;
    116  1.2  rmind 	in_port_t			n_tport;
    117  1.1  rmind };
    118  1.1  rmind 
    119  1.1  rmind /* NAT translation entry for a session. */
    120  1.1  rmind struct npf_nat {
    121  1.1  rmind 	npf_natpolicy_t *		nt_natpolicy;
    122  1.2  rmind 	/* Original address and port (for backwards translation). */
    123  1.3  rmind 	npf_addr_t			nt_oaddr;
    124  1.2  rmind 	in_port_t			nt_oport;
    125  1.2  rmind 	/* Translation port (for redirects). */
    126  1.1  rmind 	in_port_t			nt_tport;
    127  1.1  rmind 	/* ALG (if any) associated with this NAT entry. */
    128  1.1  rmind 	npf_alg_t *			nt_alg;
    129  1.1  rmind 	uintptr_t			nt_alg_arg;
    130  1.1  rmind };
    131  1.1  rmind 
    132  1.2  rmind static npf_ruleset_t *			nat_ruleset	__read_mostly;
    133  1.2  rmind static LIST_HEAD(, npf_natpolicy)	nat_policy_list	__read_mostly;
    134  1.2  rmind static pool_cache_t			nat_cache	__read_mostly;
    135  1.1  rmind 
    136  1.1  rmind /*
    137  1.1  rmind  * npf_nat_sys{init,fini}: initialise/destroy NAT subsystem structures.
    138  1.1  rmind  */
    139  1.1  rmind 
    140  1.1  rmind void
    141  1.1  rmind npf_nat_sysinit(void)
    142  1.1  rmind {
    143  1.1  rmind 
    144  1.1  rmind 	nat_cache = pool_cache_init(sizeof(npf_nat_t), coherency_unit,
    145  1.1  rmind 	    0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
    146  1.1  rmind 	KASSERT(nat_cache != NULL);
    147  1.1  rmind 	nat_ruleset = npf_ruleset_create();
    148  1.1  rmind 	LIST_INIT(&nat_policy_list);
    149  1.1  rmind }
    150  1.1  rmind 
    151  1.1  rmind void
    152  1.1  rmind npf_nat_sysfini(void)
    153  1.1  rmind {
    154  1.1  rmind 
    155  1.1  rmind 	/* Flush NAT policies. */
    156  1.1  rmind 	npf_nat_reload(NULL);
    157  1.1  rmind 	KASSERT(LIST_EMPTY(&nat_policy_list));
    158  1.1  rmind 	pool_cache_destroy(nat_cache);
    159  1.1  rmind }
    160  1.1  rmind 
    161  1.1  rmind /*
    162  1.2  rmind  * npf_nat_newpolicy: create a new NAT policy.
    163  1.1  rmind  *
    164  1.1  rmind  * => Shares portmap if policy is on existing translation address.
    165  1.1  rmind  * => XXX: serialise at upper layer.
    166  1.1  rmind  */
    167  1.1  rmind npf_natpolicy_t *
    168  1.3  rmind npf_nat_newpolicy(int type, int flags, const npf_addr_t *taddr,
    169  1.3  rmind     size_t addr_sz, 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.3  rmind 	np->n_addr_sz = addr_sz;
    182  1.3  rmind 	memcpy(&np->n_taddr, taddr, sizeof(npf_addr_t));
    183  1.2  rmind 	np->n_tport = tport;
    184  1.2  rmind 
    185  1.2  rmind 	pm = NULL;
    186  1.2  rmind 	if ((flags & NPF_NAT_PORTMAP) == 0) {
    187  1.2  rmind 		goto nopm;
    188  1.2  rmind 	}
    189  1.1  rmind 
    190  1.1  rmind 	/* Search for a NAT policy using the same translation address. */
    191  1.1  rmind 	LIST_FOREACH(it, &nat_policy_list, n_entry) {
    192  1.3  rmind 		if (memcmp(&it->n_taddr, &np->n_taddr, sizeof(npf_addr_t))) {
    193  1.1  rmind 			continue;
    194  1.3  rmind 		}
    195  1.1  rmind 		pm = it->n_portmap;
    196  1.1  rmind 		break;
    197  1.1  rmind 	}
    198  1.1  rmind 	if (pm == NULL) {
    199  1.1  rmind 		/* Allocate a new port map for the NAT policy. */
    200  1.1  rmind 		pm = kmem_zalloc(sizeof(npf_portmap_t) +
    201  1.1  rmind 		    (PORTMAP_SIZE * sizeof(uint32_t)), KM_SLEEP);
    202  1.1  rmind 		if (pm == NULL) {
    203  1.1  rmind 			kmem_free(np, sizeof(npf_natpolicy_t));
    204  1.1  rmind 			return NULL;
    205  1.1  rmind 		}
    206  1.1  rmind 		pm->p_refcnt = 1;
    207  1.1  rmind 		KASSERT((uintptr_t)pm->p_bitmap == (uintptr_t)pm + sizeof(*pm));
    208  1.1  rmind 	} else {
    209  1.1  rmind 		/* Share the port map. */
    210  1.1  rmind 		pm->p_refcnt++;
    211  1.1  rmind 	}
    212  1.2  rmind nopm:
    213  1.1  rmind 	np->n_portmap = pm;
    214  1.1  rmind 	/*
    215  1.1  rmind 	 * Note: old policies with new might co-exist in the list,
    216  1.1  rmind 	 * while reload is in progress, but that is not an issue.
    217  1.1  rmind 	 */
    218  1.1  rmind 	LIST_INSERT_HEAD(&nat_policy_list, np, n_entry);
    219  1.1  rmind 	return np;
    220  1.1  rmind }
    221  1.1  rmind 
    222  1.1  rmind /*
    223  1.1  rmind  * npf_nat_freepolicy: free NAT policy and, on last reference, free portmap.
    224  1.1  rmind  *
    225  1.1  rmind  * => Called from npf_rule_free() during the reload via npf_nat_reload().
    226  1.1  rmind  */
    227  1.1  rmind void
    228  1.1  rmind npf_nat_freepolicy(npf_natpolicy_t *np)
    229  1.1  rmind {
    230  1.1  rmind 	npf_portmap_t *pm = np->n_portmap;
    231  1.1  rmind 
    232  1.1  rmind 	LIST_REMOVE(np, n_entry);
    233  1.2  rmind 	if (pm && --pm->p_refcnt == 0) {
    234  1.2  rmind 		KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
    235  1.1  rmind 		kmem_free(pm, sizeof(npf_portmap_t) +
    236  1.1  rmind 		    (PORTMAP_SIZE * sizeof(uint32_t)));
    237  1.1  rmind 	}
    238  1.1  rmind 	kmem_free(np, sizeof(npf_natpolicy_t));
    239  1.1  rmind }
    240  1.1  rmind 
    241  1.1  rmind /*
    242  1.1  rmind  * npf_nat_reload: activate new ruleset of NAT policies and destroy old.
    243  1.1  rmind  *
    244  1.1  rmind  * => Destruction of ruleset will perform npf_nat_freepolicy() for each policy.
    245  1.1  rmind  */
    246  1.1  rmind void
    247  1.1  rmind npf_nat_reload(npf_ruleset_t *nset)
    248  1.1  rmind {
    249  1.1  rmind 	npf_ruleset_t *oldnset;
    250  1.1  rmind 
    251  1.1  rmind 	oldnset = atomic_swap_ptr(&nat_ruleset, nset);
    252  1.3  rmind 	KASSERT(oldnset != NULL);
    253  1.3  rmind 	npf_ruleset_destroy(oldnset);
    254  1.1  rmind }
    255  1.1  rmind 
    256  1.1  rmind /*
    257  1.1  rmind  * npf_nat_getport: allocate and return a port in the NAT policy portmap.
    258  1.1  rmind  *
    259  1.1  rmind  * => Returns in network byte-order.
    260  1.1  rmind  * => Zero indicates failure.
    261  1.1  rmind  */
    262  1.1  rmind static in_port_t
    263  1.1  rmind npf_nat_getport(npf_natpolicy_t *np)
    264  1.1  rmind {
    265  1.1  rmind 	npf_portmap_t *pm = np->n_portmap;
    266  1.1  rmind 	u_int n = PORTMAP_SIZE, idx, bit;
    267  1.1  rmind 	uint32_t map, nmap;
    268  1.1  rmind 
    269  1.1  rmind 	idx = arc4random() % PORTMAP_SIZE;
    270  1.1  rmind 	for (;;) {
    271  1.1  rmind 		KASSERT(idx < PORTMAP_SIZE);
    272  1.1  rmind 		map = pm->p_bitmap[idx];
    273  1.1  rmind 		if (__predict_false(map == PORTMAP_FILLED)) {
    274  1.1  rmind 			if (n-- == 0) {
    275  1.1  rmind 				/* No space. */
    276  1.1  rmind 				return 0;
    277  1.1  rmind 			}
    278  1.2  rmind 			/* This bitmap is filled, next. */
    279  1.1  rmind 			idx = (idx ? idx : PORTMAP_SIZE) - 1;
    280  1.1  rmind 			continue;
    281  1.1  rmind 		}
    282  1.1  rmind 		bit = ffs32(~map) - 1;
    283  1.1  rmind 		nmap = map | (1 << bit);
    284  1.1  rmind 		if (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map) {
    285  1.1  rmind 			/* Success. */
    286  1.1  rmind 			break;
    287  1.1  rmind 		}
    288  1.1  rmind 	}
    289  1.1  rmind 	return htons(PORTMAP_FIRST + (idx << PORTMAP_SHIFT) + bit);
    290  1.1  rmind }
    291  1.1  rmind 
    292  1.1  rmind /*
    293  1.1  rmind  * npf_nat_putport: return port as available in the NAT policy portmap.
    294  1.1  rmind  *
    295  1.1  rmind  * => Port should be in network byte-order.
    296  1.1  rmind  */
    297  1.1  rmind static void
    298  1.1  rmind npf_nat_putport(npf_natpolicy_t *np, in_port_t port)
    299  1.1  rmind {
    300  1.1  rmind 	npf_portmap_t *pm = np->n_portmap;
    301  1.1  rmind 	uint32_t map, nmap;
    302  1.1  rmind 	u_int idx, bit;
    303  1.1  rmind 
    304  1.1  rmind 	port = ntohs(port) - PORTMAP_FIRST;
    305  1.1  rmind 	idx = port >> PORTMAP_SHIFT;
    306  1.1  rmind 	bit = port & PORTMAP_MASK;
    307  1.1  rmind 	do {
    308  1.1  rmind 		map = pm->p_bitmap[idx];
    309  1.1  rmind 		KASSERT(map | (1 << bit));
    310  1.1  rmind 		nmap = map & ~(1 << bit);
    311  1.1  rmind 	} while (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) != map);
    312  1.1  rmind }
    313  1.1  rmind 
    314  1.1  rmind /*
    315  1.2  rmind  * npf_nat_inspect: inspect packet against NAT ruleset and return a policy.
    316  1.2  rmind  */
    317  1.2  rmind static npf_natpolicy_t *
    318  1.2  rmind npf_nat_inspect(npf_cache_t *npc, nbuf_t *nbuf, struct ifnet *ifp, const int di)
    319  1.2  rmind {
    320  1.2  rmind 	npf_rule_t *rl;
    321  1.2  rmind 
    322  1.2  rmind 	rl = npf_ruleset_match(nat_ruleset, npc, nbuf, ifp, di, NPF_LAYER_3);
    323  1.2  rmind 
    324  1.2  rmind 	return rl ? npf_rule_getnat(rl) : NULL;
    325  1.2  rmind }
    326  1.2  rmind 
    327  1.2  rmind /*
    328  1.2  rmind  * npf_nat_create: create a new NAT translation entry.
    329  1.1  rmind  */
    330  1.2  rmind static npf_nat_t *
    331  1.2  rmind npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np)
    332  1.1  rmind {
    333  1.3  rmind 	const int proto = npf_cache_ipproto(npc);
    334  1.2  rmind 	npf_nat_t *nt;
    335  1.2  rmind 
    336  1.3  rmind 	KASSERT(npf_iscached(npc, NPC_IP46 | NPC_LAYER4));
    337  1.3  rmind 
    338  1.2  rmind 	/* New NAT association. */
    339  1.2  rmind 	nt = pool_cache_get(nat_cache, PR_NOWAIT);
    340  1.2  rmind 	if (nt == NULL){
    341  1.2  rmind 		return NULL;
    342  1.2  rmind 	}
    343  1.2  rmind 	nt->nt_natpolicy = np;
    344  1.2  rmind 	nt->nt_alg = NULL;
    345  1.2  rmind 
    346  1.2  rmind 	/* Save the original address which may be rewritten. */
    347  1.2  rmind 	if (np->n_type == NPF_NATOUT) {
    348  1.2  rmind 		/* Source (local) for Outbound NAT. */
    349  1.3  rmind 		memcpy(&nt->nt_oaddr, npc->npc_srcip, npc->npc_ipsz);
    350  1.2  rmind 	} else {
    351  1.2  rmind 		/* Destination (external) for Inbound NAT. */
    352  1.2  rmind 		KASSERT(np->n_type == NPF_NATIN);
    353  1.3  rmind 		memcpy(&nt->nt_oaddr, npc->npc_dstip, npc->npc_ipsz);
    354  1.2  rmind 	}
    355  1.2  rmind 
    356  1.2  rmind 	/*
    357  1.2  rmind 	 * Port translation, if required, and if it is TCP/UDP.
    358  1.2  rmind 	 */
    359  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
    360  1.2  rmind 	    (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
    361  1.2  rmind 		nt->nt_oport = 0;
    362  1.2  rmind 		nt->nt_tport = 0;
    363  1.2  rmind 		return nt;
    364  1.2  rmind 	}
    365  1.3  rmind 	/* Save the relevant TCP/UDP port. */
    366  1.3  rmind 	if (proto == IPPROTO_TCP) {
    367  1.3  rmind 		struct tcphdr *th = &npc->npc_l4.tcp;
    368  1.3  rmind 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
    369  1.3  rmind 		    th->th_sport : th->th_dport;
    370  1.2  rmind 	} else {
    371  1.3  rmind 		struct udphdr *uh = &npc->npc_l4.udp;
    372  1.3  rmind 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
    373  1.3  rmind 		    uh->uh_sport : uh->uh_dport;
    374  1.2  rmind 	}
    375  1.3  rmind 
    376  1.2  rmind 	/* Get a new port for translation. */
    377  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
    378  1.2  rmind 		nt->nt_tport = npf_nat_getport(np);
    379  1.2  rmind 	} else {
    380  1.2  rmind 		nt->nt_tport = np->n_tport;
    381  1.2  rmind 	}
    382  1.2  rmind 	return nt;
    383  1.2  rmind }
    384  1.2  rmind 
    385  1.2  rmind /*
    386  1.2  rmind  * npf_nat_translate: perform address and/or port translation.
    387  1.2  rmind  */
    388  1.2  rmind static int
    389  1.2  rmind npf_nat_translate(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt,
    390  1.2  rmind     const bool forw, const int di)
    391  1.2  rmind {
    392  1.1  rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    393  1.3  rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    394  1.3  rmind 	npf_addr_t *addr;
    395  1.2  rmind 	in_port_t port;
    396  1.2  rmind 
    397  1.3  rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    398  1.2  rmind 
    399  1.2  rmind 	if (forw) {
    400  1.2  rmind 		/* "Forwards" stream: use translation address/port. */
    401  1.2  rmind 		KASSERT(
    402  1.2  rmind 		    (np->n_type == NPF_NATIN && di == PFIL_IN) ^
    403  1.2  rmind 		    (np->n_type == NPF_NATOUT && di == PFIL_OUT)
    404  1.2  rmind 		);
    405  1.3  rmind 		addr = &np->n_taddr;
    406  1.2  rmind 		port = nt->nt_tport;
    407  1.2  rmind 	} else {
    408  1.2  rmind 		/* "Backwards" stream: use original address/port. */
    409  1.2  rmind 		KASSERT(
    410  1.2  rmind 		    (np->n_type == NPF_NATIN && di == PFIL_OUT) ^
    411  1.2  rmind 		    (np->n_type == NPF_NATOUT && di == PFIL_IN)
    412  1.2  rmind 		);
    413  1.3  rmind 		addr = &nt->nt_oaddr;
    414  1.2  rmind 		port = nt->nt_oport;
    415  1.2  rmind 	}
    416  1.2  rmind 
    417  1.3  rmind 	/* Execute ALG hook first. */
    418  1.2  rmind 	npf_alg_exec(npc, nbuf, nt, di);
    419  1.2  rmind 
    420  1.2  rmind 	/*
    421  1.3  rmind 	 * Rewrite IP and/or TCP/UDP checksums first, since it will use
    422  1.3  rmind 	 * the cache containing original values for checksum calculation.
    423  1.3  rmind 	 */
    424  1.3  rmind 	if (!npf_rwrcksum(npc, nbuf, n_ptr, di, addr, port)) {
    425  1.3  rmind 		return EINVAL;
    426  1.3  rmind 	}
    427  1.3  rmind 	/*
    428  1.2  rmind 	 * Address translation: rewrite source/destination address, depending
    429  1.2  rmind 	 * on direction (PFIL_OUT - for source, PFIL_IN - for destination).
    430  1.2  rmind 	 */
    431  1.2  rmind 	if (!npf_rwrip(npc, nbuf, n_ptr, di, addr)) {
    432  1.2  rmind 		return EINVAL;
    433  1.2  rmind 	}
    434  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTS) == 0) {
    435  1.3  rmind 		/* Done. */
    436  1.2  rmind 		return 0;
    437  1.2  rmind 	}
    438  1.3  rmind 	switch (npf_cache_ipproto(npc)) {
    439  1.2  rmind 	case IPPROTO_TCP:
    440  1.2  rmind 	case IPPROTO_UDP:
    441  1.3  rmind 		KASSERT(npf_iscached(npc, NPC_TCP | NPC_UDP));
    442  1.2  rmind 		/* Rewrite source/destination port. */
    443  1.3  rmind 		if (!npf_rwrport(npc, nbuf, n_ptr, di, port)) {
    444  1.2  rmind 			return EINVAL;
    445  1.2  rmind 		}
    446  1.2  rmind 		break;
    447  1.2  rmind 	case IPPROTO_ICMP:
    448  1.3  rmind 		KASSERT(npf_iscached(npc, NPC_ICMP));
    449  1.3  rmind 		/* Nothing. */
    450  1.2  rmind 		break;
    451  1.2  rmind 	default:
    452  1.2  rmind 		return ENOTSUP;
    453  1.2  rmind 	}
    454  1.2  rmind 	return 0;
    455  1.2  rmind }
    456  1.2  rmind 
    457  1.2  rmind /*
    458  1.2  rmind  * npf_do_nat:
    459  1.2  rmind  *	- Inspect packet for a NAT policy, unless a session with a NAT
    460  1.2  rmind  *	  association already exists.  In such case, determine whether is
    461  1.2  rmind  *	  is a "forwards" or "backwards" stream.
    462  1.2  rmind  *	- Perform translation: rewrite source address if "forwards" stream
    463  1.2  rmind  *	  and destination address if "backwards".
    464  1.2  rmind  *	- Establish sessions or, if already exists, associate a NAT policy.
    465  1.2  rmind  */
    466  1.2  rmind int
    467  1.2  rmind npf_do_nat(npf_cache_t *npc, npf_session_t *se, nbuf_t *nbuf,
    468  1.2  rmind     struct ifnet *ifp, const int di)
    469  1.2  rmind {
    470  1.2  rmind 	npf_session_t *nse = NULL;
    471  1.1  rmind 	npf_natpolicy_t *np;
    472  1.1  rmind 	npf_nat_t *nt;
    473  1.1  rmind 	int error;
    474  1.2  rmind 	bool forw, new;
    475  1.1  rmind 
    476  1.1  rmind 	/* All relevant IPv4 data should be already cached. */
    477  1.3  rmind 	if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
    478  1.1  rmind 		return 0;
    479  1.1  rmind 	}
    480  1.1  rmind 
    481  1.2  rmind 	/*
    482  1.2  rmind 	 * Return the NAT entry associated with the session, if any.
    483  1.3  rmind 	 * Determines whether the stream is "forwards" or "backwards".
    484  1.2  rmind 	 */
    485  1.2  rmind 	if (se && (nt = npf_session_retnat(se, di, &forw)) != NULL) {
    486  1.1  rmind 		np = nt->nt_natpolicy;
    487  1.1  rmind 		new = false;
    488  1.2  rmind 		goto translate;
    489  1.1  rmind 	}
    490  1.1  rmind 
    491  1.2  rmind 	/* Inspect the packet for a NAT policy, if there is no session. */
    492  1.2  rmind 	np = npf_nat_inspect(npc, nbuf, ifp, di);
    493  1.1  rmind 	if (np == NULL) {
    494  1.1  rmind 		/* If packet does not match - done. */
    495  1.1  rmind 		return 0;
    496  1.1  rmind 	}
    497  1.2  rmind 	forw = true;
    498  1.1  rmind 
    499  1.2  rmind 	/* Create a new NAT translation entry. */
    500  1.2  rmind 	nt = npf_nat_create(npc, np);
    501  1.2  rmind 	if (nt == NULL) {
    502  1.1  rmind 		return ENOMEM;
    503  1.1  rmind 	}
    504  1.1  rmind 	new = true;
    505  1.1  rmind 
    506  1.3  rmind 	/* Determine whether any ALG matches. */
    507  1.3  rmind 	if (npf_alg_match(npc, nbuf, nt)) {
    508  1.3  rmind 		KASSERT(nt->nt_alg != NULL);
    509  1.3  rmind 	}
    510  1.3  rmind 
    511  1.2  rmind 	/*
    512  1.2  rmind 	 * If there is no local session (no "keep state" rule - unusual, but
    513  1.2  rmind 	 * possible configuration), establish one before translation.  Note
    514  1.2  rmind 	 * that it is not a "pass" session, therefore passing of "backwards"
    515  1.2  rmind 	 * stream depends on other, stateless filtering rules.
    516  1.2  rmind 	 */
    517  1.1  rmind 	if (se == NULL) {
    518  1.3  rmind 		nse = npf_session_establish(npc, nbuf, NULL, di);
    519  1.1  rmind 		if (nse == NULL) {
    520  1.1  rmind 			error = ENOMEM;
    521  1.1  rmind 			goto out;
    522  1.1  rmind 		}
    523  1.1  rmind 		se = nse;
    524  1.1  rmind 	}
    525  1.2  rmind translate:
    526  1.2  rmind 	/* Perform the translation. */
    527  1.2  rmind 	error = npf_nat_translate(npc, nbuf, nt, forw, di);
    528  1.2  rmind 	if (error) {
    529  1.1  rmind 		goto out;
    530  1.1  rmind 	}
    531  1.1  rmind 
    532  1.1  rmind 	if (__predict_false(new)) {
    533  1.1  rmind 		npf_session_t *natse;
    534  1.1  rmind 		/*
    535  1.1  rmind 		 * Establish a new NAT session using translated address and
    536  1.1  rmind 		 * associate NAT translation data with this session.
    537  1.1  rmind 		 *
    538  1.1  rmind 		 * Note: packet now has a translated address in the cache.
    539  1.1  rmind 		 */
    540  1.3  rmind 		natse = npf_session_establish(npc, nbuf, nt, di);
    541  1.1  rmind 		if (natse == NULL) {
    542  1.1  rmind 			error = ENOMEM;
    543  1.1  rmind 			goto out;
    544  1.1  rmind 		}
    545  1.1  rmind 		/*
    546  1.1  rmind 		 * Link local session with NAT session, if no link already.
    547  1.1  rmind 		 */
    548  1.1  rmind 		npf_session_link(se, natse);
    549  1.1  rmind 		npf_session_release(natse);
    550  1.1  rmind out:
    551  1.1  rmind 		if (error) {
    552  1.1  rmind 			if (nse != NULL) {
    553  1.2  rmind 				/* XXX: Expire it?? */
    554  1.1  rmind 			}
    555  1.1  rmind 			/* Will free the structure and return the port. */
    556  1.1  rmind 			npf_nat_expire(nt);
    557  1.1  rmind 		}
    558  1.1  rmind 		if (nse != NULL) {
    559  1.1  rmind 			npf_session_release(nse);
    560  1.1  rmind 		}
    561  1.1  rmind 	}
    562  1.1  rmind 	return error;
    563  1.1  rmind }
    564  1.1  rmind 
    565  1.1  rmind /*
    566  1.2  rmind  * npf_nat_getorig: return original IP address and port from translation entry.
    567  1.1  rmind  */
    568  1.1  rmind void
    569  1.3  rmind npf_nat_getorig(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
    570  1.1  rmind {
    571  1.1  rmind 
    572  1.3  rmind 	*addr = &nt->nt_oaddr;
    573  1.2  rmind 	*port = nt->nt_oport;
    574  1.1  rmind }
    575  1.1  rmind 
    576  1.3  rmind /*
    577  1.3  rmind  * npf_nat_setalg: associate an ALG with the NAT entry.
    578  1.3  rmind  */
    579  1.1  rmind void
    580  1.1  rmind npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
    581  1.1  rmind {
    582  1.1  rmind 
    583  1.1  rmind 	nt->nt_alg = alg;
    584  1.1  rmind 	nt->nt_alg_arg = arg;
    585  1.1  rmind }
    586  1.1  rmind 
    587  1.1  rmind /*
    588  1.1  rmind  * npf_nat_expire: free NAT-related data structures on session expiration.
    589  1.1  rmind  */
    590  1.1  rmind void
    591  1.1  rmind npf_nat_expire(npf_nat_t *nt)
    592  1.1  rmind {
    593  1.2  rmind 	npf_natpolicy_t *np = nt->nt_natpolicy;
    594  1.1  rmind 
    595  1.2  rmind 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
    596  1.2  rmind 		KASSERT(nt->nt_tport != 0);
    597  1.1  rmind 		npf_nat_putport(np, nt->nt_tport);
    598  1.1  rmind 	}
    599  1.1  rmind 	pool_cache_put(nat_cache, nt);
    600  1.1  rmind }
    601  1.1  rmind 
    602  1.1  rmind #if defined(DDB) || defined(_NPF_TESTING)
    603  1.1  rmind 
    604  1.1  rmind void
    605  1.1  rmind npf_nat_dump(npf_nat_t *nt)
    606  1.1  rmind {
    607  1.1  rmind 	npf_natpolicy_t *np;
    608  1.1  rmind 	struct in_addr ip;
    609  1.1  rmind 
    610  1.1  rmind 	if (nt) {
    611  1.1  rmind 		np = nt->nt_natpolicy;
    612  1.1  rmind 		goto skip;
    613  1.1  rmind 	}
    614  1.1  rmind 	LIST_FOREACH(np, &nat_policy_list, n_entry) {
    615  1.1  rmind skip:
    616  1.3  rmind 		memcpy(&ip, &np->n_taddr, sizeof(ip));
    617  1.3  rmind 		printf("\tNAT policy: type %d, flags 0x%x, taddr %s, tport = %d\n",
    618  1.3  rmind 		    np->n_type, np->n_flags, inet_ntoa(ip), np->n_tport);
    619  1.1  rmind 		if (nt == NULL) {
    620  1.1  rmind 			continue;
    621  1.1  rmind 		}
    622  1.3  rmind 		memcpy(&ip, &nt->nt_oaddr, sizeof(ip));
    623  1.2  rmind 		printf("\tNAT: original address %s, oport %d, tport = %d\n",
    624  1.2  rmind 		    inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
    625  1.1  rmind 		if (nt->nt_alg) {
    626  1.1  rmind 			printf("\tNAT ALG = %p, ARG = %p\n",
    627  1.1  rmind 			    nt->nt_alg, (void *)nt->nt_alg_arg);
    628  1.1  rmind 		}
    629  1.1  rmind 		return;
    630  1.1  rmind 	}
    631  1.1  rmind }
    632  1.1  rmind 
    633  1.1  rmind #endif
    634