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
37 #include <netinet/in.h>
40 #include "iscsi_proto.h"
43 text_receive(struct connection *conn)
46 struct iscsi_bhs_text_request *bhstr;
48 request = pdu_new(conn);
50 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
51 ISCSI_BHS_OPCODE_TEXT_REQUEST)
52 log_errx(1, "protocol error: received invalid opcode 0x%x",
53 request->pdu_bhs->bhs_opcode);
54 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
56 if ((bhstr->bhstr_flags & ISCSI_BHSTR_FLAGS_FINAL) == 0)
57 log_errx(1, "received Text PDU without the \"F\" flag");
60 * XXX: Implement the C flag some day.
62 if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
63 log_errx(1, "received Text PDU with unsupported \"C\" flag");
64 if (request->pdu_data_len == 0)
65 log_errx(1, "received Text PDU with empty data segment");
67 if (ntohl(bhstr->bhstr_cmdsn) < conn->conn_cmdsn) {
68 log_errx(1, "received Text PDU with decreasing CmdSN: "
69 "was %d, is %d", conn->conn_cmdsn, ntohl(bhstr->bhstr_cmdsn));
71 if (ntohl(bhstr->bhstr_expstatsn) != conn->conn_statsn) {
72 log_errx(1, "received Text PDU with wrong StatSN: "
73 "is %d, should be %d", ntohl(bhstr->bhstr_expstatsn),
76 conn->conn_cmdsn = ntohl(bhstr->bhstr_cmdsn);
82 text_new_response(struct pdu *request)
85 struct connection *conn;
86 struct iscsi_bhs_text_request *bhstr;
87 struct iscsi_bhs_text_response *bhstr2;
89 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
90 conn = request->pdu_connection;
92 response = pdu_new_response(request);
93 bhstr2 = (struct iscsi_bhs_text_response *)response->pdu_bhs;
94 bhstr2->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_RESPONSE;
95 bhstr2->bhstr_flags = BHSTR_FLAGS_FINAL;
96 bhstr2->bhstr_lun = bhstr->bhstr_lun;
97 bhstr2->bhstr_initiator_task_tag = bhstr->bhstr_initiator_task_tag;
98 bhstr2->bhstr_target_transfer_tag = bhstr->bhstr_target_transfer_tag;
99 bhstr2->bhstr_statsn = htonl(conn->conn_statsn++);
100 bhstr2->bhstr_expcmdsn = htonl(conn->conn_cmdsn);
101 bhstr2->bhstr_maxcmdsn = htonl(conn->conn_cmdsn);
107 logout_receive(struct connection *conn)
110 struct iscsi_bhs_logout_request *bhslr;
112 request = pdu_new(conn);
113 pdu_receive(request);
114 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
115 ISCSI_BHS_OPCODE_LOGOUT_REQUEST)
116 log_errx(1, "protocol error: received invalid opcode 0x%x",
117 request->pdu_bhs->bhs_opcode);
118 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
119 if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION)
120 log_debugx("received Logout PDU with invalid reason 0x%x; "
121 "continuing anyway", bhslr->bhslr_reason & 0x7f);
122 if (ntohl(bhslr->bhslr_cmdsn) < conn->conn_cmdsn) {
123 log_errx(1, "received Logout PDU with decreasing CmdSN: "
124 "was %d, is %d", conn->conn_cmdsn,
125 ntohl(bhslr->bhslr_cmdsn));
127 if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
128 log_errx(1, "received Logout PDU with wrong StatSN: "
129 "is %d, should be %d", ntohl(bhslr->bhslr_expstatsn),
132 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
138 logout_new_response(struct pdu *request)
140 struct pdu *response;
141 struct connection *conn;
142 struct iscsi_bhs_logout_request *bhslr;
143 struct iscsi_bhs_logout_response *bhslr2;
145 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
146 conn = request->pdu_connection;
148 response = pdu_new_response(request);
149 bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
150 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
151 bhslr2->bhslr_flags = 0x80;
152 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
153 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
154 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
155 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
156 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
162 discovery(struct connection *conn)
164 struct pdu *request, *response;
165 struct keys *request_keys, *response_keys;
167 const char *send_targets;
169 log_debugx("beginning discovery session; waiting for Text PDU");
170 request = text_receive(conn);
171 request_keys = keys_new();
172 keys_load(request_keys, request);
174 send_targets = keys_find(request_keys, "SendTargets");
175 if (send_targets == NULL)
176 log_errx(1, "received Text PDU without SendTargets");
178 response = text_new_response(request);
179 response_keys = keys_new();
181 if (strcmp(send_targets, "All") == 0) {
183 &conn->conn_portal->p_portal_group->pg_conf->conf_targets,
185 if (targ->t_portal_group !=
186 conn->conn_portal->p_portal_group) {
187 log_debugx("not returning target \"%s\"; "
188 "belongs to a different portal group",
192 keys_add(response_keys, "TargetName", targ->t_iqn);
195 targ = target_find(conn->conn_portal->p_portal_group->pg_conf,
198 log_debugx("initiator requested information on unknown "
199 "target \"%s\"; returning nothing", send_targets);
201 keys_add(response_keys, "TargetName", targ->t_iqn);
204 keys_save(response_keys, response);
207 pdu_delete(response);
208 keys_delete(response_keys);
210 keys_delete(request_keys);
212 log_debugx("done sending targets; waiting for Logout PDU");
213 request = logout_receive(conn);
214 response = logout_new_response(request);
217 pdu_delete(response);
220 log_debugx("discovery session done");