1 /*******************************************************************************
2 * This file contains main functions related to iSCSI Parameter negotiation.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ******************************************************************************/
19 #include <linux/ctype.h>
20 #include <linux/kthread.h>
21 #include <linux/slab.h>
22 #include <linux/sched/signal.h>
24 #include <scsi/iscsi_proto.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_fabric.h>
27 #include <target/iscsi/iscsi_transport.h>
29 #include <target/iscsi/iscsi_target_core.h>
30 #include "iscsi_target_parameters.h"
31 #include "iscsi_target_login.h"
32 #include "iscsi_target_nego.h"
33 #include "iscsi_target_tpg.h"
34 #include "iscsi_target_util.h"
35 #include "iscsi_target.h"
36 #include "iscsi_target_auth.h"
38 #define MAX_LOGIN_PDUS 7
41 void convert_null_to_semi(char *buf, int len)
45 for (i = 0; i < len; i++)
50 static int strlen_semi(char *buf)
54 while (buf[i] != '\0') {
66 unsigned int max_length,
73 if (!in_buf || !pattern || !out_buf || !type)
76 ptr = strstr(in_buf, pattern);
80 ptr = strstr(ptr, "=");
85 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
86 ptr += 2; /* skip 0x */
91 len = strlen_semi(ptr);
95 if (len >= max_length) {
96 pr_err("Length of input: %d exceeds max_length:"
97 " %d\n", len, max_length);
100 memcpy(out_buf, ptr, len);
106 static u32 iscsi_handle_authentication(
107 struct iscsi_conn *conn,
112 unsigned char *authtype)
114 struct iscsi_session *sess = conn->sess;
115 struct iscsi_node_auth *auth;
116 struct iscsi_node_acl *iscsi_nacl;
117 struct iscsi_portal_group *iscsi_tpg;
118 struct se_node_acl *se_nacl;
120 if (!sess->sess_ops->SessionType) {
122 * For SessionType=Normal
124 se_nacl = conn->sess->se_sess->se_node_acl;
126 pr_err("Unable to locate struct se_node_acl for"
130 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
133 pr_err("Unable to locate struct iscsi_node_acl for"
138 if (se_nacl->dynamic_node_acl) {
139 iscsi_tpg = container_of(se_nacl->se_tpg,
140 struct iscsi_portal_group, tpg_se_tpg);
142 auth = &iscsi_tpg->tpg_demo_auth;
144 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
147 auth = &iscsi_nacl->node_auth;
151 * For SessionType=Discovery
153 auth = &iscsit_global->discovery_acl.node_auth;
156 if (strstr("CHAP", authtype))
157 strcpy(conn->sess->auth_type, "CHAP");
159 strcpy(conn->sess->auth_type, NONE);
161 if (strstr("None", authtype))
164 else if (strstr("SRP", authtype))
165 return srp_main_loop(conn, auth, in_buf, out_buf,
166 &in_length, out_length);
168 else if (strstr("CHAP", authtype))
169 return chap_main_loop(conn, auth, in_buf, out_buf,
170 &in_length, out_length);
171 else if (strstr("SPKM1", authtype))
173 else if (strstr("SPKM2", authtype))
175 else if (strstr("KRB5", authtype))
181 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
183 kfree(conn->auth_protocol);
186 int iscsi_target_check_login_request(
187 struct iscsi_conn *conn,
188 struct iscsi_login *login)
190 int req_csg, req_nsg;
192 struct iscsi_login_req *login_req;
194 login_req = (struct iscsi_login_req *) login->req;
195 payload_length = ntoh24(login_req->dlength);
197 switch (login_req->opcode & ISCSI_OPCODE_MASK) {
201 pr_err("Received unknown opcode 0x%02x.\n",
202 login_req->opcode & ISCSI_OPCODE_MASK);
203 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
204 ISCSI_LOGIN_STATUS_INIT_ERR);
208 if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
209 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
210 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
211 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
212 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
213 ISCSI_LOGIN_STATUS_INIT_ERR);
217 req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
218 req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
220 if (req_csg != login->current_stage) {
221 pr_err("Initiator unexpectedly changed login stage"
222 " from %d to %d, login failed.\n", login->current_stage,
224 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
225 ISCSI_LOGIN_STATUS_INIT_ERR);
229 if ((req_nsg == 2) || (req_csg >= 2) ||
230 ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
231 (req_nsg <= req_csg))) {
232 pr_err("Illegal login_req->flags Combination, CSG: %d,"
233 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
234 req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
235 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
236 ISCSI_LOGIN_STATUS_INIT_ERR);
240 if ((login_req->max_version != login->version_max) ||
241 (login_req->min_version != login->version_min)) {
242 pr_err("Login request changed Version Max/Nin"
243 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
244 login_req->max_version, login_req->min_version);
245 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
246 ISCSI_LOGIN_STATUS_INIT_ERR);
250 if (memcmp(login_req->isid, login->isid, 6) != 0) {
251 pr_err("Login request changed ISID unexpectedly,"
252 " protocol error.\n");
253 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
254 ISCSI_LOGIN_STATUS_INIT_ERR);
258 if (login_req->itt != login->init_task_tag) {
259 pr_err("Login request changed ITT unexpectedly to"
260 " 0x%08x, protocol error.\n", login_req->itt);
261 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
262 ISCSI_LOGIN_STATUS_INIT_ERR);
266 if (payload_length > MAX_KEY_VALUE_PAIRS) {
267 pr_err("Login request payload exceeds default"
268 " MaxRecvDataSegmentLength: %u, protocol error.\n",
269 MAX_KEY_VALUE_PAIRS);
275 EXPORT_SYMBOL(iscsi_target_check_login_request);
277 static int iscsi_target_check_first_request(
278 struct iscsi_conn *conn,
279 struct iscsi_login *login)
281 struct iscsi_param *param = NULL;
282 struct se_node_acl *se_nacl;
284 login->first_request = 0;
286 list_for_each_entry(param, &conn->param_list->param_list, p_list) {
287 if (!strncmp(param->name, SESSIONTYPE, 11)) {
288 if (!IS_PSTATE_ACCEPTOR(param)) {
289 pr_err("SessionType key not received"
290 " in first login request.\n");
291 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
292 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
295 if (!strncmp(param->value, DISCOVERY, 9))
299 if (!strncmp(param->name, INITIATORNAME, 13)) {
300 if (!IS_PSTATE_ACCEPTOR(param)) {
301 if (!login->leading_connection)
304 pr_err("InitiatorName key not received"
305 " in first login request.\n");
306 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
307 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
312 * For non-leading connections, double check that the
313 * received InitiatorName matches the existing session's
314 * struct iscsi_node_acl.
316 if (!login->leading_connection) {
317 se_nacl = conn->sess->se_sess->se_node_acl;
319 pr_err("Unable to locate"
320 " struct se_node_acl\n");
321 iscsit_tx_login_rsp(conn,
322 ISCSI_STATUS_CLS_INITIATOR_ERR,
323 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
327 if (strcmp(param->value,
328 se_nacl->initiatorname)) {
330 " InitiatorName: %s for this"
331 " iSCSI Initiator Node.\n",
333 iscsit_tx_login_rsp(conn,
334 ISCSI_STATUS_CLS_INITIATOR_ERR,
335 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
345 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
348 struct iscsi_login_rsp *login_rsp;
350 login_rsp = (struct iscsi_login_rsp *) login->rsp;
352 login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
353 hton24(login_rsp->dlength, login->rsp_length);
354 memcpy(login_rsp->isid, login->isid, 6);
355 login_rsp->tsih = cpu_to_be16(login->tsih);
356 login_rsp->itt = login->init_task_tag;
357 login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
358 login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
359 login_rsp->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
361 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
362 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
363 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
364 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
365 ntohl(login_rsp->statsn), login->rsp_length);
367 padding = ((-login->rsp_length) & 3);
369 * Before sending the last login response containing the transition
370 * bit for full-feature-phase, go ahead and start up TX/RX threads
371 * now to avoid potential resource allocation failures after the
372 * final login response has been sent.
374 if (login->login_complete) {
375 int rc = iscsit_start_kthreads(conn);
377 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
378 ISCSI_LOGIN_STATUS_NO_RESOURCES);
383 if (conn->conn_transport->iscsit_put_login_tx(conn, login,
384 login->rsp_length + padding) < 0)
387 login->rsp_length = 0;
392 if (login->login_complete) {
393 if (conn->rx_thread && conn->rx_thread_active) {
394 send_sig(SIGINT, conn->rx_thread, 1);
395 complete(&conn->rx_login_comp);
396 kthread_stop(conn->rx_thread);
398 if (conn->tx_thread && conn->tx_thread_active) {
399 send_sig(SIGINT, conn->tx_thread, 1);
400 kthread_stop(conn->tx_thread);
402 spin_lock(&iscsit_global->ts_bitmap_lock);
403 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
405 spin_unlock(&iscsit_global->ts_bitmap_lock);
410 static void iscsi_target_sk_data_ready(struct sock *sk)
412 struct iscsi_conn *conn = sk->sk_user_data;
415 pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
417 write_lock_bh(&sk->sk_callback_lock);
418 if (!sk->sk_user_data) {
419 write_unlock_bh(&sk->sk_callback_lock);
422 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
423 write_unlock_bh(&sk->sk_callback_lock);
424 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
427 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
428 write_unlock_bh(&sk->sk_callback_lock);
429 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
432 if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
433 write_unlock_bh(&sk->sk_callback_lock);
434 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
435 if (iscsi_target_sk_data_ready == conn->orig_data_ready)
437 conn->orig_data_ready(sk);
441 rc = schedule_delayed_work(&conn->login_work, 0);
443 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
446 write_unlock_bh(&sk->sk_callback_lock);
449 static void iscsi_target_sk_state_change(struct sock *);
451 static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
459 pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
461 write_lock_bh(&sk->sk_callback_lock);
462 sk->sk_user_data = conn;
463 conn->orig_data_ready = sk->sk_data_ready;
464 conn->orig_state_change = sk->sk_state_change;
465 sk->sk_data_ready = iscsi_target_sk_data_ready;
466 sk->sk_state_change = iscsi_target_sk_state_change;
467 write_unlock_bh(&sk->sk_callback_lock);
469 sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
470 sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
473 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
481 pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
483 write_lock_bh(&sk->sk_callback_lock);
484 if (!sk->sk_user_data) {
485 write_unlock_bh(&sk->sk_callback_lock);
488 sk->sk_user_data = NULL;
489 sk->sk_data_ready = conn->orig_data_ready;
490 sk->sk_state_change = conn->orig_state_change;
491 write_unlock_bh(&sk->sk_callback_lock);
493 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
494 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
497 static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
499 static bool __iscsi_target_sk_check_close(struct sock *sk)
501 if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
502 pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
503 "returning FALSE\n");
509 static bool iscsi_target_sk_check_close(struct iscsi_conn *conn)
514 struct sock *sk = conn->sock->sk;
516 read_lock_bh(&sk->sk_callback_lock);
517 state = (__iscsi_target_sk_check_close(sk) ||
518 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
519 read_unlock_bh(&sk->sk_callback_lock);
524 static bool iscsi_target_sk_check_flag(struct iscsi_conn *conn, unsigned int flag)
529 struct sock *sk = conn->sock->sk;
531 read_lock_bh(&sk->sk_callback_lock);
532 state = test_bit(flag, &conn->login_flags);
533 read_unlock_bh(&sk->sk_callback_lock);
538 static bool iscsi_target_sk_check_and_clear(struct iscsi_conn *conn, unsigned int flag)
543 struct sock *sk = conn->sock->sk;
545 write_lock_bh(&sk->sk_callback_lock);
546 state = (__iscsi_target_sk_check_close(sk) ||
547 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
549 clear_bit(flag, &conn->login_flags);
550 write_unlock_bh(&sk->sk_callback_lock);
555 static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
557 bool zero_tsih = login->zero_tsih;
559 iscsi_remove_failed_auth_entry(conn);
560 iscsi_target_nego_release(conn);
561 iscsi_target_login_sess_out(conn, zero_tsih, true);
564 static void iscsi_target_login_timeout(unsigned long data)
566 struct iscsi_conn *conn = (struct iscsi_conn *)data;
568 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
570 if (conn->login_kworker) {
571 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
572 conn->login_kworker->comm, conn->login_kworker->pid);
573 send_sig(SIGINT, conn->login_kworker, 1);
577 static void iscsi_target_do_login_rx(struct work_struct *work)
579 struct iscsi_conn *conn = container_of(work,
580 struct iscsi_conn, login_work.work);
581 struct iscsi_login *login = conn->login;
582 struct iscsi_np *np = login->np;
583 struct iscsi_portal_group *tpg = conn->tpg;
584 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
585 struct timer_list login_timer;
586 int rc, zero_tsih = login->zero_tsih;
589 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
590 conn, current->comm, current->pid);
592 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
593 * before initial PDU processing in iscsi_target_start_negotiation()
594 * has completed, go ahead and retry until it's cleared.
596 * Otherwise if the TCP connection drops while this is occuring,
597 * iscsi_target_start_negotiation() will detect the failure, call
598 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
599 * remaining iscsi connection resources from iscsi_np process context.
601 if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
602 schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
606 spin_lock(&tpg->tpg_state_lock);
607 state = (tpg->tpg_state == TPG_STATE_ACTIVE);
608 spin_unlock(&tpg->tpg_state_lock);
611 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
615 if (iscsi_target_sk_check_close(conn)) {
616 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
620 conn->login_kworker = current;
621 allow_signal(SIGINT);
623 init_timer(&login_timer);
624 login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
625 login_timer.data = (unsigned long)conn;
626 login_timer.function = iscsi_target_login_timeout;
627 add_timer(&login_timer);
628 pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
630 rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
631 del_timer_sync(&login_timer);
632 flush_signals(current);
633 conn->login_kworker = NULL;
638 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
639 conn, current->comm, current->pid);
641 rc = iscsi_target_do_login(conn, login);
645 if (iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_READ_ACTIVE))
647 } else if (rc == 1) {
648 iscsi_target_nego_release(conn);
649 iscsi_post_login_handler(np, conn, zero_tsih);
650 iscsit_deaccess_np(np, tpg, tpg_np);
655 iscsi_target_restore_sock_callbacks(conn);
656 iscsi_target_login_drop(conn, login);
657 iscsit_deaccess_np(np, tpg, tpg_np);
660 static void iscsi_target_sk_state_change(struct sock *sk)
662 struct iscsi_conn *conn;
663 void (*orig_state_change)(struct sock *);
666 pr_debug("Entering iscsi_target_sk_state_change\n");
668 write_lock_bh(&sk->sk_callback_lock);
669 conn = sk->sk_user_data;
671 write_unlock_bh(&sk->sk_callback_lock);
674 orig_state_change = conn->orig_state_change;
676 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
677 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
679 write_unlock_bh(&sk->sk_callback_lock);
680 orig_state_change(sk);
683 state = __iscsi_target_sk_check_close(sk);
684 pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
686 if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
687 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
688 " conn: %p\n", conn);
690 set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
691 write_unlock_bh(&sk->sk_callback_lock);
692 orig_state_change(sk);
695 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
696 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
698 write_unlock_bh(&sk->sk_callback_lock);
699 orig_state_change(sk);
703 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
704 * but only queue conn->login_work -> iscsi_target_do_login_rx()
705 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
707 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
708 * will detect the dropped TCP connection from delayed workqueue context.
710 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
711 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
712 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
713 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
714 * dropped TCP connection in iscsi_np process context, and cleaning up
715 * the remaining iscsi connection resources.
718 pr_debug("iscsi_target_sk_state_change got failed state\n");
719 set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
720 state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
721 write_unlock_bh(&sk->sk_callback_lock);
723 orig_state_change(sk);
726 schedule_delayed_work(&conn->login_work, 0);
729 write_unlock_bh(&sk->sk_callback_lock);
731 orig_state_change(sk);
735 * NOTE: We check for existing sessions or connections AFTER the initiator
736 * has been successfully authenticated in order to protect against faked
737 * ISID/TSIH combinations.
739 static int iscsi_target_check_for_existing_instances(
740 struct iscsi_conn *conn,
741 struct iscsi_login *login)
743 if (login->checked_for_existing)
746 login->checked_for_existing = 1;
749 return iscsi_check_for_session_reinstatement(conn);
751 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
752 login->initial_exp_statsn);
755 static int iscsi_target_do_authentication(
756 struct iscsi_conn *conn,
757 struct iscsi_login *login)
761 struct iscsi_param *param;
762 struct iscsi_login_req *login_req;
763 struct iscsi_login_rsp *login_rsp;
765 login_req = (struct iscsi_login_req *) login->req;
766 login_rsp = (struct iscsi_login_rsp *) login->rsp;
767 payload_length = ntoh24(login_req->dlength);
769 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
773 authret = iscsi_handle_authentication(
782 pr_debug("Received OK response"
783 " from LIO Authentication, continuing.\n");
786 pr_debug("iSCSI security negotiation"
787 " completed successfully.\n");
788 login->auth_complete = 1;
789 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
790 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
791 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
792 ISCSI_FLAG_LOGIN_TRANSIT);
793 login->current_stage = 1;
795 return iscsi_target_check_for_existing_instances(
798 pr_err("Security negotiation"
800 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
801 ISCSI_LOGIN_STATUS_AUTH_FAILED);
804 pr_err("Received unknown error %d from LIO"
805 " Authentication\n", authret);
806 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
807 ISCSI_LOGIN_STATUS_TARGET_ERROR);
814 static int iscsi_target_handle_csg_zero(
815 struct iscsi_conn *conn,
816 struct iscsi_login *login)
820 struct iscsi_param *param;
821 struct iscsi_login_req *login_req;
822 struct iscsi_login_rsp *login_rsp;
824 login_req = (struct iscsi_login_req *) login->req;
825 login_rsp = (struct iscsi_login_rsp *) login->rsp;
826 payload_length = ntoh24(login_req->dlength);
828 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
832 ret = iscsi_decode_text_input(
833 PHASE_SECURITY|PHASE_DECLARATIVE,
834 SENDER_INITIATOR|SENDER_RECEIVER,
842 if (login->auth_complete) {
843 pr_err("Initiator has already been"
844 " successfully authenticated, but is still"
845 " sending %s keys.\n", param->value);
846 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
847 ISCSI_LOGIN_STATUS_INIT_ERR);
852 } else if (!payload_length) {
853 pr_err("Initiator sent zero length security payload,"
855 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
856 ISCSI_LOGIN_STATUS_AUTH_FAILED);
860 if (login->first_request)
861 if (iscsi_target_check_first_request(conn, login) < 0)
864 ret = iscsi_encode_text_output(
865 PHASE_SECURITY|PHASE_DECLARATIVE,
870 conn->tpg->tpg_attrib.login_keys_workaround);
874 if (!iscsi_check_negotiated_keys(conn->param_list)) {
875 if (conn->tpg->tpg_attrib.authentication &&
876 !strncmp(param->value, NONE, 4)) {
877 pr_err("Initiator sent AuthMethod=None but"
878 " Target is enforcing iSCSI Authentication,"
880 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
881 ISCSI_LOGIN_STATUS_AUTH_FAILED);
885 if (conn->tpg->tpg_attrib.authentication &&
886 !login->auth_complete)
889 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
892 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
893 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
894 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
895 ISCSI_FLAG_LOGIN_TRANSIT;
896 login->current_stage = 1;
902 return iscsi_target_do_authentication(conn, login);
905 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
909 struct iscsi_login_req *login_req;
910 struct iscsi_login_rsp *login_rsp;
912 login_req = (struct iscsi_login_req *) login->req;
913 login_rsp = (struct iscsi_login_rsp *) login->rsp;
914 payload_length = ntoh24(login_req->dlength);
916 ret = iscsi_decode_text_input(
917 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
918 SENDER_INITIATOR|SENDER_RECEIVER,
923 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
924 ISCSI_LOGIN_STATUS_INIT_ERR);
928 if (login->first_request)
929 if (iscsi_target_check_first_request(conn, login) < 0)
932 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
935 ret = iscsi_encode_text_output(
936 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
941 conn->tpg->tpg_attrib.login_keys_workaround);
943 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
944 ISCSI_LOGIN_STATUS_INIT_ERR);
948 if (!login->auth_complete &&
949 conn->tpg->tpg_attrib.authentication) {
950 pr_err("Initiator is requesting CSG: 1, has not been"
951 " successfully authenticated, and the Target is"
952 " enforcing iSCSI Authentication, login failed.\n");
953 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
954 ISCSI_LOGIN_STATUS_AUTH_FAILED);
958 if (!iscsi_check_negotiated_keys(conn->param_list))
959 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
960 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
961 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
962 ISCSI_FLAG_LOGIN_TRANSIT;
967 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
970 struct iscsi_login_req *login_req;
971 struct iscsi_login_rsp *login_rsp;
973 login_req = (struct iscsi_login_req *) login->req;
974 login_rsp = (struct iscsi_login_rsp *) login->rsp;
977 if (++pdu_count > MAX_LOGIN_PDUS) {
978 pr_err("MAX_LOGIN_PDUS count reached.\n");
979 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
980 ISCSI_LOGIN_STATUS_TARGET_ERROR);
984 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
986 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
987 if (iscsi_target_handle_csg_zero(conn, login) < 0)
991 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
992 if (iscsi_target_handle_csg_one(conn, login) < 0)
994 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
996 * Check to make sure the TCP connection has not
997 * dropped asynchronously while session reinstatement
998 * was occuring in this kthread context, before
999 * transitioning to full feature phase operation.
1001 if (iscsi_target_sk_check_close(conn))
1004 login->tsih = conn->sess->tsih;
1005 login->login_complete = 1;
1006 iscsi_target_restore_sock_callbacks(conn);
1007 if (iscsi_target_do_tx_login_io(conn,
1014 pr_err("Illegal CSG: %d received from"
1015 " Initiator, protocol error.\n",
1016 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
1020 if (iscsi_target_do_tx_login_io(conn, login) < 0)
1023 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1024 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1025 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1033 static void iscsi_initiatorname_tolower(
1037 u32 iqn_size = strlen(param_buf), i;
1039 for (i = 0; i < iqn_size; i++) {
1049 * Processes the first Login Request..
1051 int iscsi_target_locate_portal(
1052 struct iscsi_np *np,
1053 struct iscsi_conn *conn,
1054 struct iscsi_login *login)
1056 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1057 char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1058 struct iscsi_session *sess = conn->sess;
1059 struct iscsi_tiqn *tiqn;
1060 struct iscsi_tpg_np *tpg_np = NULL;
1061 struct iscsi_login_req *login_req;
1062 struct se_node_acl *se_nacl;
1063 u32 payload_length, queue_depth = 0;
1064 int sessiontype = 0, ret = 0, tag_num, tag_size;
1066 INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1067 iscsi_target_set_sock_callbacks(conn);
1071 login_req = (struct iscsi_login_req *) login->req;
1072 payload_length = ntoh24(login_req->dlength);
1074 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1076 pr_err("Unable to allocate memory for tmpbuf.\n");
1080 memcpy(tmpbuf, login->req_buf, payload_length);
1081 tmpbuf[payload_length] = '\0';
1083 end = (start + payload_length);
1086 * Locate the initial keys expected from the Initiator node in
1087 * the first login request in order to progress with the login phase.
1089 while (start < end) {
1090 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1095 if (!strncmp(key, "InitiatorName", 13))
1097 else if (!strncmp(key, "SessionType", 11))
1099 else if (!strncmp(key, "TargetName", 10))
1102 start += strlen(key) + strlen(value) + 2;
1105 * See 5.3. Login Phase.
1108 pr_err("InitiatorName key not received"
1109 " in first login request.\n");
1110 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1111 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1116 * Convert the incoming InitiatorName to lowercase following
1117 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1118 * are NOT case sensitive.
1120 iscsi_initiatorname_tolower(i_buf);
1123 if (!login->leading_connection)
1126 pr_err("SessionType key not received"
1127 " in first login request.\n");
1128 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1129 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1135 * Use default portal group for discovery sessions.
1137 sessiontype = strncmp(s_buf, DISCOVERY, 9);
1139 conn->tpg = iscsit_global->discovery_tpg;
1140 if (!login->leading_connection)
1143 sess->sess_ops->SessionType = 1;
1145 * Setup crc32c modules from libcrypto
1147 if (iscsi_login_setup_crypto(conn) < 0) {
1148 pr_err("iscsi_login_setup_crypto() failed\n");
1153 * Serialize access across the discovery struct iscsi_portal_group to
1154 * process login attempt.
1156 if (iscsit_access_np(np, conn->tpg) < 0) {
1157 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1158 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1168 pr_err("TargetName key not received"
1169 " in first login request while"
1170 " SessionType=Normal.\n");
1171 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1172 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1178 * Locate Target IQN from Storage Node.
1180 tiqn = iscsit_get_tiqn_for_login(t_buf);
1182 pr_err("Unable to locate Target IQN: %s in"
1183 " Storage Node\n", t_buf);
1184 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1185 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1189 pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1192 * Locate Target Portal Group from Storage Node.
1194 conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1196 pr_err("Unable to locate Target Portal Group"
1197 " on %s\n", tiqn->tiqn);
1198 iscsit_put_tiqn_for_login(tiqn);
1199 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1200 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1204 conn->tpg_np = tpg_np;
1205 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1207 * Setup crc32c modules from libcrypto
1209 if (iscsi_login_setup_crypto(conn) < 0) {
1210 pr_err("iscsi_login_setup_crypto() failed\n");
1211 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1212 iscsit_put_tiqn_for_login(tiqn);
1218 * Serialize access across the struct iscsi_portal_group to
1219 * process login attempt.
1221 if (iscsit_access_np(np, conn->tpg) < 0) {
1222 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1223 iscsit_put_tiqn_for_login(tiqn);
1224 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1225 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1232 * conn->sess->node_acl will be set when the referenced
1233 * struct iscsi_session is located from received ISID+TSIH in
1234 * iscsi_login_non_zero_tsih_s2().
1236 if (!login->leading_connection) {
1242 * This value is required in iscsi_login_zero_tsih_s2()
1244 sess->sess_ops->SessionType = 0;
1247 * Locate incoming Initiator IQN reference from Storage Node.
1249 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1250 &conn->tpg->tpg_se_tpg, i_buf);
1251 if (!sess->se_sess->se_node_acl) {
1252 pr_err("iSCSI Initiator Node: %s is not authorized to"
1253 " access iSCSI target portal group: %hu.\n",
1254 i_buf, conn->tpg->tpgt);
1255 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1256 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1260 se_nacl = sess->se_sess->se_node_acl;
1261 queue_depth = se_nacl->queue_depth;
1263 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1264 * depth for non immediate commands, plus extra tags for immediate
1267 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1268 * in per-cpu-ida tag allocation logic + small queue_depth.
1271 tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
1272 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1273 tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1275 ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1277 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1278 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1286 int iscsi_target_start_negotiation(
1287 struct iscsi_login *login,
1288 struct iscsi_conn *conn)
1293 struct sock *sk = conn->sock->sk;
1295 write_lock_bh(&sk->sk_callback_lock);
1296 set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1297 set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1298 write_unlock_bh(&sk->sk_callback_lock);
1301 * If iscsi_target_do_login returns zero to signal more PDU
1302 * exchanges are required to complete the login, go ahead and
1303 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
1306 * Otherwise if TCP connection dropped asynchronously, go ahead
1307 * and perform connection cleanup now.
1309 ret = iscsi_target_do_login(conn, login);
1310 if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
1314 cancel_delayed_work_sync(&conn->login_work);
1315 iscsi_target_restore_sock_callbacks(conn);
1316 iscsi_remove_failed_auth_entry(conn);
1319 iscsi_target_nego_release(conn);
1324 void iscsi_target_nego_release(struct iscsi_conn *conn)
1326 struct iscsi_login *login = conn->conn_login;
1331 kfree(login->req_buf);
1332 kfree(login->rsp_buf);
1335 conn->conn_login = NULL;