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