Skip to content

Commit 6877cd3

Browse files
jsitnickiMartin KaFai Lau
authored andcommitted
bpf: Enable read/write access to skb metadata through a dynptr
Now that we can create a dynptr to skb metadata, make reads to the metadata area possible with bpf_dynptr_read() or through a bpf_dynptr_slice(), and make writes to the metadata area possible with bpf_dynptr_write() or through a bpf_dynptr_slice_rdwr(). Note that for cloned skbs which share data with the original, we limit the skb metadata dynptr to be read-only since we don't unclone on a bpf_dynptr_write to metadata. Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/20250814-skb-metadata-thru-dynptr-v7-2-8a39e636e0fb@cloudflare.com
1 parent 89d912e commit 6877cd3

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

include/linux/filter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,7 @@ int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
17841784
void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
17851785
void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
17861786
void *buf, unsigned long len, bool flush);
1787+
void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset);
17871788
#else /* CONFIG_NET */
17881789
static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
17891790
void *to, u32 len)
@@ -1818,6 +1819,11 @@ static inline void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, voi
18181819
unsigned long len, bool flush)
18191820
{
18201821
}
1822+
1823+
static inline void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset)
1824+
{
1825+
return NULL;
1826+
}
18211827
#endif /* CONFIG_NET */
18221828

18231829
#endif /* __LINUX_FILTER_H__ */

kernel/bpf/helpers.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,8 @@ static int __bpf_dynptr_read(void *dst, u32 len, const struct bpf_dynptr_kern *s
17811781
case BPF_DYNPTR_TYPE_XDP:
17821782
return __bpf_xdp_load_bytes(src->data, src->offset + offset, dst, len);
17831783
case BPF_DYNPTR_TYPE_SKB_META:
1784-
return -EOPNOTSUPP; /* not implemented */
1784+
memmove(dst, bpf_skb_meta_pointer(src->data, src->offset + offset), len);
1785+
return 0;
17851786
default:
17861787
WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
17871788
return -EFAULT;
@@ -1839,7 +1840,10 @@ int __bpf_dynptr_write(const struct bpf_dynptr_kern *dst, u32 offset, void *src,
18391840
return -EINVAL;
18401841
return __bpf_xdp_store_bytes(dst->data, dst->offset + offset, src, len);
18411842
case BPF_DYNPTR_TYPE_SKB_META:
1842-
return -EOPNOTSUPP; /* not implemented */
1843+
if (flags)
1844+
return -EINVAL;
1845+
memmove(bpf_skb_meta_pointer(dst->data, dst->offset + offset), src, len);
1846+
return 0;
18431847
default:
18441848
WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
18451849
return -EFAULT;
@@ -2716,7 +2720,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr *p, u32 offset,
27162720
return buffer__opt;
27172721
}
27182722
case BPF_DYNPTR_TYPE_SKB_META:
2719-
return NULL; /* not implemented */
2723+
return bpf_skb_meta_pointer(ptr->data, ptr->offset + offset);
27202724
default:
27212725
WARN_ONCE(true, "unknown dynptr type %d\n", type);
27222726
return NULL;

net/core/filter.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11990,6 +11990,16 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1199011990
return func;
1199111991
}
1199211992

11993+
/**
11994+
* bpf_skb_meta_pointer() - Gets a mutable pointer within the skb metadata area.
11995+
* @skb: socket buffer carrying the metadata
11996+
* @offset: offset into the metadata area, must be <= skb_metadata_len()
11997+
*/
11998+
void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset)
11999+
{
12000+
return skb_metadata_end(skb) - skb_metadata_len(skb) + offset;
12001+
}
12002+
1199312003
__bpf_kfunc_start_defs();
1199412004
__bpf_kfunc int bpf_dynptr_from_skb(struct __sk_buff *s, u64 flags,
1199512005
struct bpf_dynptr *ptr__uninit)
@@ -12017,6 +12027,9 @@ __bpf_kfunc int bpf_dynptr_from_skb(struct __sk_buff *s, u64 flags,
1201712027
* XDP context with bpf_xdp_adjust_meta(). Serves as an alternative to
1201812028
* &__sk_buff->data_meta.
1201912029
*
12030+
* If passed @skb_ is a clone which shares the data with the original, the
12031+
* dynptr will be read-only. This limitation may be lifted in the future.
12032+
*
1202012033
* Return:
1202112034
* * %0 - dynptr ready to use
1202212035
* * %-EINVAL - invalid flags, dynptr set to null
@@ -12034,6 +12047,9 @@ __bpf_kfunc int bpf_dynptr_from_skb_meta(struct __sk_buff *skb_, u64 flags,
1203412047

1203512048
bpf_dynptr_init(ptr, skb, BPF_DYNPTR_TYPE_SKB_META, 0, skb_metadata_len(skb));
1203612049

12050+
if (skb_cloned(skb))
12051+
bpf_dynptr_set_rdonly(ptr);
12052+
1203712053
return 0;
1203812054
}
1203912055

0 commit comments

Comments
 (0)