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