xref: /linux-6.15/include/linux/bpf.h (revision ef700f2e)
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 
9 #include <linux/workqueue.h>
10 #include <linux/file.h>
11 #include <linux/percpu.h>
12 #include <linux/err.h>
13 #include <linux/rbtree_latch.h>
14 #include <linux/numa.h>
15 #include <linux/mm_types.h>
16 #include <linux/wait.h>
17 #include <linux/refcount.h>
18 #include <linux/mutex.h>
19 #include <linux/module.h>
20 #include <linux/kallsyms.h>
21 #include <linux/capability.h>
22 #include <linux/sched/mm.h>
23 #include <linux/slab.h>
24 
25 struct bpf_verifier_env;
26 struct bpf_verifier_log;
27 struct perf_event;
28 struct bpf_prog;
29 struct bpf_prog_aux;
30 struct bpf_map;
31 struct sock;
32 struct seq_file;
33 struct btf;
34 struct btf_type;
35 struct exception_table_entry;
36 struct seq_operations;
37 struct bpf_iter_aux_info;
38 struct bpf_local_storage;
39 struct bpf_local_storage_map;
40 struct kobject;
41 struct mem_cgroup;
42 struct bpf_func_state;
43 
44 extern struct idr btf_idr;
45 extern spinlock_t btf_idr_lock;
46 extern struct kobject *btf_kobj;
47 
48 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
49 					struct bpf_iter_aux_info *aux);
50 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
51 struct bpf_iter_seq_info {
52 	const struct seq_operations *seq_ops;
53 	bpf_iter_init_seq_priv_t init_seq_private;
54 	bpf_iter_fini_seq_priv_t fini_seq_private;
55 	u32 seq_priv_size;
56 };
57 
58 /* map is generic key/value storage optionally accesible by eBPF programs */
59 struct bpf_map_ops {
60 	/* funcs callable from userspace (via syscall) */
61 	int (*map_alloc_check)(union bpf_attr *attr);
62 	struct bpf_map *(*map_alloc)(union bpf_attr *attr);
63 	void (*map_release)(struct bpf_map *map, struct file *map_file);
64 	void (*map_free)(struct bpf_map *map);
65 	int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
66 	void (*map_release_uref)(struct bpf_map *map);
67 	void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
68 	int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
69 				union bpf_attr __user *uattr);
70 	int (*map_lookup_and_delete_batch)(struct bpf_map *map,
71 					   const union bpf_attr *attr,
72 					   union bpf_attr __user *uattr);
73 	int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
74 				union bpf_attr __user *uattr);
75 	int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
76 				union bpf_attr __user *uattr);
77 
78 	/* funcs callable from userspace and from eBPF programs */
79 	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
80 	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
81 	int (*map_delete_elem)(struct bpf_map *map, void *key);
82 	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
83 	int (*map_pop_elem)(struct bpf_map *map, void *value);
84 	int (*map_peek_elem)(struct bpf_map *map, void *value);
85 
86 	/* funcs called by prog_array and perf_event_array map */
87 	void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
88 				int fd);
89 	void (*map_fd_put_ptr)(void *ptr);
90 	int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
91 	u32 (*map_fd_sys_lookup_elem)(void *ptr);
92 	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
93 				  struct seq_file *m);
94 	int (*map_check_btf)(const struct bpf_map *map,
95 			     const struct btf *btf,
96 			     const struct btf_type *key_type,
97 			     const struct btf_type *value_type);
98 
99 	/* Prog poke tracking helpers. */
100 	int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
101 	void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
102 	void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
103 			     struct bpf_prog *new);
104 
105 	/* Direct value access helpers. */
106 	int (*map_direct_value_addr)(const struct bpf_map *map,
107 				     u64 *imm, u32 off);
108 	int (*map_direct_value_meta)(const struct bpf_map *map,
109 				     u64 imm, u32 *off);
110 	int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
111 	__poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
112 			     struct poll_table_struct *pts);
113 
114 	/* Functions called by bpf_local_storage maps */
115 	int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
116 					void *owner, u32 size);
117 	void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
118 					   void *owner, u32 size);
119 	struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
120 
121 	/* Misc helpers.*/
122 	int (*map_redirect)(struct bpf_map *map, u32 ifindex, u64 flags);
123 
124 	/* map_meta_equal must be implemented for maps that can be
125 	 * used as an inner map.  It is a runtime check to ensure
126 	 * an inner map can be inserted to an outer map.
127 	 *
128 	 * Some properties of the inner map has been used during the
129 	 * verification time.  When inserting an inner map at the runtime,
130 	 * map_meta_equal has to ensure the inserting map has the same
131 	 * properties that the verifier has used earlier.
132 	 */
133 	bool (*map_meta_equal)(const struct bpf_map *meta0,
134 			       const struct bpf_map *meta1);
135 
136 
137 	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
138 					      struct bpf_func_state *caller,
139 					      struct bpf_func_state *callee);
140 	int (*map_for_each_callback)(struct bpf_map *map, void *callback_fn,
141 				     void *callback_ctx, u64 flags);
142 
143 	/* BTF name and id of struct allocated by map_alloc */
144 	const char * const map_btf_name;
145 	int *map_btf_id;
146 
147 	/* bpf_iter info used to open a seq_file */
148 	const struct bpf_iter_seq_info *iter_seq_info;
149 };
150 
151 struct bpf_map {
152 	/* The first two cachelines with read-mostly members of which some
153 	 * are also accessed in fast-path (e.g. ops, max_entries).
154 	 */
155 	const struct bpf_map_ops *ops ____cacheline_aligned;
156 	struct bpf_map *inner_map_meta;
157 #ifdef CONFIG_SECURITY
158 	void *security;
159 #endif
160 	enum bpf_map_type map_type;
161 	u32 key_size;
162 	u32 value_size;
163 	u32 max_entries;
164 	u32 map_flags;
165 	int spin_lock_off; /* >=0 valid offset, <0 error */
166 	u32 id;
167 	int numa_node;
168 	u32 btf_key_type_id;
169 	u32 btf_value_type_id;
170 	struct btf *btf;
171 #ifdef CONFIG_MEMCG_KMEM
172 	struct mem_cgroup *memcg;
173 #endif
174 	char name[BPF_OBJ_NAME_LEN];
175 	u32 btf_vmlinux_value_type_id;
176 	bool bypass_spec_v1;
177 	bool frozen; /* write-once; write-protected by freeze_mutex */
178 	/* 22 bytes hole */
179 
180 	/* The 3rd and 4th cacheline with misc members to avoid false sharing
181 	 * particularly with refcounting.
182 	 */
183 	atomic64_t refcnt ____cacheline_aligned;
184 	atomic64_t usercnt;
185 	struct work_struct work;
186 	struct mutex freeze_mutex;
187 	u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
188 };
189 
190 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
191 {
192 	return map->spin_lock_off >= 0;
193 }
194 
195 static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
196 {
197 	if (likely(!map_value_has_spin_lock(map)))
198 		return;
199 	*(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
200 		(struct bpf_spin_lock){};
201 }
202 
203 /* copy everything but bpf_spin_lock */
204 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
205 {
206 	if (unlikely(map_value_has_spin_lock(map))) {
207 		u32 off = map->spin_lock_off;
208 
209 		memcpy(dst, src, off);
210 		memcpy(dst + off + sizeof(struct bpf_spin_lock),
211 		       src + off + sizeof(struct bpf_spin_lock),
212 		       map->value_size - off - sizeof(struct bpf_spin_lock));
213 	} else {
214 		memcpy(dst, src, map->value_size);
215 	}
216 }
217 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
218 			   bool lock_src);
219 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
220 
221 struct bpf_offload_dev;
222 struct bpf_offloaded_map;
223 
224 struct bpf_map_dev_ops {
225 	int (*map_get_next_key)(struct bpf_offloaded_map *map,
226 				void *key, void *next_key);
227 	int (*map_lookup_elem)(struct bpf_offloaded_map *map,
228 			       void *key, void *value);
229 	int (*map_update_elem)(struct bpf_offloaded_map *map,
230 			       void *key, void *value, u64 flags);
231 	int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
232 };
233 
234 struct bpf_offloaded_map {
235 	struct bpf_map map;
236 	struct net_device *netdev;
237 	const struct bpf_map_dev_ops *dev_ops;
238 	void *dev_priv;
239 	struct list_head offloads;
240 };
241 
242 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
243 {
244 	return container_of(map, struct bpf_offloaded_map, map);
245 }
246 
247 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
248 {
249 	return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
250 }
251 
252 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
253 {
254 	return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
255 		map->ops->map_seq_show_elem;
256 }
257 
258 int map_check_no_btf(const struct bpf_map *map,
259 		     const struct btf *btf,
260 		     const struct btf_type *key_type,
261 		     const struct btf_type *value_type);
262 
263 bool bpf_map_meta_equal(const struct bpf_map *meta0,
264 			const struct bpf_map *meta1);
265 
266 extern const struct bpf_map_ops bpf_map_offload_ops;
267 
268 /* function argument constraints */
269 enum bpf_arg_type {
270 	ARG_DONTCARE = 0,	/* unused argument in helper function */
271 
272 	/* the following constraints used to prototype
273 	 * bpf_map_lookup/update/delete_elem() functions
274 	 */
275 	ARG_CONST_MAP_PTR,	/* const argument used as pointer to bpf_map */
276 	ARG_PTR_TO_MAP_KEY,	/* pointer to stack used as map key */
277 	ARG_PTR_TO_MAP_VALUE,	/* pointer to stack used as map value */
278 	ARG_PTR_TO_UNINIT_MAP_VALUE,	/* pointer to valid memory used to store a map value */
279 	ARG_PTR_TO_MAP_VALUE_OR_NULL,	/* pointer to stack used as map value or NULL */
280 
281 	/* the following constraints used to prototype bpf_memcmp() and other
282 	 * functions that access data on eBPF program stack
283 	 */
284 	ARG_PTR_TO_MEM,		/* pointer to valid memory (stack, packet, map value) */
285 	ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
286 	ARG_PTR_TO_UNINIT_MEM,	/* pointer to memory does not need to be initialized,
287 				 * helper function must fill all bytes or clear
288 				 * them in error case.
289 				 */
290 
291 	ARG_CONST_SIZE,		/* number of bytes accessed from memory */
292 	ARG_CONST_SIZE_OR_ZERO,	/* number of bytes accessed from memory or 0 */
293 
294 	ARG_PTR_TO_CTX,		/* pointer to context */
295 	ARG_PTR_TO_CTX_OR_NULL,	/* pointer to context or NULL */
296 	ARG_ANYTHING,		/* any (initialized) argument is ok */
297 	ARG_PTR_TO_SPIN_LOCK,	/* pointer to bpf_spin_lock */
298 	ARG_PTR_TO_SOCK_COMMON,	/* pointer to sock_common */
299 	ARG_PTR_TO_INT,		/* pointer to int */
300 	ARG_PTR_TO_LONG,	/* pointer to long */
301 	ARG_PTR_TO_SOCKET,	/* pointer to bpf_sock (fullsock) */
302 	ARG_PTR_TO_SOCKET_OR_NULL,	/* pointer to bpf_sock (fullsock) or NULL */
303 	ARG_PTR_TO_BTF_ID,	/* pointer to in-kernel struct */
304 	ARG_PTR_TO_ALLOC_MEM,	/* pointer to dynamically allocated memory */
305 	ARG_PTR_TO_ALLOC_MEM_OR_NULL,	/* pointer to dynamically allocated memory or NULL */
306 	ARG_CONST_ALLOC_SIZE_OR_ZERO,	/* number of allocated bytes requested */
307 	ARG_PTR_TO_BTF_ID_SOCK_COMMON,	/* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
308 	ARG_PTR_TO_PERCPU_BTF_ID,	/* pointer to in-kernel percpu type */
309 	ARG_PTR_TO_FUNC,	/* pointer to a bpf program function */
310 	ARG_PTR_TO_STACK_OR_NULL,	/* pointer to stack or NULL */
311 	__BPF_ARG_TYPE_MAX,
312 };
313 
314 /* type of values returned from helper functions */
315 enum bpf_return_type {
316 	RET_INTEGER,			/* function returns integer */
317 	RET_VOID,			/* function doesn't return anything */
318 	RET_PTR_TO_MAP_VALUE,		/* returns a pointer to map elem value */
319 	RET_PTR_TO_MAP_VALUE_OR_NULL,	/* returns a pointer to map elem value or NULL */
320 	RET_PTR_TO_SOCKET_OR_NULL,	/* returns a pointer to a socket or NULL */
321 	RET_PTR_TO_TCP_SOCK_OR_NULL,	/* returns a pointer to a tcp_sock or NULL */
322 	RET_PTR_TO_SOCK_COMMON_OR_NULL,	/* returns a pointer to a sock_common or NULL */
323 	RET_PTR_TO_ALLOC_MEM_OR_NULL,	/* returns a pointer to dynamically allocated memory or NULL */
324 	RET_PTR_TO_BTF_ID_OR_NULL,	/* returns a pointer to a btf_id or NULL */
325 	RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */
326 	RET_PTR_TO_MEM_OR_BTF_ID,	/* returns a pointer to a valid memory or a btf_id */
327 	RET_PTR_TO_BTF_ID,		/* returns a pointer to a btf_id */
328 };
329 
330 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
331  * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
332  * instructions after verifying
333  */
334 struct bpf_func_proto {
335 	u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
336 	bool gpl_only;
337 	bool pkt_access;
338 	enum bpf_return_type ret_type;
339 	union {
340 		struct {
341 			enum bpf_arg_type arg1_type;
342 			enum bpf_arg_type arg2_type;
343 			enum bpf_arg_type arg3_type;
344 			enum bpf_arg_type arg4_type;
345 			enum bpf_arg_type arg5_type;
346 		};
347 		enum bpf_arg_type arg_type[5];
348 	};
349 	union {
350 		struct {
351 			u32 *arg1_btf_id;
352 			u32 *arg2_btf_id;
353 			u32 *arg3_btf_id;
354 			u32 *arg4_btf_id;
355 			u32 *arg5_btf_id;
356 		};
357 		u32 *arg_btf_id[5];
358 	};
359 	int *ret_btf_id; /* return value btf_id */
360 	bool (*allowed)(const struct bpf_prog *prog);
361 };
362 
363 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
364  * the first argument to eBPF programs.
365  * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
366  */
367 struct bpf_context;
368 
369 enum bpf_access_type {
370 	BPF_READ = 1,
371 	BPF_WRITE = 2
372 };
373 
374 /* types of values stored in eBPF registers */
375 /* Pointer types represent:
376  * pointer
377  * pointer + imm
378  * pointer + (u16) var
379  * pointer + (u16) var + imm
380  * if (range > 0) then [ptr, ptr + range - off) is safe to access
381  * if (id > 0) means that some 'var' was added
382  * if (off > 0) means that 'imm' was added
383  */
384 enum bpf_reg_type {
385 	NOT_INIT = 0,		 /* nothing was written into register */
386 	SCALAR_VALUE,		 /* reg doesn't contain a valid pointer */
387 	PTR_TO_CTX,		 /* reg points to bpf_context */
388 	CONST_PTR_TO_MAP,	 /* reg points to struct bpf_map */
389 	PTR_TO_MAP_VALUE,	 /* reg points to map element value */
390 	PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
391 	PTR_TO_STACK,		 /* reg == frame_pointer + offset */
392 	PTR_TO_PACKET_META,	 /* skb->data - meta_len */
393 	PTR_TO_PACKET,		 /* reg points to skb->data */
394 	PTR_TO_PACKET_END,	 /* skb->data + headlen */
395 	PTR_TO_FLOW_KEYS,	 /* reg points to bpf_flow_keys */
396 	PTR_TO_SOCKET,		 /* reg points to struct bpf_sock */
397 	PTR_TO_SOCKET_OR_NULL,	 /* reg points to struct bpf_sock or NULL */
398 	PTR_TO_SOCK_COMMON,	 /* reg points to sock_common */
399 	PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
400 	PTR_TO_TCP_SOCK,	 /* reg points to struct tcp_sock */
401 	PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
402 	PTR_TO_TP_BUFFER,	 /* reg points to a writable raw tp's buffer */
403 	PTR_TO_XDP_SOCK,	 /* reg points to struct xdp_sock */
404 	/* PTR_TO_BTF_ID points to a kernel struct that does not need
405 	 * to be null checked by the BPF program. This does not imply the
406 	 * pointer is _not_ null and in practice this can easily be a null
407 	 * pointer when reading pointer chains. The assumption is program
408 	 * context will handle null pointer dereference typically via fault
409 	 * handling. The verifier must keep this in mind and can make no
410 	 * assumptions about null or non-null when doing branch analysis.
411 	 * Further, when passed into helpers the helpers can not, without
412 	 * additional context, assume the value is non-null.
413 	 */
414 	PTR_TO_BTF_ID,
415 	/* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
416 	 * been checked for null. Used primarily to inform the verifier
417 	 * an explicit null check is required for this struct.
418 	 */
419 	PTR_TO_BTF_ID_OR_NULL,
420 	PTR_TO_MEM,		 /* reg points to valid memory region */
421 	PTR_TO_MEM_OR_NULL,	 /* reg points to valid memory region or NULL */
422 	PTR_TO_RDONLY_BUF,	 /* reg points to a readonly buffer */
423 	PTR_TO_RDONLY_BUF_OR_NULL, /* reg points to a readonly buffer or NULL */
424 	PTR_TO_RDWR_BUF,	 /* reg points to a read/write buffer */
425 	PTR_TO_RDWR_BUF_OR_NULL, /* reg points to a read/write buffer or NULL */
426 	PTR_TO_PERCPU_BTF_ID,	 /* reg points to a percpu kernel variable */
427 	PTR_TO_FUNC,		 /* reg points to a bpf program function */
428 	PTR_TO_MAP_KEY,		 /* reg points to a map element key */
429 };
430 
431 /* The information passed from prog-specific *_is_valid_access
432  * back to the verifier.
433  */
434 struct bpf_insn_access_aux {
435 	enum bpf_reg_type reg_type;
436 	union {
437 		int ctx_field_size;
438 		struct {
439 			struct btf *btf;
440 			u32 btf_id;
441 		};
442 	};
443 	struct bpf_verifier_log *log; /* for verbose logs */
444 };
445 
446 static inline void
447 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
448 {
449 	aux->ctx_field_size = size;
450 }
451 
452 struct bpf_prog_ops {
453 	int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
454 			union bpf_attr __user *uattr);
455 };
456 
457 struct bpf_verifier_ops {
458 	/* return eBPF function prototype for verification */
459 	const struct bpf_func_proto *
460 	(*get_func_proto)(enum bpf_func_id func_id,
461 			  const struct bpf_prog *prog);
462 
463 	/* return true if 'size' wide access at offset 'off' within bpf_context
464 	 * with 'type' (read or write) is allowed
465 	 */
466 	bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
467 				const struct bpf_prog *prog,
468 				struct bpf_insn_access_aux *info);
469 	int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
470 			    const struct bpf_prog *prog);
471 	int (*gen_ld_abs)(const struct bpf_insn *orig,
472 			  struct bpf_insn *insn_buf);
473 	u32 (*convert_ctx_access)(enum bpf_access_type type,
474 				  const struct bpf_insn *src,
475 				  struct bpf_insn *dst,
476 				  struct bpf_prog *prog, u32 *target_size);
477 	int (*btf_struct_access)(struct bpf_verifier_log *log,
478 				 const struct btf *btf,
479 				 const struct btf_type *t, int off, int size,
480 				 enum bpf_access_type atype,
481 				 u32 *next_btf_id);
482 };
483 
484 struct bpf_prog_offload_ops {
485 	/* verifier basic callbacks */
486 	int (*insn_hook)(struct bpf_verifier_env *env,
487 			 int insn_idx, int prev_insn_idx);
488 	int (*finalize)(struct bpf_verifier_env *env);
489 	/* verifier optimization callbacks (called after .finalize) */
490 	int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
491 			    struct bpf_insn *insn);
492 	int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
493 	/* program management callbacks */
494 	int (*prepare)(struct bpf_prog *prog);
495 	int (*translate)(struct bpf_prog *prog);
496 	void (*destroy)(struct bpf_prog *prog);
497 };
498 
499 struct bpf_prog_offload {
500 	struct bpf_prog		*prog;
501 	struct net_device	*netdev;
502 	struct bpf_offload_dev	*offdev;
503 	void			*dev_priv;
504 	struct list_head	offloads;
505 	bool			dev_state;
506 	bool			opt_failed;
507 	void			*jited_image;
508 	u32			jited_len;
509 };
510 
511 enum bpf_cgroup_storage_type {
512 	BPF_CGROUP_STORAGE_SHARED,
513 	BPF_CGROUP_STORAGE_PERCPU,
514 	__BPF_CGROUP_STORAGE_MAX
515 };
516 
517 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
518 
519 /* The longest tracepoint has 12 args.
520  * See include/trace/bpf_probe.h
521  */
522 #define MAX_BPF_FUNC_ARGS 12
523 
524 /* The maximum number of arguments passed through registers
525  * a single function may have.
526  */
527 #define MAX_BPF_FUNC_REG_ARGS 5
528 
529 struct btf_func_model {
530 	u8 ret_size;
531 	u8 nr_args;
532 	u8 arg_size[MAX_BPF_FUNC_ARGS];
533 };
534 
535 /* Restore arguments before returning from trampoline to let original function
536  * continue executing. This flag is used for fentry progs when there are no
537  * fexit progs.
538  */
539 #define BPF_TRAMP_F_RESTORE_REGS	BIT(0)
540 /* Call original function after fentry progs, but before fexit progs.
541  * Makes sense for fentry/fexit, normal calls and indirect calls.
542  */
543 #define BPF_TRAMP_F_CALL_ORIG		BIT(1)
544 /* Skip current frame and return to parent.  Makes sense for fentry/fexit
545  * programs only. Should not be used with normal calls and indirect calls.
546  */
547 #define BPF_TRAMP_F_SKIP_FRAME		BIT(2)
548 
549 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
550  * bytes on x86.  Pick a number to fit into BPF_IMAGE_SIZE / 2
551  */
552 #define BPF_MAX_TRAMP_PROGS 38
553 
554 struct bpf_tramp_progs {
555 	struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS];
556 	int nr_progs;
557 };
558 
559 /* Different use cases for BPF trampoline:
560  * 1. replace nop at the function entry (kprobe equivalent)
561  *    flags = BPF_TRAMP_F_RESTORE_REGS
562  *    fentry = a set of programs to run before returning from trampoline
563  *
564  * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
565  *    flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
566  *    orig_call = fentry_ip + MCOUNT_INSN_SIZE
567  *    fentry = a set of program to run before calling original function
568  *    fexit = a set of program to run after original function
569  *
570  * 3. replace direct call instruction anywhere in the function body
571  *    or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
572  *    With flags = 0
573  *      fentry = a set of programs to run before returning from trampoline
574  *    With flags = BPF_TRAMP_F_CALL_ORIG
575  *      orig_call = original callback addr or direct function addr
576  *      fentry = a set of program to run before calling original function
577  *      fexit = a set of program to run after original function
578  */
579 int arch_prepare_bpf_trampoline(void *image, void *image_end,
580 				const struct btf_func_model *m, u32 flags,
581 				struct bpf_tramp_progs *tprogs,
582 				void *orig_call);
583 /* these two functions are called from generated trampoline */
584 u64 notrace __bpf_prog_enter(struct bpf_prog *prog);
585 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
586 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog);
587 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start);
588 
589 struct bpf_ksym {
590 	unsigned long		 start;
591 	unsigned long		 end;
592 	char			 name[KSYM_NAME_LEN];
593 	struct list_head	 lnode;
594 	struct latch_tree_node	 tnode;
595 	bool			 prog;
596 };
597 
598 enum bpf_tramp_prog_type {
599 	BPF_TRAMP_FENTRY,
600 	BPF_TRAMP_FEXIT,
601 	BPF_TRAMP_MODIFY_RETURN,
602 	BPF_TRAMP_MAX,
603 	BPF_TRAMP_REPLACE, /* more than MAX */
604 };
605 
606 struct bpf_trampoline {
607 	/* hlist for trampoline_table */
608 	struct hlist_node hlist;
609 	/* serializes access to fields of this trampoline */
610 	struct mutex mutex;
611 	refcount_t refcnt;
612 	u64 key;
613 	struct {
614 		struct btf_func_model model;
615 		void *addr;
616 		bool ftrace_managed;
617 	} func;
618 	/* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
619 	 * program by replacing one of its functions. func.addr is the address
620 	 * of the function it replaced.
621 	 */
622 	struct bpf_prog *extension_prog;
623 	/* list of BPF programs using this trampoline */
624 	struct hlist_head progs_hlist[BPF_TRAMP_MAX];
625 	/* Number of attached programs. A counter per kind. */
626 	int progs_cnt[BPF_TRAMP_MAX];
627 	/* Executable image of trampoline */
628 	void *image;
629 	u64 selector;
630 	struct bpf_ksym ksym;
631 };
632 
633 struct bpf_attach_target_info {
634 	struct btf_func_model fmodel;
635 	long tgt_addr;
636 	const char *tgt_name;
637 	const struct btf_type *tgt_type;
638 };
639 
640 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
641 
642 struct bpf_dispatcher_prog {
643 	struct bpf_prog *prog;
644 	refcount_t users;
645 };
646 
647 struct bpf_dispatcher {
648 	/* dispatcher mutex */
649 	struct mutex mutex;
650 	void *func;
651 	struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
652 	int num_progs;
653 	void *image;
654 	u32 image_off;
655 	struct bpf_ksym ksym;
656 };
657 
658 static __always_inline unsigned int bpf_dispatcher_nop_func(
659 	const void *ctx,
660 	const struct bpf_insn *insnsi,
661 	unsigned int (*bpf_func)(const void *,
662 				 const struct bpf_insn *))
663 {
664 	return bpf_func(ctx, insnsi);
665 }
666 #ifdef CONFIG_BPF_JIT
667 int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr);
668 int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr);
669 struct bpf_trampoline *bpf_trampoline_get(u64 key,
670 					  struct bpf_attach_target_info *tgt_info);
671 void bpf_trampoline_put(struct bpf_trampoline *tr);
672 #define BPF_DISPATCHER_INIT(_name) {				\
673 	.mutex = __MUTEX_INITIALIZER(_name.mutex),		\
674 	.func = &_name##_func,					\
675 	.progs = {},						\
676 	.num_progs = 0,						\
677 	.image = NULL,						\
678 	.image_off = 0,						\
679 	.ksym = {						\
680 		.name  = #_name,				\
681 		.lnode = LIST_HEAD_INIT(_name.ksym.lnode),	\
682 	},							\
683 }
684 
685 #define DEFINE_BPF_DISPATCHER(name)					\
686 	noinline unsigned int bpf_dispatcher_##name##_func(		\
687 		const void *ctx,					\
688 		const struct bpf_insn *insnsi,				\
689 		unsigned int (*bpf_func)(const void *,			\
690 					 const struct bpf_insn *))	\
691 	{								\
692 		return bpf_func(ctx, insnsi);				\
693 	}								\
694 	EXPORT_SYMBOL(bpf_dispatcher_##name##_func);			\
695 	struct bpf_dispatcher bpf_dispatcher_##name =			\
696 		BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
697 #define DECLARE_BPF_DISPATCHER(name)					\
698 	unsigned int bpf_dispatcher_##name##_func(			\
699 		const void *ctx,					\
700 		const struct bpf_insn *insnsi,				\
701 		unsigned int (*bpf_func)(const void *,			\
702 					 const struct bpf_insn *));	\
703 	extern struct bpf_dispatcher bpf_dispatcher_##name;
704 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
705 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
706 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
707 				struct bpf_prog *to);
708 /* Called only from JIT-enabled code, so there's no need for stubs. */
709 void *bpf_jit_alloc_exec_page(void);
710 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
711 void bpf_image_ksym_del(struct bpf_ksym *ksym);
712 void bpf_ksym_add(struct bpf_ksym *ksym);
713 void bpf_ksym_del(struct bpf_ksym *ksym);
714 #else
715 static inline int bpf_trampoline_link_prog(struct bpf_prog *prog,
716 					   struct bpf_trampoline *tr)
717 {
718 	return -ENOTSUPP;
719 }
720 static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog,
721 					     struct bpf_trampoline *tr)
722 {
723 	return -ENOTSUPP;
724 }
725 static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
726 							struct bpf_attach_target_info *tgt_info)
727 {
728 	return ERR_PTR(-EOPNOTSUPP);
729 }
730 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
731 #define DEFINE_BPF_DISPATCHER(name)
732 #define DECLARE_BPF_DISPATCHER(name)
733 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
734 #define BPF_DISPATCHER_PTR(name) NULL
735 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
736 					      struct bpf_prog *from,
737 					      struct bpf_prog *to) {}
738 static inline bool is_bpf_image_address(unsigned long address)
739 {
740 	return false;
741 }
742 #endif
743 
744 struct bpf_func_info_aux {
745 	u16 linkage;
746 	bool unreliable;
747 };
748 
749 enum bpf_jit_poke_reason {
750 	BPF_POKE_REASON_TAIL_CALL,
751 };
752 
753 /* Descriptor of pokes pointing /into/ the JITed image. */
754 struct bpf_jit_poke_descriptor {
755 	void *tailcall_target;
756 	void *tailcall_bypass;
757 	void *bypass_addr;
758 	union {
759 		struct {
760 			struct bpf_map *map;
761 			u32 key;
762 		} tail_call;
763 	};
764 	bool tailcall_target_stable;
765 	u8 adj_off;
766 	u16 reason;
767 	u32 insn_idx;
768 };
769 
770 /* reg_type info for ctx arguments */
771 struct bpf_ctx_arg_aux {
772 	u32 offset;
773 	enum bpf_reg_type reg_type;
774 	u32 btf_id;
775 };
776 
777 struct btf_mod_pair {
778 	struct btf *btf;
779 	struct module *module;
780 };
781 
782 struct bpf_prog_aux {
783 	atomic64_t refcnt;
784 	u32 used_map_cnt;
785 	u32 used_btf_cnt;
786 	u32 max_ctx_offset;
787 	u32 max_pkt_offset;
788 	u32 max_tp_access;
789 	u32 stack_depth;
790 	u32 id;
791 	u32 func_cnt; /* used by non-func prog as the number of func progs */
792 	u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
793 	u32 attach_btf_id; /* in-kernel BTF type id to attach to */
794 	u32 ctx_arg_info_size;
795 	u32 max_rdonly_access;
796 	u32 max_rdwr_access;
797 	struct btf *attach_btf;
798 	const struct bpf_ctx_arg_aux *ctx_arg_info;
799 	struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
800 	struct bpf_prog *dst_prog;
801 	struct bpf_trampoline *dst_trampoline;
802 	enum bpf_prog_type saved_dst_prog_type;
803 	enum bpf_attach_type saved_dst_attach_type;
804 	bool verifier_zext; /* Zero extensions has been inserted by verifier. */
805 	bool offload_requested;
806 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
807 	bool func_proto_unreliable;
808 	bool sleepable;
809 	bool tail_call_reachable;
810 	enum bpf_tramp_prog_type trampoline_prog_type;
811 	struct hlist_node tramp_hlist;
812 	/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
813 	const struct btf_type *attach_func_proto;
814 	/* function name for valid attach_btf_id */
815 	const char *attach_func_name;
816 	struct bpf_prog **func;
817 	void *jit_data; /* JIT specific data. arch dependent */
818 	struct bpf_jit_poke_descriptor *poke_tab;
819 	u32 size_poke_tab;
820 	struct bpf_ksym ksym;
821 	const struct bpf_prog_ops *ops;
822 	struct bpf_map **used_maps;
823 	struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
824 	struct btf_mod_pair *used_btfs;
825 	struct bpf_prog *prog;
826 	struct user_struct *user;
827 	u64 load_time; /* ns since boottime */
828 	struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
829 	char name[BPF_OBJ_NAME_LEN];
830 #ifdef CONFIG_SECURITY
831 	void *security;
832 #endif
833 	struct bpf_prog_offload *offload;
834 	struct btf *btf;
835 	struct bpf_func_info *func_info;
836 	struct bpf_func_info_aux *func_info_aux;
837 	/* bpf_line_info loaded from userspace.  linfo->insn_off
838 	 * has the xlated insn offset.
839 	 * Both the main and sub prog share the same linfo.
840 	 * The subprog can access its first linfo by
841 	 * using the linfo_idx.
842 	 */
843 	struct bpf_line_info *linfo;
844 	/* jited_linfo is the jited addr of the linfo.  It has a
845 	 * one to one mapping to linfo:
846 	 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
847 	 * Both the main and sub prog share the same jited_linfo.
848 	 * The subprog can access its first jited_linfo by
849 	 * using the linfo_idx.
850 	 */
851 	void **jited_linfo;
852 	u32 func_info_cnt;
853 	u32 nr_linfo;
854 	/* subprog can use linfo_idx to access its first linfo and
855 	 * jited_linfo.
856 	 * main prog always has linfo_idx == 0
857 	 */
858 	u32 linfo_idx;
859 	u32 num_exentries;
860 	struct exception_table_entry *extable;
861 	union {
862 		struct work_struct work;
863 		struct rcu_head	rcu;
864 	};
865 };
866 
867 struct bpf_array_aux {
868 	/* 'Ownership' of prog array is claimed by the first program that
869 	 * is going to use this map or by the first program which FD is
870 	 * stored in the map to make sure that all callers and callees have
871 	 * the same prog type and JITed flag.
872 	 */
873 	enum bpf_prog_type type;
874 	bool jited;
875 	/* Programs with direct jumps into programs part of this array. */
876 	struct list_head poke_progs;
877 	struct bpf_map *map;
878 	struct mutex poke_mutex;
879 	struct work_struct work;
880 };
881 
882 struct bpf_link {
883 	atomic64_t refcnt;
884 	u32 id;
885 	enum bpf_link_type type;
886 	const struct bpf_link_ops *ops;
887 	struct bpf_prog *prog;
888 	struct work_struct work;
889 };
890 
891 struct bpf_link_ops {
892 	void (*release)(struct bpf_link *link);
893 	void (*dealloc)(struct bpf_link *link);
894 	int (*detach)(struct bpf_link *link);
895 	int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
896 			   struct bpf_prog *old_prog);
897 	void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
898 	int (*fill_link_info)(const struct bpf_link *link,
899 			      struct bpf_link_info *info);
900 };
901 
902 struct bpf_link_primer {
903 	struct bpf_link *link;
904 	struct file *file;
905 	int fd;
906 	u32 id;
907 };
908 
909 struct bpf_struct_ops_value;
910 struct btf_type;
911 struct btf_member;
912 
913 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
914 struct bpf_struct_ops {
915 	const struct bpf_verifier_ops *verifier_ops;
916 	int (*init)(struct btf *btf);
917 	int (*check_member)(const struct btf_type *t,
918 			    const struct btf_member *member);
919 	int (*init_member)(const struct btf_type *t,
920 			   const struct btf_member *member,
921 			   void *kdata, const void *udata);
922 	int (*reg)(void *kdata);
923 	void (*unreg)(void *kdata);
924 	const struct btf_type *type;
925 	const struct btf_type *value_type;
926 	const char *name;
927 	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
928 	u32 type_id;
929 	u32 value_id;
930 };
931 
932 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
933 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
934 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
935 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
936 bool bpf_struct_ops_get(const void *kdata);
937 void bpf_struct_ops_put(const void *kdata);
938 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
939 				       void *value);
940 static inline bool bpf_try_module_get(const void *data, struct module *owner)
941 {
942 	if (owner == BPF_MODULE_OWNER)
943 		return bpf_struct_ops_get(data);
944 	else
945 		return try_module_get(owner);
946 }
947 static inline void bpf_module_put(const void *data, struct module *owner)
948 {
949 	if (owner == BPF_MODULE_OWNER)
950 		bpf_struct_ops_put(data);
951 	else
952 		module_put(owner);
953 }
954 #else
955 static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
956 {
957 	return NULL;
958 }
959 static inline void bpf_struct_ops_init(struct btf *btf,
960 				       struct bpf_verifier_log *log)
961 {
962 }
963 static inline bool bpf_try_module_get(const void *data, struct module *owner)
964 {
965 	return try_module_get(owner);
966 }
967 static inline void bpf_module_put(const void *data, struct module *owner)
968 {
969 	module_put(owner);
970 }
971 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
972 						     void *key,
973 						     void *value)
974 {
975 	return -EINVAL;
976 }
977 #endif
978 
979 struct bpf_array {
980 	struct bpf_map map;
981 	u32 elem_size;
982 	u32 index_mask;
983 	struct bpf_array_aux *aux;
984 	union {
985 		char value[0] __aligned(8);
986 		void *ptrs[0] __aligned(8);
987 		void __percpu *pptrs[0] __aligned(8);
988 	};
989 };
990 
991 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
992 #define MAX_TAIL_CALL_CNT 32
993 
994 #define BPF_F_ACCESS_MASK	(BPF_F_RDONLY |		\
995 				 BPF_F_RDONLY_PROG |	\
996 				 BPF_F_WRONLY |		\
997 				 BPF_F_WRONLY_PROG)
998 
999 #define BPF_MAP_CAN_READ	BIT(0)
1000 #define BPF_MAP_CAN_WRITE	BIT(1)
1001 
1002 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1003 {
1004 	u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1005 
1006 	/* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1007 	 * not possible.
1008 	 */
1009 	if (access_flags & BPF_F_RDONLY_PROG)
1010 		return BPF_MAP_CAN_READ;
1011 	else if (access_flags & BPF_F_WRONLY_PROG)
1012 		return BPF_MAP_CAN_WRITE;
1013 	else
1014 		return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1015 }
1016 
1017 static inline bool bpf_map_flags_access_ok(u32 access_flags)
1018 {
1019 	return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1020 	       (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1021 }
1022 
1023 struct bpf_event_entry {
1024 	struct perf_event *event;
1025 	struct file *perf_file;
1026 	struct file *map_file;
1027 	struct rcu_head rcu;
1028 };
1029 
1030 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
1031 int bpf_prog_calc_tag(struct bpf_prog *fp);
1032 
1033 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
1034 
1035 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
1036 					unsigned long off, unsigned long len);
1037 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1038 					const struct bpf_insn *src,
1039 					struct bpf_insn *dst,
1040 					struct bpf_prog *prog,
1041 					u32 *target_size);
1042 
1043 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1044 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
1045 
1046 /* an array of programs to be executed under rcu_lock.
1047  *
1048  * Typical usage:
1049  * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
1050  *
1051  * the structure returned by bpf_prog_array_alloc() should be populated
1052  * with program pointers and the last pointer must be NULL.
1053  * The user has to keep refcnt on the program and make sure the program
1054  * is removed from the array before bpf_prog_put().
1055  * The 'struct bpf_prog_array *' should only be replaced with xchg()
1056  * since other cpus are walking the array of pointers in parallel.
1057  */
1058 struct bpf_prog_array_item {
1059 	struct bpf_prog *prog;
1060 	struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1061 };
1062 
1063 struct bpf_prog_array {
1064 	struct rcu_head rcu;
1065 	struct bpf_prog_array_item items[];
1066 };
1067 
1068 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
1069 void bpf_prog_array_free(struct bpf_prog_array *progs);
1070 int bpf_prog_array_length(struct bpf_prog_array *progs);
1071 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
1072 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
1073 				__u32 __user *prog_ids, u32 cnt);
1074 
1075 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
1076 				struct bpf_prog *old_prog);
1077 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1078 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1079 			     struct bpf_prog *prog);
1080 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
1081 			     u32 *prog_ids, u32 request_cnt,
1082 			     u32 *prog_cnt);
1083 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
1084 			struct bpf_prog *exclude_prog,
1085 			struct bpf_prog *include_prog,
1086 			struct bpf_prog_array **new_array);
1087 
1088 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1089 #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE			(1 << 0)
1090 /* BPF program asks to set CN on the packet. */
1091 #define BPF_RET_SET_CN						(1 << 0)
1092 
1093 #define BPF_PROG_RUN_ARRAY_FLAGS(array, ctx, func, ret_flags)		\
1094 	({								\
1095 		struct bpf_prog_array_item *_item;			\
1096 		struct bpf_prog *_prog;					\
1097 		struct bpf_prog_array *_array;				\
1098 		u32 _ret = 1;						\
1099 		u32 func_ret;						\
1100 		migrate_disable();					\
1101 		rcu_read_lock();					\
1102 		_array = rcu_dereference(array);			\
1103 		_item = &_array->items[0];				\
1104 		while ((_prog = READ_ONCE(_item->prog))) {		\
1105 			bpf_cgroup_storage_set(_item->cgroup_storage);	\
1106 			func_ret = func(_prog, ctx);			\
1107 			_ret &= (func_ret & 1);				\
1108 			*(ret_flags) |= (func_ret >> 1);			\
1109 			_item++;					\
1110 		}							\
1111 		rcu_read_unlock();					\
1112 		migrate_enable();					\
1113 		_ret;							\
1114 	 })
1115 
1116 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null)	\
1117 	({						\
1118 		struct bpf_prog_array_item *_item;	\
1119 		struct bpf_prog *_prog;			\
1120 		struct bpf_prog_array *_array;		\
1121 		u32 _ret = 1;				\
1122 		migrate_disable();			\
1123 		rcu_read_lock();			\
1124 		_array = rcu_dereference(array);	\
1125 		if (unlikely(check_non_null && !_array))\
1126 			goto _out;			\
1127 		_item = &_array->items[0];		\
1128 		while ((_prog = READ_ONCE(_item->prog))) {		\
1129 			bpf_cgroup_storage_set(_item->cgroup_storage);	\
1130 			_ret &= func(_prog, ctx);	\
1131 			_item++;			\
1132 		}					\
1133 _out:							\
1134 		rcu_read_unlock();			\
1135 		migrate_enable();			\
1136 		_ret;					\
1137 	 })
1138 
1139 /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
1140  * so BPF programs can request cwr for TCP packets.
1141  *
1142  * Current cgroup skb programs can only return 0 or 1 (0 to drop the
1143  * packet. This macro changes the behavior so the low order bit
1144  * indicates whether the packet should be dropped (0) or not (1)
1145  * and the next bit is a congestion notification bit. This could be
1146  * used by TCP to call tcp_enter_cwr()
1147  *
1148  * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
1149  *   0: drop packet
1150  *   1: keep packet
1151  *   2: drop packet and cn
1152  *   3: keep packet and cn
1153  *
1154  * This macro then converts it to one of the NET_XMIT or an error
1155  * code that is then interpreted as drop packet (and no cn):
1156  *   0: NET_XMIT_SUCCESS  skb should be transmitted
1157  *   1: NET_XMIT_DROP     skb should be dropped and cn
1158  *   2: NET_XMIT_CN       skb should be transmitted and cn
1159  *   3: -EPERM            skb should be dropped
1160  */
1161 #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func)		\
1162 	({						\
1163 		u32 _flags = 0;				\
1164 		bool _cn;				\
1165 		u32 _ret;				\
1166 		_ret = BPF_PROG_RUN_ARRAY_FLAGS(array, ctx, func, &_flags); \
1167 		_cn = _flags & BPF_RET_SET_CN;		\
1168 		if (_ret)				\
1169 			_ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS);	\
1170 		else					\
1171 			_ret = (_cn ? NET_XMIT_DROP : -EPERM);		\
1172 		_ret;					\
1173 	})
1174 
1175 #define BPF_PROG_RUN_ARRAY(array, ctx, func)		\
1176 	__BPF_PROG_RUN_ARRAY(array, ctx, func, false)
1177 
1178 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func)	\
1179 	__BPF_PROG_RUN_ARRAY(array, ctx, func, true)
1180 
1181 #ifdef CONFIG_BPF_SYSCALL
1182 DECLARE_PER_CPU(int, bpf_prog_active);
1183 extern struct mutex bpf_stats_enabled_mutex;
1184 
1185 /*
1186  * Block execution of BPF programs attached to instrumentation (perf,
1187  * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1188  * these events can happen inside a region which holds a map bucket lock
1189  * and can deadlock on it.
1190  *
1191  * Use the preemption safe inc/dec variants on RT because migrate disable
1192  * is preemptible on RT and preemption in the middle of the RMW operation
1193  * might lead to inconsistent state. Use the raw variants for non RT
1194  * kernels as migrate_disable() maps to preempt_disable() so the slightly
1195  * more expensive save operation can be avoided.
1196  */
1197 static inline void bpf_disable_instrumentation(void)
1198 {
1199 	migrate_disable();
1200 	if (IS_ENABLED(CONFIG_PREEMPT_RT))
1201 		this_cpu_inc(bpf_prog_active);
1202 	else
1203 		__this_cpu_inc(bpf_prog_active);
1204 }
1205 
1206 static inline void bpf_enable_instrumentation(void)
1207 {
1208 	if (IS_ENABLED(CONFIG_PREEMPT_RT))
1209 		this_cpu_dec(bpf_prog_active);
1210 	else
1211 		__this_cpu_dec(bpf_prog_active);
1212 	migrate_enable();
1213 }
1214 
1215 extern const struct file_operations bpf_map_fops;
1216 extern const struct file_operations bpf_prog_fops;
1217 extern const struct file_operations bpf_iter_fops;
1218 
1219 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1220 	extern const struct bpf_prog_ops _name ## _prog_ops; \
1221 	extern const struct bpf_verifier_ops _name ## _verifier_ops;
1222 #define BPF_MAP_TYPE(_id, _ops) \
1223 	extern const struct bpf_map_ops _ops;
1224 #define BPF_LINK_TYPE(_id, _name)
1225 #include <linux/bpf_types.h>
1226 #undef BPF_PROG_TYPE
1227 #undef BPF_MAP_TYPE
1228 #undef BPF_LINK_TYPE
1229 
1230 extern const struct bpf_prog_ops bpf_offload_prog_ops;
1231 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1232 extern const struct bpf_verifier_ops xdp_analyzer_ops;
1233 
1234 struct bpf_prog *bpf_prog_get(u32 ufd);
1235 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1236 				       bool attach_drv);
1237 void bpf_prog_add(struct bpf_prog *prog, int i);
1238 void bpf_prog_sub(struct bpf_prog *prog, int i);
1239 void bpf_prog_inc(struct bpf_prog *prog);
1240 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1241 void bpf_prog_put(struct bpf_prog *prog);
1242 
1243 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1244 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1245 
1246 struct bpf_map *bpf_map_get(u32 ufd);
1247 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1248 struct bpf_map *__bpf_map_get(struct fd f);
1249 void bpf_map_inc(struct bpf_map *map);
1250 void bpf_map_inc_with_uref(struct bpf_map *map);
1251 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1252 void bpf_map_put_with_uref(struct bpf_map *map);
1253 void bpf_map_put(struct bpf_map *map);
1254 void *bpf_map_area_alloc(u64 size, int numa_node);
1255 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1256 void bpf_map_area_free(void *base);
1257 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1258 int  generic_map_lookup_batch(struct bpf_map *map,
1259 			      const union bpf_attr *attr,
1260 			      union bpf_attr __user *uattr);
1261 int  generic_map_update_batch(struct bpf_map *map,
1262 			      const union bpf_attr *attr,
1263 			      union bpf_attr __user *uattr);
1264 int  generic_map_delete_batch(struct bpf_map *map,
1265 			      const union bpf_attr *attr,
1266 			      union bpf_attr __user *uattr);
1267 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1268 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
1269 
1270 #ifdef CONFIG_MEMCG_KMEM
1271 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1272 			   int node);
1273 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1274 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1275 				    size_t align, gfp_t flags);
1276 #else
1277 static inline void *
1278 bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1279 		     int node)
1280 {
1281 	return kmalloc_node(size, flags, node);
1282 }
1283 
1284 static inline void *
1285 bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1286 {
1287 	return kzalloc(size, flags);
1288 }
1289 
1290 static inline void __percpu *
1291 bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1292 		     gfp_t flags)
1293 {
1294 	return __alloc_percpu_gfp(size, align, flags);
1295 }
1296 #endif
1297 
1298 extern int sysctl_unprivileged_bpf_disabled;
1299 
1300 static inline bool bpf_allow_ptr_leaks(void)
1301 {
1302 	return perfmon_capable();
1303 }
1304 
1305 static inline bool bpf_allow_uninit_stack(void)
1306 {
1307 	return perfmon_capable();
1308 }
1309 
1310 static inline bool bpf_allow_ptr_to_map_access(void)
1311 {
1312 	return perfmon_capable();
1313 }
1314 
1315 static inline bool bpf_bypass_spec_v1(void)
1316 {
1317 	return perfmon_capable();
1318 }
1319 
1320 static inline bool bpf_bypass_spec_v4(void)
1321 {
1322 	return perfmon_capable();
1323 }
1324 
1325 int bpf_map_new_fd(struct bpf_map *map, int flags);
1326 int bpf_prog_new_fd(struct bpf_prog *prog);
1327 
1328 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1329 		   const struct bpf_link_ops *ops, struct bpf_prog *prog);
1330 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1331 int bpf_link_settle(struct bpf_link_primer *primer);
1332 void bpf_link_cleanup(struct bpf_link_primer *primer);
1333 void bpf_link_inc(struct bpf_link *link);
1334 void bpf_link_put(struct bpf_link *link);
1335 int bpf_link_new_fd(struct bpf_link *link);
1336 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1337 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1338 
1339 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1340 int bpf_obj_get_user(const char __user *pathname, int flags);
1341 
1342 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1343 #define DEFINE_BPF_ITER_FUNC(target, args...)			\
1344 	extern int bpf_iter_ ## target(args);			\
1345 	int __init bpf_iter_ ## target(args) { return 0; }
1346 
1347 struct bpf_iter_aux_info {
1348 	struct bpf_map *map;
1349 };
1350 
1351 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1352 					union bpf_iter_link_info *linfo,
1353 					struct bpf_iter_aux_info *aux);
1354 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
1355 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1356 					struct seq_file *seq);
1357 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1358 					 struct bpf_link_info *info);
1359 
1360 enum bpf_iter_feature {
1361 	BPF_ITER_RESCHED	= BIT(0),
1362 };
1363 
1364 #define BPF_ITER_CTX_ARG_MAX 2
1365 struct bpf_iter_reg {
1366 	const char *target;
1367 	bpf_iter_attach_target_t attach_target;
1368 	bpf_iter_detach_target_t detach_target;
1369 	bpf_iter_show_fdinfo_t show_fdinfo;
1370 	bpf_iter_fill_link_info_t fill_link_info;
1371 	u32 ctx_arg_info_size;
1372 	u32 feature;
1373 	struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1374 	const struct bpf_iter_seq_info *seq_info;
1375 };
1376 
1377 struct bpf_iter_meta {
1378 	__bpf_md_ptr(struct seq_file *, seq);
1379 	u64 session_id;
1380 	u64 seq_num;
1381 };
1382 
1383 struct bpf_iter__bpf_map_elem {
1384 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
1385 	__bpf_md_ptr(struct bpf_map *, map);
1386 	__bpf_md_ptr(void *, key);
1387 	__bpf_md_ptr(void *, value);
1388 };
1389 
1390 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1391 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1392 bool bpf_iter_prog_supported(struct bpf_prog *prog);
1393 int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
1394 int bpf_iter_new_fd(struct bpf_link *link);
1395 bool bpf_link_is_iter(struct bpf_link *link);
1396 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1397 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1398 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1399 			      struct seq_file *seq);
1400 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1401 				struct bpf_link_info *info);
1402 
1403 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1404 				   struct bpf_func_state *caller,
1405 				   struct bpf_func_state *callee);
1406 
1407 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1408 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1409 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1410 			   u64 flags);
1411 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1412 			    u64 flags);
1413 
1414 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1415 
1416 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1417 				 void *key, void *value, u64 map_flags);
1418 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1419 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1420 				void *key, void *value, u64 map_flags);
1421 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1422 
1423 int bpf_get_file_flag(int flags);
1424 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
1425 			     size_t actual_size);
1426 
1427 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
1428  * forced to use 'long' read/writes to try to atomically copy long counters.
1429  * Best-effort only.  No barriers here, since it _will_ race with concurrent
1430  * updates from BPF programs. Called from bpf syscall and mostly used with
1431  * size 8 or 16 bytes, so ask compiler to inline it.
1432  */
1433 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
1434 {
1435 	const long *lsrc = src;
1436 	long *ldst = dst;
1437 
1438 	size /= sizeof(long);
1439 	while (size--)
1440 		*ldst++ = *lsrc++;
1441 }
1442 
1443 /* verify correctness of eBPF program */
1444 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
1445 	      union bpf_attr __user *uattr);
1446 
1447 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1448 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
1449 #endif
1450 
1451 struct btf *bpf_get_btf_vmlinux(void);
1452 
1453 /* Map specifics */
1454 struct xdp_buff;
1455 struct sk_buff;
1456 struct bpf_dtab_netdev;
1457 struct bpf_cpu_map_entry;
1458 
1459 void __dev_flush(void);
1460 int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1461 		    struct net_device *dev_rx);
1462 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1463 		    struct net_device *dev_rx);
1464 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1465 			     struct bpf_prog *xdp_prog);
1466 bool dev_map_can_have_prog(struct bpf_map *map);
1467 
1468 void __cpu_map_flush(void);
1469 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
1470 		    struct net_device *dev_rx);
1471 bool cpu_map_prog_allowed(struct bpf_map *map);
1472 
1473 /* Return map's numa specified by userspace */
1474 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1475 {
1476 	return (attr->map_flags & BPF_F_NUMA_NODE) ?
1477 		attr->numa_node : NUMA_NO_NODE;
1478 }
1479 
1480 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
1481 int array_map_alloc_check(union bpf_attr *attr);
1482 
1483 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1484 			  union bpf_attr __user *uattr);
1485 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1486 			  union bpf_attr __user *uattr);
1487 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1488 			      const union bpf_attr *kattr,
1489 			      union bpf_attr __user *uattr);
1490 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1491 				     const union bpf_attr *kattr,
1492 				     union bpf_attr __user *uattr);
1493 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
1494 			     const union bpf_attr *kattr,
1495 			     union bpf_attr __user *uattr);
1496 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1497 				const union bpf_attr *kattr,
1498 				union bpf_attr __user *uattr);
1499 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1500 		    const struct bpf_prog *prog,
1501 		    struct bpf_insn_access_aux *info);
1502 int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
1503 		      const struct btf_type *t, int off, int size,
1504 		      enum bpf_access_type atype,
1505 		      u32 *next_btf_id);
1506 bool btf_struct_ids_match(struct bpf_verifier_log *log,
1507 			  const struct btf *btf, u32 id, int off,
1508 			  const struct btf *need_btf, u32 need_type_id);
1509 
1510 int btf_distill_func_proto(struct bpf_verifier_log *log,
1511 			   struct btf *btf,
1512 			   const struct btf_type *func_proto,
1513 			   const char *func_name,
1514 			   struct btf_func_model *m);
1515 
1516 struct bpf_reg_state;
1517 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
1518 			     struct bpf_reg_state *regs);
1519 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1520 			  struct bpf_reg_state *reg);
1521 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
1522 			 struct btf *btf, const struct btf_type *t);
1523 
1524 struct bpf_prog *bpf_prog_by_id(u32 id);
1525 struct bpf_link *bpf_link_by_id(u32 id);
1526 
1527 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
1528 void bpf_task_storage_free(struct task_struct *task);
1529 #else /* !CONFIG_BPF_SYSCALL */
1530 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1531 {
1532 	return ERR_PTR(-EOPNOTSUPP);
1533 }
1534 
1535 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1536 						     enum bpf_prog_type type,
1537 						     bool attach_drv)
1538 {
1539 	return ERR_PTR(-EOPNOTSUPP);
1540 }
1541 
1542 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
1543 {
1544 }
1545 
1546 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1547 {
1548 }
1549 
1550 static inline void bpf_prog_put(struct bpf_prog *prog)
1551 {
1552 }
1553 
1554 static inline void bpf_prog_inc(struct bpf_prog *prog)
1555 {
1556 }
1557 
1558 static inline struct bpf_prog *__must_check
1559 bpf_prog_inc_not_zero(struct bpf_prog *prog)
1560 {
1561 	return ERR_PTR(-EOPNOTSUPP);
1562 }
1563 
1564 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1565 				 const struct bpf_link_ops *ops,
1566 				 struct bpf_prog *prog)
1567 {
1568 }
1569 
1570 static inline int bpf_link_prime(struct bpf_link *link,
1571 				 struct bpf_link_primer *primer)
1572 {
1573 	return -EOPNOTSUPP;
1574 }
1575 
1576 static inline int bpf_link_settle(struct bpf_link_primer *primer)
1577 {
1578 	return -EOPNOTSUPP;
1579 }
1580 
1581 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
1582 {
1583 }
1584 
1585 static inline void bpf_link_inc(struct bpf_link *link)
1586 {
1587 }
1588 
1589 static inline void bpf_link_put(struct bpf_link *link)
1590 {
1591 }
1592 
1593 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
1594 {
1595 	return -EOPNOTSUPP;
1596 }
1597 
1598 static inline bool dev_map_can_have_prog(struct bpf_map *map)
1599 {
1600 	return false;
1601 }
1602 
1603 static inline void __dev_flush(void)
1604 {
1605 }
1606 
1607 struct xdp_buff;
1608 struct bpf_dtab_netdev;
1609 struct bpf_cpu_map_entry;
1610 
1611 static inline
1612 int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1613 		    struct net_device *dev_rx)
1614 {
1615 	return 0;
1616 }
1617 
1618 static inline
1619 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1620 		    struct net_device *dev_rx)
1621 {
1622 	return 0;
1623 }
1624 
1625 struct sk_buff;
1626 
1627 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
1628 					   struct sk_buff *skb,
1629 					   struct bpf_prog *xdp_prog)
1630 {
1631 	return 0;
1632 }
1633 
1634 static inline void __cpu_map_flush(void)
1635 {
1636 }
1637 
1638 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1639 				  struct xdp_buff *xdp,
1640 				  struct net_device *dev_rx)
1641 {
1642 	return 0;
1643 }
1644 
1645 static inline bool cpu_map_prog_allowed(struct bpf_map *map)
1646 {
1647 	return false;
1648 }
1649 
1650 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1651 				enum bpf_prog_type type)
1652 {
1653 	return ERR_PTR(-EOPNOTSUPP);
1654 }
1655 
1656 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1657 					const union bpf_attr *kattr,
1658 					union bpf_attr __user *uattr)
1659 {
1660 	return -ENOTSUPP;
1661 }
1662 
1663 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1664 					const union bpf_attr *kattr,
1665 					union bpf_attr __user *uattr)
1666 {
1667 	return -ENOTSUPP;
1668 }
1669 
1670 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1671 					    const union bpf_attr *kattr,
1672 					    union bpf_attr __user *uattr)
1673 {
1674 	return -ENOTSUPP;
1675 }
1676 
1677 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1678 						   const union bpf_attr *kattr,
1679 						   union bpf_attr __user *uattr)
1680 {
1681 	return -ENOTSUPP;
1682 }
1683 
1684 static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1685 					      const union bpf_attr *kattr,
1686 					      union bpf_attr __user *uattr)
1687 {
1688 	return -ENOTSUPP;
1689 }
1690 
1691 static inline void bpf_map_put(struct bpf_map *map)
1692 {
1693 }
1694 
1695 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
1696 {
1697 	return ERR_PTR(-ENOTSUPP);
1698 }
1699 
1700 static inline const struct bpf_func_proto *
1701 bpf_base_func_proto(enum bpf_func_id func_id)
1702 {
1703 	return NULL;
1704 }
1705 
1706 static inline void bpf_task_storage_free(struct task_struct *task)
1707 {
1708 }
1709 #endif /* CONFIG_BPF_SYSCALL */
1710 
1711 void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
1712 			  struct btf_mod_pair *used_btfs, u32 len);
1713 
1714 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1715 						 enum bpf_prog_type type)
1716 {
1717 	return bpf_prog_get_type_dev(ufd, type, false);
1718 }
1719 
1720 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
1721 			  struct bpf_map **used_maps, u32 len);
1722 
1723 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1724 
1725 int bpf_prog_offload_compile(struct bpf_prog *prog);
1726 void bpf_prog_offload_destroy(struct bpf_prog *prog);
1727 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1728 			       struct bpf_prog *prog);
1729 
1730 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1731 
1732 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1733 int bpf_map_offload_update_elem(struct bpf_map *map,
1734 				void *key, void *value, u64 flags);
1735 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1736 int bpf_map_offload_get_next_key(struct bpf_map *map,
1737 				 void *key, void *next_key);
1738 
1739 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1740 
1741 struct bpf_offload_dev *
1742 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1743 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1744 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1745 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1746 				    struct net_device *netdev);
1747 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1748 				       struct net_device *netdev);
1749 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1750 
1751 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1752 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1753 
1754 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1755 {
1756 	return aux->offload_requested;
1757 }
1758 
1759 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1760 {
1761 	return unlikely(map->ops == &bpf_map_offload_ops);
1762 }
1763 
1764 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1765 void bpf_map_offload_map_free(struct bpf_map *map);
1766 #else
1767 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1768 					union bpf_attr *attr)
1769 {
1770 	return -EOPNOTSUPP;
1771 }
1772 
1773 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1774 {
1775 	return false;
1776 }
1777 
1778 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1779 {
1780 	return false;
1781 }
1782 
1783 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1784 {
1785 	return ERR_PTR(-EOPNOTSUPP);
1786 }
1787 
1788 static inline void bpf_map_offload_map_free(struct bpf_map *map)
1789 {
1790 }
1791 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1792 
1793 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1794 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1795 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
1796 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
1797 void sock_map_unhash(struct sock *sk);
1798 void sock_map_close(struct sock *sk, long timeout);
1799 
1800 void bpf_sk_reuseport_detach(struct sock *sk);
1801 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1802 				       void *value);
1803 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1804 				       void *value, u64 map_flags);
1805 #else
1806 static inline void bpf_sk_reuseport_detach(struct sock *sk)
1807 {
1808 }
1809 
1810 #ifdef CONFIG_BPF_SYSCALL
1811 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1812 				       struct bpf_prog *prog)
1813 {
1814 	return -EINVAL;
1815 }
1816 
1817 static inline int sock_map_prog_detach(const union bpf_attr *attr,
1818 				       enum bpf_prog_type ptype)
1819 {
1820 	return -EOPNOTSUPP;
1821 }
1822 
1823 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
1824 					   u64 flags)
1825 {
1826 	return -EOPNOTSUPP;
1827 }
1828 
1829 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1830 						     void *key, void *value)
1831 {
1832 	return -EOPNOTSUPP;
1833 }
1834 
1835 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1836 						     void *key, void *value,
1837 						     u64 map_flags)
1838 {
1839 	return -EOPNOTSUPP;
1840 }
1841 #endif /* CONFIG_BPF_SYSCALL */
1842 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1843 
1844 /* verifier prototypes for helper functions called from eBPF programs */
1845 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1846 extern const struct bpf_func_proto bpf_map_update_elem_proto;
1847 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1848 extern const struct bpf_func_proto bpf_map_push_elem_proto;
1849 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1850 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1851 
1852 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1853 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1854 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1855 extern const struct bpf_func_proto bpf_tail_call_proto;
1856 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1857 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
1858 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1859 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1860 extern const struct bpf_func_proto bpf_get_current_comm_proto;
1861 extern const struct bpf_func_proto bpf_get_stackid_proto;
1862 extern const struct bpf_func_proto bpf_get_stack_proto;
1863 extern const struct bpf_func_proto bpf_get_task_stack_proto;
1864 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
1865 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
1866 extern const struct bpf_func_proto bpf_sock_map_update_proto;
1867 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1868 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1869 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
1870 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1871 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1872 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1873 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1874 extern const struct bpf_func_proto bpf_spin_lock_proto;
1875 extern const struct bpf_func_proto bpf_spin_unlock_proto;
1876 extern const struct bpf_func_proto bpf_get_local_storage_proto;
1877 extern const struct bpf_func_proto bpf_strtol_proto;
1878 extern const struct bpf_func_proto bpf_strtoul_proto;
1879 extern const struct bpf_func_proto bpf_tcp_sock_proto;
1880 extern const struct bpf_func_proto bpf_jiffies64_proto;
1881 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
1882 extern const struct bpf_func_proto bpf_event_output_data_proto;
1883 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
1884 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
1885 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
1886 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
1887 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
1888 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
1889 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
1890 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
1891 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
1892 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
1893 extern const struct bpf_func_proto bpf_copy_from_user_proto;
1894 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
1895 extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
1896 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
1897 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
1898 extern const struct bpf_func_proto bpf_sock_from_file_proto;
1899 extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
1900 extern const struct bpf_func_proto bpf_task_storage_get_proto;
1901 extern const struct bpf_func_proto bpf_task_storage_delete_proto;
1902 extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
1903 
1904 const struct bpf_func_proto *bpf_tracing_func_proto(
1905 	enum bpf_func_id func_id, const struct bpf_prog *prog);
1906 
1907 const struct bpf_func_proto *tracing_prog_func_proto(
1908   enum bpf_func_id func_id, const struct bpf_prog *prog);
1909 
1910 /* Shared helpers among cBPF and eBPF. */
1911 void bpf_user_rnd_init_once(void);
1912 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1913 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1914 
1915 #if defined(CONFIG_NET)
1916 bool bpf_sock_common_is_valid_access(int off, int size,
1917 				     enum bpf_access_type type,
1918 				     struct bpf_insn_access_aux *info);
1919 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1920 			      struct bpf_insn_access_aux *info);
1921 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1922 				const struct bpf_insn *si,
1923 				struct bpf_insn *insn_buf,
1924 				struct bpf_prog *prog,
1925 				u32 *target_size);
1926 #else
1927 static inline bool bpf_sock_common_is_valid_access(int off, int size,
1928 						   enum bpf_access_type type,
1929 						   struct bpf_insn_access_aux *info)
1930 {
1931 	return false;
1932 }
1933 static inline bool bpf_sock_is_valid_access(int off, int size,
1934 					    enum bpf_access_type type,
1935 					    struct bpf_insn_access_aux *info)
1936 {
1937 	return false;
1938 }
1939 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1940 					      const struct bpf_insn *si,
1941 					      struct bpf_insn *insn_buf,
1942 					      struct bpf_prog *prog,
1943 					      u32 *target_size)
1944 {
1945 	return 0;
1946 }
1947 #endif
1948 
1949 #ifdef CONFIG_INET
1950 struct sk_reuseport_kern {
1951 	struct sk_buff *skb;
1952 	struct sock *sk;
1953 	struct sock *selected_sk;
1954 	void *data_end;
1955 	u32 hash;
1956 	u32 reuseport_id;
1957 	bool bind_inany;
1958 };
1959 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1960 				  struct bpf_insn_access_aux *info);
1961 
1962 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1963 				    const struct bpf_insn *si,
1964 				    struct bpf_insn *insn_buf,
1965 				    struct bpf_prog *prog,
1966 				    u32 *target_size);
1967 
1968 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1969 				  struct bpf_insn_access_aux *info);
1970 
1971 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1972 				    const struct bpf_insn *si,
1973 				    struct bpf_insn *insn_buf,
1974 				    struct bpf_prog *prog,
1975 				    u32 *target_size);
1976 #else
1977 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1978 						enum bpf_access_type type,
1979 						struct bpf_insn_access_aux *info)
1980 {
1981 	return false;
1982 }
1983 
1984 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1985 						  const struct bpf_insn *si,
1986 						  struct bpf_insn *insn_buf,
1987 						  struct bpf_prog *prog,
1988 						  u32 *target_size)
1989 {
1990 	return 0;
1991 }
1992 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1993 						enum bpf_access_type type,
1994 						struct bpf_insn_access_aux *info)
1995 {
1996 	return false;
1997 }
1998 
1999 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2000 						  const struct bpf_insn *si,
2001 						  struct bpf_insn *insn_buf,
2002 						  struct bpf_prog *prog,
2003 						  u32 *target_size)
2004 {
2005 	return 0;
2006 }
2007 #endif /* CONFIG_INET */
2008 
2009 enum bpf_text_poke_type {
2010 	BPF_MOD_CALL,
2011 	BPF_MOD_JUMP,
2012 };
2013 
2014 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2015 		       void *addr1, void *addr2);
2016 
2017 struct btf_id_set;
2018 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
2019 
2020 #endif /* _LINUX_BPF_H */
2021