gre: Move MTU setting out of ipgre_tunnel_bind_dev
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / ip_gre.c
index 4342cba4ff823bbddfe053d9583f31b9521d735d..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;
 
@@ -473,6 +476,8 @@ static int ipgre_rcv(struct sk_buff *skb)
        read_lock(&ipgre_lock);
        if ((tunnel = ipgre_tunnel_lookup(dev_net(skb->dev),
                                        iph->saddr, iph->daddr, key)) != NULL) {
+               struct net_device_stats *stats = &tunnel->dev->stats;
+
                secpath_reset(skb);
 
                skb->protocol = *(__be16*)(h + 2);
@@ -497,28 +502,28 @@ static int ipgre_rcv(struct sk_buff *skb)
                        /* Looped back packet, drop it! */
                        if (skb->rtable->fl.iif == 0)
                                goto drop;
-                       tunnel->stat.multicast++;
+                       stats->multicast++;
                        skb->pkt_type = PACKET_BROADCAST;
                }
 #endif
 
                if (((flags&GRE_CSUM) && csum) ||
                    (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
-                       tunnel->stat.rx_crc_errors++;
-                       tunnel->stat.rx_errors++;
+                       stats->rx_crc_errors++;
+                       stats->rx_errors++;
                        goto drop;
                }
                if (tunnel->parms.i_flags&GRE_SEQ) {
                        if (!(flags&GRE_SEQ) ||
                            (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) {
-                               tunnel->stat.rx_fifo_errors++;
-                               tunnel->stat.rx_errors++;
+                               stats->rx_fifo_errors++;
+                               stats->rx_errors++;
                                goto drop;
                        }
                        tunnel->i_seqno = seqno + 1;
                }
-               tunnel->stat.rx_packets++;
-               tunnel->stat.rx_bytes += skb->len;
+               stats->rx_packets++;
+               stats->rx_bytes += skb->len;
                skb->dev = tunnel->dev;
                dst_release(skb->dst);
                skb->dst = NULL;
@@ -540,7 +545,7 @@ drop_nolock:
 static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct ip_tunnel *tunnel = netdev_priv(dev);
-       struct net_device_stats *stats = &tunnel->stat;
+       struct net_device_stats *stats = &tunnel->dev->stats;
        struct iphdr  *old_iph = ip_hdr(skb);
        struct iphdr  *tiph;
        u8     tos;
@@ -554,7 +559,7 @@ static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
        int    mtu;
 
        if (tunnel->recursion++) {
-               tunnel->stat.collisions++;
+               stats->collisions++;
                goto tx_error;
        }
 
@@ -570,7 +575,7 @@ static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                /* NBMA tunnel */
 
                if (skb->dst == NULL) {
-                       tunnel->stat.tx_fifo_errors++;
+                       stats->tx_fifo_errors++;
                        goto tx_error;
                }
 
@@ -621,7 +626,7 @@ static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                                                .tos = RT_TOS(tos) } },
                                    .proto = IPPROTO_GRE };
                if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
-                       tunnel->stat.tx_carrier_errors++;
+                       stats->tx_carrier_errors++;
                        goto tx_error;
                }
        }
@@ -629,13 +634,13 @@ static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
 
        if (tdev == dev) {
                ip_rt_put(rt);
-               tunnel->stat.collisions++;
+               stats->collisions++;
                goto tx_error;
        }
 
        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;
 
@@ -771,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;
@@ -783,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,
@@ -804,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;
@@ -818,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
@@ -915,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);
                                }
                        }
@@ -954,15 +964,11 @@ done:
        return err;
 }
 
-static struct net_device_stats *ipgre_tunnel_get_stats(struct net_device *dev)
-{
-       return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
-}
-
 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;
@@ -1084,12 +1090,11 @@ static void ipgre_tunnel_setup(struct net_device *dev)
        dev->uninit             = ipgre_tunnel_uninit;
        dev->destructor         = free_netdev;
        dev->hard_start_xmit    = ipgre_tunnel_xmit;
-       dev->get_stats          = ipgre_tunnel_get_stats;
        dev->do_ioctl           = ipgre_tunnel_ioctl;
        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;
@@ -1111,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)) {