Skip to contentSkip to conversation

Netfilter fixes for net

PATCH Applied Pablo Neira Ayuso 2 participants 13 messages Nov 18, 2021 – Nov 19, 2021

Subsystems (6)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover Netfilter fixes for net

Hi,

The following patchset contains Netfilter/IPVS fixes for net:

1) Add selftest for vrf+conntrack, from Florian Westphal.

2) Extend nfqueue selftest to cover nfqueue, also from Florian.

3) Remove duplicated include in nft_payload, from Wan Jiabing.

4) Several improvements to the nat port shadowing selftest, from Phil Sutter.

5) Fix filtering of reply tuple in ctnetlink, from Florent Fourcot.

6) Do not override error with -EINVAL in filter setup path, also from Florent.

7) Honor sysctl_expire_nodest_conn regardless conn_reuse_mode for reused connections, from yangxingwu.

8) Replace snprintf() by sysfs_emit() in xt_IDLETIMER as reported by Coccinelle, from Jing Yao.

9) Incorrect IPv6 tunnel match in flowtable offload, from Will Mortensen.

10) Switch port shadow selftest to use socat, from Florian Westphal.

Please, pull these changes from:

git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git

Thanks.

----------------------------------------------------------------

The following changes since commit c45231a7668d6b632534f692b10592ea375b55b0:

litex_liteeth: Fix a double free in the remove function (2021-11-07 21:51:17 +0000)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git HEAD

for you to fetch changes up to a2acf0c0e2da29950d0361a3b5ea05e8d0351dfe:

selftests: nft_nat: switch port shadow test cases to socat (2021-11-15 12:02:11 +0100)

----------------------------------------------------------------
Florent Fourcot (2):
netfilter: ctnetlink: fix filtering with CTA_TUPLE_REPLY
netfilter: ctnetlink: do not erase error code with EINVAL

Florian Westphal (3):
selftests: netfilter: add a vrf+conntrack testcase
selftests: netfilter: extend nfqueue tests to cover vrf device
selftests: nft_nat: switch port shadow test cases to socat

Jing Yao (1):
netfilter: xt_IDLETIMER: replace snprintf in show functions with sysfs_emit

Phil Sutter (2):
selftests: nft_nat: Improve port shadow test stability
selftests: nft_nat: Simplify port shadow notrack test

Wan Jiabing (1):
netfilter: nft_payload: Remove duplicated include in nft_payload.c

Will Mortensen (1):
netfilter: flowtable: fix IPv6 tunnel addr match

yangxingwu (1):
netfilter: ipvs: Fix reuse connection if RS weight is 0

 Documentation/networking/ipvs-sysctl.rst           |   3 +-
 net/netfilter/ipvs/ip_vs_core.c                    |   8 +-
 net/netfilter/nf_conntrack_netlink.c               |   6 +-
 net/netfilter/nf_flow_table_offload.c              |   4 +-
 net/netfilter/nft_payload.c                        |   1 -
 net/netfilter/xt_IDLETIMER.c                       |   4 +-
 tools/testing/selftests/netfilter/Makefile         |   3 +-
 tools/testing/selftests/netfilter/conntrack_vrf.sh | 219 +++++++++++++++++++++
 tools/testing/selftests/netfilter/nft_nat.sh       |  33 +++-
 tools/testing/selftests/netfilter/nft_queue.sh     |  54 +++++
 10 files changed, 309 insertions(+), 26 deletions(-)
 create mode 100755 tools/testing/selftests/netfilter/conntrack_vrf.sh
author maintainer
#1 lore raw

PATCH 1/11 selftests: netfilter: add a vrf+conntrack testcase

From: Florian Westphal <[email protected]>

Rework the reproducer for the vrf+conntrack regression reported by Eugene into a selftest and also add a test for ip masquerading that Lahav fixed recently.

With net or net-next tree, the first test fails and the latter two pass.

With 09e856d54bda5f28 ("vrf: Reset skb conntrack connection on VRF rcv") reverted first test passes but the last two fail.

A proper fix needs more work, for time being a revert seems to be the best choice, snat/masquerade did not work before the fix.

---
 tools/testing/selftests/netfilter/Makefile    |   3 +-
 .../selftests/netfilter/conntrack_vrf.sh      | 219 ++++++++++++++++++
 2 files changed, 221 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/netfilter/conntrack_vrf.sh
