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