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);
237 raw_spin_lock_bh(&stab->lock);
238 for (i = 0; i < stab->map.max_entries; i++) {
239 struct sock **psk = &stab->sks[i];
242 sk = xchg(psk, NULL);
246 sock_map_unref(sk, psk);
251 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)) {
424 sock_map_sk_acquire(sk);
425 if (sk->sk_state != TCP_ESTABLISHED)
428 ret = sock_map_update_common(map, idx, sk, flags);
429 sock_map_sk_release(sk);
435 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
436 struct bpf_map *, map, void *, key, u64, flags)
438 WARN_ON_ONCE(!rcu_read_lock_held());
440 if (likely(sock_map_sk_is_suitable(sops->sk) &&
441 sock_map_op_okay(sops)))
442 return sock_map_update_common(map, *(u32 *)key, sops->sk,
447 const struct bpf_func_proto bpf_sock_map_update_proto = {
448 .func = bpf_sock_map_update,
451 .ret_type = RET_INTEGER,
452 .arg1_type = ARG_PTR_TO_CTX,
453 .arg2_type = ARG_CONST_MAP_PTR,
454 .arg3_type = ARG_PTR_TO_MAP_KEY,
455 .arg4_type = ARG_ANYTHING,
458 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
459 struct bpf_map *, map, u32, key, u64, flags)
461 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
463 if (unlikely(flags & ~(BPF_F_INGRESS)))
465 tcb->bpf.flags = flags;
466 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
467 if (!tcb->bpf.sk_redir)
472 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
473 .func = bpf_sk_redirect_map,
475 .ret_type = RET_INTEGER,
476 .arg1_type = ARG_PTR_TO_CTX,
477 .arg2_type = ARG_CONST_MAP_PTR,
478 .arg3_type = ARG_ANYTHING,
479 .arg4_type = ARG_ANYTHING,
482 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
483 struct bpf_map *, map, u32, key, u64, flags)
485 if (unlikely(flags & ~(BPF_F_INGRESS)))
488 msg->sk_redir = __sock_map_lookup_elem(map, key);
494 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
495 .func = bpf_msg_redirect_map,
497 .ret_type = RET_INTEGER,
498 .arg1_type = ARG_PTR_TO_CTX,
499 .arg2_type = ARG_CONST_MAP_PTR,
500 .arg3_type = ARG_ANYTHING,
501 .arg4_type = ARG_ANYTHING,
504 const struct bpf_map_ops sock_map_ops = {
505 .map_alloc = sock_map_alloc,
506 .map_free = sock_map_free,
507 .map_get_next_key = sock_map_get_next_key,
508 .map_update_elem = sock_map_update_elem,
509 .map_delete_elem = sock_map_delete_elem,
510 .map_lookup_elem = sock_map_lookup,
511 .map_release_uref = sock_map_release_progs,
512 .map_check_btf = map_check_no_btf,
515 struct bpf_htab_elem {
519 struct hlist_node node;
523 struct bpf_htab_bucket {
524 struct hlist_head head;
530 struct bpf_htab_bucket *buckets;
533 struct sk_psock_progs progs;
537 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
539 return jhash(key, len, 0);
542 static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
545 return &htab->buckets[hash & (htab->buckets_num - 1)];
548 static struct bpf_htab_elem *
549 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
552 struct bpf_htab_elem *elem;
554 hlist_for_each_entry_rcu(elem, head, node) {
555 if (elem->hash == hash &&
556 !memcmp(&elem->key, key, key_size))
563 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
565 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
566 u32 key_size = map->key_size, hash;
567 struct bpf_htab_bucket *bucket;
568 struct bpf_htab_elem *elem;
570 WARN_ON_ONCE(!rcu_read_lock_held());
572 hash = sock_hash_bucket_hash(key, key_size);
573 bucket = sock_hash_select_bucket(htab, hash);
574 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
576 return elem ? elem->sk : NULL;
579 static void sock_hash_free_elem(struct bpf_htab *htab,
580 struct bpf_htab_elem *elem)
582 atomic_dec(&htab->count);
583 kfree_rcu(elem, rcu);
586 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
589 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
590 struct bpf_htab_elem *elem_probe, *elem = link_raw;
591 struct bpf_htab_bucket *bucket;
593 WARN_ON_ONCE(!rcu_read_lock_held());
594 bucket = sock_hash_select_bucket(htab, elem->hash);
596 /* elem may be deleted in parallel from the map, but access here
597 * is okay since it's going away only after RCU grace period.
598 * However, we need to check whether it's still present.
600 raw_spin_lock_bh(&bucket->lock);
601 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
602 elem->key, map->key_size);
603 if (elem_probe && elem_probe == elem) {
604 hlist_del_rcu(&elem->node);
605 sock_map_unref(elem->sk, elem);
606 sock_hash_free_elem(htab, elem);
608 raw_spin_unlock_bh(&bucket->lock);
611 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
613 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
614 u32 hash, key_size = map->key_size;
615 struct bpf_htab_bucket *bucket;
616 struct bpf_htab_elem *elem;
619 hash = sock_hash_bucket_hash(key, key_size);
620 bucket = sock_hash_select_bucket(htab, hash);
622 raw_spin_lock_bh(&bucket->lock);
623 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
625 hlist_del_rcu(&elem->node);
626 sock_map_unref(elem->sk, elem);
627 sock_hash_free_elem(htab, elem);
630 raw_spin_unlock_bh(&bucket->lock);
634 static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
635 void *key, u32 key_size,
636 u32 hash, struct sock *sk,
637 struct bpf_htab_elem *old)
639 struct bpf_htab_elem *new;
641 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
643 atomic_dec(&htab->count);
644 return ERR_PTR(-E2BIG);
648 new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
649 htab->map.numa_node);
651 atomic_dec(&htab->count);
652 return ERR_PTR(-ENOMEM);
654 memcpy(new->key, key, key_size);
660 static int sock_hash_update_common(struct bpf_map *map, void *key,
661 struct sock *sk, u64 flags)
663 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
664 struct inet_connection_sock *icsk = inet_csk(sk);
665 u32 key_size = map->key_size, hash;
666 struct bpf_htab_elem *elem, *elem_new;
667 struct bpf_htab_bucket *bucket;
668 struct sk_psock_link *link;
669 struct sk_psock *psock;
672 WARN_ON_ONCE(!rcu_read_lock_held());
673 if (unlikely(flags > BPF_EXIST))
675 if (unlikely(icsk->icsk_ulp_data))
678 link = sk_psock_init_link();
682 ret = sock_map_link(map, &htab->progs, sk);
686 psock = sk_psock(sk);
687 WARN_ON_ONCE(!psock);
689 hash = sock_hash_bucket_hash(key, key_size);
690 bucket = sock_hash_select_bucket(htab, hash);
692 raw_spin_lock_bh(&bucket->lock);
693 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
694 if (elem && flags == BPF_NOEXIST) {
697 } else if (!elem && flags == BPF_EXIST) {
702 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
703 if (IS_ERR(elem_new)) {
704 ret = PTR_ERR(elem_new);
708 sock_map_add_link(psock, link, map, elem_new);
709 /* Add new element to the head of the list, so that
710 * concurrent search will find it before old elem.
712 hlist_add_head_rcu(&elem_new->node, &bucket->head);
714 hlist_del_rcu(&elem->node);
715 sock_map_unref(elem->sk, elem);
716 sock_hash_free_elem(htab, elem);
718 raw_spin_unlock_bh(&bucket->lock);
721 raw_spin_unlock_bh(&bucket->lock);
722 sk_psock_put(sk, psock);
724 sk_psock_free_link(link);
728 static int sock_hash_update_elem(struct bpf_map *map, void *key,
729 void *value, u64 flags)
731 u32 ufd = *(u32 *)value;
736 sock = sockfd_lookup(ufd, &ret);
744 if (!sock_map_sk_is_suitable(sk)) {
749 sock_map_sk_acquire(sk);
750 if (sk->sk_state != TCP_ESTABLISHED)
753 ret = sock_hash_update_common(map, key, sk, flags);
754 sock_map_sk_release(sk);
760 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
763 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
764 struct bpf_htab_elem *elem, *elem_next;
765 u32 hash, key_size = map->key_size;
766 struct hlist_head *head;
770 goto find_first_elem;
771 hash = sock_hash_bucket_hash(key, key_size);
772 head = &sock_hash_select_bucket(htab, hash)->head;
773 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
775 goto find_first_elem;
777 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
778 struct bpf_htab_elem, node);
780 memcpy(key_next, elem_next->key, key_size);
784 i = hash & (htab->buckets_num - 1);
787 for (; i < htab->buckets_num; i++) {
788 head = &sock_hash_select_bucket(htab, i)->head;
789 elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
790 struct bpf_htab_elem, node);
792 memcpy(key_next, elem_next->key, key_size);
800 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
802 struct bpf_htab *htab;
806 if (!capable(CAP_NET_ADMIN))
807 return ERR_PTR(-EPERM);
808 if (attr->max_entries == 0 ||
809 attr->key_size == 0 ||
810 attr->value_size != 4 ||
811 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
812 return ERR_PTR(-EINVAL);
813 if (attr->key_size > MAX_BPF_STACK)
814 return ERR_PTR(-E2BIG);
816 htab = kzalloc(sizeof(*htab), GFP_USER);
818 return ERR_PTR(-ENOMEM);
820 bpf_map_init_from_attr(&htab->map, attr);
822 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
823 htab->elem_size = sizeof(struct bpf_htab_elem) +
824 round_up(htab->map.key_size, 8);
825 if (htab->buckets_num == 0 ||
826 htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
831 cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
832 (u64) htab->elem_size * htab->map.max_entries;
833 if (cost >= U32_MAX - PAGE_SIZE) {
838 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
839 sizeof(struct bpf_htab_bucket),
840 htab->map.numa_node);
841 if (!htab->buckets) {
846 for (i = 0; i < htab->buckets_num; i++) {
847 INIT_HLIST_HEAD(&htab->buckets[i].head);
848 raw_spin_lock_init(&htab->buckets[i].lock);
857 static void sock_hash_free(struct bpf_map *map)
859 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
860 struct bpf_htab_bucket *bucket;
861 struct bpf_htab_elem *elem;
862 struct hlist_node *node;
866 for (i = 0; i < htab->buckets_num; i++) {
867 bucket = sock_hash_select_bucket(htab, i);
868 raw_spin_lock_bh(&bucket->lock);
869 hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
870 hlist_del_rcu(&elem->node);
873 sock_map_unref(elem->sk, elem);
875 release_sock(elem->sk);
877 raw_spin_unlock_bh(&bucket->lock);
880 bpf_map_area_free(htab->buckets);
884 static void sock_hash_release_progs(struct bpf_map *map)
886 psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
889 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
890 struct bpf_map *, map, void *, key, u64, flags)
892 WARN_ON_ONCE(!rcu_read_lock_held());
894 if (likely(sock_map_sk_is_suitable(sops->sk) &&
895 sock_map_op_okay(sops)))
896 return sock_hash_update_common(map, key, sops->sk, flags);
900 const struct bpf_func_proto bpf_sock_hash_update_proto = {
901 .func = bpf_sock_hash_update,
904 .ret_type = RET_INTEGER,
905 .arg1_type = ARG_PTR_TO_CTX,
906 .arg2_type = ARG_CONST_MAP_PTR,
907 .arg3_type = ARG_PTR_TO_MAP_KEY,
908 .arg4_type = ARG_ANYTHING,
911 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
912 struct bpf_map *, map, void *, key, u64, flags)
914 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
916 if (unlikely(flags & ~(BPF_F_INGRESS)))
918 tcb->bpf.flags = flags;
919 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
920 if (!tcb->bpf.sk_redir)
925 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
926 .func = bpf_sk_redirect_hash,
928 .ret_type = RET_INTEGER,
929 .arg1_type = ARG_PTR_TO_CTX,
930 .arg2_type = ARG_CONST_MAP_PTR,
931 .arg3_type = ARG_PTR_TO_MAP_KEY,
932 .arg4_type = ARG_ANYTHING,
935 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
936 struct bpf_map *, map, void *, key, u64, flags)
938 if (unlikely(flags & ~(BPF_F_INGRESS)))
941 msg->sk_redir = __sock_hash_lookup_elem(map, key);
947 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
948 .func = bpf_msg_redirect_hash,
950 .ret_type = RET_INTEGER,
951 .arg1_type = ARG_PTR_TO_CTX,
952 .arg2_type = ARG_CONST_MAP_PTR,
953 .arg3_type = ARG_PTR_TO_MAP_KEY,
954 .arg4_type = ARG_ANYTHING,
957 const struct bpf_map_ops sock_hash_ops = {
958 .map_alloc = sock_hash_alloc,
959 .map_free = sock_hash_free,
960 .map_get_next_key = sock_hash_get_next_key,
961 .map_update_elem = sock_hash_update_elem,
962 .map_delete_elem = sock_hash_delete_elem,
963 .map_lookup_elem = sock_map_lookup,
964 .map_release_uref = sock_hash_release_progs,
965 .map_check_btf = map_check_no_btf,
968 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
970 switch (map->map_type) {
971 case BPF_MAP_TYPE_SOCKMAP:
972 return &container_of(map, struct bpf_stab, map)->progs;
973 case BPF_MAP_TYPE_SOCKHASH:
974 return &container_of(map, struct bpf_htab, map)->progs;
982 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
985 struct sk_psock_progs *progs = sock_map_progs(map);
991 case BPF_SK_MSG_VERDICT:
992 psock_set_prog(&progs->msg_parser, prog);
994 case BPF_SK_SKB_STREAM_PARSER:
995 psock_set_prog(&progs->skb_parser, prog);
997 case BPF_SK_SKB_STREAM_VERDICT:
998 psock_set_prog(&progs->skb_verdict, prog);
1007 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
1009 switch (link->map->map_type) {
1010 case BPF_MAP_TYPE_SOCKMAP:
1011 return sock_map_delete_from_link(link->map, sk,
1013 case BPF_MAP_TYPE_SOCKHASH:
1014 return sock_hash_delete_from_link(link->map, sk,