Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

::1/128 is not a valid prefix #969

Closed
nniehoff opened this issue Oct 1, 2021 · 2 comments · Fixed by #975
Closed

::1/128 is not a valid prefix #969

nniehoff opened this issue Oct 1, 2021 · 2 comments · Fixed by #975
Assignees
Labels
type: bug Something isn't working as expected

Comments

@nniehoff
Copy link
Contributor

nniehoff commented Oct 1, 2021

Environment

  • Python version: 3.6
  • Nautobot version: 1.1.3

When trying to create the prefix ::1/128 I get the following error:

<class 'netaddr.core.AddrFormatError'>

invalid IPNetwork 0.0.0.1/128

Both Python netaddr and ipaddress modules see this as a valid IPNetwork.

Steps to Reproduce

  1. Create a prefix or aggregate using the prefix ::1/128

Expected Behavior

Prefix created

Observed Behavior

invalid IPNetwork 0.0.0.1/128
@FragmentedPacket
Copy link
Contributor

I dug into this and I don't know the solution, but what I can tell is that this is due to the VarBinaryIPField and how it gets stored in the DB as well as how it is extracted from the DB back into Python.

It will store the value in the DB for the IPv6 address as b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' which just equates to 1. There is no logic in VarbinaryIPField to know what family it needs to cast the binary to.

I get it to work for prefixes by adding the following to the cidr_str method on the Prefix model.

    @property
    def cidr_str(self):
        if self.prefix_length is not None and self.prefix_length > 32 and "." in self.network:
            ip_address = netaddr.IPAddress(self.network)
            return "%s/%s" % (ip_address.ipv6(True), self.prefix_length)
        if self.network is not None and self.prefix_length is not None:
            return "%s/%s" % (self.network, self.prefix_length)

This resolves the whole loading bug that we were getting, but the utilization is now 100% which is different than the 0.0.0.1/32 that I have for a test for ipv4.

It also still renders prefix.network as 0.0.0.1/32 when accessing the attribute.

>>> from nautobot.ipam.models import Prefix
>>> p = Prefix.objects.first()
>>> p
<Prefix: ::1/128>
>>> p.prefix.size
1
>>> p.get_child_ips()
<IPAddressQuerySet [<IPAddress: 0.0.0.1/32>]>
>>> Prefix.STATUS_CONTAINER
<Status: Container>
>>> p.vrf
>>> from nautobot.ipam.models import IPAddress
>>> p.prefix
IPNetwork('::1/128')
>>> IPAddress.objects.net_host_contained(p.prefix).filter(vrf=None)
<IPAddressQuerySet [<IPAddress: 0.0.0.1/32>]>
>>> p.prefix.broadcast
IPAddress('::1')
>>> p.network
'0.0.0.1'
>>> p.prefix.network

Hopefully this helps a little bit, but I don't know the best way to handle this in the long run. It would be great if we could determine within the VarbinaryIPField which family the IP should exist to (seems like it will most likely need to be via the prefix_length of the Prefix model.

@glennmatthews glennmatthews self-assigned this Oct 4, 2021
@glennmatthews glennmatthews added type: bug Something isn't working as expected status: accepted labels Oct 4, 2021
@glennmatthews
Copy link
Contributor

Thanks for the report and the investigation! I'm looking into this as well.

glennmatthews added a commit that referenced this issue Oct 4, 2021
glennmatthews added a commit that referenced this issue Oct 4, 2021
glennmatthews added a commit that referenced this issue Oct 4, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants