target: replace strict_strto*() with kstrto*()
[deliverable/linux.git] / drivers / target / iscsi / iscsi_target_login.c
CommitLineData
e48354ce
NB
1/*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
3 *
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5 *
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7 *
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
20
21#include <linux/string.h>
22#include <linux/kthread.h>
23#include <linux/crypto.h>
40401530 24#include <linux/idr.h>
e48354ce
NB
25#include <scsi/iscsi_proto.h>
26#include <target/target_core_base.h>
c4795fb2 27#include <target/target_core_fabric.h>
e48354ce
NB
28
29#include "iscsi_target_core.h"
30#include "iscsi_target_tq.h"
31#include "iscsi_target_device.h"
32#include "iscsi_target_nego.h"
33#include "iscsi_target_erl0.h"
34#include "iscsi_target_erl2.h"
35#include "iscsi_target_login.h"
36#include "iscsi_target_stat.h"
37#include "iscsi_target_tpg.h"
38#include "iscsi_target_util.h"
39#include "iscsi_target.h"
40#include "iscsi_target_parameters.h"
41
baa4d64b
NB
42#include <target/iscsi/iscsi_transport.h>
43
44static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
e48354ce 45{
baa4d64b
NB
46 struct iscsi_login *login;
47
48 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
49 if (!login) {
50 pr_err("Unable to allocate memory for struct iscsi_login.\n");
51 return NULL;
52 }
53 login->conn = conn;
54 login->first_request = 1;
55
56 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
57 if (!login->req_buf) {
58 pr_err("Unable to allocate memory for response buffer.\n");
59 goto out_login;
60 }
61
62 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
63 if (!login->rsp_buf) {
64 pr_err("Unable to allocate memory for request buffer.\n");
65 goto out_req_buf;
66 }
67
68 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
69 if (!conn->conn_ops) {
70 pr_err("Unable to allocate memory for"
71 " struct iscsi_conn_ops.\n");
72 goto out_rsp_buf;
73 }
74
d5627acb 75 init_waitqueue_head(&conn->queues_wq);
e48354ce
NB
76 INIT_LIST_HEAD(&conn->conn_list);
77 INIT_LIST_HEAD(&conn->conn_cmd_list);
78 INIT_LIST_HEAD(&conn->immed_queue_list);
79 INIT_LIST_HEAD(&conn->response_queue_list);
80 init_completion(&conn->conn_post_wait_comp);
81 init_completion(&conn->conn_wait_comp);
82 init_completion(&conn->conn_wait_rcfr_comp);
83 init_completion(&conn->conn_waiting_on_uc_comp);
84 init_completion(&conn->conn_logout_comp);
85 init_completion(&conn->rx_half_close_comp);
86 init_completion(&conn->tx_half_close_comp);
87 spin_lock_init(&conn->cmd_lock);
88 spin_lock_init(&conn->conn_usage_lock);
89 spin_lock_init(&conn->immed_queue_lock);
90 spin_lock_init(&conn->nopin_timer_lock);
91 spin_lock_init(&conn->response_queue_lock);
92 spin_lock_init(&conn->state_lock);
93
94 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
95 pr_err("Unable to allocate conn->conn_cpumask\n");
baa4d64b 96 goto out_conn_ops;
e48354ce 97 }
baa4d64b 98 conn->conn_login = login;
e48354ce 99
baa4d64b
NB
100 return login;
101
102out_conn_ops:
103 kfree(conn->conn_ops);
104out_rsp_buf:
105 kfree(login->rsp_buf);
106out_req_buf:
107 kfree(login->req_buf);
108out_login:
109 kfree(login);
110 return NULL;
e48354ce
NB
111}
112
113/*
114 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
115 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
116 */
117int iscsi_login_setup_crypto(struct iscsi_conn *conn)
118{
119 /*
120 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
121 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
122 * to software 1x8 byte slicing from crc32c.ko
123 */
124 conn->conn_rx_hash.flags = 0;
125 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
126 CRYPTO_ALG_ASYNC);
127 if (IS_ERR(conn->conn_rx_hash.tfm)) {
128 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
129 return -ENOMEM;
130 }
131
132 conn->conn_tx_hash.flags = 0;
133 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
134 CRYPTO_ALG_ASYNC);
135 if (IS_ERR(conn->conn_tx_hash.tfm)) {
136 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
137 crypto_free_hash(conn->conn_rx_hash.tfm);
138 return -ENOMEM;
139 }
140
141 return 0;
142}
143
144static int iscsi_login_check_initiator_version(
145 struct iscsi_conn *conn,
146 u8 version_max,
147 u8 version_min)
148{
149 if ((version_max != 0x00) || (version_min != 0x00)) {
150 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
151 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
152 version_min, version_max);
153 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
154 ISCSI_LOGIN_STATUS_NO_VERSION);
155 return -1;
156 }
157
158 return 0;
159}
160
161int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
162{
163 int sessiontype;
164 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
165 struct iscsi_portal_group *tpg = conn->tpg;
166 struct iscsi_session *sess = NULL, *sess_p = NULL;
167 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
168 struct se_session *se_sess, *se_sess_tmp;
169
170 initiatorname_param = iscsi_find_param_from_key(
171 INITIATORNAME, conn->param_list);
e48354ce
NB
172 sessiontype_param = iscsi_find_param_from_key(
173 SESSIONTYPE, conn->param_list);
1c5c12c6
RD
174 if (!initiatorname_param || !sessiontype_param) {
175 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
176 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
e48354ce 177 return -1;
1c5c12c6 178 }
e48354ce
NB
179
180 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
181
182 spin_lock_bh(&se_tpg->session_lock);
183 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
184 sess_list) {
185
8359cf43 186 sess_p = se_sess->fabric_sess_ptr;
e48354ce
NB
187 spin_lock(&sess_p->conn_lock);
188 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
189 atomic_read(&sess_p->session_logout) ||
190 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
191 spin_unlock(&sess_p->conn_lock);
192 continue;
193 }
8359cf43
JE
194 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
195 (!strcmp(sess_p->sess_ops->InitiatorName,
196 initiatorname_param->value) &&
e48354ce
NB
197 (sess_p->sess_ops->SessionType == sessiontype))) {
198 atomic_set(&sess_p->session_reinstatement, 1);
199 spin_unlock(&sess_p->conn_lock);
200 iscsit_inc_session_usage_count(sess_p);
201 iscsit_stop_time2retain_timer(sess_p);
202 sess = sess_p;
203 break;
204 }
205 spin_unlock(&sess_p->conn_lock);
206 }
207 spin_unlock_bh(&se_tpg->session_lock);
208 /*
209 * If the Time2Retain handler has expired, the session is already gone.
210 */
211 if (!sess)
212 return 0;
213
214 pr_debug("%s iSCSI Session SID %u is still active for %s,"
215 " preforming session reinstatement.\n", (sessiontype) ?
216 "Discovery" : "Normal", sess->sid,
217 sess->sess_ops->InitiatorName);
218
219 spin_lock_bh(&sess->conn_lock);
220 if (sess->session_state == TARG_SESS_STATE_FAILED) {
221 spin_unlock_bh(&sess->conn_lock);
222 iscsit_dec_session_usage_count(sess);
99367f01
NB
223 target_put_session(sess->se_sess);
224 return 0;
e48354ce
NB
225 }
226 spin_unlock_bh(&sess->conn_lock);
227
228 iscsit_stop_session(sess, 1, 1);
229 iscsit_dec_session_usage_count(sess);
230
99367f01
NB
231 target_put_session(sess->se_sess);
232 return 0;
e48354ce
NB
233}
234
235static void iscsi_login_set_conn_values(
236 struct iscsi_session *sess,
237 struct iscsi_conn *conn,
50e5c87d 238 __be16 cid)
e48354ce
NB
239{
240 conn->sess = sess;
50e5c87d 241 conn->cid = be16_to_cpu(cid);
e48354ce
NB
242 /*
243 * Generate a random Status sequence number (statsn) for the new
244 * iSCSI connection.
245 */
246 get_random_bytes(&conn->stat_sn, sizeof(u32));
247
248 mutex_lock(&auth_id_lock);
249 conn->auth_id = iscsit_global->auth_id++;
250 mutex_unlock(&auth_id_lock);
251}
252
253/*
254 * This is the leading connection of a new session,
255 * or session reinstatement.
256 */
257static int iscsi_login_zero_tsih_s1(
258 struct iscsi_conn *conn,
259 unsigned char *buf)
260{
261 struct iscsi_session *sess = NULL;
262 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
13b5533a 263 int ret;
e48354ce
NB
264
265 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
266 if (!sess) {
267 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
268 ISCSI_LOGIN_STATUS_NO_RESOURCES);
269 pr_err("Could not allocate memory for session\n");
0957627a 270 return -ENOMEM;
e48354ce
NB
271 }
272
273 iscsi_login_set_conn_values(sess, conn, pdu->cid);
274 sess->init_task_tag = pdu->itt;
8359cf43 275 memcpy(&sess->isid, pdu->isid, 6);
50e5c87d 276 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
e48354ce
NB
277 INIT_LIST_HEAD(&sess->sess_conn_list);
278 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
279 INIT_LIST_HEAD(&sess->cr_active_list);
280 INIT_LIST_HEAD(&sess->cr_inactive_list);
281 init_completion(&sess->async_msg_comp);
282 init_completion(&sess->reinstatement_comp);
283 init_completion(&sess->session_wait_comp);
284 init_completion(&sess->session_waiting_on_uc_comp);
285 mutex_init(&sess->cmdsn_mutex);
286 spin_lock_init(&sess->conn_lock);
287 spin_lock_init(&sess->cr_a_lock);
288 spin_lock_init(&sess->cr_i_lock);
289 spin_lock_init(&sess->session_usage_lock);
290 spin_lock_init(&sess->ttt_lock);
291
c9365bd0 292 idr_preload(GFP_KERNEL);
998866b0 293 spin_lock_bh(&sess_idr_lock);
c9365bd0
TH
294 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
295 if (ret >= 0)
296 sess->session_index = ret;
998866b0 297 spin_unlock_bh(&sess_idr_lock);
c9365bd0 298 idr_preload_end();
e48354ce 299
13b5533a 300 if (ret < 0) {
c9365bd0 301 pr_err("idr_alloc() for sess_idr failed\n");
13b5533a
BW
302 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
303 ISCSI_LOGIN_STATUS_NO_RESOURCES);
304 kfree(sess);
305 return -ENOMEM;
306 }
307
e48354ce
NB
308 sess->creation_time = get_jiffies_64();
309 spin_lock_init(&sess->session_stats_lock);
310 /*
311 * The FFP CmdSN window values will be allocated from the TPG's
312 * Initiator Node's ACL once the login has been successfully completed.
313 */
50e5c87d 314 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
e48354ce
NB
315
316 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
317 if (!sess->sess_ops) {
318 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
319 ISCSI_LOGIN_STATUS_NO_RESOURCES);
320 pr_err("Unable to allocate memory for"
321 " struct iscsi_sess_ops.\n");
0957627a
NB
322 kfree(sess);
323 return -ENOMEM;
e48354ce
NB
324 }
325
326 sess->se_sess = transport_init_session();
0957627a 327 if (IS_ERR(sess->se_sess)) {
e48354ce
NB
328 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
329 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0957627a
NB
330 kfree(sess);
331 return -ENOMEM;
e48354ce
NB
332 }
333
334 return 0;
335}
336
337static int iscsi_login_zero_tsih_s2(
338 struct iscsi_conn *conn)
339{
340 struct iscsi_node_attrib *na;
341 struct iscsi_session *sess = conn->sess;
342 unsigned char buf[32];
03aa2070 343 bool iser = false;
e48354ce
NB
344
345 sess->tpg = conn->tpg;
346
347 /*
348 * Assign a new TPG Session Handle. Note this is protected with
349 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
350 */
351 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
352 if (!sess->tsih)
353 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
354
355 /*
356 * Create the default params from user defined values..
357 */
358 if (iscsi_copy_param_list(&conn->param_list,
359 ISCSI_TPG_C(conn)->param_list, 1) < 0) {
360 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
361 ISCSI_LOGIN_STATUS_NO_RESOURCES);
362 return -1;
363 }
364
03aa2070
NB
365 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
366 iser = true;
367
368 iscsi_set_keys_to_negotiate(conn->param_list, iser);
e48354ce
NB
369
370 if (sess->sess_ops->SessionType)
371 return iscsi_set_keys_irrelevant_for_discovery(
372 conn->param_list);
373
374 na = iscsit_tpg_get_node_attrib(sess);
375
376 /*
377 * Need to send TargetPortalGroupTag back in first login response
378 * on any iSCSI connection where the Initiator provides TargetName.
379 * See 5.3.1. Login Phase Start
380 *
381 * In our case, we have already located the struct iscsi_tiqn at this point.
382 */
383 memset(buf, 0, 32);
384 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
385 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
386 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
387 ISCSI_LOGIN_STATUS_NO_RESOURCES);
388 return -1;
389 }
390
391 /*
392 * Workaround for Initiators that have broken connection recovery logic.
393 *
394 * "We would really like to get rid of this." Linux-iSCSI.org team
395 */
396 memset(buf, 0, 32);
397 sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
398 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
399 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
400 ISCSI_LOGIN_STATUS_NO_RESOURCES);
401 return -1;
402 }
403
404 if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
405 return -1;
03aa2070
NB
406 /*
407 * Set RDMAExtensions=Yes by default for iSER enabled network portals
408 */
409 if (iser) {
410 struct iscsi_param *param;
411 unsigned long mrdsl, off;
412 int rc;
413
414 sprintf(buf, "RDMAExtensions=Yes");
415 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
416 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
417 ISCSI_LOGIN_STATUS_NO_RESOURCES);
418 return -1;
419 }
420 /*
421 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
422 * Immediate Data + Unsolicitied Data-OUT if necessary..
423 */
424 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
425 conn->param_list);
426 if (!param) {
427 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
428 ISCSI_LOGIN_STATUS_NO_RESOURCES);
429 return -1;
430 }
57103d7f 431 rc = kstrtoul(param->value, 0, &mrdsl);
03aa2070
NB
432 if (rc < 0) {
433 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
434 ISCSI_LOGIN_STATUS_NO_RESOURCES);
435 return -1;
436 }
437 off = mrdsl % PAGE_SIZE;
438 if (!off)
439 return 0;
440
441 if (mrdsl < PAGE_SIZE)
442 mrdsl = PAGE_SIZE;
443 else
444 mrdsl -= off;
445
446 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
447 " to PAGE_SIZE\n", mrdsl);
448
449 sprintf(buf, "MaxRecvDataSegmentLength=%lu\n", mrdsl);
450 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
451 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
452 ISCSI_LOGIN_STATUS_NO_RESOURCES);
453 return -1;
454 }
455 }
e48354ce
NB
456
457 return 0;
458}
459
460/*
461 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
462 * The Initiator node will be able to enable FIM by proposing them itself.
463 */
464int iscsi_login_disable_FIM_keys(
465 struct iscsi_param_list *param_list,
466 struct iscsi_conn *conn)
467{
468 struct iscsi_param *param;
469
470 param = iscsi_find_param_from_key("OFMarker", param_list);
471 if (!param) {
472 pr_err("iscsi_find_param_from_key() for"
473 " OFMarker failed\n");
474 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
475 ISCSI_LOGIN_STATUS_NO_RESOURCES);
476 return -1;
477 }
478 param->state &= ~PSTATE_NEGOTIATE;
479
480 param = iscsi_find_param_from_key("OFMarkInt", param_list);
481 if (!param) {
482 pr_err("iscsi_find_param_from_key() for"
483 " IFMarker failed\n");
484 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
485 ISCSI_LOGIN_STATUS_NO_RESOURCES);
486 return -1;
487 }
488 param->state &= ~PSTATE_NEGOTIATE;
489
490 param = iscsi_find_param_from_key("IFMarker", param_list);
491 if (!param) {
492 pr_err("iscsi_find_param_from_key() for"
493 " IFMarker failed\n");
494 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
495 ISCSI_LOGIN_STATUS_NO_RESOURCES);
496 return -1;
497 }
498 param->state &= ~PSTATE_NEGOTIATE;
499
500 param = iscsi_find_param_from_key("IFMarkInt", param_list);
501 if (!param) {
502 pr_err("iscsi_find_param_from_key() for"
503 " IFMarker failed\n");
504 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
505 ISCSI_LOGIN_STATUS_NO_RESOURCES);
506 return -1;
507 }
508 param->state &= ~PSTATE_NEGOTIATE;
509
510 return 0;
511}
512
513static int iscsi_login_non_zero_tsih_s1(
514 struct iscsi_conn *conn,
515 unsigned char *buf)
516{
517 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
518
519 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
520 return 0;
521}
522
523/*
524 * Add a new connection to an existing session.
525 */
526static int iscsi_login_non_zero_tsih_s2(
527 struct iscsi_conn *conn,
528 unsigned char *buf)
529{
530 struct iscsi_portal_group *tpg = conn->tpg;
531 struct iscsi_session *sess = NULL, *sess_p = NULL;
532 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
533 struct se_session *se_sess, *se_sess_tmp;
534 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
03aa2070 535 bool iser = false;
e48354ce
NB
536
537 spin_lock_bh(&se_tpg->session_lock);
538 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
539 sess_list) {
540
541 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
542 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
543 atomic_read(&sess_p->session_logout) ||
544 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
545 continue;
8359cf43 546 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
50e5c87d 547 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
e48354ce
NB
548 iscsit_inc_session_usage_count(sess_p);
549 iscsit_stop_time2retain_timer(sess_p);
550 sess = sess_p;
551 break;
552 }
553 }
554 spin_unlock_bh(&se_tpg->session_lock);
555
556 /*
557 * If the Time2Retain handler has expired, the session is already gone.
558 */
559 if (!sess) {
560 pr_err("Initiator attempting to add a connection to"
561 " a non-existent session, rejecting iSCSI Login.\n");
562 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
563 ISCSI_LOGIN_STATUS_NO_SESSION);
564 return -1;
565 }
566
567 /*
568 * Stop the Time2Retain timer if this is a failed session, we restart
569 * the timer if the login is not successful.
570 */
571 spin_lock_bh(&sess->conn_lock);
572 if (sess->session_state == TARG_SESS_STATE_FAILED)
573 atomic_set(&sess->session_continuation, 1);
574 spin_unlock_bh(&sess->conn_lock);
575
576 iscsi_login_set_conn_values(sess, conn, pdu->cid);
577
578 if (iscsi_copy_param_list(&conn->param_list,
579 ISCSI_TPG_C(conn)->param_list, 0) < 0) {
580 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
581 ISCSI_LOGIN_STATUS_NO_RESOURCES);
582 return -1;
583 }
584
03aa2070
NB
585 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
586 iser = true;
587
588 iscsi_set_keys_to_negotiate(conn->param_list, iser);
e48354ce
NB
589 /*
590 * Need to send TargetPortalGroupTag back in first login response
591 * on any iSCSI connection where the Initiator provides TargetName.
592 * See 5.3.1. Login Phase Start
593 *
594 * In our case, we have already located the struct iscsi_tiqn at this point.
595 */
596 memset(buf, 0, 32);
597 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
598 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
599 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
600 ISCSI_LOGIN_STATUS_NO_RESOURCES);
601 return -1;
602 }
603
604 return iscsi_login_disable_FIM_keys(conn->param_list, conn);
605}
606
607int iscsi_login_post_auth_non_zero_tsih(
608 struct iscsi_conn *conn,
609 u16 cid,
610 u32 exp_statsn)
611{
612 struct iscsi_conn *conn_ptr = NULL;
613 struct iscsi_conn_recovery *cr = NULL;
614 struct iscsi_session *sess = conn->sess;
615
616 /*
617 * By following item 5 in the login table, if we have found
618 * an existing ISID and a valid/existing TSIH and an existing
619 * CID we do connection reinstatement. Currently we dont not
620 * support it so we send back an non-zero status class to the
621 * initiator and release the new connection.
622 */
623 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
ee1b1b9c 624 if (conn_ptr) {
e48354ce
NB
625 pr_err("Connection exists with CID %hu for %s,"
626 " performing connection reinstatement.\n",
627 conn_ptr->cid, sess->sess_ops->InitiatorName);
628
629 iscsit_connection_reinstatement_rcfr(conn_ptr);
630 iscsit_dec_conn_usage_count(conn_ptr);
631 }
632
633 /*
634 * Check for any connection recovery entires containing CID.
635 * We use the original ExpStatSN sent in the first login request
636 * to acknowledge commands for the failed connection.
637 *
638 * Also note that an explict logout may have already been sent,
639 * but the response may not be sent due to additional connection
640 * loss.
641 */
642 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
643 cr = iscsit_get_inactive_connection_recovery_entry(
644 sess, cid);
ee1b1b9c 645 if (cr) {
e48354ce
NB
646 pr_debug("Performing implicit logout"
647 " for connection recovery on CID: %hu\n",
648 conn->cid);
649 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
650 }
651 }
652
653 /*
654 * Else we follow item 4 from the login table in that we have
655 * found an existing ISID and a valid/existing TSIH and a new
656 * CID we go ahead and continue to add a new connection to the
657 * session.
658 */
659 pr_debug("Adding CID %hu to existing session for %s.\n",
660 cid, sess->sess_ops->InitiatorName);
661
662 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
663 pr_err("Adding additional connection to this session"
664 " would exceed MaxConnections %d, login failed.\n",
665 sess->sess_ops->MaxConnections);
666 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
667 ISCSI_LOGIN_STATUS_ISID_ERROR);
668 return -1;
669 }
670
671 return 0;
672}
673
674static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
675{
676 struct iscsi_session *sess = conn->sess;
03aa2070
NB
677 /*
678 * FIXME: Unsolicitied NopIN support for ISER
679 */
680 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
681 return;
e48354ce
NB
682
683 if (!sess->sess_ops->SessionType)
684 iscsit_start_nopin_timer(conn);
685}
686
687static int iscsi_post_login_handler(
688 struct iscsi_np *np,
689 struct iscsi_conn *conn,
690 u8 zero_tsih)
691{
692 int stop_timer = 0;
693 struct iscsi_session *sess = conn->sess;
694 struct se_session *se_sess = sess->se_sess;
695 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
696 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
697 struct iscsi_thread_set *ts;
698
699 iscsit_inc_conn_usage_count(conn);
700
701 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
702 ISCSI_LOGIN_STATUS_ACCEPT);
703
704 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
705 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
706
707 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
708 iscsit_set_sync_and_steering_values(conn);
709 /*
710 * SCSI Initiator -> SCSI Target Port Mapping
711 */
712 ts = iscsi_get_thread_set();
713 if (!zero_tsih) {
714 iscsi_set_session_parameters(sess->sess_ops,
715 conn->param_list, 0);
716 iscsi_release_param_list(conn->param_list);
717 conn->param_list = NULL;
718
719 spin_lock_bh(&sess->conn_lock);
720 atomic_set(&sess->session_continuation, 0);
721 if (sess->session_state == TARG_SESS_STATE_FAILED) {
722 pr_debug("Moving to"
723 " TARG_SESS_STATE_LOGGED_IN.\n");
724 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
725 stop_timer = 1;
726 }
727
728 pr_debug("iSCSI Login successful on CID: %hu from %s to"
2f9bc894
NB
729 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
730 conn->local_ip, conn->local_port, tpg->tpgt);
e48354ce
NB
731
732 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
733 atomic_inc(&sess->nconn);
734 pr_debug("Incremented iSCSI Connection count to %hu"
735 " from node: %s\n", atomic_read(&sess->nconn),
736 sess->sess_ops->InitiatorName);
737 spin_unlock_bh(&sess->conn_lock);
738
739 iscsi_post_login_start_timers(conn);
baa4d64b 740
03aa2070 741 iscsi_activate_thread_set(conn, ts);
e48354ce
NB
742 /*
743 * Determine CPU mask to ensure connection's RX and TX kthreads
744 * are scheduled on the same CPU.
745 */
746 iscsit_thread_get_cpumask(conn);
747 conn->conn_rx_reset_cpumask = 1;
748 conn->conn_tx_reset_cpumask = 1;
749
750 iscsit_dec_conn_usage_count(conn);
751 if (stop_timer) {
752 spin_lock_bh(&se_tpg->session_lock);
753 iscsit_stop_time2retain_timer(sess);
754 spin_unlock_bh(&se_tpg->session_lock);
755 }
756 iscsit_dec_session_usage_count(sess);
757 return 0;
758 }
759
760 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
761 iscsi_release_param_list(conn->param_list);
762 conn->param_list = NULL;
763
764 iscsit_determine_maxcmdsn(sess);
765
766 spin_lock_bh(&se_tpg->session_lock);
767 __transport_register_session(&sess->tpg->tpg_se_tpg,
8359cf43 768 se_sess->se_node_acl, se_sess, sess);
e48354ce
NB
769 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
770 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
771
772 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
2f9bc894
NB
773 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
774 tpg->tpgt);
e48354ce
NB
775
776 spin_lock_bh(&sess->conn_lock);
777 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
778 atomic_inc(&sess->nconn);
779 pr_debug("Incremented iSCSI Connection count to %hu from node:"
780 " %s\n", atomic_read(&sess->nconn),
781 sess->sess_ops->InitiatorName);
782 spin_unlock_bh(&sess->conn_lock);
783
784 sess->sid = tpg->sid++;
785 if (!sess->sid)
786 sess->sid = tpg->sid++;
787 pr_debug("Established iSCSI session from node: %s\n",
788 sess->sess_ops->InitiatorName);
789
790 tpg->nsessions++;
791 if (tpg->tpg_tiqn)
792 tpg->tpg_tiqn->tiqn_nsessions++;
793
794 pr_debug("Incremented number of active iSCSI sessions to %u on"
795 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
796 spin_unlock_bh(&se_tpg->session_lock);
797
798 iscsi_post_login_start_timers(conn);
799 iscsi_activate_thread_set(conn, ts);
800 /*
801 * Determine CPU mask to ensure connection's RX and TX kthreads
802 * are scheduled on the same CPU.
803 */
804 iscsit_thread_get_cpumask(conn);
805 conn->conn_rx_reset_cpumask = 1;
806 conn->conn_tx_reset_cpumask = 1;
807
808 iscsit_dec_conn_usage_count(conn);
809
810 return 0;
811}
812
813static void iscsi_handle_login_thread_timeout(unsigned long data)
814{
815 struct iscsi_np *np = (struct iscsi_np *) data;
816
817 spin_lock_bh(&np->np_thread_lock);
818 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
819 np->np_ip, np->np_port);
820
821 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
822 spin_unlock_bh(&np->np_thread_lock);
823 return;
824 }
825
826 if (np->np_thread)
827 send_sig(SIGINT, np->np_thread, 1);
828
829 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
830 spin_unlock_bh(&np->np_thread_lock);
831}
832
833static void iscsi_start_login_thread_timer(struct iscsi_np *np)
834{
835 /*
836 * This used the TA_LOGIN_TIMEOUT constant because at this
837 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
838 */
839 spin_lock_bh(&np->np_thread_lock);
840 init_timer(&np->np_login_timer);
841 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
842 np->np_login_timer.data = (unsigned long)np;
843 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
844 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
845 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
846 add_timer(&np->np_login_timer);
847
848 pr_debug("Added timeout timer to iSCSI login request for"
849 " %u seconds.\n", TA_LOGIN_TIMEOUT);
850 spin_unlock_bh(&np->np_thread_lock);
851}
852
853static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
854{
855 spin_lock_bh(&np->np_thread_lock);
856 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
857 spin_unlock_bh(&np->np_thread_lock);
858 return;
859 }
860 np->np_login_timer_flags |= ISCSI_TF_STOP;
861 spin_unlock_bh(&np->np_thread_lock);
862
863 del_timer_sync(&np->np_login_timer);
864
865 spin_lock_bh(&np->np_thread_lock);
866 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
867 spin_unlock_bh(&np->np_thread_lock);
868}
869
baa4d64b 870int iscsit_setup_np(
e48354ce
NB
871 struct iscsi_np *np,
872 struct __kernel_sockaddr_storage *sockaddr)
873{
baa4d64b 874 struct socket *sock = NULL;
e48354ce
NB
875 int backlog = 5, ret, opt = 0, len;
876
877 switch (np->np_network_transport) {
878 case ISCSI_TCP:
879 np->np_ip_proto = IPPROTO_TCP;
880 np->np_sock_type = SOCK_STREAM;
881 break;
882 case ISCSI_SCTP_TCP:
883 np->np_ip_proto = IPPROTO_SCTP;
884 np->np_sock_type = SOCK_STREAM;
885 break;
886 case ISCSI_SCTP_UDP:
887 np->np_ip_proto = IPPROTO_SCTP;
888 np->np_sock_type = SOCK_SEQPACKET;
889 break;
e48354ce
NB
890 default:
891 pr_err("Unsupported network_transport: %d\n",
892 np->np_network_transport);
893 return -EINVAL;
894 }
895
baa4d64b
NB
896 np->np_ip_proto = IPPROTO_TCP;
897 np->np_sock_type = SOCK_STREAM;
898
e48354ce
NB
899 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
900 np->np_ip_proto, &sock);
901 if (ret < 0) {
902 pr_err("sock_create() failed.\n");
903 return ret;
904 }
905 np->np_socket = sock;
e48354ce
NB
906 /*
907 * Setup the np->np_sockaddr from the passed sockaddr setup
908 * in iscsi_target_configfs.c code..
909 */
8359cf43 910 memcpy(&np->np_sockaddr, sockaddr,
e48354ce
NB
911 sizeof(struct __kernel_sockaddr_storage));
912
913 if (sockaddr->ss_family == AF_INET6)
914 len = sizeof(struct sockaddr_in6);
915 else
916 len = sizeof(struct sockaddr_in);
917 /*
918 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
919 */
8359cf43 920 /* FIXME: Someone please explain why this is endian-safe */
e48354ce
NB
921 opt = 1;
922 if (np->np_network_transport == ISCSI_TCP) {
923 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
924 (char *)&opt, sizeof(opt));
925 if (ret < 0) {
926 pr_err("kernel_setsockopt() for TCP_NODELAY"
927 " failed: %d\n", ret);
928 goto fail;
929 }
930 }
931
8359cf43 932 /* FIXME: Someone please explain why this is endian-safe */
e48354ce
NB
933 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
934 (char *)&opt, sizeof(opt));
935 if (ret < 0) {
936 pr_err("kernel_setsockopt() for SO_REUSEADDR"
937 " failed\n");
938 goto fail;
939 }
940
9f9ef6d3
DK
941 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
942 (char *)&opt, sizeof(opt));
943 if (ret < 0) {
944 pr_err("kernel_setsockopt() for IP_FREEBIND"
945 " failed\n");
946 goto fail;
947 }
948
e48354ce
NB
949 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
950 if (ret < 0) {
951 pr_err("kernel_bind() failed: %d\n", ret);
952 goto fail;
953 }
954
955 ret = kernel_listen(sock, backlog);
956 if (ret != 0) {
957 pr_err("kernel_listen() failed: %d\n", ret);
958 goto fail;
959 }
960
961 return 0;
e48354ce
NB
962fail:
963 np->np_socket = NULL;
bf6932f4 964 if (sock)
e48354ce 965 sock_release(sock);
e48354ce
NB
966 return ret;
967}
968
baa4d64b
NB
969int iscsi_target_setup_login_socket(
970 struct iscsi_np *np,
971 struct __kernel_sockaddr_storage *sockaddr)
972{
973 struct iscsit_transport *t;
974 int rc;
975
976 t = iscsit_get_transport(np->np_network_transport);
977 if (!t)
978 return -EINVAL;
979
980 rc = t->iscsit_setup_np(np, sockaddr);
981 if (rc < 0) {
982 iscsit_put_transport(t);
983 return rc;
984 }
985
986 np->np_transport = t;
baa4d64b
NB
987 return 0;
988}
989
990int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
991{
992 struct socket *new_sock, *sock = np->np_socket;
993 struct sockaddr_in sock_in;
994 struct sockaddr_in6 sock_in6;
995 int rc, err;
996
997 rc = kernel_accept(sock, &new_sock, 0);
998 if (rc < 0)
999 return rc;
1000
1001 conn->sock = new_sock;
1002 conn->login_family = np->np_sockaddr.ss_family;
baa4d64b
NB
1003
1004 if (np->np_sockaddr.ss_family == AF_INET6) {
1005 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1006
1007 rc = conn->sock->ops->getname(conn->sock,
1008 (struct sockaddr *)&sock_in6, &err, 1);
1009 if (!rc) {
1010 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
1011 &sock_in6.sin6_addr.in6_u);
1012 conn->login_port = ntohs(sock_in6.sin6_port);
1013 }
1014
1015 rc = conn->sock->ops->getname(conn->sock,
1016 (struct sockaddr *)&sock_in6, &err, 0);
1017 if (!rc) {
1018 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
1019 &sock_in6.sin6_addr.in6_u);
1020 conn->local_port = ntohs(sock_in6.sin6_port);
1021 }
1022 } else {
1023 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1024
1025 rc = conn->sock->ops->getname(conn->sock,
1026 (struct sockaddr *)&sock_in, &err, 1);
1027 if (!rc) {
1028 sprintf(conn->login_ip, "%pI4",
1029 &sock_in.sin_addr.s_addr);
1030 conn->login_port = ntohs(sock_in.sin_port);
1031 }
1032
1033 rc = conn->sock->ops->getname(conn->sock,
1034 (struct sockaddr *)&sock_in, &err, 0);
1035 if (!rc) {
1036 sprintf(conn->local_ip, "%pI4",
1037 &sock_in.sin_addr.s_addr);
1038 conn->local_port = ntohs(sock_in.sin_port);
1039 }
1040 }
1041
1042 return 0;
1043}
1044
1045int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1046{
1047 struct iscsi_login_req *login_req;
1048 u32 padding = 0, payload_length;
1049
1050 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1051 return -1;
1052
1053 login_req = (struct iscsi_login_req *)login->req;
1054 payload_length = ntoh24(login_req->dlength);
1055 padding = ((-payload_length) & 3);
1056
1057 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1058 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1059 login_req->flags, login_req->itt, login_req->cmdsn,
1060 login_req->exp_statsn, login_req->cid, payload_length);
1061 /*
1062 * Setup the initial iscsi_login values from the leading
1063 * login request PDU.
1064 */
1065 if (login->first_request) {
1066 login_req = (struct iscsi_login_req *)login->req;
1067 login->leading_connection = (!login_req->tsih) ? 1 : 0;
3e1c81a9 1068 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
baa4d64b
NB
1069 login->version_min = login_req->min_version;
1070 login->version_max = login_req->max_version;
1071 memcpy(login->isid, login_req->isid, 6);
1072 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1073 login->init_task_tag = login_req->itt;
1074 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1075 login->cid = be16_to_cpu(login_req->cid);
1076 login->tsih = be16_to_cpu(login_req->tsih);
1077 }
1078
1079 if (iscsi_target_check_login_request(conn, login) < 0)
1080 return -1;
1081
1082 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1083 if (iscsi_login_rx_data(conn, login->req_buf,
1084 payload_length + padding) < 0)
1085 return -1;
1086
1087 return 0;
1088}
1089
1090int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1091 u32 length)
1092{
1093 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1094 return -1;
1095
1096 return 0;
1097}
1098
1099static int
1100iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1101{
1102 int rc;
1103
1104 if (!t->owner) {
1105 conn->conn_transport = t;
1106 return 0;
1107 }
1108
1109 rc = try_module_get(t->owner);
1110 if (!rc) {
1111 pr_err("try_module_get() failed for %s\n", t->name);
1112 return -EINVAL;
1113 }
1114
1115 conn->conn_transport = t;
1116 return 0;
1117}
1118
e48354ce
NB
1119static int __iscsi_target_login_thread(struct iscsi_np *np)
1120{
baa4d64b
NB
1121 u8 *buffer, zero_tsih = 0;
1122 int ret = 0, rc, stop;
e48354ce
NB
1123 struct iscsi_conn *conn = NULL;
1124 struct iscsi_login *login;
1125 struct iscsi_portal_group *tpg = NULL;
e48354ce 1126 struct iscsi_login_req *pdu;
e48354ce
NB
1127
1128 flush_signals(current);
e48354ce
NB
1129
1130 spin_lock_bh(&np->np_thread_lock);
1131 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1132 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1133 complete(&np->np_restart_comp);
1134 } else {
1135 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1136 }
1137 spin_unlock_bh(&np->np_thread_lock);
1138
e48354ce
NB
1139 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1140 if (!conn) {
1141 pr_err("Could not allocate memory for"
1142 " new connection\n");
e48354ce
NB
1143 /* Get another socket */
1144 return 1;
1145 }
e48354ce
NB
1146 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1147 conn->conn_state = TARG_CONN_STATE_FREE;
e48354ce 1148
baa4d64b
NB
1149 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1150 kfree(conn);
1151 return 1;
1152 }
e48354ce 1153
baa4d64b
NB
1154 rc = np->np_transport->iscsit_accept_np(np, conn);
1155 if (rc == -ENOSYS) {
1156 complete(&np->np_restart_comp);
1157 iscsit_put_transport(conn->conn_transport);
1158 kfree(conn);
1159 conn = NULL;
1160 goto exit;
1161 } else if (rc < 0) {
1162 spin_lock_bh(&np->np_thread_lock);
1163 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1164 spin_unlock_bh(&np->np_thread_lock);
1165 complete(&np->np_restart_comp);
1166 if (ret == -ENODEV) {
1167 iscsit_put_transport(conn->conn_transport);
1168 kfree(conn);
1169 conn = NULL;
1170 goto out;
1171 }
1172 /* Get another socket */
1173 return 1;
1174 }
1175 spin_unlock_bh(&np->np_thread_lock);
1176 iscsit_put_transport(conn->conn_transport);
1177 kfree(conn);
1178 conn = NULL;
1179 goto out;
e48354ce
NB
1180 }
1181 /*
1182 * Perform the remaining iSCSI connection initialization items..
1183 */
baa4d64b
NB
1184 login = iscsi_login_init_conn(conn);
1185 if (!login) {
e48354ce
NB
1186 goto new_sess_out;
1187 }
1188
baa4d64b 1189 iscsi_start_login_thread_timer(np);
e48354ce 1190
baa4d64b
NB
1191 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1192 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1193 /*
1194 * This will process the first login request + payload..
1195 */
1196 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1197 if (rc == 1)
1198 return 1;
1199 else if (rc < 0)
1200 goto new_sess_out;
66c7db68 1201
baa4d64b
NB
1202 buffer = &login->req[0];
1203 pdu = (struct iscsi_login_req *)buffer;
e48354ce
NB
1204 /*
1205 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1206 * when Status-Class != 0.
1207 */
baa4d64b 1208 conn->login_itt = pdu->itt;
e48354ce
NB
1209
1210 spin_lock_bh(&np->np_thread_lock);
1211 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1212 spin_unlock_bh(&np->np_thread_lock);
1213 pr_err("iSCSI Network Portal on %s:%hu currently not"
1214 " active.\n", np->np_ip, np->np_port);
1215 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1216 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1217 goto new_sess_out;
1218 }
1219 spin_unlock_bh(&np->np_thread_lock);
1220
e48354ce
NB
1221 conn->network_transport = np->np_network_transport;
1222
1223 pr_debug("Received iSCSI login request from %s on %s Network"
baa4d64b
NB
1224 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1225 conn->local_ip, conn->local_port);
e48354ce
NB
1226
1227 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1228 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1229
1230 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1231 pdu->min_version) < 0)
1232 goto new_sess_out;
1233
1234 zero_tsih = (pdu->tsih == 0x0000);
ee1b1b9c 1235 if (zero_tsih) {
e48354ce
NB
1236 /*
1237 * This is the leading connection of a new session.
1238 * We wait until after authentication to check for
1239 * session reinstatement.
1240 */
1241 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1242 goto new_sess_out;
1243 } else {
1244 /*
1245 * Add a new connection to an existing session.
1246 * We check for a non-existant session in
1247 * iscsi_login_non_zero_tsih_s2() below based
1248 * on ISID/TSIH, but wait until after authentication
1249 * to check for connection reinstatement, etc.
1250 */
1251 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1252 goto new_sess_out;
1253 }
e48354ce 1254 /*
baa4d64b
NB
1255 * SessionType: Discovery
1256 *
1257 * Locates Default Portal
1258 *
1259 * SessionType: Normal
1260 *
1261 * Locates Target Portal from NP -> Target IQN
e48354ce 1262 */
baa4d64b
NB
1263 rc = iscsi_target_locate_portal(np, conn, login);
1264 if (rc < 0) {
e48354ce
NB
1265 tpg = conn->tpg;
1266 goto new_sess_out;
1267 }
1268
1269 tpg = conn->tpg;
1270 if (!tpg) {
1271 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1272 goto new_sess_out;
1273 }
1274
1275 if (zero_tsih) {
baa4d64b 1276 if (iscsi_login_zero_tsih_s2(conn) < 0)
e48354ce 1277 goto new_sess_out;
e48354ce 1278 } else {
baa4d64b 1279 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
e48354ce 1280 goto old_sess_out;
e48354ce
NB
1281 }
1282
1283 if (iscsi_target_start_negotiation(login, conn) < 0)
1284 goto new_sess_out;
1285
1286 if (!conn->sess) {
1287 pr_err("struct iscsi_conn session pointer is NULL!\n");
1288 goto new_sess_out;
1289 }
1290
1291 iscsi_stop_login_thread_timer(np);
1292
1293 if (signal_pending(current))
1294 goto new_sess_out;
1295
1296 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1297
1298 if (ret < 0)
1299 goto new_sess_out;
1300
1301 iscsit_deaccess_np(np, tpg);
1302 tpg = NULL;
1303 /* Get another socket */
1304 return 1;
1305
1306new_sess_out:
1307 pr_err("iSCSI Login negotiation failed.\n");
1308 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1309 ISCSI_LOGIN_STATUS_INIT_ERR);
1310 if (!zero_tsih || !conn->sess)
1311 goto old_sess_out;
1312 if (conn->sess->se_sess)
1313 transport_free_session(conn->sess->se_sess);
1314 if (conn->sess->session_index != 0) {
1315 spin_lock_bh(&sess_idr_lock);
1316 idr_remove(&sess_idr, conn->sess->session_index);
1317 spin_unlock_bh(&sess_idr_lock);
1318 }
b07c28aa
SK
1319 kfree(conn->sess->sess_ops);
1320 kfree(conn->sess);
e48354ce
NB
1321old_sess_out:
1322 iscsi_stop_login_thread_timer(np);
1323 /*
1324 * If login negotiation fails check if the Time2Retain timer
1325 * needs to be restarted.
1326 */
1327 if (!zero_tsih && conn->sess) {
1328 spin_lock_bh(&conn->sess->conn_lock);
1329 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1330 struct se_portal_group *se_tpg =
1331 &ISCSI_TPG_C(conn)->tpg_se_tpg;
1332
1333 atomic_set(&conn->sess->session_continuation, 0);
1334 spin_unlock_bh(&conn->sess->conn_lock);
1335 spin_lock_bh(&se_tpg->session_lock);
1336 iscsit_start_time2retain_handler(conn->sess);
1337 spin_unlock_bh(&se_tpg->session_lock);
1338 } else
1339 spin_unlock_bh(&conn->sess->conn_lock);
1340 iscsit_dec_session_usage_count(conn->sess);
1341 }
1342
1343 if (!IS_ERR(conn->conn_rx_hash.tfm))
1344 crypto_free_hash(conn->conn_rx_hash.tfm);
1345 if (!IS_ERR(conn->conn_tx_hash.tfm))
1346 crypto_free_hash(conn->conn_tx_hash.tfm);
1347
1348 if (conn->conn_cpumask)
1349 free_cpumask_var(conn->conn_cpumask);
1350
1351 kfree(conn->conn_ops);
1352
1353 if (conn->param_list) {
1354 iscsi_release_param_list(conn->param_list);
1355 conn->param_list = NULL;
1356 }
baa4d64b
NB
1357 iscsi_target_nego_release(conn);
1358
1359 if (conn->sock) {
e48354ce 1360 sock_release(conn->sock);
baa4d64b
NB
1361 conn->sock = NULL;
1362 }
1363
1364 if (conn->conn_transport->iscsit_free_conn)
1365 conn->conn_transport->iscsit_free_conn(conn);
1366
1367 iscsit_put_transport(conn->conn_transport);
1368
e48354ce
NB
1369 kfree(conn);
1370
1371 if (tpg) {
1372 iscsit_deaccess_np(np, tpg);
1373 tpg = NULL;
1374 }
1375
1376out:
1377 stop = kthread_should_stop();
1378 if (!stop && signal_pending(current)) {
1379 spin_lock_bh(&np->np_thread_lock);
1380 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1381 spin_unlock_bh(&np->np_thread_lock);
1382 }
1383 /* Wait for another socket.. */
1384 if (!stop)
1385 return 1;
baa4d64b 1386exit:
e48354ce
NB
1387 iscsi_stop_login_thread_timer(np);
1388 spin_lock_bh(&np->np_thread_lock);
1389 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
baa4d64b 1390 np->np_thread = NULL;
e48354ce 1391 spin_unlock_bh(&np->np_thread_lock);
baa4d64b 1392
e48354ce
NB
1393 return 0;
1394}
1395
1396int iscsi_target_login_thread(void *arg)
1397{
8359cf43 1398 struct iscsi_np *np = arg;
e48354ce
NB
1399 int ret;
1400
1401 allow_signal(SIGINT);
1402
1403 while (!kthread_should_stop()) {
1404 ret = __iscsi_target_login_thread(np);
1405 /*
1406 * We break and exit here unless another sock_accept() call
1407 * is expected.
1408 */
1409 if (ret != 1)
1410 break;
1411 }
1412
1413 return 0;
1414}
This page took 0.166954 seconds and 5 git commands to generate.