From: Eric Blake <eblake@redhat.com>
To: "Daniel P. Berrange" <berrange@redhat.com>, qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>, Gerd Hoffmann <kraxel@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2 1/5] sockets: ensure we can bind to both ipv4 & ipv6 separately
Date: Mon, 22 May 2017 10:26:09 -0500 [thread overview]
Message-ID: <073211ce-d3df-da9f-8c70-cafc2dd1270e@redhat.com> (raw)
In-Reply-To: <20170519180342.19618-2-berrange@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 2643 bytes --]
On 05/19/2017 01:03 PM, Daniel P. Berrange wrote:
> 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.
>
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> ---
> util/qemu-sockets.c | 31 +++++++++++++++++++++++--------
> 1 file changed, 23 insertions(+), 8 deletions(-)
>
> 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) {
The rebind: label could go here with no change in semantics and one less
conditional when compiled without optimization. But hopefully the
compiler is smart enough to see that under -O2, so I don't see a reason
for you to change the code.
> + 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
> +
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
next prev parent reply other threads:[~2017-05-22 15:26 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-19 18:03 [Qemu-devel] [PATCH v2 0/5] Fix handling of IPv4/IPv6 dual stack Daniel P. Berrange
2017-05-19 18:03 ` [Qemu-devel] [PATCH v2 1/5] sockets: ensure we can bind to both ipv4 & ipv6 separately Daniel P. Berrange
2017-05-19 23:27 ` Philippe Mathieu-Daudé
2017-05-22 15:26 ` Eric Blake [this message]
2017-05-22 15:33 ` Daniel P. Berrange
2017-05-19 18:03 ` [Qemu-devel] [PATCH v2 2/5] sockets: don't block IPv4 clients when listening on "::" Daniel P. Berrange
2017-05-19 23:49 ` Philippe Mathieu-Daudé
2017-05-22 15:30 ` Eric Blake
2017-05-22 15:34 ` Daniel P. Berrange
2017-05-19 18:03 ` [Qemu-devel] [PATCH v2 3/5] sockets: ensure we don't accept IPv4 clients when IPv4 is disabled Daniel P. Berrange
2017-05-22 15:32 ` Eric Blake
2017-05-19 18:03 ` [Qemu-devel] [PATCH v2 4/5] io: preserve ipv4/ipv6 flags when resolving InetSocketAddress Daniel P. Berrange
2017-05-19 23:53 ` Philippe Mathieu-Daudé
2017-05-22 15:33 ` Eric Blake
2017-05-19 18:03 ` [Qemu-devel] [PATCH v2 5/5] tests: add functional test validating ipv4/ipv6 address flag handling Daniel P. Berrange
2017-05-22 16:00 ` Eric Blake
2017-05-22 16:56 ` Daniel P. Berrange
2017-05-22 17:30 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=073211ce-d3df-da9f-8c70-cafc2dd1270e@redhat.com \
--to=eblake@redhat.com \
--cc=berrange@redhat.com \
--cc=kraxel@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).