iscsi_target_nego.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*******************************************************************************
  2. * This file contains main functions related to iSCSI Parameter negotiation.
  3. *
  4. * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
  5. *
  6. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  7. *
  8. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. ******************************************************************************/
  20. #include <linux/ctype.h>
  21. #include <scsi/iscsi_proto.h>
  22. #include <target/target_core_base.h>
  23. #include <target/target_core_fabric.h>
  24. #include "iscsi_target_core.h"
  25. #include "iscsi_target_parameters.h"
  26. #include "iscsi_target_login.h"
  27. #include "iscsi_target_nego.h"
  28. #include "iscsi_target_tpg.h"
  29. #include "iscsi_target_util.h"
  30. #include "iscsi_target.h"
  31. #include "iscsi_target_auth.h"
  32. #define MAX_LOGIN_PDUS 7
  33. #define TEXT_LEN 4096
  34. void convert_null_to_semi(char *buf, int len)
  35. {
  36. int i;
  37. for (i = 0; i < len; i++)
  38. if (buf[i] == '\0')
  39. buf[i] = ';';
  40. }
  41. static int strlen_semi(char *buf)
  42. {
  43. int i = 0;
  44. while (buf[i] != '\0') {
  45. if (buf[i] == ';')
  46. return i;
  47. i++;
  48. }
  49. return -1;
  50. }
  51. int extract_param(
  52. const char *in_buf,
  53. const char *pattern,
  54. unsigned int max_length,
  55. char *out_buf,
  56. unsigned char *type)
  57. {
  58. char *ptr;
  59. int len;
  60. if (!in_buf || !pattern || !out_buf || !type)
  61. return -1;
  62. ptr = strstr(in_buf, pattern);
  63. if (!ptr)
  64. return -1;
  65. ptr = strstr(ptr, "=");
  66. if (!ptr)
  67. return -1;
  68. ptr += 1;
  69. if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
  70. ptr += 2; /* skip 0x */
  71. *type = HEX;
  72. } else
  73. *type = DECIMAL;
  74. len = strlen_semi(ptr);
  75. if (len < 0)
  76. return -1;
  77. if (len > max_length) {
  78. pr_err("Length of input: %d exceeds max_length:"
  79. " %d\n", len, max_length);
  80. return -1;
  81. }
  82. memcpy(out_buf, ptr, len);
  83. out_buf[len] = '\0';
  84. return 0;
  85. }
  86. static u32 iscsi_handle_authentication(
  87. struct iscsi_conn *conn,
  88. char *in_buf,
  89. char *out_buf,
  90. int in_length,
  91. int *out_length,
  92. unsigned char *authtype)
  93. {
  94. struct iscsi_session *sess = conn->sess;
  95. struct iscsi_node_auth *auth;
  96. struct iscsi_node_acl *iscsi_nacl;
  97. struct se_node_acl *se_nacl;
  98. if (!sess->sess_ops->SessionType) {
  99. /*
  100. * For SessionType=Normal
  101. */
  102. se_nacl = conn->sess->se_sess->se_node_acl;
  103. if (!se_nacl) {
  104. pr_err("Unable to locate struct se_node_acl for"
  105. " CHAP auth\n");
  106. return -1;
  107. }
  108. iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
  109. se_node_acl);
  110. if (!iscsi_nacl) {
  111. pr_err("Unable to locate struct iscsi_node_acl for"
  112. " CHAP auth\n");
  113. return -1;
  114. }
  115. auth = ISCSI_NODE_AUTH(iscsi_nacl);
  116. } else {
  117. /*
  118. * For SessionType=Discovery
  119. */
  120. auth = &iscsit_global->discovery_acl.node_auth;
  121. }
  122. if (strstr("CHAP", authtype))
  123. strcpy(conn->sess->auth_type, "CHAP");
  124. else
  125. strcpy(conn->sess->auth_type, NONE);
  126. if (strstr("None", authtype))
  127. return 1;
  128. #ifdef CANSRP
  129. else if (strstr("SRP", authtype))
  130. return srp_main_loop(conn, auth, in_buf, out_buf,
  131. &in_length, out_length);
  132. #endif
  133. else if (strstr("CHAP", authtype))
  134. return chap_main_loop(conn, auth, in_buf, out_buf,
  135. &in_length, out_length);
  136. else if (strstr("SPKM1", authtype))
  137. return 2;
  138. else if (strstr("SPKM2", authtype))
  139. return 2;
  140. else if (strstr("KRB5", authtype))
  141. return 2;
  142. else
  143. return 2;
  144. }
  145. static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
  146. {
  147. kfree(conn->auth_protocol);
  148. }
  149. static int iscsi_target_check_login_request(
  150. struct iscsi_conn *conn,
  151. struct iscsi_login *login)
  152. {
  153. int req_csg, req_nsg;
  154. u32 payload_length;
  155. struct iscsi_login_req *login_req;
  156. login_req = (struct iscsi_login_req *) login->req;
  157. payload_length = ntoh24(login_req->dlength);
  158. switch (login_req->opcode & ISCSI_OPCODE_MASK) {
  159. case ISCSI_OP_LOGIN:
  160. break;
  161. default:
  162. pr_err("Received unknown opcode 0x%02x.\n",
  163. login_req->opcode & ISCSI_OPCODE_MASK);
  164. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  165. ISCSI_LOGIN_STATUS_INIT_ERR);
  166. return -1;
  167. }
  168. if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
  169. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  170. pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
  171. " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
  172. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  173. ISCSI_LOGIN_STATUS_INIT_ERR);
  174. return -1;
  175. }
  176. req_csg = (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
  177. req_nsg = (login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK);
  178. if (req_csg != login->current_stage) {
  179. pr_err("Initiator unexpectedly changed login stage"
  180. " from %d to %d, login failed.\n", login->current_stage,
  181. req_csg);
  182. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  183. ISCSI_LOGIN_STATUS_INIT_ERR);
  184. return -1;
  185. }
  186. if ((req_nsg == 2) || (req_csg >= 2) ||
  187. ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
  188. (req_nsg <= req_csg))) {
  189. pr_err("Illegal login_req->flags Combination, CSG: %d,"
  190. " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
  191. req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
  192. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  193. ISCSI_LOGIN_STATUS_INIT_ERR);
  194. return -1;
  195. }
  196. if ((login_req->max_version != login->version_max) ||
  197. (login_req->min_version != login->version_min)) {
  198. pr_err("Login request changed Version Max/Nin"
  199. " unexpectedly to 0x%02x/0x%02x, protocol error\n",
  200. login_req->max_version, login_req->min_version);
  201. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  202. ISCSI_LOGIN_STATUS_INIT_ERR);
  203. return -1;
  204. }
  205. if (memcmp(login_req->isid, login->isid, 6) != 0) {
  206. pr_err("Login request changed ISID unexpectedly,"
  207. " protocol error.\n");
  208. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  209. ISCSI_LOGIN_STATUS_INIT_ERR);
  210. return -1;
  211. }
  212. if (login_req->itt != login->init_task_tag) {
  213. pr_err("Login request changed ITT unexpectedly to"
  214. " 0x%08x, protocol error.\n", login_req->itt);
  215. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  216. ISCSI_LOGIN_STATUS_INIT_ERR);
  217. return -1;
  218. }
  219. if (payload_length > MAX_KEY_VALUE_PAIRS) {
  220. pr_err("Login request payload exceeds default"
  221. " MaxRecvDataSegmentLength: %u, protocol error.\n",
  222. MAX_KEY_VALUE_PAIRS);
  223. return -1;
  224. }
  225. return 0;
  226. }
  227. static int iscsi_target_check_first_request(
  228. struct iscsi_conn *conn,
  229. struct iscsi_login *login)
  230. {
  231. struct iscsi_param *param = NULL;
  232. struct se_node_acl *se_nacl;
  233. login->first_request = 0;
  234. list_for_each_entry(param, &conn->param_list->param_list, p_list) {
  235. if (!strncmp(param->name, SESSIONTYPE, 11)) {
  236. if (!IS_PSTATE_ACCEPTOR(param)) {
  237. pr_err("SessionType key not received"
  238. " in first login request.\n");
  239. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  240. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  241. return -1;
  242. }
  243. if (!strncmp(param->value, DISCOVERY, 9))
  244. return 0;
  245. }
  246. if (!strncmp(param->name, INITIATORNAME, 13)) {
  247. if (!IS_PSTATE_ACCEPTOR(param)) {
  248. if (!login->leading_connection)
  249. continue;
  250. pr_err("InitiatorName key not received"
  251. " in first login request.\n");
  252. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  253. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  254. return -1;
  255. }
  256. /*
  257. * For non-leading connections, double check that the
  258. * received InitiatorName matches the existing session's
  259. * struct iscsi_node_acl.
  260. */
  261. if (!login->leading_connection) {
  262. se_nacl = conn->sess->se_sess->se_node_acl;
  263. if (!se_nacl) {
  264. pr_err("Unable to locate"
  265. " struct se_node_acl\n");
  266. iscsit_tx_login_rsp(conn,
  267. ISCSI_STATUS_CLS_INITIATOR_ERR,
  268. ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
  269. return -1;
  270. }
  271. if (strcmp(param->value,
  272. se_nacl->initiatorname)) {
  273. pr_err("Incorrect"
  274. " InitiatorName: %s for this"
  275. " iSCSI Initiator Node.\n",
  276. param->value);
  277. iscsit_tx_login_rsp(conn,
  278. ISCSI_STATUS_CLS_INITIATOR_ERR,
  279. ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
  280. return -1;
  281. }
  282. }
  283. }
  284. }
  285. return 0;
  286. }
  287. static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
  288. {
  289. u32 padding = 0;
  290. struct iscsi_session *sess = conn->sess;
  291. struct iscsi_login_rsp *login_rsp;
  292. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  293. login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
  294. hton24(login_rsp->dlength, login->rsp_length);
  295. memcpy(login_rsp->isid, login->isid, 6);
  296. login_rsp->tsih = cpu_to_be16(login->tsih);
  297. login_rsp->itt = cpu_to_be32(login->init_task_tag);
  298. login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
  299. login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
  300. login_rsp->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
  301. pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
  302. " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
  303. " %u\n", login_rsp->flags, ntohl(login_rsp->itt),
  304. ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
  305. ntohl(login_rsp->statsn), login->rsp_length);
  306. padding = ((-login->rsp_length) & 3);
  307. if (iscsi_login_tx_data(
  308. conn,
  309. login->rsp,
  310. login->rsp_buf,
  311. login->rsp_length + padding) < 0)
  312. return -1;
  313. login->rsp_length = 0;
  314. login_rsp->tsih = be16_to_cpu(login_rsp->tsih);
  315. login_rsp->itt = be32_to_cpu(login_rsp->itt);
  316. login_rsp->statsn = be32_to_cpu(login_rsp->statsn);
  317. mutex_lock(&sess->cmdsn_mutex);
  318. login_rsp->exp_cmdsn = be32_to_cpu(sess->exp_cmd_sn);
  319. login_rsp->max_cmdsn = be32_to_cpu(sess->max_cmd_sn);
  320. mutex_unlock(&sess->cmdsn_mutex);
  321. return 0;
  322. }
  323. static int iscsi_target_do_rx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
  324. {
  325. u32 padding = 0, payload_length;
  326. struct iscsi_login_req *login_req;
  327. if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
  328. return -1;
  329. login_req = (struct iscsi_login_req *) login->req;
  330. payload_length = ntoh24(login_req->dlength);
  331. login_req->tsih = be16_to_cpu(login_req->tsih);
  332. login_req->itt = be32_to_cpu(login_req->itt);
  333. login_req->cid = be16_to_cpu(login_req->cid);
  334. login_req->cmdsn = be32_to_cpu(login_req->cmdsn);
  335. login_req->exp_statsn = be32_to_cpu(login_req->exp_statsn);
  336. pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
  337. " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
  338. login_req->flags, login_req->itt, login_req->cmdsn,
  339. login_req->exp_statsn, login_req->cid, payload_length);
  340. if (iscsi_target_check_login_request(conn, login) < 0)
  341. return -1;
  342. padding = ((-payload_length) & 3);
  343. memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
  344. if (iscsi_login_rx_data(
  345. conn,
  346. login->req_buf,
  347. payload_length + padding) < 0)
  348. return -1;
  349. return 0;
  350. }
  351. static int iscsi_target_do_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
  352. {
  353. if (iscsi_target_do_tx_login_io(conn, login) < 0)
  354. return -1;
  355. if (iscsi_target_do_rx_login_io(conn, login) < 0)
  356. return -1;
  357. return 0;
  358. }
  359. static int iscsi_target_get_initial_payload(
  360. struct iscsi_conn *conn,
  361. struct iscsi_login *login)
  362. {
  363. u32 padding = 0, payload_length;
  364. struct iscsi_login_req *login_req;
  365. login_req = (struct iscsi_login_req *) login->req;
  366. payload_length = ntoh24(login_req->dlength);
  367. pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
  368. " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
  369. login_req->flags, login_req->itt, login_req->cmdsn,
  370. login_req->exp_statsn, payload_length);
  371. if (iscsi_target_check_login_request(conn, login) < 0)
  372. return -1;
  373. padding = ((-payload_length) & 3);
  374. if (iscsi_login_rx_data(
  375. conn,
  376. login->req_buf,
  377. payload_length + padding) < 0)
  378. return -1;
  379. return 0;
  380. }
  381. /*
  382. * NOTE: We check for existing sessions or connections AFTER the initiator
  383. * has been successfully authenticated in order to protect against faked
  384. * ISID/TSIH combinations.
  385. */
  386. static int iscsi_target_check_for_existing_instances(
  387. struct iscsi_conn *conn,
  388. struct iscsi_login *login)
  389. {
  390. if (login->checked_for_existing)
  391. return 0;
  392. login->checked_for_existing = 1;
  393. if (!login->tsih)
  394. return iscsi_check_for_session_reinstatement(conn);
  395. else
  396. return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
  397. login->initial_exp_statsn);
  398. }
  399. static int iscsi_target_do_authentication(
  400. struct iscsi_conn *conn,
  401. struct iscsi_login *login)
  402. {
  403. int authret;
  404. u32 payload_length;
  405. struct iscsi_param *param;
  406. struct iscsi_login_req *login_req;
  407. struct iscsi_login_rsp *login_rsp;
  408. login_req = (struct iscsi_login_req *) login->req;
  409. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  410. payload_length = ntoh24(login_req->dlength);
  411. param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
  412. if (!param)
  413. return -1;
  414. authret = iscsi_handle_authentication(
  415. conn,
  416. login->req_buf,
  417. login->rsp_buf,
  418. payload_length,
  419. &login->rsp_length,
  420. param->value);
  421. switch (authret) {
  422. case 0:
  423. pr_debug("Received OK response"
  424. " from LIO Authentication, continuing.\n");
  425. break;
  426. case 1:
  427. pr_debug("iSCSI security negotiation"
  428. " completed successfully.\n");
  429. login->auth_complete = 1;
  430. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
  431. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  432. login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
  433. ISCSI_FLAG_LOGIN_TRANSIT);
  434. login->current_stage = 1;
  435. }
  436. return iscsi_target_check_for_existing_instances(
  437. conn, login);
  438. case 2:
  439. pr_err("Security negotiation"
  440. " failed.\n");
  441. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  442. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  443. return -1;
  444. default:
  445. pr_err("Received unknown error %d from LIO"
  446. " Authentication\n", authret);
  447. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  448. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  449. return -1;
  450. }
  451. return 0;
  452. }
  453. static int iscsi_target_handle_csg_zero(
  454. struct iscsi_conn *conn,
  455. struct iscsi_login *login)
  456. {
  457. int ret;
  458. u32 payload_length;
  459. struct iscsi_param *param;
  460. struct iscsi_login_req *login_req;
  461. struct iscsi_login_rsp *login_rsp;
  462. login_req = (struct iscsi_login_req *) login->req;
  463. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  464. payload_length = ntoh24(login_req->dlength);
  465. param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
  466. if (!param)
  467. return -1;
  468. ret = iscsi_decode_text_input(
  469. PHASE_SECURITY|PHASE_DECLARATIVE,
  470. SENDER_INITIATOR|SENDER_RECEIVER,
  471. login->req_buf,
  472. payload_length,
  473. conn);
  474. if (ret < 0)
  475. return -1;
  476. if (ret > 0) {
  477. if (login->auth_complete) {
  478. pr_err("Initiator has already been"
  479. " successfully authenticated, but is still"
  480. " sending %s keys.\n", param->value);
  481. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  482. ISCSI_LOGIN_STATUS_INIT_ERR);
  483. return -1;
  484. }
  485. goto do_auth;
  486. }
  487. if (login->first_request)
  488. if (iscsi_target_check_first_request(conn, login) < 0)
  489. return -1;
  490. ret = iscsi_encode_text_output(
  491. PHASE_SECURITY|PHASE_DECLARATIVE,
  492. SENDER_TARGET,
  493. login->rsp_buf,
  494. &login->rsp_length,
  495. conn->param_list);
  496. if (ret < 0)
  497. return -1;
  498. if (!iscsi_check_negotiated_keys(conn->param_list)) {
  499. if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
  500. !strncmp(param->value, NONE, 4)) {
  501. pr_err("Initiator sent AuthMethod=None but"
  502. " Target is enforcing iSCSI Authentication,"
  503. " login failed.\n");
  504. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  505. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  506. return -1;
  507. }
  508. if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
  509. !login->auth_complete)
  510. return 0;
  511. if (strncmp(param->value, NONE, 4) && !login->auth_complete)
  512. return 0;
  513. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
  514. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  515. login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
  516. ISCSI_FLAG_LOGIN_TRANSIT;
  517. login->current_stage = 1;
  518. }
  519. }
  520. return 0;
  521. do_auth:
  522. return iscsi_target_do_authentication(conn, login);
  523. }
  524. static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
  525. {
  526. int ret;
  527. u32 payload_length;
  528. struct iscsi_login_req *login_req;
  529. struct iscsi_login_rsp *login_rsp;
  530. login_req = (struct iscsi_login_req *) login->req;
  531. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  532. payload_length = ntoh24(login_req->dlength);
  533. ret = iscsi_decode_text_input(
  534. PHASE_OPERATIONAL|PHASE_DECLARATIVE,
  535. SENDER_INITIATOR|SENDER_RECEIVER,
  536. login->req_buf,
  537. payload_length,
  538. conn);
  539. if (ret < 0)
  540. return -1;
  541. if (login->first_request)
  542. if (iscsi_target_check_first_request(conn, login) < 0)
  543. return -1;
  544. if (iscsi_target_check_for_existing_instances(conn, login) < 0)
  545. return -1;
  546. ret = iscsi_encode_text_output(
  547. PHASE_OPERATIONAL|PHASE_DECLARATIVE,
  548. SENDER_TARGET,
  549. login->rsp_buf,
  550. &login->rsp_length,
  551. conn->param_list);
  552. if (ret < 0)
  553. return -1;
  554. if (!login->auth_complete &&
  555. ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) {
  556. pr_err("Initiator is requesting CSG: 1, has not been"
  557. " successfully authenticated, and the Target is"
  558. " enforcing iSCSI Authentication, login failed.\n");
  559. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  560. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  561. return -1;
  562. }
  563. if (!iscsi_check_negotiated_keys(conn->param_list))
  564. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
  565. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
  566. login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
  567. ISCSI_FLAG_LOGIN_TRANSIT;
  568. return 0;
  569. }
  570. static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
  571. {
  572. int pdu_count = 0;
  573. struct iscsi_login_req *login_req;
  574. struct iscsi_login_rsp *login_rsp;
  575. login_req = (struct iscsi_login_req *) login->req;
  576. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  577. while (1) {
  578. if (++pdu_count > MAX_LOGIN_PDUS) {
  579. pr_err("MAX_LOGIN_PDUS count reached.\n");
  580. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  581. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  582. return -1;
  583. }
  584. switch ((login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2) {
  585. case 0:
  586. login_rsp->flags |= (0 & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK);
  587. if (iscsi_target_handle_csg_zero(conn, login) < 0)
  588. return -1;
  589. break;
  590. case 1:
  591. login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
  592. if (iscsi_target_handle_csg_one(conn, login) < 0)
  593. return -1;
  594. if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
  595. login->tsih = conn->sess->tsih;
  596. if (iscsi_target_do_tx_login_io(conn,
  597. login) < 0)
  598. return -1;
  599. return 0;
  600. }
  601. break;
  602. default:
  603. pr_err("Illegal CSG: %d received from"
  604. " Initiator, protocol error.\n",
  605. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
  606. >> 2);
  607. break;
  608. }
  609. if (iscsi_target_do_login_io(conn, login) < 0)
  610. return -1;
  611. if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
  612. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
  613. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
  614. }
  615. }
  616. return 0;
  617. }
  618. static void iscsi_initiatorname_tolower(
  619. char *param_buf)
  620. {
  621. char *c;
  622. u32 iqn_size = strlen(param_buf), i;
  623. for (i = 0; i < iqn_size; i++) {
  624. c = &param_buf[i];
  625. if (!isupper(*c))
  626. continue;
  627. *c = tolower(*c);
  628. }
  629. }
  630. /*
  631. * Processes the first Login Request..
  632. */
  633. static int iscsi_target_locate_portal(
  634. struct iscsi_np *np,
  635. struct iscsi_conn *conn,
  636. struct iscsi_login *login)
  637. {
  638. char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
  639. char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
  640. struct iscsi_session *sess = conn->sess;
  641. struct iscsi_tiqn *tiqn;
  642. struct iscsi_login_req *login_req;
  643. u32 payload_length;
  644. int sessiontype = 0, ret = 0;
  645. login_req = (struct iscsi_login_req *) login->req;
  646. payload_length = ntoh24(login_req->dlength);
  647. login->first_request = 1;
  648. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  649. login->current_stage =
  650. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
  651. login->version_min = login_req->min_version;
  652. login->version_max = login_req->max_version;
  653. memcpy(login->isid, login_req->isid, 6);
  654. login->cmd_sn = login_req->cmdsn;
  655. login->init_task_tag = login_req->itt;
  656. login->initial_exp_statsn = login_req->exp_statsn;
  657. login->cid = login_req->cid;
  658. login->tsih = login_req->tsih;
  659. if (iscsi_target_get_initial_payload(conn, login) < 0)
  660. return -1;
  661. tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
  662. if (!tmpbuf) {
  663. pr_err("Unable to allocate memory for tmpbuf.\n");
  664. return -1;
  665. }
  666. memcpy(tmpbuf, login->req_buf, payload_length);
  667. tmpbuf[payload_length] = '\0';
  668. start = tmpbuf;
  669. end = (start + payload_length);
  670. /*
  671. * Locate the initial keys expected from the Initiator node in
  672. * the first login request in order to progress with the login phase.
  673. */
  674. while (start < end) {
  675. if (iscsi_extract_key_value(start, &key, &value) < 0) {
  676. ret = -1;
  677. goto out;
  678. }
  679. if (!strncmp(key, "InitiatorName", 13))
  680. i_buf = value;
  681. else if (!strncmp(key, "SessionType", 11))
  682. s_buf = value;
  683. else if (!strncmp(key, "TargetName", 10))
  684. t_buf = value;
  685. start += strlen(key) + strlen(value) + 2;
  686. }
  687. /*
  688. * See 5.3. Login Phase.
  689. */
  690. if (!i_buf) {
  691. pr_err("InitiatorName key not received"
  692. " in first login request.\n");
  693. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  694. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  695. ret = -1;
  696. goto out;
  697. }
  698. /*
  699. * Convert the incoming InitiatorName to lowercase following
  700. * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
  701. * are NOT case sensitive.
  702. */
  703. iscsi_initiatorname_tolower(i_buf);
  704. if (!s_buf) {
  705. if (!login->leading_connection)
  706. goto get_target;
  707. pr_err("SessionType key not received"
  708. " in first login request.\n");
  709. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  710. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  711. ret = -1;
  712. goto out;
  713. }
  714. /*
  715. * Use default portal group for discovery sessions.
  716. */
  717. sessiontype = strncmp(s_buf, DISCOVERY, 9);
  718. if (!sessiontype) {
  719. conn->tpg = iscsit_global->discovery_tpg;
  720. if (!login->leading_connection)
  721. goto get_target;
  722. sess->sess_ops->SessionType = 1;
  723. /*
  724. * Setup crc32c modules from libcrypto
  725. */
  726. if (iscsi_login_setup_crypto(conn) < 0) {
  727. pr_err("iscsi_login_setup_crypto() failed\n");
  728. ret = -1;
  729. goto out;
  730. }
  731. /*
  732. * Serialize access across the discovery struct iscsi_portal_group to
  733. * process login attempt.
  734. */
  735. if (iscsit_access_np(np, conn->tpg) < 0) {
  736. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  737. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  738. ret = -1;
  739. goto out;
  740. }
  741. ret = 0;
  742. goto out;
  743. }
  744. get_target:
  745. if (!t_buf) {
  746. pr_err("TargetName key not received"
  747. " in first login request while"
  748. " SessionType=Normal.\n");
  749. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  750. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  751. ret = -1;
  752. goto out;
  753. }
  754. /*
  755. * Locate Target IQN from Storage Node.
  756. */
  757. tiqn = iscsit_get_tiqn_for_login(t_buf);
  758. if (!tiqn) {
  759. pr_err("Unable to locate Target IQN: %s in"
  760. " Storage Node\n", t_buf);
  761. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  762. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  763. ret = -1;
  764. goto out;
  765. }
  766. pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
  767. /*
  768. * Locate Target Portal Group from Storage Node.
  769. */
  770. conn->tpg = iscsit_get_tpg_from_np(tiqn, np);
  771. if (!conn->tpg) {
  772. pr_err("Unable to locate Target Portal Group"
  773. " on %s\n", tiqn->tiqn);
  774. iscsit_put_tiqn_for_login(tiqn);
  775. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  776. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  777. ret = -1;
  778. goto out;
  779. }
  780. pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
  781. /*
  782. * Setup crc32c modules from libcrypto
  783. */
  784. if (iscsi_login_setup_crypto(conn) < 0) {
  785. pr_err("iscsi_login_setup_crypto() failed\n");
  786. ret = -1;
  787. goto out;
  788. }
  789. /*
  790. * Serialize access across the struct iscsi_portal_group to
  791. * process login attempt.
  792. */
  793. if (iscsit_access_np(np, conn->tpg) < 0) {
  794. iscsit_put_tiqn_for_login(tiqn);
  795. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  796. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  797. ret = -1;
  798. conn->tpg = NULL;
  799. goto out;
  800. }
  801. /*
  802. * conn->sess->node_acl will be set when the referenced
  803. * struct iscsi_session is located from received ISID+TSIH in
  804. * iscsi_login_non_zero_tsih_s2().
  805. */
  806. if (!login->leading_connection) {
  807. ret = 0;
  808. goto out;
  809. }
  810. /*
  811. * This value is required in iscsi_login_zero_tsih_s2()
  812. */
  813. sess->sess_ops->SessionType = 0;
  814. /*
  815. * Locate incoming Initiator IQN reference from Storage Node.
  816. */
  817. sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  818. &conn->tpg->tpg_se_tpg, i_buf);
  819. if (!sess->se_sess->se_node_acl) {
  820. pr_err("iSCSI Initiator Node: %s is not authorized to"
  821. " access iSCSI target portal group: %hu.\n",
  822. i_buf, conn->tpg->tpgt);
  823. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  824. ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
  825. ret = -1;
  826. goto out;
  827. }
  828. ret = 0;
  829. out:
  830. kfree(tmpbuf);
  831. return ret;
  832. }
  833. struct iscsi_login *iscsi_target_init_negotiation(
  834. struct iscsi_np *np,
  835. struct iscsi_conn *conn,
  836. char *login_pdu)
  837. {
  838. struct iscsi_login *login;
  839. login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
  840. if (!login) {
  841. pr_err("Unable to allocate memory for struct iscsi_login.\n");
  842. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  843. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  844. return NULL;
  845. }
  846. login->req = kmemdup(login_pdu, ISCSI_HDR_LEN, GFP_KERNEL);
  847. if (!login->req) {
  848. pr_err("Unable to allocate memory for Login Request.\n");
  849. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  850. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  851. goto out;
  852. }
  853. login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  854. if (!login->req_buf) {
  855. pr_err("Unable to allocate memory for response buffer.\n");
  856. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  857. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  858. goto out;
  859. }
  860. /*
  861. * SessionType: Discovery
  862. *
  863. * Locates Default Portal
  864. *
  865. * SessionType: Normal
  866. *
  867. * Locates Target Portal from NP -> Target IQN
  868. */
  869. if (iscsi_target_locate_portal(np, conn, login) < 0) {
  870. goto out;
  871. }
  872. return login;
  873. out:
  874. kfree(login->req);
  875. kfree(login->req_buf);
  876. kfree(login);
  877. return NULL;
  878. }
  879. int iscsi_target_start_negotiation(
  880. struct iscsi_login *login,
  881. struct iscsi_conn *conn)
  882. {
  883. int ret = -1;
  884. login->rsp = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
  885. if (!login->rsp) {
  886. pr_err("Unable to allocate memory for"
  887. " Login Response.\n");
  888. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  889. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  890. ret = -1;
  891. goto out;
  892. }
  893. login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  894. if (!login->rsp_buf) {
  895. pr_err("Unable to allocate memory for"
  896. " request buffer.\n");
  897. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  898. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  899. ret = -1;
  900. goto out;
  901. }
  902. ret = iscsi_target_do_login(conn, login);
  903. out:
  904. if (ret != 0)
  905. iscsi_remove_failed_auth_entry(conn);
  906. iscsi_target_nego_release(login, conn);
  907. return ret;
  908. }
  909. void iscsi_target_nego_release(
  910. struct iscsi_login *login,
  911. struct iscsi_conn *conn)
  912. {
  913. kfree(login->req);
  914. kfree(login->rsp);
  915. kfree(login->req_buf);
  916. kfree(login->rsp_buf);
  917. kfree(login);
  918. }