Skip to contentSkip to conversation

ipv6: fix IPv6 scope check of the source address in a VRF

PATCH Applied Vincent Jardin 5 participants 8 messages Nov 23 – Nov 27

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover ipv6: fix IPv6 scope check of the source address in a VRF

When a packet is sent through a device of a VRF, ipv6_dev_get_saddr() first checks the addresses of that device and of the VRF device, and stops when the best of them has a scope that fits the destination.

Since the commit abb9a68d2c64 ("ipv6: take care of scope when choosing the src addr"), the check reads the scopedist of the best candidate, which is an issue:

- with a single candidate, scopedist is not written: when the stack is zero-initialized, a device that only has a link-local address sends to global destinations with it, although another device of the VRF has a global address

- with more candidates, scopedist is negative, whether the scope fits or not: all the devices of the VRF are looked at, and one of their addresses can win over the address of the VRF device

Changes:
- patch 1 compares the scope of the best candidate with the scope of the destination

- patch 2 adds test cases to fib_tests.sh

Note: abb9a68d2c64 is in every kernel since v6.11 and was backported down to 4.19, so the bug is in all the maintained trees (5.10, 5.15, 6.1, 6.6, 6.12, 6.18 and 7.3), hence this series for net.

---
Vincent Jardin (2):
ipv6: check the scope of the best source address candidate in a VRF
selftests: fib_tests: test IPv6 source address of a VRF

 net/ipv6/addrconf.c                      |  6 ++--
 tools/testing/selftests/net/fib_tests.sh | 47 +++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 3 deletions(-)

---
base-commit: af6737b9066832a2017c281159950ba92385ce00
change-id: 20261123-for-upstream-ipv6-vrf-saddr-scope-v1-4e0165804d43

Best regards,

Signature · 1 line
Vincent Jardin <[email protected]>
#1 lore raw

PATCH 1/2 ipv6: check the scope of the best source address candidate in a VRF

When a packet is sent through a device of a VRF, ipv6_dev_get_saddr() first checks the addresses of that device and of the VRF device. It stops there when the best of them has a scope that fits the destination.

Since the commit abb9a68d2c64 ("ipv6: take care of scope when choosing the src addr"), that is what the scopedist of the best candidate is tested for.

scopedist is only written when the rule scope compares 2 candidates. With a single candidate, the rules stop at the first one, which only tells that there was no candidate yet, then scopedist is read without having been written.

It is 0 with CONFIG_INIT_STACK_ALL_ZERO, which passes the test:
a device that only has an IPv6 link-local address sends to global destinations with that link-local address, although another device of the VRF has a global address.

  # ip link add site2 type vrf table 30
  # ip link set site2 up
  # ip link add dummy0 type dummy
  # ip link set dummy0 master site2 up
  # ip addr add fd00:91::1/64 dev dummy0 nodad
  # ip link add dummy1 type dummy
  # ip link set dummy1 master site2 up
  # ip -6 route add fd00:93::/64 vrf site2 dev dummy1
  # ip -6 route get fd00:93::1 vrf site2
  fd00:93::1 dev dummy1 table 30 src fe80::5cb7:13ff:fe96:87ac metric 1024 pref medium

When the scope rule does run, scopedist is negative, whether the scope fits or not: the test fails, and the other devices of the VRF are checked too. One of their addresses can then win on a later rule, the longest matching prefix for instance, over the address of the VRF device, which the first lookup is meant to prefer.

So let's compare the scope of the best candidate with the scope of the destination instead.

This issue was found for a development of a UDP tunnel device with IPv6 addresses in a VRF selecting an IPv6 link-local source address for a global destination: then it was confirmed with the fib_tests.sh case of the next commit, whose first two checks fail without this fix on a CONFIG_INIT_STACK_ALL_ZERO=y kernel.

Fixes abb9a68d2c64 ("ipv6: take care of scope when choosing the src addr")
---
 net/ipv6/addrconf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/ipv6/addrconf.c +4 -2
index d9d13e78bf13b..656bf1ea26da8 100644
@@ -1902,8 +1902,10 @@ int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
 							    master, &dst,
 							    scores, hiscore_idx);
 
