Commit | Line | Data |
---|---|---|
99c55f7d AS |
1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
2 | * | |
3 | * This program is free software; you can redistribute it and/or | |
4 | * modify it under the terms of version 2 of the GNU General Public | |
5 | * License as published by the Free Software Foundation. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, but | |
8 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * General Public License for more details. | |
11 | */ | |
12 | #include <linux/bpf.h> | |
a67edbf4 | 13 | #include <linux/bpf_trace.h> |
99c55f7d AS |
14 | #include <linux/syscalls.h> |
15 | #include <linux/slab.h> | |
3f07c014 | 16 | #include <linux/sched/signal.h> |
d407bd25 DB |
17 | #include <linux/vmalloc.h> |
18 | #include <linux/mmzone.h> | |
99c55f7d | 19 | #include <linux/anon_inodes.h> |
db20fd2b | 20 | #include <linux/file.h> |
09756af4 AS |
21 | #include <linux/license.h> |
22 | #include <linux/filter.h> | |
2541517c | 23 | #include <linux/version.h> |
535e7b4b | 24 | #include <linux/kernel.h> |
99c55f7d | 25 | |
b121d1e7 AS |
26 | DEFINE_PER_CPU(int, bpf_prog_active); |
27 | ||
1be7f75d AS |
28 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
29 | ||
40077e0c JB |
30 | static const struct bpf_map_ops * const bpf_map_types[] = { |
31 | #define BPF_PROG_TYPE(_id, _ops) | |
32 | #define BPF_MAP_TYPE(_id, _ops) \ | |
33 | [_id] = &_ops, | |
34 | #include <linux/bpf_types.h> | |
35 | #undef BPF_PROG_TYPE | |
36 | #undef BPF_MAP_TYPE | |
37 | }; | |
99c55f7d AS |
38 | |
39 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) | |
40 | { | |
99c55f7d AS |
41 | struct bpf_map *map; |
42 | ||
40077e0c JB |
43 | if (attr->map_type >= ARRAY_SIZE(bpf_map_types) || |
44 | !bpf_map_types[attr->map_type]) | |
45 | return ERR_PTR(-EINVAL); | |
99c55f7d | 46 | |
40077e0c JB |
47 | map = bpf_map_types[attr->map_type]->map_alloc(attr); |
48 | if (IS_ERR(map)) | |
49 | return map; | |
50 | map->ops = bpf_map_types[attr->map_type]; | |
51 | map->map_type = attr->map_type; | |
52 | return map; | |
99c55f7d AS |
53 | } |
54 | ||
d407bd25 DB |
55 | void *bpf_map_area_alloc(size_t size) |
56 | { | |
57 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't | |
58 | * trigger under memory pressure as we really just want to | |
59 | * fail instead. | |
60 | */ | |
61 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; | |
62 | void *area; | |
63 | ||
64 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { | |
65 | area = kmalloc(size, GFP_USER | flags); | |
66 | if (area != NULL) | |
67 | return area; | |
68 | } | |
69 | ||
19809c2d | 70 | return __vmalloc(size, GFP_KERNEL | flags, PAGE_KERNEL); |
d407bd25 DB |
71 | } |
72 | ||
73 | void bpf_map_area_free(void *area) | |
74 | { | |
75 | kvfree(area); | |
76 | } | |
77 | ||
6c905981 AS |
78 | int bpf_map_precharge_memlock(u32 pages) |
79 | { | |
80 | struct user_struct *user = get_current_user(); | |
81 | unsigned long memlock_limit, cur; | |
82 | ||
83 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
84 | cur = atomic_long_read(&user->locked_vm); | |
85 | free_uid(user); | |
86 | if (cur + pages > memlock_limit) | |
87 | return -EPERM; | |
88 | return 0; | |
89 | } | |
90 | ||
aaac3ba9 AS |
91 | static int bpf_map_charge_memlock(struct bpf_map *map) |
92 | { | |
93 | struct user_struct *user = get_current_user(); | |
94 | unsigned long memlock_limit; | |
95 | ||
96 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
97 | ||
98 | atomic_long_add(map->pages, &user->locked_vm); | |
99 | ||
100 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { | |
101 | atomic_long_sub(map->pages, &user->locked_vm); | |
102 | free_uid(user); | |
103 | return -EPERM; | |
104 | } | |
105 | map->user = user; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static void bpf_map_uncharge_memlock(struct bpf_map *map) | |
110 | { | |
111 | struct user_struct *user = map->user; | |
112 | ||
113 | atomic_long_sub(map->pages, &user->locked_vm); | |
114 | free_uid(user); | |
115 | } | |
116 | ||
99c55f7d AS |
117 | /* called from workqueue */ |
118 | static void bpf_map_free_deferred(struct work_struct *work) | |
119 | { | |
120 | struct bpf_map *map = container_of(work, struct bpf_map, work); | |
121 | ||
aaac3ba9 | 122 | bpf_map_uncharge_memlock(map); |
99c55f7d AS |
123 | /* implementation dependent freeing */ |
124 | map->ops->map_free(map); | |
125 | } | |
126 | ||
c9da161c DB |
127 | static void bpf_map_put_uref(struct bpf_map *map) |
128 | { | |
129 | if (atomic_dec_and_test(&map->usercnt)) { | |
130 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) | |
131 | bpf_fd_array_map_clear(map); | |
132 | } | |
133 | } | |
134 | ||
99c55f7d AS |
135 | /* decrement map refcnt and schedule it for freeing via workqueue |
136 | * (unrelying map implementation ops->map_free() might sleep) | |
137 | */ | |
138 | void bpf_map_put(struct bpf_map *map) | |
139 | { | |
140 | if (atomic_dec_and_test(&map->refcnt)) { | |
141 | INIT_WORK(&map->work, bpf_map_free_deferred); | |
142 | schedule_work(&map->work); | |
143 | } | |
144 | } | |
145 | ||
c9da161c | 146 | void bpf_map_put_with_uref(struct bpf_map *map) |
99c55f7d | 147 | { |
c9da161c | 148 | bpf_map_put_uref(map); |
99c55f7d | 149 | bpf_map_put(map); |
c9da161c DB |
150 | } |
151 | ||
152 | static int bpf_map_release(struct inode *inode, struct file *filp) | |
153 | { | |
61d1b6a4 DB |
154 | struct bpf_map *map = filp->private_data; |
155 | ||
156 | if (map->ops->map_release) | |
157 | map->ops->map_release(map, filp); | |
158 | ||
159 | bpf_map_put_with_uref(map); | |
99c55f7d AS |
160 | return 0; |
161 | } | |
162 | ||
f99bf205 DB |
163 | #ifdef CONFIG_PROC_FS |
164 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) | |
165 | { | |
166 | const struct bpf_map *map = filp->private_data; | |
21116b70 DB |
167 | const struct bpf_array *array; |
168 | u32 owner_prog_type = 0; | |
169 | ||
170 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { | |
171 | array = container_of(map, struct bpf_array, map); | |
172 | owner_prog_type = array->owner_prog_type; | |
173 | } | |
f99bf205 DB |
174 | |
175 | seq_printf(m, | |
176 | "map_type:\t%u\n" | |
177 | "key_size:\t%u\n" | |
178 | "value_size:\t%u\n" | |
322cea2f | 179 | "max_entries:\t%u\n" |
21116b70 DB |
180 | "map_flags:\t%#x\n" |
181 | "memlock:\t%llu\n", | |
f99bf205 DB |
182 | map->map_type, |
183 | map->key_size, | |
184 | map->value_size, | |
322cea2f | 185 | map->max_entries, |
21116b70 DB |
186 | map->map_flags, |
187 | map->pages * 1ULL << PAGE_SHIFT); | |
188 | ||
189 | if (owner_prog_type) | |
190 | seq_printf(m, "owner_prog_type:\t%u\n", | |
191 | owner_prog_type); | |
f99bf205 DB |
192 | } |
193 | #endif | |
194 | ||
99c55f7d | 195 | static const struct file_operations bpf_map_fops = { |
f99bf205 DB |
196 | #ifdef CONFIG_PROC_FS |
197 | .show_fdinfo = bpf_map_show_fdinfo, | |
198 | #endif | |
199 | .release = bpf_map_release, | |
99c55f7d AS |
200 | }; |
201 | ||
b2197755 | 202 | int bpf_map_new_fd(struct bpf_map *map) |
aa79781b DB |
203 | { |
204 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, | |
205 | O_RDWR | O_CLOEXEC); | |
206 | } | |
207 | ||
99c55f7d AS |
208 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
209 | #define CHECK_ATTR(CMD) \ | |
210 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ | |
211 | sizeof(attr->CMD##_LAST_FIELD), 0, \ | |
212 | sizeof(*attr) - \ | |
213 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ | |
214 | sizeof(attr->CMD##_LAST_FIELD)) != NULL | |
215 | ||
56f668df | 216 | #define BPF_MAP_CREATE_LAST_FIELD inner_map_fd |
99c55f7d AS |
217 | /* called via syscall */ |
218 | static int map_create(union bpf_attr *attr) | |
219 | { | |
220 | struct bpf_map *map; | |
221 | int err; | |
222 | ||
223 | err = CHECK_ATTR(BPF_MAP_CREATE); | |
224 | if (err) | |
225 | return -EINVAL; | |
226 | ||
227 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ | |
228 | map = find_and_alloc_map(attr); | |
229 | if (IS_ERR(map)) | |
230 | return PTR_ERR(map); | |
231 | ||
232 | atomic_set(&map->refcnt, 1); | |
c9da161c | 233 | atomic_set(&map->usercnt, 1); |
99c55f7d | 234 | |
aaac3ba9 AS |
235 | err = bpf_map_charge_memlock(map); |
236 | if (err) | |
20b2b24f | 237 | goto free_map_nouncharge; |
aaac3ba9 | 238 | |
aa79781b | 239 | err = bpf_map_new_fd(map); |
99c55f7d AS |
240 | if (err < 0) |
241 | /* failed to allocate fd */ | |
242 | goto free_map; | |
243 | ||
a67edbf4 | 244 | trace_bpf_map_create(map, err); |
99c55f7d AS |
245 | return err; |
246 | ||
247 | free_map: | |
20b2b24f DB |
248 | bpf_map_uncharge_memlock(map); |
249 | free_map_nouncharge: | |
99c55f7d AS |
250 | map->ops->map_free(map); |
251 | return err; | |
252 | } | |
253 | ||
db20fd2b AS |
254 | /* if error is returned, fd is released. |
255 | * On success caller should complete fd access with matching fdput() | |
256 | */ | |
c2101297 | 257 | struct bpf_map *__bpf_map_get(struct fd f) |
db20fd2b | 258 | { |
db20fd2b AS |
259 | if (!f.file) |
260 | return ERR_PTR(-EBADF); | |
db20fd2b AS |
261 | if (f.file->f_op != &bpf_map_fops) { |
262 | fdput(f); | |
263 | return ERR_PTR(-EINVAL); | |
264 | } | |
265 | ||
c2101297 DB |
266 | return f.file->private_data; |
267 | } | |
268 | ||
92117d84 AS |
269 | /* prog's and map's refcnt limit */ |
270 | #define BPF_MAX_REFCNT 32768 | |
271 | ||
272 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) | |
c9da161c | 273 | { |
92117d84 AS |
274 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
275 | atomic_dec(&map->refcnt); | |
276 | return ERR_PTR(-EBUSY); | |
277 | } | |
c9da161c DB |
278 | if (uref) |
279 | atomic_inc(&map->usercnt); | |
92117d84 | 280 | return map; |
c9da161c DB |
281 | } |
282 | ||
283 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) | |
c2101297 DB |
284 | { |
285 | struct fd f = fdget(ufd); | |
286 | struct bpf_map *map; | |
287 | ||
288 | map = __bpf_map_get(f); | |
289 | if (IS_ERR(map)) | |
290 | return map; | |
291 | ||
92117d84 | 292 | map = bpf_map_inc(map, true); |
c2101297 | 293 | fdput(f); |
db20fd2b AS |
294 | |
295 | return map; | |
296 | } | |
297 | ||
b8cdc051 AS |
298 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
299 | { | |
300 | return -ENOTSUPP; | |
301 | } | |
302 | ||
db20fd2b AS |
303 | /* last field in 'union bpf_attr' used by this command */ |
304 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value | |
305 | ||
306 | static int map_lookup_elem(union bpf_attr *attr) | |
307 | { | |
535e7b4b MS |
308 | void __user *ukey = u64_to_user_ptr(attr->key); |
309 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 310 | int ufd = attr->map_fd; |
db20fd2b | 311 | struct bpf_map *map; |
8ebe667c | 312 | void *key, *value, *ptr; |
15a07b33 | 313 | u32 value_size; |
592867bf | 314 | struct fd f; |
db20fd2b AS |
315 | int err; |
316 | ||
317 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) | |
318 | return -EINVAL; | |
319 | ||
592867bf | 320 | f = fdget(ufd); |
c2101297 | 321 | map = __bpf_map_get(f); |
db20fd2b AS |
322 | if (IS_ERR(map)) |
323 | return PTR_ERR(map); | |
324 | ||
325 | err = -ENOMEM; | |
326 | key = kmalloc(map->key_size, GFP_USER); | |
327 | if (!key) | |
328 | goto err_put; | |
329 | ||
330 | err = -EFAULT; | |
331 | if (copy_from_user(key, ukey, map->key_size) != 0) | |
332 | goto free_key; | |
333 | ||
15a07b33 | 334 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 335 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
15a07b33 AS |
336 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
337 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); | |
338 | else | |
339 | value_size = map->value_size; | |
340 | ||
8ebe667c | 341 | err = -ENOMEM; |
15a07b33 | 342 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b | 343 | if (!value) |
8ebe667c AS |
344 | goto free_key; |
345 | ||
8f844938 MKL |
346 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
347 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
348 | err = bpf_percpu_hash_copy(map, key, value); |
349 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
350 | err = bpf_percpu_array_copy(map, key, value); | |
557c0c6e AS |
351 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
352 | err = bpf_stackmap_copy(map, key, value); | |
bcc6b1b7 MKL |
353 | } else if (map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
354 | map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { | |
56f668df | 355 | err = -ENOTSUPP; |
15a07b33 AS |
356 | } else { |
357 | rcu_read_lock(); | |
358 | ptr = map->ops->map_lookup_elem(map, key); | |
359 | if (ptr) | |
360 | memcpy(value, ptr, value_size); | |
361 | rcu_read_unlock(); | |
362 | err = ptr ? 0 : -ENOENT; | |
363 | } | |
8ebe667c | 364 | |
15a07b33 | 365 | if (err) |
8ebe667c | 366 | goto free_value; |
db20fd2b AS |
367 | |
368 | err = -EFAULT; | |
15a07b33 | 369 | if (copy_to_user(uvalue, value, value_size) != 0) |
8ebe667c | 370 | goto free_value; |
db20fd2b | 371 | |
a67edbf4 | 372 | trace_bpf_map_lookup_elem(map, ufd, key, value); |
db20fd2b AS |
373 | err = 0; |
374 | ||
8ebe667c AS |
375 | free_value: |
376 | kfree(value); | |
db20fd2b AS |
377 | free_key: |
378 | kfree(key); | |
379 | err_put: | |
380 | fdput(f); | |
381 | return err; | |
382 | } | |
383 | ||
3274f520 | 384 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
db20fd2b AS |
385 | |
386 | static int map_update_elem(union bpf_attr *attr) | |
387 | { | |
535e7b4b MS |
388 | void __user *ukey = u64_to_user_ptr(attr->key); |
389 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 390 | int ufd = attr->map_fd; |
db20fd2b AS |
391 | struct bpf_map *map; |
392 | void *key, *value; | |
15a07b33 | 393 | u32 value_size; |
592867bf | 394 | struct fd f; |
db20fd2b AS |
395 | int err; |
396 | ||
397 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) | |
398 | return -EINVAL; | |
399 | ||
592867bf | 400 | f = fdget(ufd); |
c2101297 | 401 | map = __bpf_map_get(f); |
db20fd2b AS |
402 | if (IS_ERR(map)) |
403 | return PTR_ERR(map); | |
404 | ||
405 | err = -ENOMEM; | |
406 | key = kmalloc(map->key_size, GFP_USER); | |
407 | if (!key) | |
408 | goto err_put; | |
409 | ||
410 | err = -EFAULT; | |
411 | if (copy_from_user(key, ukey, map->key_size) != 0) | |
412 | goto free_key; | |
413 | ||
15a07b33 | 414 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 415 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
15a07b33 AS |
416 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
417 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); | |
418 | else | |
419 | value_size = map->value_size; | |
420 | ||
db20fd2b | 421 | err = -ENOMEM; |
15a07b33 | 422 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b AS |
423 | if (!value) |
424 | goto free_key; | |
425 | ||
426 | err = -EFAULT; | |
15a07b33 | 427 | if (copy_from_user(value, uvalue, value_size) != 0) |
db20fd2b AS |
428 | goto free_value; |
429 | ||
b121d1e7 AS |
430 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
431 | * inside bpf map update or delete otherwise deadlocks are possible | |
432 | */ | |
433 | preempt_disable(); | |
434 | __this_cpu_inc(bpf_prog_active); | |
8f844938 MKL |
435 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
436 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
437 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
438 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
439 | err = bpf_percpu_array_update(map, key, value, attr->flags); | |
d056a788 | 440 | } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || |
4ed8ec52 | 441 | map->map_type == BPF_MAP_TYPE_PROG_ARRAY || |
56f668df MKL |
442 | map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || |
443 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) { | |
d056a788 DB |
444 | rcu_read_lock(); |
445 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, | |
446 | attr->flags); | |
447 | rcu_read_unlock(); | |
bcc6b1b7 MKL |
448 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
449 | rcu_read_lock(); | |
450 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, | |
451 | attr->flags); | |
452 | rcu_read_unlock(); | |
15a07b33 AS |
453 | } else { |
454 | rcu_read_lock(); | |
455 | err = map->ops->map_update_elem(map, key, value, attr->flags); | |
456 | rcu_read_unlock(); | |
457 | } | |
b121d1e7 AS |
458 | __this_cpu_dec(bpf_prog_active); |
459 | preempt_enable(); | |
db20fd2b | 460 | |
a67edbf4 DB |
461 | if (!err) |
462 | trace_bpf_map_update_elem(map, ufd, key, value); | |
db20fd2b AS |
463 | free_value: |
464 | kfree(value); | |
465 | free_key: | |
466 | kfree(key); | |
467 | err_put: | |
468 | fdput(f); | |
469 | return err; | |
470 | } | |
471 | ||
472 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key | |
473 | ||
474 | static int map_delete_elem(union bpf_attr *attr) | |
475 | { | |
535e7b4b | 476 | void __user *ukey = u64_to_user_ptr(attr->key); |
db20fd2b | 477 | int ufd = attr->map_fd; |
db20fd2b | 478 | struct bpf_map *map; |
592867bf | 479 | struct fd f; |
db20fd2b AS |
480 | void *key; |
481 | int err; | |
482 | ||
483 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) | |
484 | return -EINVAL; | |
485 | ||
592867bf | 486 | f = fdget(ufd); |
c2101297 | 487 | map = __bpf_map_get(f); |
db20fd2b AS |
488 | if (IS_ERR(map)) |
489 | return PTR_ERR(map); | |
490 | ||
491 | err = -ENOMEM; | |
492 | key = kmalloc(map->key_size, GFP_USER); | |
493 | if (!key) | |
494 | goto err_put; | |
495 | ||
496 | err = -EFAULT; | |
497 | if (copy_from_user(key, ukey, map->key_size) != 0) | |
498 | goto free_key; | |
499 | ||
b121d1e7 AS |
500 | preempt_disable(); |
501 | __this_cpu_inc(bpf_prog_active); | |
db20fd2b AS |
502 | rcu_read_lock(); |
503 | err = map->ops->map_delete_elem(map, key); | |
504 | rcu_read_unlock(); | |
b121d1e7 AS |
505 | __this_cpu_dec(bpf_prog_active); |
506 | preempt_enable(); | |
db20fd2b | 507 | |
a67edbf4 DB |
508 | if (!err) |
509 | trace_bpf_map_delete_elem(map, ufd, key); | |
db20fd2b AS |
510 | free_key: |
511 | kfree(key); | |
512 | err_put: | |
513 | fdput(f); | |
514 | return err; | |
515 | } | |
516 | ||
517 | /* last field in 'union bpf_attr' used by this command */ | |
518 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key | |
519 | ||
520 | static int map_get_next_key(union bpf_attr *attr) | |
521 | { | |
535e7b4b MS |
522 | void __user *ukey = u64_to_user_ptr(attr->key); |
523 | void __user *unext_key = u64_to_user_ptr(attr->next_key); | |
db20fd2b | 524 | int ufd = attr->map_fd; |
db20fd2b AS |
525 | struct bpf_map *map; |
526 | void *key, *next_key; | |
592867bf | 527 | struct fd f; |
db20fd2b AS |
528 | int err; |
529 | ||
530 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) | |
531 | return -EINVAL; | |
532 | ||
592867bf | 533 | f = fdget(ufd); |
c2101297 | 534 | map = __bpf_map_get(f); |
db20fd2b AS |
535 | if (IS_ERR(map)) |
536 | return PTR_ERR(map); | |
537 | ||
8fe45924 TQ |
538 | if (ukey) { |
539 | err = -ENOMEM; | |
540 | key = kmalloc(map->key_size, GFP_USER); | |
541 | if (!key) | |
542 | goto err_put; | |
543 | ||
544 | err = -EFAULT; | |
545 | if (copy_from_user(key, ukey, map->key_size) != 0) | |
546 | goto free_key; | |
547 | } else { | |
548 | key = NULL; | |
549 | } | |
db20fd2b AS |
550 | |
551 | err = -ENOMEM; | |
552 | next_key = kmalloc(map->key_size, GFP_USER); | |
553 | if (!next_key) | |
554 | goto free_key; | |
555 | ||
556 | rcu_read_lock(); | |
557 | err = map->ops->map_get_next_key(map, key, next_key); | |
558 | rcu_read_unlock(); | |
559 | if (err) | |
560 | goto free_next_key; | |
561 | ||
562 | err = -EFAULT; | |
563 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) | |
564 | goto free_next_key; | |
565 | ||
a67edbf4 | 566 | trace_bpf_map_next_key(map, ufd, key, next_key); |
db20fd2b AS |
567 | err = 0; |
568 | ||
569 | free_next_key: | |
570 | kfree(next_key); | |
571 | free_key: | |
572 | kfree(key); | |
573 | err_put: | |
574 | fdput(f); | |
575 | return err; | |
576 | } | |
577 | ||
be9370a7 JB |
578 | static const struct bpf_verifier_ops * const bpf_prog_types[] = { |
579 | #define BPF_PROG_TYPE(_id, _ops) \ | |
580 | [_id] = &_ops, | |
40077e0c | 581 | #define BPF_MAP_TYPE(_id, _ops) |
be9370a7 JB |
582 | #include <linux/bpf_types.h> |
583 | #undef BPF_PROG_TYPE | |
40077e0c | 584 | #undef BPF_MAP_TYPE |
be9370a7 | 585 | }; |
09756af4 AS |
586 | |
587 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) | |
588 | { | |
be9370a7 JB |
589 | if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type]) |
590 | return -EINVAL; | |
09756af4 | 591 | |
be9370a7 JB |
592 | prog->aux->ops = bpf_prog_types[type]; |
593 | prog->type = type; | |
594 | return 0; | |
09756af4 AS |
595 | } |
596 | ||
597 | /* drop refcnt on maps used by eBPF program and free auxilary data */ | |
598 | static void free_used_maps(struct bpf_prog_aux *aux) | |
599 | { | |
600 | int i; | |
601 | ||
602 | for (i = 0; i < aux->used_map_cnt; i++) | |
603 | bpf_map_put(aux->used_maps[i]); | |
604 | ||
605 | kfree(aux->used_maps); | |
606 | } | |
607 | ||
5ccb071e DB |
608 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
609 | { | |
610 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
611 | unsigned long user_bufs; | |
612 | ||
613 | if (user) { | |
614 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); | |
615 | if (user_bufs > memlock_limit) { | |
616 | atomic_long_sub(pages, &user->locked_vm); | |
617 | return -EPERM; | |
618 | } | |
619 | } | |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
624 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) | |
625 | { | |
626 | if (user) | |
627 | atomic_long_sub(pages, &user->locked_vm); | |
628 | } | |
629 | ||
aaac3ba9 AS |
630 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
631 | { | |
632 | struct user_struct *user = get_current_user(); | |
5ccb071e | 633 | int ret; |
aaac3ba9 | 634 | |
5ccb071e DB |
635 | ret = __bpf_prog_charge(user, prog->pages); |
636 | if (ret) { | |
aaac3ba9 | 637 | free_uid(user); |
5ccb071e | 638 | return ret; |
aaac3ba9 | 639 | } |
5ccb071e | 640 | |
aaac3ba9 AS |
641 | prog->aux->user = user; |
642 | return 0; | |
643 | } | |
644 | ||
645 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) | |
646 | { | |
647 | struct user_struct *user = prog->aux->user; | |
648 | ||
5ccb071e | 649 | __bpf_prog_uncharge(user, prog->pages); |
aaac3ba9 AS |
650 | free_uid(user); |
651 | } | |
652 | ||
1aacde3d | 653 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
abf2e7d6 AS |
654 | { |
655 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); | |
656 | ||
657 | free_used_maps(aux); | |
aaac3ba9 | 658 | bpf_prog_uncharge_memlock(aux->prog); |
abf2e7d6 AS |
659 | bpf_prog_free(aux->prog); |
660 | } | |
661 | ||
09756af4 AS |
662 | void bpf_prog_put(struct bpf_prog *prog) |
663 | { | |
a67edbf4 DB |
664 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
665 | trace_bpf_prog_put_rcu(prog); | |
74451e66 | 666 | bpf_prog_kallsyms_del(prog); |
1aacde3d | 667 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
a67edbf4 | 668 | } |
09756af4 | 669 | } |
e2e9b654 | 670 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
09756af4 AS |
671 | |
672 | static int bpf_prog_release(struct inode *inode, struct file *filp) | |
673 | { | |
674 | struct bpf_prog *prog = filp->private_data; | |
675 | ||
1aacde3d | 676 | bpf_prog_put(prog); |
09756af4 AS |
677 | return 0; |
678 | } | |
679 | ||
7bd509e3 DB |
680 | #ifdef CONFIG_PROC_FS |
681 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) | |
682 | { | |
683 | const struct bpf_prog *prog = filp->private_data; | |
f1f7714e | 684 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
7bd509e3 | 685 | |
f1f7714e | 686 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
7bd509e3 DB |
687 | seq_printf(m, |
688 | "prog_type:\t%u\n" | |
689 | "prog_jited:\t%u\n" | |
f1f7714e | 690 | "prog_tag:\t%s\n" |
7bd509e3 DB |
691 | "memlock:\t%llu\n", |
692 | prog->type, | |
693 | prog->jited, | |
f1f7714e | 694 | prog_tag, |
7bd509e3 DB |
695 | prog->pages * 1ULL << PAGE_SHIFT); |
696 | } | |
697 | #endif | |
698 | ||
09756af4 | 699 | static const struct file_operations bpf_prog_fops = { |
7bd509e3 DB |
700 | #ifdef CONFIG_PROC_FS |
701 | .show_fdinfo = bpf_prog_show_fdinfo, | |
702 | #endif | |
703 | .release = bpf_prog_release, | |
09756af4 AS |
704 | }; |
705 | ||
b2197755 | 706 | int bpf_prog_new_fd(struct bpf_prog *prog) |
aa79781b DB |
707 | { |
708 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, | |
709 | O_RDWR | O_CLOEXEC); | |
710 | } | |
711 | ||
113214be | 712 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
09756af4 | 713 | { |
09756af4 AS |
714 | if (!f.file) |
715 | return ERR_PTR(-EBADF); | |
09756af4 AS |
716 | if (f.file->f_op != &bpf_prog_fops) { |
717 | fdput(f); | |
718 | return ERR_PTR(-EINVAL); | |
719 | } | |
720 | ||
c2101297 | 721 | return f.file->private_data; |
09756af4 AS |
722 | } |
723 | ||
59d3656d | 724 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
92117d84 | 725 | { |
59d3656d BB |
726 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
727 | atomic_sub(i, &prog->aux->refcnt); | |
92117d84 AS |
728 | return ERR_PTR(-EBUSY); |
729 | } | |
730 | return prog; | |
731 | } | |
59d3656d BB |
732 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
733 | ||
c540594f DB |
734 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
735 | { | |
736 | /* Only to be used for undoing previous bpf_prog_add() in some | |
737 | * error path. We still know that another entity in our call | |
738 | * path holds a reference to the program, thus atomic_sub() can | |
739 | * be safely used in such cases! | |
740 | */ | |
741 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); | |
742 | } | |
743 | EXPORT_SYMBOL_GPL(bpf_prog_sub); | |
744 | ||
59d3656d BB |
745 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
746 | { | |
747 | return bpf_prog_add(prog, 1); | |
748 | } | |
97bc402d | 749 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
92117d84 | 750 | |
113214be | 751 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type) |
09756af4 AS |
752 | { |
753 | struct fd f = fdget(ufd); | |
754 | struct bpf_prog *prog; | |
755 | ||
113214be | 756 | prog = ____bpf_prog_get(f); |
09756af4 AS |
757 | if (IS_ERR(prog)) |
758 | return prog; | |
113214be DB |
759 | if (type && prog->type != *type) { |
760 | prog = ERR_PTR(-EINVAL); | |
761 | goto out; | |
762 | } | |
09756af4 | 763 | |
92117d84 | 764 | prog = bpf_prog_inc(prog); |
113214be | 765 | out: |
09756af4 AS |
766 | fdput(f); |
767 | return prog; | |
768 | } | |
113214be DB |
769 | |
770 | struct bpf_prog *bpf_prog_get(u32 ufd) | |
771 | { | |
772 | return __bpf_prog_get(ufd, NULL); | |
773 | } | |
774 | ||
775 | struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type) | |
776 | { | |
a67edbf4 DB |
777 | struct bpf_prog *prog = __bpf_prog_get(ufd, &type); |
778 | ||
779 | if (!IS_ERR(prog)) | |
780 | trace_bpf_prog_get_type(prog); | |
781 | return prog; | |
113214be DB |
782 | } |
783 | EXPORT_SYMBOL_GPL(bpf_prog_get_type); | |
09756af4 AS |
784 | |
785 | /* last field in 'union bpf_attr' used by this command */ | |
e07b98d9 | 786 | #define BPF_PROG_LOAD_LAST_FIELD prog_flags |
09756af4 AS |
787 | |
788 | static int bpf_prog_load(union bpf_attr *attr) | |
789 | { | |
790 | enum bpf_prog_type type = attr->prog_type; | |
791 | struct bpf_prog *prog; | |
792 | int err; | |
793 | char license[128]; | |
794 | bool is_gpl; | |
795 | ||
796 | if (CHECK_ATTR(BPF_PROG_LOAD)) | |
797 | return -EINVAL; | |
798 | ||
e07b98d9 DM |
799 | if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) |
800 | return -EINVAL; | |
801 | ||
09756af4 | 802 | /* copy eBPF program license from user space */ |
535e7b4b | 803 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
09756af4 AS |
804 | sizeof(license) - 1) < 0) |
805 | return -EFAULT; | |
806 | license[sizeof(license) - 1] = 0; | |
807 | ||
808 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
809 | is_gpl = license_is_gpl_compatible(license); | |
810 | ||
ef0915ca DB |
811 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
812 | return -E2BIG; | |
09756af4 | 813 | |
2541517c AS |
814 | if (type == BPF_PROG_TYPE_KPROBE && |
815 | attr->kern_version != LINUX_VERSION_CODE) | |
816 | return -EINVAL; | |
817 | ||
80b7d819 CF |
818 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
819 | type != BPF_PROG_TYPE_CGROUP_SKB && | |
820 | !capable(CAP_SYS_ADMIN)) | |
1be7f75d AS |
821 | return -EPERM; |
822 | ||
09756af4 AS |
823 | /* plain bpf_prog allocation */ |
824 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); | |
825 | if (!prog) | |
826 | return -ENOMEM; | |
827 | ||
aaac3ba9 AS |
828 | err = bpf_prog_charge_memlock(prog); |
829 | if (err) | |
830 | goto free_prog_nouncharge; | |
831 | ||
09756af4 AS |
832 | prog->len = attr->insn_cnt; |
833 | ||
834 | err = -EFAULT; | |
535e7b4b | 835 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
aafe6ae9 | 836 | bpf_prog_insn_size(prog)) != 0) |
09756af4 AS |
837 | goto free_prog; |
838 | ||
839 | prog->orig_prog = NULL; | |
a91263d5 | 840 | prog->jited = 0; |
09756af4 AS |
841 | |
842 | atomic_set(&prog->aux->refcnt, 1); | |
a91263d5 | 843 | prog->gpl_compatible = is_gpl ? 1 : 0; |
09756af4 AS |
844 | |
845 | /* find program type: socket_filter vs tracing_filter */ | |
846 | err = find_prog_type(type, prog); | |
847 | if (err < 0) | |
848 | goto free_prog; | |
849 | ||
850 | /* run eBPF verifier */ | |
9bac3d6d | 851 | err = bpf_check(&prog, attr); |
09756af4 AS |
852 | if (err < 0) |
853 | goto free_used_maps; | |
854 | ||
855 | /* eBPF program is ready to be JITed */ | |
d1c55ab5 | 856 | prog = bpf_prog_select_runtime(prog, &err); |
04fd61ab AS |
857 | if (err < 0) |
858 | goto free_used_maps; | |
09756af4 | 859 | |
aa79781b | 860 | err = bpf_prog_new_fd(prog); |
09756af4 AS |
861 | if (err < 0) |
862 | /* failed to allocate fd */ | |
863 | goto free_used_maps; | |
864 | ||
74451e66 | 865 | bpf_prog_kallsyms_add(prog); |
a67edbf4 | 866 | trace_bpf_prog_load(prog, err); |
09756af4 AS |
867 | return err; |
868 | ||
869 | free_used_maps: | |
870 | free_used_maps(prog->aux); | |
871 | free_prog: | |
aaac3ba9 AS |
872 | bpf_prog_uncharge_memlock(prog); |
873 | free_prog_nouncharge: | |
09756af4 AS |
874 | bpf_prog_free(prog); |
875 | return err; | |
876 | } | |
877 | ||
b2197755 DB |
878 | #define BPF_OBJ_LAST_FIELD bpf_fd |
879 | ||
880 | static int bpf_obj_pin(const union bpf_attr *attr) | |
881 | { | |
882 | if (CHECK_ATTR(BPF_OBJ)) | |
883 | return -EINVAL; | |
884 | ||
535e7b4b | 885 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
b2197755 DB |
886 | } |
887 | ||
888 | static int bpf_obj_get(const union bpf_attr *attr) | |
889 | { | |
890 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0) | |
891 | return -EINVAL; | |
892 | ||
535e7b4b | 893 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname)); |
b2197755 DB |
894 | } |
895 | ||
f4324551 DM |
896 | #ifdef CONFIG_CGROUP_BPF |
897 | ||
7f677633 | 898 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
f4324551 DM |
899 | |
900 | static int bpf_prog_attach(const union bpf_attr *attr) | |
901 | { | |
7f677633 | 902 | enum bpf_prog_type ptype; |
f4324551 DM |
903 | struct bpf_prog *prog; |
904 | struct cgroup *cgrp; | |
7f677633 | 905 | int ret; |
f4324551 DM |
906 | |
907 | if (!capable(CAP_NET_ADMIN)) | |
908 | return -EPERM; | |
909 | ||
910 | if (CHECK_ATTR(BPF_PROG_ATTACH)) | |
911 | return -EINVAL; | |
912 | ||
7f677633 AS |
913 | if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE) |
914 | return -EINVAL; | |
915 | ||
f4324551 DM |
916 | switch (attr->attach_type) { |
917 | case BPF_CGROUP_INET_INGRESS: | |
918 | case BPF_CGROUP_INET_EGRESS: | |
b2cd1257 | 919 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
f4324551 | 920 | break; |
61023658 DA |
921 | case BPF_CGROUP_INET_SOCK_CREATE: |
922 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; | |
923 | break; | |
f4324551 DM |
924 | default: |
925 | return -EINVAL; | |
926 | } | |
927 | ||
b2cd1257 DA |
928 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
929 | if (IS_ERR(prog)) | |
930 | return PTR_ERR(prog); | |
931 | ||
932 | cgrp = cgroup_get_from_fd(attr->target_fd); | |
933 | if (IS_ERR(cgrp)) { | |
934 | bpf_prog_put(prog); | |
935 | return PTR_ERR(cgrp); | |
936 | } | |
937 | ||
7f677633 AS |
938 | ret = cgroup_bpf_update(cgrp, prog, attr->attach_type, |
939 | attr->attach_flags & BPF_F_ALLOW_OVERRIDE); | |
940 | if (ret) | |
941 | bpf_prog_put(prog); | |
b2cd1257 DA |
942 | cgroup_put(cgrp); |
943 | ||
7f677633 | 944 | return ret; |
f4324551 DM |
945 | } |
946 | ||
947 | #define BPF_PROG_DETACH_LAST_FIELD attach_type | |
948 | ||
949 | static int bpf_prog_detach(const union bpf_attr *attr) | |
950 | { | |
951 | struct cgroup *cgrp; | |
7f677633 | 952 | int ret; |
f4324551 DM |
953 | |
954 | if (!capable(CAP_NET_ADMIN)) | |
955 | return -EPERM; | |
956 | ||
957 | if (CHECK_ATTR(BPF_PROG_DETACH)) | |
958 | return -EINVAL; | |
959 | ||
960 | switch (attr->attach_type) { | |
961 | case BPF_CGROUP_INET_INGRESS: | |
962 | case BPF_CGROUP_INET_EGRESS: | |
61023658 | 963 | case BPF_CGROUP_INET_SOCK_CREATE: |
f4324551 DM |
964 | cgrp = cgroup_get_from_fd(attr->target_fd); |
965 | if (IS_ERR(cgrp)) | |
966 | return PTR_ERR(cgrp); | |
967 | ||
7f677633 | 968 | ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false); |
f4324551 DM |
969 | cgroup_put(cgrp); |
970 | break; | |
971 | ||
972 | default: | |
973 | return -EINVAL; | |
974 | } | |
975 | ||
7f677633 | 976 | return ret; |
f4324551 DM |
977 | } |
978 | #endif /* CONFIG_CGROUP_BPF */ | |
979 | ||
1cf1cae9 AS |
980 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
981 | ||
982 | static int bpf_prog_test_run(const union bpf_attr *attr, | |
983 | union bpf_attr __user *uattr) | |
984 | { | |
985 | struct bpf_prog *prog; | |
986 | int ret = -ENOTSUPP; | |
987 | ||
988 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) | |
989 | return -EINVAL; | |
990 | ||
991 | prog = bpf_prog_get(attr->test.prog_fd); | |
992 | if (IS_ERR(prog)) | |
993 | return PTR_ERR(prog); | |
994 | ||
995 | if (prog->aux->ops->test_run) | |
996 | ret = prog->aux->ops->test_run(prog, attr, uattr); | |
997 | ||
998 | bpf_prog_put(prog); | |
999 | return ret; | |
1000 | } | |
1001 | ||
99c55f7d AS |
1002 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
1003 | { | |
1004 | union bpf_attr attr = {}; | |
1005 | int err; | |
1006 | ||
1be7f75d | 1007 | if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled) |
99c55f7d AS |
1008 | return -EPERM; |
1009 | ||
1010 | if (!access_ok(VERIFY_READ, uattr, 1)) | |
1011 | return -EFAULT; | |
1012 | ||
1013 | if (size > PAGE_SIZE) /* silly large */ | |
1014 | return -E2BIG; | |
1015 | ||
1016 | /* If we're handed a bigger struct than we know of, | |
1017 | * ensure all the unknown bits are 0 - i.e. new | |
1018 | * user-space does not rely on any kernel feature | |
1019 | * extensions we dont know about yet. | |
1020 | */ | |
1021 | if (size > sizeof(attr)) { | |
1022 | unsigned char __user *addr; | |
1023 | unsigned char __user *end; | |
1024 | unsigned char val; | |
1025 | ||
1026 | addr = (void __user *)uattr + sizeof(attr); | |
1027 | end = (void __user *)uattr + size; | |
1028 | ||
1029 | for (; addr < end; addr++) { | |
1030 | err = get_user(val, addr); | |
1031 | if (err) | |
1032 | return err; | |
1033 | if (val) | |
1034 | return -E2BIG; | |
1035 | } | |
1036 | size = sizeof(attr); | |
1037 | } | |
1038 | ||
1039 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ | |
1040 | if (copy_from_user(&attr, uattr, size) != 0) | |
1041 | return -EFAULT; | |
1042 | ||
1043 | switch (cmd) { | |
1044 | case BPF_MAP_CREATE: | |
1045 | err = map_create(&attr); | |
1046 | break; | |
db20fd2b AS |
1047 | case BPF_MAP_LOOKUP_ELEM: |
1048 | err = map_lookup_elem(&attr); | |
1049 | break; | |
1050 | case BPF_MAP_UPDATE_ELEM: | |
1051 | err = map_update_elem(&attr); | |
1052 | break; | |
1053 | case BPF_MAP_DELETE_ELEM: | |
1054 | err = map_delete_elem(&attr); | |
1055 | break; | |
1056 | case BPF_MAP_GET_NEXT_KEY: | |
1057 | err = map_get_next_key(&attr); | |
1058 | break; | |
09756af4 AS |
1059 | case BPF_PROG_LOAD: |
1060 | err = bpf_prog_load(&attr); | |
1061 | break; | |
b2197755 DB |
1062 | case BPF_OBJ_PIN: |
1063 | err = bpf_obj_pin(&attr); | |
1064 | break; | |
1065 | case BPF_OBJ_GET: | |
1066 | err = bpf_obj_get(&attr); | |
1067 | break; | |
f4324551 DM |
1068 | #ifdef CONFIG_CGROUP_BPF |
1069 | case BPF_PROG_ATTACH: | |
1070 | err = bpf_prog_attach(&attr); | |
1071 | break; | |
1072 | case BPF_PROG_DETACH: | |
1073 | err = bpf_prog_detach(&attr); | |
1074 | break; | |
1075 | #endif | |
1cf1cae9 AS |
1076 | case BPF_PROG_TEST_RUN: |
1077 | err = bpf_prog_test_run(&attr, uattr); | |
1078 | break; | |
99c55f7d AS |
1079 | default: |
1080 | err = -EINVAL; | |
1081 | break; | |
1082 | } | |
1083 | ||
1084 | return err; | |
1085 | } |