discovery.c revision 262845
1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: stable/10/usr.sbin/iscsid/discovery.c 262845 2014-03-06 11:14:36Z trasz $
30 */
31
32#include <sys/types.h>
33#include <sys/ioctl.h>
34#include <assert.h>
35#include <stdbool.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <netinet/in.h>
40
41#include "iscsid.h"
42#include "iscsi_proto.h"
43
44static struct pdu *
45text_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 %d, should be %d", ntohl(bhstr->bhstr_statsn),
68		    conn->conn_statsn + 1);
69	}
70	conn->conn_statsn = ntohl(bhstr->bhstr_statsn);
71
72	return (response);
73}
74
75static struct pdu *
76text_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
96static struct pdu *
97logout_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 %d, should be %d", ntohl(bhslr->bhslr_statsn),
114		    conn->conn_statsn + 1);
115	}
116	conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
117
118	return (response);
119}
120
121static struct pdu *
122logout_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
140static void
141kernel_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
156static void
157kernel_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
169void
170discovery(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	log_debugx("discovery done; logging out");
211	request = logout_new_request(conn);
212	pdu_send(request);
213	pdu_delete(request);
214	request = NULL;
215
216	log_debugx("waiting for Logout Response");
217	response = logout_receive(conn);
218	pdu_delete(response);
219
220	log_debugx("discovery session done");
221}
222