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  * $FreeBSD: src/sys/dev/iscsi/initiator/isc_subr.c,v 1.3 2009/02/14 11:34:57 rrs Exp $
27  */
28 /*
29  | iSCSI
30  | $Id: isc_subr.c,v 1.20 2006/12/01 09:10:17 danny Exp danny $
31  */
32 
33 #include "opt_iscsi_initiator.h"
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/ctype.h>
41 #include <sys/errno.h>
42 #include <sys/sysctl.h>
43 #include <sys/file.h>
44 #include <sys/uio.h>
45 #include <sys/socketvar.h>
46 #include <sys/socket.h>
47 #include <sys/protosw.h>
48 #include <sys/proc.h>
49 #include <sys/queue.h>
50 #include <sys/kthread.h>
51 #include <sys/syslog.h>
52 #include <sys/mbuf.h>
53 #include <sys/libkern.h>
54 #include <sys/eventhandler.h>
55 #include <sys/mutex2.h>
56 
57 #include <bus/cam/cam.h>
58 
59 #include "dev/disk/iscsi/initiator/iscsi.h"
60 #include "dev/disk/iscsi/initiator/iscsivar.h"
61 
62 static char *
63 i_strdupin(char *s, size_t maxlen)
64 {
65      size_t	len;
66      char	*p, *q;
67 
68      p = kmalloc(maxlen, M_ISCSI, M_WAITOK);
69      if(copyinstr(s, p, maxlen, &len)) {
70 	  kfree(p, M_ISCSI);
71 	  return NULL;
72      }
73      q = kmalloc(len, M_ISCSI, M_WAITOK);
74      bcopy(p, q, len);
75      kfree(p, M_ISCSI);
76 
77      return q;
78 }
79 
80 /*
81  | XXX: not finished coding
82  */
83 int
84 i_setopt(isc_session_t *sp, isc_opt_t *opt)
85 {
86      const int	digsize = 6;
87      size_t	len;
88      char	hdigest[digsize], ddigest[digsize];
89 
90      debug_called(8);
91      if(opt->maxRecvDataSegmentLength > 0) {
92 	  sp->opt.maxRecvDataSegmentLength = opt->maxRecvDataSegmentLength;
93 	  sdebug(2, "maxRecvDataSegmentLength=%d", sp->opt.maxRecvDataSegmentLength);
94      }
95      if(opt->maxXmitDataSegmentLength > 0) {
96 	  // danny's RFC
97 	  sp->opt.maxXmitDataSegmentLength = opt->maxXmitDataSegmentLength;
98 	  sdebug(2, "maXmitDataSegmentLength=%d", sp->opt.maxXmitDataSegmentLength);
99      }
100      if(opt->maxBurstLength != 0) {
101 	  sp->opt.maxBurstLength = opt->maxBurstLength;
102 	  sdebug(2, "maxBurstLength=%d", sp->opt.maxBurstLength);
103      }
104 
105      if(opt->targetAddress != NULL) {
106 	  if(sp->opt.targetAddress != NULL)
107 	       kfree(sp->opt.targetAddress, M_ISCSI);
108 	  sp->opt.targetAddress = i_strdupin(opt->targetAddress, 128);
109 	  sdebug(4, "opt.targetAddress='%s'", sp->opt.targetAddress);
110      }
111      if(opt->targetName != NULL) {
112 	  if(sp->opt.targetName != NULL)
113 	       kfree(sp->opt.targetName, M_ISCSI);
114 	  sp->opt.targetName = i_strdupin(opt->targetName, 128);
115 	  sdebug(4, "opt.targetName='%s'", sp->opt.targetName);
116      }
117      if(opt->initiatorName != NULL) {
118 	  if(sp->opt.initiatorName != NULL)
119 	       kfree(sp->opt.initiatorName, M_ISCSI);
120 	  sp->opt.initiatorName = i_strdupin(opt->initiatorName, 128);
121 	  sdebug(4, "opt.initiatorName='%s'", sp->opt.initiatorName);
122      }
123 
124      if(opt->maxluns > 0) {
125 	  if(opt->maxluns > ISCSI_MAX_LUNS)
126 	       sp->opt.maxluns = ISCSI_MAX_LUNS; // silently chop it down ...
127 	  sp->opt.maxluns = opt->maxluns;
128 	  sdebug(4, "opt.maxluns=%d", sp->opt.maxluns);
129      }
130 
131      /* Try to copy the userland pointer containing the header digest */
132      if(opt->headerDigest != NULL) {
133 	  if ((copyinstr(opt->headerDigest, &hdigest, digsize, &len)) != EFAULT) {
134 	       sdebug(2, "opt.headerDigest='%s'", hdigest);
135 	       if(strcmp(hdigest, "CRC32C") == 0) {
136 		    sp->hdrDigest = (digest_t *)crc32_ext;
137 		    sdebug(2, "headerDigest set");
138 	       }
139 	  }
140      }
141      /* Try to copy the userland pointer containing the data digest */
142      if(opt->dataDigest != NULL) {
143 	  if ((copyinstr(opt->dataDigest, &ddigest, digsize, &len)) != EFAULT) {
144 	       if (strcmp(ddigest, "CRC32C") == 0) {
145 		    sp->dataDigest = (digest_t *)crc32_ext;
146 		    sdebug(2, "dataDigest set");
147 	       }
148 	  }
149      }
150 
151      return 0;
152 }
153 
154 void
155 i_freeopt(isc_opt_t *opt)
156 {
157      if(opt->targetAddress != NULL) {
158 	  kfree(opt->targetAddress, M_ISCSI);
159 	  opt->targetAddress = NULL;
160      }
161      if(opt->targetName != NULL) {
162 	  kfree(opt->targetName, M_ISCSI);
163 	  opt->targetName = NULL;
164      }
165      if(opt->initiatorName != NULL) {
166 	  kfree(opt->initiatorName, M_ISCSI);
167 	  opt->initiatorName = NULL;
168      }
169 }
170