hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/target/target_core_fabric_lib.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*******************************************************************************
23 * Filename: target_core_fabric_lib.c
34 *
....@@ -7,20 +8,6 @@
78 * (c) Copyright 2010-2013 Datera, Inc.
89 *
910 * Nicholas A. Bellinger <nab@linux-iscsi.org>
10
- *
11
- * This program is free software; you can redistribute it and/or modify
12
- * it under the terms of the GNU General Public License as published by
13
- * the Free Software Foundation; either version 2 of the License, or
14
- * (at your option) any later version.
15
- *
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU General Public License
22
- * along with this program; if not, write to the Free Software
23
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2411 *
2512 ******************************************************************************/
2613
....@@ -145,33 +132,27 @@
145132 unsigned char *buf)
146133 {
147134 u32 off = 4, padding = 0;
135
+ int isid_len;
148136 u16 len = 0;
149137
150138 spin_lock_irq(&se_nacl->nacl_sess_lock);
151139 /*
152
- * From spc4r17 Section 7.5.4.6: TransportID for initiator
153
- * ports using SCSI over iSCSI.
140
+ * Only null terminate the last field.
154141 *
155
- * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
156
- * shall contain the iSCSI name of an iSCSI initiator node (see
157
- * RFC 3720). The first ISCSI NAME field byte containing an ASCII
158
- * null character terminates the ISCSI NAME field without regard for
159
- * the specified length of the iSCSI TransportID or the contents of
160
- * the ADDITIONAL LENGTH field.
142
+ * From spc4r37 section 7.6.4.6: TransportID for initiator ports using
143
+ * SCSI over iSCSI.
144
+ *
145
+ * Table 507 TPID=0 Initiator device TransportID
146
+ *
147
+ * The null-terminated, null-padded (see 4.3.2) ISCSI NAME field shall
148
+ * contain the iSCSI name of an iSCSI initiator node (see RFC 7143).
149
+ * The first ISCSI NAME field byte containing an ASCII null character
150
+ * terminates the ISCSI NAME field without regard for the specified
151
+ * length of the iSCSI TransportID or the contents of the ADDITIONAL
152
+ * LENGTH field.
161153 */
162154 len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
163
- /*
164
- * Add Extra byte for NULL terminator
165
- */
166
- len++;
167
- /*
168
- * If there is ISID present with the registration and *format code == 1
169
- * 1, use iSCSI Initiator port TransportID format.
170
- *
171
- * Otherwise use iSCSI Initiator device TransportID format that
172
- * does not contain the ASCII encoded iSCSI Initiator iSID value
173
- * provied by the iSCSi Initiator during the iSCSI login process.
174
- */
155
+ off += len;
175156 if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
176157 /*
177158 * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
....@@ -179,8 +160,12 @@
179160 */
180161 buf[0] |= 0x40;
181162 /*
182
- * From spc4r17 Section 7.5.4.6: TransportID for initiator
183
- * ports using SCSI over iSCSI. Table 390
163
+ * From spc4r37 Section 7.6.4.6
164
+ *
165
+ * Table 508 TPID=1 Initiator port TransportID.
166
+ *
167
+ * The ISCSI NAME field shall not be null-terminated
168
+ * (see 4.3.2) and shall not be padded.
184169 *
185170 * The SEPARATOR field shall contain the five ASCII
186171 * characters ",i,0x".
....@@ -190,23 +175,24 @@
190175 * (see RFC 3720) in the form of ASCII characters that are the
191176 * hexadecimal digits converted from the binary iSCSI initiator
192177 * session identifier value. The first ISCSI INITIATOR SESSION
193
- * ID field byte containing an ASCII null character
178
+ * ID field byte containing an ASCII null character terminates
179
+ * the ISCSI INITIATOR SESSION ID field without regard for the
180
+ * specified length of the iSCSI TransportID or the contents
181
+ * of the ADDITIONAL LENGTH field.
194182 */
195
- buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
196
- buf[off+len] = 0x69; off++; /* ASCII Character: "i" */
197
- buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
198
- buf[off+len] = 0x30; off++; /* ASCII Character: "0" */
199
- buf[off+len] = 0x78; off++; /* ASCII Character: "x" */
183
+ buf[off++] = 0x2c; /* ASCII Character: "," */
184
+ buf[off++] = 0x69; /* ASCII Character: "i" */
185
+ buf[off++] = 0x2c; /* ASCII Character: "," */
186
+ buf[off++] = 0x30; /* ASCII Character: "0" */
187
+ buf[off++] = 0x78; /* ASCII Character: "x" */
200188 len += 5;
201
- buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
202
- buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
203
- buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
204
- buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
205
- buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
206
- buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
207
- buf[off+len] = '\0'; off++;
208
- len += 7;
189
+
190
+ isid_len = sprintf(buf + off, "%s", pr_reg->pr_reg_isid);
191
+ off += isid_len;
192
+ len += isid_len;
209193 }
194
+ buf[off] = '\0';
195
+ len += 1;
210196 spin_unlock_irq(&se_nacl->nacl_sess_lock);
211197 /*
212198 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
....@@ -249,7 +235,7 @@
249235 */
250236 if (pr_reg->isid_present_at_reg) {
251237 len += 5; /* For ",i,0x" ASCII separator */
252
- len += 7; /* For iSCSI Initiator Session ID + Null terminator */
238
+ len += strlen(pr_reg->pr_reg_isid);
253239 *format_code = 1;
254240 } else
255241 *format_code = 0;
....@@ -278,9 +264,7 @@
278264 char **port_nexus_ptr)
279265 {
280266 char *p;
281
- u32 tid_len, padding;
282267 int i;
283
- u16 add_len;
284268 u8 format_code = (buf[0] & 0xc0);
285269 /*
286270 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
....@@ -306,23 +290,11 @@
306290 */
307291 if (out_tid_len) {
308292 /* The shift works thanks to integer promotion rules */
309
- add_len = get_unaligned_be16(&buf[2]);
310
-
311
- tid_len = strlen(&buf[4]);
312
- tid_len += 4; /* Add four bytes for iSCSI Transport ID header */
313
- tid_len += 1; /* Add one byte for NULL terminator */
314
- padding = ((-tid_len) & 3);
315
- if (padding != 0)
316
- tid_len += padding;
317
-
318
- if ((add_len + 4) != tid_len) {
319
- pr_debug("LIO-Target Extracted add_len: %hu "
320
- "does not match calculated tid_len: %u,"
321
- " using tid_len instead\n", add_len+4, tid_len);
322
- *out_tid_len = tid_len;
323
- } else
324
- *out_tid_len = (add_len + 4);
293
+ *out_tid_len = get_unaligned_be16(&buf[2]);
294
+ /* Add four bytes for iSCSI Transport ID header */
295
+ *out_tid_len += 4;
325296 }
297
+
326298 /*
327299 * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
328300 * Session ID as defined in Table 390 - iSCSI initiator port TransportID
....@@ -347,6 +319,16 @@
347319 * iscsi_target.c:lio_sess_get_initiator_sid()
348320 */
349321 for (i = 0; i < 12; i++) {
322
+ /*
323
+ * The first ISCSI INITIATOR SESSION ID field byte
324
+ * containing an ASCII null character terminates the
325
+ * ISCSI INITIATOR SESSION ID field without regard for
326
+ * the specified length of the iSCSI TransportID or the
327
+ * contents of the ADDITIONAL LENGTH field.
328
+ */
329
+ if (*p == '\0')
330
+ break;
331
+
350332 if (isdigit(*p)) {
351333 p++;
352334 continue;
....@@ -354,7 +336,8 @@
354336 *p = tolower(*p);
355337 p++;
356338 }
357
- }
339
+ } else
340
+ *port_nexus_ptr = NULL;
358341
359342 return &buf[4];
360343 }