reuseport_array.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #include <linux/btf_ids.h>
  10. struct reuseport_array {
  11. struct bpf_map map;
  12. struct sock __rcu *ptrs[];
  13. };
  14. static struct reuseport_array *reuseport_array(struct bpf_map *map)
  15. {
  16. return (struct reuseport_array *)map;
  17. }
  18. /* The caller must hold the reuseport_lock */
  19. void bpf_sk_reuseport_detach(struct sock *sk)
  20. {
  21. struct sock __rcu **socks;
  22. write_lock_bh(&sk->sk_callback_lock);
  23. socks = __locked_read_sk_user_data_with_flags(sk, SK_USER_DATA_BPF);
  24. if (socks) {
  25. WRITE_ONCE(sk->sk_user_data, NULL);
  26. /*
  27. * Do not move this NULL assignment outside of
  28. * sk->sk_callback_lock because there is
  29. * a race with reuseport_array_free()
  30. * which does not hold the reuseport_lock.
  31. */
  32. RCU_INIT_POINTER(*socks, NULL);
  33. }
  34. write_unlock_bh(&sk->sk_callback_lock);
  35. }
  36. static int reuseport_array_alloc_check(union bpf_attr *attr)
  37. {
  38. if (attr->value_size != sizeof(u32) &&
  39. attr->value_size != sizeof(u64))
  40. return -EINVAL;
  41. return array_map_alloc_check(attr);
  42. }
  43. static void *reuseport_array_lookup_elem(struct bpf_map *map, void *key)
  44. {
  45. struct reuseport_array *array = reuseport_array(map);
  46. u32 index = *(u32 *)key;
  47. if (unlikely(index >= array->map.max_entries))
  48. return NULL;
  49. return rcu_dereference(array->ptrs[index]);
  50. }
  51. /* Called from syscall only */
  52. static int reuseport_array_delete_elem(struct bpf_map *map, void *key)
  53. {
  54. struct reuseport_array *array = reuseport_array(map);
  55. u32 index = *(u32 *)key;
  56. struct sock *sk;
  57. int err;
  58. if (index >= map->max_entries)
  59. return -E2BIG;
  60. if (!rcu_access_pointer(array->ptrs[index]))
  61. return -ENOENT;
  62. spin_lock_bh(&reuseport_lock);
  63. sk = rcu_dereference_protected(array->ptrs[index],
  64. lockdep_is_held(&reuseport_lock));
  65. if (sk) {
  66. write_lock_bh(&sk->sk_callback_lock);
  67. WRITE_ONCE(sk->sk_user_data, NULL);
  68. RCU_INIT_POINTER(array->ptrs[index], NULL);
  69. write_unlock_bh(&sk->sk_callback_lock);
  70. err = 0;
  71. } else {
  72. err = -ENOENT;
  73. }
  74. spin_unlock_bh(&reuseport_lock);
  75. return err;
  76. }
  77. static void reuseport_array_free(struct bpf_map *map)
  78. {
  79. struct reuseport_array *array = reuseport_array(map);
  80. struct sock *sk;
  81. u32 i;
  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 triggered 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. * referencing 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 numa_node = bpf_map_attr_numa_node(attr);
  131. struct reuseport_array *array;
  132. if (!bpf_capable())
  133. return ERR_PTR(-EPERM);
  134. /* allocate all map elements and zero-initialize them */
  135. array = bpf_map_area_alloc(struct_size(array, ptrs, attr->max_entries), numa_node);
  136. if (!array)
  137. return ERR_PTR(-ENOMEM);
  138. /* copy mandatory map attributes */
  139. bpf_map_init_from_attr(&array->map, attr);
  140. return &array->map;
  141. }
  142. int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
  143. void *value)
  144. {
  145. struct sock *sk;
  146. int err;
  147. if (map->value_size != sizeof(u64))
  148. return -ENOSPC;
  149. rcu_read_lock();
  150. sk = reuseport_array_lookup_elem(map, key);
  151. if (sk) {
  152. *(u64 *)value = __sock_gen_cookie(sk);
  153. err = 0;
  154. } else {
  155. err = -ENOENT;
  156. }
  157. rcu_read_unlock();
  158. return err;
  159. }
  160. static int
  161. reuseport_array_update_check(const struct reuseport_array *array,
  162. const struct sock *nsk,
  163. const struct sock *osk,
  164. const struct sock_reuseport *nsk_reuse,
  165. u32 map_flags)
  166. {
  167. if (osk && map_flags == BPF_NOEXIST)
  168. return -EEXIST;
  169. if (!osk && map_flags == BPF_EXIST)
  170. return -ENOENT;
  171. if (nsk->sk_protocol != IPPROTO_UDP && nsk->sk_protocol != IPPROTO_TCP)
  172. return -ENOTSUPP;
  173. if (nsk->sk_family != AF_INET && nsk->sk_family != AF_INET6)
  174. return -ENOTSUPP;
  175. if (nsk->sk_type != SOCK_STREAM && nsk->sk_type != SOCK_DGRAM)
  176. return -ENOTSUPP;
  177. /*
  178. * sk must be hashed (i.e. listening in the TCP case or binded
  179. * in the UDP case) and
  180. * it must also be a SO_REUSEPORT sk (i.e. reuse cannot be NULL).
  181. *
  182. * Also, sk will be used in bpf helper that is protected by
  183. * rcu_read_lock().
  184. */
  185. if (!sock_flag(nsk, SOCK_RCU_FREE) || !sk_hashed(nsk) || !nsk_reuse)
  186. return -EINVAL;
  187. /* READ_ONCE because the sk->sk_callback_lock may not be held here */
  188. if (READ_ONCE(nsk->sk_user_data))
  189. return -EBUSY;
  190. return 0;
  191. }
  192. /*
  193. * Called from syscall only.
  194. * The "nsk" in the fd refcnt.
  195. * The "osk" and "reuse" are protected by reuseport_lock.
  196. */
  197. int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
  198. void *value, u64 map_flags)
  199. {
  200. struct reuseport_array *array = reuseport_array(map);
  201. struct sock *free_osk = NULL, *osk, *nsk;
  202. struct sock_reuseport *reuse;
  203. u32 index = *(u32 *)key;
  204. uintptr_t sk_user_data;
  205. struct socket *socket;
  206. int err, fd;
  207. if (map_flags > BPF_EXIST)
  208. return -EINVAL;
  209. if (index >= map->max_entries)
  210. return -E2BIG;
  211. if (map->value_size == sizeof(u64)) {
  212. u64 fd64 = *(u64 *)value;
  213. if (fd64 > S32_MAX)
  214. return -EINVAL;
  215. fd = fd64;
  216. } else {
  217. fd = *(int *)value;
  218. }
  219. socket = sockfd_lookup(fd, &err);
  220. if (!socket)
  221. return err;
  222. nsk = socket->sk;
  223. if (!nsk) {
  224. err = -EINVAL;
  225. goto put_file;
  226. }
  227. /* Quick checks before taking reuseport_lock */
  228. err = reuseport_array_update_check(array, nsk,
  229. rcu_access_pointer(array->ptrs[index]),
  230. rcu_access_pointer(nsk->sk_reuseport_cb),
  231. map_flags);
  232. if (err)
  233. goto put_file;
  234. spin_lock_bh(&reuseport_lock);
  235. /*
  236. * Some of the checks only need reuseport_lock
  237. * but it is done under sk_callback_lock also
  238. * for simplicity reason.
  239. */
  240. write_lock_bh(&nsk->sk_callback_lock);
  241. osk = rcu_dereference_protected(array->ptrs[index],
  242. lockdep_is_held(&reuseport_lock));
  243. reuse = rcu_dereference_protected(nsk->sk_reuseport_cb,
  244. lockdep_is_held(&reuseport_lock));
  245. err = reuseport_array_update_check(array, nsk, osk, reuse, map_flags);
  246. if (err)
  247. goto put_file_unlock;
  248. sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY |
  249. SK_USER_DATA_BPF;
  250. WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data);
  251. rcu_assign_pointer(array->ptrs[index], nsk);
  252. free_osk = osk;
  253. err = 0;
  254. put_file_unlock:
  255. write_unlock_bh(&nsk->sk_callback_lock);
  256. if (free_osk) {
  257. write_lock_bh(&free_osk->sk_callback_lock);
  258. WRITE_ONCE(free_osk->sk_user_data, NULL);
  259. write_unlock_bh(&free_osk->sk_callback_lock);
  260. }
  261. spin_unlock_bh(&reuseport_lock);
  262. put_file:
  263. fput(socket->file);
  264. return err;
  265. }
  266. /* Called from syscall */
  267. static int reuseport_array_get_next_key(struct bpf_map *map, void *key,
  268. void *next_key)
  269. {
  270. struct reuseport_array *array = reuseport_array(map);
  271. u32 index = key ? *(u32 *)key : U32_MAX;
  272. u32 *next = (u32 *)next_key;
  273. if (index >= array->map.max_entries) {
  274. *next = 0;
  275. return 0;
  276. }
  277. if (index == array->map.max_entries - 1)
  278. return -ENOENT;
  279. *next = index + 1;
  280. return 0;
  281. }
  282. BTF_ID_LIST_SINGLE(reuseport_array_map_btf_ids, struct, reuseport_array)
  283. const struct bpf_map_ops reuseport_array_ops = {
  284. .map_meta_equal = bpf_map_meta_equal,
  285. .map_alloc_check = reuseport_array_alloc_check,
  286. .map_alloc = reuseport_array_alloc,
  287. .map_free = reuseport_array_free,
  288. .map_lookup_elem = reuseport_array_lookup_elem,
  289. .map_get_next_key = reuseport_array_get_next_key,
  290. .map_delete_elem = reuseport_array_delete_elem,
  291. .map_btf_id = &reuseport_array_map_btf_ids[0],
  292. };