npf_bpf_test.c revision 1.6 1 1.6 rmind /* $NetBSD: npf_bpf_test.c,v 1.6 2014/06/25 00:20:06 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.1 rmind #include <sys/types.h>
37 1.1 rmind #include <sys/endian.h>
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.1 rmind m = mbuf_construct(IPPROTO_TCP);
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.1 rmind npf_cache_t npc = { .npc_info = 0 };
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.1 rmind nbuf_init(&nbuf, m, dummy_ifp);
76 1.1 rmind npf_cache_all(&npc, &nbuf);
77 1.1 rmind
78 1.5 alnsn bc_args.pkt = (const uint8_t *)m;
79 1.6 rmind bc_args.buflen = m_length(m);
80 1.6 rmind bc_args.wirelen = bc_args.buflen;
81 1.6 rmind bc_args.mem = memstore;
82 1.4 rmind bc_args.arg = &npc;
83 1.4 rmind
84 1.4 rmind ret = npf_bpf_filter(&bc_args, code, NULL);
85 1.3 rmind
86 1.3 rmind /* JIT-compiled code. */
87 1.3 rmind jcode = npf_bpf_compile(code, size);
88 1.3 rmind if (jcode) {
89 1.4 rmind jret = npf_bpf_filter(&bc_args, NULL, jcode);
90 1.3 rmind assert(ret == jret);
91 1.3 rmind bpf_jit_freecode(jcode);
92 1.3 rmind } else if (lverbose) {
93 1.3 rmind printf("JIT-compilation failed\n");
94 1.3 rmind }
95 1.1 rmind m_freem(m);
96 1.1 rmind
97 1.1 rmind return ret;
98 1.1 rmind }
99 1.1 rmind
100 1.1 rmind static uint32_t
101 1.1 rmind npf_bpfcop_run(u_int reg)
102 1.1 rmind {
103 1.1 rmind struct bpf_insn insns_npf_bpfcop[] = {
104 1.1 rmind BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
105 1.1 rmind BPF_STMT(BPF_LD+BPF_W+BPF_MEM, reg),
106 1.1 rmind BPF_STMT(BPF_RET+BPF_A, 0),
107 1.1 rmind };
108 1.3 rmind return test_bpf_code(&insns_npf_bpfcop, sizeof(insns_npf_bpfcop));
109 1.1 rmind }
110 1.1 rmind
111 1.1 rmind static bool
112 1.1 rmind npf_bpfcop_test(void)
113 1.1 rmind {
114 1.1 rmind bool fail = false;
115 1.1 rmind
116 1.1 rmind /* A <- IP version (4 or 6) */
117 1.1 rmind struct bpf_insn insns_ipver[] = {
118 1.1 rmind BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
119 1.1 rmind BPF_STMT(BPF_RET+BPF_A, 0),
120 1.1 rmind };
121 1.3 rmind fail |= (test_bpf_code(&insns_ipver, sizeof(insns_ipver)) != IPVERSION);
122 1.1 rmind
123 1.1 rmind /* BPF_MW_IPVERI <- IP version */
124 1.1 rmind fail |= (npf_bpfcop_run(BPF_MW_IPVER) != IPVERSION);
125 1.1 rmind
126 1.1 rmind /* BPF_MW_L4OFF <- L4 header offset */
127 1.1 rmind fail |= (npf_bpfcop_run(BPF_MW_L4OFF) != sizeof(struct ip));
128 1.1 rmind
129 1.1 rmind /* BPF_MW_L4PROTO <- L4 protocol */
130 1.1 rmind fail |= (npf_bpfcop_run(BPF_MW_L4PROTO) != IPPROTO_TCP);
131 1.1 rmind
132 1.1 rmind return fail;
133 1.1 rmind }
134 1.1 rmind
135 1.1 rmind bool
136 1.1 rmind npf_bpf_test(bool verbose)
137 1.1 rmind {
138 1.1 rmind bool fail = false;
139 1.1 rmind
140 1.3 rmind lverbose = verbose;
141 1.3 rmind
142 1.1 rmind fail |= npf_bpfcop_test();
143 1.1 rmind
144 1.1 rmind return !fail;
145 1.1 rmind }
146