summaryrefslogtreecommitdiff
path: root/usr/src/cmd/isns/isnsd/func.c
blob: 3db0cae5a44abe1f52ca8c607a4ee208ecb2972e (plain)
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
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "isns_server.h"
#include "isns_msgq.h"
#include "isns_func.h"
#include "isns_cache.h"
#include "isns_obj.h"
#include "isns_dd.h"
#include "isns_pdu.h"
#include "isns_qry.h"
#include "isns_scn.h"
#include "isns_utils.h"
#include "isns_cfg.h"
#include "isns_esi.h"
#include "isns_provider.h"
#include "isns_log.h"

/*
 * extern global variables
 */
#ifdef DEBUG
extern int verbose_mc;
extern int verbose_tc;
#endif
extern const int NUM_OF_ATTRS[MAX_OBJ_TYPE_FOR_SIZE];
extern const int NUM_OF_CHILD[MAX_OBJ_TYPE];
extern const int TYPE_OF_PARENT[MAX_OBJ_TYPE_FOR_SIZE];
extern const int UID_ATTR_INDEX[MAX_OBJ_TYPE_FOR_SIZE];
extern const int TAG_RANGE[MAX_OBJ_TYPE][3];

/* scn message queue */
extern msg_queue_t *scn_q;

/*
 * extern functions.
 */

/*
 * local variables
 */

/*
 * local functions.
 */
static int dev_attr_reg(conn_arg_t *);
static int dev_attr_qry(conn_arg_t *);
static int dev_get_next(conn_arg_t *);
static int dev_dereg(conn_arg_t *);
static int scn_reg(conn_arg_t *);
static int scn_dereg(conn_arg_t *);
static int dd_reg(conn_arg_t *);
static int dd_dereg(conn_arg_t *);
static int dds_reg(conn_arg_t *);
static int dds_dereg(conn_arg_t *);
static int msg_error(conn_arg_t *);

/*
 * ****************************************************************************
 *
 * packet_get_source:
 *	get the source attributes of the packet.
 *
 * conn	- the argument of the connection.
 * return - error code.
 *
 * ****************************************************************************
 */
static int
packet_get_source(conn_arg_t *conn)
{
	int ec = 0;

	isns_pdu_t *pdu = conn->in_packet.pdu;
	isns_tlv_t *source = pdu_get_source(pdu);

	if (source == NULL) {
		ec = ISNS_RSP_SRC_ABSENT;
	} else if (source->attr_id != ISNS_ISCSI_NAME_ATTR_ID ||
	    source->attr_len == 0) {
		ec = ISNS_RSP_SRC_UNKNOWN;
	}

	if (ec == 0) {
		conn->in_packet.source = source;
	}

	return (ec);
}

/*
 * ****************************************************************************
 *
 * packet_get_key:
 *	get the key attributes of the packet.
 *
 * conn	- the argument of the connection.
 * return - error code.
 *
 * ****************************************************************************
 */
static int
packet_get_key(conn_arg_t *conn)
{
	int ec = 0;

	isns_pdu_t *pdu = conn->in_packet.pdu;
	isns_tlv_t *key;
	size_t key_len;

	key = pdu_get_key(pdu, &key_len);

	conn->in_packet.key = key;
	conn->in_packet.key_len = key_len;

	return (ec);
}

/*
 * ****************************************************************************
 *
 * packet_get_operand:
 *	get the operating attributes of the packet.
 *
 * conn	- the argument of the connection.
 * return - error code.
 *
 * ****************************************************************************
 */
static int
packet_get_operand(conn_arg_t *conn)
{
	int ec = 0;

	isns_pdu_t *pdu = conn->in_packet.pdu;
	isns_tlv_t *op;
	size_t op_len;

	op = pdu_get_operand(pdu, &op_len);

	conn->in_packet.op = op;
	conn->in_packet.op_len = op_len;

	return (ec);
}

/*
 * ****************************************************************************
 *
 * packet_split_verify:
 *	split and verify the packet, get the apporiate locking type and
 *	function handler for the packet.
 *
 * conn	- the argument of the connection.
 * return - error code.
 *
 * ****************************************************************************
 */
int
packet_split_verify(conn_arg_t *conn)
{
	int ec = 0;

	isns_pdu_t *pdu = conn->in_packet.pdu;

	int (*handler)(conn_arg_t *) = msg_error;
	int lock = CACHE_NO_ACTION;

	if (pdu->version != ISNSP_VERSION) {
		ec = ISNS_RSP_VER_NOT_SUPPORTED;
	} else {
		switch (pdu->func_id) {
		case ISNS_DEV_ATTR_REG:
			lock = CACHE_WRITE;
			handler = dev_attr_reg;
			break;
		case ISNS_DEV_ATTR_QRY:
			lock = CACHE_READ;
			handler = dev_attr_qry;
			break;
		case ISNS_DEV_GET_NEXT:
			lock = CACHE_READ;
			handler = dev_get_next;
			break;
		case ISNS_DEV_DEREG:
			lock = CACHE_WRITE;
			handler = dev_dereg;
			break;
		case ISNS_SCN_REG:
			if (scn_q != NULL) {
				lock = CACHE_WRITE;
				handler = scn_reg;
			} else {
				ec = ISNS_RSP_SCN_REGIS_REJECTED;
			}
			break;
		case ISNS_SCN_DEREG:
			if (scn_q != NULL) {
				lock = CACHE_WRITE;
				handler = scn_dereg;
			} else {
				ec = ISNS_RSP_SCN_REGIS_REJECTED;
			}
			break;
		case ISNS_SCN_EVENT:
			ec = ISNS_RSP_MSG_NOT_SUPPORTED;
			break;
		case ISNS_DD_REG:
			lock = CACHE_WRITE;
			handler = dd_reg;
			break;
		case ISNS_DD_DEREG:
			lock = CACHE_WRITE;
			handler = dd_dereg;
			break;
		case ISNS_DDS_REG:
			lock = CACHE_WRITE;
			handler = dds_reg;
			break;
		case ISNS_DDS_DEREG:
			lock = CACHE_WRITE;
			handler = dds_dereg;
			break;
		default:
			ec = ISNS_RSP_MSG_NOT_SUPPORTED;
			break;
		}
	}

	if (ISNS_OPERATION_TYPE_ENABLED()) {
		char buf[INET6_ADDRSTRLEN];
		struct sockaddr_storage *ssp = &conn->ss;
		struct sockaddr_in *sinp = (struct sockaddr_in *)ssp;
		if (ssp->ss_family == AF_INET) {
			(void) inet_ntop(AF_INET, (void *)&(sinp->sin_addr),
			    buf, sizeof (buf));
		} else {
			(void) inet_ntop(AF_INET6, (void *)&(sinp->sin_addr),
			    buf, sizeof (buf));
		}
		ISNS_OPERATION_TYPE((uintptr_t)buf, pdu->func_id);
	}

	conn->lock = lock;
	conn->handler = handler;

	/* packet split & verify */
	if (ec == 0) {
		ec = packet_get_source(conn);
		if (ec == 0) {
			ec = packet_get_key(conn);
			if (ec == 0) {
				ec = packet_get_operand(conn);
			}
		}
	}

	conn->ec = ec;

	return (ec);
}

/*
 * ****************************************************************************
 *
 * setup_key_lcp:
 *	setup the lookup control data for looking up the object
 *	which the key attributes identify.
 *
 * lcp	- the pointer of the lookup control data.
 * key	- the key attributes.
 * key_len	- the length of the key attributes.
 * return	- the pointer of the lookup control data or
 *		  NULL if there is an error.
 *
 * ****************************************************************************
 */
static int
setup_key_lcp(lookup_ctrl_t *lcp, isns_tlv_t *key, uint16_t key_len)
{
	int ec = 0;

	uint8_t *value = &key->attr_value[0];

	lcp->curr_uid = 0;
	lcp->op[0] = 0;

	switch (key->attr_id) {
	case ISNS_EID_ATTR_ID:
		if (key->attr_len >= 4) {
			lcp->type = OBJ_ENTITY;
			lcp->id[0] = ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID);
			lcp->op[0] = OP_STRING;
			lcp->data[0].ptr = (uchar_t *)value;
			lcp->op[1] = 0;
		}
		break;
	case ISNS_ISCSI_NAME_ATTR_ID:
		if (key->attr_len >= 4) {
			lcp->type = OBJ_ISCSI;
			lcp->id[0] = ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID);
			lcp->op[0] = OP_STRING;
			lcp->data[0].ptr = (uchar_t *)value;
			lcp->op[1] = 0;
		} else {
			ec = ISNS_RSP_MSG_FORMAT_ERROR;
		}
		break;
	case ISNS_PORTAL_IP_ADDR_ATTR_ID:
		if (key->attr_len == sizeof (in6_addr_t)) {
			lcp->id[0] = ATTR_INDEX_PORTAL(
			    ISNS_PORTAL_IP_ADDR_ATTR_ID);
			lcp->op[0] = OP_MEMORY_IP6;
			lcp->data[0].ip = (in6_addr_t *)value;
			NEXT_TLV(key, key_len);
			if (key_len <= 8 ||
			    key->attr_len != 4 ||
			    key->attr_id != ISNS_PORTAL_PORT_ATTR_ID) {
				return (ISNS_RSP_MSG_FORMAT_ERROR);
			}
			lcp->type = OBJ_PORTAL;
			value = &key->attr_value[0];
			lcp->id[1] = ATTR_INDEX_PORTAL(
			    ISNS_PORTAL_PORT_ATTR_ID);
			lcp->op[1] = OP_INTEGER;
			lcp->data[1].ui = ntohl(*(uint32_t *)value);
			lcp->op[2] = 0;
		} else {
			ec = ISNS_RSP_MSG_FORMAT_ERROR;
		}
		break;
	case ISNS_PG_ISCSI_NAME_ATTR_ID:
		if (key->attr_len < 4) {
			return (ISNS_RSP_MSG_FORMAT_ERROR);
		}
		lcp->id[0] = ATTR_INDEX_PG(ISNS_PG_ISCSI_NAME_ATTR_ID);
		lcp->op[0] = OP_STRING;
		lcp->data[0].ptr = (uchar_t *)value;
		NEXT_TLV(key, key_len);
		if (key_len <= 8 ||
		    key->attr_len != sizeof (in6_addr_t) ||
		    key->attr_id != ISNS_PG_PORTAL_IP_ADDR_ATTR_ID) {
			return (ISNS_RSP_MSG_FORMAT_ERROR);
		}
		value = &key->attr_value[0];
		lcp->id[1] = ATTR_INDEX_PG(ISNS_PG_PORTAL_IP_ADDR_ATTR_ID);
		lcp->op[1] = OP_MEMORY_IP6;
		lcp->data[1].ip = (in6_addr_t *)value;
		NEXT_TLV(key, key_len);
		if (key_len <= 8 ||
		    key->attr_len != 4 ||
		    key->attr_id != ISNS_PG_PORTAL_PORT_ATTR_ID) {
			return (ISNS_RSP_MSG_FORMAT_ERROR);
		}
		value = &key->attr_value[0];
		lcp->id[2] = ATTR_INDEX_PG(ISNS_PG_PORTAL_PORT_ATTR_ID);
		lcp->op[2] = OP_INTEGER;
		lcp->data[2].ui = ntohl(*(uint32_t *)value);
		lcp->type = OBJ_PG;
		break;
	default:
		lcp->type = 0; /* invalid */
		ec = ISNS_RSP_MSG_FORMAT_ERROR;
	}

	return (ec);
}

/*
 * ****************************************************************************
 *
 * rsp_add_op:
 *	add the operating attributes to the response packet.
 *
 * conn	- the argument of the connection.
 * obj	- the object which is being added as operating attributes.
 * return - error code.
 *
 * ****************************************************************************
 */
static int
rsp_add_op(conn_arg_t *conn, isns_obj_t *obj)
{
	int ec = 0;

	isns_attr_t *attr;
	int i;

	isns_pdu_t *rsp = conn->out_packet.pdu;
	size_t pl = conn->out_packet.pl;
	size_t sz = conn->out_packet.sz;

	i = 0;
	while (i < NUM_OF_ATTRS[obj->type] &&
	    ec == 0) {
		attr = &obj->attrs[i];
		/* there is an attribute, send it back */
		if (attr->tag != 0) {
			ec = pdu_add_tlv(&rsp, &pl, &sz,
			    attr->tag, attr->len,
			    (void *)attr->value.ptr, 0);
		}
		i ++;
	}

	conn->out_packet.pdu = rsp;
	conn->out_packet.pl = pl;
	conn->out_packet.sz = sz;

	return (ec);
}

/*
 * ****************************************************************************
 *
 * rsp_add_key:
 *	add the key attributes to the response packet.
 *
 * conn	- the argument of the connection.
 * entity - the object which is being added as key attributes.
 * return - error code.
 *
 * ****************************************************************************
 */
static int
rsp_add_key(conn_arg_t *conn, isns_obj_t *entity)
{
	int ec = 0;

	isns_tlv_t *key = conn->in_packet.key;
	size_t key_len = conn->in_packet.key_len;
	uint32_t tag = ISNS_EID_ATTR_ID;
	isns_attr_t *attr = &entity->attrs[ATTR_INDEX_ENTITY(tag)];
	uint32_t len = attr->len;

	isns_pdu_t *rsp = conn->out_packet.pdu;
	size_t pl = conn->out_packet.pl;
	size_t sz = conn->out_packet.sz;

	if (key_len == 0) {
		ec = pdu_add_tlv(&rsp, &pl, &sz,
		    tag, len, (void *)attr->value.ptr, 0);
	} else {
		while (key_len >= 8 &&
		    ec == 0) {
			if (key->attr_id == ISNS_EID_ATTR_ID) {
				ec = pdu_add_tlv(&rsp, &pl, &sz,
				    tag, len,
				    (void *)attr->value.ptr, 0);
			} else {
				ec = pdu_add_tlv(&rsp, &pl, &sz,
				    key->attr_id, key->attr_len,
				    (void *)key->attr_value, 1);
			}
			NEXT_TLV(key, key_len);
		}
	}

	if (ec == 0) {
		ec = pdu_add_tlv(&rsp, &pl, &sz,
		    ISNS_DELIMITER_ATTR_ID, 0, NULL, 0);
	}

	conn->out_packet.pdu = rsp;
	conn->out_packet.pl = pl;
	conn->out_packet.sz = sz;

	if (ec == 0) {
		ec = rsp_add_op(conn, entity);
	}

	return (ec);
}

/*
 * ****************************************************************************
 *
 * rsp_add_tlv:
 *	add one attribute with TLV format to the response packet.
 *
 * conn	- the argument of the connection.
 * tag	- the tag of the attribute.
 * len	- the length of the attribute.
 * value- the value of the attribute.
 * pflag- the flag of the value, 0: value; 1: pointer to value
 * return - error code.
 *
 * ****************************************************************************
 */
static int
rsp_add_tlv(conn_arg_t *conn, uint32_t tag, uint32_t len, void *value,
    int pflag)
{
	int ec = 0;

	isns_pdu_t *rsp = conn->out_packet.pdu;
	size_t pl = conn->out_packet.pl;
	size_t sz = conn->out_packet.sz;

	ec = pdu_add_tlv(&rsp, &pl, &sz, tag, len, value, pflag);

	conn->out_packet.pdu = rsp;
	conn->out_packet.pl = pl;
	conn->out_packet.sz = sz;

	return (ec);
}

/*
 * ****************************************************************************
 *
 * rsp_add_tlvs:
 *	add attributes with TLV format to the response packet.
 *
 * conn	- the argument of the connection.
 * tlv	- the attributes with TLV format being added.
 * tlv_len - the length of the attributes.
 * return - error code.
 *
 * ****************************************************************************
 */
static int
rsp_add_tlvs(conn_arg_t *conn, isns_tlv_t *tlv, uint32_t tlv_len)
{
	int ec = 0;

	uint32_t tag;
	uint32_t len;
	void *value;

	while (tlv_len >= 8 &&
	    ec == 0) {
		tag = tlv->attr_id;
		len = tlv->attr_len;
		value = (void *)tlv->attr_value;

		ec = rsp_add_tlv(conn, tag, len, value, 1);

		NEXT_TLV(tlv, tlv_len);
	}

	return (ec);
}

/*
 * ****************************************************************************
 *
 * dev_attr_reg:
 *	function which handles the isnsp DEV_ATTR_REG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dev_attr_reg(conn_arg_t *conn)
{
	int ec = 0;

	isns_pdu_t *pdu = conn->in_packet.pdu;
	isns_tlv_t *source = conn->in_packet.source;
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	boolean_t replace =
	    ((pdu->flags & ISNS_FLAG_REPLACE_REG) == ISNS_FLAG_REPLACE_REG);

	lookup_ctrl_t lc, lc_key;
	uchar_t *iscsi_name;
	int ctrl;

	isns_obj_t *ety = NULL;	/* network entity object */
	isns_type_t ptype;	/* parent object type */
	uint32_t puid;		/* parent object UID */
	void const **child[MAX_CHILD_TYPE] = { NULL };   /* children */
	int ety_update, obj_update;
	isns_attr_t *eid_attr;

	isns_obj_t *obj;	/* child object */
	isns_type_t ctype;	/* child object type */
	uint32_t uid;		/* child object uid */
	isns_attr_t pgt[3] = { 0 };

	void const **vpp = NULL;
	int i = 0;

	isnslog(LOG_DEBUG, "dev_attr_reg", "entered (replace: %d)", replace);

	ec = pdu_reset_rsp(&conn->out_packet.pdu,
	    &conn->out_packet.pl,
	    &conn->out_packet.sz);
	if (ec != 0) {
		goto reg_done;
	}

	iscsi_name = (uchar_t *)&source->attr_value[0];
	ctrl = is_control_node(iscsi_name);
	lc_key.type = 0;
	if (key != NULL) {
		/* validate key attributes and make lcp for */
		/* the object identified by key attributes. */
		ec = setup_key_lcp(&lc, key, key_len);
		if (ec == 0 && lc.type != 0) {
			lc_key = lc;
			/* object is not found */
			if ((uid = is_obj_there(&lc)) == 0) {
				/* error if it is a network entity */
				if (lc.type != OBJ_ENTITY) {
					ec = ISNS_RSP_INVALID_REGIS;
				}
			/* validate for the source attribute before */
			/* update or replace the network entity object */
			} else if (ctrl == 0 &&
#ifndef SKIP_SRC_AUTH
			    reg_auth_src(lc.type, uid, iscsi_name) == 0) {
#else
			    0) {
#endif
				ec = ISNS_RSP_SRC_UNAUTHORIZED;
			/* de-register the network entity if replace is true */
			} else if (replace != 0) {
				UPDATE_LCP_UID(&lc, uid);
				ec = dereg_object(&lc, 0);
				/* generate a SCN */
				if (ec == 0) {
					(void) queue_msg_set(scn_q,
					    SCN_TRIGGER, NULL);
				}
			}
		}
	}
	if (ec != 0) {
		goto reg_done;
	}

	/* register the network entity object */
	ec = reg_get_entity(&ety, &op, &op_len);
	if (ec != 0) {
		goto reg_done;
	}
	if (ety == NULL && lc_key.type != OBJ_ENTITY) {
		ety = make_default_entity();
	} else if (ety == NULL ||
	    (lc_key.type == OBJ_ENTITY &&
	    key_cmp(&lc_key, ety) != 0)) {
		/* the eid in key attribute and */
		/* op attribute must be the same */
		ec = ISNS_RSP_INVALID_REGIS;
		goto reg_done;
	}
	if (ety == NULL || rsp_add_key(conn, ety) != 0) {
		ec = ISNS_RSP_INTERNAL_ERROR;
	} else {
		eid_attr = &ety->attrs[ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID)];
		ec = register_object(ety, &puid, &ety_update);
		ptype = OBJ_ENTITY;
	}
	if (ec == 0 && ety_update == 0) {
		/* newly registered, reset the pointer */
		ety = NULL;
	}

	/* register the reset of objects which are specified in */
	/* operating attributes */
	while (ec == 0 &&
	    (ec = reg_get_obj(&obj, &pgt[0], &op, &op_len)) == 0 &&
	    obj != NULL &&
	    (ec = rsp_add_op(conn, obj)) == 0) {
		ctype = obj->type;
		/* set the parent object UID */
		(void) set_parent_obj(obj, puid);
		/* register it */
		ec = register_object(obj, &uid, &obj_update);
		if (ec == 0) {
			if (obj_update == 0 ||
			    is_obj_online(obj) == 0) {
				/* update the ref'd object */
				(void) update_ref_obj(obj);
				/* add the newly registered object info */
				/* to child info array of the parent object */
				ec = buff_child_obj(ptype, ctype, obj, child);
			} else {
				if (ctrl == 0 &&
#ifndef SKIP_SRC_AUTH
				    puid != get_parent_uid(obj)) {
#else
				    0) {
#endif
					ec = ISNS_RSP_SRC_UNAUTHORIZED;
				}
				/* it was for updating an existing object */
				free_one_object(obj);
			}
		} else {
			/* failed registering it */
			free_one_object(obj);
		}
	}

	/* update the portal group object for the associations between */
	/* the newly registered objects and previously registered objects */
	if (ec == 0) {
		ec = verify_ref_obj(ptype, puid, child);
	}
	if (ec != 0) {
		goto reg_done;
	}

	/* update the children list of the parent object */
	while (i < MAX_CHILD_TYPE) {
		vpp = child[i];
		if (vpp != NULL) {
			break;
		}
		i ++;
	}
	if (vpp != NULL) {
		ec = update_child_obj(ptype, puid, child, 1);
	} else {
#ifndef SKIP_SRC_AUTH
		ec = ISNS_RSP_INVALID_REGIS;
#else
		/* for interop-ability, we cannot treat this as */
		/* an error, instead, remove the network entity */
		SET_UID_LCP(&lc, OBJ_ENTITY, puid);
		ec = dereg_object(&lc, 0);
		goto reg_done;
#endif
	}
	if (ec != 0) {
		goto reg_done;
	}
	/* add esi entry */
	if (ety_update != 0) {
		(void) esi_remove(puid);
	}
	ec = esi_add(puid, eid_attr->value.ptr, eid_attr->len);

