blob: 29eb09e0cd15370fc59774cebb7450bac690a476 [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
2 * This file contains main functions related to iSCSI Parameter negotiation.
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2007-2013 Datera, Inc.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00005 *
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
19#include <linux/ctype.h>
Nicholas Bellingere5419862015-07-22 23:14:19 -070020#include <linux/kthread.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080021#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010022#include <linux/sched/signal.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080023#include <net/sock.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000024#include <scsi/iscsi_proto.h>
25#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050026#include <target/target_core_fabric.h>
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080027#include <target/iscsi/iscsi_transport.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000028
Sagi Grimberg67f091f2015-01-07 14:57:31 +020029#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000030#include "iscsi_target_parameters.h"
31#include "iscsi_target_login.h"
32#include "iscsi_target_nego.h"
33#include "iscsi_target_tpg.h"
34#include "iscsi_target_util.h"
35#include "iscsi_target.h"
36#include "iscsi_target_auth.h"
37
38#define MAX_LOGIN_PDUS 7
39#define TEXT_LEN 4096
40
41void convert_null_to_semi(char *buf, int len)
42{
43 int i;
44
45 for (i = 0; i < len; i++)
46 if (buf[i] == '\0')
47 buf[i] = ';';
48}
49
Christoph Hellwigfceb5bc2012-09-26 08:00:36 -040050static int strlen_semi(char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +000051{
52 int i = 0;
53
54 while (buf[i] != '\0') {
55 if (buf[i] == ';')
56 return i;
57 i++;
58 }
59
60 return -1;
61}
62
63int extract_param(
64 const char *in_buf,
65 const char *pattern,
66 unsigned int max_length,
67 char *out_buf,
68 unsigned char *type)
69{
70 char *ptr;
71 int len;
72
73 if (!in_buf || !pattern || !out_buf || !type)
74 return -1;
75
76 ptr = strstr(in_buf, pattern);
77 if (!ptr)
78 return -1;
79
80 ptr = strstr(ptr, "=");
81 if (!ptr)
82 return -1;
83
84 ptr += 1;
85 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
86 ptr += 2; /* skip 0x */
87 *type = HEX;
88 } else
89 *type = DECIMAL;
90
91 len = strlen_semi(ptr);
92 if (len < 0)
93 return -1;
94
Eric Seppanen369653e2013-11-20 14:19:51 -080095 if (len >= max_length) {
Masanari Iida5e58b022012-02-27 23:18:15 +090096 pr_err("Length of input: %d exceeds max_length:"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000097 " %d\n", len, max_length);
98 return -1;
99 }
100 memcpy(out_buf, ptr, len);
101 out_buf[len] = '\0';
102
103 return 0;
104}
105
106static u32 iscsi_handle_authentication(
107 struct iscsi_conn *conn,
108 char *in_buf,
109 char *out_buf,
110 int in_length,
111 int *out_length,
112 unsigned char *authtype)
113{
114 struct iscsi_session *sess = conn->sess;
115 struct iscsi_node_auth *auth;
116 struct iscsi_node_acl *iscsi_nacl;
Nicholas Bellingerc3e51442013-06-19 18:48:51 -0700117 struct iscsi_portal_group *iscsi_tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000118 struct se_node_acl *se_nacl;
119
120 if (!sess->sess_ops->SessionType) {
121 /*
122 * For SessionType=Normal
123 */
124 se_nacl = conn->sess->se_sess->se_node_acl;
125 if (!se_nacl) {
126 pr_err("Unable to locate struct se_node_acl for"
127 " CHAP auth\n");
128 return -1;
129 }
130 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
131 se_node_acl);
132 if (!iscsi_nacl) {
133 pr_err("Unable to locate struct iscsi_node_acl for"
134 " CHAP auth\n");
135 return -1;
136 }
137
Nicholas Bellingerc3e51442013-06-19 18:48:51 -0700138 if (se_nacl->dynamic_node_acl) {
139 iscsi_tpg = container_of(se_nacl->se_tpg,
140 struct iscsi_portal_group, tpg_se_tpg);
141
142 auth = &iscsi_tpg->tpg_demo_auth;
143 } else {
144 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
145 se_node_acl);
146
Andy Groverb7eec2c2013-10-09 11:05:57 -0700147 auth = &iscsi_nacl->node_auth;
Nicholas Bellingerc3e51442013-06-19 18:48:51 -0700148 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000149 } else {
150 /*
151 * For SessionType=Discovery
152 */
153 auth = &iscsit_global->discovery_acl.node_auth;
154 }
155
156 if (strstr("CHAP", authtype))
157 strcpy(conn->sess->auth_type, "CHAP");
158 else
159 strcpy(conn->sess->auth_type, NONE);
160
161 if (strstr("None", authtype))
162 return 1;
163#ifdef CANSRP
164 else if (strstr("SRP", authtype))
165 return srp_main_loop(conn, auth, in_buf, out_buf,
166 &in_length, out_length);
167#endif
168 else if (strstr("CHAP", authtype))
169 return chap_main_loop(conn, auth, in_buf, out_buf,
170 &in_length, out_length);
171 else if (strstr("SPKM1", authtype))
172 return 2;
173 else if (strstr("SPKM2", authtype))
174 return 2;
175 else if (strstr("KRB5", authtype))
176 return 2;
177 else
178 return 2;
179}
180
181static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
182{
183 kfree(conn->auth_protocol);
184}
185
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800186int iscsi_target_check_login_request(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000187 struct iscsi_conn *conn,
188 struct iscsi_login *login)
189{
Jörn Engel28168902012-03-15 15:06:58 -0400190 int req_csg, req_nsg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000191 u32 payload_length;
192 struct iscsi_login_req *login_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000193
194 login_req = (struct iscsi_login_req *) login->req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000195 payload_length = ntoh24(login_req->dlength);
196
197 switch (login_req->opcode & ISCSI_OPCODE_MASK) {
198 case ISCSI_OP_LOGIN:
199 break;
200 default:
201 pr_err("Received unknown opcode 0x%02x.\n",
202 login_req->opcode & ISCSI_OPCODE_MASK);
203 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
204 ISCSI_LOGIN_STATUS_INIT_ERR);
205 return -1;
206 }
207
208 if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
209 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
210 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
211 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
212 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
213 ISCSI_LOGIN_STATUS_INIT_ERR);
214 return -1;
215 }
216
Andy Grover5d358062013-03-04 13:52:08 -0800217 req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
218 req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000219
220 if (req_csg != login->current_stage) {
221 pr_err("Initiator unexpectedly changed login stage"
222 " from %d to %d, login failed.\n", login->current_stage,
223 req_csg);
224 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
225 ISCSI_LOGIN_STATUS_INIT_ERR);
226 return -1;
227 }
228
229 if ((req_nsg == 2) || (req_csg >= 2) ||
230 ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
231 (req_nsg <= req_csg))) {
232 pr_err("Illegal login_req->flags Combination, CSG: %d,"
233 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
234 req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
235 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
236 ISCSI_LOGIN_STATUS_INIT_ERR);
237 return -1;
238 }
239
240 if ((login_req->max_version != login->version_max) ||
241 (login_req->min_version != login->version_min)) {
242 pr_err("Login request changed Version Max/Nin"
243 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
244 login_req->max_version, login_req->min_version);
245 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
246 ISCSI_LOGIN_STATUS_INIT_ERR);
247 return -1;
248 }
249
250 if (memcmp(login_req->isid, login->isid, 6) != 0) {
251 pr_err("Login request changed ISID unexpectedly,"
252 " protocol error.\n");
253 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
254 ISCSI_LOGIN_STATUS_INIT_ERR);
255 return -1;
256 }
257
258 if (login_req->itt != login->init_task_tag) {
259 pr_err("Login request changed ITT unexpectedly to"
260 " 0x%08x, protocol error.\n", login_req->itt);
261 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
262 ISCSI_LOGIN_STATUS_INIT_ERR);
263 return -1;
264 }
265
266 if (payload_length > MAX_KEY_VALUE_PAIRS) {
267 pr_err("Login request payload exceeds default"
268 " MaxRecvDataSegmentLength: %u, protocol error.\n",
269 MAX_KEY_VALUE_PAIRS);
270 return -1;
271 }
272
273 return 0;
274}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530275EXPORT_SYMBOL(iscsi_target_check_login_request);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000276
277static int iscsi_target_check_first_request(
278 struct iscsi_conn *conn,
279 struct iscsi_login *login)
280{
281 struct iscsi_param *param = NULL;
282 struct se_node_acl *se_nacl;
283
284 login->first_request = 0;
285
286 list_for_each_entry(param, &conn->param_list->param_list, p_list) {
287 if (!strncmp(param->name, SESSIONTYPE, 11)) {
288 if (!IS_PSTATE_ACCEPTOR(param)) {
289 pr_err("SessionType key not received"
290 " in first login request.\n");
291 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
292 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
293 return -1;
294 }
295 if (!strncmp(param->value, DISCOVERY, 9))
296 return 0;
297 }
298
299 if (!strncmp(param->name, INITIATORNAME, 13)) {
300 if (!IS_PSTATE_ACCEPTOR(param)) {
301 if (!login->leading_connection)
302 continue;
303
304 pr_err("InitiatorName key not received"
305 " in first login request.\n");
306 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
307 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
308 return -1;
309 }
310
311 /*
312 * For non-leading connections, double check that the
313 * received InitiatorName matches the existing session's
314 * struct iscsi_node_acl.
315 */
316 if (!login->leading_connection) {
317 se_nacl = conn->sess->se_sess->se_node_acl;
318 if (!se_nacl) {
319 pr_err("Unable to locate"
320 " struct se_node_acl\n");
321 iscsit_tx_login_rsp(conn,
322 ISCSI_STATUS_CLS_INITIATOR_ERR,
323 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
324 return -1;
325 }
326
327 if (strcmp(param->value,
328 se_nacl->initiatorname)) {
329 pr_err("Incorrect"
330 " InitiatorName: %s for this"
331 " iSCSI Initiator Node.\n",
332 param->value);
333 iscsit_tx_login_rsp(conn,
334 ISCSI_STATUS_CLS_INITIATOR_ERR,
335 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
336 return -1;
337 }
338 }
339 }
340 }
341
342 return 0;
343}
344
345static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
346{
347 u32 padding = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000348 struct iscsi_login_rsp *login_rsp;
349
350 login_rsp = (struct iscsi_login_rsp *) login->rsp;
351
352 login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
353 hton24(login_rsp->dlength, login->rsp_length);
354 memcpy(login_rsp->isid, login->isid, 6);
355 login_rsp->tsih = cpu_to_be16(login->tsih);
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400356 login_rsp->itt = login->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000357 login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
358 login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -0700359 login_rsp->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000360
361 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
362 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400363 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000364 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
365 ntohl(login_rsp->statsn), login->rsp_length);
366
367 padding = ((-login->rsp_length) & 3);
Nicholas Bellingere5419862015-07-22 23:14:19 -0700368 /*
369 * Before sending the last login response containing the transition
370 * bit for full-feature-phase, go ahead and start up TX/RX threads
371 * now to avoid potential resource allocation failures after the
372 * final login response has been sent.
373 */
374 if (login->login_complete) {
375 int rc = iscsit_start_kthreads(conn);
376 if (rc) {
377 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
378 ISCSI_LOGIN_STATUS_NO_RESOURCES);
379 return -1;
380 }
381 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000382
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800383 if (conn->conn_transport->iscsit_put_login_tx(conn, login,
384 login->rsp_length + padding) < 0)
Nicholas Bellingere5419862015-07-22 23:14:19 -0700385 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000386
387 login->rsp_length = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000388
389 return 0;
Nicholas Bellingere5419862015-07-22 23:14:19 -0700390
391err:
392 if (login->login_complete) {
393 if (conn->rx_thread && conn->rx_thread_active) {
394 send_sig(SIGINT, conn->rx_thread, 1);
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -0800395 complete(&conn->rx_login_comp);
Nicholas Bellingere5419862015-07-22 23:14:19 -0700396 kthread_stop(conn->rx_thread);
397 }
398 if (conn->tx_thread && conn->tx_thread_active) {
399 send_sig(SIGINT, conn->tx_thread, 1);
400 kthread_stop(conn->tx_thread);
401 }
402 spin_lock(&iscsit_global->ts_bitmap_lock);
403 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
404 get_order(1));
405 spin_unlock(&iscsit_global->ts_bitmap_lock);
406 }
407 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000408}
409
David S. Miller676d2362014-04-11 16:15:36 -0400410static void iscsi_target_sk_data_ready(struct sock *sk)
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700411{
412 struct iscsi_conn *conn = sk->sk_user_data;
413 bool rc;
414
415 pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
416
417 write_lock_bh(&sk->sk_callback_lock);
418 if (!sk->sk_user_data) {
419 write_unlock_bh(&sk->sk_callback_lock);
420 return;
421 }
422 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
423 write_unlock_bh(&sk->sk_callback_lock);
424 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
425 return;
426 }
427 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
428 write_unlock_bh(&sk->sk_callback_lock);
429 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
430 return;
431 }
432 if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
433 write_unlock_bh(&sk->sk_callback_lock);
434 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
435 return;
436 }
437
438 rc = schedule_delayed_work(&conn->login_work, 0);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200439 if (!rc) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700440 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
441 " got false\n");
442 }
443 write_unlock_bh(&sk->sk_callback_lock);
444}
445
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700446static void iscsi_target_sk_state_change(struct sock *);
447
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700448static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
449{
450 struct sock *sk;
451
452 if (!conn->sock)
453 return;
454
455 sk = conn->sock->sk;
456 pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
457
458 write_lock_bh(&sk->sk_callback_lock);
459 sk->sk_user_data = conn;
460 conn->orig_data_ready = sk->sk_data_ready;
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700461 conn->orig_state_change = sk->sk_state_change;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700462 sk->sk_data_ready = iscsi_target_sk_data_ready;
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700463 sk->sk_state_change = iscsi_target_sk_state_change;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700464 write_unlock_bh(&sk->sk_callback_lock);
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700465
466 sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
467 sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700468}
469
470static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
471{
472 struct sock *sk;
473
474 if (!conn->sock)
475 return;
476
477 sk = conn->sock->sk;
478 pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
479
480 write_lock_bh(&sk->sk_callback_lock);
481 if (!sk->sk_user_data) {
482 write_unlock_bh(&sk->sk_callback_lock);
483 return;
484 }
485 sk->sk_user_data = NULL;
486 sk->sk_data_ready = conn->orig_data_ready;
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700487 sk->sk_state_change = conn->orig_state_change;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700488 write_unlock_bh(&sk->sk_callback_lock);
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700489
490 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
491 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700492}
493
494static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
495
496static bool iscsi_target_sk_state_check(struct sock *sk)
497{
498 if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
499 pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
500 "returning FALSE\n");
501 return false;
502 }
503 return true;
504}
505
506static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
507{
508 struct iscsi_np *np = login->np;
509 bool zero_tsih = login->zero_tsih;
510
511 iscsi_remove_failed_auth_entry(conn);
512 iscsi_target_nego_release(conn);
513 iscsi_target_login_sess_out(conn, np, zero_tsih, true);
514}
515
516static void iscsi_target_login_timeout(unsigned long data)
517{
518 struct iscsi_conn *conn = (struct iscsi_conn *)data;
519
520 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
521
522 if (conn->login_kworker) {
523 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
524 conn->login_kworker->comm, conn->login_kworker->pid);
525 send_sig(SIGINT, conn->login_kworker, 1);
526 }
527}
528
529static void iscsi_target_do_login_rx(struct work_struct *work)
530{
531 struct iscsi_conn *conn = container_of(work,
532 struct iscsi_conn, login_work.work);
533 struct iscsi_login *login = conn->login;
534 struct iscsi_np *np = login->np;
535 struct iscsi_portal_group *tpg = conn->tpg;
536 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
537 struct timer_list login_timer;
538 int rc, zero_tsih = login->zero_tsih;
539 bool state;
540
541 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
542 conn, current->comm, current->pid);
543
544 spin_lock(&tpg->tpg_state_lock);
545 state = (tpg->tpg_state == TPG_STATE_ACTIVE);
546 spin_unlock(&tpg->tpg_state_lock);
547
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200548 if (!state) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700549 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
550 iscsi_target_restore_sock_callbacks(conn);
551 iscsi_target_login_drop(conn, login);
552 iscsit_deaccess_np(np, tpg, tpg_np);
553 return;
554 }
555
556 if (conn->sock) {
557 struct sock *sk = conn->sock->sk;
558
559 read_lock_bh(&sk->sk_callback_lock);
560 state = iscsi_target_sk_state_check(sk);
561 read_unlock_bh(&sk->sk_callback_lock);
562
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200563 if (!state) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700564 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
565 iscsi_target_restore_sock_callbacks(conn);
566 iscsi_target_login_drop(conn, login);
567 iscsit_deaccess_np(np, tpg, tpg_np);
568 return;
569 }
570 }
571
572 conn->login_kworker = current;
573 allow_signal(SIGINT);
574
575 init_timer(&login_timer);
576 login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
577 login_timer.data = (unsigned long)conn;
578 login_timer.function = iscsi_target_login_timeout;
579 add_timer(&login_timer);
580 pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
581
582 rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
583 del_timer_sync(&login_timer);
584 flush_signals(current);
585 conn->login_kworker = NULL;
586
587 if (rc < 0) {
588 iscsi_target_restore_sock_callbacks(conn);
589 iscsi_target_login_drop(conn, login);
590 iscsit_deaccess_np(np, tpg, tpg_np);
591 return;
592 }
593
594 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
595 conn, current->comm, current->pid);
596
597 rc = iscsi_target_do_login(conn, login);
598 if (rc < 0) {
599 iscsi_target_restore_sock_callbacks(conn);
600 iscsi_target_login_drop(conn, login);
601 iscsit_deaccess_np(np, tpg, tpg_np);
602 } else if (!rc) {
603 if (conn->sock) {
604 struct sock *sk = conn->sock->sk;
605
606 write_lock_bh(&sk->sk_callback_lock);
607 clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
608 write_unlock_bh(&sk->sk_callback_lock);
609 }
610 } else if (rc == 1) {
611 iscsi_target_nego_release(conn);
612 iscsi_post_login_handler(np, conn, zero_tsih);
613 iscsit_deaccess_np(np, tpg, tpg_np);
614 }
615}
616
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700617static void iscsi_target_do_cleanup(struct work_struct *work)
618{
619 struct iscsi_conn *conn = container_of(work,
620 struct iscsi_conn, login_cleanup_work.work);
621 struct sock *sk = conn->sock->sk;
622 struct iscsi_login *login = conn->login;
623 struct iscsi_np *np = login->np;
624 struct iscsi_portal_group *tpg = conn->tpg;
625 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
626
627 pr_debug("Entering iscsi_target_do_cleanup\n");
628
629 cancel_delayed_work_sync(&conn->login_work);
630 conn->orig_state_change(sk);
631
632 iscsi_target_restore_sock_callbacks(conn);
633 iscsi_target_login_drop(conn, login);
634 iscsit_deaccess_np(np, tpg, tpg_np);
635
636 pr_debug("iscsi_target_do_cleanup done()\n");
637}
638
639static void iscsi_target_sk_state_change(struct sock *sk)
640{
641 struct iscsi_conn *conn;
642 void (*orig_state_change)(struct sock *);
643 bool state;
644
645 pr_debug("Entering iscsi_target_sk_state_change\n");
646
647 write_lock_bh(&sk->sk_callback_lock);
648 conn = sk->sk_user_data;
649 if (!conn) {
650 write_unlock_bh(&sk->sk_callback_lock);
651 return;
652 }
653 orig_state_change = conn->orig_state_change;
654
655 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
656 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
657 conn);
658 write_unlock_bh(&sk->sk_callback_lock);
659 orig_state_change(sk);
660 return;
661 }
662 if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
663 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
664 " conn: %p\n", conn);
665 write_unlock_bh(&sk->sk_callback_lock);
666 orig_state_change(sk);
667 return;
668 }
669 if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
670 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
671 conn);
672 write_unlock_bh(&sk->sk_callback_lock);
673 orig_state_change(sk);
674 return;
675 }
676
677 state = iscsi_target_sk_state_check(sk);
678 write_unlock_bh(&sk->sk_callback_lock);
679
680 pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
681
682 if (!state) {
683 pr_debug("iscsi_target_sk_state_change got failed state\n");
684 schedule_delayed_work(&conn->login_cleanup_work, 0);
685 return;
686 }
687 orig_state_change(sk);
688}
689
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000690/*
691 * NOTE: We check for existing sessions or connections AFTER the initiator
692 * has been successfully authenticated in order to protect against faked
693 * ISID/TSIH combinations.
694 */
695static int iscsi_target_check_for_existing_instances(
696 struct iscsi_conn *conn,
697 struct iscsi_login *login)
698{
699 if (login->checked_for_existing)
700 return 0;
701
702 login->checked_for_existing = 1;
703
704 if (!login->tsih)
705 return iscsi_check_for_session_reinstatement(conn);
706 else
707 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
708 login->initial_exp_statsn);
709}
710
711static int iscsi_target_do_authentication(
712 struct iscsi_conn *conn,
713 struct iscsi_login *login)
714{
715 int authret;
716 u32 payload_length;
717 struct iscsi_param *param;
718 struct iscsi_login_req *login_req;
719 struct iscsi_login_rsp *login_rsp;
720
721 login_req = (struct iscsi_login_req *) login->req;
722 login_rsp = (struct iscsi_login_rsp *) login->rsp;
723 payload_length = ntoh24(login_req->dlength);
724
725 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
726 if (!param)
727 return -1;
728
729 authret = iscsi_handle_authentication(
730 conn,
731 login->req_buf,
732 login->rsp_buf,
733 payload_length,
734 &login->rsp_length,
735 param->value);
736 switch (authret) {
737 case 0:
738 pr_debug("Received OK response"
739 " from LIO Authentication, continuing.\n");
740 break;
741 case 1:
742 pr_debug("iSCSI security negotiation"
Joe Perchesbfb90352011-08-17 06:58:04 -0700743 " completed successfully.\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000744 login->auth_complete = 1;
745 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
746 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
747 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
748 ISCSI_FLAG_LOGIN_TRANSIT);
749 login->current_stage = 1;
750 }
751 return iscsi_target_check_for_existing_instances(
752 conn, login);
753 case 2:
754 pr_err("Security negotiation"
755 " failed.\n");
756 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
757 ISCSI_LOGIN_STATUS_AUTH_FAILED);
758 return -1;
759 default:
760 pr_err("Received unknown error %d from LIO"
761 " Authentication\n", authret);
762 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
763 ISCSI_LOGIN_STATUS_TARGET_ERROR);
764 return -1;
765 }
766
767 return 0;
768}
769
770static int iscsi_target_handle_csg_zero(
771 struct iscsi_conn *conn,
772 struct iscsi_login *login)
773{
774 int ret;
775 u32 payload_length;
776 struct iscsi_param *param;
777 struct iscsi_login_req *login_req;
778 struct iscsi_login_rsp *login_rsp;
779
780 login_req = (struct iscsi_login_req *) login->req;
781 login_rsp = (struct iscsi_login_rsp *) login->rsp;
782 payload_length = ntoh24(login_req->dlength);
783
784 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
785 if (!param)
786 return -1;
787
788 ret = iscsi_decode_text_input(
789 PHASE_SECURITY|PHASE_DECLARATIVE,
790 SENDER_INITIATOR|SENDER_RECEIVER,
791 login->req_buf,
792 payload_length,
Nicholas Bellinger9977bb12012-09-29 21:49:59 -0700793 conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794 if (ret < 0)
795 return -1;
796
797 if (ret > 0) {
798 if (login->auth_complete) {
799 pr_err("Initiator has already been"
800 " successfully authenticated, but is still"
801 " sending %s keys.\n", param->value);
802 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
803 ISCSI_LOGIN_STATUS_INIT_ERR);
804 return -1;
805 }
806
807 goto do_auth;
Nicholas Bellinger91f0abf2014-05-28 12:07:40 -0700808 } else if (!payload_length) {
809 pr_err("Initiator sent zero length security payload,"
810 " login failed\n");
811 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
812 ISCSI_LOGIN_STATUS_AUTH_FAILED);
813 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000814 }
815
816 if (login->first_request)
817 if (iscsi_target_check_first_request(conn, login) < 0)
818 return -1;
819
820 ret = iscsi_encode_text_output(
821 PHASE_SECURITY|PHASE_DECLARATIVE,
822 SENDER_TARGET,
823 login->rsp_buf,
824 &login->rsp_length,
825 conn->param_list);
826 if (ret < 0)
827 return -1;
828
829 if (!iscsi_check_negotiated_keys(conn->param_list)) {
Andy Grover60bfcf82013-10-09 11:05:58 -0700830 if (conn->tpg->tpg_attrib.authentication &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000831 !strncmp(param->value, NONE, 4)) {
832 pr_err("Initiator sent AuthMethod=None but"
833 " Target is enforcing iSCSI Authentication,"
834 " login failed.\n");
835 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
836 ISCSI_LOGIN_STATUS_AUTH_FAILED);
837 return -1;
838 }
839
Andy Grover60bfcf82013-10-09 11:05:58 -0700840 if (conn->tpg->tpg_attrib.authentication &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000841 !login->auth_complete)
842 return 0;
843
844 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
845 return 0;
846
847 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
848 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
849 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
850 ISCSI_FLAG_LOGIN_TRANSIT;
851 login->current_stage = 1;
852 }
853 }
854
855 return 0;
856do_auth:
857 return iscsi_target_do_authentication(conn, login);
858}
859
860static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
861{
862 int ret;
863 u32 payload_length;
864 struct iscsi_login_req *login_req;
865 struct iscsi_login_rsp *login_rsp;
866
867 login_req = (struct iscsi_login_req *) login->req;
868 login_rsp = (struct iscsi_login_rsp *) login->rsp;
869 payload_length = ntoh24(login_req->dlength);
870
871 ret = iscsi_decode_text_input(
872 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
873 SENDER_INITIATOR|SENDER_RECEIVER,
874 login->req_buf,
875 payload_length,
Nicholas Bellinger9977bb12012-09-29 21:49:59 -0700876 conn);
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800877 if (ret < 0) {
878 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
879 ISCSI_LOGIN_STATUS_INIT_ERR);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000880 return -1;
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800881 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000882
883 if (login->first_request)
884 if (iscsi_target_check_first_request(conn, login) < 0)
885 return -1;
886
887 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
888 return -1;
889
890 ret = iscsi_encode_text_output(
891 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
892 SENDER_TARGET,
893 login->rsp_buf,
894 &login->rsp_length,
895 conn->param_list);
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800896 if (ret < 0) {
897 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
898 ISCSI_LOGIN_STATUS_INIT_ERR);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000899 return -1;
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800900 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000901
902 if (!login->auth_complete &&
Andy Grover60bfcf82013-10-09 11:05:58 -0700903 conn->tpg->tpg_attrib.authentication) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000904 pr_err("Initiator is requesting CSG: 1, has not been"
905 " successfully authenticated, and the Target is"
906 " enforcing iSCSI Authentication, login failed.\n");
907 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
908 ISCSI_LOGIN_STATUS_AUTH_FAILED);
909 return -1;
910 }
911
912 if (!iscsi_check_negotiated_keys(conn->param_list))
913 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
914 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
915 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
916 ISCSI_FLAG_LOGIN_TRANSIT;
917
918 return 0;
919}
920
921static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
922{
923 int pdu_count = 0;
924 struct iscsi_login_req *login_req;
925 struct iscsi_login_rsp *login_rsp;
926
927 login_req = (struct iscsi_login_req *) login->req;
928 login_rsp = (struct iscsi_login_rsp *) login->rsp;
929
930 while (1) {
931 if (++pdu_count > MAX_LOGIN_PDUS) {
932 pr_err("MAX_LOGIN_PDUS count reached.\n");
933 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
934 ISCSI_LOGIN_STATUS_TARGET_ERROR);
935 return -1;
936 }
937
Andy Grover5d358062013-03-04 13:52:08 -0800938 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000939 case 0:
Andy Grover5d358062013-03-04 13:52:08 -0800940 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000941 if (iscsi_target_handle_csg_zero(conn, login) < 0)
942 return -1;
943 break;
944 case 1:
945 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
946 if (iscsi_target_handle_csg_one(conn, login) < 0)
947 return -1;
948 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
949 login->tsih = conn->sess->tsih;
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800950 login->login_complete = 1;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700951 iscsi_target_restore_sock_callbacks(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000952 if (iscsi_target_do_tx_login_io(conn,
953 login) < 0)
954 return -1;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700955 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000956 }
957 break;
958 default:
959 pr_err("Illegal CSG: %d received from"
960 " Initiator, protocol error.\n",
Andy Grover5d358062013-03-04 13:52:08 -0800961 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000962 break;
963 }
964
Nicholas Bellingerea3a1792013-09-05 14:55:37 -0700965 if (iscsi_target_do_tx_login_io(conn, login) < 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000966 return -1;
967
968 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
969 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
970 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
971 }
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700972 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000973 }
974
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700975 if (conn->sock) {
976 struct sock *sk = conn->sock->sk;
977 bool state;
978
979 read_lock_bh(&sk->sk_callback_lock);
980 state = iscsi_target_sk_state_check(sk);
981 read_unlock_bh(&sk->sk_callback_lock);
982
983 if (!state) {
984 pr_debug("iscsi_target_do_login() failed state for"
985 " conn: %p\n", conn);
986 return -1;
987 }
988 }
989
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000990 return 0;
991}
992
993static void iscsi_initiatorname_tolower(
994 char *param_buf)
995{
996 char *c;
997 u32 iqn_size = strlen(param_buf), i;
998
999 for (i = 0; i < iqn_size; i++) {
Jörn Engel8359cf42011-11-24 02:05:51 +01001000 c = &param_buf[i];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001001 if (!isupper(*c))
1002 continue;
1003
1004 *c = tolower(*c);
1005 }
1006}
1007
1008/*
1009 * Processes the first Login Request..
1010 */
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001011int iscsi_target_locate_portal(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001012 struct iscsi_np *np,
1013 struct iscsi_conn *conn,
1014 struct iscsi_login *login)
1015{
1016 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1017 char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1018 struct iscsi_session *sess = conn->sess;
1019 struct iscsi_tiqn *tiqn;
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001020 struct iscsi_tpg_np *tpg_np = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001021 struct iscsi_login_req *login_req;
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001022 struct se_node_acl *se_nacl;
1023 u32 payload_length, queue_depth = 0;
1024 int sessiontype = 0, ret = 0, tag_num, tag_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001026 INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
Nicholas Bellingerbb048352013-09-05 14:54:04 -07001027 INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001028 iscsi_target_set_sock_callbacks(conn);
1029
1030 login->np = np;
1031
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001032 login_req = (struct iscsi_login_req *) login->req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001033 payload_length = ntoh24(login_req->dlength);
1034
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001035 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1036 if (!tmpbuf) {
1037 pr_err("Unable to allocate memory for tmpbuf.\n");
1038 return -1;
1039 }
1040
1041 memcpy(tmpbuf, login->req_buf, payload_length);
1042 tmpbuf[payload_length] = '\0';
1043 start = tmpbuf;
1044 end = (start + payload_length);
1045
1046 /*
1047 * Locate the initial keys expected from the Initiator node in
1048 * the first login request in order to progress with the login phase.
1049 */
1050 while (start < end) {
1051 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1052 ret = -1;
1053 goto out;
1054 }
1055
1056 if (!strncmp(key, "InitiatorName", 13))
1057 i_buf = value;
1058 else if (!strncmp(key, "SessionType", 11))
1059 s_buf = value;
1060 else if (!strncmp(key, "TargetName", 10))
1061 t_buf = value;
1062
1063 start += strlen(key) + strlen(value) + 2;
1064 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001065 /*
1066 * See 5.3. Login Phase.
1067 */
1068 if (!i_buf) {
1069 pr_err("InitiatorName key not received"
1070 " in first login request.\n");
1071 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1072 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1073 ret = -1;
1074 goto out;
1075 }
1076 /*
1077 * Convert the incoming InitiatorName to lowercase following
1078 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1079 * are NOT case sensitive.
1080 */
1081 iscsi_initiatorname_tolower(i_buf);
1082
1083 if (!s_buf) {
1084 if (!login->leading_connection)
1085 goto get_target;
1086
1087 pr_err("SessionType key not received"
1088 " in first login request.\n");
1089 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1090 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1091 ret = -1;
1092 goto out;
1093 }
1094
1095 /*
1096 * Use default portal group for discovery sessions.
1097 */
1098 sessiontype = strncmp(s_buf, DISCOVERY, 9);
1099 if (!sessiontype) {
1100 conn->tpg = iscsit_global->discovery_tpg;
1101 if (!login->leading_connection)
1102 goto get_target;
1103
1104 sess->sess_ops->SessionType = 1;
1105 /*
1106 * Setup crc32c modules from libcrypto
1107 */
1108 if (iscsi_login_setup_crypto(conn) < 0) {
1109 pr_err("iscsi_login_setup_crypto() failed\n");
1110 ret = -1;
1111 goto out;
1112 }
1113 /*
1114 * Serialize access across the discovery struct iscsi_portal_group to
1115 * process login attempt.
1116 */
1117 if (iscsit_access_np(np, conn->tpg) < 0) {
1118 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1119 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1120 ret = -1;
1121 goto out;
1122 }
1123 ret = 0;
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001124 goto alloc_tags;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001125 }
1126
1127get_target:
1128 if (!t_buf) {
1129 pr_err("TargetName key not received"
1130 " in first login request while"
1131 " SessionType=Normal.\n");
1132 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1133 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1134 ret = -1;
1135 goto out;
1136 }
1137
1138 /*
1139 * Locate Target IQN from Storage Node.
1140 */
1141 tiqn = iscsit_get_tiqn_for_login(t_buf);
1142 if (!tiqn) {
1143 pr_err("Unable to locate Target IQN: %s in"
1144 " Storage Node\n", t_buf);
1145 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1146 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1147 ret = -1;
1148 goto out;
1149 }
1150 pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1151
1152 /*
1153 * Locate Target Portal Group from Storage Node.
1154 */
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001155 conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001156 if (!conn->tpg) {
1157 pr_err("Unable to locate Target Portal Group"
1158 " on %s\n", tiqn->tiqn);
1159 iscsit_put_tiqn_for_login(tiqn);
1160 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1161 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1162 ret = -1;
1163 goto out;
1164 }
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001165 conn->tpg_np = tpg_np;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001166 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1167 /*
1168 * Setup crc32c modules from libcrypto
1169 */
1170 if (iscsi_login_setup_crypto(conn) < 0) {
1171 pr_err("iscsi_login_setup_crypto() failed\n");
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001172 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1173 iscsit_put_tiqn_for_login(tiqn);
1174 conn->tpg = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001175 ret = -1;
1176 goto out;
1177 }
1178 /*
1179 * Serialize access across the struct iscsi_portal_group to
1180 * process login attempt.
1181 */
1182 if (iscsit_access_np(np, conn->tpg) < 0) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001183 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001184 iscsit_put_tiqn_for_login(tiqn);
1185 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1186 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001187 conn->tpg = NULL;
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001188 ret = -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001189 goto out;
1190 }
1191
1192 /*
1193 * conn->sess->node_acl will be set when the referenced
1194 * struct iscsi_session is located from received ISID+TSIH in
1195 * iscsi_login_non_zero_tsih_s2().
1196 */
1197 if (!login->leading_connection) {
1198 ret = 0;
1199 goto out;
1200 }
1201
1202 /*
1203 * This value is required in iscsi_login_zero_tsih_s2()
1204 */
1205 sess->sess_ops->SessionType = 0;
1206
1207 /*
1208 * Locate incoming Initiator IQN reference from Storage Node.
1209 */
1210 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1211 &conn->tpg->tpg_se_tpg, i_buf);
1212 if (!sess->se_sess->se_node_acl) {
1213 pr_err("iSCSI Initiator Node: %s is not authorized to"
1214 " access iSCSI target portal group: %hu.\n",
1215 i_buf, conn->tpg->tpgt);
1216 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1217 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1218 ret = -1;
1219 goto out;
1220 }
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001221 se_nacl = sess->se_sess->se_node_acl;
1222 queue_depth = se_nacl->queue_depth;
1223 /*
1224 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1225 * depth for non immediate commands, plus extra tags for immediate
1226 * commands.
1227 *
1228 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1229 * in per-cpu-ida tag allocation logic + small queue_depth.
1230 */
1231alloc_tags:
1232 tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
Nicholas Bellinger4a4caa22014-01-10 02:06:59 +00001233 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001234 tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001235
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001236 ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1237 if (ret < 0) {
1238 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1239 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1240 ret = -1;
1241 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001242out:
1243 kfree(tmpbuf);
1244 return ret;
1245}
1246
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001247int iscsi_target_start_negotiation(
1248 struct iscsi_login *login,
1249 struct iscsi_conn *conn)
1250{
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001251 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001252
Nicholas Bellinger8f0dfb32016-02-27 18:15:46 -08001253 if (conn->sock) {
1254 struct sock *sk = conn->sock->sk;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255
Nicholas Bellinger8f0dfb32016-02-27 18:15:46 -08001256 write_lock_bh(&sk->sk_callback_lock);
1257 set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1258 write_unlock_bh(&sk->sk_callback_lock);
1259 }
1260
1261 ret = iscsi_target_do_login(conn, login);
1262 if (ret < 0) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001263 cancel_delayed_work_sync(&conn->login_work);
Nicholas Bellingerbb048352013-09-05 14:54:04 -07001264 cancel_delayed_work_sync(&conn->login_cleanup_work);
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001265 iscsi_target_restore_sock_callbacks(conn);
1266 iscsi_remove_failed_auth_entry(conn);
1267 }
1268 if (ret != 0)
1269 iscsi_target_nego_release(conn);
1270
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001271 return ret;
1272}
1273
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001274void iscsi_target_nego_release(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001275{
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001276 struct iscsi_login *login = conn->conn_login;
1277
1278 if (!login)
1279 return;
1280
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001281 kfree(login->req_buf);
1282 kfree(login->rsp_buf);
1283 kfree(login);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001284
1285 conn->conn_login = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001286}