1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
7 #include <uapi/linux/bpf.h>
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>
23 struct bpf_verifier_env;
24 struct bpf_verifier_log;
33 struct exception_table_entry;
34 struct seq_operations;
36 extern struct idr btf_idr;
37 extern spinlock_t btf_idr_lock;
39 /* map is generic key/value storage optionally accesible by eBPF programs */
41 /* funcs callable from userspace (via syscall) */
42 int (*map_alloc_check)(union bpf_attr *attr);
43 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
44 void (*map_release)(struct bpf_map *map, struct file *map_file);
45 void (*map_free)(struct bpf_map *map);
46 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
47 void (*map_release_uref)(struct bpf_map *map);
48 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
49 int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
50 union bpf_attr __user *uattr);
51 int (*map_lookup_and_delete_batch)(struct bpf_map *map,
52 const union bpf_attr *attr,
53 union bpf_attr __user *uattr);
54 int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
55 union bpf_attr __user *uattr);
56 int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
57 union bpf_attr __user *uattr);
59 /* funcs callable from userspace and from eBPF programs */
60 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
61 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
62 int (*map_delete_elem)(struct bpf_map *map, void *key);
63 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
64 int (*map_pop_elem)(struct bpf_map *map, void *value);
65 int (*map_peek_elem)(struct bpf_map *map, void *value);
67 /* funcs called by prog_array and perf_event_array map */
68 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
70 void (*map_fd_put_ptr)(void *ptr);
71 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
72 u32 (*map_fd_sys_lookup_elem)(void *ptr);
73 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
75 int (*map_check_btf)(const struct bpf_map *map,
76 const struct btf *btf,
77 const struct btf_type *key_type,
78 const struct btf_type *value_type);
80 /* Prog poke tracking helpers. */
81 int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
82 void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
83 void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
84 struct bpf_prog *new);
86 /* Direct value access helpers. */
87 int (*map_direct_value_addr)(const struct bpf_map *map,
89 int (*map_direct_value_meta)(const struct bpf_map *map,
91 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
94 struct bpf_map_memory {
96 struct user_struct *user;
100 /* The first two cachelines with read-mostly members of which some
101 * are also accessed in fast-path (e.g. ops, max_entries).
103 const struct bpf_map_ops *ops ____cacheline_aligned;
104 struct bpf_map *inner_map_meta;
105 #ifdef CONFIG_SECURITY
108 enum bpf_map_type map_type;
113 int spin_lock_off; /* >=0 valid offset, <0 error */
117 u32 btf_value_type_id;
119 struct bpf_map_memory memory;
120 char name[BPF_OBJ_NAME_LEN];
121 u32 btf_vmlinux_value_type_id;
123 bool frozen; /* write-once; write-protected by freeze_mutex */
126 /* The 3rd and 4th cacheline with misc members to avoid false sharing
127 * particularly with refcounting.
129 atomic64_t refcnt ____cacheline_aligned;
131 struct work_struct work;
132 struct mutex freeze_mutex;
133 u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
136 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
138 return map->spin_lock_off >= 0;
141 static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
143 if (likely(!map_value_has_spin_lock(map)))
145 *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
146 (struct bpf_spin_lock){};
149 /* copy everything but bpf_spin_lock */
150 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
152 if (unlikely(map_value_has_spin_lock(map))) {
153 u32 off = map->spin_lock_off;
155 memcpy(dst, src, off);
156 memcpy(dst + off + sizeof(struct bpf_spin_lock),
157 src + off + sizeof(struct bpf_spin_lock),
158 map->value_size - off - sizeof(struct bpf_spin_lock));
160 memcpy(dst, src, map->value_size);
163 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
165 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
167 struct bpf_offload_dev;
168 struct bpf_offloaded_map;
170 struct bpf_map_dev_ops {
171 int (*map_get_next_key)(struct bpf_offloaded_map *map,
172 void *key, void *next_key);
173 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
174 void *key, void *value);
175 int (*map_update_elem)(struct bpf_offloaded_map *map,
176 void *key, void *value, u64 flags);
177 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
180 struct bpf_offloaded_map {
182 struct net_device *netdev;
183 const struct bpf_map_dev_ops *dev_ops;
185 struct list_head offloads;
188 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
190 return container_of(map, struct bpf_offloaded_map, map);
193 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
195 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
198 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
200 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
201 map->ops->map_seq_show_elem;
204 int map_check_no_btf(const struct bpf_map *map,
205 const struct btf *btf,
206 const struct btf_type *key_type,
207 const struct btf_type *value_type);
209 extern const struct bpf_map_ops bpf_map_offload_ops;
211 /* function argument constraints */
213 ARG_DONTCARE = 0, /* unused argument in helper function */
215 /* the following constraints used to prototype
216 * bpf_map_lookup/update/delete_elem() functions
218 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
219 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
220 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
221 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
222 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
224 /* the following constraints used to prototype bpf_memcmp() and other
225 * functions that access data on eBPF program stack
227 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
228 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
229 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
230 * helper function must fill all bytes or clear
231 * them in error case.
234 ARG_CONST_SIZE, /* number of bytes accessed from memory */
235 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
237 ARG_PTR_TO_CTX, /* pointer to context */
238 ARG_PTR_TO_CTX_OR_NULL, /* pointer to context or NULL */
239 ARG_ANYTHING, /* any (initialized) argument is ok */
240 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
241 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
242 ARG_PTR_TO_INT, /* pointer to int */
243 ARG_PTR_TO_LONG, /* pointer to long */
244 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
245 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
248 /* type of values returned from helper functions */
249 enum bpf_return_type {
250 RET_INTEGER, /* function returns integer */
251 RET_VOID, /* function doesn't return anything */
252 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
253 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
254 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
255 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
256 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
259 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
260 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
261 * instructions after verifying
263 struct bpf_func_proto {
264 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
267 enum bpf_return_type ret_type;
270 enum bpf_arg_type arg1_type;
271 enum bpf_arg_type arg2_type;
272 enum bpf_arg_type arg3_type;
273 enum bpf_arg_type arg4_type;
274 enum bpf_arg_type arg5_type;
276 enum bpf_arg_type arg_type[5];
278 int *btf_id; /* BTF ids of arguments */
281 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
282 * the first argument to eBPF programs.
283 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
287 enum bpf_access_type {
292 /* types of values stored in eBPF registers */
293 /* Pointer types represent:
296 * pointer + (u16) var
297 * pointer + (u16) var + imm
298 * if (range > 0) then [ptr, ptr + range - off) is safe to access
299 * if (id > 0) means that some 'var' was added
300 * if (off > 0) means that 'imm' was added
303 NOT_INIT = 0, /* nothing was written into register */
304 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
305 PTR_TO_CTX, /* reg points to bpf_context */
306 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
307 PTR_TO_MAP_VALUE, /* reg points to map element value */
308 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
309 PTR_TO_STACK, /* reg == frame_pointer + offset */
310 PTR_TO_PACKET_META, /* skb->data - meta_len */
311 PTR_TO_PACKET, /* reg points to skb->data */
312 PTR_TO_PACKET_END, /* skb->data + headlen */
313 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
314 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
315 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
316 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
317 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
318 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
319 PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
320 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
321 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
322 PTR_TO_BTF_ID, /* reg points to kernel struct */
325 /* The information passed from prog-specific *_is_valid_access
326 * back to the verifier.
328 struct bpf_insn_access_aux {
329 enum bpf_reg_type reg_type;
334 struct bpf_verifier_log *log; /* for verbose logs */
338 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
340 aux->ctx_field_size = size;
343 struct bpf_prog_ops {
344 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
345 union bpf_attr __user *uattr);
348 struct bpf_verifier_ops {
349 /* return eBPF function prototype for verification */
350 const struct bpf_func_proto *
351 (*get_func_proto)(enum bpf_func_id func_id,
352 const struct bpf_prog *prog);
354 /* return true if 'size' wide access at offset 'off' within bpf_context
355 * with 'type' (read or write) is allowed
357 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
358 const struct bpf_prog *prog,
359 struct bpf_insn_access_aux *info);
360 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
361 const struct bpf_prog *prog);
362 int (*gen_ld_abs)(const struct bpf_insn *orig,
363 struct bpf_insn *insn_buf);
364 u32 (*convert_ctx_access)(enum bpf_access_type type,
365 const struct bpf_insn *src,
366 struct bpf_insn *dst,
367 struct bpf_prog *prog, u32 *target_size);
368 int (*btf_struct_access)(struct bpf_verifier_log *log,
369 const struct btf_type *t, int off, int size,
370 enum bpf_access_type atype,
374 struct bpf_prog_offload_ops {
375 /* verifier basic callbacks */
376 int (*insn_hook)(struct bpf_verifier_env *env,
377 int insn_idx, int prev_insn_idx);
378 int (*finalize)(struct bpf_verifier_env *env);
379 /* verifier optimization callbacks (called after .finalize) */
380 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
381 struct bpf_insn *insn);
382 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
383 /* program management callbacks */
384 int (*prepare)(struct bpf_prog *prog);
385 int (*translate)(struct bpf_prog *prog);
386 void (*destroy)(struct bpf_prog *prog);
389 struct bpf_prog_offload {
390 struct bpf_prog *prog;
391 struct net_device *netdev;
392 struct bpf_offload_dev *offdev;
394 struct list_head offloads;
401 enum bpf_cgroup_storage_type {
402 BPF_CGROUP_STORAGE_SHARED,
403 BPF_CGROUP_STORAGE_PERCPU,
404 __BPF_CGROUP_STORAGE_MAX
407 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
409 /* The longest tracepoint has 12 args.
410 * See include/trace/bpf_probe.h
412 #define MAX_BPF_FUNC_ARGS 12
414 struct bpf_prog_stats {
417 struct u64_stats_sync syncp;
418 } __aligned(2 * sizeof(u64));
420 struct btf_func_model {
423 u8 arg_size[MAX_BPF_FUNC_ARGS];
426 /* Restore arguments before returning from trampoline to let original function
427 * continue executing. This flag is used for fentry progs when there are no
430 #define BPF_TRAMP_F_RESTORE_REGS BIT(0)
431 /* Call original function after fentry progs, but before fexit progs.
432 * Makes sense for fentry/fexit, normal calls and indirect calls.
434 #define BPF_TRAMP_F_CALL_ORIG BIT(1)
435 /* Skip current frame and return to parent. Makes sense for fentry/fexit
436 * programs only. Should not be used with normal calls and indirect calls.
438 #define BPF_TRAMP_F_SKIP_FRAME BIT(2)
440 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
441 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2
443 #define BPF_MAX_TRAMP_PROGS 40
445 struct bpf_tramp_progs {
446 struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS];
450 /* Different use cases for BPF trampoline:
451 * 1. replace nop at the function entry (kprobe equivalent)
452 * flags = BPF_TRAMP_F_RESTORE_REGS
453 * fentry = a set of programs to run before returning from trampoline
455 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
456 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
457 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
458 * fentry = a set of program to run before calling original function
459 * fexit = a set of program to run after original function
461 * 3. replace direct call instruction anywhere in the function body
462 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
464 * fentry = a set of programs to run before returning from trampoline
465 * With flags = BPF_TRAMP_F_CALL_ORIG
466 * orig_call = original callback addr or direct function addr
467 * fentry = a set of program to run before calling original function
468 * fexit = a set of program to run after original function
470 int arch_prepare_bpf_trampoline(void *image, void *image_end,
471 const struct btf_func_model *m, u32 flags,
472 struct bpf_tramp_progs *tprogs,
474 /* these two functions are called from generated trampoline */
475 u64 notrace __bpf_prog_enter(void);
476 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
481 char name[KSYM_NAME_LEN];
482 struct list_head lnode;
483 struct latch_tree_node tnode;
487 enum bpf_tramp_prog_type {
490 BPF_TRAMP_MODIFY_RETURN,
492 BPF_TRAMP_REPLACE, /* more than MAX */
495 struct bpf_trampoline {
496 /* hlist for trampoline_table */
497 struct hlist_node hlist;
498 /* serializes access to fields of this trampoline */
503 struct btf_func_model model;
507 /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
508 * program by replacing one of its functions. func.addr is the address
509 * of the function it replaced.
511 struct bpf_prog *extension_prog;
512 /* list of BPF programs using this trampoline */
513 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
514 /* Number of attached programs. A counter per kind. */
515 int progs_cnt[BPF_TRAMP_MAX];
516 /* Executable image of trampoline */
519 struct bpf_ksym ksym;
522 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
524 struct bpf_dispatcher_prog {
525 struct bpf_prog *prog;
529 struct bpf_dispatcher {
530 /* dispatcher mutex */
533 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
537 struct bpf_ksym ksym;
540 static __always_inline unsigned int bpf_dispatcher_nop_func(
542 const struct bpf_insn *insnsi,
543 unsigned int (*bpf_func)(const void *,
544 const struct bpf_insn *))
546 return bpf_func(ctx, insnsi);
548 #ifdef CONFIG_BPF_JIT
549 struct bpf_trampoline *bpf_trampoline_lookup(u64 key);
550 int bpf_trampoline_link_prog(struct bpf_prog *prog);
551 int bpf_trampoline_unlink_prog(struct bpf_prog *prog);
552 void bpf_trampoline_put(struct bpf_trampoline *tr);
553 #define BPF_DISPATCHER_INIT(_name) { \
554 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
555 .func = &_name##_func, \
562 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
566 #define DEFINE_BPF_DISPATCHER(name) \
567 noinline unsigned int bpf_dispatcher_##name##_func( \
569 const struct bpf_insn *insnsi, \
570 unsigned int (*bpf_func)(const void *, \
571 const struct bpf_insn *)) \
573 return bpf_func(ctx, insnsi); \
575 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
576 struct bpf_dispatcher bpf_dispatcher_##name = \
577 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
578 #define DECLARE_BPF_DISPATCHER(name) \
579 unsigned int bpf_dispatcher_##name##_func( \
581 const struct bpf_insn *insnsi, \
582 unsigned int (*bpf_func)(const void *, \
583 const struct bpf_insn *)); \
584 extern struct bpf_dispatcher bpf_dispatcher_##name;
585 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
586 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
587 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
588 struct bpf_prog *to);
589 /* Called only from JIT-enabled code, so there's no need for stubs. */
590 void *bpf_jit_alloc_exec_page(void);
591 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
592 void bpf_image_ksym_del(struct bpf_ksym *ksym);
593 void bpf_ksym_add(struct bpf_ksym *ksym);
594 void bpf_ksym_del(struct bpf_ksym *ksym);
596 static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
600 static inline int bpf_trampoline_link_prog(struct bpf_prog *prog)
604 static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
608 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
609 #define DEFINE_BPF_DISPATCHER(name)
610 #define DECLARE_BPF_DISPATCHER(name)
611 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
612 #define BPF_DISPATCHER_PTR(name) NULL
613 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
614 struct bpf_prog *from,
615 struct bpf_prog *to) {}
616 static inline bool is_bpf_image_address(unsigned long address)
622 struct bpf_func_info_aux {
627 enum bpf_jit_poke_reason {
628 BPF_POKE_REASON_TAIL_CALL,
631 /* Descriptor of pokes pointing /into/ the JITed image. */
632 struct bpf_jit_poke_descriptor {
645 struct bpf_prog_aux {
653 u32 func_cnt; /* used by non-func prog as the number of func progs */
654 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
655 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
656 struct bpf_prog *linked_prog;
657 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
658 bool offload_requested;
659 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
660 bool func_proto_unreliable;
661 enum bpf_tramp_prog_type trampoline_prog_type;
662 struct bpf_trampoline *trampoline;
663 struct hlist_node tramp_hlist;
664 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
665 const struct btf_type *attach_func_proto;
666 /* function name for valid attach_btf_id */
667 const char *attach_func_name;
668 struct bpf_prog **func;
669 void *jit_data; /* JIT specific data. arch dependent */
670 struct bpf_jit_poke_descriptor *poke_tab;
672 struct bpf_ksym ksym;
673 const struct bpf_prog_ops *ops;
674 struct bpf_map **used_maps;
675 struct bpf_prog *prog;
676 struct user_struct *user;
677 u64 load_time; /* ns since boottime */
678 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
679 char name[BPF_OBJ_NAME_LEN];
680 #ifdef CONFIG_SECURITY
683 struct bpf_prog_offload *offload;
685 struct bpf_func_info *func_info;
686 struct bpf_func_info_aux *func_info_aux;
687 /* bpf_line_info loaded from userspace. linfo->insn_off
688 * has the xlated insn offset.
689 * Both the main and sub prog share the same linfo.
690 * The subprog can access its first linfo by
691 * using the linfo_idx.
693 struct bpf_line_info *linfo;
694 /* jited_linfo is the jited addr of the linfo. It has a
695 * one to one mapping to linfo:
696 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
697 * Both the main and sub prog share the same jited_linfo.
698 * The subprog can access its first jited_linfo by
699 * using the linfo_idx.
704 /* subprog can use linfo_idx to access its first linfo and
706 * main prog always has linfo_idx == 0
710 struct exception_table_entry *extable;
711 struct bpf_prog_stats __percpu *stats;
713 struct work_struct work;
718 struct bpf_array_aux {
719 /* 'Ownership' of prog array is claimed by the first program that
720 * is going to use this map or by the first program which FD is
721 * stored in the map to make sure that all callers and callees have
722 * the same prog type and JITed flag.
724 enum bpf_prog_type type;
726 /* Programs with direct jumps into programs part of this array. */
727 struct list_head poke_progs;
729 struct mutex poke_mutex;
730 struct work_struct work;
733 struct bpf_struct_ops_value;
737 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
738 struct bpf_struct_ops {
739 const struct bpf_verifier_ops *verifier_ops;
740 int (*init)(struct btf *btf);
741 int (*check_member)(const struct btf_type *t,
742 const struct btf_member *member);
743 int (*init_member)(const struct btf_type *t,
744 const struct btf_member *member,
745 void *kdata, const void *udata);
746 int (*reg)(void *kdata);
747 void (*unreg)(void *kdata);
748 const struct btf_type *type;
749 const struct btf_type *value_type;
751 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
756 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
757 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
758 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
759 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
760 bool bpf_struct_ops_get(const void *kdata);
761 void bpf_struct_ops_put(const void *kdata);
762 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
764 static inline bool bpf_try_module_get(const void *data, struct module *owner)
766 if (owner == BPF_MODULE_OWNER)
767 return bpf_struct_ops_get(data);
769 return try_module_get(owner);
771 static inline void bpf_module_put(const void *data, struct module *owner)
773 if (owner == BPF_MODULE_OWNER)
774 bpf_struct_ops_put(data);
779 static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
783 static inline void bpf_struct_ops_init(struct btf *btf,
784 struct bpf_verifier_log *log)
787 static inline bool bpf_try_module_get(const void *data, struct module *owner)
789 return try_module_get(owner);
791 static inline void bpf_module_put(const void *data, struct module *owner)
795 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
807 struct bpf_array_aux *aux;
809 char value[0] __aligned(8);
810 void *ptrs[0] __aligned(8);
811 void __percpu *pptrs[0] __aligned(8);
815 #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
816 #define MAX_TAIL_CALL_CNT 32
818 #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
819 BPF_F_RDONLY_PROG | \
823 #define BPF_MAP_CAN_READ BIT(0)
824 #define BPF_MAP_CAN_WRITE BIT(1)
826 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
828 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
830 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
833 if (access_flags & BPF_F_RDONLY_PROG)
834 return BPF_MAP_CAN_READ;
835 else if (access_flags & BPF_F_WRONLY_PROG)
836 return BPF_MAP_CAN_WRITE;
838 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
841 static inline bool bpf_map_flags_access_ok(u32 access_flags)
843 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
844 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
847 struct bpf_event_entry {
848 struct perf_event *event;
849 struct file *perf_file;
850 struct file *map_file;
854 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
855 int bpf_prog_calc_tag(struct bpf_prog *fp);
856 const char *kernel_type_name(u32 btf_type_id);
858 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
860 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
861 unsigned long off, unsigned long len);
862 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
863 const struct bpf_insn *src,
864 struct bpf_insn *dst,
865 struct bpf_prog *prog,
868 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
869 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
871 /* an array of programs to be executed under rcu_lock.
874 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
876 * the structure returned by bpf_prog_array_alloc() should be populated
877 * with program pointers and the last pointer must be NULL.
878 * The user has to keep refcnt on the program and make sure the program
879 * is removed from the array before bpf_prog_put().
880 * The 'struct bpf_prog_array *' should only be replaced with xchg()
881 * since other cpus are walking the array of pointers in parallel.
883 struct bpf_prog_array_item {
884 struct bpf_prog *prog;
885 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
888 struct bpf_prog_array {
890 struct bpf_prog_array_item items[];
893 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
894 void bpf_prog_array_free(struct bpf_prog_array *progs);
895 int bpf_prog_array_length(struct bpf_prog_array *progs);
896 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
897 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
898 __u32 __user *prog_ids, u32 cnt);
900 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
901 struct bpf_prog *old_prog);
902 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
903 u32 *prog_ids, u32 request_cnt,
905 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
906 struct bpf_prog *exclude_prog,
907 struct bpf_prog *include_prog,
908 struct bpf_prog_array **new_array);
910 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
912 struct bpf_prog_array_item *_item; \
913 struct bpf_prog *_prog; \
914 struct bpf_prog_array *_array; \
918 _array = rcu_dereference(array); \
919 if (unlikely(check_non_null && !_array))\
921 _item = &_array->items[0]; \
922 while ((_prog = READ_ONCE(_item->prog))) { \
923 bpf_cgroup_storage_set(_item->cgroup_storage); \
924 _ret &= func(_prog, ctx); \
933 /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
934 * so BPF programs can request cwr for TCP packets.
936 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
937 * packet. This macro changes the behavior so the low order bit
938 * indicates whether the packet should be dropped (0) or not (1)
939 * and the next bit is a congestion notification bit. This could be
940 * used by TCP to call tcp_enter_cwr()
942 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
945 * 2: drop packet and cn
946 * 3: keep packet and cn
948 * This macro then converts it to one of the NET_XMIT or an error
949 * code that is then interpreted as drop packet (and no cn):
950 * 0: NET_XMIT_SUCCESS skb should be transmitted
951 * 1: NET_XMIT_DROP skb should be dropped and cn
952 * 2: NET_XMIT_CN skb should be transmitted and cn
953 * 3: -EPERM skb should be dropped
955 #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
957 struct bpf_prog_array_item *_item; \
958 struct bpf_prog *_prog; \
959 struct bpf_prog_array *_array; \
965 _array = rcu_dereference(array); \
966 _item = &_array->items[0]; \
967 while ((_prog = READ_ONCE(_item->prog))) { \
968 bpf_cgroup_storage_set(_item->cgroup_storage); \
969 ret = func(_prog, ctx); \
977 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
979 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
983 #define BPF_PROG_RUN_ARRAY(array, ctx, func) \
984 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
986 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
987 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
989 #ifdef CONFIG_BPF_SYSCALL
990 DECLARE_PER_CPU(int, bpf_prog_active);
991 extern struct mutex bpf_stats_enabled_mutex;
994 * Block execution of BPF programs attached to instrumentation (perf,
995 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
996 * these events can happen inside a region which holds a map bucket lock
997 * and can deadlock on it.
999 * Use the preemption safe inc/dec variants on RT because migrate disable
1000 * is preemptible on RT and preemption in the middle of the RMW operation
1001 * might lead to inconsistent state. Use the raw variants for non RT
1002 * kernels as migrate_disable() maps to preempt_disable() so the slightly
1003 * more expensive save operation can be avoided.
1005 static inline void bpf_disable_instrumentation(void)
1008 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1009 this_cpu_inc(bpf_prog_active);
1011 __this_cpu_inc(bpf_prog_active);
1014 static inline void bpf_enable_instrumentation(void)
1016 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1017 this_cpu_dec(bpf_prog_active);
1019 __this_cpu_dec(bpf_prog_active);
1023 extern const struct file_operations bpf_map_fops;
1024 extern const struct file_operations bpf_prog_fops;
1025 extern const struct file_operations bpf_iter_fops;
1027 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1028 extern const struct bpf_prog_ops _name ## _prog_ops; \
1029 extern const struct bpf_verifier_ops _name ## _verifier_ops;
1030 #define BPF_MAP_TYPE(_id, _ops) \
1031 extern const struct bpf_map_ops _ops;
1032 #define BPF_LINK_TYPE(_id, _name)
1033 #include <linux/bpf_types.h>
1034 #undef BPF_PROG_TYPE
1036 #undef BPF_LINK_TYPE
1038 extern const struct bpf_prog_ops bpf_offload_prog_ops;
1039 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1040 extern const struct bpf_verifier_ops xdp_analyzer_ops;
1042 struct bpf_prog *bpf_prog_get(u32 ufd);
1043 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1045 void bpf_prog_add(struct bpf_prog *prog, int i);
1046 void bpf_prog_sub(struct bpf_prog *prog, int i);
1047 void bpf_prog_inc(struct bpf_prog *prog);
1048 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1049 void bpf_prog_put(struct bpf_prog *prog);
1050 int __bpf_prog_charge(struct user_struct *user, u32 pages);
1051 void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
1052 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
1053 struct bpf_map **used_maps, u32 len);
1055 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1056 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1058 struct bpf_map *bpf_map_get(u32 ufd);
1059 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1060 struct bpf_map *__bpf_map_get(struct fd f);
1061 void bpf_map_inc(struct bpf_map *map);
1062 void bpf_map_inc_with_uref(struct bpf_map *map);
1063 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1064 void bpf_map_put_with_uref(struct bpf_map *map);
1065 void bpf_map_put(struct bpf_map *map);
1066 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
1067 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
1068 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
1069 void bpf_map_charge_finish(struct bpf_map_memory *mem);
1070 void bpf_map_charge_move(struct bpf_map_memory *dst,
1071 struct bpf_map_memory *src);
1072 void *bpf_map_area_alloc(u64 size, int numa_node);
1073 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1074 void bpf_map_area_free(void *base);
1075 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1076 int generic_map_lookup_batch(struct bpf_map *map,
1077 const union bpf_attr *attr,
1078 union bpf_attr __user *uattr);
1079 int generic_map_update_batch(struct bpf_map *map,
1080 const union bpf_attr *attr,
1081 union bpf_attr __user *uattr);
1082 int generic_map_delete_batch(struct bpf_map *map,
1083 const union bpf_attr *attr,
1084 union bpf_attr __user *uattr);
1085 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1087 extern int sysctl_unprivileged_bpf_disabled;
1089 int bpf_map_new_fd(struct bpf_map *map, int flags);
1090 int bpf_prog_new_fd(struct bpf_prog *prog);
1095 enum bpf_link_type type;
1096 const struct bpf_link_ops *ops;
1097 struct bpf_prog *prog;
1098 struct work_struct work;
1101 struct bpf_link_primer {
1102 struct bpf_link *link;
1108 struct bpf_link_ops {
1109 void (*release)(struct bpf_link *link);
1110 void (*dealloc)(struct bpf_link *link);
1111 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1112 struct bpf_prog *old_prog);
1113 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1114 int (*fill_link_info)(const struct bpf_link *link,
1115 struct bpf_link_info *info);
1118 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1119 const struct bpf_link_ops *ops, struct bpf_prog *prog);
1120 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1121 int bpf_link_settle(struct bpf_link_primer *primer);
1122 void bpf_link_cleanup(struct bpf_link_primer *primer);
1123 void bpf_link_inc(struct bpf_link *link);
1124 void bpf_link_put(struct bpf_link *link);
1125 int bpf_link_new_fd(struct bpf_link *link);
1126 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1127 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1129 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1130 int bpf_obj_get_user(const char __user *pathname, int flags);
1132 #define BPF_ITER_FUNC_PREFIX "__bpf_iter__"
1133 #define DEFINE_BPF_ITER_FUNC(target, args...) \
1134 extern int __bpf_iter__ ## target(args); \
1135 int __init __bpf_iter__ ## target(args) { return 0; }
1137 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data);
1138 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
1140 struct bpf_iter_reg {
1142 const struct seq_operations *seq_ops;
1143 bpf_iter_init_seq_priv_t init_seq_private;
1144 bpf_iter_fini_seq_priv_t fini_seq_private;
1148 struct bpf_iter_meta {
1149 __bpf_md_ptr(struct seq_file *, seq);
1154 int bpf_iter_reg_target(struct bpf_iter_reg *reg_info);
1155 void bpf_iter_unreg_target(const char *target);
1156 bool bpf_iter_prog_supported(struct bpf_prog *prog);
1157 int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
1158 int bpf_iter_new_fd(struct bpf_link *link);
1159 bool bpf_link_is_iter(struct bpf_link *link);
1160 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1161 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1163 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1164 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1165 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1167 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1170 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1172 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1173 void *key, void *value, u64 map_flags);
1174 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1175 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1176 void *key, void *value, u64 map_flags);
1177 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1179 int bpf_get_file_flag(int flags);
1180 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
1181 size_t actual_size);
1183 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
1184 * forced to use 'long' read/writes to try to atomically copy long counters.
1185 * Best-effort only. No barriers here, since it _will_ race with concurrent
1186 * updates from BPF programs. Called from bpf syscall and mostly used with
1187 * size 8 or 16 bytes, so ask compiler to inline it.
1189 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
1191 const long *lsrc = src;
1194 size /= sizeof(long);
1199 /* verify correctness of eBPF program */
1200 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
1201 union bpf_attr __user *uattr);
1202 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
1208 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
1209 struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
1210 void __dev_flush(void);
1211 int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1212 struct net_device *dev_rx);
1213 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1214 struct net_device *dev_rx);
1215 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1216 struct bpf_prog *xdp_prog);
1218 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
1219 void __cpu_map_flush(void);
1220 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
1221 struct net_device *dev_rx);
1223 /* Return map's numa specified by userspace */
1224 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1226 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1227 attr->numa_node : NUMA_NO_NODE;
1230 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
1231 int array_map_alloc_check(union bpf_attr *attr);
1233 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1234 union bpf_attr __user *uattr);
1235 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1236 union bpf_attr __user *uattr);
1237 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1238 const union bpf_attr *kattr,
1239 union bpf_attr __user *uattr);
1240 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1241 const union bpf_attr *kattr,
1242 union bpf_attr __user *uattr);
1243 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1244 const struct bpf_prog *prog,
1245 struct bpf_insn_access_aux *info);
1246 int btf_struct_access(struct bpf_verifier_log *log,
1247 const struct btf_type *t, int off, int size,
1248 enum bpf_access_type atype,
1250 int btf_resolve_helper_id(struct bpf_verifier_log *log,
1251 const struct bpf_func_proto *fn, int);
1253 int btf_distill_func_proto(struct bpf_verifier_log *log,
1255 const struct btf_type *func_proto,
1256 const char *func_name,
1257 struct btf_func_model *m);
1259 struct bpf_reg_state;
1260 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
1261 struct bpf_reg_state *regs);
1262 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1263 struct bpf_reg_state *reg);
1264 int btf_check_type_match(struct bpf_verifier_env *env, struct bpf_prog *prog,
1265 struct btf *btf, const struct btf_type *t);
1267 struct bpf_prog *bpf_prog_by_id(u32 id);
1269 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
1270 #else /* !CONFIG_BPF_SYSCALL */
1271 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1273 return ERR_PTR(-EOPNOTSUPP);
1276 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1277 enum bpf_prog_type type,
1280 return ERR_PTR(-EOPNOTSUPP);
1283 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
1287 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1291 static inline void bpf_prog_put(struct bpf_prog *prog)
1295 static inline void bpf_prog_inc(struct bpf_prog *prog)
1299 static inline struct bpf_prog *__must_check
1300 bpf_prog_inc_not_zero(struct bpf_prog *prog)
1302 return ERR_PTR(-EOPNOTSUPP);
1305 static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
1310 static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1314 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
1319 static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
1325 static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
1331 static inline void __dev_flush(void)
1336 struct bpf_dtab_netdev;
1339 int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1340 struct net_device *dev_rx)
1346 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1347 struct net_device *dev_rx)
1354 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
1355 struct sk_buff *skb,
1356 struct bpf_prog *xdp_prog)
1362 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
1367 static inline void __cpu_map_flush(void)
1371 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1372 struct xdp_buff *xdp,
1373 struct net_device *dev_rx)
1378 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1379 enum bpf_prog_type type)
1381 return ERR_PTR(-EOPNOTSUPP);
1384 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1385 const union bpf_attr *kattr,
1386 union bpf_attr __user *uattr)
1391 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1392 const union bpf_attr *kattr,
1393 union bpf_attr __user *uattr)
1398 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1399 const union bpf_attr *kattr,
1400 union bpf_attr __user *uattr)
1405 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1406 const union bpf_attr *kattr,
1407 union bpf_attr __user *uattr)
1412 static inline void bpf_map_put(struct bpf_map *map)
1416 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
1418 return ERR_PTR(-ENOTSUPP);
1421 static inline const struct bpf_func_proto *
1422 bpf_base_func_proto(enum bpf_func_id func_id)
1426 #endif /* CONFIG_BPF_SYSCALL */
1428 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1429 enum bpf_prog_type type)
1431 return bpf_prog_get_type_dev(ufd, type, false);
1434 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1436 int bpf_prog_offload_compile(struct bpf_prog *prog);
1437 void bpf_prog_offload_destroy(struct bpf_prog *prog);
1438 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1439 struct bpf_prog *prog);
1441 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1443 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1444 int bpf_map_offload_update_elem(struct bpf_map *map,
1445 void *key, void *value, u64 flags);
1446 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1447 int bpf_map_offload_get_next_key(struct bpf_map *map,
1448 void *key, void *next_key);
1450 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1452 struct bpf_offload_dev *
1453 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1454 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1455 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1456 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1457 struct net_device *netdev);
1458 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1459 struct net_device *netdev);
1460 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1462 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1463 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1465 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1467 return aux->offload_requested;
1470 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1472 return unlikely(map->ops == &bpf_map_offload_ops);
1475 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1476 void bpf_map_offload_map_free(struct bpf_map *map);
1478 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1479 union bpf_attr *attr)
1484 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1489 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1494 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1496 return ERR_PTR(-EOPNOTSUPP);
1499 static inline void bpf_map_offload_map_free(struct bpf_map *map)
1502 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1504 #if defined(CONFIG_BPF_STREAM_PARSER)
1505 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
1506 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1507 void sock_map_unhash(struct sock *sk);
1508 void sock_map_close(struct sock *sk, long timeout);
1510 static inline int sock_map_prog_update(struct bpf_map *map,
1511 struct bpf_prog *prog, u32 which)
1516 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1517 struct bpf_prog *prog)
1521 #endif /* CONFIG_BPF_STREAM_PARSER */
1523 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1524 void bpf_sk_reuseport_detach(struct sock *sk);
1525 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1527 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1528 void *value, u64 map_flags);
1530 static inline void bpf_sk_reuseport_detach(struct sock *sk)
1534 #ifdef CONFIG_BPF_SYSCALL
1535 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1536 void *key, void *value)
1541 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1542 void *key, void *value,
1547 #endif /* CONFIG_BPF_SYSCALL */
1548 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1550 /* verifier prototypes for helper functions called from eBPF programs */
1551 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1552 extern const struct bpf_func_proto bpf_map_update_elem_proto;
1553 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1554 extern const struct bpf_func_proto bpf_map_push_elem_proto;
1555 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1556 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1558 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1559 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1560 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1561 extern const struct bpf_func_proto bpf_tail_call_proto;
1562 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1563 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
1564 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1565 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1566 extern const struct bpf_func_proto bpf_get_current_comm_proto;
1567 extern const struct bpf_func_proto bpf_get_stackid_proto;
1568 extern const struct bpf_func_proto bpf_get_stack_proto;
1569 extern const struct bpf_func_proto bpf_sock_map_update_proto;
1570 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1571 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1572 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
1573 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1574 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1575 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1576 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1577 extern const struct bpf_func_proto bpf_spin_lock_proto;
1578 extern const struct bpf_func_proto bpf_spin_unlock_proto;
1579 extern const struct bpf_func_proto bpf_get_local_storage_proto;
1580 extern const struct bpf_func_proto bpf_strtol_proto;
1581 extern const struct bpf_func_proto bpf_strtoul_proto;
1582 extern const struct bpf_func_proto bpf_tcp_sock_proto;
1583 extern const struct bpf_func_proto bpf_jiffies64_proto;
1584 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
1585 extern const struct bpf_func_proto bpf_event_output_data_proto;
1587 const struct bpf_func_proto *bpf_tracing_func_proto(
1588 enum bpf_func_id func_id, const struct bpf_prog *prog);
1590 /* Shared helpers among cBPF and eBPF. */
1591 void bpf_user_rnd_init_once(void);
1592 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1593 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1595 #if defined(CONFIG_NET)
1596 bool bpf_sock_common_is_valid_access(int off, int size,
1597 enum bpf_access_type type,
1598 struct bpf_insn_access_aux *info);
1599 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1600 struct bpf_insn_access_aux *info);
1601 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1602 const struct bpf_insn *si,
1603 struct bpf_insn *insn_buf,
1604 struct bpf_prog *prog,
1607 static inline bool bpf_sock_common_is_valid_access(int off, int size,
1608 enum bpf_access_type type,
1609 struct bpf_insn_access_aux *info)
1613 static inline bool bpf_sock_is_valid_access(int off, int size,
1614 enum bpf_access_type type,
1615 struct bpf_insn_access_aux *info)
1619 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1620 const struct bpf_insn *si,
1621 struct bpf_insn *insn_buf,
1622 struct bpf_prog *prog,
1630 struct sk_reuseport_kern {
1631 struct sk_buff *skb;
1633 struct sock *selected_sk;
1639 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1640 struct bpf_insn_access_aux *info);
1642 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1643 const struct bpf_insn *si,
1644 struct bpf_insn *insn_buf,
1645 struct bpf_prog *prog,
1648 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1649 struct bpf_insn_access_aux *info);
1651 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1652 const struct bpf_insn *si,
1653 struct bpf_insn *insn_buf,
1654 struct bpf_prog *prog,
1657 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1658 enum bpf_access_type type,
1659 struct bpf_insn_access_aux *info)
1664 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1665 const struct bpf_insn *si,
1666 struct bpf_insn *insn_buf,
1667 struct bpf_prog *prog,
1672 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1673 enum bpf_access_type type,
1674 struct bpf_insn_access_aux *info)
1679 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1680 const struct bpf_insn *si,
1681 struct bpf_insn *insn_buf,
1682 struct bpf_prog *prog,
1687 #endif /* CONFIG_INET */
1689 enum bpf_text_poke_type {
1694 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1695 void *addr1, void *addr2);
1697 #endif /* _LINUX_BPF_H */