2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
40 #include <netinet/in.h>
43 #include "iscsi_proto.h"
46 text_receive(struct connection *conn)
49 struct iscsi_bhs_text_response *bhstr;
51 response = pdu_new(conn);
52 pdu_receive(response);
53 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_TEXT_RESPONSE)
54 log_errx(1, "protocol error: received invalid opcode 0x%x",
55 response->pdu_bhs->bhs_opcode);
56 bhstr = (struct iscsi_bhs_text_response *)response->pdu_bhs;
58 if ((bhstr->bhstr_flags & BHSTR_FLAGS_FINAL) == 0)
59 log_errx(1, "received Text PDU without the \"F\" flag");
62 * XXX: Implement the C flag some day.
64 if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
65 log_errx(1, "received Text PDU with unsupported \"C\" flag");
66 if (ntohl(bhstr->bhstr_statsn) != conn->conn_statsn + 1) {
67 log_errx(1, "received Text PDU with wrong StatSN: "
68 "is %u, should be %u", ntohl(bhstr->bhstr_statsn),
69 conn->conn_statsn + 1);
71 conn->conn_statsn = ntohl(bhstr->bhstr_statsn);
77 text_new_request(struct connection *conn)
80 struct iscsi_bhs_text_request *bhstr;
82 request = pdu_new(conn);
83 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
84 bhstr->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_REQUEST |
85 ISCSI_BHS_OPCODE_IMMEDIATE;
86 bhstr->bhstr_flags = BHSTR_FLAGS_FINAL;
87 bhstr->bhstr_initiator_task_tag = 0;
88 bhstr->bhstr_target_transfer_tag = 0xffffffff;
90 bhstr->bhstr_initiator_task_tag = 0; /* XXX */
91 bhstr->bhstr_cmdsn = 0; /* XXX */
92 bhstr->bhstr_expstatsn = htonl(conn->conn_statsn + 1);
98 logout_receive(struct connection *conn)
100 struct pdu *response;
101 struct iscsi_bhs_logout_response *bhslr;
103 response = pdu_new(conn);
104 pdu_receive(response);
105 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE)
106 log_errx(1, "protocol error: received invalid opcode 0x%x",
107 response->pdu_bhs->bhs_opcode);
108 bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
109 if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY)
110 log_warnx("received Logout Response with reason %d",
111 ntohs(bhslr->bhslr_response));
112 if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
113 log_errx(1, "received Logout PDU with wrong StatSN: "
114 "is %u, should be %u", ntohl(bhslr->bhslr_statsn),
115 conn->conn_statsn + 1);
117 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
123 logout_new_request(struct connection *conn)
126 struct iscsi_bhs_logout_request *bhslr;
128 request = pdu_new(conn);
129 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
130 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST |
131 ISCSI_BHS_OPCODE_IMMEDIATE;
132 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
133 bhslr->bhslr_reason |= 0x80;
134 bhslr->bhslr_initiator_task_tag = 0; /* XXX */
135 bhslr->bhslr_cmdsn = 0; /* XXX */
136 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
142 kernel_add(const struct connection *conn, const char *target)
144 struct iscsi_session_add isa;
147 memset(&isa, 0, sizeof(isa));
148 memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf));
149 strlcpy(isa.isa_conf.isc_target, target,
150 sizeof(isa.isa_conf.isc_target));
151 isa.isa_conf.isc_discovery = 0;
152 error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa);
154 log_warn("failed to add %s: ISCSISADD", target);
158 kernel_remove(const struct connection *conn)
160 struct iscsi_session_remove isr;
163 memset(&isr, 0, sizeof(isr));
164 isr.isr_session_id = conn->conn_session_id;
165 error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
167 log_warn("ISCSISREMOVE");
171 discovery(struct connection *conn)
173 struct pdu *request, *response;
174 struct keys *request_keys, *response_keys;
177 log_debugx("beginning discovery session");
178 request = text_new_request(conn);
179 request_keys = keys_new();
180 keys_add(request_keys, "SendTargets", "All");
181 keys_save(request_keys, request);
182 keys_delete(request_keys);
188 log_debugx("waiting for Text Response");
189 response = text_receive(conn);
190 response_keys = keys_new();
191 keys_load(response_keys, response);
192 for (i = 0; i < KEYS_MAX; i++) {
193 if (response_keys->keys_names[i] == NULL)
196 if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
199 log_debugx("adding target %s", response_keys->keys_values[i]);
201 * XXX: Validate the target name?
203 kernel_add(conn, response_keys->keys_values[i]);
205 keys_delete(response_keys);
206 pdu_delete(response);
208 log_debugx("removing temporary discovery session");
211 #ifdef ICL_KERNEL_PROXY
212 if (conn->conn_conf.isc_iser == 1) {
214 * If we're going through the proxy, the kernel already
215 * sent Logout PDU for us and destroyed the session,
216 * so we can't send anything anymore.
218 log_debugx("discovery session done");
223 log_debugx("discovery done; logging out");
224 request = logout_new_request(conn);
229 log_debugx("waiting for Logout Response");
230 response = logout_receive(conn);
231 pdu_delete(response);
233 log_debugx("discovery session done");