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