#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.54 2025/07/01 18:42:37 joe Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/atomic.h>
#include <sys/condvar.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/pool.h>
#include <sys/proc.h>
#endif
#include "npf_impl.h"
#include "npf_conn.h"
struct npf_natpolicy {
npf_t * n_npfctx;
kmutex_t n_lock;
LIST_HEAD(, npf_nat) n_nat_list;
unsigned n_refcnt;
uint64_t n_id;
unsigned n_type;
unsigned n_flags;
unsigned n_alen;
npf_addr_t n_taddr;
npf_netmask_t n_tmask;
in_port_t n_tport;
unsigned n_tid;
unsigned n_algo;
union {
unsigned n_rr_idx;
uint16_t n_npt66_adj;
};
};
#define NPF_NAT_USETABLE (0x01000000 & NPF_NAT_PRIVMASK)
#define NPF_NP_CMP_START offsetof(npf_natpolicy_t, n_type)
#define NPF_NP_CMP_SIZE (sizeof(npf_natpolicy_t) - NPF_NP_CMP_START)
struct npf_nat {
npf_natpolicy_t * nt_natpolicy;
uint16_t nt_ifid;
uint16_t nt_alen;
npf_addr_t nt_taddr;
npf_addr_t nt_oaddr;
in_port_t nt_oport;
in_port_t nt_tport;
npf_alg_t * nt_alg;
uintptr_t nt_alg_arg;
LIST_ENTRY(npf_nat) nt_entry;
npf_conn_t * nt_conn;
};
static pool_cache_t nat_cache __read_mostly;
void
npf_nat_sysinit(void)
{
nat_cache = pool_cache_init(sizeof(npf_nat_t), 0,
0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
KASSERT(nat_cache != NULL);
}
void
npf_nat_sysfini(void)
{
pool_cache_destroy(nat_cache);
}
npf_natpolicy_t *
npf_natpolicy_create(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *rset)
{
npf_natpolicy_t *np;
const void *addr;
size_t len;
np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
atomic_store_relaxed(&np->n_refcnt, 1);
np->n_npfctx = npf;
np->n_type = dnvlist_get_number(nat, "type", 0);
np->n_flags = dnvlist_get_number(nat, "flags", 0) & ~NPF_NAT_PRIVMASK;
np->n_id = dnvlist_get_number(nat, "nat-policy", 0);
if (((np->n_type == NPF_NATIN) ^ (np->n_type == NPF_NATOUT)) == 0) {
goto err;
}
mutex_init(&np->n_lock, MUTEX_DEFAULT, IPL_SOFTNET);
LIST_INIT(&np->n_nat_list);
if (nvlist_exists_number(nat, "nat-table-id")) {
if (np->n_flags & NPF_NAT_STATIC) {
goto err;
}
np->n_tid = nvlist_get_number(nat, "nat-table-id");
np->n_tmask = NPF_NO_NETMASK;
np->n_flags |= NPF_NAT_USETABLE;
} else {
addr = dnvlist_get_binary(nat, "nat-addr", &len, NULL, 0);
if (!addr || len == 0 || len > sizeof(npf_addr_t)) {
goto err;
}
memcpy(&np->n_taddr, addr, len);
np->n_alen = len;
np->n_tmask = dnvlist_get_number(nat, "nat-mask", NPF_NO_NETMASK);
if (npf_netmask_check(np->n_alen, np->n_tmask)) {
goto err;
}
}
np->n_tport = dnvlist_get_number(nat, "nat-port", 0);
np->n_algo = dnvlist_get_number(nat, "nat-algo", 0);
switch (np->n_algo) {
case NPF_ALGO_NPT66:
np->n_npt66_adj = dnvlist_get_number(nat, "npt66-adj", 0);
break;
case NPF_ALGO_NETMAP:
break;
case NPF_ALGO_IPHASH:
case NPF_ALGO_RR:
default:
if (np->n_tmask != NPF_NO_NETMASK) {
goto err;
}
break;
}
return np;
err:
mutex_destroy(&np->n_lock);
kmem_free(np, sizeof(npf_natpolicy_t));
return NULL;
}
int
npf_natpolicy_export(const npf_natpolicy_t *np, nvlist_t *nat)
{
nvlist_add_number(nat, "nat-policy", np->n_id);
nvlist_add_number(nat, "type", np->n_type);
nvlist_add_number(nat, "flags", np->n_flags);
if (np->n_flags & NPF_NAT_USETABLE) {
nvlist_add_number(nat, "nat-table-id", np->n_tid);
} else {
nvlist_add_binary(nat, "nat-addr", &np->n_taddr, np->n_alen);
nvlist_add_number(nat, "nat-mask", np->n_tmask);
}
nvlist_add_number(nat, "nat-port", np->n_tport);
nvlist_add_number(nat, "nat-algo", np->n_algo);
switch (np->n_algo) {
case NPF_ALGO_NPT66:
nvlist_add_number(nat, "npt66-adj", np->n_npt66_adj);
break;
}
return 0;
}
static void
npf_natpolicy_release(npf_natpolicy_t *np)
{
KASSERT(atomic_load_relaxed(&np->n_refcnt) > 0);
membar_release();
if (atomic_dec_uint_nv(&np->n_refcnt) != 0) {
return;
}
membar_acquire();
KASSERT(LIST_EMPTY(&np->n_nat_list));
mutex_destroy(&np->n_lock);
kmem_free(np, sizeof(npf_natpolicy_t));
}
void
npf_natpolicy_destroy(npf_natpolicy_t *np)
{
if (atomic_load_relaxed(&np->n_refcnt) > 1) {
npf_nat_t *nt;
mutex_enter(&np->n_lock);
LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
npf_conn_t *con = nt->nt_conn;
KASSERT(con != NULL);
npf_conn_expire(con);
}
mutex_exit(&np->n_lock);
npf_worker_signal(np->n_npfctx);
}
KASSERT(atomic_load_relaxed(&np->n_refcnt) >= 1);
npf_natpolicy_release(np);
}
void
npf_nat_freealg(npf_natpolicy_t *np, npf_alg_t *alg)
{
npf_nat_t *nt;
mutex_enter(&np->n_lock);
LIST_FOREACH(nt, &np->n_nat_list, nt_entry) {
if (nt->nt_alg == alg) {
npf_alg_destroy(np->n_npfctx, alg, nt, nt->nt_conn);
nt->nt_alg = NULL;
}
}
mutex_exit(&np->n_lock);
}
bool
npf_natpolicy_cmp(npf_natpolicy_t *np, npf_natpolicy_t *mnp)
{
const void *np_raw, *mnp_raw;
KASSERT(np && mnp && np != mnp);
np_raw = (const uint8_t *)np + NPF_NP_CMP_START;
mnp_raw = (const uint8_t *)mnp + NPF_NP_CMP_START;
return memcmp(np_raw, mnp_raw, NPF_NP_CMP_SIZE) == 0;
}
void
npf_nat_setid(npf_natpolicy_t *np, uint64_t id)
{
np->n_id = id;
}
uint64_t
npf_nat_getid(const npf_natpolicy_t *np)
{
return np->n_id;
}
static inline unsigned
npf_nat_which(const unsigned type, const npf_flow_t flow)
{
unsigned which;
CTASSERT(NPF_SRC == 0 && NPF_DST == 1);
CTASSERT(NPF_FLOW_FORW == NPF_SRC && NPF_FLOW_BACK == NPF_DST);
KASSERT(type == NPF_NATIN || type == NPF_NATOUT);
KASSERT(flow == NPF_FLOW_FORW || flow == NPF_FLOW_BACK);
which = (type == NPF_NATOUT) ? flow : !flow;
KASSERT(which == NPF_SRC || which == NPF_DST);
return which;
}
static npf_natpolicy_t *
npf_nat_inspect(npf_cache_t *npc, const unsigned di)
{
npf_t *npf = npc->npc_ctx;
int slock = npf_config_read_enter(npf);
npf_ruleset_t *rlset = npf_config_natset(npf);
npf_natpolicy_t *np;
npf_rule_t *rl;
rl = npf_ruleset_inspect(npc, rlset, di, NPF_RULE_LAYER_3);
if (rl == NULL) {
npf_config_read_exit(npf, slock);
return NULL;
}
np = npf_rule_getnat(rl);
atomic_inc_uint(&np->n_refcnt);
npf_config_read_exit(npf, slock);
return np;
}
static void
npf_nat_algo_netmap(const npf_cache_t *npc, const npf_natpolicy_t *np,
const unsigned which, npf_addr_t *addr)
{
const npf_addr_t *orig_addr = npc->npc_ips[which];
npf_addr_mask(&np->n_taddr, np->n_tmask, npc->npc_alen, addr);
npf_addr_bitor(orig_addr, np->n_tmask, npc->npc_alen, addr);
}
static inline npf_addr_t *
npf_nat_getaddr(npf_cache_t *npc, npf_natpolicy_t *np, const unsigned alen)
{
npf_tableset_t *ts = npf_config_tableset(np->n_npfctx);
npf_table_t *t = npf_tableset_getbyid(ts, np->n_tid);
unsigned idx;
switch (np->n_algo) {
case NPF_ALGO_RR:
idx = atomic_inc_uint_nv(&np->n_rr_idx);
break;
case NPF_ALGO_IPHASH:
default:
idx = npf_addr_mix(alen,
npc->npc_ips[NPF_SRC],
npc->npc_ips[NPF_DST]);
break;
}
return npf_table_getsome(t, alen, idx);
}
static npf_nat_t *
npf_nat_create(npf_cache_t *npc, npf_natpolicy_t *np, npf_conn_t *con)
{
const unsigned proto = npc->npc_proto;
const unsigned alen = npc->npc_alen;
const nbuf_t *nbuf = npc->npc_nbuf;
npf_t *npf = npc->npc_ctx;
npf_addr_t *taddr;
npf_nat_t *nt;
KASSERT(npf_iscached(npc, NPC_IP46));
KASSERT(npf_iscached(npc, NPC_LAYER4));
nt = pool_cache_get(nat_cache, PR_NOWAIT);
if (__predict_false(!nt)) {
return NULL;
}
npf_stats_inc(npf, NPF_STAT_NAT_CREATE);
nt->nt_natpolicy = np;
nt->nt_conn = con;
nt->nt_alg = NULL;
KASSERT(nbuf->nb_ifid != 0);
nt->nt_ifid = nbuf->nb_ifid;
if (np->n_flags & NPF_NAT_USETABLE) {
int slock = npf_config_read_enter(npf);
taddr = npf_nat_getaddr(npc, np, alen);
if (__predict_false(!taddr)) {
npf_config_read_exit(npf, slock);
pool_cache_put(nat_cache, nt);
return NULL;
}
memcpy(&nt->nt_taddr, taddr, alen);
npf_config_read_exit(npf, slock);
} else if (np->n_algo == NPF_ALGO_NETMAP) {
const unsigned which = npf_nat_which(np->n_type, NPF_FLOW_FORW);
npf_nat_algo_netmap(npc, np, which, &nt->nt_taddr);
taddr = &nt->nt_taddr;
} else {
taddr = &np->n_taddr;
memcpy(&nt->nt_taddr, taddr, alen);
}
nt->nt_alen = alen;
if (np->n_type == NPF_NATOUT) {
memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_SRC], alen);
} else {
KASSERT(np->n_type == NPF_NATIN);
memcpy(&nt->nt_oaddr, npc->npc_ips[NPF_DST], alen);
}
if ((np->n_flags & NPF_NAT_PORTS) == 0 ||
(proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
nt->nt_oport = 0;
nt->nt_tport = 0;
goto out;
}
if (proto == IPPROTO_TCP) {
const struct tcphdr *th = npc->npc_l4.tcp;
nt->nt_oport = (np->n_type == NPF_NATOUT) ?
th->th_sport : th->th_dport;
} else {
const struct udphdr *uh = npc->npc_l4.udp;
nt->nt_oport = (np->n_type == NPF_NATOUT) ?
uh->uh_sport : uh->uh_dport;
}
if ((np->n_flags & NPF_NAT_PORTMAP) != 0) {
npf_portmap_t *pm = np->n_npfctx->portmap;
nt->nt_tport = npf_portmap_get(pm, alen, taddr);
} else {
nt->nt_tport = np->n_tport;
}
out:
mutex_enter(&np->n_lock);
LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
mutex_exit(&np->n_lock);
return nt;
}
static inline int
npf_dnat_translate(npf_cache_t *npc, npf_nat_t *nt, npf_flow_t flow)
{
const npf_natpolicy_t *np = nt->nt_natpolicy;
const unsigned which = npf_nat_which(np->n_type, flow);
const npf_addr_t *addr;
in_port_t port;
KASSERT(npf_iscached(npc, NPC_IP46));
KASSERT(npf_iscached(npc, NPC_LAYER4));
if (flow == NPF_FLOW_FORW) {
addr = &nt->nt_taddr;
port = nt->nt_tport;
} else {
addr = &nt->nt_oaddr;
port = nt->nt_oport;
}
KASSERT((np->n_flags & NPF_NAT_PORTS) != 0 || port == 0);
if ((npc->npc_info & NPC_ALG_EXEC) == 0) {
npc->npc_info |= NPC_ALG_EXEC;
npf_alg_exec(npc, nt, flow);
npf_recache(npc);
}
KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
return npf_napt_rwr(npc, which, addr, port);
}
static inline int
npf_snat_translate(npf_cache_t *npc, const npf_natpolicy_t *np, npf_flow_t flow)
{
const unsigned which = npf_nat_which(np->n_type, flow);
const npf_addr_t *taddr;
npf_addr_t addr;
KASSERT(np->n_flags & NPF_NAT_STATIC);
switch (np->n_algo) {
case NPF_ALGO_NETMAP:
npf_nat_algo_netmap(npc, np, which, &addr);
taddr = &addr;
break;
case NPF_ALGO_NPT66:
return npf_npt66_rwr(npc, which, &np->n_taddr,
np->n_tmask, np->n_npt66_adj);
default:
taddr = &np->n_taddr;
break;
}
return npf_napt_rwr(npc, which, taddr, np->n_tport);
}
npf_nat_t *
npf_nat_share_policy(npf_cache_t *npc, npf_conn_t *con, npf_nat_t *src_nt)
{
npf_natpolicy_t *np = src_nt->nt_natpolicy;
npf_nat_t *nt;
int ret;
nt = npf_nat_create(npc, np, con);
if (__predict_false(nt == NULL)) {
return NULL;
}
atomic_inc_uint(&np->n_refcnt);
ret = npf_conn_setnat(npc, con, nt, np->n_type);
if (__predict_false(ret)) {
npf_nat_destroy(con, nt);
return NULL;
}
return nt;
}
static npf_nat_t *
npf_nat_lookup(const npf_cache_t *npc, npf_conn_t *con,
const unsigned di, npf_flow_t *flow)
{
const nbuf_t *nbuf = npc->npc_nbuf;
const npf_natpolicy_t *np;
npf_nat_t *nt;
if ((nt = npf_conn_getnat(con)) == NULL) {
return NULL;
}
if (nt->nt_ifid != nbuf->nb_ifid) {
return NULL;
}
np = nt->nt_natpolicy;
KASSERT(atomic_load_relaxed(&np->n_refcnt) > 0);
CTASSERT(NPF_NATIN == PFIL_IN && NPF_NATOUT == PFIL_OUT);
*flow = (np->n_type == di) ? NPF_FLOW_FORW : NPF_FLOW_BACK;
return nt;
}
int
npf_do_nat(npf_cache_t *npc, npf_conn_t *con, const unsigned di)
{
nbuf_t *nbuf = npc->npc_nbuf;
npf_conn_t *ncon = NULL;
npf_natpolicy_t *np;
npf_flow_t flow;
npf_nat_t *nt;
int error;
if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
return 0;
}
KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
if (con && (nt = npf_nat_lookup(npc, con, di, &flow)) != NULL) {
np = nt->nt_natpolicy;
goto translate;
}
np = npf_nat_inspect(npc, di);
if (np == NULL) {
return 0;
}
flow = NPF_FLOW_FORW;
if (np->n_flags & NPF_NAT_STATIC) {
if (nbuf_cksum_barrier(nbuf, di)) {
npf_recache(npc);
}
error = npf_snat_translate(npc, np, flow);
npf_natpolicy_release(np);
return error;
}
if (con == NULL) {
ncon = npf_conn_establish(npc, di, true);
if (ncon == NULL) {
npf_natpolicy_release(np);
return ENOMEM;
}
con = ncon;
}
nt = npf_nat_create(npc, np, con);
if (nt == NULL) {
npf_natpolicy_release(np);
error = ENOMEM;
goto out;
}
if (npf_alg_match(npc, nt, di)) {
KASSERT(nt->nt_alg != NULL);
}
error = npf_conn_setnat(npc, con, nt, np->n_type);
if (error) {
npf_nat_destroy(con, nt);
goto out;
}
translate:
if (nbuf_cksum_barrier(nbuf, di)) {
npf_recache(npc);
}
error = npf_dnat_translate(npc, nt, flow);
out:
if (__predict_false(ncon)) {
if (error) {
npf_conn_expire(ncon);
}
npf_conn_release(ncon);
}
return error;
}
void
npf_nat_gettrans(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
{
*addr = &nt->nt_taddr;
*port = nt->nt_tport;
}
void
npf_nat_getorig(npf_nat_t *nt, npf_addr_t **addr, in_port_t *port)
{
*addr = &nt->nt_oaddr;
*port = nt->nt_oport;
}
void
npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
{
nt->nt_alg = alg;
nt->nt_alg_arg = arg;
}
npf_alg_t *
npf_nat_getalg(const npf_nat_t *nt)
{
return nt->nt_alg;
}
uintptr_t
npf_nat_getalgarg(const npf_nat_t *nt)
{
return nt->nt_alg_arg;
}
void
npf_nat_destroy(npf_conn_t *con, npf_nat_t *nt)
{
npf_natpolicy_t *np = nt->nt_natpolicy;
npf_t *npf = np->n_npfctx;
npf_alg_t *alg;
if ((alg = npf_nat_getalg(nt)) != NULL) {
npf_alg_destroy(npf, alg, nt, con);
nt->nt_alg = NULL;
}
if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
npf_portmap_t *pm = npf->portmap;
npf_portmap_put(pm, nt->nt_alen, &nt->nt_taddr, nt->nt_tport);
}
npf_stats_inc(np->n_npfctx, NPF_STAT_NAT_DESTROY);
mutex_enter(&np->n_lock);
LIST_REMOVE(nt, nt_entry);
mutex_exit(&np->n_lock);
npf_natpolicy_release(np);
pool_cache_put(nat_cache, nt);
}
void
npf_nat_export(npf_t *npf, const npf_nat_t *nt, nvlist_t *con_nv)
{
npf_natpolicy_t *np = nt->nt_natpolicy;
unsigned alen = nt->nt_alen;
nvlist_t *nat_nv;
nat_nv = nvlist_create(0);
if (nt->nt_ifid) {
char ifname[IFNAMSIZ];
npf_ifmap_copyname(npf, nt->nt_ifid, ifname, sizeof(ifname));
nvlist_add_string(nat_nv, "ifname", ifname);
}
nvlist_add_number(nat_nv, "alen", alen);
nvlist_add_binary(nat_nv, "oaddr", &nt->nt_oaddr, alen);
nvlist_add_number(nat_nv, "oport", nt->nt_oport);
nvlist_add_binary(nat_nv, "taddr", &nt->nt_taddr, alen);
nvlist_add_number(nat_nv, "tport", nt->nt_tport);
nvlist_add_number(nat_nv, "nat-policy", np->n_id);
nvlist_move_nvlist(con_nv, "nat", nat_nv);
}
npf_nat_t *
npf_nat_import(npf_t *npf, const nvlist_t *nat,
npf_ruleset_t *natlist, npf_conn_t *con)
{
npf_natpolicy_t *np;
npf_nat_t *nt;
const char *ifname;
const void *taddr, *oaddr;
size_t alen, len;
uint64_t np_id;
np_id = dnvlist_get_number(nat, "nat-policy", UINT64_MAX);
if ((np = npf_ruleset_findnat(natlist, np_id)) == NULL) {
return NULL;
}
nt = pool_cache_get(nat_cache, PR_WAITOK);
memset(nt, 0, sizeof(npf_nat_t));
ifname = dnvlist_get_string(nat, "ifname", NULL);
if (ifname && (nt->nt_ifid = npf_ifmap_register(npf, ifname)) == 0) {
goto err;
}
alen = dnvlist_get_number(nat, "alen", 0);
if (alen == 0 || alen > sizeof(npf_addr_t)) {
goto err;
}
taddr = dnvlist_get_binary(nat, "taddr", &len, NULL, 0);
if (!taddr || len != alen) {
goto err;
}
memcpy(&nt->nt_taddr, taddr, sizeof(npf_addr_t));
oaddr = dnvlist_get_binary(nat, "oaddr", &len, NULL, 0);
if (!oaddr || len != alen) {
goto err;
}
memcpy(&nt->nt_oaddr, oaddr, sizeof(npf_addr_t));
nt->nt_oport = dnvlist_get_number(nat, "oport", 0);
nt->nt_tport = dnvlist_get_number(nat, "tport", 0);
if ((np->n_flags & NPF_NAT_PORTMAP) != 0 && nt->nt_tport) {
npf_portmap_t *pm = npf->portmap;
if (!npf_portmap_take(pm, nt->nt_alen,
&nt->nt_taddr, nt->nt_tport)) {
goto err;
}
}
npf_stats_inc(npf, NPF_STAT_NAT_CREATE);
nt->nt_natpolicy = np;
nt->nt_conn = con;
atomic_store_relaxed(&np->n_refcnt,
atomic_load_relaxed(&np->n_refcnt) + 1);
LIST_INSERT_HEAD(&np->n_nat_list, nt, nt_entry);
return nt;
err:
pool_cache_put(nat_cache, nt);
return NULL;
}
#if defined(DDB) || defined(_NPF_TESTING)
void
npf_nat_dump(const npf_nat_t *nt)
{
const npf_natpolicy_t *np;
struct in_addr ip;
np = nt->nt_natpolicy;
memcpy(&ip, &nt->nt_taddr, sizeof(ip));
printf("\tNATP(%p): type %u flags 0x%x taddr %s tport %d\n", np,
np->n_type, np->n_flags, inet_ntoa(ip), ntohs(np->n_tport));
memcpy(&ip, &nt->nt_oaddr, sizeof(ip));
printf("\tNAT: original address %s oport %d tport %d\n",
inet_ntoa(ip), ntohs(nt->nt_oport), ntohs(nt->nt_tport));
if (nt->nt_alg) {
printf("\tNAT ALG = %p, ARG = %p\n",
nt->nt_alg, (void *)nt->nt_alg_arg);
}
}
#endif