xref: /freebsd/usr.sbin/iscsid/discovery.c (revision acb1f1269c6f4ff89a0d28ba742f6687e9ef779d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 text_receive(struct connection *conn)
46 {
47 	struct pdu *response;
48 	struct iscsi_bhs_text_response *bhstr;
49 
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;
56 #if 0
57 	if ((bhstr->bhstr_flags & BHSTR_FLAGS_FINAL) == 0)
58 		log_errx(1, "received Text PDU without the \"F\" flag");
59 #endif
60 	/*
61 	 * XXX: Implement the C flag some day.
62 	 */
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);
69 	}
70 	conn->conn_statsn = ntohl(bhstr->bhstr_statsn);
71 
72 	return (response);
73 }
74 
75 static struct pdu *
76 text_new_request(struct connection *conn)
77 {
78 	struct pdu *request;
79 	struct iscsi_bhs_text_request *bhstr;
80 
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;
88 
89 	bhstr->bhstr_initiator_task_tag = 0; /* XXX */
90 	bhstr->bhstr_cmdsn = 0; /* XXX */
91 	bhstr->bhstr_expstatsn = htonl(conn->conn_statsn + 1);
92 
93 	return (request);
94 }
95 
96 static struct pdu *
97 logout_receive(struct connection *conn)
98 {
99 	struct pdu *response;
100 	struct iscsi_bhs_logout_response *bhslr;
101 
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);
115 	}
116 	conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
117 
118 	return (response);
119 }
120 
121 static struct pdu *
122 logout_new_request(struct connection *conn)
123 {
124 	struct pdu *request;
125 	struct iscsi_bhs_logout_request *bhslr;
126 
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);
136 
137 	return (request);
138 }
139 
140 static void
141 kernel_add(const struct connection *conn, const char *target)
142 {
143 	struct iscsi_session_add isa;
144 	int error;
145 
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);
152 	if (error != 0)
153 		log_warn("failed to add %s: ISCSISADD", target);
154 }
155 
156 static void
157 kernel_remove(const struct connection *conn)
158 {
159 	struct iscsi_session_remove isr;
160 	int error;
161 
162 	memset(&isr, 0, sizeof(isr));
163 	isr.isr_session_id = conn->conn_session_id;
164 	error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
165 	if (error != 0)
166 		log_warn("ISCSISREMOVE");
167 }
168 
169 void
170 discovery(struct connection *conn)
171 {
172 	struct pdu *request, *response;
173 	struct keys *request_keys, *response_keys;
174 	int i;
175 
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);
182 	request_keys = NULL;
183 	pdu_send(request);
184 	pdu_delete(request);
185 	request = NULL;
186 
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)
193 			break;
194 
195 		if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
196 			continue;
197 
198 		log_debugx("adding target %s", response_keys->keys_values[i]);
199 		/*
200 		 * XXX: Validate the target name?
201 		 */
202 		kernel_add(conn, response_keys->keys_values[i]);
203 	}
204 	keys_delete(response_keys);
205 	pdu_delete(response);
206 
207 	log_debugx("removing temporary discovery session");
208 	kernel_remove(conn);
209 
210 #ifdef ICL_KERNEL_PROXY
211 	if (conn->conn_conf.isc_iser == 1) {
212 		/*
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.
216 		 */
217 		log_debugx("discovery session done");
218 		return;
219 	}
220 #endif
221 
222 	log_debugx("discovery done; logging out");
223 	request = logout_new_request(conn);
224 	pdu_send(request);
225 	pdu_delete(request);
226 	request = NULL;
227 
228 	log_debugx("waiting for Logout Response");
229 	response = logout_receive(conn);
230 	pdu_delete(response);
231 
232 	log_debugx("discovery session done");
233 }
234