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