]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/iscontrol/iscontrol.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sbin / iscontrol / iscontrol.c
1 /*-
2  * Copyright (c) 2005 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 //#include "pdu.h"
59
60 #define USAGE "[-v] [-d] [-c config] [-n name] [-t target] "
61 #define OPTIONS "vdc:t:n:"
62
63 #ifndef DEBUG
64 //int   vflag;
65 #endif
66
67 token_t AuthMethods[] = {
68      {"None",   NONE},
69      {"KRB5",   KRB5},
70      {"SPKM1",  SPKM1},
71      {"SPKM2",  SPKM2},
72      {"SRP",    SRP},
73      {"CHAP",   CHAP},
74      {0}
75 };
76
77 token_t DigestMethods[] = {
78      {"None",   0},
79      {"CRC32",  1},
80      {"CRC32C", 1},
81      {0}
82 };
83
84 u_char  isid[6 + 6];
85 /*
86  | Default values
87  */
88 isc_opt_t opvals = {
89      .port                      = 3260,
90      .sockbufsize               = 128,
91      .iqn                       = "iqn.2005-01.il.ac.huji.cs:",
92
93      .sessionType               = "Normal",
94      .targetAddress             = 0,
95      .targetName                = 0,
96      .initiatorName             = 0,
97      .authMethod                = "None",
98      .headerDigest              = "None,CRC32C",
99      .dataDigest                = "None,CRC32C",
100      .maxConnections            = 1,
101      .maxRecvDataSegmentLength  = 64 * 1024,
102      .maxXmitDataSegmentLength  = 8 * 1024, // 64 * 1024,
103      .maxBurstLength            = 128 * 1024,
104      .firstBurstLength          = 64 * 1024, // must be less than maxBurstLength
105      .defaultTime2Wait          = 0,
106      .defaultTime2Retain        = 0,
107      .maxOutstandingR2T         = 1,
108      .errorRecoveryLevel        = 0,
109
110      .dataPDUInOrder            = TRUE,
111      .dataSequenceInOrder       = TRUE,
112
113      .initialR2T                = TRUE,
114      .immediateData             = TRUE,
115 };
116
117 int
118 lookup(token_t *tbl, char *m)
119 {
120      token_t    *tp;
121
122      for(tp = tbl; tp->name != NULL; tp++)
123           if(strcasecmp(tp->name, m) == 0)
124                return tp->val;
125      return 0;
126 }
127
128 int
129 main(int cc, char **vv)
130 {
131      int        ch, disco;
132      char       *pname, *p, *ta, *kw;
133      isc_opt_t  *op;
134      FILE       *fd;
135
136      op = &opvals;
137      iscsidev = "/dev/"ISCSIDEV;
138      fd = NULL;
139      pname = vv[0];
140      if((p = strrchr(pname, '/')) != NULL)
141           pname = p + 1;
142
143      kw = ta = 0;
144      disco = 0;
145
146      while((ch = getopt(cc, vv, OPTIONS)) != -1) {
147           switch(ch) {
148           case 'v':
149                vflag++;
150                break;
151           case 'c':
152                fd = fopen(optarg, "r");
153                if(fd == NULL) {
154                     perror(optarg);
155                     exit(1);
156                }
157                break;
158           case 'd':
159                disco = 1;
160                break;
161           case 't':
162                ta = optarg;
163                break;
164           case 'n':
165                kw = optarg;
166                break;
167           default:
168           badu:
169                fprintf(stderr, "Usage: %s %s\n", pname, USAGE);
170                exit(1);
171           }
172      }
173      if(fd == NULL)
174           fd = fopen("/etc/iscsi.conf", "r");
175
176      if(fd != NULL) {
177           parseConfig(fd, kw, op);
178           fclose(fd);
179      }
180      cc -= optind;
181      vv += optind;
182      if(cc > 0) {
183           if(vflag)
184                printf("adding '%s'\n", *vv);
185           parseArgs(cc, vv, op);
186      }
187      if(ta)
188           op->targetAddress = ta;
189
190      if(op->targetAddress == NULL) {
191           fprintf(stderr, "No target!\n");
192           goto badu;
193      }
194      if((p = strchr(op->targetAddress, ':')) != NULL) {
195           *p++ = 0;
196           op->port = atoi(p);
197           p = strchr(p, ',');
198      }
199      if(p || ((p = strchr(op->targetAddress, ',')) != NULL)) {
200           *p++ = 0;
201           op->targetPortalGroupTag = atoi(p);
202      }
203      if(op->initiatorName == 0) {
204           char  hostname[256];
205
206           if(op->iqn) {
207                if(gethostname(hostname, sizeof(hostname)) == 0)
208                     asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
209                else
210                     asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
211           }
212           else {
213                if(gethostname(hostname, sizeof(hostname)) == 0)
214                     asprintf(&op->initiatorName, "%s", hostname);
215                else
216                     asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
217           }
218      }
219      if(disco) {
220           op->sessionType = "Discovery";
221           op->targetName = 0;
222      }
223
224      fsm(op);
225
226      exit(0);
227 }