1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file contains main functions related to iSCSI Parameter negotiation.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 ******************************************************************************/
11 #include <linux/ctype.h>
12 #include <linux/kthread.h>
13 #include <linux/slab.h>
14 #include <linux/sched/signal.h>
16 #include <scsi/iscsi_proto.h>
17 #include <target/target_core_base.h>
18 #include <target/target_core_fabric.h>
19 #include <target/iscsi/iscsi_transport.h>
21 #include <target/iscsi/iscsi_target_core.h>
22 #include "iscsi_target_parameters.h"
23 #include "iscsi_target_login.h"
24 #include "iscsi_target_nego.h"
25 #include "iscsi_target_tpg.h"
26 #include "iscsi_target_util.h"
27 #include "iscsi_target.h"
28 #include "iscsi_target_auth.h"
30 #define MAX_LOGIN_PDUS 7
33 void convert_null_to_semi(char *buf
, int len
)
37 for (i
= 0; i
< len
; i
++)
42 static int strlen_semi(char *buf
)
46 while (buf
[i
] != '\0') {
58 unsigned int max_length
,
65 if (!in_buf
|| !pattern
|| !out_buf
|| !type
)
68 ptr
= strstr(in_buf
, pattern
);
72 ptr
= strstr(ptr
, "=");
77 if (*ptr
== '0' && (*(ptr
+1) == 'x' || *(ptr
+1) == 'X')) {
78 ptr
+= 2; /* skip 0x */
83 len
= strlen_semi(ptr
);
87 if (len
>= max_length
) {
88 pr_err("Length of input: %d exceeds max_length:"
89 " %d\n", len
, max_length
);
92 memcpy(out_buf
, ptr
, len
);
98 static u32
iscsi_handle_authentication(
99 struct iscsi_conn
*conn
,
104 unsigned char *authtype
)
106 struct iscsi_session
*sess
= conn
->sess
;
107 struct iscsi_node_auth
*auth
;
108 struct iscsi_node_acl
*iscsi_nacl
;
109 struct iscsi_portal_group
*iscsi_tpg
;
110 struct se_node_acl
*se_nacl
;
112 if (!sess
->sess_ops
->SessionType
) {
114 * For SessionType=Normal
116 se_nacl
= conn
->sess
->se_sess
->se_node_acl
;
118 pr_err("Unable to locate struct se_node_acl for"
122 iscsi_nacl
= container_of(se_nacl
, struct iscsi_node_acl
,
125 pr_err("Unable to locate struct iscsi_node_acl for"
130 if (se_nacl
->dynamic_node_acl
) {
131 iscsi_tpg
= container_of(se_nacl
->se_tpg
,
132 struct iscsi_portal_group
, tpg_se_tpg
);
134 auth
= &iscsi_tpg
->tpg_demo_auth
;
136 iscsi_nacl
= container_of(se_nacl
, struct iscsi_node_acl
,
139 auth
= &iscsi_nacl
->node_auth
;
143 * For SessionType=Discovery
145 auth
= &iscsit_global
->discovery_acl
.node_auth
;
148 if (strstr("CHAP", authtype
))
149 strcpy(conn
->sess
->auth_type
, "CHAP");
151 strcpy(conn
->sess
->auth_type
, NONE
);
153 if (strstr("None", authtype
))
155 else if (strstr("CHAP", authtype
))
156 return chap_main_loop(conn
, auth
, in_buf
, out_buf
,
157 &in_length
, out_length
);
158 /* SRP, SPKM1, SPKM2 and KRB5 are unsupported */
162 static void iscsi_remove_failed_auth_entry(struct iscsi_conn
*conn
)
164 kfree(conn
->auth_protocol
);
167 int iscsi_target_check_login_request(
168 struct iscsi_conn
*conn
,
169 struct iscsi_login
*login
)
171 int req_csg
, req_nsg
;
173 struct iscsi_login_req
*login_req
;
175 login_req
= (struct iscsi_login_req
*) login
->req
;
176 payload_length
= ntoh24(login_req
->dlength
);
178 switch (login_req
->opcode
& ISCSI_OPCODE_MASK
) {
182 pr_err("Received unknown opcode 0x%02x.\n",
183 login_req
->opcode
& ISCSI_OPCODE_MASK
);
184 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
185 ISCSI_LOGIN_STATUS_INIT_ERR
);
189 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_CONTINUE
) &&
190 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
)) {
191 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
192 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
193 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
194 ISCSI_LOGIN_STATUS_INIT_ERR
);
198 req_csg
= ISCSI_LOGIN_CURRENT_STAGE(login_req
->flags
);
199 req_nsg
= ISCSI_LOGIN_NEXT_STAGE(login_req
->flags
);
201 if (req_csg
!= login
->current_stage
) {
202 pr_err("Initiator unexpectedly changed login stage"
203 " from %d to %d, login failed.\n", login
->current_stage
,
205 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
206 ISCSI_LOGIN_STATUS_INIT_ERR
);
210 if ((req_nsg
== 2) || (req_csg
>= 2) ||
211 ((login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
) &&
212 (req_nsg
<= req_csg
))) {
213 pr_err("Illegal login_req->flags Combination, CSG: %d,"
214 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg
,
215 req_nsg
, (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
));
216 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
217 ISCSI_LOGIN_STATUS_INIT_ERR
);
221 if ((login_req
->max_version
!= login
->version_max
) ||
222 (login_req
->min_version
!= login
->version_min
)) {
223 pr_err("Login request changed Version Max/Nin"
224 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
225 login_req
->max_version
, login_req
->min_version
);
226 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
227 ISCSI_LOGIN_STATUS_INIT_ERR
);
231 if (memcmp(login_req
->isid
, login
->isid
, 6) != 0) {
232 pr_err("Login request changed ISID unexpectedly,"
233 " protocol error.\n");
234 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
235 ISCSI_LOGIN_STATUS_INIT_ERR
);
239 if (login_req
->itt
!= login
->init_task_tag
) {
240 pr_err("Login request changed ITT unexpectedly to"
241 " 0x%08x, protocol error.\n", login_req
->itt
);
242 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
243 ISCSI_LOGIN_STATUS_INIT_ERR
);
247 if (payload_length
> MAX_KEY_VALUE_PAIRS
) {
248 pr_err("Login request payload exceeds default"
249 " MaxRecvDataSegmentLength: %u, protocol error.\n",
250 MAX_KEY_VALUE_PAIRS
);
256 EXPORT_SYMBOL(iscsi_target_check_login_request
);
258 static int iscsi_target_check_first_request(
259 struct iscsi_conn
*conn
,
260 struct iscsi_login
*login
)
262 struct iscsi_param
*param
= NULL
;
263 struct se_node_acl
*se_nacl
;
265 login
->first_request
= 0;
267 list_for_each_entry(param
, &conn
->param_list
->param_list
, p_list
) {
268 if (!strncmp(param
->name
, SESSIONTYPE
, 11)) {
269 if (!IS_PSTATE_ACCEPTOR(param
)) {
270 pr_err("SessionType key not received"
271 " in first login request.\n");
272 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
273 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
276 if (!strncmp(param
->value
, DISCOVERY
, 9))
280 if (!strncmp(param
->name
, INITIATORNAME
, 13)) {
281 if (!IS_PSTATE_ACCEPTOR(param
)) {
282 if (!login
->leading_connection
)
285 pr_err("InitiatorName key not received"
286 " in first login request.\n");
287 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
288 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
293 * For non-leading connections, double check that the
294 * received InitiatorName matches the existing session's
295 * struct iscsi_node_acl.
297 if (!login
->leading_connection
) {
298 se_nacl
= conn
->sess
->se_sess
->se_node_acl
;
300 pr_err("Unable to locate"
301 " struct se_node_acl\n");
302 iscsit_tx_login_rsp(conn
,
303 ISCSI_STATUS_CLS_INITIATOR_ERR
,
304 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND
);
308 if (strcmp(param
->value
,
309 se_nacl
->initiatorname
)) {
311 " InitiatorName: %s for this"
312 " iSCSI Initiator Node.\n",
314 iscsit_tx_login_rsp(conn
,
315 ISCSI_STATUS_CLS_INITIATOR_ERR
,
316 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND
);
326 static int iscsi_target_do_tx_login_io(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
329 struct iscsi_login_rsp
*login_rsp
;
331 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
333 login_rsp
->opcode
= ISCSI_OP_LOGIN_RSP
;
334 hton24(login_rsp
->dlength
, login
->rsp_length
);
335 memcpy(login_rsp
->isid
, login
->isid
, 6);
336 login_rsp
->tsih
= cpu_to_be16(login
->tsih
);
337 login_rsp
->itt
= login
->init_task_tag
;
338 login_rsp
->statsn
= cpu_to_be32(conn
->stat_sn
++);
339 login_rsp
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
340 login_rsp
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
342 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
343 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
344 " %u\n", login_rsp
->flags
, (__force u32
)login_rsp
->itt
,
345 ntohl(login_rsp
->exp_cmdsn
), ntohl(login_rsp
->max_cmdsn
),
346 ntohl(login_rsp
->statsn
), login
->rsp_length
);
348 padding
= ((-login
->rsp_length
) & 3);
350 * Before sending the last login response containing the transition
351 * bit for full-feature-phase, go ahead and start up TX/RX threads
352 * now to avoid potential resource allocation failures after the
353 * final login response has been sent.
355 if (login
->login_complete
) {
356 int rc
= iscsit_start_kthreads(conn
);
358 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
359 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
364 if (conn
->conn_transport
->iscsit_put_login_tx(conn
, login
,
365 login
->rsp_length
+ padding
) < 0)
368 login
->rsp_length
= 0;
373 if (login
->login_complete
) {
374 if (conn
->rx_thread
&& conn
->rx_thread_active
) {
375 send_sig(SIGINT
, conn
->rx_thread
, 1);
376 complete(&conn
->rx_login_comp
);
377 kthread_stop(conn
->rx_thread
);
379 if (conn
->tx_thread
&& conn
->tx_thread_active
) {
380 send_sig(SIGINT
, conn
->tx_thread
, 1);
381 kthread_stop(conn
->tx_thread
);
383 spin_lock(&iscsit_global
->ts_bitmap_lock
);
384 bitmap_release_region(iscsit_global
->ts_bitmap
, conn
->bitmap_id
,
386 spin_unlock(&iscsit_global
->ts_bitmap_lock
);
391 static void iscsi_target_sk_data_ready(struct sock
*sk
)
393 struct iscsi_conn
*conn
= sk
->sk_user_data
;
396 pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn
);
398 write_lock_bh(&sk
->sk_callback_lock
);
399 if (!sk
->sk_user_data
) {
400 write_unlock_bh(&sk
->sk_callback_lock
);
403 if (!test_bit(LOGIN_FLAGS_READY
, &conn
->login_flags
)) {
404 write_unlock_bh(&sk
->sk_callback_lock
);
405 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn
);
408 if (test_bit(LOGIN_FLAGS_CLOSED
, &conn
->login_flags
)) {
409 write_unlock_bh(&sk
->sk_callback_lock
);
410 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn
);
413 if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE
, &conn
->login_flags
)) {
414 write_unlock_bh(&sk
->sk_callback_lock
);
415 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn
);
416 if (iscsi_target_sk_data_ready
== conn
->orig_data_ready
)
418 conn
->orig_data_ready(sk
);
422 rc
= schedule_delayed_work(&conn
->login_work
, 0);
424 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
427 write_unlock_bh(&sk
->sk_callback_lock
);
430 static void iscsi_target_sk_state_change(struct sock
*);
432 static void iscsi_target_set_sock_callbacks(struct iscsi_conn
*conn
)
440 pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn
);
442 write_lock_bh(&sk
->sk_callback_lock
);
443 sk
->sk_user_data
= conn
;
444 conn
->orig_data_ready
= sk
->sk_data_ready
;
445 conn
->orig_state_change
= sk
->sk_state_change
;
446 sk
->sk_data_ready
= iscsi_target_sk_data_ready
;
447 sk
->sk_state_change
= iscsi_target_sk_state_change
;
448 write_unlock_bh(&sk
->sk_callback_lock
);
450 sk
->sk_sndtimeo
= TA_LOGIN_TIMEOUT
* HZ
;
451 sk
->sk_rcvtimeo
= TA_LOGIN_TIMEOUT
* HZ
;
454 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn
*conn
)
462 pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn
);
464 write_lock_bh(&sk
->sk_callback_lock
);
465 if (!sk
->sk_user_data
) {
466 write_unlock_bh(&sk
->sk_callback_lock
);
469 sk
->sk_user_data
= NULL
;
470 sk
->sk_data_ready
= conn
->orig_data_ready
;
471 sk
->sk_state_change
= conn
->orig_state_change
;
472 write_unlock_bh(&sk
->sk_callback_lock
);
474 sk
->sk_sndtimeo
= MAX_SCHEDULE_TIMEOUT
;
475 sk
->sk_rcvtimeo
= MAX_SCHEDULE_TIMEOUT
;
478 static int iscsi_target_do_login(struct iscsi_conn
*, struct iscsi_login
*);
480 static bool __iscsi_target_sk_check_close(struct sock
*sk
)
482 if (sk
->sk_state
== TCP_CLOSE_WAIT
|| sk
->sk_state
== TCP_CLOSE
) {
483 pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
484 "returning FALSE\n");
490 static bool iscsi_target_sk_check_close(struct iscsi_conn
*conn
)
495 struct sock
*sk
= conn
->sock
->sk
;
497 read_lock_bh(&sk
->sk_callback_lock
);
498 state
= (__iscsi_target_sk_check_close(sk
) ||
499 test_bit(LOGIN_FLAGS_CLOSED
, &conn
->login_flags
));
500 read_unlock_bh(&sk
->sk_callback_lock
);
505 static bool iscsi_target_sk_check_flag(struct iscsi_conn
*conn
, unsigned int flag
)
510 struct sock
*sk
= conn
->sock
->sk
;
512 read_lock_bh(&sk
->sk_callback_lock
);
513 state
= test_bit(flag
, &conn
->login_flags
);
514 read_unlock_bh(&sk
->sk_callback_lock
);
519 static bool iscsi_target_sk_check_and_clear(struct iscsi_conn
*conn
, unsigned int flag
)
524 struct sock
*sk
= conn
->sock
->sk
;
526 write_lock_bh(&sk
->sk_callback_lock
);
527 state
= (__iscsi_target_sk_check_close(sk
) ||
528 test_bit(LOGIN_FLAGS_CLOSED
, &conn
->login_flags
));
530 clear_bit(flag
, &conn
->login_flags
);
531 write_unlock_bh(&sk
->sk_callback_lock
);
536 static void iscsi_target_login_drop(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
538 struct iscsi_np
*np
= login
->np
;
539 bool zero_tsih
= login
->zero_tsih
;
541 iscsi_remove_failed_auth_entry(conn
);
542 iscsi_target_nego_release(conn
);
543 iscsi_target_login_sess_out(conn
, np
, zero_tsih
, true);
546 struct conn_timeout
{
547 struct timer_list timer
;
548 struct iscsi_conn
*conn
;
551 static void iscsi_target_login_timeout(struct timer_list
*t
)
553 struct conn_timeout
*timeout
= from_timer(timeout
, t
, timer
);
554 struct iscsi_conn
*conn
= timeout
->conn
;
556 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
558 if (conn
->login_kworker
) {
559 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
560 conn
->login_kworker
->comm
, conn
->login_kworker
->pid
);
561 send_sig(SIGINT
, conn
->login_kworker
, 1);
565 static void iscsi_target_do_login_rx(struct work_struct
*work
)
567 struct iscsi_conn
*conn
= container_of(work
,
568 struct iscsi_conn
, login_work
.work
);
569 struct iscsi_login
*login
= conn
->login
;
570 struct iscsi_np
*np
= login
->np
;
571 struct iscsi_portal_group
*tpg
= conn
->tpg
;
572 struct iscsi_tpg_np
*tpg_np
= conn
->tpg_np
;
573 struct conn_timeout timeout
;
574 int rc
, zero_tsih
= login
->zero_tsih
;
577 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
578 conn
, current
->comm
, current
->pid
);
580 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
581 * before initial PDU processing in iscsi_target_start_negotiation()
582 * has completed, go ahead and retry until it's cleared.
584 * Otherwise if the TCP connection drops while this is occuring,
585 * iscsi_target_start_negotiation() will detect the failure, call
586 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
587 * remaining iscsi connection resources from iscsi_np process context.
589 if (iscsi_target_sk_check_flag(conn
, LOGIN_FLAGS_INITIAL_PDU
)) {
590 schedule_delayed_work(&conn
->login_work
, msecs_to_jiffies(10));
594 spin_lock(&tpg
->tpg_state_lock
);
595 state
= (tpg
->tpg_state
== TPG_STATE_ACTIVE
);
596 spin_unlock(&tpg
->tpg_state_lock
);
599 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
603 if (iscsi_target_sk_check_close(conn
)) {
604 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
608 conn
->login_kworker
= current
;
609 allow_signal(SIGINT
);
612 timer_setup_on_stack(&timeout
.timer
, iscsi_target_login_timeout
, 0);
613 mod_timer(&timeout
.timer
, jiffies
+ TA_LOGIN_TIMEOUT
* HZ
);
614 pr_debug("Starting login timer for %s/%d\n", current
->comm
, current
->pid
);
616 rc
= conn
->conn_transport
->iscsit_get_login_rx(conn
, login
);
617 del_timer_sync(&timeout
.timer
);
618 destroy_timer_on_stack(&timeout
.timer
);
619 flush_signals(current
);
620 conn
->login_kworker
= NULL
;
625 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
626 conn
, current
->comm
, current
->pid
);
628 rc
= iscsi_target_do_login(conn
, login
);
632 if (iscsi_target_sk_check_and_clear(conn
, LOGIN_FLAGS_READ_ACTIVE
))
634 } else if (rc
== 1) {
635 iscsi_target_nego_release(conn
);
636 iscsi_post_login_handler(np
, conn
, zero_tsih
);
637 iscsit_deaccess_np(np
, tpg
, tpg_np
);
642 iscsi_target_restore_sock_callbacks(conn
);
643 iscsi_target_login_drop(conn
, login
);
644 iscsit_deaccess_np(np
, tpg
, tpg_np
);
647 static void iscsi_target_sk_state_change(struct sock
*sk
)
649 struct iscsi_conn
*conn
;
650 void (*orig_state_change
)(struct sock
*);
653 pr_debug("Entering iscsi_target_sk_state_change\n");
655 write_lock_bh(&sk
->sk_callback_lock
);
656 conn
= sk
->sk_user_data
;
658 write_unlock_bh(&sk
->sk_callback_lock
);
661 orig_state_change
= conn
->orig_state_change
;
663 if (!test_bit(LOGIN_FLAGS_READY
, &conn
->login_flags
)) {
664 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
666 write_unlock_bh(&sk
->sk_callback_lock
);
667 orig_state_change(sk
);
670 state
= __iscsi_target_sk_check_close(sk
);
671 pr_debug("__iscsi_target_sk_close_change: state: %d\n", state
);
673 if (test_bit(LOGIN_FLAGS_READ_ACTIVE
, &conn
->login_flags
)) {
674 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
675 " conn: %p\n", conn
);
677 set_bit(LOGIN_FLAGS_CLOSED
, &conn
->login_flags
);
678 write_unlock_bh(&sk
->sk_callback_lock
);
679 orig_state_change(sk
);
682 if (test_bit(LOGIN_FLAGS_CLOSED
, &conn
->login_flags
)) {
683 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
685 write_unlock_bh(&sk
->sk_callback_lock
);
686 orig_state_change(sk
);
690 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
691 * but only queue conn->login_work -> iscsi_target_do_login_rx()
692 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
694 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
695 * will detect the dropped TCP connection from delayed workqueue context.
697 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
698 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
699 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
700 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
701 * dropped TCP connection in iscsi_np process context, and cleaning up
702 * the remaining iscsi connection resources.
705 pr_debug("iscsi_target_sk_state_change got failed state\n");
706 set_bit(LOGIN_FLAGS_CLOSED
, &conn
->login_flags
);
707 state
= test_bit(LOGIN_FLAGS_INITIAL_PDU
, &conn
->login_flags
);
708 write_unlock_bh(&sk
->sk_callback_lock
);
710 orig_state_change(sk
);
713 schedule_delayed_work(&conn
->login_work
, 0);
716 write_unlock_bh(&sk
->sk_callback_lock
);
718 orig_state_change(sk
);
722 * NOTE: We check for existing sessions or connections AFTER the initiator
723 * has been successfully authenticated in order to protect against faked
724 * ISID/TSIH combinations.
726 static int iscsi_target_check_for_existing_instances(
727 struct iscsi_conn
*conn
,
728 struct iscsi_login
*login
)
730 if (login
->checked_for_existing
)
733 login
->checked_for_existing
= 1;
736 return iscsi_check_for_session_reinstatement(conn
);
738 return iscsi_login_post_auth_non_zero_tsih(conn
, login
->cid
,
739 login
->initial_exp_statsn
);
742 static int iscsi_target_do_authentication(
743 struct iscsi_conn
*conn
,
744 struct iscsi_login
*login
)
748 struct iscsi_param
*param
;
749 struct iscsi_login_req
*login_req
;
750 struct iscsi_login_rsp
*login_rsp
;
752 login_req
= (struct iscsi_login_req
*) login
->req
;
753 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
754 payload_length
= ntoh24(login_req
->dlength
);
756 param
= iscsi_find_param_from_key(AUTHMETHOD
, conn
->param_list
);
760 authret
= iscsi_handle_authentication(
769 pr_debug("Received OK response"
770 " from LIO Authentication, continuing.\n");
773 pr_debug("iSCSI security negotiation"
774 " completed successfully.\n");
775 login
->auth_complete
= 1;
776 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_NEXT_STAGE1
) &&
777 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
)) {
778 login_rsp
->flags
|= (ISCSI_FLAG_LOGIN_NEXT_STAGE1
|
779 ISCSI_FLAG_LOGIN_TRANSIT
);
780 login
->current_stage
= 1;
782 return iscsi_target_check_for_existing_instances(
785 pr_err("Security negotiation"
787 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
788 ISCSI_LOGIN_STATUS_AUTH_FAILED
);
791 pr_err("Received unknown error %d from LIO"
792 " Authentication\n", authret
);
793 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
794 ISCSI_LOGIN_STATUS_TARGET_ERROR
);
801 static int iscsi_target_handle_csg_zero(
802 struct iscsi_conn
*conn
,
803 struct iscsi_login
*login
)
807 struct iscsi_param
*param
;
808 struct iscsi_login_req
*login_req
;
809 struct iscsi_login_rsp
*login_rsp
;
811 login_req
= (struct iscsi_login_req
*) login
->req
;
812 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
813 payload_length
= ntoh24(login_req
->dlength
);
815 param
= iscsi_find_param_from_key(AUTHMETHOD
, conn
->param_list
);
819 ret
= iscsi_decode_text_input(
820 PHASE_SECURITY
|PHASE_DECLARATIVE
,
821 SENDER_INITIATOR
|SENDER_RECEIVER
,
829 if (login
->auth_complete
) {
830 pr_err("Initiator has already been"
831 " successfully authenticated, but is still"
832 " sending %s keys.\n", param
->value
);
833 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
834 ISCSI_LOGIN_STATUS_INIT_ERR
);
839 } else if (!payload_length
) {
840 pr_err("Initiator sent zero length security payload,"
842 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
843 ISCSI_LOGIN_STATUS_AUTH_FAILED
);
847 if (login
->first_request
)
848 if (iscsi_target_check_first_request(conn
, login
) < 0)
851 ret
= iscsi_encode_text_output(
852 PHASE_SECURITY
|PHASE_DECLARATIVE
,
857 conn
->tpg
->tpg_attrib
.login_keys_workaround
);
861 if (!iscsi_check_negotiated_keys(conn
->param_list
)) {
862 if (conn
->tpg
->tpg_attrib
.authentication
&&
863 !strncmp(param
->value
, NONE
, 4)) {
864 pr_err("Initiator sent AuthMethod=None but"
865 " Target is enforcing iSCSI Authentication,"
867 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
868 ISCSI_LOGIN_STATUS_AUTH_FAILED
);
872 if (conn
->tpg
->tpg_attrib
.authentication
&&
873 !login
->auth_complete
)
876 if (strncmp(param
->value
, NONE
, 4) && !login
->auth_complete
)
879 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_NEXT_STAGE1
) &&
880 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
)) {
881 login_rsp
->flags
|= ISCSI_FLAG_LOGIN_NEXT_STAGE1
|
882 ISCSI_FLAG_LOGIN_TRANSIT
;
883 login
->current_stage
= 1;
889 return iscsi_target_do_authentication(conn
, login
);
892 static int iscsi_target_handle_csg_one(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
896 struct iscsi_login_req
*login_req
;
897 struct iscsi_login_rsp
*login_rsp
;
899 login_req
= (struct iscsi_login_req
*) login
->req
;
900 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
901 payload_length
= ntoh24(login_req
->dlength
);
903 ret
= iscsi_decode_text_input(
904 PHASE_OPERATIONAL
|PHASE_DECLARATIVE
,
905 SENDER_INITIATOR
|SENDER_RECEIVER
,
910 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
911 ISCSI_LOGIN_STATUS_INIT_ERR
);
915 if (login
->first_request
)
916 if (iscsi_target_check_first_request(conn
, login
) < 0)
919 if (iscsi_target_check_for_existing_instances(conn
, login
) < 0)
922 ret
= iscsi_encode_text_output(
923 PHASE_OPERATIONAL
|PHASE_DECLARATIVE
,
928 conn
->tpg
->tpg_attrib
.login_keys_workaround
);
930 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
931 ISCSI_LOGIN_STATUS_INIT_ERR
);
935 if (!login
->auth_complete
&&
936 conn
->tpg
->tpg_attrib
.authentication
) {
937 pr_err("Initiator is requesting CSG: 1, has not been"
938 " successfully authenticated, and the Target is"
939 " enforcing iSCSI Authentication, login failed.\n");
940 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
941 ISCSI_LOGIN_STATUS_AUTH_FAILED
);
945 if (!iscsi_check_negotiated_keys(conn
->param_list
))
946 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_NEXT_STAGE3
) &&
947 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
))
948 login_rsp
->flags
|= ISCSI_FLAG_LOGIN_NEXT_STAGE3
|
949 ISCSI_FLAG_LOGIN_TRANSIT
;
954 static int iscsi_target_do_login(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
957 struct iscsi_login_req
*login_req
;
958 struct iscsi_login_rsp
*login_rsp
;
960 login_req
= (struct iscsi_login_req
*) login
->req
;
961 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
964 if (++pdu_count
> MAX_LOGIN_PDUS
) {
965 pr_err("MAX_LOGIN_PDUS count reached.\n");
966 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
967 ISCSI_LOGIN_STATUS_TARGET_ERROR
);
971 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req
->flags
)) {
973 login_rsp
->flags
&= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK
;
974 if (iscsi_target_handle_csg_zero(conn
, login
) < 0)
978 login_rsp
->flags
|= ISCSI_FLAG_LOGIN_CURRENT_STAGE1
;
979 if (iscsi_target_handle_csg_one(conn
, login
) < 0)
981 if (login_rsp
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
) {
983 * Check to make sure the TCP connection has not
984 * dropped asynchronously while session reinstatement
985 * was occuring in this kthread context, before
986 * transitioning to full feature phase operation.
988 if (iscsi_target_sk_check_close(conn
))
991 login
->tsih
= conn
->sess
->tsih
;
992 login
->login_complete
= 1;
993 iscsi_target_restore_sock_callbacks(conn
);
994 if (iscsi_target_do_tx_login_io(conn
,
1001 pr_err("Illegal CSG: %d received from"
1002 " Initiator, protocol error.\n",
1003 ISCSI_LOGIN_CURRENT_STAGE(login_req
->flags
));
1007 if (iscsi_target_do_tx_login_io(conn
, login
) < 0)
1010 if (login_rsp
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
) {
1011 login_rsp
->flags
&= ~ISCSI_FLAG_LOGIN_TRANSIT
;
1012 login_rsp
->flags
&= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK
;
1020 static void iscsi_initiatorname_tolower(
1024 u32 iqn_size
= strlen(param_buf
), i
;
1026 for (i
= 0; i
< iqn_size
; i
++) {
1036 * Processes the first Login Request..
1038 int iscsi_target_locate_portal(
1039 struct iscsi_np
*np
,
1040 struct iscsi_conn
*conn
,
1041 struct iscsi_login
*login
)
1043 char *i_buf
= NULL
, *s_buf
= NULL
, *t_buf
= NULL
;
1044 char *tmpbuf
, *start
= NULL
, *end
= NULL
, *key
, *value
;
1045 struct iscsi_session
*sess
= conn
->sess
;
1046 struct iscsi_tiqn
*tiqn
;
1047 struct iscsi_tpg_np
*tpg_np
= NULL
;
1048 struct iscsi_login_req
*login_req
;
1049 struct se_node_acl
*se_nacl
;
1050 u32 payload_length
, queue_depth
= 0;
1051 int sessiontype
= 0, ret
= 0, tag_num
, tag_size
;
1053 INIT_DELAYED_WORK(&conn
->login_work
, iscsi_target_do_login_rx
);
1054 iscsi_target_set_sock_callbacks(conn
);
1058 login_req
= (struct iscsi_login_req
*) login
->req
;
1059 payload_length
= ntoh24(login_req
->dlength
);
1061 tmpbuf
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1063 pr_err("Unable to allocate memory for tmpbuf.\n");
1067 memcpy(tmpbuf
, login
->req_buf
, payload_length
);
1068 tmpbuf
[payload_length
] = '\0';
1070 end
= (start
+ payload_length
);
1073 * Locate the initial keys expected from the Initiator node in
1074 * the first login request in order to progress with the login phase.
1076 while (start
< end
) {
1077 if (iscsi_extract_key_value(start
, &key
, &value
) < 0) {
1082 if (!strncmp(key
, "InitiatorName", 13))
1084 else if (!strncmp(key
, "SessionType", 11))
1086 else if (!strncmp(key
, "TargetName", 10))
1089 start
+= strlen(key
) + strlen(value
) + 2;
1092 * See 5.3. Login Phase.
1095 pr_err("InitiatorName key not received"
1096 " in first login request.\n");
1097 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
1098 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
1103 * Convert the incoming InitiatorName to lowercase following
1104 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1105 * are NOT case sensitive.
1107 iscsi_initiatorname_tolower(i_buf
);
1110 if (!login
->leading_connection
)
1113 pr_err("SessionType key not received"
1114 " in first login request.\n");
1115 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
1116 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
1122 * Use default portal group for discovery sessions.
1124 sessiontype
= strncmp(s_buf
, DISCOVERY
, 9);
1126 conn
->tpg
= iscsit_global
->discovery_tpg
;
1127 if (!login
->leading_connection
)
1130 sess
->sess_ops
->SessionType
= 1;
1132 * Setup crc32c modules from libcrypto
1134 if (iscsi_login_setup_crypto(conn
) < 0) {
1135 pr_err("iscsi_login_setup_crypto() failed\n");
1140 * Serialize access across the discovery struct iscsi_portal_group to
1141 * process login attempt.
1143 if (iscsit_access_np(np
, conn
->tpg
) < 0) {
1144 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1145 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
1155 pr_err("TargetName key not received"
1156 " in first login request while"
1157 " SessionType=Normal.\n");
1158 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
1159 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
1165 * Locate Target IQN from Storage Node.
1167 tiqn
= iscsit_get_tiqn_for_login(t_buf
);
1169 pr_err("Unable to locate Target IQN: %s in"
1170 " Storage Node\n", t_buf
);
1171 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1172 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
1176 pr_debug("Located Storage Object: %s\n", tiqn
->tiqn
);
1179 * Locate Target Portal Group from Storage Node.
1181 conn
->tpg
= iscsit_get_tpg_from_np(tiqn
, np
, &tpg_np
);
1183 pr_err("Unable to locate Target Portal Group"
1184 " on %s\n", tiqn
->tiqn
);
1185 iscsit_put_tiqn_for_login(tiqn
);
1186 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1187 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
1191 conn
->tpg_np
= tpg_np
;
1192 pr_debug("Located Portal Group Object: %hu\n", conn
->tpg
->tpgt
);
1194 * Setup crc32c modules from libcrypto
1196 if (iscsi_login_setup_crypto(conn
) < 0) {
1197 pr_err("iscsi_login_setup_crypto() failed\n");
1198 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
1199 iscsit_put_tiqn_for_login(tiqn
);
1205 * Serialize access across the struct iscsi_portal_group to
1206 * process login attempt.
1208 if (iscsit_access_np(np
, conn
->tpg
) < 0) {
1209 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
1210 iscsit_put_tiqn_for_login(tiqn
);
1211 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1212 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
1219 * conn->sess->node_acl will be set when the referenced
1220 * struct iscsi_session is located from received ISID+TSIH in
1221 * iscsi_login_non_zero_tsih_s2().
1223 if (!login
->leading_connection
) {
1229 * This value is required in iscsi_login_zero_tsih_s2()
1231 sess
->sess_ops
->SessionType
= 0;
1234 * Locate incoming Initiator IQN reference from Storage Node.
1236 sess
->se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1237 &conn
->tpg
->tpg_se_tpg
, i_buf
);
1238 if (!sess
->se_sess
->se_node_acl
) {
1239 pr_err("iSCSI Initiator Node: %s is not authorized to"
1240 " access iSCSI target portal group: %hu.\n",
1241 i_buf
, conn
->tpg
->tpgt
);
1242 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
1243 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN
);
1247 se_nacl
= sess
->se_sess
->se_node_acl
;
1248 queue_depth
= se_nacl
->queue_depth
;
1250 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1251 * depth for non immediate commands, plus extra tags for immediate
1254 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1255 * in per-cpu-ida tag allocation logic + small queue_depth.
1258 tag_num
= max_t(u32
, ISCSIT_MIN_TAGS
, queue_depth
);
1259 tag_num
= (tag_num
* 2) + ISCSIT_EXTRA_TAGS
;
1260 tag_size
= sizeof(struct iscsi_cmd
) + conn
->conn_transport
->priv_size
;
1262 ret
= transport_alloc_session_tags(sess
->se_sess
, tag_num
, tag_size
);
1264 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1265 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
1273 int iscsi_target_start_negotiation(
1274 struct iscsi_login
*login
,
1275 struct iscsi_conn
*conn
)
1280 struct sock
*sk
= conn
->sock
->sk
;
1282 write_lock_bh(&sk
->sk_callback_lock
);
1283 set_bit(LOGIN_FLAGS_READY
, &conn
->login_flags
);
1284 set_bit(LOGIN_FLAGS_INITIAL_PDU
, &conn
->login_flags
);
1285 write_unlock_bh(&sk
->sk_callback_lock
);
1288 * If iscsi_target_do_login returns zero to signal more PDU
1289 * exchanges are required to complete the login, go ahead and
1290 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
1293 * Otherwise if TCP connection dropped asynchronously, go ahead
1294 * and perform connection cleanup now.
1296 ret
= iscsi_target_do_login(conn
, login
);
1297 if (!ret
&& iscsi_target_sk_check_and_clear(conn
, LOGIN_FLAGS_INITIAL_PDU
))
1301 cancel_delayed_work_sync(&conn
->login_work
);
1302 iscsi_target_restore_sock_callbacks(conn
);
1303 iscsi_remove_failed_auth_entry(conn
);
1306 iscsi_target_nego_release(conn
);
1311 void iscsi_target_nego_release(struct iscsi_conn
*conn
)
1313 struct iscsi_login
*login
= conn
->conn_login
;
1318 kfree(login
->req_buf
);
1319 kfree(login
->rsp_buf
);
1322 conn
->conn_login
= NULL
;