Skip to contentSkip to conversation

net: ipv6: put host and anycast routes on device with address

PATCH In review David Ahern 3 participants 8 messages Aug 17, 2017 – Aug 21, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: ipv6: put host and anycast routes on device with address

One nagging difference between ipv4 and ipv6 is host routes for ipv6 addresses are installed using the loopback device or VRF / L3 Master device. e.g.,

2001:db8:1::/120 dev veth0 proto kernel metric 256 pref medium local 2001:db8:1::1 dev lo table local proto kernel metric 0 pref medium

Using the loopback device is convenient -- necessary for local tx, but has some nasty side effects, most notably setting the 'lo' device down causes all host routes for all local IPv6 address to be removed from the FIB and completely breaks IPv6 networking across all interfaces.

This patch puts FIB entries for IPv6 routes against the device. This simplifies the routes in the FIB, for example by making dst->dev and rt6i_idev->dev the same (a future patch can look at removing the device reference taken for rt6i_idev for FIB entries).

When copies are made on FIB lookups, the cloned route has dst->dev set to loopback (or the L3 master device). This is needed for the local Tx of packets to local addresses.

With fib entries allocated against the real network device, the addrconf code that reinserts host routes on admin up of 'lo' is no longer needed.

---
v2
- update icmp6_send to use rt6i_idev for linklocal addresses if skb dev is loopback. ICMP unreachables were hitting the wrong route in response to UDP messages to local linklocal addresses with no server listening

 net/ipv6/addrconf.c | 42 ------------------------------------------
 net/ipv6/icmp.c     | 15 +++++++++++++--
 net/ipv6/route.c    | 46 ++++++++++++++++++++++++++++++++++------------
 3 files changed, 47 insertions(+), 56 deletions(-)
net/ipv6/addrconf.c +0 -42
index 640792e1ecb7..45d0a24644de 100644
@@ -3030,9 +3030,6 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
 static void init_loopback(struct net_device *dev)
 {
 	struct inet6_dev  *idev;
-	struct net_device *sp_dev;
-	struct inet6_ifaddr *sp_ifa;
-	struct rt6_info *sp_rt;
 
 	/* ::1 */
 
@@ -3045,45 +3042,6 @@ static void init_loopback(struct net_device *dev)
 	}
 
 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
