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