Flatten ip6_route_get_saddr() into ip6_dst_lookup_tail (which really just means handling fib6_prefsrc), and then replace ipv6_dev_get_saddr with ipv6_fl_get_saddr to pass down the flow information. Signed-off-by: David Lamparter Cc: Lorenzo Colitti Cc: Patrick Rohr --- net/ipv6/ip6_output.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index 0412f8544695..eeda556f275f 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1111,31 +1111,44 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk, struct neighbour *n; struct rt6_info *rt; #endif - int err; + int err = 0; int flags = 0; /* The correct way to handle this would be to do - * ip6_route_get_saddr, and then ip6_route_output; however, + * ipv6_fl_get_saddr, and then ip6_route_output; however, * the route-specific preferred source forces the - * ip6_route_output call _before_ ip6_route_get_saddr. + * ip6_route_output call _before_ ipv6_fl_get_saddr. * * In source specific routing (no src=any default route), * ip6_route_output will fail given src=any saddr, though, so * that's why we try it again later. */ if (ipv6_addr_any(&fl6->saddr)) { + struct net_device *l3mdev; + struct net_device *dev; struct fib6_info *from; struct rt6_info *rt; + bool same_vrf; + int err = 0; *dst = ip6_route_output(net, sk, fl6); rt = (*dst)->error ? NULL : dst_rt6_info(*dst); rcu_read_lock(); from = rt ? rcu_dereference(rt->from) : NULL; - err = ip6_route_get_saddr(net, from, &fl6->daddr, - sk ? READ_ONCE(inet6_sk(sk)->srcprefs) : 0, - fl6->flowi6_l3mdev, - &fl6->saddr); + + l3mdev = dev_get_by_index_rcu(net, fl6->flowi6_l3mdev); + if (!from || !from->fib6_prefsrc.plen || l3mdev) + dev = from ? fib6_info_nh_dev(from) : NULL; + same_vrf = !l3mdev || l3mdev_master_dev_rcu(dev) == l3mdev; + if (from && from->fib6_prefsrc.plen && same_vrf) + fl6->saddr = from->fib6_prefsrc.addr; + else + err = ipv6_fl_get_saddr(net, *dst, + same_vrf ? dev : l3mdev, sk, + sk ? READ_ONCE(inet6_sk(sk)->srcprefs) : 0, + fl6); + rcu_read_unlock(); if (err) -- 2.47.2