iscontrol.c revision 234233
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: head/sbin/iscontrol/iscontrol.c 234233 2012-04-13 18:21:56Z jpaetzel $"); 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 <dev/iscsi/initiator/iscsi.h> 57#include "iscontrol.h" 58 59static char version[] = "2.3.1"; // keep in sync with iscsi_initiator 60 61#define USAGE "[-v] [-d] [-c config] [-n name] [-t target] [-p pidfile]" 62#define OPTIONS "vdc:t:n:p:" 63 64token_t AuthMethods[] = { 65 {"None", NONE}, 66 {"KRB5", KRB5}, 67 {"SPKM1", SPKM1}, 68 {"SPKM2", SPKM2}, 69 {"SRP", SRP}, 70 {"CHAP", CHAP}, 71 {0, 0} 72}; 73 74token_t DigestMethods[] = { 75 {"None", 0}, 76 {"CRC32", 1}, 77 {"CRC32C", 1}, 78 {0, 0} 79}; 80 81u_char isid[6 + 6]; 82/* 83 | Default values 84 */ 85isc_opt_t opvals = { 86 .port = 3260, 87 .sockbufsize = 128, 88 .iqn = "iqn.2005-01.il.ac.huji.cs:", 89 90 .sessionType = "Normal", 91 .targetAddress = 0, 92 .targetName = 0, 93 .initiatorName = 0, 94 .authMethod = "None", 95 .headerDigest = "None,CRC32C", 96 .dataDigest = "None,CRC32C", 97 .maxConnections = 1, 98 .maxRecvDataSegmentLength = 64 * 1024, 99 .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024, 100 .maxBurstLength = 128 * 1024, 101 .firstBurstLength = 64 * 1024, // must be less than maxBurstLength 102 .defaultTime2Wait = 0, 103 .defaultTime2Retain = 0, 104 .maxOutstandingR2T = 1, 105 .errorRecoveryLevel = 0, 106 107 .dataPDUInOrder = TRUE, 108 .dataSequenceInOrder = TRUE, 109 110 .initialR2T = TRUE, 111 .immediateData = TRUE, 112}; 113 114int 115lookup(token_t *tbl, char *m) 116{ 117 token_t *tp; 118 119 for(tp = tbl; tp->name != NULL; tp++) 120 if(strcasecmp(tp->name, m) == 0) 121 return tp->val; 122 return 0; 123} 124 125int 126main(int cc, char **vv) 127{ 128 int ch, disco; 129 char *pname, *pidfile, *p, *q, *ta, *kw, *v; 130 isc_opt_t *op; 131 FILE *fd; 132 size_t n; 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 pidfile = NULL; 144 /* 145 | check for driver & controller version match 146 */ 147 n = 0; 148 if(sysctlbyname("net.iscsi_initiator.driver_version", 0, &n, 0, 0) != 0) 149 perror("sysctlbyname"); 150 v = malloc(n+1); 151 if(sysctlbyname("net.iscsi_initiator.driver_version", v, &n, 0, 0) != 0) 152 perror("sysctlbyname"); 153 154 if(strncmp(version, v, 3)) { 155 fprintf(stderr, "versions missmatch\n"); 156 exit(1); 157 } 158 159 160 while((ch = getopt(cc, vv, OPTIONS)) != -1) { 161 switch(ch) { 162 case 'v': 163 vflag++; 164 break; 165 case 'c': 166 fd = fopen(optarg, "r"); 167 if(fd == NULL) { 168 perror(optarg); 169 exit(1); 170 } 171 break; 172 case 'd': 173 disco = 1; 174 break; 175 case 't': 176 ta = optarg; 177 break; 178 case 'n': 179 kw = optarg; 180 break; 181 case 'p': 182 pidfile = optarg; 183 break; 184 default: 185 badu: 186 fprintf(stderr, "Usage: %s %s\n", pname, USAGE); 187 exit(1); 188 } 189 } 190 if(fd == NULL) 191 fd = fopen("/etc/iscsi.conf", "r"); 192 193 if(fd != NULL) { 194 parseConfig(fd, kw, op); 195 fclose(fd); 196 } 197 cc -= optind; 198 vv += optind; 199 if(cc > 0) { 200 if(vflag) 201 printf("adding '%s'\n", *vv); 202 parseArgs(cc, vv, op); 203 } 204 if(ta) 205 op->targetAddress = ta; 206 207 if(op->targetAddress == NULL) { 208 fprintf(stderr, "No target!\n"); 209 goto badu; 210 } 211 q = op->targetAddress; 212 if(*q == '[' && (q = strchr(q, ']')) != NULL) { 213 *q++ = '\0'; 214 op->targetAddress++; 215 } else 216 q = op->targetAddress; 217 if((p = strchr(q, ':')) != NULL) { 218 *p++ = 0; 219 op->port = atoi(p); 220 p = strchr(p, ','); 221 } 222 if(p || ((p = strchr(q, ',')) != NULL)) { 223 *p++ = 0; 224 op->targetPortalGroupTag = atoi(p); 225 } 226 if(op->initiatorName == 0) { 227 char hostname[256]; 228 229 if(op->iqn) { 230 if(gethostname(hostname, sizeof(hostname)) == 0) 231 asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname); 232 else 233 asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX: 234 } 235 else { 236 if(gethostname(hostname, sizeof(hostname)) == 0) 237 asprintf(&op->initiatorName, "%s", hostname); 238 else 239 asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX: 240 } 241 } 242 if(disco) { 243 op->sessionType = "Discovery"; 244 op->targetName = 0; 245 } 246 op->pidfile = pidfile; 247 fsm(op); 248 249 exit(0); 250} 251