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, 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, unsigned char *dptr, int dsize)
132 printf("trying to add data to NULL pdu\n");
136 printf("Trying to append zero size data to pdu\n");
140 if (iscsi_add_data(&pdu->outdata, dptr, dsize, 1) != 0) {
141 printf("failed to add data to pdu buffer\n");
145 /* update data segment length */
146 *(uint32_t *)&pdu->outdata.data[4] = htonl(pdu->outdata.size-ISCSI_HEADER_SIZE);
151 int iscsi_get_pdu_size(const unsigned char *hdr)
155 size = (ntohl(*(uint32_t *)&hdr[4])&0x00ffffff) + ISCSI_HEADER_SIZE;
156 size = (size+3)&0xfffffffc;
162 int iscsi_process_pdu(struct iscsi_context *iscsi, const unsigned char *hdr, int size)
165 enum iscsi_opcode opcode;
166 struct iscsi_pdu *pdu;
169 opcode = hdr[0] & 0x3f;
171 itt = ntohl(*(uint32_t *)&hdr[16]);
174 printf("cant handle expanded headers yet\n");
178 for (pdu = iscsi->waitpdu; pdu; pdu = pdu->next) {
179 enum iscsi_opcode expected_response = pdu->response_opcode;
182 if (pdu->itt != itt) {
186 /* we have a special case with scsi-command opcodes, the are replied to by either a scsi-response
187 * or a data-in, or a combination of both.
189 if (opcode == ISCSI_PDU_DATA_IN && expected_response == ISCSI_PDU_SCSI_RESPONSE) {
190 expected_response = ISCSI_PDU_DATA_IN;
193 if (opcode != expected_response) {
194 printf("Got wrong opcode back for itt:%d got:%d expected %d\n", itt, opcode, pdu->response_opcode);
198 case ISCSI_PDU_LOGIN_RESPONSE:
199 if (iscsi_process_login_reply(iscsi, pdu, hdr, size) != 0) {
200 DLIST_REMOVE(iscsi->waitpdu, pdu);
201 iscsi_free_pdu(iscsi, pdu);
202 printf("iscsi login reply failed\n");
206 case ISCSI_PDU_TEXT_RESPONSE:
207 if (iscsi_process_text_reply(iscsi, pdu, hdr, size) != 0) {
208 DLIST_REMOVE(iscsi->waitpdu, pdu);
209 iscsi_free_pdu(iscsi, pdu);
210 printf("iscsi text reply failed\n");
214 case ISCSI_PDU_LOGOUT_RESPONSE:
215 if (iscsi_process_logout_reply(iscsi, pdu, hdr, size) != 0) {
216 DLIST_REMOVE(iscsi->waitpdu, pdu);
217 iscsi_free_pdu(iscsi, pdu);
218 printf("iscsi logout reply failed\n");
222 case ISCSI_PDU_SCSI_RESPONSE:
223 if (iscsi_process_scsi_reply(iscsi, pdu, hdr, size) != 0) {
224 DLIST_REMOVE(iscsi->waitpdu, pdu);
225 iscsi_free_pdu(iscsi, pdu);
226 printf("iscsi response reply failed\n");
230 case ISCSI_PDU_DATA_IN:
231 if (iscsi_process_scsi_data_in(iscsi, pdu, hdr, size, &is_finished) != 0) {
232 DLIST_REMOVE(iscsi->waitpdu, pdu);
233 iscsi_free_pdu(iscsi, pdu);
234 printf("iscsi data in failed\n");
238 case ISCSI_PDU_NOP_IN:
239 if (iscsi_process_nop_out_reply(iscsi, pdu, hdr, size) != 0) {
240 DLIST_REMOVE(iscsi->waitpdu, pdu);
241 iscsi_free_pdu(iscsi, pdu);
242 printf("iscsi nop-in failed\n");
247 printf("Dont know how to handle opcode %d\n", opcode);
252 DLIST_REMOVE(iscsi->waitpdu, pdu);
253 iscsi_free_pdu(iscsi, pdu);
255 printf("pdu is not yet finished, let it remain\n");
263 void iscsi_pdu_set_pduflags(struct iscsi_pdu *pdu, unsigned char flags)
265 pdu->outdata.data[1] = flags;
268 void iscsi_pdu_set_immediate(struct iscsi_pdu *pdu)
270 pdu->outdata.data[0] |= ISCSI_PDU_IMMEDIATE;
273 void iscsi_pdu_set_ttt(struct iscsi_pdu *pdu, uint32_t ttt)
275 *(uint32_t *)&pdu->outdata.data[20] = htonl(ttt);
278 void iscsi_pdu_set_cmdsn(struct iscsi_pdu *pdu, uint32_t cmdsn)
280 *(uint32_t *)&pdu->outdata.data[24] = htonl(cmdsn);
283 void iscsi_pdu_set_expstatsn(struct iscsi_pdu *pdu, uint32_t expstatsnsn)
285 *(uint32_t *)&pdu->outdata.data[28] = htonl(expstatsnsn);
288 void iscsi_pdu_set_cdb(struct iscsi_pdu *pdu, struct scsi_task *task)
290 bzero(&pdu->outdata.data[32], 16);
291 memcpy(&pdu->outdata.data[32], task->cdb, task->cdb_size);
294 void iscsi_pdu_set_lun(struct iscsi_pdu *pdu, uint32_t lun)
296 pdu->outdata.data[9] = lun;
299 void iscsi_pdu_set_expxferlen(struct iscsi_pdu *pdu, uint32_t expxferlen)
301 *(uint32_t *)&pdu->outdata.data[20] = htonl(expxferlen);