target_core_fabric_lib.c 12 KB

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