sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_nego.c
blob46388c9e08dad3e5de751d3280f8fc829683154d
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 <net/sock.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
38 #define TEXT_LEN 4096
40 void convert_null_to_semi(char *buf, int len)
42 int i;
44 for (i = 0; i < len; i++)
45 if (buf[i] == '\0')
46 buf[i] = ';';
49 static int strlen_semi(char *buf)
51 int i = 0;
53 while (buf[i] != '\0') {
54 if (buf[i] == ';')
55 return i;
56 i++;
59 return -1;
62 int extract_param(
63 const char *in_buf,
64 const char *pattern,
65 unsigned int max_length,
66 char *out_buf,
67 unsigned char *type)
69 char *ptr;
70 int len;
72 if (!in_buf || !pattern || !out_buf || !type)
73 return -1;
75 ptr = strstr(in_buf, pattern);
76 if (!ptr)
77 return -1;
79 ptr = strstr(ptr, "=");
80 if (!ptr)
81 return -1;
83 ptr += 1;
84 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
85 ptr += 2; /* skip 0x */
86 *type = HEX;
87 } else
88 *type = DECIMAL;
90 len = strlen_semi(ptr);
91 if (len < 0)
92 return -1;
94 if (len >= max_length) {
95 pr_err("Length of input: %d exceeds max_length:"
96 " %d\n", len, max_length);
97 return -1;
99 memcpy(out_buf, ptr, len);
100 out_buf[len] = '\0';
102 return 0;
105 static u32 iscsi_handle_authentication(
106 struct iscsi_conn *conn,
107 char *in_buf,
108 char *out_buf,
109 int in_length,
110 int *out_length,
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;
124 if (!se_nacl) {
125 pr_err("Unable to locate struct se_node_acl for"
126 " CHAP auth\n");
127 return -1;
129 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
130 se_node_acl);
131 if (!iscsi_nacl) {
132 pr_err("Unable to locate struct iscsi_node_acl for"
133 " CHAP auth\n");
134 return -1;
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;
142 } else {
143 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
144 se_node_acl);
146 auth = &iscsi_nacl->node_auth;
148 } else {
150 * For SessionType=Discovery
152 auth = &iscsit_global->discovery_acl.node_auth;
155 if (strstr("CHAP", authtype))
156 strcpy(conn->sess->auth_type, "CHAP");
157 else
158 strcpy(conn->sess->auth_type, NONE);
160 if (strstr("None", authtype))
161 return 1;
162 #ifdef CANSRP
163 else if (strstr("SRP", authtype))
164 return srp_main_loop(conn, auth, in_buf, out_buf,
165 &in_length, out_length);
166 #endif
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))
171 return 2;
172 else if (strstr("SPKM2", authtype))
173 return 2;
174 else if (strstr("KRB5", authtype))
175 return 2;
176 else
177 return 2;
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;
190 u32 payload_length;
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) {
197 case ISCSI_OP_LOGIN:
198 break;
199 default:
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);
204 return -1;
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);
213 return -1;
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,
222 req_csg);
223 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
224 ISCSI_LOGIN_STATUS_INIT_ERR);
225 return -1;
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);
236 return -1;
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);
246 return -1;
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);
254 return -1;
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);
262 return -1;
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);
269 return -1;
272 return 0;
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);
292 return -1;
294 if (!strncmp(param->value, DISCOVERY, 9))
295 return 0;
298 if (!strncmp(param->name, INITIATORNAME, 13)) {
299 if (!IS_PSTATE_ACCEPTOR(param)) {
300 if (!login->leading_connection)
301 continue;
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);
307 return -1;
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;
317 if (!se_nacl) {
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);
323 return -1;
326 if (strcmp(param->value,
327 se_nacl->initiatorname)) {
328 pr_err("Incorrect"
329 " InitiatorName: %s for this"
330 " iSCSI Initiator Node.\n",
331 param->value);
332 iscsit_tx_login_rsp(conn,
333 ISCSI_STATUS_CLS_INITIATOR_ERR,
334 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
335 return -1;
341 return 0;
344 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
346 u32 padding = 0;
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);
375 if (rc) {
376 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
377 ISCSI_LOGIN_STATUS_NO_RESOURCES);
378 return -1;
382 if (conn->conn_transport->iscsit_put_login_tx(conn, login,
383 login->rsp_length + padding) < 0)
384 goto err;
386 login->rsp_length = 0;
388 return 0;
390 err:
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,
403 get_order(1));
404 spin_unlock(&iscsit_global->ts_bitmap_lock);
406 return -1;
409 static void iscsi_target_sk_data_ready(struct sock *sk)
411 struct iscsi_conn *conn = sk->sk_user_data;
412 bool rc;
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);
419 return;
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);
424 return;
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);
429 return;
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);
434 return;
437 rc = schedule_delayed_work(&conn->login_work, 0);
438 if (!rc) {
439 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
440 " got false\n");
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)
449 struct sock *sk;
451 if (!conn->sock)
452 return;
454 sk = conn->sock->sk;
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)
471 struct sock *sk;
473 if (!conn->sock)
474 return;
476 sk = conn->sock->sk;
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);
482 return;
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");
500 return false;
502 return true;
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;
538 bool state;
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);
547 if (!state) {
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);
552 return;
555 if (conn->sock) {
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);
562 if (!state) {
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);
567 return;
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;
586 if (rc < 0) {
587 iscsi_target_restore_sock_callbacks(conn);
588 iscsi_target_login_drop(conn, login);
589 iscsit_deaccess_np(np, tpg, tpg_np);
590 return;
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);
597 if (rc < 0) {
598 iscsi_target_restore_sock_callbacks(conn);
599 iscsi_target_login_drop(conn, login);
600 iscsit_deaccess_np(np, tpg, tpg_np);
601 } else if (!rc) {
602 if (conn->sock) {
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 *);
642 bool state;
644 pr_debug("Entering iscsi_target_sk_state_change\n");
646 write_lock_bh(&sk->sk_callback_lock);
647 conn = sk->sk_user_data;
648 if (!conn) {
649 write_unlock_bh(&sk->sk_callback_lock);
650 return;
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",
656 conn);
657 write_unlock_bh(&sk->sk_callback_lock);
658 orig_state_change(sk);
659 return;
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);
666 return;
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",
670 conn);
671 write_unlock_bh(&sk->sk_callback_lock);
672 orig_state_change(sk);
673 return;
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);
681 if (!state) {
682 pr_debug("iscsi_target_sk_state_change got failed state\n");
683 schedule_delayed_work(&conn->login_cleanup_work, 0);
684 return;
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)
699 return 0;
701 login->checked_for_existing = 1;
703 if (!login->tsih)
704 return iscsi_check_for_session_reinstatement(conn);
705 else
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)
714 int authret;
715 u32 payload_length;
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);
725 if (!param)
726 return -1;
728 authret = iscsi_handle_authentication(
729 conn,
730 login->req_buf,
731 login->rsp_buf,
732 payload_length,
733 &login->rsp_length,
734 param->value);
735 switch (authret) {
736 case 0:
737 pr_debug("Received OK response"
738 " from LIO Authentication, continuing.\n");
739 break;
740 case 1:
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(
751 conn, login);
752 case 2:
753 pr_err("Security negotiation"
754 " failed.\n");
755 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
756 ISCSI_LOGIN_STATUS_AUTH_FAILED);
757 return -1;
758 default:
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);
763 return -1;
766 return 0;
769 static int iscsi_target_handle_csg_zero(
770 struct iscsi_conn *conn,
771 struct iscsi_login *login)
773 int ret;
774 u32 payload_length;
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);
784 if (!param)
785 return -1;
787 ret = iscsi_decode_text_input(
788 PHASE_SECURITY|PHASE_DECLARATIVE,
789 SENDER_INITIATOR|SENDER_RECEIVER,
790 login->req_buf,
791 payload_length,
792 conn);
793 if (ret < 0)
794 return -1;
796 if (ret > 0) {
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);
803 return -1;
806 goto do_auth;
807 } else if (!payload_length) {
808 pr_err("Initiator sent zero length security payload,"
809 " login failed\n");
810 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
811 ISCSI_LOGIN_STATUS_AUTH_FAILED);
812 return -1;
815 if (login->first_request)
816 if (iscsi_target_check_first_request(conn, login) < 0)
817 return -1;
819 ret = iscsi_encode_text_output(
820 PHASE_SECURITY|PHASE_DECLARATIVE,
821 SENDER_TARGET,
822 login->rsp_buf,
823 &login->rsp_length,
824 conn->param_list);
825 if (ret < 0)
826 return -1;
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,"
833 " login failed.\n");
834 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
835 ISCSI_LOGIN_STATUS_AUTH_FAILED);
836 return -1;
839 if (conn->tpg->tpg_attrib.authentication &&
840 !login->auth_complete)
841 return 0;
843 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
844 return 0;
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;
854 return 0;
855 do_auth:
856 return iscsi_target_do_authentication(conn, login);
859 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
861 int ret;
862 u32 payload_length;
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,
873 login->req_buf,
874 payload_length,
875 conn);
876 if (ret < 0) {
877 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
878 ISCSI_LOGIN_STATUS_INIT_ERR);
879 return -1;
882 if (login->first_request)
883 if (iscsi_target_check_first_request(conn, login) < 0)
884 return -1;
886 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
887 return -1;
889 ret = iscsi_encode_text_output(
890 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
891 SENDER_TARGET,
892 login->rsp_buf,
893 &login->rsp_length,
894 conn->param_list);
895 if (ret < 0) {
896 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
897 ISCSI_LOGIN_STATUS_INIT_ERR);
898 return -1;
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);
908 return -1;
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;
917 return 0;
920 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
922 int pdu_count = 0;
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;
929 while (1) {
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);
934 return -1;
937 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
938 case 0:
939 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
940 if (iscsi_target_handle_csg_zero(conn, login) < 0)
941 return -1;
942 break;
943 case 1:
944 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
945 if (iscsi_target_handle_csg_one(conn, login) < 0)
946 return -1;
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,
952 login) < 0)
953 return -1;
954 return 1;
956 break;
957 default:
958 pr_err("Illegal CSG: %d received from"
959 " Initiator, protocol error.\n",
960 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
961 break;
964 if (iscsi_target_do_tx_login_io(conn, login) < 0)
965 return -1;
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;
971 break;
974 if (conn->sock) {
975 struct sock *sk = conn->sock->sk;
976 bool state;
978 read_lock_bh(&sk->sk_callback_lock);
979 state = iscsi_target_sk_state_check(sk);
980 read_unlock_bh(&sk->sk_callback_lock);
982 if (!state) {
983 pr_debug("iscsi_target_do_login() failed state for"
984 " conn: %p\n", conn);
985 return -1;
989 return 0;
992 static void iscsi_initiatorname_tolower(
993 char *param_buf)
995 char *c;
996 u32 iqn_size = strlen(param_buf), i;
998 for (i = 0; i < iqn_size; i++) {
999 c = &param_buf[i];
1000 if (!isupper(*c))
1001 continue;
1003 *c = tolower(*c);
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);
1029 login->np = np;
1031 login_req = (struct iscsi_login_req *) login->req;
1032 payload_length = ntoh24(login_req->dlength);
1034 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1035 if (!tmpbuf) {
1036 pr_err("Unable to allocate memory for tmpbuf.\n");
1037 return -1;
1040 memcpy(tmpbuf, login->req_buf, payload_length);
1041 tmpbuf[payload_length] = '\0';
1042 start = tmpbuf;
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) {
1051 ret = -1;
1052 goto out;
1055 if (!strncmp(key, "InitiatorName", 13))
1056 i_buf = value;
1057 else if (!strncmp(key, "SessionType", 11))
1058 s_buf = value;
1059 else if (!strncmp(key, "TargetName", 10))
1060 t_buf = value;
1062 start += strlen(key) + strlen(value) + 2;
1065 * See 5.3. Login Phase.
1067 if (!i_buf) {
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);
1072 ret = -1;
1073 goto out;
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);
1082 if (!s_buf) {
1083 if (!login->leading_connection)
1084 goto get_target;
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);
1090 ret = -1;
1091 goto out;
1095 * Use default portal group for discovery sessions.
1097 sessiontype = strncmp(s_buf, DISCOVERY, 9);
1098 if (!sessiontype) {
1099 conn->tpg = iscsit_global->discovery_tpg;
1100 if (!login->leading_connection)
1101 goto get_target;
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");
1109 ret = -1;
1110 goto out;
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);
1119 ret = -1;
1120 goto out;
1122 ret = 0;
1123 goto alloc_tags;
1126 get_target:
1127 if (!t_buf) {
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);
1133 ret = -1;
1134 goto out;
1138 * Locate Target IQN from Storage Node.
1140 tiqn = iscsit_get_tiqn_for_login(t_buf);
1141 if (!tiqn) {
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);
1146 ret = -1;
1147 goto out;
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);
1155 if (!conn->tpg) {
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);
1161 ret = -1;
1162 goto out;
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);
1173 conn->tpg = NULL;
1174 ret = -1;
1175 goto out;
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);
1186 conn->tpg = NULL;
1187 ret = -1;
1188 goto out;
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) {
1197 ret = 0;
1198 goto out;
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);
1217 ret = -1;
1218 goto out;
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
1225 * commands.
1227 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1228 * in per-cpu-ida tag allocation logic + small queue_depth.
1230 alloc_tags:
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);
1236 if (ret < 0) {
1237 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1238 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1239 ret = -1;
1241 out:
1242 kfree(tmpbuf);
1243 return ret;
1246 int iscsi_target_start_negotiation(
1247 struct iscsi_login *login,
1248 struct iscsi_conn *conn)
1250 int ret;
1252 if (conn->sock) {
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);
1261 if (ret < 0) {
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);
1267 if (ret != 0)
1268 iscsi_target_nego_release(conn);
1270 return ret;
1273 void iscsi_target_nego_release(struct iscsi_conn *conn)
1275 struct iscsi_login *login = conn->conn_login;
1277 if (!login)
1278 return;
1280 kfree(login->req_buf);
1281 kfree(login->rsp_buf);
1282 kfree(login);
1284 conn->conn_login = NULL;