diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index aaa7abba8..0a92cac63 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -354,14 +354,17 @@ class IPAddressForm(TenancyForm, NetBoxModelForm): # Do not allow assigning a network ID or broadcast address to an interface. if interface: if address := self.cleaned_data.get('address'): - if address.ip == address.network and address.prefixlen not in (31, 32, 127, 128): - self.add_error( - 'interface', - f"{address} is a network ID, which may not be assigned to an interface.") + if address.ip == address.network: + msg = f"{address} is a network ID, which may not be assigned to an interface." + if address.version == 4: + if address.prefixlen not in (31, 32): + raise ValidationError(msg) + if address.version == 6: + if address.prefixlen not in (127, 128): + raise ValidationError(msg) if address.ip == address.broadcast: - self.add_error( - 'interface', - f"{address} is a broadcast address, which may not be assigned to an interface.") + msg = f"{address} is a broadcast address, which may not be assigned to an interface." + raise ValidationError(msg) def save(self, *args, **kwargs): ipaddress = super().save(*args, **kwargs)