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/>.
25 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
28 struct iscsi_context {
29 const char *initiator_name;
30 const char *target_name;
32 enum iscsi_session_type session_type;
33 unsigned char isid[6];
42 iscsi_command_cb connect_cb;
45 struct iscsi_pdu *outqueue;
46 struct iscsi_pdu *waitpdu;
53 #define ISCSI_HEADER_SIZE 48
55 #define ISCSI_PDU_IMMEDIATE 0x40
57 #define ISCSI_PDU_TEXT_FINAL 0x80
58 #define ISCSI_PDU_TEXT_CONTINUE 0x40
60 #define ISCSI_PDU_LOGIN_TRANSIT 0x80
61 #define ISCSI_PDU_LOGIN_CONTINUE 0x40
62 #define ISCSI_PDU_LOGIN_CSG_SECNEG 0x00
63 #define ISCSI_PDU_LOGIN_CSG_OPNEG 0x04
64 #define ISCSI_PDU_LOGIN_CSG_FF 0x0c
65 #define ISCSI_PDU_LOGIN_NSG_SECNEG 0x00
66 #define ISCSI_PDU_LOGIN_NSG_OPNEG 0x01
67 #define ISCSI_PDU_LOGIN_NSG_FF 0x03
70 #define ISCSI_PDU_SCSI_FINAL 0x80
71 #define ISCSI_PDU_SCSI_READ 0x40
72 #define ISCSI_PDU_SCSI_WRITE 0x20
73 #define ISCSI_PDU_SCSI_ATTR_UNTAGGED 0x00
74 #define ISCSI_PDU_SCSI_ATTR_SIMPLE 0x01
75 #define ISCSI_PDU_SCSI_ATTR_ORDERED 0x02
76 #define ISCSI_PDU_SCSI_ATTR_HEADOFQUEUE 0x03
77 #define ISCSI_PDU_SCSI_ATTR_ACA 0x04
79 #define ISCSI_PDU_DATA_FINAL 0x80
80 #define ISCSI_PDU_DATA_ACK_REQUESTED 0x40
81 #define ISCSI_PDU_DATA_BIDIR_OVERFLOW 0x10
82 #define ISCSI_PDU_DATA_BIDIR_UNDERFLOW 0x08
83 #define ISCSI_PDU_DATA_RESIDUAL_OVERFLOW 0x04
84 #define ISCSI_PDU_DATA_RESIDUAL_UNDERFLOW 0x02
85 #define ISCSI_PDU_DATA_CONTAINS_STATUS 0x01
87 enum iscsi_opcode {ISCSI_PDU_NOP_OUT=0x00,
88 ISCSI_PDU_SCSI_REQUEST=0x01,
89 ISCSI_PDU_LOGIN_REQUEST=0x03,
90 ISCSI_PDU_TEXT_REQUEST=0x04,
91 ISCSI_PDU_LOGOUT_REQUEST=0x06,
92 ISCSI_PDU_NOP_IN=0x20,
93 ISCSI_PDU_SCSI_RESPONSE=0x21,
94 ISCSI_PDU_LOGIN_RESPONSE=0x23,
95 ISCSI_PDU_TEXT_RESPONSE=0x24,
96 ISCSI_PDU_DATA_IN=0x25,
97 ISCSI_PDU_LOGOUT_RESPONSE=0x26};
100 struct iscsi_pdu *prev, *next;
104 enum iscsi_opcode response_opcode;
106 iscsi_command_cb callback;
110 struct iscsi_data outdata;
111 struct iscsi_data indata;
113 struct iscsi_scsi_cbdata *scsi_cbdata;
116 void iscsi_free_scsi_cbdata(struct iscsi_scsi_cbdata *scsi_cbdata);
118 struct iscsi_pdu *iscsi_allocate_pdu(struct iscsi_context *iscsi, enum iscsi_opcode opcode, enum iscsi_opcode response_opcode);
119 void iscsi_free_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
120 void iscsi_pdu_set_pduflags(struct iscsi_pdu *pdu, unsigned char flags);
121 void iscsi_pdu_set_immediate(struct iscsi_pdu *pdu);
122 void iscsi_pdu_set_ttt(struct iscsi_pdu *pdu, uint32_t ttt);
123 void iscsi_pdu_set_cmdsn(struct iscsi_pdu *pdu, uint32_t cmdsn);
124 void iscsi_pdu_set_lun(struct iscsi_pdu *pdu, uint32_t lun);
125 void iscsi_pdu_set_expstatsn(struct iscsi_pdu *pdu, uint32_t expstatsnsn);
126 void iscsi_pdu_set_expxferlen(struct iscsi_pdu *pdu, uint32_t expxferlen);
127 int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, unsigned char *dptr, int dsize);
128 int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
129 int iscsi_add_data(struct iscsi_data *data, unsigned char *dptr, int dsize, int pdualignment);
130 int iscsi_set_random_isid(struct iscsi_context *iscsi);
133 void iscsi_pdu_set_cdb(struct iscsi_pdu *pdu, struct scsi_task *task);
135 int iscsi_get_pdu_size(const unsigned char *hdr);
136 int iscsi_process_pdu(struct iscsi_context *iscsi, const unsigned char *hdr, int size);
138 int iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
139 int iscsi_process_text_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
140 int iscsi_process_logout_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
141 int iscsi_process_scsi_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
142 int iscsi_process_scsi_data_in(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size, int *is_finished);
143 int iscsi_process_nop_out_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);