1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * 6 * This software was developed by Edward Tomasz Napierala under sponsorship 7 * from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #include <sys/cdefs.h> 33 #include <sys/types.h> 34 #include <sys/ioctl.h> 35 #include <stdbool.h> 36 #include <string.h> 37 #include <netinet/in.h> 38 39 #include "iscsid.h" 40 #include "iscsi_proto.h" 41 42 static struct pdu * 43 logout_receive(struct connection *conn) 44 { 45 struct pdu *response; 46 struct iscsi_bhs_logout_response *bhslr; 47 48 response = pdu_new(conn); 49 pdu_receive(response); 50 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE) 51 log_errx(1, "protocol error: received invalid opcode 0x%x", 52 response->pdu_bhs->bhs_opcode); 53 bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs; 54 if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY) 55 log_warnx("received Logout Response with reason %d", 56 ntohs(bhslr->bhslr_response)); 57 if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) { 58 log_errx(1, "received Logout PDU with wrong StatSN: " 59 "is %u, should be %u", ntohl(bhslr->bhslr_statsn), 60 conn->conn_statsn + 1); 61 } 62 conn->conn_statsn = ntohl(bhslr->bhslr_statsn); 63 64 return (response); 65 } 66 67 static struct pdu * 68 logout_new_request(struct connection *conn) 69 { 70 struct pdu *request; 71 struct iscsi_bhs_logout_request *bhslr; 72 73 request = pdu_new(conn); 74 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs; 75 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST | 76 ISCSI_BHS_OPCODE_IMMEDIATE; 77 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION; 78 bhslr->bhslr_reason |= 0x80; 79 bhslr->bhslr_initiator_task_tag = 0; /* XXX */ 80 bhslr->bhslr_cmdsn = 0; /* XXX */ 81 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1); 82 83 return (request); 84 } 85 86 static void 87 kernel_add(const struct iscsid_connection *conn, const char *target) 88 { 89 struct iscsi_session_add isa; 90 int error; 91 92 memset(&isa, 0, sizeof(isa)); 93 memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf)); 94 strlcpy(isa.isa_conf.isc_target, target, 95 sizeof(isa.isa_conf.isc_target)); 96 isa.isa_conf.isc_discovery = 0; 97 error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa); 98 if (error != 0) 99 log_warn("failed to add %s: ISCSISADD", target); 100 } 101 102 static void 103 kernel_remove(const struct iscsid_connection *conn) 104 { 105 struct iscsi_session_remove isr; 106 int error; 107 108 memset(&isr, 0, sizeof(isr)); 109 isr.isr_session_id = conn->conn_session_id; 110 error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr); 111 if (error != 0) 112 log_warn("ISCSISREMOVE"); 113 } 114 115 void 116 discovery(struct iscsid_connection *conn) 117 { 118 struct pdu *request, *response; 119 struct keys *request_keys, *response_keys; 120 int i; 121 122 log_debugx("beginning discovery session"); 123 request_keys = keys_new(); 124 keys_add(request_keys, "SendTargets", "All"); 125 text_send_request(&conn->conn, request_keys); 126 keys_delete(request_keys); 127 request_keys = NULL; 128 129 log_debugx("waiting for Text Response"); 130 response_keys = text_read_response(&conn->conn); 131 for (i = 0; i < KEYS_MAX; i++) { 132 if (response_keys->keys_names[i] == NULL) 133 break; 134 135 if (strcmp(response_keys->keys_names[i], "TargetName") != 0) 136 continue; 137 138 log_debugx("adding target %s", response_keys->keys_values[i]); 139 /* 140 * XXX: Validate the target name? 141 */ 142 kernel_add(conn, response_keys->keys_values[i]); 143 } 144 keys_delete(response_keys); 145 146 log_debugx("removing temporary discovery session"); 147 kernel_remove(conn); 148 149 #ifdef ICL_KERNEL_PROXY 150 if (conn->conn_conf.isc_iser == 1) { 151 /* 152 * If we're going through the proxy, the kernel already 153 * sent Logout PDU for us and destroyed the session, 154 * so we can't send anything anymore. 155 */ 156 log_debugx("discovery session done"); 157 return; 158 } 159 #endif 160 161 log_debugx("discovery done; logging out"); 162 request = logout_new_request(&conn->conn); 163 pdu_send(request); 164 pdu_delete(request); 165 request = NULL; 166 167 log_debugx("waiting for Logout Response"); 168 response = logout_receive(&conn->conn); 169 pdu_delete(response); 170 171 log_debugx("discovery session done"); 172 } 173