1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  */
4 #ifndef _LINUX_BPF_H
5 #define _LINUX_BPF_H 1
6 
7 #include <uapi/linux/bpf.h>
8 #include <uapi/linux/filter.h>
9 
10 #include <linux/workqueue.h>
11 #include <linux/file.h>
12 #include <linux/percpu.h>
13 #include <linux/err.h>
14 #include <linux/rbtree_latch.h>
15 #include <linux/numa.h>
16 #include <linux/mm_types.h>
17 #include <linux/wait.h>
18 #include <linux/refcount.h>
19 #include <linux/mutex.h>
20 #include <linux/module.h>
21 #include <linux/kallsyms.h>
22 #include <linux/capability.h>
23 #include <linux/sched/mm.h>
24 #include <linux/slab.h>
25 #include <linux/percpu-refcount.h>
26 #include <linux/stddef.h>
27 #include <linux/bpfptr.h>
28 #include <linux/btf.h>
29 #include <linux/rcupdate_trace.h>
30 #include <linux/static_call.h>
31 
32 struct bpf_verifier_env;
33 struct bpf_verifier_log;
34 struct perf_event;
35 struct bpf_prog;
36 struct bpf_prog_aux;
37 struct bpf_map;
38 struct sock;
39 struct seq_file;
40 struct btf;
41 struct btf_type;
42 struct exception_table_entry;
43 struct seq_operations;
44 struct bpf_iter_aux_info;
45 struct bpf_local_storage;
46 struct bpf_local_storage_map;
47 struct kobject;
48 struct mem_cgroup;
49 struct module;
50 struct bpf_func_state;
51 struct ftrace_ops;
52 struct cgroup;
53 
54 extern struct idr btf_idr;
55 extern spinlock_t btf_idr_lock;
56 extern struct kobject *btf_kobj;
57 
58 typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
59 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
60 					struct bpf_iter_aux_info *aux);
61 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
62 typedef unsigned int (*bpf_func_t)(const void *,
63 				   const struct bpf_insn *);
64 struct bpf_iter_seq_info {
65 	const struct seq_operations *seq_ops;
66 	bpf_iter_init_seq_priv_t init_seq_private;
67 	bpf_iter_fini_seq_priv_t fini_seq_private;
68 	u32 seq_priv_size;
69 };
70 
71 /* map is generic key/value storage optionally accessible by eBPF programs */
72 struct bpf_map_ops {
73 	/* funcs callable from userspace (via syscall) */
74 	int (*map_alloc_check)(union bpf_attr *attr);
75 	struct bpf_map *(*map_alloc)(union bpf_attr *attr);
76 	void (*map_release)(struct bpf_map *map, struct file *map_file);
77 	void (*map_free)(struct bpf_map *map);
78 	int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
79 	void (*map_release_uref)(struct bpf_map *map);
80 	void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
81 	int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
82 				union bpf_attr __user *uattr);
83 	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
84 					  void *value, u64 flags);
85 	int (*map_lookup_and_delete_batch)(struct bpf_map *map,
86 					   const union bpf_attr *attr,
87 					   union bpf_attr __user *uattr);
88 	int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
89 				union bpf_attr __user *uattr);
90 	int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
91 				union bpf_attr __user *uattr);
92 
93 	/* funcs callable from userspace and from eBPF programs */
94 	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
95 	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
96 	int (*map_delete_elem)(struct bpf_map *map, void *key);
97 	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
98 	int (*map_pop_elem)(struct bpf_map *map, void *value);
99 	int (*map_peek_elem)(struct bpf_map *map, void *value);
100 	void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
101 
102 	/* funcs called by prog_array and perf_event_array map */
103 	void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
104 				int fd);
105 	void (*map_fd_put_ptr)(void *ptr);
106 	int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
107 	u32 (*map_fd_sys_lookup_elem)(void *ptr);
108 	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
109 				  struct seq_file *m);
110 	int (*map_check_btf)(const struct bpf_map *map,
111 			     const struct btf *btf,
112 			     const struct btf_type *key_type,
113 			     const struct btf_type *value_type);
114 
115 	/* Prog poke tracking helpers. */
116 	int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
117 	void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
118 	void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
119 			     struct bpf_prog *new);
120 
121 	/* Direct value access helpers. */
122 	int (*map_direct_value_addr)(const struct bpf_map *map,
123 				     u64 *imm, u32 off);
124 	int (*map_direct_value_meta)(const struct bpf_map *map,
125 				     u64 imm, u32 *off);
126 	int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
127 	__poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
128 			     struct poll_table_struct *pts);
129 
130 	/* Functions called by bpf_local_storage maps */
131 	int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
132 					void *owner, u32 size);
133 	void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
134 					   void *owner, u32 size);
135 	struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
136 
137 	/* Misc helpers.*/
138 	int (*map_redirect)(struct bpf_map *map, u32 ifindex, u64 flags);
139 
140 	/* map_meta_equal must be implemented for maps that can be
141 	 * used as an inner map.  It is a runtime check to ensure
142 	 * an inner map can be inserted to an outer map.
143 	 *
144 	 * Some properties of the inner map has been used during the
145 	 * verification time.  When inserting an inner map at the runtime,
146 	 * map_meta_equal has to ensure the inserting map has the same
147 	 * properties that the verifier has used earlier.
148 	 */
149 	bool (*map_meta_equal)(const struct bpf_map *meta0,
150 			       const struct bpf_map *meta1);
151 
152 
153 	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
154 					      struct bpf_func_state *caller,
155 					      struct bpf_func_state *callee);
156 	int (*map_for_each_callback)(struct bpf_map *map,
157 				     bpf_callback_t callback_fn,
158 				     void *callback_ctx, u64 flags);
159 
160 	/* BTF id of struct allocated by map_alloc */
161 	int *map_btf_id;
162 
163 	/* bpf_iter info used to open a seq_file */
164 	const struct bpf_iter_seq_info *iter_seq_info;
165 };
166 
167 enum {
168 	/* Support at most 8 pointers in a BPF map value */
169 	BPF_MAP_VALUE_OFF_MAX = 8,
170 	BPF_MAP_OFF_ARR_MAX   = BPF_MAP_VALUE_OFF_MAX +
171 				1 + /* for bpf_spin_lock */
172 				1,  /* for bpf_timer */
173 };
174 
175 enum bpf_kptr_type {
176 	BPF_KPTR_UNREF,
177 	BPF_KPTR_REF,
178 };
179 
180 struct bpf_map_value_off_desc {
181 	u32 offset;
182 	enum bpf_kptr_type type;
183 	struct {
184 		struct btf *btf;
185 		struct module *module;
186 		btf_dtor_kfunc_t dtor;
187 		u32 btf_id;
188 	} kptr;
189 };
190 
191 struct bpf_map_value_off {
192 	u32 nr_off;
193 	struct bpf_map_value_off_desc off[];
194 };
195 
196 struct bpf_map_off_arr {
197 	u32 cnt;
198 	u32 field_off[BPF_MAP_OFF_ARR_MAX];
199 	u8 field_sz[BPF_MAP_OFF_ARR_MAX];
200 };
201 
202 struct bpf_map {
203 	/* The first two cachelines with read-mostly members of which some
204 	 * are also accessed in fast-path (e.g. ops, max_entries).
205 	 */
206 	const struct bpf_map_ops *ops ____cacheline_aligned;
207 	struct bpf_map *inner_map_meta;
208 #ifdef CONFIG_SECURITY
209 	void *security;
210 #endif
211 	enum bpf_map_type map_type;
212 	u32 key_size;
213 	u32 value_size;
214 	u32 max_entries;
215 	u64 map_extra; /* any per-map-type extra fields */
216 	u32 map_flags;
217 	int spin_lock_off; /* >=0 valid offset, <0 error */
218 	struct bpf_map_value_off *kptr_off_tab;
219 	int timer_off; /* >=0 valid offset, <0 error */
220 	u32 id;
221 	int numa_node;
222 	u32 btf_key_type_id;
223 	u32 btf_value_type_id;
224 	u32 btf_vmlinux_value_type_id;
225 	struct btf *btf;
226 #ifdef CONFIG_MEMCG_KMEM
227 	struct obj_cgroup *objcg;
228 #endif
229 	char name[BPF_OBJ_NAME_LEN];
230 	struct bpf_map_off_arr *off_arr;
231 	/* The 3rd and 4th cacheline with misc members to avoid false sharing
232 	 * particularly with refcounting.
233 	 */
234 	atomic64_t refcnt ____cacheline_aligned;
235 	atomic64_t usercnt;
236 	struct work_struct work;
237 	struct mutex freeze_mutex;
238 	atomic64_t writecnt;
239 	/* 'Ownership' of program-containing map is claimed by the first program
240 	 * that is going to use this map or by the first program which FD is
241 	 * stored in the map to make sure that all callers and callees have the
242 	 * same prog type, JITed flag and xdp_has_frags flag.
243 	 */
244 	struct {
245 		spinlock_t lock;
246 		enum bpf_prog_type type;
247 		bool jited;
248 		bool xdp_has_frags;
249 	} owner;
250 	bool bypass_spec_v1;
251 	bool frozen; /* write-once; write-protected by freeze_mutex */
252 };
253 
map_value_has_spin_lock(const struct bpf_map * map)254 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
255 {
256 	return map->spin_lock_off >= 0;
257 }
258 
map_value_has_timer(const struct bpf_map * map)259 static inline bool map_value_has_timer(const struct bpf_map *map)
260 {
261 	return map->timer_off >= 0;
262 }
263 
map_value_has_kptrs(const struct bpf_map * map)264 static inline bool map_value_has_kptrs(const struct bpf_map *map)
265 {
266 	return !IS_ERR_OR_NULL(map->kptr_off_tab);
267 }
268 
check_and_init_map_value(struct bpf_map * map,void * dst)269 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
270 {
271 	if (unlikely(map_value_has_spin_lock(map)))
272 		memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
273 	if (unlikely(map_value_has_timer(map)))
274 		memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
275 	if (unlikely(map_value_has_kptrs(map))) {
276 		struct bpf_map_value_off *tab = map->kptr_off_tab;
277 		int i;
278 
279 		for (i = 0; i < tab->nr_off; i++)
280 			*(u64 *)(dst + tab->off[i].offset) = 0;
281 	}
282 }
283 
284 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
285  * forced to use 'long' read/writes to try to atomically copy long counters.
286  * Best-effort only.  No barriers here, since it _will_ race with concurrent
287  * updates from BPF programs. Called from bpf syscall and mostly used with
288  * size 8 or 16 bytes, so ask compiler to inline it.
289  */
bpf_long_memcpy(void * dst,const void * src,u32 size)290 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
291 {
292 	const long *lsrc = src;
293 	long *ldst = dst;
294 
295 	size /= sizeof(long);
296 	while (size--)
297 		*ldst++ = *lsrc++;
298 }
299 
300 /* copy everything but bpf_spin_lock, bpf_timer, and kptrs. There could be one of each. */
__copy_map_value(struct bpf_map * map,void * dst,void * src,bool long_memcpy)301 static inline void __copy_map_value(struct bpf_map *map, void *dst, void *src, bool long_memcpy)
302 {
303 	u32 curr_off = 0;
304 	int i;
305 
306 	if (likely(!map->off_arr)) {
307 		if (long_memcpy)
308 			bpf_long_memcpy(dst, src, round_up(map->value_size, 8));
309 		else
310 			memcpy(dst, src, map->value_size);
311 		return;
312 	}
313 
314 	for (i = 0; i < map->off_arr->cnt; i++) {
315 		u32 next_off = map->off_arr->field_off[i];
316 
317 		memcpy(dst + curr_off, src + curr_off, next_off - curr_off);
318 		curr_off = next_off + map->off_arr->field_sz[i];
319 	}
320 	memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off);
321 }
322 
copy_map_value(struct bpf_map * map,void * dst,void * src)323 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
324 {
325 	__copy_map_value(map, dst, src, false);
326 }
327 
copy_map_value_long(struct bpf_map * map,void * dst,void * src)328 static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src)
329 {
330 	__copy_map_value(map, dst, src, true);
331 }
332 
zero_map_value(struct bpf_map * map,void * dst)333 static inline void zero_map_value(struct bpf_map *map, void *dst)
334 {
335 	u32 curr_off = 0;
336 	int i;
337 
338 	if (likely(!map->off_arr)) {
339 		memset(dst, 0, map->value_size);
340 		return;
341 	}
342 
343 	for (i = 0; i < map->off_arr->cnt; i++) {
344 		u32 next_off = map->off_arr->field_off[i];
345 
346 		memset(dst + curr_off, 0, next_off - curr_off);
347 		curr_off = next_off + map->off_arr->field_sz[i];
348 	}
349 	memset(dst + curr_off, 0, map->value_size - curr_off);
350 }
351 
352 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
353 			   bool lock_src);
354 void bpf_timer_cancel_and_free(void *timer);
355 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
356 
357 struct bpf_offload_dev;
358 struct bpf_offloaded_map;
359 
360 struct bpf_map_dev_ops {
361 	int (*map_get_next_key)(struct bpf_offloaded_map *map,
362 				void *key, void *next_key);
363 	int (*map_lookup_elem)(struct bpf_offloaded_map *map,
364 			       void *key, void *value);
365 	int (*map_update_elem)(struct bpf_offloaded_map *map,
366 			       void *key, void *value, u64 flags);
367 	int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
368 };
369 
370 struct bpf_offloaded_map {
371 	struct bpf_map map;
372 	struct net_device *netdev;
373 	const struct bpf_map_dev_ops *dev_ops;
374 	void *dev_priv;
375 	struct list_head offloads;
376 };
377 
map_to_offmap(struct bpf_map * map)378 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
379 {
380 	return container_of(map, struct bpf_offloaded_map, map);
381 }
382 
bpf_map_offload_neutral(const struct bpf_map * map)383 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
384 {
385 	return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
386 }
387 
bpf_map_support_seq_show(const struct bpf_map * map)388 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
389 {
390 	return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
391 		map->ops->map_seq_show_elem;
392 }
393 
394 int map_check_no_btf(const struct bpf_map *map,
395 		     const struct btf *btf,
396 		     const struct btf_type *key_type,
397 		     const struct btf_type *value_type);
398 
399 bool bpf_map_meta_equal(const struct bpf_map *meta0,
400 			const struct bpf_map *meta1);
401 
402 extern const struct bpf_map_ops bpf_map_offload_ops;
403 
404 /* bpf_type_flag contains a set of flags that are applicable to the values of
405  * arg_type, ret_type and reg_type. For example, a pointer value may be null,
406  * or a memory is read-only. We classify types into two categories: base types
407  * and extended types. Extended types are base types combined with a type flag.
408  *
409  * Currently there are no more than 32 base types in arg_type, ret_type and
410  * reg_types.
411  */
412 #define BPF_BASE_TYPE_BITS	8
413 
414 enum bpf_type_flag {
415 	/* PTR may be NULL. */
416 	PTR_MAYBE_NULL		= BIT(0 + BPF_BASE_TYPE_BITS),
417 
418 	/* MEM is read-only. When applied on bpf_arg, it indicates the arg is
419 	 * compatible with both mutable and immutable memory.
420 	 */
421 	MEM_RDONLY		= BIT(1 + BPF_BASE_TYPE_BITS),
422 
423 	/* MEM was "allocated" from a different helper, and cannot be mixed
424 	 * with regular non-MEM_ALLOC'ed MEM types.
425 	 */
426 	MEM_ALLOC		= BIT(2 + BPF_BASE_TYPE_BITS),
427 
428 	/* MEM is in user address space. */
429 	MEM_USER		= BIT(3 + BPF_BASE_TYPE_BITS),
430 
431 	/* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged
432 	 * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In
433 	 * order to drop this tag, it must be passed into bpf_per_cpu_ptr()
434 	 * or bpf_this_cpu_ptr(), which will return the pointer corresponding
435 	 * to the specified cpu.
436 	 */
437 	MEM_PERCPU		= BIT(4 + BPF_BASE_TYPE_BITS),
438 
439 	/* Indicates that the argument will be released. */
440 	OBJ_RELEASE		= BIT(5 + BPF_BASE_TYPE_BITS),
441 
442 	/* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
443 	 * unreferenced and referenced kptr loaded from map value using a load
444 	 * instruction, so that they can only be dereferenced but not escape the
445 	 * BPF program into the kernel (i.e. cannot be passed as arguments to
446 	 * kfunc or bpf helpers).
447 	 */
448 	PTR_UNTRUSTED		= BIT(6 + BPF_BASE_TYPE_BITS),
449 
450 	MEM_UNINIT		= BIT(7 + BPF_BASE_TYPE_BITS),
451 
452 	/* DYNPTR points to memory local to the bpf program. */
453 	DYNPTR_TYPE_LOCAL	= BIT(8 + BPF_BASE_TYPE_BITS),
454 
455 	/* DYNPTR points to a kernel-produced ringbuf record. */
456 	DYNPTR_TYPE_RINGBUF	= BIT(9 + BPF_BASE_TYPE_BITS),
457 
458 	/* Size is known at compile time. */
459 	MEM_FIXED_SIZE		= BIT(10 + BPF_BASE_TYPE_BITS),
460 
461 	__BPF_TYPE_FLAG_MAX,
462 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
463 };
464 
465 #define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF)
466 
467 /* Max number of base types. */
468 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
469 
470 /* Max number of all types. */
471 #define BPF_TYPE_LIMIT		(__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
472 
473 /* function argument constraints */
474 enum bpf_arg_type {
475 	ARG_DONTCARE = 0,	/* unused argument in helper function */
476 
477 	/* the following constraints used to prototype
478 	 * bpf_map_lookup/update/delete_elem() functions
479 	 */
480 	ARG_CONST_MAP_PTR,	/* const argument used as pointer to bpf_map */
481 	ARG_PTR_TO_MAP_KEY,	/* pointer to stack used as map key */
482 	ARG_PTR_TO_MAP_VALUE,	/* pointer to stack used as map value */
483 
484 	/* Used to prototype bpf_memcmp() and other functions that access data
485 	 * on eBPF program stack
486 	 */
487 	ARG_PTR_TO_MEM,		/* pointer to valid memory (stack, packet, map value) */
488 
489 	ARG_CONST_SIZE,		/* number of bytes accessed from memory */
490 	ARG_CONST_SIZE_OR_ZERO,	/* number of bytes accessed from memory or 0 */
491 
492 	ARG_PTR_TO_CTX,		/* pointer to context */
493 	ARG_ANYTHING,		/* any (initialized) argument is ok */
494 	ARG_PTR_TO_SPIN_LOCK,	/* pointer to bpf_spin_lock */
495 	ARG_PTR_TO_SOCK_COMMON,	/* pointer to sock_common */
496 	ARG_PTR_TO_INT,		/* pointer to int */
497 	ARG_PTR_TO_LONG,	/* pointer to long */
498 	ARG_PTR_TO_SOCKET,	/* pointer to bpf_sock (fullsock) */
499 	ARG_PTR_TO_BTF_ID,	/* pointer to in-kernel struct */
500 	ARG_PTR_TO_ALLOC_MEM,	/* pointer to dynamically allocated memory */
501 	ARG_CONST_ALLOC_SIZE_OR_ZERO,	/* number of allocated bytes requested */
502 	ARG_PTR_TO_BTF_ID_SOCK_COMMON,	/* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
503 	ARG_PTR_TO_PERCPU_BTF_ID,	/* pointer to in-kernel percpu type */
504 	ARG_PTR_TO_FUNC,	/* pointer to a bpf program function */
505 	ARG_PTR_TO_STACK,	/* pointer to stack */
506 	ARG_PTR_TO_CONST_STR,	/* pointer to a null terminated read-only string */
507 	ARG_PTR_TO_TIMER,	/* pointer to bpf_timer */
508 	ARG_PTR_TO_KPTR,	/* pointer to referenced kptr */
509 	ARG_PTR_TO_DYNPTR,      /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */
510 	__BPF_ARG_TYPE_MAX,
511 
512 	/* Extended arg_types. */
513 	ARG_PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
514 	ARG_PTR_TO_MEM_OR_NULL		= PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
515 	ARG_PTR_TO_CTX_OR_NULL		= PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
516 	ARG_PTR_TO_SOCKET_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
517 	ARG_PTR_TO_ALLOC_MEM_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_ALLOC_MEM,
518 	ARG_PTR_TO_STACK_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
519 	ARG_PTR_TO_BTF_ID_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID,
520 	/* pointer to memory does not need to be initialized, helper function must fill
521 	 * all bytes or clear them in error case.
522 	 */
523 	ARG_PTR_TO_UNINIT_MEM		= MEM_UNINIT | ARG_PTR_TO_MEM,
524 	/* Pointer to valid memory of size known at compile time. */
525 	ARG_PTR_TO_FIXED_SIZE_MEM	= MEM_FIXED_SIZE | ARG_PTR_TO_MEM,
526 
527 	/* This must be the last entry. Its purpose is to ensure the enum is
528 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
529 	 */
530 	__BPF_ARG_TYPE_LIMIT	= BPF_TYPE_LIMIT,
531 };
532 static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
533 
534 /* type of values returned from helper functions */
535 enum bpf_return_type {
536 	RET_INTEGER,			/* function returns integer */
537 	RET_VOID,			/* function doesn't return anything */
538 	RET_PTR_TO_MAP_VALUE,		/* returns a pointer to map elem value */
539 	RET_PTR_TO_SOCKET,		/* returns a pointer to a socket */
540 	RET_PTR_TO_TCP_SOCK,		/* returns a pointer to a tcp_sock */
541 	RET_PTR_TO_SOCK_COMMON,		/* returns a pointer to a sock_common */
542 	RET_PTR_TO_ALLOC_MEM,		/* returns a pointer to dynamically allocated memory */
543 	RET_PTR_TO_MEM_OR_BTF_ID,	/* returns a pointer to a valid memory or a btf_id */
544 	RET_PTR_TO_BTF_ID,		/* returns a pointer to a btf_id */
545 	__BPF_RET_TYPE_MAX,
546 
547 	/* Extended ret_types. */
548 	RET_PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
549 	RET_PTR_TO_SOCKET_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
550 	RET_PTR_TO_TCP_SOCK_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
551 	RET_PTR_TO_SOCK_COMMON_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
552 	RET_PTR_TO_ALLOC_MEM_OR_NULL	= PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
553 	RET_PTR_TO_DYNPTR_MEM_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
554 	RET_PTR_TO_BTF_ID_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
555 
556 	/* This must be the last entry. Its purpose is to ensure the enum is
557 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
558 	 */
559 	__BPF_RET_TYPE_LIMIT	= BPF_TYPE_LIMIT,
560 };
561 static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
562 
563 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
564  * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
565  * instructions after verifying
566  */
567 struct bpf_func_proto {
568 	u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
569 	bool gpl_only;
570 	bool pkt_access;
571 	enum bpf_return_type ret_type;
572 	union {
573 		struct {
574 			enum bpf_arg_type arg1_type;
575 			enum bpf_arg_type arg2_type;
576 			enum bpf_arg_type arg3_type;
577 			enum bpf_arg_type arg4_type;
578 			enum bpf_arg_type arg5_type;
579 		};
580 		enum bpf_arg_type arg_type[5];
581 	};
582 	union {
583 		struct {
584 			u32 *arg1_btf_id;
585 			u32 *arg2_btf_id;
586 			u32 *arg3_btf_id;
587 			u32 *arg4_btf_id;
588 			u32 *arg5_btf_id;
589 		};
590 		u32 *arg_btf_id[5];
591 		struct {
592 			size_t arg1_size;
593 			size_t arg2_size;
594 			size_t arg3_size;
595 			size_t arg4_size;
596 			size_t arg5_size;
597 		};
598 		size_t arg_size[5];
599 	};
600 	int *ret_btf_id; /* return value btf_id */
601 	bool (*allowed)(const struct bpf_prog *prog);
602 };
603 
604 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
605  * the first argument to eBPF programs.
606  * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
607  */
608 struct bpf_context;
609 
610 enum bpf_access_type {
611 	BPF_READ = 1,
612 	BPF_WRITE = 2
613 };
614 
615 /* types of values stored in eBPF registers */
616 /* Pointer types represent:
617  * pointer
618  * pointer + imm
619  * pointer + (u16) var
620  * pointer + (u16) var + imm
621  * if (range > 0) then [ptr, ptr + range - off) is safe to access
622  * if (id > 0) means that some 'var' was added
623  * if (off > 0) means that 'imm' was added
624  */
625 enum bpf_reg_type {
626 	NOT_INIT = 0,		 /* nothing was written into register */
627 	SCALAR_VALUE,		 /* reg doesn't contain a valid pointer */
628 	PTR_TO_CTX,		 /* reg points to bpf_context */
629 	CONST_PTR_TO_MAP,	 /* reg points to struct bpf_map */
630 	PTR_TO_MAP_VALUE,	 /* reg points to map element value */
631 	PTR_TO_MAP_KEY,		 /* reg points to a map element key */
632 	PTR_TO_STACK,		 /* reg == frame_pointer + offset */
633 	PTR_TO_PACKET_META,	 /* skb->data - meta_len */
634 	PTR_TO_PACKET,		 /* reg points to skb->data */
635 	PTR_TO_PACKET_END,	 /* skb->data + headlen */
636 	PTR_TO_FLOW_KEYS,	 /* reg points to bpf_flow_keys */
637 	PTR_TO_SOCKET,		 /* reg points to struct bpf_sock */
638 	PTR_TO_SOCK_COMMON,	 /* reg points to sock_common */
639 	PTR_TO_TCP_SOCK,	 /* reg points to struct tcp_sock */
640 	PTR_TO_TP_BUFFER,	 /* reg points to a writable raw tp's buffer */
641 	PTR_TO_XDP_SOCK,	 /* reg points to struct xdp_sock */
642 	/* PTR_TO_BTF_ID points to a kernel struct that does not need
643 	 * to be null checked by the BPF program. This does not imply the
644 	 * pointer is _not_ null and in practice this can easily be a null
645 	 * pointer when reading pointer chains. The assumption is program
646 	 * context will handle null pointer dereference typically via fault
647 	 * handling. The verifier must keep this in mind and can make no
648 	 * assumptions about null or non-null when doing branch analysis.
649 	 * Further, when passed into helpers the helpers can not, without
650 	 * additional context, assume the value is non-null.
651 	 */
652 	PTR_TO_BTF_ID,
653 	/* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
654 	 * been checked for null. Used primarily to inform the verifier
655 	 * an explicit null check is required for this struct.
656 	 */
657 	PTR_TO_MEM,		 /* reg points to valid memory region */
658 	PTR_TO_BUF,		 /* reg points to a read/write buffer */
659 	PTR_TO_FUNC,		 /* reg points to a bpf program function */
660 	PTR_TO_DYNPTR,		 /* reg points to a dynptr */
661 	__BPF_REG_TYPE_MAX,
662 
663 	/* Extended reg_types. */
664 	PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
665 	PTR_TO_SOCKET_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_SOCKET,
666 	PTR_TO_SOCK_COMMON_OR_NULL	= PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
667 	PTR_TO_TCP_SOCK_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
668 	PTR_TO_BTF_ID_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_BTF_ID,
669 
670 	/* This must be the last entry. Its purpose is to ensure the enum is
671 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
672 	 */
673 	__BPF_REG_TYPE_LIMIT	= BPF_TYPE_LIMIT,
674 };
675 static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
676 
677 /* The information passed from prog-specific *_is_valid_access
678  * back to the verifier.
679  */
680 struct bpf_insn_access_aux {
681 	enum bpf_reg_type reg_type;
682 	union {
683 		int ctx_field_size;
684 		struct {
685 			struct btf *btf;
686 			u32 btf_id;
687 		};
688 	};
689 	struct bpf_verifier_log *log; /* for verbose logs */
690 };
691 
692 static inline void
bpf_ctx_record_field_size(struct bpf_insn_access_aux * aux,u32 size)693 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
694 {
695 	aux->ctx_field_size = size;
696 }
697 
bpf_pseudo_func(const struct bpf_insn * insn)698 static inline bool bpf_pseudo_func(const struct bpf_insn *insn)
699 {
700 	return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&
701 	       insn->src_reg == BPF_PSEUDO_FUNC;
702 }
703 
704 struct bpf_prog_ops {
705 	int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
706 			union bpf_attr __user *uattr);
707 };
708 
709 struct bpf_verifier_ops {
710 	/* return eBPF function prototype for verification */
711 	const struct bpf_func_proto *
712 	(*get_func_proto)(enum bpf_func_id func_id,
713 			  const struct bpf_prog *prog);
714 
715 	/* return true if 'size' wide access at offset 'off' within bpf_context
716 	 * with 'type' (read or write) is allowed
717 	 */
718 	bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
719 				const struct bpf_prog *prog,
720 				struct bpf_insn_access_aux *info);
721 	int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
722 			    const struct bpf_prog *prog);
723 	int (*gen_ld_abs)(const struct bpf_insn *orig,
724 			  struct bpf_insn *insn_buf);
725 	u32 (*convert_ctx_access)(enum bpf_access_type type,
726 				  const struct bpf_insn *src,
727 				  struct bpf_insn *dst,
728 				  struct bpf_prog *prog, u32 *target_size);
729 	int (*btf_struct_access)(struct bpf_verifier_log *log,
730 				 const struct btf *btf,
731 				 const struct btf_type *t, int off, int size,
732 				 enum bpf_access_type atype,
733 				 u32 *next_btf_id, enum bpf_type_flag *flag);
734 };
735 
736 struct bpf_prog_offload_ops {
737 	/* verifier basic callbacks */
738 	int (*insn_hook)(struct bpf_verifier_env *env,
739 			 int insn_idx, int prev_insn_idx);
740 	int (*finalize)(struct bpf_verifier_env *env);
741 	/* verifier optimization callbacks (called after .finalize) */
742 	int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
743 			    struct bpf_insn *insn);
744 	int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
745 	/* program management callbacks */
746 	int (*prepare)(struct bpf_prog *prog);
747 	int (*translate)(struct bpf_prog *prog);
748 	void (*destroy)(struct bpf_prog *prog);
749 };
750 
751 struct bpf_prog_offload {
752 	struct bpf_prog		*prog;
753 	struct net_device	*netdev;
754 	struct bpf_offload_dev	*offdev;
755 	void			*dev_priv;
756 	struct list_head	offloads;
757 	bool			dev_state;
758 	bool			opt_failed;
759 	void			*jited_image;
760 	u32			jited_len;
761 };
762 
763 enum bpf_cgroup_storage_type {
764 	BPF_CGROUP_STORAGE_SHARED,
765 	BPF_CGROUP_STORAGE_PERCPU,
766 	__BPF_CGROUP_STORAGE_MAX
767 };
768 
769 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
770 
771 /* The longest tracepoint has 12 args.
772  * See include/trace/bpf_probe.h
773  */
774 #define MAX_BPF_FUNC_ARGS 12
775 
776 /* The maximum number of arguments passed through registers
777  * a single function may have.
778  */
779 #define MAX_BPF_FUNC_REG_ARGS 5
780 
781 /* The argument is a structure. */
782 #define BTF_FMODEL_STRUCT_ARG		BIT(0)
783 
784 struct btf_func_model {
785 	u8 ret_size;
786 	u8 nr_args;
787 	u8 arg_size[MAX_BPF_FUNC_ARGS];
788 	u8 arg_flags[MAX_BPF_FUNC_ARGS];
789 };
790 
791 /* Restore arguments before returning from trampoline to let original function
792  * continue executing. This flag is used for fentry progs when there are no
793  * fexit progs.
794  */
795 #define BPF_TRAMP_F_RESTORE_REGS	BIT(0)
796 /* Call original function after fentry progs, but before fexit progs.
797  * Makes sense for fentry/fexit, normal calls and indirect calls.
798  */
799 #define BPF_TRAMP_F_CALL_ORIG		BIT(1)
800 /* Skip current frame and return to parent.  Makes sense for fentry/fexit
801  * programs only. Should not be used with normal calls and indirect calls.
802  */
803 #define BPF_TRAMP_F_SKIP_FRAME		BIT(2)
804 /* Store IP address of the caller on the trampoline stack,
805  * so it's available for trampoline's programs.
806  */
807 #define BPF_TRAMP_F_IP_ARG		BIT(3)
808 /* Return the return value of fentry prog. Only used by bpf_struct_ops. */
809 #define BPF_TRAMP_F_RET_FENTRY_RET	BIT(4)
810 
811 /* Get original function from stack instead of from provided direct address.
812  * Makes sense for trampolines with fexit or fmod_ret programs.
813  */
814 #define BPF_TRAMP_F_ORIG_STACK		BIT(5)
815 
816 /* This trampoline is on a function with another ftrace_ops with IPMODIFY,
817  * e.g., a live patch. This flag is set and cleared by ftrace call backs,
818  */
819 #define BPF_TRAMP_F_SHARE_IPMODIFY	BIT(6)
820 
821 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
822  * bytes on x86.
823  */
824 #define BPF_MAX_TRAMP_LINKS 38
825 
826 struct bpf_tramp_links {
827 	struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS];
828 	int nr_links;
829 };
830 
831 struct bpf_tramp_run_ctx;
832 
833 /* Different use cases for BPF trampoline:
834  * 1. replace nop at the function entry (kprobe equivalent)
835  *    flags = BPF_TRAMP_F_RESTORE_REGS
836  *    fentry = a set of programs to run before returning from trampoline
837  *
838  * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
839  *    flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
840  *    orig_call = fentry_ip + MCOUNT_INSN_SIZE
841  *    fentry = a set of program to run before calling original function
842  *    fexit = a set of program to run after original function
843  *
844  * 3. replace direct call instruction anywhere in the function body
845  *    or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
846  *    With flags = 0
847  *      fentry = a set of programs to run before returning from trampoline
848  *    With flags = BPF_TRAMP_F_CALL_ORIG
849  *      orig_call = original callback addr or direct function addr
850  *      fentry = a set of program to run before calling original function
851  *      fexit = a set of program to run after original function
852  */
853 struct bpf_tramp_image;
854 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
855 				const struct btf_func_model *m, u32 flags,
856 				struct bpf_tramp_links *tlinks,
857 				void *orig_call);
858 /* these two functions are called from generated trampoline */
859 u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
860 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx);
861 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
862 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
863 				       struct bpf_tramp_run_ctx *run_ctx);
864 u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
865 					struct bpf_tramp_run_ctx *run_ctx);
866 void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
867 					struct bpf_tramp_run_ctx *run_ctx);
868 u64 notrace __bpf_prog_enter_struct_ops(struct bpf_prog *prog,
869 					struct bpf_tramp_run_ctx *run_ctx);
870 void notrace __bpf_prog_exit_struct_ops(struct bpf_prog *prog, u64 start,
871 					struct bpf_tramp_run_ctx *run_ctx);
872 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
873 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
874 
875 struct bpf_ksym {
876 	unsigned long		 start;
877 	unsigned long		 end;
878 	char			 name[KSYM_NAME_LEN];
879 	struct list_head	 lnode;
880 	struct latch_tree_node	 tnode;
881 	bool			 prog;
882 };
883 
884 enum bpf_tramp_prog_type {
885 	BPF_TRAMP_FENTRY,
886 	BPF_TRAMP_FEXIT,
887 	BPF_TRAMP_MODIFY_RETURN,
888 	BPF_TRAMP_MAX,
889 	BPF_TRAMP_REPLACE, /* more than MAX */
890 };
891 
892 struct bpf_tramp_image {
893 	void *image;
894 	struct bpf_ksym ksym;
895 	struct percpu_ref pcref;
896 	void *ip_after_call;
897 	void *ip_epilogue;
898 	union {
899 		struct rcu_head rcu;
900 		struct work_struct work;
901 	};
902 };
903 
904 struct bpf_trampoline {
905 	/* hlist for trampoline_table */
906 	struct hlist_node hlist;
907 	struct ftrace_ops *fops;
908 	/* serializes access to fields of this trampoline */
909 	struct mutex mutex;
910 	refcount_t refcnt;
911 	u32 flags;
912 	u64 key;
913 	struct {
914 		struct btf_func_model model;
915 		void *addr;
916 		bool ftrace_managed;
917 	} func;
918 	/* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
919 	 * program by replacing one of its functions. func.addr is the address
920 	 * of the function it replaced.
921 	 */
922 	struct bpf_prog *extension_prog;
923 	/* list of BPF programs using this trampoline */
924 	struct hlist_head progs_hlist[BPF_TRAMP_MAX];
925 	/* Number of attached programs. A counter per kind. */
926 	int progs_cnt[BPF_TRAMP_MAX];
927 	/* Executable image of trampoline */
928 	struct bpf_tramp_image *cur_image;
929 	u64 selector;
930 	struct module *mod;
931 };
932 
933 struct bpf_attach_target_info {
934 	struct btf_func_model fmodel;
935 	long tgt_addr;
936 	const char *tgt_name;
937 	const struct btf_type *tgt_type;
938 };
939 
940 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
941 
942 struct bpf_dispatcher_prog {
943 	struct bpf_prog *prog;
944 	refcount_t users;
945 };
946 
947 struct bpf_dispatcher {
948 	/* dispatcher mutex */
949 	struct mutex mutex;
950 	void *func;
951 	struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
952 	int num_progs;
953 	void *image;
954 	void *rw_image;
955 	u32 image_off;
956 	struct bpf_ksym ksym;
957 #ifdef CONFIG_HAVE_STATIC_CALL
958 	struct static_call_key *sc_key;
959 	void *sc_tramp;
960 #endif
961 };
962 
bpf_dispatcher_nop_func(const void * ctx,const struct bpf_insn * insnsi,bpf_func_t bpf_func)963 static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
964 	const void *ctx,
965 	const struct bpf_insn *insnsi,
966 	bpf_func_t bpf_func)
967 {
968 	return bpf_func(ctx, insnsi);
969 }
970 
971 #ifdef CONFIG_BPF_JIT
972 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
973 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
974 struct bpf_trampoline *bpf_trampoline_get(u64 key,
975 					  struct bpf_attach_target_info *tgt_info);
976 void bpf_trampoline_put(struct bpf_trampoline *tr);
977 int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
978 
979 /*
980  * When the architecture supports STATIC_CALL replace the bpf_dispatcher_fn
981  * indirection with a direct call to the bpf program. If the architecture does
982  * not have STATIC_CALL, avoid a double-indirection.
983  */
984 #ifdef CONFIG_HAVE_STATIC_CALL
985 
986 #define __BPF_DISPATCHER_SC_INIT(_name)				\
987 	.sc_key = &STATIC_CALL_KEY(_name),			\
988 	.sc_tramp = STATIC_CALL_TRAMP_ADDR(_name),
989 
990 #define __BPF_DISPATCHER_SC(name)				\
991 	DEFINE_STATIC_CALL(bpf_dispatcher_##name##_call, bpf_dispatcher_nop_func)
992 
993 #define __BPF_DISPATCHER_CALL(name)				\
994 	static_call(bpf_dispatcher_##name##_call)(ctx, insnsi, bpf_func)
995 
996 #define __BPF_DISPATCHER_UPDATE(_d, _new)			\
997 	__static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
998 
999 #else
1000 #define __BPF_DISPATCHER_SC_INIT(name)
1001 #define __BPF_DISPATCHER_SC(name)
1002 #define __BPF_DISPATCHER_CALL(name)		bpf_func(ctx, insnsi)
1003 #define __BPF_DISPATCHER_UPDATE(_d, _new)
1004 #endif
1005 
1006 #define BPF_DISPATCHER_INIT(_name) {				\
1007 	.mutex = __MUTEX_INITIALIZER(_name.mutex),		\
1008 	.func = &_name##_func,					\
1009 	.progs = {},						\
1010 	.num_progs = 0,						\
1011 	.image = NULL,						\
1012 	.image_off = 0,						\
1013 	.ksym = {						\
1014 		.name  = #_name,				\
1015 		.lnode = LIST_HEAD_INIT(_name.ksym.lnode),	\
1016 	},							\
1017 	__BPF_DISPATCHER_SC_INIT(_name##_call)			\
1018 }
1019 
1020 #define DEFINE_BPF_DISPATCHER(name)					\
1021 	__BPF_DISPATCHER_SC(name);					\
1022 	noinline __nocfi unsigned int bpf_dispatcher_##name##_func(	\
1023 		const void *ctx,					\
1024 		const struct bpf_insn *insnsi,				\
1025 		bpf_func_t bpf_func)					\
1026 	{								\
1027 		return __BPF_DISPATCHER_CALL(name);			\
1028 	}								\
1029 	EXPORT_SYMBOL(bpf_dispatcher_##name##_func);			\
1030 	struct bpf_dispatcher bpf_dispatcher_##name =			\
1031 		BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
1032 
1033 #define DECLARE_BPF_DISPATCHER(name)					\
1034 	unsigned int bpf_dispatcher_##name##_func(			\
1035 		const void *ctx,					\
1036 		const struct bpf_insn *insnsi,				\
1037 		bpf_func_t bpf_func);					\
1038 	extern struct bpf_dispatcher bpf_dispatcher_##name;
1039 
1040 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
1041 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
1042 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
1043 				struct bpf_prog *to);
1044 /* Called only from JIT-enabled code, so there's no need for stubs. */
1045 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
1046 void bpf_image_ksym_del(struct bpf_ksym *ksym);
1047 void bpf_ksym_add(struct bpf_ksym *ksym);
1048 void bpf_ksym_del(struct bpf_ksym *ksym);
1049 int bpf_jit_charge_modmem(u32 size);
1050 void bpf_jit_uncharge_modmem(u32 size);
1051 bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
1052 #else
bpf_trampoline_link_prog(struct bpf_tramp_link * link,struct bpf_trampoline * tr)1053 static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
1054 					   struct bpf_trampoline *tr)
1055 {
1056 	return -ENOTSUPP;
1057 }
bpf_trampoline_unlink_prog(struct bpf_tramp_link * link,struct bpf_trampoline * tr)1058 static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
1059 					     struct bpf_trampoline *tr)
1060 {
1061 	return -ENOTSUPP;
1062 }
bpf_trampoline_get(u64 key,struct bpf_attach_target_info * tgt_info)1063 static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
1064 							struct bpf_attach_target_info *tgt_info)
1065 {
1066 	return ERR_PTR(-EOPNOTSUPP);
1067 }
bpf_trampoline_put(struct bpf_trampoline * tr)1068 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
1069 #define DEFINE_BPF_DISPATCHER(name)
1070 #define DECLARE_BPF_DISPATCHER(name)
1071 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
1072 #define BPF_DISPATCHER_PTR(name) NULL
bpf_dispatcher_change_prog(struct bpf_dispatcher * d,struct bpf_prog * from,struct bpf_prog * to)1073 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
1074 					      struct bpf_prog *from,
1075 					      struct bpf_prog *to) {}
is_bpf_image_address(unsigned long address)1076 static inline bool is_bpf_image_address(unsigned long address)
1077 {
1078 	return false;
1079 }
bpf_prog_has_trampoline(const struct bpf_prog * prog)1080 static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
1081 {
1082 	return false;
1083 }
1084 #endif
1085 
1086 struct bpf_func_info_aux {
1087 	u16 linkage;
1088 	bool unreliable;
1089 };
1090 
1091 enum bpf_jit_poke_reason {
1092 	BPF_POKE_REASON_TAIL_CALL,
1093 };
1094 
1095 /* Descriptor of pokes pointing /into/ the JITed image. */
1096 struct bpf_jit_poke_descriptor {
1097 	void *tailcall_target;
1098 	void *tailcall_bypass;
1099 	void *bypass_addr;
1100 	void *aux;
1101 	union {
1102 		struct {
1103 			struct bpf_map *map;
1104 			u32 key;
1105 		} tail_call;
1106 	};
1107 	bool tailcall_target_stable;
1108 	u8 adj_off;
1109 	u16 reason;
1110 	u32 insn_idx;
1111 };
1112 
1113 /* reg_type info for ctx arguments */
1114 struct bpf_ctx_arg_aux {
1115 	u32 offset;
1116 	enum bpf_reg_type reg_type;
1117 	u32 btf_id;
1118 };
1119 
1120 struct btf_mod_pair {
1121 	struct btf *btf;
1122 	struct module *module;
1123 };
1124 
1125 struct bpf_kfunc_desc_tab;
1126 
1127 struct bpf_prog_aux {
1128 	atomic64_t refcnt;
1129 	u32 used_map_cnt;
1130 	u32 used_btf_cnt;
1131 	u32 max_ctx_offset;
1132 	u32 max_pkt_offset;
1133 	u32 max_tp_access;
1134 	u32 stack_depth;
1135 	u32 id;
1136 	u32 func_cnt; /* used by non-func prog as the number of func progs */
1137 	u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
1138 	u32 attach_btf_id; /* in-kernel BTF type id to attach to */
1139 	u32 ctx_arg_info_size;
1140 	u32 max_rdonly_access;
1141 	u32 max_rdwr_access;
1142 	struct btf *attach_btf;
1143 	const struct bpf_ctx_arg_aux *ctx_arg_info;
1144 	struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1145 	struct bpf_prog *dst_prog;
1146 	struct bpf_trampoline *dst_trampoline;
1147 	enum bpf_prog_type saved_dst_prog_type;
1148 	enum bpf_attach_type saved_dst_attach_type;
1149 	bool verifier_zext; /* Zero extensions has been inserted by verifier. */
1150 	bool offload_requested;
1151 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
1152 	bool func_proto_unreliable;
1153 	bool sleepable;
1154 	bool tail_call_reachable;
1155 	bool xdp_has_frags;
1156 	/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1157 	const struct btf_type *attach_func_proto;
1158 	/* function name for valid attach_btf_id */
1159 	const char *attach_func_name;
1160 	struct bpf_prog **func;
1161 	void *jit_data; /* JIT specific data. arch dependent */
1162 	struct bpf_jit_poke_descriptor *poke_tab;
1163 	struct bpf_kfunc_desc_tab *kfunc_tab;
1164 	struct bpf_kfunc_btf_tab *kfunc_btf_tab;
1165 	u32 size_poke_tab;
1166 	struct bpf_ksym ksym;
1167 	const struct bpf_prog_ops *ops;
1168 	struct bpf_map **used_maps;
1169 	struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
1170 	struct btf_mod_pair *used_btfs;
1171 	struct bpf_prog *prog;
1172 	struct user_struct *user;
1173 	u64 load_time; /* ns since boottime */
1174 	u32 verified_insns;
1175 	int cgroup_atype; /* enum cgroup_bpf_attach_type */
1176 	struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1177 	char name[BPF_OBJ_NAME_LEN];
1178 #ifdef CONFIG_SECURITY
1179 	void *security;
1180 #endif
1181 	struct bpf_prog_offload *offload;
1182 	struct btf *btf;
1183 	struct bpf_func_info *func_info;
1184 	struct bpf_func_info_aux *func_info_aux;
1185 	/* bpf_line_info loaded from userspace.  linfo->insn_off
1186 	 * has the xlated insn offset.
1187 	 * Both the main and sub prog share the same linfo.
1188 	 * The subprog can access its first linfo by
1189 	 * using the linfo_idx.
1190 	 */
1191 	struct bpf_line_info *linfo;
1192 	/* jited_linfo is the jited addr of the linfo.  It has a
1193 	 * one to one mapping to linfo:
1194 	 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1195 	 * Both the main and sub prog share the same jited_linfo.
1196 	 * The subprog can access its first jited_linfo by
1197 	 * using the linfo_idx.
1198 	 */
1199 	void **jited_linfo;
1200 	u32 func_info_cnt;
1201 	u32 nr_linfo;
1202 	/* subprog can use linfo_idx to access its first linfo and
1203 	 * jited_linfo.
1204 	 * main prog always has linfo_idx == 0
1205 	 */
1206 	u32 linfo_idx;
1207 	u32 num_exentries;
1208 	struct exception_table_entry *extable;
1209 	union {
1210 		struct work_struct work;
1211 		struct rcu_head	rcu;
1212 	};
1213 };
1214 
1215 struct bpf_prog {
1216 	u16			pages;		/* Number of allocated pages */
1217 	u16			jited:1,	/* Is our filter JIT'ed? */
1218 				jit_requested:1,/* archs need to JIT the prog */
1219 				gpl_compatible:1, /* Is filter GPL compatible? */
1220 				cb_access:1,	/* Is control block accessed? */
1221 				dst_needed:1,	/* Do we need dst entry? */
1222 				blinding_requested:1, /* needs constant blinding */
1223 				blinded:1,	/* Was blinded */
1224 				is_func:1,	/* program is a bpf function */
1225 				kprobe_override:1, /* Do we override a kprobe? */
1226 				has_callchain_buf:1, /* callchain buffer allocated? */
1227 				enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1228 				call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1229 				call_get_func_ip:1, /* Do we call get_func_ip() */
1230 				tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
1231 	enum bpf_prog_type	type;		/* Type of BPF program */
1232 	enum bpf_attach_type	expected_attach_type; /* For some prog types */
1233 	u32			len;		/* Number of filter blocks */
1234 	u32			jited_len;	/* Size of jited insns in bytes */
1235 	u8			tag[BPF_TAG_SIZE];
1236 	struct bpf_prog_stats __percpu *stats;
1237 	int __percpu		*active;
1238 	unsigned int		(*bpf_func)(const void *ctx,
1239 					    const struct bpf_insn *insn);
1240 	struct bpf_prog_aux	*aux;		/* Auxiliary fields */
1241 	struct sock_fprog_kern	*orig_prog;	/* Original BPF program */
1242 	/* Instructions for interpreter */
1243 	union {
1244 		DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1245 		DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1246 	};
1247 };
1248 
1249 struct bpf_array_aux {
1250 	/* Programs with direct jumps into programs part of this array. */
1251 	struct list_head poke_progs;
1252 	struct bpf_map *map;
1253 	struct mutex poke_mutex;
1254 	struct work_struct work;
1255 };
1256 
1257 struct bpf_link {
1258 	atomic64_t refcnt;
1259 	u32 id;
1260 	enum bpf_link_type type;
1261 	const struct bpf_link_ops *ops;
1262 	struct bpf_prog *prog;
1263 	struct work_struct work;
1264 };
1265 
1266 struct bpf_link_ops {
1267 	void (*release)(struct bpf_link *link);
1268 	void (*dealloc)(struct bpf_link *link);
1269 	int (*detach)(struct bpf_link *link);
1270 	int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1271 			   struct bpf_prog *old_prog);
1272 	void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1273 	int (*fill_link_info)(const struct bpf_link *link,
1274 			      struct bpf_link_info *info);
1275 };
1276 
1277 struct bpf_tramp_link {
1278 	struct bpf_link link;
1279 	struct hlist_node tramp_hlist;
1280 	u64 cookie;
1281 };
1282 
1283 struct bpf_shim_tramp_link {
1284 	struct bpf_tramp_link link;
1285 	struct bpf_trampoline *trampoline;
1286 };
1287 
1288 struct bpf_tracing_link {
1289 	struct bpf_tramp_link link;
1290 	enum bpf_attach_type attach_type;
1291 	struct bpf_trampoline *trampoline;
1292 	struct bpf_prog *tgt_prog;
1293 };
1294 
1295 struct bpf_link_primer {
1296 	struct bpf_link *link;
1297 	struct file *file;
1298 	int fd;
1299 	u32 id;
1300 };
1301 
1302 struct bpf_struct_ops_value;
1303 struct btf_member;
1304 
1305 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
1306 struct bpf_struct_ops {
1307 	const struct bpf_verifier_ops *verifier_ops;
1308 	int (*init)(struct btf *btf);
1309 	int (*check_member)(const struct btf_type *t,
1310 			    const struct btf_member *member);
1311 	int (*init_member)(const struct btf_type *t,
1312 			   const struct btf_member *member,
1313 			   void *kdata, const void *udata);
1314 	int (*reg)(void *kdata);
1315 	void (*unreg)(void *kdata);
1316 	const struct btf_type *type;
1317 	const struct btf_type *value_type;
1318 	const char *name;
1319 	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
1320 	u32 type_id;
1321 	u32 value_id;
1322 };
1323 
1324 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
1325 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
1326 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
1327 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
1328 bool bpf_struct_ops_get(const void *kdata);
1329 void bpf_struct_ops_put(const void *kdata);
1330 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
1331 				       void *value);
1332 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
1333 				      struct bpf_tramp_link *link,
1334 				      const struct btf_func_model *model,
1335 				      void *image, void *image_end);
bpf_try_module_get(const void * data,struct module * owner)1336 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1337 {
1338 	if (owner == BPF_MODULE_OWNER)
1339 		return bpf_struct_ops_get(data);
1340 	else
1341 		return try_module_get(owner);
1342 }
bpf_module_put(const void * data,struct module * owner)1343 static inline void bpf_module_put(const void *data, struct module *owner)
1344 {
1345 	if (owner == BPF_MODULE_OWNER)
1346 		bpf_struct_ops_put(data);
1347 	else
1348 		module_put(owner);
1349 }
1350 
1351 #ifdef CONFIG_NET
1352 /* Define it here to avoid the use of forward declaration */
1353 struct bpf_dummy_ops_state {
1354 	int val;
1355 };
1356 
1357 struct bpf_dummy_ops {
1358 	int (*test_1)(struct bpf_dummy_ops_state *cb);
1359 	int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1360 		      char a3, unsigned long a4);
1361 };
1362 
1363 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1364 			    union bpf_attr __user *uattr);
1365 #endif
1366 #else
bpf_struct_ops_find(u32 type_id)1367 static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
1368 {
1369 	return NULL;
1370 }
bpf_struct_ops_init(struct btf * btf,struct bpf_verifier_log * log)1371 static inline void bpf_struct_ops_init(struct btf *btf,
1372 				       struct bpf_verifier_log *log)
1373 {
1374 }
bpf_try_module_get(const void * data,struct module * owner)1375 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1376 {
1377 	return try_module_get(owner);
1378 }
bpf_module_put(const void * data,struct module * owner)1379 static inline void bpf_module_put(const void *data, struct module *owner)
1380 {
1381 	module_put(owner);
1382 }
bpf_struct_ops_map_sys_lookup_elem(struct bpf_map * map,void * key,void * value)1383 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1384 						     void *key,
1385 						     void *value)
1386 {
1387 	return -EINVAL;
1388 }
1389 #endif
1390 
1391 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
1392 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1393 				    int cgroup_atype);
1394 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
1395 #else
bpf_trampoline_link_cgroup_shim(struct bpf_prog * prog,int cgroup_atype)1396 static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1397 						  int cgroup_atype)
1398 {
1399 	return -EOPNOTSUPP;
1400 }
bpf_trampoline_unlink_cgroup_shim(struct bpf_prog * prog)1401 static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
1402 {
1403 }
1404 #endif
1405 
1406 struct bpf_array {
1407 	struct bpf_map map;
1408 	u32 elem_size;
1409 	u32 index_mask;
1410 	struct bpf_array_aux *aux;
1411 	union {
1412 		char value[0] __aligned(8);
1413 		void *ptrs[0] __aligned(8);
1414 		void __percpu *pptrs[0] __aligned(8);
1415 	};
1416 };
1417 
1418 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
1419 #define MAX_TAIL_CALL_CNT 33
1420 
1421 /* Maximum number of loops for bpf_loop */
1422 #define BPF_MAX_LOOPS	BIT(23)
1423 
1424 #define BPF_F_ACCESS_MASK	(BPF_F_RDONLY |		\
1425 				 BPF_F_RDONLY_PROG |	\
1426 				 BPF_F_WRONLY |		\
1427 				 BPF_F_WRONLY_PROG)
1428 
1429 #define BPF_MAP_CAN_READ	BIT(0)
1430 #define BPF_MAP_CAN_WRITE	BIT(1)
1431 
1432 /* Maximum number of user-producer ring buffer samples that can be drained in
1433  * a call to bpf_user_ringbuf_drain().
1434  */
1435 #define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024)
1436 
bpf_map_flags_to_cap(struct bpf_map * map)1437 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1438 {
1439 	u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1440 
1441 	/* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1442 	 * not possible.
1443 	 */
1444 	if (access_flags & BPF_F_RDONLY_PROG)
1445 		return BPF_MAP_CAN_READ;
1446 	else if (access_flags & BPF_F_WRONLY_PROG)
1447 		return BPF_MAP_CAN_WRITE;
1448 	else
1449 		return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1450 }
1451 
bpf_map_flags_access_ok(u32 access_flags)1452 static inline bool bpf_map_flags_access_ok(u32 access_flags)
1453 {
1454 	return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1455 	       (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1456 }
1457 
1458 struct bpf_event_entry {
1459 	struct perf_event *event;
1460 	struct file *perf_file;
1461 	struct file *map_file;
1462 	struct rcu_head rcu;
1463 };
1464 
map_type_contains_progs(struct bpf_map * map)1465 static inline bool map_type_contains_progs(struct bpf_map *map)
1466 {
1467 	return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
1468 	       map->map_type == BPF_MAP_TYPE_DEVMAP ||
1469 	       map->map_type == BPF_MAP_TYPE_CPUMAP;
1470 }
1471 
1472 bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
1473 int bpf_prog_calc_tag(struct bpf_prog *fp);
1474 
1475 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
1476 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
1477 
1478 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
1479 					unsigned long off, unsigned long len);
1480 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1481 					const struct bpf_insn *src,
1482 					struct bpf_insn *dst,
1483 					struct bpf_prog *prog,
1484 					u32 *target_size);
1485 
1486 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1487 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
1488 
1489 /* an array of programs to be executed under rcu_lock.
1490  *
1491  * Typical usage:
1492  * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
1493  *
1494  * the structure returned by bpf_prog_array_alloc() should be populated
1495  * with program pointers and the last pointer must be NULL.
1496  * The user has to keep refcnt on the program and make sure the program
1497  * is removed from the array before bpf_prog_put().
1498  * The 'struct bpf_prog_array *' should only be replaced with xchg()
1499  * since other cpus are walking the array of pointers in parallel.
1500  */
1501 struct bpf_prog_array_item {
1502 	struct bpf_prog *prog;
1503 	union {
1504 		struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1505 		u64 bpf_cookie;
1506 	};
1507 };
1508 
1509 struct bpf_prog_array {
1510 	struct rcu_head rcu;
1511 	struct bpf_prog_array_item items[];
1512 };
1513 
1514 struct bpf_empty_prog_array {
1515 	struct bpf_prog_array hdr;
1516 	struct bpf_prog *null_prog;
1517 };
1518 
1519 /* to avoid allocating empty bpf_prog_array for cgroups that
1520  * don't have bpf program attached use one global 'bpf_empty_prog_array'
1521  * It will not be modified the caller of bpf_prog_array_alloc()
1522  * (since caller requested prog_cnt == 0)
1523  * that pointer should be 'freed' by bpf_prog_array_free()
1524  */
1525 extern struct bpf_empty_prog_array bpf_empty_prog_array;
1526 
1527 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
1528 void bpf_prog_array_free(struct bpf_prog_array *progs);
1529 /* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
1530 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
1531 int bpf_prog_array_length(struct bpf_prog_array *progs);
1532 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
1533 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
1534 				__u32 __user *prog_ids, u32 cnt);
1535 
1536 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
1537 				struct bpf_prog *old_prog);
1538 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1539 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1540 			     struct bpf_prog *prog);
1541 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
1542 			     u32 *prog_ids, u32 request_cnt,
1543 			     u32 *prog_cnt);
1544 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
1545 			struct bpf_prog *exclude_prog,
1546 			struct bpf_prog *include_prog,
1547 			u64 bpf_cookie,
1548 			struct bpf_prog_array **new_array);
1549 
1550 struct bpf_run_ctx {};
1551 
1552 struct bpf_cg_run_ctx {
1553 	struct bpf_run_ctx run_ctx;
1554 	const struct bpf_prog_array_item *prog_item;
1555 	int retval;
1556 };
1557 
1558 struct bpf_trace_run_ctx {
1559 	struct bpf_run_ctx run_ctx;
1560 	u64 bpf_cookie;
1561 };
1562 
1563 struct bpf_tramp_run_ctx {
1564 	struct bpf_run_ctx run_ctx;
1565 	u64 bpf_cookie;
1566 	struct bpf_run_ctx *saved_run_ctx;
1567 };
1568 
bpf_set_run_ctx(struct bpf_run_ctx * new_ctx)1569 static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
1570 {
1571 	struct bpf_run_ctx *old_ctx = NULL;
1572 
1573 #ifdef CONFIG_BPF_SYSCALL
1574 	old_ctx = current->bpf_ctx;
1575 	current->bpf_ctx = new_ctx;
1576 #endif
1577 	return old_ctx;
1578 }
1579 
bpf_reset_run_ctx(struct bpf_run_ctx * old_ctx)1580 static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
1581 {
1582 #ifdef CONFIG_BPF_SYSCALL
1583 	current->bpf_ctx = old_ctx;
1584 #endif
1585 }
1586 
1587 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1588 #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE			(1 << 0)
1589 /* BPF program asks to set CN on the packet. */
1590 #define BPF_RET_SET_CN						(1 << 0)
1591 
1592 typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
1593 
1594 static __always_inline u32
bpf_prog_run_array(const struct bpf_prog_array * array,const void * ctx,bpf_prog_run_fn run_prog)1595 bpf_prog_run_array(const struct bpf_prog_array *array,
1596 		   const void *ctx, bpf_prog_run_fn run_prog)
1597 {
1598 	const struct bpf_prog_array_item *item;
1599 	const struct bpf_prog *prog;
1600 	struct bpf_run_ctx *old_run_ctx;
1601 	struct bpf_trace_run_ctx run_ctx;
1602 	u32 ret = 1;
1603 
1604 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
1605 
1606 	if (unlikely(!array))
1607 		return ret;
1608 
1609 	migrate_disable();
1610 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1611 	item = &array->items[0];
1612 	while ((prog = READ_ONCE(item->prog))) {
1613 		run_ctx.bpf_cookie = item->bpf_cookie;
1614 		ret &= run_prog(prog, ctx);
1615 		item++;
1616 	}
1617 	bpf_reset_run_ctx(old_run_ctx);
1618 	migrate_enable();
1619 	return ret;
1620 }
1621 
1622 /* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
1623  *
1624  * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
1625  * overall. As a result, we must use the bpf_prog_array_free_sleepable
1626  * in order to use the tasks_trace rcu grace period.
1627  *
1628  * When a non-sleepable program is inside the array, we take the rcu read
1629  * section and disable preemption for that program alone, so it can access
1630  * rcu-protected dynamically sized maps.
1631  */
1632 static __always_inline u32
bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu * array_rcu,const void * ctx,bpf_prog_run_fn run_prog)1633 bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu,
1634 			     const void *ctx, bpf_prog_run_fn run_prog)
1635 {
1636 	const struct bpf_prog_array_item *item;
1637 	const struct bpf_prog *prog;
1638 	const struct bpf_prog_array *array;
1639 	struct bpf_run_ctx *old_run_ctx;
1640 	struct bpf_trace_run_ctx run_ctx;
1641 	u32 ret = 1;
1642 
1643 	might_fault();
1644 
1645 	rcu_read_lock_trace();
1646 	migrate_disable();
1647 
1648 	array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held());
1649 	if (unlikely(!array))
1650 		goto out;
1651 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1652 	item = &array->items[0];
1653 	while ((prog = READ_ONCE(item->prog))) {
1654 		if (!prog->aux->sleepable)
1655 			rcu_read_lock();
1656 
1657 		run_ctx.bpf_cookie = item->bpf_cookie;
1658 		ret &= run_prog(prog, ctx);
1659 		item++;
1660 
1661 		if (!prog->aux->sleepable)
1662 			rcu_read_unlock();
1663 	}
1664 	bpf_reset_run_ctx(old_run_ctx);
1665 out:
1666 	migrate_enable();
1667 	rcu_read_unlock_trace();
1668 	return ret;
1669 }
1670 
1671 #ifdef CONFIG_BPF_SYSCALL
1672 DECLARE_PER_CPU(int, bpf_prog_active);
1673 extern struct mutex bpf_stats_enabled_mutex;
1674 
1675 /*
1676  * Block execution of BPF programs attached to instrumentation (perf,
1677  * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1678  * these events can happen inside a region which holds a map bucket lock
1679  * and can deadlock on it.
1680  */
bpf_disable_instrumentation(void)1681 static inline void bpf_disable_instrumentation(void)
1682 {
1683 	migrate_disable();
1684 	this_cpu_inc(bpf_prog_active);
1685 }
1686 
bpf_enable_instrumentation(void)1687 static inline void bpf_enable_instrumentation(void)
1688 {
1689 	this_cpu_dec(bpf_prog_active);
1690 	migrate_enable();
1691 }
1692 
1693 extern const struct file_operations bpf_map_fops;
1694 extern const struct file_operations bpf_prog_fops;
1695 extern const struct file_operations bpf_iter_fops;
1696 
1697 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1698 	extern const struct bpf_prog_ops _name ## _prog_ops; \
1699 	extern const struct bpf_verifier_ops _name ## _verifier_ops;
1700 #define BPF_MAP_TYPE(_id, _ops) \
1701 	extern const struct bpf_map_ops _ops;
1702 #define BPF_LINK_TYPE(_id, _name)
1703 #include <linux/bpf_types.h>
1704 #undef BPF_PROG_TYPE
1705 #undef BPF_MAP_TYPE
1706 #undef BPF_LINK_TYPE
1707 
1708 extern const struct bpf_prog_ops bpf_offload_prog_ops;
1709 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1710 extern const struct bpf_verifier_ops xdp_analyzer_ops;
1711 
1712 struct bpf_prog *bpf_prog_get(u32 ufd);
1713 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1714 				       bool attach_drv);
1715 void bpf_prog_add(struct bpf_prog *prog, int i);
1716 void bpf_prog_sub(struct bpf_prog *prog, int i);
1717 void bpf_prog_inc(struct bpf_prog *prog);
1718 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1719 void bpf_prog_put(struct bpf_prog *prog);
1720 
1721 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1722 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1723 
1724 struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset);
1725 void bpf_map_free_kptr_off_tab(struct bpf_map *map);
1726 struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map);
1727 bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
1728 void bpf_map_free_kptrs(struct bpf_map *map, void *map_value);
1729 
1730 struct bpf_map *bpf_map_get(u32 ufd);
1731 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1732 struct bpf_map *__bpf_map_get(struct fd f);
1733 void bpf_map_inc(struct bpf_map *map);
1734 void bpf_map_inc_with_uref(struct bpf_map *map);
1735 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1736 void bpf_map_put_with_uref(struct bpf_map *map);
1737 void bpf_map_put(struct bpf_map *map);
1738 void *bpf_map_area_alloc(u64 size, int numa_node);
1739 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1740 void bpf_map_area_free(void *base);
1741 bool bpf_map_write_active(const struct bpf_map *map);
1742 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1743 int  generic_map_lookup_batch(struct bpf_map *map,
1744 			      const union bpf_attr *attr,
1745 			      union bpf_attr __user *uattr);
1746 int  generic_map_update_batch(struct bpf_map *map,
1747 			      const union bpf_attr *attr,
1748 			      union bpf_attr __user *uattr);
1749 int  generic_map_delete_batch(struct bpf_map *map,
1750 			      const union bpf_attr *attr,
1751 			      union bpf_attr __user *uattr);
1752 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1753 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
1754 
1755 #ifdef CONFIG_MEMCG_KMEM
1756 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1757 			   int node);
1758 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1759 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1760 				    size_t align, gfp_t flags);
1761 #else
1762 static inline void *
bpf_map_kmalloc_node(const struct bpf_map * map,size_t size,gfp_t flags,int node)1763 bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1764 		     int node)
1765 {
1766 	return kmalloc_node(size, flags, node);
1767 }
1768 
1769 static inline void *
bpf_map_kzalloc(const struct bpf_map * map,size_t size,gfp_t flags)1770 bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1771 {
1772 	return kzalloc(size, flags);
1773 }
1774 
1775 static inline void __percpu *
bpf_map_alloc_percpu(const struct bpf_map * map,size_t size,size_t align,gfp_t flags)1776 bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1777 		     gfp_t flags)
1778 {
1779 	return __alloc_percpu_gfp(size, align, flags);
1780 }
1781 #endif
1782 
1783 extern int sysctl_unprivileged_bpf_disabled;
1784 
bpf_allow_ptr_leaks(void)1785 static inline bool bpf_allow_ptr_leaks(void)
1786 {
1787 	return perfmon_capable();
1788 }
1789 
bpf_allow_uninit_stack(void)1790 static inline bool bpf_allow_uninit_stack(void)
1791 {
1792 	return perfmon_capable();
1793 }
1794 
bpf_allow_ptr_to_map_access(void)1795 static inline bool bpf_allow_ptr_to_map_access(void)
1796 {
1797 	return perfmon_capable();
1798 }
1799 
bpf_bypass_spec_v1(void)1800 static inline bool bpf_bypass_spec_v1(void)
1801 {
1802 	return perfmon_capable();
1803 }
1804 
bpf_bypass_spec_v4(void)1805 static inline bool bpf_bypass_spec_v4(void)
1806 {
1807 	return perfmon_capable();
1808 }
1809 
1810 int bpf_map_new_fd(struct bpf_map *map, int flags);
1811 int bpf_prog_new_fd(struct bpf_prog *prog);
1812 
1813 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1814 		   const struct bpf_link_ops *ops, struct bpf_prog *prog);
1815 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1816 int bpf_link_settle(struct bpf_link_primer *primer);
1817 void bpf_link_cleanup(struct bpf_link_primer *primer);
1818 void bpf_link_inc(struct bpf_link *link);
1819 void bpf_link_put(struct bpf_link *link);
1820 int bpf_link_new_fd(struct bpf_link *link);
1821 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1822 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1823 struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
1824 
1825 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1826 int bpf_obj_get_user(const char __user *pathname, int flags);
1827 
1828 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1829 #define DEFINE_BPF_ITER_FUNC(target, args...)			\
1830 	extern int bpf_iter_ ## target(args);			\
1831 	int __init bpf_iter_ ## target(args) { return 0; }
1832 
1833 /*
1834  * The task type of iterators.
1835  *
1836  * For BPF task iterators, they can be parameterized with various
1837  * parameters to visit only some of tasks.
1838  *
1839  * BPF_TASK_ITER_ALL (default)
1840  *	Iterate over resources of every task.
1841  *
1842  * BPF_TASK_ITER_TID
1843  *	Iterate over resources of a task/tid.
1844  *
1845  * BPF_TASK_ITER_TGID
1846  *	Iterate over resources of every task of a process / task group.
1847  */
1848 enum bpf_iter_task_type {
1849 	BPF_TASK_ITER_ALL = 0,
1850 	BPF_TASK_ITER_TID,
1851 	BPF_TASK_ITER_TGID,
1852 };
1853 
1854 struct bpf_iter_aux_info {
1855 	/* for map_elem iter */
1856 	struct bpf_map *map;
1857 
1858 	/* for cgroup iter */
1859 	struct {
1860 		struct cgroup *start; /* starting cgroup */
1861 		enum bpf_cgroup_iter_order order;
1862 	} cgroup;
1863 	struct {
1864 		enum bpf_iter_task_type	type;
1865 		u32 pid;
1866 	} task;
1867 };
1868 
1869 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1870 					union bpf_iter_link_info *linfo,
1871 					struct bpf_iter_aux_info *aux);
1872 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
1873 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1874 					struct seq_file *seq);
1875 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1876 					 struct bpf_link_info *info);
1877 typedef const struct bpf_func_proto *
1878 (*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
1879 			     const struct bpf_prog *prog);
1880 
1881 enum bpf_iter_feature {
1882 	BPF_ITER_RESCHED	= BIT(0),
1883 };
1884 
1885 #define BPF_ITER_CTX_ARG_MAX 2
1886 struct bpf_iter_reg {
1887 	const char *target;
1888 	bpf_iter_attach_target_t attach_target;
1889 	bpf_iter_detach_target_t detach_target;
1890 	bpf_iter_show_fdinfo_t show_fdinfo;
1891 	bpf_iter_fill_link_info_t fill_link_info;
1892 	bpf_iter_get_func_proto_t get_func_proto;
1893 	u32 ctx_arg_info_size;
1894 	u32 feature;
1895 	struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1896 	const struct bpf_iter_seq_info *seq_info;
1897 };
1898 
1899 struct bpf_iter_meta {
1900 	__bpf_md_ptr(struct seq_file *, seq);
1901 	u64 session_id;
1902 	u64 seq_num;
1903 };
1904 
1905 struct bpf_iter__bpf_map_elem {
1906 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
1907 	__bpf_md_ptr(struct bpf_map *, map);
1908 	__bpf_md_ptr(void *, key);
1909 	__bpf_md_ptr(void *, value);
1910 };
1911 
1912 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1913 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1914 bool bpf_iter_prog_supported(struct bpf_prog *prog);
1915 const struct bpf_func_proto *
1916 bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
1917 int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
1918 int bpf_iter_new_fd(struct bpf_link *link);
1919 bool bpf_link_is_iter(struct bpf_link *link);
1920 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1921 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1922 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1923 			      struct seq_file *seq);
1924 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1925 				struct bpf_link_info *info);
1926 
1927 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1928 				   struct bpf_func_state *caller,
1929 				   struct bpf_func_state *callee);
1930 
1931 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1932 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1933 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1934 			   u64 flags);
1935 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1936 			    u64 flags);
1937 
1938 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1939 
1940 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1941 				 void *key, void *value, u64 map_flags);
1942 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1943 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1944 				void *key, void *value, u64 map_flags);
1945 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1946 
1947 int bpf_get_file_flag(int flags);
1948 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
1949 			     size_t actual_size);
1950 
1951 /* verify correctness of eBPF program */
1952 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
1953 
1954 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1955 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
1956 #endif
1957 
1958 struct btf *bpf_get_btf_vmlinux(void);
1959 
1960 /* Map specifics */
1961 struct xdp_frame;
1962 struct sk_buff;
1963 struct bpf_dtab_netdev;
1964 struct bpf_cpu_map_entry;
1965 
1966 void __dev_flush(void);
1967 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
1968 		    struct net_device *dev_rx);
1969 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
1970 		    struct net_device *dev_rx);
1971 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
1972 			  struct bpf_map *map, bool exclude_ingress);
1973 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1974 			     struct bpf_prog *xdp_prog);
1975 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
1976 			   struct bpf_prog *xdp_prog, struct bpf_map *map,
1977 			   bool exclude_ingress);
1978 
1979 void __cpu_map_flush(void);
1980 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
1981 		    struct net_device *dev_rx);
1982 int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
1983 			     struct sk_buff *skb);
1984 
1985 /* Return map's numa specified by userspace */
bpf_map_attr_numa_node(const union bpf_attr * attr)1986 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1987 {
1988 	return (attr->map_flags & BPF_F_NUMA_NODE) ?
1989 		attr->numa_node : NUMA_NO_NODE;
1990 }
1991 
1992 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
1993 int array_map_alloc_check(union bpf_attr *attr);
1994 
1995 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1996 			  union bpf_attr __user *uattr);
1997 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1998 			  union bpf_attr __user *uattr);
1999 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2000 			      const union bpf_attr *kattr,
2001 			      union bpf_attr __user *uattr);
2002 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2003 				     const union bpf_attr *kattr,
2004 				     union bpf_attr __user *uattr);
2005 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
2006 			     const union bpf_attr *kattr,
2007 			     union bpf_attr __user *uattr);
2008 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2009 				const union bpf_attr *kattr,
2010 				union bpf_attr __user *uattr);
2011 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
2012 		    const struct bpf_prog *prog,
2013 		    struct bpf_insn_access_aux *info);
2014 
bpf_tracing_ctx_access(int off,int size,enum bpf_access_type type)2015 static inline bool bpf_tracing_ctx_access(int off, int size,
2016 					  enum bpf_access_type type)
2017 {
2018 	if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
2019 		return false;
2020 	if (type != BPF_READ)
2021 		return false;
2022 	if (off % size != 0)
2023 		return false;
2024 	return true;
2025 }
2026 
bpf_tracing_btf_ctx_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2027 static inline bool bpf_tracing_btf_ctx_access(int off, int size,
2028 					      enum bpf_access_type type,
2029 					      const struct bpf_prog *prog,
2030 					      struct bpf_insn_access_aux *info)
2031 {
2032 	if (!bpf_tracing_ctx_access(off, size, type))
2033 		return false;
2034 	return btf_ctx_access(off, size, type, prog, info);
2035 }
2036 
2037 int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
2038 		      const struct btf_type *t, int off, int size,
2039 		      enum bpf_access_type atype,
2040 		      u32 *next_btf_id, enum bpf_type_flag *flag);
2041 bool btf_struct_ids_match(struct bpf_verifier_log *log,
2042 			  const struct btf *btf, u32 id, int off,
2043 			  const struct btf *need_btf, u32 need_type_id,
2044 			  bool strict);
2045 
2046 int btf_distill_func_proto(struct bpf_verifier_log *log,
2047 			   struct btf *btf,
2048 			   const struct btf_type *func_proto,
2049 			   const char *func_name,
2050 			   struct btf_func_model *m);
2051 
2052 struct bpf_kfunc_arg_meta {
2053 	u64 r0_size;
2054 	bool r0_rdonly;
2055 	int ref_obj_id;
2056 	u32 flags;
2057 };
2058 
2059 struct bpf_reg_state;
2060 int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
2061 				struct bpf_reg_state *regs);
2062 int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
2063 			   struct bpf_reg_state *regs);
2064 int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
2065 			      const struct btf *btf, u32 func_id,
2066 			      struct bpf_reg_state *regs,
2067 			      struct bpf_kfunc_arg_meta *meta);
2068 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
2069 			  struct bpf_reg_state *reg);
2070 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
2071 			 struct btf *btf, const struct btf_type *t);
2072 
2073 struct bpf_prog *bpf_prog_by_id(u32 id);
2074 struct bpf_link *bpf_link_by_id(u32 id);
2075 
2076 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
2077 void bpf_task_storage_free(struct task_struct *task);
2078 bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
2079 const struct btf_func_model *
2080 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2081 			 const struct bpf_insn *insn);
2082 struct bpf_core_ctx {
2083 	struct bpf_verifier_log *log;
2084 	const struct btf *btf;
2085 };
2086 
2087 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
2088 		   int relo_idx, void *insn);
2089 
unprivileged_ebpf_enabled(void)2090 static inline bool unprivileged_ebpf_enabled(void)
2091 {
2092 	return !sysctl_unprivileged_bpf_disabled;
2093 }
2094 
2095 /* Not all bpf prog type has the bpf_ctx.
2096  * For the bpf prog type that has initialized the bpf_ctx,
2097  * this function can be used to decide if a kernel function
2098  * is called by a bpf program.
2099  */
has_current_bpf_ctx(void)2100 static inline bool has_current_bpf_ctx(void)
2101 {
2102 	return !!current->bpf_ctx;
2103 }
2104 
2105 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
2106 #else /* !CONFIG_BPF_SYSCALL */
bpf_prog_get(u32 ufd)2107 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
2108 {
2109 	return ERR_PTR(-EOPNOTSUPP);
2110 }
2111 
bpf_prog_get_type_dev(u32 ufd,enum bpf_prog_type type,bool attach_drv)2112 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
2113 						     enum bpf_prog_type type,
2114 						     bool attach_drv)
2115 {
2116 	return ERR_PTR(-EOPNOTSUPP);
2117 }
2118 
bpf_prog_add(struct bpf_prog * prog,int i)2119 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
2120 {
2121 }
2122 
bpf_prog_sub(struct bpf_prog * prog,int i)2123 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
2124 {
2125 }
2126 
bpf_prog_put(struct bpf_prog * prog)2127 static inline void bpf_prog_put(struct bpf_prog *prog)
2128 {
2129 }
2130 
bpf_prog_inc(struct bpf_prog * prog)2131 static inline void bpf_prog_inc(struct bpf_prog *prog)
2132 {
2133 }
2134 
2135 static inline struct bpf_prog *__must_check
bpf_prog_inc_not_zero(struct bpf_prog * prog)2136 bpf_prog_inc_not_zero(struct bpf_prog *prog)
2137 {
2138 	return ERR_PTR(-EOPNOTSUPP);
2139 }
2140 
bpf_link_init(struct bpf_link * link,enum bpf_link_type type,const struct bpf_link_ops * ops,struct bpf_prog * prog)2141 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2142 				 const struct bpf_link_ops *ops,
2143 				 struct bpf_prog *prog)
2144 {
2145 }
2146 
bpf_link_prime(struct bpf_link * link,struct bpf_link_primer * primer)2147 static inline int bpf_link_prime(struct bpf_link *link,
2148 				 struct bpf_link_primer *primer)
2149 {
2150 	return -EOPNOTSUPP;
2151 }
2152 
bpf_link_settle(struct bpf_link_primer * primer)2153 static inline int bpf_link_settle(struct bpf_link_primer *primer)
2154 {
2155 	return -EOPNOTSUPP;
2156 }
2157 
bpf_link_cleanup(struct bpf_link_primer * primer)2158 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
2159 {
2160 }
2161 
bpf_link_inc(struct bpf_link * link)2162 static inline void bpf_link_inc(struct bpf_link *link)
2163 {
2164 }
2165 
bpf_link_put(struct bpf_link * link)2166 static inline void bpf_link_put(struct bpf_link *link)
2167 {
2168 }
2169 
bpf_obj_get_user(const char __user * pathname,int flags)2170 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
2171 {
2172 	return -EOPNOTSUPP;
2173 }
2174 
__dev_flush(void)2175 static inline void __dev_flush(void)
2176 {
2177 }
2178 
2179 struct xdp_frame;
2180 struct bpf_dtab_netdev;
2181 struct bpf_cpu_map_entry;
2182 
2183 static inline
dev_xdp_enqueue(struct net_device * dev,struct xdp_frame * xdpf,struct net_device * dev_rx)2184 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
2185 		    struct net_device *dev_rx)
2186 {
2187 	return 0;
2188 }
2189 
2190 static inline
dev_map_enqueue(struct bpf_dtab_netdev * dst,struct xdp_frame * xdpf,struct net_device * dev_rx)2191 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
2192 		    struct net_device *dev_rx)
2193 {
2194 	return 0;
2195 }
2196 
2197 static inline
dev_map_enqueue_multi(struct xdp_frame * xdpf,struct net_device * dev_rx,struct bpf_map * map,bool exclude_ingress)2198 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
2199 			  struct bpf_map *map, bool exclude_ingress)
2200 {
2201 	return 0;
2202 }
2203 
2204 struct sk_buff;
2205 
dev_map_generic_redirect(struct bpf_dtab_netdev * dst,struct sk_buff * skb,struct bpf_prog * xdp_prog)2206 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
2207 					   struct sk_buff *skb,
2208 					   struct bpf_prog *xdp_prog)
2209 {
2210 	return 0;
2211 }
2212 
2213 static inline
dev_map_redirect_multi(struct net_device * dev,struct sk_buff * skb,struct bpf_prog * xdp_prog,struct bpf_map * map,bool exclude_ingress)2214 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2215 			   struct bpf_prog *xdp_prog, struct bpf_map *map,
2216 			   bool exclude_ingress)
2217 {
2218 	return 0;
2219 }
2220 
__cpu_map_flush(void)2221 static inline void __cpu_map_flush(void)
2222 {
2223 }
2224 
cpu_map_enqueue(struct bpf_cpu_map_entry * rcpu,struct xdp_frame * xdpf,struct net_device * dev_rx)2225 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
2226 				  struct xdp_frame *xdpf,
2227 				  struct net_device *dev_rx)
2228 {
2229 	return 0;
2230 }
2231 
cpu_map_generic_redirect(struct bpf_cpu_map_entry * rcpu,struct sk_buff * skb)2232 static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2233 					   struct sk_buff *skb)
2234 {
2235 	return -EOPNOTSUPP;
2236 }
2237 
bpf_prog_get_type_path(const char * name,enum bpf_prog_type type)2238 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
2239 				enum bpf_prog_type type)
2240 {
2241 	return ERR_PTR(-EOPNOTSUPP);
2242 }
2243 
bpf_prog_test_run_xdp(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2244 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
2245 					const union bpf_attr *kattr,
2246 					union bpf_attr __user *uattr)
2247 {
2248 	return -ENOTSUPP;
2249 }
2250 
bpf_prog_test_run_skb(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2251 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
2252 					const union bpf_attr *kattr,
2253 					union bpf_attr __user *uattr)
2254 {
2255 	return -ENOTSUPP;
2256 }
2257 
bpf_prog_test_run_tracing(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2258 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2259 					    const union bpf_attr *kattr,
2260 					    union bpf_attr __user *uattr)
2261 {
2262 	return -ENOTSUPP;
2263 }
2264 
bpf_prog_test_run_flow_dissector(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2265 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2266 						   const union bpf_attr *kattr,
2267 						   union bpf_attr __user *uattr)
2268 {
2269 	return -ENOTSUPP;
2270 }
2271 
bpf_prog_test_run_sk_lookup(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2272 static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2273 					      const union bpf_attr *kattr,
2274 					      union bpf_attr __user *uattr)
2275 {
2276 	return -ENOTSUPP;
2277 }
2278 
bpf_map_put(struct bpf_map * map)2279 static inline void bpf_map_put(struct bpf_map *map)
2280 {
2281 }
2282 
bpf_prog_by_id(u32 id)2283 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
2284 {
2285 	return ERR_PTR(-ENOTSUPP);
2286 }
2287 
btf_struct_access(struct bpf_verifier_log * log,const struct btf * btf,const struct btf_type * t,int off,int size,enum bpf_access_type atype,u32 * next_btf_id,enum bpf_type_flag * flag)2288 static inline int btf_struct_access(struct bpf_verifier_log *log,
2289 				    const struct btf *btf,
2290 				    const struct btf_type *t, int off, int size,
2291 				    enum bpf_access_type atype,
2292 				    u32 *next_btf_id, enum bpf_type_flag *flag)
2293 {
2294 	return -EACCES;
2295 }
2296 
2297 static inline const struct bpf_func_proto *
bpf_base_func_proto(enum bpf_func_id func_id)2298 bpf_base_func_proto(enum bpf_func_id func_id)
2299 {
2300 	return NULL;
2301 }
2302 
bpf_task_storage_free(struct task_struct * task)2303 static inline void bpf_task_storage_free(struct task_struct *task)
2304 {
2305 }
2306 
bpf_prog_has_kfunc_call(const struct bpf_prog * prog)2307 static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
2308 {
2309 	return false;
2310 }
2311 
2312 static inline const struct btf_func_model *
bpf_jit_find_kfunc_model(const struct bpf_prog * prog,const struct bpf_insn * insn)2313 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2314 			 const struct bpf_insn *insn)
2315 {
2316 	return NULL;
2317 }
2318 
unprivileged_ebpf_enabled(void)2319 static inline bool unprivileged_ebpf_enabled(void)
2320 {
2321 	return false;
2322 }
2323 
has_current_bpf_ctx(void)2324 static inline bool has_current_bpf_ctx(void)
2325 {
2326 	return false;
2327 }
2328 
bpf_prog_inc_misses_counter(struct bpf_prog * prog)2329 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2330 {
2331 }
2332 #endif /* CONFIG_BPF_SYSCALL */
2333 
2334 void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
2335 			  struct btf_mod_pair *used_btfs, u32 len);
2336 
bpf_prog_get_type(u32 ufd,enum bpf_prog_type type)2337 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
2338 						 enum bpf_prog_type type)
2339 {
2340 	return bpf_prog_get_type_dev(ufd, type, false);
2341 }
2342 
2343 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2344 			  struct bpf_map **used_maps, u32 len);
2345 
2346 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
2347 
2348 int bpf_prog_offload_compile(struct bpf_prog *prog);
2349 void bpf_prog_offload_destroy(struct bpf_prog *prog);
2350 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
2351 			       struct bpf_prog *prog);
2352 
2353 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2354 
2355 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
2356 int bpf_map_offload_update_elem(struct bpf_map *map,
2357 				void *key, void *value, u64 flags);
2358 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
2359 int bpf_map_offload_get_next_key(struct bpf_map *map,
2360 				 void *key, void *next_key);
2361 
2362 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
2363 
2364 struct bpf_offload_dev *
2365 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
2366 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
2367 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
2368 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
2369 				    struct net_device *netdev);
2370 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
2371 				       struct net_device *netdev);
2372 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
2373 
2374 void unpriv_ebpf_notify(int new_state);
2375 
2376 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
2377 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
2378 
bpf_prog_is_dev_bound(const struct bpf_prog_aux * aux)2379 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
2380 {
2381 	return aux->offload_requested;
2382 }
2383 
bpf_map_is_dev_bound(struct bpf_map * map)2384 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2385 {
2386 	return unlikely(map->ops == &bpf_map_offload_ops);
2387 }
2388 
2389 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
2390 void bpf_map_offload_map_free(struct bpf_map *map);
2391 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2392 			      const union bpf_attr *kattr,
2393 			      union bpf_attr __user *uattr);
2394 
2395 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
2396 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
2397 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
2398 int sock_map_bpf_prog_query(const union bpf_attr *attr,
2399 			    union bpf_attr __user *uattr);
2400 
2401 void sock_map_unhash(struct sock *sk);
2402 void sock_map_destroy(struct sock *sk);
2403 void sock_map_close(struct sock *sk, long timeout);
2404 #else
bpf_prog_offload_init(struct bpf_prog * prog,union bpf_attr * attr)2405 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
2406 					union bpf_attr *attr)
2407 {
2408 	return -EOPNOTSUPP;
2409 }
2410 
bpf_prog_is_dev_bound(struct bpf_prog_aux * aux)2411 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
2412 {
2413 	return false;
2414 }
2415 
bpf_map_is_dev_bound(struct bpf_map * map)2416 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2417 {
2418 	return false;
2419 }
2420 
bpf_map_offload_map_alloc(union bpf_attr * attr)2421 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
2422 {
2423 	return ERR_PTR(-EOPNOTSUPP);
2424 }
2425 
bpf_map_offload_map_free(struct bpf_map * map)2426 static inline void bpf_map_offload_map_free(struct bpf_map *map)
2427 {
2428 }
2429 
bpf_prog_test_run_syscall(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2430 static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2431 					    const union bpf_attr *kattr,
2432 					    union bpf_attr __user *uattr)
2433 {
2434 	return -ENOTSUPP;
2435 }
2436 
2437 #ifdef CONFIG_BPF_SYSCALL
sock_map_get_from_fd(const union bpf_attr * attr,struct bpf_prog * prog)2438 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
2439 				       struct bpf_prog *prog)
2440 {
2441 	return -EINVAL;
2442 }
2443 
sock_map_prog_detach(const union bpf_attr * attr,enum bpf_prog_type ptype)2444 static inline int sock_map_prog_detach(const union bpf_attr *attr,
2445 				       enum bpf_prog_type ptype)
2446 {
2447 	return -EOPNOTSUPP;
2448 }
2449 
sock_map_update_elem_sys(struct bpf_map * map,void * key,void * value,u64 flags)2450 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
2451 					   u64 flags)
2452 {
2453 	return -EOPNOTSUPP;
2454 }
2455 
sock_map_bpf_prog_query(const union bpf_attr * attr,union bpf_attr __user * uattr)2456 static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
2457 					  union bpf_attr __user *uattr)
2458 {
2459 	return -EINVAL;
2460 }
2461 #endif /* CONFIG_BPF_SYSCALL */
2462 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
2463 
2464 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
2465 void bpf_sk_reuseport_detach(struct sock *sk);
2466 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
2467 				       void *value);
2468 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
2469 				       void *value, u64 map_flags);
2470 #else
bpf_sk_reuseport_detach(struct sock * sk)2471 static inline void bpf_sk_reuseport_detach(struct sock *sk)
2472 {
2473 }
2474 
2475 #ifdef CONFIG_BPF_SYSCALL
bpf_fd_reuseport_array_lookup_elem(struct bpf_map * map,void * key,void * value)2476 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
2477 						     void *key, void *value)
2478 {
2479 	return -EOPNOTSUPP;
2480 }
2481 
bpf_fd_reuseport_array_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)2482 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
2483 						     void *key, void *value,
2484 						     u64 map_flags)
2485 {
2486 	return -EOPNOTSUPP;
2487 }
2488 #endif /* CONFIG_BPF_SYSCALL */
2489 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
2490 
2491 /* verifier prototypes for helper functions called from eBPF programs */
2492 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
2493 extern const struct bpf_func_proto bpf_map_update_elem_proto;
2494 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
2495 extern const struct bpf_func_proto bpf_map_push_elem_proto;
2496 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
2497 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
2498 extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
2499 
2500 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
2501 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
2502 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
2503 extern const struct bpf_func_proto bpf_tail_call_proto;
2504 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
2505 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
2506 extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
2507 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
2508 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
2509 extern const struct bpf_func_proto bpf_get_current_comm_proto;
2510 extern const struct bpf_func_proto bpf_get_stackid_proto;
2511 extern const struct bpf_func_proto bpf_get_stack_proto;
2512 extern const struct bpf_func_proto bpf_get_task_stack_proto;
2513 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
2514 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
2515 extern const struct bpf_func_proto bpf_sock_map_update_proto;
2516 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
2517 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
2518 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
2519 extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
2520 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
2521 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
2522 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
2523 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
2524 extern const struct bpf_func_proto bpf_spin_lock_proto;
2525 extern const struct bpf_func_proto bpf_spin_unlock_proto;
2526 extern const struct bpf_func_proto bpf_get_local_storage_proto;
2527 extern const struct bpf_func_proto bpf_strtol_proto;
2528 extern const struct bpf_func_proto bpf_strtoul_proto;
2529 extern const struct bpf_func_proto bpf_tcp_sock_proto;
2530 extern const struct bpf_func_proto bpf_jiffies64_proto;
2531 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
2532 extern const struct bpf_func_proto bpf_event_output_data_proto;
2533 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
2534 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
2535 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
2536 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
2537 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
2538 extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
2539 extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
2540 extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
2541 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
2542 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
2543 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
2544 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
2545 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
2546 extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
2547 extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
2548 extern const struct bpf_func_proto bpf_copy_from_user_proto;
2549 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
2550 extern const struct bpf_func_proto bpf_snprintf_proto;
2551 extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
2552 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
2553 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
2554 extern const struct bpf_func_proto bpf_sock_from_file_proto;
2555 extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
2556 extern const struct bpf_func_proto bpf_task_storage_get_proto;
2557 extern const struct bpf_func_proto bpf_task_storage_delete_proto;
2558 extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
2559 extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
2560 extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
2561 extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
2562 extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
2563 extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
2564 extern const struct bpf_func_proto bpf_find_vma_proto;
2565 extern const struct bpf_func_proto bpf_loop_proto;
2566 extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
2567 extern const struct bpf_func_proto bpf_set_retval_proto;
2568 extern const struct bpf_func_proto bpf_get_retval_proto;
2569 extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
2570 
2571 const struct bpf_func_proto *tracing_prog_func_proto(
2572   enum bpf_func_id func_id, const struct bpf_prog *prog);
2573 
2574 /* Shared helpers among cBPF and eBPF. */
2575 void bpf_user_rnd_init_once(void);
2576 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2577 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2578 
2579 #if defined(CONFIG_NET)
2580 bool bpf_sock_common_is_valid_access(int off, int size,
2581 				     enum bpf_access_type type,
2582 				     struct bpf_insn_access_aux *info);
2583 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2584 			      struct bpf_insn_access_aux *info);
2585 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2586 				const struct bpf_insn *si,
2587 				struct bpf_insn *insn_buf,
2588 				struct bpf_prog *prog,
2589 				u32 *target_size);
2590 #else
bpf_sock_common_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)2591 static inline bool bpf_sock_common_is_valid_access(int off, int size,
2592 						   enum bpf_access_type type,
2593 						   struct bpf_insn_access_aux *info)
2594 {
2595 	return false;
2596 }
bpf_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)2597 static inline bool bpf_sock_is_valid_access(int off, int size,
2598 					    enum bpf_access_type type,
2599 					    struct bpf_insn_access_aux *info)
2600 {
2601 	return false;
2602 }
bpf_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)2603 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2604 					      const struct bpf_insn *si,
2605 					      struct bpf_insn *insn_buf,
2606 					      struct bpf_prog *prog,
2607 					      u32 *target_size)
2608 {
2609 	return 0;
2610 }
2611 #endif
2612 
2613 #ifdef CONFIG_INET
2614 struct sk_reuseport_kern {
2615 	struct sk_buff *skb;
2616 	struct sock *sk;
2617 	struct sock *selected_sk;
2618 	struct sock *migrating_sk;
2619 	void *data_end;
2620 	u32 hash;
2621 	u32 reuseport_id;
2622 	bool bind_inany;
2623 };
2624 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2625 				  struct bpf_insn_access_aux *info);
2626 
2627 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2628 				    const struct bpf_insn *si,
2629 				    struct bpf_insn *insn_buf,
2630 				    struct bpf_prog *prog,
2631 				    u32 *target_size);
2632 
2633 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2634 				  struct bpf_insn_access_aux *info);
2635 
2636 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2637 				    const struct bpf_insn *si,
2638 				    struct bpf_insn *insn_buf,
2639 				    struct bpf_prog *prog,
2640 				    u32 *target_size);
2641 #else
bpf_tcp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)2642 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
2643 						enum bpf_access_type type,
2644 						struct bpf_insn_access_aux *info)
2645 {
2646 	return false;
2647 }
2648 
bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)2649 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2650 						  const struct bpf_insn *si,
2651 						  struct bpf_insn *insn_buf,
2652 						  struct bpf_prog *prog,
2653 						  u32 *target_size)
2654 {
2655 	return 0;
2656 }
bpf_xdp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)2657 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
2658 						enum bpf_access_type type,
2659 						struct bpf_insn_access_aux *info)
2660 {
2661 	return false;
2662 }
2663 
bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)2664 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2665 						  const struct bpf_insn *si,
2666 						  struct bpf_insn *insn_buf,
2667 						  struct bpf_prog *prog,
2668 						  u32 *target_size)
2669 {
2670 	return 0;
2671 }
2672 #endif /* CONFIG_INET */
2673 
2674 enum bpf_text_poke_type {
2675 	BPF_MOD_CALL,
2676 	BPF_MOD_JUMP,
2677 };
2678 
2679 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2680 		       void *addr1, void *addr2);
2681 
2682 void *bpf_arch_text_copy(void *dst, void *src, size_t len);
2683 int bpf_arch_text_invalidate(void *dst, size_t len);
2684 
2685 struct btf_id_set;
2686 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
2687 
2688 #define MAX_BPRINTF_VARARGS		12
2689 
2690 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
2691 			u32 **bin_buf, u32 num_args);
2692 void bpf_bprintf_cleanup(void);
2693 
2694 /* the implementation of the opaque uapi struct bpf_dynptr */
2695 struct bpf_dynptr_kern {
2696 	void *data;
2697 	/* Size represents the number of usable bytes of dynptr data.
2698 	 * If for example the offset is at 4 for a local dynptr whose data is
2699 	 * of type u64, the number of usable bytes is 4.
2700 	 *
2701 	 * The upper 8 bits are reserved. It is as follows:
2702 	 * Bits 0 - 23 = size
2703 	 * Bits 24 - 30 = dynptr type
2704 	 * Bit 31 = whether dynptr is read-only
2705 	 */
2706 	u32 size;
2707 	u32 offset;
2708 } __aligned(8);
2709 
2710 enum bpf_dynptr_type {
2711 	BPF_DYNPTR_TYPE_INVALID,
2712 	/* Points to memory that is local to the bpf program */
2713 	BPF_DYNPTR_TYPE_LOCAL,
2714 	/* Underlying data is a kernel-produced ringbuf record */
2715 	BPF_DYNPTR_TYPE_RINGBUF,
2716 };
2717 
2718 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2719 		     enum bpf_dynptr_type type, u32 offset, u32 size);
2720 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
2721 int bpf_dynptr_check_size(u32 size);
2722 u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr);
2723 
2724 #ifdef CONFIG_BPF_LSM
2725 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
2726 void bpf_cgroup_atype_put(int cgroup_atype);
2727 #else
bpf_cgroup_atype_get(u32 attach_btf_id,int cgroup_atype)2728 static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
bpf_cgroup_atype_put(int cgroup_atype)2729 static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
2730 #endif /* CONFIG_BPF_LSM */
2731 
2732 struct key;
2733 
2734 #ifdef CONFIG_KEYS
2735 struct bpf_key {
2736 	struct key *key;
2737 	bool has_ref;
2738 };
2739 #endif /* CONFIG_KEYS */
2740 #endif /* _LINUX_BPF_H */
2741