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