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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/* $OpenBSD: pdu.c,v 1.10 2014/11/23 13:05:51 claudio 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/types.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <scsi/iscsi.h>
#include <errno.h>
#include <event.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include "iscsid.h"
#include "log.h"
size_t pdu_readbuf_read(struct pdu_readbuf *, void *, size_t);
size_t pdu_readbuf_len(struct pdu_readbuf *);
#define PDU_MIN(_x, _y) ((_x) < (_y) ? (_x) : (_y))
void *
pdu_gethdr(struct pdu *p)
{
void *hdr;
if (!(hdr = calloc(1, sizeof(struct iscsi_pdu))))
return NULL;
if (pdu_addbuf(p, hdr, sizeof(struct iscsi_pdu), PDU_HEADER)) {
free(hdr);
return NULL;
}
return hdr;
}
int
text_to_pdu(struct kvp *k, struct pdu *p)
{
char *buf, *s;
size_t len = 0, rem;
int n, nk;
if (k == NULL)
return 0;
nk = 0;
while(k[nk].key) {
len += 2 + strlen(k[nk].key) + strlen(k[nk].value);
nk++;
}
if (!(buf = pdu_alloc(len)))
return -1;
s = buf;
rem = len;
nk = 0;
while(k[nk].key) {
n = snprintf(s, rem, "%s=%s", k[nk].key, k[nk].value);
if (n == -1 || (size_t)n >= rem)
fatalx("text_to_pdu");
rem -= n + 1;
s += n + 1;
nk++;
}
if (pdu_addbuf(p, buf, len, PDU_DATA))
return -1;
return len;
}
struct kvp *
pdu_to_text(char *buf, size_t len)
{
struct kvp *k;
size_t n;
char *eq;
unsigned int nkvp = 0, i;
/* remove padding zeros */
for (n = len; n > 0 && buf[n - 1] == '\0'; n--)
;
if (n == len) {
log_debug("pdu_to_text: badly terminated text data");
return NULL;
}
len = n + 1;
for(n = 0; n < len; n++)
if (buf[n] == '\0')
nkvp++;
if (!(k = calloc(nkvp + 1, sizeof(*k))))
return NULL;
for (i = 0; i < nkvp; i++) {
eq = strchr(buf, '=');
if (!eq) {
log_debug("pdu_to_text: badly encoded text data");
free(k);
return NULL;
}
*eq++ = '\0';
k[i].key = buf;
k[i].value = eq;
buf = eq + strlen(eq) + 1;
}
return k;
}
/* Modified version of strtonum() to fit iscsid's need
*
* Copyright (c) 2004 Ted Unangst and Todd Miller
* All rights reserved.
*
* 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.
*/
u_int64_t
text_to_num(const char *numstr, u_int64_t minval, u_int64_t maxval,
const char **errstrp)
{
unsigned long long ull = 0;
char *ep;
int error = 0;
struct errval {
const char *errstr;
int err;
} ev[4] = {
{ NULL, 0 },
{ "invalid", EINVAL },
{ "too small", ERANGE },
{ "too large", ERANGE }
};
#define INVALID 1
#define TOOSMALL 2
#define TOOLARGE 3
ev[0].err = errno;
errno = 0;
if (minval > maxval)
error = INVALID;
else {
ull = strtoull(numstr, &ep, 0);
if (numstr == ep || *ep != '\0')
error = INVALID;
else if (ull < minval)
error = TOOSMALL;
else if ((ull == ULLONG_MAX && errno == ERANGE) || ull > maxval)
error = TOOLARGE;
}
if (errstrp != NULL)
*errstrp = ev[error].errstr;
errno = ev[error].err;
if (error)
ull = 0;
return ull;
#undef INVALID
#undef TOOSMALL
#undef TOOLARGE
}
int
text_to_bool(const char *buf, const char **errstrp)
{
int val = 0;
if (!strcmp(buf, "Yes")) {
val = 1;
errno = 0;
} else if (!strcmp(buf, "No"))
errno = 0;
else
errno = EINVAL;
if (errstrp != NULL) {
if (errno == 0)
*errstrp = NULL;
else
*errstrp = "invalid";
}
return val;
}
/*
* Internal functions to send/recv pdus.
*/
void
pdu_free_queue(struct pduq *channel)
{
struct pdu *p;
while ((p = TAILQ_FIRST(channel))) {
TAILQ_REMOVE(channel, p, entry);
pdu_free(p);
}
}
ssize_t
pdu_read(struct connection *c)
{
struct iovec iov[2];
unsigned int niov = 1;
ssize_t n;
bzero(&iov, sizeof(iov));
iov[0].iov_base = c->prbuf.buf + c->prbuf.wpos;
if (c->prbuf.wpos < c->prbuf.rpos)
iov[0].iov_len = c->prbuf.rpos - c->prbuf.wpos;
else {
iov[0].iov_len = c->prbuf.size - c->prbuf.wpos;
if (c->prbuf.rpos > 0) {
niov++;
iov[1].iov_base = c->prbuf.buf;
iov[1].iov_len = c->prbuf.rpos - 1;
}
}
if ((n = readv(c->fd, iov, niov)) == -1)
return -1;
if (n == 0)
/* XXX what should we do on close with remaining data? */
return 0;
c->prbuf.wpos += n;
if (c->prbuf.wpos >= c->prbuf.size)
c->prbuf.wpos -= c->prbuf.size;
return n;
}
ssize_t
pdu_write(struct connection *c)
{
struct iovec iov[PDU_WRIOV];
struct pdu *b, *nb;
unsigned int niov = 0, j;
size_t off, resid, size;
ssize_t n;
TAILQ_FOREACH(b, &c->pdu_w, entry) {
if (niov >= PDU_WRIOV)
break;
off = b->resid;
for (j = 0; j < PDU_MAXIOV && niov < PDU_WRIOV; j++) {
if (!b->iov[j].iov_len)
continue;
if (off >= b->iov[j].iov_len) {
off -= b->iov[j].iov_len;
continue;
}
iov[niov].iov_base = (char *)b->iov[j].iov_base + off;
iov[niov++].iov_len = b->iov[j].iov_len - off;
off = 0;
}
}
if ((n = writev(c->fd, iov, niov)) == -1) {
if (errno == EAGAIN || errno == ENOBUFS ||
errno == EINTR) /* try later */
return 0;
else {
log_warn("pdu_write");
return -1;
}
}
if (n == 0)
return 0;
size = n;
for (b = TAILQ_FIRST(&c->pdu_w); b != NULL && size > 0; b = nb) {
nb = TAILQ_NEXT(b, entry);
resid = b->resid;
for (j = 0; j < PDU_MAXIOV; j++) {
if (resid >= b->iov[j].iov_len)
resid -= b->iov[j].iov_len;
else if (size >= b->iov[j].iov_len - resid) {
size -= b->iov[j].iov_len - resid;
b->resid += b->iov[j].iov_len - resid;
resid = 0;
} else {
b->resid += size;
size = 0;
break;
}
}
if (j == PDU_MAXIOV) {
/* all written */
TAILQ_REMOVE(&c->pdu_w, b, entry);
pdu_free(b);
}
}
return n;
}
int
pdu_pending(struct connection *c)
{
if (TAILQ_EMPTY(&c->pdu_w))
return 0;
else
return 1;
}
void
pdu_parse(struct connection *c)
{
struct pdu *p;
struct iscsi_pdu *ipdu;
char *ahb, *db;
size_t ahslen, dlen, off;
ssize_t n;
unsigned int j;
/* XXX XXX I DON'T LIKE YOU. CAN I REWRITE YOU? */
do {
if (!(p = c->prbuf.wip)) {
/* get and parse base header */
if (pdu_readbuf_len(&c->prbuf) < sizeof(*ipdu))
return;
if (!(p = pdu_new()))
goto fail;
if (!(ipdu = pdu_gethdr(p)))
goto fail;
c->prbuf.wip = p;
/*
* XXX maybe a pdu_readbuf_peek() would allow a better
* error handling.
*/
pdu_readbuf_read(&c->prbuf, ipdu, sizeof(*ipdu));
ahslen = ipdu->ahslen * sizeof(u_int32_t);
if (ahslen != 0) {
if (!(ahb = pdu_alloc(ahslen)) ||
pdu_addbuf(p, ahb, ahslen, PDU_AHS))
goto fail;
}
dlen = ipdu->datalen[0] << 16 | ipdu->datalen[1] << 8 |
ipdu->datalen[2];
if (dlen != 0) {
if (!(db = pdu_alloc(dlen)) ||
pdu_addbuf(p, db, dlen, PDU_DATA))
goto fail;
}
p->resid = sizeof(*ipdu);
} else {
off = p->resid;
for (j = 0; j < PDU_MAXIOV; j++) {
if (off >= p->iov[j].iov_len)
off -= p->iov[j].iov_len;
else {
n = pdu_readbuf_read(&c->prbuf,
(char *)p->iov[j].iov_base + off,
p->iov[j].iov_len - off);
p->resid += n;
if (n == 0 || off + n !=
p->iov[j].iov_len)
return;
}
}
p->resid = 0; /* reset resid so pdu can be reused */
c->prbuf.wip = NULL;
task_pdu_cb(c, p);
}
} while (1);
fail:
fatalx("pdu_parse hit a space oddity");
}
size_t
pdu_readbuf_read(struct pdu_readbuf *rb, void *ptr, size_t len)
{
size_t l;
if (rb->rpos == rb->wpos) {
return 0;
} else if (rb->rpos < rb->wpos) {
l = PDU_MIN(rb->wpos - rb->rpos, len);
memcpy(ptr, rb->buf + rb->rpos, l);
rb->rpos += l;
return l;
} else {
l = PDU_MIN(rb->size - rb->rpos, len);
memcpy(ptr, rb->buf + rb->rpos, l);
rb->rpos += l;
if (rb->rpos == rb->size)
rb->rpos = 0;
if (l < len)
return l + pdu_readbuf_read(rb, (char *)ptr + l,
len - l);
return l;
}
}
size_t
pdu_readbuf_len(struct pdu_readbuf *rb)
{
if (rb->rpos <= rb->wpos)
return rb->wpos - rb->rpos;
else
return rb->size - (rb->rpos - rb->wpos);
}
int
pdu_readbuf_set(struct pdu_readbuf *rb, size_t bsize)
{
char *nb;
if (bsize < rb->size)
/* can't shrink */
return 0;
if ((nb = realloc(rb->buf, bsize)) == NULL) {
free(rb->buf);
return -1;
}
rb->buf = nb;
rb->size = bsize;
return 0;
}
void
pdu_readbuf_free(struct pdu_readbuf *rb)
{
free(rb->buf);
}
|