#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_conn.c,v 1.35 2023/01/22 18:39:35 riastradh Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <net/pfil.h>
#include <sys/pool.h>
#include <sys/queue.h>
#include <sys/systm.h>
#endif
#define __NPF_CONN_PRIVATE
#include "npf_conn.h"
#include "npf_impl.h"
#define NPF_CONNCACHE(alen) (((alen) >> 4) & 0x1)
CTASSERT(PFIL_ALL == (0x001 | 0x002));
#define CONN_ACTIVE 0x004
#define CONN_PASS 0x008
#define CONN_EXPIRE 0x010
#define CONN_REMOVED 0x020
enum { CONN_TRACKING_OFF, CONN_TRACKING_ON };
static int npf_conn_export(npf_t *, npf_conn_t *, nvlist_t *);
void
npf_conn_init(npf_t *npf)
{
npf_conn_params_t *params = npf_param_allocgroup(npf,
NPF_PARAMS_CONN, sizeof(npf_conn_params_t));
npf_param_t param_map[] = {
{
"state.key.interface",
¶ms->connkey_interface,
.default_val = 1,
.min = 0, .max = 1
},
{
"state.key.direction",
¶ms->connkey_direction,
.default_val = 1,
.min = 0, .max = 1
},
};
npf_param_register(npf, param_map, __arraycount(param_map));
npf->conn_cache[0] = pool_cache_init(
offsetof(npf_conn_t, c_keys[NPF_CONNKEY_V4WORDS * 2]),
0, 0, 0, "npfcn4pl", NULL, IPL_NET, NULL, NULL, NULL);
npf->conn_cache[1] = pool_cache_init(
offsetof(npf_conn_t, c_keys[NPF_CONNKEY_V6WORDS * 2]),
0, 0, 0, "npfcn6pl", NULL, IPL_NET, NULL, NULL, NULL);
mutex_init(&npf->conn_lock, MUTEX_DEFAULT, IPL_NONE);
atomic_store_relaxed(&npf->conn_tracking, CONN_TRACKING_OFF);
npf->conn_db = npf_conndb_create();
npf_conndb_sysinit(npf);
npf_worker_addfunc(npf, npf_conn_worker);
}
void
npf_conn_fini(npf_t *npf)
{
const size_t len = sizeof(npf_conn_params_t);
KASSERT(atomic_load_relaxed(&npf->conn_tracking) == CONN_TRACKING_OFF);
npf_conndb_destroy(npf->conn_db);
pool_cache_destroy(npf->conn_cache[0]);
pool_cache_destroy(npf->conn_cache[1]);
mutex_destroy(&npf->conn_lock);
npf_param_freegroup(npf, NPF_PARAMS_CONN, len);
npf_conndb_sysfini(npf);
}
void
npf_conn_load(npf_t *npf, npf_conndb_t *ndb, bool track)
{
npf_conndb_t *odb = NULL;
KASSERT(npf_config_locked_p(npf));
mutex_enter(&npf->conn_lock);
if (ndb) {
KASSERT(atomic_load_relaxed(&npf->conn_tracking)
== CONN_TRACKING_OFF);
odb = atomic_load_relaxed(&npf->conn_db);
atomic_store_release(&npf->conn_db, ndb);
}
if (track) {
membar_producer();
atomic_store_relaxed(&npf->conn_tracking, CONN_TRACKING_ON);
}
mutex_exit(&npf->conn_lock);
if (odb) {
npf_conndb_gc(npf, odb, true, false);
npf_conndb_destroy(odb);
pool_cache_invalidate(npf->conn_cache[0]);
pool_cache_invalidate(npf->conn_cache[1]);
}
}
void
npf_conn_tracking(npf_t *npf, bool track)
{
KASSERT(npf_config_locked_p(npf));
atomic_store_relaxed(&npf->conn_tracking,
track ? CONN_TRACKING_ON : CONN_TRACKING_OFF);
}
static inline bool
npf_conn_trackable_p(const npf_cache_t *npc)
{
const npf_t *npf = npc->npc_ctx;
if (atomic_load_relaxed(&npf->conn_tracking) != CONN_TRACKING_ON) {
return false;
}
if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
return false;
}
return true;
}
static inline void
conn_update_atime(npf_conn_t *con)
{
struct timespec tsnow;
getnanouptime(&tsnow);
atomic_store_relaxed(&con->c_atime, tsnow.tv_sec);
}
static bool
npf_conn_check(const npf_conn_t *con, const nbuf_t *nbuf,
const unsigned di, const npf_flow_t flow)
{
const uint32_t flags = atomic_load_relaxed(&con->c_flags);
const unsigned ifid = atomic_load_relaxed(&con->c_ifid);
bool active;
active = (flags & (CONN_ACTIVE | CONN_EXPIRE)) == CONN_ACTIVE;
if (__predict_false(!active)) {
return false;
}
if (ifid && nbuf) {
const bool match = (flags & PFIL_ALL) == di;
npf_flow_t pflow = match ? NPF_FLOW_FORW : NPF_FLOW_BACK;
if (__predict_false(flow != pflow)) {
return false;
}
if (__predict_false(ifid != nbuf->nb_ifid)) {
return false;
}
}
return true;
}
npf_conn_t *
npf_conn_lookup(const npf_cache_t *npc, const unsigned di, npf_flow_t *flow)
{
npf_t *npf = npc->npc_ctx;
const nbuf_t *nbuf = npc->npc_nbuf;
npf_conn_t *con;
npf_connkey_t key;
if (!npf_conn_conkey(npc, &key, di, NPF_FLOW_FORW)) {
return NULL;
}
con = npf_conndb_lookup(npf, &key, flow);
if (con == NULL) {
return NULL;
}
KASSERT(npc->npc_proto == atomic_load_relaxed(&con->c_proto));
if (!npf_conn_check(con, nbuf, di, *flow)) {
atomic_dec_uint(&con->c_refcnt);
return NULL;
}
conn_update_atime(con);
return con;
}
npf_conn_t *
npf_conn_inspect(npf_cache_t *npc, const unsigned di, int *error)
{
nbuf_t *nbuf = npc->npc_nbuf;
npf_flow_t flow;
npf_conn_t *con;
bool ok;
KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
if (!npf_conn_trackable_p(npc)) {
return NULL;
}
if ((con = npf_alg_conn(npc, di)) != NULL) {
return con;
}
if (nbuf_head_mbuf(nbuf) == NULL) {
*error = ENOMEM;
return NULL;
}
KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
if ((con = npf_conn_lookup(npc, di, &flow)) == NULL) {
return NULL;
}
mutex_enter(&con->c_lock);
ok = npf_state_inspect(npc, &con->c_state, flow);
mutex_exit(&con->c_lock);
if (__predict_false(!ok)) {
npf_conn_release(con);
npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE);
return NULL;
}
#if 0
if (atomic_load_relaxed(&con->c_flags) & CONN_GPASS) {
(void)nbuf_add_tag(nbuf, NPF_NTAG_PASS);
}
#endif
return con;
}
npf_conn_t *
npf_conn_establish(npf_cache_t *npc, const unsigned di, bool global)
{
npf_t *npf = npc->npc_ctx;
const unsigned alen = npc->npc_alen;
const unsigned idx = NPF_CONNCACHE(alen);
const nbuf_t *nbuf = npc->npc_nbuf;
npf_connkey_t *fw, *bk;
npf_conndb_t *conn_db;
npf_conn_t *con;
int error = 0;
KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
if (!npf_conn_trackable_p(npc)) {
return NULL;
}
con = pool_cache_get(npf->conn_cache[idx], PR_NOWAIT);
if (__predict_false(!con)) {
npf_worker_signal(npf);
return NULL;
}
NPF_PRINTF(("NPF: create conn %p\n", con));
npf_stats_inc(npf, NPF_STAT_CONN_CREATE);
mutex_init(&con->c_lock, MUTEX_DEFAULT, IPL_SOFTNET);
atomic_store_relaxed(&con->c_flags, di & PFIL_ALL);
atomic_store_relaxed(&con->c_refcnt, 0);
con->c_rproc = NULL;
con->c_nat = NULL;
con->c_proto = npc->npc_proto;
CTASSERT(sizeof(con->c_proto) >= sizeof(npc->npc_proto));
con->c_alen = alen;
if (!npf_state_init(npc, &con->c_state)) {
npf_conn_destroy(npf, con);
return NULL;
}
KASSERT(npf_iscached(npc, NPC_IP46));
fw = npf_conn_getforwkey(con);
bk = npf_conn_getbackkey(con, alen);
if (!npf_conn_conkey(npc, fw, di, NPF_FLOW_FORW) ||
!npf_conn_conkey(npc, bk, di ^ PFIL_ALL, NPF_FLOW_BACK)) {
npf_conn_destroy(npf, con);
return NULL;
}
con->c_ifid = global ? nbuf->nb_ifid : 0;
conn_update_atime(con);
atomic_store_relaxed(&con->c_refcnt, 1);
mutex_enter(&con->c_lock);
conn_db = atomic_load_consume(&npf->conn_db);
if (!npf_conndb_insert(conn_db, fw, con, NPF_FLOW_FORW)) {
error = EISCONN;
goto err;
}
if (!npf_conndb_insert(conn_db, bk, con, NPF_FLOW_BACK)) {
npf_conn_t *ret __diagused;
ret = npf_conndb_remove(conn_db, fw);
KASSERT(ret == con);
error = EISCONN;
goto err;
}
err:
if (error) {
atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
atomic_dec_uint(&con->c_refcnt);
npf_stats_inc(npf, NPF_STAT_RACE_CONN);
} else {
NPF_PRINTF(("NPF: establish conn %p\n", con));
}
npf_conndb_enqueue(conn_db, con);
mutex_exit(&con->c_lock);
return error ? NULL : con;
}
void
npf_conn_destroy(npf_t *npf, npf_conn_t *con)
{
const unsigned idx __unused = NPF_CONNCACHE(con->c_alen);
KASSERT(atomic_load_relaxed(&con->c_refcnt) == 0);
if (con->c_nat) {
npf_nat_destroy(con, con->c_nat);
}
if (con->c_rproc) {
npf_rproc_release(con->c_rproc);
}
npf_state_destroy(&con->c_state);
mutex_destroy(&con->c_lock);
pool_cache_put(npf->conn_cache[idx], con);
npf_stats_inc(npf, NPF_STAT_CONN_DESTROY);
NPF_PRINTF(("NPF: conn %p destroyed\n", con));
}
int
npf_conn_setnat(const npf_cache_t *npc, npf_conn_t *con,
npf_nat_t *nt, unsigned ntype)
{
static const unsigned nat_type_which[] = {
[NPF_NATOUT] = NPF_DST,
[NPF_NATIN] = NPF_SRC,
};
npf_t *npf = npc->npc_ctx;
npf_conn_t *ret __diagused;
npf_conndb_t *conn_db;
npf_connkey_t *bk;
npf_addr_t *taddr;
in_port_t tport;
uint32_t flags;
KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
npf_nat_gettrans(nt, &taddr, &tport);
KASSERT(ntype == NPF_NATOUT || ntype == NPF_NATIN);
mutex_enter(&con->c_lock);
flags = atomic_load_relaxed(&con->c_flags);
if (__predict_false(flags & CONN_EXPIRE)) {
mutex_exit(&con->c_lock);
return EINVAL;
}
KASSERT((flags & CONN_REMOVED) == 0);
if (__predict_false(con->c_nat != NULL)) {
mutex_exit(&con->c_lock);
npf_stats_inc(npc->npc_ctx, NPF_STAT_RACE_NAT);
return EISCONN;
}
conn_db = atomic_load_consume(&npf->conn_db);
bk = npf_conn_getbackkey(con, con->c_alen);
ret = npf_conndb_remove(conn_db, bk);
KASSERT(ret == con);
npf_conn_adjkey(bk, taddr, tport, nat_type_which[ntype]);
if (!npf_conndb_insert(conn_db, bk, con, NPF_FLOW_BACK)) {
npf_connkey_t *fw = npf_conn_getforwkey(con);
ret = npf_conndb_remove(conn_db, fw);
KASSERT(ret == con);
atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
mutex_exit(&con->c_lock);
npf_stats_inc(npc->npc_ctx, NPF_STAT_RACE_NAT);
return EISCONN;
}
con->c_nat = nt;
mutex_exit(&con->c_lock);
return 0;
}
void
npf_conn_expire(npf_conn_t *con)
{
atomic_or_uint(&con->c_flags, CONN_EXPIRE);
}
bool
npf_conn_pass(const npf_conn_t *con, npf_match_info_t *mi, npf_rproc_t **rp)
{
KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
if (__predict_true(atomic_load_relaxed(&con->c_flags) & CONN_PASS)) {
mi->mi_retfl = atomic_load_relaxed(&con->c_retfl);
mi->mi_rid = con->c_rid;
*rp = con->c_rproc;
return true;
}
return false;
}
void
npf_conn_setpass(npf_conn_t *con, const npf_match_info_t *mi, npf_rproc_t *rp)
{
KASSERT((atomic_load_relaxed(&con->c_flags) & CONN_ACTIVE) == 0);
KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
KASSERT(con->c_rproc == NULL);
atomic_or_uint(&con->c_flags, CONN_PASS);
con->c_rproc = rp;
if (rp) {
con->c_rid = mi->mi_rid;
con->c_retfl = mi->mi_retfl;
}
}
void
npf_conn_release(npf_conn_t *con)
{
const unsigned flags = atomic_load_relaxed(&con->c_flags);
if ((flags & (CONN_ACTIVE | CONN_EXPIRE)) == 0) {
atomic_or_uint(&con->c_flags, CONN_ACTIVE);
}
KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
atomic_dec_uint(&con->c_refcnt);
}
npf_nat_t *
npf_conn_getnat(const npf_conn_t *con)
{
return con->c_nat;
}
bool
npf_conn_expired(npf_t *npf, const npf_conn_t *con, uint64_t tsnow)
{
const unsigned flags = atomic_load_relaxed(&con->c_flags);
const int etime = npf_state_etime(npf, &con->c_state, con->c_proto);
int elapsed;
if (__predict_false(flags & CONN_EXPIRE)) {
return true;
}
elapsed = (int64_t)tsnow - atomic_load_relaxed(&con->c_atime);
return elapsed > etime;
}
void
npf_conn_remove(npf_conndb_t *cd, npf_conn_t *con)
{
mutex_enter(&con->c_lock);
if ((atomic_load_relaxed(&con->c_flags) & CONN_REMOVED) == 0) {
npf_connkey_t *fw, *bk;
npf_conn_t *ret __diagused;
fw = npf_conn_getforwkey(con);
ret = npf_conndb_remove(cd, fw);
KASSERT(ret == con);
bk = npf_conn_getbackkey(con, NPF_CONNKEY_ALEN(fw));
ret = npf_conndb_remove(cd, bk);
KASSERT(ret == con);
}
atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
mutex_exit(&con->c_lock);
}
void
npf_conn_worker(npf_t *npf)
{
npf_conndb_t *conn_db = atomic_load_consume(&npf->conn_db);
npf_conndb_gc(npf, conn_db, false, true);
}
int
npf_conndb_export(npf_t *npf, nvlist_t *nvl)
{
npf_conn_t *head, *con;
npf_conndb_t *conn_db;
mutex_enter(&npf->conn_lock);
if (atomic_load_relaxed(&npf->conn_tracking) != CONN_TRACKING_ON) {
mutex_exit(&npf->conn_lock);
return 0;
}
conn_db = atomic_load_relaxed(&npf->conn_db);
head = npf_conndb_getlist(conn_db);
con = head;
while (con) {
nvlist_t *con_nvl;
con_nvl = nvlist_create(0);
if (npf_conn_export(npf, con, con_nvl) == 0) {
nvlist_append_nvlist_array(nvl, "conn-list", con_nvl);
}
nvlist_destroy(con_nvl);
if ((con = npf_conndb_getnext(conn_db, con)) == head) {
break;
}
}
mutex_exit(&npf->conn_lock);
return 0;
}
static int
npf_conn_export(npf_t *npf, npf_conn_t *con, nvlist_t *nvl)
{
nvlist_t *knvl;
npf_connkey_t *fw, *bk;
unsigned flags, alen;
flags = atomic_load_relaxed(&con->c_flags);
if ((flags & (CONN_ACTIVE|CONN_EXPIRE)) != CONN_ACTIVE) {
return ESRCH;
}
nvlist_add_number(nvl, "flags", flags);
nvlist_add_number(nvl, "proto", con->c_proto);
if (con->c_ifid) {
char ifname[IFNAMSIZ];
npf_ifmap_copyname(npf, con->c_ifid, ifname, sizeof(ifname));
nvlist_add_string(nvl, "ifname", ifname);
}
nvlist_add_binary(nvl, "state", &con->c_state, sizeof(npf_state_t));
fw = npf_conn_getforwkey(con);
alen = NPF_CONNKEY_ALEN(fw);
KASSERT(alen == con->c_alen);
bk = npf_conn_getbackkey(con, alen);
knvl = npf_connkey_export(npf, fw);
nvlist_move_nvlist(nvl, "forw-key", knvl);
knvl = npf_connkey_export(npf, bk);
nvlist_move_nvlist(nvl, "back-key", knvl);
nvlist_add_number(nvl, "alen", alen);
if (con->c_nat) {
npf_nat_export(npf, con->c_nat, nvl);
}
return 0;
}
int
npf_conn_import(npf_t *npf, npf_conndb_t *cd, const nvlist_t *cdict,
npf_ruleset_t *natlist)
{
npf_conn_t *con;
npf_connkey_t *fw, *bk;
const nvlist_t *nat, *conkey;
unsigned flags, alen, idx;
const char *ifname;
const void *state;
size_t len;
alen = dnvlist_get_number(cdict, "alen", 0);
idx = NPF_CONNCACHE(alen);
con = pool_cache_get(npf->conn_cache[idx], PR_WAITOK);
memset(con, 0, sizeof(npf_conn_t));
mutex_init(&con->c_lock, MUTEX_DEFAULT, IPL_SOFTNET);
npf_stats_inc(npf, NPF_STAT_CONN_CREATE);
con->c_proto = dnvlist_get_number(cdict, "proto", 0);
flags = dnvlist_get_number(cdict, "flags", 0);
flags &= PFIL_ALL | CONN_ACTIVE | CONN_PASS;
atomic_store_relaxed(&con->c_flags, flags);
conn_update_atime(con);
ifname = dnvlist_get_string(cdict, "ifname", NULL);
if (ifname && (con->c_ifid = npf_ifmap_register(npf, ifname)) == 0) {
goto err;
}
state = dnvlist_get_binary(cdict, "state", &len, NULL, 0);
if (!state || len != sizeof(npf_state_t)) {
goto err;
}
memcpy(&con->c_state, state, sizeof(npf_state_t));
if ((nat = dnvlist_get_nvlist(cdict, "nat", NULL)) != NULL &&
(con->c_nat = npf_nat_import(npf, nat, natlist, con)) == NULL) {
goto err;
}
fw = npf_conn_getforwkey(con);
conkey = dnvlist_get_nvlist(cdict, "forw-key", NULL);
if (conkey == NULL || !npf_connkey_import(npf, conkey, fw)) {
goto err;
}
bk = npf_conn_getbackkey(con, NPF_CONNKEY_ALEN(fw));
conkey = dnvlist_get_nvlist(cdict, "back-key", NULL);
if (conkey == NULL || !npf_connkey_import(npf, conkey, bk)) {
goto err;
}
if (NPF_CONNKEY_ALEN(fw) != alen || NPF_CONNKEY_ALEN(bk) != alen) {
goto err;
}
if (!npf_conndb_insert(cd, fw, con, NPF_FLOW_FORW)) {
goto err;
}
if (!npf_conndb_insert(cd, bk, con, NPF_FLOW_BACK)) {
npf_conndb_remove(cd, fw);
goto err;
}
NPF_PRINTF(("NPF: imported conn %p\n", con));
npf_conndb_enqueue(cd, con);
return 0;
err:
npf_conn_destroy(npf, con);
return EINVAL;
}
int
npf_conn_find(npf_t *npf, const nvlist_t *req, nvlist_t *resp)
{
const nvlist_t *key_nv;
npf_conn_t *con;
npf_connkey_t key;
npf_flow_t flow;
int error;
key_nv = dnvlist_get_nvlist(req, "key", NULL);
if (!key_nv || !npf_connkey_import(npf, key_nv, &key)) {
return EINVAL;
}
con = npf_conndb_lookup(npf, &key, &flow);
if (con == NULL) {
return ESRCH;
}
if (!npf_conn_check(con, NULL, 0, NPF_FLOW_FORW)) {
atomic_dec_uint(&con->c_refcnt);
return ESRCH;
}
error = npf_conn_export(npf, con, resp);
nvlist_add_number(resp, "flow", flow);
atomic_dec_uint(&con->c_refcnt);
return error;
}
#if defined(DDB) || defined(_NPF_TESTING)
void
npf_conn_print(npf_conn_t *con)
{
const npf_connkey_t *fw = npf_conn_getforwkey(con);
const npf_connkey_t *bk = npf_conn_getbackkey(con, NPF_CONNKEY_ALEN(fw));
const unsigned flags = atomic_load_relaxed(&con->c_flags);
const unsigned proto = con->c_proto;
struct timespec tspnow;
getnanouptime(&tspnow);
printf("%p:\n\tproto %d flags 0x%x tsdiff %ld etime %d\n", con,
proto, flags, (long)(tspnow.tv_sec - con->c_atime),
npf_state_etime(npf_getkernctx(), &con->c_state, proto));
npf_connkey_print(fw);
npf_connkey_print(bk);
npf_state_dump(&con->c_state);
if (con->c_nat) {
npf_nat_dump(con->c_nat);
}
}
#endif