Convert most env fields to TCG registers
[qemu/mini2440.git] / nbd.c
blob9bebe4a9bb573651948bd8449f9913b42e23bc9d
1 /*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device
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
20 #include "nbd.h"
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #ifdef __sun__
26 #include <sys/ioccom.h>
27 #endif
28 #include <ctype.h>
29 #include <inttypes.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
37 #if defined(QEMU_NBD)
38 extern int verbose;
39 #else
40 static int verbose = 0;
41 #endif
43 #define TRACE(msg, ...) do { \
44 if (verbose) LOG(msg, ## __VA_ARGS__); \
45 } while(0)
47 #define LOG(msg, ...) do { \
48 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
49 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
50 } while(0)
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)
74 size_t offset = 0;
76 while (offset < size) {
77 ssize_t len;
79 if (do_read) {
80 len = read(fd, buffer + offset, size - offset);
81 } else {
82 len = write(fd, buffer + offset, size - offset);
85 /* recoverable error */
86 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
87 continue;
90 /* eof */
91 if (len == 0) {
92 break;
95 /* unrecoverable error */
96 if (len == -1) {
97 return 0;
100 offset += len;
103 return offset;
106 int tcp_socket_outgoing(const char *address, uint16_t port)
108 int s;
109 struct in_addr in;
110 struct sockaddr_in addr;
111 int serrno;
113 s = socket(PF_INET, SOCK_STREAM, 0);
114 if (s == -1) {
115 return -1;
118 if (inet_aton(address, &in) == 0) {
119 struct hostent *ent;
121 ent = gethostbyname(address);
122 if (ent == NULL) {
123 goto error;
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) {
134 goto error;
137 return s;
138 error:
139 serrno = errno;
140 close(s);
141 errno = serrno;
142 return -1;
145 int tcp_socket_incoming(const char *address, uint16_t port)
147 int s;
148 struct in_addr in;
149 struct sockaddr_in addr;
150 int serrno;
151 int opt;
153 s = socket(PF_INET, SOCK_STREAM, 0);
154 if (s == -1) {
155 return -1;
158 if (inet_aton(address, &in) == 0) {
159 struct hostent *ent;
161 ent = gethostbyname(address);
162 if (ent == NULL) {
163 goto error;
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));
173 opt = 1;
174 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
175 goto error;
178 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
179 goto error;
182 if (listen(s, 128) == -1) {
183 goto error;
186 return s;
187 error:
188 serrno = errno;
189 close(s);
190 errno = serrno;
191 return -1;
194 int unix_socket_incoming(const char *path)
196 int s;
197 struct sockaddr_un addr;
198 int serrno;
200 s = socket(PF_UNIX, SOCK_STREAM, 0);
201 if (s == -1) {
202 return -1;
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) {
210 goto error;
213 if (listen(s, 128) == -1) {
214 goto error;
217 return s;
218 error:
219 serrno = errno;
220 close(s);
221 errno = serrno;
222 return -1;
225 int unix_socket_outgoing(const char *path)
227 int s;
228 struct sockaddr_un addr;
229 int serrno;
231 s = socket(PF_UNIX, SOCK_STREAM, 0);
232 if (s == -1) {
233 return -1;
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) {
241 goto error;
244 return s;
245 error:
246 serrno = errno;
247 close(s);
248 errno = serrno;
249 return -1;
253 /* Basic flow
255 Server Client
257 Negotiate
258 Request
259 Response
260 Request
261 Response
264 Request (type == 2)
267 int nbd_negotiate(BlockDriverState *bs, int csock, off_t size)
269 char buf[8 + 8 + 8 + 128];
271 /* Negotiate
272 [ 0 .. 7] passwd ("NBDMAGIC")
273 [ 8 .. 15] magic (0x00420281861253)
274 [16 .. 23] size
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)) {
285 LOG("write failed");
286 errno = EINVAL;
287 return -1;
290 TRACE("Negotation succeeded.");
292 return 0;
295 int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
297 char buf[8 + 8 + 8 + 128];
298 uint64_t magic;
300 TRACE("Receiving negotation.");
302 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
303 LOG("read failed");
304 errno = EINVAL;
305 return -1;
308 magic = be64_to_cpup((uint64_t*)(buf + 8));
309 *size = be64_to_cpup((uint64_t*)(buf + 16));
310 *blocksize = 1024;
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");
326 errno = EINVAL;
327 return -1;
330 TRACE("Checking magic");
332 if (magic != 0x00420281861253LL) {
333 LOG("Bad magic received");
334 errno = EINVAL;
335 return -1;
337 return 0;
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) {
345 int serrno = errno;
346 LOG("Failed setting NBD block size");
347 errno = serrno;
348 return -1;
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) {
355 int serrno = errno;
356 LOG("Failed setting size (in blocks)");
357 errno = serrno;
358 return -1;
361 TRACE("Clearing NBD socket");
363 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
364 int serrno = errno;
365 LOG("Failed clearing NBD socket");
366 errno = serrno;
367 return -1;
370 TRACE("Setting NBD socket");
372 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
373 int serrno = errno;
374 LOG("Failed to set NBD socket");
375 errno = serrno;
376 return -1;
379 TRACE("Negotiation ended");
381 return 0;
384 int nbd_disconnect(int fd)
386 ioctl(fd, NBD_CLEAR_QUE);
387 ioctl(fd, NBD_DISCONNECT);
388 ioctl(fd, NBD_CLEAR_SOCK);
389 return 0;
392 int nbd_client(int fd, int csock)
394 int ret;
395 int serrno;
397 TRACE("Doing NBD loop");
399 ret = ioctl(fd, NBD_DO_IT);
400 serrno = errno;
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);
410 errno = serrno;
411 return ret;
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");
428 errno = EINVAL;
429 return -1;
431 return 0;
435 static int nbd_receive_request(int csock, struct nbd_request *request)
437 uint8_t buf[4 + 4 + 8 + 8 + 4];
438 uint32_t magic;
440 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
441 LOG("read failed");
442 errno = EINVAL;
443 return -1;
446 /* Request
447 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
448 [ 4 .. 7] type (0 == READ, 1 == WRITE)
449 [ 8 .. 15] handle
450 [16 .. 23] from
451 [24 .. 27] len
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);
466 errno = EINVAL;
467 return -1;
469 return 0;
472 int nbd_receive_reply(int csock, struct nbd_reply *reply)
474 uint8_t buf[4 + 4 + 8];
475 uint32_t magic;
477 memset(buf, 0xAA, sizeof(buf));
479 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
480 LOG("read failed");
481 errno = EINVAL;
482 return -1;
485 /* Reply
486 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
487 [ 4 .. 7] error (0 == no error)
488 [ 7 .. 15] handle
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));
495 TRACE("Got reply: "
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);
501 errno = EINVAL;
502 return -1;
504 return 0;
507 static int nbd_send_reply(int csock, struct nbd_reply *reply)
509 uint8_t buf[4 + 4 + 8];
511 /* Reply
512 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
513 [ 4 .. 7] error (0 == no error)
514 [ 7 .. 15] handle
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");
524 errno = EINVAL;
525 return -1;
527 return 0;
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)
539 return -1;
541 if (request.len > data_size) {
542 LOG("len (%u) is larger than max len (%u)",
543 request.len, data_size);
544 errno = EINVAL;
545 return -1;
548 if ((request.from + request.len) < request.from) {
549 LOG("integer overflow detected! "
550 "you're probably being attacked");
551 errno = EINVAL;
552 return -1;
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?");
560 errno = EINVAL;
561 return -1;
564 TRACE("Decoding type");
566 reply.handle = request.handle;
567 reply.error = 0;
569 switch (request.type) {
570 case NBD_CMD_READ:
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");
576 errno = EINVAL;
577 return -1;
579 *offset += request.len;
581 TRACE("Read %u byte(s)", request.len);
583 if (nbd_send_reply(csock, &reply) == -1)
584 return -1;
586 TRACE("Sending data to client");
588 if (write_sync(csock, data, request.len) != request.len) {
589 LOG("writing to socket failed");
590 errno = EINVAL;
591 return -1;
593 break;
594 case NBD_CMD_WRITE:
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");
601 errno = EINVAL;
602 return -1;
605 if (readonly) {
606 TRACE("Server is read-only, return error");
607 reply.error = 1;
608 } else {
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");
614 errno = EINVAL;
615 return -1;
618 *offset += request.len;
621 if (nbd_send_reply(csock, &reply) == -1)
622 return -1;
623 break;
624 case NBD_CMD_DISC:
625 TRACE("Request type is DISCONNECT");
626 errno = 0;
627 return 1;
628 default:
629 LOG("invalid request type (%u) received", request.type);
630 errno = EINVAL;
631 return -1;
634 TRACE("Request/Reply complete");
636 return 0;