opengeoip, a free GeoIP database built from the registries
Contents
When you want to know which country an IP address comes from, you almost always end up at MaxMind and its free GeoLite2 database. It works well, but it’s free without being open. You need an account and have to accept their license, and you can’t do whatever you want with the files. Above all, you don’t know how it’s built. When an address lands in the wrong country, there’s no way to find out why.
What I wanted most was a database that’s free to use and transparent. Free, so anyone can use it and redistribute it without asking anyone. Transparent, so every country in the database comes from a public source you can go and check, and anyone can rebuild it themselves.
That became opengeoip, a Rust program that produces .mmdb files in the same format as GeoLite2, plus the repositories that publish them every day. I’ll explain where the data comes from, how I decide who to trust, and where it still falls short.
The starting point, the registries
IP addresses are handed out by five regional registries, the RIRs. RIPE NCC for Europe, ARIN for North America, APNIC for Asia-Pacific, LACNIC and AFRINIC. Each of them publishes a file every day listing the blocks it has allocated, with the holder’s country.
My first idea was to query whois or RDAP for each block. I gave up very quickly. Rate limits make it impossible to redo every day, and ARIN flat out forbids compiling its database through RDAP. So I did the opposite. Instead of asking the registry a question for each block, once a day I download the complete files they publish themselves, like the list of every allocated block or the full export of their database. Everything else is then computed from those files.
These files alone already put roughly 95% of the IPv4 space in the same country as GeoLite2. The problem is that they give the holder’s country, not the one where the addresses are actually used.
Layers, from least to most precise
To go further, I stack sources. Each layer overwrites the previous one on the ranges it knows about.
The first one is those registry files. The second one is the inetnum objects that operators declare in the registry database when they sub-assign part of their block, with their own country field. A French operator using part of its block in Spain declares it there.
The case of 151.87.0.0/16 shows the difference well. It’s Fiat’s former network, now owned by Stellantis. The registry file puts it in France, at the holder’s headquarters, but the network is used in Italy, and the inetnum object describing it does say IT. With this layer, the block lands in the right country.
Geofeeds
The most precise source is geofeeds. It’s a plain CSV file, defined by RFC 8805, in which an operator says itself where its prefixes are, down to the city.
192.0.2.0/25,US,US-AL,,
192.0.2.5,US,US-AL,Alabaster,
2001:db8::/32,PL,PL-MZ,,02-784
The catch is that anyone can publish a file saying Google’s addresses are in North Korea. RFC 9632 fixes this by going through the registry. The operator adds a geofeed: line to its inetnum object, and only the block holder can edit that object. So I only keep a geofeed line if the most specific object covering that prefix points to that file.
For four registries, I get these links from the full exports of their database. ARIN doesn’t publish its own freely. I use the daily file from geofeed-finder, which does that work, and I check a sample of it over RDAP every time. If the sample doesn’t match, the day’s catalog isn’t published.
Even when a file comes from the right holder, what’s in it should never be taken at face value. Going back over the data, I found an XSS probe in place of a city name and a <marquee> tag in another file, and both had made it into the city database. Any tool showing the city without escaping it would have loaded the script. Now, a city name that’s too long or contains HTML is dropped, and the rest of the line is kept.
Some big players, like AWS or Google, publish a geofeed without declaring it in a registry. I list them by hand in a separate repository, opengeoip/geofeeds, with the publisher’s AS numbers. A line from these files is only kept if the prefix is announced by one of these ASes. So a hosting provider can’t describe the addresses of its customers who have their own AS.
Every night, this repository regenerates the full catalog from the registries and publishes a release. Today it holds a little under 7,000 geofeeds, 20 of them added by hand.
ASN, through BGP and RPKI
To find out which operator an address belongs to, I don’t use the registries but the routing tables. The RIPE RIS collectors publish what hundreds of BGP routers around the world see. For each prefix, I keep the origin AS seen by the most routers.
Anyone can announce anything in BGP, so I cross-check with RPKI, which says which AS is allowed to announce which prefix. At first, I only kept routes validated by RPKI. As a result, 35% of IPv4 had no ASN anymore, because very large operators like AT&T, Lumen or Cogent have no ROA on a good part of their blocks. Now I keep routes without a ROA and only exclude those RPKI marks as invalid. Agreement with GeoLite2 went up to 99.7% of IPv4.
There were some surprises. The 6to4 prefix, 2002::/16, is announced by Hurricane Electric. Since it covers a huge space, HE ended up owning 39% of IPv6. A 6to4 address contains an IPv4 address, so I now resolve it as that IPv4 address.
Measuring without fooling myself
Comparing against GeoLite2 is handy, but it doesn’t tell you who’s right. I needed ground truth. I use RIPE Atlas probes, small boxes hosted by volunteers all over the place, whose location is known. There are about 11,000 of them on IPv4.
Even there, you have to sort them. Some probes were placed automatically from a GeoIP database, about one in five on IPv4. Keeping them means grading your database against someone else’s. I also leave out the ones that lie about their position and anycast addresses, which have no real country.
On today’s database, 96.4% of IPv4 probes are in the right country, and 95.1% on IPv6. The overall figure hides big gaps. On regular ISPs, I’m at 99%. On hosting providers and clouds, I drop to around 83%.
What’s missing is the clouds
The errors mostly come from networks that publish no geofeed at all, like Oracle Cloud or Microsoft Azure. Their addresses are registered in the United States and used all over the world.
I could have coded exceptions, read each cloud’s per-region range files and turned them into countries. I didn’t. One rule per operator is one more rule to maintain by hand, and it does nothing for the thousands of others. I’d rather have a method that works for everyone, even if it costs a few points.
So I did the simplest thing, I wrote to the operators whose geofeed was broken or missing. A little over 200 emails. Many ended up with abuse teams, who took them for complaints. But close to twenty operators fixed their file or sent me the link, including Starlink in Indonesia.
Everything is rebuilt every night
It all runs in GitHub Actions. The catalog repository rebuilds the list of geofeeds and notifies the databases repository, which downloads all the sources, about 1 GB, builds the three databases and publishes a dated release.
Publishing stops on its own if the accuracy measured on Atlas drops by more than one point compared to the day before, or if a database changes size by more than 10%. So a broken source doesn’t reach production for people who pull the latest version.
The databases are also published as OCI images, one per database, containing only the file. You can mount them directly as a volume in Kubernetes, or copy them into your own image with a COPY --from. I’m actually wiring it into an Envoy right now to get the clients’ country in the logs.
Conclusion
The registry files give you a decent database almost for free. inetnum objects and above all geofeeds cover the rest of the way, as long as you only trust a file when the registry says it comes from the right holder. On the ASN side, BGP and RPKI are enough, as long as you don’t require an RPKI that half the Internet doesn’t have yet.
The hardest part was measuring honestly. Agreeing with GeoLite2 doesn’t prove you’re right, and it took the RIPE Atlas probes to find out where addresses are really used.
There’s still a real gap with the clouds, and I don’t see any other way than waiting for them to publish their geofeeds, or going and asking them. But most uses are about addresses from regular ISPs, people at home or on their phone, and that’s where the database is best, at 99%. For that, a database where you can check every answer, and that anyone can rebuild and redistribute, suits me just fine.