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