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
|
/* $OpenBSD: iscsid.c,v 1.12 2014/04/20 22:18:04 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/time.h>
#include <sys/uio.h>
#include <err.h>
#include <event.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "iscsid.h"
#include "log.h"
void main_sig_handler(int, short, void *);
__dead void usage(void);
void shutdown_cb(int, short, void *);
struct initiator *initiator;
struct event exit_ev;
int exit_rounds;
#define ISCSI_EXIT_WAIT 5
const struct session_params iscsi_sess_defaults = {
.MaxBurstLength = 262144,
.FirstBurstLength = 65536,
.DefaultTime2Wait = 2,
.DefaultTime2Retain = 20,
.MaxOutstandingR2T = 1,
.MaxConnections = 1,
.InitialR2T = 1,
.ImmediateData = 1,
.DataPDUInOrder = 1,
.DataSequenceInOrder = 1,
.ErrorRecoveryLevel = 0
};
const struct connection_params iscsi_conn_defaults = {
.MaxRecvDataSegmentLength = 8192
};
int
main(int argc, char *argv[])
{
struct event ev_sigint, ev_sigterm, ev_sighup;
struct passwd *pw;
char *ctrlsock = ISCSID_CONTROL;
char *vscsidev = ISCSID_DEVICE;
int ch, debug = 0, verbose = 0;
log_init(1); /* log to stderr until daemonized */
log_verbose(1);
while ((ch = getopt(argc, argv, "dn:s:v")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 'n':
vscsidev = optarg;
break;
case 's':
ctrlsock = optarg;
break;
case 'v':
verbose = 1;
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();
/* check for root privileges */
if (geteuid())
errx(1, "need root privileges");
log_init(debug);
log_verbose(verbose);
if (control_init(ctrlsock) == -1)
fatalx("control socket setup failed");
if (!debug)
daemon(1, 0);
log_info("startup");
event_init();
vscsi_open(vscsidev);
/* chroot and drop to iscsid user */
if ((pw = getpwnam(ISCSID_USER)) == NULL)
errx(1, "unknown user %s", ISCSID_USER);
if (chroot(pw->pw_dir) == -1)
fatal("chroot");
if (chdir("/") == -1)
fatal("chdir(\"/\")");
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("can't drop privileges");
/* setup signal handler */
signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
signal_add(&ev_sigint, NULL);
signal_add(&ev_sigterm, NULL);
signal_add(&ev_sighup, NULL);
signal(SIGPIPE, SIG_IGN);
control_event_init();
initiator = initiator_init();
event_dispatch();
/* do some cleanup on the way out */
control_cleanup(ctrlsock);
initiator_cleanup(initiator);
log_info("exiting.");
return 0;
}
void
shutdown_cb(int fd, short event, void *arg)
{
struct timeval tv;
if (exit_rounds++ >= ISCSI_EXIT_WAIT || initiator_isdown(initiator))
event_loopexit(NULL);
timerclear(&tv);
tv.tv_sec = 1;
if (evtimer_add(&exit_ev, &tv) == -1)
fatal("shutdown_cb");
}
void
main_sig_handler(int sig, short event, void *arg)
{
struct timeval tv;
/* signal handler rules don't apply, libevent decouples for us */
switch (sig) {
case SIGTERM:
case SIGINT:
case SIGHUP:
initiator_shutdown(initiator);
evtimer_set(&exit_ev, shutdown_cb, NULL);
timerclear(&tv);
if (evtimer_add(&exit_ev, &tv) == -1)
fatal("main_sig_handler");
break;
default:
fatalx("unexpected signal");
/* NOTREACHED */
}
}
__dead void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-dv] [-n device] [-s socket]\n",
__progname);
exit(1);
}
void
iscsid_ctrl_dispatch(void *ch, struct pdu *pdu)
{
struct ctrlmsghdr *cmh;
struct initiator_config *ic;
struct session_config *sc;
struct session *s;
int *valp;
cmh = pdu_getbuf(pdu, NULL, 0);
if (cmh == NULL)
goto done;
switch (cmh->type) {
case CTRL_INITIATOR_CONFIG:
if (cmh->len[0] != sizeof(*ic)) {
log_warnx("CTRL_INITIATOR_CONFIG bad size");
control_compose(ch, CTRL_FAILURE, NULL, 0);
break;
}
ic = pdu_getbuf(pdu, NULL, 1);
memcpy(&initiator->config, ic, sizeof(initiator->config));
control_compose(ch, CTRL_SUCCESS, NULL, 0);
break;
case CTRL_SESSION_CONFIG:
if (cmh->len[0] != sizeof(*sc)) {
log_warnx("CTRL_SESSION_CONFIG bad size");
control_compose(ch, CTRL_FAILURE, NULL, 0);
break;
}
sc = pdu_getbuf(pdu, NULL, 1);
if (cmh->len[1])
sc->TargetName = pdu_getbuf(pdu, NULL, 2);
else if (sc->SessionType != SESSION_TYPE_DISCOVERY) {
control_compose(ch, CTRL_FAILURE, NULL, 0);
goto done;
} else
sc->TargetName = NULL;
if (cmh->len[2])
sc->InitiatorName = pdu_getbuf(pdu, NULL, 3);
else
sc->InitiatorName = NULL;
s = session_find(initiator, sc->SessionName);
if (s == NULL) {
s = session_new(initiator, sc->SessionType);
if (s == NULL) {
control_compose(ch, CTRL_FAILURE, NULL, 0);
goto done;
}
}
session_config(s, sc);
if (s->state == SESS_INIT)
session_fsm(s, SESS_EV_START, NULL);
control_compose(ch, CTRL_SUCCESS, NULL, 0);
break;
case CTRL_LOG_VERBOSE:
if (cmh->len[0] != sizeof(int)) {
log_warnx("CTRL_LOG_VERBOSE bad size");
control_compose(ch, CTRL_FAILURE, NULL, 0);
break;
}
valp = pdu_getbuf(pdu, NULL, 1);
log_verbose(*valp);
control_compose(ch, CTRL_SUCCESS, NULL, 0);
break;
case CTRL_VSCSI_STATS:
control_compose(ch, CTRL_VSCSI_STATS, vscsi_stats(),
sizeof(struct vscsi_stats));
break;
default:
log_warnx("unknown control message type %d", cmh->type);
control_compose(ch, CTRL_FAILURE, NULL, 0);
break;
}
done:
pdu_free(pdu);
}
#define MERGE_MIN(r, a, b, v) \
res->v = (mine->v < his->v ? mine->v : his->v)
#define MERGE_MAX(r, a, b, v) \
res->v = (mine->v > his->v ? mine->v : his->v)
#define MERGE_OR(r, a, b, v) \
res->v = (mine->v || his->v)
#define MERGE_AND(r, a, b, v) \
res->v = (mine->v && his->v)
void
iscsi_merge_sess_params(struct session_params *res,
struct session_params *mine, struct session_params *his)
{
MERGE_MIN(res, mine, his, MaxBurstLength);
MERGE_MIN(res, mine, his, FirstBurstLength);
MERGE_MAX(res, mine, his, DefaultTime2Wait);
MERGE_MIN(res, mine, his, DefaultTime2Retain);
MERGE_MIN(res, mine, his, MaxOutstandingR2T);
res->TargetPortalGroupTag = his->TargetPortalGroupTag;
MERGE_MIN(res, mine, his, MaxConnections);
MERGE_OR(res, mine, his, InitialR2T);
MERGE_AND(res, mine, his, ImmediateData);
MERGE_OR(res, mine, his, DataPDUInOrder);
MERGE_OR(res, mine, his, DataSequenceInOrder);
MERGE_MIN(res, mine, his, ErrorRecoveryLevel);
}
void
iscsi_merge_conn_params(struct connection_params *res,
struct connection_params *mine, struct connection_params *his)
{
res->MaxRecvDataSegmentLength = his->MaxRecvDataSegmentLength;
/* XXX HeaderDigest and DataDigest */
}
#undef MERGE_MIN
#undef MERGE_MAX
#undef MERGE_OR
#undef MERGE_AND
|