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 stab
->map
.pages
= round_up(cost
, PAGE_SIZE
) >> PAGE_SHIFT
;
53 err
= bpf_map_precharge_memlock(stab
->map
.pages
);
57 stab
->sks
= bpf_map_area_alloc(stab
->map
.max_entries
*
58 sizeof(struct sock
*),
68 int sock_map_get_from_fd(const union bpf_attr
*attr
, struct bpf_prog
*prog
)
70 u32 ufd
= attr
->target_fd
;
76 map
= __bpf_map_get(f
);
79 ret
= sock_map_prog_update(map
, prog
, attr
->attach_type
);
84 static void sock_map_sk_acquire(struct sock
*sk
)
85 __acquires(&sk
->sk_lock
.slock
)
92 static void sock_map_sk_release(struct sock
*sk
)
93 __releases(&sk
->sk_lock
.slock
)
100 static void sock_map_add_link(struct sk_psock
*psock
,
101 struct sk_psock_link
*link
,
102 struct bpf_map
*map
, void *link_raw
)
104 link
->link_raw
= link_raw
;
106 spin_lock_bh(&psock
->link_lock
);
107 list_add_tail(&link
->list
, &psock
->link
);
108 spin_unlock_bh(&psock
->link_lock
);
111 static void sock_map_del_link(struct sock
*sk
,
112 struct sk_psock
*psock
, void *link_raw
)
114 struct sk_psock_link
*link
, *tmp
;
115 bool strp_stop
= false;
117 spin_lock_bh(&psock
->link_lock
);
118 list_for_each_entry_safe(link
, tmp
, &psock
->link
, list
) {
119 if (link
->link_raw
== link_raw
) {
120 struct bpf_map
*map
= link
->map
;
121 struct bpf_stab
*stab
= container_of(map
, struct bpf_stab
,
123 if (psock
->parser
.enabled
&& stab
->progs
.skb_parser
)
125 list_del(&link
->list
);
126 sk_psock_free_link(link
);
129 spin_unlock_bh(&psock
->link_lock
);
131 write_lock_bh(&sk
->sk_callback_lock
);
132 sk_psock_stop_strp(sk
, psock
);
133 write_unlock_bh(&sk
->sk_callback_lock
);
137 static void sock_map_unref(struct sock
*sk
, void *link_raw
)
139 struct sk_psock
*psock
= sk_psock(sk
);
142 sock_map_del_link(sk
, psock
, link_raw
);
143 sk_psock_put(sk
, psock
);
147 static int sock_map_link(struct bpf_map
*map
, struct sk_psock_progs
*progs
,
150 struct bpf_prog
*msg_parser
, *skb_parser
, *skb_verdict
;
151 bool skb_progs
, sk_psock_is_new
= false;
152 struct sk_psock
*psock
;
155 skb_verdict
= READ_ONCE(progs
->skb_verdict
);
156 skb_parser
= READ_ONCE(progs
->skb_parser
);
157 skb_progs
= skb_parser
&& skb_verdict
;
159 skb_verdict
= bpf_prog_inc_not_zero(skb_verdict
);
160 if (IS_ERR(skb_verdict
))
161 return PTR_ERR(skb_verdict
);
162 skb_parser
= bpf_prog_inc_not_zero(skb_parser
);
163 if (IS_ERR(skb_parser
)) {
164 bpf_prog_put(skb_verdict
);
165 return PTR_ERR(skb_parser
);
169 msg_parser
= READ_ONCE(progs
->msg_parser
);
171 msg_parser
= bpf_prog_inc_not_zero(msg_parser
);
172 if (IS_ERR(msg_parser
)) {
173 ret
= PTR_ERR(msg_parser
);
178 psock
= sk_psock_get_checked(sk
);
180 ret
= PTR_ERR(psock
);
185 if ((msg_parser
&& READ_ONCE(psock
->progs
.msg_parser
)) ||
186 (skb_progs
&& READ_ONCE(psock
->progs
.skb_parser
))) {
187 sk_psock_put(sk
, psock
);
192 psock
= sk_psock_init(sk
, map
->numa_node
);
197 sk_psock_is_new
= true;
201 psock_set_prog(&psock
->progs
.msg_parser
, msg_parser
);
202 if (sk_psock_is_new
) {
203 ret
= tcp_bpf_init(sk
);
210 write_lock_bh(&sk
->sk_callback_lock
);
211 if (skb_progs
&& !psock
->parser
.enabled
) {
212 ret
= sk_psock_init_strp(sk
, psock
);
214 write_unlock_bh(&sk
->sk_callback_lock
);
217 psock_set_prog(&psock
->progs
.skb_verdict
, skb_verdict
);
218 psock_set_prog(&psock
->progs
.skb_parser
, skb_parser
);
219 sk_psock_start_strp(sk
, psock
);
221 write_unlock_bh(&sk
->sk_callback_lock
);
224 sk_psock_put(sk
, psock
);
227 bpf_prog_put(msg_parser
);
230 bpf_prog_put(skb_verdict
);
231 bpf_prog_put(skb_parser
);
236 static void sock_map_free(struct bpf_map
*map
)
238 struct bpf_stab
*stab
= container_of(map
, struct bpf_stab
, map
);
243 raw_spin_lock_bh(&stab
->lock
);
244 for (i
= 0; i
< stab
->map
.max_entries
; i
++) {
245 struct sock
**psk
= &stab
->sks
[i
];
248 sk
= xchg(psk
, NULL
);
250 sock_map_unref(sk
, psk
);
252 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
,
285 raw_spin_lock_bh(&stab
->lock
);
287 if (!sk_test
|| sk_test
== sk
)
289 raw_spin_unlock_bh(&stab
->lock
);
292 sock_map_unref(sk
, psk
);
296 static void sock_map_delete_from_link(struct bpf_map
*map
, struct sock
*sk
,
299 struct bpf_stab
*stab
= container_of(map
, struct bpf_stab
, map
);
301 __sock_map_delete(stab
, sk
, link_raw
);
304 static int sock_map_delete_elem(struct bpf_map
*map
, void *key
)
306 struct bpf_stab
*stab
= container_of(map
, struct bpf_stab
, map
);
310 if (unlikely(i
>= map
->max_entries
))
314 return __sock_map_delete(stab
, NULL
, psk
);
317 static int sock_map_get_next_key(struct bpf_map
*map
, void *key
, void *next
)
319 struct bpf_stab
*stab
= container_of(map
, struct bpf_stab
, map
);
320 u32 i
= key
? *(u32
*)key
: U32_MAX
;
321 u32
*key_next
= next
;
323 if (i
== stab
->map
.max_entries
- 1)
325 if (i
>= stab
->map
.max_entries
)
332 static int sock_map_update_common(struct bpf_map
*map
, u32 idx
,
333 struct sock
*sk
, u64 flags
)
335 struct bpf_stab
*stab
= container_of(map
, struct bpf_stab
, map
);
336 struct sk_psock_link
*link
;
337 struct sk_psock
*psock
;
341 WARN_ON_ONCE(!rcu_read_lock_held());
342 if (unlikely(flags
> BPF_EXIST
))
344 if (unlikely(idx
>= map
->max_entries
))
347 link
= sk_psock_init_link();
351 ret
= sock_map_link(map
, &stab
->progs
, sk
);
355 psock
= sk_psock(sk
);
356 WARN_ON_ONCE(!psock
);
358 raw_spin_lock_bh(&stab
->lock
);
359 osk
= stab
->sks
[idx
];
360 if (osk
&& flags
== BPF_NOEXIST
) {
363 } else if (!osk
&& flags
== BPF_EXIST
) {
368 sock_map_add_link(psock
, link
, map
, &stab
->sks
[idx
]);
371 sock_map_unref(osk
, &stab
->sks
[idx
]);
372 raw_spin_unlock_bh(&stab
->lock
);
375 raw_spin_unlock_bh(&stab
->lock
);
377 sk_psock_put(sk
, psock
);
379 sk_psock_free_link(link
);
383 static bool sock_map_op_okay(const struct bpf_sock_ops_kern
*ops
)
385 return ops
->op
== BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB
||
386 ops
->op
== BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB
;
389 static bool sock_map_sk_is_suitable(const struct sock
*sk
)
391 return sk
->sk_type
== SOCK_STREAM
&&
392 sk
->sk_protocol
== IPPROTO_TCP
;
395 static int sock_map_update_elem(struct bpf_map
*map
, void *key
,
396 void *value
, u64 flags
)
398 u32 ufd
= *(u32
*)value
;
399 u32 idx
= *(u32
*)key
;
404 sock
= sockfd_lookup(ufd
, &ret
);
412 if (!sock_map_sk_is_suitable(sk
) ||
413 sk
->sk_state
!= TCP_ESTABLISHED
) {
418 sock_map_sk_acquire(sk
);
419 ret
= sock_map_update_common(map
, idx
, sk
, flags
);
420 sock_map_sk_release(sk
);
426 BPF_CALL_4(bpf_sock_map_update
, struct bpf_sock_ops_kern
*, sops
,
427 struct bpf_map
*, map
, void *, key
, u64
, flags
)
429 WARN_ON_ONCE(!rcu_read_lock_held());
431 if (likely(sock_map_sk_is_suitable(sops
->sk
) &&
432 sock_map_op_okay(sops
)))
433 return sock_map_update_common(map
, *(u32
*)key
, sops
->sk
,
438 const struct bpf_func_proto bpf_sock_map_update_proto
= {
439 .func
= bpf_sock_map_update
,
442 .ret_type
= RET_INTEGER
,
443 .arg1_type
= ARG_PTR_TO_CTX
,
444 .arg2_type
= ARG_CONST_MAP_PTR
,
445 .arg3_type
= ARG_PTR_TO_MAP_KEY
,
446 .arg4_type
= ARG_ANYTHING
,
449 BPF_CALL_4(bpf_sk_redirect_map
, struct sk_buff
*, skb
,
450 struct bpf_map
*, map
, u32
, key
, u64
, flags
)
452 struct tcp_skb_cb
*tcb
= TCP_SKB_CB(skb
);
454 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
456 tcb
->bpf
.flags
= flags
;
457 tcb
->bpf
.sk_redir
= __sock_map_lookup_elem(map
, key
);
458 if (!tcb
->bpf
.sk_redir
)
463 const struct bpf_func_proto bpf_sk_redirect_map_proto
= {
464 .func
= bpf_sk_redirect_map
,
466 .ret_type
= RET_INTEGER
,
467 .arg1_type
= ARG_PTR_TO_CTX
,
468 .arg2_type
= ARG_CONST_MAP_PTR
,
469 .arg3_type
= ARG_ANYTHING
,
470 .arg4_type
= ARG_ANYTHING
,
473 BPF_CALL_4(bpf_msg_redirect_map
, struct sk_msg
*, msg
,
474 struct bpf_map
*, map
, u32
, key
, u64
, flags
)
476 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
479 msg
->sk_redir
= __sock_map_lookup_elem(map
, key
);
485 const struct bpf_func_proto bpf_msg_redirect_map_proto
= {
486 .func
= bpf_msg_redirect_map
,
488 .ret_type
= RET_INTEGER
,
489 .arg1_type
= ARG_PTR_TO_CTX
,
490 .arg2_type
= ARG_CONST_MAP_PTR
,
491 .arg3_type
= ARG_ANYTHING
,
492 .arg4_type
= ARG_ANYTHING
,
495 const struct bpf_map_ops sock_map_ops
= {
496 .map_alloc
= sock_map_alloc
,
497 .map_free
= sock_map_free
,
498 .map_get_next_key
= sock_map_get_next_key
,
499 .map_update_elem
= sock_map_update_elem
,
500 .map_delete_elem
= sock_map_delete_elem
,
501 .map_lookup_elem
= sock_map_lookup
,
502 .map_release_uref
= sock_map_release_progs
,
503 .map_check_btf
= map_check_no_btf
,
506 struct bpf_htab_elem
{
510 struct hlist_node node
;
514 struct bpf_htab_bucket
{
515 struct hlist_head head
;
521 struct bpf_htab_bucket
*buckets
;
524 struct sk_psock_progs progs
;
528 static inline u32
sock_hash_bucket_hash(const void *key
, u32 len
)
530 return jhash(key
, len
, 0);
533 static struct bpf_htab_bucket
*sock_hash_select_bucket(struct bpf_htab
*htab
,
536 return &htab
->buckets
[hash
& (htab
->buckets_num
- 1)];
539 static struct bpf_htab_elem
*
540 sock_hash_lookup_elem_raw(struct hlist_head
*head
, u32 hash
, void *key
,
543 struct bpf_htab_elem
*elem
;
545 hlist_for_each_entry_rcu(elem
, head
, node
) {
546 if (elem
->hash
== hash
&&
547 !memcmp(&elem
->key
, key
, key_size
))
554 static struct sock
*__sock_hash_lookup_elem(struct bpf_map
*map
, void *key
)
556 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
557 u32 key_size
= map
->key_size
, hash
;
558 struct bpf_htab_bucket
*bucket
;
559 struct bpf_htab_elem
*elem
;
561 WARN_ON_ONCE(!rcu_read_lock_held());
563 hash
= sock_hash_bucket_hash(key
, key_size
);
564 bucket
= sock_hash_select_bucket(htab
, hash
);
565 elem
= sock_hash_lookup_elem_raw(&bucket
->head
, hash
, key
, key_size
);
567 return elem
? elem
->sk
: NULL
;
570 static void sock_hash_free_elem(struct bpf_htab
*htab
,
571 struct bpf_htab_elem
*elem
)
573 atomic_dec(&htab
->count
);
574 kfree_rcu(elem
, rcu
);
577 static void sock_hash_delete_from_link(struct bpf_map
*map
, struct sock
*sk
,
580 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
581 struct bpf_htab_elem
*elem_probe
, *elem
= link_raw
;
582 struct bpf_htab_bucket
*bucket
;
584 WARN_ON_ONCE(!rcu_read_lock_held());
585 bucket
= sock_hash_select_bucket(htab
, elem
->hash
);
587 /* elem may be deleted in parallel from the map, but access here
588 * is okay since it's going away only after RCU grace period.
589 * However, we need to check whether it's still present.
591 raw_spin_lock_bh(&bucket
->lock
);
592 elem_probe
= sock_hash_lookup_elem_raw(&bucket
->head
, elem
->hash
,
593 elem
->key
, map
->key_size
);
594 if (elem_probe
&& elem_probe
== elem
) {
595 hlist_del_rcu(&elem
->node
);
596 sock_map_unref(elem
->sk
, elem
);
597 sock_hash_free_elem(htab
, elem
);
599 raw_spin_unlock_bh(&bucket
->lock
);
602 static int sock_hash_delete_elem(struct bpf_map
*map
, void *key
)
604 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
605 u32 hash
, key_size
= map
->key_size
;
606 struct bpf_htab_bucket
*bucket
;
607 struct bpf_htab_elem
*elem
;
610 hash
= sock_hash_bucket_hash(key
, key_size
);
611 bucket
= sock_hash_select_bucket(htab
, hash
);
613 raw_spin_lock_bh(&bucket
->lock
);
614 elem
= sock_hash_lookup_elem_raw(&bucket
->head
, hash
, key
, key_size
);
616 hlist_del_rcu(&elem
->node
);
617 sock_map_unref(elem
->sk
, elem
);
618 sock_hash_free_elem(htab
, elem
);
621 raw_spin_unlock_bh(&bucket
->lock
);
625 static struct bpf_htab_elem
*sock_hash_alloc_elem(struct bpf_htab
*htab
,
626 void *key
, u32 key_size
,
627 u32 hash
, struct sock
*sk
,
628 struct bpf_htab_elem
*old
)
630 struct bpf_htab_elem
*new;
632 if (atomic_inc_return(&htab
->count
) > htab
->map
.max_entries
) {
634 atomic_dec(&htab
->count
);
635 return ERR_PTR(-E2BIG
);
639 new = kmalloc_node(htab
->elem_size
, GFP_ATOMIC
| __GFP_NOWARN
,
640 htab
->map
.numa_node
);
642 atomic_dec(&htab
->count
);
643 return ERR_PTR(-ENOMEM
);
645 memcpy(new->key
, key
, key_size
);
651 static int sock_hash_update_common(struct bpf_map
*map
, void *key
,
652 struct sock
*sk
, u64 flags
)
654 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
655 u32 key_size
= map
->key_size
, hash
;
656 struct bpf_htab_elem
*elem
, *elem_new
;
657 struct bpf_htab_bucket
*bucket
;
658 struct sk_psock_link
*link
;
659 struct sk_psock
*psock
;
662 WARN_ON_ONCE(!rcu_read_lock_held());
663 if (unlikely(flags
> BPF_EXIST
))
666 link
= sk_psock_init_link();
670 ret
= sock_map_link(map
, &htab
->progs
, sk
);
674 psock
= sk_psock(sk
);
675 WARN_ON_ONCE(!psock
);
677 hash
= sock_hash_bucket_hash(key
, key_size
);
678 bucket
= sock_hash_select_bucket(htab
, hash
);
680 raw_spin_lock_bh(&bucket
->lock
);
681 elem
= sock_hash_lookup_elem_raw(&bucket
->head
, hash
, key
, key_size
);
682 if (elem
&& flags
== BPF_NOEXIST
) {
685 } else if (!elem
&& flags
== BPF_EXIST
) {
690 elem_new
= sock_hash_alloc_elem(htab
, key
, key_size
, hash
, sk
, elem
);
691 if (IS_ERR(elem_new
)) {
692 ret
= PTR_ERR(elem_new
);
696 sock_map_add_link(psock
, link
, map
, elem_new
);
697 /* Add new element to the head of the list, so that
698 * concurrent search will find it before old elem.
700 hlist_add_head_rcu(&elem_new
->node
, &bucket
->head
);
702 hlist_del_rcu(&elem
->node
);
703 sock_map_unref(elem
->sk
, elem
);
704 sock_hash_free_elem(htab
, elem
);
706 raw_spin_unlock_bh(&bucket
->lock
);
709 raw_spin_unlock_bh(&bucket
->lock
);
710 sk_psock_put(sk
, psock
);
712 sk_psock_free_link(link
);
716 static int sock_hash_update_elem(struct bpf_map
*map
, void *key
,
717 void *value
, u64 flags
)
719 u32 ufd
= *(u32
*)value
;
724 sock
= sockfd_lookup(ufd
, &ret
);
732 if (!sock_map_sk_is_suitable(sk
) ||
733 sk
->sk_state
!= TCP_ESTABLISHED
) {
738 sock_map_sk_acquire(sk
);
739 ret
= sock_hash_update_common(map
, key
, sk
, flags
);
740 sock_map_sk_release(sk
);
746 static int sock_hash_get_next_key(struct bpf_map
*map
, void *key
,
749 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
750 struct bpf_htab_elem
*elem
, *elem_next
;
751 u32 hash
, key_size
= map
->key_size
;
752 struct hlist_head
*head
;
756 goto find_first_elem
;
757 hash
= sock_hash_bucket_hash(key
, key_size
);
758 head
= &sock_hash_select_bucket(htab
, hash
)->head
;
759 elem
= sock_hash_lookup_elem_raw(head
, hash
, key
, key_size
);
761 goto find_first_elem
;
763 elem_next
= hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem
->node
)),
764 struct bpf_htab_elem
, node
);
766 memcpy(key_next
, elem_next
->key
, key_size
);
770 i
= hash
& (htab
->buckets_num
- 1);
773 for (; i
< htab
->buckets_num
; i
++) {
774 head
= &sock_hash_select_bucket(htab
, i
)->head
;
775 elem_next
= hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head
)),
776 struct bpf_htab_elem
, node
);
778 memcpy(key_next
, elem_next
->key
, key_size
);
786 static struct bpf_map
*sock_hash_alloc(union bpf_attr
*attr
)
788 struct bpf_htab
*htab
;
792 if (!capable(CAP_NET_ADMIN
))
793 return ERR_PTR(-EPERM
);
794 if (attr
->max_entries
== 0 ||
795 attr
->key_size
== 0 ||
796 attr
->value_size
!= 4 ||
797 attr
->map_flags
& ~SOCK_CREATE_FLAG_MASK
)
798 return ERR_PTR(-EINVAL
);
799 if (attr
->key_size
> MAX_BPF_STACK
)
800 return ERR_PTR(-E2BIG
);
802 htab
= kzalloc(sizeof(*htab
), GFP_USER
);
804 return ERR_PTR(-ENOMEM
);
806 bpf_map_init_from_attr(&htab
->map
, attr
);
808 htab
->buckets_num
= roundup_pow_of_two(htab
->map
.max_entries
);
809 htab
->elem_size
= sizeof(struct bpf_htab_elem
) +
810 round_up(htab
->map
.key_size
, 8);
811 if (htab
->buckets_num
== 0 ||
812 htab
->buckets_num
> U32_MAX
/ sizeof(struct bpf_htab_bucket
)) {
817 cost
= (u64
) htab
->buckets_num
* sizeof(struct bpf_htab_bucket
) +
818 (u64
) htab
->elem_size
* htab
->map
.max_entries
;
819 if (cost
>= U32_MAX
- PAGE_SIZE
) {
824 htab
->buckets
= bpf_map_area_alloc(htab
->buckets_num
*
825 sizeof(struct bpf_htab_bucket
),
826 htab
->map
.numa_node
);
827 if (!htab
->buckets
) {
832 for (i
= 0; i
< htab
->buckets_num
; i
++) {
833 INIT_HLIST_HEAD(&htab
->buckets
[i
].head
);
834 raw_spin_lock_init(&htab
->buckets
[i
].lock
);
843 static void sock_hash_free(struct bpf_map
*map
)
845 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
846 struct bpf_htab_bucket
*bucket
;
847 struct bpf_htab_elem
*elem
;
848 struct hlist_node
*node
;
853 for (i
= 0; i
< htab
->buckets_num
; i
++) {
854 bucket
= sock_hash_select_bucket(htab
, i
);
855 raw_spin_lock_bh(&bucket
->lock
);
856 hlist_for_each_entry_safe(elem
, node
, &bucket
->head
, node
) {
857 hlist_del_rcu(&elem
->node
);
858 sock_map_unref(elem
->sk
, elem
);
860 raw_spin_unlock_bh(&bucket
->lock
);
864 bpf_map_area_free(htab
->buckets
);
868 static void sock_hash_release_progs(struct bpf_map
*map
)
870 psock_progs_drop(&container_of(map
, struct bpf_htab
, map
)->progs
);
873 BPF_CALL_4(bpf_sock_hash_update
, struct bpf_sock_ops_kern
*, sops
,
874 struct bpf_map
*, map
, void *, key
, u64
, flags
)
876 WARN_ON_ONCE(!rcu_read_lock_held());
878 if (likely(sock_map_sk_is_suitable(sops
->sk
) &&
879 sock_map_op_okay(sops
)))
880 return sock_hash_update_common(map
, key
, sops
->sk
, flags
);
884 const struct bpf_func_proto bpf_sock_hash_update_proto
= {
885 .func
= bpf_sock_hash_update
,
888 .ret_type
= RET_INTEGER
,
889 .arg1_type
= ARG_PTR_TO_CTX
,
890 .arg2_type
= ARG_CONST_MAP_PTR
,
891 .arg3_type
= ARG_PTR_TO_MAP_KEY
,
892 .arg4_type
= ARG_ANYTHING
,
895 BPF_CALL_4(bpf_sk_redirect_hash
, struct sk_buff
*, skb
,
896 struct bpf_map
*, map
, void *, key
, u64
, flags
)
898 struct tcp_skb_cb
*tcb
= TCP_SKB_CB(skb
);
900 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
902 tcb
->bpf
.flags
= flags
;
903 tcb
->bpf
.sk_redir
= __sock_hash_lookup_elem(map
, key
);
904 if (!tcb
->bpf
.sk_redir
)
909 const struct bpf_func_proto bpf_sk_redirect_hash_proto
= {
910 .func
= bpf_sk_redirect_hash
,
912 .ret_type
= RET_INTEGER
,
913 .arg1_type
= ARG_PTR_TO_CTX
,
914 .arg2_type
= ARG_CONST_MAP_PTR
,
915 .arg3_type
= ARG_PTR_TO_MAP_KEY
,
916 .arg4_type
= ARG_ANYTHING
,
919 BPF_CALL_4(bpf_msg_redirect_hash
, struct sk_msg
*, msg
,
920 struct bpf_map
*, map
, void *, key
, u64
, flags
)
922 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
925 msg
->sk_redir
= __sock_hash_lookup_elem(map
, key
);
931 const struct bpf_func_proto bpf_msg_redirect_hash_proto
= {
932 .func
= bpf_msg_redirect_hash
,
934 .ret_type
= RET_INTEGER
,
935 .arg1_type
= ARG_PTR_TO_CTX
,
936 .arg2_type
= ARG_CONST_MAP_PTR
,
937 .arg3_type
= ARG_PTR_TO_MAP_KEY
,
938 .arg4_type
= ARG_ANYTHING
,
941 const struct bpf_map_ops sock_hash_ops
= {
942 .map_alloc
= sock_hash_alloc
,
943 .map_free
= sock_hash_free
,
944 .map_get_next_key
= sock_hash_get_next_key
,
945 .map_update_elem
= sock_hash_update_elem
,
946 .map_delete_elem
= sock_hash_delete_elem
,
947 .map_lookup_elem
= sock_map_lookup
,
948 .map_release_uref
= sock_hash_release_progs
,
949 .map_check_btf
= map_check_no_btf
,
952 static struct sk_psock_progs
*sock_map_progs(struct bpf_map
*map
)
954 switch (map
->map_type
) {
955 case BPF_MAP_TYPE_SOCKMAP
:
956 return &container_of(map
, struct bpf_stab
, map
)->progs
;
957 case BPF_MAP_TYPE_SOCKHASH
:
958 return &container_of(map
, struct bpf_htab
, map
)->progs
;
966 int sock_map_prog_update(struct bpf_map
*map
, struct bpf_prog
*prog
,
969 struct sk_psock_progs
*progs
= sock_map_progs(map
);
975 case BPF_SK_MSG_VERDICT
:
976 psock_set_prog(&progs
->msg_parser
, prog
);
978 case BPF_SK_SKB_STREAM_PARSER
:
979 psock_set_prog(&progs
->skb_parser
, prog
);
981 case BPF_SK_SKB_STREAM_VERDICT
:
982 psock_set_prog(&progs
->skb_verdict
, prog
);
991 void sk_psock_unlink(struct sock
*sk
, struct sk_psock_link
*link
)
993 switch (link
->map
->map_type
) {
994 case BPF_MAP_TYPE_SOCKMAP
:
995 return sock_map_delete_from_link(link
->map
, sk
,
997 case BPF_MAP_TYPE_SOCKHASH
:
998 return sock_hash_delete_from_link(link
->map
, sk
,