2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
41 #include <netinet/in.h>
44 #include "iscsi_proto.h"
47 text_receive(struct connection *conn)
50 struct iscsi_bhs_text_response *bhstr;
52 response = pdu_new(conn);
53 pdu_receive(response);
54 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_TEXT_RESPONSE)
55 log_errx(1, "protocol error: received invalid opcode 0x%x",
56 response->pdu_bhs->bhs_opcode);
57 bhstr = (struct iscsi_bhs_text_response *)response->pdu_bhs;
59 if ((bhstr->bhstr_flags & BHSTR_FLAGS_FINAL) == 0)
60 log_errx(1, "received Text PDU without the \"F\" flag");
63 * XXX: Implement the C flag some day.
65 if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
66 log_errx(1, "received Text PDU with unsupported \"C\" flag");
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);
72 conn->conn_statsn = ntohl(bhstr->bhstr_statsn);
78 text_new_request(struct connection *conn)
81 struct iscsi_bhs_text_request *bhstr;
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;
91 bhstr->bhstr_initiator_task_tag = 0; /* XXX */
92 bhstr->bhstr_cmdsn = 0; /* XXX */
93 bhstr->bhstr_expstatsn = htonl(conn->conn_statsn + 1);
99 logout_receive(struct connection *conn)
101 struct pdu *response;
102 struct iscsi_bhs_logout_response *bhslr;
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);
118 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
124 logout_new_request(struct connection *conn)
127 struct iscsi_bhs_logout_request *bhslr;
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);
143 kernel_add(const struct connection *conn, const char *target)
145 struct iscsi_session_add isa;
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);
155 log_warn("failed to add %s: ISCSISADD", target);
159 kernel_remove(const struct connection *conn)
161 struct iscsi_session_remove isr;
164 memset(&isr, 0, sizeof(isr));
165 isr.isr_session_id = conn->conn_session_id;
166 error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
168 log_warn("ISCSISREMOVE");
172 discovery(struct connection *conn)
174 struct pdu *request, *response;
175 struct keys *request_keys, *response_keys;
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);
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)
197 if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
200 log_debugx("adding target %s", response_keys->keys_values[i]);
202 * XXX: Validate the target name?
204 kernel_add(conn, response_keys->keys_values[i]);
206 keys_delete(response_keys);
207 pdu_delete(response);
209 log_debugx("removing temporary discovery session");
212 log_debugx("discovery done; logging out");
213 request = logout_new_request(conn);
218 log_debugx("waiting for Logout Response");
219 response = logout_receive(conn);
220 pdu_delete(response);
222 log_debugx("discovery session done");