iscsi_target_nego.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
  177. req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
  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 = 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, (__force u32)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. mutex_lock(&sess->cmdsn_mutex);
  315. login_rsp->exp_cmdsn = cpu_to_be32(sess->exp_cmd_sn);
  316. login_rsp->max_cmdsn = cpu_to_be32(sess->max_cmd_sn);
  317. mutex_unlock(&sess->cmdsn_mutex);
  318. return 0;
  319. }
  320. static int iscsi_target_do_rx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
  321. {
  322. u32 padding = 0, payload_length;
  323. struct iscsi_login_req *login_req;
  324. if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
  325. return -1;
  326. login_req = (struct iscsi_login_req *) login->req;
  327. payload_length = ntoh24(login_req->dlength);
  328. pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
  329. " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
  330. login_req->flags, login_req->itt, login_req->cmdsn,
  331. login_req->exp_statsn, login_req->cid, payload_length);
  332. if (iscsi_target_check_login_request(conn, login) < 0)
  333. return -1;
  334. padding = ((-payload_length) & 3);
  335. memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
  336. if (iscsi_login_rx_data(
  337. conn,
  338. login->req_buf,
  339. payload_length + padding) < 0)
  340. return -1;
  341. return 0;
  342. }
  343. static int iscsi_target_do_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
  344. {
  345. if (iscsi_target_do_tx_login_io(conn, login) < 0)
  346. return -1;
  347. if (iscsi_target_do_rx_login_io(conn, login) < 0)
  348. return -1;
  349. return 0;
  350. }
  351. static int iscsi_target_get_initial_payload(
  352. struct iscsi_conn *conn,
  353. struct iscsi_login *login)
  354. {
  355. u32 padding = 0, payload_length;
  356. struct iscsi_login_req *login_req;
  357. login_req = (struct iscsi_login_req *) login->req;
  358. payload_length = ntoh24(login_req->dlength);
  359. pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
  360. " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
  361. login_req->flags, login_req->itt, login_req->cmdsn,
  362. login_req->exp_statsn, payload_length);
  363. if (iscsi_target_check_login_request(conn, login) < 0)
  364. return -1;
  365. padding = ((-payload_length) & 3);
  366. if (iscsi_login_rx_data(
  367. conn,
  368. login->req_buf,
  369. payload_length + padding) < 0)
  370. return -1;
  371. return 0;
  372. }
  373. /*
  374. * NOTE: We check for existing sessions or connections AFTER the initiator
  375. * has been successfully authenticated in order to protect against faked
  376. * ISID/TSIH combinations.
  377. */
  378. static int iscsi_target_check_for_existing_instances(
  379. struct iscsi_conn *conn,
  380. struct iscsi_login *login)
  381. {
  382. if (login->checked_for_existing)
  383. return 0;
  384. login->checked_for_existing = 1;
  385. if (!login->tsih)
  386. return iscsi_check_for_session_reinstatement(conn);
  387. else
  388. return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
  389. login->initial_exp_statsn);
  390. }
  391. static int iscsi_target_do_authentication(
  392. struct iscsi_conn *conn,
  393. struct iscsi_login *login)
  394. {
  395. int authret;
  396. u32 payload_length;
  397. struct iscsi_param *param;
  398. struct iscsi_login_req *login_req;
  399. struct iscsi_login_rsp *login_rsp;
  400. login_req = (struct iscsi_login_req *) login->req;
  401. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  402. payload_length = ntoh24(login_req->dlength);
  403. param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
  404. if (!param)
  405. return -1;
  406. authret = iscsi_handle_authentication(
  407. conn,
  408. login->req_buf,
  409. login->rsp_buf,
  410. payload_length,
  411. &login->rsp_length,
  412. param->value);
  413. switch (authret) {
  414. case 0:
  415. pr_debug("Received OK response"
  416. " from LIO Authentication, continuing.\n");
  417. break;
  418. case 1:
  419. pr_debug("iSCSI security negotiation"
  420. " completed successfully.\n");
  421. login->auth_complete = 1;
  422. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
  423. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  424. login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
  425. ISCSI_FLAG_LOGIN_TRANSIT);
  426. login->current_stage = 1;
  427. }
  428. return iscsi_target_check_for_existing_instances(
  429. conn, login);
  430. case 2:
  431. pr_err("Security negotiation"
  432. " failed.\n");
  433. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  434. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  435. return -1;
  436. default:
  437. pr_err("Received unknown error %d from LIO"
  438. " Authentication\n", authret);
  439. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  440. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  441. return -1;
  442. }
  443. return 0;
  444. }
  445. static int iscsi_target_handle_csg_zero(
  446. struct iscsi_conn *conn,
  447. struct iscsi_login *login)
  448. {
  449. int ret;
  450. u32 payload_length;
  451. struct iscsi_param *param;
  452. struct iscsi_login_req *login_req;
  453. struct iscsi_login_rsp *login_rsp;
  454. login_req = (struct iscsi_login_req *) login->req;
  455. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  456. payload_length = ntoh24(login_req->dlength);
  457. param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
  458. if (!param)
  459. return -1;
  460. ret = iscsi_decode_text_input(
  461. PHASE_SECURITY|PHASE_DECLARATIVE,
  462. SENDER_INITIATOR|SENDER_RECEIVER,
  463. login->req_buf,
  464. payload_length,
  465. conn);
  466. if (ret < 0)
  467. return -1;
  468. if (ret > 0) {
  469. if (login->auth_complete) {
  470. pr_err("Initiator has already been"
  471. " successfully authenticated, but is still"
  472. " sending %s keys.\n", param->value);
  473. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  474. ISCSI_LOGIN_STATUS_INIT_ERR);
  475. return -1;
  476. }
  477. goto do_auth;
  478. }
  479. if (login->first_request)
  480. if (iscsi_target_check_first_request(conn, login) < 0)
  481. return -1;
  482. ret = iscsi_encode_text_output(
  483. PHASE_SECURITY|PHASE_DECLARATIVE,
  484. SENDER_TARGET,
  485. login->rsp_buf,
  486. &login->rsp_length,
  487. conn->param_list);
  488. if (ret < 0)
  489. return -1;
  490. if (!iscsi_check_negotiated_keys(conn->param_list)) {
  491. if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
  492. !strncmp(param->value, NONE, 4)) {
  493. pr_err("Initiator sent AuthMethod=None but"
  494. " Target is enforcing iSCSI Authentication,"
  495. " login failed.\n");
  496. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  497. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  498. return -1;
  499. }
  500. if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
  501. !login->auth_complete)
  502. return 0;
  503. if (strncmp(param->value, NONE, 4) && !login->auth_complete)
  504. return 0;
  505. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
  506. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  507. login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
  508. ISCSI_FLAG_LOGIN_TRANSIT;
  509. login->current_stage = 1;
  510. }
  511. }
  512. return 0;
  513. do_auth:
  514. return iscsi_target_do_authentication(conn, login);
  515. }
  516. static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
  517. {
  518. int ret;
  519. u32 payload_length;
  520. struct iscsi_login_req *login_req;
  521. struct iscsi_login_rsp *login_rsp;
  522. login_req = (struct iscsi_login_req *) login->req;
  523. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  524. payload_length = ntoh24(login_req->dlength);
  525. ret = iscsi_decode_text_input(
  526. PHASE_OPERATIONAL|PHASE_DECLARATIVE,
  527. SENDER_INITIATOR|SENDER_RECEIVER,
  528. login->req_buf,
  529. payload_length,
  530. conn);
  531. if (ret < 0) {
  532. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  533. ISCSI_LOGIN_STATUS_INIT_ERR);
  534. return -1;
  535. }
  536. if (login->first_request)
  537. if (iscsi_target_check_first_request(conn, login) < 0)
  538. return -1;
  539. if (iscsi_target_check_for_existing_instances(conn, login) < 0)
  540. return -1;
  541. ret = iscsi_encode_text_output(
  542. PHASE_OPERATIONAL|PHASE_DECLARATIVE,
  543. SENDER_TARGET,
  544. login->rsp_buf,
  545. &login->rsp_length,
  546. conn->param_list);
  547. if (ret < 0) {
  548. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  549. ISCSI_LOGIN_STATUS_INIT_ERR);
  550. return -1;
  551. }
  552. if (!login->auth_complete &&
  553. ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) {
  554. pr_err("Initiator is requesting CSG: 1, has not been"
  555. " successfully authenticated, and the Target is"
  556. " enforcing iSCSI Authentication, login failed.\n");
  557. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  558. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  559. return -1;
  560. }
  561. if (!iscsi_check_negotiated_keys(conn->param_list))
  562. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
  563. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
  564. login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
  565. ISCSI_FLAG_LOGIN_TRANSIT;
  566. return 0;
  567. }
  568. static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
  569. {
  570. int pdu_count = 0;
  571. struct iscsi_login_req *login_req;
  572. struct iscsi_login_rsp *login_rsp;
  573. login_req = (struct iscsi_login_req *) login->req;
  574. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  575. while (1) {
  576. if (++pdu_count > MAX_LOGIN_PDUS) {
  577. pr_err("MAX_LOGIN_PDUS count reached.\n");
  578. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  579. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  580. return -1;
  581. }
  582. switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
  583. case 0:
  584. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
  585. if (iscsi_target_handle_csg_zero(conn, login) < 0)
  586. return -1;
  587. break;
  588. case 1:
  589. login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
  590. if (iscsi_target_handle_csg_one(conn, login) < 0)
  591. return -1;
  592. if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
  593. login->tsih = conn->sess->tsih;
  594. if (iscsi_target_do_tx_login_io(conn,
  595. login) < 0)
  596. return -1;
  597. return 0;
  598. }
  599. break;
  600. default:
  601. pr_err("Illegal CSG: %d received from"
  602. " Initiator, protocol error.\n",
  603. ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
  604. break;
  605. }
  606. if (iscsi_target_do_login_io(conn, login) < 0)
  607. return -1;
  608. if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
  609. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
  610. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
  611. }
  612. }
  613. return 0;
  614. }
  615. static void iscsi_initiatorname_tolower(
  616. char *param_buf)
  617. {
  618. char *c;
  619. u32 iqn_size = strlen(param_buf), i;
  620. for (i = 0; i < iqn_size; i++) {
  621. c = &param_buf[i];
  622. if (!isupper(*c))
  623. continue;
  624. *c = tolower(*c);
  625. }
  626. }
  627. /*
  628. * Processes the first Login Request..
  629. */
  630. static int iscsi_target_locate_portal(
  631. struct iscsi_np *np,
  632. struct iscsi_conn *conn,
  633. struct iscsi_login *login)
  634. {
  635. char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
  636. char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
  637. struct iscsi_session *sess = conn->sess;
  638. struct iscsi_tiqn *tiqn;
  639. struct iscsi_login_req *login_req;
  640. u32 payload_length;
  641. int sessiontype = 0, ret = 0;
  642. login_req = (struct iscsi_login_req *) login->req;
  643. payload_length = ntoh24(login_req->dlength);
  644. login->first_request = 1;
  645. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  646. login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
  647. login->version_min = login_req->min_version;
  648. login->version_max = login_req->max_version;
  649. memcpy(login->isid, login_req->isid, 6);
  650. login->cmd_sn = be32_to_cpu(login_req->cmdsn);
  651. login->init_task_tag = login_req->itt;
  652. login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
  653. login->cid = be16_to_cpu(login_req->cid);
  654. login->tsih = be16_to_cpu(login_req->tsih);
  655. if (iscsi_target_get_initial_payload(conn, login) < 0)
  656. return -1;
  657. tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
  658. if (!tmpbuf) {
  659. pr_err("Unable to allocate memory for tmpbuf.\n");
  660. return -1;
  661. }
  662. memcpy(tmpbuf, login->req_buf, payload_length);
  663. tmpbuf[payload_length] = '\0';
  664. start = tmpbuf;
  665. end = (start + payload_length);
  666. /*
  667. * Locate the initial keys expected from the Initiator node in
  668. * the first login request in order to progress with the login phase.
  669. */
  670. while (start < end) {
  671. if (iscsi_extract_key_value(start, &key, &value) < 0) {
  672. ret = -1;
  673. goto out;
  674. }
  675. if (!strncmp(key, "InitiatorName", 13))
  676. i_buf = value;
  677. else if (!strncmp(key, "SessionType", 11))
  678. s_buf = value;
  679. else if (!strncmp(key, "TargetName", 10))
  680. t_buf = value;
  681. start += strlen(key) + strlen(value) + 2;
  682. }
  683. /*
  684. * See 5.3. Login Phase.
  685. */
  686. if (!i_buf) {
  687. pr_err("InitiatorName key not received"
  688. " in first login request.\n");
  689. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  690. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  691. ret = -1;
  692. goto out;
  693. }
  694. /*
  695. * Convert the incoming InitiatorName to lowercase following
  696. * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
  697. * are NOT case sensitive.
  698. */
  699. iscsi_initiatorname_tolower(i_buf);
  700. if (!s_buf) {
  701. if (!login->leading_connection)
  702. goto get_target;
  703. pr_err("SessionType key not received"
  704. " in first login request.\n");
  705. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  706. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  707. ret = -1;
  708. goto out;
  709. }
  710. /*
  711. * Use default portal group for discovery sessions.
  712. */
  713. sessiontype = strncmp(s_buf, DISCOVERY, 9);
  714. if (!sessiontype) {
  715. conn->tpg = iscsit_global->discovery_tpg;
  716. if (!login->leading_connection)
  717. goto get_target;
  718. sess->sess_ops->SessionType = 1;
  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 discovery struct iscsi_portal_group to
  729. * process login attempt.
  730. */
  731. if (iscsit_access_np(np, conn->tpg) < 0) {
  732. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  733. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  734. ret = -1;
  735. goto out;
  736. }
  737. ret = 0;
  738. goto out;
  739. }
  740. get_target:
  741. if (!t_buf) {
  742. pr_err("TargetName key not received"
  743. " in first login request while"
  744. " SessionType=Normal.\n");
  745. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  746. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  747. ret = -1;
  748. goto out;
  749. }
  750. /*
  751. * Locate Target IQN from Storage Node.
  752. */
  753. tiqn = iscsit_get_tiqn_for_login(t_buf);
  754. if (!tiqn) {
  755. pr_err("Unable to locate Target IQN: %s in"
  756. " Storage Node\n", t_buf);
  757. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  758. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  759. ret = -1;
  760. goto out;
  761. }
  762. pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
  763. /*
  764. * Locate Target Portal Group from Storage Node.
  765. */
  766. conn->tpg = iscsit_get_tpg_from_np(tiqn, np);
  767. if (!conn->tpg) {
  768. pr_err("Unable to locate Target Portal Group"
  769. " on %s\n", tiqn->tiqn);
  770. iscsit_put_tiqn_for_login(tiqn);
  771. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  772. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  773. ret = -1;
  774. goto out;
  775. }
  776. pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
  777. /*
  778. * Setup crc32c modules from libcrypto
  779. */
  780. if (iscsi_login_setup_crypto(conn) < 0) {
  781. pr_err("iscsi_login_setup_crypto() failed\n");
  782. ret = -1;
  783. goto out;
  784. }
  785. /*
  786. * Serialize access across the struct iscsi_portal_group to
  787. * process login attempt.
  788. */
  789. if (iscsit_access_np(np, conn->tpg) < 0) {
  790. iscsit_put_tiqn_for_login(tiqn);
  791. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  792. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  793. ret = -1;
  794. conn->tpg = NULL;
  795. goto out;
  796. }
  797. /*
  798. * conn->sess->node_acl will be set when the referenced
  799. * struct iscsi_session is located from received ISID+TSIH in
  800. * iscsi_login_non_zero_tsih_s2().
  801. */
  802. if (!login->leading_connection) {
  803. ret = 0;
  804. goto out;
  805. }
  806. /*
  807. * This value is required in iscsi_login_zero_tsih_s2()
  808. */
  809. sess->sess_ops->SessionType = 0;
  810. /*
  811. * Locate incoming Initiator IQN reference from Storage Node.
  812. */
  813. sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  814. &conn->tpg->tpg_se_tpg, i_buf);
  815. if (!sess->se_sess->se_node_acl) {
  816. pr_err("iSCSI Initiator Node: %s is not authorized to"
  817. " access iSCSI target portal group: %hu.\n",
  818. i_buf, conn->tpg->tpgt);
  819. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  820. ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
  821. ret = -1;
  822. goto out;
  823. }
  824. ret = 0;
  825. out:
  826. kfree(tmpbuf);
  827. return ret;
  828. }
  829. struct iscsi_login *iscsi_target_init_negotiation(
  830. struct iscsi_np *np,
  831. struct iscsi_conn *conn,
  832. char *login_pdu)
  833. {
  834. struct iscsi_login *login;
  835. login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
  836. if (!login) {
  837. pr_err("Unable to allocate memory for struct iscsi_login.\n");
  838. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  839. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  840. return NULL;
  841. }
  842. login->req = kmemdup(login_pdu, ISCSI_HDR_LEN, GFP_KERNEL);
  843. if (!login->req) {
  844. pr_err("Unable to allocate memory for Login Request.\n");
  845. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  846. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  847. goto out;
  848. }
  849. login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  850. if (!login->req_buf) {
  851. pr_err("Unable to allocate memory for response buffer.\n");
  852. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  853. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  854. goto out;
  855. }
  856. /*
  857. * SessionType: Discovery
  858. *
  859. * Locates Default Portal
  860. *
  861. * SessionType: Normal
  862. *
  863. * Locates Target Portal from NP -> Target IQN
  864. */
  865. if (iscsi_target_locate_portal(np, conn, login) < 0) {
  866. goto out;
  867. }
  868. return login;
  869. out:
  870. kfree(login->req);
  871. kfree(login->req_buf);
  872. kfree(login);
  873. return NULL;
  874. }
  875. int iscsi_target_start_negotiation(
  876. struct iscsi_login *login,
  877. struct iscsi_conn *conn)
  878. {
  879. int ret = -1;
  880. login->rsp = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
  881. if (!login->rsp) {
  882. pr_err("Unable to allocate memory for"
  883. " Login Response.\n");
  884. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  885. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  886. ret = -1;
  887. goto out;
  888. }
  889. login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  890. if (!login->rsp_buf) {
  891. pr_err("Unable to allocate memory for"
  892. " request buffer.\n");
  893. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  894. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  895. ret = -1;
  896. goto out;
  897. }
  898. ret = iscsi_target_do_login(conn, login);
  899. out:
  900. if (ret != 0)
  901. iscsi_remove_failed_auth_entry(conn);
  902. iscsi_target_nego_release(login, conn);
  903. return ret;
  904. }
  905. void iscsi_target_nego_release(
  906. struct iscsi_login *login,
  907. struct iscsi_conn *conn)
  908. {
  909. kfree(login->req);
  910. kfree(login->rsp);
  911. kfree(login->req_buf);
  912. kfree(login->rsp_buf);
  913. kfree(login);
  914. }