net: skb_reset_mac_len() must check if mac_header was set

Recent discussions show that skb_reset_mac_len() should be more careful.

We expect the MAC header being set.

If not, clear skb->mac_len and fire a warning for CONFIG_DEBUG_NET=y builds.

If after investigations we find that not having a MAC header was okay,
we can remove the warning.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Link: https://lore.kernel.org/netdev/CANn89iJZGH+yEfJxfPWa3Hm7jxb-aeY2Up4HufmLMnVuQXt38A@mail.gmail.com/T/
Cc: En-Wei Wu <en-wei.wu@canonical.com>
Reviewed-by: Joe Damato <jdamato@fastly.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Link: https://patch.msgid.link/20241105174403.850330-2-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Eric Dumazet 2024-11-05 17:43:57 +00:00 committed by Jakub Kicinski
parent 3545f9b72f
commit 1e4033b53d
1 changed files with 11 additions and 6 deletions

View File

@ -2909,9 +2909,19 @@ static inline void skb_reset_inner_headers(struct sk_buff *skb)
skb->inner_transport_header = skb->transport_header; skb->inner_transport_header = skb->transport_header;
} }
static inline int skb_mac_header_was_set(const struct sk_buff *skb)
{
return skb->mac_header != (typeof(skb->mac_header))~0U;
}
static inline void skb_reset_mac_len(struct sk_buff *skb) static inline void skb_reset_mac_len(struct sk_buff *skb)
{ {
skb->mac_len = skb->network_header - skb->mac_header; if (!skb_mac_header_was_set(skb)) {
DEBUG_NET_WARN_ON_ONCE(1);
skb->mac_len = 0;
} else {
skb->mac_len = skb->network_header - skb->mac_header;
}
} }
static inline unsigned char *skb_inner_transport_header(const struct sk_buff static inline unsigned char *skb_inner_transport_header(const struct sk_buff
@ -3014,11 +3024,6 @@ static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
skb->network_header += offset; skb->network_header += offset;
} }
static inline int skb_mac_header_was_set(const struct sk_buff *skb)
{
return skb->mac_header != (typeof(skb->mac_header))~0U;
}
static inline unsigned char *skb_mac_header(const struct sk_buff *skb) static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
{ {
DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb)); DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb));