iscsi_target_login.c 34 KB

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