Home | History | Annotate | Line # | Download | only in iscsid
iscsid_driverif.c revision 1.9
      1 /*	$NetBSD: iscsid_driverif.c,v 1.9 2023/11/25 08:06:02 mlelstv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wasabi Systems, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include "iscsid_globals.h"
     32 
     33 #include <sys/socket.h>
     34 #include <netinet/in.h>
     35 #include <netinet/tcp.h>
     36 #include <netdb.h>
     37 
     38 
     39 /* Global node name (Initiator Name and Alias) */
     40 iscsid_set_node_name_req_t node_name;
     41 
     42 /* -------------------------------------------------------------------------- */
     43 
     44 /*
     45  * set_node_name:
     46  *    Handle set_node_name request. Copy names into our own buffers and
     47  *    set the driver's info as well.
     48  *
     49  *    Parameter:
     50  *          par         The request parameter
     51  *
     52  *    Returns: Status.
     53  */
     54 
     55 uint32_t
     56 set_node_name(iscsid_set_node_name_req_t * par)
     57 {
     58 	iscsi_set_node_name_parameters_t snp;
     59 
     60 	(void) memset(&snp, 0x0, sizeof(snp));
     61 	if (!par->InitiatorName[0])
     62 		return ISCSID_STATUS_NO_INITIATOR_NAME;
     63 
     64 	if (strlen((char *)par->InitiatorName) > ISCSI_STRING_LENGTH
     65 		|| strlen((char *)par->InitiatorAlias) > ISCSI_STRING_LENGTH)
     66 		return ISCSID_STATUS_PARAMETER_INVALID;
     67 
     68 	if (!par->InitiatorAlias[0])
     69 		gethostname((char *)node_name.InitiatorAlias, sizeof(node_name.InitiatorAlias));
     70 
     71 	node_name = *par;
     72 
     73 	strlcpy((char *)snp.InitiatorName, (char *)par->InitiatorName,
     74 		sizeof(snp.InitiatorName));
     75 	strlcpy((char *)snp.InitiatorAlias, (char *)par->InitiatorAlias,
     76 		sizeof(snp.InitiatorAlias));
     77 	memcpy(snp.ISID, par->ISID, 6);
     78 
     79 	DEB(10, ("Setting Node Name: %s (%s)",
     80 			 snp.InitiatorName, snp.InitiatorAlias));
     81 	(void)ioctl(driver, ISCSI_SET_NODE_NAME, &snp);
     82 	return snp.status;
     83 }
     84 
     85 
     86 /*
     87  * bind_socket:
     88  *    Bind socket to initiator portal.
     89  *
     90  *    Parameter:
     91  *       sock     The socket
     92  *       addr     The initiator portal address
     93  *
     94  *    Returns:
     95  *       TRUE on success, FALSE on error.
     96  */
     97 
     98 static int
     99 bind_socket(int sock, uint8_t * addr)
    100 {
    101 	struct addrinfo hints, *ai, *ai0;
    102 	int ret = FALSE;
    103 
    104 	DEB(8, ("Binding to <%s>", addr));
    105 
    106 	memset(&hints, 0, sizeof(hints));
    107 	hints.ai_family = AF_UNSPEC;
    108 	hints.ai_socktype = SOCK_STREAM;
    109 	hints.ai_flags = AI_PASSIVE;
    110 	if (getaddrinfo((char *)addr, NULL, &hints, &ai0))
    111 		return ret;
    112 
    113 	for (ai = ai0; ai; ai = ai->ai_next) {
    114 		if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0)
    115 			continue;
    116 
    117 		listen(sock, 5);
    118 		ret = TRUE;
    119 		break;
    120 	}
    121 	freeaddrinfo(ai0);
    122 
    123 	return ret;
    124 }
    125 
    126 
    127 /* -------------------------------------------------------------------------- */
    128 
    129 /*
    130  * find_free_portal:
    131  *    Find the Portal with the least number of connections.
    132  *
    133  *    Parameter:  the portal group
    134  *
    135  *    Returns:    The pointer to the first free portal (or NULL if none found)
    136  */
    137 
    138 static portal_t *
    139 find_free_portal(portal_group_t * group)
    140 {
    141 	portal_t *curr, *m;
    142 	uint32_t n;
    143 
    144 	if ((curr = TAILQ_FIRST(&group->portals)) == NULL)
    145 		return NULL;
    146 
    147 	m = curr;
    148 	n = curr->active_connections;
    149 
    150 	while ((curr = TAILQ_NEXT(curr, group_list)) != NULL)
    151 		if (curr->active_connections < n) {
    152 			m = curr;
    153 			n = curr->active_connections;
    154 		}
    155 
    156 	return m;
    157 }
    158 
    159 
    160 /*
    161  * make_connection:
    162  *    Common routine for login and add_connection. Creates the connection
    163  *    structure, connects the socket, and executes the login.
    164  *
    165  *    Parameter:
    166  *          sess        The associated session. NULL for a send_targets request.
    167  *          req         The request parameters. NULL for send_targets.
    168  *          res         The response buffer. For SendTargets, only the status
    169  *                      is set. For a "real" login, the login response
    170  *                      is filled in.
    171  *          stid        Send target request only, else NULL. Pointer to uint32:
    172  *                         On Input, contains send target ID
    173  *                         On Output, receives session ID
    174  *
    175  *    Returns:    The connection structure on successful login, else NULL.
    176  *
    177  *    NOTE: Session list must be locked on entry.
    178  */
    179 
    180 static connection_t *
    181 make_connection(session_t * sess, iscsid_login_req_t * req,
    182 				iscsid_response_t * res, uint32_t * stid)
    183 {
    184 	connection_t *conn;
    185 	iscsi_login_parameters_t loginp;
    186 	int sock;
    187 	int ret;
    188 	int yes = 1;
    189 	target_t *target;
    190 	portal_t *portal = NULL;
    191 	iscsi_portal_address_t *addr;
    192 	struct addrinfo hints, *ai, *ai0;
    193 	char portnum[6];
    194 	initiator_t *init;
    195 
    196 	DEB(9, ("Make Connection sess=%p, req=%p, res=%p, stid=%p",
    197 			 sess, req, res, stid));
    198 	(void) memset(&loginp, 0x0, sizeof(loginp));
    199 
    200 	/* find the target portal */
    201 	if (stid != NULL) {
    202 		send_target_t *starget;
    203 
    204 		if ((starget = find_send_target_id(*stid)) == NULL) {
    205 			res->status = ISCSID_STATUS_INVALID_TARGET_ID;
    206 			return NULL;
    207 		}
    208 		addr = &starget->addr;
    209 		target = (target_t *)(void *)starget;
    210 	} else {
    211 		if (NO_ID(&req->portal_id)
    212 			|| (portal = find_portal(&req->portal_id)) == NULL) {
    213 			portal_group_t *group;
    214 
    215 			/* if no ID was specified, use target from existing session */
    216 			if (NO_ID(&req->portal_id)) {
    217 				if (!sess->num_connections ||
    218 					((target = find_target_id(TARGET_LIST,
    219 					sess->target.sid.id)) == NULL)) {
    220 					res->status = ISCSID_STATUS_INVALID_PORTAL_ID;
    221 					return NULL;
    222 				}
    223 			}
    224 			/* if a target was given instead, use it */
    225 			else if ((target =
    226 							  find_target(TARGET_LIST, &req->portal_id)) == NULL) {
    227 				res->status = ISCSID_STATUS_INVALID_PORTAL_ID;
    228 				return NULL;
    229 			}
    230 			/* now get from target to portal - if this is the first connection, */
    231 			/* just use the first portal group. */
    232 			if (!sess->num_connections) {
    233 				group = TAILQ_FIRST(&target->group_list);
    234 			}
    235 			/* if it's a second connection, use an available portal in the same */
    236 			/* portal group */
    237 			else {
    238 				conn = (connection_t *)(void *)
    239 				    TAILQ_FIRST(&sess->connections);
    240 
    241 				if (conn == NULL ||
    242 					(portal = find_portal_id(conn->portal.sid.id)) == NULL) {
    243 					res->status = ISCSID_STATUS_INVALID_PORTAL_ID;
    244 					return NULL;
    245 				}
    246 				group = portal->group;
    247 			}
    248 
    249 			if ((portal = find_free_portal(group)) == NULL) {
    250 				res->status = ISCSID_STATUS_INVALID_PORTAL_ID;
    251 				return NULL;
    252 			}
    253 			DEB(1, ("find_free_portal returns pid=%d", portal->entry.sid.id));
    254 		} else
    255 			target = portal->target;
    256 
    257 		addr = &portal->addr;
    258 
    259 		/* symbolic name for connection? check for duplicates */
    260 		if (req->sym_name[0]) {
    261 			void *p;
    262 
    263 			if (sess->num_connections)
    264 				p = find_connection_name(sess, req->sym_name);
    265 			else
    266 				p = find_session_name(req->sym_name);
    267 			if (p) {
    268 				res->status = ISCSID_STATUS_DUPLICATE_NAME;
    269 				return NULL;
    270 			}
    271 		}
    272 	}
    273 
    274 	if (req != NULL && !NO_ID(&req->initiator_id)) {
    275 		if ((init = find_initiator(&req->initiator_id)) == NULL) {
    276 			res->status = ISCSID_STATUS_INVALID_INITIATOR_ID;
    277 			return NULL;
    278 		}
    279 	} else
    280 		init = select_initiator();
    281 
    282 	/* translate target address */
    283 	DEB(8, ("Connecting to <%s>, port %d", addr->address, addr->port));
    284 
    285 	memset(&hints, 0, sizeof(hints));
    286 	hints.ai_family = AF_UNSPEC;
    287 	hints.ai_socktype = SOCK_STREAM;
    288 	snprintf(portnum, sizeof(portnum), "%u", addr->port);
    289 	ret = getaddrinfo((char *)addr->address, portnum, &hints, &ai0);
    290 	switch (ret) {
    291 	case 0:
    292 		break;
    293 	case EAI_NODATA:
    294 		res->status = ISCSID_STATUS_HOST_NOT_FOUND;
    295 		break;
    296 	case EAI_AGAIN:
    297 		res->status = ISCSID_STATUS_HOST_TRY_AGAIN;
    298 		break;
    299 	default:
    300 		res->status = ISCSID_STATUS_HOST_ERROR;
    301 		break;
    302 	}
    303 
    304 	/* alloc the connection structure */
    305 	conn = calloc(1, sizeof(*conn));
    306 	if (conn == NULL) {
    307 		freeaddrinfo(ai0);
    308 		res->status = ISCSID_STATUS_NO_RESOURCES;
    309 		return NULL;
    310 	}
    311 
    312 	res->status = ISCSID_STATUS_HOST_ERROR;
    313 	sock = -1;
    314 	for (ai = ai0; ai; ai = ai->ai_next) {
    315 		/* create and connect the socket */
    316 		sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    317 		if (sock < 0) {
    318 			res->status = ISCSID_STATUS_SOCKET_ERROR;
    319 			break;
    320 		}
    321 
    322 		if (init) {
    323 			if (!bind_socket(sock, init->address)) {
    324 				close(sock);
    325 				res->status = ISCSID_STATUS_INITIATOR_BIND_ERROR;
    326 				break;
    327 			}
    328 		}
    329 
    330 		DEB(8, ("Connecting socket"));
    331 		if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
    332 			close(sock);
    333 			res->status = ISCSID_STATUS_CONNECT_ERROR;
    334 			continue;
    335 		}
    336 
    337 		res->status = ISCSID_STATUS_SUCCESS;
    338 		break;
    339 	}
    340 	freeaddrinfo(ai0);
    341 
    342 	if (sock < 0) {
    343 		free(conn);
    344 		DEB(1, ("Connecting to socket failed (error %d), returning %d",
    345 				errno, res->status));
    346 		return NULL;
    347 	}
    348 
    349 	/* speed up socket processing */
    350 	setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &yes, (socklen_t)sizeof(yes));
    351 	/* setup login parameter structure */
    352 	loginp.socket = sock;
    353 	if (target->TargetName[0]) {
    354 		loginp.is_present.TargetName = 1;
    355 		loginp.TargetName = target->TargetName;
    356 	}
    357 	if (target->options.is_present.MaxConnections) {
    358 		loginp.is_present.MaxConnections = 1;
    359 		loginp.MaxConnections = target->options.MaxConnections;
    360 	}
    361 	if (target->options.is_present.DataDigest) {
    362 		loginp.is_present.DataDigest = 1;
    363 		loginp.DataDigest = target->options.DataDigest;
    364 	}
    365 	if (target->options.is_present.HeaderDigest) {
    366 		loginp.is_present.HeaderDigest = 1;
    367 		loginp.HeaderDigest = target->options.HeaderDigest;
    368 	}
    369 	if (target->options.is_present.DefaultTime2Retain) {
    370 		loginp.is_present.DefaultTime2Retain = 1;
    371 		loginp.DefaultTime2Retain = target->options.DefaultTime2Retain;
    372 	}
    373 	if (target->options.is_present.DefaultTime2Wait) {
    374 		loginp.is_present.DefaultTime2Wait = 1;
    375 		loginp.DefaultTime2Wait = target->options.DefaultTime2Wait;
    376 	}
    377 	if (target->options.is_present.ErrorRecoveryLevel) {
    378 		loginp.is_present.ErrorRecoveryLevel = 1;
    379 		loginp.ErrorRecoveryLevel = target->options.ErrorRecoveryLevel;
    380 	}
    381 	if (target->options.is_present.MaxRecvDataSegmentLength) {
    382 		loginp.is_present.MaxRecvDataSegmentLength = 1;
    383 		loginp.MaxRecvDataSegmentLength =
    384 			target->options.MaxRecvDataSegmentLength;
    385 	}
    386 	if (target->auth.auth_info.auth_number) {
    387 		loginp.is_present.auth_info = 1;
    388 		loginp.auth_info = target->auth.auth_info;
    389 		if (target->auth.password[0]) {
    390 			loginp.is_present.password = 1;
    391 			loginp.password = target->auth.password;
    392 		}
    393 		if (target->auth.target_password[0]) {
    394 			loginp.is_present.target_password = 1;
    395 			loginp.target_password = target->auth.target_password;
    396 		}
    397 		if (target->auth.user_name[0]) {
    398 			loginp.is_present.user_name = 1;
    399 			loginp.user_name = target->auth.user_name;
    400 		}
    401 	}
    402 	loginp.is_present.TargetAlias = 1;
    403 	loginp.TargetAlias = target->TargetAlias;
    404 
    405 	if (portal != NULL) {
    406 		/* override general target options with portal options (if specified) */
    407 		if (portal->options.is_present.DataDigest) {
    408 			loginp.is_present.DataDigest = 1;
    409 			loginp.DataDigest = portal->options.DataDigest;
    410 		}
    411 		if (portal->options.is_present.HeaderDigest) {
    412 			loginp.is_present.HeaderDigest = 1;
    413 			loginp.HeaderDigest = portal->options.HeaderDigest;
    414 		}
    415 		if (portal->options.is_present.MaxRecvDataSegmentLength) {
    416 			loginp.is_present.MaxRecvDataSegmentLength = 1;
    417 			loginp.MaxRecvDataSegmentLength =
    418 				portal->options.MaxRecvDataSegmentLength;
    419 		}
    420 	}
    421 
    422 	if (req != NULL) {
    423 		loginp.session_id = get_id(&list[SESSION_LIST].list, &req->session_id);
    424 		loginp.login_type = req->login_type;
    425 	} else
    426 		loginp.login_type = ISCSI_LOGINTYPE_DISCOVERY;
    427 
    428 	DEB(5, ("Calling Login..."));
    429 
    430 	ret = ioctl(driver, (sess != NULL && sess->num_connections)
    431 				? ISCSI_ADD_CONNECTION : ISCSI_LOGIN, &loginp);
    432 
    433 	res->status = loginp.status;
    434 
    435 	if (ret)
    436 		close(sock);
    437 
    438 	if (ret || loginp.status) {
    439 		free(conn);
    440 		if (!res->status)
    441 			res->status = ISCSID_STATUS_GENERAL_ERROR;
    442 		return NULL;
    443 	}
    444 	/* connection established! link connection into session and return IDs */
    445 
    446 	conn->loginp = loginp;
    447 	conn->entry.sid.id = loginp.connection_id;
    448 	if (req != NULL) {
    449 		strlcpy((char *)conn->entry.sid.name, (char *)req->sym_name,
    450 			sizeof(conn->entry.sid.name));
    451 	}
    452 
    453 	/*
    454 	   Copy important target information
    455 	 */
    456 	conn->target.sid = target->entry.sid;
    457 	strlcpy((char *)conn->target.TargetName, (char *)target->TargetName,
    458 		sizeof(conn->target.TargetName));
    459 	strlcpy((char *)conn->target.TargetAlias, (char *)target->TargetAlias,
    460 		sizeof(conn->target.TargetAlias));
    461 	conn->target.options = target->options;
    462 	conn->target.auth = target->auth;
    463 	conn->portal.addr = *addr;
    464 
    465 	conn->session = sess;
    466 
    467 	if (stid == NULL) {
    468 		iscsid_login_rsp_t *rsp = (iscsid_login_rsp_t *)(void *)
    469 		    res->parameter;
    470 
    471 		sess->entry.sid.id = loginp.session_id;
    472 		TAILQ_INSERT_TAIL(&sess->connections, &conn->entry, link);
    473 		sess->num_connections++;
    474 
    475 		res->parameter_length = sizeof(*rsp);
    476 		rsp->connection_id = conn->entry.sid;
    477 		rsp->session_id = sess->entry.sid;
    478 
    479 		if (init != NULL) {
    480 			conn->initiator_id = init->entry.sid.id;
    481 			init->active_connections++;
    482 		}
    483 	} else
    484 		*stid = loginp.session_id;
    485 
    486 	/*
    487 	   Copy important portal information
    488 	 */
    489 	if (portal != NULL) {
    490 		conn->portal.sid = portal->entry.sid;
    491 		portal->active_connections++;
    492 	}
    493 
    494 	return conn;
    495 }
    496 
    497 
    498 /*
    499  * event_recover_connection:
    500  *    Handle RECOVER_CONNECTION event: Attempt to re-establish connection.
    501  *
    502  *    Parameter:
    503  *          sid         Session ID
    504  *          cid         Connection ID
    505  */
    506 
    507 static void
    508 event_recover_connection(uint32_t sid, uint32_t cid)
    509 {
    510 	int sock, ret;
    511 	int yes = 1;
    512 	session_t *sess;
    513 	connection_t *conn;
    514 	portal_t *portal;
    515 	initiator_t *init;
    516 	iscsi_portal_address_t *addr;
    517 	struct addrinfo hints, *ai, *ai0;
    518 	char portnum[6];
    519 
    520 	DEB(1, ("Event_Recover_Connection sid=%d, cid=%d", sid, cid));
    521 
    522 	LOCK_SESSIONS;
    523 
    524 	sess = find_session_id(sid);
    525 	if (sess == NULL) {
    526 		UNLOCK_SESSIONS;
    527 		return;
    528 	}
    529 
    530 	conn = find_connection_id(sess, cid);
    531 	if (conn == NULL) {
    532 		UNLOCK_SESSIONS;
    533 		return;
    534 	}
    535 
    536 	UNLOCK_SESSIONS;
    537 
    538 	conn->loginp.status = ISCSI_STATUS_CONNECTION_FAILED;
    539 
    540 	/* If we can't find the portal to connect to, abort. */
    541 
    542 	if ((portal = find_portal_id(conn->portal.sid.id)) == NULL)
    543 		return;
    544 
    545 	init = find_initiator_id(conn->initiator_id);
    546 	addr = &portal->addr;
    547 	conn->portal.addr = *addr;
    548 
    549 	/* translate target address */
    550 	DEB(1, ("Event_Recover_Connection Connecting to <%s>, port %d",
    551 			addr->address, addr->port));
    552 
    553 	memset(&hints, 0, sizeof(hints));
    554 	hints.ai_family = AF_UNSPEC;
    555 	hints.ai_socktype = SOCK_STREAM;
    556 	snprintf(portnum, sizeof(portnum), "%u", addr->port);
    557 	ret = getaddrinfo((char *)addr->address, portnum, &hints, &ai0);
    558 	if (ret) {
    559 		DEB(1, ("getaddrinfo failed (%s)", gai_strerror(ret)));
    560 		return;
    561 	}
    562 
    563 	sock = -1;
    564 	for (ai = ai0; ai; ai = ai->ai_next) {
    565 
    566 		/* create and connect the socket */
    567 		sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    568 		if (sock < 0) {
    569 			DEB(1, ("Creating socket failed (error %d)", errno));
    570 			break;
    571 		}
    572 
    573 		DEB(1, ("recover_connection: Socket = %d", sock));
    574 
    575 		if (init) {
    576 			if (!bind_socket(sock, init->address)) {
    577 				DEB(1, ("Binding to interface failed (error %d)", errno));
    578 				close(sock);
    579 				sock = -1;
    580 				continue;
    581 			}
    582 		}
    583 
    584 		if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
    585 			close(sock);
    586 			sock = -1;
    587 			DEB(1, ("Connecting to socket failed (error %d)", errno));
    588 			continue;
    589 		}
    590 
    591 		break;
    592 	}
    593 	freeaddrinfo(ai0);
    594 
    595 	if (sock < 0)
    596 		return;
    597 
    598 	/* speed up socket processing */
    599 	setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &yes, (socklen_t)sizeof(yes));
    600 	conn->loginp.socket = sock;
    601 	conn->loginp.status = 0;
    602 	ret = ioctl(driver, ISCSI_RESTORE_CONNECTION, &conn->loginp);
    603 
    604 	if (ret)
    605 		close(sock);
    606 }
    607 
    608 
    609 /*
    610  * login:
    611  *    Handle LOGIN request: Log into given portal. Create session, then
    612  *    let make_connection do the rest.
    613  *
    614  *    Parameter:
    615  *          req         The request parameters
    616  *          res         The response buffer
    617  */
    618 
    619 void
    620 log_in(iscsid_login_req_t * req, iscsid_response_t * res)
    621 {
    622 	session_t *sess;
    623 	connection_t *conn;
    624 
    625 	sess = calloc(1, sizeof(*sess));
    626 	if (sess == NULL) {
    627 		res->status = ISCSID_STATUS_NO_RESOURCES;
    628 		return;
    629 	}
    630 	TAILQ_INIT(&sess->connections);
    631 	strlcpy((char *)sess->entry.sid.name, (char *)req->sym_name,
    632 			sizeof(sess->entry.sid.name));
    633 
    634 	LOCK_SESSIONS;
    635 	conn = make_connection(sess, req, res, 0);
    636 
    637 	if (conn == NULL)
    638 		free(sess);
    639 	else {
    640 		sess->target = conn->target;
    641 		TAILQ_INSERT_TAIL(&list[SESSION_LIST].list, &sess->entry, link);
    642 		list[SESSION_LIST].num_entries++;
    643 	}
    644 	UNLOCK_SESSIONS;
    645 }
    646 
    647 /*
    648  * add_connection:
    649  *    Handle ADD_CONNECTION request: Log secondary connection into given portal.
    650  *    Find the session, then let make_connection do the rest.
    651  *
    652  *    Parameter:
    653  *          req         The request parameters
    654  *          res         The response buffer
    655  */
    656 
    657 void
    658 add_connection(iscsid_login_req_t * req, iscsid_response_t * res)
    659 {
    660 	session_t *sess;
    661 
    662 	LOCK_SESSIONS;
    663 	sess = find_session(&req->session_id);
    664 	if (sess == NULL) {
    665 		UNLOCK_SESSIONS;
    666 		res->status = ISCSID_STATUS_INVALID_SESSION_ID;
    667 		return;
    668 	}
    669 
    670 	make_connection(sess, req, res, 0);
    671 	UNLOCK_SESSIONS;
    672 }
    673 
    674 
    675 /*
    676  * logout:
    677  *    Handle LOGOUT request: Log out the given session.
    678  *
    679  *    Parameter:
    680  *          req         The request parameters
    681  *
    682  *    Returns: Response status
    683  */
    684 
    685 uint32_t
    686 log_out(iscsid_sym_id_t * req)
    687 {
    688 	iscsi_logout_parameters_t logoutp;
    689 	session_t *sess;
    690 	int ret;
    691 
    692 	(void) memset(&logoutp, 0x0, sizeof(logoutp));
    693 	LOCK_SESSIONS;
    694 	sess = find_session(req);
    695 	if (sess == NULL) {
    696 		UNLOCK_SESSIONS;
    697 		return ISCSID_STATUS_INVALID_SESSION_ID;
    698 	}
    699 
    700 	logoutp.session_id = sess->entry.sid.id;
    701 	UNLOCK_SESSIONS;
    702 
    703 	ret = ioctl(driver, ISCSI_LOGOUT, &logoutp);
    704 	DEB(9, ("Logout returns %d, status = %d", ret, logoutp.status));
    705 
    706 	return logoutp.status;
    707 }
    708 
    709 
    710 /*
    711  * remove_connection:
    712  *    Handle REMOVE_CONNECTION request: Log out the given connection.
    713  *
    714  *    Parameter:
    715  *          req         The request parameters
    716  *
    717  *    Returns: Response status
    718  */
    719 
    720 uint32_t
    721 remove_connection(iscsid_remove_connection_req_t * req)
    722 {
    723 	iscsi_remove_parameters_t removep;
    724 	session_t *sess;
    725 	connection_t *conn;
    726 	int ret;
    727 
    728 	LOCK_SESSIONS;
    729 	sess = find_session(&req->session_id);
    730 	if (sess == NULL) {
    731 		UNLOCK_SESSIONS;
    732 		return ISCSID_STATUS_INVALID_SESSION_ID;
    733 	}
    734 	conn = find_connection(sess, &req->connection_id);
    735 	if (conn == NULL) {
    736 		UNLOCK_SESSIONS;
    737 		return ISCSID_STATUS_INVALID_CONNECTION_ID;
    738 	}
    739 
    740 	removep.session_id = sess->entry.sid.id;
    741 	removep.connection_id = conn->entry.sid.id;
    742 	UNLOCK_SESSIONS;
    743 
    744 	ret = ioctl(driver, ISCSI_REMOVE_CONNECTION, &removep);
    745 	DEB(9, ("Remove Connection returns %d, status=%d", ret, removep.status));
    746 
    747 	return removep.status;
    748 }
    749 
    750 /*
    751  * send_targets:
    752  *    Handle SEND_TARGETS request:
    753  *       First login with type = discovery.
    754  *       Then send the SendTargets iSCSI request to the target, which will
    755  *       return a list of target portals.
    756  *       Then logout.
    757  *
    758  *    Parameter:
    759  *          stid              The send target ID
    760  *          response_buffer   Pointer to pointer to buffer containing response
    761  *                            The response contains the list of the target
    762  *							  portals. The caller frees the buffer after it
    763  *							  is done with it.
    764  *          response_size     Pointer to variable which upon return will hold
    765  *							  the size of the response buffer.
    766  *
    767  *    Returns: Response status
    768  */
    769 
    770 uint32_t
    771 send_targets(uint32_t stid, uint8_t **response_buffer, uint32_t *response_size)
    772 {
    773 	iscsi_send_targets_parameters_t sendt;
    774 	iscsi_logout_parameters_t logoutp;
    775 	int ret;
    776 	connection_t *conn;
    777 	iscsid_response_t res;
    778 	uint32_t rc = ISCSID_STATUS_SUCCESS;
    779 
    780 	(void) memset(&sendt, 0x0, sizeof(sendt));
    781 	(void) memset(&logoutp, 0x0, sizeof(logoutp));
    782 	(void) memset(&res, 0x0, sizeof(res));
    783 	conn = make_connection(NULL, NULL, &res, &stid);
    784 	DEB(9, ("Make connection returns, status = %d", res.status));
    785 
    786 	if (conn == NULL)
    787 		return res.status;
    788 
    789 	sendt.session_id = stid;
    790 	sendt.response_buffer = NULL;
    791 	sendt.response_size = 0;
    792 	sendt.response_used = sendt.response_total = 0;
    793 	strlcpy((char *)sendt.key, "All", sizeof(sendt.key));
    794 
    795 	/*Call once to get the size of the buffer necessary */
    796 	ret = ioctl(driver, ISCSI_SEND_TARGETS, &sendt);
    797 
    798 	if (!ret && !sendt.status) {
    799 		/* Allocate buffer required and call again to retrieve data */
    800 		/* We allocate one extra byte so we can place a terminating 0 */
    801 		/* at the end of the buffer. */
    802 
    803 		sendt.response_size = sendt.response_total;
    804 		sendt.response_buffer = calloc(1, sendt.response_size + 1);
    805 		if (sendt.response_buffer == NULL)
    806 			rc = ISCSID_STATUS_NO_RESOURCES;
    807 		else {
    808 			ret = ioctl(driver, ISCSI_SEND_TARGETS, &sendt);
    809 			((uint8_t *)sendt.response_buffer)[sendt.response_size] = 0;
    810 
    811 			if (ret || sendt.status) {
    812 				free(sendt.response_buffer);
    813 				sendt.response_buffer = NULL;
    814 				sendt.response_used = 0;
    815 				if ((rc = sendt.status) == 0)
    816 					rc = ISCSID_STATUS_GENERAL_ERROR;
    817 			}
    818 		}
    819 	} else if ((rc = sendt.status) == 0)
    820 		rc = ISCSID_STATUS_GENERAL_ERROR;
    821 
    822 	*response_buffer = sendt.response_buffer;
    823 	*response_size = sendt.response_used;
    824 
    825 	logoutp.session_id = stid;
    826 	ret = ioctl(driver, ISCSI_LOGOUT, &logoutp);
    827 	/* ignore logout status */
    828 
    829 	free(conn);
    830 
    831 	return rc;
    832 }
    833 
    834 
    835 
    836 /*
    837  * get_version:
    838  *    Handle GET_VERSION request.
    839  *
    840  *    Returns: Filled get_version_rsp structure.
    841  */
    842 
    843 void
    844 get_version(iscsid_response_t ** prsp, int *prsp_temp)
    845 {
    846 	iscsid_response_t *rsp = *prsp;
    847 	iscsid_get_version_rsp_t *ver;
    848 	iscsi_get_version_parameters_t drv_ver;
    849 
    850 	rsp = make_rsp(sizeof(iscsid_get_version_rsp_t), prsp, prsp_temp);
    851 	if (rsp == NULL)
    852 		return;
    853 	ver = (iscsid_get_version_rsp_t *)(void *)rsp->parameter;
    854 
    855 	ver->interface_version = INTERFACE_VERSION;
    856 	ver->major = VERSION_MAJOR;
    857 	ver->minor = VERSION_MINOR;
    858 	strlcpy ((char *)ver->version_string, VERSION_STRING, sizeof(ver->version_string));
    859 
    860 	ioctl(driver, ISCSI_GET_VERSION, &drv_ver);
    861 	ver->driver_interface_version = drv_ver.interface_version;
    862 	ver->driver_major = drv_ver.major;
    863 	ver->driver_minor = drv_ver.minor;
    864 	strlcpy ((char *)ver->driver_version_string, (char *)drv_ver.version_string,
    865 			 sizeof (ver->driver_version_string));
    866 }
    867 
    868 
    869 /* -------------------------------------------------------------------------- */
    870 
    871 iscsi_register_event_parameters_t event_reg;	/* registered event ID */
    872 
    873 
    874 /*
    875  * register_event_handler:
    876  *    Call driver to register the event handler.
    877  *
    878  *    Returns:
    879  *       TRUE on success.
    880  */
    881 
    882 boolean_t
    883 register_event_handler(void)
    884 {
    885 	ioctl(driver, ISCSI_REGISTER_EVENT, &event_reg);
    886 	return event_reg.event_id != 0;
    887 }
    888 
    889 
    890 /*
    891  * deregister_event_handler:
    892  *    Call driver to deregister the event handler. If the event handler thread
    893  *    is waiting for an event, this will wake it up and cause it to exit.
    894  */
    895 
    896 void
    897 deregister_event_handler(void)
    898 {
    899 	if (event_reg.event_id) {
    900 		ioctl(driver, ISCSI_DEREGISTER_EVENT, &event_reg);
    901 		event_reg.event_id = 0;
    902 	}
    903 }
    904 
    905 
    906 /*
    907  * event_handler:
    908  *    Event handler thread. Wait for the driver to generate an event and
    909  *    process it appropriately. Exits when the driver terminates or the
    910  *    handler is deregistered because the daemon is terminating.
    911  *
    912  *    Parameter:
    913  *          par         Not used.
    914  */
    915 
    916 void *
    917 /*ARGSUSED*/
    918 event_handler(void *par)
    919 {
    920 	void (*termf)(void) = par;
    921 	iscsi_wait_event_parameters_t evtp;
    922 	int rc;
    923 
    924 	DEB(10, ("Event handler starts"));
    925 	(void) memset(&evtp, 0x0, sizeof(evtp));
    926 
    927 	evtp.event_id = event_reg.event_id;
    928 
    929 	do {
    930 		rc = ioctl(driver, ISCSI_WAIT_EVENT, &evtp);
    931 		if (rc != 0) {
    932 			DEB(10, ("event_handler ioctl failed: %s",
    933 				strerror(errno)));
    934 			break;
    935 		}
    936 
    937 		DEB(10, ("Got Event: kind %d, status %d, sid %d, cid %d, reason %d",
    938 				evtp.event_kind, evtp.status, evtp.session_id,
    939 				evtp.connection_id, evtp.reason));
    940 
    941 		if (evtp.status)
    942 			break;
    943 
    944 		switch (evtp.event_kind) {
    945 		case ISCSI_SESSION_TERMINATED:
    946 			event_kill_session(evtp.session_id);
    947 			break;
    948 
    949 		case ISCSI_CONNECTION_TERMINATED:
    950 			event_kill_connection(evtp.session_id, evtp.connection_id);
    951 			break;
    952 
    953 		case ISCSI_RECOVER_CONNECTION:
    954 			event_recover_connection(evtp.session_id, evtp.connection_id);
    955 			break;
    956 		default:
    957 			break;
    958 		}
    959 	} while (evtp.event_kind != ISCSI_DRIVER_TERMINATING);
    960 
    961 	if (termf != NULL)
    962 		(*termf)();
    963 
    964 	DEB(10, ("Event handler exits"));
    965 
    966 	return NULL;
    967 }
    968 
    969 #if 0
    970 /*
    971  * verify_connection:
    972  *    Verify that a specific connection still exists, delete it if not.
    973  *
    974  * Parameter:  The connection pointer.
    975  *
    976  * Returns:    The status returned by the driver.
    977  */
    978 
    979 uint32_t
    980 verify_connection(connection_t * conn)
    981 {
    982 	iscsi_conn_status_parameters_t req;
    983 	session_t *sess = conn->session;
    984 
    985 	req.connection_id = conn->entry.sid.id;
    986 	req.session_id = sess->entry.sid.id;
    987 
    988 	ioctl(driver, ISCSI_CONNECTION_STATUS, &req);
    989 
    990 	if (req.status) {
    991 		TAILQ_REMOVE(&sess->connections, &conn->entry, link);
    992 		sess->num_connections--;
    993 		free(conn);
    994 	}
    995 	DEB(9, ("Verify connection returns status %d", req.status));
    996 	return req.status;
    997 }
    998 
    999 #endif
   1000