Skip to content

Commit c85c659

Browse files
jsitnickiKernel Patches Daemon
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(). Reviewed-by: Jesse Brandeburg <[email protected]> Signed-off-by: Jakub Sitnicki <[email protected]>
1 parent d586f38 commit c85c659

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11987,6 +11987,16 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1198711987
return func;
1198811988
}
1198911989

11990+
/**
11991+
* bpf_skb_meta_pointer() - Gets a mutable pointer within the skb metadata area.
11992+
* @skb: socket buffer carrying the metadata
11993+
* @offset: offset into the metadata area, must be <= skb_metadata_len()
11994+
*/
11995+
void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset)
11996+
{
11997+
return skb_metadata_end(skb) - skb_metadata_len(skb) + offset;
11998+
}
11999+
1199012000
__bpf_kfunc_start_defs();
1199112001
__bpf_kfunc int bpf_dynptr_from_skb(struct __sk_buff *s, u64 flags,
1199212002
struct bpf_dynptr *ptr__uninit)

0 commit comments

Comments
 (0)