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