From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37655) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGLUP-00087T-0W for qemu-devel@nongnu.org; Thu, 01 Jun 2017 04:29:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGLUM-0001Az-F6 for qemu-devel@nongnu.org; Thu, 01 Jun 2017 04:29:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44150) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGLUM-0001AT-6b for qemu-devel@nongnu.org; Thu, 01 Jun 2017 04:29:50 -0400 From: "Daniel P. Berrange" Date: Thu, 1 Jun 2017 09:29:31 +0100 Message-Id: <20170601082935.19993-2-berrange@redhat.com> In-Reply-To: <20170601082935.19993-1-berrange@redhat.com> References: <20170601082935.19993-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v3 1/5] sockets: ensure we can bind to both ipv4 & ipv6 separately List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Gerd Hoffmann , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eric Blake , "Daniel P. Berrange" When binding to an IPv6 socket we currently force the IPV6_V6ONLY flag to off. This means that the IPv6 socket will accept both IPv4 & IPv6 sockets when QEMU is launched with something like -vnc :::1 While this is good for that case, it is bad for other cases. For example if an empty hostname is given, getaddrinfo resolves it to 2 addresses 0.0.0.0 and ::, in that order. We will thus bind to 0.0.0.0 first, and then fail to bind to :: on the same port. The same problem can happen if any other hostname lookup causes the IPv4 address to be reported before the IPv6 address. When we get an IPv6 bind failure, we should re-try the same port, but with IPV6_V6ONLY turned on again, to avoid clash with any IPv4 listener. This ensures that -vnc :1 will bind successfully to both 0.0.0.0 and ::, and also avoid -vnc :1,to=3D2 from mistakenly using a 2nd port for the :: listener. Reviewed-by: Eric Blake Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Daniel P. Berrange --- util/qemu-sockets.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index b39ae74..8720097 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -208,22 +208,37 @@ static int inet_listen_saddr(InetSocketAddress *sad= dr, } =20 socket_set_fast_reuse(slisten); -#ifdef IPV6_V6ONLY - if (e->ai_family =3D=3D PF_INET6) { - /* listen on both ipv4 and ipv6 */ - const int off =3D 0; - qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &off, - sizeof(off)); - } -#endif =20 port_min =3D inet_getport(e); port_max =3D saddr->has_to ? saddr->to + port_offset : port_min; for (p =3D port_min; p <=3D port_max; p++) { +#ifdef IPV6_V6ONLY + /* listen on both ipv4 and ipv6 */ + int v6only =3D 0; +#endif inet_setport(e, p); +#ifdef IPV6_V6ONLY + rebind: + if (e->ai_family =3D=3D PF_INET6) { + qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &v6o= nly, + sizeof(v6only)); + } +#endif if (bind(slisten, e->ai_addr, e->ai_addrlen) =3D=3D 0) { goto listen; } + +#ifdef IPV6_V6ONLY + /* If we got EADDRINUSE from an IPv6 bind & V6ONLY is unset, + * it could be that the IPv4 port is already claimed, so ret= ry + * with V6ONLY set + */ + if (e->ai_family =3D=3D PF_INET6 && errno =3D=3D EADDRINUSE = && !v6only) { + v6only =3D 1; + goto rebind; + } +#endif + if (p =3D=3D port_max) { if (!e->ai_next) { error_setg_errno(errp, errno, "Failed to bind socket= "); --=20 2.9.3