reg_done:
	conn->ec = ec;
	free_one_object(ety);
	uid = 0;
	while (uid < MAX_CHILD_TYPE) {
		if (child[uid] != NULL) {
			free(child[uid]);
		}
		uid ++;
	}

	if (ec != 0) {
		isnslog(LOG_DEBUG, "dev_attr_reg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * dev_attr_qry:
 *	function which handles the isnsp DEV_ATTR_QRY message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dev_attr_qry(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	isns_tlv_t *source = conn->in_packet.source;
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	uchar_t *iscsi_name;

	bmp_t *nodes_bmp = NULL;
	uint32_t num_of_nodes;
	uint32_t *key_uids = NULL;
	uint32_t num_of_keys;
	isns_type_t key_type;

	uint32_t key_uid;
	uint32_t op_uid;

	uint32_t size_of_ops;
	uint32_t num_of_ops;
	uint32_t *op_uids = NULL;
	isns_type_t op_type;

	isns_tlv_t *tlv;
	uint16_t tlv_len;

	isnslog(LOG_DEBUG, "dev_attr_qry", "entered");

	ec = pdu_reset_rsp(&conn->out_packet.pdu,
	    &conn->out_packet.pl,
	    &conn->out_packet.sz);
	if (ec != 0) {
		goto qry_done;
	}

	/*
	 * RFC 4171 section 5.7.5.2:
	 * If no Operating Attributes are included in the original query, then
	 * all Operating Attributes SHALL be returned in the response. ???
	 */
	if (op_len == 0) {
		goto qry_done;
	}

	iscsi_name = (uchar_t *)&source->attr_value[0];
	if (is_control_node(iscsi_name) == 0) {
		ec = get_scope(iscsi_name, &nodes_bmp, &num_of_nodes);
		if (ec != 0 || nodes_bmp == NULL) {
			goto qry_done;
		}
	}

	size_of_ops = 0;
	if (key != NULL) {
		/*
		 * Return the original message key.
		 */
		ec = rsp_add_tlvs(conn, key, key_len);
		if (ec != 0) {
			goto qry_done;
		}

		/*
		 * Delimiter
		 */
		ec = rsp_add_tlv(conn, ISNS_DELIMITER_ATTR_ID, 0, NULL, 0);
		if (ec != 0) {
			goto qry_done;
		}

		/*
		 * Query objects which match the Key Attributes.
		 */
		ec = get_qry_keys(nodes_bmp, num_of_nodes, &key_type,
		    key, key_len, &key_uids, &num_of_keys);
		if (ec != 0 || key_uids == NULL) {
			goto qry_done;
		}

		/*
		 * Iterate thru each object identified by the message key.
		 */
		tlv = op;
		tlv_len = op_len;
		FOR_EACH_OBJS(key_uids, num_of_keys, key_uid, {
			/*
			 * Iterate thru each Operating Attributes.
			 */
			op = tlv;
			op_len = tlv_len;
			FOR_EACH_OP(op, op_len, op_type, {
				if (op_type == 0) {
					ec = ISNS_RSP_INVALID_QRY;
					goto qry_done;
				}
				ec = get_qry_ops(key_uid, key_type,
				    op_type, &op_uids,
				    &num_of_ops, &size_of_ops);
				if (ec != 0) {
					goto qry_done;
				}
				/*
				 * Iterate thru each object for the Operating
				 * Attributes again.
				 */
				FOR_EACH_OBJS(op_uids, num_of_ops, op_uid, {
					ec = get_qry_attrs(op_uid, op_type,
					    op, op_len, conn);
					if (ec != 0) {
						goto qry_done;
					}
				});
			});
		});
	} else {
		/*
		 * Iterate thru each Operating Attributes.
		 */
		FOR_EACH_OP(op, op_len, op_type, {
			ec = get_qry_ops2(nodes_bmp, num_of_nodes,
			    op_type, &op_uids,
			    &num_of_ops, &size_of_ops);
			if (ec != 0) {
				goto qry_done;
			}
			/*
			 * Iterate thru each object for the Operating
			 * Attributes again.
			 */
			FOR_EACH_OBJS(op_uids, num_of_ops, op_uid, {
				ec = get_qry_attrs(op_uid, op_type,
				    op, op_len, conn);
				if (ec != 0) {
					goto qry_done;
				}
			});
		});
	}

qry_done:
	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "dev_attr_qry", "error code: %d", ec);
	}

	free(nodes_bmp);
	free(key_uids);
	free(op_uids);

	return (0);
}

/*
 * ****************************************************************************
 *
 * dev_get_next:
 *	function which handles the isnsp DEV_GET_NEXT message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dev_get_next(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	isns_tlv_t *source = conn->in_packet.source;
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	uchar_t *iscsi_name;

	bmp_t *nodes_bmp = NULL;
	uint32_t num_of_nodes;

	isns_type_t key_type;
	isns_type_t op_type;
	uint32_t size_of_obj;
	uint32_t num_of_obj;
	uint32_t *obj_uids = NULL;

	uint32_t uid;

	isnslog(LOG_DEBUG, "dev_get_next", "entered");

	ec = pdu_reset_rsp(&conn->out_packet.pdu,
	    &conn->out_packet.pl,
	    &conn->out_packet.sz);
	if (ec != 0) {
		goto get_next_done;
	}

	iscsi_name = (uchar_t *)&source->attr_value[0];
	if (is_control_node(iscsi_name) == 0) {
		ec = get_scope(iscsi_name, &nodes_bmp, &num_of_nodes);
		if (nodes_bmp == NULL) {
			ec = ISNS_RSP_NO_SUCH_ENTRY;
		}
		if (ec != 0) {
			goto get_next_done;
		}
	}

	/*
	 * Get Message Key type and validate the Message Key.
	 */
	key_type = TLV2TYPE(key);
	if (key_type == 0) {
		ec = ISNS_RSP_MSG_FORMAT_ERROR;
		goto get_next_done;
	}
	ec = validate_qry_key(key_type, key, key_len, NULL);
	if (ec != 0) {
		goto get_next_done;
	}

	size_of_obj = 0;
	if (op != NULL) {
		/*
		 * Query the objects which match the Operating Attributes.
		 */
		ec = get_qry_keys(nodes_bmp, num_of_nodes, &op_type,
		    op, op_len, &obj_uids, &num_of_obj);
		if (op_type != key_type) {
			ec = ISNS_RSP_MSG_FORMAT_ERROR;
		}
	} else {
		/*
		 * Query the objects which match the Message Key type.
		 */
		ec = get_qry_ops2(nodes_bmp, num_of_nodes,
		    key_type, &obj_uids, &num_of_obj, &size_of_obj);
	}
	if (ec != 0) {
		goto get_next_done;
	}

	/*
	 * Get the object which is next to the one indicated by the
	 * Message Key.
	 */
	uid = get_next_obj(key, key_len, key_type, obj_uids, num_of_obj);
	if (uid == 0) {
		ec = ISNS_RSP_NO_SUCH_ENTRY;
		goto get_next_done;
	}

	/*
	 * Message Key
	 */
	if ((ec = get_qry_attrs1(uid, key_type, key, key_len, conn)) != 0) {
		goto get_next_done;
	}

	/*
	 * Delimiter
	 */
	if ((ec = rsp_add_tlv(conn, ISNS_DELIMITER_ATTR_ID, 0, NULL, 0)) != 0) {
		goto get_next_done;
	}

	/*
	 * Operating Attributes
	 */
	if (op != NULL) {
		ec = get_qry_attrs(uid, op_type, op, op_len, conn);
	}

