blob: 171be30fe0ae35e4f512e40b59c2789cd85f3b84 [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>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070015#include <linux/wait.h>
Alexei Starovoitov492ecee2019-02-25 14:28:39 -080016#include <linux/u64_stats_sync.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070017
Jakub Kicinskicae19272017-12-27 18:39:05 -080018struct bpf_verifier_env;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -070019struct bpf_verifier_log;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +020020struct perf_event;
John Fastabend174a79f2017-08-15 22:32:47 -070021struct bpf_prog;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070022struct bpf_map;
John Fastabend4f738ad2018-03-18 12:57:10 -070023struct sock;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070024struct seq_file;
Roman Gushchin1b2b2342018-12-10 15:43:00 -080025struct btf;
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020026struct btf_type;
Alexei Starovoitov3dec5412019-10-15 20:25:03 -070027struct exception_table_entry;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070028
Quentin Monnet1b9ed842019-08-20 10:31:50 +010029extern struct idr btf_idr;
30extern spinlock_t btf_idr_lock;
31
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070032/* map is generic key/value storage optionally accesible by eBPF programs */
33struct bpf_map_ops {
34 /* funcs callable from userspace (via syscall) */
Jakub Kicinski1110f3a2018-01-11 20:29:03 -080035 int (*map_alloc_check)(union bpf_attr *attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070036 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +020037 void (*map_release)(struct bpf_map *map, struct file *map_file);
38 void (*map_free)(struct bpf_map *map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070039 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
John Fastabendba6b8de2018-04-23 15:39:23 -070040 void (*map_release_uref)(struct bpf_map *map);
Daniel Borkmannc6110222019-05-14 01:18:55 +020041 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070042
43 /* funcs callable from userspace and from eBPF programs */
44 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
Alexei Starovoitov3274f522014-11-13 17:36:44 -080045 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070046 int (*map_delete_elem)(struct bpf_map *map, void *key);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +020047 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
48 int (*map_pop_elem)(struct bpf_map *map, void *value);
49 int (*map_peek_elem)(struct bpf_map *map, void *value);
Wang Nan2a36f0b2015-08-06 07:02:33 +000050
51 /* funcs called by prog_array and perf_event_array map */
Daniel Borkmannd056a782016-06-15 22:47:13 +020052 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
53 int fd);
54 void (*map_fd_put_ptr)(void *ptr);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -070055 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070056 u32 (*map_fd_sys_lookup_elem)(void *ptr);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070057 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
58 struct seq_file *m);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020059 int (*map_check_btf)(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -080060 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020061 const struct btf_type *key_type,
62 const struct btf_type *value_type);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +020063
64 /* Direct value access helpers. */
65 int (*map_direct_value_addr)(const struct bpf_map *map,
66 u64 *imm, u32 off);
67 int (*map_direct_value_meta)(const struct bpf_map *map,
68 u64 imm, u32 *off);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070069};
70
Roman Gushchin3539b962019-05-29 18:03:57 -070071struct bpf_map_memory {
72 u32 pages;
73 struct user_struct *user;
74};
75
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070076struct bpf_map {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070077 /* The first two cachelines with read-mostly members of which some
Daniel Borkmannbe95a842018-01-09 13:17:44 +010078 * are also accessed in fast-path (e.g. ops, max_entries).
79 */
80 const struct bpf_map_ops *ops ____cacheline_aligned;
81 struct bpf_map *inner_map_meta;
82#ifdef CONFIG_SECURITY
83 void *security;
84#endif
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070085 enum bpf_map_type map_type;
86 u32 key_size;
87 u32 value_size;
88 u32 max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080089 u32 map_flags;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -080090 int spin_lock_off; /* >=0 valid offset, <0 error */
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070091 u32 id;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070092 int numa_node;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -070093 u32 btf_key_type_id;
94 u32 btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070095 struct btf *btf;
Roman Gushchin3539b962019-05-29 18:03:57 -070096 struct bpf_map_memory memory;
Alexei Starovoitovb2157392018-01-07 17:33:02 -080097 bool unpriv_array;
Daniel Borkmann87df15d2019-04-09 23:20:06 +020098 bool frozen; /* write-once */
99 /* 48 bytes hole */
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100100
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700101 /* The 3rd and 4th cacheline with misc members to avoid false sharing
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100102 * particularly with refcounting.
103 */
Roman Gushchin3539b962019-05-29 18:03:57 -0700104 atomic_t refcnt ____cacheline_aligned;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100105 atomic_t usercnt;
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100106 struct work_struct work;
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700107 char name[BPF_OBJ_NAME_LEN];
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108};
109
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800110static inline bool map_value_has_spin_lock(const struct bpf_map *map)
111{
112 return map->spin_lock_off >= 0;
113}
114
115static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
116{
117 if (likely(!map_value_has_spin_lock(map)))
118 return;
119 *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
120 (struct bpf_spin_lock){};
121}
122
123/* copy everything but bpf_spin_lock */
124static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
125{
126 if (unlikely(map_value_has_spin_lock(map))) {
127 u32 off = map->spin_lock_off;
128
129 memcpy(dst, src, off);
130 memcpy(dst + off + sizeof(struct bpf_spin_lock),
131 src + off + sizeof(struct bpf_spin_lock),
132 map->value_size - off - sizeof(struct bpf_spin_lock));
133 } else {
134 memcpy(dst, src, map->value_size);
135 }
136}
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800137void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
138 bool lock_src);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800139
Jakub Kicinski602144c2018-07-17 10:53:25 -0700140struct bpf_offload_dev;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800141struct bpf_offloaded_map;
142
143struct bpf_map_dev_ops {
144 int (*map_get_next_key)(struct bpf_offloaded_map *map,
145 void *key, void *next_key);
146 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
147 void *key, void *value);
148 int (*map_update_elem)(struct bpf_offloaded_map *map,
149 void *key, void *value, u64 flags);
150 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
151};
152
153struct bpf_offloaded_map {
154 struct bpf_map map;
155 struct net_device *netdev;
156 const struct bpf_map_dev_ops *dev_ops;
157 void *dev_priv;
158 struct list_head offloads;
159};
160
161static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
162{
163 return container_of(map, struct bpf_offloaded_map, map);
164}
165
Jakub Kicinski0cd3cbe2018-05-03 18:37:08 -0700166static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
167{
168 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
169}
170
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700171static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
172{
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200173 return map->btf && map->ops->map_seq_show_elem;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700174}
175
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200176int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800177 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200178 const struct btf_type *key_type,
179 const struct btf_type *value_type);
180
Jakub Kicinskia3884572018-01-11 20:29:09 -0800181extern const struct bpf_map_ops bpf_map_offload_ops;
182
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700183/* function argument constraints */
184enum bpf_arg_type {
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100185 ARG_DONTCARE = 0, /* unused argument in helper function */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700186
187 /* the following constraints used to prototype
188 * bpf_map_lookup/update/delete_elem() functions
189 */
190 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
191 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
192 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +0200193 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700194 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700195
196 /* the following constraints used to prototype bpf_memcmp() and other
197 * functions that access data on eBPF program stack
198 */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800199 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
Gianluca Borellodb1ac492017-11-22 18:32:53 +0000200 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800201 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
202 * helper function must fill all bytes or clear
203 * them in error case.
Daniel Borkmann435faee12016-04-13 00:10:51 +0200204 */
205
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800206 ARG_CONST_SIZE, /* number of bytes accessed from memory */
207 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100208
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700209 ARG_PTR_TO_CTX, /* pointer to context */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100210 ARG_ANYTHING, /* any (initialized) argument is ok */
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800211 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800212 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
Andrey Ignatov57c3bb72019-03-18 16:57:10 -0700213 ARG_PTR_TO_INT, /* pointer to int */
214 ARG_PTR_TO_LONG, /* pointer to long */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700215 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
Alexei Starovoitova7658e12019-10-15 20:25:04 -0700216 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700217};
218
219/* type of values returned from helper functions */
220enum bpf_return_type {
221 RET_INTEGER, /* function returns integer */
222 RET_VOID, /* function doesn't return anything */
Roman Gushchin3e6a4b32018-08-02 14:27:22 -0700223 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700224 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
Joe Stringerc64b7982018-10-02 13:35:33 -0700225 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800226 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
Lorenz Bauer85a51f82019-03-22 09:54:00 +0800227 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700228};
229
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700230/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
231 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
232 * instructions after verifying
233 */
234struct bpf_func_proto {
235 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
236 bool gpl_only;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200237 bool pkt_access;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700238 enum bpf_return_type ret_type;
Alexei Starovoitova7658e12019-10-15 20:25:04 -0700239 union {
240 struct {
241 enum bpf_arg_type arg1_type;
242 enum bpf_arg_type arg2_type;
243 enum bpf_arg_type arg3_type;
244 enum bpf_arg_type arg4_type;
245 enum bpf_arg_type arg5_type;
246 };
247 enum bpf_arg_type arg_type[5];
248 };
249 u32 *btf_id; /* BTF ids of arguments */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700250};
251
252/* bpf_context is intentionally undefined structure. Pointer to bpf_context is
253 * the first argument to eBPF programs.
254 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
255 */
256struct bpf_context;
257
258enum bpf_access_type {
259 BPF_READ = 1,
260 BPF_WRITE = 2
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700261};
262
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700263/* types of values stored in eBPF registers */
Edward Creef1174f72017-08-07 15:26:19 +0100264/* Pointer types represent:
265 * pointer
266 * pointer + imm
267 * pointer + (u16) var
268 * pointer + (u16) var + imm
269 * if (range > 0) then [ptr, ptr + range - off) is safe to access
270 * if (id > 0) means that some 'var' was added
271 * if (off > 0) means that 'imm' was added
272 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700273enum bpf_reg_type {
274 NOT_INIT = 0, /* nothing was written into register */
Edward Creef1174f72017-08-07 15:26:19 +0100275 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700276 PTR_TO_CTX, /* reg points to bpf_context */
277 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
278 PTR_TO_MAP_VALUE, /* reg points to map element value */
279 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
Edward Creef1174f72017-08-07 15:26:19 +0100280 PTR_TO_STACK, /* reg == frame_pointer + offset */
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200281 PTR_TO_PACKET_META, /* skb->data - meta_len */
Edward Creef1174f72017-08-07 15:26:19 +0100282 PTR_TO_PACKET, /* reg points to skb->data */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700283 PTR_TO_PACKET_END, /* skb->data + headlen */
Petar Penkovd58e4682018-09-14 07:46:18 -0700284 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
Joe Stringerc64b7982018-10-02 13:35:33 -0700285 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
286 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800287 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
288 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800289 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
290 PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
Matt Mullins9df1c282019-04-26 11:49:47 -0700291 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700292 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700293 PTR_TO_BTF_ID, /* reg points to kernel struct */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700294};
295
Yonghong Song23994632017-06-22 15:07:39 -0700296/* The information passed from prog-specific *_is_valid_access
297 * back to the verifier.
298 */
299struct bpf_insn_access_aux {
300 enum bpf_reg_type reg_type;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700301 union {
302 int ctx_field_size;
303 u32 btf_id;
304 };
305 struct bpf_verifier_log *log; /* for verbose logs */
Yonghong Song23994632017-06-22 15:07:39 -0700306};
307
Daniel Borkmannf96da092017-07-02 02:13:27 +0200308static inline void
309bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
310{
311 aux->ctx_field_size = size;
312}
313
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700314struct bpf_prog_ops {
315 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
316 union bpf_attr __user *uattr);
317};
318
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700319struct bpf_verifier_ops {
320 /* return eBPF function prototype for verification */
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700321 const struct bpf_func_proto *
322 (*get_func_proto)(enum bpf_func_id func_id,
323 const struct bpf_prog *prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700324
325 /* return true if 'size' wide access at offset 'off' within bpf_context
326 * with 'type' (read or write) is allowed
327 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700328 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700329 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700330 struct bpf_insn_access_aux *info);
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200331 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
332 const struct bpf_prog *prog);
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +0200333 int (*gen_ld_abs)(const struct bpf_insn *orig,
334 struct bpf_insn *insn_buf);
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100335 u32 (*convert_ctx_access)(enum bpf_access_type type,
336 const struct bpf_insn *src,
337 struct bpf_insn *dst,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200338 struct bpf_prog *prog, u32 *target_size);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700339};
340
Jakub Kicinskicae19272017-12-27 18:39:05 -0800341struct bpf_prog_offload_ops {
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800342 /* verifier basic callbacks */
Jakub Kicinskicae19272017-12-27 18:39:05 -0800343 int (*insn_hook)(struct bpf_verifier_env *env,
344 int insn_idx, int prev_insn_idx);
Quentin Monnetc941ce92018-10-07 12:56:47 +0100345 int (*finalize)(struct bpf_verifier_env *env);
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800346 /* verifier optimization callbacks (called after .finalize) */
347 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
348 struct bpf_insn *insn);
349 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
350 /* program management callbacks */
Quentin Monnet16a8cb5c2018-11-09 13:03:32 +0000351 int (*prepare)(struct bpf_prog *prog);
352 int (*translate)(struct bpf_prog *prog);
Quentin Monneteb911942018-11-09 13:03:30 +0000353 void (*destroy)(struct bpf_prog *prog);
Jakub Kicinskicae19272017-12-27 18:39:05 -0800354};
355
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800356struct bpf_prog_offload {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700357 struct bpf_prog *prog;
358 struct net_device *netdev;
Quentin Monnet341b3e72018-11-09 13:03:26 +0000359 struct bpf_offload_dev *offdev;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700360 void *dev_priv;
361 struct list_head offloads;
362 bool dev_state;
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800363 bool opt_failed;
Jiong Wangfcfb1262018-01-16 16:05:19 -0800364 void *jited_image;
365 u32 jited_len;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700366};
367
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000368enum bpf_cgroup_storage_type {
369 BPF_CGROUP_STORAGE_SHARED,
Roman Gushchinb741f162018-09-28 14:45:43 +0000370 BPF_CGROUP_STORAGE_PERCPU,
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000371 __BPF_CGROUP_STORAGE_MAX
372};
373
374#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
375
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800376struct bpf_prog_stats {
377 u64 cnt;
378 u64 nsecs;
379 struct u64_stats_sync syncp;
Eric Dumazet84a081f2019-10-11 11:11:40 -0700380} __aligned(2 * sizeof(u64));
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800381
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700382struct bpf_prog_aux {
383 atomic_t refcnt;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100384 u32 used_map_cnt;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700385 u32 max_ctx_offset;
Jiong Wange6478152018-11-08 04:08:42 -0500386 u32 max_pkt_offset;
Matt Mullins9df1c282019-04-26 11:49:47 -0700387 u32 max_tp_access;
Alexei Starovoitov87266792017-05-30 13:31:29 -0700388 u32 stack_depth;
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700389 u32 id;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800390 u32 func_cnt; /* used by non-func prog as the number of func progs */
391 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
Alexei Starovoitovccfe29eb22019-10-15 20:24:58 -0700392 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
Jiong Wanga4b1d3c2019-05-24 23:25:15 +0100393 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800394 bool offload_requested;
Martin KaFai Lau38207292019-10-24 17:18:11 -0700395 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
396 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
397 const struct btf_type *attach_func_proto;
398 /* function name for valid attach_btf_id */
399 const char *attach_func_name;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800400 struct bpf_prog **func;
401 void *jit_data; /* JIT specific data. arch dependent */
Daniel Borkmann74451e662017-02-16 22:24:50 +0100402 struct latch_tree_node ksym_tnode;
403 struct list_head ksym_lnode;
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700404 const struct bpf_prog_ops *ops;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700405 struct bpf_map **used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700406 struct bpf_prog *prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700407 struct user_struct *user;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700408 u64 load_time; /* ns since boottime */
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000409 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700410 char name[BPF_OBJ_NAME_LEN];
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700411#ifdef CONFIG_SECURITY
412 void *security;
413#endif
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800414 struct bpf_prog_offload *offload;
Yonghong Song838e9692018-11-19 15:29:11 -0800415 struct btf *btf;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800416 struct bpf_func_info *func_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800417 /* bpf_line_info loaded from userspace. linfo->insn_off
418 * has the xlated insn offset.
419 * Both the main and sub prog share the same linfo.
420 * The subprog can access its first linfo by
421 * using the linfo_idx.
422 */
423 struct bpf_line_info *linfo;
424 /* jited_linfo is the jited addr of the linfo. It has a
425 * one to one mapping to linfo:
426 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
427 * Both the main and sub prog share the same jited_linfo.
428 * The subprog can access its first jited_linfo by
429 * using the linfo_idx.
430 */
431 void **jited_linfo;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800432 u32 func_info_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800433 u32 nr_linfo;
434 /* subprog can use linfo_idx to access its first linfo and
435 * jited_linfo.
436 * main prog always has linfo_idx == 0
437 */
438 u32 linfo_idx;
Alexei Starovoitov3dec5412019-10-15 20:25:03 -0700439 u32 num_exentries;
440 struct exception_table_entry *extable;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800441 struct bpf_prog_stats __percpu *stats;
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700442 union {
443 struct work_struct work;
444 struct rcu_head rcu;
445 };
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700446};
447
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700448struct bpf_array {
449 struct bpf_map map;
450 u32 elem_size;
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800451 u32 index_mask;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700452 /* 'ownership' of prog_array is claimed by the first program that
453 * is going to use this map or by the first program which FD is stored
454 * in the map to make sure that all callers and callees have the same
455 * prog_type and JITed flag
456 */
457 enum bpf_prog_type owner_prog_type;
458 bool owner_jited;
459 union {
460 char value[0] __aligned(8);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000461 void *ptrs[0] __aligned(8);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800462 void __percpu *pptrs[0] __aligned(8);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700463 };
464};
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200465
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -0700466#define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700467#define MAX_TAIL_CALL_CNT 32
468
Daniel Borkmann591fe982019-04-09 23:20:05 +0200469#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
470 BPF_F_RDONLY_PROG | \
471 BPF_F_WRONLY | \
472 BPF_F_WRONLY_PROG)
473
474#define BPF_MAP_CAN_READ BIT(0)
475#define BPF_MAP_CAN_WRITE BIT(1)
476
477static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
478{
479 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
480
481 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
482 * not possible.
483 */
484 if (access_flags & BPF_F_RDONLY_PROG)
485 return BPF_MAP_CAN_READ;
486 else if (access_flags & BPF_F_WRONLY_PROG)
487 return BPF_MAP_CAN_WRITE;
488 else
489 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
490}
491
492static inline bool bpf_map_flags_access_ok(u32 access_flags)
493{
494 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
495 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
496}
497
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200498struct bpf_event_entry {
499 struct perf_event *event;
500 struct file *perf_file;
501 struct file *map_file;
502 struct rcu_head rcu;
503};
504
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700505bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100506int bpf_prog_calc_tag(struct bpf_prog *fp);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700507const char *kernel_type_name(u32 btf_type_id);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200508
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700509const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200510
511typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +0200512 unsigned long off, unsigned long len);
Joe Stringerc64b7982018-10-02 13:35:33 -0700513typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
514 const struct bpf_insn *src,
515 struct bpf_insn *dst,
516 struct bpf_prog *prog,
517 u32 *target_size);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200518
519u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
520 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700521
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700522/* an array of programs to be executed under rcu_lock.
523 *
524 * Typical usage:
525 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
526 *
527 * the structure returned by bpf_prog_array_alloc() should be populated
528 * with program pointers and the last pointer must be NULL.
529 * The user has to keep refcnt on the program and make sure the program
530 * is removed from the array before bpf_prog_put().
531 * The 'struct bpf_prog_array *' should only be replaced with xchg()
532 * since other cpus are walking the array of pointers in parallel.
533 */
Roman Gushchin394e40a2018-08-02 14:27:21 -0700534struct bpf_prog_array_item {
535 struct bpf_prog *prog;
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000536 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Roman Gushchin394e40a2018-08-02 14:27:21 -0700537};
538
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700539struct bpf_prog_array {
540 struct rcu_head rcu;
Roman Gushchin394e40a2018-08-02 14:27:21 -0700541 struct bpf_prog_array_item items[0];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700542};
543
Roman Gushchind29ab6e2018-07-13 12:41:10 -0700544struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700545void bpf_prog_array_free(struct bpf_prog_array *progs);
546int bpf_prog_array_length(struct bpf_prog_array *progs);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -0700547bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700548int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700549 __u32 __user *prog_ids, u32 cnt);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700550
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700551void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700552 struct bpf_prog *old_prog);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700553int bpf_prog_array_copy_info(struct bpf_prog_array *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -0700554 u32 *prog_ids, u32 request_cnt,
555 u32 *prog_cnt);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700556int bpf_prog_array_copy(struct bpf_prog_array *old_array,
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700557 struct bpf_prog *exclude_prog,
558 struct bpf_prog *include_prog,
559 struct bpf_prog_array **new_array);
560
561#define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700562 ({ \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700563 struct bpf_prog_array_item *_item; \
564 struct bpf_prog *_prog; \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700565 struct bpf_prog_array *_array; \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700566 u32 _ret = 1; \
Roman Gushchin6899b322018-04-23 18:09:21 +0100567 preempt_disable(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700568 rcu_read_lock(); \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700569 _array = rcu_dereference(array); \
570 if (unlikely(check_non_null && !_array))\
571 goto _out; \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700572 _item = &_array->items[0]; \
573 while ((_prog = READ_ONCE(_item->prog))) { \
574 bpf_cgroup_storage_set(_item->cgroup_storage); \
575 _ret &= func(_prog, ctx); \
576 _item++; \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700577 } \
578_out: \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700579 rcu_read_unlock(); \
Peter Zijlstra0edd6b62019-04-23 21:55:59 +0200580 preempt_enable(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700581 _ret; \
582 })
583
brakmo1f52f6c2019-05-28 16:59:35 -0700584/* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
585 * so BPF programs can request cwr for TCP packets.
586 *
587 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
588 * packet. This macro changes the behavior so the low order bit
589 * indicates whether the packet should be dropped (0) or not (1)
590 * and the next bit is a congestion notification bit. This could be
591 * used by TCP to call tcp_enter_cwr()
592 *
593 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
594 * 0: drop packet
595 * 1: keep packet
596 * 2: drop packet and cn
597 * 3: keep packet and cn
598 *
599 * This macro then converts it to one of the NET_XMIT or an error
600 * code that is then interpreted as drop packet (and no cn):
601 * 0: NET_XMIT_SUCCESS skb should be transmitted
602 * 1: NET_XMIT_DROP skb should be dropped and cn
603 * 2: NET_XMIT_CN skb should be transmitted and cn
604 * 3: -EPERM skb should be dropped
605 */
606#define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
607 ({ \
608 struct bpf_prog_array_item *_item; \
609 struct bpf_prog *_prog; \
610 struct bpf_prog_array *_array; \
611 u32 ret; \
612 u32 _ret = 1; \
613 u32 _cn = 0; \
614 preempt_disable(); \
615 rcu_read_lock(); \
616 _array = rcu_dereference(array); \
617 _item = &_array->items[0]; \
618 while ((_prog = READ_ONCE(_item->prog))) { \
619 bpf_cgroup_storage_set(_item->cgroup_storage); \
620 ret = func(_prog, ctx); \
621 _ret &= (ret & 1); \
622 _cn |= (ret & 2); \
623 _item++; \
624 } \
625 rcu_read_unlock(); \
626 preempt_enable(); \
627 if (_ret) \
628 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
629 else \
630 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
631 _ret; \
632 })
633
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700634#define BPF_PROG_RUN_ARRAY(array, ctx, func) \
635 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
636
637#define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
638 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
639
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800640#ifdef CONFIG_BPF_SYSCALL
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800641DECLARE_PER_CPU(int, bpf_prog_active);
642
Chenbo Fengf66e4482017-10-18 13:00:26 -0700643extern const struct file_operations bpf_map_fops;
644extern const struct file_operations bpf_prog_fops;
645
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700646#define BPF_PROG_TYPE(_id, _name) \
647 extern const struct bpf_prog_ops _name ## _prog_ops; \
648 extern const struct bpf_verifier_ops _name ## _verifier_ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200649#define BPF_MAP_TYPE(_id, _ops) \
650 extern const struct bpf_map_ops _ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200651#include <linux/bpf_types.h>
652#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200653#undef BPF_MAP_TYPE
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100654
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700655extern const struct bpf_prog_ops bpf_offload_prog_ops;
Jakub Kicinski4f9218a2017-10-16 16:40:55 -0700656extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
657extern const struct bpf_verifier_ops xdp_analyzer_ops;
658
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700659struct bpf_prog *bpf_prog_get(u32 ufd);
Jakub Kicinski248f3462017-11-03 13:56:20 -0700660struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -0800661 bool attach_drv);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100662struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog, int i);
Daniel Borkmannc5405942016-11-09 22:02:34 +0100663void bpf_prog_sub(struct bpf_prog *prog, int i);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100664struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog);
John Fastabenda6f6df62017-08-15 22:32:22 -0700665struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100666void bpf_prog_put(struct bpf_prog *prog);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100667int __bpf_prog_charge(struct user_struct *user, u32 pages);
668void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100669
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800670void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800671void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800672
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100673struct bpf_map *bpf_map_get_with_uref(u32 ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100674struct bpf_map *__bpf_map_get(struct fd f);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100675struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
Stanislav Fomichevb0e4701c2019-08-14 10:37:48 -0700676struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map,
677 bool uref);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100678void bpf_map_put_with_uref(struct bpf_map *map);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100679void bpf_map_put(struct bpf_map *map);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700680int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
681void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
Roman Gushchinc85d6912019-05-29 18:03:59 -0700682int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700683void bpf_map_charge_finish(struct bpf_map_memory *mem);
684void bpf_map_charge_move(struct bpf_map_memory *dst,
685 struct bpf_map_memory *src);
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700686void *bpf_map_area_alloc(size_t size, int numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100687void bpf_map_area_free(void *base);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800688void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100689
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700690extern int sysctl_unprivileged_bpf_disabled;
691
Chenbo Feng6e71b042017-10-18 13:00:22 -0700692int bpf_map_new_fd(struct bpf_map *map, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100693int bpf_prog_new_fd(struct bpf_prog *prog);
694
695int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
Chenbo Feng6e71b042017-10-18 13:00:22 -0700696int bpf_obj_get_user(const char __user *pathname, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100697
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800698int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
699int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
700int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
701 u64 flags);
702int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
703 u64 flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200704
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800705int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800706
Daniel Borkmannd056a782016-06-15 22:47:13 +0200707int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
708 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700709int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700710int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
711 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700712int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200713
Chenbo Feng6e71b042017-10-18 13:00:22 -0700714int bpf_get_file_flag(int flags);
Martin KaFai Laudcab51f2018-05-22 15:03:31 -0700715int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
716 size_t actual_size);
Chenbo Feng6e71b042017-10-18 13:00:22 -0700717
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800718/* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
719 * forced to use 'long' read/writes to try to atomically copy long counters.
720 * Best-effort only. No barriers here, since it _will_ race with concurrent
721 * updates from BPF programs. Called from bpf syscall and mostly used with
722 * size 8 or 16 bytes, so ask compiler to inline it.
723 */
724static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
725{
726 const long *lsrc = src;
727 long *ldst = dst;
728
729 size /= sizeof(long);
730 while (size--)
731 *ldst++ = *lsrc++;
732}
733
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100734/* verify correctness of eBPF program */
Yonghong Song838e9692018-11-19 15:29:11 -0800735int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
736 union bpf_attr __user *uattr);
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800737void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
John Fastabend46f55cf2017-07-17 21:56:48 -0700738
739/* Map specifics */
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200740struct xdp_buff;
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900741struct sk_buff;
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200742
743struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200744struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
John Fastabend46f55cf2017-07-17 21:56:48 -0700745void __dev_map_flush(struct bpf_map *map);
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200746int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
747 struct net_device *dev_rx);
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900748int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
749 struct bpf_prog *xdp_prog);
John Fastabend46f55cf2017-07-17 21:56:48 -0700750
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200751struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200752void __cpu_map_flush(struct bpf_map *map);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200753int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
754 struct net_device *dev_rx);
755
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700756/* Return map's numa specified by userspace */
757static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
758{
759 return (attr->map_flags & BPF_F_NUMA_NODE) ?
760 attr->numa_node : NUMA_NO_NODE;
761}
762
Al Viro040ee692017-12-02 20:20:38 -0500763struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700764int array_map_alloc_check(union bpf_attr *attr);
Al Viro040ee692017-12-02 20:20:38 -0500765
Stanislav Fomichevc6958652019-04-11 09:12:02 -0700766int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
767 union bpf_attr __user *uattr);
768int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
769 union bpf_attr __user *uattr);
770int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
771 const union bpf_attr *kattr,
772 union bpf_attr __user *uattr);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700773bool btf_ctx_access(int off, int size, enum bpf_access_type type,
774 const struct bpf_prog *prog,
775 struct bpf_insn_access_aux *info);
776int btf_struct_access(struct bpf_verifier_log *log,
777 const struct btf_type *t, int off, int size,
778 enum bpf_access_type atype,
779 u32 *next_btf_id);
Alexei Starovoitova7658e12019-10-15 20:25:04 -0700780u32 btf_resolve_helper_id(struct bpf_verifier_log *log, void *, int);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700781
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200782#else /* !CONFIG_BPF_SYSCALL */
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100783static inline struct bpf_prog *bpf_prog_get(u32 ufd)
784{
785 return ERR_PTR(-EOPNOTSUPP);
786}
787
Jakub Kicinski248f3462017-11-03 13:56:20 -0700788static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
789 enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -0800790 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -0700791{
792 return ERR_PTR(-EOPNOTSUPP);
793}
794
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100795static inline struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog,
796 int i)
Brenden Blancocc2e0b32016-07-20 07:55:52 -0700797{
798 return ERR_PTR(-EOPNOTSUPP);
799}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200800
Daniel Borkmannc5405942016-11-09 22:02:34 +0100801static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
802{
803}
804
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100805static inline void bpf_prog_put(struct bpf_prog *prog)
806{
807}
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100808
809static inline struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -0700810{
811 return ERR_PTR(-EOPNOTSUPP);
812}
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100813
John Fastabenda6f6df62017-08-15 22:32:22 -0700814static inline struct bpf_prog *__must_check
815bpf_prog_inc_not_zero(struct bpf_prog *prog)
816{
817 return ERR_PTR(-EOPNOTSUPP);
818}
819
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100820static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
821{
822 return 0;
823}
824
825static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
826{
827}
John Fastabend46f55cf2017-07-17 21:56:48 -0700828
Chenbo Feng6e71b042017-10-18 13:00:22 -0700829static inline int bpf_obj_get_user(const char __user *pathname, int flags)
Shmulik Ladkani98589a02017-10-09 15:27:15 +0300830{
831 return -EOPNOTSUPP;
832}
833
John Fastabend46f55cf2017-07-17 21:56:48 -0700834static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
835 u32 key)
836{
837 return NULL;
838}
839
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200840static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
841 u32 key)
842{
843 return NULL;
844}
845
John Fastabend46f55cf2017-07-17 21:56:48 -0700846static inline void __dev_map_flush(struct bpf_map *map)
847{
848}
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200849
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200850struct xdp_buff;
851struct bpf_dtab_netdev;
852
853static inline
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200854int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
855 struct net_device *dev_rx)
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200856{
857 return 0;
858}
859
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900860struct sk_buff;
861
862static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
863 struct sk_buff *skb,
864 struct bpf_prog *xdp_prog)
865{
866 return 0;
867}
868
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200869static inline
870struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
871{
872 return NULL;
873}
874
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200875static inline void __cpu_map_flush(struct bpf_map *map)
876{
877}
878
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200879static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
880 struct xdp_buff *xdp,
881 struct net_device *dev_rx)
882{
883 return 0;
884}
Al Viro040ee692017-12-02 20:20:38 -0500885
886static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
887 enum bpf_prog_type type)
888{
889 return ERR_PTR(-EOPNOTSUPP);
890}
Stanislav Fomichevc6958652019-04-11 09:12:02 -0700891
892static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
893 const union bpf_attr *kattr,
894 union bpf_attr __user *uattr)
895{
896 return -ENOTSUPP;
897}
898
899static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
900 const union bpf_attr *kattr,
901 union bpf_attr __user *uattr)
902{
903 return -ENOTSUPP;
904}
905
906static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
907 const union bpf_attr *kattr,
908 union bpf_attr __user *uattr)
909{
910 return -ENOTSUPP;
911}
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100912#endif /* CONFIG_BPF_SYSCALL */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700913
Jakub Kicinski479321e2017-11-20 15:21:56 -0800914static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
915 enum bpf_prog_type type)
916{
917 return bpf_prog_get_type_dev(ufd, type, false);
918}
919
Al Viro040ee692017-12-02 20:20:38 -0500920bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
921
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700922int bpf_prog_offload_compile(struct bpf_prog *prog);
923void bpf_prog_offload_destroy(struct bpf_prog *prog);
Jakub Kicinski675fc272017-12-27 18:39:09 -0800924int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
925 struct bpf_prog *prog);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700926
Jakub Kicinski52775b32018-01-17 19:13:28 -0800927int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
928
Jakub Kicinskia3884572018-01-11 20:29:09 -0800929int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
930int bpf_map_offload_update_elem(struct bpf_map *map,
931 void *key, void *value, u64 flags);
932int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
933int bpf_map_offload_get_next_key(struct bpf_map *map,
934 void *key, void *next_key);
935
Jakub Kicinski09728262018-07-17 10:53:23 -0700936bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800937
Quentin Monnet1385d752018-11-09 13:03:25 +0000938struct bpf_offload_dev *
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -0800939bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
Jakub Kicinski602144c2018-07-17 10:53:25 -0700940void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -0800941void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
Jakub Kicinski602144c2018-07-17 10:53:25 -0700942int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
943 struct net_device *netdev);
944void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
945 struct net_device *netdev);
Jakub Kicinskifd4f2272018-07-17 10:53:26 -0700946bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700947
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700948#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
949int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
950
Jakub Kicinski0d830032018-05-08 19:37:06 -0700951static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700952{
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800953 return aux->offload_requested;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700954}
Jakub Kicinskia3884572018-01-11 20:29:09 -0800955
956static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
957{
958 return unlikely(map->ops == &bpf_map_offload_ops);
959}
960
961struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
962void bpf_map_offload_map_free(struct bpf_map *map);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700963#else
964static inline int bpf_prog_offload_init(struct bpf_prog *prog,
965 union bpf_attr *attr)
966{
967 return -EOPNOTSUPP;
968}
969
970static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
971{
972 return false;
973}
Jakub Kicinskia3884572018-01-11 20:29:09 -0800974
975static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
976{
977 return false;
978}
979
980static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
981{
982 return ERR_PTR(-EOPNOTSUPP);
983}
984
985static inline void bpf_map_offload_map_free(struct bpf_map *map)
986{
987}
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700988#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
989
Daniel Borkmann604326b2018-10-13 02:45:58 +0200990#if defined(CONFIG_BPF_STREAM_PARSER)
991int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
992int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
John Fastabend6bdc9c42017-08-16 15:02:32 -0700993#else
Daniel Borkmann604326b2018-10-13 02:45:58 +0200994static inline int sock_map_prog_update(struct bpf_map *map,
995 struct bpf_prog *prog, u32 which)
John Fastabend464bc0f2017-08-28 07:10:04 -0700996{
997 return -EOPNOTSUPP;
998}
Sean Youngfdb5c452018-06-19 00:04:24 +0100999
Daniel Borkmann604326b2018-10-13 02:45:58 +02001000static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1001 struct bpf_prog *prog)
Sean Youngfdb5c452018-06-19 00:04:24 +01001002{
1003 return -EINVAL;
1004}
John Fastabend6bdc9c42017-08-16 15:02:32 -07001005#endif
1006
Björn Töpelfbfc504a2018-05-02 13:01:28 +02001007#if defined(CONFIG_XDP_SOCKETS)
1008struct xdp_sock;
1009struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key);
1010int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
1011 struct xdp_sock *xs);
1012void __xsk_map_flush(struct bpf_map *map);
1013#else
1014struct xdp_sock;
1015static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
1016 u32 key)
1017{
1018 return NULL;
1019}
1020
1021static inline int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
1022 struct xdp_sock *xs)
1023{
1024 return -EOPNOTSUPP;
1025}
1026
1027static inline void __xsk_map_flush(struct bpf_map *map)
1028{
1029}
1030#endif
1031
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -07001032#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1033void bpf_sk_reuseport_detach(struct sock *sk);
1034int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1035 void *value);
1036int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1037 void *value, u64 map_flags);
1038#else
1039static inline void bpf_sk_reuseport_detach(struct sock *sk)
1040{
1041}
1042
1043#ifdef CONFIG_BPF_SYSCALL
1044static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1045 void *key, void *value)
1046{
1047 return -EOPNOTSUPP;
1048}
1049
1050static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1051 void *key, void *value,
1052 u64 map_flags)
1053{
1054 return -EOPNOTSUPP;
1055}
1056#endif /* CONFIG_BPF_SYSCALL */
1057#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1058
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001059/* verifier prototypes for helper functions called from eBPF programs */
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01001060extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1061extern const struct bpf_func_proto bpf_map_update_elem_proto;
1062extern const struct bpf_func_proto bpf_map_delete_elem_proto;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02001063extern const struct bpf_func_proto bpf_map_push_elem_proto;
1064extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1065extern const struct bpf_func_proto bpf_map_peek_elem_proto;
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001066
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001067extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001068extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001069extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001070extern const struct bpf_func_proto bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001071extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001072extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1073extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1074extern const struct bpf_func_proto bpf_get_current_comm_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08001075extern const struct bpf_func_proto bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -07001076extern const struct bpf_func_proto bpf_get_stack_proto;
John Fastabend174a79f2017-08-15 22:32:47 -07001077extern const struct bpf_func_proto bpf_sock_map_update_proto;
John Fastabend81110382018-05-14 10:00:17 -07001078extern const struct bpf_func_proto bpf_sock_hash_update_proto;
Yonghong Songbf6fa2c82018-06-03 15:59:41 -07001079extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
Daniel Borkmann604326b2018-10-13 02:45:58 +02001080extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1081extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1082extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1083extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001084extern const struct bpf_func_proto bpf_spin_lock_proto;
1085extern const struct bpf_func_proto bpf_spin_unlock_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07001086extern const struct bpf_func_proto bpf_get_local_storage_proto;
Andrey Ignatovd7a4cb92019-03-18 17:55:26 -07001087extern const struct bpf_func_proto bpf_strtol_proto;
1088extern const struct bpf_func_proto bpf_strtoul_proto;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001089extern const struct bpf_func_proto bpf_tcp_sock_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07001090
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001091/* Shared helpers among cBPF and eBPF. */
1092void bpf_user_rnd_init_once(void);
1093u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1094
Joe Stringerc64b7982018-10-02 13:35:33 -07001095#if defined(CONFIG_NET)
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001096bool bpf_sock_common_is_valid_access(int off, int size,
1097 enum bpf_access_type type,
1098 struct bpf_insn_access_aux *info);
Joe Stringerc64b7982018-10-02 13:35:33 -07001099bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1100 struct bpf_insn_access_aux *info);
1101u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1102 const struct bpf_insn *si,
1103 struct bpf_insn *insn_buf,
1104 struct bpf_prog *prog,
1105 u32 *target_size);
1106#else
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001107static inline bool bpf_sock_common_is_valid_access(int off, int size,
1108 enum bpf_access_type type,
1109 struct bpf_insn_access_aux *info)
1110{
1111 return false;
1112}
Joe Stringerc64b7982018-10-02 13:35:33 -07001113static inline bool bpf_sock_is_valid_access(int off, int size,
1114 enum bpf_access_type type,
1115 struct bpf_insn_access_aux *info)
1116{
1117 return false;
1118}
1119static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1120 const struct bpf_insn *si,
1121 struct bpf_insn *insn_buf,
1122 struct bpf_prog *prog,
1123 u32 *target_size)
1124{
1125 return 0;
1126}
1127#endif
1128
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001129#ifdef CONFIG_INET
1130bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1131 struct bpf_insn_access_aux *info);
1132
1133u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1134 const struct bpf_insn *si,
1135 struct bpf_insn *insn_buf,
1136 struct bpf_prog *prog,
1137 u32 *target_size);
YueHaibing7f942082019-06-12 17:18:47 +08001138
1139bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1140 struct bpf_insn_access_aux *info);
1141
1142u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1143 const struct bpf_insn *si,
1144 struct bpf_insn *insn_buf,
1145 struct bpf_prog *prog,
1146 u32 *target_size);
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001147#else
1148static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1149 enum bpf_access_type type,
1150 struct bpf_insn_access_aux *info)
1151{
1152 return false;
1153}
1154
1155static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1156 const struct bpf_insn *si,
1157 struct bpf_insn *insn_buf,
1158 struct bpf_prog *prog,
1159 u32 *target_size)
1160{
1161 return 0;
1162}
YueHaibing7f942082019-06-12 17:18:47 +08001163static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1164 enum bpf_access_type type,
1165 struct bpf_insn_access_aux *info)
1166{
1167 return false;
1168}
1169
1170static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1171 const struct bpf_insn *si,
1172 struct bpf_insn *insn_buf,
1173 struct bpf_prog *prog,
1174 u32 *target_size)
1175{
1176 return 0;
1177}
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001178#endif /* CONFIG_INET */
1179
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001180#endif /* _LINUX_BPF_H */