From nobody Thu Dec 18 08:07:36 2025 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508452489737866.0166890648735; Thu, 19 Oct 2017 15:34:49 -0700 (PDT) Received: from localhost ([::1]:51456 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e5JOb-0002vv-TL for importer@patchew.org; Thu, 19 Oct 2017 18:34:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36135) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e5JHH-0005FY-Jw for qemu-devel@nongnu.org; Thu, 19 Oct 2017 18:27:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e5JHG-0004Kd-6Q for qemu-devel@nongnu.org; Thu, 19 Oct 2017 18:26:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46420) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e5JHB-0004B8-3H; Thu, 19 Oct 2017 18:26:53 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2C843267FB; Thu, 19 Oct 2017 22:26:52 +0000 (UTC) Received: from red.redhat.com (ovpn-121-90.rdu2.redhat.com [10.10.121.90]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6158E17AA7; Thu, 19 Oct 2017 22:26:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2C843267FB Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=eblake@redhat.com From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 19 Oct 2017 17:26:34 -0500 Message-Id: <20171019222637.17890-9-eblake@redhat.com> In-Reply-To: <20171019222637.17890-1-eblake@redhat.com> References: <20171019222637.17890-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 19 Oct 2017 22:26:52 +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 08/11] nbd/client: refactor nbd_receive_starttls 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: pbonzini@redhat.com, vsementsov@virtuozzo.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Split out nbd_request_simple_option to be reused for structured reply option. Signed-off-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v5: only check length for ACK responses v4: reduce redundant traces, typo fix in commit message --- nbd/client.c | 70 ++++++++++++++++++++++++++++++++++++++--------------= ---- nbd/trace-events | 4 +--- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 50f36b511e..78a0e9cdc3 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -508,35 +508,61 @@ static int nbd_receive_query_exports(QIOChannel *ioc, } } +/* nbd_request_simple_option: Send an option request, and parse the reply + * return 1 for successful negotiation, + * 0 if operation is unsupported, + * -1 with errp set for any other error + */ +static int nbd_request_simple_option(QIOChannel *ioc, int opt, Error **err= p) +{ + nbd_opt_reply reply; + int error; + + if (nbd_send_option_request(ioc, opt, 0, NULL, errp) < 0) { + return -1; + } + + if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) { + return -1; + } + error =3D nbd_handle_reply_err(ioc, &reply, errp); + if (error <=3D 0) { + return error; + } + + if (reply.type !=3D NBD_REP_ACK) { + error_setg(errp, "Server rejected request for option %d (%s) " + "with reply %" PRIx32 " (%s)", opt, nbd_opt_lookup(opt), + reply.type, nbd_rep_lookup(reply.type)); + nbd_send_opt_abort(ioc); + return -1; + } + + if (reply.length !=3D 0) { + error_setg(errp, "Option %d ('%s') response length is %" PRIu32 + " (it should be zero)", opt, nbd_opt_lookup(opt), + reply.length); + nbd_send_opt_abort(ioc); + return -1; + } + + return 1; +} + static QIOChannel *nbd_receive_starttls(QIOChannel *ioc, QCryptoTLSCreds *tlscreds, const char *hostname, Error **errp) { - nbd_opt_reply reply; + int ret; QIOChannelTLS *tioc; struct NBDTLSHandshakeData data =3D { 0 }; - trace_nbd_receive_starttls_request(); - if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0)= { - return NULL; - } - - trace_nbd_receive_starttls_reply(); - if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0)= { - return NULL; - } - - if (reply.type !=3D NBD_REP_ACK) { - error_setg(errp, "Server rejected request to start TLS %" PRIx32, - reply.type); - nbd_send_opt_abort(ioc); - return NULL; - } - - if (reply.length !=3D 0) { - error_setg(errp, "Start TLS response was not zero %" PRIu32, - reply.length); - nbd_send_opt_abort(ioc); + ret =3D nbd_request_simple_option(ioc, NBD_OPT_STARTTLS, errp); + if (ret <=3D 0) { + if (ret =3D=3D 0) { + error_setg(errp, "Server don't support STARTTLS option"); + nbd_send_opt_abort(ioc); + } return NULL; } diff --git a/nbd/trace-events b/nbd/trace-events index 52150bd738..596df96575 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -8,9 +8,7 @@ nbd_opt_go_info_unknown(int info, const char *name) "Ignori= ng unknown info %d (% nbd_opt_go_info_block_size(uint32_t minimum, uint32_t preferred, uint32_t = maximum) "Block sizes are 0x%" PRIx32 ", 0x%" PRIx32 ", 0x%" PRIx32 nbd_receive_query_exports_start(const char *wantname) "Querying export lis= t for '%s'" nbd_receive_query_exports_success(const char *wantname) "Found desired exp= ort name '%s'" -nbd_receive_starttls_request(void) "Requesting TLS from server" -nbd_receive_starttls_reply(void) "Getting TLS reply from server" -nbd_receive_starttls_new_client(void) "TLS request approved, setting up TL= S" +nbd_receive_starttls_new_client(void) "Setting up TLS" nbd_receive_starttls_tls_handshake(void) "Starting TLS handshake" nbd_receive_negotiate(void *tlscreds, const char *hostname) "Receiving neg= otiation tlscreds=3D%p hostname=3D%s" nbd_receive_negotiate_magic(uint64_t magic) "Magic is 0x%" PRIx64 --=20 2.13.6