Linux Audio

Check our new training course

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