OSDN Git Service

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