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$");
39 #include <netinet/in.h>
41 #include <sys/socket.h>
44 #include "iscsi_proto.h"
47 text_receive(struct connection *conn)
50 struct iscsi_bhs_text_request *bhstr;
52 request = pdu_new(conn);
54 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
55 ISCSI_BHS_OPCODE_TEXT_REQUEST)
56 log_errx(1, "protocol error: received invalid opcode 0x%x",
57 request->pdu_bhs->bhs_opcode);
58 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
60 if ((bhstr->bhstr_flags & ISCSI_BHSTR_FLAGS_FINAL) == 0)
61 log_errx(1, "received Text PDU without the \"F\" flag");
64 * XXX: Implement the C flag some day.
66 if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
67 log_errx(1, "received Text PDU with unsupported \"C\" flag");
68 if (ntohl(bhstr->bhstr_cmdsn) < conn->conn_cmdsn) {
69 log_errx(1, "received Text PDU with decreasing CmdSN: "
70 "was %d, is %d", conn->conn_cmdsn, ntohl(bhstr->bhstr_cmdsn));
72 if (ntohl(bhstr->bhstr_expstatsn) != conn->conn_statsn) {
73 log_errx(1, "received Text PDU with wrong StatSN: "
74 "is %d, should be %d", ntohl(bhstr->bhstr_expstatsn),
77 conn->conn_cmdsn = ntohl(bhstr->bhstr_cmdsn);
83 text_new_response(struct pdu *request)
86 struct connection *conn;
87 struct iscsi_bhs_text_request *bhstr;
88 struct iscsi_bhs_text_response *bhstr2;
90 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
91 conn = request->pdu_connection;
93 response = pdu_new_response(request);
94 bhstr2 = (struct iscsi_bhs_text_response *)response->pdu_bhs;
95 bhstr2->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_RESPONSE;
96 bhstr2->bhstr_flags = BHSTR_FLAGS_FINAL;
97 bhstr2->bhstr_lun = bhstr->bhstr_lun;
98 bhstr2->bhstr_initiator_task_tag = bhstr->bhstr_initiator_task_tag;
99 bhstr2->bhstr_target_transfer_tag = bhstr->bhstr_target_transfer_tag;
100 bhstr2->bhstr_statsn = htonl(conn->conn_statsn++);
101 bhstr2->bhstr_expcmdsn = htonl(conn->conn_cmdsn);
102 bhstr2->bhstr_maxcmdsn = htonl(conn->conn_cmdsn);
108 logout_receive(struct connection *conn)
111 struct iscsi_bhs_logout_request *bhslr;
113 request = pdu_new(conn);
114 pdu_receive(request);
115 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
116 ISCSI_BHS_OPCODE_LOGOUT_REQUEST)
117 log_errx(1, "protocol error: received invalid opcode 0x%x",
118 request->pdu_bhs->bhs_opcode);
119 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
120 if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION)
121 log_debugx("received Logout PDU with invalid reason 0x%x; "
122 "continuing anyway", bhslr->bhslr_reason & 0x7f);
123 if (ntohl(bhslr->bhslr_cmdsn) < conn->conn_cmdsn) {
124 log_errx(1, "received Logout PDU with decreasing CmdSN: "
125 "was %d, is %d", conn->conn_cmdsn,
126 ntohl(bhslr->bhslr_cmdsn));
128 if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
129 log_errx(1, "received Logout PDU with wrong StatSN: "
130 "is %d, should be %d", ntohl(bhslr->bhslr_expstatsn),
133 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
139 logout_new_response(struct pdu *request)
141 struct pdu *response;
142 struct connection *conn;
143 struct iscsi_bhs_logout_request *bhslr;
144 struct iscsi_bhs_logout_response *bhslr2;
146 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
147 conn = request->pdu_connection;
149 response = pdu_new_response(request);
150 bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
151 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
152 bhslr2->bhslr_flags = 0x80;
153 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
154 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
155 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
156 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
157 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
163 discovery_add_target(struct keys *response_keys, struct target *targ)
165 struct portal *portal;
167 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
171 keys_add(response_keys, "TargetName", targ->t_name);
172 TAILQ_FOREACH(portal, &targ->t_portal_group->pg_portals, p_next) {
174 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
175 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
176 NI_NUMERICHOST | NI_NUMERICSERV);
178 log_warnx("getnameinfo: %s", gai_strerror(ret));
181 switch (ai->ai_addr->sa_family) {
183 if (strcmp(hbuf, "0.0.0.0") == 0)
185 ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
186 targ->t_portal_group->pg_tag);
189 if (strcmp(hbuf, "::") == 0)
191 ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
192 targ->t_portal_group->pg_tag);
198 log_err(1, "asprintf");
199 keys_add(response_keys, "TargetAddress", buf);
205 discovery(struct connection *conn)
207 struct pdu *request, *response;
208 struct keys *request_keys, *response_keys;
210 const char *send_targets;
212 log_debugx("beginning discovery session; waiting for Text PDU");
213 request = text_receive(conn);
214 request_keys = keys_new();
215 keys_load(request_keys, request);
217 send_targets = keys_find(request_keys, "SendTargets");
218 if (send_targets == NULL)
219 log_errx(1, "received Text PDU without SendTargets");
221 response = text_new_response(request);
222 response_keys = keys_new();
224 if (strcmp(send_targets, "All") == 0) {
226 &conn->conn_portal->p_portal_group->pg_conf->conf_targets,
228 if (targ->t_portal_group !=
229 conn->conn_portal->p_portal_group) {
230 log_debugx("not returning target \"%s\"; "
231 "belongs to a different portal group",
235 discovery_add_target(response_keys, targ);
238 targ = target_find(conn->conn_portal->p_portal_group->pg_conf,
241 log_debugx("initiator requested information on unknown "
242 "target \"%s\"; returning nothing", send_targets);
244 discovery_add_target(response_keys, targ);
246 keys_save(response_keys, response);
249 pdu_delete(response);
250 keys_delete(response_keys);
252 keys_delete(request_keys);
254 log_debugx("done sending targets; waiting for Logout PDU");
255 request = logout_receive(conn);
256 response = logout_new_response(request);
259 pdu_delete(response);
262 log_debugx("discovery session done");