xref: /linux/tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <vmlinux.h>
4 #include "bpf_experimental.h"
5 #include "bpf_qdisc_common.h"
6 #include "bpf_misc.h"
7 
8 char _license[] SEC("license") = "GPL";
9 
10 int proto;
11 
12 SEC("struct_ops")
13 __failure __msg("Expected an initialized dynptr as R1")
14 int BPF_PROG(invalid_dynptr, struct sk_buff *skb, struct Qdisc *sch,
15 	     struct bpf_sk_buff_ptr *to_free)
16 {
17 	struct bpf_dynptr ptr;
18 	struct ethhdr *hdr;
19 
20 	bpf_dynptr_from_skb((struct __sk_buff *)skb, 0, &ptr);
21 
22 	bpf_qdisc_skb_drop(skb, to_free);
23 
24 	hdr = bpf_dynptr_slice(&ptr, 0, NULL, sizeof(*hdr));
25 	if (!hdr)
26 		return NET_XMIT_DROP;
27 
28 	proto = hdr->h_proto;
29 
30 	return NET_XMIT_DROP;
31 }
32 
33 SEC("struct_ops")
34 __auxiliary
35 struct sk_buff *BPF_PROG(bpf_qdisc_test_dequeue, struct Qdisc *sch)
36 {
37 	return NULL;
38 }
39 
40 SEC("struct_ops")
41 __auxiliary
42 int BPF_PROG(bpf_qdisc_test_init, struct Qdisc *sch, struct nlattr *opt,
43 	     struct netlink_ext_ack *extack)
44 {
45 	return 0;
46 }
47 
48 SEC("struct_ops")
49 __auxiliary
50 void BPF_PROG(bpf_qdisc_test_reset, struct Qdisc *sch)
51 {
52 }
53 
54 SEC("struct_ops")
55 __auxiliary
56 void BPF_PROG(bpf_qdisc_test_destroy, struct Qdisc *sch)
57 {
58 }
59 
60 SEC(".struct_ops")
61 struct Qdisc_ops test = {
62 	.enqueue   = (void *)invalid_dynptr,
63 	.dequeue   = (void *)bpf_qdisc_test_dequeue,
64 	.init      = (void *)bpf_qdisc_test_init,
65 	.reset     = (void *)bpf_qdisc_test_reset,
66 	.destroy   = (void *)bpf_qdisc_test_destroy,
67 	.id        = "bpf_qdisc_test",
68 };
69