iscsi_target_nego.c 27 KB

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