Document all options supported by nbdtab
[nbd.git] / nbd-client.c
blobc92fe777ecc90d99641c79973c9bf3b01b1f7fb5
1 /*
2 * Open connection for network block device
4 * Copyright 1997,1998 Pavel Machek, distribute under GPL
5 * <pavel@atrey.karlin.mff.cuni.cz>
6 * Copyright (c) 2002 - 2011 Wouter Verhelst <w@uter.be>
8 * Version 1.0 - 64bit issues should be fixed, now
9 * Version 1.1 - added bs (blocksize) option (Alexey Guzeev, aga@permonline.ru)
10 * Version 1.2 - I added new option '-d' to send the disconnect request
11 * Version 2.0 - Version synchronised with server
12 * Version 2.1 - Check for disconnection before INIT_PASSWD is received
13 * to make errormsg a bit more helpful in case the server can't
14 * open the exported file.
15 * 16/03/2010 - Add IPv6 support.
16 * Kitt Tientanopajai <kitt@kitty.in.th>
17 * Neutron Soutmun <neo.neutron@gmail.com>
18 * Suriya Soutmun <darksolar@gmail.com>
21 #include "config.h"
22 #include "lfs.h"
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <netinet/tcp.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include "netdb-compat.h"
33 #include <inttypes.h>
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include <syslog.h>
37 #include <stdlib.h>
38 #include <sys/mount.h>
39 #include <sys/mman.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <getopt.h>
43 #include <stdarg.h>
44 #include <stdbool.h>
45 #include <time.h>
47 #include <linux/ioctl.h>
49 #if HAVE_NETLINK
50 #include "nbd-netlink.h"
51 #include <netlink/netlink.h>
52 #include <netlink/genl/genl.h>
53 #include <netlink/genl/ctrl.h>
54 #endif
56 #define MY_NAME "nbd_client"
57 #include "cliserv.h"
59 #if HAVE_GNUTLS && !defined(NOTLS)
60 #include "crypto-gnutls.h"
61 #endif
63 #ifdef WITH_SDP
64 #include <sdp_inet.h>
65 #endif
67 #define NBDC_DO_LIST 1
69 #if HAVE_NETLINK
70 static int callback(struct nl_msg *msg, void *arg) {
71 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
72 struct nlattr *msg_attr[NBD_ATTR_MAX + 1];
73 int ret;
74 uint32_t index;
76 ret = nla_parse(msg_attr, NBD_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
77 genlmsg_attrlen(gnlh, 0), NULL);
78 if (ret)
79 err("Invalid response from the kernel\n");
80 if (!msg_attr[NBD_ATTR_INDEX])
81 err("Did not receive index from the kernel\n");
82 index = nla_get_u32(msg_attr[NBD_ATTR_INDEX]);
83 printf("Connected /dev/nbd%d\n", (int)index);
84 return NL_OK;
87 static struct nl_sock *get_nbd_socket(int *driver_id) {
88 struct nl_sock *socket;
90 socket = nl_socket_alloc();
91 if (!socket)
92 err("Couldn't allocate netlink socket\n");
94 if (genl_connect(socket))
95 err("Couldn't connect to the generic netlink socket\n");
96 *driver_id = genl_ctrl_resolve(socket, "nbd");
97 if (*driver_id < 0)
98 err("Couldn't resolve the nbd netlink family, make sure the nbd module is loaded and your nbd driver supports the netlink interface.\n");
99 return socket;
102 static void netlink_configure(int index, int *sockfds, int num_connects,
103 u64 size64, int blocksize, uint16_t flags,
104 int timeout) {
105 struct nl_sock *socket;
106 struct nlattr *sock_attr;
107 struct nl_msg *msg;
108 int driver_id, i;
110 socket = get_nbd_socket(&driver_id);
111 nl_socket_modify_cb(socket, NL_CB_VALID, NL_CB_CUSTOM, callback, NULL);
113 msg = nlmsg_alloc();
114 if (!msg)
115 err("Couldn't allocate netlink message\n");
116 genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
117 NBD_CMD_CONNECT, 0);
118 if (index >= 0)
119 NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
120 NLA_PUT_U64(msg, NBD_ATTR_SIZE_BYTES, size64);
121 NLA_PUT_U64(msg, NBD_ATTR_BLOCK_SIZE_BYTES, blocksize);
122 NLA_PUT_U64(msg, NBD_ATTR_SERVER_FLAGS, flags);
123 NLA_PUT_U64(msg, NBD_ATTR_TIMEOUT, timeout);
125 sock_attr = nla_nest_start(msg, NBD_ATTR_SOCKETS);
126 if (!sock_attr)
127 err("Couldn't nest the sockets for our connection\n");
128 for (i = 0; i < num_connects; i++) {
129 struct nlattr *sock_opt;
130 sock_opt = nla_nest_start(msg, NBD_SOCK_ITEM);
131 if (!sock_opt)
132 err("Couldn't nest the sockets for our connection\n");
133 NLA_PUT_U32(msg, NBD_SOCK_FD, sockfds[i]);
134 nla_nest_end(msg, sock_opt);
136 nla_nest_end(msg, sock_attr);
138 if (nl_send_sync(socket, msg) < 0)
139 err("Failed to setup device, check dmesg\n");
140 return;
141 nla_put_failure:
142 err("Failed to create netlink message\n");
145 static void netlink_disconnect(char *nbddev) {
146 struct nl_sock *socket;
147 struct nl_msg *msg;
148 int driver_id;
150 int index = -1;
151 if (nbddev) {
152 if (sscanf(nbddev, "/dev/nbd%d", &index) != 1)
153 err("Invalid nbd device target\n");
155 if (index < 0)
156 err("Invalid nbd device target\n");
158 socket = get_nbd_socket(&driver_id);
160 msg = nlmsg_alloc();
161 if (!msg)
162 err("Couldn't allocate netlink message\n");
163 genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
164 NBD_CMD_DISCONNECT, 0);
165 NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
166 if (nl_send_sync(socket, msg) < 0)
167 err("Failed to disconnect device, check dmsg\n");
168 nl_socket_free(socket);
169 return;
170 nla_put_failure:
171 err("Failed to create netlink message\n");
173 #else
174 static void netlink_configure(int index, int *sockfds, int num_connects,
175 u64 size64, int blocksize, uint16_t flags,
176 int timeout)
180 static void netlink_disconnect(char *nbddev)
183 #endif /* HAVE_NETLINK */
185 int check_conn(char* devname, int do_print) {
186 char buf[256];
187 char* p;
188 int fd;
189 int len;
191 if( (p=strrchr(devname, '/')) ) {
192 devname=p+1;
194 if((p=strchr(devname, 'p'))) {
195 /* We can't do checks on partitions. */
196 *p='\0';
198 snprintf(buf, 256, "/sys/block/%s/pid", devname);
199 if((fd=open(buf, O_RDONLY))<0) {
200 if(errno==ENOENT) {
201 return 1;
202 } else {
203 return 2;
206 len=read(fd, buf, 256);
207 if(len < 0) {
208 perror("could not read from server");
209 close(fd);
210 return 2;
212 buf[(len < 256) ? len : 255]='\0';
213 if(do_print) printf("%s\n", buf);
214 close(fd);
215 return 0;
218 int opennet(char *name, char* portstr, int sdp) {
219 int sock;
220 struct addrinfo hints;
221 struct addrinfo *ai = NULL;
222 struct addrinfo *rp = NULL;
223 int e;
225 memset(&hints,'\0',sizeof(hints));
226 hints.ai_family = AF_UNSPEC;
227 hints.ai_socktype = SOCK_STREAM;
228 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
229 hints.ai_protocol = IPPROTO_TCP;
231 e = getaddrinfo(name, portstr, &hints, &ai);
233 if(e != 0) {
234 fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
235 freeaddrinfo(ai);
236 return -1;
239 if(sdp) {
240 #ifdef WITH_SDP
241 if (ai->ai_family == AF_INET)
242 ai->ai_family = AF_INET_SDP;
243 else (ai->ai_family == AF_INET6)
244 ai->ai_family = AF_INET6_SDP;
245 #else
246 err("Can't do SDP: I was not compiled with SDP support!");
247 #endif
250 for(rp = ai; rp != NULL; rp = rp->ai_next) {
251 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
253 if(sock == -1)
254 continue; /* error */
256 if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
257 break; /* success */
259 close(sock);
262 if (rp == NULL) {
263 err_nonfatal("Socket failed: %m");
264 sock = -1;
265 goto err;
268 setmysockopt(sock);
269 err:
270 freeaddrinfo(ai);
271 return sock;
274 int openunix(const char *path) {
275 int sock;
276 struct sockaddr_un un_addr;
277 memset(&un_addr, 0, sizeof(un_addr));
279 un_addr.sun_family = AF_UNIX;
280 if (strnlen(path, sizeof(un_addr.sun_path)) == sizeof(un_addr.sun_path)) {
281 err_nonfatal("UNIX socket path too long");
282 return -1;
285 strncpy(un_addr.sun_path, path, sizeof(un_addr.sun_path) - 1);
287 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
288 err_nonfatal("SOCKET failed");
289 return -1;
292 if (connect(sock, &un_addr, sizeof(un_addr)) == -1) {
293 err_nonfatal("CONNECT failed");
294 close(sock);
295 return -1;
297 return sock;
300 void send_request(int sock, uint32_t opt, ssize_t datasize, void* data) {
301 struct {
302 uint64_t magic;
303 uint32_t opt;
304 uint32_t datasize;
305 } __attribute__((packed)) header = {
306 ntohll(opts_magic),
307 ntohl(opt),
308 ntohl(datasize),
310 if(datasize < 0) {
311 datasize = strlen((char*)data);
312 header.datasize = htonl(datasize);
314 writeit(sock, &header, sizeof(header));
315 if(data != NULL) {
316 writeit(sock, data, datasize);
320 void send_info_request(int sock, uint32_t opt, int n_reqs, uint16_t* reqs, char* name) {
321 uint16_t rlen = htons(n_reqs);
322 uint32_t nlen = htonl(strlen(name));
324 send_request(sock, opt, sizeof(uint32_t) + strlen(name) + sizeof(uint16_t) + n_reqs * sizeof(uint16_t), NULL);
325 writeit(sock, &nlen, sizeof(nlen));
326 writeit(sock, name, strlen(name));
327 writeit(sock, &rlen, sizeof(rlen));
328 if(n_reqs > 0) {
329 writeit(sock, reqs, n_reqs * sizeof(uint16_t));
333 struct reply {
334 uint64_t magic;
335 uint32_t opt;
336 uint32_t reply_type;
337 uint32_t datasize;
338 char data[];
339 } __attribute__((packed));
341 struct reply* read_reply(int sock) {
342 struct reply *retval = malloc(sizeof(struct reply));
343 readit(sock, retval, sizeof(*retval));
344 retval->magic = ntohll(retval->magic);
345 retval->opt = ntohl(retval->opt);
346 retval->reply_type = ntohl(retval->reply_type);
347 retval->datasize = ntohl(retval->datasize);
348 if (retval->magic != rep_magic) {
349 fprintf(stderr, "E: received invalid negotiation magic %" PRIu64 " (expected %" PRIu64 ")", retval->magic, rep_magic);
350 exit(EXIT_FAILURE);
352 if (retval->datasize > 0) {
353 retval = realloc(retval, sizeof(struct reply) + retval->datasize);
354 readit(sock, &(retval->data), retval->datasize);
356 return retval;
359 void ask_list(int sock) {
360 uint32_t opt_server;
361 uint32_t len;
362 uint32_t lenn;
363 uint32_t reptype;
364 uint64_t magic;
365 int rlen;
366 const int BUF_SIZE = 1024;
367 char buf[BUF_SIZE];
369 send_request(sock, NBD_OPT_LIST, 0, NULL);
370 /* newline, move away from the "Negotiation:" line */
371 printf("\n");
372 do {
373 memset(buf, 0, 1024);
374 if(read(sock, &magic, sizeof(magic)) < 0) {
375 err("Reading magic from server: %m");
377 if(read(sock, &opt_server, sizeof(opt_server)) < 0) {
378 err("Reading option: %m");
380 if(read(sock, &reptype, sizeof(reptype)) <0) {
381 err("Reading reply from server: %m");
383 if(read(sock, &len, sizeof(len)) < 0) {
384 err("Reading length from server: %m");
386 magic=ntohll(magic);
387 len=ntohl(len);
388 reptype=ntohl(reptype);
389 if(magic != rep_magic) {
390 err("Not enough magic from server");
392 if(reptype & NBD_REP_FLAG_ERROR) {
393 switch(reptype) {
394 case NBD_REP_ERR_POLICY:
395 fprintf(stderr, "\nE: listing not allowed by server.\n");
396 break;
397 default:
398 fprintf(stderr, "\nE: unexpected error from server.\n");
399 break;
401 if(len > 0 && len < BUF_SIZE) {
402 if((rlen=read(sock, buf, len)) < 0) {
403 fprintf(stderr, "\nE: could not read error message from server\n");
404 } else {
405 buf[rlen] = '\0';
406 fprintf(stderr, "Server said: %s\n", buf);
409 exit(EXIT_FAILURE);
410 } else {
411 if(reptype != NBD_REP_ACK) {
412 if(reptype != NBD_REP_SERVER) {
413 err("Server sent us a reply we don't understand!");
415 if(read(sock, &lenn, sizeof(lenn)) < 0) {
416 fprintf(stderr, "\nE: could not read export name length from server\n");
417 exit(EXIT_FAILURE);
419 lenn=ntohl(lenn);
420 if (lenn >= BUF_SIZE) {
421 fprintf(stderr, "\nE: export name on server too long\n");
422 exit(EXIT_FAILURE);
424 if(read(sock, buf, lenn) < 0) {
425 fprintf(stderr, "\nE: could not read export name from server\n");
426 exit(EXIT_FAILURE);
428 buf[lenn] = 0;
429 printf("%s", buf);
430 len -= lenn;
431 len -= sizeof(lenn);
432 if(len > 0) {
433 if(read(sock, buf, len) < 0) {
434 fprintf(stderr, "\nE: could not read export description from server\n");
435 exit(EXIT_FAILURE);
437 buf[len] = 0;
438 printf(": %s\n", buf);
439 } else {
440 printf("\n");
444 } while(reptype != NBD_REP_ACK);
445 send_request(sock, NBD_OPT_ABORT, 0, NULL);
448 void parse_sizes(char *buf, uint64_t *size, uint16_t *flags) {
449 memcpy(size, buf, sizeof(*size));
450 *size = ntohll(*size);
451 buf += sizeof(*size);
452 memcpy(flags, buf, sizeof(*flags));
453 *flags = ntohs(*flags);
455 if ((*size>>12) > (uint64_t)~0UL) {
456 printf("size = %luMB", (unsigned long)(*size>>20));
457 err("Exported device is too big for me. Get 64-bit machine :-(\n");
458 } else {
459 printf("size = %luMB", (unsigned long)(*size>>20));
461 printf("\n");
464 void send_opt_exportname(int sock, u64 *rsize64, uint16_t *flags, bool can_opt_go, char* name, uint16_t global_flags) {
465 send_request(sock, NBD_OPT_EXPORT_NAME, -1, name);
466 char b[sizeof(*flags) + sizeof(*rsize64)];
467 if(readit(sock, b, sizeof(b)) < 0 && can_opt_go) {
468 err("E: server does not support NBD_OPT_GO and dropped connection after sending NBD_OPT_EXPORT_NAME. Try -g.");
470 parse_sizes(b, rsize64, flags);
471 if(!global_flags & NBD_FLAG_NO_ZEROES) {
472 char buf[125];
473 readit(sock, buf, 124);
477 void negotiate(int *sockp, u64 *rsize64, uint16_t *flags, char* name, uint32_t needed_flags, uint32_t client_flags, uint32_t do_opts, char *certfile, char *keyfile, char *cacertfile, char *tlshostname, bool tls, bool can_opt_go) {
478 u64 magic;
479 uint16_t tmp;
480 uint16_t global_flags;
481 char buf[256] = "\0\0\0\0\0\0\0\0\0";
482 int sock = *sockp;
484 printf("Negotiation: ");
485 readit(sock, buf, 8);
486 if (strcmp(buf, INIT_PASSWD))
487 err("INIT_PASSWD bad");
488 printf(".");
489 readit(sock, &magic, sizeof(magic));
490 magic = ntohll(magic);
491 if (magic != opts_magic) {
492 if(magic == cliserv_magic) {
493 err("It looks like you're trying to connect to an oldstyle server. This is no longer supported since nbd 3.10.");
496 printf(".");
497 readit(sock, &tmp, sizeof(uint16_t));
498 global_flags = ntohs(tmp);
499 if((needed_flags & global_flags) != needed_flags) {
500 /* There's currently really only one reason why this
501 * check could possibly fail, but we may need to change
502 * this error message in the future... */
503 fprintf(stderr, "\nE: Server does not support listing exports\n");
504 exit(EXIT_FAILURE);
507 if (global_flags & NBD_FLAG_NO_ZEROES) {
508 client_flags |= NBD_FLAG_C_NO_ZEROES;
510 client_flags = htonl(client_flags);
511 if (write(sock, &client_flags, sizeof(client_flags)) < 0)
512 err("Failed/2.1: %m");
514 #if HAVE_GNUTLS && !defined(NOTLS)
515 /* TLS */
516 if (tls) {
517 int plainfd[2]; // [0] is used by the proxy, [1] is used by NBD
518 tlssession_t *s = NULL;
519 int ret;
520 uint32_t tmp32;
521 uint64_t tmp64;
523 send_request(sock, NBD_OPT_STARTTLS, 0, NULL);
525 if (read(sock, &tmp64, sizeof(tmp64)) < 0)
526 err("Could not read cliserv_magic: %m");
527 tmp64 = ntohll(tmp64);
528 if (tmp64 != NBD_OPT_REPLY_MAGIC) {
529 err("reply magic does not match");
531 if (read(sock, &tmp32, sizeof(tmp32)) < 0)
532 err("Could not read option type: %m");
533 tmp32 = ntohl(tmp32);
534 if (tmp32 != NBD_OPT_STARTTLS)
535 err("Reply to wrong option");
536 if (read(sock, &tmp32, sizeof(tmp32)) < 0)
537 err("Could not read option reply type: %m");
538 tmp32 = ntohl(tmp32);
539 if (tmp32 != NBD_REP_ACK) {
540 err("Option reply type != NBD_REP_ACK");
542 if (read(sock, &tmp32, sizeof(tmp32)) < 0) err(
543 "Could not read option data length: %m");
544 tmp32 = ntohl(tmp32);
545 if (tmp32 != 0) {
546 err("Option reply data length != 0");
548 s = tlssession_new(0,
549 keyfile,
550 certfile,
551 cacertfile,
552 tlshostname,
553 !cacertfile || !tlshostname, // insecure flag
554 #ifdef DODBG
555 1, // debug
556 #else
557 0, // debug
558 #endif
559 NULL, // quitfn
560 NULL, // erroutfn
561 NULL // opaque
563 if (!s)
564 err("Cannot establish TLS session");
566 if (socketpair(AF_UNIX, SOCK_STREAM, 0, plainfd) < 0)
567 err("Cannot get socket pair");
569 if (set_nonblocking(plainfd[0], 0) <0 ||
570 set_nonblocking(plainfd[1], 0) <0 ||
571 set_nonblocking(sock, 0) <0) {
572 close(plainfd[0]);
573 close(plainfd[1]);
574 err("Cannot set socket options");
577 ret = fork();
578 if (ret < 0)
579 err("Could not fork");
580 else if (ret == 0) {
581 // we are the child
582 if (daemon(0, 0) < 0) {
583 /* no one will see this */
584 fprintf(stderr, "Can't detach from the terminal");
585 exit(1);
587 signal (SIGPIPE, SIG_IGN);
588 close(plainfd[1]);
589 tlssession_mainloop(sock, plainfd[0], s);
590 close(sock);
591 close(plainfd[0]);
592 exit(0);
594 close(plainfd[0]);
595 close(sock);
596 sock = plainfd[1]; /* use the decrypted FD from now on */
597 *sockp = sock;
599 #else
600 if (keyfile) {
601 err("TLS requested but support not compiled in");
603 #endif
605 if(do_opts & NBDC_DO_LIST) {
606 ask_list(sock);
607 exit(EXIT_SUCCESS);
610 struct reply *rep = NULL;
612 if(!can_opt_go) {
613 send_opt_exportname(sock, rsize64, flags, can_opt_go, name, global_flags);
614 return;
617 send_info_request(sock, NBD_OPT_GO, 0, NULL, name);
619 do {
620 if(rep != NULL) free(rep);
621 rep = read_reply(sock);
622 if(rep && (rep->reply_type & NBD_REP_FLAG_ERROR)) {
623 switch(rep->reply_type) {
624 case NBD_REP_ERR_UNSUP:
625 /* server doesn't support NBD_OPT_GO or NBD_OPT_INFO,
626 * fall back to NBD_OPT_EXPORT_NAME */
627 send_opt_exportname(sock, rsize64, flags, can_opt_go, name, global_flags);
628 free(rep);
629 return;
630 case NBD_REP_ERR_POLICY:
631 if(rep->datasize > 0) {
632 char errstr[1024];
633 snprintf(errstr, sizeof errstr, "Connection not allowed by server policy. Server said: %s", rep->data);
634 err(errstr);
635 } else {
636 err("Connection not allowed by server policy.");
638 free(rep);
639 exit(EXIT_FAILURE);
640 default:
641 if(rep->datasize > 0) {
642 char errstr[1024];
643 snprintf(errstr, sizeof errstr, "Unknown error returned by server. Server said: %s", rep->data);
644 err(errstr);
645 } else {
646 err("Unknown error returned by server.");
648 free(rep);
649 exit(EXIT_FAILURE);
652 uint16_t info_type;
653 switch(rep->reply_type) {
654 case NBD_REP_INFO:
655 memcpy(&info_type, rep->data, 2);
656 info_type = htons(info_type);
657 switch(info_type) {
658 case NBD_INFO_EXPORT:
659 parse_sizes(rep->data + 2, rsize64, flags);
660 break;
661 default:
662 // ignore these, don't need them
663 break;
665 break;
666 case NBD_REP_ACK:
667 break;
668 default:
669 err_nonfatal("Unknown reply to NBD_OPT_GO received, ignoring");
671 } while(rep->reply_type != NBD_REP_ACK);
672 free(rep);
675 bool get_from_config(char* cfgname, char** name_ptr, char** dev_ptr, char** hostn_ptr, int* bs, int* timeout, int* persist, int* swap, int* sdp, int* b_unix, char**port, int* num_conns, char **certfile, char **keyfile, char **cacertfile, char **tlshostname, bool can_opt_go) {
676 int fd = open(SYSCONFDIR "/nbdtab", O_RDONLY);
677 bool retval = false;
678 if(fd < 0) {
679 fprintf(stderr, "while opening %s: ", SYSCONFDIR "/nbdtab");
680 perror("could not open config file");
681 goto out;
683 off_t size = lseek(fd, 0, SEEK_END);
684 lseek(fd, 0, SEEK_SET);
685 void *data = NULL;
686 char *fsep = "\n\t# ";
687 char *lsep = "\n#";
689 if(size < 0) {
690 perror("E: mmap'ing nbdtab");
691 exit(EXIT_FAILURE);
694 data = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED, fd, 0);
695 if(!strncmp(cfgname, "/dev/", 5)) {
696 cfgname += 5;
698 char *loc = strstr((const char*)data, cfgname);
699 if(!loc) {
700 goto out;
702 size_t l = strlen(cfgname) + 6;
703 *dev_ptr = malloc(l);
704 snprintf(*dev_ptr, l, "/dev/%s", cfgname);
706 size_t line_len, field_len, ws_len;
707 #define CHECK_LEN field_len = strcspn(loc, fsep); ws_len = strspn(loc+field_len, fsep); if(field_len > line_len || line_len <= 0) { goto out; }
708 #define MOVE_NEXT line_len -= field_len + ws_len; loc += field_len + ws_len
709 // find length of line
710 line_len = strcspn(loc, lsep);
711 // first field is the device node name, which we already know, so skip it
712 CHECK_LEN;
713 MOVE_NEXT;
714 // next field is the hostname
715 CHECK_LEN;
716 *hostn_ptr = strndup(loc, field_len);
717 MOVE_NEXT;
718 // third field is the export name
719 CHECK_LEN;
720 *name_ptr = strndup(loc, field_len);
721 if(ws_len + field_len > line_len) {
722 // optional last field is not there, so return success
723 retval = true;
724 goto out;
726 MOVE_NEXT;
727 CHECK_LEN;
728 #undef CHECK_LEN
729 #undef MOVE_NEXT
730 // fourth field is the options field, a comma-separated field of options
731 do {
732 if(!strncmp(loc, "conns=", 6)) {
733 *num_conns = (int)strtol(loc+6, &loc, 0);
734 goto next;
736 if(!strncmp(loc, "bs=", 3)) {
737 *bs = (int)strtol(loc+3, &loc, 0);
738 goto next;
740 if(!strncmp(loc, "timeout=", 8)) {
741 *timeout = (int)strtol(loc+8, &loc, 0);
742 goto next;
744 if(!strncmp(loc, "port=", 5)) {
745 *port = strndup(loc+5, strcspn(loc+5, ","));
746 goto next;
748 if(!strncmp(loc, "persist", 7)) {
749 loc += 7;
750 *persist = 1;
751 goto next;
753 if(!strncmp(loc, "swap", 4)) {
754 *swap = 1;
755 loc += 4;
756 goto next;
758 if(!strncmp(loc, "sdp", 3)) {
759 *sdp = 1;
760 loc += 3;
761 goto next;
763 if(!strncmp(loc, "unix", 4)) {
764 *b_unix = 1;
765 loc += 4;
766 goto next;
768 if(!strncmp(loc, "certfile=", 9)) {
769 *certfile = strndup(loc+9, strcspn(loc+9, ","));
770 goto next;
772 if(!strncmp(loc, "keyfile=", 8)) {
773 *keyfile = strndup(loc+8, strcspn(loc+8, ","));
774 goto next;
776 if(!strncmp(loc, "cacertfile=", 11)) {
777 *cacertfile = strndup(loc+11, strcspn(loc+11, ","));
778 goto next;
780 if(!strncmp(loc, "tlshostname=", 9)) {
781 *tlshostname = strndup(loc+9, strcspn(loc+9, ","));
782 goto next;
784 if(!strncmp(loc, "no_optgo", 8)) {
785 *can_opt_go = false;
786 goto next;
788 // skip unknown options, with a warning unless they start with a '_'
789 l = strcspn(loc, ",");
790 if(*loc != '_') {
791 char* s = strndup(loc, l);
792 fprintf(stderr, "Warning: unknown option '%s' found in nbdtab file", s);
793 free(s);
795 loc += l;
796 next:
797 if(*loc == ',') {
798 loc++;
800 } while(strcspn(loc, lsep) > 0);
801 retval = true;
802 out:
803 if(data != NULL) {
804 munmap(data, size);
806 if(fd >= 0) {
807 close(fd);
809 return retval;
812 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
813 unsigned long size;
814 int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
816 if (size64>>12 > (uint64_t)~0UL)
817 err("Device too large.\n");
818 else {
819 int tmp_blocksize = 4096;
820 if (size64 / (u64)blocksize <= (uint64_t)~0UL)
821 tmp_blocksize = blocksize;
822 if (ioctl(nbd, NBD_SET_BLKSIZE, tmp_blocksize) < 0) {
823 fprintf(stderr, "Failed to set blocksize %d\n",
824 tmp_blocksize);
825 err("Ioctl/1.1a failed: %m\n");
827 size = (unsigned long)(size64 / (u64)tmp_blocksize);
828 if (ioctl(nbd, NBD_SET_SIZE_BLOCKS, size) < 0)
829 err("Ioctl/1.1b failed: %m\n");
830 if (tmp_blocksize != blocksize) {
831 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0) {
832 fprintf(stderr, "Failed to set blocksize %d\n",
833 blocksize);
834 err("Ioctl/1.1c failed: %m\n");
837 fprintf(stderr, "bs=%d, sz=%" PRIu64 " bytes\n", blocksize, (u64)tmp_blocksize * size);
840 ioctl(nbd, NBD_CLEAR_SOCK);
842 /* ignore error as kernel may not support */
843 ioctl(nbd, NBD_SET_FLAGS, (unsigned long) flags);
845 if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
846 err("Unable to set read-only attribute for device");
849 void set_timeout(int nbd, int timeout) {
850 if (timeout) {
851 if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
852 err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
853 fprintf(stderr, "timeout=%d\n", timeout);
857 void finish_sock(int sock, int nbd, int swap) {
858 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0) {
859 if (errno == EBUSY)
860 err("Kernel doesn't support multiple connections\n");
861 else
862 err("Ioctl NBD_SET_SOCK failed: %m\n");
865 #ifndef __ANDROID__
866 if (swap)
867 mlockall(MCL_CURRENT | MCL_FUTURE);
868 #endif
871 static int
872 oom_adjust(const char *file, const char *value)
874 int fd, rc;
875 size_t len;
877 fd = open(file, O_WRONLY);
878 if (fd < 0)
879 return -1;
880 len = strlen(value);
881 rc = write(fd, value, len) != (ssize_t) len;
882 close(fd);
883 return rc ? -1 : 0;
886 void usage(char* errmsg, ...) {
887 if(errmsg) {
888 char tmp[256];
889 va_list ap;
890 va_start(ap, errmsg);
891 snprintf(tmp, 256, "ERROR: %s\n\n", errmsg);
892 vfprintf(stderr, tmp, ap);
893 va_end(ap);
894 } else {
895 fprintf(stderr, "%s version %s\n", PROG_NAME, PACKAGE_VERSION);
897 #if HAVE_NETLINK
898 fprintf(stderr, "Usage: nbd-client -name|-N name host [port] nbd_device\n\t[-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S]\n\t[-persist|-p] [-nofork|-n] [-systemd-mark|-m] [-nonetlink|-L]\n");
899 #else
900 fprintf(stderr, "Usage: nbd-client -name|-N name host [port] nbd_device\n\t[-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S]\n\t[-persist|-p] [-nofork|-n] [-systemd-mark|-m]\n");
901 #endif
902 fprintf(stderr, "Or : nbd-client -u (with same arguments as above)\n");
903 fprintf(stderr, "Or : nbd-client nbdX\n");
904 fprintf(stderr, "Or : nbd-client -d nbd_device\n");
905 fprintf(stderr, "Or : nbd-client -c nbd_device\n");
906 fprintf(stderr, "Or : nbd-client -h|--help\n");
907 fprintf(stderr, "Or : nbd-client -l|--list host\n");
908 fprintf(stderr, "Or : nbd-client -V|--version\n");
909 #if HAVE_GNUTLS && !defined(NOTLS)
910 fprintf(stderr, "All commands that connect to a host also take:\n\t[-F|-certfile certfile] [-K|-keyfile keyfile]\n\t[-A|-cacertfile cacertfile] [-H|-tlshostname hostname] [-x|-enable-tls]\n");
911 #endif
912 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
913 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
914 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
915 fprintf(stderr, "blocksizes other than 1024 without patches\n");
916 fprintf(stderr, "Default value for port is 10809. Note that port must always be numeric\n");
917 fprintf(stderr, "Bug reports and general discussion should go to %s\n", PACKAGE_BUGREPORT);
920 void disconnect(char* device) {
921 int nbd = open(device, O_RDWR);
923 if (nbd < 0)
924 err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
925 printf("disconnect, ");
926 if (ioctl(nbd, NBD_DISCONNECT)<0)
927 err("Ioctl failed: %m\n");
928 printf("sock, ");
929 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
930 err("Ioctl failed: %m\n");
931 printf("done\n");
934 #if HAVE_NETLINK
935 static const char *short_opts = "-A:b:c:C:d:H:hK:LlnN:pSst:uVx";
936 #else
937 static const char *short_opts = "-A:b:c:C:d:gH:hK:lnN:pSst:uVx";
938 #endif
940 int main(int argc, char *argv[]) {
941 char* port=NBD_DEFAULT_PORT;
942 int sock, nbd;
943 int blocksize=1024;
944 char *hostname=NULL;
945 char *nbddev=NULL;
946 int swap=0;
947 int cont=0;
948 int timeout=0;
949 int sdp=0;
950 int G_GNUC_UNUSED nofork=0; // if -dNOFORK
951 pid_t main_pid;
952 u64 size64;
953 uint16_t flags = 0;
954 int c;
955 int nonspecial=0;
956 int b_unix=0;
957 char* name="";
958 uint16_t needed_flags=0;
959 uint32_t cflags=NBD_FLAG_C_FIXED_NEWSTYLE;
960 uint32_t opts=0;
961 sigset_t block, old;
962 char *certfile = NULL;
963 char *keyfile = NULL;
964 char *cacertfile = NULL;
965 char *tlshostname = NULL;
966 bool tls = false;
967 struct sigaction sa;
968 int num_connections = 1;
969 int netlink = HAVE_NETLINK;
970 int need_disconnect = 0;
971 int *sockfds;
972 struct option long_options[] = {
973 { "block-size", required_argument, NULL, 'b' },
974 { "check", required_argument, NULL, 'c' },
975 { "connections", required_argument, NULL, 'C'},
976 { "disconnect", required_argument, NULL, 'd' },
977 { "no-optgo", no_argument, NULL, 'g' },
978 { "help", no_argument, NULL, 'h' },
979 { "list", no_argument, NULL, 'l' },
980 { "name", required_argument, NULL, 'N' },
981 #if HAVE_NETLINK
982 { "nonetlink", no_argument, NULL, 'L' },
983 #endif
984 { "nofork", no_argument, NULL, 'n' },
985 { "persist", no_argument, NULL, 'p' },
986 { "sdp", no_argument, NULL, 'S' },
987 { "swap", no_argument, NULL, 's' },
988 { "systemd-mark", no_argument, NULL, 'm' },
989 { "timeout", required_argument, NULL, 't' },
990 { "unix", no_argument, NULL, 'u' },
991 { "certfile", required_argument, NULL, 'F' },
992 { "keyfile", required_argument, NULL, 'K' },
993 { "cacertfile", required_argument, NULL, 'A' },
994 { "tlshostname", required_argument, NULL, 'H' },
995 { "enable-tls", no_argument, NULL, 'x' },
996 { "version", no_argument, NULL, 'V' },
997 { 0, 0, 0, 0 },
999 int i;
1000 bool can_opt_go = true;
1002 logging(MY_NAME);
1004 #if HAVE_GNUTLS && !defined(NOTLS)
1005 tlssession_init();
1006 #endif
1008 while((c=getopt_long_only(argc, argv, short_opts, long_options, NULL))>=0) {
1009 switch(c) {
1010 case 1:
1011 // non-option argument
1012 if(strchr(optarg, '=')) {
1013 // old-style 'bs=' or 'timeout='
1014 // argument
1015 fprintf(stderr, "WARNING: old-style command-line argument encountered. This is deprecated.\n");
1016 if(!strncmp(optarg, "bs=", 3)) {
1017 optarg+=3;
1018 goto blocksize;
1020 if(!strncmp(optarg, "timeout=", 8)) {
1021 optarg+=8;
1022 goto timeout;
1024 usage("unknown option %s encountered", optarg);
1025 exit(EXIT_FAILURE);
1027 switch(nonspecial++) {
1028 case 0:
1029 // host
1030 hostname=optarg;
1031 break;
1032 case 1:
1033 // port
1034 if(!strtol(optarg, NULL, 0)) {
1035 // not parseable as a number, assume it's the device
1036 nbddev = optarg;
1037 nonspecial++;
1038 } else {
1039 port = optarg;
1041 break;
1042 case 2:
1043 // device
1044 nbddev = optarg;
1045 break;
1046 default:
1047 usage("too many non-option arguments specified");
1048 exit(EXIT_FAILURE);
1050 break;
1051 case 'b':
1052 blocksize:
1053 blocksize=(int)strtol(optarg, NULL, 0);
1054 break;
1055 case 'c':
1056 return check_conn(optarg, 1);
1057 case 'C':
1058 num_connections = (int)strtol(optarg, NULL, 0);
1059 break;
1060 case 'd':
1061 need_disconnect = 1;
1062 nbddev = strdup(optarg);
1063 break;
1064 case 'g':
1065 can_opt_go = false;
1066 break;
1067 case 'h':
1068 usage(NULL);
1069 exit(EXIT_SUCCESS);
1070 case 'l':
1071 needed_flags |= NBD_FLAG_FIXED_NEWSTYLE;
1072 opts |= NBDC_DO_LIST;
1073 nbddev="";
1074 break;
1075 #if HAVE_NETLINK
1076 case 'L':
1077 netlink = 0;
1078 break;
1079 #endif
1080 case 'm':
1081 argv[0][0] = '@';
1082 break;
1083 case 'n':
1084 nofork=1;
1085 break;
1086 case 'N':
1087 name=optarg;
1088 break;
1089 case 'p':
1090 cont=1;
1091 break;
1092 case 's':
1093 swap=1;
1094 break;
1095 case 'S':
1096 sdp=1;
1097 break;
1098 case 't':
1099 timeout:
1100 timeout=strtol(optarg, NULL, 0);
1101 break;
1102 case 'u':
1103 b_unix = 1;
1104 break;
1105 case 'V':
1106 printf("This is %s, from %s\n", PROG_NAME, PACKAGE_STRING);
1107 return 0;
1108 #if HAVE_GNUTLS && !defined(NOTLS)
1109 case 'x':
1110 tls = true;
1111 break;
1112 case 'F':
1113 certfile=strdup(optarg);
1114 break;
1115 case 'K':
1116 keyfile=strdup(optarg);
1117 break;
1118 case 'A':
1119 cacertfile=strdup(optarg);
1120 break;
1121 case 'H':
1122 tlshostname=strdup(optarg);
1123 break;
1124 #else
1125 case 'F':
1126 case 'K':
1127 case 'H':
1128 case 'A':
1129 fprintf(stderr, "E: TLS support not compiled in\n");
1130 exit(EXIT_FAILURE);
1131 #endif
1132 default:
1133 fprintf(stderr, "E: option eaten by 42 mice\n");
1134 exit(EXIT_FAILURE);
1138 if (need_disconnect) {
1139 if (netlink)
1140 netlink_disconnect(nbddev);
1141 else
1142 disconnect(nbddev);
1143 exit(EXIT_SUCCESS);
1145 #ifdef __ANDROID__
1146 if (swap)
1147 err("swap option unsupported on Android because mlockall is unsupported.");
1148 #endif
1149 if(hostname) {
1150 if((!name || !nbddev) && !(opts & NBDC_DO_LIST)) {
1151 if(!strncmp(hostname, "nbd", 3) || !strncmp(hostname, "/dev/nbd", 8)) {
1152 if(!get_from_config(hostname, &name, &nbddev, &hostname, &blocksize, &timeout, &cont, &swap, &sdp, &b_unix, &port, &num_connections, &certfile, &keyfile, &cacertfile, &hostname)) {
1153 usage("no valid configuration for specified device found", hostname);
1154 exit(EXIT_FAILURE);
1156 } else if (!netlink) {
1157 usage("not enough information specified, and argument didn't look like an nbd device");
1158 exit(EXIT_FAILURE);
1161 } else {
1162 usage("no information specified");
1163 exit(EXIT_FAILURE);
1166 if (keyfile && !certfile)
1167 certfile = strdup(keyfile);
1169 if (certfile != NULL || keyfile != NULL || cacertfile != NULL || tlshostname != NULL) {
1170 tls = true;
1173 if (!tlshostname && hostname)
1174 tlshostname = strdup(hostname);
1176 if (netlink)
1177 nofork = 1;
1179 if(strlen(name)==0 && !(opts & NBDC_DO_LIST)) {
1180 printf("Warning: the oldstyle protocol is no longer supported.\nThis method now uses the newstyle protocol with a default export\n");
1183 if(!(opts & NBDC_DO_LIST) && !netlink) {
1184 nbd = open(nbddev, O_RDWR);
1185 if (nbd < 0)
1186 err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
1189 if (netlink) {
1190 sockfds = malloc(sizeof(int) * num_connections);
1191 if (!sockfds)
1192 err("Cannot allocate the socket fd's array");
1195 for (i = 0; i < num_connections; i++) {
1196 if (b_unix)
1197 sock = openunix(hostname);
1198 else
1199 sock = opennet(hostname, port, sdp);
1200 if (sock < 0)
1201 exit(EXIT_FAILURE);
1203 negotiate(&sock, &size64, &flags, name, needed_flags, cflags, opts, certfile, keyfile, cacertfile, tlshostname, tls, can_opt_go);
1204 if (netlink) {
1205 sockfds[i] = sock;
1206 continue;
1209 if (i == 0) {
1210 setsizes(nbd, size64, blocksize, flags);
1211 set_timeout(nbd, timeout);
1213 finish_sock(sock, nbd, swap);
1214 if (swap) {
1215 if (keyfile)
1216 fprintf(stderr, "Warning: using swap and TLS is prone to deadlock\n");
1217 /* try linux >= 2.6.36 interface first */
1218 if (oom_adjust("/proc/self/oom_score_adj", "-1000")) {
1219 /* fall back to linux <= 2.6.35 interface */
1220 oom_adjust("/proc/self/oom_adj", "-17");
1225 if (netlink) {
1226 int index = -1;
1227 if (nbddev) {
1228 if (sscanf(nbddev, "/dev/nbd%d", &index) != 1)
1229 err("Invalid nbd device target\n");
1231 netlink_configure(index, sockfds, num_connections,
1232 size64, blocksize, flags, timeout);
1233 return 0;
1235 /* Go daemon */
1237 #ifndef NOFORK
1238 if(!nofork) {
1239 if (daemon(0,0) < 0)
1240 err("Cannot detach from terminal");
1243 memset(&sa, 0, sizeof(sa));
1244 sa.sa_handler = SIG_IGN;
1245 sigaction(SIGCHLD, &sa, NULL);
1246 #endif
1247 /* For child to check its parent */
1248 main_pid = getpid();
1249 do {
1250 #ifndef NOFORK
1252 sigfillset(&block);
1253 sigdelset(&block, SIGKILL);
1254 sigdelset(&block, SIGTERM);
1255 sigdelset(&block, SIGPIPE);
1256 sigprocmask(SIG_SETMASK, &block, &old);
1258 if (!fork()) {
1259 /* Due to a race, the kernel NBD driver cannot
1260 * call for a reread of the partition table
1261 * in the handling of the NBD_DO_IT ioctl().
1262 * Therefore, this is done in the first open()
1263 * of the device. We therefore make sure that
1264 * the device is opened at least once after the
1265 * connection was made. This has to be done in a
1266 * separate process, since the NBD_DO_IT ioctl()
1267 * does not return until the NBD device has
1268 * disconnected.
1270 struct timespec req = {
1271 .tv_sec = 0,
1272 .tv_nsec = 100000000,
1274 while(check_conn(nbddev, 0)) {
1275 if (main_pid != getppid()) {
1276 /* check_conn() will not return 0 when nbd disconnected
1277 * and parent exited during this loop. So the child has to
1278 * explicitly check parent identity and exit if parent
1279 * exited */
1280 exit(0);
1282 nanosleep(&req, NULL);
1284 if(open(nbddev, O_RDONLY) < 0) {
1285 perror("could not open device for updating partition table");
1287 exit(0);
1289 #endif
1291 if (ioctl(nbd, NBD_DO_IT) < 0) {
1292 int error = errno;
1293 fprintf(stderr, "nbd,%d: Kernel call returned: %s\n", main_pid, strerror(errno));
1294 if(error==EBADR) {
1295 /* The user probably did 'nbd-client -d' on us.
1296 * quit */
1297 cont=0;
1298 } else {
1299 if(cont) {
1300 u64 new_size;
1301 uint16_t new_flags;
1303 close(sock); close(nbd);
1304 for (;;) {
1305 fprintf(stderr, " Reconnecting\n");
1306 if (b_unix)
1307 sock = openunix(hostname);
1308 else
1309 sock = opennet(hostname, port, sdp);
1310 if (sock >= 0)
1311 break;
1312 sleep (1);
1314 nbd = open(nbddev, O_RDWR);
1315 if (nbd < 0)
1316 err("Cannot open NBD: %m");
1317 negotiate(&sock, &new_size, &new_flags, name, needed_flags, cflags, opts, certfile, keyfile, cacertfile, tlshostname, tls, can_opt_go);
1318 if (size64 != new_size) {
1319 err("Size of the device changed. Bye");
1321 setsizes(nbd, size64, blocksize,
1322 new_flags);
1324 set_timeout(nbd, timeout);
1325 finish_sock(sock,nbd,swap);
1328 } else {
1329 /* We're on 2.4. It's not clearly defined what exactly
1330 * happened at this point. Probably best to quit, now
1332 fprintf(stderr, "Kernel call returned.\n");
1333 cont=0;
1335 } while(cont);
1336 printf("sock, ");
1337 ioctl(nbd, NBD_CLEAR_SOCK);
1338 printf("done\n");
1339 return 0;