Skip to content

Commit 8c0c07a

Browse files
committed
Merge branch 'vrf-next'
David Ahern says: ==================== net: vrf: Fix ipv6 source address selection IPv6 address selection is currently messed up for several use cases such as unnumbered deployments with global addresses on the VRF device and none on the enslaved devices. Update the source address selection to consider the real output route as opposed to the VRF route that sends packets to the VRF device first (ie., implement get_saddr6 similar to the IPv4 method) and update the IPv6 address selection to consider L3 domains and preference for addresses on the VRF device). ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 0023a06 + afbac60 commit 8c0c07a

7 files changed

Lines changed: 183 additions & 22 deletions

File tree

‎drivers/net/vrf.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,46 @@ static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
999999

10001000
return dst;
10011001
}
1002+
1003+
/* called under rcu_read_lock */
1004+
static int vrf_get_saddr6(struct net_device *dev, const struct sock *sk,
1005+
struct flowi6 *fl6)
1006+
{
1007+
struct net *net = dev_net(dev);
1008+
struct dst_entry *dst;
1009+
struct rt6_info *rt;
1010+
int err;
1011+
1012+
if (rt6_need_strict(&fl6->daddr)) {
1013+
rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif,
1014+
RT6_LOOKUP_F_IFACE);
1015+
if (unlikely(!rt))
1016+
return 0;
1017+
1018+
dst = &rt->dst;
1019+
} else {
1020+
__u8 flags = fl6->flowi6_flags;
1021+
1022+
fl6->flowi6_flags |= FLOWI_FLAG_L3MDEV_SRC;
1023+
fl6->flowi6_flags |= FLOWI_FLAG_SKIP_NH_OIF;
1024+
1025+
dst = ip6_route_output(net, sk, fl6);
1026+
rt = (struct rt6_info *)dst;
1027+
1028+
fl6->flowi6_flags = flags;
1029+
}
1030+
1031+
err = dst->error;
1032+
if (!err) {
1033+
err = ip6_route_get_saddr(net, rt, &fl6->daddr,
1034+
sk ? inet6_sk(sk)->srcprefs : 0,
1035+
&fl6->saddr);
1036+
}
1037+
1038+
dst_release(dst);
1039+
1040+
return err;
1041+
}
10021042
#endif
10031043

10041044
static const struct l3mdev_ops vrf_l3mdev_ops = {
@@ -1008,6 +1048,7 @@ static const struct l3mdev_ops vrf_l3mdev_ops = {
10081048
.l3mdev_l3_rcv = vrf_l3_rcv,
10091049
#if IS_ENABLED(CONFIG_IPV6)
10101050
.l3mdev_get_rt6_dst = vrf_get_rt6_dst,
1051+
.l3mdev_get_saddr6 = vrf_get_saddr6,
10111052
#endif
10121053
};
10131054

‎include/net/ip6_route.h‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct route_info {
1818
__u8 prefix[0]; /* 0,8 or 16 */
1919
};
2020

21+
#include <net/addrconf.h>
2122
#include <net/flow.h>
2223
#include <net/ip6_fib.h>
2324
#include <net/sock.h>
@@ -88,9 +89,23 @@ int ip6_route_add(struct fib6_config *cfg);
8889
int ip6_ins_rt(struct rt6_info *);
8990
int ip6_del_rt(struct rt6_info *);
9091

91-
int ip6_route_get_saddr(struct net *net, struct rt6_info *rt,
92-
const struct in6_addr *daddr, unsigned int prefs,
93-
struct in6_addr *saddr);
92+
static inline int ip6_route_get_saddr(struct net *net, struct rt6_info *rt,
93+
const struct in6_addr *daddr,
94+
unsigned int prefs,
95+
struct in6_addr *saddr)
96+
{
97+
struct inet6_dev *idev =
98+
rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
99+
int err = 0;
100+
101+
if (rt && rt->rt6i_prefsrc.plen)
102+
*saddr = rt->rt6i_prefsrc.addr;
103+
else
104+
err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
105+
daddr, prefs, saddr);
106+
107+
return err;
108+
}
94109

