npf_nat.c revision 1.46 1 1.1 rmind /*-
2 1.46 rmind * Copyright (c) 2014-2019 Mindaugas Rasiukevicius <rmind at netbsd org>
3 1.19 rmind * Copyright (c) 2010-2013 The NetBSD Foundation, Inc.
4 1.1 rmind * All rights reserved.
5 1.1 rmind *
6 1.1 rmind * This material is based upon work partially supported by The
7 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
8 1.1 rmind *
9 1.1 rmind * Redistribution and use in source and binary forms, with or without
10 1.1 rmind * modification, are permitted provided that the following conditions
11 1.1 rmind * are met:
12 1.1 rmind * 1. Redistributions of source code must retain the above copyright
13 1.1 rmind * notice, this list of conditions and the following disclaimer.
14 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 rmind * notice, this list of conditions and the following disclaimer in the
16 1.1 rmind * documentation and/or other materials provided with the distribution.
17 1.1 rmind *
18 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
29 1.1 rmind */
30 1.1 rmind
31 1.1 rmind /*
32 1.19 rmind * NPF network address port translation (NAPT) and other forms of NAT.
33 1.19 rmind * Described in RFC 2663, RFC 3022, etc.
34 1.1 rmind *
35 1.1 rmind * Overview
36 1.1 rmind *
37 1.45 rmind * There are a few mechanisms: NAT policy, port map and translation.
38 1.45 rmind * The NAT module has a separate ruleset where rules always have an
39 1.45 rmind * associated NAT policy.
40 1.1 rmind *
41 1.2 rmind * Translation types
42 1.2 rmind *
43 1.2 rmind * There are two types of translation: outbound (NPF_NATOUT) and
44 1.2 rmind * inbound (NPF_NATIN). It should not be confused with connection
45 1.23 rmind * direction. See npf_nat_which() for the description of how the
46 1.45 rmind * addresses are rewritten. The bi-directional NAT is a combined
47 1.45 rmind * outbound and inbound translation, therefore is constructed as
48 1.45 rmind * two policies.
49 1.2 rmind *
50 1.1 rmind * NAT policies and port maps
51 1.1 rmind *
52 1.45 rmind * The NAT (translation) policy is applied when packet matches the
53 1.45 rmind * rule. Apart from the filter criteria, the NAT policy always has
54 1.46 rmind * a translation IP address or a table. If port translation is set,
55 1.46 rmind * then NAT mechanism relies on port map mechanism.
56 1.1 rmind *
57 1.29 rmind * Connections, translation entries and their life-cycle
58 1.1 rmind *
59 1.45 rmind * NAT relies on the connection tracking module. Each translated
60 1.45 rmind * connection has an associated translation entry (npf_nat_t) which
61 1.4 rmind * contains information used for backwards stream translation, i.e.
62 1.45 rmind * the original IP address with port and translation port, allocated
63 1.45 rmind * from the port map. Each NAT entry is associated with the policy,
64 1.45 rmind * which contains translation IP address. Allocated port is returned
65 1.45 rmind * to the port map and NAT entry is destroyed when connection expires.
66 1.1 rmind */
67 1.1 rmind
68 1.41 christos #ifdef _KERNEL
69 1.1 rmind #include <sys/cdefs.h>
70 1.46 rmind __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.46 2019/07/23 00:52:01 rmind Exp $");
71 1.1 rmind
72 1.1 rmind #include <sys/param.h>
73 1.11 rmind #include <sys/types.h>
74 1.1 rmind
75 1.1 rmind #include <sys/atomic.h>
76 1.4 rmind #include <sys/condvar.h>
77 1.1 rmind #include <sys/kmem.h>
78 1.4 rmind #include <sys/mutex.h>
79 1.1 rmind #include <sys/pool.h>
80 1.19 rmind #include <sys/proc.h>
81 1.41 christos #endif
82 1.1 rmind
83 1.1 rmind #include "npf_impl.h"
84 1.29 rmind #include "npf_conn.h"
85 1.1 rmind
86 1.1 rmind /*
87 1.12 rmind * NAT policy structure.
88 1.12 rmind */
89 1.1 rmind struct npf_natpolicy {
90 1.41 christos npf_t * n_npfctx;
91 1.31 rmind kmutex_t n_lock;
92 1.4 rmind LIST_HEAD(, npf_nat) n_nat_list;
93 1.46 rmind volatile unsigned n_refcnt;
94 1.31 rmind uint64_t n_id;
95 1.31 rmind
96 1.31 rmind /*
97 1.45 rmind * Translation type, flags, address or table and the port.
98 1.45 rmind * Additionally, there may be translation algorithm and any
99 1.45 rmind * auxiliary data, e.g. NPTv6 adjustment value.
100 1.31 rmind *
101 1.31 rmind * NPF_NP_CMP_START mark starts here.
102 1.31 rmind */
103 1.46 rmind unsigned n_type;
104 1.45 rmind unsigned n_flags;
105 1.45 rmind unsigned n_alen;
106 1.45 rmind
107 1.4 rmind npf_addr_t n_taddr;
108 1.25 rmind npf_netmask_t n_tmask;
109 1.4 rmind in_port_t n_tport;
110 1.45 rmind unsigned n_tid;
111 1.45 rmind
112 1.45 rmind unsigned n_algo;
113 1.25 rmind union {
114 1.45 rmind unsigned n_rr_idx;
115 1.25 rmind uint16_t n_npt66_adj;
116 1.25 rmind };
117 1.1 rmind };
118 1.1 rmind
119 1.45 rmind /*
120 1.45 rmind * Private flags - must be in the NPF_NAT_PRIVMASK range.
121 1.45 rmind */
122 1.45 rmind #define NPF_NAT_USETABLE (0x01000000 & NPF_NAT_PRIVMASK)
123 1.45 rmind
124 1.4 rmind #define NPF_NP_CMP_START offsetof(npf_natpolicy_t, n_type)
125 1.4 rmind #define NPF_NP_CMP_SIZE (sizeof(npf_natpolicy_t) - NPF_NP_CMP_START)
126 1.4 rmind
127 1.12 rmind /*
128 1.29 rmind * NAT translation entry for a connection.
129 1.12 rmind */
130 1.1 rmind struct npf_nat {
131 1.28 rmind /* Associated NAT policy. */
132 1.4 rmind npf_natpolicy_t * nt_natpolicy;
133 1.28 rmind
134 1.28 rmind /*
135 1.45 rmind * Translation address as well as the original address which is
136 1.45 rmind * used for backwards translation. The same for ports.
137 1.28 rmind */
138 1.45 rmind npf_addr_t nt_taddr;
139 1.4 rmind npf_addr_t nt_oaddr;
140 1.45 rmind
141 1.46 rmind unsigned nt_alen;
142 1.4 rmind in_port_t nt_oport;
143 1.4 rmind in_port_t nt_tport;
144 1.28 rmind
145 1.1 rmind /* ALG (if any) associated with this NAT entry. */
146 1.4 rmind npf_alg_t * nt_alg;
147 1.4 rmind uintptr_t nt_alg_arg;
148 1.28 rmind
149 1.28 rmind LIST_ENTRY(npf_nat) nt_entry;
150 1.29 rmind npf_conn_t * nt_conn;
151 1.1 rmind };
152 1.1 rmind
153 1.4 rmind static pool_cache_t nat_cache __read_mostly;
154 1.1 rmind
155 1.1 rmind /*
156 1.1 rmind * npf_nat_sys{init,fini}: initialise/destroy NAT subsystem structures.
157 1.1 rmind */
158 1.1 rmind
159 1.1 rmind void
160 1.1 rmind npf_nat_sysinit(void)
161 1.1 rmind {
162 1.45 rmind nat_cache = pool_cache_init(sizeof(npf_nat_t), 0,
163 1.1 rmind 0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
164 1.1 rmind KASSERT(nat_cache != NULL);
165 1.1 rmind }
166 1.1 rmind
167 1.1 rmind void
168 1.1 rmind npf_nat_sysfini(void)
169 1.1 rmind {
170 1.23 rmind /* All NAT policies should already be destroyed. */
171 1.1 rmind pool_cache_destroy(nat_cache);
172 1.1 rmind }
173 1.1 rmind
174 1.1 rmind /*
175 1.2 rmind * npf_nat_newpolicy: create a new NAT policy.
176 1.1 rmind */
177 1.1 rmind npf_natpolicy_t *
178 1.44 rmind npf_nat_newpolicy(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *rset)
179 1.1 rmind {
180 1.5 rmind npf_natpolicy_t *np;
181 1.44 rmind const void *addr;
182 1.44 rmind size_t len;
183 1.1 rmind
184 1.1 rmind np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
185 1.41 christos np->n_npfctx = npf;
186 1.4 rmind
187 1.33 rmind /* The translation type, flags and policy ID. */
188 1.44 rmind np->n_type = dnvlist_get_number(nat, "type", 0);
189 1.45 rmind np->n_flags = dnvlist_get_number(nat, "flags", 0) & ~NPF_NAT_PRIVMASK;
190 1.44 rmind np->n_id = dnvlist_get_number(nat, "nat-policy", 0);
191 1.10 rmind
192 1.10 rmind /* Should be exclusively either inbound or outbound NAT. */
193 1.10 rmind if (((np->n_type == NPF_NATIN) ^ (np->n_type == NPF_NATOUT)) == 0) {
194 1.25 rmind goto err;
195 1.10 rmind }
196 1.10 rmind mutex_init(&np->n_lock, MUTEX_DEFAULT, IPL_SOFTNET);
197 1.10 rmind LIST_INIT(&np->n_nat_list);
198 1.4 rmind
199 1.45 rmind /*
200 1.46 rmind * Translation IP, mask and port (if applicable). If using the
201 1.46 rmind * the table, specified by the ID, then the nat-addr/nat-mask will
202 1.46 rmind * be used as a filter for the addresses selected from table.
203 1.45 rmind */
204 1.45 rmind if (nvlist_exists_number(nat, "nat-table-id")) {
205 1.45 rmind if (np->n_flags & NPF_NAT_STATIC) {
206 1.45 rmind goto err;
207 1.45 rmind }
208 1.45 rmind np->n_tid = nvlist_get_number(nat, "nat-table-id");
209 1.45 rmind np->n_tmask = NPF_NO_NETMASK;
210 1.45 rmind np->n_flags |= NPF_NAT_USETABLE;
211 1.45 rmind } else {
212 1.46 rmind addr = dnvlist_get_binary(nat, "nat-addr", &len, NULL, 0);
213 1.45 rmind if (!addr || len == 0 || len > sizeof(npf_addr_t)) {
214 1.45 rmind goto err;
215 1.45 rmind }
216 1.45 rmind memcpy(&np->n_taddr, addr, len);
217 1.45 rmind np->n_alen = len;
218 1.46 rmind np->n_tmask = dnvlist_get_number(nat, "nat-mask", NPF_NO_NETMASK);
219 1.46 rmind if (npf_netmask_check(np->n_alen, np->n_tmask)) {
220 1.46 rmind goto err;
221 1.46 rmind }
222 1.25 rmind }
223 1.44 rmind np->n_tport = dnvlist_get_number(nat, "nat-port", 0);
224 1.4 rmind
225 1.45 rmind /*
226 1.45 rmind * NAT algorithm.
227 1.45 rmind */
228 1.44 rmind np->n_algo = dnvlist_get_number(nat, "nat-algo", 0);
229 1.25 rmind switch (np->n_algo) {
230 1.25 rmind case NPF_ALGO_NPT66:
231 1.44 rmind np->n_npt66_adj = dnvlist_get_number(nat, "npt66-adj", 0);
232 1.25 rmind break;
233 1.45 rmind case NPF_ALGO_NETMAP:
234 1.45 rmind break;
235 1.45 rmind case NPF_ALGO_IPHASH:
236 1.45 rmind case NPF_ALGO_RR:
237 1.25 rmind default:
238 1.46 rmind if (np->n_tmask != NPF_NO_NETMASK) {
239 1.25 rmind goto err;
240 1.46 rmind }
241 1.25 rmind break;
242 1.25 rmind }
243 1.1 rmind return np;
244 1.25 rmind err:
245 1.39 christos mutex_destroy(&np->n_lock);
246 1.25 rmind kmem_free(np, sizeof(npf_natpolicy_t));
247 1.25 rmind return NULL;
248 1.1 rmind }
249 1.1 rmind
250 1.32 rmind int
251 1.44 rmind npf_nat_policyexport(const npf_natpolicy_t *np, nvlist_t *nat)
252 1.32 rmind {
253 1.45 rmind nvlist_add_number(nat, "nat-policy", np->n_id);
254 1.44 rmind nvlist_add_number(nat, "type", np->n_type);
255 1.44 rmind nvlist_add_number(nat, "flags", np->n_flags);
256 1.32 rmind
257 1.45 rmind if (np->n_flags & NPF_NAT_USETABLE) {
258 1.45 rmind nvlist_add_number(nat, "nat-table-id", np->n_tid);
259 1.45 rmind } else {
260 1.46 rmind nvlist_add_binary(nat, "nat-addr", &np->n_taddr, np->n_alen);
261 1.45 rmind nvlist_add_number(nat, "nat-mask", np->n_tmask);
262 1.45 rmind }
263 1.44 rmind nvlist_add_number(nat, "nat-port", np->n_tport);
264 1.44 rmind nvlist_add_number(nat, "nat-algo", np->n_algo);
265 1.32 rmind
266 1.32 rmind switch (np->n_algo) {
267 1.32 rmind case NPF_ALGO_NPT66:
268 1.44 rmind nvlist_add_number(nat, "npt66-adj", np->n_npt66_adj);
269 1.32 rmind break;
270 1.32 rmind }
271 1.32 rmind return 0;
272 1.32 rmind }
273 1.32 rmind
274 1.1 rmind /*
275 1.46 rmind * npf_nat_freepolicy: free the NAT policy.
276 1.1 rmind *
277 1.4 rmind * => Called from npf_rule_free() during the reload via npf_ruleset_destroy().
278 1.1 rmind */
279 1.1 rmind void
280 1.1 rmind npf_nat_freepolicy(npf_natpolicy_t *np)
281 1.1 rmind {
282 1.29 rmind npf_conn_t *con;
283 1.4 rmind npf_nat_t *nt;
284 1.1 rmind
285 1.22 rmind /*
286 1.22 rmind * Disassociate all entries from the policy. At this point,
287 1.22 rmind * new entries can no longer be created for this policy.
288 1.22 rmind */
289 1.28 rmind while (np->n_refcnt) {
290 1.28 rmind mutex_enter(&np->n_lock);
291 1.28 rmind LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
292 1.29 rmind con = nt->nt_conn;
293 1.29 rmind KASSERT(con != NULL);
294 1.29 rmind npf_conn_expire(con);
295 1.28 rmind }
296 1.28 rmind mutex_exit(&np->n_lock);
297 1.4 rmind
298 1.28 rmind /* Kick the worker - all references should be going away. */
299 1.41 christos npf_worker_signal(np->n_npfctx);
300 1.19 rmind kpause("npfgcnat", false, 1, NULL);
301 1.19 rmind }
302 1.22 rmind KASSERT(LIST_EMPTY(&np->n_nat_list));
303 1.4 rmind mutex_destroy(&np->n_lock);
304 1.1 rmind kmem_free(np, sizeof(npf_natpolicy_t));
305 1.1 rmind }
306 1.1 rmind
307 1.13 rmind void
308 1.15 rmind npf_nat_freealg(npf_natpolicy_t *np, npf_alg_t *alg)
309 1.13 rmind {
310 1.15 rmind npf_nat_t *nt;
311 1.15 rmind
312 1.15 rmind mutex_enter(&np->n_lock);
313 1.15 rmind LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
314 1.46 rmind if (nt->nt_alg == alg) {
315 1.31 rmind nt->nt_alg = NULL;
316 1.46 rmind }
317 1.15 rmind }
318 1.15 rmind mutex_exit(&np->n_lock);
319 1.13 rmind }
320 1.13 rmind
321 1.5 rmind /*
322 1.31 rmind * npf_nat_cmppolicy: compare two NAT policies.
323 1.5 rmind *
324 1.5 rmind * => Return 0 on match, and non-zero otherwise.
325 1.5 rmind */
326 1.4 rmind bool
327 1.31 rmind npf_nat_cmppolicy(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
328 1.1 rmind {
329 1.31 rmind const void *np_raw, *mnp_raw;
330 1.31 rmind
331 1.4 rmind /*
332 1.4 rmind * Compare the relevant NAT policy information (in raw form),
333 1.4 rmind * which is enough for matching criterion.
334 1.4 rmind */
335 1.5 rmind KASSERT(np && mnp && np != mnp);
336 1.31 rmind np_raw = (const uint8_t *)np + NPF_NP_CMP_START;
337 1.31 rmind mnp_raw = (const uint8_t *)mnp + NPF_NP_CMP_START;
338 1.31 rmind return memcmp(np_raw, mnp_raw, NPF_NP_CMP_SIZE) == 0;
339 1.1 rmind }
340 1.1 rmind
341 1.31 rmind void
342 1.31 rmind npf_nat_setid(npf_natpolicy_t *np, uint64_t id)
343 1.31 rmind {
344 1.31 rmind np->n_id = id;
345 1.31 rmind }
346 1.31 rmind
347 1.31 rmind uint64_t
348 1.31 rmind npf_nat_getid(const npf_natpolicy_t *np)
349 1.31 rmind {
350 1.31 rmind return np->n_id;
351 1.31 rmind }
352 1.31 rmind
353 1.1 rmind /*
354 1.23 rmind * npf_nat_which: tell which address (source or destination) should be
355 1.23 rmind * rewritten given the combination of the NAT type and flow direction.
356 1.23 rmind */
357 1.46 rmind static inline unsigned
358 1.46 rmind npf_nat_which(const unsigned type, bool forw)
359 1.23 rmind {
360 1.23 rmind /*
361 1.23 rmind * Outbound NAT rewrites:
362 1.24 rmind * - Source (NPF_SRC) on "forwards" stream.
363 1.24 rmind * - Destination (NPF_DST) on "backwards" stream.
364 1.23 rmind * Inbound NAT is other way round.
365 1.23 rmind */
366 1.23 rmind if (type == NPF_NATOUT) {
367 1.23 rmind forw = !forw;
368 1.23 rmind } else {
369 1.23 rmind KASSERT(type == NPF_NATIN);
370 1.23 rmind }
371 1.23 rmind CTASSERT(NPF_SRC == 0 && NPF_DST == 1);
372 1.24 rmind KASSERT(forw == NPF_SRC || forw == NPF_DST);
373 1.46 rmind return (unsigned)forw;
374 1.23 rmind }
375 1.23 rmind
376 1.23 rmind /*
377 1.2 rmind * npf_nat_inspect: inspect packet against NAT ruleset and return a policy.
378 1.19 rmind *
379 1.19 rmind * => Acquire a reference on the policy, if found.
380 1.2 rmind */
381 1.2 rmind static npf_natpolicy_t *
382 1.30 rmind npf_nat_inspect(npf_cache_t *npc, const int di)
383 1.2 rmind {
384 1.19 rmind int slock = npf_config_read_enter();
385 1.41 christos npf_ruleset_t *rlset = npf_config_natset(npc->npc_ctx);
386 1.6 rmind npf_natpolicy_t *np;
387 1.2 rmind npf_rule_t *rl;
388 1.2 rmind
389 1.30 rmind rl = npf_ruleset_inspect(npc, rlset, di, NPF_LAYER_3);
390 1.6 rmind if (rl == NULL) {
391 1.19 rmind npf_config_read_exit(slock);
392 1.6 rmind return NULL;
393 1.6 rmind }
394 1.6 rmind np = npf_rule_getnat(rl);
395 1.19 rmind atomic_inc_uint(&np->n_refcnt);
396 1.19 rmind npf_config_read_exit(slock);
397 1.6 rmind return np;
398 1.2 rmind }
399 1.2 rmind
400 1.46 rmind static void
401 1.46 rmind npf_nat_algo_netmap(const npf_cache_t *npc, const npf_natpolicy_t *np,
402 1.46 rmind const unsigned which, npf_addr_t *addr)
403 1.46 rmind {
404 1.46 rmind const npf_addr_t *orig_addr = npc->npc_ips[which];
405 1.46 rmind
406 1.46 rmind /*
407 1.46 rmind * NETMAP:
408 1.46 rmind *
409 1.46 rmind * addr = net-addr | (orig-addr & ~mask)
410 1.46 rmind */
411 1.46 rmind npf_addr_mask(&np->n_taddr, np->n_tmask, npc->npc_alen, addr);
412 1.46 rmind npf_addr_bitor(orig_addr, np->n_tmask, npc->npc_alen, addr);
413 1.46 rmind }
414 1.46 rmind
415 1.46 rmind static inline npf_addr_t *
416 1.46 rmind npf_nat_getaddr(npf_cache_t *npc, npf_natpolicy_t *np, const unsigned alen)
417 1.46 rmind {
418 1.46 rmind npf_tableset_t *ts = npf_config_tableset(np->n_npfctx);
419 1.46 rmind npf_table_t *t = npf_tableset_getbyid(ts, np->n_tid);
420 1.46 rmind unsigned idx;
421 1.46 rmind
422 1.46 rmind /*
423 1.46 rmind * Dynamically select the translation IP address.
424 1.46 rmind */
425 1.46 rmind switch (np->n_algo) {
426 1.46 rmind case NPF_ALGO_RR:
427 1.46 rmind idx = atomic_inc_uint_nv(&np->n_rr_idx);
428 1.46 rmind break;
429 1.46 rmind case NPF_ALGO_IPHASH:
430 1.46 rmind default:
431 1.46 rmind idx = npf_addr_mix(alen,
432 1.46 rmind npc->npc_ips[NPF_SRC],
433 1.46 rmind npc->npc_ips[NPF_DST]);
434 1.46 rmind break;
435 1.46 rmind }
436 1.46 rmind return npf_table_getsome(t, alen, idx);
437 1.46 rmind }
438 1.46 rmind
439 1.2 rmind /*
440 1.2 rmind * npf_nat_create: create a new NAT translation entry.
441 1.1 rmind */
442 1.2 rmind static npf_nat_t *
443 1.29 rmind npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np, npf_conn_t *con)
444 1.1 rmind {
445 1.19 rmind const int proto = npc->npc_proto;
446 1.45 rmind const unsigned alen = npc->npc_alen;
447 1.45 rmind npf_addr_t *taddr;
448 1.2 rmind npf_nat_t *nt;
449 1.2 rmind
450 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
451 1.7 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
452 1.3 rmind
453 1.29 rmind /* Construct a new NAT entry and associate it with the connection. */
454 1.2 rmind nt = pool_cache_get(nat_cache, PR_NOWAIT);
455 1.46 rmind if (__predict_false(!nt)) {
456 1.2 rmind return NULL;
457 1.2 rmind }
458 1.41 christos npf_stats_inc(npc->npc_ctx, NPF_STAT_NAT_CREATE);
459 1.5 rmind nt->nt_natpolicy = np;
460 1.29 rmind nt->nt_conn = con;
461 1.5 rmind nt->nt_alg = NULL;
462 1.5 rmind
463 1.46 rmind /*
464 1.46 rmind * Select the translation address.
465 1.46 rmind */
466 1.46 rmind if (np->n_flags & NPF_NAT_USETABLE) {
467 1.46 rmind taddr = npf_nat_getaddr(npc, np, alen);
468 1.46 rmind if (__predict_false(!taddr)) {
469 1.46 rmind pool_cache_put(nat_cache, nt);
470 1.46 rmind return NULL;
471 1.46 rmind }
472 1.46 rmind memcpy(&nt->nt_taddr, taddr, alen);
473 1.46 rmind } else if (np->n_algo == NPF_ALGO_NETMAP) {
474 1.46 rmind const unsigned which = npf_nat_which(np->n_type, true);
475 1.46 rmind npf_nat_algo_netmap(npc, np, which, &nt->nt_taddr);
476 1.46 rmind taddr = &nt->nt_taddr;
477 1.46 rmind } else {
478 1.46 rmind /* Static IP address. */
479 1.46 rmind taddr = &np->n_taddr;
480 1.46 rmind memcpy(&nt->nt_taddr, taddr, alen);
481 1.46 rmind }
482 1.46 rmind nt->nt_alen = alen;
483 1.45 rmind
484 1.2 rmind /* Save the original address which may be rewritten. */
485 1.2 rmind if (np->n_type == NPF_NATOUT) {
486 1.23 rmind /* Outbound NAT: source (think internal) address. */
487 1.45 rmind memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_SRC], alen);
488 1.2 rmind } else {
489 1.23 rmind /* Inbound NAT: destination (think external) address. */
490 1.2 rmind KASSERT(np->n_type == NPF_NATIN);
491 1.45 rmind memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_DST], alen);
492 1.2 rmind }
493 1.2 rmind
494 1.2 rmind /*
495 1.2 rmind * Port translation, if required, and if it is TCP/UDP.
496 1.2 rmind */
497 1.2 rmind if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
498 1.2 rmind (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
499 1.2 rmind nt->nt_oport = 0;
500 1.2 rmind nt->nt_tport = 0;
501 1.12 rmind goto out;
502 1.2 rmind }
503 1.12 rmind
504 1.3 rmind /* Save the relevant TCP/UDP port. */
505 1.3 rmind if (proto == IPPROTO_TCP) {
506 1.18 rmind const struct tcphdr *th = npc->npc_l4.tcp;
507 1.3 rmind nt->nt_oport = (np->n_type == NPF_NATOUT) ?
508 1.3 rmind th->th_sport : th->th_dport;
509 1.2 rmind } else {
510 1.18 rmind const struct udphdr *uh = npc->npc_l4.udp;
511 1.3 rmind nt->nt_oport = (np->n_type == NPF_NATOUT) ?
512 1.3 rmind uh->uh_sport : uh->uh_dport;
513 1.2 rmind }
514 1.3 rmind
515 1.2 rmind /* Get a new port for translation. */
516 1.2 rmind if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
517 1.46 rmind nt->nt_tport = npf_portmap_get(np->n_npfctx, alen, taddr);
518 1.2 rmind } else {
519 1.2 rmind nt->nt_tport = np->n_tport;
520 1.2 rmind }
521 1.12 rmind out:
522 1.12 rmind mutex_enter(&np->n_lock);
523 1.12 rmind LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
524 1.12 rmind mutex_exit(&np->n_lock);
525 1.2 rmind return nt;
526 1.2 rmind }
527 1.2 rmind
528 1.2 rmind /*
529 1.24 rmind * npf_nat_translate: perform translation given the state data.
530 1.24 rmind */
531 1.26 rmind static inline int
532 1.30 rmind npf_nat_translate(npf_cache_t *npc, npf_nat_t *nt, bool forw)
533 1.24 rmind {
534 1.24 rmind const npf_natpolicy_t *np = nt->nt_natpolicy;
535 1.46 rmind const unsigned which = npf_nat_which(np->n_type, forw);
536 1.24 rmind const npf_addr_t *addr;
537 1.24 rmind in_port_t port;
538 1.24 rmind
539 1.24 rmind KASSERT(npf_iscached(npc, NPC_IP46));
540 1.24 rmind KASSERT(npf_iscached(npc, NPC_LAYER4));
541 1.24 rmind
542 1.24 rmind if (forw) {
543 1.24 rmind /* "Forwards" stream: use translation address/port. */
544 1.45 rmind addr = &nt->nt_taddr;
545 1.24 rmind port = nt->nt_tport;
546 1.24 rmind } else {
547 1.24 rmind /* "Backwards" stream: use original address/port. */
548 1.24 rmind addr = &nt->nt_oaddr;
549 1.24 rmind port = nt->nt_oport;
550 1.24 rmind }
551 1.24 rmind KASSERT((np->n_flags & NPF_NAT_PORTS) != 0 || port == 0);
552 1.24 rmind
553 1.26 rmind /* Execute ALG translation first. */
554 1.24 rmind if ((npc->npc_info & NPC_ALG_EXEC) == 0) {
555 1.24 rmind npc->npc_info |= NPC_ALG_EXEC;
556 1.30 rmind npf_alg_exec(npc, nt, forw);
557 1.30 rmind npf_recache(npc);
558 1.24 rmind }
559 1.30 rmind KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
560 1.24 rmind
561 1.24 rmind /* Finally, perform the translation. */
562 1.26 rmind return npf_napt_rwr(npc, which, addr, port);
563 1.24 rmind }
564 1.24 rmind
565 1.24 rmind /*
566 1.25 rmind * npf_nat_algo: perform the translation given the algorithm.
567 1.25 rmind */
568 1.29 rmind static inline int
569 1.25 rmind npf_nat_algo(npf_cache_t *npc, const npf_natpolicy_t *np, bool forw)
570 1.25 rmind {
571 1.46 rmind const unsigned which = npf_nat_which(np->n_type, forw);
572 1.46 rmind const npf_addr_t *taddr;
573 1.45 rmind npf_addr_t addr;
574 1.45 rmind
575 1.45 rmind KASSERT(np->n_flags & NPF_NAT_STATIC);
576 1.25 rmind
577 1.25 rmind switch (np->n_algo) {
578 1.45 rmind case NPF_ALGO_NETMAP:
579 1.46 rmind npf_nat_algo_netmap(npc, np, which, &addr);
580 1.45 rmind taddr = &addr;
581 1.45 rmind break;
582 1.25 rmind case NPF_ALGO_NPT66:
583 1.45 rmind return npf_npt66_rwr(npc, which, &np->n_taddr,
584 1.25 rmind np->n_tmask, np->n_npt66_adj);
585 1.25 rmind default:
586 1.45 rmind taddr = &np->n_taddr;
587 1.25 rmind break;
588 1.25 rmind }
589 1.45 rmind return npf_napt_rwr(npc, which, taddr, np->n_tport);
590 1.31 rmind }
591 1.25 rmind
592 1.25 rmind /*
593 1.2 rmind * npf_do_nat:
594 1.45 rmind *
595 1.29 rmind * - Inspect packet for a NAT policy, unless a connection with a NAT
596 1.4 rmind * association already exists. In such case, determine whether it
597 1.2 rmind * is a "forwards" or "backwards" stream.
598 1.4 rmind * - Perform translation: rewrite source or destination fields,
599 1.4 rmind * depending on translation type and direction.
600 1.29 rmind * - Associate a NAT policy with a connection (may establish a new).
601 1.2 rmind */
602 1.2 rmind int
603 1.30 rmind npf_do_nat(npf_cache_t *npc, npf_conn_t *con, const int di)
604 1.2 rmind {
605 1.30 rmind nbuf_t *nbuf = npc->npc_nbuf;
606 1.29 rmind npf_conn_t *ncon = NULL;
607 1.1 rmind npf_natpolicy_t *np;
608 1.1 rmind npf_nat_t *nt;
609 1.1 rmind int error;
610 1.22 rmind bool forw;
611 1.1 rmind
612 1.43 maxv /* All relevant data should be already cached. */
613 1.3 rmind if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
614 1.1 rmind return 0;
615 1.1 rmind }
616 1.18 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
617 1.1 rmind
618 1.2 rmind /*
619 1.29 rmind * Return the NAT entry associated with the connection, if any.
620 1.3 rmind * Determines whether the stream is "forwards" or "backwards".
621 1.29 rmind * Note: no need to lock, since reference on connection is held.
622 1.2 rmind */
623 1.36 rmind if (con && (nt = npf_conn_getnat(con, di, &forw)) != NULL) {
624 1.1 rmind np = nt->nt_natpolicy;
625 1.2 rmind goto translate;
626 1.1 rmind }
627 1.1 rmind
628 1.6 rmind /*
629 1.29 rmind * Inspect the packet for a NAT policy, if there is no connection.
630 1.19 rmind * Note: acquires a reference if found.
631 1.6 rmind */
632 1.30 rmind np = npf_nat_inspect(npc, di);
633 1.1 rmind if (np == NULL) {
634 1.1 rmind /* If packet does not match - done. */
635 1.1 rmind return 0;
636 1.1 rmind }
637 1.2 rmind forw = true;
638 1.1 rmind
639 1.24 rmind /* Static NAT - just perform the translation. */
640 1.24 rmind if (np->n_flags & NPF_NAT_STATIC) {
641 1.24 rmind if (nbuf_cksum_barrier(nbuf, di)) {
642 1.30 rmind npf_recache(npc);
643 1.24 rmind }
644 1.25 rmind error = npf_nat_algo(npc, np, forw);
645 1.24 rmind atomic_dec_uint(&np->n_refcnt);
646 1.24 rmind return error;
647 1.24 rmind }
648 1.24 rmind
649 1.4 rmind /*
650 1.29 rmind * If there is no local connection (no "stateful" rule - unusual,
651 1.29 rmind * but possible configuration), establish one before translation.
652 1.29 rmind * Note that it is not a "pass" connection, therefore passing of
653 1.29 rmind * "backwards" stream depends on other, stateless filtering rules.
654 1.29 rmind */
655 1.29 rmind if (con == NULL) {
656 1.30 rmind ncon = npf_conn_establish(npc, di, true);
657 1.29 rmind if (ncon == NULL) {
658 1.22 rmind atomic_dec_uint(&np->n_refcnt);
659 1.22 rmind return ENOMEM;
660 1.1 rmind }
661 1.29 rmind con = ncon;
662 1.1 rmind }
663 1.22 rmind
664 1.22 rmind /*
665 1.29 rmind * Create a new NAT entry and associate with the connection.
666 1.22 rmind * We will consume the reference on success (release on error).
667 1.22 rmind */
668 1.29 rmind nt = npf_nat_create(npc, np, con);
669 1.22 rmind if (nt == NULL) {
670 1.22 rmind atomic_dec_uint(&np->n_refcnt);
671 1.22 rmind error = ENOMEM;
672 1.22 rmind goto out;
673 1.22 rmind }
674 1.22 rmind
675 1.29 rmind /* Associate the NAT translation entry with the connection. */
676 1.29 rmind error = npf_conn_setnat(npc, con, nt, np->n_type);
677 1.2 rmind if (error) {
678 1.22 rmind /* Will release the reference. */
679 1.22 rmind npf_nat_destroy(nt);
680 1.1 rmind goto out;
681 1.1 rmind }
682 1.1 rmind
683 1.22 rmind /* Determine whether any ALG matches. */
684 1.30 rmind if (npf_alg_match(npc, nt, di)) {
685 1.22 rmind KASSERT(nt->nt_alg != NULL);
686 1.22 rmind }
687 1.22 rmind
688 1.22 rmind translate:
689 1.23 rmind /* May need to process the delayed checksums first (XXX: NetBSD). */
690 1.23 rmind if (nbuf_cksum_barrier(nbuf, di)) {
691 1.30 rmind npf_recache(npc);
692 1.23 rmind }
693 1.23 rmind
694 1.22 rmind /* Perform the translation. */
695 1.30 rmind error = npf_nat_translate(npc, nt, forw);
696 1.1 rmind out:
697 1.29 rmind if (__predict_false(ncon)) {
698 1.24 rmind if (error) {
699 1.24 rmind /* It created for NAT - just expire. */
700 1.29 rmind npf_conn_expire(ncon);
701 1.24 rmind }
702 1.29 rmind npf_conn_release(ncon);
703 1.1 rmind }
704 1.1 rmind return error;
705 1.1 rmind }
706 1.1 rmind
707 1.1 rmind /*
708 1.4 rmind * npf_nat_gettrans: return translation IP address and port.
709 1.4 rmind */
710 1.4 rmind void
711 1.4 rmind npf_nat_gettrans(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
712 1.4 rmind {
713 1.45 rmind *addr = &nt->nt_taddr;
714 1.4 rmind *port = nt->nt_tport;
715 1.4 rmind }
716 1.4 rmind
717 1.4 rmind /*
718 1.2 rmind * npf_nat_getorig: return original IP address and port from translation entry.
719 1.1 rmind */
720 1.1 rmind void
721 1.3 rmind npf_nat_getorig(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
722 1.1 rmind {
723 1.3 rmind *addr = &nt->nt_oaddr;
724 1.2 rmind *port = nt->nt_oport;
725 1.1 rmind }
726 1.1 rmind
727 1.3 rmind /*
728 1.3 rmind * npf_nat_setalg: associate an ALG with the NAT entry.
729 1.3 rmind */
730 1.1 rmind void
731 1.1 rmind npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
732 1.1 rmind {
733 1.1 rmind nt->nt_alg = alg;
734 1.1 rmind nt->nt_alg_arg = arg;
735 1.1 rmind }
736 1.1 rmind
737 1.1 rmind /*
738 1.29 rmind * npf_nat_destroy: destroy NAT structure (performed on connection expiration).
739 1.1 rmind */
740 1.1 rmind void
741 1.22 rmind npf_nat_destroy(npf_nat_t *nt)
742 1.1 rmind {
743 1.2 rmind npf_natpolicy_t *np = nt->nt_natpolicy;
744 1.46 rmind npf_t *npf = np->n_npfctx;
745 1.1 rmind
746 1.46 rmind /* Return taken port to the portmap. */
747 1.4 rmind if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
748 1.46 rmind npf_portmap_put(npf, nt->nt_alen, &nt->nt_taddr, nt->nt_tport);
749 1.1 rmind }
750 1.41 christos npf_stats_inc(np->n_npfctx, NPF_STAT_NAT_DESTROY);
751 1.4 rmind
752 1.4 rmind mutex_enter(&np->n_lock);
753 1.4 rmind LIST_REMOVE(nt, nt_entry);
754 1.34 rmind KASSERT(np->n_refcnt > 0);
755 1.19 rmind atomic_dec_uint(&np->n_refcnt);
756 1.4 rmind mutex_exit(&np->n_lock);
757 1.1 rmind pool_cache_put(nat_cache, nt);
758 1.4 rmind }
759 1.4 rmind
760 1.4 rmind /*
761 1.31 rmind * npf_nat_export: serialise the NAT entry with a NAT policy ID.
762 1.4 rmind */
763 1.31 rmind void
764 1.44 rmind npf_nat_export(nvlist_t *condict, npf_nat_t *nt)
765 1.4 rmind {
766 1.4 rmind npf_natpolicy_t *np = nt->nt_natpolicy;
767 1.44 rmind nvlist_t *nat;
768 1.4 rmind
769 1.44 rmind nat = nvlist_create(0);
770 1.44 rmind nvlist_add_binary(nat, "oaddr", &nt->nt_oaddr, sizeof(npf_addr_t));
771 1.44 rmind nvlist_add_number(nat, "oport", nt->nt_oport);
772 1.44 rmind nvlist_add_number(nat, "tport", nt->nt_tport);
773 1.44 rmind nvlist_add_number(nat, "nat-policy", np->n_id);
774 1.44 rmind nvlist_move_nvlist(condict, "nat", nat);
775 1.4 rmind }
776 1.4 rmind
777 1.4 rmind /*
778 1.31 rmind * npf_nat_import: find the NAT policy and unserialise the NAT entry.
779 1.4 rmind */
780 1.4 rmind npf_nat_t *
781 1.44 rmind npf_nat_import(npf_t *npf, const nvlist_t *nat,
782 1.41 christos npf_ruleset_t *natlist, npf_conn_t *con)
783 1.4 rmind {
784 1.4 rmind npf_natpolicy_t *np;
785 1.4 rmind npf_nat_t *nt;
786 1.44 rmind const void *oaddr;
787 1.31 rmind uint64_t np_id;
788 1.44 rmind size_t len;
789 1.4 rmind
790 1.44 rmind np_id = dnvlist_get_number(nat, "nat-policy", UINT64_MAX);
791 1.31 rmind if ((np = npf_ruleset_findnat(natlist, np_id)) == NULL) {
792 1.4 rmind return NULL;
793 1.4 rmind }
794 1.31 rmind nt = pool_cache_get(nat_cache, PR_WAITOK);
795 1.31 rmind memset(nt, 0, sizeof(npf_nat_t));
796 1.4 rmind
797 1.44 rmind oaddr = dnvlist_get_binary(nat, "oaddr", &len, NULL, 0);
798 1.44 rmind if (!oaddr || len != sizeof(npf_addr_t)) {
799 1.31 rmind pool_cache_put(nat_cache, nt);
800 1.4 rmind return NULL;
801 1.4 rmind }
802 1.44 rmind memcpy(&nt->nt_oaddr, oaddr, sizeof(npf_addr_t));
803 1.44 rmind nt->nt_oport = dnvlist_get_number(nat, "oport", 0);
804 1.44 rmind nt->nt_tport = dnvlist_get_number(nat, "tport", 0);
805 1.4 rmind
806 1.4 rmind /* Take a specific port from port-map. */
807 1.42 christos if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport &&
808 1.46 rmind !npf_portmap_take(npf, nt->nt_alen, &nt->nt_taddr, nt->nt_tport)) {
809 1.31 rmind pool_cache_put(nat_cache, nt);
810 1.4 rmind return NULL;
811 1.4 rmind }
812 1.41 christos npf_stats_inc(npf, NPF_STAT_NAT_CREATE);
813 1.4 rmind
814 1.34 rmind /*
815 1.34 rmind * Associate, take a reference and insert. Unlocked since
816 1.34 rmind * the policy is not yet visible.
817 1.34 rmind */
818 1.4 rmind nt->nt_natpolicy = np;
819 1.29 rmind nt->nt_conn = con;
820 1.34 rmind np->n_refcnt++;
821 1.34 rmind LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
822 1.4 rmind return nt;
823 1.1 rmind }
824 1.1 rmind
825 1.1 rmind #if defined(DDB) || defined(_NPF_TESTING)
826 1.1 rmind
827 1.1 rmind void
828 1.14 rmind npf_nat_dump(const npf_nat_t *nt)
829 1.1 rmind {
830 1.14 rmind const npf_natpolicy_t *np;
831 1.1 rmind struct in_addr ip;
832 1.1 rmind
833 1.4 rmind np = nt->nt_natpolicy;
834 1.45 rmind memcpy(&ip, &nt->nt_taddr, sizeof(ip));
835 1.46 rmind printf("\tNATP(%p): type %u flags 0x%x taddr %s tport %d\n", np,
836 1.38 rmind np->n_type, np->n_flags, inet_ntoa(ip), ntohs(np->n_tport));
837 1.4 rmind memcpy(&ip, &nt->nt_oaddr, sizeof(ip));
838 1.4 rmind printf("\tNAT: original address %s oport %d tport %d\n",
839 1.4 rmind inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
840 1.4 rmind if (nt->nt_alg) {
841 1.4 rmind printf("\tNAT ALG = %p, ARG = %p\n",
842 1.4 rmind nt->nt_alg, (void *)nt->nt_alg_arg);
843 1.1 rmind }
844 1.1 rmind }
845 1.1 rmind
846 1.1 rmind #endif
847