iscsi_target_login.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. /*******************************************************************************
  2. * This file contains the login functions used by the iSCSI Target driver.
  3. *
  4. * (c) Copyright 2007-2013 Datera, Inc.
  5. *
  6. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. ******************************************************************************/
  18. #include <linux/string.h>
  19. #include <linux/kthread.h>
  20. #include <linux/crypto.h>
  21. #include <linux/idr.h>
  22. #include <scsi/iscsi_proto.h>
  23. #include <target/target_core_base.h>
  24. #include <target/target_core_fabric.h>
  25. #include <target/iscsi/iscsi_target_core.h>
  26. #include <target/iscsi/iscsi_target_stat.h>
  27. #include "iscsi_target_device.h"
  28. #include "iscsi_target_nego.h"
  29. #include "iscsi_target_erl0.h"
  30. #include "iscsi_target_erl2.h"
  31. #include "iscsi_target_login.h"
  32. #include "iscsi_target_tpg.h"
  33. #include "iscsi_target_util.h"
  34. #include "iscsi_target.h"
  35. #include "iscsi_target_parameters.h"
  36. #include <target/iscsi/iscsi_transport.h>
  37. static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
  38. {
  39. struct iscsi_login *login;
  40. login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
  41. if (!login) {
  42. pr_err("Unable to allocate memory for struct iscsi_login.\n");
  43. return NULL;
  44. }
  45. conn->login = login;
  46. login->conn = conn;
  47. login->first_request = 1;
  48. login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  49. if (!login->req_buf) {
  50. pr_err("Unable to allocate memory for response buffer.\n");
  51. goto out_login;
  52. }
  53. login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  54. if (!login->rsp_buf) {
  55. pr_err("Unable to allocate memory for request buffer.\n");
  56. goto out_req_buf;
  57. }
  58. conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
  59. if (!conn->conn_ops) {
  60. pr_err("Unable to allocate memory for"
  61. " struct iscsi_conn_ops.\n");
  62. goto out_rsp_buf;
  63. }
  64. init_waitqueue_head(&conn->queues_wq);
  65. INIT_LIST_HEAD(&conn->conn_list);
  66. INIT_LIST_HEAD(&conn->conn_cmd_list);
  67. INIT_LIST_HEAD(&conn->immed_queue_list);
  68. INIT_LIST_HEAD(&conn->response_queue_list);
  69. init_completion(&conn->conn_post_wait_comp);
  70. init_completion(&conn->conn_wait_comp);
  71. init_completion(&conn->conn_wait_rcfr_comp);
  72. init_completion(&conn->conn_waiting_on_uc_comp);
  73. init_completion(&conn->conn_logout_comp);
  74. init_completion(&conn->rx_half_close_comp);
  75. init_completion(&conn->tx_half_close_comp);
  76. init_completion(&conn->rx_login_comp);
  77. spin_lock_init(&conn->cmd_lock);
  78. spin_lock_init(&conn->conn_usage_lock);
  79. spin_lock_init(&conn->immed_queue_lock);
  80. spin_lock_init(&conn->nopin_timer_lock);
  81. spin_lock_init(&conn->response_queue_lock);
  82. spin_lock_init(&conn->state_lock);
  83. if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
  84. pr_err("Unable to allocate conn->conn_cpumask\n");
  85. goto out_conn_ops;
  86. }
  87. conn->conn_login = login;
  88. return login;
  89. out_conn_ops:
  90. kfree(conn->conn_ops);
  91. out_rsp_buf:
  92. kfree(login->rsp_buf);
  93. out_req_buf:
  94. kfree(login->req_buf);
  95. out_login:
  96. kfree(login);
  97. return NULL;
  98. }
  99. /*
  100. * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
  101. * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
  102. */
  103. int iscsi_login_setup_crypto(struct iscsi_conn *conn)
  104. {
  105. /*
  106. * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
  107. * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
  108. * to software 1x8 byte slicing from crc32c.ko
  109. */
  110. conn->conn_rx_hash.flags = 0;
  111. conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  112. CRYPTO_ALG_ASYNC);
  113. if (IS_ERR(conn->conn_rx_hash.tfm)) {
  114. pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
  115. return -ENOMEM;
  116. }
  117. conn->conn_tx_hash.flags = 0;
  118. conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  119. CRYPTO_ALG_ASYNC);
  120. if (IS_ERR(conn->conn_tx_hash.tfm)) {
  121. pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
  122. crypto_free_hash(conn->conn_rx_hash.tfm);
  123. return -ENOMEM;
  124. }
  125. return 0;
  126. }
  127. static int iscsi_login_check_initiator_version(
  128. struct iscsi_conn *conn,
  129. u8 version_max,
  130. u8 version_min)
  131. {
  132. if ((version_max != 0x00) || (version_min != 0x00)) {
  133. pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
  134. " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
  135. version_min, version_max);
  136. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  137. ISCSI_LOGIN_STATUS_NO_VERSION);
  138. return -1;
  139. }
  140. return 0;
  141. }
  142. int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
  143. {
  144. int sessiontype;
  145. struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
  146. struct iscsi_portal_group *tpg = conn->tpg;
  147. struct iscsi_session *sess = NULL, *sess_p = NULL;
  148. struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
  149. struct se_session *se_sess, *se_sess_tmp;
  150. initiatorname_param = iscsi_find_param_from_key(
  151. INITIATORNAME, conn->param_list);
  152. sessiontype_param = iscsi_find_param_from_key(
  153. SESSIONTYPE, conn->param_list);
  154. if (!initiatorname_param || !sessiontype_param) {
  155. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  156. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  157. return -1;
  158. }
  159. sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
  160. spin_lock_bh(&se_tpg->session_lock);
  161. list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
  162. sess_list) {
  163. sess_p = se_sess->fabric_sess_ptr;
  164. spin_lock(&sess_p->conn_lock);
  165. if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
  166. atomic_read(&sess_p->session_logout) ||
  167. (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
  168. spin_unlock(&sess_p->conn_lock);
  169. continue;
  170. }
  171. if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
  172. (!strcmp(sess_p->sess_ops->InitiatorName,
  173. initiatorname_param->value) &&
  174. (sess_p->sess_ops->SessionType == sessiontype))) {
  175. atomic_set(&sess_p->session_reinstatement, 1);
  176. atomic_set(&sess_p->session_fall_back_to_erl0, 1);
  177. spin_unlock(&sess_p->conn_lock);
  178. iscsit_inc_session_usage_count(sess_p);
  179. iscsit_stop_time2retain_timer(sess_p);
  180. sess = sess_p;
  181. break;
  182. }
  183. spin_unlock(&sess_p->conn_lock);
  184. }
  185. spin_unlock_bh(&se_tpg->session_lock);
  186. /*
  187. * If the Time2Retain handler has expired, the session is already gone.
  188. */
  189. if (!sess)
  190. return 0;
  191. pr_debug("%s iSCSI Session SID %u is still active for %s,"
  192. " preforming session reinstatement.\n", (sessiontype) ?
  193. "Discovery" : "Normal", sess->sid,
  194. sess->sess_ops->InitiatorName);
  195. spin_lock_bh(&sess->conn_lock);
  196. if (sess->session_state == TARG_SESS_STATE_FAILED) {
  197. spin_unlock_bh(&sess->conn_lock);
  198. iscsit_dec_session_usage_count(sess);
  199. target_put_session(sess->se_sess);
  200. return 0;
  201. }
  202. spin_unlock_bh(&sess->conn_lock);
  203. iscsit_stop_session(sess, 1, 1);
  204. iscsit_dec_session_usage_count(sess);
  205. target_put_session(sess->se_sess);
  206. return 0;
  207. }
  208. static void iscsi_login_set_conn_values(
  209. struct iscsi_session *sess,
  210. struct iscsi_conn *conn,
  211. __be16 cid)
  212. {
  213. conn->sess = sess;
  214. conn->cid = be16_to_cpu(cid);
  215. /*
  216. * Generate a random Status sequence number (statsn) for the new
  217. * iSCSI connection.
  218. */
  219. get_random_bytes(&conn->stat_sn, sizeof(u32));
  220. mutex_lock(&auth_id_lock);
  221. conn->auth_id = iscsit_global->auth_id++;
  222. mutex_unlock(&auth_id_lock);
  223. }
  224. static __printf(2, 3) int iscsi_change_param_sprintf(
  225. struct iscsi_conn *conn,
  226. const char *fmt, ...)
  227. {
  228. va_list args;
  229. unsigned char buf[64];
  230. memset(buf, 0, sizeof buf);
  231. va_start(args, fmt);
  232. vsnprintf(buf, sizeof buf, fmt, args);
  233. va_end(args);
  234. if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
  235. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  236. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. /*
  242. * This is the leading connection of a new session,
  243. * or session reinstatement.
  244. */
  245. static int iscsi_login_zero_tsih_s1(
  246. struct iscsi_conn *conn,
  247. unsigned char *buf)
  248. {
  249. struct iscsi_session *sess = NULL;
  250. struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
  251. int ret;
  252. sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
  253. if (!sess) {
  254. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  255. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  256. pr_err("Could not allocate memory for session\n");
  257. return -ENOMEM;
  258. }
  259. iscsi_login_set_conn_values(sess, conn, pdu->cid);
  260. sess->init_task_tag = pdu->itt;
  261. memcpy(&sess->isid, pdu->isid, 6);
  262. sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
  263. INIT_LIST_HEAD(&sess->sess_conn_list);
  264. INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
  265. INIT_LIST_HEAD(&sess->cr_active_list);
  266. INIT_LIST_HEAD(&sess->cr_inactive_list);
  267. init_completion(&sess->async_msg_comp);
  268. init_completion(&sess->reinstatement_comp);
  269. init_completion(&sess->session_wait_comp);
  270. init_completion(&sess->session_waiting_on_uc_comp);
  271. mutex_init(&sess->cmdsn_mutex);
  272. spin_lock_init(&sess->conn_lock);
  273. spin_lock_init(&sess->cr_a_lock);
  274. spin_lock_init(&sess->cr_i_lock);
  275. spin_lock_init(&sess->session_usage_lock);
  276. spin_lock_init(&sess->ttt_lock);
  277. idr_preload(GFP_KERNEL);
  278. spin_lock_bh(&sess_idr_lock);
  279. ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
  280. if (ret >= 0)
  281. sess->session_index = ret;
  282. spin_unlock_bh(&sess_idr_lock);
  283. idr_preload_end();
  284. if (ret < 0) {
  285. pr_err("idr_alloc() for sess_idr failed\n");
  286. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  287. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  288. goto free_sess;
  289. }
  290. sess->creation_time = get_jiffies_64();
  291. /*
  292. * The FFP CmdSN window values will be allocated from the TPG's
  293. * Initiator Node's ACL once the login has been successfully completed.
  294. */
  295. atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
  296. sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
  297. if (!sess->sess_ops) {
  298. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  299. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  300. pr_err("Unable to allocate memory for"
  301. " struct iscsi_sess_ops.\n");
  302. goto remove_idr;
  303. }
  304. sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
  305. if (IS_ERR(sess->se_sess)) {
  306. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  307. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  308. goto free_ops;
  309. }
  310. return 0;
  311. free_ops:
  312. kfree(sess->sess_ops);
  313. remove_idr:
  314. spin_lock_bh(&sess_idr_lock);
  315. idr_remove(&sess_idr, sess->session_index);
  316. spin_unlock_bh(&sess_idr_lock);
  317. free_sess:
  318. kfree(sess);
  319. conn->sess = NULL;
  320. return -ENOMEM;
  321. }
  322. static int iscsi_login_zero_tsih_s2(
  323. struct iscsi_conn *conn)
  324. {
  325. struct iscsi_node_attrib *na;
  326. struct iscsi_session *sess = conn->sess;
  327. bool iser = false;
  328. sess->tpg = conn->tpg;
  329. /*
  330. * Assign a new TPG Session Handle. Note this is protected with
  331. * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
  332. */
  333. sess->tsih = ++sess->tpg->ntsih;
  334. if (!sess->tsih)
  335. sess->tsih = ++sess->tpg->ntsih;
  336. /*
  337. * Create the default params from user defined values..
  338. */
  339. if (iscsi_copy_param_list(&conn->param_list,
  340. conn->tpg->param_list, 1) < 0) {
  341. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  342. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  343. return -1;
  344. }
  345. if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
  346. iser = true;
  347. iscsi_set_keys_to_negotiate(conn->param_list, iser);
  348. if (sess->sess_ops->SessionType)
  349. return iscsi_set_keys_irrelevant_for_discovery(
  350. conn->param_list);
  351. na = iscsit_tpg_get_node_attrib(sess);
  352. /*
  353. * Need to send TargetPortalGroupTag back in first login response
  354. * on any iSCSI connection where the Initiator provides TargetName.
  355. * See 5.3.1. Login Phase Start
  356. *
  357. * In our case, we have already located the struct iscsi_tiqn at this point.
  358. */
  359. if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
  360. return -1;
  361. /*
  362. * Workaround for Initiators that have broken connection recovery logic.
  363. *
  364. * "We would really like to get rid of this." Linux-iSCSI.org team
  365. */
  366. if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
  367. return -1;
  368. /*
  369. * Set RDMAExtensions=Yes by default for iSER enabled network portals
  370. */
  371. if (iser) {
  372. struct iscsi_param *param;
  373. unsigned long mrdsl, off;
  374. int rc;
  375. if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
  376. return -1;
  377. /*
  378. * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
  379. * Immediate Data + Unsolicitied Data-OUT if necessary..
  380. */
  381. param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
  382. conn->param_list);
  383. if (!param) {
  384. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  385. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  386. return -1;
  387. }
  388. rc = kstrtoul(param->value, 0, &mrdsl);
  389. if (rc < 0) {
  390. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  391. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  392. return -1;
  393. }
  394. off = mrdsl % PAGE_SIZE;
  395. if (!off)
  396. goto check_prot;
  397. if (mrdsl < PAGE_SIZE)
  398. mrdsl = PAGE_SIZE;
  399. else
  400. mrdsl -= off;
  401. pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
  402. " to PAGE_SIZE\n", mrdsl);
  403. if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
  404. return -1;
  405. /*
  406. * ISER currently requires that ImmediateData + Unsolicited
  407. * Data be disabled when protection / signature MRs are enabled.
  408. */
  409. check_prot:
  410. if (sess->se_sess->sup_prot_ops &
  411. (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
  412. TARGET_PROT_DOUT_INSERT)) {
  413. if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
  414. return -1;
  415. if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
  416. return -1;
  417. pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
  418. " T10-PI enabled ISER session\n");
  419. }
  420. }
  421. return 0;
  422. }
  423. static int iscsi_login_non_zero_tsih_s1(
  424. struct iscsi_conn *conn,
  425. unsigned char *buf)
  426. {
  427. struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
  428. iscsi_login_set_conn_values(NULL, conn, pdu->cid);
  429. return 0;
  430. }
  431. /*
  432. * Add a new connection to an existing session.
  433. */
  434. static int iscsi_login_non_zero_tsih_s2(
  435. struct iscsi_conn *conn,
  436. unsigned char *buf)
  437. {
  438. struct iscsi_portal_group *tpg = conn->tpg;
  439. struct iscsi_session *sess = NULL, *sess_p = NULL;
  440. struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
  441. struct se_session *se_sess, *se_sess_tmp;
  442. struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
  443. bool iser = false;
  444. spin_lock_bh(&se_tpg->session_lock);
  445. list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
  446. sess_list) {
  447. sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
  448. if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
  449. atomic_read(&sess_p->session_logout) ||
  450. (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
  451. continue;
  452. if (!memcmp(sess_p->isid, pdu->isid, 6) &&
  453. (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
  454. iscsit_inc_session_usage_count(sess_p);
  455. iscsit_stop_time2retain_timer(sess_p);
  456. sess = sess_p;
  457. break;
  458. }
  459. }
  460. spin_unlock_bh(&se_tpg->session_lock);
  461. /*
  462. * If the Time2Retain handler has expired, the session is already gone.
  463. */
  464. if (!sess) {
  465. pr_err("Initiator attempting to add a connection to"
  466. " a non-existent session, rejecting iSCSI Login.\n");
  467. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  468. ISCSI_LOGIN_STATUS_NO_SESSION);
  469. return -1;
  470. }
  471. /*
  472. * Stop the Time2Retain timer if this is a failed session, we restart
  473. * the timer if the login is not successful.
  474. */
  475. spin_lock_bh(&sess->conn_lock);
  476. if (sess->session_state == TARG_SESS_STATE_FAILED)
  477. atomic_set(&sess->session_continuation, 1);
  478. spin_unlock_bh(&sess->conn_lock);
  479. iscsi_login_set_conn_values(sess, conn, pdu->cid);
  480. if (iscsi_copy_param_list(&conn->param_list,
  481. conn->tpg->param_list, 0) < 0) {
  482. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  483. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  484. return -1;
  485. }
  486. if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
  487. iser = true;
  488. iscsi_set_keys_to_negotiate(conn->param_list, iser);
  489. /*
  490. * Need to send TargetPortalGroupTag back in first login response
  491. * on any iSCSI connection where the Initiator provides TargetName.
  492. * See 5.3.1. Login Phase Start
  493. *
  494. * In our case, we have already located the struct iscsi_tiqn at this point.
  495. */
  496. if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
  497. return -1;
  498. return 0;
  499. }
  500. int iscsi_login_post_auth_non_zero_tsih(
  501. struct iscsi_conn *conn,
  502. u16 cid,
  503. u32 exp_statsn)
  504. {
  505. struct iscsi_conn *conn_ptr = NULL;
  506. struct iscsi_conn_recovery *cr = NULL;
  507. struct iscsi_session *sess = conn->sess;
  508. /*
  509. * By following item 5 in the login table, if we have found
  510. * an existing ISID and a valid/existing TSIH and an existing
  511. * CID we do connection reinstatement. Currently we dont not
  512. * support it so we send back an non-zero status class to the
  513. * initiator and release the new connection.
  514. */
  515. conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
  516. if (conn_ptr) {
  517. pr_err("Connection exists with CID %hu for %s,"
  518. " performing connection reinstatement.\n",
  519. conn_ptr->cid, sess->sess_ops->InitiatorName);
  520. iscsit_connection_reinstatement_rcfr(conn_ptr);
  521. iscsit_dec_conn_usage_count(conn_ptr);
  522. }
  523. /*
  524. * Check for any connection recovery entires containing CID.
  525. * We use the original ExpStatSN sent in the first login request
  526. * to acknowledge commands for the failed connection.
  527. *
  528. * Also note that an explict logout may have already been sent,
  529. * but the response may not be sent due to additional connection
  530. * loss.
  531. */
  532. if (sess->sess_ops->ErrorRecoveryLevel == 2) {
  533. cr = iscsit_get_inactive_connection_recovery_entry(
  534. sess, cid);
  535. if (cr) {
  536. pr_debug("Performing implicit logout"
  537. " for connection recovery on CID: %hu\n",
  538. conn->cid);
  539. iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
  540. }
  541. }
  542. /*
  543. * Else we follow item 4 from the login table in that we have
  544. * found an existing ISID and a valid/existing TSIH and a new
  545. * CID we go ahead and continue to add a new connection to the
  546. * session.
  547. */
  548. pr_debug("Adding CID %hu to existing session for %s.\n",
  549. cid, sess->sess_ops->InitiatorName);
  550. if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
  551. pr_err("Adding additional connection to this session"
  552. " would exceed MaxConnections %d, login failed.\n",
  553. sess->sess_ops->MaxConnections);
  554. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  555. ISCSI_LOGIN_STATUS_ISID_ERROR);
  556. return -1;
  557. }
  558. return 0;
  559. }
  560. static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
  561. {
  562. struct iscsi_session *sess = conn->sess;
  563. /*
  564. * FIXME: Unsolicitied NopIN support for ISER
  565. */
  566. if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
  567. return;
  568. if (!sess->sess_ops->SessionType)
  569. iscsit_start_nopin_timer(conn);
  570. }
  571. int iscsit_start_kthreads(struct iscsi_conn *conn)
  572. {
  573. int ret = 0;
  574. spin_lock(&iscsit_global->ts_bitmap_lock);
  575. conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
  576. ISCSIT_BITMAP_BITS, get_order(1));
  577. spin_unlock(&iscsit_global->ts_bitmap_lock);
  578. if (conn->bitmap_id < 0) {
  579. pr_err("bitmap_find_free_region() failed for"
  580. " iscsit_start_kthreads()\n");
  581. return -ENOMEM;
  582. }
  583. conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
  584. "%s", ISCSI_TX_THREAD_NAME);
  585. if (IS_ERR(conn->tx_thread)) {
  586. pr_err("Unable to start iscsi_target_tx_thread\n");
  587. ret = PTR_ERR(conn->tx_thread);
  588. goto out_bitmap;
  589. }
  590. conn->tx_thread_active = true;
  591. conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
  592. "%s", ISCSI_RX_THREAD_NAME);
  593. if (IS_ERR(conn->rx_thread)) {
  594. pr_err("Unable to start iscsi_target_rx_thread\n");
  595. ret = PTR_ERR(conn->rx_thread);
  596. goto out_tx;
  597. }
  598. conn->rx_thread_active = true;
  599. return 0;
  600. out_tx:
  601. send_sig(SIGINT, conn->tx_thread, 1);
  602. kthread_stop(conn->tx_thread);
  603. conn->tx_thread_active = false;
  604. out_bitmap:
  605. spin_lock(&iscsit_global->ts_bitmap_lock);
  606. bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
  607. get_order(1));
  608. spin_unlock(&iscsit_global->ts_bitmap_lock);
  609. return ret;
  610. }
  611. void iscsi_post_login_handler(
  612. struct iscsi_np *np,
  613. struct iscsi_conn *conn,
  614. u8 zero_tsih)
  615. {
  616. int stop_timer = 0;
  617. struct iscsi_session *sess = conn->sess;
  618. struct se_session *se_sess = sess->se_sess;
  619. struct iscsi_portal_group *tpg = sess->tpg;
  620. struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
  621. iscsit_inc_conn_usage_count(conn);
  622. iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
  623. ISCSI_LOGIN_STATUS_ACCEPT);
  624. pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
  625. conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
  626. iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
  627. /*
  628. * SCSI Initiator -> SCSI Target Port Mapping
  629. */
  630. if (!zero_tsih) {
  631. iscsi_set_session_parameters(sess->sess_ops,
  632. conn->param_list, 0);
  633. iscsi_release_param_list(conn->param_list);
  634. conn->param_list = NULL;
  635. spin_lock_bh(&sess->conn_lock);
  636. atomic_set(&sess->session_continuation, 0);
  637. if (sess->session_state == TARG_SESS_STATE_FAILED) {
  638. pr_debug("Moving to"
  639. " TARG_SESS_STATE_LOGGED_IN.\n");
  640. sess->session_state = TARG_SESS_STATE_LOGGED_IN;
  641. stop_timer = 1;
  642. }
  643. pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
  644. " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
  645. &conn->local_sockaddr, tpg->tpgt);
  646. list_add_tail(&conn->conn_list, &sess->sess_conn_list);
  647. atomic_inc(&sess->nconn);
  648. pr_debug("Incremented iSCSI Connection count to %hu"
  649. " from node: %s\n", atomic_read(&sess->nconn),
  650. sess->sess_ops->InitiatorName);
  651. spin_unlock_bh(&sess->conn_lock);
  652. iscsi_post_login_start_timers(conn);
  653. /*
  654. * Determine CPU mask to ensure connection's RX and TX kthreads
  655. * are scheduled on the same CPU.
  656. */
  657. iscsit_thread_get_cpumask(conn);
  658. conn->conn_rx_reset_cpumask = 1;
  659. conn->conn_tx_reset_cpumask = 1;
  660. /*
  661. * Wakeup the sleeping iscsi_target_rx_thread() now that
  662. * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
  663. */
  664. complete(&conn->rx_login_comp);
  665. iscsit_dec_conn_usage_count(conn);
  666. if (stop_timer) {
  667. spin_lock_bh(&se_tpg->session_lock);
  668. iscsit_stop_time2retain_timer(sess);
  669. spin_unlock_bh(&se_tpg->session_lock);
  670. }
  671. iscsit_dec_session_usage_count(sess);
  672. return;
  673. }
  674. iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
  675. iscsi_release_param_list(conn->param_list);
  676. conn->param_list = NULL;
  677. iscsit_determine_maxcmdsn(sess);
  678. spin_lock_bh(&se_tpg->session_lock);
  679. __transport_register_session(&sess->tpg->tpg_se_tpg,
  680. se_sess->se_node_acl, se_sess, sess);
  681. pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
  682. sess->session_state = TARG_SESS_STATE_LOGGED_IN;
  683. pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
  684. conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
  685. tpg->tpgt);
  686. spin_lock_bh(&sess->conn_lock);
  687. list_add_tail(&conn->conn_list, &sess->sess_conn_list);
  688. atomic_inc(&sess->nconn);
  689. pr_debug("Incremented iSCSI Connection count to %hu from node:"
  690. " %s\n", atomic_read(&sess->nconn),
  691. sess->sess_ops->InitiatorName);
  692. spin_unlock_bh(&sess->conn_lock);
  693. sess->sid = tpg->sid++;
  694. if (!sess->sid)
  695. sess->sid = tpg->sid++;
  696. pr_debug("Established iSCSI session from node: %s\n",
  697. sess->sess_ops->InitiatorName);
  698. tpg->nsessions++;
  699. if (tpg->tpg_tiqn)
  700. tpg->tpg_tiqn->tiqn_nsessions++;
  701. pr_debug("Incremented number of active iSCSI sessions to %u on"
  702. " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
  703. spin_unlock_bh(&se_tpg->session_lock);
  704. iscsi_post_login_start_timers(conn);
  705. /*
  706. * Determine CPU mask to ensure connection's RX and TX kthreads
  707. * are scheduled on the same CPU.
  708. */
  709. iscsit_thread_get_cpumask(conn);
  710. conn->conn_rx_reset_cpumask = 1;
  711. conn->conn_tx_reset_cpumask = 1;
  712. /*
  713. * Wakeup the sleeping iscsi_target_rx_thread() now that
  714. * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
  715. */
  716. complete(&conn->rx_login_comp);
  717. iscsit_dec_conn_usage_count(conn);
  718. }
  719. static void iscsi_handle_login_thread_timeout(unsigned long data)
  720. {
  721. struct iscsi_np *np = (struct iscsi_np *) data;
  722. spin_lock_bh(&np->np_thread_lock);
  723. pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
  724. &np->np_sockaddr);
  725. if (np->np_login_timer_flags & ISCSI_TF_STOP) {
  726. spin_unlock_bh(&np->np_thread_lock);
  727. return;
  728. }
  729. if (np->np_thread)
  730. send_sig(SIGINT, np->np_thread, 1);
  731. np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
  732. spin_unlock_bh(&np->np_thread_lock);
  733. }
  734. static void iscsi_start_login_thread_timer(struct iscsi_np *np)
  735. {
  736. /*
  737. * This used the TA_LOGIN_TIMEOUT constant because at this
  738. * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
  739. */
  740. spin_lock_bh(&np->np_thread_lock);
  741. init_timer(&np->np_login_timer);
  742. np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
  743. np->np_login_timer.data = (unsigned long)np;
  744. np->np_login_timer.function = iscsi_handle_login_thread_timeout;
  745. np->np_login_timer_flags &= ~ISCSI_TF_STOP;
  746. np->np_login_timer_flags |= ISCSI_TF_RUNNING;
  747. add_timer(&np->np_login_timer);
  748. pr_debug("Added timeout timer to iSCSI login request for"
  749. " %u seconds.\n", TA_LOGIN_TIMEOUT);
  750. spin_unlock_bh(&np->np_thread_lock);
  751. }
  752. static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
  753. {
  754. spin_lock_bh(&np->np_thread_lock);
  755. if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
  756. spin_unlock_bh(&np->np_thread_lock);
  757. return;
  758. }
  759. np->np_login_timer_flags |= ISCSI_TF_STOP;
  760. spin_unlock_bh(&np->np_thread_lock);
  761. del_timer_sync(&np->np_login_timer);
  762. spin_lock_bh(&np->np_thread_lock);
  763. np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
  764. spin_unlock_bh(&np->np_thread_lock);
  765. }
  766. int iscsit_setup_np(
  767. struct iscsi_np *np,
  768. struct sockaddr_storage *sockaddr)
  769. {
  770. struct socket *sock = NULL;
  771. int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
  772. switch (np->np_network_transport) {
  773. case ISCSI_TCP:
  774. np->np_ip_proto = IPPROTO_TCP;
  775. np->np_sock_type = SOCK_STREAM;
  776. break;
  777. case ISCSI_SCTP_TCP:
  778. np->np_ip_proto = IPPROTO_SCTP;
  779. np->np_sock_type = SOCK_STREAM;
  780. break;
  781. case ISCSI_SCTP_UDP:
  782. np->np_ip_proto = IPPROTO_SCTP;
  783. np->np_sock_type = SOCK_SEQPACKET;
  784. break;
  785. default:
  786. pr_err("Unsupported network_transport: %d\n",
  787. np->np_network_transport);
  788. return -EINVAL;
  789. }
  790. np->np_ip_proto = IPPROTO_TCP;
  791. np->np_sock_type = SOCK_STREAM;
  792. ret = sock_create(sockaddr->ss_family, np->np_sock_type,
  793. np->np_ip_proto, &sock);
  794. if (ret < 0) {
  795. pr_err("sock_create() failed.\n");
  796. return ret;
  797. }
  798. np->np_socket = sock;
  799. /*
  800. * Setup the np->np_sockaddr from the passed sockaddr setup
  801. * in iscsi_target_configfs.c code..
  802. */
  803. memcpy(&np->np_sockaddr, sockaddr,
  804. sizeof(struct sockaddr_storage));
  805. if (sockaddr->ss_family == AF_INET6)
  806. len = sizeof(struct sockaddr_in6);
  807. else
  808. len = sizeof(struct sockaddr_in);
  809. /*
  810. * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
  811. */
  812. /* FIXME: Someone please explain why this is endian-safe */
  813. opt = 1;
  814. if (np->np_network_transport == ISCSI_TCP) {
  815. ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  816. (char *)&opt, sizeof(opt));
  817. if (ret < 0) {
  818. pr_err("kernel_setsockopt() for TCP_NODELAY"
  819. " failed: %d\n", ret);
  820. goto fail;
  821. }
  822. }
  823. /* FIXME: Someone please explain why this is endian-safe */
  824. ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  825. (char *)&opt, sizeof(opt));
  826. if (ret < 0) {
  827. pr_err("kernel_setsockopt() for SO_REUSEADDR"
  828. " failed\n");
  829. goto fail;
  830. }
  831. ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
  832. (char *)&opt, sizeof(opt));
  833. if (ret < 0) {
  834. pr_err("kernel_setsockopt() for IP_FREEBIND"
  835. " failed\n");
  836. goto fail;
  837. }
  838. ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
  839. if (ret < 0) {
  840. pr_err("kernel_bind() failed: %d\n", ret);
  841. goto fail;
  842. }
  843. ret = kernel_listen(sock, backlog);
  844. if (ret != 0) {
  845. pr_err("kernel_listen() failed: %d\n", ret);
  846. goto fail;
  847. }
  848. return 0;
  849. fail:
  850. np->np_socket = NULL;
  851. sock_release(sock);
  852. return ret;
  853. }
  854. int iscsi_target_setup_login_socket(
  855. struct iscsi_np *np,
  856. struct sockaddr_storage *sockaddr)
  857. {
  858. struct iscsit_transport *t;
  859. int rc;
  860. t = iscsit_get_transport(np->np_network_transport);
  861. if (!t)
  862. return -EINVAL;
  863. rc = t->iscsit_setup_np(np, sockaddr);
  864. if (rc < 0) {
  865. iscsit_put_transport(t);
  866. return rc;
  867. }
  868. np->np_transport = t;
  869. np->enabled = true;
  870. return 0;
  871. }
  872. int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
  873. {
  874. struct socket *new_sock, *sock = np->np_socket;
  875. struct sockaddr_in sock_in;
  876. struct sockaddr_in6 sock_in6;
  877. int rc, err;
  878. rc = kernel_accept(sock, &new_sock, 0);
  879. if (rc < 0)
  880. return rc;
  881. conn->sock = new_sock;
  882. conn->login_family = np->np_sockaddr.ss_family;
  883. if (np->np_sockaddr.ss_family == AF_INET6) {
  884. memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
  885. rc = conn->sock->ops->getname(conn->sock,
  886. (struct sockaddr *)&sock_in6, &err, 1);
  887. if (!rc) {
  888. if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
  889. memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
  890. } else {
  891. /* Pretend to be an ipv4 socket */
  892. sock_in.sin_family = AF_INET;
  893. sock_in.sin_port = sock_in6.sin6_port;
  894. memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
  895. memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
  896. }
  897. }
  898. rc = conn->sock->ops->getname(conn->sock,
  899. (struct sockaddr *)&sock_in6, &err, 0);
  900. if (!rc) {
  901. if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
  902. memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
  903. } else {
  904. /* Pretend to be an ipv4 socket */
  905. sock_in.sin_family = AF_INET;
  906. sock_in.sin_port = sock_in6.sin6_port;
  907. memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
  908. memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
  909. }
  910. }
  911. } else {
  912. memset(&sock_in, 0, sizeof(struct sockaddr_in));
  913. rc = conn->sock->ops->getname(conn->sock,
  914. (struct sockaddr *)&sock_in, &err, 1);
  915. if (!rc)
  916. memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
  917. rc = conn->sock->ops->getname(conn->sock,
  918. (struct sockaddr *)&sock_in, &err, 0);
  919. if (!rc)
  920. memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
  921. }
  922. return 0;
  923. }
  924. int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
  925. {
  926. struct iscsi_login_req *login_req;
  927. u32 padding = 0, payload_length;
  928. if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
  929. return -1;
  930. login_req = (struct iscsi_login_req *)login->req;
  931. payload_length = ntoh24(login_req->dlength);
  932. padding = ((-payload_length) & 3);
  933. pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
  934. " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
  935. login_req->flags, login_req->itt, login_req->cmdsn,
  936. login_req->exp_statsn, login_req->cid, payload_length);
  937. /*
  938. * Setup the initial iscsi_login values from the leading
  939. * login request PDU.
  940. */
  941. if (login->first_request) {
  942. login_req = (struct iscsi_login_req *)login->req;
  943. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  944. login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
  945. login->version_min = login_req->min_version;
  946. login->version_max = login_req->max_version;
  947. memcpy(login->isid, login_req->isid, 6);
  948. login->cmd_sn = be32_to_cpu(login_req->cmdsn);
  949. login->init_task_tag = login_req->itt;
  950. login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
  951. login->cid = be16_to_cpu(login_req->cid);
  952. login->tsih = be16_to_cpu(login_req->tsih);
  953. }
  954. if (iscsi_target_check_login_request(conn, login) < 0)
  955. return -1;
  956. memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
  957. if (iscsi_login_rx_data(conn, login->req_buf,
  958. payload_length + padding) < 0)
  959. return -1;
  960. return 0;
  961. }
  962. int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
  963. u32 length)
  964. {
  965. if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
  966. return -1;
  967. return 0;
  968. }
  969. static int
  970. iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
  971. {
  972. int rc;
  973. if (!t->owner) {
  974. conn->conn_transport = t;
  975. return 0;
  976. }
  977. rc = try_module_get(t->owner);
  978. if (!rc) {
  979. pr_err("try_module_get() failed for %s\n", t->name);
  980. return -EINVAL;
  981. }
  982. conn->conn_transport = t;
  983. return 0;
  984. }
  985. void iscsi_target_login_sess_out(struct iscsi_conn *conn,
  986. struct iscsi_np *np, bool zero_tsih, bool new_sess)
  987. {
  988. if (!new_sess)
  989. goto old_sess_out;
  990. pr_err("iSCSI Login negotiation failed.\n");
  991. iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  992. ISCSI_LOGIN_STATUS_INIT_ERR);
  993. if (!zero_tsih || !conn->sess)
  994. goto old_sess_out;
  995. transport_free_session(conn->sess->se_sess);
  996. spin_lock_bh(&sess_idr_lock);
  997. idr_remove(&sess_idr, conn->sess->session_index);
  998. spin_unlock_bh(&sess_idr_lock);
  999. kfree(conn->sess->sess_ops);
  1000. kfree(conn->sess);
  1001. conn->sess = NULL;
  1002. old_sess_out:
  1003. iscsi_stop_login_thread_timer(np);
  1004. /*
  1005. * If login negotiation fails check if the Time2Retain timer
  1006. * needs to be restarted.
  1007. */
  1008. if (!zero_tsih && conn->sess) {
  1009. spin_lock_bh(&conn->sess->conn_lock);
  1010. if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
  1011. struct se_portal_group *se_tpg =
  1012. &conn->tpg->tpg_se_tpg;
  1013. atomic_set(&conn->sess->session_continuation, 0);
  1014. spin_unlock_bh(&conn->sess->conn_lock);
  1015. spin_lock_bh(&se_tpg->session_lock);
  1016. iscsit_start_time2retain_handler(conn->sess);
  1017. spin_unlock_bh(&se_tpg->session_lock);
  1018. } else
  1019. spin_unlock_bh(&conn->sess->conn_lock);
  1020. iscsit_dec_session_usage_count(conn->sess);
  1021. }
  1022. if (!IS_ERR(conn->conn_rx_hash.tfm))
  1023. crypto_free_hash(conn->conn_rx_hash.tfm);
  1024. if (!IS_ERR(conn->conn_tx_hash.tfm))
  1025. crypto_free_hash(conn->conn_tx_hash.tfm);
  1026. free_cpumask_var(conn->conn_cpumask);
  1027. kfree(conn->conn_ops);
  1028. if (conn->param_list) {
  1029. iscsi_release_param_list(conn->param_list);
  1030. conn->param_list = NULL;
  1031. }
  1032. iscsi_target_nego_release(conn);
  1033. if (conn->sock) {
  1034. sock_release(conn->sock);
  1035. conn->sock = NULL;
  1036. }
  1037. if (conn->conn_transport->iscsit_wait_conn)
  1038. conn->conn_transport->iscsit_wait_conn(conn);
  1039. if (conn->conn_transport->iscsit_free_conn)
  1040. conn->conn_transport->iscsit_free_conn(conn);
  1041. iscsit_put_transport(conn->conn_transport);
  1042. kfree(conn);
  1043. }
  1044. static int __iscsi_target_login_thread(struct iscsi_np *np)
  1045. {
  1046. u8 *buffer, zero_tsih = 0;
  1047. int ret = 0, rc;
  1048. struct iscsi_conn *conn = NULL;
  1049. struct iscsi_login *login;
  1050. struct iscsi_portal_group *tpg = NULL;
  1051. struct iscsi_login_req *pdu;
  1052. struct iscsi_tpg_np *tpg_np;
  1053. bool new_sess = false;
  1054. flush_signals(current);
  1055. spin_lock_bh(&np->np_thread_lock);
  1056. if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
  1057. np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
  1058. spin_unlock_bh(&np->np_thread_lock);
  1059. complete(&np->np_restart_comp);
  1060. return 1;
  1061. } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
  1062. spin_unlock_bh(&np->np_thread_lock);
  1063. goto exit;
  1064. } else {
  1065. np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
  1066. }
  1067. spin_unlock_bh(&np->np_thread_lock);
  1068. conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
  1069. if (!conn) {
  1070. pr_err("Could not allocate memory for"
  1071. " new connection\n");
  1072. /* Get another socket */
  1073. return 1;
  1074. }
  1075. pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
  1076. conn->conn_state = TARG_CONN_STATE_FREE;
  1077. if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
  1078. kfree(conn);
  1079. return 1;
  1080. }
  1081. rc = np->np_transport->iscsit_accept_np(np, conn);
  1082. if (rc == -ENOSYS) {
  1083. complete(&np->np_restart_comp);
  1084. iscsit_put_transport(conn->conn_transport);
  1085. kfree(conn);
  1086. conn = NULL;
  1087. goto exit;
  1088. } else if (rc < 0) {
  1089. spin_lock_bh(&np->np_thread_lock);
  1090. if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
  1091. np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
  1092. spin_unlock_bh(&np->np_thread_lock);
  1093. complete(&np->np_restart_comp);
  1094. iscsit_put_transport(conn->conn_transport);
  1095. kfree(conn);
  1096. conn = NULL;
  1097. /* Get another socket */
  1098. return 1;
  1099. }
  1100. spin_unlock_bh(&np->np_thread_lock);
  1101. iscsit_put_transport(conn->conn_transport);
  1102. kfree(conn);
  1103. conn = NULL;
  1104. goto out;
  1105. }
  1106. /*
  1107. * Perform the remaining iSCSI connection initialization items..
  1108. */
  1109. login = iscsi_login_init_conn(conn);
  1110. if (!login) {
  1111. goto new_sess_out;
  1112. }
  1113. iscsi_start_login_thread_timer(np);
  1114. pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
  1115. conn->conn_state = TARG_CONN_STATE_XPT_UP;
  1116. /*
  1117. * This will process the first login request + payload..
  1118. */
  1119. rc = np->np_transport->iscsit_get_login_rx(conn, login);
  1120. if (rc == 1)
  1121. return 1;
  1122. else if (rc < 0)
  1123. goto new_sess_out;
  1124. buffer = &login->req[0];
  1125. pdu = (struct iscsi_login_req *)buffer;
  1126. /*
  1127. * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
  1128. * when Status-Class != 0.
  1129. */
  1130. conn->login_itt = pdu->itt;
  1131. spin_lock_bh(&np->np_thread_lock);
  1132. if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
  1133. spin_unlock_bh(&np->np_thread_lock);
  1134. pr_err("iSCSI Network Portal on %pISpc currently not"
  1135. " active.\n", &np->np_sockaddr);
  1136. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  1137. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  1138. goto new_sess_out;
  1139. }
  1140. spin_unlock_bh(&np->np_thread_lock);
  1141. conn->network_transport = np->np_network_transport;
  1142. pr_debug("Received iSCSI login request from %pISpc on %s Network"
  1143. " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
  1144. &conn->local_sockaddr);
  1145. pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
  1146. conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
  1147. if (iscsi_login_check_initiator_version(conn, pdu->max_version,
  1148. pdu->min_version) < 0)
  1149. goto new_sess_out;
  1150. zero_tsih = (pdu->tsih == 0x0000);
  1151. if (zero_tsih) {
  1152. /*
  1153. * This is the leading connection of a new session.
  1154. * We wait until after authentication to check for
  1155. * session reinstatement.
  1156. */
  1157. if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
  1158. goto new_sess_out;
  1159. } else {
  1160. /*
  1161. * Add a new connection to an existing session.
  1162. * We check for a non-existant session in
  1163. * iscsi_login_non_zero_tsih_s2() below based
  1164. * on ISID/TSIH, but wait until after authentication
  1165. * to check for connection reinstatement, etc.
  1166. */
  1167. if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
  1168. goto new_sess_out;
  1169. }
  1170. /*
  1171. * SessionType: Discovery
  1172. *
  1173. * Locates Default Portal
  1174. *
  1175. * SessionType: Normal
  1176. *
  1177. * Locates Target Portal from NP -> Target IQN
  1178. */
  1179. rc = iscsi_target_locate_portal(np, conn, login);
  1180. if (rc < 0) {
  1181. tpg = conn->tpg;
  1182. goto new_sess_out;
  1183. }
  1184. login->zero_tsih = zero_tsih;
  1185. if (conn->sess)
  1186. conn->sess->se_sess->sup_prot_ops =
  1187. conn->conn_transport->iscsit_get_sup_prot_ops(conn);
  1188. tpg = conn->tpg;
  1189. if (!tpg) {
  1190. pr_err("Unable to locate struct iscsi_conn->tpg\n");
  1191. goto new_sess_out;
  1192. }
  1193. if (zero_tsih) {
  1194. if (iscsi_login_zero_tsih_s2(conn) < 0)
  1195. goto new_sess_out;
  1196. } else {
  1197. if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
  1198. goto old_sess_out;
  1199. }
  1200. ret = iscsi_target_start_negotiation(login, conn);
  1201. if (ret < 0)
  1202. goto new_sess_out;
  1203. iscsi_stop_login_thread_timer(np);
  1204. if (ret == 1) {
  1205. tpg_np = conn->tpg_np;
  1206. iscsi_post_login_handler(np, conn, zero_tsih);
  1207. iscsit_deaccess_np(np, tpg, tpg_np);
  1208. }
  1209. tpg = NULL;
  1210. tpg_np = NULL;
  1211. /* Get another socket */
  1212. return 1;
  1213. new_sess_out:
  1214. new_sess = true;
  1215. old_sess_out:
  1216. tpg_np = conn->tpg_np;
  1217. iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
  1218. new_sess = false;
  1219. if (tpg) {
  1220. iscsit_deaccess_np(np, tpg, tpg_np);
  1221. tpg = NULL;
  1222. tpg_np = NULL;
  1223. }
  1224. out:
  1225. return 1;
  1226. exit:
  1227. iscsi_stop_login_thread_timer(np);
  1228. spin_lock_bh(&np->np_thread_lock);
  1229. np->np_thread_state = ISCSI_NP_THREAD_EXIT;
  1230. spin_unlock_bh(&np->np_thread_lock);
  1231. return 0;
  1232. }
  1233. int iscsi_target_login_thread(void *arg)
  1234. {
  1235. struct iscsi_np *np = arg;
  1236. int ret;
  1237. allow_signal(SIGINT);
  1238. while (1) {
  1239. ret = __iscsi_target_login_thread(np);
  1240. /*
  1241. * We break and exit here unless another sock_accept() call
  1242. * is expected.
  1243. */
  1244. if (ret != 1)
  1245. break;
  1246. }
  1247. while (!kthread_should_stop()) {
  1248. msleep(100);
  1249. }
  1250. return 0;
  1251. }