From: Luxiao Xu erspan_build_header() and erspan_build_header_v2() unconditionally read the inner IPv4 tos or IPv6 traffic-class field based on the outer tunnel address family flag (is_ipv4), and dereference eth->h_proto and the following 802.1Q TCI without verifying frame length or linear data. In addition, ip6erspan_tunnel_xmit() and erspan_xmit() rely on pskb_inet_may_pull(), which requests zero network-layer bytes for ETH_P_8021Q/ETH_P_8021AD frames. Short or truncated VLAN frames can therefore enter ERSPAN transmit and reach downstream ip6_tnl_xmit(), ip_tunnel_xmit(), and ip_tunnel_get_dsfield(), which call skb_protocol(skb, true) after GRE and ERSPAN headers have been pushed. Because skb_protocol(skb, true) walks VLAN tags from skb->data instead of skb_mac_header(skb), those helpers read into the pushed GRE/ERSPAN headers and inner MAC bytes and can reclassify a short non-IP VLAN frame as ETH_P_IP or ETH_P_IPV6, reading unpulled bytes past skb->len. Fix the full transmit chain: - Add erspan_xmit_prepare() in include/net/erspan.h and use it in ip6erspan_tunnel_xmit() and erspan_xmit() to ensure ETH_HLEN bytes are linear, walk VLAN tags from ETH_HLEN with skb->mac_len cleared, reject truncated VLAN tags, and pull any inner IPv4/IPv6 base header before updating the network header. - Add erspan_get_tos() and erspan_get_vlan_tci() using skb_header_pointer() so ERSPAN header construction safely handles non-IP, VLAN-tagged, and non-linear skbs, and drop the obsolete is_ipv4 parameter from both helpers and their callers. - Add ip_tunnel_payload_protocol() in include/net/ip_tunnels.h using skb_mac_offset(skb) for ARPHRD_ETHER devices and use it in ip6_tnl_xmit(), ip_tunnel_xmit(), and ip_tunnel_get_dsfield(), while guarding ip6_tnl_xmit() hop-limit inheritance with skb_header_pointer(). Note: Anton Danilov's earlier ip6_gre patchset (v6 message-id 20261008012554.202925-1-littlesmilingcloud@gmail.com) targeted ip6gre_tunnel_xmit() VLAN length checks and explicitly left the pre-existing ERSPAN header builder and downstream tunnel handling for follow-up work; this patch addresses that follow-up independently. Fixes: 84e54fe0a5ea ("gre: introduce native tunnel support for ERSPAN") Fixes: f551c91de262 ("net: erspan: introduce erspan v2 for ip_gre") Fixes: 7ae29fd1be43 ("ip_tunnel: allow to inherit from VLAN encapsulated IP") Fixes: b09ab9c92e50 ("ip6_tunnel: allow to inherit from VLAN encapsulated IP") Cc: stable@vger.kernel.org Reported-by: VEGA Reported-by: Sashiko Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261004145205.226974-1-littlesmilingcloud@gmail.com Signed-off-by: Luxiao Xu Signed-off-by: Ren Wei --- include/net/erspan.h | 119 +++++++++++++++++++++++++++++---------- include/net/ip_tunnels.h | 13 ++++- net/ipv4/ip_gre.c | 15 ++--- net/ipv4/ip_tunnel.c | 2 +- net/ipv6/ip6_gre.c | 13 ++--- net/ipv6/ip6_tunnel.c | 24 +++++--- 6 files changed, 130 insertions(+), 56 deletions(-) diff --git a/include/net/erspan.h b/include/net/erspan.h index c6209e7b6c96..04764437ea7b 100644 --- a/include/net/erspan.h +++ b/include/net/erspan.h @@ -60,7 +60,10 @@ #include #include +#include #include +#include +#include #include #define ERSPAN_VERSION 0x1 /* ERSPAN type II */ @@ -170,35 +173,103 @@ static inline u8 tos_to_cos(u8 tos) return cos; } -static inline void erspan_build_header(struct sk_buff *skb, - u32 id, u32 index, - bool truncate, bool is_ipv4) +static inline bool erspan_xmit_prepare(struct sk_buff *skb) +{ + __be16 type = skb->protocol; + int maclen = ETH_HLEN; + int nhlen = 0; + + if (!pskb_may_pull(skb, ETH_HLEN)) + return false; + + skb_reset_mac_header(skb); + + if (eth_type_vlan(type)) { + skb->mac_len = 0; + type = __vlan_get_protocol(skb, type, &maclen); + if (!type) + return false; + + switch (type) { +#if IS_ENABLED(CONFIG_IPV6) + case htons(ETH_P_IPV6): + nhlen = sizeof(struct ipv6hdr); + break; +#endif + case htons(ETH_P_IP): + nhlen = sizeof(struct iphdr); + break; + } + + if (!pskb_may_pull(skb, maclen + nhlen)) + return false; + + skb_set_network_header(skb, maclen); + return true; + } + + return pskb_inet_may_pull(skb); +} + +static inline u8 erspan_get_tos(const struct sk_buff *skb) +{ + struct ipv6hdr _ip6h, *ip6h; + struct iphdr _iph, *iph; + __be16 proto; + int nhoff; + + nhoff = skb_network_offset(skb); + if (nhoff < 0) + return 0; + + proto = eth_type_vlan(skb->protocol) ? + __vlan_get_protocol(skb, skb->protocol, NULL) : skb->protocol; + + switch (proto) { + case htons(ETH_P_IP): + iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); + return iph ? iph->tos : 0; + case htons(ETH_P_IPV6): + ip6h = skb_header_pointer(skb, nhoff, sizeof(_ip6h), &_ip6h); + return ip6h ? ipv6_get_dsfield(ip6h) : 0; + default: + return 0; + } +} + +static inline bool erspan_get_vlan_tci(const struct sk_buff *skb, + u16 *vlan_tci) { - struct ethhdr *eth = (struct ethhdr *)skb->data; - enum erspan_encap_type enc_type; - struct erspan_base_hdr *ershdr; struct qtag_prefix { __be16 eth_type; __be16 tci; - } *qp; + } *qp, _qp; + + qp = skb_header_pointer(skb, 2 * ETH_ALEN, sizeof(_qp), &_qp); + if (qp && qp->eth_type == htons(ETH_P_8021Q)) { + *vlan_tci = ntohs(qp->tci); + return true; + } + + return false; +} + +static inline void erspan_build_header(struct sk_buff *skb, + u32 id, u32 index, bool truncate) +{ + enum erspan_encap_type enc_type = ERSPAN_ENCAP_NOVLAN; + struct erspan_base_hdr *ershdr; u16 vlan_tci = 0; u8 tos; __be32 *idx; - tos = is_ipv4 ? ip_hdr(skb)->tos : - (ipv6_hdr(skb)->priority << 4) + - (ipv6_hdr(skb)->flow_lbl[0] >> 4); - - enc_type = ERSPAN_ENCAP_NOVLAN; + tos = erspan_get_tos(skb); /* If mirrored packet has vlan tag, extract tci and * preserve vlan header in the mirrored frame. */ - if (eth->h_proto == htons(ETH_P_8021Q)) { - qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN); - vlan_tci = ntohs(qp->tci); + if (erspan_get_vlan_tci(skb, &vlan_tci)) enc_type = ERSPAN_ENCAP_INFRAME; - } skb_push(skb, sizeof(*ershdr) + ERSPAN_V1_MDSIZE); ershdr = (struct erspan_base_hdr *)skb->data; @@ -266,32 +337,22 @@ static inline u8 erspan_detect_bso(struct sk_buff *skb) static inline void erspan_build_header_v2(struct sk_buff *skb, u32 id, u8 direction, u16 hwid, - bool truncate, bool is_ipv4) + bool truncate) { - struct ethhdr *eth = (struct ethhdr *)skb->data; struct erspan_base_hdr *ershdr; struct erspan_md2 *md2; - struct qtag_prefix { - __be16 eth_type; - __be16 tci; - } *qp; u16 vlan_tci = 0; u8 gra = 0; /* 100 usec */ u8 bso = 0; /* Bad/Short/Oversized */ u8 sgt = 0; u8 tos; - tos = is_ipv4 ? ip_hdr(skb)->tos : - (ipv6_hdr(skb)->priority << 4) + - (ipv6_hdr(skb)->flow_lbl[0] >> 4); + tos = erspan_get_tos(skb); /* Unlike v1, v2 does not have En field, * so only extract vlan tci field. */ - if (eth->h_proto == htons(ETH_P_8021Q)) { - qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN); - vlan_tci = ntohs(qp->tci); - } + erspan_get_vlan_tci(skb, &vlan_tci); bso = erspan_detect_bso(skb); skb_push(skb, sizeof(*ershdr) + ERSPAN_V2_MDSIZE); diff --git a/include/net/ip_tunnels.h b/include/net/ip_tunnels.h index 5cccf4c0e691..fc141cc7b253 100644 --- a/include/net/ip_tunnels.h +++ b/include/net/ip_tunnels.h @@ -567,11 +567,22 @@ static inline int ip_tunnel_encap(struct sk_buff *skb, return ret; } +static inline __be16 ip_tunnel_payload_protocol(const struct sk_buff *skb, + const struct net_device *dev) +{ + int mac_off = (dev && dev->type == ARPHRD_ETHER && + skb_mac_header_was_set(skb)) ? + max(skb_mac_offset(skb), 0) : 0; + + return vlan_get_protocol_offset_inline(skb, skb->protocol, + mac_off, NULL); +} + /* Extract dsfield from inner protocol */ static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph, const struct sk_buff *skb) { - __be16 payload_protocol = skb_protocol(skb, true); + __be16 payload_protocol = ip_tunnel_payload_protocol(skb, skb->dev); if (payload_protocol == htons(ETH_P_IP)) return iph->tos; diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index 5485586fa0e6..e26e484ca6c4 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c @@ -111,9 +111,6 @@ static struct rtnl_link_ops ipgre_link_ops __read_mostly; static const struct header_ops ipgre_header_ops; static int ipgre_tunnel_init(struct net_device *dev); -static void erspan_build_header(struct sk_buff *skb, - u32 id, u32 index, - bool truncate, bool is_ipv4); static unsigned int ipgre_net_id __read_mostly; static unsigned int gre_tap_net_id __read_mostly; @@ -596,14 +593,14 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev) if (version == 1) { erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)), - ntohl(md->u.index), truncate, true); + ntohl(md->u.index), truncate); proto = htons(ETH_P_ERSPAN); } else if (version == 2) { erspan_build_header_v2(skb, ntohl(tunnel_id_to_key32(key->tun_id)), md->u.md2.dir, get_hwid(&md->u.md2), - truncate, true); + truncate); proto = htons(ETH_P_ERSPAN2); } else { goto err_free_skb; @@ -708,7 +705,7 @@ static netdev_tx_t erspan_xmit(struct sk_buff *skb, bool truncate = false; __be16 proto; - if (!pskb_inet_may_pull(skb)) + if (!erspan_xmit_prepare(skb)) goto free_skb; if (tunnel->collect_md) { @@ -736,13 +733,11 @@ static netdev_tx_t erspan_xmit(struct sk_buff *skb, __clear_bit(IP_TUNNEL_SEQ_BIT, flags); } else if (tunnel->erspan_ver == 1) { erspan_build_header(skb, ntohl(tunnel->parms.o_key), - tunnel->index, - truncate, true); + tunnel->index, truncate); proto = htons(ETH_P_ERSPAN); } else if (tunnel->erspan_ver == 2) { erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key), - tunnel->dir, tunnel->hwid, - truncate, true); + tunnel->dir, tunnel->hwid, truncate); proto = htons(ETH_P_ERSPAN2); } else { goto free_skb; diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c index 0875474a578a..a2c8790c4363 100644 --- a/net/ipv4/ip_tunnel.c +++ b/net/ipv4/ip_tunnel.c @@ -690,7 +690,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, inner_iph = (const struct iphdr *)skb_inner_network_header(skb); connected = (tunnel->parms.iph.daddr != 0); - payload_protocol = skb_protocol(skb, true); + payload_protocol = ip_tunnel_payload_protocol(skb, dev); memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c index b5d6ed8f0c36..c8b2c9a0d191 100644 --- a/net/ipv6/ip6_gre.c +++ b/net/ipv6/ip6_gre.c @@ -935,7 +935,7 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, __u32 mtu; int nhoff; - if (!pskb_inet_may_pull(skb)) + if (!erspan_xmit_prepare(skb)) goto tx_err; if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) @@ -1004,15 +1004,14 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, if (md->version == 1) { erspan_build_header(skb, ntohl(tun_id), - ntohl(md->u.index), truncate, - false); + ntohl(md->u.index), truncate); proto = htons(ETH_P_ERSPAN); } else if (md->version == 2) { erspan_build_header_v2(skb, ntohl(tun_id), md->u.md2.dir, get_hwid(&md->u.md2), - truncate, false); + truncate); proto = htons(ETH_P_ERSPAN2); } else { goto tx_err; @@ -1038,14 +1037,12 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, if (t->parms.erspan_ver == 1) { erspan_build_header(skb, ntohl(t->parms.o_key), - t->parms.index, - truncate, false); + t->parms.index, truncate); proto = htons(ETH_P_ERSPAN); } else if (t->parms.erspan_ver == 2) { erspan_build_header_v2(skb, ntohl(t->parms.o_key), t->parms.dir, - t->parms.hwid, - truncate, false); + t->parms.hwid, truncate); proto = htons(ETH_P_ERSPAN2); } else { goto tx_err; diff --git a/net/ipv6/ip6_tunnel.c b/net/ipv6/ip6_tunnel.c index 64a184bca467..bda63dd945aa 100644 --- a/net/ipv6/ip6_tunnel.c +++ b/net/ipv6/ip6_tunnel.c @@ -1116,7 +1116,7 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield, u8 hop_limit; int err = -1; - payload_protocol = skb_protocol(skb, true); + payload_protocol = ip_tunnel_payload_protocol(skb, dev); if (t->parms.collect_md) { hop_limit = skb_tunnel_info(skb)->key.ttl; @@ -1249,12 +1249,22 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield, skb_dst_set(skb, dst); if (hop_limit == 0) { - if (payload_protocol == htons(ETH_P_IP)) - hop_limit = ip_hdr(skb)->ttl; - else if (payload_protocol == htons(ETH_P_IPV6)) - hop_limit = ipv6_hdr(skb)->hop_limit; - else - hop_limit = ip6_dst_hoplimit(dst); + int nhoff = skb_network_offset(skb); + + hop_limit = ip6_dst_hoplimit(dst); + if (nhoff >= 0 && payload_protocol == htons(ETH_P_IP)) { + struct iphdr _iph, *iph; + + iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); + if (iph) + hop_limit = iph->ttl; + } else if (nhoff >= 0 && payload_protocol == htons(ETH_P_IPV6)) { + struct ipv6hdr _ip6h, *ip6h; + + ip6h = skb_header_pointer(skb, nhoff, sizeof(_ip6h), &_ip6h); + if (ip6h) + hop_limit = ip6h->hop_limit; + } } /* Calculate max headroom for all the headers and adjust -- 2.43.0