reuseport_array.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 Facebook
  4. */
  5. #include <linux/bpf.h>
  6. #include <linux/err.h>
  7. #include <linux/sock_diag.h>
  8. #include <net/sock_reuseport.h>
  9. struct reuseport_array {
  10. struct bpf_map map;
  11. struct sock __rcu *ptrs[];
  12. };
  13. static struct reuseport_array *reuseport_array(struct bpf_map *map)
  14. {
  15. return (struct reuseport_array *)map;
  16. }
  17. /* The caller must hold the reuseport_lock */
  18. void bpf_sk_reuseport_detach(struct sock *sk)
  19. {
  20. uintptr_t sk_user_data;
  21. write_lock_bh(&sk->sk_callback_lock);
  22. sk_user_data = (uintptr_t)sk->sk_user_data;
  23. if (sk_user_data & SK_USER_DATA_BPF) {
  24. struct sock __rcu **socks;
  25. socks = (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
  26. WRITE_ONCE(sk->sk_user_data, NULL);
  27. /*
  28. * Do not move this NULL assignment outside of
  29. * sk->sk_callback_lock because there is
  30. * a race with reuseport_array_free()
  31. * which does not hold the reuseport_lock.
  32. */
  33. RCU_INIT_POINTER(*socks, NULL);
  34. }
  35. write_unlock_bh(&sk->sk_callback_lock);
  36. }
  37. static int reuseport_array_alloc_check(union bpf_attr *attr)
  38. {
  39. if (attr->value_size != sizeof(u32) &&
  40. attr->value_size != sizeof(u64))
  41. return -EINVAL;
  42. return array_map_alloc_check(attr);
  43. }
  44. static void *reuseport_array_lookup_elem(struct bpf_map *map, void *key)
  45. {
  46. struct reuseport_array *array = reuseport_array(map);
  47. u32 index = *(u32 *)key;
  48. if (unlikely(index >= array->map.max_entries))
  49. return NULL;
  50. return rcu_dereference(array->ptrs[index]);
  51. }
  52. /* Called from syscall only */
  53. static int reuseport_array_delete_elem(struct bpf_map *map, void *key)
  54. {
  55. struct reuseport_array *array = reuseport_array(map);
  56. u32 index = *(u32 *)key;
  57. struct sock *sk;
  58. int err;
  59. if (index >= map->max_entries)
  60. return -E2BIG;
  61. if (!rcu_access_pointer(array->ptrs[index]))
  62. return -ENOENT;
  63. spin_lock_bh(&reuseport_lock);
  64. sk = rcu_dereference_protected(array->ptrs[index],
  65. lockdep_is_held(&reuseport_lock));
  66. if (sk) {
  67. write_lock_bh(&sk->sk_callback_lock);
  68. WRITE_ONCE(sk->sk_user_data, NULL);
  69. RCU_INIT_POINTER(array->ptrs[index], NULL);
  70. write_unlock_bh(&sk->sk_callback_lock);
  71. err = 0;
  72. } else {
  73. err = -ENOENT;
  74. }
  75. spin_unlock_bh(&reuseport_lock);
  76. return err;
  77. }
  78. static void reuseport_array_free(struct bpf_map *map)
  79. {
  80. struct reuseport_array *array = reuseport_array(map);
  81. struct sock *sk;
  82. u32 i;
  83. /*
  84. * ops->map_*_elem() will not be able to access this
  85. * array now. Hence, this function only races with
  86. * bpf_sk_reuseport_detach() which was triggerred by
  87. * close() or disconnect().
  88. *
  89. * This function and bpf_sk_reuseport_detach() are
  90. * both removing sk from "array". Who removes it
  91. * first does not matter.
  92. *
  93. * The only concern here is bpf_sk_reuseport_detach()
  94. * may access "array" which is being freed here.
  95. * bpf_sk_reuseport_detach() access this "array"
  96. * through sk->sk_user_data _and_ with sk->sk_callback_lock
  97. * held which is enough because this "array" is not freed
  98. * until all sk->sk_user_data has stopped referencing this "array".
  99. *
  100. * Hence, due to the above, taking "reuseport_lock" is not
  101. * needed here.
  102. */
  103. /*
  104. * Since reuseport_lock is not taken, sk is accessed under
  105. * rcu_read_lock()
  106. */
  107. rcu_read_lock();
  108. for (i = 0; i < map->max_entries; i++) {
  109. sk = rcu_dereference(array->ptrs[i]);
  110. if (sk) {
  111. write_lock_bh(&sk->sk_callback_lock);
  112. /*
  113. * No need for WRITE_ONCE(). At this point,
  114. * no one is reading it without taking the
  115. * sk->sk_callback_lock.
  116. */
  117. sk->sk_user_data = NULL;
  118. write_unlock_bh(&sk->sk_callback_lock);
  119. RCU_INIT_POINTER(array->ptrs[i], NULL);
  120. }
  121. }
  122. rcu_read_unlock();
  123. /*
  124. * Once reaching here, all sk->sk_user_data is not
  125. * referenceing this "array". "array" can be freed now.
  126. */
  127. bpf_map_area_free(array);
  128. }
  129. static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
  130. {
  131. int err, numa_node = bpf_map_attr_numa_node(attr);
  132. struct reuseport_array *array;
  133. struct bpf_map_memory mem;
  134. u64 array_size;
  135. if (!bpf_capable())
  136. return ERR_PTR(-EPERM);
  137. array_size = sizeof(*array);
  138. array_size += (u64)attr->max_entries * sizeof(struct sock *);
  139. err = bpf_map_charge_init(&mem, array_size);
  140. if (err)
  141. return ERR_PTR(err);
  142. /* allocate all map elements and zero-initialize them */
  143. array = bpf_map_area_alloc(array_size, numa_node);
  144. if (!array) {
  145. bpf_map_charge_finish(&mem);
  146. return ERR_PTR(-ENOMEM);
  147. }
  148. /* copy mandatory map attributes */
  149. bpf_map_init_from_attr(&array->map, attr);
  150. bpf_map_charge_move(&array->map.memory, &mem);
  151. return &array->map;
  152. }
  153. int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
  154. void *value)
  155. {
  156. struct sock *sk;
  157. int err;
  158. if (map->value_size != sizeof(u64))
  159. return -ENOSPC;
  160. rcu_read_lock();
  161. sk = reuseport_array_lookup_elem(map, key);
  162. if (sk) {
  163. *(u64 *)value = __sock_gen_cookie(sk);
  164. err = 0;
  165. } else {
  166. err = -ENOENT;
  167. }
  168. rcu_read_unlock();
  169. return err;
  170. }
  171. static int
  172. reuseport_array_update_check(const struct reuseport_array *array,
  173. const struct sock *nsk,
  174. const struct sock *osk,
  175. const struct sock_reuseport *nsk_reuse,
  176. u32 map_flags)
  177. {
  178. if (osk && map_flags == BPF_NOEXIST)
  179. return -EEXIST;
  180. if (!osk && map_flags == BPF_EXIST)
  181. return -ENOENT;
  182. if (nsk->sk_protocol != IPPROTO_UDP && nsk->sk_protocol != IPPROTO_TCP)
  183. return -ENOTSUPP;
  184. if (nsk->sk_family != AF_INET && nsk->sk_family != AF_INET6)
  185. return -ENOTSUPP;
  186. if (nsk->sk_type != SOCK_STREAM && nsk->sk_type != SOCK_DGRAM)
  187. return -ENOTSUPP;
  188. /*
  189. * sk must be hashed (i.e. listening in the TCP case or binded
  190. * in the UDP case) and
  191. * it must also be a SO_REUSEPORT sk (i.e. reuse cannot be NULL).
  192. *
  193. * Also, sk will be used in bpf helper that is protected by
  194. * rcu_read_lock().
  195. */
  196. if (!sock_flag(nsk, SOCK_RCU_FREE) || !sk_hashed(nsk) || !nsk_reuse)
  197. return -EINVAL;
  198. /* READ_ONCE because the sk->sk_callback_lock may not be held here */
  199. if (READ_ONCE(nsk->sk_user_data))
  200. return -EBUSY;
  201. return 0;
  202. }
  203. /*
  204. * Called from syscall only.
  205. * The "nsk" in the fd refcnt.
  206. * The "osk" and "reuse" are protected by reuseport_lock.
  207. */
  208. int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
  209. void *value, u64 map_flags)
  210. {
  211. struct reuseport_array *array = reuseport_array(map);
  212. struct sock *free_osk = NULL, *osk, *nsk;
  213. struct sock_reuseport *reuse;
  214. u32 index = *(u32 *)key;
  215. uintptr_t sk_user_data;
  216. struct socket *socket;
  217. int err, fd;
  218. if (map_flags > BPF_EXIST)
  219. return -EINVAL;
  220. if (index >= map->max_entries)
  221. return -E2BIG;
  222. if (map->value_size == sizeof(u64)) {
  223. u64 fd64 = *(u64 *)value;
  224. if (fd64 > S32_MAX)
  225. return -EINVAL;
  226. fd = fd64;
  227. } else {
  228. fd = *(int *)value;
  229. }
  230. socket = sockfd_lookup(fd, &err);
  231. if (!socket)
  232. return err;
  233. nsk = socket->sk;
  234. if (!nsk) {
  235. err = -EINVAL;
  236. goto put_file;
  237. }
  238. /* Quick checks before taking reuseport_lock */
  239. err = reuseport_array_update_check(array, nsk,
  240. rcu_access_pointer(array->ptrs[index]),
  241. rcu_access_pointer(nsk->sk_reuseport_cb),
  242. map_flags);
  243. if (err)
  244. goto put_file;
  245. spin_lock_bh(&reuseport_lock);
  246. /*
  247. * Some of the checks only need reuseport_lock
  248. * but it is done under sk_callback_lock also
  249. * for simplicity reason.
  250. */
  251. write_lock_bh(&nsk->sk_callback_lock);
  252. osk = rcu_dereference_protected(array->ptrs[index],
  253. lockdep_is_held(&reuseport_lock));
  254. reuse = rcu_dereference_protected(nsk->sk_reuseport_cb,
  255. lockdep_is_held(&reuseport_lock));
  256. err = reuseport_array_update_check(array, nsk, osk, reuse, map_flags);
  257. if (err)
  258. goto put_file_unlock;
  259. sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY |
  260. SK_USER_DATA_BPF;
  261. WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data);
  262. rcu_assign_pointer(array->ptrs[index], nsk);
  263. free_osk = osk;
  264. err = 0;
  265. put_file_unlock:
  266. write_unlock_bh(&nsk->sk_callback_lock);
  267. if (free_osk) {
  268. write_lock_bh(&free_osk->sk_callback_lock);
  269. WRITE_ONCE(free_osk->sk_user_data, NULL);
  270. write_unlock_bh(&free_osk->sk_callback_lock);
  271. }
  272. spin_unlock_bh(&reuseport_lock);
  273. put_file:
  274. fput(socket->file);
  275. return err;
  276. }
  277. /* Called from syscall */
  278. static int reuseport_array_get_next_key(struct bpf_map *map, void *key,
  279. void *next_key)
  280. {
  281. struct reuseport_array *array = reuseport_array(map);
  282. u32 index = key ? *(u32 *)key : U32_MAX;
  283. u32 *next = (u32 *)next_key;
  284. if (index >= array->map.max_entries) {
  285. *next = 0;
  286. return 0;
  287. }
  288. if (index == array->map.max_entries - 1)
  289. return -ENOENT;
  290. *next = index + 1;
  291. return 0;
  292. }
  293. static int reuseport_array_map_btf_id;
  294. const struct bpf_map_ops reuseport_array_ops = {
  295. .map_meta_equal = bpf_map_meta_equal,
  296. .map_alloc_check = reuseport_array_alloc_check,
  297. .map_alloc = reuseport_array_alloc,
  298. .map_free = reuseport_array_free,
  299. .map_lookup_elem = reuseport_array_lookup_elem,
  300. .map_get_next_key = reuseport_array_get_next_key,
  301. .map_delete_elem = reuseport_array_delete_elem,
  302. .map_btf_name = "reuseport_array",
  303. .map_btf_id = &reuseport_array_map_btf_id,
  304. };