blob: 306d1fa28b19d73e02473cac34b92ec9799d03bd [file] [log] [blame]
ths75818252008-07-03 13:41:03 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
ths75818252008-07-03 13:41:03 +000018 */
bellard7a5ca862008-05-27 21:13:40 +000019
20#include "nbd.h"
21
22#include <errno.h>
23#include <string.h>
24#include <sys/ioctl.h>
25#include <ctype.h>
26#include <inttypes.h>
27#include <sys/socket.h>
thscd831bd2008-07-03 10:23:51 +000028#include <sys/un.h>
bellard7a5ca862008-05-27 21:13:40 +000029#include <netinet/in.h>
30#include <netinet/tcp.h>
31#include <arpa/inet.h>
32#include <netdb.h>
33
ths75818252008-07-03 13:41:03 +000034#if defined(QEMU_NBD)
bellard7a5ca862008-05-27 21:13:40 +000035extern int verbose;
ths75818252008-07-03 13:41:03 +000036#else
37static int verbose = 0;
38#endif
39
40#define TRACE(msg, ...) do { \
41 if (verbose) LOG(msg, ## __VA_ARGS__); \
42} while(0)
bellard7a5ca862008-05-27 21:13:40 +000043
44#define LOG(msg, ...) do { \
45 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
46 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
47} while(0)
48
bellard7a5ca862008-05-27 21:13:40 +000049/* This is all part of the "official" NBD API */
50
51#define NBD_REQUEST_MAGIC 0x25609513
52#define NBD_REPLY_MAGIC 0x67446698
53
54#define NBD_SET_SOCK _IO(0xab, 0)
55#define NBD_SET_BLKSIZE _IO(0xab, 1)
56#define NBD_SET_SIZE _IO(0xab, 2)
57#define NBD_DO_IT _IO(0xab, 3)
58#define NBD_CLEAR_SOCK _IO(0xab, 4)
59#define NBD_CLEAR_QUE _IO(0xab, 5)
60#define NBD_PRINT_DEBUG _IO(0xab, 6)
61#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
62#define NBD_DISCONNECT _IO(0xab, 8)
63
64/* That's all folks */
65
ths75818252008-07-03 13:41:03 +000066#define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
67#define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
bellard7a5ca862008-05-27 21:13:40 +000068
ths75818252008-07-03 13:41:03 +000069size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +000070{
71 size_t offset = 0;
72
73 while (offset < size) {
74 ssize_t len;
75
76 if (do_read) {
77 len = read(fd, buffer + offset, size - offset);
78 } else {
79 len = write(fd, buffer + offset, size - offset);
80 }
81
82 /* recoverable error */
ths75818252008-07-03 13:41:03 +000083 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
bellard7a5ca862008-05-27 21:13:40 +000084 continue;
85 }
86
87 /* eof */
88 if (len == 0) {
89 break;
90 }
91
92 /* unrecoverable error */
93 if (len == -1) {
94 return 0;
95 }
96
97 offset += len;
98 }
99
100 return offset;
101}
102
ths75818252008-07-03 13:41:03 +0000103int tcp_socket_outgoing(const char *address, uint16_t port)
bellard7a5ca862008-05-27 21:13:40 +0000104{
105 int s;
106 struct in_addr in;
107 struct sockaddr_in addr;
108 int serrno;
109
110 s = socket(PF_INET, SOCK_STREAM, 0);
111 if (s == -1) {
112 return -1;
113 }
114
115 if (inet_aton(address, &in) == 0) {
116 struct hostent *ent;
117
118 ent = gethostbyname(address);
119 if (ent == NULL) {
120 goto error;
121 }
122
123 memcpy(&in, ent->h_addr, sizeof(in));
124 }
125
126 addr.sin_family = AF_INET;
127 addr.sin_port = htons(port);
128 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
129
130 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
131 goto error;
132 }
133
134 return s;
135error:
136 serrno = errno;
137 close(s);
138 errno = serrno;
139 return -1;
140}
141
142int tcp_socket_incoming(const char *address, uint16_t port)
143{
144 int s;
145 struct in_addr in;
146 struct sockaddr_in addr;
147 int serrno;
148 int opt;
149
150 s = socket(PF_INET, SOCK_STREAM, 0);
151 if (s == -1) {
152 return -1;
153 }
154
155 if (inet_aton(address, &in) == 0) {
156 struct hostent *ent;
157
158 ent = gethostbyname(address);
159 if (ent == NULL) {
160 goto error;
161 }
162
163 memcpy(&in, ent->h_addr, sizeof(in));
164 }
165
166 addr.sin_family = AF_INET;
167 addr.sin_port = htons(port);
168 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
169
170 opt = 1;
171 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
172 goto error;
173 }
174
175 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
176 goto error;
177 }
178
179 if (listen(s, 128) == -1) {
180 goto error;
181 }
182
183 return s;
184error:
185 serrno = errno;
186 close(s);
187 errno = serrno;
188 return -1;
189}
190
thscd831bd2008-07-03 10:23:51 +0000191int unix_socket_incoming(const char *path)
192{
193 int s;
194 struct sockaddr_un addr;
195 int serrno;
196
197 s = socket(PF_UNIX, SOCK_STREAM, 0);
198 if (s == -1) {
199 return -1;
200 }
201
202 memset(&addr, 0, sizeof(addr));
203 addr.sun_family = AF_UNIX;
204 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
205
206 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
207 goto error;
208 }
209
210 if (listen(s, 128) == -1) {
211 goto error;
212 }
213
214 return s;
215error:
216 serrno = errno;
217 close(s);
218 errno = serrno;
219 return -1;
220}
221
222int unix_socket_outgoing(const char *path)
223{
224 int s;
225 struct sockaddr_un addr;
226 int serrno;
227
228 s = socket(PF_UNIX, SOCK_STREAM, 0);
229 if (s == -1) {
230 return -1;
231 }
232
233 memset(&addr, 0, sizeof(addr));
234 addr.sun_family = AF_UNIX;
235 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
236
237 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
238 goto error;
239 }
240
241 return s;
242error:
243 serrno = errno;
244 close(s);
245 errno = serrno;
246 return -1;
247}
248
249
bellard7a5ca862008-05-27 21:13:40 +0000250/* Basic flow
251
252 Server Client
253
254 Negotiate
255 Request
256 Response
257 Request
258 Response
259 ...
260 ...
261 Request (type == 2)
262*/
263
264int nbd_negotiate(BlockDriverState *bs, int csock, off_t size)
265{
266 char buf[8 + 8 + 8 + 128];
267
268 /* Negotiate
269 [ 0 .. 7] passwd ("NBDMAGIC")
270 [ 8 .. 15] magic (0x00420281861253)
271 [16 .. 23] size
272 [24 .. 151] reserved (0)
273 */
274
275 TRACE("Beginning negotiation.");
276 memcpy(buf, "NBDMAGIC", 8);
277 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
278 cpu_to_be64w((uint64_t*)(buf + 16), size);
279 memset(buf + 24, 0, 128);
280
281 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
282 LOG("write failed");
283 errno = EINVAL;
284 return -1;
285 }
286
287 TRACE("Negotation succeeded.");
288
289 return 0;
290}
291
thscd831bd2008-07-03 10:23:51 +0000292int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000293{
294 char buf[8 + 8 + 8 + 128];
295 uint64_t magic;
bellard7a5ca862008-05-27 21:13:40 +0000296
297 TRACE("Receiving negotation.");
298
299 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
300 LOG("read failed");
301 errno = EINVAL;
302 return -1;
303 }
304
305 magic = be64_to_cpup((uint64_t*)(buf + 8));
thscd831bd2008-07-03 10:23:51 +0000306 *size = be64_to_cpup((uint64_t*)(buf + 16));
307 *blocksize = 1024;
bellard7a5ca862008-05-27 21:13:40 +0000308
309 TRACE("Magic is %c%c%c%c%c%c%c%c",
310 isprint(buf[0]) ? buf[0] : '.',
311 isprint(buf[1]) ? buf[1] : '.',
312 isprint(buf[2]) ? buf[2] : '.',
313 isprint(buf[3]) ? buf[3] : '.',
314 isprint(buf[4]) ? buf[4] : '.',
315 isprint(buf[5]) ? buf[5] : '.',
316 isprint(buf[6]) ? buf[6] : '.',
317 isprint(buf[7]) ? buf[7] : '.');
318 TRACE("Magic is 0x%" PRIx64, magic);
thscd831bd2008-07-03 10:23:51 +0000319 TRACE("Size is %" PRIu64, *size);
bellard7a5ca862008-05-27 21:13:40 +0000320
321 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
322 LOG("Invalid magic received");
323 errno = EINVAL;
324 return -1;
325 }
326
327 TRACE("Checking magic");
328
329 if (magic != 0x00420281861253LL) {
330 LOG("Bad magic received");
331 errno = EINVAL;
332 return -1;
333 }
thscd831bd2008-07-03 10:23:51 +0000334 return 0;
335}
bellard7a5ca862008-05-27 21:13:40 +0000336
thscd831bd2008-07-03 10:23:51 +0000337int nbd_init(int fd, int csock, off_t size, size_t blocksize)
338{
bellard7a5ca862008-05-27 21:13:40 +0000339 TRACE("Setting block size to %lu", (unsigned long)blocksize);
340
341 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
342 int serrno = errno;
343 LOG("Failed setting NBD block size");
344 errno = serrno;
345 return -1;
346 }
347
348 TRACE("Setting size to %llu block(s)",
349 (unsigned long long)(size / blocksize));
350
351 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
352 int serrno = errno;
353 LOG("Failed setting size (in blocks)");
354 errno = serrno;
355 return -1;
356 }
357
358 TRACE("Clearing NBD socket");
359
360 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
361 int serrno = errno;
362 LOG("Failed clearing NBD socket");
363 errno = serrno;
364 return -1;
365 }
366
367 TRACE("Setting NBD socket");
368
369 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
370 int serrno = errno;
371 LOG("Failed to set NBD socket");
372 errno = serrno;
373 return -1;
374 }
375
376 TRACE("Negotiation ended");
377
378 return 0;
379}
380
381int nbd_disconnect(int fd)
382{
383 ioctl(fd, NBD_CLEAR_QUE);
384 ioctl(fd, NBD_DISCONNECT);
385 ioctl(fd, NBD_CLEAR_SOCK);
386 return 0;
387}
388
389int nbd_client(int fd, int csock)
390{
391 int ret;
392 int serrno;
393
394 TRACE("Doing NBD loop");
395
396 ret = ioctl(fd, NBD_DO_IT);
397 serrno = errno;
398
399 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
400
401 TRACE("Clearing NBD queue");
402 ioctl(fd, NBD_CLEAR_QUE);
403
404 TRACE("Clearing NBD socket");
405 ioctl(fd, NBD_CLEAR_SOCK);
406
407 errno = serrno;
408 return ret;
409}
410
ths75818252008-07-03 13:41:03 +0000411int nbd_send_request(int csock, struct nbd_request *request)
412{
413 uint8_t buf[4 + 4 + 8 + 8 + 4];
414
415 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
416 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
417 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
418 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
419 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
420
421 TRACE("Sending request to client");
422
423 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
424 LOG("writing to socket failed");
425 errno = EINVAL;
426 return -1;
427 }
428 return 0;
429}
430
431
432static int nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000433{
bellard7a5ca862008-05-27 21:13:40 +0000434 uint8_t buf[4 + 4 + 8 + 8 + 4];
435 uint32_t magic;
bellard7a5ca862008-05-27 21:13:40 +0000436
437 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
438 LOG("read failed");
439 errno = EINVAL;
440 return -1;
441 }
442
443 /* Request
ths75818252008-07-03 13:41:03 +0000444 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
445 [ 4 .. 7] type (0 == READ, 1 == WRITE)
446 [ 8 .. 15] handle
447 [16 .. 23] from
448 [24 .. 27] len
bellard7a5ca862008-05-27 21:13:40 +0000449 */
450
451 magic = be32_to_cpup((uint32_t*)buf);
ths75818252008-07-03 13:41:03 +0000452 request->type = be32_to_cpup((uint32_t*)(buf + 4));
453 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
454 request->from = be64_to_cpup((uint64_t*)(buf + 16));
455 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000456
457 TRACE("Got request: "
458 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
ths75818252008-07-03 13:41:03 +0000459 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000460
461 if (magic != NBD_REQUEST_MAGIC) {
462 LOG("invalid magic (got 0x%x)", magic);
463 errno = EINVAL;
464 return -1;
465 }
ths75818252008-07-03 13:41:03 +0000466}
bellard7a5ca862008-05-27 21:13:40 +0000467
ths75818252008-07-03 13:41:03 +0000468int nbd_receive_reply(int csock, struct nbd_reply *reply)
469{
470 uint8_t buf[4 + 4 + 8];
471 uint32_t magic;
472
473 memset(buf, 0xAA, sizeof(buf));
474
475 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
476 LOG("read failed");
bellard7a5ca862008-05-27 21:13:40 +0000477 errno = EINVAL;
478 return -1;
479 }
480
ths75818252008-07-03 13:41:03 +0000481 /* Reply
482 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
483 [ 4 .. 7] error (0 == no error)
484 [ 7 .. 15] handle
485 */
486
487 magic = be32_to_cpup((uint32_t*)buf);
488 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
489 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
490
491 TRACE("Got reply: "
492 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
493 magic, reply->error, reply->handle);
494
495 if (magic != NBD_REPLY_MAGIC) {
496 LOG("invalid magic (got 0x%x)", magic);
497 errno = EINVAL;
498 return -1;
499 }
500 return 0;
501}
502
503static int nbd_send_reply(int csock, struct nbd_reply *reply)
504{
505 uint8_t buf[4 + 4 + 8];
506
507 /* Reply
508 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
509 [ 4 .. 7] error (0 == no error)
510 [ 7 .. 15] handle
511 */
512 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
513 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
514 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
515
516 TRACE("Sending response to client");
517
518 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
519 LOG("writing to socket failed");
520 errno = EINVAL;
521 return -1;
522 }
523 return 0;
524}
525
526int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
527 off_t *offset, bool readonly, uint8_t *data, int data_size)
528{
529 struct nbd_request request;
530 struct nbd_reply reply;
531
532 TRACE("Reading request.");
533
534 if (nbd_receive_request(csock, &request) == -1)
535 return -1;
536
537 if (request.len > data_size) {
538 LOG("len (%u) is larger than max len (%u)",
539 request.len, data_size);
540 errno = EINVAL;
541 return -1;
542 }
543
544 if ((request.from + request.len) < request.from) {
bellard7a5ca862008-05-27 21:13:40 +0000545 LOG("integer overflow detected! "
546 "you're probably being attacked");
547 errno = EINVAL;
548 return -1;
549 }
550
ths75818252008-07-03 13:41:03 +0000551 if ((request.from + request.len) > size) {
bellard7a5ca862008-05-27 21:13:40 +0000552 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
553 ", Offset: %" PRIu64 "\n",
ths75818252008-07-03 13:41:03 +0000554 request.from, request.len, size, dev_offset);
bellard7a5ca862008-05-27 21:13:40 +0000555 LOG("requested operation past EOF--bad client?");
556 errno = EINVAL;
557 return -1;
558 }
559
bellard7a5ca862008-05-27 21:13:40 +0000560 TRACE("Decoding type");
561
ths75818252008-07-03 13:41:03 +0000562 reply.handle = request.handle;
563 reply.error = 0;
564
565 switch (request.type) {
566 case NBD_CMD_READ:
bellard7a5ca862008-05-27 21:13:40 +0000567 TRACE("Request type is READ");
568
ths75818252008-07-03 13:41:03 +0000569 if (bdrv_read(bs, (request.from + dev_offset) / 512, data,
570 request.len / 512) == -1) {
bellard7a5ca862008-05-27 21:13:40 +0000571 LOG("reading from file failed");
572 errno = EINVAL;
573 return -1;
574 }
ths75818252008-07-03 13:41:03 +0000575 *offset += request.len;
bellard7a5ca862008-05-27 21:13:40 +0000576
ths75818252008-07-03 13:41:03 +0000577 TRACE("Read %u byte(s)", request.len);
bellard7a5ca862008-05-27 21:13:40 +0000578
ths75818252008-07-03 13:41:03 +0000579 if (nbd_send_reply(csock, &reply) == -1)
bellard7a5ca862008-05-27 21:13:40 +0000580 return -1;
bellard7a5ca862008-05-27 21:13:40 +0000581
582 TRACE("Sending data to client");
583
ths75818252008-07-03 13:41:03 +0000584 if (write_sync(csock, data, request.len) != request.len) {
bellard7a5ca862008-05-27 21:13:40 +0000585 LOG("writing to socket failed");
586 errno = EINVAL;
587 return -1;
588 }
589 break;
ths75818252008-07-03 13:41:03 +0000590 case NBD_CMD_WRITE:
bellard7a5ca862008-05-27 21:13:40 +0000591 TRACE("Request type is WRITE");
592
ths75818252008-07-03 13:41:03 +0000593 TRACE("Reading %u byte(s)", request.len);
bellard7a5ca862008-05-27 21:13:40 +0000594
ths75818252008-07-03 13:41:03 +0000595 if (read_sync(csock, data, request.len) != request.len) {
bellard7a5ca862008-05-27 21:13:40 +0000596 LOG("reading from socket failed");
597 errno = EINVAL;
598 return -1;
599 }
600
601 if (readonly) {
602 TRACE("Server is read-only, return error");
ths75818252008-07-03 13:41:03 +0000603 reply.error = 1;
bellard7a5ca862008-05-27 21:13:40 +0000604 } else {
605 TRACE("Writing to device");
606
ths75818252008-07-03 13:41:03 +0000607 if (bdrv_write(bs, (request.from + dev_offset) / 512,
608 data, request.len / 512) == -1) {
bellard7a5ca862008-05-27 21:13:40 +0000609 LOG("writing to file failed");
610 errno = EINVAL;
611 return -1;
612 }
613
ths75818252008-07-03 13:41:03 +0000614 *offset += request.len;
bellard7a5ca862008-05-27 21:13:40 +0000615 }
616
ths75818252008-07-03 13:41:03 +0000617 if (nbd_send_reply(csock, &reply) == -1)
bellard7a5ca862008-05-27 21:13:40 +0000618 return -1;
bellard7a5ca862008-05-27 21:13:40 +0000619 break;
ths75818252008-07-03 13:41:03 +0000620 case NBD_CMD_DISC:
bellard7a5ca862008-05-27 21:13:40 +0000621 TRACE("Request type is DISCONNECT");
622 errno = 0;
623 return 1;
624 default:
ths75818252008-07-03 13:41:03 +0000625 LOG("invalid request type (%u) received", request.type);
bellard7a5ca862008-05-27 21:13:40 +0000626 errno = EINVAL;
627 return -1;
628 }
629
630 TRACE("Request/Reply complete");
631
632 return 0;
633}