Skip to content

Commit bdcf4c4

Browse files
committed
Fixes #15220: Move IP mask validation logic from form to model
1 parent c45acf0 commit bdcf4c4

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

netbox/ipam/forms/model_forms.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,20 +367,6 @@ def clean(self):
367367
'primary_for_parent', _("Only IP addresses assigned to an interface can be designated as primary IPs.")
368368
)
369369

370-
# Do not allow assigning a network ID or broadcast address to an interface.
371-
if interface and (address := self.cleaned_data.get('address')):
372-
if address.ip == address.network:
373-
msg = _("{ip} is a network ID, which may not be assigned to an interface.").format(ip=address.ip)
374-
if address.version == 4 and address.prefixlen not in (31, 32):
375-
raise ValidationError(msg)
376-
if address.version == 6 and address.prefixlen not in (127, 128):
377-
raise ValidationError(msg)
378-
if address.version == 4 and address.ip == address.broadcast and address.prefixlen not in (31, 32):
379-
msg = _("{ip} is a broadcast address, which may not be assigned to an interface.").format(
380-
ip=address.ip
381-
)
382-
raise ValidationError(msg)
383-
384370
def save(self, *args, **kwargs):
385371
ipaddress = super().save(*args, **kwargs)
386372

netbox/ipam/models/ip.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,25 @@ def clean(self):
844844
'address': _("Cannot create IP address with /0 mask.")
845845
})
846846

847+
# Do not allow assigning a network ID or broadcast address to an interface.
848+
if self.assigned_object:
849+
if self.address.ip == self.address.network:
850+
msg = _("{ip} is a network ID, which may not be assigned to an interface.").format(
851+
ip=self.address.ip
852+
)
853+
if self.address.version == 4 and self.address.prefixlen not in (31, 32):
854+
raise ValidationError(msg)
855+
if self.address.version == 6 and self.address.prefixlen not in (127, 128):
856+
raise ValidationError(msg)
857+
if (
858+
self.address.version == 4 and self.address.ip == self.address.broadcast and
859+
self.address.prefixlen not in (31, 32)
860+
):
861+
msg = _("{ip} is a broadcast address, which may not be assigned to an interface.").format(
862+
ip=self.address.ip
863+
)
864+
raise ValidationError(msg)
865+
847866
# Enforce unique IP space (if applicable)
848867
if (self.vrf is None and get_config().ENFORCE_GLOBAL_UNIQUE) or (self.vrf and self.vrf.enforce_unique):
849868
duplicate_ips = self.get_duplicates()

0 commit comments

Comments
 (0)