reuseport_array.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. struct sock __rcu **socks;
  21. write_lock_bh(&sk->sk_callback_lock);
  22. socks = sk->sk_user_data;
  23. if (socks) {
  24. WRITE_ONCE(sk->sk_user_data, NULL);
  25. /*
  26. * Do not move this NULL assignment outside of
  27. * sk->sk_callback_lock because there is
  28. * a race with reuseport_array_free()
  29. * which does not hold the reuseport_lock.
  30. */
  31. RCU_INIT_POINTER(*socks, NULL);
  32. }
  33. write_unlock_bh(&sk->sk_callback_lock);
  34. }
  35. static int reuseport_array_alloc_check(union bpf_attr *attr)
  36. {
  37. if (attr->value_size != sizeof(u32) &&
  38. attr->value_size != sizeof(u64))
  39. return -EINVAL;
  40. return array_map_alloc_check(attr);
  41. }
  42. static void *reuseport_array_lookup_elem(struct bpf_map *map, void *key)
  43. {
  44. struct reuseport_array *array = reuseport_array(map);
  45. u32 index = *(u32 *)key;
  46. if (unlikely(index >= array->map.max_entries))
  47. return NULL;
  48. return rcu_dereference(array->ptrs[index]);
  49. }
  50. /* Called from syscall only */
  51. static int reuseport_array_delete_elem(struct bpf_map *map, void *key)
  52. {
  53. struct reuseport_array *array = reuseport_array(map);
  54. u32 index = *(u32 *)key;
  55. struct sock *sk;
  56. int err;
  57. if (index >= map->max_entries)
  58. return -E2BIG;
  59. if (!rcu_access_pointer(array->ptrs[index]))
  60. return -ENOENT;
  61. spin_lock_bh(&reuseport_lock);
  62. sk = rcu_dereference_protected(array->ptrs[index],
  63. lockdep_is_held(&reuseport_lock));
  64. if (sk) {
  65. write_lock_bh(&sk->sk_callback_lock);
  66. WRITE_ONCE(sk->sk_user_data, NULL);
  67. RCU_INIT_POINTER(array->ptrs[index], NULL);
  68. write_unlock_bh(&sk->sk_callback_lock);
  69. err = 0;
  70. } else {
  71. err = -ENOENT;
  72. }
  73. spin_unlock_bh(&reuseport_lock);
  74. return err;
  75. }
  76. static void reuseport_array_free(struct bpf_map *map)
  77. {
  78. struct reuseport_array *array = reuseport_array(map);
  79. struct sock *sk;
  80. u32 i;
  81. synchronize_rcu();
  82. /*
  83. * ops->map_*_elem() will not be able to access this
  84. * array now. Hence, this function only races with
  85. * bpf_sk_reuseport_detach() which was triggerred by
  86. * close() or disconnect().
  87. *
  88. * This function and bpf_sk_reuseport_detach() are
  89. * both removing sk from "array". Who removes it
  90. * first does not matter.
  91. *
  92. * The only concern here is bpf_sk_reuseport_detach()
  93. * may access "array" which is being freed here.
  94. * bpf_sk_reuseport_detach() access this "array"
  95. * through sk->sk_user_data _and_ with sk->sk_callback_lock
  96. * held which is enough because this "array" is not freed
  97. * until all sk->sk_user_data has stopped referencing this "array".
  98. *
  99. * Hence, due to the above, taking "reuseport_lock" is not
  100. * needed here.
  101. */
  102. /*
  103. * Since reuseport_lock is not taken, sk is accessed under
  104. * rcu_read_lock()
  105. */
  106. rcu_read_lock();
  107. for (i = 0; i < map->max_entries; i++) {
  108. sk = rcu_dereference(array->ptrs[i]);
  109. if (sk) {
  110. write_lock_bh(&sk->sk_callback_lock);
  111. /*
  112. * No need for WRITE_ONCE(). At this point,
  113. * no one is reading it without taking the
  114. * sk->sk_callback_lock.
  115. */
  116. sk->sk_user_data = NULL;
  117. write_unlock_bh(&sk->sk_callback_lock);
  118. RCU_INIT_POINTER(array->ptrs[i], NULL);
  119. }
  120. }
  121. rcu_read_unlock();
  122. /*
  123. * Once reaching here, all sk->sk_user_data is not
  124. * referenceing this "array". "array" can be freed now.
  125. */
  126. bpf_map_area_free(array);
  127. }
  128. static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
  129. {
  130. int err, numa_node = bpf_map_attr_numa_node(attr);
  131. struct reuseport_array *array;
  132. u64 cost, array_size;
  133. if (!capable(CAP_SYS_ADMIN))
  134. return ERR_PTR(-EPERM);
  135. array_size = sizeof(*array);
  136. array_size += (u64)attr->max_entries * sizeof(struct sock *);
  137. /* make sure there is no u32 overflow later in round_up() */
  138. cost = array_size;
  139. if (cost >= U32_MAX - PAGE_SIZE)
  140. return ERR_PTR(-ENOMEM);
  141. cost = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  142. err = bpf_map_precharge_memlock(cost);
  143. if (err)
  144. return ERR_PTR(err);
  145. /* allocate all map elements and zero-initialize them */
  146. array = bpf_map_area_alloc(array_size, numa_node);
  147. if (!array)
  148. return ERR_PTR(-ENOMEM);
  149. /* copy mandatory map attributes */
  150. bpf_map_init_from_attr(&array->map, attr);
  151. array->map.pages = cost;
  152. return &array->map;
  153. }
  154. int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
  155. void *value)
  156. {
  157. struct sock *sk;
  158. int err;
  159. if (map->value_size != sizeof(u64))
  160. return -ENOSPC;
  161. rcu_read_lock();
  162. sk = reuseport_array_lookup_elem(map, key);
  163. if (sk) {
  164. *(u64 *)value = sock_gen_cookie(sk);
  165. err = 0;
  166. } else {
  167. err = -ENOENT;
  168. }
  169. rcu_read_unlock();
  170. return err;
  171. }
  172. static int
  173. reuseport_array_update_check(const struct reuseport_array *array,
  174. const struct sock *nsk,
  175. const struct sock *osk,
  176. const struct sock_reuseport *nsk_reuse,
  177. u32 map_flags)
  178. {
  179. if (osk && map_flags == BPF_NOEXIST)
  180. return -EEXIST;
  181. if (!osk && map_flags == BPF_EXIST)
  182. return -ENOENT;
  183. if (nsk->sk_protocol != IPPROTO_UDP && nsk->sk_protocol != IPPROTO_TCP)
  184. return -ENOTSUPP;
  185. if (nsk->sk_family != AF_INET && nsk->sk_family != AF_INET6)
  186. return -ENOTSUPP;
  187. if (nsk->sk_type != SOCK_STREAM && nsk->sk_type != SOCK_DGRAM)
  188. return -ENOTSUPP;
  189. /*
  190. * sk must be hashed (i.e. listening in the TCP case or binded
  191. * in the UDP case) and
  192. * it must also be a SO_REUSEPORT sk (i.e. reuse cannot be NULL).
  193. *
  194. * Also, sk will be used in bpf helper that is protected by
  195. * rcu_read_lock().
  196. */
  197. if (!sock_flag(nsk, SOCK_RCU_FREE) || !sk_hashed(nsk) || !nsk_reuse)
  198. return -EINVAL;
  199. /* READ_ONCE because the sk->sk_callback_lock may not be held here */
  200. if (READ_ONCE(nsk->sk_user_data))
  201. return -EBUSY;
  202. return 0;
  203. }
  204. /*
  205. * Called from syscall only.
  206. * The "nsk" in the fd refcnt.
  207. * The "osk" and "reuse" are protected by reuseport_lock.
  208. */
  209. int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
  210. void *value, u64 map_flags)
  211. {
  212. struct reuseport_array *array = reuseport_array(map);
  213. struct sock *free_osk = NULL, *osk, *nsk;
  214. struct sock_reuseport *reuse;
  215. u32 index = *(u32 *)key;
  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. /* Ensure reuse->reuseport_id is set */
  260. err = reuseport_get_id(reuse);
  261. if (err < 0)
  262. goto put_file_unlock;
  263. WRITE_ONCE(nsk->sk_user_data, &array->ptrs[index]);
  264. rcu_assign_pointer(array->ptrs[index], nsk);
  265. free_osk = osk;
  266. err = 0;
  267. put_file_unlock:
  268. write_unlock_bh(&nsk->sk_callback_lock);
  269. if (free_osk) {
  270. write_lock_bh(&free_osk->sk_callback_lock);
  271. WRITE_ONCE(free_osk->sk_user_data, NULL);
  272. write_unlock_bh(&free_osk->sk_callback_lock);
  273. }
  274. spin_unlock_bh(&reuseport_lock);
  275. put_file:
  276. fput(socket->file);
  277. return err;
  278. }
  279. /* Called from syscall */
  280. static int reuseport_array_get_next_key(struct bpf_map *map, void *key,
  281. void *next_key)
  282. {
  283. struct reuseport_array *array = reuseport_array(map);
  284. u32 index = key ? *(u32 *)key : U32_MAX;
  285. u32 *next = (u32 *)next_key;
  286. if (index >= array->map.max_entries) {
  287. *next = 0;
  288. return 0;
  289. }
  290. if (index == array->map.max_entries - 1)
  291. return -ENOENT;
  292. *next = index + 1;
  293. return 0;
  294. }
  295. const struct bpf_map_ops reuseport_array_ops = {
  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. };