[PATCH v2 1/3] ipv6: Use the selected nexthop for source address selection
From: Tim Menninger
Date: Thu Dec 03 2026 - 18:48:59 EST
When a route uses a multipath nexthop group and has no preferred
source, ip6_route_get_saddr() selects the source address for
fib6_info_nh_dev(), which is the group's first nexthop. The lookup may
have selected a different nexthop, so the source address can belong to
another interface than the one the packet leaves through:
$ ip -6 route get fd00:ecb:eeee::1
... via fe80::b2cf:eff:feaf:6eb6 dev ens6np0 src fd00:ecb::3 ...
where fd00:ecb::3 is assigned to ens3np0. IPv6 legacy multipath routes
and IPv4 nexthop groups both select the source address for the chosen
path. TCP and UDP can operate with a source address assigned to another
interface, but RoCE address resolution requires a matching GID on the
selected device, and rdma_cm address resolution fails with -ENODEV
whenever the hash selects a nexthop other than the first.
Pass the dst to ip6_route_get_saddr() and, for a multipath nexthop
group, select the source address for the device of the selected path.
A configured preferred source still takes precedence, and routes without
a multipath nexthop group are unchanged.
Tested on a host with two ConnectX-7 ports, an IPv6 route through a
two-member nexthop group and hash policy 1, with rdma_cm resolving the
address and route of 64 IDs per destination. Before this change, every
ID failed for three destinations whose hash selected the second
nexthop. After it, every ID resolved, with the source address on the
egress port. A route with a preferred source on another interface kept
that source, and local destinations resolved as before.
Fixes: f88d8ea67fbd ("ipv6: Plumb support for nexthop object in a fib6_info")
Signed-off-by: Tim Menninger <tmenninger@xxxxxxxxxxxxxxxx>
---
Changes in v2:
- New patch, replacing the addr6_resolve() fix proposed on v1
include/net/ip6_route.h | 9 +++++++--
net/ipv6/ip6_output.c | 3 ++-
net/ipv6/route.c | 2 +-
3 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h
index 95f936be32c6..d5754f6442bb 100644
--- a/include/net/ip6_route.h
+++ b/include/net/ip6_route.h
@@ -143,6 +143,7 @@ void rt6_age_exceptions(struct fib6_info *f6i, struct fib6_gc_args *gc_args,
unsigned long now);
static inline int ip6_route_get_saddr(struct net *net, struct fib6_info *f6i,
+ const struct dst_entry *dst,
const struct in6_addr *daddr,
unsigned int prefs, int l3mdev_index,
struct in6_addr *saddr)
@@ -155,8 +156,12 @@ static inline int ip6_route_get_saddr(struct net *net, struct fib6_info *f6i,
rcu_read_lock();
l3mdev = dev_get_by_index_rcu(net, l3mdev_index);
- if (!f6i || !f6i->fib6_prefsrc.plen || l3mdev)
- dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
+ if (!f6i || !f6i->fib6_prefsrc.plen || l3mdev) {
+ if (dst && f6i && f6i->nh && nexthop_is_multipath(f6i->nh))
+ dev = dst_dev_rcu(dst);
+ else
+ dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
+ }
same_vrf = !l3mdev || l3mdev_master_dev_rcu(dev) == l3mdev;
if (f6i && f6i->fib6_prefsrc.plen && same_vrf)
*saddr = f6i->fib6_prefsrc.addr;
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index cb648c839431..6627dac626e8 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -1162,7 +1162,8 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
rcu_read_lock();
from = rt ? rcu_dereference(rt->from) : NULL;
- err = ip6_route_get_saddr(net, from, &fl6->daddr,
+ err = ip6_route_get_saddr(net, from, rt ? &rt->dst : NULL,
+ &fl6->daddr,
sk ? READ_ONCE(inet6_sk(sk)->srcprefs) : 0,
fl6->flowi6_l3mdev,
&fl6->saddr);
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 1b2dcc066e19..aec54368e1ba 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -5881,7 +5881,7 @@ static int rt6_fill_node(struct net *net, struct sk_buff *skb,
goto nla_put_failure;
} else if (dest) {
struct in6_addr saddr_buf;
- if (ip6_route_get_saddr(net, rt, dest, 0, 0, &saddr_buf) == 0 &&
+ if (ip6_route_get_saddr(net, rt, dst, dest, 0, 0, &saddr_buf) == 0 &&
nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
goto nla_put_failure;
}
--
2.53.0