blob: 95eb59a045ea68fcad84a413eb52fcd11d3e6f5c [file] [log] [blame]
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001/* 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>
13#include <linux/rcupdate.h>
Daniel Borkmann03e69b52015-03-14 02:27:16 +010014#include <linux/random.h>
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080015
16/* If kernel subsystem is allowing eBPF programs to call this function,
17 * inside its own verifier_ops->get_func_proto() callback it should return
18 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
19 *
20 * Different map implementations will rely on rcu in map methods
21 * lookup/update/delete, therefore eBPF programs must run under rcu lock
22 * if program is allowed to access maps, so check rcu_read_lock_held in
23 * all three functions.
24 */
25static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
26{
27 /* verifier checked that R1 contains a valid pointer to bpf_map
28 * and R2 points to a program stack and map->key_size bytes were
29 * initialized
30 */
31 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
32 void *key = (void *) (unsigned long) r2;
33 void *value;
34
35 WARN_ON_ONCE(!rcu_read_lock_held());
36
37 value = map->ops->map_lookup_elem(map, key);
38
39 /* lookup() returns either pointer to element value or NULL
40 * which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type
41 */
42 return (unsigned long) value;
43}
44
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010045const struct bpf_func_proto bpf_map_lookup_elem_proto = {
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080046 .func = bpf_map_lookup_elem,
47 .gpl_only = false,
48 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
49 .arg1_type = ARG_CONST_MAP_PTR,
50 .arg2_type = ARG_PTR_TO_MAP_KEY,
51};
52
53static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
54{
55 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
56 void *key = (void *) (unsigned long) r2;
57 void *value = (void *) (unsigned long) r3;
58
59 WARN_ON_ONCE(!rcu_read_lock_held());
60
61 return map->ops->map_update_elem(map, key, value, r4);
62}
63
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010064const struct bpf_func_proto bpf_map_update_elem_proto = {
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080065 .func = bpf_map_update_elem,
66 .gpl_only = false,
67 .ret_type = RET_INTEGER,
68 .arg1_type = ARG_CONST_MAP_PTR,
69 .arg2_type = ARG_PTR_TO_MAP_KEY,
70 .arg3_type = ARG_PTR_TO_MAP_VALUE,
71 .arg4_type = ARG_ANYTHING,
72};
73
74static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
75{
76 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
77 void *key = (void *) (unsigned long) r2;
78
79 WARN_ON_ONCE(!rcu_read_lock_held());
80
81 return map->ops->map_delete_elem(map, key);
82}
83
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010084const struct bpf_func_proto bpf_map_delete_elem_proto = {
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080085 .func = bpf_map_delete_elem,
86 .gpl_only = false,
87 .ret_type = RET_INTEGER,
88 .arg1_type = ARG_CONST_MAP_PTR,
89 .arg2_type = ARG_PTR_TO_MAP_KEY,
90};
Daniel Borkmann03e69b52015-03-14 02:27:16 +010091
92static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
93{
94 return prandom_u32();
95}
96
97const struct bpf_func_proto bpf_get_prandom_u32_proto = {
98 .func = bpf_get_prandom_u32,
99 .gpl_only = false,
100 .ret_type = RET_INTEGER,
101};