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