tools/testing/selftests/netfilter/Makefile +2 -1
index 8748199ac109..ffca314897c4 100644
@@ -5,7 +5,8 @@ TEST_PROGS := nft_trans_stress.sh nft_fib.sh nft_nat.sh bridge_brouter.sh \
 	conntrack_icmp_related.sh nft_flowtable.sh ipvs.sh \
 	nft_concat_range.sh nft_conntrack_helper.sh \
 	nft_queue.sh nft_meta.sh nf_nat_edemux.sh \
-	ipip-conntrack-mtu.sh conntrack_tcp_unreplied.sh
+	ipip-conntrack-mtu.sh conntrack_tcp_unreplied.sh \
+	conntrack_vrf.sh
 
 LDLIBS = -lmnl
 TEST_GEN_FILES =  nf-queue
tools/testing/selftests/netfilter/conntrack_vrf.sh new file +219 -0

219 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH net 01/11] selftests: netfilter: add a vrf+conntrack testcase

This series was applied to netdev/net.git (master) by Pablo Neira Ayuso <[email protected]>:

Hello:

This series was applied to netdev/net.git (master) by Pablo Neira Ayuso <[email protected]>:

Quoted 10 lines · On Thu, 18 Nov 2021 23:26:08 +0100 you wrote:

From: Florian Westphal <[email protected]>

Rework the reproducer for the vrf+conntrack regression reported by Eugene into a selftest and also add a test for ip masquerading that Lahav fixed recently.

With net or net-next tree, the first test fails and the latter two pass.

[...]

Here is the summary with links:
- [net,01/11] selftests: netfilter: add a vrf+conntrack testcase

    https://git.kernel.org/netdev/net/c/33b8aad21ac1
  - [net,02/11] selftests: netfilter: extend nfqueue tests to cover vrf device
    https://git.kernel.org/netdev/net/c/228c3fa054ad
  - [net,03/11] netfilter: nft_payload: Remove duplicated include in nft_payload.c
    https://git.kernel.org/netdev/net/c/00d8b83725e9
  - [net,04/11] selftests: nft_nat: Improve port shadow test stability
    https://git.kernel.org/netdev/net/c/e1f8bc06e497
  - [net,05/11] selftests: nft_nat: Simplify port shadow notrack test
    https://git.kernel.org/netdev/net/c/85c0c8b342e8
  - [net,06/11] netfilter: ctnetlink: fix filtering with CTA_TUPLE_REPLY
    https://git.kernel.org/netdev/net/c/ad81d4daf6a3
  - [net,07/11] netfilter: ctnetlink: do not erase error code with EINVAL
    https://git.kernel.org/netdev/net/c/77522ff02f33
  - [net,08/11] netfilter: ipvs: Fix reuse connection if RS weight is 0
    https://git.kernel.org/netdev/net/c/c95c07836fa4
  - [net,09/11] netfilter: xt_IDLETIMER: replace snprintf in show functions with sysfs_emit
    https://git.kernel.org/netdev/net/c/c08d3286caf1
  - [net,10/11] netfilter: flowtable: fix IPv6 tunnel addr match
    https://git.kernel.org/netdev/net/c/39f6eed4cb20
  - [net,11/11] selftests: nft_nat: switch port shadow test cases to socat
    https://git.kernel.org/netdev/net/c/a2acf0c0e2da

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
author maintainer
#3 lore raw

PATCH 2/11 selftests: netfilter: extend nfqueue tests to cover vrf device

In reply to Pablo Neira Ayuso

From: Florian Westphal <[email protected]>

VRF device calls the output/postrouting hooks so packet should be seeon with oifname tvrf and once with eth0.

---
 .../testing/selftests/netfilter/nft_queue.sh  | 54 +++++++++++++++++++
 1 file changed, 54 insertions(+)
tools/testing/selftests/netfilter/nft_queue.sh +54 -0
index 3d202b90b33d..7d27f1f3bc01 100755
@@ -16,6 +16,10 @@ timeout=4
 
 cleanup()
 {
+	ip netns pids ${ns1} | xargs kill 2>/dev/null
+	ip netns pids ${ns2} | xargs kill 2>/dev/null
+	ip netns pids ${nsrouter} | xargs kill 2>/dev/null
+
 	ip netns del ${ns1}
 	ip netns del ${ns2}
 	ip netns del ${nsrouter}
@@ -332,6 +336,55 @@ EOF
 	echo "PASS: tcp via loopback and re-queueing"
 }
 
