xref: /netbsd-src/sys/net/npf/npf_nat.c (revision 4817a0b0b8fe9612e8ebe21a9bf2d97b95038a97)
1 /*	$NetBSD: npf_nat.c,v 1.4 2010/12/18 01:07:25 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 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).
34  * Described in RFC 2663, RFC 3022.  Commonly just "NAT".
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.
47  *
48  *	Outbound NAT rewrites:
49  *	- Source on "forwards" stream.
50  *	- Destination on "backwards" stream.
51  *	Inbound NAT rewrites:
52  *	- Destination on "forwards" stream.
53  *	- Source on "backwards" stream.
54  *
55  *	It should be noted that bi-directional NAT is a combined outbound
56  *	and inbound translation, therefore constructed as two policies.
57  *
58  * NAT policies and port maps
59  *
60  *	NAT (translation) policy is applied when a packet matches the rule.
61  *	Apart from filter criteria, NAT policy has a translation IP address
62  *	and associated port map.  Port map is a bitmap used to reserve and
63  *	use unique TCP/UDP ports for translation.  Port maps are unique to
64  *	the IP addresses, therefore multiple NAT policies with the same IP
65  *	will share the same port map.
66  *
67  * Sessions, translation entries and their life-cycle
68  *
69  *	NAT module relies on session management module.  Each translated
70  *	session has an associated translation entry (npf_nat_t), which
71  *	contains information used for backwards stream translation, i.e.
72  *	original IP address with port and translation port, allocated from
73  *	the port map.  Each NAT entry is associated with the policy, which
74  *	contains translation IP address.  Allocated port is returned to the
75  *	port map and NAT entry is destroyed when session expires.
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.4 2010/12/18 01:07:25 rmind Exp $");
80 
81 #include <sys/param.h>
82 #include <sys/kernel.h>
83 
84 #include <sys/atomic.h>
85 #include <sys/bitops.h>
86 #include <sys/condvar.h>
87 #include <sys/kmem.h>
88 #include <sys/mutex.h>
89 #include <sys/pool.h>
90 #include <net/pfil.h>
91 #include <netinet/in.h>
92 
93 #include "npf_impl.h"
94 
95 /*
96  * NPF portmap structure.
97  */
98 typedef struct {
99 	u_int			p_refcnt;
100 	uint32_t		p_bitmap[0];
101 } npf_portmap_t;
102 
103 /* Portmap range: [ 1024 .. 65535 ] */
104 #define	PORTMAP_FIRST		(1024)
105 #define	PORTMAP_SIZE		((65536 - PORTMAP_FIRST) / 32)
106 #define	PORTMAP_FILLED		((uint32_t)~0)
107 #define	PORTMAP_MASK		(31)
108 #define	PORTMAP_SHIFT		(5)
109 
110 #define	PORTMAP_MEM_SIZE	\
111     (sizeof(npf_portmap_t) + (PORTMAP_SIZE * sizeof(uint32_t)))
112 
113 /* NAT policy structure. */
114 struct npf_natpolicy {
115 	LIST_HEAD(, npf_nat)	n_nat_list;
116 	kmutex_t		n_lock;
117 	kcondvar_t		n_cv;
118 	npf_portmap_t *		n_portmap;
119 	int			n_type;
120 	int			n_flags;
121 	size_t			n_addr_sz;
122 	npf_addr_t		n_taddr;
123 	in_port_t		n_tport;
124 };
125 
126 #define	NPF_NP_CMP_START	offsetof(npf_natpolicy_t, n_type)
127 #define	NPF_NP_CMP_SIZE		(sizeof(npf_natpolicy_t) - NPF_NP_CMP_START)
128 
129 /* NAT translation entry for a session. */
130 struct npf_nat {
131 	/* Association (list entry and a link pointer) with NAT policy. */
132 	LIST_ENTRY(npf_nat)	nt_entry;
133 	npf_natpolicy_t *	nt_natpolicy;
134 	npf_session_t *		nt_session;
135 	/* Original address and port (for backwards translation). */
136 	npf_addr_t		nt_oaddr;
137 	in_port_t		nt_oport;
138 	/* Translation port (for redirects). */
139 	in_port_t		nt_tport;
140 	/* ALG (if any) associated with this NAT entry. */
141 	npf_alg_t *		nt_alg;
142 	uintptr_t		nt_alg_arg;
143 };
144 
145 static pool_cache_t		nat_cache	__read_mostly;
146 
147 /*
148  * npf_nat_sys{init,fini}: initialise/destroy NAT subsystem structures.
149  */
150 
151 void
152 npf_nat_sysinit(void)
153 {
154 
155 	nat_cache = pool_cache_init(sizeof(npf_nat_t), coherency_unit,
156 	    0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
157 	KASSERT(nat_cache != NULL);
158 }
159 
160 void
161 npf_nat_sysfini(void)
162 {
163 
164 	/* NAT policies should already be destroyed. */
165 	pool_cache_destroy(nat_cache);
166 }
167 
168 /*
169  * npf_nat_newpolicy: create a new NAT policy.
170  *
171  * => Shares portmap if policy is on existing translation address.
172  * => XXX: serialise at upper layer.
173  */
174 npf_natpolicy_t *
175 npf_nat_newpolicy(prop_dictionary_t natdict)
176 {
177 	npf_natpolicy_t *np/*, *it */;
178 	const npf_addr_t *taddr;
179 	prop_object_t obj;
180 	npf_portmap_t *pm;
181 
182 	np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
183 	mutex_init(&np->n_lock, MUTEX_DEFAULT, IPL_SOFTNET);
184 	cv_init(&np->n_cv, "npfnatcv");
185 	LIST_INIT(&np->n_nat_list);
186 
187 	/* Translation type. */
188 	obj = prop_dictionary_get(natdict, "type");
189 	np->n_type = prop_number_integer_value(obj);
190 
191 	/* Translation type. */
192 	obj = prop_dictionary_get(natdict, "flags");
193 	np->n_flags = prop_number_integer_value(obj);
194 
195 	/* Translation IP. */
196 	obj = prop_dictionary_get(natdict, "translation-ip");
197 	np->n_addr_sz = prop_data_size(obj);
198 	KASSERT(np->n_addr_sz > 0 && np->n_addr_sz <= sizeof(npf_addr_t));
199 	taddr = (const npf_addr_t *)prop_data_data_nocopy(obj);
200 	memcpy(&np->n_taddr, taddr, np->n_addr_sz);
201 
202 	/* Translation port (for redirect case). */
203 	obj = prop_dictionary_get(natdict, "translation-port");
204 	np->n_tport = (in_port_t)prop_number_integer_value(obj);
205 
206 	KASSERT(np->n_type == NPF_NATIN || np->n_type == NPF_NATOUT);
207 
208 	pm = NULL;
209 	if ((np->n_flags & NPF_NAT_PORTMAP) == 0) {
210 		goto nopm;
211 	}
212 
213 	/* Search for a NAT policy using the same translation address. */
214 #if 0
215 	LIST_FOREACH(it, &nat_policy_list, n_entry) {
216 		if (memcmp(&it->n_taddr, &np->n_taddr, sizeof(npf_addr_t))) {
217 			continue;
218 		}
219 		pm = it->n_portmap;
220 		break;
221 	}
222 #else
223 	pm = NULL;
224 #endif
225 	if (pm == NULL) {
226 		/* Allocate a new port map for the NAT policy. */
227 		pm = kmem_zalloc(PORTMAP_MEM_SIZE, KM_SLEEP);
228 		if (pm == NULL) {
229 			kmem_free(np, sizeof(npf_natpolicy_t));
230 			return NULL;
231 		}
232 		pm->p_refcnt = 1;
233 		KASSERT((uintptr_t)pm->p_bitmap == (uintptr_t)pm + sizeof(*pm));
234 	} else {
235 		/* Share the port map. */
236 		pm->p_refcnt++;
237 	}
238 nopm:
239 	np->n_portmap = pm;
240 	return np;
241 }
242 
243 /*
244  * npf_nat_freepolicy: free NAT policy and, on last reference, free portmap.
245  *
246  * => Called from npf_rule_free() during the reload via npf_ruleset_destroy().
247  */
248 void
249 npf_nat_freepolicy(npf_natpolicy_t *np)
250 {
251 	npf_portmap_t *pm = np->n_portmap;
252 	npf_nat_t *nt;
253 
254 	/* De-associate all entries from the policy. */
255 	mutex_enter(&np->n_lock);
256 	LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
257 		if (nt->nt_session == NULL) { /* XXXSMP */
258 			npf_session_expire(nt->nt_session);
259 		}
260 	}
261 	while (!LIST_EMPTY(&np->n_nat_list)) {
262 		cv_wait(&np->n_cv, &np->n_lock);
263 	}
264 	mutex_exit(&np->n_lock);
265 
266 	/* Destroy the port map, on last reference. */
267 	if (pm && --pm->p_refcnt == 0) {
268 		KASSERT((np->n_flags & NPF_NAT_PORTMAP) != 0);
269 		kmem_free(pm, PORTMAP_MEM_SIZE);
270 	}
271 	cv_destroy(&np->n_cv);
272 	mutex_destroy(&np->n_lock);
273 	kmem_free(np, sizeof(npf_natpolicy_t));
274 }
275 
276 bool
277 npf_nat_matchpolicy(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
278 {
279 	void *np_raw, *mnp_raw;
280 	/*
281 	 * Compare the relevant NAT policy information (in raw form),
282 	 * which is enough for matching criterion.
283 	 */
284 	np_raw = (uint8_t *)np + NPF_NP_CMP_START;
285 	mnp_raw = (uint8_t *)mnp + NPF_NP_CMP_START;
286 	return (memcmp(np_raw, mnp_raw, NPF_NP_CMP_SIZE) == 0);
287 }
288 
289 /*
290  * npf_nat_getport: allocate and return a port in the NAT policy portmap.
291  *
292  * => Returns in network byte-order.
293  * => Zero indicates failure.
294  */
295 static in_port_t
296 npf_nat_getport(npf_natpolicy_t *np)
297 {
298 	npf_portmap_t *pm = np->n_portmap;
299 	u_int n = PORTMAP_SIZE, idx, bit;
300 	uint32_t map, nmap;
301 
302 	idx = arc4random() % PORTMAP_SIZE;
303 	for (;;) {
304 		KASSERT(idx < PORTMAP_SIZE);
305 		map = pm->p_bitmap[idx];
306 		if (__predict_false(map == PORTMAP_FILLED)) {
307 			if (n-- == 0) {
308 				/* No space. */
309 				return 0;
310 			}
311 			/* This bitmap is filled, next. */
312 			idx = (idx ? idx : PORTMAP_SIZE) - 1;
313 			continue;
314 		}
315 		bit = ffs32(~map) - 1;
316 		nmap = map | (1 << bit);
317 		if (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map) {
318 			/* Success. */
319 			break;
320 		}
321 	}
322 	return htons(PORTMAP_FIRST + (idx << PORTMAP_SHIFT) + bit);
323 }
324 
325 /*
326  * npf_nat_takeport: allocate specific port in the NAT policy portmap.
327  */
328 static bool
329 npf_nat_takeport(npf_natpolicy_t *np, in_port_t port)
330 {
331 	npf_portmap_t *pm = np->n_portmap;
332 	uint32_t map, nmap;
333 	u_int idx, bit;
334 
335 	port = ntohs(port) - PORTMAP_FIRST;
336 	idx = port >> PORTMAP_SHIFT;
337 	bit = port & PORTMAP_MASK;
338 	map = pm->p_bitmap[idx];
339 	nmap = map | (1 << bit);
340 	if (map == nmap) {
341 		/* Already taken. */
342 		return false;
343 	}
344 	return atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map;
345 }
346 
347 /*
348  * npf_nat_putport: return port as available in the NAT policy portmap.
349  *
350  * => Port should be in network byte-order.
351  */
352 static void
353 npf_nat_putport(npf_natpolicy_t *np, in_port_t port)
354 {
355 	npf_portmap_t *pm = np->n_portmap;
356 	uint32_t map, nmap;
357 	u_int idx, bit;
358 
359 	port = ntohs(port) - PORTMAP_FIRST;
360 	idx = port >> PORTMAP_SHIFT;
361 	bit = port & PORTMAP_MASK;
362 	do {
363 		map = pm->p_bitmap[idx];
364 		KASSERT(map | (1 << bit));
365 		nmap = map & ~(1 << bit);
366 	} while (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) != map);
367 }
368 
369 /*
370  * npf_nat_inspect: inspect packet against NAT ruleset and return a policy.
371  */
372 static npf_natpolicy_t *
373 npf_nat_inspect(npf_cache_t *npc, nbuf_t *nbuf, struct ifnet *ifp, const int di)
374 {
375 	npf_ruleset_t *rlset;
376 	npf_rule_t *rl;
377 
378 	rlset = npf_core_natset();
379 	rl = npf_ruleset_match(rlset, npc, nbuf, ifp, di, NPF_LAYER_3);
380 	return rl ? npf_rule_getnat(rl) : NULL;
381 }
382 
383 /*
384  * npf_nat_create: create a new NAT translation entry.
385  */
386 static npf_nat_t *
387 npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np)
388 {
389 	const int proto = npf_cache_ipproto(npc);
390 	npf_nat_t *nt;
391 
392 	KASSERT(npf_iscached(npc, NPC_IP46 | NPC_LAYER4));
393 
394 	/* New NAT association. */
395 	nt = pool_cache_get(nat_cache, PR_NOWAIT);
396 	if (nt == NULL){
397 		return NULL;
398 	}
399 	npf_stats_inc(NPF_STAT_NAT_CREATE);
400 	mutex_enter(&np->n_lock);
401 	LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
402 	nt->nt_natpolicy = np;
403 	nt->nt_session = NULL;
404 	mutex_exit(&np->n_lock);
405 	nt->nt_alg = NULL;
406 
407 	/* Save the original address which may be rewritten. */
408 	if (np->n_type == NPF_NATOUT) {
409 		/* Source (local) for Outbound NAT. */
410 		memcpy(&nt->nt_oaddr, npc->npc_srcip, npc->npc_ipsz);
411 	} else {
412 		/* Destination (external) for Inbound NAT. */
413 		KASSERT(np->n_type == NPF_NATIN);
414 		memcpy(&nt->nt_oaddr, npc->npc_dstip, npc->npc_ipsz);
415 	}
416 
417 	/*
418 	 * Port translation, if required, and if it is TCP/UDP.
419 	 */
420 	if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
421 	    (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
422 		nt->nt_oport = 0;
423 		nt->nt_tport = 0;
424 		return nt;
425 	}
426 	/* Save the relevant TCP/UDP port. */
427 	if (proto == IPPROTO_TCP) {
428 		struct tcphdr *th = &npc->npc_l4.tcp;
429 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
430 		    th->th_sport : th->th_dport;
431 	} else {
432 		struct udphdr *uh = &npc->npc_l4.udp;
433 		nt->nt_oport = (np->n_type == NPF_NATOUT) ?
434 		    uh->uh_sport : uh->uh_dport;
435 	}
436 
437 	/* Get a new port for translation. */
438 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
439 		nt->nt_tport = npf_nat_getport(np);
440 	} else {
441 		nt->nt_tport = np->n_tport;
442 	}
443 	return nt;
444 }
445 
446 /*
447  * npf_nat_translate: perform address and/or port translation.
448  */
449 static int
450 npf_nat_translate(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt,
451     const bool forw, const int di)
452 {
453 	void *n_ptr = nbuf_dataptr(nbuf);
454 	npf_natpolicy_t *np = nt->nt_natpolicy;
455 	npf_addr_t *addr;
456 	in_port_t port;
457 
458 	KASSERT(npf_iscached(npc, NPC_IP46));
459 
460 	if (forw) {
461 		/* "Forwards" stream: use translation address/port. */
462 		KASSERT(
463 		    (np->n_type == NPF_NATIN && di == PFIL_IN) ^
464 		    (np->n_type == NPF_NATOUT && di == PFIL_OUT)
465 		);
466 		addr = &np->n_taddr;
467 		port = nt->nt_tport;
468 	} else {
469 		/* "Backwards" stream: use original address/port. */
470 		KASSERT(
471 		    (np->n_type == NPF_NATIN && di == PFIL_OUT) ^
472 		    (np->n_type == NPF_NATOUT && di == PFIL_IN)
473 		);
474 		addr = &nt->nt_oaddr;
475 		port = nt->nt_oport;
476 	}
477 
478 	/* Execute ALG hook first. */
479 	npf_alg_exec(npc, nbuf, nt, di);
480 
481 	/*
482 	 * Rewrite IP and/or TCP/UDP checksums first, since it will use
483 	 * the cache containing original values for checksum calculation.
484 	 */
485 	if (!npf_rwrcksum(npc, nbuf, n_ptr, di, addr, port)) {
486 		return EINVAL;
487 	}
488 	/*
489 	 * Address translation: rewrite source/destination address, depending
490 	 * on direction (PFIL_OUT - for source, PFIL_IN - for destination).
491 	 */
492 	if (!npf_rwrip(npc, nbuf, n_ptr, di, addr)) {
493 		return EINVAL;
494 	}
495 	if ((np->n_flags & NPF_NAT_PORTS) == 0) {
496 		/* Done. */
497 		return 0;
498 	}
499 	switch (npf_cache_ipproto(npc)) {
500 	case IPPROTO_TCP:
501 	case IPPROTO_UDP:
502 		KASSERT(npf_iscached(npc, NPC_TCP | NPC_UDP));
503 		/* Rewrite source/destination port. */
504 		if (!npf_rwrport(npc, nbuf, n_ptr, di, port)) {
505 			return EINVAL;
506 		}
507 		break;
508 	case IPPROTO_ICMP:
509 		KASSERT(npf_iscached(npc, NPC_ICMP));
510 		/* Nothing. */
511 		break;
512 	default:
513 		return ENOTSUP;
514 	}
515 	return 0;
516 }
517 
518 /*
519  * npf_do_nat:
520  *	- Inspect packet for a NAT policy, unless a session with a NAT
521  *	  association already exists.  In such case, determine whether it
522  *	  is a "forwards" or "backwards" stream.
523  *	- Perform translation: rewrite source or destination fields,
524  *	  depending on translation type and direction.
525  *	- Associate a NAT policy with a session (may establish a new).
526  */
527 int
528 npf_do_nat(npf_cache_t *npc, npf_session_t *se, nbuf_t *nbuf,
529     struct ifnet *ifp, const int di)
530 {
531 	npf_session_t *nse = NULL;
532 	npf_natpolicy_t *np;
533 	npf_nat_t *nt;
534 	int error;
535 	bool forw, new;
536 
537 	/* All relevant IPv4 data should be already cached. */
538 	if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
539 		return 0;
540 	}
541 
542 	/*
543 	 * Return the NAT entry associated with the session, if any.
544 	 * Determines whether the stream is "forwards" or "backwards".
545 	 * Note: no need to lock, since reference on session is held.
546 	 */
547 	if (se && (nt = npf_session_retnat(se, di, &forw)) != NULL) {
548 		np = nt->nt_natpolicy;
549 		new = false;
550 		goto translate;
551 	}
552 
553 	/* Inspect the packet for a NAT policy, if there is no session. */
554 	npf_core_enter();
555 	np = npf_nat_inspect(npc, nbuf, ifp, di);
556 	if (np == NULL) {
557 		/* If packet does not match - done. */
558 		npf_core_exit();
559 		return 0;
560 	}
561 	forw = true;
562 
563 	/*
564 	 * Create a new NAT entry.  Note: it is safe to unlock, since the
565 	 * NAT policy wont be desotroyed while there are list entries, which
566 	 * are removed only on session expiration.  Currently, NAT entry is
567 	 * not yet associated with any session.
568 	 */
569 	nt = npf_nat_create(npc, np);
570 	if (nt == NULL) {
571 		npf_core_exit();
572 		return ENOMEM;
573 	}
574 	npf_core_exit();
575 	new = true;
576 
577 	/* Determine whether any ALG matches. */
578 	if (npf_alg_match(npc, nbuf, nt)) {
579 		KASSERT(nt->nt_alg != NULL);
580 	}
581 
582 	/*
583 	 * If there is no local session (no "keep state" rule - unusual, but
584 	 * possible configuration), establish one before translation.  Note
585 	 * that it is not a "pass" session, therefore passing of "backwards"
586 	 * stream depends on other, stateless filtering rules.
587 	 */
588 	if (se == NULL) {
589 		nse = npf_session_establish(npc, nbuf, di);
590 		if (nse == NULL) {
591 			error = ENOMEM;
592 			goto out;
593 		}
594 		se = nse;
595 	}
596 translate:
597 	/* Perform the translation. */
598 	error = npf_nat_translate(npc, nbuf, nt, forw, di);
599 	if (error) {
600 		goto out;
601 	}
602 
603 	if (__predict_false(new)) {
604 		/*
605 		 * Associate NAT translation entry with the session.
606 		 * Note: packet now has a translated address in the cache.
607 		 */
608 		nt->nt_session = se;
609 		error = npf_session_setnat(se, nt, di);
610 out:
611 		if (error) {
612 			/* If session was for NAT only - expire it. */
613 			if (nse) {
614 				npf_session_expire(nse);
615 			}
616 			/* Will free the structure and return the port. */
617 			npf_nat_expire(nt);
618 		}
619 		if (nse != NULL) {
620 			npf_session_release(nse);
621 		}
622 	}
623 	return error;
624 }
625 
626 /*
627  * npf_nat_gettrans: return translation IP address and port.
628  */
629 void
630 npf_nat_gettrans(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
631 {
632 	npf_natpolicy_t *np = nt->nt_natpolicy;
633 
634 	*addr = &np->n_taddr;
635 	*port = nt->nt_tport;
636 }
637 
638 /*
639  * npf_nat_getorig: return original IP address and port from translation entry.
640  */
641 void
642 npf_nat_getorig(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
643 {
644 
645 	*addr = &nt->nt_oaddr;
646 	*port = nt->nt_oport;
647 }
648 
649 /*
650  * npf_nat_setalg: associate an ALG with the NAT entry.
651  */
652 void
653 npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
654 {
655 
656 	nt->nt_alg = alg;
657 	nt->nt_alg_arg = arg;
658 }
659 
660 /*
661  * npf_nat_expire: free NAT-related data structures on session expiration.
662  */
663 void
664 npf_nat_expire(npf_nat_t *nt)
665 {
666 	npf_natpolicy_t *np = nt->nt_natpolicy;
667 
668 	/* Return any taken port to the portmap. */
669 	if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
670 		npf_nat_putport(np, nt->nt_tport);
671 	}
672 
673 	/* Remove NAT entry from the list, notify any waiters if last entry. */
674 	mutex_enter(&np->n_lock);
675 	LIST_REMOVE(nt, nt_entry);
676 	if (LIST_EMPTY(&np->n_nat_list)) {
677 		cv_broadcast(&np->n_cv);
678 	}
679 	mutex_exit(&np->n_lock);
680 
681 	/* Free structure, increase the counter. */
682 	pool_cache_put(nat_cache, nt);
683 	npf_stats_inc(NPF_STAT_NAT_DESTROY);
684 }
685 
686 /*
687  * npf_nat_save: construct NAT entry and reference to the NAT policy.
688  */
689 int
690 npf_nat_save(prop_dictionary_t sedict, prop_array_t natlist, npf_nat_t *nt)
691 {
692 	npf_natpolicy_t *np = nt->nt_natpolicy;
693 	prop_object_iterator_t it;
694 	prop_dictionary_t npdict;
695 	prop_data_t nd, npd;
696 	uintptr_t itnp;
697 
698 	/* Set NAT entry data. */
699 	nd = prop_data_create_data(nt, sizeof(npf_nat_t));
700 	prop_dictionary_set(sedict, "nat-data", nd);
701 
702 	/* Find or create a NAT policy. */
703 	it = prop_array_iterator(natlist);
704 	while ((npdict = prop_object_iterator_next(it)) != NULL) {
705 		itnp = (uintptr_t)prop_number_unsigned_integer_value(
706 		    prop_dictionary_get(npdict, "id-ptr"));
707 		if (itnp == (uintptr_t)np) {
708 			break;
709 		}
710 	}
711 	if (npdict == NULL) {
712 		/* Create NAT policy dictionary and copy the data. */
713 		npdict = prop_dictionary_create();
714 		npd = prop_data_create_data(np, sizeof(npf_natpolicy_t));
715 
716 		/* Set the data, insert into the array. */
717 		prop_dictionary_set(npdict, "id-ptr",
718 		    prop_number_create_unsigned_integer((uintptr_t)np));
719 		prop_dictionary_set(npdict, "nat-policy-data", npd);
720 		prop_array_add(natlist, npdict);
721 	}
722 	prop_dictionary_set(sedict, "nat-policy",
723 	    prop_dictionary_copy(npdict));
724 	return 0;
725 }
726 
727 /*
728  * npf_nat_restore: find a matching NAT policy and restore NAT entry.
729  *
730  * => Caller should lock the active NAT ruleset.
731  */
732 npf_nat_t *
733 npf_nat_restore(prop_dictionary_t sedict, npf_session_t *se)
734 {
735 	const npf_natpolicy_t *onp;
736 	const npf_nat_t *ntraw;
737 	prop_object_t obj;
738 	npf_natpolicy_t *np;
739 	npf_rule_t *rl;
740 	npf_nat_t *nt;
741 
742 	/* Get raw NAT entry. */
743 	obj = prop_dictionary_get(sedict, "nat-data");
744 	ntraw = prop_data_data_nocopy(obj);
745 	if (ntraw == NULL || prop_data_size(obj) != sizeof(npf_nat_t)) {
746 		return NULL;
747 	}
748 
749 	/* Find a stored NAT policy information. */
750 	obj = prop_dictionary_get(
751 	    prop_dictionary_get(sedict, "nat-policy"), "nat-policy-data");
752 	onp = prop_data_data_nocopy(obj);
753 	if (onp == NULL || prop_data_size(obj) != sizeof(npf_natpolicy_t)) {
754 		return NULL;
755 	}
756 
757 	/* Match if there is an existing NAT policy. */
758 	rl = npf_ruleset_matchnat(npf_core_natset(), __UNCONST(onp));
759 	if (rl == NULL) {
760 		return NULL;
761 	}
762 	np = npf_rule_getnat(rl);
763 	KASSERT(np != NULL);
764 
765 	/* Take a specific port from port-map. */
766 	if (!npf_nat_takeport(np, ntraw->nt_tport)) {
767 		return NULL;
768 	}
769 
770 	/* Create and return NAT entry for association. */
771 	nt = pool_cache_get(nat_cache, PR_WAITOK);
772 	memcpy(nt, ntraw, sizeof(npf_nat_t));
773 	LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
774 	nt->nt_natpolicy = np;
775 	nt->nt_session = se;
776 	nt->nt_alg = NULL;
777 	return nt;
778 }
779 
780 #if defined(DDB) || defined(_NPF_TESTING)
781 
782 void
783 npf_nat_dump(npf_nat_t *nt)
784 {
785 	npf_natpolicy_t *np;
786 	struct in_addr ip;
787 
788 	np = nt->nt_natpolicy;
789 	memcpy(&ip, &np->n_taddr, sizeof(ip));
790 	printf("\tNATP(%p): type %d flags 0x%x taddr %s tport %d\n",
791 	    np, np->n_type, np->n_flags, inet_ntoa(ip), np->n_tport);
792 	memcpy(&ip, &nt->nt_oaddr, sizeof(ip));
793 	printf("\tNAT: original address %s oport %d tport %d\n",
794 	    inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
795 	if (nt->nt_alg) {
796 		printf("\tNAT ALG = %p, ARG = %p\n",
797 		    nt->nt_alg, (void *)nt->nt_alg_arg);
798 	}
799 }
800 
801 #endif
802