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