Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining when to try an IPv6 connection and when to use IPv4

I'm working on a network client program that connects to public servers, specified by the user. If the user gives me a hostname to connect to that has both IPv4 and IPv6 addresses (commonly, a DNS name with both A and AAAA records), I'm not sure how I should decide which address I should connect to.

The problem is that it's quite common for machines to support both IPv4 and IPv6, but only to have global connectivity over IPv4. The most common case of this is when only IPv6 link-local addresses are configured. At the moment the best alternatives I can come up with are:

  1. Try the IPv6 address(es) first - if the connection fails, try the IPv4 address(es); or
  2. Just let the user specify it as a config setting ("prefer_ipv6" versus "prefer_ipv4").

The problem I can see with option 1 is that the connection might not fail straight away - it might take quite a while to time out.

like image 504
caf Avatar asked Aug 29 '09 05:08

caf


People also ask

How does a computer choose whether to use IPv4 or IPv6?

The host must have DNS or hosts file entries for both IPv4 and IPv6 addresses. The host must also be configured to resolve host names and IP addresses from the DNS or from the hosts file. The network path between the two components must be enabled for IPv6 traffic.

Should I enable IPv6 or IPv4?

When possible, it is better to keep both IPv4 and IPv6 addresses enabled. For example, using only IPv6 can cause some accessibility issues, as only about one third of the internet supports IPv6 addresses. Likewise, disabling IPv6 can cause certain problems, especially if your router is already using an IPv6 address.

Why do we use IPv6 instead of IPv4?

The primary function of IPv6 is to allow for more unique TCP/IP address identifiers to be created, now that we've run out of the 4.3 billion created with IPv4. This is one of the main reasons why IPv6 is such an important innovation for the Internet of Things (IoT).

Should I use IPv4 or IPv6 on my router?

Best answer: IPv6 can potentially add support for more devices, better security, and more efficient connections. While some older software may not work as expected, most of your network should work fine with IPv6 enabled.


2 Answers

Please do try IPv6. In the significant majority of installations, trying to create an IPv6 connection will fail right away if it can't succeed for some reason:

  • if the system doesn't support IPv6 sockets, creating the socket will fail
  • if the system does support IPv6, and has link-local addresses configured, there won't be any routing table entry for the global IPv6 addresses. Again, the local kernel will report failure without sending any packets.
  • if the system does have a global IP address, but some link necessary for routing is missing, the source should be getting an ICMPv6 error message, indicating that the destination cannot be reached; likewise if the destination has an IPv6 address, but the service isn't listening on it.

There are of course cases where things can break, e.g. if a global (or tunnel) address is configured, and something falsely filters out ICMPv6 error messages. You shouldn't worry about this case - it may be just as well that IPv4 connectivity is somehow broken.

Of course, it's debatable whether you really need to try the IPv6 addresses first - you might just as well try them second. In general, you should try addresses in the order in which they are returned from getaddrinfo. Today, systems support configuration options that let administators decide in what order addresses should be returned from getaddrinfo.

like image 164
Martin v. Löwis Avatar answered Sep 30 '22 17:09

Martin v. Löwis


Subsequent to the question being asked the IETF has proposed an answer to this question with RFC6555, a.k.a. Happy Eyeballs.

The pertinent point being the client and server may both have IPv4 and IPv6 but a hop in between may not so it is impossible to reliably predict which path will work.

like image 25
Steve-o Avatar answered Sep 30 '22 16:09

Steve-o