target_core_fabric_lib.c 11 KB

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