pdu.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <strings.h>
  17. #include <string.h>
  18. #include <arpa/inet.h>
  19. #include "iscsi.h"
  20. #include "iscsi-private.h"
  21. #include "scsi-lowlevel.h"
  22. #include "dlinklist.h"
  23. struct iscsi_pdu *iscsi_allocate_pdu(struct iscsi_context *iscsi, enum iscsi_opcode opcode, enum iscsi_opcode response_opcode)
  24. {
  25. struct iscsi_pdu *pdu;
  26. pdu = malloc(sizeof(struct iscsi_pdu));
  27. if (pdu == NULL) {
  28. printf("failed to allocate pdu\n");
  29. return NULL;
  30. }
  31. bzero(pdu, sizeof(struct iscsi_pdu));
  32. pdu->outdata.size = ISCSI_HEADER_SIZE;
  33. pdu->outdata.data = malloc(pdu->outdata.size);
  34. if (pdu->outdata.data == NULL) {
  35. printf("failed to allocate pdu header\n");
  36. free(pdu);
  37. pdu = NULL;
  38. return NULL;
  39. }
  40. bzero(pdu->outdata.data, pdu->outdata.size);
  41. /* opcode */
  42. pdu->outdata.data[0] = opcode;
  43. pdu->response_opcode = response_opcode;
  44. /* isid */
  45. if (opcode ==ISCSI_PDU_LOGIN_REQUEST) {
  46. memcpy(&pdu->outdata.data[8], &iscsi->isid[0], 6);
  47. }
  48. /* itt */
  49. *(uint32_t *)&pdu->outdata.data[16] = htonl(iscsi->itt);
  50. pdu->itt = iscsi->itt;
  51. iscsi->itt++;
  52. return pdu;
  53. }
  54. void iscsi_free_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
  55. {
  56. if (pdu == NULL) {
  57. printf("trying to free NULL pdu\n");
  58. return;
  59. }
  60. if (pdu->outdata.data) {
  61. free(pdu->outdata.data);
  62. pdu->outdata.data = NULL;
  63. }
  64. if (pdu->indata.data) {
  65. free(pdu->indata.data);
  66. pdu->indata.data = NULL;
  67. }
  68. if (pdu->scsi_cbdata) {
  69. iscsi_free_scsi_cbdata(pdu->scsi_cbdata);
  70. pdu->scsi_cbdata = NULL;
  71. }
  72. free(pdu);
  73. }
  74. int iscsi_add_data(struct iscsi_data *data, unsigned char *dptr, int dsize, int pdualignment)
  75. {
  76. int len, aligned;
  77. unsigned char *buf;
  78. if (dsize == 0) {
  79. printf("Trying to append zero size data to iscsi_data\n");
  80. return -1;
  81. }
  82. len = data->size + dsize;
  83. aligned = len;
  84. if (pdualignment) {
  85. aligned = (aligned+3)&0xfffffffc;
  86. }
  87. buf = malloc(aligned);
  88. if (buf == NULL) {
  89. printf("failed to allocate buffer for %d bytes\n", len);
  90. return -2;
  91. }
  92. memcpy(buf, data->data, data->size);
  93. memcpy(buf + data->size, dptr, dsize);
  94. if (len != aligned) {
  95. /* zero out any padding at the end */
  96. bzero(buf+len, aligned-len);
  97. }
  98. free(data->data);
  99. data->data = buf;
  100. data->size = len;
  101. return 0;
  102. }
  103. int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, unsigned char *dptr, int dsize)
  104. {
  105. if (pdu == NULL) {
  106. printf("trying to add data to NULL pdu\n");
  107. return -1;
  108. }
  109. if (dsize == 0) {
  110. printf("Trying to append zero size data to pdu\n");
  111. return -2;
  112. }
  113. if (iscsi_add_data(&pdu->outdata, dptr, dsize, 1) != 0) {
  114. printf("failed to add data to pdu buffer\n");
  115. return -3;
  116. }
  117. /* update data segment length */
  118. *(uint32_t *)&pdu->outdata.data[4] = htonl(pdu->outdata.size-ISCSI_HEADER_SIZE);
  119. return 0;
  120. }
  121. int iscsi_get_pdu_size(const unsigned char *hdr)
  122. {
  123. int size;
  124. size = (ntohl(*(uint32_t *)&hdr[4])&0x00ffffff) + ISCSI_HEADER_SIZE;
  125. size = (size+3)&0xfffffffc;
  126. return size;
  127. }
  128. int iscsi_process_pdu(struct iscsi_context *iscsi, const unsigned char *hdr, int size)
  129. {
  130. uint32_t itt;
  131. enum iscsi_opcode opcode;
  132. struct iscsi_pdu *pdu;
  133. uint8_t ahslen;
  134. opcode = hdr[0] & 0x3f;
  135. ahslen = hdr[4];
  136. itt = ntohl(*(uint32_t *)&hdr[16]);
  137. if (ahslen != 0) {
  138. printf("cant handle expanded headers yet\n");
  139. return -1;
  140. }
  141. for (pdu = iscsi->waitpdu; pdu; pdu = pdu->next) {
  142. enum iscsi_opcode expected_response = pdu->response_opcode;
  143. int is_finished = 1;
  144. if (pdu->itt != itt) {
  145. continue;
  146. }
  147. /* we have a special case with scsi-command opcodes, the are replied to by either a scsi-response
  148. * or a data-in, or a combination of both.
  149. */
  150. if (opcode == ISCSI_PDU_DATA_IN && expected_response == ISCSI_PDU_SCSI_RESPONSE) {
  151. expected_response = ISCSI_PDU_DATA_IN;
  152. }
  153. if (opcode != expected_response) {
  154. printf("Got wrong opcode back for itt:%d got:%d expected %d\n", itt, opcode, pdu->response_opcode);
  155. return -1;
  156. }
  157. switch (opcode) {
  158. case ISCSI_PDU_LOGIN_RESPONSE:
  159. if (iscsi_process_login_reply(iscsi, pdu, hdr, size) != 0) {
  160. DLIST_REMOVE(iscsi->waitpdu, pdu);
  161. iscsi_free_pdu(iscsi, pdu);
  162. printf("iscsi login reply failed\n");
  163. return -2;
  164. }
  165. break;
  166. case ISCSI_PDU_TEXT_RESPONSE:
  167. if (iscsi_process_text_reply(iscsi, pdu, hdr, size) != 0) {
  168. DLIST_REMOVE(iscsi->waitpdu, pdu);
  169. iscsi_free_pdu(iscsi, pdu);
  170. printf("iscsi text reply failed\n");
  171. return -2;
  172. }
  173. break;
  174. case ISCSI_PDU_LOGOUT_RESPONSE:
  175. if (iscsi_process_logout_reply(iscsi, pdu, hdr, size) != 0) {
  176. DLIST_REMOVE(iscsi->waitpdu, pdu);
  177. iscsi_free_pdu(iscsi, pdu);
  178. printf("iscsi logout reply failed\n");
  179. return -3;
  180. }
  181. break;
  182. case ISCSI_PDU_SCSI_RESPONSE:
  183. if (iscsi_process_scsi_reply(iscsi, pdu, hdr, size) != 0) {
  184. DLIST_REMOVE(iscsi->waitpdu, pdu);
  185. iscsi_free_pdu(iscsi, pdu);
  186. printf("iscsi response reply failed\n");
  187. return -4;
  188. }
  189. break;
  190. case ISCSI_PDU_DATA_IN:
  191. if (iscsi_process_scsi_data_in(iscsi, pdu, hdr, size, &is_finished) != 0) {
  192. DLIST_REMOVE(iscsi->waitpdu, pdu);
  193. iscsi_free_pdu(iscsi, pdu);
  194. printf("iscsi data in failed\n");
  195. return -4;
  196. }
  197. break;
  198. case ISCSI_PDU_NOP_IN:
  199. if (iscsi_process_nop_out_reply(iscsi, pdu, hdr, size) != 0) {
  200. DLIST_REMOVE(iscsi->waitpdu, pdu);
  201. iscsi_free_pdu(iscsi, pdu);
  202. printf("iscsi nop-in failed\n");
  203. return -5;
  204. }
  205. break;
  206. default:
  207. printf("Dont know how to handle opcode %d\n", opcode);
  208. return -2;
  209. }
  210. if (is_finished) {
  211. DLIST_REMOVE(iscsi->waitpdu, pdu);
  212. iscsi_free_pdu(iscsi, pdu);
  213. } else {
  214. printf("pdu is not yet finished, let it remain\n");
  215. }
  216. return 0;
  217. }
  218. return 0;
  219. }
  220. void iscsi_pdu_set_pduflags(struct iscsi_pdu *pdu, unsigned char flags)
  221. {
  222. pdu->outdata.data[1] = flags;
  223. }
  224. void iscsi_pdu_set_immediate(struct iscsi_pdu *pdu)
  225. {
  226. pdu->outdata.data[0] |= ISCSI_PDU_IMMEDIATE;
  227. }
  228. void iscsi_pdu_set_ttt(struct iscsi_pdu *pdu, uint32_t ttt)
  229. {
  230. *(uint32_t *)&pdu->outdata.data[20] = htonl(ttt);
  231. }
  232. void iscsi_pdu_set_cmdsn(struct iscsi_pdu *pdu, uint32_t cmdsn)
  233. {
  234. *(uint32_t *)&pdu->outdata.data[24] = htonl(cmdsn);
  235. }
  236. void iscsi_pdu_set_expstatsn(struct iscsi_pdu *pdu, uint32_t expstatsnsn)
  237. {
  238. *(uint32_t *)&pdu->outdata.data[28] = htonl(expstatsnsn);
  239. }
  240. void iscsi_pdu_set_cdb(struct iscsi_pdu *pdu, struct scsi_task *task)
  241. {
  242. bzero(&pdu->outdata.data[32], 16);
  243. memcpy(&pdu->outdata.data[32], task->cdb, task->cdb_size);
  244. }
  245. void iscsi_pdu_set_lun(struct iscsi_pdu *pdu, uint32_t lun)
  246. {
  247. pdu->outdata.data[9] = lun;
  248. }
  249. void iscsi_pdu_set_expxferlen(struct iscsi_pdu *pdu, uint32_t expxferlen)
  250. {
  251. *(uint32_t *)&pdu->outdata.data[20] = htonl(expxferlen);
  252. }