iscsi_target_login.c 34 KB

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