]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/target/iscsi/iscsi_target_login.c
Target/iser: Fix iscsit_accept_np and rdma_cm racy flow
[mirror_ubuntu-zesty-kernel.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 *
4c76251e 4 * (c) Copyright 2007-2013 Datera, Inc.
e48354ce
NB
5 *
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
18
19#include <linux/string.h>
20#include <linux/kthread.h>
21#include <linux/crypto.h>
40401530 22#include <linux/idr.h>
e48354ce
NB
23#include <scsi/iscsi_proto.h>
24#include <target/target_core_base.h>
c4795fb2 25#include <target/target_core_fabric.h>
e48354ce
NB
26
27#include "iscsi_target_core.h"
28#include "iscsi_target_tq.h"
29#include "iscsi_target_device.h"
30#include "iscsi_target_nego.h"
31#include "iscsi_target_erl0.h"
32#include "iscsi_target_erl2.h"
33#include "iscsi_target_login.h"
34#include "iscsi_target_stat.h"
35#include "iscsi_target_tpg.h"
36#include "iscsi_target_util.h"
37#include "iscsi_target.h"
38#include "iscsi_target_parameters.h"
39
baa4d64b
NB
40#include <target/iscsi/iscsi_transport.h>
41
42static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
e48354ce 43{
baa4d64b
NB
44 struct iscsi_login *login;
45
46 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
47 if (!login) {
48 pr_err("Unable to allocate memory for struct iscsi_login.\n");
49 return NULL;
50 }
a91eb7d9 51 conn->login = login;
baa4d64b
NB
52 login->conn = conn;
53 login->first_request = 1;
54
55 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
56 if (!login->req_buf) {
57 pr_err("Unable to allocate memory for response buffer.\n");
58 goto out_login;
59 }
60
61 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
62 if (!login->rsp_buf) {
63 pr_err("Unable to allocate memory for request buffer.\n");
64 goto out_req_buf;
65 }
66
67 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
68 if (!conn->conn_ops) {
69 pr_err("Unable to allocate memory for"
70 " struct iscsi_conn_ops.\n");
71 goto out_rsp_buf;
72 }
73
d5627acb 74 init_waitqueue_head(&conn->queues_wq);
e48354ce
NB
75 INIT_LIST_HEAD(&conn->conn_list);
76 INIT_LIST_HEAD(&conn->conn_cmd_list);
77 INIT_LIST_HEAD(&conn->immed_queue_list);
78 INIT_LIST_HEAD(&conn->response_queue_list);
79 init_completion(&conn->conn_post_wait_comp);
80 init_completion(&conn->conn_wait_comp);
81 init_completion(&conn->conn_wait_rcfr_comp);
82 init_completion(&conn->conn_waiting_on_uc_comp);
83 init_completion(&conn->conn_logout_comp);
84 init_completion(&conn->rx_half_close_comp);
85 init_completion(&conn->tx_half_close_comp);
86 spin_lock_init(&conn->cmd_lock);
87 spin_lock_init(&conn->conn_usage_lock);
88 spin_lock_init(&conn->immed_queue_lock);
89 spin_lock_init(&conn->nopin_timer_lock);
90 spin_lock_init(&conn->response_queue_lock);
91 spin_lock_init(&conn->state_lock);
92
93 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
94 pr_err("Unable to allocate conn->conn_cpumask\n");
baa4d64b 95 goto out_conn_ops;
e48354ce 96 }
baa4d64b 97 conn->conn_login = login;
e48354ce 98
baa4d64b
NB
99 return login;
100
101out_conn_ops:
102 kfree(conn->conn_ops);
103out_rsp_buf:
104 kfree(login->rsp_buf);
105out_req_buf:
106 kfree(login->req_buf);
107out_login:
108 kfree(login);
109 return NULL;
e48354ce
NB
110}
111
112/*
113 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
114 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
115 */
116int iscsi_login_setup_crypto(struct iscsi_conn *conn)
117{
118 /*
119 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
120 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
121 * to software 1x8 byte slicing from crc32c.ko
122 */
123 conn->conn_rx_hash.flags = 0;
124 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
125 CRYPTO_ALG_ASYNC);
126 if (IS_ERR(conn->conn_rx_hash.tfm)) {
127 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
128 return -ENOMEM;
129 }
130
131 conn->conn_tx_hash.flags = 0;
132 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
133 CRYPTO_ALG_ASYNC);
134 if (IS_ERR(conn->conn_tx_hash.tfm)) {
135 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
136 crypto_free_hash(conn->conn_rx_hash.tfm);
137 return -ENOMEM;
138 }
139
140 return 0;
141}
142
143static int iscsi_login_check_initiator_version(
144 struct iscsi_conn *conn,
145 u8 version_max,
146 u8 version_min)
147{
148 if ((version_max != 0x00) || (version_min != 0x00)) {
149 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
150 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
151 version_min, version_max);
152 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
153 ISCSI_LOGIN_STATUS_NO_VERSION);
154 return -1;
155 }
156
157 return 0;
158}
159
160int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
161{
162 int sessiontype;
163 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
164 struct iscsi_portal_group *tpg = conn->tpg;
165 struct iscsi_session *sess = NULL, *sess_p = NULL;
166 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
167 struct se_session *se_sess, *se_sess_tmp;
168
169 initiatorname_param = iscsi_find_param_from_key(
170 INITIATORNAME, conn->param_list);
e48354ce
NB
171 sessiontype_param = iscsi_find_param_from_key(
172 SESSIONTYPE, conn->param_list);
1c5c12c6
RD
173 if (!initiatorname_param || !sessiontype_param) {
174 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
175 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
e48354ce 176 return -1;
1c5c12c6 177 }
e48354ce
NB
178
179 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
180
181 spin_lock_bh(&se_tpg->session_lock);
182 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
183 sess_list) {
184
8359cf43 185 sess_p = se_sess->fabric_sess_ptr;
e48354ce
NB
186 spin_lock(&sess_p->conn_lock);
187 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
188 atomic_read(&sess_p->session_logout) ||
189 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
190 spin_unlock(&sess_p->conn_lock);
191 continue;
192 }
8359cf43
JE
193 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
194 (!strcmp(sess_p->sess_ops->InitiatorName,
195 initiatorname_param->value) &&
e48354ce
NB
196 (sess_p->sess_ops->SessionType == sessiontype))) {
197 atomic_set(&sess_p->session_reinstatement, 1);
198 spin_unlock(&sess_p->conn_lock);
199 iscsit_inc_session_usage_count(sess_p);
200 iscsit_stop_time2retain_timer(sess_p);
201 sess = sess_p;
202 break;
203 }
204 spin_unlock(&sess_p->conn_lock);
205 }
206 spin_unlock_bh(&se_tpg->session_lock);
207 /*
208 * If the Time2Retain handler has expired, the session is already gone.
209 */
210 if (!sess)
211 return 0;
212
213 pr_debug("%s iSCSI Session SID %u is still active for %s,"
214 " preforming session reinstatement.\n", (sessiontype) ?
215 "Discovery" : "Normal", sess->sid,
216 sess->sess_ops->InitiatorName);
217
218 spin_lock_bh(&sess->conn_lock);
219 if (sess->session_state == TARG_SESS_STATE_FAILED) {
220 spin_unlock_bh(&sess->conn_lock);
221 iscsit_dec_session_usage_count(sess);
99367f01
NB
222 target_put_session(sess->se_sess);
223 return 0;
e48354ce
NB
224 }
225 spin_unlock_bh(&sess->conn_lock);
226
227 iscsit_stop_session(sess, 1, 1);
228 iscsit_dec_session_usage_count(sess);
229
99367f01
NB
230 target_put_session(sess->se_sess);
231 return 0;
e48354ce
NB
232}
233
234static void iscsi_login_set_conn_values(
235 struct iscsi_session *sess,
236 struct iscsi_conn *conn,
50e5c87d 237 __be16 cid)
e48354ce
NB
238{
239 conn->sess = sess;
50e5c87d 240 conn->cid = be16_to_cpu(cid);
e48354ce
NB
241 /*
242 * Generate a random Status sequence number (statsn) for the new
243 * iSCSI connection.
244 */
245 get_random_bytes(&conn->stat_sn, sizeof(u32));
246
247 mutex_lock(&auth_id_lock);
248 conn->auth_id = iscsit_global->auth_id++;
249 mutex_unlock(&auth_id_lock);
250}
251
252/*
253 * This is the leading connection of a new session,
254 * or session reinstatement.
255 */
256static int iscsi_login_zero_tsih_s1(
257 struct iscsi_conn *conn,
258 unsigned char *buf)
259{
260 struct iscsi_session *sess = NULL;
261 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
e70beee7 262 enum target_prot_op sup_pro_ops;
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 308 sess->creation_time = get_jiffies_64();
e48354ce
NB
309 /*
310 * The FFP CmdSN window values will be allocated from the TPG's
311 * Initiator Node's ACL once the login has been successfully completed.
312 */
50e5c87d 313 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
e48354ce
NB
314
315 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
316 if (!sess->sess_ops) {
317 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
318 ISCSI_LOGIN_STATUS_NO_RESOURCES);
319 pr_err("Unable to allocate memory for"
320 " struct iscsi_sess_ops.\n");
0957627a
NB
321 kfree(sess);
322 return -ENOMEM;
e48354ce 323 }
e70beee7 324 sup_pro_ops = conn->conn_transport->iscsit_get_sup_prot_ops(conn);
e48354ce 325
e70beee7 326 sess->se_sess = transport_init_session(sup_pro_ops);
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 */
60bfcf8e 351 sess->tsih = ++sess->tpg->ntsih;
e48354ce 352 if (!sess->tsih)
60bfcf8e 353 sess->tsih = ++sess->tpg->ntsih;
e48354ce
NB
354
355 /*
356 * Create the default params from user defined values..
357 */
358 if (iscsi_copy_param_list(&conn->param_list,
60bfcf8e 359 conn->tpg->param_list, 1) < 0) {
e48354ce
NB
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);
60bfcf8e 384 sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
e48354ce
NB
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,
60bfcf8e 579 conn->tpg->param_list, 0) < 0) {
e48354ce
NB
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);
60bfcf8e 597 sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
e48354ce
NB
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
a91eb7d9 687int iscsi_post_login_handler(
e48354ce
NB
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;
60bfcf8e 695 struct iscsi_portal_group *tpg = sess->tpg;
e48354ce
NB
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;
837f6452 875 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
e48354ce
NB
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) {
dfecf611
CL
1010 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1011 snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1012 &sock_in6.sin6_addr.in6_u);
1013 else
1014 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1015 &sock_in6.sin6_addr.s6_addr32[3]);
baa4d64b
NB
1016 conn->login_port = ntohs(sock_in6.sin6_port);
1017 }
1018
1019 rc = conn->sock->ops->getname(conn->sock,
1020 (struct sockaddr *)&sock_in6, &err, 0);
1021 if (!rc) {
dfecf611
CL
1022 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1023 snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1024 &sock_in6.sin6_addr.in6_u);
1025 else
1026 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1027 &sock_in6.sin6_addr.s6_addr32[3]);
baa4d64b
NB
1028 conn->local_port = ntohs(sock_in6.sin6_port);
1029 }
1030 } else {
1031 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1032
1033 rc = conn->sock->ops->getname(conn->sock,
1034 (struct sockaddr *)&sock_in, &err, 1);
1035 if (!rc) {
1036 sprintf(conn->login_ip, "%pI4",
1037 &sock_in.sin_addr.s_addr);
1038 conn->login_port = ntohs(sock_in.sin_port);
1039 }
1040
1041 rc = conn->sock->ops->getname(conn->sock,
1042 (struct sockaddr *)&sock_in, &err, 0);
1043 if (!rc) {
1044 sprintf(conn->local_ip, "%pI4",
1045 &sock_in.sin_addr.s_addr);
1046 conn->local_port = ntohs(sock_in.sin_port);
1047 }
1048 }
1049
1050 return 0;
1051}
1052
1053int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1054{
1055 struct iscsi_login_req *login_req;
1056 u32 padding = 0, payload_length;
1057
1058 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1059 return -1;
1060
1061 login_req = (struct iscsi_login_req *)login->req;
1062 payload_length = ntoh24(login_req->dlength);
1063 padding = ((-payload_length) & 3);
1064
1065 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1066 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1067 login_req->flags, login_req->itt, login_req->cmdsn,
1068 login_req->exp_statsn, login_req->cid, payload_length);
1069 /*
1070 * Setup the initial iscsi_login values from the leading
1071 * login request PDU.
1072 */
1073 if (login->first_request) {
1074 login_req = (struct iscsi_login_req *)login->req;
1075 login->leading_connection = (!login_req->tsih) ? 1 : 0;
3e1c81a9 1076 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
baa4d64b
NB
1077 login->version_min = login_req->min_version;
1078 login->version_max = login_req->max_version;
1079 memcpy(login->isid, login_req->isid, 6);
1080 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1081 login->init_task_tag = login_req->itt;
1082 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1083 login->cid = be16_to_cpu(login_req->cid);
1084 login->tsih = be16_to_cpu(login_req->tsih);
1085 }
1086
1087 if (iscsi_target_check_login_request(conn, login) < 0)
1088 return -1;
1089
1090 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1091 if (iscsi_login_rx_data(conn, login->req_buf,
1092 payload_length + padding) < 0)
1093 return -1;
1094
1095 return 0;
1096}
1097
1098int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1099 u32 length)
1100{
1101 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1102 return -1;
1103
1104 return 0;
1105}
1106
1107static int
1108iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1109{
1110 int rc;
1111
1112 if (!t->owner) {
1113 conn->conn_transport = t;
1114 return 0;
1115 }
1116
1117 rc = try_module_get(t->owner);
1118 if (!rc) {
1119 pr_err("try_module_get() failed for %s\n", t->name);
1120 return -EINVAL;
1121 }
1122
1123 conn->conn_transport = t;
1124 return 0;
1125}
1126
a91eb7d9
NB
1127void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1128 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1129{
1130 if (new_sess == false)
1131 goto old_sess_out;
1132
1133 pr_err("iSCSI Login negotiation failed.\n");
1134 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1135 ISCSI_LOGIN_STATUS_INIT_ERR);
1136 if (!zero_tsih || !conn->sess)
1137 goto old_sess_out;
1138 if (conn->sess->se_sess)
1139 transport_free_session(conn->sess->se_sess);
1140 if (conn->sess->session_index != 0) {
1141 spin_lock_bh(&sess_idr_lock);
1142 idr_remove(&sess_idr, conn->sess->session_index);
1143 spin_unlock_bh(&sess_idr_lock);
1144 }
1145 kfree(conn->sess->sess_ops);
1146 kfree(conn->sess);
1147
1148old_sess_out:
1149 iscsi_stop_login_thread_timer(np);
1150 /*
1151 * If login negotiation fails check if the Time2Retain timer
1152 * needs to be restarted.
1153 */
1154 if (!zero_tsih && conn->sess) {
1155 spin_lock_bh(&conn->sess->conn_lock);
1156 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1157 struct se_portal_group *se_tpg =
60bfcf8e 1158 &conn->tpg->tpg_se_tpg;
a91eb7d9
NB
1159
1160 atomic_set(&conn->sess->session_continuation, 0);
1161 spin_unlock_bh(&conn->sess->conn_lock);
1162 spin_lock_bh(&se_tpg->session_lock);
1163 iscsit_start_time2retain_handler(conn->sess);
1164 spin_unlock_bh(&se_tpg->session_lock);
1165 } else
1166 spin_unlock_bh(&conn->sess->conn_lock);
1167 iscsit_dec_session_usage_count(conn->sess);
1168 }
1169
1170 if (!IS_ERR(conn->conn_rx_hash.tfm))
1171 crypto_free_hash(conn->conn_rx_hash.tfm);
1172 if (!IS_ERR(conn->conn_tx_hash.tfm))
1173 crypto_free_hash(conn->conn_tx_hash.tfm);
1174
1175 if (conn->conn_cpumask)
1176 free_cpumask_var(conn->conn_cpumask);
1177
1178 kfree(conn->conn_ops);
1179
1180 if (conn->param_list) {
1181 iscsi_release_param_list(conn->param_list);
1182 conn->param_list = NULL;
1183 }
1184 iscsi_target_nego_release(conn);
1185
1186 if (conn->sock) {
1187 sock_release(conn->sock);
1188 conn->sock = NULL;
1189 }
1190
1191 if (conn->conn_transport->iscsit_free_conn)
1192 conn->conn_transport->iscsit_free_conn(conn);
1193
1194 iscsit_put_transport(conn->conn_transport);
1195 kfree(conn);
1196}
1197
e48354ce
NB
1198static int __iscsi_target_login_thread(struct iscsi_np *np)
1199{
baa4d64b
NB
1200 u8 *buffer, zero_tsih = 0;
1201 int ret = 0, rc, stop;
e48354ce
NB
1202 struct iscsi_conn *conn = NULL;
1203 struct iscsi_login *login;
1204 struct iscsi_portal_group *tpg = NULL;
e48354ce 1205 struct iscsi_login_req *pdu;
a91eb7d9
NB
1206 struct iscsi_tpg_np *tpg_np;
1207 bool new_sess = false;
e48354ce
NB
1208
1209 flush_signals(current);
e48354ce
NB
1210
1211 spin_lock_bh(&np->np_thread_lock);
1212 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1213 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1214 complete(&np->np_restart_comp);
1215 } else {
1216 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1217 }
1218 spin_unlock_bh(&np->np_thread_lock);
1219
e48354ce
NB
1220 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1221 if (!conn) {
1222 pr_err("Could not allocate memory for"
1223 " new connection\n");
e48354ce
NB
1224 /* Get another socket */
1225 return 1;
1226 }
e48354ce
NB
1227 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1228 conn->conn_state = TARG_CONN_STATE_FREE;
e48354ce 1229
baa4d64b
NB
1230 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1231 kfree(conn);
1232 return 1;
1233 }
e48354ce 1234
baa4d64b
NB
1235 rc = np->np_transport->iscsit_accept_np(np, conn);
1236 if (rc == -ENOSYS) {
1237 complete(&np->np_restart_comp);
1238 iscsit_put_transport(conn->conn_transport);
1239 kfree(conn);
1240 conn = NULL;
1241 goto exit;
1242 } else if (rc < 0) {
1243 spin_lock_bh(&np->np_thread_lock);
1244 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1245 spin_unlock_bh(&np->np_thread_lock);
1246 complete(&np->np_restart_comp);
c9a03c12
NB
1247 iscsit_put_transport(conn->conn_transport);
1248 kfree(conn);
1249 conn = NULL;
1250 if (ret == -ENODEV)
baa4d64b 1251 goto out;
baa4d64b
NB
1252 /* Get another socket */
1253 return 1;
1254 }
1255 spin_unlock_bh(&np->np_thread_lock);
1256 iscsit_put_transport(conn->conn_transport);
1257 kfree(conn);
1258 conn = NULL;
1259 goto out;
e48354ce
NB
1260 }
1261 /*
1262 * Perform the remaining iSCSI connection initialization items..
1263 */
baa4d64b
NB
1264 login = iscsi_login_init_conn(conn);
1265 if (!login) {
e48354ce
NB
1266 goto new_sess_out;
1267 }
1268
baa4d64b 1269 iscsi_start_login_thread_timer(np);
e48354ce 1270
baa4d64b
NB
1271 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1272 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1273 /*
1274 * This will process the first login request + payload..
1275 */
1276 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1277 if (rc == 1)
1278 return 1;
1279 else if (rc < 0)
1280 goto new_sess_out;
66c7db68 1281
baa4d64b
NB
1282 buffer = &login->req[0];
1283 pdu = (struct iscsi_login_req *)buffer;
e48354ce
NB
1284 /*
1285 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1286 * when Status-Class != 0.
1287 */
baa4d64b 1288 conn->login_itt = pdu->itt;
e48354ce
NB
1289
1290 spin_lock_bh(&np->np_thread_lock);
1291 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1292 spin_unlock_bh(&np->np_thread_lock);
1293 pr_err("iSCSI Network Portal on %s:%hu currently not"
1294 " active.\n", np->np_ip, np->np_port);
1295 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1296 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1297 goto new_sess_out;
1298 }
1299 spin_unlock_bh(&np->np_thread_lock);
1300
e48354ce
NB
1301 conn->network_transport = np->np_network_transport;
1302
1303 pr_debug("Received iSCSI login request from %s on %s Network"
baa4d64b
NB
1304 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1305 conn->local_ip, conn->local_port);
e48354ce
NB
1306
1307 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1308 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1309
1310 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1311 pdu->min_version) < 0)
1312 goto new_sess_out;
1313
1314 zero_tsih = (pdu->tsih == 0x0000);
ee1b1b9c 1315 if (zero_tsih) {
e48354ce
NB
1316 /*
1317 * This is the leading connection of a new session.
1318 * We wait until after authentication to check for
1319 * session reinstatement.
1320 */
1321 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1322 goto new_sess_out;
1323 } else {
1324 /*
1325 * Add a new connection to an existing session.
1326 * We check for a non-existant session in
1327 * iscsi_login_non_zero_tsih_s2() below based
1328 * on ISID/TSIH, but wait until after authentication
1329 * to check for connection reinstatement, etc.
1330 */
1331 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1332 goto new_sess_out;
1333 }
e48354ce 1334 /*
baa4d64b
NB
1335 * SessionType: Discovery
1336 *
1337 * Locates Default Portal
1338 *
1339 * SessionType: Normal
1340 *
1341 * Locates Target Portal from NP -> Target IQN
e48354ce 1342 */
baa4d64b
NB
1343 rc = iscsi_target_locate_portal(np, conn, login);
1344 if (rc < 0) {
e48354ce
NB
1345 tpg = conn->tpg;
1346 goto new_sess_out;
1347 }
a91eb7d9 1348 login->zero_tsih = zero_tsih;
e48354ce
NB
1349
1350 tpg = conn->tpg;
1351 if (!tpg) {
1352 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1353 goto new_sess_out;
1354 }
1355
1356 if (zero_tsih) {
baa4d64b 1357 if (iscsi_login_zero_tsih_s2(conn) < 0)
e48354ce 1358 goto new_sess_out;
e48354ce 1359 } else {
baa4d64b 1360 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
e48354ce 1361 goto old_sess_out;
e48354ce
NB
1362 }
1363
a91eb7d9
NB
1364 ret = iscsi_target_start_negotiation(login, conn);
1365 if (ret < 0)
e48354ce
NB
1366 goto new_sess_out;
1367
1368 if (!conn->sess) {
1369 pr_err("struct iscsi_conn session pointer is NULL!\n");
1370 goto new_sess_out;
1371 }
1372
1373 iscsi_stop_login_thread_timer(np);
1374
1375 if (signal_pending(current))
1376 goto new_sess_out;
1377
a91eb7d9
NB
1378 if (ret == 1) {
1379 tpg_np = conn->tpg_np;
e48354ce 1380
a91eb7d9
NB
1381 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1382 if (ret < 0)
1383 goto new_sess_out;
1384
1385 iscsit_deaccess_np(np, tpg, tpg_np);
1386 }
e48354ce 1387
e48354ce 1388 tpg = NULL;
a91eb7d9 1389 tpg_np = NULL;
e48354ce
NB
1390 /* Get another socket */
1391 return 1;
1392
1393new_sess_out:
a91eb7d9 1394 new_sess = true;
e48354ce 1395old_sess_out:
a91eb7d9
NB
1396 tpg_np = conn->tpg_np;
1397 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1398 new_sess = false;
e48354ce
NB
1399
1400 if (tpg) {
a91eb7d9 1401 iscsit_deaccess_np(np, tpg, tpg_np);
e48354ce 1402 tpg = NULL;
a91eb7d9 1403 tpg_np = NULL;
e48354ce
NB
1404 }
1405
1406out:
1407 stop = kthread_should_stop();
e48354ce
NB
1408 /* Wait for another socket.. */
1409 if (!stop)
1410 return 1;
baa4d64b 1411exit:
e48354ce
NB
1412 iscsi_stop_login_thread_timer(np);
1413 spin_lock_bh(&np->np_thread_lock);
1414 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1415 spin_unlock_bh(&np->np_thread_lock);
baa4d64b 1416
e48354ce
NB
1417 return 0;
1418}
1419
1420int iscsi_target_login_thread(void *arg)
1421{
8359cf43 1422 struct iscsi_np *np = arg;
e48354ce
NB
1423 int ret;
1424
1425 allow_signal(SIGINT);
1426
1427 while (!kthread_should_stop()) {
1428 ret = __iscsi_target_login_thread(np);
1429 /*
1430 * We break and exit here unless another sock_accept() call
1431 * is expected.
1432 */
1433 if (ret != 1)
1434 break;
1435 }
1436
1437 return 0;
1438}