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