blob: 4fd91b7c95ea33604a2a12c3bb4425f3ddcf9b7f [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#ifndef _LINUX_BPF_H
5#define _LINUX_BPF_H 1
6
7#include <uapi/linux/bpf.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +01008
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07009#include <linux/workqueue.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070010#include <linux/file.h>
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080011#include <linux/percpu.h>
Zi Shen Lim002245c2016-06-08 21:18:47 -070012#include <linux/err.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +010013#include <linux/rbtree_latch.h>
David S. Millerd6e1e462017-08-19 23:34:03 -070014#include <linux/numa.h>
Andrii Nakryikofc970222019-11-17 09:28:04 -080015#include <linux/mm_types.h>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070016#include <linux/wait.h>
Alexei Starovoitov492ecee2019-02-25 14:28:39 -080017#include <linux/u64_stats_sync.h>
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080018#include <linux/refcount.h>
19#include <linux/mutex.h>
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080020#include <linux/module.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070021
Jakub Kicinskicae19272017-12-27 18:39:05 -080022struct bpf_verifier_env;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -070023struct bpf_verifier_log;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +020024struct perf_event;
John Fastabend174a79f2017-08-15 22:32:47 -070025struct bpf_prog;
Daniel Borkmannda765a22019-11-22 21:07:58 +010026struct bpf_prog_aux;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070027struct bpf_map;
John Fastabend4f738ad2018-03-18 12:57:10 -070028struct sock;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070029struct seq_file;
Roman Gushchin1b2b2342018-12-10 15:43:00 -080030struct btf;
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020031struct btf_type;
Alexei Starovoitov3dec5412019-10-15 20:25:03 -070032struct exception_table_entry;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070033
Quentin Monnet1b9ed842019-08-20 10:31:50 +010034extern struct idr btf_idr;
35extern spinlock_t btf_idr_lock;
36
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070037/* map is generic key/value storage optionally accesible by eBPF programs */
38struct bpf_map_ops {
39 /* funcs callable from userspace (via syscall) */
Jakub Kicinski1110f3a2018-01-11 20:29:03 -080040 int (*map_alloc_check)(union bpf_attr *attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070041 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +020042 void (*map_release)(struct bpf_map *map, struct file *map_file);
43 void (*map_free)(struct bpf_map *map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070044 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
John Fastabendba6b8de2018-04-23 15:39:23 -070045 void (*map_release_uref)(struct bpf_map *map);
Daniel Borkmannc6110222019-05-14 01:18:55 +020046 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -080047 int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
48 union bpf_attr __user *uattr);
Yonghong Song05799632020-01-15 10:43:04 -080049 int (*map_lookup_and_delete_batch)(struct bpf_map *map,
50 const union bpf_attr *attr,
51 union bpf_attr __user *uattr);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -080052 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);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070056
57 /* funcs callable from userspace and from eBPF programs */
58 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
Alexei Starovoitov3274f522014-11-13 17:36:44 -080059 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070060 int (*map_delete_elem)(struct bpf_map *map, void *key);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +020061 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);
Wang Nan2a36f0b2015-08-06 07:02:33 +000064
65 /* funcs called by prog_array and perf_event_array map */
Daniel Borkmannd056a782016-06-15 22:47:13 +020066 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
67 int fd);
68 void (*map_fd_put_ptr)(void *ptr);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -070069 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070070 u32 (*map_fd_sys_lookup_elem)(void *ptr);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070071 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
72 struct seq_file *m);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020073 int (*map_check_btf)(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -080074 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020075 const struct btf_type *key_type,
76 const struct btf_type *value_type);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +020077
Daniel Borkmannda765a22019-11-22 21:07:58 +010078 /* 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
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +020084 /* 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);
Andrii Nakryikofc970222019-11-17 09:28:04 -080089 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070090};
91
Roman Gushchin3539b962019-05-29 18:03:57 -070092struct bpf_map_memory {
93 u32 pages;
94 struct user_struct *user;
95};
96
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070097struct bpf_map {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070098 /* The first two cachelines with read-mostly members of which some
Daniel Borkmannbe95a842018-01-09 13:17:44 +010099 * 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
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106 enum bpf_map_type map_type;
107 u32 key_size;
108 u32 value_size;
109 u32 max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800110 u32 map_flags;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800111 int spin_lock_off; /* >=0 valid offset, <0 error */
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700112 u32 id;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700113 int numa_node;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700114 u32 btf_key_type_id;
115 u32 btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700116 struct btf *btf;
Roman Gushchin3539b962019-05-29 18:03:57 -0700117 struct bpf_map_memory memory;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800118 char name[BPF_OBJ_NAME_LEN];
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800119 u32 btf_vmlinux_value_type_id;
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800120 bool unpriv_array;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800121 bool frozen; /* write-once; write-protected by freeze_mutex */
122 /* 22 bytes hole */
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100123
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700124 /* The 3rd and 4th cacheline with misc members to avoid false sharing
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100125 * particularly with refcounting.
126 */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800127 atomic64_t refcnt ____cacheline_aligned;
128 atomic64_t usercnt;
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100129 struct work_struct work;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800130 struct mutex freeze_mutex;
131 u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700132};
133
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800134static inline bool map_value_has_spin_lock(const struct bpf_map *map)
135{
136 return map->spin_lock_off >= 0;
137}
138
139static 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 */
148static 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}
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800161void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
162 bool lock_src);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800163
Jakub Kicinski602144c2018-07-17 10:53:25 -0700164struct bpf_offload_dev;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800165struct bpf_offloaded_map;
166
167struct 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
177struct 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
185static 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
Jakub Kicinski0cd3cbe2018-05-03 18:37:08 -0700190static 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
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700195static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
196{
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800197 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
198 map->ops->map_seq_show_elem;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700199}
200
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200201int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800202 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200203 const struct btf_type *key_type,
204 const struct btf_type *value_type);
205
Jakub Kicinskia3884572018-01-11 20:29:09 -0800206extern const struct bpf_map_ops bpf_map_offload_ops;
207
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700208/* function argument constraints */
209enum bpf_arg_type {
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100210 ARG_DONTCARE = 0, /* unused argument in helper function */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700211
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 */
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +0200218 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700219 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700220
221 /* the following constraints used to prototype bpf_memcmp() and other
222 * functions that access data on eBPF program stack
223 */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800224 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
Gianluca Borellodb1ac492017-11-22 18:32:53 +0000225 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800226 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.
Daniel Borkmann435faee12016-04-13 00:10:51 +0200229 */
230
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800231 ARG_CONST_SIZE, /* number of bytes accessed from memory */
232 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100233
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700234 ARG_PTR_TO_CTX, /* pointer to context */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100235 ARG_ANYTHING, /* any (initialized) argument is ok */
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800236 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800237 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
Andrey Ignatov57c3bb72019-03-18 16:57:10 -0700238 ARG_PTR_TO_INT, /* pointer to int */
239 ARG_PTR_TO_LONG, /* pointer to long */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700240 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
Alexei Starovoitova7658e12019-10-15 20:25:04 -0700241 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700242};
243
244/* type of values returned from helper functions */
245enum bpf_return_type {
246 RET_INTEGER, /* function returns integer */
247 RET_VOID, /* function doesn't return anything */
Roman Gushchin3e6a4b32018-08-02 14:27:22 -0700248 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700249 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
Joe Stringerc64b7982018-10-02 13:35:33 -0700250 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800251 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
Lorenz Bauer85a51f82019-03-22 09:54:00 +0800252 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700253};
254
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700255/* 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 */
259struct bpf_func_proto {
260 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
261 bool gpl_only;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200262 bool pkt_access;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700263 enum bpf_return_type ret_type;
Alexei Starovoitova7658e12019-10-15 20:25:04 -0700264 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 };
Alexei Starovoitov9cc31b32019-11-14 10:57:14 -0800274 int *btf_id; /* BTF ids of arguments */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700275};
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 */
281struct bpf_context;
282
283enum bpf_access_type {
284 BPF_READ = 1,
285 BPF_WRITE = 2
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700286};
287
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700288/* types of values stored in eBPF registers */
Edward Creef1174f72017-08-07 15:26:19 +0100289/* 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 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700298enum bpf_reg_type {
299 NOT_INIT = 0, /* nothing was written into register */
Edward Creef1174f72017-08-07 15:26:19 +0100300 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700301 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 */
Edward Creef1174f72017-08-07 15:26:19 +0100305 PTR_TO_STACK, /* reg == frame_pointer + offset */
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200306 PTR_TO_PACKET_META, /* skb->data - meta_len */
Edward Creef1174f72017-08-07 15:26:19 +0100307 PTR_TO_PACKET, /* reg points to skb->data */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700308 PTR_TO_PACKET_END, /* skb->data + headlen */
Petar Penkovd58e4682018-09-14 07:46:18 -0700309 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
Joe Stringerc64b7982018-10-02 13:35:33 -0700310 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
311 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800312 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
313 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800314 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 */
Matt Mullins9df1c282019-04-26 11:49:47 -0700316 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700317 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700318 PTR_TO_BTF_ID, /* reg points to kernel struct */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700319};
320
Yonghong Song23994632017-06-22 15:07:39 -0700321/* The information passed from prog-specific *_is_valid_access
322 * back to the verifier.
323 */
324struct bpf_insn_access_aux {
325 enum bpf_reg_type reg_type;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700326 union {
327 int ctx_field_size;
328 u32 btf_id;
329 };
330 struct bpf_verifier_log *log; /* for verbose logs */
Yonghong Song23994632017-06-22 15:07:39 -0700331};
332
Daniel Borkmannf96da092017-07-02 02:13:27 +0200333static inline void
334bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
335{
336 aux->ctx_field_size = size;
337}
338
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700339struct bpf_prog_ops {
340 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
341 union bpf_attr __user *uattr);
342};
343
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700344struct bpf_verifier_ops {
345 /* return eBPF function prototype for verification */
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700346 const struct bpf_func_proto *
347 (*get_func_proto)(enum bpf_func_id func_id,
348 const struct bpf_prog *prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700349
350 /* return true if 'size' wide access at offset 'off' within bpf_context
351 * with 'type' (read or write) is allowed
352 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700353 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700354 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700355 struct bpf_insn_access_aux *info);
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200356 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
357 const struct bpf_prog *prog);
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +0200358 int (*gen_ld_abs)(const struct bpf_insn *orig,
359 struct bpf_insn *insn_buf);
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100360 u32 (*convert_ctx_access)(enum bpf_access_type type,
361 const struct bpf_insn *src,
362 struct bpf_insn *dst,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200363 struct bpf_prog *prog, u32 *target_size);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800364 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);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700368};
369
Jakub Kicinskicae19272017-12-27 18:39:05 -0800370struct bpf_prog_offload_ops {
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800371 /* verifier basic callbacks */
Jakub Kicinskicae19272017-12-27 18:39:05 -0800372 int (*insn_hook)(struct bpf_verifier_env *env,
373 int insn_idx, int prev_insn_idx);
Quentin Monnetc941ce92018-10-07 12:56:47 +0100374 int (*finalize)(struct bpf_verifier_env *env);
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800375 /* 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 */
Quentin Monnet16a8cb5c2018-11-09 13:03:32 +0000380 int (*prepare)(struct bpf_prog *prog);
381 int (*translate)(struct bpf_prog *prog);
Quentin Monneteb911942018-11-09 13:03:30 +0000382 void (*destroy)(struct bpf_prog *prog);
Jakub Kicinskicae19272017-12-27 18:39:05 -0800383};
384
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800385struct bpf_prog_offload {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700386 struct bpf_prog *prog;
387 struct net_device *netdev;
Quentin Monnet341b3e72018-11-09 13:03:26 +0000388 struct bpf_offload_dev *offdev;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700389 void *dev_priv;
390 struct list_head offloads;
391 bool dev_state;
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800392 bool opt_failed;
Jiong Wangfcfb1262018-01-16 16:05:19 -0800393 void *jited_image;
394 u32 jited_len;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700395};
396
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000397enum bpf_cgroup_storage_type {
398 BPF_CGROUP_STORAGE_SHARED,
Roman Gushchinb741f162018-09-28 14:45:43 +0000399 BPF_CGROUP_STORAGE_PERCPU,
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000400 __BPF_CGROUP_STORAGE_MAX
401};
402
403#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
404
Alexei Starovoitovf1b95092019-10-30 15:32:11 -0700405/* The longest tracepoint has 12 args.
406 * See include/trace/bpf_probe.h
407 */
408#define MAX_BPF_FUNC_ARGS 12
409
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800410struct bpf_prog_stats {
411 u64 cnt;
412 u64 nsecs;
413 struct u64_stats_sync syncp;
Eric Dumazet84a081f2019-10-11 11:11:40 -0700414} __aligned(2 * sizeof(u64));
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800415
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800416struct 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
KP Singh88fd9e52020-03-04 20:18:47 +0100436/* 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
441struct bpf_tramp_progs {
442 struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS];
443 int nr_progs;
444};
445
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800446/* 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 */
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800466int arch_prepare_bpf_trampoline(void *image, void *image_end,
467 const struct btf_func_model *m, u32 flags,
KP Singh88fd9e52020-03-04 20:18:47 +0100468 struct bpf_tramp_progs *tprogs,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800469 void *orig_call);
470/* these two functions are called from generated trampoline */
471u64 notrace __bpf_prog_enter(void);
472void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
473
474enum bpf_tramp_prog_type {
475 BPF_TRAMP_FENTRY,
476 BPF_TRAMP_FEXIT,
KP Singhae240822020-03-04 20:18:49 +0100477 BPF_TRAMP_MODIFY_RETURN,
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800478 BPF_TRAMP_MAX,
479 BPF_TRAMP_REPLACE, /* more than MAX */
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800480};
481
482struct 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;
Alexei Starovoitovb91e014f2019-12-08 16:01:13 -0800492 bool ftrace_managed;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800493 } func;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800494 /* 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;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800499 /* 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};
Björn Töpel75ccbef2019-12-13 18:51:08 +0100507
Björn Töpel116eb782019-12-13 18:51:12 +0100508#define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
Björn Töpel75ccbef2019-12-13 18:51:08 +0100509
510struct bpf_dispatcher_prog {
511 struct bpf_prog *prog;
512 refcount_t users;
513};
514
515struct 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
Björn Töpel7e6897f2019-12-13 18:51:09 +0100525static __always_inline unsigned int bpf_dispatcher_nopfunc(
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}
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800533#ifdef CONFIG_BPF_JIT
534struct bpf_trampoline *bpf_trampoline_lookup(u64 key);
535int bpf_trampoline_link_prog(struct bpf_prog *prog);
536int bpf_trampoline_unlink_prog(struct bpf_prog *prog);
537void bpf_trampoline_put(struct bpf_trampoline *tr);
Björn Töpel75ccbef2019-12-13 18:51:08 +0100538#define BPF_DISPATCHER_INIT(name) { \
539 .mutex = __MUTEX_INITIALIZER(name.mutex), \
540 .func = &name##func, \
541 .progs = {}, \
542 .num_progs = 0, \
543 .image = NULL, \
544 .image_off = 0 \
545}
546
547#define DEFINE_BPF_DISPATCHER(name) \
548 noinline unsigned int name##func( \
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 } \
556 EXPORT_SYMBOL(name##func); \
557 struct bpf_dispatcher name = BPF_DISPATCHER_INIT(name);
558#define DECLARE_BPF_DISPATCHER(name) \
559 unsigned int name##func( \
560 const void *ctx, \
561 const struct bpf_insn *insnsi, \
562 unsigned int (*bpf_func)(const void *, \
563 const struct bpf_insn *)); \
564 extern struct bpf_dispatcher name;
565#define BPF_DISPATCHER_FUNC(name) name##func
566#define BPF_DISPATCHER_PTR(name) (&name)
567void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
568 struct bpf_prog *to);
Jiri Olsae9b4e602020-01-23 17:15:07 +0100569struct bpf_image {
570 struct latch_tree_node tnode;
571 unsigned char data[];
572};
573#define BPF_IMAGE_SIZE (PAGE_SIZE - sizeof(struct bpf_image))
574bool is_bpf_image_address(unsigned long address);
575void *bpf_image_alloc(void);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800576#else
577static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
578{
579 return NULL;
580}
581static inline int bpf_trampoline_link_prog(struct bpf_prog *prog)
582{
583 return -ENOTSUPP;
584}
585static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
586{
587 return -ENOTSUPP;
588}
589static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
Björn Töpel75ccbef2019-12-13 18:51:08 +0100590#define DEFINE_BPF_DISPATCHER(name)
591#define DECLARE_BPF_DISPATCHER(name)
592#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nopfunc
593#define BPF_DISPATCHER_PTR(name) NULL
594static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
595 struct bpf_prog *from,
596 struct bpf_prog *to) {}
Jiri Olsae9b4e602020-01-23 17:15:07 +0100597static inline bool is_bpf_image_address(unsigned long address)
598{
599 return false;
600}
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800601#endif
602
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800603struct bpf_func_info_aux {
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -0800604 u16 linkage;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800605 bool unreliable;
606};
607
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100608enum bpf_jit_poke_reason {
609 BPF_POKE_REASON_TAIL_CALL,
610};
611
612/* Descriptor of pokes pointing /into/ the JITed image. */
613struct bpf_jit_poke_descriptor {
614 void *ip;
615 union {
616 struct {
617 struct bpf_map *map;
618 u32 key;
619 } tail_call;
620 };
621 bool ip_stable;
622 u8 adj_off;
623 u16 reason;
624};
625
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700626struct bpf_prog_aux {
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -0800627 atomic64_t refcnt;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100628 u32 used_map_cnt;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700629 u32 max_ctx_offset;
Jiong Wange6478152018-11-08 04:08:42 -0500630 u32 max_pkt_offset;
Matt Mullins9df1c282019-04-26 11:49:47 -0700631 u32 max_tp_access;
Alexei Starovoitov87266792017-05-30 13:31:29 -0700632 u32 stack_depth;
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700633 u32 id;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800634 u32 func_cnt; /* used by non-func prog as the number of func progs */
635 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
Alexei Starovoitovccfe29eb22019-10-15 20:24:58 -0700636 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -0800637 struct bpf_prog *linked_prog;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +0100638 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800639 bool offload_requested;
Martin KaFai Lau38207292019-10-24 17:18:11 -0700640 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800641 bool func_proto_unreliable;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800642 enum bpf_tramp_prog_type trampoline_prog_type;
643 struct bpf_trampoline *trampoline;
644 struct hlist_node tramp_hlist;
Martin KaFai Lau38207292019-10-24 17:18:11 -0700645 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
646 const struct btf_type *attach_func_proto;
647 /* function name for valid attach_btf_id */
648 const char *attach_func_name;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800649 struct bpf_prog **func;
650 void *jit_data; /* JIT specific data. arch dependent */
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100651 struct bpf_jit_poke_descriptor *poke_tab;
652 u32 size_poke_tab;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100653 struct latch_tree_node ksym_tnode;
654 struct list_head ksym_lnode;
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700655 const struct bpf_prog_ops *ops;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700656 struct bpf_map **used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700657 struct bpf_prog *prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700658 struct user_struct *user;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700659 u64 load_time; /* ns since boottime */
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000660 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700661 char name[BPF_OBJ_NAME_LEN];
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700662#ifdef CONFIG_SECURITY
663 void *security;
664#endif
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800665 struct bpf_prog_offload *offload;
Yonghong Song838e9692018-11-19 15:29:11 -0800666 struct btf *btf;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800667 struct bpf_func_info *func_info;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800668 struct bpf_func_info_aux *func_info_aux;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800669 /* bpf_line_info loaded from userspace. linfo->insn_off
670 * has the xlated insn offset.
671 * Both the main and sub prog share the same linfo.
672 * The subprog can access its first linfo by
673 * using the linfo_idx.
674 */
675 struct bpf_line_info *linfo;
676 /* jited_linfo is the jited addr of the linfo. It has a
677 * one to one mapping to linfo:
678 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
679 * Both the main and sub prog share the same jited_linfo.
680 * The subprog can access its first jited_linfo by
681 * using the linfo_idx.
682 */
683 void **jited_linfo;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800684 u32 func_info_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800685 u32 nr_linfo;
686 /* subprog can use linfo_idx to access its first linfo and
687 * jited_linfo.
688 * main prog always has linfo_idx == 0
689 */
690 u32 linfo_idx;
Alexei Starovoitov3dec5412019-10-15 20:25:03 -0700691 u32 num_exentries;
692 struct exception_table_entry *extable;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800693 struct bpf_prog_stats __percpu *stats;
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700694 union {
695 struct work_struct work;
696 struct rcu_head rcu;
697 };
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700698};
699
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100700struct bpf_array_aux {
701 /* 'Ownership' of prog array is claimed by the first program that
702 * is going to use this map or by the first program which FD is
703 * stored in the map to make sure that all callers and callees have
704 * the same prog type and JITed flag.
705 */
706 enum bpf_prog_type type;
707 bool jited;
Daniel Borkmannda765a22019-11-22 21:07:58 +0100708 /* Programs with direct jumps into programs part of this array. */
709 struct list_head poke_progs;
710 struct bpf_map *map;
711 struct mutex poke_mutex;
712 struct work_struct work;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100713};
714
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800715struct bpf_struct_ops_value;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800716struct btf_type;
717struct btf_member;
718
719#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
720struct bpf_struct_ops {
721 const struct bpf_verifier_ops *verifier_ops;
722 int (*init)(struct btf *btf);
723 int (*check_member)(const struct btf_type *t,
724 const struct btf_member *member);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800725 int (*init_member)(const struct btf_type *t,
726 const struct btf_member *member,
727 void *kdata, const void *udata);
728 int (*reg)(void *kdata);
729 void (*unreg)(void *kdata);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800730 const struct btf_type *type;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800731 const struct btf_type *value_type;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800732 const char *name;
733 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
734 u32 type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800735 u32 value_id;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800736};
737
738#if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800739#define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800740const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
Martin KaFai Laud3e42bb2020-01-27 09:51:45 -0800741void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800742bool bpf_struct_ops_get(const void *kdata);
743void bpf_struct_ops_put(const void *kdata);
744int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
745 void *value);
746static inline bool bpf_try_module_get(const void *data, struct module *owner)
747{
748 if (owner == BPF_MODULE_OWNER)
749 return bpf_struct_ops_get(data);
750 else
751 return try_module_get(owner);
752}
753static inline void bpf_module_put(const void *data, struct module *owner)
754{
755 if (owner == BPF_MODULE_OWNER)
756 bpf_struct_ops_put(data);
757 else
758 module_put(owner);
759}
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800760#else
761static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
762{
763 return NULL;
764}
Martin KaFai Laud3e42bb2020-01-27 09:51:45 -0800765static inline void bpf_struct_ops_init(struct btf *btf,
766 struct bpf_verifier_log *log)
767{
768}
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800769static inline bool bpf_try_module_get(const void *data, struct module *owner)
770{
771 return try_module_get(owner);
772}
773static inline void bpf_module_put(const void *data, struct module *owner)
774{
775 module_put(owner);
776}
777static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
778 void *key,
779 void *value)
780{
781 return -EINVAL;
782}
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800783#endif
784
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700785struct bpf_array {
786 struct bpf_map map;
787 u32 elem_size;
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800788 u32 index_mask;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100789 struct bpf_array_aux *aux;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700790 union {
791 char value[0] __aligned(8);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000792 void *ptrs[0] __aligned(8);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800793 void __percpu *pptrs[0] __aligned(8);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700794 };
795};
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200796
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -0700797#define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700798#define MAX_TAIL_CALL_CNT 32
799
Daniel Borkmann591fe982019-04-09 23:20:05 +0200800#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
801 BPF_F_RDONLY_PROG | \
802 BPF_F_WRONLY | \
803 BPF_F_WRONLY_PROG)
804
805#define BPF_MAP_CAN_READ BIT(0)
806#define BPF_MAP_CAN_WRITE BIT(1)
807
808static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
809{
810 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
811
812 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
813 * not possible.
814 */
815 if (access_flags & BPF_F_RDONLY_PROG)
816 return BPF_MAP_CAN_READ;
817 else if (access_flags & BPF_F_WRONLY_PROG)
818 return BPF_MAP_CAN_WRITE;
819 else
820 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
821}
822
823static inline bool bpf_map_flags_access_ok(u32 access_flags)
824{
825 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
826 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
827}
828
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200829struct bpf_event_entry {
830 struct perf_event *event;
831 struct file *perf_file;
832 struct file *map_file;
833 struct rcu_head rcu;
834};
835
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700836bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100837int bpf_prog_calc_tag(struct bpf_prog *fp);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700838const char *kernel_type_name(u32 btf_type_id);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200839
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700840const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200841
842typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +0200843 unsigned long off, unsigned long len);
Joe Stringerc64b7982018-10-02 13:35:33 -0700844typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
845 const struct bpf_insn *src,
846 struct bpf_insn *dst,
847 struct bpf_prog *prog,
848 u32 *target_size);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200849
850u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
851 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700852
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700853/* an array of programs to be executed under rcu_lock.
854 *
855 * Typical usage:
856 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
857 *
858 * the structure returned by bpf_prog_array_alloc() should be populated
859 * with program pointers and the last pointer must be NULL.
860 * The user has to keep refcnt on the program and make sure the program
861 * is removed from the array before bpf_prog_put().
862 * The 'struct bpf_prog_array *' should only be replaced with xchg()
863 * since other cpus are walking the array of pointers in parallel.
864 */
Roman Gushchin394e40a2018-08-02 14:27:21 -0700865struct bpf_prog_array_item {
866 struct bpf_prog *prog;
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000867 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Roman Gushchin394e40a2018-08-02 14:27:21 -0700868};
869
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700870struct bpf_prog_array {
871 struct rcu_head rcu;
Gustavo A. R. Silvad7f10df2020-02-26 18:17:44 -0600872 struct bpf_prog_array_item items[];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700873};
874
Roman Gushchind29ab6e2018-07-13 12:41:10 -0700875struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700876void bpf_prog_array_free(struct bpf_prog_array *progs);
877int bpf_prog_array_length(struct bpf_prog_array *progs);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -0700878bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700879int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700880 __u32 __user *prog_ids, u32 cnt);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700881
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700882void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700883 struct bpf_prog *old_prog);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700884int bpf_prog_array_copy_info(struct bpf_prog_array *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -0700885 u32 *prog_ids, u32 request_cnt,
886 u32 *prog_cnt);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700887int bpf_prog_array_copy(struct bpf_prog_array *old_array,
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700888 struct bpf_prog *exclude_prog,
889 struct bpf_prog *include_prog,
890 struct bpf_prog_array **new_array);
891
892#define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700893 ({ \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700894 struct bpf_prog_array_item *_item; \
895 struct bpf_prog *_prog; \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700896 struct bpf_prog_array *_array; \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700897 u32 _ret = 1; \
David Miller2a916f22020-02-24 15:01:46 +0100898 migrate_disable(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700899 rcu_read_lock(); \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700900 _array = rcu_dereference(array); \
901 if (unlikely(check_non_null && !_array))\
902 goto _out; \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700903 _item = &_array->items[0]; \
904 while ((_prog = READ_ONCE(_item->prog))) { \
905 bpf_cgroup_storage_set(_item->cgroup_storage); \
906 _ret &= func(_prog, ctx); \
907 _item++; \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700908 } \
909_out: \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700910 rcu_read_unlock(); \
David Miller2a916f22020-02-24 15:01:46 +0100911 migrate_enable(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700912 _ret; \
913 })
914
brakmo1f52f6c2019-05-28 16:59:35 -0700915/* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
916 * so BPF programs can request cwr for TCP packets.
917 *
918 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
919 * packet. This macro changes the behavior so the low order bit
920 * indicates whether the packet should be dropped (0) or not (1)
921 * and the next bit is a congestion notification bit. This could be
922 * used by TCP to call tcp_enter_cwr()
923 *
924 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
925 * 0: drop packet
926 * 1: keep packet
927 * 2: drop packet and cn
928 * 3: keep packet and cn
929 *
930 * This macro then converts it to one of the NET_XMIT or an error
931 * code that is then interpreted as drop packet (and no cn):
932 * 0: NET_XMIT_SUCCESS skb should be transmitted
933 * 1: NET_XMIT_DROP skb should be dropped and cn
934 * 2: NET_XMIT_CN skb should be transmitted and cn
935 * 3: -EPERM skb should be dropped
936 */
937#define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
938 ({ \
939 struct bpf_prog_array_item *_item; \
940 struct bpf_prog *_prog; \
941 struct bpf_prog_array *_array; \
942 u32 ret; \
943 u32 _ret = 1; \
944 u32 _cn = 0; \
David Miller2a916f22020-02-24 15:01:46 +0100945 migrate_disable(); \
brakmo1f52f6c2019-05-28 16:59:35 -0700946 rcu_read_lock(); \
947 _array = rcu_dereference(array); \
948 _item = &_array->items[0]; \
949 while ((_prog = READ_ONCE(_item->prog))) { \
950 bpf_cgroup_storage_set(_item->cgroup_storage); \
951 ret = func(_prog, ctx); \
952 _ret &= (ret & 1); \
953 _cn |= (ret & 2); \
954 _item++; \
955 } \
956 rcu_read_unlock(); \
David Miller2a916f22020-02-24 15:01:46 +0100957 migrate_enable(); \
brakmo1f52f6c2019-05-28 16:59:35 -0700958 if (_ret) \
959 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
960 else \
961 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
962 _ret; \
963 })
964
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700965#define BPF_PROG_RUN_ARRAY(array, ctx, func) \
966 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
967
968#define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
969 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
970
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800971#ifdef CONFIG_BPF_SYSCALL
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800972DECLARE_PER_CPU(int, bpf_prog_active);
973
Thomas Gleixnerc518cfa2020-02-24 15:01:47 +0100974/*
975 * Block execution of BPF programs attached to instrumentation (perf,
976 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
977 * these events can happen inside a region which holds a map bucket lock
978 * and can deadlock on it.
979 *
980 * Use the preemption safe inc/dec variants on RT because migrate disable
981 * is preemptible on RT and preemption in the middle of the RMW operation
982 * might lead to inconsistent state. Use the raw variants for non RT
983 * kernels as migrate_disable() maps to preempt_disable() so the slightly
984 * more expensive save operation can be avoided.
985 */
986static inline void bpf_disable_instrumentation(void)
987{
988 migrate_disable();
989 if (IS_ENABLED(CONFIG_PREEMPT_RT))
990 this_cpu_inc(bpf_prog_active);
991 else
992 __this_cpu_inc(bpf_prog_active);
993}
994
995static inline void bpf_enable_instrumentation(void)
996{
997 if (IS_ENABLED(CONFIG_PREEMPT_RT))
998 this_cpu_dec(bpf_prog_active);
999 else
1000 __this_cpu_dec(bpf_prog_active);
1001 migrate_enable();
1002}
1003
Chenbo Fengf66e4482017-10-18 13:00:26 -07001004extern const struct file_operations bpf_map_fops;
1005extern const struct file_operations bpf_prog_fops;
1006
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001007#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001008 extern const struct bpf_prog_ops _name ## _prog_ops; \
1009 extern const struct bpf_verifier_ops _name ## _verifier_ops;
Johannes Berg40077e02017-04-11 15:34:58 +02001010#define BPF_MAP_TYPE(_id, _ops) \
1011 extern const struct bpf_map_ops _ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001012#include <linux/bpf_types.h>
1013#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +02001014#undef BPF_MAP_TYPE
Daniel Borkmann0fc174d2015-03-01 12:31:44 +01001015
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001016extern const struct bpf_prog_ops bpf_offload_prog_ops;
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001017extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1018extern const struct bpf_verifier_ops xdp_analyzer_ops;
1019
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001020struct bpf_prog *bpf_prog_get(u32 ufd);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001021struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -08001022 bool attach_drv);
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001023void bpf_prog_add(struct bpf_prog *prog, int i);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001024void bpf_prog_sub(struct bpf_prog *prog, int i);
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001025void bpf_prog_inc(struct bpf_prog *prog);
John Fastabenda6f6df62017-08-15 22:32:22 -07001026struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001027void bpf_prog_put(struct bpf_prog *prog);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001028int __bpf_prog_charge(struct user_struct *user, u32 pages);
1029void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
Daniel Borkmanna2ea07462019-12-16 17:49:00 +01001030void __bpf_free_used_maps(struct bpf_prog_aux *aux,
1031 struct bpf_map **used_maps, u32 len);
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001032
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001033void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001034void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001035
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -08001036struct bpf_map *bpf_map_get(u32 ufd);
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001037struct bpf_map *bpf_map_get_with_uref(u32 ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001038struct bpf_map *__bpf_map_get(struct fd f);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001039void bpf_map_inc(struct bpf_map *map);
1040void bpf_map_inc_with_uref(struct bpf_map *map);
1041struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001042void bpf_map_put_with_uref(struct bpf_map *map);
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001043void bpf_map_put(struct bpf_map *map);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -07001044int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
1045void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
Daniel Borkmann196e8ca2019-11-20 23:04:44 +01001046int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
Roman Gushchinb936ca62019-05-29 18:03:58 -07001047void bpf_map_charge_finish(struct bpf_map_memory *mem);
1048void bpf_map_charge_move(struct bpf_map_memory *dst,
1049 struct bpf_map_memory *src);
Daniel Borkmann196e8ca2019-11-20 23:04:44 +01001050void *bpf_map_area_alloc(u64 size, int numa_node);
1051void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001052void bpf_map_area_free(void *base);
Jakub Kicinskibd475642018-01-11 20:29:06 -08001053void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001054int generic_map_lookup_batch(struct bpf_map *map,
1055 const union bpf_attr *attr,
1056 union bpf_attr __user *uattr);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001057int generic_map_update_batch(struct bpf_map *map,
1058 const union bpf_attr *attr,
1059 union bpf_attr __user *uattr);
1060int generic_map_delete_batch(struct bpf_map *map,
1061 const union bpf_attr *attr,
1062 union bpf_attr __user *uattr);
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001063
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001064extern int sysctl_unprivileged_bpf_disabled;
1065
Chenbo Feng6e71b042017-10-18 13:00:22 -07001066int bpf_map_new_fd(struct bpf_map *map, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001067int bpf_prog_new_fd(struct bpf_prog *prog);
1068
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08001069struct bpf_link;
1070
1071struct bpf_link_ops {
1072 void (*release)(struct bpf_link *link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07001073 void (*dealloc)(struct bpf_link *link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08001074};
1075
1076void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,
1077 struct bpf_prog *prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07001078void bpf_link_defunct(struct bpf_link *link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08001079void bpf_link_inc(struct bpf_link *link);
1080void bpf_link_put(struct bpf_link *link);
1081int bpf_link_new_fd(struct bpf_link *link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07001082struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08001083struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1084
Daniel Borkmannb2197752015-10-29 14:58:09 +01001085int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
Chenbo Feng6e71b042017-10-18 13:00:22 -07001086int bpf_obj_get_user(const char __user *pathname, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001087
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001088int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1089int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1090int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1091 u64 flags);
1092int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1093 u64 flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +02001094
Alexei Starovoitov557c0c62016-03-07 21:57:17 -08001095int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001096
Daniel Borkmannd056a782016-06-15 22:47:13 +02001097int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1098 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001099int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001100int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1101 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001102int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Daniel Borkmannd056a782016-06-15 22:47:13 +02001103
Chenbo Feng6e71b042017-10-18 13:00:22 -07001104int bpf_get_file_flag(int flags);
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07001105int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
1106 size_t actual_size);
Chenbo Feng6e71b042017-10-18 13:00:22 -07001107
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001108/* 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 */
1114static 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
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001124/* verify correctness of eBPF program */
Yonghong Song838e9692018-11-19 15:29:11 -08001125int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
1126 union bpf_attr __user *uattr);
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001127void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
John Fastabend46f55cf2017-07-17 21:56:48 -07001128
1129/* Map specifics */
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001130struct xdp_buff;
Toshiaki Makita6d5fc192018-06-14 11:07:42 +09001131struct sk_buff;
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001132
1133struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +02001134struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001135void __dev_flush(void);
1136int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1137 struct net_device *dev_rx);
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +02001138int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1139 struct net_device *dev_rx);
Toshiaki Makita6d5fc192018-06-14 11:07:42 +09001140int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1141 struct bpf_prog *xdp_prog);
John Fastabend46f55cf2017-07-17 21:56:48 -07001142
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001143struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
Björn Töpelcdfafe92019-12-19 07:10:04 +01001144void __cpu_map_flush(void);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001145int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
1146 struct net_device *dev_rx);
1147
Martin KaFai Lau96eabe72017-08-18 11:28:00 -07001148/* Return map's numa specified by userspace */
1149static 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
Al Viro040ee692017-12-02 20:20:38 -05001155struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -07001156int array_map_alloc_check(union bpf_attr *attr);
Al Viro040ee692017-12-02 20:20:38 -05001157
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001158int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1159 union bpf_attr __user *uattr);
1160int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1161 union bpf_attr __user *uattr);
KP Singhda00d2f2020-03-04 20:18:52 +01001162int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1163 const union bpf_attr *kattr,
1164 union bpf_attr __user *uattr);
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001165int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1166 const union bpf_attr *kattr,
1167 union bpf_attr __user *uattr);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001168bool 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);
1171int 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);
Alexei Starovoitov9cc31b32019-11-14 10:57:14 -08001175int btf_resolve_helper_id(struct bpf_verifier_log *log,
1176 const struct bpf_func_proto *fn, int);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001177
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08001178int 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
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08001184struct bpf_reg_state;
1185int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
1186 struct bpf_reg_state *regs);
1187int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1188 struct bpf_reg_state *reg);
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001189int btf_check_type_match(struct bpf_verifier_env *env, struct bpf_prog *prog,
1190 struct btf *btf, const struct btf_type *t);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001191
Björn Töpel7e6897f2019-12-13 18:51:09 +01001192struct bpf_prog *bpf_prog_by_id(u32 id);
1193
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001194#else /* !CONFIG_BPF_SYSCALL */
Daniel Borkmann0fc174d2015-03-01 12:31:44 +01001195static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1196{
1197 return ERR_PTR(-EOPNOTSUPP);
1198}
1199
Jakub Kicinski248f3462017-11-03 13:56:20 -07001200static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1201 enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -08001202 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001203{
1204 return ERR_PTR(-EOPNOTSUPP);
1205}
1206
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001207static inline void bpf_prog_add(struct bpf_prog *prog, int i)
Brenden Blancocc2e0b32016-07-20 07:55:52 -07001208{
Brenden Blancocc2e0b32016-07-20 07:55:52 -07001209}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001210
Daniel Borkmannc5405942016-11-09 22:02:34 +01001211static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1212{
1213}
1214
Daniel Borkmann0fc174d2015-03-01 12:31:44 +01001215static inline void bpf_prog_put(struct bpf_prog *prog)
1216{
1217}
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +01001218
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001219static inline void bpf_prog_inc(struct bpf_prog *prog)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07001220{
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07001221}
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001222
John Fastabenda6f6df62017-08-15 22:32:22 -07001223static inline struct bpf_prog *__must_check
1224bpf_prog_inc_not_zero(struct bpf_prog *prog)
1225{
1226 return ERR_PTR(-EOPNOTSUPP);
1227}
1228
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001229static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
1230{
1231 return 0;
1232}
1233
1234static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1235{
1236}
John Fastabend46f55cf2017-07-17 21:56:48 -07001237
Chenbo Feng6e71b042017-10-18 13:00:22 -07001238static inline int bpf_obj_get_user(const char __user *pathname, int flags)
Shmulik Ladkani98589a02017-10-09 15:27:15 +03001239{
1240 return -EOPNOTSUPP;
1241}
1242
John Fastabend46f55cf2017-07-17 21:56:48 -07001243static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
1244 u32 key)
1245{
1246 return NULL;
1247}
1248
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +02001249static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
1250 u32 key)
1251{
1252 return NULL;
1253}
1254
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001255static inline void __dev_flush(void)
John Fastabend46f55cf2017-07-17 21:56:48 -07001256{
1257}
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001258
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001259struct xdp_buff;
1260struct bpf_dtab_netdev;
1261
1262static inline
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001263int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1264 struct net_device *dev_rx)
1265{
1266 return 0;
1267}
1268
1269static inline
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +02001270int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1271 struct net_device *dev_rx)
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001272{
1273 return 0;
1274}
1275
Toshiaki Makita6d5fc192018-06-14 11:07:42 +09001276struct sk_buff;
1277
1278static 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
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001285static inline
1286struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
1287{
1288 return NULL;
1289}
1290
Björn Töpelcdfafe92019-12-19 07:10:04 +01001291static inline void __cpu_map_flush(void)
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001292{
1293}
1294
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001295static 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}
Al Viro040ee692017-12-02 20:20:38 -05001301
1302static 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}
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001307
1308static 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
1315static 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
KP Singhda00d2f2020-03-04 20:18:52 +01001322static 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
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001329static 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}
Daniel Borkmann6332be02019-11-22 21:07:55 +01001335
1336static inline void bpf_map_put(struct bpf_map *map)
1337{
1338}
Björn Töpel7e6897f2019-12-13 18:51:09 +01001339
1340static inline struct bpf_prog *bpf_prog_by_id(u32 id)
1341{
1342 return ERR_PTR(-ENOTSUPP);
1343}
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001344#endif /* CONFIG_BPF_SYSCALL */
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001345
Jakub Kicinski479321e2017-11-20 15:21:56 -08001346static 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
Al Viro040ee692017-12-02 20:20:38 -05001352bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1353
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001354int bpf_prog_offload_compile(struct bpf_prog *prog);
1355void bpf_prog_offload_destroy(struct bpf_prog *prog);
Jakub Kicinski675fc272017-12-27 18:39:09 -08001356int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1357 struct bpf_prog *prog);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001358
Jakub Kicinski52775b32018-01-17 19:13:28 -08001359int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1360
Jakub Kicinskia3884572018-01-11 20:29:09 -08001361int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1362int bpf_map_offload_update_elem(struct bpf_map *map,
1363 void *key, void *value, u64 flags);
1364int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1365int bpf_map_offload_get_next_key(struct bpf_map *map,
1366 void *key, void *next_key);
1367
Jakub Kicinski09728262018-07-17 10:53:23 -07001368bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001369
Quentin Monnet1385d752018-11-09 13:03:25 +00001370struct bpf_offload_dev *
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -08001371bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
Jakub Kicinski602144c2018-07-17 10:53:25 -07001372void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -08001373void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
Jakub Kicinski602144c2018-07-17 10:53:25 -07001374int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1375 struct net_device *netdev);
1376void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1377 struct net_device *netdev);
Jakub Kicinskifd4f2272018-07-17 10:53:26 -07001378bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -07001379
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001380#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1381int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1382
Jakub Kicinski0d830032018-05-08 19:37:06 -07001383static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001384{
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001385 return aux->offload_requested;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001386}
Jakub Kicinskia3884572018-01-11 20:29:09 -08001387
1388static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1389{
1390 return unlikely(map->ops == &bpf_map_offload_ops);
1391}
1392
1393struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1394void bpf_map_offload_map_free(struct bpf_map *map);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001395#else
1396static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1397 union bpf_attr *attr)
1398{
1399 return -EOPNOTSUPP;
1400}
1401
1402static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1403{
1404 return false;
1405}
Jakub Kicinskia3884572018-01-11 20:29:09 -08001406
1407static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1408{
1409 return false;
1410}
1411
1412static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1413{
1414 return ERR_PTR(-EOPNOTSUPP);
1415}
1416
1417static inline void bpf_map_offload_map_free(struct bpf_map *map)
1418{
1419}
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001420#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1421
Daniel Borkmann604326b2018-10-13 02:45:58 +02001422#if defined(CONFIG_BPF_STREAM_PARSER)
1423int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
1424int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
Lorenz Bauerf7476322020-03-09 11:12:36 +00001425void sock_map_unhash(struct sock *sk);
1426void sock_map_close(struct sock *sk, long timeout);
John Fastabend6bdc9c42017-08-16 15:02:32 -07001427#else
Daniel Borkmann604326b2018-10-13 02:45:58 +02001428static inline int sock_map_prog_update(struct bpf_map *map,
1429 struct bpf_prog *prog, u32 which)
John Fastabend464bc0f2017-08-28 07:10:04 -07001430{
1431 return -EOPNOTSUPP;
1432}
Sean Youngfdb5c452018-06-19 00:04:24 +01001433
Daniel Borkmann604326b2018-10-13 02:45:58 +02001434static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1435 struct bpf_prog *prog)
Sean Youngfdb5c452018-06-19 00:04:24 +01001436{
1437 return -EINVAL;
1438}
Lorenz Bauerf7476322020-03-09 11:12:36 +00001439#endif /* CONFIG_BPF_STREAM_PARSER */
John Fastabend6bdc9c42017-08-16 15:02:32 -07001440
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -07001441#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1442void bpf_sk_reuseport_detach(struct sock *sk);
1443int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1444 void *value);
1445int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1446 void *value, u64 map_flags);
1447#else
1448static inline void bpf_sk_reuseport_detach(struct sock *sk)
1449{
1450}
1451
1452#ifdef CONFIG_BPF_SYSCALL
1453static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1454 void *key, void *value)
1455{
1456 return -EOPNOTSUPP;
1457}
1458
1459static 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
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001468/* verifier prototypes for helper functions called from eBPF programs */
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01001469extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1470extern const struct bpf_func_proto bpf_map_update_elem_proto;
1471extern const struct bpf_func_proto bpf_map_delete_elem_proto;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02001472extern const struct bpf_func_proto bpf_map_push_elem_proto;
1473extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1474extern const struct bpf_func_proto bpf_map_peek_elem_proto;
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001475
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001476extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001477extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001478extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001479extern const struct bpf_func_proto bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001480extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001481extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1482extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1483extern const struct bpf_func_proto bpf_get_current_comm_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08001484extern const struct bpf_func_proto bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -07001485extern const struct bpf_func_proto bpf_get_stack_proto;
John Fastabend174a79f2017-08-15 22:32:47 -07001486extern const struct bpf_func_proto bpf_sock_map_update_proto;
John Fastabend81110382018-05-14 10:00:17 -07001487extern const struct bpf_func_proto bpf_sock_hash_update_proto;
Yonghong Songbf6fa2c82018-06-03 15:59:41 -07001488extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
Daniel Borkmann604326b2018-10-13 02:45:58 +02001489extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1490extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1491extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1492extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001493extern const struct bpf_func_proto bpf_spin_lock_proto;
1494extern const struct bpf_func_proto bpf_spin_unlock_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07001495extern const struct bpf_func_proto bpf_get_local_storage_proto;
Andrey Ignatovd7a4cb92019-03-18 17:55:26 -07001496extern const struct bpf_func_proto bpf_strtol_proto;
1497extern const struct bpf_func_proto bpf_strtoul_proto;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001498extern const struct bpf_func_proto bpf_tcp_sock_proto;
Martin KaFai Lau5576b992020-01-22 15:36:46 -08001499extern const struct bpf_func_proto bpf_jiffies64_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07001500
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001501/* Shared helpers among cBPF and eBPF. */
1502void bpf_user_rnd_init_once(void);
1503u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1504
Joe Stringerc64b7982018-10-02 13:35:33 -07001505#if defined(CONFIG_NET)
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001506bool bpf_sock_common_is_valid_access(int off, int size,
1507 enum bpf_access_type type,
1508 struct bpf_insn_access_aux *info);
Joe Stringerc64b7982018-10-02 13:35:33 -07001509bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1510 struct bpf_insn_access_aux *info);
1511u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1512 const struct bpf_insn *si,
1513 struct bpf_insn *insn_buf,
1514 struct bpf_prog *prog,
1515 u32 *target_size);
1516#else
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001517static inline bool bpf_sock_common_is_valid_access(int off, int size,
1518 enum bpf_access_type type,
1519 struct bpf_insn_access_aux *info)
1520{
1521 return false;
1522}
Joe Stringerc64b7982018-10-02 13:35:33 -07001523static inline bool bpf_sock_is_valid_access(int off, int size,
1524 enum bpf_access_type type,
1525 struct bpf_insn_access_aux *info)
1526{
1527 return false;
1528}
1529static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1530 const struct bpf_insn *si,
1531 struct bpf_insn *insn_buf,
1532 struct bpf_prog *prog,
1533 u32 *target_size)
1534{
1535 return 0;
1536}
1537#endif
1538
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001539#ifdef CONFIG_INET
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001540struct sk_reuseport_kern {
1541 struct sk_buff *skb;
1542 struct sock *sk;
1543 struct sock *selected_sk;
1544 void *data_end;
1545 u32 hash;
1546 u32 reuseport_id;
1547 bool bind_inany;
1548};
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001549bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1550 struct bpf_insn_access_aux *info);
1551
1552u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1553 const struct bpf_insn *si,
1554 struct bpf_insn *insn_buf,
1555 struct bpf_prog *prog,
1556 u32 *target_size);
YueHaibing7f942082019-06-12 17:18:47 +08001557
1558bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1559 struct bpf_insn_access_aux *info);
1560
1561u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1562 const struct bpf_insn *si,
1563 struct bpf_insn *insn_buf,
1564 struct bpf_prog *prog,
1565 u32 *target_size);
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001566#else
1567static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1568 enum bpf_access_type type,
1569 struct bpf_insn_access_aux *info)
1570{
1571 return false;
1572}
1573
1574static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1575 const struct bpf_insn *si,
1576 struct bpf_insn *insn_buf,
1577 struct bpf_prog *prog,
1578 u32 *target_size)
1579{
1580 return 0;
1581}
YueHaibing7f942082019-06-12 17:18:47 +08001582static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1583 enum bpf_access_type type,
1584 struct bpf_insn_access_aux *info)
1585{
1586 return false;
1587}
1588
1589static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1590 const struct bpf_insn *si,
1591 struct bpf_insn *insn_buf,
1592 struct bpf_prog *prog,
1593 u32 *target_size)
1594{
1595 return 0;
1596}
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001597#endif /* CONFIG_INET */
1598
Alexei Starovoitov5964b202019-11-14 10:57:03 -08001599enum bpf_text_poke_type {
Daniel Borkmannb553a6e2019-11-24 01:39:42 +01001600 BPF_MOD_CALL,
1601 BPF_MOD_JUMP,
Alexei Starovoitov5964b202019-11-14 10:57:03 -08001602};
Daniel Borkmann4b3da772019-11-22 21:07:54 +01001603
Alexei Starovoitov5964b202019-11-14 10:57:03 -08001604int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1605 void *addr1, void *addr2);
1606
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001607#endif /* _LINUX_BPF_H */