xref: /titanic_53/usr/src/cmd/iscsiadm/sun_ima.c (revision 4246c8e92ef9ad6ada2b992b7af02832ff071bf7)
1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
221f6352c6Syi zhang - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  */
25fcf3ce44SJohn Forte 
26fcf3ce44SJohn Forte #include <arpa/inet.h>
27fcf3ce44SJohn Forte #include <sys/socket.h>
28fcf3ce44SJohn Forte #include <sys/types.h>
29fcf3ce44SJohn Forte #include <stdarg.h>
30fcf3ce44SJohn Forte #include <stdlib.h>
31fcf3ce44SJohn Forte #include <string.h>
32fcf3ce44SJohn Forte #include <strings.h>
33fcf3ce44SJohn Forte #include <unistd.h>
34fcf3ce44SJohn Forte #include <syslog.h>
35fcf3ce44SJohn Forte #include <errno.h>
36fcf3ce44SJohn Forte 
37fcf3ce44SJohn Forte #include <fcntl.h>
38fcf3ce44SJohn Forte #include <stdio.h>
39fcf3ce44SJohn Forte #include <time.h>
40fcf3ce44SJohn Forte #include <libdevinfo.h>
41fcf3ce44SJohn Forte 
42fcf3ce44SJohn Forte #include <sys/scsi/adapters/iscsi_if.h>
43*4246c8e9SJack Meng #include <sys/scsi/adapters/iscsi_door.h>
441a1a84a3SPeter Dunlap #include <sys/iscsi_protocol.h>
45fcf3ce44SJohn Forte #include <ima.h>
46fcf3ce44SJohn Forte #include "iscsiadm.h"
47fcf3ce44SJohn Forte #include "sun_ima.h"
48fcf3ce44SJohn Forte 
49fcf3ce44SJohn Forte #define	LIBRARY_PROPERTY_SUPPORTED_IMA_VERSION	1
50fcf3ce44SJohn Forte #define	LIBRARY_PROPERTY_IMPLEMENTATION_VERSION	L"1.0.0"
51fcf3ce44SJohn Forte #define	LIBRARY_PROPERTY_VENDOR	L"Sun Microsystems, Inc."
52fcf3ce44SJohn Forte #define	DEFAULT_NODE_NAME_FORMAT    "iqn.2003-13.com.ima.%s"
53fcf3ce44SJohn Forte #define	PLUGIN_OWNER 1
546cefaae1SJack Meng #define	MAX_CHAP_SECRET_LEN	16
55fcf3ce44SJohn Forte 
56fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
57fcf3ce44SJohn Forte static IMA_INT32		number_of_plugins = -1;
58fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
59fcf3ce44SJohn Forte static IMA_NODE_NAME		sharedNodeName;
60fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
61fcf3ce44SJohn Forte static IMA_NODE_ALIAS		sharedNodeAlias;
62fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
63fcf3ce44SJohn Forte static IMA_PLUGIN_PROPERTIES	PluginProperties;
64fcf3ce44SJohn Forte 
65fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
66fcf3ce44SJohn Forte static IMA_OID			pluginOid;
67fcf3ce44SJohn Forte static IMA_OID			lhbaObjectId;
68fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
69fcf3ce44SJohn Forte static boolean_t		pluginInit = B_FALSE;
70fcf3ce44SJohn Forte 
71fcf3ce44SJohn Forte /* Forward declaration */
72fcf3ce44SJohn Forte #define	BOOL_PARAM		1
73fcf3ce44SJohn Forte #define	MIN_MAX_PARAM		2
74fcf3ce44SJohn Forte #define	PARAM_OP_OK		0
75fcf3ce44SJohn Forte #define	PARAM_OP_FAILED		1
76fcf3ce44SJohn Forte 
77fcf3ce44SJohn Forte static int open_driver(int *fd);
78fcf3ce44SJohn Forte static IMA_STATUS getISCSINodeParameter(int paramType,
79fcf3ce44SJohn Forte     IMA_OID *oid,
80fcf3ce44SJohn Forte     void *pProps,
81fcf3ce44SJohn Forte     uint32_t paramIndex);
82fcf3ce44SJohn Forte static IMA_STATUS setISCSINodeParameter(int paramType,
83fcf3ce44SJohn Forte     IMA_OID *oid,
84fcf3ce44SJohn Forte     void *pProps,
85fcf3ce44SJohn Forte     uint32_t paramIndex);
86fcf3ce44SJohn Forte static IMA_STATUS getDigest(IMA_OID oid, int ioctlCmd,
87fcf3ce44SJohn Forte     SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm);
88fcf3ce44SJohn Forte static IMA_STATUS setAuthMethods(IMA_OID oid, IMA_UINT *pMethodCount,
89fcf3ce44SJohn Forte     const IMA_AUTHMETHOD *pMethodList);
90fcf3ce44SJohn Forte static IMA_STATUS getAuthMethods(IMA_OID oid, IMA_UINT *pMethodCount,
91fcf3ce44SJohn Forte     IMA_AUTHMETHOD *pMethodList);
92fcf3ce44SJohn Forte IMA_STATUS getNegotiatedDigest(int digestType,
93fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm,
94fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES *connProps);
95fcf3ce44SJohn Forte 
96fcf3ce44SJohn Forte /* OK */
97fcf3ce44SJohn Forte #define	DISC_ADDR_OK		    0
98fcf3ce44SJohn Forte /* Incorrect IP address */
99fcf3ce44SJohn Forte #define	DISC_ADDR_INTEGRITY_ERROR   1
100fcf3ce44SJohn Forte /* Error converting text IP address to numeric binary form */
101fcf3ce44SJohn Forte #define	DISC_ADDR_IP_CONV_ERROR	    2
102fcf3ce44SJohn Forte static int prepare_discovery_entry(SUN_IMA_TARGET_ADDRESS discoveryAddress,
103fcf3ce44SJohn Forte     entry_t *entry);
104fcf3ce44SJohn Forte static int prepare_discovery_entry_IMA(IMA_TARGET_ADDRESS discoveryAddress,
105fcf3ce44SJohn Forte     entry_t *entry);
106fcf3ce44SJohn Forte 
107fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
108fcf3ce44SJohn Forte static IMA_STATUS configure_discovery_method(IMA_BOOL enable,
109fcf3ce44SJohn Forte     iSCSIDiscoveryMethod_t method);
110fcf3ce44SJohn Forte 
111fcf3ce44SJohn Forte static IMA_STATUS get_target_oid_list(uint32_t targetListType,
112fcf3ce44SJohn Forte     IMA_OID_LIST **ppList);
113fcf3ce44SJohn Forte 
114fcf3ce44SJohn Forte static IMA_STATUS get_target_lun_oid_list(IMA_OID * targetOid,
115fcf3ce44SJohn Forte 					    iscsi_lun_list_t  **ppLunList);
116fcf3ce44SJohn Forte 
1171f6352c6Syi zhang - Sun Microsystems - Beijing China static int get_lun_devlink(di_devlink_t link, void *arg);
118fcf3ce44SJohn Forte 
119fcf3ce44SJohn Forte static IMA_STATUS getConnOidList(
120fcf3ce44SJohn Forte 	IMA_OID			*oid,
121fcf3ce44SJohn Forte 	iscsi_conn_list_t	**ppConnList);
122fcf3ce44SJohn Forte 
123fcf3ce44SJohn Forte static IMA_STATUS getConnProps(
124fcf3ce44SJohn Forte 	iscsi_if_conn_t		*pConn,
125fcf3ce44SJohn Forte 	iscsi_conn_props_t	**ppConnProps);
126fcf3ce44SJohn Forte 
127fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
128fcf3ce44SJohn Forte static void libSwprintf(wchar_t *wcs, const wchar_t *lpszFormat, ...)
129fcf3ce44SJohn Forte {
130fcf3ce44SJohn Forte 	va_list args;
131fcf3ce44SJohn Forte 	va_start(args, lpszFormat);
132fcf3ce44SJohn Forte 	(void) vswprintf(wcs, 255, lpszFormat, args);
133fcf3ce44SJohn Forte 	va_end(args);
134fcf3ce44SJohn Forte }
135fcf3ce44SJohn Forte 
136fcf3ce44SJohn Forte 
137fcf3ce44SJohn Forte char *
138fcf3ce44SJohn Forte _strlwr(char *s)
139fcf3ce44SJohn Forte {
140fcf3ce44SJohn Forte 	char *t = s;
141fcf3ce44SJohn Forte 	while (t != NULL && *t) {
142fcf3ce44SJohn Forte 		if (*t >= 'A' && *t <= 'Z')
143fcf3ce44SJohn Forte 			*t += 32;
144fcf3ce44SJohn Forte 		t++;
145fcf3ce44SJohn Forte 	}
146fcf3ce44SJohn Forte 	return (s);
147fcf3ce44SJohn Forte }
148fcf3ce44SJohn Forte 
149fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
150fcf3ce44SJohn Forte static void GetBuildTime(IMA_DATETIME* pdatetime)
151fcf3ce44SJohn Forte {
152fcf3ce44SJohn Forte #if defined(BUILD_DATE)
153fcf3ce44SJohn Forte 	if (strptime(BUILD_DATE, "%Y/%m/%d %T %Z", pdatetime) == NULL) {
154fcf3ce44SJohn Forte 		(void) memset(pdatetime, 0, sizeof (IMA_DATETIME));
155fcf3ce44SJohn Forte 	}
156fcf3ce44SJohn Forte #else
157fcf3ce44SJohn Forte 	(void) memset(pdatetime, 0, sizeof (IMA_DATETIME));
158fcf3ce44SJohn Forte #endif
159fcf3ce44SJohn Forte }
160fcf3ce44SJohn Forte 
161fcf3ce44SJohn Forte /*
162fcf3ce44SJohn Forte  * Non-IMA defined function.
163fcf3ce44SJohn Forte  */
164fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_GetDiscoveryAddressPropertiesList(
165fcf3ce44SJohn Forte     SUN_IMA_DISC_ADDR_PROP_LIST	**ppList
166fcf3ce44SJohn Forte )
167fcf3ce44SJohn Forte {
168fcf3ce44SJohn Forte 	char		    discovery_addr_str[256];
169fcf3ce44SJohn Forte 	int		    fd;
170fcf3ce44SJohn Forte 	int		    i;
171fcf3ce44SJohn Forte 	int		    discovery_addr_list_size;
172fcf3ce44SJohn Forte 	int		    status;
173fcf3ce44SJohn Forte 	int		    out_cnt;
174fcf3ce44SJohn Forte 	iscsi_addr_list_t   *ialp;
175fcf3ce44SJohn Forte 	/* LINTED E_FUNC_SET_NOT_USED */
176fcf3ce44SJohn Forte 	IMA_IP_ADDRESS	    *ipAddr;
177fcf3ce44SJohn Forte 
178fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
179fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
180fcf3ce44SJohn Forte 	}
181fcf3ce44SJohn Forte 
182fcf3ce44SJohn Forte 	ialp = (iscsi_addr_list_t *)calloc(1, sizeof (iscsi_addr_list_t));
183fcf3ce44SJohn Forte 	if (ialp == NULL) {
184fcf3ce44SJohn Forte 		(void) close(fd);
185fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
186fcf3ce44SJohn Forte 	}
187fcf3ce44SJohn Forte 
188fcf3ce44SJohn Forte 	ialp->al_vers = ISCSI_INTERFACE_VERSION;
189fcf3ce44SJohn Forte 	ialp->al_in_cnt = ialp->al_out_cnt = 1;
190fcf3ce44SJohn Forte 
191fcf3ce44SJohn Forte 	/*
192fcf3ce44SJohn Forte 	 * Issue ISCSI_DISCOVERY_ADDR_LIST_GET ioctl
193fcf3ce44SJohn Forte 	 * We have allocated space for one entry, if more than one
194fcf3ce44SJohn Forte 	 * address is going to be returned, we will re-issue the ioctl
195fcf3ce44SJohn Forte 	 */
196fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, ialp) != 0) {
197fcf3ce44SJohn Forte 		(void) close(fd);
198fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
199fcf3ce44SJohn Forte 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl failed, errno: %d",
200fcf3ce44SJohn Forte 		    errno);
201fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
202fcf3ce44SJohn Forte 	}
203fcf3ce44SJohn Forte 
204fcf3ce44SJohn Forte 	if (ialp->al_out_cnt > 1) {
205fcf3ce44SJohn Forte 		/*
206fcf3ce44SJohn Forte 		 * we need to allocate more space, save off the out_cnt
207fcf3ce44SJohn Forte 		 * and free ialp
208fcf3ce44SJohn Forte 		 */
209fcf3ce44SJohn Forte 		out_cnt = ialp->al_out_cnt;
210fcf3ce44SJohn Forte 		free(ialp);
211fcf3ce44SJohn Forte 
212fcf3ce44SJohn Forte 		discovery_addr_list_size = sizeof (iscsi_addr_list_t);
213fcf3ce44SJohn Forte 		discovery_addr_list_size += (sizeof (iscsi_addr_t) *
214fcf3ce44SJohn Forte 		    out_cnt - 1);
215fcf3ce44SJohn Forte 		ialp = (iscsi_addr_list_t *)calloc(1, discovery_addr_list_size);
216fcf3ce44SJohn Forte 		if (ialp == NULL) {
217fcf3ce44SJohn Forte 			(void) close(fd);
218fcf3ce44SJohn Forte 			return (IMA_ERROR_INSUFFICIENT_MEMORY);
219fcf3ce44SJohn Forte 		}
220fcf3ce44SJohn Forte 		ialp->al_vers = ISCSI_INTERFACE_VERSION;
221fcf3ce44SJohn Forte 		ialp->al_in_cnt = out_cnt;
222fcf3ce44SJohn Forte 
223fcf3ce44SJohn Forte 		/*
224fcf3ce44SJohn Forte 		 * Issue ISCSI_DISCOVERY_ADDR_LIST_GET ioctl again to obtain all
225fcf3ce44SJohn Forte 		 * the discovery addresses.
226fcf3ce44SJohn Forte 		 */
227fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, ialp) != 0) {
228fcf3ce44SJohn Forte #define	ERROR_STR "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl failed, errno :%d"
229fcf3ce44SJohn Forte 			free(ialp);
230fcf3ce44SJohn Forte 			(void) close(fd);
231fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
232fcf3ce44SJohn Forte 			    ERROR_STR, errno);
233fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
234fcf3ce44SJohn Forte #undef ERROR_STR
235fcf3ce44SJohn Forte 
236fcf3ce44SJohn Forte 		}
237fcf3ce44SJohn Forte 	}
238fcf3ce44SJohn Forte 
239fcf3ce44SJohn Forte 	*ppList = (SUN_IMA_DISC_ADDR_PROP_LIST *)calloc(1,
240fcf3ce44SJohn Forte 	    sizeof (SUN_IMA_DISC_ADDR_PROP_LIST) +
241fcf3ce44SJohn Forte 	    ialp->al_out_cnt * sizeof (IMA_DISCOVERY_ADDRESS_PROPERTIES));
242fcf3ce44SJohn Forte 	if (*ppList == NULL) {
243fcf3ce44SJohn Forte 		free(ialp);
244fcf3ce44SJohn Forte 		(void) close(fd);
245fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
246fcf3ce44SJohn Forte 	}
247fcf3ce44SJohn Forte 	(*ppList)->discAddrCount = ialp->al_out_cnt;
248fcf3ce44SJohn Forte 
249fcf3ce44SJohn Forte 	for (i = 0; i < ialp->al_out_cnt; i++) {
250fcf3ce44SJohn Forte 		if (ialp->al_addrs[i].a_addr.i_insize ==
251fcf3ce44SJohn Forte 		    sizeof (struct in_addr)) {
252fcf3ce44SJohn Forte 
253fcf3ce44SJohn Forte 			(*ppList)->props[i].discoveryAddress.hostnameIpAddress.
254fcf3ce44SJohn Forte 			id.ipAddress.ipv4Address = IMA_TRUE;
255fcf3ce44SJohn Forte 
256fcf3ce44SJohn Forte 		} else if (ialp->al_addrs[i].a_addr.i_insize ==
257fcf3ce44SJohn Forte 		    sizeof (struct in6_addr)) {
258fcf3ce44SJohn Forte 
259fcf3ce44SJohn Forte 			(*ppList)->props[i].discoveryAddress.hostnameIpAddress.
260fcf3ce44SJohn Forte 			id.ipAddress.ipv4Address = IMA_FALSE;
261fcf3ce44SJohn Forte 
262fcf3ce44SJohn Forte 		} else {
263fcf3ce44SJohn Forte 			(void) strlcpy(discovery_addr_str, "unknown",
264fcf3ce44SJohn Forte 			    sizeof (discovery_addr_str));
265fcf3ce44SJohn Forte 		}
266fcf3ce44SJohn Forte 
267fcf3ce44SJohn Forte 		ipAddr = &(*ppList)->props[i].discoveryAddress.
268fcf3ce44SJohn Forte 		    hostnameIpAddress.id.ipAddress;
269fcf3ce44SJohn Forte 
270fcf3ce44SJohn Forte 		bcopy(&ialp->al_addrs[i].a_addr.i_addr,
271fcf3ce44SJohn Forte 		    (*ppList)->props[i].discoveryAddress.hostnameIpAddress.id.
272fcf3ce44SJohn Forte 		    ipAddress.ipAddress,
273fcf3ce44SJohn Forte 		    sizeof (ipAddr->ipAddress));
274fcf3ce44SJohn Forte 
275fcf3ce44SJohn Forte 		(*ppList)->props[i].discoveryAddress.portNumber =
276fcf3ce44SJohn Forte 		    ialp->al_addrs[i].a_port;
277fcf3ce44SJohn Forte 	}
278fcf3ce44SJohn Forte 
279fcf3ce44SJohn Forte 	free(ialp);
280fcf3ce44SJohn Forte 	(void) close(fd);
281fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
282fcf3ce44SJohn Forte }
283fcf3ce44SJohn Forte 
284fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_GetStaticTargetProperties(
285fcf3ce44SJohn Forte 		IMA_OID	staticTargetOid,
286fcf3ce44SJohn Forte 		SUN_IMA_STATIC_TARGET_PROPERTIES *pProps
287fcf3ce44SJohn Forte )
288fcf3ce44SJohn Forte {
289fcf3ce44SJohn Forte 	int fd;
290fcf3ce44SJohn Forte 	int status;
291fcf3ce44SJohn Forte 	iscsi_static_property_t prop;
292fcf3ce44SJohn Forte 	/* LINTED */
293fcf3ce44SJohn Forte 	IMA_IP_ADDRESS	    *ipAddr;
294fcf3ce44SJohn Forte 
295fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
296fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
297fcf3ce44SJohn Forte 	}
298fcf3ce44SJohn Forte 
299fcf3ce44SJohn Forte 	(void) memset(&prop, 0, sizeof (iscsi_static_property_t));
300fcf3ce44SJohn Forte 	prop.p_vers = ISCSI_INTERFACE_VERSION;
301fcf3ce44SJohn Forte 	prop.p_oid = (uint32_t)staticTargetOid.objectSequenceNumber;
302fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_STATIC_GET, &prop) != 0) {
303fcf3ce44SJohn Forte 		status = errno;
304fcf3ce44SJohn Forte 		(void) close(fd);
305fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
306fcf3ce44SJohn Forte 		    "ISCSI_STATIC_GET ioctl failed, errno: %d", status);
307fcf3ce44SJohn Forte 		if (status == ENOENT) {
308fcf3ce44SJohn Forte 			return (IMA_ERROR_OBJECT_NOT_FOUND);
309fcf3ce44SJohn Forte 		} else {
310fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
311fcf3ce44SJohn Forte 		}
312fcf3ce44SJohn Forte 	}
313fcf3ce44SJohn Forte 	(void) close(fd);
314fcf3ce44SJohn Forte 
315fcf3ce44SJohn Forte 	(void) mbstowcs(pProps->staticTarget.targetName, (char *)prop.p_name,
316fcf3ce44SJohn Forte 	    sizeof (pProps->staticTarget.targetName)/sizeof (IMA_WCHAR));
317fcf3ce44SJohn Forte 
318fcf3ce44SJohn Forte 	if (prop.p_addr_list.al_addrs[0].a_addr.i_insize ==
319fcf3ce44SJohn Forte 	    sizeof (struct in_addr)) {
320fcf3ce44SJohn Forte 		/* IPv4 */
321fcf3ce44SJohn Forte 		pProps->staticTarget.targetAddress.imaStruct.hostnameIpAddress.
322fcf3ce44SJohn Forte 			id.ipAddress.ipv4Address = IMA_TRUE;
323fcf3ce44SJohn Forte 	} else if (prop.p_addr_list.al_addrs[0].a_addr.i_insize ==
324fcf3ce44SJohn Forte 	    sizeof (struct in6_addr)) {
325fcf3ce44SJohn Forte 		/* IPv6 */
326fcf3ce44SJohn Forte 		pProps->staticTarget.targetAddress.imaStruct.hostnameIpAddress.
327fcf3ce44SJohn Forte 			id.ipAddress.ipv4Address = IMA_FALSE;
328fcf3ce44SJohn Forte 	} else {
329fcf3ce44SJohn Forte 		/* Should not happen */
330fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
331fcf3ce44SJohn Forte 		    "ISCSI_STATIC_GET returned bad address");
332fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
333fcf3ce44SJohn Forte 	}
334fcf3ce44SJohn Forte 
335fcf3ce44SJohn Forte 	ipAddr = &pProps->staticTarget.targetAddress.imaStruct.
336fcf3ce44SJohn Forte 	    hostnameIpAddress.id.ipAddress;
337fcf3ce44SJohn Forte 
338fcf3ce44SJohn Forte 	bcopy(&prop.p_addr_list.al_addrs[0].a_addr.i_addr,
339fcf3ce44SJohn Forte 	    pProps->staticTarget.targetAddress.imaStruct.hostnameIpAddress.id.
340fcf3ce44SJohn Forte 	    ipAddress.ipAddress, sizeof (ipAddr->ipAddress));
341fcf3ce44SJohn Forte 
342fcf3ce44SJohn Forte 	pProps->staticTarget.targetAddress.imaStruct.portNumber =
343fcf3ce44SJohn Forte 	    prop.p_addr_list.al_addrs[0].a_port;
344fcf3ce44SJohn Forte 
345fcf3ce44SJohn Forte 
346fcf3ce44SJohn Forte 	if (prop.p_addr_list.al_tpgt == (uint32_t)ISCSI_DEFAULT_TPGT) {
347fcf3ce44SJohn Forte 		pProps->staticTarget.targetAddress.defaultTpgt = IMA_TRUE;
348fcf3ce44SJohn Forte 		pProps->staticTarget.targetAddress.tpgt = 0;
349fcf3ce44SJohn Forte 	} else {
350fcf3ce44SJohn Forte 		pProps->staticTarget.targetAddress.defaultTpgt = IMA_FALSE;
351fcf3ce44SJohn Forte 		pProps->staticTarget.targetAddress.tpgt =
352fcf3ce44SJohn Forte 		    prop.p_addr_list.al_tpgt;
353fcf3ce44SJohn Forte 	}
354fcf3ce44SJohn Forte 
355fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
356fcf3ce44SJohn Forte }
357fcf3ce44SJohn Forte 
358fcf3ce44SJohn Forte /*ARGSUSED*/
359fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_AddStaticTarget(
360fcf3ce44SJohn Forte 		IMA_OID lhbaOid,
361fcf3ce44SJohn Forte 		const SUN_IMA_STATIC_DISCOVERY_TARGET staticConfig,
362fcf3ce44SJohn Forte 		IMA_OID *pTargetOid
363fcf3ce44SJohn Forte )
364fcf3ce44SJohn Forte {
365fcf3ce44SJohn Forte 	iscsi_target_entry_t	target;
366fcf3ce44SJohn Forte 	int			fd;
367fcf3ce44SJohn Forte 	int			target_in_addr_size;
368fcf3ce44SJohn Forte 	int			status;
369fcf3ce44SJohn Forte 	union {
370fcf3ce44SJohn Forte 		struct in_addr	u_in4;
371fcf3ce44SJohn Forte 		struct in6_addr	u_in6;
372fcf3ce44SJohn Forte 	}			target_in;
373fcf3ce44SJohn Forte 
374fcf3ce44SJohn Forte 	/*
375fcf3ce44SJohn Forte 	 * staticConfig.address may come in with port number at its trailer.
376fcf3ce44SJohn Forte 	 * Parse it to separate the IP address and port number.
377fcf3ce44SJohn Forte 	 * Also translate the hostname to IP address if needed.
378fcf3ce44SJohn Forte 	 */
379fcf3ce44SJohn Forte 
380fcf3ce44SJohn Forte 	if (staticConfig.targetAddress.imaStruct.hostnameIpAddress.id.ipAddress.
381fcf3ce44SJohn Forte 	    ipv4Address == IMA_FALSE) {
382fcf3ce44SJohn Forte 
383fcf3ce44SJohn Forte 		bcopy(staticConfig.targetAddress.imaStruct.hostnameIpAddress.
384fcf3ce44SJohn Forte 		    id.ipAddress.ipAddress, &target_in.u_in6,
385fcf3ce44SJohn Forte 		    sizeof (target_in.u_in6));
386fcf3ce44SJohn Forte 
387fcf3ce44SJohn Forte 		target_in_addr_size = sizeof (struct in6_addr);
388fcf3ce44SJohn Forte 	} else {
389fcf3ce44SJohn Forte 
390fcf3ce44SJohn Forte 		bcopy(staticConfig.targetAddress.imaStruct.hostnameIpAddress.
391fcf3ce44SJohn Forte 		    id.ipAddress.ipAddress, &target_in.u_in4,
392fcf3ce44SJohn Forte 		    sizeof (target_in.u_in4));
393fcf3ce44SJohn Forte 
394fcf3ce44SJohn Forte 		target_in_addr_size = sizeof (struct in_addr);
395fcf3ce44SJohn Forte 	}
396fcf3ce44SJohn Forte 
397fcf3ce44SJohn Forte 	(void) memset(&target, 0, sizeof (iscsi_target_entry_t));
398fcf3ce44SJohn Forte 	target.te_entry.e_vers = ISCSI_INTERFACE_VERSION;
399fcf3ce44SJohn Forte 	target.te_entry.e_oid = ISCSI_OID_NOTSET;
400fcf3ce44SJohn Forte 
401fcf3ce44SJohn Forte 	(void) wcstombs((char *)target.te_name, staticConfig.targetName,
402fcf3ce44SJohn Forte 	    ISCSI_MAX_NAME_LEN);
403fcf3ce44SJohn Forte 
404fcf3ce44SJohn Forte 	target.te_entry.e_insize = target_in_addr_size;
405fcf3ce44SJohn Forte 	if (target.te_entry.e_insize == sizeof (struct in_addr)) {
406fcf3ce44SJohn Forte 		target.te_entry.e_u.u_in4.s_addr = target_in.u_in4.s_addr;
407fcf3ce44SJohn Forte 	} else if (target.te_entry.e_insize == sizeof (struct in6_addr)) {
408fcf3ce44SJohn Forte 		bcopy(target_in.u_in6.s6_addr,
409fcf3ce44SJohn Forte 		    target.te_entry.e_u.u_in6.s6_addr,
410fcf3ce44SJohn Forte 		    sizeof (struct in6_addr));
411fcf3ce44SJohn Forte 	} else {
412fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
413fcf3ce44SJohn Forte 	}
414fcf3ce44SJohn Forte 
415fcf3ce44SJohn Forte 	target.te_entry.e_port =
416fcf3ce44SJohn Forte 	    staticConfig.targetAddress.imaStruct.portNumber;
417fcf3ce44SJohn Forte 
418fcf3ce44SJohn Forte 	if (staticConfig.targetAddress.defaultTpgt == IMA_TRUE) {
419fcf3ce44SJohn Forte 		target.te_entry.e_tpgt = ISCSI_DEFAULT_TPGT;
420fcf3ce44SJohn Forte 	} else {
421fcf3ce44SJohn Forte 		target.te_entry.e_tpgt = staticConfig.targetAddress.tpgt;
422fcf3ce44SJohn Forte 	}
423fcf3ce44SJohn Forte 
424fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
425fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
426fcf3ce44SJohn Forte 	}
427fcf3ce44SJohn Forte 
428fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_STATIC_SET, &target)) {
429fcf3ce44SJohn Forte 		/*
430fcf3ce44SJohn Forte 		 * Encountered problem setting the IP address and port for
431fcf3ce44SJohn Forte 		 * the target just added.
432fcf3ce44SJohn Forte 		 */
433fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
434fcf3ce44SJohn Forte 		    "ISCSI_STATIC_SET ioctl failed, errno: %d", errno);
435fcf3ce44SJohn Forte 		(void) close(fd);
436fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
437fcf3ce44SJohn Forte 	}
438fcf3ce44SJohn Forte 
439fcf3ce44SJohn Forte 	pTargetOid->objectType = IMA_OBJECT_TYPE_TARGET;
440fcf3ce44SJohn Forte 	pTargetOid->ownerId = 1;
441fcf3ce44SJohn Forte 	pTargetOid->objectSequenceNumber = target.te_entry.e_oid;
442fcf3ce44SJohn Forte 
443fcf3ce44SJohn Forte 	(void) close(fd);
444fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
445fcf3ce44SJohn Forte }
446fcf3ce44SJohn Forte 
447fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_GetTargetProperties(
448fcf3ce44SJohn Forte 		IMA_OID targetId,
449fcf3ce44SJohn Forte 		SUN_IMA_TARGET_PROPERTIES *pProps
450fcf3ce44SJohn Forte )
451fcf3ce44SJohn Forte {
452fcf3ce44SJohn Forte 	int		    fd;
453fcf3ce44SJohn Forte 	int			status;
454fcf3ce44SJohn Forte 	iscsi_property_t    prop;
455fcf3ce44SJohn Forte 
456fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
457fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
458fcf3ce44SJohn Forte 	}
459fcf3ce44SJohn Forte 
460fcf3ce44SJohn Forte 	(void) memset(&prop, 0, sizeof (iscsi_property_t));
461fcf3ce44SJohn Forte 	prop.p_vers = ISCSI_INTERFACE_VERSION;
462fcf3ce44SJohn Forte 	prop.p_oid = (uint32_t)targetId.objectSequenceNumber;
463fcf3ce44SJohn Forte 
464fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_TARGET_PROPS_GET, &prop) != 0) {
465fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
466fcf3ce44SJohn Forte 		    "ISCSI_TARGET_PROPS_GET ioctl failed, errno: %d", errno);
467fcf3ce44SJohn Forte 		(void) close(fd);
468fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
469fcf3ce44SJohn Forte 	}
470fcf3ce44SJohn Forte 
471fcf3ce44SJohn Forte 	(void) mbstowcs(pProps->imaProps.name,
472fcf3ce44SJohn Forte 	    (char *)prop.p_name, IMA_NODE_NAME_LEN);
473fcf3ce44SJohn Forte 	(void) memset(pProps->imaProps.alias, 0,
474fcf3ce44SJohn Forte 	    (sizeof (IMA_WCHAR) * SUN_IMA_NODE_ALIAS_LEN));
475fcf3ce44SJohn Forte 	if (prop.p_alias_len > 0) {
476fcf3ce44SJohn Forte 		(void) mbstowcs(pProps->imaProps.alias, (char *)prop.p_alias,
477fcf3ce44SJohn Forte 		    SUN_IMA_NODE_ALIAS_LEN);
478fcf3ce44SJohn Forte 	}
479fcf3ce44SJohn Forte 
480fcf3ce44SJohn Forte 	/* Initialize the discovery method to unknown method. */
481fcf3ce44SJohn Forte 	pProps->imaProps.discoveryMethodFlags =
482fcf3ce44SJohn Forte 	    IMA_TARGET_DISCOVERY_METHOD_UNKNOWN;
483fcf3ce44SJohn Forte 	if (!((prop.p_discovery & iSCSIDiscoveryMethodStatic) ^
484fcf3ce44SJohn Forte 	    iSCSIDiscoveryMethodStatic)) {
485fcf3ce44SJohn Forte 		pProps->imaProps.discoveryMethodFlags |=
486fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_STATIC;
487fcf3ce44SJohn Forte 	}
488fcf3ce44SJohn Forte 
489fcf3ce44SJohn Forte 	if (!((prop.p_discovery & iSCSIDiscoveryMethodSLP) ^
490fcf3ce44SJohn Forte 	    iSCSIDiscoveryMethodSLP)) {
491fcf3ce44SJohn Forte 		pProps->imaProps.discoveryMethodFlags |=
492fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_SLP;
493fcf3ce44SJohn Forte 	}
494fcf3ce44SJohn Forte 
495fcf3ce44SJohn Forte 	if (!((prop.p_discovery & iSCSIDiscoveryMethodISNS) ^
496fcf3ce44SJohn Forte 	    iSCSIDiscoveryMethodISNS)) {
497fcf3ce44SJohn Forte 		pProps->imaProps.discoveryMethodFlags |=
498fcf3ce44SJohn Forte 		    iSCSIDiscoveryMethodISNS;
499fcf3ce44SJohn Forte 	}
500fcf3ce44SJohn Forte 
501fcf3ce44SJohn Forte 	if (!((prop.p_discovery & iSCSIDiscoveryMethodSendTargets) ^
502fcf3ce44SJohn Forte 	    iSCSIDiscoveryMethodSendTargets)) {
503fcf3ce44SJohn Forte 		pProps->imaProps.discoveryMethodFlags |=
504fcf3ce44SJohn Forte 		    iSCSIDiscoveryMethodSendTargets;
505fcf3ce44SJohn Forte 	}
506fcf3ce44SJohn Forte 
507fcf3ce44SJohn Forte 	if (prop.p_tpgt_conf == ISCSI_DEFAULT_TPGT) {
508fcf3ce44SJohn Forte 		pProps->defaultTpgtConf = IMA_TRUE;
509fcf3ce44SJohn Forte 		pProps->tpgtConf = 0;
510fcf3ce44SJohn Forte 	} else {
511fcf3ce44SJohn Forte 		pProps->defaultTpgtConf = IMA_FALSE;
512fcf3ce44SJohn Forte 		pProps->tpgtConf = prop.p_tpgt_conf;
513fcf3ce44SJohn Forte 	}
514fcf3ce44SJohn Forte 
515fcf3ce44SJohn Forte 	if (prop.p_tpgt_nego == ISCSI_DEFAULT_TPGT) {
516fcf3ce44SJohn Forte 		pProps->defaultTpgtNego = IMA_TRUE;
517fcf3ce44SJohn Forte 		pProps->tpgtNego = 0;
518fcf3ce44SJohn Forte 	} else {
519fcf3ce44SJohn Forte 		pProps->defaultTpgtNego = IMA_FALSE;
520fcf3ce44SJohn Forte 		pProps->tpgtNego = prop.p_tpgt_nego;
521fcf3ce44SJohn Forte 	}
522fcf3ce44SJohn Forte 
523fcf3ce44SJohn Forte 	bcopy(prop.p_isid, pProps->isid, ISCSI_ISID_LEN);
524fcf3ce44SJohn Forte 
525fcf3ce44SJohn Forte 	(void) close(fd);
526fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
527fcf3ce44SJohn Forte }
528fcf3ce44SJohn Forte 
529fcf3ce44SJohn Forte /*
530fcf3ce44SJohn Forte  * This function only sets CHAP params since we only support CHAP for now.
531fcf3ce44SJohn Forte  */
532fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_SetTargetAuthParams(
533fcf3ce44SJohn Forte     IMA_OID targetOid,
534fcf3ce44SJohn Forte     IMA_AUTHMETHOD method,
535fcf3ce44SJohn Forte     const IMA_INITIATOR_AUTHPARMS *pParms
536fcf3ce44SJohn Forte )
537fcf3ce44SJohn Forte {
538fcf3ce44SJohn Forte 	int fd;
539fcf3ce44SJohn Forte 	iscsi_chap_props_t  chap_p;
540fcf3ce44SJohn Forte 
541fcf3ce44SJohn Forte 	if (method != IMA_AUTHMETHOD_CHAP)
542fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
543fcf3ce44SJohn Forte 
544fcf3ce44SJohn Forte 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
545fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
546fcf3ce44SJohn Forte 		    ISCSI_DRIVER_DEVCTL, errno);
547fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
548fcf3ce44SJohn Forte 	}
549fcf3ce44SJohn Forte 
550fcf3ce44SJohn Forte 	(void) memset(&chap_p, 0, sizeof (iscsi_chap_props_t));
551fcf3ce44SJohn Forte 	chap_p.c_vers = ISCSI_INTERFACE_VERSION;
552fcf3ce44SJohn Forte 	chap_p.c_oid = (uint32_t)targetOid.objectSequenceNumber;
553fcf3ce44SJohn Forte 
554fcf3ce44SJohn Forte 	chap_p.c_user_len =
555fcf3ce44SJohn Forte 	    pParms->chapParms.nameLength;
556fcf3ce44SJohn Forte 	(void) memcpy(chap_p.c_user,
557fcf3ce44SJohn Forte 	    pParms->chapParms.name, chap_p.c_user_len);
558fcf3ce44SJohn Forte 
559fcf3ce44SJohn Forte 	chap_p.c_secret_len =
560fcf3ce44SJohn Forte 	    pParms->chapParms.challengeSecretLength;
561fcf3ce44SJohn Forte 	(void) memcpy(chap_p.c_secret,
562fcf3ce44SJohn Forte 	    pParms->chapParms.challengeSecret,
563fcf3ce44SJohn Forte 	    chap_p.c_secret_len);
564fcf3ce44SJohn Forte 
565fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_CHAP_SET, &chap_p) != 0) {
566fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
567fcf3ce44SJohn Forte 		    "ISCSI_CHAP_SET ioctl failed, errno: %d", errno);
568fcf3ce44SJohn Forte 		(void) close(fd);
569fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
570fcf3ce44SJohn Forte 	}
571fcf3ce44SJohn Forte 
572fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
573fcf3ce44SJohn Forte }
574fcf3ce44SJohn Forte 
575fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_GetTargetAuthMethods(
576fcf3ce44SJohn Forte     IMA_OID		lhbaOid,
577fcf3ce44SJohn Forte     IMA_OID		targetOid,
578fcf3ce44SJohn Forte     IMA_UINT	*pMethodCount,
579fcf3ce44SJohn Forte     IMA_AUTHMETHOD *pMethodList
580fcf3ce44SJohn Forte )
581fcf3ce44SJohn Forte {
582fcf3ce44SJohn Forte 	if (getAuthMethods(targetOid, pMethodCount, pMethodList)
583fcf3ce44SJohn Forte 	    != IMA_STATUS_SUCCESS) {
584fcf3ce44SJohn Forte 		return (getAuthMethods(lhbaOid, pMethodCount, pMethodList));
585fcf3ce44SJohn Forte 	}
586fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
587fcf3ce44SJohn Forte }
588fcf3ce44SJohn Forte 
589fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_SetInitiatorRadiusConfig(
590fcf3ce44SJohn Forte 		IMA_OID	lhbaOid,
591fcf3ce44SJohn Forte 		SUN_IMA_RADIUS_CONFIG *config
592fcf3ce44SJohn Forte )
593fcf3ce44SJohn Forte {
594fcf3ce44SJohn Forte 	int			af;
595fcf3ce44SJohn Forte 	int			fd;
596fcf3ce44SJohn Forte 	int			status;
597fcf3ce44SJohn Forte 	iscsi_radius_props_t	radius;
598fcf3ce44SJohn Forte 	union {
599fcf3ce44SJohn Forte 		struct in_addr	u_in4;
600fcf3ce44SJohn Forte 		struct in6_addr	u_in6;
601fcf3ce44SJohn Forte 	}	radius_in;
602fcf3ce44SJohn Forte 
603fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
604fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
605fcf3ce44SJohn Forte 	}
606fcf3ce44SJohn Forte 
607fcf3ce44SJohn Forte 	(void) memset(&radius, 0, sizeof (iscsi_radius_props_t));
608fcf3ce44SJohn Forte 	radius.r_vers = ISCSI_INTERFACE_VERSION;
609fcf3ce44SJohn Forte 	radius.r_oid = (uint32_t)lhbaOid.objectSequenceNumber;
610fcf3ce44SJohn Forte 	/* Get first because other data fields may already exist */
611fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_RADIUS_GET, &radius) != 0) {
612fcf3ce44SJohn Forte 		/* EMPTY */
613fcf3ce44SJohn Forte 		/* It's fine if other data fields are not there. */
614fcf3ce44SJohn Forte 	}
615fcf3ce44SJohn Forte 
616fcf3ce44SJohn Forte 	if (config->isIpv6 == IMA_TRUE) {
617fcf3ce44SJohn Forte 		af = AF_INET6;
618fcf3ce44SJohn Forte 	} else {
619fcf3ce44SJohn Forte 		af = AF_INET;
620fcf3ce44SJohn Forte 	}
621fcf3ce44SJohn Forte 
622fcf3ce44SJohn Forte 	if (inet_pton(af, config->hostnameIpAddress, &radius_in.u_in4) != 1) {
623fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
624fcf3ce44SJohn Forte 	}
625fcf3ce44SJohn Forte 
626fcf3ce44SJohn Forte 	switch (af) {
627fcf3ce44SJohn Forte 		case AF_INET:
628fcf3ce44SJohn Forte 			radius.r_addr.u_in4.s_addr = radius_in.u_in4.s_addr;
629fcf3ce44SJohn Forte 			radius.r_insize = sizeof (struct in_addr);
630fcf3ce44SJohn Forte 			break;
631fcf3ce44SJohn Forte 		case AF_INET6:
632fcf3ce44SJohn Forte 			(void) memcpy(radius.r_addr.u_in6.s6_addr,
633fcf3ce44SJohn Forte 			    radius_in.u_in6.s6_addr, 16);
634fcf3ce44SJohn Forte 			radius.r_insize = sizeof (struct in6_addr);
635fcf3ce44SJohn Forte 			break;
636fcf3ce44SJohn Forte 	}
637fcf3ce44SJohn Forte 	radius.r_port = config->port;
638fcf3ce44SJohn Forte 	radius.r_radius_config_valid = B_TRUE;
639fcf3ce44SJohn Forte 	/* Allow resetting the RADIUS shared secret to NULL */
640fcf3ce44SJohn Forte 	if (config->sharedSecretValid == IMA_TRUE) {
641fcf3ce44SJohn Forte 		radius.r_shared_secret_len = config->sharedSecretLength;
642fcf3ce44SJohn Forte 		(void) memset(&radius.r_shared_secret[0], 0,
643fcf3ce44SJohn Forte 		    MAX_RAD_SHARED_SECRET_LEN);
644fcf3ce44SJohn Forte 		(void) memcpy(&radius.r_shared_secret[0], config->sharedSecret,
645fcf3ce44SJohn Forte 		    config->sharedSecretLength);
646fcf3ce44SJohn Forte 	}
647fcf3ce44SJohn Forte 
648fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_RADIUS_SET, &radius) != 0) {
649fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
650fcf3ce44SJohn Forte 		    "ISCSI_RADIUS_SET ioctl failed, errno: %d", errno);
651fcf3ce44SJohn Forte 		(void) close(fd);
652fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
653fcf3ce44SJohn Forte 	}
654fcf3ce44SJohn Forte 
655fcf3ce44SJohn Forte 	(void) close(fd);
656fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
657fcf3ce44SJohn Forte }
658fcf3ce44SJohn Forte 
659fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_GetInitiatorRadiusConfig(
660fcf3ce44SJohn Forte 		IMA_OID	lhbaOid,
661fcf3ce44SJohn Forte 		SUN_IMA_RADIUS_CONFIG *config
662fcf3ce44SJohn Forte )
663fcf3ce44SJohn Forte {
664fcf3ce44SJohn Forte 	int			af;
665fcf3ce44SJohn Forte 	int			fd;
666fcf3ce44SJohn Forte 	int			status;
667fcf3ce44SJohn Forte 	iscsi_radius_props_t	radius;
668fcf3ce44SJohn Forte 
669fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
670fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
671fcf3ce44SJohn Forte 	}
672fcf3ce44SJohn Forte 
673fcf3ce44SJohn Forte 	(void) memset(&radius, 0, sizeof (iscsi_radius_props_t));
674fcf3ce44SJohn Forte 	radius.r_vers = ISCSI_INTERFACE_VERSION;
675fcf3ce44SJohn Forte 	radius.r_oid = (uint32_t)lhbaOid.objectSequenceNumber;
676fcf3ce44SJohn Forte 
677fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_RADIUS_GET, &radius) != 0) {
678fcf3ce44SJohn Forte 		(void) close(fd);
679fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
680fcf3ce44SJohn Forte 	}
681fcf3ce44SJohn Forte 
682fcf3ce44SJohn Forte 	(void) memset(config, 0, sizeof (SUN_IMA_RADIUS_CONFIG));
683fcf3ce44SJohn Forte 	if (radius.r_insize == sizeof (struct in_addr)) {
684fcf3ce44SJohn Forte 		/* IPv4 */
685fcf3ce44SJohn Forte 		af = AF_INET;
686fcf3ce44SJohn Forte 	} else if (radius.r_insize == sizeof (struct in6_addr)) {
687fcf3ce44SJohn Forte 		/* IPv6 */
688fcf3ce44SJohn Forte 		af = AF_INET6;
689fcf3ce44SJohn Forte 	} else {
690fcf3ce44SJohn Forte 		/*
691fcf3ce44SJohn Forte 		 * It's legitimate that the existing RADIUS record does not
692fcf3ce44SJohn Forte 		 * have configuration data.
693fcf3ce44SJohn Forte 		 */
694fcf3ce44SJohn Forte 		config->hostnameIpAddress[0] = '\0';
695fcf3ce44SJohn Forte 		config->port = 0;
696fcf3ce44SJohn Forte 		(void) close(fd);
697fcf3ce44SJohn Forte 		return (IMA_STATUS_SUCCESS);
698fcf3ce44SJohn Forte 	}
699fcf3ce44SJohn Forte 	(void) inet_ntop(af, (void *)&radius.r_addr.u_in4,
700fcf3ce44SJohn Forte 	    config->hostnameIpAddress, 256);
701fcf3ce44SJohn Forte 	config->port = radius.r_port;
702fcf3ce44SJohn Forte 	(void) memcpy(config->sharedSecret, &radius.r_shared_secret[0],
703fcf3ce44SJohn Forte 	    radius.r_shared_secret_len);
704fcf3ce44SJohn Forte 	config->sharedSecretLength = radius.r_shared_secret_len;
705fcf3ce44SJohn Forte 	config->sharedSecretValid = B_TRUE;
706fcf3ce44SJohn Forte 
707fcf3ce44SJohn Forte 	(void) close(fd);
708fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
709fcf3ce44SJohn Forte }
710fcf3ce44SJohn Forte 
711fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_SetInitiatorRadiusAccess(
712fcf3ce44SJohn Forte     IMA_OID lhbaOid,
713fcf3ce44SJohn Forte     IMA_BOOL radiusAccess
714fcf3ce44SJohn Forte )
715fcf3ce44SJohn Forte {
716fcf3ce44SJohn Forte 	int			fd;
717fcf3ce44SJohn Forte 	int			status;
718fcf3ce44SJohn Forte 	iscsi_radius_props_t	radius;
719fcf3ce44SJohn Forte 
720fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
721fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
722fcf3ce44SJohn Forte 	}
723fcf3ce44SJohn Forte 
724fcf3ce44SJohn Forte 	(void) memset(&radius, 0, sizeof (iscsi_radius_props_t));
725fcf3ce44SJohn Forte 	radius.r_vers = ISCSI_INTERFACE_VERSION;
726fcf3ce44SJohn Forte 	radius.r_oid = (uint32_t)lhbaOid.objectSequenceNumber;
727fcf3ce44SJohn Forte 	/* Get first because other data fields may already exist */
728fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_RADIUS_GET, &radius) != 0) {
729fcf3ce44SJohn Forte 		if (radiusAccess == IMA_TRUE) {
730fcf3ce44SJohn Forte 			/*
731fcf3ce44SJohn Forte 			 * Cannot enable RADIUS if no RADIUS configuration
732fcf3ce44SJohn Forte 			 * can be found.
733fcf3ce44SJohn Forte 			 */
734fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
735fcf3ce44SJohn Forte 			    "RADIUS config data not found - "
736fcf3ce44SJohn Forte 			    "cannot enable RADIUS, errno: %d", errno);
737fcf3ce44SJohn Forte 			(void) close(fd);
738fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
739fcf3ce44SJohn Forte 		} else {
740fcf3ce44SJohn Forte 			/* EMPTY */
741fcf3ce44SJohn Forte 			/* Otherwise it's fine to disable RADIUS */
742fcf3ce44SJohn Forte 		}
743fcf3ce44SJohn Forte 	}
744fcf3ce44SJohn Forte 
745fcf3ce44SJohn Forte 	if ((radius.r_insize != sizeof (struct in_addr)) &&
746fcf3ce44SJohn Forte 		(radius.r_insize != sizeof (struct in6_addr))) {
747fcf3ce44SJohn Forte 		/*
748fcf3ce44SJohn Forte 		 * Cannot enable RADIUS if no RADIUS configuration
749fcf3ce44SJohn Forte 		 * can be found.
750fcf3ce44SJohn Forte 		 */
751fcf3ce44SJohn Forte 		if (radiusAccess == IMA_TRUE) {
752fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
753fcf3ce44SJohn Forte 				"RADIUS config data not found - "
754fcf3ce44SJohn Forte 				"cannot enable RADIUS");
755fcf3ce44SJohn Forte 			(void) close(fd);
756fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
757fcf3ce44SJohn Forte 		}
758fcf3ce44SJohn Forte 	}
759fcf3ce44SJohn Forte 
760fcf3ce44SJohn Forte 	radius.r_radius_access = (radiusAccess == IMA_TRUE) ?
761fcf3ce44SJohn Forte 	    B_TRUE : B_FALSE;
762fcf3ce44SJohn Forte 
763fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_RADIUS_SET, &radius) != 0) {
764fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
765fcf3ce44SJohn Forte 		    "ISCSI_RADIUS_SET ioctl failed, errno: %d", errno);
766fcf3ce44SJohn Forte 		(void) close(fd);
767fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
768fcf3ce44SJohn Forte 	}
769fcf3ce44SJohn Forte 
770fcf3ce44SJohn Forte 	(void) close(fd);
771fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
772fcf3ce44SJohn Forte }
773fcf3ce44SJohn Forte 
774fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_GetInitiatorRadiusAccess(
775fcf3ce44SJohn Forte     IMA_OID lhbaOid,
776fcf3ce44SJohn Forte     IMA_BOOL *radiusAccess
777fcf3ce44SJohn Forte )
778fcf3ce44SJohn Forte {
779fcf3ce44SJohn Forte 	int			fd;
780fcf3ce44SJohn Forte 	int			status;
781fcf3ce44SJohn Forte 	iscsi_radius_props_t	radius;
782fcf3ce44SJohn Forte 
783fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
784fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
785fcf3ce44SJohn Forte 	}
786fcf3ce44SJohn Forte 
787fcf3ce44SJohn Forte 	(void) memset(&radius, 0, sizeof (iscsi_radius_props_t));
788fcf3ce44SJohn Forte 	radius.r_vers = ISCSI_INTERFACE_VERSION;
789fcf3ce44SJohn Forte 	radius.r_oid = (uint32_t)lhbaOid.objectSequenceNumber;
790fcf3ce44SJohn Forte 
791fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_RADIUS_GET, &radius) != 0) {
792fcf3ce44SJohn Forte 		(void) close(fd);
793fcf3ce44SJohn Forte 		if (errno == ENOENT) {
794fcf3ce44SJohn Forte 			return (IMA_ERROR_OBJECT_NOT_FOUND);
795fcf3ce44SJohn Forte 		} else {
796fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
797fcf3ce44SJohn Forte 		}
798fcf3ce44SJohn Forte 	}
799fcf3ce44SJohn Forte 
800fcf3ce44SJohn Forte 	*radiusAccess = (radius.r_radius_access == B_TRUE) ?
801fcf3ce44SJohn Forte 	    IMA_TRUE : IMA_FALSE;
802fcf3ce44SJohn Forte 
803fcf3ce44SJohn Forte 	(void) close(fd);
804fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
805fcf3ce44SJohn Forte }
806fcf3ce44SJohn Forte 
807fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_SendTargets(
808fcf3ce44SJohn Forte     IMA_NODE_NAME nodeName,
809fcf3ce44SJohn Forte     IMA_TARGET_ADDRESS address,
810fcf3ce44SJohn Forte     SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES **ppList
811fcf3ce44SJohn Forte )
812fcf3ce44SJohn Forte {
813fcf3ce44SJohn Forte 	char	*colonPos;
814fcf3ce44SJohn Forte 	char	discAddrStr[256];
815fcf3ce44SJohn Forte 	char	nodeNameStr[ISCSI_MAX_NAME_LEN];
816fcf3ce44SJohn Forte 	int	fd;
817fcf3ce44SJohn Forte 	int	ctr;
818fcf3ce44SJohn Forte 	int	stl_sz;
819fcf3ce44SJohn Forte 	int status;
820fcf3ce44SJohn Forte 	iscsi_sendtgts_list_t	*stl_hdr = NULL;
821fcf3ce44SJohn Forte 	IMA_BOOL		retry = IMA_TRUE;
822fcf3ce44SJohn Forte 	/* LINTED */
823fcf3ce44SJohn Forte 	IMA_IP_ADDRESS	    *ipAddr;
824fcf3ce44SJohn Forte 
825fcf3ce44SJohn Forte #define	SENDTGTS_DEFAULT_NUM_TARGETS	10
826fcf3ce44SJohn Forte 
827fcf3ce44SJohn Forte 	stl_sz = sizeof (*stl_hdr) + ((SENDTGTS_DEFAULT_NUM_TARGETS - 1) *
828fcf3ce44SJohn Forte 	    sizeof (iscsi_sendtgts_entry_t));
829fcf3ce44SJohn Forte 	stl_hdr = (iscsi_sendtgts_list_t *)calloc(1, stl_sz);
830fcf3ce44SJohn Forte 	if (stl_hdr == NULL) {
831fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
832fcf3ce44SJohn Forte 	}
833fcf3ce44SJohn Forte 	stl_hdr->stl_entry.e_vers = ISCSI_INTERFACE_VERSION;
834fcf3ce44SJohn Forte 	stl_hdr->stl_in_cnt = SENDTGTS_DEFAULT_NUM_TARGETS;
835fcf3ce44SJohn Forte 
836fcf3ce44SJohn Forte 	(void) wcstombs(nodeNameStr, nodeName, ISCSI_MAX_NAME_LEN);
837fcf3ce44SJohn Forte 
838fcf3ce44SJohn Forte 	colonPos = strchr(discAddrStr, ':');
839fcf3ce44SJohn Forte 	if (colonPos == NULL) {
840fcf3ce44SJohn Forte 		/* IPv4 */
841fcf3ce44SJohn Forte 		stl_hdr->stl_entry.e_insize = sizeof (struct in_addr);
842fcf3ce44SJohn Forte 	} else {
843fcf3ce44SJohn Forte 		/* IPv6 */
844fcf3ce44SJohn Forte 		stl_hdr->stl_entry.e_insize = sizeof (struct in6_addr);
845fcf3ce44SJohn Forte 	}
846fcf3ce44SJohn Forte 
847fcf3ce44SJohn Forte 	ipAddr = &address.hostnameIpAddress.id.ipAddress;
848fcf3ce44SJohn Forte 
849fcf3ce44SJohn Forte 	bcopy(address.hostnameIpAddress.id.ipAddress.ipAddress,
850fcf3ce44SJohn Forte 	    &stl_hdr->stl_entry.e_u, sizeof (ipAddr->ipAddress));
851fcf3ce44SJohn Forte 
852fcf3ce44SJohn Forte 	stl_hdr->stl_entry.e_port = address.portNumber;
853fcf3ce44SJohn Forte 
854fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
855fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
856fcf3ce44SJohn Forte 	}
857fcf3ce44SJohn Forte 
858fcf3ce44SJohn Forte retry_sendtgts:
859fcf3ce44SJohn Forte 	/*
860fcf3ce44SJohn Forte 	 * Issue ioctl to obtain the SendTargets list
861fcf3ce44SJohn Forte 	 */
862fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_SENDTGTS_GET, stl_hdr) != 0) {
863fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
864fcf3ce44SJohn Forte 		    "ISCSI_SENDTGTS_GET ioctl failed, errno: %d", errno);
865fcf3ce44SJohn Forte 		(void) close(fd);
866fcf3ce44SJohn Forte 		free(stl_hdr);
867fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
868fcf3ce44SJohn Forte 	}
869fcf3ce44SJohn Forte 
870fcf3ce44SJohn Forte 	/* check if all targets received */
871fcf3ce44SJohn Forte 	if (stl_hdr->stl_in_cnt < stl_hdr->stl_out_cnt) {
872fcf3ce44SJohn Forte 		if (retry == IMA_TRUE) {
873fcf3ce44SJohn Forte 			stl_sz = sizeof (*stl_hdr) +
874fcf3ce44SJohn Forte 			    ((stl_hdr->stl_out_cnt - 1) *
875fcf3ce44SJohn Forte 			    sizeof (iscsi_sendtgts_entry_t));
876fcf3ce44SJohn Forte 			stl_hdr = (iscsi_sendtgts_list_t *)
877fcf3ce44SJohn Forte 			    realloc(stl_hdr, stl_sz);
878fcf3ce44SJohn Forte 			if (stl_hdr == NULL) {
879fcf3ce44SJohn Forte 				(void) close(fd);
880fcf3ce44SJohn Forte 				return (IMA_ERROR_INSUFFICIENT_MEMORY);
881fcf3ce44SJohn Forte 			}
882fcf3ce44SJohn Forte 			stl_hdr->stl_in_cnt = stl_hdr->stl_out_cnt;
883fcf3ce44SJohn Forte 			retry = IMA_FALSE;
884fcf3ce44SJohn Forte 			goto retry_sendtgts;
885fcf3ce44SJohn Forte 		} else {
886fcf3ce44SJohn Forte 			/*
887fcf3ce44SJohn Forte 			 * don't retry after 2 attempts.  The target list
888fcf3ce44SJohn Forte 			 * shouldn't continue to growing. Justs continue
889fcf3ce44SJohn Forte 			 * on and display what was found.
890fcf3ce44SJohn Forte 			 */
891fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
892fcf3ce44SJohn Forte 			    "ISCSI_SENDTGTS_GET overflow: "
893fcf3ce44SJohn Forte 			    "failed to obtain all targets");
894fcf3ce44SJohn Forte 			stl_hdr->stl_out_cnt = stl_hdr->stl_in_cnt;
895fcf3ce44SJohn Forte 		}
896fcf3ce44SJohn Forte 	}
897fcf3ce44SJohn Forte 
898fcf3ce44SJohn Forte 	(void) close(fd);
899fcf3ce44SJohn Forte 
900fcf3ce44SJohn Forte 	/* allocate for caller return buffer */
901fcf3ce44SJohn Forte 	*ppList = (SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *)calloc(1,
902fcf3ce44SJohn Forte 	    sizeof (SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES) +
903fcf3ce44SJohn Forte 	    stl_hdr->stl_out_cnt * sizeof (SUN_IMA_DISC_ADDRESS_KEY));
904fcf3ce44SJohn Forte 	if (*ppList == NULL) {
905fcf3ce44SJohn Forte 		free(stl_hdr);
906fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
907fcf3ce44SJohn Forte 	}
908fcf3ce44SJohn Forte 
909fcf3ce44SJohn Forte 	(*ppList)->keyCount = stl_hdr->stl_out_cnt;
910fcf3ce44SJohn Forte 
911fcf3ce44SJohn Forte 	for (ctr = 0; ctr < stl_hdr->stl_out_cnt; ctr++) {
912fcf3ce44SJohn Forte 		(void) mbstowcs((*ppList)->keys[ctr].name,
913fcf3ce44SJohn Forte 		    (char *)stl_hdr->stl_list[ctr].ste_name,
914fcf3ce44SJohn Forte 		    IMA_NODE_NAME_LEN);
915fcf3ce44SJohn Forte 
916fcf3ce44SJohn Forte 		(*ppList)->keys[ctr].tpgt = stl_hdr->stl_list[ctr].ste_tpgt;
917fcf3ce44SJohn Forte 
918fcf3ce44SJohn Forte 		(*ppList)->keys[ctr].address.portNumber =
919fcf3ce44SJohn Forte 		    stl_hdr->stl_list[ctr].ste_ipaddr.a_port;
920fcf3ce44SJohn Forte 
921fcf3ce44SJohn Forte 		if (stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_insize ==
922fcf3ce44SJohn Forte 		    sizeof (struct in_addr)) {
923fcf3ce44SJohn Forte 			(*ppList)->keys[ctr].address.ipAddress.ipv4Address =
924fcf3ce44SJohn Forte 			    IMA_TRUE;
925fcf3ce44SJohn Forte 		} else if (stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_insize ==
926fcf3ce44SJohn Forte 		    sizeof (struct in6_addr)) {
927fcf3ce44SJohn Forte 			(*ppList)->keys[ctr].address.ipAddress.ipv4Address =
928fcf3ce44SJohn Forte 			    IMA_FALSE;
929fcf3ce44SJohn Forte 		} else {
930fcf3ce44SJohn Forte 			free(stl_hdr);
931fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
932fcf3ce44SJohn Forte 		}
933fcf3ce44SJohn Forte 
934fcf3ce44SJohn Forte 		(void) memcpy(&(*ppList)->keys[ctr].address.ipAddress.ipAddress,
935fcf3ce44SJohn Forte 		    &(stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_addr),
936fcf3ce44SJohn Forte 		    stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_insize);
937fcf3ce44SJohn Forte 	}
938fcf3ce44SJohn Forte 	free(stl_hdr);
939fcf3ce44SJohn Forte 
940fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
941fcf3ce44SJohn Forte }
942fcf3ce44SJohn Forte 
943fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_SetTargetBidirAuthFlag(
944fcf3ce44SJohn Forte     IMA_OID targetOid,
945fcf3ce44SJohn Forte     IMA_BOOL *bidirAuthFlag
946fcf3ce44SJohn Forte )
947fcf3ce44SJohn Forte {
948fcf3ce44SJohn Forte 	int fd;
949fcf3ce44SJohn Forte 	int status;
950fcf3ce44SJohn Forte 	iscsi_auth_props_t auth;
951fcf3ce44SJohn Forte 
952fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
953fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
954fcf3ce44SJohn Forte 	}
955fcf3ce44SJohn Forte 
956fcf3ce44SJohn Forte 	(void) memset(&auth, 0, sizeof (iscsi_auth_props_t));
957fcf3ce44SJohn Forte 	auth.a_vers = ISCSI_INTERFACE_VERSION;
958fcf3ce44SJohn Forte 	auth.a_oid = (uint32_t)targetOid.objectSequenceNumber;
959fcf3ce44SJohn Forte 	/* Get first because other data fields may already exist */
960fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_AUTH_GET, &auth) != 0) {
961fcf3ce44SJohn Forte 		/* EMPTY */
962fcf3ce44SJohn Forte 		/* It is fine if there is no other data fields. */
963fcf3ce44SJohn Forte 	}
964fcf3ce44SJohn Forte 	auth.a_bi_auth = (*bidirAuthFlag == IMA_TRUE) ? B_TRUE : B_FALSE;
965fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_AUTH_SET, &auth) != 0) {
966fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
967fcf3ce44SJohn Forte 		    "ISCSI_AUTH_SET ioctl failed, errno: %d", errno);
968fcf3ce44SJohn Forte 		(void) close(fd);
969fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
970fcf3ce44SJohn Forte 	}
971fcf3ce44SJohn Forte 
972fcf3ce44SJohn Forte 	(void) close(fd);
973fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
974fcf3ce44SJohn Forte }
975fcf3ce44SJohn Forte 
976fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_GetTargetBidirAuthFlag(
977fcf3ce44SJohn Forte     IMA_OID targetOid,
978fcf3ce44SJohn Forte     IMA_BOOL *bidirAuthFlag
979fcf3ce44SJohn Forte )
980fcf3ce44SJohn Forte {
981fcf3ce44SJohn Forte 	int fd;
982fcf3ce44SJohn Forte 	int status;
983fcf3ce44SJohn Forte 	iscsi_auth_props_t auth;
984fcf3ce44SJohn Forte 
985fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
986fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
987fcf3ce44SJohn Forte 	}
988fcf3ce44SJohn Forte 
989fcf3ce44SJohn Forte 	(void) memset(&auth, 0, sizeof (iscsi_auth_props_t));
990fcf3ce44SJohn Forte 	auth.a_vers = ISCSI_INTERFACE_VERSION;
991fcf3ce44SJohn Forte 	auth.a_oid = (uint32_t)targetOid.objectSequenceNumber;
992fcf3ce44SJohn Forte 
993fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_AUTH_GET, &auth) != 0) {
994fcf3ce44SJohn Forte 		(void) close(fd);
995fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
996fcf3ce44SJohn Forte 	}
997fcf3ce44SJohn Forte 
998fcf3ce44SJohn Forte 	*bidirAuthFlag = (auth.a_bi_auth == B_TRUE) ?
999fcf3ce44SJohn Forte 	    IMA_TRUE : IMA_FALSE;
1000fcf3ce44SJohn Forte 
1001fcf3ce44SJohn Forte 	(void) close(fd);
1002fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1003fcf3ce44SJohn Forte }
1004fcf3ce44SJohn Forte 
1005fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_CreateTargetOid(
1006fcf3ce44SJohn Forte     IMA_NODE_NAME targetName,
1007fcf3ce44SJohn Forte     IMA_OID *targetOid
1008fcf3ce44SJohn Forte )
1009fcf3ce44SJohn Forte {
1010fcf3ce44SJohn Forte 	int	    fd;
1011fcf3ce44SJohn Forte 	int		status;
1012fcf3ce44SJohn Forte 	iscsi_oid_t oid;
1013fcf3ce44SJohn Forte 
1014fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1015fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1016fcf3ce44SJohn Forte 	}
1017fcf3ce44SJohn Forte 
1018fcf3ce44SJohn Forte 	(void) memset(&oid, 0, sizeof (iscsi_oid_t));
1019fcf3ce44SJohn Forte 	(void) wcstombs((char *)oid.o_name, targetName, ISCSI_MAX_NAME_LEN);
1020fcf3ce44SJohn Forte 	oid.o_tpgt = ISCSI_DEFAULT_TPGT;
1021fcf3ce44SJohn Forte 	oid.o_vers = ISCSI_INTERFACE_VERSION;
1022fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_CREATE_OID, &oid) == -1) {
1023fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1024fcf3ce44SJohn Forte 		    "ISCSI_CREATE_OID ioctl failed, errno: %d", errno);
1025fcf3ce44SJohn Forte 		(void) close(fd);
1026fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1027fcf3ce44SJohn Forte 	}
1028fcf3ce44SJohn Forte 
1029fcf3ce44SJohn Forte 	targetOid->objectType = IMA_OBJECT_TYPE_TARGET;
1030fcf3ce44SJohn Forte 	targetOid->ownerId = 1;
1031fcf3ce44SJohn Forte 	targetOid->objectSequenceNumber = oid.o_oid;
1032fcf3ce44SJohn Forte 
1033fcf3ce44SJohn Forte 	(void) close(fd);
1034fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1035fcf3ce44SJohn Forte }
1036fcf3ce44SJohn Forte 
1037fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_RemoveTargetParam(
1038fcf3ce44SJohn Forte 		IMA_OID targetOid
1039fcf3ce44SJohn Forte )
1040fcf3ce44SJohn Forte {
1041fcf3ce44SJohn Forte 	entry_t	entry;
1042fcf3ce44SJohn Forte 	int	fd;
1043fcf3ce44SJohn Forte 	int status;
1044fcf3ce44SJohn Forte 	iscsi_auth_props_t auth_p;
1045fcf3ce44SJohn Forte 	iscsi_chap_props_t chap_p;
1046fcf3ce44SJohn Forte 
1047fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1048fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1049fcf3ce44SJohn Forte 	}
1050fcf3ce44SJohn Forte 
1051fcf3ce44SJohn Forte 	(void) memset(&entry, 0, sizeof (entry_t));
1052fcf3ce44SJohn Forte 	entry.e_vers = ISCSI_INTERFACE_VERSION;
1053fcf3ce44SJohn Forte 	entry.e_oid = (uint32_t)targetOid.objectSequenceNumber;
1054fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_TARGET_PARAM_CLEAR, &entry)) {
1055fcf3ce44SJohn Forte 		/*
1056fcf3ce44SJohn Forte 		 * It could be that the target exists but the associated
1057fcf3ce44SJohn Forte 		 * target_param does not, and that is legitimate.
1058fcf3ce44SJohn Forte 		 */
1059fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1060fcf3ce44SJohn Forte 		    "ISCSI_TARGET_PARAM_CLEAR ioctl failed, errno: %d", errno);
1061fcf3ce44SJohn Forte 	}
1062fcf3ce44SJohn Forte 
1063fcf3ce44SJohn Forte 	/* Issue ISCSI_CHAP_CLEAR ioctl */
1064fcf3ce44SJohn Forte 	(void) memset(&chap_p, 0, sizeof (iscsi_chap_props_t));
1065fcf3ce44SJohn Forte 	chap_p.c_vers = ISCSI_INTERFACE_VERSION;
1066fcf3ce44SJohn Forte 	chap_p.c_oid = (uint32_t)targetOid.objectSequenceNumber;
1067fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_CHAP_CLEAR, &chap_p) != 0) {
1068fcf3ce44SJohn Forte 		/*
1069fcf3ce44SJohn Forte 		 * It could be that the CHAP of this target has never
1070fcf3ce44SJohn Forte 		 * been set.
1071fcf3ce44SJohn Forte 		 */
1072fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1073fcf3ce44SJohn Forte 		    "ISCSI_CHAP_CLEAR ioctl failed, errno: %d", errno);
1074fcf3ce44SJohn Forte 	}
1075fcf3ce44SJohn Forte 
1076b4243054SSheng-Liang Eric Zhang 	/*
1077b4243054SSheng-Liang Eric Zhang 	 * Issue ISCSI_AUTH_CLEAR ioctl, in which the authentication information
1078b4243054SSheng-Liang Eric Zhang 	 * is removed and the target that is not discovered by initiator
1079b4243054SSheng-Liang Eric Zhang 	 * is removed from the memory. So this ioctl should be called at last
1080b4243054SSheng-Liang Eric Zhang 	 */
1081fcf3ce44SJohn Forte 	(void) memset(&auth_p, 0, sizeof (iscsi_auth_props_t));
1082fcf3ce44SJohn Forte 	auth_p.a_vers = ISCSI_INTERFACE_VERSION;
1083fcf3ce44SJohn Forte 	auth_p.a_oid = (uint32_t)targetOid.objectSequenceNumber;
1084fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_AUTH_CLEAR, &auth_p) != 0) {
1085fcf3ce44SJohn Forte 		/*
1086fcf3ce44SJohn Forte 		 * It could be that the auth data of this target has
1087fcf3ce44SJohn Forte 		 * never been set.
1088fcf3ce44SJohn Forte 		 */
1089fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1090fcf3ce44SJohn Forte 		    "ISCSI_AUTH_CLEAR ioctl failed, errno: %d", errno);
1091fcf3ce44SJohn Forte 	}
1092fcf3ce44SJohn Forte 
1093fcf3ce44SJohn Forte 	(void) close(fd);
1094fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1095fcf3ce44SJohn Forte }
1096fcf3ce44SJohn Forte 
1097fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_SetHeaderDigest(
1098fcf3ce44SJohn Forte 	IMA_OID oid,
1099fcf3ce44SJohn Forte 	IMA_UINT algorithmCount,
1100fcf3ce44SJohn Forte 	const SUN_IMA_DIGEST_ALGORITHM *algorithmList
1101fcf3ce44SJohn Forte )
1102fcf3ce44SJohn Forte {
1103fcf3ce44SJohn Forte 	IMA_MIN_MAX_VALUE mv;
1104fcf3ce44SJohn Forte 	uint32_t digest_algorithm;
1105fcf3ce44SJohn Forte 
1106fcf3ce44SJohn Forte 	/* We only support one preference of digest algorithm. */
1107fcf3ce44SJohn Forte 	if (algorithmCount > 1) {
1108fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1109fcf3ce44SJohn Forte 		    "More than one digest algorithm specified.");
1110fcf3ce44SJohn Forte 		return (IMA_ERROR_NOT_SUPPORTED);
1111fcf3ce44SJohn Forte 	}
1112fcf3ce44SJohn Forte 	switch (algorithmList[0]) {
1113fcf3ce44SJohn Forte 	case SUN_IMA_DIGEST_NONE:
1114fcf3ce44SJohn Forte 		digest_algorithm = ISCSI_DIGEST_NONE;
1115fcf3ce44SJohn Forte 		break;
1116fcf3ce44SJohn Forte 	case SUN_IMA_DIGEST_CRC32:
1117fcf3ce44SJohn Forte 		digest_algorithm = ISCSI_DIGEST_CRC32C;
1118fcf3ce44SJohn Forte 		break;
1119fcf3ce44SJohn Forte 	default:
1120fcf3ce44SJohn Forte 		digest_algorithm = ISCSI_DIGEST_NONE;
1121fcf3ce44SJohn Forte 		break;
1122fcf3ce44SJohn Forte 	}
1123fcf3ce44SJohn Forte 	mv.currentValue = digest_algorithm;
1124fcf3ce44SJohn Forte 	return (setISCSINodeParameter(MIN_MAX_PARAM, &oid, &mv,
1125fcf3ce44SJohn Forte 	    ISCSI_LOGIN_PARAM_HEADER_DIGEST));
1126fcf3ce44SJohn Forte }
1127fcf3ce44SJohn Forte 
1128fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_SetDataDigest(
1129fcf3ce44SJohn Forte 	IMA_OID oid,
1130fcf3ce44SJohn Forte 	IMA_UINT algorithmCount,
1131fcf3ce44SJohn Forte 	const SUN_IMA_DIGEST_ALGORITHM *algorithmList
1132fcf3ce44SJohn Forte )
1133fcf3ce44SJohn Forte {
1134fcf3ce44SJohn Forte 	IMA_MIN_MAX_VALUE mv;
1135fcf3ce44SJohn Forte 	uint32_t digest_algorithm;
1136fcf3ce44SJohn Forte 
1137fcf3ce44SJohn Forte 	/* We only support one preference of digest algorithm. */
1138fcf3ce44SJohn Forte 	if (algorithmCount > 1) {
1139fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1140fcf3ce44SJohn Forte 		    "More than one digest algorithm specified.");
1141fcf3ce44SJohn Forte 		return (IMA_ERROR_NOT_SUPPORTED);
1142fcf3ce44SJohn Forte 	}
1143fcf3ce44SJohn Forte 	switch (algorithmList[0]) {
1144fcf3ce44SJohn Forte 	case SUN_IMA_DIGEST_NONE:
1145fcf3ce44SJohn Forte 		digest_algorithm = ISCSI_DIGEST_NONE;
1146fcf3ce44SJohn Forte 		break;
1147fcf3ce44SJohn Forte 	case SUN_IMA_DIGEST_CRC32:
1148fcf3ce44SJohn Forte 		digest_algorithm = ISCSI_DIGEST_CRC32C;
1149fcf3ce44SJohn Forte 		break;
1150fcf3ce44SJohn Forte 	default:
1151fcf3ce44SJohn Forte 		digest_algorithm = ISCSI_DIGEST_NONE;
1152fcf3ce44SJohn Forte 		break;
1153fcf3ce44SJohn Forte 	}
1154fcf3ce44SJohn Forte 	mv.currentValue = digest_algorithm;
1155fcf3ce44SJohn Forte 	return (setISCSINodeParameter(MIN_MAX_PARAM, &oid, &mv,
1156fcf3ce44SJohn Forte 	    ISCSI_LOGIN_PARAM_DATA_DIGEST));
1157fcf3ce44SJohn Forte }
1158fcf3ce44SJohn Forte 
1159fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_GetHeaderDigest(
1160fcf3ce44SJohn Forte 	IMA_OID oid,
1161fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm
1162fcf3ce44SJohn Forte )
1163fcf3ce44SJohn Forte {
1164fcf3ce44SJohn Forte 	return (getDigest(oid, ISCSI_LOGIN_PARAM_HEADER_DIGEST, algorithm));
1165fcf3ce44SJohn Forte }
1166fcf3ce44SJohn Forte 
1167fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_GetDataDigest(
1168fcf3ce44SJohn Forte 	IMA_OID oid,
1169fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm
1170fcf3ce44SJohn Forte )
1171fcf3ce44SJohn Forte {
1172fcf3ce44SJohn Forte 	return (getDigest(oid, ISCSI_LOGIN_PARAM_DATA_DIGEST, algorithm));
1173fcf3ce44SJohn Forte }
1174fcf3ce44SJohn Forte 
11751f6352c6Syi zhang - Sun Microsystems - Beijing China typedef struct walk_devlink {
11761f6352c6Syi zhang - Sun Microsystems - Beijing China 	char *path;
11771f6352c6Syi zhang - Sun Microsystems - Beijing China 	size_t len;
11781f6352c6Syi zhang - Sun Microsystems - Beijing China 	char **linkpp;
11791f6352c6Syi zhang - Sun Microsystems - Beijing China } walk_devlink_t;
11801f6352c6Syi zhang - Sun Microsystems - Beijing China 
1181fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_GetLuProperties(
1182fcf3ce44SJohn Forte 		IMA_OID luId,
1183fcf3ce44SJohn Forte 		SUN_IMA_LU_PROPERTIES *pProps
1184fcf3ce44SJohn Forte )
1185fcf3ce44SJohn Forte {
1186fcf3ce44SJohn Forte 	IMA_STATUS		status;
1187fcf3ce44SJohn Forte 	iscsi_lun_list_t	*pLunList;
1188fcf3ce44SJohn Forte 	int			j;
1189fcf3ce44SJohn Forte 	IMA_BOOL		lunMatch = IMA_FALSE;
1190fcf3ce44SJohn Forte 	int			fd;
1191fcf3ce44SJohn Forte 	int			openStatus;
1192fcf3ce44SJohn Forte 	iscsi_lun_props_t	lun;
1193fcf3ce44SJohn Forte 	di_devlink_handle_t	hdl;
11941f6352c6Syi zhang - Sun Microsystems - Beijing China 	walk_devlink_t		warg;
11951f6352c6Syi zhang - Sun Microsystems - Beijing China 	char			*minor_path, *devlinkp, lunpath[MAXPATHLEN];
1196fcf3ce44SJohn Forte 
1197fcf3ce44SJohn Forte 	if (luId.objectType != IMA_OBJECT_TYPE_LU) {
1198fcf3ce44SJohn Forte 		return (IMA_ERROR_INCORRECT_OBJECT_TYPE);
1199fcf3ce44SJohn Forte 	}
1200fcf3ce44SJohn Forte 
1201fcf3ce44SJohn Forte 	/*
1202fcf3ce44SJohn Forte 	 * get list of lun oids for all targets
1203fcf3ce44SJohn Forte 	 */
1204fcf3ce44SJohn Forte 	status = get_target_lun_oid_list(NULL, &pLunList);
1205fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
1206fcf3ce44SJohn Forte 		return (status);
1207fcf3ce44SJohn Forte 	}
1208fcf3ce44SJohn Forte 	for (j = 0; j < pLunList->ll_out_cnt; j++) {
1209fcf3ce44SJohn Forte 		/*
1210fcf3ce44SJohn Forte 		 * for each lun, check if match is found
1211fcf3ce44SJohn Forte 		 */
1212fcf3ce44SJohn Forte 		if (pLunList->ll_luns[j].l_oid == luId.objectSequenceNumber) {
1213fcf3ce44SJohn Forte 			/*
1214fcf3ce44SJohn Forte 			 * match found, break out of lun loop
1215fcf3ce44SJohn Forte 			 */
1216fcf3ce44SJohn Forte 			lunMatch = IMA_TRUE;
1217fcf3ce44SJohn Forte 			break;
1218fcf3ce44SJohn Forte 		}
1219fcf3ce44SJohn Forte 	}
1220fcf3ce44SJohn Forte 
1221fcf3ce44SJohn Forte 	if (lunMatch == IMA_TRUE) {
1222fcf3ce44SJohn Forte 		(void) memset(&lun, 0, sizeof (iscsi_lun_props_t));
1223fcf3ce44SJohn Forte 		lun.lp_vers = ISCSI_INTERFACE_VERSION;
1224fcf3ce44SJohn Forte 		lun.lp_tgt_oid = pLunList->ll_luns[j].l_tgt_oid;
1225fcf3ce44SJohn Forte 		lun.lp_oid = pLunList->ll_luns[j].l_oid;
1226fcf3ce44SJohn Forte 	}
1227fcf3ce44SJohn Forte 
1228fcf3ce44SJohn Forte 	free(pLunList);
1229fcf3ce44SJohn Forte 
1230fcf3ce44SJohn Forte 	if (lunMatch == IMA_FALSE) {
1231fcf3ce44SJohn Forte 		return (IMA_ERROR_OBJECT_NOT_FOUND);
1232fcf3ce44SJohn Forte 	}
1233fcf3ce44SJohn Forte 
1234fcf3ce44SJohn Forte 	/*
1235fcf3ce44SJohn Forte 	 * get lun properties
1236fcf3ce44SJohn Forte 	 */
1237fcf3ce44SJohn Forte 	if ((openStatus = open_driver(&fd))) {
1238fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | openStatus);
1239fcf3ce44SJohn Forte 	}
1240fcf3ce44SJohn Forte 
1241fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_LUN_PROPS_GET, &lun)) {
1242fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1243fcf3ce44SJohn Forte 		    "ISCSI_LUN_PROPS_GET ioctl failed, errno: %d", errno);
1244fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1245fcf3ce44SJohn Forte 	}
1246fcf3ce44SJohn Forte 
1247fcf3ce44SJohn Forte 	(void) close(fd);
1248fcf3ce44SJohn Forte 
1249fcf3ce44SJohn Forte 	/*
1250fcf3ce44SJohn Forte 	 * set property values
1251fcf3ce44SJohn Forte 	 */
1252fcf3ce44SJohn Forte 	pProps->imaProps.associatedTargetOid.objectType =
1253fcf3ce44SJohn Forte 	    IMA_OBJECT_TYPE_TARGET;
1254fcf3ce44SJohn Forte 	pProps->imaProps.associatedTargetOid.ownerId = 1;
1255fcf3ce44SJohn Forte 	pProps->imaProps.associatedTargetOid.objectSequenceNumber = lun.lp_oid;
1256fcf3ce44SJohn Forte 	pProps->imaProps.targetLun = (IMA_UINT64)lun.lp_num;
1257fcf3ce44SJohn Forte 	(void) strncpy(pProps->vendorId, lun.lp_vid, SUN_IMA_LU_VENDOR_ID_LEN);
1258fcf3ce44SJohn Forte 	(void) strncpy(pProps->productId, lun.lp_pid,
1259fcf3ce44SJohn Forte 	    SUN_IMA_LU_PRODUCT_ID_LEN);
1260fcf3ce44SJohn Forte 	/*
1261fcf3ce44SJohn Forte 	 * lun.lp_status is defined as
1262fcf3ce44SJohn Forte 	 *	LunValid = 0
1263fcf3ce44SJohn Forte 	 *	LunDoesNotExist = 1
1264fcf3ce44SJohn Forte 	 * IMA_LU_PROPS.exposedtoOS is defined as an IMA_BOOL
1265fcf3ce44SJohn Forte 	 *	IMA_TRUE = 1
1266fcf3ce44SJohn Forte 	 *	IMA_FALSE = 0
1267fcf3ce44SJohn Forte 	 */
1268fcf3ce44SJohn Forte 	pProps->imaProps.exposedToOs = !lun.lp_status;
1269fcf3ce44SJohn Forte 	if (gmtime_r(&lun.lp_time_online, &pProps->imaProps.timeExposedToOs)
1270fcf3ce44SJohn Forte 	    == NULL) {
1271fcf3ce44SJohn Forte 		(void) memset(&pProps->imaProps.timeExposedToOs, 0,
1272fcf3ce44SJohn Forte 		    sizeof (pProps->imaProps.timeExposedToOs));
1273fcf3ce44SJohn Forte 	}
1274fcf3ce44SJohn Forte 
1275fcf3ce44SJohn Forte 	if (lun.lp_status == LunValid) {
12761f6352c6Syi zhang - Sun Microsystems - Beijing China 		if ((strlen(lun.lp_pathname) + strlen("/devices")) >
12771f6352c6Syi zhang - Sun Microsystems - Beijing China 		    (MAXPATHLEN -1)) {
12781f6352c6Syi zhang - Sun Microsystems - Beijing China 			/*
12791f6352c6Syi zhang - Sun Microsystems - Beijing China 			 * lun.lp_pathname length too long
12801f6352c6Syi zhang - Sun Microsystems - Beijing China 			 */
12811f6352c6Syi zhang - Sun Microsystems - Beijing China 			pProps->imaProps.osDeviceNameValid = IMA_FALSE;
12821f6352c6Syi zhang - Sun Microsystems - Beijing China 			pProps->imaProps.osParallelIdsValid = IMA_FALSE;
12831f6352c6Syi zhang - Sun Microsystems - Beijing China 			return (IMA_STATUS_SUCCESS);
12841f6352c6Syi zhang - Sun Microsystems - Beijing China 		}
12851f6352c6Syi zhang - Sun Microsystems - Beijing China 		if ((strstr(lun.lp_pathname, "st@") != NULL) ||
12861f6352c6Syi zhang - Sun Microsystems - Beijing China 		    (strstr(lun.lp_pathname, "tape@") != NULL)) {
12871f6352c6Syi zhang - Sun Microsystems - Beijing China 			(void) strlcat(lun.lp_pathname, ":n", MAXPATHLEN);
12881f6352c6Syi zhang - Sun Microsystems - Beijing China 		} else if ((strstr(lun.lp_pathname, "sd@") != NULL) ||
1289fcf3ce44SJohn Forte 		    (strstr(lun.lp_pathname, "ssd@") != NULL) ||
1290fcf3ce44SJohn Forte 		    (strstr(lun.lp_pathname, "disk@") != NULL)) {
1291fcf3ce44SJohn Forte 			/*
1292fcf3ce44SJohn Forte 			 * modify returned pathname to obtain the 2nd slice
1293fcf3ce44SJohn Forte 			 * of the raw disk
1294fcf3ce44SJohn Forte 			 */
12951f6352c6Syi zhang - Sun Microsystems - Beijing China 			(void) strlcat(lun.lp_pathname, ":c,raw", MAXPATHLEN);
12961f6352c6Syi zhang - Sun Microsystems - Beijing China 		} else if ((strstr(lun.lp_pathname, "ses@") != NULL) ||
12971f6352c6Syi zhang - Sun Microsystems - Beijing China 		    (strstr(lun.lp_pathname, "enclosure@") != NULL)) {
12981f6352c6Syi zhang - Sun Microsystems - Beijing China 			(void) strlcat(lun.lp_pathname, ":0", MAXPATHLEN);
1299fcf3ce44SJohn Forte 		}
1300fcf3ce44SJohn Forte 
13011f6352c6Syi zhang - Sun Microsystems - Beijing China 		(void) snprintf(lunpath, sizeof (lun.lp_pathname),
13021f6352c6Syi zhang - Sun Microsystems - Beijing China 		    "/devices%s", lun.lp_pathname);
13031f6352c6Syi zhang - Sun Microsystems - Beijing China 		if (strchr(lunpath, ':')) {
13041f6352c6Syi zhang - Sun Microsystems - Beijing China 			minor_path = lunpath;
13051f6352c6Syi zhang - Sun Microsystems - Beijing China 			if (strstr(minor_path, "/devices") != NULL) {
13061f6352c6Syi zhang - Sun Microsystems - Beijing China 				minor_path = lunpath +
13071f6352c6Syi zhang - Sun Microsystems - Beijing China 				    strlen("/devices");
13081f6352c6Syi zhang - Sun Microsystems - Beijing China 			} else {
13091f6352c6Syi zhang - Sun Microsystems - Beijing China 				minor_path = lunpath;
13101f6352c6Syi zhang - Sun Microsystems - Beijing China 			}
13111f6352c6Syi zhang - Sun Microsystems - Beijing China 			warg.path = NULL;
13121f6352c6Syi zhang - Sun Microsystems - Beijing China 		} else {
13131f6352c6Syi zhang - Sun Microsystems - Beijing China 			minor_path = NULL;
13141f6352c6Syi zhang - Sun Microsystems - Beijing China 			warg.len = strlen(lunpath);
13151f6352c6Syi zhang - Sun Microsystems - Beijing China 			warg.path = lunpath;
13161f6352c6Syi zhang - Sun Microsystems - Beijing China 		}
13171f6352c6Syi zhang - Sun Microsystems - Beijing China 		devlinkp = NULL;
13181f6352c6Syi zhang - Sun Microsystems - Beijing China 		warg.linkpp = &devlinkp;
13191f6352c6Syi zhang - Sun Microsystems - Beijing China 
1320fcf3ce44SJohn Forte 		/*
1321fcf3ce44SJohn Forte 		 * Pathname returned by driver is the physical device path.
1322fcf3ce44SJohn Forte 		 * This name needs to be converted to the OS device name.
1323fcf3ce44SJohn Forte 		 */
1324fcf3ce44SJohn Forte 		if (hdl = di_devlink_init(lun.lp_pathname, DI_MAKE_LINK)) {
1325fcf3ce44SJohn Forte 			pProps->imaProps.osDeviceName[0] = L'\0';
13261f6352c6Syi zhang - Sun Microsystems - Beijing China 			(void) di_devlink_walk(hdl, NULL, minor_path,
13271f6352c6Syi zhang - Sun Microsystems - Beijing China 			    DI_PRIMARY_LINK, (void *)&warg, get_lun_devlink);
13281f6352c6Syi zhang - Sun Microsystems - Beijing China 			if (devlinkp != NULL) {
1329fcf3ce44SJohn Forte 				/* OS device name synchronously made */
13301f6352c6Syi zhang - Sun Microsystems - Beijing China 				(void) mbstowcs(pProps->imaProps.osDeviceName,
13311f6352c6Syi zhang - Sun Microsystems - Beijing China 				    devlinkp, MAXPATHLEN);
13321f6352c6Syi zhang - Sun Microsystems - Beijing China 				free(devlinkp);
1333fcf3ce44SJohn Forte 				pProps->imaProps.osDeviceNameValid = IMA_TRUE;
1334fcf3ce44SJohn Forte 			} else {
1335fcf3ce44SJohn Forte 				pProps->imaProps.osDeviceNameValid = IMA_FALSE;
1336fcf3ce44SJohn Forte 			}
1337fcf3ce44SJohn Forte 
1338fcf3ce44SJohn Forte 			(void) di_devlink_fini(&hdl);
1339fcf3ce44SJohn Forte 
1340fcf3ce44SJohn Forte 		} else {
1341fcf3ce44SJohn Forte 			pProps->imaProps.osDeviceNameValid = IMA_FALSE;
1342fcf3ce44SJohn Forte 		}
1343fcf3ce44SJohn Forte 
1344fcf3ce44SJohn Forte 	} else {
1345fcf3ce44SJohn Forte 		pProps->imaProps.osDeviceNameValid = IMA_FALSE;
1346fcf3ce44SJohn Forte 	}
1347fcf3ce44SJohn Forte 
1348fcf3ce44SJohn Forte 	pProps->imaProps.osParallelIdsValid = IMA_FALSE;
1349fcf3ce44SJohn Forte 
1350fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1351fcf3ce44SJohn Forte }
1352fcf3ce44SJohn Forte 
1353fcf3ce44SJohn Forte static int
13541f6352c6Syi zhang - Sun Microsystems - Beijing China get_lun_devlink(di_devlink_t link, void *arg)
1355fcf3ce44SJohn Forte {
13561f6352c6Syi zhang - Sun Microsystems - Beijing China 	walk_devlink_t *warg = (walk_devlink_t *)arg;
13571f6352c6Syi zhang - Sun Microsystems - Beijing China 	if (warg->path) {
13581f6352c6Syi zhang - Sun Microsystems - Beijing China 		char *content = (char *)di_devlink_content(link);
13591f6352c6Syi zhang - Sun Microsystems - Beijing China 		char *start = strstr(content, "/devices");
13601f6352c6Syi zhang - Sun Microsystems - Beijing China 		if (start == NULL ||
13611f6352c6Syi zhang - Sun Microsystems - Beijing China 		    strncmp(start, warg->path, warg->len) != 0 ||
13621f6352c6Syi zhang - Sun Microsystems - Beijing China 		    start[warg->len] != ':')
13631f6352c6Syi zhang - Sun Microsystems - Beijing China 			return (DI_WALK_CONTINUE);
1364fcf3ce44SJohn Forte 	}
1365fcf3ce44SJohn Forte 
13661f6352c6Syi zhang - Sun Microsystems - Beijing China 	*(warg->linkpp) = strdup(di_devlink_path(link));
13671f6352c6Syi zhang - Sun Microsystems - Beijing China 	return (DI_WALK_TERMINATE);
13681f6352c6Syi zhang - Sun Microsystems - Beijing China 
1369fcf3ce44SJohn Forte }
1370fcf3ce44SJohn Forte 
1371fcf3ce44SJohn Forte /*
1372fcf3ce44SJohn Forte  * SUN_IMA_GetConnectionOidList -
1373fcf3ce44SJohn Forte  *
1374fcf3ce44SJohn Forte  * Non-IMA defined function.
1375fcf3ce44SJohn Forte  */
1376fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_GetConnOidList(
1377fcf3ce44SJohn Forte 	IMA_OID			*oid,
1378fcf3ce44SJohn Forte 	IMA_OID_LIST		**ppList
1379fcf3ce44SJohn Forte )
1380fcf3ce44SJohn Forte {
1381fcf3ce44SJohn Forte 	IMA_STATUS		imaStatus;
1382fcf3ce44SJohn Forte 	IMA_OID_LIST		*imaOidList;
1383fcf3ce44SJohn Forte 	iscsi_conn_list_t	*iscsiConnList = NULL;
1384fcf3ce44SJohn Forte 	int			i;
1385fcf3ce44SJohn Forte 	size_t			allocLen;
1386fcf3ce44SJohn Forte 
1387fcf3ce44SJohn Forte 	if ((lhbaObjectId.objectType == oid->objectType) &&
1388fcf3ce44SJohn Forte 	    (lhbaObjectId.ownerId == oid->ownerId) &&
1389fcf3ce44SJohn Forte 	    (lhbaObjectId.objectSequenceNumber == oid->objectSequenceNumber)) {
1390fcf3ce44SJohn Forte 		imaStatus = getConnOidList(NULL, &iscsiConnList);
1391fcf3ce44SJohn Forte 	} else {
1392fcf3ce44SJohn Forte 		if (oid->objectType == IMA_OBJECT_TYPE_TARGET) {
1393fcf3ce44SJohn Forte 			imaStatus = getConnOidList(oid, &iscsiConnList);
1394fcf3ce44SJohn Forte 		} else {
1395fcf3ce44SJohn Forte 			return (IMA_ERROR_INCORRECT_OBJECT_TYPE);
1396fcf3ce44SJohn Forte 		}
1397fcf3ce44SJohn Forte 	}
1398fcf3ce44SJohn Forte 	if (imaStatus != IMA_STATUS_SUCCESS) {
1399fcf3ce44SJohn Forte 		return (imaStatus);
1400fcf3ce44SJohn Forte 	}
1401fcf3ce44SJohn Forte 
1402fcf3ce44SJohn Forte 	/*
1403fcf3ce44SJohn Forte 	 * Based on the results a SUN_IMA_CONN_LIST structure is allocated.
1404fcf3ce44SJohn Forte 	 */
1405fcf3ce44SJohn Forte 	allocLen  = iscsiConnList->cl_out_cnt * sizeof (IMA_OID);
1406fcf3ce44SJohn Forte 	allocLen += sizeof (IMA_OID_LIST) - sizeof (IMA_OID);
1407fcf3ce44SJohn Forte 	imaOidList = (IMA_OID_LIST *)calloc(1, allocLen);
1408fcf3ce44SJohn Forte 
1409fcf3ce44SJohn Forte 	if (imaOidList == NULL) {
1410fcf3ce44SJohn Forte 		free(iscsiConnList);
1411fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
1412fcf3ce44SJohn Forte 	}
1413fcf3ce44SJohn Forte 
1414fcf3ce44SJohn Forte 	/* The data is transfered from iscsiConnList to imaConnList. */
1415fcf3ce44SJohn Forte 	imaOidList->oidCount = iscsiConnList->cl_out_cnt;
1416fcf3ce44SJohn Forte 	for (i = 0; i < iscsiConnList->cl_out_cnt; i++) {
1417fcf3ce44SJohn Forte 		imaOidList->oids[i].objectType = SUN_IMA_OBJECT_TYPE_CONN;
1418fcf3ce44SJohn Forte 		imaOidList->oids[i].ownerId = 1;
1419fcf3ce44SJohn Forte 		imaOidList->oids[i].objectSequenceNumber =
1420fcf3ce44SJohn Forte 		    iscsiConnList->cl_list[i].c_oid;
1421fcf3ce44SJohn Forte 	}
1422fcf3ce44SJohn Forte 	/* The pointer to the SUN_IMA_CONN_LIST structure is returned. */
1423fcf3ce44SJohn Forte 	*ppList = imaOidList;
1424fcf3ce44SJohn Forte 
1425fcf3ce44SJohn Forte 	free(iscsiConnList);
1426fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1427fcf3ce44SJohn Forte }
1428fcf3ce44SJohn Forte 
1429fcf3ce44SJohn Forte /*
1430fcf3ce44SJohn Forte  * SUN_IMA_GetConnProperties -
1431fcf3ce44SJohn Forte  *
1432fcf3ce44SJohn Forte  * Non-IMA defined function.
1433fcf3ce44SJohn Forte  */
1434fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_GetConnProperties(
1435fcf3ce44SJohn Forte 	IMA_OID			*connOid,
1436fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES	**pProps
1437fcf3ce44SJohn Forte )
1438fcf3ce44SJohn Forte {
1439fcf3ce44SJohn Forte 	iscsi_conn_list_t	*pConnList;
1440fcf3ce44SJohn Forte 	iscsi_conn_props_t	*pConnProps;
1441fcf3ce44SJohn Forte 	/* LINTED */
1442fcf3ce44SJohn Forte 	struct sockaddr_in6	*addrIn6;
1443fcf3ce44SJohn Forte 	/* LINTED */
1444fcf3ce44SJohn Forte 	struct sockaddr_in	*addrIn;
1445fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES	*pImaConnProps;
1446fcf3ce44SJohn Forte 	IMA_STATUS		imaStatus;
1447fcf3ce44SJohn Forte 	int			i;
1448fcf3ce44SJohn Forte 
1449fcf3ce44SJohn Forte 	/* If there is any error *pProps should be set to NULL */
1450fcf3ce44SJohn Forte 	*pProps = NULL;
1451fcf3ce44SJohn Forte 
1452fcf3ce44SJohn Forte 	pImaConnProps = (SUN_IMA_CONN_PROPERTIES *)calloc(1,
1453fcf3ce44SJohn Forte 	    sizeof (SUN_IMA_CONN_PROPERTIES));
1454fcf3ce44SJohn Forte 
1455fcf3ce44SJohn Forte 	if (pImaConnProps == NULL) {
1456fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
1457fcf3ce44SJohn Forte 	}
1458fcf3ce44SJohn Forte 
1459fcf3ce44SJohn Forte 	imaStatus = getConnOidList(NULL, &pConnList);
1460fcf3ce44SJohn Forte 
1461fcf3ce44SJohn Forte 	if (imaStatus != IMA_STATUS_SUCCESS) {
1462fcf3ce44SJohn Forte 		free(pImaConnProps);
1463fcf3ce44SJohn Forte 		return (imaStatus);
1464fcf3ce44SJohn Forte 	}
1465fcf3ce44SJohn Forte 
1466fcf3ce44SJohn Forte 	/*
1467fcf3ce44SJohn Forte 	 * Walk the list returned to find our connection.
1468fcf3ce44SJohn Forte 	 */
1469fcf3ce44SJohn Forte 	for (i = 0; i < pConnList->cl_out_cnt; i++) {
1470fcf3ce44SJohn Forte 
1471fcf3ce44SJohn Forte 		if (pConnList->cl_list[i].c_oid ==
1472fcf3ce44SJohn Forte 		    (uint32_t)connOid->objectSequenceNumber) {
1473fcf3ce44SJohn Forte 
1474fcf3ce44SJohn Forte 			/* This is our connection. */
1475fcf3ce44SJohn Forte 			imaStatus = getConnProps(&pConnList->cl_list[i],
1476fcf3ce44SJohn Forte 			    &pConnProps);
1477fcf3ce44SJohn Forte 
1478fcf3ce44SJohn Forte 			if (imaStatus != IMA_STATUS_SUCCESS) {
1479fcf3ce44SJohn Forte 				free(pConnList);
1480fcf3ce44SJohn Forte 				free(pImaConnProps);
1481fcf3ce44SJohn Forte 				return (imaStatus);
1482fcf3ce44SJohn Forte 			}
1483fcf3ce44SJohn Forte 			pImaConnProps->connectionID = pConnProps->cp_cid;
1484fcf3ce44SJohn Forte 
1485fcf3ce44SJohn Forte 			/*
1486fcf3ce44SJohn Forte 			 * Local Propeties
1487fcf3ce44SJohn Forte 			 */
1488fcf3ce44SJohn Forte 			if (pConnProps->cp_local.soa4.sin_family == AF_INET) {
1489fcf3ce44SJohn Forte 
1490fcf3ce44SJohn Forte 				pImaConnProps->local.ipAddress.ipv4Address =
1491fcf3ce44SJohn Forte 				    IMA_TRUE;
1492fcf3ce44SJohn Forte 				pImaConnProps->local.portNumber =
1493fcf3ce44SJohn Forte 				    pConnProps->cp_local.soa4.sin_port;
1494fcf3ce44SJohn Forte 				addrIn = &(pConnProps->cp_local.soa4);
1495fcf3ce44SJohn Forte 				bcopy(&pConnProps->cp_local.soa4.sin_addr,
1496fcf3ce44SJohn Forte 				    pImaConnProps->local.ipAddress.ipAddress,
1497fcf3ce44SJohn Forte 				    sizeof (addrIn->sin_addr));
1498fcf3ce44SJohn Forte 
1499fcf3ce44SJohn Forte 			} else {
1500fcf3ce44SJohn Forte 				pImaConnProps->local.ipAddress.ipv4Address =
1501fcf3ce44SJohn Forte 				    IMA_FALSE;
1502fcf3ce44SJohn Forte 				pImaConnProps->local.portNumber =
1503fcf3ce44SJohn Forte 				    pConnProps->cp_local.soa6.sin6_port;
1504fcf3ce44SJohn Forte 				addrIn6 = &(pConnProps->cp_local.soa6);
1505fcf3ce44SJohn Forte 				bcopy(&pConnProps->cp_local.soa6.sin6_addr,
1506fcf3ce44SJohn Forte 				    pImaConnProps->local.ipAddress.ipAddress,
1507fcf3ce44SJohn Forte 				    sizeof (addrIn6->sin6_addr));
1508fcf3ce44SJohn Forte 
1509fcf3ce44SJohn Forte 			}
1510fcf3ce44SJohn Forte 
1511fcf3ce44SJohn Forte 			/*
1512fcf3ce44SJohn Forte 			 * Peer Propeties
1513fcf3ce44SJohn Forte 			 */
1514fcf3ce44SJohn Forte 			if (pConnProps->cp_peer.soa4.sin_family == AF_INET) {
1515fcf3ce44SJohn Forte 
1516fcf3ce44SJohn Forte 				pImaConnProps->peer.ipAddress.ipv4Address =
1517fcf3ce44SJohn Forte 				    IMA_TRUE;
1518fcf3ce44SJohn Forte 				pImaConnProps->peer.portNumber =
1519fcf3ce44SJohn Forte 				    pConnProps->cp_peer.soa4.sin_port;
1520fcf3ce44SJohn Forte 				addrIn = &(pConnProps->cp_local.soa4);
1521fcf3ce44SJohn Forte 				bcopy(&pConnProps->cp_peer.soa4.sin_addr,
1522fcf3ce44SJohn Forte 				    pImaConnProps->peer.ipAddress.ipAddress,
1523fcf3ce44SJohn Forte 				    sizeof (addrIn->sin_addr));
1524fcf3ce44SJohn Forte 
1525fcf3ce44SJohn Forte 			} else {
1526fcf3ce44SJohn Forte 				pImaConnProps->peer.ipAddress.ipv4Address =
1527fcf3ce44SJohn Forte 				    IMA_FALSE;
1528fcf3ce44SJohn Forte 				pImaConnProps->peer.portNumber =
1529fcf3ce44SJohn Forte 				    pConnProps->cp_peer.soa6.sin6_port;
1530fcf3ce44SJohn Forte 
1531fcf3ce44SJohn Forte 				addrIn6 = &pConnProps->cp_local.soa6;
1532fcf3ce44SJohn Forte 				bcopy(&pConnProps->cp_peer.soa6.sin6_addr,
1533fcf3ce44SJohn Forte 				    pImaConnProps->peer.ipAddress.ipAddress,
1534fcf3ce44SJohn Forte 				    sizeof (addrIn6->sin6_addr));
1535fcf3ce44SJohn Forte 			}
1536fcf3ce44SJohn Forte 
1537fcf3ce44SJohn Forte 
1538fcf3ce44SJohn Forte 			pImaConnProps->valuesValid =
1539fcf3ce44SJohn Forte 			    pConnProps->cp_params_valid;
1540fcf3ce44SJohn Forte 			pImaConnProps->defaultTime2Retain =
1541fcf3ce44SJohn Forte 			    pConnProps->cp_params.default_time_to_retain;
1542fcf3ce44SJohn Forte 			pImaConnProps->defaultTime2Wait =
1543fcf3ce44SJohn Forte 			    pConnProps->cp_params.default_time_to_wait;
1544fcf3ce44SJohn Forte 			pImaConnProps->errorRecoveryLevel =
1545fcf3ce44SJohn Forte 			    pConnProps->cp_params.error_recovery_level;
1546fcf3ce44SJohn Forte 			pImaConnProps->firstBurstLength =
1547fcf3ce44SJohn Forte 			    pConnProps->cp_params.first_burst_length;
1548fcf3ce44SJohn Forte 			pImaConnProps->maxBurstLength =
1549fcf3ce44SJohn Forte 			    pConnProps->cp_params.max_burst_length;
1550fcf3ce44SJohn Forte 			pImaConnProps->maxConnections =
1551fcf3ce44SJohn Forte 			    pConnProps->cp_params.max_connections;
1552fcf3ce44SJohn Forte 			pImaConnProps->maxOutstandingR2T =
1553fcf3ce44SJohn Forte 			    pConnProps->cp_params.max_outstanding_r2t;
1554fcf3ce44SJohn Forte 			pImaConnProps->maxRecvDataSegmentLength =
1555fcf3ce44SJohn Forte 			    pConnProps->cp_params.max_recv_data_seg_len;
1556fcf3ce44SJohn Forte 
1557fcf3ce44SJohn Forte 			pImaConnProps->dataPduInOrder =
1558fcf3ce44SJohn Forte 			    pConnProps->cp_params.data_pdu_in_order;
1559fcf3ce44SJohn Forte 			pImaConnProps->dataSequenceInOrder =
1560fcf3ce44SJohn Forte 			    pConnProps->cp_params.data_sequence_in_order;
1561fcf3ce44SJohn Forte 			pImaConnProps->immediateData =
1562fcf3ce44SJohn Forte 			    pConnProps->cp_params.immediate_data;
1563fcf3ce44SJohn Forte 			pImaConnProps->initialR2T =
1564fcf3ce44SJohn Forte 			    pConnProps->cp_params.initial_r2t;
1565fcf3ce44SJohn Forte 
1566fcf3ce44SJohn Forte 			pImaConnProps->headerDigest =
1567fcf3ce44SJohn Forte 			    pConnProps->cp_params.header_digest;
1568fcf3ce44SJohn Forte 			pImaConnProps->dataDigest =
1569fcf3ce44SJohn Forte 			    pConnProps->cp_params.data_digest;
1570fcf3ce44SJohn Forte 
1571fcf3ce44SJohn Forte 			free(pConnProps);
1572fcf3ce44SJohn Forte 			break;
1573fcf3ce44SJohn Forte 		}
1574fcf3ce44SJohn Forte 	}
1575fcf3ce44SJohn Forte 	free(pConnList);
1576fcf3ce44SJohn Forte 	*pProps = pImaConnProps;
1577fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1578fcf3ce44SJohn Forte }
1579fcf3ce44SJohn Forte 
1580fcf3ce44SJohn Forte 
1581fcf3ce44SJohn Forte /*
1582fcf3ce44SJohn Forte  * SUN_IMA_GetConfigSessions -
1583fcf3ce44SJohn Forte  *
1584fcf3ce44SJohn Forte  * Non-IMA defined function.
1585fcf3ce44SJohn Forte  */
1586fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_GetConfigSessions(
1587fcf3ce44SJohn Forte     IMA_OID targetOid,
1588fcf3ce44SJohn Forte     SUN_IMA_CONFIG_SESSIONS **pConfigSessions
1589fcf3ce44SJohn Forte )
1590fcf3ce44SJohn Forte {
1591fcf3ce44SJohn Forte 	int			fd;
1592fcf3ce44SJohn Forte 	int			status;
1593fcf3ce44SJohn Forte 	iscsi_config_sess_t	*ics;
1594fcf3ce44SJohn Forte 	int			size, idx;
1595fcf3ce44SJohn Forte 
1596fcf3ce44SJohn Forte 	/* Allocate and setup initial buffer */
1597fcf3ce44SJohn Forte 	size = sizeof (*ics);
1598fcf3ce44SJohn Forte 	ics = (iscsi_config_sess_t *)calloc(1, size);
1599fcf3ce44SJohn Forte 	if (ics == NULL) {
1600fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
1601fcf3ce44SJohn Forte 	}
1602fcf3ce44SJohn Forte 	ics->ics_ver = ISCSI_INTERFACE_VERSION;
1603fcf3ce44SJohn Forte 	ics->ics_oid = targetOid.objectSequenceNumber;
1604fcf3ce44SJohn Forte 	ics->ics_in  = 1;
1605fcf3ce44SJohn Forte 
1606fcf3ce44SJohn Forte 	/* Open driver devctl for ioctl */
1607fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1608fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1609fcf3ce44SJohn Forte 	}
1610fcf3ce44SJohn Forte 
1611fcf3ce44SJohn Forte 	/* Issue ioctl request */
1612fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_GET_CONFIG_SESSIONS, ics) != 0) {
1613fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1614fcf3ce44SJohn Forte 		    "ISCSI_GET_CONFIG_SESSIONS ioctl failed, errno: %d",
1615fcf3ce44SJohn Forte 		    errno);
1616fcf3ce44SJohn Forte 		(void) close(fd);
1617fcf3ce44SJohn Forte 		free(ics);
1618fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1619fcf3ce44SJohn Forte 	}
1620fcf3ce44SJohn Forte 
1621fcf3ce44SJohn Forte 	/* Check if we need to collect more information */
1622fcf3ce44SJohn Forte 	idx = ics->ics_out;
1623fcf3ce44SJohn Forte 	if (idx > 1) {
1624fcf3ce44SJohn Forte 
1625fcf3ce44SJohn Forte 		/* Free old buffer and reallocate re-sized buffer */
1626fcf3ce44SJohn Forte 		free(ics);
1627fcf3ce44SJohn Forte 		size = ISCSI_SESSION_CONFIG_SIZE(idx);
1628fcf3ce44SJohn Forte 		ics = (iscsi_config_sess_t *)calloc(1, size);
1629fcf3ce44SJohn Forte 		if (ics == NULL) {
1630fcf3ce44SJohn Forte 			return (IMA_ERROR_INSUFFICIENT_MEMORY);
1631fcf3ce44SJohn Forte 		}
1632fcf3ce44SJohn Forte 		ics->ics_ver = ISCSI_INTERFACE_VERSION;
1633fcf3ce44SJohn Forte 		ics->ics_oid = targetOid.objectSequenceNumber;
1634fcf3ce44SJohn Forte 		ics->ics_in = idx;
1635fcf3ce44SJohn Forte 
1636fcf3ce44SJohn Forte 		/* Issue ioctl request */
1637fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_GET_CONFIG_SESSIONS, ics) != 0) {
1638fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
1639fcf3ce44SJohn Forte 			    "ISCSI_GET_CONFIG_SESSIONS ioctl failed, errno: %d",
1640fcf3ce44SJohn Forte 			    errno);
1641fcf3ce44SJohn Forte 			(void) close(fd);
1642fcf3ce44SJohn Forte 			free(ics);
1643fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1644fcf3ce44SJohn Forte 		}
1645fcf3ce44SJohn Forte 	}
1646fcf3ce44SJohn Forte 	(void) close(fd);
1647fcf3ce44SJohn Forte 
1648fcf3ce44SJohn Forte 	/* Allocate output buffer */
1649fcf3ce44SJohn Forte 	size = sizeof (SUN_IMA_CONFIG_SESSIONS) +
1650fcf3ce44SJohn Forte 	    ((ics->ics_out - 1) * sizeof (IMA_ADDRESS_KEY));
1651fcf3ce44SJohn Forte 	*pConfigSessions = (SUN_IMA_CONFIG_SESSIONS *)calloc(1, size);
1652fcf3ce44SJohn Forte 	if ((*pConfigSessions) == NULL) {
1653fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
1654fcf3ce44SJohn Forte 	}
1655fcf3ce44SJohn Forte 
1656fcf3ce44SJohn Forte 	/* Copy output information */
1657fcf3ce44SJohn Forte 	(*pConfigSessions)->bound =
1658fcf3ce44SJohn Forte 	    (ics->ics_bound == B_TRUE ?  IMA_TRUE : IMA_FALSE);
1659fcf3ce44SJohn Forte 	(*pConfigSessions)->in = ics->ics_in;
1660fcf3ce44SJohn Forte 	(*pConfigSessions)->out = ics->ics_out;
1661fcf3ce44SJohn Forte 	for (idx = 0; idx < ics->ics_in; idx++) {
1662fcf3ce44SJohn Forte 		if (ics->ics_bindings[idx].i_insize ==
1663fcf3ce44SJohn Forte 		    sizeof (struct in_addr)) {
1664fcf3ce44SJohn Forte 			(*pConfigSessions)->bindings[idx].ipAddress.
1665fcf3ce44SJohn Forte 			    ipv4Address = IMA_TRUE;
1666fcf3ce44SJohn Forte 			bcopy(&ics->ics_bindings[idx].i_addr.in4,
1667fcf3ce44SJohn Forte 			    (*pConfigSessions)->bindings[idx].ipAddress.
1668fcf3ce44SJohn Forte 			    ipAddress, sizeof (struct in_addr));
1669fcf3ce44SJohn Forte 		} else {
1670fcf3ce44SJohn Forte 			(*pConfigSessions)->bindings[idx].ipAddress.
1671fcf3ce44SJohn Forte 			    ipv4Address = IMA_FALSE;
1672fcf3ce44SJohn Forte 			bcopy(&ics->ics_bindings[idx].i_addr.in6,
1673fcf3ce44SJohn Forte 			    (*pConfigSessions)->bindings[idx].ipAddress.
1674fcf3ce44SJohn Forte 			    ipAddress, sizeof (struct in6_addr));
1675fcf3ce44SJohn Forte 		}
1676fcf3ce44SJohn Forte 	}
1677fcf3ce44SJohn Forte 
1678fcf3ce44SJohn Forte 	free(ics);
1679fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1680fcf3ce44SJohn Forte }
1681fcf3ce44SJohn Forte 
1682fcf3ce44SJohn Forte /*
1683fcf3ce44SJohn Forte  * SUN_IMA_SetConfigSessions -
1684fcf3ce44SJohn Forte  *
1685fcf3ce44SJohn Forte  * Non-IMA defined function.
1686fcf3ce44SJohn Forte  */
1687fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_SetConfigSessions(
1688fcf3ce44SJohn Forte     IMA_OID targetOid,
1689fcf3ce44SJohn Forte     SUN_IMA_CONFIG_SESSIONS *pConfigSessions
1690fcf3ce44SJohn Forte )
1691fcf3ce44SJohn Forte {
1692fcf3ce44SJohn Forte 	int		    fd;
1693fcf3ce44SJohn Forte 	int		    status;
1694fcf3ce44SJohn Forte 	iscsi_config_sess_t *ics;
1695fcf3ce44SJohn Forte 	int		    idx, size;
1696fcf3ce44SJohn Forte 
1697fcf3ce44SJohn Forte 	/* verify allowed range of sessions */
1698fcf3ce44SJohn Forte 	if ((pConfigSessions->in < ISCSI_MIN_CONFIG_SESSIONS) ||
1699fcf3ce44SJohn Forte 	    (pConfigSessions->in > ISCSI_MAX_CONFIG_SESSIONS)) {
1700fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
1701fcf3ce44SJohn Forte 	}
1702fcf3ce44SJohn Forte 
1703fcf3ce44SJohn Forte 	/* allocate record config_sess size */
1704fcf3ce44SJohn Forte 	size = ISCSI_SESSION_CONFIG_SIZE(pConfigSessions->in);
1705fcf3ce44SJohn Forte 	ics = (iscsi_config_sess_t *)malloc(size);
1706fcf3ce44SJohn Forte 
1707fcf3ce44SJohn Forte 	/* setup config_sess information */
1708fcf3ce44SJohn Forte 	(void) memset(ics, 0, sizeof (iscsi_config_sess_t));
1709fcf3ce44SJohn Forte 	ics->ics_ver = ISCSI_INTERFACE_VERSION;
1710fcf3ce44SJohn Forte 	ics->ics_oid = targetOid.objectSequenceNumber;
1711fcf3ce44SJohn Forte 	ics->ics_bound =
1712fcf3ce44SJohn Forte 	    (pConfigSessions->bound == IMA_TRUE ?  B_TRUE : B_FALSE);
1713fcf3ce44SJohn Forte 	ics->ics_in  = pConfigSessions->in;
1714fcf3ce44SJohn Forte 	for (idx = 0; idx < ics->ics_in; idx++) {
1715fcf3ce44SJohn Forte 		if (pConfigSessions->bindings[idx].ipAddress.
1716fcf3ce44SJohn Forte 		    ipv4Address == IMA_TRUE) {
1717fcf3ce44SJohn Forte 			ics->ics_bindings[idx].i_insize =
1718fcf3ce44SJohn Forte 			    sizeof (struct in_addr);
1719fcf3ce44SJohn Forte 			bcopy(pConfigSessions->bindings[idx].
1720fcf3ce44SJohn Forte 			    ipAddress.ipAddress,
1721fcf3ce44SJohn Forte 			    &ics->ics_bindings[idx].i_addr.in4,
1722fcf3ce44SJohn Forte 			    sizeof (struct in_addr));
1723fcf3ce44SJohn Forte 		} else {
1724fcf3ce44SJohn Forte 			ics->ics_bindings[idx].i_insize =
1725fcf3ce44SJohn Forte 			    sizeof (struct in6_addr);
1726fcf3ce44SJohn Forte 			bcopy(pConfigSessions->bindings[idx].
1727fcf3ce44SJohn Forte 			    ipAddress.ipAddress,
1728fcf3ce44SJohn Forte 			    &ics->ics_bindings[idx].i_addr.in6,
1729fcf3ce44SJohn Forte 			    sizeof (struct in6_addr));
1730fcf3ce44SJohn Forte 		}
1731fcf3ce44SJohn Forte 	}
1732fcf3ce44SJohn Forte 
1733fcf3ce44SJohn Forte 	/* open driver */
1734fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1735fcf3ce44SJohn Forte 		free(ics);
1736fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1737fcf3ce44SJohn Forte 	}
1738fcf3ce44SJohn Forte 
1739fcf3ce44SJohn Forte 	/* issue ioctl request */
1740fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_SET_CONFIG_SESSIONS, ics) != 0) {
1741fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1742fcf3ce44SJohn Forte 		    "ISCSI_SET_CONFIG_SESSIONS ioctl failed, errno: %d",
1743fcf3ce44SJohn Forte 		    errno);
1744fcf3ce44SJohn Forte 		(void) close(fd);
1745fcf3ce44SJohn Forte 		free(ics);
1746fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1747fcf3ce44SJohn Forte 	}
1748fcf3ce44SJohn Forte 	(void) close(fd);
1749fcf3ce44SJohn Forte 	free(ics);
1750fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1751fcf3ce44SJohn Forte }
1752fcf3ce44SJohn Forte 
1753fcf3ce44SJohn Forte /* A helper function to obtain iSCSI node parameters. */
1754fcf3ce44SJohn Forte static IMA_STATUS
1755fcf3ce44SJohn Forte getISCSINodeParameter(
1756fcf3ce44SJohn Forte     int paramType,
1757fcf3ce44SJohn Forte     IMA_OID *oid,
1758fcf3ce44SJohn Forte     void *pProps,
1759fcf3ce44SJohn Forte     uint32_t paramIndex
1760fcf3ce44SJohn Forte )
1761fcf3ce44SJohn Forte {
1762fcf3ce44SJohn Forte 	int		    fd;
1763fcf3ce44SJohn Forte 	int 		status;
1764fcf3ce44SJohn Forte 	iscsi_param_get_t   pg;
1765fcf3ce44SJohn Forte 
1766fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1767fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1768fcf3ce44SJohn Forte 	}
1769fcf3ce44SJohn Forte 
1770fcf3ce44SJohn Forte 	(void) memset(&pg, 0, sizeof (iscsi_param_get_t));
1771fcf3ce44SJohn Forte 	pg.g_vers = ISCSI_INTERFACE_VERSION;
1772fcf3ce44SJohn Forte 	pg.g_oid = (uint32_t)oid->objectSequenceNumber;
1773fcf3ce44SJohn Forte 	pg.g_param = paramIndex;
1774fcf3ce44SJohn Forte 	pg.g_param_type = ISCSI_SESS_PARAM;
1775fcf3ce44SJohn Forte 
1776fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_PARAM_GET, &pg) != 0) {
1777fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1778fcf3ce44SJohn Forte 		    "ISCSI_PARAM_GET ioctl failed, errno: %d", errno);
1779fcf3ce44SJohn Forte 		(void) close(fd);
1780fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1781fcf3ce44SJohn Forte 	}
1782fcf3ce44SJohn Forte 
1783fcf3ce44SJohn Forte 	switch (paramType) {
1784fcf3ce44SJohn Forte 		IMA_BOOL_VALUE *bp;
1785fcf3ce44SJohn Forte 		IMA_MIN_MAX_VALUE *mp;
1786fcf3ce44SJohn Forte 
1787fcf3ce44SJohn Forte 		case MIN_MAX_PARAM:
1788fcf3ce44SJohn Forte 			mp = (IMA_MIN_MAX_VALUE *)pProps;
1789fcf3ce44SJohn Forte 
1790fcf3ce44SJohn Forte 			mp->currentValueValid =
1791fcf3ce44SJohn Forte 			    (pg.g_value.v_valid == B_TRUE) ?
1792fcf3ce44SJohn Forte 			    IMA_TRUE : IMA_FALSE;
1793fcf3ce44SJohn Forte 			mp->currentValue = pg.g_value.v_integer.i_current;
1794fcf3ce44SJohn Forte 			mp->defaultValue = pg.g_value.v_integer.i_default;
1795fcf3ce44SJohn Forte 			mp->minimumValue = pg.g_value.v_integer.i_min;
1796fcf3ce44SJohn Forte 			mp->maximumValue = pg.g_value.v_integer.i_max;
1797fcf3ce44SJohn Forte 			mp->incrementValue = pg.g_value.v_integer.i_incr;
1798fcf3ce44SJohn Forte 			break;
1799fcf3ce44SJohn Forte 
1800fcf3ce44SJohn Forte 		case BOOL_PARAM:
1801fcf3ce44SJohn Forte 			bp = (IMA_BOOL_VALUE *)pProps;
1802fcf3ce44SJohn Forte 			bp->currentValueValid =
1803fcf3ce44SJohn Forte 			    (pg.g_value.v_valid == B_TRUE) ?
1804fcf3ce44SJohn Forte 			    IMA_TRUE : IMA_FALSE;
1805fcf3ce44SJohn Forte 			bp->currentValue = pg.g_value.v_bool.b_current;
1806fcf3ce44SJohn Forte 			bp->defaultValue = pg.g_value.v_bool.b_default;
1807fcf3ce44SJohn Forte 			break;
1808fcf3ce44SJohn Forte 
1809fcf3ce44SJohn Forte 		default:
1810fcf3ce44SJohn Forte 			break;
1811fcf3ce44SJohn Forte 	}
1812fcf3ce44SJohn Forte 
1813fcf3ce44SJohn Forte 	/* Issue ISCSI_PARAM_GET ioctl again to obtain connection parameters. */
1814fcf3ce44SJohn Forte 	pg.g_param_type = ISCSI_CONN_PARAM;
1815fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_PARAM_GET, &pg) != 0) {
1816fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1817fcf3ce44SJohn Forte 		    "ISCSI_PARAM_GET ioctl failed, errno: %d", errno);
1818fcf3ce44SJohn Forte 		(void) close(fd);
1819fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1820fcf3ce44SJohn Forte 	}
1821fcf3ce44SJohn Forte 
1822fcf3ce44SJohn Forte 	(void) close(fd);
1823fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1824fcf3ce44SJohn Forte }
1825fcf3ce44SJohn Forte 
1826fcf3ce44SJohn Forte /* A helper function to set iSCSI node parameters. */
1827fcf3ce44SJohn Forte static IMA_STATUS
1828fcf3ce44SJohn Forte setISCSINodeParameter(
1829fcf3ce44SJohn Forte     int paramType,
1830fcf3ce44SJohn Forte     IMA_OID *oid,
1831fcf3ce44SJohn Forte     void *pProp,
1832fcf3ce44SJohn Forte     uint32_t paramIndex
1833fcf3ce44SJohn Forte )
1834fcf3ce44SJohn Forte {
1835fcf3ce44SJohn Forte 	int		    fd;
1836fcf3ce44SJohn Forte 	int			status;
1837fcf3ce44SJohn Forte 	iscsi_param_set_t   ps;
1838fcf3ce44SJohn Forte 
1839fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1840fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1841fcf3ce44SJohn Forte 	}
1842fcf3ce44SJohn Forte 
1843fcf3ce44SJohn Forte 	(void) memset(&ps, 0, sizeof (iscsi_param_set_t));
1844fcf3ce44SJohn Forte 	ps.s_vers = ISCSI_INTERFACE_VERSION;
1845fcf3ce44SJohn Forte 	ps.s_oid = (uint32_t)oid->objectSequenceNumber;
1846fcf3ce44SJohn Forte 	ps.s_param = paramIndex;
1847fcf3ce44SJohn Forte 
1848fcf3ce44SJohn Forte 	switch (paramType) {
1849fcf3ce44SJohn Forte 		IMA_BOOL_VALUE *bp;
1850fcf3ce44SJohn Forte 		IMA_MIN_MAX_VALUE *mp;
1851fcf3ce44SJohn Forte 
1852fcf3ce44SJohn Forte 		case MIN_MAX_PARAM:
1853fcf3ce44SJohn Forte 			mp = (IMA_MIN_MAX_VALUE *)pProp;
1854fcf3ce44SJohn Forte 			ps.s_value.v_integer = mp->currentValue;
1855fcf3ce44SJohn Forte 			break;
1856fcf3ce44SJohn Forte 		case BOOL_PARAM:
1857fcf3ce44SJohn Forte 			bp = (IMA_BOOL_VALUE *)pProp;
1858fcf3ce44SJohn Forte 			ps.s_value.v_bool =
1859fcf3ce44SJohn Forte 			    (bp->currentValue == IMA_TRUE) ?
1860fcf3ce44SJohn Forte 			    B_TRUE : B_FALSE;
1861fcf3ce44SJohn Forte 			break;
1862fcf3ce44SJohn Forte 
1863fcf3ce44SJohn Forte 		default:
1864fcf3ce44SJohn Forte 			break;
1865fcf3ce44SJohn Forte 	}
1866fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_PARAM_SET, &ps)) {
1867fcf3ce44SJohn Forte 		(void) close(fd);
1868fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
1869fcf3ce44SJohn Forte 		    "ISCSI_PARAM_SET ioctl failed, errno: %d", errno);
1870fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1871fcf3ce44SJohn Forte 	}
1872fcf3ce44SJohn Forte 
1873fcf3ce44SJohn Forte 	(void) close(fd);
1874fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
1875fcf3ce44SJohn Forte }
1876fcf3ce44SJohn Forte 
1877fcf3ce44SJohn Forte static int
1878fcf3ce44SJohn Forte prepare_discovery_entry(
1879fcf3ce44SJohn Forte     SUN_IMA_TARGET_ADDRESS discoveryAddress,
1880fcf3ce44SJohn Forte     entry_t *entry
1881fcf3ce44SJohn Forte )
1882fcf3ce44SJohn Forte {
1883fcf3ce44SJohn Forte 	return (prepare_discovery_entry_IMA(discoveryAddress.imaStruct, entry));
1884fcf3ce44SJohn Forte }
1885fcf3ce44SJohn Forte 
1886fcf3ce44SJohn Forte static int
1887fcf3ce44SJohn Forte prepare_discovery_entry_IMA(
1888fcf3ce44SJohn Forte     IMA_TARGET_ADDRESS discoveryAddress,
1889fcf3ce44SJohn Forte     entry_t *entry
1890fcf3ce44SJohn Forte )
1891fcf3ce44SJohn Forte {
1892fcf3ce44SJohn Forte 	(void) memset(entry, 0, sizeof (entry_t));
1893fcf3ce44SJohn Forte 	entry->e_vers = ISCSI_INTERFACE_VERSION;
1894fcf3ce44SJohn Forte 	entry->e_oid = ISCSI_OID_NOTSET;
1895fcf3ce44SJohn Forte 
1896fcf3ce44SJohn Forte 	if (discoveryAddress.hostnameIpAddress.id.ipAddress.
1897fcf3ce44SJohn Forte 	    ipv4Address == IMA_FALSE) {
1898fcf3ce44SJohn Forte 
1899fcf3ce44SJohn Forte 		bcopy(discoveryAddress.hostnameIpAddress.id.ipAddress.
1900fcf3ce44SJohn Forte 		    ipAddress, entry->e_u.u_in6.s6_addr,
1901fcf3ce44SJohn Forte 		    sizeof (entry->e_u.u_in6.s6_addr));
1902fcf3ce44SJohn Forte 
1903fcf3ce44SJohn Forte 		entry->e_insize = sizeof (struct in6_addr);
1904fcf3ce44SJohn Forte 	} else {
1905fcf3ce44SJohn Forte 
1906fcf3ce44SJohn Forte 		bcopy(discoveryAddress.hostnameIpAddress.id.ipAddress.
1907fcf3ce44SJohn Forte 		    ipAddress, &entry->e_u.u_in4.s_addr,
1908fcf3ce44SJohn Forte 		    sizeof (entry->e_u.u_in4.s_addr));
1909fcf3ce44SJohn Forte 
1910fcf3ce44SJohn Forte 		entry->e_insize = sizeof (struct in_addr);
1911fcf3ce44SJohn Forte 	}
1912fcf3ce44SJohn Forte 
1913fcf3ce44SJohn Forte 	entry->e_port = discoveryAddress.portNumber;
1914fcf3ce44SJohn Forte 	entry->e_tpgt = 0;
1915fcf3ce44SJohn Forte 	return (DISC_ADDR_OK);
1916fcf3ce44SJohn Forte }
1917fcf3ce44SJohn Forte 
1918fcf3ce44SJohn Forte static IMA_STATUS configure_discovery_method(
1919fcf3ce44SJohn Forte     IMA_BOOL enable,
1920fcf3ce44SJohn Forte     iSCSIDiscoveryMethod_t method
1921fcf3ce44SJohn Forte )
1922fcf3ce44SJohn Forte {
1923fcf3ce44SJohn Forte 	int	fd, status;
1924fcf3ce44SJohn Forte 
1925fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1926fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1927fcf3ce44SJohn Forte 	}
1928fcf3ce44SJohn Forte 
1929fcf3ce44SJohn Forte 	if (enable == IMA_FALSE) {
1930fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_DISCOVERY_CLEAR, &method)) {
1931fcf3ce44SJohn Forte 			status = errno;
1932fcf3ce44SJohn Forte 			(void) close(fd);
1933fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
1934fcf3ce44SJohn Forte 			    "ISCSI_DISCOVERY_CLEAR ioctl failed, errno: %d",
1935fcf3ce44SJohn Forte 			    status);
1936fcf3ce44SJohn Forte 			if (status == EBUSY) {
1937fcf3ce44SJohn Forte 				return (IMA_ERROR_LU_IN_USE);
1938fcf3ce44SJohn Forte 			} else {
1939fcf3ce44SJohn Forte 				return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1940fcf3ce44SJohn Forte 			}
1941fcf3ce44SJohn Forte 		}
1942fcf3ce44SJohn Forte 
1943fcf3ce44SJohn Forte 		(void) close(fd);
1944fcf3ce44SJohn Forte 		return (IMA_STATUS_SUCCESS);
1945fcf3ce44SJohn Forte 	} else {
1946fcf3ce44SJohn Forte 		/* Set the discovery method */
1947fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_DISCOVERY_SET, &method)) {
1948fcf3ce44SJohn Forte 			status = errno;
1949fcf3ce44SJohn Forte 			(void) close(fd);
1950fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
1951fcf3ce44SJohn Forte 			    "ISCSI_DISCOVERY_SET ioctl failed, errno: %d",
1952fcf3ce44SJohn Forte 			    status);
1953fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1954fcf3ce44SJohn Forte 		}
1955fcf3ce44SJohn Forte 
1956fcf3ce44SJohn Forte 		(void) close(fd);
1957fcf3ce44SJohn Forte 		return (IMA_STATUS_SUCCESS);
1958fcf3ce44SJohn Forte 	}
1959fcf3ce44SJohn Forte }
1960fcf3ce44SJohn Forte 
1961fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
1962fcf3ce44SJohn Forte static IMA_BOOL authMethodMatch(
1963fcf3ce44SJohn Forte     IMA_AUTHMETHOD matchingMethod,
1964fcf3ce44SJohn Forte     IMA_AUTHMETHOD *methodList,
1965fcf3ce44SJohn Forte     IMA_UINT maxEntries
1966fcf3ce44SJohn Forte )
1967fcf3ce44SJohn Forte {
1968fcf3ce44SJohn Forte 	IMA_UINT i;
1969fcf3ce44SJohn Forte 
1970fcf3ce44SJohn Forte 	for (i = 0; i < maxEntries; i++) {
1971fcf3ce44SJohn Forte 		if (methodList[i] == matchingMethod) {
1972fcf3ce44SJohn Forte 			return (IMA_TRUE);
1973fcf3ce44SJohn Forte 		}
1974fcf3ce44SJohn Forte 	}
1975fcf3ce44SJohn Forte 
1976fcf3ce44SJohn Forte 	return (IMA_FALSE);
1977fcf3ce44SJohn Forte }
1978fcf3ce44SJohn Forte 
1979fcf3ce44SJohn Forte static IMA_STATUS get_target_oid_list(
1980fcf3ce44SJohn Forte     uint32_t targetListType,
1981fcf3ce44SJohn Forte     IMA_OID_LIST **ppList)
1982fcf3ce44SJohn Forte {
1983fcf3ce44SJohn Forte 	int		    fd;
1984fcf3ce44SJohn Forte 	int		    i;
1985fcf3ce44SJohn Forte 	int		    target_list_size;
1986fcf3ce44SJohn Forte 	int		    status;
1987fcf3ce44SJohn Forte 	int		    out_cnt;
1988fcf3ce44SJohn Forte 	iscsi_target_list_t *idlp;
1989fcf3ce44SJohn Forte 
1990fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
1991fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
1992fcf3ce44SJohn Forte 	}
1993fcf3ce44SJohn Forte 
1994fcf3ce44SJohn Forte 	idlp = (iscsi_target_list_t *)calloc(1, sizeof (iscsi_target_list_t));
1995fcf3ce44SJohn Forte 	if (idlp == NULL) {
1996fcf3ce44SJohn Forte 		(void) close(fd);
1997fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
1998fcf3ce44SJohn Forte 	}
1999fcf3ce44SJohn Forte 	idlp->tl_vers = ISCSI_INTERFACE_VERSION;
2000fcf3ce44SJohn Forte 	idlp->tl_in_cnt = idlp->tl_out_cnt = 1;
2001fcf3ce44SJohn Forte 	idlp->tl_tgt_list_type = targetListType;
2002fcf3ce44SJohn Forte 
2003fcf3ce44SJohn Forte 	/*
2004fcf3ce44SJohn Forte 	 * Issue ioctl.  Space has been allocted for one entry.
2005fcf3ce44SJohn Forte 	 * If more than one entry should be returned, we will re-issue the
2006fcf3ce44SJohn Forte 	 * entry with the right amount of space allocted
2007fcf3ce44SJohn Forte 	 */
2008fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_TARGET_OID_LIST_GET, idlp) != 0) {
2009fcf3ce44SJohn Forte 		(void) close(fd);
2010fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2011fcf3ce44SJohn Forte 		    "ISCSI_TARGET_OID_LIST_GET ioctl %d failed, errno: %d",
2012fcf3ce44SJohn Forte 		    targetListType, errno);
2013fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2014fcf3ce44SJohn Forte 	}
2015fcf3ce44SJohn Forte 	if (idlp->tl_out_cnt > 1) {
2016fcf3ce44SJohn Forte 		out_cnt = idlp->tl_out_cnt;
2017fcf3ce44SJohn Forte 		free(idlp);
2018fcf3ce44SJohn Forte 
2019fcf3ce44SJohn Forte 		target_list_size = sizeof (iscsi_target_list_t);
2020fcf3ce44SJohn Forte 		target_list_size += (sizeof (uint32_t) * out_cnt - 1);
2021fcf3ce44SJohn Forte 		idlp = (iscsi_target_list_t *)calloc(1, target_list_size);
2022fcf3ce44SJohn Forte 		if (idlp == NULL) {
2023fcf3ce44SJohn Forte 			(void) close(fd);
2024fcf3ce44SJohn Forte 			return (IMA_ERROR_INSUFFICIENT_MEMORY);
2025fcf3ce44SJohn Forte 		}
2026fcf3ce44SJohn Forte 		idlp->tl_vers = ISCSI_INTERFACE_VERSION;
2027fcf3ce44SJohn Forte 		idlp->tl_in_cnt = out_cnt;
2028fcf3ce44SJohn Forte 		idlp->tl_tgt_list_type = targetListType;
2029fcf3ce44SJohn Forte 
2030fcf3ce44SJohn Forte 		/* Issue the same ioctl again to obtain all the OIDs. */
2031fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_TARGET_OID_LIST_GET, idlp) != 0) {
2032fcf3ce44SJohn Forte #define	ERROR_STR "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl failed, errno :%d"
2033fcf3ce44SJohn Forte 			free(idlp);
2034fcf3ce44SJohn Forte 			(void) close(fd);
2035fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
2036fcf3ce44SJohn Forte 			    ERROR_STR, targetListType, errno);
2037fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2038fcf3ce44SJohn Forte #undef ERROR_STR
2039fcf3ce44SJohn Forte 
2040fcf3ce44SJohn Forte 		}
2041fcf3ce44SJohn Forte 	}
2042fcf3ce44SJohn Forte 
2043fcf3ce44SJohn Forte 	*ppList = (IMA_OID_LIST *)calloc(1, sizeof (IMA_OID_LIST) +
2044fcf3ce44SJohn Forte 	    idlp->tl_out_cnt * sizeof (IMA_OID));
2045fcf3ce44SJohn Forte 	(*ppList)->oidCount = idlp->tl_out_cnt;
2046fcf3ce44SJohn Forte 	for (i = 0; i < idlp->tl_out_cnt; i++) {
2047fcf3ce44SJohn Forte 		(*ppList)->oids[i].objectType = IMA_OBJECT_TYPE_TARGET;
2048fcf3ce44SJohn Forte 		(*ppList)->oids[i].ownerId = 1;
2049fcf3ce44SJohn Forte 		(*ppList)->oids[i].objectSequenceNumber = idlp->tl_oid_list[i];
2050fcf3ce44SJohn Forte 	}
2051fcf3ce44SJohn Forte 
2052fcf3ce44SJohn Forte 	free(idlp);
2053fcf3ce44SJohn Forte 	(void) close(fd);
2054fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2055fcf3ce44SJohn Forte }
2056fcf3ce44SJohn Forte 
2057fcf3ce44SJohn Forte static IMA_STATUS get_target_lun_oid_list(
2058fcf3ce44SJohn Forte     IMA_OID * targetOid,
2059fcf3ce44SJohn Forte     iscsi_lun_list_t  **ppLunList)
2060fcf3ce44SJohn Forte {
2061fcf3ce44SJohn Forte 	int			fd;
2062fcf3ce44SJohn Forte 	iscsi_lun_list_t	*illp, *illp_saved;
2063fcf3ce44SJohn Forte 	int			lun_list_size;
2064fcf3ce44SJohn Forte 	int			status;
2065fcf3ce44SJohn Forte 
2066fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2067fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2068fcf3ce44SJohn Forte 	}
2069fcf3ce44SJohn Forte 
2070fcf3ce44SJohn Forte 	illp = (iscsi_lun_list_t *)calloc(1, sizeof (iscsi_lun_list_t));
2071fcf3ce44SJohn Forte 	if (illp == NULL) {
2072fcf3ce44SJohn Forte 		(void) close(fd);
2073fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2074fcf3ce44SJohn Forte 	}
2075fcf3ce44SJohn Forte 	illp->ll_vers = ISCSI_INTERFACE_VERSION;
2076fcf3ce44SJohn Forte 	if (targetOid == NULL) {
2077fcf3ce44SJohn Forte 		/* get lun oid list for all targets */
2078fcf3ce44SJohn Forte 		illp->ll_all_tgts = B_TRUE;
2079fcf3ce44SJohn Forte 	} else {
2080fcf3ce44SJohn Forte 		/* get lun oid list for single target */
2081fcf3ce44SJohn Forte 		illp->ll_all_tgts = B_FALSE;
2082fcf3ce44SJohn Forte 		illp->ll_tgt_oid = (uint32_t)targetOid->objectSequenceNumber;
2083fcf3ce44SJohn Forte 	}
2084fcf3ce44SJohn Forte 	illp->ll_in_cnt = illp->ll_out_cnt = 1;
2085fcf3ce44SJohn Forte 
2086fcf3ce44SJohn Forte 	/*
2087fcf3ce44SJohn Forte 	 * Issue ioctl to retrieve the target luns.  Space has been allocted
2088fcf3ce44SJohn Forte 	 * for one entry.  If more than one entry should be returned, we
2089fcf3ce44SJohn Forte 	 * will re-issue the entry with the right amount of space allocted
2090fcf3ce44SJohn Forte 	 */
2091fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_LUN_OID_LIST_GET, illp) != 0) {
2092fcf3ce44SJohn Forte 		(void) close(fd);
2093fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2094fcf3ce44SJohn Forte 		    "ISCSI_LUN_LIST_GET ioctl failed, errno: %d", errno);
2095fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2096fcf3ce44SJohn Forte 	}
2097fcf3ce44SJohn Forte 
2098fcf3ce44SJohn Forte 	if (illp->ll_out_cnt > 1) {
2099fcf3ce44SJohn Forte 		illp_saved = illp;
2100fcf3ce44SJohn Forte 		lun_list_size = sizeof (iscsi_lun_list_t);
2101fcf3ce44SJohn Forte 		lun_list_size += (sizeof (iscsi_if_lun_t) *
2102fcf3ce44SJohn Forte 		    (illp->ll_out_cnt - 1));
2103fcf3ce44SJohn Forte 		illp = (iscsi_lun_list_t *)calloc(1, lun_list_size);
2104fcf3ce44SJohn Forte 		if (illp == NULL) {
2105fcf3ce44SJohn Forte 			(void) close(fd);
2106fcf3ce44SJohn Forte 			return (IMA_ERROR_INSUFFICIENT_MEMORY);
2107fcf3ce44SJohn Forte 		}
2108fcf3ce44SJohn Forte 		illp->ll_vers = ISCSI_INTERFACE_VERSION;
2109fcf3ce44SJohn Forte 		illp->ll_all_tgts = illp_saved->ll_all_tgts;
2110fcf3ce44SJohn Forte 		illp->ll_tgt_oid = illp_saved->ll_tgt_oid;
2111fcf3ce44SJohn Forte 		illp->ll_in_cnt = illp_saved->ll_out_cnt;
2112fcf3ce44SJohn Forte 
2113fcf3ce44SJohn Forte 		free(illp_saved);
2114fcf3ce44SJohn Forte 
2115fcf3ce44SJohn Forte 		/* Issue the same ioctl again to get all the target LUN list */
2116fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_LUN_OID_LIST_GET, illp) != 0) {
2117fcf3ce44SJohn Forte 			free(illp);
2118fcf3ce44SJohn Forte 			(void) close(fd);
2119fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
2120fcf3ce44SJohn Forte 			    "ISCSI_LUN_LIST_GET ioctl failed, errno: %d",
2121fcf3ce44SJohn Forte 			    errno);
2122fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2123fcf3ce44SJohn Forte 
2124fcf3ce44SJohn Forte 		}
2125fcf3ce44SJohn Forte 	}
2126fcf3ce44SJohn Forte 	*ppLunList = illp;
2127fcf3ce44SJohn Forte 
2128fcf3ce44SJohn Forte 
2129fcf3ce44SJohn Forte 	(void) close(fd);
2130fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2131fcf3ce44SJohn Forte }
2132fcf3ce44SJohn Forte 
2133fcf3ce44SJohn Forte /* A helper function to obtain digest algorithms. */
2134fcf3ce44SJohn Forte static IMA_STATUS
2135fcf3ce44SJohn Forte getDigest(
2136fcf3ce44SJohn Forte     IMA_OID oid,
2137fcf3ce44SJohn Forte     int ioctlCmd,
2138fcf3ce44SJohn Forte     SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm
2139fcf3ce44SJohn Forte )
2140fcf3ce44SJohn Forte {
2141fcf3ce44SJohn Forte 	IMA_MIN_MAX_VALUE pProps;
2142fcf3ce44SJohn Forte 	IMA_STATUS status;
2143fcf3ce44SJohn Forte 
2144fcf3ce44SJohn Forte 	if ((status = getISCSINodeParameter(MIN_MAX_PARAM, &oid, &pProps,
2145fcf3ce44SJohn Forte 	    ioctlCmd)) != IMA_STATUS_SUCCESS) {
2146fcf3ce44SJohn Forte 		return (status);
2147fcf3ce44SJohn Forte 	}
2148fcf3ce44SJohn Forte 
2149fcf3ce44SJohn Forte 	switch (pProps.defaultValue) {
2150fcf3ce44SJohn Forte 		case ISCSI_DIGEST_NONE:
2151fcf3ce44SJohn Forte 			algorithm->defaultAlgorithms[0] = ISCSI_DIGEST_NONE;
2152fcf3ce44SJohn Forte 			algorithm->defaultAlgorithmCount = 1;
2153fcf3ce44SJohn Forte 			break;
2154fcf3ce44SJohn Forte 		case ISCSI_DIGEST_CRC32C:
2155fcf3ce44SJohn Forte 			algorithm->defaultAlgorithms[0] = ISCSI_DIGEST_CRC32C;
2156fcf3ce44SJohn Forte 			algorithm->defaultAlgorithmCount = 1;
2157fcf3ce44SJohn Forte 			break;
2158fcf3ce44SJohn Forte 
2159fcf3ce44SJohn Forte 		case ISCSI_DIGEST_CRC32C_NONE:
2160fcf3ce44SJohn Forte 			algorithm->defaultAlgorithms[0] = ISCSI_DIGEST_CRC32C;
2161fcf3ce44SJohn Forte 			algorithm->defaultAlgorithms[1] = ISCSI_DIGEST_NONE;
2162fcf3ce44SJohn Forte 			algorithm->defaultAlgorithmCount = 2;
2163fcf3ce44SJohn Forte 			break;
2164fcf3ce44SJohn Forte 		case ISCSI_DIGEST_NONE_CRC32C:
2165fcf3ce44SJohn Forte 			algorithm->defaultAlgorithms[0] = ISCSI_DIGEST_NONE;
2166fcf3ce44SJohn Forte 			algorithm->defaultAlgorithms[1] = ISCSI_DIGEST_CRC32C;
2167fcf3ce44SJohn Forte 			algorithm->defaultAlgorithmCount = 2;
2168fcf3ce44SJohn Forte 			break;
2169fcf3ce44SJohn Forte 		default:
2170fcf3ce44SJohn Forte 			/* Error */
2171fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
2172fcf3ce44SJohn Forte 			    "Invalid default digest: %d", pProps.defaultValue);
2173fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2174fcf3ce44SJohn Forte 	}
2175fcf3ce44SJohn Forte 
2176fcf3ce44SJohn Forte 	/* The configured value */
2177fcf3ce44SJohn Forte 	if (pProps.currentValueValid == IMA_TRUE) {
2178fcf3ce44SJohn Forte 		algorithm->currentValid = IMA_TRUE;
2179fcf3ce44SJohn Forte 
2180fcf3ce44SJohn Forte 		switch (pProps.currentValue) {
2181fcf3ce44SJohn Forte 			case ISCSI_DIGEST_NONE:
2182fcf3ce44SJohn Forte 				algorithm->currentAlgorithms[0] =
2183fcf3ce44SJohn Forte 				    ISCSI_DIGEST_NONE;
2184fcf3ce44SJohn Forte 				algorithm->currentAlgorithmCount = 1;
2185fcf3ce44SJohn Forte 				break;
2186fcf3ce44SJohn Forte 			case ISCSI_DIGEST_CRC32C:
2187fcf3ce44SJohn Forte 				algorithm->currentAlgorithms[0] =
2188fcf3ce44SJohn Forte 				    ISCSI_DIGEST_CRC32C;
2189fcf3ce44SJohn Forte 				algorithm->currentAlgorithmCount = 1;
2190fcf3ce44SJohn Forte 				break;
2191fcf3ce44SJohn Forte 
2192fcf3ce44SJohn Forte 			case ISCSI_DIGEST_CRC32C_NONE:
2193fcf3ce44SJohn Forte 				algorithm->currentAlgorithms[0] =
2194fcf3ce44SJohn Forte 				    ISCSI_DIGEST_CRC32C;
2195fcf3ce44SJohn Forte 				algorithm->currentAlgorithms[1] =
2196fcf3ce44SJohn Forte 				    ISCSI_DIGEST_NONE;
2197fcf3ce44SJohn Forte 				algorithm->currentAlgorithmCount = 2;
2198fcf3ce44SJohn Forte 				break;
2199fcf3ce44SJohn Forte 			case ISCSI_DIGEST_NONE_CRC32C:
2200fcf3ce44SJohn Forte 				algorithm->currentAlgorithms[0] =
2201fcf3ce44SJohn Forte 				    ISCSI_DIGEST_NONE;
2202fcf3ce44SJohn Forte 				algorithm->currentAlgorithms[1] =
2203fcf3ce44SJohn Forte 				    ISCSI_DIGEST_CRC32C;
2204fcf3ce44SJohn Forte 				algorithm->currentAlgorithmCount = 2;
2205fcf3ce44SJohn Forte 				break;
2206fcf3ce44SJohn Forte 			default:
2207fcf3ce44SJohn Forte 				/* Error */
2208fcf3ce44SJohn Forte 				syslog(LOG_USER|LOG_DEBUG,
2209fcf3ce44SJohn Forte 				    "Invalid configured digest: %d",
2210fcf3ce44SJohn Forte 				    pProps.defaultValue);
2211fcf3ce44SJohn Forte 				return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2212fcf3ce44SJohn Forte 		}
2213fcf3ce44SJohn Forte 
2214fcf3ce44SJohn Forte 	} else {
2215fcf3ce44SJohn Forte 		algorithm->currentValid = IMA_FALSE;
2216fcf3ce44SJohn Forte 	}
2217fcf3ce44SJohn Forte 
2218fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2219fcf3ce44SJohn Forte }
2220fcf3ce44SJohn Forte 
2221fcf3ce44SJohn Forte /*
2222fcf3ce44SJohn Forte  * getConnOidList -
2223fcf3ce44SJohn Forte  */
2224fcf3ce44SJohn Forte static IMA_STATUS getConnOidList(
2225fcf3ce44SJohn Forte 	IMA_OID			*sessOid,
2226fcf3ce44SJohn Forte 	iscsi_conn_list_t	**ppConnList
2227fcf3ce44SJohn Forte )
2228fcf3ce44SJohn Forte {
2229fcf3ce44SJohn Forte 	iscsi_conn_list_t	*iscsiConnList = NULL;
2230fcf3ce44SJohn Forte 	size_t			allocLen;
2231fcf3ce44SJohn Forte 	int			fd;
2232fcf3ce44SJohn Forte 	int			status;
2233fcf3ce44SJohn Forte 	int			out_cnt;
2234fcf3ce44SJohn Forte 
2235fcf3ce44SJohn Forte 	/* Preset it to NULL to prepare for the case of failure */
2236fcf3ce44SJohn Forte 	*ppConnList = NULL;
2237fcf3ce44SJohn Forte 
2238fcf3ce44SJohn Forte 	/* We try to open the driver now. */
2239fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2240fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2241fcf3ce44SJohn Forte 	}
2242fcf3ce44SJohn Forte 
2243fcf3ce44SJohn Forte 	iscsiConnList = (iscsi_conn_list_t *)calloc(1,
2244fcf3ce44SJohn Forte 	    sizeof (iscsi_conn_list_t));
2245fcf3ce44SJohn Forte 	if (iscsiConnList == NULL) {
2246fcf3ce44SJohn Forte 		(void) close(fd);
2247fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2248fcf3ce44SJohn Forte 	}
2249fcf3ce44SJohn Forte 	iscsiConnList->cl_vers = ISCSI_INTERFACE_VERSION;
2250fcf3ce44SJohn Forte 	iscsiConnList->cl_in_cnt = iscsiConnList->cl_out_cnt = 1;
2251fcf3ce44SJohn Forte 	if (sessOid == NULL) {
2252fcf3ce44SJohn Forte 		iscsiConnList->cl_all_sess = B_TRUE;
2253fcf3ce44SJohn Forte 	} else {
2254fcf3ce44SJohn Forte 		iscsiConnList->cl_all_sess = B_FALSE;
2255fcf3ce44SJohn Forte 		iscsiConnList->cl_sess_oid =
2256fcf3ce44SJohn Forte 		    (uint32_t)sessOid->objectSequenceNumber;
2257fcf3ce44SJohn Forte 	}
2258fcf3ce44SJohn Forte 	/*
2259fcf3ce44SJohn Forte 	 * Issue ioctl to retrieve the connection OIDs.  Space has been
2260fcf3ce44SJohn Forte 	 * allocated for one entry.  If more than one entry should be
2261fcf3ce44SJohn Forte 	 * returned, we will re-issue the entry with the right amount of
2262fcf3ce44SJohn Forte 	 * space allocted
2263fcf3ce44SJohn Forte 	 */
2264fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_CONN_OID_LIST_GET, iscsiConnList) != 0) {
2265fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2266fcf3ce44SJohn Forte 		    "ISCSI_CONN_OID_LIST_GET ioctl failed, errno: %d", errno);
2267fcf3ce44SJohn Forte 		*ppConnList = NULL;
2268fcf3ce44SJohn Forte 		(void) close(fd);
2269fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2270fcf3ce44SJohn Forte 	}
2271fcf3ce44SJohn Forte 	if (iscsiConnList->cl_out_cnt > 1) {
2272fcf3ce44SJohn Forte 		out_cnt = iscsiConnList->cl_out_cnt;
2273fcf3ce44SJohn Forte 		free(iscsiConnList);
2274fcf3ce44SJohn Forte 
2275fcf3ce44SJohn Forte 		allocLen = sizeof (iscsi_conn_list_t);
2276fcf3ce44SJohn Forte 		allocLen += (sizeof (iscsi_if_conn_t) * out_cnt - 1);
2277fcf3ce44SJohn Forte 		iscsiConnList = (iscsi_conn_list_t *)calloc(1, allocLen);
2278fcf3ce44SJohn Forte 		if (iscsiConnList == NULL) {
2279fcf3ce44SJohn Forte 			*ppConnList = NULL;
2280fcf3ce44SJohn Forte 			(void) close(fd);
2281fcf3ce44SJohn Forte 			return (IMA_ERROR_INSUFFICIENT_MEMORY);
2282fcf3ce44SJohn Forte 		}
2283fcf3ce44SJohn Forte 		iscsiConnList->cl_vers = ISCSI_INTERFACE_VERSION;
2284fcf3ce44SJohn Forte 		iscsiConnList->cl_in_cnt = out_cnt;
2285fcf3ce44SJohn Forte 		if (sessOid == NULL) {
2286fcf3ce44SJohn Forte 			iscsiConnList->cl_all_sess = B_TRUE;
2287fcf3ce44SJohn Forte 		} else {
2288fcf3ce44SJohn Forte 			iscsiConnList->cl_all_sess = B_FALSE;
2289fcf3ce44SJohn Forte 			iscsiConnList->cl_sess_oid =
2290fcf3ce44SJohn Forte 			    (uint32_t)sessOid->objectSequenceNumber;
2291fcf3ce44SJohn Forte 		}
2292fcf3ce44SJohn Forte 		/* Issue the same ioctl again to obtain all the OIDs */
2293fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_CONN_OID_LIST_GET, iscsiConnList) != 0) {
2294fcf3ce44SJohn Forte 
2295fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
2296fcf3ce44SJohn Forte 			    "ISCSI_CONN_OID_LIST_GET ioctl failed, errno: %d",
2297fcf3ce44SJohn Forte 			    errno);
2298fcf3ce44SJohn Forte 			*ppConnList = NULL;
2299fcf3ce44SJohn Forte 			free(iscsiConnList);
2300fcf3ce44SJohn Forte 			(void) close(fd);
2301fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2302fcf3ce44SJohn Forte 
2303fcf3ce44SJohn Forte 		}
2304fcf3ce44SJohn Forte 
2305fcf3ce44SJohn Forte 		if (out_cnt < iscsiConnList->cl_out_cnt) {
2306fcf3ce44SJohn Forte 			/*
2307fcf3ce44SJohn Forte 			 * The connection list grew between the first and second
2308fcf3ce44SJohn Forte 			 * ioctls.
2309fcf3ce44SJohn Forte 			 */
2310fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
2311fcf3ce44SJohn Forte 			    "The connection list has grown. There could be "
2312fcf3ce44SJohn Forte 			    "more connections than listed.");
2313fcf3ce44SJohn Forte 		}
2314fcf3ce44SJohn Forte 	}
2315fcf3ce44SJohn Forte 
2316fcf3ce44SJohn Forte 
2317fcf3ce44SJohn Forte 	(void) close(fd);
2318fcf3ce44SJohn Forte 	*ppConnList = iscsiConnList;
2319fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2320fcf3ce44SJohn Forte }
2321fcf3ce44SJohn Forte 
2322fcf3ce44SJohn Forte /*
2323fcf3ce44SJohn Forte  * getConnProps -
2324fcf3ce44SJohn Forte  */
2325fcf3ce44SJohn Forte static IMA_STATUS getConnProps(
2326fcf3ce44SJohn Forte 	iscsi_if_conn_t		*pConn,
2327fcf3ce44SJohn Forte 	iscsi_conn_props_t	**ppConnProps
2328fcf3ce44SJohn Forte )
2329fcf3ce44SJohn Forte {
2330fcf3ce44SJohn Forte 	iscsi_conn_props_t	*iscsiConnProps;
2331fcf3ce44SJohn Forte 	int			fd;
2332fcf3ce44SJohn Forte 	int			status;
2333fcf3ce44SJohn Forte 
2334fcf3ce44SJohn Forte 	/* We try to open the driver. */
2335fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2336fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2337fcf3ce44SJohn Forte 	}
2338fcf3ce44SJohn Forte 
2339fcf3ce44SJohn Forte 	iscsiConnProps = (iscsi_conn_props_t *)calloc(1,
2340fcf3ce44SJohn Forte 	    sizeof (*iscsiConnProps));
2341fcf3ce44SJohn Forte 
2342fcf3ce44SJohn Forte 	if (iscsiConnProps == NULL) {
2343fcf3ce44SJohn Forte 		(void) close(fd);
2344fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2345fcf3ce44SJohn Forte 	}
2346fcf3ce44SJohn Forte 
2347fcf3ce44SJohn Forte 	iscsiConnProps->cp_vers = ISCSI_INTERFACE_VERSION;
2348fcf3ce44SJohn Forte 	iscsiConnProps->cp_oid = pConn->c_oid;
2349fcf3ce44SJohn Forte 	iscsiConnProps->cp_cid = pConn->c_cid;
2350fcf3ce44SJohn Forte 	iscsiConnProps->cp_sess_oid = pConn->c_sess_oid;
2351fcf3ce44SJohn Forte 
2352fcf3ce44SJohn Forte 	/* The IOCTL is submitted. */
2353fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_CONN_PROPS_GET, iscsiConnProps) != 0) {
2354fcf3ce44SJohn Forte 		/* IOCTL failed */
2355fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2356fcf3ce44SJohn Forte 		    "ISCSI_AUTH_CLEAR ioctl failed, errno: %d", errno);
2357fcf3ce44SJohn Forte 		free(iscsiConnProps);
2358fcf3ce44SJohn Forte 		(void) close(fd);
2359fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2360fcf3ce44SJohn Forte 	}
2361fcf3ce44SJohn Forte 	(void) close(fd);
2362fcf3ce44SJohn Forte 	*ppConnProps = iscsiConnProps;
2363fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2364fcf3ce44SJohn Forte }
2365fcf3ce44SJohn Forte 
2366fcf3ce44SJohn Forte /* A helper function to set authentication method. */
2367fcf3ce44SJohn Forte static IMA_STATUS
2368fcf3ce44SJohn Forte setAuthMethods(
2369fcf3ce44SJohn Forte     IMA_OID oid,
2370fcf3ce44SJohn Forte     IMA_UINT *pMethodCount,
2371fcf3ce44SJohn Forte     const IMA_AUTHMETHOD *pMethodList
2372fcf3ce44SJohn Forte )
2373fcf3ce44SJohn Forte {
2374fcf3ce44SJohn Forte 	int fd;
2375fcf3ce44SJohn Forte 	int i;
2376fcf3ce44SJohn Forte 	int status;
2377fcf3ce44SJohn Forte 	iscsi_auth_props_t auth;
2378fcf3ce44SJohn Forte 
2379fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2380fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2381fcf3ce44SJohn Forte 	}
2382fcf3ce44SJohn Forte 	(void) memset(&auth, 0, sizeof (iscsi_auth_props_t));
2383fcf3ce44SJohn Forte 	auth.a_vers = ISCSI_INTERFACE_VERSION;
2384fcf3ce44SJohn Forte 	auth.a_oid = (uint32_t)oid.objectSequenceNumber;
2385fcf3ce44SJohn Forte 
2386fcf3ce44SJohn Forte 	/*
2387fcf3ce44SJohn Forte 	 * Get the current auth fields so they don't need to be reset
2388fcf3ce44SJohn Forte 	 * here.
2389fcf3ce44SJohn Forte 	 */
2390fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_AUTH_GET, &auth) != 0) {
2391fcf3ce44SJohn Forte 	    /* EMPTY */
2392fcf3ce44SJohn Forte 	    /* Initializing auth structure with current settings */
2393fcf3ce44SJohn Forte 	}
2394fcf3ce44SJohn Forte 	auth.a_auth_method = authMethodNone;
2395fcf3ce44SJohn Forte 
2396fcf3ce44SJohn Forte 	for (i = 0; i < *pMethodCount; i++) {
2397fcf3ce44SJohn Forte 		switch (pMethodList[i]) {
2398fcf3ce44SJohn Forte 			case IMA_AUTHMETHOD_CHAP:
2399fcf3ce44SJohn Forte 				auth.a_auth_method |= authMethodCHAP;
2400fcf3ce44SJohn Forte 				break;
2401fcf3ce44SJohn Forte 			default:
2402fcf3ce44SJohn Forte 				break;
2403fcf3ce44SJohn Forte 		}
2404fcf3ce44SJohn Forte 	}
2405fcf3ce44SJohn Forte 
2406fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_AUTH_SET, &auth) != 0) {
2407fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2408fcf3ce44SJohn Forte 		    "ISCSI_AUTH_SET failed, errno: %d", errno);
2409fcf3ce44SJohn Forte 		(void) close(fd);
2410fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2411fcf3ce44SJohn Forte 	}
2412fcf3ce44SJohn Forte 
2413fcf3ce44SJohn Forte 	(void) close(fd);
2414fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2415fcf3ce44SJohn Forte }
2416fcf3ce44SJohn Forte 
2417fcf3ce44SJohn Forte /* A helper function to set authentication method. */
2418fcf3ce44SJohn Forte static IMA_STATUS getAuthMethods(
2419fcf3ce44SJohn Forte     IMA_OID oid,
2420fcf3ce44SJohn Forte     IMA_UINT	*pMethodCount,
2421fcf3ce44SJohn Forte     IMA_AUTHMETHOD *pMethodList
2422fcf3ce44SJohn Forte )
2423fcf3ce44SJohn Forte {
2424fcf3ce44SJohn Forte 	int fd;
2425fcf3ce44SJohn Forte 	int status;
2426fcf3ce44SJohn Forte 	iscsi_auth_props_t auth;
2427fcf3ce44SJohn Forte 
2428fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2429fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2430fcf3ce44SJohn Forte 	}
2431fcf3ce44SJohn Forte 
2432fcf3ce44SJohn Forte 	(void) memset(&auth, 0, sizeof (iscsi_auth_props_t));
2433fcf3ce44SJohn Forte 	auth.a_vers = ISCSI_INTERFACE_VERSION;
2434fcf3ce44SJohn Forte 	auth.a_oid = (uint32_t)oid.objectSequenceNumber;
2435fcf3ce44SJohn Forte 
2436fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_AUTH_GET, &auth) != 0) {
2437fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2438fcf3ce44SJohn Forte 		    "ISCSI_AUTH_GET failed, errno: %d", errno);
2439fcf3ce44SJohn Forte 		(void) close(fd);
2440fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2441fcf3ce44SJohn Forte 	}
2442fcf3ce44SJohn Forte 
2443fcf3ce44SJohn Forte 	if (auth.a_auth_method == authMethodNone) {
2444fcf3ce44SJohn Forte 		pMethodList[0] = IMA_AUTHMETHOD_NONE;
2445fcf3ce44SJohn Forte 		*pMethodCount = 1;
2446fcf3ce44SJohn Forte 	} else {
2447fcf3ce44SJohn Forte 		int i = 0;
2448fcf3ce44SJohn Forte 		if (!((auth.a_auth_method & authMethodCHAP)^authMethodCHAP)) {
2449fcf3ce44SJohn Forte 			pMethodList[i++] = IMA_AUTHMETHOD_CHAP;
2450fcf3ce44SJohn Forte 		}
2451fcf3ce44SJohn Forte 		*pMethodCount = i;
2452fcf3ce44SJohn Forte 	}
2453fcf3ce44SJohn Forte 
2454fcf3ce44SJohn Forte 	(void) close(fd);
2455fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2456fcf3ce44SJohn Forte }
2457fcf3ce44SJohn Forte 
2458fcf3ce44SJohn Forte /* Helper function to open driver */
2459fcf3ce44SJohn Forte int open_driver(
2460fcf3ce44SJohn Forte 	int *fd
2461fcf3ce44SJohn Forte )
2462fcf3ce44SJohn Forte {
2463fcf3ce44SJohn Forte 	int ret = 0;
2464fcf3ce44SJohn Forte 	if ((*fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2465fcf3ce44SJohn Forte 		ret = errno;
2466fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2467fcf3ce44SJohn Forte 		    ISCSI_DRIVER_DEVCTL, ret);
2468fcf3ce44SJohn Forte 	}
2469fcf3ce44SJohn Forte 	return (ret);
2470fcf3ce44SJohn Forte }
2471fcf3ce44SJohn Forte 
2472fcf3ce44SJohn Forte /*
2473fcf3ce44SJohn Forte  * Iscsi driver does not support OID for discovery address. Create
2474fcf3ce44SJohn Forte  * a modified version of IMA_RemoveDiscoveryAddress that takes
2475fcf3ce44SJohn Forte  * discoveryAddress (instead of an OID) as input argument.
2476fcf3ce44SJohn Forte  */
2477fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_RemoveDiscoveryAddress(
2478fcf3ce44SJohn Forte     SUN_IMA_TARGET_ADDRESS discoveryAddress
2479fcf3ce44SJohn Forte )
2480fcf3ce44SJohn Forte {
2481fcf3ce44SJohn Forte 	entry_t	entry;
2482fcf3ce44SJohn Forte 	int	fd;
2483fcf3ce44SJohn Forte 	int status, i, addr_list_size, insize;
2484fcf3ce44SJohn Forte 	iscsi_addr_list_t *idlp, al_info;
2485fcf3ce44SJohn Forte 	iscsi_addr_t *matched_addr = NULL;
2486fcf3ce44SJohn Forte 
2487fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2488fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2489fcf3ce44SJohn Forte 	}
2490fcf3ce44SJohn Forte 
2491fcf3ce44SJohn Forte 	if (prepare_discovery_entry(discoveryAddress, &entry) !=
2492fcf3ce44SJohn Forte 	    DISC_ADDR_OK) {
2493fcf3ce44SJohn Forte 		(void) close(fd);
2494fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
2495fcf3ce44SJohn Forte 	}
2496fcf3ce44SJohn Forte 
2497fcf3ce44SJohn Forte 	(void) memset(&al_info, 0, sizeof (al_info));
2498fcf3ce44SJohn Forte 	al_info.al_vers = ISCSI_INTERFACE_VERSION;
2499fcf3ce44SJohn Forte 	al_info.al_in_cnt = 0;
2500fcf3ce44SJohn Forte 	/*
2501fcf3ce44SJohn Forte 	 * Issue ioctl to get the number of discovery address.
2502fcf3ce44SJohn Forte 	 */
2503fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, &al_info) != 0) {
2504fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2505fcf3ce44SJohn Forte 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl %d failed, errno: %d",
2506fcf3ce44SJohn Forte 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
2507fcf3ce44SJohn Forte 		(void) close(fd);
2508fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2509fcf3ce44SJohn Forte 	}
2510fcf3ce44SJohn Forte 
2511fcf3ce44SJohn Forte 	if (al_info.al_out_cnt == 0) {
2512fcf3ce44SJohn Forte 		(void) close(fd);
2513fcf3ce44SJohn Forte 		return (IMA_ERROR_OBJECT_NOT_FOUND);
2514fcf3ce44SJohn Forte 	}
2515fcf3ce44SJohn Forte 
2516fcf3ce44SJohn Forte 	addr_list_size = sizeof (iscsi_addr_list_t);
2517fcf3ce44SJohn Forte 	if (al_info.al_out_cnt > 1) {
2518fcf3ce44SJohn Forte 		addr_list_size += (sizeof (iscsi_addr_t) *
2519fcf3ce44SJohn Forte 		    (al_info.al_out_cnt - 1));
2520fcf3ce44SJohn Forte 	}
2521fcf3ce44SJohn Forte 
2522fcf3ce44SJohn Forte 	idlp = (iscsi_addr_list_t *)calloc(1, addr_list_size);
2523fcf3ce44SJohn Forte 	if (idlp == NULL) {
2524fcf3ce44SJohn Forte 		(void) close(fd);
2525fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2526fcf3ce44SJohn Forte 	}
2527fcf3ce44SJohn Forte 
2528fcf3ce44SJohn Forte 	idlp->al_vers = ISCSI_INTERFACE_VERSION;
2529fcf3ce44SJohn Forte 	idlp->al_in_cnt = al_info.al_out_cnt;
2530fcf3ce44SJohn Forte 
2531fcf3ce44SJohn Forte 	/* issue the same ioctl to get all the discovery addresses */
2532fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, idlp) != 0) {
2533fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2534fcf3ce44SJohn Forte 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl %d failed, errno: %d",
2535fcf3ce44SJohn Forte 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
2536fcf3ce44SJohn Forte 		free(idlp);
2537fcf3ce44SJohn Forte 		(void) close(fd);
2538fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2539fcf3ce44SJohn Forte 	}
2540fcf3ce44SJohn Forte 
2541fcf3ce44SJohn Forte 	/*
2542fcf3ce44SJohn Forte 	 * find the matched discovery address
2543fcf3ce44SJohn Forte 	 */
2544fcf3ce44SJohn Forte 	for (i = 0; i < idlp->al_out_cnt; i++) {
2545fcf3ce44SJohn Forte 		insize = idlp->al_addrs[i].a_addr.i_insize;
2546fcf3ce44SJohn Forte 		if (insize != entry.e_insize) {
2547fcf3ce44SJohn Forte 			continue;
2548fcf3ce44SJohn Forte 		}
2549fcf3ce44SJohn Forte 		if (insize == sizeof (struct in_addr)) {
2550fcf3ce44SJohn Forte 			if (idlp->al_addrs[i].a_addr.i_addr.in4.s_addr ==
2551fcf3ce44SJohn Forte 			    entry.e_u.u_in4.s_addr) {
2552fcf3ce44SJohn Forte 				matched_addr = &(idlp->al_addrs[i]);
2553fcf3ce44SJohn Forte 				break;
2554fcf3ce44SJohn Forte 			}
2555fcf3ce44SJohn Forte 		}
2556fcf3ce44SJohn Forte 		if (insize == sizeof (struct in6_addr)) {
2557fcf3ce44SJohn Forte 			if (bcmp(entry.e_u.u_in6.s6_addr,
2558fcf3ce44SJohn Forte 			    idlp->al_addrs[i].a_addr.i_addr.in6.s6_addr,
2559fcf3ce44SJohn Forte 			    insize) == 0) {
2560fcf3ce44SJohn Forte 				matched_addr = &(idlp->al_addrs[i]);
2561fcf3ce44SJohn Forte 				break;
2562fcf3ce44SJohn Forte 			}
2563fcf3ce44SJohn Forte 		}
2564fcf3ce44SJohn Forte 	}
2565fcf3ce44SJohn Forte 
2566fcf3ce44SJohn Forte 	free(idlp);
2567fcf3ce44SJohn Forte 
2568fcf3ce44SJohn Forte 	if (matched_addr == NULL) {
2569fcf3ce44SJohn Forte 		(void) close(fd);
2570fcf3ce44SJohn Forte 		return (IMA_ERROR_OBJECT_NOT_FOUND);
2571fcf3ce44SJohn Forte 	}
2572fcf3ce44SJohn Forte 
2573fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_CLEAR, &entry)) {
2574fcf3ce44SJohn Forte 		status = errno;
2575fcf3ce44SJohn Forte 		(void) close(fd);
2576fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2577fcf3ce44SJohn Forte 		    "ISCSI_DISCOVERY_ADDR_CLEAR ioctl failed, errno: %d",
2578fcf3ce44SJohn Forte 		    errno);
2579fcf3ce44SJohn Forte 		if (status == EBUSY) {
2580fcf3ce44SJohn Forte 			return (IMA_ERROR_LU_IN_USE);
2581fcf3ce44SJohn Forte 		} else {
2582fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2583fcf3ce44SJohn Forte 		}
2584fcf3ce44SJohn Forte 	}
2585fcf3ce44SJohn Forte 
2586fcf3ce44SJohn Forte 	(void) close(fd);
2587fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2588fcf3ce44SJohn Forte }
2589fcf3ce44SJohn Forte 
2590fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_SetTargetAuthMethods(
2591fcf3ce44SJohn Forte 		IMA_OID targetOid,
2592fcf3ce44SJohn Forte 		IMA_UINT *methodCount,
2593fcf3ce44SJohn Forte 		const IMA_AUTHMETHOD *pMethodList
2594fcf3ce44SJohn Forte )
2595fcf3ce44SJohn Forte {
2596fcf3ce44SJohn Forte 	return (setAuthMethods(targetOid, methodCount, pMethodList));
2597fcf3ce44SJohn Forte }
2598fcf3ce44SJohn Forte 
2599fcf3ce44SJohn Forte IMA_STATUS getNegotiatedDigest(
2600fcf3ce44SJohn Forte 	int digestType,
2601fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm,
2602fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES *connProps) {
2603fcf3ce44SJohn Forte 
2604fcf3ce44SJohn Forte 	IMA_UINT digest;
2605fcf3ce44SJohn Forte 
2606fcf3ce44SJohn Forte 	if (connProps->valuesValid == IMA_TRUE) {
2607fcf3ce44SJohn Forte 		algorithm->negotiatedValid = IMA_TRUE;
2608fcf3ce44SJohn Forte 
2609fcf3ce44SJohn Forte 		if (digestType == ISCSI_LOGIN_PARAM_HEADER_DIGEST) {
2610fcf3ce44SJohn Forte 			digest = connProps->headerDigest;
2611fcf3ce44SJohn Forte 		} else {
2612fcf3ce44SJohn Forte 			digest = connProps->dataDigest;
2613fcf3ce44SJohn Forte 		}
2614fcf3ce44SJohn Forte 
2615fcf3ce44SJohn Forte 		switch (digest) {
2616fcf3ce44SJohn Forte 			case ISCSI_DIGEST_NONE:
2617fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithms[0] =
2618fcf3ce44SJohn Forte 				    ISCSI_DIGEST_NONE;
2619fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithmCount = 1;
2620fcf3ce44SJohn Forte 				break;
2621fcf3ce44SJohn Forte 			case ISCSI_DIGEST_CRC32C:
2622fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithms[0] =
2623fcf3ce44SJohn Forte 				    ISCSI_DIGEST_CRC32C;
2624fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithmCount = 1;
2625fcf3ce44SJohn Forte 				break;
2626fcf3ce44SJohn Forte 
2627fcf3ce44SJohn Forte 			case ISCSI_DIGEST_CRC32C_NONE:
2628fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithms[0] =
2629fcf3ce44SJohn Forte 				    ISCSI_DIGEST_CRC32C;
2630fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithms[1] =
2631fcf3ce44SJohn Forte 				    ISCSI_DIGEST_NONE;
2632fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithmCount = 2;
2633fcf3ce44SJohn Forte 				break;
2634fcf3ce44SJohn Forte 			case ISCSI_DIGEST_NONE_CRC32C:
2635fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithms[0] =
2636fcf3ce44SJohn Forte 				    ISCSI_DIGEST_NONE;
2637fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithms[1] =
2638fcf3ce44SJohn Forte 				    ISCSI_DIGEST_CRC32C;
2639fcf3ce44SJohn Forte 				algorithm->negotiatedAlgorithmCount = 2;
2640fcf3ce44SJohn Forte 				break;
2641fcf3ce44SJohn Forte 			default:
2642fcf3ce44SJohn Forte 				syslog(LOG_USER|LOG_DEBUG,
2643fcf3ce44SJohn Forte 				    "Invalid negotiated digest: %d",
2644fcf3ce44SJohn Forte 				    digest);
2645fcf3ce44SJohn Forte 				return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2646fcf3ce44SJohn Forte 		}
2647fcf3ce44SJohn Forte 	} else {
2648fcf3ce44SJohn Forte 		algorithm->negotiatedValid = IMA_FALSE;
2649fcf3ce44SJohn Forte 	}
2650fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2651fcf3ce44SJohn Forte }
2652fcf3ce44SJohn Forte 
2653fcf3ce44SJohn Forte /*
2654fcf3ce44SJohn Forte  * Non-IMA defined function.
2655fcf3ce44SJohn Forte  */
2656fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_GetISNSServerAddressPropertiesList(
2657fcf3ce44SJohn Forte     SUN_IMA_DISC_ADDR_PROP_LIST	**ppList
2658fcf3ce44SJohn Forte )
2659fcf3ce44SJohn Forte {
2660fcf3ce44SJohn Forte 	char		    isns_server_addr_str[256];
2661fcf3ce44SJohn Forte 	int		    fd;
2662fcf3ce44SJohn Forte 	int		    i;
2663fcf3ce44SJohn Forte 	int		    isns_server_addr_list_size;
2664fcf3ce44SJohn Forte 	int		    status;
2665fcf3ce44SJohn Forte 	int		    out_cnt;
2666fcf3ce44SJohn Forte 	iscsi_addr_list_t   *ialp;
2667fcf3ce44SJohn Forte 	/* LINTED */
2668fcf3ce44SJohn Forte 	IMA_IP_ADDRESS	    *ipAddr;
2669fcf3ce44SJohn Forte 
2670fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2671fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2672fcf3ce44SJohn Forte 	}
2673fcf3ce44SJohn Forte 
2674fcf3ce44SJohn Forte 	ialp = (iscsi_addr_list_t *)calloc(1, sizeof (iscsi_addr_list_t));
2675fcf3ce44SJohn Forte 	if (ialp == NULL) {
2676fcf3ce44SJohn Forte 		(void) close(fd);
2677fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2678fcf3ce44SJohn Forte 	}
2679fcf3ce44SJohn Forte 	ialp->al_vers = ISCSI_INTERFACE_VERSION;
2680fcf3ce44SJohn Forte 	ialp->al_in_cnt = ialp->al_out_cnt = 1;
2681fcf3ce44SJohn Forte 
2682fcf3ce44SJohn Forte 	/*
2683fcf3ce44SJohn Forte 	 * Issue ioctl to retrieve the isns server addresses.  Space has been
2684fcf3ce44SJohn Forte 	 * allocted for one entry.  If more than one entry should be returned,
2685fcf3ce44SJohn Forte 	 * we will re-issue the entry with the right amount of space allocted
2686fcf3ce44SJohn Forte 	 */
2687fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_ISNS_SERVER_ADDR_LIST_GET, ialp) != 0) {
2688fcf3ce44SJohn Forte 		(void) close(fd);
2689fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2690fcf3ce44SJohn Forte 		    "ISCSI_ISNS_SERVER_ADDR_LIST_GET ioctl failed, errno: %d",
2691fcf3ce44SJohn Forte 		    errno);
2692fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2693fcf3ce44SJohn Forte 	}
2694fcf3ce44SJohn Forte 
2695fcf3ce44SJohn Forte 	isns_server_addr_list_size = sizeof (iscsi_addr_list_t);
2696fcf3ce44SJohn Forte 	if (ialp->al_out_cnt > 1) {
2697fcf3ce44SJohn Forte 		out_cnt = ialp->al_out_cnt;
2698fcf3ce44SJohn Forte 		free(ialp);
2699fcf3ce44SJohn Forte 
2700fcf3ce44SJohn Forte 		isns_server_addr_list_size += (sizeof (iscsi_addr_t) *
2701fcf3ce44SJohn Forte 		    out_cnt - 1);
2702fcf3ce44SJohn Forte 		ialp = (iscsi_addr_list_t *)calloc(1,
2703fcf3ce44SJohn Forte 		    isns_server_addr_list_size);
2704fcf3ce44SJohn Forte 		if (ialp == NULL) {
2705fcf3ce44SJohn Forte 			(void) close(fd);
2706fcf3ce44SJohn Forte 			return (IMA_ERROR_INSUFFICIENT_MEMORY);
2707fcf3ce44SJohn Forte 		}
2708fcf3ce44SJohn Forte 		ialp->al_vers = ISCSI_INTERFACE_VERSION;
2709fcf3ce44SJohn Forte 		ialp->al_in_cnt = out_cnt;
2710fcf3ce44SJohn Forte 
2711fcf3ce44SJohn Forte 		/*
2712fcf3ce44SJohn Forte 		 * Issue ISCSI_ISNS_SERVER_ADDR_LIST_GET ioctl again to obtain
2713fcf3ce44SJohn Forte 		 * the list of all the iSNS server addresses
2714fcf3ce44SJohn Forte 		 */
2715fcf3ce44SJohn Forte 		if (ioctl(fd, ISCSI_ISNS_SERVER_ADDR_LIST_GET, ialp) != 0) {
2716fcf3ce44SJohn Forte 			free(ialp);
2717fcf3ce44SJohn Forte 			(void) close(fd);
2718fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
2719fcf3ce44SJohn Forte 			    "ISCSI_ISNS_SERVER_ADDR_LIST_GET ioctl failed, "
2720fcf3ce44SJohn Forte 			    "errno: %d", errno);
2721fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2722fcf3ce44SJohn Forte 
2723fcf3ce44SJohn Forte 		}
2724fcf3ce44SJohn Forte 	}
2725fcf3ce44SJohn Forte 
2726fcf3ce44SJohn Forte 	*ppList = (SUN_IMA_DISC_ADDR_PROP_LIST *)calloc(1,
2727fcf3ce44SJohn Forte 	    sizeof (SUN_IMA_DISC_ADDR_PROP_LIST) +
2728fcf3ce44SJohn Forte 	    ialp->al_out_cnt * sizeof (IMA_DISCOVERY_ADDRESS_PROPERTIES));
2729fcf3ce44SJohn Forte 	if (*ppList == NULL) {
2730fcf3ce44SJohn Forte 		free(ialp);
2731fcf3ce44SJohn Forte 		(void) close(fd);
2732fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2733fcf3ce44SJohn Forte 	}
2734fcf3ce44SJohn Forte 	(*ppList)->discAddrCount = ialp->al_out_cnt;
2735fcf3ce44SJohn Forte 
2736fcf3ce44SJohn Forte 	for (i = 0; i < ialp->al_out_cnt; i++) {
2737fcf3ce44SJohn Forte 		if (ialp->al_addrs[i].a_addr.i_insize ==
2738fcf3ce44SJohn Forte 		    sizeof (struct in_addr)) {
2739fcf3ce44SJohn Forte 			(*ppList)->props[i].discoveryAddress.hostnameIpAddress.
2740fcf3ce44SJohn Forte 			id.ipAddress.ipv4Address = IMA_TRUE;
2741fcf3ce44SJohn Forte 		} else if (ialp->al_addrs[i].a_addr.i_insize ==
2742fcf3ce44SJohn Forte 		    sizeof (struct in6_addr)) {
2743fcf3ce44SJohn Forte 			(*ppList)->props[i].discoveryAddress.hostnameIpAddress.
2744fcf3ce44SJohn Forte 			    id.ipAddress.ipv4Address = IMA_FALSE;
2745fcf3ce44SJohn Forte 		} else {
2746fcf3ce44SJohn Forte 			(void) strlcpy(isns_server_addr_str, "unknown",
2747fcf3ce44SJohn Forte 			    sizeof (isns_server_addr_str));
2748fcf3ce44SJohn Forte 		}
2749fcf3ce44SJohn Forte 
2750fcf3ce44SJohn Forte 		ipAddr = &(*ppList)->props[i].discoveryAddress.
2751fcf3ce44SJohn Forte 		    hostnameIpAddress.id.ipAddress;
2752fcf3ce44SJohn Forte 		bcopy(&ialp->al_addrs[i].a_addr.i_addr,
2753fcf3ce44SJohn Forte 		    (*ppList)->props[i].discoveryAddress.hostnameIpAddress.id.
2754fcf3ce44SJohn Forte 		    ipAddress.ipAddress,
2755fcf3ce44SJohn Forte 		    sizeof (ipAddr->ipAddress));
2756fcf3ce44SJohn Forte 		(*ppList)->props[i].discoveryAddress.portNumber =
2757fcf3ce44SJohn Forte 		    ialp->al_addrs[i].a_port;
2758fcf3ce44SJohn Forte 	}
2759fcf3ce44SJohn Forte 
2760fcf3ce44SJohn Forte 	free(ialp);
2761fcf3ce44SJohn Forte 	(void) close(fd);
2762fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2763fcf3ce44SJohn Forte }
2764fcf3ce44SJohn Forte 
2765fcf3ce44SJohn Forte /*ARGSUSED*/
2766fcf3ce44SJohn Forte /*
2767fcf3ce44SJohn Forte  * Remove iSNS Server Address
2768fcf3ce44SJohn Forte  */
2769fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_RemoveISNSServerAddress(
2770fcf3ce44SJohn Forte     SUN_IMA_TARGET_ADDRESS isnsServerAddress
2771fcf3ce44SJohn Forte )
2772fcf3ce44SJohn Forte {
2773fcf3ce44SJohn Forte 	entry_t	entry;
2774fcf3ce44SJohn Forte 	int	fd, status;
2775fcf3ce44SJohn Forte 
2776fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2777fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2778fcf3ce44SJohn Forte 	}
2779fcf3ce44SJohn Forte 
2780fcf3ce44SJohn Forte 	if (prepare_discovery_entry(isnsServerAddress, &entry) !=
2781fcf3ce44SJohn Forte 	    DISC_ADDR_OK) {
2782fcf3ce44SJohn Forte 		(void) close(fd);
2783fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
2784fcf3ce44SJohn Forte 	}
2785fcf3ce44SJohn Forte 
2786fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_ISNS_SERVER_ADDR_CLEAR, &entry)) {
2787fcf3ce44SJohn Forte 		status = errno;
2788fcf3ce44SJohn Forte 		(void) close(fd);
2789fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2790fcf3ce44SJohn Forte 		    "ISCSI_ISNS_SERVER_ADDR_CLEAR ioctl failed, errno: %d",
2791fcf3ce44SJohn Forte 		    status);
2792fcf3ce44SJohn Forte 		if (status == EBUSY) {
2793fcf3ce44SJohn Forte 			return (IMA_ERROR_LU_IN_USE);
2794fcf3ce44SJohn Forte 		} else {
2795fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2796fcf3ce44SJohn Forte 		}
2797fcf3ce44SJohn Forte 	}
2798fcf3ce44SJohn Forte 
2799fcf3ce44SJohn Forte 	(void) close(fd);
2800fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2801fcf3ce44SJohn Forte }
2802fcf3ce44SJohn Forte 
2803fcf3ce44SJohn Forte /*ARGSUSED*/
2804fcf3ce44SJohn Forte IMA_API IMA_STATUS SUN_IMA_AddISNSServerAddress(
2805fcf3ce44SJohn Forte 		const SUN_IMA_TARGET_ADDRESS isnsServerAddress
2806fcf3ce44SJohn Forte )
2807fcf3ce44SJohn Forte {
2808fcf3ce44SJohn Forte 	entry_t			    entry;
2809fcf3ce44SJohn Forte 	int			    fd;
2810fcf3ce44SJohn Forte 	int			    status;
2811fcf3ce44SJohn Forte 
2812fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2813fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2814fcf3ce44SJohn Forte 	}
2815fcf3ce44SJohn Forte 
2816fcf3ce44SJohn Forte 	if (prepare_discovery_entry(isnsServerAddress, &entry) !=
2817fcf3ce44SJohn Forte 	    DISC_ADDR_OK) {
2818fcf3ce44SJohn Forte 		(void) close(fd);
2819fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
2820fcf3ce44SJohn Forte 	}
2821fcf3ce44SJohn Forte 
2822fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_ISNS_SERVER_ADDR_SET, &entry)) {
2823fcf3ce44SJohn Forte 		/*
2824fcf3ce44SJohn Forte 		 * Encountered problem setting the discovery address.
2825fcf3ce44SJohn Forte 		 */
2826fcf3ce44SJohn Forte 		(void) close(fd);
2827fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2828fcf3ce44SJohn Forte 		    "ISCSI_ISNS_SERVER_ADDR_SET ioctl failed, errno: %d",
2829fcf3ce44SJohn Forte 		    errno);
2830fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2831fcf3ce44SJohn Forte 	}
2832fcf3ce44SJohn Forte 
2833fcf3ce44SJohn Forte 	(void) close(fd);
2834fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2835fcf3ce44SJohn Forte }
2836fcf3ce44SJohn Forte 
2837fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_RetrieveISNSServerTargets(
2838fcf3ce44SJohn Forte     IMA_TARGET_ADDRESS serverAddress,
2839fcf3ce44SJohn Forte     SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES **ppList
2840fcf3ce44SJohn Forte )
2841fcf3ce44SJohn Forte {
2842fcf3ce44SJohn Forte 	int				    fd;
2843fcf3ce44SJohn Forte 	int				    ctr;
2844fcf3ce44SJohn Forte 	int				    server_pg_list_sz;
2845fcf3ce44SJohn Forte 	int				    status;
2846fcf3ce44SJohn Forte 	isns_server_portal_group_list_t	    *server_pg_list = NULL;
2847fcf3ce44SJohn Forte 	isns_portal_group_list_t	    *pg_list = NULL;
2848fcf3ce44SJohn Forte 	IMA_BOOL			    retry = IMA_TRUE;
2849fcf3ce44SJohn Forte 	entry_t				    entry;
2850fcf3ce44SJohn Forte 
2851fcf3ce44SJohn Forte #define	ISNS_SERVER_DEFAULT_NUM_TARGETS	50
2852fcf3ce44SJohn Forte 
2853fcf3ce44SJohn Forte 	server_pg_list_sz = sizeof (*server_pg_list) +
2854fcf3ce44SJohn Forte 	    ((ISNS_SERVER_DEFAULT_NUM_TARGETS - 1) *
2855fcf3ce44SJohn Forte 	    sizeof (isns_portal_group_t));
2856fcf3ce44SJohn Forte 
2857fcf3ce44SJohn Forte 	server_pg_list = (isns_server_portal_group_list_t *)calloc(1,
2858fcf3ce44SJohn Forte 	    server_pg_list_sz);
2859fcf3ce44SJohn Forte 	if (server_pg_list == NULL) {
2860fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2861fcf3ce44SJohn Forte 	}
2862fcf3ce44SJohn Forte 	server_pg_list->addr_port_list.pg_in_cnt =
2863fcf3ce44SJohn Forte 	    ISNS_SERVER_DEFAULT_NUM_TARGETS;
2864fcf3ce44SJohn Forte 
2865fcf3ce44SJohn Forte 	if ((prepare_discovery_entry_IMA(serverAddress, &entry)
2866fcf3ce44SJohn Forte 	    != DISC_ADDR_OK)) {
2867fcf3ce44SJohn Forte 		free(server_pg_list);
2868fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
2869fcf3ce44SJohn Forte 	}
2870fcf3ce44SJohn Forte 	server_pg_list->addr.a_port = entry.e_port;
2871fcf3ce44SJohn Forte 	server_pg_list->addr.a_addr.i_insize = entry.e_insize;
2872fcf3ce44SJohn Forte 	if (entry.e_insize == sizeof (struct in_addr)) {
2873fcf3ce44SJohn Forte 		server_pg_list->addr.a_addr.i_addr.in4.s_addr =
2874fcf3ce44SJohn Forte 		    (entry.e_u.u_in4.s_addr);
2875fcf3ce44SJohn Forte 	} else if (entry.e_insize == sizeof (struct in6_addr)) {
2876fcf3ce44SJohn Forte 		bcopy(&entry.e_u.u_in6.s6_addr,
2877fcf3ce44SJohn Forte 		    server_pg_list->addr.a_addr.i_addr.in6.s6_addr, 16);
2878fcf3ce44SJohn Forte 	}
2879fcf3ce44SJohn Forte 
2880fcf3ce44SJohn Forte 	if ((status = open_driver(&fd))) {
2881fcf3ce44SJohn Forte 		free(server_pg_list);
2882fcf3ce44SJohn Forte 		return (SUN_IMA_ERROR_SYSTEM_ERROR | status);
2883fcf3ce44SJohn Forte 	}
2884fcf3ce44SJohn Forte 
2885fcf3ce44SJohn Forte retry_isns:
2886fcf3ce44SJohn Forte 	/*
2887fcf3ce44SJohn Forte 	 * Issue ioctl to obtain the ISNS Portal Group List list
2888fcf3ce44SJohn Forte 	 */
2889fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_ISNS_SERVER_GET, server_pg_list) != 0) {
2890fcf3ce44SJohn Forte 		int tmp_errno = errno;
2891fcf3ce44SJohn Forte 		IMA_STATUS return_status;
2892fcf3ce44SJohn Forte 
2893fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
2894fcf3ce44SJohn Forte 		    "ISCSI_ISNS_SERVER_GET ioctl failed, errno: %d", tmp_errno);
2895fcf3ce44SJohn Forte 		if (tmp_errno == EACCES) {
2896fcf3ce44SJohn Forte 			return_status = IMA_ERROR_OBJECT_NOT_FOUND;
2897fcf3ce44SJohn Forte 		} else {
2898fcf3ce44SJohn Forte 			return_status = IMA_ERROR_UNEXPECTED_OS_ERROR;
2899fcf3ce44SJohn Forte 		}
2900fcf3ce44SJohn Forte 		(void) close(fd);
2901fcf3ce44SJohn Forte 		free(server_pg_list);
2902fcf3ce44SJohn Forte 		return (return_status);
2903fcf3ce44SJohn Forte 	}
2904fcf3ce44SJohn Forte 	pg_list = &server_pg_list->addr_port_list;
2905fcf3ce44SJohn Forte 
2906fcf3ce44SJohn Forte 	/* check if all targets received */
2907fcf3ce44SJohn Forte 	if (pg_list->pg_in_cnt < pg_list->pg_out_cnt) {
2908fcf3ce44SJohn Forte 		if (retry == IMA_TRUE) {
2909fcf3ce44SJohn Forte 			server_pg_list_sz = sizeof (*server_pg_list) +
2910fcf3ce44SJohn Forte 			    ((pg_list->pg_out_cnt - 1) *
2911fcf3ce44SJohn Forte 			    sizeof (isns_server_portal_group_list_t));
2912fcf3ce44SJohn Forte 			server_pg_list = (isns_server_portal_group_list_t *)
2913fcf3ce44SJohn Forte 			    realloc(server_pg_list, server_pg_list_sz);
2914fcf3ce44SJohn Forte 			if (server_pg_list == NULL) {
2915fcf3ce44SJohn Forte 				(void) close(fd);
2916fcf3ce44SJohn Forte 				return (IMA_ERROR_INSUFFICIENT_MEMORY);
2917fcf3ce44SJohn Forte 			}
2918fcf3ce44SJohn Forte 			pg_list = &server_pg_list->addr_port_list;
2919fcf3ce44SJohn Forte 			pg_list->pg_in_cnt = pg_list->pg_out_cnt;
2920fcf3ce44SJohn Forte 			retry = IMA_FALSE;
2921fcf3ce44SJohn Forte 			goto retry_isns;
2922fcf3ce44SJohn Forte 		} else {
2923fcf3ce44SJohn Forte 			/*
2924fcf3ce44SJohn Forte 			 * don't retry after 2 attempts.  The target list
2925fcf3ce44SJohn Forte 			 * shouldn't continue growing. Just continue
2926fcf3ce44SJohn Forte 			 * on and display what was found.
2927fcf3ce44SJohn Forte 			 */
2928fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
2929fcf3ce44SJohn Forte 			    "ISCSI_SENDTGTS_GET overflow: "
2930fcf3ce44SJohn Forte 			    "failed to obtain all targets");
2931fcf3ce44SJohn Forte 			pg_list->pg_out_cnt = pg_list->pg_in_cnt;
2932fcf3ce44SJohn Forte 		}
2933fcf3ce44SJohn Forte 	}
2934fcf3ce44SJohn Forte 
2935fcf3ce44SJohn Forte 	(void) close(fd);
2936fcf3ce44SJohn Forte 
2937fcf3ce44SJohn Forte 	/* allocate for caller return buffer */
2938fcf3ce44SJohn Forte 	*ppList = (SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *)calloc(1,
2939fcf3ce44SJohn Forte 	    sizeof (SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES) +
2940fcf3ce44SJohn Forte 	    pg_list->pg_out_cnt * sizeof (SUN_IMA_DISC_ADDRESS_KEY));
2941fcf3ce44SJohn Forte 	if (*ppList == NULL) {
2942fcf3ce44SJohn Forte 		free(server_pg_list);
2943fcf3ce44SJohn Forte 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2944fcf3ce44SJohn Forte 	}
2945fcf3ce44SJohn Forte 
2946fcf3ce44SJohn Forte 	(*ppList)->keyCount = pg_list->pg_out_cnt;
2947fcf3ce44SJohn Forte 
2948fcf3ce44SJohn Forte 	for (ctr = 0; ctr < pg_list->pg_out_cnt; ctr++) {
2949fcf3ce44SJohn Forte 		(void) mbstowcs((*ppList)->keys[ctr].name,
2950fcf3ce44SJohn Forte 		    (char *)pg_list->pg_list[ctr].pg_iscsi_name,
2951fcf3ce44SJohn Forte 		    IMA_NODE_NAME_LEN);
2952fcf3ce44SJohn Forte 
2953fcf3ce44SJohn Forte 		(*ppList)->keys[ctr].tpgt = pg_list->pg_list[ctr].pg_tag;
2954fcf3ce44SJohn Forte 
2955fcf3ce44SJohn Forte 		(*ppList)->keys[ctr].address.portNumber =
2956fcf3ce44SJohn Forte 		    pg_list->pg_list[ctr].pg_port;
2957fcf3ce44SJohn Forte 
2958fcf3ce44SJohn Forte 		if (pg_list->pg_list[ctr].insize == sizeof (struct in_addr)) {
2959fcf3ce44SJohn Forte 			(*ppList)->keys[ctr].address.ipAddress.ipv4Address =
2960fcf3ce44SJohn Forte 			    IMA_TRUE;
2961fcf3ce44SJohn Forte 		} else if (pg_list->pg_list[ctr].insize ==
2962fcf3ce44SJohn Forte 		    sizeof (struct in6_addr)) {
2963fcf3ce44SJohn Forte 			(*ppList)->keys[ctr].address.ipAddress.ipv4Address =
2964fcf3ce44SJohn Forte 			    IMA_FALSE;
2965fcf3ce44SJohn Forte 		} else {
2966fcf3ce44SJohn Forte 			free(pg_list);
2967fcf3ce44SJohn Forte 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2968fcf3ce44SJohn Forte 		}
2969fcf3ce44SJohn Forte 
2970fcf3ce44SJohn Forte 		(void) memcpy(&(*ppList)->keys[ctr].address.ipAddress.ipAddress,
2971fcf3ce44SJohn Forte 		    &(pg_list->pg_list[ctr].pg_ip_addr),
2972fcf3ce44SJohn Forte 		    pg_list->pg_list[ctr].insize);
2973fcf3ce44SJohn Forte 	}
2974fcf3ce44SJohn Forte 	free(server_pg_list);
2975fcf3ce44SJohn Forte 
2976fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
2977fcf3ce44SJohn Forte }
2978fcf3ce44SJohn Forte 
2979fcf3ce44SJohn Forte /* ARGSUSED */
2980fcf3ce44SJohn Forte IMA_STATUS SUN_IMA_GetSessionOidList(
2981fcf3ce44SJohn Forte     IMA_OID initiatorOid,
2982fcf3ce44SJohn Forte     IMA_OID_LIST **ppList
2983fcf3ce44SJohn Forte )
2984fcf3ce44SJohn Forte {
2985fcf3ce44SJohn Forte 	return (get_target_oid_list(ISCSI_TGT_OID_LIST, ppList));
2986fcf3ce44SJohn Forte }
2987fcf3ce44SJohn Forte 
2988fcf3ce44SJohn Forte /*ARGSUSED*/
2989fcf3ce44SJohn Forte IMA_API	IMA_STATUS SUN_IMA_GetTargetAuthParms(
2990fcf3ce44SJohn Forte 	IMA_OID oid,
2991fcf3ce44SJohn Forte 	IMA_AUTHMETHOD method,
2992fcf3ce44SJohn Forte 	IMA_INITIATOR_AUTHPARMS *pParms
2993fcf3ce44SJohn Forte )
2994fcf3ce44SJohn Forte {
2995fcf3ce44SJohn Forte 	int fd;
2996fcf3ce44SJohn Forte 	iscsi_chap_props_t  chap_p;
2997fcf3ce44SJohn Forte 
2998fcf3ce44SJohn Forte 	if (pParms == NULL) {
2999fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
3000fcf3ce44SJohn Forte 	}
3001fcf3ce44SJohn Forte 
3002fcf3ce44SJohn Forte 	if (oid.objectType != IMA_OBJECT_TYPE_TARGET) {
3003fcf3ce44SJohn Forte 		return (IMA_ERROR_INCORRECT_OBJECT_TYPE);
3004fcf3ce44SJohn Forte 	}
3005fcf3ce44SJohn Forte 
3006fcf3ce44SJohn Forte 	if (method != IMA_AUTHMETHOD_CHAP) {
3007fcf3ce44SJohn Forte 		return (IMA_ERROR_INVALID_PARAMETER);
3008fcf3ce44SJohn Forte 	}
3009fcf3ce44SJohn Forte 
3010fcf3ce44SJohn Forte 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
3011fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
3012fcf3ce44SJohn Forte 		    ISCSI_DRIVER_DEVCTL, errno);
3013fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3014fcf3ce44SJohn Forte 	}
3015fcf3ce44SJohn Forte 
3016fcf3ce44SJohn Forte 	(void) memset(&chap_p, 0, sizeof (iscsi_chap_props_t));
3017fcf3ce44SJohn Forte 	chap_p.c_vers = ISCSI_INTERFACE_VERSION;
3018fcf3ce44SJohn Forte 	chap_p.c_oid = (uint32_t)oid.objectSequenceNumber;
3019fcf3ce44SJohn Forte 
3020fcf3ce44SJohn Forte 	if (ioctl(fd, ISCSI_CHAP_GET, &chap_p) != 0) {
3021fcf3ce44SJohn Forte 		syslog(LOG_USER|LOG_DEBUG,
3022fcf3ce44SJohn Forte 
3023fcf3ce44SJohn Forte 		    "ISCSI_CHAP_GET ioctl failed, errno: %d",
3024fcf3ce44SJohn Forte 		    errno);
3025fcf3ce44SJohn Forte 		(void) close(fd);
3026fcf3ce44SJohn Forte 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3027fcf3ce44SJohn Forte 	}
3028fcf3ce44SJohn Forte 
3029fcf3ce44SJohn Forte 	(void) memcpy(pParms->chapParms.name, chap_p.c_user,
3030fcf3ce44SJohn Forte 	    chap_p.c_user_len);
3031fcf3ce44SJohn Forte 
3032fcf3ce44SJohn Forte 	pParms->chapParms.nameLength = chap_p.c_user_len;
3033fcf3ce44SJohn Forte 	(void) memcpy(pParms->chapParms.challengeSecret, chap_p.c_secret,
3034fcf3ce44SJohn Forte 	    chap_p.c_secret_len);
3035fcf3ce44SJohn Forte 
3036fcf3ce44SJohn Forte 	pParms->chapParms.challengeSecretLength = chap_p.c_secret_len;
3037fcf3ce44SJohn Forte 
3038fcf3ce44SJohn Forte 	return (IMA_STATUS_SUCCESS);
3039fcf3ce44SJohn Forte }
30406cefaae1SJack Meng 
30416cefaae1SJack Meng IMA_API IMA_STATUS SUN_IMA_GetBootTargetName(
30426cefaae1SJack Meng     IMA_NODE_NAME tgtName
30436cefaae1SJack Meng )
30446cefaae1SJack Meng {
30456cefaae1SJack Meng 	int fd;
30466cefaae1SJack Meng 	IMA_STATUS rtn;
30476cefaae1SJack Meng 	iscsi_boot_property_t bootProp;
30486cefaae1SJack Meng 
30496cefaae1SJack Meng 	bootProp.tgt_name.n_name[0] = '\0';
30506cefaae1SJack Meng 	bootProp.tgt_chap.c_user[0] = '\0';
30516cefaae1SJack Meng 	tgtName[0] = L'\0';
30526cefaae1SJack Meng 	rtn = IMA_ERROR_UNEXPECTED_OS_ERROR;
30536cefaae1SJack Meng 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
30546cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG, "Unable to open %s (%d)",
30556cefaae1SJack Meng 		    ISCSI_DRIVER_DEVCTL, errno);
30566cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
30576cefaae1SJack Meng 	}
30586cefaae1SJack Meng 
30596cefaae1SJack Meng 	if (ioctl(fd, ISCSI_BOOTPROP_GET, &bootProp) != 0) {
30606cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG,
30616cefaae1SJack Meng 		    "ISCSI_BOOTPROP_GET ioctl failed, errno: %d",
30626cefaae1SJack Meng 		    errno);
30636cefaae1SJack Meng 		(void) close(fd);
30646cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
30656cefaae1SJack Meng 	}
30666cefaae1SJack Meng 
30676cefaae1SJack Meng 	if ((bootProp.tgt_name.n_name[0] != '\0') && (tgtName != NULL)) {
30686cefaae1SJack Meng 		if (mbstowcs(tgtName, (const char *)bootProp.tgt_name.n_name,
30696cefaae1SJack Meng 		    IMA_NODE_NAME_LEN) == (size_t)-1) {
30706cefaae1SJack Meng 			syslog(LOG_USER|LOG_DEBUG,
30716cefaae1SJack Meng 			    "ISCSI Target name covert to WCHAR fail");
30726cefaae1SJack Meng 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
30736cefaae1SJack Meng 		} else {
30746cefaae1SJack Meng 			rtn = IMA_STATUS_SUCCESS;
30756cefaae1SJack Meng 		}
30766cefaae1SJack Meng 	}
30776cefaae1SJack Meng 
30786cefaae1SJack Meng 	return (rtn);
30796cefaae1SJack Meng }
30806cefaae1SJack Meng 
30816cefaae1SJack Meng IMA_API IMA_STATUS SUN_IMA_GetBootTargetAuthParams(
30826cefaae1SJack Meng     IMA_INITIATOR_AUTHPARMS *pTgtCHAP
30836cefaae1SJack Meng )
30846cefaae1SJack Meng {
30856cefaae1SJack Meng 	int fd;
30866cefaae1SJack Meng 	IMA_STATUS rtn;
30876cefaae1SJack Meng 	iscsi_boot_property_t bootProp;
30886cefaae1SJack Meng 
30896cefaae1SJack Meng 	bootProp.tgt_name.n_name[0] = '\0';
30906cefaae1SJack Meng 	bootProp.tgt_chap.c_user[0] = '\0';
30916cefaae1SJack Meng 	bootProp.tgt_chap.c_secret[0] = '\0';
30926cefaae1SJack Meng 	rtn = IMA_ERROR_UNEXPECTED_OS_ERROR;
30936cefaae1SJack Meng 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
30946cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG, "Unable to open %s (%d)",
30956cefaae1SJack Meng 		    ISCSI_DRIVER_DEVCTL, errno);
30966cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
30976cefaae1SJack Meng 	}
30986cefaae1SJack Meng 
30996cefaae1SJack Meng 	if (ioctl(fd, ISCSI_BOOTPROP_GET, &bootProp) != 0) {
31006cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG,
31016cefaae1SJack Meng 		    "ISCSI_BOOTPROP_GET ioctl failed, errno: %d",
31026cefaae1SJack Meng 		    errno);
31036cefaae1SJack Meng 		(void) close(fd);
31046cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
31056cefaae1SJack Meng 	}
31066cefaae1SJack Meng 
31076cefaae1SJack Meng 	if (pTgtCHAP != NULL) {
31086cefaae1SJack Meng 		if (bootProp.tgt_chap.c_user[0] != '\0') {
31096cefaae1SJack Meng 			(void) memcpy(pTgtCHAP->chapParms.name,
31106cefaae1SJack Meng 			    bootProp.tgt_chap.c_user, ISCSI_MAX_NAME_LEN);
31116cefaae1SJack Meng 		} else {
31126cefaae1SJack Meng 			pTgtCHAP->chapParms.name[0] = '\0';
31136cefaae1SJack Meng 		}
31146cefaae1SJack Meng 		if (bootProp.tgt_chap.c_secret[0] != '\0') {
31156cefaae1SJack Meng 			(void) memcpy(pTgtCHAP->chapParms.challengeSecret,
31166cefaae1SJack Meng 			    bootProp.tgt_chap.c_secret, MAX_CHAP_SECRET_LEN);
31176cefaae1SJack Meng 		} else {
31186cefaae1SJack Meng 			pTgtCHAP->chapParms.challengeSecret[0] = '\0';
31196cefaae1SJack Meng 		}
31206cefaae1SJack Meng 		rtn = IMA_STATUS_SUCCESS;
31216cefaae1SJack Meng 	}
31226cefaae1SJack Meng 	return (rtn);
31236cefaae1SJack Meng }
31246cefaae1SJack Meng 
31256cefaae1SJack Meng IMA_STATUS SUN_IMA_GetBootMpxio(
31266cefaae1SJack Meng     IMA_BOOL *pMpxioEnabled
31276cefaae1SJack Meng )
31286cefaae1SJack Meng {
31296cefaae1SJack Meng 	int fd;
31306cefaae1SJack Meng 	iscsi_boot_property_t bootProp;
31316cefaae1SJack Meng 
31326cefaae1SJack Meng 	bootProp.hba_mpxio_enabled = B_FALSE;
31336cefaae1SJack Meng 	*pMpxioEnabled = IMA_UNKNOWN;
31346cefaae1SJack Meng 
31356cefaae1SJack Meng 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
31366cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG, "Unable to open %s (%d)",
31376cefaae1SJack Meng 		    ISCSI_DRIVER_DEVCTL, errno);
31386cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
31396cefaae1SJack Meng 	}
31406cefaae1SJack Meng 
31416cefaae1SJack Meng 	if (ioctl(fd, ISCSI_BOOTPROP_GET, &bootProp) != 0) {
31426cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG,
31436cefaae1SJack Meng 		    "ISCSI_BOOTPROP_GET ioctl failed, errno: %d",
31446cefaae1SJack Meng 		    errno);
31456cefaae1SJack Meng 		(void) close(fd);
31466cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
31476cefaae1SJack Meng 	}
31486cefaae1SJack Meng 
31496cefaae1SJack Meng 	if (bootProp.hba_mpxio_enabled) {
31506cefaae1SJack Meng 		*pMpxioEnabled = IMA_TRUE;
31516cefaae1SJack Meng 	} else {
31526cefaae1SJack Meng 		*pMpxioEnabled = IMA_FALSE;
31536cefaae1SJack Meng 	}
31546cefaae1SJack Meng 
31556cefaae1SJack Meng 	(void) close(fd);
31566cefaae1SJack Meng 	return (IMA_STATUS_SUCCESS);
31576cefaae1SJack Meng }
31586cefaae1SJack Meng 
31596cefaae1SJack Meng IMA_STATUS SUN_IMA_GetBootIscsi(
31606cefaae1SJack Meng     IMA_BOOL *pIscsiBoot
31616cefaae1SJack Meng )
31626cefaae1SJack Meng {
31636cefaae1SJack Meng 	int fd;
31646cefaae1SJack Meng 	iscsi_boot_property_t bootProp;
31656cefaae1SJack Meng 
31666cefaae1SJack Meng 	bootProp.iscsiboot = 0;
31676cefaae1SJack Meng 	*pIscsiBoot = 0;
31686cefaae1SJack Meng 
31696cefaae1SJack Meng 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
31706cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG, "Unable to open %s (%d)",
31716cefaae1SJack Meng 		    ISCSI_DRIVER_DEVCTL, errno);
31726cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
31736cefaae1SJack Meng 	}
31746cefaae1SJack Meng 
31756cefaae1SJack Meng 	if (ioctl(fd, ISCSI_BOOTPROP_GET, &bootProp) != 0) {
31766cefaae1SJack Meng 		syslog(LOG_USER|LOG_DEBUG,
31776cefaae1SJack Meng 		    "ISCSI_BOOTPROP_GET ioctl failed, errno: %d",
31786cefaae1SJack Meng 		    errno);
31796cefaae1SJack Meng 		(void) close(fd);
31806cefaae1SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
31816cefaae1SJack Meng 	}
31826cefaae1SJack Meng 
31836cefaae1SJack Meng 	*pIscsiBoot = bootProp.iscsiboot;
31846cefaae1SJack Meng 
31856cefaae1SJack Meng 	(void) close(fd);
31866cefaae1SJack Meng 	return (IMA_STATUS_SUCCESS);
31876cefaae1SJack Meng }
3188*4246c8e9SJack Meng 
3189*4246c8e9SJack Meng IMA_STATUS SUN_IMA_GetSvcStatus(
3190*4246c8e9SJack Meng     IMA_BOOL *pSvcEnabled)
3191*4246c8e9SJack Meng {
3192*4246c8e9SJack Meng 	int		fd;
3193*4246c8e9SJack Meng 	uint32_t	status = ISCSI_SERVICE_DISABLED;
3194*4246c8e9SJack Meng 
3195*4246c8e9SJack Meng 	if (pSvcEnabled == NULL)
3196*4246c8e9SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3197*4246c8e9SJack Meng 	*pSvcEnabled = 0;
3198*4246c8e9SJack Meng 
3199*4246c8e9SJack Meng 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
3200*4246c8e9SJack Meng 		syslog(LOG_USER|LOG_DEBUG, "Unable to open %s (%d)",
3201*4246c8e9SJack Meng 		    ISCSI_DRIVER_DEVCTL, errno);
3202*4246c8e9SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3203*4246c8e9SJack Meng 	}
3204*4246c8e9SJack Meng 
3205*4246c8e9SJack Meng 	if (ioctl(fd, ISCSI_SMF_GET, &status) != 0) {
3206*4246c8e9SJack Meng 		syslog(LOG_USER|LOG_DEBUG,
3207*4246c8e9SJack Meng 		    "ISCSI_SVC_GET ioctl failed, errno: %d",
3208*4246c8e9SJack Meng 		    errno);
3209*4246c8e9SJack Meng 		(void) close(fd);
3210*4246c8e9SJack Meng 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3211*4246c8e9SJack Meng 	}
3212*4246c8e9SJack Meng 
3213*4246c8e9SJack Meng 	if (status == ISCSI_SERVICE_ENABLED) {
3214*4246c8e9SJack Meng 		*pSvcEnabled = 1;
3215*4246c8e9SJack Meng 	}
3216*4246c8e9SJack Meng 
3217*4246c8e9SJack Meng 	(void) close(fd);
3218*4246c8e9SJack Meng 	return (IMA_STATUS_SUCCESS);
3219*4246c8e9SJack Meng }
3220