npf_nat.c revision 1.52 1 1.1 rmind /*-
2 1.50 rmind * Copyright (c) 2014-2020 Mindaugas Rasiukevicius <rmind at noxt eu>
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.52 riastrad __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.52 2022/04/09 23:38:33 riastradh 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.50 rmind 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.50 rmind * NAT 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.50 rmind uint16_t nt_ifid;
135 1.50 rmind uint16_t nt_alen;
136 1.50 rmind
137 1.28 rmind /*
138 1.45 rmind * Translation address as well as the original address which is
139 1.45 rmind * used for backwards translation. The same for ports.
140 1.28 rmind */
141 1.45 rmind npf_addr_t nt_taddr;
142 1.4 rmind npf_addr_t nt_oaddr;
143 1.45 rmind
144 1.4 rmind in_port_t nt_oport;
145 1.4 rmind in_port_t nt_tport;
146 1.28 rmind
147 1.1 rmind /* ALG (if any) associated with this NAT entry. */
148 1.4 rmind npf_alg_t * nt_alg;
149 1.4 rmind uintptr_t nt_alg_arg;
150 1.28 rmind
151 1.28 rmind LIST_ENTRY(npf_nat) nt_entry;
152 1.29 rmind npf_conn_t * nt_conn;
153 1.1 rmind };
154 1.1 rmind
155 1.4 rmind static pool_cache_t nat_cache __read_mostly;
156 1.1 rmind
157 1.1 rmind /*
158 1.50 rmind * npf_nat_sys{init,fini}: initialize/destroy NAT subsystem structures.
159 1.1 rmind */
160 1.1 rmind
161 1.1 rmind void
162 1.1 rmind npf_nat_sysinit(void)
163 1.1 rmind {
164 1.45 rmind nat_cache = pool_cache_init(sizeof(npf_nat_t), 0,
165 1.1 rmind 0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
166 1.1 rmind KASSERT(nat_cache != NULL);
167 1.1 rmind }
168 1.1 rmind
169 1.1 rmind void
170 1.1 rmind npf_nat_sysfini(void)
171 1.1 rmind {
172 1.23 rmind /* All NAT policies should already be destroyed. */
173 1.1 rmind pool_cache_destroy(nat_cache);
174 1.1 rmind }
175 1.1 rmind
176 1.1 rmind /*
177 1.50 rmind * npf_natpolicy_create: create a new NAT policy.
178 1.1 rmind */
179 1.1 rmind npf_natpolicy_t *
180 1.50 rmind npf_natpolicy_create(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *rset)
181 1.1 rmind {
182 1.5 rmind npf_natpolicy_t *np;
183 1.44 rmind const void *addr;
184 1.44 rmind size_t len;
185 1.1 rmind
186 1.1 rmind np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
187 1.49 rmind atomic_store_relaxed(&np->n_refcnt, 1);
188 1.41 christos np->n_npfctx = npf;
189 1.4 rmind
190 1.33 rmind /* The translation type, flags and policy ID. */
191 1.44 rmind np->n_type = dnvlist_get_number(nat, "type", 0);
192 1.45 rmind np->n_flags = dnvlist_get_number(nat, "flags", 0) & ~NPF_NAT_PRIVMASK;
193 1.44 rmind np->n_id = dnvlist_get_number(nat, "nat-policy", 0);
194 1.10 rmind
195 1.10 rmind /* Should be exclusively either inbound or outbound NAT. */
196 1.10 rmind if (((np->n_type == NPF_NATIN) ^ (np->n_type == NPF_NATOUT)) == 0) {
197 1.25 rmind goto err;
198 1.10 rmind }
199 1.10 rmind mutex_init(&np->n_lock, MUTEX_DEFAULT, IPL_SOFTNET);
200 1.10 rmind LIST_INIT(&np->n_nat_list);
201 1.4 rmind
202 1.45 rmind /*
203 1.46 rmind * Translation IP, mask and port (if applicable). If using the
204 1.46 rmind * the table, specified by the ID, then the nat-addr/nat-mask will
205 1.46 rmind * be used as a filter for the addresses selected from table.
206 1.45 rmind */
207 1.45 rmind if (nvlist_exists_number(nat, "nat-table-id")) {
208 1.45 rmind if (np->n_flags & NPF_NAT_STATIC) {
209 1.45 rmind goto err;
210 1.45 rmind }
211 1.45 rmind np->n_tid = nvlist_get_number(nat, "nat-table-id");
212 1.45 rmind np->n_tmask = NPF_NO_NETMASK;
213 1.45 rmind np->n_flags |= NPF_NAT_USETABLE;
214 1.45 rmind } else {
215 1.46 rmind addr = dnvlist_get_binary(nat, "nat-addr", &len, NULL, 0);
216 1.45 rmind if (!addr || len == 0 || len > sizeof(npf_addr_t)) {
217 1.45 rmind goto err;
218 1.45 rmind }
219 1.45 rmind memcpy(&np->n_taddr, addr, len);
220 1.45 rmind np->n_alen = len;
221 1.46 rmind np->n_tmask = dnvlist_get_number(nat, "nat-mask", NPF_NO_NETMASK);
222 1.46 rmind if (npf_netmask_check(np->n_alen, np->n_tmask)) {
223 1.46 rmind goto err;
224 1.46 rmind }
225 1.25 rmind }
226 1.44 rmind np->n_tport = dnvlist_get_number(nat, "nat-port", 0);
227 1.4 rmind
228 1.45 rmind /*
229 1.45 rmind * NAT algorithm.
230 1.45 rmind */
231 1.44 rmind np->n_algo = dnvlist_get_number(nat, "nat-algo", 0);
232 1.25 rmind switch (np->n_algo) {
233 1.25 rmind case NPF_ALGO_NPT66:
234 1.44 rmind np->n_npt66_adj = dnvlist_get_number(nat, "npt66-adj", 0);
235 1.25 rmind break;
236 1.45 rmind case NPF_ALGO_NETMAP:
237 1.45 rmind break;
238 1.45 rmind case NPF_ALGO_IPHASH:
239 1.45 rmind case NPF_ALGO_RR:
240 1.25 rmind default:
241 1.46 rmind if (np->n_tmask != NPF_NO_NETMASK) {
242 1.25 rmind goto err;
243 1.46 rmind }
244 1.25 rmind break;
245 1.25 rmind }
246 1.1 rmind return np;
247 1.25 rmind err:
248 1.39 christos mutex_destroy(&np->n_lock);
249 1.25 rmind kmem_free(np, sizeof(npf_natpolicy_t));
250 1.25 rmind return NULL;
251 1.1 rmind }
252 1.1 rmind
253 1.32 rmind int
254 1.50 rmind npf_natpolicy_export(const npf_natpolicy_t *np, nvlist_t *nat)
255 1.32 rmind {
256 1.45 rmind nvlist_add_number(nat, "nat-policy", np->n_id);
257 1.44 rmind nvlist_add_number(nat, "type", np->n_type);
258 1.44 rmind nvlist_add_number(nat, "flags", np->n_flags);
259 1.32 rmind
260 1.45 rmind if (np->n_flags & NPF_NAT_USETABLE) {
261 1.45 rmind nvlist_add_number(nat, "nat-table-id", np->n_tid);
262 1.45 rmind } else {
263 1.46 rmind nvlist_add_binary(nat, "nat-addr", &np->n_taddr, np->n_alen);
264 1.45 rmind nvlist_add_number(nat, "nat-mask", np->n_tmask);
265 1.45 rmind }
266 1.44 rmind nvlist_add_number(nat, "nat-port", np->n_tport);
267 1.44 rmind nvlist_add_number(nat, "nat-algo", np->n_algo);
268 1.32 rmind
269 1.32 rmind switch (np->n_algo) {
270 1.32 rmind case NPF_ALGO_NPT66:
271 1.44 rmind nvlist_add_number(nat, "npt66-adj", np->n_npt66_adj);
272 1.32 rmind break;
273 1.32 rmind }
274 1.32 rmind return 0;
275 1.32 rmind }
276 1.32 rmind
277 1.49 rmind static void
278 1.49 rmind npf_natpolicy_release(npf_natpolicy_t *np)
279 1.49 rmind {
280 1.49 rmind KASSERT(atomic_load_relaxed(&np->n_refcnt) > 0);
281 1.49 rmind
282 1.51 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
283 1.52 riastrad membar_release();
284 1.51 riastrad #endif
285 1.49 rmind if (atomic_dec_uint_nv(&np->n_refcnt) != 0) {
286 1.49 rmind return;
287 1.49 rmind }
288 1.51 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
289 1.52 riastrad membar_acquire();
290 1.51 riastrad #endif
291 1.49 rmind KASSERT(LIST_EMPTY(&np->n_nat_list));
292 1.49 rmind mutex_destroy(&np->n_lock);
293 1.49 rmind kmem_free(np, sizeof(npf_natpolicy_t));
294 1.49 rmind }
295 1.49 rmind
296 1.1 rmind /*
297 1.50 rmind * npf_natpolicy_destroy: free the NAT policy.
298 1.1 rmind *
299 1.4 rmind * => Called from npf_rule_free() during the reload via npf_ruleset_destroy().
300 1.49 rmind * => At this point, NAT policy cannot acquire new references.
301 1.1 rmind */
302 1.1 rmind void
303 1.50 rmind npf_natpolicy_destroy(npf_natpolicy_t *np)
304 1.1 rmind {
305 1.22 rmind /*
306 1.49 rmind * Drain the references. If there are active NAT connections,
307 1.49 rmind * then expire them and kick the worker.
308 1.22 rmind */
309 1.49 rmind if (atomic_load_relaxed(&np->n_refcnt) > 1) {
310 1.49 rmind npf_nat_t *nt;
311 1.49 rmind
312 1.28 rmind mutex_enter(&np->n_lock);
313 1.28 rmind LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
314 1.49 rmind npf_conn_t *con = nt->nt_conn;
315 1.29 rmind KASSERT(con != NULL);
316 1.29 rmind npf_conn_expire(con);
317 1.28 rmind }
318 1.28 rmind mutex_exit(&np->n_lock);
319 1.41 christos npf_worker_signal(np->n_npfctx);
320 1.19 rmind }
321 1.49 rmind KASSERT(atomic_load_relaxed(&np->n_refcnt) >= 1);
322 1.49 rmind
323 1.49 rmind /*
324 1.49 rmind * Drop the initial reference, but it might not be the last one.
325 1.49 rmind * If so, the last reference will be triggered via:
326 1.49 rmind *
327 1.49 rmind * npf_conn_destroy() -> npf_nat_destroy() -> npf_natpolicy_release()
328 1.49 rmind */
329 1.49 rmind npf_natpolicy_release(np);
330 1.1 rmind }
331 1.1 rmind
332 1.13 rmind void
333 1.15 rmind npf_nat_freealg(npf_natpolicy_t *np, npf_alg_t *alg)
334 1.13 rmind {
335 1.15 rmind npf_nat_t *nt;
336 1.15 rmind
337 1.15 rmind mutex_enter(&np->n_lock);
338 1.15 rmind LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
339 1.46 rmind if (nt->nt_alg == alg) {
340 1.50 rmind npf_alg_destroy(np->n_npfctx, alg, nt, nt->nt_conn);
341 1.31 rmind nt->nt_alg = NULL;
342 1.46 rmind }
343 1.15 rmind }
344 1.15 rmind mutex_exit(&np->n_lock);
345 1.13 rmind }
346 1.13 rmind
347 1.5 rmind /*
348 1.50 rmind * npf_natpolicy_cmp: compare two NAT policies.
349 1.5 rmind *
350 1.5 rmind * => Return 0 on match, and non-zero otherwise.
351 1.5 rmind */
352 1.4 rmind bool
353 1.50 rmind npf_natpolicy_cmp(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
354 1.1 rmind {
355 1.31 rmind const void *np_raw, *mnp_raw;
356 1.31 rmind
357 1.4 rmind /*
358 1.50 rmind * Compare the relevant NAT policy information (in its raw form)
359 1.50 rmind * that is enough as a matching criteria.
360 1.4 rmind */
361 1.5 rmind KASSERT(np && mnp && np != mnp);
362 1.31 rmind np_raw = (const uint8_t *)np + NPF_NP_CMP_START;
363 1.31 rmind mnp_raw = (const uint8_t *)mnp + NPF_NP_CMP_START;
364 1.31 rmind return memcmp(np_raw, mnp_raw, NPF_NP_CMP_SIZE) == 0;
365 1.1 rmind }
366 1.1 rmind
367 1.31 rmind void
368 1.31 rmind npf_nat_setid(npf_natpolicy_t *np, uint64_t id)
369 1.31 rmind {
370 1.31 rmind np->n_id = id;
371 1.31 rmind }
372 1.31 rmind
373 1.31 rmind uint64_t
374 1.31 rmind npf_nat_getid(const npf_natpolicy_t *np)
375 1.31 rmind {
376 1.31 rmind return np->n_id;
377 1.31 rmind }
378 1.31 rmind
379 1.1 rmind /*
380 1.23 rmind * npf_nat_which: tell which address (source or destination) should be
381 1.23 rmind * rewritten given the combination of the NAT type and flow direction.
382 1.50 rmind *
383 1.50 rmind * => Returns NPF_SRC or NPF_DST constant.
384 1.23 rmind */
385 1.46 rmind static inline unsigned
386 1.50 rmind npf_nat_which(const unsigned type, const npf_flow_t flow)
387 1.23 rmind {
388 1.50 rmind unsigned which;
389 1.50 rmind
390 1.50 rmind /* The logic below relies on these values being 0 or 1. */
391 1.50 rmind CTASSERT(NPF_SRC == 0 && NPF_DST == 1);
392 1.50 rmind CTASSERT(NPF_FLOW_FORW == NPF_SRC && NPF_FLOW_BACK == NPF_DST);
393 1.50 rmind
394 1.50 rmind KASSERT(type == NPF_NATIN || type == NPF_NATOUT);
395 1.50 rmind KASSERT(flow == NPF_FLOW_FORW || flow == NPF_FLOW_BACK);
396 1.50 rmind
397 1.23 rmind /*
398 1.23 rmind * Outbound NAT rewrites:
399 1.50 rmind *
400 1.24 rmind * - Source (NPF_SRC) on "forwards" stream.
401 1.24 rmind * - Destination (NPF_DST) on "backwards" stream.
402 1.50 rmind *
403 1.23 rmind * Inbound NAT is other way round.
404 1.23 rmind */
405 1.50 rmind which = (type == NPF_NATOUT) ? flow : !flow;
406 1.50 rmind KASSERT(which == NPF_SRC || which == NPF_DST);
407 1.50 rmind return which;
408 1.23 rmind }
409 1.23 rmind
410 1.23 rmind /*
411 1.2 rmind * npf_nat_inspect: inspect packet against NAT ruleset and return a policy.
412 1.19 rmind *
413 1.19 rmind * => Acquire a reference on the policy, if found.
414 1.50 rmind * => NAT lookup is protected by EBR.
415 1.2 rmind */
416 1.2 rmind static npf_natpolicy_t *
417 1.50 rmind npf_nat_inspect(npf_cache_t *npc, const unsigned di)
418 1.2 rmind {
419 1.48 rmind npf_t *npf = npc->npc_ctx;
420 1.48 rmind int slock = npf_config_read_enter(npf);
421 1.48 rmind npf_ruleset_t *rlset = npf_config_natset(npf);
422 1.6 rmind npf_natpolicy_t *np;
423 1.2 rmind npf_rule_t *rl;
424 1.2 rmind
425 1.30 rmind rl = npf_ruleset_inspect(npc, rlset, di, NPF_LAYER_3);
426 1.6 rmind if (rl == NULL) {
427 1.48 rmind npf_config_read_exit(npf, slock);
428 1.6 rmind return NULL;
429 1.6 rmind }
430 1.6 rmind np = npf_rule_getnat(rl);
431 1.19 rmind atomic_inc_uint(&np->n_refcnt);
432 1.48 rmind npf_config_read_exit(npf, slock);
433 1.6 rmind return np;
434 1.2 rmind }
435 1.2 rmind
436 1.46 rmind static void
437 1.46 rmind npf_nat_algo_netmap(const npf_cache_t *npc, const npf_natpolicy_t *np,
438 1.46 rmind const unsigned which, npf_addr_t *addr)
439 1.46 rmind {
440 1.46 rmind const npf_addr_t *orig_addr = npc->npc_ips[which];
441 1.46 rmind
442 1.46 rmind /*
443 1.46 rmind * NETMAP:
444 1.46 rmind *
445 1.46 rmind * addr = net-addr | (orig-addr & ~mask)
446 1.46 rmind */
447 1.46 rmind npf_addr_mask(&np->n_taddr, np->n_tmask, npc->npc_alen, addr);
448 1.46 rmind npf_addr_bitor(orig_addr, np->n_tmask, npc->npc_alen, addr);
449 1.46 rmind }
450 1.46 rmind
451 1.46 rmind static inline npf_addr_t *
452 1.46 rmind npf_nat_getaddr(npf_cache_t *npc, npf_natpolicy_t *np, const unsigned alen)
453 1.46 rmind {
454 1.46 rmind npf_tableset_t *ts = npf_config_tableset(np->n_npfctx);
455 1.46 rmind npf_table_t *t = npf_tableset_getbyid(ts, np->n_tid);
456 1.46 rmind unsigned idx;
457 1.46 rmind
458 1.46 rmind /*
459 1.46 rmind * Dynamically select the translation IP address.
460 1.46 rmind */
461 1.46 rmind switch (np->n_algo) {
462 1.46 rmind case NPF_ALGO_RR:
463 1.46 rmind idx = atomic_inc_uint_nv(&np->n_rr_idx);
464 1.46 rmind break;
465 1.46 rmind case NPF_ALGO_IPHASH:
466 1.46 rmind default:
467 1.46 rmind idx = npf_addr_mix(alen,
468 1.46 rmind npc->npc_ips[NPF_SRC],
469 1.46 rmind npc->npc_ips[NPF_DST]);
470 1.46 rmind break;
471 1.46 rmind }
472 1.46 rmind return npf_table_getsome(t, alen, idx);
473 1.46 rmind }
474 1.46 rmind
475 1.2 rmind /*
476 1.2 rmind * npf_nat_create: create a new NAT translation entry.
477 1.50 rmind *
478 1.50 rmind * => The caller must pass the NAT policy with a reference acquired for us.
479 1.1 rmind */
480 1.2 rmind static npf_nat_t *
481 1.29 rmind npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np, npf_conn_t *con)
482 1.1 rmind {
483 1.50 rmind const unsigned proto = npc->npc_proto;
484 1.45 rmind const unsigned alen = npc->npc_alen;
485 1.50 rmind const nbuf_t *nbuf = npc->npc_nbuf;
486 1.48 rmind npf_t *npf = npc->npc_ctx;
487 1.45 rmind npf_addr_t *taddr;
488 1.2 rmind npf_nat_t *nt;
489 1.2 rmind
490 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
491 1.7 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
492 1.3 rmind
493 1.29 rmind /* Construct a new NAT entry and associate it with the connection. */
494 1.2 rmind nt = pool_cache_get(nat_cache, PR_NOWAIT);
495 1.46 rmind if (__predict_false(!nt)) {
496 1.2 rmind return NULL;
497 1.2 rmind }
498 1.48 rmind npf_stats_inc(npf, NPF_STAT_NAT_CREATE);
499 1.5 rmind nt->nt_natpolicy = np;
500 1.29 rmind nt->nt_conn = con;
501 1.5 rmind nt->nt_alg = NULL;
502 1.5 rmind
503 1.46 rmind /*
504 1.50 rmind * Save the interface ID.
505 1.50 rmind *
506 1.50 rmind * Note: this can be different from the given connection if it
507 1.50 rmind * was established on a different interface, using the global state
508 1.50 rmind * mode (state.key.interface = 0).
509 1.50 rmind */
510 1.50 rmind KASSERT(nbuf->nb_ifid != 0);
511 1.50 rmind nt->nt_ifid = nbuf->nb_ifid;
512 1.50 rmind
513 1.50 rmind /*
514 1.46 rmind * Select the translation address.
515 1.46 rmind */
516 1.46 rmind if (np->n_flags & NPF_NAT_USETABLE) {
517 1.48 rmind int slock = npf_config_read_enter(npf);
518 1.46 rmind taddr = npf_nat_getaddr(npc, np, alen);
519 1.46 rmind if (__predict_false(!taddr)) {
520 1.48 rmind npf_config_read_exit(npf, slock);
521 1.46 rmind pool_cache_put(nat_cache, nt);
522 1.46 rmind return NULL;
523 1.46 rmind }
524 1.46 rmind memcpy(&nt->nt_taddr, taddr, alen);
525 1.48 rmind npf_config_read_exit(npf, slock);
526 1.48 rmind
527 1.46 rmind } else if (np->n_algo == NPF_ALGO_NETMAP) {
528 1.50 rmind const unsigned which = npf_nat_which(np->n_type, NPF_FLOW_FORW);
529 1.46 rmind npf_nat_algo_netmap(npc, np, which, &nt->nt_taddr);
530 1.46 rmind taddr = &nt->nt_taddr;
531 1.46 rmind } else {
532 1.46 rmind /* Static IP address. */
533 1.46 rmind taddr = &np->n_taddr;
534 1.46 rmind memcpy(&nt->nt_taddr, taddr, alen);
535 1.46 rmind }
536 1.46 rmind nt->nt_alen = alen;
537 1.45 rmind
538 1.2 rmind /* Save the original address which may be rewritten. */
539 1.2 rmind if (np->n_type == NPF_NATOUT) {
540 1.23 rmind /* Outbound NAT: source (think internal) address. */
541 1.45 rmind memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_SRC], alen);
542 1.2 rmind } else {
543 1.23 rmind /* Inbound NAT: destination (think external) address. */
544 1.2 rmind KASSERT(np->n_type == NPF_NATIN);
545 1.45 rmind memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_DST], alen);
546 1.2 rmind }
547 1.2 rmind
548 1.2 rmind /*
549 1.2 rmind * Port translation, if required, and if it is TCP/UDP.
550 1.2 rmind */
551 1.2 rmind if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
552 1.2 rmind (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
553 1.2 rmind nt->nt_oport = 0;
554 1.2 rmind nt->nt_tport = 0;
555 1.12 rmind goto out;
556 1.2 rmind }
557 1.12 rmind
558 1.3 rmind /* Save the relevant TCP/UDP port. */
559 1.3 rmind if (proto == IPPROTO_TCP) {
560 1.18 rmind const struct tcphdr *th = npc->npc_l4.tcp;
561 1.3 rmind nt->nt_oport = (np->n_type == NPF_NATOUT) ?
562 1.3 rmind th->th_sport : th->th_dport;
563 1.2 rmind } else {
564 1.18 rmind const struct udphdr *uh = npc->npc_l4.udp;
565 1.3 rmind nt->nt_oport = (np->n_type == NPF_NATOUT) ?
566 1.3 rmind uh->uh_sport : uh->uh_dport;
567 1.2 rmind }
568 1.3 rmind
569 1.2 rmind /* Get a new port for translation. */
570 1.2 rmind if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
571 1.47 rmind npf_portmap_t *pm = np->n_npfctx->portmap;
572 1.47 rmind nt->nt_tport = npf_portmap_get(pm, alen, taddr);
573 1.2 rmind } else {
574 1.2 rmind nt->nt_tport = np->n_tport;
575 1.2 rmind }
576 1.12 rmind out:
577 1.12 rmind mutex_enter(&np->n_lock);
578 1.12 rmind LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
579 1.50 rmind /* Note: we also consume the reference on policy. */
580 1.12 rmind mutex_exit(&np->n_lock);
581 1.2 rmind return nt;
582 1.2 rmind }
583 1.2 rmind
584 1.2 rmind /*
585 1.50 rmind * npf_dnat_translate: perform translation given the state data.
586 1.24 rmind */
587 1.26 rmind static inline int
588 1.50 rmind npf_dnat_translate(npf_cache_t *npc, npf_nat_t *nt, npf_flow_t flow)
589 1.24 rmind {
590 1.24 rmind const npf_natpolicy_t *np = nt->nt_natpolicy;
591 1.50 rmind const unsigned which = npf_nat_which(np->n_type, flow);
592 1.24 rmind const npf_addr_t *addr;
593 1.24 rmind in_port_t port;
594 1.24 rmind
595 1.24 rmind KASSERT(npf_iscached(npc, NPC_IP46));
596 1.24 rmind KASSERT(npf_iscached(npc, NPC_LAYER4));
597 1.24 rmind
598 1.50 rmind if (flow == NPF_FLOW_FORW) {
599 1.24 rmind /* "Forwards" stream: use translation address/port. */
600 1.45 rmind addr = &nt->nt_taddr;
601 1.24 rmind port = nt->nt_tport;
602 1.24 rmind } else {
603 1.24 rmind /* "Backwards" stream: use original address/port. */
604 1.24 rmind addr = &nt->nt_oaddr;
605 1.24 rmind port = nt->nt_oport;
606 1.24 rmind }
607 1.24 rmind KASSERT((np->n_flags & NPF_NAT_PORTS) != 0 || port == 0);
608 1.24 rmind
609 1.26 rmind /* Execute ALG translation first. */
610 1.24 rmind if ((npc->npc_info & NPC_ALG_EXEC) == 0) {
611 1.24 rmind npc->npc_info |= NPC_ALG_EXEC;
612 1.50 rmind npf_alg_exec(npc, nt, flow);
613 1.30 rmind npf_recache(npc);
614 1.24 rmind }
615 1.30 rmind KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
616 1.24 rmind
617 1.24 rmind /* Finally, perform the translation. */
618 1.26 rmind return npf_napt_rwr(npc, which, addr, port);
619 1.24 rmind }
620 1.24 rmind
621 1.24 rmind /*
622 1.50 rmind * npf_snat_translate: perform translation given the algorithm.
623 1.25 rmind */
624 1.29 rmind static inline int
625 1.50 rmind npf_snat_translate(npf_cache_t *npc, const npf_natpolicy_t *np, npf_flow_t flow)
626 1.25 rmind {
627 1.50 rmind const unsigned which = npf_nat_which(np->n_type, flow);
628 1.46 rmind const npf_addr_t *taddr;
629 1.45 rmind npf_addr_t addr;
630 1.45 rmind
631 1.45 rmind KASSERT(np->n_flags & NPF_NAT_STATIC);
632 1.25 rmind
633 1.25 rmind switch (np->n_algo) {
634 1.45 rmind case NPF_ALGO_NETMAP:
635 1.46 rmind npf_nat_algo_netmap(npc, np, which, &addr);
636 1.45 rmind taddr = &addr;
637 1.45 rmind break;
638 1.25 rmind case NPF_ALGO_NPT66:
639 1.45 rmind return npf_npt66_rwr(npc, which, &np->n_taddr,
640 1.25 rmind np->n_tmask, np->n_npt66_adj);
641 1.25 rmind default:
642 1.45 rmind taddr = &np->n_taddr;
643 1.25 rmind break;
644 1.25 rmind }
645 1.45 rmind return npf_napt_rwr(npc, which, taddr, np->n_tport);
646 1.31 rmind }
647 1.25 rmind
648 1.25 rmind /*
649 1.50 rmind * Associate NAT policy with an existing connection state.
650 1.50 rmind */
651 1.50 rmind npf_nat_t *
652 1.50 rmind npf_nat_share_policy(npf_cache_t *npc, npf_conn_t *con, npf_nat_t *src_nt)
653 1.50 rmind {
654 1.50 rmind npf_natpolicy_t *np = src_nt->nt_natpolicy;
655 1.50 rmind npf_nat_t *nt;
656 1.50 rmind int ret;
657 1.50 rmind
658 1.50 rmind /* Create a new NAT entry. */
659 1.50 rmind nt = npf_nat_create(npc, np, con);
660 1.50 rmind if (__predict_false(nt == NULL)) {
661 1.50 rmind return NULL;
662 1.50 rmind }
663 1.50 rmind atomic_inc_uint(&np->n_refcnt);
664 1.50 rmind
665 1.50 rmind /* Associate the NAT translation entry with the connection. */
666 1.50 rmind ret = npf_conn_setnat(npc, con, nt, np->n_type);
667 1.50 rmind if (__predict_false(ret)) {
668 1.50 rmind /* Will release the reference. */
669 1.50 rmind npf_nat_destroy(con, nt);
670 1.50 rmind return NULL;
671 1.50 rmind }
672 1.50 rmind return nt;
673 1.50 rmind }
674 1.50 rmind
675 1.50 rmind /*
676 1.50 rmind * npf_nat_lookup: lookup the (dynamic) NAT state and return its entry,
677 1.50 rmind *
678 1.50 rmind * => Checks that the packet is on the interface where NAT policy is applied.
679 1.50 rmind * => Determines the flow direction in the context of the NAT policy.
680 1.50 rmind */
681 1.50 rmind static npf_nat_t *
682 1.50 rmind npf_nat_lookup(const npf_cache_t *npc, npf_conn_t *con,
683 1.50 rmind const unsigned di, npf_flow_t *flow)
684 1.50 rmind {
685 1.50 rmind const nbuf_t *nbuf = npc->npc_nbuf;
686 1.50 rmind const npf_natpolicy_t *np;
687 1.50 rmind npf_nat_t *nt;
688 1.50 rmind
689 1.50 rmind if ((nt = npf_conn_getnat(con)) == NULL) {
690 1.50 rmind return NULL;
691 1.50 rmind }
692 1.50 rmind if (nt->nt_ifid != nbuf->nb_ifid) {
693 1.50 rmind return NULL;
694 1.50 rmind }
695 1.50 rmind
696 1.50 rmind np = nt->nt_natpolicy;
697 1.50 rmind KASSERT(atomic_load_relaxed(&np->n_refcnt) > 0);
698 1.50 rmind
699 1.50 rmind /*
700 1.50 rmind * We rely on NPF_NAT{IN,OUT} being equal to PFIL_{IN,OUT}.
701 1.50 rmind */
702 1.50 rmind CTASSERT(NPF_NATIN == PFIL_IN && NPF_NATOUT == PFIL_OUT);
703 1.50 rmind *flow = (np->n_type == di) ? NPF_FLOW_FORW : NPF_FLOW_BACK;
704 1.50 rmind return nt;
705 1.50 rmind }
706 1.50 rmind
707 1.50 rmind /*
708 1.2 rmind * npf_do_nat:
709 1.45 rmind *
710 1.29 rmind * - Inspect packet for a NAT policy, unless a connection with a NAT
711 1.4 rmind * association already exists. In such case, determine whether it
712 1.2 rmind * is a "forwards" or "backwards" stream.
713 1.50 rmind *
714 1.4 rmind * - Perform translation: rewrite source or destination fields,
715 1.4 rmind * depending on translation type and direction.
716 1.50 rmind *
717 1.29 rmind * - Associate a NAT policy with a connection (may establish a new).
718 1.2 rmind */
719 1.2 rmind int
720 1.50 rmind npf_do_nat(npf_cache_t *npc, npf_conn_t *con, const unsigned di)
721 1.2 rmind {
722 1.30 rmind nbuf_t *nbuf = npc->npc_nbuf;
723 1.29 rmind npf_conn_t *ncon = NULL;
724 1.1 rmind npf_natpolicy_t *np;
725 1.50 rmind npf_flow_t flow;
726 1.1 rmind npf_nat_t *nt;
727 1.1 rmind int error;
728 1.1 rmind
729 1.43 maxv /* All relevant data should be already cached. */
730 1.3 rmind if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
731 1.1 rmind return 0;
732 1.1 rmind }
733 1.18 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
734 1.1 rmind
735 1.2 rmind /*
736 1.29 rmind * Return the NAT entry associated with the connection, if any.
737 1.3 rmind * Determines whether the stream is "forwards" or "backwards".
738 1.29 rmind * Note: no need to lock, since reference on connection is held.
739 1.2 rmind */
740 1.50 rmind if (con && (nt = npf_nat_lookup(npc, con, di, &flow)) != NULL) {
741 1.1 rmind np = nt->nt_natpolicy;
742 1.2 rmind goto translate;
743 1.1 rmind }
744 1.1 rmind
745 1.6 rmind /*
746 1.29 rmind * Inspect the packet for a NAT policy, if there is no connection.
747 1.19 rmind * Note: acquires a reference if found.
748 1.6 rmind */
749 1.30 rmind np = npf_nat_inspect(npc, di);
750 1.1 rmind if (np == NULL) {
751 1.1 rmind /* If packet does not match - done. */
752 1.1 rmind return 0;
753 1.1 rmind }
754 1.50 rmind flow = NPF_FLOW_FORW;
755 1.1 rmind
756 1.24 rmind /* Static NAT - just perform the translation. */
757 1.24 rmind if (np->n_flags & NPF_NAT_STATIC) {
758 1.24 rmind if (nbuf_cksum_barrier(nbuf, di)) {
759 1.30 rmind npf_recache(npc);
760 1.24 rmind }
761 1.50 rmind error = npf_snat_translate(npc, np, flow);
762 1.49 rmind npf_natpolicy_release(np);
763 1.24 rmind return error;
764 1.24 rmind }
765 1.24 rmind
766 1.4 rmind /*
767 1.29 rmind * If there is no local connection (no "stateful" rule - unusual,
768 1.29 rmind * but possible configuration), establish one before translation.
769 1.29 rmind * Note that it is not a "pass" connection, therefore passing of
770 1.29 rmind * "backwards" stream depends on other, stateless filtering rules.
771 1.29 rmind */
772 1.29 rmind if (con == NULL) {
773 1.30 rmind ncon = npf_conn_establish(npc, di, true);
774 1.29 rmind if (ncon == NULL) {
775 1.49 rmind npf_natpolicy_release(np);
776 1.22 rmind return ENOMEM;
777 1.1 rmind }
778 1.29 rmind con = ncon;
779 1.1 rmind }
780 1.22 rmind
781 1.22 rmind /*
782 1.29 rmind * Create a new NAT entry and associate with the connection.
783 1.22 rmind * We will consume the reference on success (release on error).
784 1.22 rmind */
785 1.29 rmind nt = npf_nat_create(npc, np, con);
786 1.22 rmind if (nt == NULL) {
787 1.49 rmind npf_natpolicy_release(np);
788 1.22 rmind error = ENOMEM;
789 1.22 rmind goto out;
790 1.22 rmind }
791 1.22 rmind
792 1.50 rmind /* Determine whether any ALG matches. */
793 1.50 rmind if (npf_alg_match(npc, nt, di)) {
794 1.50 rmind KASSERT(nt->nt_alg != NULL);
795 1.50 rmind }
796 1.50 rmind
797 1.29 rmind /* Associate the NAT translation entry with the connection. */
798 1.29 rmind error = npf_conn_setnat(npc, con, nt, np->n_type);
799 1.2 rmind if (error) {
800 1.22 rmind /* Will release the reference. */
801 1.50 rmind npf_nat_destroy(con, nt);
802 1.1 rmind goto out;
803 1.1 rmind }
804 1.1 rmind
805 1.22 rmind translate:
806 1.23 rmind /* May need to process the delayed checksums first (XXX: NetBSD). */
807 1.23 rmind if (nbuf_cksum_barrier(nbuf, di)) {
808 1.30 rmind npf_recache(npc);
809 1.23 rmind }
810 1.23 rmind
811 1.22 rmind /* Perform the translation. */
812 1.50 rmind error = npf_dnat_translate(npc, nt, flow);
813 1.1 rmind out:
814 1.29 rmind if (__predict_false(ncon)) {
815 1.24 rmind if (error) {
816 1.50 rmind /* It was created for NAT - just expire. */
817 1.29 rmind npf_conn_expire(ncon);
818 1.24 rmind }
819 1.29 rmind npf_conn_release(ncon);
820 1.1 rmind }
821 1.1 rmind return error;
822 1.1 rmind }
823 1.1 rmind
824 1.1 rmind /*
825 1.4 rmind * npf_nat_gettrans: return translation IP address and port.
826 1.4 rmind */
827 1.4 rmind void
828 1.4 rmind npf_nat_gettrans(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
829 1.4 rmind {
830 1.45 rmind *addr = &nt->nt_taddr;
831 1.4 rmind *port = nt->nt_tport;
832 1.4 rmind }
833 1.4 rmind
834 1.4 rmind /*
835 1.2 rmind * npf_nat_getorig: return original IP address and port from translation entry.
836 1.1 rmind */
837 1.1 rmind void
838 1.3 rmind npf_nat_getorig(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
839 1.1 rmind {
840 1.3 rmind *addr = &nt->nt_oaddr;
841 1.2 rmind *port = nt->nt_oport;
842 1.1 rmind }
843 1.1 rmind
844 1.3 rmind /*
845 1.3 rmind * npf_nat_setalg: associate an ALG with the NAT entry.
846 1.3 rmind */
847 1.1 rmind void
848 1.1 rmind npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
849 1.1 rmind {
850 1.1 rmind nt->nt_alg = alg;
851 1.1 rmind nt->nt_alg_arg = arg;
852 1.1 rmind }
853 1.1 rmind
854 1.50 rmind npf_alg_t *
855 1.50 rmind npf_nat_getalg(const npf_nat_t *nt)
856 1.50 rmind {
857 1.50 rmind return nt->nt_alg;
858 1.50 rmind }
859 1.50 rmind
860 1.50 rmind uintptr_t
861 1.50 rmind npf_nat_getalgarg(const npf_nat_t *nt)
862 1.50 rmind {
863 1.50 rmind return nt->nt_alg_arg;
864 1.50 rmind }
865 1.50 rmind
866 1.1 rmind /*
867 1.29 rmind * npf_nat_destroy: destroy NAT structure (performed on connection expiration).
868 1.1 rmind */
869 1.1 rmind void
870 1.50 rmind npf_nat_destroy(npf_conn_t *con, npf_nat_t *nt)
871 1.1 rmind {
872 1.2 rmind npf_natpolicy_t *np = nt->nt_natpolicy;
873 1.46 rmind npf_t *npf = np->n_npfctx;
874 1.50 rmind npf_alg_t *alg;
875 1.50 rmind
876 1.50 rmind /* Execute the ALG destroy callback, if any. */
877 1.50 rmind if ((alg = npf_nat_getalg(nt)) != NULL) {
878 1.50 rmind npf_alg_destroy(npf, alg, nt, con);
879 1.50 rmind nt->nt_alg = NULL;
880 1.50 rmind }
881 1.1 rmind
882 1.46 rmind /* Return taken port to the portmap. */
883 1.4 rmind if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
884 1.47 rmind npf_portmap_t *pm = npf->portmap;
885 1.47 rmind npf_portmap_put(pm, nt->nt_alen, &nt->nt_taddr, nt->nt_tport);
886 1.1 rmind }
887 1.41 christos npf_stats_inc(np->n_npfctx, NPF_STAT_NAT_DESTROY);
888 1.4 rmind
889 1.49 rmind /*
890 1.49 rmind * Remove the connection from the list and drop the reference on
891 1.49 rmind * the NAT policy. Note: this might trigger its destruction.
892 1.49 rmind */
893 1.4 rmind mutex_enter(&np->n_lock);
894 1.4 rmind LIST_REMOVE(nt, nt_entry);
895 1.4 rmind mutex_exit(&np->n_lock);
896 1.49 rmind npf_natpolicy_release(np);
897 1.49 rmind
898 1.1 rmind pool_cache_put(nat_cache, nt);
899 1.4 rmind }
900 1.4 rmind
901 1.4 rmind /*
902 1.50 rmind * npf_nat_export: serialize the NAT entry with a NAT policy ID.
903 1.4 rmind */
904 1.31 rmind void
905 1.50 rmind npf_nat_export(npf_t *npf, const npf_nat_t *nt, nvlist_t *con_nv)
906 1.4 rmind {
907 1.4 rmind npf_natpolicy_t *np = nt->nt_natpolicy;
908 1.49 rmind unsigned alen = nt->nt_alen;
909 1.50 rmind nvlist_t *nat_nv;
910 1.50 rmind
911 1.50 rmind nat_nv = nvlist_create(0);
912 1.50 rmind if (nt->nt_ifid) {
913 1.50 rmind char ifname[IFNAMSIZ];
914 1.50 rmind npf_ifmap_copyname(npf, nt->nt_ifid, ifname, sizeof(ifname));
915 1.50 rmind nvlist_add_string(nat_nv, "ifname", ifname);
916 1.50 rmind }
917 1.50 rmind nvlist_add_number(nat_nv, "alen", alen);
918 1.50 rmind
919 1.50 rmind nvlist_add_binary(nat_nv, "oaddr", &nt->nt_oaddr, alen);
920 1.50 rmind nvlist_add_number(nat_nv, "oport", nt->nt_oport);
921 1.4 rmind
922 1.50 rmind nvlist_add_binary(nat_nv, "taddr", &nt->nt_taddr, alen);
923 1.50 rmind nvlist_add_number(nat_nv, "tport", nt->nt_tport);
924 1.50 rmind
925 1.50 rmind nvlist_add_number(nat_nv, "nat-policy", np->n_id);
926 1.50 rmind nvlist_move_nvlist(con_nv, "nat", nat_nv);
927 1.4 rmind }
928 1.4 rmind
929 1.4 rmind /*
930 1.50 rmind * npf_nat_import: find the NAT policy and unserialize the NAT entry.
931 1.4 rmind */
932 1.4 rmind npf_nat_t *
933 1.44 rmind npf_nat_import(npf_t *npf, const nvlist_t *nat,
934 1.41 christos npf_ruleset_t *natlist, npf_conn_t *con)
935 1.4 rmind {
936 1.4 rmind npf_natpolicy_t *np;
937 1.4 rmind npf_nat_t *nt;
938 1.50 rmind const char *ifname;
939 1.49 rmind const void *taddr, *oaddr;
940 1.49 rmind size_t alen, len;
941 1.31 rmind uint64_t np_id;
942 1.4 rmind
943 1.44 rmind np_id = dnvlist_get_number(nat, "nat-policy", UINT64_MAX);
944 1.31 rmind if ((np = npf_ruleset_findnat(natlist, np_id)) == NULL) {
945 1.4 rmind return NULL;
946 1.4 rmind }
947 1.31 rmind nt = pool_cache_get(nat_cache, PR_WAITOK);
948 1.31 rmind memset(nt, 0, sizeof(npf_nat_t));
949 1.4 rmind
950 1.50 rmind ifname = dnvlist_get_string(nat, "ifname", NULL);
951 1.50 rmind if (ifname && (nt->nt_ifid = npf_ifmap_register(npf, ifname)) == 0) {
952 1.50 rmind goto err;
953 1.50 rmind }
954 1.50 rmind
955 1.49 rmind alen = dnvlist_get_number(nat, "alen", 0);
956 1.49 rmind if (alen == 0 || alen > sizeof(npf_addr_t)) {
957 1.49 rmind goto err;
958 1.49 rmind }
959 1.49 rmind
960 1.49 rmind taddr = dnvlist_get_binary(nat, "taddr", &len, NULL, 0);
961 1.49 rmind if (!taddr || len != alen) {
962 1.49 rmind goto err;
963 1.49 rmind }
964 1.49 rmind memcpy(&nt->nt_taddr, taddr, sizeof(npf_addr_t));
965 1.49 rmind
966 1.44 rmind oaddr = dnvlist_get_binary(nat, "oaddr", &len, NULL, 0);
967 1.49 rmind if (!oaddr || len != alen) {
968 1.49 rmind goto err;
969 1.4 rmind }
970 1.44 rmind memcpy(&nt->nt_oaddr, oaddr, sizeof(npf_addr_t));
971 1.49 rmind
972 1.44 rmind nt->nt_oport = dnvlist_get_number(nat, "oport", 0);
973 1.44 rmind nt->nt_tport = dnvlist_get_number(nat, "tport", 0);
974 1.4 rmind
975 1.4 rmind /* Take a specific port from port-map. */
976 1.47 rmind if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
977 1.47 rmind npf_portmap_t *pm = npf->portmap;
978 1.47 rmind
979 1.47 rmind if (!npf_portmap_take(pm, nt->nt_alen,
980 1.47 rmind &nt->nt_taddr, nt->nt_tport)) {
981 1.49 rmind goto err;
982 1.47 rmind }
983 1.4 rmind }
984 1.41 christos npf_stats_inc(npf, NPF_STAT_NAT_CREATE);
985 1.4 rmind
986 1.34 rmind /*
987 1.50 rmind * Associate, take a reference and insert. Unlocked/non-atomic
988 1.50 rmind * since the policy is not yet globally visible.
989 1.34 rmind */
990 1.4 rmind nt->nt_natpolicy = np;
991 1.29 rmind nt->nt_conn = con;
992 1.50 rmind atomic_store_relaxed(&np->n_refcnt,
993 1.50 rmind atomic_load_relaxed(&np->n_refcnt) + 1);
994 1.34 rmind LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
995 1.4 rmind return nt;
996 1.49 rmind err:
997 1.49 rmind pool_cache_put(nat_cache, nt);
998 1.49 rmind return NULL;
999 1.1 rmind }
1000 1.1 rmind
1001 1.1 rmind #if defined(DDB) || defined(_NPF_TESTING)
1002 1.1 rmind
1003 1.1 rmind void
1004 1.14 rmind npf_nat_dump(const npf_nat_t *nt)
1005 1.1 rmind {
1006 1.14 rmind const npf_natpolicy_t *np;
1007 1.1 rmind struct in_addr ip;
1008 1.1 rmind
1009 1.4 rmind np = nt->nt_natpolicy;
1010 1.45 rmind memcpy(&ip, &nt->nt_taddr, sizeof(ip));
1011 1.46 rmind printf("\tNATP(%p): type %u flags 0x%x taddr %s tport %d\n", np,
1012 1.38 rmind np->n_type, np->n_flags, inet_ntoa(ip), ntohs(np->n_tport));
1013 1.4 rmind memcpy(&ip, &nt->nt_oaddr, sizeof(ip));
1014 1.4 rmind printf("\tNAT: original address %s oport %d tport %d\n",
1015 1.4 rmind inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
1016 1.4 rmind if (nt->nt_alg) {
1017 1.4 rmind printf("\tNAT ALG = %p, ARG = %p\n",
1018 1.4 rmind nt->nt_alg, (void *)nt->nt_alg_arg);
1019 1.1 rmind }
1020 1.1 rmind }
1021 1.1 rmind
1022 1.1 rmind #endif
1023