Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*******************************************************************************
3 * Filename: target_core_fabric_lib.c
4 *
5 * This file contains generic high level protocol identifier and PR
6 * handlers for TCM fabric modules
7 *
8 * (c) Copyright 2010-2013 Datera, Inc.
9 *
10 * Nicholas A. Bellinger <nab@linux-iscsi.org>
11 *
12 ******************************************************************************/
13
14/*
15 * See SPC4, section 7.5 "Protocol specific parameters" for details
16 * on the formats implemented in this file.
17 */
18
19#include <linux/kernel.h>
20#include <linux/string.h>
21#include <linux/ctype.h>
22#include <linux/spinlock.h>
23#include <linux/export.h>
24#include <asm/unaligned.h>
25
26#include <scsi/scsi_proto.h>
27
28#include <target/target_core_base.h>
29#include <target/target_core_fabric.h>
30
31#include "target_core_internal.h"
32#include "target_core_pr.h"
33
34
35static int sas_get_pr_transport_id(
36 struct se_node_acl *nacl,
37 int *format_code,
38 unsigned char *buf)
39{
40 int ret;
41
42 /* Skip over 'naa. prefix */
43 ret = hex2bin(&buf[4], &nacl->initiatorname[4], 8);
44 if (ret) {
45 pr_debug("%s: invalid hex string\n", __func__);
46 return ret;
47 }
48
49 return 24;
50}
51
52static int fc_get_pr_transport_id(
53 struct se_node_acl *se_nacl,
54 int *format_code,
55 unsigned char *buf)
56{
57 unsigned char *ptr;
58 int i, ret;
59 u32 off = 8;
60
61 /*
62 * We convert the ASCII formatted N Port name into a binary
63 * encoded TransportID.
64 */
65 ptr = &se_nacl->initiatorname[0];
66 for (i = 0; i < 24; ) {
67 if (!strncmp(&ptr[i], ":", 1)) {
68 i++;
69 continue;
70 }
71 ret = hex2bin(&buf[off++], &ptr[i], 1);
72 if (ret < 0) {
73 pr_debug("%s: invalid hex string\n", __func__);
74 return ret;
75 }
76 i += 2;
77 }
78 /*
79 * The FC Transport ID is a hardcoded 24-byte length
80 */
81 return 24;
82}
83
84static int sbp_get_pr_transport_id(
85 struct se_node_acl *nacl,
86 int *format_code,
87 unsigned char *buf)
88{
89 int ret;
90
91 ret = hex2bin(&buf[8], nacl->initiatorname, 8);
92 if (ret) {
93 pr_debug("%s: invalid hex string\n", __func__);
94 return ret;
95 }
96
97 return 24;
98}
99
100static int srp_get_pr_transport_id(
101 struct se_node_acl *nacl,
102 int *format_code,
103 unsigned char *buf)
104{
105 const char *p;
106 unsigned len, count, leading_zero_bytes;
107 int rc;
108
109 p = nacl->initiatorname;
110 if (strncasecmp(p, "0x", 2) == 0)
111 p += 2;
112 len = strlen(p);
113 if (len % 2)
114 return -EINVAL;
115
116 count = min(len / 2, 16U);
117 leading_zero_bytes = 16 - count;
118 memset(buf + 8, 0, leading_zero_bytes);
119 rc = hex2bin(buf + 8 + leading_zero_bytes, p, count);
120 if (rc < 0) {
121 pr_debug("hex2bin failed for %s: %d\n", __func__, rc);
122 return rc;
123 }
124
125 return 24;
126}
127
128static int iscsi_get_pr_transport_id(
129 struct se_node_acl *se_nacl,
130 struct t10_pr_registration *pr_reg,
131 int *format_code,
132 unsigned char *buf)
133{
134 u32 off = 4, padding = 0;
135 u16 len = 0;
136
137 spin_lock_irq(&se_nacl->nacl_sess_lock);
138 /*
139 * From spc4r17 Section 7.5.4.6: TransportID for initiator
140 * ports using SCSI over iSCSI.
141 *
142 * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
143 * shall contain the iSCSI name of an iSCSI initiator node (see
144 * RFC 3720). The first ISCSI NAME field byte containing an ASCII
145 * null character terminates the ISCSI NAME field without regard for
146 * the specified length of the iSCSI TransportID or the contents of
147 * the ADDITIONAL LENGTH field.
148 */
149 len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
150 /*
151 * Add Extra byte for NULL terminator
152 */
153 len++;
154 /*
155 * If there is ISID present with the registration and *format code == 1
156 * 1, use iSCSI Initiator port TransportID format.
157 *
158 * Otherwise use iSCSI Initiator device TransportID format that
159 * does not contain the ASCII encoded iSCSI Initiator iSID value
160 * provied by the iSCSi Initiator during the iSCSI login process.
161 */
162 if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
163 /*
164 * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
165 * format.
166 */
167 buf[0] |= 0x40;
168 /*
169 * From spc4r17 Section 7.5.4.6: TransportID for initiator
170 * ports using SCSI over iSCSI. Table 390
171 *
172 * The SEPARATOR field shall contain the five ASCII
173 * characters ",i,0x".
174 *
175 * The null-terminated, null-padded ISCSI INITIATOR SESSION ID
176 * field shall contain the iSCSI initiator session identifier
177 * (see RFC 3720) in the form of ASCII characters that are the
178 * hexadecimal digits converted from the binary iSCSI initiator
179 * session identifier value. The first ISCSI INITIATOR SESSION
180 * ID field byte containing an ASCII null character
181 */
182 buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
183 buf[off+len] = 0x69; off++; /* ASCII Character: "i" */
184 buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
185 buf[off+len] = 0x30; off++; /* ASCII Character: "0" */
186 buf[off+len] = 0x78; off++; /* ASCII Character: "x" */
187 len += 5;
188 buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
189 buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
190 buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
191 buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
192 buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
193 buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
194 buf[off+len] = '\0'; off++;
195 len += 7;
196 }
197 spin_unlock_irq(&se_nacl->nacl_sess_lock);
198 /*
199 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
200 * in the TransportID. The additional length shall be at least 20 and
201 * shall be a multiple of four.
202 */
203 padding = ((-len) & 3);
204 if (padding != 0)
205 len += padding;
206
207 put_unaligned_be16(len, &buf[2]);
208 /*
209 * Increment value for total payload + header length for
210 * full status descriptor
211 */
212 len += 4;
213
214 return len;
215}
216
217static int iscsi_get_pr_transport_id_len(
218 struct se_node_acl *se_nacl,
219 struct t10_pr_registration *pr_reg,
220 int *format_code)
221{
222 u32 len = 0, padding = 0;
223
224 spin_lock_irq(&se_nacl->nacl_sess_lock);
225 len = strlen(se_nacl->initiatorname);
226 /*
227 * Add extra byte for NULL terminator
228 */
229 len++;
230 /*
231 * If there is ISID present with the registration, use format code:
232 * 01b: iSCSI Initiator port TransportID format
233 *
234 * If there is not an active iSCSI session, use format code:
235 * 00b: iSCSI Initiator device TransportID format
236 */
237 if (pr_reg->isid_present_at_reg) {
238 len += 5; /* For ",i,0x" ASCII separator */
239 len += 7; /* For iSCSI Initiator Session ID + Null terminator */
240 *format_code = 1;
241 } else
242 *format_code = 0;
243 spin_unlock_irq(&se_nacl->nacl_sess_lock);
244 /*
245 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
246 * in the TransportID. The additional length shall be at least 20 and
247 * shall be a multiple of four.
248 */
249 padding = ((-len) & 3);
250 if (padding != 0)
251 len += padding;
252 /*
253 * Increment value for total payload + header length for
254 * full status descriptor
255 */
256 len += 4;
257
258 return len;
259}
260
261static char *iscsi_parse_pr_out_transport_id(
262 struct se_portal_group *se_tpg,
263 char *buf,
264 u32 *out_tid_len,
265 char **port_nexus_ptr)
266{
267 char *p;
268 u32 tid_len, padding;
269 int i;
270 u16 add_len;
271 u8 format_code = (buf[0] & 0xc0);
272 /*
273 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
274 *
275 * TransportID for initiator ports using SCSI over iSCSI,
276 * from Table 388 -- iSCSI TransportID formats.
277 *
278 * 00b Initiator port is identified using the world wide unique
279 * SCSI device name of the iSCSI initiator
280 * device containing the initiator port (see table 389).
281 * 01b Initiator port is identified using the world wide unique
282 * initiator port identifier (see table 390).10b to 11b
283 * Reserved
284 */
285 if ((format_code != 0x00) && (format_code != 0x40)) {
286 pr_err("Illegal format code: 0x%02x for iSCSI"
287 " Initiator Transport ID\n", format_code);
288 return NULL;
289 }
290 /*
291 * If the caller wants the TransportID Length, we set that value for the
292 * entire iSCSI Tarnsport ID now.
293 */
294 if (out_tid_len) {
295 /* The shift works thanks to integer promotion rules */
296 add_len = get_unaligned_be16(&buf[2]);
297
298 tid_len = strlen(&buf[4]);
299 tid_len += 4; /* Add four bytes for iSCSI Transport ID header */
300 tid_len += 1; /* Add one byte for NULL terminator */
301 padding = ((-tid_len) & 3);
302 if (padding != 0)
303 tid_len += padding;
304
305 if ((add_len + 4) != tid_len) {
306 pr_debug("LIO-Target Extracted add_len: %hu "
307 "does not match calculated tid_len: %u,"
308 " using tid_len instead\n", add_len+4, tid_len);
309 *out_tid_len = tid_len;
310 } else
311 *out_tid_len = (add_len + 4);
312 }
313 /*
314 * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
315 * Session ID as defined in Table 390 - iSCSI initiator port TransportID
316 * format.
317 */
318 if (format_code == 0x40) {
319 p = strstr(&buf[4], ",i,0x");
320 if (!p) {
321 pr_err("Unable to locate \",i,0x\" separator"
322 " for Initiator port identifier: %s\n",
323 &buf[4]);
324 return NULL;
325 }
326 *p = '\0'; /* Terminate iSCSI Name */
327 p += 5; /* Skip over ",i,0x" separator */
328
329 *port_nexus_ptr = p;
330 /*
331 * Go ahead and do the lower case conversion of the received
332 * 12 ASCII characters representing the ISID in the TransportID
333 * for comparison against the running iSCSI session's ISID from
334 * iscsi_target.c:lio_sess_get_initiator_sid()
335 */
336 for (i = 0; i < 12; i++) {
337 if (isdigit(*p)) {
338 p++;
339 continue;
340 }
341 *p = tolower(*p);
342 p++;
343 }
344 }
345
346 return &buf[4];
347}
348
349int target_get_pr_transport_id_len(struct se_node_acl *nacl,
350 struct t10_pr_registration *pr_reg, int *format_code)
351{
352 switch (nacl->se_tpg->proto_id) {
353 case SCSI_PROTOCOL_FCP:
354 case SCSI_PROTOCOL_SBP:
355 case SCSI_PROTOCOL_SRP:
356 case SCSI_PROTOCOL_SAS:
357 break;
358 case SCSI_PROTOCOL_ISCSI:
359 return iscsi_get_pr_transport_id_len(nacl, pr_reg, format_code);
360 default:
361 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
362 return -EINVAL;
363 }
364
365 /*
366 * Most transports use a fixed length 24 byte identifier.
367 */
368 *format_code = 0;
369 return 24;
370}
371
372int target_get_pr_transport_id(struct se_node_acl *nacl,
373 struct t10_pr_registration *pr_reg, int *format_code,
374 unsigned char *buf)
375{
376 switch (nacl->se_tpg->proto_id) {
377 case SCSI_PROTOCOL_SAS:
378 return sas_get_pr_transport_id(nacl, format_code, buf);
379 case SCSI_PROTOCOL_SBP:
380 return sbp_get_pr_transport_id(nacl, format_code, buf);
381 case SCSI_PROTOCOL_SRP:
382 return srp_get_pr_transport_id(nacl, format_code, buf);
383 case SCSI_PROTOCOL_FCP:
384 return fc_get_pr_transport_id(nacl, format_code, buf);
385 case SCSI_PROTOCOL_ISCSI:
386 return iscsi_get_pr_transport_id(nacl, pr_reg, format_code,
387 buf);
388 default:
389 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
390 return -EINVAL;
391 }
392}
393
394const char *target_parse_pr_out_transport_id(struct se_portal_group *tpg,
395 char *buf, u32 *out_tid_len, char **port_nexus_ptr)
396{
397 u32 offset;
398
399 switch (tpg->proto_id) {
400 case SCSI_PROTOCOL_SAS:
401 /*
402 * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID
403 * for initiator ports using SCSI over SAS Serial SCSI Protocol.
404 */
405 offset = 4;
406 break;
407 case SCSI_PROTOCOL_SBP:
408 case SCSI_PROTOCOL_SRP:
409 case SCSI_PROTOCOL_FCP:
410 offset = 8;
411 break;
412 case SCSI_PROTOCOL_ISCSI:
413 return iscsi_parse_pr_out_transport_id(tpg, buf, out_tid_len,
414 port_nexus_ptr);
415 default:
416 pr_err("Unknown proto_id: 0x%02x\n", tpg->proto_id);
417 return NULL;
418 }
419
420 *port_nexus_ptr = NULL;
421 *out_tid_len = 24;
422 return buf + offset;
423}
1/*******************************************************************************
2 * Filename: target_core_fabric_lib.c
3 *
4 * This file contains generic high level protocol identifier and PR
5 * handlers for TCM fabric modules
6 *
7 * (c) Copyright 2010-2013 Datera, Inc.
8 *
9 * 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.
24 *
25 ******************************************************************************/
26
27#include <linux/kernel.h>
28#include <linux/string.h>
29#include <linux/ctype.h>
30#include <linux/spinlock.h>
31#include <linux/export.h>
32#include <scsi/scsi.h>
33#include <scsi/scsi_cmnd.h>
34
35#include <target/target_core_base.h>
36#include <target/target_core_fabric.h>
37#include <target/target_core_configfs.h>
38
39#include "target_core_internal.h"
40#include "target_core_pr.h"
41
42/*
43 * Handlers for Serial Attached SCSI (SAS)
44 */
45u8 sas_get_fabric_proto_ident(struct se_portal_group *se_tpg)
46{
47 /*
48 * Return a SAS Serial SCSI Protocol identifier for loopback operations
49 * This is defined in section 7.5.1 Table 362 in spc4r17
50 */
51 return 0x6;
52}
53EXPORT_SYMBOL(sas_get_fabric_proto_ident);
54
55u32 sas_get_pr_transport_id(
56 struct se_portal_group *se_tpg,
57 struct se_node_acl *se_nacl,
58 struct t10_pr_registration *pr_reg,
59 int *format_code,
60 unsigned char *buf)
61{
62 unsigned char *ptr;
63 int ret;
64
65 /*
66 * Set PROTOCOL IDENTIFIER to 6h for SAS
67 */
68 buf[0] = 0x06;
69 /*
70 * From spc4r17, 7.5.4.7 TransportID for initiator ports using SCSI
71 * over SAS Serial SCSI Protocol
72 */
73 ptr = &se_nacl->initiatorname[4]; /* Skip over 'naa. prefix */
74
75 ret = hex2bin(&buf[4], ptr, 8);
76 if (ret < 0)
77 pr_debug("sas transport_id: invalid hex string\n");
78
79 /*
80 * The SAS Transport ID is a hardcoded 24-byte length
81 */
82 return 24;
83}
84EXPORT_SYMBOL(sas_get_pr_transport_id);
85
86u32 sas_get_pr_transport_id_len(
87 struct se_portal_group *se_tpg,
88 struct se_node_acl *se_nacl,
89 struct t10_pr_registration *pr_reg,
90 int *format_code)
91{
92 *format_code = 0;
93 /*
94 * From spc4r17, 7.5.4.7 TransportID for initiator ports using SCSI
95 * over SAS Serial SCSI Protocol
96 *
97 * The SAS Transport ID is a hardcoded 24-byte length
98 */
99 return 24;
100}
101EXPORT_SYMBOL(sas_get_pr_transport_id_len);
102
103/*
104 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
105 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
106 */
107char *sas_parse_pr_out_transport_id(
108 struct se_portal_group *se_tpg,
109 const char *buf,
110 u32 *out_tid_len,
111 char **port_nexus_ptr)
112{
113 /*
114 * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID
115 * for initiator ports using SCSI over SAS Serial SCSI Protocol
116 *
117 * The TransportID for a SAS Initiator Port is of fixed size of
118 * 24 bytes, and SAS does not contain a I_T nexus identifier,
119 * so we return the **port_nexus_ptr set to NULL.
120 */
121 *port_nexus_ptr = NULL;
122 *out_tid_len = 24;
123
124 return (char *)&buf[4];
125}
126EXPORT_SYMBOL(sas_parse_pr_out_transport_id);
127
128/*
129 * Handlers for Fibre Channel Protocol (FCP)
130 */
131u8 fc_get_fabric_proto_ident(struct se_portal_group *se_tpg)
132{
133 return 0x0; /* 0 = fcp-2 per SPC4 section 7.5.1 */
134}
135EXPORT_SYMBOL(fc_get_fabric_proto_ident);
136
137u32 fc_get_pr_transport_id_len(
138 struct se_portal_group *se_tpg,
139 struct se_node_acl *se_nacl,
140 struct t10_pr_registration *pr_reg,
141 int *format_code)
142{
143 *format_code = 0;
144 /*
145 * The FC Transport ID is a hardcoded 24-byte length
146 */
147 return 24;
148}
149EXPORT_SYMBOL(fc_get_pr_transport_id_len);
150
151u32 fc_get_pr_transport_id(
152 struct se_portal_group *se_tpg,
153 struct se_node_acl *se_nacl,
154 struct t10_pr_registration *pr_reg,
155 int *format_code,
156 unsigned char *buf)
157{
158 unsigned char *ptr;
159 int i, ret;
160 u32 off = 8;
161
162 /*
163 * PROTOCOL IDENTIFIER is 0h for FCP-2
164 *
165 * From spc4r17, 7.5.4.2 TransportID for initiator ports using
166 * SCSI over Fibre Channel
167 *
168 * We convert the ASCII formatted N Port name into a binary
169 * encoded TransportID.
170 */
171 ptr = &se_nacl->initiatorname[0];
172
173 for (i = 0; i < 24; ) {
174 if (!strncmp(&ptr[i], ":", 1)) {
175 i++;
176 continue;
177 }
178 ret = hex2bin(&buf[off++], &ptr[i], 1);
179 if (ret < 0)
180 pr_debug("fc transport_id: invalid hex string\n");
181 i += 2;
182 }
183 /*
184 * The FC Transport ID is a hardcoded 24-byte length
185 */
186 return 24;
187}
188EXPORT_SYMBOL(fc_get_pr_transport_id);
189
190char *fc_parse_pr_out_transport_id(
191 struct se_portal_group *se_tpg,
192 const char *buf,
193 u32 *out_tid_len,
194 char **port_nexus_ptr)
195{
196 /*
197 * The TransportID for a FC N Port is of fixed size of
198 * 24 bytes, and FC does not contain a I_T nexus identifier,
199 * so we return the **port_nexus_ptr set to NULL.
200 */
201 *port_nexus_ptr = NULL;
202 *out_tid_len = 24;
203
204 return (char *)&buf[8];
205}
206EXPORT_SYMBOL(fc_parse_pr_out_transport_id);
207
208/*
209 * Handlers for Internet Small Computer Systems Interface (iSCSI)
210 */
211
212u8 iscsi_get_fabric_proto_ident(struct se_portal_group *se_tpg)
213{
214 /*
215 * This value is defined for "Internet SCSI (iSCSI)"
216 * in spc4r17 section 7.5.1 Table 362
217 */
218 return 0x5;
219}
220EXPORT_SYMBOL(iscsi_get_fabric_proto_ident);
221
222u32 iscsi_get_pr_transport_id(
223 struct se_portal_group *se_tpg,
224 struct se_node_acl *se_nacl,
225 struct t10_pr_registration *pr_reg,
226 int *format_code,
227 unsigned char *buf)
228{
229 u32 off = 4, padding = 0;
230 u16 len = 0;
231
232 spin_lock_irq(&se_nacl->nacl_sess_lock);
233 /*
234 * Set PROTOCOL IDENTIFIER to 5h for iSCSI
235 */
236 buf[0] = 0x05;
237 /*
238 * From spc4r17 Section 7.5.4.6: TransportID for initiator
239 * ports using SCSI over iSCSI.
240 *
241 * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
242 * shall contain the iSCSI name of an iSCSI initiator node (see
243 * RFC 3720). The first ISCSI NAME field byte containing an ASCII
244 * null character terminates the ISCSI NAME field without regard for
245 * the specified length of the iSCSI TransportID or the contents of
246 * the ADDITIONAL LENGTH field.
247 */
248 len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
249 /*
250 * Add Extra byte for NULL terminator
251 */
252 len++;
253 /*
254 * If there is ISID present with the registration and *format code == 1
255 * 1, use iSCSI Initiator port TransportID format.
256 *
257 * Otherwise use iSCSI Initiator device TransportID format that
258 * does not contain the ASCII encoded iSCSI Initiator iSID value
259 * provied by the iSCSi Initiator during the iSCSI login process.
260 */
261 if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
262 /*
263 * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
264 * format.
265 */
266 buf[0] |= 0x40;
267 /*
268 * From spc4r17 Section 7.5.4.6: TransportID for initiator
269 * ports using SCSI over iSCSI. Table 390
270 *
271 * The SEPARATOR field shall contain the five ASCII
272 * characters ",i,0x".
273 *
274 * The null-terminated, null-padded ISCSI INITIATOR SESSION ID
275 * field shall contain the iSCSI initiator session identifier
276 * (see RFC 3720) in the form of ASCII characters that are the
277 * hexadecimal digits converted from the binary iSCSI initiator
278 * session identifier value. The first ISCSI INITIATOR SESSION
279 * ID field byte containing an ASCII null character
280 */
281 buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
282 buf[off+len] = 0x69; off++; /* ASCII Character: "i" */
283 buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
284 buf[off+len] = 0x30; off++; /* ASCII Character: "0" */
285 buf[off+len] = 0x78; off++; /* ASCII Character: "x" */
286 len += 5;
287 buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
288 buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
289 buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
290 buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
291 buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
292 buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
293 buf[off+len] = '\0'; off++;
294 len += 7;
295 }
296 spin_unlock_irq(&se_nacl->nacl_sess_lock);
297 /*
298 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
299 * in the TransportID. The additional length shall be at least 20 and
300 * shall be a multiple of four.
301 */
302 padding = ((-len) & 3);
303 if (padding != 0)
304 len += padding;
305
306 buf[2] = ((len >> 8) & 0xff);
307 buf[3] = (len & 0xff);
308 /*
309 * Increment value for total payload + header length for
310 * full status descriptor
311 */
312 len += 4;
313
314 return len;
315}
316EXPORT_SYMBOL(iscsi_get_pr_transport_id);
317
318u32 iscsi_get_pr_transport_id_len(
319 struct se_portal_group *se_tpg,
320 struct se_node_acl *se_nacl,
321 struct t10_pr_registration *pr_reg,
322 int *format_code)
323{
324 u32 len = 0, padding = 0;
325
326 spin_lock_irq(&se_nacl->nacl_sess_lock);
327 len = strlen(se_nacl->initiatorname);
328 /*
329 * Add extra byte for NULL terminator
330 */
331 len++;
332 /*
333 * If there is ISID present with the registration, use format code:
334 * 01b: iSCSI Initiator port TransportID format
335 *
336 * If there is not an active iSCSI session, use format code:
337 * 00b: iSCSI Initiator device TransportID format
338 */
339 if (pr_reg->isid_present_at_reg) {
340 len += 5; /* For ",i,0x" ASCII separator */
341 len += 7; /* For iSCSI Initiator Session ID + Null terminator */
342 *format_code = 1;
343 } else
344 *format_code = 0;
345 spin_unlock_irq(&se_nacl->nacl_sess_lock);
346 /*
347 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
348 * in the TransportID. The additional length shall be at least 20 and
349 * shall be a multiple of four.
350 */
351 padding = ((-len) & 3);
352 if (padding != 0)
353 len += padding;
354 /*
355 * Increment value for total payload + header length for
356 * full status descriptor
357 */
358 len += 4;
359
360 return len;
361}
362EXPORT_SYMBOL(iscsi_get_pr_transport_id_len);
363
364char *iscsi_parse_pr_out_transport_id(
365 struct se_portal_group *se_tpg,
366 const char *buf,
367 u32 *out_tid_len,
368 char **port_nexus_ptr)
369{
370 char *p;
371 u32 tid_len, padding;
372 int i;
373 u16 add_len;
374 u8 format_code = (buf[0] & 0xc0);
375 /*
376 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
377 *
378 * TransportID for initiator ports using SCSI over iSCSI,
379 * from Table 388 -- iSCSI TransportID formats.
380 *
381 * 00b Initiator port is identified using the world wide unique
382 * SCSI device name of the iSCSI initiator
383 * device containing the initiator port (see table 389).
384 * 01b Initiator port is identified using the world wide unique
385 * initiator port identifier (see table 390).10b to 11b
386 * Reserved
387 */
388 if ((format_code != 0x00) && (format_code != 0x40)) {
389 pr_err("Illegal format code: 0x%02x for iSCSI"
390 " Initiator Transport ID\n", format_code);
391 return NULL;
392 }
393 /*
394 * If the caller wants the TransportID Length, we set that value for the
395 * entire iSCSI Tarnsport ID now.
396 */
397 if (out_tid_len != NULL) {
398 add_len = ((buf[2] >> 8) & 0xff);
399 add_len |= (buf[3] & 0xff);
400
401 tid_len = strlen(&buf[4]);
402 tid_len += 4; /* Add four bytes for iSCSI Transport ID header */
403 tid_len += 1; /* Add one byte for NULL terminator */
404 padding = ((-tid_len) & 3);
405 if (padding != 0)
406 tid_len += padding;
407
408 if ((add_len + 4) != tid_len) {
409 pr_debug("LIO-Target Extracted add_len: %hu "
410 "does not match calculated tid_len: %u,"
411 " using tid_len instead\n", add_len+4, tid_len);
412 *out_tid_len = tid_len;
413 } else
414 *out_tid_len = (add_len + 4);
415 }
416 /*
417 * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
418 * Session ID as defined in Table 390 - iSCSI initiator port TransportID
419 * format.
420 */
421 if (format_code == 0x40) {
422 p = strstr(&buf[4], ",i,0x");
423 if (!p) {
424 pr_err("Unable to locate \",i,0x\" separator"
425 " for Initiator port identifier: %s\n",
426 &buf[4]);
427 return NULL;
428 }
429 *p = '\0'; /* Terminate iSCSI Name */
430 p += 5; /* Skip over ",i,0x" separator */
431
432 *port_nexus_ptr = p;
433 /*
434 * Go ahead and do the lower case conversion of the received
435 * 12 ASCII characters representing the ISID in the TransportID
436 * for comparison against the running iSCSI session's ISID from
437 * iscsi_target.c:lio_sess_get_initiator_sid()
438 */
439 for (i = 0; i < 12; i++) {
440 if (isdigit(*p)) {
441 p++;
442 continue;
443 }
444 *p = tolower(*p);
445 p++;
446 }
447 }
448
449 return (char *)&buf[4];
450}
451EXPORT_SYMBOL(iscsi_parse_pr_out_transport_id);