Adjust NPF to the recent BPF / BPF JIT changes and make it work again.

All regression tests are happy now (hi alnsn!).
This commit is contained in:
rmind 2014-06-25 00:20:06 +00:00
parent f7e5164657
commit 263d30c43e
5 changed files with 40 additions and 17 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: npf.h,v 1.40 2014/05/30 23:26:06 rmind Exp $ */
/* $NetBSD: npf.h,v 1.41 2014/06/25 00:20:06 rmind Exp $ */
/*-
* Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
@ -67,6 +67,8 @@ typedef uint8_t npf_netmask_t;
#define BPF_MW_L4OFF 1
#define BPF_MW_L4PROTO 2
#endif
/* The number of words used. */
#define NPF_BPF_NWORDS 3
#if defined(_KERNEL)

View File

@ -1,4 +1,4 @@
/* $NetBSD: npf_bpf.c,v 1.7 2014/06/24 11:31:49 alnsn Exp $ */
/* $NetBSD: npf_bpf.c,v 1.8 2014/06/25 00:20:06 rmind Exp $ */
/*-
* Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
@ -34,11 +34,12 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_bpf.c,v 1.7 2014/06/24 11:31:49 alnsn Exp $");
__KERNEL_RCSID(0, "$NetBSD: npf_bpf.c,v 1.8 2014/06/25 00:20:06 rmind Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/bitops.h>
#include <sys/mbuf.h>
#include <net/bpf.h>
@ -59,12 +60,15 @@ static const bpf_copfunc_t npf_bpfcop[] = {
[NPF_COP_TABLE] = npf_cop_table,
};
#define BPF_MW_ALLMASK \
((1U << BPF_MW_IPVER) | (1U << BPF_MW_L4OFF) | (1U << BPF_MW_L4PROTO))
void
npf_bpf_sysinit(void)
{
npf_bpfctx = bpf_create();
KASSERT(npf_bpfctx != NULL);
bpf_set_cop(npf_bpfctx, npf_bpfcop, __arraycount(npf_bpfcop));
bpf_set_extmem(npf_bpfctx, NPF_BPF_NWORDS, BPF_MW_ALLMASK);
}
void
@ -73,6 +77,20 @@ npf_bpf_sysfini(void)
bpf_destroy(npf_bpfctx);
}
void
npf_bpf_prepare(npf_cache_t *npc, nbuf_t *nbuf, bpf_args_t *args, uint32_t *m)
{
const struct mbuf *mbuf = nbuf_head_mbuf(nbuf);
const size_t pktlen = m_length(mbuf);
/* Prepare the arguments for the BPF programs. */
args->pkt = (const uint8_t *)mbuf;
args->wirelen = pktlen;
args->buflen = 0;
args->mem = m;
args->arg = npc;
}
int
npf_bpf_filter(bpf_args_t *args, const void *code, bpfjit_func_t jcode)
{

View File

@ -1,4 +1,4 @@
/* $NetBSD: npf_impl.h,v 1.52 2014/05/30 23:26:06 rmind Exp $ */
/* $NetBSD: npf_impl.h,v 1.53 2014/06/25 00:20:06 rmind Exp $ */
/*-
* Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
@ -223,6 +223,7 @@ bool npf_return_block(npf_cache_t *, nbuf_t *, const int);
/* BPF interface. */
void npf_bpf_sysinit(void);
void npf_bpf_sysfini(void);
void npf_bpf_prepare(npf_cache_t *, nbuf_t *, bpf_args_t *, uint32_t *);
int npf_bpf_filter(bpf_args_t *, const void *, bpfjit_func_t);
void * npf_bpf_compile(void *, size_t);
bool npf_bpf_validate(const void *, size_t);

View File

@ -1,4 +1,4 @@
/* $NetBSD: npf_ruleset.c,v 1.32 2014/06/24 10:53:30 alnsn Exp $ */
/* $NetBSD: npf_ruleset.c,v 1.33 2014/06/25 00:20:06 rmind Exp $ */
/*-
* Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
@ -34,7 +34,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.32 2014/06/24 10:53:30 alnsn Exp $");
__KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.33 2014/06/25 00:20:06 rmind Exp $");
#include <sys/param.h>
#include <sys/types.h>
@ -741,18 +741,18 @@ npf_ruleset_inspect(npf_cache_t *npc, nbuf_t *nbuf,
const u_int nitems = rlset->rs_nitems;
const u_int ifid = nbuf->nb_ifid;
npf_rule_t *final_rl = NULL;
const struct mbuf *m;
bpf_args_t bc_args;
u_int n = 0;
memset(&bc_args, 0, sizeof(bpf_args_t));
m = nbuf_head_mbuf(nbuf);
bc_args.pkt = (const uint8_t *)m;
bc_args.wirelen = m_length(m);
bc_args.arg = npc;
KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
/*
* Prepare the external memory store and the arguments for
* the BPF programs to be executed.
*/
uint32_t bc_words[NPF_BPF_NWORDS];
npf_bpf_prepare(npc, nbuf, &bc_args, bc_words);
while (n < nitems) {
npf_rule_t *rl = rlset->rs_rules[n];
const u_int skip_to = rl->r_skip_to;

View File

@ -1,4 +1,4 @@
/* $NetBSD: npf_bpf_test.c,v 1.5 2014/06/24 10:53:30 alnsn Exp $ */
/* $NetBSD: npf_bpf_test.c,v 1.6 2014/06/25 00:20:06 rmind Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
@ -63,6 +63,7 @@ test_bpf_code(void *code, size_t size)
{
ifnet_t *dummy_ifp = npf_test_addif(IFNAME_TEST, false, false);
npf_cache_t npc = { .npc_info = 0 };
uint32_t memstore[BPF_MEMWORDS];
bpf_args_t bc_args;
struct mbuf *m;
nbuf_t nbuf;
@ -74,9 +75,10 @@ test_bpf_code(void *code, size_t size)
nbuf_init(&nbuf, m, dummy_ifp);
npf_cache_all(&npc, &nbuf);
memset(&bc_args, 0, sizeof(bpf_args_t));
bc_args.pkt = (const uint8_t *)m;
bc_args.wirelen = m_length(m);
bc_args.buflen = m_length(m);
bc_args.wirelen = bc_args.buflen;
bc_args.mem = memstore;
bc_args.arg = &npc;
ret = npf_bpf_filter(&bc_args, code, NULL);