Skip to content

Commit

Permalink
interfaces: prefer GUAs over ULAs when returning addresses
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fichtner committed Nov 10, 2023
1 parent 2bff035 commit 0adf8a2
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/etc/inc/interfaces.inc
Expand Up @@ -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']),
];
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

2 comments on commit 0adf8a2

@subnetspider
Copy link

@subnetspider subnetspider commented on 0adf8a2 Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this should do what I thought what commit 89ee410 would do. Thanks 🙂

@fichtner
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better late than never :)

Please sign in to comment.