Skip to content

Commit 0daf434

Browse files
borkmanndavem330
authored andcommitted
bpf, net: add skb_mac_header_len helper
Add a small skb_mac_header_len() helper similarly as the skb_network_header_len() we have and replace open coded places in BPF's bpf_skb_change_proto() helper. Will also be used in upcoming work. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent a68491f commit 0daf434

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

include/linux/skbuff.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,11 @@ static inline int skb_mac_offset(const struct sk_buff *skb)
22062206
return skb_mac_header(skb) - skb->data;
22072207
}
22082208

2209+
static inline u32 skb_mac_header_len(const struct sk_buff *skb)
2210+
{
2211+
return skb->network_header - skb->mac_header;
2212+
}
2213+
22092214
static inline int skb_mac_header_was_set(const struct sk_buff *skb)
22102215
{
22112216
return skb->mac_header != (typeof(skb->mac_header))~0U;

net/core/filter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,7 @@ static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
20122012
static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
20132013
{
20142014
const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2015-
u32 off = skb->network_header - skb->mac_header;
2015+
u32 off = skb_mac_header_len(skb);
20162016
int ret;
20172017

20182018
ret = skb_cow(skb, len_diff);
@@ -2048,7 +2048,7 @@ static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
20482048
static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
20492049
{
20502050
const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2051-
u32 off = skb->network_header - skb->mac_header;
2051+
u32 off = skb_mac_header_len(skb);
20522052
int ret;
20532053

20542054
ret = skb_unclone(skb, GFP_ATOMIC);

0 commit comments

Comments
 (0)