blob: be8d57e6e78a21a82ac309cf29bf27b109999b51 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#ifndef _LINUX_BPF_H
5#define _LINUX_BPF_H 1
6
7#include <uapi/linux/bpf.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +01008
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07009#include <linux/workqueue.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070010#include <linux/file.h>
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080011#include <linux/percpu.h>
Zi Shen Lim002245c2016-06-08 21:18:47 -070012#include <linux/err.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +010013#include <linux/rbtree_latch.h>
David S. Millerd6e1e462017-08-19 23:34:03 -070014#include <linux/numa.h>
Andrii Nakryikofc970222019-11-17 09:28:04 -080015#include <linux/mm_types.h>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070016#include <linux/wait.h>
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080017#include <linux/refcount.h>
18#include <linux/mutex.h>
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080019#include <linux/module.h>
Jiri Olsabfea9a82020-03-12 20:55:59 +010020#include <linux/kallsyms.h>
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -070021#include <linux/capability.h>
Roman Gushchin48edc1f2020-12-01 13:58:32 -080022#include <linux/sched/mm.h>
23#include <linux/slab.h>
Alexei Starovoitove21aa342021-03-16 14:00:07 -070024#include <linux/percpu-refcount.h>
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -070025#include <linux/bpfptr.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070026
Jakub Kicinskicae19272017-12-27 18:39:05 -080027struct bpf_verifier_env;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -070028struct bpf_verifier_log;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +020029struct perf_event;
John Fastabend174a79f2017-08-15 22:32:47 -070030struct bpf_prog;
Daniel Borkmannda765a22019-11-22 21:07:58 +010031struct bpf_prog_aux;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070032struct bpf_map;
John Fastabend4f738ad2018-03-18 12:57:10 -070033struct sock;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070034struct seq_file;
Roman Gushchin1b2b2342018-12-10 15:43:00 -080035struct btf;
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020036struct btf_type;
Alexei Starovoitov3dec5412019-10-15 20:25:03 -070037struct exception_table_entry;
Yonghong Songae243452020-05-09 10:58:59 -070038struct seq_operations;
Yonghong Songf9c79272020-07-23 11:41:10 -070039struct bpf_iter_aux_info;
KP Singhf836a562020-08-25 20:29:15 +020040struct bpf_local_storage;
41struct bpf_local_storage_map;
Andrii Nakryiko36e68442020-11-09 17:19:31 -080042struct kobject;
Roman Gushchin48edc1f2020-12-01 13:58:32 -080043struct mem_cgroup;
Jiri Olsa861de022021-03-26 11:59:00 +010044struct module;
Yonghong Song69c087b2021-02-26 12:49:25 -080045struct bpf_func_state;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070046
Quentin Monnet1b9ed842019-08-20 10:31:50 +010047extern struct idr btf_idr;
48extern spinlock_t btf_idr_lock;
Andrii Nakryiko36e68442020-11-09 17:19:31 -080049extern struct kobject *btf_kobj;
Quentin Monnet1b9ed842019-08-20 10:31:50 +010050
Yonghong Songf9c79272020-07-23 11:41:10 -070051typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
52 struct bpf_iter_aux_info *aux);
Yonghong Song14fc6bd62020-07-23 11:41:09 -070053typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
54struct bpf_iter_seq_info {
55 const struct seq_operations *seq_ops;
56 bpf_iter_init_seq_priv_t init_seq_private;
57 bpf_iter_fini_seq_priv_t fini_seq_private;
58 u32 seq_priv_size;
59};
60
Ricardo Ribalda5d903492021-03-18 21:22:22 +010061/* map is generic key/value storage optionally accessible by eBPF programs */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070062struct bpf_map_ops {
63 /* funcs callable from userspace (via syscall) */
Jakub Kicinski1110f3a2018-01-11 20:29:03 -080064 int (*map_alloc_check)(union bpf_attr *attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070065 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +020066 void (*map_release)(struct bpf_map *map, struct file *map_file);
67 void (*map_free)(struct bpf_map *map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070068 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
John Fastabendba6b8de2018-04-23 15:39:23 -070069 void (*map_release_uref)(struct bpf_map *map);
Daniel Borkmannc6110222019-05-14 01:18:55 +020070 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -080071 int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
72 union bpf_attr __user *uattr);
Denis Salopek3e87f192021-05-11 23:00:04 +020073 int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
74 void *value, u64 flags);
Yonghong Song05799632020-01-15 10:43:04 -080075 int (*map_lookup_and_delete_batch)(struct bpf_map *map,
76 const union bpf_attr *attr,
77 union bpf_attr __user *uattr);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -080078 int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
79 union bpf_attr __user *uattr);
80 int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
81 union bpf_attr __user *uattr);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070082
83 /* funcs callable from userspace and from eBPF programs */
84 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
Alexei Starovoitov3274f522014-11-13 17:36:44 -080085 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070086 int (*map_delete_elem)(struct bpf_map *map, void *key);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +020087 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
88 int (*map_pop_elem)(struct bpf_map *map, void *value);
89 int (*map_peek_elem)(struct bpf_map *map, void *value);
Wang Nan2a36f0b2015-08-06 07:02:33 +000090
91 /* funcs called by prog_array and perf_event_array map */
Daniel Borkmannd056a782016-06-15 22:47:13 +020092 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
93 int fd);
94 void (*map_fd_put_ptr)(void *ptr);
Daniel Borkmann4a8f87e2020-10-11 01:40:03 +020095 int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070096 u32 (*map_fd_sys_lookup_elem)(void *ptr);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070097 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
98 struct seq_file *m);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020099 int (*map_check_btf)(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800100 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200101 const struct btf_type *key_type,
102 const struct btf_type *value_type);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +0200103
Daniel Borkmannda765a22019-11-22 21:07:58 +0100104 /* Prog poke tracking helpers. */
105 int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
106 void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
107 void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
108 struct bpf_prog *new);
109
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +0200110 /* Direct value access helpers. */
111 int (*map_direct_value_addr)(const struct bpf_map *map,
112 u64 *imm, u32 off);
113 int (*map_direct_value_meta)(const struct bpf_map *map,
114 u64 imm, u32 *off);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800115 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
Andrii Nakryiko457f44362020-05-29 00:54:20 -0700116 __poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
117 struct poll_table_struct *pts);
Andrey Ignatov41c48f32020-06-19 14:11:43 -0700118
KP Singhf836a562020-08-25 20:29:15 +0200119 /* Functions called by bpf_local_storage maps */
120 int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
121 void *owner, u32 size);
122 void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
123 void *owner, u32 size);
124 struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
Martin KaFai Lauf4d05252020-08-27 18:18:06 -0700125
Björn Töpele6a47502021-03-08 12:29:06 +0100126 /* Misc helpers.*/
127 int (*map_redirect)(struct bpf_map *map, u32 ifindex, u64 flags);
128
Martin KaFai Lauf4d05252020-08-27 18:18:06 -0700129 /* map_meta_equal must be implemented for maps that can be
130 * used as an inner map. It is a runtime check to ensure
131 * an inner map can be inserted to an outer map.
132 *
133 * Some properties of the inner map has been used during the
134 * verification time. When inserting an inner map at the runtime,
135 * map_meta_equal has to ensure the inserting map has the same
136 * properties that the verifier has used earlier.
137 */
138 bool (*map_meta_equal)(const struct bpf_map *meta0,
139 const struct bpf_map *meta1);
140
Yonghong Song69c087b2021-02-26 12:49:25 -0800141
142 int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
143 struct bpf_func_state *caller,
144 struct bpf_func_state *callee);
145 int (*map_for_each_callback)(struct bpf_map *map, void *callback_fn,
146 void *callback_ctx, u64 flags);
147
Andrey Ignatov41c48f32020-06-19 14:11:43 -0700148 /* BTF name and id of struct allocated by map_alloc */
149 const char * const map_btf_name;
150 int *map_btf_id;
Yonghong Songa5cbe052020-07-23 11:41:12 -0700151
152 /* bpf_iter info used to open a seq_file */
153 const struct bpf_iter_seq_info *iter_seq_info;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700154};
155
156struct bpf_map {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700157 /* The first two cachelines with read-mostly members of which some
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100158 * are also accessed in fast-path (e.g. ops, max_entries).
159 */
160 const struct bpf_map_ops *ops ____cacheline_aligned;
161 struct bpf_map *inner_map_meta;
162#ifdef CONFIG_SECURITY
163 void *security;
164#endif
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700165 enum bpf_map_type map_type;
166 u32 key_size;
167 u32 value_size;
168 u32 max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800169 u32 map_flags;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800170 int spin_lock_off; /* >=0 valid offset, <0 error */
Alexei Starovoitovb00628b2021-07-14 17:54:09 -0700171 int timer_off; /* >=0 valid offset, <0 error */
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700172 u32 id;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700173 int numa_node;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700174 u32 btf_key_type_id;
175 u32 btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700176 struct btf *btf;
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800177#ifdef CONFIG_MEMCG_KMEM
178 struct mem_cgroup *memcg;
179#endif
Andrii Nakryikofc970222019-11-17 09:28:04 -0800180 char name[BPF_OBJ_NAME_LEN];
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800181 u32 btf_vmlinux_value_type_id;
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -0700182 bool bypass_spec_v1;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800183 bool frozen; /* write-once; write-protected by freeze_mutex */
184 /* 22 bytes hole */
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100185
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700186 /* The 3rd and 4th cacheline with misc members to avoid false sharing
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100187 * particularly with refcounting.
188 */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800189 atomic64_t refcnt ____cacheline_aligned;
190 atomic64_t usercnt;
Daniel Borkmannbe95a842018-01-09 13:17:44 +0100191 struct work_struct work;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800192 struct mutex freeze_mutex;
193 u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700194};
195
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800196static inline bool map_value_has_spin_lock(const struct bpf_map *map)
197{
198 return map->spin_lock_off >= 0;
199}
200
Alexei Starovoitov68134662021-07-14 17:54:10 -0700201static inline bool map_value_has_timer(const struct bpf_map *map)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800202{
Alexei Starovoitov68134662021-07-14 17:54:10 -0700203 return map->timer_off >= 0;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800204}
205
Alexei Starovoitov68134662021-07-14 17:54:10 -0700206static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
207{
208 if (unlikely(map_value_has_spin_lock(map)))
209 *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
210 (struct bpf_spin_lock){};
211 if (unlikely(map_value_has_timer(map)))
212 *(struct bpf_timer *)(dst + map->timer_off) =
213 (struct bpf_timer){};
214}
215
216/* copy everything but bpf_spin_lock and bpf_timer. There could be one of each. */
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800217static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
218{
Alexei Starovoitov68134662021-07-14 17:54:10 -0700219 u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800220
Alexei Starovoitov68134662021-07-14 17:54:10 -0700221 if (unlikely(map_value_has_spin_lock(map))) {
222 s_off = map->spin_lock_off;
223 s_sz = sizeof(struct bpf_spin_lock);
224 } else if (unlikely(map_value_has_timer(map))) {
225 t_off = map->timer_off;
226 t_sz = sizeof(struct bpf_timer);
227 }
228
229 if (unlikely(s_sz || t_sz)) {
230 if (s_off < t_off || !s_sz) {
231 swap(s_off, t_off);
232 swap(s_sz, t_sz);
233 }
234 memcpy(dst, src, t_off);
235 memcpy(dst + t_off + t_sz,
236 src + t_off + t_sz,
237 s_off - t_off - t_sz);
238 memcpy(dst + s_off + s_sz,
239 src + s_off + s_sz,
240 map->value_size - s_off - s_sz);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800241 } else {
242 memcpy(dst, src, map->value_size);
243 }
244}
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800245void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
246 bool lock_src);
Alexei Starovoitovb00628b2021-07-14 17:54:09 -0700247void bpf_timer_cancel_and_free(void *timer);
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700248int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800249
Jakub Kicinski602144c2018-07-17 10:53:25 -0700250struct bpf_offload_dev;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800251struct bpf_offloaded_map;
252
253struct bpf_map_dev_ops {
254 int (*map_get_next_key)(struct bpf_offloaded_map *map,
255 void *key, void *next_key);
256 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
257 void *key, void *value);
258 int (*map_update_elem)(struct bpf_offloaded_map *map,
259 void *key, void *value, u64 flags);
260 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
261};
262
263struct bpf_offloaded_map {
264 struct bpf_map map;
265 struct net_device *netdev;
266 const struct bpf_map_dev_ops *dev_ops;
267 void *dev_priv;
268 struct list_head offloads;
269};
270
271static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
272{
273 return container_of(map, struct bpf_offloaded_map, map);
274}
275
Jakub Kicinski0cd3cbe2018-05-03 18:37:08 -0700276static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
277{
278 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
279}
280
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700281static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
282{
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800283 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
284 map->ops->map_seq_show_elem;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700285}
286
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200287int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800288 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200289 const struct btf_type *key_type,
290 const struct btf_type *value_type);
291
Martin KaFai Lauf4d05252020-08-27 18:18:06 -0700292bool bpf_map_meta_equal(const struct bpf_map *meta0,
293 const struct bpf_map *meta1);
294
Jakub Kicinskia3884572018-01-11 20:29:09 -0800295extern const struct bpf_map_ops bpf_map_offload_ops;
296
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700297/* function argument constraints */
298enum bpf_arg_type {
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100299 ARG_DONTCARE = 0, /* unused argument in helper function */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700300
301 /* the following constraints used to prototype
302 * bpf_map_lookup/update/delete_elem() functions
303 */
304 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
305 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
306 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +0200307 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700308 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700309
310 /* the following constraints used to prototype bpf_memcmp() and other
311 * functions that access data on eBPF program stack
312 */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800313 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
Gianluca Borellodb1ac492017-11-22 18:32:53 +0000314 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800315 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
316 * helper function must fill all bytes or clear
317 * them in error case.
Daniel Borkmann435faee12016-04-13 00:10:51 +0200318 */
319
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800320 ARG_CONST_SIZE, /* number of bytes accessed from memory */
321 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100322
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700323 ARG_PTR_TO_CTX, /* pointer to context */
Daniel Borkmannf3189032020-03-27 16:58:52 +0100324 ARG_PTR_TO_CTX_OR_NULL, /* pointer to context or NULL */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100325 ARG_ANYTHING, /* any (initialized) argument is ok */
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800326 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800327 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
Andrey Ignatov57c3bb72019-03-18 16:57:10 -0700328 ARG_PTR_TO_INT, /* pointer to int */
329 ARG_PTR_TO_LONG, /* pointer to long */
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700330 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +0200331 ARG_PTR_TO_SOCKET_OR_NULL, /* pointer to bpf_sock (fullsock) or NULL */
Alexei Starovoitova7658e12019-10-15 20:25:04 -0700332 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
Andrii Nakryiko457f44362020-05-29 00:54:20 -0700333 ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */
334 ARG_PTR_TO_ALLOC_MEM_OR_NULL, /* pointer to dynamically allocated memory or NULL */
335 ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
Martin KaFai Lau1df8f552020-09-24 17:03:50 -0700336 ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
Hao Luoeaa6bcb2020-09-29 16:50:47 -0700337 ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */
Yonghong Song69c087b2021-02-26 12:49:25 -0800338 ARG_PTR_TO_FUNC, /* pointer to a bpf program function */
339 ARG_PTR_TO_STACK_OR_NULL, /* pointer to stack or NULL */
Florent Revestfff13c42021-04-19 17:52:39 +0200340 ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */
Alexei Starovoitovb00628b2021-07-14 17:54:09 -0700341 ARG_PTR_TO_TIMER, /* pointer to bpf_timer */
Lorenz Bauerf79e7ea2020-09-21 13:12:27 +0100342 __BPF_ARG_TYPE_MAX,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700343};
344
345/* type of values returned from helper functions */
346enum bpf_return_type {
347 RET_INTEGER, /* function returns integer */
348 RET_VOID, /* function doesn't return anything */
Roman Gushchin3e6a4b32018-08-02 14:27:22 -0700349 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700350 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
Joe Stringerc64b7982018-10-02 13:35:33 -0700351 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800352 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
Lorenz Bauer85a51f82019-03-22 09:54:00 +0800353 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
Andrii Nakryiko457f44362020-05-29 00:54:20 -0700354 RET_PTR_TO_ALLOC_MEM_OR_NULL, /* returns a pointer to dynamically allocated memory or NULL */
Yonghong Songaf7ec132020-06-23 16:08:09 -0700355 RET_PTR_TO_BTF_ID_OR_NULL, /* returns a pointer to a btf_id or NULL */
Hao Luoeaa6bcb2020-09-29 16:50:47 -0700356 RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */
Hao Luo63d9b802020-09-29 16:50:48 -0700357 RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
KP Singh3ca10322020-11-06 10:37:43 +0000358 RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700359};
360
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700361/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
362 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
363 * instructions after verifying
364 */
365struct bpf_func_proto {
366 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
367 bool gpl_only;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200368 bool pkt_access;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700369 enum bpf_return_type ret_type;
Alexei Starovoitova7658e12019-10-15 20:25:04 -0700370 union {
371 struct {
372 enum bpf_arg_type arg1_type;
373 enum bpf_arg_type arg2_type;
374 enum bpf_arg_type arg3_type;
375 enum bpf_arg_type arg4_type;
376 enum bpf_arg_type arg5_type;
377 };
378 enum bpf_arg_type arg_type[5];
379 };
Lorenz Bauer9436ef62020-09-21 13:12:20 +0100380 union {
381 struct {
382 u32 *arg1_btf_id;
383 u32 *arg2_btf_id;
384 u32 *arg3_btf_id;
385 u32 *arg4_btf_id;
386 u32 *arg5_btf_id;
387 };
388 u32 *arg_btf_id[5];
389 };
Yonghong Songaf7ec132020-06-23 16:08:09 -0700390 int *ret_btf_id; /* return value btf_id */
Jiri Olsaeae2e832020-08-25 21:21:19 +0200391 bool (*allowed)(const struct bpf_prog *prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700392};
393
394/* bpf_context is intentionally undefined structure. Pointer to bpf_context is
395 * the first argument to eBPF programs.
396 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
397 */
398struct bpf_context;
399
400enum bpf_access_type {
401 BPF_READ = 1,
402 BPF_WRITE = 2
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700403};
404
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700405/* types of values stored in eBPF registers */
Edward Creef1174f72017-08-07 15:26:19 +0100406/* Pointer types represent:
407 * pointer
408 * pointer + imm
409 * pointer + (u16) var
410 * pointer + (u16) var + imm
411 * if (range > 0) then [ptr, ptr + range - off) is safe to access
412 * if (id > 0) means that some 'var' was added
413 * if (off > 0) means that 'imm' was added
414 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700415enum bpf_reg_type {
416 NOT_INIT = 0, /* nothing was written into register */
Edward Creef1174f72017-08-07 15:26:19 +0100417 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700418 PTR_TO_CTX, /* reg points to bpf_context */
419 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
420 PTR_TO_MAP_VALUE, /* reg points to map element value */
421 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
Edward Creef1174f72017-08-07 15:26:19 +0100422 PTR_TO_STACK, /* reg == frame_pointer + offset */
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200423 PTR_TO_PACKET_META, /* skb->data - meta_len */
Edward Creef1174f72017-08-07 15:26:19 +0100424 PTR_TO_PACKET, /* reg points to skb->data */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700425 PTR_TO_PACKET_END, /* skb->data + headlen */
Petar Penkovd58e4682018-09-14 07:46:18 -0700426 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
Joe Stringerc64b7982018-10-02 13:35:33 -0700427 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
428 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800429 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
430 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800431 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
432 PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
Matt Mullins9df1c282019-04-26 11:49:47 -0700433 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700434 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
John Fastabendba5f4cf2020-09-24 12:58:40 -0700435 /* PTR_TO_BTF_ID points to a kernel struct that does not need
436 * to be null checked by the BPF program. This does not imply the
437 * pointer is _not_ null and in practice this can easily be a null
438 * pointer when reading pointer chains. The assumption is program
439 * context will handle null pointer dereference typically via fault
440 * handling. The verifier must keep this in mind and can make no
441 * assumptions about null or non-null when doing branch analysis.
442 * Further, when passed into helpers the helpers can not, without
443 * additional context, assume the value is non-null.
444 */
445 PTR_TO_BTF_ID,
446 /* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
447 * been checked for null. Used primarily to inform the verifier
448 * an explicit null check is required for this struct.
449 */
450 PTR_TO_BTF_ID_OR_NULL,
Andrii Nakryiko457f44362020-05-29 00:54:20 -0700451 PTR_TO_MEM, /* reg points to valid memory region */
452 PTR_TO_MEM_OR_NULL, /* reg points to valid memory region or NULL */
Yonghong Songafbf21d2020-07-23 11:41:11 -0700453 PTR_TO_RDONLY_BUF, /* reg points to a readonly buffer */
454 PTR_TO_RDONLY_BUF_OR_NULL, /* reg points to a readonly buffer or NULL */
455 PTR_TO_RDWR_BUF, /* reg points to a read/write buffer */
456 PTR_TO_RDWR_BUF_OR_NULL, /* reg points to a read/write buffer or NULL */
Hao Luoeaa6bcb2020-09-29 16:50:47 -0700457 PTR_TO_PERCPU_BTF_ID, /* reg points to a percpu kernel variable */
Yonghong Song69c087b2021-02-26 12:49:25 -0800458 PTR_TO_FUNC, /* reg points to a bpf program function */
459 PTR_TO_MAP_KEY, /* reg points to a map element key */
Martin KaFai Laue6ac2452021-03-24 18:51:42 -0700460 __BPF_REG_TYPE_MAX,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700461};
462
Yonghong Song23994632017-06-22 15:07:39 -0700463/* The information passed from prog-specific *_is_valid_access
464 * back to the verifier.
465 */
466struct bpf_insn_access_aux {
467 enum bpf_reg_type reg_type;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700468 union {
469 int ctx_field_size;
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -0800470 struct {
471 struct btf *btf;
472 u32 btf_id;
473 };
Alexei Starovoitov9e15db62019-10-15 20:25:00 -0700474 };
475 struct bpf_verifier_log *log; /* for verbose logs */
Yonghong Song23994632017-06-22 15:07:39 -0700476};
477
Daniel Borkmannf96da092017-07-02 02:13:27 +0200478static inline void
479bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
480{
481 aux->ctx_field_size = size;
482}
483
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700484struct bpf_prog_ops {
485 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
486 union bpf_attr __user *uattr);
487};
488
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700489struct bpf_verifier_ops {
490 /* return eBPF function prototype for verification */
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700491 const struct bpf_func_proto *
492 (*get_func_proto)(enum bpf_func_id func_id,
493 const struct bpf_prog *prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700494
495 /* return true if 'size' wide access at offset 'off' within bpf_context
496 * with 'type' (read or write) is allowed
497 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700498 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700499 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700500 struct bpf_insn_access_aux *info);
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200501 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
502 const struct bpf_prog *prog);
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +0200503 int (*gen_ld_abs)(const struct bpf_insn *orig,
504 struct bpf_insn *insn_buf);
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100505 u32 (*convert_ctx_access)(enum bpf_access_type type,
506 const struct bpf_insn *src,
507 struct bpf_insn *dst,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200508 struct bpf_prog *prog, u32 *target_size);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800509 int (*btf_struct_access)(struct bpf_verifier_log *log,
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -0800510 const struct btf *btf,
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800511 const struct btf_type *t, int off, int size,
512 enum bpf_access_type atype,
513 u32 *next_btf_id);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -0700514 bool (*check_kfunc_call)(u32 kfunc_btf_id);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700515};
516
Jakub Kicinskicae19272017-12-27 18:39:05 -0800517struct bpf_prog_offload_ops {
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800518 /* verifier basic callbacks */
Jakub Kicinskicae19272017-12-27 18:39:05 -0800519 int (*insn_hook)(struct bpf_verifier_env *env,
520 int insn_idx, int prev_insn_idx);
Quentin Monnetc941ce92018-10-07 12:56:47 +0100521 int (*finalize)(struct bpf_verifier_env *env);
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800522 /* verifier optimization callbacks (called after .finalize) */
523 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
524 struct bpf_insn *insn);
525 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
526 /* program management callbacks */
Quentin Monnet16a8cb5c2018-11-09 13:03:32 +0000527 int (*prepare)(struct bpf_prog *prog);
528 int (*translate)(struct bpf_prog *prog);
Quentin Monneteb911942018-11-09 13:03:30 +0000529 void (*destroy)(struct bpf_prog *prog);
Jakub Kicinskicae19272017-12-27 18:39:05 -0800530};
531
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800532struct bpf_prog_offload {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700533 struct bpf_prog *prog;
534 struct net_device *netdev;
Quentin Monnet341b3e72018-11-09 13:03:26 +0000535 struct bpf_offload_dev *offdev;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700536 void *dev_priv;
537 struct list_head offloads;
538 bool dev_state;
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800539 bool opt_failed;
Jiong Wangfcfb1262018-01-16 16:05:19 -0800540 void *jited_image;
541 u32 jited_len;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700542};
543
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000544enum bpf_cgroup_storage_type {
545 BPF_CGROUP_STORAGE_SHARED,
Roman Gushchinb741f162018-09-28 14:45:43 +0000546 BPF_CGROUP_STORAGE_PERCPU,
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000547 __BPF_CGROUP_STORAGE_MAX
548};
549
550#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
551
Alexei Starovoitovf1b95092019-10-30 15:32:11 -0700552/* The longest tracepoint has 12 args.
553 * See include/trace/bpf_probe.h
554 */
555#define MAX_BPF_FUNC_ARGS 12
556
Dmitrii Banshchikov523a4cf2021-02-26 00:26:29 +0400557/* The maximum number of arguments passed through registers
558 * a single function may have.
559 */
560#define MAX_BPF_FUNC_REG_ARGS 5
561
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800562struct btf_func_model {
563 u8 ret_size;
564 u8 nr_args;
565 u8 arg_size[MAX_BPF_FUNC_ARGS];
566};
567
568/* Restore arguments before returning from trampoline to let original function
569 * continue executing. This flag is used for fentry progs when there are no
570 * fexit progs.
571 */
572#define BPF_TRAMP_F_RESTORE_REGS BIT(0)
573/* Call original function after fentry progs, but before fexit progs.
574 * Makes sense for fentry/fexit, normal calls and indirect calls.
575 */
576#define BPF_TRAMP_F_CALL_ORIG BIT(1)
577/* Skip current frame and return to parent. Makes sense for fentry/fexit
578 * programs only. Should not be used with normal calls and indirect calls.
579 */
580#define BPF_TRAMP_F_SKIP_FRAME BIT(2)
581
Jiri Olsa7e6f3cd2021-07-14 11:43:53 +0200582/* Store IP address of the caller on the trampoline stack,
583 * so it's available for trampoline's programs.
584 */
585#define BPF_TRAMP_F_IP_ARG BIT(3)
586
KP Singh88fd9e52020-03-04 20:18:47 +0100587/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
588 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2
589 */
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800590#define BPF_MAX_TRAMP_PROGS 38
KP Singh88fd9e52020-03-04 20:18:47 +0100591
592struct bpf_tramp_progs {
593 struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS];
594 int nr_progs;
595};
596
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800597/* Different use cases for BPF trampoline:
598 * 1. replace nop at the function entry (kprobe equivalent)
599 * flags = BPF_TRAMP_F_RESTORE_REGS
600 * fentry = a set of programs to run before returning from trampoline
601 *
602 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
603 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
604 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
605 * fentry = a set of program to run before calling original function
606 * fexit = a set of program to run after original function
607 *
608 * 3. replace direct call instruction anywhere in the function body
609 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
610 * With flags = 0
611 * fentry = a set of programs to run before returning from trampoline
612 * With flags = BPF_TRAMP_F_CALL_ORIG
613 * orig_call = original callback addr or direct function addr
614 * fentry = a set of program to run before calling original function
615 * fexit = a set of program to run after original function
616 */
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700617struct bpf_tramp_image;
618int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800619 const struct btf_func_model *m, u32 flags,
KP Singh88fd9e52020-03-04 20:18:47 +0100620 struct bpf_tramp_progs *tprogs,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800621 void *orig_call);
622/* these two functions are called from generated trampoline */
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800623u64 notrace __bpf_prog_enter(struct bpf_prog *prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800624void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800625u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog);
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800626void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start);
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700627void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
628void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800629
Jiri Olsa535911c2020-03-12 20:55:58 +0100630struct bpf_ksym {
631 unsigned long start;
632 unsigned long end;
Jiri Olsabfea9a82020-03-12 20:55:59 +0100633 char name[KSYM_NAME_LEN];
Jiri Olsaecb60d12020-03-12 20:56:00 +0100634 struct list_head lnode;
Jiri Olsaca4424c2020-03-12 20:56:01 +0100635 struct latch_tree_node tnode;
Jiri Olsacbd76f8d2020-03-12 20:56:03 +0100636 bool prog;
Jiri Olsa535911c2020-03-12 20:55:58 +0100637};
638
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800639enum bpf_tramp_prog_type {
640 BPF_TRAMP_FENTRY,
641 BPF_TRAMP_FEXIT,
KP Singhae240822020-03-04 20:18:49 +0100642 BPF_TRAMP_MODIFY_RETURN,
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800643 BPF_TRAMP_MAX,
644 BPF_TRAMP_REPLACE, /* more than MAX */
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800645};
646
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700647struct bpf_tramp_image {
648 void *image;
649 struct bpf_ksym ksym;
650 struct percpu_ref pcref;
651 void *ip_after_call;
652 void *ip_epilogue;
653 union {
654 struct rcu_head rcu;
655 struct work_struct work;
656 };
657};
658
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800659struct bpf_trampoline {
660 /* hlist for trampoline_table */
661 struct hlist_node hlist;
662 /* serializes access to fields of this trampoline */
663 struct mutex mutex;
664 refcount_t refcnt;
665 u64 key;
666 struct {
667 struct btf_func_model model;
668 void *addr;
Alexei Starovoitovb91e014f2019-12-08 16:01:13 -0800669 bool ftrace_managed;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800670 } func;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800671 /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
672 * program by replacing one of its functions. func.addr is the address
673 * of the function it replaced.
674 */
675 struct bpf_prog *extension_prog;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800676 /* list of BPF programs using this trampoline */
677 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
678 /* Number of attached programs. A counter per kind. */
679 int progs_cnt[BPF_TRAMP_MAX];
680 /* Executable image of trampoline */
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700681 struct bpf_tramp_image *cur_image;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800682 u64 selector;
Jiri Olsa861de022021-03-26 11:59:00 +0100683 struct module *mod;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800684};
Björn Töpel75ccbef2019-12-13 18:51:08 +0100685
Toke Høiland-Jørgensenf7b12b62020-09-25 23:25:02 +0200686struct bpf_attach_target_info {
687 struct btf_func_model fmodel;
688 long tgt_addr;
689 const char *tgt_name;
690 const struct btf_type *tgt_type;
691};
692
Björn Töpel116eb782019-12-13 18:51:12 +0100693#define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
Björn Töpel75ccbef2019-12-13 18:51:08 +0100694
695struct bpf_dispatcher_prog {
696 struct bpf_prog *prog;
697 refcount_t users;
698};
699
700struct bpf_dispatcher {
701 /* dispatcher mutex */
702 struct mutex mutex;
703 void *func;
704 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
705 int num_progs;
706 void *image;
707 u32 image_off;
Jiri Olsa517b75e2020-03-12 20:56:06 +0100708 struct bpf_ksym ksym;
Björn Töpel75ccbef2019-12-13 18:51:08 +0100709};
710
Sami Tolvanen9f5b4002021-04-08 11:28:33 -0700711static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
Björn Töpel7e6897f2019-12-13 18:51:09 +0100712 const void *ctx,
713 const struct bpf_insn *insnsi,
714 unsigned int (*bpf_func)(const void *,
715 const struct bpf_insn *))
716{
717 return bpf_func(ctx, insnsi);
718}
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800719#ifdef CONFIG_BPF_JIT
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200720int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr);
721int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr);
Toke Høiland-Jørgensenf7b12b62020-09-25 23:25:02 +0200722struct bpf_trampoline *bpf_trampoline_get(u64 key,
723 struct bpf_attach_target_info *tgt_info);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800724void bpf_trampoline_put(struct bpf_trampoline *tr);
Jiri Olsa517b75e2020-03-12 20:56:06 +0100725#define BPF_DISPATCHER_INIT(_name) { \
726 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
727 .func = &_name##_func, \
728 .progs = {}, \
729 .num_progs = 0, \
730 .image = NULL, \
731 .image_off = 0, \
732 .ksym = { \
733 .name = #_name, \
734 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
735 }, \
Björn Töpel75ccbef2019-12-13 18:51:08 +0100736}
737
738#define DEFINE_BPF_DISPATCHER(name) \
Sami Tolvanen9f5b4002021-04-08 11:28:33 -0700739 noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \
Björn Töpel75ccbef2019-12-13 18:51:08 +0100740 const void *ctx, \
741 const struct bpf_insn *insnsi, \
742 unsigned int (*bpf_func)(const void *, \
743 const struct bpf_insn *)) \
744 { \
745 return bpf_func(ctx, insnsi); \
746 } \
Björn Töpel6a640372020-03-12 20:55:57 +0100747 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
748 struct bpf_dispatcher bpf_dispatcher_##name = \
749 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
Björn Töpel75ccbef2019-12-13 18:51:08 +0100750#define DECLARE_BPF_DISPATCHER(name) \
Björn Töpel6a640372020-03-12 20:55:57 +0100751 unsigned int bpf_dispatcher_##name##_func( \
Björn Töpel75ccbef2019-12-13 18:51:08 +0100752 const void *ctx, \
753 const struct bpf_insn *insnsi, \
754 unsigned int (*bpf_func)(const void *, \
755 const struct bpf_insn *)); \
Björn Töpel6a640372020-03-12 20:55:57 +0100756 extern struct bpf_dispatcher bpf_dispatcher_##name;
757#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
758#define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
Björn Töpel75ccbef2019-12-13 18:51:08 +0100759void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
760 struct bpf_prog *to);
Jiri Olsadba122fb2020-03-12 20:56:04 +0100761/* Called only from JIT-enabled code, so there's no need for stubs. */
Jiri Olsa7ac88eb2020-03-12 20:56:07 +0100762void *bpf_jit_alloc_exec_page(void);
Jiri Olsaa108f7d2020-03-12 20:56:05 +0100763void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
764void bpf_image_ksym_del(struct bpf_ksym *ksym);
Jiri Olsadba122fb2020-03-12 20:56:04 +0100765void bpf_ksym_add(struct bpf_ksym *ksym);
766void bpf_ksym_del(struct bpf_ksym *ksym);
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700767int bpf_jit_charge_modmem(u32 pages);
768void bpf_jit_uncharge_modmem(u32 pages);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800769#else
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200770static inline int bpf_trampoline_link_prog(struct bpf_prog *prog,
771 struct bpf_trampoline *tr)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800772{
773 return -ENOTSUPP;
774}
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200775static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog,
776 struct bpf_trampoline *tr)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800777{
778 return -ENOTSUPP;
779}
Toke Høiland-Jørgensenf7b12b62020-09-25 23:25:02 +0200780static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
781 struct bpf_attach_target_info *tgt_info)
782{
783 return ERR_PTR(-EOPNOTSUPP);
784}
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800785static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
Björn Töpel75ccbef2019-12-13 18:51:08 +0100786#define DEFINE_BPF_DISPATCHER(name)
787#define DECLARE_BPF_DISPATCHER(name)
Björn Töpel6a640372020-03-12 20:55:57 +0100788#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
Björn Töpel75ccbef2019-12-13 18:51:08 +0100789#define BPF_DISPATCHER_PTR(name) NULL
790static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
791 struct bpf_prog *from,
792 struct bpf_prog *to) {}
Jiri Olsae9b4e602020-01-23 17:15:07 +0100793static inline bool is_bpf_image_address(unsigned long address)
794{
795 return false;
796}
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800797#endif
798
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800799struct bpf_func_info_aux {
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -0800800 u16 linkage;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800801 bool unreliable;
802};
803
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100804enum bpf_jit_poke_reason {
805 BPF_POKE_REASON_TAIL_CALL,
806};
807
808/* Descriptor of pokes pointing /into/ the JITed image. */
809struct bpf_jit_poke_descriptor {
Maciej Fijalkowskicf71b172020-09-16 23:10:06 +0200810 void *tailcall_target;
Maciej Fijalkowskiebf7d1f52020-09-16 23:10:08 +0200811 void *tailcall_bypass;
812 void *bypass_addr;
John Fastabendf263a812021-07-07 15:38:47 -0700813 void *aux;
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100814 union {
815 struct {
816 struct bpf_map *map;
817 u32 key;
818 } tail_call;
819 };
Maciej Fijalkowskicf71b172020-09-16 23:10:06 +0200820 bool tailcall_target_stable;
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100821 u8 adj_off;
822 u16 reason;
Maciej Fijalkowskia748c692020-09-16 23:10:05 +0200823 u32 insn_idx;
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100824};
825
Yonghong Song3c32cc12020-05-13 11:02:21 -0700826/* reg_type info for ctx arguments */
827struct bpf_ctx_arg_aux {
828 u32 offset;
829 enum bpf_reg_type reg_type;
Yonghong Song951cf362020-07-20 09:34:03 -0700830 u32 btf_id;
Yonghong Song3c32cc12020-05-13 11:02:21 -0700831};
832
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -0800833struct btf_mod_pair {
834 struct btf *btf;
835 struct module *module;
836};
837
Martin KaFai Laue6ac2452021-03-24 18:51:42 -0700838struct bpf_kfunc_desc_tab;
839
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700840struct bpf_prog_aux {
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -0800841 atomic64_t refcnt;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100842 u32 used_map_cnt;
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -0800843 u32 used_btf_cnt;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700844 u32 max_ctx_offset;
Jiong Wange6478152018-11-08 04:08:42 -0500845 u32 max_pkt_offset;
Matt Mullins9df1c282019-04-26 11:49:47 -0700846 u32 max_tp_access;
Alexei Starovoitov87266792017-05-30 13:31:29 -0700847 u32 stack_depth;
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700848 u32 id;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800849 u32 func_cnt; /* used by non-func prog as the number of func progs */
850 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
Alexei Starovoitovccfe29eb22019-10-15 20:24:58 -0700851 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
Yonghong Song3c32cc12020-05-13 11:02:21 -0700852 u32 ctx_arg_info_size;
Yonghong Songafbf21d2020-07-23 11:41:11 -0700853 u32 max_rdonly_access;
854 u32 max_rdwr_access;
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -0800855 struct btf *attach_btf;
Yonghong Song3c32cc12020-05-13 11:02:21 -0700856 const struct bpf_ctx_arg_aux *ctx_arg_info;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200857 struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
858 struct bpf_prog *dst_prog;
859 struct bpf_trampoline *dst_trampoline;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +0200860 enum bpf_prog_type saved_dst_prog_type;
861 enum bpf_attach_type saved_dst_attach_type;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +0100862 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800863 bool offload_requested;
Martin KaFai Lau38207292019-10-24 17:18:11 -0700864 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800865 bool func_proto_unreliable;
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -0700866 bool sleepable;
Maciej Fijalkowskiebf7d1f52020-09-16 23:10:08 +0200867 bool tail_call_reachable;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800868 struct hlist_node tramp_hlist;
Martin KaFai Lau38207292019-10-24 17:18:11 -0700869 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
870 const struct btf_type *attach_func_proto;
871 /* function name for valid attach_btf_id */
872 const char *attach_func_name;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800873 struct bpf_prog **func;
874 void *jit_data; /* JIT specific data. arch dependent */
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100875 struct bpf_jit_poke_descriptor *poke_tab;
Martin KaFai Laue6ac2452021-03-24 18:51:42 -0700876 struct bpf_kfunc_desc_tab *kfunc_tab;
Daniel Borkmanna66886f2019-11-22 21:07:57 +0100877 u32 size_poke_tab;
Jiri Olsa535911c2020-03-12 20:55:58 +0100878 struct bpf_ksym ksym;
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700879 const struct bpf_prog_ops *ops;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700880 struct bpf_map **used_maps;
YiFei Zhu984fe942020-09-15 16:45:39 -0700881 struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -0800882 struct btf_mod_pair *used_btfs;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700883 struct bpf_prog *prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700884 struct user_struct *user;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700885 u64 load_time; /* ns since boottime */
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000886 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700887 char name[BPF_OBJ_NAME_LEN];
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700888#ifdef CONFIG_SECURITY
889 void *security;
890#endif
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800891 struct bpf_prog_offload *offload;
Yonghong Song838e9692018-11-19 15:29:11 -0800892 struct btf *btf;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800893 struct bpf_func_info *func_info;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -0800894 struct bpf_func_info_aux *func_info_aux;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800895 /* bpf_line_info loaded from userspace. linfo->insn_off
896 * has the xlated insn offset.
897 * Both the main and sub prog share the same linfo.
898 * The subprog can access its first linfo by
899 * using the linfo_idx.
900 */
901 struct bpf_line_info *linfo;
902 /* jited_linfo is the jited addr of the linfo. It has a
903 * one to one mapping to linfo:
904 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
905 * Both the main and sub prog share the same jited_linfo.
906 * The subprog can access its first jited_linfo by
907 * using the linfo_idx.
908 */
909 void **jited_linfo;
Yonghong Songba64e7d2018-11-24 23:20:44 -0800910 u32 func_info_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800911 u32 nr_linfo;
912 /* subprog can use linfo_idx to access its first linfo and
913 * jited_linfo.
914 * main prog always has linfo_idx == 0
915 */
916 u32 linfo_idx;
Alexei Starovoitov3dec5412019-10-15 20:25:03 -0700917 u32 num_exentries;
918 struct exception_table_entry *extable;
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700919 union {
920 struct work_struct work;
921 struct rcu_head rcu;
922 };
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700923};
924
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100925struct bpf_array_aux {
926 /* 'Ownership' of prog array is claimed by the first program that
927 * is going to use this map or by the first program which FD is
928 * stored in the map to make sure that all callers and callees have
929 * the same prog type and JITed flag.
930 */
931 enum bpf_prog_type type;
932 bool jited;
Daniel Borkmannda765a22019-11-22 21:07:58 +0100933 /* Programs with direct jumps into programs part of this array. */
934 struct list_head poke_progs;
935 struct bpf_map *map;
936 struct mutex poke_mutex;
937 struct work_struct work;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100938};
939
Andrii Nakryiko6cc7d1e2020-07-21 23:45:54 -0700940struct bpf_link {
941 atomic64_t refcnt;
942 u32 id;
943 enum bpf_link_type type;
944 const struct bpf_link_ops *ops;
945 struct bpf_prog *prog;
946 struct work_struct work;
947};
948
949struct bpf_link_ops {
950 void (*release)(struct bpf_link *link);
951 void (*dealloc)(struct bpf_link *link);
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -0700952 int (*detach)(struct bpf_link *link);
Andrii Nakryiko6cc7d1e2020-07-21 23:45:54 -0700953 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
954 struct bpf_prog *old_prog);
955 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
956 int (*fill_link_info)(const struct bpf_link *link,
957 struct bpf_link_info *info);
958};
959
960struct bpf_link_primer {
961 struct bpf_link *link;
962 struct file *file;
963 int fd;
964 u32 id;
965};
966
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800967struct bpf_struct_ops_value;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800968struct btf_member;
969
970#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
971struct bpf_struct_ops {
972 const struct bpf_verifier_ops *verifier_ops;
973 int (*init)(struct btf *btf);
974 int (*check_member)(const struct btf_type *t,
975 const struct btf_member *member);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800976 int (*init_member)(const struct btf_type *t,
977 const struct btf_member *member,
978 void *kdata, const void *udata);
979 int (*reg)(void *kdata);
980 void (*unreg)(void *kdata);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800981 const struct btf_type *type;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800982 const struct btf_type *value_type;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800983 const char *name;
984 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
985 u32 type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800986 u32 value_id;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800987};
988
989#if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800990#define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800991const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
Martin KaFai Laud3e42bb2020-01-27 09:51:45 -0800992void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800993bool bpf_struct_ops_get(const void *kdata);
994void bpf_struct_ops_put(const void *kdata);
995int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
996 void *value);
997static inline bool bpf_try_module_get(const void *data, struct module *owner)
998{
999 if (owner == BPF_MODULE_OWNER)
1000 return bpf_struct_ops_get(data);
1001 else
1002 return try_module_get(owner);
1003}
1004static inline void bpf_module_put(const void *data, struct module *owner)
1005{
1006 if (owner == BPF_MODULE_OWNER)
1007 bpf_struct_ops_put(data);
1008 else
1009 module_put(owner);
1010}
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001011#else
1012static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
1013{
1014 return NULL;
1015}
Martin KaFai Laud3e42bb2020-01-27 09:51:45 -08001016static inline void bpf_struct_ops_init(struct btf *btf,
1017 struct bpf_verifier_log *log)
1018{
1019}
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001020static inline bool bpf_try_module_get(const void *data, struct module *owner)
1021{
1022 return try_module_get(owner);
1023}
1024static inline void bpf_module_put(const void *data, struct module *owner)
1025{
1026 module_put(owner);
1027}
1028static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1029 void *key,
1030 void *value)
1031{
1032 return -EINVAL;
1033}
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001034#endif
1035
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001036struct bpf_array {
1037 struct bpf_map map;
1038 u32 elem_size;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08001039 u32 index_mask;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +01001040 struct bpf_array_aux *aux;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001041 union {
1042 char value[0] __aligned(8);
Wang Nan2a36f0b2015-08-06 07:02:33 +00001043 void *ptrs[0] __aligned(8);
Alexei Starovoitova10423b2016-02-01 22:39:54 -08001044 void __percpu *pptrs[0] __aligned(8);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001045 };
1046};
Daniel Borkmann3b1efb12016-06-15 22:47:14 +02001047
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001048#define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001049#define MAX_TAIL_CALL_CNT 32
1050
Daniel Borkmann591fe982019-04-09 23:20:05 +02001051#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
1052 BPF_F_RDONLY_PROG | \
1053 BPF_F_WRONLY | \
1054 BPF_F_WRONLY_PROG)
1055
1056#define BPF_MAP_CAN_READ BIT(0)
1057#define BPF_MAP_CAN_WRITE BIT(1)
1058
1059static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1060{
1061 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1062
1063 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1064 * not possible.
1065 */
1066 if (access_flags & BPF_F_RDONLY_PROG)
1067 return BPF_MAP_CAN_READ;
1068 else if (access_flags & BPF_F_WRONLY_PROG)
1069 return BPF_MAP_CAN_WRITE;
1070 else
1071 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1072}
1073
1074static inline bool bpf_map_flags_access_ok(u32 access_flags)
1075{
1076 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1077 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1078}
1079
Daniel Borkmann3b1efb12016-06-15 22:47:14 +02001080struct bpf_event_entry {
1081 struct perf_event *event;
1082 struct file *perf_file;
1083 struct file *map_file;
1084 struct rcu_head rcu;
1085};
1086
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001087bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001088int bpf_prog_calc_tag(struct bpf_prog *fp);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001089
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001090const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001091
1092typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02001093 unsigned long off, unsigned long len);
Joe Stringerc64b7982018-10-02 13:35:33 -07001094typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1095 const struct bpf_insn *src,
1096 struct bpf_insn *dst,
1097 struct bpf_prog *prog,
1098 u32 *target_size);
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001099
1100u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1101 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001102
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001103/* an array of programs to be executed under rcu_lock.
1104 *
1105 * Typical usage:
Andrii Nakryikofb7dd8b2021-08-15 00:05:54 -07001106 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, bpf_prog_run);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001107 *
1108 * the structure returned by bpf_prog_array_alloc() should be populated
1109 * with program pointers and the last pointer must be NULL.
1110 * The user has to keep refcnt on the program and make sure the program
1111 * is removed from the array before bpf_prog_put().
1112 * The 'struct bpf_prog_array *' should only be replaced with xchg()
1113 * since other cpus are walking the array of pointers in parallel.
1114 */
Roman Gushchin394e40a2018-08-02 14:27:21 -07001115struct bpf_prog_array_item {
1116 struct bpf_prog *prog;
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07001117 union {
1118 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1119 u64 bpf_cookie;
1120 };
Roman Gushchin394e40a2018-08-02 14:27:21 -07001121};
1122
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001123struct bpf_prog_array {
1124 struct rcu_head rcu;
Gustavo A. R. Silvad7f10df2020-02-26 18:17:44 -06001125 struct bpf_prog_array_item items[];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001126};
1127
Roman Gushchind29ab6e2018-07-13 12:41:10 -07001128struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -07001129void bpf_prog_array_free(struct bpf_prog_array *progs);
1130int bpf_prog_array_length(struct bpf_prog_array *progs);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001131bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -07001132int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001133 __u32 __user *prog_ids, u32 cnt);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001134
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -07001135void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001136 struct bpf_prog *old_prog);
Jakub Sitnickice3aa9c2020-07-17 12:35:22 +02001137int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1138int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1139 struct bpf_prog *prog);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -07001140int bpf_prog_array_copy_info(struct bpf_prog_array *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001141 u32 *prog_ids, u32 request_cnt,
1142 u32 *prog_cnt);
Stanislav Fomichev54e9c9d2019-05-28 14:14:41 -07001143int bpf_prog_array_copy(struct bpf_prog_array *old_array,
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001144 struct bpf_prog *exclude_prog,
1145 struct bpf_prog *include_prog,
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07001146 u64 bpf_cookie,
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001147 struct bpf_prog_array **new_array);
1148
Andrii Nakryikoc7603cf2021-07-12 16:06:15 -07001149struct bpf_run_ctx {};
1150
1151struct bpf_cg_run_ctx {
1152 struct bpf_run_ctx run_ctx;
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001153 const struct bpf_prog_array_item *prog_item;
Andrii Nakryikoc7603cf2021-07-12 16:06:15 -07001154};
1155
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07001156struct bpf_trace_run_ctx {
1157 struct bpf_run_ctx run_ctx;
1158 u64 bpf_cookie;
1159};
1160
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001161static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
1162{
1163 struct bpf_run_ctx *old_ctx = NULL;
1164
1165#ifdef CONFIG_BPF_SYSCALL
1166 old_ctx = current->bpf_ctx;
1167 current->bpf_ctx = new_ctx;
1168#endif
1169 return old_ctx;
1170}
1171
1172static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
1173{
1174#ifdef CONFIG_BPF_SYSCALL
1175 current->bpf_ctx = old_ctx;
1176#endif
1177}
1178
Stanislav Fomichev77241212021-01-27 11:31:39 -08001179/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1180#define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)
1181/* BPF program asks to set CN on the packet. */
1182#define BPF_RET_SET_CN (1 << 0)
1183
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001184typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
Stanislav Fomichev77241212021-01-27 11:31:39 -08001185
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001186static __always_inline u32
1187BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,
1188 const void *ctx, bpf_prog_run_fn run_prog,
1189 u32 *ret_flags)
1190{
1191 const struct bpf_prog_array_item *item;
1192 const struct bpf_prog *prog;
1193 const struct bpf_prog_array *array;
1194 struct bpf_run_ctx *old_run_ctx;
1195 struct bpf_cg_run_ctx run_ctx;
1196 u32 ret = 1;
1197 u32 func_ret;
1198
1199 migrate_disable();
1200 rcu_read_lock();
1201 array = rcu_dereference(array_rcu);
1202 item = &array->items[0];
1203 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1204 while ((prog = READ_ONCE(item->prog))) {
1205 run_ctx.prog_item = item;
1206 func_ret = run_prog(prog, ctx);
1207 ret &= (func_ret & 1);
1208 *(ret_flags) |= (func_ret >> 1);
1209 item++;
1210 }
1211 bpf_reset_run_ctx(old_run_ctx);
1212 rcu_read_unlock();
1213 migrate_enable();
1214 return ret;
1215}
1216
1217static __always_inline u32
1218BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu,
1219 const void *ctx, bpf_prog_run_fn run_prog)
1220{
1221 const struct bpf_prog_array_item *item;
1222 const struct bpf_prog *prog;
1223 const struct bpf_prog_array *array;
1224 struct bpf_run_ctx *old_run_ctx;
1225 struct bpf_cg_run_ctx run_ctx;
1226 u32 ret = 1;
1227
1228 migrate_disable();
1229 rcu_read_lock();
1230 array = rcu_dereference(array_rcu);
1231 item = &array->items[0];
1232 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1233 while ((prog = READ_ONCE(item->prog))) {
1234 run_ctx.prog_item = item;
1235 ret &= run_prog(prog, ctx);
1236 item++;
1237 }
1238 bpf_reset_run_ctx(old_run_ctx);
1239 rcu_read_unlock();
1240 migrate_enable();
1241 return ret;
1242}
1243
1244static __always_inline u32
1245BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,
1246 const void *ctx, bpf_prog_run_fn run_prog)
1247{
1248 const struct bpf_prog_array_item *item;
1249 const struct bpf_prog *prog;
1250 const struct bpf_prog_array *array;
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07001251 struct bpf_run_ctx *old_run_ctx;
1252 struct bpf_trace_run_ctx run_ctx;
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001253 u32 ret = 1;
1254
1255 migrate_disable();
1256 rcu_read_lock();
1257 array = rcu_dereference(array_rcu);
1258 if (unlikely(!array))
1259 goto out;
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07001260 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001261 item = &array->items[0];
1262 while ((prog = READ_ONCE(item->prog))) {
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07001263 run_ctx.bpf_cookie = item->bpf_cookie;
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001264 ret &= run_prog(prog, ctx);
1265 item++;
1266 }
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07001267 bpf_reset_run_ctx(old_run_ctx);
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001268out:
1269 rcu_read_unlock();
1270 migrate_enable();
1271 return ret;
1272}
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001273
brakmo1f52f6c2019-05-28 16:59:35 -07001274/* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
1275 * so BPF programs can request cwr for TCP packets.
1276 *
1277 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
1278 * packet. This macro changes the behavior so the low order bit
1279 * indicates whether the packet should be dropped (0) or not (1)
1280 * and the next bit is a congestion notification bit. This could be
1281 * used by TCP to call tcp_enter_cwr()
1282 *
1283 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
1284 * 0: drop packet
1285 * 1: keep packet
1286 * 2: drop packet and cn
1287 * 3: keep packet and cn
1288 *
1289 * This macro then converts it to one of the NET_XMIT or an error
1290 * code that is then interpreted as drop packet (and no cn):
1291 * 0: NET_XMIT_SUCCESS skb should be transmitted
1292 * 1: NET_XMIT_DROP skb should be dropped and cn
1293 * 2: NET_XMIT_CN skb should be transmitted and cn
1294 * 3: -EPERM skb should be dropped
1295 */
1296#define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
1297 ({ \
Stanislav Fomichev77241212021-01-27 11:31:39 -08001298 u32 _flags = 0; \
1299 bool _cn; \
1300 u32 _ret; \
Andrii Nakryiko7d08c2c2021-08-15 00:05:55 -07001301 _ret = BPF_PROG_RUN_ARRAY_CG_FLAGS(array, ctx, func, &_flags); \
Stanislav Fomichev77241212021-01-27 11:31:39 -08001302 _cn = _flags & BPF_RET_SET_CN; \
brakmo1f52f6c2019-05-28 16:59:35 -07001303 if (_ret) \
1304 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
1305 else \
1306 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
1307 _ret; \
1308 })
1309
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001310#ifdef CONFIG_BPF_SYSCALL
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001311DECLARE_PER_CPU(int, bpf_prog_active);
Song Liud46edd62020-04-30 00:15:04 -07001312extern struct mutex bpf_stats_enabled_mutex;
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001313
Thomas Gleixnerc518cfa2020-02-24 15:01:47 +01001314/*
1315 * Block execution of BPF programs attached to instrumentation (perf,
1316 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1317 * these events can happen inside a region which holds a map bucket lock
1318 * and can deadlock on it.
1319 *
1320 * Use the preemption safe inc/dec variants on RT because migrate disable
1321 * is preemptible on RT and preemption in the middle of the RMW operation
1322 * might lead to inconsistent state. Use the raw variants for non RT
1323 * kernels as migrate_disable() maps to preempt_disable() so the slightly
1324 * more expensive save operation can be avoided.
1325 */
1326static inline void bpf_disable_instrumentation(void)
1327{
1328 migrate_disable();
1329 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1330 this_cpu_inc(bpf_prog_active);
1331 else
1332 __this_cpu_inc(bpf_prog_active);
1333}
1334
1335static inline void bpf_enable_instrumentation(void)
1336{
1337 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1338 this_cpu_dec(bpf_prog_active);
1339 else
1340 __this_cpu_dec(bpf_prog_active);
1341 migrate_enable();
1342}
1343
Chenbo Fengf66e4482017-10-18 13:00:26 -07001344extern const struct file_operations bpf_map_fops;
1345extern const struct file_operations bpf_prog_fops;
Yonghong Song367ec3e2020-05-09 10:59:06 -07001346extern const struct file_operations bpf_iter_fops;
Chenbo Fengf66e4482017-10-18 13:00:26 -07001347
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001348#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001349 extern const struct bpf_prog_ops _name ## _prog_ops; \
1350 extern const struct bpf_verifier_ops _name ## _verifier_ops;
Johannes Berg40077e02017-04-11 15:34:58 +02001351#define BPF_MAP_TYPE(_id, _ops) \
1352 extern const struct bpf_map_ops _ops;
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001353#define BPF_LINK_TYPE(_id, _name)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001354#include <linux/bpf_types.h>
1355#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +02001356#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001357#undef BPF_LINK_TYPE
Daniel Borkmann0fc174d2015-03-01 12:31:44 +01001358
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001359extern const struct bpf_prog_ops bpf_offload_prog_ops;
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001360extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1361extern const struct bpf_verifier_ops xdp_analyzer_ops;
1362
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001363struct bpf_prog *bpf_prog_get(u32 ufd);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001364struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -08001365 bool attach_drv);
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001366void bpf_prog_add(struct bpf_prog *prog, int i);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001367void bpf_prog_sub(struct bpf_prog *prog, int i);
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001368void bpf_prog_inc(struct bpf_prog *prog);
John Fastabenda6f6df62017-08-15 22:32:22 -07001369struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001370void bpf_prog_put(struct bpf_prog *prog);
1371
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001372void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001373void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001374
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -08001375struct bpf_map *bpf_map_get(u32 ufd);
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001376struct bpf_map *bpf_map_get_with_uref(u32 ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001377struct bpf_map *__bpf_map_get(struct fd f);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001378void bpf_map_inc(struct bpf_map *map);
1379void bpf_map_inc_with_uref(struct bpf_map *map);
1380struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001381void bpf_map_put_with_uref(struct bpf_map *map);
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001382void bpf_map_put(struct bpf_map *map);
Daniel Borkmann196e8ca2019-11-20 23:04:44 +01001383void *bpf_map_area_alloc(u64 size, int numa_node);
1384void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001385void bpf_map_area_free(void *base);
Jakub Kicinskibd475642018-01-11 20:29:06 -08001386void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001387int generic_map_lookup_batch(struct bpf_map *map,
1388 const union bpf_attr *attr,
1389 union bpf_attr __user *uattr);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001390int generic_map_update_batch(struct bpf_map *map,
1391 const union bpf_attr *attr,
1392 union bpf_attr __user *uattr);
1393int generic_map_delete_batch(struct bpf_map *map,
1394 const union bpf_attr *attr,
1395 union bpf_attr __user *uattr);
Yonghong Song6086d292020-05-09 10:59:09 -07001396struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
Alexei Starovoitova228a642020-07-01 18:10:18 -07001397struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001398
Roman Gushchin48edc1f2020-12-01 13:58:32 -08001399#ifdef CONFIG_MEMCG_KMEM
1400void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1401 int node);
1402void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1403void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1404 size_t align, gfp_t flags);
1405#else
1406static inline void *
1407bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1408 int node)
1409{
1410 return kmalloc_node(size, flags, node);
1411}
1412
1413static inline void *
1414bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1415{
1416 return kzalloc(size, flags);
1417}
1418
1419static inline void __percpu *
1420bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1421 gfp_t flags)
1422{
1423 return __alloc_percpu_gfp(size, align, flags);
1424}
1425#endif
1426
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001427extern int sysctl_unprivileged_bpf_disabled;
1428
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001429static inline bool bpf_allow_ptr_leaks(void)
1430{
1431 return perfmon_capable();
1432}
1433
Andrei Matei01f810a2021-02-06 20:10:24 -05001434static inline bool bpf_allow_uninit_stack(void)
1435{
1436 return perfmon_capable();
1437}
1438
Andrey Ignatov41c48f32020-06-19 14:11:43 -07001439static inline bool bpf_allow_ptr_to_map_access(void)
1440{
1441 return perfmon_capable();
1442}
1443
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001444static inline bool bpf_bypass_spec_v1(void)
1445{
1446 return perfmon_capable();
1447}
1448
1449static inline bool bpf_bypass_spec_v4(void)
1450{
1451 return perfmon_capable();
1452}
1453
Chenbo Feng6e71b042017-10-18 13:00:22 -07001454int bpf_map_new_fd(struct bpf_map *map, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001455int bpf_prog_new_fd(struct bpf_prog *prog);
1456
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001457void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07001458 const struct bpf_link_ops *ops, struct bpf_prog *prog);
1459int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1460int bpf_link_settle(struct bpf_link_primer *primer);
1461void bpf_link_cleanup(struct bpf_link_primer *primer);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08001462void bpf_link_inc(struct bpf_link *link);
1463void bpf_link_put(struct bpf_link *link);
1464int bpf_link_new_fd(struct bpf_link *link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07001465struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08001466struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1467
Daniel Borkmannb2197752015-10-29 14:58:09 +01001468int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
Chenbo Feng6e71b042017-10-18 13:00:22 -07001469int bpf_obj_get_user(const char __user *pathname, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001470
Yonghong Song21aef702020-05-13 11:02:16 -07001471#define BPF_ITER_FUNC_PREFIX "bpf_iter_"
Yonghong Songe5158d92020-05-09 10:59:07 -07001472#define DEFINE_BPF_ITER_FUNC(target, args...) \
Yonghong Song21aef702020-05-13 11:02:16 -07001473 extern int bpf_iter_ ## target(args); \
1474 int __init bpf_iter_ ## target(args) { return 0; }
Yonghong Song15d83c42020-05-09 10:59:00 -07001475
Yonghong Songf9c79272020-07-23 11:41:10 -07001476struct bpf_iter_aux_info {
Yonghong Songa5cbe052020-07-23 11:41:12 -07001477 struct bpf_map *map;
Yonghong Songf9c79272020-07-23 11:41:10 -07001478};
1479
Yonghong Song5e7b3022020-08-04 22:50:56 -07001480typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1481 union bpf_iter_link_info *linfo,
1482 struct bpf_iter_aux_info *aux);
1483typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
Yonghong Song6b0a2492020-08-21 11:44:18 -07001484typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1485 struct seq_file *seq);
1486typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1487 struct bpf_link_info *info);
Martin KaFai Lau3cee6fb2021-07-01 13:06:19 -07001488typedef const struct bpf_func_proto *
1489(*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
1490 const struct bpf_prog *prog);
Yonghong Songa5cbe052020-07-23 11:41:12 -07001491
Yonghong Songcf83b2d2020-10-27 23:10:54 -07001492enum bpf_iter_feature {
1493 BPF_ITER_RESCHED = BIT(0),
1494};
1495
Yonghong Song3c32cc12020-05-13 11:02:21 -07001496#define BPF_ITER_CTX_ARG_MAX 2
Yonghong Songae243452020-05-09 10:58:59 -07001497struct bpf_iter_reg {
1498 const char *target;
Yonghong Song5e7b3022020-08-04 22:50:56 -07001499 bpf_iter_attach_target_t attach_target;
1500 bpf_iter_detach_target_t detach_target;
Yonghong Song6b0a2492020-08-21 11:44:18 -07001501 bpf_iter_show_fdinfo_t show_fdinfo;
1502 bpf_iter_fill_link_info_t fill_link_info;
Martin KaFai Lau3cee6fb2021-07-01 13:06:19 -07001503 bpf_iter_get_func_proto_t get_func_proto;
Yonghong Song3c32cc12020-05-13 11:02:21 -07001504 u32 ctx_arg_info_size;
Yonghong Songcf83b2d2020-10-27 23:10:54 -07001505 u32 feature;
Yonghong Song3c32cc12020-05-13 11:02:21 -07001506 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
Yonghong Song14fc6bd62020-07-23 11:41:09 -07001507 const struct bpf_iter_seq_info *seq_info;
Yonghong Songae243452020-05-09 10:58:59 -07001508};
1509
Yonghong Songe5158d92020-05-09 10:59:07 -07001510struct bpf_iter_meta {
1511 __bpf_md_ptr(struct seq_file *, seq);
1512 u64 session_id;
1513 u64 seq_num;
1514};
1515
Yonghong Songa5cbe052020-07-23 11:41:12 -07001516struct bpf_iter__bpf_map_elem {
1517 __bpf_md_ptr(struct bpf_iter_meta *, meta);
1518 __bpf_md_ptr(struct bpf_map *, map);
1519 __bpf_md_ptr(void *, key);
1520 __bpf_md_ptr(void *, value);
1521};
1522
Yonghong Song15172a42020-05-13 11:02:19 -07001523int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
Yonghong Songab2ee4f2020-05-13 11:02:20 -07001524void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
Yonghong Song15d83c42020-05-09 10:59:00 -07001525bool bpf_iter_prog_supported(struct bpf_prog *prog);
Martin KaFai Lau3cee6fb2021-07-01 13:06:19 -07001526const struct bpf_func_proto *
1527bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001528int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
Yonghong Songac51d992020-05-09 10:59:05 -07001529int bpf_iter_new_fd(struct bpf_link *link);
Yonghong Song367ec3e2020-05-09 10:59:06 -07001530bool bpf_link_is_iter(struct bpf_link *link);
Yonghong Songe5158d92020-05-09 10:59:07 -07001531struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1532int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
Yonghong Songb76f2222020-08-21 11:44:19 -07001533void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1534 struct seq_file *seq);
1535int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1536 struct bpf_link_info *info);
Yonghong Songae243452020-05-09 10:58:59 -07001537
Yonghong Song314ee052021-02-26 12:49:27 -08001538int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1539 struct bpf_func_state *caller,
1540 struct bpf_func_state *callee);
1541
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001542int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1543int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1544int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1545 u64 flags);
1546int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1547 u64 flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +02001548
Alexei Starovoitov557c0c62016-03-07 21:57:17 -08001549int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001550
Daniel Borkmannd056a782016-06-15 22:47:13 +02001551int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1552 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001553int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001554int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1555 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001556int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Daniel Borkmannd056a782016-06-15 22:47:13 +02001557
Chenbo Feng6e71b042017-10-18 13:00:22 -07001558int bpf_get_file_flag(int flags);
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001559int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07001560 size_t actual_size);
Chenbo Feng6e71b042017-10-18 13:00:22 -07001561
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001562/* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
1563 * forced to use 'long' read/writes to try to atomically copy long counters.
1564 * Best-effort only. No barriers here, since it _will_ race with concurrent
1565 * updates from BPF programs. Called from bpf syscall and mostly used with
1566 * size 8 or 16 bytes, so ask compiler to inline it.
1567 */
1568static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
1569{
1570 const long *lsrc = src;
1571 long *ldst = dst;
1572
1573 size /= sizeof(long);
1574 while (size--)
1575 *ldst++ = *lsrc++;
1576}
1577
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001578/* verify correctness of eBPF program */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001579int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
Andrii Nakryikoa643bff2021-01-11 23:55:14 -08001580
1581#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001582void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
Andrii Nakryikoa643bff2021-01-11 23:55:14 -08001583#endif
John Fastabend46f55cf2017-07-17 21:56:48 -07001584
Alan Maguire76654e62020-09-28 12:31:03 +01001585struct btf *bpf_get_btf_vmlinux(void);
1586
John Fastabend46f55cf2017-07-17 21:56:48 -07001587/* Map specifics */
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001588struct xdp_buff;
Toshiaki Makita6d5fc192018-06-14 11:07:42 +09001589struct sk_buff;
Björn Töpele6a47502021-03-08 12:29:06 +01001590struct bpf_dtab_netdev;
1591struct bpf_cpu_map_entry;
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001592
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001593void __dev_flush(void);
1594int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1595 struct net_device *dev_rx);
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +02001596int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1597 struct net_device *dev_rx);
Hangbin Liue624d4e2021-05-19 17:07:45 +08001598int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
1599 struct bpf_map *map, bool exclude_ingress);
Toshiaki Makita6d5fc192018-06-14 11:07:42 +09001600int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1601 struct bpf_prog *xdp_prog);
Hangbin Liue624d4e2021-05-19 17:07:45 +08001602int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
1603 struct bpf_prog *xdp_prog, struct bpf_map *map,
1604 bool exclude_ingress);
John Fastabend46f55cf2017-07-17 21:56:48 -07001605
Björn Töpelcdfafe92019-12-19 07:10:04 +01001606void __cpu_map_flush(void);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001607int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
1608 struct net_device *dev_rx);
Kumar Kartikeya Dwivedi11941f82021-07-02 16:48:23 +05301609int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
1610 struct sk_buff *skb);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001611
Martin KaFai Lau96eabe72017-08-18 11:28:00 -07001612/* Return map's numa specified by userspace */
1613static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1614{
1615 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1616 attr->numa_node : NUMA_NO_NODE;
1617}
1618
Al Viro040ee692017-12-02 20:20:38 -05001619struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -07001620int array_map_alloc_check(union bpf_attr *attr);
Al Viro040ee692017-12-02 20:20:38 -05001621
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001622int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1623 union bpf_attr __user *uattr);
1624int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1625 union bpf_attr __user *uattr);
KP Singhda00d2f2020-03-04 20:18:52 +01001626int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1627 const union bpf_attr *kattr,
1628 union bpf_attr __user *uattr);
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001629int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1630 const union bpf_attr *kattr,
1631 union bpf_attr __user *uattr);
Song Liu1b4d60e2020-09-25 13:54:29 -07001632int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
1633 const union bpf_attr *kattr,
1634 union bpf_attr __user *uattr);
Lorenz Bauer7c32e8f2021-03-03 10:18:13 +00001635int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1636 const union bpf_attr *kattr,
1637 union bpf_attr __user *uattr);
Martin KaFai Lau7bd15902021-03-24 18:52:52 -07001638bool bpf_prog_test_check_kfunc_call(u32 kfunc_id);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001639bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1640 const struct bpf_prog *prog,
1641 struct bpf_insn_access_aux *info);
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08001642int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001643 const struct btf_type *t, int off, int size,
1644 enum bpf_access_type atype,
1645 u32 *next_btf_id);
Jiri Olsafaaf4a72020-08-25 21:21:18 +02001646bool btf_struct_ids_match(struct bpf_verifier_log *log,
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08001647 const struct btf *btf, u32 id, int off,
1648 const struct btf *need_btf, u32 need_type_id);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001649
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08001650int btf_distill_func_proto(struct bpf_verifier_log *log,
1651 struct btf *btf,
1652 const struct btf_type *func_proto,
1653 const char *func_name,
1654 struct btf_func_model *m);
1655
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08001656struct bpf_reg_state;
Martin KaFai Lau34747c42021-03-24 18:51:36 -07001657int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
1658 struct bpf_reg_state *regs);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07001659int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
1660 const struct btf *btf, u32 func_id,
1661 struct bpf_reg_state *regs);
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08001662int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1663 struct bpf_reg_state *reg);
Toke Høiland-Jørgensenefc68152020-09-25 23:25:01 +02001664int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001665 struct btf *btf, const struct btf_type *t);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001666
Björn Töpel7e6897f2019-12-13 18:51:09 +01001667struct bpf_prog *bpf_prog_by_id(u32 id);
Alexei Starovoitov005142b2020-08-18 21:27:56 -07001668struct bpf_link *bpf_link_by_id(u32 id);
Björn Töpel7e6897f2019-12-13 18:51:09 +01001669
Stanislav Fomichev68908962020-04-24 16:59:41 -07001670const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
Song Liua10787e2021-02-25 15:43:14 -08001671void bpf_task_storage_free(struct task_struct *task);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07001672bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
1673const struct btf_func_model *
1674bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
1675 const struct bpf_insn *insn);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001676#else /* !CONFIG_BPF_SYSCALL */
Daniel Borkmann0fc174d2015-03-01 12:31:44 +01001677static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1678{
1679 return ERR_PTR(-EOPNOTSUPP);
1680}
1681
Jakub Kicinski248f3462017-11-03 13:56:20 -07001682static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1683 enum bpf_prog_type type,
Jakub Kicinski288b3de52017-11-20 15:21:54 -08001684 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001685{
1686 return ERR_PTR(-EOPNOTSUPP);
1687}
1688
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001689static inline void bpf_prog_add(struct bpf_prog *prog, int i)
Brenden Blancocc2e0b32016-07-20 07:55:52 -07001690{
Brenden Blancocc2e0b32016-07-20 07:55:52 -07001691}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001692
Daniel Borkmannc5405942016-11-09 22:02:34 +01001693static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1694{
1695}
1696
Daniel Borkmann0fc174d2015-03-01 12:31:44 +01001697static inline void bpf_prog_put(struct bpf_prog *prog)
1698{
1699}
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +01001700
Andrii Nakryiko85192dbf2019-11-17 09:28:03 -08001701static inline void bpf_prog_inc(struct bpf_prog *prog)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07001702{
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07001703}
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001704
John Fastabenda6f6df62017-08-15 22:32:22 -07001705static inline struct bpf_prog *__must_check
1706bpf_prog_inc_not_zero(struct bpf_prog *prog)
1707{
1708 return ERR_PTR(-EOPNOTSUPP);
1709}
1710
Andrii Nakryiko6cc7d1e2020-07-21 23:45:54 -07001711static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1712 const struct bpf_link_ops *ops,
1713 struct bpf_prog *prog)
1714{
1715}
1716
1717static inline int bpf_link_prime(struct bpf_link *link,
1718 struct bpf_link_primer *primer)
1719{
1720 return -EOPNOTSUPP;
1721}
1722
1723static inline int bpf_link_settle(struct bpf_link_primer *primer)
1724{
1725 return -EOPNOTSUPP;
1726}
1727
1728static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
1729{
1730}
1731
1732static inline void bpf_link_inc(struct bpf_link *link)
1733{
1734}
1735
1736static inline void bpf_link_put(struct bpf_link *link)
1737{
1738}
1739
Chenbo Feng6e71b042017-10-18 13:00:22 -07001740static inline int bpf_obj_get_user(const char __user *pathname, int flags)
Shmulik Ladkani98589a02017-10-09 15:27:15 +03001741{
1742 return -EOPNOTSUPP;
1743}
1744
David Ahernfbee97f2020-05-29 16:07:13 -06001745static inline bool dev_map_can_have_prog(struct bpf_map *map)
1746{
1747 return false;
1748}
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +02001749
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001750static inline void __dev_flush(void)
John Fastabend46f55cf2017-07-17 21:56:48 -07001751{
1752}
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001753
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001754struct xdp_buff;
1755struct bpf_dtab_netdev;
Björn Töpele6a47502021-03-08 12:29:06 +01001756struct bpf_cpu_map_entry;
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001757
1758static inline
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001759int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1760 struct net_device *dev_rx)
1761{
1762 return 0;
1763}
1764
1765static inline
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +02001766int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1767 struct net_device *dev_rx)
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +02001768{
1769 return 0;
1770}
1771
Hangbin Liue624d4e2021-05-19 17:07:45 +08001772static inline
1773int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
1774 struct bpf_map *map, bool exclude_ingress)
1775{
1776 return 0;
1777}
1778
Toshiaki Makita6d5fc192018-06-14 11:07:42 +09001779struct sk_buff;
1780
1781static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
1782 struct sk_buff *skb,
1783 struct bpf_prog *xdp_prog)
1784{
1785 return 0;
1786}
1787
Hangbin Liue624d4e2021-05-19 17:07:45 +08001788static inline
1789int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
1790 struct bpf_prog *xdp_prog, struct bpf_map *map,
1791 bool exclude_ingress)
1792{
1793 return 0;
1794}
1795
Björn Töpelcdfafe92019-12-19 07:10:04 +01001796static inline void __cpu_map_flush(void)
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001797{
1798}
1799
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001800static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1801 struct xdp_buff *xdp,
1802 struct net_device *dev_rx)
1803{
1804 return 0;
1805}
Al Viro040ee692017-12-02 20:20:38 -05001806
Kumar Kartikeya Dwivedi11941f82021-07-02 16:48:23 +05301807static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
1808 struct sk_buff *skb)
1809{
1810 return -EOPNOTSUPP;
1811}
1812
Lorenzo Bianconi92164772020-07-14 15:56:38 +02001813static inline bool cpu_map_prog_allowed(struct bpf_map *map)
1814{
1815 return false;
1816}
1817
Al Viro040ee692017-12-02 20:20:38 -05001818static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1819 enum bpf_prog_type type)
1820{
1821 return ERR_PTR(-EOPNOTSUPP);
1822}
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001823
1824static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1825 const union bpf_attr *kattr,
1826 union bpf_attr __user *uattr)
1827{
1828 return -ENOTSUPP;
1829}
1830
1831static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1832 const union bpf_attr *kattr,
1833 union bpf_attr __user *uattr)
1834{
1835 return -ENOTSUPP;
1836}
1837
KP Singhda00d2f2020-03-04 20:18:52 +01001838static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1839 const union bpf_attr *kattr,
1840 union bpf_attr __user *uattr)
1841{
1842 return -ENOTSUPP;
1843}
1844
Stanislav Fomichevc6958652019-04-11 09:12:02 -07001845static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1846 const union bpf_attr *kattr,
1847 union bpf_attr __user *uattr)
1848{
1849 return -ENOTSUPP;
1850}
Daniel Borkmann6332be02019-11-22 21:07:55 +01001851
Lorenz Bauer7c32e8f2021-03-03 10:18:13 +00001852static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1853 const union bpf_attr *kattr,
1854 union bpf_attr __user *uattr)
1855{
1856 return -ENOTSUPP;
1857}
1858
Martin KaFai Lau7bd15902021-03-24 18:52:52 -07001859static inline bool bpf_prog_test_check_kfunc_call(u32 kfunc_id)
1860{
1861 return false;
1862}
1863
Daniel Borkmann6332be02019-11-22 21:07:55 +01001864static inline void bpf_map_put(struct bpf_map *map)
1865{
1866}
Björn Töpel7e6897f2019-12-13 18:51:09 +01001867
1868static inline struct bpf_prog *bpf_prog_by_id(u32 id)
1869{
1870 return ERR_PTR(-ENOTSUPP);
1871}
Stanislav Fomichev68908962020-04-24 16:59:41 -07001872
1873static inline const struct bpf_func_proto *
1874bpf_base_func_proto(enum bpf_func_id func_id)
1875{
1876 return NULL;
1877}
Song Liua10787e2021-02-25 15:43:14 -08001878
1879static inline void bpf_task_storage_free(struct task_struct *task)
1880{
1881}
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07001882
1883static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
1884{
1885 return false;
1886}
1887
1888static inline const struct btf_func_model *
1889bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
1890 const struct bpf_insn *insn)
1891{
1892 return NULL;
1893}
Daniel Borkmann61e021f32015-03-02 15:21:55 +01001894#endif /* CONFIG_BPF_SYSCALL */
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001895
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -08001896void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
1897 struct btf_mod_pair *used_btfs, u32 len);
1898
Jakub Kicinski479321e2017-11-20 15:21:56 -08001899static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1900 enum bpf_prog_type type)
1901{
1902 return bpf_prog_get_type_dev(ufd, type, false);
1903}
1904
Andrii Nakryiko936f8942021-01-11 23:55:16 -08001905void __bpf_free_used_maps(struct bpf_prog_aux *aux,
1906 struct bpf_map **used_maps, u32 len);
1907
Al Viro040ee692017-12-02 20:20:38 -05001908bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1909
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001910int bpf_prog_offload_compile(struct bpf_prog *prog);
1911void bpf_prog_offload_destroy(struct bpf_prog *prog);
Jakub Kicinski675fc272017-12-27 18:39:09 -08001912int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1913 struct bpf_prog *prog);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001914
Jakub Kicinski52775b32018-01-17 19:13:28 -08001915int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1916
Jakub Kicinskia3884572018-01-11 20:29:09 -08001917int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1918int bpf_map_offload_update_elem(struct bpf_map *map,
1919 void *key, void *value, u64 flags);
1920int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1921int bpf_map_offload_get_next_key(struct bpf_map *map,
1922 void *key, void *next_key);
1923
Jakub Kicinski09728262018-07-17 10:53:23 -07001924bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001925
Quentin Monnet1385d752018-11-09 13:03:25 +00001926struct bpf_offload_dev *
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -08001927bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
Jakub Kicinski602144c2018-07-17 10:53:25 -07001928void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
Jakub Kicinskidd27c2e2019-02-12 00:20:39 -08001929void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
Jakub Kicinski602144c2018-07-17 10:53:25 -07001930int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1931 struct net_device *netdev);
1932void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1933 struct net_device *netdev);
Jakub Kicinskifd4f2272018-07-17 10:53:26 -07001934bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -07001935
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001936#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1937int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1938
Jakub Kicinski0d830032018-05-08 19:37:06 -07001939static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001940{
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001941 return aux->offload_requested;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001942}
Jakub Kicinskia3884572018-01-11 20:29:09 -08001943
1944static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1945{
1946 return unlikely(map->ops == &bpf_map_offload_ops);
1947}
1948
1949struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1950void bpf_map_offload_map_free(struct bpf_map *map);
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07001951int bpf_prog_test_run_syscall(struct bpf_prog *prog,
1952 const union bpf_attr *kattr,
1953 union bpf_attr __user *uattr);
Cong Wang17edea22021-07-04 12:02:42 -07001954
1955int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1956int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
1957int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
1958void sock_map_unhash(struct sock *sk);
1959void sock_map_close(struct sock *sk, long timeout);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001960#else
1961static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1962 union bpf_attr *attr)
1963{
1964 return -EOPNOTSUPP;
1965}
1966
1967static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1968{
1969 return false;
1970}
Jakub Kicinskia3884572018-01-11 20:29:09 -08001971
1972static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1973{
1974 return false;
1975}
1976
1977static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1978{
1979 return ERR_PTR(-EOPNOTSUPP);
1980}
1981
1982static inline void bpf_map_offload_map_free(struct bpf_map *map)
1983{
1984}
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07001985
1986static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
1987 const union bpf_attr *kattr,
1988 union bpf_attr __user *uattr)
1989{
1990 return -ENOTSUPP;
1991}
Sean Youngfdb5c452018-06-19 00:04:24 +01001992
Cong Wang88759602021-02-23 10:49:26 -08001993#ifdef CONFIG_BPF_SYSCALL
Daniel Borkmann604326b2018-10-13 02:45:58 +02001994static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1995 struct bpf_prog *prog)
Sean Youngfdb5c452018-06-19 00:04:24 +01001996{
1997 return -EINVAL;
1998}
Lorenz Bauerbb0de312020-06-29 10:56:28 +01001999
2000static inline int sock_map_prog_detach(const union bpf_attr *attr,
2001 enum bpf_prog_type ptype)
2002{
2003 return -EOPNOTSUPP;
2004}
Lorenz Bauer13b79d32020-08-21 11:29:45 +01002005
2006static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
2007 u64 flags)
2008{
2009 return -EOPNOTSUPP;
2010}
Cong Wang17edea22021-07-04 12:02:42 -07002011#endif /* CONFIG_BPF_SYSCALL */
2012#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
John Fastabend6bdc9c42017-08-16 15:02:32 -07002013
Cong Wang17edea22021-07-04 12:02:42 -07002014#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
2015void bpf_sk_reuseport_detach(struct sock *sk);
2016int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
2017 void *value);
2018int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
2019 void *value, u64 map_flags);
2020#else
2021static inline void bpf_sk_reuseport_detach(struct sock *sk)
2022{
2023}
2024
2025#ifdef CONFIG_BPF_SYSCALL
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -07002026static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
2027 void *key, void *value)
2028{
2029 return -EOPNOTSUPP;
2030}
2031
2032static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
2033 void *key, void *value,
2034 u64 map_flags)
2035{
2036 return -EOPNOTSUPP;
2037}
2038#endif /* CONFIG_BPF_SYSCALL */
2039#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
2040
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08002041/* verifier prototypes for helper functions called from eBPF programs */
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01002042extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
2043extern const struct bpf_func_proto bpf_map_update_elem_proto;
2044extern const struct bpf_func_proto bpf_map_delete_elem_proto;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002045extern const struct bpf_func_proto bpf_map_push_elem_proto;
2046extern const struct bpf_func_proto bpf_map_pop_elem_proto;
2047extern const struct bpf_func_proto bpf_map_peek_elem_proto;
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08002048
Daniel Borkmann03e69b52015-03-14 02:27:16 +01002049extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01002050extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02002051extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002052extern const struct bpf_func_proto bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02002053extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
Maciej Żenczykowski71d19212020-04-26 09:15:25 -07002054extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07002055extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
2056extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
2057extern const struct bpf_func_proto bpf_get_current_comm_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08002058extern const struct bpf_func_proto bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -07002059extern const struct bpf_func_proto bpf_get_stack_proto;
Song Liufa28dcb2020-06-29 23:28:44 -07002060extern const struct bpf_func_proto bpf_get_task_stack_proto;
Song Liu7b04d6d2020-07-23 11:06:44 -07002061extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
2062extern const struct bpf_func_proto bpf_get_stack_proto_pe;
John Fastabend174a79f2017-08-15 22:32:47 -07002063extern const struct bpf_func_proto bpf_sock_map_update_proto;
John Fastabend81110382018-05-14 10:00:17 -07002064extern const struct bpf_func_proto bpf_sock_hash_update_proto;
Yonghong Songbf6fa2c82018-06-03 15:59:41 -07002065extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
Daniel Borkmann0f09abd2020-03-27 16:58:54 +01002066extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
Daniel Borkmann604326b2018-10-13 02:45:58 +02002067extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
2068extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
2069extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
2070extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002071extern const struct bpf_func_proto bpf_spin_lock_proto;
2072extern const struct bpf_func_proto bpf_spin_unlock_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07002073extern const struct bpf_func_proto bpf_get_local_storage_proto;
Andrey Ignatovd7a4cb92019-03-18 17:55:26 -07002074extern const struct bpf_func_proto bpf_strtol_proto;
2075extern const struct bpf_func_proto bpf_strtoul_proto;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002076extern const struct bpf_func_proto bpf_tcp_sock_proto;
Martin KaFai Lau5576b992020-01-22 15:36:46 -08002077extern const struct bpf_func_proto bpf_jiffies64_proto;
Carlos Neirab4490c52020-03-04 17:41:56 -03002078extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
Stanislav Fomichev0456ea12020-04-20 10:46:10 -07002079extern const struct bpf_func_proto bpf_event_output_data_proto;
Andrii Nakryiko457f44362020-05-29 00:54:20 -07002080extern const struct bpf_func_proto bpf_ringbuf_output_proto;
2081extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
2082extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
2083extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
2084extern const struct bpf_func_proto bpf_ringbuf_query_proto;
Yonghong Songaf7ec132020-06-23 16:08:09 -07002085extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
Yonghong Song478cfbd2020-06-23 16:08:11 -07002086extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
2087extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
2088extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
Yonghong Song0d4fad3e52020-06-23 16:08:15 -07002089extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
Alexei Starovoitov07be4c42020-08-27 15:01:12 -07002090extern const struct bpf_func_proto bpf_copy_from_user_proto;
Alan Maguirec4d0bfb2020-09-28 12:31:05 +01002091extern const struct bpf_func_proto bpf_snprintf_btf_proto;
Florent Revest7b155232021-04-19 17:52:40 +02002092extern const struct bpf_func_proto bpf_snprintf_proto;
Hao Luoeaa6bcb2020-09-29 16:50:47 -07002093extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
Hao Luo63d9b802020-09-29 16:50:48 -07002094extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
Dmitrii Banshchikovd0551262020-11-17 18:45:49 +00002095extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
Florent Revestb60da492020-12-08 18:36:23 +01002096extern const struct bpf_func_proto bpf_sock_from_file_proto;
Florent Revestc5dbb892021-02-10 12:14:03 +01002097extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
Song Liua10787e2021-02-25 15:43:14 -08002098extern const struct bpf_func_proto bpf_task_storage_get_proto;
2099extern const struct bpf_func_proto bpf_task_storage_delete_proto;
Yonghong Song69c087b2021-02-26 12:49:25 -08002100extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
Alexei Starovoitov3d784172021-05-13 17:36:11 -07002101extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
Martin KaFai Lau3cee6fb2021-07-01 13:06:19 -07002102extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
2103extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07002104
Jiri Olsa958a3f22020-05-31 17:42:55 +02002105const struct bpf_func_proto *tracing_prog_func_proto(
2106 enum bpf_func_id func_id, const struct bpf_prog *prog);
2107
Daniel Borkmann3ad00402015-10-08 01:20:39 +02002108/* Shared helpers among cBPF and eBPF. */
2109void bpf_user_rnd_init_once(void);
2110u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Stanislav Fomichev68908962020-04-24 16:59:41 -07002111u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Daniel Borkmann3ad00402015-10-08 01:20:39 +02002112
Joe Stringerc64b7982018-10-02 13:35:33 -07002113#if defined(CONFIG_NET)
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002114bool bpf_sock_common_is_valid_access(int off, int size,
2115 enum bpf_access_type type,
2116 struct bpf_insn_access_aux *info);
Joe Stringerc64b7982018-10-02 13:35:33 -07002117bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2118 struct bpf_insn_access_aux *info);
2119u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2120 const struct bpf_insn *si,
2121 struct bpf_insn *insn_buf,
2122 struct bpf_prog *prog,
2123 u32 *target_size);
2124#else
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002125static inline bool bpf_sock_common_is_valid_access(int off, int size,
2126 enum bpf_access_type type,
2127 struct bpf_insn_access_aux *info)
2128{
2129 return false;
2130}
Joe Stringerc64b7982018-10-02 13:35:33 -07002131static inline bool bpf_sock_is_valid_access(int off, int size,
2132 enum bpf_access_type type,
2133 struct bpf_insn_access_aux *info)
2134{
2135 return false;
2136}
2137static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2138 const struct bpf_insn *si,
2139 struct bpf_insn *insn_buf,
2140 struct bpf_prog *prog,
2141 u32 *target_size)
2142{
2143 return 0;
2144}
2145#endif
2146
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08002147#ifdef CONFIG_INET
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08002148struct sk_reuseport_kern {
2149 struct sk_buff *skb;
2150 struct sock *sk;
2151 struct sock *selected_sk;
Kuniyuki Iwashimad5e4ddaeb2021-06-12 21:32:22 +09002152 struct sock *migrating_sk;
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08002153 void *data_end;
2154 u32 hash;
2155 u32 reuseport_id;
2156 bool bind_inany;
2157};
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08002158bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2159 struct bpf_insn_access_aux *info);
2160
2161u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2162 const struct bpf_insn *si,
2163 struct bpf_insn *insn_buf,
2164 struct bpf_prog *prog,
2165 u32 *target_size);
YueHaibing7f942082019-06-12 17:18:47 +08002166
2167bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2168 struct bpf_insn_access_aux *info);
2169
2170u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2171 const struct bpf_insn *si,
2172 struct bpf_insn *insn_buf,
2173 struct bpf_prog *prog,
2174 u32 *target_size);
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08002175#else
2176static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
2177 enum bpf_access_type type,
2178 struct bpf_insn_access_aux *info)
2179{
2180 return false;
2181}
2182
2183static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2184 const struct bpf_insn *si,
2185 struct bpf_insn *insn_buf,
2186 struct bpf_prog *prog,
2187 u32 *target_size)
2188{
2189 return 0;
2190}
YueHaibing7f942082019-06-12 17:18:47 +08002191static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
2192 enum bpf_access_type type,
2193 struct bpf_insn_access_aux *info)
2194{
2195 return false;
2196}
2197
2198static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2199 const struct bpf_insn *si,
2200 struct bpf_insn *insn_buf,
2201 struct bpf_prog *prog,
2202 u32 *target_size)
2203{
2204 return 0;
2205}
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08002206#endif /* CONFIG_INET */
2207
Alexei Starovoitov5964b202019-11-14 10:57:03 -08002208enum bpf_text_poke_type {
Daniel Borkmannb553a6e2019-11-24 01:39:42 +01002209 BPF_MOD_CALL,
2210 BPF_MOD_JUMP,
Alexei Starovoitov5964b202019-11-14 10:57:03 -08002211};
Daniel Borkmann4b3da772019-11-22 21:07:54 +01002212
Alexei Starovoitov5964b202019-11-14 10:57:03 -08002213int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2214 void *addr1, void *addr2);
2215
Jiri Olsaeae2e832020-08-25 21:21:19 +02002216struct btf_id_set;
Lorenz Bauer2af30f12020-09-21 13:12:17 +01002217bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
Jiri Olsaeae2e832020-08-25 21:21:19 +02002218
Dave Marchevsky335ff492021-09-17 11:29:03 -07002219#define MAX_BPRINTF_VARARGS 12
2220
Florent Revest48cac3f2021-04-27 19:43:13 +02002221int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
2222 u32 **bin_buf, u32 num_args);
2223void bpf_bprintf_cleanup(void);
Florent Revestd9c9e4d2021-04-19 17:52:38 +02002224
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002225#endif /* _LINUX_BPF_H */