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/types.h>
33 #include <sys/ioctl.h>
36 #include <netinet/in.h>
39 #include "iscsi_proto.h"
42 logout_receive(struct connection *conn)
45 struct iscsi_bhs_logout_response *bhslr;
47 response = pdu_new(conn);
48 pdu_receive(response);
49 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE)
50 log_errx(1, "protocol error: received invalid opcode 0x%x",
51 response->pdu_bhs->bhs_opcode);
52 bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
53 if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY)
54 log_warnx("received Logout Response with reason %d",
55 ntohs(bhslr->bhslr_response));
56 if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
57 log_errx(1, "received Logout PDU with wrong StatSN: "
58 "is %u, should be %u", ntohl(bhslr->bhslr_statsn),
59 conn->conn_statsn + 1);
61 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
67 logout_new_request(struct connection *conn)
70 struct iscsi_bhs_logout_request *bhslr;
72 request = pdu_new(conn);
73 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
74 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST |
75 ISCSI_BHS_OPCODE_IMMEDIATE;
76 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
77 bhslr->bhslr_reason |= 0x80;
78 bhslr->bhslr_initiator_task_tag = 0; /* XXX */
79 bhslr->bhslr_cmdsn = 0; /* XXX */
80 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
86 kernel_add(const struct iscsid_connection *conn, const char *target)
88 struct iscsi_session_add isa;
91 memset(&isa, 0, sizeof(isa));
92 memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf));
93 strlcpy(isa.isa_conf.isc_target, target,
94 sizeof(isa.isa_conf.isc_target));
95 isa.isa_conf.isc_discovery = 0;
96 error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa);
98 log_warn("failed to add %s: ISCSISADD", target);
102 kernel_remove(const struct iscsid_connection *conn)
104 struct iscsi_session_remove isr;
107 memset(&isr, 0, sizeof(isr));
108 isr.isr_session_id = conn->conn_session_id;
109 error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
111 log_warn("ISCSISREMOVE");
115 discovery(struct iscsid_connection *conn)
117 struct pdu *request, *response;
118 struct keys *request_keys, *response_keys;
121 log_debugx("beginning discovery session");
122 request_keys = keys_new();
123 keys_add(request_keys, "SendTargets", "All");
124 text_send_request(&conn->conn, request_keys);
125 keys_delete(request_keys);
128 log_debugx("waiting for Text Response");
129 response_keys = text_read_response(&conn->conn);
130 for (i = 0; i < KEYS_MAX; i++) {
131 if (response_keys->keys_names[i] == NULL)
134 if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
137 log_debugx("adding target %s", response_keys->keys_values[i]);
139 * XXX: Validate the target name?
141 kernel_add(conn, response_keys->keys_values[i]);
143 keys_delete(response_keys);
145 log_debugx("removing temporary discovery session");
148 #ifdef ICL_KERNEL_PROXY
149 if (conn->conn_conf.isc_iser == 1) {
151 * If we're going through the proxy, the kernel already
152 * sent Logout PDU for us and destroyed the session,
153 * so we can't send anything anymore.
155 log_debugx("discovery session done");
160 log_debugx("discovery done; logging out");
161 request = logout_new_request(&conn->conn);
166 log_debugx("waiting for Logout Response");
167 response = logout_receive(&conn->conn);
168 pdu_delete(response);
170 log_debugx("discovery session done");