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