e913dd5946ae72327375a29d45973fae21a57451
[sfrench/cifs-2.6.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
9 #include <linux/workqueue.h>
10 #include <linux/file.h>
11 #include <linux/percpu.h>
12 #include <linux/err.h>
13 #include <linux/rbtree_latch.h>
14 #include <linux/numa.h>
15 #include <linux/mm_types.h>
16 #include <linux/wait.h>
17 #include <linux/u64_stats_sync.h>
18 #include <linux/refcount.h>
19 #include <linux/mutex.h>
20
21 struct bpf_verifier_env;
22 struct bpf_verifier_log;
23 struct perf_event;
24 struct bpf_prog;
25 struct bpf_map;
26 struct sock;
27 struct seq_file;
28 struct btf;
29 struct btf_type;
30 struct exception_table_entry;
31
32 extern struct idr btf_idr;
33 extern spinlock_t btf_idr_lock;
34
35 /* map is generic key/value storage optionally accesible by eBPF programs */
36 struct bpf_map_ops {
37         /* funcs callable from userspace (via syscall) */
38         int (*map_alloc_check)(union bpf_attr *attr);
39         struct bpf_map *(*map_alloc)(union bpf_attr *attr);
40         void (*map_release)(struct bpf_map *map, struct file *map_file);
41         void (*map_free)(struct bpf_map *map);
42         int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
43         void (*map_release_uref)(struct bpf_map *map);
44         void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
45
46         /* funcs callable from userspace and from eBPF programs */
47         void *(*map_lookup_elem)(struct bpf_map *map, void *key);
48         int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
49         int (*map_delete_elem)(struct bpf_map *map, void *key);
50         int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
51         int (*map_pop_elem)(struct bpf_map *map, void *value);
52         int (*map_peek_elem)(struct bpf_map *map, void *value);
53
54         /* funcs called by prog_array and perf_event_array map */
55         void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
56                                 int fd);
57         void (*map_fd_put_ptr)(void *ptr);
58         u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
59         u32 (*map_fd_sys_lookup_elem)(void *ptr);
60         void (*map_seq_show_elem)(struct bpf_map *map, void *key,
61                                   struct seq_file *m);
62         int (*map_check_btf)(const struct bpf_map *map,
63                              const struct btf *btf,
64                              const struct btf_type *key_type,
65                              const struct btf_type *value_type);
66
67         /* Direct value access helpers. */
68         int (*map_direct_value_addr)(const struct bpf_map *map,
69                                      u64 *imm, u32 off);
70         int (*map_direct_value_meta)(const struct bpf_map *map,
71                                      u64 imm, u32 *off);
72         int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
73 };
74
75 struct bpf_map_memory {
76         u32 pages;
77         struct user_struct *user;
78 };
79
80 struct bpf_map {
81         /* The first two cachelines with read-mostly members of which some
82          * are also accessed in fast-path (e.g. ops, max_entries).
83          */
84         const struct bpf_map_ops *ops ____cacheline_aligned;
85         struct bpf_map *inner_map_meta;
86 #ifdef CONFIG_SECURITY
87         void *security;
88 #endif
89         enum bpf_map_type map_type;
90         u32 key_size;
91         u32 value_size;
92         u32 max_entries;
93         u32 map_flags;
94         int spin_lock_off; /* >=0 valid offset, <0 error */
95         u32 id;
96         int numa_node;
97         u32 btf_key_type_id;
98         u32 btf_value_type_id;
99         struct btf *btf;
100         struct bpf_map_memory memory;
101         char name[BPF_OBJ_NAME_LEN];
102         bool unpriv_array;
103         bool frozen; /* write-once; write-protected by freeze_mutex */
104         /* 22 bytes hole */
105
106         /* The 3rd and 4th cacheline with misc members to avoid false sharing
107          * particularly with refcounting.
108          */
109         atomic64_t refcnt ____cacheline_aligned;
110         atomic64_t usercnt;
111         struct work_struct work;
112         struct mutex freeze_mutex;
113         u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
114 };
115
116 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
117 {
118         return map->spin_lock_off >= 0;
119 }
120
121 static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
122 {
123         if (likely(!map_value_has_spin_lock(map)))
124                 return;
125         *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
126                 (struct bpf_spin_lock){};
127 }
128
129 /* copy everything but bpf_spin_lock */
130 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
131 {
132         if (unlikely(map_value_has_spin_lock(map))) {
133                 u32 off = map->spin_lock_off;
134
135                 memcpy(dst, src, off);
136                 memcpy(dst + off + sizeof(struct bpf_spin_lock),
137                        src + off + sizeof(struct bpf_spin_lock),
138                        map->value_size - off - sizeof(struct bpf_spin_lock));
139         } else {
140                 memcpy(dst, src, map->value_size);
141         }
142 }
143 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
144                            bool lock_src);
145
146 struct bpf_offload_dev;
147 struct bpf_offloaded_map;
148
149 struct bpf_map_dev_ops {
150         int (*map_get_next_key)(struct bpf_offloaded_map *map,
151                                 void *key, void *next_key);
152         int (*map_lookup_elem)(struct bpf_offloaded_map *map,
153                                void *key, void *value);
154         int (*map_update_elem)(struct bpf_offloaded_map *map,
155                                void *key, void *value, u64 flags);
156         int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
157 };
158
159 struct bpf_offloaded_map {
160         struct bpf_map map;
161         struct net_device *netdev;
162         const struct bpf_map_dev_ops *dev_ops;
163         void *dev_priv;
164         struct list_head offloads;
165 };
166
167 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
168 {
169         return container_of(map, struct bpf_offloaded_map, map);
170 }
171
172 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
173 {
174         return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
175 }
176
177 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
178 {
179         return map->btf && map->ops->map_seq_show_elem;
180 }
181
182 int map_check_no_btf(const struct bpf_map *map,
183                      const struct btf *btf,
184                      const struct btf_type *key_type,
185                      const struct btf_type *value_type);
186
187 extern const struct bpf_map_ops bpf_map_offload_ops;
188
189 /* function argument constraints */
190 enum bpf_arg_type {
191         ARG_DONTCARE = 0,       /* unused argument in helper function */
192
193         /* the following constraints used to prototype
194          * bpf_map_lookup/update/delete_elem() functions
195          */
196         ARG_CONST_MAP_PTR,      /* const argument used as pointer to bpf_map */
197         ARG_PTR_TO_MAP_KEY,     /* pointer to stack used as map key */
198         ARG_PTR_TO_MAP_VALUE,   /* pointer to stack used as map value */
199         ARG_PTR_TO_UNINIT_MAP_VALUE,    /* pointer to valid memory used to store a map value */
200         ARG_PTR_TO_MAP_VALUE_OR_NULL,   /* pointer to stack used as map value or NULL */
201
202         /* the following constraints used to prototype bpf_memcmp() and other
203          * functions that access data on eBPF program stack
204          */
205         ARG_PTR_TO_MEM,         /* pointer to valid memory (stack, packet, map value) */
206         ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
207         ARG_PTR_TO_UNINIT_MEM,  /* pointer to memory does not need to be initialized,
208                                  * helper function must fill all bytes or clear
209                                  * them in error case.
210                                  */
211
212         ARG_CONST_SIZE,         /* number of bytes accessed from memory */
213         ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
214
215         ARG_PTR_TO_CTX,         /* pointer to context */
216         ARG_ANYTHING,           /* any (initialized) argument is ok */
217         ARG_PTR_TO_SPIN_LOCK,   /* pointer to bpf_spin_lock */
218         ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
219         ARG_PTR_TO_INT,         /* pointer to int */
220         ARG_PTR_TO_LONG,        /* pointer to long */
221         ARG_PTR_TO_SOCKET,      /* pointer to bpf_sock (fullsock) */
222         ARG_PTR_TO_BTF_ID,      /* pointer to in-kernel struct */
223 };
224
225 /* type of values returned from helper functions */
226 enum bpf_return_type {
227         RET_INTEGER,                    /* function returns integer */
228         RET_VOID,                       /* function doesn't return anything */
229         RET_PTR_TO_MAP_VALUE,           /* returns a pointer to map elem value */
230         RET_PTR_TO_MAP_VALUE_OR_NULL,   /* returns a pointer to map elem value or NULL */
231         RET_PTR_TO_SOCKET_OR_NULL,      /* returns a pointer to a socket or NULL */
232         RET_PTR_TO_TCP_SOCK_OR_NULL,    /* returns a pointer to a tcp_sock or NULL */
233         RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
234 };
235
236 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
237  * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
238  * instructions after verifying
239  */
240 struct bpf_func_proto {
241         u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
242         bool gpl_only;
243         bool pkt_access;
244         enum bpf_return_type ret_type;
245         union {
246                 struct {
247                         enum bpf_arg_type arg1_type;
248                         enum bpf_arg_type arg2_type;
249                         enum bpf_arg_type arg3_type;
250                         enum bpf_arg_type arg4_type;
251                         enum bpf_arg_type arg5_type;
252                 };
253                 enum bpf_arg_type arg_type[5];
254         };
255         int *btf_id; /* BTF ids of arguments */
256 };
257
258 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
259  * the first argument to eBPF programs.
260  * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
261  */
262 struct bpf_context;
263
264 enum bpf_access_type {
265         BPF_READ = 1,
266         BPF_WRITE = 2
267 };
268
269 /* types of values stored in eBPF registers */
270 /* Pointer types represent:
271  * pointer
272  * pointer + imm
273  * pointer + (u16) var
274  * pointer + (u16) var + imm
275  * if (range > 0) then [ptr, ptr + range - off) is safe to access
276  * if (id > 0) means that some 'var' was added
277  * if (off > 0) means that 'imm' was added
278  */
279 enum bpf_reg_type {
280         NOT_INIT = 0,            /* nothing was written into register */
281         SCALAR_VALUE,            /* reg doesn't contain a valid pointer */
282         PTR_TO_CTX,              /* reg points to bpf_context */
283         CONST_PTR_TO_MAP,        /* reg points to struct bpf_map */
284         PTR_TO_MAP_VALUE,        /* reg points to map element value */
285         PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
286         PTR_TO_STACK,            /* reg == frame_pointer + offset */
287         PTR_TO_PACKET_META,      /* skb->data - meta_len */
288         PTR_TO_PACKET,           /* reg points to skb->data */
289         PTR_TO_PACKET_END,       /* skb->data + headlen */
290         PTR_TO_FLOW_KEYS,        /* reg points to bpf_flow_keys */
291         PTR_TO_SOCKET,           /* reg points to struct bpf_sock */
292         PTR_TO_SOCKET_OR_NULL,   /* reg points to struct bpf_sock or NULL */
293         PTR_TO_SOCK_COMMON,      /* reg points to sock_common */
294         PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
295         PTR_TO_TCP_SOCK,         /* reg points to struct tcp_sock */
296         PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
297         PTR_TO_TP_BUFFER,        /* reg points to a writable raw tp's buffer */
298         PTR_TO_XDP_SOCK,         /* reg points to struct xdp_sock */
299         PTR_TO_BTF_ID,           /* reg points to kernel struct */
300 };
301
302 /* The information passed from prog-specific *_is_valid_access
303  * back to the verifier.
304  */
305 struct bpf_insn_access_aux {
306         enum bpf_reg_type reg_type;
307         union {
308                 int ctx_field_size;
309                 u32 btf_id;
310         };
311         struct bpf_verifier_log *log; /* for verbose logs */
312 };
313
314 static inline void
315 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
316 {
317         aux->ctx_field_size = size;
318 }
319
320 struct bpf_prog_ops {
321         int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
322                         union bpf_attr __user *uattr);
323 };
324
325 struct bpf_verifier_ops {
326         /* return eBPF function prototype for verification */
327         const struct bpf_func_proto *
328         (*get_func_proto)(enum bpf_func_id func_id,
329                           const struct bpf_prog *prog);
330
331         /* return true if 'size' wide access at offset 'off' within bpf_context
332          * with 'type' (read or write) is allowed
333          */
334         bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
335                                 const struct bpf_prog *prog,
336                                 struct bpf_insn_access_aux *info);
337         int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
338                             const struct bpf_prog *prog);
339         int (*gen_ld_abs)(const struct bpf_insn *orig,
340                           struct bpf_insn *insn_buf);
341         u32 (*convert_ctx_access)(enum bpf_access_type type,
342                                   const struct bpf_insn *src,
343                                   struct bpf_insn *dst,
344                                   struct bpf_prog *prog, u32 *target_size);
345 };
346
347 struct bpf_prog_offload_ops {
348         /* verifier basic callbacks */
349         int (*insn_hook)(struct bpf_verifier_env *env,
350                          int insn_idx, int prev_insn_idx);
351         int (*finalize)(struct bpf_verifier_env *env);
352         /* verifier optimization callbacks (called after .finalize) */
353         int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
354                             struct bpf_insn *insn);
355         int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
356         /* program management callbacks */
357         int (*prepare)(struct bpf_prog *prog);
358         int (*translate)(struct bpf_prog *prog);
359         void (*destroy)(struct bpf_prog *prog);
360 };
361
362 struct bpf_prog_offload {
363         struct bpf_prog         *prog;
364         struct net_device       *netdev;
365         struct bpf_offload_dev  *offdev;
366         void                    *dev_priv;
367         struct list_head        offloads;
368         bool                    dev_state;
369         bool                    opt_failed;
370         void                    *jited_image;
371         u32                     jited_len;
372 };
373
374 enum bpf_cgroup_storage_type {
375         BPF_CGROUP_STORAGE_SHARED,
376         BPF_CGROUP_STORAGE_PERCPU,
377         __BPF_CGROUP_STORAGE_MAX
378 };
379
380 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
381
382 /* The longest tracepoint has 12 args.
383  * See include/trace/bpf_probe.h
384  */
385 #define MAX_BPF_FUNC_ARGS 12
386
387 struct bpf_prog_stats {
388         u64 cnt;
389         u64 nsecs;
390         struct u64_stats_sync syncp;
391 } __aligned(2 * sizeof(u64));
392
393 struct btf_func_model {
394         u8 ret_size;
395         u8 nr_args;
396         u8 arg_size[MAX_BPF_FUNC_ARGS];
397 };
398
399 /* Restore arguments before returning from trampoline to let original function
400  * continue executing. This flag is used for fentry progs when there are no
401  * fexit progs.
402  */
403 #define BPF_TRAMP_F_RESTORE_REGS        BIT(0)
404 /* Call original function after fentry progs, but before fexit progs.
405  * Makes sense for fentry/fexit, normal calls and indirect calls.
406  */
407 #define BPF_TRAMP_F_CALL_ORIG           BIT(1)
408 /* Skip current frame and return to parent.  Makes sense for fentry/fexit
409  * programs only. Should not be used with normal calls and indirect calls.
410  */
411 #define BPF_TRAMP_F_SKIP_FRAME          BIT(2)
412
413 /* Different use cases for BPF trampoline:
414  * 1. replace nop at the function entry (kprobe equivalent)
415  *    flags = BPF_TRAMP_F_RESTORE_REGS
416  *    fentry = a set of programs to run before returning from trampoline
417  *
418  * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
419  *    flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
420  *    orig_call = fentry_ip + MCOUNT_INSN_SIZE
421  *    fentry = a set of program to run before calling original function
422  *    fexit = a set of program to run after original function
423  *
424  * 3. replace direct call instruction anywhere in the function body
425  *    or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
426  *    With flags = 0
427  *      fentry = a set of programs to run before returning from trampoline
428  *    With flags = BPF_TRAMP_F_CALL_ORIG
429  *      orig_call = original callback addr or direct function addr
430  *      fentry = a set of program to run before calling original function
431  *      fexit = a set of program to run after original function
432  */
433 int arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags,
434                                 struct bpf_prog **fentry_progs, int fentry_cnt,
435                                 struct bpf_prog **fexit_progs, int fexit_cnt,
436                                 void *orig_call);
437 /* these two functions are called from generated trampoline */
438 u64 notrace __bpf_prog_enter(void);
439 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
440
441 enum bpf_tramp_prog_type {
442         BPF_TRAMP_FENTRY,
443         BPF_TRAMP_FEXIT,
444         BPF_TRAMP_MAX
445 };
446
447 struct bpf_trampoline {
448         /* hlist for trampoline_table */
449         struct hlist_node hlist;
450         /* serializes access to fields of this trampoline */
451         struct mutex mutex;
452         refcount_t refcnt;
453         u64 key;
454         struct {
455                 struct btf_func_model model;
456                 void *addr;
457         } func;
458         /* list of BPF programs using this trampoline */
459         struct hlist_head progs_hlist[BPF_TRAMP_MAX];
460         /* Number of attached programs. A counter per kind. */
461         int progs_cnt[BPF_TRAMP_MAX];
462         /* Executable image of trampoline */
463         void *image;
464         u64 selector;
465 };
466 #ifdef CONFIG_BPF_JIT
467 struct bpf_trampoline *bpf_trampoline_lookup(u64 key);
468 int bpf_trampoline_link_prog(struct bpf_prog *prog);
469 int bpf_trampoline_unlink_prog(struct bpf_prog *prog);
470 void bpf_trampoline_put(struct bpf_trampoline *tr);
471 #else
472 static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
473 {
474         return NULL;
475 }
476 static inline int bpf_trampoline_link_prog(struct bpf_prog *prog)
477 {
478         return -ENOTSUPP;
479 }
480 static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
481 {
482         return -ENOTSUPP;
483 }
484 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
485 #endif
486
487 struct bpf_func_info_aux {
488         bool unreliable;
489 };
490
491 struct bpf_prog_aux {
492         atomic64_t refcnt;
493         u32 used_map_cnt;
494         u32 max_ctx_offset;
495         u32 max_pkt_offset;
496         u32 max_tp_access;
497         u32 stack_depth;
498         u32 id;
499         u32 func_cnt; /* used by non-func prog as the number of func progs */
500         u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
501         u32 attach_btf_id; /* in-kernel BTF type id to attach to */
502         struct bpf_prog *linked_prog;
503         bool verifier_zext; /* Zero extensions has been inserted by verifier. */
504         bool offload_requested;
505         bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
506         bool func_proto_unreliable;
507         enum bpf_tramp_prog_type trampoline_prog_type;
508         struct bpf_trampoline *trampoline;
509         struct hlist_node tramp_hlist;
510         /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
511         const struct btf_type *attach_func_proto;
512         /* function name for valid attach_btf_id */
513         const char *attach_func_name;
514         struct bpf_prog **func;
515         void *jit_data; /* JIT specific data. arch dependent */
516         struct latch_tree_node ksym_tnode;
517         struct list_head ksym_lnode;
518         const struct bpf_prog_ops *ops;
519         struct bpf_map **used_maps;
520         struct bpf_prog *prog;
521         struct user_struct *user;
522         u64 load_time; /* ns since boottime */
523         struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
524         char name[BPF_OBJ_NAME_LEN];
525 #ifdef CONFIG_SECURITY
526         void *security;
527 #endif
528         struct bpf_prog_offload *offload;
529         struct btf *btf;
530         struct bpf_func_info *func_info;
531         struct bpf_func_info_aux *func_info_aux;
532         /* bpf_line_info loaded from userspace.  linfo->insn_off
533          * has the xlated insn offset.
534          * Both the main and sub prog share the same linfo.
535          * The subprog can access its first linfo by
536          * using the linfo_idx.
537          */
538         struct bpf_line_info *linfo;
539         /* jited_linfo is the jited addr of the linfo.  It has a
540          * one to one mapping to linfo:
541          * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
542          * Both the main and sub prog share the same jited_linfo.
543          * The subprog can access its first jited_linfo by
544          * using the linfo_idx.
545          */
546         void **jited_linfo;
547         u32 func_info_cnt;
548         u32 nr_linfo;
549         /* subprog can use linfo_idx to access its first linfo and
550          * jited_linfo.
551          * main prog always has linfo_idx == 0
552          */
553         u32 linfo_idx;
554         u32 num_exentries;
555         struct exception_table_entry *extable;
556         struct bpf_prog_stats __percpu *stats;
557         union {
558                 struct work_struct work;
559                 struct rcu_head rcu;
560         };
561 };
562
563 struct bpf_array {
564         struct bpf_map map;
565         u32 elem_size;
566         u32 index_mask;
567         /* 'ownership' of prog_array is claimed by the first program that
568          * is going to use this map or by the first program which FD is stored
569          * in the map to make sure that all callers and callees have the same
570          * prog_type and JITed flag
571          */
572         enum bpf_prog_type owner_prog_type;
573         bool owner_jited;
574         union {
575                 char value[0] __aligned(8);
576                 void *ptrs[0] __aligned(8);
577                 void __percpu *pptrs[0] __aligned(8);
578         };
579 };
580
581 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
582 #define MAX_TAIL_CALL_CNT 32
583
584 #define BPF_F_ACCESS_MASK       (BPF_F_RDONLY |         \
585                                  BPF_F_RDONLY_PROG |    \
586                                  BPF_F_WRONLY |         \
587                                  BPF_F_WRONLY_PROG)
588
589 #define BPF_MAP_CAN_READ        BIT(0)
590 #define BPF_MAP_CAN_WRITE       BIT(1)
591
592 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
593 {
594         u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
595
596         /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
597          * not possible.
598          */
599         if (access_flags & BPF_F_RDONLY_PROG)
600                 return BPF_MAP_CAN_READ;
601         else if (access_flags & BPF_F_WRONLY_PROG)
602                 return BPF_MAP_CAN_WRITE;
603         else
604                 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
605 }
606
607 static inline bool bpf_map_flags_access_ok(u32 access_flags)
608 {
609         return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
610                (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
611 }
612
613 struct bpf_event_entry {
614         struct perf_event *event;
615         struct file *perf_file;
616         struct file *map_file;
617         struct rcu_head rcu;
618 };
619
620 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
621 int bpf_prog_calc_tag(struct bpf_prog *fp);
622 const char *kernel_type_name(u32 btf_type_id);
623
624 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
625
626 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
627                                         unsigned long off, unsigned long len);
628 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
629                                         const struct bpf_insn *src,
630                                         struct bpf_insn *dst,
631                                         struct bpf_prog *prog,
632                                         u32 *target_size);
633
634 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
635                      void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
636
637 /* an array of programs to be executed under rcu_lock.
638  *
639  * Typical usage:
640  * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
641  *
642  * the structure returned by bpf_prog_array_alloc() should be populated
643  * with program pointers and the last pointer must be NULL.
644  * The user has to keep refcnt on the program and make sure the program
645  * is removed from the array before bpf_prog_put().
646  * The 'struct bpf_prog_array *' should only be replaced with xchg()
647  * since other cpus are walking the array of pointers in parallel.
648  */
649 struct bpf_prog_array_item {
650         struct bpf_prog *prog;
651         struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
652 };
653
654 struct bpf_prog_array {
655         struct rcu_head rcu;
656         struct bpf_prog_array_item items[0];
657 };
658
659 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
660 void bpf_prog_array_free(struct bpf_prog_array *progs);
661 int bpf_prog_array_length(struct bpf_prog_array *progs);
662 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
663 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
664                                 __u32 __user *prog_ids, u32 cnt);
665
666 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
667                                 struct bpf_prog *old_prog);
668 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
669                              u32 *prog_ids, u32 request_cnt,
670                              u32 *prog_cnt);
671 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
672                         struct bpf_prog *exclude_prog,
673                         struct bpf_prog *include_prog,
674                         struct bpf_prog_array **new_array);
675
676 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null)  \
677         ({                                              \
678                 struct bpf_prog_array_item *_item;      \
679                 struct bpf_prog *_prog;                 \
680                 struct bpf_prog_array *_array;          \
681                 u32 _ret = 1;                           \
682                 preempt_disable();                      \
683                 rcu_read_lock();                        \
684                 _array = rcu_dereference(array);        \
685                 if (unlikely(check_non_null && !_array))\
686                         goto _out;                      \
687                 _item = &_array->items[0];              \
688                 while ((_prog = READ_ONCE(_item->prog))) {              \
689                         bpf_cgroup_storage_set(_item->cgroup_storage);  \
690                         _ret &= func(_prog, ctx);       \
691                         _item++;                        \
692                 }                                       \
693 _out:                                                   \
694                 rcu_read_unlock();                      \
695                 preempt_enable();                       \
696                 _ret;                                   \
697          })
698
699 /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
700  * so BPF programs can request cwr for TCP packets.
701  *
702  * Current cgroup skb programs can only return 0 or 1 (0 to drop the
703  * packet. This macro changes the behavior so the low order bit
704  * indicates whether the packet should be dropped (0) or not (1)
705  * and the next bit is a congestion notification bit. This could be
706  * used by TCP to call tcp_enter_cwr()
707  *
708  * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
709  *   0: drop packet
710  *   1: keep packet
711  *   2: drop packet and cn
712  *   3: keep packet and cn
713  *
714  * This macro then converts it to one of the NET_XMIT or an error
715  * code that is then interpreted as drop packet (and no cn):
716  *   0: NET_XMIT_SUCCESS  skb should be transmitted
717  *   1: NET_XMIT_DROP     skb should be dropped and cn
718  *   2: NET_XMIT_CN       skb should be transmitted and cn
719  *   3: -EPERM            skb should be dropped
720  */
721 #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func)         \
722         ({                                              \
723                 struct bpf_prog_array_item *_item;      \
724                 struct bpf_prog *_prog;                 \
725                 struct bpf_prog_array *_array;          \
726                 u32 ret;                                \
727                 u32 _ret = 1;                           \
728                 u32 _cn = 0;                            \
729                 preempt_disable();                      \
730                 rcu_read_lock();                        \
731                 _array = rcu_dereference(array);        \
732                 _item = &_array->items[0];              \
733                 while ((_prog = READ_ONCE(_item->prog))) {              \
734                         bpf_cgroup_storage_set(_item->cgroup_storage);  \
735                         ret = func(_prog, ctx);         \
736                         _ret &= (ret & 1);              \
737                         _cn |= (ret & 2);               \
738                         _item++;                        \
739                 }                                       \
740                 rcu_read_unlock();                      \
741                 preempt_enable();                       \
742                 if (_ret)                               \
743                         _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS);  \
744                 else                                    \
745                         _ret = (_cn ? NET_XMIT_DROP : -EPERM);          \
746                 _ret;                                   \
747         })
748
749 #define BPF_PROG_RUN_ARRAY(array, ctx, func)            \
750         __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
751
752 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func)      \
753         __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
754
755 #ifdef CONFIG_BPF_SYSCALL
756 DECLARE_PER_CPU(int, bpf_prog_active);
757
758 extern const struct file_operations bpf_map_fops;
759 extern const struct file_operations bpf_prog_fops;
760
761 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
762         extern const struct bpf_prog_ops _name ## _prog_ops; \
763         extern const struct bpf_verifier_ops _name ## _verifier_ops;
764 #define BPF_MAP_TYPE(_id, _ops) \
765         extern const struct bpf_map_ops _ops;
766 #include <linux/bpf_types.h>
767 #undef BPF_PROG_TYPE
768 #undef BPF_MAP_TYPE
769
770 extern const struct bpf_prog_ops bpf_offload_prog_ops;
771 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
772 extern const struct bpf_verifier_ops xdp_analyzer_ops;
773
774 struct bpf_prog *bpf_prog_get(u32 ufd);
775 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
776                                        bool attach_drv);
777 void bpf_prog_add(struct bpf_prog *prog, int i);
778 void bpf_prog_sub(struct bpf_prog *prog, int i);
779 void bpf_prog_inc(struct bpf_prog *prog);
780 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
781 void bpf_prog_put(struct bpf_prog *prog);
782 int __bpf_prog_charge(struct user_struct *user, u32 pages);
783 void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
784
785 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
786 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
787
788 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
789 struct bpf_map *__bpf_map_get(struct fd f);
790 void bpf_map_inc(struct bpf_map *map);
791 void bpf_map_inc_with_uref(struct bpf_map *map);
792 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
793 void bpf_map_put_with_uref(struct bpf_map *map);
794 void bpf_map_put(struct bpf_map *map);
795 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
796 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
797 int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size);
798 void bpf_map_charge_finish(struct bpf_map_memory *mem);
799 void bpf_map_charge_move(struct bpf_map_memory *dst,
800                          struct bpf_map_memory *src);
801 void *bpf_map_area_alloc(size_t size, int numa_node);
802 void *bpf_map_area_mmapable_alloc(size_t size, int numa_node);
803 void bpf_map_area_free(void *base);
804 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
805
806 extern int sysctl_unprivileged_bpf_disabled;
807
808 int bpf_map_new_fd(struct bpf_map *map, int flags);
809 int bpf_prog_new_fd(struct bpf_prog *prog);
810
811 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
812 int bpf_obj_get_user(const char __user *pathname, int flags);
813
814 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
815 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
816 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
817                            u64 flags);
818 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
819                             u64 flags);
820
821 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
822
823 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
824                                  void *key, void *value, u64 map_flags);
825 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
826 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
827                                 void *key, void *value, u64 map_flags);
828 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
829
830 int bpf_get_file_flag(int flags);
831 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
832                              size_t actual_size);
833
834 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
835  * forced to use 'long' read/writes to try to atomically copy long counters.
836  * Best-effort only.  No barriers here, since it _will_ race with concurrent
837  * updates from BPF programs. Called from bpf syscall and mostly used with
838  * size 8 or 16 bytes, so ask compiler to inline it.
839  */
840 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
841 {
842         const long *lsrc = src;
843         long *ldst = dst;
844
845         size /= sizeof(long);
846         while (size--)
847                 *ldst++ = *lsrc++;
848 }
849
850 /* verify correctness of eBPF program */
851 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
852               union bpf_attr __user *uattr);
853 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
854
855 /* Map specifics */
856 struct xdp_buff;
857 struct sk_buff;
858
859 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
860 struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
861 void __dev_map_flush(struct bpf_map *map);
862 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
863                     struct net_device *dev_rx);
864 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
865                              struct bpf_prog *xdp_prog);
866
867 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
868 void __cpu_map_flush(struct bpf_map *map);
869 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
870                     struct net_device *dev_rx);
871
872 /* Return map's numa specified by userspace */
873 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
874 {
875         return (attr->map_flags & BPF_F_NUMA_NODE) ?
876                 attr->numa_node : NUMA_NO_NODE;
877 }
878
879 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
880 int array_map_alloc_check(union bpf_attr *attr);
881
882 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
883                           union bpf_attr __user *uattr);
884 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
885                           union bpf_attr __user *uattr);
886 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
887                                      const union bpf_attr *kattr,
888                                      union bpf_attr __user *uattr);
889 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
890                     const struct bpf_prog *prog,
891                     struct bpf_insn_access_aux *info);
892 int btf_struct_access(struct bpf_verifier_log *log,
893                       const struct btf_type *t, int off, int size,
894                       enum bpf_access_type atype,
895                       u32 *next_btf_id);
896 int btf_resolve_helper_id(struct bpf_verifier_log *log,
897                           const struct bpf_func_proto *fn, int);
898
899 int btf_distill_func_proto(struct bpf_verifier_log *log,
900                            struct btf *btf,
901                            const struct btf_type *func_proto,
902                            const char *func_name,
903                            struct btf_func_model *m);
904
905 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog);
906
907 #else /* !CONFIG_BPF_SYSCALL */
908 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
909 {
910         return ERR_PTR(-EOPNOTSUPP);
911 }
912
913 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
914                                                      enum bpf_prog_type type,
915                                                      bool attach_drv)
916 {
917         return ERR_PTR(-EOPNOTSUPP);
918 }
919
920 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
921 {
922 }
923
924 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
925 {
926 }
927
928 static inline void bpf_prog_put(struct bpf_prog *prog)
929 {
930 }
931
932 static inline void bpf_prog_inc(struct bpf_prog *prog)
933 {
934 }
935
936 static inline struct bpf_prog *__must_check
937 bpf_prog_inc_not_zero(struct bpf_prog *prog)
938 {
939         return ERR_PTR(-EOPNOTSUPP);
940 }
941
942 static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
943 {
944         return 0;
945 }
946
947 static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
948 {
949 }
950
951 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
952 {
953         return -EOPNOTSUPP;
954 }
955
956 static inline struct net_device  *__dev_map_lookup_elem(struct bpf_map *map,
957                                                        u32 key)
958 {
959         return NULL;
960 }
961
962 static inline struct net_device  *__dev_map_hash_lookup_elem(struct bpf_map *map,
963                                                              u32 key)
964 {
965         return NULL;
966 }
967
968 static inline void __dev_map_flush(struct bpf_map *map)
969 {
970 }
971
972 struct xdp_buff;
973 struct bpf_dtab_netdev;
974
975 static inline
976 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
977                     struct net_device *dev_rx)
978 {
979         return 0;
980 }
981
982 struct sk_buff;
983
984 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
985                                            struct sk_buff *skb,
986                                            struct bpf_prog *xdp_prog)
987 {
988         return 0;
989 }
990
991 static inline
992 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
993 {
994         return NULL;
995 }
996
997 static inline void __cpu_map_flush(struct bpf_map *map)
998 {
999 }
1000
1001 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1002                                   struct xdp_buff *xdp,
1003                                   struct net_device *dev_rx)
1004 {
1005         return 0;
1006 }
1007
1008 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1009                                 enum bpf_prog_type type)
1010 {
1011         return ERR_PTR(-EOPNOTSUPP);
1012 }
1013
1014 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1015                                         const union bpf_attr *kattr,
1016                                         union bpf_attr __user *uattr)
1017 {
1018         return -ENOTSUPP;
1019 }
1020
1021 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1022                                         const union bpf_attr *kattr,
1023                                         union bpf_attr __user *uattr)
1024 {
1025         return -ENOTSUPP;
1026 }
1027
1028 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1029                                                    const union bpf_attr *kattr,
1030                                                    union bpf_attr __user *uattr)
1031 {
1032         return -ENOTSUPP;
1033 }
1034 #endif /* CONFIG_BPF_SYSCALL */
1035
1036 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1037                                                  enum bpf_prog_type type)
1038 {
1039         return bpf_prog_get_type_dev(ufd, type, false);
1040 }
1041
1042 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1043
1044 int bpf_prog_offload_compile(struct bpf_prog *prog);
1045 void bpf_prog_offload_destroy(struct bpf_prog *prog);
1046 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1047                                struct bpf_prog *prog);
1048
1049 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1050
1051 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1052 int bpf_map_offload_update_elem(struct bpf_map *map,
1053                                 void *key, void *value, u64 flags);
1054 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1055 int bpf_map_offload_get_next_key(struct bpf_map *map,
1056                                  void *key, void *next_key);
1057
1058 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1059
1060 struct bpf_offload_dev *
1061 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1062 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1063 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1064 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1065                                     struct net_device *netdev);
1066 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1067                                        struct net_device *netdev);
1068 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1069
1070 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1071 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1072
1073 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1074 {
1075         return aux->offload_requested;
1076 }
1077
1078 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1079 {
1080         return unlikely(map->ops == &bpf_map_offload_ops);
1081 }
1082
1083 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1084 void bpf_map_offload_map_free(struct bpf_map *map);
1085 #else
1086 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1087                                         union bpf_attr *attr)
1088 {
1089         return -EOPNOTSUPP;
1090 }
1091
1092 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1093 {
1094         return false;
1095 }
1096
1097 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1098 {
1099         return false;
1100 }
1101
1102 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1103 {
1104         return ERR_PTR(-EOPNOTSUPP);
1105 }
1106
1107 static inline void bpf_map_offload_map_free(struct bpf_map *map)
1108 {
1109 }
1110 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1111
1112 #if defined(CONFIG_BPF_STREAM_PARSER)
1113 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
1114 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1115 #else
1116 static inline int sock_map_prog_update(struct bpf_map *map,
1117                                        struct bpf_prog *prog, u32 which)
1118 {
1119         return -EOPNOTSUPP;
1120 }
1121
1122 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1123                                        struct bpf_prog *prog)
1124 {
1125         return -EINVAL;
1126 }
1127 #endif
1128
1129 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1130 void bpf_sk_reuseport_detach(struct sock *sk);
1131 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1132                                        void *value);
1133 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1134                                        void *value, u64 map_flags);
1135 #else
1136 static inline void bpf_sk_reuseport_detach(struct sock *sk)
1137 {
1138 }
1139
1140 #ifdef CONFIG_BPF_SYSCALL
1141 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1142                                                      void *key, void *value)
1143 {
1144         return -EOPNOTSUPP;
1145 }
1146
1147 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1148                                                      void *key, void *value,
1149                                                      u64 map_flags)
1150 {
1151         return -EOPNOTSUPP;
1152 }
1153 #endif /* CONFIG_BPF_SYSCALL */
1154 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1155
1156 /* verifier prototypes for helper functions called from eBPF programs */
1157 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1158 extern const struct bpf_func_proto bpf_map_update_elem_proto;
1159 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1160 extern const struct bpf_func_proto bpf_map_push_elem_proto;
1161 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1162 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1163
1164 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1165 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1166 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1167 extern const struct bpf_func_proto bpf_tail_call_proto;
1168 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1169 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1170 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1171 extern const struct bpf_func_proto bpf_get_current_comm_proto;
1172 extern const struct bpf_func_proto bpf_get_stackid_proto;
1173 extern const struct bpf_func_proto bpf_get_stack_proto;
1174 extern const struct bpf_func_proto bpf_sock_map_update_proto;
1175 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1176 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1177 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1178 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1179 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1180 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1181 extern const struct bpf_func_proto bpf_spin_lock_proto;
1182 extern const struct bpf_func_proto bpf_spin_unlock_proto;
1183 extern const struct bpf_func_proto bpf_get_local_storage_proto;
1184 extern const struct bpf_func_proto bpf_strtol_proto;
1185 extern const struct bpf_func_proto bpf_strtoul_proto;
1186 extern const struct bpf_func_proto bpf_tcp_sock_proto;
1187
1188 /* Shared helpers among cBPF and eBPF. */
1189 void bpf_user_rnd_init_once(void);
1190 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1191
1192 #if defined(CONFIG_NET)
1193 bool bpf_sock_common_is_valid_access(int off, int size,
1194                                      enum bpf_access_type type,
1195                                      struct bpf_insn_access_aux *info);
1196 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1197                               struct bpf_insn_access_aux *info);
1198 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1199                                 const struct bpf_insn *si,
1200                                 struct bpf_insn *insn_buf,
1201                                 struct bpf_prog *prog,
1202                                 u32 *target_size);
1203 #else
1204 static inline bool bpf_sock_common_is_valid_access(int off, int size,
1205                                                    enum bpf_access_type type,
1206                                                    struct bpf_insn_access_aux *info)
1207 {
1208         return false;
1209 }
1210 static inline bool bpf_sock_is_valid_access(int off, int size,
1211                                             enum bpf_access_type type,
1212                                             struct bpf_insn_access_aux *info)
1213 {
1214         return false;
1215 }
1216 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1217                                               const struct bpf_insn *si,
1218                                               struct bpf_insn *insn_buf,
1219                                               struct bpf_prog *prog,
1220                                               u32 *target_size)
1221 {
1222         return 0;
1223 }
1224 #endif
1225
1226 #ifdef CONFIG_INET
1227 struct sk_reuseport_kern {
1228         struct sk_buff *skb;
1229         struct sock *sk;
1230         struct sock *selected_sk;
1231         void *data_end;
1232         u32 hash;
1233         u32 reuseport_id;
1234         bool bind_inany;
1235 };
1236 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1237                                   struct bpf_insn_access_aux *info);
1238
1239 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1240                                     const struct bpf_insn *si,
1241                                     struct bpf_insn *insn_buf,
1242                                     struct bpf_prog *prog,
1243                                     u32 *target_size);
1244
1245 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1246                                   struct bpf_insn_access_aux *info);
1247
1248 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1249                                     const struct bpf_insn *si,
1250                                     struct bpf_insn *insn_buf,
1251                                     struct bpf_prog *prog,
1252                                     u32 *target_size);
1253 #else
1254 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1255                                                 enum bpf_access_type type,
1256                                                 struct bpf_insn_access_aux *info)
1257 {
1258         return false;
1259 }
1260
1261 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1262                                                   const struct bpf_insn *si,
1263                                                   struct bpf_insn *insn_buf,
1264                                                   struct bpf_prog *prog,
1265                                                   u32 *target_size)
1266 {
1267         return 0;
1268 }
1269 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1270                                                 enum bpf_access_type type,
1271                                                 struct bpf_insn_access_aux *info)
1272 {
1273         return false;
1274 }
1275
1276 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1277                                                   const struct bpf_insn *si,
1278                                                   struct bpf_insn *insn_buf,
1279                                                   struct bpf_prog *prog,
1280                                                   u32 *target_size)
1281 {
1282         return 0;
1283 }
1284 #endif /* CONFIG_INET */
1285
1286 enum bpf_text_poke_type {
1287         BPF_MOD_NOP_TO_CALL,
1288         BPF_MOD_CALL_TO_CALL,
1289         BPF_MOD_CALL_TO_NOP,
1290 };
1291 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1292                        void *addr1, void *addr2);
1293
1294 #endif /* _LINUX_BPF_H */