iscsi_target_nego.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 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 successfully.\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 = &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 = kmemdup(login_pdu, 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. login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  860. if (!login->req_buf) {
  861. pr_err("Unable to allocate memory for response buffer.\n");
  862. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  863. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  864. goto out;
  865. }
  866. /*
  867. * SessionType: Discovery
  868. *
  869. * Locates Default Portal
  870. *
  871. * SessionType: Normal
  872. *
  873. * Locates Target Portal from NP -> Target IQN
  874. */
  875. if (iscsi_target_locate_portal(np, conn, login) < 0) {
  876. pr_err("iSCSI Login negotiation failed.\n");
  877. goto out;
  878. }
  879. return login;
  880. out:
  881. kfree(login->req);
  882. kfree(login->req_buf);
  883. kfree(login);
  884. return NULL;
  885. }
  886. int iscsi_target_start_negotiation(
  887. struct iscsi_login *login,
  888. struct iscsi_conn *conn)
  889. {
  890. int ret = -1;
  891. login->rsp = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
  892. if (!login->rsp) {
  893. pr_err("Unable to allocate memory for"
  894. " Login Response.\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. login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  901. if (!login->rsp_buf) {
  902. pr_err("Unable to allocate memory for"
  903. " request buffer.\n");
  904. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  905. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  906. ret = -1;
  907. goto out;
  908. }
  909. ret = iscsi_target_do_login(conn, login);
  910. out:
  911. if (ret != 0)
  912. iscsi_remove_failed_auth_entry(conn);
  913. iscsi_target_nego_release(login, conn);
  914. return ret;
  915. }
  916. void iscsi_target_nego_release(
  917. struct iscsi_login *login,
  918. struct iscsi_conn *conn)
  919. {
  920. kfree(login->req);
  921. kfree(login->rsp);
  922. kfree(login->req_buf);
  923. kfree(login->rsp_buf);
  924. kfree(login);
  925. }