iscsi_target_nego.c 27 KB

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