iscsi_target_nego.c 23 KB

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