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 (!capable(CAP_NET_ADMIN))
36 return ERR_PTR(-EPERM);
37 if (attr->max_entries == 0 ||
38 attr->key_size != 4 ||
39 (attr->value_size != sizeof(u32) &&
40 attr->value_size != sizeof(u64)) ||
41 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
42 return ERR_PTR(-EINVAL);
44 stab = bpf_map_area_alloc(sizeof(*stab), NUMA_NO_NODE);
46 return ERR_PTR(-ENOMEM);
48 bpf_map_init_from_attr(&stab->map, attr);
49 raw_spin_lock_init(&stab->lock);
51 stab->sks = bpf_map_area_alloc((u64) stab->map.max_entries *
52 sizeof(struct sock *),
55 bpf_map_area_free(stab);
56 return ERR_PTR(-ENOMEM);
62 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
64 u32 ufd = attr->target_fd;
69 if (attr->attach_flags || attr->replace_bpf_fd)
73 map = __bpf_map_get(f);
76 ret = sock_map_prog_update(map, prog, NULL, attr->attach_type);
81 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
83 u32 ufd = attr->target_fd;
84 struct bpf_prog *prog;
89 if (attr->attach_flags || attr->replace_bpf_fd)
93 map = __bpf_map_get(f);
97 prog = bpf_prog_get(attr->attach_bpf_fd);
103 if (prog->type != ptype) {
108 ret = sock_map_prog_update(map, NULL, prog, attr->attach_type);
116 static void sock_map_sk_acquire(struct sock *sk)
117 __acquires(&sk->sk_lock.slock)
124 static void sock_map_sk_release(struct sock *sk)
125 __releases(&sk->sk_lock.slock)
132 static void sock_map_add_link(struct sk_psock *psock,
133 struct sk_psock_link *link,
134 struct bpf_map *map, void *link_raw)
136 link->link_raw = link_raw;
138 spin_lock_bh(&psock->link_lock);
139 list_add_tail(&link->list, &psock->link);
140 spin_unlock_bh(&psock->link_lock);
143 static void sock_map_del_link(struct sock *sk,
144 struct sk_psock *psock, void *link_raw)
146 bool strp_stop = false, verdict_stop = false;
147 struct sk_psock_link *link, *tmp;
149 spin_lock_bh(&psock->link_lock);
150 list_for_each_entry_safe(link, tmp, &psock->link, list) {
151 if (link->link_raw == link_raw) {
152 struct bpf_map *map = link->map;
153 struct bpf_stab *stab = container_of(map, struct bpf_stab,
155 if (psock->saved_data_ready && stab->progs.stream_parser)
157 if (psock->saved_data_ready && stab->progs.stream_verdict)
159 if (psock->saved_data_ready && stab->progs.skb_verdict)
161 list_del(&link->list);
162 sk_psock_free_link(link);
165 spin_unlock_bh(&psock->link_lock);
166 if (strp_stop || verdict_stop) {
167 write_lock_bh(&sk->sk_callback_lock);
169 sk_psock_stop_strp(sk, psock);
171 sk_psock_stop_verdict(sk, psock);
173 if (psock->psock_update_sk_prot)
174 psock->psock_update_sk_prot(sk, psock, false);
175 write_unlock_bh(&sk->sk_callback_lock);
179 static void sock_map_unref(struct sock *sk, void *link_raw)
181 struct sk_psock *psock = sk_psock(sk);
184 sock_map_del_link(sk, psock, link_raw);
185 sk_psock_put(sk, psock);
189 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
191 if (!sk->sk_prot->psock_update_sk_prot)
193 psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
194 return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
197 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
199 struct sk_psock *psock;
202 psock = sk_psock(sk);
204 if (sk->sk_prot->close != sock_map_close) {
205 psock = ERR_PTR(-EBUSY);
209 if (!refcount_inc_not_zero(&psock->refcnt))
210 psock = ERR_PTR(-EBUSY);
217 static int sock_map_link(struct bpf_map *map, struct sock *sk)
219 struct sk_psock_progs *progs = sock_map_progs(map);
220 struct bpf_prog *stream_verdict = NULL;
221 struct bpf_prog *stream_parser = NULL;
222 struct bpf_prog *skb_verdict = NULL;
223 struct bpf_prog *msg_parser = NULL;
224 struct sk_psock *psock;
227 stream_verdict = READ_ONCE(progs->stream_verdict);
228 if (stream_verdict) {
229 stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
230 if (IS_ERR(stream_verdict))
231 return PTR_ERR(stream_verdict);
234 stream_parser = READ_ONCE(progs->stream_parser);
236 stream_parser = bpf_prog_inc_not_zero(stream_parser);
237 if (IS_ERR(stream_parser)) {
238 ret = PTR_ERR(stream_parser);
239 goto out_put_stream_verdict;
243 msg_parser = READ_ONCE(progs->msg_parser);
245 msg_parser = bpf_prog_inc_not_zero(msg_parser);
246 if (IS_ERR(msg_parser)) {
247 ret = PTR_ERR(msg_parser);
248 goto out_put_stream_parser;
252 skb_verdict = READ_ONCE(progs->skb_verdict);
254 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
255 if (IS_ERR(skb_verdict)) {
256 ret = PTR_ERR(skb_verdict);
257 goto out_put_msg_parser;
261 psock = sock_map_psock_get_checked(sk);
263 ret = PTR_ERR(psock);
268 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
269 (stream_parser && READ_ONCE(psock->progs.stream_parser)) ||
270 (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
271 (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) ||
272 (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
273 (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) {
274 sk_psock_put(sk, psock);
279 psock = sk_psock_init(sk, map->numa_node);
281 ret = PTR_ERR(psock);
287 psock_set_prog(&psock->progs.msg_parser, msg_parser);
289 psock_set_prog(&psock->progs.stream_parser, stream_parser);
291 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
293 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
295 /* msg_* and stream_* programs references tracked in psock after this
296 * point. Reference dec and cleanup will occur through psock destructor
298 ret = sock_map_init_proto(sk, psock);
300 sk_psock_put(sk, psock);
304 write_lock_bh(&sk->sk_callback_lock);
305 if (stream_parser && stream_verdict && !psock->saved_data_ready) {
306 ret = sk_psock_init_strp(sk, psock);
308 write_unlock_bh(&sk->sk_callback_lock);
309 sk_psock_put(sk, psock);
312 sk_psock_start_strp(sk, psock);
313 } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
314 sk_psock_start_verdict(sk,psock);
315 } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
316 sk_psock_start_verdict(sk, psock);
318 write_unlock_bh(&sk->sk_callback_lock);
322 bpf_prog_put(skb_verdict);
325 bpf_prog_put(msg_parser);
326 out_put_stream_parser:
328 bpf_prog_put(stream_parser);
329 out_put_stream_verdict:
331 bpf_prog_put(stream_verdict);
336 static void sock_map_free(struct bpf_map *map)
338 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
341 /* After the sync no updates or deletes will be in-flight so it
342 * is safe to walk map and remove entries without risking a race
343 * in EEXIST update case.
346 for (i = 0; i < stab->map.max_entries; i++) {
347 struct sock **psk = &stab->sks[i];
350 sk = xchg(psk, NULL);
355 sock_map_unref(sk, psk);
362 /* wait for psock readers accessing its map link */
365 bpf_map_area_free(stab->sks);
366 bpf_map_area_free(stab);
369 static void sock_map_release_progs(struct bpf_map *map)
371 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
374 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
376 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
378 WARN_ON_ONCE(!rcu_read_lock_held());
380 if (unlikely(key >= map->max_entries))
382 return READ_ONCE(stab->sks[key]);
385 static void *sock_map_lookup(struct bpf_map *map, void *key)
389 sk = __sock_map_lookup_elem(map, *(u32 *)key);
392 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
397 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
401 if (map->value_size != sizeof(u64))
402 return ERR_PTR(-ENOSPC);
404 sk = __sock_map_lookup_elem(map, *(u32 *)key);
406 return ERR_PTR(-ENOENT);
408 __sock_gen_cookie(sk);
409 return &sk->sk_cookie;
412 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
418 raw_spin_lock_bh(&stab->lock);
420 if (!sk_test || sk_test == sk)
421 sk = xchg(psk, NULL);
424 sock_map_unref(sk, psk);
428 raw_spin_unlock_bh(&stab->lock);
432 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
435 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
437 __sock_map_delete(stab, sk, link_raw);
440 static long sock_map_delete_elem(struct bpf_map *map, void *key)
442 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
446 if (unlikely(i >= map->max_entries))
450 return __sock_map_delete(stab, NULL, psk);
453 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
455 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
456 u32 i = key ? *(u32 *)key : U32_MAX;
457 u32 *key_next = next;
459 if (i == stab->map.max_entries - 1)
461 if (i >= stab->map.max_entries)
468 static int sock_map_update_common(struct bpf_map *map, u32 idx,
469 struct sock *sk, u64 flags)
471 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
472 struct sk_psock_link *link;
473 struct sk_psock *psock;
477 WARN_ON_ONCE(!rcu_read_lock_held());
478 if (unlikely(flags > BPF_EXIST))
480 if (unlikely(idx >= map->max_entries))
483 link = sk_psock_init_link();
487 ret = sock_map_link(map, sk);
491 psock = sk_psock(sk);
492 WARN_ON_ONCE(!psock);
494 raw_spin_lock_bh(&stab->lock);
495 osk = stab->sks[idx];
496 if (osk && flags == BPF_NOEXIST) {
499 } else if (!osk && flags == BPF_EXIST) {
504 sock_map_add_link(psock, link, map, &stab->sks[idx]);
507 sock_map_unref(osk, &stab->sks[idx]);
508 raw_spin_unlock_bh(&stab->lock);
511 raw_spin_unlock_bh(&stab->lock);
513 sk_psock_put(sk, psock);
515 sk_psock_free_link(link);
519 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
521 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
522 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
523 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
526 static bool sock_map_redirect_allowed(const struct sock *sk)
529 return sk->sk_state != TCP_LISTEN;
531 return sk->sk_state == TCP_ESTABLISHED;
534 static bool sock_map_sk_is_suitable(const struct sock *sk)
536 return !!sk->sk_prot->psock_update_sk_prot;
539 static bool sock_map_sk_state_allowed(const struct sock *sk)
542 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
546 static int sock_hash_update_common(struct bpf_map *map, void *key,
547 struct sock *sk, u64 flags);
549 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
557 if (map->value_size == sizeof(u64))
564 sock = sockfd_lookup(ufd, &ret);
572 if (!sock_map_sk_is_suitable(sk)) {
577 sock_map_sk_acquire(sk);
578 if (!sock_map_sk_state_allowed(sk))
580 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
581 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
583 ret = sock_hash_update_common(map, key, sk, flags);
584 sock_map_sk_release(sk);
590 static long sock_map_update_elem(struct bpf_map *map, void *key,
591 void *value, u64 flags)
593 struct sock *sk = (struct sock *)value;
596 if (unlikely(!sk || !sk_fullsock(sk)))
599 if (!sock_map_sk_is_suitable(sk))
604 if (!sock_map_sk_state_allowed(sk))
606 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
607 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
609 ret = sock_hash_update_common(map, key, sk, flags);
615 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
616 struct bpf_map *, map, void *, key, u64, flags)
618 WARN_ON_ONCE(!rcu_read_lock_held());
620 if (likely(sock_map_sk_is_suitable(sops->sk) &&
621 sock_map_op_okay(sops)))
622 return sock_map_update_common(map, *(u32 *)key, sops->sk,
627 const struct bpf_func_proto bpf_sock_map_update_proto = {
628 .func = bpf_sock_map_update,
631 .ret_type = RET_INTEGER,
632 .arg1_type = ARG_PTR_TO_CTX,
633 .arg2_type = ARG_CONST_MAP_PTR,
634 .arg3_type = ARG_PTR_TO_MAP_KEY,
635 .arg4_type = ARG_ANYTHING,
638 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
639 struct bpf_map *, map, u32, key, u64, flags)
643 if (unlikely(flags & ~(BPF_F_INGRESS)))
646 sk = __sock_map_lookup_elem(map, key);
647 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
650 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
654 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
655 .func = bpf_sk_redirect_map,
657 .ret_type = RET_INTEGER,
658 .arg1_type = ARG_PTR_TO_CTX,
659 .arg2_type = ARG_CONST_MAP_PTR,
660 .arg3_type = ARG_ANYTHING,
661 .arg4_type = ARG_ANYTHING,
664 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
665 struct bpf_map *, map, u32, key, u64, flags)
669 if (unlikely(flags & ~(BPF_F_INGRESS)))
672 sk = __sock_map_lookup_elem(map, key);
673 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
681 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
682 .func = bpf_msg_redirect_map,
684 .ret_type = RET_INTEGER,
685 .arg1_type = ARG_PTR_TO_CTX,
686 .arg2_type = ARG_CONST_MAP_PTR,
687 .arg3_type = ARG_ANYTHING,
688 .arg4_type = ARG_ANYTHING,
691 struct sock_map_seq_info {
697 struct bpf_iter__sockmap {
698 __bpf_md_ptr(struct bpf_iter_meta *, meta);
699 __bpf_md_ptr(struct bpf_map *, map);
700 __bpf_md_ptr(void *, key);
701 __bpf_md_ptr(struct sock *, sk);
704 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
705 struct bpf_map *map, void *key,
708 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
710 if (unlikely(info->index >= info->map->max_entries))
713 info->sk = __sock_map_lookup_elem(info->map, info->index);
715 /* can't return sk directly, since that might be NULL */
719 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
722 struct sock_map_seq_info *info = seq->private;
727 /* pairs with sock_map_seq_stop */
729 return sock_map_seq_lookup_elem(info);
732 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
735 struct sock_map_seq_info *info = seq->private;
740 return sock_map_seq_lookup_elem(info);
743 static int sock_map_seq_show(struct seq_file *seq, void *v)
746 struct sock_map_seq_info *info = seq->private;
747 struct bpf_iter__sockmap ctx = {};
748 struct bpf_iter_meta meta;
749 struct bpf_prog *prog;
752 prog = bpf_iter_get_info(&meta, !v);
759 ctx.key = &info->index;
763 return bpf_iter_run_prog(prog, &ctx);
766 static void sock_map_seq_stop(struct seq_file *seq, void *v)
770 (void)sock_map_seq_show(seq, NULL);
772 /* pairs with sock_map_seq_start */
776 static const struct seq_operations sock_map_seq_ops = {
777 .start = sock_map_seq_start,
778 .next = sock_map_seq_next,
779 .stop = sock_map_seq_stop,
780 .show = sock_map_seq_show,
783 static int sock_map_init_seq_private(void *priv_data,
784 struct bpf_iter_aux_info *aux)
786 struct sock_map_seq_info *info = priv_data;
788 bpf_map_inc_with_uref(aux->map);
789 info->map = aux->map;
793 static void sock_map_fini_seq_private(void *priv_data)
795 struct sock_map_seq_info *info = priv_data;
797 bpf_map_put_with_uref(info->map);
800 static u64 sock_map_mem_usage(const struct bpf_map *map)
802 u64 usage = sizeof(struct bpf_stab);
804 usage += (u64)map->max_entries * sizeof(struct sock *);
808 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
809 .seq_ops = &sock_map_seq_ops,
810 .init_seq_private = sock_map_init_seq_private,
811 .fini_seq_private = sock_map_fini_seq_private,
812 .seq_priv_size = sizeof(struct sock_map_seq_info),
815 BTF_ID_LIST_SINGLE(sock_map_btf_ids, struct, bpf_stab)
816 const struct bpf_map_ops sock_map_ops = {
817 .map_meta_equal = bpf_map_meta_equal,
818 .map_alloc = sock_map_alloc,
819 .map_free = sock_map_free,
820 .map_get_next_key = sock_map_get_next_key,
821 .map_lookup_elem_sys_only = sock_map_lookup_sys,
822 .map_update_elem = sock_map_update_elem,
823 .map_delete_elem = sock_map_delete_elem,
824 .map_lookup_elem = sock_map_lookup,
825 .map_release_uref = sock_map_release_progs,
826 .map_check_btf = map_check_no_btf,
827 .map_mem_usage = sock_map_mem_usage,
828 .map_btf_id = &sock_map_btf_ids[0],
829 .iter_seq_info = &sock_map_iter_seq_info,
832 struct bpf_shtab_elem {
836 struct hlist_node node;
840 struct bpf_shtab_bucket {
841 struct hlist_head head;
847 struct bpf_shtab_bucket *buckets;
850 struct sk_psock_progs progs;
854 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
856 return jhash(key, len, 0);
859 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
862 return &htab->buckets[hash & (htab->buckets_num - 1)];
865 static struct bpf_shtab_elem *
866 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
869 struct bpf_shtab_elem *elem;
871 hlist_for_each_entry_rcu(elem, head, node) {
872 if (elem->hash == hash &&
873 !memcmp(&elem->key, key, key_size))
880 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
882 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
883 u32 key_size = map->key_size, hash;
884 struct bpf_shtab_bucket *bucket;
885 struct bpf_shtab_elem *elem;
887 WARN_ON_ONCE(!rcu_read_lock_held());
889 hash = sock_hash_bucket_hash(key, key_size);
890 bucket = sock_hash_select_bucket(htab, hash);
891 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
893 return elem ? elem->sk : NULL;
896 static void sock_hash_free_elem(struct bpf_shtab *htab,
897 struct bpf_shtab_elem *elem)
899 atomic_dec(&htab->count);
900 kfree_rcu(elem, rcu);
903 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
906 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
907 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
908 struct bpf_shtab_bucket *bucket;
910 WARN_ON_ONCE(!rcu_read_lock_held());
911 bucket = sock_hash_select_bucket(htab, elem->hash);
913 /* elem may be deleted in parallel from the map, but access here
914 * is okay since it's going away only after RCU grace period.
915 * However, we need to check whether it's still present.
917 raw_spin_lock_bh(&bucket->lock);
918 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
919 elem->key, map->key_size);
920 if (elem_probe && elem_probe == elem) {
921 hlist_del_rcu(&elem->node);
922 sock_map_unref(elem->sk, elem);
923 sock_hash_free_elem(htab, elem);
925 raw_spin_unlock_bh(&bucket->lock);
928 static long sock_hash_delete_elem(struct bpf_map *map, void *key)
930 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
931 u32 hash, key_size = map->key_size;
932 struct bpf_shtab_bucket *bucket;
933 struct bpf_shtab_elem *elem;
936 hash = sock_hash_bucket_hash(key, key_size);
937 bucket = sock_hash_select_bucket(htab, hash);
939 raw_spin_lock_bh(&bucket->lock);
940 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
942 hlist_del_rcu(&elem->node);
943 sock_map_unref(elem->sk, elem);
944 sock_hash_free_elem(htab, elem);
947 raw_spin_unlock_bh(&bucket->lock);
951 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
952 void *key, u32 key_size,
953 u32 hash, struct sock *sk,
954 struct bpf_shtab_elem *old)
956 struct bpf_shtab_elem *new;
958 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
960 atomic_dec(&htab->count);
961 return ERR_PTR(-E2BIG);
965 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
966 GFP_ATOMIC | __GFP_NOWARN,
967 htab->map.numa_node);
969 atomic_dec(&htab->count);
970 return ERR_PTR(-ENOMEM);
972 memcpy(new->key, key, key_size);
978 static int sock_hash_update_common(struct bpf_map *map, void *key,
979 struct sock *sk, u64 flags)
981 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
982 u32 key_size = map->key_size, hash;
983 struct bpf_shtab_elem *elem, *elem_new;
984 struct bpf_shtab_bucket *bucket;
985 struct sk_psock_link *link;
986 struct sk_psock *psock;
989 WARN_ON_ONCE(!rcu_read_lock_held());
990 if (unlikely(flags > BPF_EXIST))
993 link = sk_psock_init_link();
997 ret = sock_map_link(map, sk);
1001 psock = sk_psock(sk);
1002 WARN_ON_ONCE(!psock);
1004 hash = sock_hash_bucket_hash(key, key_size);
1005 bucket = sock_hash_select_bucket(htab, hash);
1007 raw_spin_lock_bh(&bucket->lock);
1008 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
1009 if (elem && flags == BPF_NOEXIST) {
1012 } else if (!elem && flags == BPF_EXIST) {
1017 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
1018 if (IS_ERR(elem_new)) {
1019 ret = PTR_ERR(elem_new);
1023 sock_map_add_link(psock, link, map, elem_new);
1024 /* Add new element to the head of the list, so that
1025 * concurrent search will find it before old elem.
1027 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1029 hlist_del_rcu(&elem->node);
1030 sock_map_unref(elem->sk, elem);
1031 sock_hash_free_elem(htab, elem);
1033 raw_spin_unlock_bh(&bucket->lock);
1036 raw_spin_unlock_bh(&bucket->lock);
1037 sk_psock_put(sk, psock);
1039 sk_psock_free_link(link);
1043 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1046 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1047 struct bpf_shtab_elem *elem, *elem_next;
1048 u32 hash, key_size = map->key_size;
1049 struct hlist_head *head;
1053 goto find_first_elem;
1054 hash = sock_hash_bucket_hash(key, key_size);
1055 head = &sock_hash_select_bucket(htab, hash)->head;
1056 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1058 goto find_first_elem;
1060 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1061 struct bpf_shtab_elem, node);
1063 memcpy(key_next, elem_next->key, key_size);
1067 i = hash & (htab->buckets_num - 1);
1070 for (; i < htab->buckets_num; i++) {
1071 head = &sock_hash_select_bucket(htab, i)->head;
1072 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1073 struct bpf_shtab_elem, node);
1075 memcpy(key_next, elem_next->key, key_size);
1083 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1085 struct bpf_shtab *htab;
1088 if (!capable(CAP_NET_ADMIN))
1089 return ERR_PTR(-EPERM);
1090 if (attr->max_entries == 0 ||
1091 attr->key_size == 0 ||
1092 (attr->value_size != sizeof(u32) &&
1093 attr->value_size != sizeof(u64)) ||
1094 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1095 return ERR_PTR(-EINVAL);
1096 if (attr->key_size > MAX_BPF_STACK)
1097 return ERR_PTR(-E2BIG);
1099 htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
1101 return ERR_PTR(-ENOMEM);
1103 bpf_map_init_from_attr(&htab->map, attr);
1105 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1106 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1107 round_up(htab->map.key_size, 8);
1108 if (htab->buckets_num == 0 ||
1109 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1114 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1115 sizeof(struct bpf_shtab_bucket),
1116 htab->map.numa_node);
1117 if (!htab->buckets) {
1122 for (i = 0; i < htab->buckets_num; i++) {
1123 INIT_HLIST_HEAD(&htab->buckets[i].head);
1124 raw_spin_lock_init(&htab->buckets[i].lock);
1129 bpf_map_area_free(htab);
1130 return ERR_PTR(err);
1133 static void sock_hash_free(struct bpf_map *map)
1135 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1136 struct bpf_shtab_bucket *bucket;
1137 struct hlist_head unlink_list;
1138 struct bpf_shtab_elem *elem;
1139 struct hlist_node *node;
1142 /* After the sync no updates or deletes will be in-flight so it
1143 * is safe to walk map and remove entries without risking a race
1144 * in EEXIST update case.
1147 for (i = 0; i < htab->buckets_num; i++) {
1148 bucket = sock_hash_select_bucket(htab, i);
1150 /* We are racing with sock_hash_delete_from_link to
1151 * enter the spin-lock critical section. Every socket on
1152 * the list is still linked to sockhash. Since link
1153 * exists, psock exists and holds a ref to socket. That
1154 * lets us to grab a socket ref too.
1156 raw_spin_lock_bh(&bucket->lock);
1157 hlist_for_each_entry(elem, &bucket->head, node)
1158 sock_hold(elem->sk);
1159 hlist_move_list(&bucket->head, &unlink_list);
1160 raw_spin_unlock_bh(&bucket->lock);
1162 /* Process removed entries out of atomic context to
1163 * block for socket lock before deleting the psock's
1166 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1167 hlist_del(&elem->node);
1168 lock_sock(elem->sk);
1170 sock_map_unref(elem->sk, elem);
1172 release_sock(elem->sk);
1174 sock_hash_free_elem(htab, elem);
1178 /* wait for psock readers accessing its map link */
1181 bpf_map_area_free(htab->buckets);
1182 bpf_map_area_free(htab);
1185 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1189 if (map->value_size != sizeof(u64))
1190 return ERR_PTR(-ENOSPC);
1192 sk = __sock_hash_lookup_elem(map, key);
1194 return ERR_PTR(-ENOENT);
1196 __sock_gen_cookie(sk);
1197 return &sk->sk_cookie;
1200 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1204 sk = __sock_hash_lookup_elem(map, key);
1207 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1212 static void sock_hash_release_progs(struct bpf_map *map)
1214 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1217 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1218 struct bpf_map *, map, void *, key, u64, flags)
1220 WARN_ON_ONCE(!rcu_read_lock_held());
1222 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1223 sock_map_op_okay(sops)))
1224 return sock_hash_update_common(map, key, sops->sk, flags);
1228 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1229 .func = bpf_sock_hash_update,
1232 .ret_type = RET_INTEGER,
1233 .arg1_type = ARG_PTR_TO_CTX,
1234 .arg2_type = ARG_CONST_MAP_PTR,
1235 .arg3_type = ARG_PTR_TO_MAP_KEY,
1236 .arg4_type = ARG_ANYTHING,
1239 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1240 struct bpf_map *, map, void *, key, u64, flags)
1244 if (unlikely(flags & ~(BPF_F_INGRESS)))
1247 sk = __sock_hash_lookup_elem(map, key);
1248 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1251 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
1255 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1256 .func = bpf_sk_redirect_hash,
1258 .ret_type = RET_INTEGER,
1259 .arg1_type = ARG_PTR_TO_CTX,
1260 .arg2_type = ARG_CONST_MAP_PTR,
1261 .arg3_type = ARG_PTR_TO_MAP_KEY,
1262 .arg4_type = ARG_ANYTHING,
1265 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1266 struct bpf_map *, map, void *, key, u64, flags)
1270 if (unlikely(flags & ~(BPF_F_INGRESS)))
1273 sk = __sock_hash_lookup_elem(map, key);
1274 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1282 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1283 .func = bpf_msg_redirect_hash,
1285 .ret_type = RET_INTEGER,
1286 .arg1_type = ARG_PTR_TO_CTX,
1287 .arg2_type = ARG_CONST_MAP_PTR,
1288 .arg3_type = ARG_PTR_TO_MAP_KEY,
1289 .arg4_type = ARG_ANYTHING,
1292 struct sock_hash_seq_info {
1293 struct bpf_map *map;
1294 struct bpf_shtab *htab;
1298 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1299 struct bpf_shtab_elem *prev_elem)
1301 const struct bpf_shtab *htab = info->htab;
1302 struct bpf_shtab_bucket *bucket;
1303 struct bpf_shtab_elem *elem;
1304 struct hlist_node *node;
1306 /* try to find next elem in the same bucket */
1308 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1309 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1313 /* no more elements, continue in the next bucket */
1317 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1318 bucket = &htab->buckets[info->bucket_id];
1319 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1320 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1328 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1331 struct sock_hash_seq_info *info = seq->private;
1336 /* pairs with sock_hash_seq_stop */
1338 return sock_hash_seq_find_next(info, NULL);
1341 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1344 struct sock_hash_seq_info *info = seq->private;
1347 return sock_hash_seq_find_next(info, v);
1350 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1353 struct sock_hash_seq_info *info = seq->private;
1354 struct bpf_iter__sockmap ctx = {};
1355 struct bpf_shtab_elem *elem = v;
1356 struct bpf_iter_meta meta;
1357 struct bpf_prog *prog;
1360 prog = bpf_iter_get_info(&meta, !elem);
1365 ctx.map = info->map;
1367 ctx.key = elem->key;
1371 return bpf_iter_run_prog(prog, &ctx);
1374 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1378 (void)sock_hash_seq_show(seq, NULL);
1380 /* pairs with sock_hash_seq_start */
1384 static const struct seq_operations sock_hash_seq_ops = {
1385 .start = sock_hash_seq_start,
1386 .next = sock_hash_seq_next,
1387 .stop = sock_hash_seq_stop,
1388 .show = sock_hash_seq_show,
1391 static int sock_hash_init_seq_private(void *priv_data,
1392 struct bpf_iter_aux_info *aux)
1394 struct sock_hash_seq_info *info = priv_data;
1396 bpf_map_inc_with_uref(aux->map);
1397 info->map = aux->map;
1398 info->htab = container_of(aux->map, struct bpf_shtab, map);
1402 static void sock_hash_fini_seq_private(void *priv_data)
1404 struct sock_hash_seq_info *info = priv_data;
1406 bpf_map_put_with_uref(info->map);
1409 static u64 sock_hash_mem_usage(const struct bpf_map *map)
1411 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1412 u64 usage = sizeof(*htab);
1414 usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket);
1415 usage += atomic_read(&htab->count) * (u64)htab->elem_size;
1419 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1420 .seq_ops = &sock_hash_seq_ops,
1421 .init_seq_private = sock_hash_init_seq_private,
1422 .fini_seq_private = sock_hash_fini_seq_private,
1423 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1426 BTF_ID_LIST_SINGLE(sock_hash_map_btf_ids, struct, bpf_shtab)
1427 const struct bpf_map_ops sock_hash_ops = {
1428 .map_meta_equal = bpf_map_meta_equal,
1429 .map_alloc = sock_hash_alloc,
1430 .map_free = sock_hash_free,
1431 .map_get_next_key = sock_hash_get_next_key,
1432 .map_update_elem = sock_map_update_elem,
1433 .map_delete_elem = sock_hash_delete_elem,
1434 .map_lookup_elem = sock_hash_lookup,
1435 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1436 .map_release_uref = sock_hash_release_progs,
1437 .map_check_btf = map_check_no_btf,
1438 .map_mem_usage = sock_hash_mem_usage,
1439 .map_btf_id = &sock_hash_map_btf_ids[0],
1440 .iter_seq_info = &sock_hash_iter_seq_info,
1443 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1445 switch (map->map_type) {
1446 case BPF_MAP_TYPE_SOCKMAP:
1447 return &container_of(map, struct bpf_stab, map)->progs;
1448 case BPF_MAP_TYPE_SOCKHASH:
1449 return &container_of(map, struct bpf_shtab, map)->progs;
1457 static int sock_map_prog_lookup(struct bpf_map *map, struct bpf_prog ***pprog,
1460 struct sk_psock_progs *progs = sock_map_progs(map);
1466 case BPF_SK_MSG_VERDICT:
1467 *pprog = &progs->msg_parser;
1469 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1470 case BPF_SK_SKB_STREAM_PARSER:
1471 *pprog = &progs->stream_parser;
1474 case BPF_SK_SKB_STREAM_VERDICT:
1475 if (progs->skb_verdict)
1477 *pprog = &progs->stream_verdict;
1479 case BPF_SK_SKB_VERDICT:
1480 if (progs->stream_verdict)
1482 *pprog = &progs->skb_verdict;
1491 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1492 struct bpf_prog *old, u32 which)
1494 struct bpf_prog **pprog;
1497 ret = sock_map_prog_lookup(map, &pprog, which);
1502 return psock_replace_prog(pprog, prog, old);
1504 psock_set_prog(pprog, prog);
1508 int sock_map_bpf_prog_query(const union bpf_attr *attr,
1509 union bpf_attr __user *uattr)
1511 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
1512 u32 prog_cnt = 0, flags = 0, ufd = attr->target_fd;
1513 struct bpf_prog **pprog;
1514 struct bpf_prog *prog;
1515 struct bpf_map *map;
1520 if (attr->query.query_flags)
1524 map = __bpf_map_get(f);
1526 return PTR_ERR(map);
1530 ret = sock_map_prog_lookup(map, &pprog, attr->query.attach_type);
1535 prog_cnt = !prog ? 0 : 1;
1537 if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
1540 /* we do not hold the refcnt, the bpf prog may be released
1541 * asynchronously and the id would be set to 0.
1543 id = data_race(prog->aux->id);
1550 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)) ||
1551 (id != 0 && copy_to_user(prog_ids, &id, sizeof(u32))) ||
1552 copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
1559 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1561 switch (link->map->map_type) {
1562 case BPF_MAP_TYPE_SOCKMAP:
1563 return sock_map_delete_from_link(link->map, sk,
1565 case BPF_MAP_TYPE_SOCKHASH:
1566 return sock_hash_delete_from_link(link->map, sk,
1573 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1575 struct sk_psock_link *link;
1577 while ((link = sk_psock_link_pop(psock))) {
1578 sock_map_unlink(sk, link);
1579 sk_psock_free_link(link);
1583 void sock_map_unhash(struct sock *sk)
1585 void (*saved_unhash)(struct sock *sk);
1586 struct sk_psock *psock;
1589 psock = sk_psock(sk);
1590 if (unlikely(!psock)) {
1592 saved_unhash = READ_ONCE(sk->sk_prot)->unhash;
1594 saved_unhash = psock->saved_unhash;
1595 sock_map_remove_links(sk, psock);
1598 if (WARN_ON_ONCE(saved_unhash == sock_map_unhash))
1603 EXPORT_SYMBOL_GPL(sock_map_unhash);
1605 void sock_map_destroy(struct sock *sk)
1607 void (*saved_destroy)(struct sock *sk);
1608 struct sk_psock *psock;
1611 psock = sk_psock_get(sk);
1612 if (unlikely(!psock)) {
1614 saved_destroy = READ_ONCE(sk->sk_prot)->destroy;
1616 saved_destroy = psock->saved_destroy;
1617 sock_map_remove_links(sk, psock);
1619 sk_psock_stop(psock);
1620 sk_psock_put(sk, psock);
1622 if (WARN_ON_ONCE(saved_destroy == sock_map_destroy))
1627 EXPORT_SYMBOL_GPL(sock_map_destroy);
1629 void sock_map_close(struct sock *sk, long timeout)
1631 void (*saved_close)(struct sock *sk, long timeout);
1632 struct sk_psock *psock;
1636 psock = sk_psock_get(sk);
1637 if (unlikely(!psock)) {
1640 saved_close = READ_ONCE(sk->sk_prot)->close;
1642 saved_close = psock->saved_close;
1643 sock_map_remove_links(sk, psock);
1645 sk_psock_stop(psock);
1647 cancel_delayed_work_sync(&psock->work);
1648 sk_psock_put(sk, psock);
1651 /* Make sure we do not recurse. This is a bug.
1652 * Leak the socket instead of crashing on a stack overflow.
1654 if (WARN_ON_ONCE(saved_close == sock_map_close))
1656 saved_close(sk, timeout);
1658 EXPORT_SYMBOL_GPL(sock_map_close);
1660 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1661 union bpf_iter_link_info *linfo,
1662 struct bpf_iter_aux_info *aux)
1664 struct bpf_map *map;
1667 if (!linfo->map.map_fd)
1670 map = bpf_map_get_with_uref(linfo->map.map_fd);
1672 return PTR_ERR(map);
1674 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1675 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1678 if (prog->aux->max_rdonly_access > map->key_size) {
1687 bpf_map_put_with_uref(map);
1691 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1693 bpf_map_put_with_uref(aux->map);
1696 static struct bpf_iter_reg sock_map_iter_reg = {
1697 .target = "sockmap",
1698 .attach_target = sock_map_iter_attach_target,
1699 .detach_target = sock_map_iter_detach_target,
1700 .show_fdinfo = bpf_iter_map_show_fdinfo,
1701 .fill_link_info = bpf_iter_map_fill_link_info,
1702 .ctx_arg_info_size = 2,
1704 { offsetof(struct bpf_iter__sockmap, key),
1705 PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
1706 { offsetof(struct bpf_iter__sockmap, sk),
1707 PTR_TO_BTF_ID_OR_NULL },
1711 static int __init bpf_sockmap_iter_init(void)
1713 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1714 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1715 return bpf_iter_reg_target(&sock_map_iter_reg);
1717 late_initcall(bpf_sockmap_iter_init);