xref: /openbmc/linux/net/core/sock_map.c (revision da60fbe7)
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/filter.h>
6 #include <linux/errno.h>
7 #include <linux/file.h>
8 #include <linux/net.h>
9 #include <linux/workqueue.h>
10 #include <linux/skmsg.h>
11 #include <linux/list.h>
12 #include <linux/jhash.h>
13 #include <linux/sock_diag.h>
14 
15 struct bpf_stab {
16 	struct bpf_map map;
17 	struct sock **sks;
18 	struct sk_psock_progs progs;
19 	raw_spinlock_t lock;
20 };
21 
22 #define SOCK_CREATE_FLAG_MASK				\
23 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
24 
25 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
26 {
27 	struct bpf_stab *stab;
28 	u64 cost;
29 	int err;
30 
31 	if (!capable(CAP_NET_ADMIN))
32 		return ERR_PTR(-EPERM);
33 	if (attr->max_entries == 0 ||
34 	    attr->key_size    != 4 ||
35 	    (attr->value_size != sizeof(u32) &&
36 	     attr->value_size != sizeof(u64)) ||
37 	    attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
38 		return ERR_PTR(-EINVAL);
39 
40 	stab = kzalloc(sizeof(*stab), GFP_USER);
41 	if (!stab)
42 		return ERR_PTR(-ENOMEM);
43 
44 	bpf_map_init_from_attr(&stab->map, attr);
45 	raw_spin_lock_init(&stab->lock);
46 
47 	/* Make sure page count doesn't overflow. */
48 	cost = (u64) stab->map.max_entries * sizeof(struct sock *);
49 	err = bpf_map_charge_init(&stab->map.memory, cost);
50 	if (err)
51 		goto free_stab;
52 
53 	stab->sks = bpf_map_area_alloc(stab->map.max_entries *
54 				       sizeof(struct sock *),
55 				       stab->map.numa_node);
56 	if (stab->sks)
57 		return &stab->map;
58 	err = -ENOMEM;
59 	bpf_map_charge_finish(&stab->map.memory);
60 free_stab:
61 	kfree(stab);
62 	return ERR_PTR(err);
63 }
64 
65 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
66 {
67 	u32 ufd = attr->target_fd;
68 	struct bpf_map *map;
69 	struct fd f;
70 	int ret;
71 
72 	f = fdget(ufd);
73 	map = __bpf_map_get(f);
74 	if (IS_ERR(map))
75 		return PTR_ERR(map);
76 	ret = sock_map_prog_update(map, prog, attr->attach_type);
77 	fdput(f);
78 	return ret;
79 }
80 
81 static void sock_map_sk_acquire(struct sock *sk)
82 	__acquires(&sk->sk_lock.slock)
83 {
84 	lock_sock(sk);
85 	preempt_disable();
86 	rcu_read_lock();
87 }
88 
89 static void sock_map_sk_release(struct sock *sk)
90 	__releases(&sk->sk_lock.slock)
91 {
92 	rcu_read_unlock();
93 	preempt_enable();
94 	release_sock(sk);
95 }
96 
97 static void sock_map_add_link(struct sk_psock *psock,
98 			      struct sk_psock_link *link,
99 			      struct bpf_map *map, void *link_raw)
100 {
101 	link->link_raw = link_raw;
102 	link->map = map;
103 	spin_lock_bh(&psock->link_lock);
104 	list_add_tail(&link->list, &psock->link);
105 	spin_unlock_bh(&psock->link_lock);
106 }
107 
108 static void sock_map_del_link(struct sock *sk,
109 			      struct sk_psock *psock, void *link_raw)
110 {
111 	struct sk_psock_link *link, *tmp;
112 	bool strp_stop = false;
113 
114 	spin_lock_bh(&psock->link_lock);
115 	list_for_each_entry_safe(link, tmp, &psock->link, list) {
116 		if (link->link_raw == link_raw) {
117 			struct bpf_map *map = link->map;
118 			struct bpf_stab *stab = container_of(map, struct bpf_stab,
119 							     map);
120 			if (psock->parser.enabled && stab->progs.skb_parser)
121 				strp_stop = true;
122 			list_del(&link->list);
123 			sk_psock_free_link(link);
124 		}
125 	}
126 	spin_unlock_bh(&psock->link_lock);
127 	if (strp_stop) {
128 		write_lock_bh(&sk->sk_callback_lock);
129 		sk_psock_stop_strp(sk, psock);
130 		write_unlock_bh(&sk->sk_callback_lock);
131 	}
132 }
133 
134 static void sock_map_unref(struct sock *sk, void *link_raw)
135 {
136 	struct sk_psock *psock = sk_psock(sk);
137 
138 	if (likely(psock)) {
139 		sock_map_del_link(sk, psock, link_raw);
140 		sk_psock_put(sk, psock);
141 	}
142 }
143 
144 static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
145 			 struct sock *sk)
146 {
147 	struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
148 	bool skb_progs, sk_psock_is_new = false;
149 	struct sk_psock *psock;
150 	int ret;
151 
152 	skb_verdict = READ_ONCE(progs->skb_verdict);
153 	skb_parser = READ_ONCE(progs->skb_parser);
154 	skb_progs = skb_parser && skb_verdict;
155 	if (skb_progs) {
156 		skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
157 		if (IS_ERR(skb_verdict))
158 			return PTR_ERR(skb_verdict);
159 		skb_parser = bpf_prog_inc_not_zero(skb_parser);
160 		if (IS_ERR(skb_parser)) {
161 			bpf_prog_put(skb_verdict);
162 			return PTR_ERR(skb_parser);
163 		}
164 	}
165 
166 	msg_parser = READ_ONCE(progs->msg_parser);
167 	if (msg_parser) {
168 		msg_parser = bpf_prog_inc_not_zero(msg_parser);
169 		if (IS_ERR(msg_parser)) {
170 			ret = PTR_ERR(msg_parser);
171 			goto out;
172 		}
173 	}
174 
175 	psock = sk_psock_get_checked(sk);
176 	if (IS_ERR(psock)) {
177 		ret = PTR_ERR(psock);
178 		goto out_progs;
179 	}
180 
181 	if (psock) {
182 		if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
183 		    (skb_progs  && READ_ONCE(psock->progs.skb_parser))) {
184 			sk_psock_put(sk, psock);
185 			ret = -EBUSY;
186 			goto out_progs;
187 		}
188 	} else {
189 		psock = sk_psock_init(sk, map->numa_node);
190 		if (!psock) {
191 			ret = -ENOMEM;
192 			goto out_progs;
193 		}
194 		sk_psock_is_new = true;
195 	}
196 
197 	if (msg_parser)
198 		psock_set_prog(&psock->progs.msg_parser, msg_parser);
199 	if (sk_psock_is_new) {
200 		ret = tcp_bpf_init(sk);
201 		if (ret < 0)
202 			goto out_drop;
203 	} else {
204 		tcp_bpf_reinit(sk);
205 	}
206 
207 	write_lock_bh(&sk->sk_callback_lock);
208 	if (skb_progs && !psock->parser.enabled) {
209 		ret = sk_psock_init_strp(sk, psock);
210 		if (ret) {
211 			write_unlock_bh(&sk->sk_callback_lock);
212 			goto out_drop;
213 		}
214 		psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
215 		psock_set_prog(&psock->progs.skb_parser, skb_parser);
216 		sk_psock_start_strp(sk, psock);
217 	}
218 	write_unlock_bh(&sk->sk_callback_lock);
219 	return 0;
220 out_drop:
221 	sk_psock_put(sk, psock);
222 out_progs:
223 	if (msg_parser)
224 		bpf_prog_put(msg_parser);
225 out:
226 	if (skb_progs) {
227 		bpf_prog_put(skb_verdict);
228 		bpf_prog_put(skb_parser);
229 	}
230 	return ret;
231 }
232 
233 static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)
234 {
235 	struct sk_psock *psock;
236 	int ret;
237 
238 	psock = sk_psock_get_checked(sk);
239 	if (IS_ERR(psock))
240 		return PTR_ERR(psock);
241 
242 	if (psock) {
243 		tcp_bpf_reinit(sk);
244 		return 0;
245 	}
246 
247 	psock = sk_psock_init(sk, map->numa_node);
248 	if (!psock)
249 		return -ENOMEM;
250 
251 	ret = tcp_bpf_init(sk);
252 	if (ret < 0)
253 		sk_psock_put(sk, psock);
254 	return ret;
255 }
256 
257 static void sock_map_free(struct bpf_map *map)
258 {
259 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
260 	int i;
261 
262 	synchronize_rcu();
263 	raw_spin_lock_bh(&stab->lock);
264 	for (i = 0; i < stab->map.max_entries; i++) {
265 		struct sock **psk = &stab->sks[i];
266 		struct sock *sk;
267 
268 		sk = xchg(psk, NULL);
269 		if (sk) {
270 			lock_sock(sk);
271 			rcu_read_lock();
272 			sock_map_unref(sk, psk);
273 			rcu_read_unlock();
274 			release_sock(sk);
275 		}
276 	}
277 	raw_spin_unlock_bh(&stab->lock);
278 
279 	/* wait for psock readers accessing its map link */
280 	synchronize_rcu();
281 
282 	bpf_map_area_free(stab->sks);
283 	kfree(stab);
284 }
285 
286 static void sock_map_release_progs(struct bpf_map *map)
287 {
288 	psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
289 }
290 
291 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
292 {
293 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
294 
295 	WARN_ON_ONCE(!rcu_read_lock_held());
296 
297 	if (unlikely(key >= map->max_entries))
298 		return NULL;
299 	return READ_ONCE(stab->sks[key]);
300 }
301 
302 static void *sock_map_lookup(struct bpf_map *map, void *key)
303 {
304 	return __sock_map_lookup_elem(map, *(u32 *)key);
305 }
306 
307 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
308 {
309 	struct sock *sk;
310 
311 	if (map->value_size != sizeof(u64))
312 		return ERR_PTR(-ENOSPC);
313 
314 	sk = __sock_map_lookup_elem(map, *(u32 *)key);
315 	if (!sk)
316 		return ERR_PTR(-ENOENT);
317 
318 	sock_gen_cookie(sk);
319 	return &sk->sk_cookie;
320 }
321 
322 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
323 			     struct sock **psk)
324 {
325 	struct sock *sk;
326 	int err = 0;
327 
328 	raw_spin_lock_bh(&stab->lock);
329 	sk = *psk;
330 	if (!sk_test || sk_test == sk)
331 		sk = xchg(psk, NULL);
332 
333 	if (likely(sk))
334 		sock_map_unref(sk, psk);
335 	else
336 		err = -EINVAL;
337 
338 	raw_spin_unlock_bh(&stab->lock);
339 	return err;
340 }
341 
342 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
343 				      void *link_raw)
344 {
345 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
346 
347 	__sock_map_delete(stab, sk, link_raw);
348 }
349 
350 static int sock_map_delete_elem(struct bpf_map *map, void *key)
351 {
352 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
353 	u32 i = *(u32 *)key;
354 	struct sock **psk;
355 
356 	if (unlikely(i >= map->max_entries))
357 		return -EINVAL;
358 
359 	psk = &stab->sks[i];
360 	return __sock_map_delete(stab, NULL, psk);
361 }
362 
363 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
364 {
365 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
366 	u32 i = key ? *(u32 *)key : U32_MAX;
367 	u32 *key_next = next;
368 
369 	if (i == stab->map.max_entries - 1)
370 		return -ENOENT;
371 	if (i >= stab->map.max_entries)
372 		*key_next = 0;
373 	else
374 		*key_next = i + 1;
375 	return 0;
376 }
377 
378 static bool sock_map_redirect_allowed(const struct sock *sk)
379 {
380 	return sk->sk_state != TCP_LISTEN;
381 }
382 
383 static int sock_map_update_common(struct bpf_map *map, u32 idx,
384 				  struct sock *sk, u64 flags)
385 {
386 	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
387 	struct inet_connection_sock *icsk = inet_csk(sk);
388 	struct sk_psock_link *link;
389 	struct sk_psock *psock;
390 	struct sock *osk;
391 	int ret;
392 
393 	WARN_ON_ONCE(!rcu_read_lock_held());
394 	if (unlikely(flags > BPF_EXIST))
395 		return -EINVAL;
396 	if (unlikely(idx >= map->max_entries))
397 		return -E2BIG;
398 	if (unlikely(rcu_access_pointer(icsk->icsk_ulp_data)))
399 		return -EINVAL;
400 
401 	link = sk_psock_init_link();
402 	if (!link)
403 		return -ENOMEM;
404 
405 	/* Only sockets we can redirect into/from in BPF need to hold
406 	 * refs to parser/verdict progs and have their sk_data_ready
407 	 * and sk_write_space callbacks overridden.
408 	 */
409 	if (sock_map_redirect_allowed(sk))
410 		ret = sock_map_link(map, &stab->progs, sk);
411 	else
412 		ret = sock_map_link_no_progs(map, sk);
413 	if (ret < 0)
414 		goto out_free;
415 
416 	psock = sk_psock(sk);
417 	WARN_ON_ONCE(!psock);
418 
419 	raw_spin_lock_bh(&stab->lock);
420 	osk = stab->sks[idx];
421 	if (osk && flags == BPF_NOEXIST) {
422 		ret = -EEXIST;
423 		goto out_unlock;
424 	} else if (!osk && flags == BPF_EXIST) {
425 		ret = -ENOENT;
426 		goto out_unlock;
427 	}
428 
429 	sock_map_add_link(psock, link, map, &stab->sks[idx]);
430 	stab->sks[idx] = sk;
431 	if (osk)
432 		sock_map_unref(osk, &stab->sks[idx]);
433 	raw_spin_unlock_bh(&stab->lock);
434 	return 0;
435 out_unlock:
436 	raw_spin_unlock_bh(&stab->lock);
437 	if (psock)
438 		sk_psock_put(sk, psock);
439 out_free:
440 	sk_psock_free_link(link);
441 	return ret;
442 }
443 
444 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
445 {
446 	return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
447 	       ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
448 	       ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
449 }
450 
451 static bool sock_map_sk_is_suitable(const struct sock *sk)
452 {
453 	return sk->sk_type == SOCK_STREAM &&
454 	       sk->sk_protocol == IPPROTO_TCP;
455 }
456 
457 static bool sock_map_sk_state_allowed(const struct sock *sk)
458 {
459 	return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
460 }
461 
462 static int sock_map_update_elem(struct bpf_map *map, void *key,
463 				void *value, u64 flags)
464 {
465 	u32 idx = *(u32 *)key;
466 	struct socket *sock;
467 	struct sock *sk;
468 	int ret;
469 	u64 ufd;
470 
471 	if (map->value_size == sizeof(u64))
472 		ufd = *(u64 *)value;
473 	else
474 		ufd = *(u32 *)value;
475 	if (ufd > S32_MAX)
476 		return -EINVAL;
477 
478 	sock = sockfd_lookup(ufd, &ret);
479 	if (!sock)
480 		return ret;
481 	sk = sock->sk;
482 	if (!sk) {
483 		ret = -EINVAL;
484 		goto out;
485 	}
486 	if (!sock_map_sk_is_suitable(sk)) {
487 		ret = -EOPNOTSUPP;
488 		goto out;
489 	}
490 
491 	sock_map_sk_acquire(sk);
492 	if (!sock_map_sk_state_allowed(sk))
493 		ret = -EOPNOTSUPP;
494 	else
495 		ret = sock_map_update_common(map, idx, sk, flags);
496 	sock_map_sk_release(sk);
497 out:
498 	fput(sock->file);
499 	return ret;
500 }
501 
502 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
503 	   struct bpf_map *, map, void *, key, u64, flags)
504 {
505 	WARN_ON_ONCE(!rcu_read_lock_held());
506 
507 	if (likely(sock_map_sk_is_suitable(sops->sk) &&
508 		   sock_map_op_okay(sops)))
509 		return sock_map_update_common(map, *(u32 *)key, sops->sk,
510 					      flags);
511 	return -EOPNOTSUPP;
512 }
513 
514 const struct bpf_func_proto bpf_sock_map_update_proto = {
515 	.func		= bpf_sock_map_update,
516 	.gpl_only	= false,
517 	.pkt_access	= true,
518 	.ret_type	= RET_INTEGER,
519 	.arg1_type	= ARG_PTR_TO_CTX,
520 	.arg2_type	= ARG_CONST_MAP_PTR,
521 	.arg3_type	= ARG_PTR_TO_MAP_KEY,
522 	.arg4_type	= ARG_ANYTHING,
523 };
524 
525 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
526 	   struct bpf_map *, map, u32, key, u64, flags)
527 {
528 	struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
529 	struct sock *sk;
530 
531 	if (unlikely(flags & ~(BPF_F_INGRESS)))
532 		return SK_DROP;
533 
534 	sk = __sock_map_lookup_elem(map, key);
535 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
536 		return SK_DROP;
537 
538 	tcb->bpf.flags = flags;
539 	tcb->bpf.sk_redir = sk;
540 	return SK_PASS;
541 }
542 
543 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
544 	.func           = bpf_sk_redirect_map,
545 	.gpl_only       = false,
546 	.ret_type       = RET_INTEGER,
547 	.arg1_type	= ARG_PTR_TO_CTX,
548 	.arg2_type      = ARG_CONST_MAP_PTR,
549 	.arg3_type      = ARG_ANYTHING,
550 	.arg4_type      = ARG_ANYTHING,
551 };
552 
553 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
554 	   struct bpf_map *, map, u32, key, u64, flags)
555 {
556 	struct sock *sk;
557 
558 	if (unlikely(flags & ~(BPF_F_INGRESS)))
559 		return SK_DROP;
560 
561 	sk = __sock_map_lookup_elem(map, key);
562 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
563 		return SK_DROP;
564 
565 	msg->flags = flags;
566 	msg->sk_redir = sk;
567 	return SK_PASS;
568 }
569 
570 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
571 	.func           = bpf_msg_redirect_map,
572 	.gpl_only       = false,
573 	.ret_type       = RET_INTEGER,
574 	.arg1_type	= ARG_PTR_TO_CTX,
575 	.arg2_type      = ARG_CONST_MAP_PTR,
576 	.arg3_type      = ARG_ANYTHING,
577 	.arg4_type      = ARG_ANYTHING,
578 };
579 
580 const struct bpf_map_ops sock_map_ops = {
581 	.map_alloc		= sock_map_alloc,
582 	.map_free		= sock_map_free,
583 	.map_get_next_key	= sock_map_get_next_key,
584 	.map_lookup_elem_sys_only = sock_map_lookup_sys,
585 	.map_update_elem	= sock_map_update_elem,
586 	.map_delete_elem	= sock_map_delete_elem,
587 	.map_lookup_elem	= sock_map_lookup,
588 	.map_release_uref	= sock_map_release_progs,
589 	.map_check_btf		= map_check_no_btf,
590 };
591 
592 struct bpf_htab_elem {
593 	struct rcu_head rcu;
594 	u32 hash;
595 	struct sock *sk;
596 	struct hlist_node node;
597 	u8 key[];
598 };
599 
600 struct bpf_htab_bucket {
601 	struct hlist_head head;
602 	raw_spinlock_t lock;
603 };
604 
605 struct bpf_htab {
606 	struct bpf_map map;
607 	struct bpf_htab_bucket *buckets;
608 	u32 buckets_num;
609 	u32 elem_size;
610 	struct sk_psock_progs progs;
611 	atomic_t count;
612 };
613 
614 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
615 {
616 	return jhash(key, len, 0);
617 }
618 
619 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
620 						       u32 hash)
621 {
622 	return &htab->buckets[hash & (htab->buckets_num - 1)];
623 }
624 
625 static struct bpf_htab_elem *
626 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
627 			  u32 key_size)
628 {
629 	struct bpf_htab_elem *elem;
630 
631 	hlist_for_each_entry_rcu(elem, head, node) {
632 		if (elem->hash == hash &&
633 		    !memcmp(&elem->key, key, key_size))
634 			return elem;
635 	}
636 
637 	return NULL;
638 }
639 
640 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
641 {
642 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
643 	u32 key_size = map->key_size, hash;
644 	struct bpf_htab_bucket *bucket;
645 	struct bpf_htab_elem *elem;
646 
647 	WARN_ON_ONCE(!rcu_read_lock_held());
648 
649 	hash = sock_hash_bucket_hash(key, key_size);
650 	bucket = sock_hash_select_bucket(htab, hash);
651 	elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
652 
653 	return elem ? elem->sk : NULL;
654 }
655 
656 static void sock_hash_free_elem(struct bpf_htab *htab,
657 				struct bpf_htab_elem *elem)
658 {
659 	atomic_dec(&htab->count);
660 	kfree_rcu(elem, rcu);
661 }
662 
663 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
664 				       void *link_raw)
665 {
666 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
667 	struct bpf_htab_elem *elem_probe, *elem = link_raw;
668 	struct bpf_htab_bucket *bucket;
669 
670 	WARN_ON_ONCE(!rcu_read_lock_held());
671 	bucket = sock_hash_select_bucket(htab, elem->hash);
672 
673 	/* elem may be deleted in parallel from the map, but access here
674 	 * is okay since it's going away only after RCU grace period.
675 	 * However, we need to check whether it's still present.
676 	 */
677 	raw_spin_lock_bh(&bucket->lock);
678 	elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
679 					       elem->key, map->key_size);
680 	if (elem_probe && elem_probe == elem) {
681 		hlist_del_rcu(&elem->node);
682 		sock_map_unref(elem->sk, elem);
683 		sock_hash_free_elem(htab, elem);
684 	}
685 	raw_spin_unlock_bh(&bucket->lock);
686 }
687 
688 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
689 {
690 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
691 	u32 hash, key_size = map->key_size;
692 	struct bpf_htab_bucket *bucket;
693 	struct bpf_htab_elem *elem;
694 	int ret = -ENOENT;
695 
696 	hash = sock_hash_bucket_hash(key, key_size);
697 	bucket = sock_hash_select_bucket(htab, hash);
698 
699 	raw_spin_lock_bh(&bucket->lock);
700 	elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
701 	if (elem) {
702 		hlist_del_rcu(&elem->node);
703 		sock_map_unref(elem->sk, elem);
704 		sock_hash_free_elem(htab, elem);
705 		ret = 0;
706 	}
707 	raw_spin_unlock_bh(&bucket->lock);
708 	return ret;
709 }
710 
711 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
712 						  void *key, u32 key_size,
713 						  u32 hash, struct sock *sk,
714 						  struct bpf_htab_elem *old)
715 {
716 	struct bpf_htab_elem *new;
717 
718 	if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
719 		if (!old) {
720 			atomic_dec(&htab->count);
721 			return ERR_PTR(-E2BIG);
722 		}
723 	}
724 
725 	new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
726 			   htab->map.numa_node);
727 	if (!new) {
728 		atomic_dec(&htab->count);
729 		return ERR_PTR(-ENOMEM);
730 	}
731 	memcpy(new->key, key, key_size);
732 	new->sk = sk;
733 	new->hash = hash;
734 	return new;
735 }
736 
737 static int sock_hash_update_common(struct bpf_map *map, void *key,
738 				   struct sock *sk, u64 flags)
739 {
740 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
741 	struct inet_connection_sock *icsk = inet_csk(sk);
742 	u32 key_size = map->key_size, hash;
743 	struct bpf_htab_elem *elem, *elem_new;
744 	struct bpf_htab_bucket *bucket;
745 	struct sk_psock_link *link;
746 	struct sk_psock *psock;
747 	int ret;
748 
749 	WARN_ON_ONCE(!rcu_read_lock_held());
750 	if (unlikely(flags > BPF_EXIST))
751 		return -EINVAL;
752 	if (unlikely(icsk->icsk_ulp_data))
753 		return -EINVAL;
754 
755 	link = sk_psock_init_link();
756 	if (!link)
757 		return -ENOMEM;
758 
759 	/* Only sockets we can redirect into/from in BPF need to hold
760 	 * refs to parser/verdict progs and have their sk_data_ready
761 	 * and sk_write_space callbacks overridden.
762 	 */
763 	if (sock_map_redirect_allowed(sk))
764 		ret = sock_map_link(map, &htab->progs, sk);
765 	else
766 		ret = sock_map_link_no_progs(map, sk);
767 	if (ret < 0)
768 		goto out_free;
769 
770 	psock = sk_psock(sk);
771 	WARN_ON_ONCE(!psock);
772 
773 	hash = sock_hash_bucket_hash(key, key_size);
774 	bucket = sock_hash_select_bucket(htab, hash);
775 
776 	raw_spin_lock_bh(&bucket->lock);
777 	elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
778 	if (elem && flags == BPF_NOEXIST) {
779 		ret = -EEXIST;
780 		goto out_unlock;
781 	} else if (!elem && flags == BPF_EXIST) {
782 		ret = -ENOENT;
783 		goto out_unlock;
784 	}
785 
786 	elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
787 	if (IS_ERR(elem_new)) {
788 		ret = PTR_ERR(elem_new);
789 		goto out_unlock;
790 	}
791 
792 	sock_map_add_link(psock, link, map, elem_new);
793 	/* Add new element to the head of the list, so that
794 	 * concurrent search will find it before old elem.
795 	 */
796 	hlist_add_head_rcu(&elem_new->node, &bucket->head);
797 	if (elem) {
798 		hlist_del_rcu(&elem->node);
799 		sock_map_unref(elem->sk, elem);
800 		sock_hash_free_elem(htab, elem);
801 	}
802 	raw_spin_unlock_bh(&bucket->lock);
803 	return 0;
804 out_unlock:
805 	raw_spin_unlock_bh(&bucket->lock);
806 	sk_psock_put(sk, psock);
807 out_free:
808 	sk_psock_free_link(link);
809 	return ret;
810 }
811 
812 static int sock_hash_update_elem(struct bpf_map *map, void *key,
813 				 void *value, u64 flags)
814 {
815 	struct socket *sock;
816 	struct sock *sk;
817 	int ret;
818 	u64 ufd;
819 
820 	if (map->value_size == sizeof(u64))
821 		ufd = *(u64 *)value;
822 	else
823 		ufd = *(u32 *)value;
824 	if (ufd > S32_MAX)
825 		return -EINVAL;
826 
827 	sock = sockfd_lookup(ufd, &ret);
828 	if (!sock)
829 		return ret;
830 	sk = sock->sk;
831 	if (!sk) {
832 		ret = -EINVAL;
833 		goto out;
834 	}
835 	if (!sock_map_sk_is_suitable(sk)) {
836 		ret = -EOPNOTSUPP;
837 		goto out;
838 	}
839 
840 	sock_map_sk_acquire(sk);
841 	if (!sock_map_sk_state_allowed(sk))
842 		ret = -EOPNOTSUPP;
843 	else
844 		ret = sock_hash_update_common(map, key, sk, flags);
845 	sock_map_sk_release(sk);
846 out:
847 	fput(sock->file);
848 	return ret;
849 }
850 
851 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
852 				  void *key_next)
853 {
854 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
855 	struct bpf_htab_elem *elem, *elem_next;
856 	u32 hash, key_size = map->key_size;
857 	struct hlist_head *head;
858 	int i = 0;
859 
860 	if (!key)
861 		goto find_first_elem;
862 	hash = sock_hash_bucket_hash(key, key_size);
863 	head = &sock_hash_select_bucket(htab, hash)->head;
864 	elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
865 	if (!elem)
866 		goto find_first_elem;
867 
868 	elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
869 				     struct bpf_htab_elem, node);
870 	if (elem_next) {
871 		memcpy(key_next, elem_next->key, key_size);
872 		return 0;
873 	}
874 
875 	i = hash & (htab->buckets_num - 1);
876 	i++;
877 find_first_elem:
878 	for (; i < htab->buckets_num; i++) {
879 		head = &sock_hash_select_bucket(htab, i)->head;
880 		elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
881 					     struct bpf_htab_elem, node);
882 		if (elem_next) {
883 			memcpy(key_next, elem_next->key, key_size);
884 			return 0;
885 		}
886 	}
887 
888 	return -ENOENT;
889 }
890 
891 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
892 {
893 	struct bpf_htab *htab;
894 	int i, err;
895 	u64 cost;
896 
897 	if (!capable(CAP_NET_ADMIN))
898 		return ERR_PTR(-EPERM);
899 	if (attr->max_entries == 0 ||
900 	    attr->key_size    == 0 ||
901 	    (attr->value_size != sizeof(u32) &&
902 	     attr->value_size != sizeof(u64)) ||
903 	    attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
904 		return ERR_PTR(-EINVAL);
905 	if (attr->key_size > MAX_BPF_STACK)
906 		return ERR_PTR(-E2BIG);
907 
908 	htab = kzalloc(sizeof(*htab), GFP_USER);
909 	if (!htab)
910 		return ERR_PTR(-ENOMEM);
911 
912 	bpf_map_init_from_attr(&htab->map, attr);
913 
914 	htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
915 	htab->elem_size = sizeof(struct bpf_htab_elem) +
916 			  round_up(htab->map.key_size, 8);
917 	if (htab->buckets_num == 0 ||
918 	    htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
919 		err = -EINVAL;
920 		goto free_htab;
921 	}
922 
923 	cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
924 	       (u64) htab->elem_size * htab->map.max_entries;
925 	if (cost >= U32_MAX - PAGE_SIZE) {
926 		err = -EINVAL;
927 		goto free_htab;
928 	}
929 
930 	htab->buckets = bpf_map_area_alloc(htab->buckets_num *
931 					   sizeof(struct bpf_htab_bucket),
932 					   htab->map.numa_node);
933 	if (!htab->buckets) {
934 		err = -ENOMEM;
935 		goto free_htab;
936 	}
937 
938 	for (i = 0; i < htab->buckets_num; i++) {
939 		INIT_HLIST_HEAD(&htab->buckets[i].head);
940 		raw_spin_lock_init(&htab->buckets[i].lock);
941 	}
942 
943 	return &htab->map;
944 free_htab:
945 	kfree(htab);
946 	return ERR_PTR(err);
947 }
948 
949 static void sock_hash_free(struct bpf_map *map)
950 {
951 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
952 	struct bpf_htab_bucket *bucket;
953 	struct bpf_htab_elem *elem;
954 	struct hlist_node *node;
955 	int i;
956 
957 	synchronize_rcu();
958 	for (i = 0; i < htab->buckets_num; i++) {
959 		bucket = sock_hash_select_bucket(htab, i);
960 		raw_spin_lock_bh(&bucket->lock);
961 		hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
962 			hlist_del_rcu(&elem->node);
963 			lock_sock(elem->sk);
964 			rcu_read_lock();
965 			sock_map_unref(elem->sk, elem);
966 			rcu_read_unlock();
967 			release_sock(elem->sk);
968 		}
969 		raw_spin_unlock_bh(&bucket->lock);
970 	}
971 
972 	/* wait for psock readers accessing its map link */
973 	synchronize_rcu();
974 
975 	bpf_map_area_free(htab->buckets);
976 	kfree(htab);
977 }
978 
979 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
980 {
981 	struct sock *sk;
982 
983 	if (map->value_size != sizeof(u64))
984 		return ERR_PTR(-ENOSPC);
985 
986 	sk = __sock_hash_lookup_elem(map, key);
987 	if (!sk)
988 		return ERR_PTR(-ENOENT);
989 
990 	sock_gen_cookie(sk);
991 	return &sk->sk_cookie;
992 }
993 
994 static void *sock_hash_lookup(struct bpf_map *map, void *key)
995 {
996 	return __sock_hash_lookup_elem(map, key);
997 }
998 
999 static void sock_hash_release_progs(struct bpf_map *map)
1000 {
1001 	psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
1002 }
1003 
1004 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1005 	   struct bpf_map *, map, void *, key, u64, flags)
1006 {
1007 	WARN_ON_ONCE(!rcu_read_lock_held());
1008 
1009 	if (likely(sock_map_sk_is_suitable(sops->sk) &&
1010 		   sock_map_op_okay(sops)))
1011 		return sock_hash_update_common(map, key, sops->sk, flags);
1012 	return -EOPNOTSUPP;
1013 }
1014 
1015 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1016 	.func		= bpf_sock_hash_update,
1017 	.gpl_only	= false,
1018 	.pkt_access	= true,
1019 	.ret_type	= RET_INTEGER,
1020 	.arg1_type	= ARG_PTR_TO_CTX,
1021 	.arg2_type	= ARG_CONST_MAP_PTR,
1022 	.arg3_type	= ARG_PTR_TO_MAP_KEY,
1023 	.arg4_type	= ARG_ANYTHING,
1024 };
1025 
1026 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1027 	   struct bpf_map *, map, void *, key, u64, flags)
1028 {
1029 	struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1030 	struct sock *sk;
1031 
1032 	if (unlikely(flags & ~(BPF_F_INGRESS)))
1033 		return SK_DROP;
1034 
1035 	sk = __sock_hash_lookup_elem(map, key);
1036 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1037 		return SK_DROP;
1038 
1039 	tcb->bpf.flags = flags;
1040 	tcb->bpf.sk_redir = sk;
1041 	return SK_PASS;
1042 }
1043 
1044 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1045 	.func           = bpf_sk_redirect_hash,
1046 	.gpl_only       = false,
1047 	.ret_type       = RET_INTEGER,
1048 	.arg1_type	= ARG_PTR_TO_CTX,
1049 	.arg2_type      = ARG_CONST_MAP_PTR,
1050 	.arg3_type      = ARG_PTR_TO_MAP_KEY,
1051 	.arg4_type      = ARG_ANYTHING,
1052 };
1053 
1054 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1055 	   struct bpf_map *, map, void *, key, u64, flags)
1056 {
1057 	struct sock *sk;
1058 
1059 	if (unlikely(flags & ~(BPF_F_INGRESS)))
1060 		return SK_DROP;
1061 
1062 	sk = __sock_hash_lookup_elem(map, key);
1063 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1064 		return SK_DROP;
1065 
1066 	msg->flags = flags;
1067 	msg->sk_redir = sk;
1068 	return SK_PASS;
1069 }
1070 
1071 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1072 	.func           = bpf_msg_redirect_hash,
1073 	.gpl_only       = false,
1074 	.ret_type       = RET_INTEGER,
1075 	.arg1_type	= ARG_PTR_TO_CTX,
1076 	.arg2_type      = ARG_CONST_MAP_PTR,
1077 	.arg3_type      = ARG_PTR_TO_MAP_KEY,
1078 	.arg4_type      = ARG_ANYTHING,
1079 };
1080 
1081 const struct bpf_map_ops sock_hash_ops = {
1082 	.map_alloc		= sock_hash_alloc,
1083 	.map_free		= sock_hash_free,
1084 	.map_get_next_key	= sock_hash_get_next_key,
1085 	.map_update_elem	= sock_hash_update_elem,
1086 	.map_delete_elem	= sock_hash_delete_elem,
1087 	.map_lookup_elem	= sock_hash_lookup,
1088 	.map_lookup_elem_sys_only = sock_hash_lookup_sys,
1089 	.map_release_uref	= sock_hash_release_progs,
1090 	.map_check_btf		= map_check_no_btf,
1091 };
1092 
1093 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1094 {
1095 	switch (map->map_type) {
1096 	case BPF_MAP_TYPE_SOCKMAP:
1097 		return &container_of(map, struct bpf_stab, map)->progs;
1098 	case BPF_MAP_TYPE_SOCKHASH:
1099 		return &container_of(map, struct bpf_htab, map)->progs;
1100 	default:
1101 		break;
1102 	}
1103 
1104 	return NULL;
1105 }
1106 
1107 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1108 			 u32 which)
1109 {
1110 	struct sk_psock_progs *progs = sock_map_progs(map);
1111 
1112 	if (!progs)
1113 		return -EOPNOTSUPP;
1114 
1115 	switch (which) {
1116 	case BPF_SK_MSG_VERDICT:
1117 		psock_set_prog(&progs->msg_parser, prog);
1118 		break;
1119 	case BPF_SK_SKB_STREAM_PARSER:
1120 		psock_set_prog(&progs->skb_parser, prog);
1121 		break;
1122 	case BPF_SK_SKB_STREAM_VERDICT:
1123 		psock_set_prog(&progs->skb_verdict, prog);
1124 		break;
1125 	default:
1126 		return -EOPNOTSUPP;
1127 	}
1128 
1129 	return 0;
1130 }
1131 
1132 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
1133 {
1134 	switch (link->map->map_type) {
1135 	case BPF_MAP_TYPE_SOCKMAP:
1136 		return sock_map_delete_from_link(link->map, sk,
1137 						 link->link_raw);
1138 	case BPF_MAP_TYPE_SOCKHASH:
1139 		return sock_hash_delete_from_link(link->map, sk,
1140 						  link->link_raw);
1141 	default:
1142 		break;
1143 	}
1144 }
1145