docs, tests: Improved method to probe for plugins and filters.
[nbdkit/ericb.git] / server / protocol-handshake-newstyle.c
blob7179186fd7f24f5ea91c829f346384a7f6de14b4
1 /* nbdkit
2 * Copyright (C) 2013-2019 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <string.h>
40 #include <unistd.h>
42 #include "internal.h"
43 #include "byte-swapping.h"
44 #include "nbd-protocol.h"
45 #include "protostrings.h"
47 /* Maximum number of client options we allow before giving up. */
48 #define MAX_NR_OPTIONS 32
50 /* Receive newstyle options. */
51 static int
52 send_newstyle_option_reply (struct connection *conn,
53 uint32_t option, uint32_t reply)
55 struct nbd_fixed_new_option_reply fixed_new_option_reply;
57 fixed_new_option_reply.magic = htobe64 (NBD_REP_MAGIC);
58 fixed_new_option_reply.option = htobe32 (option);
59 fixed_new_option_reply.reply = htobe32 (reply);
60 fixed_new_option_reply.replylen = htobe32 (0);
62 if (conn->send (conn,
63 &fixed_new_option_reply,
64 sizeof fixed_new_option_reply, 0) == -1) {
65 /* The protocol document says that the client is allowed to simply
66 * drop the connection after sending NBD_OPT_ABORT, or may read
67 * the reply.
69 if (option == NBD_OPT_ABORT)
70 debug ("write: %s: %m", name_of_nbd_opt (option));
71 else
72 nbdkit_error ("write: %s: %m", name_of_nbd_opt (option));
73 return -1;
76 return 0;
79 static int
80 send_newstyle_option_reply_exportname (struct connection *conn,
81 uint32_t option, uint32_t reply)
83 struct nbd_fixed_new_option_reply fixed_new_option_reply;
84 size_t name_len = strlen (exportname);
85 uint32_t len;
87 fixed_new_option_reply.magic = htobe64 (NBD_REP_MAGIC);
88 fixed_new_option_reply.option = htobe32 (option);
89 fixed_new_option_reply.reply = htobe32 (reply);
90 fixed_new_option_reply.replylen = htobe32 (name_len + sizeof (len));
92 if (conn->send (conn,
93 &fixed_new_option_reply,
94 sizeof fixed_new_option_reply, SEND_MORE) == -1) {
95 nbdkit_error ("write: %s: %m", name_of_nbd_opt (option));
96 return -1;
99 len = htobe32 (name_len);
100 if (conn->send (conn, &len, sizeof len, SEND_MORE) == -1) {
101 nbdkit_error ("write: %s: %s: %m",
102 name_of_nbd_opt (option), "sending length");
103 return -1;
105 if (conn->send (conn, exportname, name_len, 0) == -1) {
106 nbdkit_error ("write: %s: %s: %m",
107 name_of_nbd_opt (option), "sending export name");
108 return -1;
111 return 0;
114 static int
115 send_newstyle_option_reply_info_export (struct connection *conn,
116 uint32_t option, uint32_t reply,
117 uint16_t info, uint64_t exportsize)
119 struct nbd_fixed_new_option_reply fixed_new_option_reply;
120 struct nbd_fixed_new_option_reply_info_export export;
122 fixed_new_option_reply.magic = htobe64 (NBD_REP_MAGIC);
123 fixed_new_option_reply.option = htobe32 (option);
124 fixed_new_option_reply.reply = htobe32 (reply);
125 fixed_new_option_reply.replylen = htobe32 (sizeof export);
126 export.info = htobe16 (info);
127 export.exportsize = htobe64 (exportsize);
128 export.eflags = htobe16 (conn->eflags);
130 if (conn->send (conn,
131 &fixed_new_option_reply,
132 sizeof fixed_new_option_reply, SEND_MORE) == -1 ||
133 conn->send (conn, &export, sizeof export, 0) == -1) {
134 nbdkit_error ("write: %s: %m", name_of_nbd_opt (option));
135 return -1;
138 return 0;
141 static int
142 send_newstyle_option_reply_meta_context (struct connection *conn,
143 uint32_t option, uint32_t reply,
144 uint32_t context_id,
145 const char *name)
147 struct nbd_fixed_new_option_reply fixed_new_option_reply;
148 struct nbd_fixed_new_option_reply_meta_context context;
149 const size_t namelen = strlen (name);
151 debug ("newstyle negotiation: %s: replying with %s id %d",
152 name_of_nbd_opt (option), name, context_id);
153 fixed_new_option_reply.magic = htobe64 (NBD_REP_MAGIC);
154 fixed_new_option_reply.option = htobe32 (option);
155 fixed_new_option_reply.reply = htobe32 (reply);
156 fixed_new_option_reply.replylen = htobe32 (sizeof context + namelen);
157 context.context_id = htobe32 (context_id);
159 if (conn->send (conn,
160 &fixed_new_option_reply,
161 sizeof fixed_new_option_reply, SEND_MORE) == -1 ||
162 conn->send (conn, &context, sizeof context, SEND_MORE) == -1 ||
163 conn->send (conn, name, namelen, 0) == -1) {
164 nbdkit_error ("write: %s: %m", name_of_nbd_opt (option));
165 return -1;
168 return 0;
171 /* Sub-function during negotiate_handshake_newstyle, to uniformly handle
172 * a client hanging up on a message boundary.
174 static int __attribute__ ((format (printf, 4, 5)))
175 conn_recv_full (struct connection *conn, void *buf, size_t len,
176 const char *fmt, ...)
178 int r = conn->recv (conn, buf, len);
179 va_list args;
181 if (r == -1) {
182 va_start (args, fmt);
183 nbdkit_verror (fmt, args);
184 va_end (args);
185 return -1;
187 if (r == 0) {
188 /* During negotiation, client EOF on message boundary is less
189 * severe than failure in the middle of the buffer. */
190 debug ("client closed input socket, closing connection");
191 return -1;
193 return r;
196 /* Sub-function of negotiate_handshake_newstyle_options below. It
197 * must be called on all non-error paths out of the options for-loop
198 * in that function, and must not cause any wire traffic.
200 static int
201 finish_newstyle_options (struct connection *conn, uint64_t *exportsize)
203 if (protocol_common_open (conn, exportsize, &conn->eflags) == -1)
204 return -1;
206 debug ("newstyle negotiation: flags: export 0x%x", conn->eflags);
207 return 0;
210 /* Check that the string sent as part of @option, beginning at @buf,
211 * and with a client-reported length of @len, fits within @maxlen
212 * bytes and is well-formed. If not, report an error mentioning
213 * @name.
215 static int
216 check_string (uint32_t option, char *buf, uint32_t len, uint32_t maxlen,
217 const char *name)
219 if (len > NBD_MAX_STRING || len > maxlen) {
220 nbdkit_error ("%s: %s too long", name_of_nbd_opt (option), name);
221 return -1;
223 if (strnlen (buf, len) != len) {
224 nbdkit_error ("%s: %s may not include NUL bytes",
225 name_of_nbd_opt (option), name);
226 return -1;
228 /* TODO: Check for valid UTF-8? */
229 return 0;
232 /* Sub-function of negotiate_handshake_newstyle_options, to grab and
233 * validate an export name.
235 static int
236 check_export_name (struct connection *conn, uint32_t option, char *buf,
237 uint32_t exportnamelen, uint32_t maxlen, bool save)
239 if (check_string (option, buf, exportnamelen, maxlen, "export name") == -1)
240 return -1;
242 assert (exportnamelen < sizeof conn->exportname);
243 if (save) {
244 if (exportnamelen != conn->exportnamelen ||
245 memcmp (conn->exportname, buf, exportnamelen) != 0)
246 conn->meta_context_base_allocation = false;
247 memcpy (conn->exportname, buf, exportnamelen);
248 conn->exportname[exportnamelen] = '\0';
249 conn->exportnamelen = exportnamelen;
251 debug ("newstyle negotiation: %s: client requested export '%.*s'",
252 name_of_nbd_opt (option), (int) exportnamelen, buf);
253 return 0;
256 static int
257 negotiate_handshake_newstyle_options (struct connection *conn)
259 struct nbd_new_option new_option;
260 size_t nr_options;
261 uint64_t version;
262 uint32_t option;
263 uint32_t optlen;
264 struct nbd_export_name_option_reply handshake_finish;
265 const char *optname;
266 uint64_t exportsize;
268 for (nr_options = 0; nr_options < MAX_NR_OPTIONS; ++nr_options) {
269 CLEANUP_FREE char *data = NULL;
271 if (conn_recv_full (conn, &new_option, sizeof new_option,
272 "reading option: conn->recv: %m") == -1)
273 return -1;
275 version = be64toh (new_option.version);
276 if (version != NBD_NEW_VERSION) {
277 nbdkit_error ("unknown option version %" PRIx64
278 ", expecting %" PRIx64,
279 version, NBD_NEW_VERSION);
280 return -1;
283 /* There is a maximum option length we will accept, regardless
284 * of the option type.
286 optlen = be32toh (new_option.optlen);
287 if (optlen > MAX_REQUEST_SIZE) {
288 nbdkit_error ("client option data too long (%" PRIu32 ")", optlen);
289 return -1;
291 data = malloc (optlen + 1); /* Allowing a trailing NUL helps some uses */
292 if (data == NULL) {
293 nbdkit_error ("malloc: %m");
294 return -1;
297 option = be32toh (new_option.option);
298 optname = name_of_nbd_opt (option);
300 /* If the client lacks fixed newstyle support, it should only send
301 * NBD_OPT_EXPORT_NAME.
303 if (!(conn->cflags & NBD_FLAG_FIXED_NEWSTYLE) &&
304 option != NBD_OPT_EXPORT_NAME) {
305 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID))
306 return -1;
307 continue;
310 /* In --tls=require / FORCEDTLS mode the only options allowed
311 * before TLS negotiation are NBD_OPT_ABORT and NBD_OPT_STARTTLS.
313 if (tls == 2 && !conn->using_tls &&
314 !(option == NBD_OPT_ABORT || option == NBD_OPT_STARTTLS)) {
315 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_TLS_REQD))
316 return -1;
317 continue;
320 switch (option) {
321 case NBD_OPT_EXPORT_NAME:
322 if (conn_recv_full (conn, data, optlen,
323 "read: %s: %m", name_of_nbd_opt (option)) == -1)
324 return -1;
325 if (check_export_name (conn, option, data, optlen, optlen, true) == -1)
326 return -1;
328 /* We have to finish the handshake by sending handshake_finish.
329 * On failure, we have to disconnect.
331 if (finish_newstyle_options (conn, &exportsize) == -1)
332 return -1;
334 memset (&handshake_finish, 0, sizeof handshake_finish);
335 handshake_finish.exportsize = htobe64 (exportsize);
336 handshake_finish.eflags = htobe16 (conn->eflags);
338 if (conn->send (conn,
339 &handshake_finish,
340 (conn->cflags & NBD_FLAG_NO_ZEROES)
341 ? offsetof (struct nbd_export_name_option_reply, zeroes)
342 : sizeof handshake_finish, 0) == -1) {
343 nbdkit_error ("write: %s: %m", optname);
344 return -1;
346 break;
348 case NBD_OPT_ABORT:
349 if (send_newstyle_option_reply (conn, option, NBD_REP_ACK) == -1)
350 return -1;
351 debug ("client sent %s to abort the connection",
352 name_of_nbd_opt (option));
353 return -1;
355 case NBD_OPT_LIST:
356 if (optlen != 0) {
357 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
358 == -1)
359 return -1;
360 if (conn_recv_full (conn, data, optlen,
361 "read: %s: %m", name_of_nbd_opt (option)) == -1)
362 return -1;
363 continue;
366 /* Send back the exportname. */
367 debug ("newstyle negotiation: %s: advertising export '%s'",
368 name_of_nbd_opt (option), exportname);
369 if (send_newstyle_option_reply_exportname (conn, option,
370 NBD_REP_SERVER) == -1)
371 return -1;
373 if (send_newstyle_option_reply (conn, option, NBD_REP_ACK) == -1)
374 return -1;
375 break;
377 case NBD_OPT_STARTTLS:
378 if (optlen != 0) {
379 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
380 == -1)
381 return -1;
382 if (conn_recv_full (conn, data, optlen,
383 "read: %s: %m", name_of_nbd_opt (option)) == -1)
384 return -1;
385 continue;
388 if (tls == 0) { /* --tls=off (NOTLS mode). */
389 #ifdef HAVE_GNUTLS
390 #define NO_TLS_REPLY NBD_REP_ERR_POLICY
391 #else
392 #define NO_TLS_REPLY NBD_REP_ERR_UNSUP
393 #endif
394 if (send_newstyle_option_reply (conn, option, NO_TLS_REPLY) == -1)
395 return -1;
397 else /* --tls=on or --tls=require */ {
398 /* We can't upgrade to TLS twice on the same connection. */
399 if (conn->using_tls) {
400 if (send_newstyle_option_reply (conn, option,
401 NBD_REP_ERR_INVALID) == -1)
402 return -1;
403 continue;
406 /* We have to send the (unencrypted) reply before starting
407 * the handshake.
409 if (send_newstyle_option_reply (conn, option, NBD_REP_ACK) == -1)
410 return -1;
412 /* Upgrade the connection to TLS. Also performs access control. */
413 if (crypto_negotiate_tls (conn, conn->sockin, conn->sockout) == -1)
414 return -1;
415 conn->using_tls = true;
416 debug ("using TLS on this connection");
418 break;
420 case NBD_OPT_INFO:
421 case NBD_OPT_GO:
422 if (conn_recv_full (conn, data, optlen,
423 "read: %s: %m", optname) == -1)
424 return -1;
426 if (optlen < 6) { /* 32 bit export length + 16 bit nr info */
427 debug ("newstyle negotiation: %s option length < 6", optname);
429 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
430 == -1)
431 return -1;
432 continue;
436 uint32_t exportnamelen;
437 uint16_t nrinfos;
438 uint16_t info;
439 size_t i;
441 /* Validate the name length and number of INFO requests. */
442 memcpy (&exportnamelen, &data[0], 4);
443 exportnamelen = be32toh (exportnamelen);
444 if (exportnamelen > optlen-6 /* NB optlen >= 6, see above */) {
445 debug ("newstyle negotiation: %s: export name too long", optname);
446 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
447 == -1)
448 return -1;
449 continue;
451 memcpy (&nrinfos, &data[exportnamelen+4], 2);
452 nrinfos = be16toh (nrinfos);
453 if (optlen != 4 + exportnamelen + 2 + 2*nrinfos) {
454 debug ("newstyle negotiation: %s: "
455 "number of information requests incorrect", optname);
456 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
457 == -1)
458 return -1;
459 continue;
462 /* As with NBD_OPT_EXPORT_NAME we print the export name and
463 * save it in the connection. If an earlier
464 * NBD_OPT_SET_META_CONTEXT used an export name, it must match
465 * or else we drop the support for that context.
467 if (check_export_name (conn, option, &data[4], exportnamelen,
468 optlen - 6, true) == -1) {
469 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
470 == -1)
471 return -1;
472 continue;
475 /* The spec is confusing, but it is required that we send back
476 * NBD_INFO_EXPORT, even if the client did not request it!
477 * qemu client in particular does not request this, but will
478 * fail if we don't send it. Note that if .open fails, but we
479 * succeed at .close, then we merely return an error to the
480 * client and let them try another NBD_OPT, rather than
481 * disconnecting.
483 if (finish_newstyle_options (conn, &exportsize) == -1) {
484 if (backend_finalize (backend, conn) == -1)
485 return -1;
486 backend_close (backend, conn);
487 if (send_newstyle_option_reply (conn, option,
488 NBD_REP_ERR_UNKNOWN) == -1)
489 return -1;
490 continue;
493 if (send_newstyle_option_reply_info_export (conn, option,
494 NBD_REP_INFO,
495 NBD_INFO_EXPORT,
496 exportsize) == -1)
497 return -1;
499 /* For now we ignore all other info requests (but we must
500 * ignore NBD_INFO_EXPORT if it was requested, because we
501 * replied already above). Therefore this loop doesn't do
502 * much at the moment.
504 for (i = 0; i < nrinfos; ++i) {
505 memcpy (&info, &data[4 + exportnamelen + 2 + i*2], 2);
506 info = be16toh (info);
507 switch (info) {
508 case NBD_INFO_EXPORT: /* ignore - reply sent above */ break;
509 default:
510 debug ("newstyle negotiation: %s: "
511 "ignoring NBD_INFO_* request %u (%s)",
512 optname, (unsigned) info, name_of_nbd_info (info));
513 break;
518 /* Unlike NBD_OPT_EXPORT_NAME, NBD_OPT_GO sends back an ACK
519 * or ERROR packet. If this was NBD_OPT_LIST, call .close.
521 if (send_newstyle_option_reply (conn, option, NBD_REP_ACK) == -1)
522 return -1;
524 if (option == NBD_OPT_INFO) {
525 if (backend_finalize (backend, conn) == -1)
526 return -1;
527 backend_close (backend, conn);
530 break;
532 case NBD_OPT_STRUCTURED_REPLY:
533 if (optlen != 0) {
534 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
535 == -1)
536 return -1;
537 if (conn_recv_full (conn, data, optlen,
538 "read: %s: %m", name_of_nbd_opt (option)) == -1)
539 return -1;
540 continue;
543 debug ("newstyle negotiation: %s: client requested structured replies",
544 name_of_nbd_opt (option));
546 if (no_sr) {
547 /* Must fail with ERR_UNSUP for qemu 4.2 to remain happy;
548 * but failing with ERR_POLICY would have been nicer.
550 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_UNSUP) == -1)
551 return -1;
552 debug ("newstyle negotiation: %s: structured replies are disabled",
553 name_of_nbd_opt (option));
554 break;
557 if (send_newstyle_option_reply (conn, option, NBD_REP_ACK) == -1)
558 return -1;
560 conn->structured_replies = true;
561 break;
563 case NBD_OPT_LIST_META_CONTEXT:
564 case NBD_OPT_SET_META_CONTEXT:
566 uint32_t opt_index;
567 uint32_t exportnamelen;
568 uint32_t nr_queries;
569 uint32_t querylen;
570 const char *what;
572 if (conn_recv_full (conn, data, optlen, "read: %s: %m", optname) == -1)
573 return -1;
575 /* Note that we support base:allocation whether or not the plugin
576 * supports can_extents.
578 if (!conn->structured_replies) {
579 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
580 == -1)
581 return -1;
582 continue;
585 /* Minimum length of the option payload is:
586 * 32 bit export name length followed by empty export name
587 * + 32 bit number of queries followed by no queries
588 * = 8 bytes.
590 what = "optlen < 8";
591 if (optlen < 8) {
592 opt_meta_invalid_option_len:
593 debug ("newstyle negotiation: %s: invalid option length: %s",
594 optname, what);
596 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_INVALID)
597 == -1)
598 return -1;
599 continue;
602 /* Remember the export name: the NBD spec says that if the client
603 * later uses NBD_OPT_GO on a different export, then the context
604 * returned here is not usable.
606 memcpy (&exportnamelen, &data[0], 4);
607 exportnamelen = be32toh (exportnamelen);
608 what = "validating export name";
609 if (check_export_name (conn, option, &data[4], exportnamelen,
610 optlen - 8,
611 option == NBD_OPT_SET_META_CONTEXT) == -1)
612 goto opt_meta_invalid_option_len;
613 opt_index = 4 + exportnamelen;
615 /* Read the number of queries. */
616 what = "reading number of queries";
617 if (opt_index+4 > optlen)
618 goto opt_meta_invalid_option_len;
619 memcpy (&nr_queries, &data[opt_index], 4);
620 nr_queries = be32toh (nr_queries);
621 opt_index += 4;
623 /* for LIST: nr_queries == 0 means return all meta contexts
624 * for SET: nr_queries == 0 means reset all contexts
626 debug ("newstyle negotiation: %s: %s count: %d", optname,
627 option == NBD_OPT_LIST_META_CONTEXT ? "query" : "set",
628 nr_queries);
629 if (option == NBD_OPT_SET_META_CONTEXT)
630 conn->meta_context_base_allocation = false;
631 if (nr_queries == 0) {
632 if (option == NBD_OPT_LIST_META_CONTEXT) {
633 if (send_newstyle_option_reply_meta_context
634 (conn, option, NBD_REP_META_CONTEXT,
635 0, "base:allocation") == -1)
636 return -1;
639 if (send_newstyle_option_reply (conn, option, NBD_REP_ACK) == -1)
640 return -1;
642 else {
643 /* Read and answer each query. */
644 while (nr_queries > 0) {
645 what = "reading query string length";
646 if (opt_index+4 > optlen)
647 goto opt_meta_invalid_option_len;
648 memcpy (&querylen, &data[opt_index], 4);
649 querylen = be32toh (querylen);
650 opt_index += 4;
651 what = "reading query string";
652 if (check_string (option, &data[opt_index], querylen,
653 optlen - opt_index, "meta context query") == -1)
654 goto opt_meta_invalid_option_len;
656 debug ("newstyle negotiation: %s: %s %.*s",
657 optname,
658 option == NBD_OPT_LIST_META_CONTEXT ? "query" : "set",
659 (int) querylen, &data[opt_index]);
661 /* For LIST, "base:" returns all supported contexts in the
662 * base namespace. We only support "base:allocation".
664 if (option == NBD_OPT_LIST_META_CONTEXT &&
665 querylen == 5 &&
666 strncmp (&data[opt_index], "base:", 5) == 0) {
667 if (send_newstyle_option_reply_meta_context
668 (conn, option, NBD_REP_META_CONTEXT,
669 0, "base:allocation") == -1)
670 return -1;
672 /* "base:allocation" requested by name. */
673 else if (querylen == 15 &&
674 strncmp (&data[opt_index], "base:allocation", 15) == 0) {
675 if (send_newstyle_option_reply_meta_context
676 (conn, option, NBD_REP_META_CONTEXT,
677 option == NBD_OPT_SET_META_CONTEXT
678 ? base_allocation_id : 0,
679 "base:allocation") == -1)
680 return -1;
681 if (option == NBD_OPT_SET_META_CONTEXT)
682 conn->meta_context_base_allocation = true;
684 /* Every other query must be ignored. */
686 opt_index += querylen;
687 nr_queries--;
689 if (send_newstyle_option_reply (conn, option, NBD_REP_ACK) == -1)
690 return -1;
692 debug ("newstyle negotiation: %s: reply complete", optname);
694 break;
696 default:
697 /* Unknown option. */
698 if (send_newstyle_option_reply (conn, option, NBD_REP_ERR_UNSUP) == -1)
699 return -1;
700 if (conn_recv_full (conn, data, optlen,
701 "reading unknown option data: conn->recv: %m") == -1)
702 return -1;
705 /* Note, since it's not very clear from the protocol doc, that the
706 * client must send NBD_OPT_EXPORT_NAME or NBD_OPT_GO last, and
707 * that ends option negotiation.
709 if (option == NBD_OPT_EXPORT_NAME || option == NBD_OPT_GO)
710 break;
713 if (nr_options >= MAX_NR_OPTIONS) {
714 nbdkit_error ("client exceeded maximum number of options (%d)",
715 MAX_NR_OPTIONS);
716 return -1;
719 /* In --tls=require / FORCEDTLS mode, we must have upgraded to TLS
720 * by the time we finish option negotiation. If not, give up.
722 if (tls == 2 && !conn->using_tls) {
723 nbdkit_error ("non-TLS client tried to connect in --tls=require mode");
724 return -1;
727 return 0;
731 protocol_handshake_newstyle (struct connection *conn)
733 struct nbd_new_handshake handshake;
734 uint16_t gflags;
736 gflags = (NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES) & mask_handshake;
738 debug ("newstyle negotiation: flags: global 0x%x", gflags);
740 handshake.nbdmagic = htobe64 (NBD_MAGIC);
741 handshake.version = htobe64 (NBD_NEW_VERSION);
742 handshake.gflags = htobe16 (gflags);
744 if (conn->send (conn, &handshake, sizeof handshake, 0) == -1) {
745 nbdkit_error ("write: %s: %m", "sending newstyle handshake");
746 return -1;
749 /* Client now sends us its 32 bit flags word ... */
750 if (conn_recv_full (conn, &conn->cflags, sizeof conn->cflags,
751 "reading initial client flags: conn->recv: %m") == -1)
752 return -1;
753 conn->cflags = be32toh (conn->cflags);
754 /* ... which we check for accuracy. */
755 debug ("newstyle negotiation: client flags: 0x%x", conn->cflags);
756 if (conn->cflags & ~gflags) {
757 nbdkit_error ("client requested unexpected flags 0x%x", conn->cflags);
758 return -1;
761 /* Receive newstyle options. */
762 if (negotiate_handshake_newstyle_options (conn) == -1)
763 return -1;
765 return 0;