iscsi_target_login.c 34 KB

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