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