2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
25 #include <sys/ioctl.h>
28 #include <sys/ioccom.h>
33 #include "qemu_socket.h"
38 #define TRACE(msg, ...) do { \
39 LOG(msg, ## __VA_ARGS__); \
42 #define TRACE(msg, ...) \
46 #define LOG(msg, ...) do { \
47 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
48 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
51 /* This is all part of the "official" NBD API */
53 #define NBD_REQUEST_MAGIC 0x25609513
54 #define NBD_REPLY_MAGIC 0x67446698
56 #define NBD_SET_SOCK _IO(0xab, 0)
57 #define NBD_SET_BLKSIZE _IO(0xab, 1)
58 #define NBD_SET_SIZE _IO(0xab, 2)
59 #define NBD_DO_IT _IO(0xab, 3)
60 #define NBD_CLEAR_SOCK _IO(0xab, 4)
61 #define NBD_CLEAR_QUE _IO(0xab, 5)
62 #define NBD_PRINT_DEBUG _IO(0xab, 6)
63 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
64 #define NBD_DISCONNECT _IO(0xab, 8)
66 /* That's all folks */
68 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
69 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
71 size_t nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
75 while (offset
< size
) {
79 len
= recv(fd
, buffer
+ offset
, size
- offset
, 0);
81 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
85 errno
= socket_error();
87 /* recoverable error */
88 if (len
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) {
97 /* unrecoverable error */
108 int tcp_socket_outgoing(const char *address
, uint16_t port
)
112 struct sockaddr_in addr
;
114 s
= socket(PF_INET
, SOCK_STREAM
, 0);
119 if (inet_aton(address
, &in
) == 0) {
122 ent
= gethostbyname(address
);
127 memcpy(&in
, ent
->h_addr
, sizeof(in
));
130 addr
.sin_family
= AF_INET
;
131 addr
.sin_port
= htons(port
);
132 memcpy(&addr
.sin_addr
.s_addr
, &in
, sizeof(in
));
134 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
144 int tcp_socket_incoming(const char *address
, uint16_t port
)
148 struct sockaddr_in addr
;
151 s
= socket(PF_INET
, SOCK_STREAM
, 0);
156 if (inet_aton(address
, &in
) == 0) {
159 ent
= gethostbyname(address
);
164 memcpy(&in
, ent
->h_addr
, sizeof(in
));
167 addr
.sin_family
= AF_INET
;
168 addr
.sin_port
= htons(port
);
169 memcpy(&addr
.sin_addr
.s_addr
, &in
, sizeof(in
));
172 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
173 (const void *) &opt
, sizeof(opt
)) == -1) {
177 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
181 if (listen(s
, 128) == -1) {
192 int unix_socket_incoming(const char *path
)
195 struct sockaddr_un addr
;
197 s
= socket(PF_UNIX
, SOCK_STREAM
, 0);
202 memset(&addr
, 0, sizeof(addr
));
203 addr
.sun_family
= AF_UNIX
;
204 pstrcpy(addr
.sun_path
, sizeof(addr
.sun_path
), path
);
206 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
210 if (listen(s
, 128) == -1) {
220 int unix_socket_outgoing(const char *path
)
223 struct sockaddr_un addr
;
225 s
= socket(PF_UNIX
, SOCK_STREAM
, 0);
230 memset(&addr
, 0, sizeof(addr
));
231 addr
.sun_family
= AF_UNIX
;
232 pstrcpy(addr
.sun_path
, sizeof(addr
.sun_path
), path
);
234 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
244 int unix_socket_incoming(const char *path
)
250 int unix_socket_outgoing(const char *path
)
272 int nbd_negotiate(int csock
, off_t size
)
274 char buf
[8 + 8 + 8 + 128];
277 [ 0 .. 7] passwd ("NBDMAGIC")
278 [ 8 .. 15] magic (0x00420281861253)
280 [24 .. 151] reserved (0)
283 TRACE("Beginning negotiation.");
284 memcpy(buf
, "NBDMAGIC", 8);
285 cpu_to_be64w((uint64_t*)(buf
+ 8), 0x00420281861253LL
);
286 cpu_to_be64w((uint64_t*)(buf
+ 16), size
);
287 memset(buf
+ 24, 0, 128);
289 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
295 TRACE("Negotation succeeded.");
300 int nbd_receive_negotiate(int csock
, off_t
*size
, size_t *blocksize
)
302 char buf
[8 + 8 + 8 + 128];
305 TRACE("Receiving negotation.");
307 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
313 magic
= be64_to_cpup((uint64_t*)(buf
+ 8));
314 *size
= be64_to_cpup((uint64_t*)(buf
+ 16));
317 TRACE("Magic is %c%c%c%c%c%c%c%c",
318 qemu_isprint(buf
[0]) ? buf
[0] : '.',
319 qemu_isprint(buf
[1]) ? buf
[1] : '.',
320 qemu_isprint(buf
[2]) ? buf
[2] : '.',
321 qemu_isprint(buf
[3]) ? buf
[3] : '.',
322 qemu_isprint(buf
[4]) ? buf
[4] : '.',
323 qemu_isprint(buf
[5]) ? buf
[5] : '.',
324 qemu_isprint(buf
[6]) ? buf
[6] : '.',
325 qemu_isprint(buf
[7]) ? buf
[7] : '.');
326 TRACE("Magic is 0x%" PRIx64
, magic
);
327 TRACE("Size is %" PRIu64
, *size
);
329 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
330 LOG("Invalid magic received");
335 TRACE("Checking magic");
337 if (magic
!= 0x00420281861253LL
) {
338 LOG("Bad magic received");
346 int nbd_init(int fd
, int csock
, off_t size
, size_t blocksize
)
348 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
350 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) == -1) {
352 LOG("Failed setting NBD block size");
357 TRACE("Setting size to %llu block(s)",
358 (unsigned long long)(size
/ blocksize
));
360 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) == -1) {
362 LOG("Failed setting size (in blocks)");
367 TRACE("Clearing NBD socket");
369 if (ioctl(fd
, NBD_CLEAR_SOCK
) == -1) {
371 LOG("Failed clearing NBD socket");
376 TRACE("Setting NBD socket");
378 if (ioctl(fd
, NBD_SET_SOCK
, csock
) == -1) {
380 LOG("Failed to set NBD socket");
385 TRACE("Negotiation ended");
390 int nbd_disconnect(int fd
)
392 ioctl(fd
, NBD_CLEAR_QUE
);
393 ioctl(fd
, NBD_DISCONNECT
);
394 ioctl(fd
, NBD_CLEAR_SOCK
);
398 int nbd_client(int fd
, int csock
)
403 TRACE("Doing NBD loop");
405 ret
= ioctl(fd
, NBD_DO_IT
);
408 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
410 TRACE("Clearing NBD queue");
411 ioctl(fd
, NBD_CLEAR_QUE
);
413 TRACE("Clearing NBD socket");
414 ioctl(fd
, NBD_CLEAR_SOCK
);
420 int nbd_init(int fd
, int csock
, off_t size
, size_t blocksize
)
426 int nbd_disconnect(int fd
)
432 int nbd_client(int fd
, int csock
)
439 int nbd_send_request(int csock
, struct nbd_request
*request
)
441 uint8_t buf
[4 + 4 + 8 + 8 + 4];
443 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
444 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
445 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
446 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
447 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
449 TRACE("Sending request to client");
451 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
452 LOG("writing to socket failed");
460 static int nbd_receive_request(int csock
, struct nbd_request
*request
)
462 uint8_t buf
[4 + 4 + 8 + 8 + 4];
465 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
472 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
473 [ 4 .. 7] type (0 == READ, 1 == WRITE)
479 magic
= be32_to_cpup((uint32_t*)buf
);
480 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
481 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
482 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
483 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
485 TRACE("Got request: "
486 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
487 magic
, request
->type
, request
->from
, request
->len
);
489 if (magic
!= NBD_REQUEST_MAGIC
) {
490 LOG("invalid magic (got 0x%x)", magic
);
497 int nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
499 uint8_t buf
[4 + 4 + 8];
502 memset(buf
, 0xAA, sizeof(buf
));
504 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
511 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
512 [ 4 .. 7] error (0 == no error)
516 magic
= be32_to_cpup((uint32_t*)buf
);
517 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
518 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
521 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
522 magic
, reply
->error
, reply
->handle
);
524 if (magic
!= NBD_REPLY_MAGIC
) {
525 LOG("invalid magic (got 0x%x)", magic
);
532 static int nbd_send_reply(int csock
, struct nbd_reply
*reply
)
534 uint8_t buf
[4 + 4 + 8];
537 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
538 [ 4 .. 7] error (0 == no error)
541 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
542 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
543 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
545 TRACE("Sending response to client");
547 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
548 LOG("writing to socket failed");
555 int nbd_trip(BlockDriverState
*bs
, int csock
, off_t size
, uint64_t dev_offset
,
556 off_t
*offset
, bool readonly
, uint8_t *data
, int data_size
)
558 struct nbd_request request
;
559 struct nbd_reply reply
;
561 TRACE("Reading request.");
563 if (nbd_receive_request(csock
, &request
) == -1)
566 if (request
.len
> data_size
) {
567 LOG("len (%u) is larger than max len (%u)",
568 request
.len
, data_size
);
573 if ((request
.from
+ request
.len
) < request
.from
) {
574 LOG("integer overflow detected! "
575 "you're probably being attacked");
580 if ((request
.from
+ request
.len
) > size
) {
581 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
582 ", Offset: %" PRIu64
"\n",
583 request
.from
, request
.len
, (uint64_t)size
, dev_offset
);
584 LOG("requested operation past EOF--bad client?");
589 TRACE("Decoding type");
591 reply
.handle
= request
.handle
;
594 switch (request
.type
) {
596 TRACE("Request type is READ");
598 if (bdrv_read(bs
, (request
.from
+ dev_offset
) / 512, data
,
599 request
.len
/ 512) == -1) {
600 LOG("reading from file failed");
604 *offset
+= request
.len
;
606 TRACE("Read %u byte(s)", request
.len
);
608 if (nbd_send_reply(csock
, &reply
) == -1)
611 TRACE("Sending data to client");
613 if (write_sync(csock
, data
, request
.len
) != request
.len
) {
614 LOG("writing to socket failed");
620 TRACE("Request type is WRITE");
622 TRACE("Reading %u byte(s)", request
.len
);
624 if (read_sync(csock
, data
, request
.len
) != request
.len
) {
625 LOG("reading from socket failed");
631 TRACE("Server is read-only, return error");
634 TRACE("Writing to device");
636 if (bdrv_write(bs
, (request
.from
+ dev_offset
) / 512,
637 data
, request
.len
/ 512) == -1) {
638 LOG("writing to file failed");
643 *offset
+= request
.len
;
646 if (nbd_send_reply(csock
, &reply
) == -1)
650 TRACE("Request type is DISCONNECT");
654 LOG("invalid request type (%u) received", request
.type
);
659 TRACE("Request/Reply complete");