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