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