iscsi_target_nego.c 23 KB

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