1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/* $OpenBSD: initiator.c,v 1.3 2010/09/25 16:20:06 sobrado Exp $ */
/*
* Copyright (c) 2009 Claudio Jeker <claudio@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <scsi/iscsi.h>
#include <event.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "iscsid.h"
#include "log.h"
struct initiator *initiator;
struct kvp *initiator_login_kvp(struct session *);
char *default_initiator_name(void);
struct initiator *
initiator_init(void)
{
if (!(initiator = calloc(1, sizeof(*initiator))))
fatal("initiator_init");
initiator->config.isid_base =
arc4random_uniform(0xffffff) | ISCSI_ISID_RAND;
initiator->config.isid_qual = arc4random_uniform(0xffff);
TAILQ_INIT(&initiator->sessions);
return (initiator);
}
void
initiator_cleanup(struct initiator *i)
{
struct session *s;
while ((s = TAILQ_FIRST(&i->sessions)) != NULL) {
TAILQ_REMOVE(&i->sessions, s, entry);
session_close(s);
}
free(initiator);
}
struct session *
initiator_t2s(u_int target)
{
struct session *s;
TAILQ_FOREACH(s, &initiator->sessions, entry) {
if (s->target == target)
return s;
}
return NULL;
}
struct session *
session_find(struct initiator *i, char *name)
{
struct session *s;
TAILQ_FOREACH(s, &initiator->sessions, entry) {
if (strcmp(s->config.SessionName, name) == 0)
return s;
}
return NULL;
}
struct session *
session_new(struct initiator *i, u_int8_t st)
{
struct session *s;
if (!(s = calloc(1, sizeof(*s))))
return NULL;
/* use the same qualifier unless there is a conflict */
s->isid_base = i->config.isid_base;
s->isid_qual = i->config.isid_qual;
s->cmdseqnum = arc4random();
s->itt = arc4random();
s->initiator = i;
s->state = SESS_FREE;
if (st == SESSION_TYPE_DISCOVERY)
s->target = 0;
else
s->target = s->initiator->target++;
TAILQ_INSERT_HEAD(&i->sessions, s, entry);
TAILQ_INIT(&s->connections);
TAILQ_INIT(&s->tasks);
return s;
}
void
session_close(struct session *s)
{
struct connection *c;
while ((c = TAILQ_FIRST(&s->connections)) != NULL)
conn_free(c);
free(s->config.TargetName);
free(s->config.InitiatorName);
free(s);
}
void
session_config(struct session *s, struct session_config *sc)
{
if (s->config.TargetName)
free(s->config.TargetName);
s->config.TargetName = NULL;
if (s->config.InitiatorName)
free(s->config.InitiatorName);
s->config.InitiatorName = NULL;
s->config = *sc;
if (sc->TargetName) {
s->config.TargetName = strdup(sc->TargetName);
if (s->config.TargetName == NULL)
fatal("strdup");
}
if (sc->InitiatorName) {
s->config.InitiatorName = strdup(sc->InitiatorName);
if (s->config.InitiatorName == NULL)
fatal("strdup");
} else
s->config.InitiatorName = default_initiator_name();
}
void
session_task_issue(struct session *s, struct task *t)
{
TAILQ_INSERT_TAIL(&s->tasks, t, entry);
session_schedule(s);
}
void
session_schedule(struct session *s)
{
struct task *t = TAILQ_FIRST(&s->tasks);
struct connection *c;
if (!t)
return;
/* XXX IMMEDIATE TASK NEED SPECIAL HANDLING !!!! */
/* wake up a idle connection or a not busy one */
/* XXX this needs more work as it makes the daemon go wrooOOOMM */
TAILQ_REMOVE(&s->tasks, t, entry);
TAILQ_FOREACH(c, &s->connections, entry)
if (conn_task_issue(c, t))
return;
/* all connections are busy readd task to the head */
TAILQ_INSERT_HEAD(&s->tasks, t, entry);
}
struct task_login {
struct task task;
struct connection *c;
u_int16_t tsih;
u_int8_t stage;
};
struct pdu *initiator_login_build(struct task_login *, struct kvp *);
void initiator_login_cb(struct connection *, void *, struct pdu *);
void initiator_discovery_cb(struct connection *, void *, struct pdu *);
struct pdu *initiator_text_build(struct task *, struct session *, struct kvp *);
struct kvp *
initiator_login_kvp(struct session *s)
{
struct kvp *kvp;
if (!(kvp = calloc(4, sizeof(*kvp))))
return NULL;
kvp[0].key = "AuthMethod";
kvp[0].value = "None";
kvp[1].key = "InitiatorName";
kvp[1].value = s->config.InitiatorName;
if (s->config.SessionType == SESSION_TYPE_DISCOVERY) {
kvp[2].key = "SessionType";
kvp[2].value = "Discovery";
} else {
kvp[2].key = "TargetName";
kvp[2].value = s->config.TargetName;
}
return kvp;
}
void
initiator_login(struct connection *c)
{
struct task_login *tl;
struct pdu *p;
struct kvp *kvp;
if (!(tl = calloc(1, sizeof(*tl)))) {
log_warn("initiator_login");
conn_fail(c);
return;
}
tl->c = c;
tl->stage = ISCSI_LOGIN_STG_SECNEG;
if (!(kvp = initiator_login_kvp(c->session))) {
log_warnx("initiator_login_kvp failed");
free(tl);
conn_fail(c);
return;
}
if (!(p = initiator_login_build(tl, kvp))) {
log_warnx("initiator_login_build failed");
free(tl);
conn_fail(c);
return;
}
free(kvp);
task_init(&tl->task, c->session, 1, tl, initiator_login_cb);
task_pdu_add(&tl->task, p);
/* XXX this is wrong, login needs to run on a specific connection */
session_task_issue(c->session, &tl->task);
}
struct pdu *
initiator_login_build(struct task_login *tl, struct kvp *kvp)
{
struct pdu *p;
struct iscsi_pdu_login_request *lreq;
int n;
if (!(p = pdu_new()))
return NULL;
if (!(lreq = pdu_gethdr(p)))
return NULL;
lreq->opcode = ISCSI_OP_LOGIN_REQUEST | ISCSI_OP_F_IMMEDIATE;
if (tl->stage == ISCSI_LOGIN_STG_SECNEG)
lreq->flags = ISCSI_LOGIN_F_T |
ISCSI_LOGIN_F_CSG(ISCSI_LOGIN_STG_OPNEG) |
ISCSI_LOGIN_F_NSG(ISCSI_LOGIN_STG_FULL);
else if (tl->stage == ISCSI_LOGIN_STG_OPNEG)
lreq->flags = ISCSI_LOGIN_F_T |
ISCSI_LOGIN_F_CSG(ISCSI_LOGIN_STG_OPNEG) |
ISCSI_LOGIN_F_NSG(ISCSI_LOGIN_STG_FULL);
lreq->isid_base = htonl(tl->c->session->isid_base);
lreq->isid_qual = htons(tl->c->session->isid_qual);
lreq->tsih = tl->tsih;
lreq->cid = htons(tl->c->cid);
lreq->expstatsn = htonl(tl->c->expstatsn);
if ((n = text_to_pdu(kvp, p)) == -1)
return NULL;
n = htonl(n);
bcopy(&n, &lreq->ahslen, sizeof(n));
return p;
}
void
initiator_login_cb(struct connection *c, void *arg, struct pdu *p)
{
struct task_login *tl = arg;
struct iscsi_pdu_login_response *lresp;
lresp = pdu_getbuf(p, NULL, PDU_HEADER);
/* XXX handle packet would be great */
log_pdu(p, 1);
if (ISCSI_PDU_OPCODE(lresp->opcode) != ISCSI_OP_LOGIN_RESPONSE) {
log_debug("Unknown crap");
}
task_cleanup(&tl->task, c);
conn_loggedin(c);
free(tl);
pdu_free(p);
}
void
initiator_discovery(struct session *s)
{
struct task *t;
struct pdu *p;
struct kvp kvp[] = {
{ "SendTargets", "All" },
{ NULL, NULL }
};
if (!(t = calloc(1, sizeof(*t)))) {
log_warn("initiator_discovery");
return;
}
if (!(p = initiator_text_build(t, s, kvp))) {
log_warnx("initiator_text_build failed");
return;
}
task_init(t, s, 0, t, initiator_discovery_cb);
task_pdu_add(t, p);
session_task_issue(s, t);
}
struct pdu *
initiator_text_build(struct task *t, struct session *s, struct kvp *kvp)
{
struct pdu *p;
struct iscsi_pdu_text_request *lreq;
int n;
if (!(p = pdu_new()))
return NULL;
if (!(lreq = pdu_gethdr(p)))
return NULL;
lreq->opcode = ISCSI_OP_TEXT_REQUEST;
lreq->flags = ISCSI_TEXT_F_F;
lreq->ttt = 0xffffffff;
if ((n = text_to_pdu(kvp, p)) == -1)
return NULL;
n = htonl(n);
bcopy(&n, &lreq->ahslen, sizeof(n));
return p;
}
void
initiator_discovery_cb(struct connection *c, void *arg, struct pdu *p)
{
struct iscsi_pdu_text_response *lresp;
u_char *buf = NULL;
struct kvp *kvp, *k;
size_t n, size;
lresp = pdu_getbuf(p, NULL, PDU_HEADER);
switch (ISCSI_PDU_OPCODE(lresp->opcode)) {
case ISCSI_OP_TEXT_RESPONSE:
buf = pdu_getbuf(p, &n, PDU_DATA);
if (buf == NULL)
goto fail;
size = lresp->datalen[0] << 16 | lresp->datalen[1] << 8 |
lresp->datalen[2];
if (size > n)
goto fail;
kvp = pdu_to_text(buf, size);
if (kvp == NULL)
goto fail;
log_debug("ISCSI_OP_TEXT_RESPONSE");
for (k = kvp; k->key; k++) {
log_debug("%s\t=>\t%s", k->key, k->value);
}
free(kvp);
free(arg);
conn_close(c);
break;
default:
fail:
conn_fail(c);
}
pdu_free(p);
}
char *
default_initiator_name(void)
{
char *s, hostname[MAXHOSTNAMELEN];
if (gethostname(hostname, sizeof(hostname)))
strlcpy(hostname, "initiator", sizeof(hostname));
if ((s = strchr(hostname, '.')))
*s = '\0';
if (asprintf(&s, "%s:%s", ISCSID_BASE_NAME, hostname) == -1)
return ISCSID_BASE_NAME ":initiator";
return s;
}
|