]> git.proxmox.com Git - qemu.git/blob - nbd.c
Merge remote branch 'agraf/ppc-next' into staging
[qemu.git] / nbd.c
1 /*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
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
19 #include "nbd.h"
20
21 #include <errno.h>
22 #include <string.h>
23 #ifndef _WIN32
24 #include <sys/ioctl.h>
25 #endif
26 #ifdef __sun__
27 #include <sys/ioccom.h>
28 #endif
29 #include <ctype.h>
30 #include <inttypes.h>
31
32 #include "qemu_socket.h"
33
34 //#define DEBUG_NBD
35
36 #ifdef DEBUG_NBD
37 #define TRACE(msg, ...) do { \
38 LOG(msg, ## __VA_ARGS__); \
39 } while(0)
40 #else
41 #define TRACE(msg, ...) \
42 do { } while (0)
43 #endif
44
45 #define LOG(msg, ...) do { \
46 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
47 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
48 } while(0)
49
50 /* This is all part of the "official" NBD API */
51
52 #define NBD_REQUEST_MAGIC 0x25609513
53 #define NBD_REPLY_MAGIC 0x67446698
54
55 #define NBD_SET_SOCK _IO(0xab, 0)
56 #define NBD_SET_BLKSIZE _IO(0xab, 1)
57 #define NBD_SET_SIZE _IO(0xab, 2)
58 #define NBD_DO_IT _IO(0xab, 3)
59 #define NBD_CLEAR_SOCK _IO(0xab, 4)
60 #define NBD_CLEAR_QUE _IO(0xab, 5)
61 #define NBD_PRINT_DEBUG _IO(0xab, 6)
62 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
63 #define NBD_DISCONNECT _IO(0xab, 8)
64
65 #define NBD_OPT_EXPORT_NAME (1 << 0)
66
67 /* That's all folks */
68
69 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
70 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
71
72 size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
73 {
74 size_t offset = 0;
75
76 while (offset < size) {
77 ssize_t len;
78
79 if (do_read) {
80 len = recv(fd, buffer + offset, size - offset, 0);
81 } else {
82 len = send(fd, buffer + offset, size - offset, 0);
83 }
84
85 if (len == -1)
86 errno = socket_error();
87
88 /* recoverable error */
89 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
90 continue;
91 }
92
93 /* eof */
94 if (len == 0) {
95 break;
96 }
97
98 /* unrecoverable error */
99 if (len == -1) {
100 return 0;
101 }
102
103 offset += len;
104 }
105
106 return offset;
107 }
108
109 int tcp_socket_outgoing(const char *address, uint16_t port)
110 {
111 int s;
112 struct in_addr in;
113 struct sockaddr_in addr;
114
115 s = socket(PF_INET, SOCK_STREAM, 0);
116 if (s == -1) {
117 return -1;
118 }
119
120 if (inet_aton(address, &in) == 0) {
121 struct hostent *ent;
122
123 ent = gethostbyname(address);
124 if (ent == NULL) {
125 goto error;
126 }
127
128 memcpy(&in, ent->h_addr, sizeof(in));
129 }
130
131 addr.sin_family = AF_INET;
132 addr.sin_port = htons(port);
133 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
134
135 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
136 goto error;
137 }
138
139 return s;
140 error:
141 closesocket(s);
142 return -1;
143 }
144
145 int tcp_socket_incoming(const char *address, uint16_t port)
146 {
147 int s;
148 struct in_addr in;
149 struct sockaddr_in addr;
150 int opt;
151
152 s = socket(PF_INET, SOCK_STREAM, 0);
153 if (s == -1) {
154 return -1;
155 }
156
157 if (inet_aton(address, &in) == 0) {
158 struct hostent *ent;
159
160 ent = gethostbyname(address);
161 if (ent == NULL) {
162 goto error;
163 }
164
165 memcpy(&in, ent->h_addr, sizeof(in));
166 }
167
168 addr.sin_family = AF_INET;
169 addr.sin_port = htons(port);
170 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
171
172 opt = 1;
173 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
174 (const void *) &opt, sizeof(opt)) == -1) {
175 goto error;
176 }
177
178 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
179 goto error;
180 }
181
182 if (listen(s, 128) == -1) {
183 goto error;
184 }
185
186 return s;
187 error:
188 closesocket(s);
189 return -1;
190 }
191
192 #ifndef _WIN32
193 int unix_socket_incoming(const char *path)
194 {
195 int s;
196 struct sockaddr_un addr;
197
198 s = socket(PF_UNIX, SOCK_STREAM, 0);
199 if (s == -1) {
200 return -1;
201 }
202
203 memset(&addr, 0, sizeof(addr));
204 addr.sun_family = AF_UNIX;
205 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
206
207 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
208 goto error;
209 }
210
211 if (listen(s, 128) == -1) {
212 goto error;
213 }
214
215 return s;
216 error:
217 closesocket(s);
218 return -1;
219 }
220
221 int unix_socket_outgoing(const char *path)
222 {
223 int s;
224 struct sockaddr_un addr;
225
226 s = socket(PF_UNIX, SOCK_STREAM, 0);
227 if (s == -1) {
228 return -1;
229 }
230
231 memset(&addr, 0, sizeof(addr));
232 addr.sun_family = AF_UNIX;
233 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
234
235 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
236 goto error;
237 }
238
239 return s;
240 error:
241 closesocket(s);
242 return -1;
243 }
244 #else
245 int unix_socket_incoming(const char *path)
246 {
247 errno = ENOTSUP;
248 return -1;
249 }
250
251 int unix_socket_outgoing(const char *path)
252 {
253 errno = ENOTSUP;
254 return -1;
255 }
256 #endif
257
258
259 /* Basic flow
260
261 Server Client
262
263 Negotiate
264 Request
265 Response
266 Request
267 Response
268 ...
269 ...
270 Request (type == 2)
271 */
272
273 int nbd_negotiate(int csock, off_t size)
274 {
275 char buf[8 + 8 + 8 + 128];
276
277 /* Negotiate
278 [ 0 .. 7] passwd ("NBDMAGIC")
279 [ 8 .. 15] magic (0x00420281861253)
280 [16 .. 23] size
281 [24 .. 151] reserved (0)
282 */
283
284 TRACE("Beginning negotiation.");
285 memcpy(buf, "NBDMAGIC", 8);
286 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
287 cpu_to_be64w((uint64_t*)(buf + 16), size);
288 memset(buf + 24, 0, 128);
289
290 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
291 LOG("write failed");
292 errno = EINVAL;
293 return -1;
294 }
295
296 TRACE("Negotation succeeded.");
297
298 return 0;
299 }
300
301 int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
302 off_t *size, size_t *blocksize)
303 {
304 char buf[256];
305 uint64_t magic, s;
306 uint16_t tmp;
307
308 TRACE("Receiving negotation.");
309
310 if (read_sync(csock, buf, 8) != 8) {
311 LOG("read failed");
312 errno = EINVAL;
313 return -1;
314 }
315
316 buf[8] = '\0';
317 if (strlen(buf) == 0) {
318 LOG("server connection closed");
319 errno = EINVAL;
320 return -1;
321 }
322
323 TRACE("Magic is %c%c%c%c%c%c%c%c",
324 qemu_isprint(buf[0]) ? buf[0] : '.',
325 qemu_isprint(buf[1]) ? buf[1] : '.',
326 qemu_isprint(buf[2]) ? buf[2] : '.',
327 qemu_isprint(buf[3]) ? buf[3] : '.',
328 qemu_isprint(buf[4]) ? buf[4] : '.',
329 qemu_isprint(buf[5]) ? buf[5] : '.',
330 qemu_isprint(buf[6]) ? buf[6] : '.',
331 qemu_isprint(buf[7]) ? buf[7] : '.');
332
333 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
334 LOG("Invalid magic received");
335 errno = EINVAL;
336 return -1;
337 }
338
339 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
340 LOG("read failed");
341 errno = EINVAL;
342 return -1;
343 }
344 magic = be64_to_cpu(magic);
345 TRACE("Magic is 0x%" PRIx64, magic);
346
347 if (name) {
348 uint32_t reserved = 0;
349 uint32_t opt;
350 uint32_t namesize;
351
352 TRACE("Checking magic (opts_magic)");
353 if (magic != 0x49484156454F5054LL) {
354 LOG("Bad magic received");
355 errno = EINVAL;
356 return -1;
357 }
358 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
359 LOG("flags read failed");
360 errno = EINVAL;
361 return -1;
362 }
363 *flags = be16_to_cpu(tmp) << 16;
364 /* reserved for future use */
365 if (write_sync(csock, &reserved, sizeof(reserved)) !=
366 sizeof(reserved)) {
367 LOG("write failed (reserved)");
368 errno = EINVAL;
369 return -1;
370 }
371 /* write the export name */
372 magic = cpu_to_be64(magic);
373 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
374 LOG("write failed (magic)");
375 errno = EINVAL;
376 return -1;
377 }
378 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
379 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
380 LOG("write failed (opt)");
381 errno = EINVAL;
382 return -1;
383 }
384 namesize = cpu_to_be32(strlen(name));
385 if (write_sync(csock, &namesize, sizeof(namesize)) !=
386 sizeof(namesize)) {
387 LOG("write failed (namesize)");
388 errno = EINVAL;
389 return -1;
390 }
391 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
392 LOG("write failed (name)");
393 errno = EINVAL;
394 return -1;
395 }
396 } else {
397 TRACE("Checking magic (cli_magic)");
398
399 if (magic != 0x00420281861253LL) {
400 LOG("Bad magic received");
401 errno = EINVAL;
402 return -1;
403 }
404 }
405
406 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
407 LOG("read failed");
408 errno = EINVAL;
409 return -1;
410 }
411 *size = be64_to_cpu(s);
412 *blocksize = 1024;
413 TRACE("Size is %" PRIu64, *size);
414
415 if (!name) {
416 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
417 LOG("read failed (flags)");
418 errno = EINVAL;
419 return -1;
420 }
421 *flags = be32_to_cpup(flags);
422 } else {
423 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
424 LOG("read failed (tmp)");
425 errno = EINVAL;
426 return -1;
427 }
428 *flags |= be32_to_cpu(tmp);
429 }
430 if (read_sync(csock, &buf, 124) != 124) {
431 LOG("read failed (buf)");
432 errno = EINVAL;
433 return -1;
434 }
435 return 0;
436 }
437
438 #ifndef _WIN32
439 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
440 {
441 TRACE("Setting block size to %lu", (unsigned long)blocksize);
442
443 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
444 int serrno = errno;
445 LOG("Failed setting NBD block size");
446 errno = serrno;
447 return -1;
448 }
449
450 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
451
452 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
453 int serrno = errno;
454 LOG("Failed setting size (in blocks)");
455 errno = serrno;
456 return -1;
457 }
458
459 TRACE("Clearing NBD socket");
460
461 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
462 int serrno = errno;
463 LOG("Failed clearing NBD socket");
464 errno = serrno;
465 return -1;
466 }
467
468 TRACE("Setting NBD socket");
469
470 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
471 int serrno = errno;
472 LOG("Failed to set NBD socket");
473 errno = serrno;
474 return -1;
475 }
476
477 TRACE("Negotiation ended");
478
479 return 0;
480 }
481
482 int nbd_disconnect(int fd)
483 {
484 ioctl(fd, NBD_CLEAR_QUE);
485 ioctl(fd, NBD_DISCONNECT);
486 ioctl(fd, NBD_CLEAR_SOCK);
487 return 0;
488 }
489
490 int nbd_client(int fd)
491 {
492 int ret;
493 int serrno;
494
495 TRACE("Doing NBD loop");
496
497 ret = ioctl(fd, NBD_DO_IT);
498 serrno = errno;
499
500 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
501
502 TRACE("Clearing NBD queue");
503 ioctl(fd, NBD_CLEAR_QUE);
504
505 TRACE("Clearing NBD socket");
506 ioctl(fd, NBD_CLEAR_SOCK);
507
508 errno = serrno;
509 return ret;
510 }
511 #else
512 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
513 {
514 errno = ENOTSUP;
515 return -1;
516 }
517
518 int nbd_disconnect(int fd)
519 {
520 errno = ENOTSUP;
521 return -1;
522 }
523
524 int nbd_client(int fd)
525 {
526 errno = ENOTSUP;
527 return -1;
528 }
529 #endif
530
531 int nbd_send_request(int csock, struct nbd_request *request)
532 {
533 uint8_t buf[4 + 4 + 8 + 8 + 4];
534
535 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
536 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
537 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
538 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
539 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
540
541 TRACE("Sending request to client");
542
543 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
544 LOG("writing to socket failed");
545 errno = EINVAL;
546 return -1;
547 }
548 return 0;
549 }
550
551
552 static int nbd_receive_request(int csock, struct nbd_request *request)
553 {
554 uint8_t buf[4 + 4 + 8 + 8 + 4];
555 uint32_t magic;
556
557 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
558 LOG("read failed");
559 errno = EINVAL;
560 return -1;
561 }
562
563 /* Request
564 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
565 [ 4 .. 7] type (0 == READ, 1 == WRITE)
566 [ 8 .. 15] handle
567 [16 .. 23] from
568 [24 .. 27] len
569 */
570
571 magic = be32_to_cpup((uint32_t*)buf);
572 request->type = be32_to_cpup((uint32_t*)(buf + 4));
573 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
574 request->from = be64_to_cpup((uint64_t*)(buf + 16));
575 request->len = be32_to_cpup((uint32_t*)(buf + 24));
576
577 TRACE("Got request: "
578 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
579 magic, request->type, request->from, request->len);
580
581 if (magic != NBD_REQUEST_MAGIC) {
582 LOG("invalid magic (got 0x%x)", magic);
583 errno = EINVAL;
584 return -1;
585 }
586 return 0;
587 }
588
589 int nbd_receive_reply(int csock, struct nbd_reply *reply)
590 {
591 uint8_t buf[4 + 4 + 8];
592 uint32_t magic;
593
594 memset(buf, 0xAA, sizeof(buf));
595
596 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
597 LOG("read failed");
598 errno = EINVAL;
599 return -1;
600 }
601
602 /* Reply
603 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
604 [ 4 .. 7] error (0 == no error)
605 [ 7 .. 15] handle
606 */
607
608 magic = be32_to_cpup((uint32_t*)buf);
609 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
610 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
611
612 TRACE("Got reply: "
613 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
614 magic, reply->error, reply->handle);
615
616 if (magic != NBD_REPLY_MAGIC) {
617 LOG("invalid magic (got 0x%x)", magic);
618 errno = EINVAL;
619 return -1;
620 }
621 return 0;
622 }
623
624 static int nbd_send_reply(int csock, struct nbd_reply *reply)
625 {
626 uint8_t buf[4 + 4 + 8];
627
628 /* Reply
629 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
630 [ 4 .. 7] error (0 == no error)
631 [ 7 .. 15] handle
632 */
633 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
634 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
635 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
636
637 TRACE("Sending response to client");
638
639 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
640 LOG("writing to socket failed");
641 errno = EINVAL;
642 return -1;
643 }
644 return 0;
645 }
646
647 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
648 off_t *offset, bool readonly, uint8_t *data, int data_size)
649 {
650 struct nbd_request request;
651 struct nbd_reply reply;
652
653 TRACE("Reading request.");
654
655 if (nbd_receive_request(csock, &request) == -1)
656 return -1;
657
658 if (request.len > data_size) {
659 LOG("len (%u) is larger than max len (%u)",
660 request.len, data_size);
661 errno = EINVAL;
662 return -1;
663 }
664
665 if ((request.from + request.len) < request.from) {
666 LOG("integer overflow detected! "
667 "you're probably being attacked");
668 errno = EINVAL;
669 return -1;
670 }
671
672 if ((request.from + request.len) > size) {
673 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
674 ", Offset: %" PRIu64 "\n",
675 request.from, request.len, (uint64_t)size, dev_offset);
676 LOG("requested operation past EOF--bad client?");
677 errno = EINVAL;
678 return -1;
679 }
680
681 TRACE("Decoding type");
682
683 reply.handle = request.handle;
684 reply.error = 0;
685
686 switch (request.type) {
687 case NBD_CMD_READ:
688 TRACE("Request type is READ");
689
690 if (bdrv_read(bs, (request.from + dev_offset) / 512, data,
691 request.len / 512) == -1) {
692 LOG("reading from file failed");
693 errno = EINVAL;
694 return -1;
695 }
696 *offset += request.len;
697
698 TRACE("Read %u byte(s)", request.len);
699
700 if (nbd_send_reply(csock, &reply) == -1)
701 return -1;
702
703 TRACE("Sending data to client");
704
705 if (write_sync(csock, data, request.len) != request.len) {
706 LOG("writing to socket failed");
707 errno = EINVAL;
708 return -1;
709 }
710 break;
711 case NBD_CMD_WRITE:
712 TRACE("Request type is WRITE");
713
714 TRACE("Reading %u byte(s)", request.len);
715
716 if (read_sync(csock, data, request.len) != request.len) {
717 LOG("reading from socket failed");
718 errno = EINVAL;
719 return -1;
720 }
721
722 if (readonly) {
723 TRACE("Server is read-only, return error");
724 reply.error = 1;
725 } else {
726 TRACE("Writing to device");
727
728 if (bdrv_write(bs, (request.from + dev_offset) / 512,
729 data, request.len / 512) == -1) {
730 LOG("writing to file failed");
731 errno = EINVAL;
732 return -1;
733 }
734
735 *offset += request.len;
736 }
737
738 if (nbd_send_reply(csock, &reply) == -1)
739 return -1;
740 break;
741 case NBD_CMD_DISC:
742 TRACE("Request type is DISCONNECT");
743 errno = 0;
744 return 1;
745 default:
746 LOG("invalid request type (%u) received", request.type);
747 errno = EINVAL;
748 return -1;
749 }
750
751 TRACE("Request/Reply complete");
752
753 return 0;
754 }