xref: /freebsd/usr.sbin/iscsid/discovery.c (revision fd45b686f9d92f583366c75b22c04c7ee49709c0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/types.h>
33 #include <sys/ioctl.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <netinet/in.h>
37 
38 #include "iscsid.h"
39 #include "iscsi_proto.h"
40 
41 static struct pdu *
42 logout_receive(struct connection *conn)
43 {
44 	struct pdu *response;
45 	struct iscsi_bhs_logout_response *bhslr;
46 
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);
60 	}
61 	conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
62 
63 	return (response);
64 }
65 
66 static struct pdu *
67 logout_new_request(struct connection *conn)
68 {
69 	struct pdu *request;
70 	struct iscsi_bhs_logout_request *bhslr;
71 
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);
81 
82 	return (request);
83 }
84 
85 static void
86 kernel_add(const struct iscsid_connection *conn, const char *target)
87 {
88 	struct iscsi_session_add isa;
89 	int error;
90 
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);
97 	if (error != 0)
98 		log_warn("failed to add %s: ISCSISADD", target);
99 }
100 
101 static void
102 kernel_remove(const struct iscsid_connection *conn)
103 {
104 	struct iscsi_session_remove isr;
105 	int error;
106 
107 	memset(&isr, 0, sizeof(isr));
108 	isr.isr_session_id = conn->conn_session_id;
109 	error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
110 	if (error != 0)
111 		log_warn("ISCSISREMOVE");
112 }
113 
114 void
115 discovery(struct iscsid_connection *conn)
116 {
117 	struct pdu *request, *response;
118 	struct keys *request_keys, *response_keys;
119 	int i;
120 
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);
126 	request_keys = NULL;
127 
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)
132 			break;
133 
134 		if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
135 			continue;
136 
137 		log_debugx("adding target %s", response_keys->keys_values[i]);
138 		/*
139 		 * XXX: Validate the target name?
140 		 */
141 		kernel_add(conn, response_keys->keys_values[i]);
142 	}
143 	keys_delete(response_keys);
144 
145 	log_debugx("removing temporary discovery session");
146 	kernel_remove(conn);
147 
148 #ifdef ICL_KERNEL_PROXY
149 	if (conn->conn_conf.isc_iser == 1) {
150 		/*
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.
154 		 */
155 		log_debugx("discovery session done");
156 		return;
157 	}
158 #endif
159 
160 	log_debugx("discovery done; logging out");
161 	request = logout_new_request(&conn->conn);
162 	pdu_send(request);
163 	pdu_delete(request);
164 	request = NULL;
165 
166 	log_debugx("waiting for Logout Response");
167 	response = logout_receive(&conn->conn);
168 	pdu_delete(response);
169 
170 	log_debugx("discovery session done");
171 }
172