1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
5 #include <linux/btf_ids.h>
6 #include <linux/filter.h>
7 #include <linux/errno.h>
8 #include <linux/file.h>
10 #include <linux/workqueue.h>
11 #include <linux/skmsg.h>
12 #include <linux/list.h>
13 #include <linux/jhash.h>
14 #include <linux/sock_diag.h>
20 struct sk_psock_progs progs;
24 #define SOCK_CREATE_FLAG_MASK \
25 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
27 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
28 struct bpf_prog *old, u32 which);
29 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map);
31 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
33 struct bpf_stab *stab;
35 if (attr->max_entries == 0 ||
36 attr->key_size != 4 ||
37 (attr->value_size != sizeof(u32) &&
38 attr->value_size != sizeof(u64)) ||
39 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
40 return ERR_PTR(-EINVAL);
42 stab = bpf_map_area_alloc(sizeof(*stab), NUMA_NO_NODE);
44 return ERR_PTR(-ENOMEM);
46 bpf_map_init_from_attr(&stab->map, attr);
47 spin_lock_init(&stab->lock);
49 stab->sks = bpf_map_area_alloc((u64) stab->map.max_entries *
50 sizeof(struct sock *),
53 bpf_map_area_free(stab);
54 return ERR_PTR(-ENOMEM);
60 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
62 u32 ufd = attr->target_fd;
67 if (attr->attach_flags || attr->replace_bpf_fd)
71 map = __bpf_map_get(f);
74 ret = sock_map_prog_update(map, prog, NULL, attr->attach_type);
79 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
81 u32 ufd = attr->target_fd;
82 struct bpf_prog *prog;
87 if (attr->attach_flags || attr->replace_bpf_fd)
91 map = __bpf_map_get(f);
95 prog = bpf_prog_get(attr->attach_bpf_fd);
101 if (prog->type != ptype) {
106 ret = sock_map_prog_update(map, NULL, prog, attr->attach_type);
114 static void sock_map_sk_acquire(struct sock *sk)
115 __acquires(&sk->sk_lock.slock)
121 static void sock_map_sk_release(struct sock *sk)
122 __releases(&sk->sk_lock.slock)
128 static void sock_map_add_link(struct sk_psock *psock,
129 struct sk_psock_link *link,
130 struct bpf_map *map, void *link_raw)
132 link->link_raw = link_raw;
134 spin_lock_bh(&psock->link_lock);
135 list_add_tail(&link->list, &psock->link);
136 spin_unlock_bh(&psock->link_lock);
139 static void sock_map_del_link(struct sock *sk,
140 struct sk_psock *psock, void *link_raw)
142 bool strp_stop = false, verdict_stop = false;
143 struct sk_psock_link *link, *tmp;
145 spin_lock_bh(&psock->link_lock);
146 list_for_each_entry_safe(link, tmp, &psock->link, list) {
147 if (link->link_raw == link_raw) {
148 struct bpf_map *map = link->map;
149 struct sk_psock_progs *progs = sock_map_progs(map);
151 if (psock->saved_data_ready && progs->stream_parser)
153 if (psock->saved_data_ready && progs->stream_verdict)
155 if (psock->saved_data_ready && progs->skb_verdict)
157 list_del(&link->list);
158 sk_psock_free_link(link);
161 spin_unlock_bh(&psock->link_lock);
162 if (strp_stop || verdict_stop) {
163 write_lock_bh(&sk->sk_callback_lock);
165 sk_psock_stop_strp(sk, psock);
167 sk_psock_stop_verdict(sk, psock);
169 if (psock->psock_update_sk_prot)
170 psock->psock_update_sk_prot(sk, psock, false);
171 write_unlock_bh(&sk->sk_callback_lock);
175 static void sock_map_unref(struct sock *sk, void *link_raw)
177 struct sk_psock *psock = sk_psock(sk);
180 sock_map_del_link(sk, psock, link_raw);
181 sk_psock_put(sk, psock);
185 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
187 if (!sk->sk_prot->psock_update_sk_prot)
189 psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
190 return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
193 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
195 struct sk_psock *psock;
198 psock = sk_psock(sk);
200 if (sk->sk_prot->close != sock_map_close) {
201 psock = ERR_PTR(-EBUSY);
205 if (!refcount_inc_not_zero(&psock->refcnt))
206 psock = ERR_PTR(-EBUSY);
213 static int sock_map_link(struct bpf_map *map, struct sock *sk)
215 struct sk_psock_progs *progs = sock_map_progs(map);
216 struct bpf_prog *stream_verdict = NULL;
217 struct bpf_prog *stream_parser = NULL;
218 struct bpf_prog *skb_verdict = NULL;
219 struct bpf_prog *msg_parser = NULL;
220 struct sk_psock *psock;
223 stream_verdict = READ_ONCE(progs->stream_verdict);
224 if (stream_verdict) {
225 stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
226 if (IS_ERR(stream_verdict))
227 return PTR_ERR(stream_verdict);
230 stream_parser = READ_ONCE(progs->stream_parser);
232 stream_parser = bpf_prog_inc_not_zero(stream_parser);
233 if (IS_ERR(stream_parser)) {
234 ret = PTR_ERR(stream_parser);
235 goto out_put_stream_verdict;
239 msg_parser = READ_ONCE(progs->msg_parser);
241 msg_parser = bpf_prog_inc_not_zero(msg_parser);
242 if (IS_ERR(msg_parser)) {
243 ret = PTR_ERR(msg_parser);
244 goto out_put_stream_parser;
248 skb_verdict = READ_ONCE(progs->skb_verdict);
250 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
251 if (IS_ERR(skb_verdict)) {
252 ret = PTR_ERR(skb_verdict);
253 goto out_put_msg_parser;
257 psock = sock_map_psock_get_checked(sk);
259 ret = PTR_ERR(psock);
264 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
265 (stream_parser && READ_ONCE(psock->progs.stream_parser)) ||
266 (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
267 (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) ||
268 (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
269 (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) {
270 sk_psock_put(sk, psock);
275 psock = sk_psock_init(sk, map->numa_node);
277 ret = PTR_ERR(psock);
283 psock_set_prog(&psock->progs.msg_parser, msg_parser);
285 psock_set_prog(&psock->progs.stream_parser, stream_parser);
287 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
289 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
291 /* msg_* and stream_* programs references tracked in psock after this
292 * point. Reference dec and cleanup will occur through psock destructor
294 ret = sock_map_init_proto(sk, psock);
296 sk_psock_put(sk, psock);
300 write_lock_bh(&sk->sk_callback_lock);
301 if (stream_parser && stream_verdict && !psock->saved_data_ready) {
302 ret = sk_psock_init_strp(sk, psock);
304 write_unlock_bh(&sk->sk_callback_lock);
305 sk_psock_put(sk, psock);
308 sk_psock_start_strp(sk, psock);
309 } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
310 sk_psock_start_verdict(sk,psock);
311 } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
312 sk_psock_start_verdict(sk, psock);
314 write_unlock_bh(&sk->sk_callback_lock);
318 bpf_prog_put(skb_verdict);
321 bpf_prog_put(msg_parser);
322 out_put_stream_parser:
324 bpf_prog_put(stream_parser);
325 out_put_stream_verdict:
327 bpf_prog_put(stream_verdict);
332 static void sock_map_free(struct bpf_map *map)
334 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
337 /* After the sync no updates or deletes will be in-flight so it
338 * is safe to walk map and remove entries without risking a race
339 * in EEXIST update case.
342 for (i = 0; i < stab->map.max_entries; i++) {
343 struct sock **psk = &stab->sks[i];
346 sk = xchg(psk, NULL);
351 sock_map_unref(sk, psk);
358 /* wait for psock readers accessing its map link */
361 bpf_map_area_free(stab->sks);
362 bpf_map_area_free(stab);
365 static void sock_map_release_progs(struct bpf_map *map)
367 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
370 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
372 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
374 WARN_ON_ONCE(!rcu_read_lock_held());
376 if (unlikely(key >= map->max_entries))
378 return READ_ONCE(stab->sks[key]);
381 static void *sock_map_lookup(struct bpf_map *map, void *key)
385 sk = __sock_map_lookup_elem(map, *(u32 *)key);
388 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
393 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
397 if (map->value_size != sizeof(u64))
398 return ERR_PTR(-ENOSPC);
400 sk = __sock_map_lookup_elem(map, *(u32 *)key);
402 return ERR_PTR(-ENOENT);
404 __sock_gen_cookie(sk);
405 return &sk->sk_cookie;
408 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
414 spin_lock_bh(&stab->lock);
416 if (!sk_test || sk_test == sk)
417 sk = xchg(psk, NULL);
420 sock_map_unref(sk, psk);
424 spin_unlock_bh(&stab->lock);
428 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
431 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
433 __sock_map_delete(stab, sk, link_raw);
436 static long sock_map_delete_elem(struct bpf_map *map, void *key)
438 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
442 if (unlikely(i >= map->max_entries))
446 return __sock_map_delete(stab, NULL, psk);
449 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
451 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
452 u32 i = key ? *(u32 *)key : U32_MAX;
453 u32 *key_next = next;
455 if (i == stab->map.max_entries - 1)
457 if (i >= stab->map.max_entries)
464 static int sock_map_update_common(struct bpf_map *map, u32 idx,
465 struct sock *sk, u64 flags)
467 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
468 struct sk_psock_link *link;
469 struct sk_psock *psock;
473 WARN_ON_ONCE(!rcu_read_lock_held());
474 if (unlikely(flags > BPF_EXIST))
476 if (unlikely(idx >= map->max_entries))
479 link = sk_psock_init_link();
483 ret = sock_map_link(map, sk);
487 psock = sk_psock(sk);
488 WARN_ON_ONCE(!psock);
490 spin_lock_bh(&stab->lock);
491 osk = stab->sks[idx];
492 if (osk && flags == BPF_NOEXIST) {
495 } else if (!osk && flags == BPF_EXIST) {
500 sock_map_add_link(psock, link, map, &stab->sks[idx]);
503 sock_map_unref(osk, &stab->sks[idx]);
504 spin_unlock_bh(&stab->lock);
507 spin_unlock_bh(&stab->lock);
509 sk_psock_put(sk, psock);
511 sk_psock_free_link(link);
515 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
517 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
518 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
519 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
522 static bool sock_map_redirect_allowed(const struct sock *sk)
525 return sk->sk_state != TCP_LISTEN;
527 return sk->sk_state == TCP_ESTABLISHED;
530 static bool sock_map_sk_is_suitable(const struct sock *sk)
532 return !!sk->sk_prot->psock_update_sk_prot;
535 static bool sock_map_sk_state_allowed(const struct sock *sk)
538 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
542 static int sock_hash_update_common(struct bpf_map *map, void *key,
543 struct sock *sk, u64 flags);
545 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
553 if (map->value_size == sizeof(u64))
560 sock = sockfd_lookup(ufd, &ret);
568 if (!sock_map_sk_is_suitable(sk)) {
573 sock_map_sk_acquire(sk);
574 if (!sock_map_sk_state_allowed(sk))
576 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
577 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
579 ret = sock_hash_update_common(map, key, sk, flags);
580 sock_map_sk_release(sk);
586 static long sock_map_update_elem(struct bpf_map *map, void *key,
587 void *value, u64 flags)
589 struct sock *sk = (struct sock *)value;
592 if (unlikely(!sk || !sk_fullsock(sk)))
595 if (!sock_map_sk_is_suitable(sk))
600 if (!sock_map_sk_state_allowed(sk))
602 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
603 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
605 ret = sock_hash_update_common(map, key, sk, flags);
611 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
612 struct bpf_map *, map, void *, key, u64, flags)
614 WARN_ON_ONCE(!rcu_read_lock_held());
616 if (likely(sock_map_sk_is_suitable(sops->sk) &&
617 sock_map_op_okay(sops)))
618 return sock_map_update_common(map, *(u32 *)key, sops->sk,
623 const struct bpf_func_proto bpf_sock_map_update_proto = {
624 .func = bpf_sock_map_update,
627 .ret_type = RET_INTEGER,
628 .arg1_type = ARG_PTR_TO_CTX,
629 .arg2_type = ARG_CONST_MAP_PTR,
630 .arg3_type = ARG_PTR_TO_MAP_KEY,
631 .arg4_type = ARG_ANYTHING,
634 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
635 struct bpf_map *, map, u32, key, u64, flags)
639 if (unlikely(flags & ~(BPF_F_INGRESS)))
642 sk = __sock_map_lookup_elem(map, key);
643 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
646 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
650 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
651 .func = bpf_sk_redirect_map,
653 .ret_type = RET_INTEGER,
654 .arg1_type = ARG_PTR_TO_CTX,
655 .arg2_type = ARG_CONST_MAP_PTR,
656 .arg3_type = ARG_ANYTHING,
657 .arg4_type = ARG_ANYTHING,
660 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
661 struct bpf_map *, map, u32, key, u64, flags)
665 if (unlikely(flags & ~(BPF_F_INGRESS)))
668 sk = __sock_map_lookup_elem(map, key);
669 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
671 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
679 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
680 .func = bpf_msg_redirect_map,
682 .ret_type = RET_INTEGER,
683 .arg1_type = ARG_PTR_TO_CTX,
684 .arg2_type = ARG_CONST_MAP_PTR,
685 .arg3_type = ARG_ANYTHING,
686 .arg4_type = ARG_ANYTHING,
689 struct sock_map_seq_info {
695 struct bpf_iter__sockmap {
696 __bpf_md_ptr(struct bpf_iter_meta *, meta);
697 __bpf_md_ptr(struct bpf_map *, map);
698 __bpf_md_ptr(void *, key);
699 __bpf_md_ptr(struct sock *, sk);
702 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
703 struct bpf_map *map, void *key,
706 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
708 if (unlikely(info->index >= info->map->max_entries))
711 info->sk = __sock_map_lookup_elem(info->map, info->index);
713 /* can't return sk directly, since that might be NULL */
717 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
720 struct sock_map_seq_info *info = seq->private;
725 /* pairs with sock_map_seq_stop */
727 return sock_map_seq_lookup_elem(info);
730 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
733 struct sock_map_seq_info *info = seq->private;
738 return sock_map_seq_lookup_elem(info);
741 static int sock_map_seq_show(struct seq_file *seq, void *v)
744 struct sock_map_seq_info *info = seq->private;
745 struct bpf_iter__sockmap ctx = {};
746 struct bpf_iter_meta meta;
747 struct bpf_prog *prog;
750 prog = bpf_iter_get_info(&meta, !v);
757 ctx.key = &info->index;
761 return bpf_iter_run_prog(prog, &ctx);
764 static void sock_map_seq_stop(struct seq_file *seq, void *v)
768 (void)sock_map_seq_show(seq, NULL);
770 /* pairs with sock_map_seq_start */
774 static const struct seq_operations sock_map_seq_ops = {
775 .start = sock_map_seq_start,
776 .next = sock_map_seq_next,
777 .stop = sock_map_seq_stop,
778 .show = sock_map_seq_show,
781 static int sock_map_init_seq_private(void *priv_data,
782 struct bpf_iter_aux_info *aux)
784 struct sock_map_seq_info *info = priv_data;
786 bpf_map_inc_with_uref(aux->map);
787 info->map = aux->map;
791 static void sock_map_fini_seq_private(void *priv_data)
793 struct sock_map_seq_info *info = priv_data;
795 bpf_map_put_with_uref(info->map);
798 static u64 sock_map_mem_usage(const struct bpf_map *map)
800 u64 usage = sizeof(struct bpf_stab);
802 usage += (u64)map->max_entries * sizeof(struct sock *);
806 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
807 .seq_ops = &sock_map_seq_ops,
808 .init_seq_private = sock_map_init_seq_private,
809 .fini_seq_private = sock_map_fini_seq_private,
810 .seq_priv_size = sizeof(struct sock_map_seq_info),
813 BTF_ID_LIST_SINGLE(sock_map_btf_ids, struct, bpf_stab)
814 const struct bpf_map_ops sock_map_ops = {
815 .map_meta_equal = bpf_map_meta_equal,
816 .map_alloc = sock_map_alloc,
817 .map_free = sock_map_free,
818 .map_get_next_key = sock_map_get_next_key,
819 .map_lookup_elem_sys_only = sock_map_lookup_sys,
820 .map_update_elem = sock_map_update_elem,
821 .map_delete_elem = sock_map_delete_elem,
822 .map_lookup_elem = sock_map_lookup,
823 .map_release_uref = sock_map_release_progs,
824 .map_check_btf = map_check_no_btf,
825 .map_mem_usage = sock_map_mem_usage,
826 .map_btf_id = &sock_map_btf_ids[0],
827 .iter_seq_info = &sock_map_iter_seq_info,
830 struct bpf_shtab_elem {
834 struct hlist_node node;
838 struct bpf_shtab_bucket {
839 struct hlist_head head;
845 struct bpf_shtab_bucket *buckets;
848 struct sk_psock_progs progs;
852 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
854 return jhash(key, len, 0);
857 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
860 return &htab->buckets[hash & (htab->buckets_num - 1)];
863 static struct bpf_shtab_elem *
864 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
867 struct bpf_shtab_elem *elem;
869 hlist_for_each_entry_rcu(elem, head, node) {
870 if (elem->hash == hash &&
871 !memcmp(&elem->key, key, key_size))
878 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
880 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
881 u32 key_size = map->key_size, hash;
882 struct bpf_shtab_bucket *bucket;
883 struct bpf_shtab_elem *elem;
885 WARN_ON_ONCE(!rcu_read_lock_held());
887 hash = sock_hash_bucket_hash(key, key_size);
888 bucket = sock_hash_select_bucket(htab, hash);
889 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
891 return elem ? elem->sk : NULL;
894 static void sock_hash_free_elem(struct bpf_shtab *htab,
895 struct bpf_shtab_elem *elem)
897 atomic_dec(&htab->count);
898 kfree_rcu(elem, rcu);
901 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
904 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
905 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
906 struct bpf_shtab_bucket *bucket;
908 WARN_ON_ONCE(!rcu_read_lock_held());
909 bucket = sock_hash_select_bucket(htab, elem->hash);
911 /* elem may be deleted in parallel from the map, but access here
912 * is okay since it's going away only after RCU grace period.
913 * However, we need to check whether it's still present.
915 spin_lock_bh(&bucket->lock);
916 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
917 elem->key, map->key_size);
918 if (elem_probe && elem_probe == elem) {
919 hlist_del_rcu(&elem->node);
920 sock_map_unref(elem->sk, elem);
921 sock_hash_free_elem(htab, elem);
923 spin_unlock_bh(&bucket->lock);
926 static long sock_hash_delete_elem(struct bpf_map *map, void *key)
928 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
929 u32 hash, key_size = map->key_size;
930 struct bpf_shtab_bucket *bucket;
931 struct bpf_shtab_elem *elem;
934 hash = sock_hash_bucket_hash(key, key_size);
935 bucket = sock_hash_select_bucket(htab, hash);
937 spin_lock_bh(&bucket->lock);
938 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
940 hlist_del_rcu(&elem->node);
941 sock_map_unref(elem->sk, elem);
942 sock_hash_free_elem(htab, elem);
945 spin_unlock_bh(&bucket->lock);
949 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
950 void *key, u32 key_size,
951 u32 hash, struct sock *sk,
952 struct bpf_shtab_elem *old)
954 struct bpf_shtab_elem *new;
956 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
958 atomic_dec(&htab->count);
959 return ERR_PTR(-E2BIG);
963 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
964 GFP_ATOMIC | __GFP_NOWARN,
965 htab->map.numa_node);
967 atomic_dec(&htab->count);
968 return ERR_PTR(-ENOMEM);
970 memcpy(new->key, key, key_size);
976 static int sock_hash_update_common(struct bpf_map *map, void *key,
977 struct sock *sk, u64 flags)
979 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
980 u32 key_size = map->key_size, hash;
981 struct bpf_shtab_elem *elem, *elem_new;
982 struct bpf_shtab_bucket *bucket;
983 struct sk_psock_link *link;
984 struct sk_psock *psock;
987 WARN_ON_ONCE(!rcu_read_lock_held());
988 if (unlikely(flags > BPF_EXIST))
991 link = sk_psock_init_link();
995 ret = sock_map_link(map, sk);
999 psock = sk_psock(sk);
1000 WARN_ON_ONCE(!psock);
1002 hash = sock_hash_bucket_hash(key, key_size);
1003 bucket = sock_hash_select_bucket(htab, hash);
1005 spin_lock_bh(&bucket->lock);
1006 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
1007 if (elem && flags == BPF_NOEXIST) {
1010 } else if (!elem && flags == BPF_EXIST) {
1015 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
1016 if (IS_ERR(elem_new)) {
1017 ret = PTR_ERR(elem_new);
1021 sock_map_add_link(psock, link, map, elem_new);
1022 /* Add new element to the head of the list, so that
1023 * concurrent search will find it before old elem.
1025 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1027 hlist_del_rcu(&elem->node);
1028 sock_map_unref(elem->sk, elem);
1029 sock_hash_free_elem(htab, elem);
1031 spin_unlock_bh(&bucket->lock);
1034 spin_unlock_bh(&bucket->lock);
1035 sk_psock_put(sk, psock);
1037 sk_psock_free_link(link);
1041 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1044 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1045 struct bpf_shtab_elem *elem, *elem_next;
1046 u32 hash, key_size = map->key_size;
1047 struct hlist_head *head;
1051 goto find_first_elem;
1052 hash = sock_hash_bucket_hash(key, key_size);
1053 head = &sock_hash_select_bucket(htab, hash)->head;
1054 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1056 goto find_first_elem;
1058 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1059 struct bpf_shtab_elem, node);
1061 memcpy(key_next, elem_next->key, key_size);
1065 i = hash & (htab->buckets_num - 1);
1068 for (; i < htab->buckets_num; i++) {
1069 head = &sock_hash_select_bucket(htab, i)->head;
1070 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1071 struct bpf_shtab_elem, node);
1073 memcpy(key_next, elem_next->key, key_size);
1081 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1083 struct bpf_shtab *htab;
1086 if (attr->max_entries == 0 ||
1087 attr->key_size == 0 ||
1088 (attr->value_size != sizeof(u32) &&
1089 attr->value_size != sizeof(u64)) ||
1090 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1091 return ERR_PTR(-EINVAL);
1092 if (attr->key_size > MAX_BPF_STACK)
1093 return ERR_PTR(-E2BIG);
1095 htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
1097 return ERR_PTR(-ENOMEM);
1099 bpf_map_init_from_attr(&htab->map, attr);
1101 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1102 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1103 round_up(htab->map.key_size, 8);
1104 if (htab->buckets_num == 0 ||
1105 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1110 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1111 sizeof(struct bpf_shtab_bucket),
1112 htab->map.numa_node);
1113 if (!htab->buckets) {
1118 for (i = 0; i < htab->buckets_num; i++) {
1119 INIT_HLIST_HEAD(&htab->buckets[i].head);
1120 spin_lock_init(&htab->buckets[i].lock);
1125 bpf_map_area_free(htab);
1126 return ERR_PTR(err);
1129 static void sock_hash_free(struct bpf_map *map)
1131 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1132 struct bpf_shtab_bucket *bucket;
1133 struct hlist_head unlink_list;
1134 struct bpf_shtab_elem *elem;
1135 struct hlist_node *node;
1138 /* After the sync no updates or deletes will be in-flight so it
1139 * is safe to walk map and remove entries without risking a race
1140 * in EEXIST update case.
1143 for (i = 0; i < htab->buckets_num; i++) {
1144 bucket = sock_hash_select_bucket(htab, i);
1146 /* We are racing with sock_hash_delete_from_link to
1147 * enter the spin-lock critical section. Every socket on
1148 * the list is still linked to sockhash. Since link
1149 * exists, psock exists and holds a ref to socket. That
1150 * lets us to grab a socket ref too.
1152 spin_lock_bh(&bucket->lock);
1153 hlist_for_each_entry(elem, &bucket->head, node)
1154 sock_hold(elem->sk);
1155 hlist_move_list(&bucket->head, &unlink_list);
1156 spin_unlock_bh(&bucket->lock);
1158 /* Process removed entries out of atomic context to
1159 * block for socket lock before deleting the psock's
1162 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1163 hlist_del(&elem->node);
1164 lock_sock(elem->sk);
1166 sock_map_unref(elem->sk, elem);
1168 release_sock(elem->sk);
1170 sock_hash_free_elem(htab, elem);
1174 /* wait for psock readers accessing its map link */
1177 bpf_map_area_free(htab->buckets);
1178 bpf_map_area_free(htab);
1181 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1185 if (map->value_size != sizeof(u64))
1186 return ERR_PTR(-ENOSPC);
1188 sk = __sock_hash_lookup_elem(map, key);
1190 return ERR_PTR(-ENOENT);
1192 __sock_gen_cookie(sk);
1193 return &sk->sk_cookie;
1196 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1200 sk = __sock_hash_lookup_elem(map, key);
1203 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1208 static void sock_hash_release_progs(struct bpf_map *map)
1210 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1213 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1214 struct bpf_map *, map, void *, key, u64, flags)
1216 WARN_ON_ONCE(!rcu_read_lock_held());
1218 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1219 sock_map_op_okay(sops)))
1220 return sock_hash_update_common(map, key, sops->sk, flags);
1224 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1225 .func = bpf_sock_hash_update,
1228 .ret_type = RET_INTEGER,
1229 .arg1_type = ARG_PTR_TO_CTX,
1230 .arg2_type = ARG_CONST_MAP_PTR,
1231 .arg3_type = ARG_PTR_TO_MAP_KEY,
1232 .arg4_type = ARG_ANYTHING,
1235 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1236 struct bpf_map *, map, void *, key, u64, flags)
1240 if (unlikely(flags & ~(BPF_F_INGRESS)))
1243 sk = __sock_hash_lookup_elem(map, key);
1244 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1247 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
1251 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1252 .func = bpf_sk_redirect_hash,
1254 .ret_type = RET_INTEGER,
1255 .arg1_type = ARG_PTR_TO_CTX,
1256 .arg2_type = ARG_CONST_MAP_PTR,
1257 .arg3_type = ARG_PTR_TO_MAP_KEY,
1258 .arg4_type = ARG_ANYTHING,
1261 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1262 struct bpf_map *, map, void *, key, u64, flags)
1266 if (unlikely(flags & ~(BPF_F_INGRESS)))
1269 sk = __sock_hash_lookup_elem(map, key);
1270 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1272 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
1280 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1281 .func = bpf_msg_redirect_hash,
1283 .ret_type = RET_INTEGER,
1284 .arg1_type = ARG_PTR_TO_CTX,
1285 .arg2_type = ARG_CONST_MAP_PTR,
1286 .arg3_type = ARG_PTR_TO_MAP_KEY,
1287 .arg4_type = ARG_ANYTHING,
1290 struct sock_hash_seq_info {
1291 struct bpf_map *map;
1292 struct bpf_shtab *htab;
1296 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1297 struct bpf_shtab_elem *prev_elem)
1299 const struct bpf_shtab *htab = info->htab;
1300 struct bpf_shtab_bucket *bucket;
1301 struct bpf_shtab_elem *elem;
1302 struct hlist_node *node;
1304 /* try to find next elem in the same bucket */
1306 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1307 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1311 /* no more elements, continue in the next bucket */
1315 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1316 bucket = &htab->buckets[info->bucket_id];
1317 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1318 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1326 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1329 struct sock_hash_seq_info *info = seq->private;
1334 /* pairs with sock_hash_seq_stop */
1336 return sock_hash_seq_find_next(info, NULL);
1339 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1342 struct sock_hash_seq_info *info = seq->private;
1345 return sock_hash_seq_find_next(info, v);
1348 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1351 struct sock_hash_seq_info *info = seq->private;
1352 struct bpf_iter__sockmap ctx = {};
1353 struct bpf_shtab_elem *elem = v;
1354 struct bpf_iter_meta meta;
1355 struct bpf_prog *prog;
1358 prog = bpf_iter_get_info(&meta, !elem);
1363 ctx.map = info->map;
1365 ctx.key = elem->key;
1369 return bpf_iter_run_prog(prog, &ctx);
1372 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1376 (void)sock_hash_seq_show(seq, NULL);
1378 /* pairs with sock_hash_seq_start */
1382 static const struct seq_operations sock_hash_seq_ops = {
1383 .start = sock_hash_seq_start,
1384 .next = sock_hash_seq_next,
1385 .stop = sock_hash_seq_stop,
1386 .show = sock_hash_seq_show,
1389 static int sock_hash_init_seq_private(void *priv_data,
1390 struct bpf_iter_aux_info *aux)
1392 struct sock_hash_seq_info *info = priv_data;
1394 bpf_map_inc_with_uref(aux->map);
1395 info->map = aux->map;
1396 info->htab = container_of(aux->map, struct bpf_shtab, map);
1400 static void sock_hash_fini_seq_private(void *priv_data)
1402 struct sock_hash_seq_info *info = priv_data;
1404 bpf_map_put_with_uref(info->map);
1407 static u64 sock_hash_mem_usage(const struct bpf_map *map)
1409 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1410 u64 usage = sizeof(*htab);
1412 usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket);
1413 usage += atomic_read(&htab->count) * (u64)htab->elem_size;
1417 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1418 .seq_ops = &sock_hash_seq_ops,
1419 .init_seq_private = sock_hash_init_seq_private,
1420 .fini_seq_private = sock_hash_fini_seq_private,
1421 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1424 BTF_ID_LIST_SINGLE(sock_hash_map_btf_ids, struct, bpf_shtab)
1425 const struct bpf_map_ops sock_hash_ops = {
1426 .map_meta_equal = bpf_map_meta_equal,
1427 .map_alloc = sock_hash_alloc,
1428 .map_free = sock_hash_free,
1429 .map_get_next_key = sock_hash_get_next_key,
1430 .map_update_elem = sock_map_update_elem,
1431 .map_delete_elem = sock_hash_delete_elem,
1432 .map_lookup_elem = sock_hash_lookup,
1433 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1434 .map_release_uref = sock_hash_release_progs,
1435 .map_check_btf = map_check_no_btf,
1436 .map_mem_usage = sock_hash_mem_usage,
1437 .map_btf_id = &sock_hash_map_btf_ids[0],
1438 .iter_seq_info = &sock_hash_iter_seq_info,
1441 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1443 switch (map->map_type) {
1444 case BPF_MAP_TYPE_SOCKMAP:
1445 return &container_of(map, struct bpf_stab, map)->progs;
1446 case BPF_MAP_TYPE_SOCKHASH:
1447 return &container_of(map, struct bpf_shtab, map)->progs;
1455 static int sock_map_prog_lookup(struct bpf_map *map, struct bpf_prog ***pprog,
1458 struct sk_psock_progs *progs = sock_map_progs(map);
1464 case BPF_SK_MSG_VERDICT:
1465 *pprog = &progs->msg_parser;
1467 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1468 case BPF_SK_SKB_STREAM_PARSER:
1469 *pprog = &progs->stream_parser;
1472 case BPF_SK_SKB_STREAM_VERDICT:
1473 if (progs->skb_verdict)
1475 *pprog = &progs->stream_verdict;
1477 case BPF_SK_SKB_VERDICT:
1478 if (progs->stream_verdict)
1480 *pprog = &progs->skb_verdict;
1489 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1490 struct bpf_prog *old, u32 which)
1492 struct bpf_prog **pprog;
1495 ret = sock_map_prog_lookup(map, &pprog, which);
1500 return psock_replace_prog(pprog, prog, old);
1502 psock_set_prog(pprog, prog);
1506 int sock_map_bpf_prog_query(const union bpf_attr *attr,
1507 union bpf_attr __user *uattr)
1509 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
1510 u32 prog_cnt = 0, flags = 0, ufd = attr->target_fd;
1511 struct bpf_prog **pprog;
1512 struct bpf_prog *prog;
1513 struct bpf_map *map;
1518 if (attr->query.query_flags)
1522 map = __bpf_map_get(f);
1524 return PTR_ERR(map);
1528 ret = sock_map_prog_lookup(map, &pprog, attr->query.attach_type);
1533 prog_cnt = !prog ? 0 : 1;
1535 if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
1538 /* we do not hold the refcnt, the bpf prog may be released
1539 * asynchronously and the id would be set to 0.
1541 id = data_race(prog->aux->id);
1548 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)) ||
1549 (id != 0 && copy_to_user(prog_ids, &id, sizeof(u32))) ||
1550 copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
1557 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1559 switch (link->map->map_type) {
1560 case BPF_MAP_TYPE_SOCKMAP:
1561 return sock_map_delete_from_link(link->map, sk,
1563 case BPF_MAP_TYPE_SOCKHASH:
1564 return sock_hash_delete_from_link(link->map, sk,
1571 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1573 struct sk_psock_link *link;
1575 while ((link = sk_psock_link_pop(psock))) {
1576 sock_map_unlink(sk, link);
1577 sk_psock_free_link(link);
1581 void sock_map_unhash(struct sock *sk)
1583 void (*saved_unhash)(struct sock *sk);
1584 struct sk_psock *psock;
1587 psock = sk_psock(sk);
1588 if (unlikely(!psock)) {
1590 saved_unhash = READ_ONCE(sk->sk_prot)->unhash;
1592 saved_unhash = psock->saved_unhash;
1593 sock_map_remove_links(sk, psock);
1596 if (WARN_ON_ONCE(saved_unhash == sock_map_unhash))
1601 EXPORT_SYMBOL_GPL(sock_map_unhash);
1603 void sock_map_destroy(struct sock *sk)
1605 void (*saved_destroy)(struct sock *sk);
1606 struct sk_psock *psock;
1609 psock = sk_psock_get(sk);
1610 if (unlikely(!psock)) {
1612 saved_destroy = READ_ONCE(sk->sk_prot)->destroy;
1614 saved_destroy = psock->saved_destroy;
1615 sock_map_remove_links(sk, psock);
1617 sk_psock_stop(psock);
1618 sk_psock_put(sk, psock);
1620 if (WARN_ON_ONCE(saved_destroy == sock_map_destroy))
1625 EXPORT_SYMBOL_GPL(sock_map_destroy);
1627 void sock_map_close(struct sock *sk, long timeout)
1629 void (*saved_close)(struct sock *sk, long timeout);
1630 struct sk_psock *psock;
1634 psock = sk_psock_get(sk);
1635 if (unlikely(!psock)) {
1638 saved_close = READ_ONCE(sk->sk_prot)->close;
1640 saved_close = psock->saved_close;
1641 sock_map_remove_links(sk, psock);
1643 sk_psock_stop(psock);
1645 cancel_delayed_work_sync(&psock->work);
1646 sk_psock_put(sk, psock);
1649 /* Make sure we do not recurse. This is a bug.
1650 * Leak the socket instead of crashing on a stack overflow.
1652 if (WARN_ON_ONCE(saved_close == sock_map_close))
1654 saved_close(sk, timeout);
1656 EXPORT_SYMBOL_GPL(sock_map_close);
1658 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1659 union bpf_iter_link_info *linfo,
1660 struct bpf_iter_aux_info *aux)
1662 struct bpf_map *map;
1665 if (!linfo->map.map_fd)
1668 map = bpf_map_get_with_uref(linfo->map.map_fd);
1670 return PTR_ERR(map);
1672 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1673 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1676 if (prog->aux->max_rdonly_access > map->key_size) {
1685 bpf_map_put_with_uref(map);
1689 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1691 bpf_map_put_with_uref(aux->map);
1694 static struct bpf_iter_reg sock_map_iter_reg = {
1695 .target = "sockmap",
1696 .attach_target = sock_map_iter_attach_target,
1697 .detach_target = sock_map_iter_detach_target,
1698 .show_fdinfo = bpf_iter_map_show_fdinfo,
1699 .fill_link_info = bpf_iter_map_fill_link_info,
1700 .ctx_arg_info_size = 2,
1702 { offsetof(struct bpf_iter__sockmap, key),
1703 PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
1704 { offsetof(struct bpf_iter__sockmap, sk),
1705 PTR_TO_BTF_ID_OR_NULL },
1709 static int __init bpf_sockmap_iter_init(void)
1711 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1712 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1713 return bpf_iter_reg_target(&sock_map_iter_reg);
1715 late_initcall(bpf_sockmap_iter_init);