~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/bpf.h

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp