iscsi_target_configfs.c 53 KB

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