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