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 = kzalloc(sizeof(*stab), GFP_USER | __GFP_ACCOUNT);
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 *),
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);
172 write_unlock_bh(&sk->sk_callback_lock);
176 static void sock_map_unref(struct sock *sk, void *link_raw)
178 struct sk_psock *psock = sk_psock(sk);
181 sock_map_del_link(sk, psock, link_raw);
182 sk_psock_put(sk, psock);
186 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
188 if (!sk->sk_prot->psock_update_sk_prot)
190 psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
191 return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
194 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
196 struct sk_psock *psock;
199 psock = sk_psock(sk);
201 if (sk->sk_prot->close != sock_map_close) {
202 psock = ERR_PTR(-EBUSY);
206 if (!refcount_inc_not_zero(&psock->refcnt))
207 psock = ERR_PTR(-EBUSY);
214 static int sock_map_link(struct bpf_map *map, struct sock *sk)
216 struct sk_psock_progs *progs = sock_map_progs(map);
217 struct bpf_prog *stream_verdict = NULL;
218 struct bpf_prog *stream_parser = NULL;
219 struct bpf_prog *skb_verdict = NULL;
220 struct bpf_prog *msg_parser = NULL;
221 struct sk_psock *psock;
224 stream_verdict = READ_ONCE(progs->stream_verdict);
225 if (stream_verdict) {
226 stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
227 if (IS_ERR(stream_verdict))
228 return PTR_ERR(stream_verdict);
231 stream_parser = READ_ONCE(progs->stream_parser);
233 stream_parser = bpf_prog_inc_not_zero(stream_parser);
234 if (IS_ERR(stream_parser)) {
235 ret = PTR_ERR(stream_parser);
236 goto out_put_stream_verdict;
240 msg_parser = READ_ONCE(progs->msg_parser);
242 msg_parser = bpf_prog_inc_not_zero(msg_parser);
243 if (IS_ERR(msg_parser)) {
244 ret = PTR_ERR(msg_parser);
245 goto out_put_stream_parser;
249 skb_verdict = READ_ONCE(progs->skb_verdict);
251 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
252 if (IS_ERR(skb_verdict)) {
253 ret = PTR_ERR(skb_verdict);
254 goto out_put_msg_parser;
258 psock = sock_map_psock_get_checked(sk);
260 ret = PTR_ERR(psock);
265 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
266 (stream_parser && READ_ONCE(psock->progs.stream_parser)) ||
267 (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
268 (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) ||
269 (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
270 (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) {
271 sk_psock_put(sk, psock);
276 psock = sk_psock_init(sk, map->numa_node);
278 ret = PTR_ERR(psock);
284 psock_set_prog(&psock->progs.msg_parser, msg_parser);
286 ret = sock_map_init_proto(sk, psock);
290 write_lock_bh(&sk->sk_callback_lock);
291 if (stream_parser && stream_verdict && !psock->saved_data_ready) {
292 ret = sk_psock_init_strp(sk, psock);
294 goto out_unlock_drop;
295 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
296 psock_set_prog(&psock->progs.stream_parser, stream_parser);
297 sk_psock_start_strp(sk, psock);
298 } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
299 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
300 sk_psock_start_verdict(sk,psock);
301 } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
302 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
303 sk_psock_start_verdict(sk, psock);
305 write_unlock_bh(&sk->sk_callback_lock);
308 write_unlock_bh(&sk->sk_callback_lock);
310 sk_psock_put(sk, psock);
313 bpf_prog_put(skb_verdict);
316 bpf_prog_put(msg_parser);
317 out_put_stream_parser:
319 bpf_prog_put(stream_parser);
320 out_put_stream_verdict:
322 bpf_prog_put(stream_verdict);
326 static void sock_map_free(struct bpf_map *map)
328 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
331 /* After the sync no updates or deletes will be in-flight so it
332 * is safe to walk map and remove entries without risking a race
333 * in EEXIST update case.
336 for (i = 0; i < stab->map.max_entries; i++) {
337 struct sock **psk = &stab->sks[i];
340 sk = xchg(psk, NULL);
344 sock_map_unref(sk, psk);
350 /* wait for psock readers accessing its map link */
353 bpf_map_area_free(stab->sks);
357 static void sock_map_release_progs(struct bpf_map *map)
359 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
362 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
364 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
366 WARN_ON_ONCE(!rcu_read_lock_held());
368 if (unlikely(key >= map->max_entries))
370 return READ_ONCE(stab->sks[key]);
373 static void *sock_map_lookup(struct bpf_map *map, void *key)
377 sk = __sock_map_lookup_elem(map, *(u32 *)key);
380 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
385 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
389 if (map->value_size != sizeof(u64))
390 return ERR_PTR(-ENOSPC);
392 sk = __sock_map_lookup_elem(map, *(u32 *)key);
394 return ERR_PTR(-ENOENT);
396 __sock_gen_cookie(sk);
397 return &sk->sk_cookie;
400 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
406 raw_spin_lock_bh(&stab->lock);
408 if (!sk_test || sk_test == sk)
409 sk = xchg(psk, NULL);
412 sock_map_unref(sk, psk);
416 raw_spin_unlock_bh(&stab->lock);
420 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
423 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
425 __sock_map_delete(stab, sk, link_raw);
428 static int sock_map_delete_elem(struct bpf_map *map, void *key)
430 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
434 if (unlikely(i >= map->max_entries))
438 return __sock_map_delete(stab, NULL, psk);
441 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
443 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
444 u32 i = key ? *(u32 *)key : U32_MAX;
445 u32 *key_next = next;
447 if (i == stab->map.max_entries - 1)
449 if (i >= stab->map.max_entries)
456 static int sock_map_update_common(struct bpf_map *map, u32 idx,
457 struct sock *sk, u64 flags)
459 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
460 struct sk_psock_link *link;
461 struct sk_psock *psock;
465 WARN_ON_ONCE(!rcu_read_lock_held());
466 if (unlikely(flags > BPF_EXIST))
468 if (unlikely(idx >= map->max_entries))
471 link = sk_psock_init_link();
475 ret = sock_map_link(map, sk);
479 psock = sk_psock(sk);
480 WARN_ON_ONCE(!psock);
482 raw_spin_lock_bh(&stab->lock);
483 osk = stab->sks[idx];
484 if (osk && flags == BPF_NOEXIST) {
487 } else if (!osk && flags == BPF_EXIST) {
492 sock_map_add_link(psock, link, map, &stab->sks[idx]);
495 sock_map_unref(osk, &stab->sks[idx]);
496 raw_spin_unlock_bh(&stab->lock);
499 raw_spin_unlock_bh(&stab->lock);
501 sk_psock_put(sk, psock);
503 sk_psock_free_link(link);
507 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
509 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
510 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
511 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
514 static bool sk_is_tcp(const struct sock *sk)
516 return sk->sk_type == SOCK_STREAM &&
517 sk->sk_protocol == IPPROTO_TCP;
520 static bool sock_map_redirect_allowed(const struct sock *sk)
523 return sk->sk_state != TCP_LISTEN;
525 return sk->sk_state == TCP_ESTABLISHED;
528 static bool sock_map_sk_is_suitable(const struct sock *sk)
530 return !!sk->sk_prot->psock_update_sk_prot;
533 static bool sock_map_sk_state_allowed(const struct sock *sk)
536 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
540 static int sock_hash_update_common(struct bpf_map *map, void *key,
541 struct sock *sk, u64 flags);
543 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
551 if (map->value_size == sizeof(u64))
558 sock = sockfd_lookup(ufd, &ret);
566 if (!sock_map_sk_is_suitable(sk)) {
571 sock_map_sk_acquire(sk);
572 if (!sock_map_sk_state_allowed(sk))
574 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
575 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
577 ret = sock_hash_update_common(map, key, sk, flags);
578 sock_map_sk_release(sk);
584 static int sock_map_update_elem(struct bpf_map *map, void *key,
585 void *value, u64 flags)
587 struct sock *sk = (struct sock *)value;
590 if (unlikely(!sk || !sk_fullsock(sk)))
593 if (!sock_map_sk_is_suitable(sk))
598 if (!sock_map_sk_state_allowed(sk))
600 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
601 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
603 ret = sock_hash_update_common(map, key, sk, flags);
609 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
610 struct bpf_map *, map, void *, key, u64, flags)
612 WARN_ON_ONCE(!rcu_read_lock_held());
614 if (likely(sock_map_sk_is_suitable(sops->sk) &&
615 sock_map_op_okay(sops)))
616 return sock_map_update_common(map, *(u32 *)key, sops->sk,
621 const struct bpf_func_proto bpf_sock_map_update_proto = {
622 .func = bpf_sock_map_update,
625 .ret_type = RET_INTEGER,
626 .arg1_type = ARG_PTR_TO_CTX,
627 .arg2_type = ARG_CONST_MAP_PTR,
628 .arg3_type = ARG_PTR_TO_MAP_KEY,
629 .arg4_type = ARG_ANYTHING,
632 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
633 struct bpf_map *, map, u32, key, u64, flags)
637 if (unlikely(flags & ~(BPF_F_INGRESS)))
640 sk = __sock_map_lookup_elem(map, key);
641 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
644 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
648 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
649 .func = bpf_sk_redirect_map,
651 .ret_type = RET_INTEGER,
652 .arg1_type = ARG_PTR_TO_CTX,
653 .arg2_type = ARG_CONST_MAP_PTR,
654 .arg3_type = ARG_ANYTHING,
655 .arg4_type = ARG_ANYTHING,
658 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
659 struct bpf_map *, map, u32, key, u64, flags)
663 if (unlikely(flags & ~(BPF_F_INGRESS)))
666 sk = __sock_map_lookup_elem(map, key);
667 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
675 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
676 .func = bpf_msg_redirect_map,
678 .ret_type = RET_INTEGER,
679 .arg1_type = ARG_PTR_TO_CTX,
680 .arg2_type = ARG_CONST_MAP_PTR,
681 .arg3_type = ARG_ANYTHING,
682 .arg4_type = ARG_ANYTHING,
685 struct sock_map_seq_info {
691 struct bpf_iter__sockmap {
692 __bpf_md_ptr(struct bpf_iter_meta *, meta);
693 __bpf_md_ptr(struct bpf_map *, map);
694 __bpf_md_ptr(void *, key);
695 __bpf_md_ptr(struct sock *, sk);
698 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
699 struct bpf_map *map, void *key,
702 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
704 if (unlikely(info->index >= info->map->max_entries))
707 info->sk = __sock_map_lookup_elem(info->map, info->index);
709 /* can't return sk directly, since that might be NULL */
713 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
716 struct sock_map_seq_info *info = seq->private;
721 /* pairs with sock_map_seq_stop */
723 return sock_map_seq_lookup_elem(info);
726 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
729 struct sock_map_seq_info *info = seq->private;
734 return sock_map_seq_lookup_elem(info);
737 static int sock_map_seq_show(struct seq_file *seq, void *v)
740 struct sock_map_seq_info *info = seq->private;
741 struct bpf_iter__sockmap ctx = {};
742 struct bpf_iter_meta meta;
743 struct bpf_prog *prog;
746 prog = bpf_iter_get_info(&meta, !v);
753 ctx.key = &info->index;
757 return bpf_iter_run_prog(prog, &ctx);
760 static void sock_map_seq_stop(struct seq_file *seq, void *v)
764 (void)sock_map_seq_show(seq, NULL);
766 /* pairs with sock_map_seq_start */
770 static const struct seq_operations sock_map_seq_ops = {
771 .start = sock_map_seq_start,
772 .next = sock_map_seq_next,
773 .stop = sock_map_seq_stop,
774 .show = sock_map_seq_show,
777 static int sock_map_init_seq_private(void *priv_data,
778 struct bpf_iter_aux_info *aux)
780 struct sock_map_seq_info *info = priv_data;
782 info->map = aux->map;
786 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
787 .seq_ops = &sock_map_seq_ops,
788 .init_seq_private = sock_map_init_seq_private,
789 .seq_priv_size = sizeof(struct sock_map_seq_info),
792 static int sock_map_btf_id;
793 const struct bpf_map_ops sock_map_ops = {
794 .map_meta_equal = bpf_map_meta_equal,
795 .map_alloc = sock_map_alloc,
796 .map_free = sock_map_free,
797 .map_get_next_key = sock_map_get_next_key,
798 .map_lookup_elem_sys_only = sock_map_lookup_sys,
799 .map_update_elem = sock_map_update_elem,
800 .map_delete_elem = sock_map_delete_elem,
801 .map_lookup_elem = sock_map_lookup,
802 .map_release_uref = sock_map_release_progs,
803 .map_check_btf = map_check_no_btf,
804 .map_btf_name = "bpf_stab",
805 .map_btf_id = &sock_map_btf_id,
806 .iter_seq_info = &sock_map_iter_seq_info,
809 struct bpf_shtab_elem {
813 struct hlist_node node;
817 struct bpf_shtab_bucket {
818 struct hlist_head head;
824 struct bpf_shtab_bucket *buckets;
827 struct sk_psock_progs progs;
831 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
833 return jhash(key, len, 0);
836 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
839 return &htab->buckets[hash & (htab->buckets_num - 1)];
842 static struct bpf_shtab_elem *
843 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
846 struct bpf_shtab_elem *elem;
848 hlist_for_each_entry_rcu(elem, head, node) {
849 if (elem->hash == hash &&
850 !memcmp(&elem->key, key, key_size))
857 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
859 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
860 u32 key_size = map->key_size, hash;
861 struct bpf_shtab_bucket *bucket;
862 struct bpf_shtab_elem *elem;
864 WARN_ON_ONCE(!rcu_read_lock_held());
866 hash = sock_hash_bucket_hash(key, key_size);
867 bucket = sock_hash_select_bucket(htab, hash);
868 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
870 return elem ? elem->sk : NULL;
873 static void sock_hash_free_elem(struct bpf_shtab *htab,
874 struct bpf_shtab_elem *elem)
876 atomic_dec(&htab->count);
877 kfree_rcu(elem, rcu);
880 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
883 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
884 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
885 struct bpf_shtab_bucket *bucket;
887 WARN_ON_ONCE(!rcu_read_lock_held());
888 bucket = sock_hash_select_bucket(htab, elem->hash);
890 /* elem may be deleted in parallel from the map, but access here
891 * is okay since it's going away only after RCU grace period.
892 * However, we need to check whether it's still present.
894 raw_spin_lock_bh(&bucket->lock);
895 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
896 elem->key, map->key_size);
897 if (elem_probe && elem_probe == elem) {
898 hlist_del_rcu(&elem->node);
899 sock_map_unref(elem->sk, elem);
900 sock_hash_free_elem(htab, elem);
902 raw_spin_unlock_bh(&bucket->lock);
905 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
907 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
908 u32 hash, key_size = map->key_size;
909 struct bpf_shtab_bucket *bucket;
910 struct bpf_shtab_elem *elem;
913 hash = sock_hash_bucket_hash(key, key_size);
914 bucket = sock_hash_select_bucket(htab, hash);
916 raw_spin_lock_bh(&bucket->lock);
917 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
919 hlist_del_rcu(&elem->node);
920 sock_map_unref(elem->sk, elem);
921 sock_hash_free_elem(htab, elem);
924 raw_spin_unlock_bh(&bucket->lock);
928 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
929 void *key, u32 key_size,
930 u32 hash, struct sock *sk,
931 struct bpf_shtab_elem *old)
933 struct bpf_shtab_elem *new;
935 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
937 atomic_dec(&htab->count);
938 return ERR_PTR(-E2BIG);
942 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
943 GFP_ATOMIC | __GFP_NOWARN,
944 htab->map.numa_node);
946 atomic_dec(&htab->count);
947 return ERR_PTR(-ENOMEM);
949 memcpy(new->key, key, key_size);
955 static int sock_hash_update_common(struct bpf_map *map, void *key,
956 struct sock *sk, u64 flags)
958 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
959 u32 key_size = map->key_size, hash;
960 struct bpf_shtab_elem *elem, *elem_new;
961 struct bpf_shtab_bucket *bucket;
962 struct sk_psock_link *link;
963 struct sk_psock *psock;
966 WARN_ON_ONCE(!rcu_read_lock_held());
967 if (unlikely(flags > BPF_EXIST))
970 link = sk_psock_init_link();
974 ret = sock_map_link(map, sk);
978 psock = sk_psock(sk);
979 WARN_ON_ONCE(!psock);
981 hash = sock_hash_bucket_hash(key, key_size);
982 bucket = sock_hash_select_bucket(htab, hash);
984 raw_spin_lock_bh(&bucket->lock);
985 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
986 if (elem && flags == BPF_NOEXIST) {
989 } else if (!elem && flags == BPF_EXIST) {
994 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
995 if (IS_ERR(elem_new)) {
996 ret = PTR_ERR(elem_new);
1000 sock_map_add_link(psock, link, map, elem_new);
1001 /* Add new element to the head of the list, so that
1002 * concurrent search will find it before old elem.
1004 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1006 hlist_del_rcu(&elem->node);
1007 sock_map_unref(elem->sk, elem);
1008 sock_hash_free_elem(htab, elem);
1010 raw_spin_unlock_bh(&bucket->lock);
1013 raw_spin_unlock_bh(&bucket->lock);
1014 sk_psock_put(sk, psock);
1016 sk_psock_free_link(link);
1020 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1023 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1024 struct bpf_shtab_elem *elem, *elem_next;
1025 u32 hash, key_size = map->key_size;
1026 struct hlist_head *head;
1030 goto find_first_elem;
1031 hash = sock_hash_bucket_hash(key, key_size);
1032 head = &sock_hash_select_bucket(htab, hash)->head;
1033 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1035 goto find_first_elem;
1037 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1038 struct bpf_shtab_elem, node);
1040 memcpy(key_next, elem_next->key, key_size);
1044 i = hash & (htab->buckets_num - 1);
1047 for (; i < htab->buckets_num; i++) {
1048 head = &sock_hash_select_bucket(htab, i)->head;
1049 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1050 struct bpf_shtab_elem, node);
1052 memcpy(key_next, elem_next->key, key_size);
1060 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1062 struct bpf_shtab *htab;
1065 if (!capable(CAP_NET_ADMIN))
1066 return ERR_PTR(-EPERM);
1067 if (attr->max_entries == 0 ||
1068 attr->key_size == 0 ||
1069 (attr->value_size != sizeof(u32) &&
1070 attr->value_size != sizeof(u64)) ||
1071 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1072 return ERR_PTR(-EINVAL);
1073 if (attr->key_size > MAX_BPF_STACK)
1074 return ERR_PTR(-E2BIG);
1076 htab = kzalloc(sizeof(*htab), GFP_USER | __GFP_ACCOUNT);
1078 return ERR_PTR(-ENOMEM);
1080 bpf_map_init_from_attr(&htab->map, attr);
1082 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1083 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1084 round_up(htab->map.key_size, 8);
1085 if (htab->buckets_num == 0 ||
1086 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1091 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1092 sizeof(struct bpf_shtab_bucket),
1093 htab->map.numa_node);
1094 if (!htab->buckets) {
1099 for (i = 0; i < htab->buckets_num; i++) {
1100 INIT_HLIST_HEAD(&htab->buckets[i].head);
1101 raw_spin_lock_init(&htab->buckets[i].lock);
1107 return ERR_PTR(err);
1110 static void sock_hash_free(struct bpf_map *map)
1112 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1113 struct bpf_shtab_bucket *bucket;
1114 struct hlist_head unlink_list;
1115 struct bpf_shtab_elem *elem;
1116 struct hlist_node *node;
1119 /* After the sync no updates or deletes will be in-flight so it
1120 * is safe to walk map and remove entries without risking a race
1121 * in EEXIST update case.
1124 for (i = 0; i < htab->buckets_num; i++) {
1125 bucket = sock_hash_select_bucket(htab, i);
1127 /* We are racing with sock_hash_delete_from_link to
1128 * enter the spin-lock critical section. Every socket on
1129 * the list is still linked to sockhash. Since link
1130 * exists, psock exists and holds a ref to socket. That
1131 * lets us to grab a socket ref too.
1133 raw_spin_lock_bh(&bucket->lock);
1134 hlist_for_each_entry(elem, &bucket->head, node)
1135 sock_hold(elem->sk);
1136 hlist_move_list(&bucket->head, &unlink_list);
1137 raw_spin_unlock_bh(&bucket->lock);
1139 /* Process removed entries out of atomic context to
1140 * block for socket lock before deleting the psock's
1143 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1144 hlist_del(&elem->node);
1145 lock_sock(elem->sk);
1147 sock_map_unref(elem->sk, elem);
1149 release_sock(elem->sk);
1151 sock_hash_free_elem(htab, elem);
1155 /* wait for psock readers accessing its map link */
1158 bpf_map_area_free(htab->buckets);
1162 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1166 if (map->value_size != sizeof(u64))
1167 return ERR_PTR(-ENOSPC);
1169 sk = __sock_hash_lookup_elem(map, key);
1171 return ERR_PTR(-ENOENT);
1173 __sock_gen_cookie(sk);
1174 return &sk->sk_cookie;
1177 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1181 sk = __sock_hash_lookup_elem(map, key);
1184 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1189 static void sock_hash_release_progs(struct bpf_map *map)
1191 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1194 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1195 struct bpf_map *, map, void *, key, u64, flags)
1197 WARN_ON_ONCE(!rcu_read_lock_held());
1199 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1200 sock_map_op_okay(sops)))
1201 return sock_hash_update_common(map, key, sops->sk, flags);
1205 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1206 .func = bpf_sock_hash_update,
1209 .ret_type = RET_INTEGER,
1210 .arg1_type = ARG_PTR_TO_CTX,
1211 .arg2_type = ARG_CONST_MAP_PTR,
1212 .arg3_type = ARG_PTR_TO_MAP_KEY,
1213 .arg4_type = ARG_ANYTHING,
1216 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1217 struct bpf_map *, map, void *, key, u64, flags)
1221 if (unlikely(flags & ~(BPF_F_INGRESS)))
1224 sk = __sock_hash_lookup_elem(map, key);
1225 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1228 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
1232 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1233 .func = bpf_sk_redirect_hash,
1235 .ret_type = RET_INTEGER,
1236 .arg1_type = ARG_PTR_TO_CTX,
1237 .arg2_type = ARG_CONST_MAP_PTR,
1238 .arg3_type = ARG_PTR_TO_MAP_KEY,
1239 .arg4_type = ARG_ANYTHING,
1242 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1243 struct bpf_map *, map, void *, key, u64, flags)
1247 if (unlikely(flags & ~(BPF_F_INGRESS)))
1250 sk = __sock_hash_lookup_elem(map, key);
1251 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1259 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1260 .func = bpf_msg_redirect_hash,
1262 .ret_type = RET_INTEGER,
1263 .arg1_type = ARG_PTR_TO_CTX,
1264 .arg2_type = ARG_CONST_MAP_PTR,
1265 .arg3_type = ARG_PTR_TO_MAP_KEY,
1266 .arg4_type = ARG_ANYTHING,
1269 struct sock_hash_seq_info {
1270 struct bpf_map *map;
1271 struct bpf_shtab *htab;
1275 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1276 struct bpf_shtab_elem *prev_elem)
1278 const struct bpf_shtab *htab = info->htab;
1279 struct bpf_shtab_bucket *bucket;
1280 struct bpf_shtab_elem *elem;
1281 struct hlist_node *node;
1283 /* try to find next elem in the same bucket */
1285 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1286 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1290 /* no more elements, continue in the next bucket */
1294 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1295 bucket = &htab->buckets[info->bucket_id];
1296 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1297 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1305 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1308 struct sock_hash_seq_info *info = seq->private;
1313 /* pairs with sock_hash_seq_stop */
1315 return sock_hash_seq_find_next(info, NULL);
1318 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1321 struct sock_hash_seq_info *info = seq->private;
1324 return sock_hash_seq_find_next(info, v);
1327 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1330 struct sock_hash_seq_info *info = seq->private;
1331 struct bpf_iter__sockmap ctx = {};
1332 struct bpf_shtab_elem *elem = v;
1333 struct bpf_iter_meta meta;
1334 struct bpf_prog *prog;
1337 prog = bpf_iter_get_info(&meta, !elem);
1342 ctx.map = info->map;
1344 ctx.key = elem->key;
1348 return bpf_iter_run_prog(prog, &ctx);
1351 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1355 (void)sock_hash_seq_show(seq, NULL);
1357 /* pairs with sock_hash_seq_start */
1361 static const struct seq_operations sock_hash_seq_ops = {
1362 .start = sock_hash_seq_start,
1363 .next = sock_hash_seq_next,
1364 .stop = sock_hash_seq_stop,
1365 .show = sock_hash_seq_show,
1368 static int sock_hash_init_seq_private(void *priv_data,
1369 struct bpf_iter_aux_info *aux)
1371 struct sock_hash_seq_info *info = priv_data;
1373 info->map = aux->map;
1374 info->htab = container_of(aux->map, struct bpf_shtab, map);
1378 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1379 .seq_ops = &sock_hash_seq_ops,
1380 .init_seq_private = sock_hash_init_seq_private,
1381 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1384 static int sock_hash_map_btf_id;
1385 const struct bpf_map_ops sock_hash_ops = {
1386 .map_meta_equal = bpf_map_meta_equal,
1387 .map_alloc = sock_hash_alloc,
1388 .map_free = sock_hash_free,
1389 .map_get_next_key = sock_hash_get_next_key,
1390 .map_update_elem = sock_map_update_elem,
1391 .map_delete_elem = sock_hash_delete_elem,
1392 .map_lookup_elem = sock_hash_lookup,
1393 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1394 .map_release_uref = sock_hash_release_progs,
1395 .map_check_btf = map_check_no_btf,
1396 .map_btf_name = "bpf_shtab",
1397 .map_btf_id = &sock_hash_map_btf_id,
1398 .iter_seq_info = &sock_hash_iter_seq_info,
1401 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1403 switch (map->map_type) {
1404 case BPF_MAP_TYPE_SOCKMAP:
1405 return &container_of(map, struct bpf_stab, map)->progs;
1406 case BPF_MAP_TYPE_SOCKHASH:
1407 return &container_of(map, struct bpf_shtab, map)->progs;
1415 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1416 struct bpf_prog *old, u32 which)
1418 struct sk_psock_progs *progs = sock_map_progs(map);
1419 struct bpf_prog **pprog;
1425 case BPF_SK_MSG_VERDICT:
1426 pprog = &progs->msg_parser;
1428 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1429 case BPF_SK_SKB_STREAM_PARSER:
1430 pprog = &progs->stream_parser;
1433 case BPF_SK_SKB_STREAM_VERDICT:
1434 if (progs->skb_verdict)
1436 pprog = &progs->stream_verdict;
1438 case BPF_SK_SKB_VERDICT:
1439 if (progs->stream_verdict)
1441 pprog = &progs->skb_verdict;
1448 return psock_replace_prog(pprog, prog, old);
1450 psock_set_prog(pprog, prog);
1454 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1456 switch (link->map->map_type) {
1457 case BPF_MAP_TYPE_SOCKMAP:
1458 return sock_map_delete_from_link(link->map, sk,
1460 case BPF_MAP_TYPE_SOCKHASH:
1461 return sock_hash_delete_from_link(link->map, sk,
1468 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1470 struct sk_psock_link *link;
1472 while ((link = sk_psock_link_pop(psock))) {
1473 sock_map_unlink(sk, link);
1474 sk_psock_free_link(link);
1478 void sock_map_unhash(struct sock *sk)
1480 void (*saved_unhash)(struct sock *sk);
1481 struct sk_psock *psock;
1484 psock = sk_psock(sk);
1485 if (unlikely(!psock)) {
1487 if (sk->sk_prot->unhash)
1488 sk->sk_prot->unhash(sk);
1492 saved_unhash = psock->saved_unhash;
1493 sock_map_remove_links(sk, psock);
1497 EXPORT_SYMBOL_GPL(sock_map_unhash);
1499 void sock_map_close(struct sock *sk, long timeout)
1501 void (*saved_close)(struct sock *sk, long timeout);
1502 struct sk_psock *psock;
1506 psock = sk_psock_get(sk);
1507 if (unlikely(!psock)) {
1510 return sk->sk_prot->close(sk, timeout);
1513 saved_close = psock->saved_close;
1514 sock_map_remove_links(sk, psock);
1516 sk_psock_stop(psock, true);
1517 sk_psock_put(sk, psock);
1519 saved_close(sk, timeout);
1521 EXPORT_SYMBOL_GPL(sock_map_close);
1523 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1524 union bpf_iter_link_info *linfo,
1525 struct bpf_iter_aux_info *aux)
1527 struct bpf_map *map;
1530 if (!linfo->map.map_fd)
1533 map = bpf_map_get_with_uref(linfo->map.map_fd);
1535 return PTR_ERR(map);
1537 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1538 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1541 if (prog->aux->max_rdonly_access > map->key_size) {
1550 bpf_map_put_with_uref(map);
1554 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1556 bpf_map_put_with_uref(aux->map);
1559 static struct bpf_iter_reg sock_map_iter_reg = {
1560 .target = "sockmap",
1561 .attach_target = sock_map_iter_attach_target,
1562 .detach_target = sock_map_iter_detach_target,
1563 .show_fdinfo = bpf_iter_map_show_fdinfo,
1564 .fill_link_info = bpf_iter_map_fill_link_info,
1565 .ctx_arg_info_size = 2,
1567 { offsetof(struct bpf_iter__sockmap, key),
1568 PTR_TO_RDONLY_BUF_OR_NULL },
1569 { offsetof(struct bpf_iter__sockmap, sk),
1570 PTR_TO_BTF_ID_OR_NULL },
1574 static int __init bpf_sockmap_iter_init(void)
1576 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1577 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1578 return bpf_iter_reg_target(&sock_map_iter_reg);
1580 late_initcall(bpf_sockmap_iter_init);