#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.55 2026/04/08 00:33:07 joe Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <net/if.h>
#include <net/pfil.h>
#include <sys/socketvar.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#endif
#include "npf_impl.h"
#include "npf_conn.h"
#if defined(_NPF_STANDALONE)
#define m_freem(m) npf->mbufops->free(m)
#define m_clear_flag(m,f)
#else
#define m_clear_flag(m,f) (m)->m_flags &= ~(f)
#endif
#ifndef INET6
#define ip6_reass_packet(x, y) ENOTSUP
#endif
static int
npf_reassembly(npf_t *npf, npf_cache_t *npc, bool *mff)
{
nbuf_t *nbuf = npc->npc_nbuf;
int error = EINVAL;
struct mbuf *m;
*mff = false;
m = nbuf_head_mbuf(nbuf);
if (npf_iscached(npc, NPC_IP4) && npf->ip4_reassembly) {
error = ip_reass_packet(&m);
} else if (npf_iscached(npc, NPC_IP6) && npf->ip6_reassembly) {
error = ip6_reass_packet(&m, npc->npc_hlen);
} else {
return 0;
}
if (error) {
npf_stats_inc(npf, NPF_STAT_REASSFAIL);
m_freem(m);
memset(nbuf, 0, sizeof(nbuf_t));
return error;
}
if (m == NULL) {
npf_stats_inc(npf, NPF_STAT_FRAGMENTS);
*mff = true;
return 0;
}
nbuf_init(npf, nbuf, m, nbuf->nb_ifp);
npc->npc_info = 0;
if (npf_cache_all(npc) & (NPC_IPFRAG|NPC_FMTERR)) {
return EINVAL;
}
npf_stats_inc(npf, NPF_STAT_REASSEMBLY);
return 0;
}
static inline bool
npf_packet_bypass_tag_p(nbuf_t *nbuf)
{
uint32_t ntag;
return nbuf_find_tag(nbuf, &ntag) == 0 && (ntag & NPF_NTAG_PASS) != 0;
}
__dso_public int
npfk_packet_handler(npf_t *npf, struct mbuf **mp, ifnet_t *ifp, int di)
{
nbuf_t nbuf;
npf_cache_t npc;
npf_conn_t *con;
npf_rule_t *rl;
npf_rproc_t *rp;
int error, decision, flags, id_match;
npf_match_info_t mi;
bool mff;
KASSERT(ifp != NULL);
nbuf_init(npf, &nbuf, *mp, ifp);
memset(&npc, 0, sizeof(npf_cache_t));
npc.npc_ctx = npf;
npc.npc_nbuf = &nbuf;
mi.mi_di = di;
mi.mi_rid = 0;
mi.mi_retfl = 0;
*mp = NULL;
decision = NPF_DECISION_BLOCK;
error = 0;
rp = NULL;
con = NULL;
flags = npf_cache_all(&npc);
if (flags & NPC_FMTERR) {
error = EINVAL;
goto out;
}
if (__predict_false(flags & NPC_IPFRAG)) {
error = npf_reassembly(npf, &npc, &mff);
if (error) {
goto out;
}
if (mff) {
return 0;
}
}
if (npf_packet_bypass_tag_p(&nbuf)) {
goto pass;
}
con = npf_conn_inspect(&npc, di, &error);
if (con && npf_conn_pass(con, &mi, &rp)) {
npf_stats_inc(npf, NPF_STAT_PASS_CONN);
KASSERT(error == 0);
goto pass;
}
if (__predict_false(error)) {
if (error == ENETUNREACH)
goto block;
goto out;
}
int slock = npf_config_read_enter(npf);
npf_ruleset_t *rlset = npf_config_ruleset(npf);
rl = npf_ruleset_inspect(&npc, rlset, di, NPF_RULE_LAYER_3);
if (__predict_false(rl == NULL)) {
const bool pass = npf_default_pass(npf);
npf_config_read_exit(npf, slock);
if (pass) {
npf_stats_inc(npf, NPF_STAT_PASS_DEFAULT);
goto pass;
}
npf_stats_inc(npf, NPF_STAT_BLOCK_DEFAULT);
goto block;
}
KASSERT(rp == NULL);
rp = npf_rule_getrproc(rl);
id_match = npf_rule_match_rid(rl, &npc, di);
error = npf_rule_conclude(rl, &mi);
npf_config_read_exit(npf, slock);
if (id_match != -1 && !id_match) {
error = npf_rule_reverse(&npc, &mi, error);
}
if (id_match == ENOTCONN || error) {
npf_stats_inc(npf, NPF_STAT_BLOCK_RULESET);
goto block;
}
npf_stats_inc(npf, NPF_STAT_PASS_RULESET);
if ((mi.mi_retfl & NPF_RULE_STATEFUL) != 0 && !con) {
con = npf_conn_establish(&npc, di,
(mi.mi_retfl & NPF_RULE_GSTATEFUL) == 0);
if (con) {
npf_conn_setpass(con, &mi, rp);
}
}
pass:
decision = NPF_DECISION_PASS;
KASSERT(error == 0);
error = npf_do_nat(&npc, con, di);
block:
if (rp && !npf_rproc_run(&npc, rp, &mi, &decision)) {
if (con) {
npf_conn_release(con);
if (is_rproc_route(rp))
return 0;
}
npf_rproc_release(rp);
return 0;
}
out:
if (con) {
npf_conn_release(con);
} else if (rp) {
npf_rproc_release(rp);
}
if ((*mp = nbuf_head_mbuf(&nbuf)) == NULL) {
return error ? error : ENOMEM;
}
if (decision == NPF_DECISION_PASS && !error) {
m_clear_flag(*mp, M_CANFASTFWD);
return 0;
}
if (mi.mi_retfl && npf_return_block(&npc, mi.mi_retfl)) {
*mp = NULL;
}
if (!error) {
error = ENETUNREACH;
}
m_freem(*mp);
*mp = NULL;
return error;
}
__dso_public int
npfk_layer2_handler(npf_t *npf, struct mbuf **mp, ifnet_t *ifp, int di)
{
nbuf_t nbuf;
npf_cache_t npc;
npf_rule_t *rl;
int error, decision, flags;
npf_match_info_t mi;
KASSERT(ifp != NULL);
nbuf_init(npf, &nbuf, *mp, ifp);
memset(&npc, 0, sizeof(npc));
npc.npc_ctx = npf;
npc.npc_nbuf = &nbuf;
mi.mi_di = di;
mi.mi_rid = 0;
mi.mi_retfl = 0;
*mp = NULL;
decision = NPF_DECISION_BLOCK;
error = 0;
flags = npf_cache_ether(&npc);
if (flags & NPC_FMTERR) {
error = EINVAL;
goto out;
}
if (npf_packet_bypass_tag_p(&nbuf)) {
goto pass;
}
int slock = npf_config_read_enter(npf);
npf_ruleset_t *rlset = npf_config_ruleset(npf);
rl = npf_ruleset_inspect(&npc, rlset, di, NPF_RULE_LAYER_2);
if (__predict_false(rl == NULL)) {
npf_config_read_exit(npf, slock);
npf_stats_inc(npf, NPF_STAT_PASS_DEFAULT);
goto pass;
}
error = npf_rule_conclude(rl, &mi);
npf_config_read_exit(npf, slock);
if (error) {
npf_stats_inc(npf, NPF_ETHER_STAT_BLOCK);
goto out;
}
npf_stats_inc(npf, NPF_ETHER_STAT_PASS);
pass:
decision = NPF_DECISION_PASS;
KASSERT(error == 0);
out:
if ((*mp = nbuf_head_mbuf(&nbuf)) == NULL) {
return error ? error : ENOMEM;
}
if (decision == NPF_DECISION_PASS && !error) {
return 0;
}
if (!error) {
error = ENETUNREACH;
}
if (*mp) {
m_freem(*mp);
*mp = NULL;
}
return error;
}