xref: /openbmc/linux/net/core/bpf_sk_storage.c (revision 0760aad038b5a032c31ea124feed63d88627d2f1)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook  */
3 #include <linux/rculist.h>
4 #include <linux/list.h>
5 #include <linux/hash.h>
6 #include <linux/types.h>
7 #include <linux/spinlock.h>
8 #include <linux/bpf.h>
9 #include <linux/btf_ids.h>
10 #include <linux/bpf_local_storage.h>
11 #include <net/bpf_sk_storage.h>
12 #include <net/sock.h>
13 #include <uapi/linux/sock_diag.h>
14 #include <uapi/linux/btf.h>
15 #include <linux/btf_ids.h>
16 
17 DEFINE_BPF_STORAGE_CACHE(sk_cache);
18 
19 static int omem_charge(struct sock *sk, unsigned int size)
20 {
21 	/* same check as in sock_kmalloc() */
22 	if (size <= sysctl_optmem_max &&
23 	    atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
24 		atomic_add(size, &sk->sk_omem_alloc);
25 		return 0;
26 	}
27 
28 	return -ENOMEM;
29 }
30 
31 static struct bpf_local_storage_data *
32 sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit)
33 {
34 	struct bpf_local_storage *sk_storage;
35 	struct bpf_local_storage_map *smap;
36 
37 	sk_storage = rcu_dereference(sk->sk_bpf_storage);
38 	if (!sk_storage)
39 		return NULL;
40 
41 	smap = (struct bpf_local_storage_map *)map;
42 	return bpf_local_storage_lookup(sk_storage, smap, cacheit_lockit);
43 }
44 
45 static int sk_storage_delete(struct sock *sk, struct bpf_map *map)
46 {
47 	struct bpf_local_storage_data *sdata;
48 
49 	sdata = sk_storage_lookup(sk, map, false);
50 	if (!sdata)
51 		return -ENOENT;
52 
53 	bpf_selem_unlink(SELEM(sdata));
54 
55 	return 0;
56 }
57 
58 /* Called by __sk_destruct() & bpf_sk_storage_clone() */
59 void bpf_sk_storage_free(struct sock *sk)
60 {
61 	struct bpf_local_storage_elem *selem;
62 	struct bpf_local_storage *sk_storage;
63 	bool free_sk_storage = false;
64 	struct hlist_node *n;
65 
66 	rcu_read_lock();
67 	sk_storage = rcu_dereference(sk->sk_bpf_storage);
68 	if (!sk_storage) {
69 		rcu_read_unlock();
70 		return;
71 	}
72 
73 	/* Netiher the bpf_prog nor the bpf-map's syscall
74 	 * could be modifying the sk_storage->list now.
75 	 * Thus, no elem can be added-to or deleted-from the
76 	 * sk_storage->list by the bpf_prog or by the bpf-map's syscall.
77 	 *
78 	 * It is racing with bpf_local_storage_map_free() alone
79 	 * when unlinking elem from the sk_storage->list and
80 	 * the map's bucket->list.
81 	 */
82 	raw_spin_lock_bh(&sk_storage->lock);
83 	hlist_for_each_entry_safe(selem, n, &sk_storage->list, snode) {
84 		/* Always unlink from map before unlinking from
85 		 * sk_storage.
86 		 */
87 		bpf_selem_unlink_map(selem);
88 		free_sk_storage = bpf_selem_unlink_storage_nolock(sk_storage,
89 								  selem, true);
90 	}
91 	raw_spin_unlock_bh(&sk_storage->lock);
92 	rcu_read_unlock();
93 
94 	if (free_sk_storage)
95 		kfree_rcu(sk_storage, rcu);
96 }
97 
98 static void sk_storage_map_free(struct bpf_map *map)
99 {
100 	struct bpf_local_storage_map *smap;
101 
102 	smap = (struct bpf_local_storage_map *)map;
103 	bpf_local_storage_cache_idx_free(&sk_cache, smap->cache_idx);
104 	bpf_local_storage_map_free(smap);
105 }
106 
107 static struct bpf_map *sk_storage_map_alloc(union bpf_attr *attr)
108 {
109 	struct bpf_local_storage_map *smap;
110 
111 	smap = bpf_local_storage_map_alloc(attr);
112 	if (IS_ERR(smap))
113 		return ERR_CAST(smap);
114 
115 	smap->cache_idx = bpf_local_storage_cache_idx_get(&sk_cache);
116 	return &smap->map;
117 }
118 
119 static int notsupp_get_next_key(struct bpf_map *map, void *key,
120 				void *next_key)
121 {
122 	return -ENOTSUPP;
123 }
124 
125 static void *bpf_fd_sk_storage_lookup_elem(struct bpf_map *map, void *key)
126 {
127 	struct bpf_local_storage_data *sdata;
128 	struct socket *sock;
129 	int fd, err;
130 
131 	fd = *(int *)key;
132 	sock = sockfd_lookup(fd, &err);
133 	if (sock) {
134 		sdata = sk_storage_lookup(sock->sk, map, true);
135 		sockfd_put(sock);
136 		return sdata ? sdata->data : NULL;
137 	}
138 
139 	return ERR_PTR(err);
140 }
141 
142 static int bpf_fd_sk_storage_update_elem(struct bpf_map *map, void *key,
143 					 void *value, u64 map_flags)
144 {
145 	struct bpf_local_storage_data *sdata;
146 	struct socket *sock;
147 	int fd, err;
148 
149 	fd = *(int *)key;
150 	sock = sockfd_lookup(fd, &err);
151 	if (sock) {
152 		sdata = bpf_local_storage_update(
153 			sock->sk, (struct bpf_local_storage_map *)map, value,
154 			map_flags);
155 		sockfd_put(sock);
156 		return PTR_ERR_OR_ZERO(sdata);
157 	}
158 
159 	return err;
160 }
161 
162 static int bpf_fd_sk_storage_delete_elem(struct bpf_map *map, void *key)
163 {
164 	struct socket *sock;
165 	int fd, err;
166 
167 	fd = *(int *)key;
168 	sock = sockfd_lookup(fd, &err);
169 	if (sock) {
170 		err = sk_storage_delete(sock->sk, map);
171 		sockfd_put(sock);
172 		return err;
173 	}
174 
175 	return err;
176 }
177 
178 static struct bpf_local_storage_elem *
179 bpf_sk_storage_clone_elem(struct sock *newsk,
180 			  struct bpf_local_storage_map *smap,
181 			  struct bpf_local_storage_elem *selem)
182 {
183 	struct bpf_local_storage_elem *copy_selem;
184 
185 	copy_selem = bpf_selem_alloc(smap, newsk, NULL, true);
186 	if (!copy_selem)
187 		return NULL;
188 
189 	if (map_value_has_spin_lock(&smap->map))
190 		copy_map_value_locked(&smap->map, SDATA(copy_selem)->data,
191 				      SDATA(selem)->data, true);
192 	else
193 		copy_map_value(&smap->map, SDATA(copy_selem)->data,
194 			       SDATA(selem)->data);
195 
196 	return copy_selem;
197 }
198 
199 int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
200 {
201 	struct bpf_local_storage *new_sk_storage = NULL;
202 	struct bpf_local_storage *sk_storage;
203 	struct bpf_local_storage_elem *selem;
204 	int ret = 0;
205 
206 	RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
207 
208 	rcu_read_lock();
209 	sk_storage = rcu_dereference(sk->sk_bpf_storage);
210 
211 	if (!sk_storage || hlist_empty(&sk_storage->list))
212 		goto out;
213 
214 	hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
215 		struct bpf_local_storage_elem *copy_selem;
216 		struct bpf_local_storage_map *smap;
217 		struct bpf_map *map;
218 
219 		smap = rcu_dereference(SDATA(selem)->smap);
220 		if (!(smap->map.map_flags & BPF_F_CLONE))
221 			continue;
222 
223 		/* Note that for lockless listeners adding new element
224 		 * here can race with cleanup in bpf_local_storage_map_free.
225 		 * Try to grab map refcnt to make sure that it's still
226 		 * alive and prevent concurrent removal.
227 		 */
228 		map = bpf_map_inc_not_zero(&smap->map);
229 		if (IS_ERR(map))
230 			continue;
231 
232 		copy_selem = bpf_sk_storage_clone_elem(newsk, smap, selem);
233 		if (!copy_selem) {
234 			ret = -ENOMEM;
235 			bpf_map_put(map);
236 			goto out;
237 		}
238 
239 		if (new_sk_storage) {
240 			bpf_selem_link_map(smap, copy_selem);
241 			bpf_selem_link_storage_nolock(new_sk_storage, copy_selem);
242 		} else {
243 			ret = bpf_local_storage_alloc(newsk, smap, copy_selem);
244 			if (ret) {
245 				kfree(copy_selem);
246 				atomic_sub(smap->elem_size,
247 					   &newsk->sk_omem_alloc);
248 				bpf_map_put(map);
249 				goto out;
250 			}
251 
252 			new_sk_storage =
253 				rcu_dereference(copy_selem->local_storage);
254 		}
255 		bpf_map_put(map);
256 	}
257 
258 out:
259 	rcu_read_unlock();
260 
261 	/* In case of an error, don't free anything explicitly here, the
262 	 * caller is responsible to call bpf_sk_storage_free.
263 	 */
264 
265 	return ret;
266 }
267 
268 BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
269 	   void *, value, u64, flags)
270 {
271 	struct bpf_local_storage_data *sdata;
272 
273 	if (flags > BPF_SK_STORAGE_GET_F_CREATE)
274 		return (unsigned long)NULL;
275 
276 	sdata = sk_storage_lookup(sk, map, true);
277 	if (sdata)
278 		return (unsigned long)sdata->data;
279 
280 	if (flags == BPF_SK_STORAGE_GET_F_CREATE &&
281 	    /* Cannot add new elem to a going away sk.
282 	     * Otherwise, the new elem may become a leak
283 	     * (and also other memory issues during map
284 	     *  destruction).
285 	     */
286 	    refcount_inc_not_zero(&sk->sk_refcnt)) {
287 		sdata = bpf_local_storage_update(
288 			sk, (struct bpf_local_storage_map *)map, value,
289 			BPF_NOEXIST);
290 		/* sk must be a fullsock (guaranteed by verifier),
291 		 * so sock_gen_put() is unnecessary.
292 		 */
293 		sock_put(sk);
294 		return IS_ERR(sdata) ?
295 			(unsigned long)NULL : (unsigned long)sdata->data;
296 	}
297 
298 	return (unsigned long)NULL;
299 }
300 
301 BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk)
302 {
303 	if (refcount_inc_not_zero(&sk->sk_refcnt)) {
304 		int err;
305 
306 		err = sk_storage_delete(sk, map);
307 		sock_put(sk);
308 		return err;
309 	}
310 
311 	return -ENOENT;
312 }
313 
314 static int sk_storage_charge(struct bpf_local_storage_map *smap,
315 			     void *owner, u32 size)
316 {
317 	return omem_charge(owner, size);
318 }
319 
320 static void sk_storage_uncharge(struct bpf_local_storage_map *smap,
321 				void *owner, u32 size)
322 {
323 	struct sock *sk = owner;
324 
325 	atomic_sub(size, &sk->sk_omem_alloc);
326 }
327 
328 static struct bpf_local_storage __rcu **
329 sk_storage_ptr(void *owner)
330 {
331 	struct sock *sk = owner;
332 
333 	return &sk->sk_bpf_storage;
334 }
335 
336 static int sk_storage_map_btf_id;
337 const struct bpf_map_ops sk_storage_map_ops = {
338 	.map_meta_equal = bpf_map_meta_equal,
339 	.map_alloc_check = bpf_local_storage_map_alloc_check,
340 	.map_alloc = sk_storage_map_alloc,
341 	.map_free = sk_storage_map_free,
342 	.map_get_next_key = notsupp_get_next_key,
343 	.map_lookup_elem = bpf_fd_sk_storage_lookup_elem,
344 	.map_update_elem = bpf_fd_sk_storage_update_elem,
345 	.map_delete_elem = bpf_fd_sk_storage_delete_elem,
346 	.map_check_btf = bpf_local_storage_map_check_btf,
347 	.map_btf_name = "bpf_local_storage_map",
348 	.map_btf_id = &sk_storage_map_btf_id,
349 	.map_local_storage_charge = sk_storage_charge,
350 	.map_local_storage_uncharge = sk_storage_uncharge,
351 	.map_owner_storage_ptr = sk_storage_ptr,
352 };
353 
354 const struct bpf_func_proto bpf_sk_storage_get_proto = {
355 	.func		= bpf_sk_storage_get,
356 	.gpl_only	= false,
357 	.ret_type	= RET_PTR_TO_MAP_VALUE_OR_NULL,
358 	.arg1_type	= ARG_CONST_MAP_PTR,
359 	.arg2_type	= ARG_PTR_TO_SOCKET,
360 	.arg3_type	= ARG_PTR_TO_MAP_VALUE_OR_NULL,
361 	.arg4_type	= ARG_ANYTHING,
362 };
363 
364 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto = {
365 	.func		= bpf_sk_storage_get,
366 	.gpl_only	= false,
367 	.ret_type	= RET_PTR_TO_MAP_VALUE_OR_NULL,
368 	.arg1_type	= ARG_CONST_MAP_PTR,
369 	.arg2_type	= ARG_PTR_TO_CTX, /* context is 'struct sock' */
370 	.arg3_type	= ARG_PTR_TO_MAP_VALUE_OR_NULL,
371 	.arg4_type	= ARG_ANYTHING,
372 };
373 
374 const struct bpf_func_proto bpf_sk_storage_delete_proto = {
375 	.func		= bpf_sk_storage_delete,
376 	.gpl_only	= false,
377 	.ret_type	= RET_INTEGER,
378 	.arg1_type	= ARG_CONST_MAP_PTR,
379 	.arg2_type	= ARG_PTR_TO_SOCKET,
380 };
381 
382 BTF_ID_LIST(sk_storage_btf_ids)
383 BTF_ID_UNUSED
384 BTF_ID(struct, sock)
385 
386 const struct bpf_func_proto sk_storage_get_btf_proto = {
387 	.func		= bpf_sk_storage_get,
388 	.gpl_only	= false,
389 	.ret_type	= RET_PTR_TO_MAP_VALUE_OR_NULL,
390 	.arg1_type	= ARG_CONST_MAP_PTR,
391 	.arg2_type	= ARG_PTR_TO_BTF_ID,
392 	.arg3_type	= ARG_PTR_TO_MAP_VALUE_OR_NULL,
393 	.arg4_type	= ARG_ANYTHING,
394 	.btf_id		= sk_storage_btf_ids,
395 };
396 
397 const struct bpf_func_proto sk_storage_delete_btf_proto = {
398 	.func		= bpf_sk_storage_delete,
399 	.gpl_only	= false,
400 	.ret_type	= RET_INTEGER,
401 	.arg1_type	= ARG_CONST_MAP_PTR,
402 	.arg2_type	= ARG_PTR_TO_BTF_ID,
403 	.btf_id		= sk_storage_btf_ids,
404 };
405 
406 struct bpf_sk_storage_diag {
407 	u32 nr_maps;
408 	struct bpf_map *maps[];
409 };
410 
411 /* The reply will be like:
412  * INET_DIAG_BPF_SK_STORAGES (nla_nest)
413  *	SK_DIAG_BPF_STORAGE (nla_nest)
414  *		SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32)
415  *		SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit)
416  *	SK_DIAG_BPF_STORAGE (nla_nest)
417  *		SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32)
418  *		SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit)
419  *	....
420  */
421 static int nla_value_size(u32 value_size)
422 {
423 	/* SK_DIAG_BPF_STORAGE (nla_nest)
424 	 *	SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32)
425 	 *	SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit)
426 	 */
427 	return nla_total_size(0) + nla_total_size(sizeof(u32)) +
428 		nla_total_size_64bit(value_size);
429 }
430 
431 void bpf_sk_storage_diag_free(struct bpf_sk_storage_diag *diag)
432 {
433 	u32 i;
434 
435 	if (!diag)
436 		return;
437 
438 	for (i = 0; i < diag->nr_maps; i++)
439 		bpf_map_put(diag->maps[i]);
440 
441 	kfree(diag);
442 }
443 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_free);
444 
445 static bool diag_check_dup(const struct bpf_sk_storage_diag *diag,
446 			   const struct bpf_map *map)
447 {
448 	u32 i;
449 
450 	for (i = 0; i < diag->nr_maps; i++) {
451 		if (diag->maps[i] == map)
452 			return true;
453 	}
454 
455 	return false;
456 }
457 
458 struct bpf_sk_storage_diag *
459 bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
460 {
461 	struct bpf_sk_storage_diag *diag;
462 	struct nlattr *nla;
463 	u32 nr_maps = 0;
464 	int rem, err;
465 
466 	/* bpf_local_storage_map is currently limited to CAP_SYS_ADMIN as
467 	 * the map_alloc_check() side also does.
468 	 */
469 	if (!bpf_capable())
470 		return ERR_PTR(-EPERM);
471 
472 	nla_for_each_nested(nla, nla_stgs, rem) {
473 		if (nla_type(nla) == SK_DIAG_BPF_STORAGE_REQ_MAP_FD)
474 			nr_maps++;
475 	}
476 
477 	diag = kzalloc(sizeof(*diag) + sizeof(diag->maps[0]) * nr_maps,
478 		       GFP_KERNEL);
479 	if (!diag)
480 		return ERR_PTR(-ENOMEM);
481 
482 	nla_for_each_nested(nla, nla_stgs, rem) {
483 		struct bpf_map *map;
484 		int map_fd;
485 
486 		if (nla_type(nla) != SK_DIAG_BPF_STORAGE_REQ_MAP_FD)
487 			continue;
488 
489 		map_fd = nla_get_u32(nla);
490 		map = bpf_map_get(map_fd);
491 		if (IS_ERR(map)) {
492 			err = PTR_ERR(map);
493 			goto err_free;
494 		}
495 		if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) {
496 			bpf_map_put(map);
497 			err = -EINVAL;
498 			goto err_free;
499 		}
500 		if (diag_check_dup(diag, map)) {
501 			bpf_map_put(map);
502 			err = -EEXIST;
503 			goto err_free;
504 		}
505 		diag->maps[diag->nr_maps++] = map;
506 	}
507 
508 	return diag;
509 
510 err_free:
511 	bpf_sk_storage_diag_free(diag);
512 	return ERR_PTR(err);
513 }
514 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_alloc);
515 
516 static int diag_get(struct bpf_local_storage_data *sdata, struct sk_buff *skb)
517 {
518 	struct nlattr *nla_stg, *nla_value;
519 	struct bpf_local_storage_map *smap;
520 
521 	/* It cannot exceed max nlattr's payload */
522 	BUILD_BUG_ON(U16_MAX - NLA_HDRLEN < BPF_LOCAL_STORAGE_MAX_VALUE_SIZE);
523 
524 	nla_stg = nla_nest_start(skb, SK_DIAG_BPF_STORAGE);
525 	if (!nla_stg)
526 		return -EMSGSIZE;
527 
528 	smap = rcu_dereference(sdata->smap);
529 	if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, smap->map.id))
530 		goto errout;
531 
532 	nla_value = nla_reserve_64bit(skb, SK_DIAG_BPF_STORAGE_MAP_VALUE,
533 				      smap->map.value_size,
534 				      SK_DIAG_BPF_STORAGE_PAD);
535 	if (!nla_value)
536 		goto errout;
537 
538 	if (map_value_has_spin_lock(&smap->map))
539 		copy_map_value_locked(&smap->map, nla_data(nla_value),
540 				      sdata->data, true);
541 	else
542 		copy_map_value(&smap->map, nla_data(nla_value), sdata->data);
543 
544 	nla_nest_end(skb, nla_stg);
545 	return 0;
546 
547 errout:
548 	nla_nest_cancel(skb, nla_stg);
549 	return -EMSGSIZE;
550 }
551 
552 static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb,
553 				       int stg_array_type,
554 				       unsigned int *res_diag_size)
555 {
556 	/* stg_array_type (e.g. INET_DIAG_BPF_SK_STORAGES) */
557 	unsigned int diag_size = nla_total_size(0);
558 	struct bpf_local_storage *sk_storage;
559 	struct bpf_local_storage_elem *selem;
560 	struct bpf_local_storage_map *smap;
561 	struct nlattr *nla_stgs;
562 	unsigned int saved_len;
563 	int err = 0;
564 
565 	rcu_read_lock();
566 
567 	sk_storage = rcu_dereference(sk->sk_bpf_storage);
568 	if (!sk_storage || hlist_empty(&sk_storage->list)) {
569 		rcu_read_unlock();
570 		return 0;
571 	}
572 
573 	nla_stgs = nla_nest_start(skb, stg_array_type);
574 	if (!nla_stgs)
575 		/* Continue to learn diag_size */
576 		err = -EMSGSIZE;
577 
578 	saved_len = skb->len;
579 	hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
580 		smap = rcu_dereference(SDATA(selem)->smap);
581 		diag_size += nla_value_size(smap->map.value_size);
582 
583 		if (nla_stgs && diag_get(SDATA(selem), skb))
584 			/* Continue to learn diag_size */
585 			err = -EMSGSIZE;
586 	}
587 
588 	rcu_read_unlock();
589 
590 	if (nla_stgs) {
591 		if (saved_len == skb->len)
592 			nla_nest_cancel(skb, nla_stgs);
593 		else
594 			nla_nest_end(skb, nla_stgs);
595 	}
596 
597 	if (diag_size == nla_total_size(0)) {
598 		*res_diag_size = 0;
599 		return 0;
600 	}
601 
602 	*res_diag_size = diag_size;
603 	return err;
604 }
605 
606 int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,
607 			    struct sock *sk, struct sk_buff *skb,
608 			    int stg_array_type,
609 			    unsigned int *res_diag_size)
610 {
611 	/* stg_array_type (e.g. INET_DIAG_BPF_SK_STORAGES) */
612 	unsigned int diag_size = nla_total_size(0);
613 	struct bpf_local_storage *sk_storage;
614 	struct bpf_local_storage_data *sdata;
615 	struct nlattr *nla_stgs;
616 	unsigned int saved_len;
617 	int err = 0;
618 	u32 i;
619 
620 	*res_diag_size = 0;
621 
622 	/* No map has been specified.  Dump all. */
623 	if (!diag->nr_maps)
624 		return bpf_sk_storage_diag_put_all(sk, skb, stg_array_type,
625 						   res_diag_size);
626 
627 	rcu_read_lock();
628 	sk_storage = rcu_dereference(sk->sk_bpf_storage);
629 	if (!sk_storage || hlist_empty(&sk_storage->list)) {
630 		rcu_read_unlock();
631 		return 0;
632 	}
633 
634 	nla_stgs = nla_nest_start(skb, stg_array_type);
635 	if (!nla_stgs)
636 		/* Continue to learn diag_size */
637 		err = -EMSGSIZE;
638 
639 	saved_len = skb->len;
640 	for (i = 0; i < diag->nr_maps; i++) {
641 		sdata = bpf_local_storage_lookup(sk_storage,
642 				(struct bpf_local_storage_map *)diag->maps[i],
643 				false);
644 
645 		if (!sdata)
646 			continue;
647 
648 		diag_size += nla_value_size(diag->maps[i]->value_size);
649 
650 		if (nla_stgs && diag_get(sdata, skb))
651 			/* Continue to learn diag_size */
652 			err = -EMSGSIZE;
653 	}
654 	rcu_read_unlock();
655 
656 	if (nla_stgs) {
657 		if (saved_len == skb->len)
658 			nla_nest_cancel(skb, nla_stgs);
659 		else
660 			nla_nest_end(skb, nla_stgs);
661 	}
662 
663 	if (diag_size == nla_total_size(0)) {
664 		*res_diag_size = 0;
665 		return 0;
666 	}
667 
668 	*res_diag_size = diag_size;
669 	return err;
670 }
671 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_put);
672 
673 struct bpf_iter_seq_sk_storage_map_info {
674 	struct bpf_map *map;
675 	unsigned int bucket_id;
676 	unsigned skip_elems;
677 };
678 
679 static struct bpf_local_storage_elem *
680 bpf_sk_storage_map_seq_find_next(struct bpf_iter_seq_sk_storage_map_info *info,
681 				 struct bpf_local_storage_elem *prev_selem)
682 {
683 	struct bpf_local_storage *sk_storage;
684 	struct bpf_local_storage_elem *selem;
685 	u32 skip_elems = info->skip_elems;
686 	struct bpf_local_storage_map *smap;
687 	u32 bucket_id = info->bucket_id;
688 	u32 i, count, n_buckets;
689 	struct bpf_local_storage_map_bucket *b;
690 
691 	smap = (struct bpf_local_storage_map *)info->map;
692 	n_buckets = 1U << smap->bucket_log;
693 	if (bucket_id >= n_buckets)
694 		return NULL;
695 
696 	/* try to find next selem in the same bucket */
697 	selem = prev_selem;
698 	count = 0;
699 	while (selem) {
700 		selem = hlist_entry_safe(selem->map_node.next,
701 					 struct bpf_local_storage_elem, map_node);
702 		if (!selem) {
703 			/* not found, unlock and go to the next bucket */
704 			b = &smap->buckets[bucket_id++];
705 			raw_spin_unlock_bh(&b->lock);
706 			skip_elems = 0;
707 			break;
708 		}
709 		sk_storage = rcu_dereference_raw(selem->local_storage);
710 		if (sk_storage) {
711 			info->skip_elems = skip_elems + count;
712 			return selem;
713 		}
714 		count++;
715 	}
716 
717 	for (i = bucket_id; i < (1U << smap->bucket_log); i++) {
718 		b = &smap->buckets[i];
719 		raw_spin_lock_bh(&b->lock);
720 		count = 0;
721 		hlist_for_each_entry(selem, &b->list, map_node) {
722 			sk_storage = rcu_dereference_raw(selem->local_storage);
723 			if (sk_storage && count >= skip_elems) {
724 				info->bucket_id = i;
725 				info->skip_elems = count;
726 				return selem;
727 			}
728 			count++;
729 		}
730 		raw_spin_unlock_bh(&b->lock);
731 		skip_elems = 0;
732 	}
733 
734 	info->bucket_id = i;
735 	info->skip_elems = 0;
736 	return NULL;
737 }
738 
739 static void *bpf_sk_storage_map_seq_start(struct seq_file *seq, loff_t *pos)
740 {
741 	struct bpf_local_storage_elem *selem;
742 
743 	selem = bpf_sk_storage_map_seq_find_next(seq->private, NULL);
744 	if (!selem)
745 		return NULL;
746 
747 	if (*pos == 0)
748 		++*pos;
749 	return selem;
750 }
751 
752 static void *bpf_sk_storage_map_seq_next(struct seq_file *seq, void *v,
753 					 loff_t *pos)
754 {
755 	struct bpf_iter_seq_sk_storage_map_info *info = seq->private;
756 
757 	++*pos;
758 	++info->skip_elems;
759 	return bpf_sk_storage_map_seq_find_next(seq->private, v);
760 }
761 
762 struct bpf_iter__bpf_sk_storage_map {
763 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
764 	__bpf_md_ptr(struct bpf_map *, map);
765 	__bpf_md_ptr(struct sock *, sk);
766 	__bpf_md_ptr(void *, value);
767 };
768 
769 DEFINE_BPF_ITER_FUNC(bpf_sk_storage_map, struct bpf_iter_meta *meta,
770 		     struct bpf_map *map, struct sock *sk,
771 		     void *value)
772 
773 static int __bpf_sk_storage_map_seq_show(struct seq_file *seq,
774 					 struct bpf_local_storage_elem *selem)
775 {
776 	struct bpf_iter_seq_sk_storage_map_info *info = seq->private;
777 	struct bpf_iter__bpf_sk_storage_map ctx = {};
778 	struct bpf_local_storage *sk_storage;
779 	struct bpf_iter_meta meta;
780 	struct bpf_prog *prog;
781 	int ret = 0;
782 
783 	meta.seq = seq;
784 	prog = bpf_iter_get_info(&meta, selem == NULL);
785 	if (prog) {
786 		ctx.meta = &meta;
787 		ctx.map = info->map;
788 		if (selem) {
789 			sk_storage = rcu_dereference_raw(selem->local_storage);
790 			ctx.sk = sk_storage->owner;
791 			ctx.value = SDATA(selem)->data;
792 		}
793 		ret = bpf_iter_run_prog(prog, &ctx);
794 	}
795 
796 	return ret;
797 }
798 
799 static int bpf_sk_storage_map_seq_show(struct seq_file *seq, void *v)
800 {
801 	return __bpf_sk_storage_map_seq_show(seq, v);
802 }
803 
804 static void bpf_sk_storage_map_seq_stop(struct seq_file *seq, void *v)
805 {
806 	struct bpf_iter_seq_sk_storage_map_info *info = seq->private;
807 	struct bpf_local_storage_map *smap;
808 	struct bpf_local_storage_map_bucket *b;
809 
810 	if (!v) {
811 		(void)__bpf_sk_storage_map_seq_show(seq, v);
812 	} else {
813 		smap = (struct bpf_local_storage_map *)info->map;
814 		b = &smap->buckets[info->bucket_id];
815 		raw_spin_unlock_bh(&b->lock);
816 	}
817 }
818 
819 static int bpf_iter_init_sk_storage_map(void *priv_data,
820 					struct bpf_iter_aux_info *aux)
821 {
822 	struct bpf_iter_seq_sk_storage_map_info *seq_info = priv_data;
823 
824 	seq_info->map = aux->map;
825 	return 0;
826 }
827 
828 static int bpf_iter_attach_map(struct bpf_prog *prog,
829 			       union bpf_iter_link_info *linfo,
830 			       struct bpf_iter_aux_info *aux)
831 {
832 	struct bpf_map *map;
833 	int err = -EINVAL;
834 
835 	if (!linfo->map.map_fd)
836 		return -EBADF;
837 
838 	map = bpf_map_get_with_uref(linfo->map.map_fd);
839 	if (IS_ERR(map))
840 		return PTR_ERR(map);
841 
842 	if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
843 		goto put_map;
844 
845 	if (prog->aux->max_rdonly_access > map->value_size) {
846 		err = -EACCES;
847 		goto put_map;
848 	}
849 
850 	aux->map = map;
851 	return 0;
852 
853 put_map:
854 	bpf_map_put_with_uref(map);
855 	return err;
856 }
857 
858 static void bpf_iter_detach_map(struct bpf_iter_aux_info *aux)
859 {
860 	bpf_map_put_with_uref(aux->map);
861 }
862 
863 static const struct seq_operations bpf_sk_storage_map_seq_ops = {
864 	.start  = bpf_sk_storage_map_seq_start,
865 	.next   = bpf_sk_storage_map_seq_next,
866 	.stop   = bpf_sk_storage_map_seq_stop,
867 	.show   = bpf_sk_storage_map_seq_show,
868 };
869 
870 static const struct bpf_iter_seq_info iter_seq_info = {
871 	.seq_ops		= &bpf_sk_storage_map_seq_ops,
872 	.init_seq_private	= bpf_iter_init_sk_storage_map,
873 	.fini_seq_private	= NULL,
874 	.seq_priv_size		= sizeof(struct bpf_iter_seq_sk_storage_map_info),
875 };
876 
877 static struct bpf_iter_reg bpf_sk_storage_map_reg_info = {
878 	.target			= "bpf_sk_storage_map",
879 	.attach_target		= bpf_iter_attach_map,
880 	.detach_target		= bpf_iter_detach_map,
881 	.show_fdinfo		= bpf_iter_map_show_fdinfo,
882 	.fill_link_info		= bpf_iter_map_fill_link_info,
883 	.ctx_arg_info_size	= 2,
884 	.ctx_arg_info		= {
885 		{ offsetof(struct bpf_iter__bpf_sk_storage_map, sk),
886 		  PTR_TO_BTF_ID_OR_NULL },
887 		{ offsetof(struct bpf_iter__bpf_sk_storage_map, value),
888 		  PTR_TO_RDWR_BUF_OR_NULL },
889 	},
890 	.seq_info		= &iter_seq_info,
891 };
892 
893 static int __init bpf_sk_storage_map_iter_init(void)
894 {
895 	bpf_sk_storage_map_reg_info.ctx_arg_info[0].btf_id =
896 		btf_sock_ids[BTF_SOCK_TYPE_SOCK];
897 	return bpf_iter_reg_target(&bpf_sk_storage_map_reg_info);
898 }
899 late_initcall(bpf_sk_storage_map_iter_init);
900