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