gre: Move MTU setting out of ipgre_tunnel_bind_dev
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / ip_gre.c
index 2a61158ea7226cdff5d6b28fa0cbb0f6b2533d96..80622dd6fb3f7cec0b01b95c4fb6f5dfd86a6079 100644 (file)
 
 static int ipgre_tunnel_init(struct net_device *dev);
 static void ipgre_tunnel_setup(struct net_device *dev);
+static int ipgre_tunnel_bind_dev(struct net_device *dev);
 
 /* Fallback tunnel: no source, no destination, no key, no options */
 
@@ -289,6 +290,8 @@ static struct ip_tunnel * ipgre_tunnel_locate(struct net *net,
        nt = netdev_priv(dev);
        nt->parms = *parms;
 
+       dev->mtu = ipgre_tunnel_bind_dev(dev);
+
        if (register_netdevice(dev) < 0)
                goto failed_free;
 
@@ -637,7 +640,7 @@ static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
 
        df = tiph->frag_off;
        if (df)
-               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
+               mtu = dst_mtu(&rt->u.dst) - dev->hard_header_len - tunnel->hlen;
        else
                mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
 
@@ -773,7 +776,7 @@ tx_error:
        return 0;
 }
 
-static void ipgre_tunnel_bind_dev(struct net_device *dev)
+static int ipgre_tunnel_bind_dev(struct net_device *dev)
 {
        struct net_device *tdev = NULL;
        struct ip_tunnel *tunnel;
@@ -785,7 +788,7 @@ static void ipgre_tunnel_bind_dev(struct net_device *dev)
        tunnel = netdev_priv(dev);
        iph = &tunnel->parms.iph;
 
-       /* Guess output device to choose reasonable mtu and hard_header_len */
+       /* Guess output device to choose reasonable mtu and needed_headroom */
 
        if (iph->daddr) {
                struct flowi fl = { .oif = tunnel->parms.link,
@@ -806,7 +809,7 @@ static void ipgre_tunnel_bind_dev(struct net_device *dev)
                tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
 
        if (tdev) {
-               hlen = tdev->hard_header_len;
+               hlen = tdev->hard_header_len + tdev->needed_headroom;
                mtu = tdev->mtu;
        }
        dev->iflink = tunnel->parms.link;
@@ -820,10 +823,15 @@ static void ipgre_tunnel_bind_dev(struct net_device *dev)
                if (tunnel->parms.o_flags&GRE_SEQ)
                        addend += 4;
        }
-       dev->hard_header_len = hlen + addend;
-       dev->mtu = mtu - addend;
+       dev->needed_headroom = addend + hlen;
+       mtu -= dev->hard_header_len - addend;
+
+       if (mtu < 68)
+               mtu = 68;
+
        tunnel->hlen = addend;
 
+       return mtu;
 }
 
 static int
@@ -917,7 +925,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                                t->parms.iph.frag_off = p.iph.frag_off;
                                if (t->parms.link != p.link) {
                                        t->parms.link = p.link;
-                                       ipgre_tunnel_bind_dev(dev);
+                                       dev->mtu = ipgre_tunnel_bind_dev(dev);
                                        netdev_state_change(dev);
                                }
                        }
@@ -959,7 +967,8 @@ done:
 static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
 {
        struct ip_tunnel *tunnel = netdev_priv(dev);
-       if (new_mtu < 68 || new_mtu > 0xFFF8 - tunnel->hlen)
+       if (new_mtu < 68 ||
+           new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
                return -EINVAL;
        dev->mtu = new_mtu;
        return 0;
@@ -1085,7 +1094,7 @@ static void ipgre_tunnel_setup(struct net_device *dev)
        dev->change_mtu         = ipgre_tunnel_change_mtu;
 
        dev->type               = ARPHRD_IPGRE;
-       dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
+       dev->needed_headroom    = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
        dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
        dev->flags              = IFF_NOARP;
        dev->iflink             = 0;
@@ -1107,8 +1116,6 @@ static int ipgre_tunnel_init(struct net_device *dev)
        memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
        memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
 
-       ipgre_tunnel_bind_dev(dev);
-
        if (iph->daddr) {
 #ifdef CONFIG_NET_IPGRE_BROADCAST
                if (ipv4_is_multicast(iph->daddr)) {