2 * SPDX-License-Identifier: BSD-2-Clause
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 logout_receive(struct connection *conn)
48 struct iscsi_bhs_logout_response *bhslr;
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);
64 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
70 logout_new_request(struct connection *conn)
73 struct iscsi_bhs_logout_request *bhslr;
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);
89 kernel_add(const struct iscsid_connection *conn, const char *target)
91 struct iscsi_session_add isa;
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);
101 log_warn("failed to add %s: ISCSISADD", target);
105 kernel_remove(const struct iscsid_connection *conn)
107 struct iscsi_session_remove isr;
110 memset(&isr, 0, sizeof(isr));
111 isr.isr_session_id = conn->conn_session_id;
112 error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
114 log_warn("ISCSISREMOVE");
118 discovery(struct iscsid_connection *conn)
120 struct pdu *request, *response;
121 struct keys *request_keys, *response_keys;
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);
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)
137 if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
140 log_debugx("adding target %s", response_keys->keys_values[i]);
142 * XXX: Validate the target name?
144 kernel_add(conn, response_keys->keys_values[i]);
146 keys_delete(response_keys);
148 log_debugx("removing temporary discovery session");
151 #ifdef ICL_KERNEL_PROXY
152 if (conn->conn_conf.isc_iser == 1) {
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.
158 log_debugx("discovery session done");
163 log_debugx("discovery done; logging out");
164 request = logout_new_request(&conn->conn);
169 log_debugx("waiting for Logout Response");
170 response = logout_receive(&conn->conn);
171 pdu_delete(response);
173 log_debugx("discovery session done");