1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
5 #include <linux/filter.h>
6 #include <linux/errno.h>
7 #include <linux/file.h>
9 #include <linux/workqueue.h>
10 #include <linux/skmsg.h>
11 #include <linux/list.h>
12 #include <linux/jhash.h>
17 struct sk_psock_progs progs;
21 #define SOCK_CREATE_FLAG_MASK \
22 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
24 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
26 struct bpf_stab *stab;
30 if (!capable(CAP_NET_ADMIN))
31 return ERR_PTR(-EPERM);
32 if (attr->max_entries == 0 ||
33 attr->key_size != 4 ||
34 attr->value_size != 4 ||
35 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
36 return ERR_PTR(-EINVAL);
38 stab = kzalloc(sizeof(*stab), GFP_USER);
40 return ERR_PTR(-ENOMEM);
42 bpf_map_init_from_attr(&stab->map, attr);
43 raw_spin_lock_init(&stab->lock);
45 /* Make sure page count doesn't overflow. */
46 cost = (u64) stab->map.max_entries * sizeof(struct sock *);
47 err = bpf_map_charge_init(&stab->map.memory, cost);
51 stab->sks = bpf_map_area_alloc(stab->map.max_entries *
52 sizeof(struct sock *),
57 bpf_map_charge_finish(&stab->map.memory);
63 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
65 u32 ufd = attr->target_fd;
71 map = __bpf_map_get(f);
74 ret = sock_map_prog_update(map, prog, attr->attach_type);
79 static void sock_map_sk_acquire(struct sock *sk)
80 __acquires(&sk->sk_lock.slock)
87 static void sock_map_sk_release(struct sock *sk)
88 __releases(&sk->sk_lock.slock)
95 static void sock_map_add_link(struct sk_psock *psock,
96 struct sk_psock_link *link,
97 struct bpf_map *map, void *link_raw)
99 link->link_raw = link_raw;
101 spin_lock_bh(&psock->link_lock);
102 list_add_tail(&link->list, &psock->link);
103 spin_unlock_bh(&psock->link_lock);
106 static void sock_map_del_link(struct sock *sk,
107 struct sk_psock *psock, void *link_raw)
109 struct sk_psock_link *link, *tmp;
110 bool strp_stop = false;
112 spin_lock_bh(&psock->link_lock);
113 list_for_each_entry_safe(link, tmp, &psock->link, list) {
114 if (link->link_raw == link_raw) {
115 struct bpf_map *map = link->map;
116 struct bpf_stab *stab = container_of(map, struct bpf_stab,
118 if (psock->parser.enabled && stab->progs.skb_parser)
120 list_del(&link->list);
121 sk_psock_free_link(link);
124 spin_unlock_bh(&psock->link_lock);
126 write_lock_bh(&sk->sk_callback_lock);
127 sk_psock_stop_strp(sk, psock);
128 write_unlock_bh(&sk->sk_callback_lock);
132 static void sock_map_unref(struct sock *sk, void *link_raw)
134 struct sk_psock *psock = sk_psock(sk);
137 sock_map_del_link(sk, psock, link_raw);
138 sk_psock_put(sk, psock);
142 static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
145 struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
146 bool skb_progs, sk_psock_is_new = false;
147 struct sk_psock *psock;
150 skb_verdict = READ_ONCE(progs->skb_verdict);
151 skb_parser = READ_ONCE(progs->skb_parser);
152 skb_progs = skb_parser && skb_verdict;
154 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
155 if (IS_ERR(skb_verdict))
156 return PTR_ERR(skb_verdict);
157 skb_parser = bpf_prog_inc_not_zero(skb_parser);
158 if (IS_ERR(skb_parser)) {
159 bpf_prog_put(skb_verdict);
160 return PTR_ERR(skb_parser);
164 msg_parser = READ_ONCE(progs->msg_parser);
166 msg_parser = bpf_prog_inc_not_zero(msg_parser);
167 if (IS_ERR(msg_parser)) {
168 ret = PTR_ERR(msg_parser);
173 psock = sk_psock_get_checked(sk);
175 ret = PTR_ERR(psock);
180 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
181 (skb_progs && READ_ONCE(psock->progs.skb_parser))) {
182 sk_psock_put(sk, psock);
187 psock = sk_psock_init(sk, map->numa_node);
192 sk_psock_is_new = true;
196 psock_set_prog(&psock->progs.msg_parser, msg_parser);
197 if (sk_psock_is_new) {
198 ret = tcp_bpf_init(sk);
205 write_lock_bh(&sk->sk_callback_lock);
206 if (skb_progs && !psock->parser.enabled) {
207 ret = sk_psock_init_strp(sk, psock);
209 write_unlock_bh(&sk->sk_callback_lock);
212 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
213 psock_set_prog(&psock->progs.skb_parser, skb_parser);
214 sk_psock_start_strp(sk, psock);
216 write_unlock_bh(&sk->sk_callback_lock);
219 sk_psock_put(sk, psock);
222 bpf_prog_put(msg_parser);
225 bpf_prog_put(skb_verdict);
226 bpf_prog_put(skb_parser);
231 static void sock_map_free(struct bpf_map *map)
233 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
238 raw_spin_lock_bh(&stab->lock);
239 for (i = 0; i < stab->map.max_entries; i++) {
240 struct sock **psk = &stab->sks[i];
243 sk = xchg(psk, NULL);
246 sock_map_unref(sk, psk);
250 raw_spin_unlock_bh(&stab->lock);
255 bpf_map_area_free(stab->sks);
259 static void sock_map_release_progs(struct bpf_map *map)
261 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
264 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
266 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
268 WARN_ON_ONCE(!rcu_read_lock_held());
270 if (unlikely(key >= map->max_entries))
272 return READ_ONCE(stab->sks[key]);
275 static void *sock_map_lookup(struct bpf_map *map, void *key)
277 return ERR_PTR(-EOPNOTSUPP);
280 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
286 raw_spin_lock_bh(&stab->lock);
288 if (!sk_test || sk_test == sk)
289 sk = xchg(psk, NULL);
292 sock_map_unref(sk, psk);
296 raw_spin_unlock_bh(&stab->lock);
300 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
303 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
305 __sock_map_delete(stab, sk, link_raw);
308 static int sock_map_delete_elem(struct bpf_map *map, void *key)
310 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
314 if (unlikely(i >= map->max_entries))
318 return __sock_map_delete(stab, NULL, psk);
321 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
323 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
324 u32 i = key ? *(u32 *)key : U32_MAX;
325 u32 *key_next = next;
327 if (i == stab->map.max_entries - 1)
329 if (i >= stab->map.max_entries)
336 static int sock_map_update_common(struct bpf_map *map, u32 idx,
337 struct sock *sk, u64 flags)
339 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
340 struct inet_connection_sock *icsk = inet_csk(sk);
341 struct sk_psock_link *link;
342 struct sk_psock *psock;
346 WARN_ON_ONCE(!rcu_read_lock_held());
347 if (unlikely(flags > BPF_EXIST))
349 if (unlikely(idx >= map->max_entries))
351 if (unlikely(rcu_access_pointer(icsk->icsk_ulp_data)))
354 link = sk_psock_init_link();
358 ret = sock_map_link(map, &stab->progs, sk);
362 psock = sk_psock(sk);
363 WARN_ON_ONCE(!psock);
365 raw_spin_lock_bh(&stab->lock);
366 osk = stab->sks[idx];
367 if (osk && flags == BPF_NOEXIST) {
370 } else if (!osk && flags == BPF_EXIST) {
375 sock_map_add_link(psock, link, map, &stab->sks[idx]);
378 sock_map_unref(osk, &stab->sks[idx]);
379 raw_spin_unlock_bh(&stab->lock);
382 raw_spin_unlock_bh(&stab->lock);
384 sk_psock_put(sk, psock);
386 sk_psock_free_link(link);
390 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
392 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
393 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB;
396 static bool sock_map_sk_is_suitable(const struct sock *sk)
398 return sk->sk_type == SOCK_STREAM &&
399 sk->sk_protocol == IPPROTO_TCP;
402 static int sock_map_update_elem(struct bpf_map *map, void *key,
403 void *value, u64 flags)
405 u32 ufd = *(u32 *)value;
406 u32 idx = *(u32 *)key;
411 sock = sockfd_lookup(ufd, &ret);
419 if (!sock_map_sk_is_suitable(sk) ||
420 sk->sk_state != TCP_ESTABLISHED) {
425 sock_map_sk_acquire(sk);
426 ret = sock_map_update_common(map, idx, sk, flags);
427 sock_map_sk_release(sk);
433 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
434 struct bpf_map *, map, void *, key, u64, flags)
436 WARN_ON_ONCE(!rcu_read_lock_held());
438 if (likely(sock_map_sk_is_suitable(sops->sk) &&
439 sock_map_op_okay(sops)))
440 return sock_map_update_common(map, *(u32 *)key, sops->sk,
445 const struct bpf_func_proto bpf_sock_map_update_proto = {
446 .func = bpf_sock_map_update,
449 .ret_type = RET_INTEGER,
450 .arg1_type = ARG_PTR_TO_CTX,
451 .arg2_type = ARG_CONST_MAP_PTR,
452 .arg3_type = ARG_PTR_TO_MAP_KEY,
453 .arg4_type = ARG_ANYTHING,
456 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
457 struct bpf_map *, map, u32, key, u64, flags)
459 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
461 if (unlikely(flags & ~(BPF_F_INGRESS)))
463 tcb->bpf.flags = flags;
464 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
465 if (!tcb->bpf.sk_redir)
470 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
471 .func = bpf_sk_redirect_map,
473 .ret_type = RET_INTEGER,
474 .arg1_type = ARG_PTR_TO_CTX,
475 .arg2_type = ARG_CONST_MAP_PTR,
476 .arg3_type = ARG_ANYTHING,
477 .arg4_type = ARG_ANYTHING,
480 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
481 struct bpf_map *, map, u32, key, u64, flags)
483 if (unlikely(flags & ~(BPF_F_INGRESS)))
486 msg->sk_redir = __sock_map_lookup_elem(map, key);
492 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
493 .func = bpf_msg_redirect_map,
495 .ret_type = RET_INTEGER,
496 .arg1_type = ARG_PTR_TO_CTX,
497 .arg2_type = ARG_CONST_MAP_PTR,
498 .arg3_type = ARG_ANYTHING,
499 .arg4_type = ARG_ANYTHING,
502 const struct bpf_map_ops sock_map_ops = {
503 .map_alloc = sock_map_alloc,
504 .map_free = sock_map_free,
505 .map_get_next_key = sock_map_get_next_key,
506 .map_update_elem = sock_map_update_elem,
507 .map_delete_elem = sock_map_delete_elem,
508 .map_lookup_elem = sock_map_lookup,
509 .map_release_uref = sock_map_release_progs,
510 .map_check_btf = map_check_no_btf,
513 struct bpf_htab_elem {
517 struct hlist_node node;
521 struct bpf_htab_bucket {
522 struct hlist_head head;
528 struct bpf_htab_bucket *buckets;
531 struct sk_psock_progs progs;
535 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
537 return jhash(key, len, 0);
540 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
543 return &htab->buckets[hash & (htab->buckets_num - 1)];
546 static struct bpf_htab_elem *
547 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
550 struct bpf_htab_elem *elem;
552 hlist_for_each_entry_rcu(elem, head, node) {
553 if (elem->hash == hash &&
554 !memcmp(&elem->key, key, key_size))
561 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
563 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
564 u32 key_size = map->key_size, hash;
565 struct bpf_htab_bucket *bucket;
566 struct bpf_htab_elem *elem;
568 WARN_ON_ONCE(!rcu_read_lock_held());
570 hash = sock_hash_bucket_hash(key, key_size);
571 bucket = sock_hash_select_bucket(htab, hash);
572 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
574 return elem ? elem->sk : NULL;
577 static void sock_hash_free_elem(struct bpf_htab *htab,
578 struct bpf_htab_elem *elem)
580 atomic_dec(&htab->count);
581 kfree_rcu(elem, rcu);
584 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
587 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
588 struct bpf_htab_elem *elem_probe, *elem = link_raw;
589 struct bpf_htab_bucket *bucket;
591 WARN_ON_ONCE(!rcu_read_lock_held());
592 bucket = sock_hash_select_bucket(htab, elem->hash);
594 /* elem may be deleted in parallel from the map, but access here
595 * is okay since it's going away only after RCU grace period.
596 * However, we need to check whether it's still present.
598 raw_spin_lock_bh(&bucket->lock);
599 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
600 elem->key, map->key_size);
601 if (elem_probe && elem_probe == elem) {
602 hlist_del_rcu(&elem->node);
603 sock_map_unref(elem->sk, elem);
604 sock_hash_free_elem(htab, elem);
606 raw_spin_unlock_bh(&bucket->lock);
609 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
611 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
612 u32 hash, key_size = map->key_size;
613 struct bpf_htab_bucket *bucket;
614 struct bpf_htab_elem *elem;
617 hash = sock_hash_bucket_hash(key, key_size);
618 bucket = sock_hash_select_bucket(htab, hash);
620 raw_spin_lock_bh(&bucket->lock);
621 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
623 hlist_del_rcu(&elem->node);
624 sock_map_unref(elem->sk, elem);
625 sock_hash_free_elem(htab, elem);
628 raw_spin_unlock_bh(&bucket->lock);
632 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
633 void *key, u32 key_size,
634 u32 hash, struct sock *sk,
635 struct bpf_htab_elem *old)
637 struct bpf_htab_elem *new;
639 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
641 atomic_dec(&htab->count);
642 return ERR_PTR(-E2BIG);
646 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
647 htab->map.numa_node);
649 atomic_dec(&htab->count);
650 return ERR_PTR(-ENOMEM);
652 memcpy(new->key, key, key_size);
658 static int sock_hash_update_common(struct bpf_map *map, void *key,
659 struct sock *sk, u64 flags)
661 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
662 struct inet_connection_sock *icsk = inet_csk(sk);
663 u32 key_size = map->key_size, hash;
664 struct bpf_htab_elem *elem, *elem_new;
665 struct bpf_htab_bucket *bucket;
666 struct sk_psock_link *link;
667 struct sk_psock *psock;
670 WARN_ON_ONCE(!rcu_read_lock_held());
671 if (unlikely(flags > BPF_EXIST))
673 if (unlikely(icsk->icsk_ulp_data))
676 link = sk_psock_init_link();
680 ret = sock_map_link(map, &htab->progs, sk);
684 psock = sk_psock(sk);
685 WARN_ON_ONCE(!psock);
687 hash = sock_hash_bucket_hash(key, key_size);
688 bucket = sock_hash_select_bucket(htab, hash);
690 raw_spin_lock_bh(&bucket->lock);
691 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
692 if (elem && flags == BPF_NOEXIST) {
695 } else if (!elem && flags == BPF_EXIST) {
700 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
701 if (IS_ERR(elem_new)) {
702 ret = PTR_ERR(elem_new);
706 sock_map_add_link(psock, link, map, elem_new);
707 /* Add new element to the head of the list, so that
708 * concurrent search will find it before old elem.
710 hlist_add_head_rcu(&elem_new->node, &bucket->head);
712 hlist_del_rcu(&elem->node);
713 sock_map_unref(elem->sk, elem);
714 sock_hash_free_elem(htab, elem);
716 raw_spin_unlock_bh(&bucket->lock);
719 raw_spin_unlock_bh(&bucket->lock);
720 sk_psock_put(sk, psock);
722 sk_psock_free_link(link);
726 static int sock_hash_update_elem(struct bpf_map *map, void *key,
727 void *value, u64 flags)
729 u32 ufd = *(u32 *)value;
734 sock = sockfd_lookup(ufd, &ret);
742 if (!sock_map_sk_is_suitable(sk) ||
743 sk->sk_state != TCP_ESTABLISHED) {
748 sock_map_sk_acquire(sk);
749 ret = sock_hash_update_common(map, key, sk, flags);
750 sock_map_sk_release(sk);
756 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
759 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
760 struct bpf_htab_elem *elem, *elem_next;
761 u32 hash, key_size = map->key_size;
762 struct hlist_head *head;
766 goto find_first_elem;
767 hash = sock_hash_bucket_hash(key, key_size);
768 head = &sock_hash_select_bucket(htab, hash)->head;
769 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
771 goto find_first_elem;
773 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
774 struct bpf_htab_elem, node);
776 memcpy(key_next, elem_next->key, key_size);
780 i = hash & (htab->buckets_num - 1);
783 for (; i < htab->buckets_num; i++) {
784 head = &sock_hash_select_bucket(htab, i)->head;
785 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
786 struct bpf_htab_elem, node);
788 memcpy(key_next, elem_next->key, key_size);
796 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
798 struct bpf_htab *htab;
802 if (!capable(CAP_NET_ADMIN))
803 return ERR_PTR(-EPERM);
804 if (attr->max_entries == 0 ||
805 attr->key_size == 0 ||
806 attr->value_size != 4 ||
807 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
808 return ERR_PTR(-EINVAL);
809 if (attr->key_size > MAX_BPF_STACK)
810 return ERR_PTR(-E2BIG);
812 htab = kzalloc(sizeof(*htab), GFP_USER);
814 return ERR_PTR(-ENOMEM);
816 bpf_map_init_from_attr(&htab->map, attr);
818 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
819 htab->elem_size = sizeof(struct bpf_htab_elem) +
820 round_up(htab->map.key_size, 8);
821 if (htab->buckets_num == 0 ||
822 htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
827 cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
828 (u64) htab->elem_size * htab->map.max_entries;
829 if (cost >= U32_MAX - PAGE_SIZE) {
834 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
835 sizeof(struct bpf_htab_bucket),
836 htab->map.numa_node);
837 if (!htab->buckets) {
842 for (i = 0; i < htab->buckets_num; i++) {
843 INIT_HLIST_HEAD(&htab->buckets[i].head);
844 raw_spin_lock_init(&htab->buckets[i].lock);
853 static void sock_hash_free(struct bpf_map *map)
855 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
856 struct bpf_htab_bucket *bucket;
857 struct bpf_htab_elem *elem;
858 struct hlist_node *node;
863 for (i = 0; i < htab->buckets_num; i++) {
864 bucket = sock_hash_select_bucket(htab, i);
865 raw_spin_lock_bh(&bucket->lock);
866 hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
867 hlist_del_rcu(&elem->node);
869 sock_map_unref(elem->sk, elem);
870 release_sock(elem->sk);
872 raw_spin_unlock_bh(&bucket->lock);
876 bpf_map_area_free(htab->buckets);
880 static void sock_hash_release_progs(struct bpf_map *map)
882 psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
885 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
886 struct bpf_map *, map, void *, key, u64, flags)
888 WARN_ON_ONCE(!rcu_read_lock_held());
890 if (likely(sock_map_sk_is_suitable(sops->sk) &&
891 sock_map_op_okay(sops)))
892 return sock_hash_update_common(map, key, sops->sk, flags);
896 const struct bpf_func_proto bpf_sock_hash_update_proto = {
897 .func = bpf_sock_hash_update,
900 .ret_type = RET_INTEGER,
901 .arg1_type = ARG_PTR_TO_CTX,
902 .arg2_type = ARG_CONST_MAP_PTR,
903 .arg3_type = ARG_PTR_TO_MAP_KEY,
904 .arg4_type = ARG_ANYTHING,
907 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
908 struct bpf_map *, map, void *, key, u64, flags)
910 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
912 if (unlikely(flags & ~(BPF_F_INGRESS)))
914 tcb->bpf.flags = flags;
915 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
916 if (!tcb->bpf.sk_redir)
921 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
922 .func = bpf_sk_redirect_hash,
924 .ret_type = RET_INTEGER,
925 .arg1_type = ARG_PTR_TO_CTX,
926 .arg2_type = ARG_CONST_MAP_PTR,
927 .arg3_type = ARG_PTR_TO_MAP_KEY,
928 .arg4_type = ARG_ANYTHING,
931 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
932 struct bpf_map *, map, void *, key, u64, flags)
934 if (unlikely(flags & ~(BPF_F_INGRESS)))
937 msg->sk_redir = __sock_hash_lookup_elem(map, key);
943 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
944 .func = bpf_msg_redirect_hash,
946 .ret_type = RET_INTEGER,
947 .arg1_type = ARG_PTR_TO_CTX,
948 .arg2_type = ARG_CONST_MAP_PTR,
949 .arg3_type = ARG_PTR_TO_MAP_KEY,
950 .arg4_type = ARG_ANYTHING,
953 const struct bpf_map_ops sock_hash_ops = {
954 .map_alloc = sock_hash_alloc,
955 .map_free = sock_hash_free,
956 .map_get_next_key = sock_hash_get_next_key,
957 .map_update_elem = sock_hash_update_elem,
958 .map_delete_elem = sock_hash_delete_elem,
959 .map_lookup_elem = sock_map_lookup,
960 .map_release_uref = sock_hash_release_progs,
961 .map_check_btf = map_check_no_btf,
964 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
966 switch (map->map_type) {
967 case BPF_MAP_TYPE_SOCKMAP:
968 return &container_of(map, struct bpf_stab, map)->progs;
969 case BPF_MAP_TYPE_SOCKHASH:
970 return &container_of(map, struct bpf_htab, map)->progs;
978 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
981 struct sk_psock_progs *progs = sock_map_progs(map);
987 case BPF_SK_MSG_VERDICT:
988 psock_set_prog(&progs->msg_parser, prog);
990 case BPF_SK_SKB_STREAM_PARSER:
991 psock_set_prog(&progs->skb_parser, prog);
993 case BPF_SK_SKB_STREAM_VERDICT:
994 psock_set_prog(&progs->skb_verdict, prog);
1003 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
1005 switch (link->map->map_type) {
1006 case BPF_MAP_TYPE_SOCKMAP:
1007 return sock_map_delete_from_link(link->map, sk,
1009 case BPF_MAP_TYPE_SOCKHASH:
1010 return sock_hash_delete_from_link(link->map, sk,