1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005-2010 Daniel Braniss <[email protected]>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29 /*
30 | $Id: iscontrol.c,v 2.2 2006/12/01 09:11:56 danny Exp danny $
31 */
32 /*
33 | the user level initiator (client)
34 */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
46 #include <arpa/inet.h>
47 #include <sys/ioctl.h>
48 #include <netdb.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <libgen.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <camlib.h>
59
60 #include <dev/iscsi_initiator/iscsi.h>
61 #include "iscontrol.h"
62
63 static char version[] = "2.3.1"; // keep in sync with iscsi_initiator
64
65 #define USAGE "[-v] [-d] [-c config] [-n name] [-t target] [-p pidfile]"
66 #define OPTIONS "vdc:t:n:p:"
67
68 token_t AuthMethods[] = {
69 {"None", NONE},
70 {"KRB5", KRB5},
71 {"SPKM1", SPKM1},
72 {"SPKM2", SPKM2},
73 {"SRP", SRP},
74 {"CHAP", CHAP},
75 {0, 0}
76 };
77
78 token_t DigestMethods[] = {
79 {"None", 0},
80 {"CRC32", 1},
81 {"CRC32C", 1},
82 {0, 0}
83 };
84
85 u_char isid[6 + 6];
86 /*
87 | Default values
88 */
89 isc_opt_t opvals = {
90 .port = 3260,
91 .sockbufsize = 128,
92 .iqn = "iqn.2005-01.il.ac.huji.cs:",
93
94 .sessionType = "Normal",
95 .targetAddress = 0,
96 .targetName = 0,
97 .initiatorName = 0,
98 .authMethod = "None",
99 .headerDigest = "None,CRC32C",
100 .dataDigest = "None,CRC32C",
101 .maxConnections = 1,
102 .maxRecvDataSegmentLength = 64 * 1024,
103 .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024,
104 .maxBurstLength = 128 * 1024,
105 .firstBurstLength = 64 * 1024, // must be less than maxBurstLength
106 .defaultTime2Wait = 0,
107 .defaultTime2Retain = 0,
108 .maxOutstandingR2T = 1,
109 .errorRecoveryLevel = 0,
110
111 .dataPDUInOrder = TRUE,
112 .dataSequenceInOrder = TRUE,
113
114 .initialR2T = TRUE,
115 .immediateData = TRUE,
116 };
117
118 static void
usage(const char * pname)119 usage(const char *pname)
120 {
121 fprintf(stderr, "usage: %s " USAGE "\n", pname);
122 exit(1);
123 }
124
125 int
lookup(token_t * tbl,char * m)126 lookup(token_t *tbl, char *m)
127 {
128 token_t *tp;
129
130 for(tp = tbl; tp->name != NULL; tp++)
131 if(strcasecmp(tp->name, m) == 0)
132 return tp->val;
133 return 0;
134 }
135
136 int
main(int cc,char ** vv)137 main(int cc, char **vv)
138 {
139 int ch, disco;
140 char *pname, *pidfile, *p, *q, *ta, *kw, *v;
141 isc_opt_t *op;
142 FILE *fd;
143 size_t n;
144
145 op = &opvals;
146 iscsidev = "/dev/"ISCSIDEV;
147 fd = NULL;
148 pname = vv[0];
149 if ((pname = basename(pname)) == NULL)
150 err(1, "basename");
151
152 kw = ta = 0;
153 disco = 0;
154 pidfile = NULL;
155 /*
156 | check for driver & controller version match
157 */
158 n = 0;
159 #define VERSION_OID_S "net.iscsi_initiator.driver_version"
160 if (sysctlbyname(VERSION_OID_S, 0, &n, 0, 0) != 0) {
161 if (errno == ENOENT)
162 errx(1, "sysctlbyname(\"" VERSION_OID_S "\") "
163 "failed; is the iscsi driver loaded?");
164 err(1, "sysctlbyname(\"" VERSION_OID_S "\")");
165 }
166 v = malloc(n+1);
167 if (v == NULL)
168 err(1, "malloc");
169 if (sysctlbyname(VERSION_OID_S, v, &n, 0, 0) != 0)
170 err(1, "sysctlbyname");
171
172 if (strncmp(version, v, 3) != 0)
173 errx(1, "versions mismatch");
174
175 while((ch = getopt(cc, vv, OPTIONS)) != -1) {
176 switch(ch) {
177 case 'v':
178 vflag++;
179 break;
180 case 'c':
181 fd = fopen(optarg, "r");
182 if (fd == NULL)
183 err(1, "fopen(\"%s\")", optarg);
184 break;
185 case 'd':
186 disco = 1;
187 break;
188 case 't':
189 ta = optarg;
190 break;
191 case 'n':
192 kw = optarg;
193 break;
194 case 'p':
195 pidfile = optarg;
196 break;
197 default:
198 usage(pname);
199 }
200 }
201 if(fd == NULL)
202 fd = fopen("/etc/iscsi.conf", "r");
203
204 if(fd != NULL) {
205 parseConfig(fd, kw, op);
206 fclose(fd);
207 }
208 cc -= optind;
209 vv += optind;
210 if(cc > 0) {
211 if(vflag)
212 printf("adding '%s'\n", *vv);
213 parseArgs(cc, vv, op);
214 }
215 if(ta)
216 op->targetAddress = ta;
217
218 if(op->targetAddress == NULL) {
219 warnx("no target specified!");
220 usage(pname);
221 }
222 q = op->targetAddress;
223 if(*q == '[' && (q = strchr(q, ']')) != NULL) {
224 *q++ = '\0';
225 op->targetAddress++;
226 } else
227 q = op->targetAddress;
228 if((p = strchr(q, ':')) != NULL) {
229 *p++ = 0;
230 op->port = atoi(p);
231 p = strchr(p, ',');
232 }
233 if(p || ((p = strchr(q, ',')) != NULL)) {
234 *p++ = 0;
235 op->targetPortalGroupTag = atoi(p);
236 }
237 if(op->initiatorName == 0) {
238 char hostname[MAXHOSTNAMELEN];
239
240 if(op->iqn) {
241 if(gethostname(hostname, sizeof(hostname)) == 0)
242 asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
243 else
244 asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
245 }
246 else {
247 if(gethostname(hostname, sizeof(hostname)) == 0)
248 asprintf(&op->initiatorName, "%s", hostname);
249 else
250 asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
251 }
252 }
253 if(disco) {
254 op->sessionType = "Discovery";
255 op->targetName = 0;
256 }
257 op->pidfile = pidfile;
258 fsm(op);
259
260 exit(0);
261 }
262