iscontrol.c revision 359754
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
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: stable/11/sbin/iscontrol/iscontrol.c 359754 2020-04-09 20:38:36Z kevans $");
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
63static 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
68token_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
78token_t	DigestMethods[] = {
79     {"None",	0},
80     {"CRC32",	1},
81     {"CRC32C",	1},
82     {0, 0}
83};
84
85int	vflag;
86char	*iscsidev;
87
88u_char	isid[6 + 6];
89/*
90 | Default values
91 */
92isc_opt_t opvals = {
93     .port			= 3260,
94     .sockbufsize		= 128,
95     .iqn			= "iqn.2005-01.il.ac.huji.cs:",
96
97     .sessionType		= "Normal",
98     .targetAddress		= 0,
99     .targetName		= 0,
100     .initiatorName		= 0,
101     .authMethod		= "None",
102     .headerDigest		= "None,CRC32C",
103     .dataDigest		= "None,CRC32C",
104     .maxConnections		= 1,
105     .maxRecvDataSegmentLength	= 64 * 1024,
106     .maxXmitDataSegmentLength	= 8 * 1024, // 64 * 1024,
107     .maxBurstLength		= 128 * 1024,
108     .firstBurstLength		= 64 * 1024, // must be less than maxBurstLength
109     .defaultTime2Wait		= 0,
110     .defaultTime2Retain	= 0,
111     .maxOutstandingR2T		= 1,
112     .errorRecoveryLevel	= 0,
113
114     .dataPDUInOrder		= TRUE,
115     .dataSequenceInOrder	= TRUE,
116
117     .initialR2T		= TRUE,
118     .immediateData		= TRUE,
119};
120
121static void
122usage(const char *pname)
123{
124	fprintf(stderr, "usage: %s " USAGE "\n", pname);
125	exit(1);
126}
127
128int
129lookup(token_t *tbl, char *m)
130{
131     token_t	*tp;
132
133     for(tp = tbl; tp->name != NULL; tp++)
134	  if(strcasecmp(tp->name, m) == 0)
135	       return tp->val;
136     return 0;
137}
138
139int
140main(int cc, char **vv)
141{
142     int	ch, disco;
143     char	*pname, *pidfile, *p, *q, *ta, *kw, *v;
144     isc_opt_t	*op;
145     FILE	*fd;
146     size_t	n;
147
148     op = &opvals;
149     iscsidev = "/dev/"ISCSIDEV;
150     fd = NULL;
151     pname = vv[0];
152     if ((pname = basename(pname)) == NULL)
153	  err(1, "basename");
154
155     kw = ta = 0;
156     disco = 0;
157     pidfile = NULL;
158     /*
159      | check for driver & controller version match
160      */
161     n = 0;
162#define VERSION_OID_S	"net.iscsi_initiator.driver_version"
163     if (sysctlbyname(VERSION_OID_S, 0, &n, 0, 0) != 0) {
164	  if (errno == ENOENT)
165		errx(1, "sysctlbyname(\"" VERSION_OID_S "\") "
166			"failed; is the iscsi driver loaded?");
167	  err(1, "sysctlbyname(\"" VERSION_OID_S "\")");
168     }
169     v = malloc(n+1);
170     if (v == NULL)
171	  err(1, "malloc");
172     if (sysctlbyname(VERSION_OID_S, v, &n, 0, 0) != 0)
173	  err(1, "sysctlbyname");
174
175     if (strncmp(version, v, 3) != 0)
176	  errx(1, "versions mismatch");
177
178     while((ch = getopt(cc, vv, OPTIONS)) != -1) {
179	  switch(ch) {
180	  case 'v':
181	       vflag++;
182	       break;
183	  case 'c':
184	       fd = fopen(optarg, "r");
185	       if (fd == NULL)
186		    err(1, "fopen(\"%s\")", optarg);
187	       break;
188	  case 'd':
189	       disco = 1;
190	       break;
191	  case 't':
192	       ta = optarg;
193	       break;
194	  case 'n':
195	       kw = optarg;
196	       break;
197	  case 'p':
198	       pidfile = optarg;
199	       break;
200	  default:
201	       usage(pname);
202	  }
203     }
204     if(fd == NULL)
205	  fd = fopen("/etc/iscsi.conf", "r");
206
207     if(fd != NULL) {
208	  parseConfig(fd, kw, op);
209	  fclose(fd);
210     }
211     cc -= optind;
212     vv += optind;
213     if(cc > 0) {
214	  if(vflag)
215	       printf("adding '%s'\n", *vv);
216	  parseArgs(cc, vv, op);
217     }
218     if(ta)
219	  op->targetAddress = ta;
220
221     if(op->targetAddress == NULL) {
222	  warnx("no target specified!");
223	  usage(pname);
224     }
225     q = op->targetAddress;
226     if(*q == '[' && (q = strchr(q, ']')) != NULL) {
227	  *q++ = '\0';
228	  op->targetAddress++;
229     } else
230	  q = op->targetAddress;
231     if((p = strchr(q, ':')) != NULL) {
232	  *p++ = 0;
233	  op->port = atoi(p);
234	  p = strchr(p, ',');
235     }
236     if(p || ((p = strchr(q, ',')) != NULL)) {
237	  *p++ = 0;
238	  op->targetPortalGroupTag = atoi(p);
239     }
240     if(op->initiatorName == 0) {
241	  char	hostname[MAXHOSTNAMELEN];
242
243	  if(op->iqn) {
244	       if(gethostname(hostname, sizeof(hostname)) == 0)
245		    asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
246	       else
247		    asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
248	  }
249	  else {
250	       if(gethostname(hostname, sizeof(hostname)) == 0)
251		    asprintf(&op->initiatorName, "%s", hostname);
252	       else
253		    asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
254	  }
255     }
256     if(disco) {
257	  op->sessionType = "Discovery";
258	  op->targetName = 0;
259     }
260     op->pidfile = pidfile;
261     fsm(op);
262
263     exit(0);
264}
265