+test_icmp_vrf() {
+	ip -net $ns1 link add tvrf type vrf table 9876
+	if [ $? -ne 0 ];then
+		echo "SKIP: Could not add vrf device"
+		return
+	fi
+
+	ip -net $ns1 li set eth0 master tvrf
+	ip -net $ns1 li set tvrf up
+
+	ip -net $ns1 route add 10.0.2.0/24 via 10.0.1.1 dev eth0 table 9876
+ip netns exec ${ns1} nft -f /dev/stdin <<EOF
+flush ruleset
+table inet filter {
+	chain output {
+		type filter hook output priority 0; policy accept;
+		meta oifname "tvrf" icmp type echo-request counter queue num 1
+		meta oifname "eth0" icmp type echo-request counter queue num 1
+	}
+	chain post {
+		type filter hook postrouting priority 0; policy accept;
+		meta oifname "tvrf" icmp type echo-request counter queue num 1
+		meta oifname "eth0" icmp type echo-request counter queue num 1
+	}
+}
+EOF
+	ip netns exec ${ns1} ./nf-queue -q 1 -t $timeout &
+	local nfqpid=$!
+
+	sleep 1
+	ip netns exec ${ns1} ip vrf exec tvrf ping -c 1 10.0.2.99 > /dev/null
+
+	for n in output post; do
+		for d in tvrf eth0; do
+			ip netns exec ${ns1} nft list chain inet filter $n | grep -q "oifname \"$d\" icmp type echo-request counter packets 1"
+			if [ $? -ne 0 ] ; then
+				echo "FAIL: chain $n: icmp packet counter mismatch for device $d" 1>&2
+				ip netns exec ${ns1} nft list ruleset
+				ret=1
+				return
+			fi
+		done
+	done
+
+	wait $nfqpid
+	[ $? -eq 0 ] && echo "PASS: icmp+nfqueue via vrf"
+	wait 2>/dev/null
+}
+
 ip netns exec ${nsrouter} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
 ip netns exec ${nsrouter} sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
 ip netns exec ${nsrouter} sysctl net.ipv4.conf.veth1.forwarding=1 > /dev/null
@@ -372,5 +425,6 @@ test_queue 20
 test_tcp_forward
 test_tcp_localhost
 test_tcp_localhost_requeue
+test_icmp_vrf
 
 exit $ret
Signature · 1 line
2.30.2
author maintainer
#4 lore raw

PATCH 3/11 netfilter: nft_payload: Remove duplicated include in nft_payload.c

In reply to Pablo Neira Ayuso

From: Wan Jiabing <[email protected]>

Fix following checkincludes.pl warning:
./net/netfilter/nft_payload.c: linux/ip.h is included more than once.

---
 net/netfilter/nft_payload.c | 1 -
 1 file changed, 1 deletion(-)
net/netfilter/nft_payload.c +0 -1
index cbfe4e4a4ad7..bd689938a2e0 100644
@@ -22,7 +22,6 @@
 #include <linux/icmpv6.h>
 #include <linux/ip.h>
 #include <linux/ipv6.h>
