Skip to contentSkip to conversation

Merge sockets 2017/07/11

GIT PULL Applied Daniel P. Berrange 2 participants 6 messages Jul 14, 2017

lore
Share & tools
lore
#0 lore raw

The following changes since commit 6c6076662d98c068059983d411cb2a8987ba5670:

Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging (2017-07-14 12:16:09 +0100)

are available in the git repository at:

git://github.com/berrange/qemu tags/pull-sockets-2017-07-11-3

for you to fetch changes up to 563a3987b980a36e6941720a99d5cf36960f78ea:

io: preserve ipv4/ipv6 flags when resolving InetSocketAddress (2017-07-14 14:28:29 +0100)

----------------------------------------------------------------
Merge sockets 2017/07/11 v3

----------------------------------------------------------------

Daniel P. Berrange (4):
sockets: ensure we can bind to both ipv4 & ipv6 separately
sockets: don't block IPv4 clients when listening on "::"
sockets: ensure we don't accept IPv4 clients when IPv4 is disabled
io: preserve ipv4/ipv6 flags when resolving InetSocketAddress

 io/dns-resolver.c   |  6 +++--
 util/qemu-sockets.c | 71 +++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 56 insertions(+), 21 deletions(-)
Signature · 1 line
2.13.0
#1 lore raw

[Qemu-devel] [PULL v3 1/4] sockets: ensure we can bind to both ipv4 & ipv6 separately

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".

---
 util/qemu-sockets.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
util/qemu-sockets.c +23 -8
index 55b6c17f5f..3d2e51140e 100644
@@ -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");
Signature · 1 line
2.13.0
#2 lore raw

[Qemu-devel] [PULL v3 2/4] sockets: don't block IPv4 clients when listening on "::"

In reply to Daniel P. Berrange

When inet_parse() parses the hostname, it is forcing the has_ipv6 && ipv6 flags if the address contains a ":". This means that if the user had set the ipv4=on flag, to try to restrict the listener to just ipv4, an error would not have been raised. eg

-incoming tcp:[::]:9000,ipv4

should have raised an error because listening for IPv4 on "::" is a non-sensical combination. With this removed, we now call getaddrinfo() on "::" passing PF_INET and so getaddrinfo reports an error about the hostname being incompatible with the requested protocol:

qemu-system-x86_64: -incoming tcp:[::]:9000,ipv4: address resolution
failed for :::9000: Address family for hostname not supported

Likewise it is explicitly setting the has_ipv4 & ipv4 flags when the address contains only digits + '.'. This has no ill-effect, but also has no benefit, so is removed.

---
 util/qemu-sockets.c | 4 ----
 1 file changed, 4 deletions(-)
util/qemu-sockets.c +0 -4
index 3d2e51140e..8ec4b1a6c9 100644
@@ -618,16 +618,12 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp)
             error_setg(errp, "error parsing IPv6 address '%s'", str);
             return -1;
         }
-        addr->ipv6 = addr->has_ipv6 = true;
     } else {
         /* hostname or IPv4 addr */
         if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) {
             error_setg(errp, "error parsing address '%s'", str);
             return -1;
         }
-        if (host[strspn(host, "0123456789.")] == '\0') {
-            addr->ipv4 = addr->has_ipv4 = true;
-        }
     }
 
     addr->host = g_strdup(host);
Signature · 1 line
2.13.0
#3 lore raw

[Qemu-devel] [PULL v3 3/4] sockets: ensure we don't accept IPv4 clients when IPv4 is disabled

In reply to Daniel P. Berrange

Currently if you disable listening on IPv4 addresses, via the CLI flag ipv4=off, we still mistakenly accept IPv4 clients via the IPv6 listener socket due to IPV6_V6ONLY flag being unset.

We must ensure IPV6_V6ONLY is always set if ipv4=off

This fixes the following scenarios

-incoming tcp::9000,ipv6=on
-incoming tcp:[::]:9000,ipv6=on
-chardev socket,id=cdev0,host=,port=9000,server,nowait,ipv4=off -chardev socket,id=cdev0,host=,port=9000,server,nowait,ipv6=on -chardev socket,id=cdev0,host=::,port=9000,server,nowait,ipv4=off -chardev socket,id=cdev0,host=::,port=9000,server,nowait,ipv6=on

which all mistakenly accepted IPv4 clients

---
 util/qemu-sockets.c | 40 +++++++++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 9 deletions(-)
util/qemu-sockets.c +31 -9
index 8ec4b1a6c9..1358c81bcc 100644
@@ -104,17 +104,16 @@ NetworkAddressFamily inet_netfamily(int family)
  *   f     t       PF_INET6
  *   t     -       PF_INET
  *   t     f       PF_INET
- *   t     t       PF_INET6
+ *   t     t       PF_INET6/PF_UNSPEC
  *
  * NB, this matrix is only about getting the necessary results
  * from getaddrinfo(). Some of the cases require further work
  * after reading results from getaddrinfo in order to fully