get_next_done:
	conn->ec = ec;

	if (ec != 0 && ec != ISNS_RSP_NO_SUCH_ENTRY) {
		isnslog(LOG_DEBUG, "dev_get_next", "error code: %d", ec);
	}

	free(nodes_bmp);
	free(obj_uids);

	return (0);
}

/*
 * ****************************************************************************
 *
 * dev_dereg:
 *	function which handles the isnsp DEV_DEREG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dev_dereg(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	isns_tlv_t *source = conn->in_packet.source;
	/* isns_tlv_t *key = conn->in_packet.key; */
	/* uint16_t key_len = conn->in_packet.key_len; */
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	uchar_t *iscsi_name;
	int ctrl;
	uint32_t puid;

	lookup_ctrl_t lc;
	uint8_t *value;

	isnslog(LOG_DEBUG, "dev_dereg", "entered");

	iscsi_name = (uchar_t *)&source->attr_value[0];
	ctrl = is_control_node(iscsi_name);
	if (ctrl == 0) {
		puid = is_parent_there(iscsi_name);
	}

	while (op_len > 8 && ec == 0) {
		lc.curr_uid = 0;
		value = &op->attr_value[0];
		switch (op->attr_id) {
		case ISNS_EID_ATTR_ID:
			lc.id[0] = ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID);
			lc.op[0] = OP_STRING;
			lc.data[0].ptr = (uchar_t *)value;
			lc.op[1] = 0;
			lc.type = OBJ_ENTITY;
			break;
		case ISNS_ISCSI_NAME_ATTR_ID:
			lc.id[0] = ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID);
			lc.op[0] = OP_STRING;
			lc.data[0].ptr = (uchar_t *)value;
			lc.op[1] = 0;
			lc.type = OBJ_ISCSI;
			break;
		case ISNS_ISCSI_NODE_INDEX_ATTR_ID:
			lc.id[0] = ATTR_INDEX_ISCSI(
			    ISNS_ISCSI_NODE_INDEX_ATTR_ID);
			lc.op[0] = OP_INTEGER;
			lc.data[0].ui = ntohl(*(uint32_t *)value);
			lc.op[1] = 0;
			lc.type = OBJ_ISCSI;
			break;
		case ISNS_PORTAL_IP_ADDR_ATTR_ID:
			lc.id[0] = ATTR_INDEX_PORTAL(
			    ISNS_PORTAL_IP_ADDR_ATTR_ID);
			lc.op[0] = OP_MEMORY_IP6;
			lc.data[0].ip = (in6_addr_t *)value;
			NEXT_TLV(op, op_len);
			if (op_len > 8 &&
			    op->attr_id == ISNS_PORTAL_PORT_ATTR_ID) {
				value = &op->attr_value[0];
				lc.id[1] = ATTR_INDEX_PORTAL(
				    ISNS_PORTAL_PORT_ATTR_ID);
				lc.op[1] = OP_INTEGER;
				lc.data[1].ui = ntohl(*(uint32_t *)value);
				lc.op[2] = 0;
				lc.type = OBJ_PORTAL;
			} else {
				ec = ISNS_RSP_MSG_FORMAT_ERROR;
			}
			break;
		case ISNS_PORTAL_INDEX_ATTR_ID:
			lc.id[0] = ATTR_INDEX_PORTAL(
			    ISNS_PORTAL_INDEX_ATTR_ID);
			lc.op[0] = OP_INTEGER;
			lc.data[0].ui = ntohl(*(uint32_t *)value);
			lc.op[1] = 0;
			lc.type = OBJ_PORTAL;
			break;
		default:
			ec = ISNS_RSP_MSG_FORMAT_ERROR;
			break;
		}
		if (ec == 0 &&
		    (ec = dereg_object(&lc, 0)) == 0) {
			if (ctrl == 0 &&
#ifndef SKIP_SRC_AUTH
			    lc.curr_uid != 0 &&
			    puid != lc.curr_uid) {
#else
			    0) {
#endif
				ec = ISNS_RSP_SRC_UNAUTHORIZED;
			} else {
				NEXT_TLV(op, op_len);
			}
		}
	}

	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "dev_dereg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * scn_reg:
 *	function which handles the isnsp SCN_REG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
