diff --git a/sys/net/npf/npf.h b/sys/net/npf/npf.h index 065c862a75a0..e250f6d51305 100644 --- a/sys/net/npf/npf.h +++ b/sys/net/npf/npf.h @@ -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) diff --git a/sys/net/npf/npf_bpf.c b/sys/net/npf/npf_bpf.c index 015300d0e69b..2f82d0f15cc9 100644 --- a/sys/net/npf/npf_bpf.c +++ b/sys/net/npf/npf_bpf.c @@ -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 -__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 #include +#include #include #include @@ -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) { diff --git a/sys/net/npf/npf_impl.h b/sys/net/npf/npf_impl.h index f14bdf2ddfa6..61ee67140d40 100644 --- a/sys/net/npf/npf_impl.h +++ b/sys/net/npf/npf_impl.h @@ -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); diff --git a/sys/net/npf/npf_ruleset.c b/sys/net/npf/npf_ruleset.c index 8cab4f04f651..335c0304718c 100644 --- a/sys/net/npf/npf_ruleset.c +++ b/sys/net/npf/npf_ruleset.c @@ -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 -__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 #include @@ -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; diff --git a/usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c b/usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c index 5c2780432a34..83ad4f9a0e1f 100644 --- a/usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c +++ b/usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c @@ -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);