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