Netdev Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2 0/3] fix bugs when enable route_localnet
@ 2019-06-18 15:14 luoshijie
  2019-06-18 15:14 ` [PATCH v2 1/3] ipv4: fix inet_select_addr() " luoshijie
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: luoshijie @ 2019-06-18 15:14 UTC (permalink / raw)
  To: davem, tgraf, dsahern
  Cc: netdev, liuzhiqiang26, wangxiaogang3, mingfangsen, zhoukang7

From: Shijie Luo <luoshijie1@huawei.com>

When enable route_localnet, route of the 127/8 address is enabled.
But in some situations like arp_announce=2, ARP requests or reply
work abnormally.

This patchset fix some bugs when enable route_localnet. 

Change History:
V2:
- Change a single patch to a patchset.
- Add bug fix for arp_ignore = 3.
- Add a couple of test for enabling route_localnet in selftests.

Shijie Luo (3):
  ipv4: fix inet_select_addr() when enable route_localnet
  ipv4: fix confirm_addr_indev() when enable route_localnet
  selftests: add route_localnet test script

 net/ipv4/devinet.c                            | 15 +++-
 tools/testing/selftests/net/route_localnet.sh | 74 +++++++++++++++++++
 2 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100755 tools/testing/selftests/net/route_localnet.sh

-- 
2.19.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/3] ipv4: fix inet_select_addr() when enable route_localnet
  2019-06-18 15:14 [PATCH v2 0/3] fix bugs when enable route_localnet luoshijie
@ 2019-06-18 15:14 ` " luoshijie
  2019-06-18 15:14 ` [PATCH v2 2/3] ipv4: fix confirm_addr_indev() " luoshijie
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 11+ messages in thread
From: luoshijie @ 2019-06-18 15:14 UTC (permalink / raw)
  To: davem, tgraf, dsahern
  Cc: netdev, liuzhiqiang26, wangxiaogang3, mingfangsen, zhoukang7

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 2/3] ipv4: fix confirm_addr_indev() when enable route_localnet
  2019-06-18 15:14 [PATCH v2 0/3] fix bugs when enable route_localnet luoshijie
  2019-06-18 15:14 ` [PATCH v2 1/3] ipv4: fix inet_select_addr() " luoshijie
@ 2019-06-18 15:14 ` " luoshijie
  2019-06-18 15:14 ` [PATCH v2 3/3] selftests: add route_localnet test script luoshijie
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 11+ messages in thread
From: luoshijie @ 2019-06-18 15:14 UTC (permalink / raw)
  To: davem, tgraf, dsahern
  Cc: netdev, liuzhiqiang26, wangxiaogang3, mingfangsen, zhoukang7

From: Shijie Luo <luoshijie1@huawei.com>

