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);
236 /* After the sync no updates or deletes will be in-flight so it
237 * is safe to walk map and remove entries without risking a race
238 * in EEXIST update case.
241 for (i = 0; i < stab->map.max_entries; i++) {
242 struct sock **psk = &stab->sks[i];
245 sk = xchg(psk, NULL);
249 sock_map_unref(sk, psk);
255 /* wait for psock readers accessing its map link */
258 bpf_map_area_free(stab->sks);
262 static void sock_map_release_progs(struct bpf_map *map)
264 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
267 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
269 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
271 WARN_ON_ONCE(!rcu_read_lock_held());
273 if (unlikely(key >= map->max_entries))
275 return READ_ONCE(stab->sks[key]);
278 static void *sock_map_lookup(struct bpf_map *map, void *key)
280 return ERR_PTR(-EOPNOTSUPP);
283 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
289 raw_spin_lock_bh(&stab->lock);
291 if (!sk_test || sk_test == sk)
292 sk = xchg(psk, NULL);
295 sock_map_unref(sk, psk);
299 raw_spin_unlock_bh(&stab->lock);
303 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
306 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
308 __sock_map_delete(stab, sk, link_raw);
311 static int sock_map_delete_elem(struct bpf_map *map, void *key)
313 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
317 if (unlikely(i >= map->max_entries))
321 return __sock_map_delete(stab, NULL, psk);
324 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
326 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
327 u32 i = key ? *(u32 *)key : U32_MAX;
328 u32 *key_next = next;
330 if (i == stab->map.max_entries - 1)
332 if (i >= stab->map.max_entries)
339 static int sock_map_update_common(struct bpf_map *map, u32 idx,
340 struct sock *sk, u64 flags)
342 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
343 struct inet_connection_sock *icsk = inet_csk(sk);
344 struct sk_psock_link *link;
345 struct sk_psock *psock;
349 WARN_ON_ONCE(!rcu_read_lock_held());
350 if (unlikely(flags > BPF_EXIST))
352 if (unlikely(idx >= map->max_entries))
354 if (unlikely(rcu_access_pointer(icsk->icsk_ulp_data)))
357 link = sk_psock_init_link();
361 ret = sock_map_link(map, &stab->progs, sk);
365 psock = sk_psock(sk);
366 WARN_ON_ONCE(!psock);
368 raw_spin_lock_bh(&stab->lock);
369 osk = stab->sks[idx];
370 if (osk && flags == BPF_NOEXIST) {
373 } else if (!osk && flags == BPF_EXIST) {
378 sock_map_add_link(psock, link, map, &stab->sks[idx]);
381 sock_map_unref(osk, &stab->sks[idx]);
382 raw_spin_unlock_bh(&stab->lock);
385 raw_spin_unlock_bh(&stab->lock);
387 sk_psock_put(sk, psock);
389 sk_psock_free_link(link);
393 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
395 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
396 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB;
399 static bool sock_map_sk_is_suitable(const struct sock *sk)
401 return sk->sk_type == SOCK_STREAM &&
402 sk->sk_protocol == IPPROTO_TCP;
405 static int sock_map_update_elem(struct bpf_map *map, void *key,
406 void *value, u64 flags)
408 u32 ufd = *(u32 *)value;
409 u32 idx = *(u32 *)key;
414 sock = sockfd_lookup(ufd, &ret);
422 if (!sock_map_sk_is_suitable(sk)) {
427 sock_map_sk_acquire(sk);
428 if (sk->sk_state != TCP_ESTABLISHED)
431 ret = sock_map_update_common(map, idx, sk, flags);
432 sock_map_sk_release(sk);
438 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
439 struct bpf_map *, map, void *, key, u64, flags)
441 WARN_ON_ONCE(!rcu_read_lock_held());
443 if (likely(sock_map_sk_is_suitable(sops->sk) &&
444 sock_map_op_okay(sops)))
445 return sock_map_update_common(map, *(u32 *)key, sops->sk,
450 const struct bpf_func_proto bpf_sock_map_update_proto = {
451 .func = bpf_sock_map_update,
454 .ret_type = RET_INTEGER,
455 .arg1_type = ARG_PTR_TO_CTX,
456 .arg2_type = ARG_CONST_MAP_PTR,
457 .arg3_type = ARG_PTR_TO_MAP_KEY,
458 .arg4_type = ARG_ANYTHING,
461 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
462 struct bpf_map *, map, u32, key, u64, flags)
464 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
466 if (unlikely(flags & ~(BPF_F_INGRESS)))
468 tcb->bpf.flags = flags;
469 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
470 if (!tcb->bpf.sk_redir)
475 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
476 .func = bpf_sk_redirect_map,
478 .ret_type = RET_INTEGER,
479 .arg1_type = ARG_PTR_TO_CTX,
480 .arg2_type = ARG_CONST_MAP_PTR,
481 .arg3_type = ARG_ANYTHING,
482 .arg4_type = ARG_ANYTHING,
485 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
486 struct bpf_map *, map, u32, key, u64, flags)
488 if (unlikely(flags & ~(BPF_F_INGRESS)))
491 msg->sk_redir = __sock_map_lookup_elem(map, key);
497 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
498 .func = bpf_msg_redirect_map,
500 .ret_type = RET_INTEGER,
501 .arg1_type = ARG_PTR_TO_CTX,
502 .arg2_type = ARG_CONST_MAP_PTR,
503 .arg3_type = ARG_ANYTHING,
504 .arg4_type = ARG_ANYTHING,
507 const struct bpf_map_ops sock_map_ops = {
508 .map_alloc = sock_map_alloc,
509 .map_free = sock_map_free,
510 .map_get_next_key = sock_map_get_next_key,
511 .map_update_elem = sock_map_update_elem,
512 .map_delete_elem = sock_map_delete_elem,
513 .map_lookup_elem = sock_map_lookup,
514 .map_release_uref = sock_map_release_progs,
515 .map_check_btf = map_check_no_btf,
518 struct bpf_htab_elem {
522 struct hlist_node node;
526 struct bpf_htab_bucket {
527 struct hlist_head head;
533 struct bpf_htab_bucket *buckets;
536 struct sk_psock_progs progs;
540 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
542 return jhash(key, len, 0);
545 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
548 return &htab->buckets[hash & (htab->buckets_num - 1)];
551 static struct bpf_htab_elem *
552 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
555 struct bpf_htab_elem *elem;
557 hlist_for_each_entry_rcu(elem, head, node) {
558 if (elem->hash == hash &&
559 !memcmp(&elem->key, key, key_size))
566 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
568 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
569 u32 key_size = map->key_size, hash;
570 struct bpf_htab_bucket *bucket;
571 struct bpf_htab_elem *elem;
573 WARN_ON_ONCE(!rcu_read_lock_held());
575 hash = sock_hash_bucket_hash(key, key_size);
576 bucket = sock_hash_select_bucket(htab, hash);
577 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
579 return elem ? elem->sk : NULL;
582 static void sock_hash_free_elem(struct bpf_htab *htab,
583 struct bpf_htab_elem *elem)
585 atomic_dec(&htab->count);
586 kfree_rcu(elem, rcu);
589 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
592 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
593 struct bpf_htab_elem *elem_probe, *elem = link_raw;
594 struct bpf_htab_bucket *bucket;
596 WARN_ON_ONCE(!rcu_read_lock_held());
597 bucket = sock_hash_select_bucket(htab, elem->hash);
599 /* elem may be deleted in parallel from the map, but access here
600 * is okay since it's going away only after RCU grace period.
601 * However, we need to check whether it's still present.
603 raw_spin_lock_bh(&bucket->lock);
604 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
605 elem->key, map->key_size);
606 if (elem_probe && elem_probe == elem) {
607 hlist_del_rcu(&elem->node);
608 sock_map_unref(elem->sk, elem);
609 sock_hash_free_elem(htab, elem);
611 raw_spin_unlock_bh(&bucket->lock);
614 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
616 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
617 u32 hash, key_size = map->key_size;
618 struct bpf_htab_bucket *bucket;
619 struct bpf_htab_elem *elem;
622 hash = sock_hash_bucket_hash(key, key_size);
623 bucket = sock_hash_select_bucket(htab, hash);
625 raw_spin_lock_bh(&bucket->lock);
626 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
628 hlist_del_rcu(&elem->node);
629 sock_map_unref(elem->sk, elem);
630 sock_hash_free_elem(htab, elem);
633 raw_spin_unlock_bh(&bucket->lock);
637 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
638 void *key, u32 key_size,
639 u32 hash, struct sock *sk,
640 struct bpf_htab_elem *old)
642 struct bpf_htab_elem *new;
644 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
646 atomic_dec(&htab->count);
647 return ERR_PTR(-E2BIG);
651 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
652 htab->map.numa_node);
654 atomic_dec(&htab->count);
655 return ERR_PTR(-ENOMEM);
657 memcpy(new->key, key, key_size);
663 static int sock_hash_update_common(struct bpf_map *map, void *key,
664 struct sock *sk, u64 flags)
666 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
667 struct inet_connection_sock *icsk = inet_csk(sk);
668 u32 key_size = map->key_size, hash;
669 struct bpf_htab_elem *elem, *elem_new;
670 struct bpf_htab_bucket *bucket;
671 struct sk_psock_link *link;
672 struct sk_psock *psock;
675 WARN_ON_ONCE(!rcu_read_lock_held());
676 if (unlikely(flags > BPF_EXIST))
678 if (unlikely(icsk->icsk_ulp_data))
681 link = sk_psock_init_link();
685 ret = sock_map_link(map, &htab->progs, sk);
689 psock = sk_psock(sk);
690 WARN_ON_ONCE(!psock);
692 hash = sock_hash_bucket_hash(key, key_size);
693 bucket = sock_hash_select_bucket(htab, hash);
695 raw_spin_lock_bh(&bucket->lock);
696 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
697 if (elem && flags == BPF_NOEXIST) {
700 } else if (!elem && flags == BPF_EXIST) {
705 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
706 if (IS_ERR(elem_new)) {
707 ret = PTR_ERR(elem_new);
711 sock_map_add_link(psock, link, map, elem_new);
712 /* Add new element to the head of the list, so that
713 * concurrent search will find it before old elem.
715 hlist_add_head_rcu(&elem_new->node, &bucket->head);
717 hlist_del_rcu(&elem->node);
718 sock_map_unref(elem->sk, elem);
719 sock_hash_free_elem(htab, elem);
721 raw_spin_unlock_bh(&bucket->lock);
724 raw_spin_unlock_bh(&bucket->lock);
725 sk_psock_put(sk, psock);
727 sk_psock_free_link(link);
731 static int sock_hash_update_elem(struct bpf_map *map, void *key,
732 void *value, u64 flags)
734 u32 ufd = *(u32 *)value;
739 sock = sockfd_lookup(ufd, &ret);
747 if (!sock_map_sk_is_suitable(sk)) {
752 sock_map_sk_acquire(sk);
753 if (sk->sk_state != TCP_ESTABLISHED)
756 ret = sock_hash_update_common(map, key, sk, flags);
757 sock_map_sk_release(sk);
763 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
766 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
767 struct bpf_htab_elem *elem, *elem_next;
768 u32 hash, key_size = map->key_size;
769 struct hlist_head *head;
773 goto find_first_elem;
774 hash = sock_hash_bucket_hash(key, key_size);
775 head = &sock_hash_select_bucket(htab, hash)->head;
776 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
778 goto find_first_elem;
780 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
781 struct bpf_htab_elem, node);
783 memcpy(key_next, elem_next->key, key_size);
787 i = hash & (htab->buckets_num - 1);
790 for (; i < htab->buckets_num; i++) {
791 head = &sock_hash_select_bucket(htab, i)->head;
792 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
793 struct bpf_htab_elem, node);
795 memcpy(key_next, elem_next->key, key_size);
803 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
805 struct bpf_htab *htab;
809 if (!capable(CAP_NET_ADMIN))
810 return ERR_PTR(-EPERM);
811 if (attr->max_entries == 0 ||
812 attr->key_size == 0 ||
813 attr->value_size != 4 ||
814 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
815 return ERR_PTR(-EINVAL);
816 if (attr->key_size > MAX_BPF_STACK)
817 return ERR_PTR(-E2BIG);
819 htab = kzalloc(sizeof(*htab), GFP_USER);
821 return ERR_PTR(-ENOMEM);
823 bpf_map_init_from_attr(&htab->map, attr);
825 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
826 htab->elem_size = sizeof(struct bpf_htab_elem) +
827 round_up(htab->map.key_size, 8);
828 if (htab->buckets_num == 0 ||
829 htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
834 cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
835 (u64) htab->elem_size * htab->map.max_entries;
836 if (cost >= U32_MAX - PAGE_SIZE) {
841 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
842 sizeof(struct bpf_htab_bucket),
843 htab->map.numa_node);
844 if (!htab->buckets) {
849 for (i = 0; i < htab->buckets_num; i++) {
850 INIT_HLIST_HEAD(&htab->buckets[i].head);
851 raw_spin_lock_init(&htab->buckets[i].lock);
860 static void sock_hash_free(struct bpf_map *map)
862 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
863 struct bpf_htab_bucket *bucket;
864 struct bpf_htab_elem *elem;
865 struct hlist_node *node;
868 /* After the sync no updates or deletes will be in-flight so it
869 * is safe to walk map and remove entries without risking a race
870 * in EEXIST update case.
873 for (i = 0; i < htab->buckets_num; i++) {
874 bucket = sock_hash_select_bucket(htab, i);
875 hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
876 hlist_del_rcu(&elem->node);
879 sock_map_unref(elem->sk, elem);
881 release_sock(elem->sk);
885 /* wait for psock readers accessing its map link */
888 /* wait for psock readers accessing its map link */
891 bpf_map_area_free(htab->buckets);
895 static void sock_hash_release_progs(struct bpf_map *map)
897 psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
900 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
901 struct bpf_map *, map, void *, key, u64, flags)
903 WARN_ON_ONCE(!rcu_read_lock_held());
905 if (likely(sock_map_sk_is_suitable(sops->sk) &&
906 sock_map_op_okay(sops)))
907 return sock_hash_update_common(map, key, sops->sk, flags);
911 const struct bpf_func_proto bpf_sock_hash_update_proto = {
912 .func = bpf_sock_hash_update,
915 .ret_type = RET_INTEGER,
916 .arg1_type = ARG_PTR_TO_CTX,
917 .arg2_type = ARG_CONST_MAP_PTR,
918 .arg3_type = ARG_PTR_TO_MAP_KEY,
919 .arg4_type = ARG_ANYTHING,
922 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
923 struct bpf_map *, map, void *, key, u64, flags)
925 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
927 if (unlikely(flags & ~(BPF_F_INGRESS)))
929 tcb->bpf.flags = flags;
930 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
931 if (!tcb->bpf.sk_redir)
936 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
937 .func = bpf_sk_redirect_hash,
939 .ret_type = RET_INTEGER,
940 .arg1_type = ARG_PTR_TO_CTX,
941 .arg2_type = ARG_CONST_MAP_PTR,
942 .arg3_type = ARG_PTR_TO_MAP_KEY,
943 .arg4_type = ARG_ANYTHING,
946 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
947 struct bpf_map *, map, void *, key, u64, flags)
949 if (unlikely(flags & ~(BPF_F_INGRESS)))
952 msg->sk_redir = __sock_hash_lookup_elem(map, key);
958 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
959 .func = bpf_msg_redirect_hash,
961 .ret_type = RET_INTEGER,
962 .arg1_type = ARG_PTR_TO_CTX,
963 .arg2_type = ARG_CONST_MAP_PTR,
964 .arg3_type = ARG_PTR_TO_MAP_KEY,
965 .arg4_type = ARG_ANYTHING,
968 const struct bpf_map_ops sock_hash_ops = {
969 .map_alloc = sock_hash_alloc,
970 .map_free = sock_hash_free,
971 .map_get_next_key = sock_hash_get_next_key,
972 .map_update_elem = sock_hash_update_elem,
973 .map_delete_elem = sock_hash_delete_elem,
974 .map_lookup_elem = sock_map_lookup,
975 .map_release_uref = sock_hash_release_progs,
976 .map_check_btf = map_check_no_btf,
979 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
981 switch (map->map_type) {
982 case BPF_MAP_TYPE_SOCKMAP:
983 return &container_of(map, struct bpf_stab, map)->progs;
984 case BPF_MAP_TYPE_SOCKHASH:
985 return &container_of(map, struct bpf_htab, map)->progs;
993 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
996 struct sk_psock_progs *progs = sock_map_progs(map);
1002 case BPF_SK_MSG_VERDICT:
1003 psock_set_prog(&progs->msg_parser, prog);
1005 case BPF_SK_SKB_STREAM_PARSER:
1006 psock_set_prog(&progs->skb_parser, prog);
1008 case BPF_SK_SKB_STREAM_VERDICT:
1009 psock_set_prog(&progs->skb_verdict, prog);
1018 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
1020 switch (link->map->map_type) {
1021 case BPF_MAP_TYPE_SOCKMAP:
1022 return sock_map_delete_from_link(link->map, sk,
1024 case BPF_MAP_TYPE_SOCKHASH:
1025 return sock_hash_delete_from_link(link->map, sk,