95110
struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
96111
const struct in6_addr *saddr, int oif, int flags);

‎include/net/l3mdev.h‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ struct l3mdev_ops {
3939
/* IPv6 ops */
4040
struct dst_entry * (*l3mdev_get_rt6_dst)(const struct net_device *dev,
4141
struct flowi6 *fl6);
42+
int (*l3mdev_get_saddr6)(struct net_device *dev,
43+
const struct sock *sk,
44+
struct flowi6 *fl6);
4245
};
4346

4447
#ifdef CONFIG_NET_L3_MASTER_DEV
@@ -76,6 +79,31 @@ static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
7679
return rc;
7780
}
7881

82+
static inline
83+
const struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
84+
{
85+
/* netdev_master_upper_dev_get_rcu calls
86+
* list_first_or_null_rcu to walk the upper dev list.
87+
* list_first_or_null_rcu does not handle a const arg. We aren't
88+
* making changes, just want the master device from that list so
89+
* typecast to remove the const
90+
*/
91+
struct net_device *dev = (struct net_device *)_dev;
92+
const struct net_device *master;
93+
94+
if (!dev)
95+
return NULL;
96+
97+
if (netif_is_l3_master(dev))
98+
master = dev;
99+
else if (netif_is_l3_slave(dev))
100+
master = netdev_master_upper_dev_get_rcu(dev);
101+
else
102+
master = NULL;
103+
104+
return master;
105+
}
106+
79107
/* get index of an interface to use for FIB lookups. For devices
80108
* enslaved to an L3 master device FIB lookups are based on the
81109
* master index
@@ -140,6 +168,8 @@ static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
140168
int l3mdev_get_saddr(struct net *net, int ifindex, struct flowi4 *fl4);
141169

142170
struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6);
171+
int l3mdev_get_saddr6(struct net *net, const struct sock *sk,
172+
struct flowi6 *fl6);
143173

144174
static inline
145175
struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
@@ -185,6 +215,12 @@ static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
185215
return 0;
186216
}
187217

218+
static inline
219+
const struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
220+
{
221+
return NULL;
222+
}
223+
188224
static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
189225
{
190226
return dev ? dev->ifindex : 0;
@@ -230,6 +266,12 @@ struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6)
230266
return NULL;
231267
}
232268

269+
static inline int l3mdev_get_saddr6(struct net *net, const struct sock *sk,
270+
struct flowi6 *fl6)
271+
{
272+
return 0;
273+
}
274+
233275
static inline
234276
struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
235277
{

‎net/ipv6/addrconf.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,28 @@ static int __ipv6_dev_get_saddr(struct net *net,
15241524
return hiscore_idx;
15251525
}
15261526

1527+
static int ipv6_get_saddr_master(struct net *net,
1528+
const struct net_device *dst_dev,
1529+
const struct net_device *master,
1530+
struct ipv6_saddr_dst *dst,
1531+
struct ipv6_saddr_score *scores,
1532+
int hiscore_idx)
1533+
{
1534+
struct inet6_dev *idev;
1535+
1536+
idev = __in6_dev_get(dst_dev);
1537+
if (idev)
1538+
hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1539+
scores, hiscore_idx);
1540+
1541+
idev = __in6_dev_get(master);
1542+
if (idev)
1543+
hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1544+
scores, hiscore_idx);
1545+
1546+
return hiscore_idx;
1547+
}
1548+
15271549
int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
15281550
const struct in6_addr *daddr, unsigned int prefs,
15291551
struct in6_addr *saddr)
@@ -1577,13 +1599,39 @@ int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
15771599
if (idev)
15781600
hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15791601
} else {
1602+
const struct net_device *master;
1603+
int master_idx = 0;
1604+
1605+
/* if dst_dev exists and is enslaved to an L3 device, then
1606+
* prefer addresses from dst_dev and then the master over
1607+
* any other enslaved devices in the L3 domain.
1608+
*/
1609+
master = l3mdev_master_dev_rcu(dst_dev);
1610+
if (master) {
1611+
master_idx = master->ifindex;
1612+
1613+
hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1614+
master, &dst,
1615+
scores, hiscore_idx);
1616+
1617+
if (scores[hiscore_idx].ifa)
1618+
goto out;
1619+
}
1620+
15801621
for_each_netdev_rcu(net, dev) {
1622+
/* only consider addresses on devices in the
1623+
* same L3 domain
1624+
*/
1625+
if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1626+
continue;
15811627
idev = __in6_dev_get(dev);
15821628
if (!idev)
15831629
continue;
15841630
hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15851631
}
15861632
}
1633+
1634+
out:
15871635
rcu_read_unlock();
15881636

