From 159682124c790f24123cdff55c89fed86dda7bfa Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Mon, 15 May 2023 14:39:26 -0500 Subject: [PATCH] Closes #9068: Disallow assigning bcast/networks to interfaces --- netbox/ipam/forms/model_forms.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index d85ce7a30..d1c7dd551 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -351,12 +351,17 @@ class IPAddressForm(TenancyForm, NetBoxModelForm): 'primary_for_parent', "Only IP addresses assigned to an interface can be designated as primary IPs." ) - # Do not allow assigning a network ID or broadcast address to an interface + # Do not allow assigning a network ID or broadcast address to an interface. if interface: - if self.instance.ip == self.instance.network: - self.add_error('interface', "This address is a network ID, which may not be assigned to an interface.") - if self.instance.ip == self.instance.broadcast: - self.add_error('interface', "This address is a broadcast address, which may not be assigned to an interface.") + if address := self.cleaned_data.get('address'): + if address.ip == address.network: + self.add_error( + 'interface', + "This address is a network ID, which may not be assigned to an interface.") + if address.ip == address.broadcast: + self.add_error( + 'interface', + "This is a broadcast address, which may not be assigned to an interface.") def save(self, *args, **kwargs): ipaddress = super().save(*args, **kwargs)