iscsi_target_configfs.c 56 KB

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