-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbd-client.c
1303 lines (1034 loc) · 28.4 KB
/
nbd-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sys/capsicum.h>
#include <sys/endian.h>
#include <sys/socket.h>
#include <sys/syslimits.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "check.h"
#include "nbd-client.h"
#include "nbd-protocol.h"
enum {
NBD_CLIENT_TIMEOUT = 8,
NBD_REPLY_QUEUE_TIMEOUT = 1,
};
struct nbd_client {
int sock;
_Atomic(bool) disconnect;
uint32_t flags;
uint64_t size;
};
struct nbd_client *
nbd_client_alloc()
{
struct nbd_client *client;
client = (struct nbd_client *)malloc(sizeof (struct nbd_client));
if (client == NULL) {
assert(errno == ENOMEM);
syslog(LOG_ERR, "%s: failed to allocate nbd client: %m",
__func__);
}
return client;
}
void
nbd_client_free(struct nbd_client *client)
{
free(client);
}
int
nbd_client_init(struct nbd_client *client)
{
struct timeval tv;
int sock;
int on;
on = 1;
memset(client, 0, sizeof *client);
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == FAILURE) {
syslog(LOG_ERR, "%s: failed to create socket: %m", __func__);
return FAILURE;
}
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on)
== FAILURE) {
syslog(LOG_ERR,
"%s: failed to set socket option TCP_NODELAY: %m",
__func__);
return FAILURE;
}
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof on)
== FAILURE) {
syslog(LOG_ERR,
"%s: failed to set socket option SO_KEEPALIVE: %m",
__func__);
return FAILURE;
}
/*
tv.tv_sec = NBD_CLIENT_TIMEOUT;
tv.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv)
== FAILURE) {
syslog(LOG_ERR,
"%s: failed to set socket option SO_SNDTIMEO: %m",
__func__);
return FAILURE;
}
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv)
== FAILURE) {
syslog(LOG_ERR,
"%s: failed to set socket option SO_RCVTIMEO: %m",
__func__);
return FAILURE;
}
*/
client->sock = sock;
return SUCCESS;
}
void
nbd_client_close(struct nbd_client *client)
{
close(client->sock);
}
int
nbd_client_rights_limit(struct nbd_client *client)
{
cap_rights_t rights;
cap_rights_init(&rights, CAP_SEND, CAP_RECV, CAP_SHUTDOWN);
if (cap_rights_limit(client->sock, &rights) == FAILURE) {
syslog(LOG_ERR, "%s: failed to limit capabilities: %m",
__func__);
return FAILURE;
}
return SUCCESS;
}
uint64_t
nbd_client_get_size(struct nbd_client *client)
{
return client->size;
}
bool
nbd_client_get_disconnect(struct nbd_client *client)
{
return client->disconnect;
}
void
nbd_client_set_disconnect(struct nbd_client *client, bool disconnect)
{
client->disconnect = disconnect;
}
void
nbd_client_disable_trim(struct nbd_client *client)
{
client->flags &= ~NBD_FLAG_SEND_TRIM;
}
int
nbd_client_connect(struct nbd_client *client, struct addrinfo *ai)
{
int sock;
sock = client->sock;
if (connect(sock, ai->ai_addr, sizeof *ai->ai_addr) == FAILURE) {
syslog(LOG_ERR,
"%s: failed to connect to remote server (%s): %m",
__func__, ai->ai_canonname);
return FAILURE;
}
return SUCCESS;
}
void
nbd_client_shutdown(struct nbd_client *client)
{
shutdown(client->sock, SHUT_RDWR);
}
static inline char const *
nbd_client_flag_string(uint32_t flag)
{
switch (flag) {
#define CASE_MESSAGE(c) case c: return #c
CASE_MESSAGE(NBD_FLAG_HAS_FLAGS);
CASE_MESSAGE(NBD_FLAG_READ_ONLY);
CASE_MESSAGE(NBD_FLAG_SEND_FLUSH);
CASE_MESSAGE(NBD_FLAG_SEND_FUA);
CASE_MESSAGE(NBD_FLAG_ROTATIONAL);
CASE_MESSAGE(NBD_FLAG_SEND_TRIM);
#undef CASE_MESSAGE
default: return NULL;
}
}
static inline void
nbd_client_dump(struct nbd_client *client)
{
const uint32_t Flags[] = {
NBD_FLAG_HAS_FLAGS, NBD_FLAG_READ_ONLY, NBD_FLAG_SEND_FLUSH,
NBD_FLAG_SEND_FUA, NBD_FLAG_ROTATIONAL, NBD_FLAG_SEND_TRIM,
};
const size_t FlagsLen = sizeof Flags / sizeof Flags[0];
char flag_string[128], *curflag;
uint32_t flags;
size_t i, len;
syslog(LOG_DEBUG, "\tsock: %d", client->sock);
syslog(LOG_DEBUG, "\tdisconnect: %s",
client->disconnect ? "true" : "false");
flags = client->flags;
curflag = flag_string;
len = sizeof flag_string;
i = FlagsLen;
while (len && i--) {
char const *name;
size_t namelen, t;
uint32_t value;
bool match, last;
value = Flags[i];
name = nbd_client_flag_string(value);
namelen = strlen(name);
match = (flags & value) != 0;
last = i > 0;
assert(name != NULL);
snprintf(curflag, len, "%s%s", match ? name : "",
last ? (match ? "|" : "") : "");
t = namelen + (match ? 1 : 0); // doesn't matter on last
curflag += t;
len -= t;
}
syslog(LOG_DEBUG, "\tflags: %#010x [%s]", flags, flag_string);
syslog(LOG_DEBUG, "\tsize: %lu", client->size);
}
static inline void
nbd_oldstyle_negotiation_ntoh(struct nbd_oldstyle_negotiation *handshake)
{
handshake->magic = be64toh(handshake->magic);
handshake->oldstyle_magic = be64toh(handshake->oldstyle_magic);
handshake->size = be64toh(handshake->size);
handshake->flags = be32toh(handshake->flags);
}
static inline bool
nbd_oldstyle_negotiation_is_valid(struct nbd_oldstyle_negotiation *handshake)
{
if (handshake->magic != NBD_MAGIC) {
syslog(LOG_ERR,
"%s: invalid magic: %#018lx (expected %#018lx)",
__func__, handshake->magic, NBD_MAGIC);
return false;
}
if (handshake->oldstyle_magic != NBD_OLDSTYLE_MAGIC) {
syslog(LOG_ERR,
"%s: invalid oldstyle magic: %#018lx (expected %#018lx)",
__func__, handshake->oldstyle_magic, NBD_OLDSTYLE_MAGIC);
return false;
}
if (!(handshake->flags & NBD_FLAG_HAS_FLAGS)) {
syslog(LOG_ERR,
"%s: invalid flags: %#010x (expected low bit set)",
__func__, handshake->flags);
return false;
}
return true;
}
static inline void
nbd_oldstyle_negotiation_dump(struct nbd_oldstyle_negotiation *handshake)
{
uint32_t flags = handshake->flags;
syslog(LOG_DEBUG, "\tmagic: %#018lx", handshake->magic);
syslog(LOG_DEBUG, "\toldstyle_magic: %#018lx",
handshake->oldstyle_magic);
syslog(LOG_DEBUG, "\tsize: %lu", handshake->size);
syslog(LOG_DEBUG, "\tflags: %#010x%s", flags,
(flags & NBD_FLAG_HAS_FLAGS) ? "" : " (invalid)");
}
static int
nbd_client_oldstyle_handshake(struct nbd_client *client)
{
struct nbd_oldstyle_negotiation handshake;
ssize_t len;
int sock;
sock = client->sock;
while (true) {
len = recv(sock, &handshake, sizeof handshake, MSG_WAITALL);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len != sizeof handshake) {
syslog(LOG_ERR, "%s: connection failed: %m", __func__);
return FAILURE;
}
break;
}
nbd_oldstyle_negotiation_ntoh(&handshake);
//syslog(LOG_DEBUG, "%s: negotiation", __func__);
//nbd_oldstyle_negotiation_dump(&handshake);
if (!nbd_oldstyle_negotiation_is_valid(&handshake)) {
syslog(LOG_ERR, "%s: invalid handshake", __func__);
nbd_oldstyle_negotiation_dump(&handshake);
return FAILURE;
}
client->size = handshake.size;
client->flags = handshake.flags;
//syslog(LOG_DEBUG, "%s: client", __func__);
//nbd_client_dump(client);
if (!(handshake.flags & NBD_FLAG_SEND_FLUSH))
syslog(LOG_INFO,
"%s: server does not support FLUSH command", __func__);
if (!(handshake.flags & NBD_FLAG_SEND_TRIM))
syslog(LOG_INFO,
"%s: server does not support TRIM command", __func__);
return SUCCESS;
}
static inline void
nbd_newstyle_negotiation_ntoh(struct nbd_newstyle_negotiation *handshake)
{
handshake->magic = be64toh(handshake->magic);
handshake->newstyle_magic = be64toh(handshake->newstyle_magic);
handshake->handshake_flags = be16toh(handshake->handshake_flags);
}
#define VALID_NEWSTYLE_FLAGS (NBD_FLAG_FIXED_NEWSTYLE|NBD_FLAG_NO_ZEROES)
static inline bool
nbd_newstyle_negotiation_is_valid(struct nbd_newstyle_negotiation *handshake)
{
uint16_t flags = handshake->handshake_flags;
if (handshake->magic != NBD_MAGIC) {
syslog(LOG_ERR,
"%s: invalid magic: %#018lx (expected %#018lx)",
__func__, handshake->magic, NBD_MAGIC);
return false;
}
if (handshake->newstyle_magic != NBD_NEWSTYLE_MAGIC) {
syslog(LOG_ERR,
"%s: invalid newstyle magic: %#018lx (expected %#018lx)",
__func__, handshake->newstyle_magic, NBD_NEWSTYLE_MAGIC);
return false;
}
if (flags & ~VALID_NEWSTYLE_FLAGS)
syslog(LOG_ERR, "%s: ignoring unknown handshake flags: %#06x",
__func__, flags);
if (!(flags & NBD_FLAG_FIXED_NEWSTYLE)) {
syslog(LOG_ERR, "%s: this server does not support the fixed "
"newstyle protocol", __func__);
return false;
}
return true;
}
static inline void
nbd_newstyle_negotiation_dump(struct nbd_newstyle_negotiation *handshake)
{
uint16_t flags = handshake->handshake_flags;
syslog(LOG_DEBUG, "\tmagic: %#018lx", handshake->magic);
syslog(LOG_DEBUG, "\tnewstyle_magic: %#018lx",
handshake->newstyle_magic);
syslog(LOG_DEBUG, "\thandshake_flags: %#06x [%s%s%s]%s", flags,
(flags & NBD_FLAG_FIXED_NEWSTYLE) ? "FIXED_NEWSTYLE" : "",
((flags & VALID_NEWSTYLE_FLAGS)
== VALID_NEWSTYLE_FLAGS) ? "|" : "",
(flags & NBD_FLAG_NO_ZEROES) ? "NO_ZEROES" : "",
(flags & ~VALID_NEWSTYLE_FLAGS) ? " (invalid)" : "");
}
static inline void
nbd_client_flags_set_client_flags(struct nbd_client_flags *client_flags,
uint32_t flags)
{
client_flags->client_flags = htobe32(flags);
}
/*
* Client handshake
*
* If the client and server agree not to send the reserved portion of the
* EXPORT_NAME option reply, 1 is returned, otherwise 0.
*
* Returns -1 if an error is encountered.
*/
static int
nbd_client_newstyle_handshake(struct nbd_client *client)
{
struct nbd_newstyle_negotiation handshake;
struct nbd_client_flags response;
uint32_t client_flags;
ssize_t len;
int sock;
sock = client->sock;
while (true) {
len = recv(sock, &handshake, sizeof handshake, MSG_WAITALL);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len != sizeof handshake)
goto connection_fail;
break;
}
nbd_newstyle_negotiation_ntoh(&handshake);
if (!nbd_newstyle_negotiation_is_valid(&handshake)) {
syslog(LOG_ERR, "%s: invalid handshake", __func__);
nbd_newstyle_negotiation_dump(&handshake);
return FAILURE;
}
client_flags = NBD_CLIENT_FLAG_FIXED_NEWSTYLE;
if (handshake.handshake_flags & NBD_FLAG_NO_ZEROES)
client_flags |= NBD_CLIENT_FLAG_NO_ZEROES;
nbd_client_flags_set_client_flags(&response, client_flags);
len = send(sock, &response, sizeof response, MSG_NOSIGNAL);
if (len != sizeof response)
goto connection_fail;
client->flags = handshake.handshake_flags << 16;
return SUCCESS;
connection_fail:
syslog(LOG_ERR, "%s: connection failed: %m", __func__);
return FAILURE;
}
static inline void
nbd_option_init(struct nbd_option *option)
{
memset(option, 0, sizeof *option);
option->magic = htobe64(NBD_OPTION_MAGIC);
}
static inline void
nbd_option_set_option(struct nbd_option *option, uint32_t opt)
{
option->option = htobe32(opt);
}
static inline void
nbd_option_set_length(struct nbd_option *option, uint32_t length)
{
option->length = htobe32(length);
}
static int
nbd_client_send_option(struct nbd_client *client,
struct nbd_option *option,
size_t length, uint8_t *data)
{
ssize_t len;
int sock;
sock = client->sock;
len = send(sock, option, sizeof *option, MSG_NOSIGNAL);
if (len != sizeof *option)
goto connection_fail;
if (length == 0)
return SUCCESS;
assert(data != NULL);
len = send(sock, data, length, MSG_NOSIGNAL);
if (len != length)
goto connection_fail;
return SUCCESS;
connection_fail:
syslog(LOG_ERR, "%s: connection failed: %m", __func__);
return FAILURE;
}
static inline void
nbd_option_reply_ntoh(struct nbd_option_reply *reply)
{
reply->magic = be64toh(reply->magic);
reply->option = be32toh(reply->option);
reply->type = be32toh(reply->type);
reply->length = be32toh(reply->length);
}
static inline bool
nbd_option_reply_is_valid(struct nbd_option_reply *reply,
struct nbd_option *option)
{
uint32_t opt;
opt = be32toh(option->option);
assert(option->option != NBD_OPTION_EXPORT_NAME);
if (reply->magic != NBD_OPTION_REPLY_MAGIC) {
syslog(LOG_ERR,
"%s: invalid magic: %#018lx (expected %#018lx)",
__func__, reply->magic, NBD_OPTION_REPLY_MAGIC);
return false;
}
if (reply->option != opt) {
syslog(LOG_ERR,
"%s: unexpected option: %#010x (expected %#010x)",
__func__, reply->option, opt);
return false;
}
return true;
}
static inline char const *
nbd_option_reply_option_string(struct nbd_option_reply *reply)
{
switch (reply->option) {
#define CASE_MESSAGE(c) case c: return #c
#define EXTENSION " [unsupported extension]"
#define WITHDRAWN " [withdrawn]"
CASE_MESSAGE(NBD_OPTION_EXPORT_NAME);
CASE_MESSAGE(NBD_OPTION_ABORT);
CASE_MESSAGE(NBD_OPTION_LIST);
CASE_MESSAGE(NBD_OPTION_PEEK_EXPORT) WITHDRAWN;
CASE_MESSAGE(NBD_OPTION_STARTTLS);
CASE_MESSAGE(NBD_OPTION_INFO) EXTENSION;
CASE_MESSAGE(NBD_OPTION_GO) EXTENSION;
CASE_MESSAGE(NBD_OPTION_STRUCTURED_REPLY) EXTENSION;
CASE_MESSAGE(NBD_OPTION_BLOCK_SIZE) EXTENSION;
#undef WITHDRAWN
#undef EXTENSION
#undef CASE_MESSAGE
default: return NULL;
}
}
static inline char const *
nbd_option_reply_type_string(struct nbd_option_reply *reply)
{
switch (reply->type) {
#define CASE_MESSAGE(c) case c: return #c
#define EXTENSION " [unsupported extension]"
#define UNUSED " [unused]"
#define TODO " [todo]"
CASE_MESSAGE(NBD_REPLY_ACK);
CASE_MESSAGE(NBD_REPLY_SERVER);
CASE_MESSAGE(NBD_REPLY_INFO) EXTENSION;
CASE_MESSAGE(NBD_REPLY_ERROR_UNSUPPORTED);
CASE_MESSAGE(NBD_REPLY_ERROR_POLICY);
CASE_MESSAGE(NBD_REPLY_ERROR_INVALID);
CASE_MESSAGE(NBD_REPLY_ERROR_PLATFORM) UNUSED;
CASE_MESSAGE(NBD_REPLY_ERROR_TLS_REQUIRED) TODO;
CASE_MESSAGE(NBD_REPLY_ERROR_UNKNOWN) EXTENSION;
CASE_MESSAGE(NBD_REPLY_ERROR_SHUTDOWN);
CASE_MESSAGE(NBD_REPLY_ERROR_BLOCK_SIZE_REQD) EXTENSION;
#undef TODO
#undef UNUSED
#undef EXTENSION
#undef CASE_MESSAGE
default: return NULL;
}
}
static inline void
nbd_option_reply_dump(struct nbd_option_reply *reply)
{
char const *option = nbd_option_reply_option_string(reply);
char const *type = nbd_option_reply_type_string(reply);
syslog(LOG_DEBUG, "\tmagic: %#018lx", reply->magic);
if (option == NULL)
syslog(LOG_DEBUG, "\toption: [unknown] %#010x (%d)",
reply->option, reply->option);
else
syslog(LOG_DEBUG, "\toption: %s", option);
if (type == NULL)
syslog(LOG_DEBUG, "\ttype: [unknown] %#010x (%d)",
reply->type, reply->type);
else
syslog(LOG_DEBUG, "\ttype: %s", type);
syslog(LOG_DEBUG, "\tlength: %u", reply->length);
}
static int
nbd_client_recv_option_reply(struct nbd_client *client,
struct nbd_option *option,
struct nbd_option_reply *reply,
size_t datalen, uint8_t *data)
{
size_t recvlen;
ssize_t len;
int sock;
sock = client->sock;
while (true) {
len = recv(sock, reply, sizeof *reply, MSG_WAITALL);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len != sizeof *reply)
goto connection_fail;
break;
}
nbd_option_reply_ntoh(reply);
if (!nbd_option_reply_is_valid(reply, option)) {
syslog(LOG_ERR, "%s: invalid option reply", __func__);
nbd_option_reply_dump(reply);
return FAILURE;
}
if (reply->length == 0)
return SUCCESS;
if (datalen == 0)
return MOREDATA;
assert(data != NULL);
recvlen = MIN(reply->length, datalen);
while (true) {
len = recv(sock, data, recvlen, MSG_WAITALL);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len != recvlen)
goto connection_fail;
break;
}
if (recvlen > datalen)
return MOREDATA;
return SUCCESS;
connection_fail:
syslog(LOG_ERR, "%s: connection failed: %m", __func__);
return FAILURE;
}
static inline void
nbd_export_info_ntoh(struct nbd_export_info *info)
{
info->size = be64toh(info->size);
info->transmission_flags = be16toh(info->transmission_flags);
}
static int
nbd_client_recv_export_info(struct nbd_client *client,
struct nbd_export_info *info)
{
static size_t const SHORT_INFO_LEN =
sizeof *info - sizeof info->reserved;
ssize_t len;
int sock;
sock = client->sock;
while (true) {
len = recv(sock, info, SHORT_INFO_LEN, MSG_WAITALL);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len != SHORT_INFO_LEN)
goto connection_fail;
break;
}
nbd_export_info_ntoh(info);
client->size = info->size;
client->flags |= info->transmission_flags;
if ((client->flags >> 16) & NBD_FLAG_NO_ZEROES)
return SUCCESS;
while (true) {
len = recv(sock, info + SHORT_INFO_LEN,
sizeof info->reserved, MSG_WAITALL);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len != sizeof info->reserved)
goto connection_fail;
break;
}
return SUCCESS;
connection_fail:
syslog(LOG_ERR, "%s: connection failed: %m", __func__);
return FAILURE;
}
static int
nbd_client_negotiate_options_fixed_newstyle(struct nbd_client *client)
{
struct nbd_option option;
struct nbd_option_reply reply;
struct nbd_export_info info;
nbd_option_init(&option);
nbd_option_set_option(&option, NBD_OPTION_EXPORT_NAME);
if (nbd_client_send_option(client, &option, 0, NULL) == FAILURE) {
syslog(LOG_ERR, "%s: sending option EXPORT_NAME failed",
__func__);
return FAILURE;
}
if (nbd_client_recv_export_info(client, &info) == FAILURE) {
syslog(LOG_ERR, "%s: receiving export info failed",
__func__);
return FAILURE;
}
if (!(info.transmission_flags & NBD_FLAG_SEND_FLUSH))
syslog(LOG_INFO, "%s: server does not support FLUSH command",
__func__);
if (!(info.transmission_flags & NBD_FLAG_SEND_TRIM))
syslog(LOG_INFO, "%s: server does not support TRIM command",
__func__);
return SUCCESS;
}
static inline void
nbd_option_reply_server_ntoh(struct nbd_option_reply_server *server_export)
{
server_export->length = be32toh(server_export->length);
}
static int
nbd_client_negotiate_list_fixed_newstyle(struct nbd_client *client)
{
struct nbd_option option;
struct nbd_option_reply reply;
struct nbd_option_reply_server *server_export;
size_t const BUFLEN = NAME_MAX;
char *export_name, buf[BUFLEN + 1];
size_t recvlen, remaining;
ssize_t len;
int rc;
nbd_option_init(&option);
nbd_option_set_option(&option, NBD_OPTION_LIST);
if (nbd_client_send_option(client, &option, 0, NULL) == FAILURE) {
syslog(LOG_ERR, "%s: sending option LIST failed", __func__);
return FAILURE;
}
while (true) {
rc = nbd_client_recv_option_reply(client, &option, &reply,
BUFLEN, (uint8_t *)buf);
if (rc == FAILURE) {
syslog(LOG_ERR,
"%s: receiving option LIST reply failed",
__func__);
return FAILURE;
}
if (reply.type < 0) {
char const *msg =
nbd_option_reply_type_string(&reply);
if (msg == NULL) {
syslog(LOG_ERR, "%s: unknown server error (%d)",
__func__, reply.type);
syslog(LOG_DEBUG, "\tbe32toh: %d",
be32toh(reply.type));
} else {
syslog(LOG_ERR, "%s: server error: %s",
__func__, msg);
}
nbd_option_reply_dump(&reply);
return FAILURE;
}
if (reply.type == NBD_REPLY_ACK)
break;
assert(reply.type == NBD_REPLY_SERVER);
server_export = (struct nbd_option_reply_server *)buf;
nbd_option_reply_server_ntoh(server_export);
if (server_export->length == 0) {
printf("\t[default export]\n");
continue;
}
recvlen = MIN(server_export->length, BUFLEN - 4);
export_name = buf + sizeof *server_export;
export_name[recvlen] = '\0';
printf("\t%s", export_name);
if (rc == SUCCESS) {
printf("\n");
continue;
}
remaining = server_export->length - BUFLEN;
assert(remaining > 0);
do {
recvlen = MIN(remaining, BUFLEN);
len = recv(client->sock, buf, recvlen, 0);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len != recvlen) {
syslog(LOG_ERR, "%s: connection failed: %m",
__func__);
return FAILURE;
}
buf[recvlen] = '\0';
printf("%s", buf);
remaining -= recvlen;
} while (remaining > 0);
printf("\n");
}
return SUCCESS;
}
struct handshake_magic {
uint64_t magic;
uint64_t style;
} __packed;
static inline void
handshake_magic_ntoh(struct handshake_magic *handshake)
{
handshake->magic = be64toh(handshake->magic);
handshake->style = be64toh(handshake->style);
}
static inline void
handshake_magic_dump(struct handshake_magic *handshake)
{
syslog(LOG_DEBUG, "\tmagic: %#018lx (expected %#018lx)",
handshake->magic, NBD_MAGIC);
syslog(LOG_DEBUG, "\tstyle: %#018lx (expected %#018lx or %#018lx)",
handshake->style, NBD_OLDSTYLE_MAGIC, NBD_NEWSTYLE_MAGIC);
}
int
nbd_client_negotiate(struct nbd_client *client)
{
struct handshake_magic handshake;
ssize_t len;
int sock;
sock = client->sock;
while (true) {
len = recv(sock, &handshake, sizeof handshake, MSG_PEEK);
if (client->disconnect)
return FAILURE;
if (len == -1 && errno == EINTR)
continue;
if (len == -1) {
syslog(LOG_ERR, "%s: connection failed: %m", __func__);
return FAILURE;
}
if (len == sizeof handshake)
break;
}
handshake_magic_ntoh(&handshake);
if (handshake.magic != NBD_MAGIC) {
syslog(LOG_ERR, "%s: handshake failed: invalid magic", __func__);
return FAILURE;
}
if (handshake.style == NBD_OLDSTYLE_MAGIC) {
syslog(LOG_INFO, "%s: oldstyle handshake detected", __func__);
if (nbd_client_oldstyle_handshake(client) == FAILURE) {
syslog(LOG_ERR, "%s: handshake failed", __func__);
return FAILURE;
}
return SUCCESS;
} else if (handshake.style == NBD_NEWSTYLE_MAGIC) {
syslog(LOG_INFO, "%s: newstyle handshake detected", __func__);
if (nbd_client_newstyle_handshake(client) == FAILURE) {
syslog(LOG_ERR, "%s: handshake failed", __func__);
return FAILURE;
}
if (nbd_client_negotiate_options_fixed_newstyle(client)
== FAILURE) {
syslog(LOG_ERR, "%s: option negotiation failed",
__func__);
return FAILURE;
}
return SUCCESS;
}
syslog(LOG_ERR, "%s: handshake failed: unknown style", __func__);
handshake_magic_dump(&handshake);
return FAILURE;
}
int
nbd_client_list(struct nbd_client *client)
{
if (nbd_client_newstyle_handshake(client) == FAILURE) {
syslog(LOG_ERR, "%s: handshake failed", __func__);
return FAILURE;
}
if (nbd_client_negotiate_list_fixed_newstyle(client) == FAILURE) {