From 4ea3fad27ce5fe680cb4cd4337fd9c35eeed3858 Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Mon, 15 May 2023 14:16:14 -0500 Subject: [PATCH 1/6] Begin logic --- netbox/ipam/forms/model_forms.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index cf8117bf7ab..d85ce7a3048 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -350,6 +350,13 @@ def clean(self): self.add_error( '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 + 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.") def save(self, *args, **kwargs): ipaddress = super().save(*args, **kwargs) 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 2/6] 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 d85ce7a3048..d1c7dd55174 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -351,12 +351,17 @@ def clean(self): '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) From 478b27ae18660a4069a2a6aa859bced7570a2b16 Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Mon, 15 May 2023 14:52:29 -0500 Subject: [PATCH 3/6] Allow net IDs in /31, /32, /127, /128 --- netbox/ipam/forms/model_forms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index d1c7dd55174..0a851345b02 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -354,14 +354,14 @@ def clean(self): # 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: + if address.ip == address.network and address.prefixlen not in (31, 32, 127, 128): self.add_error( 'interface', - "This address is a network ID, which may not be assigned to an interface.") + f"{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.") + f"{address} is a broadcast address, which may not be assigned to an interface.") def save(self, *args, **kwargs): ipaddress = super().save(*args, **kwargs) From 73891479ae2b8e1c75d389fc6634b43abfa06556 Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Mon, 15 May 2023 15:00:41 -0500 Subject: [PATCH 4/6] linting error --- netbox/ipam/forms/model_forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index 0a851345b02..aaa7abba8c2 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -350,7 +350,7 @@ def clean(self): self.add_error( '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. if interface: if address := self.cleaned_data.get('address'): From 0b0977d8427b28de5888657279318cda08bab574 Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Tue, 16 May 2023 11:45:18 -0500 Subject: [PATCH 5/6] Implement requested changes --- netbox/ipam/forms/model_forms.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index aaa7abba8c2..0a92cac6352 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -354,14 +354,17 @@ def clean(self): # 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) From 0338f566a23cc5346a2effef78cf02646c46cc0b Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 22 May 2023 16:02:34 -0400 Subject: [PATCH 6/6] Condensed the "if" logic a bit --- netbox/ipam/forms/model_forms.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index 0a92cac6352..ac75e2cc30b 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -352,19 +352,16 @@ def clean(self): ) # 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: - 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: - msg = f"{address} is a broadcast address, which may not be assigned to an interface." + if interface and (address := self.cleaned_data.get('address')): + if address.ip == address.network: + msg = f"{address} is a network ID, which may not be assigned to an interface." + if address.version == 4 and address.prefixlen not in (31, 32): raise ValidationError(msg) + if address.version == 6 and address.prefixlen not in (127, 128): + raise ValidationError(msg) + if address.ip == address.broadcast: + 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)