1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005-2011 Daniel Braniss <danny@cs.huji.ac.il>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29/*
30 | iSCSI
31 | $Id: isc_subr.c 560 2009-05-07 07:37:49Z danny $
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36
37#include "opt_iscsi_initiator.h"
38
39#include <sys/param.h>
40#include <sys/kernel.h>
41#include <sys/conf.h>
42#include <sys/gsb_crc32.h>
43#include <sys/systm.h>
44#include <sys/malloc.h>
45#include <sys/ctype.h>
46#include <sys/errno.h>
47#include <sys/sysctl.h>
48#include <sys/file.h>
49#include <sys/uio.h>
50#include <sys/socketvar.h>
51#include <sys/socket.h>
52#include <sys/protosw.h>
53#include <sys/proc.h>
54#include <sys/ioccom.h>
55#include <sys/queue.h>
56#include <sys/kthread.h>
57#include <sys/syslog.h>
58#include <sys/mbuf.h>
59#include <sys/libkern.h>
60#include <vm/uma.h>
61
62#include <dev/iscsi_initiator/iscsi.h>
63#include <dev/iscsi_initiator/iscsivar.h>
64
65static MALLOC_DEFINE(M_ISC, "iSC", "iSCSI driver options");
66
67static char *
68i_strdupin(char *s, size_t maxlen)
69{
70     size_t	len;
71     char	*p, *q;
72
73     p = malloc(maxlen, M_ISC, M_WAITOK);
74     if(copyinstr(s, p, maxlen, &len)) {
75	  free(p, M_ISC);
76	  return NULL;
77     }
78     q = malloc(len, M_ISC, M_WAITOK);
79     bcopy(p, q, len);
80     free(p, M_ISC);
81
82     return q;
83}
84
85static uint32_t
86i_crc32c(const void *buf, size_t size, uint32_t crc)
87{
88     crc = crc ^ 0xffffffff;
89     crc = calculate_crc32c(crc, buf, size);
90     crc = crc ^ 0xffffffff;
91     return crc;
92}
93
94/*
95 | XXX: not finished coding
96 */
97int
98i_setopt(isc_session_t *sp, isc_opt_t *opt)
99{
100     char buf[16];
101     int error;
102
103     if(opt->maxRecvDataSegmentLength > 0) {
104	  sp->opt.maxRecvDataSegmentLength = opt->maxRecvDataSegmentLength;
105	  sdebug(2, "maxRecvDataSegmentLength=%d", sp->opt.maxRecvDataSegmentLength);
106     }
107     if(opt->maxXmitDataSegmentLength > 0) {
108	  // danny's RFC
109	  sp->opt.maxXmitDataSegmentLength = opt->maxXmitDataSegmentLength;
110	  sdebug(2, "opt.maXmitDataSegmentLength=%d", sp->opt.maxXmitDataSegmentLength);
111     }
112     if(opt->maxBurstLength != 0) {
113	  sp->opt.maxBurstLength = opt->maxBurstLength;
114	  sdebug(2, "opt.maxBurstLength=%d", sp->opt.maxBurstLength);
115     }
116
117     if(opt->targetAddress != NULL) {
118	  if(sp->opt.targetAddress != NULL)
119	       free(sp->opt.targetAddress, M_ISC);
120	  sp->opt.targetAddress = i_strdupin(opt->targetAddress, 128);
121	  sdebug(2, "opt.targetAddress='%s'", sp->opt.targetAddress);
122     }
123     if(opt->targetName != NULL) {
124	  if(sp->opt.targetName != NULL)
125	       free(sp->opt.targetName, M_ISC);
126	  sp->opt.targetName = i_strdupin(opt->targetName, 128);
127	  sdebug(2, "opt.targetName='%s'", sp->opt.targetName);
128     }
129     if(opt->initiatorName != NULL) {
130	  if(sp->opt.initiatorName != NULL)
131	       free(sp->opt.initiatorName, M_ISC);
132	  sp->opt.initiatorName = i_strdupin(opt->initiatorName, 128);
133	  sdebug(2, "opt.initiatorName='%s'", sp->opt.initiatorName);
134     }
135
136     if(opt->maxluns > 0) {
137	  if(opt->maxluns > ISCSI_MAX_LUNS)
138	       sp->opt.maxluns = ISCSI_MAX_LUNS; // silently chop it down ...
139	  sp->opt.maxluns = opt->maxluns;
140	  sdebug(2, "opt.maxluns=%d", sp->opt.maxluns);
141     }
142
143     if(opt->headerDigest != NULL) {
144	  error = copyinstr(opt->headerDigest, buf, sizeof(buf), NULL);
145	  if (error != 0)
146	       return (error);
147	  sdebug(2, "opt.headerDigest='%s'", buf);
148	  if(strcmp(buf, "CRC32C") == 0) {
149	       sp->hdrDigest = (digest_t *)i_crc32c;
150	       sdebug(2, "opt.headerDigest set");
151	  }
152     }
153     if(opt->dataDigest != NULL) {
154	  error = copyinstr(opt->dataDigest, buf, sizeof(buf), NULL);
155	  if (error != 0)
156	       return (error);
157	  sdebug(2, "opt.dataDigest='%s'", opt->dataDigest);
158	  if(strcmp(buf, "CRC32C") == 0) {
159	       sp->dataDigest = (digest_t *)i_crc32c;
160	       sdebug(2, "opt.dataDigest set");
161	  }
162     }
163
164     return 0;
165}
166
167void
168i_freeopt(isc_opt_t *opt)
169{
170     debug_called(8);
171
172     if(opt->targetAddress != NULL) {
173	  free(opt->targetAddress, M_ISC);
174	  opt->targetAddress = NULL;
175     }
176     if(opt->targetName != NULL) {
177	  free(opt->targetName, M_ISC);
178	  opt->targetName = NULL;
179     }
180     if(opt->initiatorName != NULL) {
181	  free(opt->initiatorName, M_ISC);
182	  opt->initiatorName = NULL;
183     }
184}
185