2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
39 #include <netinet/in.h>
42 #include "iscsi_proto.h"
45 text_receive(struct connection *conn)
48 struct iscsi_bhs_text_response *bhstr;
50 response = pdu_new(conn);
51 pdu_receive(response);
52 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_TEXT_RESPONSE)
53 log_errx(1, "protocol error: received invalid opcode 0x%x",
54 response->pdu_bhs->bhs_opcode);
55 bhstr = (struct iscsi_bhs_text_response *)response->pdu_bhs;
57 if ((bhstr->bhstr_flags & BHSTR_FLAGS_FINAL) == 0)
58 log_errx(1, "received Text PDU without the \"F\" flag");
61 * XXX: Implement the C flag some day.
63 if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
64 log_errx(1, "received Text PDU with unsupported \"C\" flag");
65 if (ntohl(bhstr->bhstr_statsn) != conn->conn_statsn + 1) {
66 log_errx(1, "received Text PDU with wrong StatSN: "
67 "is %u, should be %u", ntohl(bhstr->bhstr_statsn),
68 conn->conn_statsn + 1);
70 conn->conn_statsn = ntohl(bhstr->bhstr_statsn);
76 text_new_request(struct connection *conn)
79 struct iscsi_bhs_text_request *bhstr;
81 request = pdu_new(conn);
82 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
83 bhstr->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_REQUEST |
84 ISCSI_BHS_OPCODE_IMMEDIATE;
85 bhstr->bhstr_flags = BHSTR_FLAGS_FINAL;
86 bhstr->bhstr_initiator_task_tag = 0;
87 bhstr->bhstr_target_transfer_tag = 0xffffffff;
89 bhstr->bhstr_initiator_task_tag = 0; /* XXX */
90 bhstr->bhstr_cmdsn = 0; /* XXX */
91 bhstr->bhstr_expstatsn = htonl(conn->conn_statsn + 1);
97 logout_receive(struct connection *conn)
100 struct iscsi_bhs_logout_response *bhslr;
102 response = pdu_new(conn);
103 pdu_receive(response);
104 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE)
105 log_errx(1, "protocol error: received invalid opcode 0x%x",
106 response->pdu_bhs->bhs_opcode);
107 bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
108 if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY)
109 log_warnx("received Logout Response with reason %d",
110 ntohs(bhslr->bhslr_response));
111 if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
112 log_errx(1, "received Logout PDU with wrong StatSN: "
113 "is %u, should be %u", ntohl(bhslr->bhslr_statsn),
114 conn->conn_statsn + 1);
116 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
122 logout_new_request(struct connection *conn)
125 struct iscsi_bhs_logout_request *bhslr;
127 request = pdu_new(conn);
128 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
129 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST |
130 ISCSI_BHS_OPCODE_IMMEDIATE;
131 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
132 bhslr->bhslr_reason |= 0x80;
133 bhslr->bhslr_initiator_task_tag = 0; /* XXX */
134 bhslr->bhslr_cmdsn = 0; /* XXX */
135 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
141 kernel_add(const struct connection *conn, const char *target)
143 struct iscsi_session_add isa;
146 memset(&isa, 0, sizeof(isa));
147 memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf));
148 strlcpy(isa.isa_conf.isc_target, target,
149 sizeof(isa.isa_conf.isc_target));
150 isa.isa_conf.isc_discovery = 0;
151 error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa);
153 log_warn("failed to add %s: ISCSISADD", target);
157 kernel_remove(const struct connection *conn)
159 struct iscsi_session_remove isr;
162 memset(&isr, 0, sizeof(isr));
163 isr.isr_session_id = conn->conn_session_id;
164 error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
166 log_warn("ISCSISREMOVE");
170 discovery(struct connection *conn)
172 struct pdu *request, *response;
173 struct keys *request_keys, *response_keys;
176 log_debugx("beginning discovery session");
177 request = text_new_request(conn);
178 request_keys = keys_new();
179 keys_add(request_keys, "SendTargets", "All");
180 keys_save(request_keys, request);
181 keys_delete(request_keys);
187 log_debugx("waiting for Text Response");
188 response = text_receive(conn);
189 response_keys = keys_new();
190 keys_load(response_keys, response);
191 for (i = 0; i < KEYS_MAX; i++) {
192 if (response_keys->keys_names[i] == NULL)
195 if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
198 log_debugx("adding target %s", response_keys->keys_values[i]);
200 * XXX: Validate the target name?
202 kernel_add(conn, response_keys->keys_values[i]);
204 keys_delete(response_keys);
205 pdu_delete(response);
207 log_debugx("removing temporary discovery session");
210 #ifdef ICL_KERNEL_PROXY
211 if (conn->conn_conf.isc_iser == 1) {
213 * If we're going through the proxy, the kernel already
214 * sent Logout PDU for us and destroyed the session,
215 * so we can't send anything anymore.
217 log_debugx("discovery session done");
222 log_debugx("discovery done; logging out");
223 request = logout_new_request(conn);
228 log_debugx("waiting for Logout Response");
229 response = logout_receive(conn);
230 pdu_delete(response);
232 log_debugx("discovery session done");