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;
74 map = __bpf_map_get(f);
77 ret = sock_map_prog_update(map, prog, attr->attach_type);
82 static void sock_map_sk_acquire(struct sock *sk)
83 __acquires(&sk->sk_lock.slock)
90 static void sock_map_sk_release(struct sock *sk)
91 __releases(&sk->sk_lock.slock)
98 static void sock_map_add_link(struct sk_psock *psock,
99 struct sk_psock_link *link,
100 struct bpf_map *map, void *link_raw)
102 link->link_raw = link_raw;
104 spin_lock_bh(&psock->link_lock);
105 list_add_tail(&link->list, &psock->link);
106 spin_unlock_bh(&psock->link_lock);
109 static void sock_map_del_link(struct sock *sk,
110 struct sk_psock *psock, void *link_raw)
112 struct sk_psock_link *link, *tmp;
113 bool strp_stop = false;
115 spin_lock_bh(&psock->link_lock);
116 list_for_each_entry_safe(link, tmp, &psock->link, list) {
117 if (link->link_raw == link_raw) {
118 struct bpf_map *map = link->map;
119 struct bpf_stab *stab = container_of(map, struct bpf_stab,
121 if (psock->parser.enabled && stab->progs.skb_parser)
123 list_del(&link->list);
124 sk_psock_free_link(link);
127 spin_unlock_bh(&psock->link_lock);
129 write_lock_bh(&sk->sk_callback_lock);
130 sk_psock_stop_strp(sk, psock);
131 write_unlock_bh(&sk->sk_callback_lock);
135 static void sock_map_unref(struct sock *sk, void *link_raw)
137 struct sk_psock *psock = sk_psock(sk);
140 sock_map_del_link(sk, psock, link_raw);
141 sk_psock_put(sk, psock);
145 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
149 sock_owned_by_me(sk);
151 switch (sk->sk_type) {
153 prot = tcp_bpf_get_proto(sk, psock);
157 prot = udp_bpf_get_proto(sk, psock);
165 return PTR_ERR(prot);
167 sk_psock_update_proto(sk, psock, prot);
171 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
173 struct sk_psock *psock;
176 psock = sk_psock(sk);
178 if (sk->sk_prot->close != sock_map_close) {
179 psock = ERR_PTR(-EBUSY);
183 if (!refcount_inc_not_zero(&psock->refcnt))
184 psock = ERR_PTR(-EBUSY);
191 static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
194 struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
195 struct sk_psock *psock;
199 skb_verdict = READ_ONCE(progs->skb_verdict);
200 skb_parser = READ_ONCE(progs->skb_parser);
201 skb_progs = skb_parser && skb_verdict;
203 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
204 if (IS_ERR(skb_verdict))
205 return PTR_ERR(skb_verdict);
206 skb_parser = bpf_prog_inc_not_zero(skb_parser);
207 if (IS_ERR(skb_parser)) {
208 bpf_prog_put(skb_verdict);
209 return PTR_ERR(skb_parser);
213 msg_parser = READ_ONCE(progs->msg_parser);
215 msg_parser = bpf_prog_inc_not_zero(msg_parser);
216 if (IS_ERR(msg_parser)) {
217 ret = PTR_ERR(msg_parser);
222 psock = sock_map_psock_get_checked(sk);
224 ret = PTR_ERR(psock);
229 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
230 (skb_progs && READ_ONCE(psock->progs.skb_parser))) {
231 sk_psock_put(sk, psock);
236 psock = sk_psock_init(sk, map->numa_node);
244 psock_set_prog(&psock->progs.msg_parser, msg_parser);
246 ret = sock_map_init_proto(sk, psock);
250 write_lock_bh(&sk->sk_callback_lock);
251 if (skb_progs && !psock->parser.enabled) {
252 ret = sk_psock_init_strp(sk, psock);
254 write_unlock_bh(&sk->sk_callback_lock);
257 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
258 psock_set_prog(&psock->progs.skb_parser, skb_parser);
259 sk_psock_start_strp(sk, psock);
261 write_unlock_bh(&sk->sk_callback_lock);
264 sk_psock_put(sk, psock);
267 bpf_prog_put(msg_parser);
270 bpf_prog_put(skb_verdict);
271 bpf_prog_put(skb_parser);
276 static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)
278 struct sk_psock *psock;
281 psock = sock_map_psock_get_checked(sk);
283 return PTR_ERR(psock);
286 psock = sk_psock_init(sk, map->numa_node);
291 ret = sock_map_init_proto(sk, psock);
293 sk_psock_put(sk, psock);
297 static void sock_map_free(struct bpf_map *map)
299 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
302 /* After the sync no updates or deletes will be in-flight so it
303 * is safe to walk map and remove entries without risking a race
304 * in EEXIST update case.
307 for (i = 0; i < stab->map.max_entries; i++) {
308 struct sock **psk = &stab->sks[i];
311 sk = xchg(psk, NULL);
315 sock_map_unref(sk, psk);
321 /* wait for psock readers accessing its map link */
324 bpf_map_area_free(stab->sks);
328 static void sock_map_release_progs(struct bpf_map *map)
330 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
333 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
335 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
337 WARN_ON_ONCE(!rcu_read_lock_held());
339 if (unlikely(key >= map->max_entries))
341 return READ_ONCE(stab->sks[key]);
344 static void *sock_map_lookup(struct bpf_map *map, void *key)
348 sk = __sock_map_lookup_elem(map, *(u32 *)key);
349 if (!sk || !sk_fullsock(sk))
351 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
356 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
360 if (map->value_size != sizeof(u64))
361 return ERR_PTR(-ENOSPC);
363 sk = __sock_map_lookup_elem(map, *(u32 *)key);
365 return ERR_PTR(-ENOENT);
368 return &sk->sk_cookie;
371 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
377 raw_spin_lock_bh(&stab->lock);
379 if (!sk_test || sk_test == sk)
380 sk = xchg(psk, NULL);
383 sock_map_unref(sk, psk);
387 raw_spin_unlock_bh(&stab->lock);
391 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
394 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
396 __sock_map_delete(stab, sk, link_raw);
399 static int sock_map_delete_elem(struct bpf_map *map, void *key)
401 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
405 if (unlikely(i >= map->max_entries))
409 return __sock_map_delete(stab, NULL, psk);
412 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
414 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
415 u32 i = key ? *(u32 *)key : U32_MAX;
416 u32 *key_next = next;
418 if (i == stab->map.max_entries - 1)
420 if (i >= stab->map.max_entries)
427 static bool sock_map_redirect_allowed(const struct sock *sk);
429 static int sock_map_update_common(struct bpf_map *map, u32 idx,
430 struct sock *sk, u64 flags)
432 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
433 struct sk_psock_link *link;
434 struct sk_psock *psock;
438 WARN_ON_ONCE(!rcu_read_lock_held());
439 if (unlikely(flags > BPF_EXIST))
441 if (unlikely(idx >= map->max_entries))
443 if (inet_csk_has_ulp(sk))
446 link = sk_psock_init_link();
450 /* Only sockets we can redirect into/from in BPF need to hold
451 * refs to parser/verdict progs and have their sk_data_ready
452 * and sk_write_space callbacks overridden.
454 if (sock_map_redirect_allowed(sk))
455 ret = sock_map_link(map, &stab->progs, sk);
457 ret = sock_map_link_no_progs(map, sk);
461 psock = sk_psock(sk);
462 WARN_ON_ONCE(!psock);
464 raw_spin_lock_bh(&stab->lock);
465 osk = stab->sks[idx];
466 if (osk && flags == BPF_NOEXIST) {
469 } else if (!osk && flags == BPF_EXIST) {
474 sock_map_add_link(psock, link, map, &stab->sks[idx]);
477 sock_map_unref(osk, &stab->sks[idx]);
478 raw_spin_unlock_bh(&stab->lock);
481 raw_spin_unlock_bh(&stab->lock);
483 sk_psock_put(sk, psock);
485 sk_psock_free_link(link);
489 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
491 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
492 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
493 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
496 static bool sk_is_tcp(const struct sock *sk)
498 return sk->sk_type == SOCK_STREAM &&
499 sk->sk_protocol == IPPROTO_TCP;
502 static bool sk_is_udp(const struct sock *sk)
504 return sk->sk_type == SOCK_DGRAM &&
505 sk->sk_protocol == IPPROTO_UDP;
508 static bool sock_map_redirect_allowed(const struct sock *sk)
510 return sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN;
513 static bool sock_map_sk_is_suitable(const struct sock *sk)
515 return sk_is_tcp(sk) || sk_is_udp(sk);
518 static bool sock_map_sk_state_allowed(const struct sock *sk)
521 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
522 else if (sk_is_udp(sk))
523 return sk_hashed(sk);
528 static int sock_map_update_elem(struct bpf_map *map, void *key,
529 void *value, u64 flags)
531 u32 idx = *(u32 *)key;
537 if (map->value_size == sizeof(u64))
544 sock = sockfd_lookup(ufd, &ret);
552 if (!sock_map_sk_is_suitable(sk)) {
557 sock_map_sk_acquire(sk);
558 if (!sock_map_sk_state_allowed(sk))
561 ret = sock_map_update_common(map, idx, sk, flags);
562 sock_map_sk_release(sk);
568 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
569 struct bpf_map *, map, void *, key, u64, flags)
571 WARN_ON_ONCE(!rcu_read_lock_held());
573 if (likely(sock_map_sk_is_suitable(sops->sk) &&
574 sock_map_op_okay(sops)))
575 return sock_map_update_common(map, *(u32 *)key, sops->sk,
580 const struct bpf_func_proto bpf_sock_map_update_proto = {
581 .func = bpf_sock_map_update,
584 .ret_type = RET_INTEGER,
585 .arg1_type = ARG_PTR_TO_CTX,
586 .arg2_type = ARG_CONST_MAP_PTR,
587 .arg3_type = ARG_PTR_TO_MAP_KEY,
588 .arg4_type = ARG_ANYTHING,
591 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
592 struct bpf_map *, map, u32, key, u64, flags)
594 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
597 if (unlikely(flags & ~(BPF_F_INGRESS)))
600 sk = __sock_map_lookup_elem(map, key);
601 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
604 tcb->bpf.flags = flags;
605 tcb->bpf.sk_redir = sk;
609 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
610 .func = bpf_sk_redirect_map,
612 .ret_type = RET_INTEGER,
613 .arg1_type = ARG_PTR_TO_CTX,
614 .arg2_type = ARG_CONST_MAP_PTR,
615 .arg3_type = ARG_ANYTHING,
616 .arg4_type = ARG_ANYTHING,
619 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
620 struct bpf_map *, map, u32, key, u64, flags)
624 if (unlikely(flags & ~(BPF_F_INGRESS)))
627 sk = __sock_map_lookup_elem(map, key);
628 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
636 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
637 .func = bpf_msg_redirect_map,
639 .ret_type = RET_INTEGER,
640 .arg1_type = ARG_PTR_TO_CTX,
641 .arg2_type = ARG_CONST_MAP_PTR,
642 .arg3_type = ARG_ANYTHING,
643 .arg4_type = ARG_ANYTHING,
646 static int sock_map_btf_id;
647 const struct bpf_map_ops sock_map_ops = {
648 .map_alloc = sock_map_alloc,
649 .map_free = sock_map_free,
650 .map_get_next_key = sock_map_get_next_key,
651 .map_lookup_elem_sys_only = sock_map_lookup_sys,
652 .map_update_elem = sock_map_update_elem,
653 .map_delete_elem = sock_map_delete_elem,
654 .map_lookup_elem = sock_map_lookup,
655 .map_release_uref = sock_map_release_progs,
656 .map_check_btf = map_check_no_btf,
657 .map_btf_name = "bpf_stab",
658 .map_btf_id = &sock_map_btf_id,
661 struct bpf_shtab_elem {
665 struct hlist_node node;
669 struct bpf_shtab_bucket {
670 struct hlist_head head;
676 struct bpf_shtab_bucket *buckets;
679 struct sk_psock_progs progs;
683 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
685 return jhash(key, len, 0);
688 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
691 return &htab->buckets[hash & (htab->buckets_num - 1)];
694 static struct bpf_shtab_elem *
695 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
698 struct bpf_shtab_elem *elem;
700 hlist_for_each_entry_rcu(elem, head, node) {
701 if (elem->hash == hash &&
702 !memcmp(&elem->key, key, key_size))
709 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
711 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
712 u32 key_size = map->key_size, hash;
713 struct bpf_shtab_bucket *bucket;
714 struct bpf_shtab_elem *elem;
716 WARN_ON_ONCE(!rcu_read_lock_held());
718 hash = sock_hash_bucket_hash(key, key_size);
719 bucket = sock_hash_select_bucket(htab, hash);
720 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
722 return elem ? elem->sk : NULL;
725 static void sock_hash_free_elem(struct bpf_shtab *htab,
726 struct bpf_shtab_elem *elem)
728 atomic_dec(&htab->count);
729 kfree_rcu(elem, rcu);
732 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
735 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
736 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
737 struct bpf_shtab_bucket *bucket;
739 WARN_ON_ONCE(!rcu_read_lock_held());
740 bucket = sock_hash_select_bucket(htab, elem->hash);
742 /* elem may be deleted in parallel from the map, but access here
743 * is okay since it's going away only after RCU grace period.
744 * However, we need to check whether it's still present.
746 raw_spin_lock_bh(&bucket->lock);
747 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
748 elem->key, map->key_size);
749 if (elem_probe && elem_probe == elem) {
750 hlist_del_rcu(&elem->node);
751 sock_map_unref(elem->sk, elem);
752 sock_hash_free_elem(htab, elem);
754 raw_spin_unlock_bh(&bucket->lock);
757 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
759 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
760 u32 hash, key_size = map->key_size;
761 struct bpf_shtab_bucket *bucket;
762 struct bpf_shtab_elem *elem;
765 hash = sock_hash_bucket_hash(key, key_size);
766 bucket = sock_hash_select_bucket(htab, hash);
768 raw_spin_lock_bh(&bucket->lock);
769 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
771 hlist_del_rcu(&elem->node);
772 sock_map_unref(elem->sk, elem);
773 sock_hash_free_elem(htab, elem);
776 raw_spin_unlock_bh(&bucket->lock);
780 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
781 void *key, u32 key_size,
782 u32 hash, struct sock *sk,
783 struct bpf_shtab_elem *old)
785 struct bpf_shtab_elem *new;
787 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
789 atomic_dec(&htab->count);
790 return ERR_PTR(-E2BIG);
794 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
795 htab->map.numa_node);
797 atomic_dec(&htab->count);
798 return ERR_PTR(-ENOMEM);
800 memcpy(new->key, key, key_size);
806 static int sock_hash_update_common(struct bpf_map *map, void *key,
807 struct sock *sk, u64 flags)
809 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
810 u32 key_size = map->key_size, hash;
811 struct bpf_shtab_elem *elem, *elem_new;
812 struct bpf_shtab_bucket *bucket;
813 struct sk_psock_link *link;
814 struct sk_psock *psock;
817 WARN_ON_ONCE(!rcu_read_lock_held());
818 if (unlikely(flags > BPF_EXIST))
820 if (inet_csk_has_ulp(sk))
823 link = sk_psock_init_link();
827 /* Only sockets we can redirect into/from in BPF need to hold
828 * refs to parser/verdict progs and have their sk_data_ready
829 * and sk_write_space callbacks overridden.
831 if (sock_map_redirect_allowed(sk))
832 ret = sock_map_link(map, &htab->progs, sk);
834 ret = sock_map_link_no_progs(map, sk);
838 psock = sk_psock(sk);
839 WARN_ON_ONCE(!psock);
841 hash = sock_hash_bucket_hash(key, key_size);
842 bucket = sock_hash_select_bucket(htab, hash);
844 raw_spin_lock_bh(&bucket->lock);
845 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
846 if (elem && flags == BPF_NOEXIST) {
849 } else if (!elem && flags == BPF_EXIST) {
854 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
855 if (IS_ERR(elem_new)) {
856 ret = PTR_ERR(elem_new);
860 sock_map_add_link(psock, link, map, elem_new);
861 /* Add new element to the head of the list, so that
862 * concurrent search will find it before old elem.
864 hlist_add_head_rcu(&elem_new->node, &bucket->head);
866 hlist_del_rcu(&elem->node);
867 sock_map_unref(elem->sk, elem);
868 sock_hash_free_elem(htab, elem);
870 raw_spin_unlock_bh(&bucket->lock);
873 raw_spin_unlock_bh(&bucket->lock);
874 sk_psock_put(sk, psock);
876 sk_psock_free_link(link);
880 static int sock_hash_update_elem(struct bpf_map *map, void *key,
881 void *value, u64 flags)
888 if (map->value_size == sizeof(u64))
895 sock = sockfd_lookup(ufd, &ret);
903 if (!sock_map_sk_is_suitable(sk)) {
908 sock_map_sk_acquire(sk);
909 if (!sock_map_sk_state_allowed(sk))
912 ret = sock_hash_update_common(map, key, sk, flags);
913 sock_map_sk_release(sk);
919 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
922 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
923 struct bpf_shtab_elem *elem, *elem_next;
924 u32 hash, key_size = map->key_size;
925 struct hlist_head *head;
929 goto find_first_elem;
930 hash = sock_hash_bucket_hash(key, key_size);
931 head = &sock_hash_select_bucket(htab, hash)->head;
932 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
934 goto find_first_elem;
936 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
937 struct bpf_shtab_elem, node);
939 memcpy(key_next, elem_next->key, key_size);
943 i = hash & (htab->buckets_num - 1);
946 for (; i < htab->buckets_num; i++) {
947 head = &sock_hash_select_bucket(htab, i)->head;
948 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
949 struct bpf_shtab_elem, node);
951 memcpy(key_next, elem_next->key, key_size);
959 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
961 struct bpf_shtab *htab;
965 if (!capable(CAP_NET_ADMIN))
966 return ERR_PTR(-EPERM);
967 if (attr->max_entries == 0 ||
968 attr->key_size == 0 ||
969 (attr->value_size != sizeof(u32) &&
970 attr->value_size != sizeof(u64)) ||
971 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
972 return ERR_PTR(-EINVAL);
973 if (attr->key_size > MAX_BPF_STACK)
974 return ERR_PTR(-E2BIG);
976 htab = kzalloc(sizeof(*htab), GFP_USER);
978 return ERR_PTR(-ENOMEM);
980 bpf_map_init_from_attr(&htab->map, attr);
982 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
983 htab->elem_size = sizeof(struct bpf_shtab_elem) +
984 round_up(htab->map.key_size, 8);
985 if (htab->buckets_num == 0 ||
986 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
991 cost = (u64) htab->buckets_num * sizeof(struct bpf_shtab_bucket) +
992 (u64) htab->elem_size * htab->map.max_entries;
993 if (cost >= U32_MAX - PAGE_SIZE) {
997 err = bpf_map_charge_init(&htab->map.memory, cost);
1001 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1002 sizeof(struct bpf_shtab_bucket),
1003 htab->map.numa_node);
1004 if (!htab->buckets) {
1005 bpf_map_charge_finish(&htab->map.memory);
1010 for (i = 0; i < htab->buckets_num; i++) {
1011 INIT_HLIST_HEAD(&htab->buckets[i].head);
1012 raw_spin_lock_init(&htab->buckets[i].lock);
1018 return ERR_PTR(err);
1021 static void sock_hash_free(struct bpf_map *map)
1023 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1024 struct bpf_shtab_bucket *bucket;
1025 struct hlist_head unlink_list;
1026 struct bpf_shtab_elem *elem;
1027 struct hlist_node *node;
1030 /* After the sync no updates or deletes will be in-flight so it
1031 * is safe to walk map and remove entries without risking a race
1032 * in EEXIST update case.
1035 for (i = 0; i < htab->buckets_num; i++) {
1036 bucket = sock_hash_select_bucket(htab, i);
1038 /* We are racing with sock_hash_delete_from_link to
1039 * enter the spin-lock critical section. Every socket on
1040 * the list is still linked to sockhash. Since link
1041 * exists, psock exists and holds a ref to socket. That
1042 * lets us to grab a socket ref too.
1044 raw_spin_lock_bh(&bucket->lock);
1045 hlist_for_each_entry(elem, &bucket->head, node)
1046 sock_hold(elem->sk);
1047 hlist_move_list(&bucket->head, &unlink_list);
1048 raw_spin_unlock_bh(&bucket->lock);
1050 /* Process removed entries out of atomic context to
1051 * block for socket lock before deleting the psock's
1054 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1055 hlist_del(&elem->node);
1056 lock_sock(elem->sk);
1058 sock_map_unref(elem->sk, elem);
1060 release_sock(elem->sk);
1062 sock_hash_free_elem(htab, elem);
1066 /* wait for psock readers accessing its map link */
1069 bpf_map_area_free(htab->buckets);
1073 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1077 if (map->value_size != sizeof(u64))
1078 return ERR_PTR(-ENOSPC);
1080 sk = __sock_hash_lookup_elem(map, key);
1082 return ERR_PTR(-ENOENT);
1084 sock_gen_cookie(sk);
1085 return &sk->sk_cookie;
1088 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1092 sk = __sock_hash_lookup_elem(map, key);
1093 if (!sk || !sk_fullsock(sk))
1095 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1100 static void sock_hash_release_progs(struct bpf_map *map)
1102 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1105 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1106 struct bpf_map *, map, void *, key, u64, flags)
1108 WARN_ON_ONCE(!rcu_read_lock_held());
1110 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1111 sock_map_op_okay(sops)))
1112 return sock_hash_update_common(map, key, sops->sk, flags);
1116 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1117 .func = bpf_sock_hash_update,
1120 .ret_type = RET_INTEGER,
1121 .arg1_type = ARG_PTR_TO_CTX,
1122 .arg2_type = ARG_CONST_MAP_PTR,
1123 .arg3_type = ARG_PTR_TO_MAP_KEY,
1124 .arg4_type = ARG_ANYTHING,
1127 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1128 struct bpf_map *, map, void *, key, u64, flags)
1130 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1133 if (unlikely(flags & ~(BPF_F_INGRESS)))
1136 sk = __sock_hash_lookup_elem(map, key);
1137 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1140 tcb->bpf.flags = flags;
1141 tcb->bpf.sk_redir = sk;
1145 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1146 .func = bpf_sk_redirect_hash,
1148 .ret_type = RET_INTEGER,
1149 .arg1_type = ARG_PTR_TO_CTX,
1150 .arg2_type = ARG_CONST_MAP_PTR,
1151 .arg3_type = ARG_PTR_TO_MAP_KEY,
1152 .arg4_type = ARG_ANYTHING,
1155 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1156 struct bpf_map *, map, void *, key, u64, flags)
1160 if (unlikely(flags & ~(BPF_F_INGRESS)))
1163 sk = __sock_hash_lookup_elem(map, key);
1164 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1172 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1173 .func = bpf_msg_redirect_hash,
1175 .ret_type = RET_INTEGER,
1176 .arg1_type = ARG_PTR_TO_CTX,
1177 .arg2_type = ARG_CONST_MAP_PTR,
1178 .arg3_type = ARG_PTR_TO_MAP_KEY,
1179 .arg4_type = ARG_ANYTHING,
1182 static int sock_hash_map_btf_id;
1183 const struct bpf_map_ops sock_hash_ops = {
1184 .map_alloc = sock_hash_alloc,
1185 .map_free = sock_hash_free,
1186 .map_get_next_key = sock_hash_get_next_key,
1187 .map_update_elem = sock_hash_update_elem,
1188 .map_delete_elem = sock_hash_delete_elem,
1189 .map_lookup_elem = sock_hash_lookup,
1190 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1191 .map_release_uref = sock_hash_release_progs,
1192 .map_check_btf = map_check_no_btf,
1193 .map_btf_name = "bpf_shtab",
1194 .map_btf_id = &sock_hash_map_btf_id,
1197 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1199 switch (map->map_type) {
1200 case BPF_MAP_TYPE_SOCKMAP:
1201 return &container_of(map, struct bpf_stab, map)->progs;
1202 case BPF_MAP_TYPE_SOCKHASH:
1203 return &container_of(map, struct bpf_shtab, map)->progs;
1211 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1214 struct sk_psock_progs *progs = sock_map_progs(map);
1220 case BPF_SK_MSG_VERDICT:
1221 psock_set_prog(&progs->msg_parser, prog);
1223 case BPF_SK_SKB_STREAM_PARSER:
1224 psock_set_prog(&progs->skb_parser, prog);
1226 case BPF_SK_SKB_STREAM_VERDICT:
1227 psock_set_prog(&progs->skb_verdict, prog);
1236 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1238 switch (link->map->map_type) {
1239 case BPF_MAP_TYPE_SOCKMAP:
1240 return sock_map_delete_from_link(link->map, sk,
1242 case BPF_MAP_TYPE_SOCKHASH:
1243 return sock_hash_delete_from_link(link->map, sk,
1250 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1252 struct sk_psock_link *link;
1254 while ((link = sk_psock_link_pop(psock))) {
1255 sock_map_unlink(sk, link);
1256 sk_psock_free_link(link);
1260 void sock_map_unhash(struct sock *sk)
1262 void (*saved_unhash)(struct sock *sk);
1263 struct sk_psock *psock;
1266 psock = sk_psock(sk);
1267 if (unlikely(!psock)) {
1269 if (sk->sk_prot->unhash)
1270 sk->sk_prot->unhash(sk);
1274 saved_unhash = psock->saved_unhash;
1275 sock_map_remove_links(sk, psock);
1280 void sock_map_close(struct sock *sk, long timeout)
1282 void (*saved_close)(struct sock *sk, long timeout);
1283 struct sk_psock *psock;
1287 psock = sk_psock(sk);
1288 if (unlikely(!psock)) {
1291 return sk->sk_prot->close(sk, timeout);
1294 saved_close = psock->saved_close;
1295 sock_map_remove_links(sk, psock);
1298 saved_close(sk, timeout);