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