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 struct bpf_map *sock_map_alloc(union bpf_attr *attr)
29 struct bpf_stab *stab;
33 if (!capable(CAP_NET_ADMIN))
34 return ERR_PTR(-EPERM);
35 if (attr->max_entries == 0 ||
36 attr->key_size != 4 ||
37 (attr->value_size != sizeof(u32) &&
38 attr->value_size != sizeof(u64)) ||
39 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
40 return ERR_PTR(-EINVAL);
42 stab = kzalloc(sizeof(*stab), GFP_USER);
44 return ERR_PTR(-ENOMEM);
46 bpf_map_init_from_attr(&stab->map, attr);
47 raw_spin_lock_init(&stab->lock);
49 /* Make sure page count doesn't overflow. */
50 cost = (u64) stab->map.max_entries * sizeof(struct sock *);
51 err = bpf_map_charge_init(&stab->map.memory, cost);
55 stab->sks = bpf_map_area_alloc(stab->map.max_entries *
56 sizeof(struct sock *),
61 bpf_map_charge_finish(&stab->map.memory);
67 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
69 u32 ufd = attr->target_fd;
74 if (attr->attach_flags || attr->replace_bpf_fd)
78 map = __bpf_map_get(f);
81 ret = sock_map_prog_update(map, prog, NULL, attr->attach_type);
86 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
88 u32 ufd = attr->target_fd;
89 struct bpf_prog *prog;
94 if (attr->attach_flags || attr->replace_bpf_fd)
98 map = __bpf_map_get(f);
102 prog = bpf_prog_get(attr->attach_bpf_fd);
108 if (prog->type != ptype) {
113 ret = sock_map_prog_update(map, NULL, prog, attr->attach_type);
121 static void sock_map_sk_acquire(struct sock *sk)
122 __acquires(&sk->sk_lock.slock)
129 static void sock_map_sk_release(struct sock *sk)
130 __releases(&sk->sk_lock.slock)
137 static void sock_map_add_link(struct sk_psock *psock,
138 struct sk_psock_link *link,
139 struct bpf_map *map, void *link_raw)
141 link->link_raw = link_raw;
143 spin_lock_bh(&psock->link_lock);
144 list_add_tail(&link->list, &psock->link);
145 spin_unlock_bh(&psock->link_lock);
148 static void sock_map_del_link(struct sock *sk,
149 struct sk_psock *psock, void *link_raw)
151 struct sk_psock_link *link, *tmp;
152 bool strp_stop = false;
154 spin_lock_bh(&psock->link_lock);
155 list_for_each_entry_safe(link, tmp, &psock->link, list) {
156 if (link->link_raw == link_raw) {
157 struct bpf_map *map = link->map;
158 struct bpf_stab *stab = container_of(map, struct bpf_stab,
160 if (psock->parser.enabled && stab->progs.skb_parser)
162 list_del(&link->list);
163 sk_psock_free_link(link);
166 spin_unlock_bh(&psock->link_lock);
168 write_lock_bh(&sk->sk_callback_lock);
169 sk_psock_stop_strp(sk, psock);
170 write_unlock_bh(&sk->sk_callback_lock);
174 static void sock_map_unref(struct sock *sk, void *link_raw)
176 struct sk_psock *psock = sk_psock(sk);
179 sock_map_del_link(sk, psock, link_raw);
180 sk_psock_put(sk, psock);
184 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
188 switch (sk->sk_type) {
190 prot = tcp_bpf_get_proto(sk, psock);
194 prot = udp_bpf_get_proto(sk, psock);
202 return PTR_ERR(prot);
204 sk_psock_update_proto(sk, psock, prot);
208 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
210 struct sk_psock *psock;
213 psock = sk_psock(sk);
215 if (sk->sk_prot->close != sock_map_close) {
216 psock = ERR_PTR(-EBUSY);
220 if (!refcount_inc_not_zero(&psock->refcnt))
221 psock = ERR_PTR(-EBUSY);
228 static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
231 struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
232 struct sk_psock *psock;
236 skb_verdict = READ_ONCE(progs->skb_verdict);
237 skb_parser = READ_ONCE(progs->skb_parser);
238 skb_progs = skb_parser && skb_verdict;
240 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
241 if (IS_ERR(skb_verdict))
242 return PTR_ERR(skb_verdict);
243 skb_parser = bpf_prog_inc_not_zero(skb_parser);
244 if (IS_ERR(skb_parser)) {
245 bpf_prog_put(skb_verdict);
246 return PTR_ERR(skb_parser);
250 msg_parser = READ_ONCE(progs->msg_parser);
252 msg_parser = bpf_prog_inc_not_zero(msg_parser);
253 if (IS_ERR(msg_parser)) {
254 ret = PTR_ERR(msg_parser);
259 psock = sock_map_psock_get_checked(sk);
261 ret = PTR_ERR(psock);
266 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
267 (skb_progs && READ_ONCE(psock->progs.skb_parser))) {
268 sk_psock_put(sk, psock);
273 psock = sk_psock_init(sk, map->numa_node);
275 ret = PTR_ERR(psock);
281 psock_set_prog(&psock->progs.msg_parser, msg_parser);
283 ret = sock_map_init_proto(sk, psock);
287 write_lock_bh(&sk->sk_callback_lock);
288 if (skb_progs && !psock->parser.enabled) {
289 ret = sk_psock_init_strp(sk, psock);
291 write_unlock_bh(&sk->sk_callback_lock);
294 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
295 psock_set_prog(&psock->progs.skb_parser, skb_parser);
296 sk_psock_start_strp(sk, psock);
298 write_unlock_bh(&sk->sk_callback_lock);
301 sk_psock_put(sk, psock);
304 bpf_prog_put(msg_parser);
307 bpf_prog_put(skb_verdict);
308 bpf_prog_put(skb_parser);
313 static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)
315 struct sk_psock *psock;
318 psock = sock_map_psock_get_checked(sk);
320 return PTR_ERR(psock);
323 psock = sk_psock_init(sk, map->numa_node);
325 return PTR_ERR(psock);
328 ret = sock_map_init_proto(sk, psock);
330 sk_psock_put(sk, psock);
334 static void sock_map_free(struct bpf_map *map)
336 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
339 /* After the sync no updates or deletes will be in-flight so it
340 * is safe to walk map and remove entries without risking a race
341 * in EEXIST update case.
344 for (i = 0; i < stab->map.max_entries; i++) {
345 struct sock **psk = &stab->sks[i];
348 sk = xchg(psk, NULL);
352 sock_map_unref(sk, psk);
358 /* wait for psock readers accessing its map link */
361 bpf_map_area_free(stab->sks);
365 static void sock_map_release_progs(struct bpf_map *map)
367 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
370 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
372 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
374 WARN_ON_ONCE(!rcu_read_lock_held());
376 if (unlikely(key >= map->max_entries))
378 return READ_ONCE(stab->sks[key]);
381 static void *sock_map_lookup(struct bpf_map *map, void *key)
385 sk = __sock_map_lookup_elem(map, *(u32 *)key);
388 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
393 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
397 if (map->value_size != sizeof(u64))
398 return ERR_PTR(-ENOSPC);
400 sk = __sock_map_lookup_elem(map, *(u32 *)key);
402 return ERR_PTR(-ENOENT);
404 __sock_gen_cookie(sk);
405 return &sk->sk_cookie;
408 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
414 raw_spin_lock_bh(&stab->lock);
416 if (!sk_test || sk_test == sk)
417 sk = xchg(psk, NULL);
420 sock_map_unref(sk, psk);
424 raw_spin_unlock_bh(&stab->lock);
428 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
431 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
433 __sock_map_delete(stab, sk, link_raw);
436 static int sock_map_delete_elem(struct bpf_map *map, void *key)
438 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
442 if (unlikely(i >= map->max_entries))
446 return __sock_map_delete(stab, NULL, psk);
449 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
451 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
452 u32 i = key ? *(u32 *)key : U32_MAX;
453 u32 *key_next = next;
455 if (i == stab->map.max_entries - 1)
457 if (i >= stab->map.max_entries)
464 static bool sock_map_redirect_allowed(const struct sock *sk);
466 static int sock_map_update_common(struct bpf_map *map, u32 idx,
467 struct sock *sk, u64 flags)
469 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
470 struct sk_psock_link *link;
471 struct sk_psock *psock;
475 WARN_ON_ONCE(!rcu_read_lock_held());
476 if (unlikely(flags > BPF_EXIST))
478 if (unlikely(idx >= map->max_entries))
481 link = sk_psock_init_link();
485 /* Only sockets we can redirect into/from in BPF need to hold
486 * refs to parser/verdict progs and have their sk_data_ready
487 * and sk_write_space callbacks overridden.
489 if (sock_map_redirect_allowed(sk))
490 ret = sock_map_link(map, &stab->progs, sk);
492 ret = sock_map_link_no_progs(map, sk);
496 psock = sk_psock(sk);
497 WARN_ON_ONCE(!psock);
499 raw_spin_lock_bh(&stab->lock);
500 osk = stab->sks[idx];
501 if (osk && flags == BPF_NOEXIST) {
504 } else if (!osk && flags == BPF_EXIST) {
509 sock_map_add_link(psock, link, map, &stab->sks[idx]);
512 sock_map_unref(osk, &stab->sks[idx]);
513 raw_spin_unlock_bh(&stab->lock);
516 raw_spin_unlock_bh(&stab->lock);
518 sk_psock_put(sk, psock);
520 sk_psock_free_link(link);
524 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
526 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
527 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
528 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
531 static bool sk_is_tcp(const struct sock *sk)
533 return sk->sk_type == SOCK_STREAM &&
534 sk->sk_protocol == IPPROTO_TCP;
537 static bool sk_is_udp(const struct sock *sk)
539 return sk->sk_type == SOCK_DGRAM &&
540 sk->sk_protocol == IPPROTO_UDP;
543 static bool sock_map_redirect_allowed(const struct sock *sk)
545 return sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN;
548 static bool sock_map_sk_is_suitable(const struct sock *sk)
550 return sk_is_tcp(sk) || sk_is_udp(sk);
553 static bool sock_map_sk_state_allowed(const struct sock *sk)
556 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
557 else if (sk_is_udp(sk))
558 return sk_hashed(sk);
563 static int sock_hash_update_common(struct bpf_map *map, void *key,
564 struct sock *sk, u64 flags);
566 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
574 if (map->value_size == sizeof(u64))
581 sock = sockfd_lookup(ufd, &ret);
589 if (!sock_map_sk_is_suitable(sk)) {
594 sock_map_sk_acquire(sk);
595 if (!sock_map_sk_state_allowed(sk))
597 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
598 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
600 ret = sock_hash_update_common(map, key, sk, flags);
601 sock_map_sk_release(sk);
607 static int sock_map_update_elem(struct bpf_map *map, void *key,
608 void *value, u64 flags)
610 struct sock *sk = (struct sock *)value;
613 if (unlikely(!sk || !sk_fullsock(sk)))
616 if (!sock_map_sk_is_suitable(sk))
621 if (!sock_map_sk_state_allowed(sk))
623 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
624 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
626 ret = sock_hash_update_common(map, key, sk, flags);
632 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
633 struct bpf_map *, map, void *, key, u64, flags)
635 WARN_ON_ONCE(!rcu_read_lock_held());
637 if (likely(sock_map_sk_is_suitable(sops->sk) &&
638 sock_map_op_okay(sops)))
639 return sock_map_update_common(map, *(u32 *)key, sops->sk,
644 const struct bpf_func_proto bpf_sock_map_update_proto = {
645 .func = bpf_sock_map_update,
648 .ret_type = RET_INTEGER,
649 .arg1_type = ARG_PTR_TO_CTX,
650 .arg2_type = ARG_CONST_MAP_PTR,
651 .arg3_type = ARG_PTR_TO_MAP_KEY,
652 .arg4_type = ARG_ANYTHING,
655 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
656 struct bpf_map *, map, u32, key, u64, flags)
658 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
661 if (unlikely(flags & ~(BPF_F_INGRESS)))
664 sk = __sock_map_lookup_elem(map, key);
665 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
668 tcb->bpf.flags = flags;
669 tcb->bpf.sk_redir = sk;
673 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
674 .func = bpf_sk_redirect_map,
676 .ret_type = RET_INTEGER,
677 .arg1_type = ARG_PTR_TO_CTX,
678 .arg2_type = ARG_CONST_MAP_PTR,
679 .arg3_type = ARG_ANYTHING,
680 .arg4_type = ARG_ANYTHING,
683 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
684 struct bpf_map *, map, u32, key, u64, flags)
688 if (unlikely(flags & ~(BPF_F_INGRESS)))
691 sk = __sock_map_lookup_elem(map, key);
692 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
700 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
701 .func = bpf_msg_redirect_map,
703 .ret_type = RET_INTEGER,
704 .arg1_type = ARG_PTR_TO_CTX,
705 .arg2_type = ARG_CONST_MAP_PTR,
706 .arg3_type = ARG_ANYTHING,
707 .arg4_type = ARG_ANYTHING,
710 struct sock_map_seq_info {
716 struct bpf_iter__sockmap {
717 __bpf_md_ptr(struct bpf_iter_meta *, meta);
718 __bpf_md_ptr(struct bpf_map *, map);
719 __bpf_md_ptr(void *, key);
720 __bpf_md_ptr(struct sock *, sk);
723 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
724 struct bpf_map *map, void *key,
727 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
729 if (unlikely(info->index >= info->map->max_entries))
732 info->sk = __sock_map_lookup_elem(info->map, info->index);
734 /* can't return sk directly, since that might be NULL */
738 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
740 struct sock_map_seq_info *info = seq->private;
745 /* pairs with sock_map_seq_stop */
747 return sock_map_seq_lookup_elem(info);
750 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
752 struct sock_map_seq_info *info = seq->private;
757 return sock_map_seq_lookup_elem(info);
760 static int sock_map_seq_show(struct seq_file *seq, void *v)
762 struct sock_map_seq_info *info = seq->private;
763 struct bpf_iter__sockmap ctx = {};
764 struct bpf_iter_meta meta;
765 struct bpf_prog *prog;
768 prog = bpf_iter_get_info(&meta, !v);
775 ctx.key = &info->index;
779 return bpf_iter_run_prog(prog, &ctx);
782 static void sock_map_seq_stop(struct seq_file *seq, void *v)
785 (void)sock_map_seq_show(seq, NULL);
787 /* pairs with sock_map_seq_start */
791 static const struct seq_operations sock_map_seq_ops = {
792 .start = sock_map_seq_start,
793 .next = sock_map_seq_next,
794 .stop = sock_map_seq_stop,
795 .show = sock_map_seq_show,
798 static int sock_map_init_seq_private(void *priv_data,
799 struct bpf_iter_aux_info *aux)
801 struct sock_map_seq_info *info = priv_data;
803 info->map = aux->map;
807 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
808 .seq_ops = &sock_map_seq_ops,
809 .init_seq_private = sock_map_init_seq_private,
810 .seq_priv_size = sizeof(struct sock_map_seq_info),
813 static int sock_map_btf_id;
814 const struct bpf_map_ops sock_map_ops = {
815 .map_meta_equal = bpf_map_meta_equal,
816 .map_alloc = sock_map_alloc,
817 .map_free = sock_map_free,
818 .map_get_next_key = sock_map_get_next_key,
819 .map_lookup_elem_sys_only = sock_map_lookup_sys,
820 .map_update_elem = sock_map_update_elem,
821 .map_delete_elem = sock_map_delete_elem,
822 .map_lookup_elem = sock_map_lookup,
823 .map_release_uref = sock_map_release_progs,
824 .map_check_btf = map_check_no_btf,
825 .map_btf_name = "bpf_stab",
826 .map_btf_id = &sock_map_btf_id,
827 .iter_seq_info = &sock_map_iter_seq_info,
830 struct bpf_shtab_elem {
834 struct hlist_node node;
838 struct bpf_shtab_bucket {
839 struct hlist_head head;
845 struct bpf_shtab_bucket *buckets;
848 struct sk_psock_progs progs;
852 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
854 return jhash(key, len, 0);
857 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
860 return &htab->buckets[hash & (htab->buckets_num - 1)];
863 static struct bpf_shtab_elem *
864 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
867 struct bpf_shtab_elem *elem;
869 hlist_for_each_entry_rcu(elem, head, node) {
870 if (elem->hash == hash &&
871 !memcmp(&elem->key, key, key_size))
878 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
880 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
881 u32 key_size = map->key_size, hash;
882 struct bpf_shtab_bucket *bucket;
883 struct bpf_shtab_elem *elem;
885 WARN_ON_ONCE(!rcu_read_lock_held());
887 hash = sock_hash_bucket_hash(key, key_size);
888 bucket = sock_hash_select_bucket(htab, hash);
889 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
891 return elem ? elem->sk : NULL;
894 static void sock_hash_free_elem(struct bpf_shtab *htab,
895 struct bpf_shtab_elem *elem)
897 atomic_dec(&htab->count);
898 kfree_rcu(elem, rcu);
901 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
904 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
905 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
906 struct bpf_shtab_bucket *bucket;
908 WARN_ON_ONCE(!rcu_read_lock_held());
909 bucket = sock_hash_select_bucket(htab, elem->hash);
911 /* elem may be deleted in parallel from the map, but access here
912 * is okay since it's going away only after RCU grace period.
913 * However, we need to check whether it's still present.
915 raw_spin_lock_bh(&bucket->lock);
916 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
917 elem->key, map->key_size);
918 if (elem_probe && elem_probe == elem) {
919 hlist_del_rcu(&elem->node);
920 sock_map_unref(elem->sk, elem);
921 sock_hash_free_elem(htab, elem);
923 raw_spin_unlock_bh(&bucket->lock);
926 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
928 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
929 u32 hash, key_size = map->key_size;
930 struct bpf_shtab_bucket *bucket;
931 struct bpf_shtab_elem *elem;
934 hash = sock_hash_bucket_hash(key, key_size);
935 bucket = sock_hash_select_bucket(htab, hash);
937 raw_spin_lock_bh(&bucket->lock);
938 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
940 hlist_del_rcu(&elem->node);
941 sock_map_unref(elem->sk, elem);
942 sock_hash_free_elem(htab, elem);
945 raw_spin_unlock_bh(&bucket->lock);
949 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
950 void *key, u32 key_size,
951 u32 hash, struct sock *sk,
952 struct bpf_shtab_elem *old)
954 struct bpf_shtab_elem *new;
956 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
958 atomic_dec(&htab->count);
959 return ERR_PTR(-E2BIG);
963 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
964 htab->map.numa_node);
966 atomic_dec(&htab->count);
967 return ERR_PTR(-ENOMEM);
969 memcpy(new->key, key, key_size);
975 static int sock_hash_update_common(struct bpf_map *map, void *key,
976 struct sock *sk, u64 flags)
978 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
979 u32 key_size = map->key_size, hash;
980 struct bpf_shtab_elem *elem, *elem_new;
981 struct bpf_shtab_bucket *bucket;
982 struct sk_psock_link *link;
983 struct sk_psock *psock;
986 WARN_ON_ONCE(!rcu_read_lock_held());
987 if (unlikely(flags > BPF_EXIST))
990 link = sk_psock_init_link();
994 /* Only sockets we can redirect into/from in BPF need to hold
995 * refs to parser/verdict progs and have their sk_data_ready
996 * and sk_write_space callbacks overridden.
998 if (sock_map_redirect_allowed(sk))
999 ret = sock_map_link(map, &htab->progs, sk);
1001 ret = sock_map_link_no_progs(map, sk);
1005 psock = sk_psock(sk);
1006 WARN_ON_ONCE(!psock);
1008 hash = sock_hash_bucket_hash(key, key_size);
1009 bucket = sock_hash_select_bucket(htab, hash);
1011 raw_spin_lock_bh(&bucket->lock);
1012 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
1013 if (elem && flags == BPF_NOEXIST) {
1016 } else if (!elem && flags == BPF_EXIST) {
1021 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
1022 if (IS_ERR(elem_new)) {
1023 ret = PTR_ERR(elem_new);
1027 sock_map_add_link(psock, link, map, elem_new);
1028 /* Add new element to the head of the list, so that
1029 * concurrent search will find it before old elem.
1031 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1033 hlist_del_rcu(&elem->node);
1034 sock_map_unref(elem->sk, elem);
1035 sock_hash_free_elem(htab, elem);
1037 raw_spin_unlock_bh(&bucket->lock);
1040 raw_spin_unlock_bh(&bucket->lock);
1041 sk_psock_put(sk, psock);
1043 sk_psock_free_link(link);
1047 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1050 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1051 struct bpf_shtab_elem *elem, *elem_next;
1052 u32 hash, key_size = map->key_size;
1053 struct hlist_head *head;
1057 goto find_first_elem;
1058 hash = sock_hash_bucket_hash(key, key_size);
1059 head = &sock_hash_select_bucket(htab, hash)->head;
1060 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1062 goto find_first_elem;
1064 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1065 struct bpf_shtab_elem, node);
1067 memcpy(key_next, elem_next->key, key_size);
1071 i = hash & (htab->buckets_num - 1);
1074 for (; i < htab->buckets_num; i++) {
1075 head = &sock_hash_select_bucket(htab, i)->head;
1076 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1077 struct bpf_shtab_elem, node);
1079 memcpy(key_next, elem_next->key, key_size);
1087 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1089 struct bpf_shtab *htab;
1093 if (!capable(CAP_NET_ADMIN))
1094 return ERR_PTR(-EPERM);
1095 if (attr->max_entries == 0 ||
1096 attr->key_size == 0 ||
1097 (attr->value_size != sizeof(u32) &&
1098 attr->value_size != sizeof(u64)) ||
1099 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1100 return ERR_PTR(-EINVAL);
1101 if (attr->key_size > MAX_BPF_STACK)
1102 return ERR_PTR(-E2BIG);
1104 htab = kzalloc(sizeof(*htab), GFP_USER);
1106 return ERR_PTR(-ENOMEM);
1108 bpf_map_init_from_attr(&htab->map, attr);
1110 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1111 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1112 round_up(htab->map.key_size, 8);
1113 if (htab->buckets_num == 0 ||
1114 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1119 cost = (u64) htab->buckets_num * sizeof(struct bpf_shtab_bucket) +
1120 (u64) htab->elem_size * htab->map.max_entries;
1121 if (cost >= U32_MAX - PAGE_SIZE) {
1125 err = bpf_map_charge_init(&htab->map.memory, cost);
1129 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1130 sizeof(struct bpf_shtab_bucket),
1131 htab->map.numa_node);
1132 if (!htab->buckets) {
1133 bpf_map_charge_finish(&htab->map.memory);
1138 for (i = 0; i < htab->buckets_num; i++) {
1139 INIT_HLIST_HEAD(&htab->buckets[i].head);
1140 raw_spin_lock_init(&htab->buckets[i].lock);
1146 return ERR_PTR(err);
1149 static void sock_hash_free(struct bpf_map *map)
1151 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1152 struct bpf_shtab_bucket *bucket;
1153 struct hlist_head unlink_list;
1154 struct bpf_shtab_elem *elem;
1155 struct hlist_node *node;
1158 /* After the sync no updates or deletes will be in-flight so it
1159 * is safe to walk map and remove entries without risking a race
1160 * in EEXIST update case.
1163 for (i = 0; i < htab->buckets_num; i++) {
1164 bucket = sock_hash_select_bucket(htab, i);
1166 /* We are racing with sock_hash_delete_from_link to
1167 * enter the spin-lock critical section. Every socket on
1168 * the list is still linked to sockhash. Since link
1169 * exists, psock exists and holds a ref to socket. That
1170 * lets us to grab a socket ref too.
1172 raw_spin_lock_bh(&bucket->lock);
1173 hlist_for_each_entry(elem, &bucket->head, node)
1174 sock_hold(elem->sk);
1175 hlist_move_list(&bucket->head, &unlink_list);
1176 raw_spin_unlock_bh(&bucket->lock);
1178 /* Process removed entries out of atomic context to
1179 * block for socket lock before deleting the psock's
1182 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1183 hlist_del(&elem->node);
1184 lock_sock(elem->sk);
1186 sock_map_unref(elem->sk, elem);
1188 release_sock(elem->sk);
1190 sock_hash_free_elem(htab, elem);
1194 /* wait for psock readers accessing its map link */
1197 bpf_map_area_free(htab->buckets);
1201 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1205 if (map->value_size != sizeof(u64))
1206 return ERR_PTR(-ENOSPC);
1208 sk = __sock_hash_lookup_elem(map, key);
1210 return ERR_PTR(-ENOENT);
1212 __sock_gen_cookie(sk);
1213 return &sk->sk_cookie;
1216 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1220 sk = __sock_hash_lookup_elem(map, key);
1223 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1228 static void sock_hash_release_progs(struct bpf_map *map)
1230 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1233 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1234 struct bpf_map *, map, void *, key, u64, flags)
1236 WARN_ON_ONCE(!rcu_read_lock_held());
1238 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1239 sock_map_op_okay(sops)))
1240 return sock_hash_update_common(map, key, sops->sk, flags);
1244 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1245 .func = bpf_sock_hash_update,
1248 .ret_type = RET_INTEGER,
1249 .arg1_type = ARG_PTR_TO_CTX,
1250 .arg2_type = ARG_CONST_MAP_PTR,
1251 .arg3_type = ARG_PTR_TO_MAP_KEY,
1252 .arg4_type = ARG_ANYTHING,
1255 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1256 struct bpf_map *, map, void *, key, u64, flags)
1258 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1261 if (unlikely(flags & ~(BPF_F_INGRESS)))
1264 sk = __sock_hash_lookup_elem(map, key);
1265 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1268 tcb->bpf.flags = flags;
1269 tcb->bpf.sk_redir = sk;
1273 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1274 .func = bpf_sk_redirect_hash,
1276 .ret_type = RET_INTEGER,
1277 .arg1_type = ARG_PTR_TO_CTX,
1278 .arg2_type = ARG_CONST_MAP_PTR,
1279 .arg3_type = ARG_PTR_TO_MAP_KEY,
1280 .arg4_type = ARG_ANYTHING,
1283 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1284 struct bpf_map *, map, void *, key, u64, flags)
1288 if (unlikely(flags & ~(BPF_F_INGRESS)))
1291 sk = __sock_hash_lookup_elem(map, key);
1292 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1300 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1301 .func = bpf_msg_redirect_hash,
1303 .ret_type = RET_INTEGER,
1304 .arg1_type = ARG_PTR_TO_CTX,
1305 .arg2_type = ARG_CONST_MAP_PTR,
1306 .arg3_type = ARG_PTR_TO_MAP_KEY,
1307 .arg4_type = ARG_ANYTHING,
1310 struct sock_hash_seq_info {
1311 struct bpf_map *map;
1312 struct bpf_shtab *htab;
1316 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1317 struct bpf_shtab_elem *prev_elem)
1319 const struct bpf_shtab *htab = info->htab;
1320 struct bpf_shtab_bucket *bucket;
1321 struct bpf_shtab_elem *elem;
1322 struct hlist_node *node;
1324 /* try to find next elem in the same bucket */
1326 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1327 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1331 /* no more elements, continue in the next bucket */
1335 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1336 bucket = &htab->buckets[info->bucket_id];
1337 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1338 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1346 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1348 struct sock_hash_seq_info *info = seq->private;
1353 /* pairs with sock_hash_seq_stop */
1355 return sock_hash_seq_find_next(info, NULL);
1358 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1360 struct sock_hash_seq_info *info = seq->private;
1363 return sock_hash_seq_find_next(info, v);
1366 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1368 struct sock_hash_seq_info *info = seq->private;
1369 struct bpf_iter__sockmap ctx = {};
1370 struct bpf_shtab_elem *elem = v;
1371 struct bpf_iter_meta meta;
1372 struct bpf_prog *prog;
1375 prog = bpf_iter_get_info(&meta, !elem);
1380 ctx.map = info->map;
1382 ctx.key = elem->key;
1386 return bpf_iter_run_prog(prog, &ctx);
1389 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1392 (void)sock_hash_seq_show(seq, NULL);
1394 /* pairs with sock_hash_seq_start */
1398 static const struct seq_operations sock_hash_seq_ops = {
1399 .start = sock_hash_seq_start,
1400 .next = sock_hash_seq_next,
1401 .stop = sock_hash_seq_stop,
1402 .show = sock_hash_seq_show,
1405 static int sock_hash_init_seq_private(void *priv_data,
1406 struct bpf_iter_aux_info *aux)
1408 struct sock_hash_seq_info *info = priv_data;
1410 info->map = aux->map;
1411 info->htab = container_of(aux->map, struct bpf_shtab, map);
1415 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1416 .seq_ops = &sock_hash_seq_ops,
1417 .init_seq_private = sock_hash_init_seq_private,
1418 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1421 static int sock_hash_map_btf_id;
1422 const struct bpf_map_ops sock_hash_ops = {
1423 .map_meta_equal = bpf_map_meta_equal,
1424 .map_alloc = sock_hash_alloc,
1425 .map_free = sock_hash_free,
1426 .map_get_next_key = sock_hash_get_next_key,
1427 .map_update_elem = sock_map_update_elem,
1428 .map_delete_elem = sock_hash_delete_elem,
1429 .map_lookup_elem = sock_hash_lookup,
1430 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1431 .map_release_uref = sock_hash_release_progs,
1432 .map_check_btf = map_check_no_btf,
1433 .map_btf_name = "bpf_shtab",
1434 .map_btf_id = &sock_hash_map_btf_id,
1435 .iter_seq_info = &sock_hash_iter_seq_info,
1438 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1440 switch (map->map_type) {
1441 case BPF_MAP_TYPE_SOCKMAP:
1442 return &container_of(map, struct bpf_stab, map)->progs;
1443 case BPF_MAP_TYPE_SOCKHASH:
1444 return &container_of(map, struct bpf_shtab, map)->progs;
1452 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1453 struct bpf_prog *old, u32 which)
1455 struct sk_psock_progs *progs = sock_map_progs(map);
1456 struct bpf_prog **pprog;
1462 case BPF_SK_MSG_VERDICT:
1463 pprog = &progs->msg_parser;
1465 case BPF_SK_SKB_STREAM_PARSER:
1466 pprog = &progs->skb_parser;
1468 case BPF_SK_SKB_STREAM_VERDICT:
1469 pprog = &progs->skb_verdict;
1476 return psock_replace_prog(pprog, prog, old);
1478 psock_set_prog(pprog, prog);
1482 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1484 switch (link->map->map_type) {
1485 case BPF_MAP_TYPE_SOCKMAP:
1486 return sock_map_delete_from_link(link->map, sk,
1488 case BPF_MAP_TYPE_SOCKHASH:
1489 return sock_hash_delete_from_link(link->map, sk,
1496 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1498 struct sk_psock_link *link;
1500 while ((link = sk_psock_link_pop(psock))) {
1501 sock_map_unlink(sk, link);
1502 sk_psock_free_link(link);
1506 void sock_map_unhash(struct sock *sk)
1508 void (*saved_unhash)(struct sock *sk);
1509 struct sk_psock *psock;
1512 psock = sk_psock(sk);
1513 if (unlikely(!psock)) {
1515 if (sk->sk_prot->unhash)
1516 sk->sk_prot->unhash(sk);
1520 saved_unhash = psock->saved_unhash;
1521 sock_map_remove_links(sk, psock);
1526 void sock_map_close(struct sock *sk, long timeout)
1528 void (*saved_close)(struct sock *sk, long timeout);
1529 struct sk_psock *psock;
1533 psock = sk_psock(sk);
1534 if (unlikely(!psock)) {
1537 return sk->sk_prot->close(sk, timeout);
1540 saved_close = psock->saved_close;
1541 sock_map_remove_links(sk, psock);
1544 saved_close(sk, timeout);
1547 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1548 union bpf_iter_link_info *linfo,
1549 struct bpf_iter_aux_info *aux)
1551 struct bpf_map *map;
1554 if (!linfo->map.map_fd)
1557 map = bpf_map_get_with_uref(linfo->map.map_fd);
1559 return PTR_ERR(map);
1561 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1562 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1565 if (prog->aux->max_rdonly_access > map->key_size) {
1574 bpf_map_put_with_uref(map);
1578 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1580 bpf_map_put_with_uref(aux->map);
1583 static struct bpf_iter_reg sock_map_iter_reg = {
1584 .target = "sockmap",
1585 .attach_target = sock_map_iter_attach_target,
1586 .detach_target = sock_map_iter_detach_target,
1587 .show_fdinfo = bpf_iter_map_show_fdinfo,
1588 .fill_link_info = bpf_iter_map_fill_link_info,
1589 .ctx_arg_info_size = 2,
1591 { offsetof(struct bpf_iter__sockmap, key),
1592 PTR_TO_RDONLY_BUF_OR_NULL },
1593 { offsetof(struct bpf_iter__sockmap, sk),
1594 PTR_TO_BTF_ID_OR_NULL },
1598 static int __init bpf_sockmap_iter_init(void)
1600 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1601 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1602 return bpf_iter_reg_target(&sock_map_iter_reg);
1604 late_initcall(bpf_sockmap_iter_init);