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