21 #include <linux/ctype.h>
35 #define MAX_LOGIN_PDUS 7
42 for (i = 0; i < len; i++)
47 static 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);
82 if (*ptr ==
'0' && (*(ptr+1) ==
'x' || *(ptr+1) ==
'X')) {
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);
103 static u32 iscsi_handle_authentication(
109 unsigned char *authtype)
120 se_nacl = conn->
sess->se_sess->se_node_acl;
122 pr_err(
"Unable to locate struct se_node_acl for"
129 pr_err(
"Unable to locate struct iscsi_node_acl for"
142 if (
strstr(
"CHAP", authtype))
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))
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)
172 static int iscsi_target_check_login_request(
176 int req_csg, req_nsg;
187 pr_err(
"Received unknown opcode 0x%02x.\n",
196 pr_err(
"Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
197 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
207 pr_err(
"Initiator unexpectedly changed login stage"
215 if ((req_nsg == 2) || (req_csg >= 2) ||
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,
228 pr_err(
"Login request changed Version Max/Nin"
229 " unexpectedly to 0x%02x/0x%02x, protocol error\n",
237 pr_err(
"Login request changed ISID unexpectedly,"
238 " protocol error.\n");
245 pr_err(
"Login request changed ITT unexpectedly to"
246 " 0x%08x, protocol error.\n", login_req->
itt);
253 pr_err(
"Login request payload exceeds default"
254 " MaxRecvDataSegmentLength: %u, protocol error.\n",
262 static int iscsi_target_check_first_request(
274 pr_err(
"SessionType key not received"
275 " in first login request.\n");
289 pr_err(
"InitiatorName key not received"
290 " in first login request.\n");
302 se_nacl = conn->
sess->se_sess->se_node_acl;
305 " struct se_node_acl\n");
315 " InitiatorName: %s for this"
316 " iSCSI Initiator Node.\n",
347 pr_debug(
"Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
348 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
382 pr_debug(
"Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
383 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
387 if (iscsi_target_check_login_request(conn, login) < 0)
396 payload_length + padding) < 0)
404 if (iscsi_target_do_tx_login_io(conn, login) < 0)
407 if (iscsi_target_do_rx_login_io(conn, login) < 0)
413 static int iscsi_target_get_initial_payload(
423 pr_debug(
"Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
424 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
428 if (iscsi_target_check_login_request(conn, login) < 0)
436 payload_length + padding) < 0)
447 static int iscsi_target_check_for_existing_instances(
463 static int iscsi_target_do_authentication(
481 authret = iscsi_handle_authentication(
491 " from LIO Authentication, continuing.\n");
494 pr_debug(
"iSCSI security negotiation"
495 " completed successfully.\n");
503 return iscsi_target_check_for_existing_instances(
506 pr_err(
"Security negotiation"
512 pr_err(
"Received unknown error %d from LIO"
513 " Authentication\n", authret);
522 static int iscsi_target_handle_csg_zero(
551 pr_err(
"Initiator has already been"
552 " successfully authenticated, but is still"
553 " sending %s keys.\n", param->
value);
563 if (iscsi_target_check_first_request(conn, login) < 0)
578 pr_err(
"Initiator sent AuthMethod=None but"
579 " Target is enforcing iSCSI Authentication,"
603 return iscsi_target_do_authentication(conn, login);
627 if (iscsi_target_check_first_request(conn, login) < 0)
630 if (iscsi_target_check_for_existing_instances(conn, login) < 0)
644 pr_err(
"Initiator is requesting CSG: 1, has not been"
645 " successfully authenticated, and the Target is"
646 " enforcing iSCSI Authentication, login failed.\n");
672 pr_err(
"MAX_LOGIN_PDUS count reached.\n");
681 if (iscsi_target_handle_csg_zero(conn, login) < 0)
686 if (iscsi_target_handle_csg_one(conn, login) < 0)
690 if (iscsi_target_do_tx_login_io(conn,
697 pr_err(
"Illegal CSG: %d received from"
698 " Initiator, protocol error.\n",
704 if (iscsi_target_do_login_io(conn, login) < 0)
716 static void iscsi_initiatorname_tolower(
720 u32 iqn_size =
strlen(param_buf),
i;
722 for (i = 0; i < iqn_size; i++) {
734 static int iscsi_target_locate_portal(
745 int sessiontype = 0, ret = 0;
763 if (iscsi_target_get_initial_payload(conn, login) < 0)
766 tmpbuf = kzalloc(payload_length + 1,
GFP_KERNEL);
768 pr_err(
"Unable to allocate memory for tmpbuf.\n");
781 while (start < end) {
787 if (!
strncmp(key,
"InitiatorName", 13))
789 else if (!
strncmp(key,
"SessionType", 11))
791 else if (!
strncmp(key,
"TargetName", 10))
801 pr_err(
"InitiatorName key not received"
802 " in first login request.\n");
813 iscsi_initiatorname_tolower(i_buf);
819 pr_err(
"SessionType key not received"
820 " in first login request.\n");
841 pr_err(
"iscsi_login_setup_crypto() failed\n");
861 pr_err(
"TargetName key not received"
862 " in first login request while"
863 " SessionType=Normal.\n");
875 pr_err(
"Unable to locate Target IQN: %s in"
876 " Storage Node\n", t_buf);
889 pr_err(
"Unable to locate Target Portal Group"
890 " on %s\n", tiqn->
tiqn);
897 pr_debug(
"Located Portal Group Object: %hu\n", conn->
tpg->tpgt);
902 pr_err(
"iscsi_login_setup_crypto() failed\n");
938 &conn->
tpg->tpg_se_tpg, i_buf);
939 if (!sess->
se_sess->se_node_acl) {
940 pr_err(
"iSCSI Initiator Node: %s is not authorized to"
941 " access iSCSI target portal group: %hu.\n",
942 i_buf, conn->
tpg->tpgt);
964 pr_err(
"Unable to allocate memory for struct iscsi_login.\n");
972 pr_err(
"Unable to allocate memory for Login Request.\n");
980 pr_err(
"Unable to allocate memory for response buffer.\n");
994 if (iscsi_target_locate_portal(np, conn, login) < 0) {
1015 pr_err(
"Unable to allocate memory for"
1016 " Login Response.\n");
1025 pr_err(
"Unable to allocate memory for"
1026 " request buffer.\n");
1033 ret = iscsi_target_do_login(conn, login);
1036 iscsi_remove_failed_auth_entry(conn);