]>
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: | |
9a76bd78 | 235 | error_setg(errp, "Requested export not available"); |
f95910fe DB |
236 | break; |
237 | ||
b6f5d3b5 | 238 | case NBD_REP_ERR_SHUTDOWN: |
3736cc5b EB |
239 | error_setg(errp, "Server shutting down before option %" PRIx32 " (%s)", |
240 | reply->option, nbd_opt_lookup(reply->option)); | |
241 | break; | |
242 | ||
243 | case NBD_REP_ERR_BLOCK_SIZE_REQD: | |
244 | error_setg(errp, "Server requires INFO_BLOCK_SIZE for option %" PRIx32 | |
245 | " (%s)", reply->option, nbd_opt_lookup(reply->option)); | |
b6f5d3b5 EB |
246 | break; |
247 | ||
9344e5f5 | 248 | default: |
3736cc5b EB |
249 | error_setg(errp, "Unknown error code when asking for option %" PRIx32 |
250 | " (%s)", reply->option, nbd_opt_lookup(reply->option)); | |
9344e5f5 DB |
251 | break; |
252 | } | |
253 | ||
6ff58164 | 254 | if (msg) { |
9a76bd78 | 255 | error_append_hint(errp, "server reported: %s\n", msg); |
6ff58164 AB |
256 | } |
257 | ||
258 | cleanup: | |
259 | g_free(msg); | |
2cdbf413 EB |
260 | if (result < 0) { |
261 | nbd_send_opt_abort(ioc); | |
262 | } | |
6ff58164 | 263 | return result; |
9344e5f5 DB |
264 | } |
265 | ||
75368aab EB |
266 | /* Process another portion of the NBD_OPT_LIST reply. Set *@match if |
267 | * the current reply matches @want or if the server does not support | |
268 | * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration | |
269 | * is complete, positive if more replies are expected, or negative | |
270 | * with @errp set if an unrecoverable error occurred. */ | |
271 | static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match, | |
272 | Error **errp) | |
9344e5f5 | 273 | { |
c8a3a1b6 | 274 | nbd_opt_reply reply; |
9344e5f5 DB |
275 | uint32_t len; |
276 | uint32_t namelen; | |
75368aab | 277 | char name[NBD_MAX_NAME_SIZE + 1]; |
6ff58164 | 278 | int error; |
9344e5f5 | 279 | |
c8a3a1b6 | 280 | if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) { |
9344e5f5 DB |
281 | return -1; |
282 | } | |
c8a3a1b6 | 283 | error = nbd_handle_reply_err(ioc, &reply, errp); |
6ff58164 | 284 | if (error <= 0) { |
75368aab EB |
285 | /* The server did not support NBD_OPT_LIST, so set *match on |
286 | * the assumption that any name will be accepted. */ | |
287 | *match = true; | |
6ff58164 | 288 | return error; |
9344e5f5 | 289 | } |
c8a3a1b6 | 290 | len = reply.length; |
9344e5f5 | 291 | |
c8a3a1b6 | 292 | if (reply.type == NBD_REP_ACK) { |
9344e5f5 DB |
293 | if (len != 0) { |
294 | error_setg(errp, "length too long for option end"); | |
2cdbf413 | 295 | nbd_send_opt_abort(ioc); |
9344e5f5 DB |
296 | return -1; |
297 | } | |
75368aab EB |
298 | return 0; |
299 | } else if (reply.type != NBD_REP_SERVER) { | |
300 | error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x", | |
301 | reply.type, NBD_REP_SERVER); | |
302 | nbd_send_opt_abort(ioc); | |
303 | return -1; | |
304 | } | |
9344e5f5 | 305 | |
75368aab EB |
306 | if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) { |
307 | error_setg(errp, "incorrect option length %" PRIu32, len); | |
308 | nbd_send_opt_abort(ioc); | |
309 | return -1; | |
310 | } | |
d1fdf257 | 311 | if (nbd_read(ioc, &namelen, sizeof(namelen), errp) < 0) { |
e44ed99d | 312 | error_prepend(errp, "failed to read option name length"); |
75368aab EB |
313 | nbd_send_opt_abort(ioc); |
314 | return -1; | |
315 | } | |
316 | namelen = be32_to_cpu(namelen); | |
317 | len -= sizeof(namelen); | |
318 | if (len < namelen) { | |
319 | error_setg(errp, "incorrect option name length"); | |
320 | nbd_send_opt_abort(ioc); | |
321 | return -1; | |
322 | } | |
323 | if (namelen != strlen(want)) { | |
d1fdf257 | 324 | if (nbd_drop(ioc, len, errp) < 0) { |
e44ed99d | 325 | error_prepend(errp, "failed to skip export name with wrong length"); |
7d3123e1 EB |
326 | nbd_send_opt_abort(ioc); |
327 | return -1; | |
200650d4 | 328 | } |
75368aab EB |
329 | return 1; |
330 | } | |
331 | ||
332 | assert(namelen < sizeof(name)); | |
d1fdf257 | 333 | if (nbd_read(ioc, name, namelen, errp) < 0) { |
e44ed99d | 334 | error_prepend(errp, "failed to read export name"); |
75368aab EB |
335 | nbd_send_opt_abort(ioc); |
336 | return -1; | |
337 | } | |
338 | name[namelen] = '\0'; | |
339 | len -= namelen; | |
d1fdf257 | 340 | if (nbd_drop(ioc, len, errp) < 0) { |
e44ed99d | 341 | error_prepend(errp, "failed to read export description"); |
2cdbf413 | 342 | nbd_send_opt_abort(ioc); |
9344e5f5 DB |
343 | return -1; |
344 | } | |
75368aab EB |
345 | if (!strcmp(name, want)) { |
346 | *match = true; | |
347 | } | |
9344e5f5 DB |
348 | return 1; |
349 | } | |
350 | ||
351 | ||
8ecaeae8 EB |
352 | /* Returns -1 if NBD_OPT_GO proves the export @wantname cannot be |
353 | * used, 0 if NBD_OPT_GO is unsupported (fall back to NBD_OPT_LIST and | |
354 | * NBD_OPT_EXPORT_NAME in that case), and > 0 if the export is good to | |
355 | * go (with @info populated). */ | |
356 | static int nbd_opt_go(QIOChannel *ioc, const char *wantname, | |
357 | NBDExportInfo *info, Error **errp) | |
358 | { | |
359 | nbd_opt_reply reply; | |
360 | uint32_t len = strlen(wantname); | |
361 | uint16_t type; | |
362 | int error; | |
363 | char *buf; | |
364 | ||
365 | /* The protocol requires that the server send NBD_INFO_EXPORT with | |
366 | * a non-zero flags (at least NBD_FLAG_HAS_FLAGS must be set); so | |
367 | * flags still 0 is a witness of a broken server. */ | |
368 | info->flags = 0; | |
369 | ||
370 | trace_nbd_opt_go_start(wantname); | |
081dd1fe | 371 | buf = g_malloc(4 + len + 2 + 2 * info->request_sizes + 1); |
8ecaeae8 EB |
372 | stl_be_p(buf, len); |
373 | memcpy(buf + 4, wantname, len); | |
081dd1fe EB |
374 | /* At most one request, everything else up to server */ |
375 | stw_be_p(buf + 4 + len, info->request_sizes); | |
376 | if (info->request_sizes) { | |
377 | stw_be_p(buf + 4 + len + 2, NBD_INFO_BLOCK_SIZE); | |
378 | } | |
158b9aa5 PMD |
379 | error = nbd_send_option_request(ioc, NBD_OPT_GO, |
380 | 4 + len + 2 + 2 * info->request_sizes, | |
381 | buf, errp); | |
382 | g_free(buf); | |
383 | if (error < 0) { | |
8ecaeae8 EB |
384 | return -1; |
385 | } | |
386 | ||
387 | while (1) { | |
388 | if (nbd_receive_option_reply(ioc, NBD_OPT_GO, &reply, errp) < 0) { | |
389 | return -1; | |
390 | } | |
391 | error = nbd_handle_reply_err(ioc, &reply, errp); | |
392 | if (error <= 0) { | |
393 | return error; | |
394 | } | |
395 | len = reply.length; | |
396 | ||
397 | if (reply.type == NBD_REP_ACK) { | |
398 | /* Server is done sending info and moved into transmission | |
399 | phase, but make sure it sent flags */ | |
400 | if (len) { | |
401 | error_setg(errp, "server sent invalid NBD_REP_ACK"); | |
8ecaeae8 EB |
402 | return -1; |
403 | } | |
404 | if (!info->flags) { | |
405 | error_setg(errp, "broken server omitted NBD_INFO_EXPORT"); | |
8ecaeae8 EB |
406 | return -1; |
407 | } | |
408 | trace_nbd_opt_go_success(); | |
409 | return 1; | |
410 | } | |
411 | if (reply.type != NBD_REP_INFO) { | |
081dd1fe EB |
412 | error_setg(errp, "unexpected reply type %" PRIx32 |
413 | " (%s), expected %x", | |
414 | reply.type, nbd_rep_lookup(reply.type), NBD_REP_INFO); | |
8ecaeae8 EB |
415 | nbd_send_opt_abort(ioc); |
416 | return -1; | |
417 | } | |
418 | if (len < sizeof(type)) { | |
419 | error_setg(errp, "NBD_REP_INFO length %" PRIu32 " is too short", | |
420 | len); | |
421 | nbd_send_opt_abort(ioc); | |
422 | return -1; | |
423 | } | |
424 | if (nbd_read(ioc, &type, sizeof(type), errp) < 0) { | |
425 | error_prepend(errp, "failed to read info type"); | |
426 | nbd_send_opt_abort(ioc); | |
427 | return -1; | |
428 | } | |
429 | len -= sizeof(type); | |
430 | be16_to_cpus(&type); | |
431 | switch (type) { | |
432 | case NBD_INFO_EXPORT: | |
433 | if (len != sizeof(info->size) + sizeof(info->flags)) { | |
434 | error_setg(errp, "remaining export info len %" PRIu32 | |
435 | " is unexpected size", len); | |
436 | nbd_send_opt_abort(ioc); | |
437 | return -1; | |
438 | } | |
439 | if (nbd_read(ioc, &info->size, sizeof(info->size), errp) < 0) { | |
440 | error_prepend(errp, "failed to read info size"); | |
441 | nbd_send_opt_abort(ioc); | |
442 | return -1; | |
443 | } | |
444 | be64_to_cpus(&info->size); | |
445 | if (nbd_read(ioc, &info->flags, sizeof(info->flags), errp) < 0) { | |
446 | error_prepend(errp, "failed to read info flags"); | |
447 | nbd_send_opt_abort(ioc); | |
448 | return -1; | |
449 | } | |
450 | be16_to_cpus(&info->flags); | |
451 | trace_nbd_receive_negotiate_size_flags(info->size, info->flags); | |
452 | break; | |
453 | ||
081dd1fe EB |
454 | case NBD_INFO_BLOCK_SIZE: |
455 | if (len != sizeof(info->min_block) * 3) { | |
456 | error_setg(errp, "remaining export info len %" PRIu32 | |
457 | " is unexpected size", len); | |
458 | nbd_send_opt_abort(ioc); | |
459 | return -1; | |
460 | } | |
461 | if (nbd_read(ioc, &info->min_block, sizeof(info->min_block), | |
462 | errp) < 0) { | |
463 | error_prepend(errp, "failed to read info minimum block size"); | |
464 | nbd_send_opt_abort(ioc); | |
465 | return -1; | |
466 | } | |
467 | be32_to_cpus(&info->min_block); | |
468 | if (!is_power_of_2(info->min_block)) { | |
469 | error_setg(errp, "server minimum block size %" PRId32 | |
470 | "is not a power of two", info->min_block); | |
471 | nbd_send_opt_abort(ioc); | |
472 | return -1; | |
473 | } | |
474 | if (nbd_read(ioc, &info->opt_block, sizeof(info->opt_block), | |
475 | errp) < 0) { | |
476 | error_prepend(errp, "failed to read info preferred block size"); | |
477 | nbd_send_opt_abort(ioc); | |
478 | return -1; | |
479 | } | |
480 | be32_to_cpus(&info->opt_block); | |
481 | if (!is_power_of_2(info->opt_block) || | |
482 | info->opt_block < info->min_block) { | |
483 | error_setg(errp, "server preferred block size %" PRId32 | |
484 | "is not valid", info->opt_block); | |
485 | nbd_send_opt_abort(ioc); | |
486 | return -1; | |
487 | } | |
488 | if (nbd_read(ioc, &info->max_block, sizeof(info->max_block), | |
489 | errp) < 0) { | |
490 | error_prepend(errp, "failed to read info maximum block size"); | |
491 | nbd_send_opt_abort(ioc); | |
492 | return -1; | |
493 | } | |
494 | be32_to_cpus(&info->max_block); | |
495 | trace_nbd_opt_go_info_block_size(info->min_block, info->opt_block, | |
496 | info->max_block); | |
497 | break; | |
498 | ||
8ecaeae8 EB |
499 | default: |
500 | trace_nbd_opt_go_info_unknown(type, nbd_info_lookup(type)); | |
501 | if (nbd_drop(ioc, len, errp) < 0) { | |
502 | error_prepend(errp, "Failed to read info payload"); | |
503 | nbd_send_opt_abort(ioc); | |
504 | return -1; | |
505 | } | |
506 | break; | |
507 | } | |
508 | } | |
509 | } | |
510 | ||
75368aab | 511 | /* Return -1 on failure, 0 if wantname is an available export. */ |
9344e5f5 DB |
512 | static int nbd_receive_query_exports(QIOChannel *ioc, |
513 | const char *wantname, | |
514 | Error **errp) | |
515 | { | |
9344e5f5 DB |
516 | bool foundExport = false; |
517 | ||
9588463e | 518 | trace_nbd_receive_query_exports_start(wantname); |
c8a3a1b6 | 519 | if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) { |
9344e5f5 DB |
520 | return -1; |
521 | } | |
522 | ||
9344e5f5 | 523 | while (1) { |
75368aab | 524 | int ret = nbd_receive_list(ioc, wantname, &foundExport, errp); |
9344e5f5 DB |
525 | |
526 | if (ret < 0) { | |
75368aab | 527 | /* Server gave unexpected reply */ |
9344e5f5 | 528 | return -1; |
75368aab EB |
529 | } else if (ret == 0) { |
530 | /* Done iterating. */ | |
531 | if (!foundExport) { | |
532 | error_setg(errp, "No export with name '%s' available", | |
533 | wantname); | |
534 | nbd_send_opt_abort(ioc); | |
535 | return -1; | |
536 | } | |
9588463e | 537 | trace_nbd_receive_query_exports_success(wantname); |
75368aab | 538 | return 0; |
9344e5f5 | 539 | } |
9344e5f5 | 540 | } |
9344e5f5 DB |
541 | } |
542 | ||
f95910fe DB |
543 | static QIOChannel *nbd_receive_starttls(QIOChannel *ioc, |
544 | QCryptoTLSCreds *tlscreds, | |
545 | const char *hostname, Error **errp) | |
546 | { | |
c8a3a1b6 | 547 | nbd_opt_reply reply; |
f95910fe DB |
548 | QIOChannelTLS *tioc; |
549 | struct NBDTLSHandshakeData data = { 0 }; | |
550 | ||
9588463e | 551 | trace_nbd_receive_starttls_request(); |
c8a3a1b6 | 552 | if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) { |
f95910fe DB |
553 | return NULL; |
554 | } | |
555 | ||
9588463e | 556 | trace_nbd_receive_starttls_reply(); |
c8a3a1b6 | 557 | if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) { |
f95910fe DB |
558 | return NULL; |
559 | } | |
c8a3a1b6 EB |
560 | |
561 | if (reply.type != NBD_REP_ACK) { | |
2cb34749 | 562 | error_setg(errp, "Server rejected request to start TLS %" PRIx32, |
c8a3a1b6 | 563 | reply.type); |
2cdbf413 | 564 | nbd_send_opt_abort(ioc); |
f95910fe DB |
565 | return NULL; |
566 | } | |
567 | ||
c8a3a1b6 | 568 | if (reply.length != 0) { |
2cb34749 | 569 | error_setg(errp, "Start TLS response was not zero %" PRIu32, |
c8a3a1b6 | 570 | reply.length); |
2cdbf413 | 571 | nbd_send_opt_abort(ioc); |
f95910fe DB |
572 | return NULL; |
573 | } | |
574 | ||
9588463e | 575 | trace_nbd_receive_starttls_new_client(); |
f95910fe DB |
576 | tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp); |
577 | if (!tioc) { | |
578 | return NULL; | |
579 | } | |
0d73f725 | 580 | qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls"); |
f95910fe | 581 | data.loop = g_main_loop_new(g_main_context_default(), FALSE); |
9588463e | 582 | trace_nbd_receive_starttls_tls_handshake(); |
f95910fe DB |
583 | qio_channel_tls_handshake(tioc, |
584 | nbd_tls_handshake, | |
585 | &data, | |
586 | NULL); | |
587 | ||
588 | if (!data.complete) { | |
589 | g_main_loop_run(data.loop); | |
590 | } | |
591 | g_main_loop_unref(data.loop); | |
592 | if (data.error) { | |
593 | error_propagate(errp, data.error); | |
594 | object_unref(OBJECT(tioc)); | |
595 | return NULL; | |
596 | } | |
597 | ||
598 | return QIO_CHANNEL(tioc); | |
599 | } | |
600 | ||
601 | ||
004a89fc | 602 | int nbd_receive_negotiate(QIOChannel *ioc, const char *name, |
f95910fe | 603 | QCryptoTLSCreds *tlscreds, const char *hostname, |
004a89fc EB |
604 | QIOChannel **outioc, NBDExportInfo *info, |
605 | Error **errp) | |
798bfe00 FZ |
606 | { |
607 | char buf[256]; | |
004a89fc | 608 | uint64_t magic; |
798bfe00 | 609 | int rc; |
c203c59a | 610 | bool zeroes = true; |
798bfe00 | 611 | |
9588463e | 612 | trace_nbd_receive_negotiate(tlscreds, hostname ? hostname : "<null>"); |
798bfe00 FZ |
613 | |
614 | rc = -EINVAL; | |
615 | ||
f95910fe DB |
616 | if (outioc) { |
617 | *outioc = NULL; | |
618 | } | |
619 | if (tlscreds && !outioc) { | |
620 | error_setg(errp, "Output I/O channel required for TLS"); | |
621 | goto fail; | |
622 | } | |
623 | ||
d1fdf257 | 624 | if (nbd_read(ioc, buf, 8, errp) < 0) { |
e44ed99d | 625 | error_prepend(errp, "Failed to read data"); |
798bfe00 FZ |
626 | goto fail; |
627 | } | |
628 | ||
629 | buf[8] = '\0'; | |
630 | if (strlen(buf) == 0) { | |
631 | error_setg(errp, "Server connection closed unexpectedly"); | |
632 | goto fail; | |
633 | } | |
634 | ||
458d7a69 | 635 | magic = ldq_be_p(buf); |
9588463e | 636 | trace_nbd_receive_negotiate_magic(magic); |
798bfe00 FZ |
637 | |
638 | if (memcmp(buf, "NBDMAGIC", 8) != 0) { | |
639 | error_setg(errp, "Invalid magic received"); | |
640 | goto fail; | |
641 | } | |
642 | ||
d1fdf257 | 643 | if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) { |
e44ed99d | 644 | error_prepend(errp, "Failed to read magic"); |
798bfe00 FZ |
645 | goto fail; |
646 | } | |
647 | magic = be64_to_cpu(magic); | |
9588463e | 648 | trace_nbd_receive_negotiate_magic(magic); |
798bfe00 | 649 | |
f72d705f | 650 | if (magic == NBD_OPTS_MAGIC) { |
e2a9d9a3 | 651 | uint32_t clientflags = 0; |
e2a9d9a3 | 652 | uint16_t globalflags; |
9344e5f5 | 653 | bool fixedNewStyle = false; |
798bfe00 | 654 | |
d1fdf257 | 655 | if (nbd_read(ioc, &globalflags, sizeof(globalflags), errp) < 0) { |
e44ed99d | 656 | error_prepend(errp, "Failed to read server flags"); |
798bfe00 FZ |
657 | goto fail; |
658 | } | |
9344e5f5 | 659 | globalflags = be16_to_cpu(globalflags); |
9588463e | 660 | trace_nbd_receive_negotiate_server_flags(globalflags); |
e2a9d9a3 | 661 | if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) { |
9344e5f5 | 662 | fixedNewStyle = true; |
e2a9d9a3 DB |
663 | clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE; |
664 | } | |
c203c59a EB |
665 | if (globalflags & NBD_FLAG_NO_ZEROES) { |
666 | zeroes = false; | |
c203c59a EB |
667 | clientflags |= NBD_FLAG_C_NO_ZEROES; |
668 | } | |
e2a9d9a3 | 669 | /* client requested flags */ |
9344e5f5 | 670 | clientflags = cpu_to_be32(clientflags); |
d1fdf257 | 671 | if (nbd_write(ioc, &clientflags, sizeof(clientflags), errp) < 0) { |
e44ed99d | 672 | error_prepend(errp, "Failed to send clientflags field"); |
798bfe00 FZ |
673 | goto fail; |
674 | } | |
f95910fe DB |
675 | if (tlscreds) { |
676 | if (fixedNewStyle) { | |
677 | *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp); | |
678 | if (!*outioc) { | |
679 | goto fail; | |
680 | } | |
681 | ioc = *outioc; | |
682 | } else { | |
683 | error_setg(errp, "Server does not support STARTTLS"); | |
684 | goto fail; | |
685 | } | |
686 | } | |
f72d705f | 687 | if (!name) { |
9588463e | 688 | trace_nbd_receive_negotiate_default_name(); |
69b49502 | 689 | name = ""; |
f72d705f | 690 | } |
9344e5f5 | 691 | if (fixedNewStyle) { |
8ecaeae8 EB |
692 | int result; |
693 | ||
694 | /* Try NBD_OPT_GO first - if it works, we are done (it | |
695 | * also gives us a good message if the server requires | |
696 | * TLS). If it is not available, fall back to | |
697 | * NBD_OPT_LIST for nicer error messages about a missing | |
698 | * export, then use NBD_OPT_EXPORT_NAME. */ | |
699 | result = nbd_opt_go(ioc, name, info, errp); | |
700 | if (result < 0) { | |
701 | goto fail; | |
702 | } | |
703 | if (result > 0) { | |
704 | return 0; | |
705 | } | |
9344e5f5 DB |
706 | /* Check our desired export is present in the |
707 | * server export list. Since NBD_OPT_EXPORT_NAME | |
708 | * cannot return an error message, running this | |
8ecaeae8 EB |
709 | * query gives us better error reporting if the |
710 | * export name is not available. | |
9344e5f5 DB |
711 | */ |
712 | if (nbd_receive_query_exports(ioc, name, errp) < 0) { | |
713 | goto fail; | |
714 | } | |
715 | } | |
c8a3a1b6 EB |
716 | /* write the export name request */ |
717 | if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name, | |
718 | errp) < 0) { | |
798bfe00 FZ |
719 | goto fail; |
720 | } | |
f72d705f | 721 | |
c8a3a1b6 | 722 | /* Read the response */ |
004a89fc | 723 | if (nbd_read(ioc, &info->size, sizeof(info->size), errp) < 0) { |
e44ed99d | 724 | error_prepend(errp, "Failed to read export length"); |
798bfe00 FZ |
725 | goto fail; |
726 | } | |
004a89fc | 727 | be64_to_cpus(&info->size); |
798bfe00 | 728 | |
004a89fc | 729 | if (nbd_read(ioc, &info->flags, sizeof(info->flags), errp) < 0) { |
e44ed99d | 730 | error_prepend(errp, "Failed to read export flags"); |
f72d705f DB |
731 | goto fail; |
732 | } | |
004a89fc | 733 | be16_to_cpus(&info->flags); |
f72d705f | 734 | } else if (magic == NBD_CLIENT_MAGIC) { |
7423f417 EB |
735 | uint32_t oldflags; |
736 | ||
f72d705f DB |
737 | if (name) { |
738 | error_setg(errp, "Server does not support export names"); | |
739 | goto fail; | |
740 | } | |
f95910fe DB |
741 | if (tlscreds) { |
742 | error_setg(errp, "Server does not support STARTTLS"); | |
743 | goto fail; | |
744 | } | |
f72d705f | 745 | |
004a89fc | 746 | if (nbd_read(ioc, &info->size, sizeof(info->size), errp) < 0) { |
e44ed99d | 747 | error_prepend(errp, "Failed to read export length"); |
f72d705f DB |
748 | goto fail; |
749 | } | |
004a89fc | 750 | be64_to_cpus(&info->size); |
798bfe00 | 751 | |
d1fdf257 | 752 | if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) { |
e44ed99d | 753 | error_prepend(errp, "Failed to read export flags"); |
798bfe00 FZ |
754 | goto fail; |
755 | } | |
7423f417 EB |
756 | be32_to_cpus(&oldflags); |
757 | if (oldflags & ~0xffff) { | |
758 | error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags); | |
759 | goto fail; | |
760 | } | |
004a89fc | 761 | info->flags = oldflags; |
798bfe00 | 762 | } else { |
f72d705f DB |
763 | error_setg(errp, "Bad magic received"); |
764 | goto fail; | |
798bfe00 | 765 | } |
f72d705f | 766 | |
004a89fc | 767 | trace_nbd_receive_negotiate_size_flags(info->size, info->flags); |
d1fdf257 | 768 | if (zeroes && nbd_drop(ioc, 124, errp) < 0) { |
e44ed99d | 769 | error_prepend(errp, "Failed to read reserved block"); |
798bfe00 FZ |
770 | goto fail; |
771 | } | |
772 | rc = 0; | |
773 | ||
774 | fail: | |
775 | return rc; | |
776 | } | |
777 | ||
778 | #ifdef __linux__ | |
004a89fc | 779 | int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info, |
be41c100 | 780 | Error **errp) |
798bfe00 | 781 | { |
081dd1fe EB |
782 | unsigned long sector_size = MAX(BDRV_SECTOR_SIZE, info->min_block); |
783 | unsigned long sectors = info->size / sector_size; | |
784 | ||
785 | /* FIXME: Once the kernel module is patched to honor block sizes, | |
786 | * and to advertise that fact to user space, we should update the | |
787 | * hand-off to the kernel to use any block sizes we learned. */ | |
788 | assert(!info->request_sizes); | |
789 | if (info->size / sector_size != sectors) { | |
004a89fc EB |
790 | error_setg(errp, "Export size %" PRIu64 " too large for 32-bit kernel", |
791 | info->size); | |
f57e2416 EB |
792 | return -E2BIG; |
793 | } | |
794 | ||
9588463e | 795 | trace_nbd_init_set_socket(); |
798bfe00 | 796 | |
f57e2416 | 797 | if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) { |
798bfe00 | 798 | int serrno = errno; |
be41c100 | 799 | error_setg(errp, "Failed to set NBD socket"); |
798bfe00 FZ |
800 | return -serrno; |
801 | } | |
802 | ||
081dd1fe | 803 | trace_nbd_init_set_block_size(sector_size); |
798bfe00 | 804 | |
081dd1fe | 805 | if (ioctl(fd, NBD_SET_BLKSIZE, sector_size) < 0) { |
798bfe00 | 806 | int serrno = errno; |
be41c100 | 807 | error_setg(errp, "Failed setting NBD block size"); |
798bfe00 FZ |
808 | return -serrno; |
809 | } | |
810 | ||
9588463e | 811 | trace_nbd_init_set_size(sectors); |
081dd1fe EB |
812 | if (info->size % sector_size) { |
813 | trace_nbd_init_trailing_bytes(info->size % sector_size); | |
f57e2416 | 814 | } |
798bfe00 | 815 | |
f57e2416 | 816 | if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) { |
798bfe00 | 817 | int serrno = errno; |
be41c100 | 818 | error_setg(errp, "Failed setting size (in blocks)"); |
798bfe00 FZ |
819 | return -serrno; |
820 | } | |
821 | ||
004a89fc | 822 | if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) info->flags) < 0) { |
798bfe00 | 823 | if (errno == ENOTTY) { |
004a89fc | 824 | int read_only = (info->flags & NBD_FLAG_READ_ONLY) != 0; |
9588463e | 825 | trace_nbd_init_set_readonly(); |
798bfe00 FZ |
826 | |
827 | if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { | |
828 | int serrno = errno; | |
be41c100 | 829 | error_setg(errp, "Failed setting read-only attribute"); |
798bfe00 FZ |
830 | return -serrno; |
831 | } | |
832 | } else { | |
833 | int serrno = errno; | |
be41c100 | 834 | error_setg(errp, "Failed setting flags"); |
798bfe00 FZ |
835 | return -serrno; |
836 | } | |
837 | } | |
838 | ||
9588463e | 839 | trace_nbd_init_finish(); |
798bfe00 FZ |
840 | |
841 | return 0; | |
842 | } | |
843 | ||
844 | int nbd_client(int fd) | |
845 | { | |
846 | int ret; | |
847 | int serrno; | |
848 | ||
9588463e | 849 | trace_nbd_client_loop(); |
798bfe00 FZ |
850 | |
851 | ret = ioctl(fd, NBD_DO_IT); | |
852 | if (ret < 0 && errno == EPIPE) { | |
853 | /* NBD_DO_IT normally returns EPIPE when someone has disconnected | |
854 | * the socket via NBD_DISCONNECT. We do not want to return 1 in | |
855 | * that case. | |
856 | */ | |
857 | ret = 0; | |
858 | } | |
859 | serrno = errno; | |
860 | ||
9588463e | 861 | trace_nbd_client_loop_ret(ret, strerror(serrno)); |
798bfe00 | 862 | |
9588463e | 863 | trace_nbd_client_clear_queue(); |
798bfe00 FZ |
864 | ioctl(fd, NBD_CLEAR_QUE); |
865 | ||
9588463e | 866 | trace_nbd_client_clear_socket(); |
798bfe00 FZ |
867 | ioctl(fd, NBD_CLEAR_SOCK); |
868 | ||
869 | errno = serrno; | |
870 | return ret; | |
871 | } | |
98494e3b EB |
872 | |
873 | int nbd_disconnect(int fd) | |
874 | { | |
875 | ioctl(fd, NBD_CLEAR_QUE); | |
876 | ioctl(fd, NBD_DISCONNECT); | |
877 | ioctl(fd, NBD_CLEAR_SOCK); | |
878 | return 0; | |
879 | } | |
880 | ||
798bfe00 | 881 | #else |
004a89fc | 882 | int nbd_init(int fd, QIOChannelSocket *ioc, NBDExportInfo *info, |
be41c100 | 883 | Error **errp) |
798bfe00 | 884 | { |
be41c100 | 885 | error_setg(errp, "nbd_init is only supported on Linux"); |
798bfe00 FZ |
886 | return -ENOTSUP; |
887 | } | |
888 | ||
889 | int nbd_client(int fd) | |
890 | { | |
891 | return -ENOTSUP; | |
892 | } | |
98494e3b EB |
893 | int nbd_disconnect(int fd) |
894 | { | |
895 | return -ENOTSUP; | |
896 | } | |
798bfe00 FZ |
897 | #endif |
898 | ||
ed2dd912 | 899 | ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request) |
798bfe00 FZ |
900 | { |
901 | uint8_t buf[NBD_REQUEST_SIZE]; | |
798bfe00 | 902 | |
9588463e | 903 | trace_nbd_send_request(request->from, request->len, request->handle, |
48000eb3 EB |
904 | request->flags, request->type, |
905 | nbd_cmd_lookup(request->type)); | |
7548fe31 | 906 | |
f6be6720 | 907 | stl_be_p(buf, NBD_REQUEST_MAGIC); |
b626b51a EB |
908 | stw_be_p(buf + 4, request->flags); |
909 | stw_be_p(buf + 6, request->type); | |
f6be6720 PM |
910 | stq_be_p(buf + 8, request->handle); |
911 | stq_be_p(buf + 16, request->from); | |
912 | stl_be_p(buf + 24, request->len); | |
798bfe00 | 913 | |
d1fdf257 | 914 | return nbd_write(ioc, buf, sizeof(buf), NULL); |
798bfe00 FZ |
915 | } |
916 | ||
be41c100 | 917 | ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp) |
798bfe00 FZ |
918 | { |
919 | uint8_t buf[NBD_REPLY_SIZE]; | |
920 | uint32_t magic; | |
921 | ssize_t ret; | |
922 | ||
d1fdf257 | 923 | ret = nbd_read_eof(ioc, buf, sizeof(buf), errp); |
ff82911c | 924 | if (ret <= 0) { |
798bfe00 FZ |
925 | return ret; |
926 | } | |
927 | ||
798bfe00 FZ |
928 | /* Reply |
929 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
930 | [ 4 .. 7] error (0 == no error) | |
931 | [ 7 .. 15] handle | |
932 | */ | |
933 | ||
773dce3c PM |
934 | magic = ldl_be_p(buf); |
935 | reply->error = ldl_be_p(buf + 4); | |
936 | reply->handle = ldq_be_p(buf + 8); | |
798bfe00 FZ |
937 | |
938 | reply->error = nbd_errno_to_system_errno(reply->error); | |
939 | ||
b6f5d3b5 EB |
940 | if (reply->error == ESHUTDOWN) { |
941 | /* This works even on mingw which lacks a native ESHUTDOWN */ | |
be41c100 | 942 | error_setg(errp, "server shutting down"); |
b6f5d3b5 EB |
943 | return -EINVAL; |
944 | } | |
9588463e | 945 | trace_nbd_receive_reply(magic, reply->error, reply->handle); |
798bfe00 FZ |
946 | |
947 | if (magic != NBD_REPLY_MAGIC) { | |
be41c100 | 948 | error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic); |
798bfe00 FZ |
949 | return -EINVAL; |
950 | } | |
a12a712a | 951 | return sizeof(buf); |
798bfe00 FZ |
952 | } |
953 |