-
-	/* Add routes to other interface's IPv6 addresses */
-	for_each_netdev(dev_net(dev), sp_dev) {
-		if (!strcmp(sp_dev->name, dev->name))
-			continue;
-
-		idev = __in6_dev_get(sp_dev);
-		if (!idev)
-			continue;
-
-		read_lock_bh(&idev->lock);
-		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
-
-			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
-				continue;
-
-			if (sp_ifa->rt) {
-				/* This dst has been added to garbage list when
-				 * lo device down, release this obsolete dst and
-				 * reallocate a new router for ifa.
-				 */
-				if (!sp_ifa->rt->rt6i_node) {
-					ip6_rt_put(sp_ifa->rt);
-					sp_ifa->rt = NULL;
-				} else {
-					continue;
-				}
-			}
-
-			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
-
-			/* Failure cases are ignored */
-			if (!IS_ERR(sp_rt)) {
-				sp_ifa->rt = sp_rt;
-				ip6_ins_rt(sp_rt);
-			}
-		}
-		read_unlock_bh(&idev->lock);
-	}
 }
 
 void addrconf_add_linklocal(struct inet6_dev *idev,
net/ipv6/icmp.c +13 -2
index 8d7b113958b1..4f82830fc068 100644
@@ -459,9 +459,20 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
 	 *	Source addr check
 	 */
 
-	if (__ipv6_addr_needs_scope_id(addr_type))
+	if (__ipv6_addr_needs_scope_id(addr_type)) {
 		iif = skb->dev->ifindex;
-	else {
+
+		/* for local packets, get the real device index */
+		if (iif == LOOPBACK_IFINDEX) {
+			dst = skb_dst(skb);
+			if (dst) {
+				struct rt6_info *rt;
+
+				rt = container_of(dst, struct rt6_info, dst);
+				iif = rt->rt6i_idev->dev->ifindex;
+			}
+		}
+	} else {
 		dst = skb_dst(skb);
 		iif = l3mdev_master_ifindex(dst ? dst->dev : skb->dev);
 	}
net/ipv6/route.c +34 -12

78 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw

Hello David,

Quoted 7 lines · David Ahern
net/ipv6/route.c +1 -1
@@ -2688,15 +2716,9 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
 {
 	u32 tb_id;
 	struct net *net = dev_net(idev->dev);
-	struct net_device *dev = net->loopback_dev;
+	struct net_device *dev = idev->dev;
 	struct rt6_info *rt;

net/ipv6/route.c struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, · line 2724

Quoted 10 lines
net/ipv6/route.c +0 -6
 
-	/* 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);

I am afraid this change might break Java:

<http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l574>

I am all in for this change, but maybe it might be necessary to mask RTF_LOCAL routes with "lo" somehow.

Bye,
Hannes

#2 lore raw
Quoted 24 lines · Hannes Frederic Sowa

Hello David,

Quoted 7 lines · David Ahern
net/ipv6/route.c +1 -1
@@ -2688,15 +2716,9 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
 {
 	u32 tb_id;
 	struct net *net = dev_net(idev->dev);
-	struct net_device *dev = net->loopback_dev;
+	struct net_device *dev = idev->dev;
 	struct rt6_info *rt;

net/ipv6/route.c struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, · line 2724

Quoted 10 lines
net/ipv6/route.c +0 -6
 
-	/* 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);

I am afraid this change might break Java:

Quoted 4 lines

<http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l574>

I am all in for this change, but maybe it might be necessary to mask RTF_LOCAL routes with "lo" somehow.

That's asinine. The if_inet6 processing is just getting the 'lo' interface index. Why scan the file looking for that? The ipv6_route processing is assembling routes against the loopback device regardless of what the route is. Do you know why - what the route list is used for?

If it matters, we could keep 'lo' as the device for RTF_LOCAL routes in the proc files to keep backwards compatibility.

#3 lore raw
Quoted 27 lines · David Ahern
Quoted 26 lines · Hannes Frederic Sowa

Hello David,

Quoted 7 lines · David Ahern
net/ipv6/route.c +1 -1
@@ -2688,15 +2716,9 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
 {
 	u32 tb_id;
 	struct net *net = dev_net(idev->dev);
-	struct net_device *dev = net->loopback_dev;
+	struct net_device *dev = idev->dev;
 	struct rt6_info *rt;

net/ipv6/route.c struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, · line 2724

Quoted 10 lines
net/ipv6/route.c +0 -6
 
-	/* 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);

I am afraid this change might break Java:

<http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l574>

Quoted 7 lines
Quoted 2 lines

I am all in for this change, but maybe it might be necessary to mask RTF_LOCAL routes with "lo" somehow.

That's asinine. The if_inet6 processing is just getting the 'lo' interface index. Why scan the file looking for that? The ipv6_route processing is assembling routes against the loopback device regardless of what the route is. Do you know why - what the route list is used for?

If I read it correctly, seems to be a 2.4 workaround:
- only user of the route list is needsLoopbackRoute()
- only caller of needsLoopbackRoute is here:

http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l828

#4 lore raw
Quoted 37 lines · David Ahern
Quoted 27 lines · David Ahern
Quoted 26 lines · Hannes Frederic Sowa

Hello David,

Quoted 7 lines · David Ahern
net/ipv6/route.c +1 -1
@@ -2688,15 +2716,9 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
 {
 	u32 tb_id;
 	struct net *net = dev_net(idev->dev);
-	struct net_device *dev = net->loopback_dev;
+	struct net_device *dev = idev->dev;
 	struct rt6_info *rt;

net/ipv6/route.c struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, · line 2724

Quoted 10 lines
net/ipv6/route.c +0 -6
 
-	/* 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);

I am afraid this change might break Java:

<http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l574>

Quoted 7 lines
Quoted 2 lines

I am all in for this change, but maybe it might be necessary to mask RTF_LOCAL routes with "lo" somehow.

That's asinine. The if_inet6 processing is just getting the 'lo' interface index. Why scan the file looking for that? The ipv6_route processing is assembling routes against the loopback device regardless of what the route is. Do you know why - what the route list is used for?

Quoted 5 lines

If I read it correctly, seems to be a 2.4 workaround:
- only user of the route list is needsLoopbackRoute()
- only caller of needsLoopbackRoute is here:

http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l828

I agree that it looks like dead code now. But I know for sure that this code has been excercised at least at some point in time and caused problems for JVMs on Linux with IPv6.

On the top of this file I found this comment:

-- >8 --
/* following code creates a list of addresses from the kernel
* routing table that are routed via the loopback address.
* We check all destination addresses against this table
* and override the scope_id field to use the relevant value for "lo"
* in order to work-around the Linux bug that prevents packets destined
* for certain local addresses from being sent via a physical interface. */
-- 8< --

I don't know if it makes sense to dive down into java history (and I also found e.g. net-snmp scanning /proc/net/ipv6_route). The same problem might be visible via RTM_GETROUTE dumps if applications implement their own source address selection maybe. :/

Bye, Hannes

#5 lore raw
Quoted 61 lines · Hannes Frederic Sowa
Quoted 37 lines · David Ahern
Quoted 27 lines · David Ahern
Quoted 26 lines · Hannes Frederic Sowa

Hello David,

Quoted 7 lines · David Ahern
net/ipv6/route.c +1 -1
@@ -2688,15 +2716,9 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
 {
 	u32 tb_id;
 	struct net *net = dev_net(idev->dev);
-	struct net_device *dev = net->loopback_dev;
+	struct net_device *dev = idev->dev;
 	struct rt6_info *rt;

net/ipv6/route.c struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, · line 2724

Quoted 10 lines
net/ipv6/route.c +0 -6
 
-	/* 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);

I am afraid this change might break Java:

<http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l574>

Quoted 7 lines
Quoted 2 lines

I am all in for this change, but maybe it might be necessary to mask RTF_LOCAL routes with "lo" somehow.

That's asinine. The if_inet6 processing is just getting the 'lo' interface index. Why scan the file looking for that? The ipv6_route processing is assembling routes against the loopback device regardless of what the route is. Do you know why - what the route list is used for?

Quoted 5 lines

If I read it correctly, seems to be a 2.4 workaround:
- only user of the route list is needsLoopbackRoute()
- only caller of needsLoopbackRoute is here:

http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l828

I agree that it looks like dead code now. But I know for sure that this code has been excercised at least at some point in time and caused problems for JVMs on Linux with IPv6.

On the top of this file I found this comment:

-- >8 --
/* following code creates a list of addresses from the kernel
* routing table that are routed via the loopback address.
* We check all destination addresses against this table
* and override the scope_id field to use the relevant value for "lo"
* in order to work-around the Linux bug that prevents packets destined
* for certain local addresses from being sent via a physical interface. */
-- 8< --

Quoted 4 lines

I don't know if it makes sense to dive down into java history (and I also found e.g. net-snmp scanning /proc/net/ipv6_route). The same problem might be visible via RTM_GETROUTE dumps if applications implement their own source address selection maybe. :/

Dave: The java code appears to be dead code from 2.4 time frame. no longer relevant for this patch.

Hannes: I believe the net-snmp use case is populating the MIB. MIB entries, like the proc files and rtnetlink responses, will have the change in that host routes show the device with the address rather than loopback. But, looking at the send code I don't see this change having an impact.

maintainer
#6 lore raw
In reply to David Ahern

From: David Ahern <[email protected]>
Date: Fri, 18 Aug 2017 18:05:56 -0600

Quoted 25 lines · Hannes Frederic Sowa
Quoted 24 lines

Hello David,

Quoted 7 lines · David Ahern
net/ipv6/route.c +1 -1
@@ -2688,15 +2716,9 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
 {
 	u32 tb_id;
 	struct net *net = dev_net(idev->dev);
-	struct net_device *dev = net->loopback_dev;
+	struct net_device *dev = idev->dev;
 	struct rt6_info *rt;

net/ipv6/route.c struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, · line 2724

Quoted 10 lines
net/ipv6/route.c +0 -6
 
-	/* 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);

I am afraid this change might break Java:

Quoted 6 lines
Quoted 4 lines

<http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/unix/native/libnet/net_util_md.c#l574>

I am all in for this change, but maybe it might be necessary to mask RTF_LOCAL routes with "lo" somehow.

That's asinine.

To say the least.

We really should suggest to them a better way to do what they are trying to do.

maintainer
#7 lore raw
In reply to David Ahern

From: David Ahern <[email protected]>
Date: Thu, 17 Aug 2017 12:17:20 -0700

Quoted 17 lines

One nagging difference between ipv4 and ipv6 is host routes for ipv6 addresses are installed using the loopback device or VRF / L3 Master device. e.g.,

2001:db8:1::/120 dev veth0 proto kernel metric 256 pref medium local 2001:db8:1::1 dev lo table local proto kernel metric 0 pref medium

Using the loopback device is convenient -- necessary for local tx, but has some nasty side effects, most notably setting the 'lo' device down causes all host routes for all local IPv6 address to be removed from the FIB and completely breaks IPv6 networking across all interfaces.

This patch puts FIB entries for IPv6 routes against the device. This simplifies the routes in the FIB, for example by making dst->dev and rt6i_idev->dev the same (a future patch can look at removing the device reference taken for rt6i_idev for FIB entries).

Quoted 8 lines

When copies are made on FIB lookups, the cloned route has dst->dev set to loopback (or the L3 master device). This is needed for the local Tx of packets to local addresses.

With fib entries allocated against the real network device, the addrconf code that reinserts host routes on admin up of 'lo' is no longer needed.

I've applied this.

I'm unlikely to revert this just for java, given the kinds of things they are doing.