]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/target/iscsi/iscsi_target_configfs.c
iscsi-target: Fix tfc_tpg_auth_cit configfs length overflow
[karo-tx-linux.git] / drivers / target / iscsi / iscsi_target_configfs.c
1 /*******************************************************************************
2  * This file contains the configfs implementation for iSCSI Target mode
3  * from the LIO-Target Project.
4  *
5  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6  *
7  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8  *
9  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  ****************************************************************************/
21
22 #include <linux/configfs.h>
23 #include <linux/export.h>
24 #include <linux/inet.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_fabric.h>
27 #include <target/target_core_fabric_configfs.h>
28 #include <target/target_core_configfs.h>
29 #include <target/configfs_macros.h>
30 #include <target/iscsi/iscsi_transport.h>
31
32 #include "iscsi_target_core.h"
33 #include "iscsi_target_parameters.h"
34 #include "iscsi_target_device.h"
35 #include "iscsi_target_erl0.h"
36 #include "iscsi_target_nodeattrib.h"
37 #include "iscsi_target_tpg.h"
38 #include "iscsi_target_util.h"
39 #include "iscsi_target.h"
40 #include "iscsi_target_stat.h"
41 #include "iscsi_target_configfs.h"
42
43 struct target_fabric_configfs *lio_target_fabric_configfs;
44
45 struct lio_target_configfs_attribute {
46         struct configfs_attribute attr;
47         ssize_t (*show)(void *, char *);
48         ssize_t (*store)(void *, const char *, size_t);
49 };
50
51 /* Start items for lio_target_portal_cit */
52
53 static ssize_t lio_target_np_show_sctp(
54         struct se_tpg_np *se_tpg_np,
55         char *page)
56 {
57         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
58                                 struct iscsi_tpg_np, se_tpg_np);
59         struct iscsi_tpg_np *tpg_np_sctp;
60         ssize_t rb;
61
62         tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
63         if (tpg_np_sctp)
64                 rb = sprintf(page, "1\n");
65         else
66                 rb = sprintf(page, "0\n");
67
68         return rb;
69 }
70
71 static ssize_t lio_target_np_store_sctp(
72         struct se_tpg_np *se_tpg_np,
73         const char *page,
74         size_t count)
75 {
76         struct iscsi_np *np;
77         struct iscsi_portal_group *tpg;
78         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
79                                 struct iscsi_tpg_np, se_tpg_np);
80         struct iscsi_tpg_np *tpg_np_sctp = NULL;
81         char *endptr;
82         u32 op;
83         int ret;
84
85         op = simple_strtoul(page, &endptr, 0);
86         if ((op != 1) && (op != 0)) {
87                 pr_err("Illegal value for tpg_enable: %u\n", op);
88                 return -EINVAL;
89         }
90         np = tpg_np->tpg_np;
91         if (!np) {
92                 pr_err("Unable to locate struct iscsi_np from"
93                                 " struct iscsi_tpg_np\n");
94                 return -EINVAL;
95         }
96
97         tpg = tpg_np->tpg;
98         if (iscsit_get_tpg(tpg) < 0)
99                 return -EINVAL;
100
101         if (op) {
102                 /*
103                  * Use existing np->np_sockaddr for SCTP network portal reference
104                  */
105                 tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
106                                         np->np_ip, tpg_np, ISCSI_SCTP_TCP);
107                 if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
108                         goto out;
109         } else {
110                 tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
111                 if (!tpg_np_sctp)
112                         goto out;
113
114                 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
115                 if (ret < 0)
116                         goto out;
117         }
118
119         iscsit_put_tpg(tpg);
120         return count;
121 out:
122         iscsit_put_tpg(tpg);
123         return -EINVAL;
124 }
125
126 TF_NP_BASE_ATTR(lio_target, sctp, S_IRUGO | S_IWUSR);
127
128 static ssize_t lio_target_np_show_iser(
129         struct se_tpg_np *se_tpg_np,
130         char *page)
131 {
132         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
133                                 struct iscsi_tpg_np, se_tpg_np);
134         struct iscsi_tpg_np *tpg_np_iser;
135         ssize_t rb;
136
137         tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
138         if (tpg_np_iser)
139                 rb = sprintf(page, "1\n");
140         else
141                 rb = sprintf(page, "0\n");
142
143         return rb;
144 }
145
146 static ssize_t lio_target_np_store_iser(
147         struct se_tpg_np *se_tpg_np,
148         const char *page,
149         size_t count)
150 {
151         struct iscsi_np *np;
152         struct iscsi_portal_group *tpg;
153         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
154                                 struct iscsi_tpg_np, se_tpg_np);
155         struct iscsi_tpg_np *tpg_np_iser = NULL;
156         char *endptr;
157         u32 op;
158         int rc;
159
160         op = simple_strtoul(page, &endptr, 0);
161         if ((op != 1) && (op != 0)) {
162                 pr_err("Illegal value for tpg_enable: %u\n", op);
163                 return -EINVAL;
164         }
165         np = tpg_np->tpg_np;
166         if (!np) {
167                 pr_err("Unable to locate struct iscsi_np from"
168                                 " struct iscsi_tpg_np\n");
169                 return -EINVAL;
170         }
171
172         tpg = tpg_np->tpg;
173         if (iscsit_get_tpg(tpg) < 0)
174                 return -EINVAL;
175
176         if (op) {
177                 int rc = request_module("ib_isert");
178                 if (rc != 0)
179                         pr_warn("Unable to request_module for ib_isert\n");
180
181                 tpg_np_iser = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
182                                 np->np_ip, tpg_np, ISCSI_INFINIBAND);
183                 if (!tpg_np_iser || IS_ERR(tpg_np_iser))
184                         goto out;
185         } else {
186                 tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
187                 if (!tpg_np_iser)
188                         goto out;
189
190                 rc = iscsit_tpg_del_network_portal(tpg, tpg_np_iser);
191                 if (rc < 0)
192                         goto out;
193         }
194
195         printk("lio_target_np_store_iser() done, op: %d\n", op);
196
197         iscsit_put_tpg(tpg);
198         return count;
199 out:
200         iscsit_put_tpg(tpg);
201         return -EINVAL;
202 }
203
204 TF_NP_BASE_ATTR(lio_target, iser, S_IRUGO | S_IWUSR);
205
206 static struct configfs_attribute *lio_target_portal_attrs[] = {
207         &lio_target_np_sctp.attr,
208         &lio_target_np_iser.attr,
209         NULL,
210 };
211
212 /* Stop items for lio_target_portal_cit */
213
214 /* Start items for lio_target_np_cit */
215
216 #define MAX_PORTAL_LEN          256
217
218 static struct se_tpg_np *lio_target_call_addnptotpg(
219         struct se_portal_group *se_tpg,
220         struct config_group *group,
221         const char *name)
222 {
223         struct iscsi_portal_group *tpg;
224         struct iscsi_tpg_np *tpg_np;
225         char *str, *str2, *ip_str, *port_str;
226         struct __kernel_sockaddr_storage sockaddr;
227         struct sockaddr_in *sock_in;
228         struct sockaddr_in6 *sock_in6;
229         unsigned long port;
230         int ret;
231         char buf[MAX_PORTAL_LEN + 1];
232
233         if (strlen(name) > MAX_PORTAL_LEN) {
234                 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
235                         (int)strlen(name), MAX_PORTAL_LEN);
236                 return ERR_PTR(-EOVERFLOW);
237         }
238         memset(buf, 0, MAX_PORTAL_LEN + 1);
239         snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
240
241         memset(&sockaddr, 0, sizeof(struct __kernel_sockaddr_storage));
242
243         str = strstr(buf, "[");
244         if (str) {
245                 const char *end;
246
247                 str2 = strstr(str, "]");
248                 if (!str2) {
249                         pr_err("Unable to locate trailing \"]\""
250                                 " in IPv6 iSCSI network portal address\n");
251                         return ERR_PTR(-EINVAL);
252                 }
253                 str++; /* Skip over leading "[" */
254                 *str2 = '\0'; /* Terminate the IPv6 address */
255                 str2++; /* Skip over the "]" */
256                 port_str = strstr(str2, ":");
257                 if (!port_str) {
258                         pr_err("Unable to locate \":port\""
259                                 " in IPv6 iSCSI network portal address\n");
260                         return ERR_PTR(-EINVAL);
261                 }
262                 *port_str = '\0'; /* Terminate string for IP */
263                 port_str++; /* Skip over ":" */
264
265                 ret = strict_strtoul(port_str, 0, &port);
266                 if (ret < 0) {
267                         pr_err("strict_strtoul() failed for port_str: %d\n", ret);
268                         return ERR_PTR(ret);
269                 }
270                 sock_in6 = (struct sockaddr_in6 *)&sockaddr;
271                 sock_in6->sin6_family = AF_INET6;
272                 sock_in6->sin6_port = htons((unsigned short)port);
273                 ret = in6_pton(str, IPV6_ADDRESS_SPACE,
274                                 (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
275                 if (ret <= 0) {
276                         pr_err("in6_pton returned: %d\n", ret);
277                         return ERR_PTR(-EINVAL);
278                 }
279         } else {
280                 str = ip_str = &buf[0];
281                 port_str = strstr(ip_str, ":");
282                 if (!port_str) {
283                         pr_err("Unable to locate \":port\""
284                                 " in IPv4 iSCSI network portal address\n");
285                         return ERR_PTR(-EINVAL);
286                 }
287                 *port_str = '\0'; /* Terminate string for IP */
288                 port_str++; /* Skip over ":" */
289
290                 ret = strict_strtoul(port_str, 0, &port);
291                 if (ret < 0) {
292                         pr_err("strict_strtoul() failed for port_str: %d\n", ret);
293                         return ERR_PTR(ret);
294                 }
295                 sock_in = (struct sockaddr_in *)&sockaddr;
296                 sock_in->sin_family = AF_INET;
297                 sock_in->sin_port = htons((unsigned short)port);
298                 sock_in->sin_addr.s_addr = in_aton(ip_str);
299         }
300         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
301         ret = iscsit_get_tpg(tpg);
302         if (ret < 0)
303                 return ERR_PTR(-EINVAL);
304
305         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
306                 " PORTAL: %s\n",
307                 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
308                 tpg->tpgt, name);
309         /*
310          * Assume ISCSI_TCP by default.  Other network portals for other
311          * iSCSI fabrics:
312          *
313          * Traditional iSCSI over SCTP (initial support)
314          * iSER/TCP (TODO, hardware available)
315          * iSER/SCTP (TODO, software emulation with osc-iwarp)
316          * iSER/IB (TODO, hardware available)
317          *
318          * can be enabled with attributes under
319          * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
320          *
321          */
322         tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, str, NULL,
323                                 ISCSI_TCP);
324         if (IS_ERR(tpg_np)) {
325                 iscsit_put_tpg(tpg);
326                 return ERR_CAST(tpg_np);
327         }
328         pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
329
330         iscsit_put_tpg(tpg);
331         return &tpg_np->se_tpg_np;
332 }
333
334 static void lio_target_call_delnpfromtpg(
335         struct se_tpg_np *se_tpg_np)
336 {
337         struct iscsi_portal_group *tpg;
338         struct iscsi_tpg_np *tpg_np;
339         struct se_portal_group *se_tpg;
340         int ret;
341
342         tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
343         tpg = tpg_np->tpg;
344         ret = iscsit_get_tpg(tpg);
345         if (ret < 0)
346                 return;
347
348         se_tpg = &tpg->tpg_se_tpg;
349         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
350                 " PORTAL: %s:%hu\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
351                 tpg->tpgt, tpg_np->tpg_np->np_ip, tpg_np->tpg_np->np_port);
352
353         ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
354         if (ret < 0)
355                 goto out;
356
357         pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
358 out:
359         iscsit_put_tpg(tpg);
360 }
361
362 /* End items for lio_target_np_cit */
363
364 /* Start items for lio_target_nacl_attrib_cit */
365
366 #define DEF_NACL_ATTRIB(name)                                           \
367 static ssize_t iscsi_nacl_attrib_show_##name(                           \
368         struct se_node_acl *se_nacl,                                    \
369         char *page)                                                     \
370 {                                                                       \
371         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
372                                         se_node_acl);                   \
373                                                                         \
374         return sprintf(page, "%u\n", ISCSI_NODE_ATTRIB(nacl)->name);    \
375 }                                                                       \
376                                                                         \
377 static ssize_t iscsi_nacl_attrib_store_##name(                          \
378         struct se_node_acl *se_nacl,                                    \
379         const char *page,                                               \
380         size_t count)                                                   \
381 {                                                                       \
382         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
383                                         se_node_acl);                   \
384         char *endptr;                                                   \
385         u32 val;                                                        \
386         int ret;                                                        \
387                                                                         \
388         val = simple_strtoul(page, &endptr, 0);                         \
389         ret = iscsit_na_##name(nacl, val);                              \
390         if (ret < 0)                                                    \
391                 return ret;                                             \
392                                                                         \
393         return count;                                                   \
394 }
395
396 #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
397 /*
398  * Define iscsi_node_attrib_s_dataout_timeout
399  */
400 DEF_NACL_ATTRIB(dataout_timeout);
401 NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
402 /*
403  * Define iscsi_node_attrib_s_dataout_timeout_retries
404  */
405 DEF_NACL_ATTRIB(dataout_timeout_retries);
406 NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
407 /*
408  * Define iscsi_node_attrib_s_default_erl
409  */
410 DEF_NACL_ATTRIB(default_erl);
411 NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
412 /*
413  * Define iscsi_node_attrib_s_nopin_timeout
414  */
415 DEF_NACL_ATTRIB(nopin_timeout);
416 NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
417 /*
418  * Define iscsi_node_attrib_s_nopin_response_timeout
419  */
420 DEF_NACL_ATTRIB(nopin_response_timeout);
421 NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
422 /*
423  * Define iscsi_node_attrib_s_random_datain_pdu_offsets
424  */
425 DEF_NACL_ATTRIB(random_datain_pdu_offsets);
426 NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
427 /*
428  * Define iscsi_node_attrib_s_random_datain_seq_offsets
429  */
430 DEF_NACL_ATTRIB(random_datain_seq_offsets);
431 NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
432 /*
433  * Define iscsi_node_attrib_s_random_r2t_offsets
434  */
435 DEF_NACL_ATTRIB(random_r2t_offsets);
436 NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
437
438 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
439         &iscsi_nacl_attrib_dataout_timeout.attr,
440         &iscsi_nacl_attrib_dataout_timeout_retries.attr,
441         &iscsi_nacl_attrib_default_erl.attr,
442         &iscsi_nacl_attrib_nopin_timeout.attr,
443         &iscsi_nacl_attrib_nopin_response_timeout.attr,
444         &iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
445         &iscsi_nacl_attrib_random_datain_seq_offsets.attr,
446         &iscsi_nacl_attrib_random_r2t_offsets.attr,
447         NULL,
448 };
449
450 /* End items for lio_target_nacl_attrib_cit */
451
452 /* Start items for lio_target_nacl_auth_cit */
453
454 #define __DEF_NACL_AUTH_STR(prefix, name, flags)                        \
455 static ssize_t __iscsi_##prefix##_show_##name(                          \
456         struct iscsi_node_acl *nacl,                                    \
457         char *page)                                                     \
458 {                                                                       \
459         struct iscsi_node_auth *auth = &nacl->node_auth;                \
460                                                                         \
461         if (!capable(CAP_SYS_ADMIN))                                    \
462                 return -EPERM;                                          \
463         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);           \
464 }                                                                       \
465                                                                         \
466 static ssize_t __iscsi_##prefix##_store_##name(                         \
467         struct iscsi_node_acl *nacl,                                    \
468         const char *page,                                               \
469         size_t count)                                                   \
470 {                                                                       \
471         struct iscsi_node_auth *auth = &nacl->node_auth;                \
472                                                                         \
473         if (!capable(CAP_SYS_ADMIN))                                    \
474                 return -EPERM;                                          \
475                                                                         \
476         snprintf(auth->name, sizeof(auth->name), "%s", page);           \
477         if (!strncmp("NULL", auth->name, 4))                            \
478                 auth->naf_flags &= ~flags;                              \
479         else                                                            \
480                 auth->naf_flags |= flags;                               \
481                                                                         \
482         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                    \
483             (auth->naf_flags & NAF_PASSWORD_IN_SET))                    \
484                 auth->authenticate_target = 1;                          \
485         else                                                            \
486                 auth->authenticate_target = 0;                          \
487                                                                         \
488         return count;                                                   \
489 }
490
491 #define __DEF_NACL_AUTH_INT(prefix, name)                               \
492 static ssize_t __iscsi_##prefix##_show_##name(                          \
493         struct iscsi_node_acl *nacl,                                    \
494         char *page)                                                     \
495 {                                                                       \
496         struct iscsi_node_auth *auth = &nacl->node_auth;                \
497                                                                         \
498         if (!capable(CAP_SYS_ADMIN))                                    \
499                 return -EPERM;                                          \
500                                                                         \
501         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);           \
502 }
503
504 #define DEF_NACL_AUTH_STR(name, flags)                                  \
505         __DEF_NACL_AUTH_STR(nacl_auth, name, flags)                     \
506 static ssize_t iscsi_nacl_auth_show_##name(                             \
507         struct se_node_acl *nacl,                                       \
508         char *page)                                                     \
509 {                                                                       \
510         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
511                         struct iscsi_node_acl, se_node_acl), page);             \
512 }                                                                       \
513 static ssize_t iscsi_nacl_auth_store_##name(                            \
514         struct se_node_acl *nacl,                                       \
515         const char *page,                                               \
516         size_t count)                                                   \
517 {                                                                       \
518         return __iscsi_nacl_auth_store_##name(container_of(nacl,        \
519                         struct iscsi_node_acl, se_node_acl), page, count);      \
520 }
521
522 #define DEF_NACL_AUTH_INT(name)                                         \
523         __DEF_NACL_AUTH_INT(nacl_auth, name)                            \
524 static ssize_t iscsi_nacl_auth_show_##name(                             \
525         struct se_node_acl *nacl,                                       \
526         char *page)                                                     \
527 {                                                                       \
528         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
529                         struct iscsi_node_acl, se_node_acl), page);             \
530 }
531
532 #define AUTH_ATTR(_name, _mode) TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
533 #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
534
535 /*
536  * One-way authentication userid
537  */
538 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
539 AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
540 /*
541  * One-way authentication password
542  */
543 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
544 AUTH_ATTR(password, S_IRUGO | S_IWUSR);
545 /*
546  * Enforce mutual authentication
547  */
548 DEF_NACL_AUTH_INT(authenticate_target);
549 AUTH_ATTR_RO(authenticate_target);
550 /*
551  * Mutual authentication userid
552  */
553 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
554 AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
555 /*
556  * Mutual authentication password
557  */
558 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
559 AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
560
561 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
562         &iscsi_nacl_auth_userid.attr,
563         &iscsi_nacl_auth_password.attr,
564         &iscsi_nacl_auth_authenticate_target.attr,
565         &iscsi_nacl_auth_userid_mutual.attr,
566         &iscsi_nacl_auth_password_mutual.attr,
567         NULL,
568 };
569
570 /* End items for lio_target_nacl_auth_cit */
571
572 /* Start items for lio_target_nacl_param_cit */
573
574 #define DEF_NACL_PARAM(name)                                            \
575 static ssize_t iscsi_nacl_param_show_##name(                            \
576         struct se_node_acl *se_nacl,                                    \
577         char *page)                                                     \
578 {                                                                       \
579         struct iscsi_session *sess;                                     \
580         struct se_session *se_sess;                                     \
581         ssize_t rb;                                                     \
582                                                                         \
583         spin_lock_bh(&se_nacl->nacl_sess_lock);                         \
584         se_sess = se_nacl->nacl_sess;                                   \
585         if (!se_sess) {                                                 \
586                 rb = snprintf(page, PAGE_SIZE,                          \
587                         "No Active iSCSI Session\n");                   \
588         } else {                                                        \
589                 sess = se_sess->fabric_sess_ptr;                        \
590                 rb = snprintf(page, PAGE_SIZE, "%u\n",                  \
591                         (u32)sess->sess_ops->name);                     \
592         }                                                               \
593         spin_unlock_bh(&se_nacl->nacl_sess_lock);                       \
594                                                                         \
595         return rb;                                                      \
596 }
597
598 #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
599
600 DEF_NACL_PARAM(MaxConnections);
601 NACL_PARAM_ATTR(MaxConnections);
602
603 DEF_NACL_PARAM(InitialR2T);
604 NACL_PARAM_ATTR(InitialR2T);
605
606 DEF_NACL_PARAM(ImmediateData);
607 NACL_PARAM_ATTR(ImmediateData);
608
609 DEF_NACL_PARAM(MaxBurstLength);
610 NACL_PARAM_ATTR(MaxBurstLength);
611
612 DEF_NACL_PARAM(FirstBurstLength);
613 NACL_PARAM_ATTR(FirstBurstLength);
614
615 DEF_NACL_PARAM(DefaultTime2Wait);
616 NACL_PARAM_ATTR(DefaultTime2Wait);
617
618 DEF_NACL_PARAM(DefaultTime2Retain);
619 NACL_PARAM_ATTR(DefaultTime2Retain);
620
621 DEF_NACL_PARAM(MaxOutstandingR2T);
622 NACL_PARAM_ATTR(MaxOutstandingR2T);
623
624 DEF_NACL_PARAM(DataPDUInOrder);
625 NACL_PARAM_ATTR(DataPDUInOrder);
626
627 DEF_NACL_PARAM(DataSequenceInOrder);
628 NACL_PARAM_ATTR(DataSequenceInOrder);
629
630 DEF_NACL_PARAM(ErrorRecoveryLevel);
631 NACL_PARAM_ATTR(ErrorRecoveryLevel);
632
633 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
634         &iscsi_nacl_param_MaxConnections.attr,
635         &iscsi_nacl_param_InitialR2T.attr,
636         &iscsi_nacl_param_ImmediateData.attr,
637         &iscsi_nacl_param_MaxBurstLength.attr,
638         &iscsi_nacl_param_FirstBurstLength.attr,
639         &iscsi_nacl_param_DefaultTime2Wait.attr,
640         &iscsi_nacl_param_DefaultTime2Retain.attr,
641         &iscsi_nacl_param_MaxOutstandingR2T.attr,
642         &iscsi_nacl_param_DataPDUInOrder.attr,
643         &iscsi_nacl_param_DataSequenceInOrder.attr,
644         &iscsi_nacl_param_ErrorRecoveryLevel.attr,
645         NULL,
646 };
647
648 /* End items for lio_target_nacl_param_cit */
649
650 /* Start items for lio_target_acl_cit */
651
652 static ssize_t lio_target_nacl_show_info(
653         struct se_node_acl *se_nacl,
654         char *page)
655 {
656         struct iscsi_session *sess;
657         struct iscsi_conn *conn;
658         struct se_session *se_sess;
659         ssize_t rb = 0;
660
661         spin_lock_bh(&se_nacl->nacl_sess_lock);
662         se_sess = se_nacl->nacl_sess;
663         if (!se_sess) {
664                 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
665                         " Endpoint: %s\n", se_nacl->initiatorname);
666         } else {
667                 sess = se_sess->fabric_sess_ptr;
668
669                 if (sess->sess_ops->InitiatorName)
670                         rb += sprintf(page+rb, "InitiatorName: %s\n",
671                                 sess->sess_ops->InitiatorName);
672                 if (sess->sess_ops->InitiatorAlias)
673                         rb += sprintf(page+rb, "InitiatorAlias: %s\n",
674                                 sess->sess_ops->InitiatorAlias);
675
676                 rb += sprintf(page+rb, "LIO Session ID: %u   "
677                         "ISID: 0x%02x %02x %02x %02x %02x %02x  "
678                         "TSIH: %hu  ", sess->sid,
679                         sess->isid[0], sess->isid[1], sess->isid[2],
680                         sess->isid[3], sess->isid[4], sess->isid[5],
681                         sess->tsih);
682                 rb += sprintf(page+rb, "SessionType: %s\n",
683                                 (sess->sess_ops->SessionType) ?
684                                 "Discovery" : "Normal");
685                 rb += sprintf(page+rb, "Session State: ");
686                 switch (sess->session_state) {
687                 case TARG_SESS_STATE_FREE:
688                         rb += sprintf(page+rb, "TARG_SESS_FREE\n");
689                         break;
690                 case TARG_SESS_STATE_ACTIVE:
691                         rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
692                         break;
693                 case TARG_SESS_STATE_LOGGED_IN:
694                         rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
695                         break;
696                 case TARG_SESS_STATE_FAILED:
697                         rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
698                         break;
699                 case TARG_SESS_STATE_IN_CONTINUE:
700                         rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
701                         break;
702                 default:
703                         rb += sprintf(page+rb, "ERROR: Unknown Session"
704                                         " State!\n");
705                         break;
706                 }
707
708                 rb += sprintf(page+rb, "---------------------[iSCSI Session"
709                                 " Values]-----------------------\n");
710                 rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
711                                 "  :  MaxCmdSN  :     ITT    :     TTT\n");
712                 rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
713                                 "   0x%08x   0x%08x\n",
714                         sess->cmdsn_window,
715                         (sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
716                         sess->exp_cmd_sn, sess->max_cmd_sn,
717                         sess->init_task_tag, sess->targ_xfer_tag);
718                 rb += sprintf(page+rb, "----------------------[iSCSI"
719                                 " Connections]-------------------------\n");
720
721                 spin_lock(&sess->conn_lock);
722                 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
723                         rb += sprintf(page+rb, "CID: %hu  Connection"
724                                         " State: ", conn->cid);
725                         switch (conn->conn_state) {
726                         case TARG_CONN_STATE_FREE:
727                                 rb += sprintf(page+rb,
728                                         "TARG_CONN_STATE_FREE\n");
729                                 break;
730                         case TARG_CONN_STATE_XPT_UP:
731                                 rb += sprintf(page+rb,
732                                         "TARG_CONN_STATE_XPT_UP\n");
733                                 break;
734                         case TARG_CONN_STATE_IN_LOGIN:
735                                 rb += sprintf(page+rb,
736                                         "TARG_CONN_STATE_IN_LOGIN\n");
737                                 break;
738                         case TARG_CONN_STATE_LOGGED_IN:
739                                 rb += sprintf(page+rb,
740                                         "TARG_CONN_STATE_LOGGED_IN\n");
741                                 break;
742                         case TARG_CONN_STATE_IN_LOGOUT:
743                                 rb += sprintf(page+rb,
744                                         "TARG_CONN_STATE_IN_LOGOUT\n");
745                                 break;
746                         case TARG_CONN_STATE_LOGOUT_REQUESTED:
747                                 rb += sprintf(page+rb,
748                                         "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
749                                 break;
750                         case TARG_CONN_STATE_CLEANUP_WAIT:
751                                 rb += sprintf(page+rb,
752                                         "TARG_CONN_STATE_CLEANUP_WAIT\n");
753                                 break;
754                         default:
755                                 rb += sprintf(page+rb,
756                                         "ERROR: Unknown Connection State!\n");
757                                 break;
758                         }
759
760                         rb += sprintf(page+rb, "   Address %s %s", conn->login_ip,
761                                 (conn->network_transport == ISCSI_TCP) ?
762                                 "TCP" : "SCTP");
763                         rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
764                                 conn->stat_sn);
765                 }
766                 spin_unlock(&sess->conn_lock);
767         }
768         spin_unlock_bh(&se_nacl->nacl_sess_lock);
769
770         return rb;
771 }
772
773 TF_NACL_BASE_ATTR_RO(lio_target, info);
774
775 static ssize_t lio_target_nacl_show_cmdsn_depth(
776         struct se_node_acl *se_nacl,
777         char *page)
778 {
779         return sprintf(page, "%u\n", se_nacl->queue_depth);
780 }
781
782 static ssize_t lio_target_nacl_store_cmdsn_depth(
783         struct se_node_acl *se_nacl,
784         const char *page,
785         size_t count)
786 {
787         struct se_portal_group *se_tpg = se_nacl->se_tpg;
788         struct iscsi_portal_group *tpg = container_of(se_tpg,
789                         struct iscsi_portal_group, tpg_se_tpg);
790         struct config_item *acl_ci, *tpg_ci, *wwn_ci;
791         char *endptr;
792         u32 cmdsn_depth = 0;
793         int ret;
794
795         cmdsn_depth = simple_strtoul(page, &endptr, 0);
796         if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
797                 pr_err("Passed cmdsn_depth: %u exceeds"
798                         " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
799                         TA_DEFAULT_CMDSN_DEPTH_MAX);
800                 return -EINVAL;
801         }
802         acl_ci = &se_nacl->acl_group.cg_item;
803         if (!acl_ci) {
804                 pr_err("Unable to locatel acl_ci\n");
805                 return -EINVAL;
806         }
807         tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
808         if (!tpg_ci) {
809                 pr_err("Unable to locate tpg_ci\n");
810                 return -EINVAL;
811         }
812         wwn_ci = &tpg_ci->ci_group->cg_item;
813         if (!wwn_ci) {
814                 pr_err("Unable to locate config_item wwn_ci\n");
815                 return -EINVAL;
816         }
817
818         if (iscsit_get_tpg(tpg) < 0)
819                 return -EINVAL;
820         /*
821          * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
822          */
823         ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
824                                 config_item_name(acl_ci), cmdsn_depth, 1);
825
826         pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
827                 "InitiatorName: %s\n", config_item_name(wwn_ci),
828                 config_item_name(tpg_ci), cmdsn_depth,
829                 config_item_name(acl_ci));
830
831         iscsit_put_tpg(tpg);
832         return (!ret) ? count : (ssize_t)ret;
833 }
834
835 TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
836
837 static ssize_t lio_target_nacl_show_tag(
838         struct se_node_acl *se_nacl,
839         char *page)
840 {
841         return snprintf(page, PAGE_SIZE, "%s", se_nacl->acl_tag);
842 }
843
844 static ssize_t lio_target_nacl_store_tag(
845         struct se_node_acl *se_nacl,
846         const char *page,
847         size_t count)
848 {
849         int ret;
850
851         ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
852
853         if (ret < 0)
854                 return ret;
855         return count;
856 }
857
858 TF_NACL_BASE_ATTR(lio_target, tag, S_IRUGO | S_IWUSR);
859
860 static struct configfs_attribute *lio_target_initiator_attrs[] = {
861         &lio_target_nacl_info.attr,
862         &lio_target_nacl_cmdsn_depth.attr,
863         &lio_target_nacl_tag.attr,
864         NULL,
865 };
866
867 static struct se_node_acl *lio_tpg_alloc_fabric_acl(
868         struct se_portal_group *se_tpg)
869 {
870         struct iscsi_node_acl *acl;
871
872         acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
873         if (!acl) {
874                 pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
875                 return NULL;
876         }
877
878         return &acl->se_node_acl;
879 }
880
881 static struct se_node_acl *lio_target_make_nodeacl(
882         struct se_portal_group *se_tpg,
883         struct config_group *group,
884         const char *name)
885 {
886         struct config_group *stats_cg;
887         struct iscsi_node_acl *acl;
888         struct se_node_acl *se_nacl_new, *se_nacl;
889         struct iscsi_portal_group *tpg = container_of(se_tpg,
890                         struct iscsi_portal_group, tpg_se_tpg);
891         u32 cmdsn_depth;
892
893         se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
894         if (!se_nacl_new)
895                 return ERR_PTR(-ENOMEM);
896
897         cmdsn_depth = ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
898         /*
899          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
900          * when converting a NdoeACL from demo mode -> explict
901          */
902         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
903                                 name, cmdsn_depth);
904         if (IS_ERR(se_nacl))
905                 return se_nacl;
906
907         acl = container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
908         stats_cg = &se_nacl->acl_fabric_stat_group;
909
910         stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
911                                 GFP_KERNEL);
912         if (!stats_cg->default_groups) {
913                 pr_err("Unable to allocate memory for"
914                                 " stats_cg->default_groups\n");
915                 core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
916                 kfree(acl);
917                 return ERR_PTR(-ENOMEM);
918         }
919
920         stats_cg->default_groups[0] = &NODE_STAT_GRPS(acl)->iscsi_sess_stats_group;
921         stats_cg->default_groups[1] = NULL;
922         config_group_init_type_name(&NODE_STAT_GRPS(acl)->iscsi_sess_stats_group,
923                         "iscsi_sess_stats", &iscsi_stat_sess_cit);
924
925         return se_nacl;
926 }
927
928 static void lio_target_drop_nodeacl(
929         struct se_node_acl *se_nacl)
930 {
931         struct se_portal_group *se_tpg = se_nacl->se_tpg;
932         struct iscsi_node_acl *acl = container_of(se_nacl,
933                         struct iscsi_node_acl, se_node_acl);
934         struct config_item *df_item;
935         struct config_group *stats_cg;
936         int i;
937
938         stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
939         for (i = 0; stats_cg->default_groups[i]; i++) {
940                 df_item = &stats_cg->default_groups[i]->cg_item;
941                 stats_cg->default_groups[i] = NULL;
942                 config_item_put(df_item);
943         }
944         kfree(stats_cg->default_groups);
945
946         core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
947         kfree(acl);
948 }
949
950 /* End items for lio_target_acl_cit */
951
952 /* Start items for lio_target_tpg_attrib_cit */
953
954 #define DEF_TPG_ATTRIB(name)                                            \
955                                                                         \
956 static ssize_t iscsi_tpg_attrib_show_##name(                            \
957         struct se_portal_group *se_tpg,                         \
958         char *page)                                                     \
959 {                                                                       \
960         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
961                         struct iscsi_portal_group, tpg_se_tpg); \
962         ssize_t rb;                                                     \
963                                                                         \
964         if (iscsit_get_tpg(tpg) < 0)                                    \
965                 return -EINVAL;                                         \
966                                                                         \
967         rb = sprintf(page, "%u\n", ISCSI_TPG_ATTRIB(tpg)->name);        \
968         iscsit_put_tpg(tpg);                                            \
969         return rb;                                                      \
970 }                                                                       \
971                                                                         \
972 static ssize_t iscsi_tpg_attrib_store_##name(                           \
973         struct se_portal_group *se_tpg,                         \
974         const char *page,                                               \
975         size_t count)                                                   \
976 {                                                                       \
977         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
978                         struct iscsi_portal_group, tpg_se_tpg); \
979         char *endptr;                                                   \
980         u32 val;                                                        \
981         int ret;                                                        \
982                                                                         \
983         if (iscsit_get_tpg(tpg) < 0)                                    \
984                 return -EINVAL;                                         \
985                                                                         \
986         val = simple_strtoul(page, &endptr, 0);                         \
987         ret = iscsit_ta_##name(tpg, val);                               \
988         if (ret < 0)                                                    \
989                 goto out;                                               \
990                                                                         \
991         iscsit_put_tpg(tpg);                                            \
992         return count;                                                   \
993 out:                                                                    \
994         iscsit_put_tpg(tpg);                                            \
995         return ret;                                                     \
996 }
997
998 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
999
1000 /*
1001  * Define iscsi_tpg_attrib_s_authentication
1002  */
1003 DEF_TPG_ATTRIB(authentication);
1004 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
1005 /*
1006  * Define iscsi_tpg_attrib_s_login_timeout
1007  */
1008 DEF_TPG_ATTRIB(login_timeout);
1009 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
1010 /*
1011  * Define iscsi_tpg_attrib_s_netif_timeout
1012  */
1013 DEF_TPG_ATTRIB(netif_timeout);
1014 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
1015 /*
1016  * Define iscsi_tpg_attrib_s_generate_node_acls
1017  */
1018 DEF_TPG_ATTRIB(generate_node_acls);
1019 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
1020 /*
1021  * Define iscsi_tpg_attrib_s_default_cmdsn_depth
1022  */
1023 DEF_TPG_ATTRIB(default_cmdsn_depth);
1024 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
1025 /*
1026  Define iscsi_tpg_attrib_s_cache_dynamic_acls
1027  */
1028 DEF_TPG_ATTRIB(cache_dynamic_acls);
1029 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
1030 /*
1031  * Define iscsi_tpg_attrib_s_demo_mode_write_protect
1032  */
1033 DEF_TPG_ATTRIB(demo_mode_write_protect);
1034 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
1035 /*
1036  * Define iscsi_tpg_attrib_s_prod_mode_write_protect
1037  */
1038 DEF_TPG_ATTRIB(prod_mode_write_protect);
1039 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
1040
1041 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
1042         &iscsi_tpg_attrib_authentication.attr,
1043         &iscsi_tpg_attrib_login_timeout.attr,
1044         &iscsi_tpg_attrib_netif_timeout.attr,
1045         &iscsi_tpg_attrib_generate_node_acls.attr,
1046         &iscsi_tpg_attrib_default_cmdsn_depth.attr,
1047         &iscsi_tpg_attrib_cache_dynamic_acls.attr,
1048         &iscsi_tpg_attrib_demo_mode_write_protect.attr,
1049         &iscsi_tpg_attrib_prod_mode_write_protect.attr,
1050         NULL,
1051 };
1052
1053 /* End items for lio_target_tpg_attrib_cit */
1054
1055 /* Start items for lio_target_tpg_auth_cit */
1056
1057 #define __DEF_TPG_AUTH_STR(prefix, name, flags)                                 \
1058 static ssize_t __iscsi_##prefix##_show_##name(                                  \
1059         struct se_portal_group *se_tpg,                                         \
1060         char *page)                                                             \
1061 {                                                                               \
1062         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
1063                                 struct iscsi_portal_group, tpg_se_tpg);         \
1064         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
1065                                                                                 \
1066         if (!capable(CAP_SYS_ADMIN))                                            \
1067                 return -EPERM;                                                  \
1068                                                                                 \
1069         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);                   \
1070 }                                                                               \
1071                                                                                 \
1072 static ssize_t __iscsi_##prefix##_store_##name(                                 \
1073         struct se_portal_group *se_tpg,                                         \
1074         const char *page,                                                       \
1075         size_t count)                                                           \
1076 {                                                                               \
1077         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
1078                                 struct iscsi_portal_group, tpg_se_tpg);         \
1079         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
1080                                                                                 \
1081         if (!capable(CAP_SYS_ADMIN))                                            \
1082                 return -EPERM;                                                  \
1083                                                                                 \
1084         snprintf(auth->name, sizeof(auth->name), "%s", page);                   \
1085         if (!(strncmp("NULL", auth->name, 4)))                                  \
1086                 auth->naf_flags &= ~flags;                                      \
1087         else                                                                    \
1088                 auth->naf_flags |= flags;                                       \
1089                                                                                 \
1090         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                            \
1091             (auth->naf_flags & NAF_PASSWORD_IN_SET))                            \
1092                 auth->authenticate_target = 1;                                  \
1093         else                                                                    \
1094                 auth->authenticate_target = 0;                                  \
1095                                                                                 \
1096         return count;                                                           \
1097 }
1098
1099 #define __DEF_TPG_AUTH_INT(prefix, name)                                        \
1100 static ssize_t __iscsi_##prefix##_show_##name(                                  \
1101         struct se_portal_group *se_tpg,                                         \
1102         char *page)                                                             \
1103 {                                                                               \
1104         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
1105                                 struct iscsi_portal_group, tpg_se_tpg);         \
1106         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
1107                                                                                 \
1108         if (!capable(CAP_SYS_ADMIN))                                            \
1109                 return -EPERM;                                                  \
1110                                                                                 \
1111         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);                   \
1112 }
1113
1114 #define DEF_TPG_AUTH_STR(name, flags)                                           \
1115         __DEF_TPG_AUTH_STR(tpg_auth, name, flags)                               \
1116 static ssize_t iscsi_tpg_auth_show_##name(                                      \
1117         struct se_portal_group *se_tpg,                                         \
1118         char *page)                                                             \
1119 {                                                                               \
1120         return __iscsi_tpg_auth_show_##name(se_tpg, page);                      \
1121 }                                                                               \
1122                                                                                 \
1123 static ssize_t iscsi_tpg_auth_store_##name(                                     \
1124         struct se_portal_group *se_tpg,                                         \
1125         const char *page,                                                       \
1126         size_t count)                                                           \
1127 {                                                                               \
1128         return __iscsi_tpg_auth_store_##name(se_tpg, page, count);              \
1129 }
1130
1131 #define DEF_TPG_AUTH_INT(name)                                                  \
1132         __DEF_TPG_AUTH_INT(tpg_auth, name)                                      \
1133 static ssize_t iscsi_tpg_auth_show_##name(                                      \
1134         struct se_portal_group *se_tpg,                                         \
1135         char *page)                                                             \
1136 {                                                                               \
1137         return __iscsi_tpg_auth_show_##name(se_tpg, page);                      \
1138 }
1139
1140 #define TPG_AUTH_ATTR(_name, _mode) TF_TPG_AUTH_ATTR(iscsi, _name, _mode);
1141 #define TPG_AUTH_ATTR_RO(_name) TF_TPG_AUTH_ATTR_RO(iscsi, _name);
1142
1143 /*
1144  *  * One-way authentication userid
1145  *   */
1146 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
1147 TPG_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1148 /*
1149  *  * One-way authentication password
1150  *   */
1151 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
1152 TPG_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1153 /*
1154  *  * Enforce mutual authentication
1155  *   */
1156 DEF_TPG_AUTH_INT(authenticate_target);
1157 TPG_AUTH_ATTR_RO(authenticate_target);
1158 /*
1159  *  * Mutual authentication userid
1160  *   */
1161 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1162 TPG_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1163 /*
1164  *  * Mutual authentication password
1165  *   */
1166 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1167 TPG_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1168
1169 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
1170         &iscsi_tpg_auth_userid.attr,
1171         &iscsi_tpg_auth_password.attr,
1172         &iscsi_tpg_auth_authenticate_target.attr,
1173         &iscsi_tpg_auth_userid_mutual.attr,
1174         &iscsi_tpg_auth_password_mutual.attr,
1175         NULL,
1176 };
1177
1178 /* End items for lio_target_tpg_auth_cit */
1179
1180 /* Start items for lio_target_tpg_param_cit */
1181
1182 #define DEF_TPG_PARAM(name)                                             \
1183 static ssize_t iscsi_tpg_param_show_##name(                             \
1184         struct se_portal_group *se_tpg,                                 \
1185         char *page)                                                     \
1186 {                                                                       \
1187         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
1188                         struct iscsi_portal_group, tpg_se_tpg);         \
1189         struct iscsi_param *param;                                      \
1190         ssize_t rb;                                                     \
1191                                                                         \
1192         if (iscsit_get_tpg(tpg) < 0)                                    \
1193                 return -EINVAL;                                         \
1194                                                                         \
1195         param = iscsi_find_param_from_key(__stringify(name),            \
1196                                 tpg->param_list);                       \
1197         if (!param) {                                                   \
1198                 iscsit_put_tpg(tpg);                                    \
1199                 return -EINVAL;                                         \
1200         }                                                               \
1201         rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);           \
1202                                                                         \
1203         iscsit_put_tpg(tpg);                                            \
1204         return rb;                                                      \
1205 }                                                                       \
1206 static ssize_t iscsi_tpg_param_store_##name(                            \
1207         struct se_portal_group *se_tpg,                         \
1208         const char *page,                                               \
1209         size_t count)                                                   \
1210 {                                                                       \
1211         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
1212                         struct iscsi_portal_group, tpg_se_tpg);         \
1213         char *buf;                                                      \
1214         int ret;                                                        \
1215                                                                         \
1216         buf = kzalloc(PAGE_SIZE, GFP_KERNEL);                           \
1217         if (!buf)                                                       \
1218                 return -ENOMEM;                                         \
1219         snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);     \
1220         buf[strlen(buf)-1] = '\0'; /* Kill newline */                   \
1221                                                                         \
1222         if (iscsit_get_tpg(tpg) < 0) {                                  \
1223                 kfree(buf);                                             \
1224                 return -EINVAL;                                         \
1225         }                                                               \
1226                                                                         \
1227         ret = iscsi_change_param_value(buf, tpg->param_list, 1);        \
1228         if (ret < 0)                                                    \
1229                 goto out;                                               \
1230                                                                         \
1231         kfree(buf);                                                     \
1232         iscsit_put_tpg(tpg);                                            \
1233         return count;                                                   \
1234 out:                                                                    \
1235         kfree(buf);                                                     \
1236         iscsit_put_tpg(tpg);                                            \
1237         return -EINVAL;                                         \
1238 }
1239
1240 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1241
1242 DEF_TPG_PARAM(AuthMethod);
1243 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1244
1245 DEF_TPG_PARAM(HeaderDigest);
1246 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1247
1248 DEF_TPG_PARAM(DataDigest);
1249 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1250
1251 DEF_TPG_PARAM(MaxConnections);
1252 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1253
1254 DEF_TPG_PARAM(TargetAlias);
1255 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1256
1257 DEF_TPG_PARAM(InitialR2T);
1258 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1259
1260 DEF_TPG_PARAM(ImmediateData);
1261 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1262
1263 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1264 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1265
1266 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
1267 TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
1268
1269 DEF_TPG_PARAM(MaxBurstLength);
1270 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1271
1272 DEF_TPG_PARAM(FirstBurstLength);
1273 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1274
1275 DEF_TPG_PARAM(DefaultTime2Wait);
1276 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1277
1278 DEF_TPG_PARAM(DefaultTime2Retain);
1279 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1280
1281 DEF_TPG_PARAM(MaxOutstandingR2T);
1282 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1283
1284 DEF_TPG_PARAM(DataPDUInOrder);
1285 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1286
1287 DEF_TPG_PARAM(DataSequenceInOrder);
1288 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1289
1290 DEF_TPG_PARAM(ErrorRecoveryLevel);
1291 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1292
1293 DEF_TPG_PARAM(IFMarker);
1294 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1295
1296 DEF_TPG_PARAM(OFMarker);
1297 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1298
1299 DEF_TPG_PARAM(IFMarkInt);
1300 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1301
1302 DEF_TPG_PARAM(OFMarkInt);
1303 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1304
1305 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1306         &iscsi_tpg_param_AuthMethod.attr,
1307         &iscsi_tpg_param_HeaderDigest.attr,
1308         &iscsi_tpg_param_DataDigest.attr,
1309         &iscsi_tpg_param_MaxConnections.attr,
1310         &iscsi_tpg_param_TargetAlias.attr,
1311         &iscsi_tpg_param_InitialR2T.attr,
1312         &iscsi_tpg_param_ImmediateData.attr,
1313         &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
1314         &iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
1315         &iscsi_tpg_param_MaxBurstLength.attr,
1316         &iscsi_tpg_param_FirstBurstLength.attr,
1317         &iscsi_tpg_param_DefaultTime2Wait.attr,
1318         &iscsi_tpg_param_DefaultTime2Retain.attr,
1319         &iscsi_tpg_param_MaxOutstandingR2T.attr,
1320         &iscsi_tpg_param_DataPDUInOrder.attr,
1321         &iscsi_tpg_param_DataSequenceInOrder.attr,
1322         &iscsi_tpg_param_ErrorRecoveryLevel.attr,
1323         &iscsi_tpg_param_IFMarker.attr,
1324         &iscsi_tpg_param_OFMarker.attr,
1325         &iscsi_tpg_param_IFMarkInt.attr,
1326         &iscsi_tpg_param_OFMarkInt.attr,
1327         NULL,
1328 };
1329
1330 /* End items for lio_target_tpg_param_cit */
1331
1332 /* Start items for lio_target_tpg_cit */
1333
1334 static ssize_t lio_target_tpg_show_enable(
1335         struct se_portal_group *se_tpg,
1336         char *page)
1337 {
1338         struct iscsi_portal_group *tpg = container_of(se_tpg,
1339                         struct iscsi_portal_group, tpg_se_tpg);
1340         ssize_t len;
1341
1342         spin_lock(&tpg->tpg_state_lock);
1343         len = sprintf(page, "%d\n",
1344                         (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1345         spin_unlock(&tpg->tpg_state_lock);
1346
1347         return len;
1348 }
1349
1350 static ssize_t lio_target_tpg_store_enable(
1351         struct se_portal_group *se_tpg,
1352         const char *page,
1353         size_t count)
1354 {
1355         struct iscsi_portal_group *tpg = container_of(se_tpg,
1356                         struct iscsi_portal_group, tpg_se_tpg);
1357         char *endptr;
1358         u32 op;
1359         int ret = 0;
1360
1361         op = simple_strtoul(page, &endptr, 0);
1362         if ((op != 1) && (op != 0)) {
1363                 pr_err("Illegal value for tpg_enable: %u\n", op);
1364                 return -EINVAL;
1365         }
1366
1367         ret = iscsit_get_tpg(tpg);
1368         if (ret < 0)
1369                 return -EINVAL;
1370
1371         if (op) {
1372                 ret = iscsit_tpg_enable_portal_group(tpg);
1373                 if (ret < 0)
1374                         goto out;
1375         } else {
1376                 /*
1377                  * iscsit_tpg_disable_portal_group() assumes force=1
1378                  */
1379                 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1380                 if (ret < 0)
1381                         goto out;
1382         }
1383
1384         iscsit_put_tpg(tpg);
1385         return count;
1386 out:
1387         iscsit_put_tpg(tpg);
1388         return -EINVAL;
1389 }
1390
1391 TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
1392
1393 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1394         &lio_target_tpg_enable.attr,
1395         NULL,
1396 };
1397
1398 /* End items for lio_target_tpg_cit */
1399
1400 /* Start items for lio_target_tiqn_cit */
1401
1402 static struct se_portal_group *lio_target_tiqn_addtpg(
1403         struct se_wwn *wwn,
1404         struct config_group *group,
1405         const char *name)
1406 {
1407         struct iscsi_portal_group *tpg;
1408         struct iscsi_tiqn *tiqn;
1409         char *tpgt_str, *end_ptr;
1410         int ret = 0;
1411         unsigned short int tpgt;
1412
1413         tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1414         /*
1415          * Only tpgt_# directory groups can be created below
1416          * target/iscsi/iqn.superturodiskarry/
1417         */
1418         tpgt_str = strstr(name, "tpgt_");
1419         if (!tpgt_str) {
1420                 pr_err("Unable to locate \"tpgt_#\" directory"
1421                                 " group\n");
1422                 return NULL;
1423         }
1424         tpgt_str += 5; /* Skip ahead of "tpgt_" */
1425         tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1426
1427         tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1428         if (!tpg)
1429                 return NULL;
1430
1431         ret = core_tpg_register(
1432                         &lio_target_fabric_configfs->tf_ops,
1433                         wwn, &tpg->tpg_se_tpg, tpg,
1434                         TRANSPORT_TPG_TYPE_NORMAL);
1435         if (ret < 0)
1436                 return NULL;
1437
1438         ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1439         if (ret != 0)
1440                 goto out;
1441
1442         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1443         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1444                         name);
1445         return &tpg->tpg_se_tpg;
1446 out:
1447         core_tpg_deregister(&tpg->tpg_se_tpg);
1448         kfree(tpg);
1449         return NULL;
1450 }
1451
1452 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1453 {
1454         struct iscsi_portal_group *tpg;
1455         struct iscsi_tiqn *tiqn;
1456
1457         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1458         tiqn = tpg->tpg_tiqn;
1459         /*
1460          * iscsit_tpg_del_portal_group() assumes force=1
1461          */
1462         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1463         iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1464 }
1465
1466 /* End items for lio_target_tiqn_cit */
1467
1468 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1469
1470 static ssize_t lio_target_wwn_show_attr_lio_version(
1471         struct target_fabric_configfs *tf,
1472         char *page)
1473 {
1474         return sprintf(page, "RisingTide Systems Linux-iSCSI Target "ISCSIT_VERSION"\n");
1475 }
1476
1477 TF_WWN_ATTR_RO(lio_target, lio_version);
1478
1479 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1480         &lio_target_wwn_lio_version.attr,
1481         NULL,
1482 };
1483
1484 static struct se_wwn *lio_target_call_coreaddtiqn(
1485         struct target_fabric_configfs *tf,
1486         struct config_group *group,
1487         const char *name)
1488 {
1489         struct config_group *stats_cg;
1490         struct iscsi_tiqn *tiqn;
1491
1492         tiqn = iscsit_add_tiqn((unsigned char *)name);
1493         if (IS_ERR(tiqn))
1494                 return ERR_CAST(tiqn);
1495         /*
1496          * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
1497          */
1498         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1499
1500         stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
1501                                 GFP_KERNEL);
1502         if (!stats_cg->default_groups) {
1503                 pr_err("Unable to allocate memory for"
1504                                 " stats_cg->default_groups\n");
1505                 iscsit_del_tiqn(tiqn);
1506                 return ERR_PTR(-ENOMEM);
1507         }
1508
1509         stats_cg->default_groups[0] = &WWN_STAT_GRPS(tiqn)->iscsi_instance_group;
1510         stats_cg->default_groups[1] = &WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group;
1511         stats_cg->default_groups[2] = &WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group;
1512         stats_cg->default_groups[3] = &WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group;
1513         stats_cg->default_groups[4] = &WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group;
1514         stats_cg->default_groups[5] = NULL;
1515         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_instance_group,
1516                         "iscsi_instance", &iscsi_stat_instance_cit);
1517         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group,
1518                         "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1519         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group,
1520                         "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1521         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group,
1522                         "iscsi_login_stats", &iscsi_stat_login_cit);
1523         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group,
1524                         "iscsi_logout_stats", &iscsi_stat_logout_cit);
1525
1526         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1527         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1528                         " %s\n", name);
1529         return &tiqn->tiqn_wwn;
1530 }
1531
1532 static void lio_target_call_coredeltiqn(
1533         struct se_wwn *wwn)
1534 {
1535         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1536         struct config_item *df_item;
1537         struct config_group *stats_cg;
1538         int i;
1539
1540         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1541         for (i = 0; stats_cg->default_groups[i]; i++) {
1542                 df_item = &stats_cg->default_groups[i]->cg_item;
1543                 stats_cg->default_groups[i] = NULL;
1544                 config_item_put(df_item);
1545         }
1546         kfree(stats_cg->default_groups);
1547
1548         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1549                         tiqn->tiqn);
1550         iscsit_del_tiqn(tiqn);
1551 }
1552
1553 /* End LIO-Target TIQN struct contig_lio_target_cit */
1554
1555 /* Start lio_target_discovery_auth_cit */
1556
1557 #define DEF_DISC_AUTH_STR(name, flags)                                  \
1558         __DEF_NACL_AUTH_STR(disc, name, flags)                          \
1559 static ssize_t iscsi_disc_show_##name(                                  \
1560         struct target_fabric_configfs *tf,                              \
1561         char *page)                                                     \
1562 {                                                                       \
1563         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1564                 page);                                                  \
1565 }                                                                       \
1566 static ssize_t iscsi_disc_store_##name(                                 \
1567         struct target_fabric_configfs *tf,                              \
1568         const char *page,                                               \
1569         size_t count)                                                   \
1570 {                                                                       \
1571         return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
1572                 page, count);                                           \
1573 }
1574
1575 #define DEF_DISC_AUTH_INT(name)                                         \
1576         __DEF_NACL_AUTH_INT(disc, name)                                 \
1577 static ssize_t iscsi_disc_show_##name(                                  \
1578         struct target_fabric_configfs *tf,                              \
1579         char *page)                                                     \
1580 {                                                                       \
1581         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1582                         page);                                          \
1583 }
1584
1585 #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
1586 #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
1587
1588 /*
1589  * One-way authentication userid
1590  */
1591 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1592 DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1593 /*
1594  * One-way authentication password
1595  */
1596 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1597 DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1598 /*
1599  * Enforce mutual authentication
1600  */
1601 DEF_DISC_AUTH_INT(authenticate_target);
1602 DISC_AUTH_ATTR_RO(authenticate_target);
1603 /*
1604  * Mutual authentication userid
1605  */
1606 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1607 DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1608 /*
1609  * Mutual authentication password
1610  */
1611 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1612 DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1613
1614 /*
1615  * enforce_discovery_auth
1616  */
1617 static ssize_t iscsi_disc_show_enforce_discovery_auth(
1618         struct target_fabric_configfs *tf,
1619         char *page)
1620 {
1621         struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1622
1623         return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1624 }
1625
1626 static ssize_t iscsi_disc_store_enforce_discovery_auth(
1627         struct target_fabric_configfs *tf,
1628         const char *page,
1629         size_t count)
1630 {
1631         struct iscsi_param *param;
1632         struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1633         char *endptr;
1634         u32 op;
1635
1636         op = simple_strtoul(page, &endptr, 0);
1637         if ((op != 1) && (op != 0)) {
1638                 pr_err("Illegal value for enforce_discovery_auth:"
1639                                 " %u\n", op);
1640                 return -EINVAL;
1641         }
1642
1643         if (!discovery_tpg) {
1644                 pr_err("iscsit_global->discovery_tpg is NULL\n");
1645                 return -EINVAL;
1646         }
1647
1648         param = iscsi_find_param_from_key(AUTHMETHOD,
1649                                 discovery_tpg->param_list);
1650         if (!param)
1651                 return -EINVAL;
1652
1653         if (op) {
1654                 /*
1655                  * Reset the AuthMethod key to CHAP.
1656                  */
1657                 if (iscsi_update_param_value(param, CHAP) < 0)
1658                         return -EINVAL;
1659
1660                 discovery_tpg->tpg_attrib.authentication = 1;
1661                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1662                 pr_debug("LIO-CORE[0] Successfully enabled"
1663                         " authentication enforcement for iSCSI"
1664                         " Discovery TPG\n");
1665         } else {
1666                 /*
1667                  * Reset the AuthMethod key to CHAP,None
1668                  */
1669                 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1670                         return -EINVAL;
1671
1672                 discovery_tpg->tpg_attrib.authentication = 0;
1673                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1674                 pr_debug("LIO-CORE[0] Successfully disabled"
1675                         " authentication enforcement for iSCSI"
1676                         " Discovery TPG\n");
1677         }
1678
1679         return count;
1680 }
1681
1682 DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
1683
1684 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1685         &iscsi_disc_userid.attr,
1686         &iscsi_disc_password.attr,
1687         &iscsi_disc_authenticate_target.attr,
1688         &iscsi_disc_userid_mutual.attr,
1689         &iscsi_disc_password_mutual.attr,
1690         &iscsi_disc_enforce_discovery_auth.attr,
1691         NULL,
1692 };
1693
1694 /* End lio_target_discovery_auth_cit */
1695
1696 /* Start functions for target_core_fabric_ops */
1697
1698 static char *iscsi_get_fabric_name(void)
1699 {
1700         return "iSCSI";
1701 }
1702
1703 static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
1704 {
1705         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1706
1707         /* only used for printks or comparism with ->ref_task_tag */
1708         return (__force u32)cmd->init_task_tag;
1709 }
1710
1711 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1712 {
1713         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1714
1715         return cmd->i_state;
1716 }
1717
1718 static u32 lio_sess_get_index(struct se_session *se_sess)
1719 {
1720         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1721
1722         return sess->session_index;
1723 }
1724
1725 static u32 lio_sess_get_initiator_sid(
1726         struct se_session *se_sess,
1727         unsigned char *buf,
1728         u32 size)
1729 {
1730         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1731         /*
1732          * iSCSI Initiator Session Identifier from RFC-3720.
1733          */
1734         return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
1735                 sess->isid[0], sess->isid[1], sess->isid[2],
1736                 sess->isid[3], sess->isid[4], sess->isid[5]);
1737 }
1738
1739 static int lio_queue_data_in(struct se_cmd *se_cmd)
1740 {
1741         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1742
1743         cmd->i_state = ISTATE_SEND_DATAIN;
1744         cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
1745
1746         return 0;
1747 }
1748
1749 static int lio_write_pending(struct se_cmd *se_cmd)
1750 {
1751         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1752         struct iscsi_conn *conn = cmd->conn;
1753
1754         if (!cmd->immediate_data && !cmd->unsolicited_data)
1755                 return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1756
1757         return 0;
1758 }
1759
1760 static int lio_write_pending_status(struct se_cmd *se_cmd)
1761 {
1762         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1763         int ret;
1764
1765         spin_lock_bh(&cmd->istate_lock);
1766         ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1767         spin_unlock_bh(&cmd->istate_lock);
1768
1769         return ret;
1770 }
1771
1772 static int lio_queue_status(struct se_cmd *se_cmd)
1773 {
1774         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1775
1776         cmd->i_state = ISTATE_SEND_STATUS;
1777         cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
1778
1779         return 0;
1780 }
1781
1782 static int lio_queue_tm_rsp(struct se_cmd *se_cmd)
1783 {
1784         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1785
1786         cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1787         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1788         return 0;
1789 }
1790
1791 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1792 {
1793         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1794
1795         return &tpg->tpg_tiqn->tiqn[0];
1796 }
1797
1798 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1799 {
1800         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1801
1802         return tpg->tpgt;
1803 }
1804
1805 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1806 {
1807         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1808
1809         return ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
1810 }
1811
1812 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1813 {
1814         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1815
1816         return ISCSI_TPG_ATTRIB(tpg)->generate_node_acls;
1817 }
1818
1819 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1820 {
1821         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1822
1823         return ISCSI_TPG_ATTRIB(tpg)->cache_dynamic_acls;
1824 }
1825
1826 static int lio_tpg_check_demo_mode_write_protect(
1827         struct se_portal_group *se_tpg)
1828 {
1829         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1830
1831         return ISCSI_TPG_ATTRIB(tpg)->demo_mode_write_protect;
1832 }
1833
1834 static int lio_tpg_check_prod_mode_write_protect(
1835         struct se_portal_group *se_tpg)
1836 {
1837         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1838
1839         return ISCSI_TPG_ATTRIB(tpg)->prod_mode_write_protect;
1840 }
1841
1842 static void lio_tpg_release_fabric_acl(
1843         struct se_portal_group *se_tpg,
1844         struct se_node_acl *se_acl)
1845 {
1846         struct iscsi_node_acl *acl = container_of(se_acl,
1847                                 struct iscsi_node_acl, se_node_acl);
1848         kfree(acl);
1849 }
1850
1851 /*
1852  * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
1853  *
1854  * Also, this function calls iscsit_inc_session_usage_count() on the
1855  * struct iscsi_session in question.
1856  */
1857 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1858 {
1859         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1860
1861         spin_lock(&sess->conn_lock);
1862         if (atomic_read(&sess->session_fall_back_to_erl0) ||
1863             atomic_read(&sess->session_logout) ||
1864             (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1865                 spin_unlock(&sess->conn_lock);
1866                 return 0;
1867         }
1868         atomic_set(&sess->session_reinstatement, 1);
1869         spin_unlock(&sess->conn_lock);
1870
1871         iscsit_stop_time2retain_timer(sess);
1872         iscsit_stop_session(sess, 1, 1);
1873
1874         return 1;
1875 }
1876
1877 /*
1878  * Calls iscsit_dec_session_usage_count() as inverse of
1879  * lio_tpg_shutdown_session()
1880  */
1881 static void lio_tpg_close_session(struct se_session *se_sess)
1882 {
1883         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1884         /*
1885          * If the iSCSI Session for the iSCSI Initiator Node exists,
1886          * forcefully shutdown the iSCSI NEXUS.
1887          */
1888         iscsit_close_session(sess);
1889 }
1890
1891 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1892 {
1893         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1894
1895         return tpg->tpg_tiqn->tiqn_index;
1896 }
1897
1898 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1899 {
1900         struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1901                                 se_node_acl);
1902
1903         ISCSI_NODE_ATTRIB(acl)->nacl = acl;
1904         iscsit_set_default_node_attribues(acl);
1905 }
1906
1907 static int lio_check_stop_free(struct se_cmd *se_cmd)
1908 {
1909         return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1910 }
1911
1912 static void lio_release_cmd(struct se_cmd *se_cmd)
1913 {
1914         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1915
1916         pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1917         cmd->release_cmd(cmd);
1918 }
1919
1920 /* End functions for target_core_fabric_ops */
1921
1922 int iscsi_target_register_configfs(void)
1923 {
1924         struct target_fabric_configfs *fabric;
1925         int ret;
1926
1927         lio_target_fabric_configfs = NULL;
1928         fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
1929         if (IS_ERR(fabric)) {
1930                 pr_err("target_fabric_configfs_init() for"
1931                                 " LIO-Target failed!\n");
1932                 return PTR_ERR(fabric);
1933         }
1934         /*
1935          * Setup the fabric API of function pointers used by target_core_mod..
1936          */
1937         fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
1938         fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
1939         fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
1940         fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
1941         fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
1942         fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
1943         fabric->tf_ops.tpg_get_pr_transport_id_len =
1944                                 &iscsi_get_pr_transport_id_len;
1945         fabric->tf_ops.tpg_parse_pr_out_transport_id =
1946                                 &iscsi_parse_pr_out_transport_id;
1947         fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
1948         fabric->tf_ops.tpg_check_demo_mode_cache =
1949                                 &lio_tpg_check_demo_mode_cache;
1950         fabric->tf_ops.tpg_check_demo_mode_write_protect =
1951                                 &lio_tpg_check_demo_mode_write_protect;
1952         fabric->tf_ops.tpg_check_prod_mode_write_protect =
1953                                 &lio_tpg_check_prod_mode_write_protect;
1954         fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
1955         fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
1956         fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
1957         fabric->tf_ops.check_stop_free = &lio_check_stop_free,
1958         fabric->tf_ops.release_cmd = &lio_release_cmd;
1959         fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
1960         fabric->tf_ops.close_session = &lio_tpg_close_session;
1961         fabric->tf_ops.sess_get_index = &lio_sess_get_index;
1962         fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
1963         fabric->tf_ops.write_pending = &lio_write_pending;
1964         fabric->tf_ops.write_pending_status = &lio_write_pending_status;
1965         fabric->tf_ops.set_default_node_attributes =
1966                                 &lio_set_default_node_attributes;
1967         fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
1968         fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
1969         fabric->tf_ops.queue_data_in = &lio_queue_data_in;
1970         fabric->tf_ops.queue_status = &lio_queue_status;
1971         fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
1972         /*
1973          * Setup function pointers for generic logic in target_core_fabric_configfs.c
1974          */
1975         fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
1976         fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
1977         fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
1978         fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
1979         fabric->tf_ops.fabric_post_link = NULL;
1980         fabric->tf_ops.fabric_pre_unlink = NULL;
1981         fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
1982         fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
1983         fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
1984         fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
1985         /*
1986          * Setup default attribute lists for various fabric->tf_cit_tmpl
1987          * sturct config_item_type's
1988          */
1989         TF_CIT_TMPL(fabric)->tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
1990         TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
1991         TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
1992         TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
1993         TF_CIT_TMPL(fabric)->tfc_tpg_auth_cit.ct_attrs = lio_target_tpg_auth_attrs;
1994         TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
1995         TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
1996         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
1997         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
1998         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
1999         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
2000
2001         ret = target_fabric_configfs_register(fabric);
2002         if (ret < 0) {
2003                 pr_err("target_fabric_configfs_register() for"
2004                                 " LIO-Target failed!\n");
2005                 target_fabric_configfs_free(fabric);
2006                 return ret;
2007         }
2008
2009         lio_target_fabric_configfs = fabric;
2010         pr_debug("LIO_TARGET[0] - Set fabric ->"
2011                         " lio_target_fabric_configfs\n");
2012         return 0;
2013 }
2014
2015
2016 void iscsi_target_deregister_configfs(void)
2017 {
2018         if (!lio_target_fabric_configfs)
2019                 return;
2020         /*
2021          * Shutdown discovery sessions and disable discovery TPG
2022          */
2023         if (iscsit_global->discovery_tpg)
2024                 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
2025
2026         target_fabric_configfs_deregister(lio_target_fabric_configfs);
2027         lio_target_fabric_configfs = NULL;
2028         pr_debug("LIO_TARGET[0] - Cleared"
2029                                 " lio_target_fabric_configfs\n");
2030 }