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 const struct bpf_map_ops sock_map_ops = {
647 .map_alloc = sock_map_alloc,
648 .map_free = sock_map_free,
649 .map_get_next_key = sock_map_get_next_key,
650 .map_lookup_elem_sys_only = sock_map_lookup_sys,
651 .map_update_elem = sock_map_update_elem,
652 .map_delete_elem = sock_map_delete_elem,
653 .map_lookup_elem = sock_map_lookup,
654 .map_release_uref = sock_map_release_progs,
655 .map_check_btf = map_check_no_btf,
658 struct bpf_htab_elem {
662 struct hlist_node node;
666 struct bpf_htab_bucket {
667 struct hlist_head head;
673 struct bpf_htab_bucket *buckets;
676 struct sk_psock_progs progs;
680 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
682 return jhash(key, len, 0);
685 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
688 return &htab->buckets[hash & (htab->buckets_num - 1)];
691 static struct bpf_htab_elem *
692 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
695 struct bpf_htab_elem *elem;
697 hlist_for_each_entry_rcu(elem, head, node) {
698 if (elem->hash == hash &&
699 !memcmp(&elem->key, key, key_size))
706 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
708 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
709 u32 key_size = map->key_size, hash;
710 struct bpf_htab_bucket *bucket;
711 struct bpf_htab_elem *elem;
713 WARN_ON_ONCE(!rcu_read_lock_held());
715 hash = sock_hash_bucket_hash(key, key_size);
716 bucket = sock_hash_select_bucket(htab, hash);
717 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
719 return elem ? elem->sk : NULL;
722 static void sock_hash_free_elem(struct bpf_htab *htab,
723 struct bpf_htab_elem *elem)
725 atomic_dec(&htab->count);
726 kfree_rcu(elem, rcu);
729 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
732 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
733 struct bpf_htab_elem *elem_probe, *elem = link_raw;
734 struct bpf_htab_bucket *bucket;
736 WARN_ON_ONCE(!rcu_read_lock_held());
737 bucket = sock_hash_select_bucket(htab, elem->hash);
739 /* elem may be deleted in parallel from the map, but access here
740 * is okay since it's going away only after RCU grace period.
741 * However, we need to check whether it's still present.
743 raw_spin_lock_bh(&bucket->lock);
744 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
745 elem->key, map->key_size);
746 if (elem_probe && elem_probe == elem) {
747 hlist_del_rcu(&elem->node);
748 sock_map_unref(elem->sk, elem);
749 sock_hash_free_elem(htab, elem);
751 raw_spin_unlock_bh(&bucket->lock);
754 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
756 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
757 u32 hash, key_size = map->key_size;
758 struct bpf_htab_bucket *bucket;
759 struct bpf_htab_elem *elem;
762 hash = sock_hash_bucket_hash(key, key_size);
763 bucket = sock_hash_select_bucket(htab, hash);
765 raw_spin_lock_bh(&bucket->lock);
766 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
768 hlist_del_rcu(&elem->node);
769 sock_map_unref(elem->sk, elem);
770 sock_hash_free_elem(htab, elem);
773 raw_spin_unlock_bh(&bucket->lock);
777 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
778 void *key, u32 key_size,
779 u32 hash, struct sock *sk,
780 struct bpf_htab_elem *old)
782 struct bpf_htab_elem *new;
784 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
786 atomic_dec(&htab->count);
787 return ERR_PTR(-E2BIG);
791 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
792 htab->map.numa_node);
794 atomic_dec(&htab->count);
795 return ERR_PTR(-ENOMEM);
797 memcpy(new->key, key, key_size);
803 static int sock_hash_update_common(struct bpf_map *map, void *key,
804 struct sock *sk, u64 flags)
806 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
807 u32 key_size = map->key_size, hash;
808 struct bpf_htab_elem *elem, *elem_new;
809 struct bpf_htab_bucket *bucket;
810 struct sk_psock_link *link;
811 struct sk_psock *psock;
814 WARN_ON_ONCE(!rcu_read_lock_held());
815 if (unlikely(flags > BPF_EXIST))
817 if (inet_csk_has_ulp(sk))
820 link = sk_psock_init_link();
824 /* Only sockets we can redirect into/from in BPF need to hold
825 * refs to parser/verdict progs and have their sk_data_ready
826 * and sk_write_space callbacks overridden.
828 if (sock_map_redirect_allowed(sk))
829 ret = sock_map_link(map, &htab->progs, sk);
831 ret = sock_map_link_no_progs(map, sk);
835 psock = sk_psock(sk);
836 WARN_ON_ONCE(!psock);
838 hash = sock_hash_bucket_hash(key, key_size);
839 bucket = sock_hash_select_bucket(htab, hash);
841 raw_spin_lock_bh(&bucket->lock);
842 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
843 if (elem && flags == BPF_NOEXIST) {
846 } else if (!elem && flags == BPF_EXIST) {
851 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
852 if (IS_ERR(elem_new)) {
853 ret = PTR_ERR(elem_new);
857 sock_map_add_link(psock, link, map, elem_new);
858 /* Add new element to the head of the list, so that
859 * concurrent search will find it before old elem.
861 hlist_add_head_rcu(&elem_new->node, &bucket->head);
863 hlist_del_rcu(&elem->node);
864 sock_map_unref(elem->sk, elem);
865 sock_hash_free_elem(htab, elem);
867 raw_spin_unlock_bh(&bucket->lock);
870 raw_spin_unlock_bh(&bucket->lock);
871 sk_psock_put(sk, psock);
873 sk_psock_free_link(link);
877 static int sock_hash_update_elem(struct bpf_map *map, void *key,
878 void *value, u64 flags)
885 if (map->value_size == sizeof(u64))
892 sock = sockfd_lookup(ufd, &ret);
900 if (!sock_map_sk_is_suitable(sk)) {
905 sock_map_sk_acquire(sk);
906 if (!sock_map_sk_state_allowed(sk))
909 ret = sock_hash_update_common(map, key, sk, flags);
910 sock_map_sk_release(sk);
916 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
919 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
920 struct bpf_htab_elem *elem, *elem_next;
921 u32 hash, key_size = map->key_size;
922 struct hlist_head *head;
926 goto find_first_elem;
927 hash = sock_hash_bucket_hash(key, key_size);
928 head = &sock_hash_select_bucket(htab, hash)->head;
929 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
931 goto find_first_elem;
933 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
934 struct bpf_htab_elem, node);
936 memcpy(key_next, elem_next->key, key_size);
940 i = hash & (htab->buckets_num - 1);
943 for (; i < htab->buckets_num; i++) {
944 head = &sock_hash_select_bucket(htab, i)->head;
945 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
946 struct bpf_htab_elem, node);
948 memcpy(key_next, elem_next->key, key_size);
956 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
958 struct bpf_htab *htab;
962 if (!capable(CAP_NET_ADMIN))
963 return ERR_PTR(-EPERM);
964 if (attr->max_entries == 0 ||
965 attr->key_size == 0 ||
966 (attr->value_size != sizeof(u32) &&
967 attr->value_size != sizeof(u64)) ||
968 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
969 return ERR_PTR(-EINVAL);
970 if (attr->key_size > MAX_BPF_STACK)
971 return ERR_PTR(-E2BIG);
973 htab = kzalloc(sizeof(*htab), GFP_USER);
975 return ERR_PTR(-ENOMEM);
977 bpf_map_init_from_attr(&htab->map, attr);
979 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
980 htab->elem_size = sizeof(struct bpf_htab_elem) +
981 round_up(htab->map.key_size, 8);
982 if (htab->buckets_num == 0 ||
983 htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
988 cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
989 (u64) htab->elem_size * htab->map.max_entries;
990 if (cost >= U32_MAX - PAGE_SIZE) {
995 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
996 sizeof(struct bpf_htab_bucket),
997 htab->map.numa_node);
998 if (!htab->buckets) {
1003 for (i = 0; i < htab->buckets_num; i++) {
1004 INIT_HLIST_HEAD(&htab->buckets[i].head);
1005 raw_spin_lock_init(&htab->buckets[i].lock);
1011 return ERR_PTR(err);
1014 static void sock_hash_free(struct bpf_map *map)
1016 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1017 struct bpf_htab_bucket *bucket;
1018 struct hlist_head unlink_list;
1019 struct bpf_htab_elem *elem;
1020 struct hlist_node *node;
1023 /* After the sync no updates or deletes will be in-flight so it
1024 * is safe to walk map and remove entries without risking a race
1025 * in EEXIST update case.
1028 for (i = 0; i < htab->buckets_num; i++) {
1029 bucket = sock_hash_select_bucket(htab, i);
1031 /* We are racing with sock_hash_delete_from_link to
1032 * enter the spin-lock critical section. Every socket on
1033 * the list is still linked to sockhash. Since link
1034 * exists, psock exists and holds a ref to socket. That
1035 * lets us to grab a socket ref too.
1037 raw_spin_lock_bh(&bucket->lock);
1038 hlist_for_each_entry(elem, &bucket->head, node)
1039 sock_hold(elem->sk);
1040 hlist_move_list(&bucket->head, &unlink_list);
1041 raw_spin_unlock_bh(&bucket->lock);
1043 /* Process removed entries out of atomic context to
1044 * block for socket lock before deleting the psock's
1047 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1048 hlist_del(&elem->node);
1049 lock_sock(elem->sk);
1051 sock_map_unref(elem->sk, elem);
1053 release_sock(elem->sk);
1055 sock_hash_free_elem(htab, elem);
1059 /* wait for psock readers accessing its map link */
1062 bpf_map_area_free(htab->buckets);
1066 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1070 if (map->value_size != sizeof(u64))
1071 return ERR_PTR(-ENOSPC);
1073 sk = __sock_hash_lookup_elem(map, key);
1075 return ERR_PTR(-ENOENT);
1077 sock_gen_cookie(sk);
1078 return &sk->sk_cookie;
1081 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1085 sk = __sock_hash_lookup_elem(map, key);
1086 if (!sk || !sk_fullsock(sk))
1088 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1093 static void sock_hash_release_progs(struct bpf_map *map)
1095 psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
1098 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1099 struct bpf_map *, map, void *, key, u64, flags)
1101 WARN_ON_ONCE(!rcu_read_lock_held());
1103 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1104 sock_map_op_okay(sops)))
1105 return sock_hash_update_common(map, key, sops->sk, flags);
1109 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1110 .func = bpf_sock_hash_update,
1113 .ret_type = RET_INTEGER,
1114 .arg1_type = ARG_PTR_TO_CTX,
1115 .arg2_type = ARG_CONST_MAP_PTR,
1116 .arg3_type = ARG_PTR_TO_MAP_KEY,
1117 .arg4_type = ARG_ANYTHING,
1120 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1121 struct bpf_map *, map, void *, key, u64, flags)
1123 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1126 if (unlikely(flags & ~(BPF_F_INGRESS)))
1129 sk = __sock_hash_lookup_elem(map, key);
1130 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1133 tcb->bpf.flags = flags;
1134 tcb->bpf.sk_redir = sk;
1138 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1139 .func = bpf_sk_redirect_hash,
1141 .ret_type = RET_INTEGER,
1142 .arg1_type = ARG_PTR_TO_CTX,
1143 .arg2_type = ARG_CONST_MAP_PTR,
1144 .arg3_type = ARG_PTR_TO_MAP_KEY,
1145 .arg4_type = ARG_ANYTHING,
1148 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1149 struct bpf_map *, map, void *, key, u64, flags)
1153 if (unlikely(flags & ~(BPF_F_INGRESS)))
1156 sk = __sock_hash_lookup_elem(map, key);
1157 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1165 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1166 .func = bpf_msg_redirect_hash,
1168 .ret_type = RET_INTEGER,
1169 .arg1_type = ARG_PTR_TO_CTX,
1170 .arg2_type = ARG_CONST_MAP_PTR,
1171 .arg3_type = ARG_PTR_TO_MAP_KEY,
1172 .arg4_type = ARG_ANYTHING,
1175 const struct bpf_map_ops sock_hash_ops = {
1176 .map_alloc = sock_hash_alloc,
1177 .map_free = sock_hash_free,
1178 .map_get_next_key = sock_hash_get_next_key,
1179 .map_update_elem = sock_hash_update_elem,
1180 .map_delete_elem = sock_hash_delete_elem,
1181 .map_lookup_elem = sock_hash_lookup,
1182 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1183 .map_release_uref = sock_hash_release_progs,
1184 .map_check_btf = map_check_no_btf,
1187 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1189 switch (map->map_type) {
1190 case BPF_MAP_TYPE_SOCKMAP:
1191 return &container_of(map, struct bpf_stab, map)->progs;
1192 case BPF_MAP_TYPE_SOCKHASH:
1193 return &container_of(map, struct bpf_htab, map)->progs;
1201 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1204 struct sk_psock_progs *progs = sock_map_progs(map);
1210 case BPF_SK_MSG_VERDICT:
1211 psock_set_prog(&progs->msg_parser, prog);
1213 case BPF_SK_SKB_STREAM_PARSER:
1214 psock_set_prog(&progs->skb_parser, prog);
1216 case BPF_SK_SKB_STREAM_VERDICT:
1217 psock_set_prog(&progs->skb_verdict, prog);
1226 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1228 switch (link->map->map_type) {
1229 case BPF_MAP_TYPE_SOCKMAP:
1230 return sock_map_delete_from_link(link->map, sk,
1232 case BPF_MAP_TYPE_SOCKHASH:
1233 return sock_hash_delete_from_link(link->map, sk,
1240 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1242 struct sk_psock_link *link;
1244 while ((link = sk_psock_link_pop(psock))) {
1245 sock_map_unlink(sk, link);
1246 sk_psock_free_link(link);
1250 void sock_map_unhash(struct sock *sk)
1252 void (*saved_unhash)(struct sock *sk);
1253 struct sk_psock *psock;
1256 psock = sk_psock(sk);
1257 if (unlikely(!psock)) {
1259 if (sk->sk_prot->unhash)
1260 sk->sk_prot->unhash(sk);
1264 saved_unhash = psock->saved_unhash;
1265 sock_map_remove_links(sk, psock);
1270 void sock_map_close(struct sock *sk, long timeout)
1272 void (*saved_close)(struct sock *sk, long timeout);
1273 struct sk_psock *psock;
1277 psock = sk_psock(sk);
1278 if (unlikely(!psock)) {
1281 return sk->sk_prot->close(sk, timeout);
1284 saved_close = psock->saved_close;
1285 sock_map_remove_links(sk, psock);
1288 saved_close(sk, timeout);