scn_reg(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	/* isns_tlv_t *source = conn->in_packet.source; */
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	/* uchar_t *src; */
	uchar_t *node_name;
	uint32_t nlen;
	uint32_t scn;

	isnslog(LOG_DEBUG, "scn_reg", "entered");

	/* src = (uchar_t *)&source->attr_value[0]; */

	if (op == NULL ||
	    op->attr_id != ISNS_ISCSI_SCN_BITMAP_ATTR_ID ||
	    op_len != 12 ||
	    key == NULL ||
	    key->attr_id != ISNS_ISCSI_NAME_ATTR_ID ||
	    key_len != 8 + key->attr_len) {
		ec = ISNS_RSP_MSG_FORMAT_ERROR;
		goto scn_reg_done;
	}

	node_name = (uchar_t *)&key->attr_value[0];
	nlen = key->attr_len;
	scn = ntohl(*(uint32_t *)&op->attr_value[0]);

	ec = add_scn_entry(node_name, nlen, scn);

scn_reg_done:
	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "scn_reg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * scn_dereg:
 *	function which handles the isnsp SCN_DEREG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
scn_dereg(conn_arg_t *conn)
{
	int ec = 0;

	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;

	uchar_t *node_name;

	isnslog(LOG_DEBUG, "scn_dereg", "entered");

	if (key != NULL &&
	    key->attr_len != 0 &&
	    key_len == 8 + key->attr_len &&
	    key->attr_id == ISNS_ISCSI_NAME_ATTR_ID) {
		node_name = (uchar_t *)&key->attr_value[0];
		ec = remove_scn_entry(node_name);
	} else {
		ec = ISNS_RSP_MSG_FORMAT_ERROR;
	}

	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "scn_dereg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * setup_ddid_lcp:
 *	setup the lookup control data for looking up the DD object
 *	by using the dd_id attribute.
 *
 * lcp	- pointer to the lookup control data.
 * dd_id- the unique ID of the DD object.
 * return - the pointer to the lcp.
 *
 * ****************************************************************************
 */
#ifndef DEBUG
static
#endif
lookup_ctrl_t *
setup_ddid_lcp(lookup_ctrl_t *lcp, uint32_t dd_id)
{
	lcp->curr_uid = 0;
	lcp->type = OBJ_DD;
	lcp->id[0] = ATTR_INDEX_DD(ISNS_DD_ID_ATTR_ID);
	lcp->op[0] = OP_INTEGER;
	lcp->data[0].ui = dd_id;
	lcp->op[1] = 0;

	return (lcp);
}

/*
 * ****************************************************************************
 *
 * setup_ddsid_lcp:
 *	setup the lookup control data for looking up the DD-set object
 *	by using the dds_id attribute.
 *
 * lcp	- pointer to the lookup control data.
 * dds_id - the unique ID of the DD-set object.
 * return - the pointer to the lcp.
 *
 * ****************************************************************************
 */
#ifndef DEBUG
static
#endif
lookup_ctrl_t *
setup_ddsid_lcp(lookup_ctrl_t *lcp, uint32_t dds_id)
{
	lcp->curr_uid = 0;
	lcp->type = OBJ_DDS;
	lcp->id[0] = ATTR_INDEX_DDS(ISNS_DD_SET_ID_ATTR_ID);
	lcp->op[0] = OP_INTEGER;
	lcp->data[0].ui = dds_id;
	lcp->op[1] = 0;

	return (lcp);
}

/*
 * ****************************************************************************
 *
 * dd_reg:
 *	function which handles the isnsp DD_REG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dd_reg(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	isns_tlv_t *source = conn->in_packet.source;
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	uint32_t dd_id = 0;
	uint8_t *value;

	isns_obj_t *dd = NULL;

	uchar_t *iscsi_name;

	lookup_ctrl_t lc;
	isns_assoc_iscsi_t aiscsi;
	isns_obj_t *assoc;
	isns_attr_t *attr;

	uint32_t features;

	isnslog(LOG_DEBUG, "dd_reg", "entered");

	iscsi_name = (uchar_t *)&source->attr_value[0];
	if (is_control_node(iscsi_name) == 0) {
		ec = ISNS_RSP_SRC_UNAUTHORIZED;
		goto dd_reg_done;
	}

	ec = pdu_reset_rsp(&conn->out_packet.pdu,
	    &conn->out_packet.pl,
	    &conn->out_packet.sz);
	if (ec != 0) {
		goto dd_reg_done;
	}

	if (op == NULL ||
	    (key != NULL &&
	    (key_len != 12 ||
	    key->attr_id != ISNS_DD_ID_ATTR_ID ||
	    key->attr_len != 4 ||
	    (dd_id = ntohl(*(uint32_t *)&key->attr_value[0])) == 0 ||
	    is_obj_there(setup_ddid_lcp(&lc, dd_id)) == 0))) {
		ec = ISNS_RSP_INVALID_REGIS;
		goto dd_reg_done;
	}

	/* message key */
	if (key != NULL &&
	    (ec = rsp_add_tlv(conn, ISNS_DD_ID_ATTR_ID, 4,
	    (void *)dd_id, 0)) != 0) {
		goto dd_reg_done;
	}

	/* delimiter */
	if ((ec = rsp_add_tlv(conn, ISNS_DELIMITER_ATTR_ID, 0,
	    NULL, 0)) != 0) {
		goto dd_reg_done;
	}

	/* A DDReg message with no Message Key SHALL result in the */
	/* attempted creation of a new Discovery Domain (DD). */
	if (dd_id == 0) {
		ec = create_dd_object(op, op_len, &dd);
		if (ec == 0) {
			ec = register_object(dd, &dd_id, NULL);
			if (ec == ERR_NAME_IN_USE) {
				ec = ISNS_RSP_INVALID_REGIS;
			}
			if (ec != 0) {
				free_object(dd);
				goto dd_reg_done;
			}
		} else {
			goto dd_reg_done;
		}
	}

	/* add the newly created dd to the response */
	if (dd != NULL) {
		ec = rsp_add_op(conn, dd);
	}

	aiscsi.type = OBJ_ASSOC_ISCSI;
	aiscsi.puid = dd_id;

	while (op_len > 8 && ec == 0) {
		value = &op->attr_value[0];
		switch (op->attr_id) {
		case ISNS_DD_ID_ATTR_ID:
			/* if the DD_ID is included in both the Message Key */
			/* and Operating Attributes, then the DD_ID value */
			/* in the Message Key MUST be the same as the DD_ID */
			/* value in the Operating Attributes. */
			if (dd == NULL) {
				if (op->attr_len != 4 ||
				    dd_id != ntohl(*(uint32_t *)value)) {
					ec = ISNS_RSP_INVALID_REGIS;
				} else {
					ec = rsp_add_tlv(conn,
					    ISNS_DD_ID_ATTR_ID, 4,
					    (void *)dd_id, 0);
				}
			}
			break;
		case ISNS_DD_NAME_ATTR_ID:
			/* It is going to modify the DD Symbolic Name. */
			if (dd == NULL) {
				if (op->attr_len > 0 && op->attr_len <= 256) {
					ec = update_dd_name(
					    dd_id,
					    op->attr_len,
					    (uchar_t *)value);
					if (ec == ERR_NAME_IN_USE) {
						ec = ISNS_RSP_INVALID_REGIS;
					}
				} else {
					ec = ISNS_RSP_INVALID_REGIS;
				}
				if (ec == 0) {
					ec = rsp_add_tlv(conn,
					    ISNS_DD_NAME_ATTR_ID,
					    op->attr_len, (void *)value, 1);
				}
			}
			break;
		case ISNS_DD_ISCSI_INDEX_ATTR_ID:
			if (op->attr_len == 4) {
				/* zero the association object */
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_INDEX_ATTR_ID)];
				attr->tag = ISNS_DD_ISCSI_INDEX_ATTR_ID;
				attr->len = 4;
				attr->value.ui = ntohl(*(uint32_t *)value);
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_NAME_ATTR_ID)];
				attr->tag = 0; /* clear it */
				attr->value.ptr = NULL; /* clear it */
				assoc = (isns_obj_t *)&aiscsi;
				if ((ec = add_dd_member(assoc)) ==
				    ERR_ALREADY_ASSOCIATED) {
					ec = 0;
				}
				if (attr->value.ptr != NULL) {
					free(attr->value.ptr);
				}
			} else {
				ec = ISNS_RSP_INVALID_REGIS;
			}
			if (ec == 0) {
				ec = rsp_add_tlv(conn,
				    ISNS_DD_ISCSI_INDEX_ATTR_ID,
				    4, (void *)attr->value.ui, 0);
			}
			break;
		case ISNS_DD_ISCSI_NAME_ATTR_ID:
			if (op->attr_len > 0 && op->attr_len <= 224) {
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_NAME_ATTR_ID)];
				attr->tag = ISNS_DD_ISCSI_NAME_ATTR_ID;
				attr->len = op->attr_len;
				attr->value.ptr = (uchar_t *)value;
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_INDEX_ATTR_ID)];
				attr->tag = 0; /* clear it */
				assoc = (isns_obj_t *)&aiscsi;
				if ((ec = add_dd_member(assoc)) ==
				    ERR_ALREADY_ASSOCIATED) {
					ec = 0;
				}
			} else {
				ec = ISNS_RSP_INVALID_REGIS;
			}
			if (ec == 0) {
				ec = rsp_add_tlv(conn,
				    ISNS_DD_ISCSI_NAME_ATTR_ID,
				    op->attr_len, (void *)value, 1);
			}
			break;
		case ISNS_DD_FC_PORT_NAME_ATTR_ID:
		case ISNS_DD_PORTAL_INDEX_ATTR_ID:
		case ISNS_DD_PORTAL_IP_ADDR_ATTR_ID:
		case ISNS_DD_PORTAL_PORT_ATTR_ID:
			ec = ISNS_RSP_REGIS_NOT_SUPPORTED;
			break;
		case ISNS_DD_FEATURES_ATTR_ID:
			/* It is going to modify the DD Symbolic Name. */
			if (dd == NULL) {
				if (op->attr_len == 4) {
					features = ntohl(*(uint32_t *)value);
					ec = update_dd_features(
					    dd_id, features);
				} else {
					ec = ISNS_RSP_INVALID_REGIS;
				}
				if (ec == 0) {
					ec = rsp_add_tlv(conn,
					    ISNS_DD_FEATURES_ATTR_ID,
					    4, (void *)features, 0);
				}
			}
			break;
		default:
			ec = ISNS_RSP_INVALID_REGIS;
			break;
		}

		NEXT_TLV(op, op_len);
	}

