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