OSDN Git Service

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