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);
245 sock_map_unref(sk, psk);
247 raw_spin_unlock_bh(&stab->lock);
250 bpf_map_area_free(stab->sks);
254 static void sock_map_release_progs(struct bpf_map *map)
256 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
259 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
261 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
263 WARN_ON_ONCE(!rcu_read_lock_held());
265 if (unlikely(key >= map->max_entries))
267 return READ_ONCE(stab->sks[key]);
270 static void *sock_map_lookup(struct bpf_map *map, void *key)
272 return ERR_PTR(-EOPNOTSUPP);
275 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
280 raw_spin_lock_bh(&stab->lock);
282 if (!sk_test || sk_test == sk)
284 raw_spin_unlock_bh(&stab->lock);
287 sock_map_unref(sk, psk);
291 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
294 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
296 __sock_map_delete(stab, sk, link_raw);
299 static int sock_map_delete_elem(struct bpf_map *map, void *key)
301 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
305 if (unlikely(i >= map->max_entries))
309 return __sock_map_delete(stab, NULL, psk);
312 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
314 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
315 u32 i = key ? *(u32 *)key : U32_MAX;
316 u32 *key_next = next;
318 if (i == stab->map.max_entries - 1)
320 if (i >= stab->map.max_entries)
327 static int sock_map_update_common(struct bpf_map *map, u32 idx,
328 struct sock *sk, u64 flags)
330 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
331 struct sk_psock_link *link;
332 struct sk_psock *psock;
336 WARN_ON_ONCE(!rcu_read_lock_held());
337 if (unlikely(flags > BPF_EXIST))
339 if (unlikely(idx >= map->max_entries))
342 link = sk_psock_init_link();
346 ret = sock_map_link(map, &stab->progs, sk);
350 psock = sk_psock(sk);
351 WARN_ON_ONCE(!psock);
353 raw_spin_lock_bh(&stab->lock);
354 osk = stab->sks[idx];
355 if (osk && flags == BPF_NOEXIST) {
358 } else if (!osk && flags == BPF_EXIST) {
363 sock_map_add_link(psock, link, map, &stab->sks[idx]);
366 sock_map_unref(osk, &stab->sks[idx]);
367 raw_spin_unlock_bh(&stab->lock);
370 raw_spin_unlock_bh(&stab->lock);
372 sk_psock_put(sk, psock);
374 sk_psock_free_link(link);
378 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
380 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
381 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB;
384 static bool sock_map_sk_is_suitable(const struct sock *sk)
386 return sk->sk_type == SOCK_STREAM &&
387 sk->sk_protocol == IPPROTO_TCP;
390 static int sock_map_update_elem(struct bpf_map *map, void *key,
391 void *value, u64 flags)
393 u32 ufd = *(u32 *)value;
394 u32 idx = *(u32 *)key;
399 sock = sockfd_lookup(ufd, &ret);
407 if (!sock_map_sk_is_suitable(sk) ||
408 sk->sk_state != TCP_ESTABLISHED) {
413 sock_map_sk_acquire(sk);
414 ret = sock_map_update_common(map, idx, sk, flags);
415 sock_map_sk_release(sk);
421 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
422 struct bpf_map *, map, void *, key, u64, flags)
424 WARN_ON_ONCE(!rcu_read_lock_held());
426 if (likely(sock_map_sk_is_suitable(sops->sk) &&
427 sock_map_op_okay(sops)))
428 return sock_map_update_common(map, *(u32 *)key, sops->sk,
433 const struct bpf_func_proto bpf_sock_map_update_proto = {
434 .func = bpf_sock_map_update,
437 .ret_type = RET_INTEGER,
438 .arg1_type = ARG_PTR_TO_CTX,
439 .arg2_type = ARG_CONST_MAP_PTR,
440 .arg3_type = ARG_PTR_TO_MAP_KEY,
441 .arg4_type = ARG_ANYTHING,
444 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
445 struct bpf_map *, map, u32, key, u64, flags)
447 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
449 if (unlikely(flags & ~(BPF_F_INGRESS)))
451 tcb->bpf.flags = flags;
452 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
453 if (!tcb->bpf.sk_redir)
458 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
459 .func = bpf_sk_redirect_map,
461 .ret_type = RET_INTEGER,
462 .arg1_type = ARG_PTR_TO_CTX,
463 .arg2_type = ARG_CONST_MAP_PTR,
464 .arg3_type = ARG_ANYTHING,
465 .arg4_type = ARG_ANYTHING,
468 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
469 struct bpf_map *, map, u32, key, u64, flags)
471 if (unlikely(flags & ~(BPF_F_INGRESS)))
474 msg->sk_redir = __sock_map_lookup_elem(map, key);
480 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
481 .func = bpf_msg_redirect_map,
483 .ret_type = RET_INTEGER,
484 .arg1_type = ARG_PTR_TO_CTX,
485 .arg2_type = ARG_CONST_MAP_PTR,
486 .arg3_type = ARG_ANYTHING,
487 .arg4_type = ARG_ANYTHING,
490 const struct bpf_map_ops sock_map_ops = {
491 .map_alloc = sock_map_alloc,
492 .map_free = sock_map_free,
493 .map_get_next_key = sock_map_get_next_key,
494 .map_update_elem = sock_map_update_elem,
495 .map_delete_elem = sock_map_delete_elem,
496 .map_lookup_elem = sock_map_lookup,
497 .map_release_uref = sock_map_release_progs,
498 .map_check_btf = map_check_no_btf,
501 struct bpf_htab_elem {
505 struct hlist_node node;
509 struct bpf_htab_bucket {
510 struct hlist_head head;
516 struct bpf_htab_bucket *buckets;
519 struct sk_psock_progs progs;
523 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
525 return jhash(key, len, 0);
528 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
531 return &htab->buckets[hash & (htab->buckets_num - 1)];
534 static struct bpf_htab_elem *
535 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
538 struct bpf_htab_elem *elem;
540 hlist_for_each_entry_rcu(elem, head, node) {
541 if (elem->hash == hash &&
542 !memcmp(&elem->key, key, key_size))
549 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
551 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
552 u32 key_size = map->key_size, hash;
553 struct bpf_htab_bucket *bucket;
554 struct bpf_htab_elem *elem;
556 WARN_ON_ONCE(!rcu_read_lock_held());
558 hash = sock_hash_bucket_hash(key, key_size);
559 bucket = sock_hash_select_bucket(htab, hash);
560 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
562 return elem ? elem->sk : NULL;
565 static void sock_hash_free_elem(struct bpf_htab *htab,
566 struct bpf_htab_elem *elem)
568 atomic_dec(&htab->count);
569 kfree_rcu(elem, rcu);
572 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
575 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
576 struct bpf_htab_elem *elem_probe, *elem = link_raw;
577 struct bpf_htab_bucket *bucket;
579 WARN_ON_ONCE(!rcu_read_lock_held());
580 bucket = sock_hash_select_bucket(htab, elem->hash);
582 /* elem may be deleted in parallel from the map, but access here
583 * is okay since it's going away only after RCU grace period.
584 * However, we need to check whether it's still present.
586 raw_spin_lock_bh(&bucket->lock);
587 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
588 elem->key, map->key_size);
589 if (elem_probe && elem_probe == elem) {
590 hlist_del_rcu(&elem->node);
591 sock_map_unref(elem->sk, elem);
592 sock_hash_free_elem(htab, elem);
594 raw_spin_unlock_bh(&bucket->lock);
597 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
599 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
600 u32 hash, key_size = map->key_size;
601 struct bpf_htab_bucket *bucket;
602 struct bpf_htab_elem *elem;
605 hash = sock_hash_bucket_hash(key, key_size);
606 bucket = sock_hash_select_bucket(htab, hash);
608 raw_spin_lock_bh(&bucket->lock);
609 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
611 hlist_del_rcu(&elem->node);
612 sock_map_unref(elem->sk, elem);
613 sock_hash_free_elem(htab, elem);
616 raw_spin_unlock_bh(&bucket->lock);
620 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
621 void *key, u32 key_size,
622 u32 hash, struct sock *sk,
623 struct bpf_htab_elem *old)
625 struct bpf_htab_elem *new;
627 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
629 atomic_dec(&htab->count);
630 return ERR_PTR(-E2BIG);
634 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
635 htab->map.numa_node);
637 atomic_dec(&htab->count);
638 return ERR_PTR(-ENOMEM);
640 memcpy(new->key, key, key_size);
646 static int sock_hash_update_common(struct bpf_map *map, void *key,
647 struct sock *sk, u64 flags)
649 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
650 u32 key_size = map->key_size, hash;
651 struct bpf_htab_elem *elem, *elem_new;
652 struct bpf_htab_bucket *bucket;
653 struct sk_psock_link *link;
654 struct sk_psock *psock;
657 WARN_ON_ONCE(!rcu_read_lock_held());
658 if (unlikely(flags > BPF_EXIST))
661 link = sk_psock_init_link();
665 ret = sock_map_link(map, &htab->progs, sk);
669 psock = sk_psock(sk);
670 WARN_ON_ONCE(!psock);
672 hash = sock_hash_bucket_hash(key, key_size);
673 bucket = sock_hash_select_bucket(htab, hash);
675 raw_spin_lock_bh(&bucket->lock);
676 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
677 if (elem && flags == BPF_NOEXIST) {
680 } else if (!elem && flags == BPF_EXIST) {
685 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
686 if (IS_ERR(elem_new)) {
687 ret = PTR_ERR(elem_new);
691 sock_map_add_link(psock, link, map, elem_new);
692 /* Add new element to the head of the list, so that
693 * concurrent search will find it before old elem.
695 hlist_add_head_rcu(&elem_new->node, &bucket->head);
697 hlist_del_rcu(&elem->node);
698 sock_map_unref(elem->sk, elem);
699 sock_hash_free_elem(htab, elem);
701 raw_spin_unlock_bh(&bucket->lock);
704 raw_spin_unlock_bh(&bucket->lock);
705 sk_psock_put(sk, psock);
707 sk_psock_free_link(link);
711 static int sock_hash_update_elem(struct bpf_map *map, void *key,
712 void *value, u64 flags)
714 u32 ufd = *(u32 *)value;
719 sock = sockfd_lookup(ufd, &ret);
727 if (!sock_map_sk_is_suitable(sk) ||
728 sk->sk_state != TCP_ESTABLISHED) {
733 sock_map_sk_acquire(sk);
734 ret = sock_hash_update_common(map, key, sk, flags);
735 sock_map_sk_release(sk);
741 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
744 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
745 struct bpf_htab_elem *elem, *elem_next;
746 u32 hash, key_size = map->key_size;
747 struct hlist_head *head;
751 goto find_first_elem;
752 hash = sock_hash_bucket_hash(key, key_size);
753 head = &sock_hash_select_bucket(htab, hash)->head;
754 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
756 goto find_first_elem;
758 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
759 struct bpf_htab_elem, node);
761 memcpy(key_next, elem_next->key, key_size);
765 i = hash & (htab->buckets_num - 1);
768 for (; i < htab->buckets_num; i++) {
769 head = &sock_hash_select_bucket(htab, i)->head;
770 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
771 struct bpf_htab_elem, node);
773 memcpy(key_next, elem_next->key, key_size);
781 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
783 struct bpf_htab *htab;
787 if (!capable(CAP_NET_ADMIN))
788 return ERR_PTR(-EPERM);
789 if (attr->max_entries == 0 ||
790 attr->key_size == 0 ||
791 attr->value_size != 4 ||
792 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
793 return ERR_PTR(-EINVAL);
794 if (attr->key_size > MAX_BPF_STACK)
795 return ERR_PTR(-E2BIG);
797 htab = kzalloc(sizeof(*htab), GFP_USER);
799 return ERR_PTR(-ENOMEM);
801 bpf_map_init_from_attr(&htab->map, attr);
803 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
804 htab->elem_size = sizeof(struct bpf_htab_elem) +
805 round_up(htab->map.key_size, 8);
806 if (htab->buckets_num == 0 ||
807 htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
812 cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
813 (u64) htab->elem_size * htab->map.max_entries;
814 if (cost >= U32_MAX - PAGE_SIZE) {
819 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
820 sizeof(struct bpf_htab_bucket),
821 htab->map.numa_node);
822 if (!htab->buckets) {
827 for (i = 0; i < htab->buckets_num; i++) {
828 INIT_HLIST_HEAD(&htab->buckets[i].head);
829 raw_spin_lock_init(&htab->buckets[i].lock);
838 static void sock_hash_free(struct bpf_map *map)
840 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
841 struct bpf_htab_bucket *bucket;
842 struct bpf_htab_elem *elem;
843 struct hlist_node *node;
848 for (i = 0; i < htab->buckets_num; i++) {
849 bucket = sock_hash_select_bucket(htab, i);
850 raw_spin_lock_bh(&bucket->lock);
851 hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
852 hlist_del_rcu(&elem->node);
853 sock_map_unref(elem->sk, elem);
855 raw_spin_unlock_bh(&bucket->lock);
859 bpf_map_area_free(htab->buckets);
863 static void sock_hash_release_progs(struct bpf_map *map)
865 psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
868 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
869 struct bpf_map *, map, void *, key, u64, flags)
871 WARN_ON_ONCE(!rcu_read_lock_held());
873 if (likely(sock_map_sk_is_suitable(sops->sk) &&
874 sock_map_op_okay(sops)))
875 return sock_hash_update_common(map, key, sops->sk, flags);
879 const struct bpf_func_proto bpf_sock_hash_update_proto = {
880 .func = bpf_sock_hash_update,
883 .ret_type = RET_INTEGER,
884 .arg1_type = ARG_PTR_TO_CTX,
885 .arg2_type = ARG_CONST_MAP_PTR,
886 .arg3_type = ARG_PTR_TO_MAP_KEY,
887 .arg4_type = ARG_ANYTHING,
890 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
891 struct bpf_map *, map, void *, key, u64, flags)
893 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
895 if (unlikely(flags & ~(BPF_F_INGRESS)))
897 tcb->bpf.flags = flags;
898 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
899 if (!tcb->bpf.sk_redir)
904 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
905 .func = bpf_sk_redirect_hash,
907 .ret_type = RET_INTEGER,
908 .arg1_type = ARG_PTR_TO_CTX,
909 .arg2_type = ARG_CONST_MAP_PTR,
910 .arg3_type = ARG_PTR_TO_MAP_KEY,
911 .arg4_type = ARG_ANYTHING,
914 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
915 struct bpf_map *, map, void *, key, u64, flags)
917 if (unlikely(flags & ~(BPF_F_INGRESS)))
920 msg->sk_redir = __sock_hash_lookup_elem(map, key);
926 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
927 .func = bpf_msg_redirect_hash,
929 .ret_type = RET_INTEGER,
930 .arg1_type = ARG_PTR_TO_CTX,
931 .arg2_type = ARG_CONST_MAP_PTR,
932 .arg3_type = ARG_PTR_TO_MAP_KEY,
933 .arg4_type = ARG_ANYTHING,
936 const struct bpf_map_ops sock_hash_ops = {
937 .map_alloc = sock_hash_alloc,
938 .map_free = sock_hash_free,
939 .map_get_next_key = sock_hash_get_next_key,
940 .map_update_elem = sock_hash_update_elem,
941 .map_delete_elem = sock_hash_delete_elem,
942 .map_lookup_elem = sock_map_lookup,
943 .map_release_uref = sock_hash_release_progs,
944 .map_check_btf = map_check_no_btf,
947 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
949 switch (map->map_type) {
950 case BPF_MAP_TYPE_SOCKMAP:
951 return &container_of(map, struct bpf_stab, map)->progs;
952 case BPF_MAP_TYPE_SOCKHASH:
953 return &container_of(map, struct bpf_htab, map)->progs;
961 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
964 struct sk_psock_progs *progs = sock_map_progs(map);
970 case BPF_SK_MSG_VERDICT:
971 psock_set_prog(&progs->msg_parser, prog);
973 case BPF_SK_SKB_STREAM_PARSER:
974 psock_set_prog(&progs->skb_parser, prog);
976 case BPF_SK_SKB_STREAM_VERDICT:
977 psock_set_prog(&progs->skb_verdict, prog);
986 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
988 switch (link->map->map_type) {
989 case BPF_MAP_TYPE_SOCKMAP:
990 return sock_map_delete_from_link(link->map, sk,
992 case BPF_MAP_TYPE_SOCKHASH:
993 return sock_hash_delete_from_link(link->map, sk,