iscsi_target_login.c 34 KB

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