iscsi_target_nego.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 = (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 = 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 ((login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2) {
  583. case 0:
  584. login_rsp->flags |= (0 & 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. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
  604. >> 2);
  605. break;
  606. }
  607. if (iscsi_target_do_login_io(conn, login) < 0)
  608. return -1;
  609. if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
  610. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
  611. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
  612. }
  613. }
  614. return 0;
  615. }
  616. static void iscsi_initiatorname_tolower(
  617. char *param_buf)
  618. {
  619. char *c;
  620. u32 iqn_size = strlen(param_buf), i;
  621. for (i = 0; i < iqn_size; i++) {
  622. c = &param_buf[i];
  623. if (!isupper(*c))
  624. continue;
  625. *c = tolower(*c);
  626. }
  627. }
  628. /*
  629. * Processes the first Login Request..
  630. */
  631. static int iscsi_target_locate_portal(
  632. struct iscsi_np *np,
  633. struct iscsi_conn *conn,
  634. struct iscsi_login *login)
  635. {
  636. char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
  637. char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
  638. struct iscsi_session *sess = conn->sess;
  639. struct iscsi_tiqn *tiqn;
  640. struct iscsi_login_req *login_req;
  641. u32 payload_length;
  642. int sessiontype = 0, ret = 0;
  643. login_req = (struct iscsi_login_req *) login->req;
  644. payload_length = ntoh24(login_req->dlength);
  645. login->first_request = 1;
  646. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  647. login->current_stage =
  648. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
  649. login->version_min = login_req->min_version;
  650. login->version_max = login_req->max_version;
  651. memcpy(login->isid, login_req->isid, 6);
  652. login->cmd_sn = be32_to_cpu(login_req->cmdsn);
  653. login->init_task_tag = login_req->itt;
  654. login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
  655. login->cid = be16_to_cpu(login_req->cid);
  656. login->tsih = be16_to_cpu(login_req->tsih);
  657. if (iscsi_target_get_initial_payload(conn, login) < 0)
  658. return -1;
  659. tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
  660. if (!tmpbuf) {
  661. pr_err("Unable to allocate memory for tmpbuf.\n");
  662. return -1;
  663. }
  664. memcpy(tmpbuf, login->req_buf, payload_length);
  665. tmpbuf[payload_length] = '\0';
  666. start = tmpbuf;
  667. end = (start + payload_length);
  668. /*
  669. * Locate the initial keys expected from the Initiator node in
  670. * the first login request in order to progress with the login phase.
  671. */
  672. while (start < end) {
  673. if (iscsi_extract_key_value(start, &key, &value) < 0) {
  674. ret = -1;
  675. goto out;
  676. }
  677. if (!strncmp(key, "InitiatorName", 13))
  678. i_buf = value;
  679. else if (!strncmp(key, "SessionType", 11))
  680. s_buf = value;
  681. else if (!strncmp(key, "TargetName", 10))
  682. t_buf = value;
  683. start += strlen(key) + strlen(value) + 2;
  684. }
  685. /*
  686. * See 5.3. Login Phase.
  687. */
  688. if (!i_buf) {
  689. pr_err("InitiatorName key not received"
  690. " in first login request.\n");
  691. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  692. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  693. ret = -1;
  694. goto out;
  695. }
  696. /*
  697. * Convert the incoming InitiatorName to lowercase following
  698. * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
  699. * are NOT case sensitive.
  700. */
  701. iscsi_initiatorname_tolower(i_buf);
  702. if (!s_buf) {
  703. if (!login->leading_connection)
  704. goto get_target;
  705. pr_err("SessionType key not received"
  706. " in first login request.\n");
  707. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  708. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  709. ret = -1;
  710. goto out;
  711. }
  712. /*
  713. * Use default portal group for discovery sessions.
  714. */
  715. sessiontype = strncmp(s_buf, DISCOVERY, 9);
  716. if (!sessiontype) {
  717. conn->tpg = iscsit_global->discovery_tpg;
  718. if (!login->leading_connection)
  719. goto get_target;
  720. sess->sess_ops->SessionType = 1;
  721. /*
  722. * Setup crc32c modules from libcrypto
  723. */
  724. if (iscsi_login_setup_crypto(conn) < 0) {
  725. pr_err("iscsi_login_setup_crypto() failed\n");
  726. ret = -1;
  727. goto out;
  728. }
  729. /*
  730. * Serialize access across the discovery struct iscsi_portal_group to
  731. * process login attempt.
  732. */
  733. if (iscsit_access_np(np, conn->tpg) < 0) {
  734. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  735. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  736. ret = -1;
  737. goto out;
  738. }
  739. ret = 0;
  740. goto out;
  741. }
  742. get_target:
  743. if (!t_buf) {
  744. pr_err("TargetName key not received"
  745. " in first login request while"
  746. " SessionType=Normal.\n");
  747. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  748. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  749. ret = -1;
  750. goto out;
  751. }
  752. /*
  753. * Locate Target IQN from Storage Node.
  754. */
  755. tiqn = iscsit_get_tiqn_for_login(t_buf);
  756. if (!tiqn) {
  757. pr_err("Unable to locate Target IQN: %s in"
  758. " Storage Node\n", t_buf);
  759. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  760. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  761. ret = -1;
  762. goto out;
  763. }
  764. pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
  765. /*
  766. * Locate Target Portal Group from Storage Node.
  767. */
  768. conn->tpg = iscsit_get_tpg_from_np(tiqn, np);
  769. if (!conn->tpg) {
  770. pr_err("Unable to locate Target Portal Group"
  771. " on %s\n", tiqn->tiqn);
  772. iscsit_put_tiqn_for_login(tiqn);
  773. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  774. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  775. ret = -1;
  776. goto out;
  777. }
  778. pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
  779. /*
  780. * Setup crc32c modules from libcrypto
  781. */
  782. if (iscsi_login_setup_crypto(conn) < 0) {
  783. pr_err("iscsi_login_setup_crypto() failed\n");
  784. ret = -1;
  785. goto out;
  786. }
  787. /*
  788. * Serialize access across the struct iscsi_portal_group to
  789. * process login attempt.
  790. */
  791. if (iscsit_access_np(np, conn->tpg) < 0) {
  792. iscsit_put_tiqn_for_login(tiqn);
  793. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  794. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  795. ret = -1;
  796. conn->tpg = NULL;
  797. goto out;
  798. }
  799. /*
  800. * conn->sess->node_acl will be set when the referenced
  801. * struct iscsi_session is located from received ISID+TSIH in
  802. * iscsi_login_non_zero_tsih_s2().
  803. */
  804. if (!login->leading_connection) {
  805. ret = 0;
  806. goto out;
  807. }
  808. /*
  809. * This value is required in iscsi_login_zero_tsih_s2()
  810. */
  811. sess->sess_ops->SessionType = 0;
  812. /*
  813. * Locate incoming Initiator IQN reference from Storage Node.
  814. */
  815. sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  816. &conn->tpg->tpg_se_tpg, i_buf);
  817. if (!sess->se_sess->se_node_acl) {
  818. pr_err("iSCSI Initiator Node: %s is not authorized to"
  819. " access iSCSI target portal group: %hu.\n",
  820. i_buf, conn->tpg->tpgt);
  821. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  822. ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
  823. ret = -1;
  824. goto out;
  825. }
  826. ret = 0;
  827. out:
  828. kfree(tmpbuf);
  829. return ret;
  830. }
  831. struct iscsi_login *iscsi_target_init_negotiation(
  832. struct iscsi_np *np,
  833. struct iscsi_conn *conn,
  834. char *login_pdu)
  835. {
  836. struct iscsi_login *login;
  837. login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
  838. if (!login) {
  839. pr_err("Unable to allocate memory for struct iscsi_login.\n");
  840. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  841. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  842. return NULL;
  843. }
  844. login->req = kmemdup(login_pdu, ISCSI_HDR_LEN, GFP_KERNEL);
  845. if (!login->req) {
  846. pr_err("Unable to allocate memory for Login Request.\n");
  847. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  848. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  849. goto out;
  850. }
  851. login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  852. if (!login->req_buf) {
  853. pr_err("Unable to allocate memory for response buffer.\n");
  854. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  855. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  856. goto out;
  857. }
  858. /*
  859. * SessionType: Discovery
  860. *
  861. * Locates Default Portal
  862. *
  863. * SessionType: Normal
  864. *
  865. * Locates Target Portal from NP -> Target IQN
  866. */
  867. if (iscsi_target_locate_portal(np, conn, login) < 0) {
  868. goto out;
  869. }
  870. return login;
  871. out:
  872. kfree(login->req);
  873. kfree(login->req_buf);
  874. kfree(login);
  875. return NULL;
  876. }
  877. int iscsi_target_start_negotiation(
  878. struct iscsi_login *login,
  879. struct iscsi_conn *conn)
  880. {
  881. int ret = -1;
  882. login->rsp = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
  883. if (!login->rsp) {
  884. pr_err("Unable to allocate memory for"
  885. " Login Response.\n");
  886. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  887. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  888. ret = -1;
  889. goto out;
  890. }
  891. login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  892. if (!login->rsp_buf) {
  893. pr_err("Unable to allocate memory for"
  894. " request buffer.\n");
  895. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  896. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  897. ret = -1;
  898. goto out;
  899. }
  900. ret = iscsi_target_do_login(conn, login);
  901. out:
  902. if (ret != 0)
  903. iscsi_remove_failed_auth_entry(conn);
  904. iscsi_target_nego_release(login, conn);
  905. return ret;
  906. }
  907. void iscsi_target_nego_release(
  908. struct iscsi_login *login,
  909. struct iscsi_conn *conn)
  910. {
  911. kfree(login->req);
  912. kfree(login->rsp);
  913. kfree(login->req_buf);
  914. kfree(login->rsp_buf);
  915. kfree(login);
  916. }