Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions netbox/ipam/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,24 @@ def clean(self):

# Do not allow assigning a network ID or broadcast address 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):
allow_assignment_error = True
if self.instance.vrf is None:
prefix_obj = Prefix.objects.filter(prefix=address.cidr)
else:
prefix_obj = Prefix.objects.filter(prefix=address.cidr, vrf=self.vrf)
if prefix_obj.exists() and prefix_obj.first().is_pool:
allow_assignment_error = False

if allow_assignment_error:
if address.ip == address.network:
msg = f"{address} is a network ID, which may not be assigned to an interface unless the prefix is a pool."
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.version == 4 and address.ip == address.broadcast and address.prefixlen not in (31, 32):
msg = f"{address} is a broadcast address, which may not be assigned to an interface unless the prefix is a pool."
raise ValidationError(msg)
if address.version == 6 and address.prefixlen not in (127, 128):
raise ValidationError(msg)
if address.version == 4 and address.ip == address.broadcast and address.prefixlen not in (31, 32):
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)
Expand Down