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 int 46 test_bpf_code(void *code, size_t size) 47 { 48 uint32_t memstore[BPF_MEMWORDS]; 49 bpf_args_t bc_args; 50 npf_cache_t *npc; 51 struct mbuf *m; 52 int ret, jret; 53 void *jcode; 54 55 /* Layer 3 (IP + TCP). */ 56 m = mbuf_get_pkt(AF_INET, IPPROTO_TCP, 57 "192.168.2.100", "10.0.0.1", 15000, 80); 58 npc = get_cached_pkt(m, NULL); 59 #ifdef _NPF_STANDALONE 60 bc_args.pkt = (const uint8_t *)nbuf_dataptr(npc->npc_nbuf); 61 #else 62 bc_args.pkt = (const uint8_t *)m; 63 #endif 64 bc_args.buflen = m_length(m); 65 bc_args.wirelen = bc_args.buflen; 66 bc_args.mem = memstore; 67 bc_args.arg = npc; 68 69 ret = npf_bpf_filter(&bc_args, code, NULL); 70 71 /* JIT-compiled code. */ 72 jcode = npf_bpf_compile(code, size); 73 if (jcode) { 74 jret = npf_bpf_filter(&bc_args, NULL, jcode); 75 assert(ret == jret); (void)jret; 76 bpf_jit_freecode(jcode); 77 } else if (lverbose) { 78 printf("JIT-compilation failed\n"); 79 } 80 put_cached_pkt(npc); 81 return ret; 82 } 83 84 static uint32_t 85 npf_bpfcop_run(unsigned reg) 86 { 87 struct bpf_insn insns_npf_bpfcop[] = { 88 BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3), 89 BPF_STMT(BPF_LD+BPF_W+BPF_MEM, reg), 90 BPF_STMT(BPF_RET+BPF_A, 0), 91 }; 92 return test_bpf_code(&insns_npf_bpfcop, sizeof(insns_npf_bpfcop)); 93 } 94 95 static bool 96 npf_bpfcop_test(void) 97 { 98 /* A <- IP version (4 or 6) */ 99 struct bpf_insn insns_ipver[] = { 100 BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3), 101 BPF_STMT(BPF_RET+BPF_A, 0), 102 }; 103 CHECK_TRUE(test_bpf_code(&insns_ipver, sizeof(insns_ipver)) == IPVERSION); 104 105 /* BPF_MW_IPVERI <- IP version */ 106 CHECK_TRUE(npf_bpfcop_run(BPF_MW_IPVER) == IPVERSION); 107 108 /* BPF_MW_L4OFF <- L4 header offset */ 109 CHECK_TRUE(npf_bpfcop_run(BPF_MW_L4OFF) == sizeof(struct ip)); 110 111 /* BPF_MW_L4PROTO <- L4 protocol */ 112 CHECK_TRUE(npf_bpfcop_run(BPF_MW_L4PROTO) == IPPROTO_TCP); 113 114 return true; 115 } 116 117 bool 118 npf_bpf_test(bool verbose) 119 { 120 lverbose = verbose; 121 return npf_bpfcop_test(); 122 } 123