npf_bpf_test.c revision 1.1 1 /* $NetBSD: npf_bpf_test.c,v 1.1 2013/09/19 01:04:45 rmind 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 #include <sys/types.h>
37 #include <sys/endian.h>
38
39 #define NPF_BPFCOP
40 #include "npf_impl.h"
41 #include "npf_test.h"
42
43 static struct mbuf *
44 fill_packet(int proto)
45 {
46 struct mbuf *m;
47 struct ip *ip;
48 struct tcphdr *th;
49
50 m = mbuf_construct(IPPROTO_TCP);
51 th = mbuf_return_hdrs(m, false, &ip);
52 ip->ip_src.s_addr = inet_addr("192.168.2.100");
53 ip->ip_dst.s_addr = inet_addr("10.0.0.1");
54 th->th_sport = htons(15000);
55 th->th_dport = htons(80);
56 return m;
57 }
58
59 static int
60 test_bpf_code(const void *code)
61 {
62 npf_cache_t npc = { .npc_info = 0 };
63 const void *dummy_ifp = (void *)0xdeadbeef;
64 struct mbuf *m;
65 nbuf_t nbuf;
66 int ret;
67
68 /* Layer 3 (IP + TCP). */
69 m = fill_packet(IPPROTO_TCP);
70 nbuf_init(&nbuf, m, dummy_ifp);
71 npf_cache_all(&npc, &nbuf);
72
73 ret = npf_bpf_filter(&npc, &nbuf, code, NULL);
74 m_freem(m);
75
76 return ret;
77 }
78
79 static uint32_t
80 npf_bpfcop_run(u_int reg)
81 {
82 struct bpf_insn insns_npf_bpfcop[] = {
83 BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
84 BPF_STMT(BPF_LD+BPF_W+BPF_MEM, reg),
85 BPF_STMT(BPF_RET+BPF_A, 0),
86 };
87 return test_bpf_code(&insns_npf_bpfcop);
88 }
89
90 static bool
91 npf_bpfcop_test(void)
92 {
93 bool fail = false;
94
95 /* A <- IP version (4 or 6) */
96 struct bpf_insn insns_ipver[] = {
97 BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
98 BPF_STMT(BPF_RET+BPF_A, 0),
99 };
100 fail |= (test_bpf_code(&insns_ipver) != IPVERSION);
101
102 /* BPF_MW_IPVERI <- IP version */
103 fail |= (npf_bpfcop_run(BPF_MW_IPVER) != IPVERSION);
104
105 /* BPF_MW_L4OFF <- L4 header offset */
106 fail |= (npf_bpfcop_run(BPF_MW_L4OFF) != sizeof(struct ip));
107
108 /* BPF_MW_L4PROTO <- L4 protocol */
109 fail |= (npf_bpfcop_run(BPF_MW_L4PROTO) != IPPROTO_TCP);
110
111 return fail;
112 }
113
114 bool
115 npf_bpf_test(bool verbose)
116 {
117 bool fail = false;
118
119 fail |= npf_bpfcop_test();
120
121 return !fail;
122 }
123