15891637
hiscore = &scores[hiscore_idx];

‎net/ipv6/ip6_output.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,13 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
910910
int err;
911911
int flags = 0;
912912

913+
if (ipv6_addr_any(&fl6->saddr) && fl6->flowi6_oif &&
914+
(!*dst || !(*dst)->error)) {
915+
err = l3mdev_get_saddr6(net, sk, fl6);
916+
if (err)
917+
goto out_err;
918+
}
919+
913920
/* The correct way to handle this would be to do
914921
* ip6_route_get_saddr, and then ip6_route_output; however,
915922
* the route-specific preferred source forces the
@@ -999,10 +1006,11 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
9991006
return 0;
10001007

10011008
out_err_release:
1002-
if (err == -ENETUNREACH)
1003-
IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
10041009
dst_release(*dst);
10051010
*dst = NULL;
1011+
out_err:
1012+
if (err == -ENETUNREACH)
1013+
IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
10061014
return err;
10071015
}
10081016

‎net/ipv6/route.c‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,23 +2586,6 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
25862586
return rt;
25872587
}
25882588

2589-
int ip6_route_get_saddr(struct net *net,
2590-
struct rt6_info *rt,
2591-
const struct in6_addr *daddr,
2592-
unsigned int prefs,
2593-
struct in6_addr *saddr)
2594-
{
2595-
struct inet6_dev *idev =
2596-
rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
2597-
int err = 0;
2598-
if (rt && rt->rt6i_prefsrc.plen)
2599-
*saddr = rt->rt6i_prefsrc.addr;
2600-
else
2601-
err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2602-
daddr, prefs, saddr);
2603-
return err;
2604-
}
2605-
26062589
/* remove deleted ip from prefsrc entries */
26072590
struct arg_dev_net_ip {
26082591
struct net_device *dev;

‎net/l3mdev/l3mdev.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ int l3mdev_get_saddr(struct net *net, int ifindex, struct flowi4 *fl4)
162162
}
163163
EXPORT_SYMBOL_GPL(l3mdev_get_saddr);
164164

165+
int l3mdev_get_saddr6(struct net *net, const struct sock *sk,
166+
struct flowi6 *fl6)
167+
{
168+
struct net_device *dev;
169+
int rc = 0;
170+
171+
if (fl6->flowi6_oif) {
172+
rcu_read_lock();
173+
174+
dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
175+
if (dev && netif_is_l3_slave(dev))
176+
dev = netdev_master_upper_dev_get_rcu(dev);
177+
178+
if (dev && netif_is_l3_master(dev) &&
179+
dev->l3mdev_ops->l3mdev_get_saddr6)
180+
rc = dev->l3mdev_ops->l3mdev_get_saddr6(dev, sk, fl6);
181+
182+
rcu_read_unlock();
183+
}
184+
185+
return rc;
186+
}
187+
EXPORT_SYMBOL_GPL(l3mdev_get_saddr6);
188+
165189
/**
166190
* l3mdev_fib_rule_match - Determine if flowi references an
167191
* L3 master device

0 commit comments

Comments
 (0)