]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/iscontrol/iscontrol.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / iscontrol / iscontrol.c
1 /*-
2  * Copyright (c) 2005-2008 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 <stdlib.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <time.h>
54 #include <camlib.h>
55
56 #include "iscsi.h"
57 #include "iscontrol.h"
58
59 #define USAGE "[-v] [-d] [-c config] [-n name] [-t target] "
60 #define OPTIONS "vdc:t:n:"
61
62 #ifndef DEBUG
63 //int   vflag;
64 #endif
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}
74 };
75
76 token_t DigestMethods[] = {
77      {"None",   0},
78      {"CRC32",  1},
79      {"CRC32C", 1},
80      {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 int
117 lookup(token_t *tbl, char *m)
118 {
119      token_t    *tp;
120
121      for(tp = tbl; tp->name != NULL; tp++)
122           if(strcasecmp(tp->name, m) == 0)
123                return tp->val;
124      return 0;
125 }
126
127 int
128 main(int cc, char **vv)
129 {
130      int        ch, disco;
131      char       *pname, *p, *q, *ta, *kw;
132      isc_opt_t  *op;
133      FILE       *fd;
134
135      op = &opvals;
136      iscsidev = "/dev/"ISCSIDEV;
137      fd = NULL;
138      pname = vv[0];
139      if((p = strrchr(pname, '/')) != NULL)
140           pname = p + 1;
141
142      kw = ta = 0;
143      disco = 0;
144
145      while((ch = getopt(cc, vv, OPTIONS)) != -1) {
146           switch(ch) {
147           case 'v':
148                vflag++;
149                break;
150           case 'c':
151                fd = fopen(optarg, "r");
152                if(fd == NULL) {
153                     perror(optarg);
154                     exit(1);
155                }
156                break;
157           case 'd':
158                disco = 1;
159                break;
160           case 't':
161                ta = optarg;
162                break;
163           case 'n':
164                kw = optarg;
165                break;
166           default:
167           badu:
168                fprintf(stderr, "Usage: %s %s\n", pname, USAGE);
169                exit(1);
170           }
171      }
172      if(fd == NULL)
173           fd = fopen("/etc/iscsi.conf", "r");
174
175      if(fd != NULL) {
176           parseConfig(fd, kw, op);
177           fclose(fd);
178      }
179      cc -= optind;
180      vv += optind;
181      if(cc > 0) {
182           if(vflag)
183                printf("adding '%s'\n", *vv);
184           parseArgs(cc, vv, op);
185      }
186      if(ta)
187           op->targetAddress = ta;
188
189      if(op->targetAddress == NULL) {
190           fprintf(stderr, "No target!\n");
191           goto badu;
192      }
193      q = op->targetAddress;
194      if(*q == '[' && (q = strchr(q, ']')) != NULL) {
195           *q++ = '\0';
196           op->targetAddress++;
197      } else
198           q = op->targetAddress;
199      if((p = strchr(q, ':')) != NULL) {
200           *p++ = 0;
201           op->port = atoi(p);
202           p = strchr(p, ',');
203      }
204      if(p || ((p = strchr(q, ',')) != NULL)) {
205           *p++ = 0;
206           op->targetPortalGroupTag = atoi(p);
207      }
208      if(op->initiatorName == 0) {
209           char  hostname[256];
210
211           if(op->iqn) {
212                if(gethostname(hostname, sizeof(hostname)) == 0)
213                     asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
214                else
215                     asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
216           }
217           else {
218                if(gethostname(hostname, sizeof(hostname)) == 0)
219                     asprintf(&op->initiatorName, "%s", hostname);
220                else
221                     asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
222           }
223      }
224      if(disco) {
225           op->sessionType = "Discovery";
226           op->targetName = 0;
227      }
228
229      fsm(op);
230
231      exit(0);
232 }