iscsi_target_login.c 34 KB

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