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