iscsi_target_configfs.c 56 KB

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