Skip to content

Commit 97ede2b

Browse files
jsitnickiKernel Patches Daemon
authored andcommitted
net: Helper to move packet data and metadata after skb_push/pull
Lay groundwork for fixing BPF helpers available to TC(X) programs. When skb_push() or skb_pull() is called in a TC(X) ingress BPF program, the skb metadata must be kept in front of the MAC header. Otherwise, BPF programs using the __sk_buff->data_meta pseudo-pointer lose access to it. Introduce a helper that moves both metadata and a specified number of packet data bytes together, suitable as a drop-in replacement for memmove(). Signed-off-by: Jakub Sitnicki <[email protected]>
1 parent a41ab27 commit 97ede2b

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

include/linux/skbuff.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4561,6 +4561,80 @@ static inline void skb_metadata_clear(struct sk_buff *skb)
45614561
skb_metadata_set(skb, 0);
45624562
}
45634563

4564+
/**
4565+
* skb_data_move - Move packet data and metadata after skb_push() or skb_pull().
4566+
* @skb: packet to operate on
4567+
* @len: number of bytes pushed or pulled from &sk_buff->data
4568+
* @n: number of bytes to memmove() from pre-push/pull &sk_buff->data
4569+
*
4570+
* Moves both packet data (@n bytes) and metadata. Assumes metadata is located
4571+
* immediately before &sk_buff->data prior to the push/pull, and that sufficient
4572+
* headroom exists to hold it after an skb_push(). Otherwise, metadata is
4573+
* cleared and a one-time warning is issued.
4574+
*
4575+
* Use skb_postpull_data_move() or skb_postpush_data_move() instead of calling
4576+
* this helper directly.
4577+
*/
4578+
static inline void skb_data_move(struct sk_buff *skb, const int len,
4579+
const unsigned int n)
4580+
{
4581+
const u8 meta_len = skb_metadata_len(skb);
4582+
u8 *meta, *meta_end;
4583+
4584+
if (!len || (!n && !meta_len))
4585+
return;
4586+
4587+
if (!meta_len)
4588+
goto no_metadata;
4589+
4590+
meta_end = skb_metadata_end(skb);
4591+
meta = meta_end - meta_len;
4592+
4593+
if (WARN_ON_ONCE(meta_end + len != skb->data ||
4594+
meta_len > skb_headroom(skb))) {
4595+
skb_metadata_clear(skb);
4596+
goto no_metadata;
4597+
}
4598+
4599+
memmove(meta + len, meta, meta_len + n);
4600+
return;
4601+
4602+
no_metadata:
4603+
memmove(skb->data, skb->data - len, n);
4604+
}
4605+
4606+
/**
4607+
* skb_postpull_data_move - Move packet data and metadata after skb_pull().
4608+
* @skb: packet to operate on
4609+
* @len: number of bytes pulled from &sk_buff->data
4610+
* @n: number of bytes to memmove() from pre-pull &sk_buff->data
4611+
*
4612+
* See skb_data_move() for details.
4613+
*/
4614+
static inline void skb_postpull_data_move(struct sk_buff *skb,
4615+
const unsigned int len,
4616+
const unsigned int n)
4617+
{
4618+
DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
4619+
skb_data_move(skb, len, n);
4620+
}
4621+
4622+
/**
4623+
* skb_postpush_data_move - Move packet data and metadata after skb_push().
4624+
* @skb: packet to operate on
4625+
* @len: number of bytes pushed onto &sk_buff->data
4626+
* @n: number of bytes to memmove() from pre-push &sk_buff->data
4627+
*
4628+
* See skb_data_move() for details.
4629+
*/
4630+
static inline void skb_postpush_data_move(struct sk_buff *skb,
4631+
const unsigned int len,
4632+
const unsigned int n)
4633+
{
4634+
DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
4635+
skb_data_move(skb, -len, n);
4636+
}
4637+
45644638
struct sk_buff *skb_clone_sk(struct sk_buff *skb);
45654639

45664640
#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING

0 commit comments

Comments
 (0)