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