]>
Commit | Line | Data |
---|---|---|
798bfe00 | 1 | /* |
b626b51a | 2 | * Copyright (C) 2016 Red Hat, Inc. |
798bfe00 FZ |
3 | * Copyright (C) 2005 Anthony Liguori <[email protected]> |
4 | * | |
5 | * Network Block Device Client Side | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; under version 2 of the License. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
d38ea87a | 20 | #include "qemu/osdep.h" |
da34e65c | 21 | #include "qapi/error.h" |
798bfe00 FZ |
22 | #include "nbd-internal.h" |
23 | ||
24 | static int nbd_errno_to_system_errno(int err) | |
25 | { | |
8b34a9db | 26 | int ret; |
798bfe00 FZ |
27 | switch (err) { |
28 | case NBD_SUCCESS: | |
8b34a9db EB |
29 | ret = 0; |
30 | break; | |
798bfe00 | 31 | case NBD_EPERM: |
8b34a9db EB |
32 | ret = EPERM; |
33 | break; | |
798bfe00 | 34 | case NBD_EIO: |
8b34a9db EB |
35 | ret = EIO; |
36 | break; | |
798bfe00 | 37 | case NBD_ENOMEM: |
8b34a9db EB |
38 | ret = ENOMEM; |
39 | break; | |
798bfe00 | 40 | case NBD_ENOSPC: |
8b34a9db EB |
41 | ret = ENOSPC; |
42 | break; | |
798bfe00 | 43 | default: |
f3c32fce EB |
44 | TRACE("Squashing unexpected error %d to EINVAL", err); |
45 | /* fallthrough */ | |
46 | case NBD_EINVAL: | |
8b34a9db EB |
47 | ret = EINVAL; |
48 | break; | |
798bfe00 | 49 | } |
8b34a9db | 50 | return ret; |
798bfe00 FZ |
51 | } |
52 | ||
53 | /* Definitions for opaque data types */ | |
54 | ||
55 | static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); | |
56 | ||
57 | /* That's all folks */ | |
58 | ||
59 | /* Basic flow for negotiation | |
60 | ||
61 | Server Client | |
62 | Negotiate | |
63 | ||
64 | or | |
65 | ||
66 | Server Client | |
67 | Negotiate #1 | |
68 | Option | |
69 | Negotiate #2 | |
70 | ||
71 | ---- | |
72 | ||
73 | followed by | |
74 | ||
75 | Server Client | |
76 | Request | |
77 | Response | |
78 | Request | |
79 | Response | |
80 | ... | |
81 | ... | |
82 | Request (type == 2) | |
83 | ||
84 | */ | |
85 | ||
7d3123e1 EB |
86 | /* Discard length bytes from channel. Return -errno on failure, or |
87 | * the amount of bytes consumed. */ | |
88 | static ssize_t drop_sync(QIOChannel *ioc, size_t size) | |
89 | { | |
90 | ssize_t ret, dropped = size; | |
91 | char small[1024]; | |
92 | char *buffer; | |
93 | ||
94 | buffer = sizeof(small) < size ? small : g_malloc(MIN(65536, size)); | |
95 | while (size > 0) { | |
96 | ret = read_sync(ioc, buffer, MIN(65536, size)); | |
97 | if (ret < 0) { | |
98 | goto cleanup; | |
99 | } | |
100 | assert(ret <= size); | |
101 | size -= ret; | |
102 | } | |
103 | ret = dropped; | |
104 | ||
105 | cleanup: | |
106 | if (buffer != small) { | |
107 | g_free(buffer); | |
108 | } | |
109 | return ret; | |
110 | } | |
111 | ||
c8a3a1b6 EB |
112 | /* Send an option request. |
113 | * | |
114 | * The request is for option @opt, with @data containing @len bytes of | |
115 | * additional payload for the request (@len may be -1 to treat @data as | |
116 | * a C string; and @data may be NULL if @len is 0). | |
117 | * Return 0 if successful, -1 with errp set if it is impossible to | |
118 | * continue. */ | |
119 | static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt, | |
120 | uint32_t len, const char *data, | |
121 | Error **errp) | |
122 | { | |
123 | nbd_option req; | |
124 | QEMU_BUILD_BUG_ON(sizeof(req) != 16); | |
125 | ||
126 | if (len == -1) { | |
127 | req.length = len = strlen(data); | |
128 | } | |
129 | TRACE("Sending option request %" PRIu32", len %" PRIu32, opt, len); | |
130 | ||
131 | stq_be_p(&req.magic, NBD_OPTS_MAGIC); | |
132 | stl_be_p(&req.option, opt); | |
133 | stl_be_p(&req.length, len); | |
134 | ||
135 | if (write_sync(ioc, &req, sizeof(req)) != sizeof(req)) { | |
136 | error_setg(errp, "Failed to send option request header"); | |
137 | return -1; | |
138 | } | |
139 | ||
140 | if (len && write_sync(ioc, (char *) data, len) != len) { | |
141 | error_setg(errp, "Failed to send option request data"); | |
142 | return -1; | |
143 | } | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
2cdbf413 EB |
148 | /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are |
149 | * not going to attempt further negotiation. */ | |
150 | static void nbd_send_opt_abort(QIOChannel *ioc) | |
151 | { | |
152 | /* Technically, a compliant server is supposed to reply to us; but | |
153 | * older servers disconnected instead. At any rate, we're allowed | |
154 | * to disconnect without waiting for the server reply, so we don't | |
155 | * even care if the request makes it to the server, let alone | |
156 | * waiting around for whether the server replies. */ | |
157 | nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL); | |
158 | } | |
159 | ||
160 | ||
c8a3a1b6 EB |
161 | /* Receive the header of an option reply, which should match the given |
162 | * opt. Read through the length field, but NOT the length bytes of | |
163 | * payload. Return 0 if successful, -1 with errp set if it is | |
164 | * impossible to continue. */ | |
165 | static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt, | |
166 | nbd_opt_reply *reply, Error **errp) | |
167 | { | |
168 | QEMU_BUILD_BUG_ON(sizeof(*reply) != 20); | |
169 | if (read_sync(ioc, reply, sizeof(*reply)) != sizeof(*reply)) { | |
170 | error_setg(errp, "failed to read option reply"); | |
2cdbf413 | 171 | nbd_send_opt_abort(ioc); |
c8a3a1b6 EB |
172 | return -1; |
173 | } | |
174 | be64_to_cpus(&reply->magic); | |
175 | be32_to_cpus(&reply->option); | |
176 | be32_to_cpus(&reply->type); | |
177 | be32_to_cpus(&reply->length); | |
178 | ||
179 | TRACE("Received option reply %" PRIx32", type %" PRIx32", len %" PRIu32, | |
180 | reply->option, reply->type, reply->length); | |
9344e5f5 | 181 | |
c8a3a1b6 EB |
182 | if (reply->magic != NBD_REP_MAGIC) { |
183 | error_setg(errp, "Unexpected option reply magic"); | |
2cdbf413 | 184 | nbd_send_opt_abort(ioc); |
c8a3a1b6 EB |
185 | return -1; |
186 | } | |
187 | if (reply->option != opt) { | |
188 | error_setg(errp, "Unexpected option type %x expected %x", | |
189 | reply->option, opt); | |
2cdbf413 | 190 | nbd_send_opt_abort(ioc); |
c8a3a1b6 EB |
191 | return -1; |
192 | } | |
193 | return 0; | |
194 | } | |
195 | ||
196 | /* If reply represents success, return 1 without further action. | |
197 | * If reply represents an error, consume the optional payload of | |
198 | * the packet on ioc. Then return 0 for unsupported (so the client | |
199 | * can fall back to other approaches), or -1 with errp set for other | |
200 | * errors. | |
6ff58164 | 201 | */ |
c8a3a1b6 | 202 | static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply, |
6ff58164 | 203 | Error **errp) |
9344e5f5 | 204 | { |
6ff58164 AB |
205 | char *msg = NULL; |
206 | int result = -1; | |
207 | ||
c8a3a1b6 | 208 | if (!(reply->type & (1 << 31))) { |
6ff58164 AB |
209 | return 1; |
210 | } | |
211 | ||
c8a3a1b6 EB |
212 | if (reply->length) { |
213 | if (reply->length > NBD_MAX_BUFFER_SIZE) { | |
6ff58164 AB |
214 | error_setg(errp, "server's error message is too long"); |
215 | goto cleanup; | |
216 | } | |
c8a3a1b6 EB |
217 | msg = g_malloc(reply->length + 1); |
218 | if (read_sync(ioc, msg, reply->length) != reply->length) { | |
6ff58164 AB |
219 | error_setg(errp, "failed to read option error message"); |
220 | goto cleanup; | |
221 | } | |
c8a3a1b6 | 222 | msg[reply->length] = '\0'; |
9344e5f5 DB |
223 | } |
224 | ||
c8a3a1b6 | 225 | switch (reply->type) { |
9344e5f5 | 226 | case NBD_REP_ERR_UNSUP: |
2cb34749 | 227 | TRACE("server doesn't understand request %" PRIx32 |
c8a3a1b6 | 228 | ", attempting fallback", reply->option); |
6ff58164 AB |
229 | result = 0; |
230 | goto cleanup; | |
9344e5f5 | 231 | |
f95910fe | 232 | case NBD_REP_ERR_POLICY: |
c8a3a1b6 EB |
233 | error_setg(errp, "Denied by server for option %" PRIx32, |
234 | reply->option); | |
f95910fe DB |
235 | break; |
236 | ||
9344e5f5 | 237 | case NBD_REP_ERR_INVALID: |
c8a3a1b6 EB |
238 | error_setg(errp, "Invalid data length for option %" PRIx32, |
239 | reply->option); | |
9344e5f5 DB |
240 | break; |
241 | ||
f95910fe | 242 | case NBD_REP_ERR_TLS_REQD: |
2cb34749 | 243 | error_setg(errp, "TLS negotiation required before option %" PRIx32, |
c8a3a1b6 | 244 | reply->option); |
f95910fe DB |
245 | break; |
246 | ||
9344e5f5 | 247 | default: |
2cb34749 | 248 | error_setg(errp, "Unknown error code when asking for option %" PRIx32, |
c8a3a1b6 | 249 | reply->option); |
9344e5f5 DB |
250 | break; |
251 | } | |
252 | ||
6ff58164 AB |
253 | if (msg) { |
254 | error_append_hint(errp, "%s\n", msg); | |
255 | } | |
256 | ||
257 | cleanup: | |
258 | g_free(msg); | |
2cdbf413 EB |
259 | if (result < 0) { |
260 | nbd_send_opt_abort(ioc); | |
261 | } | |
6ff58164 | 262 | return result; |
9344e5f5 DB |
263 | } |
264 | ||
75368aab EB |
265 | /* Process another portion of the NBD_OPT_LIST reply. Set *@match if |
266 | * the current reply matches @want or if the server does not support | |
267 | * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration | |
268 | * is complete, positive if more replies are expected, or negative | |
269 | * with @errp set if an unrecoverable error occurred. */ | |
270 | static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match, | |
271 | Error **errp) | |
9344e5f5 | 272 | { |
c8a3a1b6 | 273 | nbd_opt_reply reply; |
9344e5f5 DB |
274 | uint32_t len; |
275 | uint32_t namelen; | |
75368aab | 276 | char name[NBD_MAX_NAME_SIZE + 1]; |
6ff58164 | 277 | int error; |
9344e5f5 | 278 | |
c8a3a1b6 | 279 | if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) { |
9344e5f5 DB |
280 | return -1; |
281 | } | |
c8a3a1b6 | 282 | error = nbd_handle_reply_err(ioc, &reply, errp); |
6ff58164 | 283 | if (error <= 0) { |
75368aab EB |
284 | /* The server did not support NBD_OPT_LIST, so set *match on |
285 | * the assumption that any name will be accepted. */ | |
286 | *match = true; | |
6ff58164 | 287 | return error; |
9344e5f5 | 288 | } |
c8a3a1b6 | 289 | len = reply.length; |
9344e5f5 | 290 | |
c8a3a1b6 | 291 | if (reply.type == NBD_REP_ACK) { |
9344e5f5 DB |
292 | if (len != 0) { |
293 | error_setg(errp, "length too long for option end"); | |
2cdbf413 | 294 | nbd_send_opt_abort(ioc); |
9344e5f5 DB |
295 | return -1; |
296 | } | |
75368aab EB |
297 | return 0; |
298 | } else if (reply.type != NBD_REP_SERVER) { | |
299 | error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x", | |
300 | reply.type, NBD_REP_SERVER); | |
301 | nbd_send_opt_abort(ioc); | |
302 | return -1; | |
303 | } | |
9344e5f5 | 304 | |
75368aab EB |
305 | if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) { |
306 | error_setg(errp, "incorrect option length %" PRIu32, len); | |
307 | nbd_send_opt_abort(ioc); | |
308 | return -1; | |
309 | } | |
310 | if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) { | |
311 | error_setg(errp, "failed to read option name length"); | |
312 | nbd_send_opt_abort(ioc); | |
313 | return -1; | |
314 | } | |
315 | namelen = be32_to_cpu(namelen); | |
316 | len -= sizeof(namelen); | |
317 | if (len < namelen) { | |
318 | error_setg(errp, "incorrect option name length"); | |
319 | nbd_send_opt_abort(ioc); | |
320 | return -1; | |
321 | } | |
322 | if (namelen != strlen(want)) { | |
7d3123e1 | 323 | if (drop_sync(ioc, len) != len) { |
75368aab | 324 | error_setg(errp, "failed to skip export name with wrong length"); |
7d3123e1 EB |
325 | nbd_send_opt_abort(ioc); |
326 | return -1; | |
200650d4 | 327 | } |
75368aab EB |
328 | return 1; |
329 | } | |
330 | ||
331 | assert(namelen < sizeof(name)); | |
332 | if (read_sync(ioc, name, namelen) != namelen) { | |
333 | error_setg(errp, "failed to read export name"); | |
334 | nbd_send_opt_abort(ioc); | |
335 | return -1; | |
336 | } | |
337 | name[namelen] = '\0'; | |
338 | len -= namelen; | |
339 | if (drop_sync(ioc, len) != len) { | |
340 | error_setg(errp, "failed to read export description"); | |
2cdbf413 | 341 | nbd_send_opt_abort(ioc); |
9344e5f5 DB |
342 | return -1; |
343 | } | |
75368aab EB |
344 | if (!strcmp(name, want)) { |
345 | *match = true; | |
346 | } | |
9344e5f5 DB |
347 | return 1; |
348 | } | |
349 | ||
350 | ||
75368aab | 351 | /* Return -1 on failure, 0 if wantname is an available export. */ |
9344e5f5 DB |
352 | static int nbd_receive_query_exports(QIOChannel *ioc, |
353 | const char *wantname, | |
354 | Error **errp) | |
355 | { | |
9344e5f5 DB |
356 | bool foundExport = false; |
357 | ||
75368aab | 358 | TRACE("Querying export list for '%s'", wantname); |
c8a3a1b6 | 359 | if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) { |
9344e5f5 DB |
360 | return -1; |
361 | } | |
362 | ||
363 | TRACE("Reading available export names"); | |
364 | while (1) { | |
75368aab | 365 | int ret = nbd_receive_list(ioc, wantname, &foundExport, errp); |
9344e5f5 DB |
366 | |
367 | if (ret < 0) { | |
75368aab | 368 | /* Server gave unexpected reply */ |
9344e5f5 | 369 | return -1; |
75368aab EB |
370 | } else if (ret == 0) { |
371 | /* Done iterating. */ | |
372 | if (!foundExport) { | |
373 | error_setg(errp, "No export with name '%s' available", | |
374 | wantname); | |
375 | nbd_send_opt_abort(ioc); | |
376 | return -1; | |
377 | } | |
378 | TRACE("Found desired export name '%s'", wantname); | |
379 | return 0; | |
9344e5f5 | 380 | } |
9344e5f5 | 381 | } |
9344e5f5 DB |
382 | } |
383 | ||
f95910fe DB |
384 | static QIOChannel *nbd_receive_starttls(QIOChannel *ioc, |
385 | QCryptoTLSCreds *tlscreds, | |
386 | const char *hostname, Error **errp) | |
387 | { | |
c8a3a1b6 | 388 | nbd_opt_reply reply; |
f95910fe DB |
389 | QIOChannelTLS *tioc; |
390 | struct NBDTLSHandshakeData data = { 0 }; | |
391 | ||
392 | TRACE("Requesting TLS from server"); | |
c8a3a1b6 | 393 | if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) { |
f95910fe DB |
394 | return NULL; |
395 | } | |
396 | ||
397 | TRACE("Getting TLS reply from server"); | |
c8a3a1b6 | 398 | if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) { |
f95910fe DB |
399 | return NULL; |
400 | } | |
c8a3a1b6 EB |
401 | |
402 | if (reply.type != NBD_REP_ACK) { | |
2cb34749 | 403 | error_setg(errp, "Server rejected request to start TLS %" PRIx32, |
c8a3a1b6 | 404 | reply.type); |
2cdbf413 | 405 | nbd_send_opt_abort(ioc); |
f95910fe DB |
406 | return NULL; |
407 | } | |
408 | ||
c8a3a1b6 | 409 | if (reply.length != 0) { |
2cb34749 | 410 | error_setg(errp, "Start TLS response was not zero %" PRIu32, |
c8a3a1b6 | 411 | reply.length); |
2cdbf413 | 412 | nbd_send_opt_abort(ioc); |
f95910fe DB |
413 | return NULL; |
414 | } | |
415 | ||
416 | TRACE("TLS request approved, setting up TLS"); | |
417 | tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp); | |
418 | if (!tioc) { | |
419 | return NULL; | |
420 | } | |
0d73f725 | 421 | qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls"); |
f95910fe | 422 | data.loop = g_main_loop_new(g_main_context_default(), FALSE); |
2cb34749 | 423 | TRACE("Starting TLS handshake"); |
f95910fe DB |
424 | qio_channel_tls_handshake(tioc, |
425 | nbd_tls_handshake, | |
426 | &data, | |
427 | NULL); | |
428 | ||
429 | if (!data.complete) { | |
430 | g_main_loop_run(data.loop); | |
431 | } | |
432 | g_main_loop_unref(data.loop); | |
433 | if (data.error) { | |
434 | error_propagate(errp, data.error); | |
435 | object_unref(OBJECT(tioc)); | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | return QIO_CHANNEL(tioc); | |
440 | } | |
441 | ||
442 | ||
7423f417 | 443 | int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, |
f95910fe DB |
444 | QCryptoTLSCreds *tlscreds, const char *hostname, |
445 | QIOChannel **outioc, | |
798bfe00 FZ |
446 | off_t *size, Error **errp) |
447 | { | |
448 | char buf[256]; | |
449 | uint64_t magic, s; | |
798bfe00 | 450 | int rc; |
c203c59a | 451 | bool zeroes = true; |
798bfe00 | 452 | |
f95910fe DB |
453 | TRACE("Receiving negotiation tlscreds=%p hostname=%s.", |
454 | tlscreds, hostname ? hostname : "<null>"); | |
798bfe00 FZ |
455 | |
456 | rc = -EINVAL; | |
457 | ||
f95910fe DB |
458 | if (outioc) { |
459 | *outioc = NULL; | |
460 | } | |
461 | if (tlscreds && !outioc) { | |
462 | error_setg(errp, "Output I/O channel required for TLS"); | |
463 | goto fail; | |
464 | } | |
465 | ||
1c778ef7 | 466 | if (read_sync(ioc, buf, 8) != 8) { |
798bfe00 FZ |
467 | error_setg(errp, "Failed to read data"); |
468 | goto fail; | |
469 | } | |
470 | ||
471 | buf[8] = '\0'; | |
472 | if (strlen(buf) == 0) { | |
473 | error_setg(errp, "Server connection closed unexpectedly"); | |
474 | goto fail; | |
475 | } | |
476 | ||
477 | TRACE("Magic is %c%c%c%c%c%c%c%c", | |
478 | qemu_isprint(buf[0]) ? buf[0] : '.', | |
479 | qemu_isprint(buf[1]) ? buf[1] : '.', | |
480 | qemu_isprint(buf[2]) ? buf[2] : '.', | |
481 | qemu_isprint(buf[3]) ? buf[3] : '.', | |
482 | qemu_isprint(buf[4]) ? buf[4] : '.', | |
483 | qemu_isprint(buf[5]) ? buf[5] : '.', | |
484 | qemu_isprint(buf[6]) ? buf[6] : '.', | |
485 | qemu_isprint(buf[7]) ? buf[7] : '.'); | |
486 | ||
487 | if (memcmp(buf, "NBDMAGIC", 8) != 0) { | |
488 | error_setg(errp, "Invalid magic received"); | |
489 | goto fail; | |
490 | } | |
491 | ||
1c778ef7 | 492 | if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
798bfe00 FZ |
493 | error_setg(errp, "Failed to read magic"); |
494 | goto fail; | |
495 | } | |
496 | magic = be64_to_cpu(magic); | |
497 | TRACE("Magic is 0x%" PRIx64, magic); | |
498 | ||
f72d705f | 499 | if (magic == NBD_OPTS_MAGIC) { |
e2a9d9a3 | 500 | uint32_t clientflags = 0; |
e2a9d9a3 | 501 | uint16_t globalflags; |
9344e5f5 | 502 | bool fixedNewStyle = false; |
798bfe00 | 503 | |
e2a9d9a3 DB |
504 | if (read_sync(ioc, &globalflags, sizeof(globalflags)) != |
505 | sizeof(globalflags)) { | |
798bfe00 FZ |
506 | error_setg(errp, "Failed to read server flags"); |
507 | goto fail; | |
508 | } | |
9344e5f5 | 509 | globalflags = be16_to_cpu(globalflags); |
2cb34749 | 510 | TRACE("Global flags are %" PRIx32, globalflags); |
e2a9d9a3 | 511 | if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) { |
9344e5f5 | 512 | fixedNewStyle = true; |
e2a9d9a3 DB |
513 | TRACE("Server supports fixed new style"); |
514 | clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE; | |
515 | } | |
c203c59a EB |
516 | if (globalflags & NBD_FLAG_NO_ZEROES) { |
517 | zeroes = false; | |
518 | TRACE("Server supports no zeroes"); | |
519 | clientflags |= NBD_FLAG_C_NO_ZEROES; | |
520 | } | |
e2a9d9a3 | 521 | /* client requested flags */ |
9344e5f5 | 522 | clientflags = cpu_to_be32(clientflags); |
e2a9d9a3 DB |
523 | if (write_sync(ioc, &clientflags, sizeof(clientflags)) != |
524 | sizeof(clientflags)) { | |
525 | error_setg(errp, "Failed to send clientflags field"); | |
798bfe00 FZ |
526 | goto fail; |
527 | } | |
f95910fe DB |
528 | if (tlscreds) { |
529 | if (fixedNewStyle) { | |
530 | *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp); | |
531 | if (!*outioc) { | |
532 | goto fail; | |
533 | } | |
534 | ioc = *outioc; | |
535 | } else { | |
536 | error_setg(errp, "Server does not support STARTTLS"); | |
537 | goto fail; | |
538 | } | |
539 | } | |
f72d705f | 540 | if (!name) { |
69b49502 DB |
541 | TRACE("Using default NBD export name \"\""); |
542 | name = ""; | |
f72d705f | 543 | } |
9344e5f5 DB |
544 | if (fixedNewStyle) { |
545 | /* Check our desired export is present in the | |
546 | * server export list. Since NBD_OPT_EXPORT_NAME | |
547 | * cannot return an error message, running this | |
548 | * query gives us good error reporting if the | |
549 | * server required TLS | |
550 | */ | |
551 | if (nbd_receive_query_exports(ioc, name, errp) < 0) { | |
552 | goto fail; | |
553 | } | |
554 | } | |
c8a3a1b6 EB |
555 | /* write the export name request */ |
556 | if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name, | |
557 | errp) < 0) { | |
798bfe00 FZ |
558 | goto fail; |
559 | } | |
f72d705f | 560 | |
c8a3a1b6 | 561 | /* Read the response */ |
f72d705f DB |
562 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { |
563 | error_setg(errp, "Failed to read export length"); | |
798bfe00 FZ |
564 | goto fail; |
565 | } | |
f72d705f | 566 | *size = be64_to_cpu(s); |
798bfe00 | 567 | |
7423f417 | 568 | if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { |
f72d705f DB |
569 | error_setg(errp, "Failed to read export flags"); |
570 | goto fail; | |
571 | } | |
7423f417 | 572 | be16_to_cpus(flags); |
f72d705f | 573 | } else if (magic == NBD_CLIENT_MAGIC) { |
7423f417 EB |
574 | uint32_t oldflags; |
575 | ||
f72d705f DB |
576 | if (name) { |
577 | error_setg(errp, "Server does not support export names"); | |
578 | goto fail; | |
579 | } | |
f95910fe DB |
580 | if (tlscreds) { |
581 | error_setg(errp, "Server does not support STARTTLS"); | |
582 | goto fail; | |
583 | } | |
f72d705f DB |
584 | |
585 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
586 | error_setg(errp, "Failed to read export length"); | |
587 | goto fail; | |
588 | } | |
589 | *size = be64_to_cpu(s); | |
590 | TRACE("Size is %" PRIu64, *size); | |
798bfe00 | 591 | |
7423f417 | 592 | if (read_sync(ioc, &oldflags, sizeof(oldflags)) != sizeof(oldflags)) { |
798bfe00 FZ |
593 | error_setg(errp, "Failed to read export flags"); |
594 | goto fail; | |
595 | } | |
7423f417 EB |
596 | be32_to_cpus(&oldflags); |
597 | if (oldflags & ~0xffff) { | |
598 | error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags); | |
599 | goto fail; | |
600 | } | |
601 | *flags = oldflags; | |
798bfe00 | 602 | } else { |
f72d705f DB |
603 | error_setg(errp, "Bad magic received"); |
604 | goto fail; | |
798bfe00 | 605 | } |
f72d705f | 606 | |
7423f417 | 607 | TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags); |
c203c59a | 608 | if (zeroes && drop_sync(ioc, 124) != 124) { |
798bfe00 FZ |
609 | error_setg(errp, "Failed to read reserved block"); |
610 | goto fail; | |
611 | } | |
612 | rc = 0; | |
613 | ||
614 | fail: | |
615 | return rc; | |
616 | } | |
617 | ||
618 | #ifdef __linux__ | |
7423f417 | 619 | int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size) |
798bfe00 | 620 | { |
f57e2416 EB |
621 | unsigned long sectors = size / BDRV_SECTOR_SIZE; |
622 | if (size / BDRV_SECTOR_SIZE != sectors) { | |
623 | LOG("Export size %lld too large for 32-bit kernel", (long long) size); | |
624 | return -E2BIG; | |
625 | } | |
626 | ||
798bfe00 FZ |
627 | TRACE("Setting NBD socket"); |
628 | ||
f57e2416 | 629 | if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) { |
798bfe00 FZ |
630 | int serrno = errno; |
631 | LOG("Failed to set NBD socket"); | |
632 | return -serrno; | |
633 | } | |
634 | ||
635 | TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE); | |
636 | ||
f57e2416 | 637 | if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) { |
798bfe00 FZ |
638 | int serrno = errno; |
639 | LOG("Failed setting NBD block size"); | |
640 | return -serrno; | |
641 | } | |
642 | ||
f57e2416 EB |
643 | TRACE("Setting size to %lu block(s)", sectors); |
644 | if (size % BDRV_SECTOR_SIZE) { | |
645 | TRACE("Ignoring trailing %d bytes of export", | |
646 | (int) (size % BDRV_SECTOR_SIZE)); | |
647 | } | |
798bfe00 | 648 | |
f57e2416 | 649 | if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) { |
798bfe00 FZ |
650 | int serrno = errno; |
651 | LOG("Failed setting size (in blocks)"); | |
652 | return -serrno; | |
653 | } | |
654 | ||
f57e2416 | 655 | if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) { |
798bfe00 FZ |
656 | if (errno == ENOTTY) { |
657 | int read_only = (flags & NBD_FLAG_READ_ONLY) != 0; | |
658 | TRACE("Setting readonly attribute"); | |
659 | ||
660 | if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { | |
661 | int serrno = errno; | |
662 | LOG("Failed setting read-only attribute"); | |
663 | return -serrno; | |
664 | } | |
665 | } else { | |
666 | int serrno = errno; | |
667 | LOG("Failed setting flags"); | |
668 | return -serrno; | |
669 | } | |
670 | } | |
671 | ||
672 | TRACE("Negotiation ended"); | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
677 | int nbd_client(int fd) | |
678 | { | |
679 | int ret; | |
680 | int serrno; | |
681 | ||
682 | TRACE("Doing NBD loop"); | |
683 | ||
684 | ret = ioctl(fd, NBD_DO_IT); | |
685 | if (ret < 0 && errno == EPIPE) { | |
686 | /* NBD_DO_IT normally returns EPIPE when someone has disconnected | |
687 | * the socket via NBD_DISCONNECT. We do not want to return 1 in | |
688 | * that case. | |
689 | */ | |
690 | ret = 0; | |
691 | } | |
692 | serrno = errno; | |
693 | ||
694 | TRACE("NBD loop returned %d: %s", ret, strerror(serrno)); | |
695 | ||
696 | TRACE("Clearing NBD queue"); | |
697 | ioctl(fd, NBD_CLEAR_QUE); | |
698 | ||
699 | TRACE("Clearing NBD socket"); | |
700 | ioctl(fd, NBD_CLEAR_SOCK); | |
701 | ||
702 | errno = serrno; | |
703 | return ret; | |
704 | } | |
98494e3b EB |
705 | |
706 | int nbd_disconnect(int fd) | |
707 | { | |
708 | ioctl(fd, NBD_CLEAR_QUE); | |
709 | ioctl(fd, NBD_DISCONNECT); | |
710 | ioctl(fd, NBD_CLEAR_SOCK); | |
711 | return 0; | |
712 | } | |
713 | ||
798bfe00 | 714 | #else |
7423f417 | 715 | int nbd_init(int fd, QIOChannelSocket *ioc, uint16_t flags, off_t size) |
798bfe00 FZ |
716 | { |
717 | return -ENOTSUP; | |
718 | } | |
719 | ||
720 | int nbd_client(int fd) | |
721 | { | |
722 | return -ENOTSUP; | |
723 | } | |
98494e3b EB |
724 | int nbd_disconnect(int fd) |
725 | { | |
726 | return -ENOTSUP; | |
727 | } | |
798bfe00 FZ |
728 | #endif |
729 | ||
ed2dd912 | 730 | ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request) |
798bfe00 FZ |
731 | { |
732 | uint8_t buf[NBD_REQUEST_SIZE]; | |
733 | ssize_t ret; | |
734 | ||
7548fe31 | 735 | TRACE("Sending request to server: " |
2cb34749 | 736 | "{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64 |
b626b51a EB |
737 | ", .flags = %" PRIx16 ", .type = %" PRIu16 " }", |
738 | request->from, request->len, request->handle, | |
739 | request->flags, request->type); | |
7548fe31 | 740 | |
f6be6720 | 741 | stl_be_p(buf, NBD_REQUEST_MAGIC); |
b626b51a EB |
742 | stw_be_p(buf + 4, request->flags); |
743 | stw_be_p(buf + 6, request->type); | |
f6be6720 PM |
744 | stq_be_p(buf + 8, request->handle); |
745 | stq_be_p(buf + 16, request->from); | |
746 | stl_be_p(buf + 24, request->len); | |
798bfe00 | 747 | |
1c778ef7 | 748 | ret = write_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
749 | if (ret < 0) { |
750 | return ret; | |
751 | } | |
752 | ||
753 | if (ret != sizeof(buf)) { | |
754 | LOG("writing to socket failed"); | |
755 | return -EINVAL; | |
756 | } | |
757 | return 0; | |
758 | } | |
759 | ||
ed2dd912 | 760 | ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply) |
798bfe00 FZ |
761 | { |
762 | uint8_t buf[NBD_REPLY_SIZE]; | |
763 | uint32_t magic; | |
764 | ssize_t ret; | |
765 | ||
1c778ef7 | 766 | ret = read_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
767 | if (ret < 0) { |
768 | return ret; | |
769 | } | |
770 | ||
771 | if (ret != sizeof(buf)) { | |
772 | LOG("read failed"); | |
773 | return -EINVAL; | |
774 | } | |
775 | ||
776 | /* Reply | |
777 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
778 | [ 4 .. 7] error (0 == no error) | |
779 | [ 7 .. 15] handle | |
780 | */ | |
781 | ||
773dce3c PM |
782 | magic = ldl_be_p(buf); |
783 | reply->error = ldl_be_p(buf + 4); | |
784 | reply->handle = ldq_be_p(buf + 8); | |
798bfe00 FZ |
785 | |
786 | reply->error = nbd_errno_to_system_errno(reply->error); | |
787 | ||
2cb34749 EB |
788 | TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32 |
789 | ", handle = %" PRIu64" }", | |
798bfe00 FZ |
790 | magic, reply->error, reply->handle); |
791 | ||
792 | if (magic != NBD_REPLY_MAGIC) { | |
2cb34749 | 793 | LOG("invalid magic (got 0x%" PRIx32 ")", magic); |
798bfe00 FZ |
794 | return -EINVAL; |
795 | } | |
796 | return 0; | |
797 | } | |
798 |