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/param.h> 35 #include <sys/types.h> 36 #include <sys/socket.h> 37 #include <sys/sysctl.h> 38 #include <sys/module.h> 39 #include <sys/linker.h> 40 41 #include <netinet/in.h> 42 #include <netinet/tcp.h> 43 #include <arpa/inet.h> 44 #include <sys/ioctl.h> 45 #include <netdb.h> 46 #include <stdlib.h> 47 #include <unistd.h> 48 #include <stdio.h> 49 #include <string.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <time.h> 53 #include <camlib.h> 54 55 #include "iscsi.h" 56 #include "iscontrol.h" 57 58 #define USAGE "[-dv] [-c file] [-n nickname] [-t target] [variable=value ...]" 59 #define OPTIONS "vdc:t:n:" 60 61 token_t AuthMethods[] = { 62 {"None", NONE}, 63 {"KRB5", KRB5}, 64 {"SPKM1", SPKM1}, 65 {"SPKM2", SPKM2}, 66 {"SRP", SRP}, 67 {"CHAP", CHAP}, 68 {0, 0} 69 }; 70 71 token_t DigestMethods[] = { 72 {"None", 0}, 73 {"CRC32", 1}, 74 {"CRC32C", 1}, 75 {0, 0} 76 }; 77 78 int vflag; 79 char *iscsidev; 80 81 /* 82 | Default values 83 */ 84 static isc_opt_t opvals = { 85 .port = 3260, 86 .sockbufsize = 128, 87 .iqn = "iqn.2005-01.il.ac.huji.cs:", 88 89 .sessionType = "Normal", 90 .targetAddress = 0, 91 .targetName = 0, 92 .initiatorName = 0, 93 .authMethod = "None", 94 .headerDigest = "None,CRC32C", 95 .dataDigest = "None,CRC32C", 96 .maxConnections = 1, 97 .maxRecvDataSegmentLength = 64 * 1024, 98 .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024, 99 .maxBurstLength = 128 * 1024, 100 .firstBurstLength = 64 * 1024, // must be less than maxBurstLength 101 .defaultTime2Wait = 0, 102 .defaultTime2Retain = 0, 103 .maxOutstandingR2T = 1, 104 .errorRecoveryLevel = 0, 105 106 .dataPDUInOrder = true, 107 .dataSequenceInOrder = true, 108 109 .initialR2T = true, 110 .immediateData = true, 111 }; 112 113 int 114 lookup(token_t *tbl, char *m) 115 { 116 token_t *tp; 117 118 for(tp = tbl; tp->name != NULL; tp++) 119 if(strcasecmp(tp->name, m) == 0) 120 return tp->val; 121 return 0; 122 } 123 124 int 125 main(int cc, char **vv) 126 { 127 int ch, disco; 128 char *pname, *p, *q, *ta, *kw; 129 isc_opt_t *op; 130 FILE *fd; 131 132 /* Try to load iscsi_initiator module before starting its operation */ 133 if (modfind(INITIATORMOD) < 0) { 134 if (kldload(INITIATORMOD) < 0 || modfind(INITIATORMOD) < 0) { 135 perror(INITIATORMOD ": Error while handling kernel module"); 136 return 1; 137 } 138 } 139 140 op = &opvals; 141 iscsidev = "/dev/"ISCSIDEV; 142 fd = NULL; 143 pname = vv[0]; 144 if((p = strrchr(pname, '/')) != NULL) 145 pname = p + 1; 146 147 kw = ta = NULL; 148 disco = 0; 149 150 while((ch = getopt(cc, vv, OPTIONS)) != -1) { 151 switch(ch) { 152 case 'v': 153 vflag++; 154 break; 155 case 'c': 156 fd = fopen(optarg, "r"); 157 if(fd == NULL) { 158 perror(optarg); 159 exit(1); 160 } 161 break; 162 case 'd': 163 disco = 1; 164 break; 165 case 't': 166 ta = optarg; 167 break; 168 case 'n': 169 kw = optarg; 170 break; 171 default: 172 badu: 173 fprintf(stderr, "Usage: %s %s\n", pname, USAGE); 174 exit(1); 175 } 176 } 177 if(fd == NULL) 178 fd = fopen("/etc/iscsi.conf", "r"); 179 180 if(fd != NULL) { 181 parseConfig(fd, kw, op); 182 fclose(fd); 183 } 184 cc -= optind; 185 vv += optind; 186 if(cc > 0) { 187 if(vflag) 188 printf("adding '%s'\n", *vv); 189 parseArgs(cc, vv, op); 190 } 191 if(ta) 192 op->targetAddress = ta; 193 194 if(op->targetAddress == NULL) { 195 fprintf(stderr, "No target!\n"); 196 goto badu; 197 } 198 q = op->targetAddress; 199 if(*q == '[' && (q = strchr(q, ']')) != NULL) { 200 *q++ = '\0'; 201 op->targetAddress++; 202 } else 203 q = op->targetAddress; 204 if((p = strchr(q, ':')) != NULL) { 205 *p++ = 0; 206 op->port = atoi(p); 207 p = strchr(p, ','); 208 } 209 if(p || ((p = strchr(q, ',')) != NULL)) { 210 *p++ = 0; 211 op->targetPortalGroupTag = atoi(p); 212 } 213 if(op->initiatorName == 0) { 214 char hostname[256]; 215 216 if(op->iqn) { 217 if(gethostname(hostname, sizeof(hostname)) == 0) 218 asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname); 219 else 220 asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX: 221 } 222 else { 223 if(gethostname(hostname, sizeof(hostname)) == 0) 224 asprintf(&op->initiatorName, "%s", hostname); 225 else 226 asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX: 227 } 228 } 229 if(disco) { 230 op->sessionType = "Discovery"; 231 op->targetName = 0; 232 } 233 234 fsm(op); 235 236 exit(0); 237 } 238