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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/ioctl.h>
26 #include <sys/ioccom.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <arpa/inet.h>
40 static int verbose
= 0;
43 #define TRACE(msg, ...) do { \
44 if (verbose) LOG(msg, ## __VA_ARGS__); \
47 #define LOG(msg, ...) do { \
48 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
49 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
52 /* This is all part of the "official" NBD API */
54 #define NBD_REQUEST_MAGIC 0x25609513
55 #define NBD_REPLY_MAGIC 0x67446698
57 #define NBD_SET_SOCK _IO(0xab, 0)
58 #define NBD_SET_BLKSIZE _IO(0xab, 1)
59 #define NBD_SET_SIZE _IO(0xab, 2)
60 #define NBD_DO_IT _IO(0xab, 3)
61 #define NBD_CLEAR_SOCK _IO(0xab, 4)
62 #define NBD_CLEAR_QUE _IO(0xab, 5)
63 #define NBD_PRINT_DEBUG _IO(0xab, 6)
64 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
65 #define NBD_DISCONNECT _IO(0xab, 8)
67 /* That's all folks */
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)
72 size_t nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
76 while (offset
< size
) {
80 len
= read(fd
, buffer
+ offset
, size
- offset
);
82 len
= write(fd
, buffer
+ offset
, size
- offset
);
85 /* recoverable error */
86 if (len
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) {
95 /* unrecoverable error */
106 int tcp_socket_outgoing(const char *address
, uint16_t port
)
110 struct sockaddr_in addr
;
113 s
= socket(PF_INET
, SOCK_STREAM
, 0);
118 if (inet_aton(address
, &in
) == 0) {
121 ent
= gethostbyname(address
);
126 memcpy(&in
, ent
->h_addr
, sizeof(in
));
129 addr
.sin_family
= AF_INET
;
130 addr
.sin_port
= htons(port
);
131 memcpy(&addr
.sin_addr
.s_addr
, &in
, sizeof(in
));
133 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
145 int tcp_socket_incoming(const char *address
, uint16_t port
)
149 struct sockaddr_in addr
;
153 s
= socket(PF_INET
, SOCK_STREAM
, 0);
158 if (inet_aton(address
, &in
) == 0) {
161 ent
= gethostbyname(address
);
166 memcpy(&in
, ent
->h_addr
, sizeof(in
));
169 addr
.sin_family
= AF_INET
;
170 addr
.sin_port
= htons(port
);
171 memcpy(&addr
.sin_addr
.s_addr
, &in
, sizeof(in
));
174 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(opt
)) == -1) {
178 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
182 if (listen(s
, 128) == -1) {
194 int unix_socket_incoming(const char *path
)
197 struct sockaddr_un addr
;
200 s
= socket(PF_UNIX
, SOCK_STREAM
, 0);
205 memset(&addr
, 0, sizeof(addr
));
206 addr
.sun_family
= AF_UNIX
;
207 pstrcpy(addr
.sun_path
, sizeof(addr
.sun_path
), path
);
209 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
213 if (listen(s
, 128) == -1) {
225 int unix_socket_outgoing(const char *path
)
228 struct sockaddr_un addr
;
231 s
= socket(PF_UNIX
, SOCK_STREAM
, 0);
236 memset(&addr
, 0, sizeof(addr
));
237 addr
.sun_family
= AF_UNIX
;
238 pstrcpy(addr
.sun_path
, sizeof(addr
.sun_path
), path
);
240 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
267 int nbd_negotiate(BlockDriverState
*bs
, int csock
, off_t size
)
269 char buf
[8 + 8 + 8 + 128];
272 [ 0 .. 7] passwd ("NBDMAGIC")
273 [ 8 .. 15] magic (0x00420281861253)
275 [24 .. 151] reserved (0)
278 TRACE("Beginning negotiation.");
279 memcpy(buf
, "NBDMAGIC", 8);
280 cpu_to_be64w((uint64_t*)(buf
+ 8), 0x00420281861253LL
);
281 cpu_to_be64w((uint64_t*)(buf
+ 16), size
);
282 memset(buf
+ 24, 0, 128);
284 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
290 TRACE("Negotation succeeded.");
295 int nbd_receive_negotiate(int csock
, off_t
*size
, size_t *blocksize
)
297 char buf
[8 + 8 + 8 + 128];
300 TRACE("Receiving negotation.");
302 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
308 magic
= be64_to_cpup((uint64_t*)(buf
+ 8));
309 *size
= be64_to_cpup((uint64_t*)(buf
+ 16));
312 TRACE("Magic is %c%c%c%c%c%c%c%c",
313 isprint(buf
[0]) ? buf
[0] : '.',
314 isprint(buf
[1]) ? buf
[1] : '.',
315 isprint(buf
[2]) ? buf
[2] : '.',
316 isprint(buf
[3]) ? buf
[3] : '.',
317 isprint(buf
[4]) ? buf
[4] : '.',
318 isprint(buf
[5]) ? buf
[5] : '.',
319 isprint(buf
[6]) ? buf
[6] : '.',
320 isprint(buf
[7]) ? buf
[7] : '.');
321 TRACE("Magic is 0x%" PRIx64
, magic
);
322 TRACE("Size is %" PRIu64
, *size
);
324 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
325 LOG("Invalid magic received");
330 TRACE("Checking magic");
332 if (magic
!= 0x00420281861253LL
) {
333 LOG("Bad magic received");
340 int nbd_init(int fd
, int csock
, off_t size
, size_t blocksize
)
342 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
344 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) == -1) {
346 LOG("Failed setting NBD block size");
351 TRACE("Setting size to %llu block(s)",
352 (unsigned long long)(size
/ blocksize
));
354 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) == -1) {
356 LOG("Failed setting size (in blocks)");
361 TRACE("Clearing NBD socket");
363 if (ioctl(fd
, NBD_CLEAR_SOCK
) == -1) {
365 LOG("Failed clearing NBD socket");
370 TRACE("Setting NBD socket");
372 if (ioctl(fd
, NBD_SET_SOCK
, csock
) == -1) {
374 LOG("Failed to set NBD socket");
379 TRACE("Negotiation ended");
384 int nbd_disconnect(int fd
)
386 ioctl(fd
, NBD_CLEAR_QUE
);
387 ioctl(fd
, NBD_DISCONNECT
);
388 ioctl(fd
, NBD_CLEAR_SOCK
);
392 int nbd_client(int fd
, int csock
)
397 TRACE("Doing NBD loop");
399 ret
= ioctl(fd
, NBD_DO_IT
);
402 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
404 TRACE("Clearing NBD queue");
405 ioctl(fd
, NBD_CLEAR_QUE
);
407 TRACE("Clearing NBD socket");
408 ioctl(fd
, NBD_CLEAR_SOCK
);
414 int nbd_send_request(int csock
, struct nbd_request
*request
)
416 uint8_t buf
[4 + 4 + 8 + 8 + 4];
418 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
419 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
420 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
421 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
422 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
424 TRACE("Sending request to client");
426 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
427 LOG("writing to socket failed");
435 static int nbd_receive_request(int csock
, struct nbd_request
*request
)
437 uint8_t buf
[4 + 4 + 8 + 8 + 4];
440 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
447 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
448 [ 4 .. 7] type (0 == READ, 1 == WRITE)
454 magic
= be32_to_cpup((uint32_t*)buf
);
455 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
456 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
457 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
458 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
460 TRACE("Got request: "
461 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
462 magic
, request
->type
, request
->from
, request
->len
);
464 if (magic
!= NBD_REQUEST_MAGIC
) {
465 LOG("invalid magic (got 0x%x)", magic
);
472 int nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
474 uint8_t buf
[4 + 4 + 8];
477 memset(buf
, 0xAA, sizeof(buf
));
479 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
486 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
487 [ 4 .. 7] error (0 == no error)
491 magic
= be32_to_cpup((uint32_t*)buf
);
492 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
493 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
496 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
497 magic
, reply
->error
, reply
->handle
);
499 if (magic
!= NBD_REPLY_MAGIC
) {
500 LOG("invalid magic (got 0x%x)", magic
);
507 static int nbd_send_reply(int csock
, struct nbd_reply
*reply
)
509 uint8_t buf
[4 + 4 + 8];
512 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
513 [ 4 .. 7] error (0 == no error)
516 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
517 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
518 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
520 TRACE("Sending response to client");
522 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
523 LOG("writing to socket failed");
530 int nbd_trip(BlockDriverState
*bs
, int csock
, off_t size
, uint64_t dev_offset
,
531 off_t
*offset
, bool readonly
, uint8_t *data
, int data_size
)
533 struct nbd_request request
;
534 struct nbd_reply reply
;
536 TRACE("Reading request.");
538 if (nbd_receive_request(csock
, &request
) == -1)
541 if (request
.len
> data_size
) {
542 LOG("len (%u) is larger than max len (%u)",
543 request
.len
, data_size
);
548 if ((request
.from
+ request
.len
) < request
.from
) {
549 LOG("integer overflow detected! "
550 "you're probably being attacked");
555 if ((request
.from
+ request
.len
) > size
) {
556 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
557 ", Offset: %" PRIu64
"\n",
558 request
.from
, request
.len
, size
, dev_offset
);
559 LOG("requested operation past EOF--bad client?");
564 TRACE("Decoding type");
566 reply
.handle
= request
.handle
;
569 switch (request
.type
) {
571 TRACE("Request type is READ");
573 if (bdrv_read(bs
, (request
.from
+ dev_offset
) / 512, data
,
574 request
.len
/ 512) == -1) {
575 LOG("reading from file failed");
579 *offset
+= request
.len
;
581 TRACE("Read %u byte(s)", request
.len
);
583 if (nbd_send_reply(csock
, &reply
) == -1)
586 TRACE("Sending data to client");
588 if (write_sync(csock
, data
, request
.len
) != request
.len
) {
589 LOG("writing to socket failed");
595 TRACE("Request type is WRITE");
597 TRACE("Reading %u byte(s)", request
.len
);
599 if (read_sync(csock
, data
, request
.len
) != request
.len
) {
600 LOG("reading from socket failed");
606 TRACE("Server is read-only, return error");
609 TRACE("Writing to device");
611 if (bdrv_write(bs
, (request
.from
+ dev_offset
) / 512,
612 data
, request
.len
/ 512) == -1) {
613 LOG("writing to file failed");
618 *offset
+= request
.len
;
621 if (nbd_send_reply(csock
, &reply
) == -1)
625 TRACE("Request type is DISCONNECT");
629 LOG("invalid request type (%u) received", request
.type
);
634 TRACE("Request/Reply complete");