From patchwork Fri Jul 7 15:15:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 785552 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3x3yrL2DVnz9s4s for ; Sat, 8 Jul 2017 01:16:48 +1000 (AEST) Received: from localhost ([::1]:57063 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dTUzt-0001PZ-TX for incoming@patchwork.ozlabs.org; Fri, 07 Jul 2017 11:16:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36103) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dTUzA-0001NL-HH for qemu-devel@nongnu.org; Fri, 07 Jul 2017 11:16:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dTUz7-0002Ol-EJ for qemu-devel@nongnu.org; Fri, 07 Jul 2017 11:16:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52776) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dTUz7-0002OO-5F for qemu-devel@nongnu.org; Fri, 07 Jul 2017 11:15:57 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3750580C07; Fri, 7 Jul 2017 15:15:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3750580C07 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=berrange@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3750580C07 Received: from t460.redhat.com (ovpn-116-132.ams2.redhat.com [10.36.116.132]) by smtp.corp.redhat.com (Postfix) with ESMTP id A4DDF7F55E; Fri, 7 Jul 2017 15:15:54 +0000 (UTC) From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Fri, 7 Jul 2017 16:15:42 +0100 Message-Id: <20170707151546.7893-2-berrange@redhat.com> In-Reply-To: <20170707151546.7893-1-berrange@redhat.com> References: <20170707151546.7893-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 07 Jul 2017 15:15:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v5 1/5] sockets: ensure we can bind to both ipv4 & ipv6 separately X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gerd Hoffmann , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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=2 from mistakenly using a 2nd port for the :: listener. This is a regression due to commit 396f935 "ui: add ability to specify multiple VNC listen addresses". Acked-by: Gerd Hoffmann Reviewed-by: Eric Blake Reviewed-by: Philippe Mathieu-Daudé 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 d3e5108..4c2f84b 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -208,22 +208,37 @@ static int inet_listen_saddr(InetSocketAddress *saddr, } socket_set_fast_reuse(slisten); -#ifdef IPV6_V6ONLY - if (e->ai_family == PF_INET6) { - /* listen on both ipv4 and ipv6 */ - const int off = 0; - qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &off, - sizeof(off)); - } -#endif port_min = inet_getport(e); port_max = saddr->has_to ? saddr->to + port_offset : port_min; for (p = port_min; p <= port_max; p++) { +#ifdef IPV6_V6ONLY + /* listen on both ipv4 and ipv6 */ + int v6only = 0; +#endif inet_setport(e, p); +#ifdef IPV6_V6ONLY + rebind: + if (e->ai_family == PF_INET6) { + qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, + sizeof(v6only)); + } +#endif if (bind(slisten, e->ai_addr, e->ai_addrlen) == 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 retry + * with V6ONLY set + */ + if (e->ai_family == PF_INET6 && errno == EADDRINUSE && !v6only) { + v6only = 1; + goto rebind; + } +#endif + if (p == port_max) { if (!e->ai_next) { error_setg_errno(errp, errno, "Failed to bind socket");