iscsi-target: Fix use-after-free during TPG session shutdown
[pandora-kernel.git] / drivers / target / iscsi / iscsi_target_nego.c
1 /*******************************************************************************
2  * This file contains main functions related to iSCSI Parameter negotiation.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
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.
14  *
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  ******************************************************************************/
20
21 #include <linux/ctype.h>
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_tpg.h>
25
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"
34
35 #define MAX_LOGIN_PDUS  7
36 #define TEXT_LEN        4096
37
38 void convert_null_to_semi(char *buf, int len)
39 {
40         int i;
41
42         for (i = 0; i < len; i++)
43                 if (buf[i] == '\0')
44                         buf[i] = ';';
45 }
46
47 int strlen_semi(char *buf)
48 {
49         int i = 0;
50
51         while (buf[i] != '\0') {
52                 if (buf[i] == ';')
53                         return i;
54                 i++;
55         }
56
57         return -1;
58 }
59
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)
66 {
67         char *ptr;
68         int len;
69
70         if (!in_buf || !pattern || !out_buf || !type)
71                 return -1;
72
73         ptr = strstr(in_buf, pattern);
74         if (!ptr)
75                 return -1;
76
77         ptr = strstr(ptr, "=");
78         if (!ptr)
79                 return -1;
80
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;
87
88         len = strlen_semi(ptr);
89         if (len < 0)
90                 return -1;
91
92         if (len >= max_length) {
93                 pr_err("Length of input: %d exeeds max_length:"
94                         " %d\n", len, max_length);
95                 return -1;
96         }
97         memcpy(out_buf, ptr, len);
98         out_buf[len] = '\0';
99
100         return 0;
101 }
102
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)
110 {
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;
115
116         if (!sess->sess_ops->SessionType) {
117                 /*
118                  * For SessionType=Normal
119                  */
120                 se_nacl = conn->sess->se_sess->se_node_acl;
121                 if (!se_nacl) {
122                         pr_err("Unable to locate struct se_node_acl for"
123                                         " CHAP auth\n");
124                         return -1;
125                 }
126                 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
127                                 se_node_acl);
128                 if (!iscsi_nacl) {
129                         pr_err("Unable to locate struct iscsi_node_acl for"
130                                         " CHAP auth\n");
131                         return -1;
132                 }
133
134                 auth = ISCSI_NODE_AUTH(iscsi_nacl);
135         } else {
136                 /*
137                  * For SessionType=Discovery
138                  */
139                 auth = &iscsit_global->discovery_acl.node_auth;
140         }
141
142         if (strstr("CHAP", authtype))
143                 strcpy(conn->sess->auth_type, "CHAP");
144         else
145                 strcpy(conn->sess->auth_type, NONE);
146
147         if (strstr("None", authtype))
148                 return 1;
149 #ifdef CANSRP
150         else if (strstr("SRP", authtype))
151                 return srp_main_loop(conn, auth, in_buf, out_buf,
152                                 &in_length, out_length);
153 #endif
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))
158                 return 2;
159         else if (strstr("SPKM2", authtype))
160                 return 2;
161         else if (strstr("KRB5", authtype))
162                 return 2;
163         else
164                 return 2;
165 }
166
167 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
168 {
169         kfree(conn->auth_protocol);
170 }
171
172 static int iscsi_target_check_login_request(
173         struct iscsi_conn *conn,
174         struct iscsi_login *login)
175 {
176         int req_csg, req_nsg, rsp_csg, rsp_nsg;
177         u32 payload_length;
178         struct iscsi_login_req *login_req;
179         struct iscsi_login_rsp *login_rsp;
180
181         login_req = (struct iscsi_login_req *) login->req;
182         login_rsp = (struct iscsi_login_rsp *) login->rsp;
183         payload_length = ntoh24(login_req->dlength);
184
185         switch (login_req->opcode & ISCSI_OPCODE_MASK) {
186         case ISCSI_OP_LOGIN:
187                 break;
188         default:
189                 pr_err("Received unknown opcode 0x%02x.\n",
190                                 login_req->opcode & ISCSI_OPCODE_MASK);
191                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
192                                 ISCSI_LOGIN_STATUS_INIT_ERR);
193                 return -1;
194         }
195
196         if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
197             (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
198                 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
199                         " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
200                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
201                                 ISCSI_LOGIN_STATUS_INIT_ERR);
202                 return -1;
203         }
204
205         req_csg = (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
206         rsp_csg = (login_rsp->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
207         req_nsg = (login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK);
208         rsp_nsg = (login_rsp->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK);
209
210         if (req_csg != login->current_stage) {
211                 pr_err("Initiator unexpectedly changed login stage"
212                         " from %d to %d, login failed.\n", login->current_stage,
213                         req_csg);
214                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
215                                 ISCSI_LOGIN_STATUS_INIT_ERR);
216                 return -1;
217         }
218
219         if ((req_nsg == 2) || (req_csg >= 2) ||
220            ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
221             (req_nsg <= req_csg))) {
222                 pr_err("Illegal login_req->flags Combination, CSG: %d,"
223                         " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
224                         req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
225                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
226                                 ISCSI_LOGIN_STATUS_INIT_ERR);
227                 return -1;
228         }
229
230         if ((login_req->max_version != login->version_max) ||
231             (login_req->min_version != login->version_min)) {
232                 pr_err("Login request changed Version Max/Nin"
233                         " unexpectedly to 0x%02x/0x%02x, protocol error\n",
234                         login_req->max_version, login_req->min_version);
235                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
236                                 ISCSI_LOGIN_STATUS_INIT_ERR);
237                 return -1;
238         }
239
240         if (memcmp(login_req->isid, login->isid, 6) != 0) {
241                 pr_err("Login request changed ISID unexpectedly,"
242                                 " protocol error.\n");
243                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
244                                 ISCSI_LOGIN_STATUS_INIT_ERR);
245                 return -1;
246         }
247
248         if (login_req->itt != login->init_task_tag) {
249                 pr_err("Login request changed ITT unexpectedly to"
250                         " 0x%08x, protocol error.\n", login_req->itt);
251                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
252                                 ISCSI_LOGIN_STATUS_INIT_ERR);
253                 return -1;
254         }
255
256         if (payload_length > MAX_KEY_VALUE_PAIRS) {
257                 pr_err("Login request payload exceeds default"
258                         " MaxRecvDataSegmentLength: %u, protocol error.\n",
259                                 MAX_KEY_VALUE_PAIRS);
260                 return -1;
261         }
262
263         return 0;
264 }
265
266 static int iscsi_target_check_first_request(
267         struct iscsi_conn *conn,
268         struct iscsi_login *login)
269 {
270         struct iscsi_param *param = NULL;
271         struct se_node_acl *se_nacl;
272
273         login->first_request = 0;
274
275         list_for_each_entry(param, &conn->param_list->param_list, p_list) {
276                 if (!strncmp(param->name, SESSIONTYPE, 11)) {
277                         if (!IS_PSTATE_ACCEPTOR(param)) {
278                                 pr_err("SessionType key not received"
279                                         " in first login request.\n");
280                                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
281                                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
282                                 return -1;
283                         }
284                         if (!strncmp(param->value, DISCOVERY, 9))
285                                 return 0;
286                 }
287
288                 if (!strncmp(param->name, INITIATORNAME, 13)) {
289                         if (!IS_PSTATE_ACCEPTOR(param)) {
290                                 if (!login->leading_connection)
291                                         continue;
292
293                                 pr_err("InitiatorName key not received"
294                                         " in first login request.\n");
295                                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
296                                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
297                                 return -1;
298                         }
299
300                         /*
301                          * For non-leading connections, double check that the
302                          * received InitiatorName matches the existing session's
303                          * struct iscsi_node_acl.
304                          */
305                         if (!login->leading_connection) {
306                                 se_nacl = conn->sess->se_sess->se_node_acl;
307                                 if (!se_nacl) {
308                                         pr_err("Unable to locate"
309                                                 " struct se_node_acl\n");
310                                         iscsit_tx_login_rsp(conn,
311                                                         ISCSI_STATUS_CLS_INITIATOR_ERR,
312                                                         ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
313                                         return -1;
314                                 }
315
316                                 if (strcmp(param->value,
317                                                 se_nacl->initiatorname)) {
318                                         pr_err("Incorrect"
319                                                 " InitiatorName: %s for this"
320                                                 " iSCSI Initiator Node.\n",
321                                                 param->value);
322                                         iscsit_tx_login_rsp(conn,
323                                                         ISCSI_STATUS_CLS_INITIATOR_ERR,
324                                                         ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
325                                         return -1;
326                                 }
327                         }
328                 }
329         }
330
331         return 0;
332 }
333
334 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
335 {
336         u32 padding = 0;
337         struct iscsi_session *sess = conn->sess;
338         struct iscsi_login_rsp *login_rsp;
339
340         login_rsp = (struct iscsi_login_rsp *) login->rsp;
341
342         login_rsp->opcode               = ISCSI_OP_LOGIN_RSP;
343         hton24(login_rsp->dlength, login->rsp_length);
344         memcpy(login_rsp->isid, login->isid, 6);
345         login_rsp->tsih                 = cpu_to_be16(login->tsih);
346         login_rsp->itt                  = cpu_to_be32(login->init_task_tag);
347         login_rsp->statsn               = cpu_to_be32(conn->stat_sn++);
348         login_rsp->exp_cmdsn            = cpu_to_be32(conn->sess->exp_cmd_sn);
349         login_rsp->max_cmdsn            = cpu_to_be32(conn->sess->max_cmd_sn);
350
351         pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
352                 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
353                 " %u\n", login_rsp->flags, ntohl(login_rsp->itt),
354                 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
355                 ntohl(login_rsp->statsn), login->rsp_length);
356
357         padding = ((-login->rsp_length) & 3);
358
359         if (iscsi_login_tx_data(
360                         conn,
361                         login->rsp,
362                         login->rsp_buf,
363                         login->rsp_length + padding) < 0)
364                 return -1;
365
366         login->rsp_length               = 0;
367         login_rsp->tsih                 = be16_to_cpu(login_rsp->tsih);
368         login_rsp->itt                  = be32_to_cpu(login_rsp->itt);
369         login_rsp->statsn               = be32_to_cpu(login_rsp->statsn);
370         mutex_lock(&sess->cmdsn_mutex);
371         login_rsp->exp_cmdsn            = be32_to_cpu(sess->exp_cmd_sn);
372         login_rsp->max_cmdsn            = be32_to_cpu(sess->max_cmd_sn);
373         mutex_unlock(&sess->cmdsn_mutex);
374
375         return 0;
376 }
377
378 static int iscsi_target_do_rx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
379 {
380         u32 padding = 0, payload_length;
381         struct iscsi_login_req *login_req;
382
383         if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
384                 return -1;
385
386         login_req = (struct iscsi_login_req *) login->req;
387         payload_length                  = ntoh24(login_req->dlength);
388         login_req->tsih                 = be16_to_cpu(login_req->tsih);
389         login_req->itt                  = be32_to_cpu(login_req->itt);
390         login_req->cid                  = be16_to_cpu(login_req->cid);
391         login_req->cmdsn                = be32_to_cpu(login_req->cmdsn);
392         login_req->exp_statsn           = be32_to_cpu(login_req->exp_statsn);
393
394         pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
395                 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
396                  login_req->flags, login_req->itt, login_req->cmdsn,
397                  login_req->exp_statsn, login_req->cid, payload_length);
398
399         if (iscsi_target_check_login_request(conn, login) < 0)
400                 return -1;
401
402         padding = ((-payload_length) & 3);
403         memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
404
405         if (iscsi_login_rx_data(
406                         conn,
407                         login->req_buf,
408                         payload_length + padding) < 0)
409                 return -1;
410
411         return 0;
412 }
413
414 static int iscsi_target_do_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
415 {
416         if (iscsi_target_do_tx_login_io(conn, login) < 0)
417                 return -1;
418
419         if (iscsi_target_do_rx_login_io(conn, login) < 0)
420                 return -1;
421
422         return 0;
423 }
424
425 static int iscsi_target_get_initial_payload(
426         struct iscsi_conn *conn,
427         struct iscsi_login *login)
428 {
429         u32 padding = 0, payload_length;
430         struct iscsi_login_req *login_req;
431
432         login_req = (struct iscsi_login_req *) login->req;
433         payload_length = ntoh24(login_req->dlength);
434
435         pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
436                 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
437                 login_req->flags, login_req->itt, login_req->cmdsn,
438                 login_req->exp_statsn, payload_length);
439
440         if (iscsi_target_check_login_request(conn, login) < 0)
441                 return -1;
442
443         padding = ((-payload_length) & 3);
444
445         if (iscsi_login_rx_data(
446                         conn,
447                         login->req_buf,
448                         payload_length + padding) < 0)
449                 return -1;
450
451         return 0;
452 }
453
454 /*
455  *      NOTE: We check for existing sessions or connections AFTER the initiator
456  *      has been successfully authenticated in order to protect against faked
457  *      ISID/TSIH combinations.
458  */
459 static int iscsi_target_check_for_existing_instances(
460         struct iscsi_conn *conn,
461         struct iscsi_login *login)
462 {
463         if (login->checked_for_existing)
464                 return 0;
465
466         login->checked_for_existing = 1;
467
468         if (!login->tsih)
469                 return iscsi_check_for_session_reinstatement(conn);
470         else
471                 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
472                                 login->initial_exp_statsn);
473 }
474
475 static int iscsi_target_do_authentication(
476         struct iscsi_conn *conn,
477         struct iscsi_login *login)
478 {
479         int authret;
480         u32 payload_length;
481         struct iscsi_param *param;
482         struct iscsi_login_req *login_req;
483         struct iscsi_login_rsp *login_rsp;
484
485         login_req = (struct iscsi_login_req *) login->req;
486         login_rsp = (struct iscsi_login_rsp *) login->rsp;
487         payload_length = ntoh24(login_req->dlength);
488
489         param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
490         if (!param)
491                 return -1;
492
493         authret = iscsi_handle_authentication(
494                         conn,
495                         login->req_buf,
496                         login->rsp_buf,
497                         payload_length,
498                         &login->rsp_length,
499                         param->value);
500         switch (authret) {
501         case 0:
502                 pr_debug("Received OK response"
503                 " from LIO Authentication, continuing.\n");
504                 break;
505         case 1:
506                 pr_debug("iSCSI security negotiation"
507                         " completed successfully.\n");
508                 login->auth_complete = 1;
509                 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
510                     (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
511                         login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
512                                              ISCSI_FLAG_LOGIN_TRANSIT);
513                         login->current_stage = 1;
514                 }
515                 return iscsi_target_check_for_existing_instances(
516                                 conn, login);
517         case 2:
518                 pr_err("Security negotiation"
519                         " failed.\n");
520                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
521                                 ISCSI_LOGIN_STATUS_AUTH_FAILED);
522                 return -1;
523         default:
524                 pr_err("Received unknown error %d from LIO"
525                                 " Authentication\n", authret);
526                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
527                                 ISCSI_LOGIN_STATUS_TARGET_ERROR);
528                 return -1;
529         }
530
531         return 0;
532 }
533
534 static int iscsi_target_handle_csg_zero(
535         struct iscsi_conn *conn,
536         struct iscsi_login *login)
537 {
538         int ret;
539         u32 payload_length;
540         struct iscsi_param *param;
541         struct iscsi_login_req *login_req;
542         struct iscsi_login_rsp *login_rsp;
543
544         login_req = (struct iscsi_login_req *) login->req;
545         login_rsp = (struct iscsi_login_rsp *) login->rsp;
546         payload_length = ntoh24(login_req->dlength);
547
548         param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
549         if (!param)
550                 return -1;
551
552         ret = iscsi_decode_text_input(
553                         PHASE_SECURITY|PHASE_DECLARATIVE,
554                         SENDER_INITIATOR|SENDER_RECEIVER,
555                         login->req_buf,
556                         payload_length,
557                         conn->param_list);
558         if (ret < 0)
559                 return -1;
560
561         if (ret > 0) {
562                 if (login->auth_complete) {
563                         pr_err("Initiator has already been"
564                                 " successfully authenticated, but is still"
565                                 " sending %s keys.\n", param->value);
566                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
567                                         ISCSI_LOGIN_STATUS_INIT_ERR);
568                         return -1;
569                 }
570
571                 goto do_auth;
572         }
573
574         if (login->first_request)
575                 if (iscsi_target_check_first_request(conn, login) < 0)
576                         return -1;
577
578         ret = iscsi_encode_text_output(
579                         PHASE_SECURITY|PHASE_DECLARATIVE,
580                         SENDER_TARGET,
581                         login->rsp_buf,
582                         &login->rsp_length,
583                         conn->param_list);
584         if (ret < 0)
585                 return -1;
586
587         if (!iscsi_check_negotiated_keys(conn->param_list)) {
588                 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
589                     !strncmp(param->value, NONE, 4)) {
590                         pr_err("Initiator sent AuthMethod=None but"
591                                 " Target is enforcing iSCSI Authentication,"
592                                         " login failed.\n");
593                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
594                                         ISCSI_LOGIN_STATUS_AUTH_FAILED);
595                         return -1;
596                 }
597
598                 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
599                     !login->auth_complete)
600                         return 0;
601
602                 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
603                         return 0;
604
605                 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
606                     (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
607                         login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
608                                             ISCSI_FLAG_LOGIN_TRANSIT;
609                         login->current_stage = 1;
610                 }
611         }
612
613         return 0;
614 do_auth:
615         return iscsi_target_do_authentication(conn, login);
616 }
617
618 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
619 {
620         int ret;
621         u32 payload_length;
622         struct iscsi_login_req *login_req;
623         struct iscsi_login_rsp *login_rsp;
624
625         login_req = (struct iscsi_login_req *) login->req;
626         login_rsp = (struct iscsi_login_rsp *) login->rsp;
627         payload_length = ntoh24(login_req->dlength);
628
629         ret = iscsi_decode_text_input(
630                         PHASE_OPERATIONAL|PHASE_DECLARATIVE,
631                         SENDER_INITIATOR|SENDER_RECEIVER,
632                         login->req_buf,
633                         payload_length,
634                         conn->param_list);
635         if (ret < 0) {
636                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
637                                 ISCSI_LOGIN_STATUS_INIT_ERR);
638                 return -1;
639         }
640
641         if (login->first_request)
642                 if (iscsi_target_check_first_request(conn, login) < 0)
643                         return -1;
644
645         if (iscsi_target_check_for_existing_instances(conn, login) < 0)
646                 return -1;
647
648         ret = iscsi_encode_text_output(
649                         PHASE_OPERATIONAL|PHASE_DECLARATIVE,
650                         SENDER_TARGET,
651                         login->rsp_buf,
652                         &login->rsp_length,
653                         conn->param_list);
654         if (ret < 0) {
655                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
656                                 ISCSI_LOGIN_STATUS_INIT_ERR);
657                 return -1;
658         }
659
660         if (!login->auth_complete &&
661              ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) {
662                 pr_err("Initiator is requesting CSG: 1, has not been"
663                          " successfully authenticated, and the Target is"
664                         " enforcing iSCSI Authentication, login failed.\n");
665                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
666                                 ISCSI_LOGIN_STATUS_AUTH_FAILED);
667                 return -1;
668         }
669
670         if (!iscsi_check_negotiated_keys(conn->param_list))
671                 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
672                     (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
673                         login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
674                                             ISCSI_FLAG_LOGIN_TRANSIT;
675
676         return 0;
677 }
678
679 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
680 {
681         int pdu_count = 0;
682         struct iscsi_login_req *login_req;
683         struct iscsi_login_rsp *login_rsp;
684
685         login_req = (struct iscsi_login_req *) login->req;
686         login_rsp = (struct iscsi_login_rsp *) login->rsp;
687
688         while (1) {
689                 if (++pdu_count > MAX_LOGIN_PDUS) {
690                         pr_err("MAX_LOGIN_PDUS count reached.\n");
691                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
692                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
693                         return -1;
694                 }
695
696                 switch ((login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2) {
697                 case 0:
698                         login_rsp->flags |= (0 & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK);
699                         if (iscsi_target_handle_csg_zero(conn, login) < 0)
700                                 return -1;
701                         break;
702                 case 1:
703                         login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
704                         if (iscsi_target_handle_csg_one(conn, login) < 0)
705                                 return -1;
706                         if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
707                                 login->tsih = conn->sess->tsih;
708                                 if (iscsi_target_do_tx_login_io(conn,
709                                                 login) < 0)
710                                         return -1;
711                                 return 0;
712                         }
713                         break;
714                 default:
715                         pr_err("Illegal CSG: %d received from"
716                                 " Initiator, protocol error.\n",
717                                 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
718                                 >> 2);
719                         break;
720                 }
721
722                 if (iscsi_target_do_login_io(conn, login) < 0)
723                         return -1;
724
725                 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
726                         login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
727                         login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
728                 }
729         }
730
731         return 0;
732 }
733
734 static void iscsi_initiatorname_tolower(
735         char *param_buf)
736 {
737         char *c;
738         u32 iqn_size = strlen(param_buf), i;
739
740         for (i = 0; i < iqn_size; i++) {
741                 c = (char *)&param_buf[i];
742                 if (!isupper(*c))
743                         continue;
744
745                 *c = tolower(*c);
746         }
747 }
748
749 /*
750  * Processes the first Login Request..
751  */
752 static int iscsi_target_locate_portal(
753         struct iscsi_np *np,
754         struct iscsi_conn *conn,
755         struct iscsi_login *login)
756 {
757         char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
758         char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
759         struct iscsi_session *sess = conn->sess;
760         struct iscsi_tiqn *tiqn;
761         struct iscsi_login_req *login_req;
762         struct iscsi_targ_login_rsp *login_rsp;
763         u32 payload_length;
764         int sessiontype = 0, ret = 0;
765
766         login_req = (struct iscsi_login_req *) login->req;
767         login_rsp = (struct iscsi_targ_login_rsp *) login->rsp;
768         payload_length = ntoh24(login_req->dlength);
769
770         login->first_request    = 1;
771         login->leading_connection = (!login_req->tsih) ? 1 : 0;
772         login->current_stage    =
773                 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
774         login->version_min      = login_req->min_version;
775         login->version_max      = login_req->max_version;
776         memcpy(login->isid, login_req->isid, 6);
777         login->cmd_sn           = login_req->cmdsn;
778         login->init_task_tag    = login_req->itt;
779         login->initial_exp_statsn = login_req->exp_statsn;
780         login->cid              = login_req->cid;
781         login->tsih             = login_req->tsih;
782
783         if (iscsi_target_get_initial_payload(conn, login) < 0)
784                 return -1;
785
786         tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
787         if (!tmpbuf) {
788                 pr_err("Unable to allocate memory for tmpbuf.\n");
789                 return -1;
790         }
791
792         memcpy(tmpbuf, login->req_buf, payload_length);
793         tmpbuf[payload_length] = '\0';
794         start = tmpbuf;
795         end = (start + payload_length);
796
797         /*
798          * Locate the initial keys expected from the Initiator node in
799          * the first login request in order to progress with the login phase.
800          */
801         while (start < end) {
802                 if (iscsi_extract_key_value(start, &key, &value) < 0) {
803                         ret = -1;
804                         goto out;
805                 }
806
807                 if (!strncmp(key, "InitiatorName", 13))
808                         i_buf = value;
809                 else if (!strncmp(key, "SessionType", 11))
810                         s_buf = value;
811                 else if (!strncmp(key, "TargetName", 10))
812                         t_buf = value;
813
814                 start += strlen(key) + strlen(value) + 2;
815         }
816
817         /*
818          * See 5.3.  Login Phase.
819          */
820         if (!i_buf) {
821                 pr_err("InitiatorName key not received"
822                         " in first login request.\n");
823                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
824                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
825                 ret = -1;
826                 goto out;
827         }
828         /*
829          * Convert the incoming InitiatorName to lowercase following
830          * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
831          * are NOT case sensitive.
832          */
833         iscsi_initiatorname_tolower(i_buf);
834
835         if (!s_buf) {
836                 if (!login->leading_connection)
837                         goto get_target;
838
839                 pr_err("SessionType key not received"
840                         " in first login request.\n");
841                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
842                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
843                 ret = -1;
844                 goto out;
845         }
846
847         /*
848          * Use default portal group for discovery sessions.
849          */
850         sessiontype = strncmp(s_buf, DISCOVERY, 9);
851         if (!sessiontype) {
852                 conn->tpg = iscsit_global->discovery_tpg;
853                 if (!login->leading_connection)
854                         goto get_target;
855
856                 sess->sess_ops->SessionType = 1;
857                 /*
858                  * Setup crc32c modules from libcrypto
859                  */
860                 if (iscsi_login_setup_crypto(conn) < 0) {
861                         pr_err("iscsi_login_setup_crypto() failed\n");
862                         ret = -1;
863                         goto out;
864                 }
865                 /*
866                  * Serialize access across the discovery struct iscsi_portal_group to
867                  * process login attempt.
868                  */
869                 if (iscsit_access_np(np, conn->tpg) < 0) {
870                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
871                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
872                         ret = -1;
873                         goto out;
874                 }
875                 ret = 0;
876                 goto out;
877         }
878
879 get_target:
880         if (!t_buf) {
881                 pr_err("TargetName key not received"
882                         " in first login request while"
883                         " SessionType=Normal.\n");
884                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
885                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
886                 ret = -1;
887                 goto out;
888         }
889
890         /*
891          * Locate Target IQN from Storage Node.
892          */
893         tiqn = iscsit_get_tiqn_for_login(t_buf);
894         if (!tiqn) {
895                 pr_err("Unable to locate Target IQN: %s in"
896                         " Storage Node\n", t_buf);
897                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
898                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
899                 ret = -1;
900                 goto out;
901         }
902         pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
903
904         /*
905          * Locate Target Portal Group from Storage Node.
906          */
907         conn->tpg = iscsit_get_tpg_from_np(tiqn, np);
908         if (!conn->tpg) {
909                 pr_err("Unable to locate Target Portal Group"
910                                 " on %s\n", tiqn->tiqn);
911                 iscsit_put_tiqn_for_login(tiqn);
912                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
913                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
914                 ret = -1;
915                 goto out;
916         }
917         pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
918         /*
919          * Setup crc32c modules from libcrypto
920          */
921         if (iscsi_login_setup_crypto(conn) < 0) {
922                 pr_err("iscsi_login_setup_crypto() failed\n");
923                 ret = -1;
924                 goto out;
925         }
926         /*
927          * Serialize access across the struct iscsi_portal_group to
928          * process login attempt.
929          */
930         if (iscsit_access_np(np, conn->tpg) < 0) {
931                 iscsit_put_tiqn_for_login(tiqn);
932                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
933                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
934                 ret = -1;
935                 conn->tpg = NULL;
936                 goto out;
937         }
938
939         /*
940          * conn->sess->node_acl will be set when the referenced
941          * struct iscsi_session is located from received ISID+TSIH in
942          * iscsi_login_non_zero_tsih_s2().
943          */
944         if (!login->leading_connection) {
945                 ret = 0;
946                 goto out;
947         }
948
949         /*
950          * This value is required in iscsi_login_zero_tsih_s2()
951          */
952         sess->sess_ops->SessionType = 0;
953
954         /*
955          * Locate incoming Initiator IQN reference from Storage Node.
956          */
957         sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
958                         &conn->tpg->tpg_se_tpg, i_buf);
959         if (!sess->se_sess->se_node_acl) {
960                 pr_err("iSCSI Initiator Node: %s is not authorized to"
961                         " access iSCSI target portal group: %hu.\n",
962                                 i_buf, conn->tpg->tpgt);
963                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
964                                 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
965                 ret = -1;
966                 goto out;
967         }
968
969         ret = 0;
970 out:
971         kfree(tmpbuf);
972         return ret;
973 }
974
975 struct iscsi_login *iscsi_target_init_negotiation(
976         struct iscsi_np *np,
977         struct iscsi_conn *conn,
978         char *login_pdu)
979 {
980         struct iscsi_login *login;
981
982         login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
983         if (!login) {
984                 pr_err("Unable to allocate memory for struct iscsi_login.\n");
985                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
986                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
987                 return NULL;
988         }
989
990         login->req = kmemdup(login_pdu, ISCSI_HDR_LEN, GFP_KERNEL);
991         if (!login->req) {
992                 pr_err("Unable to allocate memory for Login Request.\n");
993                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
994                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
995                 goto out;
996         }
997
998         login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
999         if (!login->req_buf) {
1000                 pr_err("Unable to allocate memory for response buffer.\n");
1001                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1002                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1003                 goto out;
1004         }
1005         /*
1006          * SessionType: Discovery
1007          *
1008          *      Locates Default Portal
1009          *
1010          * SessionType: Normal
1011          *
1012          *      Locates Target Portal from NP -> Target IQN
1013          */
1014         if (iscsi_target_locate_portal(np, conn, login) < 0) {
1015                 pr_err("iSCSI Login negotiation failed.\n");
1016                 goto out;
1017         }
1018
1019         return login;
1020 out:
1021         kfree(login->req);
1022         kfree(login->req_buf);
1023         kfree(login);
1024
1025         return NULL;
1026 }
1027
1028 int iscsi_target_start_negotiation(
1029         struct iscsi_login *login,
1030         struct iscsi_conn *conn)
1031 {
1032         int ret = -1;
1033
1034         login->rsp = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
1035         if (!login->rsp) {
1036                 pr_err("Unable to allocate memory for"
1037                                 " Login Response.\n");
1038                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1039                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1040                 ret = -1;
1041                 goto out;
1042         }
1043
1044         login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
1045         if (!login->rsp_buf) {
1046                 pr_err("Unable to allocate memory for"
1047                         " request buffer.\n");
1048                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1049                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
1050                 ret = -1;
1051                 goto out;
1052         }
1053
1054         ret = iscsi_target_do_login(conn, login);
1055 out:
1056         if (ret != 0)
1057                 iscsi_remove_failed_auth_entry(conn);
1058
1059         iscsi_target_nego_release(login, conn);
1060         return ret;
1061 }
1062
1063 void iscsi_target_nego_release(
1064         struct iscsi_login *login,
1065         struct iscsi_conn *conn)
1066 {
1067         kfree(login->req);
1068         kfree(login->rsp);
1069         kfree(login->req_buf);
1070         kfree(login->rsp_buf);
1071         kfree(login);
1072 }