Repository: netbox-community/netbox

Issue:
Validation of IP Addresses when of type network or broadcast
### NetBox version

v3.1.11

### Feature type

New functionality

### Proposed functionality

One of the many ways we use netbox is to assign IP addresses to device interfaces, which are used to push config changes to device.

Currently, one can add any ip address, including the network or broadcast address, to an interface. This is not a valid config from any devices standpoint. There are some obvious exceptions to this:

* /31 - rfc3021
* /32 - In our case, we set these to Role Loopback
* /127 - rfc6164

Are there other use cases other than above where an IP object on a broadcast or network address should be added? 

I would propose that some sort of validation happens when 'invalid' IP addresses are added to netbox. It's not entirely clear what the best validation here is but a few possibilities would be:

* don't accept the IP
* throw a big scary warning, but accept the IP

One could also consider a config knob to disable this sanity check and revert to current behavour.

It sounds like there may be a way to handle this via a custom validator, but it seems to be enough of a common issue that it seems worth discussion built in functionality. 

There is some discussion about this in [this slack thread.](https://netdev-community.slack.com/archives/C01P0FRSXRV/p1649339405458799)



### Use case

Should help with user friendliness of end users using netbox, preventing humans clumsy entering invalid data.

### Database changes

_No response_

### External dependencies

_No response_


Discussion:
> Are there other use cases other than above where an IP object on a broadcast or network address should be added?

I can't think of any, but in case there are, we could optionally apply this validation only to IP addresses which have been assigned to an object (e.g. interface). That I think would be reasonable regardless.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
I think this would be best blocked until we get #7845 implemented
@DanSheps I don't think this has any dependency on #7845, since the IP address object has all the necessary information locally.

Resolving patch:
diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py
--- a/netbox/ipam/forms/model_forms.py
+++ b/netbox/ipam/forms/model_forms.py
@@ -351,6 +351,18 @@ 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.
+        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)
 


Test patch: