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 <linux/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 35 static 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 52 static 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 84 static 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 100 static 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 128 static 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 216 static 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 260 static void sas_parse_pr_out_transport_id(char *buf, char *i_str) 261 { 262 char hex[17] = {}; 263 264 bin2hex(hex, buf + 4, 8); 265 snprintf(i_str, TRANSPORT_IQN_LEN, "naa.%s", hex); 266 } 267 268 static void srp_parse_pr_out_transport_id(char *buf, char *i_str) 269 { 270 char hex[33] = {}; 271 272 bin2hex(hex, buf + 8, 16); 273 snprintf(i_str, TRANSPORT_IQN_LEN, "0x%s", hex); 274 } 275 276 static void fcp_parse_pr_out_transport_id(char *buf, char *i_str) 277 { 278 snprintf(i_str, TRANSPORT_IQN_LEN, "%8phC", buf + 8); 279 } 280 281 static void sbp_parse_pr_out_transport_id(char *buf, char *i_str) 282 { 283 char hex[17] = {}; 284 285 bin2hex(hex, buf + 8, 8); 286 snprintf(i_str, TRANSPORT_IQN_LEN, "%s", hex); 287 } 288 289 static bool iscsi_parse_pr_out_transport_id( 290 struct se_portal_group *se_tpg, 291 char *buf, 292 u32 *out_tid_len, 293 char **port_nexus_ptr, 294 char *i_str) 295 { 296 char *p; 297 int i; 298 u8 format_code = (buf[0] & 0xc0); 299 /* 300 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6: 301 * 302 * TransportID for initiator ports using SCSI over iSCSI, 303 * from Table 388 -- iSCSI TransportID formats. 304 * 305 * 00b Initiator port is identified using the world wide unique 306 * SCSI device name of the iSCSI initiator 307 * device containing the initiator port (see table 389). 308 * 01b Initiator port is identified using the world wide unique 309 * initiator port identifier (see table 390).10b to 11b 310 * Reserved 311 */ 312 if ((format_code != 0x00) && (format_code != 0x40)) { 313 pr_err("Illegal format code: 0x%02x for iSCSI" 314 " Initiator Transport ID\n", format_code); 315 return false; 316 } 317 /* 318 * If the caller wants the TransportID Length, we set that value for the 319 * entire iSCSI Tarnsport ID now. 320 */ 321 if (out_tid_len) { 322 /* The shift works thanks to integer promotion rules */ 323 *out_tid_len = get_unaligned_be16(&buf[2]); 324 /* Add four bytes for iSCSI Transport ID header */ 325 *out_tid_len += 4; 326 } 327 328 /* 329 * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator 330 * Session ID as defined in Table 390 - iSCSI initiator port TransportID 331 * format. 332 */ 333 if (format_code == 0x40) { 334 p = strstr(&buf[4], ",i,0x"); 335 if (!p) { 336 pr_err("Unable to locate \",i,0x\" separator" 337 " for Initiator port identifier: %s\n", 338 &buf[4]); 339 return false; 340 } 341 *p = '\0'; /* Terminate iSCSI Name */ 342 p += 5; /* Skip over ",i,0x" separator */ 343 344 *port_nexus_ptr = p; 345 /* 346 * Go ahead and do the lower case conversion of the received 347 * 12 ASCII characters representing the ISID in the TransportID 348 * for comparison against the running iSCSI session's ISID from 349 * iscsi_target.c:lio_sess_get_initiator_sid() 350 */ 351 for (i = 0; i < 12; i++) { 352 /* 353 * The first ISCSI INITIATOR SESSION ID field byte 354 * containing an ASCII null character terminates the 355 * ISCSI INITIATOR SESSION ID field without regard for 356 * the specified length of the iSCSI TransportID or the 357 * contents of the ADDITIONAL LENGTH field. 358 */ 359 if (*p == '\0') 360 break; 361 362 if (isdigit(*p)) { 363 p++; 364 continue; 365 } 366 *p = tolower(*p); 367 p++; 368 } 369 } else 370 *port_nexus_ptr = NULL; 371 372 strscpy(i_str, &buf[4], TRANSPORT_IQN_LEN); 373 return true; 374 } 375 376 int target_get_pr_transport_id_len(struct se_node_acl *nacl, 377 struct t10_pr_registration *pr_reg, int *format_code) 378 { 379 switch (nacl->se_tpg->proto_id) { 380 case SCSI_PROTOCOL_FCP: 381 case SCSI_PROTOCOL_SBP: 382 case SCSI_PROTOCOL_SRP: 383 case SCSI_PROTOCOL_SAS: 384 break; 385 case SCSI_PROTOCOL_ISCSI: 386 return iscsi_get_pr_transport_id_len(nacl, pr_reg, format_code); 387 default: 388 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id); 389 return -EINVAL; 390 } 391 392 /* 393 * Most transports use a fixed length 24 byte identifier. 394 */ 395 *format_code = 0; 396 return 24; 397 } 398 399 int target_get_pr_transport_id(struct se_node_acl *nacl, 400 struct t10_pr_registration *pr_reg, int *format_code, 401 unsigned char *buf) 402 { 403 switch (nacl->se_tpg->proto_id) { 404 case SCSI_PROTOCOL_SAS: 405 return sas_get_pr_transport_id(nacl, format_code, buf); 406 case SCSI_PROTOCOL_SBP: 407 return sbp_get_pr_transport_id(nacl, format_code, buf); 408 case SCSI_PROTOCOL_SRP: 409 return srp_get_pr_transport_id(nacl, format_code, buf); 410 case SCSI_PROTOCOL_FCP: 411 return fc_get_pr_transport_id(nacl, format_code, buf); 412 case SCSI_PROTOCOL_ISCSI: 413 return iscsi_get_pr_transport_id(nacl, pr_reg, format_code, 414 buf); 415 default: 416 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id); 417 return -EINVAL; 418 } 419 } 420 421 bool target_parse_pr_out_transport_id(struct se_portal_group *tpg, 422 char *buf, u32 *out_tid_len, char **port_nexus_ptr, char *i_str) 423 { 424 switch (tpg->proto_id) { 425 case SCSI_PROTOCOL_SAS: 426 /* 427 * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID 428 * for initiator ports using SCSI over SAS Serial SCSI Protocol. 429 */ 430 sas_parse_pr_out_transport_id(buf, i_str); 431 break; 432 case SCSI_PROTOCOL_SRP: 433 srp_parse_pr_out_transport_id(buf, i_str); 434 break; 435 case SCSI_PROTOCOL_FCP: 436 fcp_parse_pr_out_transport_id(buf, i_str); 437 break; 438 case SCSI_PROTOCOL_SBP: 439 sbp_parse_pr_out_transport_id(buf, i_str); 440 break; 441 case SCSI_PROTOCOL_ISCSI: 442 return iscsi_parse_pr_out_transport_id(tpg, buf, out_tid_len, 443 port_nexus_ptr, i_str); 444 default: 445 pr_err("Unknown proto_id: 0x%02x\n", tpg->proto_id); 446 return false; 447 } 448 449 *port_nexus_ptr = NULL; 450 *out_tid_len = 24; 451 return true; 452 } 453