forked from gregkh/ndas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathndemul.c
More file actions
3907 lines (3422 loc) · 126 KB
/
ndemul.c
File metadata and controls
3907 lines (3422 loc) · 126 KB
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
/**********************************************************************
* Copyright ( C ) 2012 IOCELL Networks
* All rights reserved.
**********************************************************************/
#include "linux_ver.h"
#include "inc/xplatcfg.h"
#include "inc/sal/libc.h"
#include "inc/sal/types.h"
#include "inc/sal/debug.h"
#include "inc/sal/net.h"
#include "inc/sal/io.h"
#include "inc/sal/thread.h"
#include "inc/lpx/lpx.h"
#include "inc/lpx/lpxutil.h"
#include "inc/ndasuser/io.h"
#include "inc/ndasuser/ndasuser.h"
#include "inc/xlib/xbuf.h"
#include "inc/xlib/gtypes.h" /* g_ntohl g_htonl g_ntohs g_htons */
#include "inc/netdisk/netdiskid.h"
#ifdef NDAS_EMU
#include "hash.h"
#include "hdreg.h"
#include "binaryparameters.h"
#include "lanscsi.h"
#include "lsp.h"
//#define EMU_MAX_BLOCKS 64 /* Reduced max blocks to reduce peak memory usage */
#define EMU_MAX_BLOCKS 512
#define EMU_BYTES_OF_BLOCK 512
#define EMU_IDENTIFY_BYTES 512
#define __SOFTWARE_DISK_INFO__ 1
#ifdef DEBUG
#define debug_emu(l,x...) do { \
if ( (l) <= DEBUG_LEVEL_NDASEMU ) {\
sal_debug_print("EM|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x); \
} \
} while(0)
#else
#define debug_emu(l, x...) do {} while(0)
#endif
#define NR_MAX_TARGET 1
#define MAX_DATA_BUFFER_SIZE EMU_MAX_BLOCKS * EMU_BYTES_OF_BLOCK
#define MAX_CONNECTION 16
#define LANSCSI_CURRENT_EMU_VERSION LANSCSI_VERSION_2_0 // MAX supported version
#define LANSCSI_CURRENT_EMU_REVISION 0x0e00
#ifndef MAX_REQUEST_SIZE
#define MAX_REQUEST_SIZE 1500
#endif
// Start from 0xe00
// v0. Released 2006/5/23 to ALtech.
#define HTONLL(Data) ( (((Data)&0x00000000000000FFLL) << 56) | (((Data)&0x000000000000FF00LL) << 40) \
| (((Data)&0x0000000000FF0000LL) << 24) | (((Data)&0x00000000FF000000LL) << 8) \
| (((Data)&0x000000FF00000000LL) >> 8) | (((Data)&0x0000FF0000000000LL) >> 24) \
| (((Data)&0x00FF000000000000LL) >> 40) | (((Data)&0xFF00000000000000LL) >> 56))
#define NTOHLL(Data) ( (((Data)&0x00000000000000FFLL) << 56) | (((Data)&0x000000000000FF00LL) << 40) \
| (((Data)&0x0000000000FF0000LL) << 24) | (((Data)&0x00000000FF000000LL) << 8) \
| (((Data)&0x000000FF00000000LL) >> 8) | (((Data)&0x0000FF0000000000LL) >> 24) \
| (((Data)&0x00FF000000000000LL) >> 40) | (((Data)&0xFF00000000000000LL) >> 56))
#define SOCKET int // lpxfd
//#define NDASEMU_ZEROCOPY_MODE 1
#if !defined(NDASEMU_ZEROCOPY_MODE)
//#define NDASEMU_DIRECT_IO 1
#endif
#define NETDISK_DEV_LOCK_COUNT 4
typedef struct _LOCK_TABLE_ENTRY {
volatile xbool Locked;
volatile int count;
volatile char owner[6];
} LOCK_TABLE_ENTRY, *PLOCK_TABLE_ENTRY;
/*
* NDAS task
*/
typedef union _NDASEMU_PDU_REQ {
LANSCSI_H2R_PDU_HEADER gen;
LANSCSI_LOGIN_REQUEST_PDU_HEADER login;
LANSCSI_LOGOUT_REQUEST_PDU_HEADER logout;
LANSCSI_TEXT_REQUEST_PDU_HEADER text;
LANSCSI_VENDOR_REQUEST_PDU_HEADER vendor;
LANSCSI_IDE_REQUEST_PDU_HEADER ide_v0;
LANSCSI_IDE_REQUEST_PDU_HEADER_V1 ide;
unsigned char pduheader_buffer[MAX_REQUEST_SIZE];
} NDASEMU_PDU_REQ, *PNDASEMU_PDU_REQ;
typedef union _NDASEMU_PDU_REP {
LANSCSI_R2H_PDU_HEADER gen;
LANSCSI_LOGIN_REPLY_PDU_HEADER login;
LANSCSI_LOGOUT_REPLY_PDU_HEADER logout;
LANSCSI_TEXT_REPLY_PDU_HEADER text;
LANSCSI_VENDOR_REPLY_PDU_HEADER vendor;
LANSCSI_IDE_REPLY_PDU_HEADER ide_v0;
LANSCSI_IDE_REPLY_PDU_HEADER_V1 ide;
unsigned char pduheader_buffer[MAX_REQUEST_SIZE];
} NDASEMU_PDU_REP, *PNDASEMU_PDU_REP;
struct _NDASEMU_BUFFER_SEGMENT;
struct _NDASEMU_BUFFER_POOL;
struct _SESSION_DATA;
typedef struct _NDAS_TASK {
// task queue entry
struct list_head next;
struct _SESSION_DATA * session;
int sockfd;
lpx_aio aio;
// immediate data buffer specified in PDU's data segment length or
// data transfer legnth.
struct _NDASEMU_BUFFER_SEGMENT *bufseg;
struct _NDASEMU_BUFFER_POOL *bufpool;
// Task addresses
unsigned int targetid;
// PDU request information
unsigned char command;
xint64 blockaddr;
unsigned int blocklen;
unsigned int transferlen;
xuint16 feature;
LANSCSI_PDU_POINTERS pdureq_pointers;
// PDU request/reply header
PNDASEMU_PDU_REQ pdu_req;
PNDASEMU_PDU_REP pdu_rep;
sal_page *pdurep_pages[2];
unsigned int pdurep_offset;
} NDAS_TASK, *PNDAS_TASK;
typedef struct _NDAS_TASK_QUEUE {
sal_spinlock task_queue_mutex;
struct list_head task_head;
} NDAS_TASK_QUEUE, PNDAS_TASK_QUEUE;
static
void
inline
ndasemu_init_task(
PNDAS_TASK task,
int sockfd,
struct _NDASEMU_BUFFER_POOL * bufpool,
struct _NDASEMU_BUFFER_SEGMENT * bufseg
){
task->sockfd = sockfd;
task->session = NULL;
task->bufpool = bufpool;
task->bufseg = bufseg;
}
/*
* Target data
*/
typedef struct _TARGET_DATA {
xbool bPresent;
xbool bLBA;
xbool bLBA48;
xint8 NRRWHost;
xint8 NRROHost;
xuint16 PIO;
xuint16 UDMA;
xuint16 MWDMA;
xuchar Info[EMU_IDENTIFY_BYTES];
char * ExportDev;
sal_file Export;
xuint64 Size;
xuint32 TargetDataHi;
xuint32 TargetDataLow;
LOCK_TABLE_ENTRY LockTable[NETDISK_DEV_LOCK_COUNT];
// Task queue
NDAS_TASK_QUEUE task_queue;
} TARGET_DATA, *PTARGET_DATA;
/*
* NDAS session
*/
#define NDASEMU_MAX_PENDING_TASKS 3
typedef struct _SESSION_DATA {
SOCKET connSock;
int logincount;
xuint16 TargetID;
xuint32 LUN;
int iSessionPhase;
xuint16 CSubPacketSeq;
xuint8 iLoginType;
xuint16 CPSlot;
unsigned PathCommandTag;
unsigned iUser;
xuint32 HPID;
xuint16 RPID;
unsigned CHAP_I;
unsigned CHAP_C;
xbool bIncCount;
xuchar iPassword[8];
xuchar Addr[6];
xbool HeldLock[NETDISK_DEV_LOCK_COUNT];
NDAS_TASK tasks[NDASEMU_MAX_PENDING_TASKS];
PNDAS_TASK next_avail_task;
unsigned int rtask_sn;
unsigned int rtask_pending_sn;
} SESSION_DATA, *PSESSION_DATA;
/*
* PROM data
*/
typedef struct _PROM_DATA {
xuint64 MaxConnTime;
xuint64 MaxRetTime;
xuchar UserPasswd[8];
xuchar SuperPasswd[8];
} PROM_DATA, *PPROM_DATA;
#if 1
struct _NDASEMU_BUFFER_POOL;
struct SESSION_THREAD_PARAM {
SOCKET sock;
struct sockaddr_lpx slpx;
struct _NDASEMU_BUFFER_POOL *ndasemu_bufferpool;
};
#endif
/*
* Task thread worker data
*/
typedef struct _TASK_WORKER_CONTEXT {
sal_semaphore tw_task_semaphore;
NDAS_TASK_QUEUE tw_task_queue;
} TASK_WORKER_CONTEXT, *PTASK_WORKER_CONTEXT;
#define NDASEMU_MAX_TASK_WORKERS 1
TASK_WORKER_CONTEXT task_worker_context[NDASEMU_MAX_TASK_WORKERS];
// Global Variables
static xint16 G_RPID = 0;
static int ListenSock;
static sal_thread_id GenPnpThreadId;
static sal_semaphore GenPnpExited;
static volatile xbool GenPnpRunning;
static sal_thread_id ListenThreadId;
static sal_semaphore ListenThreadExited;
static volatile xbool ListenThreadRunning;
// to support version 1.1, 2.0
static xuint8 thisHWVersion;
static TARGET_DATA *PerTarget[NR_MAX_TARGET];
static PROM_DATA *Prom;
// Data buffers
#define NDASEMU_DATABUFFER_ACQUIRED 0x0001
typedef struct _NDASEMU_BUFFER_SEGMENT {
void * bufseg_addr;
sal_page **bufseg_pages;
unsigned int bufseg_offset;
int bufseg_size;
int bufseg_flags;
} NDASEMU_BUFFER_SEGMENT, *PNDASEMU_BUFFER_SEGMENT;
//#define NDASEMU_BUFFER_MAX_SEGMENT_NUM NDASEMU_MAX_PENDING_TASKS
#define NDASEMU_BUFFER_MAX_SEGMENT_NUM 1
typedef struct _NDASEMU_BUFFER_POOL {
NDASEMU_BUFFER_SEGMENT segments[NDASEMU_BUFFER_MAX_SEGMENT_NUM];
sal_spinlock buffer_mutex; /* Not accessed at interrupt context. Okay to use spinlock without irqsave */
sal_semaphore buffer_avail;
PNDASEMU_BUFFER_SEGMENT recent_segment;
int acquired_count;
} NDASEMU_BUFFER_POOL, *PNDASEMU_BUFFER_POOL;
static NDASEMU_BUFFER_POOL global_databuffer;
static xuint16 HeaderEncryptAlgo = 0;
static xuint16 DataEncryptAlgo = 0;
static sal_spinlock EmuLock;
//static sal_semaphore DiskIoLock = SAL_INVALID_SEMAPHORE;
static int MaxBlockSize = EMU_MAX_BLOCKS; // EMU_BYTES_OF_BLOCK bytes of one block size.
/*
* Random number generator
*/
static int _rand(void)
{
static int i = 32217;
return i++;
}
/*
* Task service functions
*/
ndas_error_t
ndasemu_task_login_decode_service(
PNDAS_TASK ndastask
);
ndas_error_t
ndasemu_task_logout_decode_service(
PNDAS_TASK ndastask
);
ndas_error_t
ndasemu_task_text_decode_service(
PNDAS_TASK ndastask
);
ndas_error_t
ndasemu_task_vendor_decode_service(
PNDAS_TASK ndastask
);
ndas_error_t
ndasemu_task_ide_v0_decode(
PNDAS_TASK ndastask
);
ndas_error_t
ndasemu_task_ide_decode(
PNDAS_TASK ndastask
);
/*
* Buffer management
*/
int
ndasemu_is_bufseg_avail(
PNDASEMU_BUFFER_POOL ndasemu_bufferpool);
PNDASEMU_BUFFER_SEGMENT
ndasemu_get_segment(
PNDASEMU_BUFFER_POOL ndasemu_bufferpool,
int wait);
void
ndasemu_put_segment(
PNDASEMU_BUFFER_POOL ndasemu_bufferpool,
PNDASEMU_BUFFER_SEGMENT segment);
/*
* Raw device communication management
*/
NDAS_SAL_API
int ndasemu_read_actor(
sal_read_descriptor_t *read_desc,
sal_page *page,
unsigned long offset,
unsigned long size
){
int ret;
int sockdesc = (int)read_desc->arg.data;
xsize_t written;
char *vaddr;
unsigned long count = read_desc->count;
if (size > count)
size = count;
#if 0
vaddr = sal_map_page(page);
ret = lpx_send(sockdesc, vaddr + offset, size, 0);
sal_unmap_page(page);
#else
ret = lpx_sendpages(sockdesc, &page, offset, size, 0);
#endif
if(ret < 0) {
debug_emu(1, "lpx_send failed vaddr=%p ret=%d", vaddr, ret);
written = 0;
} else {
// debug_emu(1, "lpx_send vaddr=%p offset=%lu size=%lu", vaddr, offset, size);
written = size;
read_desc->count = count - size;
read_desc->written += size;
}
return written;
}
//
// Send file data to network using SAL senfile API to reduce copy.
//
int
ndasemu_sendfile(
int sockfd,
sal_file file,
xuint64 offset,
xint32 size,
int *res
){
xint32 ret;
sal_read_actor_context read_actor_ctx;
read_actor_ctx.sal_read_actor = ndasemu_read_actor;
read_actor_ctx.target = (void *)sockfd;
ret = sal_file_send(
file,
offset,
size,
&read_actor_ctx);
if (ret<=0) {
debug_emu(1, "sal_file_send faild. offset=%llu size=%d", offset, size);
*res = 0;
return -1;
} else {
// debug_emu(1, "sal_file_send. offset=%llu size=%d", offset, size);
*res = size;
return 0;
}
}
//
// Send file data to network using SAL senfile API to reduce copy.
//
int
ndasemu_recvfile(
int sockfd,
sal_file file,
xuint64 offset,
xint32 size,
int *res
){
xint32 ret;
struct xbuf_queue rx_xq;
xbuf_queue_init(&rx_xq);
ret = lpx_recv_xbuf(sockfd, &rx_xq, size);
xbuf_queue_destroy(&rx_xq);
if (ret<=0) {
debug_emu(1, "lpx_recv_xbuf faild. offset=%llu size=%d", offset, size);
return -1;
} else {
// debug_emu(1, "lpx_recv_xbuf. offset=%llu size=%d", offset, size);
*res = size;
return 0;
}
}
static int SendIt(int lpxfd, void* buf, int size, int *res)
{
int ret;
lpx_aio aio;
lpx_prepare_aio(
lpxfd,
0,
(void *)buf,
0,
size,
0,
&aio);
ret = lpx_send_aio(&aio);
if (ret<=0) {
return -1;
} else {
*res = size;
return 0;
}
}
void ndasemu_aio_complete(struct _lpx_aio * aio, void *userparam)
{
PNDAS_TASK task = (PNDAS_TASK)userparam;
debug_emu(4, "aio=%p sockfd=%d len=%d comp=%d err=%d ret=%d",
aio, aio->sockfd, aio->len, aio->completed_len, aio->error_len, aio->returncode);
// return the buffer
if(task->bufseg)
{
ndasemu_put_segment(task->bufpool, task->bufseg);
task->bufseg = NULL;
}
}
static
int
ndasemu_sendpages(PNDAS_TASK task, int size, int *res)
{
int ret;
debug_emu(4, "task=%p requested len=%d", task, size);
if(size == 0) {
*res = 0;
return 0;
}
ret = lpx_sendpages(
task->sockfd,
task->bufseg->bufseg_pages,
task->bufseg->bufseg_offset,
size,
0
);
if (ret<=0) {
return -1;
} else {
*res = size;
return 0;
}
}
int SendtoIt(int lpxfd, void * buff, int len, unsigned flags,
const struct sockaddr_lpx *addr, int addr_len)
{
return lpx_sendto(lpxfd, buff, len, flags, addr, addr_len);
}
int RecvIt(int lpxfd, void* buf, int len, int *res, lpx_aio_head_lookahead aio_head_lookahead, void *userparam)
{
int ret;
lpx_aio aio;
lpx_prepare_aio(
lpxfd,
0,
buf,
0,
len,
0,
&aio);
if(aio_head_lookahead) {
lpx_set_head_lookahead_routine( aio_head_lookahead, &aio);
lpx_set_userparam(userparam, &aio);
}
ret = lpx_recv_aio(&aio);
if (ret<0) {
debug_emu(1, "failed len=%d", len);
return -1;
} else if (ret==0) {
// timeout
debug_emu(4, "timeout len=%d", len);
*res = 0;
return 0;
} else {
*res = len;
return 0;
}
}
/*
* PDU management
*/
int
ndasemu_head_lookahead(
lpx_aio *aio,
void *userparam,
void *recvdata,
int recvdata_len,
int *request_len_adjustment
)
{
PNDAS_TASK ndastask = (PNDAS_TASK)userparam;
PSESSION_DATA pSessionData = ndastask->session;
PLANSCSI_H2R_PDU_HEADER pdu_gen_req = (PLANSCSI_H2R_PDU_HEADER)recvdata;
xuchar* pPtr = (xuchar*)pdu_gen_req;
PLANSCSI_PDU_POINTERS pPdu = &ndastask->pdureq_pointers;
int iTotalRecved;
int dataseg_len, ahseg_len;
int iret;
if(recvdata_len < sizeof(LANSCSI_H2R_PDU_HEADER)) {
return 0;
}
if(pSessionData->iSessionPhase == FLAG_FULL_FEATURE_PHASE
&& HeaderEncryptAlgo != 0) {
Decrypt32(
(unsigned char*)pdu_gen_req,
sizeof(LANSCSI_H2R_PDU_HEADER),
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
ndastask->pdu_req = (PNDASEMU_PDU_REQ)recvdata;
iTotalRecved = sizeof(LANSCSI_H2R_PDU_HEADER);
pPdu->u.pH2RHeader = (PLANSCSI_H2R_PDU_HEADER)pPtr;
pPtr += sizeof(LANSCSI_H2R_PDU_HEADER);
ahseg_len = g_ntohs(pPdu->u.pH2RHeader->AHSLen);
dataseg_len = g_ntohl(pPdu->u.pH2RHeader->DataSegLen);
// Read AHS.
if(ahseg_len > 0) {
//debug_emu(1, "AHSLen = %d", g_ntohs(pPdu->u.pH2RHeader->AHSLen));
if(recvdata_len < iTotalRecved +ahseg_len) {
debug_emu(1, "Can't Recv AHS...");
return iTotalRecved;
}
iTotalRecved += ahseg_len;
pPdu->pAHS = pPtr;
pPtr += ahseg_len;
// to support version 1.1, 2.0
if (thisHWVersion == LANSCSI_VERSION_1_1 ||
thisHWVersion == LANSCSI_VERSION_2_0) {
if (pSessionData->iSessionPhase == FLAG_FULL_FEATURE_PHASE
&& HeaderEncryptAlgo != 0) {
Decrypt32(
(unsigned char*)pPdu->pAHS,
ahseg_len,
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
}
}
// Read Header Dig.
pPdu->pHeaderDig = NULL;
// Read Data segment.
if(dataseg_len > 0) {
//debug_emu(1, "DataSegLen = %d", g_ntohl(pPdu->u.pH2RHeader->DataSegLen));
if(recvdata_len < iTotalRecved + dataseg_len) {
debug_emu(1, "Can't Recv Data segment...");
return iTotalRecved;
}
iTotalRecved += dataseg_len;
pPdu->pDataSeg = pPtr;
pPtr += dataseg_len;
if(pSessionData->iSessionPhase == FLAG_FULL_FEATURE_PHASE
&& DataEncryptAlgo != 0) {
Decrypt32(
(unsigned char*)pPdu->pDataSeg,
dataseg_len,
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
}
// Read Data Dig.
pPdu->pDataDig = NULL;
sal_assert(iTotalRecved >= sizeof(LANSCSI_H2R_PDU_HEADER));
*request_len_adjustment = iTotalRecved - sizeof(LANSCSI_H2R_PDU_HEADER);
//
// Decode the request PDU.
//
ndastask->pdu_rep->gen.Flags = ndastask->pdu_req->gen.Flags;
ndastask->pdu_rep->gen.PathCommandTag = ndastask->pdu_req->gen.PathCommandTag;
ndastask->pdu_rep->gen.InitiatorTaskTag = ndastask->pdu_req->gen.InitiatorTaskTag;
ndastask->pdu_rep->gen.TargetID = ndastask->pdu_req->gen.TargetID;
ndastask->pdu_rep->gen.LunHi = ndastask->pdu_req->gen.LunHi;
ndastask->pdu_rep->gen.LunLow = ndastask->pdu_req->gen.LunLow;
ndastask->pdu_rep->gen.CSubPacketSeq = ndastask->pdu_req->gen.CSubPacketSeq;
switch(ndastask->pdu_req->gen.Opcode) {
case LOGIN_REQUEST:
(void)ndasemu_task_login_decode_service(ndastask);
break;
case LOGOUT_REQUEST:
(void)ndasemu_task_logout_decode_service(ndastask);
break;
case TEXT_REQUEST:
(void)ndasemu_task_text_decode_service(ndastask);
break;
case VENDOR_SPECIFIC_COMMAND:
(void)ndasemu_task_vendor_decode_service(ndastask);
break;
case IDE_COMMAND:
if (thisHWVersion == LANSCSI_VERSION_1_0) {
iret = ndasemu_task_ide_v0_decode(
ndastask);
} else if (thisHWVersion == LANSCSI_VERSION_1_1 ||
thisHWVersion == LANSCSI_VERSION_2_0) {
iret = ndasemu_task_ide_decode(
ndastask);
} else {
iret = -1;
}
break;
case NOP_H2R:
ndastask->pdu_rep->gen.Opcode = NOP_R2H;
break;
}
ndastask->pdu_req = NULL;
return iTotalRecved;
}
#define NDASEMU_RECVREQ_FLAG_NO_WAIT 0x0001
#define NDASEMU_RECVREQ_FLAG_LOOKAHEAD 0x0002
int
ndasemu_receive_request(
PNDAS_TASK task,
int flags
)
{
PSESSION_DATA pSessionData = task->session;
PLANSCSI_H2R_PDU_HEADER pRequest;
PLANSCSI_PDU_POINTERS pPdu;
xuchar* pPtr;
int iResult, res;
int iTotalRecved = 0;
if(flags & NDASEMU_RECVREQ_FLAG_NO_WAIT) {
if(!lpx_is_recv_data(task->sockfd)) {
return 0;
}
}
pRequest = &task->pdu_req->gen;
pPdu = &task->pdureq_pointers;
pPtr = (xuchar*)pRequest;
timeout_retry:
// Read Header.
if(flags & NDASEMU_RECVREQ_FLAG_LOOKAHEAD) {
iResult = RecvIt(
task->sockfd,
pPtr,
sizeof(LANSCSI_H2R_PDU_HEADER),
&res,
ndasemu_head_lookahead,
(void*)task
);
} else {
iResult = RecvIt(
task->sockfd,
pPtr,
sizeof(LANSCSI_H2R_PDU_HEADER),
&res,
NULL,
NULL
);
}
if (iResult == 0 && res == 0) {
// Emulator should wait forever for header.
debug_emu(1, "timeout. Wait again");
goto timeout_retry;
} else if(iResult < 0 || res != sizeof(LANSCSI_H2R_PDU_HEADER)) {
debug_emu(1, "Can't Recv Header...");
return iResult;
} else {
iTotalRecved += res;
}
if(flags & NDASEMU_RECVREQ_FLAG_LOOKAHEAD) {
return res;
}
task->pdu_req = (PNDASEMU_PDU_REQ)task->pdu_rep;
pPdu->u.pH2RHeader = (PLANSCSI_H2R_PDU_HEADER)pPtr;
pPtr += sizeof(LANSCSI_H2R_PDU_HEADER);
if(pSessionData->iSessionPhase == FLAG_FULL_FEATURE_PHASE
&& HeaderEncryptAlgo != 0) {
Decrypt32(
(unsigned char*)pPdu->u.pH2RHeader,
sizeof(LANSCSI_H2R_PDU_HEADER),
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
// Read AHS.
if(g_ntohs(pPdu->u.pH2RHeader->AHSLen) > 0) {
//debug_emu(1, "AHSLen = %d", g_ntohs(pPdu->u.pH2RHeader->AHSLen));
iResult = RecvIt(
task->sockfd,
pPtr,
g_ntohs(pPdu->u.pH2RHeader->AHSLen),
&res,
NULL,
NULL
);
if(iResult < 0 || res != g_ntohs(pPdu->u.pH2RHeader->AHSLen)) {
debug_emu(1, "Can't Recv AHS...");
return iResult;
} else
iTotalRecved += res;
pPdu->pAHS = pPtr;
pPtr += g_ntohs(pPdu->u.pH2RHeader->AHSLen);
// to support version 1.1, 2.0
if (thisHWVersion == LANSCSI_VERSION_1_1 ||
thisHWVersion == LANSCSI_VERSION_2_0) {
if (pSessionData->iSessionPhase == FLAG_FULL_FEATURE_PHASE
&& HeaderEncryptAlgo != 0) {
Decrypt32(
(unsigned char*)pPdu->pAHS,
g_ntohs(pPdu->u.pH2RHeader->AHSLen),
// sizeof(pPdu->u.pR2HHeader->AHSLen),
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
}
// end of supporting version
}
// Read Header Dig.
pPdu->pHeaderDig = NULL;
// Read Data segment.
if(g_ntohl(pPdu->u.pH2RHeader->DataSegLen) > 0) {
//debug_emu(1, "DataSegLen = %d", g_ntohl(pPdu->u.pH2RHeader->DataSegLen));
iResult = RecvIt(
task->sockfd,
pPtr,
g_ntohl(pPdu->u.pH2RHeader->DataSegLen),
&res,
NULL,
NULL
);
if(iResult < 0 || res != g_ntohl(pPdu->u.pH2RHeader->DataSegLen)) {
debug_emu(1, "Can't Recv Data segment...");
return iResult;
} else
iTotalRecved += res;
pPdu->pDataSeg = pPtr;
pPtr += g_ntohl(pPdu->u.pH2RHeader->DataSegLen);
if(pSessionData->iSessionPhase == FLAG_FULL_FEATURE_PHASE
&& DataEncryptAlgo != 0) {
Decrypt32(
(unsigned char*)pPdu->pDataSeg,
g_ntohl(pPdu->u.pH2RHeader->DataSegLen),
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
}
// Read Data Dig.
pPdu->pDataDig = NULL;
return iTotalRecved;
}
//
// Send reply
//
ndas_error_t
ndasemu_send_reply(
PSESSION_DATA pSessionData,
PLANSCSI_R2H_PDU_HEADER pReplyHeader,
PNDAS_TASK task
){
int iResult, res =0;
xuint32 iTemp = 0;
// Send Reply.
pReplyHeader->HPID = g_htonl(pSessionData->HPID);
pReplyHeader->RPID = g_htons(pSessionData->RPID);
pReplyHeader->CPSlot = g_htons(pSessionData->CPSlot);
// to support version 1.1, 2.0
if (thisHWVersion == LANSCSI_VERSION_1_0) {
pReplyHeader->AHSLen = 0;
}
if (thisHWVersion == LANSCSI_VERSION_1_1 ||
thisHWVersion == LANSCSI_VERSION_2_0) {
pReplyHeader->DataSegLen = 0;
}
// end of supporting version
// to support version 1.1, 2.0
if (thisHWVersion == LANSCSI_VERSION_1_0) {
iTemp = sizeof(LANSCSI_LOGIN_REPLY_PDU_HEADER) + g_ntohl(pReplyHeader->DataSegLen);
}
if (thisHWVersion == LANSCSI_VERSION_1_1 ||
thisHWVersion == LANSCSI_VERSION_2_0) {
iTemp = sizeof(LANSCSI_LOGIN_REPLY_PDU_HEADER) + g_ntohs(pReplyHeader->AHSLen);
}
// end of supporting version
//
// Encrypt the PDU header.
//
if(pReplyHeader->Opcode != LOGIN_RESPONSE) {
if(HeaderEncryptAlgo != 0) {
Encrypt32(
(unsigned char*)pReplyHeader,
sizeof(LANSCSI_LOGIN_REPLY_PDU_HEADER),
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
// to support version 1.1, 2.0
if (thisHWVersion == LANSCSI_VERSION_1_0) {
if(DataEncryptAlgo != 0
&& g_ntohl(pReplyHeader->DataSegLen) != 0) {
Encrypt32(
(unsigned char*)pReplyHeader + sizeof(LANSCSI_LOGIN_REPLY_PDU_HEADER),
iTemp,
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
}
if (thisHWVersion == LANSCSI_VERSION_1_1 ||
thisHWVersion == LANSCSI_VERSION_2_0) {
if(HeaderEncryptAlgo != 0
&& g_ntohs(pReplyHeader->AHSLen) != 0) {
Encrypt32(
(unsigned char*)pReplyHeader + sizeof(LANSCSI_LOGIN_REPLY_PDU_HEADER),
iTemp,
(unsigned char *)&pSessionData->CHAP_C,
(unsigned char *)&pSessionData->iPassword
);
}
}
// end of supporting version
}
//debug_emu(1, "SendIt : Send count is %d", iTemp);
if(task) {
lpx_prepare_aio(
task->sockfd,
0, // Indicate no mem block used.
task->pdurep_pages,
task->pdurep_offset,
iTemp,
0,
&task->aio);
lpx_set_comp_routine(task, ndasemu_aio_complete, &task->aio);
iResult = lpx_sendpages_aio(&task->aio);
if(iResult > 0) {
res = iTemp;
}
} else {
iResult = SendIt(
pSessionData->connSock,
pReplyHeader,
iTemp,
&res
);
}
if(iResult < 0 || res != iTemp) {
debug_emu(1, "Can't Send data body...");
pSessionData->iSessionPhase = LOGOUT_PHASE;
return NDAS_ERROR;
}
return NDAS_OK;
}
/*
* Task functions
*/
/*
Login procedure
Initiator Target
Step 1 [ CHAP_A=<A1,A2...> ] ->
<- [ CHAP_A=<A> CHAP_I=<I> CHAP_C=<C> ]
Step 3 [ CHAP_N=<N> CHAP_R=<R> ] ->
or, if it requires target authentication, with:
[ CHAP_N=<N> CHAP_R=<R> CHAP_I=<I> CHAP_C=<C> ] ->