iscsi_target_login.c 34 KB

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