Skip to content

Commit 4ef1a7c

Browse files
lxindavem330
authored andcommitted
ipv6: some fixes for ipv6_dev_find()
This patch is to do 3 things for ipv6_dev_find(): As David A. noticed, - rt6_lookup() is not really needed. Different from __ip_dev_find(), ipv6_dev_find() doesn't have a compatibility problem, so remove it. As Hideaki suggested, - "valid" (non-tentative) check for the address is also needed. ipv6_chk_addr() calls ipv6_chk_addr_and_flags(), which will traverse the address hash list, but it's heavy to be called inside ipv6_dev_find(). This patch is to reuse the code of ipv6_chk_addr_and_flags() for ipv6_dev_find(). - dev parameter is passed into ipv6_dev_find(), as link-local addresses from user space has sin6_scope_id set and the dev lookup needs it. Fixes: 81f6cb3 ("ipv6: add ipv6_dev_find()") Suggested-by: YOSHIFUJI Hideaki <[email protected]> Reported-by: David Ahern <[email protected]> Signed-off-by: Xin Long <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 0410d07 commit 4ef1a7c

File tree

3 files changed

+28
-43
lines changed

3 files changed

+28
-43
lines changed

include/net/addrconf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
9797

9898
int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev);
9999

100-
struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr);
100+
struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
101+
struct net_device *dev);
101102

102103
struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net,
103104
const struct in6_addr *addr,

net/ipv6/addrconf.c

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,12 +1893,13 @@ EXPORT_SYMBOL(ipv6_chk_addr);
18931893
* 2. does the address exist on the specific device
18941894
* (skip_dev_check = false)
18951895
*/
1896-
int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1897-
const struct net_device *dev, bool skip_dev_check,
1898-
int strict, u32 banned_flags)
1896+
static struct net_device *
1897+
__ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1898+
const struct net_device *dev, bool skip_dev_check,
1899+
int strict, u32 banned_flags)
18991900
{
19001901
unsigned int hash = inet6_addr_hash(net, addr);
1901-
const struct net_device *l3mdev;
1902+
struct net_device *l3mdev, *ndev;
19021903
struct inet6_ifaddr *ifp;
19031904
u32 ifp_flags;
19041905

@@ -1909,10 +1910,11 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
19091910
dev = NULL;
19101911

19111912
hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1912-
if (!net_eq(dev_net(ifp->idev->dev), net))
1913+
ndev = ifp->idev->dev;
1914+
if (!net_eq(dev_net(ndev), net))
19131915
continue;
19141916

1915-
if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
1917+
if (l3mdev_master_dev_rcu(ndev) != l3mdev)
19161918
continue;
19171919

19181920
/* Decouple optimistic from tentative for evaluation here.
@@ -1923,15 +1925,23 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
19231925
: ifp->flags;
19241926
if (ipv6_addr_equal(&ifp->addr, addr) &&
19251927
!(ifp_flags&banned_flags) &&
1926-
(!dev || ifp->idev->dev == dev ||
1928+
(!dev || ndev == dev ||
19271929
!(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
19281930
rcu_read_unlock();
1929-
return 1;
1931+
return ndev;
19301932
}
19311933
}
19321934

19331935
rcu_read_unlock();
1934-
return 0;
1936+
return NULL;
1937+
}
1938+
1939+
int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1940+
const struct net_device *dev, bool skip_dev_check,
1941+
int strict, u32 banned_flags)
1942+
{
1943+
return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
1944+
strict, banned_flags) ? 1 : 0;
19351945
}
19361946
EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
19371947

@@ -1990,35 +2000,11 @@ EXPORT_SYMBOL(ipv6_chk_prefix);
19902000
*
19912001
* The caller should be protected by RCU, or RTNL.
19922002
*/
1993-
struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr)
2003+
struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
2004+
struct net_device *dev)
19942005
{
1995-
unsigned int hash = inet6_addr_hash(net, addr);
1996-
struct inet6_ifaddr *ifp, *result = NULL;
1997-
struct net_device *dev = NULL;
1998-
1999-
rcu_read_lock();
2000-
hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
2001-
if (net_eq(dev_net(ifp->idev->dev), net) &&
2002-
ipv6_addr_equal(&ifp->addr, addr)) {
2003-
result = ifp;
2004-
break;
2005-
}
2006-
}
2007-
2008-
if (!result) {
2009-
struct rt6_info *rt;
2010-
2011-
rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
2012-
if (rt) {
2013-
dev = rt->dst.dev;
2014-
ip6_rt_put(rt);
2015-
}
2016-
} else {
2017-
dev = result->idev->dev;
2018-
}
2019-
rcu_read_unlock();
2020-
2021-
return dev;
2006+
return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
2007+
IFA_F_TENTATIVE);
20222008
}
20232009
EXPORT_SYMBOL(ipv6_dev_find);
20242010

net/tipc/udp_media.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
660660
struct udp_tunnel_sock_cfg tuncfg = {NULL};
661661
struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
662662
u8 node_id[NODE_ID_LEN] = {0,};
663+
struct net_device *dev;
663664
int rmcast = 0;
664665

665666
ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
@@ -714,8 +715,6 @@ static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
714715
rcu_assign_pointer(ub->bearer, b);
715716
tipc_udp_media_addr_set(&b->addr, &local);
716717
if (local.proto == htons(ETH_P_IP)) {
717-
struct net_device *dev;
718-
719718
dev = __ip_dev_find(net, local.ipv4.s_addr, false);
720719
if (!dev) {
721720
err = -ENODEV;
@@ -738,9 +737,8 @@ static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
738737
b->mtu = b->media->mtu;
739738
#if IS_ENABLED(CONFIG_IPV6)
740739
} else if (local.proto == htons(ETH_P_IPV6)) {
741-
struct net_device *dev;
742-
743-
dev = ipv6_dev_find(net, &local.ipv6);
740+
dev = ub->ifindex ? __dev_get_by_index(net, ub->ifindex) : NULL;
741+
dev = ipv6_dev_find(net, &local.ipv6, dev);
744742
if (!dev) {
745743
err = -ENODEV;
746744
goto err;

0 commit comments

Comments
 (0)