Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3 | */ |
| 4 | #ifndef _LINUX_BPF_H |
| 5 | #define _LINUX_BPF_H 1 |
| 6 | |
| 7 | #include <uapi/linux/bpf.h> |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 8 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 9 | #include <linux/workqueue.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 10 | #include <linux/file.h> |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 11 | #include <linux/percpu.h> |
Zi Shen Lim | 002245c | 2016-06-08 21:18:47 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 13 | #include <linux/rbtree_latch.h> |
David S. Miller | d6e1e46 | 2017-08-19 23:34:03 -0700 | [diff] [blame] | 14 | #include <linux/numa.h> |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 15 | #include <linux/mm_types.h> |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 16 | #include <linux/wait.h> |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 17 | #include <linux/u64_stats_sync.h> |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 18 | #include <linux/refcount.h> |
| 19 | #include <linux/mutex.h> |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 20 | #include <linux/module.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 21 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 22 | struct bpf_verifier_env; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 23 | struct bpf_verifier_log; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 24 | struct perf_event; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 25 | struct bpf_prog; |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 26 | struct bpf_prog_aux; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 27 | struct bpf_map; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 28 | struct sock; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 29 | struct seq_file; |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 30 | struct btf; |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 31 | struct btf_type; |
Alexei Starovoitov | 3dec541 | 2019-10-15 20:25:03 -0700 | [diff] [blame] | 32 | struct exception_table_entry; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 33 | |
Quentin Monnet | 1b9ed84 | 2019-08-20 10:31:50 +0100 | [diff] [blame] | 34 | extern struct idr btf_idr; |
| 35 | extern spinlock_t btf_idr_lock; |
| 36 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 37 | /* map is generic key/value storage optionally accesible by eBPF programs */ |
| 38 | struct bpf_map_ops { |
| 39 | /* funcs callable from userspace (via syscall) */ |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 40 | int (*map_alloc_check)(union bpf_attr *attr); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 41 | struct bpf_map *(*map_alloc)(union bpf_attr *attr); |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 42 | void (*map_release)(struct bpf_map *map, struct file *map_file); |
| 43 | void (*map_free)(struct bpf_map *map); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 44 | int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key); |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 45 | void (*map_release_uref)(struct bpf_map *map); |
Daniel Borkmann | c611022 | 2019-05-14 01:18:55 +0200 | [diff] [blame] | 46 | void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 47 | int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr, |
| 48 | union bpf_attr __user *uattr); |
Yonghong Song | 0579963 | 2020-01-15 10:43:04 -0800 | [diff] [blame] | 49 | int (*map_lookup_and_delete_batch)(struct bpf_map *map, |
| 50 | const union bpf_attr *attr, |
| 51 | union bpf_attr __user *uattr); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 52 | int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr, |
| 53 | union bpf_attr __user *uattr); |
| 54 | int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr, |
| 55 | union bpf_attr __user *uattr); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 56 | |
| 57 | /* funcs callable from userspace and from eBPF programs */ |
| 58 | void *(*map_lookup_elem)(struct bpf_map *map, void *key); |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 59 | int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 60 | int (*map_delete_elem)(struct bpf_map *map, void *key); |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 61 | int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags); |
| 62 | int (*map_pop_elem)(struct bpf_map *map, void *value); |
| 63 | int (*map_peek_elem)(struct bpf_map *map, void *value); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 64 | |
| 65 | /* funcs called by prog_array and perf_event_array map */ |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 66 | void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file, |
| 67 | int fd); |
| 68 | void (*map_fd_put_ptr)(void *ptr); |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 69 | u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 70 | u32 (*map_fd_sys_lookup_elem)(void *ptr); |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 71 | void (*map_seq_show_elem)(struct bpf_map *map, void *key, |
| 72 | struct seq_file *m); |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 73 | int (*map_check_btf)(const struct bpf_map *map, |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 74 | const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 75 | const struct btf_type *key_type, |
| 76 | const struct btf_type *value_type); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 77 | |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 78 | /* Prog poke tracking helpers. */ |
| 79 | int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux); |
| 80 | void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux); |
| 81 | void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old, |
| 82 | struct bpf_prog *new); |
| 83 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 84 | /* Direct value access helpers. */ |
| 85 | int (*map_direct_value_addr)(const struct bpf_map *map, |
| 86 | u64 *imm, u32 off); |
| 87 | int (*map_direct_value_meta)(const struct bpf_map *map, |
| 88 | u64 imm, u32 *off); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 89 | int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 92 | struct bpf_map_memory { |
| 93 | u32 pages; |
| 94 | struct user_struct *user; |
| 95 | }; |
| 96 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 97 | struct bpf_map { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 98 | /* The first two cachelines with read-mostly members of which some |
Daniel Borkmann | be95a84 | 2018-01-09 13:17:44 +0100 | [diff] [blame] | 99 | * are also accessed in fast-path (e.g. ops, max_entries). |
| 100 | */ |
| 101 | const struct bpf_map_ops *ops ____cacheline_aligned; |
| 102 | struct bpf_map *inner_map_meta; |
| 103 | #ifdef CONFIG_SECURITY |
| 104 | void *security; |
| 105 | #endif |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 106 | enum bpf_map_type map_type; |
| 107 | u32 key_size; |
| 108 | u32 value_size; |
| 109 | u32 max_entries; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 110 | u32 map_flags; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 111 | int spin_lock_off; /* >=0 valid offset, <0 error */ |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 112 | u32 id; |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 113 | int numa_node; |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 114 | u32 btf_key_type_id; |
| 115 | u32 btf_value_type_id; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 116 | struct btf *btf; |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 117 | struct bpf_map_memory memory; |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 118 | char name[BPF_OBJ_NAME_LEN]; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 119 | u32 btf_vmlinux_value_type_id; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 120 | bool unpriv_array; |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 121 | bool frozen; /* write-once; write-protected by freeze_mutex */ |
| 122 | /* 22 bytes hole */ |
Daniel Borkmann | be95a84 | 2018-01-09 13:17:44 +0100 | [diff] [blame] | 123 | |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 124 | /* The 3rd and 4th cacheline with misc members to avoid false sharing |
Daniel Borkmann | be95a84 | 2018-01-09 13:17:44 +0100 | [diff] [blame] | 125 | * particularly with refcounting. |
| 126 | */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 127 | atomic64_t refcnt ____cacheline_aligned; |
| 128 | atomic64_t usercnt; |
Daniel Borkmann | be95a84 | 2018-01-09 13:17:44 +0100 | [diff] [blame] | 129 | struct work_struct work; |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 130 | struct mutex freeze_mutex; |
| 131 | u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */ |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 134 | static inline bool map_value_has_spin_lock(const struct bpf_map *map) |
| 135 | { |
| 136 | return map->spin_lock_off >= 0; |
| 137 | } |
| 138 | |
| 139 | static inline void check_and_init_map_lock(struct bpf_map *map, void *dst) |
| 140 | { |
| 141 | if (likely(!map_value_has_spin_lock(map))) |
| 142 | return; |
| 143 | *(struct bpf_spin_lock *)(dst + map->spin_lock_off) = |
| 144 | (struct bpf_spin_lock){}; |
| 145 | } |
| 146 | |
| 147 | /* copy everything but bpf_spin_lock */ |
| 148 | static inline void copy_map_value(struct bpf_map *map, void *dst, void *src) |
| 149 | { |
| 150 | if (unlikely(map_value_has_spin_lock(map))) { |
| 151 | u32 off = map->spin_lock_off; |
| 152 | |
| 153 | memcpy(dst, src, off); |
| 154 | memcpy(dst + off + sizeof(struct bpf_spin_lock), |
| 155 | src + off + sizeof(struct bpf_spin_lock), |
| 156 | map->value_size - off - sizeof(struct bpf_spin_lock)); |
| 157 | } else { |
| 158 | memcpy(dst, src, map->value_size); |
| 159 | } |
| 160 | } |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 161 | void copy_map_value_locked(struct bpf_map *map, void *dst, void *src, |
| 162 | bool lock_src); |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 163 | |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 164 | struct bpf_offload_dev; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 165 | struct bpf_offloaded_map; |
| 166 | |
| 167 | struct bpf_map_dev_ops { |
| 168 | int (*map_get_next_key)(struct bpf_offloaded_map *map, |
| 169 | void *key, void *next_key); |
| 170 | int (*map_lookup_elem)(struct bpf_offloaded_map *map, |
| 171 | void *key, void *value); |
| 172 | int (*map_update_elem)(struct bpf_offloaded_map *map, |
| 173 | void *key, void *value, u64 flags); |
| 174 | int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key); |
| 175 | }; |
| 176 | |
| 177 | struct bpf_offloaded_map { |
| 178 | struct bpf_map map; |
| 179 | struct net_device *netdev; |
| 180 | const struct bpf_map_dev_ops *dev_ops; |
| 181 | void *dev_priv; |
| 182 | struct list_head offloads; |
| 183 | }; |
| 184 | |
| 185 | static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map) |
| 186 | { |
| 187 | return container_of(map, struct bpf_offloaded_map, map); |
| 188 | } |
| 189 | |
Jakub Kicinski | 0cd3cbe | 2018-05-03 18:37:08 -0700 | [diff] [blame] | 190 | static inline bool bpf_map_offload_neutral(const struct bpf_map *map) |
| 191 | { |
| 192 | return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY; |
| 193 | } |
| 194 | |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 195 | static inline bool bpf_map_support_seq_show(const struct bpf_map *map) |
| 196 | { |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 197 | return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) && |
| 198 | map->ops->map_seq_show_elem; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 201 | int map_check_no_btf(const struct bpf_map *map, |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 202 | const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 203 | const struct btf_type *key_type, |
| 204 | const struct btf_type *value_type); |
| 205 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 206 | extern const struct bpf_map_ops bpf_map_offload_ops; |
| 207 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 208 | /* function argument constraints */ |
| 209 | enum bpf_arg_type { |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 210 | ARG_DONTCARE = 0, /* unused argument in helper function */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 211 | |
| 212 | /* the following constraints used to prototype |
| 213 | * bpf_map_lookup/update/delete_elem() functions |
| 214 | */ |
| 215 | ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */ |
| 216 | ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */ |
| 217 | ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */ |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 218 | ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */ |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 219 | ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 220 | |
| 221 | /* the following constraints used to prototype bpf_memcmp() and other |
| 222 | * functions that access data on eBPF program stack |
| 223 | */ |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 224 | ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */ |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 225 | ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */ |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 226 | ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized, |
| 227 | * helper function must fill all bytes or clear |
| 228 | * them in error case. |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 229 | */ |
| 230 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 231 | ARG_CONST_SIZE, /* number of bytes accessed from memory */ |
| 232 | ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */ |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 233 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 234 | ARG_PTR_TO_CTX, /* pointer to context */ |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 235 | ARG_ANYTHING, /* any (initialized) argument is ok */ |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 236 | ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 237 | ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */ |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 238 | ARG_PTR_TO_INT, /* pointer to int */ |
| 239 | ARG_PTR_TO_LONG, /* pointer to long */ |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 240 | ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */ |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 241 | ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | /* type of values returned from helper functions */ |
| 245 | enum bpf_return_type { |
| 246 | RET_INTEGER, /* function returns integer */ |
| 247 | RET_VOID, /* function doesn't return anything */ |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 248 | RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 249 | RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */ |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 250 | RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */ |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 251 | RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */ |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 252 | RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 255 | /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs |
| 256 | * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL |
| 257 | * instructions after verifying |
| 258 | */ |
| 259 | struct bpf_func_proto { |
| 260 | u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
| 261 | bool gpl_only; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 262 | bool pkt_access; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 263 | enum bpf_return_type ret_type; |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 264 | union { |
| 265 | struct { |
| 266 | enum bpf_arg_type arg1_type; |
| 267 | enum bpf_arg_type arg2_type; |
| 268 | enum bpf_arg_type arg3_type; |
| 269 | enum bpf_arg_type arg4_type; |
| 270 | enum bpf_arg_type arg5_type; |
| 271 | }; |
| 272 | enum bpf_arg_type arg_type[5]; |
| 273 | }; |
Alexei Starovoitov | 9cc31b3 | 2019-11-14 10:57:14 -0800 | [diff] [blame] | 274 | int *btf_id; /* BTF ids of arguments */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /* bpf_context is intentionally undefined structure. Pointer to bpf_context is |
| 278 | * the first argument to eBPF programs. |
| 279 | * For socket filters: 'struct bpf_context *' == 'struct sk_buff *' |
| 280 | */ |
| 281 | struct bpf_context; |
| 282 | |
| 283 | enum bpf_access_type { |
| 284 | BPF_READ = 1, |
| 285 | BPF_WRITE = 2 |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 286 | }; |
| 287 | |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 288 | /* types of values stored in eBPF registers */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 289 | /* Pointer types represent: |
| 290 | * pointer |
| 291 | * pointer + imm |
| 292 | * pointer + (u16) var |
| 293 | * pointer + (u16) var + imm |
| 294 | * if (range > 0) then [ptr, ptr + range - off) is safe to access |
| 295 | * if (id > 0) means that some 'var' was added |
| 296 | * if (off > 0) means that 'imm' was added |
| 297 | */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 298 | enum bpf_reg_type { |
| 299 | NOT_INIT = 0, /* nothing was written into register */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 300 | SCALAR_VALUE, /* reg doesn't contain a valid pointer */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 301 | PTR_TO_CTX, /* reg points to bpf_context */ |
| 302 | CONST_PTR_TO_MAP, /* reg points to struct bpf_map */ |
| 303 | PTR_TO_MAP_VALUE, /* reg points to map element value */ |
| 304 | PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 305 | PTR_TO_STACK, /* reg == frame_pointer + offset */ |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 306 | PTR_TO_PACKET_META, /* skb->data - meta_len */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 307 | PTR_TO_PACKET, /* reg points to skb->data */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 308 | PTR_TO_PACKET_END, /* skb->data + headlen */ |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 309 | PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */ |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 310 | PTR_TO_SOCKET, /* reg points to struct bpf_sock */ |
| 311 | PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 312 | PTR_TO_SOCK_COMMON, /* reg points to sock_common */ |
| 313 | PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */ |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 314 | PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */ |
| 315 | PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */ |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 316 | PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */ |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 317 | PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */ |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 318 | PTR_TO_BTF_ID, /* reg points to kernel struct */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 321 | /* The information passed from prog-specific *_is_valid_access |
| 322 | * back to the verifier. |
| 323 | */ |
| 324 | struct bpf_insn_access_aux { |
| 325 | enum bpf_reg_type reg_type; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 326 | union { |
| 327 | int ctx_field_size; |
| 328 | u32 btf_id; |
| 329 | }; |
| 330 | struct bpf_verifier_log *log; /* for verbose logs */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 333 | static inline void |
| 334 | bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size) |
| 335 | { |
| 336 | aux->ctx_field_size = size; |
| 337 | } |
| 338 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 339 | struct bpf_prog_ops { |
| 340 | int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 341 | union bpf_attr __user *uattr); |
| 342 | }; |
| 343 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 344 | struct bpf_verifier_ops { |
| 345 | /* return eBPF function prototype for verification */ |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 346 | const struct bpf_func_proto * |
| 347 | (*get_func_proto)(enum bpf_func_id func_id, |
| 348 | const struct bpf_prog *prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 349 | |
| 350 | /* return true if 'size' wide access at offset 'off' within bpf_context |
| 351 | * with 'type' (read or write) is allowed |
| 352 | */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 353 | bool (*is_valid_access)(int off, int size, enum bpf_access_type type, |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 354 | const struct bpf_prog *prog, |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 355 | struct bpf_insn_access_aux *info); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 356 | int (*gen_prologue)(struct bpf_insn *insn, bool direct_write, |
| 357 | const struct bpf_prog *prog); |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 358 | int (*gen_ld_abs)(const struct bpf_insn *orig, |
| 359 | struct bpf_insn *insn_buf); |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 360 | u32 (*convert_ctx_access)(enum bpf_access_type type, |
| 361 | const struct bpf_insn *src, |
| 362 | struct bpf_insn *dst, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 363 | struct bpf_prog *prog, u32 *target_size); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 364 | int (*btf_struct_access)(struct bpf_verifier_log *log, |
| 365 | const struct btf_type *t, int off, int size, |
| 366 | enum bpf_access_type atype, |
| 367 | u32 *next_btf_id); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 370 | struct bpf_prog_offload_ops { |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 371 | /* verifier basic callbacks */ |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 372 | int (*insn_hook)(struct bpf_verifier_env *env, |
| 373 | int insn_idx, int prev_insn_idx); |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 374 | int (*finalize)(struct bpf_verifier_env *env); |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 375 | /* verifier optimization callbacks (called after .finalize) */ |
| 376 | int (*replace_insn)(struct bpf_verifier_env *env, u32 off, |
| 377 | struct bpf_insn *insn); |
| 378 | int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt); |
| 379 | /* program management callbacks */ |
Quentin Monnet | 16a8cb5c | 2018-11-09 13:03:32 +0000 | [diff] [blame] | 380 | int (*prepare)(struct bpf_prog *prog); |
| 381 | int (*translate)(struct bpf_prog *prog); |
Quentin Monnet | eb91194 | 2018-11-09 13:03:30 +0000 | [diff] [blame] | 382 | void (*destroy)(struct bpf_prog *prog); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 383 | }; |
| 384 | |
Jakub Kicinski | 0a9c199 | 2018-01-11 20:29:07 -0800 | [diff] [blame] | 385 | struct bpf_prog_offload { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 386 | struct bpf_prog *prog; |
| 387 | struct net_device *netdev; |
Quentin Monnet | 341b3e7 | 2018-11-09 13:03:26 +0000 | [diff] [blame] | 388 | struct bpf_offload_dev *offdev; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 389 | void *dev_priv; |
| 390 | struct list_head offloads; |
| 391 | bool dev_state; |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 392 | bool opt_failed; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 393 | void *jited_image; |
| 394 | u32 jited_len; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 395 | }; |
| 396 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 397 | enum bpf_cgroup_storage_type { |
| 398 | BPF_CGROUP_STORAGE_SHARED, |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 399 | BPF_CGROUP_STORAGE_PERCPU, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 400 | __BPF_CGROUP_STORAGE_MAX |
| 401 | }; |
| 402 | |
| 403 | #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX |
| 404 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 405 | /* The longest tracepoint has 12 args. |
| 406 | * See include/trace/bpf_probe.h |
| 407 | */ |
| 408 | #define MAX_BPF_FUNC_ARGS 12 |
| 409 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 410 | struct bpf_prog_stats { |
| 411 | u64 cnt; |
| 412 | u64 nsecs; |
| 413 | struct u64_stats_sync syncp; |
Eric Dumazet | 84a081f | 2019-10-11 11:11:40 -0700 | [diff] [blame] | 414 | } __aligned(2 * sizeof(u64)); |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 415 | |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 416 | struct btf_func_model { |
| 417 | u8 ret_size; |
| 418 | u8 nr_args; |
| 419 | u8 arg_size[MAX_BPF_FUNC_ARGS]; |
| 420 | }; |
| 421 | |
| 422 | /* Restore arguments before returning from trampoline to let original function |
| 423 | * continue executing. This flag is used for fentry progs when there are no |
| 424 | * fexit progs. |
| 425 | */ |
| 426 | #define BPF_TRAMP_F_RESTORE_REGS BIT(0) |
| 427 | /* Call original function after fentry progs, but before fexit progs. |
| 428 | * Makes sense for fentry/fexit, normal calls and indirect calls. |
| 429 | */ |
| 430 | #define BPF_TRAMP_F_CALL_ORIG BIT(1) |
| 431 | /* Skip current frame and return to parent. Makes sense for fentry/fexit |
| 432 | * programs only. Should not be used with normal calls and indirect calls. |
| 433 | */ |
| 434 | #define BPF_TRAMP_F_SKIP_FRAME BIT(2) |
| 435 | |
KP Singh | 88fd9e5 | 2020-03-04 20:18:47 +0100 | [diff] [blame] | 436 | /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 |
| 437 | * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2 |
| 438 | */ |
| 439 | #define BPF_MAX_TRAMP_PROGS 40 |
| 440 | |
| 441 | struct bpf_tramp_progs { |
| 442 | struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS]; |
| 443 | int nr_progs; |
| 444 | }; |
| 445 | |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 446 | /* Different use cases for BPF trampoline: |
| 447 | * 1. replace nop at the function entry (kprobe equivalent) |
| 448 | * flags = BPF_TRAMP_F_RESTORE_REGS |
| 449 | * fentry = a set of programs to run before returning from trampoline |
| 450 | * |
| 451 | * 2. replace nop at the function entry (kprobe + kretprobe equivalent) |
| 452 | * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME |
| 453 | * orig_call = fentry_ip + MCOUNT_INSN_SIZE |
| 454 | * fentry = a set of program to run before calling original function |
| 455 | * fexit = a set of program to run after original function |
| 456 | * |
| 457 | * 3. replace direct call instruction anywhere in the function body |
| 458 | * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid) |
| 459 | * With flags = 0 |
| 460 | * fentry = a set of programs to run before returning from trampoline |
| 461 | * With flags = BPF_TRAMP_F_CALL_ORIG |
| 462 | * orig_call = original callback addr or direct function addr |
| 463 | * fentry = a set of program to run before calling original function |
| 464 | * fexit = a set of program to run after original function |
| 465 | */ |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 466 | int arch_prepare_bpf_trampoline(void *image, void *image_end, |
| 467 | const struct btf_func_model *m, u32 flags, |
KP Singh | 88fd9e5 | 2020-03-04 20:18:47 +0100 | [diff] [blame] | 468 | struct bpf_tramp_progs *tprogs, |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 469 | void *orig_call); |
| 470 | /* these two functions are called from generated trampoline */ |
| 471 | u64 notrace __bpf_prog_enter(void); |
| 472 | void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start); |
| 473 | |
| 474 | enum bpf_tramp_prog_type { |
| 475 | BPF_TRAMP_FENTRY, |
| 476 | BPF_TRAMP_FEXIT, |
KP Singh | ae24082 | 2020-03-04 20:18:49 +0100 | [diff] [blame] | 477 | BPF_TRAMP_MODIFY_RETURN, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 478 | BPF_TRAMP_MAX, |
| 479 | BPF_TRAMP_REPLACE, /* more than MAX */ |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | struct bpf_trampoline { |
| 483 | /* hlist for trampoline_table */ |
| 484 | struct hlist_node hlist; |
| 485 | /* serializes access to fields of this trampoline */ |
| 486 | struct mutex mutex; |
| 487 | refcount_t refcnt; |
| 488 | u64 key; |
| 489 | struct { |
| 490 | struct btf_func_model model; |
| 491 | void *addr; |
Alexei Starovoitov | b91e014f | 2019-12-08 16:01:13 -0800 | [diff] [blame] | 492 | bool ftrace_managed; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 493 | } func; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 494 | /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF |
| 495 | * program by replacing one of its functions. func.addr is the address |
| 496 | * of the function it replaced. |
| 497 | */ |
| 498 | struct bpf_prog *extension_prog; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 499 | /* list of BPF programs using this trampoline */ |
| 500 | struct hlist_head progs_hlist[BPF_TRAMP_MAX]; |
| 501 | /* Number of attached programs. A counter per kind. */ |
| 502 | int progs_cnt[BPF_TRAMP_MAX]; |
| 503 | /* Executable image of trampoline */ |
| 504 | void *image; |
| 505 | u64 selector; |
| 506 | }; |
Björn Töpel | 75ccbef | 2019-12-13 18:51:08 +0100 | [diff] [blame] | 507 | |
Björn Töpel | 116eb78 | 2019-12-13 18:51:12 +0100 | [diff] [blame] | 508 | #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */ |
Björn Töpel | 75ccbef | 2019-12-13 18:51:08 +0100 | [diff] [blame] | 509 | |
| 510 | struct bpf_dispatcher_prog { |
| 511 | struct bpf_prog *prog; |
| 512 | refcount_t users; |
| 513 | }; |
| 514 | |
| 515 | struct bpf_dispatcher { |
| 516 | /* dispatcher mutex */ |
| 517 | struct mutex mutex; |
| 518 | void *func; |
| 519 | struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX]; |
| 520 | int num_progs; |
| 521 | void *image; |
| 522 | u32 image_off; |
| 523 | }; |
| 524 | |
Björn Töpel | 7e6897f | 2019-12-13 18:51:09 +0100 | [diff] [blame] | 525 | static __always_inline unsigned int bpf_dispatcher_nopfunc( |
| 526 | const void *ctx, |
| 527 | const struct bpf_insn *insnsi, |
| 528 | unsigned int (*bpf_func)(const void *, |
| 529 | const struct bpf_insn *)) |
| 530 | { |
| 531 | return bpf_func(ctx, insnsi); |
| 532 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 533 | #ifdef CONFIG_BPF_JIT |
| 534 | struct bpf_trampoline *bpf_trampoline_lookup(u64 key); |
| 535 | int bpf_trampoline_link_prog(struct bpf_prog *prog); |
| 536 | int bpf_trampoline_unlink_prog(struct bpf_prog *prog); |
| 537 | void bpf_trampoline_put(struct bpf_trampoline *tr); |
Björn Töpel | 75ccbef | 2019-12-13 18:51:08 +0100 | [diff] [blame] | 538 | #define BPF_DISPATCHER_INIT(name) { \ |
| 539 | .mutex = __MUTEX_INITIALIZER(name.mutex), \ |
| 540 | .func = &name##func, \ |
| 541 | .progs = {}, \ |
| 542 | .num_progs = 0, \ |
| 543 | .image = NULL, \ |
| 544 | .image_off = 0 \ |
| 545 | } |
| 546 | |
| 547 | #define DEFINE_BPF_DISPATCHER(name) \ |
| 548 | noinline unsigned int name##func( \ |
| 549 | const void *ctx, \ |
| 550 | const struct bpf_insn *insnsi, \ |
| 551 | unsigned int (*bpf_func)(const void *, \ |
| 552 | const struct bpf_insn *)) \ |
| 553 | { \ |
| 554 | return bpf_func(ctx, insnsi); \ |
| 555 | } \ |
| 556 | EXPORT_SYMBOL(name##func); \ |
| 557 | struct bpf_dispatcher name = BPF_DISPATCHER_INIT(name); |
| 558 | #define DECLARE_BPF_DISPATCHER(name) \ |
| 559 | unsigned int name##func( \ |
| 560 | const void *ctx, \ |
| 561 | const struct bpf_insn *insnsi, \ |
| 562 | unsigned int (*bpf_func)(const void *, \ |
| 563 | const struct bpf_insn *)); \ |
| 564 | extern struct bpf_dispatcher name; |
| 565 | #define BPF_DISPATCHER_FUNC(name) name##func |
| 566 | #define BPF_DISPATCHER_PTR(name) (&name) |
| 567 | void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from, |
| 568 | struct bpf_prog *to); |
Jiri Olsa | e9b4e60 | 2020-01-23 17:15:07 +0100 | [diff] [blame] | 569 | struct bpf_image { |
| 570 | struct latch_tree_node tnode; |
| 571 | unsigned char data[]; |
| 572 | }; |
| 573 | #define BPF_IMAGE_SIZE (PAGE_SIZE - sizeof(struct bpf_image)) |
| 574 | bool is_bpf_image_address(unsigned long address); |
| 575 | void *bpf_image_alloc(void); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 576 | #else |
| 577 | static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key) |
| 578 | { |
| 579 | return NULL; |
| 580 | } |
| 581 | static inline int bpf_trampoline_link_prog(struct bpf_prog *prog) |
| 582 | { |
| 583 | return -ENOTSUPP; |
| 584 | } |
| 585 | static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog) |
| 586 | { |
| 587 | return -ENOTSUPP; |
| 588 | } |
| 589 | static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {} |
Björn Töpel | 75ccbef | 2019-12-13 18:51:08 +0100 | [diff] [blame] | 590 | #define DEFINE_BPF_DISPATCHER(name) |
| 591 | #define DECLARE_BPF_DISPATCHER(name) |
| 592 | #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nopfunc |
| 593 | #define BPF_DISPATCHER_PTR(name) NULL |
| 594 | static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, |
| 595 | struct bpf_prog *from, |
| 596 | struct bpf_prog *to) {} |
Jiri Olsa | e9b4e60 | 2020-01-23 17:15:07 +0100 | [diff] [blame] | 597 | static inline bool is_bpf_image_address(unsigned long address) |
| 598 | { |
| 599 | return false; |
| 600 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 601 | #endif |
| 602 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 603 | struct bpf_func_info_aux { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 604 | u16 linkage; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 605 | bool unreliable; |
| 606 | }; |
| 607 | |
Daniel Borkmann | a66886f | 2019-11-22 21:07:57 +0100 | [diff] [blame] | 608 | enum bpf_jit_poke_reason { |
| 609 | BPF_POKE_REASON_TAIL_CALL, |
| 610 | }; |
| 611 | |
| 612 | /* Descriptor of pokes pointing /into/ the JITed image. */ |
| 613 | struct bpf_jit_poke_descriptor { |
| 614 | void *ip; |
| 615 | union { |
| 616 | struct { |
| 617 | struct bpf_map *map; |
| 618 | u32 key; |
| 619 | } tail_call; |
| 620 | }; |
| 621 | bool ip_stable; |
| 622 | u8 adj_off; |
| 623 | u16 reason; |
| 624 | }; |
| 625 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 626 | struct bpf_prog_aux { |
Andrii Nakryiko | 85192dbf | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 627 | atomic64_t refcnt; |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 628 | u32 used_map_cnt; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 629 | u32 max_ctx_offset; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 630 | u32 max_pkt_offset; |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 631 | u32 max_tp_access; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 632 | u32 stack_depth; |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 633 | u32 id; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 634 | u32 func_cnt; /* used by non-func prog as the number of func progs */ |
| 635 | u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */ |
Alexei Starovoitov | ccfe29eb2 | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 636 | u32 attach_btf_id; /* in-kernel BTF type id to attach to */ |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 637 | struct bpf_prog *linked_prog; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 638 | bool verifier_zext; /* Zero extensions has been inserted by verifier. */ |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 639 | bool offload_requested; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 640 | bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */ |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 641 | bool func_proto_unreliable; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 642 | enum bpf_tramp_prog_type trampoline_prog_type; |
| 643 | struct bpf_trampoline *trampoline; |
| 644 | struct hlist_node tramp_hlist; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 645 | /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ |
| 646 | const struct btf_type *attach_func_proto; |
| 647 | /* function name for valid attach_btf_id */ |
| 648 | const char *attach_func_name; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 649 | struct bpf_prog **func; |
| 650 | void *jit_data; /* JIT specific data. arch dependent */ |
Daniel Borkmann | a66886f | 2019-11-22 21:07:57 +0100 | [diff] [blame] | 651 | struct bpf_jit_poke_descriptor *poke_tab; |
| 652 | u32 size_poke_tab; |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 653 | struct latch_tree_node ksym_tnode; |
| 654 | struct list_head ksym_lnode; |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 655 | const struct bpf_prog_ops *ops; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 656 | struct bpf_map **used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 657 | struct bpf_prog *prog; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 658 | struct user_struct *user; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 659 | u64 load_time; /* ns since boottime */ |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 660 | struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]; |
Martin KaFai Lau | 067cae4 | 2017-10-05 21:52:12 -0700 | [diff] [blame] | 661 | char name[BPF_OBJ_NAME_LEN]; |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 662 | #ifdef CONFIG_SECURITY |
| 663 | void *security; |
| 664 | #endif |
Jakub Kicinski | 0a9c199 | 2018-01-11 20:29:07 -0800 | [diff] [blame] | 665 | struct bpf_prog_offload *offload; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 666 | struct btf *btf; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 667 | struct bpf_func_info *func_info; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 668 | struct bpf_func_info_aux *func_info_aux; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 669 | /* bpf_line_info loaded from userspace. linfo->insn_off |
| 670 | * has the xlated insn offset. |
| 671 | * Both the main and sub prog share the same linfo. |
| 672 | * The subprog can access its first linfo by |
| 673 | * using the linfo_idx. |
| 674 | */ |
| 675 | struct bpf_line_info *linfo; |
| 676 | /* jited_linfo is the jited addr of the linfo. It has a |
| 677 | * one to one mapping to linfo: |
| 678 | * jited_linfo[i] is the jited addr for the linfo[i]->insn_off. |
| 679 | * Both the main and sub prog share the same jited_linfo. |
| 680 | * The subprog can access its first jited_linfo by |
| 681 | * using the linfo_idx. |
| 682 | */ |
| 683 | void **jited_linfo; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 684 | u32 func_info_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 685 | u32 nr_linfo; |
| 686 | /* subprog can use linfo_idx to access its first linfo and |
| 687 | * jited_linfo. |
| 688 | * main prog always has linfo_idx == 0 |
| 689 | */ |
| 690 | u32 linfo_idx; |
Alexei Starovoitov | 3dec541 | 2019-10-15 20:25:03 -0700 | [diff] [blame] | 691 | u32 num_exentries; |
| 692 | struct exception_table_entry *extable; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 693 | struct bpf_prog_stats __percpu *stats; |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 694 | union { |
| 695 | struct work_struct work; |
| 696 | struct rcu_head rcu; |
| 697 | }; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 700 | struct bpf_array_aux { |
| 701 | /* 'Ownership' of prog array is claimed by the first program that |
| 702 | * is going to use this map or by the first program which FD is |
| 703 | * stored in the map to make sure that all callers and callees have |
| 704 | * the same prog type and JITed flag. |
| 705 | */ |
| 706 | enum bpf_prog_type type; |
| 707 | bool jited; |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 708 | /* Programs with direct jumps into programs part of this array. */ |
| 709 | struct list_head poke_progs; |
| 710 | struct bpf_map *map; |
| 711 | struct mutex poke_mutex; |
| 712 | struct work_struct work; |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 713 | }; |
| 714 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 715 | struct bpf_struct_ops_value; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 716 | struct btf_type; |
| 717 | struct btf_member; |
| 718 | |
| 719 | #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64 |
| 720 | struct bpf_struct_ops { |
| 721 | const struct bpf_verifier_ops *verifier_ops; |
| 722 | int (*init)(struct btf *btf); |
| 723 | int (*check_member)(const struct btf_type *t, |
| 724 | const struct btf_member *member); |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 725 | int (*init_member)(const struct btf_type *t, |
| 726 | const struct btf_member *member, |
| 727 | void *kdata, const void *udata); |
| 728 | int (*reg)(void *kdata); |
| 729 | void (*unreg)(void *kdata); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 730 | const struct btf_type *type; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 731 | const struct btf_type *value_type; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 732 | const char *name; |
| 733 | struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS]; |
| 734 | u32 type_id; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 735 | u32 value_id; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 736 | }; |
| 737 | |
| 738 | #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL) |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 739 | #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA)) |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 740 | const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id); |
Martin KaFai Lau | d3e42bb | 2020-01-27 09:51:45 -0800 | [diff] [blame] | 741 | void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log); |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 742 | bool bpf_struct_ops_get(const void *kdata); |
| 743 | void bpf_struct_ops_put(const void *kdata); |
| 744 | int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key, |
| 745 | void *value); |
| 746 | static inline bool bpf_try_module_get(const void *data, struct module *owner) |
| 747 | { |
| 748 | if (owner == BPF_MODULE_OWNER) |
| 749 | return bpf_struct_ops_get(data); |
| 750 | else |
| 751 | return try_module_get(owner); |
| 752 | } |
| 753 | static inline void bpf_module_put(const void *data, struct module *owner) |
| 754 | { |
| 755 | if (owner == BPF_MODULE_OWNER) |
| 756 | bpf_struct_ops_put(data); |
| 757 | else |
| 758 | module_put(owner); |
| 759 | } |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 760 | #else |
| 761 | static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) |
| 762 | { |
| 763 | return NULL; |
| 764 | } |
Martin KaFai Lau | d3e42bb | 2020-01-27 09:51:45 -0800 | [diff] [blame] | 765 | static inline void bpf_struct_ops_init(struct btf *btf, |
| 766 | struct bpf_verifier_log *log) |
| 767 | { |
| 768 | } |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 769 | static inline bool bpf_try_module_get(const void *data, struct module *owner) |
| 770 | { |
| 771 | return try_module_get(owner); |
| 772 | } |
| 773 | static inline void bpf_module_put(const void *data, struct module *owner) |
| 774 | { |
| 775 | module_put(owner); |
| 776 | } |
| 777 | static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, |
| 778 | void *key, |
| 779 | void *value) |
| 780 | { |
| 781 | return -EINVAL; |
| 782 | } |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 783 | #endif |
| 784 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 785 | struct bpf_array { |
| 786 | struct bpf_map map; |
| 787 | u32 elem_size; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 788 | u32 index_mask; |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 789 | struct bpf_array_aux *aux; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 790 | union { |
| 791 | char value[0] __aligned(8); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 792 | void *ptrs[0] __aligned(8); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 793 | void __percpu *pptrs[0] __aligned(8); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 794 | }; |
| 795 | }; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 796 | |
Alexei Starovoitov | c04c0d2 | 2019-04-01 21:27:45 -0700 | [diff] [blame] | 797 | #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */ |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 798 | #define MAX_TAIL_CALL_CNT 32 |
| 799 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 800 | #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \ |
| 801 | BPF_F_RDONLY_PROG | \ |
| 802 | BPF_F_WRONLY | \ |
| 803 | BPF_F_WRONLY_PROG) |
| 804 | |
| 805 | #define BPF_MAP_CAN_READ BIT(0) |
| 806 | #define BPF_MAP_CAN_WRITE BIT(1) |
| 807 | |
| 808 | static inline u32 bpf_map_flags_to_cap(struct bpf_map *map) |
| 809 | { |
| 810 | u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG); |
| 811 | |
| 812 | /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is |
| 813 | * not possible. |
| 814 | */ |
| 815 | if (access_flags & BPF_F_RDONLY_PROG) |
| 816 | return BPF_MAP_CAN_READ; |
| 817 | else if (access_flags & BPF_F_WRONLY_PROG) |
| 818 | return BPF_MAP_CAN_WRITE; |
| 819 | else |
| 820 | return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE; |
| 821 | } |
| 822 | |
| 823 | static inline bool bpf_map_flags_access_ok(u32 access_flags) |
| 824 | { |
| 825 | return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) != |
| 826 | (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG); |
| 827 | } |
| 828 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 829 | struct bpf_event_entry { |
| 830 | struct perf_event *event; |
| 831 | struct file *perf_file; |
| 832 | struct file *map_file; |
| 833 | struct rcu_head rcu; |
| 834 | }; |
| 835 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 836 | bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp); |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 837 | int bpf_prog_calc_tag(struct bpf_prog *fp); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 838 | const char *kernel_type_name(u32 btf_type_id); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 839 | |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 840 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void); |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 841 | |
| 842 | typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src, |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 843 | unsigned long off, unsigned long len); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 844 | typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type, |
| 845 | const struct bpf_insn *src, |
| 846 | struct bpf_insn *dst, |
| 847 | struct bpf_prog *prog, |
| 848 | u32 *target_size); |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 849 | |
| 850 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 851 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 852 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 853 | /* an array of programs to be executed under rcu_lock. |
| 854 | * |
| 855 | * Typical usage: |
| 856 | * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN); |
| 857 | * |
| 858 | * the structure returned by bpf_prog_array_alloc() should be populated |
| 859 | * with program pointers and the last pointer must be NULL. |
| 860 | * The user has to keep refcnt on the program and make sure the program |
| 861 | * is removed from the array before bpf_prog_put(). |
| 862 | * The 'struct bpf_prog_array *' should only be replaced with xchg() |
| 863 | * since other cpus are walking the array of pointers in parallel. |
| 864 | */ |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 865 | struct bpf_prog_array_item { |
| 866 | struct bpf_prog *prog; |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 867 | struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]; |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 868 | }; |
| 869 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 870 | struct bpf_prog_array { |
| 871 | struct rcu_head rcu; |
Gustavo A. R. Silva | d7f10df | 2020-02-26 18:17:44 -0600 | [diff] [blame] | 872 | struct bpf_prog_array_item items[]; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 873 | }; |
| 874 | |
Roman Gushchin | d29ab6e | 2018-07-13 12:41:10 -0700 | [diff] [blame] | 875 | struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags); |
Stanislav Fomichev | 54e9c9d | 2019-05-28 14:14:41 -0700 | [diff] [blame] | 876 | void bpf_prog_array_free(struct bpf_prog_array *progs); |
| 877 | int bpf_prog_array_length(struct bpf_prog_array *progs); |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 878 | bool bpf_prog_array_is_empty(struct bpf_prog_array *array); |
Stanislav Fomichev | 54e9c9d | 2019-05-28 14:14:41 -0700 | [diff] [blame] | 879 | int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs, |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 880 | __u32 __user *prog_ids, u32 cnt); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 881 | |
Stanislav Fomichev | 54e9c9d | 2019-05-28 14:14:41 -0700 | [diff] [blame] | 882 | void bpf_prog_array_delete_safe(struct bpf_prog_array *progs, |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 883 | struct bpf_prog *old_prog); |
Stanislav Fomichev | 54e9c9d | 2019-05-28 14:14:41 -0700 | [diff] [blame] | 884 | int bpf_prog_array_copy_info(struct bpf_prog_array *array, |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 885 | u32 *prog_ids, u32 request_cnt, |
| 886 | u32 *prog_cnt); |
Stanislav Fomichev | 54e9c9d | 2019-05-28 14:14:41 -0700 | [diff] [blame] | 887 | int bpf_prog_array_copy(struct bpf_prog_array *old_array, |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 888 | struct bpf_prog *exclude_prog, |
| 889 | struct bpf_prog *include_prog, |
| 890 | struct bpf_prog_array **new_array); |
| 891 | |
| 892 | #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 893 | ({ \ |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 894 | struct bpf_prog_array_item *_item; \ |
| 895 | struct bpf_prog *_prog; \ |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 896 | struct bpf_prog_array *_array; \ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 897 | u32 _ret = 1; \ |
David Miller | 2a916f2 | 2020-02-24 15:01:46 +0100 | [diff] [blame] | 898 | migrate_disable(); \ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 899 | rcu_read_lock(); \ |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 900 | _array = rcu_dereference(array); \ |
| 901 | if (unlikely(check_non_null && !_array))\ |
| 902 | goto _out; \ |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 903 | _item = &_array->items[0]; \ |
| 904 | while ((_prog = READ_ONCE(_item->prog))) { \ |
| 905 | bpf_cgroup_storage_set(_item->cgroup_storage); \ |
| 906 | _ret &= func(_prog, ctx); \ |
| 907 | _item++; \ |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 908 | } \ |
| 909 | _out: \ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 910 | rcu_read_unlock(); \ |
David Miller | 2a916f2 | 2020-02-24 15:01:46 +0100 | [diff] [blame] | 911 | migrate_enable(); \ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 912 | _ret; \ |
| 913 | }) |
| 914 | |
brakmo | 1f52f6c | 2019-05-28 16:59:35 -0700 | [diff] [blame] | 915 | /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs |
| 916 | * so BPF programs can request cwr for TCP packets. |
| 917 | * |
| 918 | * Current cgroup skb programs can only return 0 or 1 (0 to drop the |
| 919 | * packet. This macro changes the behavior so the low order bit |
| 920 | * indicates whether the packet should be dropped (0) or not (1) |
| 921 | * and the next bit is a congestion notification bit. This could be |
| 922 | * used by TCP to call tcp_enter_cwr() |
| 923 | * |
| 924 | * Hence, new allowed return values of CGROUP EGRESS BPF programs are: |
| 925 | * 0: drop packet |
| 926 | * 1: keep packet |
| 927 | * 2: drop packet and cn |
| 928 | * 3: keep packet and cn |
| 929 | * |
| 930 | * This macro then converts it to one of the NET_XMIT or an error |
| 931 | * code that is then interpreted as drop packet (and no cn): |
| 932 | * 0: NET_XMIT_SUCCESS skb should be transmitted |
| 933 | * 1: NET_XMIT_DROP skb should be dropped and cn |
| 934 | * 2: NET_XMIT_CN skb should be transmitted and cn |
| 935 | * 3: -EPERM skb should be dropped |
| 936 | */ |
| 937 | #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \ |
| 938 | ({ \ |
| 939 | struct bpf_prog_array_item *_item; \ |
| 940 | struct bpf_prog *_prog; \ |
| 941 | struct bpf_prog_array *_array; \ |
| 942 | u32 ret; \ |
| 943 | u32 _ret = 1; \ |
| 944 | u32 _cn = 0; \ |
David Miller | 2a916f2 | 2020-02-24 15:01:46 +0100 | [diff] [blame] | 945 | migrate_disable(); \ |
brakmo | 1f52f6c | 2019-05-28 16:59:35 -0700 | [diff] [blame] | 946 | rcu_read_lock(); \ |
| 947 | _array = rcu_dereference(array); \ |
| 948 | _item = &_array->items[0]; \ |
| 949 | while ((_prog = READ_ONCE(_item->prog))) { \ |
| 950 | bpf_cgroup_storage_set(_item->cgroup_storage); \ |
| 951 | ret = func(_prog, ctx); \ |
| 952 | _ret &= (ret & 1); \ |
| 953 | _cn |= (ret & 2); \ |
| 954 | _item++; \ |
| 955 | } \ |
| 956 | rcu_read_unlock(); \ |
David Miller | 2a916f2 | 2020-02-24 15:01:46 +0100 | [diff] [blame] | 957 | migrate_enable(); \ |
brakmo | 1f52f6c | 2019-05-28 16:59:35 -0700 | [diff] [blame] | 958 | if (_ret) \ |
| 959 | _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \ |
| 960 | else \ |
| 961 | _ret = (_cn ? NET_XMIT_DROP : -EPERM); \ |
| 962 | _ret; \ |
| 963 | }) |
| 964 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 965 | #define BPF_PROG_RUN_ARRAY(array, ctx, func) \ |
| 966 | __BPF_PROG_RUN_ARRAY(array, ctx, func, false) |
| 967 | |
| 968 | #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \ |
| 969 | __BPF_PROG_RUN_ARRAY(array, ctx, func, true) |
| 970 | |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 971 | #ifdef CONFIG_BPF_SYSCALL |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 972 | DECLARE_PER_CPU(int, bpf_prog_active); |
| 973 | |
Thomas Gleixner | c518cfa | 2020-02-24 15:01:47 +0100 | [diff] [blame] | 974 | /* |
| 975 | * Block execution of BPF programs attached to instrumentation (perf, |
| 976 | * kprobes, tracepoints) to prevent deadlocks on map operations as any of |
| 977 | * these events can happen inside a region which holds a map bucket lock |
| 978 | * and can deadlock on it. |
| 979 | * |
| 980 | * Use the preemption safe inc/dec variants on RT because migrate disable |
| 981 | * is preemptible on RT and preemption in the middle of the RMW operation |
| 982 | * might lead to inconsistent state. Use the raw variants for non RT |
| 983 | * kernels as migrate_disable() maps to preempt_disable() so the slightly |
| 984 | * more expensive save operation can be avoided. |
| 985 | */ |
| 986 | static inline void bpf_disable_instrumentation(void) |
| 987 | { |
| 988 | migrate_disable(); |
| 989 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
| 990 | this_cpu_inc(bpf_prog_active); |
| 991 | else |
| 992 | __this_cpu_inc(bpf_prog_active); |
| 993 | } |
| 994 | |
| 995 | static inline void bpf_enable_instrumentation(void) |
| 996 | { |
| 997 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
| 998 | this_cpu_dec(bpf_prog_active); |
| 999 | else |
| 1000 | __this_cpu_dec(bpf_prog_active); |
| 1001 | migrate_enable(); |
| 1002 | } |
| 1003 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1004 | extern const struct file_operations bpf_map_fops; |
| 1005 | extern const struct file_operations bpf_prog_fops; |
| 1006 | |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 1007 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1008 | extern const struct bpf_prog_ops _name ## _prog_ops; \ |
| 1009 | extern const struct bpf_verifier_ops _name ## _verifier_ops; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 1010 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 1011 | extern const struct bpf_map_ops _ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1012 | #include <linux/bpf_types.h> |
| 1013 | #undef BPF_PROG_TYPE |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 1014 | #undef BPF_MAP_TYPE |
Daniel Borkmann | 0fc174d | 2015-03-01 12:31:44 +0100 | [diff] [blame] | 1015 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1016 | extern const struct bpf_prog_ops bpf_offload_prog_ops; |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1017 | extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops; |
| 1018 | extern const struct bpf_verifier_ops xdp_analyzer_ops; |
| 1019 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1020 | struct bpf_prog *bpf_prog_get(u32 ufd); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1021 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
Jakub Kicinski | 288b3de5 | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1022 | bool attach_drv); |
Andrii Nakryiko | 85192dbf | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1023 | void bpf_prog_add(struct bpf_prog *prog, int i); |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1024 | void bpf_prog_sub(struct bpf_prog *prog, int i); |
Andrii Nakryiko | 85192dbf | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1025 | void bpf_prog_inc(struct bpf_prog *prog); |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1026 | struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog); |
Daniel Borkmann | 61e021f3 | 2015-03-02 15:21:55 +0100 | [diff] [blame] | 1027 | void bpf_prog_put(struct bpf_prog *prog); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1028 | int __bpf_prog_charge(struct user_struct *user, u32 pages); |
| 1029 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages); |
Daniel Borkmann | a2ea0746 | 2019-12-16 17:49:00 +0100 | [diff] [blame] | 1030 | void __bpf_free_used_maps(struct bpf_prog_aux *aux, |
| 1031 | struct bpf_map **used_maps, u32 len); |
Daniel Borkmann | 61e021f3 | 2015-03-02 15:21:55 +0100 | [diff] [blame] | 1032 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1033 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1034 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1035 | |
Martin KaFai Lau | 1ed4d92 | 2020-02-25 15:04:21 -0800 | [diff] [blame] | 1036 | struct bpf_map *bpf_map_get(u32 ufd); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 1037 | struct bpf_map *bpf_map_get_with_uref(u32 ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1038 | struct bpf_map *__bpf_map_get(struct fd f); |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 1039 | void bpf_map_inc(struct bpf_map *map); |
| 1040 | void bpf_map_inc_with_uref(struct bpf_map *map); |
| 1041 | struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 1042 | void bpf_map_put_with_uref(struct bpf_map *map); |
Daniel Borkmann | 61e021f3 | 2015-03-02 15:21:55 +0100 | [diff] [blame] | 1043 | void bpf_map_put(struct bpf_map *map); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 1044 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages); |
| 1045 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages); |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 1046 | int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 1047 | void bpf_map_charge_finish(struct bpf_map_memory *mem); |
| 1048 | void bpf_map_charge_move(struct bpf_map_memory *dst, |
| 1049 | struct bpf_map_memory *src); |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 1050 | void *bpf_map_area_alloc(u64 size, int numa_node); |
| 1051 | void *bpf_map_area_mmapable_alloc(u64 size, int numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 1052 | void bpf_map_area_free(void *base); |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 1053 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1054 | int generic_map_lookup_batch(struct bpf_map *map, |
| 1055 | const union bpf_attr *attr, |
| 1056 | union bpf_attr __user *uattr); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1057 | int generic_map_update_batch(struct bpf_map *map, |
| 1058 | const union bpf_attr *attr, |
| 1059 | union bpf_attr __user *uattr); |
| 1060 | int generic_map_delete_batch(struct bpf_map *map, |
| 1061 | const union bpf_attr *attr, |
| 1062 | union bpf_attr __user *uattr); |
Daniel Borkmann | 61e021f3 | 2015-03-02 15:21:55 +0100 | [diff] [blame] | 1063 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1064 | extern int sysctl_unprivileged_bpf_disabled; |
| 1065 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1066 | int bpf_map_new_fd(struct bpf_map *map, int flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1067 | int bpf_prog_new_fd(struct bpf_prog *prog); |
| 1068 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 1069 | struct bpf_link; |
| 1070 | |
| 1071 | struct bpf_link_ops { |
| 1072 | void (*release)(struct bpf_link *link); |
| 1073 | }; |
| 1074 | |
| 1075 | void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops, |
| 1076 | struct bpf_prog *prog); |
| 1077 | void bpf_link_inc(struct bpf_link *link); |
| 1078 | void bpf_link_put(struct bpf_link *link); |
| 1079 | int bpf_link_new_fd(struct bpf_link *link); |
| 1080 | struct bpf_link *bpf_link_get_from_fd(u32 ufd); |
| 1081 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1082 | int bpf_obj_pin_user(u32 ufd, const char __user *pathname); |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1083 | int bpf_obj_get_user(const char __user *pathname, int flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1084 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1085 | int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); |
| 1086 | int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value); |
| 1087 | int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, |
| 1088 | u64 flags); |
| 1089 | int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value, |
| 1090 | u64 flags); |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 1091 | |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 1092 | int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1093 | |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 1094 | int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file, |
| 1095 | void *key, void *value, u64 map_flags); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 1096 | int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1097 | int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file, |
| 1098 | void *key, void *value, u64 map_flags); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 1099 | int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value); |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 1100 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1101 | int bpf_get_file_flag(int flags); |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 1102 | int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size, |
| 1103 | size_t actual_size); |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1104 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1105 | /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and |
| 1106 | * forced to use 'long' read/writes to try to atomically copy long counters. |
| 1107 | * Best-effort only. No barriers here, since it _will_ race with concurrent |
| 1108 | * updates from BPF programs. Called from bpf syscall and mostly used with |
| 1109 | * size 8 or 16 bytes, so ask compiler to inline it. |
| 1110 | */ |
| 1111 | static inline void bpf_long_memcpy(void *dst, const void *src, u32 size) |
| 1112 | { |
| 1113 | const long *lsrc = src; |
| 1114 | long *ldst = dst; |
| 1115 | |
| 1116 | size /= sizeof(long); |
| 1117 | while (size--) |
| 1118 | *ldst++ = *lsrc++; |
| 1119 | } |
| 1120 | |
Daniel Borkmann | 61e021f3 | 2015-03-02 15:21:55 +0100 | [diff] [blame] | 1121 | /* verify correctness of eBPF program */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 1122 | int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, |
| 1123 | union bpf_attr __user *uattr); |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1124 | void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); |
John Fastabend | 46f55cf | 2017-07-17 21:56:48 -0700 | [diff] [blame] | 1125 | |
| 1126 | /* Map specifics */ |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 1127 | struct xdp_buff; |
Toshiaki Makita | 6d5fc19 | 2018-06-14 11:07:42 +0900 | [diff] [blame] | 1128 | struct sk_buff; |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 1129 | |
| 1130 | struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key); |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 1131 | struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key); |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 1132 | void __dev_flush(void); |
| 1133 | int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp, |
| 1134 | struct net_device *dev_rx); |
Jesper Dangaard Brouer | 38edddb | 2018-05-24 16:45:57 +0200 | [diff] [blame] | 1135 | int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp, |
| 1136 | struct net_device *dev_rx); |
Toshiaki Makita | 6d5fc19 | 2018-06-14 11:07:42 +0900 | [diff] [blame] | 1137 | int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb, |
| 1138 | struct bpf_prog *xdp_prog); |
John Fastabend | 46f55cf | 2017-07-17 21:56:48 -0700 | [diff] [blame] | 1139 | |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1140 | struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key); |
Björn Töpel | cdfafe9 | 2019-12-19 07:10:04 +0100 | [diff] [blame] | 1141 | void __cpu_map_flush(void); |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1142 | int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp, |
| 1143 | struct net_device *dev_rx); |
| 1144 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 1145 | /* Return map's numa specified by userspace */ |
| 1146 | static inline int bpf_map_attr_numa_node(const union bpf_attr *attr) |
| 1147 | { |
| 1148 | return (attr->map_flags & BPF_F_NUMA_NODE) ? |
| 1149 | attr->numa_node : NUMA_NO_NODE; |
| 1150 | } |
| 1151 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1152 | struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type); |
Martin KaFai Lau | 5dc4c4b | 2018-08-08 01:01:24 -0700 | [diff] [blame] | 1153 | int array_map_alloc_check(union bpf_attr *attr); |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1154 | |
Stanislav Fomichev | c695865 | 2019-04-11 09:12:02 -0700 | [diff] [blame] | 1155 | int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 1156 | union bpf_attr __user *uattr); |
| 1157 | int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 1158 | union bpf_attr __user *uattr); |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 1159 | int bpf_prog_test_run_tracing(struct bpf_prog *prog, |
| 1160 | const union bpf_attr *kattr, |
| 1161 | union bpf_attr __user *uattr); |
Stanislav Fomichev | c695865 | 2019-04-11 09:12:02 -0700 | [diff] [blame] | 1162 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
| 1163 | const union bpf_attr *kattr, |
| 1164 | union bpf_attr __user *uattr); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 1165 | bool btf_ctx_access(int off, int size, enum bpf_access_type type, |
| 1166 | const struct bpf_prog *prog, |
| 1167 | struct bpf_insn_access_aux *info); |
| 1168 | int btf_struct_access(struct bpf_verifier_log *log, |
| 1169 | const struct btf_type *t, int off, int size, |
| 1170 | enum bpf_access_type atype, |
| 1171 | u32 *next_btf_id); |
Alexei Starovoitov | 9cc31b3 | 2019-11-14 10:57:14 -0800 | [diff] [blame] | 1172 | int btf_resolve_helper_id(struct bpf_verifier_log *log, |
| 1173 | const struct bpf_func_proto *fn, int); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 1174 | |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 1175 | int btf_distill_func_proto(struct bpf_verifier_log *log, |
| 1176 | struct btf *btf, |
| 1177 | const struct btf_type *func_proto, |
| 1178 | const char *func_name, |
| 1179 | struct btf_func_model *m); |
| 1180 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 1181 | struct bpf_reg_state; |
| 1182 | int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, |
| 1183 | struct bpf_reg_state *regs); |
| 1184 | int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, |
| 1185 | struct bpf_reg_state *reg); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 1186 | int btf_check_type_match(struct bpf_verifier_env *env, struct bpf_prog *prog, |
| 1187 | struct btf *btf, const struct btf_type *t); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 1188 | |
Björn Töpel | 7e6897f | 2019-12-13 18:51:09 +0100 | [diff] [blame] | 1189 | struct bpf_prog *bpf_prog_by_id(u32 id); |
| 1190 | |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1191 | #else /* !CONFIG_BPF_SYSCALL */ |
Daniel Borkmann | 0fc174d | 2015-03-01 12:31:44 +0100 | [diff] [blame] | 1192 | static inline struct bpf_prog *bpf_prog_get(u32 ufd) |
| 1193 | { |
| 1194 | return ERR_PTR(-EOPNOTSUPP); |
| 1195 | } |
| 1196 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1197 | static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, |
| 1198 | enum bpf_prog_type type, |
Jakub Kicinski | 288b3de5 | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1199 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1200 | { |
| 1201 | return ERR_PTR(-EOPNOTSUPP); |
| 1202 | } |
| 1203 | |
Andrii Nakryiko | 85192dbf | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1204 | static inline void bpf_prog_add(struct bpf_prog *prog, int i) |
Brenden Blanco | cc2e0b3 | 2016-07-20 07:55:52 -0700 | [diff] [blame] | 1205 | { |
Brenden Blanco | cc2e0b3 | 2016-07-20 07:55:52 -0700 | [diff] [blame] | 1206 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1207 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1208 | static inline void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1209 | { |
| 1210 | } |
| 1211 | |
Daniel Borkmann | 0fc174d | 2015-03-01 12:31:44 +0100 | [diff] [blame] | 1212 | static inline void bpf_prog_put(struct bpf_prog *prog) |
| 1213 | { |
| 1214 | } |
Daniel Borkmann | 6d67942dd | 2016-11-19 01:45:03 +0100 | [diff] [blame] | 1215 | |
Andrii Nakryiko | 85192dbf | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1216 | static inline void bpf_prog_inc(struct bpf_prog *prog) |
Alexei Starovoitov | aa6a5f3 | 2016-09-01 18:37:24 -0700 | [diff] [blame] | 1217 | { |
Alexei Starovoitov | aa6a5f3 | 2016-09-01 18:37:24 -0700 | [diff] [blame] | 1218 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1219 | |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1220 | static inline struct bpf_prog *__must_check |
| 1221 | bpf_prog_inc_not_zero(struct bpf_prog *prog) |
| 1222 | { |
| 1223 | return ERR_PTR(-EOPNOTSUPP); |
| 1224 | } |
| 1225 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1226 | static inline int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 1227 | { |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 1232 | { |
| 1233 | } |
John Fastabend | 46f55cf | 2017-07-17 21:56:48 -0700 | [diff] [blame] | 1234 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1235 | static inline int bpf_obj_get_user(const char __user *pathname, int flags) |
Shmulik Ladkani | 98589a0 | 2017-10-09 15:27:15 +0300 | [diff] [blame] | 1236 | { |
| 1237 | return -EOPNOTSUPP; |
| 1238 | } |
| 1239 | |
John Fastabend | 46f55cf | 2017-07-17 21:56:48 -0700 | [diff] [blame] | 1240 | static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map, |
| 1241 | u32 key) |
| 1242 | { |
| 1243 | return NULL; |
| 1244 | } |
| 1245 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 1246 | static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map, |
| 1247 | u32 key) |
| 1248 | { |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 1252 | static inline void __dev_flush(void) |
John Fastabend | 46f55cf | 2017-07-17 21:56:48 -0700 | [diff] [blame] | 1253 | { |
| 1254 | } |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1255 | |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 1256 | struct xdp_buff; |
| 1257 | struct bpf_dtab_netdev; |
| 1258 | |
| 1259 | static inline |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 1260 | int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp, |
| 1261 | struct net_device *dev_rx) |
| 1262 | { |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | static inline |
Jesper Dangaard Brouer | 38edddb | 2018-05-24 16:45:57 +0200 | [diff] [blame] | 1267 | int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp, |
| 1268 | struct net_device *dev_rx) |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 1269 | { |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Toshiaki Makita | 6d5fc19 | 2018-06-14 11:07:42 +0900 | [diff] [blame] | 1273 | struct sk_buff; |
| 1274 | |
| 1275 | static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, |
| 1276 | struct sk_buff *skb, |
| 1277 | struct bpf_prog *xdp_prog) |
| 1278 | { |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1282 | static inline |
| 1283 | struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key) |
| 1284 | { |
| 1285 | return NULL; |
| 1286 | } |
| 1287 | |
Björn Töpel | cdfafe9 | 2019-12-19 07:10:04 +0100 | [diff] [blame] | 1288 | static inline void __cpu_map_flush(void) |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1289 | { |
| 1290 | } |
| 1291 | |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1292 | static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, |
| 1293 | struct xdp_buff *xdp, |
| 1294 | struct net_device *dev_rx) |
| 1295 | { |
| 1296 | return 0; |
| 1297 | } |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1298 | |
| 1299 | static inline struct bpf_prog *bpf_prog_get_type_path(const char *name, |
| 1300 | enum bpf_prog_type type) |
| 1301 | { |
| 1302 | return ERR_PTR(-EOPNOTSUPP); |
| 1303 | } |
Stanislav Fomichev | c695865 | 2019-04-11 09:12:02 -0700 | [diff] [blame] | 1304 | |
| 1305 | static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog, |
| 1306 | const union bpf_attr *kattr, |
| 1307 | union bpf_attr __user *uattr) |
| 1308 | { |
| 1309 | return -ENOTSUPP; |
| 1310 | } |
| 1311 | |
| 1312 | static inline int bpf_prog_test_run_skb(struct bpf_prog *prog, |
| 1313 | const union bpf_attr *kattr, |
| 1314 | union bpf_attr __user *uattr) |
| 1315 | { |
| 1316 | return -ENOTSUPP; |
| 1317 | } |
| 1318 | |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 1319 | static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog, |
| 1320 | const union bpf_attr *kattr, |
| 1321 | union bpf_attr __user *uattr) |
| 1322 | { |
| 1323 | return -ENOTSUPP; |
| 1324 | } |
| 1325 | |
Stanislav Fomichev | c695865 | 2019-04-11 09:12:02 -0700 | [diff] [blame] | 1326 | static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
| 1327 | const union bpf_attr *kattr, |
| 1328 | union bpf_attr __user *uattr) |
| 1329 | { |
| 1330 | return -ENOTSUPP; |
| 1331 | } |
Daniel Borkmann | 6332be0 | 2019-11-22 21:07:55 +0100 | [diff] [blame] | 1332 | |
| 1333 | static inline void bpf_map_put(struct bpf_map *map) |
| 1334 | { |
| 1335 | } |
Björn Töpel | 7e6897f | 2019-12-13 18:51:09 +0100 | [diff] [blame] | 1336 | |
| 1337 | static inline struct bpf_prog *bpf_prog_by_id(u32 id) |
| 1338 | { |
| 1339 | return ERR_PTR(-ENOTSUPP); |
| 1340 | } |
Daniel Borkmann | 61e021f3 | 2015-03-02 15:21:55 +0100 | [diff] [blame] | 1341 | #endif /* CONFIG_BPF_SYSCALL */ |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1342 | |
Jakub Kicinski | 479321e | 2017-11-20 15:21:56 -0800 | [diff] [blame] | 1343 | static inline struct bpf_prog *bpf_prog_get_type(u32 ufd, |
| 1344 | enum bpf_prog_type type) |
| 1345 | { |
| 1346 | return bpf_prog_get_type_dev(ufd, type, false); |
| 1347 | } |
| 1348 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1349 | bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool); |
| 1350 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1351 | int bpf_prog_offload_compile(struct bpf_prog *prog); |
| 1352 | void bpf_prog_offload_destroy(struct bpf_prog *prog); |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 1353 | int bpf_prog_offload_info_fill(struct bpf_prog_info *info, |
| 1354 | struct bpf_prog *prog); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1355 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 1356 | int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map); |
| 1357 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1358 | int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value); |
| 1359 | int bpf_map_offload_update_elem(struct bpf_map *map, |
| 1360 | void *key, void *value, u64 flags); |
| 1361 | int bpf_map_offload_delete_elem(struct bpf_map *map, void *key); |
| 1362 | int bpf_map_offload_get_next_key(struct bpf_map *map, |
| 1363 | void *key, void *next_key); |
| 1364 | |
Jakub Kicinski | 0972826 | 2018-07-17 10:53:23 -0700 | [diff] [blame] | 1365 | bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1366 | |
Quentin Monnet | 1385d75 | 2018-11-09 13:03:25 +0000 | [diff] [blame] | 1367 | struct bpf_offload_dev * |
Jakub Kicinski | dd27c2e | 2019-02-12 00:20:39 -0800 | [diff] [blame] | 1368 | bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv); |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 1369 | void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev); |
Jakub Kicinski | dd27c2e | 2019-02-12 00:20:39 -0800 | [diff] [blame] | 1370 | void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev); |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 1371 | int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev, |
| 1372 | struct net_device *netdev); |
| 1373 | void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, |
| 1374 | struct net_device *netdev); |
Jakub Kicinski | fd4f227 | 2018-07-17 10:53:26 -0700 | [diff] [blame] | 1375 | bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev); |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 1376 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1377 | #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL) |
| 1378 | int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr); |
| 1379 | |
Jakub Kicinski | 0d83003 | 2018-05-08 19:37:06 -0700 | [diff] [blame] | 1380 | static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux) |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1381 | { |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1382 | return aux->offload_requested; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1383 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1384 | |
| 1385 | static inline bool bpf_map_is_dev_bound(struct bpf_map *map) |
| 1386 | { |
| 1387 | return unlikely(map->ops == &bpf_map_offload_ops); |
| 1388 | } |
| 1389 | |
| 1390 | struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr); |
| 1391 | void bpf_map_offload_map_free(struct bpf_map *map); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1392 | #else |
| 1393 | static inline int bpf_prog_offload_init(struct bpf_prog *prog, |
| 1394 | union bpf_attr *attr) |
| 1395 | { |
| 1396 | return -EOPNOTSUPP; |
| 1397 | } |
| 1398 | |
| 1399 | static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux) |
| 1400 | { |
| 1401 | return false; |
| 1402 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1403 | |
| 1404 | static inline bool bpf_map_is_dev_bound(struct bpf_map *map) |
| 1405 | { |
| 1406 | return false; |
| 1407 | } |
| 1408 | |
| 1409 | static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr) |
| 1410 | { |
| 1411 | return ERR_PTR(-EOPNOTSUPP); |
| 1412 | } |
| 1413 | |
| 1414 | static inline void bpf_map_offload_map_free(struct bpf_map *map) |
| 1415 | { |
| 1416 | } |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1417 | #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */ |
| 1418 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 1419 | #if defined(CONFIG_BPF_STREAM_PARSER) |
| 1420 | int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which); |
| 1421 | int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog); |
Lorenz Bauer | f747632 | 2020-03-09 11:12:36 +0000 | [diff] [blame^] | 1422 | void sock_map_unhash(struct sock *sk); |
| 1423 | void sock_map_close(struct sock *sk, long timeout); |
John Fastabend | 6bdc9c4 | 2017-08-16 15:02:32 -0700 | [diff] [blame] | 1424 | #else |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 1425 | static inline int sock_map_prog_update(struct bpf_map *map, |
| 1426 | struct bpf_prog *prog, u32 which) |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1427 | { |
| 1428 | return -EOPNOTSUPP; |
| 1429 | } |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1430 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 1431 | static inline int sock_map_get_from_fd(const union bpf_attr *attr, |
| 1432 | struct bpf_prog *prog) |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1433 | { |
| 1434 | return -EINVAL; |
| 1435 | } |
Lorenz Bauer | f747632 | 2020-03-09 11:12:36 +0000 | [diff] [blame^] | 1436 | #endif /* CONFIG_BPF_STREAM_PARSER */ |
John Fastabend | 6bdc9c4 | 2017-08-16 15:02:32 -0700 | [diff] [blame] | 1437 | |
Martin KaFai Lau | 5dc4c4b | 2018-08-08 01:01:24 -0700 | [diff] [blame] | 1438 | #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) |
| 1439 | void bpf_sk_reuseport_detach(struct sock *sk); |
| 1440 | int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key, |
| 1441 | void *value); |
| 1442 | int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, |
| 1443 | void *value, u64 map_flags); |
| 1444 | #else |
| 1445 | static inline void bpf_sk_reuseport_detach(struct sock *sk) |
| 1446 | { |
| 1447 | } |
| 1448 | |
| 1449 | #ifdef CONFIG_BPF_SYSCALL |
| 1450 | static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, |
| 1451 | void *key, void *value) |
| 1452 | { |
| 1453 | return -EOPNOTSUPP; |
| 1454 | } |
| 1455 | |
| 1456 | static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, |
| 1457 | void *key, void *value, |
| 1458 | u64 map_flags) |
| 1459 | { |
| 1460 | return -EOPNOTSUPP; |
| 1461 | } |
| 1462 | #endif /* CONFIG_BPF_SYSCALL */ |
| 1463 | #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */ |
| 1464 | |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 1465 | /* verifier prototypes for helper functions called from eBPF programs */ |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 1466 | extern const struct bpf_func_proto bpf_map_lookup_elem_proto; |
| 1467 | extern const struct bpf_func_proto bpf_map_update_elem_proto; |
| 1468 | extern const struct bpf_func_proto bpf_map_delete_elem_proto; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 1469 | extern const struct bpf_func_proto bpf_map_push_elem_proto; |
| 1470 | extern const struct bpf_func_proto bpf_map_pop_elem_proto; |
| 1471 | extern const struct bpf_func_proto bpf_map_peek_elem_proto; |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 1472 | |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 1473 | extern const struct bpf_func_proto bpf_get_prandom_u32_proto; |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 1474 | extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; |
Daniel Borkmann | 2d0e30c | 2016-10-21 12:46:33 +0200 | [diff] [blame] | 1475 | extern const struct bpf_func_proto bpf_get_numa_node_id_proto; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1476 | extern const struct bpf_func_proto bpf_tail_call_proto; |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 1477 | extern const struct bpf_func_proto bpf_ktime_get_ns_proto; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 1478 | extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto; |
| 1479 | extern const struct bpf_func_proto bpf_get_current_uid_gid_proto; |
| 1480 | extern const struct bpf_func_proto bpf_get_current_comm_proto; |
Alexei Starovoitov | d5a3b1f | 2016-02-17 19:58:58 -0800 | [diff] [blame] | 1481 | extern const struct bpf_func_proto bpf_get_stackid_proto; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 1482 | extern const struct bpf_func_proto bpf_get_stack_proto; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1483 | extern const struct bpf_func_proto bpf_sock_map_update_proto; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 1484 | extern const struct bpf_func_proto bpf_sock_hash_update_proto; |
Yonghong Song | bf6fa2c8 | 2018-06-03 15:59:41 -0700 | [diff] [blame] | 1485 | extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 1486 | extern const struct bpf_func_proto bpf_msg_redirect_hash_proto; |
| 1487 | extern const struct bpf_func_proto bpf_msg_redirect_map_proto; |
| 1488 | extern const struct bpf_func_proto bpf_sk_redirect_hash_proto; |
| 1489 | extern const struct bpf_func_proto bpf_sk_redirect_map_proto; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 1490 | extern const struct bpf_func_proto bpf_spin_lock_proto; |
| 1491 | extern const struct bpf_func_proto bpf_spin_unlock_proto; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 1492 | extern const struct bpf_func_proto bpf_get_local_storage_proto; |
Andrey Ignatov | d7a4cb9 | 2019-03-18 17:55:26 -0700 | [diff] [blame] | 1493 | extern const struct bpf_func_proto bpf_strtol_proto; |
| 1494 | extern const struct bpf_func_proto bpf_strtoul_proto; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1495 | extern const struct bpf_func_proto bpf_tcp_sock_proto; |
Martin KaFai Lau | 5576b99 | 2020-01-22 15:36:46 -0800 | [diff] [blame] | 1496 | extern const struct bpf_func_proto bpf_jiffies64_proto; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 1497 | |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 1498 | /* Shared helpers among cBPF and eBPF. */ |
| 1499 | void bpf_user_rnd_init_once(void); |
| 1500 | u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
| 1501 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1502 | #if defined(CONFIG_NET) |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1503 | bool bpf_sock_common_is_valid_access(int off, int size, |
| 1504 | enum bpf_access_type type, |
| 1505 | struct bpf_insn_access_aux *info); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1506 | bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
| 1507 | struct bpf_insn_access_aux *info); |
| 1508 | u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, |
| 1509 | const struct bpf_insn *si, |
| 1510 | struct bpf_insn *insn_buf, |
| 1511 | struct bpf_prog *prog, |
| 1512 | u32 *target_size); |
| 1513 | #else |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1514 | static inline bool bpf_sock_common_is_valid_access(int off, int size, |
| 1515 | enum bpf_access_type type, |
| 1516 | struct bpf_insn_access_aux *info) |
| 1517 | { |
| 1518 | return false; |
| 1519 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1520 | static inline bool bpf_sock_is_valid_access(int off, int size, |
| 1521 | enum bpf_access_type type, |
| 1522 | struct bpf_insn_access_aux *info) |
| 1523 | { |
| 1524 | return false; |
| 1525 | } |
| 1526 | static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, |
| 1527 | const struct bpf_insn *si, |
| 1528 | struct bpf_insn *insn_buf, |
| 1529 | struct bpf_prog *prog, |
| 1530 | u32 *target_size) |
| 1531 | { |
| 1532 | return 0; |
| 1533 | } |
| 1534 | #endif |
| 1535 | |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1536 | #ifdef CONFIG_INET |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 1537 | struct sk_reuseport_kern { |
| 1538 | struct sk_buff *skb; |
| 1539 | struct sock *sk; |
| 1540 | struct sock *selected_sk; |
| 1541 | void *data_end; |
| 1542 | u32 hash; |
| 1543 | u32 reuseport_id; |
| 1544 | bool bind_inany; |
| 1545 | }; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1546 | bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
| 1547 | struct bpf_insn_access_aux *info); |
| 1548 | |
| 1549 | u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, |
| 1550 | const struct bpf_insn *si, |
| 1551 | struct bpf_insn *insn_buf, |
| 1552 | struct bpf_prog *prog, |
| 1553 | u32 *target_size); |
YueHaibing | 7f94208 | 2019-06-12 17:18:47 +0800 | [diff] [blame] | 1554 | |
| 1555 | bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
| 1556 | struct bpf_insn_access_aux *info); |
| 1557 | |
| 1558 | u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, |
| 1559 | const struct bpf_insn *si, |
| 1560 | struct bpf_insn *insn_buf, |
| 1561 | struct bpf_prog *prog, |
| 1562 | u32 *target_size); |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1563 | #else |
| 1564 | static inline bool bpf_tcp_sock_is_valid_access(int off, int size, |
| 1565 | enum bpf_access_type type, |
| 1566 | struct bpf_insn_access_aux *info) |
| 1567 | { |
| 1568 | return false; |
| 1569 | } |
| 1570 | |
| 1571 | static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, |
| 1572 | const struct bpf_insn *si, |
| 1573 | struct bpf_insn *insn_buf, |
| 1574 | struct bpf_prog *prog, |
| 1575 | u32 *target_size) |
| 1576 | { |
| 1577 | return 0; |
| 1578 | } |
YueHaibing | 7f94208 | 2019-06-12 17:18:47 +0800 | [diff] [blame] | 1579 | static inline bool bpf_xdp_sock_is_valid_access(int off, int size, |
| 1580 | enum bpf_access_type type, |
| 1581 | struct bpf_insn_access_aux *info) |
| 1582 | { |
| 1583 | return false; |
| 1584 | } |
| 1585 | |
| 1586 | static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, |
| 1587 | const struct bpf_insn *si, |
| 1588 | struct bpf_insn *insn_buf, |
| 1589 | struct bpf_prog *prog, |
| 1590 | u32 *target_size) |
| 1591 | { |
| 1592 | return 0; |
| 1593 | } |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1594 | #endif /* CONFIG_INET */ |
| 1595 | |
Alexei Starovoitov | 5964b20 | 2019-11-14 10:57:03 -0800 | [diff] [blame] | 1596 | enum bpf_text_poke_type { |
Daniel Borkmann | b553a6e | 2019-11-24 01:39:42 +0100 | [diff] [blame] | 1597 | BPF_MOD_CALL, |
| 1598 | BPF_MOD_JUMP, |
Alexei Starovoitov | 5964b20 | 2019-11-14 10:57:03 -0800 | [diff] [blame] | 1599 | }; |
Daniel Borkmann | 4b3da77 | 2019-11-22 21:07:54 +0100 | [diff] [blame] | 1600 | |
Alexei Starovoitov | 5964b20 | 2019-11-14 10:57:03 -0800 | [diff] [blame] | 1601 | int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t, |
| 1602 | void *addr1, void *addr2); |
| 1603 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1604 | #endif /* _LINUX_BPF_H */ |