Home | History | Annotate | Line # | Download | only in libnpftest
npf_bpf_test.c revision 1.9
      1  1.9     rmind /*	$NetBSD: npf_bpf_test.c,v 1.9 2018/09/29 14:41:36 rmind Exp $	*/
      2  1.1     rmind 
      3  1.1     rmind /*-
      4  1.1     rmind  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.1     rmind  * All rights reserved.
      6  1.1     rmind  *
      7  1.1     rmind  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     rmind  * by Mindaugas Rasiukevicius.
      9  1.1     rmind  *
     10  1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1     rmind  * modification, are permitted provided that the following conditions
     12  1.1     rmind  * are met:
     13  1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1     rmind  *
     19  1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     rmind  */
     31  1.1     rmind 
     32  1.1     rmind /*
     33  1.1     rmind  * NPF test of BPF coprocessor.
     34  1.1     rmind  */
     35  1.1     rmind 
     36  1.8  christos #ifdef _KERNEL
     37  1.1     rmind #include <sys/types.h>
     38  1.1     rmind #include <sys/endian.h>
     39  1.8  christos #endif
     40  1.1     rmind 
     41  1.1     rmind #define	NPF_BPFCOP
     42  1.1     rmind #include "npf_impl.h"
     43  1.1     rmind #include "npf_test.h"
     44  1.1     rmind 
     45  1.3     rmind static bool	lverbose = false;
     46  1.3     rmind 
     47  1.1     rmind static struct mbuf *
     48  1.1     rmind fill_packet(int proto)
     49  1.1     rmind {
     50  1.1     rmind 	struct mbuf *m;
     51  1.1     rmind 	struct ip *ip;
     52  1.1     rmind 	struct tcphdr *th;
     53  1.1     rmind 
     54  1.9     rmind 	m = mbuf_construct(proto);
     55  1.1     rmind 	th = mbuf_return_hdrs(m, false, &ip);
     56  1.1     rmind 	ip->ip_src.s_addr = inet_addr("192.168.2.100");
     57  1.1     rmind 	ip->ip_dst.s_addr = inet_addr("10.0.0.1");
     58  1.1     rmind 	th->th_sport = htons(15000);
     59  1.1     rmind 	th->th_dport = htons(80);
     60  1.1     rmind 	return m;
     61  1.1     rmind }
     62  1.1     rmind 
     63  1.1     rmind static int
     64  1.3     rmind test_bpf_code(void *code, size_t size)
     65  1.1     rmind {
     66  1.2     rmind 	ifnet_t *dummy_ifp = npf_test_addif(IFNAME_TEST, false, false);
     67  1.8  christos 	npf_cache_t npc = { .npc_info = 0, .npc_ctx = npf_getkernctx() };
     68  1.6     rmind 	uint32_t memstore[BPF_MEMWORDS];
     69  1.4     rmind 	bpf_args_t bc_args;
     70  1.1     rmind 	struct mbuf *m;
     71  1.1     rmind 	nbuf_t nbuf;
     72  1.3     rmind 	int ret, jret;
     73  1.3     rmind 	void *jcode;
     74  1.1     rmind 
     75  1.1     rmind 	/* Layer 3 (IP + TCP). */
     76  1.1     rmind 	m = fill_packet(IPPROTO_TCP);
     77  1.8  christos 	nbuf_init(npf_getkernctx(), &nbuf, m, dummy_ifp);
     78  1.7     rmind 	npc.npc_nbuf = &nbuf;
     79  1.7     rmind 	npf_cache_all(&npc);
     80  1.8  christos #ifdef _NPF_STANDALONE
     81  1.8  christos 	bc_args.pkt = (const uint8_t *)nbuf_dataptr(&nbuf);
     82  1.8  christos #else
     83  1.5     alnsn 	bc_args.pkt = (const uint8_t *)m;
     84  1.8  christos #endif
     85  1.6     rmind 	bc_args.buflen = m_length(m);
     86  1.6     rmind 	bc_args.wirelen = bc_args.buflen;
     87  1.6     rmind 	bc_args.mem = memstore;
     88  1.4     rmind 	bc_args.arg = &npc;
     89  1.4     rmind 
     90  1.4     rmind 	ret = npf_bpf_filter(&bc_args, code, NULL);
     91  1.3     rmind 
     92  1.3     rmind 	/* JIT-compiled code. */
     93  1.3     rmind 	jcode = npf_bpf_compile(code, size);
     94  1.3     rmind 	if (jcode) {
     95  1.4     rmind 		jret = npf_bpf_filter(&bc_args, NULL, jcode);
     96  1.9     rmind 		assert(ret == jret); (void)jret;
     97  1.3     rmind 		bpf_jit_freecode(jcode);
     98  1.3     rmind 	} else if (lverbose) {
     99  1.3     rmind 		printf("JIT-compilation failed\n");
    100  1.3     rmind 	}
    101  1.1     rmind 	m_freem(m);
    102  1.1     rmind 
    103  1.1     rmind 	return ret;
    104  1.1     rmind }
    105  1.1     rmind 
    106  1.1     rmind static uint32_t
    107  1.1     rmind npf_bpfcop_run(u_int reg)
    108  1.1     rmind {
    109  1.1     rmind 	struct bpf_insn insns_npf_bpfcop[] = {
    110  1.1     rmind 		BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
    111  1.1     rmind 		BPF_STMT(BPF_LD+BPF_W+BPF_MEM, reg),
    112  1.1     rmind 		BPF_STMT(BPF_RET+BPF_A, 0),
    113  1.1     rmind 	};
    114  1.3     rmind 	return test_bpf_code(&insns_npf_bpfcop, sizeof(insns_npf_bpfcop));
    115  1.1     rmind }
    116  1.1     rmind 
    117  1.1     rmind static bool
    118  1.1     rmind npf_bpfcop_test(void)
    119  1.1     rmind {
    120  1.1     rmind 	bool fail = false;
    121  1.1     rmind 
    122  1.1     rmind 	/* A <- IP version (4 or 6) */
    123  1.1     rmind 	struct bpf_insn insns_ipver[] = {
    124  1.1     rmind 		BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
    125  1.1     rmind 		BPF_STMT(BPF_RET+BPF_A, 0),
    126  1.1     rmind 	};
    127  1.3     rmind 	fail |= (test_bpf_code(&insns_ipver, sizeof(insns_ipver)) != IPVERSION);
    128  1.1     rmind 
    129  1.1     rmind 	/* BPF_MW_IPVERI <- IP version */
    130  1.1     rmind 	fail |= (npf_bpfcop_run(BPF_MW_IPVER) != IPVERSION);
    131  1.1     rmind 
    132  1.1     rmind 	/* BPF_MW_L4OFF <- L4 header offset */
    133  1.1     rmind 	fail |= (npf_bpfcop_run(BPF_MW_L4OFF) != sizeof(struct ip));
    134  1.1     rmind 
    135  1.1     rmind 	/* BPF_MW_L4PROTO <- L4 protocol */
    136  1.1     rmind 	fail |= (npf_bpfcop_run(BPF_MW_L4PROTO) != IPPROTO_TCP);
    137  1.1     rmind 
    138  1.1     rmind 	return fail;
    139  1.1     rmind }
    140  1.1     rmind 
    141  1.1     rmind bool
    142  1.1     rmind npf_bpf_test(bool verbose)
    143  1.1     rmind {
    144  1.1     rmind 	bool fail = false;
    145  1.1     rmind 
    146  1.3     rmind 	lverbose = verbose;
    147  1.3     rmind 
    148  1.1     rmind 	fail |= npf_bpfcop_test();
    149  1.1     rmind 
    150  1.1     rmind 	return !fail;
    151  1.1     rmind }
    152