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