Allow an L3 master device to act as the loopback for that L3 domain.
For IPv4 the device can also have the address 127.0.0.1.
Signed-off-by: David Ahern <dsa@cumulusnetworks.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
 }
 
 static inline
-const struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
+struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
 {
        /* netdev_master_upper_dev_get_rcu calls
         * list_first_or_null_rcu to walk the upper dev list.
         * typecast to remove the const
         */
        struct net_device *dev = (struct net_device *)_dev;
-       const struct net_device *master;
+       struct net_device *master;
 
        if (!dev)
                return NULL;
 }
 
 static inline
-const struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
+struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
 {
        return NULL;
 }
 
                return ERR_PTR(-EINVAL);
 
        if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
-               if (ipv4_is_loopback(fl4->saddr) && !(dev_out->flags & IFF_LOOPBACK))
+               if (ipv4_is_loopback(fl4->saddr) &&
+                   !(dev_out->flags & IFF_LOOPBACK) &&
+                   !netif_is_l3_master(dev_out))
                        return ERR_PTR(-EINVAL);
 
        if (ipv4_is_lbcast(fl4->daddr))
                        else
                                fl4->saddr = fl4->daddr;
                }
-               dev_out = net->loopback_dev;
+
+               /* L3 master device is the loopback for that domain */
+               dev_out = l3mdev_master_dev_rcu(dev_out) ? : net->loopback_dev;
                fl4->flowi4_oif = dev_out->ifindex;
                flags |= RTCF_LOCAL;
                goto make_route;
 
 {
        u32 tb_id;
        struct net *net = dev_net(idev->dev);
-       struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
-                                           DST_NOCOUNT);
+       struct net_device *dev = net->loopback_dev;
+       struct rt6_info *rt;
+
+       /* use L3 Master device as loopback for host routes if device
+        * is enslaved and address is not link local or multicast
+        */
+       if (!rt6_need_strict(addr))
+               dev = l3mdev_master_dev_rcu(idev->dev) ? : dev;
+
+       rt = ip6_dst_alloc(net, dev, DST_NOCOUNT);
        if (!rt)
                return ERR_PTR(-ENOMEM);