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