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