xref: /linux/net/core/sock_map.c (revision e3b9626f09d429788d929c9b9000a069fcfc056e)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3 
4 #include <linux/bpf.h>
5 #include <linux/btf_ids.h>
6 #include <linux/filter.h>
7 #include <linux/errno.h>
8 #include <linux/file.h>
9 #include <linux/net.h>
10 #include <linux/workqueue.h>
11 #include <linux/skmsg.h>
12 #include <linux/list.h>
13 #include <linux/jhash.h>
14 #include <linux/sock_diag.h>
15 #include <net/udp.h>
16 
17 struct bpf_stab {
18 	struct bpf_map map;
19 	struct sock **sks;
20 	struct sk_psock_progs progs;
21 	raw_spinlock_t lock;
22 };
23 
24 #define SOCK_CREATE_FLAG_MASK				\
25 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
26 
27 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
28 {
29 	struct bpf_stab *stab;
30 	u64 cost;
31 	int err;
32 
33 	if (!capable(CAP_NET_ADMIN))
34 		return ERR_PTR(-EPERM);
35 	if (attr->max_entries == 0 ||
36 	    attr->key_size    != 4 ||
37 	    (attr->value_size != sizeof(u32) &&
38 	     attr->value_size != sizeof(u64)) ||
39 	    attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
40 		return ERR_PTR(-EINVAL);
41 
42 	stab = kzalloc(sizeof(*stab), GFP_USER);
43 	if (!stab)
44 		return ERR_PTR(-ENOMEM);
45 
46 	bpf_map_init_from_attr(&stab->map, attr);
47 	raw_spin_lock_init(&stab->lock);
48 
49 	/* Make sure page count doesn't overflow. */
50 	cost = (u64) stab->map.max_entries * sizeof(struct sock *);
51 	err = bpf_map_charge_init(&stab->map.memory, cost);
52 	if (err)
53 		goto free_stab;
54 
55 	stab->sks = bpf_map_area_alloc(stab->map.max_entries *
56 				       sizeof(struct sock *),
57 				       stab->map.numa_node);
58 	if (stab->sks)
59 		return &stab->map;
60 	err = -ENOMEM;
61 	bpf_map_charge_finish(&stab->map.memory);
62 free_stab:
63 	kfree(stab);
64 	return ERR_PTR(err);
65 }
66 
67 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
68 {
69 	u32 ufd = attr->target_fd;
70 	struct bpf_map *map;
71 	struct fd f;
72 	int ret;
73 
74 	if (attr->attach_flags || attr->replace_bpf_fd)
75 		return -EINVAL;
76 
77 	f = fdget(ufd);
78 	map = __bpf_map_get(f);
79 	if (IS_ERR(map))
80 		return PTR_ERR(map);
81 	ret = sock_map_prog_update(map, prog, NULL, attr->attach_type);
82 	fdput(f);
83 	return ret;
84 }
85 
86 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
87 {
88 	u32 ufd = attr->target_fd;
89 	struct bpf_prog *prog;
90 	struct bpf_map *map;
91 	struct fd f;
92 	int ret;
93 
94 	if (attr->attach_flags || attr->replace_bpf_fd)
95 		return -EINVAL;
96 
97 	f = fdget(ufd);
98 	map = __bpf_map_get(f);
99 	if (IS_ERR(map))
100 		return PTR_ERR(map);
101 
102 	prog = bpf_prog_get(attr->attach_bpf_fd);
103 	if (IS_ERR(prog)) {
104 		ret = PTR_ERR(prog);
105 		goto put_map;
106 	}
107 
108 	if (prog->type != ptype) {
109 		ret = -EINVAL;
110 		goto put_prog;
111 	}
112 
113 	ret = sock_map_prog_update(map, NULL, prog, attr->attach_type);
114 put_prog:
115 	bpf_prog_put(prog);
116 put_map:
117 	fdput(f);
118 	return ret;
119 }
120 
121 static void sock_map_sk_acquire(struct sock *sk)
122 	__acquires(&sk->sk_lock.slock)
123 {
124 	lock_sock(sk);
125 	preempt_disable();
126 	rcu_read_lock();
127 }
128 
129 static void sock_map_sk_release(struct sock *sk)
130 	__releases(&sk->sk_lock.slock)
131 {
132 	rcu_read_unlock();
133 	preempt_enable();
134 	release_sock(sk);
135 }
136 
137 static void sock_map_add_link(struct sk_psock *psock,
138 			      struct sk_psock_link *link,
139 			      struct bpf_map *map, void *link_raw)
140 {
141 	link->link_raw = link_raw;
142 	link->map = map;
143 	spin_lock_bh(&psock->link_lock);
144 	list_add_tail(&link->list, &psock->link);
145 	spin_unlock_bh(&psock->link_lock);
146 }
147 
148 static void sock_map_del_link(struct sock *sk,
149 			      struct sk_psock *psock, void *link_raw)
150 {
151 	struct sk_psock_link *link, *tmp;
152 	bool strp_stop = false;
153 
154 	spin_lock_bh(&psock->link_lock);
155 	list_for_each_entry_safe(link, tmp, &psock->link, list) {
156 		if (link->link_raw == link_raw) {
157 			struct bpf_map *map = link->map;
158 			struct bpf_stab *stab = container_of(map, struct bpf_stab,
159 							     map);
160 			if (psock->parser.enabled && stab->progs.skb_parser)
161 				strp_stop = true;
162 			list_del(&link->list);
163 			sk_psock_free_link(link);
164 		}
165 	}
166 	spin_unlock_bh(&psock->link_lock);
167 	if (strp_stop) {
168 		write_lock_bh(&sk->sk_callback_lock);
169 		sk_psock_stop_strp(sk, psock);
170 		write_unlock_bh(&sk->sk_callback_lock);
171 	}
172 }
173 
174 static void sock_map_unref(struct sock *sk, void *link_raw)
175 {
176 	struct sk_psock *psock = sk_psock(sk);
177 
178 	if (likely(psock)) {
179 		sock_map_del_link(sk, psock, link_raw);
180 		sk_psock_put(sk, psock);
181 	}
182 }
183 
184 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
185 {
186 	struct proto *prot;
187 
188 	switch (sk->sk_type) {
189 	case SOCK_STREAM:
190 		prot = tcp_bpf_get_proto(sk, psock);
191 		break;
192 
193 	case SOCK_DGRAM:
194 		prot = udp_bpf_get_proto(sk, psock);
195 		break;
196 
197 	default:
198 		return -EINVAL;
199 	}
200 
201 	if (IS_ERR(prot))
202 		return PTR_ERR(prot);
203 
204 	sk_psock_update_proto(sk, psock, prot);
205 	return 0;
206 }
207 
208 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
209 {
210 	struct sk_psock *psock;
211 
212 	rcu_read_lock();
213 	psock = sk_psock(sk);
214 	if (psock) {
215 		if (sk->sk_prot->close != sock_map_close) {
216 			psock = ERR_PTR(-EBUSY);
217 			goto out;
218 		}
219 
220 		if (!refcount_inc_not_zero(&psock->refcnt))
221 			psock = ERR_PTR(-EBUSY);
222 	}
223 out:
224 	rcu_read_unlock();
225 	return psock;
226 }
227 
228 static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
229 			 struct sock *sk)
230 {
231 	struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
232 	struct sk_psock *psock;
233 	bool skb_progs;
234 	int ret;
235 
236 	skb_verdict = READ_ONCE(progs->skb_verdict);
237 	skb_parser = READ_ONCE(progs->skb_parser);
238 	skb_progs = skb_parser && skb_verdict;
239 	if (skb_progs) {
240 		skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
241 		if (IS_ERR(skb_verdict))
242 			return PTR_ERR(skb_verdict);
243 		skb_parser = bpf_prog_inc_not_zero(skb_parser);
244 		if (IS_ERR(skb_parser)) {
245 			bpf_prog_put(skb_verdict);
246 			return PTR_ERR(skb_parser);
247 		}
248 	}
249 
250 	msg_parser = READ_ONCE(progs->msg_parser);
251 	if (msg_parser) {
252 		msg_parser = bpf_prog_inc_not_zero(msg_parser);
253 		if (IS_ERR(msg_parser)) {
254 			ret = PTR_ERR(msg_parser);
255 			goto out;
256 		}
257 	}
258 
259 	psock = sock_map_psock_get_checked(sk);
260 	if (IS_ERR(psock)) {
261 		ret = PTR_ERR(psock);
262 		goto out_progs;
263 	}
264 
265 	if (psock) {
266 		if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
267 		    (skb_progs  && READ_ONCE(psock->progs.skb_parser))) {
268 			sk_psock_put(sk, psock);
269 			ret = -EBUSY;
270 			goto out_progs;
271 		}
272 	} else {
273 		psock = sk_psock_init(sk, map->numa_node);
274 		if (IS_ERR(psock)) {
275 			ret = PTR_ERR(psock);
276 			goto out_progs;
277 		}
278 	}
279 
280 	if (msg_parser)
281 		psock_set_prog(&psock->progs.msg_parser, msg_parser);
282 
283 	ret = sock_map_init_proto(sk, psock);
284 	if (ret < 0)
285 		goto out_drop;
286 
287 	write_lock_bh(&sk->sk_callback_lock);
288 	if (skb_progs && !psock->parser.enabled) {
289 		ret = sk_psock_init_strp(sk, psock);
290 		if (ret) {
291 			write_unlock_bh(&sk->sk_callback_lock);
292 			goto out_drop;
293 		}
294 		psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
295 		psock_set_prog(&psock->progs.skb_parser, skb_parser);
296 		sk_psock_start_strp(sk, psock);
297 	}
298 	write_unlock_bh(&sk->sk_callback_lock);
299 	return 0;
300 out_drop:
301 	sk_psock_put(sk, psock);
302 out_progs:
303 	if (msg_parser)
304 		bpf_prog_put(msg_parser);
305 out:
306 	if (skb_progs) {
307 		bpf_prog_put(skb_verdict);
308 		bpf_prog_put(skb_parser);
309 	}
310 	return ret;
311 }
312 
313 static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)
314 {
315 	struct sk_psock *psock;
316 	int ret;
317 
318 	psock = sock_map_psock_get_checked(sk);
319 	if (IS_ERR(psock))
320 		return PTR_ERR(psock);
321 
322 	if (!psock) {
323 		psock = sk_psock_init(sk, map->numa_node);
324 		if (IS_ERR(psock))
325 			return PTR_ERR(psock);
326 	}
327 
328 	ret = sock_map_init_proto(sk, psock);
329 	if (ret < 0)
330 		sk_psock_put(sk, psock);
331 	return ret;
332 }
333 
334 static void sock_map_free(struct bpf_map *map)
335 {
336 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
337 	int i;
338 
339 	/* After the sync no updates or deletes will be in-flight so it
340 	 * is safe to walk map and remove entries without risking a race
341 	 * in EEXIST update case.
342 	 */
343 	synchronize_rcu();
344 	for (i = 0; i < stab->map.max_entries; i++) {
345 		struct sock **psk = &stab->sks[i];
346 		struct sock *sk;
347 
348 		sk = xchg(psk, NULL);
349 		if (sk) {
350 			lock_sock(sk);
351 			rcu_read_lock();
352 			sock_map_unref(sk, psk);
353 			rcu_read_unlock();
354 			release_sock(sk);
355 		}
356 	}
357 
358 	/* wait for psock readers accessing its map link */
359 	synchronize_rcu();
360 
361 	bpf_map_area_free(stab->sks);
362 	kfree(stab);
363 }
364 
365 static void sock_map_release_progs(struct bpf_map *map)
366 {
367 	psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
368 }
369 
370 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
371 {
372 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
373 
374 	WARN_ON_ONCE(!rcu_read_lock_held());
375 
376 	if (unlikely(key >= map->max_entries))
377 		return NULL;
378 	return READ_ONCE(stab->sks[key]);
379 }
380 
381 static void *sock_map_lookup(struct bpf_map *map, void *key)
382 {
383 	struct sock *sk;
384 
385 	sk = __sock_map_lookup_elem(map, *(u32 *)key);
386 	if (!sk)
387 		return NULL;
388 	if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
389 		return NULL;
390 	return sk;
391 }
392 
393 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
394 {
395 	struct sock *sk;
396 
397 	if (map->value_size != sizeof(u64))
398 		return ERR_PTR(-ENOSPC);
399 
400 	sk = __sock_map_lookup_elem(map, *(u32 *)key);
401 	if (!sk)
402 		return ERR_PTR(-ENOENT);
403 
404 	sock_gen_cookie(sk);
405 	return &sk->sk_cookie;
406 }
407 
408 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
409 			     struct sock **psk)
410 {
411 	struct sock *sk;
412 	int err = 0;
413 
414 	raw_spin_lock_bh(&stab->lock);
415 	sk = *psk;
416 	if (!sk_test || sk_test == sk)
417 		sk = xchg(psk, NULL);
418 
419 	if (likely(sk))
420 		sock_map_unref(sk, psk);
421 	else
422 		err = -EINVAL;
423 
424 	raw_spin_unlock_bh(&stab->lock);
425 	return err;
426 }
427 
428 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
429 				      void *link_raw)
430 {
431 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
432 
433 	__sock_map_delete(stab, sk, link_raw);
434 }
435 
436 static int sock_map_delete_elem(struct bpf_map *map, void *key)
437 {
438 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
439 	u32 i = *(u32 *)key;
440 	struct sock **psk;
441 
442 	if (unlikely(i >= map->max_entries))
443 		return -EINVAL;
444 
445 	psk = &stab->sks[i];
446 	return __sock_map_delete(stab, NULL, psk);
447 }
448 
449 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
450 {
451 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
452 	u32 i = key ? *(u32 *)key : U32_MAX;
453 	u32 *key_next = next;
454 
455 	if (i == stab->map.max_entries - 1)
456 		return -ENOENT;
457 	if (i >= stab->map.max_entries)
458 		*key_next = 0;
459 	else
460 		*key_next = i + 1;
461 	return 0;
462 }
463 
464 static bool sock_map_redirect_allowed(const struct sock *sk);
465 
466 static int sock_map_update_common(struct bpf_map *map, u32 idx,
467 				  struct sock *sk, u64 flags)
468 {
469 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
470 	struct sk_psock_link *link;
471 	struct sk_psock *psock;
472 	struct sock *osk;
473 	int ret;
474 
475 	WARN_ON_ONCE(!rcu_read_lock_held());
476 	if (unlikely(flags > BPF_EXIST))
477 		return -EINVAL;
478 	if (unlikely(idx >= map->max_entries))
479 		return -E2BIG;
480 
481 	link = sk_psock_init_link();
482 	if (!link)
483 		return -ENOMEM;
484 
485 	/* Only sockets we can redirect into/from in BPF need to hold
486 	 * refs to parser/verdict progs and have their sk_data_ready
487 	 * and sk_write_space callbacks overridden.
488 	 */
489 	if (sock_map_redirect_allowed(sk))
490 		ret = sock_map_link(map, &stab->progs, sk);
491 	else
492 		ret = sock_map_link_no_progs(map, sk);
493 	if (ret < 0)
494 		goto out_free;
495 
496 	psock = sk_psock(sk);
497 	WARN_ON_ONCE(!psock);
498 
499 	raw_spin_lock_bh(&stab->lock);
500 	osk = stab->sks[idx];
501 	if (osk && flags == BPF_NOEXIST) {
502 		ret = -EEXIST;
503 		goto out_unlock;
504 	} else if (!osk && flags == BPF_EXIST) {
505 		ret = -ENOENT;
506 		goto out_unlock;
507 	}
508 
509 	sock_map_add_link(psock, link, map, &stab->sks[idx]);
510 	stab->sks[idx] = sk;
511 	if (osk)
512 		sock_map_unref(osk, &stab->sks[idx]);
513 	raw_spin_unlock_bh(&stab->lock);
514 	return 0;
515 out_unlock:
516 	raw_spin_unlock_bh(&stab->lock);
517 	if (psock)
518 		sk_psock_put(sk, psock);
519 out_free:
520 	sk_psock_free_link(link);
521 	return ret;
522 }
523 
524 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
525 {
526 	return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
527 	       ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
528 	       ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
529 }
530 
531 static bool sk_is_tcp(const struct sock *sk)
532 {
533 	return sk->sk_type == SOCK_STREAM &&
534 	       sk->sk_protocol == IPPROTO_TCP;
535 }
536 
537 static bool sk_is_udp(const struct sock *sk)
538 {
539 	return sk->sk_type == SOCK_DGRAM &&
540 	       sk->sk_protocol == IPPROTO_UDP;
541 }
542 
543 static bool sock_map_redirect_allowed(const struct sock *sk)
544 {
545 	return sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN;
546 }
547 
548 static bool sock_map_sk_is_suitable(const struct sock *sk)
549 {
550 	return sk_is_tcp(sk) || sk_is_udp(sk);
551 }
552 
553 static bool sock_map_sk_state_allowed(const struct sock *sk)
554 {
555 	if (sk_is_tcp(sk))
556 		return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
557 	else if (sk_is_udp(sk))
558 		return sk_hashed(sk);
559 
560 	return false;
561 }
562 
563 static int sock_hash_update_common(struct bpf_map *map, void *key,
564 				   struct sock *sk, u64 flags);
565 
566 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
567 			     u64 flags)
568 {
569 	struct socket *sock;
570 	struct sock *sk;
571 	int ret;
572 	u64 ufd;
573 
574 	if (map->value_size == sizeof(u64))
575 		ufd = *(u64 *)value;
576 	else
577 		ufd = *(u32 *)value;
578 	if (ufd > S32_MAX)
579 		return -EINVAL;
580 
581 	sock = sockfd_lookup(ufd, &ret);
582 	if (!sock)
583 		return ret;
584 	sk = sock->sk;
585 	if (!sk) {
586 		ret = -EINVAL;
587 		goto out;
588 	}
589 	if (!sock_map_sk_is_suitable(sk)) {
590 		ret = -EOPNOTSUPP;
591 		goto out;
592 	}
593 
594 	sock_map_sk_acquire(sk);
595 	if (!sock_map_sk_state_allowed(sk))
596 		ret = -EOPNOTSUPP;
597 	else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
598 		ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
599 	else
600 		ret = sock_hash_update_common(map, key, sk, flags);
601 	sock_map_sk_release(sk);
602 out:
603 	fput(sock->file);
604 	return ret;
605 }
606 
607 static int sock_map_update_elem(struct bpf_map *map, void *key,
608 				void *value, u64 flags)
609 {
610 	struct sock *sk = (struct sock *)value;
611 	int ret;
612 
613 	if (!sock_map_sk_is_suitable(sk))
614 		return -EOPNOTSUPP;
615 
616 	local_bh_disable();
617 	bh_lock_sock(sk);
618 	if (!sock_map_sk_state_allowed(sk))
619 		ret = -EOPNOTSUPP;
620 	else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
621 		ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
622 	else
623 		ret = sock_hash_update_common(map, key, sk, flags);
624 	bh_unlock_sock(sk);
625 	local_bh_enable();
626 	return ret;
627 }
628 
629 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
630 	   struct bpf_map *, map, void *, key, u64, flags)
631 {
632 	WARN_ON_ONCE(!rcu_read_lock_held());
633 
634 	if (likely(sock_map_sk_is_suitable(sops->sk) &&
635 		   sock_map_op_okay(sops)))
636 		return sock_map_update_common(map, *(u32 *)key, sops->sk,
637 					      flags);
638 	return -EOPNOTSUPP;
639 }
640 
641 const struct bpf_func_proto bpf_sock_map_update_proto = {
642 	.func		= bpf_sock_map_update,
643 	.gpl_only	= false,
644 	.pkt_access	= true,
645 	.ret_type	= RET_INTEGER,
646 	.arg1_type	= ARG_PTR_TO_CTX,
647 	.arg2_type	= ARG_CONST_MAP_PTR,
648 	.arg3_type	= ARG_PTR_TO_MAP_KEY,
649 	.arg4_type	= ARG_ANYTHING,
650 };
651 
652 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
653 	   struct bpf_map *, map, u32, key, u64, flags)
654 {
655 	struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
656 	struct sock *sk;
657 
658 	if (unlikely(flags & ~(BPF_F_INGRESS)))
659 		return SK_DROP;
660 
661 	sk = __sock_map_lookup_elem(map, key);
662 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
663 		return SK_DROP;
664 
665 	tcb->bpf.flags = flags;
666 	tcb->bpf.sk_redir = sk;
667 	return SK_PASS;
668 }
669 
670 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
671 	.func           = bpf_sk_redirect_map,
672 	.gpl_only       = false,
673 	.ret_type       = RET_INTEGER,
674 	.arg1_type	= ARG_PTR_TO_CTX,
675 	.arg2_type      = ARG_CONST_MAP_PTR,
676 	.arg3_type      = ARG_ANYTHING,
677 	.arg4_type      = ARG_ANYTHING,
678 };
679 
680 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
681 	   struct bpf_map *, map, u32, key, u64, flags)
682 {
683 	struct sock *sk;
684 
685 	if (unlikely(flags & ~(BPF_F_INGRESS)))
686 		return SK_DROP;
687 
688 	sk = __sock_map_lookup_elem(map, key);
689 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
690 		return SK_DROP;
691 
692 	msg->flags = flags;
693 	msg->sk_redir = sk;
694 	return SK_PASS;
695 }
696 
697 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
698 	.func           = bpf_msg_redirect_map,
699 	.gpl_only       = false,
700 	.ret_type       = RET_INTEGER,
701 	.arg1_type	= ARG_PTR_TO_CTX,
702 	.arg2_type      = ARG_CONST_MAP_PTR,
703 	.arg3_type      = ARG_ANYTHING,
704 	.arg4_type      = ARG_ANYTHING,
705 };
706 
707 struct sock_map_seq_info {
708 	struct bpf_map *map;
709 	struct sock *sk;
710 	u32 index;
711 };
712 
713 struct bpf_iter__sockmap {
714 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
715 	__bpf_md_ptr(struct bpf_map *, map);
716 	__bpf_md_ptr(void *, key);
717 	__bpf_md_ptr(struct sock *, sk);
718 };
719 
720 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
721 		     struct bpf_map *map, void *key,
722 		     struct sock *sk)
723 
724 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
725 {
726 	if (unlikely(info->index >= info->map->max_entries))
727 		return NULL;
728 
729 	info->sk = __sock_map_lookup_elem(info->map, info->index);
730 
731 	/* can't return sk directly, since that might be NULL */
732 	return info;
733 }
734 
735 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
736 {
737 	struct sock_map_seq_info *info = seq->private;
738 
739 	if (*pos == 0)
740 		++*pos;
741 
742 	/* pairs with sock_map_seq_stop */
743 	rcu_read_lock();
744 	return sock_map_seq_lookup_elem(info);
745 }
746 
747 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
748 {
749 	struct sock_map_seq_info *info = seq->private;
750 
751 	++*pos;
752 	++info->index;
753 
754 	return sock_map_seq_lookup_elem(info);
755 }
756 
757 static int sock_map_seq_show(struct seq_file *seq, void *v)
758 {
759 	struct sock_map_seq_info *info = seq->private;
760 	struct bpf_iter__sockmap ctx = {};
761 	struct bpf_iter_meta meta;
762 	struct bpf_prog *prog;
763 
764 	meta.seq = seq;
765 	prog = bpf_iter_get_info(&meta, !v);
766 	if (!prog)
767 		return 0;
768 
769 	ctx.meta = &meta;
770 	ctx.map = info->map;
771 	if (v) {
772 		ctx.key = &info->index;
773 		ctx.sk = info->sk;
774 	}
775 
776 	return bpf_iter_run_prog(prog, &ctx);
777 }
778 
779 static void sock_map_seq_stop(struct seq_file *seq, void *v)
780 {
781 	if (!v)
782 		(void)sock_map_seq_show(seq, NULL);
783 
784 	/* pairs with sock_map_seq_start */
785 	rcu_read_unlock();
786 }
787 
788 static const struct seq_operations sock_map_seq_ops = {
789 	.start	= sock_map_seq_start,
790 	.next	= sock_map_seq_next,
791 	.stop	= sock_map_seq_stop,
792 	.show	= sock_map_seq_show,
793 };
794 
795 static int sock_map_init_seq_private(void *priv_data,
796 				     struct bpf_iter_aux_info *aux)
797 {
798 	struct sock_map_seq_info *info = priv_data;
799 
800 	info->map = aux->map;
801 	return 0;
802 }
803 
804 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
805 	.seq_ops		= &sock_map_seq_ops,
806 	.init_seq_private	= sock_map_init_seq_private,
807 	.seq_priv_size		= sizeof(struct sock_map_seq_info),
808 };
809 
810 static int sock_map_btf_id;
811 const struct bpf_map_ops sock_map_ops = {
812 	.map_meta_equal		= bpf_map_meta_equal,
813 	.map_alloc		= sock_map_alloc,
814 	.map_free		= sock_map_free,
815 	.map_get_next_key	= sock_map_get_next_key,
816 	.map_lookup_elem_sys_only = sock_map_lookup_sys,
817 	.map_update_elem	= sock_map_update_elem,
818 	.map_delete_elem	= sock_map_delete_elem,
819 	.map_lookup_elem	= sock_map_lookup,
820 	.map_release_uref	= sock_map_release_progs,
821 	.map_check_btf		= map_check_no_btf,
822 	.map_btf_name		= "bpf_stab",
823 	.map_btf_id		= &sock_map_btf_id,
824 	.iter_seq_info		= &sock_map_iter_seq_info,
825 };
826 
827 struct bpf_shtab_elem {
828 	struct rcu_head rcu;
829 	u32 hash;
830 	struct sock *sk;
831 	struct hlist_node node;
832 	u8 key[];
833 };
834 
835 struct bpf_shtab_bucket {
836 	struct hlist_head head;
837 	raw_spinlock_t lock;
838 };
839 
840 struct bpf_shtab {
841 	struct bpf_map map;
842 	struct bpf_shtab_bucket *buckets;
843 	u32 buckets_num;
844 	u32 elem_size;
845 	struct sk_psock_progs progs;
846 	atomic_t count;
847 };
848 
849 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
850 {
851 	return jhash(key, len, 0);
852 }
853 
854 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
855 							u32 hash)
856 {
857 	return &htab->buckets[hash & (htab->buckets_num - 1)];
858 }
859 
860 static struct bpf_shtab_elem *
861 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
862 			  u32 key_size)
863 {
864 	struct bpf_shtab_elem *elem;
865 
866 	hlist_for_each_entry_rcu(elem, head, node) {
867 		if (elem->hash == hash &&
868 		    !memcmp(&elem->key, key, key_size))
869 			return elem;
870 	}
871 
872 	return NULL;
873 }
874 
875 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
876 {
877 	struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
878 	u32 key_size = map->key_size, hash;
879 	struct bpf_shtab_bucket *bucket;
880 	struct bpf_shtab_elem *elem;
881 
882 	WARN_ON_ONCE(!rcu_read_lock_held());
883 
884 	hash = sock_hash_bucket_hash(key, key_size);
885 	bucket = sock_hash_select_bucket(htab, hash);
886 	elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
887 
888 	return elem ? elem->sk : NULL;
889 }
890 
891 static void sock_hash_free_elem(struct bpf_shtab *htab,
892 				struct bpf_shtab_elem *elem)
893 {
894 	atomic_dec(&htab->count);
895 	kfree_rcu(elem, rcu);
896 }
897 
898 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
899 				       void *link_raw)
900 {
901 	struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
902 	struct bpf_shtab_elem *elem_probe, *elem = link_raw;
903 	struct bpf_shtab_bucket *bucket;
904 
905 	WARN_ON_ONCE(!rcu_read_lock_held());
906 	bucket = sock_hash_select_bucket(htab, elem->hash);
907 
908 	/* elem may be deleted in parallel from the map, but access here
909 	 * is okay since it's going away only after RCU grace period.
910 	 * However, we need to check whether it's still present.
911 	 */
912 	raw_spin_lock_bh(&bucket->lock);
913 	elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
914 					       elem->key, map->key_size);
915 	if (elem_probe && elem_probe == elem) {
916 		hlist_del_rcu(&elem->node);
917 		sock_map_unref(elem->sk, elem);
918 		sock_hash_free_elem(htab, elem);
919 	}
920 	raw_spin_unlock_bh(&bucket->lock);
921 }
922 
923 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
924 {
925 	struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
926 	u32 hash, key_size = map->key_size;
927 	struct bpf_shtab_bucket *bucket;
928 	struct bpf_shtab_elem *elem;
929 	int ret = -ENOENT;
930 
931 	hash = sock_hash_bucket_hash(key, key_size);
932 	bucket = sock_hash_select_bucket(htab, hash);
933 
934 	raw_spin_lock_bh(&bucket->lock);
935 	elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
936 	if (elem) {
937 		hlist_del_rcu(&elem->node);
938 		sock_map_unref(elem->sk, elem);
939 		sock_hash_free_elem(htab, elem);
940 		ret = 0;
941 	}
942 	raw_spin_unlock_bh(&bucket->lock);
943 	return ret;
944 }
945 
946 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
947 						   void *key, u32 key_size,
948 						   u32 hash, struct sock *sk,
949 						   struct bpf_shtab_elem *old)
950 {
951 	struct bpf_shtab_elem *new;
952 
953 	if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
954 		if (!old) {
955 			atomic_dec(&htab->count);
956 			return ERR_PTR(-E2BIG);
957 		}
958 	}
959 
960 	new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
961 			   htab->map.numa_node);
962 	if (!new) {
963 		atomic_dec(&htab->count);
964 		return ERR_PTR(-ENOMEM);
965 	}
966 	memcpy(new->key, key, key_size);
967 	new->sk = sk;
968 	new->hash = hash;
969 	return new;
970 }
971 
972 static int sock_hash_update_common(struct bpf_map *map, void *key,
973 				   struct sock *sk, u64 flags)
974 {
975 	struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
976 	u32 key_size = map->key_size, hash;
977 	struct bpf_shtab_elem *elem, *elem_new;
978 	struct bpf_shtab_bucket *bucket;
979 	struct sk_psock_link *link;
980 	struct sk_psock *psock;
981 	int ret;
982 
983 	WARN_ON_ONCE(!rcu_read_lock_held());
984 	if (unlikely(flags > BPF_EXIST))
985 		return -EINVAL;
986 
987 	link = sk_psock_init_link();
988 	if (!link)
989 		return -ENOMEM;
990 
991 	/* Only sockets we can redirect into/from in BPF need to hold
992 	 * refs to parser/verdict progs and have their sk_data_ready
993 	 * and sk_write_space callbacks overridden.
994 	 */
995 	if (sock_map_redirect_allowed(sk))
996 		ret = sock_map_link(map, &htab->progs, sk);
997 	else
998 		ret = sock_map_link_no_progs(map, sk);
999 	if (ret < 0)
1000 		goto out_free;
1001 
1002 	psock = sk_psock(sk);
1003 	WARN_ON_ONCE(!psock);
1004 
1005 	hash = sock_hash_bucket_hash(key, key_size);
1006 	bucket = sock_hash_select_bucket(htab, hash);
1007 
1008 	raw_spin_lock_bh(&bucket->lock);
1009 	elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
1010 	if (elem && flags == BPF_NOEXIST) {
1011 		ret = -EEXIST;
1012 		goto out_unlock;
1013 	} else if (!elem && flags == BPF_EXIST) {
1014 		ret = -ENOENT;
1015 		goto out_unlock;
1016 	}
1017 
1018 	elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
1019 	if (IS_ERR(elem_new)) {
1020 		ret = PTR_ERR(elem_new);
1021 		goto out_unlock;
1022 	}
1023 
1024 	sock_map_add_link(psock, link, map, elem_new);
1025 	/* Add new element to the head of the list, so that
1026 	 * concurrent search will find it before old elem.
1027 	 */
1028 	hlist_add_head_rcu(&elem_new->node, &bucket->head);
1029 	if (elem) {
1030 		hlist_del_rcu(&elem->node);
1031 		sock_map_unref(elem->sk, elem);
1032 		sock_hash_free_elem(htab, elem);
1033 	}
1034 	raw_spin_unlock_bh(&bucket->lock);
1035 	return 0;
1036 out_unlock:
1037 	raw_spin_unlock_bh(&bucket->lock);
1038 	sk_psock_put(sk, psock);
1039 out_free:
1040 	sk_psock_free_link(link);
1041 	return ret;
1042 }
1043 
1044 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1045 				  void *key_next)
1046 {
1047 	struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1048 	struct bpf_shtab_elem *elem, *elem_next;
1049 	u32 hash, key_size = map->key_size;
1050 	struct hlist_head *head;
1051 	int i = 0;
1052 
1053 	if (!key)
1054 		goto find_first_elem;
1055 	hash = sock_hash_bucket_hash(key, key_size);
1056 	head = &sock_hash_select_bucket(htab, hash)->head;
1057 	elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1058 	if (!elem)
1059 		goto find_first_elem;
1060 
1061 	elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1062 				     struct bpf_shtab_elem, node);
1063 	if (elem_next) {
1064 		memcpy(key_next, elem_next->key, key_size);
1065 		return 0;
1066 	}
1067 
1068 	i = hash & (htab->buckets_num - 1);
1069 	i++;
1070 find_first_elem:
1071 	for (; i < htab->buckets_num; i++) {
1072 		head = &sock_hash_select_bucket(htab, i)->head;
1073 		elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1074 					     struct bpf_shtab_elem, node);
1075 		if (elem_next) {
1076 			memcpy(key_next, elem_next->key, key_size);
1077 			return 0;
1078 		}
1079 	}
1080 
1081 	return -ENOENT;
1082 }
1083 
1084 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1085 {
1086 	struct bpf_shtab *htab;
1087 	int i, err;
1088 	u64 cost;
1089 
1090 	if (!capable(CAP_NET_ADMIN))
1091 		return ERR_PTR(-EPERM);
1092 	if (attr->max_entries == 0 ||
1093 	    attr->key_size    == 0 ||
1094 	    (attr->value_size != sizeof(u32) &&
1095 	     attr->value_size != sizeof(u64)) ||
1096 	    attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1097 		return ERR_PTR(-EINVAL);
1098 	if (attr->key_size > MAX_BPF_STACK)
1099 		return ERR_PTR(-E2BIG);
1100 
1101 	htab = kzalloc(sizeof(*htab), GFP_USER);
1102 	if (!htab)
1103 		return ERR_PTR(-ENOMEM);
1104 
1105 	bpf_map_init_from_attr(&htab->map, attr);
1106 
1107 	htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1108 	htab->elem_size = sizeof(struct bpf_shtab_elem) +
1109 			  round_up(htab->map.key_size, 8);
1110 	if (htab->buckets_num == 0 ||
1111 	    htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1112 		err = -EINVAL;
1113 		goto free_htab;
1114 	}
1115 
1116 	cost = (u64) htab->buckets_num * sizeof(struct bpf_shtab_bucket) +
1117 	       (u64) htab->elem_size * htab->map.max_entries;
1118 	if (cost >= U32_MAX - PAGE_SIZE) {
1119 		err = -EINVAL;
1120 		goto free_htab;
1121 	}
1122 	err = bpf_map_charge_init(&htab->map.memory, cost);
1123 	if (err)
1124 		goto free_htab;
1125 
1126 	htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1127 					   sizeof(struct bpf_shtab_bucket),
1128 					   htab->map.numa_node);
1129 	if (!htab->buckets) {
1130 		bpf_map_charge_finish(&htab->map.memory);
1131 		err = -ENOMEM;
1132 		goto free_htab;
1133 	}
1134 
1135 	for (i = 0; i < htab->buckets_num; i++) {
1136 		INIT_HLIST_HEAD(&htab->buckets[i].head);
1137 		raw_spin_lock_init(&htab->buckets[i].lock);
1138 	}
1139 
1140 	return &htab->map;
1141 free_htab:
1142 	kfree(htab);
1143 	return ERR_PTR(err);
1144 }
1145 
1146 static void sock_hash_free(struct bpf_map *map)
1147 {
1148 	struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1149 	struct bpf_shtab_bucket *bucket;
1150 	struct hlist_head unlink_list;
1151 	struct bpf_shtab_elem *elem;
1152 	struct hlist_node *node;
1153 	int i;
1154 
1155 	/* After the sync no updates or deletes will be in-flight so it
1156 	 * is safe to walk map and remove entries without risking a race
1157 	 * in EEXIST update case.
1158 	 */
1159 	synchronize_rcu();
1160 	for (i = 0; i < htab->buckets_num; i++) {
1161 		bucket = sock_hash_select_bucket(htab, i);
1162 
1163 		/* We are racing with sock_hash_delete_from_link to
1164 		 * enter the spin-lock critical section. Every socket on
1165 		 * the list is still linked to sockhash. Since link
1166 		 * exists, psock exists and holds a ref to socket. That
1167 		 * lets us to grab a socket ref too.
1168 		 */
1169 		raw_spin_lock_bh(&bucket->lock);
1170 		hlist_for_each_entry(elem, &bucket->head, node)
1171 			sock_hold(elem->sk);
1172 		hlist_move_list(&bucket->head, &unlink_list);
1173 		raw_spin_unlock_bh(&bucket->lock);
1174 
1175 		/* Process removed entries out of atomic context to
1176 		 * block for socket lock before deleting the psock's
1177 		 * link to sockhash.
1178 		 */
1179 		hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1180 			hlist_del(&elem->node);
1181 			lock_sock(elem->sk);
1182 			rcu_read_lock();
1183 			sock_map_unref(elem->sk, elem);
1184 			rcu_read_unlock();
1185 			release_sock(elem->sk);
1186 			sock_put(elem->sk);
1187 			sock_hash_free_elem(htab, elem);
1188 		}
1189 	}
1190 
1191 	/* wait for psock readers accessing its map link */
1192 	synchronize_rcu();
1193 
1194 	bpf_map_area_free(htab->buckets);
1195 	kfree(htab);
1196 }
1197 
1198 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1199 {
1200 	struct sock *sk;
1201 
1202 	if (map->value_size != sizeof(u64))
1203 		return ERR_PTR(-ENOSPC);
1204 
1205 	sk = __sock_hash_lookup_elem(map, key);
1206 	if (!sk)
1207 		return ERR_PTR(-ENOENT);
1208 
1209 	sock_gen_cookie(sk);
1210 	return &sk->sk_cookie;
1211 }
1212 
1213 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1214 {
1215 	struct sock *sk;
1216 
1217 	sk = __sock_hash_lookup_elem(map, key);
1218 	if (!sk)
1219 		return NULL;
1220 	if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1221 		return NULL;
1222 	return sk;
1223 }
1224 
1225 static void sock_hash_release_progs(struct bpf_map *map)
1226 {
1227 	psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1228 }
1229 
1230 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1231 	   struct bpf_map *, map, void *, key, u64, flags)
1232 {
1233 	WARN_ON_ONCE(!rcu_read_lock_held());
1234 
1235 	if (likely(sock_map_sk_is_suitable(sops->sk) &&
1236 		   sock_map_op_okay(sops)))
1237 		return sock_hash_update_common(map, key, sops->sk, flags);
1238 	return -EOPNOTSUPP;
1239 }
1240 
1241 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1242 	.func		= bpf_sock_hash_update,
1243 	.gpl_only	= false,
1244 	.pkt_access	= true,
1245 	.ret_type	= RET_INTEGER,
1246 	.arg1_type	= ARG_PTR_TO_CTX,
1247 	.arg2_type	= ARG_CONST_MAP_PTR,
1248 	.arg3_type	= ARG_PTR_TO_MAP_KEY,
1249 	.arg4_type	= ARG_ANYTHING,
1250 };
1251 
1252 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1253 	   struct bpf_map *, map, void *, key, u64, flags)
1254 {
1255 	struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1256 	struct sock *sk;
1257 
1258 	if (unlikely(flags & ~(BPF_F_INGRESS)))
1259 		return SK_DROP;
1260 
1261 	sk = __sock_hash_lookup_elem(map, key);
1262 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1263 		return SK_DROP;
1264 
1265 	tcb->bpf.flags = flags;
1266 	tcb->bpf.sk_redir = sk;
1267 	return SK_PASS;
1268 }
1269 
1270 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1271 	.func           = bpf_sk_redirect_hash,
1272 	.gpl_only       = false,
1273 	.ret_type       = RET_INTEGER,
1274 	.arg1_type	= ARG_PTR_TO_CTX,
1275 	.arg2_type      = ARG_CONST_MAP_PTR,
1276 	.arg3_type      = ARG_PTR_TO_MAP_KEY,
1277 	.arg4_type      = ARG_ANYTHING,
1278 };
1279 
1280 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1281 	   struct bpf_map *, map, void *, key, u64, flags)
1282 {
1283 	struct sock *sk;
1284 
1285 	if (unlikely(flags & ~(BPF_F_INGRESS)))
1286 		return SK_DROP;
1287 
1288 	sk = __sock_hash_lookup_elem(map, key);
1289 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1290 		return SK_DROP;
1291 
1292 	msg->flags = flags;
1293 	msg->sk_redir = sk;
1294 	return SK_PASS;
1295 }
1296 
1297 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1298 	.func           = bpf_msg_redirect_hash,
1299 	.gpl_only       = false,
1300 	.ret_type       = RET_INTEGER,
1301 	.arg1_type	= ARG_PTR_TO_CTX,
1302 	.arg2_type      = ARG_CONST_MAP_PTR,
1303 	.arg3_type      = ARG_PTR_TO_MAP_KEY,
1304 	.arg4_type      = ARG_ANYTHING,
1305 };
1306 
1307 struct sock_hash_seq_info {
1308 	struct bpf_map *map;
1309 	struct bpf_shtab *htab;
1310 	u32 bucket_id;
1311 };
1312 
1313 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1314 				     struct bpf_shtab_elem *prev_elem)
1315 {
1316 	const struct bpf_shtab *htab = info->htab;
1317 	struct bpf_shtab_bucket *bucket;
1318 	struct bpf_shtab_elem *elem;
1319 	struct hlist_node *node;
1320 
1321 	/* try to find next elem in the same bucket */
1322 	if (prev_elem) {
1323 		node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1324 		elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1325 		if (elem)
1326 			return elem;
1327 
1328 		/* no more elements, continue in the next bucket */
1329 		info->bucket_id++;
1330 	}
1331 
1332 	for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1333 		bucket = &htab->buckets[info->bucket_id];
1334 		node = rcu_dereference(hlist_first_rcu(&bucket->head));
1335 		elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1336 		if (elem)
1337 			return elem;
1338 	}
1339 
1340 	return NULL;
1341 }
1342 
1343 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1344 {
1345 	struct sock_hash_seq_info *info = seq->private;
1346 
1347 	if (*pos == 0)
1348 		++*pos;
1349 
1350 	/* pairs with sock_hash_seq_stop */
1351 	rcu_read_lock();
1352 	return sock_hash_seq_find_next(info, NULL);
1353 }
1354 
1355 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1356 {
1357 	struct sock_hash_seq_info *info = seq->private;
1358 
1359 	++*pos;
1360 	return sock_hash_seq_find_next(info, v);
1361 }
1362 
1363 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1364 {
1365 	struct sock_hash_seq_info *info = seq->private;
1366 	struct bpf_iter__sockmap ctx = {};
1367 	struct bpf_shtab_elem *elem = v;
1368 	struct bpf_iter_meta meta;
1369 	struct bpf_prog *prog;
1370 
1371 	meta.seq = seq;
1372 	prog = bpf_iter_get_info(&meta, !elem);
1373 	if (!prog)
1374 		return 0;
1375 
1376 	ctx.meta = &meta;
1377 	ctx.map = info->map;
1378 	if (elem) {
1379 		ctx.key = elem->key;
1380 		ctx.sk = elem->sk;
1381 	}
1382 
1383 	return bpf_iter_run_prog(prog, &ctx);
1384 }
1385 
1386 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1387 {
1388 	if (!v)
1389 		(void)sock_hash_seq_show(seq, NULL);
1390 
1391 	/* pairs with sock_hash_seq_start */
1392 	rcu_read_unlock();
1393 }
1394 
1395 static const struct seq_operations sock_hash_seq_ops = {
1396 	.start	= sock_hash_seq_start,
1397 	.next	= sock_hash_seq_next,
1398 	.stop	= sock_hash_seq_stop,
1399 	.show	= sock_hash_seq_show,
1400 };
1401 
1402 static int sock_hash_init_seq_private(void *priv_data,
1403 				     struct bpf_iter_aux_info *aux)
1404 {
1405 	struct sock_hash_seq_info *info = priv_data;
1406 
1407 	info->map = aux->map;
1408 	info->htab = container_of(aux->map, struct bpf_shtab, map);
1409 	return 0;
1410 }
1411 
1412 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1413 	.seq_ops		= &sock_hash_seq_ops,
1414 	.init_seq_private	= sock_hash_init_seq_private,
1415 	.seq_priv_size		= sizeof(struct sock_hash_seq_info),
1416 };
1417 
1418 static int sock_hash_map_btf_id;
1419 const struct bpf_map_ops sock_hash_ops = {
1420 	.map_meta_equal		= bpf_map_meta_equal,
1421 	.map_alloc		= sock_hash_alloc,
1422 	.map_free		= sock_hash_free,
1423 	.map_get_next_key	= sock_hash_get_next_key,
1424 	.map_update_elem	= sock_map_update_elem,
1425 	.map_delete_elem	= sock_hash_delete_elem,
1426 	.map_lookup_elem	= sock_hash_lookup,
1427 	.map_lookup_elem_sys_only = sock_hash_lookup_sys,
1428 	.map_release_uref	= sock_hash_release_progs,
1429 	.map_check_btf		= map_check_no_btf,
1430 	.map_btf_name		= "bpf_shtab",
1431 	.map_btf_id		= &sock_hash_map_btf_id,
1432 	.iter_seq_info		= &sock_hash_iter_seq_info,
1433 };
1434 
1435 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1436 {
1437 	switch (map->map_type) {
1438 	case BPF_MAP_TYPE_SOCKMAP:
1439 		return &container_of(map, struct bpf_stab, map)->progs;
1440 	case BPF_MAP_TYPE_SOCKHASH:
1441 		return &container_of(map, struct bpf_shtab, map)->progs;
1442 	default:
1443 		break;
1444 	}
1445 
1446 	return NULL;
1447 }
1448 
1449 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1450 			 struct bpf_prog *old, u32 which)
1451 {
1452 	struct sk_psock_progs *progs = sock_map_progs(map);
1453 	struct bpf_prog **pprog;
1454 
1455 	if (!progs)
1456 		return -EOPNOTSUPP;
1457 
1458 	switch (which) {
1459 	case BPF_SK_MSG_VERDICT:
1460 		pprog = &progs->msg_parser;
1461 		break;
1462 	case BPF_SK_SKB_STREAM_PARSER:
1463 		pprog = &progs->skb_parser;
1464 		break;
1465 	case BPF_SK_SKB_STREAM_VERDICT:
1466 		pprog = &progs->skb_verdict;
1467 		break;
1468 	default:
1469 		return -EOPNOTSUPP;
1470 	}
1471 
1472 	if (old)
1473 		return psock_replace_prog(pprog, prog, old);
1474 
1475 	psock_set_prog(pprog, prog);
1476 	return 0;
1477 }
1478 
1479 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1480 {
1481 	switch (link->map->map_type) {
1482 	case BPF_MAP_TYPE_SOCKMAP:
1483 		return sock_map_delete_from_link(link->map, sk,
1484 						 link->link_raw);
1485 	case BPF_MAP_TYPE_SOCKHASH:
1486 		return sock_hash_delete_from_link(link->map, sk,
1487 						  link->link_raw);
1488 	default:
1489 		break;
1490 	}
1491 }
1492 
1493 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1494 {
1495 	struct sk_psock_link *link;
1496 
1497 	while ((link = sk_psock_link_pop(psock))) {
1498 		sock_map_unlink(sk, link);
1499 		sk_psock_free_link(link);
1500 	}
1501 }
1502 
1503 void sock_map_unhash(struct sock *sk)
1504 {
1505 	void (*saved_unhash)(struct sock *sk);
1506 	struct sk_psock *psock;
1507 
1508 	rcu_read_lock();
1509 	psock = sk_psock(sk);
1510 	if (unlikely(!psock)) {
1511 		rcu_read_unlock();
1512 		if (sk->sk_prot->unhash)
1513 			sk->sk_prot->unhash(sk);
1514 		return;
1515 	}
1516 
1517 	saved_unhash = psock->saved_unhash;
1518 	sock_map_remove_links(sk, psock);
1519 	rcu_read_unlock();
1520 	saved_unhash(sk);
1521 }
1522 
1523 void sock_map_close(struct sock *sk, long timeout)
1524 {
1525 	void (*saved_close)(struct sock *sk, long timeout);
1526 	struct sk_psock *psock;
1527 
1528 	lock_sock(sk);
1529 	rcu_read_lock();
1530 	psock = sk_psock(sk);
1531 	if (unlikely(!psock)) {
1532 		rcu_read_unlock();
1533 		release_sock(sk);
1534 		return sk->sk_prot->close(sk, timeout);
1535 	}
1536 
1537 	saved_close = psock->saved_close;
1538 	sock_map_remove_links(sk, psock);
1539 	rcu_read_unlock();
1540 	release_sock(sk);
1541 	saved_close(sk, timeout);
1542 }
1543 
1544 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1545 				       union bpf_iter_link_info *linfo,
1546 				       struct bpf_iter_aux_info *aux)
1547 {
1548 	struct bpf_map *map;
1549 	int err = -EINVAL;
1550 
1551 	if (!linfo->map.map_fd)
1552 		return -EBADF;
1553 
1554 	map = bpf_map_get_with_uref(linfo->map.map_fd);
1555 	if (IS_ERR(map))
1556 		return PTR_ERR(map);
1557 
1558 	if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1559 	    map->map_type != BPF_MAP_TYPE_SOCKHASH)
1560 		goto put_map;
1561 
1562 	if (prog->aux->max_rdonly_access > map->key_size) {
1563 		err = -EACCES;
1564 		goto put_map;
1565 	}
1566 
1567 	aux->map = map;
1568 	return 0;
1569 
1570 put_map:
1571 	bpf_map_put_with_uref(map);
1572 	return err;
1573 }
1574 
1575 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1576 {
1577 	bpf_map_put_with_uref(aux->map);
1578 }
1579 
1580 static struct bpf_iter_reg sock_map_iter_reg = {
1581 	.target			= "sockmap",
1582 	.attach_target		= sock_map_iter_attach_target,
1583 	.detach_target		= sock_map_iter_detach_target,
1584 	.show_fdinfo		= bpf_iter_map_show_fdinfo,
1585 	.fill_link_info		= bpf_iter_map_fill_link_info,
1586 	.ctx_arg_info_size	= 2,
1587 	.ctx_arg_info		= {
1588 		{ offsetof(struct bpf_iter__sockmap, key),
1589 		  PTR_TO_RDONLY_BUF_OR_NULL },
1590 		{ offsetof(struct bpf_iter__sockmap, sk),
1591 		  PTR_TO_BTF_ID_OR_NULL },
1592 	},
1593 };
1594 
1595 static int __init bpf_sockmap_iter_init(void)
1596 {
1597 	sock_map_iter_reg.ctx_arg_info[1].btf_id =
1598 		btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1599 	return bpf_iter_reg_target(&sock_map_iter_reg);
1600 }
1601 late_initcall(bpf_sockmap_iter_init);
1602