- * apply the logic the end user wants. eg with the last case
- * ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
- * guarantee the ipv6=t part of the request - we need more
- * checks to provide ipv4=t part of the guarantee. This is
- * outside scope of this method and not currently handled by
- * callers at all.
+ * apply the logic the end user wants.
+ *
+ * In the first and last cases, we must set IPV6_V6ONLY=0
+ * when binding, to allow a single listener to potentially
+ * accept both IPv4+6 addresses.
  */
 int inet_ai_family_from_address(InetSocketAddress *addr,
                                 Error **errp)
@@ -124,6 +123,23 @@ int inet_ai_family_from_address(InetSocketAddress *addr,
         error_setg(errp, "Cannot disable IPv4 and IPv6 at same time");
         return PF_UNSPEC;
     }
+    if ((addr->has_ipv6 && addr->ipv6) && (addr->has_ipv4 && addr->ipv4)) {
+        /*
+         * Some backends can only do a single listener. In that case
+         * we want empty hostname to resolve to "::" and then use the
+         * flag IPV6_V6ONLY==0 to get both protocols on 1 socket. This
+         * doesn't work for addresses other than "", so they're just
+         * inevitably broken until multiple listeners can be used,
+         * and thus we honour getaddrinfo automatic protocol detection
+         * Once all backends do multi-listener, remove the PF_INET6
+         * branch entirely.
+         */
+        if (!addr->host || g_str_equal(addr->host, "")) {
+            return PF_INET6;
+        } else {
+            return PF_UNSPEC;
+        }
+    }
     if ((addr->has_ipv6 && addr->ipv6) || (addr->has_ipv4 && !addr->ipv4)) {
         return PF_INET6;
     }
@@ -213,8 +229,14 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
         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;
+            /*
+             * Deals with first & last cases in matrix in comment
+             * for inet_ai_family_from_address().
+             */
+            int v6only =
+                ((!saddr->has_ipv4 && !saddr->has_ipv6) ||
+                 (saddr->has_ipv4 && saddr->ipv4 &&
+                  saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1;
 #endif
             inet_setport(e, p);
 #ifdef IPV6_V6ONLY
Signature · 1 line
2.13.0
#4 lore raw

[Qemu-devel] [PULL v3 4/4] io: preserve ipv4/ipv6 flags when resolving InetSocketAddress

In reply to Daniel P. Berrange

The original InetSocketAddress struct may have has_ipv4 and has_ipv6 fields set, which will control both the ai_family used during DNS resolution, and later use of the V6ONLY flag.

Currently the standalone DNS resolver code drops the has_ipv4 & has_ipv6 flags after resolving, which means the later bind() code won't correctly set V6ONLY.

This fixes the following scenarios

-vnc :0,ipv4=off
-vnc :0,ipv6=on
-vnc :::0,ipv4=off
-vnc :::0,ipv6=on

which all mistakenly accepted IPv4 clients

---
 io/dns-resolver.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
io/dns-resolver.c +4 -2
index 57a8896cbb..c072d121c3 100644
@@ -116,8 +116,10 @@ static int qio_dns_resolver_lookup_sync_inet(QIODNSResolver *resolver,
             .numeric = true,
             .has_to = iaddr->has_to,
             .to = iaddr->to,
-            .has_ipv4 = false,
-            .has_ipv6 = false,
+            .has_ipv4 = iaddr->has_ipv4,
+            .ipv4 = iaddr->ipv4,
+            .has_ipv6 = iaddr->has_ipv6,
+            .ipv6 = iaddr->ipv6,
         };
 
         (*addrs)[i] = newaddr;
Signature · 1 line
2.13.0
#5 lore raw
In reply to Daniel P. Berrange
Quoted 23 lines · Daniel P. Berrange

The following changes since commit 6c6076662d98c068059983d411cb2a8987ba5670:

Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging (2017-07-14 12:16:09 +0100)

are available in the git repository at:

git://github.com/berrange/qemu tags/pull-sockets-2017-07-11-3

for you to fetch changes up to 563a3987b980a36e6941720a99d5cf36960f78ea:

io: preserve ipv4/ipv6 flags when resolving InetSocketAddress (2017-07-14 14:28:29 +0100)

----------------------------------------------------------------
Merge sockets 2017/07/11 v3

----------------------------------------------------------------

Daniel P. Berrange (4):
sockets: ensure we can bind to both ipv4 & ipv6 separately
sockets: don't block IPv4 clients when listening on "::"
sockets: ensure we don't accept IPv4 clients when IPv4 is disabled
io: preserve ipv4/ipv6 flags when resolving InetSocketAddress

Quoted 4 lines
 io/dns-resolver.c   |  6 +++--
 util/qemu-sockets.c | 71 +++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 56 insertions(+), 21 deletions(-)

Applied, thanks.

-- PMM