Skip to content

KFunc bpf_rdonly_cast

v6.2

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:

Example

Docs could be improved

This part of the docs is incomplete, contributions are very welcome