1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4
serial_test_flow_dissector_load_bytes(void)5 void serial_test_flow_dissector_load_bytes(void)
6 {
7 struct bpf_flow_keys flow_keys;
8 __u32 duration = 0, retval, size;
9 struct bpf_insn prog[] = {
10 // BPF_REG_1 - 1st argument: context
11 // BPF_REG_2 - 2nd argument: offset, start at first byte
12 BPF_MOV64_IMM(BPF_REG_2, 0),
13 // BPF_REG_3 - 3rd argument: destination, reserve byte on stack
14 BPF_ALU64_REG(BPF_MOV, BPF_REG_3, BPF_REG_10),
15 BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -1),
16 // BPF_REG_4 - 4th argument: copy one byte
17 BPF_MOV64_IMM(BPF_REG_4, 1),
18 // bpf_skb_load_bytes(ctx, sizeof(pkt_v4), ptr, 1)
19 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
20 BPF_FUNC_skb_load_bytes),
21 BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
22 // if (ret == 0) return BPF_DROP (2)
23 BPF_MOV64_IMM(BPF_REG_0, BPF_DROP),
24 BPF_EXIT_INSN(),
25 // if (ret != 0) return BPF_OK (0)
26 BPF_MOV64_IMM(BPF_REG_0, BPF_OK),
27 BPF_EXIT_INSN(),
28 };
29 int fd, err;
30
31 /* make sure bpf_skb_load_bytes is not allowed from skb-less context
32 */
33 fd = bpf_load_program(BPF_PROG_TYPE_FLOW_DISSECTOR, prog,
34 ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
35 CHECK(fd < 0,
36 "flow_dissector-bpf_skb_load_bytes-load",
37 "fd %d errno %d\n",
38 fd, errno);
39
40 err = bpf_prog_test_run(fd, 1, &pkt_v4, sizeof(pkt_v4),
41 &flow_keys, &size, &retval, &duration);
42 CHECK(size != sizeof(flow_keys) || err || retval != 1,
43 "flow_dissector-bpf_skb_load_bytes",
44 "err %d errno %d retval %d duration %d size %u/%zu\n",
45 err, errno, retval, duration, size, sizeof(flow_keys));
46
47 if (fd >= -1)
48 close(fd);
49 }
50