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