From 0adf8a2bb0393d7048dab86ca21516aa2572d9d7 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Fri, 10 Nov 2023 13:37:08 +0100 Subject: [PATCH] interfaces: prefer GUAs over ULAs when returning addresses The concept is a bit convoluted, but apparently better than ignoring the fact that a ULA cannot replace a GUA ever. PR: https://forum.opnsense.org/index.php?topic=36893.0 --- src/etc/inc/interfaces.inc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/etc/inc/interfaces.inc b/src/etc/inc/interfaces.inc index 44e590424..1cb6d965e 100644 --- a/src/etc/inc/interfaces.inc +++ b/src/etc/inc/interfaces.inc @@ -4357,6 +4357,7 @@ function interfaces_addresses($interfaces, $as_subnet = false, $ifconfig_details 'key' => $key, 'name' => $realif, 'scope' => !empty($address['link-local']), + 'unique' => ($proto == 'ipv4' || !empty($address['link-local'])) ? false : is_uniquelocal($address['ipaddr']), ]; } } @@ -4397,6 +4398,11 @@ function interfaces_addresses($interfaces, $as_subnet = false, $ifconfig_details } } + /* move ULAs to the bottom to prefer GUA addresses */ + usort($result, function ($a, $b) { + return $a['unique'] - $b['unique']; + }); + return $result; } @@ -4462,7 +4468,7 @@ function _interfaces_primary_address6($interface, $ifconfig_details = null, $all if ($link_local && !$addr['scope']) { continue; - } elseif (!$link_local && ($addr['scope'] || is_uniquelocal($addr['address']))) { + } elseif (!$link_local && $addr['scope']) { continue; }