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/wait.h>
16 #include <linux/u64_stats_sync.h>
17 #include <linux/refcount.h>
18 #include <linux/mutex.h>
20 struct bpf_verifier_env;
21 struct bpf_verifier_log;
29 struct exception_table_entry;
31 extern struct idr btf_idr;
32 extern spinlock_t btf_idr_lock;
34 /* map is generic key/value storage optionally accesible by eBPF programs */
36 /* funcs callable from userspace (via syscall) */
37 int (*map_alloc_check)(union bpf_attr *attr);
38 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
39 void (*map_release)(struct bpf_map *map, struct file *map_file);
40 void (*map_free)(struct bpf_map *map);
41 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
42 void (*map_release_uref)(struct bpf_map *map);
43 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
45 /* funcs callable from userspace and from eBPF programs */
46 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
47 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
48 int (*map_delete_elem)(struct bpf_map *map, void *key);
49 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
50 int (*map_pop_elem)(struct bpf_map *map, void *value);
51 int (*map_peek_elem)(struct bpf_map *map, void *value);
53 /* funcs called by prog_array and perf_event_array map */
54 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
56 void (*map_fd_put_ptr)(void *ptr);
57 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
58 u32 (*map_fd_sys_lookup_elem)(void *ptr);
59 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
61 int (*map_check_btf)(const struct bpf_map *map,
62 const struct btf *btf,
63 const struct btf_type *key_type,
64 const struct btf_type *value_type);
66 /* Direct value access helpers. */
67 int (*map_direct_value_addr)(const struct bpf_map *map,
69 int (*map_direct_value_meta)(const struct bpf_map *map,
73 struct bpf_map_memory {
75 struct user_struct *user;
79 /* The first two cachelines with read-mostly members of which some
80 * are also accessed in fast-path (e.g. ops, max_entries).
82 const struct bpf_map_ops *ops ____cacheline_aligned;
83 struct bpf_map *inner_map_meta;
84 #ifdef CONFIG_SECURITY
87 enum bpf_map_type map_type;
92 int spin_lock_off; /* >=0 valid offset, <0 error */
96 u32 btf_value_type_id;
98 struct bpf_map_memory memory;
100 bool frozen; /* write-once */
103 /* The 3rd and 4th cacheline with misc members to avoid false sharing
104 * particularly with refcounting.
106 atomic_t refcnt ____cacheline_aligned;
108 struct work_struct work;
109 char name[BPF_OBJ_NAME_LEN];
112 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
114 return map->spin_lock_off >= 0;
117 static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
119 if (likely(!map_value_has_spin_lock(map)))
121 *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
122 (struct bpf_spin_lock){};
125 /* copy everything but bpf_spin_lock */
126 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
128 if (unlikely(map_value_has_spin_lock(map))) {
129 u32 off = map->spin_lock_off;
131 memcpy(dst, src, off);
132 memcpy(dst + off + sizeof(struct bpf_spin_lock),
133 src + off + sizeof(struct bpf_spin_lock),
134 map->value_size - off - sizeof(struct bpf_spin_lock));
136 memcpy(dst, src, map->value_size);
139 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
142 struct bpf_offload_dev;
143 struct bpf_offloaded_map;
145 struct bpf_map_dev_ops {
146 int (*map_get_next_key)(struct bpf_offloaded_map *map,
147 void *key, void *next_key);
148 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
149 void *key, void *value);
150 int (*map_update_elem)(struct bpf_offloaded_map *map,
151 void *key, void *value, u64 flags);
152 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
155 struct bpf_offloaded_map {
157 struct net_device *netdev;
158 const struct bpf_map_dev_ops *dev_ops;
160 struct list_head offloads;
163 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
165 return container_of(map, struct bpf_offloaded_map, map);
168 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
170 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
173 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
175 return map->btf && map->ops->map_seq_show_elem;
178 int map_check_no_btf(const struct bpf_map *map,
179 const struct btf *btf,
180 const struct btf_type *key_type,
181 const struct btf_type *value_type);
183 extern const struct bpf_map_ops bpf_map_offload_ops;
185 /* function argument constraints */
187 ARG_DONTCARE = 0, /* unused argument in helper function */
189 /* the following constraints used to prototype
190 * bpf_map_lookup/update/delete_elem() functions
192 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
193 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
194 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
195 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
196 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
198 /* the following constraints used to prototype bpf_memcmp() and other
199 * functions that access data on eBPF program stack
201 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
202 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
203 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
204 * helper function must fill all bytes or clear
205 * them in error case.
208 ARG_CONST_SIZE, /* number of bytes accessed from memory */
209 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
211 ARG_PTR_TO_CTX, /* pointer to context */
212 ARG_ANYTHING, /* any (initialized) argument is ok */
213 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
214 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
215 ARG_PTR_TO_INT, /* pointer to int */
216 ARG_PTR_TO_LONG, /* pointer to long */
217 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
218 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
221 /* type of values returned from helper functions */
222 enum bpf_return_type {
223 RET_INTEGER, /* function returns integer */
224 RET_VOID, /* function doesn't return anything */
225 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
226 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
227 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
228 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
229 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
232 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
233 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
234 * instructions after verifying
236 struct bpf_func_proto {
237 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
240 enum bpf_return_type ret_type;
243 enum bpf_arg_type arg1_type;
244 enum bpf_arg_type arg2_type;
245 enum bpf_arg_type arg3_type;
246 enum bpf_arg_type arg4_type;
247 enum bpf_arg_type arg5_type;
249 enum bpf_arg_type arg_type[5];
251 int *btf_id; /* BTF ids of arguments */
254 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
255 * the first argument to eBPF programs.
256 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
260 enum bpf_access_type {
265 /* types of values stored in eBPF registers */
266 /* Pointer types represent:
269 * pointer + (u16) var
270 * pointer + (u16) var + imm
271 * if (range > 0) then [ptr, ptr + range - off) is safe to access
272 * if (id > 0) means that some 'var' was added
273 * if (off > 0) means that 'imm' was added
276 NOT_INIT = 0, /* nothing was written into register */
277 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
278 PTR_TO_CTX, /* reg points to bpf_context */
279 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
280 PTR_TO_MAP_VALUE, /* reg points to map element value */
281 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
282 PTR_TO_STACK, /* reg == frame_pointer + offset */
283 PTR_TO_PACKET_META, /* skb->data - meta_len */
284 PTR_TO_PACKET, /* reg points to skb->data */
285 PTR_TO_PACKET_END, /* skb->data + headlen */
286 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
287 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
288 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
289 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
290 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
291 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
292 PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
293 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
294 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
295 PTR_TO_BTF_ID, /* reg points to kernel struct */
298 /* The information passed from prog-specific *_is_valid_access
299 * back to the verifier.
301 struct bpf_insn_access_aux {
302 enum bpf_reg_type reg_type;
307 struct bpf_verifier_log *log; /* for verbose logs */
311 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
313 aux->ctx_field_size = size;
316 struct bpf_prog_ops {
317 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
318 union bpf_attr __user *uattr);
321 struct bpf_verifier_ops {
322 /* return eBPF function prototype for verification */
323 const struct bpf_func_proto *
324 (*get_func_proto)(enum bpf_func_id func_id,
325 const struct bpf_prog *prog);
327 /* return true if 'size' wide access at offset 'off' within bpf_context
328 * with 'type' (read or write) is allowed
330 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
331 const struct bpf_prog *prog,
332 struct bpf_insn_access_aux *info);
333 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
334 const struct bpf_prog *prog);
335 int (*gen_ld_abs)(const struct bpf_insn *orig,
336 struct bpf_insn *insn_buf);
337 u32 (*convert_ctx_access)(enum bpf_access_type type,
338 const struct bpf_insn *src,
339 struct bpf_insn *dst,
340 struct bpf_prog *prog, u32 *target_size);
343 struct bpf_prog_offload_ops {
344 /* verifier basic callbacks */
345 int (*insn_hook)(struct bpf_verifier_env *env,
346 int insn_idx, int prev_insn_idx);
347 int (*finalize)(struct bpf_verifier_env *env);
348 /* verifier optimization callbacks (called after .finalize) */
349 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
350 struct bpf_insn *insn);
351 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
352 /* program management callbacks */
353 int (*prepare)(struct bpf_prog *prog);
354 int (*translate)(struct bpf_prog *prog);
355 void (*destroy)(struct bpf_prog *prog);
358 struct bpf_prog_offload {
359 struct bpf_prog *prog;
360 struct net_device *netdev;
361 struct bpf_offload_dev *offdev;
363 struct list_head offloads;
370 enum bpf_cgroup_storage_type {
371 BPF_CGROUP_STORAGE_SHARED,
372 BPF_CGROUP_STORAGE_PERCPU,
373 __BPF_CGROUP_STORAGE_MAX
376 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
378 /* The longest tracepoint has 12 args.
379 * See include/trace/bpf_probe.h
381 #define MAX_BPF_FUNC_ARGS 12
383 struct bpf_prog_stats {
386 struct u64_stats_sync syncp;
387 } __aligned(2 * sizeof(u64));
389 struct btf_func_model {
392 u8 arg_size[MAX_BPF_FUNC_ARGS];
395 /* Restore arguments before returning from trampoline to let original function
396 * continue executing. This flag is used for fentry progs when there are no
399 #define BPF_TRAMP_F_RESTORE_REGS BIT(0)
400 /* Call original function after fentry progs, but before fexit progs.
401 * Makes sense for fentry/fexit, normal calls and indirect calls.
403 #define BPF_TRAMP_F_CALL_ORIG BIT(1)
404 /* Skip current frame and return to parent. Makes sense for fentry/fexit
405 * programs only. Should not be used with normal calls and indirect calls.
407 #define BPF_TRAMP_F_SKIP_FRAME BIT(2)
409 /* Different use cases for BPF trampoline:
410 * 1. replace nop at the function entry (kprobe equivalent)
411 * flags = BPF_TRAMP_F_RESTORE_REGS
412 * fentry = a set of programs to run before returning from trampoline
414 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
415 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
416 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
417 * fentry = a set of program to run before calling original function
418 * fexit = a set of program to run after original function
420 * 3. replace direct call instruction anywhere in the function body
421 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
423 * fentry = a set of programs to run before returning from trampoline
424 * With flags = BPF_TRAMP_F_CALL_ORIG
425 * orig_call = original callback addr or direct function addr
426 * fentry = a set of program to run before calling original function
427 * fexit = a set of program to run after original function
429 int arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags,
430 struct bpf_prog **fentry_progs, int fentry_cnt,
431 struct bpf_prog **fexit_progs, int fexit_cnt,
433 /* these two functions are called from generated trampoline */
434 u64 notrace __bpf_prog_enter(void);
435 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
437 enum bpf_tramp_prog_type {
443 struct bpf_trampoline {
444 /* hlist for trampoline_table */
445 struct hlist_node hlist;
446 /* serializes access to fields of this trampoline */
451 struct btf_func_model model;
454 /* list of BPF programs using this trampoline */
455 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
456 /* Number of attached programs. A counter per kind. */
457 int progs_cnt[BPF_TRAMP_MAX];
458 /* Executable image of trampoline */
462 #ifdef CONFIG_BPF_JIT
463 struct bpf_trampoline *bpf_trampoline_lookup(u64 key);
464 int bpf_trampoline_link_prog(struct bpf_prog *prog);
465 int bpf_trampoline_unlink_prog(struct bpf_prog *prog);
466 void bpf_trampoline_put(struct bpf_trampoline *tr);
468 static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
472 static inline int bpf_trampoline_link_prog(struct bpf_prog *prog)
476 static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
480 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
483 struct bpf_prog_aux {
491 u32 func_cnt; /* used by non-func prog as the number of func progs */
492 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
493 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
494 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
495 bool offload_requested;
496 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
497 enum bpf_tramp_prog_type trampoline_prog_type;
498 struct bpf_trampoline *trampoline;
499 struct hlist_node tramp_hlist;
500 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
501 const struct btf_type *attach_func_proto;
502 /* function name for valid attach_btf_id */
503 const char *attach_func_name;
504 struct bpf_prog **func;
505 void *jit_data; /* JIT specific data. arch dependent */
506 struct latch_tree_node ksym_tnode;
507 struct list_head ksym_lnode;
508 const struct bpf_prog_ops *ops;
509 struct bpf_map **used_maps;
510 struct bpf_prog *prog;
511 struct user_struct *user;
512 u64 load_time; /* ns since boottime */
513 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
514 char name[BPF_OBJ_NAME_LEN];
515 #ifdef CONFIG_SECURITY
518 struct bpf_prog_offload *offload;
520 struct bpf_func_info *func_info;
521 /* bpf_line_info loaded from userspace. linfo->insn_off
522 * has the xlated insn offset.
523 * Both the main and sub prog share the same linfo.
524 * The subprog can access its first linfo by
525 * using the linfo_idx.
527 struct bpf_line_info *linfo;
528 /* jited_linfo is the jited addr of the linfo. It has a
529 * one to one mapping to linfo:
530 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
531 * Both the main and sub prog share the same jited_linfo.
532 * The subprog can access its first jited_linfo by
533 * using the linfo_idx.
538 /* subprog can use linfo_idx to access its first linfo and
540 * main prog always has linfo_idx == 0
544 struct exception_table_entry *extable;
545 struct bpf_prog_stats __percpu *stats;
547 struct work_struct work;
556 /* 'ownership' of prog_array is claimed by the first program that
557 * is going to use this map or by the first program which FD is stored
558 * in the map to make sure that all callers and callees have the same
559 * prog_type and JITed flag
561 enum bpf_prog_type owner_prog_type;
564 char value[0] __aligned(8);
565 void *ptrs[0] __aligned(8);
566 void __percpu *pptrs[0] __aligned(8);
570 #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
571 #define MAX_TAIL_CALL_CNT 32
573 #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
574 BPF_F_RDONLY_PROG | \
578 #define BPF_MAP_CAN_READ BIT(0)
579 #define BPF_MAP_CAN_WRITE BIT(1)
581 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
583 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
585 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
588 if (access_flags & BPF_F_RDONLY_PROG)
589 return BPF_MAP_CAN_READ;
590 else if (access_flags & BPF_F_WRONLY_PROG)
591 return BPF_MAP_CAN_WRITE;
593 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
596 static inline bool bpf_map_flags_access_ok(u32 access_flags)
598 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
599 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
602 struct bpf_event_entry {
603 struct perf_event *event;
604 struct file *perf_file;
605 struct file *map_file;
609 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
610 int bpf_prog_calc_tag(struct bpf_prog *fp);
611 const char *kernel_type_name(u32 btf_type_id);
613 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
615 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
616 unsigned long off, unsigned long len);
617 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
618 const struct bpf_insn *src,
619 struct bpf_insn *dst,
620 struct bpf_prog *prog,
623 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
624 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
626 /* an array of programs to be executed under rcu_lock.
629 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
631 * the structure returned by bpf_prog_array_alloc() should be populated
632 * with program pointers and the last pointer must be NULL.
633 * The user has to keep refcnt on the program and make sure the program
634 * is removed from the array before bpf_prog_put().
635 * The 'struct bpf_prog_array *' should only be replaced with xchg()
636 * since other cpus are walking the array of pointers in parallel.
638 struct bpf_prog_array_item {
639 struct bpf_prog *prog;
640 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
643 struct bpf_prog_array {
645 struct bpf_prog_array_item items[0];
648 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
649 void bpf_prog_array_free(struct bpf_prog_array *progs);
650 int bpf_prog_array_length(struct bpf_prog_array *progs);
651 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
652 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
653 __u32 __user *prog_ids, u32 cnt);
655 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
656 struct bpf_prog *old_prog);
657 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
658 u32 *prog_ids, u32 request_cnt,
660 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
661 struct bpf_prog *exclude_prog,
662 struct bpf_prog *include_prog,
663 struct bpf_prog_array **new_array);
665 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
667 struct bpf_prog_array_item *_item; \
668 struct bpf_prog *_prog; \
669 struct bpf_prog_array *_array; \
673 _array = rcu_dereference(array); \
674 if (unlikely(check_non_null && !_array))\
676 _item = &_array->items[0]; \
677 while ((_prog = READ_ONCE(_item->prog))) { \
678 bpf_cgroup_storage_set(_item->cgroup_storage); \
679 _ret &= func(_prog, ctx); \
688 /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
689 * so BPF programs can request cwr for TCP packets.
691 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
692 * packet. This macro changes the behavior so the low order bit
693 * indicates whether the packet should be dropped (0) or not (1)
694 * and the next bit is a congestion notification bit. This could be
695 * used by TCP to call tcp_enter_cwr()
697 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
700 * 2: drop packet and cn
701 * 3: keep packet and cn
703 * This macro then converts it to one of the NET_XMIT or an error
704 * code that is then interpreted as drop packet (and no cn):
705 * 0: NET_XMIT_SUCCESS skb should be transmitted
706 * 1: NET_XMIT_DROP skb should be dropped and cn
707 * 2: NET_XMIT_CN skb should be transmitted and cn
708 * 3: -EPERM skb should be dropped
710 #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
712 struct bpf_prog_array_item *_item; \
713 struct bpf_prog *_prog; \
714 struct bpf_prog_array *_array; \
720 _array = rcu_dereference(array); \
721 _item = &_array->items[0]; \
722 while ((_prog = READ_ONCE(_item->prog))) { \
723 bpf_cgroup_storage_set(_item->cgroup_storage); \
724 ret = func(_prog, ctx); \
732 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
734 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
738 #define BPF_PROG_RUN_ARRAY(array, ctx, func) \
739 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
741 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
742 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
744 #ifdef CONFIG_BPF_SYSCALL
745 DECLARE_PER_CPU(int, bpf_prog_active);
747 extern const struct file_operations bpf_map_fops;
748 extern const struct file_operations bpf_prog_fops;
750 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
751 extern const struct bpf_prog_ops _name ## _prog_ops; \
752 extern const struct bpf_verifier_ops _name ## _verifier_ops;
753 #define BPF_MAP_TYPE(_id, _ops) \
754 extern const struct bpf_map_ops _ops;
755 #include <linux/bpf_types.h>
759 extern const struct bpf_prog_ops bpf_offload_prog_ops;
760 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
761 extern const struct bpf_verifier_ops xdp_analyzer_ops;
763 struct bpf_prog *bpf_prog_get(u32 ufd);
764 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
766 struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog, int i);
767 void bpf_prog_sub(struct bpf_prog *prog, int i);
768 struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog);
769 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
770 void bpf_prog_put(struct bpf_prog *prog);
771 int __bpf_prog_charge(struct user_struct *user, u32 pages);
772 void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
774 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
775 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
777 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
778 struct bpf_map *__bpf_map_get(struct fd f);
779 struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
780 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map,
782 void bpf_map_put_with_uref(struct bpf_map *map);
783 void bpf_map_put(struct bpf_map *map);
784 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
785 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
786 int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size);
787 void bpf_map_charge_finish(struct bpf_map_memory *mem);
788 void bpf_map_charge_move(struct bpf_map_memory *dst,
789 struct bpf_map_memory *src);
790 void *bpf_map_area_alloc(size_t size, int numa_node);
791 void bpf_map_area_free(void *base);
792 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
794 extern int sysctl_unprivileged_bpf_disabled;
796 int bpf_map_new_fd(struct bpf_map *map, int flags);
797 int bpf_prog_new_fd(struct bpf_prog *prog);
799 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
800 int bpf_obj_get_user(const char __user *pathname, int flags);
802 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
803 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
804 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
806 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
809 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
811 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
812 void *key, void *value, u64 map_flags);
813 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
814 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
815 void *key, void *value, u64 map_flags);
816 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
818 int bpf_get_file_flag(int flags);
819 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
822 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
823 * forced to use 'long' read/writes to try to atomically copy long counters.
824 * Best-effort only. No barriers here, since it _will_ race with concurrent
825 * updates from BPF programs. Called from bpf syscall and mostly used with
826 * size 8 or 16 bytes, so ask compiler to inline it.
828 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
830 const long *lsrc = src;
833 size /= sizeof(long);
838 /* verify correctness of eBPF program */
839 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
840 union bpf_attr __user *uattr);
841 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
847 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
848 struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
849 void __dev_map_flush(struct bpf_map *map);
850 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
851 struct net_device *dev_rx);
852 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
853 struct bpf_prog *xdp_prog);
855 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
856 void __cpu_map_flush(struct bpf_map *map);
857 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
858 struct net_device *dev_rx);
860 /* Return map's numa specified by userspace */
861 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
863 return (attr->map_flags & BPF_F_NUMA_NODE) ?
864 attr->numa_node : NUMA_NO_NODE;
867 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
868 int array_map_alloc_check(union bpf_attr *attr);
870 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
871 union bpf_attr __user *uattr);
872 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
873 union bpf_attr __user *uattr);
874 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
875 const union bpf_attr *kattr,
876 union bpf_attr __user *uattr);
877 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
878 const struct bpf_prog *prog,
879 struct bpf_insn_access_aux *info);
880 int btf_struct_access(struct bpf_verifier_log *log,
881 const struct btf_type *t, int off, int size,
882 enum bpf_access_type atype,
884 int btf_resolve_helper_id(struct bpf_verifier_log *log,
885 const struct bpf_func_proto *fn, int);
887 int btf_distill_func_proto(struct bpf_verifier_log *log,
889 const struct btf_type *func_proto,
890 const char *func_name,
891 struct btf_func_model *m);
893 #else /* !CONFIG_BPF_SYSCALL */
894 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
896 return ERR_PTR(-EOPNOTSUPP);
899 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
900 enum bpf_prog_type type,
903 return ERR_PTR(-EOPNOTSUPP);
906 static inline struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog,
909 return ERR_PTR(-EOPNOTSUPP);
912 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
916 static inline void bpf_prog_put(struct bpf_prog *prog)
920 static inline struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog)
922 return ERR_PTR(-EOPNOTSUPP);
925 static inline struct bpf_prog *__must_check
926 bpf_prog_inc_not_zero(struct bpf_prog *prog)
928 return ERR_PTR(-EOPNOTSUPP);
931 static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
936 static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
940 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
945 static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
951 static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
957 static inline void __dev_map_flush(struct bpf_map *map)
962 struct bpf_dtab_netdev;
965 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
966 struct net_device *dev_rx)
973 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
975 struct bpf_prog *xdp_prog)
981 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
986 static inline void __cpu_map_flush(struct bpf_map *map)
990 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
991 struct xdp_buff *xdp,
992 struct net_device *dev_rx)
997 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
998 enum bpf_prog_type type)
1000 return ERR_PTR(-EOPNOTSUPP);
1003 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1004 const union bpf_attr *kattr,
1005 union bpf_attr __user *uattr)
1010 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1011 const union bpf_attr *kattr,
1012 union bpf_attr __user *uattr)
1017 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1018 const union bpf_attr *kattr,
1019 union bpf_attr __user *uattr)
1023 #endif /* CONFIG_BPF_SYSCALL */
1025 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1026 enum bpf_prog_type type)
1028 return bpf_prog_get_type_dev(ufd, type, false);
1031 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1033 int bpf_prog_offload_compile(struct bpf_prog *prog);
1034 void bpf_prog_offload_destroy(struct bpf_prog *prog);
1035 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1036 struct bpf_prog *prog);
1038 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1040 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1041 int bpf_map_offload_update_elem(struct bpf_map *map,
1042 void *key, void *value, u64 flags);
1043 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1044 int bpf_map_offload_get_next_key(struct bpf_map *map,
1045 void *key, void *next_key);
1047 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1049 struct bpf_offload_dev *
1050 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1051 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1052 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1053 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1054 struct net_device *netdev);
1055 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1056 struct net_device *netdev);
1057 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1059 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1060 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1062 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1064 return aux->offload_requested;
1067 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1069 return unlikely(map->ops == &bpf_map_offload_ops);
1072 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1073 void bpf_map_offload_map_free(struct bpf_map *map);
1075 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1076 union bpf_attr *attr)
1081 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1086 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1091 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1093 return ERR_PTR(-EOPNOTSUPP);
1096 static inline void bpf_map_offload_map_free(struct bpf_map *map)
1099 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1101 #if defined(CONFIG_BPF_STREAM_PARSER)
1102 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
1103 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1105 static inline int sock_map_prog_update(struct bpf_map *map,
1106 struct bpf_prog *prog, u32 which)
1111 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1112 struct bpf_prog *prog)
1118 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1119 void bpf_sk_reuseport_detach(struct sock *sk);
1120 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1122 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1123 void *value, u64 map_flags);
1125 static inline void bpf_sk_reuseport_detach(struct sock *sk)
1129 #ifdef CONFIG_BPF_SYSCALL
1130 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1131 void *key, void *value)
1136 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1137 void *key, void *value,
1142 #endif /* CONFIG_BPF_SYSCALL */
1143 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1145 /* verifier prototypes for helper functions called from eBPF programs */
1146 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1147 extern const struct bpf_func_proto bpf_map_update_elem_proto;
1148 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1149 extern const struct bpf_func_proto bpf_map_push_elem_proto;
1150 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1151 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1153 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1154 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1155 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1156 extern const struct bpf_func_proto bpf_tail_call_proto;
1157 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1158 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1159 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1160 extern const struct bpf_func_proto bpf_get_current_comm_proto;
1161 extern const struct bpf_func_proto bpf_get_stackid_proto;
1162 extern const struct bpf_func_proto bpf_get_stack_proto;
1163 extern const struct bpf_func_proto bpf_sock_map_update_proto;
1164 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1165 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1166 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1167 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1168 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1169 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1170 extern const struct bpf_func_proto bpf_spin_lock_proto;
1171 extern const struct bpf_func_proto bpf_spin_unlock_proto;
1172 extern const struct bpf_func_proto bpf_get_local_storage_proto;
1173 extern const struct bpf_func_proto bpf_strtol_proto;
1174 extern const struct bpf_func_proto bpf_strtoul_proto;
1175 extern const struct bpf_func_proto bpf_tcp_sock_proto;
1177 /* Shared helpers among cBPF and eBPF. */
1178 void bpf_user_rnd_init_once(void);
1179 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1181 #if defined(CONFIG_NET)
1182 bool bpf_sock_common_is_valid_access(int off, int size,
1183 enum bpf_access_type type,
1184 struct bpf_insn_access_aux *info);
1185 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1186 struct bpf_insn_access_aux *info);
1187 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1188 const struct bpf_insn *si,
1189 struct bpf_insn *insn_buf,
1190 struct bpf_prog *prog,
1193 static inline bool bpf_sock_common_is_valid_access(int off, int size,
1194 enum bpf_access_type type,
1195 struct bpf_insn_access_aux *info)
1199 static inline bool bpf_sock_is_valid_access(int off, int size,
1200 enum bpf_access_type type,
1201 struct bpf_insn_access_aux *info)
1205 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1206 const struct bpf_insn *si,
1207 struct bpf_insn *insn_buf,
1208 struct bpf_prog *prog,
1216 struct sk_reuseport_kern {
1217 struct sk_buff *skb;
1219 struct sock *selected_sk;
1225 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1226 struct bpf_insn_access_aux *info);
1228 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1229 const struct bpf_insn *si,
1230 struct bpf_insn *insn_buf,
1231 struct bpf_prog *prog,
1234 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1235 struct bpf_insn_access_aux *info);
1237 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1238 const struct bpf_insn *si,
1239 struct bpf_insn *insn_buf,
1240 struct bpf_prog *prog,
1243 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1244 enum bpf_access_type type,
1245 struct bpf_insn_access_aux *info)
1250 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1251 const struct bpf_insn *si,
1252 struct bpf_insn *insn_buf,
1253 struct bpf_prog *prog,
1258 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1259 enum bpf_access_type type,
1260 struct bpf_insn_access_aux *info)
1265 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1266 const struct bpf_insn *si,
1267 struct bpf_insn *insn_buf,
1268 struct bpf_prog *prog,
1273 #endif /* CONFIG_INET */
1275 enum bpf_text_poke_type {
1276 BPF_MOD_NOP_TO_CALL,
1277 BPF_MOD_CALL_TO_CALL,
1278 BPF_MOD_CALL_TO_NOP,
1280 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1281 void *addr1, void *addr2);
1283 #endif /* _LINUX_BPF_H */