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 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/types.h>
35#include <sys/ioctl.h>
36#include <stdbool.h>
37#include <string.h>
38#include <netinet/in.h>
39
40#include "iscsid.h"
41#include "iscsi_proto.h"
42
43static struct pdu *
44text_receive(struct connection *conn)
45{
46	struct pdu *response;
47	struct iscsi_bhs_text_response *bhstr;
48
49	response = pdu_new(conn);
50	pdu_receive(response);
51	if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_TEXT_RESPONSE)
52		log_errx(1, "protocol error: received invalid opcode 0x%x",
53		    response->pdu_bhs->bhs_opcode);
54	bhstr = (struct iscsi_bhs_text_response *)response->pdu_bhs;
55#if 0
56	if ((bhstr->bhstr_flags & BHSTR_FLAGS_FINAL) == 0)
57		log_errx(1, "received Text PDU without the \"F\" flag");
58#endif
59	/*
60	 * XXX: Implement the C flag some day.
61	 */
62	if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
63		log_errx(1, "received Text PDU with unsupported \"C\" flag");
64	if (ntohl(bhstr->bhstr_statsn) != conn->conn_statsn + 1) {
65		log_errx(1, "received Text PDU with wrong StatSN: "
66		    "is %u, should be %u", ntohl(bhstr->bhstr_statsn),
67		    conn->conn_statsn + 1);
68	}
69	conn->conn_statsn = ntohl(bhstr->bhstr_statsn);
70
71	return (response);
72}
73
74static struct pdu *
75text_new_request(struct connection *conn)
76{
77	struct pdu *request;
78	struct iscsi_bhs_text_request *bhstr;
79
80	request = pdu_new(conn);
81	bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
82	bhstr->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_REQUEST |
83	    ISCSI_BHS_OPCODE_IMMEDIATE;
84	bhstr->bhstr_flags = BHSTR_FLAGS_FINAL;
85	bhstr->bhstr_initiator_task_tag = 0;
86	bhstr->bhstr_target_transfer_tag = 0xffffffff;
87
88	bhstr->bhstr_initiator_task_tag = 0; /* XXX */
89	bhstr->bhstr_cmdsn = 0; /* XXX */
90	bhstr->bhstr_expstatsn = htonl(conn->conn_statsn + 1);
91
92	return (request);
93}
94
95static struct pdu *
96logout_receive(struct connection *conn)
97{
98	struct pdu *response;
99	struct iscsi_bhs_logout_response *bhslr;
100
101	response = pdu_new(conn);
102	pdu_receive(response);
103	if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE)
104		log_errx(1, "protocol error: received invalid opcode 0x%x",
105		    response->pdu_bhs->bhs_opcode);
106	bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
107	if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY)
108		log_warnx("received Logout Response with reason %d",
109		    ntohs(bhslr->bhslr_response));
110	if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
111		log_errx(1, "received Logout PDU with wrong StatSN: "
112		    "is %u, should be %u", ntohl(bhslr->bhslr_statsn),
113		    conn->conn_statsn + 1);
114	}
115	conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
116
117	return (response);
118}
119
120static struct pdu *
121logout_new_request(struct connection *conn)
122{
123	struct pdu *request;
124	struct iscsi_bhs_logout_request *bhslr;
125
126	request = pdu_new(conn);
127	bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
128	bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST |
129	    ISCSI_BHS_OPCODE_IMMEDIATE;
130	bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
131	bhslr->bhslr_reason |= 0x80;
132	bhslr->bhslr_initiator_task_tag = 0; /* XXX */
133	bhslr->bhslr_cmdsn = 0; /* XXX */
134	bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
135
136	return (request);
137}
138
139static void
140kernel_add(const struct connection *conn, const char *target)
141{
142	struct iscsi_session_add isa;
143	int error;
144
145	memset(&isa, 0, sizeof(isa));
146	memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf));
147	strlcpy(isa.isa_conf.isc_target, target,
148	    sizeof(isa.isa_conf.isc_target));
149	isa.isa_conf.isc_discovery = 0;
150	error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa);
151	if (error != 0)
152		log_warn("failed to add %s: ISCSISADD", target);
153}
154
155static void
156kernel_remove(const struct connection *conn)
157{
158	struct iscsi_session_remove isr;
159	int error;
160
161	memset(&isr, 0, sizeof(isr));
162	isr.isr_session_id = conn->conn_session_id;
163	error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
164	if (error != 0)
165		log_warn("ISCSISREMOVE");
166}
167
168void
169discovery(struct connection *conn)
170{
171	struct pdu *request, *response;
172	struct keys *request_keys, *response_keys;
173	int i;
174
175	log_debugx("beginning discovery session");
176	request = text_new_request(conn);
177	request_keys = keys_new();
178	keys_add(request_keys, "SendTargets", "All");
179	keys_save(request_keys, request);
180	keys_delete(request_keys);
181	request_keys = NULL;
182	pdu_send(request);
183	pdu_delete(request);
184	request = NULL;
185
186	log_debugx("waiting for Text Response");
187	response = text_receive(conn);
188	response_keys = keys_new();
189	keys_load(response_keys, response);
190	for (i = 0; i < KEYS_MAX; i++) {
191		if (response_keys->keys_names[i] == NULL)
192			break;
193
194		if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
195			continue;
196
197		log_debugx("adding target %s", response_keys->keys_values[i]);
198		/*
199		 * XXX: Validate the target name?
200		 */
201		kernel_add(conn, response_keys->keys_values[i]);
202	}
203	keys_delete(response_keys);
204	pdu_delete(response);
205
206	log_debugx("removing temporary discovery session");
207	kernel_remove(conn);
208
209	log_debugx("discovery done; logging out");
210	request = logout_new_request(conn);
211	pdu_send(request);
212	pdu_delete(request);
213	request = NULL;
214
215	log_debugx("waiting for Logout Response");
216	response = logout_receive(conn);
217	pdu_delete(response);
218
219	log_debugx("discovery session done");
220}
221