1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
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.
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.
12 #include <linux/bpf.h>
13 #include <linux/rcupdate.h>
14 #include <linux/random.h>
15 #include <linux/smp.h>
16 #include <linux/topology.h>
17 #include <linux/ktime.h>
18 #include <linux/sched.h>
19 #include <linux/uidgid.h>
20 #include <linux/filter.h>
22 /* If kernel subsystem is allowing eBPF programs to call this function,
23 * inside its own verifier_ops->get_func_proto() callback it should return
24 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
26 * Different map implementations will rely on rcu in map methods
27 * lookup/update/delete, therefore eBPF programs must run under rcu lock
28 * if program is allowed to access maps, so check rcu_read_lock_held in
29 * all three functions.
31 BPF_CALL_2(bpf_map_lookup_elem
, struct bpf_map
*, map
, void *, key
)
33 WARN_ON_ONCE(!rcu_read_lock_held());
34 return (unsigned long) map
->ops
->map_lookup_elem(map
, key
);
37 const struct bpf_func_proto bpf_map_lookup_elem_proto
= {
38 .func
= bpf_map_lookup_elem
,
41 .ret_type
= RET_PTR_TO_MAP_VALUE_OR_NULL
,
42 .arg1_type
= ARG_CONST_MAP_PTR
,
43 .arg2_type
= ARG_PTR_TO_MAP_KEY
,
46 BPF_CALL_4(bpf_map_update_elem
, struct bpf_map
*, map
, void *, key
,
47 void *, value
, u64
, flags
)
49 WARN_ON_ONCE(!rcu_read_lock_held());
50 return map
->ops
->map_update_elem(map
, key
, value
, flags
);
53 const struct bpf_func_proto bpf_map_update_elem_proto
= {
54 .func
= bpf_map_update_elem
,
57 .ret_type
= RET_INTEGER
,
58 .arg1_type
= ARG_CONST_MAP_PTR
,
59 .arg2_type
= ARG_PTR_TO_MAP_KEY
,
60 .arg3_type
= ARG_PTR_TO_MAP_VALUE
,
61 .arg4_type
= ARG_ANYTHING
,
64 BPF_CALL_2(bpf_map_delete_elem
, struct bpf_map
*, map
, void *, key
)
66 WARN_ON_ONCE(!rcu_read_lock_held());
67 return map
->ops
->map_delete_elem(map
, key
);
70 const struct bpf_func_proto bpf_map_delete_elem_proto
= {
71 .func
= bpf_map_delete_elem
,
74 .ret_type
= RET_INTEGER
,
75 .arg1_type
= ARG_CONST_MAP_PTR
,
76 .arg2_type
= ARG_PTR_TO_MAP_KEY
,
79 const struct bpf_func_proto bpf_get_prandom_u32_proto
= {
80 .func
= bpf_user_rnd_u32
,
82 .ret_type
= RET_INTEGER
,
85 BPF_CALL_0(bpf_get_smp_processor_id
)
87 return smp_processor_id();
90 const struct bpf_func_proto bpf_get_smp_processor_id_proto
= {
91 .func
= bpf_get_smp_processor_id
,
93 .ret_type
= RET_INTEGER
,
96 BPF_CALL_0(bpf_get_numa_node_id
)
98 return numa_node_id();
101 const struct bpf_func_proto bpf_get_numa_node_id_proto
= {
102 .func
= bpf_get_numa_node_id
,
104 .ret_type
= RET_INTEGER
,
107 BPF_CALL_0(bpf_ktime_get_ns
)
109 /* NMI safe access to clock monotonic */
110 return ktime_get_mono_fast_ns();
113 const struct bpf_func_proto bpf_ktime_get_ns_proto
= {
114 .func
= bpf_ktime_get_ns
,
116 .ret_type
= RET_INTEGER
,
119 BPF_CALL_0(bpf_get_current_pid_tgid
)
121 struct task_struct
*task
= current
;
126 return (u64
) task
->tgid
<< 32 | task
->pid
;
129 const struct bpf_func_proto bpf_get_current_pid_tgid_proto
= {
130 .func
= bpf_get_current_pid_tgid
,
132 .ret_type
= RET_INTEGER
,
135 BPF_CALL_0(bpf_get_current_uid_gid
)
137 struct task_struct
*task
= current
;
144 current_uid_gid(&uid
, &gid
);
145 return (u64
) from_kgid(&init_user_ns
, gid
) << 32 |
146 from_kuid(&init_user_ns
, uid
);
149 const struct bpf_func_proto bpf_get_current_uid_gid_proto
= {
150 .func
= bpf_get_current_uid_gid
,
152 .ret_type
= RET_INTEGER
,
155 BPF_CALL_2(bpf_get_current_comm
, char *, buf
, u32
, size
)
157 struct task_struct
*task
= current
;
162 strncpy(buf
, task
->comm
, size
);
164 /* Verifier guarantees that size > 0. For task->comm exceeding
165 * size, guarantee that buf is %NUL-terminated. Unconditionally
166 * done here to save the size test.
171 memset(buf
, 0, size
);
175 const struct bpf_func_proto bpf_get_current_comm_proto
= {
176 .func
= bpf_get_current_comm
,
178 .ret_type
= RET_INTEGER
,
179 .arg1_type
= ARG_PTR_TO_RAW_STACK
,
180 .arg2_type
= ARG_CONST_STACK_SIZE
,