iscsid_driverif.c revision 1.1 1 /* $NetBSD: iscsid_driverif.c,v 1.1 2011/10/23 21:11:23 agc 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 int rc;
60
61 (void) memset(&snp, 0x0, sizeof(snp));
62 if (!par->InitiatorName[0])
63 return ISCSID_STATUS_NO_INITIATOR_NAME;
64
65 if (strlen((char *)par->InitiatorName) > ISCSI_STRING_LENGTH
66 || strlen((char *)par->InitiatorAlias) > ISCSI_STRING_LENGTH)
67 return ISCSID_STATUS_PARAMETER_INVALID;
68
69 if (!par->InitiatorAlias[0])
70 gethostname((char *)node_name.InitiatorAlias, sizeof(node_name.InitiatorAlias));
71
72 node_name = *par;
73
74 #ifdef ISCSI_DEBUG /* DEBUG ONLY: Allow op without driver present */
75 if (driver < 0)
76 return ISCSID_STATUS_SUCCESS;
77 #endif
78
79 strlcpy((char *)snp.InitiatorName, (char *)par->InitiatorName,
80 sizeof(snp.InitiatorName));
81 strlcpy((char *)snp.InitiatorAlias, (char *)par->InitiatorAlias,
82 sizeof(snp.InitiatorAlias));
83 memcpy(snp.ISID, par->ISID, 6);
84
85 DEB(10, ("Setting Node Name: %s (%s)\n",
86 snp.InitiatorName, snp.InitiatorAlias));
87 rc = ioctl(driver, ISCSI_SET_NODE_NAME, &snp);
88 return snp.status;
89 }
90
91
92 /*
93 * bind_socket:
94 * Bind socket to initiator portal.
95 *
96 * Parameter:
97 * sock The socket
98 * addr The initiator portal address
99 *
100 * Returns:
101 * TRUE on success, FALSE on error.
102 */
103
104 STATIC int
105 bind_socket(int sock, uint8_t * addr)
106 {
107 struct sockaddr_in serverAddress;
108 struct hostent *host;
109
110 DEB(8, ("Binding to <%s>\n", addr));
111 (void) memset(&serverAddress, 0x0, sizeof(serverAddress));
112 host = gethostbyname((char *)addr);
113 if (host == NULL)
114 return FALSE;
115 if (host->h_length > (int)sizeof(serverAddress.sin_addr))
116 return FALSE;
117 serverAddress.sin_family = host->h_addrtype;
118 serverAddress.sin_port = 0;
119 serverAddress.sin_len = host->h_length;
120 memcpy(&serverAddress.sin_addr, host->h_addr_list[0], host->h_length);
121
122 return bind(sock, (struct sockaddr *) &serverAddress,
123 sizeof(serverAddress)) >= 0;
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 sockaddr_in serverAddress;
193 struct hostent *host;
194 initiator_t *init;
195
196 DEB(9, ("Make Connection sess=%x, req=%x, res=%x, stid=%x\n",
197 (int) sess, (int) req, (int) res, (int) stid));
198 (void) memset(&loginp, 0x0, sizeof(loginp));
199 (void) memset(&serverAddress, 0x0, sizeof(serverAddress));
200
201 /* find the target portal */
202 if (stid != NULL) {
203 send_target_t *starget;
204
205 if ((starget = find_send_target_id(*stid)) == NULL) {
206 res->status = ISCSID_STATUS_INVALID_TARGET_ID;
207 return NULL;
208 }
209 addr = &starget->addr;
210 target = (target_t *) starget;
211 } else {
212 if (NO_ID(&req->portal_id)
213 || (portal = find_portal(&req->portal_id)) == NULL) {
214 portal_group_t *group;
215
216 /* if no ID was specified, use target from existing session */
217 if (NO_ID(&req->portal_id)) {
218 if (!sess->num_connections ||
219 ((target =
220 find_target_id(TARGET_LIST, sess->target.sid.id)) == NULL)) {
221 res->status = ISCSID_STATUS_INVALID_PORTAL_ID;
222 return NULL;
223 }
224 }
225 /* if a target was given instead, use it */
226 else if ((target =
227 find_target(TARGET_LIST, &req->portal_id)) == NULL) {
228 res->status = ISCSID_STATUS_INVALID_PORTAL_ID;
229 return NULL;
230 }
231 /* now get from target to portal - if this is the first connection, */
232 /* just use the first portal group. */
233 if (!sess->num_connections) {
234 group = TAILQ_FIRST(&target->group_list);
235 }
236 /* if it's a second connection, use an available portal in the same */
237 /* portal group */
238 else {
239 conn = (connection_t *) 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\n", 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\n", addr->address, addr->port));
284
285 host = gethostbyname((char *)addr->address);
286 if (host == NULL) {
287 switch (h_errno) {
288 case HOST_NOT_FOUND:
289 res->status = ISCSID_STATUS_HOST_NOT_FOUND;
290 break;
291 case TRY_AGAIN:
292 res->status = ISCSID_STATUS_HOST_TRY_AGAIN;
293 break;
294 default:
295 res->status = ISCSID_STATUS_HOST_ERROR;
296 break;
297 }
298 return NULL;
299 }
300 if (host->h_length > (int)sizeof(serverAddress.sin_addr)) {
301 res->status = ISCSID_STATUS_HOST_ERROR;
302 return NULL;
303 }
304 DEB(8, ("Gethostbyname OK, addrtype %d, len %d, addr %x\n",
305 host->h_addrtype, host->h_length, *((int *) host->h_addr_list[0])));
306 serverAddress.sin_family = host->h_addrtype;
307 serverAddress.sin_port = htons((addr->port)
308 ? addr->port : ISCSI_DEFAULT_PORT);
309 serverAddress.sin_len = host->h_length;
310 memcpy(&serverAddress.sin_addr, host->h_addr_list[0], host->h_length);
311
312 /* alloc the connection structure */
313 conn = calloc(1, sizeof(*conn));
314 if (conn == NULL) {
315 res->status = ISCSID_STATUS_NO_RESOURCES;
316 return NULL;
317 }
318 /* create and connect the socket */
319 sock = socket(AF_INET, SOCK_STREAM, 0);
320 if (sock < 0) {
321 free(conn);
322 res->status = ISCSID_STATUS_SOCKET_ERROR;
323 return NULL;
324 }
325
326 if (init) {
327 if (!bind_socket(sock, init->address)) {
328 close(sock);
329 free(conn);
330 res->status = ISCSID_STATUS_INITIATOR_BIND_ERROR;
331 return NULL;
332 }
333 }
334
335 DEB(8, ("Connecting socket\n"));
336 if (connect(sock, (struct sockaddr *) &serverAddress,
337 sizeof(serverAddress)) < 0) {
338 close(sock);
339 free(conn);
340 res->status = ISCSID_STATUS_CONNECT_ERROR;
341 DEB(1, ("Connecting to socket failed (error %d), returning %d\n",
342 errno, res->status));
343 return NULL;
344 }
345 /* speed up socket processing */
346 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &yes, sizeof(yes));
347
348 /* setup login parameter structure */
349 loginp.socket = sock;
350 if (target->TargetName[0]) {
351 loginp.is_present.TargetName = 1;
352 loginp.TargetName = target->TargetName;
353 }
354 if (target->options.is_present.MaxConnections) {
355 loginp.is_present.MaxConnections = 1;
356 loginp.MaxConnections = target->options.MaxConnections;
357 }
358 if (target->options.is_present.DataDigest) {
359 loginp.is_present.DataDigest = 1;
360 loginp.DataDigest = target->options.DataDigest;
361 }
362 if (target->options.is_present.HeaderDigest) {
363 loginp.is_present.HeaderDigest = 1;
364 loginp.HeaderDigest = target->options.HeaderDigest;
365 }
366 if (target->options.is_present.DefaultTime2Retain) {
367 loginp.is_present.DefaultTime2Retain = 1;
368 loginp.DefaultTime2Retain = target->options.DefaultTime2Retain;
369 }
370 if (target->options.is_present.DefaultTime2Wait) {
371 loginp.is_present.DefaultTime2Wait = 1;
372 loginp.DefaultTime2Wait = target->options.DefaultTime2Wait;
373 }
374 if (target->options.is_present.ErrorRecoveryLevel) {
375 loginp.is_present.ErrorRecoveryLevel = 1;
376 loginp.ErrorRecoveryLevel = target->options.ErrorRecoveryLevel;
377 }
378 if (target->options.is_present.MaxRecvDataSegmentLength) {
379 loginp.is_present.MaxRecvDataSegmentLength = 1;
380 loginp.MaxRecvDataSegmentLength =
381 target->options.MaxRecvDataSegmentLength;
382 }
383 if (target->auth.auth_info.auth_number) {
384 loginp.is_present.auth_info = 1;
385 loginp.auth_info = target->auth.auth_info;
386 if (target->auth.password[0]) {
387 loginp.is_present.password = 1;
388 loginp.password = target->auth.password;
389 }
390 if (target->auth.target_password[0]) {
391 loginp.is_present.target_password = 1;
392 loginp.target_password = target->auth.target_password;
393 }
394 if (target->auth.user_name[0]) {
395 loginp.is_present.user_name = 1;
396 loginp.user_name = target->auth.user_name;
397 }
398 }
399 loginp.is_present.TargetAlias = 1;
400 loginp.TargetAlias = target->TargetAlias;
401
402 if (portal != NULL) {
403 /* override general target options with portal options (if specified) */
404 if (portal->options.is_present.DataDigest) {
405 loginp.is_present.DataDigest = 1;
406 loginp.DataDigest = portal->options.DataDigest;
407 }
408 if (portal->options.is_present.HeaderDigest) {
409 loginp.is_present.HeaderDigest = 1;
410 loginp.HeaderDigest = portal->options.HeaderDigest;
411 }
412 if (portal->options.is_present.MaxRecvDataSegmentLength) {
413 loginp.is_present.MaxRecvDataSegmentLength = 1;
414 loginp.MaxRecvDataSegmentLength =
415 portal->options.MaxRecvDataSegmentLength;
416 }
417 }
418
419 if (req != NULL) {
420 loginp.session_id = get_id(&list[SESSION_LIST].list, &req->session_id);
421 loginp.login_type = req->login_type;
422 } else
423 loginp.login_type = ISCSI_LOGINTYPE_DISCOVERY;
424
425 DEB(5, ("Calling Login...\n"));
426
427 ret = ioctl(driver, (sess != NULL && sess->num_connections)
428 ? ISCSI_ADD_CONNECTION : ISCSI_LOGIN, &loginp);
429
430 res->status = loginp.status;
431
432 if (ret)
433 close(sock);
434
435 if (ret || loginp.status) {
436 free(conn);
437 if (!res->status)
438 res->status = ISCSID_STATUS_GENERAL_ERROR;
439 return NULL;
440 }
441 /* connection established! link connection into session and return IDs */
442
443 conn->loginp = loginp;
444 conn->entry.sid.id = loginp.connection_id;
445 if (req != NULL) {
446 strlcpy((char *)conn->entry.sid.name, (char *)req->sym_name,
447 sizeof(conn->entry.sid.name));
448 }
449
450 /*
451 Copy important target information
452 */
453 conn->target.sid = target->entry.sid;
454 strlcpy((char *)conn->target.TargetName, (char *)target->TargetName,
455 sizeof(conn->target.TargetName));
456 strlcpy((char *)conn->target.TargetAlias, (char *)target->TargetAlias,
457 sizeof(conn->target.TargetAlias));
458 conn->target.options = target->options;
459 conn->target.auth = target->auth;
460 conn->portal.addr = *addr;
461
462 conn->session = sess;
463
464 if (stid == NULL) {
465 iscsid_login_rsp_t *rsp = (iscsid_login_rsp_t *) res->parameter;
466
467 sess->entry.sid.id = loginp.session_id;
468 TAILQ_INSERT_TAIL(&sess->connections, &conn->entry, link);
469 sess->num_connections++;
470
471 res->parameter_length = sizeof(*rsp);
472 rsp->connection_id = conn->entry.sid;
473 rsp->session_id = sess->entry.sid;
474
475 if (init != NULL) {
476 conn->initiator_id = init->entry.sid.id;
477 init->active_connections++;
478 }
479 } else
480 *stid = loginp.session_id;
481
482 /*
483 Copy important portal information
484 */
485 if (portal != NULL) {
486 conn->portal.sid = portal->entry.sid;
487 portal->active_connections++;
488 }
489
490 return conn;
491 }
492
493
494 /*
495 * event_recover_connection:
496 * Handle RECOVER_CONNECTION event: Attempt to re-establish connection.
497 *
498 * Parameter:
499 * sid Session ID
500 * cid Connection ID
501 */
502
503 STATIC void
504 event_recover_connection(uint32_t sid, uint32_t cid)
505 {
506 int sock, ret;
507 int yes = 1;
508 session_t *sess;
509 connection_t *conn;
510 portal_t *portal;
511 initiator_t *init;
512 iscsi_portal_address_t *addr;
513 struct sockaddr_in serverAddress;
514 struct hostent *host;
515
516 DEB(1, ("Event_Recover_Connection sid=%d, cid=%d\n", sid, cid));
517 (void) memset(&serverAddress, 0x0, sizeof(serverAddress));
518
519 LOCK_SESSIONS;
520
521 sess = find_session_id(sid);
522 if (sess == NULL) {
523 UNLOCK_SESSIONS;
524 return;
525 }
526
527 conn = find_connection_id(sess, cid);
528 if (conn == NULL) {
529 UNLOCK_SESSIONS;
530 return;
531 }
532
533 UNLOCK_SESSIONS;
534
535 conn->loginp.status = ISCSI_STATUS_CONNECTION_FAILED;
536
537 /* If we can't find the portal to connect to, abort. */
538
539 if ((portal = find_portal_id(conn->portal.sid.id)) == NULL)
540 return;
541
542 init = find_initiator_id(conn->initiator_id);
543 addr = &portal->addr;
544 conn->portal.addr = *addr;
545
546 /* translate target address */
547 DEB(1, ("Event_Recover_Connection Connecting to <%s>, port %d\n",
548 addr->address, addr->port));
549
550 if ((host = gethostbyname((char *)addr->address)) == NULL) {
551 DEB(1, ("GetHostByName failed (error %d)\n", h_errno));
552 return;
553 }
554 if (host->h_length > (int)sizeof(serverAddress.sin_addr)) {
555 DEB(1, ("Host address length invalid (%d)\n", host->h_length));
556 return;
557 }
558
559 serverAddress.sin_family = host->h_addrtype;
560 serverAddress.sin_port = htons((addr->port)
561 ? addr->port : ISCSI_DEFAULT_PORT);
562 serverAddress.sin_len = host->h_length;
563 memcpy(&serverAddress.sin_addr, host->h_addr_list[0], host->h_length);
564
565 /* create and connect the socket */
566 sock = socket(AF_INET, SOCK_STREAM, 0);
567 if (sock < 0) {
568 DEB(1, ("Creating socket failed (error %d)\n", errno));
569 return;
570 }
571
572 DEB(1, ("recover_connection: Socket = %d\n", sock));
573
574 if (init) {
575 if (!bind_socket(sock, init->address)) {
576 DEB(1, ("Binding to interface failed (error %d)\n", errno));
577 close(sock);
578 return;
579 }
580 }
581
582 if (connect(sock, (struct sockaddr *) &serverAddress,
583 sizeof(serverAddress)) < 0) {
584 DEB(1, ("Connecting to socket failed (error %d)\n", errno));
585 close(sock);
586 return;
587 }
588 /* speed up socket processing */
589 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &yes, sizeof(yes));
590
591 conn->loginp.socket = sock;
592 conn->loginp.status = 0;
593 ret = ioctl(driver, ISCSI_RESTORE_CONNECTION, &conn->loginp);
594
595 if (ret)
596 close(sock);
597 }
598
599
600 /*
601 * login:
602 * Handle LOGIN request: Log into given portal. Create session, then
603 * let make_connection do the rest.
604 *
605 * Parameter:
606 * req The request parameters
607 * res The response buffer
608 */
609
610 void
611 login(iscsid_login_req_t * req, iscsid_response_t * res)
612 {
613 session_t *sess;
614 connection_t *conn;
615
616 sess = calloc(1, sizeof(*sess));
617 if (sess == NULL) {
618 res->status = ISCSID_STATUS_NO_RESOURCES;
619 return;
620 }
621 TAILQ_INIT(&sess->connections);
622 strlcpy((char *)sess->entry.sid.name, (char *)req->sym_name,
623 sizeof(sess->entry.sid.name));
624
625 LOCK_SESSIONS;
626 conn = make_connection(sess, req, res, 0);
627
628 if (conn == NULL)
629 free(sess);
630 else {
631 sess->target = conn->target;
632 TAILQ_INSERT_TAIL(&list[SESSION_LIST].list, &sess->entry, link);
633 list[SESSION_LIST].num_entries++;
634 }
635 UNLOCK_SESSIONS;
636 }
637
638
639 /*
640 * add_connection:
641 * Handle ADD_CONNECTION request: Log secondary connection into given portal.
642 * Find the session, then let make_connection do the rest.
643 *
644 * Parameter:
645 * req The request parameters
646 * res The response buffer
647 */
648
649 void
650 add_connection(iscsid_login_req_t * req, iscsid_response_t * res)
651 {
652 session_t *sess;
653
654 LOCK_SESSIONS;
655 sess = find_session(&req->session_id);
656 if (sess == NULL) {
657 UNLOCK_SESSIONS;
658 res->status = ISCSID_STATUS_INVALID_SESSION_ID;
659 return;
660 }
661
662 make_connection(sess, req, res, 0);
663 UNLOCK_SESSIONS;
664 }
665
666
667 /*
668 * logout:
669 * Handle LOGOUT request: Log out the given session.
670 *
671 * Parameter:
672 * req The request parameters
673 *
674 * Returns: Response status
675 */
676
677 uint32_t
678 logout(iscsid_sym_id_t * req)
679 {
680 iscsi_logout_parameters_t logoutp;
681 session_t *sess;
682 int ret;
683
684 (void) memset(&logoutp, 0x0, sizeof(logoutp));
685 LOCK_SESSIONS;
686 sess = find_session(req);
687 if (sess == NULL) {
688 UNLOCK_SESSIONS;
689 return ISCSID_STATUS_INVALID_SESSION_ID;
690 }
691
692 logoutp.session_id = sess->entry.sid.id;
693 UNLOCK_SESSIONS;
694
695 ret = ioctl(driver, ISCSI_LOGOUT, &logoutp);
696 DEB(9, ("Logout returns %d, status = %d\n", ret, logoutp.status));
697
698 return logoutp.status;
699 }
700
701
702 /*
703 * remove_connection:
704 * Handle REMOVE_CONNECTION request: Log out the given connection.
705 *
706 * Parameter:
707 * req The request parameters
708 *
709 * Returns: Response status
710 */
711
712 uint32_t
713 remove_connection(iscsid_remove_connection_req_t * req)
714 {
715 iscsi_remove_parameters_t removep;
716 session_t *sess;
717 connection_t *conn;
718 int ret;
719
720 LOCK_SESSIONS;
721 sess = find_session(&req->session_id);
722 if (sess == NULL) {
723 UNLOCK_SESSIONS;
724 return ISCSID_STATUS_INVALID_SESSION_ID;
725 }
726 conn = find_connection(sess, &req->connection_id);
727 if (conn == NULL) {
728 UNLOCK_SESSIONS;
729 return ISCSID_STATUS_INVALID_CONNECTION_ID;
730 }
731
732 removep.session_id = sess->entry.sid.id;
733 removep.connection_id = conn->entry.sid.id;
734 UNLOCK_SESSIONS;
735
736 ret = ioctl(driver, ISCSI_REMOVE_CONNECTION, &removep);
737 DEB(9, ("Remove Connection returns %d, status=%d\n", ret, removep.status));
738
739 return removep.status;
740 }
741
742 /*
743 * send_targets:
744 * Handle SEND_TARGETS request:
745 * First login with type = discovery.
746 * Then send the SendTargets iSCSI request to the target, which will
747 * return a list of target portals.
748 * Then logout.
749 *
750 * Parameter:
751 * stid The send target ID
752 * response_buffer Pointer to pointer to buffer containing response
753 * The response contains the list of the target
754 * portals. The caller frees the buffer after it
755 * is done with it.
756 * response_size Pointer to variable which upon return will hold
757 * the size of the response buffer.
758 *
759 * Returns: Response status
760 */
761
762 uint32_t
763 send_targets(uint32_t stid, uint8_t **response_buffer, uint32_t *response_size)
764 {
765 iscsi_send_targets_parameters_t sendt;
766 iscsi_logout_parameters_t logoutp;
767 int ret;
768 connection_t *conn;
769 iscsid_response_t res;
770 uint32_t rc = ISCSID_STATUS_SUCCESS;
771
772 (void) memset(&sendt, 0x0, sizeof(sendt));
773 (void) memset(&logoutp, 0x0, sizeof(logoutp));
774 (void) memset(&res, 0x0, sizeof(res));
775 conn = make_connection(NULL, NULL, &res, &stid);
776 DEB(9, ("Make connection returns, status = %d\n", res.status));
777
778 if (conn == NULL)
779 return res.status;
780
781 sendt.session_id = stid;
782 sendt.response_buffer = NULL;
783 sendt.response_size = 0;
784 sendt.response_used = sendt.response_total = 0;
785 strlcpy((char *)sendt.key, "All", sizeof(sendt.key));
786
787 /*Call once to get the size of the buffer necessary */
788 ret = ioctl(driver, ISCSI_SEND_TARGETS, &sendt);
789
790 if (!ret && !sendt.status) {
791 /* Allocate buffer required and call again to retrieve data */
792 /* We allocate one extra byte so we can place a terminating 0 */
793 /* at the end of the buffer. */
794
795 sendt.response_size = sendt.response_total;
796 sendt.response_buffer = calloc(1, sendt.response_size + 1);
797 if (sendt.response_buffer == NULL)
798 rc = ISCSID_STATUS_NO_RESOURCES;
799 else {
800 ret = ioctl(driver, ISCSI_SEND_TARGETS, &sendt);
801 ((uint8_t *)sendt.response_buffer)[sendt.response_size] = 0;
802
803 if (ret || sendt.status) {
804 free(sendt.response_buffer);
805 sendt.response_buffer = NULL;
806 sendt.response_used = 0;
807 if ((rc = sendt.status) == 0)
808 rc = ISCSID_STATUS_GENERAL_ERROR;
809 }
810 }
811 } else if ((rc = sendt.status) == 0)
812 rc = ISCSID_STATUS_GENERAL_ERROR;
813
814 *response_buffer = sendt.response_buffer;
815 *response_size = sendt.response_used;
816
817 logoutp.session_id = stid;
818 ret = ioctl(driver, ISCSI_LOGOUT, &logoutp);
819 /* ignore logout status */
820
821 free(conn);
822
823 return rc;
824 }
825
826
827
828 /*
829 * get_version:
830 * Handle GET_VERSION request.
831 *
832 * Returns: Filled get_version_rsp structure.
833 */
834
835 void
836 get_version(iscsid_response_t ** prsp, int *prsp_temp)
837 {
838 iscsid_response_t *rsp = *prsp;
839 iscsid_get_version_rsp_t *ver;
840 iscsi_get_version_parameters_t drv_ver;
841
842 rsp = make_rsp(sizeof(iscsid_get_version_rsp_t), prsp, prsp_temp);
843 if (rsp == NULL)
844 return;
845 ver = (iscsid_get_version_rsp_t *) rsp->parameter;
846
847 ver->interface_version = INTERFACE_VERSION;
848 ver->major = VERSION_MAJOR;
849 ver->minor = VERSION_MINOR;
850 strlcpy ((char *)ver->version_string, VERSION_STRING, sizeof(ver->version_string));
851
852 #ifdef ISCSI_DEBUG /* DEBUG ONLY: Allow op without driver present */
853 if (driver < 0)
854 return;
855 #endif
856 ioctl(driver, ISCSI_GET_VERSION, &drv_ver);
857 ver->driver_interface_version = drv_ver.interface_version;
858 ver->driver_major = drv_ver.major;
859 ver->driver_minor = drv_ver.minor;
860 strlcpy ((char *)ver->driver_version_string, (char *)drv_ver.version_string,
861 sizeof (ver->driver_version_string));
862 }
863
864
865 /* -------------------------------------------------------------------------- */
866
867 iscsi_register_event_parameters_t event_reg; /* registered event ID */
868
869
870 /*
871 * register_event_handler:
872 * Call driver to register the event handler.
873 *
874 * Returns:
875 * TRUE on success.
876 */
877
878 boolean_t
879 register_event_handler(void)
880 {
881 ioctl(driver, ISCSI_REGISTER_EVENT, &event_reg);
882 return event_reg.event_id != 0;
883 }
884
885
886 /*
887 * deregister_event_handler:
888 * Call driver to deregister the event handler. If the event handler thread
889 * is waiting for an event, this will wake it up and cause it to exit.
890 */
891
892 void
893 deregister_event_handler(void)
894 {
895 if (event_reg.event_id) {
896 ioctl(driver, ISCSI_DEREGISTER_EVENT, &event_reg);
897 event_reg.event_id = 0;
898 }
899 }
900
901
902 /*
903 * event_handler:
904 * Event handler thread. Wait for the driver to generate an event and
905 * process it appropriately. Exits when the driver terminates or the
906 * handler is deregistered because the daemon is terminating.
907 *
908 * Parameter:
909 * par Not used.
910 */
911
912 void *
913 event_handler(void *par)
914 {
915 iscsi_wait_event_parameters_t evtp;
916 int rc;
917
918 DEB(99, ("Event handler starts\n"));
919 (void) memset(&evtp, 0x0, sizeof(evtp));
920
921 evtp.event_id = event_reg.event_id;
922
923 do {
924 #ifndef ISCSI_NOTHREAD
925 rc = ioctl(driver, ISCSI_WAIT_EVENT, &evtp);
926 #else
927 rc = ioctl(driver, ISCSI_POLL_EVENT, &evtp);
928 #endif
929 if (rc || evtp.status)
930 break;
931
932 DEB(1, ("Got Event: kind %d, status %d, sid %d, cid %d, reason %d\n",
933 evtp.event_kind, evtp.status, evtp.session_id,
934 evtp.connection_id, evtp.reason));
935
936 switch (evtp.event_kind) {
937 case ISCSI_SESSION_TERMINATED:
938 event_kill_session(evtp.session_id);
939 break;
940
941 case ISCSI_CONNECTION_TERMINATED:
942 event_kill_connection(evtp.session_id, evtp.connection_id);
943 break;
944
945 case ISCSI_RECOVER_CONNECTION:
946 event_recover_connection(evtp.session_id, evtp.connection_id);
947 break;
948
949 default:
950 break;
951 }
952 } while (evtp.event_kind != ISCSI_DRIVER_TERMINATING);
953
954 #ifdef ISCSI_NOTHREAD
955 if (evtp.event_kind == ISCSI_DRIVER_TERMINATING)
956 #endif
957 exit_daemon();
958
959 return NULL;
960 }
961
962 #if 0
963 /*
964 * verify_connection:
965 * Verify that a specific connection still exists, delete it if not.
966 *
967 * Parameter: The connection pointer.
968 *
969 * Returns: The status returned by the driver.
970 */
971
972 uint32_t
973 verify_connection(connection_t * conn)
974 {
975 iscsi_conn_status_parameters_t req;
976 session_t *sess = conn->session;
977
978 req.connection_id = conn->entry.sid.id;
979 req.session_id = sess->entry.sid.id;
980
981 ioctl(driver, ISCSI_CONNECTION_STATUS, &req);
982
983 if (req.status) {
984 TAILQ_REMOVE(&sess->connections, &conn->entry, link);
985 sess->num_connections--;
986 free(conn);
987 }
988 DEB(9, ("Verify connection returns status %d\n", req.status));
989 return req.status;
990 }
991
992 #endif
993