Skip to content

Commit 252442f

Browse files
NicolasDichtelkuba-moo
authored andcommitted
ipv6: fix source address selection with route leak
By default, an address assigned to the output interface is selected when the source address is not specified. This is problematic when a route, configured in a vrf, uses an interface from another vrf (aka route leak). The original vrf does not own the selected source address. Let's add a check against the output interface and call the appropriate function to select the source address. CC: [email protected] Fixes: 0d240e7 ("net: vrf: Implement get_saddr for IPv6") Signed-off-by: Nicolas Dichtel <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 6807352 commit 252442f

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

include/net/ip6_route.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,26 @@ void rt6_age_exceptions(struct fib6_info *f6i, struct fib6_gc_args *gc_args,
127127

128128
static inline int ip6_route_get_saddr(struct net *net, struct fib6_info *f6i,
129129
const struct in6_addr *daddr,
130-
unsigned int prefs,
130+
unsigned int prefs, int l3mdev_index,
131131
struct in6_addr *saddr)
132132
{
133+
struct net_device *l3mdev;
134+
struct net_device *dev;
135+
bool same_vrf;
133136
int err = 0;
134137

135-
if (f6i && f6i->fib6_prefsrc.plen) {
138+
rcu_read_lock();
139+
140+
l3mdev = dev_get_by_index_rcu(net, l3mdev_index);
141+
if (!f6i || !f6i->fib6_prefsrc.plen || l3mdev)
142+
dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
143+
same_vrf = !l3mdev || l3mdev_master_dev_rcu(dev) == l3mdev;
144+
if (f6i && f6i->fib6_prefsrc.plen && same_vrf)
136145
*saddr = f6i->fib6_prefsrc.addr;
137-
} else {
138-
struct net_device *dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
146+
else
147+
err = ipv6_dev_get_saddr(net, same_vrf ? dev : l3mdev, daddr, prefs, saddr);
139148

140-
err = ipv6_dev_get_saddr(net, dev, daddr, prefs, saddr);
141-
}
149+
rcu_read_unlock();
142150

143151
return err;
144152
}

net/ipv6/ip6_output.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
11241124
from = rt ? rcu_dereference(rt->from) : NULL;
11251125
err = ip6_route_get_saddr(net, from, &fl6->daddr,
11261126
sk ? READ_ONCE(inet6_sk(sk)->srcprefs) : 0,
1127+
fl6->flowi6_l3mdev,
11271128
&fl6->saddr);
11281129
rcu_read_unlock();
11291130

net/ipv6/route.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5689,7 +5689,7 @@ static int rt6_fill_node(struct net *net, struct sk_buff *skb,
56895689
goto nla_put_failure;
56905690
} else if (dest) {
56915691
struct in6_addr saddr_buf;
5692-
if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5692+
if (ip6_route_get_saddr(net, rt, dest, 0, 0, &saddr_buf) == 0 &&
56935693
nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
56945694
goto nla_put_failure;
56955695
}

0 commit comments

Comments
 (0)