2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <arpa/inet.h>
24 #include "iscsi-private.h"
25 #include "scsi-lowlevel.h"
26 #include "dlinklist.h"
28 struct iscsi_pdu *iscsi_allocate_pdu(struct iscsi_context *iscsi, enum iscsi_opcode opcode, enum iscsi_opcode response_opcode)
30 struct iscsi_pdu *pdu;
32 pdu = malloc(sizeof(struct iscsi_pdu));
34 printf("failed to allocate pdu\n");
37 bzero(pdu, sizeof(struct iscsi_pdu));
39 pdu->outdata.size = ISCSI_HEADER_SIZE;
40 pdu->outdata.data = malloc(pdu->outdata.size);
42 if (pdu->outdata.data == NULL) {
43 printf("failed to allocate pdu header\n");
48 bzero(pdu->outdata.data, pdu->outdata.size);
51 pdu->outdata.data[0] = opcode;
52 pdu->response_opcode = response_opcode;
55 if (opcode ==ISCSI_PDU_LOGIN_REQUEST) {
56 memcpy(&pdu->outdata.data[8], &iscsi->isid[0], 6);
60 *(uint32_t *)&pdu->outdata.data[16] = htonl(iscsi->itt);
61 pdu->itt = iscsi->itt;
68 void iscsi_free_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
71 printf("trying to free NULL pdu\n");
74 if (pdu->outdata.data) {
75 free(pdu->outdata.data);
76 pdu->outdata.data = NULL;
78 if (pdu->indata.data) {
79 free(pdu->indata.data);
80 pdu->indata.data = NULL;
82 if (pdu->scsi_cbdata) {
83 iscsi_free_scsi_cbdata(pdu->scsi_cbdata);
84 pdu->scsi_cbdata = NULL;
91 int iscsi_add_data(struct iscsi_data *data, const unsigned char *dptr, int dsize, int pdualignment)
97 printf("Trying to append zero size data to iscsi_data\n");
101 len = data->size + dsize;
104 aligned = (aligned+3)&0xfffffffc;
106 buf = malloc(aligned);
108 printf("failed to allocate buffer for %d bytes\n", len);
112 memcpy(buf, data->data, data->size);
113 memcpy(buf + data->size, dptr, dsize);
114 if (len != aligned) {
115 /* zero out any padding at the end */
116 bzero(buf+len, aligned-len);
126 int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *dptr, int dsize)
129 printf("trying to add data to NULL pdu\n");
133 printf("Trying to append zero size data to pdu\n");
137 if (iscsi_add_data(&pdu->outdata, dptr, dsize, 1) != 0) {
138 printf("failed to add data to pdu buffer\n");
142 /* update data segment length */
143 *(uint32_t *)&pdu->outdata.data[4] = htonl(pdu->outdata.size-ISCSI_HEADER_SIZE);
148 int iscsi_get_pdu_size(const unsigned char *hdr)
152 size = (ntohl(*(uint32_t *)&hdr[4])&0x00ffffff) + ISCSI_HEADER_SIZE;
153 size = (size+3)&0xfffffffc;
159 int iscsi_process_pdu(struct iscsi_context *iscsi, const unsigned char *hdr, int size)
162 enum iscsi_opcode opcode;
163 struct iscsi_pdu *pdu;
166 opcode = hdr[0] & 0x3f;
168 itt = ntohl(*(uint32_t *)&hdr[16]);
171 printf("cant handle expanded headers yet\n");
175 for (pdu = iscsi->waitpdu; pdu; pdu = pdu->next) {
176 enum iscsi_opcode expected_response = pdu->response_opcode;
179 if (pdu->itt != itt) {
183 /* we have a special case with scsi-command opcodes, the are replied to by either a scsi-response
184 * or a data-in, or a combination of both.
186 if (opcode == ISCSI_PDU_DATA_IN && expected_response == ISCSI_PDU_SCSI_RESPONSE) {
187 expected_response = ISCSI_PDU_DATA_IN;
190 if (opcode != expected_response) {
191 printf("Got wrong opcode back for itt:%d got:%d expected %d\n", itt, opcode, pdu->response_opcode);
195 case ISCSI_PDU_LOGIN_RESPONSE:
196 if (iscsi_process_login_reply(iscsi, pdu, hdr, size) != 0) {
197 DLIST_REMOVE(iscsi->waitpdu, pdu);
198 iscsi_free_pdu(iscsi, pdu);
199 printf("iscsi login reply failed\n");
203 case ISCSI_PDU_TEXT_RESPONSE:
204 if (iscsi_process_text_reply(iscsi, pdu, hdr, size) != 0) {
205 DLIST_REMOVE(iscsi->waitpdu, pdu);
206 iscsi_free_pdu(iscsi, pdu);
207 printf("iscsi text reply failed\n");
211 case ISCSI_PDU_LOGOUT_RESPONSE:
212 if (iscsi_process_logout_reply(iscsi, pdu, hdr, size) != 0) {
213 DLIST_REMOVE(iscsi->waitpdu, pdu);
214 iscsi_free_pdu(iscsi, pdu);
215 printf("iscsi logout reply failed\n");
219 case ISCSI_PDU_SCSI_RESPONSE:
220 if (iscsi_process_scsi_reply(iscsi, pdu, hdr, size) != 0) {
221 DLIST_REMOVE(iscsi->waitpdu, pdu);
222 iscsi_free_pdu(iscsi, pdu);
223 printf("iscsi response reply failed\n");
227 case ISCSI_PDU_DATA_IN:
228 if (iscsi_process_scsi_data_in(iscsi, pdu, hdr, size, &is_finished) != 0) {
229 DLIST_REMOVE(iscsi->waitpdu, pdu);
230 iscsi_free_pdu(iscsi, pdu);
231 printf("iscsi data in failed\n");
235 case ISCSI_PDU_NOP_IN:
236 if (iscsi_process_nop_out_reply(iscsi, pdu, hdr, size) != 0) {
237 DLIST_REMOVE(iscsi->waitpdu, pdu);
238 iscsi_free_pdu(iscsi, pdu);
239 printf("iscsi nop-in failed\n");
244 printf("Don't know how to handle opcode %d\n", opcode);
249 DLIST_REMOVE(iscsi->waitpdu, pdu);
250 iscsi_free_pdu(iscsi, pdu);
252 printf("pdu is not yet finished, let it remain\n");
260 void iscsi_pdu_set_pduflags(struct iscsi_pdu *pdu, unsigned char flags)
262 pdu->outdata.data[1] = flags;
265 void iscsi_pdu_set_immediate(struct iscsi_pdu *pdu)
267 pdu->outdata.data[0] |= ISCSI_PDU_IMMEDIATE;
270 void iscsi_pdu_set_ttt(struct iscsi_pdu *pdu, uint32_t ttt)
272 *(uint32_t *)&pdu->outdata.data[20] = htonl(ttt);
275 void iscsi_pdu_set_cmdsn(struct iscsi_pdu *pdu, uint32_t cmdsn)
277 *(uint32_t *)&pdu->outdata.data[24] = htonl(cmdsn);
280 void iscsi_pdu_set_expstatsn(struct iscsi_pdu *pdu, uint32_t expstatsnsn)
282 *(uint32_t *)&pdu->outdata.data[28] = htonl(expstatsnsn);
285 void iscsi_pdu_set_cdb(struct iscsi_pdu *pdu, struct scsi_task *task)
287 bzero(&pdu->outdata.data[32], 16);
288 memcpy(&pdu->outdata.data[32], task->cdb, task->cdb_size);
291 void iscsi_pdu_set_lun(struct iscsi_pdu *pdu, uint32_t lun)
293 pdu->outdata.data[9] = lun;
296 void iscsi_pdu_set_expxferlen(struct iscsi_pdu *pdu, uint32_t expxferlen)
298 *(uint32_t *)&pdu->outdata.data[20] = htonl(expxferlen);