-			if (scores[hiscore_idx].ifa &&
-			    scores[hiscore_idx].scopedist >= 0)
+			hiscore = &scores[hiscore_idx];
+			if (hiscore->ifa &&
+			    __ipv6_addr_src_scope(hiscore->addr_type) >=
+			    dst.scope)
 				goto out;
 		}
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH net 1/2] ipv6: check the scope of the best source address candidate in a VRF

Le 23/11/2026 à 15:30, Vincent Jardin via B4 Relay a écrit :

Quoted 47 lines

From: Vincent Jardin <[email protected]>

When a packet is sent through a device of a VRF, ipv6_dev_get_saddr() first checks the addresses of that device and of the VRF device. It stops there when the best of them has a scope that fits the destination.

Since the commit abb9a68d2c64 ("ipv6: take care of scope when choosing the src addr"), that is what the scopedist of the best candidate is tested for.

scopedist is only written when the rule scope compares 2 candidates. With a single candidate, the rules stop at the first one, which only tells that there was no candidate yet, then scopedist is read without having been written.

It is 0 with CONFIG_INIT_STACK_ALL_ZERO, which passes the test:
a device that only has an IPv6 link-local address sends to global destinations with that link-local address, although another device of the VRF has a global address.

  # ip link add site2 type vrf table 30
  # ip link set site2 up
  # ip link add dummy0 type dummy
  # ip link set dummy0 master site2 up
  # ip addr add fd00:91::1/64 dev dummy0 nodad
  # ip link add dummy1 type dummy
  # ip link set dummy1 master site2 up
  # ip -6 route add fd00:93::/64 vrf site2 dev dummy1
  # ip -6 route get fd00:93::1 vrf site2
  fd00:93::1 dev dummy1 table 30 src fe80::5cb7:13ff:fe96:87ac metric 1024 pref medium

When the scope rule does run, scopedist is negative, whether the scope fits or not: the test fails, and the other devices of the VRF are checked too. One of their addresses can then win on a later rule, the longest matching prefix for instance, over the address of the VRF device, which the first lookup is meant to prefer.

So let's compare the scope of the best candidate with the scope of the destination instead.

This issue was found for a development of a UDP tunnel device with IPv6 addresses in a VRF selecting an IPv6 link-local source address for a global destination: then it was confirmed with the fib_tests.sh case of the next commit, whose first two checks fail without this fix on a CONFIG_INIT_STACK_ALL_ZERO=y kernel.

Quoted 3 lines
Fixes abb9a68d2c64 ("ipv6: take care of scope when choosing the src addr")
#3 lore raw

PATCH 2/2 selftests: fib_tests: test IPv6 source address of a VRF

In reply to Vincent Jardin

Check the IPv6 source address of a route of a VRF through a device that only has an IPv6 link-local address:
- the address of another device of the VRF
- then the one of the VRF device once it has one
- then the one of the output device

---
 tools/testing/selftests/net/fib_tests.sh | 47 +++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
tools/testing/selftests/net/fib_tests.sh +46 -1
index d7ac3c727f704..e211ea5a945ef 100755
@@ -14,7 +14,7 @@ TESTS="unregister down carrier nexthop suppress ipv6_notify ipv4_notify \
        ipv4_mpath_list ipv6_mpath_list ipv4_mpath_balance ipv6_mpath_balance \
        ipv4_mpath_balance_preferred ipv4_mpath_oif ipv4_mpath_oif_nh \
        ipv4_mpath_oif_vrf ipv6_mpath_oif ipv6_mpath_oif_nh ipv6_mpath_oif_vrf \
-       fib6_ra_to_static fib6_temp_addr_renewal"
+       ipv6_saddr_vrf fib6_ra_to_static fib6_temp_addr_renewal"
 
 VERBOSE=0
 PAUSE_ON_FAIL=no
@@ -3228,6 +3228,50 @@ ipv6_mpath_oif_vrf_test()
 	cleanup
 }
 
