1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
5 #include <linux/filter.h>
6 #include <linux/errno.h>
7 #include <linux/file.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>
19 struct sk_psock_progs progs;
23 #define SOCK_CREATE_FLAG_MASK \
24 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
26 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
28 struct bpf_stab *stab;
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);
41 stab = kzalloc(sizeof(*stab), GFP_USER);
43 return ERR_PTR(-ENOMEM);
45 bpf_map_init_from_attr(&stab->map, attr);
46 raw_spin_lock_init(&stab->lock);
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);
54 stab->sks = bpf_map_area_alloc(stab->map.max_entries *
55 sizeof(struct sock *),
60 bpf_map_charge_finish(&stab->map.memory);
66 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
68 u32 ufd = attr->target_fd;
73 if (attr->attach_flags || attr->replace_bpf_fd)
77 map = __bpf_map_get(f);
80 ret = sock_map_prog_update(map, prog, NULL, attr->attach_type);
85 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
87 u32 ufd = attr->target_fd;
88 struct bpf_prog *prog;
93 if (attr->attach_flags || attr->replace_bpf_fd)
97 map = __bpf_map_get(f);
101 prog = bpf_prog_get(attr->attach_bpf_fd);
107 if (prog->type != ptype) {
112 ret = sock_map_prog_update(map, NULL, prog, attr->attach_type);
120 static void sock_map_sk_acquire(struct sock *sk)
121 __acquires(&sk->sk_lock.slock)
128 static void sock_map_sk_release(struct sock *sk)
129 __releases(&sk->sk_lock.slock)
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)
140 link->link_raw = link_raw;
142 spin_lock_bh(&psock->link_lock);
143 list_add_tail(&link->list, &psock->link);
144 spin_unlock_bh(&psock->link_lock);
147 static void sock_map_del_link(struct sock *sk,
148 struct sk_psock *psock, void *link_raw)
150 struct sk_psock_link *link, *tmp;
151 bool strp_stop = false;
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,
159 if (psock->parser.enabled && stab->progs.skb_parser)
161 list_del(&link->list);
162 sk_psock_free_link(link);
165 spin_unlock_bh(&psock->link_lock);
167 write_lock_bh(&sk->sk_callback_lock);
168 sk_psock_stop_strp(sk, psock);
169 write_unlock_bh(&sk->sk_callback_lock);
173 static void sock_map_unref(struct sock *sk, void *link_raw)
175 struct sk_psock *psock = sk_psock(sk);
178 sock_map_del_link(sk, psock, link_raw);
179 sk_psock_put(sk, psock);
183 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
187 sock_owned_by_me(sk);
189 switch (sk->sk_type) {
191 prot = tcp_bpf_get_proto(sk, psock);
195 prot = udp_bpf_get_proto(sk, psock);
203 return PTR_ERR(prot);
205 sk_psock_update_proto(sk, psock, prot);
209 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
211 struct sk_psock *psock;
214 psock = sk_psock(sk);
216 if (sk->sk_prot->close != sock_map_close) {
217 psock = ERR_PTR(-EBUSY);
221 if (!refcount_inc_not_zero(&psock->refcnt))
222 psock = ERR_PTR(-EBUSY);
229 static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
232 struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
233 struct sk_psock *psock;
237 skb_verdict = READ_ONCE(progs->skb_verdict);
238 skb_parser = READ_ONCE(progs->skb_parser);
239 skb_progs = skb_parser && skb_verdict;
241 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
242 if (IS_ERR(skb_verdict))
243 return PTR_ERR(skb_verdict);
244 skb_parser = bpf_prog_inc_not_zero(skb_parser);
245 if (IS_ERR(skb_parser)) {
246 bpf_prog_put(skb_verdict);
247 return PTR_ERR(skb_parser);
251 msg_parser = READ_ONCE(progs->msg_parser);
253 msg_parser = bpf_prog_inc_not_zero(msg_parser);
254 if (IS_ERR(msg_parser)) {
255 ret = PTR_ERR(msg_parser);
260 psock = sock_map_psock_get_checked(sk);
262 ret = PTR_ERR(psock);
267 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
268 (skb_progs && READ_ONCE(psock->progs.skb_parser))) {
269 sk_psock_put(sk, psock);
274 psock = sk_psock_init(sk, map->numa_node);
282 psock_set_prog(&psock->progs.msg_parser, msg_parser);
284 ret = sock_map_init_proto(sk, psock);
288 write_lock_bh(&sk->sk_callback_lock);
289 if (skb_progs && !psock->parser.enabled) {
290 ret = sk_psock_init_strp(sk, psock);
292 write_unlock_bh(&sk->sk_callback_lock);
295 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
296 psock_set_prog(&psock->progs.skb_parser, skb_parser);
297 sk_psock_start_strp(sk, psock);
299 write_unlock_bh(&sk->sk_callback_lock);
302 sk_psock_put(sk, psock);
305 bpf_prog_put(msg_parser);
308 bpf_prog_put(skb_verdict);
309 bpf_prog_put(skb_parser);
314 static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)
316 struct sk_psock *psock;
319 psock = sock_map_psock_get_checked(sk);
321 return PTR_ERR(psock);
324 psock = sk_psock_init(sk, map->numa_node);
329 ret = sock_map_init_proto(sk, psock);
331 sk_psock_put(sk, psock);
335 static void sock_map_free(struct bpf_map *map)
337 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
340 /* After the sync no updates or deletes will be in-flight so it
341 * is safe to walk map and remove entries without risking a race
342 * in EEXIST update case.
345 for (i = 0; i < stab->map.max_entries; i++) {
346 struct sock **psk = &stab->sks[i];
349 sk = xchg(psk, NULL);
353 sock_map_unref(sk, psk);
359 /* wait for psock readers accessing its map link */
362 bpf_map_area_free(stab->sks);
366 static void sock_map_release_progs(struct bpf_map *map)
368 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
371 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
373 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
375 WARN_ON_ONCE(!rcu_read_lock_held());
377 if (unlikely(key >= map->max_entries))
379 return READ_ONCE(stab->sks[key]);
382 static void *sock_map_lookup(struct bpf_map *map, void *key)
386 sk = __sock_map_lookup_elem(map, *(u32 *)key);
387 if (!sk || !sk_fullsock(sk))
389 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
394 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
398 if (map->value_size != sizeof(u64))
399 return ERR_PTR(-ENOSPC);
401 sk = __sock_map_lookup_elem(map, *(u32 *)key);
403 return ERR_PTR(-ENOENT);
406 return &sk->sk_cookie;
409 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
415 raw_spin_lock_bh(&stab->lock);
417 if (!sk_test || sk_test == sk)
418 sk = xchg(psk, NULL);
421 sock_map_unref(sk, psk);
425 raw_spin_unlock_bh(&stab->lock);
429 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
432 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
434 __sock_map_delete(stab, sk, link_raw);
437 static int sock_map_delete_elem(struct bpf_map *map, void *key)
439 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
443 if (unlikely(i >= map->max_entries))
447 return __sock_map_delete(stab, NULL, psk);
450 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
452 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
453 u32 i = key ? *(u32 *)key : U32_MAX;
454 u32 *key_next = next;
456 if (i == stab->map.max_entries - 1)
458 if (i >= stab->map.max_entries)
465 static bool sock_map_redirect_allowed(const struct sock *sk);
467 static int sock_map_update_common(struct bpf_map *map, u32 idx,
468 struct sock *sk, u64 flags)
470 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
471 struct sk_psock_link *link;
472 struct sk_psock *psock;
476 WARN_ON_ONCE(!rcu_read_lock_held());
477 if (unlikely(flags > BPF_EXIST))
479 if (unlikely(idx >= map->max_entries))
481 if (inet_csk_has_ulp(sk))
484 link = sk_psock_init_link();
488 /* Only sockets we can redirect into/from in BPF need to hold
489 * refs to parser/verdict progs and have their sk_data_ready
490 * and sk_write_space callbacks overridden.
492 if (sock_map_redirect_allowed(sk))
493 ret = sock_map_link(map, &stab->progs, sk);
495 ret = sock_map_link_no_progs(map, sk);
499 psock = sk_psock(sk);
500 WARN_ON_ONCE(!psock);
502 raw_spin_lock_bh(&stab->lock);
503 osk = stab->sks[idx];
504 if (osk && flags == BPF_NOEXIST) {
507 } else if (!osk && flags == BPF_EXIST) {
512 sock_map_add_link(psock, link, map, &stab->sks[idx]);
515 sock_map_unref(osk, &stab->sks[idx]);
516 raw_spin_unlock_bh(&stab->lock);
519 raw_spin_unlock_bh(&stab->lock);
521 sk_psock_put(sk, psock);
523 sk_psock_free_link(link);
527 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
529 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
530 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
531 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
534 static bool sk_is_tcp(const struct sock *sk)
536 return sk->sk_type == SOCK_STREAM &&
537 sk->sk_protocol == IPPROTO_TCP;
540 static bool sk_is_udp(const struct sock *sk)
542 return sk->sk_type == SOCK_DGRAM &&
543 sk->sk_protocol == IPPROTO_UDP;
546 static bool sock_map_redirect_allowed(const struct sock *sk)
548 return sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN;
551 static bool sock_map_sk_is_suitable(const struct sock *sk)
553 return sk_is_tcp(sk) || sk_is_udp(sk);
556 static bool sock_map_sk_state_allowed(const struct sock *sk)
559 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
560 else if (sk_is_udp(sk))
561 return sk_hashed(sk);
566 static int sock_map_update_elem(struct bpf_map *map, void *key,
567 void *value, u64 flags)
569 u32 idx = *(u32 *)key;
575 if (map->value_size == sizeof(u64))
582 sock = sockfd_lookup(ufd, &ret);
590 if (!sock_map_sk_is_suitable(sk)) {
595 sock_map_sk_acquire(sk);
596 if (!sock_map_sk_state_allowed(sk))
599 ret = sock_map_update_common(map, idx, sk, flags);
600 sock_map_sk_release(sk);
606 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
607 struct bpf_map *, map, void *, key, u64, flags)
609 WARN_ON_ONCE(!rcu_read_lock_held());
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,
618 const struct bpf_func_proto bpf_sock_map_update_proto = {
619 .func = bpf_sock_map_update,
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,
629 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
630 struct bpf_map *, map, u32, key, u64, flags)
632 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
635 if (unlikely(flags & ~(BPF_F_INGRESS)))
638 sk = __sock_map_lookup_elem(map, key);
639 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
642 tcb->bpf.flags = flags;
643 tcb->bpf.sk_redir = sk;
647 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
648 .func = bpf_sk_redirect_map,
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,
657 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
658 struct bpf_map *, map, u32, key, u64, flags)
662 if (unlikely(flags & ~(BPF_F_INGRESS)))
665 sk = __sock_map_lookup_elem(map, key);
666 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
674 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
675 .func = bpf_msg_redirect_map,
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,
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,
699 struct bpf_shtab_elem {
703 struct hlist_node node;
707 struct bpf_shtab_bucket {
708 struct hlist_head head;
714 struct bpf_shtab_bucket *buckets;
717 struct sk_psock_progs progs;
721 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
723 return jhash(key, len, 0);
726 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
729 return &htab->buckets[hash & (htab->buckets_num - 1)];
732 static struct bpf_shtab_elem *
733 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
736 struct bpf_shtab_elem *elem;
738 hlist_for_each_entry_rcu(elem, head, node) {
739 if (elem->hash == hash &&
740 !memcmp(&elem->key, key, key_size))
747 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
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;
754 WARN_ON_ONCE(!rcu_read_lock_held());
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);
760 return elem ? elem->sk : NULL;
763 static void sock_hash_free_elem(struct bpf_shtab *htab,
764 struct bpf_shtab_elem *elem)
766 atomic_dec(&htab->count);
767 kfree_rcu(elem, rcu);
770 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
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;
777 WARN_ON_ONCE(!rcu_read_lock_held());
778 bucket = sock_hash_select_bucket(htab, elem->hash);
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.
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);
792 raw_spin_unlock_bh(&bucket->lock);
795 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
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;
803 hash = sock_hash_bucket_hash(key, key_size);
804 bucket = sock_hash_select_bucket(htab, hash);
806 raw_spin_lock_bh(&bucket->lock);
807 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
809 hlist_del_rcu(&elem->node);
810 sock_map_unref(elem->sk, elem);
811 sock_hash_free_elem(htab, elem);
814 raw_spin_unlock_bh(&bucket->lock);
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)
823 struct bpf_shtab_elem *new;
825 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
827 atomic_dec(&htab->count);
828 return ERR_PTR(-E2BIG);
832 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
833 htab->map.numa_node);
835 atomic_dec(&htab->count);
836 return ERR_PTR(-ENOMEM);
838 memcpy(new->key, key, key_size);
844 static int sock_hash_update_common(struct bpf_map *map, void *key,
845 struct sock *sk, u64 flags)
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;
855 WARN_ON_ONCE(!rcu_read_lock_held());
856 if (unlikely(flags > BPF_EXIST))
858 if (inet_csk_has_ulp(sk))
861 link = sk_psock_init_link();
865 /* Only sockets we can redirect into/from in BPF need to hold
866 * refs to parser/verdict progs and have their sk_data_ready
867 * and sk_write_space callbacks overridden.
869 if (sock_map_redirect_allowed(sk))
870 ret = sock_map_link(map, &htab->progs, sk);
872 ret = sock_map_link_no_progs(map, sk);
876 psock = sk_psock(sk);
877 WARN_ON_ONCE(!psock);
879 hash = sock_hash_bucket_hash(key, key_size);
880 bucket = sock_hash_select_bucket(htab, hash);
882 raw_spin_lock_bh(&bucket->lock);
883 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
884 if (elem && flags == BPF_NOEXIST) {
887 } else if (!elem && flags == BPF_EXIST) {
892 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
893 if (IS_ERR(elem_new)) {
894 ret = PTR_ERR(elem_new);
898 sock_map_add_link(psock, link, map, elem_new);
899 /* Add new element to the head of the list, so that
900 * concurrent search will find it before old elem.
902 hlist_add_head_rcu(&elem_new->node, &bucket->head);
904 hlist_del_rcu(&elem->node);
905 sock_map_unref(elem->sk, elem);
906 sock_hash_free_elem(htab, elem);
908 raw_spin_unlock_bh(&bucket->lock);
911 raw_spin_unlock_bh(&bucket->lock);
912 sk_psock_put(sk, psock);
914 sk_psock_free_link(link);
918 static int sock_hash_update_elem(struct bpf_map *map, void *key,
919 void *value, u64 flags)
926 if (map->value_size == sizeof(u64))
933 sock = sockfd_lookup(ufd, &ret);
941 if (!sock_map_sk_is_suitable(sk)) {
946 sock_map_sk_acquire(sk);
947 if (!sock_map_sk_state_allowed(sk))
950 ret = sock_hash_update_common(map, key, sk, flags);
951 sock_map_sk_release(sk);
957 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
960 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
961 struct bpf_shtab_elem *elem, *elem_next;
962 u32 hash, key_size = map->key_size;
963 struct hlist_head *head;
967 goto find_first_elem;
968 hash = sock_hash_bucket_hash(key, key_size);
969 head = &sock_hash_select_bucket(htab, hash)->head;
970 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
972 goto find_first_elem;
974 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
975 struct bpf_shtab_elem, node);
977 memcpy(key_next, elem_next->key, key_size);
981 i = hash & (htab->buckets_num - 1);
984 for (; i < htab->buckets_num; i++) {
985 head = &sock_hash_select_bucket(htab, i)->head;
986 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
987 struct bpf_shtab_elem, node);
989 memcpy(key_next, elem_next->key, key_size);
997 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
999 struct bpf_shtab *htab;
1003 if (!capable(CAP_NET_ADMIN))
1004 return ERR_PTR(-EPERM);
1005 if (attr->max_entries == 0 ||
1006 attr->key_size == 0 ||
1007 (attr->value_size != sizeof(u32) &&
1008 attr->value_size != sizeof(u64)) ||
1009 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1010 return ERR_PTR(-EINVAL);
1011 if (attr->key_size > MAX_BPF_STACK)
1012 return ERR_PTR(-E2BIG);
1014 htab = kzalloc(sizeof(*htab), GFP_USER);
1016 return ERR_PTR(-ENOMEM);
1018 bpf_map_init_from_attr(&htab->map, attr);
1020 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1021 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1022 round_up(htab->map.key_size, 8);
1023 if (htab->buckets_num == 0 ||
1024 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1029 cost = (u64) htab->buckets_num * sizeof(struct bpf_shtab_bucket) +
1030 (u64) htab->elem_size * htab->map.max_entries;
1031 if (cost >= U32_MAX - PAGE_SIZE) {
1035 err = bpf_map_charge_init(&htab->map.memory, cost);
1039 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1040 sizeof(struct bpf_shtab_bucket),
1041 htab->map.numa_node);
1042 if (!htab->buckets) {
1043 bpf_map_charge_finish(&htab->map.memory);
1048 for (i = 0; i < htab->buckets_num; i++) {
1049 INIT_HLIST_HEAD(&htab->buckets[i].head);
1050 raw_spin_lock_init(&htab->buckets[i].lock);
1056 return ERR_PTR(err);
1059 static void sock_hash_free(struct bpf_map *map)
1061 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1062 struct bpf_shtab_bucket *bucket;
1063 struct hlist_head unlink_list;
1064 struct bpf_shtab_elem *elem;
1065 struct hlist_node *node;
1068 /* After the sync no updates or deletes will be in-flight so it
1069 * is safe to walk map and remove entries without risking a race
1070 * in EEXIST update case.
1073 for (i = 0; i < htab->buckets_num; i++) {
1074 bucket = sock_hash_select_bucket(htab, i);
1076 /* We are racing with sock_hash_delete_from_link to
1077 * enter the spin-lock critical section. Every socket on
1078 * the list is still linked to sockhash. Since link
1079 * exists, psock exists and holds a ref to socket. That
1080 * lets us to grab a socket ref too.
1082 raw_spin_lock_bh(&bucket->lock);
1083 hlist_for_each_entry(elem, &bucket->head, node)
1084 sock_hold(elem->sk);
1085 hlist_move_list(&bucket->head, &unlink_list);
1086 raw_spin_unlock_bh(&bucket->lock);
1088 /* Process removed entries out of atomic context to
1089 * block for socket lock before deleting the psock's
1092 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1093 hlist_del(&elem->node);
1094 lock_sock(elem->sk);
1096 sock_map_unref(elem->sk, elem);
1098 release_sock(elem->sk);
1100 sock_hash_free_elem(htab, elem);
1104 /* wait for psock readers accessing its map link */
1107 bpf_map_area_free(htab->buckets);
1111 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1115 if (map->value_size != sizeof(u64))
1116 return ERR_PTR(-ENOSPC);
1118 sk = __sock_hash_lookup_elem(map, key);
1120 return ERR_PTR(-ENOENT);
1122 sock_gen_cookie(sk);
1123 return &sk->sk_cookie;
1126 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1130 sk = __sock_hash_lookup_elem(map, key);
1131 if (!sk || !sk_fullsock(sk))
1133 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1138 static void sock_hash_release_progs(struct bpf_map *map)
1140 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1143 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1144 struct bpf_map *, map, void *, key, u64, flags)
1146 WARN_ON_ONCE(!rcu_read_lock_held());
1148 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1149 sock_map_op_okay(sops)))
1150 return sock_hash_update_common(map, key, sops->sk, flags);
1154 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1155 .func = bpf_sock_hash_update,
1158 .ret_type = RET_INTEGER,
1159 .arg1_type = ARG_PTR_TO_CTX,
1160 .arg2_type = ARG_CONST_MAP_PTR,
1161 .arg3_type = ARG_PTR_TO_MAP_KEY,
1162 .arg4_type = ARG_ANYTHING,
1165 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1166 struct bpf_map *, map, void *, key, u64, flags)
1168 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1171 if (unlikely(flags & ~(BPF_F_INGRESS)))
1174 sk = __sock_hash_lookup_elem(map, key);
1175 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1178 tcb->bpf.flags = flags;
1179 tcb->bpf.sk_redir = sk;
1183 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1184 .func = bpf_sk_redirect_hash,
1186 .ret_type = RET_INTEGER,
1187 .arg1_type = ARG_PTR_TO_CTX,
1188 .arg2_type = ARG_CONST_MAP_PTR,
1189 .arg3_type = ARG_PTR_TO_MAP_KEY,
1190 .arg4_type = ARG_ANYTHING,
1193 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1194 struct bpf_map *, map, void *, key, u64, flags)
1198 if (unlikely(flags & ~(BPF_F_INGRESS)))
1201 sk = __sock_hash_lookup_elem(map, key);
1202 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1210 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1211 .func = bpf_msg_redirect_hash,
1213 .ret_type = RET_INTEGER,
1214 .arg1_type = ARG_PTR_TO_CTX,
1215 .arg2_type = ARG_CONST_MAP_PTR,
1216 .arg3_type = ARG_PTR_TO_MAP_KEY,
1217 .arg4_type = ARG_ANYTHING,
1220 static int sock_hash_map_btf_id;
1221 const struct bpf_map_ops sock_hash_ops = {
1222 .map_alloc = sock_hash_alloc,
1223 .map_free = sock_hash_free,
1224 .map_get_next_key = sock_hash_get_next_key,
1225 .map_update_elem = sock_hash_update_elem,
1226 .map_delete_elem = sock_hash_delete_elem,
1227 .map_lookup_elem = sock_hash_lookup,
1228 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1229 .map_release_uref = sock_hash_release_progs,
1230 .map_check_btf = map_check_no_btf,
1231 .map_btf_name = "bpf_shtab",
1232 .map_btf_id = &sock_hash_map_btf_id,
1235 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1237 switch (map->map_type) {
1238 case BPF_MAP_TYPE_SOCKMAP:
1239 return &container_of(map, struct bpf_stab, map)->progs;
1240 case BPF_MAP_TYPE_SOCKHASH:
1241 return &container_of(map, struct bpf_shtab, map)->progs;
1249 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1250 struct bpf_prog *old, u32 which)
1252 struct sk_psock_progs *progs = sock_map_progs(map);
1253 struct bpf_prog **pprog;
1259 case BPF_SK_MSG_VERDICT:
1260 pprog = &progs->msg_parser;
1262 case BPF_SK_SKB_STREAM_PARSER:
1263 pprog = &progs->skb_parser;
1265 case BPF_SK_SKB_STREAM_VERDICT:
1266 pprog = &progs->skb_verdict;
1273 return psock_replace_prog(pprog, prog, old);
1275 psock_set_prog(pprog, prog);
1279 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1281 switch (link->map->map_type) {
1282 case BPF_MAP_TYPE_SOCKMAP:
1283 return sock_map_delete_from_link(link->map, sk,
1285 case BPF_MAP_TYPE_SOCKHASH:
1286 return sock_hash_delete_from_link(link->map, sk,
1293 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1295 struct sk_psock_link *link;
1297 while ((link = sk_psock_link_pop(psock))) {
1298 sock_map_unlink(sk, link);
1299 sk_psock_free_link(link);
1303 void sock_map_unhash(struct sock *sk)
1305 void (*saved_unhash)(struct sock *sk);
1306 struct sk_psock *psock;
1309 psock = sk_psock(sk);
1310 if (unlikely(!psock)) {
1312 if (sk->sk_prot->unhash)
1313 sk->sk_prot->unhash(sk);
1317 saved_unhash = psock->saved_unhash;
1318 sock_map_remove_links(sk, psock);
1323 void sock_map_close(struct sock *sk, long timeout)
1325 void (*saved_close)(struct sock *sk, long timeout);
1326 struct sk_psock *psock;
1330 psock = sk_psock(sk);
1331 if (unlikely(!psock)) {
1334 return sk->sk_prot->close(sk, timeout);
1337 saved_close = psock->saved_close;
1338 sock_map_remove_links(sk, psock);
1341 saved_close(sk, timeout);