Home | History | Annotate | Line # | Download | only in libnpftest
      1 /*-
      2  * Copyright (c) 2026 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Mindaugas Rasiukevicius.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * NPF tests of BPF coprocessor.
     32  */
     33 
     34 #ifdef _KERNEL
     35 #include <sys/types.h>
     36 #include <sys/endian.h>
     37 #endif
     38 
     39 #define	NPF_BPFCOP
     40 #include "npf_impl.h"
     41 #include "npf_test.h"
     42 
     43 static bool	lverbose = false;
     44 
     45 static int
     46 test_bpf_code(void *code, size_t size)
     47 {
     48 	uint32_t memstore[BPF_MEMWORDS];
     49 	bpf_args_t bc_args;
     50 	npf_cache_t *npc;
     51 	struct mbuf *m;
     52 	int ret, jret;
     53 	void *jcode;
     54 
     55 	/* Layer 3 (IP + TCP). */
     56 	m = mbuf_get_pkt(AF_INET, IPPROTO_TCP,
     57 	    "192.168.2.100", "10.0.0.1", 15000, 80);
     58 	npc = get_cached_pkt(m, NULL, NPF_RULE_LAYER_3);
     59 
     60 	npf_bpf_prepare(npc, &bc_args, memstore);
     61 
     62 	/* BPF byte code */
     63 	ret = npf_bpf_filter(&bc_args, code, NULL);
     64 
     65 	/* JIT-compiled code. */
     66 	jcode = npf_bpf_compile(code, size);
     67 	assert(jcode);
     68 	jret = npf_bpf_filter(&bc_args, NULL, jcode);
     69 
     70 	assert(ret == jret); (void)jret;
     71 	bpf_jit_freecode(jcode);
     72 
     73 	if (lverbose) {
     74 		printf("JIT-compilation succeeded\n");
     75 	}
     76 	put_cached_pkt(npc);
     77 	return ret;
     78 }
     79 
     80 static uint32_t
     81 npf_bpfcop_run(unsigned reg)
     82 {
     83 	struct bpf_insn insns_npf_bpfcop[] = {
     84 		BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
     85 		BPF_STMT(BPF_LD+BPF_W+BPF_MEM, reg),
     86 		BPF_STMT(BPF_RET+BPF_A, 0),
     87 	};
     88 	return test_bpf_code(&insns_npf_bpfcop, __arraycount(insns_npf_bpfcop));
     89 }
     90 
     91 static bool
     92 npf_bpfcop_test(void)
     93 {
     94 	/* A <- IP version (4 or 6) */
     95 	struct bpf_insn insns_ipver[] = {
     96 		BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
     97 		BPF_STMT(BPF_RET+BPF_A, 0),
     98 	};
     99 
    100 	// PR kern/60975 add validate tests to be more verbose
    101 	CHECK_TRUE(npf_bpf_validate(&insns_ipver, __arraycount(insns_ipver)) != 0);
    102 
    103 	CHECK_TRUE(test_bpf_code(&insns_ipver, __arraycount(insns_ipver)) == IPVERSION);
    104 
    105 	/* BPF_MW_IPVERI <- IP version */
    106 	CHECK_TRUE(npf_bpfcop_run(BPF_MW_IPVER) == IPVERSION);
    107 
    108 	/* BPF_MW_L4OFF <- L4 header offset */
    109 	CHECK_TRUE(npf_bpfcop_run(BPF_MW_L4OFF) == sizeof(struct ip));
    110 
    111 	/* BPF_MW_L4PROTO <- L4 protocol */
    112 	CHECK_TRUE(npf_bpfcop_run(BPF_MW_L4PROTO) == IPPROTO_TCP);
    113 
    114 	return true;
    115 }
    116 
    117 bool
    118 npf_bpf_test(bool verbose)
    119 {
    120 	lverbose = verbose;
    121 	return npf_bpfcop_test();
    122 }
    123