From: luoshijie <luoshijie1@huawei.com>
To: <davem@davemloft.net>, <tgraf@suug.ch>, <dsahern@gmail.com>
Cc: <netdev@vger.kernel.org>, <liuzhiqiang26@huawei.com>,
<wangxiaogang3@huawei.com>, <mingfangsen@huawei.com>,
<zhoukang7@huawei.com>
Subject: [PATCH v2 1/3] ipv4: fix inet_select_addr() when enable route_localnet
Date: Tue, 18 Jun 2019 15:14:03 +0000 [thread overview]
Message-ID: <1560870845-172395-2-git-send-email-luoshijie1@huawei.com> (raw)
In-Reply-To: <1560870845-172395-1-git-send-email-luoshijie1@huawei.com>
From: Shijie Luo <luoshijie1@huawei.com>
Suppose we have two interfaces eth0 and eth1 in two hosts, follow
the same steps in the two hosts:
# sysctl -w net.ipv4.conf.eth1.route_localnet=1
# sysctl -w net.ipv4.conf.eth1.arp_announce=2
# ip route del 127.0.0.0/8 dev lo table local
and then set ip to eth1 in host1 like:
# ifconfig eth1 127.25.3.4/24
set ip to eth2 in host2 and ping host1:
# ifconfig eth1 127.25.3.14/24
# ping -I eth1 127.25.3.4
Well, host2 cannot connect to host1.
When set a ip address with head 127, the scope of the address defaults
to RT_SCOPE_HOST. In this situation, host2 will use arp_solicit() to
send a arp request for the mac address of host1 with ip
address 127.25.3.14. When arp_announce=2, inet_select_addr() cannot
select a correct saddr with condition ifa->ifa_scope > scope, because
ifa_scope is RT_SCOPE_HOST and scope is RT_SCOPE_LINK. Then,
inet_select_addr() will go to no_in_dev to lookup all interfaces to find
a primary ip and finally get the primary ip of eth0.
Here I add a localnet_scope defaults to RT_SCOPE_HOST, and when
route_localnet is enabled, this value changes to RT_SCOPE_LINK to make
inet_select_addr() find a correct primary ip as saddr of arp request.
Fixes: d0daebc3d622 ("ipv4: Add interface option to enable routing of 127.0.0.0/8")
Signed-off-by: Shijie Luo <luoshijie1@huawei.com>
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
net/ipv4/devinet.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index c6bd0f7a020a..08c6c7c41749 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -1254,6 +1254,7 @@ static __be32 in_dev_select_addr(const struct in_device *in_dev,
__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
{
__be32 addr = 0;
+ unsigned char localnet_scope = RT_SCOPE_HOST;
struct in_device *in_dev;
struct net *net = dev_net(dev);
int master_idx;
@@ -1263,8 +1264,11 @@ __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
if (!in_dev)
goto no_in_dev;
+ if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
+ localnet_scope = RT_SCOPE_LINK;
+
for_primary_ifa(in_dev) {
- if (ifa->ifa_scope > scope)
+ if (min(ifa->ifa_scope, localnet_scope) > scope)
continue;
if (!dst || inet_ifa_match(dst, ifa)) {
addr = ifa->ifa_local;
--
2.19.1
next prev parent reply other threads:[~2019-06-18 13:08 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-18 15:14 [PATCH v2 0/3] fix bugs when enable route_localnet luoshijie
2019-06-18 15:14 ` luoshijie [this message]
2019-06-18 15:14 ` [PATCH v2 2/3] ipv4: fix confirm_addr_indev() " luoshijie
2019-06-18 15:14 ` [PATCH v2 3/3] selftests: add route_localnet test script luoshijie
2019-06-22 8:41 ` [PATCH v2 0/3] fix bugs when enable route_localnet Zhiqiang Liu
2019-06-22 12:46 ` David Miller
2019-06-24 1:19 ` Zhiqiang Liu
2019-06-24 3:47 ` David Ahern
2019-06-24 16:03 ` David Miller
2019-06-25 1:25 ` Zhiqiang Liu
2019-06-25 1:50 ` Luoshijie (Poincare Lab)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1560870845-172395-2-git-send-email-luoshijie1@huawei.com \
--to=luoshijie1@huawei.com \
--cc=davem@davemloft.net \
--cc=dsahern@gmail.com \
--cc=liuzhiqiang26@huawei.com \
--cc=mingfangsen@huawei.com \
--cc=netdev@vger.kernel.org \
--cc=tgraf@suug.ch \
--cc=wangxiaogang3@huawei.com \
--cc=zhoukang7@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).