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