iscsi_target_login.c 35 KB

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