When arp_ignore=3, the NIC won't reply for scope host addresses, but
if enable route_locanet, we need to reply ip address with head 127 and
scope RT_SCOPE_HOST.

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 | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index 08c6c7c41749..cfef8df59373 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -1321,13 +1321,18 @@ EXPORT_SYMBOL(inet_select_addr);
 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
 			      __be32 local, int scope)
 {
+	unsigned char localnet_scope = RT_SCOPE_HOST;
 	int same = 0;
 	__be32 addr = 0;
 
+	if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
+		localnet_scope = RT_SCOPE_LINK;
+
 	for_ifa(in_dev) {
+		unsigned char min_scope = min(ifa->ifa_scope, localnet_scope);
 		if (!addr &&
 		    (local == ifa->ifa_local || !local) &&
-		    ifa->ifa_scope <= scope) {
+		    min_scope <= scope) {
 			addr = ifa->ifa_local;
 			if (same)
 				break;
@@ -1342,7 +1347,7 @@ static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
 				if (inet_ifa_match(addr, ifa))
 					break;
 				/* No, then can we use new local src? */
-				if (ifa->ifa_scope <= scope) {
+				if (min_scope <= scope) {
 					addr = ifa->ifa_local;
 					break;
 				}
-- 
2.19.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 3/3] selftests: add route_localnet test script
  2019-06-18 15:14 [PATCH v2 0/3] fix bugs when enable route_localnet luoshijie
  2019-06-18 15:14 ` [PATCH v2 1/3] ipv4: fix inet_select_addr() " luoshijie
  2019-06-18 15:14 ` [PATCH v2 2/3] ipv4: fix confirm_addr_indev() " luoshijie
@ 2019-06-18 15:14 ` luoshijie
  2019-06-22  8:41 ` [PATCH v2 0/3] fix bugs when enable route_localnet Zhiqiang Liu
  2019-06-24 16:03 ` David Miller
  4 siblings, 0 replies; 11+ messages in thread
From: luoshijie @ 2019-06-18 15:14 UTC (permalink / raw)
  To: davem, tgraf, dsahern
  Cc: netdev, liuzhiqiang26, wangxiaogang3, mingfangsen, zhoukang7

From: Shijie Luo <luoshijie1@huawei.com>

Add a simple scripts to exercise several situations when enable
route_localnet.

Signed-off-by: Shijie Luo <luoshijie1@huawei.com>
Signed-off-by: Zhiqiang liu <liuzhiqiang26@huawei.com>
---
 tools/testing/selftests/net/route_localnet.sh | 74 +++++++++++++++++++
 1 file changed, 74 insertions(+)
 create mode 100755 tools/testing/selftests/net/route_localnet.sh

diff --git a/tools/testing/selftests/net/route_localnet.sh b/tools/testing/selftests/net/route_localnet.sh
new file mode 100755
index 000000000000..116bfeab72fa
--- /dev/null
+++ b/tools/testing/selftests/net/route_localnet.sh
@@ -0,0 +1,74 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Run a couple of tests when route_localnet = 1.
+
+readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
+
+setup() {
+    ip netns add "${PEER_NS}"
+    ip -netns "${PEER_NS}" link set dev lo up
+    ip link add name veth0 type veth peer name veth1
+    ip link set dev veth0 up
+    ip link set dev veth1 netns "${PEER_NS}"
+
+    # Enable route_localnet and delete useless route 127.0.0.0/8.
+    sysctl -w net.ipv4.conf.veth0.route_localnet=1
+    ip netns exec "${PEER_NS}" sysctl -w net.ipv4.conf.veth1.route_localnet=1
+    ip route del 127.0.0.0/8 dev lo table local
+    ip netns exec "${PEER_NS}" ip route del 127.0.0.0/8 dev lo table local
+
+    ifconfig veth0 127.25.3.4/24 up
+    ip netns exec "${PEER_NS}" ifconfig veth1 127.25.3.14/24 up
+
+    ip route flush cache
+    ip netns exec "${PEER_NS}" ip route flush cache
+}
+
+cleanup() {
+    ip link del veth0
+    ip route add local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
+    local -r ns="$(ip netns list|grep $PEER_NS)"
+    [ -n "$ns" ] && ip netns del $ns 2>/dev/null
+}
+
+# Run test when arp_announce = 2.
+run_arp_announce_test() {
+    echo "run arp_announce test"
+    setup
+
+    sysctl -w net.ipv4.conf.veth0.arp_announce=2
+    ip netns exec "${PEER_NS}" sysctl -w net.ipv4.conf.veth1.arp_announce=2
+    ping -c5 -I veth0 127.25.3.14
+    if [ $? -ne 0 ];then
+        echo "failed"
+    else
+        echo "ok"
+    fi
+
+    cleanup
+}
+
+# Run test when arp_ignore = 3.
+run_arp_ignore_test() {
+    echo "run arp_ignore test"
+    setup
+
+    sysctl -w net.ipv4.conf.veth0.arp_ignore=3
+    ip netns exec "${PEER_NS}" sysctl -w net.ipv4.conf.veth1.arp_ignore=3
+    ping -c5 -I veth0 127.25.3.14
+    if [ $? -ne 0 ];then
+        echo "failed"
+    else
+        echo "ok"
+    fi
+
+    cleanup
+}
+
+run_all_tests() {
+    run_arp_announce_test
+    run_arp_ignore_test
+}
+
+run_all_tests
-- 
2.19.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/3] fix bugs when enable route_localnet
  2019-06-18 15:14 [PATCH v2 0/3] fix bugs when enable route_localnet luoshijie
                   ` (2 preceding siblings ...)
  2019-06-18 15:14 ` [PATCH v2 3/3] selftests: add route_localnet test script luoshijie
@ 2019-06-22  8:41 ` Zhiqiang Liu
  2019-06-22 12:46   ` David Miller
  2019-06-24 16:03 ` David Miller
  4 siblings, 1 reply; 11+ messages in thread
From: Zhiqiang Liu @ 2019-06-22  8:41 UTC (permalink / raw)
  To: luoshijie, davem, tgraf, dsahern
  Cc: netdev, wangxiaogang3, mingfangsen, zhoukang7

Friendly ping ...


> From: Shijie Luo <luoshijie1@huawei.com>
> 
> When enable route_localnet, route of the 127/8 address is enabled.
> But in some situations like arp_announce=2, ARP requests or reply
> work abnormally.
> 
> This patchset fix some bugs when enable route_localnet. 
> 
> Change History:
> V2:
> - Change a single patch to a patchset.
> - Add bug fix for arp_ignore = 3.
> - Add a couple of test for enabling route_localnet in selftests.
> 
> Shijie Luo (3):
>   ipv4: fix inet_select_addr() when enable route_localnet
>   ipv4: fix confirm_addr_indev() when enable route_localnet
>   selftests: add route_localnet test script
> 
>  net/ipv4/devinet.c                            | 15 +++-
>  tools/testing/selftests/net/route_localnet.sh | 74 +++++++++++++++++++
>  2 files changed, 86 insertions(+), 3 deletions(-)
>  create mode 100755 tools/testing/selftests/net/route_localnet.sh
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/3] fix bugs when enable route_localnet
  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
  0 siblings, 2 replies; 11+ messages in thread
From: David Miller @ 2019-06-22 12:46 UTC (permalink / raw)
  To: liuzhiqiang26
  Cc: luoshijie1, tgraf, dsahern, netdev, wangxiaogang3, mingfangsen,
	zhoukang7

From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Date: Sat, 22 Jun 2019 16:41:49 +0800

> Friendly ping ...

I'm not applying this patch series without someone reviewing it.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/3] fix bugs when enable route_localnet
  2019-06-22 12:46   ` David Miller
@ 2019-06-24  1:19     ` Zhiqiang Liu
  2019-06-24  3:47     ` David Ahern
  1 sibling, 0 replies; 11+ messages in thread
From: Zhiqiang Liu @ 2019-06-24  1:19 UTC (permalink / raw)
  To: David Miller
  Cc: luoshijie1, tgraf, dsahern, netdev, wangxiaogang3, mingfangsen,
	zhoukang7

> From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Date: Sat, 22 Jun 2019 16:41:49 +0800
> 
>> Friendly ping ...
> 
> I'm not applying this patch series without someone reviewing it.
> 
Of course, all patches should be reviewd before deciding whether to apply.
In v2, we add a couple of test for enabling route_localnet in selftests suggested
by David Ahern.
In additon, another similar bugfix is added for arp_ignore = 3.

We would appreciate David Ahern or someone could help review the patch series.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/3] fix bugs when enable route_localnet
  2019-06-22 12:46   ` David Miller
  2019-06-24  1:19     ` Zhiqiang Liu
@ 2019-06-24  3:47     ` David Ahern
  1 sibling, 0 replies; 11+ messages in thread
From: David Ahern @ 2019-06-24  3:47 UTC (permalink / raw)
  To: David Miller, liuzhiqiang26
  Cc: luoshijie1, tgraf, netdev, wangxiaogang3, mingfangsen, zhoukang7

On 6/22/19 6:46 AM, David Miller wrote:
> From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Date: Sat, 22 Jun 2019 16:41:49 +0800
> 
>> Friendly ping ...
> 
> I'm not applying this patch series without someone reviewing it.
> 

I have stared at it a few times since the patches were sent and can not
find anything obviously wrong about it. The fallout seems limited to
users of route_localnet which I have to believe is small (I only know of
2 other users of 127/8 for non-loopback and those were almost 10 years ago).

Putting in net-next is the safest.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/3] fix bugs when enable route_localnet
  2019-06-18 15:14 [PATCH v2 0/3] fix bugs when enable route_localnet luoshijie
                   ` (3 preceding siblings ...)
  2019-06-22  8:41 ` [PATCH v2 0/3] fix bugs when enable route_localnet Zhiqiang Liu
@ 2019-06-24 16:03 ` David Miller
  2019-06-25  1:25   ` Zhiqiang Liu
  2019-06-25  1:50   ` Luoshijie (Poincare Lab)
  4 siblings, 2 replies; 11+ messages in thread
From: David Miller @ 2019-06-24 16:03 UTC (permalink / raw)
  To: luoshijie1
  Cc: tgraf, dsahern, netdev, liuzhiqiang26, wangxiaogang3,
	mingfangsen, zhoukang7

From: luoshijie <luoshijie1@huawei.com>
Date: Tue, 18 Jun 2019 15:14:02 +0000

> From: Shijie Luo <luoshijie1@huawei.com>
> 
> When enable route_localnet, route of the 127/8 address is enabled.
> But in some situations like arp_announce=2, ARP requests or reply
> work abnormally.
> 
> This patchset fix some bugs when enable route_localnet. 
> 
> Change History:
> V2:
> - Change a single patch to a patchset.
> - Add bug fix for arp_ignore = 3.
> - Add a couple of test for enabling route_localnet in selftests.

Series applied to net-next, thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/3] fix bugs when enable route_localnet
  2019-06-24 16:03 ` David Miller
@ 2019-06-25  1:25   ` Zhiqiang Liu
  2019-06-25  1:50   ` Luoshijie (Poincare Lab)
  1 sibling, 0 replies; 11+ messages in thread
From: Zhiqiang Liu @ 2019-06-25  1:25 UTC (permalink / raw)
  To: David Miller, luoshijie1
  Cc: tgraf, dsahern, netdev, wangxiaogang3, mingfangsen, zhoukang7

[-- Warning: decoded text below may be mangled --]
[-- Attachment #0: Type: text/plain; charset="gb18030", Size: 654 bytes --]



On 2019/6/25 0:03, David Miller wrote:
> From: luoshijie <luoshijie1@huawei.com>
> Date: Tue, 18 Jun 2019 15:14:02 +0000
> 
>> From: Shijie Luo <luoshijie1@huawei.com>
>>
>> When enable route_localnet, route of the 127/8 address is enabled.
>> But in some situations like arp_announce=2, ARP requests or reply
>> work abnormally.
>>
>> This patchset fix some bugs when enable route_localnet. 
>>
>> Change History:
>> V2:
>> - Change a single patch to a patchset.
>> - Add bug fix for arp_ignore = 3.
>> - Add a couple of test for enabling route_localnet in selftests.
> 
> Series applied to net-next, thanks.
> 
Thanks again for you and David Ahern.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/3] fix bugs when enable route_localnet
  2019-06-24 16:03 ` David Miller
  2019-06-25  1:25   ` Zhiqiang Liu
@ 2019-06-25  1:50   ` Luoshijie (Poincare Lab)
  1 sibling, 0 replies; 11+ messages in thread
From: Luoshijie (Poincare Lab) @ 2019-06-25  1:50 UTC (permalink / raw)
  To: David Miller
  Cc: tgraf, dsahern, netdev, liuzhiqiang26, wangxiaogang3,
	mingfangsen, zhoukang7

On 2019/6/25 0:03, David Miller wrote:
> From: luoshijie <luoshijie1@huawei.com>
> Date: Tue, 18 Jun 2019 15:14:02 +0000
> 
>> From: Shijie Luo <luoshijie1@huawei.com>
>>
>> When enable route_localnet, route of the 127/8 address is enabled.
>> But in some situations like arp_announce=2, ARP requests or reply
>> work abnormally.
>>
>> This patchset fix some bugs when enable route_localnet. 
>>
>> Change History:
>> V2:
>> - Change a single patch to a patchset.
>> - Add bug fix for arp_ignore = 3.
>> - Add a couple of test for enabling route_localnet in selftests.
> 
> Series applied to net-next, thanks.
> 

Thanks a lot, and I'm truly grateful for advice and help of David Ahern.


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, back to index

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-18 15:14 [PATCH v2 0/3] fix bugs when enable route_localnet luoshijie
2019-06-18 15:14 ` [PATCH v2 1/3] ipv4: fix inet_select_addr() " luoshijie
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)

Netdev Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/netdev/0 netdev/git/0.git
	git clone --mirror https://lore.kernel.org/netdev/1 netdev/git/1.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 netdev netdev/ https://lore.kernel.org/netdev \
		netdev@vger.kernel.org netdev@archiver.kernel.org
	public-inbox-index netdev


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.netdev


AGPL code for this site: git clone https://public-inbox.org/ public-inbox