[PATCH pve-network 1/1] fix: prevent duplicate DHCP leases for identical MAC address

Philippos Giavridis philippos.g at me.com
Tue Nov 4 13:20:26 CET 2025


When cloning a VM with the same MAC address, the SDN IPAM module
currently assigns a new IP instead of using the existing reservation.
This patch adds a check for existing MAC address mappings before
allocating a new IP address.
If such a mapping exists, the new VM receives the defined IP instead
of a new one from the DHCP pool.

Signed-off-by: Philippos Giavridis <philippos.g at me.com>
---
 src/PVE/Network/SDN/Subnets.pm | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/PVE/Network/SDN/Subnets.pm b/src/PVE/Network/SDN/Subnets.pm
index 1f27fac..66fb721 100644
--- a/src/PVE/Network/SDN/Subnets.pm
+++ b/src/PVE/Network/SDN/Subnets.pm
@@ -235,6 +235,30 @@ sub add_next_free_ip {
     #verify dns zones before ipam
     verify_dns_zone($dnszone, $dns) if !$skipdns;
 
+    if ($mac && $ipamid) {
+        my ($zoneid) = split(/-/, $subnetid);
+        my ($existing_ip4, $existing_ip6) = PVE::Network::SDN::Ipams::get_ips_from_mac(
+            $mac, $zoneid, $zone,
+        );
+
+        my $is_ipv4 = Net::IP::ip_is_ipv4($subnet->{network});
+        my $existing_ip = $is_ipv4 ? $existing_ip4 : $existing_ip6;
+
+        if ($existing_ip) {
+            my $ip_obj = NetAddr::IP->new($existing_ip);
+            my $subnet_obj = NetAddr::IP->new($subnet->{cidr});
+
+            if ($subnet_obj->contains($ip_obj)) {
+                $ip = $existing_ip;
+
+                eval { PVE::Network::SDN::Ipams::add_cache_mac_ip($mac, $ip); };
+                warn $@ if $@;
+
+                goto DNS_SETUP;
+            }
+        }
+    }
+
     if ($ipamid) {
         my $ipam_cfg = PVE::Network::SDN::Ipams::config();
         my $plugin_config = $ipam_cfg->{ids}->{$ipamid};
@@ -267,6 +291,7 @@ sub add_next_free_ip {
         warn $@ if $@;
     }
 
+DNS_SETUP:
     eval {
         my $reversednszone = get_reversedns_zone($subnetid, $subnet, $reversedns, $ip);
 
-- 
2.43.0




More information about the pve-devel mailing list