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