Commit | Line | Data |
---|---|---|
25763b3c | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
99c55f7d | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
99c55f7d AS |
3 | */ |
4 | #ifndef _LINUX_BPF_H | |
5 | #define _LINUX_BPF_H 1 | |
6 | ||
7 | #include <uapi/linux/bpf.h> | |
d687f621 | 8 | #include <uapi/linux/filter.h> |
74451e66 | 9 | |
99c55f7d | 10 | #include <linux/workqueue.h> |
db20fd2b | 11 | #include <linux/file.h> |
b121d1e7 | 12 | #include <linux/percpu.h> |
002245cc | 13 | #include <linux/err.h> |
74451e66 | 14 | #include <linux/rbtree_latch.h> |
d6e1e46f | 15 | #include <linux/numa.h> |
fc970227 | 16 | #include <linux/mm_types.h> |
ab3f0063 | 17 | #include <linux/wait.h> |
fec56f58 AS |
18 | #include <linux/refcount.h> |
19 | #include <linux/mutex.h> | |
85d33df3 | 20 | #include <linux/module.h> |
bfea9a85 | 21 | #include <linux/kallsyms.h> |
2c78ee89 | 22 | #include <linux/capability.h> |
48edc1f7 RG |
23 | #include <linux/sched/mm.h> |
24 | #include <linux/slab.h> | |
e21aa341 | 25 | #include <linux/percpu-refcount.h> |
d687f621 | 26 | #include <linux/stddef.h> |
af2ac3e1 | 27 | #include <linux/bpfptr.h> |
14a324f6 | 28 | #include <linux/btf.h> |
8c7dcb84 | 29 | #include <linux/rcupdate_trace.h> |
dbe69b29 | 30 | #include <linux/init.h> |
99c55f7d | 31 | |
cae1927c | 32 | struct bpf_verifier_env; |
9e15db66 | 33 | struct bpf_verifier_log; |
3b1efb19 | 34 | struct perf_event; |
174a79ff | 35 | struct bpf_prog; |
da765a2f | 36 | struct bpf_prog_aux; |
99c55f7d | 37 | struct bpf_map; |
4f738adb | 38 | struct sock; |
a26ca7c9 | 39 | struct seq_file; |
1b2b234b | 40 | struct btf; |
e8d2bec0 | 41 | struct btf_type; |
3dec541b | 42 | struct exception_table_entry; |
ae24345d | 43 | struct seq_operations; |
f9c79272 | 44 | struct bpf_iter_aux_info; |
f836a56e KS |
45 | struct bpf_local_storage; |
46 | struct bpf_local_storage_map; | |
36e68442 | 47 | struct kobject; |
48edc1f7 | 48 | struct mem_cgroup; |
861de02e | 49 | struct module; |
69c087ba | 50 | struct bpf_func_state; |
00963a2e | 51 | struct ftrace_ops; |
d4ccaf58 | 52 | struct cgroup; |
99c55f7d | 53 | |
1b9ed84e QM |
54 | extern struct idr btf_idr; |
55 | extern spinlock_t btf_idr_lock; | |
36e68442 | 56 | extern struct kobject *btf_kobj; |
958cf2e2 KKD |
57 | extern struct bpf_mem_alloc bpf_global_ma; |
58 | extern bool bpf_global_ma_set; | |
1b9ed84e | 59 | |
102acbac | 60 | typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64); |
f9c79272 YS |
61 | typedef int (*bpf_iter_init_seq_priv_t)(void *private_data, |
62 | struct bpf_iter_aux_info *aux); | |
14fc6bd6 | 63 | typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data); |
af3f4134 SF |
64 | typedef unsigned int (*bpf_func_t)(const void *, |
65 | const struct bpf_insn *); | |
14fc6bd6 YS |
66 | struct bpf_iter_seq_info { |
67 | const struct seq_operations *seq_ops; | |
68 | bpf_iter_init_seq_priv_t init_seq_private; | |
69 | bpf_iter_fini_seq_priv_t fini_seq_private; | |
70 | u32 seq_priv_size; | |
71 | }; | |
72 | ||
5d903493 | 73 | /* map is generic key/value storage optionally accessible by eBPF programs */ |
99c55f7d AS |
74 | struct bpf_map_ops { |
75 | /* funcs callable from userspace (via syscall) */ | |
1110f3a9 | 76 | int (*map_alloc_check)(union bpf_attr *attr); |
99c55f7d | 77 | struct bpf_map *(*map_alloc)(union bpf_attr *attr); |
61d1b6a4 DB |
78 | void (*map_release)(struct bpf_map *map, struct file *map_file); |
79 | void (*map_free)(struct bpf_map *map); | |
db20fd2b | 80 | int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key); |
ba6b8de4 | 81 | void (*map_release_uref)(struct bpf_map *map); |
c6110222 | 82 | void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key); |
cb4d03ab BV |
83 | int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr, |
84 | union bpf_attr __user *uattr); | |
3e87f192 DS |
85 | int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key, |
86 | void *value, u64 flags); | |
05799638 YS |
87 | int (*map_lookup_and_delete_batch)(struct bpf_map *map, |
88 | const union bpf_attr *attr, | |
89 | union bpf_attr __user *uattr); | |
3af43ba4 HT |
90 | int (*map_update_batch)(struct bpf_map *map, struct file *map_file, |
91 | const union bpf_attr *attr, | |
aa2e93b8 BV |
92 | union bpf_attr __user *uattr); |
93 | int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr, | |
94 | union bpf_attr __user *uattr); | |
db20fd2b AS |
95 | |
96 | /* funcs callable from userspace and from eBPF programs */ | |
97 | void *(*map_lookup_elem)(struct bpf_map *map, void *key); | |
3274f520 | 98 | int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags); |
db20fd2b | 99 | int (*map_delete_elem)(struct bpf_map *map, void *key); |
f1a2e44a MV |
100 | int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags); |
101 | int (*map_pop_elem)(struct bpf_map *map, void *value); | |
102 | int (*map_peek_elem)(struct bpf_map *map, void *value); | |
07343110 | 103 | void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu); |
2a36f0b9 WN |
104 | |
105 | /* funcs called by prog_array and perf_event_array map */ | |
d056a788 DB |
106 | void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file, |
107 | int fd); | |
108 | void (*map_fd_put_ptr)(void *ptr); | |
4a8f87e6 | 109 | int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf); |
14dc6f04 | 110 | u32 (*map_fd_sys_lookup_elem)(void *ptr); |
a26ca7c9 MKL |
111 | void (*map_seq_show_elem)(struct bpf_map *map, void *key, |
112 | struct seq_file *m); | |
e8d2bec0 | 113 | int (*map_check_btf)(const struct bpf_map *map, |
1b2b234b | 114 | const struct btf *btf, |
e8d2bec0 DB |
115 | const struct btf_type *key_type, |
116 | const struct btf_type *value_type); | |
d8eca5bb | 117 | |
da765a2f DB |
118 | /* Prog poke tracking helpers. */ |
119 | int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux); | |
120 | void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux); | |
121 | void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old, | |
122 | struct bpf_prog *new); | |
123 | ||
d8eca5bb DB |
124 | /* Direct value access helpers. */ |
125 | int (*map_direct_value_addr)(const struct bpf_map *map, | |
126 | u64 *imm, u32 off); | |
127 | int (*map_direct_value_meta)(const struct bpf_map *map, | |
128 | u64 imm, u32 *off); | |
fc970227 | 129 | int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma); |
457f4436 AN |
130 | __poll_t (*map_poll)(struct bpf_map *map, struct file *filp, |
131 | struct poll_table_struct *pts); | |
41c48f3a | 132 | |
f836a56e KS |
133 | /* Functions called by bpf_local_storage maps */ |
134 | int (*map_local_storage_charge)(struct bpf_local_storage_map *smap, | |
135 | void *owner, u32 size); | |
136 | void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap, | |
137 | void *owner, u32 size); | |
138 | struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner); | |
f4d05259 | 139 | |
e6a4750f | 140 | /* Misc helpers.*/ |
32637e33 | 141 | int (*map_redirect)(struct bpf_map *map, u64 key, u64 flags); |
e6a4750f | 142 | |
f4d05259 MKL |
143 | /* map_meta_equal must be implemented for maps that can be |
144 | * used as an inner map. It is a runtime check to ensure | |
145 | * an inner map can be inserted to an outer map. | |
146 | * | |
147 | * Some properties of the inner map has been used during the | |
148 | * verification time. When inserting an inner map at the runtime, | |
149 | * map_meta_equal has to ensure the inserting map has the same | |
150 | * properties that the verifier has used earlier. | |
151 | */ | |
152 | bool (*map_meta_equal)(const struct bpf_map *meta0, | |
153 | const struct bpf_map *meta1); | |
154 | ||
69c087ba YS |
155 | |
156 | int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env, | |
157 | struct bpf_func_state *caller, | |
158 | struct bpf_func_state *callee); | |
102acbac KC |
159 | int (*map_for_each_callback)(struct bpf_map *map, |
160 | bpf_callback_t callback_fn, | |
69c087ba YS |
161 | void *callback_ctx, u64 flags); |
162 | ||
c317ab71 | 163 | /* BTF id of struct allocated by map_alloc */ |
41c48f3a | 164 | int *map_btf_id; |
a5cbe05a YS |
165 | |
166 | /* bpf_iter info used to open a seq_file */ | |
167 | const struct bpf_iter_seq_info *iter_seq_info; | |
99c55f7d AS |
168 | }; |
169 | ||
61df10c7 | 170 | enum { |
2d577252 KKD |
171 | /* Support at most 10 fields in a BTF type */ |
172 | BTF_FIELDS_MAX = 10, | |
61df10c7 KKD |
173 | }; |
174 | ||
aa3496ac | 175 | enum btf_field_type { |
db559117 KKD |
176 | BPF_SPIN_LOCK = (1 << 0), |
177 | BPF_TIMER = (1 << 1), | |
aa3496ac KKD |
178 | BPF_KPTR_UNREF = (1 << 2), |
179 | BPF_KPTR_REF = (1 << 3), | |
180 | BPF_KPTR = BPF_KPTR_UNREF | BPF_KPTR_REF, | |
f0c5941f | 181 | BPF_LIST_HEAD = (1 << 4), |
8ffa5cc1 | 182 | BPF_LIST_NODE = (1 << 5), |
c0a5a21c KKD |
183 | }; |
184 | ||
aa3496ac KKD |
185 | struct btf_field_kptr { |
186 | struct btf *btf; | |
187 | struct module *module; | |
188 | btf_dtor_kfunc_t dtor; | |
189 | u32 btf_id; | |
190 | }; | |
191 | ||
f0c5941f KKD |
192 | struct btf_field_list_head { |
193 | struct btf *btf; | |
194 | u32 value_btf_id; | |
195 | u32 node_offset; | |
865ce09a | 196 | struct btf_record *value_rec; |
f0c5941f KKD |
197 | }; |
198 | ||
aa3496ac | 199 | struct btf_field { |
61df10c7 | 200 | u32 offset; |
aa3496ac KKD |
201 | enum btf_field_type type; |
202 | union { | |
203 | struct btf_field_kptr kptr; | |
f0c5941f | 204 | struct btf_field_list_head list_head; |
aa3496ac | 205 | }; |
61df10c7 KKD |
206 | }; |
207 | ||
aa3496ac KKD |
208 | struct btf_record { |
209 | u32 cnt; | |
210 | u32 field_mask; | |
db559117 KKD |
211 | int spin_lock_off; |
212 | int timer_off; | |
aa3496ac | 213 | struct btf_field fields[]; |
61df10c7 KKD |
214 | }; |
215 | ||
aa3496ac | 216 | struct btf_field_offs { |
4d7d7f69 | 217 | u32 cnt; |
2d577252 KKD |
218 | u32 field_off[BTF_FIELDS_MAX]; |
219 | u8 field_sz[BTF_FIELDS_MAX]; | |
4d7d7f69 KKD |
220 | }; |
221 | ||
99c55f7d | 222 | struct bpf_map { |
a26ca7c9 | 223 | /* The first two cachelines with read-mostly members of which some |
be95a845 DB |
224 | * are also accessed in fast-path (e.g. ops, max_entries). |
225 | */ | |
226 | const struct bpf_map_ops *ops ____cacheline_aligned; | |
227 | struct bpf_map *inner_map_meta; | |
228 | #ifdef CONFIG_SECURITY | |
229 | void *security; | |
230 | #endif | |
99c55f7d AS |
231 | enum bpf_map_type map_type; |
232 | u32 key_size; | |
233 | u32 value_size; | |
234 | u32 max_entries; | |
9330986c | 235 | u64 map_extra; /* any per-map-type extra fields */ |
6c905981 | 236 | u32 map_flags; |
f3f1c054 | 237 | u32 id; |
db559117 | 238 | struct btf_record *record; |
96eabe7a | 239 | int numa_node; |
9b2cf328 MKL |
240 | u32 btf_key_type_id; |
241 | u32 btf_value_type_id; | |
8845b468 | 242 | u32 btf_vmlinux_value_type_id; |
a26ca7c9 | 243 | struct btf *btf; |
48edc1f7 | 244 | #ifdef CONFIG_MEMCG_KMEM |
4201d9ab | 245 | struct obj_cgroup *objcg; |
48edc1f7 | 246 | #endif |
fc970227 | 247 | char name[BPF_OBJ_NAME_LEN]; |
aa3496ac | 248 | struct btf_field_offs *field_offs; |
a26ca7c9 | 249 | /* The 3rd and 4th cacheline with misc members to avoid false sharing |
be95a845 DB |
250 | * particularly with refcounting. |
251 | */ | |
1e0bd5a0 AN |
252 | atomic64_t refcnt ____cacheline_aligned; |
253 | atomic64_t usercnt; | |
be95a845 | 254 | struct work_struct work; |
fc970227 | 255 | struct mutex freeze_mutex; |
353050be | 256 | atomic64_t writecnt; |
f45d5b6c THJ |
257 | /* 'Ownership' of program-containing map is claimed by the first program |
258 | * that is going to use this map or by the first program which FD is | |
259 | * stored in the map to make sure that all callers and callees have the | |
260 | * same prog type, JITed flag and xdp_has_frags flag. | |
261 | */ | |
262 | struct { | |
263 | spinlock_t lock; | |
264 | enum bpf_prog_type type; | |
265 | bool jited; | |
266 | bool xdp_has_frags; | |
267 | } owner; | |
4d7d7f69 KKD |
268 | bool bypass_spec_v1; |
269 | bool frozen; /* write-once; write-protected by freeze_mutex */ | |
99c55f7d AS |
270 | }; |
271 | ||
db559117 KKD |
272 | static inline const char *btf_field_type_name(enum btf_field_type type) |
273 | { | |
274 | switch (type) { | |
275 | case BPF_SPIN_LOCK: | |
276 | return "bpf_spin_lock"; | |
277 | case BPF_TIMER: | |
278 | return "bpf_timer"; | |
279 | case BPF_KPTR_UNREF: | |
280 | case BPF_KPTR_REF: | |
281 | return "kptr"; | |
f0c5941f KKD |
282 | case BPF_LIST_HEAD: |
283 | return "bpf_list_head"; | |
8ffa5cc1 KKD |
284 | case BPF_LIST_NODE: |
285 | return "bpf_list_node"; | |
db559117 KKD |
286 | default: |
287 | WARN_ON_ONCE(1); | |
288 | return "unknown"; | |
289 | } | |
290 | } | |
291 | ||
aa3496ac KKD |
292 | static inline u32 btf_field_type_size(enum btf_field_type type) |
293 | { | |
294 | switch (type) { | |
db559117 KKD |
295 | case BPF_SPIN_LOCK: |
296 | return sizeof(struct bpf_spin_lock); | |
297 | case BPF_TIMER: | |
298 | return sizeof(struct bpf_timer); | |
aa3496ac KKD |
299 | case BPF_KPTR_UNREF: |
300 | case BPF_KPTR_REF: | |
301 | return sizeof(u64); | |
f0c5941f KKD |
302 | case BPF_LIST_HEAD: |
303 | return sizeof(struct bpf_list_head); | |
8ffa5cc1 KKD |
304 | case BPF_LIST_NODE: |
305 | return sizeof(struct bpf_list_node); | |
aa3496ac KKD |
306 | default: |
307 | WARN_ON_ONCE(1); | |
308 | return 0; | |
309 | } | |
310 | } | |
311 | ||
312 | static inline u32 btf_field_type_align(enum btf_field_type type) | |
313 | { | |
314 | switch (type) { | |
db559117 KKD |
315 | case BPF_SPIN_LOCK: |
316 | return __alignof__(struct bpf_spin_lock); | |
317 | case BPF_TIMER: | |
318 | return __alignof__(struct bpf_timer); | |
aa3496ac KKD |
319 | case BPF_KPTR_UNREF: |
320 | case BPF_KPTR_REF: | |
321 | return __alignof__(u64); | |
f0c5941f KKD |
322 | case BPF_LIST_HEAD: |
323 | return __alignof__(struct bpf_list_head); | |
8ffa5cc1 KKD |
324 | case BPF_LIST_NODE: |
325 | return __alignof__(struct bpf_list_node); | |
aa3496ac KKD |
326 | default: |
327 | WARN_ON_ONCE(1); | |
328 | return 0; | |
329 | } | |
330 | } | |
331 | ||
332 | static inline bool btf_record_has_field(const struct btf_record *rec, enum btf_field_type type) | |
333 | { | |
334 | if (IS_ERR_OR_NULL(rec)) | |
335 | return false; | |
336 | return rec->field_mask & type; | |
337 | } | |
338 | ||
958cf2e2 | 339 | static inline void bpf_obj_init(const struct btf_field_offs *foffs, void *obj) |
68134668 | 340 | { |
958cf2e2 | 341 | int i; |
4d7d7f69 | 342 | |
958cf2e2 KKD |
343 | if (!foffs) |
344 | return; | |
345 | for (i = 0; i < foffs->cnt; i++) | |
346 | memset(obj + foffs->field_off[i], 0, foffs->field_sz[i]); | |
347 | } | |
348 | ||
349 | static inline void check_and_init_map_value(struct bpf_map *map, void *dst) | |
350 | { | |
351 | bpf_obj_init(map->field_offs, dst); | |
68134668 AS |
352 | } |
353 | ||
44832519 KKD |
354 | /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and |
355 | * forced to use 'long' read/writes to try to atomically copy long counters. | |
356 | * Best-effort only. No barriers here, since it _will_ race with concurrent | |
357 | * updates from BPF programs. Called from bpf syscall and mostly used with | |
358 | * size 8 or 16 bytes, so ask compiler to inline it. | |
359 | */ | |
360 | static inline void bpf_long_memcpy(void *dst, const void *src, u32 size) | |
361 | { | |
362 | const long *lsrc = src; | |
363 | long *ldst = dst; | |
364 | ||
365 | size /= sizeof(long); | |
366 | while (size--) | |
367 | *ldst++ = *lsrc++; | |
368 | } | |
369 | ||
370 | /* copy everything but bpf_spin_lock, bpf_timer, and kptrs. There could be one of each. */ | |
f71b2f64 KKD |
371 | static inline void bpf_obj_memcpy(struct btf_field_offs *foffs, |
372 | void *dst, void *src, u32 size, | |
373 | bool long_memcpy) | |
d83525ca | 374 | { |
4d7d7f69 KKD |
375 | u32 curr_off = 0; |
376 | int i; | |
68134668 | 377 | |
f71b2f64 | 378 | if (likely(!foffs)) { |
44832519 | 379 | if (long_memcpy) |
f71b2f64 | 380 | bpf_long_memcpy(dst, src, round_up(size, 8)); |
44832519 | 381 | else |
f71b2f64 | 382 | memcpy(dst, src, size); |
4d7d7f69 | 383 | return; |
68134668 | 384 | } |
d83525ca | 385 | |
f71b2f64 KKD |
386 | for (i = 0; i < foffs->cnt; i++) { |
387 | u32 next_off = foffs->field_off[i]; | |
aa3496ac | 388 | u32 sz = next_off - curr_off; |
4d7d7f69 | 389 | |
aa3496ac | 390 | memcpy(dst + curr_off, src + curr_off, sz); |
e5feed0f | 391 | curr_off += foffs->field_sz[i] + sz; |
d83525ca | 392 | } |
f71b2f64 | 393 | memcpy(dst + curr_off, src + curr_off, size - curr_off); |
d83525ca | 394 | } |
44832519 KKD |
395 | |
396 | static inline void copy_map_value(struct bpf_map *map, void *dst, void *src) | |
397 | { | |
f71b2f64 | 398 | bpf_obj_memcpy(map->field_offs, dst, src, map->value_size, false); |
44832519 KKD |
399 | } |
400 | ||
401 | static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src) | |
402 | { | |
f71b2f64 | 403 | bpf_obj_memcpy(map->field_offs, dst, src, map->value_size, true); |
44832519 KKD |
404 | } |
405 | ||
f71b2f64 | 406 | static inline void bpf_obj_memzero(struct btf_field_offs *foffs, void *dst, u32 size) |
cc487558 KKD |
407 | { |
408 | u32 curr_off = 0; | |
409 | int i; | |
410 | ||
f71b2f64 KKD |
411 | if (likely(!foffs)) { |
412 | memset(dst, 0, size); | |
cc487558 KKD |
413 | return; |
414 | } | |
415 | ||
f71b2f64 KKD |
416 | for (i = 0; i < foffs->cnt; i++) { |
417 | u32 next_off = foffs->field_off[i]; | |
aa3496ac | 418 | u32 sz = next_off - curr_off; |
cc487558 | 419 | |
aa3496ac | 420 | memset(dst + curr_off, 0, sz); |
e5feed0f | 421 | curr_off += foffs->field_sz[i] + sz; |
cc487558 | 422 | } |
f71b2f64 KKD |
423 | memset(dst + curr_off, 0, size - curr_off); |
424 | } | |
425 | ||
426 | static inline void zero_map_value(struct bpf_map *map, void *dst) | |
427 | { | |
428 | bpf_obj_memzero(map->field_offs, dst, map->value_size); | |
cc487558 KKD |
429 | } |
430 | ||
96049f3a AS |
431 | void copy_map_value_locked(struct bpf_map *map, void *dst, void *src, |
432 | bool lock_src); | |
b00628b1 | 433 | void bpf_timer_cancel_and_free(void *timer); |
f0c5941f KKD |
434 | void bpf_list_head_free(const struct btf_field *field, void *list_head, |
435 | struct bpf_spin_lock *spin_lock); | |
436 | ||
8e7ae251 | 437 | int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size); |
d83525ca | 438 | |
602144c2 | 439 | struct bpf_offload_dev; |
a3884572 JK |
440 | struct bpf_offloaded_map; |
441 | ||
442 | struct bpf_map_dev_ops { | |
443 | int (*map_get_next_key)(struct bpf_offloaded_map *map, | |
444 | void *key, void *next_key); | |
445 | int (*map_lookup_elem)(struct bpf_offloaded_map *map, | |
446 | void *key, void *value); | |
447 | int (*map_update_elem)(struct bpf_offloaded_map *map, | |
448 | void *key, void *value, u64 flags); | |
449 | int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key); | |
450 | }; | |
451 | ||
452 | struct bpf_offloaded_map { | |
453 | struct bpf_map map; | |
454 | struct net_device *netdev; | |
455 | const struct bpf_map_dev_ops *dev_ops; | |
456 | void *dev_priv; | |
457 | struct list_head offloads; | |
458 | }; | |
459 | ||
460 | static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map) | |
461 | { | |
462 | return container_of(map, struct bpf_offloaded_map, map); | |
463 | } | |
464 | ||
0cd3cbed JK |
465 | static inline bool bpf_map_offload_neutral(const struct bpf_map *map) |
466 | { | |
467 | return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY; | |
468 | } | |
469 | ||
a26ca7c9 MKL |
470 | static inline bool bpf_map_support_seq_show(const struct bpf_map *map) |
471 | { | |
85d33df3 MKL |
472 | return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) && |
473 | map->ops->map_seq_show_elem; | |
a26ca7c9 MKL |
474 | } |
475 | ||
e8d2bec0 | 476 | int map_check_no_btf(const struct bpf_map *map, |
1b2b234b | 477 | const struct btf *btf, |
e8d2bec0 DB |
478 | const struct btf_type *key_type, |
479 | const struct btf_type *value_type); | |
480 | ||
f4d05259 MKL |
481 | bool bpf_map_meta_equal(const struct bpf_map *meta0, |
482 | const struct bpf_map *meta1); | |
483 | ||
a3884572 JK |
484 | extern const struct bpf_map_ops bpf_map_offload_ops; |
485 | ||
d639b9d1 HL |
486 | /* bpf_type_flag contains a set of flags that are applicable to the values of |
487 | * arg_type, ret_type and reg_type. For example, a pointer value may be null, | |
488 | * or a memory is read-only. We classify types into two categories: base types | |
489 | * and extended types. Extended types are base types combined with a type flag. | |
490 | * | |
491 | * Currently there are no more than 32 base types in arg_type, ret_type and | |
492 | * reg_types. | |
493 | */ | |
494 | #define BPF_BASE_TYPE_BITS 8 | |
495 | ||
496 | enum bpf_type_flag { | |
497 | /* PTR may be NULL. */ | |
498 | PTR_MAYBE_NULL = BIT(0 + BPF_BASE_TYPE_BITS), | |
499 | ||
216e3cd2 HL |
500 | /* MEM is read-only. When applied on bpf_arg, it indicates the arg is |
501 | * compatible with both mutable and immutable memory. | |
502 | */ | |
20b2aff4 HL |
503 | MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS), |
504 | ||
894f2a8b KKD |
505 | /* MEM points to BPF ring buffer reservation. */ |
506 | MEM_RINGBUF = BIT(2 + BPF_BASE_TYPE_BITS), | |
a672b2e3 | 507 | |
c6f1bfe8 YS |
508 | /* MEM is in user address space. */ |
509 | MEM_USER = BIT(3 + BPF_BASE_TYPE_BITS), | |
510 | ||
5844101a HL |
511 | /* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged |
512 | * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In | |
513 | * order to drop this tag, it must be passed into bpf_per_cpu_ptr() | |
514 | * or bpf_this_cpu_ptr(), which will return the pointer corresponding | |
515 | * to the specified cpu. | |
516 | */ | |
517 | MEM_PERCPU = BIT(4 + BPF_BASE_TYPE_BITS), | |
518 | ||
8f14852e KKD |
519 | /* Indicates that the argument will be released. */ |
520 | OBJ_RELEASE = BIT(5 + BPF_BASE_TYPE_BITS), | |
521 | ||
6efe152d KKD |
522 | /* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark |
523 | * unreferenced and referenced kptr loaded from map value using a load | |
524 | * instruction, so that they can only be dereferenced but not escape the | |
525 | * BPF program into the kernel (i.e. cannot be passed as arguments to | |
526 | * kfunc or bpf helpers). | |
527 | */ | |
528 | PTR_UNTRUSTED = BIT(6 + BPF_BASE_TYPE_BITS), | |
529 | ||
16d1e00c JK |
530 | MEM_UNINIT = BIT(7 + BPF_BASE_TYPE_BITS), |
531 | ||
97e03f52 JK |
532 | /* DYNPTR points to memory local to the bpf program. */ |
533 | DYNPTR_TYPE_LOCAL = BIT(8 + BPF_BASE_TYPE_BITS), | |
534 | ||
20571567 | 535 | /* DYNPTR points to a kernel-produced ringbuf record. */ |
bc34dee6 JK |
536 | DYNPTR_TYPE_RINGBUF = BIT(9 + BPF_BASE_TYPE_BITS), |
537 | ||
508362ac MM |
538 | /* Size is known at compile time. */ |
539 | MEM_FIXED_SIZE = BIT(10 + BPF_BASE_TYPE_BITS), | |
540 | ||
282de143 KKD |
541 | /* MEM is of an allocated object of type in program BTF. This is used to |
542 | * tag PTR_TO_BTF_ID allocated using bpf_obj_new. | |
543 | */ | |
544 | MEM_ALLOC = BIT(11 + BPF_BASE_TYPE_BITS), | |
545 | ||
16d1e00c JK |
546 | __BPF_TYPE_FLAG_MAX, |
547 | __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1, | |
d639b9d1 HL |
548 | }; |
549 | ||
bc34dee6 | 550 | #define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF) |
97e03f52 | 551 | |
d639b9d1 HL |
552 | /* Max number of base types. */ |
553 | #define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS) | |
554 | ||
555 | /* Max number of all types. */ | |
556 | #define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1)) | |
557 | ||
17a52670 AS |
558 | /* function argument constraints */ |
559 | enum bpf_arg_type { | |
80f1d68c | 560 | ARG_DONTCARE = 0, /* unused argument in helper function */ |
17a52670 AS |
561 | |
562 | /* the following constraints used to prototype | |
563 | * bpf_map_lookup/update/delete_elem() functions | |
564 | */ | |
565 | ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */ | |
566 | ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */ | |
567 | ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */ | |
568 | ||
16d1e00c JK |
569 | /* Used to prototype bpf_memcmp() and other functions that access data |
570 | * on eBPF program stack | |
17a52670 | 571 | */ |
39f19ebb | 572 | ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */ |
435faee1 | 573 | |
39f19ebb AS |
574 | ARG_CONST_SIZE, /* number of bytes accessed from memory */ |
575 | ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */ | |
80f1d68c | 576 | |
608cd71a | 577 | ARG_PTR_TO_CTX, /* pointer to context */ |
80f1d68c | 578 | ARG_ANYTHING, /* any (initialized) argument is ok */ |
d83525ca | 579 | ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */ |
46f8bc92 | 580 | ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */ |
57c3bb72 AI |
581 | ARG_PTR_TO_INT, /* pointer to int */ |
582 | ARG_PTR_TO_LONG, /* pointer to long */ | |
6ac99e8f | 583 | ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */ |
a7658e1a | 584 | ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */ |
894f2a8b | 585 | ARG_PTR_TO_RINGBUF_MEM, /* pointer to dynamically reserved ringbuf memory */ |
457f4436 | 586 | ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */ |
1df8f55a | 587 | ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */ |
eaa6bcb7 | 588 | ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */ |
69c087ba | 589 | ARG_PTR_TO_FUNC, /* pointer to a bpf program function */ |
48946bd6 | 590 | ARG_PTR_TO_STACK, /* pointer to stack */ |
fff13c4b | 591 | ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */ |
b00628b1 | 592 | ARG_PTR_TO_TIMER, /* pointer to bpf_timer */ |
c0a5a21c | 593 | ARG_PTR_TO_KPTR, /* pointer to referenced kptr */ |
97e03f52 | 594 | ARG_PTR_TO_DYNPTR, /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */ |
f79e7ea5 | 595 | __BPF_ARG_TYPE_MAX, |
d639b9d1 | 596 | |
48946bd6 HL |
597 | /* Extended arg_types. */ |
598 | ARG_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE, | |
599 | ARG_PTR_TO_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MEM, | |
600 | ARG_PTR_TO_CTX_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_CTX, | |
601 | ARG_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET, | |
48946bd6 | 602 | ARG_PTR_TO_STACK_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_STACK, |
c0a5a21c | 603 | ARG_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID, |
16d1e00c JK |
604 | /* pointer to memory does not need to be initialized, helper function must fill |
605 | * all bytes or clear them in error case. | |
606 | */ | |
607 | ARG_PTR_TO_UNINIT_MEM = MEM_UNINIT | ARG_PTR_TO_MEM, | |
508362ac MM |
608 | /* Pointer to valid memory of size known at compile time. */ |
609 | ARG_PTR_TO_FIXED_SIZE_MEM = MEM_FIXED_SIZE | ARG_PTR_TO_MEM, | |
48946bd6 | 610 | |
d639b9d1 HL |
611 | /* This must be the last entry. Its purpose is to ensure the enum is |
612 | * wide enough to hold the higher bits reserved for bpf_type_flag. | |
613 | */ | |
614 | __BPF_ARG_TYPE_LIMIT = BPF_TYPE_LIMIT, | |
17a52670 | 615 | }; |
d639b9d1 | 616 | static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT); |
17a52670 AS |
617 | |
618 | /* type of values returned from helper functions */ | |
619 | enum bpf_return_type { | |
620 | RET_INTEGER, /* function returns integer */ | |
621 | RET_VOID, /* function doesn't return anything */ | |
3e6a4b3e | 622 | RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */ |
3c480732 HL |
623 | RET_PTR_TO_SOCKET, /* returns a pointer to a socket */ |
624 | RET_PTR_TO_TCP_SOCK, /* returns a pointer to a tcp_sock */ | |
625 | RET_PTR_TO_SOCK_COMMON, /* returns a pointer to a sock_common */ | |
2de2669b | 626 | RET_PTR_TO_MEM, /* returns a pointer to memory */ |
63d9b80d | 627 | RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */ |
3ca1032a | 628 | RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */ |
d639b9d1 HL |
629 | __BPF_RET_TYPE_MAX, |
630 | ||
3c480732 HL |
631 | /* Extended ret_types. */ |
632 | RET_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE, | |
633 | RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET, | |
634 | RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK, | |
635 | RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON, | |
894f2a8b | 636 | RET_PTR_TO_RINGBUF_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_RINGBUF | RET_PTR_TO_MEM, |
2de2669b | 637 | RET_PTR_TO_DYNPTR_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_MEM, |
3c480732 HL |
638 | RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID, |
639 | ||
d639b9d1 HL |
640 | /* This must be the last entry. Its purpose is to ensure the enum is |
641 | * wide enough to hold the higher bits reserved for bpf_type_flag. | |
642 | */ | |
643 | __BPF_RET_TYPE_LIMIT = BPF_TYPE_LIMIT, | |
17a52670 | 644 | }; |
d639b9d1 | 645 | static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT); |
17a52670 | 646 | |
09756af4 AS |
647 | /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs |
648 | * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL | |
649 | * instructions after verifying | |
650 | */ | |
651 | struct bpf_func_proto { | |
652 | u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); | |
653 | bool gpl_only; | |
36bbef52 | 654 | bool pkt_access; |
17a52670 | 655 | enum bpf_return_type ret_type; |
a7658e1a AS |
656 | union { |
657 | struct { | |
658 | enum bpf_arg_type arg1_type; | |
659 | enum bpf_arg_type arg2_type; | |
660 | enum bpf_arg_type arg3_type; | |
661 | enum bpf_arg_type arg4_type; | |
662 | enum bpf_arg_type arg5_type; | |
663 | }; | |
664 | enum bpf_arg_type arg_type[5]; | |
665 | }; | |
9436ef6e LB |
666 | union { |
667 | struct { | |
668 | u32 *arg1_btf_id; | |
669 | u32 *arg2_btf_id; | |
670 | u32 *arg3_btf_id; | |
671 | u32 *arg4_btf_id; | |
672 | u32 *arg5_btf_id; | |
673 | }; | |
674 | u32 *arg_btf_id[5]; | |
508362ac MM |
675 | struct { |
676 | size_t arg1_size; | |
677 | size_t arg2_size; | |
678 | size_t arg3_size; | |
679 | size_t arg4_size; | |
680 | size_t arg5_size; | |
681 | }; | |
682 | size_t arg_size[5]; | |
9436ef6e | 683 | }; |
af7ec138 | 684 | int *ret_btf_id; /* return value btf_id */ |
eae2e83e | 685 | bool (*allowed)(const struct bpf_prog *prog); |
17a52670 AS |
686 | }; |
687 | ||
688 | /* bpf_context is intentionally undefined structure. Pointer to bpf_context is | |
689 | * the first argument to eBPF programs. | |
690 | * For socket filters: 'struct bpf_context *' == 'struct sk_buff *' | |
691 | */ | |
692 | struct bpf_context; | |
693 | ||
694 | enum bpf_access_type { | |
695 | BPF_READ = 1, | |
696 | BPF_WRITE = 2 | |
09756af4 AS |
697 | }; |
698 | ||
19de99f7 | 699 | /* types of values stored in eBPF registers */ |
f1174f77 EC |
700 | /* Pointer types represent: |
701 | * pointer | |
702 | * pointer + imm | |
703 | * pointer + (u16) var | |
704 | * pointer + (u16) var + imm | |
705 | * if (range > 0) then [ptr, ptr + range - off) is safe to access | |
706 | * if (id > 0) means that some 'var' was added | |
707 | * if (off > 0) means that 'imm' was added | |
708 | */ | |
19de99f7 AS |
709 | enum bpf_reg_type { |
710 | NOT_INIT = 0, /* nothing was written into register */ | |
f1174f77 | 711 | SCALAR_VALUE, /* reg doesn't contain a valid pointer */ |
19de99f7 AS |
712 | PTR_TO_CTX, /* reg points to bpf_context */ |
713 | CONST_PTR_TO_MAP, /* reg points to struct bpf_map */ | |
714 | PTR_TO_MAP_VALUE, /* reg points to map element value */ | |
c25b2ae1 | 715 | PTR_TO_MAP_KEY, /* reg points to a map element key */ |
f1174f77 | 716 | PTR_TO_STACK, /* reg == frame_pointer + offset */ |
de8f3a83 | 717 | PTR_TO_PACKET_META, /* skb->data - meta_len */ |
f1174f77 | 718 | PTR_TO_PACKET, /* reg points to skb->data */ |
19de99f7 | 719 | PTR_TO_PACKET_END, /* skb->data + headlen */ |
d58e468b | 720 | PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */ |
c64b7983 | 721 | PTR_TO_SOCKET, /* reg points to struct bpf_sock */ |
46f8bc92 | 722 | PTR_TO_SOCK_COMMON, /* reg points to sock_common */ |
655a51e5 | 723 | PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */ |
9df1c28b | 724 | PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */ |
fada7fdc | 725 | PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */ |
ba5f4cfe JF |
726 | /* PTR_TO_BTF_ID points to a kernel struct that does not need |
727 | * to be null checked by the BPF program. This does not imply the | |
728 | * pointer is _not_ null and in practice this can easily be a null | |
729 | * pointer when reading pointer chains. The assumption is program | |
730 | * context will handle null pointer dereference typically via fault | |
731 | * handling. The verifier must keep this in mind and can make no | |
732 | * assumptions about null or non-null when doing branch analysis. | |
733 | * Further, when passed into helpers the helpers can not, without | |
734 | * additional context, assume the value is non-null. | |
735 | */ | |
736 | PTR_TO_BTF_ID, | |
737 | /* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not | |
738 | * been checked for null. Used primarily to inform the verifier | |
739 | * an explicit null check is required for this struct. | |
740 | */ | |
457f4436 | 741 | PTR_TO_MEM, /* reg points to valid memory region */ |
20b2aff4 | 742 | PTR_TO_BUF, /* reg points to a read/write buffer */ |
69c087ba | 743 | PTR_TO_FUNC, /* reg points to a bpf program function */ |
20571567 | 744 | PTR_TO_DYNPTR, /* reg points to a dynptr */ |
e6ac2450 | 745 | __BPF_REG_TYPE_MAX, |
d639b9d1 | 746 | |
c25b2ae1 HL |
747 | /* Extended reg_types. */ |
748 | PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | PTR_TO_MAP_VALUE, | |
749 | PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCKET, | |
750 | PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON, | |
751 | PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | PTR_TO_TCP_SOCK, | |
752 | PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | PTR_TO_BTF_ID, | |
c25b2ae1 | 753 | |
d639b9d1 HL |
754 | /* This must be the last entry. Its purpose is to ensure the enum is |
755 | * wide enough to hold the higher bits reserved for bpf_type_flag. | |
756 | */ | |
757 | __BPF_REG_TYPE_LIMIT = BPF_TYPE_LIMIT, | |
19de99f7 | 758 | }; |
d639b9d1 | 759 | static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT); |
19de99f7 | 760 | |
23994631 YS |
761 | /* The information passed from prog-specific *_is_valid_access |
762 | * back to the verifier. | |
763 | */ | |
764 | struct bpf_insn_access_aux { | |
765 | enum bpf_reg_type reg_type; | |
9e15db66 AS |
766 | union { |
767 | int ctx_field_size; | |
22dc4a0f AN |
768 | struct { |
769 | struct btf *btf; | |
770 | u32 btf_id; | |
771 | }; | |
9e15db66 AS |
772 | }; |
773 | struct bpf_verifier_log *log; /* for verbose logs */ | |
23994631 YS |
774 | }; |
775 | ||
f96da094 DB |
776 | static inline void |
777 | bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size) | |
778 | { | |
779 | aux->ctx_field_size = size; | |
780 | } | |
781 | ||
3990ed4c MKL |
782 | static inline bool bpf_pseudo_func(const struct bpf_insn *insn) |
783 | { | |
784 | return insn->code == (BPF_LD | BPF_IMM | BPF_DW) && | |
785 | insn->src_reg == BPF_PSEUDO_FUNC; | |
786 | } | |
787 | ||
7de16e3a JK |
788 | struct bpf_prog_ops { |
789 | int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr, | |
790 | union bpf_attr __user *uattr); | |
791 | }; | |
792 | ||
6728aea7 | 793 | struct bpf_reg_state; |
09756af4 AS |
794 | struct bpf_verifier_ops { |
795 | /* return eBPF function prototype for verification */ | |
5e43f899 AI |
796 | const struct bpf_func_proto * |
797 | (*get_func_proto)(enum bpf_func_id func_id, | |
798 | const struct bpf_prog *prog); | |
17a52670 AS |
799 | |
800 | /* return true if 'size' wide access at offset 'off' within bpf_context | |
801 | * with 'type' (read or write) is allowed | |
802 | */ | |
19de99f7 | 803 | bool (*is_valid_access)(int off, int size, enum bpf_access_type type, |
5e43f899 | 804 | const struct bpf_prog *prog, |
23994631 | 805 | struct bpf_insn_access_aux *info); |
36bbef52 DB |
806 | int (*gen_prologue)(struct bpf_insn *insn, bool direct_write, |
807 | const struct bpf_prog *prog); | |
e0cea7ce DB |
808 | int (*gen_ld_abs)(const struct bpf_insn *orig, |
809 | struct bpf_insn *insn_buf); | |
6b8cc1d1 DB |
810 | u32 (*convert_ctx_access)(enum bpf_access_type type, |
811 | const struct bpf_insn *src, | |
812 | struct bpf_insn *dst, | |
f96da094 | 813 | struct bpf_prog *prog, u32 *target_size); |
27ae7997 | 814 | int (*btf_struct_access)(struct bpf_verifier_log *log, |
6728aea7 KKD |
815 | const struct bpf_reg_state *reg, |
816 | int off, int size, enum bpf_access_type atype, | |
c6f1bfe8 | 817 | u32 *next_btf_id, enum bpf_type_flag *flag); |
09756af4 AS |
818 | }; |
819 | ||
cae1927c | 820 | struct bpf_prog_offload_ops { |
08ca90af | 821 | /* verifier basic callbacks */ |
cae1927c JK |
822 | int (*insn_hook)(struct bpf_verifier_env *env, |
823 | int insn_idx, int prev_insn_idx); | |
c941ce9c | 824 | int (*finalize)(struct bpf_verifier_env *env); |
08ca90af JK |
825 | /* verifier optimization callbacks (called after .finalize) */ |
826 | int (*replace_insn)(struct bpf_verifier_env *env, u32 off, | |
827 | struct bpf_insn *insn); | |
828 | int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt); | |
829 | /* program management callbacks */ | |
16a8cb5c QM |
830 | int (*prepare)(struct bpf_prog *prog); |
831 | int (*translate)(struct bpf_prog *prog); | |
eb911947 | 832 | void (*destroy)(struct bpf_prog *prog); |
cae1927c JK |
833 | }; |
834 | ||
0a9c1991 | 835 | struct bpf_prog_offload { |
ab3f0063 JK |
836 | struct bpf_prog *prog; |
837 | struct net_device *netdev; | |
341b3e7b | 838 | struct bpf_offload_dev *offdev; |
ab3f0063 JK |
839 | void *dev_priv; |
840 | struct list_head offloads; | |
841 | bool dev_state; | |
08ca90af | 842 | bool opt_failed; |
fcfb126d JW |
843 | void *jited_image; |
844 | u32 jited_len; | |
ab3f0063 JK |
845 | }; |
846 | ||
8bad74f9 RG |
847 | enum bpf_cgroup_storage_type { |
848 | BPF_CGROUP_STORAGE_SHARED, | |
b741f163 | 849 | BPF_CGROUP_STORAGE_PERCPU, |
8bad74f9 RG |
850 | __BPF_CGROUP_STORAGE_MAX |
851 | }; | |
852 | ||
853 | #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX | |
854 | ||
f1b9509c AS |
855 | /* The longest tracepoint has 12 args. |
856 | * See include/trace/bpf_probe.h | |
857 | */ | |
858 | #define MAX_BPF_FUNC_ARGS 12 | |
859 | ||
523a4cf4 DB |
860 | /* The maximum number of arguments passed through registers |
861 | * a single function may have. | |
862 | */ | |
863 | #define MAX_BPF_FUNC_REG_ARGS 5 | |
864 | ||
720e6a43 YS |
865 | /* The argument is a structure. */ |
866 | #define BTF_FMODEL_STRUCT_ARG BIT(0) | |
867 | ||
fec56f58 AS |
868 | struct btf_func_model { |
869 | u8 ret_size; | |
870 | u8 nr_args; | |
871 | u8 arg_size[MAX_BPF_FUNC_ARGS]; | |
720e6a43 | 872 | u8 arg_flags[MAX_BPF_FUNC_ARGS]; |
fec56f58 AS |
873 | }; |
874 | ||
875 | /* Restore arguments before returning from trampoline to let original function | |
876 | * continue executing. This flag is used for fentry progs when there are no | |
877 | * fexit progs. | |
878 | */ | |
879 | #define BPF_TRAMP_F_RESTORE_REGS BIT(0) | |
880 | /* Call original function after fentry progs, but before fexit progs. | |
881 | * Makes sense for fentry/fexit, normal calls and indirect calls. | |
882 | */ | |
883 | #define BPF_TRAMP_F_CALL_ORIG BIT(1) | |
884 | /* Skip current frame and return to parent. Makes sense for fentry/fexit | |
885 | * programs only. Should not be used with normal calls and indirect calls. | |
886 | */ | |
887 | #define BPF_TRAMP_F_SKIP_FRAME BIT(2) | |
7e6f3cd8 JO |
888 | /* Store IP address of the caller on the trampoline stack, |
889 | * so it's available for trampoline's programs. | |
890 | */ | |
891 | #define BPF_TRAMP_F_IP_ARG BIT(3) | |
356ed649 HT |
892 | /* Return the return value of fentry prog. Only used by bpf_struct_ops. */ |
893 | #define BPF_TRAMP_F_RET_FENTRY_RET BIT(4) | |
7e6f3cd8 | 894 | |
316cba62 JO |
895 | /* Get original function from stack instead of from provided direct address. |
896 | * Makes sense for trampolines with fexit or fmod_ret programs. | |
897 | */ | |
898 | #define BPF_TRAMP_F_ORIG_STACK BIT(5) | |
899 | ||
00963a2e SL |
900 | /* This trampoline is on a function with another ftrace_ops with IPMODIFY, |
901 | * e.g., a live patch. This flag is set and cleared by ftrace call backs, | |
902 | */ | |
903 | #define BPF_TRAMP_F_SHARE_IPMODIFY BIT(6) | |
904 | ||
88fd9e53 | 905 | /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 |
b23316aa | 906 | * bytes on x86. |
88fd9e53 | 907 | */ |
f7e0beaf | 908 | #define BPF_MAX_TRAMP_LINKS 38 |
88fd9e53 | 909 | |
f7e0beaf KFL |
910 | struct bpf_tramp_links { |
911 | struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS]; | |
912 | int nr_links; | |
88fd9e53 KS |
913 | }; |
914 | ||
e384c7b7 KFL |
915 | struct bpf_tramp_run_ctx; |
916 | ||
fec56f58 AS |
917 | /* Different use cases for BPF trampoline: |
918 | * 1. replace nop at the function entry (kprobe equivalent) | |
919 | * flags = BPF_TRAMP_F_RESTORE_REGS | |
920 | * fentry = a set of programs to run before returning from trampoline | |
921 | * | |
922 | * 2. replace nop at the function entry (kprobe + kretprobe equivalent) | |
923 | * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME | |
924 | * orig_call = fentry_ip + MCOUNT_INSN_SIZE | |
925 | * fentry = a set of program to run before calling original function | |
926 | * fexit = a set of program to run after original function | |
927 | * | |
928 | * 3. replace direct call instruction anywhere in the function body | |
929 | * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid) | |
930 | * With flags = 0 | |
931 | * fentry = a set of programs to run before returning from trampoline | |
932 | * With flags = BPF_TRAMP_F_CALL_ORIG | |
933 | * orig_call = original callback addr or direct function addr | |
934 | * fentry = a set of program to run before calling original function | |
935 | * fexit = a set of program to run after original function | |
936 | */ | |
e21aa341 AS |
937 | struct bpf_tramp_image; |
938 | int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end, | |
85d33df3 | 939 | const struct btf_func_model *m, u32 flags, |
f7e0beaf | 940 | struct bpf_tramp_links *tlinks, |
fec56f58 | 941 | void *orig_call); |
271de525 MKL |
942 | u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, |
943 | struct bpf_tramp_run_ctx *run_ctx); | |
944 | void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start, | |
945 | struct bpf_tramp_run_ctx *run_ctx); | |
e21aa341 AS |
946 | void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr); |
947 | void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr); | |
271de525 MKL |
948 | typedef u64 (*bpf_trampoline_enter_t)(struct bpf_prog *prog, |
949 | struct bpf_tramp_run_ctx *run_ctx); | |
950 | typedef void (*bpf_trampoline_exit_t)(struct bpf_prog *prog, u64 start, | |
951 | struct bpf_tramp_run_ctx *run_ctx); | |
952 | bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog); | |
953 | bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog); | |
fec56f58 | 954 | |
535911c8 JO |
955 | struct bpf_ksym { |
956 | unsigned long start; | |
957 | unsigned long end; | |
bfea9a85 | 958 | char name[KSYM_NAME_LEN]; |
ecb60d1c | 959 | struct list_head lnode; |
ca4424c9 | 960 | struct latch_tree_node tnode; |
cbd76f8d | 961 | bool prog; |
535911c8 JO |
962 | }; |
963 | ||
fec56f58 AS |
964 | enum bpf_tramp_prog_type { |
965 | BPF_TRAMP_FENTRY, | |
966 | BPF_TRAMP_FEXIT, | |
ae240823 | 967 | BPF_TRAMP_MODIFY_RETURN, |
be8704ff AS |
968 | BPF_TRAMP_MAX, |
969 | BPF_TRAMP_REPLACE, /* more than MAX */ | |
fec56f58 AS |
970 | }; |
971 | ||
e21aa341 AS |
972 | struct bpf_tramp_image { |
973 | void *image; | |
974 | struct bpf_ksym ksym; | |
975 | struct percpu_ref pcref; | |
976 | void *ip_after_call; | |
977 | void *ip_epilogue; | |
978 | union { | |
979 | struct rcu_head rcu; | |
980 | struct work_struct work; | |
981 | }; | |
982 | }; | |
983 | ||
fec56f58 AS |
984 | struct bpf_trampoline { |
985 | /* hlist for trampoline_table */ | |
986 | struct hlist_node hlist; | |
00963a2e | 987 | struct ftrace_ops *fops; |
fec56f58 AS |
988 | /* serializes access to fields of this trampoline */ |
989 | struct mutex mutex; | |
990 | refcount_t refcnt; | |
00963a2e | 991 | u32 flags; |
fec56f58 AS |
992 | u64 key; |
993 | struct { | |
994 | struct btf_func_model model; | |
995 | void *addr; | |
b91e014f | 996 | bool ftrace_managed; |
fec56f58 | 997 | } func; |
be8704ff AS |
998 | /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF |
999 | * program by replacing one of its functions. func.addr is the address | |
1000 | * of the function it replaced. | |
1001 | */ | |
1002 | struct bpf_prog *extension_prog; | |
fec56f58 AS |
1003 | /* list of BPF programs using this trampoline */ |
1004 | struct hlist_head progs_hlist[BPF_TRAMP_MAX]; | |
1005 | /* Number of attached programs. A counter per kind. */ | |
1006 | int progs_cnt[BPF_TRAMP_MAX]; | |
1007 | /* Executable image of trampoline */ | |
e21aa341 | 1008 | struct bpf_tramp_image *cur_image; |
fec56f58 | 1009 | u64 selector; |
861de02e | 1010 | struct module *mod; |
fec56f58 | 1011 | }; |
75ccbef6 | 1012 | |
f7b12b6f THJ |
1013 | struct bpf_attach_target_info { |
1014 | struct btf_func_model fmodel; | |
1015 | long tgt_addr; | |
1016 | const char *tgt_name; | |
1017 | const struct btf_type *tgt_type; | |
1018 | }; | |
1019 | ||
116eb788 | 1020 | #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */ |
75ccbef6 BT |
1021 | |
1022 | struct bpf_dispatcher_prog { | |
1023 | struct bpf_prog *prog; | |
1024 | refcount_t users; | |
1025 | }; | |
1026 | ||
1027 | struct bpf_dispatcher { | |
1028 | /* dispatcher mutex */ | |
1029 | struct mutex mutex; | |
1030 | void *func; | |
1031 | struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX]; | |
1032 | int num_progs; | |
1033 | void *image; | |
19c02415 | 1034 | void *rw_image; |
75ccbef6 | 1035 | u32 image_off; |
517b75e4 | 1036 | struct bpf_ksym ksym; |
75ccbef6 BT |
1037 | }; |
1038 | ||
9f5b4009 | 1039 | static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func( |
7e6897f9 BT |
1040 | const void *ctx, |
1041 | const struct bpf_insn *insnsi, | |
af3f4134 | 1042 | bpf_func_t bpf_func) |
7e6897f9 BT |
1043 | { |
1044 | return bpf_func(ctx, insnsi); | |
1045 | } | |
f7e0beaf | 1046 | |
fec56f58 | 1047 | #ifdef CONFIG_BPF_JIT |
f7e0beaf KFL |
1048 | int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr); |
1049 | int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr); | |
f7b12b6f THJ |
1050 | struct bpf_trampoline *bpf_trampoline_get(u64 key, |
1051 | struct bpf_attach_target_info *tgt_info); | |
fec56f58 | 1052 | void bpf_trampoline_put(struct bpf_trampoline *tr); |
19c02415 | 1053 | int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs); |
dbe69b29 JO |
1054 | int __init bpf_arch_init_dispatcher_early(void *ip); |
1055 | ||
517b75e4 JO |
1056 | #define BPF_DISPATCHER_INIT(_name) { \ |
1057 | .mutex = __MUTEX_INITIALIZER(_name.mutex), \ | |
1058 | .func = &_name##_func, \ | |
1059 | .progs = {}, \ | |
1060 | .num_progs = 0, \ | |
1061 | .image = NULL, \ | |
1062 | .image_off = 0, \ | |
1063 | .ksym = { \ | |
1064 | .name = #_name, \ | |
1065 | .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \ | |
1066 | }, \ | |
75ccbef6 BT |
1067 | } |
1068 | ||
dbe69b29 JO |
1069 | #define BPF_DISPATCHER_INIT_CALL(_name) \ |
1070 | static int __init _name##_init(void) \ | |
1071 | { \ | |
1072 | return bpf_arch_init_dispatcher_early(_name##_func); \ | |
1073 | } \ | |
1074 | early_initcall(_name##_init) | |
1075 | ||
ceea991a JO |
1076 | #ifdef CONFIG_X86_64 |
1077 | #define BPF_DISPATCHER_ATTRIBUTES __attribute__((patchable_function_entry(5))) | |
1078 | #else | |
1079 | #define BPF_DISPATCHER_ATTRIBUTES | |
1080 | #endif | |
1081 | ||
75ccbef6 | 1082 | #define DEFINE_BPF_DISPATCHER(name) \ |
ceea991a | 1083 | notrace BPF_DISPATCHER_ATTRIBUTES \ |
9f5b4009 | 1084 | noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \ |
75ccbef6 BT |
1085 | const void *ctx, \ |
1086 | const struct bpf_insn *insnsi, \ | |
af3f4134 | 1087 | bpf_func_t bpf_func) \ |
75ccbef6 BT |
1088 | { \ |
1089 | return bpf_func(ctx, insnsi); \ | |
1090 | } \ | |
6a64037d BT |
1091 | EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \ |
1092 | struct bpf_dispatcher bpf_dispatcher_##name = \ | |
dbe69b29 JO |
1093 | BPF_DISPATCHER_INIT(bpf_dispatcher_##name); \ |
1094 | BPF_DISPATCHER_INIT_CALL(bpf_dispatcher_##name); | |
1095 | ||
75ccbef6 | 1096 | #define DECLARE_BPF_DISPATCHER(name) \ |
6a64037d | 1097 | unsigned int bpf_dispatcher_##name##_func( \ |
75ccbef6 BT |
1098 | const void *ctx, \ |
1099 | const struct bpf_insn *insnsi, \ | |
af3f4134 | 1100 | bpf_func_t bpf_func); \ |
6a64037d BT |
1101 | extern struct bpf_dispatcher bpf_dispatcher_##name; |
1102 | #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func | |
1103 | #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name) | |
75ccbef6 BT |
1104 | void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from, |
1105 | struct bpf_prog *to); | |
dba122fb | 1106 | /* Called only from JIT-enabled code, so there's no need for stubs. */ |
a108f7dc JO |
1107 | void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym); |
1108 | void bpf_image_ksym_del(struct bpf_ksym *ksym); | |
dba122fb JO |
1109 | void bpf_ksym_add(struct bpf_ksym *ksym); |
1110 | void bpf_ksym_del(struct bpf_ksym *ksym); | |
3486bedd SL |
1111 | int bpf_jit_charge_modmem(u32 size); |
1112 | void bpf_jit_uncharge_modmem(u32 size); | |
f92c1e18 | 1113 | bool bpf_prog_has_trampoline(const struct bpf_prog *prog); |
fec56f58 | 1114 | #else |
f7e0beaf | 1115 | static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link, |
3aac1ead | 1116 | struct bpf_trampoline *tr) |
fec56f58 AS |
1117 | { |
1118 | return -ENOTSUPP; | |
1119 | } | |
f7e0beaf | 1120 | static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, |
3aac1ead | 1121 | struct bpf_trampoline *tr) |
fec56f58 AS |
1122 | { |
1123 | return -ENOTSUPP; | |
1124 | } | |
f7b12b6f THJ |
1125 | static inline struct bpf_trampoline *bpf_trampoline_get(u64 key, |
1126 | struct bpf_attach_target_info *tgt_info) | |
1127 | { | |
1128 | return ERR_PTR(-EOPNOTSUPP); | |
1129 | } | |
fec56f58 | 1130 | static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {} |
75ccbef6 BT |
1131 | #define DEFINE_BPF_DISPATCHER(name) |
1132 | #define DECLARE_BPF_DISPATCHER(name) | |
6a64037d | 1133 | #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func |
75ccbef6 BT |
1134 | #define BPF_DISPATCHER_PTR(name) NULL |
1135 | static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, | |
1136 | struct bpf_prog *from, | |
1137 | struct bpf_prog *to) {} | |
e9b4e606 JO |
1138 | static inline bool is_bpf_image_address(unsigned long address) |
1139 | { | |
1140 | return false; | |
1141 | } | |
f92c1e18 JO |
1142 | static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog) |
1143 | { | |
1144 | return false; | |
1145 | } | |
fec56f58 AS |
1146 | #endif |
1147 | ||
8c1b6e69 | 1148 | struct bpf_func_info_aux { |
51c39bb1 | 1149 | u16 linkage; |
8c1b6e69 AS |
1150 | bool unreliable; |
1151 | }; | |
1152 | ||
a66886fe DB |
1153 | enum bpf_jit_poke_reason { |
1154 | BPF_POKE_REASON_TAIL_CALL, | |
1155 | }; | |
1156 | ||
1157 | /* Descriptor of pokes pointing /into/ the JITed image. */ | |
1158 | struct bpf_jit_poke_descriptor { | |
cf71b174 | 1159 | void *tailcall_target; |
ebf7d1f5 MF |
1160 | void *tailcall_bypass; |
1161 | void *bypass_addr; | |
f263a814 | 1162 | void *aux; |
a66886fe DB |
1163 | union { |
1164 | struct { | |
1165 | struct bpf_map *map; | |
1166 | u32 key; | |
1167 | } tail_call; | |
1168 | }; | |
cf71b174 | 1169 | bool tailcall_target_stable; |
a66886fe DB |
1170 | u8 adj_off; |
1171 | u16 reason; | |
a748c697 | 1172 | u32 insn_idx; |
a66886fe DB |
1173 | }; |
1174 | ||
3c32cc1b YS |
1175 | /* reg_type info for ctx arguments */ |
1176 | struct bpf_ctx_arg_aux { | |
1177 | u32 offset; | |
1178 | enum bpf_reg_type reg_type; | |
951cf368 | 1179 | u32 btf_id; |
3c32cc1b YS |
1180 | }; |
1181 | ||
541c3bad AN |
1182 | struct btf_mod_pair { |
1183 | struct btf *btf; | |
1184 | struct module *module; | |
1185 | }; | |
1186 | ||
e6ac2450 MKL |
1187 | struct bpf_kfunc_desc_tab; |
1188 | ||
09756af4 | 1189 | struct bpf_prog_aux { |
85192dbf | 1190 | atomic64_t refcnt; |
24701ece | 1191 | u32 used_map_cnt; |
541c3bad | 1192 | u32 used_btf_cnt; |
32bbe007 | 1193 | u32 max_ctx_offset; |
e647815a | 1194 | u32 max_pkt_offset; |
9df1c28b | 1195 | u32 max_tp_access; |
8726679a | 1196 | u32 stack_depth; |
dc4bb0e2 | 1197 | u32 id; |
ba64e7d8 YS |
1198 | u32 func_cnt; /* used by non-func prog as the number of func progs */ |
1199 | u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */ | |
ccfe29eb | 1200 | u32 attach_btf_id; /* in-kernel BTF type id to attach to */ |
3c32cc1b | 1201 | u32 ctx_arg_info_size; |
afbf21dc YS |
1202 | u32 max_rdonly_access; |
1203 | u32 max_rdwr_access; | |
22dc4a0f | 1204 | struct btf *attach_btf; |
3c32cc1b | 1205 | const struct bpf_ctx_arg_aux *ctx_arg_info; |
3aac1ead THJ |
1206 | struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */ |
1207 | struct bpf_prog *dst_prog; | |
1208 | struct bpf_trampoline *dst_trampoline; | |
4a1e7c0c THJ |
1209 | enum bpf_prog_type saved_dst_prog_type; |
1210 | enum bpf_attach_type saved_dst_attach_type; | |
a4b1d3c1 | 1211 | bool verifier_zext; /* Zero extensions has been inserted by verifier. */ |
9a18eedb | 1212 | bool offload_requested; |
38207291 | 1213 | bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */ |
8c1b6e69 | 1214 | bool func_proto_unreliable; |
1e6c62a8 | 1215 | bool sleepable; |
ebf7d1f5 | 1216 | bool tail_call_reachable; |
c2f2cdbe | 1217 | bool xdp_has_frags; |
38207291 MKL |
1218 | /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ |
1219 | const struct btf_type *attach_func_proto; | |
1220 | /* function name for valid attach_btf_id */ | |
1221 | const char *attach_func_name; | |
1c2a088a AS |
1222 | struct bpf_prog **func; |
1223 | void *jit_data; /* JIT specific data. arch dependent */ | |
a66886fe | 1224 | struct bpf_jit_poke_descriptor *poke_tab; |
e6ac2450 | 1225 | struct bpf_kfunc_desc_tab *kfunc_tab; |
2357672c | 1226 | struct bpf_kfunc_btf_tab *kfunc_btf_tab; |
a66886fe | 1227 | u32 size_poke_tab; |
535911c8 | 1228 | struct bpf_ksym ksym; |
7de16e3a | 1229 | const struct bpf_prog_ops *ops; |
09756af4 | 1230 | struct bpf_map **used_maps; |
984fe94f | 1231 | struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */ |
541c3bad | 1232 | struct btf_mod_pair *used_btfs; |
09756af4 | 1233 | struct bpf_prog *prog; |
aaac3ba9 | 1234 | struct user_struct *user; |
cb4d2b3f | 1235 | u64 load_time; /* ns since boottime */ |
aba64c7d | 1236 | u32 verified_insns; |
69fd337a | 1237 | int cgroup_atype; /* enum cgroup_bpf_attach_type */ |
8bad74f9 | 1238 | struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]; |
067cae47 | 1239 | char name[BPF_OBJ_NAME_LEN]; |
afdb09c7 CF |
1240 | #ifdef CONFIG_SECURITY |
1241 | void *security; | |
1242 | #endif | |
0a9c1991 | 1243 | struct bpf_prog_offload *offload; |
838e9690 | 1244 | struct btf *btf; |
ba64e7d8 | 1245 | struct bpf_func_info *func_info; |
8c1b6e69 | 1246 | struct bpf_func_info_aux *func_info_aux; |
c454a46b MKL |
1247 | /* bpf_line_info loaded from userspace. linfo->insn_off |
1248 | * has the xlated insn offset. | |
1249 | * Both the main and sub prog share the same linfo. | |
1250 | * The subprog can access its first linfo by | |
1251 | * using the linfo_idx. | |
1252 | */ | |
1253 | struct bpf_line_info *linfo; | |
1254 | /* jited_linfo is the jited addr of the linfo. It has a | |
1255 | * one to one mapping to linfo: | |
1256 | * jited_linfo[i] is the jited addr for the linfo[i]->insn_off. | |
1257 | * Both the main and sub prog share the same jited_linfo. | |
1258 | * The subprog can access its first jited_linfo by | |
1259 | * using the linfo_idx. | |
1260 | */ | |
1261 | void **jited_linfo; | |
ba64e7d8 | 1262 | u32 func_info_cnt; |
c454a46b MKL |
1263 | u32 nr_linfo; |
1264 | /* subprog can use linfo_idx to access its first linfo and | |
1265 | * jited_linfo. | |
1266 | * main prog always has linfo_idx == 0 | |
1267 | */ | |
1268 | u32 linfo_idx; | |
3dec541b AS |
1269 | u32 num_exentries; |
1270 | struct exception_table_entry *extable; | |
abf2e7d6 AS |
1271 | union { |
1272 | struct work_struct work; | |
1273 | struct rcu_head rcu; | |
1274 | }; | |
09756af4 AS |
1275 | }; |
1276 | ||
d687f621 DK |
1277 | struct bpf_prog { |
1278 | u16 pages; /* Number of allocated pages */ | |
1279 | u16 jited:1, /* Is our filter JIT'ed? */ | |
1280 | jit_requested:1,/* archs need to JIT the prog */ | |
1281 | gpl_compatible:1, /* Is filter GPL compatible? */ | |
1282 | cb_access:1, /* Is control block accessed? */ | |
1283 | dst_needed:1, /* Do we need dst entry? */ | |
1284 | blinding_requested:1, /* needs constant blinding */ | |
1285 | blinded:1, /* Was blinded */ | |
1286 | is_func:1, /* program is a bpf function */ | |
1287 | kprobe_override:1, /* Do we override a kprobe? */ | |
1288 | has_callchain_buf:1, /* callchain buffer allocated? */ | |
1289 | enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */ | |
1290 | call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ | |
1291 | call_get_func_ip:1, /* Do we call get_func_ip() */ | |
1292 | tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */ | |
1293 | enum bpf_prog_type type; /* Type of BPF program */ | |
1294 | enum bpf_attach_type expected_attach_type; /* For some prog types */ | |
1295 | u32 len; /* Number of filter blocks */ | |
1296 | u32 jited_len; /* Size of jited insns in bytes */ | |
1297 | u8 tag[BPF_TAG_SIZE]; | |
1298 | struct bpf_prog_stats __percpu *stats; | |
1299 | int __percpu *active; | |
1300 | unsigned int (*bpf_func)(const void *ctx, | |
1301 | const struct bpf_insn *insn); | |
1302 | struct bpf_prog_aux *aux; /* Auxiliary fields */ | |
1303 | struct sock_fprog_kern *orig_prog; /* Original BPF program */ | |
1304 | /* Instructions for interpreter */ | |
1305 | union { | |
1306 | DECLARE_FLEX_ARRAY(struct sock_filter, insns); | |
1307 | DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi); | |
1308 | }; | |
1309 | }; | |
1310 | ||
2beee5f5 | 1311 | struct bpf_array_aux { |
da765a2f DB |
1312 | /* Programs with direct jumps into programs part of this array. */ |
1313 | struct list_head poke_progs; | |
1314 | struct bpf_map *map; | |
1315 | struct mutex poke_mutex; | |
1316 | struct work_struct work; | |
2beee5f5 DB |
1317 | }; |
1318 | ||
6cc7d1e8 AN |
1319 | struct bpf_link { |
1320 | atomic64_t refcnt; | |
1321 | u32 id; | |
1322 | enum bpf_link_type type; | |
1323 | const struct bpf_link_ops *ops; | |
1324 | struct bpf_prog *prog; | |
1325 | struct work_struct work; | |
1326 | }; | |
1327 | ||
1328 | struct bpf_link_ops { | |
1329 | void (*release)(struct bpf_link *link); | |
1330 | void (*dealloc)(struct bpf_link *link); | |
73b11c2a | 1331 | int (*detach)(struct bpf_link *link); |
6cc7d1e8 AN |
1332 | int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog, |
1333 | struct bpf_prog *old_prog); | |
1334 | void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq); | |
1335 | int (*fill_link_info)(const struct bpf_link *link, | |
1336 | struct bpf_link_info *info); | |
1337 | }; | |
1338 | ||
f7e0beaf KFL |
1339 | struct bpf_tramp_link { |
1340 | struct bpf_link link; | |
1341 | struct hlist_node tramp_hlist; | |
2fcc8241 | 1342 | u64 cookie; |
f7e0beaf KFL |
1343 | }; |
1344 | ||
69fd337a SF |
1345 | struct bpf_shim_tramp_link { |
1346 | struct bpf_tramp_link link; | |
1347 | struct bpf_trampoline *trampoline; | |
1348 | }; | |
1349 | ||
f7e0beaf KFL |
1350 | struct bpf_tracing_link { |
1351 | struct bpf_tramp_link link; | |
1352 | enum bpf_attach_type attach_type; | |
1353 | struct bpf_trampoline *trampoline; | |
1354 | struct bpf_prog *tgt_prog; | |
1355 | }; | |
1356 | ||
6cc7d1e8 AN |
1357 | struct bpf_link_primer { |
1358 | struct bpf_link *link; | |
1359 | struct file *file; | |
1360 | int fd; | |
1361 | u32 id; | |
1362 | }; | |
1363 | ||
85d33df3 | 1364 | struct bpf_struct_ops_value; |
27ae7997 MKL |
1365 | struct btf_member; |
1366 | ||
1367 | #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64 | |
1368 | struct bpf_struct_ops { | |
1369 | const struct bpf_verifier_ops *verifier_ops; | |
1370 | int (*init)(struct btf *btf); | |
1371 | int (*check_member)(const struct btf_type *t, | |
1372 | const struct btf_member *member); | |
85d33df3 MKL |
1373 | int (*init_member)(const struct btf_type *t, |
1374 | const struct btf_member *member, | |
1375 | void *kdata, const void *udata); | |
1376 | int (*reg)(void *kdata); | |
1377 | void (*unreg)(void *kdata); | |
27ae7997 | 1378 | const struct btf_type *type; |
85d33df3 | 1379 | const struct btf_type *value_type; |
27ae7997 MKL |
1380 | const char *name; |
1381 | struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS]; | |
1382 | u32 type_id; | |
85d33df3 | 1383 | u32 value_id; |
27ae7997 MKL |
1384 | }; |
1385 | ||
1386 | #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL) | |
85d33df3 | 1387 | #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA)) |
27ae7997 | 1388 | const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id); |
d3e42bb0 | 1389 | void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log); |
85d33df3 MKL |
1390 | bool bpf_struct_ops_get(const void *kdata); |
1391 | void bpf_struct_ops_put(const void *kdata); | |
1392 | int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key, | |
1393 | void *value); | |
f7e0beaf KFL |
1394 | int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks, |
1395 | struct bpf_tramp_link *link, | |
31a645ae HT |
1396 | const struct btf_func_model *model, |
1397 | void *image, void *image_end); | |
85d33df3 MKL |
1398 | static inline bool bpf_try_module_get(const void *data, struct module *owner) |
1399 | { | |
1400 | if (owner == BPF_MODULE_OWNER) | |
1401 | return bpf_struct_ops_get(data); | |
1402 | else | |
1403 | return try_module_get(owner); | |
1404 | } | |
1405 | static inline void bpf_module_put(const void *data, struct module *owner) | |
1406 | { | |
1407 | if (owner == BPF_MODULE_OWNER) | |
1408 | bpf_struct_ops_put(data); | |
1409 | else | |
1410 | module_put(owner); | |
1411 | } | |
c196906d HT |
1412 | |
1413 | #ifdef CONFIG_NET | |
1414 | /* Define it here to avoid the use of forward declaration */ | |
1415 | struct bpf_dummy_ops_state { | |
1416 | int val; | |
1417 | }; | |
1418 | ||
1419 | struct bpf_dummy_ops { | |
1420 | int (*test_1)(struct bpf_dummy_ops_state *cb); | |
1421 | int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2, | |
1422 | char a3, unsigned long a4); | |
1423 | }; | |
1424 | ||
1425 | int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr, | |
1426 | union bpf_attr __user *uattr); | |
1427 | #endif | |
27ae7997 MKL |
1428 | #else |
1429 | static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) | |
1430 | { | |
1431 | return NULL; | |
1432 | } | |
d3e42bb0 MKL |
1433 | static inline void bpf_struct_ops_init(struct btf *btf, |
1434 | struct bpf_verifier_log *log) | |
1435 | { | |
1436 | } | |
85d33df3 MKL |
1437 | static inline bool bpf_try_module_get(const void *data, struct module *owner) |
1438 | { | |
1439 | return try_module_get(owner); | |
1440 | } | |
1441 | static inline void bpf_module_put(const void *data, struct module *owner) | |
1442 | { | |
1443 | module_put(owner); | |
1444 | } | |
1445 | static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, | |
1446 | void *key, | |
1447 | void *value) | |
1448 | { | |
1449 | return -EINVAL; | |
1450 | } | |
9cb61fda SF |
1451 | #endif |
1452 | ||
1453 | #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM) | |
1454 | int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, | |
1455 | int cgroup_atype); | |
1456 | void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog); | |
1457 | #else | |
69fd337a SF |
1458 | static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, |
1459 | int cgroup_atype) | |
1460 | { | |
1461 | return -EOPNOTSUPP; | |
1462 | } | |
1463 | static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog) | |
1464 | { | |
1465 | } | |
27ae7997 MKL |
1466 | #endif |
1467 | ||
04fd61ab AS |
1468 | struct bpf_array { |
1469 | struct bpf_map map; | |
1470 | u32 elem_size; | |
b2157399 | 1471 | u32 index_mask; |
2beee5f5 | 1472 | struct bpf_array_aux *aux; |
04fd61ab AS |
1473 | union { |
1474 | char value[0] __aligned(8); | |
2a36f0b9 | 1475 | void *ptrs[0] __aligned(8); |
a10423b8 | 1476 | void __percpu *pptrs[0] __aligned(8); |
04fd61ab AS |
1477 | }; |
1478 | }; | |
3b1efb19 | 1479 | |
c04c0d2b | 1480 | #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */ |
ebf7f6f0 | 1481 | #define MAX_TAIL_CALL_CNT 33 |
04fd61ab | 1482 | |
1ade2371 EZ |
1483 | /* Maximum number of loops for bpf_loop */ |
1484 | #define BPF_MAX_LOOPS BIT(23) | |
1485 | ||
591fe988 DB |
1486 | #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \ |
1487 | BPF_F_RDONLY_PROG | \ | |
1488 | BPF_F_WRONLY | \ | |
1489 | BPF_F_WRONLY_PROG) | |
1490 | ||
1491 | #define BPF_MAP_CAN_READ BIT(0) | |
1492 | #define BPF_MAP_CAN_WRITE BIT(1) | |
1493 | ||
20571567 DV |
1494 | /* Maximum number of user-producer ring buffer samples that can be drained in |
1495 | * a call to bpf_user_ringbuf_drain(). | |
1496 | */ | |
1497 | #define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024) | |
1498 | ||
591fe988 DB |
1499 | static inline u32 bpf_map_flags_to_cap(struct bpf_map *map) |
1500 | { | |
1501 | u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG); | |
1502 | ||
1503 | /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is | |
1504 | * not possible. | |
1505 | */ | |
1506 | if (access_flags & BPF_F_RDONLY_PROG) | |
1507 | return BPF_MAP_CAN_READ; | |
1508 | else if (access_flags & BPF_F_WRONLY_PROG) | |
1509 | return BPF_MAP_CAN_WRITE; | |
1510 | else | |
1511 | return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE; | |
1512 | } | |
1513 | ||
1514 | static inline bool bpf_map_flags_access_ok(u32 access_flags) | |
1515 | { | |
1516 | return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) != | |
1517 | (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG); | |
1518 | } | |
1519 | ||
3b1efb19 DB |
1520 | struct bpf_event_entry { |
1521 | struct perf_event *event; | |
1522 | struct file *perf_file; | |
1523 | struct file *map_file; | |
1524 | struct rcu_head rcu; | |
1525 | }; | |
1526 | ||
f45d5b6c THJ |
1527 | static inline bool map_type_contains_progs(struct bpf_map *map) |
1528 | { | |
1529 | return map->map_type == BPF_MAP_TYPE_PROG_ARRAY || | |
1530 | map->map_type == BPF_MAP_TYPE_DEVMAP || | |
1531 | map->map_type == BPF_MAP_TYPE_CPUMAP; | |
1532 | } | |
1533 | ||
1534 | bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp); | |
f1f7714e | 1535 | int bpf_prog_calc_tag(struct bpf_prog *fp); |
bd570ff9 | 1536 | |
0756ea3e | 1537 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void); |
10aceb62 | 1538 | const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void); |
555c8a86 DB |
1539 | |
1540 | typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src, | |
aa7145c1 | 1541 | unsigned long off, unsigned long len); |
c64b7983 JS |
1542 | typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type, |
1543 | const struct bpf_insn *src, | |
1544 | struct bpf_insn *dst, | |
1545 | struct bpf_prog *prog, | |
1546 | u32 *target_size); | |
555c8a86 DB |
1547 | |
1548 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, | |
1549 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy); | |
04fd61ab | 1550 | |
324bda9e AS |
1551 | /* an array of programs to be executed under rcu_lock. |
1552 | * | |
1553 | * Typical usage: | |
055eb955 | 1554 | * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run); |
324bda9e AS |
1555 | * |
1556 | * the structure returned by bpf_prog_array_alloc() should be populated | |
1557 | * with program pointers and the last pointer must be NULL. | |
1558 | * The user has to keep refcnt on the program and make sure the program | |
1559 | * is removed from the array before bpf_prog_put(). | |
1560 | * The 'struct bpf_prog_array *' should only be replaced with xchg() | |
1561 | * since other cpus are walking the array of pointers in parallel. | |
1562 | */ | |
394e40a2 RG |
1563 | struct bpf_prog_array_item { |
1564 | struct bpf_prog *prog; | |
82e6b1ee AN |
1565 | union { |
1566 | struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]; | |
1567 | u64 bpf_cookie; | |
1568 | }; | |
394e40a2 RG |
1569 | }; |
1570 | ||
324bda9e AS |
1571 | struct bpf_prog_array { |
1572 | struct rcu_head rcu; | |
d7f10df8 | 1573 | struct bpf_prog_array_item items[]; |
324bda9e AS |
1574 | }; |
1575 | ||
46531a30 PB |
1576 | struct bpf_empty_prog_array { |
1577 | struct bpf_prog_array hdr; | |
1578 | struct bpf_prog *null_prog; | |
1579 | }; | |
1580 | ||
1581 | /* to avoid allocating empty bpf_prog_array for cgroups that | |
1582 | * don't have bpf program attached use one global 'bpf_empty_prog_array' | |
1583 | * It will not be modified the caller of bpf_prog_array_alloc() | |
1584 | * (since caller requested prog_cnt == 0) | |
1585 | * that pointer should be 'freed' by bpf_prog_array_free() | |
1586 | */ | |
1587 | extern struct bpf_empty_prog_array bpf_empty_prog_array; | |
1588 | ||
d29ab6e1 | 1589 | struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags); |
54e9c9d4 | 1590 | void bpf_prog_array_free(struct bpf_prog_array *progs); |
8c7dcb84 DK |
1591 | /* Use when traversal over the bpf_prog_array uses tasks_trace rcu */ |
1592 | void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs); | |
54e9c9d4 | 1593 | int bpf_prog_array_length(struct bpf_prog_array *progs); |
0d01da6a | 1594 | bool bpf_prog_array_is_empty(struct bpf_prog_array *array); |
54e9c9d4 | 1595 | int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs, |
468e2f64 | 1596 | __u32 __user *prog_ids, u32 cnt); |
324bda9e | 1597 | |
54e9c9d4 | 1598 | void bpf_prog_array_delete_safe(struct bpf_prog_array *progs, |
e87c6bc3 | 1599 | struct bpf_prog *old_prog); |
ce3aa9cc JS |
1600 | int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index); |
1601 | int bpf_prog_array_update_at(struct bpf_prog_array *array, int index, | |
1602 | struct bpf_prog *prog); | |
54e9c9d4 | 1603 | int bpf_prog_array_copy_info(struct bpf_prog_array *array, |
3a38bb98 YS |
1604 | u32 *prog_ids, u32 request_cnt, |
1605 | u32 *prog_cnt); | |
54e9c9d4 | 1606 | int bpf_prog_array_copy(struct bpf_prog_array *old_array, |
e87c6bc3 YS |
1607 | struct bpf_prog *exclude_prog, |
1608 | struct bpf_prog *include_prog, | |
82e6b1ee | 1609 | u64 bpf_cookie, |
e87c6bc3 YS |
1610 | struct bpf_prog_array **new_array); |
1611 | ||
c7603cfa AN |
1612 | struct bpf_run_ctx {}; |
1613 | ||
1614 | struct bpf_cg_run_ctx { | |
1615 | struct bpf_run_ctx run_ctx; | |
7d08c2c9 | 1616 | const struct bpf_prog_array_item *prog_item; |
c4dcfdd4 | 1617 | int retval; |
c7603cfa AN |
1618 | }; |
1619 | ||
82e6b1ee AN |
1620 | struct bpf_trace_run_ctx { |
1621 | struct bpf_run_ctx run_ctx; | |
1622 | u64 bpf_cookie; | |
1623 | }; | |
1624 | ||
e384c7b7 KFL |
1625 | struct bpf_tramp_run_ctx { |
1626 | struct bpf_run_ctx run_ctx; | |
1627 | u64 bpf_cookie; | |
1628 | struct bpf_run_ctx *saved_run_ctx; | |
1629 | }; | |
1630 | ||
7d08c2c9 AN |
1631 | static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx) |
1632 | { | |
1633 | struct bpf_run_ctx *old_ctx = NULL; | |
1634 | ||
1635 | #ifdef CONFIG_BPF_SYSCALL | |
1636 | old_ctx = current->bpf_ctx; | |
1637 | current->bpf_ctx = new_ctx; | |
1638 | #endif | |
1639 | return old_ctx; | |
1640 | } | |
1641 | ||
1642 | static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx) | |
1643 | { | |
1644 | #ifdef CONFIG_BPF_SYSCALL | |
1645 | current->bpf_ctx = old_ctx; | |
1646 | #endif | |
1647 | } | |
1648 | ||
77241217 SF |
1649 | /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ |
1650 | #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0) | |
1651 | /* BPF program asks to set CN on the packet. */ | |
1652 | #define BPF_RET_SET_CN (1 << 0) | |
1653 | ||
7d08c2c9 AN |
1654 | typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx); |
1655 | ||
7d08c2c9 | 1656 | static __always_inline u32 |
055eb955 | 1657 | bpf_prog_run_array(const struct bpf_prog_array *array, |
7d08c2c9 AN |
1658 | const void *ctx, bpf_prog_run_fn run_prog) |
1659 | { | |
1660 | const struct bpf_prog_array_item *item; | |
1661 | const struct bpf_prog *prog; | |
82e6b1ee AN |
1662 | struct bpf_run_ctx *old_run_ctx; |
1663 | struct bpf_trace_run_ctx run_ctx; | |
7d08c2c9 AN |
1664 | u32 ret = 1; |
1665 | ||
055eb955 SF |
1666 | RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held"); |
1667 | ||
7d08c2c9 | 1668 | if (unlikely(!array)) |
055eb955 SF |
1669 | return ret; |
1670 | ||
1671 | migrate_disable(); | |
82e6b1ee | 1672 | old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); |
7d08c2c9 AN |
1673 | item = &array->items[0]; |
1674 | while ((prog = READ_ONCE(item->prog))) { | |
82e6b1ee | 1675 | run_ctx.bpf_cookie = item->bpf_cookie; |
7d08c2c9 AN |
1676 | ret &= run_prog(prog, ctx); |
1677 | item++; | |
1678 | } | |
82e6b1ee | 1679 | bpf_reset_run_ctx(old_run_ctx); |
7d08c2c9 AN |
1680 | migrate_enable(); |
1681 | return ret; | |
1682 | } | |
324bda9e | 1683 | |
8c7dcb84 DK |
1684 | /* Notes on RCU design for bpf_prog_arrays containing sleepable programs: |
1685 | * | |
1686 | * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array | |
1687 | * overall. As a result, we must use the bpf_prog_array_free_sleepable | |
1688 | * in order to use the tasks_trace rcu grace period. | |
1689 | * | |
1690 | * When a non-sleepable program is inside the array, we take the rcu read | |
1691 | * section and disable preemption for that program alone, so it can access | |
1692 | * rcu-protected dynamically sized maps. | |
1693 | */ | |
1694 | static __always_inline u32 | |
1695 | bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu, | |
1696 | const void *ctx, bpf_prog_run_fn run_prog) | |
1697 | { | |
1698 | const struct bpf_prog_array_item *item; | |
1699 | const struct bpf_prog *prog; | |
1700 | const struct bpf_prog_array *array; | |
1701 | struct bpf_run_ctx *old_run_ctx; | |
1702 | struct bpf_trace_run_ctx run_ctx; | |
1703 | u32 ret = 1; | |
1704 | ||
1705 | might_fault(); | |
1706 | ||
1707 | rcu_read_lock_trace(); | |
1708 | migrate_disable(); | |
1709 | ||
1710 | array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held()); | |
1711 | if (unlikely(!array)) | |
1712 | goto out; | |
1713 | old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); | |
1714 | item = &array->items[0]; | |
1715 | while ((prog = READ_ONCE(item->prog))) { | |
1716 | if (!prog->aux->sleepable) | |
1717 | rcu_read_lock(); | |
1718 | ||
1719 | run_ctx.bpf_cookie = item->bpf_cookie; | |
1720 | ret &= run_prog(prog, ctx); | |
1721 | item++; | |
1722 | ||
1723 | if (!prog->aux->sleepable) | |
1724 | rcu_read_unlock(); | |
1725 | } | |
1726 | bpf_reset_run_ctx(old_run_ctx); | |
1727 | out: | |
1728 | migrate_enable(); | |
1729 | rcu_read_unlock_trace(); | |
1730 | return ret; | |
1731 | } | |
1732 | ||
89aa0758 | 1733 | #ifdef CONFIG_BPF_SYSCALL |
b121d1e7 | 1734 | DECLARE_PER_CPU(int, bpf_prog_active); |
d46edd67 | 1735 | extern struct mutex bpf_stats_enabled_mutex; |
b121d1e7 | 1736 | |
c518cfa0 TG |
1737 | /* |
1738 | * Block execution of BPF programs attached to instrumentation (perf, | |
1739 | * kprobes, tracepoints) to prevent deadlocks on map operations as any of | |
1740 | * these events can happen inside a region which holds a map bucket lock | |
1741 | * and can deadlock on it. | |
c518cfa0 TG |
1742 | */ |
1743 | static inline void bpf_disable_instrumentation(void) | |
1744 | { | |
1745 | migrate_disable(); | |
79364031 | 1746 | this_cpu_inc(bpf_prog_active); |
c518cfa0 TG |
1747 | } |
1748 | ||
1749 | static inline void bpf_enable_instrumentation(void) | |
1750 | { | |
79364031 | 1751 | this_cpu_dec(bpf_prog_active); |
c518cfa0 TG |
1752 | migrate_enable(); |
1753 | } | |
1754 | ||
f66e448c CF |
1755 | extern const struct file_operations bpf_map_fops; |
1756 | extern const struct file_operations bpf_prog_fops; | |
367ec3e4 | 1757 | extern const struct file_operations bpf_iter_fops; |
f66e448c | 1758 | |
91cc1a99 | 1759 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
7de16e3a JK |
1760 | extern const struct bpf_prog_ops _name ## _prog_ops; \ |
1761 | extern const struct bpf_verifier_ops _name ## _verifier_ops; | |
40077e0c JB |
1762 | #define BPF_MAP_TYPE(_id, _ops) \ |
1763 | extern const struct bpf_map_ops _ops; | |
f2e10bff | 1764 | #define BPF_LINK_TYPE(_id, _name) |
be9370a7 JB |
1765 | #include <linux/bpf_types.h> |
1766 | #undef BPF_PROG_TYPE | |
40077e0c | 1767 | #undef BPF_MAP_TYPE |
f2e10bff | 1768 | #undef BPF_LINK_TYPE |
0fc174de | 1769 | |
ab3f0063 | 1770 | extern const struct bpf_prog_ops bpf_offload_prog_ops; |
4f9218aa JK |
1771 | extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops; |
1772 | extern const struct bpf_verifier_ops xdp_analyzer_ops; | |
1773 | ||
0fc174de | 1774 | struct bpf_prog *bpf_prog_get(u32 ufd); |
248f346f | 1775 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
288b3de5 | 1776 | bool attach_drv); |
85192dbf | 1777 | void bpf_prog_add(struct bpf_prog *prog, int i); |
c540594f | 1778 | void bpf_prog_sub(struct bpf_prog *prog, int i); |
85192dbf | 1779 | void bpf_prog_inc(struct bpf_prog *prog); |
a6f6df69 | 1780 | struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog); |
61e021f3 DB |
1781 | void bpf_prog_put(struct bpf_prog *prog); |
1782 | ||
ad8ad79f | 1783 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock); |
a3884572 | 1784 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock); |
ad8ad79f | 1785 | |
aa3496ac KKD |
1786 | struct btf_field *btf_record_find(const struct btf_record *rec, |
1787 | u32 offset, enum btf_field_type type); | |
1788 | void btf_record_free(struct btf_record *rec); | |
1789 | void bpf_map_free_record(struct bpf_map *map); | |
1790 | struct btf_record *btf_record_dup(const struct btf_record *rec); | |
1791 | bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b); | |
db559117 | 1792 | void bpf_obj_free_timer(const struct btf_record *rec, void *obj); |
aa3496ac | 1793 | void bpf_obj_free_fields(const struct btf_record *rec, void *obj); |
61df10c7 | 1794 | |
1ed4d924 | 1795 | struct bpf_map *bpf_map_get(u32 ufd); |
c9da161c | 1796 | struct bpf_map *bpf_map_get_with_uref(u32 ufd); |
c2101297 | 1797 | struct bpf_map *__bpf_map_get(struct fd f); |
1e0bd5a0 AN |
1798 | void bpf_map_inc(struct bpf_map *map); |
1799 | void bpf_map_inc_with_uref(struct bpf_map *map); | |
1800 | struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map); | |
c9da161c | 1801 | void bpf_map_put_with_uref(struct bpf_map *map); |
61e021f3 | 1802 | void bpf_map_put(struct bpf_map *map); |
196e8ca7 DB |
1803 | void *bpf_map_area_alloc(u64 size, int numa_node); |
1804 | void *bpf_map_area_mmapable_alloc(u64 size, int numa_node); | |
d407bd25 | 1805 | void bpf_map_area_free(void *base); |
353050be | 1806 | bool bpf_map_write_active(const struct bpf_map *map); |
bd475643 | 1807 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); |
cb4d03ab BV |
1808 | int generic_map_lookup_batch(struct bpf_map *map, |
1809 | const union bpf_attr *attr, | |
aa2e93b8 | 1810 | union bpf_attr __user *uattr); |
3af43ba4 | 1811 | int generic_map_update_batch(struct bpf_map *map, struct file *map_file, |
aa2e93b8 BV |
1812 | const union bpf_attr *attr, |
1813 | union bpf_attr __user *uattr); | |
1814 | int generic_map_delete_batch(struct bpf_map *map, | |
1815 | const union bpf_attr *attr, | |
cb4d03ab | 1816 | union bpf_attr __user *uattr); |
6086d29d | 1817 | struct bpf_map *bpf_map_get_curr_or_next(u32 *id); |
a228a64f | 1818 | struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id); |
61e021f3 | 1819 | |
48edc1f7 RG |
1820 | #ifdef CONFIG_MEMCG_KMEM |
1821 | void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, | |
1822 | int node); | |
1823 | void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags); | |
1824 | void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, | |
1825 | size_t align, gfp_t flags); | |
1826 | #else | |
1827 | static inline void * | |
1828 | bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, | |
1829 | int node) | |
1830 | { | |
1831 | return kmalloc_node(size, flags, node); | |
1832 | } | |
1833 | ||
1834 | static inline void * | |
1835 | bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) | |
1836 | { | |
1837 | return kzalloc(size, flags); | |
1838 | } | |
1839 | ||
1840 | static inline void __percpu * | |
1841 | bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, | |
1842 | gfp_t flags) | |
1843 | { | |
1844 | return __alloc_percpu_gfp(size, align, flags); | |
1845 | } | |
1846 | #endif | |
1847 | ||
1be7f75d AS |
1848 | extern int sysctl_unprivileged_bpf_disabled; |
1849 | ||
2c78ee89 AS |
1850 | static inline bool bpf_allow_ptr_leaks(void) |
1851 | { | |
1852 | return perfmon_capable(); | |
1853 | } | |
1854 | ||
01f810ac AM |
1855 | static inline bool bpf_allow_uninit_stack(void) |
1856 | { | |
1857 | return perfmon_capable(); | |
1858 | } | |
1859 | ||
41c48f3a AI |
1860 | static inline bool bpf_allow_ptr_to_map_access(void) |
1861 | { | |
1862 | return perfmon_capable(); | |
1863 | } | |
1864 | ||
2c78ee89 AS |
1865 | static inline bool bpf_bypass_spec_v1(void) |
1866 | { | |
1867 | return perfmon_capable(); | |
1868 | } | |
1869 | ||
1870 | static inline bool bpf_bypass_spec_v4(void) | |
1871 | { | |
1872 | return perfmon_capable(); | |
1873 | } | |
1874 | ||
6e71b04a | 1875 | int bpf_map_new_fd(struct bpf_map *map, int flags); |
b2197755 DB |
1876 | int bpf_prog_new_fd(struct bpf_prog *prog); |
1877 | ||
f2e10bff | 1878 | void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, |
a3b80e10 AN |
1879 | const struct bpf_link_ops *ops, struct bpf_prog *prog); |
1880 | int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer); | |
1881 | int bpf_link_settle(struct bpf_link_primer *primer); | |
1882 | void bpf_link_cleanup(struct bpf_link_primer *primer); | |
70ed506c AN |
1883 | void bpf_link_inc(struct bpf_link *link); |
1884 | void bpf_link_put(struct bpf_link *link); | |
1885 | int bpf_link_new_fd(struct bpf_link *link); | |
babf3164 | 1886 | struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd); |
70ed506c | 1887 | struct bpf_link *bpf_link_get_from_fd(u32 ufd); |
9f883612 | 1888 | struct bpf_link *bpf_link_get_curr_or_next(u32 *id); |
70ed506c | 1889 | |
b2197755 | 1890 | int bpf_obj_pin_user(u32 ufd, const char __user *pathname); |
6e71b04a | 1891 | int bpf_obj_get_user(const char __user *pathname, int flags); |
b2197755 | 1892 | |
21aef70e | 1893 | #define BPF_ITER_FUNC_PREFIX "bpf_iter_" |
e5158d98 | 1894 | #define DEFINE_BPF_ITER_FUNC(target, args...) \ |
21aef70e YS |
1895 | extern int bpf_iter_ ## target(args); \ |
1896 | int __init bpf_iter_ ## target(args) { return 0; } | |
15d83c4d | 1897 | |
f0d74c4d KFL |
1898 | /* |
1899 | * The task type of iterators. | |
1900 | * | |
1901 | * For BPF task iterators, they can be parameterized with various | |
1902 | * parameters to visit only some of tasks. | |
1903 | * | |
1904 | * BPF_TASK_ITER_ALL (default) | |
1905 | * Iterate over resources of every task. | |
1906 | * | |
1907 | * BPF_TASK_ITER_TID | |
1908 | * Iterate over resources of a task/tid. | |
1909 | * | |
1910 | * BPF_TASK_ITER_TGID | |
1911 | * Iterate over resources of every task of a process / task group. | |
1912 | */ | |
1913 | enum bpf_iter_task_type { | |
1914 | BPF_TASK_ITER_ALL = 0, | |
1915 | BPF_TASK_ITER_TID, | |
1916 | BPF_TASK_ITER_TGID, | |
1917 | }; | |
1918 | ||
f9c79272 | 1919 | struct bpf_iter_aux_info { |
d4ccaf58 | 1920 | /* for map_elem iter */ |
a5cbe05a | 1921 | struct bpf_map *map; |
d4ccaf58 HL |
1922 | |
1923 | /* for cgroup iter */ | |
1924 | struct { | |
1925 | struct cgroup *start; /* starting cgroup */ | |
1926 | enum bpf_cgroup_iter_order order; | |
1927 | } cgroup; | |
f0d74c4d KFL |
1928 | struct { |
1929 | enum bpf_iter_task_type type; | |
1930 | u32 pid; | |
1931 | } task; | |
f9c79272 YS |
1932 | }; |
1933 | ||
5e7b3020 YS |
1934 | typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog, |
1935 | union bpf_iter_link_info *linfo, | |
1936 | struct bpf_iter_aux_info *aux); | |
1937 | typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux); | |
6b0a249a YS |
1938 | typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux, |
1939 | struct seq_file *seq); | |
1940 | typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux, | |
1941 | struct bpf_link_info *info); | |
3cee6fb8 MKL |
1942 | typedef const struct bpf_func_proto * |
1943 | (*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id, | |
1944 | const struct bpf_prog *prog); | |
a5cbe05a | 1945 | |
cf83b2d2 YS |
1946 | enum bpf_iter_feature { |
1947 | BPF_ITER_RESCHED = BIT(0), | |
1948 | }; | |
1949 | ||
3c32cc1b | 1950 | #define BPF_ITER_CTX_ARG_MAX 2 |
ae24345d YS |
1951 | struct bpf_iter_reg { |
1952 | const char *target; | |
5e7b3020 YS |
1953 | bpf_iter_attach_target_t attach_target; |
1954 | bpf_iter_detach_target_t detach_target; | |
6b0a249a YS |
1955 | bpf_iter_show_fdinfo_t show_fdinfo; |
1956 | bpf_iter_fill_link_info_t fill_link_info; | |
3cee6fb8 | 1957 | bpf_iter_get_func_proto_t get_func_proto; |
3c32cc1b | 1958 | u32 ctx_arg_info_size; |
cf83b2d2 | 1959 | u32 feature; |
3c32cc1b | 1960 | struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX]; |
14fc6bd6 | 1961 | const struct bpf_iter_seq_info *seq_info; |
ae24345d YS |
1962 | }; |
1963 | ||
e5158d98 YS |
1964 | struct bpf_iter_meta { |
1965 | __bpf_md_ptr(struct seq_file *, seq); | |
1966 | u64 session_id; | |
1967 | u64 seq_num; | |
1968 | }; | |
1969 | ||
a5cbe05a YS |
1970 | struct bpf_iter__bpf_map_elem { |
1971 | __bpf_md_ptr(struct bpf_iter_meta *, meta); | |
1972 | __bpf_md_ptr(struct bpf_map *, map); | |
1973 | __bpf_md_ptr(void *, key); | |
1974 | __bpf_md_ptr(void *, value); | |
1975 | }; | |
1976 | ||
15172a46 | 1977 | int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info); |
ab2ee4fc | 1978 | void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info); |
15d83c4d | 1979 | bool bpf_iter_prog_supported(struct bpf_prog *prog); |
3cee6fb8 MKL |
1980 | const struct bpf_func_proto * |
1981 | bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog); | |
af2ac3e1 | 1982 | int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog); |
ac51d99b | 1983 | int bpf_iter_new_fd(struct bpf_link *link); |
367ec3e4 | 1984 | bool bpf_link_is_iter(struct bpf_link *link); |
e5158d98 YS |
1985 | struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop); |
1986 | int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx); | |
b76f2226 YS |
1987 | void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux, |
1988 | struct seq_file *seq); | |
1989 | int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux, | |
1990 | struct bpf_link_info *info); | |
ae24345d | 1991 | |
314ee05e YS |
1992 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
1993 | struct bpf_func_state *caller, | |
1994 | struct bpf_func_state *callee); | |
1995 | ||
15a07b33 AS |
1996 | int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); |
1997 | int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value); | |
1998 | int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, | |
1999 | u64 flags); | |
2000 | int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value, | |
2001 | u64 flags); | |
d056a788 | 2002 | |
557c0c6e | 2003 | int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value); |
15a07b33 | 2004 | |
d056a788 DB |
2005 | int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file, |
2006 | void *key, void *value, u64 map_flags); | |
14dc6f04 | 2007 | int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value); |
bcc6b1b7 MKL |
2008 | int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file, |
2009 | void *key, void *value, u64 map_flags); | |
14dc6f04 | 2010 | int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value); |
d056a788 | 2011 | |
6e71b04a | 2012 | int bpf_get_file_flag(int flags); |
af2ac3e1 | 2013 | int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size, |
dcab51f1 | 2014 | size_t actual_size); |
6e71b04a | 2015 | |
61e021f3 | 2016 | /* verify correctness of eBPF program */ |
af2ac3e1 | 2017 | int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr); |
a643bff7 AN |
2018 | |
2019 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 | 2020 | void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); |
a643bff7 | 2021 | #endif |
46f55cff | 2022 | |
76654e67 AM |
2023 | struct btf *bpf_get_btf_vmlinux(void); |
2024 | ||
46f55cff | 2025 | /* Map specifics */ |
d53ad5d8 | 2026 | struct xdp_frame; |
6d5fc195 | 2027 | struct sk_buff; |
e6a4750f BT |
2028 | struct bpf_dtab_netdev; |
2029 | struct bpf_cpu_map_entry; | |
67f29e07 | 2030 | |
1d233886 | 2031 | void __dev_flush(void); |
d53ad5d8 | 2032 | int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf, |
1d233886 | 2033 | struct net_device *dev_rx); |
d53ad5d8 | 2034 | int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf, |
38edddb8 | 2035 | struct net_device *dev_rx); |
d53ad5d8 | 2036 | int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx, |
e624d4ed | 2037 | struct bpf_map *map, bool exclude_ingress); |
6d5fc195 TM |
2038 | int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb, |
2039 | struct bpf_prog *xdp_prog); | |
e624d4ed HL |
2040 | int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb, |
2041 | struct bpf_prog *xdp_prog, struct bpf_map *map, | |
2042 | bool exclude_ingress); | |
46f55cff | 2043 | |
cdfafe98 | 2044 | void __cpu_map_flush(void); |
d53ad5d8 | 2045 | int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf, |
9c270af3 | 2046 | struct net_device *dev_rx); |
11941f8a KKD |
2047 | int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu, |
2048 | struct sk_buff *skb); | |
9c270af3 | 2049 | |
96eabe7a MKL |
2050 | /* Return map's numa specified by userspace */ |
2051 | static inline int bpf_map_attr_numa_node(const union bpf_attr *attr) | |
2052 | { | |
2053 | return (attr->map_flags & BPF_F_NUMA_NODE) ? | |
2054 | attr->numa_node : NUMA_NO_NODE; | |
2055 | } | |
2056 | ||
040ee692 | 2057 | struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type); |
5dc4c4b7 | 2058 | int array_map_alloc_check(union bpf_attr *attr); |
040ee692 | 2059 | |
c695865c SF |
2060 | int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, |
2061 | union bpf_attr __user *uattr); | |
2062 | int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, | |
2063 | union bpf_attr __user *uattr); | |
da00d2f1 KS |
2064 | int bpf_prog_test_run_tracing(struct bpf_prog *prog, |
2065 | const union bpf_attr *kattr, | |
2066 | union bpf_attr __user *uattr); | |
c695865c SF |
2067 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
2068 | const union bpf_attr *kattr, | |
2069 | union bpf_attr __user *uattr); | |
1b4d60ec SL |
2070 | int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, |
2071 | const union bpf_attr *kattr, | |
2072 | union bpf_attr __user *uattr); | |
7c32e8f8 LB |
2073 | int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, |
2074 | const union bpf_attr *kattr, | |
2075 | union bpf_attr __user *uattr); | |
9e15db66 AS |
2076 | bool btf_ctx_access(int off, int size, enum bpf_access_type type, |
2077 | const struct bpf_prog *prog, | |
2078 | struct bpf_insn_access_aux *info); | |
35346ab6 HT |
2079 | |
2080 | static inline bool bpf_tracing_ctx_access(int off, int size, | |
2081 | enum bpf_access_type type) | |
2082 | { | |
2083 | if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) | |
2084 | return false; | |
2085 | if (type != BPF_READ) | |
2086 | return false; | |
2087 | if (off % size != 0) | |
2088 | return false; | |
2089 | return true; | |
2090 | } | |
2091 | ||
2092 | static inline bool bpf_tracing_btf_ctx_access(int off, int size, | |
2093 | enum bpf_access_type type, | |
2094 | const struct bpf_prog *prog, | |
2095 | struct bpf_insn_access_aux *info) | |
2096 | { | |
2097 | if (!bpf_tracing_ctx_access(off, size, type)) | |
2098 | return false; | |
2099 | return btf_ctx_access(off, size, type, prog, info); | |
2100 | } | |
2101 | ||
6728aea7 KKD |
2102 | int btf_struct_access(struct bpf_verifier_log *log, |
2103 | const struct bpf_reg_state *reg, | |
2104 | int off, int size, enum bpf_access_type atype, | |
c6f1bfe8 | 2105 | u32 *next_btf_id, enum bpf_type_flag *flag); |
faaf4a79 | 2106 | bool btf_struct_ids_match(struct bpf_verifier_log *log, |
22dc4a0f | 2107 | const struct btf *btf, u32 id, int off, |
2ab3b380 KKD |
2108 | const struct btf *need_btf, u32 need_type_id, |
2109 | bool strict); | |
9e15db66 | 2110 | |
fec56f58 AS |
2111 | int btf_distill_func_proto(struct bpf_verifier_log *log, |
2112 | struct btf *btf, | |
2113 | const struct btf_type *func_proto, | |
2114 | const char *func_name, | |
2115 | struct btf_func_model *m); | |
2116 | ||
51c39bb1 | 2117 | struct bpf_reg_state; |
34747c41 MKL |
2118 | int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, |
2119 | struct bpf_reg_state *regs); | |
95f2f26f BT |
2120 | int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog, |
2121 | struct bpf_reg_state *regs); | |
51c39bb1 AS |
2122 | int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, |
2123 | struct bpf_reg_state *reg); | |
efc68158 | 2124 | int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog, |
be8704ff | 2125 | struct btf *btf, const struct btf_type *t); |
8c1b6e69 | 2126 | |
7e6897f9 | 2127 | struct bpf_prog *bpf_prog_by_id(u32 id); |
005142b8 | 2128 | struct bpf_link *bpf_link_by_id(u32 id); |
7e6897f9 | 2129 | |
6890896b | 2130 | const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id); |
a10787e6 | 2131 | void bpf_task_storage_free(struct task_struct *task); |
c4bcfb38 | 2132 | void bpf_cgrp_storage_free(struct cgroup *cgroup); |
e6ac2450 MKL |
2133 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog); |
2134 | const struct btf_func_model * | |
2135 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2136 | const struct bpf_insn *insn); | |
fbd94c7a AS |
2137 | struct bpf_core_ctx { |
2138 | struct bpf_verifier_log *log; | |
2139 | const struct btf *btf; | |
2140 | }; | |
2141 | ||
2142 | int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, | |
2143 | int relo_idx, void *insn); | |
2144 | ||
44a3918c JP |
2145 | static inline bool unprivileged_ebpf_enabled(void) |
2146 | { | |
2147 | return !sysctl_unprivileged_bpf_disabled; | |
2148 | } | |
2149 | ||
24426654 MKL |
2150 | /* Not all bpf prog type has the bpf_ctx. |
2151 | * For the bpf prog type that has initialized the bpf_ctx, | |
2152 | * this function can be used to decide if a kernel function | |
2153 | * is called by a bpf program. | |
2154 | */ | |
2155 | static inline bool has_current_bpf_ctx(void) | |
2156 | { | |
2157 | return !!current->bpf_ctx; | |
2158 | } | |
05b24ff9 JO |
2159 | |
2160 | void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog); | |
9c270af3 | 2161 | #else /* !CONFIG_BPF_SYSCALL */ |
0fc174de DB |
2162 | static inline struct bpf_prog *bpf_prog_get(u32 ufd) |
2163 | { | |
2164 | return ERR_PTR(-EOPNOTSUPP); | |
2165 | } | |
2166 | ||
248f346f JK |
2167 | static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, |
2168 | enum bpf_prog_type type, | |
288b3de5 | 2169 | bool attach_drv) |
248f346f JK |
2170 | { |
2171 | return ERR_PTR(-EOPNOTSUPP); | |
2172 | } | |
2173 | ||
85192dbf | 2174 | static inline void bpf_prog_add(struct bpf_prog *prog, int i) |
cc2e0b3f | 2175 | { |
cc2e0b3f | 2176 | } |
113214be | 2177 | |
c540594f DB |
2178 | static inline void bpf_prog_sub(struct bpf_prog *prog, int i) |
2179 | { | |
2180 | } | |
2181 | ||
0fc174de DB |
2182 | static inline void bpf_prog_put(struct bpf_prog *prog) |
2183 | { | |
2184 | } | |
6d67942d | 2185 | |
85192dbf | 2186 | static inline void bpf_prog_inc(struct bpf_prog *prog) |
aa6a5f3c | 2187 | { |
aa6a5f3c | 2188 | } |
5ccb071e | 2189 | |
a6f6df69 JF |
2190 | static inline struct bpf_prog *__must_check |
2191 | bpf_prog_inc_not_zero(struct bpf_prog *prog) | |
2192 | { | |
2193 | return ERR_PTR(-EOPNOTSUPP); | |
2194 | } | |
2195 | ||
6cc7d1e8 AN |
2196 | static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, |
2197 | const struct bpf_link_ops *ops, | |
2198 | struct bpf_prog *prog) | |
2199 | { | |
2200 | } | |
2201 | ||
2202 | static inline int bpf_link_prime(struct bpf_link *link, | |
2203 | struct bpf_link_primer *primer) | |
2204 | { | |
2205 | return -EOPNOTSUPP; | |
2206 | } | |
2207 | ||
2208 | static inline int bpf_link_settle(struct bpf_link_primer *primer) | |
2209 | { | |
2210 | return -EOPNOTSUPP; | |
2211 | } | |
2212 | ||
2213 | static inline void bpf_link_cleanup(struct bpf_link_primer *primer) | |
2214 | { | |
2215 | } | |
2216 | ||
2217 | static inline void bpf_link_inc(struct bpf_link *link) | |
2218 | { | |
2219 | } | |
2220 | ||
2221 | static inline void bpf_link_put(struct bpf_link *link) | |
2222 | { | |
2223 | } | |
2224 | ||
6e71b04a | 2225 | static inline int bpf_obj_get_user(const char __user *pathname, int flags) |
98589a09 SL |
2226 | { |
2227 | return -EOPNOTSUPP; | |
2228 | } | |
2229 | ||
1d233886 | 2230 | static inline void __dev_flush(void) |
46f55cff JF |
2231 | { |
2232 | } | |
9c270af3 | 2233 | |
d53ad5d8 | 2234 | struct xdp_frame; |
67f29e07 | 2235 | struct bpf_dtab_netdev; |
e6a4750f | 2236 | struct bpf_cpu_map_entry; |
67f29e07 | 2237 | |
1d233886 | 2238 | static inline |
d53ad5d8 | 2239 | int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf, |
1d233886 THJ |
2240 | struct net_device *dev_rx) |
2241 | { | |
2242 | return 0; | |
2243 | } | |
2244 | ||
67f29e07 | 2245 | static inline |
d53ad5d8 | 2246 | int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf, |
38edddb8 | 2247 | struct net_device *dev_rx) |
67f29e07 JDB |
2248 | { |
2249 | return 0; | |
2250 | } | |
2251 | ||
e624d4ed | 2252 | static inline |
d53ad5d8 | 2253 | int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx, |
e624d4ed HL |
2254 | struct bpf_map *map, bool exclude_ingress) |
2255 | { | |
2256 | return 0; | |
2257 | } | |
2258 | ||
6d5fc195 TM |
2259 | struct sk_buff; |
2260 | ||
2261 | static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, | |
2262 | struct sk_buff *skb, | |
2263 | struct bpf_prog *xdp_prog) | |
2264 | { | |
2265 | return 0; | |
2266 | } | |
2267 | ||
e624d4ed HL |
2268 | static inline |
2269 | int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb, | |
2270 | struct bpf_prog *xdp_prog, struct bpf_map *map, | |
2271 | bool exclude_ingress) | |
2272 | { | |
2273 | return 0; | |
2274 | } | |
2275 | ||
cdfafe98 | 2276 | static inline void __cpu_map_flush(void) |
9c270af3 JDB |
2277 | { |
2278 | } | |
2279 | ||
9c270af3 | 2280 | static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, |
d53ad5d8 | 2281 | struct xdp_frame *xdpf, |
9c270af3 JDB |
2282 | struct net_device *dev_rx) |
2283 | { | |
2284 | return 0; | |
2285 | } | |
040ee692 | 2286 | |
11941f8a KKD |
2287 | static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu, |
2288 | struct sk_buff *skb) | |
2289 | { | |
2290 | return -EOPNOTSUPP; | |
2291 | } | |
2292 | ||
040ee692 AV |
2293 | static inline struct bpf_prog *bpf_prog_get_type_path(const char *name, |
2294 | enum bpf_prog_type type) | |
2295 | { | |
2296 | return ERR_PTR(-EOPNOTSUPP); | |
2297 | } | |
c695865c SF |
2298 | |
2299 | static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog, | |
2300 | const union bpf_attr *kattr, | |
2301 | union bpf_attr __user *uattr) | |
2302 | { | |
2303 | return -ENOTSUPP; | |
2304 | } | |
2305 | ||
2306 | static inline int bpf_prog_test_run_skb(struct bpf_prog *prog, | |
2307 | const union bpf_attr *kattr, | |
2308 | union bpf_attr __user *uattr) | |
2309 | { | |
2310 | return -ENOTSUPP; | |
2311 | } | |
2312 | ||
da00d2f1 KS |
2313 | static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog, |
2314 | const union bpf_attr *kattr, | |
2315 | union bpf_attr __user *uattr) | |
2316 | { | |
2317 | return -ENOTSUPP; | |
2318 | } | |
2319 | ||
c695865c SF |
2320 | static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
2321 | const union bpf_attr *kattr, | |
2322 | union bpf_attr __user *uattr) | |
2323 | { | |
2324 | return -ENOTSUPP; | |
2325 | } | |
6332be04 | 2326 | |
7c32e8f8 LB |
2327 | static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, |
2328 | const union bpf_attr *kattr, | |
2329 | union bpf_attr __user *uattr) | |
2330 | { | |
2331 | return -ENOTSUPP; | |
2332 | } | |
2333 | ||
6332be04 DB |
2334 | static inline void bpf_map_put(struct bpf_map *map) |
2335 | { | |
2336 | } | |
7e6897f9 BT |
2337 | |
2338 | static inline struct bpf_prog *bpf_prog_by_id(u32 id) | |
2339 | { | |
2340 | return ERR_PTR(-ENOTSUPP); | |
2341 | } | |
6890896b | 2342 | |
d4f7bdb2 | 2343 | static inline int btf_struct_access(struct bpf_verifier_log *log, |
6728aea7 KKD |
2344 | const struct bpf_reg_state *reg, |
2345 | int off, int size, enum bpf_access_type atype, | |
d4f7bdb2 DX |
2346 | u32 *next_btf_id, enum bpf_type_flag *flag) |
2347 | { | |
2348 | return -EACCES; | |
2349 | } | |
2350 | ||
6890896b SF |
2351 | static inline const struct bpf_func_proto * |
2352 | bpf_base_func_proto(enum bpf_func_id func_id) | |
2353 | { | |
2354 | return NULL; | |
2355 | } | |
a10787e6 SL |
2356 | |
2357 | static inline void bpf_task_storage_free(struct task_struct *task) | |
2358 | { | |
2359 | } | |
e6ac2450 MKL |
2360 | |
2361 | static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
2362 | { | |
2363 | return false; | |
2364 | } | |
2365 | ||
2366 | static inline const struct btf_func_model * | |
2367 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2368 | const struct bpf_insn *insn) | |
2369 | { | |
2370 | return NULL; | |
2371 | } | |
44a3918c JP |
2372 | |
2373 | static inline bool unprivileged_ebpf_enabled(void) | |
2374 | { | |
2375 | return false; | |
2376 | } | |
2377 | ||
24426654 MKL |
2378 | static inline bool has_current_bpf_ctx(void) |
2379 | { | |
2380 | return false; | |
2381 | } | |
05b24ff9 JO |
2382 | |
2383 | static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog) | |
2384 | { | |
2385 | } | |
c4bcfb38 YS |
2386 | |
2387 | static inline void bpf_cgrp_storage_free(struct cgroup *cgroup) | |
2388 | { | |
2389 | } | |
61e021f3 | 2390 | #endif /* CONFIG_BPF_SYSCALL */ |
09756af4 | 2391 | |
541c3bad AN |
2392 | void __bpf_free_used_btfs(struct bpf_prog_aux *aux, |
2393 | struct btf_mod_pair *used_btfs, u32 len); | |
2394 | ||
479321e9 JK |
2395 | static inline struct bpf_prog *bpf_prog_get_type(u32 ufd, |
2396 | enum bpf_prog_type type) | |
2397 | { | |
2398 | return bpf_prog_get_type_dev(ufd, type, false); | |
2399 | } | |
2400 | ||
936f8946 AN |
2401 | void __bpf_free_used_maps(struct bpf_prog_aux *aux, |
2402 | struct bpf_map **used_maps, u32 len); | |
2403 | ||
040ee692 AV |
2404 | bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool); |
2405 | ||
ab3f0063 JK |
2406 | int bpf_prog_offload_compile(struct bpf_prog *prog); |
2407 | void bpf_prog_offload_destroy(struct bpf_prog *prog); | |
675fc275 JK |
2408 | int bpf_prog_offload_info_fill(struct bpf_prog_info *info, |
2409 | struct bpf_prog *prog); | |
ab3f0063 | 2410 | |
52775b33 JK |
2411 | int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map); |
2412 | ||
a3884572 JK |
2413 | int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value); |
2414 | int bpf_map_offload_update_elem(struct bpf_map *map, | |
2415 | void *key, void *value, u64 flags); | |
2416 | int bpf_map_offload_delete_elem(struct bpf_map *map, void *key); | |
2417 | int bpf_map_offload_get_next_key(struct bpf_map *map, | |
2418 | void *key, void *next_key); | |
2419 | ||
09728266 | 2420 | bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map); |
a3884572 | 2421 | |
1385d755 | 2422 | struct bpf_offload_dev * |
dd27c2e3 | 2423 | bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv); |
602144c2 | 2424 | void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev); |
dd27c2e3 | 2425 | void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev); |
602144c2 JK |
2426 | int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev, |
2427 | struct net_device *netdev); | |
2428 | void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, | |
2429 | struct net_device *netdev); | |
fd4f227d | 2430 | bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev); |
9fd7c555 | 2431 | |
2147c438 JP |
2432 | void unpriv_ebpf_notify(int new_state); |
2433 | ||
ab3f0063 JK |
2434 | #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL) |
2435 | int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr); | |
2436 | ||
0d830032 | 2437 | static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux) |
ab3f0063 | 2438 | { |
9a18eedb | 2439 | return aux->offload_requested; |
ab3f0063 | 2440 | } |
a3884572 JK |
2441 | |
2442 | static inline bool bpf_map_is_dev_bound(struct bpf_map *map) | |
2443 | { | |
2444 | return unlikely(map->ops == &bpf_map_offload_ops); | |
2445 | } | |
2446 | ||
2447 | struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr); | |
2448 | void bpf_map_offload_map_free(struct bpf_map *map); | |
79a7f8bd AS |
2449 | int bpf_prog_test_run_syscall(struct bpf_prog *prog, |
2450 | const union bpf_attr *kattr, | |
2451 | union bpf_attr __user *uattr); | |
17edea21 CW |
2452 | |
2453 | int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog); | |
2454 | int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype); | |
2455 | int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags); | |
748cd572 DZ |
2456 | int sock_map_bpf_prog_query(const union bpf_attr *attr, |
2457 | union bpf_attr __user *uattr); | |
2458 | ||
17edea21 | 2459 | void sock_map_unhash(struct sock *sk); |
d8616ee2 | 2460 | void sock_map_destroy(struct sock *sk); |
17edea21 | 2461 | void sock_map_close(struct sock *sk, long timeout); |
ab3f0063 JK |
2462 | #else |
2463 | static inline int bpf_prog_offload_init(struct bpf_prog *prog, | |
2464 | union bpf_attr *attr) | |
2465 | { | |
2466 | return -EOPNOTSUPP; | |
2467 | } | |
2468 | ||
2469 | static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux) | |
2470 | { | |
2471 | return false; | |
2472 | } | |
a3884572 JK |
2473 | |
2474 | static inline bool bpf_map_is_dev_bound(struct bpf_map *map) | |
2475 | { | |
2476 | return false; | |
2477 | } | |
2478 | ||
2479 | static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr) | |
2480 | { | |
2481 | return ERR_PTR(-EOPNOTSUPP); | |
2482 | } | |
2483 | ||
2484 | static inline void bpf_map_offload_map_free(struct bpf_map *map) | |
2485 | { | |
2486 | } | |
79a7f8bd AS |
2487 | |
2488 | static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog, | |
2489 | const union bpf_attr *kattr, | |
2490 | union bpf_attr __user *uattr) | |
2491 | { | |
2492 | return -ENOTSUPP; | |
2493 | } | |
fdb5c453 | 2494 | |
88759609 | 2495 | #ifdef CONFIG_BPF_SYSCALL |
604326b4 DB |
2496 | static inline int sock_map_get_from_fd(const union bpf_attr *attr, |
2497 | struct bpf_prog *prog) | |
fdb5c453 SY |
2498 | { |
2499 | return -EINVAL; | |
2500 | } | |
bb0de313 LB |
2501 | |
2502 | static inline int sock_map_prog_detach(const union bpf_attr *attr, | |
2503 | enum bpf_prog_type ptype) | |
2504 | { | |
2505 | return -EOPNOTSUPP; | |
2506 | } | |
13b79d3f LB |
2507 | |
2508 | static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, | |
2509 | u64 flags) | |
2510 | { | |
2511 | return -EOPNOTSUPP; | |
2512 | } | |
748cd572 DZ |
2513 | |
2514 | static inline int sock_map_bpf_prog_query(const union bpf_attr *attr, | |
2515 | union bpf_attr __user *uattr) | |
2516 | { | |
2517 | return -EINVAL; | |
2518 | } | |
17edea21 CW |
2519 | #endif /* CONFIG_BPF_SYSCALL */ |
2520 | #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */ | |
5dc4c4b7 | 2521 | |
17edea21 CW |
2522 | #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) |
2523 | void bpf_sk_reuseport_detach(struct sock *sk); | |
2524 | int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key, | |
2525 | void *value); | |
2526 | int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, | |
2527 | void *value, u64 map_flags); | |
2528 | #else | |
2529 | static inline void bpf_sk_reuseport_detach(struct sock *sk) | |
2530 | { | |
2531 | } | |
5dc4c4b7 | 2532 | |
17edea21 | 2533 | #ifdef CONFIG_BPF_SYSCALL |
5dc4c4b7 MKL |
2534 | static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, |
2535 | void *key, void *value) | |
2536 | { | |
2537 | return -EOPNOTSUPP; | |
2538 | } | |
2539 | ||
2540 | static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, | |
2541 | void *key, void *value, | |
2542 | u64 map_flags) | |
2543 | { | |
2544 | return -EOPNOTSUPP; | |
2545 | } | |
2546 | #endif /* CONFIG_BPF_SYSCALL */ | |
2547 | #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */ | |
2548 | ||
d0003ec0 | 2549 | /* verifier prototypes for helper functions called from eBPF programs */ |
a2c83fff DB |
2550 | extern const struct bpf_func_proto bpf_map_lookup_elem_proto; |
2551 | extern const struct bpf_func_proto bpf_map_update_elem_proto; | |
2552 | extern const struct bpf_func_proto bpf_map_delete_elem_proto; | |
f1a2e44a MV |
2553 | extern const struct bpf_func_proto bpf_map_push_elem_proto; |
2554 | extern const struct bpf_func_proto bpf_map_pop_elem_proto; | |
2555 | extern const struct bpf_func_proto bpf_map_peek_elem_proto; | |
07343110 | 2556 | extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto; |
d0003ec0 | 2557 | |
03e69b50 | 2558 | extern const struct bpf_func_proto bpf_get_prandom_u32_proto; |
c04167ce | 2559 | extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; |
2d0e30c3 | 2560 | extern const struct bpf_func_proto bpf_get_numa_node_id_proto; |
04fd61ab | 2561 | extern const struct bpf_func_proto bpf_tail_call_proto; |
17ca8cbf | 2562 | extern const struct bpf_func_proto bpf_ktime_get_ns_proto; |
71d19214 | 2563 | extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto; |
c8996c98 | 2564 | extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto; |
ffeedafb AS |
2565 | extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto; |
2566 | extern const struct bpf_func_proto bpf_get_current_uid_gid_proto; | |
2567 | extern const struct bpf_func_proto bpf_get_current_comm_proto; | |
d5a3b1f6 | 2568 | extern const struct bpf_func_proto bpf_get_stackid_proto; |
c195651e | 2569 | extern const struct bpf_func_proto bpf_get_stack_proto; |
fa28dcb8 | 2570 | extern const struct bpf_func_proto bpf_get_task_stack_proto; |
7b04d6d6 SL |
2571 | extern const struct bpf_func_proto bpf_get_stackid_proto_pe; |
2572 | extern const struct bpf_func_proto bpf_get_stack_proto_pe; | |
174a79ff | 2573 | extern const struct bpf_func_proto bpf_sock_map_update_proto; |
81110384 | 2574 | extern const struct bpf_func_proto bpf_sock_hash_update_proto; |
bf6fa2c8 | 2575 | extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto; |
0f09abd1 | 2576 | extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto; |
bed89185 | 2577 | extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto; |
604326b4 DB |
2578 | extern const struct bpf_func_proto bpf_msg_redirect_hash_proto; |
2579 | extern const struct bpf_func_proto bpf_msg_redirect_map_proto; | |
2580 | extern const struct bpf_func_proto bpf_sk_redirect_hash_proto; | |
2581 | extern const struct bpf_func_proto bpf_sk_redirect_map_proto; | |
d83525ca AS |
2582 | extern const struct bpf_func_proto bpf_spin_lock_proto; |
2583 | extern const struct bpf_func_proto bpf_spin_unlock_proto; | |
cd339431 | 2584 | extern const struct bpf_func_proto bpf_get_local_storage_proto; |
d7a4cb9b AI |
2585 | extern const struct bpf_func_proto bpf_strtol_proto; |
2586 | extern const struct bpf_func_proto bpf_strtoul_proto; | |
0d01da6a | 2587 | extern const struct bpf_func_proto bpf_tcp_sock_proto; |
5576b991 | 2588 | extern const struct bpf_func_proto bpf_jiffies64_proto; |
b4490c5c | 2589 | extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto; |
0456ea17 | 2590 | extern const struct bpf_func_proto bpf_event_output_data_proto; |
457f4436 AN |
2591 | extern const struct bpf_func_proto bpf_ringbuf_output_proto; |
2592 | extern const struct bpf_func_proto bpf_ringbuf_reserve_proto; | |
2593 | extern const struct bpf_func_proto bpf_ringbuf_submit_proto; | |
2594 | extern const struct bpf_func_proto bpf_ringbuf_discard_proto; | |
2595 | extern const struct bpf_func_proto bpf_ringbuf_query_proto; | |
bc34dee6 JK |
2596 | extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto; |
2597 | extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto; | |
2598 | extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto; | |
af7ec138 | 2599 | extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto; |
478cfbdf YS |
2600 | extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto; |
2601 | extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto; | |
2602 | extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto; | |
0d4fad3e | 2603 | extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto; |
9eeb3aa3 | 2604 | extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto; |
3bc253c2 | 2605 | extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto; |
07be4c4a | 2606 | extern const struct bpf_func_proto bpf_copy_from_user_proto; |
c4d0bfb4 | 2607 | extern const struct bpf_func_proto bpf_snprintf_btf_proto; |
7b15523a | 2608 | extern const struct bpf_func_proto bpf_snprintf_proto; |
eaa6bcb7 | 2609 | extern const struct bpf_func_proto bpf_per_cpu_ptr_proto; |
63d9b80d | 2610 | extern const struct bpf_func_proto bpf_this_cpu_ptr_proto; |
d0551261 | 2611 | extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto; |
b60da495 | 2612 | extern const struct bpf_func_proto bpf_sock_from_file_proto; |
c5dbb89f | 2613 | extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto; |
0593dd34 | 2614 | extern const struct bpf_func_proto bpf_task_storage_get_recur_proto; |
a10787e6 | 2615 | extern const struct bpf_func_proto bpf_task_storage_get_proto; |
0593dd34 | 2616 | extern const struct bpf_func_proto bpf_task_storage_delete_recur_proto; |
a10787e6 | 2617 | extern const struct bpf_func_proto bpf_task_storage_delete_proto; |
69c087ba | 2618 | extern const struct bpf_func_proto bpf_for_each_map_elem_proto; |
3d78417b | 2619 | extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto; |
3cee6fb8 MKL |
2620 | extern const struct bpf_func_proto bpf_sk_setsockopt_proto; |
2621 | extern const struct bpf_func_proto bpf_sk_getsockopt_proto; | |
9113d7e4 SF |
2622 | extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto; |
2623 | extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto; | |
7c7e3d31 | 2624 | extern const struct bpf_func_proto bpf_find_vma_proto; |
e6f2dd0f | 2625 | extern const struct bpf_func_proto bpf_loop_proto; |
376040e4 | 2626 | extern const struct bpf_func_proto bpf_copy_from_user_task_proto; |
69fd337a SF |
2627 | extern const struct bpf_func_proto bpf_set_retval_proto; |
2628 | extern const struct bpf_func_proto bpf_get_retval_proto; | |
20571567 | 2629 | extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto; |
c4bcfb38 YS |
2630 | extern const struct bpf_func_proto bpf_cgrp_storage_get_proto; |
2631 | extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto; | |
cd339431 | 2632 | |
958a3f2d JO |
2633 | const struct bpf_func_proto *tracing_prog_func_proto( |
2634 | enum bpf_func_id func_id, const struct bpf_prog *prog); | |
2635 | ||
3ad00405 DB |
2636 | /* Shared helpers among cBPF and eBPF. */ |
2637 | void bpf_user_rnd_init_once(void); | |
2638 | u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); | |
6890896b | 2639 | u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
3ad00405 | 2640 | |
c64b7983 | 2641 | #if defined(CONFIG_NET) |
46f8bc92 MKL |
2642 | bool bpf_sock_common_is_valid_access(int off, int size, |
2643 | enum bpf_access_type type, | |
2644 | struct bpf_insn_access_aux *info); | |
c64b7983 JS |
2645 | bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
2646 | struct bpf_insn_access_aux *info); | |
2647 | u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, | |
2648 | const struct bpf_insn *si, | |
2649 | struct bpf_insn *insn_buf, | |
2650 | struct bpf_prog *prog, | |
2651 | u32 *target_size); | |
2652 | #else | |
46f8bc92 MKL |
2653 | static inline bool bpf_sock_common_is_valid_access(int off, int size, |
2654 | enum bpf_access_type type, | |
2655 | struct bpf_insn_access_aux *info) | |
2656 | { | |
2657 | return false; | |
2658 | } | |
c64b7983 JS |
2659 | static inline bool bpf_sock_is_valid_access(int off, int size, |
2660 | enum bpf_access_type type, | |
2661 | struct bpf_insn_access_aux *info) | |
2662 | { | |
2663 | return false; | |
2664 | } | |
2665 | static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, | |
2666 | const struct bpf_insn *si, | |
2667 | struct bpf_insn *insn_buf, | |
2668 | struct bpf_prog *prog, | |
2669 | u32 *target_size) | |
2670 | { | |
2671 | return 0; | |
2672 | } | |
2673 | #endif | |
2674 | ||
655a51e5 | 2675 | #ifdef CONFIG_INET |
91cc1a99 AS |
2676 | struct sk_reuseport_kern { |
2677 | struct sk_buff *skb; | |
2678 | struct sock *sk; | |
2679 | struct sock *selected_sk; | |
d5e4ddae | 2680 | struct sock *migrating_sk; |
91cc1a99 AS |
2681 | void *data_end; |
2682 | u32 hash; | |
2683 | u32 reuseport_id; | |
2684 | bool bind_inany; | |
2685 | }; | |
655a51e5 MKL |
2686 | bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
2687 | struct bpf_insn_access_aux *info); | |
2688 | ||
2689 | u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, | |
2690 | const struct bpf_insn *si, | |
2691 | struct bpf_insn *insn_buf, | |
2692 | struct bpf_prog *prog, | |
2693 | u32 *target_size); | |
7f94208c Y |
2694 | |
2695 | bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type, | |
2696 | struct bpf_insn_access_aux *info); | |
2697 | ||
2698 | u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, | |
2699 | const struct bpf_insn *si, | |
2700 | struct bpf_insn *insn_buf, | |
2701 | struct bpf_prog *prog, | |
2702 | u32 *target_size); | |
655a51e5 MKL |
2703 | #else |
2704 | static inline bool bpf_tcp_sock_is_valid_access(int off, int size, | |
2705 | enum bpf_access_type type, | |
2706 | struct bpf_insn_access_aux *info) | |
2707 | { | |
2708 | return false; | |
2709 | } | |
2710 | ||
2711 | static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, | |
2712 | const struct bpf_insn *si, | |
2713 | struct bpf_insn *insn_buf, | |
2714 | struct bpf_prog *prog, | |
2715 | u32 *target_size) | |
2716 | { | |
2717 | return 0; | |
2718 | } | |
7f94208c Y |
2719 | static inline bool bpf_xdp_sock_is_valid_access(int off, int size, |
2720 | enum bpf_access_type type, | |
2721 | struct bpf_insn_access_aux *info) | |
2722 | { | |
2723 | return false; | |
2724 | } | |
2725 | ||
2726 | static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, | |
2727 | const struct bpf_insn *si, | |
2728 | struct bpf_insn *insn_buf, | |
2729 | struct bpf_prog *prog, | |
2730 | u32 *target_size) | |
2731 | { | |
2732 | return 0; | |
2733 | } | |
655a51e5 MKL |
2734 | #endif /* CONFIG_INET */ |
2735 | ||
5964b200 | 2736 | enum bpf_text_poke_type { |
b553a6ec DB |
2737 | BPF_MOD_CALL, |
2738 | BPF_MOD_JUMP, | |
5964b200 | 2739 | }; |
4b3da77b | 2740 | |
5964b200 AS |
2741 | int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t, |
2742 | void *addr1, void *addr2); | |
2743 | ||
ebc1415d | 2744 | void *bpf_arch_text_copy(void *dst, void *src, size_t len); |
fe736565 | 2745 | int bpf_arch_text_invalidate(void *dst, size_t len); |
ebc1415d | 2746 | |
eae2e83e | 2747 | struct btf_id_set; |
2af30f11 | 2748 | bool btf_id_set_contains(const struct btf_id_set *set, u32 id); |
eae2e83e | 2749 | |
335ff499 DM |
2750 | #define MAX_BPRINTF_VARARGS 12 |
2751 | ||
48cac3f4 FR |
2752 | int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args, |
2753 | u32 **bin_buf, u32 num_args); | |
2754 | void bpf_bprintf_cleanup(void); | |
d9c9e4db | 2755 | |
97e03f52 JK |
2756 | /* the implementation of the opaque uapi struct bpf_dynptr */ |
2757 | struct bpf_dynptr_kern { | |
2758 | void *data; | |
2759 | /* Size represents the number of usable bytes of dynptr data. | |
2760 | * If for example the offset is at 4 for a local dynptr whose data is | |
2761 | * of type u64, the number of usable bytes is 4. | |
2762 | * | |
2763 | * The upper 8 bits are reserved. It is as follows: | |
2764 | * Bits 0 - 23 = size | |
2765 | * Bits 24 - 30 = dynptr type | |
2766 | * Bit 31 = whether dynptr is read-only | |
2767 | */ | |
2768 | u32 size; | |
2769 | u32 offset; | |
2770 | } __aligned(8); | |
2771 | ||
2772 | enum bpf_dynptr_type { | |
2773 | BPF_DYNPTR_TYPE_INVALID, | |
2774 | /* Points to memory that is local to the bpf program */ | |
2775 | BPF_DYNPTR_TYPE_LOCAL, | |
20571567 | 2776 | /* Underlying data is a kernel-produced ringbuf record */ |
bc34dee6 | 2777 | BPF_DYNPTR_TYPE_RINGBUF, |
97e03f52 JK |
2778 | }; |
2779 | ||
bc34dee6 JK |
2780 | void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data, |
2781 | enum bpf_dynptr_type type, u32 offset, u32 size); | |
2782 | void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr); | |
2783 | int bpf_dynptr_check_size(u32 size); | |
51df4865 | 2784 | u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr); |
bc34dee6 | 2785 | |
c0e19f2c SF |
2786 | #ifdef CONFIG_BPF_LSM |
2787 | void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype); | |
2788 | void bpf_cgroup_atype_put(int cgroup_atype); | |
2789 | #else | |
2790 | static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {} | |
2791 | static inline void bpf_cgroup_atype_put(int cgroup_atype) {} | |
2792 | #endif /* CONFIG_BPF_LSM */ | |
2793 | ||
f3cf4134 RS |
2794 | struct key; |
2795 | ||
2796 | #ifdef CONFIG_KEYS | |
2797 | struct bpf_key { | |
2798 | struct key *key; | |
2799 | bool has_ref; | |
2800 | }; | |
2801 | #endif /* CONFIG_KEYS */ | |
282de143 KKD |
2802 | |
2803 | static inline bool type_is_alloc(u32 type) | |
2804 | { | |
2805 | return type & MEM_ALLOC; | |
2806 | } | |
2807 | ||
99c55f7d | 2808 | #endif /* _LINUX_BPF_H */ |