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