bpf: Implement an interface to register bpf_iter targets
[linux-block.git] / include / linux / bpf.h
CommitLineData
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 23struct bpf_verifier_env;
9e15db66 24struct bpf_verifier_log;
3b1efb19 25struct perf_event;
174a79ff 26struct bpf_prog;
da765a2f 27struct bpf_prog_aux;
99c55f7d 28struct bpf_map;
4f738adb 29struct sock;
a26ca7c9 30struct seq_file;
1b2b234b 31struct btf;
e8d2bec0 32struct btf_type;
3dec541b 33struct exception_table_entry;
ae24345d 34struct seq_operations;
99c55f7d 35
1b9ed84e
QM
36extern struct idr btf_idr;
37extern spinlock_t btf_idr_lock;
38
99c55f7d
AS
39/* map is generic key/value storage optionally accesible by eBPF programs */
40struct 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
94struct bpf_map_memory {
95 u32 pages;
96 struct user_struct *user;
97};
98
99c55f7d 99struct 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
136static inline bool map_value_has_spin_lock(const struct bpf_map *map)
137{
138 return map->spin_lock_off >= 0;
139}
140
141static 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 */
150static 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
163void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
164 bool lock_src);
8e7ae251 165int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
d83525ca 166
602144c2 167struct bpf_offload_dev;
a3884572
JK
168struct bpf_offloaded_map;
169
170struct 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
180struct 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
188static 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
193static 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
198static 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 204int 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
209extern const struct bpf_map_ops bpf_map_offload_ops;
210
17a52670
AS
211/* function argument constraints */
212enum 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 */
249enum 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 */
263struct 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 */
285struct bpf_context;
286
287enum 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
302enum 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 */
328struct 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
337static inline void
338bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
339{
340 aux->ctx_field_size = size;
341}
342
7de16e3a
JK
343struct 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
348struct 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 374struct 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 389struct 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
401enum 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
414struct 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
420struct 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
445struct 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
470int 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 */
475u64 notrace __bpf_prog_enter(void);
476void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
477
535911c8
JO
478struct 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
487enum 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
495struct 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
524struct bpf_dispatcher_prog {
525 struct bpf_prog *prog;
526 refcount_t users;
527};
528
529struct 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 540static __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
549struct bpf_trampoline *bpf_trampoline_lookup(u64 key);
550int bpf_trampoline_link_prog(struct bpf_prog *prog);
551int bpf_trampoline_unlink_prog(struct bpf_prog *prog);
552void 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
587void 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 590void *bpf_jit_alloc_exec_page(void);
a108f7dc
JO
591void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
592void bpf_image_ksym_del(struct bpf_ksym *ksym);
dba122fb
JO
593void bpf_ksym_add(struct bpf_ksym *ksym);
594void bpf_ksym_del(struct bpf_ksym *ksym);
fec56f58
AS
595#else
596static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
597{
598 return NULL;
599}
600static inline int bpf_trampoline_link_prog(struct bpf_prog *prog)
601{
602 return -ENOTSUPP;
603}
604static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
605{
606 return -ENOTSUPP;
607}
608static 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
613static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
614 struct bpf_prog *from,
615 struct bpf_prog *to) {}
e9b4e606
JO
616static inline bool is_bpf_image_address(unsigned long address)
617{
618 return false;
619}
fec56f58
AS
620#endif
621
8c1b6e69 622struct bpf_func_info_aux {
51c39bb1 623 u16 linkage;
8c1b6e69
AS
624 bool unreliable;
625};
626
a66886fe
DB
627enum bpf_jit_poke_reason {
628 BPF_POKE_REASON_TAIL_CALL,
629};
630
631/* Descriptor of pokes pointing /into/ the JITed image. */
632struct 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 645struct 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
718struct 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 733struct bpf_struct_ops_value;
27ae7997
MKL
734struct btf_type;
735struct btf_member;
736
737#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
738struct 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 758const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
d3e42bb0 759void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
85d33df3
MKL
760bool bpf_struct_ops_get(const void *kdata);
761void bpf_struct_ops_put(const void *kdata);
762int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
763 void *value);
764static 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}
771static 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
779static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
780{
781 return NULL;
782}
d3e42bb0
MKL
783static inline void bpf_struct_ops_init(struct btf *btf,
784 struct bpf_verifier_log *log)
785{
786}
85d33df3
MKL
787static inline bool bpf_try_module_get(const void *data, struct module *owner)
788{
789 return try_module_get(owner);
790}
791static inline void bpf_module_put(const void *data, struct module *owner)
792{
793 module_put(owner);
794}
795static 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
803struct 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
826static 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
841static 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
847struct 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 854bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
f1f7714e 855int bpf_prog_calc_tag(struct bpf_prog *fp);
9e15db66 856const char *kernel_type_name(u32 btf_type_id);
bd570ff9 857
0756ea3e 858const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
555c8a86
DB
859
860typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
aa7145c1 861 unsigned long off, unsigned long len);
c64b7983
JS
862typedef 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
868u64 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
883struct 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
888struct bpf_prog_array {
889 struct rcu_head rcu;
d7f10df8 890 struct bpf_prog_array_item items[];
324bda9e
AS
891};
892
d29ab6e1 893struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
54e9c9d4
SF
894void bpf_prog_array_free(struct bpf_prog_array *progs);
895int bpf_prog_array_length(struct bpf_prog_array *progs);
0d01da6a 896bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
54e9c9d4 897int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
468e2f64 898 __u32 __user *prog_ids, u32 cnt);
324bda9e 899
54e9c9d4 900void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
e87c6bc3 901 struct bpf_prog *old_prog);
54e9c9d4 902int bpf_prog_array_copy_info(struct bpf_prog_array *array,
3a38bb98
YS
903 u32 *prog_ids, u32 request_cnt,
904 u32 *prog_cnt);
54e9c9d4 905int 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 990DECLARE_PER_CPU(int, bpf_prog_active);
d46edd67 991extern 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 */
1005static 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
1014static 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
1023extern const struct file_operations bpf_map_fops;
1024extern 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 1037extern const struct bpf_prog_ops bpf_offload_prog_ops;
4f9218aa
JK
1038extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1039extern const struct bpf_verifier_ops xdp_analyzer_ops;
1040
0fc174de 1041struct bpf_prog *bpf_prog_get(u32 ufd);
248f346f 1042struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
288b3de5 1043 bool attach_drv);
85192dbf 1044void bpf_prog_add(struct bpf_prog *prog, int i);
c540594f 1045void bpf_prog_sub(struct bpf_prog *prog, int i);
85192dbf 1046void bpf_prog_inc(struct bpf_prog *prog);
a6f6df69 1047struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
61e021f3 1048void bpf_prog_put(struct bpf_prog *prog);
5ccb071e
DB
1049int __bpf_prog_charge(struct user_struct *user, u32 pages);
1050void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
a2ea0746
DB
1051void __bpf_free_used_maps(struct bpf_prog_aux *aux,
1052 struct bpf_map **used_maps, u32 len);
61e021f3 1053
ad8ad79f 1054void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
a3884572 1055void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
ad8ad79f 1056
1ed4d924 1057struct bpf_map *bpf_map_get(u32 ufd);
c9da161c 1058struct bpf_map *bpf_map_get_with_uref(u32 ufd);
c2101297 1059struct bpf_map *__bpf_map_get(struct fd f);
1e0bd5a0
AN
1060void bpf_map_inc(struct bpf_map *map);
1061void bpf_map_inc_with_uref(struct bpf_map *map);
1062struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
c9da161c 1063void bpf_map_put_with_uref(struct bpf_map *map);
61e021f3 1064void bpf_map_put(struct bpf_map *map);
0a4c58f5
RG
1065int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
1066void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
196e8ca7 1067int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
b936ca64
RG
1068void bpf_map_charge_finish(struct bpf_map_memory *mem);
1069void bpf_map_charge_move(struct bpf_map_memory *dst,
1070 struct bpf_map_memory *src);
196e8ca7
DB
1071void *bpf_map_area_alloc(u64 size, int numa_node);
1072void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
d407bd25 1073void bpf_map_area_free(void *base);
bd475643 1074void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
cb4d03ab
BV
1075int generic_map_lookup_batch(struct bpf_map *map,
1076 const union bpf_attr *attr,
aa2e93b8
BV
1077 union bpf_attr __user *uattr);
1078int generic_map_update_batch(struct bpf_map *map,
1079 const union bpf_attr *attr,
1080 union bpf_attr __user *uattr);
1081int 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
1085extern int sysctl_unprivileged_bpf_disabled;
1086
6e71b04a 1087int bpf_map_new_fd(struct bpf_map *map, int flags);
b2197755
DB
1088int bpf_prog_new_fd(struct bpf_prog *prog);
1089
af6eea57
AN
1090struct 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
1099struct bpf_link_primer {
1100 struct bpf_link *link;
1101 struct file *file;
1102 int fd;
1103 u32 id;
1104};
1105
70ed506c
AN
1106struct 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 1116void 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);
1118int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1119int bpf_link_settle(struct bpf_link_primer *primer);
1120void bpf_link_cleanup(struct bpf_link_primer *primer);
70ed506c
AN
1121void bpf_link_inc(struct bpf_link *link);
1122void bpf_link_put(struct bpf_link *link);
1123int bpf_link_new_fd(struct bpf_link *link);
babf3164 1124struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
70ed506c
AN
1125struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1126
b2197755 1127int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
6e71b04a 1128int bpf_obj_get_user(const char __user *pathname, int flags);
b2197755 1129
ae24345d
YS
1130typedef int (*bpf_iter_init_seq_priv_t)(void *private_data);
1131typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
1132
1133struct bpf_iter_reg {
1134 const char *target;
1135 const struct seq_operations *seq_ops;
1136 bpf_iter_init_seq_priv_t init_seq_private;
1137 bpf_iter_fini_seq_priv_t fini_seq_private;
1138 u32 seq_priv_size;
1139};
1140
1141int bpf_iter_reg_target(struct bpf_iter_reg *reg_info);
1142void bpf_iter_unreg_target(const char *target);
1143
15a07b33
AS
1144int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1145int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1146int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1147 u64 flags);
1148int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1149 u64 flags);
d056a788 1150
557c0c6e 1151int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
15a07b33 1152
d056a788
DB
1153int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1154 void *key, void *value, u64 map_flags);
14dc6f04 1155int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
bcc6b1b7
MKL
1156int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1157 void *key, void *value, u64 map_flags);
14dc6f04 1158int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
d056a788 1159
6e71b04a 1160int bpf_get_file_flag(int flags);
dcab51f1
MKL
1161int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
1162 size_t actual_size);
6e71b04a 1163
15a07b33
AS
1164/* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
1165 * forced to use 'long' read/writes to try to atomically copy long counters.
1166 * Best-effort only. No barriers here, since it _will_ race with concurrent
1167 * updates from BPF programs. Called from bpf syscall and mostly used with
1168 * size 8 or 16 bytes, so ask compiler to inline it.
1169 */
1170static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
1171{
1172 const long *lsrc = src;
1173 long *ldst = dst;
1174
1175 size /= sizeof(long);
1176 while (size--)
1177 *ldst++ = *lsrc++;
1178}
1179
61e021f3 1180/* verify correctness of eBPF program */
838e9690
YS
1181int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
1182 union bpf_attr __user *uattr);
1ea47e01 1183void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
46f55cff
JF
1184
1185/* Map specifics */
67f29e07 1186struct xdp_buff;
6d5fc195 1187struct sk_buff;
67f29e07
JDB
1188
1189struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
6f9d451a 1190struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
1d233886
THJ
1191void __dev_flush(void);
1192int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1193 struct net_device *dev_rx);
38edddb8
JDB
1194int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1195 struct net_device *dev_rx);
6d5fc195
TM
1196int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1197 struct bpf_prog *xdp_prog);
46f55cff 1198
9c270af3 1199struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
cdfafe98 1200void __cpu_map_flush(void);
9c270af3
JDB
1201int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
1202 struct net_device *dev_rx);
1203
96eabe7a
MKL
1204/* Return map's numa specified by userspace */
1205static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1206{
1207 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1208 attr->numa_node : NUMA_NO_NODE;
1209}
1210
040ee692 1211struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
5dc4c4b7 1212int array_map_alloc_check(union bpf_attr *attr);
040ee692 1213
c695865c
SF
1214int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1215 union bpf_attr __user *uattr);
1216int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1217 union bpf_attr __user *uattr);
da00d2f1
KS
1218int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1219 const union bpf_attr *kattr,
1220 union bpf_attr __user *uattr);
c695865c
SF
1221int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1222 const union bpf_attr *kattr,
1223 union bpf_attr __user *uattr);
9e15db66
AS
1224bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1225 const struct bpf_prog *prog,
1226 struct bpf_insn_access_aux *info);
1227int btf_struct_access(struct bpf_verifier_log *log,
1228 const struct btf_type *t, int off, int size,
1229 enum bpf_access_type atype,
1230 u32 *next_btf_id);
9cc31b3a
AS
1231int btf_resolve_helper_id(struct bpf_verifier_log *log,
1232 const struct bpf_func_proto *fn, int);
9e15db66 1233
fec56f58
AS
1234int btf_distill_func_proto(struct bpf_verifier_log *log,
1235 struct btf *btf,
1236 const struct btf_type *func_proto,
1237 const char *func_name,
1238 struct btf_func_model *m);
1239
51c39bb1
AS
1240struct bpf_reg_state;
1241int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
1242 struct bpf_reg_state *regs);
1243int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1244 struct bpf_reg_state *reg);
be8704ff
AS
1245int btf_check_type_match(struct bpf_verifier_env *env, struct bpf_prog *prog,
1246 struct btf *btf, const struct btf_type *t);
8c1b6e69 1247
7e6897f9
BT
1248struct bpf_prog *bpf_prog_by_id(u32 id);
1249
6890896b 1250const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
9c270af3 1251#else /* !CONFIG_BPF_SYSCALL */
0fc174de
DB
1252static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1253{
1254 return ERR_PTR(-EOPNOTSUPP);
1255}
1256
248f346f
JK
1257static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1258 enum bpf_prog_type type,
288b3de5 1259 bool attach_drv)
248f346f
JK
1260{
1261 return ERR_PTR(-EOPNOTSUPP);
1262}
1263
85192dbf 1264static inline void bpf_prog_add(struct bpf_prog *prog, int i)
cc2e0b3f 1265{
cc2e0b3f 1266}
113214be 1267
c540594f
DB
1268static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1269{
1270}
1271
0fc174de
DB
1272static inline void bpf_prog_put(struct bpf_prog *prog)
1273{
1274}
6d67942d 1275
85192dbf 1276static inline void bpf_prog_inc(struct bpf_prog *prog)
aa6a5f3c 1277{
aa6a5f3c 1278}
5ccb071e 1279
a6f6df69
JF
1280static inline struct bpf_prog *__must_check
1281bpf_prog_inc_not_zero(struct bpf_prog *prog)
1282{
1283 return ERR_PTR(-EOPNOTSUPP);
1284}
1285
5ccb071e
DB
1286static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
1287{
1288 return 0;
1289}
1290
1291static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1292{
1293}
46f55cff 1294
6e71b04a 1295static inline int bpf_obj_get_user(const char __user *pathname, int flags)
98589a09
SL
1296{
1297 return -EOPNOTSUPP;
1298}
1299
46f55cff
JF
1300static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
1301 u32 key)
1302{
1303 return NULL;
1304}
1305
6f9d451a
THJ
1306static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
1307 u32 key)
1308{
1309 return NULL;
1310}
1311
1d233886 1312static inline void __dev_flush(void)
46f55cff
JF
1313{
1314}
9c270af3 1315
67f29e07
JDB
1316struct xdp_buff;
1317struct bpf_dtab_netdev;
1318
1d233886
THJ
1319static inline
1320int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1321 struct net_device *dev_rx)
1322{
1323 return 0;
1324}
1325
67f29e07 1326static inline
38edddb8
JDB
1327int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1328 struct net_device *dev_rx)
67f29e07
JDB
1329{
1330 return 0;
1331}
1332
6d5fc195
TM
1333struct sk_buff;
1334
1335static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
1336 struct sk_buff *skb,
1337 struct bpf_prog *xdp_prog)
1338{
1339 return 0;
1340}
1341
9c270af3
JDB
1342static inline
1343struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
1344{
1345 return NULL;
1346}
1347
cdfafe98 1348static inline void __cpu_map_flush(void)
9c270af3
JDB
1349{
1350}
1351
9c270af3
JDB
1352static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1353 struct xdp_buff *xdp,
1354 struct net_device *dev_rx)
1355{
1356 return 0;
1357}
040ee692
AV
1358
1359static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1360 enum bpf_prog_type type)
1361{
1362 return ERR_PTR(-EOPNOTSUPP);
1363}
c695865c
SF
1364
1365static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1366 const union bpf_attr *kattr,
1367 union bpf_attr __user *uattr)
1368{
1369 return -ENOTSUPP;
1370}
1371
1372static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1373 const union bpf_attr *kattr,
1374 union bpf_attr __user *uattr)
1375{
1376 return -ENOTSUPP;
1377}
1378
da00d2f1
KS
1379static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1380 const union bpf_attr *kattr,
1381 union bpf_attr __user *uattr)
1382{
1383 return -ENOTSUPP;
1384}
1385
c695865c
SF
1386static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1387 const union bpf_attr *kattr,
1388 union bpf_attr __user *uattr)
1389{
1390 return -ENOTSUPP;
1391}
6332be04
DB
1392
1393static inline void bpf_map_put(struct bpf_map *map)
1394{
1395}
7e6897f9
BT
1396
1397static inline struct bpf_prog *bpf_prog_by_id(u32 id)
1398{
1399 return ERR_PTR(-ENOTSUPP);
1400}
6890896b
SF
1401
1402static inline const struct bpf_func_proto *
1403bpf_base_func_proto(enum bpf_func_id func_id)
1404{
1405 return NULL;
1406}
61e021f3 1407#endif /* CONFIG_BPF_SYSCALL */
09756af4 1408
479321e9
JK
1409static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1410 enum bpf_prog_type type)
1411{
1412 return bpf_prog_get_type_dev(ufd, type, false);
1413}
1414
040ee692
AV
1415bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1416
ab3f0063
JK
1417int bpf_prog_offload_compile(struct bpf_prog *prog);
1418void bpf_prog_offload_destroy(struct bpf_prog *prog);
675fc275
JK
1419int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1420 struct bpf_prog *prog);
ab3f0063 1421
52775b33
JK
1422int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1423
a3884572
JK
1424int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1425int bpf_map_offload_update_elem(struct bpf_map *map,
1426 void *key, void *value, u64 flags);
1427int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1428int bpf_map_offload_get_next_key(struct bpf_map *map,
1429 void *key, void *next_key);
1430
09728266 1431bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
a3884572 1432
1385d755 1433struct bpf_offload_dev *
dd27c2e3 1434bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
602144c2 1435void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
dd27c2e3 1436void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
602144c2
JK
1437int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1438 struct net_device *netdev);
1439void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1440 struct net_device *netdev);
fd4f227d 1441bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
9fd7c555 1442
ab3f0063
JK
1443#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1444int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1445
0d830032 1446static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
ab3f0063 1447{
9a18eedb 1448 return aux->offload_requested;
ab3f0063 1449}
a3884572
JK
1450
1451static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1452{
1453 return unlikely(map->ops == &bpf_map_offload_ops);
1454}
1455
1456struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1457void bpf_map_offload_map_free(struct bpf_map *map);
ab3f0063
JK
1458#else
1459static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1460 union bpf_attr *attr)
1461{
1462 return -EOPNOTSUPP;
1463}
1464
1465static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1466{
1467 return false;
1468}
a3884572
JK
1469
1470static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1471{
1472 return false;
1473}
1474
1475static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1476{
1477 return ERR_PTR(-EOPNOTSUPP);
1478}
1479
1480static inline void bpf_map_offload_map_free(struct bpf_map *map)
1481{
1482}
ab3f0063
JK
1483#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1484
604326b4
DB
1485#if defined(CONFIG_BPF_STREAM_PARSER)
1486int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
1487int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
f747632b
LB
1488void sock_map_unhash(struct sock *sk);
1489void sock_map_close(struct sock *sk, long timeout);
6bdc9c4c 1490#else
604326b4
DB
1491static inline int sock_map_prog_update(struct bpf_map *map,
1492 struct bpf_prog *prog, u32 which)
464bc0fd
JF
1493{
1494 return -EOPNOTSUPP;
1495}
fdb5c453 1496
604326b4
DB
1497static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1498 struct bpf_prog *prog)
fdb5c453
SY
1499{
1500 return -EINVAL;
1501}
f747632b 1502#endif /* CONFIG_BPF_STREAM_PARSER */
6bdc9c4c 1503
5dc4c4b7
MKL
1504#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1505void bpf_sk_reuseport_detach(struct sock *sk);
1506int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1507 void *value);
1508int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1509 void *value, u64 map_flags);
1510#else
1511static inline void bpf_sk_reuseport_detach(struct sock *sk)
1512{
1513}
1514
1515#ifdef CONFIG_BPF_SYSCALL
1516static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1517 void *key, void *value)
1518{
1519 return -EOPNOTSUPP;
1520}
1521
1522static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1523 void *key, void *value,
1524 u64 map_flags)
1525{
1526 return -EOPNOTSUPP;
1527}
1528#endif /* CONFIG_BPF_SYSCALL */
1529#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1530
d0003ec0 1531/* verifier prototypes for helper functions called from eBPF programs */
a2c83fff
DB
1532extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1533extern const struct bpf_func_proto bpf_map_update_elem_proto;
1534extern const struct bpf_func_proto bpf_map_delete_elem_proto;
f1a2e44a
MV
1535extern const struct bpf_func_proto bpf_map_push_elem_proto;
1536extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1537extern const struct bpf_func_proto bpf_map_peek_elem_proto;
d0003ec0 1538
03e69b50 1539extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
c04167ce 1540extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
2d0e30c3 1541extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
04fd61ab 1542extern const struct bpf_func_proto bpf_tail_call_proto;
17ca8cbf 1543extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
71d19214 1544extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
ffeedafb
AS
1545extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1546extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1547extern const struct bpf_func_proto bpf_get_current_comm_proto;
d5a3b1f6 1548extern const struct bpf_func_proto bpf_get_stackid_proto;
c195651e 1549extern const struct bpf_func_proto bpf_get_stack_proto;
174a79ff 1550extern const struct bpf_func_proto bpf_sock_map_update_proto;
81110384 1551extern const struct bpf_func_proto bpf_sock_hash_update_proto;
bf6fa2c8 1552extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
0f09abd1 1553extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
604326b4
DB
1554extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1555extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1556extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1557extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
d83525ca
AS
1558extern const struct bpf_func_proto bpf_spin_lock_proto;
1559extern const struct bpf_func_proto bpf_spin_unlock_proto;
cd339431 1560extern const struct bpf_func_proto bpf_get_local_storage_proto;
d7a4cb9b
AI
1561extern const struct bpf_func_proto bpf_strtol_proto;
1562extern const struct bpf_func_proto bpf_strtoul_proto;
0d01da6a 1563extern const struct bpf_func_proto bpf_tcp_sock_proto;
5576b991 1564extern const struct bpf_func_proto bpf_jiffies64_proto;
b4490c5c 1565extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
0456ea17 1566extern const struct bpf_func_proto bpf_event_output_data_proto;
cd339431 1567
fc611f47
KS
1568const struct bpf_func_proto *bpf_tracing_func_proto(
1569 enum bpf_func_id func_id, const struct bpf_prog *prog);
1570
3ad00405
DB
1571/* Shared helpers among cBPF and eBPF. */
1572void bpf_user_rnd_init_once(void);
1573u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
6890896b 1574u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
3ad00405 1575
c64b7983 1576#if defined(CONFIG_NET)
46f8bc92
MKL
1577bool bpf_sock_common_is_valid_access(int off, int size,
1578 enum bpf_access_type type,
1579 struct bpf_insn_access_aux *info);
c64b7983
JS
1580bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1581 struct bpf_insn_access_aux *info);
1582u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1583 const struct bpf_insn *si,
1584 struct bpf_insn *insn_buf,
1585 struct bpf_prog *prog,
1586 u32 *target_size);
1587#else
46f8bc92
MKL
1588static inline bool bpf_sock_common_is_valid_access(int off, int size,
1589 enum bpf_access_type type,
1590 struct bpf_insn_access_aux *info)
1591{
1592 return false;
1593}
c64b7983
JS
1594static inline bool bpf_sock_is_valid_access(int off, int size,
1595 enum bpf_access_type type,
1596 struct bpf_insn_access_aux *info)
1597{
1598 return false;
1599}
1600static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1601 const struct bpf_insn *si,
1602 struct bpf_insn *insn_buf,
1603 struct bpf_prog *prog,
1604 u32 *target_size)
1605{
1606 return 0;
1607}
1608#endif
1609
655a51e5 1610#ifdef CONFIG_INET
91cc1a99
AS
1611struct sk_reuseport_kern {
1612 struct sk_buff *skb;
1613 struct sock *sk;
1614 struct sock *selected_sk;
1615 void *data_end;
1616 u32 hash;
1617 u32 reuseport_id;
1618 bool bind_inany;
1619};
655a51e5
MKL
1620bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1621 struct bpf_insn_access_aux *info);
1622
1623u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1624 const struct bpf_insn *si,
1625 struct bpf_insn *insn_buf,
1626 struct bpf_prog *prog,
1627 u32 *target_size);
7f94208c
Y
1628
1629bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1630 struct bpf_insn_access_aux *info);
1631
1632u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1633 const struct bpf_insn *si,
1634 struct bpf_insn *insn_buf,
1635 struct bpf_prog *prog,
1636 u32 *target_size);
655a51e5
MKL
1637#else
1638static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1639 enum bpf_access_type type,
1640 struct bpf_insn_access_aux *info)
1641{
1642 return false;
1643}
1644
1645static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1646 const struct bpf_insn *si,
1647 struct bpf_insn *insn_buf,
1648 struct bpf_prog *prog,
1649 u32 *target_size)
1650{
1651 return 0;
1652}
7f94208c
Y
1653static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1654 enum bpf_access_type type,
1655 struct bpf_insn_access_aux *info)
1656{
1657 return false;
1658}
1659
1660static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1661 const struct bpf_insn *si,
1662 struct bpf_insn *insn_buf,
1663 struct bpf_prog *prog,
1664 u32 *target_size)
1665{
1666 return 0;
1667}
655a51e5
MKL
1668#endif /* CONFIG_INET */
1669
5964b200 1670enum bpf_text_poke_type {
b553a6ec
DB
1671 BPF_MOD_CALL,
1672 BPF_MOD_JUMP,
5964b200 1673};
4b3da77b 1674
5964b200
AS
1675int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1676 void *addr1, void *addr2);
1677
99c55f7d 1678#endif /* _LINUX_BPF_H */