dd_reg_done:
	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "dd_reg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * dds_reg:
 *	function which handles the isnsp DDS_REG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dds_reg(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	isns_tlv_t *source = conn->in_packet.source;
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	uint32_t dds_id = 0;
	uint8_t *value;

	isns_obj_t *dds = NULL;

	uchar_t *iscsi_name;

	lookup_ctrl_t lc;
	isns_assoc_dd_t add;
	isns_obj_t *assoc;
	isns_attr_t *attr;

	uint32_t code;

	isnslog(LOG_DEBUG, "dds_reg", "entered");

	iscsi_name = (uchar_t *)&source->attr_value[0];
	if (is_control_node(iscsi_name) == 0) {
		ec = ISNS_RSP_SRC_UNAUTHORIZED;
		goto dds_reg_done;
	}

	ec = pdu_reset_rsp(&conn->out_packet.pdu,
	    &conn->out_packet.pl,
	    &conn->out_packet.sz);
	if (ec != 0) {
		goto dds_reg_done;
	}

	if (op == NULL ||
	    (key != NULL &&
	    (key_len != 12 ||
	    key->attr_id != ISNS_DD_SET_ID_ATTR_ID ||
	    key->attr_len != 4 ||
	    (dds_id = ntohl(*(uint32_t *)&key->attr_value[0])) == 0 ||
	    is_obj_there(setup_ddsid_lcp(&lc, dds_id)) == 0))) {
		ec = ISNS_RSP_INVALID_REGIS;
		goto dds_reg_done;
	}

	/* message key */
	if (key != NULL &&
	    (ec = rsp_add_tlv(conn, ISNS_DD_SET_ID_ATTR_ID, 4,
	    (void *)dds_id, 0)) != 0) {
		goto dds_reg_done;
	}

	/* delimiter */
	if ((ec = rsp_add_tlv(conn, ISNS_DELIMITER_ATTR_ID, 0,
	    NULL, 0)) != 0) {
		goto dds_reg_done;
	}

	/* A DDSReg message with no Message Key SHALL result in the */
	/* attempted creation of a new Discovery Domain (DD). */
	if (dds_id == 0) {
		ec = create_dds_object(op, op_len, &dds);
		if (ec == 0) {
			ec = register_object(dds, &dds_id, NULL);
			if (ec == ERR_NAME_IN_USE) {
				ec = ISNS_RSP_INVALID_REGIS;
			}
			if (ec != 0) {
				free_object(dds);
				goto dds_reg_done;
			}
		} else {
			goto dds_reg_done;
		}
	}

	/* add the newly created dd to the response */
	if (dds != NULL) {
		ec = rsp_add_op(conn, dds);
	}

	add.type = OBJ_ASSOC_DD;
	add.puid = dds_id;

	while (op_len > 8 && ec == 0) {
		value = &op->attr_value[0];
		switch (op->attr_id) {
		case ISNS_DD_SET_ID_ATTR_ID:
			/* if the DDS_ID is included in both the Message Key */
			/* and Operating Attributes, then the DDS_ID value */
			/* in the Message Key MUST be the same as the DDS_ID */
			/* value in the Operating Attributes. */
			if (dds == NULL) {
				if (op->attr_len != 4 ||
				    dds_id != ntohl(*(uint32_t *)value)) {
					ec = ISNS_RSP_INVALID_REGIS;
				} else {
					ec = rsp_add_tlv(conn,
					    ISNS_DD_SET_ID_ATTR_ID,
					    4, (void *)dds_id, 0);
				}
			}
			break;
		case ISNS_DD_SET_NAME_ATTR_ID:
			/* It is going to modify the DD Symbolic Name. */
			if (dds == NULL) {
				if (op->attr_len > 0 && op->attr_len <= 256) {
					ec = update_dds_name(
					    dds_id,
					    op->attr_len,
					    (uchar_t *)value);
					if (ec == ERR_NAME_IN_USE) {
						ec = ISNS_RSP_INVALID_REGIS;
					}
				} else {
					ec = ISNS_RSP_INVALID_REGIS;
				}
				if (ec == 0) {
					ec = rsp_add_tlv(conn,
					    ISNS_DD_SET_NAME_ATTR_ID,
					    op->attr_len, (void *)value, 1);
				}
			}
			break;
		case ISNS_DD_SET_STATUS_ATTR_ID:
			/* It is going to modify the DD Symbolic Name. */
			if (dds == NULL) {
				if (op->attr_len == 4) {
					code = ntohl(*(uint32_t *)value);
					ec = update_dds_status(
					    dds_id, code);
				} else {
					ec = ISNS_RSP_INVALID_REGIS;
				}
				if (ec == 0) {
					ec = rsp_add_tlv(conn,
					    ISNS_DD_SET_STATUS_ATTR_ID,
					    4, (void *)code, 0);
				}
			}
			break;
		case ISNS_DD_ID_ATTR_ID:
			if (op->attr_len == 4) {
				/* zero the association object */
				attr = &add.attrs[ATTR_INDEX_ASSOC_DD(
				    ISNS_DD_ID_ATTR_ID)];
				attr->tag = ISNS_DD_ID_ATTR_ID;
				attr->len = 4;
				attr->value.ui = ntohl(*(uint32_t *)value);
				assoc = (isns_obj_t *)&add;
				if ((ec = add_dds_member(assoc)) ==
				    ERR_ALREADY_ASSOCIATED) {
					ec = 0;
				}
			} else {
				ec = ISNS_RSP_INVALID_REGIS;
			}
			if (ec == 0) {
				ec = rsp_add_tlv(conn,
				    ISNS_DD_ID_ATTR_ID, 4,
				    (void *)attr->value.ui, 0);
			}
			break;
		default:
			ec = ISNS_RSP_INVALID_REGIS;
			break;
		}

		NEXT_TLV(op, op_len);
	}

dds_reg_done:
	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "dds_reg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * dd_dereg:
 *	function which handles the isnsp DD_DEREG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dd_dereg(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	isns_tlv_t *source = conn->in_packet.source;
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	uint32_t dd_id;
	uint8_t *value;

	uchar_t *iscsi_name;

	isns_assoc_iscsi_t aiscsi;
	isns_obj_t *assoc;
	isns_attr_t *attr;

	isnslog(LOG_DEBUG, "dd_dereg", "entered");

	iscsi_name = (uchar_t *)&source->attr_value[0];
	if (is_control_node(iscsi_name) == 0) {
		ec = ISNS_RSP_SRC_UNAUTHORIZED;
		goto dd_dereg_done;
	}

	if (key == NULL ||
	    key_len != 12 ||
	    key->attr_id != ISNS_DD_ID_ATTR_ID ||
	    (dd_id = ntohl(*(uint32_t *)&key->attr_value[0])) == 0) {
		ec = ISNS_RSP_MSG_FORMAT_ERROR;
		goto dd_dereg_done;
	}

	if (op == NULL) {
		ec = remove_dd_object(dd_id);
	} else {
		aiscsi.type = OBJ_ASSOC_ISCSI;
		aiscsi.puid = dd_id;

		while (op_len > 8 && ec == 0) {
			value = &op->attr_value[0];
			switch (op->attr_id) {
			case ISNS_DD_ISCSI_INDEX_ATTR_ID:
				/* zero the association object */
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_INDEX_ATTR_ID)];
				attr->tag = ISNS_DD_ISCSI_INDEX_ATTR_ID;
				attr->len = 4;
				attr->value.ui = ntohl(*(uint32_t *)value);
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_NAME_ATTR_ID)];
				attr->tag = 0; /* clear it */
				attr->value.ptr = NULL; /* clear it */
				assoc = (isns_obj_t *)&aiscsi;
				if ((ec = remove_dd_member(assoc)) ==
				    ERR_NO_SUCH_ASSOCIATION) {
					ec = 0;
				}
				if (attr->value.ptr != NULL) {
					free(attr->value.ptr);
				}
				break;
			case ISNS_DD_ISCSI_NAME_ATTR_ID:
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_NAME_ATTR_ID)];
				attr->tag = ISNS_DD_ISCSI_NAME_ATTR_ID;
				attr->len = op->attr_len;
				attr->value.ptr = (uchar_t *)value;
				attr = &aiscsi.attrs[ATTR_INDEX_ASSOC_ISCSI(
				    ISNS_DD_ISCSI_INDEX_ATTR_ID)];
				attr->tag = 0; /* clear it */
				assoc = (isns_obj_t *)&aiscsi;
				if ((ec = remove_dd_member(assoc)) ==
				    ERR_NO_SUCH_ASSOCIATION) {
					ec = 0;
				}
				break;
			case ISNS_DD_FC_PORT_NAME_ATTR_ID:
			case ISNS_DD_PORTAL_INDEX_ATTR_ID:
			case ISNS_DD_PORTAL_IP_ADDR_ATTR_ID:
			case ISNS_DD_PORTAL_PORT_ATTR_ID:
				ec = ISNS_RSP_REGIS_NOT_SUPPORTED;
				break;
			default:
				ec = ISNS_RSP_MSG_FORMAT_ERROR;
				break;
			}

			NEXT_TLV(op, op_len);
		}
	}

