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>
21 struct bpf_verifier_env;
22 struct bpf_verifier_log;
30 struct exception_table_entry;
32 extern struct idr btf_idr;
33 extern spinlock_t btf_idr_lock;
35 /* map is generic key/value storage optionally accesible by eBPF programs */
37 /* funcs callable from userspace (via syscall) */
38 int (*map_alloc_check)(union bpf_attr *attr);
39 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
40 void (*map_release)(struct bpf_map *map, struct file *map_file);
41 void (*map_free)(struct bpf_map *map);
42 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
43 void (*map_release_uref)(struct bpf_map *map);
44 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
46 /* funcs callable from userspace and from eBPF programs */
47 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
48 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
49 int (*map_delete_elem)(struct bpf_map *map, void *key);
50 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
51 int (*map_pop_elem)(struct bpf_map *map, void *value);
52 int (*map_peek_elem)(struct bpf_map *map, void *value);
54 /* funcs called by prog_array and perf_event_array map */
55 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
57 void (*map_fd_put_ptr)(void *ptr);
58 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
59 u32 (*map_fd_sys_lookup_elem)(void *ptr);
60 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
62 int (*map_check_btf)(const struct bpf_map *map,
63 const struct btf *btf,
64 const struct btf_type *key_type,
65 const struct btf_type *value_type);
67 /* Direct value access helpers. */
68 int (*map_direct_value_addr)(const struct bpf_map *map,
70 int (*map_direct_value_meta)(const struct bpf_map *map,
72 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
75 struct bpf_map_memory {
77 struct user_struct *user;
81 /* The first two cachelines with read-mostly members of which some
82 * are also accessed in fast-path (e.g. ops, max_entries).
84 const struct bpf_map_ops *ops ____cacheline_aligned;
85 struct bpf_map *inner_map_meta;
86 #ifdef CONFIG_SECURITY
89 enum bpf_map_type map_type;
94 int spin_lock_off; /* >=0 valid offset, <0 error */
98 u32 btf_value_type_id;
100 struct bpf_map_memory memory;
101 char name[BPF_OBJ_NAME_LEN];
103 bool frozen; /* write-once; write-protected by freeze_mutex */
106 /* The 3rd and 4th cacheline with misc members to avoid false sharing
107 * particularly with refcounting.
109 atomic64_t refcnt ____cacheline_aligned;
111 struct work_struct work;
112 struct mutex freeze_mutex;
113 u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
116 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
118 return map->spin_lock_off >= 0;
121 static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
123 if (likely(!map_value_has_spin_lock(map)))
125 *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
126 (struct bpf_spin_lock){};
129 /* copy everything but bpf_spin_lock */
130 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
132 if (unlikely(map_value_has_spin_lock(map))) {
133 u32 off = map->spin_lock_off;
135 memcpy(dst, src, off);
136 memcpy(dst + off + sizeof(struct bpf_spin_lock),
137 src + off + sizeof(struct bpf_spin_lock),
138 map->value_size - off - sizeof(struct bpf_spin_lock));
140 memcpy(dst, src, map->value_size);
143 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
146 struct bpf_offload_dev;
147 struct bpf_offloaded_map;
149 struct bpf_map_dev_ops {
150 int (*map_get_next_key)(struct bpf_offloaded_map *map,
151 void *key, void *next_key);
152 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
153 void *key, void *value);
154 int (*map_update_elem)(struct bpf_offloaded_map *map,
155 void *key, void *value, u64 flags);
156 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
159 struct bpf_offloaded_map {
161 struct net_device *netdev;
162 const struct bpf_map_dev_ops *dev_ops;
164 struct list_head offloads;
167 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
169 return container_of(map, struct bpf_offloaded_map, map);
172 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
174 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
177 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
179 return map->btf && map->ops->map_seq_show_elem;
182 int map_check_no_btf(const struct bpf_map *map,
183 const struct btf *btf,
184 const struct btf_type *key_type,
185 const struct btf_type *value_type);
187 extern const struct bpf_map_ops bpf_map_offload_ops;
189 /* function argument constraints */
191 ARG_DONTCARE = 0, /* unused argument in helper function */
193 /* the following constraints used to prototype
194 * bpf_map_lookup/update/delete_elem() functions
196 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
197 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
198 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
199 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
200 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
202 /* the following constraints used to prototype bpf_memcmp() and other
203 * functions that access data on eBPF program stack
205 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
206 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
207 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
208 * helper function must fill all bytes or clear
209 * them in error case.
212 ARG_CONST_SIZE, /* number of bytes accessed from memory */
213 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
215 ARG_PTR_TO_CTX, /* pointer to context */
216 ARG_ANYTHING, /* any (initialized) argument is ok */
217 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
218 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
219 ARG_PTR_TO_INT, /* pointer to int */
220 ARG_PTR_TO_LONG, /* pointer to long */
221 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
222 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
225 /* type of values returned from helper functions */
226 enum bpf_return_type {
227 RET_INTEGER, /* function returns integer */
228 RET_VOID, /* function doesn't return anything */
229 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
230 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
231 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
232 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
233 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
236 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
237 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
238 * instructions after verifying
240 struct bpf_func_proto {
241 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
244 enum bpf_return_type ret_type;
247 enum bpf_arg_type arg1_type;
248 enum bpf_arg_type arg2_type;
249 enum bpf_arg_type arg3_type;
250 enum bpf_arg_type arg4_type;
251 enum bpf_arg_type arg5_type;
253 enum bpf_arg_type arg_type[5];
255 int *btf_id; /* BTF ids of arguments */
258 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
259 * the first argument to eBPF programs.
260 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
264 enum bpf_access_type {
269 /* types of values stored in eBPF registers */
270 /* Pointer types represent:
273 * pointer + (u16) var
274 * pointer + (u16) var + imm
275 * if (range > 0) then [ptr, ptr + range - off) is safe to access
276 * if (id > 0) means that some 'var' was added
277 * if (off > 0) means that 'imm' was added
280 NOT_INIT = 0, /* nothing was written into register */
281 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
282 PTR_TO_CTX, /* reg points to bpf_context */
283 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
284 PTR_TO_MAP_VALUE, /* reg points to map element value */
285 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
286 PTR_TO_STACK, /* reg == frame_pointer + offset */
287 PTR_TO_PACKET_META, /* skb->data - meta_len */
288 PTR_TO_PACKET, /* reg points to skb->data */
289 PTR_TO_PACKET_END, /* skb->data + headlen */
290 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
291 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
292 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
293 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
294 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
295 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
296 PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
297 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
298 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
299 PTR_TO_BTF_ID, /* reg points to kernel struct */
302 /* The information passed from prog-specific *_is_valid_access
303 * back to the verifier.
305 struct bpf_insn_access_aux {
306 enum bpf_reg_type reg_type;
311 struct bpf_verifier_log *log; /* for verbose logs */
315 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
317 aux->ctx_field_size = size;
320 struct bpf_prog_ops {
321 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
322 union bpf_attr __user *uattr);
325 struct bpf_verifier_ops {
326 /* return eBPF function prototype for verification */
327 const struct bpf_func_proto *
328 (*get_func_proto)(enum bpf_func_id func_id,
329 const struct bpf_prog *prog);
331 /* return true if 'size' wide access at offset 'off' within bpf_context
332 * with 'type' (read or write) is allowed
334 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
335 const struct bpf_prog *prog,
336 struct bpf_insn_access_aux *info);
337 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
338 const struct bpf_prog *prog);
339 int (*gen_ld_abs)(const struct bpf_insn *orig,
340 struct bpf_insn *insn_buf);
341 u32 (*convert_ctx_access)(enum bpf_access_type type,
342 const struct bpf_insn *src,
343 struct bpf_insn *dst,
344 struct bpf_prog *prog, u32 *target_size);
347 struct bpf_prog_offload_ops {
348 /* verifier basic callbacks */
349 int (*insn_hook)(struct bpf_verifier_env *env,
350 int insn_idx, int prev_insn_idx);
351 int (*finalize)(struct bpf_verifier_env *env);
352 /* verifier optimization callbacks (called after .finalize) */
353 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
354 struct bpf_insn *insn);
355 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
356 /* program management callbacks */
357 int (*prepare)(struct bpf_prog *prog);
358 int (*translate)(struct bpf_prog *prog);
359 void (*destroy)(struct bpf_prog *prog);
362 struct bpf_prog_offload {
363 struct bpf_prog *prog;
364 struct net_device *netdev;
365 struct bpf_offload_dev *offdev;
367 struct list_head offloads;
374 enum bpf_cgroup_storage_type {
375 BPF_CGROUP_STORAGE_SHARED,
376 BPF_CGROUP_STORAGE_PERCPU,
377 __BPF_CGROUP_STORAGE_MAX
380 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
382 /* The longest tracepoint has 12 args.
383 * See include/trace/bpf_probe.h
385 #define MAX_BPF_FUNC_ARGS 12
387 struct bpf_prog_stats {
390 struct u64_stats_sync syncp;
391 } __aligned(2 * sizeof(u64));
393 struct btf_func_model {
396 u8 arg_size[MAX_BPF_FUNC_ARGS];
399 /* Restore arguments before returning from trampoline to let original function
400 * continue executing. This flag is used for fentry progs when there are no
403 #define BPF_TRAMP_F_RESTORE_REGS BIT(0)
404 /* Call original function after fentry progs, but before fexit progs.
405 * Makes sense for fentry/fexit, normal calls and indirect calls.
407 #define BPF_TRAMP_F_CALL_ORIG BIT(1)
408 /* Skip current frame and return to parent. Makes sense for fentry/fexit
409 * programs only. Should not be used with normal calls and indirect calls.
411 #define BPF_TRAMP_F_SKIP_FRAME BIT(2)
413 /* Different use cases for BPF trampoline:
414 * 1. replace nop at the function entry (kprobe equivalent)
415 * flags = BPF_TRAMP_F_RESTORE_REGS
416 * fentry = a set of programs to run before returning from trampoline
418 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
419 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
420 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
421 * fentry = a set of program to run before calling original function
422 * fexit = a set of program to run after original function
424 * 3. replace direct call instruction anywhere in the function body
425 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
427 * fentry = a set of programs to run before returning from trampoline
428 * With flags = BPF_TRAMP_F_CALL_ORIG
429 * orig_call = original callback addr or direct function addr
430 * fentry = a set of program to run before calling original function
431 * fexit = a set of program to run after original function
433 int arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags,
434 struct bpf_prog **fentry_progs, int fentry_cnt,
435 struct bpf_prog **fexit_progs, int fexit_cnt,
437 /* these two functions are called from generated trampoline */
438 u64 notrace __bpf_prog_enter(void);
439 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
441 enum bpf_tramp_prog_type {
447 struct bpf_trampoline {
448 /* hlist for trampoline_table */
449 struct hlist_node hlist;
450 /* serializes access to fields of this trampoline */
455 struct btf_func_model model;
458 /* list of BPF programs using this trampoline */
459 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
460 /* Number of attached programs. A counter per kind. */
461 int progs_cnt[BPF_TRAMP_MAX];
462 /* Executable image of trampoline */
466 #ifdef CONFIG_BPF_JIT
467 struct bpf_trampoline *bpf_trampoline_lookup(u64 key);
468 int bpf_trampoline_link_prog(struct bpf_prog *prog);
469 int bpf_trampoline_unlink_prog(struct bpf_prog *prog);
470 void bpf_trampoline_put(struct bpf_trampoline *tr);
472 static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
476 static inline int bpf_trampoline_link_prog(struct bpf_prog *prog)
480 static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
484 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
487 struct bpf_func_info_aux {
491 struct bpf_prog_aux {
499 u32 func_cnt; /* used by non-func prog as the number of func progs */
500 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
501 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
502 struct bpf_prog *linked_prog;
503 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
504 bool offload_requested;
505 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
506 bool func_proto_unreliable;
507 enum bpf_tramp_prog_type trampoline_prog_type;
508 struct bpf_trampoline *trampoline;
509 struct hlist_node tramp_hlist;
510 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
511 const struct btf_type *attach_func_proto;
512 /* function name for valid attach_btf_id */
513 const char *attach_func_name;
514 struct bpf_prog **func;
515 void *jit_data; /* JIT specific data. arch dependent */
516 struct latch_tree_node ksym_tnode;
517 struct list_head ksym_lnode;
518 const struct bpf_prog_ops *ops;
519 struct bpf_map **used_maps;
520 struct bpf_prog *prog;
521 struct user_struct *user;
522 u64 load_time; /* ns since boottime */
523 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
524 char name[BPF_OBJ_NAME_LEN];
525 #ifdef CONFIG_SECURITY
528 struct bpf_prog_offload *offload;
530 struct bpf_func_info *func_info;
531 struct bpf_func_info_aux *func_info_aux;
532 /* bpf_line_info loaded from userspace. linfo->insn_off
533 * has the xlated insn offset.
534 * Both the main and sub prog share the same linfo.
535 * The subprog can access its first linfo by
536 * using the linfo_idx.
538 struct bpf_line_info *linfo;
539 /* jited_linfo is the jited addr of the linfo. It has a
540 * one to one mapping to linfo:
541 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
542 * Both the main and sub prog share the same jited_linfo.
543 * The subprog can access its first jited_linfo by
544 * using the linfo_idx.
549 /* subprog can use linfo_idx to access its first linfo and
551 * main prog always has linfo_idx == 0
555 struct exception_table_entry *extable;
556 struct bpf_prog_stats __percpu *stats;
558 struct work_struct work;
567 /* 'ownership' of prog_array is claimed by the first program that
568 * is going to use this map or by the first program which FD is stored
569 * in the map to make sure that all callers and callees have the same
570 * prog_type and JITed flag
572 enum bpf_prog_type owner_prog_type;
575 char value[0] __aligned(8);
576 void *ptrs[0] __aligned(8);
577 void __percpu *pptrs[0] __aligned(8);
581 #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
582 #define MAX_TAIL_CALL_CNT 32
584 #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
585 BPF_F_RDONLY_PROG | \
589 #define BPF_MAP_CAN_READ BIT(0)
590 #define BPF_MAP_CAN_WRITE BIT(1)
592 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
594 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
596 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
599 if (access_flags & BPF_F_RDONLY_PROG)
600 return BPF_MAP_CAN_READ;
601 else if (access_flags & BPF_F_WRONLY_PROG)
602 return BPF_MAP_CAN_WRITE;
604 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
607 static inline bool bpf_map_flags_access_ok(u32 access_flags)
609 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
610 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
613 struct bpf_event_entry {
614 struct perf_event *event;
615 struct file *perf_file;
616 struct file *map_file;
620 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
621 int bpf_prog_calc_tag(struct bpf_prog *fp);
622 const char *kernel_type_name(u32 btf_type_id);
624 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
626 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
627 unsigned long off, unsigned long len);
628 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
629 const struct bpf_insn *src,
630 struct bpf_insn *dst,
631 struct bpf_prog *prog,
634 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
635 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
637 /* an array of programs to be executed under rcu_lock.
640 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
642 * the structure returned by bpf_prog_array_alloc() should be populated
643 * with program pointers and the last pointer must be NULL.
644 * The user has to keep refcnt on the program and make sure the program
645 * is removed from the array before bpf_prog_put().
646 * The 'struct bpf_prog_array *' should only be replaced with xchg()
647 * since other cpus are walking the array of pointers in parallel.
649 struct bpf_prog_array_item {
650 struct bpf_prog *prog;
651 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
654 struct bpf_prog_array {
656 struct bpf_prog_array_item items[0];
659 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
660 void bpf_prog_array_free(struct bpf_prog_array *progs);
661 int bpf_prog_array_length(struct bpf_prog_array *progs);
662 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
663 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
664 __u32 __user *prog_ids, u32 cnt);
666 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
667 struct bpf_prog *old_prog);
668 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
669 u32 *prog_ids, u32 request_cnt,
671 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
672 struct bpf_prog *exclude_prog,
673 struct bpf_prog *include_prog,
674 struct bpf_prog_array **new_array);
676 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
678 struct bpf_prog_array_item *_item; \
679 struct bpf_prog *_prog; \
680 struct bpf_prog_array *_array; \
684 _array = rcu_dereference(array); \
685 if (unlikely(check_non_null && !_array))\
687 _item = &_array->items[0]; \
688 while ((_prog = READ_ONCE(_item->prog))) { \
689 bpf_cgroup_storage_set(_item->cgroup_storage); \
690 _ret &= func(_prog, ctx); \
699 /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
700 * so BPF programs can request cwr for TCP packets.
702 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
703 * packet. This macro changes the behavior so the low order bit
704 * indicates whether the packet should be dropped (0) or not (1)
705 * and the next bit is a congestion notification bit. This could be
706 * used by TCP to call tcp_enter_cwr()
708 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
711 * 2: drop packet and cn
712 * 3: keep packet and cn
714 * This macro then converts it to one of the NET_XMIT or an error
715 * code that is then interpreted as drop packet (and no cn):
716 * 0: NET_XMIT_SUCCESS skb should be transmitted
717 * 1: NET_XMIT_DROP skb should be dropped and cn
718 * 2: NET_XMIT_CN skb should be transmitted and cn
719 * 3: -EPERM skb should be dropped
721 #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
723 struct bpf_prog_array_item *_item; \
724 struct bpf_prog *_prog; \
725 struct bpf_prog_array *_array; \
731 _array = rcu_dereference(array); \
732 _item = &_array->items[0]; \
733 while ((_prog = READ_ONCE(_item->prog))) { \
734 bpf_cgroup_storage_set(_item->cgroup_storage); \
735 ret = func(_prog, ctx); \
743 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
745 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
749 #define BPF_PROG_RUN_ARRAY(array, ctx, func) \
750 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
752 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
753 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
755 #ifdef CONFIG_BPF_SYSCALL
756 DECLARE_PER_CPU(int, bpf_prog_active);
758 extern const struct file_operations bpf_map_fops;
759 extern const struct file_operations bpf_prog_fops;
761 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
762 extern const struct bpf_prog_ops _name ## _prog_ops; \
763 extern const struct bpf_verifier_ops _name ## _verifier_ops;
764 #define BPF_MAP_TYPE(_id, _ops) \
765 extern const struct bpf_map_ops _ops;
766 #include <linux/bpf_types.h>
770 extern const struct bpf_prog_ops bpf_offload_prog_ops;
771 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
772 extern const struct bpf_verifier_ops xdp_analyzer_ops;
774 struct bpf_prog *bpf_prog_get(u32 ufd);
775 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
777 void bpf_prog_add(struct bpf_prog *prog, int i);
778 void bpf_prog_sub(struct bpf_prog *prog, int i);
779 void bpf_prog_inc(struct bpf_prog *prog);
780 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
781 void bpf_prog_put(struct bpf_prog *prog);
782 int __bpf_prog_charge(struct user_struct *user, u32 pages);
783 void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
785 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
786 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
788 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
789 struct bpf_map *__bpf_map_get(struct fd f);
790 void bpf_map_inc(struct bpf_map *map);
791 void bpf_map_inc_with_uref(struct bpf_map *map);
792 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
793 void bpf_map_put_with_uref(struct bpf_map *map);
794 void bpf_map_put(struct bpf_map *map);
795 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
796 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
797 int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size);
798 void bpf_map_charge_finish(struct bpf_map_memory *mem);
799 void bpf_map_charge_move(struct bpf_map_memory *dst,
800 struct bpf_map_memory *src);
801 void *bpf_map_area_alloc(size_t size, int numa_node);
802 void *bpf_map_area_mmapable_alloc(size_t size, int numa_node);
803 void bpf_map_area_free(void *base);
804 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
806 extern int sysctl_unprivileged_bpf_disabled;
808 int bpf_map_new_fd(struct bpf_map *map, int flags);
809 int bpf_prog_new_fd(struct bpf_prog *prog);
811 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
812 int bpf_obj_get_user(const char __user *pathname, int flags);
814 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
815 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
816 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
818 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
821 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
823 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
824 void *key, void *value, u64 map_flags);
825 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
826 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
827 void *key, void *value, u64 map_flags);
828 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
830 int bpf_get_file_flag(int flags);
831 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
834 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
835 * forced to use 'long' read/writes to try to atomically copy long counters.
836 * Best-effort only. No barriers here, since it _will_ race with concurrent
837 * updates from BPF programs. Called from bpf syscall and mostly used with
838 * size 8 or 16 bytes, so ask compiler to inline it.
840 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
842 const long *lsrc = src;
845 size /= sizeof(long);
850 /* verify correctness of eBPF program */
851 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
852 union bpf_attr __user *uattr);
853 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
859 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
860 struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
861 void __dev_map_flush(struct bpf_map *map);
862 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
863 struct net_device *dev_rx);
864 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
865 struct bpf_prog *xdp_prog);
867 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
868 void __cpu_map_flush(struct bpf_map *map);
869 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
870 struct net_device *dev_rx);
872 /* Return map's numa specified by userspace */
873 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
875 return (attr->map_flags & BPF_F_NUMA_NODE) ?
876 attr->numa_node : NUMA_NO_NODE;
879 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
880 int array_map_alloc_check(union bpf_attr *attr);
882 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
883 union bpf_attr __user *uattr);
884 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
885 union bpf_attr __user *uattr);
886 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
887 const union bpf_attr *kattr,
888 union bpf_attr __user *uattr);
889 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
890 const struct bpf_prog *prog,
891 struct bpf_insn_access_aux *info);
892 int btf_struct_access(struct bpf_verifier_log *log,
893 const struct btf_type *t, int off, int size,
894 enum bpf_access_type atype,
896 int btf_resolve_helper_id(struct bpf_verifier_log *log,
897 const struct bpf_func_proto *fn, int);
899 int btf_distill_func_proto(struct bpf_verifier_log *log,
901 const struct btf_type *func_proto,
902 const char *func_name,
903 struct btf_func_model *m);
905 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog);
907 #else /* !CONFIG_BPF_SYSCALL */
908 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
910 return ERR_PTR(-EOPNOTSUPP);
913 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
914 enum bpf_prog_type type,
917 return ERR_PTR(-EOPNOTSUPP);
920 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
924 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
928 static inline void bpf_prog_put(struct bpf_prog *prog)
932 static inline void bpf_prog_inc(struct bpf_prog *prog)
936 static inline struct bpf_prog *__must_check
937 bpf_prog_inc_not_zero(struct bpf_prog *prog)
939 return ERR_PTR(-EOPNOTSUPP);
942 static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
947 static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
951 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
956 static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
962 static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
968 static inline void __dev_map_flush(struct bpf_map *map)
973 struct bpf_dtab_netdev;
976 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
977 struct net_device *dev_rx)
984 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
986 struct bpf_prog *xdp_prog)
992 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
997 static inline void __cpu_map_flush(struct bpf_map *map)
1001 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1002 struct xdp_buff *xdp,
1003 struct net_device *dev_rx)
1008 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1009 enum bpf_prog_type type)
1011 return ERR_PTR(-EOPNOTSUPP);
1014 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1015 const union bpf_attr *kattr,
1016 union bpf_attr __user *uattr)
1021 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1022 const union bpf_attr *kattr,
1023 union bpf_attr __user *uattr)
1028 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1029 const union bpf_attr *kattr,
1030 union bpf_attr __user *uattr)
1034 #endif /* CONFIG_BPF_SYSCALL */
1036 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1037 enum bpf_prog_type type)
1039 return bpf_prog_get_type_dev(ufd, type, false);
1042 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1044 int bpf_prog_offload_compile(struct bpf_prog *prog);
1045 void bpf_prog_offload_destroy(struct bpf_prog *prog);
1046 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1047 struct bpf_prog *prog);
1049 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1051 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1052 int bpf_map_offload_update_elem(struct bpf_map *map,
1053 void *key, void *value, u64 flags);
1054 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1055 int bpf_map_offload_get_next_key(struct bpf_map *map,
1056 void *key, void *next_key);
1058 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1060 struct bpf_offload_dev *
1061 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1062 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1063 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1064 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1065 struct net_device *netdev);
1066 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1067 struct net_device *netdev);
1068 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1070 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1071 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1073 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1075 return aux->offload_requested;
1078 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1080 return unlikely(map->ops == &bpf_map_offload_ops);
1083 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1084 void bpf_map_offload_map_free(struct bpf_map *map);
1086 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1087 union bpf_attr *attr)
1092 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1097 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1102 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1104 return ERR_PTR(-EOPNOTSUPP);
1107 static inline void bpf_map_offload_map_free(struct bpf_map *map)
1110 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1112 #if defined(CONFIG_BPF_STREAM_PARSER)
1113 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
1114 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1116 static inline int sock_map_prog_update(struct bpf_map *map,
1117 struct bpf_prog *prog, u32 which)
1122 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1123 struct bpf_prog *prog)
1129 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1130 void bpf_sk_reuseport_detach(struct sock *sk);
1131 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1133 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1134 void *value, u64 map_flags);
1136 static inline void bpf_sk_reuseport_detach(struct sock *sk)
1140 #ifdef CONFIG_BPF_SYSCALL
1141 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1142 void *key, void *value)
1147 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1148 void *key, void *value,
1153 #endif /* CONFIG_BPF_SYSCALL */
1154 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1156 /* verifier prototypes for helper functions called from eBPF programs */
1157 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1158 extern const struct bpf_func_proto bpf_map_update_elem_proto;
1159 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1160 extern const struct bpf_func_proto bpf_map_push_elem_proto;
1161 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1162 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1164 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1165 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1166 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1167 extern const struct bpf_func_proto bpf_tail_call_proto;
1168 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1169 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1170 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1171 extern const struct bpf_func_proto bpf_get_current_comm_proto;
1172 extern const struct bpf_func_proto bpf_get_stackid_proto;
1173 extern const struct bpf_func_proto bpf_get_stack_proto;
1174 extern const struct bpf_func_proto bpf_sock_map_update_proto;
1175 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1176 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1177 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1178 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1179 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1180 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1181 extern const struct bpf_func_proto bpf_spin_lock_proto;
1182 extern const struct bpf_func_proto bpf_spin_unlock_proto;
1183 extern const struct bpf_func_proto bpf_get_local_storage_proto;
1184 extern const struct bpf_func_proto bpf_strtol_proto;
1185 extern const struct bpf_func_proto bpf_strtoul_proto;
1186 extern const struct bpf_func_proto bpf_tcp_sock_proto;
1188 /* Shared helpers among cBPF and eBPF. */
1189 void bpf_user_rnd_init_once(void);
1190 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1192 #if defined(CONFIG_NET)
1193 bool bpf_sock_common_is_valid_access(int off, int size,
1194 enum bpf_access_type type,
1195 struct bpf_insn_access_aux *info);
1196 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1197 struct bpf_insn_access_aux *info);
1198 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1199 const struct bpf_insn *si,
1200 struct bpf_insn *insn_buf,
1201 struct bpf_prog *prog,
1204 static inline bool bpf_sock_common_is_valid_access(int off, int size,
1205 enum bpf_access_type type,
1206 struct bpf_insn_access_aux *info)
1210 static inline bool bpf_sock_is_valid_access(int off, int size,
1211 enum bpf_access_type type,
1212 struct bpf_insn_access_aux *info)
1216 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1217 const struct bpf_insn *si,
1218 struct bpf_insn *insn_buf,
1219 struct bpf_prog *prog,
1227 struct sk_reuseport_kern {
1228 struct sk_buff *skb;
1230 struct sock *selected_sk;
1236 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1237 struct bpf_insn_access_aux *info);
1239 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1240 const struct bpf_insn *si,
1241 struct bpf_insn *insn_buf,
1242 struct bpf_prog *prog,
1245 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1246 struct bpf_insn_access_aux *info);
1248 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1249 const struct bpf_insn *si,
1250 struct bpf_insn *insn_buf,
1251 struct bpf_prog *prog,
1254 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1255 enum bpf_access_type type,
1256 struct bpf_insn_access_aux *info)
1261 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1262 const struct bpf_insn *si,
1263 struct bpf_insn *insn_buf,
1264 struct bpf_prog *prog,
1269 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1270 enum bpf_access_type type,
1271 struct bpf_insn_access_aux *info)
1276 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1277 const struct bpf_insn *si,
1278 struct bpf_insn *insn_buf,
1279 struct bpf_prog *prog,
1284 #endif /* CONFIG_INET */
1286 enum bpf_text_poke_type {
1287 BPF_MOD_NOP_TO_CALL,
1288 BPF_MOD_CALL_TO_CALL,
1289 BPF_MOD_CALL_TO_NOP,
1291 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1292 void *addr1, void *addr2);
1294 #endif /* _LINUX_BPF_H */