From 4a72518cccee124bd02dcdd6e036c65225173f21 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Sat, 13 Apr 2019 10:05:22 -0500 Subject: [PATCH] Don't lose DHCP range when NIC address changes When a server or firewall changes the IP address of a network card, any DHCP ranges associated with that old number were zeroed out. Invalidating the range COULD be a nice feature in case the new IP address falls within the DHCP range - thus preventing conflicting IP addresses. So just to be nice I did allow the range to invalidate in that situation. However, in real life it wouldn't be so nice, and so perhaps it would actually be better to keep the existing rules intact even if it would cause a problem. Scenario: Level 2 Firewall Test -change firewall eth0 to 192.168.1.10 and eth1 to 192.168.2.20 -click OK -edit firewall again and look at the DHCP rules -notice that 192.168.1 has all zeros. -same thing with 192.168.2 -after the fix, the 192.168.2 rules are retained since 2.20 doesn't conflict with the range, but 1.10 does conflict, so that rule was invalidated. --- EduNetworkBuilder/IPAddress.cs | 5 +++++ EduNetworkBuilder/NetworkDevice.cs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/EduNetworkBuilder/IPAddress.cs b/EduNetworkBuilder/IPAddress.cs index 42e0d81..b2ed9f0 100644 --- a/EduNetworkBuilder/IPAddress.cs +++ b/EduNetworkBuilder/IPAddress.cs @@ -80,6 +80,11 @@ namespace EduNetworkBuilder } } + public void SetIP(UInt32 newIP) + { + _ip = newIP; + } + public bool Equals(NB_IPAddress CompareWith) { if (_ip != CompareWith._ip) return false; diff --git a/EduNetworkBuilder/NetworkDevice.cs b/EduNetworkBuilder/NetworkDevice.cs index c9eb29a..d5ffeea 100644 --- a/EduNetworkBuilder/NetworkDevice.cs +++ b/EduNetworkBuilder/NetworkDevice.cs @@ -1806,6 +1806,24 @@ namespace EduNetworkBuilder RouteTable.AddRange(ndCopyFrom.RouteTable); DHCPRanges.Clear(); DHCPRanges.AddRange(ndCopyFrom.DHCPRanges); + //if the IP address has changed, also update the corresponding address in DHCP rules + foreach (NB_IPAddress dhcpIP in DHCPRanges) + { + foreach (NetworkCard nic in NICs) + { + NetworkInterface matchingIF = nic.LocalInterface(dhcpIP, null); + if (matchingIF != null && !matchingIF.myIP.Equals(dhcpIP.GetIP) && !matchingIF.myIP.Equals(0)) + { + //only validate the entry if the new address doesn't conflict with the DHCP range. + UInt32 nStartingDHCP = dhcpIP.GetMask; + UInt32 nEndingDHCP = dhcpIP.GetGateway; + if (matchingIF.myIP.GetIP < nStartingDHCP || matchingIF.myIP.GetIP > nEndingDHCP) + { + dhcpIP.SetIP(matchingIF.myIP.GetIP); + } + } + } + } DHCPLeases.Clear(); DHCPLeases.AddRange(ndCopyFrom.DHCPLeases);