1
2
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
24struct bpf_verifier_env;
25struct bpf_verifier_log;
26struct perf_event;
27struct bpf_prog;
28struct bpf_prog_aux;
29struct bpf_map;
30struct sock;
31struct seq_file;
32struct btf;
33struct btf_type;
34struct exception_table_entry;
35struct seq_operations;
36struct bpf_iter_aux_info;
37struct bpf_local_storage;
38struct bpf_local_storage_map;
39
40extern struct idr btf_idr;
41extern spinlock_t btf_idr_lock;
42
43typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
44 struct bpf_iter_aux_info *aux);
45typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
46struct 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
54struct bpf_map_ops {
55
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
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
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 int (*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
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
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
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
117
118
119
120
121
122
123
124
125 bool (*map_meta_equal)(const struct bpf_map *meta0,
126 const struct bpf_map *meta1);
127
128
129 const char * const map_btf_name;
130 int *map_btf_id;
131
132
133 const struct bpf_iter_seq_info *iter_seq_info;
134};
135
136struct bpf_map_memory {
137 u32 pages;
138 struct user_struct *user;
139};
140
141struct bpf_map {
142
143
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;
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;
166
167
168
169
170
171 atomic64_t refcnt ____cacheline_aligned;
172 atomic64_t usercnt;
173 struct work_struct work;
174 struct mutex freeze_mutex;
175 u64 writecnt;
176};
177
178static inline bool map_value_has_spin_lock(const struct bpf_map *map)
179{
180 return map->spin_lock_off >= 0;
181}
182
183static 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
192static 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}
205void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
206 bool lock_src);
207int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
208
209struct bpf_offload_dev;
210struct bpf_offloaded_map;
211
212struct 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
222struct 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
230static 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
235static 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
240static 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
246int 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
251bool bpf_map_meta_equal(const struct bpf_map *meta0,
252 const struct bpf_map *meta1);
253
254extern const struct bpf_map_ops bpf_map_offload_ops;
255
256
257enum bpf_arg_type {
258 ARG_DONTCARE = 0,
259
260
261
262
263 ARG_CONST_MAP_PTR,
264 ARG_PTR_TO_MAP_KEY,
265 ARG_PTR_TO_MAP_VALUE,
266 ARG_PTR_TO_UNINIT_MAP_VALUE,
267 ARG_PTR_TO_MAP_VALUE_OR_NULL,
268
269
270
271
272 ARG_PTR_TO_MEM,
273 ARG_PTR_TO_MEM_OR_NULL,
274 ARG_PTR_TO_UNINIT_MEM,
275
276
277
278
279 ARG_CONST_SIZE,
280 ARG_CONST_SIZE_OR_ZERO,
281
282 ARG_PTR_TO_CTX,
283 ARG_PTR_TO_CTX_OR_NULL,
284 ARG_ANYTHING,
285 ARG_PTR_TO_SPIN_LOCK,
286 ARG_PTR_TO_SOCK_COMMON,
287 ARG_PTR_TO_INT,
288 ARG_PTR_TO_LONG,
289 ARG_PTR_TO_SOCKET,
290 ARG_PTR_TO_SOCKET_OR_NULL,
291 ARG_PTR_TO_BTF_ID,
292 ARG_PTR_TO_ALLOC_MEM,
293 ARG_PTR_TO_ALLOC_MEM_OR_NULL,
294 ARG_CONST_ALLOC_SIZE_OR_ZERO,
295 ARG_PTR_TO_BTF_ID_SOCK_COMMON,
296 ARG_PTR_TO_PERCPU_BTF_ID,
297 __BPF_ARG_TYPE_MAX,
298};
299
300
301enum bpf_return_type {
302 RET_INTEGER,
303 RET_VOID,
304 RET_PTR_TO_MAP_VALUE,
305 RET_PTR_TO_MAP_VALUE_OR_NULL,
306 RET_PTR_TO_SOCKET_OR_NULL,
307 RET_PTR_TO_TCP_SOCK_OR_NULL,
308 RET_PTR_TO_SOCK_COMMON_OR_NULL,
309 RET_PTR_TO_ALLOC_MEM_OR_NULL,
310 RET_PTR_TO_BTF_ID_OR_NULL,
311 RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL,
312 RET_PTR_TO_MEM_OR_BTF_ID,
313};
314
315
316
317
318
319struct bpf_func_proto {
320 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
321 bool gpl_only;
322 bool pkt_access;
323 enum bpf_return_type ret_type;
324 union {
325 struct {
326 enum bpf_arg_type arg1_type;
327 enum bpf_arg_type arg2_type;
328 enum bpf_arg_type arg3_type;
329 enum bpf_arg_type arg4_type;
330 enum bpf_arg_type arg5_type;
331 };
332 enum bpf_arg_type arg_type[5];
333 };
334 union {
335 struct {
336 u32 *arg1_btf_id;
337 u32 *arg2_btf_id;
338 u32 *arg3_btf_id;
339 u32 *arg4_btf_id;
340 u32 *arg5_btf_id;
341 };
342 u32 *arg_btf_id[5];
343 };
344 int *ret_btf_id;
345 bool (*allowed)(const struct bpf_prog *prog);
346};
347
348
349
350
351
352struct bpf_context;
353
354enum bpf_access_type {
355 BPF_READ = 1,
356 BPF_WRITE = 2
357};
358
359
360
361
362
363
364
365
366
367
368
369enum bpf_reg_type {
370 NOT_INIT = 0,
371 SCALAR_VALUE,
372 PTR_TO_CTX,
373 CONST_PTR_TO_MAP,
374 PTR_TO_MAP_VALUE,
375 PTR_TO_MAP_VALUE_OR_NULL,
376 PTR_TO_STACK,
377 PTR_TO_PACKET_META,
378 PTR_TO_PACKET,
379 PTR_TO_PACKET_END,
380 PTR_TO_FLOW_KEYS,
381 PTR_TO_SOCKET,
382 PTR_TO_SOCKET_OR_NULL,
383 PTR_TO_SOCK_COMMON,
384 PTR_TO_SOCK_COMMON_OR_NULL,
385 PTR_TO_TCP_SOCK,
386 PTR_TO_TCP_SOCK_OR_NULL,
387 PTR_TO_TP_BUFFER,
388 PTR_TO_XDP_SOCK,
389
390
391
392
393
394
395
396
397
398
399 PTR_TO_BTF_ID,
400
401
402
403
404 PTR_TO_BTF_ID_OR_NULL,
405 PTR_TO_MEM,
406 PTR_TO_MEM_OR_NULL,
407 PTR_TO_RDONLY_BUF,
408 PTR_TO_RDONLY_BUF_OR_NULL,
409 PTR_TO_RDWR_BUF,
410 PTR_TO_RDWR_BUF_OR_NULL,
411 PTR_TO_PERCPU_BTF_ID,
412};
413
414
415
416
417struct bpf_insn_access_aux {
418 enum bpf_reg_type reg_type;
419 union {
420 int ctx_field_size;
421 u32 btf_id;
422 };
423 struct bpf_verifier_log *log;
424};
425
426static inline void
427bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
428{
429 aux->ctx_field_size = size;
430}
431
432struct bpf_prog_ops {
433 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
434 union bpf_attr __user *uattr);
435};
436
437struct bpf_verifier_ops {
438
439 const struct bpf_func_proto *
440 (*get_func_proto)(enum bpf_func_id func_id,
441 const struct bpf_prog *prog);
442
443
444
445
446 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
447 const struct bpf_prog *prog,
448 struct bpf_insn_access_aux *info);
449 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
450 const struct bpf_prog *prog);
451 int (*gen_ld_abs)(const struct bpf_insn *orig,
452 struct bpf_insn *insn_buf);
453 u32 (*convert_ctx_access)(enum bpf_access_type type,
454 const struct bpf_insn *src,
455 struct bpf_insn *dst,
456 struct bpf_prog *prog, u32 *target_size);
457 int (*btf_struct_access)(struct bpf_verifier_log *log,
458 const struct btf_type *t, int off, int size,
459 enum bpf_access_type atype,
460 u32 *next_btf_id);
461};
462
463struct bpf_prog_offload_ops {
464
465 int (*insn_hook)(struct bpf_verifier_env *env,
466 int insn_idx, int prev_insn_idx);
467 int (*finalize)(struct bpf_verifier_env *env);
468
469 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
470 struct bpf_insn *insn);
471 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
472
473 int (*prepare)(struct bpf_prog *prog);
474 int (*translate)(struct bpf_prog *prog);
475 void (*destroy)(struct bpf_prog *prog);
476};
477
478struct bpf_prog_offload {
479 struct bpf_prog *prog;
480 struct net_device *netdev;
481 struct bpf_offload_dev *offdev;
482 void *dev_priv;
483 struct list_head offloads;
484 bool dev_state;
485 bool opt_failed;
486 void *jited_image;
487 u32 jited_len;
488};
489
490enum bpf_cgroup_storage_type {
491 BPF_CGROUP_STORAGE_SHARED,
492 BPF_CGROUP_STORAGE_PERCPU,
493 __BPF_CGROUP_STORAGE_MAX
494};
495
496#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
497
498
499
500
501#define MAX_BPF_FUNC_ARGS 12
502
503struct bpf_prog_stats {
504 u64 cnt;
505 u64 nsecs;
506 struct u64_stats_sync syncp;
507} __aligned(2 * sizeof(u64));
508
509struct btf_func_model {
510 u8 ret_size;
511 u8 nr_args;
512 u8 arg_size[MAX_BPF_FUNC_ARGS];
513};
514
515
516
517
518
519#define BPF_TRAMP_F_RESTORE_REGS BIT(0)
520
521
522
523#define BPF_TRAMP_F_CALL_ORIG BIT(1)
524
525
526
527#define BPF_TRAMP_F_SKIP_FRAME BIT(2)
528
529
530
531
532#define BPF_MAX_TRAMP_PROGS 40
533
534struct bpf_tramp_progs {
535 struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS];
536 int nr_progs;
537};
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559int arch_prepare_bpf_trampoline(void *image, void *image_end,
560 const struct btf_func_model *m, u32 flags,
561 struct bpf_tramp_progs *tprogs,
562 void *orig_call);
563
564u64 notrace __bpf_prog_enter(void);
565void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
566void notrace __bpf_prog_enter_sleepable(void);
567void notrace __bpf_prog_exit_sleepable(void);
568
569struct bpf_ksym {
570 unsigned long start;
571 unsigned long end;
572 char name[KSYM_NAME_LEN];
573 struct list_head lnode;
574 struct latch_tree_node tnode;
575 bool prog;
576};
577
578enum bpf_tramp_prog_type {
579 BPF_TRAMP_FENTRY,
580 BPF_TRAMP_FEXIT,
581 BPF_TRAMP_MODIFY_RETURN,
582 BPF_TRAMP_MAX,
583 BPF_TRAMP_REPLACE,
584};
585
586struct bpf_trampoline {
587
588 struct hlist_node hlist;
589
590 struct mutex mutex;
591 refcount_t refcnt;
592 u64 key;
593 struct {
594 struct btf_func_model model;
595 void *addr;
596 bool ftrace_managed;
597 } func;
598
599
600
601
602 struct bpf_prog *extension_prog;
603
604 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
605
606 int progs_cnt[BPF_TRAMP_MAX];
607
608 void *image;
609 u64 selector;
610 struct bpf_ksym ksym;
611};
612
613struct bpf_attach_target_info {
614 struct btf_func_model fmodel;
615 long tgt_addr;
616 const char *tgt_name;
617 const struct btf_type *tgt_type;
618};
619
620#define BPF_DISPATCHER_MAX 48
621
622struct bpf_dispatcher_prog {
623 struct bpf_prog *prog;
624 refcount_t users;
625};
626
627struct bpf_dispatcher {
628
629 struct mutex mutex;
630 void *func;
631 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
632 int num_progs;
633 void *image;
634 u32 image_off;
635 struct bpf_ksym ksym;
636};
637
638static __always_inline unsigned int bpf_dispatcher_nop_func(
639 const void *ctx,
640 const struct bpf_insn *insnsi,
641 unsigned int (*bpf_func)(const void *,
642 const struct bpf_insn *))
643{
644 return bpf_func(ctx, insnsi);
645}
646#ifdef CONFIG_BPF_JIT
647int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr);
648int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr);
649struct bpf_trampoline *bpf_trampoline_get(u64 key,
650 struct bpf_attach_target_info *tgt_info);
651void bpf_trampoline_put(struct bpf_trampoline *tr);
652#define BPF_DISPATCHER_INIT(_name) { \
653 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
654 .func = &_name##_func, \
655 .progs = {}, \
656 .num_progs = 0, \
657 .image = NULL, \
658 .image_off = 0, \
659 .ksym = { \
660 .name = #_name, \
661 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
662 }, \
663}
664
665#define DEFINE_BPF_DISPATCHER(name) \
666 noinline unsigned int bpf_dispatcher_##name##_func( \
667 const void *ctx, \
668 const struct bpf_insn *insnsi, \
669 unsigned int (*bpf_func)(const void *, \
670 const struct bpf_insn *)) \
671 { \
672 return bpf_func(ctx, insnsi); \
673 } \
674 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
675 struct bpf_dispatcher bpf_dispatcher_##name = \
676 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
677#define DECLARE_BPF_DISPATCHER(name) \
678 unsigned int bpf_dispatcher_##name##_func( \
679 const void *ctx, \
680 const struct bpf_insn *insnsi, \
681 unsigned int (*bpf_func)(const void *, \
682 const struct bpf_insn *)); \
683 extern struct bpf_dispatcher bpf_dispatcher_##name;
684#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
685#define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
686void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
687 struct bpf_prog *to);
688
689void *bpf_jit_alloc_exec_page(void);
690void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
691void bpf_image_ksym_del(struct bpf_ksym *ksym);
692void bpf_ksym_add(struct bpf_ksym *ksym);
693void bpf_ksym_del(struct bpf_ksym *ksym);
694#else
695static inline int bpf_trampoline_link_prog(struct bpf_prog *prog,
696 struct bpf_trampoline *tr)
697{
698 return -ENOTSUPP;
699}
700static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog,
701 struct bpf_trampoline *tr)
702{
703 return -ENOTSUPP;
704}
705static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
706 struct bpf_attach_target_info *tgt_info)
707{
708 return ERR_PTR(-EOPNOTSUPP);
709}
710static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
711#define DEFINE_BPF_DISPATCHER(name)
712#define DECLARE_BPF_DISPATCHER(name)
713#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
714#define BPF_DISPATCHER_PTR(name) NULL
715static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
716 struct bpf_prog *from,
717 struct bpf_prog *to) {}
718static inline bool is_bpf_image_address(unsigned long address)
719{
720 return false;
721}
722#endif
723
724struct bpf_func_info_aux {
725 u16 linkage;
726 bool unreliable;
727};
728
729enum bpf_jit_poke_reason {
730 BPF_POKE_REASON_TAIL_CALL,
731};
732
733
734struct bpf_jit_poke_descriptor {
735 void *tailcall_target;
736 void *tailcall_bypass;
737 void *bypass_addr;
738 union {
739 struct {
740 struct bpf_map *map;
741 u32 key;
742 } tail_call;
743 };
744 bool tailcall_target_stable;
745 u8 adj_off;
746 u16 reason;
747 u32 insn_idx;
748};
749
750
751struct bpf_ctx_arg_aux {
752 u32 offset;
753 enum bpf_reg_type reg_type;
754 u32 btf_id;
755};
756
757struct bpf_prog_aux {
758 atomic64_t refcnt;
759 u32 used_map_cnt;
760 u32 max_ctx_offset;
761 u32 max_pkt_offset;
762 u32 max_tp_access;
763 u32 stack_depth;
764 u32 id;
765 u32 func_cnt;
766 u32 func_idx;
767 u32 attach_btf_id;
768 u32 ctx_arg_info_size;
769 u32 max_rdonly_access;
770 u32 max_rdwr_access;
771 const struct bpf_ctx_arg_aux *ctx_arg_info;
772 struct mutex dst_mutex;
773 struct bpf_prog *dst_prog;
774 struct bpf_trampoline *dst_trampoline;
775 enum bpf_prog_type saved_dst_prog_type;
776 enum bpf_attach_type saved_dst_attach_type;
777 bool verifier_zext;
778 bool offload_requested;
779 bool attach_btf_trace;
780 bool func_proto_unreliable;
781 bool sleepable;
782 bool tail_call_reachable;
783 enum bpf_tramp_prog_type trampoline_prog_type;
784 struct hlist_node tramp_hlist;
785
786 const struct btf_type *attach_func_proto;
787
788 const char *attach_func_name;
789 struct bpf_prog **func;
790 void *jit_data;
791 struct bpf_jit_poke_descriptor *poke_tab;
792 u32 size_poke_tab;
793 struct bpf_ksym ksym;
794 const struct bpf_prog_ops *ops;
795 struct bpf_map **used_maps;
796 struct mutex used_maps_mutex;
797 struct bpf_prog *prog;
798 struct user_struct *user;
799 u64 load_time;
800 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
801 char name[BPF_OBJ_NAME_LEN];
802#ifdef CONFIG_SECURITY
803 void *security;
804#endif
805 struct bpf_prog_offload *offload;
806 struct btf *btf;
807 struct bpf_func_info *func_info;
808 struct bpf_func_info_aux *func_info_aux;
809
810
811
812
813
814
815 struct bpf_line_info *linfo;
816
817
818
819
820
821
822
823 void **jited_linfo;
824 u32 func_info_cnt;
825 u32 nr_linfo;
826
827
828
829
830 u32 linfo_idx;
831 u32 num_exentries;
832 struct exception_table_entry *extable;
833 struct bpf_prog_stats __percpu *stats;
834 union {
835 struct work_struct work;
836 struct rcu_head rcu;
837 };
838};
839
840struct bpf_array_aux {
841
842
843
844
845
846 enum bpf_prog_type type;
847 bool jited;
848
849 struct list_head poke_progs;
850 struct bpf_map *map;
851 struct mutex poke_mutex;
852 struct work_struct work;
853};
854
855struct bpf_link {
856 atomic64_t refcnt;
857 u32 id;
858 enum bpf_link_type type;
859 const struct bpf_link_ops *ops;
860 struct bpf_prog *prog;
861 struct work_struct work;
862};
863
864struct bpf_link_ops {
865 void (*release)(struct bpf_link *link);
866 void (*dealloc)(struct bpf_link *link);
867 int (*detach)(struct bpf_link *link);
868 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
869 struct bpf_prog *old_prog);
870 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
871 int (*fill_link_info)(const struct bpf_link *link,
872 struct bpf_link_info *info);
873};
874
875struct bpf_link_primer {
876 struct bpf_link *link;
877 struct file *file;
878 int fd;
879 u32 id;
880};
881
882struct bpf_struct_ops_value;
883struct btf_type;
884struct btf_member;
885
886#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
887struct bpf_struct_ops {
888 const struct bpf_verifier_ops *verifier_ops;
889 int (*init)(struct btf *btf);
890 int (*check_member)(const struct btf_type *t,
891 const struct btf_member *member);
892 int (*init_member)(const struct btf_type *t,
893 const struct btf_member *member,
894 void *kdata, const void *udata);
895 int (*reg)(void *kdata);
896 void (*unreg)(void *kdata);
897 const struct btf_type *type;
898 const struct btf_type *value_type;
899 const char *name;
900 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
901 u32 type_id;
902 u32 value_id;
903};
904
905#if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
906#define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
907const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
908void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
909bool bpf_struct_ops_get(const void *kdata);
910void bpf_struct_ops_put(const void *kdata);
911int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
912 void *value);
913static inline bool bpf_try_module_get(const void *data, struct module *owner)
914{
915 if (owner == BPF_MODULE_OWNER)
916 return bpf_struct_ops_get(data);
917 else
918 return try_module_get(owner);
919}
920static inline void bpf_module_put(const void *data, struct module *owner)
921{
922 if (owner == BPF_MODULE_OWNER)
923 bpf_struct_ops_put(data);
924 else
925 module_put(owner);
926}
927#else
928static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
929{
930 return NULL;
931}
932static inline void bpf_struct_ops_init(struct btf *btf,
933 struct bpf_verifier_log *log)
934{
935}
936static inline bool bpf_try_module_get(const void *data, struct module *owner)
937{
938 return try_module_get(owner);
939}
940static inline void bpf_module_put(const void *data, struct module *owner)
941{
942 module_put(owner);
943}
944static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
945 void *key,
946 void *value)
947{
948 return -EINVAL;
949}
950#endif
951
952struct bpf_array {
953 struct bpf_map map;
954 u32 elem_size;
955 u32 index_mask;
956 struct bpf_array_aux *aux;
957 union {
958 char value[0] __aligned(8);
959 void *ptrs[0] __aligned(8);
960 void __percpu *pptrs[0] __aligned(8);
961 };
962};
963
964#define BPF_COMPLEXITY_LIMIT_INSNS 1000000
965#define MAX_TAIL_CALL_CNT 32
966
967#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
968 BPF_F_RDONLY_PROG | \
969 BPF_F_WRONLY | \
970 BPF_F_WRONLY_PROG)
971
972#define BPF_MAP_CAN_READ BIT(0)
973#define BPF_MAP_CAN_WRITE BIT(1)
974
975static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
976{
977 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
978
979
980
981
982 if (access_flags & BPF_F_RDONLY_PROG)
983 return BPF_MAP_CAN_READ;
984 else if (access_flags & BPF_F_WRONLY_PROG)
985 return BPF_MAP_CAN_WRITE;
986 else
987 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
988}
989
990static inline bool bpf_map_flags_access_ok(u32 access_flags)
991{
992 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
993 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
994}
995
996struct bpf_event_entry {
997 struct perf_event *event;
998 struct file *perf_file;
999 struct file *map_file;
1000 struct rcu_head rcu;
1001};
1002
1003bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
1004int bpf_prog_calc_tag(struct bpf_prog *fp);
1005const char *kernel_type_name(u32 btf_type_id);
1006
1007const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
1008
1009typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
1010 unsigned long off, unsigned long len);
1011typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1012 const struct bpf_insn *src,
1013 struct bpf_insn *dst,
1014 struct bpf_prog *prog,
1015 u32 *target_size);
1016
1017u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1018 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032struct bpf_prog_array_item {
1033 struct bpf_prog *prog;
1034 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1035};
1036
1037struct bpf_prog_array {
1038 struct rcu_head rcu;
1039 struct bpf_prog_array_item items[];
1040};
1041
1042struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
1043void bpf_prog_array_free(struct bpf_prog_array *progs);
1044int bpf_prog_array_length(struct bpf_prog_array *progs);
1045bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
1046int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
1047 __u32 __user *prog_ids, u32 cnt);
1048
1049void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
1050 struct bpf_prog *old_prog);
1051int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1052int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1053 struct bpf_prog *prog);
1054int bpf_prog_array_copy_info(struct bpf_prog_array *array,
1055 u32 *prog_ids, u32 request_cnt,
1056 u32 *prog_cnt);
1057int bpf_prog_array_copy(struct bpf_prog_array *old_array,
1058 struct bpf_prog *exclude_prog,
1059 struct bpf_prog *include_prog,
1060 struct bpf_prog_array **new_array);
1061
1062#define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
1063 ({ \
1064 struct bpf_prog_array_item *_item; \
1065 struct bpf_prog *_prog; \
1066 struct bpf_prog_array *_array; \
1067 u32 _ret = 1; \
1068 migrate_disable(); \
1069 rcu_read_lock(); \
1070 _array = rcu_dereference(array); \
1071 if (unlikely(check_non_null && !_array))\
1072 goto _out; \
1073 _item = &_array->items[0]; \
1074 while ((_prog = READ_ONCE(_item->prog))) { \
1075 bpf_cgroup_storage_set(_item->cgroup_storage); \
1076 _ret &= func(_prog, ctx); \
1077 _item++; \
1078 } \
1079_out: \
1080 rcu_read_unlock(); \
1081 migrate_enable(); \
1082 _ret; \
1083 })
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107#define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
1108 ({ \
1109 struct bpf_prog_array_item *_item; \
1110 struct bpf_prog *_prog; \
1111 struct bpf_prog_array *_array; \
1112 u32 ret; \
1113 u32 _ret = 1; \
1114 u32 _cn = 0; \
1115 migrate_disable(); \
1116 rcu_read_lock(); \
1117 _array = rcu_dereference(array); \
1118 _item = &_array->items[0]; \
1119 while ((_prog = READ_ONCE(_item->prog))) { \
1120 bpf_cgroup_storage_set(_item->cgroup_storage); \
1121 ret = func(_prog, ctx); \
1122 _ret &= (ret & 1); \
1123 _cn |= (ret & 2); \
1124 _item++; \
1125 } \
1126 rcu_read_unlock(); \
1127 migrate_enable(); \
1128 if (_ret) \
1129 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
1130 else \
1131 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
1132 _ret; \
1133 })
1134
1135#define BPF_PROG_RUN_ARRAY(array, ctx, func) \
1136 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
1137
1138#define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
1139 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
1140
1141#ifdef CONFIG_BPF_SYSCALL
1142DECLARE_PER_CPU(int, bpf_prog_active);
1143extern struct mutex bpf_stats_enabled_mutex;
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157static inline void bpf_disable_instrumentation(void)
1158{
1159 migrate_disable();
1160 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1161 this_cpu_inc(bpf_prog_active);
1162 else
1163 __this_cpu_inc(bpf_prog_active);
1164}
1165
1166static inline void bpf_enable_instrumentation(void)
1167{
1168 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1169 this_cpu_dec(bpf_prog_active);
1170 else
1171 __this_cpu_dec(bpf_prog_active);
1172 migrate_enable();
1173}
1174
1175extern const struct file_operations bpf_map_fops;
1176extern const struct file_operations bpf_prog_fops;
1177extern const struct file_operations bpf_iter_fops;
1178
1179#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1180 extern const struct bpf_prog_ops _name ## _prog_ops; \
1181 extern const struct bpf_verifier_ops _name ## _verifier_ops;
1182#define BPF_MAP_TYPE(_id, _ops) \
1183 extern const struct bpf_map_ops _ops;
1184#define BPF_LINK_TYPE(_id, _name)
1185#include <linux/bpf_types.h>
1186#undef BPF_PROG_TYPE
1187#undef BPF_MAP_TYPE
1188#undef BPF_LINK_TYPE
1189
1190extern const struct bpf_prog_ops bpf_offload_prog_ops;
1191extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1192extern const struct bpf_verifier_ops xdp_analyzer_ops;
1193
1194struct bpf_prog *bpf_prog_get(u32 ufd);
1195struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1196 bool attach_drv);
1197void bpf_prog_add(struct bpf_prog *prog, int i);
1198void bpf_prog_sub(struct bpf_prog *prog, int i);
1199void bpf_prog_inc(struct bpf_prog *prog);
1200struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1201void bpf_prog_put(struct bpf_prog *prog);
1202int __bpf_prog_charge(struct user_struct *user, u32 pages);
1203void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
1204void __bpf_free_used_maps(struct bpf_prog_aux *aux,
1205 struct bpf_map **used_maps, u32 len);
1206
1207void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1208void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1209
1210struct bpf_map *bpf_map_get(u32 ufd);
1211struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1212struct bpf_map *__bpf_map_get(struct fd f);
1213void bpf_map_inc(struct bpf_map *map);
1214void bpf_map_inc_with_uref(struct bpf_map *map);
1215struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1216void bpf_map_put_with_uref(struct bpf_map *map);
1217void bpf_map_put(struct bpf_map *map);
1218int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
1219void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
1220int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
1221void bpf_map_charge_finish(struct bpf_map_memory *mem);
1222void bpf_map_charge_move(struct bpf_map_memory *dst,
1223 struct bpf_map_memory *src);
1224void *bpf_map_area_alloc(u64 size, int numa_node);
1225void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1226void bpf_map_area_free(void *base);
1227void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1228int generic_map_lookup_batch(struct bpf_map *map,
1229 const union bpf_attr *attr,
1230 union bpf_attr __user *uattr);
1231int generic_map_update_batch(struct bpf_map *map,
1232 const union bpf_attr *attr,
1233 union bpf_attr __user *uattr);
1234int generic_map_delete_batch(struct bpf_map *map,
1235 const union bpf_attr *attr,
1236 union bpf_attr __user *uattr);
1237struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1238struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
1239
1240extern int sysctl_unprivileged_bpf_disabled;
1241
1242static inline bool bpf_allow_ptr_leaks(void)
1243{
1244 return perfmon_capable();
1245}
1246
1247static inline bool bpf_allow_ptr_to_map_access(void)
1248{
1249 return perfmon_capable();
1250}
1251
1252static inline bool bpf_bypass_spec_v1(void)
1253{
1254 return perfmon_capable();
1255}
1256
1257static inline bool bpf_bypass_spec_v4(void)
1258{
1259 return perfmon_capable();
1260}
1261
1262int bpf_map_new_fd(struct bpf_map *map, int flags);
1263int bpf_prog_new_fd(struct bpf_prog *prog);
1264
1265void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1266 const struct bpf_link_ops *ops, struct bpf_prog *prog);
1267int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1268int bpf_link_settle(struct bpf_link_primer *primer);
1269void bpf_link_cleanup(struct bpf_link_primer *primer);
1270void bpf_link_inc(struct bpf_link *link);
1271void bpf_link_put(struct bpf_link *link);
1272int bpf_link_new_fd(struct bpf_link *link);
1273struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1274struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1275
1276int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1277int bpf_obj_get_user(const char __user *pathname, int flags);
1278
1279#define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1280#define DEFINE_BPF_ITER_FUNC(target, args...) \
1281 extern int bpf_iter_ ## target(args); \
1282 int __init bpf_iter_ ## target(args) { return 0; }
1283
1284struct bpf_iter_aux_info {
1285 struct bpf_map *map;
1286};
1287
1288typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1289 union bpf_iter_link_info *linfo,
1290 struct bpf_iter_aux_info *aux);
1291typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
1292typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1293 struct seq_file *seq);
1294typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1295 struct bpf_link_info *info);
1296
1297#define BPF_ITER_CTX_ARG_MAX 2
1298struct bpf_iter_reg {
1299 const char *target;
1300 bpf_iter_attach_target_t attach_target;
1301 bpf_iter_detach_target_t detach_target;
1302 bpf_iter_show_fdinfo_t show_fdinfo;
1303 bpf_iter_fill_link_info_t fill_link_info;
1304 u32 ctx_arg_info_size;
1305 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1306 const struct bpf_iter_seq_info *seq_info;
1307};
1308
1309struct bpf_iter_meta {
1310 __bpf_md_ptr(struct seq_file *, seq);
1311 u64 session_id;
1312 u64 seq_num;
1313};
1314
1315struct bpf_iter__bpf_map_elem {
1316 __bpf_md_ptr(struct bpf_iter_meta *, meta);
1317 __bpf_md_ptr(struct bpf_map *, map);
1318 __bpf_md_ptr(void *, key);
1319 __bpf_md_ptr(void *, value);
1320};
1321
1322int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1323void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1324bool bpf_iter_prog_supported(struct bpf_prog *prog);
1325int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
1326int bpf_iter_new_fd(struct bpf_link *link);
1327bool bpf_link_is_iter(struct bpf_link *link);
1328struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1329int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1330void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1331 struct seq_file *seq);
1332int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1333 struct bpf_link_info *info);
1334
1335int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1336int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1337int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1338 u64 flags);
1339int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1340 u64 flags);
1341
1342int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1343
1344int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1345 void *key, void *value, u64 map_flags);
1346int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1347int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1348 void *key, void *value, u64 map_flags);
1349int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1350
1351int bpf_get_file_flag(int flags);
1352int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
1353 size_t actual_size);
1354
1355
1356
1357
1358
1359
1360
1361static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
1362{
1363 const long *lsrc = src;
1364 long *ldst = dst;
1365
1366 size /= sizeof(long);
1367 while (size--)
1368 *ldst++ = *lsrc++;
1369}
1370
1371
1372int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
1373 union bpf_attr __user *uattr);
1374void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
1375
1376struct btf *bpf_get_btf_vmlinux(void);
1377
1378
1379struct xdp_buff;
1380struct sk_buff;
1381
1382struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
1383struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
1384void __dev_flush(void);
1385int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1386 struct net_device *dev_rx);
1387int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1388 struct net_device *dev_rx);
1389int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1390 struct bpf_prog *xdp_prog);
1391bool dev_map_can_have_prog(struct bpf_map *map);
1392
1393struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
1394void __cpu_map_flush(void);
1395int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
1396 struct net_device *dev_rx);
1397bool cpu_map_prog_allowed(struct bpf_map *map);
1398
1399
1400static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1401{
1402 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1403 attr->numa_node : NUMA_NO_NODE;
1404}
1405
1406struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
1407int array_map_alloc_check(union bpf_attr *attr);
1408
1409int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1410 union bpf_attr __user *uattr);
1411int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1412 union bpf_attr __user *uattr);
1413int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1414 const union bpf_attr *kattr,
1415 union bpf_attr __user *uattr);
1416int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1417 const union bpf_attr *kattr,
1418 union bpf_attr __user *uattr);
1419int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
1420 const union bpf_attr *kattr,
1421 union bpf_attr __user *uattr);
1422bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1423 const struct bpf_prog *prog,
1424 struct bpf_insn_access_aux *info);
1425int btf_struct_access(struct bpf_verifier_log *log,
1426 const struct btf_type *t, int off, int size,
1427 enum bpf_access_type atype,
1428 u32 *next_btf_id);
1429bool btf_struct_ids_match(struct bpf_verifier_log *log,
1430 int off, u32 id, u32 need_type_id);
1431
1432int btf_distill_func_proto(struct bpf_verifier_log *log,
1433 struct btf *btf,
1434 const struct btf_type *func_proto,
1435 const char *func_name,
1436 struct btf_func_model *m);
1437
1438struct bpf_reg_state;
1439int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
1440 struct bpf_reg_state *regs);
1441int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1442 struct bpf_reg_state *reg);
1443int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
1444 struct btf *btf, const struct btf_type *t);
1445
1446struct bpf_prog *bpf_prog_by_id(u32 id);
1447struct bpf_link *bpf_link_by_id(u32 id);
1448
1449const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
1450#else
1451static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1452{
1453 return ERR_PTR(-EOPNOTSUPP);
1454}
1455
1456static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1457 enum bpf_prog_type type,
1458 bool attach_drv)
1459{
1460 return ERR_PTR(-EOPNOTSUPP);
1461}
1462
1463static inline void bpf_prog_add(struct bpf_prog *prog, int i)
1464{
1465}
1466
1467static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1468{
1469}
1470
1471static inline void bpf_prog_put(struct bpf_prog *prog)
1472{
1473}
1474
1475static inline void bpf_prog_inc(struct bpf_prog *prog)
1476{
1477}
1478
1479static inline struct bpf_prog *__must_check
1480bpf_prog_inc_not_zero(struct bpf_prog *prog)
1481{
1482 return ERR_PTR(-EOPNOTSUPP);
1483}
1484
1485static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
1486{
1487 return 0;
1488}
1489
1490static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1491{
1492}
1493
1494static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1495 const struct bpf_link_ops *ops,
1496 struct bpf_prog *prog)
1497{
1498}
1499
1500static inline int bpf_link_prime(struct bpf_link *link,
1501 struct bpf_link_primer *primer)
1502{
1503 return -EOPNOTSUPP;
1504}
1505
1506static inline int bpf_link_settle(struct bpf_link_primer *primer)
1507{
1508 return -EOPNOTSUPP;
1509}
1510
1511static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
1512{
1513}
1514
1515static inline void bpf_link_inc(struct bpf_link *link)
1516{
1517}
1518
1519static inline void bpf_link_put(struct bpf_link *link)
1520{
1521}
1522
1523static inline int bpf_obj_get_user(const char __user *pathname, int flags)
1524{
1525 return -EOPNOTSUPP;
1526}
1527
1528static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
1529 u32 key)
1530{
1531 return NULL;
1532}
1533
1534static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
1535 u32 key)
1536{
1537 return NULL;
1538}
1539static inline bool dev_map_can_have_prog(struct bpf_map *map)
1540{
1541 return false;
1542}
1543
1544static inline void __dev_flush(void)
1545{
1546}
1547
1548struct xdp_buff;
1549struct bpf_dtab_netdev;
1550
1551static inline
1552int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1553 struct net_device *dev_rx)
1554{
1555 return 0;
1556}
1557
1558static inline
1559int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1560 struct net_device *dev_rx)
1561{
1562 return 0;
1563}
1564
1565struct sk_buff;
1566
1567static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
1568 struct sk_buff *skb,
1569 struct bpf_prog *xdp_prog)
1570{
1571 return 0;
1572}
1573
1574static inline
1575struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
1576{
1577 return NULL;
1578}
1579
1580static inline void __cpu_map_flush(void)
1581{
1582}
1583
1584static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1585 struct xdp_buff *xdp,
1586 struct net_device *dev_rx)
1587{
1588 return 0;
1589}
1590
1591static inline bool cpu_map_prog_allowed(struct bpf_map *map)
1592{
1593 return false;
1594}
1595
1596static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1597 enum bpf_prog_type type)
1598{
1599 return ERR_PTR(-EOPNOTSUPP);
1600}
1601
1602static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1603 const union bpf_attr *kattr,
1604 union bpf_attr __user *uattr)
1605{
1606 return -ENOTSUPP;
1607}
1608
1609static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1610 const union bpf_attr *kattr,
1611 union bpf_attr __user *uattr)
1612{
1613 return -ENOTSUPP;
1614}
1615
1616static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1617 const union bpf_attr *kattr,
1618 union bpf_attr __user *uattr)
1619{
1620 return -ENOTSUPP;
1621}
1622
1623static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1624 const union bpf_attr *kattr,
1625 union bpf_attr __user *uattr)
1626{
1627 return -ENOTSUPP;
1628}
1629
1630static inline void bpf_map_put(struct bpf_map *map)
1631{
1632}
1633
1634static inline struct bpf_prog *bpf_prog_by_id(u32 id)
1635{
1636 return ERR_PTR(-ENOTSUPP);
1637}
1638
1639static inline const struct bpf_func_proto *
1640bpf_base_func_proto(enum bpf_func_id func_id)
1641{
1642 return NULL;
1643}
1644#endif
1645
1646static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1647 enum bpf_prog_type type)
1648{
1649 return bpf_prog_get_type_dev(ufd, type, false);
1650}
1651
1652bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1653
1654int bpf_prog_offload_compile(struct bpf_prog *prog);
1655void bpf_prog_offload_destroy(struct bpf_prog *prog);
1656int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1657 struct bpf_prog *prog);
1658
1659int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1660
1661int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1662int bpf_map_offload_update_elem(struct bpf_map *map,
1663 void *key, void *value, u64 flags);
1664int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1665int bpf_map_offload_get_next_key(struct bpf_map *map,
1666 void *key, void *next_key);
1667
1668bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1669
1670struct bpf_offload_dev *
1671bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1672void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1673void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1674int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1675 struct net_device *netdev);
1676void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1677 struct net_device *netdev);
1678bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1679
1680#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1681int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1682
1683static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1684{
1685 return aux->offload_requested;
1686}
1687
1688static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1689{
1690 return unlikely(map->ops == &bpf_map_offload_ops);
1691}
1692
1693struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1694void bpf_map_offload_map_free(struct bpf_map *map);
1695#else
1696static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1697 union bpf_attr *attr)
1698{
1699 return -EOPNOTSUPP;
1700}
1701
1702static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1703{
1704 return false;
1705}
1706
1707static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1708{
1709 return false;
1710}
1711
1712static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1713{
1714 return ERR_PTR(-EOPNOTSUPP);
1715}
1716
1717static inline void bpf_map_offload_map_free(struct bpf_map *map)
1718{
1719}
1720#endif
1721
1722#if defined(CONFIG_BPF_STREAM_PARSER)
1723int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1724 struct bpf_prog *old, u32 which);
1725int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1726int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
1727int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
1728void sock_map_unhash(struct sock *sk);
1729void sock_map_close(struct sock *sk, long timeout);
1730#else
1731static inline int sock_map_prog_update(struct bpf_map *map,
1732 struct bpf_prog *prog,
1733 struct bpf_prog *old, u32 which)
1734{
1735 return -EOPNOTSUPP;
1736}
1737
1738static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1739 struct bpf_prog *prog)
1740{
1741 return -EINVAL;
1742}
1743
1744static inline int sock_map_prog_detach(const union bpf_attr *attr,
1745 enum bpf_prog_type ptype)
1746{
1747 return -EOPNOTSUPP;
1748}
1749
1750static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
1751 u64 flags)
1752{
1753 return -EOPNOTSUPP;
1754}
1755#endif
1756
1757#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1758void bpf_sk_reuseport_detach(struct sock *sk);
1759int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1760 void *value);
1761int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1762 void *value, u64 map_flags);
1763#else
1764static inline void bpf_sk_reuseport_detach(struct sock *sk)
1765{
1766}
1767
1768#ifdef CONFIG_BPF_SYSCALL
1769static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1770 void *key, void *value)
1771{
1772 return -EOPNOTSUPP;
1773}
1774
1775static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1776 void *key, void *value,
1777 u64 map_flags)
1778{
1779 return -EOPNOTSUPP;
1780}
1781#endif
1782#endif
1783
1784
1785extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1786extern const struct bpf_func_proto bpf_map_update_elem_proto;
1787extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1788extern const struct bpf_func_proto bpf_map_push_elem_proto;
1789extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1790extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1791
1792extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1793extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1794extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1795extern const struct bpf_func_proto bpf_tail_call_proto;
1796extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1797extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
1798extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1799extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1800extern const struct bpf_func_proto bpf_get_current_comm_proto;
1801extern const struct bpf_func_proto bpf_get_stackid_proto;
1802extern const struct bpf_func_proto bpf_get_stack_proto;
1803extern const struct bpf_func_proto bpf_get_task_stack_proto;
1804extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
1805extern const struct bpf_func_proto bpf_get_stack_proto_pe;
1806extern const struct bpf_func_proto bpf_sock_map_update_proto;
1807extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1808extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1809extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
1810extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1811extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1812extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1813extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1814extern const struct bpf_func_proto bpf_spin_lock_proto;
1815extern const struct bpf_func_proto bpf_spin_unlock_proto;
1816extern const struct bpf_func_proto bpf_get_local_storage_proto;
1817extern const struct bpf_func_proto bpf_strtol_proto;
1818extern const struct bpf_func_proto bpf_strtoul_proto;
1819extern const struct bpf_func_proto bpf_tcp_sock_proto;
1820extern const struct bpf_func_proto bpf_jiffies64_proto;
1821extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
1822extern const struct bpf_func_proto bpf_event_output_data_proto;
1823extern const struct bpf_func_proto bpf_ringbuf_output_proto;
1824extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
1825extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
1826extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
1827extern const struct bpf_func_proto bpf_ringbuf_query_proto;
1828extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
1829extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
1830extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
1831extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
1832extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
1833extern const struct bpf_func_proto bpf_copy_from_user_proto;
1834extern const struct bpf_func_proto bpf_snprintf_btf_proto;
1835extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
1836extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
1837
1838const struct bpf_func_proto *bpf_tracing_func_proto(
1839 enum bpf_func_id func_id, const struct bpf_prog *prog);
1840
1841const struct bpf_func_proto *tracing_prog_func_proto(
1842 enum bpf_func_id func_id, const struct bpf_prog *prog);
1843
1844
1845void bpf_user_rnd_init_once(void);
1846u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1847u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1848
1849#if defined(CONFIG_NET)
1850bool bpf_sock_common_is_valid_access(int off, int size,
1851 enum bpf_access_type type,
1852 struct bpf_insn_access_aux *info);
1853bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1854 struct bpf_insn_access_aux *info);
1855u32 bpf_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#else
1861static inline bool bpf_sock_common_is_valid_access(int off, int size,
1862 enum bpf_access_type type,
1863 struct bpf_insn_access_aux *info)
1864{
1865 return false;
1866}
1867static inline bool bpf_sock_is_valid_access(int off, int size,
1868 enum bpf_access_type type,
1869 struct bpf_insn_access_aux *info)
1870{
1871 return false;
1872}
1873static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1874 const struct bpf_insn *si,
1875 struct bpf_insn *insn_buf,
1876 struct bpf_prog *prog,
1877 u32 *target_size)
1878{
1879 return 0;
1880}
1881#endif
1882
1883#ifdef CONFIG_INET
1884struct sk_reuseport_kern {
1885 struct sk_buff *skb;
1886 struct sock *sk;
1887 struct sock *selected_sk;
1888 void *data_end;
1889 u32 hash;
1890 u32 reuseport_id;
1891 bool bind_inany;
1892};
1893bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1894 struct bpf_insn_access_aux *info);
1895
1896u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1897 const struct bpf_insn *si,
1898 struct bpf_insn *insn_buf,
1899 struct bpf_prog *prog,
1900 u32 *target_size);
1901
1902bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1903 struct bpf_insn_access_aux *info);
1904
1905u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1906 const struct bpf_insn *si,
1907 struct bpf_insn *insn_buf,
1908 struct bpf_prog *prog,
1909 u32 *target_size);
1910#else
1911static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1912 enum bpf_access_type type,
1913 struct bpf_insn_access_aux *info)
1914{
1915 return false;
1916}
1917
1918static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1919 const struct bpf_insn *si,
1920 struct bpf_insn *insn_buf,
1921 struct bpf_prog *prog,
1922 u32 *target_size)
1923{
1924 return 0;
1925}
1926static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1927 enum bpf_access_type type,
1928 struct bpf_insn_access_aux *info)
1929{
1930 return false;
1931}
1932
1933static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1934 const struct bpf_insn *si,
1935 struct bpf_insn *insn_buf,
1936 struct bpf_prog *prog,
1937 u32 *target_size)
1938{
1939 return 0;
1940}
1941#endif
1942
1943enum bpf_text_poke_type {
1944 BPF_MOD_CALL,
1945 BPF_MOD_JUMP,
1946};
1947
1948int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1949 void *addr1, void *addr2);
1950
1951struct btf_id_set;
1952bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
1953
1954#endif
1955