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>
23 #include <scsi/iscsi_proto.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_fabric.h>
26 #include <target/iscsi/iscsi_transport.h>
28 #include <target/iscsi/iscsi_target_core.h>
29 #include "iscsi_target_parameters.h"
30 #include "iscsi_target_login.h"
31 #include "iscsi_target_nego.h"
32 #include "iscsi_target_tpg.h"
33 #include "iscsi_target_util.h"
34 #include "iscsi_target.h"
35 #include "iscsi_target_auth.h"
37 #define MAX_LOGIN_PDUS 7
40 void convert_null_to_semi(char *buf, int len)
44 for (i = 0; i < len; i++)
49 static int strlen_semi(char *buf)
53 while (buf[i] != '\0') {
65 unsigned int max_length,
72 if (!in_buf || !pattern || !out_buf || !type)
75 ptr = strstr(in_buf, pattern);
79 ptr = strstr(ptr, "=");
84 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
85 ptr += 2; /* skip 0x */
90 len = strlen_semi(ptr);
94 if (len >= max_length) {
95 pr_err("Length of input: %d exceeds max_length:"
96 " %d\n", len, max_length);
99 memcpy(out_buf, ptr, len);
105 static u32 iscsi_handle_authentication(
106 struct iscsi_conn *conn,
111 unsigned char *authtype)
113 struct iscsi_session *sess = conn->sess;
114 struct iscsi_node_auth *auth;
115 struct iscsi_node_acl *iscsi_nacl;
116 struct iscsi_portal_group *iscsi_tpg;
117 struct se_node_acl *se_nacl;
119 if (!sess->sess_ops->SessionType) {
121 * For SessionType=Normal
123 se_nacl = conn->sess->se_sess->se_node_acl;
125 pr_err("Unable to locate struct se_node_acl for"
129 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
132 pr_err("Unable to locate struct iscsi_node_acl for"
137 if (se_nacl->dynamic_node_acl) {
138 iscsi_tpg = container_of(se_nacl->se_tpg,
139 struct iscsi_portal_group, tpg_se_tpg);
141 auth = &iscsi_tpg->tpg_demo_auth;
143 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
146 auth = &iscsi_nacl->node_auth;
150 * For SessionType=Discovery
152 auth = &iscsit_global->discovery_acl.node_auth;
155 if (strstr("CHAP", authtype))
156 strcpy(conn->sess->auth_type, "CHAP");
158 strcpy(conn->sess->auth_type, NONE);
160 if (strstr("None", authtype))
163 else if (strstr("SRP", authtype))
164 return srp_main_loop(conn, auth, in_buf, out_buf,
165 &in_length, out_length);
167 else if (strstr("CHAP", authtype))
168 return chap_main_loop(conn, auth, in_buf, out_buf,
169 &in_length, out_length);
170 else if (strstr("SPKM1", authtype))
172 else if (strstr("SPKM2", authtype))
174 else if (strstr("KRB5", authtype))
180 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
182 kfree(conn->auth_protocol);
185 int iscsi_target_check_login_request(
186 struct iscsi_conn *conn,
187 struct iscsi_login *login)
189 int req_csg, req_nsg;
191 struct iscsi_login_req *login_req;
193 login_req = (struct iscsi_login_req *) login->req;
194 payload_length = ntoh24(login_req->dlength);
196 switch (login_req->opcode & ISCSI_OPCODE_MASK) {
200 pr_err("Received unknown opcode 0x%02x.\n",
201 login_req->opcode & ISCSI_OPCODE_MASK);
202 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
203 ISCSI_LOGIN_STATUS_INIT_ERR);
207 if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
208 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
209 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
210 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
211 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
212 ISCSI_LOGIN_STATUS_INIT_ERR);
216 req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
217 req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
219 if (req_csg != login->current_stage) {
220 pr_err("Initiator unexpectedly changed login stage"
221 " from %d to %d, login failed.\n", login->current_stage,
223 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
224 ISCSI_LOGIN_STATUS_INIT_ERR);
228 if ((req_nsg == 2) || (req_csg >= 2) ||
229 ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
230 (req_nsg <= req_csg))) {
231 pr_err("Illegal login_req->flags Combination, CSG: %d,"
232 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
233 req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
234 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
235 ISCSI_LOGIN_STATUS_INIT_ERR);
239 if ((login_req->max_version != login->version_max) ||
240 (login_req->min_version != login->version_min)) {
241 pr_err("Login request changed Version Max/Nin"
242 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
243 login_req->max_version, login_req->min_version);
244 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
245 ISCSI_LOGIN_STATUS_INIT_ERR);
249 if (memcmp(login_req->isid, login->isid, 6) != 0) {
250 pr_err("Login request changed ISID unexpectedly,"
251 " protocol error.\n");
252 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
253 ISCSI_LOGIN_STATUS_INIT_ERR);
257 if (login_req->itt != login->init_task_tag) {
258 pr_err("Login request changed ITT unexpectedly to"
259 " 0x%08x, protocol error.\n", login_req->itt);
260 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
261 ISCSI_LOGIN_STATUS_INIT_ERR);
265 if (payload_length > MAX_KEY_VALUE_PAIRS) {
266 pr_err("Login request payload exceeds default"
267 " MaxRecvDataSegmentLength: %u, protocol error.\n",
268 MAX_KEY_VALUE_PAIRS);
274 EXPORT_SYMBOL(iscsi_target_check_login_request);
276 static int iscsi_target_check_first_request(
277 struct iscsi_conn *conn,
278 struct iscsi_login *login)
280 struct iscsi_param *param = NULL;
281 struct se_node_acl *se_nacl;
283 login->first_request = 0;
285 list_for_each_entry(param, &conn->param_list->param_list, p_list) {
286 if (!strncmp(param->name, SESSIONTYPE, 11)) {
287 if (!IS_PSTATE_ACCEPTOR(param)) {
288 pr_err("SessionType key not received"
289 " in first login request.\n");
290 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
291 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
294 if (!strncmp(param->value, DISCOVERY, 9))
298 if (!strncmp(param->name, INITIATORNAME, 13)) {
299 if (!IS_PSTATE_ACCEPTOR(param)) {
300 if (!login->leading_connection)
303 pr_err("InitiatorName key not received"
304 " in first login request.\n");
305 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
306 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
311 * For non-leading connections, double check that the
312 * received InitiatorName matches the existing session's
313 * struct iscsi_node_acl.
315 if (!login->leading_connection) {
316 se_nacl = conn->sess->se_sess->se_node_acl;
318 pr_err("Unable to locate"
319 " struct se_node_acl\n");
320 iscsit_tx_login_rsp(conn,
321 ISCSI_STATUS_CLS_INITIATOR_ERR,
322 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
326 if (strcmp(param->value,
327 se_nacl->initiatorname)) {
329 " InitiatorName: %s for this"
330 " iSCSI Initiator Node.\n",
332 iscsit_tx_login_rsp(conn,
333 ISCSI_STATUS_CLS_INITIATOR_ERR,
334 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
344 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
347 struct iscsi_login_rsp *login_rsp;
349 login_rsp = (struct iscsi_login_rsp *) login->rsp;
351 login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
352 hton24(login_rsp->dlength, login->rsp_length);
353 memcpy(login_rsp->isid, login->isid, 6);
354 login_rsp->tsih = cpu_to_be16(login->tsih);
355 login_rsp->itt = login->init_task_tag;
356 login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
357 login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
358 login_rsp->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
360 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
361 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
362 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
363 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
364 ntohl(login_rsp->statsn), login->rsp_length);
366 padding = ((-login->rsp_length) & 3);
368 * Before sending the last login response containing the transition
369 * bit for full-feature-phase, go ahead and start up TX/RX threads
370 * now to avoid potential resource allocation failures after the
371 * final login response has been sent.
373 if (login->login_complete) {
374 int rc = iscsit_start_kthreads(conn);
376 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
377 ISCSI_LOGIN_STATUS_NO_RESOURCES);
382 if (conn->conn_transport->iscsit_put_login_tx(conn, login,
383 login->rsp_length + padding) < 0)
386 login->rsp_length = 0;
391 if (login->login_complete) {
392 if (conn->rx_thread && conn->rx_thread_active) {
393 send_sig(SIGINT, conn->rx_thread, 1);
394 complete(&conn->rx_login_comp);
395 kthread_stop(conn->rx_thread);
397 if (conn->tx_thread && conn->tx_thread_active) {
398 send_sig(SIGINT, conn->tx_thread, 1);
399 kthread_stop(conn->tx_thread);
401 spin_lock(&iscsit_global->ts_bitmap_lock);
402 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
404 spin_unlock(&iscsit_global->ts_bitmap_lock);
409 static void iscsi_target_sk_data_ready(struct sock *sk)
411 struct iscsi_conn *conn = sk->sk_user_data;
414 pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
416 write_lock_bh(&sk->sk_callback_lock);
417 if (!sk->sk_user_data) {
418 write_unlock_bh(&sk->sk_callback_lock);
421 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
422 write_unlock_bh(&sk->sk_callback_lock);
423 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
426 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
427 write_unlock_bh(&sk->sk_callback_lock);
428 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
431 if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
432 write_unlock_bh(&sk->sk_callback_lock);
433 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
437 rc = schedule_delayed_work(&conn->login_work, 0);
439 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
442 write_unlock_bh(&sk->sk_callback_lock);
445 static void iscsi_target_sk_state_change(struct sock *);
447 static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
455 pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
457 write_lock_bh(&sk->sk_callback_lock);
458 sk->sk_user_data = conn;
459 conn->orig_data_ready = sk->sk_data_ready;
460 conn->orig_state_change = sk->sk_state_change;
461 sk->sk_data_ready = iscsi_target_sk_data_ready;
462 sk->sk_state_change = iscsi_target_sk_state_change;
463 write_unlock_bh(&sk->sk_callback_lock);
465 sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
466 sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
469 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
477 pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
479 write_lock_bh(&sk->sk_callback_lock);
480 if (!sk->sk_user_data) {
481 write_unlock_bh(&sk->sk_callback_lock);
484 sk->sk_user_data = NULL;
485 sk->sk_data_ready = conn->orig_data_ready;
486 sk->sk_state_change = conn->orig_state_change;
487 write_unlock_bh(&sk->sk_callback_lock);
489 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
490 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
493 static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
495 static bool iscsi_target_sk_state_check(struct sock *sk)
497 if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
498 pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
499 "returning FALSE\n");
505 static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
507 struct iscsi_np *np = login->np;
508 bool zero_tsih = login->zero_tsih;
510 iscsi_remove_failed_auth_entry(conn);
511 iscsi_target_nego_release(conn);
512 iscsi_target_login_sess_out(conn, np, zero_tsih, true);
515 static void iscsi_target_login_timeout(unsigned long data)
517 struct iscsi_conn *conn = (struct iscsi_conn *)data;
519 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
521 if (conn->login_kworker) {
522 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
523 conn->login_kworker->comm, conn->login_kworker->pid);
524 send_sig(SIGINT, conn->login_kworker, 1);
528 static void iscsi_target_do_login_rx(struct work_struct *work)
530 struct iscsi_conn *conn = container_of(work,
531 struct iscsi_conn, login_work.work);
532 struct iscsi_login *login = conn->login;
533 struct iscsi_np *np = login->np;
534 struct iscsi_portal_group *tpg = conn->tpg;
535 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
536 struct timer_list login_timer;
537 int rc, zero_tsih = login->zero_tsih;
540 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
541 conn, current->comm, current->pid);
543 spin_lock(&tpg->tpg_state_lock);
544 state = (tpg->tpg_state == TPG_STATE_ACTIVE);
545 spin_unlock(&tpg->tpg_state_lock);
548 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
549 iscsi_target_restore_sock_callbacks(conn);
550 iscsi_target_login_drop(conn, login);
551 iscsit_deaccess_np(np, tpg, tpg_np);
556 struct sock *sk = conn->sock->sk;
558 read_lock_bh(&sk->sk_callback_lock);
559 state = iscsi_target_sk_state_check(sk);
560 read_unlock_bh(&sk->sk_callback_lock);
563 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
564 iscsi_target_restore_sock_callbacks(conn);
565 iscsi_target_login_drop(conn, login);
566 iscsit_deaccess_np(np, tpg, tpg_np);
571 conn->login_kworker = current;
572 allow_signal(SIGINT);
574 init_timer(&login_timer);
575 login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
576 login_timer.data = (unsigned long)conn;
577 login_timer.function = iscsi_target_login_timeout;
578 add_timer(&login_timer);
579 pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
581 rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
582 del_timer_sync(&login_timer);
583 flush_signals(current);
584 conn->login_kworker = NULL;
587 iscsi_target_restore_sock_callbacks(conn);
588 iscsi_target_login_drop(conn, login);
589 iscsit_deaccess_np(np, tpg, tpg_np);
593 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
594 conn, current->comm, current->pid);
596 rc = iscsi_target_do_login(conn, login);
598 iscsi_target_restore_sock_callbacks(conn);
599 iscsi_target_login_drop(conn, login);
600 iscsit_deaccess_np(np, tpg, tpg_np);
603 struct sock *sk = conn->sock->sk;
605 write_lock_bh(&sk->sk_callback_lock);
606 clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
607 write_unlock_bh(&sk->sk_callback_lock);
609 } else if (rc == 1) {
610 iscsi_target_nego_release(conn);
611 iscsi_post_login_handler(np, conn, zero_tsih);
612 iscsit_deaccess_np(np, tpg, tpg_np);
616 static void iscsi_target_do_cleanup(struct work_struct *work)
618 struct iscsi_conn *conn = container_of(work,
619 struct iscsi_conn, login_cleanup_work.work);
620 struct sock *sk = conn->sock->sk;
621 struct iscsi_login *login = conn->login;
622 struct iscsi_np *np = login->np;
623 struct iscsi_portal_group *tpg = conn->tpg;
624 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
626 pr_debug("Entering iscsi_target_do_cleanup\n");
628 cancel_delayed_work_sync(&conn->login_work);
629 conn->orig_state_change(sk);
631 iscsi_target_restore_sock_callbacks(conn);
632 iscsi_target_login_drop(conn, login);
633 iscsit_deaccess_np(np, tpg, tpg_np);
635 pr_debug("iscsi_target_do_cleanup done()\n");
638 static void iscsi_target_sk_state_change(struct sock *sk)
640 struct iscsi_conn *conn;
641 void (*orig_state_change)(struct sock *);
644 pr_debug("Entering iscsi_target_sk_state_change\n");
646 write_lock_bh(&sk->sk_callback_lock);
647 conn = sk->sk_user_data;
649 write_unlock_bh(&sk->sk_callback_lock);
652 orig_state_change = conn->orig_state_change;
654 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
655 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
657 write_unlock_bh(&sk->sk_callback_lock);
658 orig_state_change(sk);
661 if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
662 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
663 " conn: %p\n", conn);
664 write_unlock_bh(&sk->sk_callback_lock);
665 orig_state_change(sk);
668 if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
669 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
671 write_unlock_bh(&sk->sk_callback_lock);
672 orig_state_change(sk);
676 state = iscsi_target_sk_state_check(sk);
677 write_unlock_bh(&sk->sk_callback_lock);
679 pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
682 pr_debug("iscsi_target_sk_state_change got failed state\n");
683 schedule_delayed_work(&conn->login_cleanup_work, 0);
686 orig_state_change(sk);
690 * NOTE: We check for existing sessions or connections AFTER the initiator
691 * has been successfully authenticated in order to protect against faked
692 * ISID/TSIH combinations.
694 static int iscsi_target_check_for_existing_instances(
695 struct iscsi_conn *conn,
696 struct iscsi_login *login)
698 if (login->checked_for_existing)
701 login->checked_for_existing = 1;
704 return iscsi_check_for_session_reinstatement(conn);
706 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
707 login->initial_exp_statsn);
710 static int iscsi_target_do_authentication(
711 struct iscsi_conn *conn,
712 struct iscsi_login *login)
716 struct iscsi_param *param;
717 struct iscsi_login_req *login_req;
718 struct iscsi_login_rsp *login_rsp;
720 login_req = (struct iscsi_login_req *) login->req;
721 login_rsp = (struct iscsi_login_rsp *) login->rsp;
722 payload_length = ntoh24(login_req->dlength);
724 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
728 authret = iscsi_handle_authentication(
737 pr_debug("Received OK response"
738 " from LIO Authentication, continuing.\n");
741 pr_debug("iSCSI security negotiation"
742 " completed successfully.\n");
743 login->auth_complete = 1;
744 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
745 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
746 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
747 ISCSI_FLAG_LOGIN_TRANSIT);
748 login->current_stage = 1;
750 return iscsi_target_check_for_existing_instances(
753 pr_err("Security negotiation"
755 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
756 ISCSI_LOGIN_STATUS_AUTH_FAILED);
759 pr_err("Received unknown error %d from LIO"
760 " Authentication\n", authret);
761 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
762 ISCSI_LOGIN_STATUS_TARGET_ERROR);
769 static int iscsi_target_handle_csg_zero(
770 struct iscsi_conn *conn,
771 struct iscsi_login *login)
775 struct iscsi_param *param;
776 struct iscsi_login_req *login_req;
777 struct iscsi_login_rsp *login_rsp;
779 login_req = (struct iscsi_login_req *) login->req;
780 login_rsp = (struct iscsi_login_rsp *) login->rsp;
781 payload_length = ntoh24(login_req->dlength);
783 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
787 ret = iscsi_decode_text_input(
788 PHASE_SECURITY|PHASE_DECLARATIVE,
789 SENDER_INITIATOR|SENDER_RECEIVER,
797 if (login->auth_complete) {
798 pr_err("Initiator has already been"
799 " successfully authenticated, but is still"
800 " sending %s keys.\n", param->value);
801 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
802 ISCSI_LOGIN_STATUS_INIT_ERR);
807 } else if (!payload_length) {
808 pr_err("Initiator sent zero length security payload,"
810 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
811 ISCSI_LOGIN_STATUS_AUTH_FAILED);
815 if (login->first_request)
816 if (iscsi_target_check_first_request(conn, login) < 0)
819 ret = iscsi_encode_text_output(
820 PHASE_SECURITY|PHASE_DECLARATIVE,
828 if (!iscsi_check_negotiated_keys(conn->param_list)) {
829 if (conn->tpg->tpg_attrib.authentication &&
830 !strncmp(param->value, NONE, 4)) {
831 pr_err("Initiator sent AuthMethod=None but"
832 " Target is enforcing iSCSI Authentication,"
834 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
835 ISCSI_LOGIN_STATUS_AUTH_FAILED);
839 if (conn->tpg->tpg_attrib.authentication &&
840 !login->auth_complete)
843 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
846 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
847 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
848 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
849 ISCSI_FLAG_LOGIN_TRANSIT;
850 login->current_stage = 1;
856 return iscsi_target_do_authentication(conn, login);
859 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
863 struct iscsi_login_req *login_req;
864 struct iscsi_login_rsp *login_rsp;
866 login_req = (struct iscsi_login_req *) login->req;
867 login_rsp = (struct iscsi_login_rsp *) login->rsp;
868 payload_length = ntoh24(login_req->dlength);
870 ret = iscsi_decode_text_input(
871 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
872 SENDER_INITIATOR|SENDER_RECEIVER,
877 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
878 ISCSI_LOGIN_STATUS_INIT_ERR);
882 if (login->first_request)
883 if (iscsi_target_check_first_request(conn, login) < 0)
886 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
889 ret = iscsi_encode_text_output(
890 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
896 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
897 ISCSI_LOGIN_STATUS_INIT_ERR);
901 if (!login->auth_complete &&
902 conn->tpg->tpg_attrib.authentication) {
903 pr_err("Initiator is requesting CSG: 1, has not been"
904 " successfully authenticated, and the Target is"
905 " enforcing iSCSI Authentication, login failed.\n");
906 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
907 ISCSI_LOGIN_STATUS_AUTH_FAILED);
911 if (!iscsi_check_negotiated_keys(conn->param_list))
912 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
913 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
914 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
915 ISCSI_FLAG_LOGIN_TRANSIT;
920 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
923 struct iscsi_login_req *login_req;
924 struct iscsi_login_rsp *login_rsp;
926 login_req = (struct iscsi_login_req *) login->req;
927 login_rsp = (struct iscsi_login_rsp *) login->rsp;
930 if (++pdu_count > MAX_LOGIN_PDUS) {
931 pr_err("MAX_LOGIN_PDUS count reached.\n");
932 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
933 ISCSI_LOGIN_STATUS_TARGET_ERROR);
937 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
939 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
940 if (iscsi_target_handle_csg_zero(conn, login) < 0)
944 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
945 if (iscsi_target_handle_csg_one(conn, login) < 0)
947 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
948 login->tsih = conn->sess->tsih;
949 login->login_complete = 1;
950 iscsi_target_restore_sock_callbacks(conn);
951 if (iscsi_target_do_tx_login_io(conn,
958 pr_err("Illegal CSG: %d received from"
959 " Initiator, protocol error.\n",
960 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
964 if (iscsi_target_do_tx_login_io(conn, login) < 0)
967 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
968 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
969 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
975 struct sock *sk = conn->sock->sk;
978 read_lock_bh(&sk->sk_callback_lock);
979 state = iscsi_target_sk_state_check(sk);
980 read_unlock_bh(&sk->sk_callback_lock);
983 pr_debug("iscsi_target_do_login() failed state for"
984 " conn: %p\n", conn);
992 static void iscsi_initiatorname_tolower(
996 u32 iqn_size = strlen(param_buf), i;
998 for (i = 0; i < iqn_size; i++) {
1008 * Processes the first Login Request..
1010 int iscsi_target_locate_portal(
1011 struct iscsi_np *np,
1012 struct iscsi_conn *conn,
1013 struct iscsi_login *login)
1015 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1016 char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1017 struct iscsi_session *sess = conn->sess;
1018 struct iscsi_tiqn *tiqn;
1019 struct iscsi_tpg_np *tpg_np = NULL;
1020 struct iscsi_login_req *login_req;
1021 struct se_node_acl *se_nacl;
1022 u32 payload_length, queue_depth = 0;
1023 int sessiontype = 0, ret = 0, tag_num, tag_size;
1025 INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1026 INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
1027 iscsi_target_set_sock_callbacks(conn);
1031 login_req = (struct iscsi_login_req *) login->req;
1032 payload_length = ntoh24(login_req->dlength);
1034 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1036 pr_err("Unable to allocate memory for tmpbuf.\n");
1040 memcpy(tmpbuf, login->req_buf, payload_length);
1041 tmpbuf[payload_length] = '\0';
1043 end = (start + payload_length);
1046 * Locate the initial keys expected from the Initiator node in
1047 * the first login request in order to progress with the login phase.
1049 while (start < end) {
1050 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1055 if (!strncmp(key, "InitiatorName", 13))
1057 else if (!strncmp(key, "SessionType", 11))
1059 else if (!strncmp(key, "TargetName", 10))
1062 start += strlen(key) + strlen(value) + 2;
1065 * See 5.3. Login Phase.
1068 pr_err("InitiatorName key not received"
1069 " in first login request.\n");
1070 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1071 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1076 * Convert the incoming InitiatorName to lowercase following
1077 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1078 * are NOT case sensitive.
1080 iscsi_initiatorname_tolower(i_buf);
1083 if (!login->leading_connection)
1086 pr_err("SessionType key not received"
1087 " in first login request.\n");
1088 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1089 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1095 * Use default portal group for discovery sessions.
1097 sessiontype = strncmp(s_buf, DISCOVERY, 9);
1099 conn->tpg = iscsit_global->discovery_tpg;
1100 if (!login->leading_connection)
1103 sess->sess_ops->SessionType = 1;
1105 * Setup crc32c modules from libcrypto
1107 if (iscsi_login_setup_crypto(conn) < 0) {
1108 pr_err("iscsi_login_setup_crypto() failed\n");
1113 * Serialize access across the discovery struct iscsi_portal_group to
1114 * process login attempt.
1116 if (iscsit_access_np(np, conn->tpg) < 0) {
1117 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1118 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1128 pr_err("TargetName key not received"
1129 " in first login request while"
1130 " SessionType=Normal.\n");
1131 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1132 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1138 * Locate Target IQN from Storage Node.
1140 tiqn = iscsit_get_tiqn_for_login(t_buf);
1142 pr_err("Unable to locate Target IQN: %s in"
1143 " Storage Node\n", t_buf);
1144 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1145 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1149 pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1152 * Locate Target Portal Group from Storage Node.
1154 conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1156 pr_err("Unable to locate Target Portal Group"
1157 " on %s\n", tiqn->tiqn);
1158 iscsit_put_tiqn_for_login(tiqn);
1159 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1160 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1164 conn->tpg_np = tpg_np;
1165 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1167 * Setup crc32c modules from libcrypto
1169 if (iscsi_login_setup_crypto(conn) < 0) {
1170 pr_err("iscsi_login_setup_crypto() failed\n");
1171 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1172 iscsit_put_tiqn_for_login(tiqn);
1178 * Serialize access across the struct iscsi_portal_group to
1179 * process login attempt.
1181 if (iscsit_access_np(np, conn->tpg) < 0) {
1182 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1183 iscsit_put_tiqn_for_login(tiqn);
1184 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1185 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1192 * conn->sess->node_acl will be set when the referenced
1193 * struct iscsi_session is located from received ISID+TSIH in
1194 * iscsi_login_non_zero_tsih_s2().
1196 if (!login->leading_connection) {
1202 * This value is required in iscsi_login_zero_tsih_s2()
1204 sess->sess_ops->SessionType = 0;
1207 * Locate incoming Initiator IQN reference from Storage Node.
1209 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1210 &conn->tpg->tpg_se_tpg, i_buf);
1211 if (!sess->se_sess->se_node_acl) {
1212 pr_err("iSCSI Initiator Node: %s is not authorized to"
1213 " access iSCSI target portal group: %hu.\n",
1214 i_buf, conn->tpg->tpgt);
1215 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1216 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1220 se_nacl = sess->se_sess->se_node_acl;
1221 queue_depth = se_nacl->queue_depth;
1223 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1224 * depth for non immediate commands, plus extra tags for immediate
1227 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1228 * in per-cpu-ida tag allocation logic + small queue_depth.
1231 tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
1232 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1233 tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1235 ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1237 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1238 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1246 int iscsi_target_start_negotiation(
1247 struct iscsi_login *login,
1248 struct iscsi_conn *conn)
1253 struct sock *sk = conn->sock->sk;
1255 write_lock_bh(&sk->sk_callback_lock);
1256 set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1257 write_unlock_bh(&sk->sk_callback_lock);
1260 ret = iscsi_target_do_login(conn, login);
1262 cancel_delayed_work_sync(&conn->login_work);
1263 cancel_delayed_work_sync(&conn->login_cleanup_work);
1264 iscsi_target_restore_sock_callbacks(conn);
1265 iscsi_remove_failed_auth_entry(conn);
1268 iscsi_target_nego_release(conn);
1273 void iscsi_target_nego_release(struct iscsi_conn *conn)
1275 struct iscsi_login *login = conn->conn_login;
1280 kfree(login->req_buf);
1281 kfree(login->rsp_buf);
1284 conn->conn_login = NULL;