dd_dereg_done:
	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "dd_dereg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * dds_dereg:
 *	function which handles the isnsp DDS_DEREG message.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
dds_dereg(conn_arg_t *conn)
{
	int ec = 0;

	/* isns_pdu_t *pdu = conn->in_packet.pdu; */
	isns_tlv_t *source = conn->in_packet.source;
	isns_tlv_t *key = conn->in_packet.key;
	uint16_t key_len = conn->in_packet.key_len;
	isns_tlv_t *op = conn->in_packet.op;
	uint16_t op_len = conn->in_packet.op_len;

	uint32_t dds_id;
	uint32_t uid;
	uint8_t *value;

	uchar_t *iscsi_name;

	isnslog(LOG_DEBUG, "dds_dereg", "entered");

	iscsi_name = (uchar_t *)&source->attr_value[0];
	if (is_control_node(iscsi_name) == 0) {
		ec = ISNS_RSP_SRC_UNAUTHORIZED;
		goto dds_dereg_done;
	}

	if (key == NULL ||
	    key_len != 12 ||
	    key->attr_id != ISNS_DD_SET_ID_ATTR_ID ||
	    (dds_id = ntohl(*(uint32_t *)&key->attr_value[0])) == 0) {
		ec = ISNS_RSP_MSG_FORMAT_ERROR;
		goto dds_dereg_done;
	}

	if (op == NULL) {
		ec = remove_dds_object(dds_id);
	} else {
		while (op_len > 8 && ec == 0) {
			value = &op->attr_value[0];
			if (op->attr_id == ISNS_DD_ID_ATTR_ID) {
				uid = ntohl(*(uint32_t *)value);
				if ((ec = remove_dds_member(dds_id, uid)) ==
				    ERR_NO_SUCH_ASSOCIATION) {
					ec = 0;
				}
			} else {
				ec = ISNS_RSP_MSG_FORMAT_ERROR;
			}

			NEXT_TLV(op, op_len);
		}
	}

dds_dereg_done:
	conn->ec = ec;

	if (ec != 0) {
		isnslog(LOG_DEBUG, "dds_dereg", "error code: %d", ec);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * msg_error:
 *	function which handles any unknown isnsp messages or the
 *	messages which are not supported.
 *
 * conn	- the argument of the connection.
 * return - 0: the message requires response.
 *
 * ****************************************************************************
 */
static int
msg_error(conn_arg_t *conn __unused)
{
	return (0);
}

/*
 * ****************************************************************************
 *
 * isns_response_ec:
 *	send the response message to the client with error code.
 *
 * so	- the socket descriptor.
 * pdu	- the received pdu.
 * ec	- the error code which is being responsed.
 * return - status of the sending operation.
 *
 * ****************************************************************************
 */
static int
isns_response_ec(int so, isns_pdu_t *pdu, int ec)
{
	int status;

	uint8_t buff[sizeof (isns_pdu_t) + 8];

	isns_pdu_t *rsp = (isns_pdu_t *)&buff;
	isns_resp_t *resp = (isns_resp_t *)rsp->payload;
	size_t pl = 4;

	rsp->version = htons((uint16_t)ISNSP_VERSION);
	rsp->func_id = htons(pdu->func_id | ISNS_RSP_MASK);
	rsp->xid = htons(pdu->xid);
	resp->status = htonl(ec);

	status = isns_send_pdu(so, rsp, pl);

	return (status);
}

/*
 * ****************************************************************************
 *
 * isns_response:
 *	send the response message to the client.
 *
 * conn	- the argument of the connection.
 * return - status of the sending operation.
 *
 * ****************************************************************************
 */
int
isns_response(conn_arg_t *conn)
{
	int status;

	int so = conn->so;
	int ec = conn->ec;
	isns_pdu_t *pdu = conn->in_packet.pdu;
	isns_pdu_t *rsp = conn->out_packet.pdu;
	size_t pl = conn->out_packet.pl;

	if (rsp != NULL) {
		rsp->version = htons((uint16_t)ISNSP_VERSION);
		rsp->func_id = htons(pdu->func_id | ISNS_RSP_MASK);
		rsp->xid = htons(pdu->xid);
		(void) pdu_update_code(rsp, &pl, ec);
		status = isns_send_pdu(so, rsp, pl);
	} else {
		status = isns_response_ec(so, pdu, ec);
	}

	return (status);
}