iscsi_target_nego.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*******************************************************************************
  2. * This file contains main functions related to iSCSI Parameter negotiation.
  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/ctype.h>
  19. #include <linux/kthread.h>
  20. #include <linux/slab.h>
  21. #include <net/sock.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_transport.h>
  26. #include <target/iscsi/iscsi_target_core.h>
  27. #include "iscsi_target_parameters.h"
  28. #include "iscsi_target_login.h"
  29. #include "iscsi_target_nego.h"
  30. #include "iscsi_target_tpg.h"
  31. #include "iscsi_target_util.h"
  32. #include "iscsi_target.h"
  33. #include "iscsi_target_auth.h"
  34. #define MAX_LOGIN_PDUS 7
  35. #define TEXT_LEN 4096
  36. void convert_null_to_semi(char *buf, int len)
  37. {
  38. int i;
  39. for (i = 0; i < len; i++)
  40. if (buf[i] == '\0')
  41. buf[i] = ';';
  42. }
  43. static int strlen_semi(char *buf)
  44. {
  45. int i = 0;
  46. while (buf[i] != '\0') {
  47. if (buf[i] == ';')
  48. return i;
  49. i++;
  50. }
  51. return -1;
  52. }
  53. int extract_param(
  54. const char *in_buf,
  55. const char *pattern,
  56. unsigned int max_length,
  57. char *out_buf,
  58. unsigned char *type)
  59. {
  60. char *ptr;
  61. int len;
  62. if (!in_buf || !pattern || !out_buf || !type)
  63. return -1;
  64. ptr = strstr(in_buf, pattern);
  65. if (!ptr)
  66. return -1;
  67. ptr = strstr(ptr, "=");
  68. if (!ptr)
  69. return -1;
  70. ptr += 1;
  71. if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
  72. ptr += 2; /* skip 0x */
  73. *type = HEX;
  74. } else
  75. *type = DECIMAL;
  76. len = strlen_semi(ptr);
  77. if (len < 0)
  78. return -1;
  79. if (len >= max_length) {
  80. pr_err("Length of input: %d exceeds max_length:"
  81. " %d\n", len, max_length);
  82. return -1;
  83. }
  84. memcpy(out_buf, ptr, len);
  85. out_buf[len] = '\0';
  86. return 0;
  87. }
  88. static u32 iscsi_handle_authentication(
  89. struct iscsi_conn *conn,
  90. char *in_buf,
  91. char *out_buf,
  92. int in_length,
  93. int *out_length,
  94. unsigned char *authtype)
  95. {
  96. struct iscsi_session *sess = conn->sess;
  97. struct iscsi_node_auth *auth;
  98. struct iscsi_node_acl *iscsi_nacl;
  99. struct iscsi_portal_group *iscsi_tpg;
  100. struct se_node_acl *se_nacl;
  101. if (!sess->sess_ops->SessionType) {
  102. /*
  103. * For SessionType=Normal
  104. */
  105. se_nacl = conn->sess->se_sess->se_node_acl;
  106. if (!se_nacl) {
  107. pr_err("Unable to locate struct se_node_acl for"
  108. " CHAP auth\n");
  109. return -1;
  110. }
  111. iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
  112. se_node_acl);
  113. if (!iscsi_nacl) {
  114. pr_err("Unable to locate struct iscsi_node_acl for"
  115. " CHAP auth\n");
  116. return -1;
  117. }
  118. if (se_nacl->dynamic_node_acl) {
  119. iscsi_tpg = container_of(se_nacl->se_tpg,
  120. struct iscsi_portal_group, tpg_se_tpg);
  121. auth = &iscsi_tpg->tpg_demo_auth;
  122. } else {
  123. iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
  124. se_node_acl);
  125. auth = &iscsi_nacl->node_auth;
  126. }
  127. } else {
  128. /*
  129. * For SessionType=Discovery
  130. */
  131. auth = &iscsit_global->discovery_acl.node_auth;
  132. }
  133. if (strstr("CHAP", authtype))
  134. strcpy(conn->sess->auth_type, "CHAP");
  135. else
  136. strcpy(conn->sess->auth_type, NONE);
  137. if (strstr("None", authtype))
  138. return 1;
  139. #ifdef CANSRP
  140. else if (strstr("SRP", authtype))
  141. return srp_main_loop(conn, auth, in_buf, out_buf,
  142. &in_length, out_length);
  143. #endif
  144. else if (strstr("CHAP", authtype))
  145. return chap_main_loop(conn, auth, in_buf, out_buf,
  146. &in_length, out_length);
  147. else if (strstr("SPKM1", authtype))
  148. return 2;
  149. else if (strstr("SPKM2", authtype))
  150. return 2;
  151. else if (strstr("KRB5", authtype))
  152. return 2;
  153. else
  154. return 2;
  155. }
  156. static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
  157. {
  158. kfree(conn->auth_protocol);
  159. }
  160. int iscsi_target_check_login_request(
  161. struct iscsi_conn *conn,
  162. struct iscsi_login *login)
  163. {
  164. int req_csg, req_nsg;
  165. u32 payload_length;
  166. struct iscsi_login_req *login_req;
  167. login_req = (struct iscsi_login_req *) login->req;
  168. payload_length = ntoh24(login_req->dlength);
  169. switch (login_req->opcode & ISCSI_OPCODE_MASK) {
  170. case ISCSI_OP_LOGIN:
  171. break;
  172. default:
  173. pr_err("Received unknown opcode 0x%02x.\n",
  174. login_req->opcode & ISCSI_OPCODE_MASK);
  175. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  176. ISCSI_LOGIN_STATUS_INIT_ERR);
  177. return -1;
  178. }
  179. if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
  180. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  181. pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
  182. " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
  183. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  184. ISCSI_LOGIN_STATUS_INIT_ERR);
  185. return -1;
  186. }
  187. req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
  188. req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
  189. if (req_csg != login->current_stage) {
  190. pr_err("Initiator unexpectedly changed login stage"
  191. " from %d to %d, login failed.\n", login->current_stage,
  192. req_csg);
  193. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  194. ISCSI_LOGIN_STATUS_INIT_ERR);
  195. return -1;
  196. }
  197. if ((req_nsg == 2) || (req_csg >= 2) ||
  198. ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
  199. (req_nsg <= req_csg))) {
  200. pr_err("Illegal login_req->flags Combination, CSG: %d,"
  201. " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
  202. req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
  203. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  204. ISCSI_LOGIN_STATUS_INIT_ERR);
  205. return -1;
  206. }
  207. if ((login_req->max_version != login->version_max) ||
  208. (login_req->min_version != login->version_min)) {
  209. pr_err("Login request changed Version Max/Nin"
  210. " unexpectedly to 0x%02x/0x%02x, protocol error\n",
  211. login_req->max_version, login_req->min_version);
  212. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  213. ISCSI_LOGIN_STATUS_INIT_ERR);
  214. return -1;
  215. }
  216. if (memcmp(login_req->isid, login->isid, 6) != 0) {
  217. pr_err("Login request changed ISID unexpectedly,"
  218. " protocol error.\n");
  219. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  220. ISCSI_LOGIN_STATUS_INIT_ERR);
  221. return -1;
  222. }
  223. if (login_req->itt != login->init_task_tag) {
  224. pr_err("Login request changed ITT unexpectedly to"
  225. " 0x%08x, protocol error.\n", login_req->itt);
  226. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  227. ISCSI_LOGIN_STATUS_INIT_ERR);
  228. return -1;
  229. }
  230. if (payload_length > MAX_KEY_VALUE_PAIRS) {
  231. pr_err("Login request payload exceeds default"
  232. " MaxRecvDataSegmentLength: %u, protocol error.\n",
  233. MAX_KEY_VALUE_PAIRS);
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(iscsi_target_check_login_request);
  239. static int iscsi_target_check_first_request(
  240. struct iscsi_conn *conn,
  241. struct iscsi_login *login)
  242. {
  243. struct iscsi_param *param = NULL;
  244. struct se_node_acl *se_nacl;
  245. login->first_request = 0;
  246. list_for_each_entry(param, &conn->param_list->param_list, p_list) {
  247. if (!strncmp(param->name, SESSIONTYPE, 11)) {
  248. if (!IS_PSTATE_ACCEPTOR(param)) {
  249. pr_err("SessionType key not received"
  250. " in first login request.\n");
  251. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  252. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  253. return -1;
  254. }
  255. if (!strncmp(param->value, DISCOVERY, 9))
  256. return 0;
  257. }
  258. if (!strncmp(param->name, INITIATORNAME, 13)) {
  259. if (!IS_PSTATE_ACCEPTOR(param)) {
  260. if (!login->leading_connection)
  261. continue;
  262. pr_err("InitiatorName key not received"
  263. " in first login request.\n");
  264. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  265. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  266. return -1;
  267. }
  268. /*
  269. * For non-leading connections, double check that the
  270. * received InitiatorName matches the existing session's
  271. * struct iscsi_node_acl.
  272. */
  273. if (!login->leading_connection) {
  274. se_nacl = conn->sess->se_sess->se_node_acl;
  275. if (!se_nacl) {
  276. pr_err("Unable to locate"
  277. " struct se_node_acl\n");
  278. iscsit_tx_login_rsp(conn,
  279. ISCSI_STATUS_CLS_INITIATOR_ERR,
  280. ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
  281. return -1;
  282. }
  283. if (strcmp(param->value,
  284. se_nacl->initiatorname)) {
  285. pr_err("Incorrect"
  286. " InitiatorName: %s for this"
  287. " iSCSI Initiator Node.\n",
  288. param->value);
  289. iscsit_tx_login_rsp(conn,
  290. ISCSI_STATUS_CLS_INITIATOR_ERR,
  291. ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
  292. return -1;
  293. }
  294. }
  295. }
  296. }
  297. return 0;
  298. }
  299. static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
  300. {
  301. u32 padding = 0;
  302. struct iscsi_login_rsp *login_rsp;
  303. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  304. login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
  305. hton24(login_rsp->dlength, login->rsp_length);
  306. memcpy(login_rsp->isid, login->isid, 6);
  307. login_rsp->tsih = cpu_to_be16(login->tsih);
  308. login_rsp->itt = login->init_task_tag;
  309. login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
  310. login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
  311. login_rsp->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
  312. pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
  313. " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
  314. " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
  315. ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
  316. ntohl(login_rsp->statsn), login->rsp_length);
  317. padding = ((-login->rsp_length) & 3);
  318. /*
  319. * Before sending the last login response containing the transition
  320. * bit for full-feature-phase, go ahead and start up TX/RX threads
  321. * now to avoid potential resource allocation failures after the
  322. * final login response has been sent.
  323. */
  324. if (login->login_complete) {
  325. int rc = iscsit_start_kthreads(conn);
  326. if (rc) {
  327. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  328. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  329. return -1;
  330. }
  331. }
  332. if (conn->conn_transport->iscsit_put_login_tx(conn, login,
  333. login->rsp_length + padding) < 0)
  334. goto err;
  335. login->rsp_length = 0;
  336. return 0;
  337. err:
  338. if (login->login_complete) {
  339. if (conn->rx_thread && conn->rx_thread_active) {
  340. send_sig(SIGINT, conn->rx_thread, 1);
  341. complete(&conn->rx_login_comp);
  342. kthread_stop(conn->rx_thread);
  343. }
  344. if (conn->tx_thread && conn->tx_thread_active) {
  345. send_sig(SIGINT, conn->tx_thread, 1);
  346. kthread_stop(conn->tx_thread);
  347. }
  348. spin_lock(&iscsit_global->ts_bitmap_lock);
  349. bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
  350. get_order(1));
  351. spin_unlock(&iscsit_global->ts_bitmap_lock);
  352. }
  353. return -1;
  354. }
  355. static void iscsi_target_sk_data_ready(struct sock *sk)
  356. {
  357. struct iscsi_conn *conn = sk->sk_user_data;
  358. bool rc;
  359. pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
  360. write_lock_bh(&sk->sk_callback_lock);
  361. if (!sk->sk_user_data) {
  362. write_unlock_bh(&sk->sk_callback_lock);
  363. return;
  364. }
  365. if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
  366. write_unlock_bh(&sk->sk_callback_lock);
  367. pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
  368. return;
  369. }
  370. if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
  371. write_unlock_bh(&sk->sk_callback_lock);
  372. pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
  373. return;
  374. }
  375. if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
  376. write_unlock_bh(&sk->sk_callback_lock);
  377. pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
  378. return;
  379. }
  380. rc = schedule_delayed_work(&conn->login_work, 0);
  381. if (!rc) {
  382. pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
  383. " got false\n");
  384. }
  385. write_unlock_bh(&sk->sk_callback_lock);
  386. }
  387. static void iscsi_target_sk_state_change(struct sock *);
  388. static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
  389. {
  390. struct sock *sk;
  391. if (!conn->sock)
  392. return;
  393. sk = conn->sock->sk;
  394. pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
  395. write_lock_bh(&sk->sk_callback_lock);
  396. sk->sk_user_data = conn;
  397. conn->orig_data_ready = sk->sk_data_ready;
  398. conn->orig_state_change = sk->sk_state_change;
  399. sk->sk_data_ready = iscsi_target_sk_data_ready;
  400. sk->sk_state_change = iscsi_target_sk_state_change;
  401. write_unlock_bh(&sk->sk_callback_lock);
  402. sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
  403. sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
  404. }
  405. static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
  406. {
  407. struct sock *sk;
  408. if (!conn->sock)
  409. return;
  410. sk = conn->sock->sk;
  411. pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
  412. write_lock_bh(&sk->sk_callback_lock);
  413. if (!sk->sk_user_data) {
  414. write_unlock_bh(&sk->sk_callback_lock);
  415. return;
  416. }
  417. sk->sk_user_data = NULL;
  418. sk->sk_data_ready = conn->orig_data_ready;
  419. sk->sk_state_change = conn->orig_state_change;
  420. write_unlock_bh(&sk->sk_callback_lock);
  421. sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  422. sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  423. }
  424. static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
  425. static bool iscsi_target_sk_state_check(struct sock *sk)
  426. {
  427. if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
  428. pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
  429. "returning FALSE\n");
  430. return false;
  431. }
  432. return true;
  433. }
  434. static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
  435. {
  436. struct iscsi_np *np = login->np;
  437. bool zero_tsih = login->zero_tsih;
  438. iscsi_remove_failed_auth_entry(conn);
  439. iscsi_target_nego_release(conn);
  440. iscsi_target_login_sess_out(conn, np, zero_tsih, true);
  441. }
  442. static void iscsi_target_login_timeout(unsigned long data)
  443. {
  444. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  445. pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
  446. if (conn->login_kworker) {
  447. pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
  448. conn->login_kworker->comm, conn->login_kworker->pid);
  449. send_sig(SIGINT, conn->login_kworker, 1);
  450. }
  451. }
  452. static void iscsi_target_do_login_rx(struct work_struct *work)
  453. {
  454. struct iscsi_conn *conn = container_of(work,
  455. struct iscsi_conn, login_work.work);
  456. struct iscsi_login *login = conn->login;
  457. struct iscsi_np *np = login->np;
  458. struct iscsi_portal_group *tpg = conn->tpg;
  459. struct iscsi_tpg_np *tpg_np = conn->tpg_np;
  460. struct timer_list login_timer;
  461. int rc, zero_tsih = login->zero_tsih;
  462. bool state;
  463. pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
  464. conn, current->comm, current->pid);
  465. spin_lock(&tpg->tpg_state_lock);
  466. state = (tpg->tpg_state == TPG_STATE_ACTIVE);
  467. spin_unlock(&tpg->tpg_state_lock);
  468. if (!state) {
  469. pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
  470. iscsi_target_restore_sock_callbacks(conn);
  471. iscsi_target_login_drop(conn, login);
  472. iscsit_deaccess_np(np, tpg, tpg_np);
  473. return;
  474. }
  475. if (conn->sock) {
  476. struct sock *sk = conn->sock->sk;
  477. read_lock_bh(&sk->sk_callback_lock);
  478. state = iscsi_target_sk_state_check(sk);
  479. read_unlock_bh(&sk->sk_callback_lock);
  480. if (!state) {
  481. pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
  482. iscsi_target_restore_sock_callbacks(conn);
  483. iscsi_target_login_drop(conn, login);
  484. iscsit_deaccess_np(np, tpg, tpg_np);
  485. return;
  486. }
  487. }
  488. conn->login_kworker = current;
  489. allow_signal(SIGINT);
  490. init_timer(&login_timer);
  491. login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
  492. login_timer.data = (unsigned long)conn;
  493. login_timer.function = iscsi_target_login_timeout;
  494. add_timer(&login_timer);
  495. pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
  496. rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
  497. del_timer_sync(&login_timer);
  498. flush_signals(current);
  499. conn->login_kworker = NULL;
  500. if (rc < 0) {
  501. iscsi_target_restore_sock_callbacks(conn);
  502. iscsi_target_login_drop(conn, login);
  503. iscsit_deaccess_np(np, tpg, tpg_np);
  504. return;
  505. }
  506. pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
  507. conn, current->comm, current->pid);
  508. rc = iscsi_target_do_login(conn, login);
  509. if (rc < 0) {
  510. iscsi_target_restore_sock_callbacks(conn);
  511. iscsi_target_login_drop(conn, login);
  512. iscsit_deaccess_np(np, tpg, tpg_np);
  513. } else if (!rc) {
  514. if (conn->sock) {
  515. struct sock *sk = conn->sock->sk;
  516. write_lock_bh(&sk->sk_callback_lock);
  517. clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
  518. write_unlock_bh(&sk->sk_callback_lock);
  519. }
  520. } else if (rc == 1) {
  521. iscsi_target_nego_release(conn);
  522. iscsi_post_login_handler(np, conn, zero_tsih);
  523. iscsit_deaccess_np(np, tpg, tpg_np);
  524. }
  525. }
  526. static void iscsi_target_do_cleanup(struct work_struct *work)
  527. {
  528. struct iscsi_conn *conn = container_of(work,
  529. struct iscsi_conn, login_cleanup_work.work);
  530. struct sock *sk = conn->sock->sk;
  531. struct iscsi_login *login = conn->login;
  532. struct iscsi_np *np = login->np;
  533. struct iscsi_portal_group *tpg = conn->tpg;
  534. struct iscsi_tpg_np *tpg_np = conn->tpg_np;
  535. pr_debug("Entering iscsi_target_do_cleanup\n");
  536. cancel_delayed_work_sync(&conn->login_work);
  537. conn->orig_state_change(sk);
  538. iscsi_target_restore_sock_callbacks(conn);
  539. iscsi_target_login_drop(conn, login);
  540. iscsit_deaccess_np(np, tpg, tpg_np);
  541. pr_debug("iscsi_target_do_cleanup done()\n");
  542. }
  543. static void iscsi_target_sk_state_change(struct sock *sk)
  544. {
  545. struct iscsi_conn *conn;
  546. void (*orig_state_change)(struct sock *);
  547. bool state;
  548. pr_debug("Entering iscsi_target_sk_state_change\n");
  549. write_lock_bh(&sk->sk_callback_lock);
  550. conn = sk->sk_user_data;
  551. if (!conn) {
  552. write_unlock_bh(&sk->sk_callback_lock);
  553. return;
  554. }
  555. orig_state_change = conn->orig_state_change;
  556. if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
  557. pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
  558. conn);
  559. write_unlock_bh(&sk->sk_callback_lock);
  560. orig_state_change(sk);
  561. return;
  562. }
  563. if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
  564. pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
  565. " conn: %p\n", conn);
  566. write_unlock_bh(&sk->sk_callback_lock);
  567. orig_state_change(sk);
  568. return;
  569. }
  570. if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
  571. pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
  572. conn);
  573. write_unlock_bh(&sk->sk_callback_lock);
  574. orig_state_change(sk);
  575. return;
  576. }
  577. state = iscsi_target_sk_state_check(sk);
  578. write_unlock_bh(&sk->sk_callback_lock);
  579. pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
  580. if (!state) {
  581. pr_debug("iscsi_target_sk_state_change got failed state\n");
  582. schedule_delayed_work(&conn->login_cleanup_work, 0);
  583. return;
  584. }
  585. orig_state_change(sk);
  586. }
  587. /*
  588. * NOTE: We check for existing sessions or connections AFTER the initiator
  589. * has been successfully authenticated in order to protect against faked
  590. * ISID/TSIH combinations.
  591. */
  592. static int iscsi_target_check_for_existing_instances(
  593. struct iscsi_conn *conn,
  594. struct iscsi_login *login)
  595. {
  596. if (login->checked_for_existing)
  597. return 0;
  598. login->checked_for_existing = 1;
  599. if (!login->tsih)
  600. return iscsi_check_for_session_reinstatement(conn);
  601. else
  602. return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
  603. login->initial_exp_statsn);
  604. }
  605. static int iscsi_target_do_authentication(
  606. struct iscsi_conn *conn,
  607. struct iscsi_login *login)
  608. {
  609. int authret;
  610. u32 payload_length;
  611. struct iscsi_param *param;
  612. struct iscsi_login_req *login_req;
  613. struct iscsi_login_rsp *login_rsp;
  614. login_req = (struct iscsi_login_req *) login->req;
  615. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  616. payload_length = ntoh24(login_req->dlength);
  617. param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
  618. if (!param)
  619. return -1;
  620. authret = iscsi_handle_authentication(
  621. conn,
  622. login->req_buf,
  623. login->rsp_buf,
  624. payload_length,
  625. &login->rsp_length,
  626. param->value);
  627. switch (authret) {
  628. case 0:
  629. pr_debug("Received OK response"
  630. " from LIO Authentication, continuing.\n");
  631. break;
  632. case 1:
  633. pr_debug("iSCSI security negotiation"
  634. " completed successfully.\n");
  635. login->auth_complete = 1;
  636. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
  637. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  638. login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
  639. ISCSI_FLAG_LOGIN_TRANSIT);
  640. login->current_stage = 1;
  641. }
  642. return iscsi_target_check_for_existing_instances(
  643. conn, login);
  644. case 2:
  645. pr_err("Security negotiation"
  646. " failed.\n");
  647. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  648. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  649. return -1;
  650. default:
  651. pr_err("Received unknown error %d from LIO"
  652. " Authentication\n", authret);
  653. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  654. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  655. return -1;
  656. }
  657. return 0;
  658. }
  659. static int iscsi_target_handle_csg_zero(
  660. struct iscsi_conn *conn,
  661. struct iscsi_login *login)
  662. {
  663. int ret;
  664. u32 payload_length;
  665. struct iscsi_param *param;
  666. struct iscsi_login_req *login_req;
  667. struct iscsi_login_rsp *login_rsp;
  668. login_req = (struct iscsi_login_req *) login->req;
  669. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  670. payload_length = ntoh24(login_req->dlength);
  671. param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
  672. if (!param)
  673. return -1;
  674. ret = iscsi_decode_text_input(
  675. PHASE_SECURITY|PHASE_DECLARATIVE,
  676. SENDER_INITIATOR|SENDER_RECEIVER,
  677. login->req_buf,
  678. payload_length,
  679. conn);
  680. if (ret < 0)
  681. return -1;
  682. if (ret > 0) {
  683. if (login->auth_complete) {
  684. pr_err("Initiator has already been"
  685. " successfully authenticated, but is still"
  686. " sending %s keys.\n", param->value);
  687. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  688. ISCSI_LOGIN_STATUS_INIT_ERR);
  689. return -1;
  690. }
  691. goto do_auth;
  692. } else if (!payload_length) {
  693. pr_err("Initiator sent zero length security payload,"
  694. " login failed\n");
  695. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  696. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  697. return -1;
  698. }
  699. if (login->first_request)
  700. if (iscsi_target_check_first_request(conn, login) < 0)
  701. return -1;
  702. ret = iscsi_encode_text_output(
  703. PHASE_SECURITY|PHASE_DECLARATIVE,
  704. SENDER_TARGET,
  705. login->rsp_buf,
  706. &login->rsp_length,
  707. conn->param_list);
  708. if (ret < 0)
  709. return -1;
  710. if (!iscsi_check_negotiated_keys(conn->param_list)) {
  711. if (conn->tpg->tpg_attrib.authentication &&
  712. !strncmp(param->value, NONE, 4)) {
  713. pr_err("Initiator sent AuthMethod=None but"
  714. " Target is enforcing iSCSI Authentication,"
  715. " login failed.\n");
  716. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  717. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  718. return -1;
  719. }
  720. if (conn->tpg->tpg_attrib.authentication &&
  721. !login->auth_complete)
  722. return 0;
  723. if (strncmp(param->value, NONE, 4) && !login->auth_complete)
  724. return 0;
  725. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
  726. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
  727. login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
  728. ISCSI_FLAG_LOGIN_TRANSIT;
  729. login->current_stage = 1;
  730. }
  731. }
  732. return 0;
  733. do_auth:
  734. return iscsi_target_do_authentication(conn, login);
  735. }
  736. static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
  737. {
  738. int ret;
  739. u32 payload_length;
  740. struct iscsi_login_req *login_req;
  741. struct iscsi_login_rsp *login_rsp;
  742. login_req = (struct iscsi_login_req *) login->req;
  743. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  744. payload_length = ntoh24(login_req->dlength);
  745. ret = iscsi_decode_text_input(
  746. PHASE_OPERATIONAL|PHASE_DECLARATIVE,
  747. SENDER_INITIATOR|SENDER_RECEIVER,
  748. login->req_buf,
  749. payload_length,
  750. conn);
  751. if (ret < 0) {
  752. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  753. ISCSI_LOGIN_STATUS_INIT_ERR);
  754. return -1;
  755. }
  756. if (login->first_request)
  757. if (iscsi_target_check_first_request(conn, login) < 0)
  758. return -1;
  759. if (iscsi_target_check_for_existing_instances(conn, login) < 0)
  760. return -1;
  761. ret = iscsi_encode_text_output(
  762. PHASE_OPERATIONAL|PHASE_DECLARATIVE,
  763. SENDER_TARGET,
  764. login->rsp_buf,
  765. &login->rsp_length,
  766. conn->param_list);
  767. if (ret < 0) {
  768. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  769. ISCSI_LOGIN_STATUS_INIT_ERR);
  770. return -1;
  771. }
  772. if (!login->auth_complete &&
  773. conn->tpg->tpg_attrib.authentication) {
  774. pr_err("Initiator is requesting CSG: 1, has not been"
  775. " successfully authenticated, and the Target is"
  776. " enforcing iSCSI Authentication, login failed.\n");
  777. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  778. ISCSI_LOGIN_STATUS_AUTH_FAILED);
  779. return -1;
  780. }
  781. if (!iscsi_check_negotiated_keys(conn->param_list))
  782. if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
  783. (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
  784. login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
  785. ISCSI_FLAG_LOGIN_TRANSIT;
  786. return 0;
  787. }
  788. static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
  789. {
  790. int pdu_count = 0;
  791. struct iscsi_login_req *login_req;
  792. struct iscsi_login_rsp *login_rsp;
  793. login_req = (struct iscsi_login_req *) login->req;
  794. login_rsp = (struct iscsi_login_rsp *) login->rsp;
  795. while (1) {
  796. if (++pdu_count > MAX_LOGIN_PDUS) {
  797. pr_err("MAX_LOGIN_PDUS count reached.\n");
  798. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  799. ISCSI_LOGIN_STATUS_TARGET_ERROR);
  800. return -1;
  801. }
  802. switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
  803. case 0:
  804. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
  805. if (iscsi_target_handle_csg_zero(conn, login) < 0)
  806. return -1;
  807. break;
  808. case 1:
  809. login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
  810. if (iscsi_target_handle_csg_one(conn, login) < 0)
  811. return -1;
  812. if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
  813. login->tsih = conn->sess->tsih;
  814. login->login_complete = 1;
  815. iscsi_target_restore_sock_callbacks(conn);
  816. if (iscsi_target_do_tx_login_io(conn,
  817. login) < 0)
  818. return -1;
  819. return 1;
  820. }
  821. break;
  822. default:
  823. pr_err("Illegal CSG: %d received from"
  824. " Initiator, protocol error.\n",
  825. ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
  826. break;
  827. }
  828. if (iscsi_target_do_tx_login_io(conn, login) < 0)
  829. return -1;
  830. if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
  831. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
  832. login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
  833. }
  834. break;
  835. }
  836. if (conn->sock) {
  837. struct sock *sk = conn->sock->sk;
  838. bool state;
  839. read_lock_bh(&sk->sk_callback_lock);
  840. state = iscsi_target_sk_state_check(sk);
  841. read_unlock_bh(&sk->sk_callback_lock);
  842. if (!state) {
  843. pr_debug("iscsi_target_do_login() failed state for"
  844. " conn: %p\n", conn);
  845. return -1;
  846. }
  847. }
  848. return 0;
  849. }
  850. static void iscsi_initiatorname_tolower(
  851. char *param_buf)
  852. {
  853. char *c;
  854. u32 iqn_size = strlen(param_buf), i;
  855. for (i = 0; i < iqn_size; i++) {
  856. c = &param_buf[i];
  857. if (!isupper(*c))
  858. continue;
  859. *c = tolower(*c);
  860. }
  861. }
  862. /*
  863. * Processes the first Login Request..
  864. */
  865. int iscsi_target_locate_portal(
  866. struct iscsi_np *np,
  867. struct iscsi_conn *conn,
  868. struct iscsi_login *login)
  869. {
  870. char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
  871. char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
  872. struct iscsi_session *sess = conn->sess;
  873. struct iscsi_tiqn *tiqn;
  874. struct iscsi_tpg_np *tpg_np = NULL;
  875. struct iscsi_login_req *login_req;
  876. struct se_node_acl *se_nacl;
  877. u32 payload_length, queue_depth = 0;
  878. int sessiontype = 0, ret = 0, tag_num, tag_size;
  879. INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
  880. INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
  881. iscsi_target_set_sock_callbacks(conn);
  882. login->np = np;
  883. login_req = (struct iscsi_login_req *) login->req;
  884. payload_length = ntoh24(login_req->dlength);
  885. tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
  886. if (!tmpbuf) {
  887. pr_err("Unable to allocate memory for tmpbuf.\n");
  888. return -1;
  889. }
  890. memcpy(tmpbuf, login->req_buf, payload_length);
  891. tmpbuf[payload_length] = '\0';
  892. start = tmpbuf;
  893. end = (start + payload_length);
  894. /*
  895. * Locate the initial keys expected from the Initiator node in
  896. * the first login request in order to progress with the login phase.
  897. */
  898. while (start < end) {
  899. if (iscsi_extract_key_value(start, &key, &value) < 0) {
  900. ret = -1;
  901. goto out;
  902. }
  903. if (!strncmp(key, "InitiatorName", 13))
  904. i_buf = value;
  905. else if (!strncmp(key, "SessionType", 11))
  906. s_buf = value;
  907. else if (!strncmp(key, "TargetName", 10))
  908. t_buf = value;
  909. start += strlen(key) + strlen(value) + 2;
  910. }
  911. /*
  912. * See 5.3. Login Phase.
  913. */
  914. if (!i_buf) {
  915. pr_err("InitiatorName key not received"
  916. " in first login request.\n");
  917. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  918. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  919. ret = -1;
  920. goto out;
  921. }
  922. /*
  923. * Convert the incoming InitiatorName to lowercase following
  924. * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
  925. * are NOT case sensitive.
  926. */
  927. iscsi_initiatorname_tolower(i_buf);
  928. if (!s_buf) {
  929. if (!login->leading_connection)
  930. goto get_target;
  931. pr_err("SessionType key not received"
  932. " in first login request.\n");
  933. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  934. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  935. ret = -1;
  936. goto out;
  937. }
  938. /*
  939. * Use default portal group for discovery sessions.
  940. */
  941. sessiontype = strncmp(s_buf, DISCOVERY, 9);
  942. if (!sessiontype) {
  943. conn->tpg = iscsit_global->discovery_tpg;
  944. if (!login->leading_connection)
  945. goto get_target;
  946. sess->sess_ops->SessionType = 1;
  947. /*
  948. * Setup crc32c modules from libcrypto
  949. */
  950. if (iscsi_login_setup_crypto(conn) < 0) {
  951. pr_err("iscsi_login_setup_crypto() failed\n");
  952. ret = -1;
  953. goto out;
  954. }
  955. /*
  956. * Serialize access across the discovery struct iscsi_portal_group to
  957. * process login attempt.
  958. */
  959. if (iscsit_access_np(np, conn->tpg) < 0) {
  960. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  961. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  962. ret = -1;
  963. goto out;
  964. }
  965. ret = 0;
  966. goto alloc_tags;
  967. }
  968. get_target:
  969. if (!t_buf) {
  970. pr_err("TargetName key not received"
  971. " in first login request while"
  972. " SessionType=Normal.\n");
  973. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  974. ISCSI_LOGIN_STATUS_MISSING_FIELDS);
  975. ret = -1;
  976. goto out;
  977. }
  978. /*
  979. * Locate Target IQN from Storage Node.
  980. */
  981. tiqn = iscsit_get_tiqn_for_login(t_buf);
  982. if (!tiqn) {
  983. pr_err("Unable to locate Target IQN: %s in"
  984. " Storage Node\n", t_buf);
  985. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  986. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  987. ret = -1;
  988. goto out;
  989. }
  990. pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
  991. /*
  992. * Locate Target Portal Group from Storage Node.
  993. */
  994. conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
  995. if (!conn->tpg) {
  996. pr_err("Unable to locate Target Portal Group"
  997. " on %s\n", tiqn->tiqn);
  998. iscsit_put_tiqn_for_login(tiqn);
  999. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  1000. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  1001. ret = -1;
  1002. goto out;
  1003. }
  1004. conn->tpg_np = tpg_np;
  1005. pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
  1006. /*
  1007. * Setup crc32c modules from libcrypto
  1008. */
  1009. if (iscsi_login_setup_crypto(conn) < 0) {
  1010. pr_err("iscsi_login_setup_crypto() failed\n");
  1011. kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
  1012. iscsit_put_tiqn_for_login(tiqn);
  1013. conn->tpg = NULL;
  1014. ret = -1;
  1015. goto out;
  1016. }
  1017. /*
  1018. * Serialize access across the struct iscsi_portal_group to
  1019. * process login attempt.
  1020. */
  1021. if (iscsit_access_np(np, conn->tpg) < 0) {
  1022. kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
  1023. iscsit_put_tiqn_for_login(tiqn);
  1024. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  1025. ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
  1026. conn->tpg = NULL;
  1027. ret = -1;
  1028. goto out;
  1029. }
  1030. /*
  1031. * conn->sess->node_acl will be set when the referenced
  1032. * struct iscsi_session is located from received ISID+TSIH in
  1033. * iscsi_login_non_zero_tsih_s2().
  1034. */
  1035. if (!login->leading_connection) {
  1036. ret = 0;
  1037. goto out;
  1038. }
  1039. /*
  1040. * This value is required in iscsi_login_zero_tsih_s2()
  1041. */
  1042. sess->sess_ops->SessionType = 0;
  1043. /*
  1044. * Locate incoming Initiator IQN reference from Storage Node.
  1045. */
  1046. sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1047. &conn->tpg->tpg_se_tpg, i_buf);
  1048. if (!sess->se_sess->se_node_acl) {
  1049. pr_err("iSCSI Initiator Node: %s is not authorized to"
  1050. " access iSCSI target portal group: %hu.\n",
  1051. i_buf, conn->tpg->tpgt);
  1052. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
  1053. ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
  1054. ret = -1;
  1055. goto out;
  1056. }
  1057. se_nacl = sess->se_sess->se_node_acl;
  1058. queue_depth = se_nacl->queue_depth;
  1059. /*
  1060. * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
  1061. * depth for non immediate commands, plus extra tags for immediate
  1062. * commands.
  1063. *
  1064. * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
  1065. * in per-cpu-ida tag allocation logic + small queue_depth.
  1066. */
  1067. alloc_tags:
  1068. tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
  1069. tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
  1070. tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
  1071. ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
  1072. if (ret < 0) {
  1073. iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
  1074. ISCSI_LOGIN_STATUS_NO_RESOURCES);
  1075. ret = -1;
  1076. }
  1077. out:
  1078. kfree(tmpbuf);
  1079. return ret;
  1080. }
  1081. int iscsi_target_start_negotiation(
  1082. struct iscsi_login *login,
  1083. struct iscsi_conn *conn)
  1084. {
  1085. int ret;
  1086. if (conn->sock) {
  1087. struct sock *sk = conn->sock->sk;
  1088. write_lock_bh(&sk->sk_callback_lock);
  1089. set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
  1090. write_unlock_bh(&sk->sk_callback_lock);
  1091. }
  1092. ret = iscsi_target_do_login(conn, login);
  1093. if (ret < 0) {
  1094. cancel_delayed_work_sync(&conn->login_work);
  1095. cancel_delayed_work_sync(&conn->login_cleanup_work);
  1096. iscsi_target_restore_sock_callbacks(conn);
  1097. iscsi_remove_failed_auth_entry(conn);
  1098. }
  1099. if (ret != 0)
  1100. iscsi_target_nego_release(conn);
  1101. return ret;
  1102. }
  1103. void iscsi_target_nego_release(struct iscsi_conn *conn)
  1104. {
  1105. struct iscsi_login *login = conn->conn_login;
  1106. if (!login)
  1107. return;
  1108. kfree(login->req_buf);
  1109. kfree(login->rsp_buf);
  1110. kfree(login);
  1111. conn->conn_login = NULL;
  1112. }