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