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