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>
26 #include "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
38 void convert_null_to_semi(char *buf
, int len
)
42 for (i
= 0; i
< len
; i
++)
47 int strlen_semi(char *buf
)
51 while (buf
[i
] != '\0') {
63 unsigned int max_length
,
70 if (!in_buf
|| !pattern
|| !out_buf
|| !type
)
73 ptr
= strstr(in_buf
, pattern
);
77 ptr
= strstr(ptr
, "=");
82 if (*ptr
== '0' && (*(ptr
+1) == 'x' || *(ptr
+1) == 'X')) {
83 ptr
+= 2; /* skip 0x */
88 len
= strlen_semi(ptr
);
92 if (len
>= max_length
) {
93 pr_err("Length of input: %d exceeds max_length:"
94 " %d\n", len
, max_length
);
97 memcpy(out_buf
, ptr
, len
);
103 static u32
iscsi_handle_authentication(
104 struct iscsi_conn
*conn
,
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 se_node_acl
*se_nacl
;
116 if (!sess
->sess_ops
->SessionType
) {
118 * For SessionType=Normal
120 se_nacl
= conn
->sess
->se_sess
->se_node_acl
;
122 pr_err("Unable to locate struct se_node_acl for"
126 iscsi_nacl
= container_of(se_nacl
, struct iscsi_node_acl
,
129 pr_err("Unable to locate struct iscsi_node_acl for"
134 auth
= ISCSI_NODE_AUTH(iscsi_nacl
);
137 * For SessionType=Discovery
139 auth
= &iscsit_global
->discovery_acl
.node_auth
;
142 if (strstr("CHAP", authtype
))
143 strcpy(conn
->sess
->auth_type
, "CHAP");
145 strcpy(conn
->sess
->auth_type
, NONE
);
147 if (strstr("None", authtype
))
150 else if (strstr("SRP", authtype
))
151 return srp_main_loop(conn
, auth
, in_buf
, out_buf
,
152 &in_length
, out_length
);
154 else if (strstr("CHAP", authtype
))
155 return chap_main_loop(conn
, auth
, in_buf
, out_buf
,
156 &in_length
, out_length
);
157 else if (strstr("SPKM1", authtype
))
159 else if (strstr("SPKM2", authtype
))
161 else if (strstr("KRB5", authtype
))
167 static void iscsi_remove_failed_auth_entry(struct iscsi_conn
*conn
)
169 kfree(conn
->auth_protocol
);
172 static int iscsi_target_check_login_request(
173 struct iscsi_conn
*conn
,
174 struct iscsi_login
*login
)
176 int req_csg
, req_nsg
;
178 struct iscsi_login_req
*login_req
;
180 login_req
= (struct iscsi_login_req
*) login
->req
;
181 payload_length
= ntoh24(login_req
->dlength
);
183 switch (login_req
->opcode
& ISCSI_OPCODE_MASK
) {
187 pr_err("Received unknown opcode 0x%02x.\n",
188 login_req
->opcode
& ISCSI_OPCODE_MASK
);
189 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
190 ISCSI_LOGIN_STATUS_INIT_ERR
);
194 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_CONTINUE
) &&
195 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
)) {
196 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
197 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
198 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
199 ISCSI_LOGIN_STATUS_INIT_ERR
);
203 req_csg
= (login_req
->flags
& ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK
) >> 2;
204 req_nsg
= (login_req
->flags
& ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK
);
206 if (req_csg
!= login
->current_stage
) {
207 pr_err("Initiator unexpectedly changed login stage"
208 " from %d to %d, login failed.\n", login
->current_stage
,
210 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
211 ISCSI_LOGIN_STATUS_INIT_ERR
);
215 if ((req_nsg
== 2) || (req_csg
>= 2) ||
216 ((login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
) &&
217 (req_nsg
<= req_csg
))) {
218 pr_err("Illegal login_req->flags Combination, CSG: %d,"
219 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg
,
220 req_nsg
, (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
));
221 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
222 ISCSI_LOGIN_STATUS_INIT_ERR
);
226 if ((login_req
->max_version
!= login
->version_max
) ||
227 (login_req
->min_version
!= login
->version_min
)) {
228 pr_err("Login request changed Version Max/Nin"
229 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
230 login_req
->max_version
, login_req
->min_version
);
231 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
232 ISCSI_LOGIN_STATUS_INIT_ERR
);
236 if (memcmp(login_req
->isid
, login
->isid
, 6) != 0) {
237 pr_err("Login request changed ISID unexpectedly,"
238 " protocol error.\n");
239 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
240 ISCSI_LOGIN_STATUS_INIT_ERR
);
244 if (login_req
->itt
!= login
->init_task_tag
) {
245 pr_err("Login request changed ITT unexpectedly to"
246 " 0x%08x, protocol error.\n", login_req
->itt
);
247 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
248 ISCSI_LOGIN_STATUS_INIT_ERR
);
252 if (payload_length
> MAX_KEY_VALUE_PAIRS
) {
253 pr_err("Login request payload exceeds default"
254 " MaxRecvDataSegmentLength: %u, protocol error.\n",
255 MAX_KEY_VALUE_PAIRS
);
262 static int iscsi_target_check_first_request(
263 struct iscsi_conn
*conn
,
264 struct iscsi_login
*login
)
266 struct iscsi_param
*param
= NULL
;
267 struct se_node_acl
*se_nacl
;
269 login
->first_request
= 0;
271 list_for_each_entry(param
, &conn
->param_list
->param_list
, p_list
) {
272 if (!strncmp(param
->name
, SESSIONTYPE
, 11)) {
273 if (!IS_PSTATE_ACCEPTOR(param
)) {
274 pr_err("SessionType key not received"
275 " in first login request.\n");
276 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
277 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
280 if (!strncmp(param
->value
, DISCOVERY
, 9))
284 if (!strncmp(param
->name
, INITIATORNAME
, 13)) {
285 if (!IS_PSTATE_ACCEPTOR(param
)) {
286 if (!login
->leading_connection
)
289 pr_err("InitiatorName key not received"
290 " in first login request.\n");
291 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
292 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
297 * For non-leading connections, double check that the
298 * received InitiatorName matches the existing session's
299 * struct iscsi_node_acl.
301 if (!login
->leading_connection
) {
302 se_nacl
= conn
->sess
->se_sess
->se_node_acl
;
304 pr_err("Unable to locate"
305 " struct se_node_acl\n");
306 iscsit_tx_login_rsp(conn
,
307 ISCSI_STATUS_CLS_INITIATOR_ERR
,
308 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND
);
312 if (strcmp(param
->value
,
313 se_nacl
->initiatorname
)) {
315 " InitiatorName: %s for this"
316 " iSCSI Initiator Node.\n",
318 iscsit_tx_login_rsp(conn
,
319 ISCSI_STATUS_CLS_INITIATOR_ERR
,
320 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND
);
330 static int iscsi_target_do_tx_login_io(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
333 struct iscsi_session
*sess
= conn
->sess
;
334 struct iscsi_login_rsp
*login_rsp
;
336 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
338 login_rsp
->opcode
= ISCSI_OP_LOGIN_RSP
;
339 hton24(login_rsp
->dlength
, login
->rsp_length
);
340 memcpy(login_rsp
->isid
, login
->isid
, 6);
341 login_rsp
->tsih
= cpu_to_be16(login
->tsih
);
342 login_rsp
->itt
= cpu_to_be32(login
->init_task_tag
);
343 login_rsp
->statsn
= cpu_to_be32(conn
->stat_sn
++);
344 login_rsp
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
345 login_rsp
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
347 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
348 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
349 " %u\n", login_rsp
->flags
, ntohl(login_rsp
->itt
),
350 ntohl(login_rsp
->exp_cmdsn
), ntohl(login_rsp
->max_cmdsn
),
351 ntohl(login_rsp
->statsn
), login
->rsp_length
);
353 padding
= ((-login
->rsp_length
) & 3);
355 if (iscsi_login_tx_data(
359 login
->rsp_length
+ padding
) < 0)
362 login
->rsp_length
= 0;
363 login_rsp
->tsih
= be16_to_cpu(login_rsp
->tsih
);
364 login_rsp
->itt
= be32_to_cpu(login_rsp
->itt
);
365 login_rsp
->statsn
= be32_to_cpu(login_rsp
->statsn
);
366 mutex_lock(&sess
->cmdsn_mutex
);
367 login_rsp
->exp_cmdsn
= be32_to_cpu(sess
->exp_cmd_sn
);
368 login_rsp
->max_cmdsn
= be32_to_cpu(sess
->max_cmd_sn
);
369 mutex_unlock(&sess
->cmdsn_mutex
);
374 static int iscsi_target_do_rx_login_io(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
376 u32 padding
= 0, payload_length
;
377 struct iscsi_login_req
*login_req
;
379 if (iscsi_login_rx_data(conn
, login
->req
, ISCSI_HDR_LEN
) < 0)
382 login_req
= (struct iscsi_login_req
*) login
->req
;
383 payload_length
= ntoh24(login_req
->dlength
);
384 login_req
->tsih
= be16_to_cpu(login_req
->tsih
);
385 login_req
->itt
= be32_to_cpu(login_req
->itt
);
386 login_req
->cid
= be16_to_cpu(login_req
->cid
);
387 login_req
->cmdsn
= be32_to_cpu(login_req
->cmdsn
);
388 login_req
->exp_statsn
= be32_to_cpu(login_req
->exp_statsn
);
390 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
391 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
392 login_req
->flags
, login_req
->itt
, login_req
->cmdsn
,
393 login_req
->exp_statsn
, login_req
->cid
, payload_length
);
395 if (iscsi_target_check_login_request(conn
, login
) < 0)
398 padding
= ((-payload_length
) & 3);
399 memset(login
->req_buf
, 0, MAX_KEY_VALUE_PAIRS
);
401 if (iscsi_login_rx_data(
404 payload_length
+ padding
) < 0)
410 static int iscsi_target_do_login_io(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
412 if (iscsi_target_do_tx_login_io(conn
, login
) < 0)
415 if (iscsi_target_do_rx_login_io(conn
, login
) < 0)
421 static int iscsi_target_get_initial_payload(
422 struct iscsi_conn
*conn
,
423 struct iscsi_login
*login
)
425 u32 padding
= 0, payload_length
;
426 struct iscsi_login_req
*login_req
;
428 login_req
= (struct iscsi_login_req
*) login
->req
;
429 payload_length
= ntoh24(login_req
->dlength
);
431 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
432 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
433 login_req
->flags
, login_req
->itt
, login_req
->cmdsn
,
434 login_req
->exp_statsn
, payload_length
);
436 if (iscsi_target_check_login_request(conn
, login
) < 0)
439 padding
= ((-payload_length
) & 3);
441 if (iscsi_login_rx_data(
444 payload_length
+ padding
) < 0)
451 * NOTE: We check for existing sessions or connections AFTER the initiator
452 * has been successfully authenticated in order to protect against faked
453 * ISID/TSIH combinations.
455 static int iscsi_target_check_for_existing_instances(
456 struct iscsi_conn
*conn
,
457 struct iscsi_login
*login
)
459 if (login
->checked_for_existing
)
462 login
->checked_for_existing
= 1;
465 return iscsi_check_for_session_reinstatement(conn
);
467 return iscsi_login_post_auth_non_zero_tsih(conn
, login
->cid
,
468 login
->initial_exp_statsn
);
471 static int iscsi_target_do_authentication(
472 struct iscsi_conn
*conn
,
473 struct iscsi_login
*login
)
477 struct iscsi_param
*param
;
478 struct iscsi_login_req
*login_req
;
479 struct iscsi_login_rsp
*login_rsp
;
481 login_req
= (struct iscsi_login_req
*) login
->req
;
482 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
483 payload_length
= ntoh24(login_req
->dlength
);
485 param
= iscsi_find_param_from_key(AUTHMETHOD
, conn
->param_list
);
489 authret
= iscsi_handle_authentication(
498 pr_debug("Received OK response"
499 " from LIO Authentication, continuing.\n");
502 pr_debug("iSCSI security negotiation"
503 " completed successfully.\n");
504 login
->auth_complete
= 1;
505 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_NEXT_STAGE1
) &&
506 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
)) {
507 login_rsp
->flags
|= (ISCSI_FLAG_LOGIN_NEXT_STAGE1
|
508 ISCSI_FLAG_LOGIN_TRANSIT
);
509 login
->current_stage
= 1;
511 return iscsi_target_check_for_existing_instances(
514 pr_err("Security negotiation"
516 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
517 ISCSI_LOGIN_STATUS_AUTH_FAILED
);
520 pr_err("Received unknown error %d from LIO"
521 " Authentication\n", authret
);
522 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
523 ISCSI_LOGIN_STATUS_TARGET_ERROR
);
530 static int iscsi_target_handle_csg_zero(
531 struct iscsi_conn
*conn
,
532 struct iscsi_login
*login
)
536 struct iscsi_param
*param
;
537 struct iscsi_login_req
*login_req
;
538 struct iscsi_login_rsp
*login_rsp
;
540 login_req
= (struct iscsi_login_req
*) login
->req
;
541 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
542 payload_length
= ntoh24(login_req
->dlength
);
544 param
= iscsi_find_param_from_key(AUTHMETHOD
, conn
->param_list
);
548 ret
= iscsi_decode_text_input(
549 PHASE_SECURITY
|PHASE_DECLARATIVE
,
550 SENDER_INITIATOR
|SENDER_RECEIVER
,
558 if (login
->auth_complete
) {
559 pr_err("Initiator has already been"
560 " successfully authenticated, but is still"
561 " sending %s keys.\n", param
->value
);
562 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
563 ISCSI_LOGIN_STATUS_INIT_ERR
);
570 if (login
->first_request
)
571 if (iscsi_target_check_first_request(conn
, login
) < 0)
574 ret
= iscsi_encode_text_output(
575 PHASE_SECURITY
|PHASE_DECLARATIVE
,
583 if (!iscsi_check_negotiated_keys(conn
->param_list
)) {
584 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn
))->authentication
&&
585 !strncmp(param
->value
, NONE
, 4)) {
586 pr_err("Initiator sent AuthMethod=None but"
587 " Target is enforcing iSCSI Authentication,"
589 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
590 ISCSI_LOGIN_STATUS_AUTH_FAILED
);
594 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn
))->authentication
&&
595 !login
->auth_complete
)
598 if (strncmp(param
->value
, NONE
, 4) && !login
->auth_complete
)
601 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_NEXT_STAGE1
) &&
602 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
)) {
603 login_rsp
->flags
|= ISCSI_FLAG_LOGIN_NEXT_STAGE1
|
604 ISCSI_FLAG_LOGIN_TRANSIT
;
605 login
->current_stage
= 1;
611 return iscsi_target_do_authentication(conn
, login
);
614 static int iscsi_target_handle_csg_one(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
618 struct iscsi_login_req
*login_req
;
619 struct iscsi_login_rsp
*login_rsp
;
621 login_req
= (struct iscsi_login_req
*) login
->req
;
622 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
623 payload_length
= ntoh24(login_req
->dlength
);
625 ret
= iscsi_decode_text_input(
626 PHASE_OPERATIONAL
|PHASE_DECLARATIVE
,
627 SENDER_INITIATOR
|SENDER_RECEIVER
,
632 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
633 ISCSI_LOGIN_STATUS_INIT_ERR
);
637 if (login
->first_request
)
638 if (iscsi_target_check_first_request(conn
, login
) < 0)
641 if (iscsi_target_check_for_existing_instances(conn
, login
) < 0)
644 ret
= iscsi_encode_text_output(
645 PHASE_OPERATIONAL
|PHASE_DECLARATIVE
,
651 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
652 ISCSI_LOGIN_STATUS_INIT_ERR
);
656 if (!login
->auth_complete
&&
657 ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn
))->authentication
) {
658 pr_err("Initiator is requesting CSG: 1, has not been"
659 " successfully authenticated, and the Target is"
660 " enforcing iSCSI Authentication, login failed.\n");
661 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
662 ISCSI_LOGIN_STATUS_AUTH_FAILED
);
666 if (!iscsi_check_negotiated_keys(conn
->param_list
))
667 if ((login_req
->flags
& ISCSI_FLAG_LOGIN_NEXT_STAGE3
) &&
668 (login_req
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
))
669 login_rsp
->flags
|= ISCSI_FLAG_LOGIN_NEXT_STAGE3
|
670 ISCSI_FLAG_LOGIN_TRANSIT
;
675 static int iscsi_target_do_login(struct iscsi_conn
*conn
, struct iscsi_login
*login
)
678 struct iscsi_login_req
*login_req
;
679 struct iscsi_login_rsp
*login_rsp
;
681 login_req
= (struct iscsi_login_req
*) login
->req
;
682 login_rsp
= (struct iscsi_login_rsp
*) login
->rsp
;
685 if (++pdu_count
> MAX_LOGIN_PDUS
) {
686 pr_err("MAX_LOGIN_PDUS count reached.\n");
687 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
688 ISCSI_LOGIN_STATUS_TARGET_ERROR
);
692 switch ((login_req
->flags
& ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK
) >> 2) {
694 login_rsp
->flags
|= (0 & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK
);
695 if (iscsi_target_handle_csg_zero(conn
, login
) < 0)
699 login_rsp
->flags
|= ISCSI_FLAG_LOGIN_CURRENT_STAGE1
;
700 if (iscsi_target_handle_csg_one(conn
, login
) < 0)
702 if (login_rsp
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
) {
703 login
->tsih
= conn
->sess
->tsih
;
704 if (iscsi_target_do_tx_login_io(conn
,
711 pr_err("Illegal CSG: %d received from"
712 " Initiator, protocol error.\n",
713 (login_req
->flags
& ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK
)
718 if (iscsi_target_do_login_io(conn
, login
) < 0)
721 if (login_rsp
->flags
& ISCSI_FLAG_LOGIN_TRANSIT
) {
722 login_rsp
->flags
&= ~ISCSI_FLAG_LOGIN_TRANSIT
;
723 login_rsp
->flags
&= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK
;
730 static void iscsi_initiatorname_tolower(
734 u32 iqn_size
= strlen(param_buf
), i
;
736 for (i
= 0; i
< iqn_size
; i
++) {
746 * Processes the first Login Request..
748 static int iscsi_target_locate_portal(
750 struct iscsi_conn
*conn
,
751 struct iscsi_login
*login
)
753 char *i_buf
= NULL
, *s_buf
= NULL
, *t_buf
= NULL
;
754 char *tmpbuf
, *start
= NULL
, *end
= NULL
, *key
, *value
;
755 struct iscsi_session
*sess
= conn
->sess
;
756 struct iscsi_tiqn
*tiqn
;
757 struct iscsi_login_req
*login_req
;
759 int sessiontype
= 0, ret
= 0;
761 login_req
= (struct iscsi_login_req
*) login
->req
;
762 payload_length
= ntoh24(login_req
->dlength
);
764 login
->first_request
= 1;
765 login
->leading_connection
= (!login_req
->tsih
) ? 1 : 0;
766 login
->current_stage
=
767 (login_req
->flags
& ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK
) >> 2;
768 login
->version_min
= login_req
->min_version
;
769 login
->version_max
= login_req
->max_version
;
770 memcpy(login
->isid
, login_req
->isid
, 6);
771 login
->cmd_sn
= login_req
->cmdsn
;
772 login
->init_task_tag
= login_req
->itt
;
773 login
->initial_exp_statsn
= login_req
->exp_statsn
;
774 login
->cid
= login_req
->cid
;
775 login
->tsih
= login_req
->tsih
;
777 if (iscsi_target_get_initial_payload(conn
, login
) < 0)
780 tmpbuf
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
782 pr_err("Unable to allocate memory for tmpbuf.\n");
786 memcpy(tmpbuf
, login
->req_buf
, payload_length
);
787 tmpbuf
[payload_length
] = '\0';
789 end
= (start
+ payload_length
);
792 * Locate the initial keys expected from the Initiator node in
793 * the first login request in order to progress with the login phase.
795 while (start
< end
) {
796 if (iscsi_extract_key_value(start
, &key
, &value
) < 0) {
801 if (!strncmp(key
, "InitiatorName", 13))
803 else if (!strncmp(key
, "SessionType", 11))
805 else if (!strncmp(key
, "TargetName", 10))
808 start
+= strlen(key
) + strlen(value
) + 2;
812 * See 5.3. Login Phase.
815 pr_err("InitiatorName key not received"
816 " in first login request.\n");
817 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
818 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
823 * Convert the incoming InitiatorName to lowercase following
824 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
825 * are NOT case sensitive.
827 iscsi_initiatorname_tolower(i_buf
);
830 if (!login
->leading_connection
)
833 pr_err("SessionType key not received"
834 " in first login request.\n");
835 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
836 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
842 * Use default portal group for discovery sessions.
844 sessiontype
= strncmp(s_buf
, DISCOVERY
, 9);
846 conn
->tpg
= iscsit_global
->discovery_tpg
;
847 if (!login
->leading_connection
)
850 sess
->sess_ops
->SessionType
= 1;
852 * Setup crc32c modules from libcrypto
854 if (iscsi_login_setup_crypto(conn
) < 0) {
855 pr_err("iscsi_login_setup_crypto() failed\n");
860 * Serialize access across the discovery struct iscsi_portal_group to
861 * process login attempt.
863 if (iscsit_access_np(np
, conn
->tpg
) < 0) {
864 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
865 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
875 pr_err("TargetName key not received"
876 " in first login request while"
877 " SessionType=Normal.\n");
878 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
879 ISCSI_LOGIN_STATUS_MISSING_FIELDS
);
885 * Locate Target IQN from Storage Node.
887 tiqn
= iscsit_get_tiqn_for_login(t_buf
);
889 pr_err("Unable to locate Target IQN: %s in"
890 " Storage Node\n", t_buf
);
891 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
892 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
896 pr_debug("Located Storage Object: %s\n", tiqn
->tiqn
);
899 * Locate Target Portal Group from Storage Node.
901 conn
->tpg
= iscsit_get_tpg_from_np(tiqn
, np
);
903 pr_err("Unable to locate Target Portal Group"
904 " on %s\n", tiqn
->tiqn
);
905 iscsit_put_tiqn_for_login(tiqn
);
906 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
907 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
911 pr_debug("Located Portal Group Object: %hu\n", conn
->tpg
->tpgt
);
913 * Setup crc32c modules from libcrypto
915 if (iscsi_login_setup_crypto(conn
) < 0) {
916 pr_err("iscsi_login_setup_crypto() failed\n");
921 * Serialize access across the struct iscsi_portal_group to
922 * process login attempt.
924 if (iscsit_access_np(np
, conn
->tpg
) < 0) {
925 iscsit_put_tiqn_for_login(tiqn
);
926 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
927 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
934 * conn->sess->node_acl will be set when the referenced
935 * struct iscsi_session is located from received ISID+TSIH in
936 * iscsi_login_non_zero_tsih_s2().
938 if (!login
->leading_connection
) {
944 * This value is required in iscsi_login_zero_tsih_s2()
946 sess
->sess_ops
->SessionType
= 0;
949 * Locate incoming Initiator IQN reference from Storage Node.
951 sess
->se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
952 &conn
->tpg
->tpg_se_tpg
, i_buf
);
953 if (!sess
->se_sess
->se_node_acl
) {
954 pr_err("iSCSI Initiator Node: %s is not authorized to"
955 " access iSCSI target portal group: %hu.\n",
956 i_buf
, conn
->tpg
->tpgt
);
957 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
958 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN
);
969 struct iscsi_login
*iscsi_target_init_negotiation(
971 struct iscsi_conn
*conn
,
974 struct iscsi_login
*login
;
976 login
= kzalloc(sizeof(struct iscsi_login
), GFP_KERNEL
);
978 pr_err("Unable to allocate memory for struct iscsi_login.\n");
979 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
980 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
984 login
->req
= kmemdup(login_pdu
, ISCSI_HDR_LEN
, GFP_KERNEL
);
986 pr_err("Unable to allocate memory for Login Request.\n");
987 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
988 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
992 login
->req_buf
= kzalloc(MAX_KEY_VALUE_PAIRS
, GFP_KERNEL
);
993 if (!login
->req_buf
) {
994 pr_err("Unable to allocate memory for response buffer.\n");
995 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
996 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
1000 * SessionType: Discovery
1002 * Locates Default Portal
1004 * SessionType: Normal
1006 * Locates Target Portal from NP -> Target IQN
1008 if (iscsi_target_locate_portal(np
, conn
, login
) < 0) {
1009 pr_err("iSCSI Login negotiation failed.\n");
1016 kfree(login
->req_buf
);
1022 int iscsi_target_start_negotiation(
1023 struct iscsi_login
*login
,
1024 struct iscsi_conn
*conn
)
1028 login
->rsp
= kzalloc(ISCSI_HDR_LEN
, GFP_KERNEL
);
1030 pr_err("Unable to allocate memory for"
1031 " Login Response.\n");
1032 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1033 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
1038 login
->rsp_buf
= kzalloc(MAX_KEY_VALUE_PAIRS
, GFP_KERNEL
);
1039 if (!login
->rsp_buf
) {
1040 pr_err("Unable to allocate memory for"
1041 " request buffer.\n");
1042 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1043 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
1048 ret
= iscsi_target_do_login(conn
, login
);
1051 iscsi_remove_failed_auth_entry(conn
);
1053 iscsi_target_nego_release(login
, conn
);
1057 void iscsi_target_nego_release(
1058 struct iscsi_login
*login
,
1059 struct iscsi_conn
*conn
)
1063 kfree(login
->req_buf
);
1064 kfree(login
->rsp_buf
);