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, see <http://www.gnu.org/licenses/>.
24 #include <sys/ioctl.h>
27 #include <sys/ioccom.h>
32 #include "qemu_socket.h"
37 #define TRACE(msg, ...) do { \
38 LOG(msg, ## __VA_ARGS__); \
41 #define TRACE(msg, ...) \
45 #define LOG(msg, ...) do { \
46 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
47 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
50 /* This is all part of the "official" NBD API */
52 #define NBD_REQUEST_MAGIC 0x25609513
53 #define NBD_REPLY_MAGIC 0x67446698
55 #define NBD_SET_SOCK _IO(0xab, 0)
56 #define NBD_SET_BLKSIZE _IO(0xab, 1)
57 #define NBD_SET_SIZE _IO(0xab, 2)
58 #define NBD_DO_IT _IO(0xab, 3)
59 #define NBD_CLEAR_SOCK _IO(0xab, 4)
60 #define NBD_CLEAR_QUE _IO(0xab, 5)
61 #define NBD_PRINT_DEBUG _IO(0xab, 6)
62 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
63 #define NBD_DISCONNECT _IO(0xab, 8)
65 /* That's all folks */
67 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
68 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
70 size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
74 while (offset < size) {
78 len = recv(fd, buffer + offset, size - offset, 0);
80 len = send(fd, buffer + offset, size - offset, 0);
84 errno = socket_error();
86 /* recoverable error */
87 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
96 /* unrecoverable error */
107 int tcp_socket_outgoing(const char *address, uint16_t port)
111 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) {
143 int tcp_socket_incoming(const char *address, uint16_t port)
147 struct sockaddr_in addr;
150 s = socket(PF_INET, SOCK_STREAM, 0);
155 if (inet_aton(address, &in) == 0) {
158 ent = gethostbyname(address);
163 memcpy(&in, ent->h_addr, sizeof(in));
166 addr.sin_family = AF_INET;
167 addr.sin_port = htons(port);
168 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
171 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
172 (const void *) &opt, sizeof(opt)) == -1) {
176 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
180 if (listen(s, 128) == -1) {
191 int unix_socket_incoming(const char *path)
194 struct sockaddr_un addr;
196 s = socket(PF_UNIX, SOCK_STREAM, 0);
201 memset(&addr, 0, sizeof(addr));
202 addr.sun_family = AF_UNIX;
203 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
205 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
209 if (listen(s, 128) == -1) {
219 int unix_socket_outgoing(const char *path)
222 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) {
243 int unix_socket_incoming(const char *path)
249 int unix_socket_outgoing(const char *path)
271 int nbd_negotiate(int csock, off_t size)
273 char buf[8 + 8 + 8 + 128];
276 [ 0 .. 7] passwd ("NBDMAGIC")
277 [ 8 .. 15] magic (0x00420281861253)
279 [24 .. 151] reserved (0)
282 TRACE("Beginning negotiation.");
283 memcpy(buf, "NBDMAGIC", 8);
284 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
285 cpu_to_be64w((uint64_t*)(buf + 16), size);
286 memset(buf + 24, 0, 128);
288 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
294 TRACE("Negotation succeeded.");
299 int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
301 char buf[8 + 8 + 8 + 128];
304 TRACE("Receiving negotation.");
306 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
312 magic = be64_to_cpup((uint64_t*)(buf + 8));
313 *size = be64_to_cpup((uint64_t*)(buf + 16));
316 TRACE("Magic is %c%c%c%c%c%c%c%c",
317 qemu_isprint(buf[0]) ? buf[0] : '.',
318 qemu_isprint(buf[1]) ? buf[1] : '.',
319 qemu_isprint(buf[2]) ? buf[2] : '.',
320 qemu_isprint(buf[3]) ? buf[3] : '.',
321 qemu_isprint(buf[4]) ? buf[4] : '.',
322 qemu_isprint(buf[5]) ? buf[5] : '.',
323 qemu_isprint(buf[6]) ? buf[6] : '.',
324 qemu_isprint(buf[7]) ? buf[7] : '.');
325 TRACE("Magic is 0x%" PRIx64, magic);
326 TRACE("Size is %" PRIu64, *size);
328 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
329 LOG("Invalid magic received");
334 TRACE("Checking magic");
336 if (magic != 0x00420281861253LL) {
337 LOG("Bad magic received");
345 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
347 TRACE("Setting block size to %lu", (unsigned long)blocksize);
349 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
351 LOG("Failed setting NBD block size");
356 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
358 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
360 LOG("Failed setting size (in blocks)");
365 TRACE("Clearing NBD socket");
367 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
369 LOG("Failed clearing NBD socket");
374 TRACE("Setting NBD socket");
376 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
378 LOG("Failed to set NBD socket");
383 TRACE("Negotiation ended");
388 int nbd_disconnect(int fd)
390 ioctl(fd, NBD_CLEAR_QUE);
391 ioctl(fd, NBD_DISCONNECT);
392 ioctl(fd, NBD_CLEAR_SOCK);
396 int nbd_client(int fd, int csock)
401 TRACE("Doing NBD loop");
403 ret = ioctl(fd, NBD_DO_IT);
406 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
408 TRACE("Clearing NBD queue");
409 ioctl(fd, NBD_CLEAR_QUE);
411 TRACE("Clearing NBD socket");
412 ioctl(fd, NBD_CLEAR_SOCK);
418 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
424 int nbd_disconnect(int fd)
430 int nbd_client(int fd, int csock)
437 int nbd_send_request(int csock, struct nbd_request *request)
439 uint8_t buf[4 + 4 + 8 + 8 + 4];
441 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
442 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
443 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
444 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
445 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
447 TRACE("Sending request to client");
449 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
450 LOG("writing to socket failed");
458 static int nbd_receive_request(int csock, struct nbd_request *request)
460 uint8_t buf[4 + 4 + 8 + 8 + 4];
463 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
470 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
471 [ 4 .. 7] type (0 == READ, 1 == WRITE)
477 magic = be32_to_cpup((uint32_t*)buf);
478 request->type = be32_to_cpup((uint32_t*)(buf + 4));
479 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
480 request->from = be64_to_cpup((uint64_t*)(buf + 16));
481 request->len = be32_to_cpup((uint32_t*)(buf + 24));
483 TRACE("Got request: "
484 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
485 magic, request->type, request->from, request->len);
487 if (magic != NBD_REQUEST_MAGIC) {
488 LOG("invalid magic (got 0x%x)", magic);
495 int nbd_receive_reply(int csock, struct nbd_reply *reply)
497 uint8_t buf[4 + 4 + 8];
500 memset(buf, 0xAA, sizeof(buf));
502 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
509 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
510 [ 4 .. 7] error (0 == no error)
514 magic = be32_to_cpup((uint32_t*)buf);
515 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
516 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
519 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
520 magic, reply->error, reply->handle);
522 if (magic != NBD_REPLY_MAGIC) {
523 LOG("invalid magic (got 0x%x)", magic);
530 static int nbd_send_reply(int csock, struct nbd_reply *reply)
532 uint8_t buf[4 + 4 + 8];
535 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
536 [ 4 .. 7] error (0 == no error)
539 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
540 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
541 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
543 TRACE("Sending response to client");
545 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
546 LOG("writing to socket failed");
553 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
554 off_t *offset, bool readonly, uint8_t *data, int data_size)
556 struct nbd_request request;
557 struct nbd_reply reply;
559 TRACE("Reading request.");
561 if (nbd_receive_request(csock, &request) == -1)
564 if (request.len > data_size) {
565 LOG("len (%u) is larger than max len (%u)",
566 request.len, data_size);
571 if ((request.from + request.len) < request.from) {
572 LOG("integer overflow detected! "
573 "you're probably being attacked");
578 if ((request.from + request.len) > size) {
579 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
580 ", Offset: %" PRIu64 "\n",
581 request.from, request.len, (uint64_t)size, dev_offset);
582 LOG("requested operation past EOF--bad client?");
587 TRACE("Decoding type");
589 reply.handle = request.handle;
592 switch (request.type) {
594 TRACE("Request type is READ");
596 if (bdrv_read(bs, (request.from + dev_offset) / 512, data,
597 request.len / 512) == -1) {
598 LOG("reading from file failed");
602 *offset += request.len;
604 TRACE("Read %u byte(s)", request.len);
606 if (nbd_send_reply(csock, &reply) == -1)
609 TRACE("Sending data to client");
611 if (write_sync(csock, data, request.len) != request.len) {
612 LOG("writing to socket failed");
618 TRACE("Request type is WRITE");
620 TRACE("Reading %u byte(s)", request.len);
622 if (read_sync(csock, data, request.len) != request.len) {
623 LOG("reading from socket failed");
629 TRACE("Server is read-only, return error");
632 TRACE("Writing to device");
634 if (bdrv_write(bs, (request.from + dev_offset) / 512,
635 data, request.len / 512) == -1) {
636 LOG("writing to file failed");
641 *offset += request.len;
644 if (nbd_send_reply(csock, &reply) == -1)
648 TRACE("Request type is DISCONNECT");
652 LOG("invalid request type (%u) received", request.type);
657 TRACE("Request/Reply complete");