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 bool sock_map_redirect_allowed(const struct sock *sk);
216 static int sock_map_link(struct bpf_map *map, struct sock *sk)
218 struct sk_psock_progs *progs = sock_map_progs(map);
219 struct bpf_prog *stream_verdict = NULL;
220 struct bpf_prog *stream_parser = NULL;
221 struct bpf_prog *skb_verdict = NULL;
222 struct bpf_prog *msg_parser = NULL;
223 struct sk_psock *psock;
226 /* Only sockets we can redirect into/from in BPF need to hold
227 * refs to parser/verdict progs and have their sk_data_ready
228 * and sk_write_space callbacks overridden.
230 if (!sock_map_redirect_allowed(sk))
233 stream_verdict = READ_ONCE(progs->stream_verdict);
234 if (stream_verdict) {
235 stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
236 if (IS_ERR(stream_verdict))
237 return PTR_ERR(stream_verdict);
240 stream_parser = READ_ONCE(progs->stream_parser);
242 stream_parser = bpf_prog_inc_not_zero(stream_parser);
243 if (IS_ERR(stream_parser)) {
244 ret = PTR_ERR(stream_parser);
245 goto out_put_stream_verdict;
249 msg_parser = READ_ONCE(progs->msg_parser);
251 msg_parser = bpf_prog_inc_not_zero(msg_parser);
252 if (IS_ERR(msg_parser)) {
253 ret = PTR_ERR(msg_parser);
254 goto out_put_stream_parser;
258 skb_verdict = READ_ONCE(progs->skb_verdict);
260 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
261 if (IS_ERR(skb_verdict)) {
262 ret = PTR_ERR(skb_verdict);
263 goto out_put_msg_parser;
268 psock = sock_map_psock_get_checked(sk);
270 ret = PTR_ERR(psock);
275 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
276 (stream_parser && READ_ONCE(psock->progs.stream_parser)) ||
277 (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
278 (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) ||
279 (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
280 (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) {
281 sk_psock_put(sk, psock);
286 psock = sk_psock_init(sk, map->numa_node);
288 ret = PTR_ERR(psock);
294 psock_set_prog(&psock->progs.msg_parser, msg_parser);
296 ret = sock_map_init_proto(sk, psock);
300 write_lock_bh(&sk->sk_callback_lock);
301 if (stream_parser && stream_verdict && !psock->saved_data_ready) {
302 ret = sk_psock_init_strp(sk, psock);
304 goto out_unlock_drop;
305 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
306 psock_set_prog(&psock->progs.stream_parser, stream_parser);
307 sk_psock_start_strp(sk, psock);
308 } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
309 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
310 sk_psock_start_verdict(sk,psock);
311 } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
312 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
313 sk_psock_start_verdict(sk, psock);
315 write_unlock_bh(&sk->sk_callback_lock);
318 write_unlock_bh(&sk->sk_callback_lock);
320 sk_psock_put(sk, psock);
323 bpf_prog_put(skb_verdict);
326 bpf_prog_put(msg_parser);
327 out_put_stream_parser:
329 bpf_prog_put(stream_parser);
330 out_put_stream_verdict:
332 bpf_prog_put(stream_verdict);
336 static void sock_map_free(struct bpf_map *map)
338 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
341 /* After the sync no updates or deletes will be in-flight so it
342 * is safe to walk map and remove entries without risking a race
343 * in EEXIST update case.
346 for (i = 0; i < stab->map.max_entries; i++) {
347 struct sock **psk = &stab->sks[i];
350 sk = xchg(psk, NULL);
354 sock_map_unref(sk, psk);
360 /* wait for psock readers accessing its map link */
363 bpf_map_area_free(stab->sks);
367 static void sock_map_release_progs(struct bpf_map *map)
369 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
372 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
374 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
376 WARN_ON_ONCE(!rcu_read_lock_held());
378 if (unlikely(key >= map->max_entries))
380 return READ_ONCE(stab->sks[key]);
383 static void *sock_map_lookup(struct bpf_map *map, void *key)
387 sk = __sock_map_lookup_elem(map, *(u32 *)key);
390 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
395 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
399 if (map->value_size != sizeof(u64))
400 return ERR_PTR(-ENOSPC);
402 sk = __sock_map_lookup_elem(map, *(u32 *)key);
404 return ERR_PTR(-ENOENT);
406 __sock_gen_cookie(sk);
407 return &sk->sk_cookie;
410 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
416 raw_spin_lock_bh(&stab->lock);
418 if (!sk_test || sk_test == sk)
419 sk = xchg(psk, NULL);
422 sock_map_unref(sk, psk);
426 raw_spin_unlock_bh(&stab->lock);
430 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
433 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
435 __sock_map_delete(stab, sk, link_raw);
438 static int sock_map_delete_elem(struct bpf_map *map, void *key)
440 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
444 if (unlikely(i >= map->max_entries))
448 return __sock_map_delete(stab, NULL, psk);
451 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
453 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
454 u32 i = key ? *(u32 *)key : U32_MAX;
455 u32 *key_next = next;
457 if (i == stab->map.max_entries - 1)
459 if (i >= stab->map.max_entries)
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 ret = sock_map_link(map, sk);
489 psock = sk_psock(sk);
490 WARN_ON_ONCE(!psock);
492 raw_spin_lock_bh(&stab->lock);
493 osk = stab->sks[idx];
494 if (osk && flags == BPF_NOEXIST) {
497 } else if (!osk && flags == BPF_EXIST) {
502 sock_map_add_link(psock, link, map, &stab->sks[idx]);
505 sock_map_unref(osk, &stab->sks[idx]);
506 raw_spin_unlock_bh(&stab->lock);
509 raw_spin_unlock_bh(&stab->lock);
511 sk_psock_put(sk, psock);
513 sk_psock_free_link(link);
517 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
519 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
520 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
521 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
524 static bool sk_is_tcp(const struct sock *sk)
526 return sk->sk_type == SOCK_STREAM &&
527 sk->sk_protocol == IPPROTO_TCP;
530 static bool sk_is_udp(const struct sock *sk)
532 return sk->sk_type == SOCK_DGRAM &&
533 sk->sk_protocol == IPPROTO_UDP;
536 static bool sock_map_redirect_allowed(const struct sock *sk)
539 return sk->sk_state != TCP_LISTEN;
541 return sk->sk_state == TCP_ESTABLISHED;
544 static bool sock_map_sk_is_suitable(const struct sock *sk)
546 return !!sk->sk_prot->psock_update_sk_prot;
549 static bool sock_map_sk_state_allowed(const struct sock *sk)
552 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
553 else if (sk_is_udp(sk))
554 return sk_hashed(sk);
559 static int sock_hash_update_common(struct bpf_map *map, void *key,
560 struct sock *sk, u64 flags);
562 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
570 if (map->value_size == sizeof(u64))
577 sock = sockfd_lookup(ufd, &ret);
585 if (!sock_map_sk_is_suitable(sk)) {
590 sock_map_sk_acquire(sk);
591 if (!sock_map_sk_state_allowed(sk))
593 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
594 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
596 ret = sock_hash_update_common(map, key, sk, flags);
597 sock_map_sk_release(sk);
603 static int sock_map_update_elem(struct bpf_map *map, void *key,
604 void *value, u64 flags)
606 struct sock *sk = (struct sock *)value;
609 if (unlikely(!sk || !sk_fullsock(sk)))
612 if (!sock_map_sk_is_suitable(sk))
617 if (!sock_map_sk_state_allowed(sk))
619 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
620 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
622 ret = sock_hash_update_common(map, key, sk, flags);
628 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
629 struct bpf_map *, map, void *, key, u64, flags)
631 WARN_ON_ONCE(!rcu_read_lock_held());
633 if (likely(sock_map_sk_is_suitable(sops->sk) &&
634 sock_map_op_okay(sops)))
635 return sock_map_update_common(map, *(u32 *)key, sops->sk,
640 const struct bpf_func_proto bpf_sock_map_update_proto = {
641 .func = bpf_sock_map_update,
644 .ret_type = RET_INTEGER,
645 .arg1_type = ARG_PTR_TO_CTX,
646 .arg2_type = ARG_CONST_MAP_PTR,
647 .arg3_type = ARG_PTR_TO_MAP_KEY,
648 .arg4_type = ARG_ANYTHING,
651 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
652 struct bpf_map *, map, u32, key, u64, flags)
656 if (unlikely(flags & ~(BPF_F_INGRESS)))
659 sk = __sock_map_lookup_elem(map, key);
660 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
663 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
667 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
668 .func = bpf_sk_redirect_map,
670 .ret_type = RET_INTEGER,
671 .arg1_type = ARG_PTR_TO_CTX,
672 .arg2_type = ARG_CONST_MAP_PTR,
673 .arg3_type = ARG_ANYTHING,
674 .arg4_type = ARG_ANYTHING,
677 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
678 struct bpf_map *, map, u32, key, u64, flags)
682 if (unlikely(flags & ~(BPF_F_INGRESS)))
685 sk = __sock_map_lookup_elem(map, key);
686 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
694 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
695 .func = bpf_msg_redirect_map,
697 .ret_type = RET_INTEGER,
698 .arg1_type = ARG_PTR_TO_CTX,
699 .arg2_type = ARG_CONST_MAP_PTR,
700 .arg3_type = ARG_ANYTHING,
701 .arg4_type = ARG_ANYTHING,
704 struct sock_map_seq_info {
710 struct bpf_iter__sockmap {
711 __bpf_md_ptr(struct bpf_iter_meta *, meta);
712 __bpf_md_ptr(struct bpf_map *, map);
713 __bpf_md_ptr(void *, key);
714 __bpf_md_ptr(struct sock *, sk);
717 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
718 struct bpf_map *map, void *key,
721 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
723 if (unlikely(info->index >= info->map->max_entries))
726 info->sk = __sock_map_lookup_elem(info->map, info->index);
728 /* can't return sk directly, since that might be NULL */
732 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
735 struct sock_map_seq_info *info = seq->private;
740 /* pairs with sock_map_seq_stop */
742 return sock_map_seq_lookup_elem(info);
745 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
748 struct sock_map_seq_info *info = seq->private;
753 return sock_map_seq_lookup_elem(info);
756 static int sock_map_seq_show(struct seq_file *seq, void *v)
759 struct sock_map_seq_info *info = seq->private;
760 struct bpf_iter__sockmap ctx = {};
761 struct bpf_iter_meta meta;
762 struct bpf_prog *prog;
765 prog = bpf_iter_get_info(&meta, !v);
772 ctx.key = &info->index;
776 return bpf_iter_run_prog(prog, &ctx);
779 static void sock_map_seq_stop(struct seq_file *seq, void *v)
783 (void)sock_map_seq_show(seq, NULL);
785 /* pairs with sock_map_seq_start */
789 static const struct seq_operations sock_map_seq_ops = {
790 .start = sock_map_seq_start,
791 .next = sock_map_seq_next,
792 .stop = sock_map_seq_stop,
793 .show = sock_map_seq_show,
796 static int sock_map_init_seq_private(void *priv_data,
797 struct bpf_iter_aux_info *aux)
799 struct sock_map_seq_info *info = priv_data;
801 info->map = aux->map;
805 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
806 .seq_ops = &sock_map_seq_ops,
807 .init_seq_private = sock_map_init_seq_private,
808 .seq_priv_size = sizeof(struct sock_map_seq_info),
811 static int sock_map_btf_id;
812 const struct bpf_map_ops sock_map_ops = {
813 .map_meta_equal = bpf_map_meta_equal,
814 .map_alloc = sock_map_alloc,
815 .map_free = sock_map_free,
816 .map_get_next_key = sock_map_get_next_key,
817 .map_lookup_elem_sys_only = sock_map_lookup_sys,
818 .map_update_elem = sock_map_update_elem,
819 .map_delete_elem = sock_map_delete_elem,
820 .map_lookup_elem = sock_map_lookup,
821 .map_release_uref = sock_map_release_progs,
822 .map_check_btf = map_check_no_btf,
823 .map_btf_name = "bpf_stab",
824 .map_btf_id = &sock_map_btf_id,
825 .iter_seq_info = &sock_map_iter_seq_info,
828 struct bpf_shtab_elem {
832 struct hlist_node node;
836 struct bpf_shtab_bucket {
837 struct hlist_head head;
843 struct bpf_shtab_bucket *buckets;
846 struct sk_psock_progs progs;
850 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
852 return jhash(key, len, 0);
855 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
858 return &htab->buckets[hash & (htab->buckets_num - 1)];
861 static struct bpf_shtab_elem *
862 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
865 struct bpf_shtab_elem *elem;
867 hlist_for_each_entry_rcu(elem, head, node) {
868 if (elem->hash == hash &&
869 !memcmp(&elem->key, key, key_size))
876 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
878 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
879 u32 key_size = map->key_size, hash;
880 struct bpf_shtab_bucket *bucket;
881 struct bpf_shtab_elem *elem;
883 WARN_ON_ONCE(!rcu_read_lock_held());
885 hash = sock_hash_bucket_hash(key, key_size);
886 bucket = sock_hash_select_bucket(htab, hash);
887 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
889 return elem ? elem->sk : NULL;
892 static void sock_hash_free_elem(struct bpf_shtab *htab,
893 struct bpf_shtab_elem *elem)
895 atomic_dec(&htab->count);
896 kfree_rcu(elem, rcu);
899 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
902 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
903 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
904 struct bpf_shtab_bucket *bucket;
906 WARN_ON_ONCE(!rcu_read_lock_held());
907 bucket = sock_hash_select_bucket(htab, elem->hash);
909 /* elem may be deleted in parallel from the map, but access here
910 * is okay since it's going away only after RCU grace period.
911 * However, we need to check whether it's still present.
913 raw_spin_lock_bh(&bucket->lock);
914 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
915 elem->key, map->key_size);
916 if (elem_probe && elem_probe == elem) {
917 hlist_del_rcu(&elem->node);
918 sock_map_unref(elem->sk, elem);
919 sock_hash_free_elem(htab, elem);
921 raw_spin_unlock_bh(&bucket->lock);
924 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
926 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
927 u32 hash, key_size = map->key_size;
928 struct bpf_shtab_bucket *bucket;
929 struct bpf_shtab_elem *elem;
932 hash = sock_hash_bucket_hash(key, key_size);
933 bucket = sock_hash_select_bucket(htab, hash);
935 raw_spin_lock_bh(&bucket->lock);
936 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
938 hlist_del_rcu(&elem->node);
939 sock_map_unref(elem->sk, elem);
940 sock_hash_free_elem(htab, elem);
943 raw_spin_unlock_bh(&bucket->lock);
947 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
948 void *key, u32 key_size,
949 u32 hash, struct sock *sk,
950 struct bpf_shtab_elem *old)
952 struct bpf_shtab_elem *new;
954 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
956 atomic_dec(&htab->count);
957 return ERR_PTR(-E2BIG);
961 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
962 GFP_ATOMIC | __GFP_NOWARN,
963 htab->map.numa_node);
965 atomic_dec(&htab->count);
966 return ERR_PTR(-ENOMEM);
968 memcpy(new->key, key, key_size);
974 static int sock_hash_update_common(struct bpf_map *map, void *key,
975 struct sock *sk, u64 flags)
977 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
978 u32 key_size = map->key_size, hash;
979 struct bpf_shtab_elem *elem, *elem_new;
980 struct bpf_shtab_bucket *bucket;
981 struct sk_psock_link *link;
982 struct sk_psock *psock;
985 WARN_ON_ONCE(!rcu_read_lock_held());
986 if (unlikely(flags > BPF_EXIST))
989 link = sk_psock_init_link();
993 ret = sock_map_link(map, sk);
997 psock = sk_psock(sk);
998 WARN_ON_ONCE(!psock);
1000 hash = sock_hash_bucket_hash(key, key_size);
1001 bucket = sock_hash_select_bucket(htab, hash);
1003 raw_spin_lock_bh(&bucket->lock);
1004 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
1005 if (elem && flags == BPF_NOEXIST) {
1008 } else if (!elem && flags == BPF_EXIST) {
1013 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
1014 if (IS_ERR(elem_new)) {
1015 ret = PTR_ERR(elem_new);
1019 sock_map_add_link(psock, link, map, elem_new);
1020 /* Add new element to the head of the list, so that
1021 * concurrent search will find it before old elem.
1023 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1025 hlist_del_rcu(&elem->node);
1026 sock_map_unref(elem->sk, elem);
1027 sock_hash_free_elem(htab, elem);
1029 raw_spin_unlock_bh(&bucket->lock);
1032 raw_spin_unlock_bh(&bucket->lock);
1033 sk_psock_put(sk, psock);
1035 sk_psock_free_link(link);
1039 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1042 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1043 struct bpf_shtab_elem *elem, *elem_next;
1044 u32 hash, key_size = map->key_size;
1045 struct hlist_head *head;
1049 goto find_first_elem;
1050 hash = sock_hash_bucket_hash(key, key_size);
1051 head = &sock_hash_select_bucket(htab, hash)->head;
1052 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1054 goto find_first_elem;
1056 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1057 struct bpf_shtab_elem, node);
1059 memcpy(key_next, elem_next->key, key_size);
1063 i = hash & (htab->buckets_num - 1);
1066 for (; i < htab->buckets_num; i++) {
1067 head = &sock_hash_select_bucket(htab, i)->head;
1068 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1069 struct bpf_shtab_elem, node);
1071 memcpy(key_next, elem_next->key, key_size);
1079 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1081 struct bpf_shtab *htab;
1084 if (!capable(CAP_NET_ADMIN))
1085 return ERR_PTR(-EPERM);
1086 if (attr->max_entries == 0 ||
1087 attr->key_size == 0 ||
1088 (attr->value_size != sizeof(u32) &&
1089 attr->value_size != sizeof(u64)) ||
1090 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1091 return ERR_PTR(-EINVAL);
1092 if (attr->key_size > MAX_BPF_STACK)
1093 return ERR_PTR(-E2BIG);
1095 htab = kzalloc(sizeof(*htab), GFP_USER | __GFP_ACCOUNT);
1097 return ERR_PTR(-ENOMEM);
1099 bpf_map_init_from_attr(&htab->map, attr);
1101 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1102 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1103 round_up(htab->map.key_size, 8);
1104 if (htab->buckets_num == 0 ||
1105 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1110 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1111 sizeof(struct bpf_shtab_bucket),
1112 htab->map.numa_node);
1113 if (!htab->buckets) {
1118 for (i = 0; i < htab->buckets_num; i++) {
1119 INIT_HLIST_HEAD(&htab->buckets[i].head);
1120 raw_spin_lock_init(&htab->buckets[i].lock);
1126 return ERR_PTR(err);
1129 static void sock_hash_free(struct bpf_map *map)
1131 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1132 struct bpf_shtab_bucket *bucket;
1133 struct hlist_head unlink_list;
1134 struct bpf_shtab_elem *elem;
1135 struct hlist_node *node;
1138 /* After the sync no updates or deletes will be in-flight so it
1139 * is safe to walk map and remove entries without risking a race
1140 * in EEXIST update case.
1143 for (i = 0; i < htab->buckets_num; i++) {
1144 bucket = sock_hash_select_bucket(htab, i);
1146 /* We are racing with sock_hash_delete_from_link to
1147 * enter the spin-lock critical section. Every socket on
1148 * the list is still linked to sockhash. Since link
1149 * exists, psock exists and holds a ref to socket. That
1150 * lets us to grab a socket ref too.
1152 raw_spin_lock_bh(&bucket->lock);
1153 hlist_for_each_entry(elem, &bucket->head, node)
1154 sock_hold(elem->sk);
1155 hlist_move_list(&bucket->head, &unlink_list);
1156 raw_spin_unlock_bh(&bucket->lock);
1158 /* Process removed entries out of atomic context to
1159 * block for socket lock before deleting the psock's
1162 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1163 hlist_del(&elem->node);
1164 lock_sock(elem->sk);
1166 sock_map_unref(elem->sk, elem);
1168 release_sock(elem->sk);
1170 sock_hash_free_elem(htab, elem);
1174 /* wait for psock readers accessing its map link */
1177 bpf_map_area_free(htab->buckets);
1181 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1185 if (map->value_size != sizeof(u64))
1186 return ERR_PTR(-ENOSPC);
1188 sk = __sock_hash_lookup_elem(map, key);
1190 return ERR_PTR(-ENOENT);
1192 __sock_gen_cookie(sk);
1193 return &sk->sk_cookie;
1196 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1200 sk = __sock_hash_lookup_elem(map, key);
1203 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1208 static void sock_hash_release_progs(struct bpf_map *map)
1210 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1213 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1214 struct bpf_map *, map, void *, key, u64, flags)
1216 WARN_ON_ONCE(!rcu_read_lock_held());
1218 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1219 sock_map_op_okay(sops)))
1220 return sock_hash_update_common(map, key, sops->sk, flags);
1224 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1225 .func = bpf_sock_hash_update,
1228 .ret_type = RET_INTEGER,
1229 .arg1_type = ARG_PTR_TO_CTX,
1230 .arg2_type = ARG_CONST_MAP_PTR,
1231 .arg3_type = ARG_PTR_TO_MAP_KEY,
1232 .arg4_type = ARG_ANYTHING,
1235 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1236 struct bpf_map *, map, void *, key, u64, flags)
1240 if (unlikely(flags & ~(BPF_F_INGRESS)))
1243 sk = __sock_hash_lookup_elem(map, key);
1244 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1247 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
1251 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1252 .func = bpf_sk_redirect_hash,
1254 .ret_type = RET_INTEGER,
1255 .arg1_type = ARG_PTR_TO_CTX,
1256 .arg2_type = ARG_CONST_MAP_PTR,
1257 .arg3_type = ARG_PTR_TO_MAP_KEY,
1258 .arg4_type = ARG_ANYTHING,
1261 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1262 struct bpf_map *, map, void *, key, u64, flags)
1266 if (unlikely(flags & ~(BPF_F_INGRESS)))
1269 sk = __sock_hash_lookup_elem(map, key);
1270 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1278 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1279 .func = bpf_msg_redirect_hash,
1281 .ret_type = RET_INTEGER,
1282 .arg1_type = ARG_PTR_TO_CTX,
1283 .arg2_type = ARG_CONST_MAP_PTR,
1284 .arg3_type = ARG_PTR_TO_MAP_KEY,
1285 .arg4_type = ARG_ANYTHING,
1288 struct sock_hash_seq_info {
1289 struct bpf_map *map;
1290 struct bpf_shtab *htab;
1294 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1295 struct bpf_shtab_elem *prev_elem)
1297 const struct bpf_shtab *htab = info->htab;
1298 struct bpf_shtab_bucket *bucket;
1299 struct bpf_shtab_elem *elem;
1300 struct hlist_node *node;
1302 /* try to find next elem in the same bucket */
1304 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1305 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1309 /* no more elements, continue in the next bucket */
1313 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1314 bucket = &htab->buckets[info->bucket_id];
1315 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1316 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1324 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1327 struct sock_hash_seq_info *info = seq->private;
1332 /* pairs with sock_hash_seq_stop */
1334 return sock_hash_seq_find_next(info, NULL);
1337 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1340 struct sock_hash_seq_info *info = seq->private;
1343 return sock_hash_seq_find_next(info, v);
1346 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1349 struct sock_hash_seq_info *info = seq->private;
1350 struct bpf_iter__sockmap ctx = {};
1351 struct bpf_shtab_elem *elem = v;
1352 struct bpf_iter_meta meta;
1353 struct bpf_prog *prog;
1356 prog = bpf_iter_get_info(&meta, !elem);
1361 ctx.map = info->map;
1363 ctx.key = elem->key;
1367 return bpf_iter_run_prog(prog, &ctx);
1370 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1374 (void)sock_hash_seq_show(seq, NULL);
1376 /* pairs with sock_hash_seq_start */
1380 static const struct seq_operations sock_hash_seq_ops = {
1381 .start = sock_hash_seq_start,
1382 .next = sock_hash_seq_next,
1383 .stop = sock_hash_seq_stop,
1384 .show = sock_hash_seq_show,
1387 static int sock_hash_init_seq_private(void *priv_data,
1388 struct bpf_iter_aux_info *aux)
1390 struct sock_hash_seq_info *info = priv_data;
1392 info->map = aux->map;
1393 info->htab = container_of(aux->map, struct bpf_shtab, map);
1397 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1398 .seq_ops = &sock_hash_seq_ops,
1399 .init_seq_private = sock_hash_init_seq_private,
1400 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1403 static int sock_hash_map_btf_id;
1404 const struct bpf_map_ops sock_hash_ops = {
1405 .map_meta_equal = bpf_map_meta_equal,
1406 .map_alloc = sock_hash_alloc,
1407 .map_free = sock_hash_free,
1408 .map_get_next_key = sock_hash_get_next_key,
1409 .map_update_elem = sock_map_update_elem,
1410 .map_delete_elem = sock_hash_delete_elem,
1411 .map_lookup_elem = sock_hash_lookup,
1412 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1413 .map_release_uref = sock_hash_release_progs,
1414 .map_check_btf = map_check_no_btf,
1415 .map_btf_name = "bpf_shtab",
1416 .map_btf_id = &sock_hash_map_btf_id,
1417 .iter_seq_info = &sock_hash_iter_seq_info,
1420 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1422 switch (map->map_type) {
1423 case BPF_MAP_TYPE_SOCKMAP:
1424 return &container_of(map, struct bpf_stab, map)->progs;
1425 case BPF_MAP_TYPE_SOCKHASH:
1426 return &container_of(map, struct bpf_shtab, map)->progs;
1434 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1435 struct bpf_prog *old, u32 which)
1437 struct sk_psock_progs *progs = sock_map_progs(map);
1438 struct bpf_prog **pprog;
1444 case BPF_SK_MSG_VERDICT:
1445 pprog = &progs->msg_parser;
1447 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1448 case BPF_SK_SKB_STREAM_PARSER:
1449 pprog = &progs->stream_parser;
1452 case BPF_SK_SKB_STREAM_VERDICT:
1453 if (progs->skb_verdict)
1455 pprog = &progs->stream_verdict;
1457 case BPF_SK_SKB_VERDICT:
1458 if (progs->stream_verdict)
1460 pprog = &progs->skb_verdict;
1467 return psock_replace_prog(pprog, prog, old);
1469 psock_set_prog(pprog, prog);
1473 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1475 switch (link->map->map_type) {
1476 case BPF_MAP_TYPE_SOCKMAP:
1477 return sock_map_delete_from_link(link->map, sk,
1479 case BPF_MAP_TYPE_SOCKHASH:
1480 return sock_hash_delete_from_link(link->map, sk,
1487 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1489 struct sk_psock_link *link;
1491 while ((link = sk_psock_link_pop(psock))) {
1492 sock_map_unlink(sk, link);
1493 sk_psock_free_link(link);
1497 void sock_map_unhash(struct sock *sk)
1499 void (*saved_unhash)(struct sock *sk);
1500 struct sk_psock *psock;
1503 psock = sk_psock(sk);
1504 if (unlikely(!psock)) {
1506 if (sk->sk_prot->unhash)
1507 sk->sk_prot->unhash(sk);
1511 saved_unhash = psock->saved_unhash;
1512 sock_map_remove_links(sk, psock);
1517 void sock_map_close(struct sock *sk, long timeout)
1519 void (*saved_close)(struct sock *sk, long timeout);
1520 struct sk_psock *psock;
1524 psock = sk_psock_get(sk);
1525 if (unlikely(!psock)) {
1528 return sk->sk_prot->close(sk, timeout);
1531 saved_close = psock->saved_close;
1532 sock_map_remove_links(sk, psock);
1534 sk_psock_stop(psock, true);
1535 sk_psock_put(sk, psock);
1537 saved_close(sk, timeout);
1540 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1541 union bpf_iter_link_info *linfo,
1542 struct bpf_iter_aux_info *aux)
1544 struct bpf_map *map;
1547 if (!linfo->map.map_fd)
1550 map = bpf_map_get_with_uref(linfo->map.map_fd);
1552 return PTR_ERR(map);
1554 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1555 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1558 if (prog->aux->max_rdonly_access > map->key_size) {
1567 bpf_map_put_with_uref(map);
1571 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1573 bpf_map_put_with_uref(aux->map);
1576 static struct bpf_iter_reg sock_map_iter_reg = {
1577 .target = "sockmap",
1578 .attach_target = sock_map_iter_attach_target,
1579 .detach_target = sock_map_iter_detach_target,
1580 .show_fdinfo = bpf_iter_map_show_fdinfo,
1581 .fill_link_info = bpf_iter_map_fill_link_info,
1582 .ctx_arg_info_size = 2,
1584 { offsetof(struct bpf_iter__sockmap, key),
1585 PTR_TO_RDONLY_BUF_OR_NULL },
1586 { offsetof(struct bpf_iter__sockmap, sk),
1587 PTR_TO_BTF_ID_OR_NULL },
1591 static int __init bpf_sockmap_iter_init(void)
1593 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1594 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1595 return bpf_iter_reg_target(&sock_map_iter_reg);
1597 late_initcall(bpf_sockmap_iter_init);