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 if (cost >= U32_MAX - PAGE_SIZE) {
52 err = bpf_map_charge_init(&stab->map.memory,
53 round_up(cost, PAGE_SIZE) >> PAGE_SHIFT);
57 stab->sks = bpf_map_area_alloc(stab->map.max_entries *
58 sizeof(struct sock *),
63 bpf_map_charge_finish(&stab->map.memory);
69 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
71 u32 ufd = attr->target_fd;
77 map = __bpf_map_get(f);
80 ret = sock_map_prog_update(map, prog, attr->attach_type);
85 static void sock_map_sk_acquire(struct sock *sk)
86 __acquires(&sk->sk_lock.slock)
93 static void sock_map_sk_release(struct sock *sk)
94 __releases(&sk->sk_lock.slock)
101 static void sock_map_add_link(struct sk_psock *psock,
102 struct sk_psock_link *link,
103 struct bpf_map *map, void *link_raw)
105 link->link_raw = link_raw;
107 spin_lock_bh(&psock->link_lock);
108 list_add_tail(&link->list, &psock->link);
109 spin_unlock_bh(&psock->link_lock);
112 static void sock_map_del_link(struct sock *sk,
113 struct sk_psock *psock, void *link_raw)
115 struct sk_psock_link *link, *tmp;
116 bool strp_stop = false;
118 spin_lock_bh(&psock->link_lock);
119 list_for_each_entry_safe(link, tmp, &psock->link, list) {
120 if (link->link_raw == link_raw) {
121 struct bpf_map *map = link->map;
122 struct bpf_stab *stab = container_of(map, struct bpf_stab,
124 if (psock->parser.enabled && stab->progs.skb_parser)
126 list_del(&link->list);
127 sk_psock_free_link(link);
130 spin_unlock_bh(&psock->link_lock);
132 write_lock_bh(&sk->sk_callback_lock);
133 sk_psock_stop_strp(sk, psock);
134 write_unlock_bh(&sk->sk_callback_lock);
138 static void sock_map_unref(struct sock *sk, void *link_raw)
140 struct sk_psock *psock = sk_psock(sk);
143 sock_map_del_link(sk, psock, link_raw);
144 sk_psock_put(sk, psock);
148 static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
151 struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
152 bool skb_progs, sk_psock_is_new = false;
153 struct sk_psock *psock;
156 skb_verdict = READ_ONCE(progs->skb_verdict);
157 skb_parser = READ_ONCE(progs->skb_parser);
158 skb_progs = skb_parser && skb_verdict;
160 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
161 if (IS_ERR(skb_verdict))
162 return PTR_ERR(skb_verdict);
163 skb_parser = bpf_prog_inc_not_zero(skb_parser);
164 if (IS_ERR(skb_parser)) {
165 bpf_prog_put(skb_verdict);
166 return PTR_ERR(skb_parser);
170 msg_parser = READ_ONCE(progs->msg_parser);
172 msg_parser = bpf_prog_inc_not_zero(msg_parser);
173 if (IS_ERR(msg_parser)) {
174 ret = PTR_ERR(msg_parser);
179 psock = sk_psock_get_checked(sk);
181 ret = PTR_ERR(psock);
186 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
187 (skb_progs && READ_ONCE(psock->progs.skb_parser))) {
188 sk_psock_put(sk, psock);
193 psock = sk_psock_init(sk, map->numa_node);
198 sk_psock_is_new = true;
202 psock_set_prog(&psock->progs.msg_parser, msg_parser);
203 if (sk_psock_is_new) {
204 ret = tcp_bpf_init(sk);
211 write_lock_bh(&sk->sk_callback_lock);
212 if (skb_progs && !psock->parser.enabled) {
213 ret = sk_psock_init_strp(sk, psock);
215 write_unlock_bh(&sk->sk_callback_lock);
218 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
219 psock_set_prog(&psock->progs.skb_parser, skb_parser);
220 sk_psock_start_strp(sk, psock);
222 write_unlock_bh(&sk->sk_callback_lock);
225 sk_psock_put(sk, psock);
228 bpf_prog_put(msg_parser);
231 bpf_prog_put(skb_verdict);
232 bpf_prog_put(skb_parser);
237 static void sock_map_free(struct bpf_map *map)
239 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
244 raw_spin_lock_bh(&stab->lock);
245 for (i = 0; i < stab->map.max_entries; i++) {
246 struct sock **psk = &stab->sks[i];
249 sk = xchg(psk, NULL);
251 sock_map_unref(sk, psk);
253 raw_spin_unlock_bh(&stab->lock);
256 bpf_map_area_free(stab->sks);
260 static void sock_map_release_progs(struct bpf_map *map)
262 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
265 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
267 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
269 WARN_ON_ONCE(!rcu_read_lock_held());
271 if (unlikely(key >= map->max_entries))
273 return READ_ONCE(stab->sks[key]);
276 static void *sock_map_lookup(struct bpf_map *map, void *key)
278 return ERR_PTR(-EOPNOTSUPP);
281 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)
290 raw_spin_unlock_bh(&stab->lock);
293 sock_map_unref(sk, psk);
297 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
300 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
302 __sock_map_delete(stab, sk, link_raw);
305 static int sock_map_delete_elem(struct bpf_map *map, void *key)
307 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
311 if (unlikely(i >= map->max_entries))
315 return __sock_map_delete(stab, NULL, psk);
318 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
320 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
321 u32 i = key ? *(u32 *)key : U32_MAX;
322 u32 *key_next = next;
324 if (i == stab->map.max_entries - 1)
326 if (i >= stab->map.max_entries)
333 static int sock_map_update_common(struct bpf_map *map, u32 idx,
334 struct sock *sk, u64 flags)
336 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
337 struct sk_psock_link *link;
338 struct sk_psock *psock;
342 WARN_ON_ONCE(!rcu_read_lock_held());
343 if (unlikely(flags > BPF_EXIST))
345 if (unlikely(idx >= map->max_entries))
348 link = sk_psock_init_link();
352 ret = sock_map_link(map, &stab->progs, sk);
356 psock = sk_psock(sk);
357 WARN_ON_ONCE(!psock);
359 raw_spin_lock_bh(&stab->lock);
360 osk = stab->sks[idx];
361 if (osk && flags == BPF_NOEXIST) {
364 } else if (!osk && flags == BPF_EXIST) {
369 sock_map_add_link(psock, link, map, &stab->sks[idx]);
372 sock_map_unref(osk, &stab->sks[idx]);
373 raw_spin_unlock_bh(&stab->lock);
376 raw_spin_unlock_bh(&stab->lock);
378 sk_psock_put(sk, psock);
380 sk_psock_free_link(link);
384 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
386 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
387 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB;
390 static bool sock_map_sk_is_suitable(const struct sock *sk)
392 return sk->sk_type == SOCK_STREAM &&
393 sk->sk_protocol == IPPROTO_TCP;
396 static int sock_map_update_elem(struct bpf_map *map, void *key,
397 void *value, u64 flags)
399 u32 ufd = *(u32 *)value;
400 u32 idx = *(u32 *)key;
405 sock = sockfd_lookup(ufd, &ret);
413 if (!sock_map_sk_is_suitable(sk) ||
414 sk->sk_state != TCP_ESTABLISHED) {
419 sock_map_sk_acquire(sk);
420 ret = sock_map_update_common(map, idx, sk, flags);
421 sock_map_sk_release(sk);
427 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
428 struct bpf_map *, map, void *, key, u64, flags)
430 WARN_ON_ONCE(!rcu_read_lock_held());
432 if (likely(sock_map_sk_is_suitable(sops->sk) &&
433 sock_map_op_okay(sops)))
434 return sock_map_update_common(map, *(u32 *)key, sops->sk,
439 const struct bpf_func_proto bpf_sock_map_update_proto = {
440 .func = bpf_sock_map_update,
443 .ret_type = RET_INTEGER,
444 .arg1_type = ARG_PTR_TO_CTX,
445 .arg2_type = ARG_CONST_MAP_PTR,
446 .arg3_type = ARG_PTR_TO_MAP_KEY,
447 .arg4_type = ARG_ANYTHING,
450 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
451 struct bpf_map *, map, u32, key, u64, flags)
453 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
455 if (unlikely(flags & ~(BPF_F_INGRESS)))
457 tcb->bpf.flags = flags;
458 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
459 if (!tcb->bpf.sk_redir)
464 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
465 .func = bpf_sk_redirect_map,
467 .ret_type = RET_INTEGER,
468 .arg1_type = ARG_PTR_TO_CTX,
469 .arg2_type = ARG_CONST_MAP_PTR,
470 .arg3_type = ARG_ANYTHING,
471 .arg4_type = ARG_ANYTHING,
474 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
475 struct bpf_map *, map, u32, key, u64, flags)
477 if (unlikely(flags & ~(BPF_F_INGRESS)))
480 msg->sk_redir = __sock_map_lookup_elem(map, key);
486 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
487 .func = bpf_msg_redirect_map,
489 .ret_type = RET_INTEGER,
490 .arg1_type = ARG_PTR_TO_CTX,
491 .arg2_type = ARG_CONST_MAP_PTR,
492 .arg3_type = ARG_ANYTHING,
493 .arg4_type = ARG_ANYTHING,
496 const struct bpf_map_ops sock_map_ops = {
497 .map_alloc = sock_map_alloc,
498 .map_free = sock_map_free,
499 .map_get_next_key = sock_map_get_next_key,
500 .map_update_elem = sock_map_update_elem,
501 .map_delete_elem = sock_map_delete_elem,
502 .map_lookup_elem = sock_map_lookup,
503 .map_release_uref = sock_map_release_progs,
504 .map_check_btf = map_check_no_btf,
507 struct bpf_htab_elem {
511 struct hlist_node node;
515 struct bpf_htab_bucket {
516 struct hlist_head head;
522 struct bpf_htab_bucket *buckets;
525 struct sk_psock_progs progs;
529 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
531 return jhash(key, len, 0);
534 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
537 return &htab->buckets[hash & (htab->buckets_num - 1)];
540 static struct bpf_htab_elem *
541 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
544 struct bpf_htab_elem *elem;
546 hlist_for_each_entry_rcu(elem, head, node) {
547 if (elem->hash == hash &&
548 !memcmp(&elem->key, key, key_size))
555 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
557 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
558 u32 key_size = map->key_size, hash;
559 struct bpf_htab_bucket *bucket;
560 struct bpf_htab_elem *elem;
562 WARN_ON_ONCE(!rcu_read_lock_held());
564 hash = sock_hash_bucket_hash(key, key_size);
565 bucket = sock_hash_select_bucket(htab, hash);
566 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
568 return elem ? elem->sk : NULL;
571 static void sock_hash_free_elem(struct bpf_htab *htab,
572 struct bpf_htab_elem *elem)
574 atomic_dec(&htab->count);
575 kfree_rcu(elem, rcu);
578 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
581 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
582 struct bpf_htab_elem *elem_probe, *elem = link_raw;
583 struct bpf_htab_bucket *bucket;
585 WARN_ON_ONCE(!rcu_read_lock_held());
586 bucket = sock_hash_select_bucket(htab, elem->hash);
588 /* elem may be deleted in parallel from the map, but access here
589 * is okay since it's going away only after RCU grace period.
590 * However, we need to check whether it's still present.
592 raw_spin_lock_bh(&bucket->lock);
593 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
594 elem->key, map->key_size);
595 if (elem_probe && elem_probe == elem) {
596 hlist_del_rcu(&elem->node);
597 sock_map_unref(elem->sk, elem);
598 sock_hash_free_elem(htab, elem);
600 raw_spin_unlock_bh(&bucket->lock);
603 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
605 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
606 u32 hash, key_size = map->key_size;
607 struct bpf_htab_bucket *bucket;
608 struct bpf_htab_elem *elem;
611 hash = sock_hash_bucket_hash(key, key_size);
612 bucket = sock_hash_select_bucket(htab, hash);
614 raw_spin_lock_bh(&bucket->lock);
615 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
617 hlist_del_rcu(&elem->node);
618 sock_map_unref(elem->sk, elem);
619 sock_hash_free_elem(htab, elem);
622 raw_spin_unlock_bh(&bucket->lock);
626 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
627 void *key, u32 key_size,
628 u32 hash, struct sock *sk,
629 struct bpf_htab_elem *old)
631 struct bpf_htab_elem *new;
633 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
635 atomic_dec(&htab->count);
636 return ERR_PTR(-E2BIG);
640 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
641 htab->map.numa_node);
643 atomic_dec(&htab->count);
644 return ERR_PTR(-ENOMEM);
646 memcpy(new->key, key, key_size);
652 static int sock_hash_update_common(struct bpf_map *map, void *key,
653 struct sock *sk, u64 flags)
655 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
656 u32 key_size = map->key_size, hash;
657 struct bpf_htab_elem *elem, *elem_new;
658 struct bpf_htab_bucket *bucket;
659 struct sk_psock_link *link;
660 struct sk_psock *psock;
663 WARN_ON_ONCE(!rcu_read_lock_held());
664 if (unlikely(flags > BPF_EXIST))
667 link = sk_psock_init_link();
671 ret = sock_map_link(map, &htab->progs, sk);
675 psock = sk_psock(sk);
676 WARN_ON_ONCE(!psock);
678 hash = sock_hash_bucket_hash(key, key_size);
679 bucket = sock_hash_select_bucket(htab, hash);
681 raw_spin_lock_bh(&bucket->lock);
682 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
683 if (elem && flags == BPF_NOEXIST) {
686 } else if (!elem && flags == BPF_EXIST) {
691 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
692 if (IS_ERR(elem_new)) {
693 ret = PTR_ERR(elem_new);
697 sock_map_add_link(psock, link, map, elem_new);
698 /* Add new element to the head of the list, so that
699 * concurrent search will find it before old elem.
701 hlist_add_head_rcu(&elem_new->node, &bucket->head);
703 hlist_del_rcu(&elem->node);
704 sock_map_unref(elem->sk, elem);
705 sock_hash_free_elem(htab, elem);
707 raw_spin_unlock_bh(&bucket->lock);
710 raw_spin_unlock_bh(&bucket->lock);
711 sk_psock_put(sk, psock);
713 sk_psock_free_link(link);
717 static int sock_hash_update_elem(struct bpf_map *map, void *key,
718 void *value, u64 flags)
720 u32 ufd = *(u32 *)value;
725 sock = sockfd_lookup(ufd, &ret);
733 if (!sock_map_sk_is_suitable(sk) ||
734 sk->sk_state != TCP_ESTABLISHED) {
739 sock_map_sk_acquire(sk);
740 ret = sock_hash_update_common(map, key, sk, flags);
741 sock_map_sk_release(sk);
747 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
750 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
751 struct bpf_htab_elem *elem, *elem_next;
752 u32 hash, key_size = map->key_size;
753 struct hlist_head *head;
757 goto find_first_elem;
758 hash = sock_hash_bucket_hash(key, key_size);
759 head = &sock_hash_select_bucket(htab, hash)->head;
760 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
762 goto find_first_elem;
764 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
765 struct bpf_htab_elem, node);
767 memcpy(key_next, elem_next->key, key_size);
771 i = hash & (htab->buckets_num - 1);
774 for (; i < htab->buckets_num; i++) {
775 head = &sock_hash_select_bucket(htab, i)->head;
776 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
777 struct bpf_htab_elem, node);
779 memcpy(key_next, elem_next->key, key_size);
787 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
789 struct bpf_htab *htab;
793 if (!capable(CAP_NET_ADMIN))
794 return ERR_PTR(-EPERM);
795 if (attr->max_entries == 0 ||
796 attr->key_size == 0 ||
797 attr->value_size != 4 ||
798 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
799 return ERR_PTR(-EINVAL);
800 if (attr->key_size > MAX_BPF_STACK)
801 return ERR_PTR(-E2BIG);
803 htab = kzalloc(sizeof(*htab), GFP_USER);
805 return ERR_PTR(-ENOMEM);
807 bpf_map_init_from_attr(&htab->map, attr);
809 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
810 htab->elem_size = sizeof(struct bpf_htab_elem) +
811 round_up(htab->map.key_size, 8);
812 if (htab->buckets_num == 0 ||
813 htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
818 cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
819 (u64) htab->elem_size * htab->map.max_entries;
820 if (cost >= U32_MAX - PAGE_SIZE) {
825 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
826 sizeof(struct bpf_htab_bucket),
827 htab->map.numa_node);
828 if (!htab->buckets) {
833 for (i = 0; i < htab->buckets_num; i++) {
834 INIT_HLIST_HEAD(&htab->buckets[i].head);
835 raw_spin_lock_init(&htab->buckets[i].lock);
844 static void sock_hash_free(struct bpf_map *map)
846 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
847 struct bpf_htab_bucket *bucket;
848 struct bpf_htab_elem *elem;
849 struct hlist_node *node;
854 for (i = 0; i < htab->buckets_num; i++) {
855 bucket = sock_hash_select_bucket(htab, i);
856 raw_spin_lock_bh(&bucket->lock);
857 hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
858 hlist_del_rcu(&elem->node);
859 sock_map_unref(elem->sk, elem);
861 raw_spin_unlock_bh(&bucket->lock);
865 bpf_map_area_free(htab->buckets);
869 static void sock_hash_release_progs(struct bpf_map *map)
871 psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
874 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
875 struct bpf_map *, map, void *, key, u64, flags)
877 WARN_ON_ONCE(!rcu_read_lock_held());
879 if (likely(sock_map_sk_is_suitable(sops->sk) &&
880 sock_map_op_okay(sops)))
881 return sock_hash_update_common(map, key, sops->sk, flags);
885 const struct bpf_func_proto bpf_sock_hash_update_proto = {
886 .func = bpf_sock_hash_update,
889 .ret_type = RET_INTEGER,
890 .arg1_type = ARG_PTR_TO_CTX,
891 .arg2_type = ARG_CONST_MAP_PTR,
892 .arg3_type = ARG_PTR_TO_MAP_KEY,
893 .arg4_type = ARG_ANYTHING,
896 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
897 struct bpf_map *, map, void *, key, u64, flags)
899 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
901 if (unlikely(flags & ~(BPF_F_INGRESS)))
903 tcb->bpf.flags = flags;
904 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
905 if (!tcb->bpf.sk_redir)
910 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
911 .func = bpf_sk_redirect_hash,
913 .ret_type = RET_INTEGER,
914 .arg1_type = ARG_PTR_TO_CTX,
915 .arg2_type = ARG_CONST_MAP_PTR,
916 .arg3_type = ARG_PTR_TO_MAP_KEY,
917 .arg4_type = ARG_ANYTHING,
920 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
921 struct bpf_map *, map, void *, key, u64, flags)
923 if (unlikely(flags & ~(BPF_F_INGRESS)))
926 msg->sk_redir = __sock_hash_lookup_elem(map, key);
932 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
933 .func = bpf_msg_redirect_hash,
935 .ret_type = RET_INTEGER,
936 .arg1_type = ARG_PTR_TO_CTX,
937 .arg2_type = ARG_CONST_MAP_PTR,
938 .arg3_type = ARG_PTR_TO_MAP_KEY,
939 .arg4_type = ARG_ANYTHING,
942 const struct bpf_map_ops sock_hash_ops = {
943 .map_alloc = sock_hash_alloc,
944 .map_free = sock_hash_free,
945 .map_get_next_key = sock_hash_get_next_key,
946 .map_update_elem = sock_hash_update_elem,
947 .map_delete_elem = sock_hash_delete_elem,
948 .map_lookup_elem = sock_map_lookup,
949 .map_release_uref = sock_hash_release_progs,
950 .map_check_btf = map_check_no_btf,
953 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
955 switch (map->map_type) {
956 case BPF_MAP_TYPE_SOCKMAP:
957 return &container_of(map, struct bpf_stab, map)->progs;
958 case BPF_MAP_TYPE_SOCKHASH:
959 return &container_of(map, struct bpf_htab, map)->progs;
967 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
970 struct sk_psock_progs *progs = sock_map_progs(map);
976 case BPF_SK_MSG_VERDICT:
977 psock_set_prog(&progs->msg_parser, prog);
979 case BPF_SK_SKB_STREAM_PARSER:
980 psock_set_prog(&progs->skb_parser, prog);
982 case BPF_SK_SKB_STREAM_VERDICT:
983 psock_set_prog(&progs->skb_verdict, prog);
992 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
994 switch (link->map->map_type) {
995 case BPF_MAP_TYPE_SOCKMAP:
996 return sock_map_delete_from_link(link->map, sk,
998 case BPF_MAP_TYPE_SOCKHASH:
999 return sock_hash_delete_from_link(link->map, sk,