-#include <linux/ip.h>
 #include <net/sctp/checksum.h>
 
 static bool nft_payload_rebuild_vlan_hdr(const struct sk_buff *skb, int mac_off,
Signature · 1 line
2.30.2
author maintainer
#5 lore raw

PATCH 4/11 selftests: nft_nat: Improve port shadow test stability

In reply to Pablo Neira Ayuso

From: Phil Sutter <[email protected]>

Setup phase in test_port_shadow() relied upon a race-condition:
Listening nc on port 1405 was started in background before attempting to create the fake conntrack entry using the same source port. If listening nc won, fake conntrack entry could not be created causing wrong behaviour. Reorder nc calls to fix this and introduce a short delay before testing the setup to wait for listening nc process startup.

Fixes 465f15a6d1a8f ("selftests: nft_nat: add udp hole punch test case")
---
 tools/testing/selftests/netfilter/nft_nat.sh | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/netfilter/nft_nat.sh +4 -2
index da1c1e4b6c86..905c033db74d 100755
@@ -759,14 +759,16 @@ test_port_shadow()
 	local result=""
 	local logmsg=""
 
+	# make shadow entry, from client (ns2), going to (ns1), port 41404, sport 1405.
+	echo "fake-entry" | ip netns exec "$ns2" nc -w 1 -p 1405 -u "$daddrc" 41404 > /dev/null
+
 	echo ROUTER | ip netns exec "$ns0" nc -w 5 -u -l -p 1405 >/dev/null 2>&1 &
 	nc_r=$!
 
 	echo CLIENT | ip netns exec "$ns2" nc -w 5 -u -l -p 1405 >/dev/null 2>&1 &
 	nc_c=$!
 
-	# make shadow entry, from client (ns2), going to (ns1), port 41404, sport 1405.
-	echo "fake-entry" | ip netns exec "$ns2" nc -w 1 -p 1405 -u "$daddrc" 41404 > /dev/null
+	sleep 0.3
 
 	# ns1 tries to connect to ns0:1405.  With default settings this should connect
 	# to client, it matches the conntrack entry created above.
Signature · 1 line
2.30.2
author maintainer
#6 lore raw

PATCH 5/11 selftests: nft_nat: Simplify port shadow notrack test

In reply to Pablo Neira Ayuso

From: Phil Sutter <[email protected]>

The second rule in prerouting chain was probably a leftover: The router listens on veth0, so not tracking connections via that interface is sufficient. Likewise, the rule in output chain can be limited to that interface as well.

---
 tools/testing/selftests/netfilter/nft_nat.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/netfilter/nft_nat.sh +1 -2
index 905c033db74d..c62e4e26252c 100755
@@ -818,11 +818,10 @@ table $family raw {
 	chain prerouting {
 		type filter hook prerouting priority -300; policy accept;
 		meta iif veth0 udp dport 1405 notrack
-		udp dport 1405 notrack
 	}
 	chain output {
 		type filter hook output priority -300; policy accept;
-		udp sport 1405 notrack
+		meta oif veth0 udp sport 1405 notrack
 	}
 }
 EOF
Signature · 1 line
2.30.2
author maintainer
#7 lore raw

PATCH 6/11 netfilter: ctnetlink: fix filtering with CTA_TUPLE_REPLY

In reply to Pablo Neira Ayuso

From: Florent Fourcot <[email protected]>

filter->orig_flags was used for a reply context.

Fixes cb8aa9a3affb ("netfilter: ctnetlink: add kernel side filtering for dump")
---
 net/netfilter/nf_conntrack_netlink.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_conntrack_netlink.c +1 -1
index f1e5443fe7c7..2663764d0b6e 100644
@@ -1011,7 +1011,7 @@ ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
 						   CTA_TUPLE_REPLY,
 						   filter->family,
 						   &filter->zone,
-						   filter->orig_flags);
+						   filter->reply_flags);
 		if (err < 0) {
 			err = -EINVAL;
 			goto err_filter;
Signature · 1 line
2.30.2
author maintainer
#8 lore raw

PATCH 7/11 netfilter: ctnetlink: do not erase error code with EINVAL

In reply to Pablo Neira Ayuso

From: Florent Fourcot <[email protected]>

And be consistent in error management for both orig/reply filtering

Fixes cb8aa9a3affb ("netfilter: ctnetlink: add kernel side filtering for dump")
---
 net/netfilter/nf_conntrack_netlink.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
net/netfilter/nf_conntrack_netlink.c +1 -3
index 2663764d0b6e..c7708bde057c 100644
@@ -1012,10 +1012,8 @@ ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
 						   filter->family,
 						   &filter->zone,
 						   filter->reply_flags);
-		if (err < 0) {
-			err = -EINVAL;
+		if (err < 0)
 			goto err_filter;
-		}
 	}
 
 	return filter;
Signature · 1 line
2.30.2
author maintainer
#9 lore raw

PATCH 8/11 netfilter: ipvs: Fix reuse connection if RS weight is 0

In reply to Pablo Neira Ayuso

From: yangxingwu <[email protected]>

