1255570Strasz/*-
2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation
3255570Strasz * All rights reserved.
4255570Strasz *
5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6255570Strasz * from the FreeBSD Foundation.
7255570Strasz *
8255570Strasz * Redistribution and use in source and binary forms, with or without
9255570Strasz * modification, are permitted provided that the following conditions
10255570Strasz * are met:
11255570Strasz * 1. Redistributions of source code must retain the above copyright
12255570Strasz *    notice, this list of conditions and the following disclaimer.
13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright
14255570Strasz *    notice, this list of conditions and the following disclaimer in the
15255570Strasz *    documentation and/or other materials provided with the distribution.
16255570Strasz *
17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20255570Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27255570Strasz * SUCH DAMAGE.
28255570Strasz *
29255570Strasz */
30255570Strasz
31270888Strasz#include <sys/cdefs.h>
32270888Strasz__FBSDID("$FreeBSD$");
33270888Strasz
34255570Strasz#include <sys/types.h>
35255570Strasz#include <sys/ioctl.h>
36255570Strasz#include <stdbool.h>
37255570Strasz#include <string.h>
38255570Strasz#include <netinet/in.h>
39255570Strasz
40255570Strasz#include "iscsid.h"
41255570Strasz#include "iscsi_proto.h"
42255570Strasz
43255570Straszstatic struct pdu *
44255570Strasztext_receive(struct connection *conn)
45255570Strasz{
46255570Strasz	struct pdu *response;
47255570Strasz	struct iscsi_bhs_text_response *bhstr;
48255570Strasz
49255570Strasz	response = pdu_new(conn);
50255570Strasz	pdu_receive(response);
51255570Strasz	if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_TEXT_RESPONSE)
52255570Strasz		log_errx(1, "protocol error: received invalid opcode 0x%x",
53255570Strasz		    response->pdu_bhs->bhs_opcode);
54255570Strasz	bhstr = (struct iscsi_bhs_text_response *)response->pdu_bhs;
55255570Strasz#if 0
56255570Strasz	if ((bhstr->bhstr_flags & BHSTR_FLAGS_FINAL) == 0)
57255570Strasz		log_errx(1, "received Text PDU without the \"F\" flag");
58255570Strasz#endif
59255570Strasz	/*
60255570Strasz	 * XXX: Implement the C flag some day.
61255570Strasz	 */
62255570Strasz	if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
63255570Strasz		log_errx(1, "received Text PDU with unsupported \"C\" flag");
64255570Strasz	if (ntohl(bhstr->bhstr_statsn) != conn->conn_statsn + 1) {
65255570Strasz		log_errx(1, "received Text PDU with wrong StatSN: "
66276613Smav		    "is %u, should be %u", ntohl(bhstr->bhstr_statsn),
67255570Strasz		    conn->conn_statsn + 1);
68255570Strasz	}
69255570Strasz	conn->conn_statsn = ntohl(bhstr->bhstr_statsn);
70255570Strasz
71255570Strasz	return (response);
72255570Strasz}
73255570Strasz
74255570Straszstatic struct pdu *
75255570Strasztext_new_request(struct connection *conn)
76255570Strasz{
77255570Strasz	struct pdu *request;
78255570Strasz	struct iscsi_bhs_text_request *bhstr;
79255570Strasz
80255570Strasz	request = pdu_new(conn);
81255570Strasz	bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
82255570Strasz	bhstr->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_REQUEST |
83255570Strasz	    ISCSI_BHS_OPCODE_IMMEDIATE;
84255570Strasz	bhstr->bhstr_flags = BHSTR_FLAGS_FINAL;
85255570Strasz	bhstr->bhstr_initiator_task_tag = 0;
86255570Strasz	bhstr->bhstr_target_transfer_tag = 0xffffffff;
87255570Strasz
88255570Strasz	bhstr->bhstr_initiator_task_tag = 0; /* XXX */
89255570Strasz	bhstr->bhstr_cmdsn = 0; /* XXX */
90255570Strasz	bhstr->bhstr_expstatsn = htonl(conn->conn_statsn + 1);
91255570Strasz
92255570Strasz	return (request);
93255570Strasz}
94255570Strasz
95255570Straszstatic struct pdu *
96255570Straszlogout_receive(struct connection *conn)
97255570Strasz{
98255570Strasz	struct pdu *response;
99255570Strasz	struct iscsi_bhs_logout_response *bhslr;
100255570Strasz
101255570Strasz	response = pdu_new(conn);
102255570Strasz	pdu_receive(response);
103255570Strasz	if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE)
104255570Strasz		log_errx(1, "protocol error: received invalid opcode 0x%x",
105255570Strasz		    response->pdu_bhs->bhs_opcode);
106255570Strasz	bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
107255570Strasz	if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY)
108255570Strasz		log_warnx("received Logout Response with reason %d",
109255570Strasz		    ntohs(bhslr->bhslr_response));
110255570Strasz	if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
111255570Strasz		log_errx(1, "received Logout PDU with wrong StatSN: "
112276613Smav		    "is %u, should be %u", ntohl(bhslr->bhslr_statsn),
113255570Strasz		    conn->conn_statsn + 1);
114255570Strasz	}
115255570Strasz	conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
116255570Strasz
117255570Strasz	return (response);
118255570Strasz}
119255570Strasz
120255570Straszstatic struct pdu *
121255570Straszlogout_new_request(struct connection *conn)
122255570Strasz{
123255570Strasz	struct pdu *request;
124255570Strasz	struct iscsi_bhs_logout_request *bhslr;
125255570Strasz
126255570Strasz	request = pdu_new(conn);
127255570Strasz	bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
128255570Strasz	bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST |
129255570Strasz	    ISCSI_BHS_OPCODE_IMMEDIATE;
130255570Strasz	bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
131255570Strasz	bhslr->bhslr_reason |= 0x80;
132255570Strasz	bhslr->bhslr_initiator_task_tag = 0; /* XXX */
133255570Strasz	bhslr->bhslr_cmdsn = 0; /* XXX */
134255570Strasz	bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
135255570Strasz
136255570Strasz	return (request);
137255570Strasz}
138255570Strasz
139255570Straszstatic void
140255570Straszkernel_add(const struct connection *conn, const char *target)
141255570Strasz{
142255570Strasz	struct iscsi_session_add isa;
143255570Strasz	int error;
144255570Strasz
145255570Strasz	memset(&isa, 0, sizeof(isa));
146256193Strasz	memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf));
147255570Strasz	strlcpy(isa.isa_conf.isc_target, target,
148255570Strasz	    sizeof(isa.isa_conf.isc_target));
149255570Strasz	isa.isa_conf.isc_discovery = 0;
150255570Strasz	error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa);
151255570Strasz	if (error != 0)
152255570Strasz		log_warn("failed to add %s: ISCSISADD", target);
153255570Strasz}
154255570Strasz
155255570Straszstatic void
156255570Straszkernel_remove(const struct connection *conn)
157255570Strasz{
158255570Strasz	struct iscsi_session_remove isr;
159255570Strasz	int error;
160255570Strasz
161255570Strasz	memset(&isr, 0, sizeof(isr));
162255570Strasz	isr.isr_session_id = conn->conn_session_id;
163255570Strasz	error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
164255570Strasz	if (error != 0)
165255570Strasz		log_warn("ISCSISREMOVE");
166255570Strasz}
167255570Strasz
168255570Straszvoid
169255570Straszdiscovery(struct connection *conn)
170255570Strasz{
171255570Strasz	struct pdu *request, *response;
172255570Strasz	struct keys *request_keys, *response_keys;
173255570Strasz	int i;
174255570Strasz
175255570Strasz	log_debugx("beginning discovery session");
176255570Strasz	request = text_new_request(conn);
177255570Strasz	request_keys = keys_new();
178255570Strasz	keys_add(request_keys, "SendTargets", "All");
179255570Strasz	keys_save(request_keys, request);
180255570Strasz	keys_delete(request_keys);
181255570Strasz	request_keys = NULL;
182255570Strasz	pdu_send(request);
183255570Strasz	pdu_delete(request);
184255570Strasz	request = NULL;
185255570Strasz
186255570Strasz	log_debugx("waiting for Text Response");
187255570Strasz	response = text_receive(conn);
188255570Strasz	response_keys = keys_new();
189255570Strasz	keys_load(response_keys, response);
190255570Strasz	for (i = 0; i < KEYS_MAX; i++) {
191255570Strasz		if (response_keys->keys_names[i] == NULL)
192255570Strasz			break;
193255570Strasz
194255570Strasz		if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
195255570Strasz			continue;
196255570Strasz
197255570Strasz		log_debugx("adding target %s", response_keys->keys_values[i]);
198255570Strasz		/*
199255570Strasz		 * XXX: Validate the target name?
200255570Strasz		 */
201255570Strasz		kernel_add(conn, response_keys->keys_values[i]);
202255570Strasz	}
203255570Strasz	keys_delete(response_keys);
204255570Strasz	pdu_delete(response);
205255570Strasz
206255570Strasz	log_debugx("removing temporary discovery session");
207255570Strasz	kernel_remove(conn);
208255570Strasz
209255570Strasz	log_debugx("discovery done; logging out");
210255570Strasz	request = logout_new_request(conn);
211255570Strasz	pdu_send(request);
212256194Strasz	pdu_delete(request);
213255570Strasz	request = NULL;
214255570Strasz
215255570Strasz	log_debugx("waiting for Logout Response");
216255570Strasz	response = logout_receive(conn);
217255570Strasz	pdu_delete(response);
218255570Strasz
219255570Strasz	log_debugx("discovery session done");
220255570Strasz}
221