From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36067) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aIdy8-0004LJ-1g for qemu-devel@nongnu.org; Mon, 11 Jan 2016 10:01:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aIdy7-0004qx-1g for qemu-devel@nongnu.org; Mon, 11 Jan 2016 10:01:15 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40166) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aIdy6-0004qm-RJ for qemu-devel@nongnu.org; Mon, 11 Jan 2016 10:01:14 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 68AB18C1C0 for ; Mon, 11 Jan 2016 15:01:14 +0000 (UTC) From: "Daniel P. Berrange" Date: Mon, 11 Jan 2016 15:00:52 +0000 Message-Id: <1452524459-4132-9-git-send-email-berrange@redhat.com> In-Reply-To: <1452524459-4132-1-git-send-email-berrange@redhat.com> References: <1452524459-4132-1-git-send-email-berrange@redhat.com> Subject: [Qemu-devel] [PATCH v2 08/15] nbd: make server compliant with fixed newstyle spec List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini If the client does not request the fixed new style protocol, then we should only accept NBD_OPT_EXPORT_NAME. All other options are only valid when fixed new style has been activated. The qemu-nbd client doesn't currently request fixed new style protocol, but this change won't break qemu-nbd, because it fortunately only ever uses NBD_OPT_EXPORT_NAME, so was never triggering the non-compliant server behaviour. Signed-off-by: Daniel P. Berrange --- nbd.c | 68 ++++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/nbd.c b/nbd.c index bdfc45e..09a32a9 100644 --- a/nbd.c +++ b/nbd.c @@ -486,6 +486,7 @@ static int nbd_receive_options(NBDClient *client) { QIOChannel *ioc = client->ioc; uint32_t flags; + bool fixedNewstyle = false; /* Client sends: [ 0 .. 3] client flags @@ -507,14 +508,19 @@ static int nbd_receive_options(NBDClient *client) } TRACE("Checking client flags"); be32_to_cpus(&flags); - if (flags != 0 && flags != NBD_FLAG_C_FIXED_NEWSTYLE) { - LOG("Bad client flags received"); + if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) { + TRACE("Support supports fixed newstyle handshake"); + fixedNewstyle = true; + flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE; + } + if (flags != 0) { + TRACE("Unknown client flags 0x%x received", flags); return -EIO; } while (1) { int ret; - uint32_t tmp, length; + uint32_t clientflags, length; uint64_t magic; if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { @@ -527,10 +533,12 @@ static int nbd_receive_options(NBDClient *client) return -EINVAL; } - if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) { + if (read_sync(ioc, &clientflags, sizeof(clientflags)) != + sizeof(clientflags)) { LOG("read failed"); return -EINVAL; } + clientflags = be32_to_cpu(clientflags); if (read_sync(ioc, &length, sizeof(length)) != sizeof(length)) { LOG("read failed"); @@ -538,26 +546,40 @@ static int nbd_receive_options(NBDClient *client) } length = be32_to_cpu(length); - TRACE("Checking option"); - switch (be32_to_cpu(tmp)) { - case NBD_OPT_LIST: - ret = nbd_handle_list(client, length); - if (ret < 0) { - return ret; + TRACE("Checking option 0x%x", clientflags); + if (fixedNewstyle) { + switch (clientflags) { + case NBD_OPT_LIST: + ret = nbd_handle_list(client, length); + if (ret < 0) { + return ret; + } + break; + + case NBD_OPT_ABORT: + return -EINVAL; + + case NBD_OPT_EXPORT_NAME: + return nbd_handle_export_name(client, length); + + default: + TRACE("Unsupported option 0x%x", clientflags); + nbd_send_rep(client->ioc, NBD_REP_ERR_UNSUP, clientflags); + return -EINVAL; + } + } else { + /* + * If broken new-style we should drop the connection + * for anything except NBD_OPT_EXPORT_NAME + */ + switch (clientflags) { + case NBD_OPT_EXPORT_NAME: + return nbd_handle_export_name(client, length); + + default: + TRACE("Unsupported option 0x%x", clientflags); + return -EINVAL; } - break; - - case NBD_OPT_ABORT: - return -EINVAL; - - case NBD_OPT_EXPORT_NAME: - return nbd_handle_export_name(client, length); - - default: - tmp = be32_to_cpu(tmp); - LOG("Unsupported option 0x%x", tmp); - nbd_send_rep(client->ioc, NBD_REP_ERR_UNSUP, tmp); - return -EINVAL; } } } -- 2.5.0