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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
|
/* $NetBSD: isns_pdu.c,v 1.5 2021/08/21 23:00:30 andvar Exp $ */
/*-
* Copyright (c) 2004,2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* isns_pdu.c
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: isns_pdu.c,v 1.5 2021/08/21 23:00:30 andvar Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "isns.h"
#include "isns_config.h"
/*
* Local function prototypes.
*/
static struct isns_buffer_list_s *isns_lookup_buffer_list(int);
static struct isns_pdu_s *isns_init_pdu(struct isns_buffer_s *,
struct isns_config_s *, uint16_t, uint16_t, uint16_t);
static int isns_add_pdu_payload_data(struct isns_trans_s *, const void *, int);
static void isns_get_tlv_info_advance(struct isns_get_tlv_info_s *);
static int isns_get_tlv_uint32(struct isns_get_tlv_info_s *, uint32_t *);
static int isns_get_tlv_data(struct isns_get_tlv_info_s *, int, void **);
static void isns_add_pdu_list(struct isns_pdu_s **, struct isns_pdu_s *);
static struct isns_buffer_s *isns_get_pdu_head_buffer(struct isns_pdu_s *);
#if 0
static struct isns_buffer_s *isns_get_pdu_tail_buffer(struct isns_pdu_s *);
#endif
static struct isns_buffer_s *isns_get_pdu_active_buffer(struct isns_pdu_s *);
static uint32_t isns_get_next_trans_id(void);
/*
* Buffer pool structures and global (static) var.
*/
struct isns_buffer_list_s {
int buf_size;
int alloc_count;
struct isns_buffer_s *head;
struct isns_buffer_list_s *next;
};
struct isns_buffer_pool_s {
int active;
struct isns_buffer_list_s *list_p;
pthread_mutex_t mutex;
};
static struct isns_buffer_pool_s G_buffer_pool;
/*
* isns_init_buffer_pool - initialize buffer pool for use
*/
void
isns_init_buffer_pool(void)
{
pthread_mutexattr_t mutexattr;
DBG("isns_init_buffer_pool: entered\n");
assert(!G_buffer_pool.active);
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_settype(&mutexattr, ISNS_MUTEX_TYPE_NORMAL);
pthread_mutex_init(&G_buffer_pool.mutex, &mutexattr);
G_buffer_pool.active = 1;
}
/*
* isns_lookup_buffer_list - locates the pool buffer list for the buf_size
* specified.
*
* Returns: pointer to list in pool containing buf_size buffers
* NULL if no list for size indicated exists
*/
static struct isns_buffer_list_s *
isns_lookup_buffer_list(int buf_size)
{
struct isns_buffer_list_s *list_p;
/*
* WARNING: G_buffer_pool.mutex MUST already be locked.
*/
list_p = G_buffer_pool.list_p;
while (list_p != NULL) {
if (list_p->buf_size == buf_size)
break;
list_p = list_p->next;
}
return list_p;
}
/*
* isns_add_buffer_pool - allocates buffers of in pool
*
* If a list containing buf_size buffers already exists in pool, additional
* buffers are added (allocated) to the list.
*/
int
isns_add_buffer_pool(int buf_size, int count)
{
struct isns_buffer_list_s *list_p, *p, *p_next;
struct isns_buffer_s *buf_p;
int n;
DBG("isns_add_buffer_pool: buf_size=%d, count=%d\n", buf_size, count);
assert(G_buffer_pool.active);
/* Make our buffer lengths always a multiple of 4. */
buf_size = (buf_size + 0x03) & ~0x03;
/*
* Lookup existing list for size specified. If no exists, allocate
* a new list and initialize.
*/
pthread_mutex_lock(&G_buffer_pool.mutex);
list_p = isns_lookup_buffer_list(buf_size);
if (list_p == NULL) {
pthread_mutex_unlock(&G_buffer_pool.mutex);
list_p = (struct isns_buffer_list_s *)
isns_malloc(sizeof(struct isns_buffer_list_s));
if (list_p == NULL) {
DBG("isns_add_buffer_pool: error on isns_malloc()\n");
return ENOMEM;
}
list_p->buf_size = buf_size;
list_p->alloc_count = 0;
list_p->head = NULL;
}
/* If this is a new list, insert into pool in buf_size order. */
if (list_p->alloc_count == 0) {
pthread_mutex_lock(&G_buffer_pool.mutex);
if (G_buffer_pool.list_p == NULL) {
G_buffer_pool.list_p = list_p;
list_p->next = NULL;
} else if (G_buffer_pool.list_p->buf_size > buf_size) {
list_p->next = G_buffer_pool.list_p;
G_buffer_pool.list_p = list_p;
} else {
p = G_buffer_pool.list_p;
while (p->next != NULL) {
p_next = p->next;
if (p_next->buf_size > buf_size) {
p->next = list_p;
list_p->next = p_next;
break;
}
p = p->next;
}
if (p->next == NULL) {
p->next = list_p;
list_p->next = NULL;
}
}
}
/* Allocate (possibly additional) buffers for list. */
for (n = 0; n < count; n++) {
buf_p = (struct isns_buffer_s *)
isns_malloc(buf_size + sizeof(struct isns_buffer_s));
if (buf_p == NULL)
break;
buf_p->next = list_p->head;
list_p->head = buf_p;
}
list_p->alloc_count += n;
pthread_mutex_unlock(&G_buffer_pool.mutex);
DBG("isns_init_buffer_pool: %d %d-byte buffers allocated\n",
n, buf_size);
return (n > 0 ? 0 : ENOMEM);
}
/*
* isns_destroy_buffer_pool - destroys previously allocated buffer pool
*/
void
isns_destroy_buffer_pool(void)
{
struct isns_buffer_list_s *list_p;
struct isns_buffer_s *buf_p;
#ifdef ISNS_DEBUG
char dbg_buffer[1024] = { 0 };
#endif
DBG("isns_destroy_buffer_pool: entered\n");
assert(G_buffer_pool.active);
pthread_mutex_lock(&G_buffer_pool.mutex);
while (G_buffer_pool.list_p != NULL) {
list_p = G_buffer_pool.list_p;
while (list_p->head != NULL) {
buf_p = list_p->head;
list_p->head = buf_p->next;
list_p->alloc_count--;
isns_free(buf_p);
}
#ifdef ISNS_DEBUG
if (list_p->alloc_count > 0) {
snprintf(&dbg_buffer[(int) strlen(dbg_buffer)],
(sizeof(dbg_buffer) - strlen(dbg_buffer)),
"isns_destroy_buffer_pool: "
"%d %d-byte buffer(s) not freed\n",
list_p->alloc_count, list_p->buf_size);
}
#endif
G_buffer_pool.list_p = list_p->next;
isns_free(list_p);
}
G_buffer_pool.active = 0;
pthread_mutex_unlock(&G_buffer_pool.mutex);
pthread_mutex_destroy(&G_buffer_pool.mutex);
DBG(dbg_buffer);
}
/*
* isns_new_buffer - allocates a new ISNS buffer
*
* Typically, the buffer is returned from the pool, but if no free buffers
* are available in the pool, or a buf size larger than the largest pool buffer
* size is requested, a normal malloc is used to allocate the buffer. The
* buffer type is recorded so that a subsequent isns_free_buffer will correctly
* free the buffer or return it to the pool.
*/
struct isns_buffer_s *
isns_new_buffer(int buf_size)
{
struct isns_buffer_list_s *list_p;
struct isns_buffer_s *buf_p;
int buf_type;
if (buf_size == 0)
buf_size = ISNS_BUF_SIZE;
buf_p = NULL;
pthread_mutex_lock(&G_buffer_pool.mutex);
list_p = G_buffer_pool.list_p;
while (list_p != NULL) {
if ((list_p->head != NULL)
&& (list_p->buf_size >= buf_size)) {
buf_p = list_p->head;
list_p->head = buf_p->next;
buf_size = list_p->buf_size;
buf_type = ISNS_BUF_POOL;
break;
}
list_p = list_p->next;
}
pthread_mutex_unlock(&G_buffer_pool.mutex);
if (buf_p == NULL) {
buf_p = (struct isns_buffer_s *)isns_malloc(
buf_size + sizeof(struct isns_buffer_s));
buf_type = ISNS_BUF_MALLOC;
}
if (buf_p != NULL)
ISNS_INIT_BUFFER(buf_p, buf_size, buf_type);
DBG("isns_new_buffer: %p (buf_size=%d, type=%d)\n", buf_p, buf_size,
buf_type);
return buf_p;
}
/*
* isns_free_buffer - free a ISNS buffer
*/
void
isns_free_buffer(struct isns_buffer_s *buf_p)
{
struct isns_buffer_list_s *list_p;
DBG("isns_free_buffer: %p (type=%d, alloc_len=%d)\n",
buf_p, (buf_p == NULL ? 0 : buf_p->buf_type),
(buf_p == NULL ? 0 : buf_p->alloc_len));
if (buf_p != NULL) {
switch (buf_p->buf_type) {
case ISNS_BUF_POOL:
/* Return buffer to proper pool list. */
pthread_mutex_lock(&G_buffer_pool.mutex);
list_p = isns_lookup_buffer_list((int)buf_p->alloc_len);
if (list_p != NULL) {
buf_p->next = list_p->head;
list_p->head = buf_p;
}
pthread_mutex_unlock(&G_buffer_pool.mutex);
break;
case ISNS_BUF_MALLOC:
/* Malloc allocated buf, so free normally. */
isns_free(buf_p);
break;
case ISNS_BUF_STATIC:
/* Static buf with no allocation, so do nothing here. */
break;
}
}
}
/*
* isns_new_trans - create a new ISNS transaction
*/
ISNS_TRANS
isns_new_trans(ISNS_HANDLE isns_handle, uint16_t func_id, uint16_t pdu_flags)
{
struct isns_trans_s *trans_p;
struct isns_pdu_s *pdu_p;
struct isns_buffer_s *buf_p;
if (isns_handle == ISNS_INVALID_HANDLE) {
DBG("isns_new_trans: error - handle=%p\n", isns_handle);
return ISNS_INVALID_TRANS;
}
buf_p = isns_new_buffer((int)sizeof(struct isns_trans_s));
if (buf_p == NULL) {
DBG("isns_new_trans: error on isns_new_buffer()\n");
return ISNS_INVALID_TRANS;
}
trans_p = (struct isns_trans_s *)isns_buffer_data(buf_p, 0);
trans_p->id = isns_get_next_trans_id();
trans_p->func_id = func_id;
trans_p->flags = 0;
trans_p->cfg_p = (struct isns_config_s *)isns_handle;
trans_p->pdu_req_list = NULL;
trans_p->pdu_rsp_list = NULL;
trans_p->disconnect_cnt = 0;
trans_p->get_tlv_info.pdu_p = NULL;
trans_p->get_tlv_info.buf_p = NULL;
trans_p->get_tlv_info.extra_buf_list = NULL;
trans_p->get_tlv_info.buf_ofs = 0;
buf_p->cur_len = sizeof(struct isns_trans_s);
/*
* Mask off all but the AUTH and possibly REPLACE_REG pdu flags. Then,
* set the appropriate server/client sender flag. The first/last PDU
* flags will be set when the PDU is sent.
*/
if (func_id == isnsp_DevAttrReg)
pdu_flags &= (ISNS_FLAG_AUTH | ISNS_FLAG_REPLACE_REG);
else
pdu_flags &= ISNS_FLAG_AUTH;
if (trans_p->cfg_p->is_server)
pdu_flags |= ISNS_FLAG_SND_SERVER;
else
pdu_flags |= ISNS_FLAG_SND_CLIENT;
pdu_p = isns_new_pdu(trans_p->cfg_p, trans_p->id, func_id, pdu_flags);
if (pdu_p == NULL) {
DBG("isns_new_trans: error on isns_new_pdu()\n");
isns_free_buffer(buf_p);
return ISNS_INVALID_TRANS;
}
isns_add_pdu_request((ISNS_TRANS)trans_p, pdu_p);
DBG("isns_new_trans: %p\n", trans_p);
return (ISNS_TRANS)trans_p;
}
/*
* isns_free_trans - free ISNS transaction created with isns_new_trans
*/
void
isns_free_trans(ISNS_TRANS trans)
{
struct isns_trans_s *trans_p;
struct isns_pdu_s *pdu_p;
struct isns_buffer_s *buf_p, *free_buf_p;
uint32_t trans_flags;
DBG("isns_free_trans: %p\n", trans);
if (trans != ISNS_INVALID_TRANS) {
trans_p = (struct isns_trans_s *)trans;
trans_flags = isns_set_trans_flags(trans_p,
ISNS_TRANSF_FREE_WHEN_COMPLETE);
if ((trans_flags & ISNS_TRANSF_COMPLETE) == 0) {
DBG("isns_free_trans: deferred - trans not complete\n");
return;
}
DBG("isns_free_trans: pdu_req_list=%p\n",
trans_p->pdu_req_list);
while ((pdu_p = trans_p->pdu_req_list) != NULL) {
trans_p->pdu_req_list = pdu_p->next;
isns_free_pdu(pdu_p);
}
DBG("isns_free_trans: pdu_rsp_list=%p\n",
trans_p->pdu_rsp_list);
while ((pdu_p = trans_p->pdu_rsp_list) != NULL) {
trans_p->pdu_rsp_list = pdu_p->next;
isns_free_pdu(pdu_p);
}
DBG("isns_free_trans: extra_buf_list=%p\n",
trans_p->get_tlv_info.extra_buf_list);
buf_p = trans_p->get_tlv_info.extra_buf_list;
while (buf_p != NULL) {
free_buf_p = buf_p;
buf_p = buf_p->next;
isns_free_buffer(free_buf_p);
}
DBG("isns_free_trans: freeing base trans buffer\n");
buf_p = ((struct isns_buffer_s *)(void *)(trans_p))-1;
isns_free_buffer(buf_p);
}
}
/*
* isns_send_trans - send ISNS transaction PDU(s) and optionally wait
*
* If a successful wait occurs (i.e., the transaction completes without
* a timeout), then the response PDU status is place in *status_p. For
* all other cases, the data returned in *status_p is undefined.
*
*/
int
isns_send_trans(ISNS_TRANS trans, const struct timespec *timeout_p,
uint32_t *status_p)
{
struct isns_trans_s *trans_p;
struct isns_pdu_s *pdu_p;
int rval;
trans_p = (struct isns_trans_s *)trans;
DBG("isns_send_trans: trans_p=%p, timeout_p=%p\n", trans_p, timeout_p);
if (status_p != NULL)
*status_p = 0;
if (!isns_is_socket_init_done(trans_p->cfg_p)) {
DBG("isns_send_trans: socket not initialized\n");
isns_complete_trans(trans_p);
return EINVAL;
}
if ((pdu_p = isns_get_pdu_request(trans)) == NULL) {
DBG("isns_send_trans: no request PDU\n");
return EINVAL;
}
/* Set the FIRST_PDU flag in the first PDU. */
pdu_p->hdr.flags |= ISNS_FLAG_FIRST_PDU;
/* Set our PDU sequence numbers for the PDU chain. */
while (pdu_p->next != NULL) {
pdu_p->next->hdr.seq_id = pdu_p->hdr.seq_id + 1;
pdu_p = pdu_p->next;
}
/* Set the LAST_PDU flag in the last PDU. */
pdu_p->hdr.flags |= ISNS_FLAG_LAST_PDU;
rval = isns_send_pdu(trans, isns_get_pdu_request(trans), timeout_p);
if ((rval == 0) && (status_p != NULL))
isns_get_pdu_response_status(trans, status_p);
return rval;
}
void
isns_complete_trans(struct isns_trans_s *trans_p)
{
uint32_t flags;
DBG("isns_complete_trans: trans_p=%p\n", trans_p);
flags = isns_set_trans_flags(trans_p, ISNS_TRANSF_COMPLETE);
if ((flags & ISNS_TRANSF_FREE_WHEN_COMPLETE) != 0)
isns_free_trans(trans_p);
}
int
isns_abort_trans(struct isns_config_s *cfg_p, uint16_t trans_id)
{
struct isns_task_s *task_p;
/* First, look at current task. */
if (((task_p = cfg_p->curtask_p) != NULL)
&& (task_p->task_type == ISNS_TASK_SEND_PDU)
&& (task_p->var.send_pdu.trans_p->id == trans_id)) {
isns_complete_trans(task_p->var.send_pdu.trans_p);
isns_end_task(task_p);
return 0;
}
/* If not current task, look in task queue. */
task_p = isns_taskq_remove_trans(cfg_p, trans_id);
if (task_p) {
isns_complete_trans(task_p->var.send_pdu.trans_p);
isns_end_task(task_p);
return 0;
}
return EINVAL;
}
/*
* isns_add_string - add a TLV which is a C string
*
* Wrapper around isns_add_tlv()
*/
int
isns_add_string(ISNS_TRANS trans, uint32_t tag, const char *s)
{
/* Add string, including required NULL. */
return isns_add_tlv(trans, tag, (uint32_t)strlen(s) + 1, s);
}
/*
* isns_add_tlv - adds a TLV to an existing transaction
*/
int
isns_add_tlv(ISNS_TRANS trans, uint32_t tag, uint32_t data_len,
const void *data_p)
{
struct isns_trans_s *trans_p;
uint8_t tlv_buf[ISNS_TLV_HDR_SIZE];
int rval;
DBG("isns_add_tlv: trans=%p, tag=%d, data_len=%d, data_p=%p\n",
trans, tag, data_len, data_p);
if (trans == ISNS_INVALID_TRANS) {
DBG("isns_add_tlv: error - trans=%p\n", trans);
return EINVAL;
}
if ((data_len > 0) && (data_p == NULL)) {
DBG("isns_add_tlv: error data_len=%d, data_p=%p\n",
data_len, data_p);
return EINVAL;
}
/* Set tag, length in header buffer and add to PDU payload data. */
trans_p = (struct isns_trans_s *)trans;
ISNS_TLV_SET_TAG(tlv_buf, tag);
ISNS_TLV_SET_LEN(tlv_buf, ISNS_PAD4_LEN(data_len));
rval = isns_add_pdu_payload_data(trans_p, tlv_buf, ISNS_TLV_HDR_SIZE);
/* If header added okay, add value portion to PDU payload data. */
if ((rval == 0) && (data_len > 0))
rval = isns_add_pdu_payload_data(trans_p, data_p, data_len);
return rval;
}
/*
* isns_get_tlv - get TLV value from response PDU for transaction
*
* returns:
* 0 - success
* ENOENT - no (more) TLVs in this transaction
* EINVAL - invalid arg
* EPERM - operation not permitted - transaction not complete
* ENOMEM - could not allocate storage for spanning TLV data
*/
int
isns_get_tlv(ISNS_TRANS trans, int which_tlv, uint32_t *tag_p,
uint32_t *data_len_p, void **data_pp)
{
struct isns_trans_s *trans_p;
struct isns_get_tlv_info_s *info_p;
struct isns_pdu_s *pdu_p;
int rval;
if (trans == ISNS_INVALID_TRANS) {
DBG("isns_get_tlv: error - trans=%p\n", trans);
return EINVAL;
}
trans_p = (struct isns_trans_s *)trans;
if ((isns_get_trans_flags(trans_p) & ISNS_TRANSF_COMPLETE) == 0) {
DBG("isns_get_tlv: error - trans not complete\n");
return EPERM;
}
/* Get response PDU for this transaction. */
pdu_p = isns_get_pdu_response(trans_p);
if (pdu_p == NULL) {
DBG("isns_get_tlv: error - no response PDU in transaction\n");
return EINVAL;
}
if (pdu_p->payload_p->cur_len == 0) {
DBG("isns_get_tlv: error - zero length PDU payload\n");
return EINVAL;
}
/* If get_tlv_info unset, treat ISNS_TLV_NEXT as ISNS_TLV_FIRST. */
info_p = &trans_p->get_tlv_info;
if ((which_tlv == ISNS_TLV_NEXT) && (info_p->pdu_p == NULL))
which_tlv = ISNS_TLV_FIRST;
/*!!! make sure PDU uses TLVs here */
switch (which_tlv) {
case ISNS_TLV_NEXT:
/* For next TLV, nothing to do here - use get_tlv_info as-is. */
break;
case ISNS_TLV_FIRST:
/* For first TLV, reset get_tlv_info. */
info_p->pdu_p = pdu_p;
info_p->buf_p = isns_get_pdu_head_buffer(pdu_p);
info_p->buf_ofs = 4;
break;
default:
DBG("isns_get_tlv: invalid arg (which_tlv=%d)\n", which_tlv);
return EINVAL;
}
/*
* Get the type, length, and data (value) for the TLV. The get calls
* below will advance the pointers in get_tlv_info_s *info_p. Note that
* if the get of the TAG type fails, ENOENT is returned indicating that
* no more TLVs exist for this request PDU.
*/
if ((rval = isns_get_tlv_uint32(info_p, tag_p)) != 0) {
DBG("isns_get_tlv: error on isns_get_tlv_uint32() tag\n");
return ENOENT;
}
if ((rval = isns_get_tlv_uint32(info_p, (uint32_t *)data_len_p)) != 0) {
DBG("isns_get_tlv: error on isns_get_tlv_uint32() data len\n");
return rval;
}
rval = isns_get_tlv_data(info_p, *data_len_p, data_pp);
if (rval != 0) {
DBG("isns_get_tlv: error on isns_get_tlv_data()\n");
return rval;
}
return 0;
}
/*
* isns_set_trans_flags - sets flag bit(s) in transaction flags member
*/
uint32_t
isns_set_trans_flags(struct isns_trans_s *trans_p, uint32_t flags)
{
pthread_mutex_lock(&trans_p->cfg_p->trans_mutex);
trans_p->flags |= flags;
flags = trans_p->flags;
pthread_mutex_unlock(&trans_p->cfg_p->trans_mutex);
return flags;
}
/*
* isns_add_pdu_request - adds PDU to transaction request PDU list
*/
void
isns_add_pdu_request(struct isns_trans_s *trans_p, struct isns_pdu_s *pdu_p)
{
DBG("isns_add_pdu_request: trans_p=%p, pdu_p=%p\n", trans_p, pdu_p);
isns_add_pdu_list(&trans_p->pdu_req_list, pdu_p);
}
/*
* isns_add_pdu_response - adds PDU to transaction response PDU list
*/
void
isns_add_pdu_response(struct isns_trans_s *trans_p, struct isns_pdu_s *pdu_p)
{
DBG("isns_add_pdu_response: trans_p=%p, pdu_p=%p\n", trans_p, pdu_p);
isns_add_pdu_list(&trans_p->pdu_rsp_list, pdu_p);
}
/*
* isns_get_pdu_request_tail - returns last PDU in request PDU chain
*/
struct isns_pdu_s *
isns_get_pdu_request_tail(struct isns_trans_s *trans_p)
{
struct isns_pdu_s *pdu_p;
if ((pdu_p = isns_get_pdu_request(trans_p)) != NULL) {
while (pdu_p->next != NULL)
pdu_p = pdu_p->next;
}
return pdu_p;
}
/*
* isns_new_pdu - allocates a new PDU and assigns function ID and flags
*/
struct isns_pdu_s *
isns_new_pdu(struct isns_config_s *cfg_p, uint16_t trans_id, uint16_t func_id,
uint16_t flags)
{
struct isns_buffer_s *buf_p;
/*
* Allocate a buffer at least large enough for our isns_pdu_s struct
* and the embedded isns_buffer_s struct for the PDU payload data and 4
* bytes of actual payload data.
*/
buf_p = isns_new_buffer(/* CONSTCOND */(int)MAX(ISNS_BUF_SIZE,
sizeof(struct isns_pdu_s) + sizeof(struct isns_buffer_s) + 4));
if (buf_p == NULL) {
DBG("isns_new_pdu: error on isns_new_buffer()\n");
return NULL;
}
return isns_init_pdu(buf_p, cfg_p, trans_id, func_id, flags);
}
/*
* isns_free_pdu - frees a PDU and all associated buffers
*/
void
isns_free_pdu(struct isns_pdu_s *pdu_p)
{
struct isns_buffer_s *buf_p, *free_buf_p;
DBG("isns_free_pdu: %p\n", pdu_p);
if (pdu_p != NULL) {
/* Free all payload buffers. */
buf_p = pdu_p->payload_p;
while (buf_p != NULL) {
free_buf_p = buf_p;
buf_p = buf_p->next;
isns_free_buffer(free_buf_p);
}
/*
* Get a pointer to the ISNS buffer in which this PDU is
* contained, and then free it.
*/
buf_p = ((struct isns_buffer_s *)(void *)(pdu_p))-1 ;
isns_free_buffer(buf_p);
}
}
/*
* isns_send_pdu - initiates the send PDU task
*/
int
isns_send_pdu(ISNS_TRANS trans, struct isns_pdu_s *pdu_p,
const struct timespec *timeout_p)
{
struct isns_trans_s *trans_p;
struct isns_task_s* task_p;
int rval;
if (trans == ISNS_INVALID_TRANS) {
DBG("isns_send_pdu: error - trans=%p\n", trans);
return EINVAL;
}
if (pdu_p == NULL) {
DBG("isns_send_pdu: error - pdu_p=%p\n", pdu_p);
return EINVAL;
}
trans_p = (struct isns_trans_s *)trans;
/* Build SEND_PDU task, insert on queue and issue command. */
task_p = isns_new_task(pdu_p->cfg_p, ISNS_TASK_SEND_PDU,
(timeout_p != NULL));
task_p->var.send_pdu.trans_p = trans_p;
task_p->var.send_pdu.pdu_p = pdu_p;
isns_taskq_insert_tail(pdu_p->cfg_p, task_p);
isns_issue_cmd(pdu_p->cfg_p, ISNS_CMD_PROCESS_TASKQ);
if (timeout_p == NULL)
rval = 0;
else {
rval = isns_wait_task(task_p, timeout_p);
if (rval == ETIMEDOUT) {
DBG("isns_send_pdu: "
"timeout on isns_wait_task() trans_id=%d\n",
trans_p->id);
isns_issue_cmd_with_data(task_p->cfg_p,
ISNS_CMD_ABORT_TRANS, (void *)&trans_p->id,
(int)sizeof(trans_p->id));
}
}
return rval;
}
/*
* isns_init_pdu - initialize ISNS buffer to be a PDU
*/
static struct isns_pdu_s *
isns_init_pdu(struct isns_buffer_s *buf_p, struct isns_config_s *cfg_p,
uint16_t trans_id, uint16_t func_id, uint16_t flags)
{
struct isns_pdu_s *pdu_p;
/* The config and buffer pointers must be valid here. */
assert(cfg_p != NULL);
assert(buf_p != NULL);
/* The PDU starts at offset 0 for the ISNS buffer data. */
pdu_p = isns_buffer_data(buf_p, 0);
buf_p->cur_len = sizeof(struct isns_pdu_s);
/* Assign PDU members. */
pdu_p->cfg_p = cfg_p;
pdu_p->hdr.isnsp_version = ISNSP_VERSION;
pdu_p->hdr.func_id = func_id;
pdu_p->hdr.payload_len = 0;
pdu_p->hdr.flags = flags;
pdu_p->hdr.trans_id = trans_id;
pdu_p->hdr.seq_id = 0;
pdu_p->byteorder_host = 1;
pdu_p->next = NULL;
/*
* The PDU payload buffer starts after the PDU struct portion in the
* ISNS buffer passed in to this function. So, assign the payload_p
* pointer accordingly, and then init the buffer with the proper length
* and ISNS_BUF_STATIC type.
*/
pdu_p->payload_p = (struct isns_buffer_s *)
isns_buffer_data(buf_p, buf_p->cur_len);
ISNS_INIT_BUFFER(pdu_p->payload_p, (unsigned)(buf_p->alloc_len -
sizeof(struct isns_pdu_s) - sizeof(struct isns_buffer_s)),
ISNS_BUF_STATIC);
DBG("isns_init_pdu: %p\n", pdu_p);
return pdu_p;
}
/*
* isns_add_pdu_payload_data - add data to PDU payload
*/
static int
isns_add_pdu_payload_data(struct isns_trans_s *trans_p, const void *data_p,
int len)
{
struct isns_pdu_s *pdu_p, *new_pdu_p;
struct isns_buffer_s *buf_p, *new_buf_p;
const uint8_t *src_p;
uint8_t *dst_p;
int pad_bytes;
/* Get the request PDU for this transaction. */
if ((pdu_p = isns_get_pdu_request_tail(trans_p)) == NULL) {
DBG("isns_add_pdu_payload_data: no request PDU\n");
return EINVAL;
}
/* Get the active buffer for this PDU (where data should be copied). */
buf_p = isns_get_pdu_active_buffer(pdu_p);
/* Set up source and destination pointers. Calculate pad bytes. */
src_p = data_p;
dst_p = isns_buffer_data(buf_p, buf_p->cur_len);
pad_bytes = ISNS_PAD4_BYTES(len);
/*
* Move data from source to PDU buffer(s), allocated new pdus and
* buffers as necessary to accommodate the data.
*/
while (len--) {
/* If at max for one PDU payload, add a new PDU. */
if (pdu_p->hdr.payload_len == ISNS_MAX_PDU_PAYLOAD) {
new_pdu_p = isns_new_pdu(trans_p->cfg_p,
trans_p->id, trans_p->func_id, pdu_p->hdr.flags);
if (new_pdu_p == NULL)
return ENOMEM;
isns_add_pdu_request(trans_p, new_pdu_p);
pdu_p = new_pdu_p;
buf_p = pdu_p->payload_p;
dst_p = isns_buffer_data(buf_p, 0);
}
/* If at end of current buffer, add a new buffer. */
if (buf_p->cur_len == buf_p->alloc_len) {
if (buf_p->next != NULL)
buf_p = buf_p->next;
else {
new_buf_p = isns_new_buffer(0);
if (new_buf_p == NULL)
return ENOMEM;
buf_p->next = new_buf_p;
buf_p = new_buf_p;
}
dst_p = isns_buffer_data(buf_p, 0);
}
pdu_p->hdr.payload_len++;
buf_p->cur_len++;
*dst_p++ = *src_p++;
}
/*
* Since the buffer alloc length is always a multiple of 4, we are
* guaranteed to have enough room for the pad bytes.
*/
if (pad_bytes > 0) {
pdu_p->hdr.payload_len += pad_bytes;
buf_p->cur_len += pad_bytes;
while (pad_bytes--)
*dst_p++ = 0;
}
return 0;
}
/*
* isns_get_tlv_info_advance - advances pdu/buffer pointers if at end of
* current buffer.
*/
static void
isns_get_tlv_info_advance(struct isns_get_tlv_info_s *info_p)
{
if ((info_p->buf_p != NULL) &&
(info_p->buf_ofs == (int)info_p->buf_p->cur_len)) {
info_p->buf_p = info_p->buf_p->next;
info_p->buf_ofs = 0;
}
if ((info_p->buf_p == NULL) && (info_p->pdu_p->next != NULL)) {
info_p->pdu_p = info_p->pdu_p->next;
info_p->buf_p = isns_get_pdu_head_buffer(info_p->pdu_p);
info_p->buf_ofs = 0;
}
}
/*
* isns_get_tlv_uint32 - retrieve host-ordered uint32_t from PDU buffer at
* starting offset and adjusts isns_get_tlv_info
* pointers accordingly.
*/
static int
isns_get_tlv_uint32(struct isns_get_tlv_info_s *info_p, uint32_t *uint32_p)
{
/* Advance to next buffer/pdu (if necessary). */
isns_get_tlv_info_advance(info_p);
if ((info_p->buf_p == NULL) ||
((info_p->buf_ofs + 4) > (int)info_p->buf_p->cur_len)) {
DBG("isns_get_tlv_uint32: end of buffer reached\n");
return EFAULT;
}
*uint32_p = ntohl(*(uint32_t *)isns_buffer_data(info_p->buf_p,
info_p->buf_ofs));
info_p->buf_ofs += 4;
return 0;
}
/*
* isns_get_tlv_data - retrieves data from PDU buffer at starting offset
* for TLV data contained in specified isns_get_tlv_info.
*/
static int
isns_get_tlv_data(struct isns_get_tlv_info_s *info_p, int data_len,
void **data_pp)
{
struct isns_buffer_s *extra_buf_p;
struct isns_get_tlv_info_s gti;
uint8_t *data_p, *extra_data_p;
int bytes_remaining, cbytes;
/* First, NULL return data pointer. */
*data_pp = NULL;
/* Advance to next buffer/pdu (if necessary). */
isns_get_tlv_info_advance(info_p);
/* Make sure we have a current get tlv info buffer. */
if (info_p->buf_p == NULL) {
DBG("isns_get_tlv_data: no next buffer\n");
return EFAULT;
}
/* Get pointer into buffer where desired TLV resides. */
data_p = isns_buffer_data(info_p->buf_p, info_p->buf_ofs);
/* TLV data completely resides in current buffer. */
if ((info_p->buf_ofs + data_len) <= (int)info_p->buf_p->cur_len) {
info_p->buf_ofs += data_len;
*data_pp = data_p;
return 0;
}
/*
* TLV data extends into next buffer so an "extra" buffer is allocated
* that is large enough to hold the entire data value. The extra buffer
* is added to the transaction's extra buffer list so we can free it
* when the transaction is freed.
*/
if ((extra_buf_p = isns_new_buffer(data_len)) == NULL) {
DBG("isns_get_tlv_data: error on isns_new_buffer()\n");
return ENOMEM;
}
if (info_p->extra_buf_list == NULL)
info_p->extra_buf_list = extra_buf_p;
else {
extra_buf_p->next = info_p->extra_buf_list;
info_p->extra_buf_list = extra_buf_p;
}
/* Setup to copy data bytes out to extra buffer. */
gti = *info_p;
extra_data_p = isns_buffer_data(extra_buf_p, 0);
bytes_remaining = data_len;
while (bytes_remaining > 0) {
/*
* Advance to next buffer/pdu (if necessary), using local copy
* of the get_tlv_info structure.
*/
isns_get_tlv_info_advance(>i);
if (gti.buf_p == NULL) {
DBG("isns_get_tlv_data: no next buffer\n");
return EFAULT;
}
data_p = isns_buffer_data(gti.buf_p, gti.buf_ofs);
cbytes = MIN(bytes_remaining, ((int)gti.buf_p->cur_len - gti.buf_ofs));
bytes_remaining -= cbytes;
gti.buf_ofs += cbytes;
while (cbytes--)
*extra_data_p++ = *data_p++;
}
/* Update isns_get_tlv_info with our local copy. */
*info_p = gti;
/* Assign return data pointer. */
*data_pp = isns_buffer_data(extra_buf_p, 0);
return 0;
}
/*
* isns_get_pdu_response_status - returns status in PDU response
*
* Returns: 0 - success
* EPERM - operation not permitted, trans not complete
* EINVAL - invalid trans PDU response/payload
*/
int
isns_get_pdu_response_status(struct isns_trans_s *trans_p, uint32_t *status_p)
{
struct isns_pdu_s *pdu_p;
if ((isns_get_trans_flags(trans_p) & ISNS_TRANSF_COMPLETE) == 0)
return EPERM;
pdu_p = isns_get_pdu_response(trans_p);
if ((pdu_p == NULL)
|| (pdu_p->payload_p == NULL)
|| (pdu_p->payload_p->cur_len < 4))
return EINVAL;
*status_p = htonl(*(uint32_t *)isns_buffer_data(pdu_p->payload_p, 0));
return 0;
}
/*
* isns_add_pdu_list - adds pdu to specified pdu list
*/
static void
isns_add_pdu_list(struct isns_pdu_s **list_pp, struct isns_pdu_s *pdu_p)
{
struct isns_pdu_s *p, *p_prev;
if (*list_pp == NULL) {
*list_pp = pdu_p;
pdu_p->next = NULL;
return;
}
p = *list_pp;
while (p != NULL) {
if (pdu_p->hdr.seq_id < p->hdr.seq_id) {
if (p == *list_pp) {
*list_pp = pdu_p;
pdu_p->next = p;
} else {
p_prev = *list_pp;
while (p_prev->next != p)
p_prev = p_prev->next;
p_prev->next = pdu_p;
pdu_p->next = p;
}
return;
}
p = p->next;
}
/* pdu_p->hdr.seq_id > hdr.seq_id of all list elements */
p = *list_pp;
while (p->next != NULL)
p = p->next;
p->next = pdu_p;
pdu_p->next = NULL;
}
/*
* isns_get_pdu_head_buffer - returns PDU payload head buffer
*/
static struct isns_buffer_s *
isns_get_pdu_head_buffer(struct isns_pdu_s *pdu_p)
{
return pdu_p->payload_p;
}
#if 0
/*
* isns_get_pdu_tail_buffer - returns PDU payload tail buffer
*/
static struct isns_buffer_s *
isns_get_pdu_tail_buffer(struct isns_pdu_s *pdu_p)
{
struct isns_buffer_s *buf_p;
buf_p = pdu_p->payload_p;
if (buf_p != NULL)
while (buf_p->next != NULL) buf_p = buf_p->next;
return buf_p;
}
#endif
/*
* isns_get_pdu_active_buffer - returns PDU payload "active buffer where the
* next TLV/data should be written
*/
static struct isns_buffer_s *
isns_get_pdu_active_buffer(struct isns_pdu_s *pdu_p)
{
struct isns_buffer_s *buf_p;
buf_p = pdu_p->payload_p;
while ((buf_p->next != NULL) && (buf_p->cur_len == buf_p->alloc_len)) {
buf_p = buf_p->next;
}
return buf_p;
}
/*
* isns_get_next_trans_id - returns next ISNS transaction ID to use
*/
static uint32_t
isns_get_next_trans_id(void)
{
static int trans_id = 1;
return trans_id++;
}
#ifdef ISNS_DEBUG
/*
* isns_dump_pdu - dumps PDU contents
*/
void
isns_dump_pdu(struct isns_pdu_s *pdu_p)
{
int n, pos;
struct isns_buffer_s *buf_p;
uint8_t *p;
char text[17];
if (pdu_p == NULL) {
DBG("isns_dump_pdu: pdu_p is NULL\n");
return;
}
DBG("pdu header:\n");
if (pdu_p->byteorder_host) {
DBG("ver=0x%04X, func=%d(%s), len=%d, flags=0x%04X, trans=%d, "
"seq=%d\n",
pdu_p->hdr.isnsp_version,
pdu_p->hdr.func_id & ~0x8000,
(pdu_p->hdr.func_id & 0x8000 ? "rsp" : "req"),
pdu_p->hdr.payload_len,
pdu_p->hdr.flags,
pdu_p->hdr.trans_id,
pdu_p->hdr.seq_id);
} else {
DBG("ver=0x%04X, func=%d(%s), len=%d, flags=0x%04X, trans=%d, "
"seq=%d\n",
isns_ntohs(pdu_p->hdr.isnsp_version),
isns_ntohs(pdu_p->hdr.func_id) & ~0x8000,
(pdu_p->hdr.func_id & 0x0080 ? "rsp" : "req"),
isns_ntohs(pdu_p->hdr.payload_len),
isns_ntohs(pdu_p->hdr.flags),
isns_ntohs(pdu_p->hdr.trans_id),
isns_ntohs(pdu_p->hdr.seq_id));
}
DBG("pdu buffers:\n");
buf_p = pdu_p->payload_p;
while (buf_p != NULL) {
DBG("[%p]: alloc_len=%d, cur_len=%d\n",
buf_p, buf_p->alloc_len, buf_p->cur_len);
buf_p = buf_p->next;
}
DBG("pdu payload:\n");
buf_p = pdu_p->payload_p;
if (buf_p == NULL) {
DBG("<none>\n");
return;
}
pos = 0;
memset(text, 0, 17);
while (buf_p != NULL) {
p = isns_buffer_data(buf_p, 0);
for (n = 0; n < buf_p->cur_len; n++) {
DBG("%02X ", *p);
text[pos] = (isprint(*p) ? *p : '.');
pos++;
p++;
if ((pos % 16) == 0) {
DBG(" %s\n", text);
memset(text, 0, 17);
pos = 0;
}
}
buf_p = buf_p->next;
}
if ((pos % 16) != 0)
DBG("%*c %s\n", (16 - (pos % 16)) * 3, ' ', text);
}
#endif /* ISNS_DEBUG */
|