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 __FBSDID("$FreeBSD$"); 34 35 #include <sys/types.h> 36 #include <sys/ioctl.h> 37 #include <stdbool.h> 38 #include <string.h> 39 #include <netinet/in.h> 40 41 #include "iscsid.h" 42 #include "iscsi_proto.h" 43 44 static struct pdu * 45 logout_receive(struct connection *conn) 46 { 47 struct pdu *response; 48 struct iscsi_bhs_logout_response *bhslr; 49 50 response = pdu_new(conn); 51 pdu_receive(response); 52 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE) 53 log_errx(1, "protocol error: received invalid opcode 0x%x", 54 response->pdu_bhs->bhs_opcode); 55 bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs; 56 if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY) 57 log_warnx("received Logout Response with reason %d", 58 ntohs(bhslr->bhslr_response)); 59 if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) { 60 log_errx(1, "received Logout PDU with wrong StatSN: " 61 "is %u, should be %u", ntohl(bhslr->bhslr_statsn), 62 conn->conn_statsn + 1); 63 } 64 conn->conn_statsn = ntohl(bhslr->bhslr_statsn); 65 66 return (response); 67 } 68 69 static struct pdu * 70 logout_new_request(struct connection *conn) 71 { 72 struct pdu *request; 73 struct iscsi_bhs_logout_request *bhslr; 74 75 request = pdu_new(conn); 76 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs; 77 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST | 78 ISCSI_BHS_OPCODE_IMMEDIATE; 79 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION; 80 bhslr->bhslr_reason |= 0x80; 81 bhslr->bhslr_initiator_task_tag = 0; /* XXX */ 82 bhslr->bhslr_cmdsn = 0; /* XXX */ 83 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1); 84 85 return (request); 86 } 87 88 static void 89 kernel_add(const struct iscsid_connection *conn, const char *target) 90 { 91 struct iscsi_session_add isa; 92 int error; 93 94 memset(&isa, 0, sizeof(isa)); 95 memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf)); 96 strlcpy(isa.isa_conf.isc_target, target, 97 sizeof(isa.isa_conf.isc_target)); 98 isa.isa_conf.isc_discovery = 0; 99 error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa); 100 if (error != 0) 101 log_warn("failed to add %s: ISCSISADD", target); 102 } 103 104 static void 105 kernel_remove(const struct iscsid_connection *conn) 106 { 107 struct iscsi_session_remove isr; 108 int error; 109 110 memset(&isr, 0, sizeof(isr)); 111 isr.isr_session_id = conn->conn_session_id; 112 error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr); 113 if (error != 0) 114 log_warn("ISCSISREMOVE"); 115 } 116 117 void 118 discovery(struct iscsid_connection *conn) 119 { 120 struct pdu *request, *response; 121 struct keys *request_keys, *response_keys; 122 int i; 123 124 log_debugx("beginning discovery session"); 125 request_keys = keys_new(); 126 keys_add(request_keys, "SendTargets", "All"); 127 text_send_request(&conn->conn, request_keys); 128 keys_delete(request_keys); 129 request_keys = NULL; 130 131 log_debugx("waiting for Text Response"); 132 response_keys = text_read_response(&conn->conn); 133 for (i = 0; i < KEYS_MAX; i++) { 134 if (response_keys->keys_names[i] == NULL) 135 break; 136 137 if (strcmp(response_keys->keys_names[i], "TargetName") != 0) 138 continue; 139 140 log_debugx("adding target %s", response_keys->keys_values[i]); 141 /* 142 * XXX: Validate the target name? 143 */ 144 kernel_add(conn, response_keys->keys_values[i]); 145 } 146 keys_delete(response_keys); 147 148 log_debugx("removing temporary discovery session"); 149 kernel_remove(conn); 150 151 #ifdef ICL_KERNEL_PROXY 152 if (conn->conn_conf.isc_iser == 1) { 153 /* 154 * If we're going through the proxy, the kernel already 155 * sent Logout PDU for us and destroyed the session, 156 * so we can't send anything anymore. 157 */ 158 log_debugx("discovery session done"); 159 return; 160 } 161 #endif 162 163 log_debugx("discovery done; logging out"); 164 request = logout_new_request(&conn->conn); 165 pdu_send(request); 166 pdu_delete(request); 167 request = NULL; 168 169 log_debugx("waiting for Logout Response"); 170 response = logout_receive(&conn->conn); 171 pdu_delete(response); 172 173 log_debugx("discovery session done"); 174 } 175