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