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