Skip to contentSkip to conversation

ipv4/route: do not check saddr dev if iif is LOOPBACK_IFINDEX

PATCH In review Hangbin Liu 4 participants 8 messages Aug 1, 2019 – Aug 13, 2019

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

PATCH 1/1 ipv4/route: do not check saddr dev if iif is LOOPBACK_IFINDEX

Jianlin reported a bug that for IPv4, ip route get from src_addr would fail if src_addr is not an address on local system.

\# ip route get 1.1.1.1 from 2.2.2.2
RTNETLINK answers: Invalid argument

While IPv6 would get the default route

\# ip -6 route get 1111::1 from 2222::2

1111::1 from 2222::2 via fe80:52:0:4982::1fe dev eth0 proto ra src \
	2620::2cf:e2ff:fe40:37 metric 1024 hoplimit 64 pref medium

Fix it by disabling the __ip_dev_find() check if iif is LOOPBACK_IFINDEX.

---
 net/ipv4/route.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/route.c +2 -1
index 517300d587a7..1be78e8f9e3c 100644
@@ -2512,7 +2512,8 @@ struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *fl4,
 			goto make_route;
 		}
 
-		if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
+		if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC) &&
+		    fl4->flowi4_iif != LOOPBACK_IFINDEX) {
 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
 			if (!__ip_dev_find(net, fl4->saddr, false))
 				goto out;
Signature · 1 line
2.19.2
#1 lore raw
Quoted 5 lines · Hangbin Liu

Jianlin reported a bug that for IPv4, ip route get from src_addr would fail if src_addr is not an address on local system.

\# ip route get 1.1.1.1 from 2.2.2.2
RTNETLINK answers: Invalid argument

so this is a forwarding lookup in which case iif should be set. Based on the above 'route get' inet_rtm_getroute is doing a lookup as if it is locally generated traffic.

#2 lore raw
Quoted 8 lines · David Ahern
Quoted 5 lines · Hangbin Liu

Jianlin reported a bug that for IPv4, ip route get from src_addr would fail if src_addr is not an address on local system.

\# ip route get 1.1.1.1 from 2.2.2.2
RTNETLINK answers: Invalid argument

so this is a forwarding lookup in which case iif should be set. Based on

with out setting iif in userspace, the kernel set iif to lo by default.

Quoted 2 lines

the above 'route get' inet_rtm_getroute is doing a lookup as if it is locally generated traffic.

yeah... but what about the IPv6 part. That cause a different behavior in userspace.

Thanks
Hangbin

#3 lore raw
Quoted 11 lines · Hangbin Liu
Quoted 8 lines · David Ahern
Quoted 5 lines · Hangbin Liu

Jianlin reported a bug that for IPv4, ip route get from src_addr would fail if src_addr is not an address on local system.

\# ip route get 1.1.1.1 from 2.2.2.2
RTNETLINK answers: Invalid argument

so this is a forwarding lookup in which case iif should be set. Based on

with out setting iif in userspace, the kernel set iif to lo by default.

right, it presumes locally generated traffic.

Quoted 6 lines
Quoted 3 lines

the above 'route get' inet_rtm_getroute is doing a lookup as if it is locally generated traffic.

yeah... but what about the IPv6 part. That cause a different behavior in userspace.

just one of many, many annoying differences between v4 and v6. We could try to catalog it.

maintainer
#4 lore raw

From: David Ahern <[email protected]>
Date: Thu, 1 Aug 2019 22:16:00 -0600

Quoted 15 lines · Hangbin Liu
Quoted 11 lines · David Ahern
Quoted 8 lines · Hangbin Liu
Quoted 5 lines

Jianlin reported a bug that for IPv4, ip route get from src_addr would fail if src_addr is not an address on local system.

\# ip route get 1.1.1.1 from 2.2.2.2
RTNETLINK answers: Invalid argument

so this is a forwarding lookup in which case iif should be set. Based on

with out setting iif in userspace, the kernel set iif to lo by default.

right, it presumes locally generated traffic.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

the above 'route get' inet_rtm_getroute is doing a lookup as if it is locally generated traffic.

yeah... but what about the IPv6 part. That cause a different behavior in userspace.

just one of many, many annoying differences between v4 and v6. We could try to catalog it.

I think we just have to accept this difference because this change would change behavior for all route lookups, not just those done by ip route get.

#5 lore raw
Quoted 21 lines · On Sun, 11 Aug 2019 20:49:18 -0700 (PDT) David Miller <[email protected]>…

From: David Ahern <[email protected]>
Date: Thu, 1 Aug 2019 22:16:00 -0600

Quoted 18 lines · Hangbin Liu
Quoted 11 lines · David Ahern
Quoted 8 lines · Hangbin Liu
Quoted 5 lines

Jianlin reported a bug that for IPv4, ip route get from src_addr would fail if src_addr is not an address on local system.

\# ip route get 1.1.1.1 from 2.2.2.2
RTNETLINK answers: Invalid argument

so this is a forwarding lookup in which case iif should be set. Based on

with out setting iif in userspace, the kernel set iif to lo by default.

right, it presumes locally generated traffic.

Quoted 4 lines
Quoted 3 lines

the above 'route get' inet_rtm_getroute is doing a lookup as if it is locally generated traffic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

yeah... but what about the IPv6 part. That cause a different behavior in userspace.

just one of many, many annoying differences between v4 and v6. We could try to catalog it.

I think we just have to accept this difference because this change would change behavior for all route lookups, not just those done by ip route get.

How so, actually? I don't see how that would happen. On the forwarding path, 'iif' is set (not to loopback interface), so that's not affected.

Is there any other route lookup possibility I'm missing?

Signature · 1 line
Stefano
#6 lore raw
Quoted 4 lines · Stefano Brivio

How so, actually? I don't see how that would happen. On the forwarding path, 'iif' is set (not to loopback interface), so that's not affected.

Is there any other route lookup possibility I'm missing?

Use case is saddr is set and FLOWI_FLAG_ANYSRC is not set and that seems pretty common to me. From a quick look, icmp_route_lookup, ipv4_update_pmtu, ipv4_redirect, inet_csk_route_req, ...

Enable trace_fib_table_lookup and look at the flags for various use cases.

#7 lore raw
In reply to David Ahern

David,

Quoted 8 lines · On Thu, 1 Aug 2019 13:51:25 -0600 David Ahern <[email protected]> wrote:
Quoted 5 lines · Hangbin Liu

Jianlin reported a bug that for IPv4, ip route get from src_addr would fail if src_addr is not an address on local system.

\# ip route get 1.1.1.1 from 2.2.2.2
RTNETLINK answers: Invalid argument

so this is a forwarding lookup in which case iif should be set.

On actual forwarding, yes, it will be set.

But if we are just doing a lookup for a route (iif is LOOPBACK_IFINDEX), I think this should still give us the matching route, which is what IPv6 already does and what this patch fixes for IPv4.

Otherwise, we have no way to fetch that route, no matter if source routing is configured. So I think this patch is correct and to some extent necessary.

Signature · 1 line
Stefano