IP Range's get_child_ips does not return IP Addresses with different prefix lengths #11019

Closed
opened 2025-12-29 21:39:14 +01:00 by adam · 1 comment
Owner

Originally created by @mlow on GitHub (Apr 11, 2025).

Deployment Type

Self-hosted

NetBox Version

v4.2.7

Python Version

3.11

Steps to Reproduce

$ ./manage.py shell
>>> from netaddr import IPNetwork
>>> from ipam.models import IPRange, IPAddress
>>> range = IPRange.objects.create(start_address=IPNetwork('10.0.0.2/24'), end_address=IPNetwork('10.0.0.254/24'))
>>> IPAddress.objects.create(address='10.0.0.2/32')
>>> IPAddress.objects.create(address='10.0.0.3/24')
>>> range.get_child_ips()
<RestrictedQuerySet [<IPAddress: 10.0.0.3/24>]>

Expected Behavior

I would expect get_child_ips to return all IP addresses whose host part falls within the IP range. It seems odd that the prefix length should be considered at all when talking about IP Ranges.

Updating get_child_ips as follows would bring the behaviour I expect:

diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py
index e1a8d91..ae1f498 100644
--- a/netbox/ipam/models/ip.py
+++ b/netbox/ipam/models/ip.py
@@ -654,8 +654,8 @@ class IPRange(ContactsMixin, PrimaryModel):
         Return all IPAddresses within this IPRange and VRF.
         """
         return IPAddress.objects.filter(
-            address__gte=self.start_address,
-            address__lte=self.end_address,
+            address__host__inet__gte=self.start_address.ip,
+            address__host__inet__lte=self.end_address.ip,
             vrf=self.vrf
         )
 

Observed Behavior

Only IP Addresses with prefix length matching that of the IP Range are returned from IPRange.get_child_ips()

Originally created by @mlow on GitHub (Apr 11, 2025). ### Deployment Type Self-hosted ### NetBox Version v4.2.7 ### Python Version 3.11 ### Steps to Reproduce ```console $ ./manage.py shell >>> from netaddr import IPNetwork >>> from ipam.models import IPRange, IPAddress >>> range = IPRange.objects.create(start_address=IPNetwork('10.0.0.2/24'), end_address=IPNetwork('10.0.0.254/24')) >>> IPAddress.objects.create(address='10.0.0.2/32') >>> IPAddress.objects.create(address='10.0.0.3/24') >>> range.get_child_ips() <RestrictedQuerySet [<IPAddress: 10.0.0.3/24>]> ``` ### Expected Behavior I would expect `get_child_ips` to return all IP addresses whose host part falls within the IP range. It seems odd that the prefix length should be considered at all when talking about IP Ranges. Updating `get_child_ips` as follows would bring the behaviour I expect: ```diff diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index e1a8d91..ae1f498 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -654,8 +654,8 @@ class IPRange(ContactsMixin, PrimaryModel): Return all IPAddresses within this IPRange and VRF. """ return IPAddress.objects.filter( - address__gte=self.start_address, - address__lte=self.end_address, + address__host__inet__gte=self.start_address.ip, + address__host__inet__lte=self.end_address.ip, vrf=self.vrf ) ``` ### Observed Behavior Only IP Addresses with prefix length matching that of the IP Range are returned from `IPRange.get_child_ips()`
adam added the type: bug label 2025-12-29 21:39:14 +01:00
adam closed this issue 2025-12-29 21:39:14 +01:00
Author
Owner

@arthanson commented on GitHub (Apr 17, 2025):

This is by design, this should only return prefixes which are wholly within the range. You are free to put in a feature request if you can specify the use case and give a valid reason.

@arthanson commented on GitHub (Apr 17, 2025): This is by design, this should only return prefixes which are wholly within the range. You are free to put in a feature request if you can specify the use case and give a valid reason.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11019