blob: f9a506147c8a4410b122bb6db3525e1672262af3 [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;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +020019struct perf_event;
John Fastabend174a79f2017-08-15 22:32:47 -070020struct bpf_prog;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070021struct bpf_map;
John Fastabend4f738ad2018-03-18 12:57:10 -070022struct sock;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070023struct seq_file;
Roman Gushchin1b2b2342018-12-10 15:43:00 -080024struct btf;
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020025struct btf_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070026
27/* map is generic key/value storage optionally accesible by eBPF programs */
28struct bpf_map_ops {
29 /* funcs callable from userspace (via syscall) */
Jakub Kicinski1110f3a2018-01-11 20:29:03 -080030 int (*map_alloc_check)(union bpf_attr *attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070031 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +020032 void (*map_release)(struct bpf_map *map, struct file *map_file);
33 void (*map_free)(struct bpf_map *map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070034 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
John Fastabendba6b8de2018-04-23 15:39:23 -070035 void (*map_release_uref)(struct bpf_map *map);
Daniel Borkmannc6110222019-05-14 01:18:55 +020036 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070037
38 /* funcs callable from userspace and from eBPF programs */
39 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
Alexei Starovoitov3274f522014-11-13 17:36:44 -080040 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070041 int (*map_delete_elem)(struct bpf_map *map, void *key);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +020042 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
43 int (*map_pop_elem)(struct bpf_map *map, void *value);
44 int (*map_peek_elem)(struct bpf_map *map, void *value);
Wang Nan2a36f0b2015-08-06 07:02:33 +000045
46 /* funcs called by prog_array and perf_event_array map */
Daniel Borkmannd056a782016-06-15 22:47:13 +020047 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
48 int fd);
49 void (*map_fd_put_ptr)(void *ptr);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -070050 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070051 u32 (*map_fd_sys_lookup_elem)(void *ptr);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070052 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
53 struct seq_file *m);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020054 int (*map_check_btf)(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -080055 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020056 const struct btf_type *key_type,
57 const struct btf_type *value_type);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +020058
59 /* Direct value access helpers. */
60 int (*map_direct_value_addr)(const struct bpf_map *map,
61 u64 *imm, u32 off);
62 int (*map_direct_value_meta)(const struct bpf_map *map,
63 u64 imm, u32 *off);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070064};
65
Roman Gushchin3539b962019-05-29 18:03:57 -070066struct bpf_map_memory {
67 u32 pages;
68 struct user_struct *user;
69};
70
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070071struct bpf_map {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070072 /* The first two cachelines with read-mostly members of which some
Daniel Borkmannbe95a842018-01-09 13:17:44 +010073 * are also accessed in fast-path (e.g. ops, max_entries).
74 */
75 const struct bpf_map_ops *ops ____cacheline_aligned;
76 struct bpf_map *inner_map_meta;
77#ifdef CONFIG_SECURITY
78 void *security;
79#endif
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070080 enum bpf_map_type map_type;
81 u32 key_size;
82 u32 value_size;
83 u32 max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080084 u32 map_flags;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -080085 int spin_lock_off; /* >=0 valid offset, <0 error */
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070086 u32 id;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070087 int numa_node;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -070088 u32 btf_key_type_id;
89 u32 btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070090 struct btf *btf;
Roman Gushchin3539b962019-05-29 18:03:57 -070091 struct bpf_map_memory memory;
Alexei Starovoitovb2157392018-01-07 17:33:02 -080092 bool unpriv_array;
Daniel Borkmann87df15d2019-04-09 23:20:06 +020093 bool frozen; /* write-once */
94 /* 48 bytes hole */
Daniel Borkmannbe95a842018-01-09 13:17:44 +010095
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070096 /* The 3rd and 4th cacheline with misc members to avoid false sharing
Daniel Borkmannbe95a842018-01-09 13:17:44 +010097 * particularly with refcounting.
98 */
Roman Gushchin3539b962019-05-29 18:03:57 -070099 atomic_t refcnt ____cacheline_aligned;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100100 atomic_t usercnt;
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100101 struct work_struct work;
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700102 char name[BPF_OBJ_NAME_LEN];
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700103};
104
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800105static inline bool map_value_has_spin_lock(const struct bpf_map *map)
106{
107 return map->spin_lock_off >= 0;
108}
109
110static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
111{
112 if (likely(!map_value_has_spin_lock(map)))
113 return;
114 *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
115 (struct bpf_spin_lock){};
116}
117
118/* copy everything but bpf_spin_lock */
119static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
120{
121 if (unlikely(map_value_has_spin_lock(map))) {
122 u32 off = map->spin_lock_off;
123
124 memcpy(dst, src, off);
125 memcpy(dst + off + sizeof(struct bpf_spin_lock),
126 src + off + sizeof(struct bpf_spin_lock),
127 map->value_size - off - sizeof(struct bpf_spin_lock));
128 } else {
129 memcpy(dst, src, map->value_size);
130 }
131}
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800132void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
133 bool lock_src);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800134
Jakub Kicinski602144c2018-07-17 10:53:25 -0700135struct bpf_offload_dev;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800136struct bpf_offloaded_map;
137
138struct bpf_map_dev_ops {
139 int (*map_get_next_key)(struct bpf_offloaded_map *map,
140 void *key, void *next_key);
141 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
142 void *key, void *value);
143 int (*map_update_elem)(struct bpf_offloaded_map *map,
144 void *key, void *value, u64 flags);
145 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
146};
147
148struct bpf_offloaded_map {
149 struct bpf_map map;
150 struct net_device *netdev;
151 const struct bpf_map_dev_ops *dev_ops;
152 void *dev_priv;
153 struct list_head offloads;
154};
155
156static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
157{
158 return container_of(map, struct bpf_offloaded_map, map);
159}
160
Jakub Kicinski0cd3cbe2018-05-03 18:37:08 -0700161static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
162{
163 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
164}
165
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700166static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
167{
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200168 return map->btf && map->ops->map_seq_show_elem;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700169}
170
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200171int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800172 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200173 const struct btf_type *key_type,
174 const struct btf_type *value_type);
175
Jakub Kicinskia3884572018-01-11 20:29:09 -0800176extern const struct bpf_map_ops bpf_map_offload_ops;
177
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700178/* function argument constraints */
179enum bpf_arg_type {
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100180 ARG_DONTCARE = 0, /* unused argument in helper function */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700181
182 /* the following constraints used to prototype
183 * bpf_map_lookup/update/delete_elem() functions
184 */
185 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
186 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
187 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +0200188 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700189 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700190
191 /* the following constraints used to prototype bpf_memcmp() and other
192 * functions that access data on eBPF program stack
193 */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800194 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
Gianluca Borellodb1ac492017-11-22 18:32:53 +0000195 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800196 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
197 * helper function must fill all bytes or clear
198 * them in error case.
Daniel Borkmann435faee12016-04-13 00:10:51 +0200199 */
200
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800201 ARG_CONST_SIZE, /* number of bytes accessed from memory */
202 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100203
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700204 ARG_PTR_TO_CTX, /* pointer to context */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100205 ARG_ANYTHING, /* any (initialized) argument is ok */
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800206 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800207 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
Andrey Ignatov57c3bb72019-03-18 16:57:10 -0700208 ARG_PTR_TO_INT, /* pointer to int */
209 ARG_PTR_TO_LONG, /* pointer to long */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700210 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700211};
212
213/* type of values returned from helper functions */
214enum bpf_return_type {
215 RET_INTEGER, /* function returns integer */
216 RET_VOID, /* function doesn't return anything */
Roman Gushchin3e6a4b32018-08-02 14:27:22 -0700217 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700218 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
Joe Stringerc64b7982018-10-02 13:35:33 -0700219 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800220 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
Lorenz Bauer85a51f82019-03-22 09:54:00 +0800221 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700222};
223
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700224/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
225 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
226 * instructions after verifying
227 */
228struct bpf_func_proto {
229 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
230 bool gpl_only;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200231 bool pkt_access;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700232 enum bpf_return_type ret_type;
233 enum bpf_arg_type arg1_type;
234 enum bpf_arg_type arg2_type;
235 enum bpf_arg_type arg3_type;
236 enum bpf_arg_type arg4_type;
237 enum bpf_arg_type arg5_type;
238};
239
240/* bpf_context is intentionally undefined structure. Pointer to bpf_context is
241 * the first argument to eBPF programs.
242 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
243 */
244struct bpf_context;
245
246enum bpf_access_type {
247 BPF_READ = 1,
248 BPF_WRITE = 2
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700249};
250
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700251/* types of values stored in eBPF registers */
Edward Creef1174f72017-08-07 15:26:19 +0100252/* Pointer types represent:
253 * pointer
254 * pointer + imm
255 * pointer + (u16) var
256 * pointer + (u16) var + imm
257 * if (range > 0) then [ptr, ptr + range - off) is safe to access
258 * if (id > 0) means that some 'var' was added
259 * if (off > 0) means that 'imm' was added
260 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700261enum bpf_reg_type {
262 NOT_INIT = 0, /* nothing was written into register */
Edward Creef1174f72017-08-07 15:26:19 +0100263 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700264 PTR_TO_CTX, /* reg points to bpf_context */
265 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
266 PTR_TO_MAP_VALUE, /* reg points to map element value */
267 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
Edward Creef1174f72017-08-07 15:26:19 +0100268 PTR_TO_STACK, /* reg == frame_pointer + offset */
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200269 PTR_TO_PACKET_META, /* skb->data - meta_len */
Edward Creef1174f72017-08-07 15:26:19 +0100270 PTR_TO_PACKET, /* reg points to skb->data */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700271 PTR_TO_PACKET_END, /* skb->data + headlen */
Petar Penkovd58e4682018-09-14 07:46:18 -0700272 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
Joe Stringerc64b7982018-10-02 13:35:33 -0700273 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
274 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800275 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
276 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800277 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
278 PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
Matt Mullins9df1c282019-04-26 11:49:47 -0700279 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700280 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700281};
282
Yonghong Song23994632017-06-22 15:07:39 -0700283/* The information passed from prog-specific *_is_valid_access
284 * back to the verifier.
285 */
286struct bpf_insn_access_aux {
287 enum bpf_reg_type reg_type;
288 int ctx_field_size;
Yonghong Song23994632017-06-22 15:07:39 -0700289};
290
Daniel Borkmannf96da092017-07-02 02:13:27 +0200291static inline void
292bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
293{
294 aux->ctx_field_size = size;
295}
296
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700297struct bpf_prog_ops {
298 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
299 union bpf_attr __user *uattr);
300};
301
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700302struct bpf_verifier_ops {
303 /* return eBPF function prototype for verification */
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700304 const struct bpf_func_proto *
305 (*get_func_proto)(enum bpf_func_id func_id,
306 const struct bpf_prog *prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700307
308 /* return true if 'size' wide access at offset 'off' within bpf_context
309 * with 'type' (read or write) is allowed
310 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700311 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700312 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700313 struct bpf_insn_access_aux *info);
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200314 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
315 const struct bpf_prog *prog);
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +0200316 int (*gen_ld_abs)(const struct bpf_insn *orig,
317 struct bpf_insn *insn_buf);
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100318 u32 (*convert_ctx_access)(enum bpf_access_type type,
319 const struct bpf_insn *src,
320 struct bpf_insn *dst,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200321 struct bpf_prog *prog, u32 *target_size);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700322};
323
Jakub Kicinskicae19272017-12-27 18:39:05 -0800324struct bpf_prog_offload_ops {
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800325 /* verifier basic callbacks */
Jakub Kicinskicae19272017-12-27 18:39:05 -0800326 int (*insn_hook)(struct bpf_verifier_env *env,
327 int insn_idx, int prev_insn_idx);
Quentin Monnetc941ce92018-10-07 12:56:47 +0100328 int (*finalize)(struct bpf_verifier_env *env);
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800329 /* verifier optimization callbacks (called after .finalize) */
330 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
331 struct bpf_insn *insn);
332 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
333 /* program management callbacks */
Quentin Monnet16a8cb5c2018-11-09 13:03:32 +0000334 int (*prepare)(struct bpf_prog *prog);
335 int (*translate)(struct bpf_prog *prog);
Quentin Monneteb911942018-11-09 13:03:30 +0000336 void (*destroy)(struct bpf_prog *prog);
Jakub Kicinskicae19272017-12-27 18:39:05 -0800337};
338
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800339struct bpf_prog_offload {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700340 struct bpf_prog *prog;
341 struct net_device *netdev;
Quentin Monnet341b3e72018-11-09 13:03:26 +0000342 struct bpf_offload_dev *offdev;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700343 void *dev_priv;
344 struct list_head offloads;
345 bool dev_state;
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800346 bool opt_failed;
Jiong Wangfcfb1262018-01-16 16:05:19 -0800347 void *jited_image;
348 u32 jited_len;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700349};
350
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000351enum bpf_cgroup_storage_type {
352 BPF_CGROUP_STORAGE_SHARED,
Roman Gushchinb741f162018-09-28 14:45:43 +0000353 BPF_CGROUP_STORAGE_PERCPU,
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000354 __BPF_CGROUP_STORAGE_MAX
355};
356
357#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
358
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800359struct bpf_prog_stats {
360 u64 cnt;
361 u64 nsecs;
362 struct u64_stats_sync syncp;
363};
364
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700365struct bpf_prog_aux {
366 atomic_t refcnt;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100367 u32 used_map_cnt;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700368 u32 max_ctx_offset;
Jiong Wange6478152018-11-08 04:08:42 -0500369 u32 max_pkt_offset;
Matt Mullins9df1c282019-04-26 11:49:47 -0700370 u32 max_tp_access;
Alexei Starovoitov87266792017-05-30 13:31:29 -0700371 u32 stack_depth;
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700372 u32 id;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800373 u32 func_cnt; /* used by non-func prog as the number of func progs */
374 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
Jiong Wanga4b1d3c2019-05-24 23:25:15 +0100375 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800376 bool offload_requested;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800377 struct bpf_prog **func;
378 void *jit_data; /* JIT specific data. arch dependent */
Daniel Borkmann74451e662017-02-16 22:24:50 +0100379 struct latch_tree_node ksym_tnode;
380 struct list_head ksym_lnode;
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700381 const struct bpf_prog_ops *ops;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700382 struct bpf_map **used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700383 struct bpf_prog *prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700384 struct user_struct *user;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700385 u64 load_time; /* ns since boottime */
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000386 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700387 char name[BPF_OBJ_NAME_LEN];
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700388#ifdef CONFIG_SECURITY
389 void *security;
390#endif
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800391 struct bpf_prog_offload *offload;
Yonghong Song838e9692018-11-19 15:29:11 -0800392 struct btf *btf;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800393 struct bpf_func_info *func_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800394 /* bpf_line_info loaded from userspace. linfo->insn_off
395 * has the xlated insn offset.
396 * Both the main and sub prog share the same linfo.
397 * The subprog can access its first linfo by
398 * using the linfo_idx.
399 */
400 struct bpf_line_info *linfo;
401 /* jited_linfo is the jited addr of the linfo. It has a
402 * one to one mapping to linfo:
403 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
404 * Both the main and sub prog share the same jited_linfo.
405 * The subprog can access its first jited_linfo by
406 * using the linfo_idx.
407 */
408 void **jited_linfo;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800409 u32 func_info_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800410 u32 nr_linfo;
411 /* subprog can use linfo_idx to access its first linfo and
412 * jited_linfo.
413 * main prog always has linfo_idx == 0
414 */
415 u32 linfo_idx;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -0800416 struct bpf_prog_stats __percpu *stats;
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700417 union {
418 struct work_struct work;
419 struct rcu_head rcu;
420 };
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700421};
422
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700423struct bpf_array {
424 struct bpf_map map;
425 u32 elem_size;
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800426 u32 index_mask;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700427 /* 'ownership' of prog_array is claimed by the first program that
428 * is going to use this map or by the first program which FD is stored
429 * in the map to make sure that all callers and callees have the same
430 * prog_type and JITed flag
431 */
432 enum bpf_prog_type owner_prog_type;
433 bool owner_jited;
434 union {
435 char value[0] __aligned(8);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000436 void *ptrs[0] __aligned(8);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800437 void __percpu *pptrs[0] __aligned(8);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700438 };
439};
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200440
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -0700441#define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700442#define MAX_TAIL_CALL_CNT 32
443
Daniel Borkmann591fe982019-04-09 23:20:05 +0200444#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
445 BPF_F_RDONLY_PROG | \
446 BPF_F_WRONLY | \
447 BPF_F_WRONLY_PROG)
448
449#define BPF_MAP_CAN_READ BIT(0)
450#define BPF_MAP_CAN_WRITE BIT(1)
451
452static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
453{
454 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
455
456 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
457 * not possible.
458 */
459 if (access_flags & BPF_F_RDONLY_PROG)
460 return BPF_MAP_CAN_READ;
461 else if (access_flags & BPF_F_WRONLY_PROG)
462 return BPF_MAP_CAN_WRITE;
463 else
464 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
465}
466
467static inline bool bpf_map_flags_access_ok(u32 access_flags)
468{
469 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
470 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
471}
472
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200473struct bpf_event_entry {
474 struct perf_event *event;
475 struct file *perf_file;
476 struct file *map_file;
477 struct rcu_head rcu;
478};
479
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700480bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100481int bpf_prog_calc_tag(struct bpf_prog *fp);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200482
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700483const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200484
485typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +0200486 unsigned long off, unsigned long len);
Joe Stringerc64b7982018-10-02 13:35:33 -0700487typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
488 const struct bpf_insn *src,
489 struct bpf_insn *dst,
490 struct bpf_prog *prog,
491 u32 *target_size);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200492
493u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
494 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700495
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700496/* an array of programs to be executed under rcu_lock.
497 *
498 * Typical usage:
499 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
500 *
501 * the structure returned by bpf_prog_array_alloc() should be populated
502 * with program pointers and the last pointer must be NULL.
503 * The user has to keep refcnt on the program and make sure the program
504 * is removed from the array before bpf_prog_put().
505 * The 'struct bpf_prog_array *' should only be replaced with xchg()
506 * since other cpus are walking the array of pointers in parallel.
507 */
Roman Gushchin394e40a2018-08-02 14:27:21 -0700508struct bpf_prog_array_item {
509 struct bpf_prog *prog;
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000510 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Roman Gushchin394e40a2018-08-02 14:27:21 -0700511};
512
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700513struct bpf_prog_array {
514 struct rcu_head rcu;
Roman Gushchin394e40a2018-08-02 14:27:21 -0700515 struct bpf_prog_array_item items[0];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700516};
517
Roman Gushchind29ab6e2018-07-13 12:41:10 -0700518struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700519void bpf_prog_array_free(struct bpf_prog_array *progs);
520int bpf_prog_array_length(struct bpf_prog_array *progs);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -0700521bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700522int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700523 __u32 __user *prog_ids, u32 cnt);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700524
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700525void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700526 struct bpf_prog *old_prog);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700527int bpf_prog_array_copy_info(struct bpf_prog_array *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -0700528 u32 *prog_ids, u32 request_cnt,
529 u32 *prog_cnt);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -0700530int bpf_prog_array_copy(struct bpf_prog_array *old_array,
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700531 struct bpf_prog *exclude_prog,
532 struct bpf_prog *include_prog,
533 struct bpf_prog_array **new_array);
534
535#define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700536 ({ \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700537 struct bpf_prog_array_item *_item; \
538 struct bpf_prog *_prog; \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700539 struct bpf_prog_array *_array; \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700540 u32 _ret = 1; \
Roman Gushchin6899b322018-04-23 18:09:21 +0100541 preempt_disable(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700542 rcu_read_lock(); \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700543 _array = rcu_dereference(array); \
544 if (unlikely(check_non_null && !_array))\
545 goto _out; \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700546 _item = &_array->items[0]; \
547 while ((_prog = READ_ONCE(_item->prog))) { \
548 bpf_cgroup_storage_set(_item->cgroup_storage); \
549 _ret &= func(_prog, ctx); \
550 _item++; \
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700551 } \
552_out: \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700553 rcu_read_unlock(); \
Peter Zijlstra0edd6b62019-04-23 21:55:59 +0200554 preempt_enable(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700555 _ret; \
556 })
557
brakmo1f52f6c2019-05-28 16:59:35 -0700558/* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
559 * so BPF programs can request cwr for TCP packets.
560 *
561 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
562 * packet. This macro changes the behavior so the low order bit
563 * indicates whether the packet should be dropped (0) or not (1)
564 * and the next bit is a congestion notification bit. This could be
565 * used by TCP to call tcp_enter_cwr()
566 *
567 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
568 * 0: drop packet
569 * 1: keep packet
570 * 2: drop packet and cn
571 * 3: keep packet and cn
572 *
573 * This macro then converts it to one of the NET_XMIT or an error
574 * code that is then interpreted as drop packet (and no cn):
575 * 0: NET_XMIT_SUCCESS skb should be transmitted
576 * 1: NET_XMIT_DROP skb should be dropped and cn
577 * 2: NET_XMIT_CN skb should be transmitted and cn
578 * 3: -EPERM skb should be dropped
579 */
580#define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
581 ({ \
582 struct bpf_prog_array_item *_item; \
583 struct bpf_prog *_prog; \
584 struct bpf_prog_array *_array; \
585 u32 ret; \
586 u32 _ret = 1; \
587 u32 _cn = 0; \
588 preempt_disable(); \
589 rcu_read_lock(); \
590 _array = rcu_dereference(array); \
591 _item = &_array->items[0]; \
592 while ((_prog = READ_ONCE(_item->prog))) { \
593 bpf_cgroup_storage_set(_item->cgroup_storage); \
594 ret = func(_prog, ctx); \
595 _ret &= (ret & 1); \
596 _cn |= (ret & 2); \
597 _item++; \
598 } \
599 rcu_read_unlock(); \
600 preempt_enable(); \
601 if (_ret) \
602 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
603 else \
604 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
605 _ret; \
606 })
607
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700608#define BPF_PROG_RUN_ARRAY(array, ctx, func) \
609 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
610
611#define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
612 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
613
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800614#ifdef CONFIG_BPF_SYSCALL
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800615DECLARE_PER_CPU(int, bpf_prog_active);
616
Chenbo Fengf66e4482017-10-18 13:00:26 -0700617extern const struct file_operations bpf_map_fops;
618extern const struct file_operations bpf_prog_fops;
619
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700620#define BPF_PROG_TYPE(_id, _name) \
621 extern const struct bpf_prog_ops _name ## _prog_ops; \
622 extern const struct bpf_verifier_ops _name ## _verifier_ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200623#define BPF_MAP_TYPE(_id, _ops) \
624 extern const struct bpf_map_ops _ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200625#include <linux/bpf_types.h>
626#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200627#undef BPF_MAP_TYPE
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100628
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700629extern const struct bpf_prog_ops bpf_offload_prog_ops;
Jakub Kicinski4f9218a2017-10-16 16:40:55 -0700630extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
631extern const struct bpf_verifier_ops xdp_analyzer_ops;
632
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700633struct bpf_prog *bpf_prog_get(u32 ufd);
Jakub Kicinski248f3462017-11-03 13:56:20 -0700634struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -0800635 bool attach_drv);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100636struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog, int i);
Daniel Borkmannc5405942016-11-09 22:02:34 +0100637void bpf_prog_sub(struct bpf_prog *prog, int i);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100638struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog);
John Fastabenda6f6df62017-08-15 22:32:22 -0700639struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100640void bpf_prog_put(struct bpf_prog *prog);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100641int __bpf_prog_charge(struct user_struct *user, u32 pages);
642void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100643
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800644void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800645void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800646
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100647struct bpf_map *bpf_map_get_with_uref(u32 ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100648struct bpf_map *__bpf_map_get(struct fd f);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100649struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100650void bpf_map_put_with_uref(struct bpf_map *map);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100651void bpf_map_put(struct bpf_map *map);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700652int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
653void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
Roman Gushchinc85d6912019-05-29 18:03:59 -0700654int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700655void bpf_map_charge_finish(struct bpf_map_memory *mem);
656void bpf_map_charge_move(struct bpf_map_memory *dst,
657 struct bpf_map_memory *src);
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700658void *bpf_map_area_alloc(size_t size, int numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100659void bpf_map_area_free(void *base);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800660void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100661
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700662extern int sysctl_unprivileged_bpf_disabled;
663
Chenbo Feng6e71b042017-10-18 13:00:22 -0700664int bpf_map_new_fd(struct bpf_map *map, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100665int bpf_prog_new_fd(struct bpf_prog *prog);
666
667int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
Chenbo Feng6e71b042017-10-18 13:00:22 -0700668int bpf_obj_get_user(const char __user *pathname, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100669
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800670int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
671int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
672int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
673 u64 flags);
674int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
675 u64 flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200676
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800677int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800678
Daniel Borkmannd056a782016-06-15 22:47:13 +0200679int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
680 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700681int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700682int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
683 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700684int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200685
Chenbo Feng6e71b042017-10-18 13:00:22 -0700686int bpf_get_file_flag(int flags);
Martin KaFai Laudcab51f2018-05-22 15:03:31 -0700687int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
688 size_t actual_size);
Chenbo Feng6e71b042017-10-18 13:00:22 -0700689
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800690/* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
691 * forced to use 'long' read/writes to try to atomically copy long counters.
692 * Best-effort only. No barriers here, since it _will_ race with concurrent
693 * updates from BPF programs. Called from bpf syscall and mostly used with
694 * size 8 or 16 bytes, so ask compiler to inline it.
695 */
696static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
697{
698 const long *lsrc = src;
699 long *ldst = dst;
700
701 size /= sizeof(long);
702 while (size--)
703 *ldst++ = *lsrc++;
704}
705
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100706/* verify correctness of eBPF program */
Yonghong Song838e9692018-11-19 15:29:11 -0800707int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
708 union bpf_attr __user *uattr);
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800709void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
John Fastabend46f55cf2017-07-17 21:56:48 -0700710
711/* Map specifics */
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200712struct xdp_buff;
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900713struct sk_buff;
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200714
715struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200716struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
John Fastabend46f55cf2017-07-17 21:56:48 -0700717void __dev_map_flush(struct bpf_map *map);
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200718int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
719 struct net_device *dev_rx);
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900720int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
721 struct bpf_prog *xdp_prog);
John Fastabend46f55cf2017-07-17 21:56:48 -0700722
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200723struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200724void __cpu_map_flush(struct bpf_map *map);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200725int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
726 struct net_device *dev_rx);
727
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700728/* Return map's numa specified by userspace */
729static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
730{
731 return (attr->map_flags & BPF_F_NUMA_NODE) ?
732 attr->numa_node : NUMA_NO_NODE;
733}
734
Al Viro040ee692017-12-02 20:20:38 -0500735struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700736int array_map_alloc_check(union bpf_attr *attr);
Al Viro040ee692017-12-02 20:20:38 -0500737
Stanislav Fomichevc6958652019-04-11 09:12:02 -0700738int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
739 union bpf_attr __user *uattr);
740int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
741 union bpf_attr __user *uattr);
742int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
743 const union bpf_attr *kattr,
744 union bpf_attr __user *uattr);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200745#else /* !CONFIG_BPF_SYSCALL */
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100746static inline struct bpf_prog *bpf_prog_get(u32 ufd)
747{
748 return ERR_PTR(-EOPNOTSUPP);
749}
750
Jakub Kicinski248f3462017-11-03 13:56:20 -0700751static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
752 enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -0800753 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -0700754{
755 return ERR_PTR(-EOPNOTSUPP);
756}
757
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100758static inline struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog,
759 int i)
Brenden Blancocc2e0b32016-07-20 07:55:52 -0700760{
761 return ERR_PTR(-EOPNOTSUPP);
762}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200763
Daniel Borkmannc5405942016-11-09 22:02:34 +0100764static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
765{
766}
767
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100768static inline void bpf_prog_put(struct bpf_prog *prog)
769{
770}
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100771
772static inline struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -0700773{
774 return ERR_PTR(-EOPNOTSUPP);
775}
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100776
John Fastabenda6f6df62017-08-15 22:32:22 -0700777static inline struct bpf_prog *__must_check
778bpf_prog_inc_not_zero(struct bpf_prog *prog)
779{
780 return ERR_PTR(-EOPNOTSUPP);
781}
782
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100783static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
784{
785 return 0;
786}
787
788static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
789{
790}
John Fastabend46f55cf2017-07-17 21:56:48 -0700791
Chenbo Feng6e71b042017-10-18 13:00:22 -0700792static inline int bpf_obj_get_user(const char __user *pathname, int flags)
Shmulik Ladkani98589a02017-10-09 15:27:15 +0300793{
794 return -EOPNOTSUPP;
795}
796
John Fastabend46f55cf2017-07-17 21:56:48 -0700797static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
798 u32 key)
799{
800 return NULL;
801}
802
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200803static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
804 u32 key)
805{
806 return NULL;
807}
808
John Fastabend46f55cf2017-07-17 21:56:48 -0700809static inline void __dev_map_flush(struct bpf_map *map)
810{
811}
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200812
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200813struct xdp_buff;
814struct bpf_dtab_netdev;
815
816static inline
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200817int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
818 struct net_device *dev_rx)
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200819{
820 return 0;
821}
822
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900823struct sk_buff;
824
825static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
826 struct sk_buff *skb,
827 struct bpf_prog *xdp_prog)
828{
829 return 0;
830}
831
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200832static inline
833struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
834{
835 return NULL;
836}
837
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200838static inline void __cpu_map_flush(struct bpf_map *map)
839{
840}
841
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200842static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
843 struct xdp_buff *xdp,
844 struct net_device *dev_rx)
845{
846 return 0;
847}
Al Viro040ee692017-12-02 20:20:38 -0500848
849static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
850 enum bpf_prog_type type)
851{
852 return ERR_PTR(-EOPNOTSUPP);
853}
Stanislav Fomichevc6958652019-04-11 09:12:02 -0700854
855static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
856 const union bpf_attr *kattr,
857 union bpf_attr __user *uattr)
858{
859 return -ENOTSUPP;
860}
861
862static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
863 const union bpf_attr *kattr,
864 union bpf_attr __user *uattr)
865{
866 return -ENOTSUPP;
867}
868
869static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
870 const union bpf_attr *kattr,
871 union bpf_attr __user *uattr)
872{
873 return -ENOTSUPP;
874}
Daniel Borkmann61e021f32015-03-02 15:21:55 +0100875#endif /* CONFIG_BPF_SYSCALL */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700876
Jakub Kicinski479321e2017-11-20 15:21:56 -0800877static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
878 enum bpf_prog_type type)
879{
880 return bpf_prog_get_type_dev(ufd, type, false);
881}
882
Al Viro040ee692017-12-02 20:20:38 -0500883bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
884
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700885int bpf_prog_offload_compile(struct bpf_prog *prog);
886void bpf_prog_offload_destroy(struct bpf_prog *prog);
Jakub Kicinski675fc272017-12-27 18:39:09 -0800887int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
888 struct bpf_prog *prog);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700889
Jakub Kicinski52775b32018-01-17 19:13:28 -0800890int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
891
Jakub Kicinskia3884572018-01-11 20:29:09 -0800892int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
893int bpf_map_offload_update_elem(struct bpf_map *map,
894 void *key, void *value, u64 flags);
895int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
896int bpf_map_offload_get_next_key(struct bpf_map *map,
897 void *key, void *next_key);
898
Jakub Kicinski09728262018-07-17 10:53:23 -0700899bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800900
Quentin Monnet1385d752018-11-09 13:03:25 +0000901struct bpf_offload_dev *
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -0800902bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
Jakub Kicinski602144c2018-07-17 10:53:25 -0700903void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -0800904void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
Jakub Kicinski602144c2018-07-17 10:53:25 -0700905int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
906 struct net_device *netdev);
907void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
908 struct net_device *netdev);
Jakub Kicinskifd4f2272018-07-17 10:53:26 -0700909bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700910
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700911#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
912int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
913
Jakub Kicinski0d830032018-05-08 19:37:06 -0700914static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700915{
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800916 return aux->offload_requested;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700917}
Jakub Kicinskia3884572018-01-11 20:29:09 -0800918
919static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
920{
921 return unlikely(map->ops == &bpf_map_offload_ops);
922}
923
924struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
925void bpf_map_offload_map_free(struct bpf_map *map);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700926#else
927static inline int bpf_prog_offload_init(struct bpf_prog *prog,
928 union bpf_attr *attr)
929{
930 return -EOPNOTSUPP;
931}
932
933static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
934{
935 return false;
936}
Jakub Kicinskia3884572018-01-11 20:29:09 -0800937
938static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
939{
940 return false;
941}
942
943static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
944{
945 return ERR_PTR(-EOPNOTSUPP);
946}
947
948static inline void bpf_map_offload_map_free(struct bpf_map *map)
949{
950}
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700951#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
952
Daniel Borkmann604326b2018-10-13 02:45:58 +0200953#if defined(CONFIG_BPF_STREAM_PARSER)
954int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
955int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
John Fastabend6bdc9c42017-08-16 15:02:32 -0700956#else
Daniel Borkmann604326b2018-10-13 02:45:58 +0200957static inline int sock_map_prog_update(struct bpf_map *map,
958 struct bpf_prog *prog, u32 which)
John Fastabend464bc0f2017-08-28 07:10:04 -0700959{
960 return -EOPNOTSUPP;
961}
Sean Youngfdb5c452018-06-19 00:04:24 +0100962
Daniel Borkmann604326b2018-10-13 02:45:58 +0200963static inline int sock_map_get_from_fd(const union bpf_attr *attr,
964 struct bpf_prog *prog)
Sean Youngfdb5c452018-06-19 00:04:24 +0100965{
966 return -EINVAL;
967}
John Fastabend6bdc9c42017-08-16 15:02:32 -0700968#endif
969
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200970#if defined(CONFIG_XDP_SOCKETS)
971struct xdp_sock;
972struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key);
973int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
974 struct xdp_sock *xs);
975void __xsk_map_flush(struct bpf_map *map);
976#else
977struct xdp_sock;
978static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
979 u32 key)
980{
981 return NULL;
982}
983
984static inline int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
985 struct xdp_sock *xs)
986{
987 return -EOPNOTSUPP;
988}
989
990static inline void __xsk_map_flush(struct bpf_map *map)
991{
992}
993#endif
994
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700995#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
996void bpf_sk_reuseport_detach(struct sock *sk);
997int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
998 void *value);
999int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1000 void *value, u64 map_flags);
1001#else
1002static inline void bpf_sk_reuseport_detach(struct sock *sk)
1003{
1004}
1005
1006#ifdef CONFIG_BPF_SYSCALL
1007static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1008 void *key, void *value)
1009{
1010 return -EOPNOTSUPP;
1011}
1012
1013static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1014 void *key, void *value,
1015 u64 map_flags)
1016{
1017 return -EOPNOTSUPP;
1018}
1019#endif /* CONFIG_BPF_SYSCALL */
1020#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1021
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001022/* verifier prototypes for helper functions called from eBPF programs */
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01001023extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1024extern const struct bpf_func_proto bpf_map_update_elem_proto;
1025extern const struct bpf_func_proto bpf_map_delete_elem_proto;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02001026extern const struct bpf_func_proto bpf_map_push_elem_proto;
1027extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1028extern const struct bpf_func_proto bpf_map_peek_elem_proto;
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001029
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001030extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001031extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001032extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001033extern const struct bpf_func_proto bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001034extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001035extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1036extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1037extern const struct bpf_func_proto bpf_get_current_comm_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08001038extern const struct bpf_func_proto bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -07001039extern const struct bpf_func_proto bpf_get_stack_proto;
John Fastabend174a79f2017-08-15 22:32:47 -07001040extern const struct bpf_func_proto bpf_sock_map_update_proto;
John Fastabend81110382018-05-14 10:00:17 -07001041extern const struct bpf_func_proto bpf_sock_hash_update_proto;
Yonghong Songbf6fa2c82018-06-03 15:59:41 -07001042extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
Daniel Borkmann604326b2018-10-13 02:45:58 +02001043extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1044extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1045extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1046extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001047extern const struct bpf_func_proto bpf_spin_lock_proto;
1048extern const struct bpf_func_proto bpf_spin_unlock_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07001049extern const struct bpf_func_proto bpf_get_local_storage_proto;
Andrey Ignatovd7a4cb92019-03-18 17:55:26 -07001050extern const struct bpf_func_proto bpf_strtol_proto;
1051extern const struct bpf_func_proto bpf_strtoul_proto;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001052extern const struct bpf_func_proto bpf_tcp_sock_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07001053
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001054/* Shared helpers among cBPF and eBPF. */
1055void bpf_user_rnd_init_once(void);
1056u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1057
Joe Stringerc64b7982018-10-02 13:35:33 -07001058#if defined(CONFIG_NET)
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001059bool bpf_sock_common_is_valid_access(int off, int size,
1060 enum bpf_access_type type,
1061 struct bpf_insn_access_aux *info);
Joe Stringerc64b7982018-10-02 13:35:33 -07001062bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1063 struct bpf_insn_access_aux *info);
1064u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1065 const struct bpf_insn *si,
1066 struct bpf_insn *insn_buf,
1067 struct bpf_prog *prog,
1068 u32 *target_size);
1069#else
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001070static inline bool bpf_sock_common_is_valid_access(int off, int size,
1071 enum bpf_access_type type,
1072 struct bpf_insn_access_aux *info)
1073{
1074 return false;
1075}
Joe Stringerc64b7982018-10-02 13:35:33 -07001076static inline bool bpf_sock_is_valid_access(int off, int size,
1077 enum bpf_access_type type,
1078 struct bpf_insn_access_aux *info)
1079{
1080 return false;
1081}
1082static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1083 const struct bpf_insn *si,
1084 struct bpf_insn *insn_buf,
1085 struct bpf_prog *prog,
1086 u32 *target_size)
1087{
1088 return 0;
1089}
1090#endif
1091
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001092#ifdef CONFIG_INET
1093bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1094 struct bpf_insn_access_aux *info);
1095
1096u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1097 const struct bpf_insn *si,
1098 struct bpf_insn *insn_buf,
1099 struct bpf_prog *prog,
1100 u32 *target_size);
YueHaibing7f942082019-06-12 17:18:47 +08001101
1102bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1103 struct bpf_insn_access_aux *info);
1104
1105u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1106 const struct bpf_insn *si,
1107 struct bpf_insn *insn_buf,
1108 struct bpf_prog *prog,
1109 u32 *target_size);
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001110#else
1111static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1112 enum bpf_access_type type,
1113 struct bpf_insn_access_aux *info)
1114{
1115 return false;
1116}
1117
1118static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1119 const struct bpf_insn *si,
1120 struct bpf_insn *insn_buf,
1121 struct bpf_prog *prog,
1122 u32 *target_size)
1123{
1124 return 0;
1125}
YueHaibing7f942082019-06-12 17:18:47 +08001126static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1127 enum bpf_access_type type,
1128 struct bpf_insn_access_aux *info)
1129{
1130 return false;
1131}
1132
1133static inline u32 bpf_xdp_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)
1138{
1139 return 0;
1140}
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001141#endif /* CONFIG_INET */
1142
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001143#endif /* _LINUX_BPF_H */