1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 struct meta {
6 	int ifindex;
7 	__u32 cb32_0;
8 	__u8 cb8_0;
9 };
10 
11 static union {
12 	__u32 cb32[5];
13 	__u8 cb8[20];
14 } cb = {
15 	.cb32[0] = 0x81828384,
16 };
17 
18 static void on_sample(void *ctx, int cpu, void *data, __u32 size)
19 {
20 	struct meta *meta = (struct meta *)data;
21 	struct ipv6_packet *pkt_v6 = data + sizeof(*meta);
22 	int duration = 0;
23 
24 	if (CHECK(size != 72 + sizeof(*meta), "check_size", "size %u != %zu\n",
25 		  size, 72 + sizeof(*meta)))
26 		return;
27 	if (CHECK(meta->ifindex != 1, "check_meta_ifindex",
28 		  "meta->ifindex = %d\n", meta->ifindex))
29 		/* spurious kfree_skb not on loopback device */
30 		return;
31 	if (CHECK(meta->cb8_0 != cb.cb8[0], "check_cb8_0", "cb8_0 %x != %x\n",
32 		  meta->cb8_0, cb.cb8[0]))
33 		return;
34 	if (CHECK(meta->cb32_0 != cb.cb32[0], "check_cb32_0",
35 		  "cb32_0 %x != %x\n",
36 		  meta->cb32_0, cb.cb32[0]))
37 		return;
38 	if (CHECK(pkt_v6->eth.h_proto != 0xdd86, "check_eth",
39 		  "h_proto %x\n", pkt_v6->eth.h_proto))
40 		return;
41 	if (CHECK(pkt_v6->iph.nexthdr != 6, "check_ip",
42 		  "iph.nexthdr %x\n", pkt_v6->iph.nexthdr))
43 		return;
44 	if (CHECK(pkt_v6->tcp.doff != 5, "check_tcp",
45 		  "tcp.doff %x\n", pkt_v6->tcp.doff))
46 		return;
47 
48 	*(bool *)ctx = true;
49 }
50 
51 void test_kfree_skb(void)
52 {
53 	struct __sk_buff skb = {};
54 	struct bpf_prog_test_run_attr tattr = {
55 		.data_in = &pkt_v6,
56 		.data_size_in = sizeof(pkt_v6),
57 		.ctx_in = &skb,
58 		.ctx_size_in = sizeof(skb),
59 	};
60 	struct bpf_prog_load_attr attr = {
61 		.file = "./kfree_skb.o",
62 	};
63 
64 	struct bpf_link *link = NULL, *link_fentry = NULL, *link_fexit = NULL;
65 	struct bpf_map *perf_buf_map, *global_data;
66 	struct bpf_program *prog, *fentry, *fexit;
67 	struct bpf_object *obj, *obj2 = NULL;
68 	struct perf_buffer_opts pb_opts = {};
69 	struct perf_buffer *pb = NULL;
70 	int err, kfree_skb_fd;
71 	bool passed = false;
72 	__u32 duration = 0;
73 	const int zero = 0;
74 	bool test_ok[2];
75 
76 	err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
77 			    &obj, &tattr.prog_fd);
78 	if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
79 		return;
80 
81 	err = bpf_prog_load_xattr(&attr, &obj2, &kfree_skb_fd);
82 	if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
83 		goto close_prog;
84 
85 	prog = bpf_object__find_program_by_title(obj2, "tp_btf/kfree_skb");
86 	if (CHECK(!prog, "find_prog", "prog kfree_skb not found\n"))
87 		goto close_prog;
88 	fentry = bpf_object__find_program_by_title(obj2, "fentry/eth_type_trans");
89 	if (CHECK(!fentry, "find_prog", "prog eth_type_trans not found\n"))
90 		goto close_prog;
91 	fexit = bpf_object__find_program_by_title(obj2, "fexit/eth_type_trans");
92 	if (CHECK(!fexit, "find_prog", "prog eth_type_trans not found\n"))
93 		goto close_prog;
94 
95 	global_data = bpf_object__find_map_by_name(obj2, "kfree_sk.bss");
96 	if (CHECK(!global_data, "find global data", "not found\n"))
97 		goto close_prog;
98 
99 	link = bpf_program__attach_raw_tracepoint(prog, NULL);
100 	if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
101 		goto close_prog;
102 	link_fentry = bpf_program__attach_trace(fentry);
103 	if (!ASSERT_OK_PTR(link_fentry, "attach fentry"))
104 		goto close_prog;
105 	link_fexit = bpf_program__attach_trace(fexit);
106 	if (!ASSERT_OK_PTR(link_fexit, "attach fexit"))
107 		goto close_prog;
108 
109 	perf_buf_map = bpf_object__find_map_by_name(obj2, "perf_buf_map");
110 	if (CHECK(!perf_buf_map, "find_perf_buf_map", "not found\n"))
111 		goto close_prog;
112 
113 	/* set up perf buffer */
114 	pb_opts.sample_cb = on_sample;
115 	pb_opts.ctx = &passed;
116 	pb = perf_buffer__new(bpf_map__fd(perf_buf_map), 1, &pb_opts);
117 	if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
118 		goto close_prog;
119 
120 	memcpy(skb.cb, &cb, sizeof(cb));
121 	err = bpf_prog_test_run_xattr(&tattr);
122 	duration = tattr.duration;
123 	CHECK(err || tattr.retval, "ipv6",
124 	      "err %d errno %d retval %d duration %d\n",
125 	      err, errno, tattr.retval, duration);
126 
127 	/* read perf buffer */
128 	err = perf_buffer__poll(pb, 100);
129 	if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
130 		goto close_prog;
131 
132 	/* make sure kfree_skb program was triggered
133 	 * and it sent expected skb into ring buffer
134 	 */
135 	ASSERT_TRUE(passed, "passed");
136 
137 	err = bpf_map_lookup_elem(bpf_map__fd(global_data), &zero, test_ok);
138 	if (CHECK(err, "get_result",
139 		  "failed to get output data: %d\n", err))
140 		goto close_prog;
141 
142 	CHECK_FAIL(!test_ok[0] || !test_ok[1]);
143 close_prog:
144 	perf_buffer__free(pb);
145 	bpf_link__destroy(link);
146 	bpf_link__destroy(link_fentry);
147 	bpf_link__destroy(link_fexit);
148 	bpf_object__close(obj);
149 	bpf_object__close(obj2);
150 }
151