1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 
6 extern long bpf_kfunc_call_test4(signed char a, short b, int c, long d) __ksym;
7 extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;
8 extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b,
9 				  __u32 c, __u64 d) __ksym;
10 
11 extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
12 extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
13 extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym;
14 extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;
15 extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;
16 extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym;
17 extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;
18 extern int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p, const int rdwr_buf_size) __ksym;
19 extern int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p, const int rdonly_buf_size) __ksym;
20 extern u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused) __ksym;
21 
22 SEC("tc")
23 int kfunc_call_test4(struct __sk_buff *skb)
24 {
25 	struct bpf_sock *sk = skb->sk;
26 	long tmp;
27 
28 	if (!sk)
29 		return -1;
30 
31 	sk = bpf_sk_fullsock(sk);
32 	if (!sk)
33 		return -1;
34 
35 	tmp = bpf_kfunc_call_test4(-3, -30, -200, -1000);
36 	return (tmp >> 32) + tmp;
37 }
38 
39 SEC("tc")
40 int kfunc_call_test2(struct __sk_buff *skb)
41 {
42 	struct bpf_sock *sk = skb->sk;
43 
44 	if (!sk)
45 		return -1;
46 
47 	sk = bpf_sk_fullsock(sk);
48 	if (!sk)
49 		return -1;
50 
51 	return bpf_kfunc_call_test2((struct sock *)sk, 1, 2);
52 }
53 
54 SEC("tc")
55 int kfunc_call_test1(struct __sk_buff *skb)
56 {
57 	struct bpf_sock *sk = skb->sk;
58 	__u64 a = 1ULL << 32;
59 	__u32 ret;
60 
61 	if (!sk)
62 		return -1;
63 
64 	sk = bpf_sk_fullsock(sk);
65 	if (!sk)
66 		return -1;
67 
68 	a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4);
69 	ret = a >> 32;   /* ret should be 2 */
70 	ret += (__u32)a; /* ret should be 12 */
71 
72 	return ret;
73 }
74 
75 SEC("tc")
76 int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
77 {
78 	struct prog_test_ref_kfunc *pt;
79 	unsigned long s = 0;
80 	int ret = 0;
81 
82 	pt = bpf_kfunc_call_test_acquire(&s);
83 	if (pt) {
84 		if (pt->a != 42 || pt->b != 108)
85 			ret = -1;
86 		bpf_kfunc_call_test_release(pt);
87 	}
88 	return ret;
89 }
90 
91 SEC("tc")
92 int kfunc_call_test_pass(struct __sk_buff *skb)
93 {
94 	struct prog_test_pass1 p1 = {};
95 	struct prog_test_pass2 p2 = {};
96 	short a = 0;
97 	__u64 b = 0;
98 	long c = 0;
99 	char d = 0;
100 	int e = 0;
101 
102 	bpf_kfunc_call_test_pass_ctx(skb);
103 	bpf_kfunc_call_test_pass1(&p1);
104 	bpf_kfunc_call_test_pass2(&p2);
105 
106 	bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
107 	bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
108 	bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
109 	bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
110 	bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
111 	bpf_kfunc_call_test_mem_len_fail2(&b, -1);
112 
113 	return 0;
114 }
115 
116 struct syscall_test_args {
117 	__u8 data[16];
118 	size_t size;
119 };
120 
121 SEC("syscall")
122 int kfunc_syscall_test(struct syscall_test_args *args)
123 {
124 	const long size = args->size;
125 
126 	if (size > sizeof(args->data))
127 		return -7; /* -E2BIG */
128 
129 	bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(args->data));
130 	bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args));
131 	bpf_kfunc_call_test_mem_len_pass1(&args->data, size);
132 
133 	return 0;
134 }
135 
136 SEC("syscall")
137 int kfunc_syscall_test_null(struct syscall_test_args *args)
138 {
139 	/* Must be called with args as a NULL pointer
140 	 * we do not check for it to have the verifier consider that
141 	 * the pointer might not be null, and so we can load it.
142 	 *
143 	 * So the following can not be added:
144 	 *
145 	 * if (args)
146 	 *      return -22;
147 	 */
148 
149 	bpf_kfunc_call_test_mem_len_pass1(args, 0);
150 
151 	return 0;
152 }
153 
154 SEC("tc")
155 int kfunc_call_test_get_mem(struct __sk_buff *skb)
156 {
157 	struct prog_test_ref_kfunc *pt;
158 	unsigned long s = 0;
159 	int *p = NULL;
160 	int ret = 0;
161 
162 	pt = bpf_kfunc_call_test_acquire(&s);
163 	if (pt) {
164 		p = bpf_kfunc_call_test_get_rdwr_mem(pt, 2 * sizeof(int));
165 		if (p) {
166 			p[0] = 42;
167 			ret = p[1]; /* 108 */
168 		} else {
169 			ret = -1;
170 		}
171 
172 		if (ret >= 0) {
173 			p = bpf_kfunc_call_test_get_rdonly_mem(pt, 2 * sizeof(int));
174 			if (p)
175 				ret = p[0]; /* 42 */
176 			else
177 				ret = -1;
178 		}
179 
180 		bpf_kfunc_call_test_release(pt);
181 	}
182 	return ret;
183 }
184 
185 SEC("tc")
186 int kfunc_call_test_static_unused_arg(struct __sk_buff *skb)
187 {
188 
189 	u32 expected = 5, actual;
190 
191 	actual = bpf_kfunc_call_test_static_unused_arg(expected, 0xdeadbeef);
192 	return actual != expected ? -1 : 0;
193 }
194 
195 char _license[] SEC("license") = "GPL";
196