iscsi_target_nego.c 32 KB

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