KFunc bpf_rdonly_cast
Definition
This kfunc tries to cast the object to a specified type.
The function returns the same obj
but with PTR_TO_BTF_ID with
btf_id. The verifier will ensure btf_id being a struct type.
Since the supported type cast may not reflect what the 'obj' represents, the returned btf_id is marked as PTR_UNTRUSTED, so the return value and subsequent pointer chasing cannot be used as helper/kfunc arguments.
void *bpf_rdonly_cast(const void *obj__ign, u32 btf_id__k)
signature changed
The signature of this kfunc has changed in v6.9. The previous signature was void *bpf_rdonly_cast(void *obj__ign, u32 btf_id__k)
weak ELF symbols can be used to support both versions.
Usage
This tries to support use case like below:
#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
where skb_end_pointer(SKB)
is a unsigned char *
and needs to
be casted to struct skb_shared_info *
.
Program types
The following program types can make use of this kfunc:
- BPF_PROG_TYPE_CGROUP_SKB
- BPF_PROG_TYPE_CGROUP_SOCK_ADDR
- BPF_PROG_TYPE_LSM
- BPF_PROG_TYPE_LWT_IN
- BPF_PROG_TYPE_LWT_OUT
- BPF_PROG_TYPE_LWT_SEG6LOCAL
- BPF_PROG_TYPE_LWT_XMIT
- BPF_PROG_TYPE_NETFILTER
- BPF_PROG_TYPE_SCHED_ACT
- BPF_PROG_TYPE_SCHED_CLS
- BPF_PROG_TYPE_SK_SKB
- BPF_PROG_TYPE_SOCKET_FILTER
- BPF_PROG_TYPE_STRUCT_OPS
- BPF_PROG_TYPE_SYSCALL
- BPF_PROG_TYPE_TRACING
- BPF_PROG_TYPE_XDP
Example
Docs could be improved
This part of the docs is incomplete, contributions are very welcome