From cf6ab25c47818550bfd95d9bdc0cb3aa1b96a90e Mon Sep 17 00:00:00 2001 From: codex Date: Wed, 4 Nov 2026 02:20:48 +0800 Subject: [PATCH] fix: project reserved sparse IPv6 addresses --- .../Cloud/CloudAdminHostNetworkService.php | 41 +++++++++++++++++-- .../CloudAdminHostNetworkServiceTest.php | 6 ++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/web/app/Services/Cloud/CloudAdminHostNetworkService.php b/web/app/Services/Cloud/CloudAdminHostNetworkService.php index 0875707f..1d5f5e86 100644 --- a/web/app/Services/Cloud/CloudAdminHostNetworkService.php +++ b/web/app/Services/Cloud/CloudAdminHostNetworkService.php @@ -1148,16 +1148,51 @@ final class CloudAdminHostNetworkService } } - /** IPv6 pools materialize only addresses that were actually claimed. */ + /** + * IPv6 pools materialize claimed addresses and the bounded set of + * control-plane blocked addresses. The latter keeps administrator + * disabled-address projections exact without expanding the sparse range. + */ private static function refreshSparseIpv6Ledger(CloudPublicIpPool $pool, array $config, array $blocked): void { + $network = CloudIpPrefix::network((string) $config['cidr']); $start = CloudIpAddress::parse((string) $config['allocation_start']); $end = CloudIpAddress::parse((string) $config['allocation_end']); $now = time(); - foreach (CloudPublicIp::where( + $rows = CloudPublicIp::where( 'public_ip_pool_id', (int) $pool->id - )->orderBy('id')->lockForUpdate()->get() as $row) { + )->orderBy('id')->lockForUpdate()->get(); + $byAddress = []; + foreach ($rows as $row) { + $byAddress[(string) $row->address] = true; + } + foreach (array_keys($blocked) as $blockedText) { + try { + $blockedAddress = CloudIpAddress::parse($blockedText); + } catch (\Throwable) { + continue; + } + if ($blockedAddress->family() !== 6 + || !$network->contains($blockedAddress) + || isset($byAddress[$blockedAddress->text()])) { + continue; + } + $row = new CloudPublicIp(); + $row->host_id = (int) $pool->host_id; + $row->public_ip_pool_id = (int) $pool->id; + $row->address_family = 6; + $row->address = $blockedAddress->text(); + $row->address_binary = $blockedAddress->binary(); + $row->prefix_length = (int) $config['guest_prefix_length']; + $row->state = 'disabled'; + $row->created_at = $now; + $row->updated_at = $now; + $row->save(); + $rows->push($row); + $byAddress[$blockedAddress->text()] = true; + } + foreach ($rows as $row) { try { $address = CloudIpAddress::parse((string) $row->address); $inRange = $address->family() === 6 diff --git a/web/tests/CloudAdminHostNetworkServiceTest.php b/web/tests/CloudAdminHostNetworkServiceTest.php index a83204b4..9d934982 100644 --- a/web/tests/CloudAdminHostNetworkServiceTest.php +++ b/web/tests/CloudAdminHostNetworkServiceTest.php @@ -545,7 +545,9 @@ $assert($ipv6Pool !== null && (string) $ipv6Pool->allocation_start === '2404:c140:156:3a::1000' && (string) $ipv6Pool->allocation_end === '2404:c140:156:3a::1fff' && (int) $ipv6Pool->max_allocations === 100 - && CloudPublicIp::where('public_ip_pool_id', (int) $ipv6Pool->id)->count() === 0 + && CloudPublicIp::where('public_ip_pool_id', (int) $ipv6Pool->id) + ->where('address', '2404:c140:156:3a::2') + ->where('state', 'disabled')->count() === 1 && is_array($ipv6Item) && (int) $ipv6Item['available_count'] === 100 && $ipv6Item['dual_stack_ready'] === true @@ -557,6 +559,8 @@ $assert((int) $publicCapacity['bundles']['available'] === 4 'Host network did not expose the canonical dual-stack bundle capacity.'); $assert((int) $publicCapacity['incomplete_claims']['allocated'] === 1, 'A single-family allocation was not exposed as an incomplete identity.'); +$assert((int) $publicCapacity['disabled_addresses']['ipv6'] === 1, + 'An unclaimed reserved IPv6 address was absent from disabled capacity.'); $expectCloudError( static fn () => $service->update( $hostId,