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)
346 return __sock_map_lookup_elem(map, *(u32 *)key);
349 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
353 if (map->value_size != sizeof(u64))
354 return ERR_PTR(-ENOSPC);
356 sk = __sock_map_lookup_elem(map, *(u32 *)key);
358 return ERR_PTR(-ENOENT);
361 return &sk->sk_cookie;
364 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
370 raw_spin_lock_bh(&stab->lock);
372 if (!sk_test || sk_test == sk)
373 sk = xchg(psk, NULL);
376 sock_map_unref(sk, psk);
380 raw_spin_unlock_bh(&stab->lock);
384 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
387 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
389 __sock_map_delete(stab, sk, link_raw);
392 static int sock_map_delete_elem(struct bpf_map *map, void *key)
394 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
398 if (unlikely(i >= map->max_entries))
402 return __sock_map_delete(stab, NULL, psk);
405 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
407 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
408 u32 i = key ? *(u32 *)key : U32_MAX;
409 u32 *key_next = next;
411 if (i == stab->map.max_entries - 1)
413 if (i >= stab->map.max_entries)
420 static bool sock_map_redirect_allowed(const struct sock *sk)
422 return sk->sk_state != TCP_LISTEN;
425 static int sock_map_update_common(struct bpf_map *map, u32 idx,
426 struct sock *sk, u64 flags)
428 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
429 struct sk_psock_link *link;
430 struct sk_psock *psock;
434 WARN_ON_ONCE(!rcu_read_lock_held());
435 if (unlikely(flags > BPF_EXIST))
437 if (unlikely(idx >= map->max_entries))
439 if (inet_csk_has_ulp(sk))
442 link = sk_psock_init_link();
446 /* Only sockets we can redirect into/from in BPF need to hold
447 * refs to parser/verdict progs and have their sk_data_ready
448 * and sk_write_space callbacks overridden.
450 if (sock_map_redirect_allowed(sk))
451 ret = sock_map_link(map, &stab->progs, sk);
453 ret = sock_map_link_no_progs(map, sk);
457 psock = sk_psock(sk);
458 WARN_ON_ONCE(!psock);
460 raw_spin_lock_bh(&stab->lock);
461 osk = stab->sks[idx];
462 if (osk && flags == BPF_NOEXIST) {
465 } else if (!osk && flags == BPF_EXIST) {
470 sock_map_add_link(psock, link, map, &stab->sks[idx]);
473 sock_map_unref(osk, &stab->sks[idx]);
474 raw_spin_unlock_bh(&stab->lock);
477 raw_spin_unlock_bh(&stab->lock);
479 sk_psock_put(sk, psock);
481 sk_psock_free_link(link);
485 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
487 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
488 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
489 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
492 static bool sk_is_tcp(const struct sock *sk)
494 return sk->sk_type == SOCK_STREAM &&
495 sk->sk_protocol == IPPROTO_TCP;
498 static bool sk_is_udp(const struct sock *sk)
500 return sk->sk_type == SOCK_DGRAM &&
501 sk->sk_protocol == IPPROTO_UDP;
504 static bool sock_map_sk_is_suitable(const struct sock *sk)
506 return sk_is_tcp(sk) || sk_is_udp(sk);
509 static bool sock_map_sk_state_allowed(const struct sock *sk)
512 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
513 else if (sk_is_udp(sk))
514 return sk_hashed(sk);
519 static int sock_map_update_elem(struct bpf_map *map, void *key,
520 void *value, u64 flags)
522 u32 idx = *(u32 *)key;
528 if (map->value_size == sizeof(u64))
535 sock = sockfd_lookup(ufd, &ret);
543 if (!sock_map_sk_is_suitable(sk)) {
548 sock_map_sk_acquire(sk);
549 if (!sock_map_sk_state_allowed(sk))
552 ret = sock_map_update_common(map, idx, sk, flags);
553 sock_map_sk_release(sk);
559 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
560 struct bpf_map *, map, void *, key, u64, flags)
562 WARN_ON_ONCE(!rcu_read_lock_held());
564 if (likely(sock_map_sk_is_suitable(sops->sk) &&
565 sock_map_op_okay(sops)))
566 return sock_map_update_common(map, *(u32 *)key, sops->sk,
571 const struct bpf_func_proto bpf_sock_map_update_proto = {
572 .func = bpf_sock_map_update,
575 .ret_type = RET_INTEGER,
576 .arg1_type = ARG_PTR_TO_CTX,
577 .arg2_type = ARG_CONST_MAP_PTR,
578 .arg3_type = ARG_PTR_TO_MAP_KEY,
579 .arg4_type = ARG_ANYTHING,
582 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
583 struct bpf_map *, map, u32, key, u64, flags)
585 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
588 if (unlikely(flags & ~(BPF_F_INGRESS)))
591 sk = __sock_map_lookup_elem(map, key);
592 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
595 tcb->bpf.flags = flags;
596 tcb->bpf.sk_redir = sk;
600 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
601 .func = bpf_sk_redirect_map,
603 .ret_type = RET_INTEGER,
604 .arg1_type = ARG_PTR_TO_CTX,
605 .arg2_type = ARG_CONST_MAP_PTR,
606 .arg3_type = ARG_ANYTHING,
607 .arg4_type = ARG_ANYTHING,
610 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
611 struct bpf_map *, map, u32, key, u64, flags)
615 if (unlikely(flags & ~(BPF_F_INGRESS)))
618 sk = __sock_map_lookup_elem(map, key);
619 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
627 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
628 .func = bpf_msg_redirect_map,
630 .ret_type = RET_INTEGER,
631 .arg1_type = ARG_PTR_TO_CTX,
632 .arg2_type = ARG_CONST_MAP_PTR,
633 .arg3_type = ARG_ANYTHING,
634 .arg4_type = ARG_ANYTHING,
637 const struct bpf_map_ops sock_map_ops = {
638 .map_alloc = sock_map_alloc,
639 .map_free = sock_map_free,
640 .map_get_next_key = sock_map_get_next_key,
641 .map_lookup_elem_sys_only = sock_map_lookup_sys,
642 .map_update_elem = sock_map_update_elem,
643 .map_delete_elem = sock_map_delete_elem,
644 .map_lookup_elem = sock_map_lookup,
645 .map_release_uref = sock_map_release_progs,
646 .map_check_btf = map_check_no_btf,
649 struct bpf_htab_elem {
653 struct hlist_node node;
657 struct bpf_htab_bucket {
658 struct hlist_head head;
664 struct bpf_htab_bucket *buckets;
667 struct sk_psock_progs progs;
671 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
673 return jhash(key, len, 0);
676 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
679 return &htab->buckets[hash & (htab->buckets_num - 1)];
682 static struct bpf_htab_elem *
683 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
686 struct bpf_htab_elem *elem;
688 hlist_for_each_entry_rcu(elem, head, node) {
689 if (elem->hash == hash &&
690 !memcmp(&elem->key, key, key_size))
697 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
699 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
700 u32 key_size = map->key_size, hash;
701 struct bpf_htab_bucket *bucket;
702 struct bpf_htab_elem *elem;
704 WARN_ON_ONCE(!rcu_read_lock_held());
706 hash = sock_hash_bucket_hash(key, key_size);
707 bucket = sock_hash_select_bucket(htab, hash);
708 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
710 return elem ? elem->sk : NULL;
713 static void sock_hash_free_elem(struct bpf_htab *htab,
714 struct bpf_htab_elem *elem)
716 atomic_dec(&htab->count);
717 kfree_rcu(elem, rcu);
720 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
723 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
724 struct bpf_htab_elem *elem_probe, *elem = link_raw;
725 struct bpf_htab_bucket *bucket;
727 WARN_ON_ONCE(!rcu_read_lock_held());
728 bucket = sock_hash_select_bucket(htab, elem->hash);
730 /* elem may be deleted in parallel from the map, but access here
731 * is okay since it's going away only after RCU grace period.
732 * However, we need to check whether it's still present.
734 raw_spin_lock_bh(&bucket->lock);
735 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
736 elem->key, map->key_size);
737 if (elem_probe && elem_probe == elem) {
738 hlist_del_rcu(&elem->node);
739 sock_map_unref(elem->sk, elem);
740 sock_hash_free_elem(htab, elem);
742 raw_spin_unlock_bh(&bucket->lock);
745 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
747 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
748 u32 hash, key_size = map->key_size;
749 struct bpf_htab_bucket *bucket;
750 struct bpf_htab_elem *elem;
753 hash = sock_hash_bucket_hash(key, key_size);
754 bucket = sock_hash_select_bucket(htab, hash);
756 raw_spin_lock_bh(&bucket->lock);
757 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
759 hlist_del_rcu(&elem->node);
760 sock_map_unref(elem->sk, elem);
761 sock_hash_free_elem(htab, elem);
764 raw_spin_unlock_bh(&bucket->lock);
768 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
769 void *key, u32 key_size,
770 u32 hash, struct sock *sk,
771 struct bpf_htab_elem *old)
773 struct bpf_htab_elem *new;
775 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
777 atomic_dec(&htab->count);
778 return ERR_PTR(-E2BIG);
782 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
783 htab->map.numa_node);
785 atomic_dec(&htab->count);
786 return ERR_PTR(-ENOMEM);
788 memcpy(new->key, key, key_size);
794 static int sock_hash_update_common(struct bpf_map *map, void *key,
795 struct sock *sk, u64 flags)
797 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
798 u32 key_size = map->key_size, hash;
799 struct bpf_htab_elem *elem, *elem_new;
800 struct bpf_htab_bucket *bucket;
801 struct sk_psock_link *link;
802 struct sk_psock *psock;
805 WARN_ON_ONCE(!rcu_read_lock_held());
806 if (unlikely(flags > BPF_EXIST))
808 if (inet_csk_has_ulp(sk))
811 link = sk_psock_init_link();
815 /* Only sockets we can redirect into/from in BPF need to hold
816 * refs to parser/verdict progs and have their sk_data_ready
817 * and sk_write_space callbacks overridden.
819 if (sock_map_redirect_allowed(sk))
820 ret = sock_map_link(map, &htab->progs, sk);
822 ret = sock_map_link_no_progs(map, sk);
826 psock = sk_psock(sk);
827 WARN_ON_ONCE(!psock);
829 hash = sock_hash_bucket_hash(key, key_size);
830 bucket = sock_hash_select_bucket(htab, hash);
832 raw_spin_lock_bh(&bucket->lock);
833 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
834 if (elem && flags == BPF_NOEXIST) {
837 } else if (!elem && flags == BPF_EXIST) {
842 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
843 if (IS_ERR(elem_new)) {
844 ret = PTR_ERR(elem_new);
848 sock_map_add_link(psock, link, map, elem_new);
849 /* Add new element to the head of the list, so that
850 * concurrent search will find it before old elem.
852 hlist_add_head_rcu(&elem_new->node, &bucket->head);
854 hlist_del_rcu(&elem->node);
855 sock_map_unref(elem->sk, elem);
856 sock_hash_free_elem(htab, elem);
858 raw_spin_unlock_bh(&bucket->lock);
861 raw_spin_unlock_bh(&bucket->lock);
862 sk_psock_put(sk, psock);
864 sk_psock_free_link(link);
868 static int sock_hash_update_elem(struct bpf_map *map, void *key,
869 void *value, u64 flags)
876 if (map->value_size == sizeof(u64))
883 sock = sockfd_lookup(ufd, &ret);
891 if (!sock_map_sk_is_suitable(sk)) {
896 sock_map_sk_acquire(sk);
897 if (!sock_map_sk_state_allowed(sk))
900 ret = sock_hash_update_common(map, key, sk, flags);
901 sock_map_sk_release(sk);
907 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
910 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
911 struct bpf_htab_elem *elem, *elem_next;
912 u32 hash, key_size = map->key_size;
913 struct hlist_head *head;
917 goto find_first_elem;
918 hash = sock_hash_bucket_hash(key, key_size);
919 head = &sock_hash_select_bucket(htab, hash)->head;
920 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
922 goto find_first_elem;
924 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
925 struct bpf_htab_elem, node);
927 memcpy(key_next, elem_next->key, key_size);
931 i = hash & (htab->buckets_num - 1);
934 for (; i < htab->buckets_num; i++) {
935 head = &sock_hash_select_bucket(htab, i)->head;
936 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
937 struct bpf_htab_elem, node);
939 memcpy(key_next, elem_next->key, key_size);
947 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
949 struct bpf_htab *htab;
953 if (!capable(CAP_NET_ADMIN))
954 return ERR_PTR(-EPERM);
955 if (attr->max_entries == 0 ||
956 attr->key_size == 0 ||
957 (attr->value_size != sizeof(u32) &&
958 attr->value_size != sizeof(u64)) ||
959 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
960 return ERR_PTR(-EINVAL);
961 if (attr->key_size > MAX_BPF_STACK)
962 return ERR_PTR(-E2BIG);
964 htab = kzalloc(sizeof(*htab), GFP_USER);
966 return ERR_PTR(-ENOMEM);
968 bpf_map_init_from_attr(&htab->map, attr);
970 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
971 htab->elem_size = sizeof(struct bpf_htab_elem) +
972 round_up(htab->map.key_size, 8);
973 if (htab->buckets_num == 0 ||
974 htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
979 cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
980 (u64) htab->elem_size * htab->map.max_entries;
981 if (cost >= U32_MAX - PAGE_SIZE) {
986 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
987 sizeof(struct bpf_htab_bucket),
988 htab->map.numa_node);
989 if (!htab->buckets) {
994 for (i = 0; i < htab->buckets_num; i++) {
995 INIT_HLIST_HEAD(&htab->buckets[i].head);
996 raw_spin_lock_init(&htab->buckets[i].lock);
1002 return ERR_PTR(err);
1005 static void sock_hash_free(struct bpf_map *map)
1007 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1008 struct bpf_htab_bucket *bucket;
1009 struct bpf_htab_elem *elem;
1010 struct hlist_node *node;
1013 /* After the sync no updates or deletes will be in-flight so it
1014 * is safe to walk map and remove entries without risking a race
1015 * in EEXIST update case.
1018 for (i = 0; i < htab->buckets_num; i++) {
1019 bucket = sock_hash_select_bucket(htab, i);
1020 hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
1021 hlist_del_rcu(&elem->node);
1022 lock_sock(elem->sk);
1024 sock_map_unref(elem->sk, elem);
1026 release_sock(elem->sk);
1030 /* wait for psock readers accessing its map link */
1033 bpf_map_area_free(htab->buckets);
1037 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1041 if (map->value_size != sizeof(u64))
1042 return ERR_PTR(-ENOSPC);
1044 sk = __sock_hash_lookup_elem(map, key);
1046 return ERR_PTR(-ENOENT);
1048 sock_gen_cookie(sk);
1049 return &sk->sk_cookie;
1052 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1054 return __sock_hash_lookup_elem(map, key);
1057 static void sock_hash_release_progs(struct bpf_map *map)
1059 psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
1062 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1063 struct bpf_map *, map, void *, key, u64, flags)
1065 WARN_ON_ONCE(!rcu_read_lock_held());
1067 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1068 sock_map_op_okay(sops)))
1069 return sock_hash_update_common(map, key, sops->sk, flags);
1073 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1074 .func = bpf_sock_hash_update,
1077 .ret_type = RET_INTEGER,
1078 .arg1_type = ARG_PTR_TO_CTX,
1079 .arg2_type = ARG_CONST_MAP_PTR,
1080 .arg3_type = ARG_PTR_TO_MAP_KEY,
1081 .arg4_type = ARG_ANYTHING,
1084 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1085 struct bpf_map *, map, void *, key, u64, flags)
1087 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1090 if (unlikely(flags & ~(BPF_F_INGRESS)))
1093 sk = __sock_hash_lookup_elem(map, key);
1094 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1097 tcb->bpf.flags = flags;
1098 tcb->bpf.sk_redir = sk;
1102 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1103 .func = bpf_sk_redirect_hash,
1105 .ret_type = RET_INTEGER,
1106 .arg1_type = ARG_PTR_TO_CTX,
1107 .arg2_type = ARG_CONST_MAP_PTR,
1108 .arg3_type = ARG_PTR_TO_MAP_KEY,
1109 .arg4_type = ARG_ANYTHING,
1112 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1113 struct bpf_map *, map, void *, key, u64, flags)
1117 if (unlikely(flags & ~(BPF_F_INGRESS)))
1120 sk = __sock_hash_lookup_elem(map, key);
1121 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1129 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1130 .func = bpf_msg_redirect_hash,
1132 .ret_type = RET_INTEGER,
1133 .arg1_type = ARG_PTR_TO_CTX,
1134 .arg2_type = ARG_CONST_MAP_PTR,
1135 .arg3_type = ARG_PTR_TO_MAP_KEY,
1136 .arg4_type = ARG_ANYTHING,
1139 const struct bpf_map_ops sock_hash_ops = {
1140 .map_alloc = sock_hash_alloc,
1141 .map_free = sock_hash_free,
1142 .map_get_next_key = sock_hash_get_next_key,
1143 .map_update_elem = sock_hash_update_elem,
1144 .map_delete_elem = sock_hash_delete_elem,
1145 .map_lookup_elem = sock_hash_lookup,
1146 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1147 .map_release_uref = sock_hash_release_progs,
1148 .map_check_btf = map_check_no_btf,
1151 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1153 switch (map->map_type) {
1154 case BPF_MAP_TYPE_SOCKMAP:
1155 return &container_of(map, struct bpf_stab, map)->progs;
1156 case BPF_MAP_TYPE_SOCKHASH:
1157 return &container_of(map, struct bpf_htab, map)->progs;
1165 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1168 struct sk_psock_progs *progs = sock_map_progs(map);
1174 case BPF_SK_MSG_VERDICT:
1175 psock_set_prog(&progs->msg_parser, prog);
1177 case BPF_SK_SKB_STREAM_PARSER:
1178 psock_set_prog(&progs->skb_parser, prog);
1180 case BPF_SK_SKB_STREAM_VERDICT:
1181 psock_set_prog(&progs->skb_verdict, prog);
1190 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1192 switch (link->map->map_type) {
1193 case BPF_MAP_TYPE_SOCKMAP:
1194 return sock_map_delete_from_link(link->map, sk,
1196 case BPF_MAP_TYPE_SOCKHASH:
1197 return sock_hash_delete_from_link(link->map, sk,
1204 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1206 struct sk_psock_link *link;
1208 while ((link = sk_psock_link_pop(psock))) {
1209 sock_map_unlink(sk, link);
1210 sk_psock_free_link(link);
1214 void sock_map_unhash(struct sock *sk)
1216 void (*saved_unhash)(struct sock *sk);
1217 struct sk_psock *psock;
1220 psock = sk_psock(sk);
1221 if (unlikely(!psock)) {
1223 if (sk->sk_prot->unhash)
1224 sk->sk_prot->unhash(sk);
1228 saved_unhash = psock->saved_unhash;
1229 sock_map_remove_links(sk, psock);
1234 void sock_map_close(struct sock *sk, long timeout)
1236 void (*saved_close)(struct sock *sk, long timeout);
1237 struct sk_psock *psock;
1241 psock = sk_psock(sk);
1242 if (unlikely(!psock)) {
1245 return sk->sk_prot->close(sk, timeout);
1248 saved_close = psock->saved_close;
1249 sock_map_remove_links(sk, psock);
1252 saved_close(sk, timeout);