+ipv6_saddr_vrf_check()
+{
+	local saddr="$1"
+	local desc="$2"
+	local route_get="$IP -6 route get 2001:db8:10::1 vrf vrf-123"
+
+	run_cmd "$route_get | grep -q 'src $saddr '"
+	log_test $? 0 "$desc"
+}
+
+ipv6_saddr_vrf_test()
+{
+	echo
+	echo "IPv6 source address selection with VRF test"
+
+	setup
+
+	if ! $IP link add name vrf-123 up type vrf table 123 &> /dev/null; then
+		echo "SKIP: Could not add a VRF device."
+		cleanup
+		return
+	fi
+
+	set -e
+	$NS_EXEC sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1
+	$IP link set dev dummy0 master vrf-123
+	$IP link add dummy1 up master vrf-123 type dummy
+	$IP -6 route add 2001:db8:10::/64 vrf vrf-123 dev dummy1
+	set +e
+
+	ipv6_saddr_vrf_check "2001:db8:1::1" \
+		"Output device with a link-local address only"
+
+	$IP address add 2001:db8:100::1/128 dev vrf-123 nodad
+	ipv6_saddr_vrf_check "2001:db8:100::1" \
+		"Address of the VRF device before those of other devices"
+
+	$IP address add 2001:db8:2::1/64 dev dummy1 nodad
+	ipv6_saddr_vrf_check "2001:db8:2::1" \
+		"Address of the output device first"
+
+	cleanup
+}
+
 ################################################################################
 # usage
 
@@ -3320,6 +3364,7 @@ do
 	ipv6_mpath_oif)			ipv6_mpath_oif_test;;
 	ipv6_mpath_oif_nh)		ipv6_mpath_oif_nh_test;;
 	ipv6_mpath_oif_vrf)		ipv6_mpath_oif_vrf_test;;
+	ipv6_saddr_vrf)			ipv6_saddr_vrf_test;;
 	fib6_ra_to_static)		fib6_ra_to_static;;
 	fib6_temp_addr_renewal)		fib6_temp_addr_renewal;;
 
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH net 2/2] selftests: fib_tests: test IPv6 source address of a VRF

Le 23/11/2026 à 15:30, Vincent Jardin via B4 Relay a écrit :

Quoted 9 lines

From: Vincent Jardin <[email protected]>

Check the IPv6 source address of a route of a VRF through a device that only has an IPv6 link-local address:
- the address of another device of the VRF
- then the one of the VRF device once it has one
- then the one of the output device

maintainer Reviewed-by on the v1 series
#5 lore raw
In reply to Vincent Jardin
Quoted 17 lines · On Mon, Nov 23, 2026 at 03:30:42PM +0100, Vincent Jardin via B4 Relay wrote:

When a packet is sent through a device of a VRF, ipv6_dev_get_saddr() first checks the addresses of that device and of the VRF device, and stops when the best of them has a scope that fits the destination.

Since the commit abb9a68d2c64 ("ipv6: take care of scope when choosing the src addr"), the check reads the scopedist of the best candidate, which is an issue:

- with a single candidate, scopedist is not written: when the stack is zero-initialized, a device that only has a link-local address sends to global destinations with it, although another device of the VRF has a global address

- with more candidates, scopedist is negative, whether the scope fits or not: all the devices of the VRF are looked at, and one of their addresses can win over the address of the VRF device

Quoted 5 lines

Changes:
- patch 1 compares the scope of the best candidate with the scope of the destination

- patch 2 adds test cases to fib_tests.sh

#7 lore raw
In reply to Vincent Jardin
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Mon, 23 Nov 2026 15:30:42 +0100 you wrote:

When a packet is sent through a device of a VRF, ipv6_dev_get_saddr() first checks the addresses of that device and of the VRF device, and stops when the best of them has a scope that fits the destination.

Since the commit abb9a68d2c64 ("ipv6: take care of scope when choosing the src addr"), the check reads the scopedist of the best candidate, which is an issue:

[...]

Here is the summary with links:
- [net,1/2] ipv6: check the scope of the best source address candidate in a VRF

    https://git.kernel.org/netdev/net/c/b026cc5ce2f1
  - [net,2/2] selftests: fib_tests: test IPv6 source address of a VRF
    https://git.kernel.org/netdev/net/c/c83fc88d628f

You are awesome, thank you!

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