[PATCH net-next 2/2] selftests: net: add test for IPv4 address scope ordering
From: Tim Wong
Date: Mon Jul 20 2026 - 14:25:04 EST
Add a selftest verifying that IPv4 addresses on an interface are
enumerated with global-scope addresses listed before link-scope
addresses, regardless of the order in which the addresses were
configured. This exercises the ordering fixed in the preceding
patch ("ipv4: devinet: list global scope addresses before link
scope addresses") and guards against regression.
Signed-off-by: kanman.wong <kanman.wong@xxxxxxxx>
---
tools/testing/selftests/net/Makefile | 1 +
.../selftests/net/fib_ipv4_scope_order.sh | 55 +++++++++++++++++++
2 files changed, 56 insertions(+)
create mode 100755 tools/testing/selftests/net/fib_ipv4_scope_order.sh
diff --git a/tools/testing/selftests/net/Makefile
b/tools/testing/selftests/net/Makefile
index 708d960ae07d..66a79c915961 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -33,6 +33,7 @@ TEST_PROGS := \
fdb_flush.sh \
fdb_notify.sh \
fib-onlink-tests.sh \
+ fib_ipv4_scope_order.sh \
fib_nexthop_multiprefix.sh \
fib_nexthop_nongw.sh \
fib_nexthops.sh \
diff --git a/tools/testing/selftests/net/fib_ipv4_scope_order.sh
b/tools/testing/selftests/net/fib_ipv4_scope_order.sh
new file mode 100755
index 000000000000..3bbb7eefa2d8
--- /dev/null
+++ b/tools/testing/selftests/net/fib_ipv4_scope_order.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Verify IPv4 addresses are enumerated with global-scope addresses
+# listed before link-scope addresses on the same interface,
+# regardless of the order in which they were configured -- matching
+# existing IPv6 address enumeration behavior.
+
+source lib.sh
+
+ret=0
+
+setup() {
+ setup_ns TEST_NS
+ ip -n "$TEST_NS" link add dummy0 type dummy
+ ip -n "$TEST_NS" link set dummy0 up
+}
+
+cleanup() {
+ cleanup_ns "$TEST_NS"
+}
+
+check_order() {
+ local desc="$1"
+ local order
+ local first
+
+ order=$(ip -n "$TEST_NS" -o addr show dev dummy0 | grep -oP
'(?<=scope )(global|link)')
+ first=$(echo "$order" | head -1)
+
+ if [ "$first" != "global" ]; then
+ echo "FAIL: $desc: link-scope address listed before global-scope address"
+ ret=1
+ else
+ echo "PASS: $desc"
+ fi
+}
+
+trap cleanup EXIT
+setup
+
+# Case 1: link-scope address added before global-scope address.
+ip -n "$TEST_NS" addr add 169.254.1.1/16 dev dummy0 scope link
+ip -n "$TEST_NS" addr add 192.0.2.1/24 dev dummy0 scope global
+check_order "link added before global"
+
+ip -n "$TEST_NS" addr flush dev dummy0
+
+# Case 2: global-scope address added before link-scope address
+# (sanity check -- should already pass even without the fix).
+ip -n "$TEST_NS" addr add 192.0.2.1/24 dev dummy0 scope global
+ip -n "$TEST_NS" addr add 169.254.1.1/16 dev dummy0 scope link
+check_order "global added before link"
+
+exit $ret
--
2.51.0