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>
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/tcp.h>
31 #include <arpa/inet.h>
36 #define LOG(msg, ...) do { \
37 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
38 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
41 #define TRACE(msg, ...) do { \
42 if (verbose) LOG(msg, ## __VA_ARGS__); \
45 /* This is all part of the "official" NBD API */
47 #define NBD_REQUEST_MAGIC 0x25609513
48 #define NBD_REPLY_MAGIC 0x67446698
50 #define NBD_SET_SOCK _IO(0xab, 0)
51 #define NBD_SET_BLKSIZE _IO(0xab, 1)
52 #define NBD_SET_SIZE _IO(0xab, 2)
53 #define NBD_DO_IT _IO(0xab, 3)
54 #define NBD_CLEAR_SOCK _IO(0xab, 4)
55 #define NBD_CLEAR_QUE _IO(0xab, 5)
56 #define NBD_PRINT_DEBUG _IO(0xab, 6)
57 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
58 #define NBD_DISCONNECT _IO(0xab, 8)
60 /* That's all folks */
62 #define read_sync(fd, buffer, size) wr_sync(fd, buffer, size, true)
63 #define write_sync(fd, buffer, size) wr_sync(fd, buffer, size, false)
65 static size_t wr_sync(int fd, void *buffer, size_t size, bool do_read)
69 while (offset < size) {
73 len = read(fd, buffer + offset, size - offset);
75 len = write(fd, buffer + offset, size - offset);
78 /* recoverable error */
79 if (len == -1 && errno == EAGAIN) {
88 /* unrecoverable error */
99 static int tcp_socket_outgoing(const char *address, uint16_t port)
103 struct sockaddr_in addr;
106 s = socket(PF_INET, SOCK_STREAM, 0);
111 if (inet_aton(address, &in) == 0) {
114 ent = gethostbyname(address);
119 memcpy(&in, ent->h_addr, sizeof(in));
122 addr.sin_family = AF_INET;
123 addr.sin_port = htons(port);
124 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
126 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
138 int tcp_socket_incoming(const char *address, uint16_t port)
142 struct sockaddr_in addr;
146 s = socket(PF_INET, SOCK_STREAM, 0);
151 if (inet_aton(address, &in) == 0) {
154 ent = gethostbyname(address);
159 memcpy(&in, ent->h_addr, sizeof(in));
162 addr.sin_family = AF_INET;
163 addr.sin_port = htons(port);
164 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
167 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
171 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
175 if (listen(s, 128) == -1) {
187 int unix_socket_incoming(const char *path)
190 struct sockaddr_un addr;
193 s = socket(PF_UNIX, SOCK_STREAM, 0);
198 memset(&addr, 0, sizeof(addr));
199 addr.sun_family = AF_UNIX;
200 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
202 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
206 if (listen(s, 128) == -1) {
218 int unix_socket_outgoing(const char *path)
221 struct sockaddr_un addr;
224 s = socket(PF_UNIX, SOCK_STREAM, 0);
229 memset(&addr, 0, sizeof(addr));
230 addr.sun_family = AF_UNIX;
231 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
233 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
260 int nbd_negotiate(BlockDriverState *bs, int csock, off_t size)
262 char buf[8 + 8 + 8 + 128];
265 [ 0 .. 7] passwd ("NBDMAGIC")
266 [ 8 .. 15] magic (0x00420281861253)
268 [24 .. 151] reserved (0)
271 TRACE("Beginning negotiation.");
272 memcpy(buf, "NBDMAGIC", 8);
273 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
274 cpu_to_be64w((uint64_t*)(buf + 16), size);
275 memset(buf + 24, 0, 128);
277 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
283 TRACE("Negotation succeeded.");
288 int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
290 char buf[8 + 8 + 8 + 128];
293 TRACE("Receiving negotation.");
295 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
301 magic = be64_to_cpup((uint64_t*)(buf + 8));
302 *size = be64_to_cpup((uint64_t*)(buf + 16));
305 TRACE("Magic is %c%c%c%c%c%c%c%c",
306 isprint(buf[0]) ? buf[0] : '.',
307 isprint(buf[1]) ? buf[1] : '.',
308 isprint(buf[2]) ? buf[2] : '.',
309 isprint(buf[3]) ? buf[3] : '.',
310 isprint(buf[4]) ? buf[4] : '.',
311 isprint(buf[5]) ? buf[5] : '.',
312 isprint(buf[6]) ? buf[6] : '.',
313 isprint(buf[7]) ? buf[7] : '.');
314 TRACE("Magic is 0x%" PRIx64, magic);
315 TRACE("Size is %" PRIu64, *size);
317 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
318 LOG("Invalid magic received");
323 TRACE("Checking magic");
325 if (magic != 0x00420281861253LL) {
326 LOG("Bad magic received");
333 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
335 TRACE("Setting block size to %lu", (unsigned long)blocksize);
337 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
339 LOG("Failed setting NBD block size");
344 TRACE("Setting size to %llu block(s)",
345 (unsigned long long)(size / blocksize));
347 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
349 LOG("Failed setting size (in blocks)");
354 TRACE("Clearing NBD socket");
356 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
358 LOG("Failed clearing NBD socket");
363 TRACE("Setting NBD socket");
365 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
367 LOG("Failed to set NBD socket");
372 TRACE("Negotiation ended");
377 int nbd_disconnect(int fd)
379 ioctl(fd, NBD_CLEAR_QUE);
380 ioctl(fd, NBD_DISCONNECT);
381 ioctl(fd, NBD_CLEAR_SOCK);
385 int nbd_client(int fd, int csock)
390 TRACE("Doing NBD loop");
392 ret = ioctl(fd, NBD_DO_IT);
395 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
397 TRACE("Clearing NBD queue");
398 ioctl(fd, NBD_CLEAR_QUE);
400 TRACE("Clearing NBD socket");
401 ioctl(fd, NBD_CLEAR_SOCK);
407 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
408 off_t *offset, bool readonly, uint8_t *data, int data_size)
410 uint8_t buf[4 + 4 + 8 + 8 + 4];
416 TRACE("Reading request.");
418 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
425 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
426 [ 4 .. 7] type (0 == READ, 1 == WRITE)
432 magic = be32_to_cpup((uint32_t*)buf);
433 type = be32_to_cpup((uint32_t*)(buf + 4));
434 from = be64_to_cpup((uint64_t*)(buf + 16));
435 len = be32_to_cpup((uint32_t*)(buf + 24));
437 TRACE("Got request: "
438 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
439 magic, type, from, len);
442 if (magic != NBD_REQUEST_MAGIC) {
443 LOG("invalid magic (got 0x%x)", magic);
448 if (len > data_size) {
449 LOG("len (%u) is larger than max len (%u)",
455 if ((from + len) < from) {
456 LOG("integer overflow detected! "
457 "you're probably being attacked");
462 if ((from + len) > size) {
463 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
464 ", Offset: %" PRIu64 "\n",
465 from, len, size, dev_offset);
466 LOG("requested operation past EOF--bad client?");
472 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
473 [ 4 .. 7] error (0 == no error)
476 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
477 cpu_to_be32w((uint32_t*)(buf + 4), 0);
479 TRACE("Decoding type");
483 TRACE("Request type is READ");
485 if (bdrv_read(bs, (from + dev_offset) / 512, data, len / 512) == -1) {
486 LOG("reading from file failed");
492 TRACE("Read %u byte(s)", len);
494 TRACE("Sending OK response");
496 if (write_sync(csock, buf, 16) != 16) {
497 LOG("writing to socket failed");
502 TRACE("Sending data to client");
504 if (write_sync(csock, data, len) != len) {
505 LOG("writing to socket failed");
511 TRACE("Request type is WRITE");
513 TRACE("Reading %u byte(s)", len);
515 if (read_sync(csock, data, len) != len) {
516 LOG("reading from socket failed");
522 TRACE("Server is read-only, return error");
524 cpu_to_be32w((uint32_t*)(buf + 4), 1);
526 TRACE("Writing to device");
528 if (bdrv_write(bs, (from + dev_offset) / 512, data, len / 512) == -1) {
529 LOG("writing to file failed");
537 TRACE("Sending response to client");
539 if (write_sync(csock, buf, 16) != 16) {
540 LOG("writing to socket failed");
546 TRACE("Request type is DISCONNECT");
550 LOG("invalid request type (%u) received", type);
555 TRACE("Request/Reply complete");