helpers.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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>
  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>
  21. /* If kernel subsystem is allowing eBPF programs to call this function,
  22. * inside its own verifier_ops->get_func_proto() callback it should return
  23. * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
  24. *
  25. * Different map implementations will rely on rcu in map methods
  26. * lookup/update/delete, therefore eBPF programs must run under rcu lock
  27. * if program is allowed to access maps, so check rcu_read_lock_held in
  28. * all three functions.
  29. */
  30. BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
  31. {
  32. WARN_ON_ONCE(!rcu_read_lock_held());
  33. return (unsigned long) map->ops->map_lookup_elem(map, key);
  34. }
  35. const struct bpf_func_proto bpf_map_lookup_elem_proto = {
  36. .func = bpf_map_lookup_elem,
  37. .gpl_only = false,
  38. .pkt_access = true,
  39. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  40. .arg1_type = ARG_CONST_MAP_PTR,
  41. .arg2_type = ARG_PTR_TO_MAP_KEY,
  42. };
  43. BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
  44. void *, value, u64, flags)
  45. {
  46. WARN_ON_ONCE(!rcu_read_lock_held());
  47. return map->ops->map_update_elem(map, key, value, flags);
  48. }
  49. const struct bpf_func_proto bpf_map_update_elem_proto = {
  50. .func = bpf_map_update_elem,
  51. .gpl_only = false,
  52. .pkt_access = true,
  53. .ret_type = RET_INTEGER,
  54. .arg1_type = ARG_CONST_MAP_PTR,
  55. .arg2_type = ARG_PTR_TO_MAP_KEY,
  56. .arg3_type = ARG_PTR_TO_MAP_VALUE,
  57. .arg4_type = ARG_ANYTHING,
  58. };
  59. BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
  60. {
  61. WARN_ON_ONCE(!rcu_read_lock_held());
  62. return map->ops->map_delete_elem(map, key);
  63. }
  64. const struct bpf_func_proto bpf_map_delete_elem_proto = {
  65. .func = bpf_map_delete_elem,
  66. .gpl_only = false,
  67. .pkt_access = true,
  68. .ret_type = RET_INTEGER,
  69. .arg1_type = ARG_CONST_MAP_PTR,
  70. .arg2_type = ARG_PTR_TO_MAP_KEY,
  71. };
  72. BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
  73. {
  74. return map->ops->map_push_elem(map, value, flags);
  75. }
  76. const struct bpf_func_proto bpf_map_push_elem_proto = {
  77. .func = bpf_map_push_elem,
  78. .gpl_only = false,
  79. .pkt_access = true,
  80. .ret_type = RET_INTEGER,
  81. .arg1_type = ARG_CONST_MAP_PTR,
  82. .arg2_type = ARG_PTR_TO_MAP_VALUE,
  83. .arg3_type = ARG_ANYTHING,
  84. };
  85. BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
  86. {
  87. return map->ops->map_pop_elem(map, value);
  88. }
  89. const struct bpf_func_proto bpf_map_pop_elem_proto = {
  90. .func = bpf_map_pop_elem,
  91. .gpl_only = false,
  92. .ret_type = RET_INTEGER,
  93. .arg1_type = ARG_CONST_MAP_PTR,
  94. .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
  95. };
  96. BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
  97. {
  98. return map->ops->map_peek_elem(map, value);
  99. }
  100. const struct bpf_func_proto bpf_map_peek_elem_proto = {
  101. .func = bpf_map_pop_elem,
  102. .gpl_only = false,
  103. .ret_type = RET_INTEGER,
  104. .arg1_type = ARG_CONST_MAP_PTR,
  105. .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
  106. };
  107. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  108. .func = bpf_user_rnd_u32,
  109. .gpl_only = false,
  110. .ret_type = RET_INTEGER,
  111. };
  112. BPF_CALL_0(bpf_get_smp_processor_id)
  113. {
  114. return smp_processor_id();
  115. }
  116. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  117. .func = bpf_get_smp_processor_id,
  118. .gpl_only = false,
  119. .ret_type = RET_INTEGER,
  120. };
  121. BPF_CALL_0(bpf_get_numa_node_id)
  122. {
  123. return numa_node_id();
  124. }
  125. const struct bpf_func_proto bpf_get_numa_node_id_proto = {
  126. .func = bpf_get_numa_node_id,
  127. .gpl_only = false,
  128. .ret_type = RET_INTEGER,
  129. };
  130. BPF_CALL_0(bpf_ktime_get_ns)
  131. {
  132. /* NMI safe access to clock monotonic */
  133. return ktime_get_mono_fast_ns();
  134. }
  135. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  136. .func = bpf_ktime_get_ns,
  137. .gpl_only = true,
  138. .ret_type = RET_INTEGER,
  139. };
  140. BPF_CALL_0(bpf_get_current_pid_tgid)
  141. {
  142. struct task_struct *task = current;
  143. if (unlikely(!task))
  144. return -EINVAL;
  145. return (u64) task->tgid << 32 | task->pid;
  146. }
  147. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  148. .func = bpf_get_current_pid_tgid,
  149. .gpl_only = false,
  150. .ret_type = RET_INTEGER,
  151. };
  152. BPF_CALL_0(bpf_get_current_uid_gid)
  153. {
  154. struct task_struct *task = current;
  155. kuid_t uid;
  156. kgid_t gid;
  157. if (unlikely(!task))
  158. return -EINVAL;
  159. current_uid_gid(&uid, &gid);
  160. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  161. from_kuid(&init_user_ns, uid);
  162. }
  163. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  164. .func = bpf_get_current_uid_gid,
  165. .gpl_only = false,
  166. .ret_type = RET_INTEGER,
  167. };
  168. BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  169. {
  170. struct task_struct *task = current;
  171. if (unlikely(!task))
  172. goto err_clear;
  173. strncpy(buf, task->comm, size);
  174. /* Verifier guarantees that size > 0. For task->comm exceeding
  175. * size, guarantee that buf is %NUL-terminated. Unconditionally
  176. * done here to save the size test.
  177. */
  178. buf[size - 1] = 0;
  179. return 0;
  180. err_clear:
  181. memset(buf, 0, size);
  182. return -EINVAL;
  183. }
  184. const struct bpf_func_proto bpf_get_current_comm_proto = {
  185. .func = bpf_get_current_comm,
  186. .gpl_only = false,
  187. .ret_type = RET_INTEGER,
  188. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  189. .arg2_type = ARG_CONST_SIZE,
  190. };
  191. #ifdef CONFIG_CGROUPS
  192. BPF_CALL_0(bpf_get_current_cgroup_id)
  193. {
  194. struct cgroup *cgrp = task_dfl_cgroup(current);
  195. return cgrp->kn->id.id;
  196. }
  197. const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
  198. .func = bpf_get_current_cgroup_id,
  199. .gpl_only = false,
  200. .ret_type = RET_INTEGER,
  201. };
  202. #ifdef CONFIG_CGROUP_BPF
  203. DECLARE_PER_CPU(struct bpf_cgroup_storage*,
  204. bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
  205. BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
  206. {
  207. /* flags argument is not used now,
  208. * but provides an ability to extend the API.
  209. * verifier checks that its value is correct.
  210. */
  211. enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
  212. struct bpf_cgroup_storage *storage;
  213. void *ptr;
  214. storage = this_cpu_read(bpf_cgroup_storage[stype]);
  215. if (stype == BPF_CGROUP_STORAGE_SHARED)
  216. ptr = &READ_ONCE(storage->buf)->data[0];
  217. else
  218. ptr = this_cpu_ptr(storage->percpu_buf);
  219. return (unsigned long)ptr;
  220. }
  221. const struct bpf_func_proto bpf_get_local_storage_proto = {
  222. .func = bpf_get_local_storage,
  223. .gpl_only = false,
  224. .ret_type = RET_PTR_TO_MAP_VALUE,
  225. .arg1_type = ARG_CONST_MAP_PTR,
  226. .arg2_type = ARG_ANYTHING,
  227. };
  228. #endif
  229. #endif