iscsi_target_login.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /*******************************************************************************
  2. * This file contains the login functions used by the iSCSI Target driver.
  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/string.h>
  21. #include <linux/kthread.h>
  22. #include <linux/crypto.h>
  23. #include <linux/idr.h>
  24. #include <scsi/iscsi_proto.h>
  25. #include <target/target_core_base.h>
  26. #include <target/target_core_fabric.h>
  27. #include "iscsi_target_core.h"
  28. #include "iscsi_target_tq.h"
  29. #include "iscsi_target_device.h"
  30. #include "iscsi_target_nego.h"
  31. #include "iscsi_target_erl0.h"
  32. #include "iscsi_target_erl2.h"
  33. #include "iscsi_target_login.h"
  34. #include "iscsi_target_stat.h"
  35. #include "iscsi_target_tpg.h"
  36. #include "iscsi_target_util.h"
  37. #include "iscsi_target.h"
  38. #include "iscsi_target_parameters.h"
  39. static int iscsi_login_init_conn(struct iscsi_conn *conn)
  40. {
  41. init_waitqueue_head(&conn->queues_wq);
  42. INIT_LIST_HEAD(&conn->conn_list);
  43. INIT_LIST_HEAD(&conn->conn_cmd_list);
  44. INIT_LIST_HEAD(&conn->immed_queue_list);
  45. INIT_LIST_HEAD(&conn->response_queue_list);
  46. init_completion(&conn->conn_post_wait_comp);
  47. init_completion(&conn->conn_wait_comp);
  48. init_completion(&conn->conn_wait_rcfr_comp);
  49. init_completion(&conn->conn_waiting_on_uc_comp);
  50. init_completion(&conn->conn_logout_comp);
  51. init_completion(&conn->rx_half_close_comp);
  52. init_completion(&conn->tx_half_close_comp);
  53. spin_lock_init(&conn->cmd_lock);
  54. spin_lock_init(&conn->conn_usage_lock);
  55. spin_lock_init(&conn->immed_queue_lock);
  56. spin_lock_init(&conn->nopin_timer_lock);
  57. spin_lock_init(&conn->response_queue_lock);
  58. spin_lock_init(&conn->state_lock);
  59. if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
  60. pr_err("Unable to allocate conn->conn_cpumask\n");
  61. return -ENOMEM;
  62. }
  63. return 0;
  64. }
  65. /*
  66. * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
  67. * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
  68. */
  69. int iscsi_login_setup_crypto(struct iscsi_conn *conn)
  70. {
  71. /*
  72. * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
  73. * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
  74. * to software 1x8 byte slicing from crc32c.ko
  75. */
  76. conn->conn_rx_hash.flags = 0;
  77. conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  78. CRYPTO_ALG_ASYNC);
  79. if (IS_ERR(conn->conn_rx_hash.tfm)) {
  80. pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
  81. return -ENOMEM;
  82. }
  83. conn->conn_tx_hash.flags = 0;
  84. conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  85. CRYPTO_ALG_ASYNC);
  86. if (IS_ERR(conn->conn_tx_hash.tfm)) {
  87. pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
  88. crypto_free_hash(conn->conn_rx_hash.tfm);
  89. return -ENOMEM;
  90. }
  91. return 0;
  92. }
  93. static int iscsi_login_check_initiator_version(
  94. struct iscsi_conn *conn,
  95. u8 version_max,
  96. u8 version_min)
  97. {
  98. if ((version_max != 0x00) || (version_min != 0x00)) {
  99. pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
  100. " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
  101. version_min, version_max);
  102. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  103. ISCSI_LOGIN_STATUS_NO_VERSION);
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
  109. {
  110. int sessiontype;
  111. struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
  112. struct iscsi_portal_group *tpg = conn->tpg;
  113. struct iscsi_session *sess = NULL, *sess_p = NULL;
  114. struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
  115. struct se_session *se_sess, *se_sess_tmp;
  116. initiatorname_param = iscsi_find_param_from_key(
  117. INITIATORNAME, conn->param_list);
  118. sessiontype_param = iscsi_find_param_from_key(
  119. SESSIONTYPE, conn->param_list);
  120. if (!initiatorname_param || !sessiontype_param) {
  121. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  122. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  123. return -1;
  124. }
  125. sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
  126. spin_lock_bh(&se_tpg->session_lock);
  127. list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
  128. sess_list) {
  129. sess_p = se_sess->fabric_sess_ptr;
  130. spin_lock(&sess_p->conn_lock);
  131. if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
  132. atomic_read(&sess_p->session_logout) ||
  133. (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
  134. spin_unlock(&sess_p->conn_lock);
  135. continue;
  136. }
  137. if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
  138. (!strcmp(sess_p->sess_ops->InitiatorName,
  139. initiatorname_param->value) &&
  140. (sess_p->sess_ops->SessionType == sessiontype))) {
  141. atomic_set(&sess_p->session_reinstatement, 1);
  142. spin_unlock(&sess_p->conn_lock);
  143. iscsit_inc_session_usage_count(sess_p);
  144. iscsit_stop_time2retain_timer(sess_p);
  145. sess = sess_p;
  146. break;
  147. }
  148. spin_unlock(&sess_p->conn_lock);
  149. }
  150. spin_unlock_bh(&se_tpg->session_lock);
  151. /*
  152. * If the Time2Retain handler has expired, the session is already gone.
  153. */
  154. if (!sess)
  155. return 0;
  156. pr_debug("%s iSCSI Session SID %u is still active for %s,"
  157. " preforming session reinstatement.\n", (sessiontype) ?
  158. "Discovery" : "Normal", sess->sid,
  159. sess->sess_ops->InitiatorName);
  160. spin_lock_bh(&sess->conn_lock);
  161. if (sess->session_state == TARG_SESS_STATE_FAILED) {
  162. spin_unlock_bh(&sess->conn_lock);
  163. iscsit_dec_session_usage_count(sess);
  164. target_put_session(sess->se_sess);
  165. return 0;
  166. }
  167. spin_unlock_bh(&sess->conn_lock);
  168. iscsit_stop_session(sess, 1, 1);
  169. iscsit_dec_session_usage_count(sess);
  170. target_put_session(sess->se_sess);
  171. return 0;
  172. }
  173. static void iscsi_login_set_conn_values(
  174. struct iscsi_session *sess,
  175. struct iscsi_conn *conn,
  176. __be16 cid)
  177. {
  178. conn->sess = sess;
  179. conn->cid = be16_to_cpu(cid);
  180. /*
  181. * Generate a random Status sequence number (statsn) for the new
  182. * iSCSI connection.
  183. */
  184. get_random_bytes(&conn->stat_sn, sizeof(u32));
  185. mutex_lock(&auth_id_lock);
  186. conn->auth_id = iscsit_global->auth_id++;
  187. mutex_unlock(&auth_id_lock);
  188. }
  189. /*
  190. * This is the leading connection of a new session,
  191. * or session reinstatement.
  192. */
  193. static int iscsi_login_zero_tsih_s1(
  194. struct iscsi_conn *conn,
  195. unsigned char *buf)
  196. {
  197. struct iscsi_session *sess = NULL;
  198. struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
  199. int ret;
  200. sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
  201. if (!sess) {
  202. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  203. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  204. pr_err("Could not allocate memory for session\n");
  205. return -ENOMEM;
  206. }
  207. iscsi_login_set_conn_values(sess, conn, pdu->cid);
  208. sess->init_task_tag = pdu->itt;
  209. memcpy(&sess->isid, pdu->isid, 6);
  210. sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
  211. INIT_LIST_HEAD(&sess->sess_conn_list);
  212. INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
  213. INIT_LIST_HEAD(&sess->cr_active_list);
  214. INIT_LIST_HEAD(&sess->cr_inactive_list);
  215. init_completion(&sess->async_msg_comp);
  216. init_completion(&sess->reinstatement_comp);
  217. init_completion(&sess->session_wait_comp);
  218. init_completion(&sess->session_waiting_on_uc_comp);
  219. mutex_init(&sess->cmdsn_mutex);
  220. spin_lock_init(&sess->conn_lock);
  221. spin_lock_init(&sess->cr_a_lock);
  222. spin_lock_init(&sess->cr_i_lock);
  223. spin_lock_init(&sess->session_usage_lock);
  224. spin_lock_init(&sess->ttt_lock);
  225. if (!idr_pre_get(&sess_idr, GFP_KERNEL)) {
  226. pr_err("idr_pre_get() for sess_idr failed\n");
  227. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  228. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  229. kfree(sess);
  230. return -ENOMEM;
  231. }
  232. spin_lock_bh(&sess_idr_lock);
  233. ret = idr_get_new(&sess_idr, NULL, &sess->session_index);
  234. spin_unlock_bh(&sess_idr_lock);
  235. if (ret < 0) {
  236. pr_err("idr_get_new() for sess_idr failed\n");
  237. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  238. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  239. kfree(sess);
  240. return -ENOMEM;
  241. }
  242. sess->creation_time = get_jiffies_64();
  243. spin_lock_init(&sess->session_stats_lock);
  244. /*
  245. * The FFP CmdSN window values will be allocated from the TPG's
  246. * Initiator Node's ACL once the login has been successfully completed.
  247. */
  248. sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
  249. sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
  250. if (!sess->sess_ops) {
  251. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  252. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  253. pr_err("Unable to allocate memory for"
  254. " struct iscsi_sess_ops.\n");
  255. kfree(sess);
  256. return -ENOMEM;
  257. }
  258. sess->se_sess = transport_init_session();
  259. if (IS_ERR(sess->se_sess)) {
  260. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  261. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  262. kfree(sess);
  263. return -ENOMEM;
  264. }
  265. return 0;
  266. }
  267. static int iscsi_login_zero_tsih_s2(
  268. struct iscsi_conn *conn)
  269. {
  270. struct iscsi_node_attrib *na;
  271. struct iscsi_session *sess = conn->sess;
  272. unsigned char buf[32];
  273. sess->tpg = conn->tpg;
  274. /*
  275. * Assign a new TPG Session Handle. Note this is protected with
  276. * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
  277. */
  278. sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
  279. if (!sess->tsih)
  280. sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
  281. /*
  282. * Create the default params from user defined values..
  283. */
  284. if (iscsi_copy_param_list(&conn->param_list,
  285. ISCSI_TPG_C(conn)->param_list, 1) < 0) {
  286. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  287. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  288. return -1;
  289. }
  290. iscsi_set_keys_to_negotiate(0, conn->param_list);
  291. if (sess->sess_ops->SessionType)
  292. return iscsi_set_keys_irrelevant_for_discovery(
  293. conn->param_list);
  294. na = iscsit_tpg_get_node_attrib(sess);
  295. /*
  296. * Need to send TargetPortalGroupTag back in first login response
  297. * on any iSCSI connection where the Initiator provides TargetName.
  298. * See 5.3.1. Login Phase Start
  299. *
  300. * In our case, we have already located the struct iscsi_tiqn at this point.
  301. */
  302. memset(buf, 0, 32);
  303. sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
  304. if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
  305. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  306. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  307. return -1;
  308. }
  309. /*
  310. * Workaround for Initiators that have broken connection recovery logic.
  311. *
  312. * "We would really like to get rid of this." Linux-iSCSI.org team
  313. */
  314. memset(buf, 0, 32);
  315. sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
  316. if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
  317. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  318. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  319. return -1;
  320. }
  321. if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
  322. return -1;
  323. return 0;
  324. }
  325. /*
  326. * Remove PSTATE_NEGOTIATE for the four FIM related keys.
  327. * The Initiator node will be able to enable FIM by proposing them itself.
  328. */
  329. int iscsi_login_disable_FIM_keys(
  330. struct iscsi_param_list *param_list,
  331. struct iscsi_conn *conn)
  332. {
  333. struct iscsi_param *param;
  334. param = iscsi_find_param_from_key("OFMarker", param_list);
  335. if (!param) {
  336. pr_err("iscsi_find_param_from_key() for"
  337. " OFMarker failed\n");
  338. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  339. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  340. return -1;
  341. }
  342. param->state &= ~PSTATE_NEGOTIATE;
  343. param = iscsi_find_param_from_key("OFMarkInt", param_list);
  344. if (!param) {
  345. pr_err("iscsi_find_param_from_key() for"
  346. " IFMarker failed\n");
  347. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  348. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  349. return -1;
  350. }
  351. param->state &= ~PSTATE_NEGOTIATE;
  352. param = iscsi_find_param_from_key("IFMarker", param_list);
  353. if (!param) {
  354. pr_err("iscsi_find_param_from_key() for"
  355. " IFMarker failed\n");
  356. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  357. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  358. return -1;
  359. }
  360. param->state &= ~PSTATE_NEGOTIATE;
  361. param = iscsi_find_param_from_key("IFMarkInt", param_list);
  362. if (!param) {
  363. pr_err("iscsi_find_param_from_key() for"
  364. " IFMarker failed\n");
  365. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  366. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  367. return -1;
  368. }
  369. param->state &= ~PSTATE_NEGOTIATE;
  370. return 0;
  371. }
  372. static int iscsi_login_non_zero_tsih_s1(
  373. struct iscsi_conn *conn,
  374. unsigned char *buf)
  375. {
  376. struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
  377. iscsi_login_set_conn_values(NULL, conn, pdu->cid);
  378. return 0;
  379. }
  380. /*
  381. * Add a new connection to an existing session.
  382. */
  383. static int iscsi_login_non_zero_tsih_s2(
  384. struct iscsi_conn *conn,
  385. unsigned char *buf)
  386. {
  387. struct iscsi_portal_group *tpg = conn->tpg;
  388. struct iscsi_session *sess = NULL, *sess_p = NULL;
  389. struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
  390. struct se_session *se_sess, *se_sess_tmp;
  391. struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
  392. spin_lock_bh(&se_tpg->session_lock);
  393. list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
  394. sess_list) {
  395. sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
  396. if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
  397. atomic_read(&sess_p->session_logout) ||
  398. (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
  399. continue;
  400. if (!memcmp(sess_p->isid, pdu->isid, 6) &&
  401. (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
  402. iscsit_inc_session_usage_count(sess_p);
  403. iscsit_stop_time2retain_timer(sess_p);
  404. sess = sess_p;
  405. break;
  406. }
  407. }
  408. spin_unlock_bh(&se_tpg->session_lock);
  409. /*
  410. * If the Time2Retain handler has expired, the session is already gone.
  411. */
  412. if (!sess) {
  413. pr_err("Initiator attempting to add a connection to"
  414. " a non-existent session, rejecting iSCSI Login.\n");
  415. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  416. ISCSI_LOGIN_STATUS_NO_SESSION);
  417. return -1;
  418. }
  419. /*
  420. * Stop the Time2Retain timer if this is a failed session, we restart
  421. * the timer if the login is not successful.
  422. */
  423. spin_lock_bh(&sess->conn_lock);
  424. if (sess->session_state == TARG_SESS_STATE_FAILED)
  425. atomic_set(&sess->session_continuation, 1);
  426. spin_unlock_bh(&sess->conn_lock);
  427. iscsi_login_set_conn_values(sess, conn, pdu->cid);
  428. if (iscsi_copy_param_list(&conn->param_list,
  429. ISCSI_TPG_C(conn)->param_list, 0) < 0) {
  430. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  431. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  432. return -1;
  433. }
  434. iscsi_set_keys_to_negotiate(0, conn->param_list);
  435. /*
  436. * Need to send TargetPortalGroupTag back in first login response
  437. * on any iSCSI connection where the Initiator provides TargetName.
  438. * See 5.3.1. Login Phase Start
  439. *
  440. * In our case, we have already located the struct iscsi_tiqn at this point.
  441. */
  442. memset(buf, 0, 32);
  443. sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
  444. if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
  445. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  446. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  447. return -1;
  448. }
  449. return iscsi_login_disable_FIM_keys(conn->param_list, conn);
  450. }
  451. int iscsi_login_post_auth_non_zero_tsih(
  452. struct iscsi_conn *conn,
  453. u16 cid,
  454. u32 exp_statsn)
  455. {
  456. struct iscsi_conn *conn_ptr = NULL;
  457. struct iscsi_conn_recovery *cr = NULL;
  458. struct iscsi_session *sess = conn->sess;
  459. /*
  460. * By following item 5 in the login table, if we have found
  461. * an existing ISID and a valid/existing TSIH and an existing
  462. * CID we do connection reinstatement. Currently we dont not
  463. * support it so we send back an non-zero status class to the
  464. * initiator and release the new connection.
  465. */
  466. conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
  467. if (conn_ptr) {
  468. pr_err("Connection exists with CID %hu for %s,"
  469. " performing connection reinstatement.\n",
  470. conn_ptr->cid, sess->sess_ops->InitiatorName);
  471. iscsit_connection_reinstatement_rcfr(conn_ptr);
  472. iscsit_dec_conn_usage_count(conn_ptr);
  473. }
  474. /*
  475. * Check for any connection recovery entires containing CID.
  476. * We use the original ExpStatSN sent in the first login request
  477. * to acknowledge commands for the failed connection.
  478. *
  479. * Also note that an explict logout may have already been sent,
  480. * but the response may not be sent due to additional connection
  481. * loss.
  482. */
  483. if (sess->sess_ops->ErrorRecoveryLevel == 2) {
  484. cr = iscsit_get_inactive_connection_recovery_entry(
  485. sess, cid);
  486. if (cr) {
  487. pr_debug("Performing implicit logout"
  488. " for connection recovery on CID: %hu\n",
  489. conn->cid);
  490. iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
  491. }
  492. }
  493. /*
  494. * Else we follow item 4 from the login table in that we have
  495. * found an existing ISID and a valid/existing TSIH and a new
  496. * CID we go ahead and continue to add a new connection to the
  497. * session.
  498. */
  499. pr_debug("Adding CID %hu to existing session for %s.\n",
  500. cid, sess->sess_ops->InitiatorName);
  501. if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
  502. pr_err("Adding additional connection to this session"
  503. " would exceed MaxConnections %d, login failed.\n",
  504. sess->sess_ops->MaxConnections);
  505. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  506. ISCSI_LOGIN_STATUS_ISID_ERROR);
  507. return -1;
  508. }
  509. return 0;
  510. }
  511. static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
  512. {
  513. struct iscsi_session *sess = conn->sess;
  514. if (!sess->sess_ops->SessionType)
  515. iscsit_start_nopin_timer(conn);
  516. }
  517. static int iscsi_post_login_handler(
  518. struct iscsi_np *np,
  519. struct iscsi_conn *conn,
  520. u8 zero_tsih)
  521. {
  522. int stop_timer = 0;
  523. struct iscsi_session *sess = conn->sess;
  524. struct se_session *se_sess = sess->se_sess;
  525. struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
  526. struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
  527. struct iscsi_thread_set *ts;
  528. iscsit_inc_conn_usage_count(conn);
  529. iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
  530. ISCSI_LOGIN_STATUS_ACCEPT);
  531. pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
  532. conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
  533. iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
  534. iscsit_set_sync_and_steering_values(conn);
  535. /*
  536. * SCSI Initiator -> SCSI Target Port Mapping
  537. */
  538. ts = iscsi_get_thread_set();
  539. if (!zero_tsih) {
  540. iscsi_set_session_parameters(sess->sess_ops,
  541. conn->param_list, 0);
  542. iscsi_release_param_list(conn->param_list);
  543. conn->param_list = NULL;
  544. spin_lock_bh(&sess->conn_lock);
  545. atomic_set(&sess->session_continuation, 0);
  546. if (sess->session_state == TARG_SESS_STATE_FAILED) {
  547. pr_debug("Moving to"
  548. " TARG_SESS_STATE_LOGGED_IN.\n");
  549. sess->session_state = TARG_SESS_STATE_LOGGED_IN;
  550. stop_timer = 1;
  551. }
  552. pr_debug("iSCSI Login successful on CID: %hu from %s to"
  553. " %s:%hu,%hu\n", conn->cid, conn->login_ip,
  554. conn->local_ip, conn->local_port, tpg->tpgt);
  555. list_add_tail(&conn->conn_list, &sess->sess_conn_list);
  556. atomic_inc(&sess->nconn);
  557. pr_debug("Incremented iSCSI Connection count to %hu"
  558. " from node: %s\n", atomic_read(&sess->nconn),
  559. sess->sess_ops->InitiatorName);
  560. spin_unlock_bh(&sess->conn_lock);
  561. iscsi_post_login_start_timers(conn);
  562. iscsi_activate_thread_set(conn, ts);
  563. /*
  564. * Determine CPU mask to ensure connection's RX and TX kthreads
  565. * are scheduled on the same CPU.
  566. */
  567. iscsit_thread_get_cpumask(conn);
  568. conn->conn_rx_reset_cpumask = 1;
  569. conn->conn_tx_reset_cpumask = 1;
  570. iscsit_dec_conn_usage_count(conn);
  571. if (stop_timer) {
  572. spin_lock_bh(&se_tpg->session_lock);
  573. iscsit_stop_time2retain_timer(sess);
  574. spin_unlock_bh(&se_tpg->session_lock);
  575. }
  576. iscsit_dec_session_usage_count(sess);
  577. return 0;
  578. }
  579. iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
  580. iscsi_release_param_list(conn->param_list);
  581. conn->param_list = NULL;
  582. iscsit_determine_maxcmdsn(sess);
  583. spin_lock_bh(&se_tpg->session_lock);
  584. __transport_register_session(&sess->tpg->tpg_se_tpg,
  585. se_sess->se_node_acl, se_sess, sess);
  586. pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
  587. sess->session_state = TARG_SESS_STATE_LOGGED_IN;
  588. pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
  589. conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
  590. tpg->tpgt);
  591. spin_lock_bh(&sess->conn_lock);
  592. list_add_tail(&conn->conn_list, &sess->sess_conn_list);
  593. atomic_inc(&sess->nconn);
  594. pr_debug("Incremented iSCSI Connection count to %hu from node:"
  595. " %s\n", atomic_read(&sess->nconn),
  596. sess->sess_ops->InitiatorName);
  597. spin_unlock_bh(&sess->conn_lock);
  598. sess->sid = tpg->sid++;
  599. if (!sess->sid)
  600. sess->sid = tpg->sid++;
  601. pr_debug("Established iSCSI session from node: %s\n",
  602. sess->sess_ops->InitiatorName);
  603. tpg->nsessions++;
  604. if (tpg->tpg_tiqn)
  605. tpg->tpg_tiqn->tiqn_nsessions++;
  606. pr_debug("Incremented number of active iSCSI sessions to %u on"
  607. " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
  608. spin_unlock_bh(&se_tpg->session_lock);
  609. iscsi_post_login_start_timers(conn);
  610. iscsi_activate_thread_set(conn, ts);
  611. /*
  612. * Determine CPU mask to ensure connection's RX and TX kthreads
  613. * are scheduled on the same CPU.
  614. */
  615. iscsit_thread_get_cpumask(conn);
  616. conn->conn_rx_reset_cpumask = 1;
  617. conn->conn_tx_reset_cpumask = 1;
  618. iscsit_dec_conn_usage_count(conn);
  619. return 0;
  620. }
  621. static void iscsi_handle_login_thread_timeout(unsigned long data)
  622. {
  623. struct iscsi_np *np = (struct iscsi_np *) data;
  624. spin_lock_bh(&np->np_thread_lock);
  625. pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
  626. np->np_ip, np->np_port);
  627. if (np->np_login_timer_flags & ISCSI_TF_STOP) {
  628. spin_unlock_bh(&np->np_thread_lock);
  629. return;
  630. }
  631. if (np->np_thread)
  632. send_sig(SIGINT, np->np_thread, 1);
  633. np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
  634. spin_unlock_bh(&np->np_thread_lock);
  635. }
  636. static void iscsi_start_login_thread_timer(struct iscsi_np *np)
  637. {
  638. /*
  639. * This used the TA_LOGIN_TIMEOUT constant because at this
  640. * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
  641. */
  642. spin_lock_bh(&np->np_thread_lock);
  643. init_timer(&np->np_login_timer);
  644. np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
  645. np->np_login_timer.data = (unsigned long)np;
  646. np->np_login_timer.function = iscsi_handle_login_thread_timeout;
  647. np->np_login_timer_flags &= ~ISCSI_TF_STOP;
  648. np->np_login_timer_flags |= ISCSI_TF_RUNNING;
  649. add_timer(&np->np_login_timer);
  650. pr_debug("Added timeout timer to iSCSI login request for"
  651. " %u seconds.\n", TA_LOGIN_TIMEOUT);
  652. spin_unlock_bh(&np->np_thread_lock);
  653. }
  654. static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
  655. {
  656. spin_lock_bh(&np->np_thread_lock);
  657. if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
  658. spin_unlock_bh(&np->np_thread_lock);
  659. return;
  660. }
  661. np->np_login_timer_flags |= ISCSI_TF_STOP;
  662. spin_unlock_bh(&np->np_thread_lock);
  663. del_timer_sync(&np->np_login_timer);
  664. spin_lock_bh(&np->np_thread_lock);
  665. np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
  666. spin_unlock_bh(&np->np_thread_lock);
  667. }
  668. int iscsi_target_setup_login_socket(
  669. struct iscsi_np *np,
  670. struct __kernel_sockaddr_storage *sockaddr)
  671. {
  672. struct socket *sock;
  673. int backlog = 5, ret, opt = 0, len;
  674. switch (np->np_network_transport) {
  675. case ISCSI_TCP:
  676. np->np_ip_proto = IPPROTO_TCP;
  677. np->np_sock_type = SOCK_STREAM;
  678. break;
  679. case ISCSI_SCTP_TCP:
  680. np->np_ip_proto = IPPROTO_SCTP;
  681. np->np_sock_type = SOCK_STREAM;
  682. break;
  683. case ISCSI_SCTP_UDP:
  684. np->np_ip_proto = IPPROTO_SCTP;
  685. np->np_sock_type = SOCK_SEQPACKET;
  686. break;
  687. case ISCSI_IWARP_TCP:
  688. case ISCSI_IWARP_SCTP:
  689. case ISCSI_INFINIBAND:
  690. default:
  691. pr_err("Unsupported network_transport: %d\n",
  692. np->np_network_transport);
  693. return -EINVAL;
  694. }
  695. ret = sock_create(sockaddr->ss_family, np->np_sock_type,
  696. np->np_ip_proto, &sock);
  697. if (ret < 0) {
  698. pr_err("sock_create() failed.\n");
  699. return ret;
  700. }
  701. np->np_socket = sock;
  702. /*
  703. * Setup the np->np_sockaddr from the passed sockaddr setup
  704. * in iscsi_target_configfs.c code..
  705. */
  706. memcpy(&np->np_sockaddr, sockaddr,
  707. sizeof(struct __kernel_sockaddr_storage));
  708. if (sockaddr->ss_family == AF_INET6)
  709. len = sizeof(struct sockaddr_in6);
  710. else
  711. len = sizeof(struct sockaddr_in);
  712. /*
  713. * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
  714. */
  715. /* FIXME: Someone please explain why this is endian-safe */
  716. opt = 1;
  717. if (np->np_network_transport == ISCSI_TCP) {
  718. ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  719. (char *)&opt, sizeof(opt));
  720. if (ret < 0) {
  721. pr_err("kernel_setsockopt() for TCP_NODELAY"
  722. " failed: %d\n", ret);
  723. goto fail;
  724. }
  725. }
  726. /* FIXME: Someone please explain why this is endian-safe */
  727. ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  728. (char *)&opt, sizeof(opt));
  729. if (ret < 0) {
  730. pr_err("kernel_setsockopt() for SO_REUSEADDR"
  731. " failed\n");
  732. goto fail;
  733. }
  734. ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
  735. (char *)&opt, sizeof(opt));
  736. if (ret < 0) {
  737. pr_err("kernel_setsockopt() for IP_FREEBIND"
  738. " failed\n");
  739. goto fail;
  740. }
  741. ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
  742. if (ret < 0) {
  743. pr_err("kernel_bind() failed: %d\n", ret);
  744. goto fail;
  745. }
  746. ret = kernel_listen(sock, backlog);
  747. if (ret != 0) {
  748. pr_err("kernel_listen() failed: %d\n", ret);
  749. goto fail;
  750. }
  751. return 0;
  752. fail:
  753. np->np_socket = NULL;
  754. if (sock)
  755. sock_release(sock);
  756. return ret;
  757. }
  758. static int __iscsi_target_login_thread(struct iscsi_np *np)
  759. {
  760. u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0;
  761. int err, ret = 0, stop;
  762. struct iscsi_conn *conn = NULL;
  763. struct iscsi_login *login;
  764. struct iscsi_portal_group *tpg = NULL;
  765. struct socket *new_sock, *sock;
  766. struct kvec iov;
  767. struct iscsi_login_req *pdu;
  768. struct sockaddr_in sock_in;
  769. struct sockaddr_in6 sock_in6;
  770. flush_signals(current);
  771. sock = np->np_socket;
  772. spin_lock_bh(&np->np_thread_lock);
  773. if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
  774. np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
  775. complete(&np->np_restart_comp);
  776. } else {
  777. np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
  778. }
  779. spin_unlock_bh(&np->np_thread_lock);
  780. if (kernel_accept(sock, &new_sock, 0) < 0) {
  781. spin_lock_bh(&np->np_thread_lock);
  782. if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
  783. spin_unlock_bh(&np->np_thread_lock);
  784. complete(&np->np_restart_comp);
  785. /* Get another socket */
  786. return 1;
  787. }
  788. spin_unlock_bh(&np->np_thread_lock);
  789. goto out;
  790. }
  791. iscsi_start_login_thread_timer(np);
  792. conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
  793. if (!conn) {
  794. pr_err("Could not allocate memory for"
  795. " new connection\n");
  796. sock_release(new_sock);
  797. /* Get another socket */
  798. return 1;
  799. }
  800. pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
  801. conn->conn_state = TARG_CONN_STATE_FREE;
  802. conn->sock = new_sock;
  803. pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
  804. conn->conn_state = TARG_CONN_STATE_XPT_UP;
  805. /*
  806. * Allocate conn->conn_ops early as a failure calling
  807. * iscsit_tx_login_rsp() below will call tx_data().
  808. */
  809. conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
  810. if (!conn->conn_ops) {
  811. pr_err("Unable to allocate memory for"
  812. " struct iscsi_conn_ops.\n");
  813. goto new_sess_out;
  814. }
  815. /*
  816. * Perform the remaining iSCSI connection initialization items..
  817. */
  818. if (iscsi_login_init_conn(conn) < 0)
  819. goto new_sess_out;
  820. memset(buffer, 0, ISCSI_HDR_LEN);
  821. memset(&iov, 0, sizeof(struct kvec));
  822. iov.iov_base = buffer;
  823. iov.iov_len = ISCSI_HDR_LEN;
  824. if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) {
  825. pr_err("rx_data() returned an error.\n");
  826. goto new_sess_out;
  827. }
  828. iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK);
  829. if (!(iscsi_opcode & ISCSI_OP_LOGIN)) {
  830. pr_err("First opcode is not login request,"
  831. " failing login request.\n");
  832. goto new_sess_out;
  833. }
  834. pdu = (struct iscsi_login_req *) buffer;
  835. /*
  836. * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
  837. * when Status-Class != 0.
  838. */
  839. conn->login_itt = pdu->itt;
  840. spin_lock_bh(&np->np_thread_lock);
  841. if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
  842. spin_unlock_bh(&np->np_thread_lock);
  843. pr_err("iSCSI Network Portal on %s:%hu currently not"
  844. " active.\n", np->np_ip, np->np_port);
  845. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  846. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  847. goto new_sess_out;
  848. }
  849. spin_unlock_bh(&np->np_thread_lock);
  850. if (np->np_sockaddr.ss_family == AF_INET6) {
  851. memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
  852. if (conn->sock->ops->getname(conn->sock,
  853. (struct sockaddr *)&sock_in6, &err, 1) < 0) {
  854. pr_err("sock_ops->getname() failed.\n");
  855. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  856. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  857. goto new_sess_out;
  858. }
  859. snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
  860. &sock_in6.sin6_addr.in6_u);
  861. conn->login_port = ntohs(sock_in6.sin6_port);
  862. if (conn->sock->ops->getname(conn->sock,
  863. (struct sockaddr *)&sock_in6, &err, 0) < 0) {
  864. pr_err("sock_ops->getname() failed.\n");
  865. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  866. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  867. goto new_sess_out;
  868. }
  869. snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
  870. &sock_in6.sin6_addr.in6_u);
  871. conn->local_port = ntohs(sock_in6.sin6_port);
  872. } else {
  873. memset(&sock_in, 0, sizeof(struct sockaddr_in));
  874. if (conn->sock->ops->getname(conn->sock,
  875. (struct sockaddr *)&sock_in, &err, 1) < 0) {
  876. pr_err("sock_ops->getname() failed.\n");
  877. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  878. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  879. goto new_sess_out;
  880. }
  881. sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr);
  882. conn->login_port = ntohs(sock_in.sin_port);
  883. if (conn->sock->ops->getname(conn->sock,
  884. (struct sockaddr *)&sock_in, &err, 0) < 0) {
  885. pr_err("sock_ops->getname() failed.\n");
  886. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  887. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  888. goto new_sess_out;
  889. }
  890. sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr);
  891. conn->local_port = ntohs(sock_in.sin_port);
  892. }
  893. conn->network_transport = np->np_network_transport;
  894. pr_debug("Received iSCSI login request from %s on %s Network"
  895. " Portal %s:%hu\n", conn->login_ip,
  896. (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP",
  897. conn->local_ip, conn->local_port);
  898. pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
  899. conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
  900. if (iscsi_login_check_initiator_version(conn, pdu->max_version,
  901. pdu->min_version) < 0)
  902. goto new_sess_out;
  903. zero_tsih = (pdu->tsih == 0x0000);
  904. if (zero_tsih) {
  905. /*
  906. * This is the leading connection of a new session.
  907. * We wait until after authentication to check for
  908. * session reinstatement.
  909. */
  910. if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
  911. goto new_sess_out;
  912. } else {
  913. /*
  914. * Add a new connection to an existing session.
  915. * We check for a non-existant session in
  916. * iscsi_login_non_zero_tsih_s2() below based
  917. * on ISID/TSIH, but wait until after authentication
  918. * to check for connection reinstatement, etc.
  919. */
  920. if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
  921. goto new_sess_out;
  922. }
  923. /*
  924. * This will process the first login request, and call
  925. * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
  926. */
  927. login = iscsi_target_init_negotiation(np, conn, buffer);
  928. if (!login) {
  929. tpg = conn->tpg;
  930. goto new_sess_out;
  931. }
  932. tpg = conn->tpg;
  933. if (!tpg) {
  934. pr_err("Unable to locate struct iscsi_conn->tpg\n");
  935. goto new_sess_out;
  936. }
  937. if (zero_tsih) {
  938. if (iscsi_login_zero_tsih_s2(conn) < 0) {
  939. iscsi_target_nego_release(login, conn);
  940. goto new_sess_out;
  941. }
  942. } else {
  943. if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) {
  944. iscsi_target_nego_release(login, conn);
  945. goto old_sess_out;
  946. }
  947. }
  948. if (iscsi_target_start_negotiation(login, conn) < 0)
  949. goto new_sess_out;
  950. if (!conn->sess) {
  951. pr_err("struct iscsi_conn session pointer is NULL!\n");
  952. goto new_sess_out;
  953. }
  954. iscsi_stop_login_thread_timer(np);
  955. if (signal_pending(current))
  956. goto new_sess_out;
  957. ret = iscsi_post_login_handler(np, conn, zero_tsih);
  958. if (ret < 0)
  959. goto new_sess_out;
  960. iscsit_deaccess_np(np, tpg);
  961. tpg = NULL;
  962. /* Get another socket */
  963. return 1;
  964. new_sess_out:
  965. pr_err("iSCSI Login negotiation failed.\n");
  966. iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  967. ISCSI_LOGIN_STATUS_INIT_ERR);
  968. if (!zero_tsih || !conn->sess)
  969. goto old_sess_out;
  970. if (conn->sess->se_sess)
  971. transport_free_session(conn->sess->se_sess);
  972. if (conn->sess->session_index != 0) {
  973. spin_lock_bh(&sess_idr_lock);
  974. idr_remove(&sess_idr, conn->sess->session_index);
  975. spin_unlock_bh(&sess_idr_lock);
  976. }
  977. if (conn->sess->sess_ops)
  978. kfree(conn->sess->sess_ops);
  979. if (conn->sess)
  980. kfree(conn->sess);
  981. old_sess_out:
  982. iscsi_stop_login_thread_timer(np);
  983. /*
  984. * If login negotiation fails check if the Time2Retain timer
  985. * needs to be restarted.
  986. */
  987. if (!zero_tsih && conn->sess) {
  988. spin_lock_bh(&conn->sess->conn_lock);
  989. if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
  990. struct se_portal_group *se_tpg =
  991. &ISCSI_TPG_C(conn)->tpg_se_tpg;
  992. atomic_set(&conn->sess->session_continuation, 0);
  993. spin_unlock_bh(&conn->sess->conn_lock);
  994. spin_lock_bh(&se_tpg->session_lock);
  995. iscsit_start_time2retain_handler(conn->sess);
  996. spin_unlock_bh(&se_tpg->session_lock);
  997. } else
  998. spin_unlock_bh(&conn->sess->conn_lock);
  999. iscsit_dec_session_usage_count(conn->sess);
  1000. }
  1001. if (!IS_ERR(conn->conn_rx_hash.tfm))
  1002. crypto_free_hash(conn->conn_rx_hash.tfm);
  1003. if (!IS_ERR(conn->conn_tx_hash.tfm))
  1004. crypto_free_hash(conn->conn_tx_hash.tfm);
  1005. if (conn->conn_cpumask)
  1006. free_cpumask_var(conn->conn_cpumask);
  1007. kfree(conn->conn_ops);
  1008. if (conn->param_list) {
  1009. iscsi_release_param_list(conn->param_list);
  1010. conn->param_list = NULL;
  1011. }
  1012. if (conn->sock)
  1013. sock_release(conn->sock);
  1014. kfree(conn);
  1015. if (tpg) {
  1016. iscsit_deaccess_np(np, tpg);
  1017. tpg = NULL;
  1018. }
  1019. out:
  1020. stop = kthread_should_stop();
  1021. if (!stop && signal_pending(current)) {
  1022. spin_lock_bh(&np->np_thread_lock);
  1023. stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
  1024. spin_unlock_bh(&np->np_thread_lock);
  1025. }
  1026. /* Wait for another socket.. */
  1027. if (!stop)
  1028. return 1;
  1029. iscsi_stop_login_thread_timer(np);
  1030. spin_lock_bh(&np->np_thread_lock);
  1031. np->np_thread_state = ISCSI_NP_THREAD_EXIT;
  1032. spin_unlock_bh(&np->np_thread_lock);
  1033. return 0;
  1034. }
  1035. int iscsi_target_login_thread(void *arg)
  1036. {
  1037. struct iscsi_np *np = arg;
  1038. int ret;
  1039. allow_signal(SIGINT);
  1040. while (!kthread_should_stop()) {
  1041. ret = __iscsi_target_login_thread(np);
  1042. /*
  1043. * We break and exit here unless another sock_accept() call
  1044. * is expected.
  1045. */
  1046. if (ret != 1)
  1047. break;
  1048. }
  1049. return 0;
  1050. }