Skip to content

Commit dfdc7f5

Browse files
rootnomadRamgopal Nagaboina
authored andcommitted
network: validate the isolated DHCP range before persisting and guard it on reservation
Validate a custom isolated-network DHCP range before committing the network, so a range outside the CIDR (or reversed, or over the gateway) is rejected up front instead of throwing after the network is saved and leaving an orphaned network. The checks move into validateIsolatedNetworkDhcpRange, reused by storeIsolatedNetworkDhcpRange. Also reject an IP reservation (guestvmcidr) that would leave the stored DHCP range outside the reserved Guest VM CIDR, which would otherwise starve guest IP allocation.
1 parent 6d687a9 commit dfdc7f5

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎server/src/main/java/com/cloud/network/NetworkServiceImpl.java‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,13 +2682,20 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
26822682
}
26832683
}
26842684

2685+
// For an isolated network a start/end IP defines a custom DHCP range within the CIDR that guest IPs are
2686+
// allocated from; without it the whole CIDR is used, which is the default. Validate it up front so a bad
2687+
// range is rejected before the network is persisted, rather than throwing after commit and leaving an
2688+
// orphaned network.
2689+
boolean hasIsolatedDhcpRange = ntwkOff.getGuestType() == GuestType.Isolated && StringUtils.isNotBlank(startIP);
2690+
if (hasIsolatedDhcpRange) {
2691+
validateIsolatedNetworkDhcpRange(startIP, endIP, cidr, gateway);
2692+
}
2693+
26852694
Network network = commitNetwork(networkOfferingId, gateway, startIP, endIP, netmask, networkDomain, vlanId, bypassVlanOverlapCheck, name, displayText, caller, physicalNetworkId, zone.getId(),
26862695
domainId, isDomainSpecific, subdomainAccess, vpcId, startIPv6, endIPv6, ip6Gateway, ip6Cidr, displayNetwork, aclId, secondaryVlanId, privateVlanType, ntwkOff, pNtwk, aclType, owner, cidr, createVlan,
26872696
externalId, routerIPv4, routerIPv6, associatedNetwork, ip4Dns1, ip4Dns2, ip6Dns1, ip6Dns2, interfaceMTUs, networkCidrSize, keepMacAddressOnPublicNic);
26882697

2689-
// For an isolated network a start/end IP defines a custom DHCP range within the CIDR that
2690-
// guest IPs are allocated from; without it the whole CIDR is used, which is the default.
2691-
if (ntwkOff.getGuestType() == GuestType.Isolated && StringUtils.isNotBlank(startIP)) {
2698+
if (hasIsolatedDhcpRange) {
26922699
storeIsolatedNetworkDhcpRange(network.getId(), startIP, endIP);
26932700
}
26942701

@@ -2726,9 +2733,7 @@ private boolean isNonVpcNetworkSupportingDynamicRouting(NetworkOffering networkO
27262733
return !networkOffering.isForVpc() && NetworkOffering.RoutingMode.Dynamic == networkOffering.getRoutingMode();
27272734
}
27282735

2729-
protected void storeIsolatedNetworkDhcpRange(long networkId, String startIP, String endIP) {
2730-
NetworkVO network = _networksDao.findById(networkId);
2731-
String cidr = network.getCidr();
2736+
protected void validateIsolatedNetworkDhcpRange(String startIP, String endIP, String cidr, String gateway) {
27322737
if (endIP == null) {
27332738
endIP = startIP;
27342739
}
@@ -2738,10 +2743,17 @@ protected void storeIsolatedNetworkDhcpRange(long networkId, String startIP, Str
27382743
if (NetUtils.ip2Long(startIP) > NetUtils.ip2Long(endIP)) {
27392744
throw new InvalidParameterValueException(String.format("The DHCP start IP %s is greater than the end IP %s", startIP, endIP));
27402745
}
2741-
String gateway = network.getGateway();
27422746
if (gateway != null && NetUtils.ip2Long(gateway) >= NetUtils.ip2Long(startIP) && NetUtils.ip2Long(gateway) <= NetUtils.ip2Long(endIP)) {
27432747
throw new InvalidParameterValueException(String.format("The DHCP range %s-%s must not include the gateway %s", startIP, endIP, gateway));
27442748
}
2749+
}
2750+
2751+
protected void storeIsolatedNetworkDhcpRange(long networkId, String startIP, String endIP) {
2752+
NetworkVO network = _networksDao.findById(networkId);
2753+
if (endIP == null) {
2754+
endIP = startIP;
2755+
}
2756+
validateIsolatedNetworkDhcpRange(startIP, endIP, network.getCidr(), network.getGateway());
27452757
network.setDhcpStartIp(startIP);
27462758
network.setDhcpEndIp(endIP);
27472759
_networksDao.update(networkId, network);
@@ -4298,6 +4310,17 @@ public Network updateGuestNetwork(final UpdateNetworkCmd cmd) {
42984310
}
42994311
}
43004312

4313+
// A custom DHCP range must stay within the reserved Guest VM CIDR; reject a reservation that would
4314+
// leave the stored range partly or wholly outside it, otherwise guest IP allocation could be starved.
4315+
if (StringUtils.isNoneBlank(network.getDhcpStartIp(), network.getDhcpEndIp())) {
4316+
long dhcpStart = NetUtils.ip2Long(network.getDhcpStartIp());
4317+
long dhcpEnd = NetUtils.ip2Long(network.getDhcpEndIp());
4318+
if (dhcpStart < startIp || dhcpEnd > endIp) {
4319+
throw new InvalidParameterValueException(String.format("The network has a custom DHCP range %s-%s that falls outside the requested Guest VM CIDR %s. Remove or adjust the DHCP range before applying the reservation.",
4320+
network.getDhcpStartIp(), network.getDhcpEndIp(), guestVmCidr));
4321+
}
4322+
}
4323+
43014324
// In some scenarios even though guesVmCidr and network CIDR do not appear similar but
43024325
// the IP ranges exactly matches, in these special cases make sure no Reservation gets applied
43034326
if (network.getNetworkCidr() == null) {

‎server/src/test/java/com/cloud/network/NetworkServiceImplTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,4 +2599,15 @@ public void storeIsolatedNetworkDhcpRangeRejectsRangeIncludingGateway() {
25992599
isolatedNetworkForDhcpRange();
26002600
service.storeIsolatedNetworkDhcpRange(5L, "10.1.1.1", "10.1.1.20");
26012601
}
2602+
2603+
@Test
2604+
public void validateIsolatedNetworkDhcpRangeAcceptsRangeWithinCidr() {
2605+
// validated up front (before the network is persisted) so a bad range never leaves an orphaned network
2606+
service.validateIsolatedNetworkDhcpRange("10.1.1.10", "10.1.1.20", "10.1.1.0/24", "10.1.1.1");
2607+
}
2608+
2609+
@Test(expected = InvalidParameterValueException.class)
2610+
public void validateIsolatedNetworkDhcpRangeRejectsRangeOutsideCidr() {
2611+
service.validateIsolatedNetworkDhcpRange("10.9.9.10", "10.9.9.20", "10.1.1.0/24", "10.1.1.1");
2612+
}
26022613
}

0 commit comments

Comments
 (0)