g-andrade / locus

MMDB reader for geolocation and ASN lookup of IP addresses
https://hexdocs.pm/locus/
MIT License
109 stars 14 forks source link

IPv4-in-IPv6 root node is wrong #44

Closed g-andrade closed 2 months ago

g-andrade commented 2 months ago

The definition in question: https://github.com/g-andrade/locus/blob/8d0019d3010fdd7a1bf6db68488d7be36eda7664/src/locus_mmdb_tree.erl#L48-L49

Following tentative PR #39 a few months ago and, more recently, evidence of this in #43 .

As per the spec, the root node seems to be ::/96 rather than ::ffff:0:0/96 (which is for IPv4-mapped IPv6 addresses) and I had read it wrong all this time: https://maxmind.github.io/MaxMind-DB/#ipv4-addresses-in-an-ipv6-tree

Official Python implementation mentioned by @nickjacob in #39, which further suggests this in the way it walks over the IPv6 tree to get the IPv4 root node: https://github.com/maxmind/MaxMind-DB-Reader-python/blob/b59335627a27b96c6e5a3178632ed0ab77d53cfb/maxminddb/reader.py#L118-L127

This being true, the existing behavior worked purely out of chance: all the databases I had tested so far happened to account for IPv4-mapped IPv6 addresses, and therefore I didn't run into the bug for 6+ years.

g-andrade commented 2 months ago

Fixed in 75e38f3.

g-andrade commented 2 months ago

Released in 2.3.8, pushed to Hex

oschwald commented 2 months ago

If there is a clarification to the specification that can be made, we would be happy to include that. The root node for IPv4 is at ::/96 with the other mapped locations being (optional) pointers to that. All the MaxMind databases are build with these pointers, but third-party databases may have disabled them.