1 /*******************************************************************************
2 * This file contains main functions related to iSCSI Parameter negotiation.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/ctype.h>
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_fabric.h>
25 #include <target/iscsi/iscsi_transport.h>
27 #include "iscsi_target_core.h"
28 #include "iscsi_target_parameters.h"
29 #include "iscsi_target_login.h"
30 #include "iscsi_target_nego.h"
31 #include "iscsi_target_tpg.h"
32 #include "iscsi_target_util.h"
33 #include "iscsi_target.h"
34 #include "iscsi_target_auth.h"
36 #define MAX_LOGIN_PDUS 7
39 void convert_null_to_semi(char *buf, int len)
43 for (i = 0; i < len; i++)
48 static int strlen_semi(char *buf)
52 while (buf[i] != '\0') {
64 unsigned int max_length,
71 if (!in_buf || !pattern || !out_buf || !type)
74 ptr = strstr(in_buf, pattern);
78 ptr = strstr(ptr, "=");
83 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
84 ptr += 2; /* skip 0x */
89 len = strlen_semi(ptr);
93 if (len > max_length) {
94 pr_err("Length of input: %d exceeds max_length:"
95 " %d\n", len, max_length);
98 memcpy(out_buf, ptr, len);
104 static u32 iscsi_handle_authentication(
105 struct iscsi_conn *conn,
110 unsigned char *authtype)
112 struct iscsi_session *sess = conn->sess;
113 struct iscsi_node_auth *auth;
114 struct iscsi_node_acl *iscsi_nacl;
115 struct iscsi_portal_group *iscsi_tpg;
116 struct se_node_acl *se_nacl;
118 if (!sess->sess_ops->SessionType) {
120 * For SessionType=Normal
122 se_nacl = conn->sess->se_sess->se_node_acl;
124 pr_err("Unable to locate struct se_node_acl for"
128 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
131 pr_err("Unable to locate struct iscsi_node_acl for"
136 if (se_nacl->dynamic_node_acl) {
137 iscsi_tpg = container_of(se_nacl->se_tpg,
138 struct iscsi_portal_group, tpg_se_tpg);
140 auth = &iscsi_tpg->tpg_demo_auth;
142 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
145 auth = ISCSI_NODE_AUTH(iscsi_nacl);
149 * For SessionType=Discovery
151 auth = &iscsit_global->discovery_acl.node_auth;
154 if (strstr("CHAP", authtype))
155 strcpy(conn->sess->auth_type, "CHAP");
157 strcpy(conn->sess->auth_type, NONE);
159 if (strstr("None", authtype))
162 else if (strstr("SRP", authtype))
163 return srp_main_loop(conn, auth, in_buf, out_buf,
164 &in_length, out_length);
166 else if (strstr("CHAP", authtype))
167 return chap_main_loop(conn, auth, in_buf, out_buf,
168 &in_length, out_length);
169 else if (strstr("SPKM1", authtype))
171 else if (strstr("SPKM2", authtype))
173 else if (strstr("KRB5", authtype))
179 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
181 kfree(conn->auth_protocol);
184 int iscsi_target_check_login_request(
185 struct iscsi_conn *conn,
186 struct iscsi_login *login)
188 int req_csg, req_nsg;
190 struct iscsi_login_req *login_req;
192 login_req = (struct iscsi_login_req *) login->req;
193 payload_length = ntoh24(login_req->dlength);
195 switch (login_req->opcode & ISCSI_OPCODE_MASK) {
199 pr_err("Received unknown opcode 0x%02x.\n",
200 login_req->opcode & ISCSI_OPCODE_MASK);
201 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
202 ISCSI_LOGIN_STATUS_INIT_ERR);
206 if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
207 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
208 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
209 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
210 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
211 ISCSI_LOGIN_STATUS_INIT_ERR);
215 req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
216 req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
218 if (req_csg != login->current_stage) {
219 pr_err("Initiator unexpectedly changed login stage"
220 " from %d to %d, login failed.\n", login->current_stage,
222 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
223 ISCSI_LOGIN_STATUS_INIT_ERR);
227 if ((req_nsg == 2) || (req_csg >= 2) ||
228 ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
229 (req_nsg <= req_csg))) {
230 pr_err("Illegal login_req->flags Combination, CSG: %d,"
231 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
232 req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
233 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
234 ISCSI_LOGIN_STATUS_INIT_ERR);
238 if ((login_req->max_version != login->version_max) ||
239 (login_req->min_version != login->version_min)) {
240 pr_err("Login request changed Version Max/Nin"
241 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
242 login_req->max_version, login_req->min_version);
243 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
244 ISCSI_LOGIN_STATUS_INIT_ERR);
248 if (memcmp(login_req->isid, login->isid, 6) != 0) {
249 pr_err("Login request changed ISID unexpectedly,"
250 " protocol error.\n");
251 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
252 ISCSI_LOGIN_STATUS_INIT_ERR);
256 if (login_req->itt != login->init_task_tag) {
257 pr_err("Login request changed ITT unexpectedly to"
258 " 0x%08x, protocol error.\n", login_req->itt);
259 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
260 ISCSI_LOGIN_STATUS_INIT_ERR);
264 if (payload_length > MAX_KEY_VALUE_PAIRS) {
265 pr_err("Login request payload exceeds default"
266 " MaxRecvDataSegmentLength: %u, protocol error.\n",
267 MAX_KEY_VALUE_PAIRS);
274 static int iscsi_target_check_first_request(
275 struct iscsi_conn *conn,
276 struct iscsi_login *login)
278 struct iscsi_param *param = NULL;
279 struct se_node_acl *se_nacl;
281 login->first_request = 0;
283 list_for_each_entry(param, &conn->param_list->param_list, p_list) {
284 if (!strncmp(param->name, SESSIONTYPE, 11)) {
285 if (!IS_PSTATE_ACCEPTOR(param)) {
286 pr_err("SessionType key not received"
287 " in first login request.\n");
288 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
289 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
292 if (!strncmp(param->value, DISCOVERY, 9))
296 if (!strncmp(param->name, INITIATORNAME, 13)) {
297 if (!IS_PSTATE_ACCEPTOR(param)) {
298 if (!login->leading_connection)
301 pr_err("InitiatorName key not received"
302 " in first login request.\n");
303 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
304 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
309 * For non-leading connections, double check that the
310 * received InitiatorName matches the existing session's
311 * struct iscsi_node_acl.
313 if (!login->leading_connection) {
314 se_nacl = conn->sess->se_sess->se_node_acl;
316 pr_err("Unable to locate"
317 " struct se_node_acl\n");
318 iscsit_tx_login_rsp(conn,
319 ISCSI_STATUS_CLS_INITIATOR_ERR,
320 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
324 if (strcmp(param->value,
325 se_nacl->initiatorname)) {
327 " InitiatorName: %s for this"
328 " iSCSI Initiator Node.\n",
330 iscsit_tx_login_rsp(conn,
331 ISCSI_STATUS_CLS_INITIATOR_ERR,
332 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
342 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
345 struct iscsi_session *sess = conn->sess;
346 struct iscsi_login_rsp *login_rsp;
348 login_rsp = (struct iscsi_login_rsp *) login->rsp;
350 login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
351 hton24(login_rsp->dlength, login->rsp_length);
352 memcpy(login_rsp->isid, login->isid, 6);
353 login_rsp->tsih = cpu_to_be16(login->tsih);
354 login_rsp->itt = login->init_task_tag;
355 login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
356 login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
357 login_rsp->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
359 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
360 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
361 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
362 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
363 ntohl(login_rsp->statsn), login->rsp_length);
365 padding = ((-login->rsp_length) & 3);
367 if (conn->conn_transport->iscsit_put_login_tx(conn, login,
368 login->rsp_length + padding) < 0)
371 login->rsp_length = 0;
372 mutex_lock(&sess->cmdsn_mutex);
373 login_rsp->exp_cmdsn = cpu_to_be32(sess->exp_cmd_sn);
374 login_rsp->max_cmdsn = cpu_to_be32(sess->max_cmd_sn);
375 mutex_unlock(&sess->cmdsn_mutex);
380 static void iscsi_target_sk_data_ready(struct sock *sk, int count)
382 struct iscsi_conn *conn = sk->sk_user_data;
385 pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
387 write_lock_bh(&sk->sk_callback_lock);
388 if (!sk->sk_user_data) {
389 write_unlock_bh(&sk->sk_callback_lock);
392 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
393 write_unlock_bh(&sk->sk_callback_lock);
394 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
397 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
398 write_unlock_bh(&sk->sk_callback_lock);
399 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
402 if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
403 write_unlock_bh(&sk->sk_callback_lock);
404 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
408 rc = schedule_delayed_work(&conn->login_work, 0);
410 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
413 write_unlock_bh(&sk->sk_callback_lock);
416 static void iscsi_target_sk_state_change(struct sock *);
418 static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
426 pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
428 write_lock_bh(&sk->sk_callback_lock);
429 sk->sk_user_data = conn;
430 conn->orig_data_ready = sk->sk_data_ready;
431 conn->orig_state_change = sk->sk_state_change;
432 sk->sk_data_ready = iscsi_target_sk_data_ready;
433 sk->sk_state_change = iscsi_target_sk_state_change;
434 write_unlock_bh(&sk->sk_callback_lock);
436 sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
437 sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
440 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
448 pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
450 write_lock_bh(&sk->sk_callback_lock);
451 if (!sk->sk_user_data) {
452 write_unlock_bh(&sk->sk_callback_lock);
455 sk->sk_user_data = NULL;
456 sk->sk_data_ready = conn->orig_data_ready;
457 sk->sk_state_change = conn->orig_state_change;
458 write_unlock_bh(&sk->sk_callback_lock);
460 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
461 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
464 static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
466 static bool iscsi_target_sk_state_check(struct sock *sk)
468 if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
469 pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
470 "returning FALSE\n");
476 static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
478 struct iscsi_np *np = login->np;
479 bool zero_tsih = login->zero_tsih;
481 iscsi_remove_failed_auth_entry(conn);
482 iscsi_target_nego_release(conn);
483 iscsi_target_login_sess_out(conn, np, zero_tsih, true);
486 static void iscsi_target_login_timeout(unsigned long data)
488 struct iscsi_conn *conn = (struct iscsi_conn *)data;
490 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
492 if (conn->login_kworker) {
493 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
494 conn->login_kworker->comm, conn->login_kworker->pid);
495 send_sig(SIGINT, conn->login_kworker, 1);
499 static void iscsi_target_do_login_rx(struct work_struct *work)
501 struct iscsi_conn *conn = container_of(work,
502 struct iscsi_conn, login_work.work);
503 struct iscsi_login *login = conn->login;
504 struct iscsi_np *np = login->np;
505 struct iscsi_portal_group *tpg = conn->tpg;
506 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
507 struct timer_list login_timer;
508 int rc, zero_tsih = login->zero_tsih;
511 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
512 conn, current->comm, current->pid);
514 spin_lock(&tpg->tpg_state_lock);
515 state = (tpg->tpg_state == TPG_STATE_ACTIVE);
516 spin_unlock(&tpg->tpg_state_lock);
518 if (state == false) {
519 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
520 iscsi_target_restore_sock_callbacks(conn);
521 iscsi_target_login_drop(conn, login);
522 iscsit_deaccess_np(np, tpg, tpg_np);
527 struct sock *sk = conn->sock->sk;
529 read_lock_bh(&sk->sk_callback_lock);
530 state = iscsi_target_sk_state_check(sk);
531 read_unlock_bh(&sk->sk_callback_lock);
533 if (state == false) {
534 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
535 iscsi_target_restore_sock_callbacks(conn);
536 iscsi_target_login_drop(conn, login);
537 iscsit_deaccess_np(np, tpg, tpg_np);
542 conn->login_kworker = current;
543 allow_signal(SIGINT);
545 init_timer(&login_timer);
546 login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
547 login_timer.data = (unsigned long)conn;
548 login_timer.function = iscsi_target_login_timeout;
549 add_timer(&login_timer);
550 pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
552 rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
553 del_timer_sync(&login_timer);
554 flush_signals(current);
555 conn->login_kworker = NULL;
558 iscsi_target_restore_sock_callbacks(conn);
559 iscsi_target_login_drop(conn, login);
560 iscsit_deaccess_np(np, tpg, tpg_np);
564 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
565 conn, current->comm, current->pid);
567 rc = iscsi_target_do_login(conn, login);
569 iscsi_target_restore_sock_callbacks(conn);
570 iscsi_target_login_drop(conn, login);
571 iscsit_deaccess_np(np, tpg, tpg_np);
574 struct sock *sk = conn->sock->sk;
576 write_lock_bh(&sk->sk_callback_lock);
577 clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
578 write_unlock_bh(&sk->sk_callback_lock);
580 } else if (rc == 1) {
581 iscsi_target_nego_release(conn);
582 iscsi_post_login_handler(np, conn, zero_tsih);
583 iscsit_deaccess_np(np, tpg, tpg_np);
587 static void iscsi_target_do_cleanup(struct work_struct *work)
589 struct iscsi_conn *conn = container_of(work,
590 struct iscsi_conn, login_cleanup_work.work);
591 struct sock *sk = conn->sock->sk;
592 struct iscsi_login *login = conn->login;
593 struct iscsi_np *np = login->np;
594 struct iscsi_portal_group *tpg = conn->tpg;
595 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
597 pr_debug("Entering iscsi_target_do_cleanup\n");
599 cancel_delayed_work_sync(&conn->login_work);
600 conn->orig_state_change(sk);
602 iscsi_target_restore_sock_callbacks(conn);
603 iscsi_target_login_drop(conn, login);
604 iscsit_deaccess_np(np, tpg, tpg_np);
606 pr_debug("iscsi_target_do_cleanup done()\n");
609 static void iscsi_target_sk_state_change(struct sock *sk)
611 struct iscsi_conn *conn;
612 void (*orig_state_change)(struct sock *);
615 pr_debug("Entering iscsi_target_sk_state_change\n");
617 write_lock_bh(&sk->sk_callback_lock);
618 conn = sk->sk_user_data;
620 write_unlock_bh(&sk->sk_callback_lock);
623 orig_state_change = conn->orig_state_change;
625 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
626 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
628 write_unlock_bh(&sk->sk_callback_lock);
629 orig_state_change(sk);
632 if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
633 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
634 " conn: %p\n", conn);
635 write_unlock_bh(&sk->sk_callback_lock);
636 orig_state_change(sk);
639 if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
640 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
642 write_unlock_bh(&sk->sk_callback_lock);
643 orig_state_change(sk);
647 state = iscsi_target_sk_state_check(sk);
648 write_unlock_bh(&sk->sk_callback_lock);
650 pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
653 pr_debug("iscsi_target_sk_state_change got failed state\n");
654 schedule_delayed_work(&conn->login_cleanup_work, 0);
657 orig_state_change(sk);
661 * NOTE: We check for existing sessions or connections AFTER the initiator
662 * has been successfully authenticated in order to protect against faked
663 * ISID/TSIH combinations.
665 static int iscsi_target_check_for_existing_instances(
666 struct iscsi_conn *conn,
667 struct iscsi_login *login)
669 if (login->checked_for_existing)
672 login->checked_for_existing = 1;
675 return iscsi_check_for_session_reinstatement(conn);
677 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
678 login->initial_exp_statsn);
681 static int iscsi_target_do_authentication(
682 struct iscsi_conn *conn,
683 struct iscsi_login *login)
687 struct iscsi_param *param;
688 struct iscsi_login_req *login_req;
689 struct iscsi_login_rsp *login_rsp;
691 login_req = (struct iscsi_login_req *) login->req;
692 login_rsp = (struct iscsi_login_rsp *) login->rsp;
693 payload_length = ntoh24(login_req->dlength);
695 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
699 authret = iscsi_handle_authentication(
708 pr_debug("Received OK response"
709 " from LIO Authentication, continuing.\n");
712 pr_debug("iSCSI security negotiation"
713 " completed successfully.\n");
714 login->auth_complete = 1;
715 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
716 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
717 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
718 ISCSI_FLAG_LOGIN_TRANSIT);
719 login->current_stage = 1;
721 return iscsi_target_check_for_existing_instances(
724 pr_err("Security negotiation"
726 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
727 ISCSI_LOGIN_STATUS_AUTH_FAILED);
730 pr_err("Received unknown error %d from LIO"
731 " Authentication\n", authret);
732 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
733 ISCSI_LOGIN_STATUS_TARGET_ERROR);
740 static int iscsi_target_handle_csg_zero(
741 struct iscsi_conn *conn,
742 struct iscsi_login *login)
746 struct iscsi_param *param;
747 struct iscsi_login_req *login_req;
748 struct iscsi_login_rsp *login_rsp;
750 login_req = (struct iscsi_login_req *) login->req;
751 login_rsp = (struct iscsi_login_rsp *) login->rsp;
752 payload_length = ntoh24(login_req->dlength);
754 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
758 ret = iscsi_decode_text_input(
759 PHASE_SECURITY|PHASE_DECLARATIVE,
760 SENDER_INITIATOR|SENDER_RECEIVER,
768 if (login->auth_complete) {
769 pr_err("Initiator has already been"
770 " successfully authenticated, but is still"
771 " sending %s keys.\n", param->value);
772 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
773 ISCSI_LOGIN_STATUS_INIT_ERR);
780 if (login->first_request)
781 if (iscsi_target_check_first_request(conn, login) < 0)
784 ret = iscsi_encode_text_output(
785 PHASE_SECURITY|PHASE_DECLARATIVE,
793 if (!iscsi_check_negotiated_keys(conn->param_list)) {
794 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
795 !strncmp(param->value, NONE, 4)) {
796 pr_err("Initiator sent AuthMethod=None but"
797 " Target is enforcing iSCSI Authentication,"
799 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
800 ISCSI_LOGIN_STATUS_AUTH_FAILED);
804 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
805 !login->auth_complete)
808 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
811 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
812 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
813 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
814 ISCSI_FLAG_LOGIN_TRANSIT;
815 login->current_stage = 1;
821 return iscsi_target_do_authentication(conn, login);
824 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
828 struct iscsi_login_req *login_req;
829 struct iscsi_login_rsp *login_rsp;
831 login_req = (struct iscsi_login_req *) login->req;
832 login_rsp = (struct iscsi_login_rsp *) login->rsp;
833 payload_length = ntoh24(login_req->dlength);
835 ret = iscsi_decode_text_input(
836 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
837 SENDER_INITIATOR|SENDER_RECEIVER,
842 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
843 ISCSI_LOGIN_STATUS_INIT_ERR);
847 if (login->first_request)
848 if (iscsi_target_check_first_request(conn, login) < 0)
851 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
854 ret = iscsi_encode_text_output(
855 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
861 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
862 ISCSI_LOGIN_STATUS_INIT_ERR);
866 if (!login->auth_complete &&
867 ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) {
868 pr_err("Initiator is requesting CSG: 1, has not been"
869 " successfully authenticated, and the Target is"
870 " enforcing iSCSI Authentication, login failed.\n");
871 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
872 ISCSI_LOGIN_STATUS_AUTH_FAILED);
876 if (!iscsi_check_negotiated_keys(conn->param_list))
877 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
878 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
879 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
880 ISCSI_FLAG_LOGIN_TRANSIT;
885 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
888 struct iscsi_login_req *login_req;
889 struct iscsi_login_rsp *login_rsp;
891 login_req = (struct iscsi_login_req *) login->req;
892 login_rsp = (struct iscsi_login_rsp *) login->rsp;
895 if (++pdu_count > MAX_LOGIN_PDUS) {
896 pr_err("MAX_LOGIN_PDUS count reached.\n");
897 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
898 ISCSI_LOGIN_STATUS_TARGET_ERROR);
902 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
904 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
905 if (iscsi_target_handle_csg_zero(conn, login) < 0)
909 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
910 if (iscsi_target_handle_csg_one(conn, login) < 0)
912 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
913 login->tsih = conn->sess->tsih;
914 login->login_complete = 1;
915 iscsi_target_restore_sock_callbacks(conn);
916 if (iscsi_target_do_tx_login_io(conn,
923 pr_err("Illegal CSG: %d received from"
924 " Initiator, protocol error.\n",
925 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
929 if (iscsi_target_do_tx_login_io(conn, login) < 0)
932 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
933 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
934 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
940 struct sock *sk = conn->sock->sk;
943 read_lock_bh(&sk->sk_callback_lock);
944 state = iscsi_target_sk_state_check(sk);
945 read_unlock_bh(&sk->sk_callback_lock);
948 pr_debug("iscsi_target_do_login() failed state for"
949 " conn: %p\n", conn);
957 static void iscsi_initiatorname_tolower(
961 u32 iqn_size = strlen(param_buf), i;
963 for (i = 0; i < iqn_size; i++) {
973 * Processes the first Login Request..
975 int iscsi_target_locate_portal(
977 struct iscsi_conn *conn,
978 struct iscsi_login *login)
980 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
981 char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
982 struct iscsi_session *sess = conn->sess;
983 struct iscsi_tiqn *tiqn;
984 struct iscsi_tpg_np *tpg_np = NULL;
985 struct iscsi_login_req *login_req;
987 int sessiontype = 0, ret = 0;
989 INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
990 INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
991 iscsi_target_set_sock_callbacks(conn);
995 login_req = (struct iscsi_login_req *) login->req;
996 payload_length = ntoh24(login_req->dlength);
998 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1000 pr_err("Unable to allocate memory for tmpbuf.\n");
1004 memcpy(tmpbuf, login->req_buf, payload_length);
1005 tmpbuf[payload_length] = '\0';
1007 end = (start + payload_length);
1010 * Locate the initial keys expected from the Initiator node in
1011 * the first login request in order to progress with the login phase.
1013 while (start < end) {
1014 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1019 if (!strncmp(key, "InitiatorName", 13))
1021 else if (!strncmp(key, "SessionType", 11))
1023 else if (!strncmp(key, "TargetName", 10))
1026 start += strlen(key) + strlen(value) + 2;
1029 * See 5.3. Login Phase.
1032 pr_err("InitiatorName key not received"
1033 " in first login request.\n");
1034 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1035 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1040 * Convert the incoming InitiatorName to lowercase following
1041 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1042 * are NOT case sensitive.
1044 iscsi_initiatorname_tolower(i_buf);
1047 if (!login->leading_connection)
1050 pr_err("SessionType key not received"
1051 " in first login request.\n");
1052 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1053 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1059 * Use default portal group for discovery sessions.
1061 sessiontype = strncmp(s_buf, DISCOVERY, 9);
1063 conn->tpg = iscsit_global->discovery_tpg;
1064 if (!login->leading_connection)
1067 sess->sess_ops->SessionType = 1;
1069 * Setup crc32c modules from libcrypto
1071 if (iscsi_login_setup_crypto(conn) < 0) {
1072 pr_err("iscsi_login_setup_crypto() failed\n");
1077 * Serialize access across the discovery struct iscsi_portal_group to
1078 * process login attempt.
1080 if (iscsit_access_np(np, conn->tpg) < 0) {
1081 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1082 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1092 pr_err("TargetName key not received"
1093 " in first login request while"
1094 " SessionType=Normal.\n");
1095 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1096 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1102 * Locate Target IQN from Storage Node.
1104 tiqn = iscsit_get_tiqn_for_login(t_buf);
1106 pr_err("Unable to locate Target IQN: %s in"
1107 " Storage Node\n", t_buf);
1108 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1109 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1113 pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1116 * Locate Target Portal Group from Storage Node.
1118 conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1120 pr_err("Unable to locate Target Portal Group"
1121 " on %s\n", tiqn->tiqn);
1122 iscsit_put_tiqn_for_login(tiqn);
1123 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1124 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1128 conn->tpg_np = tpg_np;
1129 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1131 * Setup crc32c modules from libcrypto
1133 if (iscsi_login_setup_crypto(conn) < 0) {
1134 pr_err("iscsi_login_setup_crypto() failed\n");
1135 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1136 iscsit_put_tiqn_for_login(tiqn);
1142 * Serialize access across the struct iscsi_portal_group to
1143 * process login attempt.
1145 if (iscsit_access_np(np, conn->tpg) < 0) {
1146 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1147 iscsit_put_tiqn_for_login(tiqn);
1148 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1149 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1156 * conn->sess->node_acl will be set when the referenced
1157 * struct iscsi_session is located from received ISID+TSIH in
1158 * iscsi_login_non_zero_tsih_s2().
1160 if (!login->leading_connection) {
1166 * This value is required in iscsi_login_zero_tsih_s2()
1168 sess->sess_ops->SessionType = 0;
1171 * Locate incoming Initiator IQN reference from Storage Node.
1173 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1174 &conn->tpg->tpg_se_tpg, i_buf);
1175 if (!sess->se_sess->se_node_acl) {
1176 pr_err("iSCSI Initiator Node: %s is not authorized to"
1177 " access iSCSI target portal group: %hu.\n",
1178 i_buf, conn->tpg->tpgt);
1179 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1180 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1191 int iscsi_target_start_negotiation(
1192 struct iscsi_login *login,
1193 struct iscsi_conn *conn)
1197 ret = iscsi_target_do_login(conn, login);
1200 struct sock *sk = conn->sock->sk;
1202 write_lock_bh(&sk->sk_callback_lock);
1203 set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1204 write_unlock_bh(&sk->sk_callback_lock);
1206 } else if (ret < 0) {
1207 cancel_delayed_work_sync(&conn->login_work);
1208 cancel_delayed_work_sync(&conn->login_cleanup_work);
1209 iscsi_target_restore_sock_callbacks(conn);
1210 iscsi_remove_failed_auth_entry(conn);
1213 iscsi_target_nego_release(conn);
1218 void iscsi_target_nego_release(struct iscsi_conn *conn)
1220 struct iscsi_login *login = conn->conn_login;
1225 kfree(login->req_buf);
1226 kfree(login->rsp_buf);
1229 conn->conn_login = NULL;