1e25c779eSMatthew Dillon /*-
2e25c779eSMatthew Dillon * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3e25c779eSMatthew Dillon * All rights reserved.
4e25c779eSMatthew Dillon *
5e25c779eSMatthew Dillon * Redistribution and use in source and binary forms, with or without
6e25c779eSMatthew Dillon * modification, are permitted provided that the following conditions
7e25c779eSMatthew Dillon * are met:
8e25c779eSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
9e25c779eSMatthew Dillon * notice, this list of conditions and the following disclaimer.
10e25c779eSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
11e25c779eSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
12e25c779eSMatthew Dillon * documentation and/or other materials provided with the distribution.
13e25c779eSMatthew Dillon *
14e25c779eSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e25c779eSMatthew Dillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e25c779eSMatthew Dillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e25c779eSMatthew Dillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e25c779eSMatthew Dillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e25c779eSMatthew Dillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e25c779eSMatthew Dillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e25c779eSMatthew Dillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e25c779eSMatthew Dillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e25c779eSMatthew Dillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e25c779eSMatthew Dillon * SUCH DAMAGE.
25e25c779eSMatthew Dillon *
26e25c779eSMatthew Dillon */
27e25c779eSMatthew Dillon /*
28e25c779eSMatthew Dillon | $Id: iscontrol.c,v 2.2 2006/12/01 09:11:56 danny Exp danny $
29e25c779eSMatthew Dillon */
30e25c779eSMatthew Dillon /*
31e25c779eSMatthew Dillon | the user level initiator (client)
32e25c779eSMatthew Dillon */
33e25c779eSMatthew Dillon
34e25c779eSMatthew Dillon #include <sys/param.h>
35e25c779eSMatthew Dillon #include <sys/types.h>
36e25c779eSMatthew Dillon #include <sys/socket.h>
37e25c779eSMatthew Dillon #include <sys/sysctl.h>
381cdc323aSAntonio Huete #include <sys/module.h>
391cdc323aSAntonio Huete #include <sys/linker.h>
40e25c779eSMatthew Dillon
41e25c779eSMatthew Dillon #include <netinet/in.h>
42e25c779eSMatthew Dillon #include <netinet/tcp.h>
43e25c779eSMatthew Dillon #include <arpa/inet.h>
44e25c779eSMatthew Dillon #include <sys/ioctl.h>
45e25c779eSMatthew Dillon #include <netdb.h>
46e25c779eSMatthew Dillon #include <stdlib.h>
47e25c779eSMatthew Dillon #include <unistd.h>
48e25c779eSMatthew Dillon #include <stdio.h>
49e25c779eSMatthew Dillon #include <string.h>
50e25c779eSMatthew Dillon #include <errno.h>
51e25c779eSMatthew Dillon #include <fcntl.h>
52e25c779eSMatthew Dillon #include <time.h>
53e25c779eSMatthew Dillon #include <camlib.h>
54e25c779eSMatthew Dillon
55e25c779eSMatthew Dillon #include "iscsi.h"
56e25c779eSMatthew Dillon #include "iscontrol.h"
57e25c779eSMatthew Dillon
58e53224f8SThomas Nikolajsen #define USAGE "[-dv] [-c file] [-n nickname] [-t target] [variable=value ...]"
59e25c779eSMatthew Dillon #define OPTIONS "vdc:t:n:"
60e25c779eSMatthew Dillon
61e25c779eSMatthew Dillon token_t AuthMethods[] = {
62e25c779eSMatthew Dillon {"None", NONE},
63e25c779eSMatthew Dillon {"KRB5", KRB5},
64e25c779eSMatthew Dillon {"SPKM1", SPKM1},
65e25c779eSMatthew Dillon {"SPKM2", SPKM2},
66e25c779eSMatthew Dillon {"SRP", SRP},
67e25c779eSMatthew Dillon {"CHAP", CHAP},
68e25c779eSMatthew Dillon {0, 0}
69e25c779eSMatthew Dillon };
70e25c779eSMatthew Dillon
71e25c779eSMatthew Dillon token_t DigestMethods[] = {
72e25c779eSMatthew Dillon {"None", 0},
73e25c779eSMatthew Dillon {"CRC32", 1},
74e25c779eSMatthew Dillon {"CRC32C", 1},
75e25c779eSMatthew Dillon {0, 0}
76e25c779eSMatthew Dillon };
77e25c779eSMatthew Dillon
78760270c4SSascha Wildner int vflag;
79760270c4SSascha Wildner char *iscsidev;
80760270c4SSascha Wildner
81e25c779eSMatthew Dillon /*
82e25c779eSMatthew Dillon | Default values
83e25c779eSMatthew Dillon */
84*d50f9ae3SSascha Wildner static isc_opt_t opvals = {
85e25c779eSMatthew Dillon .port = 3260,
86e25c779eSMatthew Dillon .sockbufsize = 128,
87e25c779eSMatthew Dillon .iqn = "iqn.2005-01.il.ac.huji.cs:",
88e25c779eSMatthew Dillon
89e25c779eSMatthew Dillon .sessionType = "Normal",
90e25c779eSMatthew Dillon .targetAddress = 0,
91e25c779eSMatthew Dillon .targetName = 0,
92e25c779eSMatthew Dillon .initiatorName = 0,
93e25c779eSMatthew Dillon .authMethod = "None",
94e25c779eSMatthew Dillon .headerDigest = "None,CRC32C",
95e25c779eSMatthew Dillon .dataDigest = "None,CRC32C",
96e25c779eSMatthew Dillon .maxConnections = 1,
97e25c779eSMatthew Dillon .maxRecvDataSegmentLength = 64 * 1024,
98e25c779eSMatthew Dillon .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024,
99e25c779eSMatthew Dillon .maxBurstLength = 128 * 1024,
100e25c779eSMatthew Dillon .firstBurstLength = 64 * 1024, // must be less than maxBurstLength
101e25c779eSMatthew Dillon .defaultTime2Wait = 0,
102e25c779eSMatthew Dillon .defaultTime2Retain = 0,
103e25c779eSMatthew Dillon .maxOutstandingR2T = 1,
104e25c779eSMatthew Dillon .errorRecoveryLevel = 0,
105e25c779eSMatthew Dillon
106ae2af236SSascha Wildner .dataPDUInOrder = true,
107ae2af236SSascha Wildner .dataSequenceInOrder = true,
108e25c779eSMatthew Dillon
109ae2af236SSascha Wildner .initialR2T = true,
110ae2af236SSascha Wildner .immediateData = true,
111e25c779eSMatthew Dillon };
112e25c779eSMatthew Dillon
113e25c779eSMatthew Dillon int
lookup(token_t * tbl,char * m)114e25c779eSMatthew Dillon lookup(token_t *tbl, char *m)
115e25c779eSMatthew Dillon {
116e25c779eSMatthew Dillon token_t *tp;
117e25c779eSMatthew Dillon
118e25c779eSMatthew Dillon for(tp = tbl; tp->name != NULL; tp++)
119e25c779eSMatthew Dillon if(strcasecmp(tp->name, m) == 0)
120e25c779eSMatthew Dillon return tp->val;
121e25c779eSMatthew Dillon return 0;
122e25c779eSMatthew Dillon }
123e25c779eSMatthew Dillon
124e25c779eSMatthew Dillon int
main(int cc,char ** vv)125e25c779eSMatthew Dillon main(int cc, char **vv)
126e25c779eSMatthew Dillon {
127e25c779eSMatthew Dillon int ch, disco;
128e25c779eSMatthew Dillon char *pname, *p, *q, *ta, *kw;
129e25c779eSMatthew Dillon isc_opt_t *op;
130e25c779eSMatthew Dillon FILE *fd;
131e25c779eSMatthew Dillon
1321cdc323aSAntonio Huete /* Try to load iscsi_initiator module before starting its operation */
1331cdc323aSAntonio Huete if (modfind(INITIATORMOD) < 0) {
1341cdc323aSAntonio Huete if (kldload(INITIATORMOD) < 0 || modfind(INITIATORMOD) < 0) {
1351cdc323aSAntonio Huete perror(INITIATORMOD ": Error while handling kernel module");
1361cdc323aSAntonio Huete return 1;
1371cdc323aSAntonio Huete }
1381cdc323aSAntonio Huete }
1391cdc323aSAntonio Huete
140e25c779eSMatthew Dillon op = &opvals;
141e25c779eSMatthew Dillon iscsidev = "/dev/"ISCSIDEV;
142e25c779eSMatthew Dillon fd = NULL;
143e25c779eSMatthew Dillon pname = vv[0];
144e25c779eSMatthew Dillon if((p = strrchr(pname, '/')) != NULL)
145e25c779eSMatthew Dillon pname = p + 1;
146e25c779eSMatthew Dillon
147678e8cc6SSascha Wildner kw = ta = NULL;
148e25c779eSMatthew Dillon disco = 0;
149e25c779eSMatthew Dillon
150e25c779eSMatthew Dillon while((ch = getopt(cc, vv, OPTIONS)) != -1) {
151e25c779eSMatthew Dillon switch(ch) {
152e25c779eSMatthew Dillon case 'v':
153e25c779eSMatthew Dillon vflag++;
154e25c779eSMatthew Dillon break;
155e25c779eSMatthew Dillon case 'c':
156e25c779eSMatthew Dillon fd = fopen(optarg, "r");
157e25c779eSMatthew Dillon if(fd == NULL) {
158e25c779eSMatthew Dillon perror(optarg);
159e25c779eSMatthew Dillon exit(1);
160e25c779eSMatthew Dillon }
161e25c779eSMatthew Dillon break;
162e25c779eSMatthew Dillon case 'd':
163e25c779eSMatthew Dillon disco = 1;
164e25c779eSMatthew Dillon break;
165e25c779eSMatthew Dillon case 't':
166e25c779eSMatthew Dillon ta = optarg;
167e25c779eSMatthew Dillon break;
168e25c779eSMatthew Dillon case 'n':
169e25c779eSMatthew Dillon kw = optarg;
170e25c779eSMatthew Dillon break;
171e25c779eSMatthew Dillon default:
172e25c779eSMatthew Dillon badu:
173e25c779eSMatthew Dillon fprintf(stderr, "Usage: %s %s\n", pname, USAGE);
174e25c779eSMatthew Dillon exit(1);
175e25c779eSMatthew Dillon }
176e25c779eSMatthew Dillon }
177e25c779eSMatthew Dillon if(fd == NULL)
178e25c779eSMatthew Dillon fd = fopen("/etc/iscsi.conf", "r");
179e25c779eSMatthew Dillon
180e25c779eSMatthew Dillon if(fd != NULL) {
181e25c779eSMatthew Dillon parseConfig(fd, kw, op);
182e25c779eSMatthew Dillon fclose(fd);
183e25c779eSMatthew Dillon }
184e25c779eSMatthew Dillon cc -= optind;
185e25c779eSMatthew Dillon vv += optind;
186e25c779eSMatthew Dillon if(cc > 0) {
187e25c779eSMatthew Dillon if(vflag)
188e25c779eSMatthew Dillon printf("adding '%s'\n", *vv);
189e25c779eSMatthew Dillon parseArgs(cc, vv, op);
190e25c779eSMatthew Dillon }
191e25c779eSMatthew Dillon if(ta)
192e25c779eSMatthew Dillon op->targetAddress = ta;
193e25c779eSMatthew Dillon
194e25c779eSMatthew Dillon if(op->targetAddress == NULL) {
195e25c779eSMatthew Dillon fprintf(stderr, "No target!\n");
196e25c779eSMatthew Dillon goto badu;
197e25c779eSMatthew Dillon }
198e25c779eSMatthew Dillon q = op->targetAddress;
199e25c779eSMatthew Dillon if(*q == '[' && (q = strchr(q, ']')) != NULL) {
200e25c779eSMatthew Dillon *q++ = '\0';
201e25c779eSMatthew Dillon op->targetAddress++;
202e25c779eSMatthew Dillon } else
203e25c779eSMatthew Dillon q = op->targetAddress;
204e25c779eSMatthew Dillon if((p = strchr(q, ':')) != NULL) {
205e25c779eSMatthew Dillon *p++ = 0;
206e25c779eSMatthew Dillon op->port = atoi(p);
207e25c779eSMatthew Dillon p = strchr(p, ',');
208e25c779eSMatthew Dillon }
209e25c779eSMatthew Dillon if(p || ((p = strchr(q, ',')) != NULL)) {
210e25c779eSMatthew Dillon *p++ = 0;
211e25c779eSMatthew Dillon op->targetPortalGroupTag = atoi(p);
212e25c779eSMatthew Dillon }
213e25c779eSMatthew Dillon if(op->initiatorName == 0) {
214e25c779eSMatthew Dillon char hostname[256];
215e25c779eSMatthew Dillon
216e25c779eSMatthew Dillon if(op->iqn) {
217e25c779eSMatthew Dillon if(gethostname(hostname, sizeof(hostname)) == 0)
218e25c779eSMatthew Dillon asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
219e25c779eSMatthew Dillon else
220e25c779eSMatthew Dillon asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
221e25c779eSMatthew Dillon }
222e25c779eSMatthew Dillon else {
223e25c779eSMatthew Dillon if(gethostname(hostname, sizeof(hostname)) == 0)
224e25c779eSMatthew Dillon asprintf(&op->initiatorName, "%s", hostname);
225e25c779eSMatthew Dillon else
226e25c779eSMatthew Dillon asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
227e25c779eSMatthew Dillon }
228e25c779eSMatthew Dillon }
229e25c779eSMatthew Dillon if(disco) {
230e25c779eSMatthew Dillon op->sessionType = "Discovery";
231e25c779eSMatthew Dillon op->targetName = 0;
232e25c779eSMatthew Dillon }
233e25c779eSMatthew Dillon
234e25c779eSMatthew Dillon fsm(op);
235e25c779eSMatthew Dillon
236e25c779eSMatthew Dillon exit(0);
237e25c779eSMatthew Dillon }
238