]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/bpf.h
bpf: Move owner type, jited info into array auxiliary data
[linux.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_aux {
564         /* 'Ownership' of prog array is claimed by the first program that
565          * is going to use this map or by the first program which FD is
566          * stored in the map to make sure that all callers and callees have
567          * the same prog type and JITed flag.
568          */
569         enum bpf_prog_type type;
570         bool jited;
571 };
572
573 struct bpf_array {
574         struct bpf_map map;
575         u32 elem_size;
576         u32 index_mask;
577         struct bpf_array_aux *aux;
578         union {
579                 char value[0] __aligned(8);
580                 void *ptrs[0] __aligned(8);
581                 void __percpu *pptrs[0] __aligned(8);
582         };
583 };
584
585 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
586 #define MAX_TAIL_CALL_CNT 32
587
588 #define BPF_F_ACCESS_MASK       (BPF_F_RDONLY |         \
589                                  BPF_F_RDONLY_PROG |    \
590                                  BPF_F_WRONLY |         \
591                                  BPF_F_WRONLY_PROG)
592
593 #define BPF_MAP_CAN_READ        BIT(0)
594 #define BPF_MAP_CAN_WRITE       BIT(1)
595
596 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
597 {
598         u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
599
600         /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
601          * not possible.
602          */
603         if (access_flags & BPF_F_RDONLY_PROG)
604                 return BPF_MAP_CAN_READ;
605         else if (access_flags & BPF_F_WRONLY_PROG)
606                 return BPF_MAP_CAN_WRITE;
607         else
608                 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
609 }
610
611 static inline bool bpf_map_flags_access_ok(u32 access_flags)
612 {
613         return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
614                (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
615 }
616
617 struct bpf_event_entry {
618         struct perf_event *event;
619         struct file *perf_file;
620         struct file *map_file;
621         struct rcu_head rcu;
622 };
623
624 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
625 int bpf_prog_calc_tag(struct bpf_prog *fp);
626 const char *kernel_type_name(u32 btf_type_id);
627
628 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
629
630 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
631                                         unsigned long off, unsigned long len);
632 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
633                                         const struct bpf_insn *src,
634                                         struct bpf_insn *dst,
635                                         struct bpf_prog *prog,
636                                         u32 *target_size);
637
638 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
639                      void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
640
641 /* an array of programs to be executed under rcu_lock.
642  *
643  * Typical usage:
644  * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
645  *
646  * the structure returned by bpf_prog_array_alloc() should be populated
647  * with program pointers and the last pointer must be NULL.
648  * The user has to keep refcnt on the program and make sure the program
649  * is removed from the array before bpf_prog_put().
650  * The 'struct bpf_prog_array *' should only be replaced with xchg()
651  * since other cpus are walking the array of pointers in parallel.
652  */
653 struct bpf_prog_array_item {
654         struct bpf_prog *prog;
655         struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
656 };
657
658 struct bpf_prog_array {
659         struct rcu_head rcu;
660         struct bpf_prog_array_item items[0];
661 };
662
663 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
664 void bpf_prog_array_free(struct bpf_prog_array *progs);
665 int bpf_prog_array_length(struct bpf_prog_array *progs);
666 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
667 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
668                                 __u32 __user *prog_ids, u32 cnt);
669
670 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
671                                 struct bpf_prog *old_prog);
672 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
673                              u32 *prog_ids, u32 request_cnt,
674                              u32 *prog_cnt);
675 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
676                         struct bpf_prog *exclude_prog,
677                         struct bpf_prog *include_prog,
678                         struct bpf_prog_array **new_array);
679
680 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null)  \
681         ({                                              \
682                 struct bpf_prog_array_item *_item;      \
683                 struct bpf_prog *_prog;                 \
684                 struct bpf_prog_array *_array;          \
685                 u32 _ret = 1;                           \
686                 preempt_disable();                      \
687                 rcu_read_lock();                        \
688                 _array = rcu_dereference(array);        \
689                 if (unlikely(check_non_null && !_array))\
690                         goto _out;                      \
691                 _item = &_array->items[0];              \
692                 while ((_prog = READ_ONCE(_item->prog))) {              \
693                         bpf_cgroup_storage_set(_item->cgroup_storage);  \
694                         _ret &= func(_prog, ctx);       \
695                         _item++;                        \
696                 }                                       \
697 _out:                                                   \
698                 rcu_read_unlock();                      \
699                 preempt_enable();                       \
700                 _ret;                                   \
701          })
702
703 /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
704  * so BPF programs can request cwr for TCP packets.
705  *
706  * Current cgroup skb programs can only return 0 or 1 (0 to drop the
707  * packet. This macro changes the behavior so the low order bit
708  * indicates whether the packet should be dropped (0) or not (1)
709  * and the next bit is a congestion notification bit. This could be
710  * used by TCP to call tcp_enter_cwr()
711  *
712  * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
713  *   0: drop packet
714  *   1: keep packet
715  *   2: drop packet and cn
716  *   3: keep packet and cn
717  *
718  * This macro then converts it to one of the NET_XMIT or an error
719  * code that is then interpreted as drop packet (and no cn):
720  *   0: NET_XMIT_SUCCESS  skb should be transmitted
721  *   1: NET_XMIT_DROP     skb should be dropped and cn
722  *   2: NET_XMIT_CN       skb should be transmitted and cn
723  *   3: -EPERM            skb should be dropped
724  */
725 #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func)         \
726         ({                                              \
727                 struct bpf_prog_array_item *_item;      \
728                 struct bpf_prog *_prog;                 \
729                 struct bpf_prog_array *_array;          \
730                 u32 ret;                                \
731                 u32 _ret = 1;                           \
732                 u32 _cn = 0;                            \
733                 preempt_disable();                      \
734                 rcu_read_lock();                        \
735                 _array = rcu_dereference(array);        \
736                 _item = &_array->items[0];              \
737                 while ((_prog = READ_ONCE(_item->prog))) {              \
738                         bpf_cgroup_storage_set(_item->cgroup_storage);  \
739                         ret = func(_prog, ctx);         \
740                         _ret &= (ret & 1);              \
741                         _cn |= (ret & 2);               \
742                         _item++;                        \
743                 }                                       \
744                 rcu_read_unlock();                      \
745                 preempt_enable();                       \
746                 if (_ret)                               \
747                         _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS);  \
748                 else                                    \
749                         _ret = (_cn ? NET_XMIT_DROP : -EPERM);          \
750                 _ret;                                   \
751         })
752
753 #define BPF_PROG_RUN_ARRAY(array, ctx, func)            \
754         __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
755
756 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func)      \
757         __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
758
759 #ifdef CONFIG_BPF_SYSCALL
760 DECLARE_PER_CPU(int, bpf_prog_active);
761
762 extern const struct file_operations bpf_map_fops;
763 extern const struct file_operations bpf_prog_fops;
764
765 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
766         extern const struct bpf_prog_ops _name ## _prog_ops; \
767         extern const struct bpf_verifier_ops _name ## _verifier_ops;
768 #define BPF_MAP_TYPE(_id, _ops) \
769         extern const struct bpf_map_ops _ops;
770 #include <linux/bpf_types.h>
771 #undef BPF_PROG_TYPE
772 #undef BPF_MAP_TYPE
773
774 extern const struct bpf_prog_ops bpf_offload_prog_ops;
775 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
776 extern const struct bpf_verifier_ops xdp_analyzer_ops;
777
778 struct bpf_prog *bpf_prog_get(u32 ufd);
779 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
780                                        bool attach_drv);
781 void bpf_prog_add(struct bpf_prog *prog, int i);
782 void bpf_prog_sub(struct bpf_prog *prog, int i);
783 void bpf_prog_inc(struct bpf_prog *prog);
784 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
785 void bpf_prog_put(struct bpf_prog *prog);
786 int __bpf_prog_charge(struct user_struct *user, u32 pages);
787 void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
788
789 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
790 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
791
792 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
793 struct bpf_map *__bpf_map_get(struct fd f);
794 void bpf_map_inc(struct bpf_map *map);
795 void bpf_map_inc_with_uref(struct bpf_map *map);
796 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
797 void bpf_map_put_with_uref(struct bpf_map *map);
798 void bpf_map_put(struct bpf_map *map);
799 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
800 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
801 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
802 void bpf_map_charge_finish(struct bpf_map_memory *mem);
803 void bpf_map_charge_move(struct bpf_map_memory *dst,
804                          struct bpf_map_memory *src);
805 void *bpf_map_area_alloc(u64 size, int numa_node);
806 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
807 void bpf_map_area_free(void *base);
808 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
809
810 extern int sysctl_unprivileged_bpf_disabled;
811
812 int bpf_map_new_fd(struct bpf_map *map, int flags);
813 int bpf_prog_new_fd(struct bpf_prog *prog);
814
815 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
816 int bpf_obj_get_user(const char __user *pathname, int flags);
817
818 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
819 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
820 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
821                            u64 flags);
822 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
823                             u64 flags);
824
825 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
826
827 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
828                                  void *key, void *value, u64 map_flags);
829 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
830 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
831                                 void *key, void *value, u64 map_flags);
832 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
833
834 int bpf_get_file_flag(int flags);
835 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
836                              size_t actual_size);
837
838 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
839  * forced to use 'long' read/writes to try to atomically copy long counters.
840  * Best-effort only.  No barriers here, since it _will_ race with concurrent
841  * updates from BPF programs. Called from bpf syscall and mostly used with
842  * size 8 or 16 bytes, so ask compiler to inline it.
843  */
844 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
845 {
846         const long *lsrc = src;
847         long *ldst = dst;
848
849         size /= sizeof(long);
850         while (size--)
851                 *ldst++ = *lsrc++;
852 }
853
854 /* verify correctness of eBPF program */
855 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
856               union bpf_attr __user *uattr);
857 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
858
859 /* Map specifics */
860 struct xdp_buff;
861 struct sk_buff;
862
863 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
864 struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
865 void __dev_map_flush(struct bpf_map *map);
866 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
867                     struct net_device *dev_rx);
868 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
869                              struct bpf_prog *xdp_prog);
870
871 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
872 void __cpu_map_flush(struct bpf_map *map);
873 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
874                     struct net_device *dev_rx);
875
876 /* Return map's numa specified by userspace */
877 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
878 {
879         return (attr->map_flags & BPF_F_NUMA_NODE) ?
880                 attr->numa_node : NUMA_NO_NODE;
881 }
882
883 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
884 int array_map_alloc_check(union bpf_attr *attr);
885
886 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
887                           union bpf_attr __user *uattr);
888 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
889                           union bpf_attr __user *uattr);
890 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
891                                      const union bpf_attr *kattr,
892                                      union bpf_attr __user *uattr);
893 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
894                     const struct bpf_prog *prog,
895                     struct bpf_insn_access_aux *info);
896 int btf_struct_access(struct bpf_verifier_log *log,
897                       const struct btf_type *t, int off, int size,
898                       enum bpf_access_type atype,
899                       u32 *next_btf_id);
900 int btf_resolve_helper_id(struct bpf_verifier_log *log,
901                           const struct bpf_func_proto *fn, int);
902
903 int btf_distill_func_proto(struct bpf_verifier_log *log,
904                            struct btf *btf,
905                            const struct btf_type *func_proto,
906                            const char *func_name,
907                            struct btf_func_model *m);
908
909 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog);
910
911 #else /* !CONFIG_BPF_SYSCALL */
912 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
913 {
914         return ERR_PTR(-EOPNOTSUPP);
915 }
916
917 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
918                                                      enum bpf_prog_type type,
919                                                      bool attach_drv)
920 {
921         return ERR_PTR(-EOPNOTSUPP);
922 }
923
924 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
925 {
926 }
927
928 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
929 {
930 }
931
932 static inline void bpf_prog_put(struct bpf_prog *prog)
933 {
934 }
935
936 static inline void bpf_prog_inc(struct bpf_prog *prog)
937 {
938 }
939
940 static inline struct bpf_prog *__must_check
941 bpf_prog_inc_not_zero(struct bpf_prog *prog)
942 {
943         return ERR_PTR(-EOPNOTSUPP);
944 }
945
946 static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
947 {
948         return 0;
949 }
950
951 static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
952 {
953 }
954
955 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
956 {
957         return -EOPNOTSUPP;
958 }
959
960 static inline struct net_device  *__dev_map_lookup_elem(struct bpf_map *map,
961                                                        u32 key)
962 {
963         return NULL;
964 }
965
966 static inline struct net_device  *__dev_map_hash_lookup_elem(struct bpf_map *map,
967                                                              u32 key)
968 {
969         return NULL;
970 }
971
972 static inline void __dev_map_flush(struct bpf_map *map)
973 {
974 }
975
976 struct xdp_buff;
977 struct bpf_dtab_netdev;
978
979 static inline
980 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
981                     struct net_device *dev_rx)
982 {
983         return 0;
984 }
985
986 struct sk_buff;
987
988 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
989                                            struct sk_buff *skb,
990                                            struct bpf_prog *xdp_prog)
991 {
992         return 0;
993 }
994
995 static inline
996 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
997 {
998         return NULL;
999 }
1000
1001 static inline void __cpu_map_flush(struct bpf_map *map)
1002 {
1003 }
1004
1005 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1006                                   struct xdp_buff *xdp,
1007                                   struct net_device *dev_rx)
1008 {
1009         return 0;
1010 }
1011
1012 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1013                                 enum bpf_prog_type type)
1014 {
1015         return ERR_PTR(-EOPNOTSUPP);
1016 }
1017
1018 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1019                                         const union bpf_attr *kattr,
1020                                         union bpf_attr __user *uattr)
1021 {
1022         return -ENOTSUPP;
1023 }
1024
1025 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1026                                         const union bpf_attr *kattr,
1027                                         union bpf_attr __user *uattr)
1028 {
1029         return -ENOTSUPP;
1030 }
1031
1032 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1033                                                    const union bpf_attr *kattr,
1034                                                    union bpf_attr __user *uattr)
1035 {
1036         return -ENOTSUPP;
1037 }
1038
1039 static inline void bpf_map_put(struct bpf_map *map)
1040 {
1041 }
1042 #endif /* CONFIG_BPF_SYSCALL */
1043
1044 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1045                                                  enum bpf_prog_type type)
1046 {
1047         return bpf_prog_get_type_dev(ufd, type, false);
1048 }
1049
1050 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1051
1052 int bpf_prog_offload_compile(struct bpf_prog *prog);
1053 void bpf_prog_offload_destroy(struct bpf_prog *prog);
1054 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1055                                struct bpf_prog *prog);
1056
1057 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1058
1059 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1060 int bpf_map_offload_update_elem(struct bpf_map *map,
1061                                 void *key, void *value, u64 flags);
1062 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1063 int bpf_map_offload_get_next_key(struct bpf_map *map,
1064                                  void *key, void *next_key);
1065
1066 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1067
1068 struct bpf_offload_dev *
1069 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1070 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1071 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1072 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1073                                     struct net_device *netdev);
1074 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1075                                        struct net_device *netdev);
1076 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1077
1078 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1079 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1080
1081 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1082 {
1083         return aux->offload_requested;
1084 }
1085
1086 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1087 {
1088         return unlikely(map->ops == &bpf_map_offload_ops);
1089 }
1090
1091 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1092 void bpf_map_offload_map_free(struct bpf_map *map);
1093 #else
1094 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1095                                         union bpf_attr *attr)
1096 {
1097         return -EOPNOTSUPP;
1098 }
1099
1100 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1101 {
1102         return false;
1103 }
1104
1105 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1106 {
1107         return false;
1108 }
1109
1110 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1111 {
1112         return ERR_PTR(-EOPNOTSUPP);
1113 }
1114
1115 static inline void bpf_map_offload_map_free(struct bpf_map *map)
1116 {
1117 }
1118 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1119
1120 #if defined(CONFIG_BPF_STREAM_PARSER)
1121 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
1122 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1123 #else
1124 static inline int sock_map_prog_update(struct bpf_map *map,
1125                                        struct bpf_prog *prog, u32 which)
1126 {
1127         return -EOPNOTSUPP;
1128 }
1129
1130 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1131                                        struct bpf_prog *prog)
1132 {
1133         return -EINVAL;
1134 }
1135 #endif
1136
1137 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1138 void bpf_sk_reuseport_detach(struct sock *sk);
1139 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1140                                        void *value);
1141 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1142                                        void *value, u64 map_flags);
1143 #else
1144 static inline void bpf_sk_reuseport_detach(struct sock *sk)
1145 {
1146 }
1147
1148 #ifdef CONFIG_BPF_SYSCALL
1149 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1150                                                      void *key, void *value)
1151 {
1152         return -EOPNOTSUPP;
1153 }
1154
1155 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1156                                                      void *key, void *value,
1157                                                      u64 map_flags)
1158 {
1159         return -EOPNOTSUPP;
1160 }
1161 #endif /* CONFIG_BPF_SYSCALL */
1162 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1163
1164 /* verifier prototypes for helper functions called from eBPF programs */
1165 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1166 extern const struct bpf_func_proto bpf_map_update_elem_proto;
1167 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1168 extern const struct bpf_func_proto bpf_map_push_elem_proto;
1169 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1170 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1171
1172 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1173 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1174 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1175 extern const struct bpf_func_proto bpf_tail_call_proto;
1176 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1177 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1178 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1179 extern const struct bpf_func_proto bpf_get_current_comm_proto;
1180 extern const struct bpf_func_proto bpf_get_stackid_proto;
1181 extern const struct bpf_func_proto bpf_get_stack_proto;
1182 extern const struct bpf_func_proto bpf_sock_map_update_proto;
1183 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1184 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1185 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1186 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1187 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1188 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1189 extern const struct bpf_func_proto bpf_spin_lock_proto;
1190 extern const struct bpf_func_proto bpf_spin_unlock_proto;
1191 extern const struct bpf_func_proto bpf_get_local_storage_proto;
1192 extern const struct bpf_func_proto bpf_strtol_proto;
1193 extern const struct bpf_func_proto bpf_strtoul_proto;
1194 extern const struct bpf_func_proto bpf_tcp_sock_proto;
1195
1196 /* Shared helpers among cBPF and eBPF. */
1197 void bpf_user_rnd_init_once(void);
1198 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1199
1200 #if defined(CONFIG_NET)
1201 bool bpf_sock_common_is_valid_access(int off, int size,
1202                                      enum bpf_access_type type,
1203                                      struct bpf_insn_access_aux *info);
1204 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1205                               struct bpf_insn_access_aux *info);
1206 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1207                                 const struct bpf_insn *si,
1208                                 struct bpf_insn *insn_buf,
1209                                 struct bpf_prog *prog,
1210                                 u32 *target_size);
1211 #else
1212 static inline bool bpf_sock_common_is_valid_access(int off, int size,
1213                                                    enum bpf_access_type type,
1214                                                    struct bpf_insn_access_aux *info)
1215 {
1216         return false;
1217 }
1218 static inline bool bpf_sock_is_valid_access(int off, int size,
1219                                             enum bpf_access_type type,
1220                                             struct bpf_insn_access_aux *info)
1221 {
1222         return false;
1223 }
1224 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1225                                               const struct bpf_insn *si,
1226                                               struct bpf_insn *insn_buf,
1227                                               struct bpf_prog *prog,
1228                                               u32 *target_size)
1229 {
1230         return 0;
1231 }
1232 #endif
1233
1234 #ifdef CONFIG_INET
1235 struct sk_reuseport_kern {
1236         struct sk_buff *skb;
1237         struct sock *sk;
1238         struct sock *selected_sk;
1239         void *data_end;
1240         u32 hash;
1241         u32 reuseport_id;
1242         bool bind_inany;
1243 };
1244 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1245                                   struct bpf_insn_access_aux *info);
1246
1247 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1248                                     const struct bpf_insn *si,
1249                                     struct bpf_insn *insn_buf,
1250                                     struct bpf_prog *prog,
1251                                     u32 *target_size);
1252
1253 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1254                                   struct bpf_insn_access_aux *info);
1255
1256 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1257                                     const struct bpf_insn *si,
1258                                     struct bpf_insn *insn_buf,
1259                                     struct bpf_prog *prog,
1260                                     u32 *target_size);
1261 #else
1262 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1263                                                 enum bpf_access_type type,
1264                                                 struct bpf_insn_access_aux *info)
1265 {
1266         return false;
1267 }
1268
1269 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1270                                                   const struct bpf_insn *si,
1271                                                   struct bpf_insn *insn_buf,
1272                                                   struct bpf_prog *prog,
1273                                                   u32 *target_size)
1274 {
1275         return 0;
1276 }
1277 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1278                                                 enum bpf_access_type type,
1279                                                 struct bpf_insn_access_aux *info)
1280 {
1281         return false;
1282 }
1283
1284 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1285                                                   const struct bpf_insn *si,
1286                                                   struct bpf_insn *insn_buf,
1287                                                   struct bpf_prog *prog,
1288                                                   u32 *target_size)
1289 {
1290         return 0;
1291 }
1292 #endif /* CONFIG_INET */
1293
1294 enum bpf_text_poke_type {
1295         /* All call-related pokes. */
1296         BPF_MOD_NOP_TO_CALL,
1297         BPF_MOD_CALL_TO_CALL,
1298         BPF_MOD_CALL_TO_NOP,
1299         /* All jump-related pokes. */
1300         BPF_MOD_NOP_TO_JUMP,
1301         BPF_MOD_JUMP_TO_JUMP,
1302         BPF_MOD_JUMP_TO_NOP,
1303 };
1304
1305 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1306                        void *addr1, void *addr2);
1307
1308 #endif /* _LINUX_BPF_H */