gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_nego.c
blob9a96f1712b7a3ee28aa156e5be21a60241e30174
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 <scsi/iscsi_proto.h>
22 #include <target/target_core_base.h>
23 #include <target/target_core_fabric.h>
24 #include <target/iscsi/iscsi_transport.h>
26 #include <target/iscsi/iscsi_target_core.h>
27 #include "iscsi_target_parameters.h"
28 #include "iscsi_target_login.h"
29 #include "iscsi_target_nego.h"
30 #include "iscsi_target_tpg.h"
31 #include "iscsi_target_util.h"
32 #include "iscsi_target.h"
33 #include "iscsi_target_auth.h"
35 #define MAX_LOGIN_PDUS 7
36 #define TEXT_LEN 4096
38 void convert_null_to_semi(char *buf, int len)
40 int i;
42 for (i = 0; i < len; i++)
43 if (buf[i] == '\0')
44 buf[i] = ';';
47 static int strlen_semi(char *buf)
49 int i = 0;
51 while (buf[i] != '\0') {
52 if (buf[i] == ';')
53 return i;
54 i++;
57 return -1;
60 int extract_param(
61 const char *in_buf,
62 const char *pattern,
63 unsigned int max_length,
64 char *out_buf,
65 unsigned char *type)
67 char *ptr;
68 int len;
70 if (!in_buf || !pattern || !out_buf || !type)
71 return -1;
73 ptr = strstr(in_buf, pattern);
74 if (!ptr)
75 return -1;
77 ptr = strstr(ptr, "=");
78 if (!ptr)
79 return -1;
81 ptr += 1;
82 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
83 ptr += 2; /* skip 0x */
84 *type = HEX;
85 } else
86 *type = DECIMAL;
88 len = strlen_semi(ptr);
89 if (len < 0)
90 return -1;
92 if (len >= max_length) {
93 pr_err("Length of input: %d exceeds max_length:"
94 " %d\n", len, max_length);
95 return -1;
97 memcpy(out_buf, ptr, len);
98 out_buf[len] = '\0';
100 return 0;
103 static u32 iscsi_handle_authentication(
104 struct iscsi_conn *conn,
105 char *in_buf,
106 char *out_buf,
107 int in_length,
108 int *out_length,
109 unsigned char *authtype)
111 struct iscsi_session *sess = conn->sess;
112 struct iscsi_node_auth *auth;
113 struct iscsi_node_acl *iscsi_nacl;
114 struct iscsi_portal_group *iscsi_tpg;
115 struct se_node_acl *se_nacl;
117 if (!sess->sess_ops->SessionType) {
119 * For SessionType=Normal
121 se_nacl = conn->sess->se_sess->se_node_acl;
122 if (!se_nacl) {
123 pr_err("Unable to locate struct se_node_acl for"
124 " CHAP auth\n");
125 return -1;
127 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
128 se_node_acl);
129 if (!iscsi_nacl) {
130 pr_err("Unable to locate struct iscsi_node_acl for"
131 " CHAP auth\n");
132 return -1;
135 if (se_nacl->dynamic_node_acl) {
136 iscsi_tpg = container_of(se_nacl->se_tpg,
137 struct iscsi_portal_group, tpg_se_tpg);
139 auth = &iscsi_tpg->tpg_demo_auth;
140 } else {
141 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
142 se_node_acl);
144 auth = &iscsi_nacl->node_auth;
146 } else {
148 * For SessionType=Discovery
150 auth = &iscsit_global->discovery_acl.node_auth;
153 if (strstr("CHAP", authtype))
154 strcpy(conn->sess->auth_type, "CHAP");
155 else
156 strcpy(conn->sess->auth_type, NONE);
158 if (strstr("None", authtype))
159 return 1;
160 #ifdef CANSRP
161 else if (strstr("SRP", authtype))
162 return srp_main_loop(conn, auth, in_buf, out_buf,
163 &in_length, out_length);
164 #endif
165 else if (strstr("CHAP", authtype))
166 return chap_main_loop(conn, auth, in_buf, out_buf,
167 &in_length, out_length);
168 else if (strstr("SPKM1", authtype))
169 return 2;
170 else if (strstr("SPKM2", authtype))
171 return 2;
172 else if (strstr("KRB5", authtype))
173 return 2;
174 else
175 return 2;
178 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
180 kfree(conn->auth_protocol);
183 int iscsi_target_check_login_request(
184 struct iscsi_conn *conn,
185 struct iscsi_login *login)
187 int req_csg, req_nsg;
188 u32 payload_length;
189 struct iscsi_login_req *login_req;
191 login_req = (struct iscsi_login_req *) login->req;
192 payload_length = ntoh24(login_req->dlength);
194 switch (login_req->opcode & ISCSI_OPCODE_MASK) {
195 case ISCSI_OP_LOGIN:
196 break;
197 default:
198 pr_err("Received unknown opcode 0x%02x.\n",
199 login_req->opcode & ISCSI_OPCODE_MASK);
200 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
201 ISCSI_LOGIN_STATUS_INIT_ERR);
202 return -1;
205 if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
206 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
207 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
208 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
209 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
210 ISCSI_LOGIN_STATUS_INIT_ERR);
211 return -1;
214 req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
215 req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
217 if (req_csg != login->current_stage) {
218 pr_err("Initiator unexpectedly changed login stage"
219 " from %d to %d, login failed.\n", login->current_stage,
220 req_csg);
221 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
222 ISCSI_LOGIN_STATUS_INIT_ERR);
223 return -1;
226 if ((req_nsg == 2) || (req_csg >= 2) ||
227 ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
228 (req_nsg <= req_csg))) {
229 pr_err("Illegal login_req->flags Combination, CSG: %d,"
230 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
231 req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
232 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
233 ISCSI_LOGIN_STATUS_INIT_ERR);
234 return -1;
237 if ((login_req->max_version != login->version_max) ||
238 (login_req->min_version != login->version_min)) {
239 pr_err("Login request changed Version Max/Nin"
240 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
241 login_req->max_version, login_req->min_version);
242 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
243 ISCSI_LOGIN_STATUS_INIT_ERR);
244 return -1;
247 if (memcmp(login_req->isid, login->isid, 6) != 0) {
248 pr_err("Login request changed ISID unexpectedly,"
249 " protocol error.\n");
250 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
251 ISCSI_LOGIN_STATUS_INIT_ERR);
252 return -1;
255 if (login_req->itt != login->init_task_tag) {
256 pr_err("Login request changed ITT unexpectedly to"
257 " 0x%08x, protocol error.\n", login_req->itt);
258 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
259 ISCSI_LOGIN_STATUS_INIT_ERR);
260 return -1;
263 if (payload_length > MAX_KEY_VALUE_PAIRS) {
264 pr_err("Login request payload exceeds default"
265 " MaxRecvDataSegmentLength: %u, protocol error.\n",
266 MAX_KEY_VALUE_PAIRS);
267 return -1;
270 return 0;
273 static int iscsi_target_check_first_request(
274 struct iscsi_conn *conn,
275 struct iscsi_login *login)
277 struct iscsi_param *param = NULL;
278 struct se_node_acl *se_nacl;
280 login->first_request = 0;
282 list_for_each_entry(param, &conn->param_list->param_list, p_list) {
283 if (!strncmp(param->name, SESSIONTYPE, 11)) {
284 if (!IS_PSTATE_ACCEPTOR(param)) {
285 pr_err("SessionType key not received"
286 " in first login request.\n");
287 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
288 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
289 return -1;
291 if (!strncmp(param->value, DISCOVERY, 9))
292 return 0;
295 if (!strncmp(param->name, INITIATORNAME, 13)) {
296 if (!IS_PSTATE_ACCEPTOR(param)) {
297 if (!login->leading_connection)
298 continue;
300 pr_err("InitiatorName key not received"
301 " in first login request.\n");
302 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
303 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
304 return -1;
308 * For non-leading connections, double check that the
309 * received InitiatorName matches the existing session's
310 * struct iscsi_node_acl.
312 if (!login->leading_connection) {
313 se_nacl = conn->sess->se_sess->se_node_acl;
314 if (!se_nacl) {
315 pr_err("Unable to locate"
316 " struct se_node_acl\n");
317 iscsit_tx_login_rsp(conn,
318 ISCSI_STATUS_CLS_INITIATOR_ERR,
319 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
320 return -1;
323 if (strcmp(param->value,
324 se_nacl->initiatorname)) {
325 pr_err("Incorrect"
326 " InitiatorName: %s for this"
327 " iSCSI Initiator Node.\n",
328 param->value);
329 iscsit_tx_login_rsp(conn,
330 ISCSI_STATUS_CLS_INITIATOR_ERR,
331 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
332 return -1;
338 return 0;
341 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
343 u32 padding = 0;
344 struct iscsi_session *sess = conn->sess;
345 struct iscsi_login_rsp *login_rsp;
347 login_rsp = (struct iscsi_login_rsp *) login->rsp;
349 login_rsp->opcode = ISCSI_OP_LOGIN_RSP;
350 hton24(login_rsp->dlength, login->rsp_length);
351 memcpy(login_rsp->isid, login->isid, 6);
352 login_rsp->tsih = cpu_to_be16(login->tsih);
353 login_rsp->itt = login->init_task_tag;
354 login_rsp->statsn = cpu_to_be32(conn->stat_sn++);
355 login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
356 login_rsp->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
358 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
359 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
360 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
361 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
362 ntohl(login_rsp->statsn), login->rsp_length);
364 padding = ((-login->rsp_length) & 3);
366 * Before sending the last login response containing the transition
367 * bit for full-feature-phase, go ahead and start up TX/RX threads
368 * now to avoid potential resource allocation failures after the
369 * final login response has been sent.
371 if (login->login_complete) {
372 int rc = iscsit_start_kthreads(conn);
373 if (rc) {
374 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
375 ISCSI_LOGIN_STATUS_NO_RESOURCES);
376 return -1;
380 if (conn->conn_transport->iscsit_put_login_tx(conn, login,
381 login->rsp_length + padding) < 0)
382 goto err;
384 login->rsp_length = 0;
385 mutex_lock(&sess->cmdsn_mutex);
386 login_rsp->exp_cmdsn = cpu_to_be32(sess->exp_cmd_sn);
387 login_rsp->max_cmdsn = cpu_to_be32(sess->max_cmd_sn);
388 mutex_unlock(&sess->cmdsn_mutex);
390 return 0;
392 err:
393 if (login->login_complete) {
394 if (conn->rx_thread && conn->rx_thread_active) {
395 send_sig(SIGINT, conn->rx_thread, 1);
396 complete(&conn->rx_login_comp);
397 kthread_stop(conn->rx_thread);
399 if (conn->tx_thread && conn->tx_thread_active) {
400 send_sig(SIGINT, conn->tx_thread, 1);
401 kthread_stop(conn->tx_thread);
403 spin_lock(&iscsit_global->ts_bitmap_lock);
404 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
405 get_order(1));
406 spin_unlock(&iscsit_global->ts_bitmap_lock);
408 return -1;
411 static void iscsi_target_sk_data_ready(struct sock *sk)
413 struct iscsi_conn *conn = sk->sk_user_data;
414 bool rc;
416 pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
418 write_lock_bh(&sk->sk_callback_lock);
419 if (!sk->sk_user_data) {
420 write_unlock_bh(&sk->sk_callback_lock);
421 return;
423 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
424 write_unlock_bh(&sk->sk_callback_lock);
425 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
426 return;
428 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
429 write_unlock_bh(&sk->sk_callback_lock);
430 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
431 return;
433 if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
434 write_unlock_bh(&sk->sk_callback_lock);
435 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
436 return;
439 rc = schedule_delayed_work(&conn->login_work, 0);
440 if (!rc) {
441 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
442 " got false\n");
444 write_unlock_bh(&sk->sk_callback_lock);
447 static void iscsi_target_sk_state_change(struct sock *);
449 static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
451 struct sock *sk;
453 if (!conn->sock)
454 return;
456 sk = conn->sock->sk;
457 pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
459 write_lock_bh(&sk->sk_callback_lock);
460 sk->sk_user_data = conn;
461 conn->orig_data_ready = sk->sk_data_ready;
462 conn->orig_state_change = sk->sk_state_change;
463 sk->sk_data_ready = iscsi_target_sk_data_ready;
464 sk->sk_state_change = iscsi_target_sk_state_change;
465 write_unlock_bh(&sk->sk_callback_lock);
467 sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
468 sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
471 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
473 struct sock *sk;
475 if (!conn->sock)
476 return;
478 sk = conn->sock->sk;
479 pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
481 write_lock_bh(&sk->sk_callback_lock);
482 if (!sk->sk_user_data) {
483 write_unlock_bh(&sk->sk_callback_lock);
484 return;
486 sk->sk_user_data = NULL;
487 sk->sk_data_ready = conn->orig_data_ready;
488 sk->sk_state_change = conn->orig_state_change;
489 write_unlock_bh(&sk->sk_callback_lock);
491 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
492 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
495 static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
497 static bool iscsi_target_sk_state_check(struct sock *sk)
499 if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
500 pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
501 "returning FALSE\n");
502 return false;
504 return true;
507 static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
509 struct iscsi_np *np = login->np;
510 bool zero_tsih = login->zero_tsih;
512 iscsi_remove_failed_auth_entry(conn);
513 iscsi_target_nego_release(conn);
514 iscsi_target_login_sess_out(conn, np, zero_tsih, true);
517 static void iscsi_target_login_timeout(unsigned long data)
519 struct iscsi_conn *conn = (struct iscsi_conn *)data;
521 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
523 if (conn->login_kworker) {
524 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
525 conn->login_kworker->comm, conn->login_kworker->pid);
526 send_sig(SIGINT, conn->login_kworker, 1);
530 static void iscsi_target_do_login_rx(struct work_struct *work)
532 struct iscsi_conn *conn = container_of(work,
533 struct iscsi_conn, login_work.work);
534 struct iscsi_login *login = conn->login;
535 struct iscsi_np *np = login->np;
536 struct iscsi_portal_group *tpg = conn->tpg;
537 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
538 struct timer_list login_timer;
539 int rc, zero_tsih = login->zero_tsih;
540 bool state;
542 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
543 conn, current->comm, current->pid);
545 spin_lock(&tpg->tpg_state_lock);
546 state = (tpg->tpg_state == TPG_STATE_ACTIVE);
547 spin_unlock(&tpg->tpg_state_lock);
549 if (!state) {
550 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
551 iscsi_target_restore_sock_callbacks(conn);
552 iscsi_target_login_drop(conn, login);
553 iscsit_deaccess_np(np, tpg, tpg_np);
554 return;
557 if (conn->sock) {
558 struct sock *sk = conn->sock->sk;
560 read_lock_bh(&sk->sk_callback_lock);
561 state = iscsi_target_sk_state_check(sk);
562 read_unlock_bh(&sk->sk_callback_lock);
564 if (!state) {
565 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
566 iscsi_target_restore_sock_callbacks(conn);
567 iscsi_target_login_drop(conn, login);
568 iscsit_deaccess_np(np, tpg, tpg_np);
569 return;
573 conn->login_kworker = current;
574 allow_signal(SIGINT);
576 init_timer(&login_timer);
577 login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
578 login_timer.data = (unsigned long)conn;
579 login_timer.function = iscsi_target_login_timeout;
580 add_timer(&login_timer);
581 pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
583 rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
584 del_timer_sync(&login_timer);
585 flush_signals(current);
586 conn->login_kworker = NULL;
588 if (rc < 0) {
589 iscsi_target_restore_sock_callbacks(conn);
590 iscsi_target_login_drop(conn, login);
591 iscsit_deaccess_np(np, tpg, tpg_np);
592 return;
595 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
596 conn, current->comm, current->pid);
598 rc = iscsi_target_do_login(conn, login);
599 if (rc < 0) {
600 iscsi_target_restore_sock_callbacks(conn);
601 iscsi_target_login_drop(conn, login);
602 iscsit_deaccess_np(np, tpg, tpg_np);
603 } else if (!rc) {
604 if (conn->sock) {
605 struct sock *sk = conn->sock->sk;
607 write_lock_bh(&sk->sk_callback_lock);
608 clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
609 write_unlock_bh(&sk->sk_callback_lock);
611 } else if (rc == 1) {
612 iscsi_target_nego_release(conn);
613 iscsi_post_login_handler(np, conn, zero_tsih);
614 iscsit_deaccess_np(np, tpg, tpg_np);
618 static void iscsi_target_do_cleanup(struct work_struct *work)
620 struct iscsi_conn *conn = container_of(work,
621 struct iscsi_conn, login_cleanup_work.work);
622 struct sock *sk = conn->sock->sk;
623 struct iscsi_login *login = conn->login;
624 struct iscsi_np *np = login->np;
625 struct iscsi_portal_group *tpg = conn->tpg;
626 struct iscsi_tpg_np *tpg_np = conn->tpg_np;
628 pr_debug("Entering iscsi_target_do_cleanup\n");
630 cancel_delayed_work_sync(&conn->login_work);
631 conn->orig_state_change(sk);
633 iscsi_target_restore_sock_callbacks(conn);
634 iscsi_target_login_drop(conn, login);
635 iscsit_deaccess_np(np, tpg, tpg_np);
637 pr_debug("iscsi_target_do_cleanup done()\n");
640 static void iscsi_target_sk_state_change(struct sock *sk)
642 struct iscsi_conn *conn;
643 void (*orig_state_change)(struct sock *);
644 bool state;
646 pr_debug("Entering iscsi_target_sk_state_change\n");
648 write_lock_bh(&sk->sk_callback_lock);
649 conn = sk->sk_user_data;
650 if (!conn) {
651 write_unlock_bh(&sk->sk_callback_lock);
652 return;
654 orig_state_change = conn->orig_state_change;
656 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
657 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
658 conn);
659 write_unlock_bh(&sk->sk_callback_lock);
660 orig_state_change(sk);
661 return;
663 if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
664 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
665 " conn: %p\n", conn);
666 write_unlock_bh(&sk->sk_callback_lock);
667 orig_state_change(sk);
668 return;
670 if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
671 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
672 conn);
673 write_unlock_bh(&sk->sk_callback_lock);
674 orig_state_change(sk);
675 return;
678 state = iscsi_target_sk_state_check(sk);
679 write_unlock_bh(&sk->sk_callback_lock);
681 pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
683 if (!state) {
684 pr_debug("iscsi_target_sk_state_change got failed state\n");
685 schedule_delayed_work(&conn->login_cleanup_work, 0);
686 return;
688 orig_state_change(sk);
692 * NOTE: We check for existing sessions or connections AFTER the initiator
693 * has been successfully authenticated in order to protect against faked
694 * ISID/TSIH combinations.
696 static int iscsi_target_check_for_existing_instances(
697 struct iscsi_conn *conn,
698 struct iscsi_login *login)
700 if (login->checked_for_existing)
701 return 0;
703 login->checked_for_existing = 1;
705 if (!login->tsih)
706 return iscsi_check_for_session_reinstatement(conn);
707 else
708 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
709 login->initial_exp_statsn);
712 static int iscsi_target_do_authentication(
713 struct iscsi_conn *conn,
714 struct iscsi_login *login)
716 int authret;
717 u32 payload_length;
718 struct iscsi_param *param;
719 struct iscsi_login_req *login_req;
720 struct iscsi_login_rsp *login_rsp;
722 login_req = (struct iscsi_login_req *) login->req;
723 login_rsp = (struct iscsi_login_rsp *) login->rsp;
724 payload_length = ntoh24(login_req->dlength);
726 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
727 if (!param)
728 return -1;
730 authret = iscsi_handle_authentication(
731 conn,
732 login->req_buf,
733 login->rsp_buf,
734 payload_length,
735 &login->rsp_length,
736 param->value);
737 switch (authret) {
738 case 0:
739 pr_debug("Received OK response"
740 " from LIO Authentication, continuing.\n");
741 break;
742 case 1:
743 pr_debug("iSCSI security negotiation"
744 " completed successfully.\n");
745 login->auth_complete = 1;
746 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
747 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
748 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
749 ISCSI_FLAG_LOGIN_TRANSIT);
750 login->current_stage = 1;
752 return iscsi_target_check_for_existing_instances(
753 conn, login);
754 case 2:
755 pr_err("Security negotiation"
756 " failed.\n");
757 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
758 ISCSI_LOGIN_STATUS_AUTH_FAILED);
759 return -1;
760 default:
761 pr_err("Received unknown error %d from LIO"
762 " Authentication\n", authret);
763 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
764 ISCSI_LOGIN_STATUS_TARGET_ERROR);
765 return -1;
768 return 0;
771 static int iscsi_target_handle_csg_zero(
772 struct iscsi_conn *conn,
773 struct iscsi_login *login)
775 int ret;
776 u32 payload_length;
777 struct iscsi_param *param;
778 struct iscsi_login_req *login_req;
779 struct iscsi_login_rsp *login_rsp;
781 login_req = (struct iscsi_login_req *) login->req;
782 login_rsp = (struct iscsi_login_rsp *) login->rsp;
783 payload_length = ntoh24(login_req->dlength);
785 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
786 if (!param)
787 return -1;
789 ret = iscsi_decode_text_input(
790 PHASE_SECURITY|PHASE_DECLARATIVE,
791 SENDER_INITIATOR|SENDER_RECEIVER,
792 login->req_buf,
793 payload_length,
794 conn);
795 if (ret < 0)
796 return -1;
798 if (ret > 0) {
799 if (login->auth_complete) {
800 pr_err("Initiator has already been"
801 " successfully authenticated, but is still"
802 " sending %s keys.\n", param->value);
803 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
804 ISCSI_LOGIN_STATUS_INIT_ERR);
805 return -1;
808 goto do_auth;
809 } else if (!payload_length) {
810 pr_err("Initiator sent zero length security payload,"
811 " login failed\n");
812 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
813 ISCSI_LOGIN_STATUS_AUTH_FAILED);
814 return -1;
817 if (login->first_request)
818 if (iscsi_target_check_first_request(conn, login) < 0)
819 return -1;
821 ret = iscsi_encode_text_output(
822 PHASE_SECURITY|PHASE_DECLARATIVE,
823 SENDER_TARGET,
824 login->rsp_buf,
825 &login->rsp_length,
826 conn->param_list);
827 if (ret < 0)
828 return -1;
830 if (!iscsi_check_negotiated_keys(conn->param_list)) {
831 if (conn->tpg->tpg_attrib.authentication &&
832 !strncmp(param->value, NONE, 4)) {
833 pr_err("Initiator sent AuthMethod=None but"
834 " Target is enforcing iSCSI Authentication,"
835 " login failed.\n");
836 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
837 ISCSI_LOGIN_STATUS_AUTH_FAILED);
838 return -1;
841 if (conn->tpg->tpg_attrib.authentication &&
842 !login->auth_complete)
843 return 0;
845 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
846 return 0;
848 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
849 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
850 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
851 ISCSI_FLAG_LOGIN_TRANSIT;
852 login->current_stage = 1;
856 return 0;
857 do_auth:
858 return iscsi_target_do_authentication(conn, login);
861 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
863 int ret;
864 u32 payload_length;
865 struct iscsi_login_req *login_req;
866 struct iscsi_login_rsp *login_rsp;
868 login_req = (struct iscsi_login_req *) login->req;
869 login_rsp = (struct iscsi_login_rsp *) login->rsp;
870 payload_length = ntoh24(login_req->dlength);
872 ret = iscsi_decode_text_input(
873 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
874 SENDER_INITIATOR|SENDER_RECEIVER,
875 login->req_buf,
876 payload_length,
877 conn);
878 if (ret < 0) {
879 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
880 ISCSI_LOGIN_STATUS_INIT_ERR);
881 return -1;
884 if (login->first_request)
885 if (iscsi_target_check_first_request(conn, login) < 0)
886 return -1;
888 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
889 return -1;
891 ret = iscsi_encode_text_output(
892 PHASE_OPERATIONAL|PHASE_DECLARATIVE,
893 SENDER_TARGET,
894 login->rsp_buf,
895 &login->rsp_length,
896 conn->param_list);
897 if (ret < 0) {
898 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
899 ISCSI_LOGIN_STATUS_INIT_ERR);
900 return -1;
903 if (!login->auth_complete &&
904 conn->tpg->tpg_attrib.authentication) {
905 pr_err("Initiator is requesting CSG: 1, has not been"
906 " successfully authenticated, and the Target is"
907 " enforcing iSCSI Authentication, login failed.\n");
908 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
909 ISCSI_LOGIN_STATUS_AUTH_FAILED);
910 return -1;
913 if (!iscsi_check_negotiated_keys(conn->param_list))
914 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
915 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
916 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
917 ISCSI_FLAG_LOGIN_TRANSIT;
919 return 0;
922 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
924 int pdu_count = 0;
925 struct iscsi_login_req *login_req;
926 struct iscsi_login_rsp *login_rsp;
928 login_req = (struct iscsi_login_req *) login->req;
929 login_rsp = (struct iscsi_login_rsp *) login->rsp;
931 while (1) {
932 if (++pdu_count > MAX_LOGIN_PDUS) {
933 pr_err("MAX_LOGIN_PDUS count reached.\n");
934 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
935 ISCSI_LOGIN_STATUS_TARGET_ERROR);
936 return -1;
939 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
940 case 0:
941 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
942 if (iscsi_target_handle_csg_zero(conn, login) < 0)
943 return -1;
944 break;
945 case 1:
946 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
947 if (iscsi_target_handle_csg_one(conn, login) < 0)
948 return -1;
949 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
950 login->tsih = conn->sess->tsih;
951 login->login_complete = 1;
952 iscsi_target_restore_sock_callbacks(conn);
953 if (iscsi_target_do_tx_login_io(conn,
954 login) < 0)
955 return -1;
956 return 1;
958 break;
959 default:
960 pr_err("Illegal CSG: %d received from"
961 " Initiator, protocol error.\n",
962 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
963 break;
966 if (iscsi_target_do_tx_login_io(conn, login) < 0)
967 return -1;
969 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
970 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
971 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
973 break;
976 if (conn->sock) {
977 struct sock *sk = conn->sock->sk;
978 bool state;
980 read_lock_bh(&sk->sk_callback_lock);
981 state = iscsi_target_sk_state_check(sk);
982 read_unlock_bh(&sk->sk_callback_lock);
984 if (!state) {
985 pr_debug("iscsi_target_do_login() failed state for"
986 " conn: %p\n", conn);
987 return -1;
991 return 0;
994 static void iscsi_initiatorname_tolower(
995 char *param_buf)
997 char *c;
998 u32 iqn_size = strlen(param_buf), i;
1000 for (i = 0; i < iqn_size; i++) {
1001 c = &param_buf[i];
1002 if (!isupper(*c))
1003 continue;
1005 *c = tolower(*c);
1010 * Processes the first Login Request..
1012 int iscsi_target_locate_portal(
1013 struct iscsi_np *np,
1014 struct iscsi_conn *conn,
1015 struct iscsi_login *login)
1017 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1018 char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1019 struct iscsi_session *sess = conn->sess;
1020 struct iscsi_tiqn *tiqn;
1021 struct iscsi_tpg_np *tpg_np = NULL;
1022 struct iscsi_login_req *login_req;
1023 struct se_node_acl *se_nacl;
1024 u32 payload_length, queue_depth = 0;
1025 int sessiontype = 0, ret = 0, tag_num, tag_size;
1027 INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1028 INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
1029 iscsi_target_set_sock_callbacks(conn);
1031 login->np = np;
1033 login_req = (struct iscsi_login_req *) login->req;
1034 payload_length = ntoh24(login_req->dlength);
1036 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1037 if (!tmpbuf) {
1038 pr_err("Unable to allocate memory for tmpbuf.\n");
1039 return -1;
1042 memcpy(tmpbuf, login->req_buf, payload_length);
1043 tmpbuf[payload_length] = '\0';
1044 start = tmpbuf;
1045 end = (start + payload_length);
1048 * Locate the initial keys expected from the Initiator node in
1049 * the first login request in order to progress with the login phase.
1051 while (start < end) {
1052 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1053 ret = -1;
1054 goto out;
1057 if (!strncmp(key, "InitiatorName", 13))
1058 i_buf = value;
1059 else if (!strncmp(key, "SessionType", 11))
1060 s_buf = value;
1061 else if (!strncmp(key, "TargetName", 10))
1062 t_buf = value;
1064 start += strlen(key) + strlen(value) + 2;
1067 * See 5.3. Login Phase.
1069 if (!i_buf) {
1070 pr_err("InitiatorName key not received"
1071 " in first login request.\n");
1072 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1073 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1074 ret = -1;
1075 goto out;
1078 * Convert the incoming InitiatorName to lowercase following
1079 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1080 * are NOT case sensitive.
1082 iscsi_initiatorname_tolower(i_buf);
1084 if (!s_buf) {
1085 if (!login->leading_connection)
1086 goto get_target;
1088 pr_err("SessionType key not received"
1089 " in first login request.\n");
1090 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1091 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1092 ret = -1;
1093 goto out;
1097 * Use default portal group for discovery sessions.
1099 sessiontype = strncmp(s_buf, DISCOVERY, 9);
1100 if (!sessiontype) {
1101 conn->tpg = iscsit_global->discovery_tpg;
1102 if (!login->leading_connection)
1103 goto get_target;
1105 sess->sess_ops->SessionType = 1;
1107 * Setup crc32c modules from libcrypto
1109 if (iscsi_login_setup_crypto(conn) < 0) {
1110 pr_err("iscsi_login_setup_crypto() failed\n");
1111 ret = -1;
1112 goto out;
1115 * Serialize access across the discovery struct iscsi_portal_group to
1116 * process login attempt.
1118 if (iscsit_access_np(np, conn->tpg) < 0) {
1119 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1120 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1121 ret = -1;
1122 goto out;
1124 ret = 0;
1125 goto alloc_tags;
1128 get_target:
1129 if (!t_buf) {
1130 pr_err("TargetName key not received"
1131 " in first login request while"
1132 " SessionType=Normal.\n");
1133 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1134 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1135 ret = -1;
1136 goto out;
1140 * Locate Target IQN from Storage Node.
1142 tiqn = iscsit_get_tiqn_for_login(t_buf);
1143 if (!tiqn) {
1144 pr_err("Unable to locate Target IQN: %s in"
1145 " Storage Node\n", t_buf);
1146 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1147 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1148 ret = -1;
1149 goto out;
1151 pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1154 * Locate Target Portal Group from Storage Node.
1156 conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1157 if (!conn->tpg) {
1158 pr_err("Unable to locate Target Portal Group"
1159 " on %s\n", tiqn->tiqn);
1160 iscsit_put_tiqn_for_login(tiqn);
1161 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1162 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1163 ret = -1;
1164 goto out;
1166 conn->tpg_np = tpg_np;
1167 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1169 * Setup crc32c modules from libcrypto
1171 if (iscsi_login_setup_crypto(conn) < 0) {
1172 pr_err("iscsi_login_setup_crypto() failed\n");
1173 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1174 iscsit_put_tiqn_for_login(tiqn);
1175 conn->tpg = NULL;
1176 ret = -1;
1177 goto out;
1180 * Serialize access across the struct iscsi_portal_group to
1181 * process login attempt.
1183 if (iscsit_access_np(np, conn->tpg) < 0) {
1184 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1185 iscsit_put_tiqn_for_login(tiqn);
1186 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1187 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1188 conn->tpg = NULL;
1189 ret = -1;
1190 goto out;
1194 * conn->sess->node_acl will be set when the referenced
1195 * struct iscsi_session is located from received ISID+TSIH in
1196 * iscsi_login_non_zero_tsih_s2().
1198 if (!login->leading_connection) {
1199 ret = 0;
1200 goto out;
1204 * This value is required in iscsi_login_zero_tsih_s2()
1206 sess->sess_ops->SessionType = 0;
1209 * Locate incoming Initiator IQN reference from Storage Node.
1211 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1212 &conn->tpg->tpg_se_tpg, i_buf);
1213 if (!sess->se_sess->se_node_acl) {
1214 pr_err("iSCSI Initiator Node: %s is not authorized to"
1215 " access iSCSI target portal group: %hu.\n",
1216 i_buf, conn->tpg->tpgt);
1217 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1218 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1219 ret = -1;
1220 goto out;
1222 se_nacl = sess->se_sess->se_node_acl;
1223 queue_depth = se_nacl->queue_depth;
1225 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1226 * depth for non immediate commands, plus extra tags for immediate
1227 * commands.
1229 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1230 * in per-cpu-ida tag allocation logic + small queue_depth.
1232 alloc_tags:
1233 tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
1234 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1235 tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1237 ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1238 if (ret < 0) {
1239 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1240 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1241 ret = -1;
1243 out:
1244 kfree(tmpbuf);
1245 return ret;
1248 int iscsi_target_start_negotiation(
1249 struct iscsi_login *login,
1250 struct iscsi_conn *conn)
1252 int ret;
1254 ret = iscsi_target_do_login(conn, login);
1255 if (!ret) {
1256 if (conn->sock) {
1257 struct sock *sk = conn->sock->sk;
1259 write_lock_bh(&sk->sk_callback_lock);
1260 set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1261 write_unlock_bh(&sk->sk_callback_lock);
1263 } else if (ret < 0) {
1264 cancel_delayed_work_sync(&conn->login_work);
1265 cancel_delayed_work_sync(&conn->login_cleanup_work);
1266 iscsi_target_restore_sock_callbacks(conn);
1267 iscsi_remove_failed_auth_entry(conn);
1269 if (ret != 0)
1270 iscsi_target_nego_release(conn);
1272 return ret;
1275 void iscsi_target_nego_release(struct iscsi_conn *conn)
1277 struct iscsi_login *login = conn->conn_login;
1279 if (!login)
1280 return;
1282 kfree(login->req_buf);
1283 kfree(login->rsp_buf);
1284 kfree(login);
1286 conn->conn_login = NULL;