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