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