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 sock_map_redirect_allowed(const struct sock *sk)
517 return sk->sk_state != TCP_LISTEN;
519 return sk->sk_state == TCP_ESTABLISHED;
522 static bool sock_map_sk_is_suitable(const struct sock *sk)
524 return !!sk->sk_prot->psock_update_sk_prot;
527 static bool sock_map_sk_state_allowed(const struct sock *sk)
530 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
534 static int sock_hash_update_common(struct bpf_map *map, void *key,
535 struct sock *sk, u64 flags);
537 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
545 if (map->value_size == sizeof(u64))
552 sock = sockfd_lookup(ufd, &ret);
560 if (!sock_map_sk_is_suitable(sk)) {
565 sock_map_sk_acquire(sk);
566 if (!sock_map_sk_state_allowed(sk))
568 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
569 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
571 ret = sock_hash_update_common(map, key, sk, flags);
572 sock_map_sk_release(sk);
578 static int sock_map_update_elem(struct bpf_map *map, void *key,
579 void *value, u64 flags)
581 struct sock *sk = (struct sock *)value;
584 if (unlikely(!sk || !sk_fullsock(sk)))
587 if (!sock_map_sk_is_suitable(sk))
592 if (!sock_map_sk_state_allowed(sk))
594 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
595 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
597 ret = sock_hash_update_common(map, key, sk, flags);
603 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
604 struct bpf_map *, map, void *, key, u64, flags)
606 WARN_ON_ONCE(!rcu_read_lock_held());
608 if (likely(sock_map_sk_is_suitable(sops->sk) &&
609 sock_map_op_okay(sops)))
610 return sock_map_update_common(map, *(u32 *)key, sops->sk,
615 const struct bpf_func_proto bpf_sock_map_update_proto = {
616 .func = bpf_sock_map_update,
619 .ret_type = RET_INTEGER,
620 .arg1_type = ARG_PTR_TO_CTX,
621 .arg2_type = ARG_CONST_MAP_PTR,
622 .arg3_type = ARG_PTR_TO_MAP_KEY,
623 .arg4_type = ARG_ANYTHING,
626 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
627 struct bpf_map *, map, u32, key, u64, flags)
631 if (unlikely(flags & ~(BPF_F_INGRESS)))
634 sk = __sock_map_lookup_elem(map, key);
635 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
638 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
642 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
643 .func = bpf_sk_redirect_map,
645 .ret_type = RET_INTEGER,
646 .arg1_type = ARG_PTR_TO_CTX,
647 .arg2_type = ARG_CONST_MAP_PTR,
648 .arg3_type = ARG_ANYTHING,
649 .arg4_type = ARG_ANYTHING,
652 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
653 struct bpf_map *, map, u32, key, u64, flags)
657 if (unlikely(flags & ~(BPF_F_INGRESS)))
660 sk = __sock_map_lookup_elem(map, key);
661 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
669 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
670 .func = bpf_msg_redirect_map,
672 .ret_type = RET_INTEGER,
673 .arg1_type = ARG_PTR_TO_CTX,
674 .arg2_type = ARG_CONST_MAP_PTR,
675 .arg3_type = ARG_ANYTHING,
676 .arg4_type = ARG_ANYTHING,
679 struct sock_map_seq_info {
685 struct bpf_iter__sockmap {
686 __bpf_md_ptr(struct bpf_iter_meta *, meta);
687 __bpf_md_ptr(struct bpf_map *, map);
688 __bpf_md_ptr(void *, key);
689 __bpf_md_ptr(struct sock *, sk);
692 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
693 struct bpf_map *map, void *key,
696 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
698 if (unlikely(info->index >= info->map->max_entries))
701 info->sk = __sock_map_lookup_elem(info->map, info->index);
703 /* can't return sk directly, since that might be NULL */
707 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
710 struct sock_map_seq_info *info = seq->private;
715 /* pairs with sock_map_seq_stop */
717 return sock_map_seq_lookup_elem(info);
720 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
723 struct sock_map_seq_info *info = seq->private;
728 return sock_map_seq_lookup_elem(info);
731 static int sock_map_seq_show(struct seq_file *seq, void *v)
734 struct sock_map_seq_info *info = seq->private;
735 struct bpf_iter__sockmap ctx = {};
736 struct bpf_iter_meta meta;
737 struct bpf_prog *prog;
740 prog = bpf_iter_get_info(&meta, !v);
747 ctx.key = &info->index;
751 return bpf_iter_run_prog(prog, &ctx);
754 static void sock_map_seq_stop(struct seq_file *seq, void *v)
758 (void)sock_map_seq_show(seq, NULL);
760 /* pairs with sock_map_seq_start */
764 static const struct seq_operations sock_map_seq_ops = {
765 .start = sock_map_seq_start,
766 .next = sock_map_seq_next,
767 .stop = sock_map_seq_stop,
768 .show = sock_map_seq_show,
771 static int sock_map_init_seq_private(void *priv_data,
772 struct bpf_iter_aux_info *aux)
774 struct sock_map_seq_info *info = priv_data;
776 info->map = aux->map;
780 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
781 .seq_ops = &sock_map_seq_ops,
782 .init_seq_private = sock_map_init_seq_private,
783 .seq_priv_size = sizeof(struct sock_map_seq_info),
786 static int sock_map_btf_id;
787 const struct bpf_map_ops sock_map_ops = {
788 .map_meta_equal = bpf_map_meta_equal,
789 .map_alloc = sock_map_alloc,
790 .map_free = sock_map_free,
791 .map_get_next_key = sock_map_get_next_key,
792 .map_lookup_elem_sys_only = sock_map_lookup_sys,
793 .map_update_elem = sock_map_update_elem,
794 .map_delete_elem = sock_map_delete_elem,
795 .map_lookup_elem = sock_map_lookup,
796 .map_release_uref = sock_map_release_progs,
797 .map_check_btf = map_check_no_btf,
798 .map_btf_name = "bpf_stab",
799 .map_btf_id = &sock_map_btf_id,
800 .iter_seq_info = &sock_map_iter_seq_info,
803 struct bpf_shtab_elem {
807 struct hlist_node node;
811 struct bpf_shtab_bucket {
812 struct hlist_head head;
818 struct bpf_shtab_bucket *buckets;
821 struct sk_psock_progs progs;
825 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
827 return jhash(key, len, 0);
830 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
833 return &htab->buckets[hash & (htab->buckets_num - 1)];
836 static struct bpf_shtab_elem *
837 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
840 struct bpf_shtab_elem *elem;
842 hlist_for_each_entry_rcu(elem, head, node) {
843 if (elem->hash == hash &&
844 !memcmp(&elem->key, key, key_size))
851 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
853 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
854 u32 key_size = map->key_size, hash;
855 struct bpf_shtab_bucket *bucket;
856 struct bpf_shtab_elem *elem;
858 WARN_ON_ONCE(!rcu_read_lock_held());
860 hash = sock_hash_bucket_hash(key, key_size);
861 bucket = sock_hash_select_bucket(htab, hash);
862 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
864 return elem ? elem->sk : NULL;
867 static void sock_hash_free_elem(struct bpf_shtab *htab,
868 struct bpf_shtab_elem *elem)
870 atomic_dec(&htab->count);
871 kfree_rcu(elem, rcu);
874 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
877 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
878 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
879 struct bpf_shtab_bucket *bucket;
881 WARN_ON_ONCE(!rcu_read_lock_held());
882 bucket = sock_hash_select_bucket(htab, elem->hash);
884 /* elem may be deleted in parallel from the map, but access here
885 * is okay since it's going away only after RCU grace period.
886 * However, we need to check whether it's still present.
888 raw_spin_lock_bh(&bucket->lock);
889 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
890 elem->key, map->key_size);
891 if (elem_probe && elem_probe == elem) {
892 hlist_del_rcu(&elem->node);
893 sock_map_unref(elem->sk, elem);
894 sock_hash_free_elem(htab, elem);
896 raw_spin_unlock_bh(&bucket->lock);
899 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
901 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
902 u32 hash, key_size = map->key_size;
903 struct bpf_shtab_bucket *bucket;
904 struct bpf_shtab_elem *elem;
907 hash = sock_hash_bucket_hash(key, key_size);
908 bucket = sock_hash_select_bucket(htab, hash);
910 raw_spin_lock_bh(&bucket->lock);
911 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
913 hlist_del_rcu(&elem->node);
914 sock_map_unref(elem->sk, elem);
915 sock_hash_free_elem(htab, elem);
918 raw_spin_unlock_bh(&bucket->lock);
922 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
923 void *key, u32 key_size,
924 u32 hash, struct sock *sk,
925 struct bpf_shtab_elem *old)
927 struct bpf_shtab_elem *new;
929 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
931 atomic_dec(&htab->count);
932 return ERR_PTR(-E2BIG);
936 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
937 GFP_ATOMIC | __GFP_NOWARN,
938 htab->map.numa_node);
940 atomic_dec(&htab->count);
941 return ERR_PTR(-ENOMEM);
943 memcpy(new->key, key, key_size);
949 static int sock_hash_update_common(struct bpf_map *map, void *key,
950 struct sock *sk, u64 flags)
952 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
953 u32 key_size = map->key_size, hash;
954 struct bpf_shtab_elem *elem, *elem_new;
955 struct bpf_shtab_bucket *bucket;
956 struct sk_psock_link *link;
957 struct sk_psock *psock;
960 WARN_ON_ONCE(!rcu_read_lock_held());
961 if (unlikely(flags > BPF_EXIST))
964 link = sk_psock_init_link();
968 ret = sock_map_link(map, sk);
972 psock = sk_psock(sk);
973 WARN_ON_ONCE(!psock);
975 hash = sock_hash_bucket_hash(key, key_size);
976 bucket = sock_hash_select_bucket(htab, hash);
978 raw_spin_lock_bh(&bucket->lock);
979 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
980 if (elem && flags == BPF_NOEXIST) {
983 } else if (!elem && flags == BPF_EXIST) {
988 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
989 if (IS_ERR(elem_new)) {
990 ret = PTR_ERR(elem_new);
994 sock_map_add_link(psock, link, map, elem_new);
995 /* Add new element to the head of the list, so that
996 * concurrent search will find it before old elem.
998 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1000 hlist_del_rcu(&elem->node);
1001 sock_map_unref(elem->sk, elem);
1002 sock_hash_free_elem(htab, elem);
1004 raw_spin_unlock_bh(&bucket->lock);
1007 raw_spin_unlock_bh(&bucket->lock);
1008 sk_psock_put(sk, psock);
1010 sk_psock_free_link(link);
1014 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1017 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1018 struct bpf_shtab_elem *elem, *elem_next;
1019 u32 hash, key_size = map->key_size;
1020 struct hlist_head *head;
1024 goto find_first_elem;
1025 hash = sock_hash_bucket_hash(key, key_size);
1026 head = &sock_hash_select_bucket(htab, hash)->head;
1027 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1029 goto find_first_elem;
1031 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1032 struct bpf_shtab_elem, node);
1034 memcpy(key_next, elem_next->key, key_size);
1038 i = hash & (htab->buckets_num - 1);
1041 for (; i < htab->buckets_num; i++) {
1042 head = &sock_hash_select_bucket(htab, i)->head;
1043 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1044 struct bpf_shtab_elem, node);
1046 memcpy(key_next, elem_next->key, key_size);
1054 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1056 struct bpf_shtab *htab;
1059 if (!capable(CAP_NET_ADMIN))
1060 return ERR_PTR(-EPERM);
1061 if (attr->max_entries == 0 ||
1062 attr->key_size == 0 ||
1063 (attr->value_size != sizeof(u32) &&
1064 attr->value_size != sizeof(u64)) ||
1065 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1066 return ERR_PTR(-EINVAL);
1067 if (attr->key_size > MAX_BPF_STACK)
1068 return ERR_PTR(-E2BIG);
1070 htab = kzalloc(sizeof(*htab), GFP_USER | __GFP_ACCOUNT);
1072 return ERR_PTR(-ENOMEM);
1074 bpf_map_init_from_attr(&htab->map, attr);
1076 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1077 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1078 round_up(htab->map.key_size, 8);
1079 if (htab->buckets_num == 0 ||
1080 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1085 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1086 sizeof(struct bpf_shtab_bucket),
1087 htab->map.numa_node);
1088 if (!htab->buckets) {
1093 for (i = 0; i < htab->buckets_num; i++) {
1094 INIT_HLIST_HEAD(&htab->buckets[i].head);
1095 raw_spin_lock_init(&htab->buckets[i].lock);
1101 return ERR_PTR(err);
1104 static void sock_hash_free(struct bpf_map *map)
1106 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1107 struct bpf_shtab_bucket *bucket;
1108 struct hlist_head unlink_list;
1109 struct bpf_shtab_elem *elem;
1110 struct hlist_node *node;
1113 /* After the sync no updates or deletes will be in-flight so it
1114 * is safe to walk map and remove entries without risking a race
1115 * in EEXIST update case.
1118 for (i = 0; i < htab->buckets_num; i++) {
1119 bucket = sock_hash_select_bucket(htab, i);
1121 /* We are racing with sock_hash_delete_from_link to
1122 * enter the spin-lock critical section. Every socket on
1123 * the list is still linked to sockhash. Since link
1124 * exists, psock exists and holds a ref to socket. That
1125 * lets us to grab a socket ref too.
1127 raw_spin_lock_bh(&bucket->lock);
1128 hlist_for_each_entry(elem, &bucket->head, node)
1129 sock_hold(elem->sk);
1130 hlist_move_list(&bucket->head, &unlink_list);
1131 raw_spin_unlock_bh(&bucket->lock);
1133 /* Process removed entries out of atomic context to
1134 * block for socket lock before deleting the psock's
1137 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1138 hlist_del(&elem->node);
1139 lock_sock(elem->sk);
1141 sock_map_unref(elem->sk, elem);
1143 release_sock(elem->sk);
1145 sock_hash_free_elem(htab, elem);
1149 /* wait for psock readers accessing its map link */
1152 bpf_map_area_free(htab->buckets);
1156 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1160 if (map->value_size != sizeof(u64))
1161 return ERR_PTR(-ENOSPC);
1163 sk = __sock_hash_lookup_elem(map, key);
1165 return ERR_PTR(-ENOENT);
1167 __sock_gen_cookie(sk);
1168 return &sk->sk_cookie;
1171 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1175 sk = __sock_hash_lookup_elem(map, key);
1178 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1183 static void sock_hash_release_progs(struct bpf_map *map)
1185 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1188 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1189 struct bpf_map *, map, void *, key, u64, flags)
1191 WARN_ON_ONCE(!rcu_read_lock_held());
1193 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1194 sock_map_op_okay(sops)))
1195 return sock_hash_update_common(map, key, sops->sk, flags);
1199 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1200 .func = bpf_sock_hash_update,
1203 .ret_type = RET_INTEGER,
1204 .arg1_type = ARG_PTR_TO_CTX,
1205 .arg2_type = ARG_CONST_MAP_PTR,
1206 .arg3_type = ARG_PTR_TO_MAP_KEY,
1207 .arg4_type = ARG_ANYTHING,
1210 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1211 struct bpf_map *, map, void *, key, u64, flags)
1215 if (unlikely(flags & ~(BPF_F_INGRESS)))
1218 sk = __sock_hash_lookup_elem(map, key);
1219 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1222 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
1226 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1227 .func = bpf_sk_redirect_hash,
1229 .ret_type = RET_INTEGER,
1230 .arg1_type = ARG_PTR_TO_CTX,
1231 .arg2_type = ARG_CONST_MAP_PTR,
1232 .arg3_type = ARG_PTR_TO_MAP_KEY,
1233 .arg4_type = ARG_ANYTHING,
1236 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1237 struct bpf_map *, map, void *, key, u64, flags)
1241 if (unlikely(flags & ~(BPF_F_INGRESS)))
1244 sk = __sock_hash_lookup_elem(map, key);
1245 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1253 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1254 .func = bpf_msg_redirect_hash,
1256 .ret_type = RET_INTEGER,
1257 .arg1_type = ARG_PTR_TO_CTX,
1258 .arg2_type = ARG_CONST_MAP_PTR,
1259 .arg3_type = ARG_PTR_TO_MAP_KEY,
1260 .arg4_type = ARG_ANYTHING,
1263 struct sock_hash_seq_info {
1264 struct bpf_map *map;
1265 struct bpf_shtab *htab;
1269 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1270 struct bpf_shtab_elem *prev_elem)
1272 const struct bpf_shtab *htab = info->htab;
1273 struct bpf_shtab_bucket *bucket;
1274 struct bpf_shtab_elem *elem;
1275 struct hlist_node *node;
1277 /* try to find next elem in the same bucket */
1279 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1280 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1284 /* no more elements, continue in the next bucket */
1288 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1289 bucket = &htab->buckets[info->bucket_id];
1290 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1291 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1299 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1302 struct sock_hash_seq_info *info = seq->private;
1307 /* pairs with sock_hash_seq_stop */
1309 return sock_hash_seq_find_next(info, NULL);
1312 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1315 struct sock_hash_seq_info *info = seq->private;
1318 return sock_hash_seq_find_next(info, v);
1321 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1324 struct sock_hash_seq_info *info = seq->private;
1325 struct bpf_iter__sockmap ctx = {};
1326 struct bpf_shtab_elem *elem = v;
1327 struct bpf_iter_meta meta;
1328 struct bpf_prog *prog;
1331 prog = bpf_iter_get_info(&meta, !elem);
1336 ctx.map = info->map;
1338 ctx.key = elem->key;
1342 return bpf_iter_run_prog(prog, &ctx);
1345 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1349 (void)sock_hash_seq_show(seq, NULL);
1351 /* pairs with sock_hash_seq_start */
1355 static const struct seq_operations sock_hash_seq_ops = {
1356 .start = sock_hash_seq_start,
1357 .next = sock_hash_seq_next,
1358 .stop = sock_hash_seq_stop,
1359 .show = sock_hash_seq_show,
1362 static int sock_hash_init_seq_private(void *priv_data,
1363 struct bpf_iter_aux_info *aux)
1365 struct sock_hash_seq_info *info = priv_data;
1367 info->map = aux->map;
1368 info->htab = container_of(aux->map, struct bpf_shtab, map);
1372 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1373 .seq_ops = &sock_hash_seq_ops,
1374 .init_seq_private = sock_hash_init_seq_private,
1375 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1378 static int sock_hash_map_btf_id;
1379 const struct bpf_map_ops sock_hash_ops = {
1380 .map_meta_equal = bpf_map_meta_equal,
1381 .map_alloc = sock_hash_alloc,
1382 .map_free = sock_hash_free,
1383 .map_get_next_key = sock_hash_get_next_key,
1384 .map_update_elem = sock_map_update_elem,
1385 .map_delete_elem = sock_hash_delete_elem,
1386 .map_lookup_elem = sock_hash_lookup,
1387 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1388 .map_release_uref = sock_hash_release_progs,
1389 .map_check_btf = map_check_no_btf,
1390 .map_btf_name = "bpf_shtab",
1391 .map_btf_id = &sock_hash_map_btf_id,
1392 .iter_seq_info = &sock_hash_iter_seq_info,
1395 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1397 switch (map->map_type) {
1398 case BPF_MAP_TYPE_SOCKMAP:
1399 return &container_of(map, struct bpf_stab, map)->progs;
1400 case BPF_MAP_TYPE_SOCKHASH:
1401 return &container_of(map, struct bpf_shtab, map)->progs;
1409 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1410 struct bpf_prog *old, u32 which)
1412 struct sk_psock_progs *progs = sock_map_progs(map);
1413 struct bpf_prog **pprog;
1419 case BPF_SK_MSG_VERDICT:
1420 pprog = &progs->msg_parser;
1422 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1423 case BPF_SK_SKB_STREAM_PARSER:
1424 pprog = &progs->stream_parser;
1427 case BPF_SK_SKB_STREAM_VERDICT:
1428 if (progs->skb_verdict)
1430 pprog = &progs->stream_verdict;
1432 case BPF_SK_SKB_VERDICT:
1433 if (progs->stream_verdict)
1435 pprog = &progs->skb_verdict;
1442 return psock_replace_prog(pprog, prog, old);
1444 psock_set_prog(pprog, prog);
1448 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1450 switch (link->map->map_type) {
1451 case BPF_MAP_TYPE_SOCKMAP:
1452 return sock_map_delete_from_link(link->map, sk,
1454 case BPF_MAP_TYPE_SOCKHASH:
1455 return sock_hash_delete_from_link(link->map, sk,
1462 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1464 struct sk_psock_link *link;
1466 while ((link = sk_psock_link_pop(psock))) {
1467 sock_map_unlink(sk, link);
1468 sk_psock_free_link(link);
1472 void sock_map_unhash(struct sock *sk)
1474 void (*saved_unhash)(struct sock *sk);
1475 struct sk_psock *psock;
1478 psock = sk_psock(sk);
1479 if (unlikely(!psock)) {
1481 if (sk->sk_prot->unhash)
1482 sk->sk_prot->unhash(sk);
1486 saved_unhash = psock->saved_unhash;
1487 sock_map_remove_links(sk, psock);
1491 EXPORT_SYMBOL_GPL(sock_map_unhash);
1493 void sock_map_close(struct sock *sk, long timeout)
1495 void (*saved_close)(struct sock *sk, long timeout);
1496 struct sk_psock *psock;
1500 psock = sk_psock_get(sk);
1501 if (unlikely(!psock)) {
1504 return sk->sk_prot->close(sk, timeout);
1507 saved_close = psock->saved_close;
1508 sock_map_remove_links(sk, psock);
1510 sk_psock_stop(psock, true);
1511 sk_psock_put(sk, psock);
1513 saved_close(sk, timeout);
1515 EXPORT_SYMBOL_GPL(sock_map_close);
1517 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1518 union bpf_iter_link_info *linfo,
1519 struct bpf_iter_aux_info *aux)
1521 struct bpf_map *map;
1524 if (!linfo->map.map_fd)
1527 map = bpf_map_get_with_uref(linfo->map.map_fd);
1529 return PTR_ERR(map);
1531 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1532 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1535 if (prog->aux->max_rdonly_access > map->key_size) {
1544 bpf_map_put_with_uref(map);
1548 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1550 bpf_map_put_with_uref(aux->map);
1553 static struct bpf_iter_reg sock_map_iter_reg = {
1554 .target = "sockmap",
1555 .attach_target = sock_map_iter_attach_target,
1556 .detach_target = sock_map_iter_detach_target,
1557 .show_fdinfo = bpf_iter_map_show_fdinfo,
1558 .fill_link_info = bpf_iter_map_fill_link_info,
1559 .ctx_arg_info_size = 2,
1561 { offsetof(struct bpf_iter__sockmap, key),
1562 PTR_TO_RDONLY_BUF_OR_NULL },
1563 { offsetof(struct bpf_iter__sockmap, sk),
1564 PTR_TO_BTF_ID_OR_NULL },
1568 static int __init bpf_sockmap_iter_init(void)
1570 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1571 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1572 return bpf_iter_reg_target(&sock_map_iter_reg);
1574 late_initcall(bpf_sockmap_iter_init);