erspan_tunnel_init() is the only place computing tunnel->tun_hlen and tunnel->hlen, but erspan_changelink() can change both: tunnel->erspan_ver selects a 4 or 8 byte GRE header and feeds erspan_hdr_len(), while ip_tunnel_encap_setup() recomputes tunnel->hlen without the ERSPAN part. dev->needed_headroom is not refreshed either, since ip_tunnel_update() only rebinds when the link or the fwmark changes. erspan_xmit() calls skb_cow_head(skb, dev->needed_headroom) before erspan_build_header[_v2]() pushes the header. Going from version 0 to version 2 adds 20 bytes, so a packet with little headroom can hit skb_under_panic(). Move the computation into erspan_set_hlen() and add erspan_link_update(), refreshing the lengths as the previous patch does for plain GRE. It runs before the erspan_netlink_parms() error check and before ip_tunnel_changelink(), because the new version is committed by then and erspan_xmit() sizes its push from it. Fixes: f551c91de262 ("net: erspan: introduce erspan v2 for ip_gre") Signed-off-by: Eric Dumazet --- net/ipv4/ip_gre.c | 48 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index 556ebf2c5bd0d110290f2ed82ca1221dcc19c5f2..4807fc6d2fd886621480dcff20f8691ec32c67bc 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c @@ -1368,18 +1368,43 @@ static const struct net_device_ops gre_tap_netdev_ops = { .ndo_fill_metadata_dst = gre_fill_metadata_dst, }; +static void erspan_set_hlen(struct ip_tunnel *tunnel) +{ + /* Version 0 uses a 4-byte GRE header, other versions use 8 bytes. */ + tunnel->tun_hlen = tunnel->erspan_ver == 0 ? 4 : 8; + + tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + + erspan_hdr_len(tunnel->erspan_ver); +} + +/* Both tunnel->erspan_ver and tunnel->encap_hlen can be changed from + * erspan_changelink(), and both feed tunnel->hlen. Recompute it, then let + * ip_tunnel_bind_dev() derive the device lengths from it. + * + * As in ipgre_link_update(), @old_hlen only tells whether the MTU became + * stale and must be sampled before ip_tunnel_encap_setup(), which + * recomputes tunnel->hlen without the ERSPAN part. + */ +static void erspan_link_update(struct net_device *dev, bool set_mtu, + int old_hlen) +{ + struct ip_tunnel *tunnel = netdev_priv(dev); + + erspan_set_hlen(tunnel); + + /* Only reset a MTU that the header length just invalidated, so that + * a MTU configured by the user survives an unrelated change. + */ + ip_tunnel_refresh_lengths(dev, set_mtu && tunnel->hlen != old_hlen); +} + static int erspan_tunnel_init(struct net_device *dev) { struct ip_tunnel *tunnel = netdev_priv(dev); - if (tunnel->erspan_ver == 0) - tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */ - else - tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */ + erspan_set_hlen(tunnel); tunnel->parms.iph.protocol = IPPROTO_GRE; - tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + - erspan_hdr_len(tunnel->erspan_ver); dev->features |= GRE_FEATURES; dev->hw_features |= GRE_FEATURES; @@ -1515,6 +1540,7 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], struct ip_tunnel *t = netdev_priv(dev); struct ip_tunnel_parm_kern p; __u32 fwmark = t->fwmark; + int old_hlen = t->hlen; int err; if (!rtnl_dev_link_net_capable(dev, t->net)) @@ -1525,6 +1551,16 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], return err; err = erspan_netlink_parms(dev, data, tb, &p, &fwmark); + + /* ipgre_newlink_encap_setup() has published a new encapsulation, and + * erspan_netlink_parms() a new ERSPAN version, both of which change + * the header length. Refresh the lengths before looking at @err: + * erspan_xmit() sizes its push from tunnel->erspan_ver, and both this + * error path and ip_tunnel_changelink() below leave the new + * encapsulation behind. + */ + erspan_link_update(dev, !tb[IFLA_MTU], old_hlen); + if (err < 0) return err; -- 2.55.0.1007.g17ff1f9808-goog