blob: e8efb4299a950d7aa950fd02d457685a2ff7568b [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>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000021#include <scsi/iscsi_proto.h>
22#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050023#include <target/target_core_fabric.h>
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080024#include <target/iscsi/iscsi_transport.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000025
Sagi Grimberg67f091f2015-01-07 14:57:31 +020026#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000027#include "iscsi_target_parameters.h"
28#include "iscsi_target_login.h"
29#include "iscsi_target_nego.h"
30#include "iscsi_target_tpg.h"
31#include "iscsi_target_util.h"
32#include "iscsi_target.h"
33#include "iscsi_target_auth.h"
34
35#define MAX_LOGIN_PDUS 7
36#define TEXT_LEN 4096
37
38void convert_null_to_semi(char *buf, int len)
39{
40 int i;
41
42 for (i = 0; i < len; i++)
43 if (buf[i] == '\0')
44 buf[i] = ';';
45}
46
Christoph Hellwigfceb5bc2012-09-26 08:00:36 -040047static int strlen_semi(char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +000048{
49 int i = 0;
50
51 while (buf[i] != '\0') {
52 if (buf[i] == ';')
53 return i;
54 i++;
55 }
56
57 return -1;
58}
59
60int extract_param(
61 const char *in_buf,
62 const char *pattern,
63 unsigned int max_length,
64 char *out_buf,
65 unsigned char *type)
66{
67 char *ptr;
68 int len;
69
70 if (!in_buf || !pattern || !out_buf || !type)
71 return -1;
72
73 ptr = strstr(in_buf, pattern);
74 if (!ptr)
75 return -1;
76
77 ptr = strstr(ptr, "=");
78 if (!ptr)
79 return -1;
80
81 ptr += 1;
82 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
83 ptr += 2; /* skip 0x */
84 *type = HEX;
85 } else
86 *type = DECIMAL;
87
88 len = strlen_semi(ptr);
89 if (len < 0)
90 return -1;
91
Eric Seppanen369653e2013-11-20 14:19:51 -080092 if (len >= max_length) {
Masanari Iida5e58b022012-02-27 23:18:15 +090093 pr_err("Length of input: %d exceeds max_length:"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000094 " %d\n", len, max_length);
95 return -1;
96 }
97 memcpy(out_buf, ptr, len);
98 out_buf[len] = '\0';
99
100 return 0;
101}
102
103static u32 iscsi_handle_authentication(
104 struct iscsi_conn *conn,
105 char *in_buf,
106 char *out_buf,
107 int in_length,
108 int *out_length,
109 unsigned char *authtype)
110{
111 struct iscsi_session *sess = conn->sess;
112 struct iscsi_node_auth *auth;
113 struct iscsi_node_acl *iscsi_nacl;
Nicholas Bellingerc3e51442013-06-19 18:48:51 -0700114 struct iscsi_portal_group *iscsi_tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000115 struct se_node_acl *se_nacl;
116
117 if (!sess->sess_ops->SessionType) {
118 /*
119 * For SessionType=Normal
120 */
121 se_nacl = conn->sess->se_sess->se_node_acl;
122 if (!se_nacl) {
123 pr_err("Unable to locate struct se_node_acl for"
124 " CHAP auth\n");
125 return -1;
126 }
127 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
128 se_node_acl);
129 if (!iscsi_nacl) {
130 pr_err("Unable to locate struct iscsi_node_acl for"
131 " CHAP auth\n");
132 return -1;
133 }
134
Nicholas Bellingerc3e51442013-06-19 18:48:51 -0700135 if (se_nacl->dynamic_node_acl) {
136 iscsi_tpg = container_of(se_nacl->se_tpg,
137 struct iscsi_portal_group, tpg_se_tpg);
138
139 auth = &iscsi_tpg->tpg_demo_auth;
140 } else {
141 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
142 se_node_acl);
143
Andy Groverb7eec2c2013-10-09 11:05:57 -0700144 auth = &iscsi_nacl->node_auth;
Nicholas Bellingerc3e51442013-06-19 18:48:51 -0700145 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000146 } else {
147 /*
148 * For SessionType=Discovery
149 */
150 auth = &iscsit_global->discovery_acl.node_auth;
151 }
152
153 if (strstr("CHAP", authtype))
154 strcpy(conn->sess->auth_type, "CHAP");
155 else
156 strcpy(conn->sess->auth_type, NONE);
157
158 if (strstr("None", authtype))
159 return 1;
160#ifdef CANSRP
161 else if (strstr("SRP", authtype))
162 return srp_main_loop(conn, auth, in_buf, out_buf,
163 &in_length, out_length);
164#endif
165 else if (strstr("CHAP", authtype))
166 return chap_main_loop(conn, auth, in_buf, out_buf,
167 &in_length, out_length);
168 else if (strstr("SPKM1", authtype))
169 return 2;
170 else if (strstr("SPKM2", authtype))
171 return 2;
172 else if (strstr("KRB5", authtype))
173 return 2;
174 else
175 return 2;
176}
177
178static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
179{
180 kfree(conn->auth_protocol);
181}
182
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800183int iscsi_target_check_login_request(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000184 struct iscsi_conn *conn,
185 struct iscsi_login *login)
186{
Jörn Engel28168902012-03-15 15:06:58 -0400187 int req_csg, req_nsg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000188 u32 payload_length;
189 struct iscsi_login_req *login_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000190
191 login_req = (struct iscsi_login_req *) login->req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000192 payload_length = ntoh24(login_req->dlength);
193
194 switch (login_req->opcode & ISCSI_OPCODE_MASK) {
195 case ISCSI_OP_LOGIN:
196 break;
197 default:
198 pr_err("Received unknown opcode 0x%02x.\n",
199 login_req->opcode & ISCSI_OPCODE_MASK);
200 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
201 ISCSI_LOGIN_STATUS_INIT_ERR);
202 return -1;
203 }
204
205 if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
206 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
207 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
208 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
209 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
210 ISCSI_LOGIN_STATUS_INIT_ERR);
211 return -1;
212 }
213
Andy Grover5d358062013-03-04 13:52:08 -0800214 req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
215 req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000216
217 if (req_csg != login->current_stage) {
218 pr_err("Initiator unexpectedly changed login stage"
219 " from %d to %d, login failed.\n", login->current_stage,
220 req_csg);
221 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
222 ISCSI_LOGIN_STATUS_INIT_ERR);
223 return -1;
224 }
225
226 if ((req_nsg == 2) || (req_csg >= 2) ||
227 ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
228 (req_nsg <= req_csg))) {
229 pr_err("Illegal login_req->flags Combination, CSG: %d,"
230 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
231 req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
232 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
233 ISCSI_LOGIN_STATUS_INIT_ERR);
234 return -1;
235 }
236
237 if ((login_req->max_version != login->version_max) ||
238 (login_req->min_version != login->version_min)) {
239 pr_err("Login request changed Version Max/Nin"
240 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
241 login_req->max_version, login_req->min_version);
242 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
243 ISCSI_LOGIN_STATUS_INIT_ERR);
244 return -1;
245 }
246
247 if (memcmp(login_req->isid, login->isid, 6) != 0) {
248 pr_err("Login request changed ISID unexpectedly,"
249 " protocol error.\n");
250 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
251 ISCSI_LOGIN_STATUS_INIT_ERR);
252 return -1;
253 }
254
255 if (login_req->itt != login->init_task_tag) {
256 pr_err("Login request changed ITT unexpectedly to"
257 " 0x%08x, protocol error.\n", login_req->itt);
258 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
259 ISCSI_LOGIN_STATUS_INIT_ERR);
260 return -1;
261 }
262
263 if (payload_length > MAX_KEY_VALUE_PAIRS) {
264 pr_err("Login request payload exceeds default"
265 " MaxRecvDataSegmentLength: %u, protocol error.\n",
266 MAX_KEY_VALUE_PAIRS);
267 return -1;
268 }
269
270 return 0;
271}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530272EXPORT_SYMBOL(iscsi_target_check_login_request);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000273
274static int iscsi_target_check_first_request(
275 struct iscsi_conn *conn,
276 struct iscsi_login *login)
277{
278 struct iscsi_param *param = NULL;
279 struct se_node_acl *se_nacl;
280
281 login->first_request = 0;
282
283 list_for_each_entry(param, &conn->param_list->param_list, p_list) {
284 if (!strncmp(param->name, SESSIONTYPE, 11)) {
285 if (!IS_PSTATE_ACCEPTOR(param)) {
286 pr_err("SessionType key not received"
287 " in first login request.\n");
288 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
289 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
290 return -1;
291 }
292 if (!strncmp(param->value, DISCOVERY, 9))
293 return 0;
294 }
295
296 if (!strncmp(param->name, INITIATORNAME, 13)) {
297 if (!IS_PSTATE_ACCEPTOR(param)) {
298 if (!login->leading_connection)
299 continue;
300
301 pr_err("InitiatorName key not received"
302 " in first login request.\n");
303 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
304 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
305 return -1;
306 }
307
308 /*
309 * For non-leading connections, double check that the
310 * received InitiatorName matches the existing session's
311 * struct iscsi_node_acl.
312 */
313 if (!login->leading_connection) {
314 se_nacl = conn->sess->se_sess->se_node_acl;
315 if (!se_nacl) {
316 pr_err("Unable to locate"
317 " struct se_node_acl\n");
318 iscsit_tx_login_rsp(conn,
319 ISCSI_STATUS_CLS_INITIATOR_ERR,
320 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
321 return -1;
322 }
323
324 if (strcmp(param->value,
325 se_nacl->initiatorname)) {
326 pr_err("Incorrect"
327 " InitiatorName: %s for this"
328 " iSCSI Initiator Node.\n",
329 param->value);
330 iscsit_tx_login_rsp(conn,
331 ISCSI_STATUS_CLS_INITIATOR_ERR,
332 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
333 return -1;
334 }
335 }
336 }
337 }
338
339 return 0;
340}
341
342static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
343{
344 u32 padding = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000345 struct iscsi_login_rsp *login_rsp;
346
347 login_rsp = (struct iscsi_login_rsp *) login->rsp;
348
349 login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
350 hton24(login_rsp->dlength, login->rsp_length);
351 memcpy(login_rsp->isid, login->isid, 6);
352 login_rsp->tsih = cpu_to_be16(login->tsih);
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400353 login_rsp->itt = login->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000354 login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
355 login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -0700356 login_rsp->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000357
358 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
359 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400360 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000361 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
362 ntohl(login_rsp->statsn), login->rsp_length);
363
364 padding = ((-login->rsp_length) & 3);
Nicholas Bellingere5419862015-07-22 23:14:19 -0700365 /*
366 * Before sending the last login response containing the transition
367 * bit for full-feature-phase, go ahead and start up TX/RX threads
368 * now to avoid potential resource allocation failures after the
369 * final login response has been sent.
370 */
371 if (login->login_complete) {
372 int rc = iscsit_start_kthreads(conn);
373 if (rc) {
374 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
375 ISCSI_LOGIN_STATUS_NO_RESOURCES);
376 return -1;
377 }
378 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000379
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800380 if (conn->conn_transport->iscsit_put_login_tx(conn, login,
381 login->rsp_length + padding) < 0)
Nicholas Bellingere5419862015-07-22 23:14:19 -0700382 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000383
384 login->rsp_length = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000385
386 return 0;
Nicholas Bellingere5419862015-07-22 23:14:19 -0700387
388err:
389 if (login->login_complete) {
390 if (conn->rx_thread && conn->rx_thread_active) {
391 send_sig(SIGINT, conn->rx_thread, 1);
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -0800392 complete(&conn->rx_login_comp);
Nicholas Bellingere5419862015-07-22 23:14:19 -0700393 kthread_stop(conn->rx_thread);
394 }
395 if (conn->tx_thread && conn->tx_thread_active) {
396 send_sig(SIGINT, conn->tx_thread, 1);
397 kthread_stop(conn->tx_thread);
398 }
399 spin_lock(&iscsit_global->ts_bitmap_lock);
400 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
401 get_order(1));
402 spin_unlock(&iscsit_global->ts_bitmap_lock);
403 }
404 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000405}
406
David S. Miller676d2362014-04-11 16:15:36 -0400407static void iscsi_target_sk_data_ready(struct sock *sk)
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700408{
409 struct iscsi_conn *conn = sk->sk_user_data;
410 bool rc;
411
412 pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
413
414 write_lock_bh(&sk->sk_callback_lock);
415 if (!sk->sk_user_data) {
416 write_unlock_bh(&sk->sk_callback_lock);
417 return;
418 }
419 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
420 write_unlock_bh(&sk->sk_callback_lock);
421 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
422 return;
423 }
424 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
425 write_unlock_bh(&sk->sk_callback_lock);
426 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
427 return;
428 }
429 if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
430 write_unlock_bh(&sk->sk_callback_lock);
431 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
432 return;
433 }
434
435 rc = schedule_delayed_work(&conn->login_work, 0);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200436 if (!rc) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700437 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
438 " got false\n");
439 }
440 write_unlock_bh(&sk->sk_callback_lock);
441}
442
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700443static void iscsi_target_sk_state_change(struct sock *);
444
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700445static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
446{
447 struct sock *sk;
448
449 if (!conn->sock)
450 return;
451
452 sk = conn->sock->sk;
453 pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
454
455 write_lock_bh(&sk->sk_callback_lock);
456 sk->sk_user_data = conn;
457 conn->orig_data_ready = sk->sk_data_ready;
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700458 conn->orig_state_change = sk->sk_state_change;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700459 sk->sk_data_ready = iscsi_target_sk_data_ready;
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700460 sk->sk_state_change = iscsi_target_sk_state_change;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700461 write_unlock_bh(&sk->sk_callback_lock);
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700462
463 sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
464 sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700465}
466
467static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
468{
469 struct sock *sk;
470
471 if (!conn->sock)
472 return;
473
474 sk = conn->sock->sk;
475 pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
476
477 write_lock_bh(&sk->sk_callback_lock);
478 if (!sk->sk_user_data) {
479 write_unlock_bh(&sk->sk_callback_lock);
480 return;
481 }
482 sk->sk_user_data = NULL;
483 sk->sk_data_ready = conn->orig_data_ready;
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700484 sk->sk_state_change = conn->orig_state_change;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700485 write_unlock_bh(&sk->sk_callback_lock);
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700486
487 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
488 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700489}
490
491static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
492
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700493static bool __iscsi_target_sk_check_close(struct sock *sk)
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700494{
495 if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700496 pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700497 "returning FALSE\n");
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700498 return true;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700499 }
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700500 return false;
501}
502
503static bool iscsi_target_sk_check_close(struct iscsi_conn *conn)
504{
505 bool state = false;
506
507 if (conn->sock) {
508 struct sock *sk = conn->sock->sk;
509
510 read_lock_bh(&sk->sk_callback_lock);
511 state = (__iscsi_target_sk_check_close(sk) ||
512 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
513 read_unlock_bh(&sk->sk_callback_lock);
514 }
515 return state;
516}
517
518static bool iscsi_target_sk_check_flag(struct iscsi_conn *conn, unsigned int flag)
519{
520 bool state = false;
521
522 if (conn->sock) {
523 struct sock *sk = conn->sock->sk;
524
525 read_lock_bh(&sk->sk_callback_lock);
526 state = test_bit(flag, &conn->login_flags);
527 read_unlock_bh(&sk->sk_callback_lock);
528 }
529 return state;
530}
531
532static bool iscsi_target_sk_check_and_clear(struct iscsi_conn *conn, unsigned int flag)
533{
534 bool state = false;
535
536 if (conn->sock) {
537 struct sock *sk = conn->sock->sk;
538
539 write_lock_bh(&sk->sk_callback_lock);
540 state = (__iscsi_target_sk_check_close(sk) ||
541 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
542 if (!state)
543 clear_bit(flag, &conn->login_flags);
544 write_unlock_bh(&sk->sk_callback_lock);
545 }
546 return state;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700547}
548
549static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
550{
551 struct iscsi_np *np = login->np;
552 bool zero_tsih = login->zero_tsih;
553
554 iscsi_remove_failed_auth_entry(conn);
555 iscsi_target_nego_release(conn);
556 iscsi_target_login_sess_out(conn, np, zero_tsih, true);
557}
558
559static void iscsi_target_login_timeout(unsigned long data)
560{
561 struct iscsi_conn *conn = (struct iscsi_conn *)data;
562
563 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
564
565 if (conn->login_kworker) {
566 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
567 conn->login_kworker->comm, conn->login_kworker->pid);
568 send_sig(SIGINT, conn->login_kworker, 1);
569 }
570}
571
572static void iscsi_target_do_login_rx(struct work_struct *work)
573{
574 struct iscsi_conn *conn = container_of(work,
575 struct iscsi_conn, login_work.work);
576 struct iscsi_login *login = conn->login;
577 struct iscsi_np *np = login->np;
578 struct iscsi_portal_group *tpg = conn->tpg;
579 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
580 struct timer_list login_timer;
581 int rc, zero_tsih = login->zero_tsih;
582 bool state;
583
584 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
585 conn, current->comm, current->pid);
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700586 /*
587 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
588 * before initial PDU processing in iscsi_target_start_negotiation()
589 * has completed, go ahead and retry until it's cleared.
590 *
591 * Otherwise if the TCP connection drops while this is occuring,
592 * iscsi_target_start_negotiation() will detect the failure, call
593 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
594 * remaining iscsi connection resources from iscsi_np process context.
595 */
596 if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
597 schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
598 return;
599 }
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700600
601 spin_lock(&tpg->tpg_state_lock);
602 state = (tpg->tpg_state == TPG_STATE_ACTIVE);
603 spin_unlock(&tpg->tpg_state_lock);
604
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200605 if (!state) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700606 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700607 goto err;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700608 }
609
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700610 if (iscsi_target_sk_check_close(conn)) {
611 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
612 goto err;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700613 }
614
615 conn->login_kworker = current;
616 allow_signal(SIGINT);
617
618 init_timer(&login_timer);
619 login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
620 login_timer.data = (unsigned long)conn;
621 login_timer.function = iscsi_target_login_timeout;
622 add_timer(&login_timer);
623 pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
624
625 rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
626 del_timer_sync(&login_timer);
627 flush_signals(current);
628 conn->login_kworker = NULL;
629
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700630 if (rc < 0)
631 goto err;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700632
633 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
634 conn, current->comm, current->pid);
635
636 rc = iscsi_target_do_login(conn, login);
637 if (rc < 0) {
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700638 goto err;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700639 } else if (!rc) {
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700640 if (iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_READ_ACTIVE))
641 goto err;
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700642 } else if (rc == 1) {
643 iscsi_target_nego_release(conn);
644 iscsi_post_login_handler(np, conn, zero_tsih);
645 iscsit_deaccess_np(np, tpg, tpg_np);
646 }
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700647 return;
648
649err:
650 iscsi_target_restore_sock_callbacks(conn);
651 iscsi_target_login_drop(conn, login);
652 iscsit_deaccess_np(np, tpg, tpg_np);
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700653}
654
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700655static void iscsi_target_do_cleanup(struct work_struct *work)
656{
657 struct iscsi_conn *conn = container_of(work,
658 struct iscsi_conn, login_cleanup_work.work);
659 struct sock *sk = conn->sock->sk;
660 struct iscsi_login *login = conn->login;
661 struct iscsi_np *np = login->np;
662 struct iscsi_portal_group *tpg = conn->tpg;
663 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
664
665 pr_debug("Entering iscsi_target_do_cleanup\n");
666
667 cancel_delayed_work_sync(&conn->login_work);
668 conn->orig_state_change(sk);
669
670 iscsi_target_restore_sock_callbacks(conn);
671 iscsi_target_login_drop(conn, login);
672 iscsit_deaccess_np(np, tpg, tpg_np);
673
674 pr_debug("iscsi_target_do_cleanup done()\n");
675}
676
677static void iscsi_target_sk_state_change(struct sock *sk)
678{
679 struct iscsi_conn *conn;
680 void (*orig_state_change)(struct sock *);
681 bool state;
682
683 pr_debug("Entering iscsi_target_sk_state_change\n");
684
685 write_lock_bh(&sk->sk_callback_lock);
686 conn = sk->sk_user_data;
687 if (!conn) {
688 write_unlock_bh(&sk->sk_callback_lock);
689 return;
690 }
691 orig_state_change = conn->orig_state_change;
692
693 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
694 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
695 conn);
696 write_unlock_bh(&sk->sk_callback_lock);
697 orig_state_change(sk);
698 return;
699 }
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700700 state = __iscsi_target_sk_check_close(sk);
701 pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
702
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700703 if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
704 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
705 " conn: %p\n", conn);
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700706 if (state)
707 set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700708 write_unlock_bh(&sk->sk_callback_lock);
709 orig_state_change(sk);
710 return;
711 }
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700712 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700713 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
714 conn);
715 write_unlock_bh(&sk->sk_callback_lock);
716 orig_state_change(sk);
717 return;
718 }
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700719 /*
720 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
721 * but only queue conn->login_work -> iscsi_target_do_login_rx()
722 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
723 *
724 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
725 * will detect the dropped TCP connection from delayed workqueue context.
726 *
727 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
728 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
729 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
730 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
731 * dropped TCP connection in iscsi_np process context, and cleaning up
732 * the remaining iscsi connection resources.
733 */
734 if (state) {
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700735 pr_debug("iscsi_target_sk_state_change got failed state\n");
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700736 set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
737 state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
738 write_unlock_bh(&sk->sk_callback_lock);
739
740 orig_state_change(sk);
741
742 if (!state)
743 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700744 return;
745 }
Nicholas Bellingerbdabf092017-05-24 21:47:09 -0700746 write_unlock_bh(&sk->sk_callback_lock);
747
Nicholas Bellingerbb048352013-09-05 14:54:04 -0700748 orig_state_change(sk);
749}
750
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000751/*
752 * NOTE: We check for existing sessions or connections AFTER the initiator
753 * has been successfully authenticated in order to protect against faked
754 * ISID/TSIH combinations.
755 */
756static int iscsi_target_check_for_existing_instances(
757 struct iscsi_conn *conn,
758 struct iscsi_login *login)
759{
760 if (login->checked_for_existing)
761 return 0;
762
763 login->checked_for_existing = 1;
764
765 if (!login->tsih)
766 return iscsi_check_for_session_reinstatement(conn);
767 else
768 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
769 login->initial_exp_statsn);
770}
771
772static int iscsi_target_do_authentication(
773 struct iscsi_conn *conn,
774 struct iscsi_login *login)
775{
776 int authret;
777 u32 payload_length;
778 struct iscsi_param *param;
779 struct iscsi_login_req *login_req;
780 struct iscsi_login_rsp *login_rsp;
781
782 login_req = (struct iscsi_login_req *) login->req;
783 login_rsp = (struct iscsi_login_rsp *) login->rsp;
784 payload_length = ntoh24(login_req->dlength);
785
786 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
787 if (!param)
788 return -1;
789
790 authret = iscsi_handle_authentication(
791 conn,
792 login->req_buf,
793 login->rsp_buf,
794 payload_length,
795 &login->rsp_length,
796 param->value);
797 switch (authret) {
798 case 0:
799 pr_debug("Received OK response"
800 " from LIO Authentication, continuing.\n");
801 break;
802 case 1:
803 pr_debug("iSCSI security negotiation"
Joe Perchesbfb90352011-08-17 06:58:04 -0700804 " completed successfully.\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000805 login->auth_complete = 1;
806 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
807 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
808 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
809 ISCSI_FLAG_LOGIN_TRANSIT);
810 login->current_stage = 1;
811 }
812 return iscsi_target_check_for_existing_instances(
813 conn, login);
814 case 2:
815 pr_err("Security negotiation"
816 " failed.\n");
817 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
818 ISCSI_LOGIN_STATUS_AUTH_FAILED);
819 return -1;
820 default:
821 pr_err("Received unknown error %d from LIO"
822 " Authentication\n", authret);
823 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
824 ISCSI_LOGIN_STATUS_TARGET_ERROR);
825 return -1;
826 }
827
828 return 0;
829}
830
831static int iscsi_target_handle_csg_zero(
832 struct iscsi_conn *conn,
833 struct iscsi_login *login)
834{
835 int ret;
836 u32 payload_length;
837 struct iscsi_param *param;
838 struct iscsi_login_req *login_req;
839 struct iscsi_login_rsp *login_rsp;
840
841 login_req = (struct iscsi_login_req *) login->req;
842 login_rsp = (struct iscsi_login_rsp *) login->rsp;
843 payload_length = ntoh24(login_req->dlength);
844
845 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
846 if (!param)
847 return -1;
848
849 ret = iscsi_decode_text_input(
850 PHASE_SECURITY|PHASE_DECLARATIVE,
851 SENDER_INITIATOR|SENDER_RECEIVER,
852 login->req_buf,
853 payload_length,
Nicholas Bellinger9977bb12012-09-29 21:49:59 -0700854 conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000855 if (ret < 0)
856 return -1;
857
858 if (ret > 0) {
859 if (login->auth_complete) {
860 pr_err("Initiator has already been"
861 " successfully authenticated, but is still"
862 " sending %s keys.\n", param->value);
863 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
864 ISCSI_LOGIN_STATUS_INIT_ERR);
865 return -1;
866 }
867
868 goto do_auth;
Nicholas Bellinger91f0abf2014-05-28 12:07:40 -0700869 } else if (!payload_length) {
870 pr_err("Initiator sent zero length security payload,"
871 " login failed\n");
872 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
873 ISCSI_LOGIN_STATUS_AUTH_FAILED);
874 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000875 }
876
877 if (login->first_request)
878 if (iscsi_target_check_first_request(conn, login) < 0)
879 return -1;
880
881 ret = iscsi_encode_text_output(
882 PHASE_SECURITY|PHASE_DECLARATIVE,
883 SENDER_TARGET,
884 login->rsp_buf,
885 &login->rsp_length,
Nicholas Bellinger732e3c72017-07-07 14:45:49 -0700886 conn->param_list,
887 conn->tpg->tpg_attrib.login_keys_workaround);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 if (ret < 0)
889 return -1;
890
891 if (!iscsi_check_negotiated_keys(conn->param_list)) {
Andy Grover60bfcf82013-10-09 11:05:58 -0700892 if (conn->tpg->tpg_attrib.authentication &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000893 !strncmp(param->value, NONE, 4)) {
894 pr_err("Initiator sent AuthMethod=None but"
895 " Target is enforcing iSCSI Authentication,"
896 " login failed.\n");
897 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
898 ISCSI_LOGIN_STATUS_AUTH_FAILED);
899 return -1;
900 }
901
Andy Grover60bfcf82013-10-09 11:05:58 -0700902 if (conn->tpg->tpg_attrib.authentication &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000903 !login->auth_complete)
904 return 0;
905
906 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
907 return 0;
908
909 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
910 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
911 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
912 ISCSI_FLAG_LOGIN_TRANSIT;
913 login->current_stage = 1;
914 }
915 }
916
917 return 0;
918do_auth:
919 return iscsi_target_do_authentication(conn, login);
920}
921
922static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
923{
924 int ret;
925 u32 payload_length;
926 struct iscsi_login_req *login_req;
927 struct iscsi_login_rsp *login_rsp;
928
929 login_req = (struct iscsi_login_req *) login->req;
930 login_rsp = (struct iscsi_login_rsp *) login->rsp;
931 payload_length = ntoh24(login_req->dlength);
932
933 ret = iscsi_decode_text_input(
934 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
935 SENDER_INITIATOR|SENDER_RECEIVER,
936 login->req_buf,
937 payload_length,
Nicholas Bellinger9977bb12012-09-29 21:49:59 -0700938 conn);
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800939 if (ret < 0) {
940 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
941 ISCSI_LOGIN_STATUS_INIT_ERR);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000942 return -1;
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800943 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000944
945 if (login->first_request)
946 if (iscsi_target_check_first_request(conn, login) < 0)
947 return -1;
948
949 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
950 return -1;
951
952 ret = iscsi_encode_text_output(
953 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
954 SENDER_TARGET,
955 login->rsp_buf,
956 &login->rsp_length,
Nicholas Bellinger732e3c72017-07-07 14:45:49 -0700957 conn->param_list,
958 conn->tpg->tpg_attrib.login_keys_workaround);
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800959 if (ret < 0) {
960 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
961 ISCSI_LOGIN_STATUS_INIT_ERR);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000962 return -1;
Roland Dreier1c5c12c2012-11-05 18:02:42 -0800963 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000964
965 if (!login->auth_complete &&
Andy Grover60bfcf82013-10-09 11:05:58 -0700966 conn->tpg->tpg_attrib.authentication) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000967 pr_err("Initiator is requesting CSG: 1, has not been"
968 " successfully authenticated, and the Target is"
969 " enforcing iSCSI Authentication, login failed.\n");
970 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
971 ISCSI_LOGIN_STATUS_AUTH_FAILED);
972 return -1;
973 }
974
975 if (!iscsi_check_negotiated_keys(conn->param_list))
976 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
977 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
978 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
979 ISCSI_FLAG_LOGIN_TRANSIT;
980
981 return 0;
982}
983
984static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
985{
986 int pdu_count = 0;
987 struct iscsi_login_req *login_req;
988 struct iscsi_login_rsp *login_rsp;
989
990 login_req = (struct iscsi_login_req *) login->req;
991 login_rsp = (struct iscsi_login_rsp *) login->rsp;
992
993 while (1) {
994 if (++pdu_count > MAX_LOGIN_PDUS) {
995 pr_err("MAX_LOGIN_PDUS count reached.\n");
996 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
997 ISCSI_LOGIN_STATUS_TARGET_ERROR);
998 return -1;
999 }
1000
Andy Grover5d358062013-03-04 13:52:08 -08001001 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001002 case 0:
Andy Grover5d358062013-03-04 13:52:08 -08001003 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001004 if (iscsi_target_handle_csg_zero(conn, login) < 0)
1005 return -1;
1006 break;
1007 case 1:
1008 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
1009 if (iscsi_target_handle_csg_one(conn, login) < 0)
1010 return -1;
1011 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
Nicholas Bellingerbdabf092017-05-24 21:47:09 -07001012 /*
1013 * Check to make sure the TCP connection has not
1014 * dropped asynchronously while session reinstatement
1015 * was occuring in this kthread context, before
1016 * transitioning to full feature phase operation.
1017 */
1018 if (iscsi_target_sk_check_close(conn))
1019 return -1;
1020
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001021 login->tsih = conn->sess->tsih;
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001022 login->login_complete = 1;
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001023 iscsi_target_restore_sock_callbacks(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024 if (iscsi_target_do_tx_login_io(conn,
1025 login) < 0)
1026 return -1;
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001027 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001028 }
1029 break;
1030 default:
1031 pr_err("Illegal CSG: %d received from"
1032 " Initiator, protocol error.\n",
Andy Grover5d358062013-03-04 13:52:08 -08001033 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001034 break;
1035 }
1036
Nicholas Bellingerea3a1792013-09-05 14:55:37 -07001037 if (iscsi_target_do_tx_login_io(conn, login) < 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001038 return -1;
1039
1040 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1041 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1042 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1043 }
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001044 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001045 }
1046
1047 return 0;
1048}
1049
1050static void iscsi_initiatorname_tolower(
1051 char *param_buf)
1052{
1053 char *c;
1054 u32 iqn_size = strlen(param_buf), i;
1055
1056 for (i = 0; i < iqn_size; i++) {
Jörn Engel8359cf42011-11-24 02:05:51 +01001057 c = &param_buf[i];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001058 if (!isupper(*c))
1059 continue;
1060
1061 *c = tolower(*c);
1062 }
1063}
1064
1065/*
1066 * Processes the first Login Request..
1067 */
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001068int iscsi_target_locate_portal(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001069 struct iscsi_np *np,
1070 struct iscsi_conn *conn,
1071 struct iscsi_login *login)
1072{
1073 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1074 char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1075 struct iscsi_session *sess = conn->sess;
1076 struct iscsi_tiqn *tiqn;
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001077 struct iscsi_tpg_np *tpg_np = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 struct iscsi_login_req *login_req;
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001079 struct se_node_acl *se_nacl;
1080 u32 payload_length, queue_depth = 0;
1081 int sessiontype = 0, ret = 0, tag_num, tag_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001082
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001083 INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
Nicholas Bellingerbb048352013-09-05 14:54:04 -07001084 INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001085 iscsi_target_set_sock_callbacks(conn);
1086
1087 login->np = np;
1088
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001089 login_req = (struct iscsi_login_req *) login->req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001090 payload_length = ntoh24(login_req->dlength);
1091
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001092 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1093 if (!tmpbuf) {
1094 pr_err("Unable to allocate memory for tmpbuf.\n");
1095 return -1;
1096 }
1097
1098 memcpy(tmpbuf, login->req_buf, payload_length);
1099 tmpbuf[payload_length] = '\0';
1100 start = tmpbuf;
1101 end = (start + payload_length);
1102
1103 /*
1104 * Locate the initial keys expected from the Initiator node in
1105 * the first login request in order to progress with the login phase.
1106 */
1107 while (start < end) {
1108 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1109 ret = -1;
1110 goto out;
1111 }
1112
1113 if (!strncmp(key, "InitiatorName", 13))
1114 i_buf = value;
1115 else if (!strncmp(key, "SessionType", 11))
1116 s_buf = value;
1117 else if (!strncmp(key, "TargetName", 10))
1118 t_buf = value;
1119
1120 start += strlen(key) + strlen(value) + 2;
1121 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001122 /*
1123 * See 5.3. Login Phase.
1124 */
1125 if (!i_buf) {
1126 pr_err("InitiatorName key not received"
1127 " in first login request.\n");
1128 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1129 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1130 ret = -1;
1131 goto out;
1132 }
1133 /*
1134 * Convert the incoming InitiatorName to lowercase following
1135 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1136 * are NOT case sensitive.
1137 */
1138 iscsi_initiatorname_tolower(i_buf);
1139
1140 if (!s_buf) {
1141 if (!login->leading_connection)
1142 goto get_target;
1143
1144 pr_err("SessionType key not received"
1145 " in first login request.\n");
1146 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1147 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1148 ret = -1;
1149 goto out;
1150 }
1151
1152 /*
1153 * Use default portal group for discovery sessions.
1154 */
1155 sessiontype = strncmp(s_buf, DISCOVERY, 9);
1156 if (!sessiontype) {
1157 conn->tpg = iscsit_global->discovery_tpg;
1158 if (!login->leading_connection)
1159 goto get_target;
1160
1161 sess->sess_ops->SessionType = 1;
1162 /*
1163 * Setup crc32c modules from libcrypto
1164 */
1165 if (iscsi_login_setup_crypto(conn) < 0) {
1166 pr_err("iscsi_login_setup_crypto() failed\n");
1167 ret = -1;
1168 goto out;
1169 }
1170 /*
1171 * Serialize access across the discovery struct iscsi_portal_group to
1172 * process login attempt.
1173 */
1174 if (iscsit_access_np(np, conn->tpg) < 0) {
1175 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1176 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1177 ret = -1;
1178 goto out;
1179 }
1180 ret = 0;
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001181 goto alloc_tags;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001182 }
1183
1184get_target:
1185 if (!t_buf) {
1186 pr_err("TargetName key not received"
1187 " in first login request while"
1188 " SessionType=Normal.\n");
1189 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1190 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1191 ret = -1;
1192 goto out;
1193 }
1194
1195 /*
1196 * Locate Target IQN from Storage Node.
1197 */
1198 tiqn = iscsit_get_tiqn_for_login(t_buf);
1199 if (!tiqn) {
1200 pr_err("Unable to locate Target IQN: %s in"
1201 " Storage Node\n", t_buf);
1202 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1203 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1204 ret = -1;
1205 goto out;
1206 }
1207 pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1208
1209 /*
1210 * Locate Target Portal Group from Storage Node.
1211 */
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001212 conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001213 if (!conn->tpg) {
1214 pr_err("Unable to locate Target Portal Group"
1215 " on %s\n", tiqn->tiqn);
1216 iscsit_put_tiqn_for_login(tiqn);
1217 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1218 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1219 ret = -1;
1220 goto out;
1221 }
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001222 conn->tpg_np = tpg_np;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001223 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1224 /*
1225 * Setup crc32c modules from libcrypto
1226 */
1227 if (iscsi_login_setup_crypto(conn) < 0) {
1228 pr_err("iscsi_login_setup_crypto() failed\n");
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001229 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1230 iscsit_put_tiqn_for_login(tiqn);
1231 conn->tpg = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001232 ret = -1;
1233 goto out;
1234 }
1235 /*
1236 * Serialize access across the struct iscsi_portal_group to
1237 * process login attempt.
1238 */
1239 if (iscsit_access_np(np, conn->tpg) < 0) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001240 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001241 iscsit_put_tiqn_for_login(tiqn);
1242 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1243 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001244 conn->tpg = NULL;
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001245 ret = -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001246 goto out;
1247 }
1248
1249 /*
1250 * conn->sess->node_acl will be set when the referenced
1251 * struct iscsi_session is located from received ISID+TSIH in
1252 * iscsi_login_non_zero_tsih_s2().
1253 */
1254 if (!login->leading_connection) {
1255 ret = 0;
1256 goto out;
1257 }
1258
1259 /*
1260 * This value is required in iscsi_login_zero_tsih_s2()
1261 */
1262 sess->sess_ops->SessionType = 0;
1263
1264 /*
1265 * Locate incoming Initiator IQN reference from Storage Node.
1266 */
1267 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1268 &conn->tpg->tpg_se_tpg, i_buf);
1269 if (!sess->se_sess->se_node_acl) {
1270 pr_err("iSCSI Initiator Node: %s is not authorized to"
1271 " access iSCSI target portal group: %hu.\n",
1272 i_buf, conn->tpg->tpgt);
1273 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1274 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1275 ret = -1;
1276 goto out;
1277 }
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001278 se_nacl = sess->se_sess->se_node_acl;
1279 queue_depth = se_nacl->queue_depth;
1280 /*
1281 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1282 * depth for non immediate commands, plus extra tags for immediate
1283 * commands.
1284 *
1285 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1286 * in per-cpu-ida tag allocation logic + small queue_depth.
1287 */
1288alloc_tags:
1289 tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
Nicholas Bellinger4a4caa22014-01-10 02:06:59 +00001290 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001291 tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001292
Nicholas Bellinger988e3a82013-08-17 15:49:08 -07001293 ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1294 if (ret < 0) {
1295 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1296 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1297 ret = -1;
1298 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001299out:
1300 kfree(tmpbuf);
1301 return ret;
1302}
1303
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001304int iscsi_target_start_negotiation(
1305 struct iscsi_login *login,
1306 struct iscsi_conn *conn)
1307{
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001308 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001309
Nicholas Bellinger8f0dfb32016-02-27 18:15:46 -08001310 if (conn->sock) {
1311 struct sock *sk = conn->sock->sk;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001312
Nicholas Bellingerbdabf092017-05-24 21:47:09 -07001313 write_lock_bh(&sk->sk_callback_lock);
1314 set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1315 set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1316 write_unlock_bh(&sk->sk_callback_lock);
1317 }
1318 /*
1319 * If iscsi_target_do_login returns zero to signal more PDU
1320 * exchanges are required to complete the login, go ahead and
1321 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
1322 * is still active.
1323 *
1324 * Otherwise if TCP connection dropped asynchronously, go ahead
1325 * and perform connection cleanup now.
1326 */
1327 ret = iscsi_target_do_login(conn, login);
1328 if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
1329 ret = -1;
Nicholas Bellinger8f0dfb32016-02-27 18:15:46 -08001330
Nicholas Bellingerbdabf092017-05-24 21:47:09 -07001331 if (ret < 0) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001332 cancel_delayed_work_sync(&conn->login_work);
Nicholas Bellingerbb048352013-09-05 14:54:04 -07001333 cancel_delayed_work_sync(&conn->login_cleanup_work);
Nicholas Bellingerd381a802013-08-15 13:40:17 -07001334 iscsi_target_restore_sock_callbacks(conn);
1335 iscsi_remove_failed_auth_entry(conn);
1336 }
1337 if (ret != 0)
1338 iscsi_target_nego_release(conn);
1339
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001340 return ret;
1341}
1342
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001343void iscsi_target_nego_release(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001344{
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001345 struct iscsi_login *login = conn->conn_login;
1346
1347 if (!login)
1348 return;
1349
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001350 kfree(login->req_buf);
1351 kfree(login->rsp_buf);
1352 kfree(login);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001353
1354 conn->conn_login = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001355}