We are changing expire_nodest_conn to work even for reused connections when conn_reuse_mode=0, just as what was done with commit dc7b3eb900aa ("ipvs:
Fix reuse connection if real server is dead").

For controlled and persistent connections, the new connection will get the needed real server depending on the rules in ip_vs_check_template().

Fixes d752c3645717 ("ipvs: allow rescheduling of new connections when port reuse is detected")
---
 Documentation/networking/ipvs-sysctl.rst | 3 +--
 net/netfilter/ipvs/ip_vs_core.c          | 8 ++++----
 2 files changed, 5 insertions(+), 6 deletions(-)
Documentation/networking/ipvs-sysctl.rst +1 -2
index 95ef56d62077..387fda80f05f 100644
@@ -37,8 +37,7 @@ conn_reuse_mode - INTEGER
 
 	0: disable any special handling on port reuse. The new
 	connection will be delivered to the same real server that was
-	servicing the previous connection. This will effectively
-	disable expire_nodest_conn.
+	servicing the previous connection.
 
 	bit 1: enable rescheduling of new connections when it is safe.
 	That is, whenever expire_nodest_conn and for TCP sockets, when
net/netfilter/ipvs/ip_vs_core.c +4 -4
index e93c937a8bf0..51ad557a525b 100644
@@ -1919,7 +1919,6 @@ ip_vs_in_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *state
 	struct ip_vs_proto_data *pd;
 	struct ip_vs_conn *cp;
 	int ret, pkts;
-	int conn_reuse_mode;
 	struct sock *sk;
 	int af = state->pf;
 
@@ -1997,15 +1996,16 @@ ip_vs_in_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *state
 	cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto,
 			     ipvs, af, skb, &iph);
 
