OSDN Git Service

iscsi-target: Disable Immediate + Unsolicited Data with ISER Protection
[uclinux-h8/linux.git] / 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 "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
40 #include <target/iscsi/iscsi_transport.h>
41
42 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
43 {
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         }
51         conn->login = login;
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
74         init_waitqueue_head(&conn->queues_wq);
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");
95                 goto out_conn_ops;
96         }
97         conn->conn_login = login;
98
99         return login;
100
101 out_conn_ops:
102         kfree(conn->conn_ops);
103 out_rsp_buf:
104         kfree(login->rsp_buf);
105 out_req_buf:
106         kfree(login->req_buf);
107 out_login:
108         kfree(login);
109         return NULL;
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  */
116 int 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
143 static 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
160 int 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);
171         sessiontype_param = iscsi_find_param_from_key(
172                         SESSIONTYPE, conn->param_list);
173         if (!initiatorname_param || !sessiontype_param) {
174                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
175                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
176                 return -1;
177         }
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
185                 sess_p = se_sess->fabric_sess_ptr;
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                 }
193                 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
194                    (!strcmp(sess_p->sess_ops->InitiatorName,
195                             initiatorname_param->value) &&
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);
222                 target_put_session(sess->se_sess);
223                 return 0;
224         }
225         spin_unlock_bh(&sess->conn_lock);
226
227         iscsit_stop_session(sess, 1, 1);
228         iscsit_dec_session_usage_count(sess);
229
230         target_put_session(sess->se_sess);
231         return 0;
232 }
233
234 static void iscsi_login_set_conn_values(
235         struct iscsi_session *sess,
236         struct iscsi_conn *conn,
237         __be16 cid)
238 {
239         conn->sess              = sess;
240         conn->cid               = be16_to_cpu(cid);
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  */
256 static 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;
262         enum target_prot_op sup_pro_ops;
263         int ret;
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");
270                 return -ENOMEM;
271         }
272
273         iscsi_login_set_conn_values(sess, conn, pdu->cid);
274         sess->init_task_tag     = pdu->itt;
275         memcpy(&sess->isid, pdu->isid, 6);
276         sess->exp_cmd_sn        = be32_to_cpu(pdu->cmdsn);
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
292         idr_preload(GFP_KERNEL);
293         spin_lock_bh(&sess_idr_lock);
294         ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
295         if (ret >= 0)
296                 sess->session_index = ret;
297         spin_unlock_bh(&sess_idr_lock);
298         idr_preload_end();
299
300         if (ret < 0) {
301                 pr_err("idr_alloc() for sess_idr failed\n");
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
308         sess->creation_time = get_jiffies_64();
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          */
313         sess->max_cmd_sn        = be32_to_cpu(pdu->cmdsn);
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");
321                 kfree(sess);
322                 return -ENOMEM;
323         }
324         sup_pro_ops = conn->conn_transport->iscsit_get_sup_prot_ops(conn);
325
326         sess->se_sess = transport_init_session(sup_pro_ops);
327         if (IS_ERR(sess->se_sess)) {
328                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
329                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
330                 kfree(sess);
331                 return -ENOMEM;
332         }
333
334         return 0;
335 }
336
337 static 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];
343         bool iser = false;
344
345         sess->tpg = conn->tpg;
346
347         /*
348          * Assign a new TPG Session Handle.  Note this is protected with
349          * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
350          */
351         sess->tsih = ++sess->tpg->ntsih;
352         if (!sess->tsih)
353                 sess->tsih = ++sess->tpg->ntsih;
354
355         /*
356          * Create the default params from user defined values..
357          */
358         if (iscsi_copy_param_list(&conn->param_list,
359                                 conn->tpg->param_list, 1) < 0) {
360                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
361                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
362                 return -1;
363         }
364
365         if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
366                 iser = true;
367
368         iscsi_set_keys_to_negotiate(conn->param_list, iser);
369
370         if (sess->sess_ops->SessionType)
371                 return iscsi_set_keys_irrelevant_for_discovery(
372                                 conn->param_list);
373
374         na = iscsit_tpg_get_node_attrib(sess);
375
376         /*
377          * Need to send TargetPortalGroupTag back in first login response
378          * on any iSCSI connection where the Initiator provides TargetName.
379          * See 5.3.1.  Login Phase Start
380          *
381          * In our case, we have already located the struct iscsi_tiqn at this point.
382          */
383         memset(buf, 0, 32);
384         sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
385         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
386                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
387                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
388                 return -1;
389         }
390
391         /*
392          * Workaround for Initiators that have broken connection recovery logic.
393          *
394          * "We would really like to get rid of this." Linux-iSCSI.org team
395          */
396         memset(buf, 0, 32);
397         sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
398         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
399                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
400                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
401                 return -1;
402         }
403
404         if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
405                 return -1;
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                 }
431                 rc = kstrtoul(param->value, 0, &mrdsl);
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                         goto check_prot;
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                 /*
456                  * ISER currently requires that ImmediateData + Unsolicited
457                  * Data be disabled when protection / signature MRs are enabled.
458                  */
459 check_prot:
460                 if (sess->se_sess->sup_prot_ops &
461                    (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
462                     TARGET_PROT_DOUT_INSERT)) {
463
464                         sprintf(buf, "ImmediateData=No");
465                         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
466                                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
467                                                     ISCSI_LOGIN_STATUS_NO_RESOURCES);
468                                 return -1;
469                         }
470
471                         sprintf(buf, "InitialR2T=Yes");
472                         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
473                                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
474                                                     ISCSI_LOGIN_STATUS_NO_RESOURCES);
475                                 return -1;
476                         }
477                         pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
478                                  " T10-PI enabled ISER session\n");
479                 }
480         }
481
482         return 0;
483 }
484
485 /*
486  * Remove PSTATE_NEGOTIATE for the four FIM related keys.
487  * The Initiator node will be able to enable FIM by proposing them itself.
488  */
489 int iscsi_login_disable_FIM_keys(
490         struct iscsi_param_list *param_list,
491         struct iscsi_conn *conn)
492 {
493         struct iscsi_param *param;
494
495         param = iscsi_find_param_from_key("OFMarker", param_list);
496         if (!param) {
497                 pr_err("iscsi_find_param_from_key() for"
498                                 " OFMarker failed\n");
499                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
500                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
501                 return -1;
502         }
503         param->state &= ~PSTATE_NEGOTIATE;
504
505         param = iscsi_find_param_from_key("OFMarkInt", param_list);
506         if (!param) {
507                 pr_err("iscsi_find_param_from_key() for"
508                                 " IFMarker failed\n");
509                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
510                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
511                 return -1;
512         }
513         param->state &= ~PSTATE_NEGOTIATE;
514
515         param = iscsi_find_param_from_key("IFMarker", param_list);
516         if (!param) {
517                 pr_err("iscsi_find_param_from_key() for"
518                                 " IFMarker failed\n");
519                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
520                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
521                 return -1;
522         }
523         param->state &= ~PSTATE_NEGOTIATE;
524
525         param = iscsi_find_param_from_key("IFMarkInt", param_list);
526         if (!param) {
527                 pr_err("iscsi_find_param_from_key() for"
528                                 " IFMarker failed\n");
529                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
530                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
531                 return -1;
532         }
533         param->state &= ~PSTATE_NEGOTIATE;
534
535         return 0;
536 }
537
538 static int iscsi_login_non_zero_tsih_s1(
539         struct iscsi_conn *conn,
540         unsigned char *buf)
541 {
542         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
543
544         iscsi_login_set_conn_values(NULL, conn, pdu->cid);
545         return 0;
546 }
547
548 /*
549  *      Add a new connection to an existing session.
550  */
551 static int iscsi_login_non_zero_tsih_s2(
552         struct iscsi_conn *conn,
553         unsigned char *buf)
554 {
555         struct iscsi_portal_group *tpg = conn->tpg;
556         struct iscsi_session *sess = NULL, *sess_p = NULL;
557         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
558         struct se_session *se_sess, *se_sess_tmp;
559         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
560         bool iser = false;
561
562         spin_lock_bh(&se_tpg->session_lock);
563         list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
564                         sess_list) {
565
566                 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
567                 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
568                     atomic_read(&sess_p->session_logout) ||
569                    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
570                         continue;
571                 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
572                      (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
573                         iscsit_inc_session_usage_count(sess_p);
574                         iscsit_stop_time2retain_timer(sess_p);
575                         sess = sess_p;
576                         break;
577                 }
578         }
579         spin_unlock_bh(&se_tpg->session_lock);
580
581         /*
582          * If the Time2Retain handler has expired, the session is already gone.
583          */
584         if (!sess) {
585                 pr_err("Initiator attempting to add a connection to"
586                         " a non-existent session, rejecting iSCSI Login.\n");
587                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
588                                 ISCSI_LOGIN_STATUS_NO_SESSION);
589                 return -1;
590         }
591
592         /*
593          * Stop the Time2Retain timer if this is a failed session, we restart
594          * the timer if the login is not successful.
595          */
596         spin_lock_bh(&sess->conn_lock);
597         if (sess->session_state == TARG_SESS_STATE_FAILED)
598                 atomic_set(&sess->session_continuation, 1);
599         spin_unlock_bh(&sess->conn_lock);
600
601         iscsi_login_set_conn_values(sess, conn, pdu->cid);
602
603         if (iscsi_copy_param_list(&conn->param_list,
604                         conn->tpg->param_list, 0) < 0) {
605                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
606                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
607                 return -1;
608         }
609
610         if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
611                 iser = true;
612
613         iscsi_set_keys_to_negotiate(conn->param_list, iser);
614         /*
615          * Need to send TargetPortalGroupTag back in first login response
616          * on any iSCSI connection where the Initiator provides TargetName.
617          * See 5.3.1.  Login Phase Start
618          *
619          * In our case, we have already located the struct iscsi_tiqn at this point.
620          */
621         memset(buf, 0, 32);
622         sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
623         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
624                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
625                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
626                 return -1;
627         }
628
629         return iscsi_login_disable_FIM_keys(conn->param_list, conn);
630 }
631
632 int iscsi_login_post_auth_non_zero_tsih(
633         struct iscsi_conn *conn,
634         u16 cid,
635         u32 exp_statsn)
636 {
637         struct iscsi_conn *conn_ptr = NULL;
638         struct iscsi_conn_recovery *cr = NULL;
639         struct iscsi_session *sess = conn->sess;
640
641         /*
642          * By following item 5 in the login table,  if we have found
643          * an existing ISID and a valid/existing TSIH and an existing
644          * CID we do connection reinstatement.  Currently we dont not
645          * support it so we send back an non-zero status class to the
646          * initiator and release the new connection.
647          */
648         conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
649         if (conn_ptr) {
650                 pr_err("Connection exists with CID %hu for %s,"
651                         " performing connection reinstatement.\n",
652                         conn_ptr->cid, sess->sess_ops->InitiatorName);
653
654                 iscsit_connection_reinstatement_rcfr(conn_ptr);
655                 iscsit_dec_conn_usage_count(conn_ptr);
656         }
657
658         /*
659          * Check for any connection recovery entires containing CID.
660          * We use the original ExpStatSN sent in the first login request
661          * to acknowledge commands for the failed connection.
662          *
663          * Also note that an explict logout may have already been sent,
664          * but the response may not be sent due to additional connection
665          * loss.
666          */
667         if (sess->sess_ops->ErrorRecoveryLevel == 2) {
668                 cr = iscsit_get_inactive_connection_recovery_entry(
669                                 sess, cid);
670                 if (cr) {
671                         pr_debug("Performing implicit logout"
672                                 " for connection recovery on CID: %hu\n",
673                                         conn->cid);
674                         iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
675                 }
676         }
677
678         /*
679          * Else we follow item 4 from the login table in that we have
680          * found an existing ISID and a valid/existing TSIH and a new
681          * CID we go ahead and continue to add a new connection to the
682          * session.
683          */
684         pr_debug("Adding CID %hu to existing session for %s.\n",
685                         cid, sess->sess_ops->InitiatorName);
686
687         if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
688                 pr_err("Adding additional connection to this session"
689                         " would exceed MaxConnections %d, login failed.\n",
690                                 sess->sess_ops->MaxConnections);
691                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
692                                 ISCSI_LOGIN_STATUS_ISID_ERROR);
693                 return -1;
694         }
695
696         return 0;
697 }
698
699 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
700 {
701         struct iscsi_session *sess = conn->sess;
702         /*
703          * FIXME: Unsolicitied NopIN support for ISER
704          */
705         if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
706                 return;
707
708         if (!sess->sess_ops->SessionType)
709                 iscsit_start_nopin_timer(conn);
710 }
711
712 int iscsi_post_login_handler(
713         struct iscsi_np *np,
714         struct iscsi_conn *conn,
715         u8 zero_tsih)
716 {
717         int stop_timer = 0;
718         struct iscsi_session *sess = conn->sess;
719         struct se_session *se_sess = sess->se_sess;
720         struct iscsi_portal_group *tpg = sess->tpg;
721         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
722         struct iscsi_thread_set *ts;
723
724         iscsit_inc_conn_usage_count(conn);
725
726         iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
727                         ISCSI_LOGIN_STATUS_ACCEPT);
728
729         pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
730         conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
731
732         iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
733         iscsit_set_sync_and_steering_values(conn);
734         /*
735          * SCSI Initiator -> SCSI Target Port Mapping
736          */
737         ts = iscsi_get_thread_set();
738         if (!zero_tsih) {
739                 iscsi_set_session_parameters(sess->sess_ops,
740                                 conn->param_list, 0);
741                 iscsi_release_param_list(conn->param_list);
742                 conn->param_list = NULL;
743
744                 spin_lock_bh(&sess->conn_lock);
745                 atomic_set(&sess->session_continuation, 0);
746                 if (sess->session_state == TARG_SESS_STATE_FAILED) {
747                         pr_debug("Moving to"
748                                         " TARG_SESS_STATE_LOGGED_IN.\n");
749                         sess->session_state = TARG_SESS_STATE_LOGGED_IN;
750                         stop_timer = 1;
751                 }
752
753                 pr_debug("iSCSI Login successful on CID: %hu from %s to"
754                         " %s:%hu,%hu\n", conn->cid, conn->login_ip,
755                         conn->local_ip, conn->local_port, tpg->tpgt);
756
757                 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
758                 atomic_inc(&sess->nconn);
759                 pr_debug("Incremented iSCSI Connection count to %hu"
760                         " from node: %s\n", atomic_read(&sess->nconn),
761                         sess->sess_ops->InitiatorName);
762                 spin_unlock_bh(&sess->conn_lock);
763
764                 iscsi_post_login_start_timers(conn);
765
766                 iscsi_activate_thread_set(conn, ts);
767                 /*
768                  * Determine CPU mask to ensure connection's RX and TX kthreads
769                  * are scheduled on the same CPU.
770                  */
771                 iscsit_thread_get_cpumask(conn);
772                 conn->conn_rx_reset_cpumask = 1;
773                 conn->conn_tx_reset_cpumask = 1;
774
775                 iscsit_dec_conn_usage_count(conn);
776                 if (stop_timer) {
777                         spin_lock_bh(&se_tpg->session_lock);
778                         iscsit_stop_time2retain_timer(sess);
779                         spin_unlock_bh(&se_tpg->session_lock);
780                 }
781                 iscsit_dec_session_usage_count(sess);
782                 return 0;
783         }
784
785         iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
786         iscsi_release_param_list(conn->param_list);
787         conn->param_list = NULL;
788
789         iscsit_determine_maxcmdsn(sess);
790
791         spin_lock_bh(&se_tpg->session_lock);
792         __transport_register_session(&sess->tpg->tpg_se_tpg,
793                         se_sess->se_node_acl, se_sess, sess);
794         pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
795         sess->session_state = TARG_SESS_STATE_LOGGED_IN;
796
797         pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
798                 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
799                 tpg->tpgt);
800
801         spin_lock_bh(&sess->conn_lock);
802         list_add_tail(&conn->conn_list, &sess->sess_conn_list);
803         atomic_inc(&sess->nconn);
804         pr_debug("Incremented iSCSI Connection count to %hu from node:"
805                 " %s\n", atomic_read(&sess->nconn),
806                 sess->sess_ops->InitiatorName);
807         spin_unlock_bh(&sess->conn_lock);
808
809         sess->sid = tpg->sid++;
810         if (!sess->sid)
811                 sess->sid = tpg->sid++;
812         pr_debug("Established iSCSI session from node: %s\n",
813                         sess->sess_ops->InitiatorName);
814
815         tpg->nsessions++;
816         if (tpg->tpg_tiqn)
817                 tpg->tpg_tiqn->tiqn_nsessions++;
818
819         pr_debug("Incremented number of active iSCSI sessions to %u on"
820                 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
821         spin_unlock_bh(&se_tpg->session_lock);
822
823         iscsi_post_login_start_timers(conn);
824         iscsi_activate_thread_set(conn, ts);
825         /*
826          * Determine CPU mask to ensure connection's RX and TX kthreads
827          * are scheduled on the same CPU.
828          */
829         iscsit_thread_get_cpumask(conn);
830         conn->conn_rx_reset_cpumask = 1;
831         conn->conn_tx_reset_cpumask = 1;
832
833         iscsit_dec_conn_usage_count(conn);
834
835         return 0;
836 }
837
838 static void iscsi_handle_login_thread_timeout(unsigned long data)
839 {
840         struct iscsi_np *np = (struct iscsi_np *) data;
841
842         spin_lock_bh(&np->np_thread_lock);
843         pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
844                         np->np_ip, np->np_port);
845
846         if (np->np_login_timer_flags & ISCSI_TF_STOP) {
847                 spin_unlock_bh(&np->np_thread_lock);
848                 return;
849         }
850
851         if (np->np_thread)
852                 send_sig(SIGINT, np->np_thread, 1);
853
854         np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
855         spin_unlock_bh(&np->np_thread_lock);
856 }
857
858 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
859 {
860         /*
861          * This used the TA_LOGIN_TIMEOUT constant because at this
862          * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
863          */
864         spin_lock_bh(&np->np_thread_lock);
865         init_timer(&np->np_login_timer);
866         np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
867         np->np_login_timer.data = (unsigned long)np;
868         np->np_login_timer.function = iscsi_handle_login_thread_timeout;
869         np->np_login_timer_flags &= ~ISCSI_TF_STOP;
870         np->np_login_timer_flags |= ISCSI_TF_RUNNING;
871         add_timer(&np->np_login_timer);
872
873         pr_debug("Added timeout timer to iSCSI login request for"
874                         " %u seconds.\n", TA_LOGIN_TIMEOUT);
875         spin_unlock_bh(&np->np_thread_lock);
876 }
877
878 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
879 {
880         spin_lock_bh(&np->np_thread_lock);
881         if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
882                 spin_unlock_bh(&np->np_thread_lock);
883                 return;
884         }
885         np->np_login_timer_flags |= ISCSI_TF_STOP;
886         spin_unlock_bh(&np->np_thread_lock);
887
888         del_timer_sync(&np->np_login_timer);
889
890         spin_lock_bh(&np->np_thread_lock);
891         np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
892         spin_unlock_bh(&np->np_thread_lock);
893 }
894
895 int iscsit_setup_np(
896         struct iscsi_np *np,
897         struct __kernel_sockaddr_storage *sockaddr)
898 {
899         struct socket *sock = NULL;
900         int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
901
902         switch (np->np_network_transport) {
903         case ISCSI_TCP:
904                 np->np_ip_proto = IPPROTO_TCP;
905                 np->np_sock_type = SOCK_STREAM;
906                 break;
907         case ISCSI_SCTP_TCP:
908                 np->np_ip_proto = IPPROTO_SCTP;
909                 np->np_sock_type = SOCK_STREAM;
910                 break;
911         case ISCSI_SCTP_UDP:
912                 np->np_ip_proto = IPPROTO_SCTP;
913                 np->np_sock_type = SOCK_SEQPACKET;
914                 break;
915         default:
916                 pr_err("Unsupported network_transport: %d\n",
917                                 np->np_network_transport);
918                 return -EINVAL;
919         }
920
921         np->np_ip_proto = IPPROTO_TCP;
922         np->np_sock_type = SOCK_STREAM;
923
924         ret = sock_create(sockaddr->ss_family, np->np_sock_type,
925                         np->np_ip_proto, &sock);
926         if (ret < 0) {
927                 pr_err("sock_create() failed.\n");
928                 return ret;
929         }
930         np->np_socket = sock;
931         /*
932          * Setup the np->np_sockaddr from the passed sockaddr setup
933          * in iscsi_target_configfs.c code..
934          */
935         memcpy(&np->np_sockaddr, sockaddr,
936                         sizeof(struct __kernel_sockaddr_storage));
937
938         if (sockaddr->ss_family == AF_INET6)
939                 len = sizeof(struct sockaddr_in6);
940         else
941                 len = sizeof(struct sockaddr_in);
942         /*
943          * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
944          */
945         /* FIXME: Someone please explain why this is endian-safe */
946         opt = 1;
947         if (np->np_network_transport == ISCSI_TCP) {
948                 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
949                                 (char *)&opt, sizeof(opt));
950                 if (ret < 0) {
951                         pr_err("kernel_setsockopt() for TCP_NODELAY"
952                                 " failed: %d\n", ret);
953                         goto fail;
954                 }
955         }
956
957         /* FIXME: Someone please explain why this is endian-safe */
958         ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
959                         (char *)&opt, sizeof(opt));
960         if (ret < 0) {
961                 pr_err("kernel_setsockopt() for SO_REUSEADDR"
962                         " failed\n");
963                 goto fail;
964         }
965
966         ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
967                         (char *)&opt, sizeof(opt));
968         if (ret < 0) {
969                 pr_err("kernel_setsockopt() for IP_FREEBIND"
970                         " failed\n");
971                 goto fail;
972         }
973
974         ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
975         if (ret < 0) {
976                 pr_err("kernel_bind() failed: %d\n", ret);
977                 goto fail;
978         }
979
980         ret = kernel_listen(sock, backlog);
981         if (ret != 0) {
982                 pr_err("kernel_listen() failed: %d\n", ret);
983                 goto fail;
984         }
985
986         return 0;
987 fail:
988         np->np_socket = NULL;
989         if (sock)
990                 sock_release(sock);
991         return ret;
992 }
993
994 int iscsi_target_setup_login_socket(
995         struct iscsi_np *np,
996         struct __kernel_sockaddr_storage *sockaddr)
997 {
998         struct iscsit_transport *t;
999         int rc;
1000
1001         t = iscsit_get_transport(np->np_network_transport);
1002         if (!t)
1003                 return -EINVAL;
1004
1005         rc = t->iscsit_setup_np(np, sockaddr);
1006         if (rc < 0) {
1007                 iscsit_put_transport(t);
1008                 return rc;
1009         }
1010
1011         np->np_transport = t;
1012         np->enabled = true;
1013         return 0;
1014 }
1015
1016 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1017 {
1018         struct socket *new_sock, *sock = np->np_socket;
1019         struct sockaddr_in sock_in;
1020         struct sockaddr_in6 sock_in6;
1021         int rc, err;
1022
1023         rc = kernel_accept(sock, &new_sock, 0);
1024         if (rc < 0)
1025                 return rc;
1026
1027         conn->sock = new_sock;
1028         conn->login_family = np->np_sockaddr.ss_family;
1029
1030         if (np->np_sockaddr.ss_family == AF_INET6) {
1031                 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1032
1033                 rc = conn->sock->ops->getname(conn->sock,
1034                                 (struct sockaddr *)&sock_in6, &err, 1);
1035                 if (!rc) {
1036                         if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1037                                 snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1038                                         &sock_in6.sin6_addr.in6_u);
1039                         else
1040                                 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1041                                         &sock_in6.sin6_addr.s6_addr32[3]);
1042                         conn->login_port = ntohs(sock_in6.sin6_port);
1043                 }
1044
1045                 rc = conn->sock->ops->getname(conn->sock,
1046                                 (struct sockaddr *)&sock_in6, &err, 0);
1047                 if (!rc) {
1048                         if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1049                                 snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1050                                         &sock_in6.sin6_addr.in6_u);
1051                         else
1052                                 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1053                                         &sock_in6.sin6_addr.s6_addr32[3]);
1054                         conn->local_port = ntohs(sock_in6.sin6_port);
1055                 }
1056         } else {
1057                 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1058
1059                 rc = conn->sock->ops->getname(conn->sock,
1060                                 (struct sockaddr *)&sock_in, &err, 1);
1061                 if (!rc) {
1062                         sprintf(conn->login_ip, "%pI4",
1063                                         &sock_in.sin_addr.s_addr);
1064                         conn->login_port = ntohs(sock_in.sin_port);
1065                 }
1066
1067                 rc = conn->sock->ops->getname(conn->sock,
1068                                 (struct sockaddr *)&sock_in, &err, 0);
1069                 if (!rc) {
1070                         sprintf(conn->local_ip, "%pI4",
1071                                         &sock_in.sin_addr.s_addr);
1072                         conn->local_port = ntohs(sock_in.sin_port);
1073                 }
1074         }
1075
1076         return 0;
1077 }
1078
1079 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1080 {
1081         struct iscsi_login_req *login_req;
1082         u32 padding = 0, payload_length;
1083
1084         if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1085                 return -1;
1086
1087         login_req = (struct iscsi_login_req *)login->req;
1088         payload_length  = ntoh24(login_req->dlength);
1089         padding = ((-payload_length) & 3);
1090
1091         pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1092                 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1093                 login_req->flags, login_req->itt, login_req->cmdsn,
1094                 login_req->exp_statsn, login_req->cid, payload_length);
1095         /*
1096          * Setup the initial iscsi_login values from the leading
1097          * login request PDU.
1098          */
1099         if (login->first_request) {
1100                 login_req = (struct iscsi_login_req *)login->req;
1101                 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1102                 login->current_stage    = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1103                 login->version_min      = login_req->min_version;
1104                 login->version_max      = login_req->max_version;
1105                 memcpy(login->isid, login_req->isid, 6);
1106                 login->cmd_sn           = be32_to_cpu(login_req->cmdsn);
1107                 login->init_task_tag    = login_req->itt;
1108                 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1109                 login->cid              = be16_to_cpu(login_req->cid);
1110                 login->tsih             = be16_to_cpu(login_req->tsih);
1111         }
1112
1113         if (iscsi_target_check_login_request(conn, login) < 0)
1114                 return -1;
1115
1116         memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1117         if (iscsi_login_rx_data(conn, login->req_buf,
1118                                 payload_length + padding) < 0)
1119                 return -1;
1120
1121         return 0;
1122 }
1123
1124 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1125                         u32 length)
1126 {
1127         if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1128                 return -1;
1129
1130         return 0;
1131 }
1132
1133 static int
1134 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1135 {
1136         int rc;
1137
1138         if (!t->owner) {
1139                 conn->conn_transport = t;
1140                 return 0;
1141         }
1142
1143         rc = try_module_get(t->owner);
1144         if (!rc) {
1145                 pr_err("try_module_get() failed for %s\n", t->name);
1146                 return -EINVAL;
1147         }
1148
1149         conn->conn_transport = t;
1150         return 0;
1151 }
1152
1153 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1154                 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1155 {
1156         if (new_sess == false)
1157                 goto old_sess_out;
1158
1159         pr_err("iSCSI Login negotiation failed.\n");
1160         iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1161                                    ISCSI_LOGIN_STATUS_INIT_ERR);
1162         if (!zero_tsih || !conn->sess)
1163                 goto old_sess_out;
1164         if (conn->sess->se_sess)
1165                 transport_free_session(conn->sess->se_sess);
1166         if (conn->sess->session_index != 0) {
1167                 spin_lock_bh(&sess_idr_lock);
1168                 idr_remove(&sess_idr, conn->sess->session_index);
1169                 spin_unlock_bh(&sess_idr_lock);
1170         }
1171         kfree(conn->sess->sess_ops);
1172         kfree(conn->sess);
1173
1174 old_sess_out:
1175         iscsi_stop_login_thread_timer(np);
1176         /*
1177          * If login negotiation fails check if the Time2Retain timer
1178          * needs to be restarted.
1179          */
1180         if (!zero_tsih && conn->sess) {
1181                 spin_lock_bh(&conn->sess->conn_lock);
1182                 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1183                         struct se_portal_group *se_tpg =
1184                                         &conn->tpg->tpg_se_tpg;
1185
1186                         atomic_set(&conn->sess->session_continuation, 0);
1187                         spin_unlock_bh(&conn->sess->conn_lock);
1188                         spin_lock_bh(&se_tpg->session_lock);
1189                         iscsit_start_time2retain_handler(conn->sess);
1190                         spin_unlock_bh(&se_tpg->session_lock);
1191                 } else
1192                         spin_unlock_bh(&conn->sess->conn_lock);
1193                 iscsit_dec_session_usage_count(conn->sess);
1194         }
1195
1196         if (!IS_ERR(conn->conn_rx_hash.tfm))
1197                 crypto_free_hash(conn->conn_rx_hash.tfm);
1198         if (!IS_ERR(conn->conn_tx_hash.tfm))
1199                 crypto_free_hash(conn->conn_tx_hash.tfm);
1200
1201         if (conn->conn_cpumask)
1202                 free_cpumask_var(conn->conn_cpumask);
1203
1204         kfree(conn->conn_ops);
1205
1206         if (conn->param_list) {
1207                 iscsi_release_param_list(conn->param_list);
1208                 conn->param_list = NULL;
1209         }
1210         iscsi_target_nego_release(conn);
1211
1212         if (conn->sock) {
1213                 sock_release(conn->sock);
1214                 conn->sock = NULL;
1215         }
1216
1217         if (conn->conn_transport->iscsit_free_conn)
1218                 conn->conn_transport->iscsit_free_conn(conn);
1219
1220         iscsit_put_transport(conn->conn_transport);
1221         kfree(conn);
1222 }
1223
1224 static int __iscsi_target_login_thread(struct iscsi_np *np)
1225 {
1226         u8 *buffer, zero_tsih = 0;
1227         int ret = 0, rc, stop;
1228         struct iscsi_conn *conn = NULL;
1229         struct iscsi_login *login;
1230         struct iscsi_portal_group *tpg = NULL;
1231         struct iscsi_login_req *pdu;
1232         struct iscsi_tpg_np *tpg_np;
1233         bool new_sess = false;
1234
1235         flush_signals(current);
1236
1237         spin_lock_bh(&np->np_thread_lock);
1238         if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1239                 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1240                 complete(&np->np_restart_comp);
1241         } else {
1242                 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1243         }
1244         spin_unlock_bh(&np->np_thread_lock);
1245
1246         conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1247         if (!conn) {
1248                 pr_err("Could not allocate memory for"
1249                         " new connection\n");
1250                 /* Get another socket */
1251                 return 1;
1252         }
1253         pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1254         conn->conn_state = TARG_CONN_STATE_FREE;
1255
1256         if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1257                 kfree(conn);
1258                 return 1;
1259         }
1260
1261         rc = np->np_transport->iscsit_accept_np(np, conn);
1262         if (rc == -ENOSYS) {
1263                 complete(&np->np_restart_comp);
1264                 iscsit_put_transport(conn->conn_transport);
1265                 kfree(conn);
1266                 conn = NULL;
1267                 goto exit;
1268         } else if (rc < 0) {
1269                 spin_lock_bh(&np->np_thread_lock);
1270                 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1271                         spin_unlock_bh(&np->np_thread_lock);
1272                         complete(&np->np_restart_comp);
1273                         iscsit_put_transport(conn->conn_transport);
1274                         kfree(conn);
1275                         conn = NULL;
1276                         if (ret == -ENODEV)
1277                                 goto out;
1278                         /* Get another socket */
1279                         return 1;
1280                 }
1281                 spin_unlock_bh(&np->np_thread_lock);
1282                 iscsit_put_transport(conn->conn_transport);
1283                 kfree(conn);
1284                 conn = NULL;
1285                 goto out;
1286         }
1287         /*
1288          * Perform the remaining iSCSI connection initialization items..
1289          */
1290         login = iscsi_login_init_conn(conn);
1291         if (!login) {
1292                 goto new_sess_out;
1293         }
1294
1295         iscsi_start_login_thread_timer(np);
1296
1297         pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1298         conn->conn_state = TARG_CONN_STATE_XPT_UP;
1299         /*
1300          * This will process the first login request + payload..
1301          */
1302         rc = np->np_transport->iscsit_get_login_rx(conn, login);
1303         if (rc == 1)
1304                 return 1;
1305         else if (rc < 0)
1306                 goto new_sess_out;
1307
1308         buffer = &login->req[0];
1309         pdu = (struct iscsi_login_req *)buffer;
1310         /*
1311          * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1312          * when Status-Class != 0.
1313         */
1314         conn->login_itt = pdu->itt;
1315
1316         spin_lock_bh(&np->np_thread_lock);
1317         if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1318                 spin_unlock_bh(&np->np_thread_lock);
1319                 pr_err("iSCSI Network Portal on %s:%hu currently not"
1320                         " active.\n", np->np_ip, np->np_port);
1321                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1322                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1323                 goto new_sess_out;
1324         }
1325         spin_unlock_bh(&np->np_thread_lock);
1326
1327         conn->network_transport = np->np_network_transport;
1328
1329         pr_debug("Received iSCSI login request from %s on %s Network"
1330                 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1331                 conn->local_ip, conn->local_port);
1332
1333         pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1334         conn->conn_state        = TARG_CONN_STATE_IN_LOGIN;
1335
1336         if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1337                         pdu->min_version) < 0)
1338                 goto new_sess_out;
1339
1340         zero_tsih = (pdu->tsih == 0x0000);
1341         if (zero_tsih) {
1342                 /*
1343                  * This is the leading connection of a new session.
1344                  * We wait until after authentication to check for
1345                  * session reinstatement.
1346                  */
1347                 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1348                         goto new_sess_out;
1349         } else {
1350                 /*
1351                  * Add a new connection to an existing session.
1352                  * We check for a non-existant session in
1353                  * iscsi_login_non_zero_tsih_s2() below based
1354                  * on ISID/TSIH, but wait until after authentication
1355                  * to check for connection reinstatement, etc.
1356                  */
1357                 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1358                         goto new_sess_out;
1359         }
1360         /*
1361          * SessionType: Discovery
1362          *
1363          *      Locates Default Portal
1364          *
1365          * SessionType: Normal
1366          *
1367          *      Locates Target Portal from NP -> Target IQN
1368          */
1369         rc = iscsi_target_locate_portal(np, conn, login);
1370         if (rc < 0) {
1371                 tpg = conn->tpg;
1372                 goto new_sess_out;
1373         }
1374         login->zero_tsih = zero_tsih;
1375
1376         tpg = conn->tpg;
1377         if (!tpg) {
1378                 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1379                 goto new_sess_out;
1380         }
1381
1382         if (zero_tsih) {
1383                 if (iscsi_login_zero_tsih_s2(conn) < 0)
1384                         goto new_sess_out;
1385         } else {
1386                 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1387                         goto old_sess_out;
1388         }
1389
1390         ret = iscsi_target_start_negotiation(login, conn);
1391         if (ret < 0)
1392                 goto new_sess_out;
1393
1394         if (!conn->sess) {
1395                 pr_err("struct iscsi_conn session pointer is NULL!\n");
1396                 goto new_sess_out;
1397         }
1398
1399         iscsi_stop_login_thread_timer(np);
1400
1401         if (signal_pending(current))
1402                 goto new_sess_out;
1403
1404         if (ret == 1) {
1405                 tpg_np = conn->tpg_np;
1406
1407                 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1408                 if (ret < 0)
1409                         goto new_sess_out;
1410
1411                 iscsit_deaccess_np(np, tpg, tpg_np);
1412         }
1413
1414         tpg = NULL;
1415         tpg_np = NULL;
1416         /* Get another socket */
1417         return 1;
1418
1419 new_sess_out:
1420         new_sess = true;
1421 old_sess_out:
1422         tpg_np = conn->tpg_np;
1423         iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1424         new_sess = false;
1425
1426         if (tpg) {
1427                 iscsit_deaccess_np(np, tpg, tpg_np);
1428                 tpg = NULL;
1429                 tpg_np = NULL;
1430         }
1431
1432 out:
1433         stop = kthread_should_stop();
1434         /* Wait for another socket.. */
1435         if (!stop)
1436                 return 1;
1437 exit:
1438         iscsi_stop_login_thread_timer(np);
1439         spin_lock_bh(&np->np_thread_lock);
1440         np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1441         spin_unlock_bh(&np->np_thread_lock);
1442
1443         return 0;
1444 }
1445
1446 int iscsi_target_login_thread(void *arg)
1447 {
1448         struct iscsi_np *np = arg;
1449         int ret;
1450
1451         allow_signal(SIGINT);
1452
1453         while (!kthread_should_stop()) {
1454                 ret = __iscsi_target_login_thread(np);
1455                 /*
1456                  * We break and exit here unless another sock_accept() call
1457                  * is expected.
1458                  */
1459                 if (ret != 1)
1460                         break;
1461         }
1462
1463         return 0;
1464 }