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 /* This mutex is used to
28 * - protect race between prog/link attach/detach and link prog update, and
29 * - protect race between releasing and accessing map in bpf_link.
30 * A single global mutex lock is used since it is expected contention is low.
32 static DEFINE_MUTEX(sockmap_mutex);
34 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
35 struct bpf_prog *old, struct bpf_link *link,
37 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map);
39 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
41 struct bpf_stab *stab;
43 if (attr->max_entries == 0 ||
44 attr->key_size != 4 ||
45 (attr->value_size != sizeof(u32) &&
46 attr->value_size != sizeof(u64)) ||
47 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
48 return ERR_PTR(-EINVAL);
50 stab = bpf_map_area_alloc(sizeof(*stab), NUMA_NO_NODE);
52 return ERR_PTR(-ENOMEM);
54 bpf_map_init_from_attr(&stab->map, attr);
55 spin_lock_init(&stab->lock);
57 stab->sks = bpf_map_area_alloc((u64) stab->map.max_entries *
58 sizeof(struct sock *),
61 bpf_map_area_free(stab);
62 return ERR_PTR(-ENOMEM);
68 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
70 u32 ufd = attr->target_fd;
75 if (attr->attach_flags || attr->replace_bpf_fd)
79 map = __bpf_map_get(f);
82 mutex_lock(&sockmap_mutex);
83 ret = sock_map_prog_update(map, prog, NULL, NULL, attr->attach_type);
84 mutex_unlock(&sockmap_mutex);
89 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
91 u32 ufd = attr->target_fd;
92 struct bpf_prog *prog;
97 if (attr->attach_flags || attr->replace_bpf_fd)
101 map = __bpf_map_get(f);
105 prog = bpf_prog_get(attr->attach_bpf_fd);
111 if (prog->type != ptype) {
116 mutex_lock(&sockmap_mutex);
117 ret = sock_map_prog_update(map, NULL, prog, NULL, attr->attach_type);
118 mutex_unlock(&sockmap_mutex);
126 static void sock_map_sk_acquire(struct sock *sk)
127 __acquires(&sk->sk_lock.slock)
133 static void sock_map_sk_release(struct sock *sk)
134 __releases(&sk->sk_lock.slock)
140 static void sock_map_add_link(struct sk_psock *psock,
141 struct sk_psock_link *link,
142 struct bpf_map *map, void *link_raw)
144 link->link_raw = link_raw;
146 spin_lock_bh(&psock->link_lock);
147 list_add_tail(&link->list, &psock->link);
148 spin_unlock_bh(&psock->link_lock);
151 static void sock_map_del_link(struct sock *sk,
152 struct sk_psock *psock, void *link_raw)
154 bool strp_stop = false, verdict_stop = false;
155 struct sk_psock_link *link, *tmp;
157 spin_lock_bh(&psock->link_lock);
158 list_for_each_entry_safe(link, tmp, &psock->link, list) {
159 if (link->link_raw == link_raw) {
160 struct bpf_map *map = link->map;
161 struct sk_psock_progs *progs = sock_map_progs(map);
163 if (psock->saved_data_ready && progs->stream_parser)
165 if (psock->saved_data_ready && progs->stream_verdict)
167 if (psock->saved_data_ready && progs->skb_verdict)
169 list_del(&link->list);
170 sk_psock_free_link(link);
173 spin_unlock_bh(&psock->link_lock);
174 if (strp_stop || verdict_stop) {
175 write_lock_bh(&sk->sk_callback_lock);
177 sk_psock_stop_strp(sk, psock);
179 sk_psock_stop_verdict(sk, psock);
181 if (psock->psock_update_sk_prot)
182 psock->psock_update_sk_prot(sk, psock, false);
183 write_unlock_bh(&sk->sk_callback_lock);
187 static void sock_map_unref(struct sock *sk, void *link_raw)
189 struct sk_psock *psock = sk_psock(sk);
192 sock_map_del_link(sk, psock, link_raw);
193 sk_psock_put(sk, psock);
197 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
199 if (!sk->sk_prot->psock_update_sk_prot)
201 psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
202 return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
205 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
207 struct sk_psock *psock;
210 psock = sk_psock(sk);
212 if (sk->sk_prot->close != sock_map_close) {
213 psock = ERR_PTR(-EBUSY);
217 if (!refcount_inc_not_zero(&psock->refcnt))
218 psock = ERR_PTR(-EBUSY);
225 static int sock_map_link(struct bpf_map *map, struct sock *sk)
227 struct sk_psock_progs *progs = sock_map_progs(map);
228 struct bpf_prog *stream_verdict = NULL;
229 struct bpf_prog *stream_parser = NULL;
230 struct bpf_prog *skb_verdict = NULL;
231 struct bpf_prog *msg_parser = NULL;
232 struct sk_psock *psock;
235 stream_verdict = READ_ONCE(progs->stream_verdict);
236 if (stream_verdict) {
237 stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
238 if (IS_ERR(stream_verdict))
239 return PTR_ERR(stream_verdict);
242 stream_parser = READ_ONCE(progs->stream_parser);
244 stream_parser = bpf_prog_inc_not_zero(stream_parser);
245 if (IS_ERR(stream_parser)) {
246 ret = PTR_ERR(stream_parser);
247 goto out_put_stream_verdict;
251 msg_parser = READ_ONCE(progs->msg_parser);
253 msg_parser = bpf_prog_inc_not_zero(msg_parser);
254 if (IS_ERR(msg_parser)) {
255 ret = PTR_ERR(msg_parser);
256 goto out_put_stream_parser;
260 skb_verdict = READ_ONCE(progs->skb_verdict);
262 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
263 if (IS_ERR(skb_verdict)) {
264 ret = PTR_ERR(skb_verdict);
265 goto out_put_msg_parser;
269 psock = sock_map_psock_get_checked(sk);
271 ret = PTR_ERR(psock);
276 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
277 (stream_parser && READ_ONCE(psock->progs.stream_parser)) ||
278 (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
279 (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) ||
280 (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
281 (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) {
282 sk_psock_put(sk, psock);
287 psock = sk_psock_init(sk, map->numa_node);
289 ret = PTR_ERR(psock);
295 psock_set_prog(&psock->progs.msg_parser, msg_parser);
297 psock_set_prog(&psock->progs.stream_parser, stream_parser);
299 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
301 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
303 /* msg_* and stream_* programs references tracked in psock after this
304 * point. Reference dec and cleanup will occur through psock destructor
306 ret = sock_map_init_proto(sk, psock);
308 sk_psock_put(sk, psock);
312 write_lock_bh(&sk->sk_callback_lock);
313 if (stream_parser && stream_verdict && !psock->saved_data_ready) {
314 ret = sk_psock_init_strp(sk, psock);
316 write_unlock_bh(&sk->sk_callback_lock);
317 sk_psock_put(sk, psock);
320 sk_psock_start_strp(sk, psock);
321 } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
322 sk_psock_start_verdict(sk,psock);
323 } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
324 sk_psock_start_verdict(sk, psock);
326 write_unlock_bh(&sk->sk_callback_lock);
330 bpf_prog_put(skb_verdict);
333 bpf_prog_put(msg_parser);
334 out_put_stream_parser:
336 bpf_prog_put(stream_parser);
337 out_put_stream_verdict:
339 bpf_prog_put(stream_verdict);
344 static void sock_map_free(struct bpf_map *map)
346 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
349 /* After the sync no updates or deletes will be in-flight so it
350 * is safe to walk map and remove entries without risking a race
351 * in EEXIST update case.
354 for (i = 0; i < stab->map.max_entries; i++) {
355 struct sock **psk = &stab->sks[i];
358 sk = xchg(psk, NULL);
363 sock_map_unref(sk, psk);
370 /* wait for psock readers accessing its map link */
373 bpf_map_area_free(stab->sks);
374 bpf_map_area_free(stab);
377 static void sock_map_release_progs(struct bpf_map *map)
379 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
382 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
384 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
386 WARN_ON_ONCE(!rcu_read_lock_held());
388 if (unlikely(key >= map->max_entries))
390 return READ_ONCE(stab->sks[key]);
393 static void *sock_map_lookup(struct bpf_map *map, void *key)
397 sk = __sock_map_lookup_elem(map, *(u32 *)key);
400 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
405 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
409 if (map->value_size != sizeof(u64))
410 return ERR_PTR(-ENOSPC);
412 sk = __sock_map_lookup_elem(map, *(u32 *)key);
414 return ERR_PTR(-ENOENT);
416 __sock_gen_cookie(sk);
417 return &sk->sk_cookie;
420 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
426 spin_lock_bh(&stab->lock);
428 if (!sk_test || sk_test == sk)
429 sk = xchg(psk, NULL);
432 sock_map_unref(sk, psk);
436 spin_unlock_bh(&stab->lock);
440 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
443 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
445 __sock_map_delete(stab, sk, link_raw);
448 static long sock_map_delete_elem(struct bpf_map *map, void *key)
450 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
454 if (unlikely(i >= map->max_entries))
458 return __sock_map_delete(stab, NULL, psk);
461 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
463 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
464 u32 i = key ? *(u32 *)key : U32_MAX;
465 u32 *key_next = next;
467 if (i == stab->map.max_entries - 1)
469 if (i >= stab->map.max_entries)
476 static int sock_map_update_common(struct bpf_map *map, u32 idx,
477 struct sock *sk, u64 flags)
479 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
480 struct sk_psock_link *link;
481 struct sk_psock *psock;
485 WARN_ON_ONCE(!rcu_read_lock_held());
486 if (unlikely(flags > BPF_EXIST))
488 if (unlikely(idx >= map->max_entries))
491 link = sk_psock_init_link();
495 ret = sock_map_link(map, sk);
499 psock = sk_psock(sk);
500 WARN_ON_ONCE(!psock);
502 spin_lock_bh(&stab->lock);
503 osk = stab->sks[idx];
504 if (osk && flags == BPF_NOEXIST) {
507 } else if (!osk && flags == BPF_EXIST) {
512 sock_map_add_link(psock, link, map, &stab->sks[idx]);
515 sock_map_unref(osk, &stab->sks[idx]);
516 spin_unlock_bh(&stab->lock);
519 spin_unlock_bh(&stab->lock);
521 sk_psock_put(sk, psock);
523 sk_psock_free_link(link);
527 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
529 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
530 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
531 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
534 static bool sock_map_redirect_allowed(const struct sock *sk)
537 return sk->sk_state != TCP_LISTEN;
539 return sk->sk_state == TCP_ESTABLISHED;
542 static bool sock_map_sk_is_suitable(const struct sock *sk)
544 return !!sk->sk_prot->psock_update_sk_prot;
547 static bool sock_map_sk_state_allowed(const struct sock *sk)
550 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
551 if (sk_is_stream_unix(sk))
552 return (1 << sk->sk_state) & TCPF_ESTABLISHED;
556 static int sock_hash_update_common(struct bpf_map *map, void *key,
557 struct sock *sk, u64 flags);
559 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
567 if (map->value_size == sizeof(u64))
574 sock = sockfd_lookup(ufd, &ret);
582 if (!sock_map_sk_is_suitable(sk)) {
587 sock_map_sk_acquire(sk);
588 if (!sock_map_sk_state_allowed(sk))
590 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
591 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
593 ret = sock_hash_update_common(map, key, sk, flags);
594 sock_map_sk_release(sk);
600 static long sock_map_update_elem(struct bpf_map *map, void *key,
601 void *value, u64 flags)
603 struct sock *sk = (struct sock *)value;
606 if (unlikely(!sk || !sk_fullsock(sk)))
609 if (!sock_map_sk_is_suitable(sk))
614 if (!sock_map_sk_state_allowed(sk))
616 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
617 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
619 ret = sock_hash_update_common(map, key, sk, flags);
625 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
626 struct bpf_map *, map, void *, key, u64, flags)
628 WARN_ON_ONCE(!rcu_read_lock_held());
630 if (likely(sock_map_sk_is_suitable(sops->sk) &&
631 sock_map_op_okay(sops)))
632 return sock_map_update_common(map, *(u32 *)key, sops->sk,
637 const struct bpf_func_proto bpf_sock_map_update_proto = {
638 .func = bpf_sock_map_update,
641 .ret_type = RET_INTEGER,
642 .arg1_type = ARG_PTR_TO_CTX,
643 .arg2_type = ARG_CONST_MAP_PTR,
644 .arg3_type = ARG_PTR_TO_MAP_KEY,
645 .arg4_type = ARG_ANYTHING,
648 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
649 struct bpf_map *, map, u32, key, u64, flags)
653 if (unlikely(flags & ~(BPF_F_INGRESS)))
656 sk = __sock_map_lookup_elem(map, key);
657 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
660 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
664 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
665 .func = bpf_sk_redirect_map,
667 .ret_type = RET_INTEGER,
668 .arg1_type = ARG_PTR_TO_CTX,
669 .arg2_type = ARG_CONST_MAP_PTR,
670 .arg3_type = ARG_ANYTHING,
671 .arg4_type = ARG_ANYTHING,
674 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
675 struct bpf_map *, map, u32, key, u64, flags)
679 if (unlikely(flags & ~(BPF_F_INGRESS)))
682 sk = __sock_map_lookup_elem(map, key);
683 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
685 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
693 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
694 .func = bpf_msg_redirect_map,
696 .ret_type = RET_INTEGER,
697 .arg1_type = ARG_PTR_TO_CTX,
698 .arg2_type = ARG_CONST_MAP_PTR,
699 .arg3_type = ARG_ANYTHING,
700 .arg4_type = ARG_ANYTHING,
703 struct sock_map_seq_info {
709 struct bpf_iter__sockmap {
710 __bpf_md_ptr(struct bpf_iter_meta *, meta);
711 __bpf_md_ptr(struct bpf_map *, map);
712 __bpf_md_ptr(void *, key);
713 __bpf_md_ptr(struct sock *, sk);
716 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
717 struct bpf_map *map, void *key,
720 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
722 if (unlikely(info->index >= info->map->max_entries))
725 info->sk = __sock_map_lookup_elem(info->map, info->index);
727 /* can't return sk directly, since that might be NULL */
731 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
734 struct sock_map_seq_info *info = seq->private;
739 /* pairs with sock_map_seq_stop */
741 return sock_map_seq_lookup_elem(info);
744 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
747 struct sock_map_seq_info *info = seq->private;
752 return sock_map_seq_lookup_elem(info);
755 static int sock_map_seq_show(struct seq_file *seq, void *v)
758 struct sock_map_seq_info *info = seq->private;
759 struct bpf_iter__sockmap ctx = {};
760 struct bpf_iter_meta meta;
761 struct bpf_prog *prog;
764 prog = bpf_iter_get_info(&meta, !v);
771 ctx.key = &info->index;
775 return bpf_iter_run_prog(prog, &ctx);
778 static void sock_map_seq_stop(struct seq_file *seq, void *v)
782 (void)sock_map_seq_show(seq, NULL);
784 /* pairs with sock_map_seq_start */
788 static const struct seq_operations sock_map_seq_ops = {
789 .start = sock_map_seq_start,
790 .next = sock_map_seq_next,
791 .stop = sock_map_seq_stop,
792 .show = sock_map_seq_show,
795 static int sock_map_init_seq_private(void *priv_data,
796 struct bpf_iter_aux_info *aux)
798 struct sock_map_seq_info *info = priv_data;
800 bpf_map_inc_with_uref(aux->map);
801 info->map = aux->map;
805 static void sock_map_fini_seq_private(void *priv_data)
807 struct sock_map_seq_info *info = priv_data;
809 bpf_map_put_with_uref(info->map);
812 static u64 sock_map_mem_usage(const struct bpf_map *map)
814 u64 usage = sizeof(struct bpf_stab);
816 usage += (u64)map->max_entries * sizeof(struct sock *);
820 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
821 .seq_ops = &sock_map_seq_ops,
822 .init_seq_private = sock_map_init_seq_private,
823 .fini_seq_private = sock_map_fini_seq_private,
824 .seq_priv_size = sizeof(struct sock_map_seq_info),
827 BTF_ID_LIST_SINGLE(sock_map_btf_ids, struct, bpf_stab)
828 const struct bpf_map_ops sock_map_ops = {
829 .map_meta_equal = bpf_map_meta_equal,
830 .map_alloc = sock_map_alloc,
831 .map_free = sock_map_free,
832 .map_get_next_key = sock_map_get_next_key,
833 .map_lookup_elem_sys_only = sock_map_lookup_sys,
834 .map_update_elem = sock_map_update_elem,
835 .map_delete_elem = sock_map_delete_elem,
836 .map_lookup_elem = sock_map_lookup,
837 .map_release_uref = sock_map_release_progs,
838 .map_check_btf = map_check_no_btf,
839 .map_mem_usage = sock_map_mem_usage,
840 .map_btf_id = &sock_map_btf_ids[0],
841 .iter_seq_info = &sock_map_iter_seq_info,
844 struct bpf_shtab_elem {
848 struct hlist_node node;
852 struct bpf_shtab_bucket {
853 struct hlist_head head;
859 struct bpf_shtab_bucket *buckets;
862 struct sk_psock_progs progs;
866 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
868 return jhash(key, len, 0);
871 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
874 return &htab->buckets[hash & (htab->buckets_num - 1)];
877 static struct bpf_shtab_elem *
878 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
881 struct bpf_shtab_elem *elem;
883 hlist_for_each_entry_rcu(elem, head, node) {
884 if (elem->hash == hash &&
885 !memcmp(&elem->key, key, key_size))
892 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
894 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
895 u32 key_size = map->key_size, hash;
896 struct bpf_shtab_bucket *bucket;
897 struct bpf_shtab_elem *elem;
899 WARN_ON_ONCE(!rcu_read_lock_held());
901 hash = sock_hash_bucket_hash(key, key_size);
902 bucket = sock_hash_select_bucket(htab, hash);
903 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
905 return elem ? elem->sk : NULL;
908 static void sock_hash_free_elem(struct bpf_shtab *htab,
909 struct bpf_shtab_elem *elem)
911 atomic_dec(&htab->count);
912 kfree_rcu(elem, rcu);
915 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
918 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
919 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
920 struct bpf_shtab_bucket *bucket;
922 WARN_ON_ONCE(!rcu_read_lock_held());
923 bucket = sock_hash_select_bucket(htab, elem->hash);
925 /* elem may be deleted in parallel from the map, but access here
926 * is okay since it's going away only after RCU grace period.
927 * However, we need to check whether it's still present.
929 spin_lock_bh(&bucket->lock);
930 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
931 elem->key, map->key_size);
932 if (elem_probe && elem_probe == elem) {
933 hlist_del_rcu(&elem->node);
934 sock_map_unref(elem->sk, elem);
935 sock_hash_free_elem(htab, elem);
937 spin_unlock_bh(&bucket->lock);
940 static long sock_hash_delete_elem(struct bpf_map *map, void *key)
942 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
943 u32 hash, key_size = map->key_size;
944 struct bpf_shtab_bucket *bucket;
945 struct bpf_shtab_elem *elem;
948 hash = sock_hash_bucket_hash(key, key_size);
949 bucket = sock_hash_select_bucket(htab, hash);
951 spin_lock_bh(&bucket->lock);
952 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
954 hlist_del_rcu(&elem->node);
955 sock_map_unref(elem->sk, elem);
956 sock_hash_free_elem(htab, elem);
959 spin_unlock_bh(&bucket->lock);
963 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
964 void *key, u32 key_size,
965 u32 hash, struct sock *sk,
966 struct bpf_shtab_elem *old)
968 struct bpf_shtab_elem *new;
970 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
972 atomic_dec(&htab->count);
973 return ERR_PTR(-E2BIG);
977 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
978 GFP_ATOMIC | __GFP_NOWARN,
979 htab->map.numa_node);
981 atomic_dec(&htab->count);
982 return ERR_PTR(-ENOMEM);
984 memcpy(new->key, key, key_size);
990 static int sock_hash_update_common(struct bpf_map *map, void *key,
991 struct sock *sk, u64 flags)
993 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
994 u32 key_size = map->key_size, hash;
995 struct bpf_shtab_elem *elem, *elem_new;
996 struct bpf_shtab_bucket *bucket;
997 struct sk_psock_link *link;
998 struct sk_psock *psock;
1001 WARN_ON_ONCE(!rcu_read_lock_held());
1002 if (unlikely(flags > BPF_EXIST))
1005 link = sk_psock_init_link();
1009 ret = sock_map_link(map, sk);
1013 psock = sk_psock(sk);
1014 WARN_ON_ONCE(!psock);
1016 hash = sock_hash_bucket_hash(key, key_size);
1017 bucket = sock_hash_select_bucket(htab, hash);
1019 spin_lock_bh(&bucket->lock);
1020 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
1021 if (elem && flags == BPF_NOEXIST) {
1024 } else if (!elem && flags == BPF_EXIST) {
1029 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
1030 if (IS_ERR(elem_new)) {
1031 ret = PTR_ERR(elem_new);
1035 sock_map_add_link(psock, link, map, elem_new);
1036 /* Add new element to the head of the list, so that
1037 * concurrent search will find it before old elem.
1039 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1041 hlist_del_rcu(&elem->node);
1042 sock_map_unref(elem->sk, elem);
1043 sock_hash_free_elem(htab, elem);
1045 spin_unlock_bh(&bucket->lock);
1048 spin_unlock_bh(&bucket->lock);
1049 sk_psock_put(sk, psock);
1051 sk_psock_free_link(link);
1055 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1058 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1059 struct bpf_shtab_elem *elem, *elem_next;
1060 u32 hash, key_size = map->key_size;
1061 struct hlist_head *head;
1065 goto find_first_elem;
1066 hash = sock_hash_bucket_hash(key, key_size);
1067 head = &sock_hash_select_bucket(htab, hash)->head;
1068 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1070 goto find_first_elem;
1072 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1073 struct bpf_shtab_elem, node);
1075 memcpy(key_next, elem_next->key, key_size);
1079 i = hash & (htab->buckets_num - 1);
1082 for (; i < htab->buckets_num; i++) {
1083 head = &sock_hash_select_bucket(htab, i)->head;
1084 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1085 struct bpf_shtab_elem, node);
1087 memcpy(key_next, elem_next->key, key_size);
1095 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1097 struct bpf_shtab *htab;
1100 if (attr->max_entries == 0 ||
1101 attr->key_size == 0 ||
1102 (attr->value_size != sizeof(u32) &&
1103 attr->value_size != sizeof(u64)) ||
1104 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1105 return ERR_PTR(-EINVAL);
1106 if (attr->key_size > MAX_BPF_STACK)
1107 return ERR_PTR(-E2BIG);
1109 htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
1111 return ERR_PTR(-ENOMEM);
1113 bpf_map_init_from_attr(&htab->map, attr);
1115 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1116 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1117 round_up(htab->map.key_size, 8);
1118 if (htab->buckets_num == 0 ||
1119 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1124 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1125 sizeof(struct bpf_shtab_bucket),
1126 htab->map.numa_node);
1127 if (!htab->buckets) {
1132 for (i = 0; i < htab->buckets_num; i++) {
1133 INIT_HLIST_HEAD(&htab->buckets[i].head);
1134 spin_lock_init(&htab->buckets[i].lock);
1139 bpf_map_area_free(htab);
1140 return ERR_PTR(err);
1143 static void sock_hash_free(struct bpf_map *map)
1145 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1146 struct bpf_shtab_bucket *bucket;
1147 struct hlist_head unlink_list;
1148 struct bpf_shtab_elem *elem;
1149 struct hlist_node *node;
1152 /* After the sync no updates or deletes will be in-flight so it
1153 * is safe to walk map and remove entries without risking a race
1154 * in EEXIST update case.
1157 for (i = 0; i < htab->buckets_num; i++) {
1158 bucket = sock_hash_select_bucket(htab, i);
1160 /* We are racing with sock_hash_delete_from_link to
1161 * enter the spin-lock critical section. Every socket on
1162 * the list is still linked to sockhash. Since link
1163 * exists, psock exists and holds a ref to socket. That
1164 * lets us to grab a socket ref too.
1166 spin_lock_bh(&bucket->lock);
1167 hlist_for_each_entry(elem, &bucket->head, node)
1168 sock_hold(elem->sk);
1169 hlist_move_list(&bucket->head, &unlink_list);
1170 spin_unlock_bh(&bucket->lock);
1172 /* Process removed entries out of atomic context to
1173 * block for socket lock before deleting the psock's
1176 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1177 hlist_del(&elem->node);
1178 lock_sock(elem->sk);
1180 sock_map_unref(elem->sk, elem);
1182 release_sock(elem->sk);
1184 sock_hash_free_elem(htab, elem);
1188 /* wait for psock readers accessing its map link */
1191 bpf_map_area_free(htab->buckets);
1192 bpf_map_area_free(htab);
1195 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1199 if (map->value_size != sizeof(u64))
1200 return ERR_PTR(-ENOSPC);
1202 sk = __sock_hash_lookup_elem(map, key);
1204 return ERR_PTR(-ENOENT);
1206 __sock_gen_cookie(sk);
1207 return &sk->sk_cookie;
1210 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1214 sk = __sock_hash_lookup_elem(map, key);
1217 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1222 static void sock_hash_release_progs(struct bpf_map *map)
1224 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1227 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1228 struct bpf_map *, map, void *, key, u64, flags)
1230 WARN_ON_ONCE(!rcu_read_lock_held());
1232 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1233 sock_map_op_okay(sops)))
1234 return sock_hash_update_common(map, key, sops->sk, flags);
1238 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1239 .func = bpf_sock_hash_update,
1242 .ret_type = RET_INTEGER,
1243 .arg1_type = ARG_PTR_TO_CTX,
1244 .arg2_type = ARG_CONST_MAP_PTR,
1245 .arg3_type = ARG_PTR_TO_MAP_KEY,
1246 .arg4_type = ARG_ANYTHING,
1249 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1250 struct bpf_map *, map, void *, key, u64, flags)
1254 if (unlikely(flags & ~(BPF_F_INGRESS)))
1257 sk = __sock_hash_lookup_elem(map, key);
1258 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1261 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
1265 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1266 .func = bpf_sk_redirect_hash,
1268 .ret_type = RET_INTEGER,
1269 .arg1_type = ARG_PTR_TO_CTX,
1270 .arg2_type = ARG_CONST_MAP_PTR,
1271 .arg3_type = ARG_PTR_TO_MAP_KEY,
1272 .arg4_type = ARG_ANYTHING,
1275 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1276 struct bpf_map *, map, void *, key, u64, flags)
1280 if (unlikely(flags & ~(BPF_F_INGRESS)))
1283 sk = __sock_hash_lookup_elem(map, key);
1284 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1286 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
1294 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1295 .func = bpf_msg_redirect_hash,
1297 .ret_type = RET_INTEGER,
1298 .arg1_type = ARG_PTR_TO_CTX,
1299 .arg2_type = ARG_CONST_MAP_PTR,
1300 .arg3_type = ARG_PTR_TO_MAP_KEY,
1301 .arg4_type = ARG_ANYTHING,
1304 struct sock_hash_seq_info {
1305 struct bpf_map *map;
1306 struct bpf_shtab *htab;
1310 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1311 struct bpf_shtab_elem *prev_elem)
1313 const struct bpf_shtab *htab = info->htab;
1314 struct bpf_shtab_bucket *bucket;
1315 struct bpf_shtab_elem *elem;
1316 struct hlist_node *node;
1318 /* try to find next elem in the same bucket */
1320 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1321 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1325 /* no more elements, continue in the next bucket */
1329 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1330 bucket = &htab->buckets[info->bucket_id];
1331 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1332 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1340 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1343 struct sock_hash_seq_info *info = seq->private;
1348 /* pairs with sock_hash_seq_stop */
1350 return sock_hash_seq_find_next(info, NULL);
1353 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1356 struct sock_hash_seq_info *info = seq->private;
1359 return sock_hash_seq_find_next(info, v);
1362 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1365 struct sock_hash_seq_info *info = seq->private;
1366 struct bpf_iter__sockmap ctx = {};
1367 struct bpf_shtab_elem *elem = v;
1368 struct bpf_iter_meta meta;
1369 struct bpf_prog *prog;
1372 prog = bpf_iter_get_info(&meta, !elem);
1377 ctx.map = info->map;
1379 ctx.key = elem->key;
1383 return bpf_iter_run_prog(prog, &ctx);
1386 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1390 (void)sock_hash_seq_show(seq, NULL);
1392 /* pairs with sock_hash_seq_start */
1396 static const struct seq_operations sock_hash_seq_ops = {
1397 .start = sock_hash_seq_start,
1398 .next = sock_hash_seq_next,
1399 .stop = sock_hash_seq_stop,
1400 .show = sock_hash_seq_show,
1403 static int sock_hash_init_seq_private(void *priv_data,
1404 struct bpf_iter_aux_info *aux)
1406 struct sock_hash_seq_info *info = priv_data;
1408 bpf_map_inc_with_uref(aux->map);
1409 info->map = aux->map;
1410 info->htab = container_of(aux->map, struct bpf_shtab, map);
1414 static void sock_hash_fini_seq_private(void *priv_data)
1416 struct sock_hash_seq_info *info = priv_data;
1418 bpf_map_put_with_uref(info->map);
1421 static u64 sock_hash_mem_usage(const struct bpf_map *map)
1423 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1424 u64 usage = sizeof(*htab);
1426 usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket);
1427 usage += atomic_read(&htab->count) * (u64)htab->elem_size;
1431 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1432 .seq_ops = &sock_hash_seq_ops,
1433 .init_seq_private = sock_hash_init_seq_private,
1434 .fini_seq_private = sock_hash_fini_seq_private,
1435 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1438 BTF_ID_LIST_SINGLE(sock_hash_map_btf_ids, struct, bpf_shtab)
1439 const struct bpf_map_ops sock_hash_ops = {
1440 .map_meta_equal = bpf_map_meta_equal,
1441 .map_alloc = sock_hash_alloc,
1442 .map_free = sock_hash_free,
1443 .map_get_next_key = sock_hash_get_next_key,
1444 .map_update_elem = sock_map_update_elem,
1445 .map_delete_elem = sock_hash_delete_elem,
1446 .map_lookup_elem = sock_hash_lookup,
1447 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1448 .map_release_uref = sock_hash_release_progs,
1449 .map_check_btf = map_check_no_btf,
1450 .map_mem_usage = sock_hash_mem_usage,
1451 .map_btf_id = &sock_hash_map_btf_ids[0],
1452 .iter_seq_info = &sock_hash_iter_seq_info,
1455 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1457 switch (map->map_type) {
1458 case BPF_MAP_TYPE_SOCKMAP:
1459 return &container_of(map, struct bpf_stab, map)->progs;
1460 case BPF_MAP_TYPE_SOCKHASH:
1461 return &container_of(map, struct bpf_shtab, map)->progs;
1469 static int sock_map_prog_link_lookup(struct bpf_map *map, struct bpf_prog ***pprog,
1470 struct bpf_link ***plink, u32 which)
1472 struct sk_psock_progs *progs = sock_map_progs(map);
1473 struct bpf_prog **cur_pprog;
1474 struct bpf_link **cur_plink;
1480 case BPF_SK_MSG_VERDICT:
1481 cur_pprog = &progs->msg_parser;
1482 cur_plink = &progs->msg_parser_link;
1484 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1485 case BPF_SK_SKB_STREAM_PARSER:
1486 cur_pprog = &progs->stream_parser;
1487 cur_plink = &progs->stream_parser_link;
1490 case BPF_SK_SKB_STREAM_VERDICT:
1491 if (progs->skb_verdict)
1493 cur_pprog = &progs->stream_verdict;
1494 cur_plink = &progs->stream_verdict_link;
1496 case BPF_SK_SKB_VERDICT:
1497 if (progs->stream_verdict)
1499 cur_pprog = &progs->skb_verdict;
1500 cur_plink = &progs->skb_verdict_link;
1512 /* Handle the following four cases:
1513 * prog_attach: prog != NULL, old == NULL, link == NULL
1514 * prog_detach: prog == NULL, old != NULL, link == NULL
1515 * link_attach: prog != NULL, old == NULL, link != NULL
1516 * link_detach: prog == NULL, old != NULL, link != NULL
1518 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1519 struct bpf_prog *old, struct bpf_link *link,
1522 struct bpf_prog **pprog;
1523 struct bpf_link **plink;
1526 ret = sock_map_prog_link_lookup(map, &pprog, &plink, which);
1530 /* for prog_attach/prog_detach/link_attach, return error if a bpf_link
1531 * exists for that prog.
1533 if ((!link || prog) && *plink)
1537 ret = psock_replace_prog(pprog, prog, old);
1541 psock_set_prog(pprog, prog);
1549 int sock_map_bpf_prog_query(const union bpf_attr *attr,
1550 union bpf_attr __user *uattr)
1552 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
1553 u32 prog_cnt = 0, flags = 0, ufd = attr->target_fd;
1554 struct bpf_prog **pprog;
1555 struct bpf_prog *prog;
1556 struct bpf_map *map;
1561 if (attr->query.query_flags)
1565 map = __bpf_map_get(f);
1567 return PTR_ERR(map);
1571 ret = sock_map_prog_link_lookup(map, &pprog, NULL, attr->query.attach_type);
1576 prog_cnt = !prog ? 0 : 1;
1578 if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
1581 /* we do not hold the refcnt, the bpf prog may be released
1582 * asynchronously and the id would be set to 0.
1584 id = data_race(prog->aux->id);
1591 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)) ||
1592 (id != 0 && copy_to_user(prog_ids, &id, sizeof(u32))) ||
1593 copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
1600 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1602 switch (link->map->map_type) {
1603 case BPF_MAP_TYPE_SOCKMAP:
1604 return sock_map_delete_from_link(link->map, sk,
1606 case BPF_MAP_TYPE_SOCKHASH:
1607 return sock_hash_delete_from_link(link->map, sk,
1614 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1616 struct sk_psock_link *link;
1618 while ((link = sk_psock_link_pop(psock))) {
1619 sock_map_unlink(sk, link);
1620 sk_psock_free_link(link);
1624 void sock_map_unhash(struct sock *sk)
1626 void (*saved_unhash)(struct sock *sk);
1627 struct sk_psock *psock;
1630 psock = sk_psock(sk);
1631 if (unlikely(!psock)) {
1633 saved_unhash = READ_ONCE(sk->sk_prot)->unhash;
1635 saved_unhash = psock->saved_unhash;
1636 sock_map_remove_links(sk, psock);
1639 if (WARN_ON_ONCE(saved_unhash == sock_map_unhash))
1644 EXPORT_SYMBOL_GPL(sock_map_unhash);
1646 void sock_map_destroy(struct sock *sk)
1648 void (*saved_destroy)(struct sock *sk);
1649 struct sk_psock *psock;
1652 psock = sk_psock_get(sk);
1653 if (unlikely(!psock)) {
1655 saved_destroy = READ_ONCE(sk->sk_prot)->destroy;
1657 saved_destroy = psock->saved_destroy;
1658 sock_map_remove_links(sk, psock);
1660 sk_psock_stop(psock);
1661 sk_psock_put(sk, psock);
1663 if (WARN_ON_ONCE(saved_destroy == sock_map_destroy))
1668 EXPORT_SYMBOL_GPL(sock_map_destroy);
1670 void sock_map_close(struct sock *sk, long timeout)
1672 void (*saved_close)(struct sock *sk, long timeout);
1673 struct sk_psock *psock;
1677 psock = sk_psock(sk);
1678 if (likely(psock)) {
1679 saved_close = psock->saved_close;
1680 sock_map_remove_links(sk, psock);
1681 psock = sk_psock_get(sk);
1682 if (unlikely(!psock))
1685 sk_psock_stop(psock);
1687 cancel_delayed_work_sync(&psock->work);
1688 sk_psock_put(sk, psock);
1690 saved_close = READ_ONCE(sk->sk_prot)->close;
1696 /* Make sure we do not recurse. This is a bug.
1697 * Leak the socket instead of crashing on a stack overflow.
1699 if (WARN_ON_ONCE(saved_close == sock_map_close))
1701 saved_close(sk, timeout);
1703 EXPORT_SYMBOL_GPL(sock_map_close);
1705 struct sockmap_link {
1706 struct bpf_link link;
1707 struct bpf_map *map;
1708 enum bpf_attach_type attach_type;
1711 static void sock_map_link_release(struct bpf_link *link)
1713 struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
1715 mutex_lock(&sockmap_mutex);
1716 if (!sockmap_link->map)
1719 WARN_ON_ONCE(sock_map_prog_update(sockmap_link->map, NULL, link->prog, link,
1720 sockmap_link->attach_type));
1722 bpf_map_put_with_uref(sockmap_link->map);
1723 sockmap_link->map = NULL;
1725 mutex_unlock(&sockmap_mutex);
1728 static int sock_map_link_detach(struct bpf_link *link)
1730 sock_map_link_release(link);
1734 static void sock_map_link_dealloc(struct bpf_link *link)
1739 /* Handle the following two cases:
1740 * case 1: link != NULL, prog != NULL, old != NULL
1741 * case 2: link != NULL, prog != NULL, old == NULL
1743 static int sock_map_link_update_prog(struct bpf_link *link,
1744 struct bpf_prog *prog,
1745 struct bpf_prog *old)
1747 const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
1748 struct bpf_prog **pprog, *old_link_prog;
1749 struct bpf_link **plink;
1752 mutex_lock(&sockmap_mutex);
1754 /* If old prog is not NULL, ensure old prog is the same as link->prog. */
1755 if (old && link->prog != old) {
1759 /* Ensure link->prog has the same type/attach_type as the new prog. */
1760 if (link->prog->type != prog->type ||
1761 link->prog->expected_attach_type != prog->expected_attach_type) {
1766 ret = sock_map_prog_link_lookup(sockmap_link->map, &pprog, &plink,
1767 sockmap_link->attach_type);
1771 /* return error if the stored bpf_link does not match the incoming bpf_link. */
1772 if (link != *plink) {
1778 ret = psock_replace_prog(pprog, prog, old);
1782 psock_set_prog(pprog, prog);
1786 old_link_prog = xchg(&link->prog, prog);
1787 bpf_prog_put(old_link_prog);
1790 mutex_unlock(&sockmap_mutex);
1794 static u32 sock_map_link_get_map_id(const struct sockmap_link *sockmap_link)
1798 mutex_lock(&sockmap_mutex);
1799 if (sockmap_link->map)
1800 map_id = sockmap_link->map->id;
1801 mutex_unlock(&sockmap_mutex);
1805 static int sock_map_link_fill_info(const struct bpf_link *link,
1806 struct bpf_link_info *info)
1808 const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
1809 u32 map_id = sock_map_link_get_map_id(sockmap_link);
1811 info->sockmap.map_id = map_id;
1812 info->sockmap.attach_type = sockmap_link->attach_type;
1816 static void sock_map_link_show_fdinfo(const struct bpf_link *link,
1817 struct seq_file *seq)
1819 const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
1820 u32 map_id = sock_map_link_get_map_id(sockmap_link);
1822 seq_printf(seq, "map_id:\t%u\n", map_id);
1823 seq_printf(seq, "attach_type:\t%u\n", sockmap_link->attach_type);
1826 static const struct bpf_link_ops sock_map_link_ops = {
1827 .release = sock_map_link_release,
1828 .dealloc = sock_map_link_dealloc,
1829 .detach = sock_map_link_detach,
1830 .update_prog = sock_map_link_update_prog,
1831 .fill_link_info = sock_map_link_fill_info,
1832 .show_fdinfo = sock_map_link_show_fdinfo,
1835 int sock_map_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
1837 struct bpf_link_primer link_primer;
1838 struct sockmap_link *sockmap_link;
1839 enum bpf_attach_type attach_type;
1840 struct bpf_map *map;
1843 if (attr->link_create.flags)
1846 map = bpf_map_get_with_uref(attr->link_create.target_fd);
1848 return PTR_ERR(map);
1849 if (map->map_type != BPF_MAP_TYPE_SOCKMAP && map->map_type != BPF_MAP_TYPE_SOCKHASH) {
1854 sockmap_link = kzalloc(sizeof(*sockmap_link), GFP_USER);
1855 if (!sockmap_link) {
1860 attach_type = attr->link_create.attach_type;
1861 bpf_link_init(&sockmap_link->link, BPF_LINK_TYPE_SOCKMAP, &sock_map_link_ops, prog);
1862 sockmap_link->map = map;
1863 sockmap_link->attach_type = attach_type;
1865 ret = bpf_link_prime(&sockmap_link->link, &link_primer);
1867 kfree(sockmap_link);
1871 mutex_lock(&sockmap_mutex);
1872 ret = sock_map_prog_update(map, prog, NULL, &sockmap_link->link, attach_type);
1873 mutex_unlock(&sockmap_mutex);
1875 bpf_link_cleanup(&link_primer);
1879 /* Increase refcnt for the prog since when old prog is replaced with
1880 * psock_replace_prog() and psock_set_prog() its refcnt will be decreased.
1882 * Actually, we do not need to increase refcnt for the prog since bpf_link
1883 * will hold a reference. But in order to have less complexity w.r.t.
1884 * replacing/setting prog, let us increase the refcnt to make things simpler.
1888 return bpf_link_settle(&link_primer);
1891 bpf_map_put_with_uref(map);
1895 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1896 union bpf_iter_link_info *linfo,
1897 struct bpf_iter_aux_info *aux)
1899 struct bpf_map *map;
1902 if (!linfo->map.map_fd)
1905 map = bpf_map_get_with_uref(linfo->map.map_fd);
1907 return PTR_ERR(map);
1909 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1910 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1913 if (prog->aux->max_rdonly_access > map->key_size) {
1922 bpf_map_put_with_uref(map);
1926 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1928 bpf_map_put_with_uref(aux->map);
1931 static struct bpf_iter_reg sock_map_iter_reg = {
1932 .target = "sockmap",
1933 .attach_target = sock_map_iter_attach_target,
1934 .detach_target = sock_map_iter_detach_target,
1935 .show_fdinfo = bpf_iter_map_show_fdinfo,
1936 .fill_link_info = bpf_iter_map_fill_link_info,
1937 .ctx_arg_info_size = 2,
1939 { offsetof(struct bpf_iter__sockmap, key),
1940 PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
1941 { offsetof(struct bpf_iter__sockmap, sk),
1942 PTR_TO_BTF_ID_OR_NULL },
1946 static int __init bpf_sockmap_iter_init(void)
1948 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1949 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1950 return bpf_iter_reg_target(&sock_map_iter_reg);
1952 late_initcall(bpf_sockmap_iter_init);