-	conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
-	if (conn_reuse_mode && !iph.fragoffs && is_new_conn(skb, &iph) && cp) {
+	if (!iph.fragoffs && is_new_conn(skb, &iph) && cp) {
+		int conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
 		bool old_ct = false, resched = false;
 
 		if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
 		    unlikely(!atomic_read(&cp->dest->weight))) {
 			resched = true;
 			old_ct = ip_vs_conn_uses_old_conntrack(cp, skb);
-		} else if (is_new_conn_expected(cp, conn_reuse_mode)) {
+		} else if (conn_reuse_mode &&
+			   is_new_conn_expected(cp, conn_reuse_mode)) {
 			old_ct = ip_vs_conn_uses_old_conntrack(cp, skb);
 			if (!atomic_read(&cp->n_control)) {
 				resched = true;
Signature · 1 line
2.30.2
author maintainer
#10 lore raw

PATCH 9/11 netfilter: xt_IDLETIMER: replace snprintf in show functions with sysfs_emit

In reply to Pablo Neira Ayuso

From: Jing Yao <[email protected]>

coccicheck complains about the use of snprintf() in sysfs show functions:
WARNING use scnprintf or sprintf

Use sysfs_emit instead of scnprintf, snprintf or sprintf makes more sense.

---
 net/netfilter/xt_IDLETIMER.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/xt_IDLETIMER.c +2 -2
index 2f7cf5ecebf4..0f8bb0bf558f 100644
@@ -85,9 +85,9 @@ static ssize_t idletimer_tg_show(struct device *dev,
 	mutex_unlock(&list_mutex);
 
 	if (time_after(expires, jiffies) || ktimespec.tv_sec > 0)
-		return snprintf(buf, PAGE_SIZE, "%ld\n", time_diff);
+		return sysfs_emit(buf, "%ld\n", time_diff);
 
-	return snprintf(buf, PAGE_SIZE, "0\n");
+	return sysfs_emit(buf, "0\n");
 }
 
 static void idletimer_tg_work(struct work_struct *work)
Signature · 1 line
2.30.2
author maintainer
#11 lore raw

PATCH 10/11 netfilter: flowtable: fix IPv6 tunnel addr match

In reply to Pablo Neira Ayuso

From: Will Mortensen <[email protected]>

Previously the IPv6 addresses in the key were clobbered and the mask was left unset.

I haven't tested this; I noticed it while skimming the code to understand an unrelated issue.

Fixes cfab6dbd0ecf ("netfilter: flowtable: add tunnel match offload support")
---
 net/netfilter/nf_flow_table_offload.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_flow_table_offload.c +2 -2
index d6bf1b2cd541..b561e0a44a45 100644
@@ -65,11 +65,11 @@ static void nf_flow_rule_lwt_match(struct nf_flow_match *match,
 		       sizeof(struct in6_addr));
 		if (memcmp(&key->enc_ipv6.src, &in6addr_any,
 			   sizeof(struct in6_addr)))
-			memset(&key->enc_ipv6.src, 0xff,
+			memset(&mask->enc_ipv6.src, 0xff,
 			       sizeof(struct in6_addr));
 		if (memcmp(&key->enc_ipv6.dst, &in6addr_any,
 			   sizeof(struct in6_addr)))
-			memset(&key->enc_ipv6.dst, 0xff,
+			memset(&mask->enc_ipv6.dst, 0xff,
 			       sizeof(struct in6_addr));
 		enc_keys |= BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS);
 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Signature · 1 line
2.30.2
author maintainer
#12 lore raw

PATCH 11/11 selftests: nft_nat: switch port shadow test cases to socat

In reply to Pablo Neira Ayuso

From: Florian Westphal <[email protected]>

There are now at least three distinct flavours of netcat/nc tool:
'original' version, one version ported from openbsd and nmap-ncat.

The script only works with original because it sets SOREUSEPORT option.

Other nc versions return 'port already in use' error and port shadow test fails:

PASS: inet IPv6 redirection for ns2-hMHcaRvx
nc: bind failed: Address already in use
ERROR: portshadow test default: got reply from "ROUTER", not CLIENT as intended

Switch to socat instead.

---
 tools/testing/selftests/netfilter/nft_nat.sh | 26 ++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)
tools/testing/selftests/netfilter/nft_nat.sh +19 -7
index c62e4e26252c..d88867d2fed7 100755
@@ -760,20 +760,20 @@ test_port_shadow()
 	local logmsg=""
 
 	# make shadow entry, from client (ns2), going to (ns1), port 41404, sport 1405.
-	echo "fake-entry" | ip netns exec "$ns2" nc -w 1 -p 1405 -u "$daddrc" 41404 > /dev/null
+	echo "fake-entry" | ip netns exec "$ns2" timeout 1 socat -u STDIN UDP:"$daddrc":41404,sourceport=1405
 
-	echo ROUTER | ip netns exec "$ns0" nc -w 5 -u -l -p 1405 >/dev/null 2>&1 &
-	nc_r=$!
+	echo ROUTER | ip netns exec "$ns0" timeout 5 socat -u STDIN UDP4-LISTEN:1405 &
+	sc_r=$!
 
-	echo CLIENT | ip netns exec "$ns2" nc -w 5 -u -l -p 1405 >/dev/null 2>&1 &
-	nc_c=$!
+	echo CLIENT | ip netns exec "$ns2" timeout 5 socat -u STDIN UDP4-LISTEN:1405,reuseport &
+	sc_c=$!
 
 	sleep 0.3
 
 	# ns1 tries to connect to ns0:1405.  With default settings this should connect
 	# to client, it matches the conntrack entry created above.
 
-	result=$(echo "" | ip netns exec "$ns1" nc -w 1 -p 41404 -u "$daddrs" 1405)
+	result=$(echo "data" | ip netns exec "$ns1" timeout 1 socat - UDP:"$daddrs":1405,sourceport=41404)
 
 	if [ "$result" = "$expect" ] ;then
 		echo "PASS: portshadow test $test: got reply from ${expect}${logmsg}"
@@ -782,7 +782,7 @@ test_port_shadow()
 		ret=1
 	fi
 
-	kill $nc_r $nc_c 2>/dev/null
+	kill $sc_r $sc_c 2>/dev/null
 
 	# flush udp entries for next test round, if any
 	ip netns exec "$ns0" conntrack -F >/dev/null 2>&1
@@ -852,6 +852,18 @@ test_port_shadowing()
 {
 	local family="ip"
 
+	conntrack -h >/dev/null 2>&1
+	if [ $? -ne 0 ];then
+		echo "SKIP: Could not run nat port shadowing test without conntrack tool"
+		return
+	fi
+
+	socat -h > /dev/null 2>&1
+	if [ $? -ne 0 ];then
+		echo "SKIP: Could not run nat port shadowing test without socat tool"
+		return
+	fi
+
 	ip netns exec "$ns0" sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
 	ip netns exec "$ns0" sysctl net.ipv4.conf.veth1.forwarding=1 > /dev/null
 
Signature · 1 line
2.30.2