iscsi_target_parameters.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. /*******************************************************************************
  2. * This file contains main functions related to iSCSI Parameter negotiation.
  3. *
  4. * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
  5. *
  6. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  7. *
  8. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. ******************************************************************************/
  20. #include <linux/slab.h>
  21. #include "iscsi_target_core.h"
  22. #include "iscsi_target_util.h"
  23. #include "iscsi_target_parameters.h"
  24. int iscsi_login_rx_data(
  25. struct iscsi_conn *conn,
  26. char *buf,
  27. int length)
  28. {
  29. int rx_got;
  30. struct kvec iov;
  31. memset(&iov, 0, sizeof(struct kvec));
  32. iov.iov_len = length;
  33. iov.iov_base = buf;
  34. /*
  35. * Initial Marker-less Interval.
  36. * Add the values regardless of IFMarker/OFMarker, considering
  37. * it may not be negoitated yet.
  38. */
  39. conn->of_marker += length;
  40. rx_got = rx_data(conn, &iov, 1, length);
  41. if (rx_got != length) {
  42. pr_err("rx_data returned %d, expecting %d.\n",
  43. rx_got, length);
  44. return -1;
  45. }
  46. return 0 ;
  47. }
  48. int iscsi_login_tx_data(
  49. struct iscsi_conn *conn,
  50. char *pdu_buf,
  51. char *text_buf,
  52. int text_length)
  53. {
  54. int length, tx_sent, iov_cnt = 1;
  55. struct kvec iov[2];
  56. length = (ISCSI_HDR_LEN + text_length);
  57. memset(&iov[0], 0, 2 * sizeof(struct kvec));
  58. iov[0].iov_len = ISCSI_HDR_LEN;
  59. iov[0].iov_base = pdu_buf;
  60. if (text_buf && text_length) {
  61. iov[1].iov_len = text_length;
  62. iov[1].iov_base = text_buf;
  63. iov_cnt++;
  64. }
  65. /*
  66. * Initial Marker-less Interval.
  67. * Add the values regardless of IFMarker/OFMarker, considering
  68. * it may not be negoitated yet.
  69. */
  70. conn->if_marker += length;
  71. tx_sent = tx_data(conn, &iov[0], iov_cnt, length);
  72. if (tx_sent != length) {
  73. pr_err("tx_data returned %d, expecting %d.\n",
  74. tx_sent, length);
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. void iscsi_dump_conn_ops(struct iscsi_conn_ops *conn_ops)
  80. {
  81. pr_debug("HeaderDigest: %s\n", (conn_ops->HeaderDigest) ?
  82. "CRC32C" : "None");
  83. pr_debug("DataDigest: %s\n", (conn_ops->DataDigest) ?
  84. "CRC32C" : "None");
  85. pr_debug("MaxRecvDataSegmentLength: %u\n",
  86. conn_ops->MaxRecvDataSegmentLength);
  87. pr_debug("OFMarker: %s\n", (conn_ops->OFMarker) ? "Yes" : "No");
  88. pr_debug("IFMarker: %s\n", (conn_ops->IFMarker) ? "Yes" : "No");
  89. if (conn_ops->OFMarker)
  90. pr_debug("OFMarkInt: %u\n", conn_ops->OFMarkInt);
  91. if (conn_ops->IFMarker)
  92. pr_debug("IFMarkInt: %u\n", conn_ops->IFMarkInt);
  93. }
  94. void iscsi_dump_sess_ops(struct iscsi_sess_ops *sess_ops)
  95. {
  96. pr_debug("InitiatorName: %s\n", sess_ops->InitiatorName);
  97. pr_debug("InitiatorAlias: %s\n", sess_ops->InitiatorAlias);
  98. pr_debug("TargetName: %s\n", sess_ops->TargetName);
  99. pr_debug("TargetAlias: %s\n", sess_ops->TargetAlias);
  100. pr_debug("TargetPortalGroupTag: %hu\n",
  101. sess_ops->TargetPortalGroupTag);
  102. pr_debug("MaxConnections: %hu\n", sess_ops->MaxConnections);
  103. pr_debug("InitialR2T: %s\n",
  104. (sess_ops->InitialR2T) ? "Yes" : "No");
  105. pr_debug("ImmediateData: %s\n", (sess_ops->ImmediateData) ?
  106. "Yes" : "No");
  107. pr_debug("MaxBurstLength: %u\n", sess_ops->MaxBurstLength);
  108. pr_debug("FirstBurstLength: %u\n", sess_ops->FirstBurstLength);
  109. pr_debug("DefaultTime2Wait: %hu\n", sess_ops->DefaultTime2Wait);
  110. pr_debug("DefaultTime2Retain: %hu\n",
  111. sess_ops->DefaultTime2Retain);
  112. pr_debug("MaxOutstandingR2T: %hu\n",
  113. sess_ops->MaxOutstandingR2T);
  114. pr_debug("DataPDUInOrder: %s\n",
  115. (sess_ops->DataPDUInOrder) ? "Yes" : "No");
  116. pr_debug("DataSequenceInOrder: %s\n",
  117. (sess_ops->DataSequenceInOrder) ? "Yes" : "No");
  118. pr_debug("ErrorRecoveryLevel: %hu\n",
  119. sess_ops->ErrorRecoveryLevel);
  120. pr_debug("SessionType: %s\n", (sess_ops->SessionType) ?
  121. "Discovery" : "Normal");
  122. }
  123. void iscsi_print_params(struct iscsi_param_list *param_list)
  124. {
  125. struct iscsi_param *param;
  126. list_for_each_entry(param, &param_list->param_list, p_list)
  127. pr_debug("%s: %s\n", param->name, param->value);
  128. }
  129. static struct iscsi_param *iscsi_set_default_param(struct iscsi_param_list *param_list,
  130. char *name, char *value, u8 phase, u8 scope, u8 sender,
  131. u16 type_range, u8 use)
  132. {
  133. struct iscsi_param *param = NULL;
  134. param = kzalloc(sizeof(struct iscsi_param), GFP_KERNEL);
  135. if (!param) {
  136. pr_err("Unable to allocate memory for parameter.\n");
  137. goto out;
  138. }
  139. INIT_LIST_HEAD(&param->p_list);
  140. param->name = kstrdup(name, GFP_KERNEL);
  141. if (!param->name) {
  142. pr_err("Unable to allocate memory for parameter name.\n");
  143. goto out;
  144. }
  145. param->value = kstrdup(value, GFP_KERNEL);
  146. if (!param->value) {
  147. pr_err("Unable to allocate memory for parameter value.\n");
  148. goto out;
  149. }
  150. param->phase = phase;
  151. param->scope = scope;
  152. param->sender = sender;
  153. param->use = use;
  154. param->type_range = type_range;
  155. switch (param->type_range) {
  156. case TYPERANGE_BOOL_AND:
  157. param->type = TYPE_BOOL_AND;
  158. break;
  159. case TYPERANGE_BOOL_OR:
  160. param->type = TYPE_BOOL_OR;
  161. break;
  162. case TYPERANGE_0_TO_2:
  163. case TYPERANGE_0_TO_3600:
  164. case TYPERANGE_0_TO_32767:
  165. case TYPERANGE_0_TO_65535:
  166. case TYPERANGE_1_TO_65535:
  167. case TYPERANGE_2_TO_3600:
  168. case TYPERANGE_512_TO_16777215:
  169. param->type = TYPE_NUMBER;
  170. break;
  171. case TYPERANGE_AUTH:
  172. case TYPERANGE_DIGEST:
  173. param->type = TYPE_VALUE_LIST | TYPE_STRING;
  174. break;
  175. case TYPERANGE_MARKINT:
  176. param->type = TYPE_NUMBER_RANGE;
  177. param->type_range |= TYPERANGE_1_TO_65535;
  178. break;
  179. case TYPERANGE_ISCSINAME:
  180. case TYPERANGE_SESSIONTYPE:
  181. case TYPERANGE_TARGETADDRESS:
  182. case TYPERANGE_UTF8:
  183. param->type = TYPE_STRING;
  184. break;
  185. default:
  186. pr_err("Unknown type_range 0x%02x\n",
  187. param->type_range);
  188. goto out;
  189. }
  190. list_add_tail(&param->p_list, &param_list->param_list);
  191. return param;
  192. out:
  193. if (param) {
  194. kfree(param->value);
  195. kfree(param->name);
  196. kfree(param);
  197. }
  198. return NULL;
  199. }
  200. /* #warning Add extension keys */
  201. int iscsi_create_default_params(struct iscsi_param_list **param_list_ptr)
  202. {
  203. struct iscsi_param *param = NULL;
  204. struct iscsi_param_list *pl;
  205. pl = kzalloc(sizeof(struct iscsi_param_list), GFP_KERNEL);
  206. if (!pl) {
  207. pr_err("Unable to allocate memory for"
  208. " struct iscsi_param_list.\n");
  209. return -1 ;
  210. }
  211. INIT_LIST_HEAD(&pl->param_list);
  212. INIT_LIST_HEAD(&pl->extra_response_list);
  213. /*
  214. * The format for setting the initial parameter definitions are:
  215. *
  216. * Parameter name:
  217. * Initial value:
  218. * Allowable phase:
  219. * Scope:
  220. * Allowable senders:
  221. * Typerange:
  222. * Use:
  223. */
  224. param = iscsi_set_default_param(pl, AUTHMETHOD, INITIAL_AUTHMETHOD,
  225. PHASE_SECURITY, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  226. TYPERANGE_AUTH, USE_INITIAL_ONLY);
  227. if (!param)
  228. goto out;
  229. param = iscsi_set_default_param(pl, HEADERDIGEST, INITIAL_HEADERDIGEST,
  230. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  231. TYPERANGE_DIGEST, USE_INITIAL_ONLY);
  232. if (!param)
  233. goto out;
  234. param = iscsi_set_default_param(pl, DATADIGEST, INITIAL_DATADIGEST,
  235. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  236. TYPERANGE_DIGEST, USE_INITIAL_ONLY);
  237. if (!param)
  238. goto out;
  239. param = iscsi_set_default_param(pl, MAXCONNECTIONS,
  240. INITIAL_MAXCONNECTIONS, PHASE_OPERATIONAL,
  241. SCOPE_SESSION_WIDE, SENDER_BOTH,
  242. TYPERANGE_1_TO_65535, USE_LEADING_ONLY);
  243. if (!param)
  244. goto out;
  245. param = iscsi_set_default_param(pl, SENDTARGETS, INITIAL_SENDTARGETS,
  246. PHASE_FFP0, SCOPE_SESSION_WIDE, SENDER_INITIATOR,
  247. TYPERANGE_UTF8, 0);
  248. if (!param)
  249. goto out;
  250. param = iscsi_set_default_param(pl, TARGETNAME, INITIAL_TARGETNAME,
  251. PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_BOTH,
  252. TYPERANGE_ISCSINAME, USE_ALL);
  253. if (!param)
  254. goto out;
  255. param = iscsi_set_default_param(pl, INITIATORNAME,
  256. INITIAL_INITIATORNAME, PHASE_DECLARATIVE,
  257. SCOPE_SESSION_WIDE, SENDER_INITIATOR,
  258. TYPERANGE_ISCSINAME, USE_INITIAL_ONLY);
  259. if (!param)
  260. goto out;
  261. param = iscsi_set_default_param(pl, TARGETALIAS, INITIAL_TARGETALIAS,
  262. PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_TARGET,
  263. TYPERANGE_UTF8, USE_ALL);
  264. if (!param)
  265. goto out;
  266. param = iscsi_set_default_param(pl, INITIATORALIAS,
  267. INITIAL_INITIATORALIAS, PHASE_DECLARATIVE,
  268. SCOPE_SESSION_WIDE, SENDER_INITIATOR, TYPERANGE_UTF8,
  269. USE_ALL);
  270. if (!param)
  271. goto out;
  272. param = iscsi_set_default_param(pl, TARGETADDRESS,
  273. INITIAL_TARGETADDRESS, PHASE_DECLARATIVE,
  274. SCOPE_SESSION_WIDE, SENDER_TARGET,
  275. TYPERANGE_TARGETADDRESS, USE_ALL);
  276. if (!param)
  277. goto out;
  278. param = iscsi_set_default_param(pl, TARGETPORTALGROUPTAG,
  279. INITIAL_TARGETPORTALGROUPTAG,
  280. PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_TARGET,
  281. TYPERANGE_0_TO_65535, USE_INITIAL_ONLY);
  282. if (!param)
  283. goto out;
  284. param = iscsi_set_default_param(pl, INITIALR2T, INITIAL_INITIALR2T,
  285. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  286. TYPERANGE_BOOL_OR, USE_LEADING_ONLY);
  287. if (!param)
  288. goto out;
  289. param = iscsi_set_default_param(pl, IMMEDIATEDATA,
  290. INITIAL_IMMEDIATEDATA, PHASE_OPERATIONAL,
  291. SCOPE_SESSION_WIDE, SENDER_BOTH, TYPERANGE_BOOL_AND,
  292. USE_LEADING_ONLY);
  293. if (!param)
  294. goto out;
  295. param = iscsi_set_default_param(pl, MAXXMITDATASEGMENTLENGTH,
  296. INITIAL_MAXXMITDATASEGMENTLENGTH,
  297. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  298. TYPERANGE_512_TO_16777215, USE_ALL);
  299. if (!param)
  300. goto out;
  301. param = iscsi_set_default_param(pl, MAXRECVDATASEGMENTLENGTH,
  302. INITIAL_MAXRECVDATASEGMENTLENGTH,
  303. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  304. TYPERANGE_512_TO_16777215, USE_ALL);
  305. if (!param)
  306. goto out;
  307. param = iscsi_set_default_param(pl, MAXBURSTLENGTH,
  308. INITIAL_MAXBURSTLENGTH, PHASE_OPERATIONAL,
  309. SCOPE_SESSION_WIDE, SENDER_BOTH,
  310. TYPERANGE_512_TO_16777215, USE_LEADING_ONLY);
  311. if (!param)
  312. goto out;
  313. param = iscsi_set_default_param(pl, FIRSTBURSTLENGTH,
  314. INITIAL_FIRSTBURSTLENGTH,
  315. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  316. TYPERANGE_512_TO_16777215, USE_LEADING_ONLY);
  317. if (!param)
  318. goto out;
  319. param = iscsi_set_default_param(pl, DEFAULTTIME2WAIT,
  320. INITIAL_DEFAULTTIME2WAIT,
  321. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  322. TYPERANGE_0_TO_3600, USE_LEADING_ONLY);
  323. if (!param)
  324. goto out;
  325. param = iscsi_set_default_param(pl, DEFAULTTIME2RETAIN,
  326. INITIAL_DEFAULTTIME2RETAIN,
  327. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  328. TYPERANGE_0_TO_3600, USE_LEADING_ONLY);
  329. if (!param)
  330. goto out;
  331. param = iscsi_set_default_param(pl, MAXOUTSTANDINGR2T,
  332. INITIAL_MAXOUTSTANDINGR2T,
  333. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  334. TYPERANGE_1_TO_65535, USE_LEADING_ONLY);
  335. if (!param)
  336. goto out;
  337. param = iscsi_set_default_param(pl, DATAPDUINORDER,
  338. INITIAL_DATAPDUINORDER, PHASE_OPERATIONAL,
  339. SCOPE_SESSION_WIDE, SENDER_BOTH, TYPERANGE_BOOL_OR,
  340. USE_LEADING_ONLY);
  341. if (!param)
  342. goto out;
  343. param = iscsi_set_default_param(pl, DATASEQUENCEINORDER,
  344. INITIAL_DATASEQUENCEINORDER,
  345. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  346. TYPERANGE_BOOL_OR, USE_LEADING_ONLY);
  347. if (!param)
  348. goto out;
  349. param = iscsi_set_default_param(pl, ERRORRECOVERYLEVEL,
  350. INITIAL_ERRORRECOVERYLEVEL,
  351. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  352. TYPERANGE_0_TO_2, USE_LEADING_ONLY);
  353. if (!param)
  354. goto out;
  355. param = iscsi_set_default_param(pl, SESSIONTYPE, INITIAL_SESSIONTYPE,
  356. PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_INITIATOR,
  357. TYPERANGE_SESSIONTYPE, USE_LEADING_ONLY);
  358. if (!param)
  359. goto out;
  360. param = iscsi_set_default_param(pl, IFMARKER, INITIAL_IFMARKER,
  361. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  362. TYPERANGE_BOOL_AND, USE_INITIAL_ONLY);
  363. if (!param)
  364. goto out;
  365. param = iscsi_set_default_param(pl, OFMARKER, INITIAL_OFMARKER,
  366. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  367. TYPERANGE_BOOL_AND, USE_INITIAL_ONLY);
  368. if (!param)
  369. goto out;
  370. param = iscsi_set_default_param(pl, IFMARKINT, INITIAL_IFMARKINT,
  371. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  372. TYPERANGE_MARKINT, USE_INITIAL_ONLY);
  373. if (!param)
  374. goto out;
  375. param = iscsi_set_default_param(pl, OFMARKINT, INITIAL_OFMARKINT,
  376. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  377. TYPERANGE_MARKINT, USE_INITIAL_ONLY);
  378. if (!param)
  379. goto out;
  380. /*
  381. * Extra parameters for ISER from RFC-5046
  382. */
  383. param = iscsi_set_default_param(pl, RDMAEXTENSIONS, INITIAL_RDMAEXTENSIONS,
  384. PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
  385. TYPERANGE_BOOL_AND, USE_LEADING_ONLY);
  386. if (!param)
  387. goto out;
  388. param = iscsi_set_default_param(pl, INITIATORRECVDATASEGMENTLENGTH,
  389. INITIAL_INITIATORRECVDATASEGMENTLENGTH,
  390. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  391. TYPERANGE_512_TO_16777215, USE_ALL);
  392. if (!param)
  393. goto out;
  394. param = iscsi_set_default_param(pl, TARGETRECVDATASEGMENTLENGTH,
  395. INITIAL_TARGETRECVDATASEGMENTLENGTH,
  396. PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
  397. TYPERANGE_512_TO_16777215, USE_ALL);
  398. if (!param)
  399. goto out;
  400. *param_list_ptr = pl;
  401. return 0;
  402. out:
  403. iscsi_release_param_list(pl);
  404. return -1;
  405. }
  406. int iscsi_set_keys_to_negotiate(
  407. struct iscsi_param_list *param_list,
  408. bool iser)
  409. {
  410. struct iscsi_param *param;
  411. param_list->iser = iser;
  412. list_for_each_entry(param, &param_list->param_list, p_list) {
  413. param->state = 0;
  414. if (!strcmp(param->name, AUTHMETHOD)) {
  415. SET_PSTATE_NEGOTIATE(param);
  416. } else if (!strcmp(param->name, HEADERDIGEST)) {
  417. if (iser == false)
  418. SET_PSTATE_NEGOTIATE(param);
  419. } else if (!strcmp(param->name, DATADIGEST)) {
  420. if (iser == false)
  421. SET_PSTATE_NEGOTIATE(param);
  422. } else if (!strcmp(param->name, MAXCONNECTIONS)) {
  423. SET_PSTATE_NEGOTIATE(param);
  424. } else if (!strcmp(param->name, TARGETNAME)) {
  425. continue;
  426. } else if (!strcmp(param->name, INITIATORNAME)) {
  427. continue;
  428. } else if (!strcmp(param->name, TARGETALIAS)) {
  429. if (param->value)
  430. SET_PSTATE_NEGOTIATE(param);
  431. } else if (!strcmp(param->name, INITIATORALIAS)) {
  432. continue;
  433. } else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
  434. SET_PSTATE_NEGOTIATE(param);
  435. } else if (!strcmp(param->name, INITIALR2T)) {
  436. SET_PSTATE_NEGOTIATE(param);
  437. } else if (!strcmp(param->name, IMMEDIATEDATA)) {
  438. SET_PSTATE_NEGOTIATE(param);
  439. } else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
  440. if (iser == false)
  441. SET_PSTATE_NEGOTIATE(param);
  442. } else if (!strcmp(param->name, MAXXMITDATASEGMENTLENGTH)) {
  443. continue;
  444. } else if (!strcmp(param->name, MAXBURSTLENGTH)) {
  445. SET_PSTATE_NEGOTIATE(param);
  446. } else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
  447. SET_PSTATE_NEGOTIATE(param);
  448. } else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
  449. SET_PSTATE_NEGOTIATE(param);
  450. } else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
  451. SET_PSTATE_NEGOTIATE(param);
  452. } else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
  453. SET_PSTATE_NEGOTIATE(param);
  454. } else if (!strcmp(param->name, DATAPDUINORDER)) {
  455. SET_PSTATE_NEGOTIATE(param);
  456. } else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
  457. SET_PSTATE_NEGOTIATE(param);
  458. } else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
  459. SET_PSTATE_NEGOTIATE(param);
  460. } else if (!strcmp(param->name, SESSIONTYPE)) {
  461. SET_PSTATE_NEGOTIATE(param);
  462. } else if (!strcmp(param->name, IFMARKER)) {
  463. SET_PSTATE_NEGOTIATE(param);
  464. } else if (!strcmp(param->name, OFMARKER)) {
  465. SET_PSTATE_NEGOTIATE(param);
  466. } else if (!strcmp(param->name, IFMARKINT)) {
  467. SET_PSTATE_NEGOTIATE(param);
  468. } else if (!strcmp(param->name, OFMARKINT)) {
  469. SET_PSTATE_NEGOTIATE(param);
  470. } else if (!strcmp(param->name, RDMAEXTENSIONS)) {
  471. if (iser == true)
  472. SET_PSTATE_NEGOTIATE(param);
  473. } else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH)) {
  474. if (iser == true)
  475. SET_PSTATE_NEGOTIATE(param);
  476. } else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH)) {
  477. if (iser == true)
  478. SET_PSTATE_NEGOTIATE(param);
  479. }
  480. }
  481. return 0;
  482. }
  483. int iscsi_set_keys_irrelevant_for_discovery(
  484. struct iscsi_param_list *param_list)
  485. {
  486. struct iscsi_param *param;
  487. list_for_each_entry(param, &param_list->param_list, p_list) {
  488. if (!strcmp(param->name, MAXCONNECTIONS))
  489. param->state &= ~PSTATE_NEGOTIATE;
  490. else if (!strcmp(param->name, INITIALR2T))
  491. param->state &= ~PSTATE_NEGOTIATE;
  492. else if (!strcmp(param->name, IMMEDIATEDATA))
  493. param->state &= ~PSTATE_NEGOTIATE;
  494. else if (!strcmp(param->name, MAXBURSTLENGTH))
  495. param->state &= ~PSTATE_NEGOTIATE;
  496. else if (!strcmp(param->name, FIRSTBURSTLENGTH))
  497. param->state &= ~PSTATE_NEGOTIATE;
  498. else if (!strcmp(param->name, MAXOUTSTANDINGR2T))
  499. param->state &= ~PSTATE_NEGOTIATE;
  500. else if (!strcmp(param->name, DATAPDUINORDER))
  501. param->state &= ~PSTATE_NEGOTIATE;
  502. else if (!strcmp(param->name, DATASEQUENCEINORDER))
  503. param->state &= ~PSTATE_NEGOTIATE;
  504. else if (!strcmp(param->name, ERRORRECOVERYLEVEL))
  505. param->state &= ~PSTATE_NEGOTIATE;
  506. else if (!strcmp(param->name, DEFAULTTIME2WAIT))
  507. param->state &= ~PSTATE_NEGOTIATE;
  508. else if (!strcmp(param->name, DEFAULTTIME2RETAIN))
  509. param->state &= ~PSTATE_NEGOTIATE;
  510. else if (!strcmp(param->name, IFMARKER))
  511. param->state &= ~PSTATE_NEGOTIATE;
  512. else if (!strcmp(param->name, OFMARKER))
  513. param->state &= ~PSTATE_NEGOTIATE;
  514. else if (!strcmp(param->name, IFMARKINT))
  515. param->state &= ~PSTATE_NEGOTIATE;
  516. else if (!strcmp(param->name, OFMARKINT))
  517. param->state &= ~PSTATE_NEGOTIATE;
  518. else if (!strcmp(param->name, RDMAEXTENSIONS))
  519. param->state &= ~PSTATE_NEGOTIATE;
  520. else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH))
  521. param->state &= ~PSTATE_NEGOTIATE;
  522. else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH))
  523. param->state &= ~PSTATE_NEGOTIATE;
  524. }
  525. return 0;
  526. }
  527. int iscsi_copy_param_list(
  528. struct iscsi_param_list **dst_param_list,
  529. struct iscsi_param_list *src_param_list,
  530. int leading)
  531. {
  532. struct iscsi_param *param = NULL;
  533. struct iscsi_param *new_param = NULL;
  534. struct iscsi_param_list *param_list = NULL;
  535. param_list = kzalloc(sizeof(struct iscsi_param_list), GFP_KERNEL);
  536. if (!param_list) {
  537. pr_err("Unable to allocate memory for struct iscsi_param_list.\n");
  538. goto err_out;
  539. }
  540. INIT_LIST_HEAD(&param_list->param_list);
  541. INIT_LIST_HEAD(&param_list->extra_response_list);
  542. list_for_each_entry(param, &src_param_list->param_list, p_list) {
  543. if (!leading && (param->scope & SCOPE_SESSION_WIDE)) {
  544. if ((strcmp(param->name, "TargetName") != 0) &&
  545. (strcmp(param->name, "InitiatorName") != 0) &&
  546. (strcmp(param->name, "TargetPortalGroupTag") != 0))
  547. continue;
  548. }
  549. new_param = kzalloc(sizeof(struct iscsi_param), GFP_KERNEL);
  550. if (!new_param) {
  551. pr_err("Unable to allocate memory for struct iscsi_param.\n");
  552. goto err_out;
  553. }
  554. new_param->name = kstrdup(param->name, GFP_KERNEL);
  555. new_param->value = kstrdup(param->value, GFP_KERNEL);
  556. if (!new_param->value || !new_param->name) {
  557. kfree(new_param->value);
  558. kfree(new_param->name);
  559. kfree(new_param);
  560. pr_err("Unable to allocate memory for parameter name/value.\n");
  561. goto err_out;
  562. }
  563. new_param->set_param = param->set_param;
  564. new_param->phase = param->phase;
  565. new_param->scope = param->scope;
  566. new_param->sender = param->sender;
  567. new_param->type = param->type;
  568. new_param->use = param->use;
  569. new_param->type_range = param->type_range;
  570. list_add_tail(&new_param->p_list, &param_list->param_list);
  571. }
  572. if (!list_empty(&param_list->param_list)) {
  573. *dst_param_list = param_list;
  574. } else {
  575. pr_err("No parameters allocated.\n");
  576. goto err_out;
  577. }
  578. return 0;
  579. err_out:
  580. iscsi_release_param_list(param_list);
  581. return -1;
  582. }
  583. static void iscsi_release_extra_responses(struct iscsi_param_list *param_list)
  584. {
  585. struct iscsi_extra_response *er, *er_tmp;
  586. list_for_each_entry_safe(er, er_tmp, &param_list->extra_response_list,
  587. er_list) {
  588. list_del(&er->er_list);
  589. kfree(er);
  590. }
  591. }
  592. void iscsi_release_param_list(struct iscsi_param_list *param_list)
  593. {
  594. struct iscsi_param *param, *param_tmp;
  595. list_for_each_entry_safe(param, param_tmp, &param_list->param_list,
  596. p_list) {
  597. list_del(&param->p_list);
  598. kfree(param->name);
  599. kfree(param->value);
  600. kfree(param);
  601. }
  602. iscsi_release_extra_responses(param_list);
  603. kfree(param_list);
  604. }
  605. struct iscsi_param *iscsi_find_param_from_key(
  606. char *key,
  607. struct iscsi_param_list *param_list)
  608. {
  609. struct iscsi_param *param;
  610. if (!key || !param_list) {
  611. pr_err("Key or parameter list pointer is NULL.\n");
  612. return NULL;
  613. }
  614. list_for_each_entry(param, &param_list->param_list, p_list) {
  615. if (!strcmp(key, param->name))
  616. return param;
  617. }
  618. pr_err("Unable to locate key \"%s\".\n", key);
  619. return NULL;
  620. }
  621. int iscsi_extract_key_value(char *textbuf, char **key, char **value)
  622. {
  623. *value = strchr(textbuf, '=');
  624. if (!*value) {
  625. pr_err("Unable to locate \"=\" separator for key,"
  626. " ignoring request.\n");
  627. return -1;
  628. }
  629. *key = textbuf;
  630. **value = '\0';
  631. *value = *value + 1;
  632. return 0;
  633. }
  634. int iscsi_update_param_value(struct iscsi_param *param, char *value)
  635. {
  636. kfree(param->value);
  637. param->value = kstrdup(value, GFP_KERNEL);
  638. if (!param->value) {
  639. pr_err("Unable to allocate memory for value.\n");
  640. return -ENOMEM;
  641. }
  642. pr_debug("iSCSI Parameter updated to %s=%s\n",
  643. param->name, param->value);
  644. return 0;
  645. }
  646. static int iscsi_add_notunderstood_response(
  647. char *key,
  648. char *value,
  649. struct iscsi_param_list *param_list)
  650. {
  651. struct iscsi_extra_response *extra_response;
  652. if (strlen(value) > VALUE_MAXLEN) {
  653. pr_err("Value for notunderstood key \"%s\" exceeds %d,"
  654. " protocol error.\n", key, VALUE_MAXLEN);
  655. return -1;
  656. }
  657. extra_response = kzalloc(sizeof(struct iscsi_extra_response), GFP_KERNEL);
  658. if (!extra_response) {
  659. pr_err("Unable to allocate memory for"
  660. " struct iscsi_extra_response.\n");
  661. return -1;
  662. }
  663. INIT_LIST_HEAD(&extra_response->er_list);
  664. strlcpy(extra_response->key, key, sizeof(extra_response->key));
  665. strlcpy(extra_response->value, NOTUNDERSTOOD,
  666. sizeof(extra_response->value));
  667. list_add_tail(&extra_response->er_list,
  668. &param_list->extra_response_list);
  669. return 0;
  670. }
  671. static int iscsi_check_for_auth_key(char *key)
  672. {
  673. /*
  674. * RFC 1994
  675. */
  676. if (!strcmp(key, "CHAP_A") || !strcmp(key, "CHAP_I") ||
  677. !strcmp(key, "CHAP_C") || !strcmp(key, "CHAP_N") ||
  678. !strcmp(key, "CHAP_R"))
  679. return 1;
  680. /*
  681. * RFC 2945
  682. */
  683. if (!strcmp(key, "SRP_U") || !strcmp(key, "SRP_N") ||
  684. !strcmp(key, "SRP_g") || !strcmp(key, "SRP_s") ||
  685. !strcmp(key, "SRP_A") || !strcmp(key, "SRP_B") ||
  686. !strcmp(key, "SRP_M") || !strcmp(key, "SRP_HM"))
  687. return 1;
  688. return 0;
  689. }
  690. static void iscsi_check_proposer_for_optional_reply(struct iscsi_param *param)
  691. {
  692. if (IS_TYPE_BOOL_AND(param)) {
  693. if (!strcmp(param->value, NO))
  694. SET_PSTATE_REPLY_OPTIONAL(param);
  695. } else if (IS_TYPE_BOOL_OR(param)) {
  696. if (!strcmp(param->value, YES))
  697. SET_PSTATE_REPLY_OPTIONAL(param);
  698. /*
  699. * Required for gPXE iSCSI boot client
  700. */
  701. if (!strcmp(param->name, IMMEDIATEDATA))
  702. SET_PSTATE_REPLY_OPTIONAL(param);
  703. } else if (IS_TYPE_NUMBER(param)) {
  704. if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH))
  705. SET_PSTATE_REPLY_OPTIONAL(param);
  706. /*
  707. * The GlobalSAN iSCSI Initiator for MacOSX does
  708. * not respond to MaxBurstLength, FirstBurstLength,
  709. * DefaultTime2Wait or DefaultTime2Retain parameter keys.
  710. * So, we set them to 'reply optional' here, and assume the
  711. * the defaults from iscsi_parameters.h if the initiator
  712. * is not RFC compliant and the keys are not negotiated.
  713. */
  714. if (!strcmp(param->name, MAXBURSTLENGTH))
  715. SET_PSTATE_REPLY_OPTIONAL(param);
  716. if (!strcmp(param->name, FIRSTBURSTLENGTH))
  717. SET_PSTATE_REPLY_OPTIONAL(param);
  718. if (!strcmp(param->name, DEFAULTTIME2WAIT))
  719. SET_PSTATE_REPLY_OPTIONAL(param);
  720. if (!strcmp(param->name, DEFAULTTIME2RETAIN))
  721. SET_PSTATE_REPLY_OPTIONAL(param);
  722. /*
  723. * Required for gPXE iSCSI boot client
  724. */
  725. if (!strcmp(param->name, MAXCONNECTIONS))
  726. SET_PSTATE_REPLY_OPTIONAL(param);
  727. } else if (IS_PHASE_DECLARATIVE(param))
  728. SET_PSTATE_REPLY_OPTIONAL(param);
  729. }
  730. static int iscsi_check_boolean_value(struct iscsi_param *param, char *value)
  731. {
  732. if (strcmp(value, YES) && strcmp(value, NO)) {
  733. pr_err("Illegal value for \"%s\", must be either"
  734. " \"%s\" or \"%s\".\n", param->name, YES, NO);
  735. return -1;
  736. }
  737. return 0;
  738. }
  739. static int iscsi_check_numerical_value(struct iscsi_param *param, char *value_ptr)
  740. {
  741. char *tmpptr;
  742. int value = 0;
  743. value = simple_strtoul(value_ptr, &tmpptr, 0);
  744. if (IS_TYPERANGE_0_TO_2(param)) {
  745. if ((value < 0) || (value > 2)) {
  746. pr_err("Illegal value for \"%s\", must be"
  747. " between 0 and 2.\n", param->name);
  748. return -1;
  749. }
  750. return 0;
  751. }
  752. if (IS_TYPERANGE_0_TO_3600(param)) {
  753. if ((value < 0) || (value > 3600)) {
  754. pr_err("Illegal value for \"%s\", must be"
  755. " between 0 and 3600.\n", param->name);
  756. return -1;
  757. }
  758. return 0;
  759. }
  760. if (IS_TYPERANGE_0_TO_32767(param)) {
  761. if ((value < 0) || (value > 32767)) {
  762. pr_err("Illegal value for \"%s\", must be"
  763. " between 0 and 32767.\n", param->name);
  764. return -1;
  765. }
  766. return 0;
  767. }
  768. if (IS_TYPERANGE_0_TO_65535(param)) {
  769. if ((value < 0) || (value > 65535)) {
  770. pr_err("Illegal value for \"%s\", must be"
  771. " between 0 and 65535.\n", param->name);
  772. return -1;
  773. }
  774. return 0;
  775. }
  776. if (IS_TYPERANGE_1_TO_65535(param)) {
  777. if ((value < 1) || (value > 65535)) {
  778. pr_err("Illegal value for \"%s\", must be"
  779. " between 1 and 65535.\n", param->name);
  780. return -1;
  781. }
  782. return 0;
  783. }
  784. if (IS_TYPERANGE_2_TO_3600(param)) {
  785. if ((value < 2) || (value > 3600)) {
  786. pr_err("Illegal value for \"%s\", must be"
  787. " between 2 and 3600.\n", param->name);
  788. return -1;
  789. }
  790. return 0;
  791. }
  792. if (IS_TYPERANGE_512_TO_16777215(param)) {
  793. if ((value < 512) || (value > 16777215)) {
  794. pr_err("Illegal value for \"%s\", must be"
  795. " between 512 and 16777215.\n", param->name);
  796. return -1;
  797. }
  798. return 0;
  799. }
  800. return 0;
  801. }
  802. static int iscsi_check_numerical_range_value(struct iscsi_param *param, char *value)
  803. {
  804. char *left_val_ptr = NULL, *right_val_ptr = NULL;
  805. char *tilde_ptr = NULL;
  806. u32 left_val, right_val, local_left_val;
  807. if (strcmp(param->name, IFMARKINT) &&
  808. strcmp(param->name, OFMARKINT)) {
  809. pr_err("Only parameters \"%s\" or \"%s\" may contain a"
  810. " numerical range value.\n", IFMARKINT, OFMARKINT);
  811. return -1;
  812. }
  813. if (IS_PSTATE_PROPOSER(param))
  814. return 0;
  815. tilde_ptr = strchr(value, '~');
  816. if (!tilde_ptr) {
  817. pr_err("Unable to locate numerical range indicator"
  818. " \"~\" for \"%s\".\n", param->name);
  819. return -1;
  820. }
  821. *tilde_ptr = '\0';
  822. left_val_ptr = value;
  823. right_val_ptr = value + strlen(left_val_ptr) + 1;
  824. if (iscsi_check_numerical_value(param, left_val_ptr) < 0)
  825. return -1;
  826. if (iscsi_check_numerical_value(param, right_val_ptr) < 0)
  827. return -1;
  828. left_val = simple_strtoul(left_val_ptr, NULL, 0);
  829. right_val = simple_strtoul(right_val_ptr, NULL, 0);
  830. *tilde_ptr = '~';
  831. if (right_val < left_val) {
  832. pr_err("Numerical range for parameter \"%s\" contains"
  833. " a right value which is less than the left.\n",
  834. param->name);
  835. return -1;
  836. }
  837. /*
  838. * For now, enforce reasonable defaults for [I,O]FMarkInt.
  839. */
  840. tilde_ptr = strchr(param->value, '~');
  841. if (!tilde_ptr) {
  842. pr_err("Unable to locate numerical range indicator"
  843. " \"~\" for \"%s\".\n", param->name);
  844. return -1;
  845. }
  846. *tilde_ptr = '\0';
  847. left_val_ptr = param->value;
  848. right_val_ptr = param->value + strlen(left_val_ptr) + 1;
  849. local_left_val = simple_strtoul(left_val_ptr, NULL, 0);
  850. *tilde_ptr = '~';
  851. if (param->set_param) {
  852. if ((left_val < local_left_val) ||
  853. (right_val < local_left_val)) {
  854. pr_err("Passed value range \"%u~%u\" is below"
  855. " minimum left value \"%u\" for key \"%s\","
  856. " rejecting.\n", left_val, right_val,
  857. local_left_val, param->name);
  858. return -1;
  859. }
  860. } else {
  861. if ((left_val < local_left_val) &&
  862. (right_val < local_left_val)) {
  863. pr_err("Received value range \"%u~%u\" is"
  864. " below minimum left value \"%u\" for key"
  865. " \"%s\", rejecting.\n", left_val, right_val,
  866. local_left_val, param->name);
  867. SET_PSTATE_REJECT(param);
  868. if (iscsi_update_param_value(param, REJECT) < 0)
  869. return -1;
  870. }
  871. }
  872. return 0;
  873. }
  874. static int iscsi_check_string_or_list_value(struct iscsi_param *param, char *value)
  875. {
  876. if (IS_PSTATE_PROPOSER(param))
  877. return 0;
  878. if (IS_TYPERANGE_AUTH_PARAM(param)) {
  879. if (strcmp(value, KRB5) && strcmp(value, SPKM1) &&
  880. strcmp(value, SPKM2) && strcmp(value, SRP) &&
  881. strcmp(value, CHAP) && strcmp(value, NONE)) {
  882. pr_err("Illegal value for \"%s\", must be"
  883. " \"%s\", \"%s\", \"%s\", \"%s\", \"%s\""
  884. " or \"%s\".\n", param->name, KRB5,
  885. SPKM1, SPKM2, SRP, CHAP, NONE);
  886. return -1;
  887. }
  888. }
  889. if (IS_TYPERANGE_DIGEST_PARAM(param)) {
  890. if (strcmp(value, CRC32C) && strcmp(value, NONE)) {
  891. pr_err("Illegal value for \"%s\", must be"
  892. " \"%s\" or \"%s\".\n", param->name,
  893. CRC32C, NONE);
  894. return -1;
  895. }
  896. }
  897. if (IS_TYPERANGE_SESSIONTYPE(param)) {
  898. if (strcmp(value, DISCOVERY) && strcmp(value, NORMAL)) {
  899. pr_err("Illegal value for \"%s\", must be"
  900. " \"%s\" or \"%s\".\n", param->name,
  901. DISCOVERY, NORMAL);
  902. return -1;
  903. }
  904. }
  905. return 0;
  906. }
  907. /*
  908. * This function is used to pick a value range number, currently just
  909. * returns the lesser of both right values.
  910. */
  911. static char *iscsi_get_value_from_number_range(
  912. struct iscsi_param *param,
  913. char *value)
  914. {
  915. char *end_ptr, *tilde_ptr1 = NULL, *tilde_ptr2 = NULL;
  916. u32 acceptor_right_value, proposer_right_value;
  917. tilde_ptr1 = strchr(value, '~');
  918. if (!tilde_ptr1)
  919. return NULL;
  920. *tilde_ptr1++ = '\0';
  921. proposer_right_value = simple_strtoul(tilde_ptr1, &end_ptr, 0);
  922. tilde_ptr2 = strchr(param->value, '~');
  923. if (!tilde_ptr2)
  924. return NULL;
  925. *tilde_ptr2++ = '\0';
  926. acceptor_right_value = simple_strtoul(tilde_ptr2, &end_ptr, 0);
  927. return (acceptor_right_value >= proposer_right_value) ?
  928. tilde_ptr1 : tilde_ptr2;
  929. }
  930. static char *iscsi_check_valuelist_for_support(
  931. struct iscsi_param *param,
  932. char *value)
  933. {
  934. char *tmp1 = NULL, *tmp2 = NULL;
  935. char *acceptor_values = NULL, *proposer_values = NULL;
  936. acceptor_values = param->value;
  937. proposer_values = value;
  938. do {
  939. if (!proposer_values)
  940. return NULL;
  941. tmp1 = strchr(proposer_values, ',');
  942. if (tmp1)
  943. *tmp1 = '\0';
  944. acceptor_values = param->value;
  945. do {
  946. if (!acceptor_values) {
  947. if (tmp1)
  948. *tmp1 = ',';
  949. return NULL;
  950. }
  951. tmp2 = strchr(acceptor_values, ',');
  952. if (tmp2)
  953. *tmp2 = '\0';
  954. if (!strcmp(acceptor_values, proposer_values)) {
  955. if (tmp2)
  956. *tmp2 = ',';
  957. goto out;
  958. }
  959. if (tmp2)
  960. *tmp2++ = ',';
  961. acceptor_values = tmp2;
  962. } while (acceptor_values);
  963. if (tmp1)
  964. *tmp1++ = ',';
  965. proposer_values = tmp1;
  966. } while (proposer_values);
  967. out:
  968. return proposer_values;
  969. }
  970. static int iscsi_check_acceptor_state(struct iscsi_param *param, char *value,
  971. struct iscsi_conn *conn)
  972. {
  973. u8 acceptor_boolean_value = 0, proposer_boolean_value = 0;
  974. char *negoitated_value = NULL;
  975. if (IS_PSTATE_ACCEPTOR(param)) {
  976. pr_err("Received key \"%s\" twice, protocol error.\n",
  977. param->name);
  978. return -1;
  979. }
  980. if (IS_PSTATE_REJECT(param))
  981. return 0;
  982. if (IS_TYPE_BOOL_AND(param)) {
  983. if (!strcmp(value, YES))
  984. proposer_boolean_value = 1;
  985. if (!strcmp(param->value, YES))
  986. acceptor_boolean_value = 1;
  987. if (acceptor_boolean_value && proposer_boolean_value)
  988. do {} while (0);
  989. else {
  990. if (iscsi_update_param_value(param, NO) < 0)
  991. return -1;
  992. if (!proposer_boolean_value)
  993. SET_PSTATE_REPLY_OPTIONAL(param);
  994. }
  995. } else if (IS_TYPE_BOOL_OR(param)) {
  996. if (!strcmp(value, YES))
  997. proposer_boolean_value = 1;
  998. if (!strcmp(param->value, YES))
  999. acceptor_boolean_value = 1;
  1000. if (acceptor_boolean_value || proposer_boolean_value) {
  1001. if (iscsi_update_param_value(param, YES) < 0)
  1002. return -1;
  1003. if (proposer_boolean_value)
  1004. SET_PSTATE_REPLY_OPTIONAL(param);
  1005. }
  1006. } else if (IS_TYPE_NUMBER(param)) {
  1007. char *tmpptr, buf[11];
  1008. u32 acceptor_value = simple_strtoul(param->value, &tmpptr, 0);
  1009. u32 proposer_value = simple_strtoul(value, &tmpptr, 0);
  1010. memset(buf, 0, sizeof(buf));
  1011. if (!strcmp(param->name, MAXCONNECTIONS) ||
  1012. !strcmp(param->name, MAXBURSTLENGTH) ||
  1013. !strcmp(param->name, FIRSTBURSTLENGTH) ||
  1014. !strcmp(param->name, MAXOUTSTANDINGR2T) ||
  1015. !strcmp(param->name, DEFAULTTIME2RETAIN) ||
  1016. !strcmp(param->name, ERRORRECOVERYLEVEL)) {
  1017. if (proposer_value > acceptor_value) {
  1018. sprintf(buf, "%u", acceptor_value);
  1019. if (iscsi_update_param_value(param,
  1020. &buf[0]) < 0)
  1021. return -1;
  1022. } else {
  1023. if (iscsi_update_param_value(param, value) < 0)
  1024. return -1;
  1025. }
  1026. } else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
  1027. if (acceptor_value > proposer_value) {
  1028. sprintf(buf, "%u", acceptor_value);
  1029. if (iscsi_update_param_value(param,
  1030. &buf[0]) < 0)
  1031. return -1;
  1032. } else {
  1033. if (iscsi_update_param_value(param, value) < 0)
  1034. return -1;
  1035. }
  1036. } else {
  1037. if (iscsi_update_param_value(param, value) < 0)
  1038. return -1;
  1039. }
  1040. if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
  1041. struct iscsi_param *param_mxdsl;
  1042. unsigned long long tmp;
  1043. int rc;
  1044. rc = strict_strtoull(param->value, 0, &tmp);
  1045. if (rc < 0)
  1046. return -1;
  1047. conn->conn_ops->MaxRecvDataSegmentLength = tmp;
  1048. pr_debug("Saving op->MaxRecvDataSegmentLength from"
  1049. " original initiator received value: %u\n",
  1050. conn->conn_ops->MaxRecvDataSegmentLength);
  1051. param_mxdsl = iscsi_find_param_from_key(
  1052. MAXXMITDATASEGMENTLENGTH,
  1053. conn->param_list);
  1054. if (!param_mxdsl)
  1055. return -1;
  1056. rc = iscsi_update_param_value(param,
  1057. param_mxdsl->value);
  1058. if (rc < 0)
  1059. return -1;
  1060. pr_debug("Updated %s to target MXDSL value: %s\n",
  1061. param->name, param->value);
  1062. }
  1063. } else if (IS_TYPE_NUMBER_RANGE(param)) {
  1064. negoitated_value = iscsi_get_value_from_number_range(
  1065. param, value);
  1066. if (!negoitated_value)
  1067. return -1;
  1068. if (iscsi_update_param_value(param, negoitated_value) < 0)
  1069. return -1;
  1070. } else if (IS_TYPE_VALUE_LIST(param)) {
  1071. negoitated_value = iscsi_check_valuelist_for_support(
  1072. param, value);
  1073. if (!negoitated_value) {
  1074. pr_err("Proposer's value list \"%s\" contains"
  1075. " no valid values from Acceptor's value list"
  1076. " \"%s\".\n", value, param->value);
  1077. return -1;
  1078. }
  1079. if (iscsi_update_param_value(param, negoitated_value) < 0)
  1080. return -1;
  1081. } else if (IS_PHASE_DECLARATIVE(param)) {
  1082. if (iscsi_update_param_value(param, value) < 0)
  1083. return -1;
  1084. SET_PSTATE_REPLY_OPTIONAL(param);
  1085. }
  1086. return 0;
  1087. }
  1088. static int iscsi_check_proposer_state(struct iscsi_param *param, char *value)
  1089. {
  1090. if (IS_PSTATE_RESPONSE_GOT(param)) {
  1091. pr_err("Received key \"%s\" twice, protocol error.\n",
  1092. param->name);
  1093. return -1;
  1094. }
  1095. if (IS_TYPE_NUMBER_RANGE(param)) {
  1096. u32 left_val = 0, right_val = 0, recieved_value = 0;
  1097. char *left_val_ptr = NULL, *right_val_ptr = NULL;
  1098. char *tilde_ptr = NULL;
  1099. if (!strcmp(value, IRRELEVANT) || !strcmp(value, REJECT)) {
  1100. if (iscsi_update_param_value(param, value) < 0)
  1101. return -1;
  1102. return 0;
  1103. }
  1104. tilde_ptr = strchr(value, '~');
  1105. if (tilde_ptr) {
  1106. pr_err("Illegal \"~\" in response for \"%s\".\n",
  1107. param->name);
  1108. return -1;
  1109. }
  1110. tilde_ptr = strchr(param->value, '~');
  1111. if (!tilde_ptr) {
  1112. pr_err("Unable to locate numerical range"
  1113. " indicator \"~\" for \"%s\".\n", param->name);
  1114. return -1;
  1115. }
  1116. *tilde_ptr = '\0';
  1117. left_val_ptr = param->value;
  1118. right_val_ptr = param->value + strlen(left_val_ptr) + 1;
  1119. left_val = simple_strtoul(left_val_ptr, NULL, 0);
  1120. right_val = simple_strtoul(right_val_ptr, NULL, 0);
  1121. recieved_value = simple_strtoul(value, NULL, 0);
  1122. *tilde_ptr = '~';
  1123. if ((recieved_value < left_val) ||
  1124. (recieved_value > right_val)) {
  1125. pr_err("Illegal response \"%s=%u\", value must"
  1126. " be between %u and %u.\n", param->name,
  1127. recieved_value, left_val, right_val);
  1128. return -1;
  1129. }
  1130. } else if (IS_TYPE_VALUE_LIST(param)) {
  1131. char *comma_ptr = NULL, *tmp_ptr = NULL;
  1132. comma_ptr = strchr(value, ',');
  1133. if (comma_ptr) {
  1134. pr_err("Illegal \",\" in response for \"%s\".\n",
  1135. param->name);
  1136. return -1;
  1137. }
  1138. tmp_ptr = iscsi_check_valuelist_for_support(param, value);
  1139. if (!tmp_ptr)
  1140. return -1;
  1141. }
  1142. if (iscsi_update_param_value(param, value) < 0)
  1143. return -1;
  1144. return 0;
  1145. }
  1146. static int iscsi_check_value(struct iscsi_param *param, char *value)
  1147. {
  1148. char *comma_ptr = NULL;
  1149. if (!strcmp(value, REJECT)) {
  1150. if (!strcmp(param->name, IFMARKINT) ||
  1151. !strcmp(param->name, OFMARKINT)) {
  1152. /*
  1153. * Reject is not fatal for [I,O]FMarkInt, and causes
  1154. * [I,O]FMarker to be reset to No. (See iSCSI v20 A.3.2)
  1155. */
  1156. SET_PSTATE_REJECT(param);
  1157. return 0;
  1158. }
  1159. pr_err("Received %s=%s\n", param->name, value);
  1160. return -1;
  1161. }
  1162. if (!strcmp(value, IRRELEVANT)) {
  1163. pr_debug("Received %s=%s\n", param->name, value);
  1164. SET_PSTATE_IRRELEVANT(param);
  1165. return 0;
  1166. }
  1167. if (!strcmp(value, NOTUNDERSTOOD)) {
  1168. if (!IS_PSTATE_PROPOSER(param)) {
  1169. pr_err("Received illegal offer %s=%s\n",
  1170. param->name, value);
  1171. return -1;
  1172. }
  1173. /* #warning FIXME: Add check for X-ExtensionKey here */
  1174. pr_err("Standard iSCSI key \"%s\" cannot be answered"
  1175. " with \"%s\", protocol error.\n", param->name, value);
  1176. return -1;
  1177. }
  1178. do {
  1179. comma_ptr = NULL;
  1180. comma_ptr = strchr(value, ',');
  1181. if (comma_ptr && !IS_TYPE_VALUE_LIST(param)) {
  1182. pr_err("Detected value separator \",\", but"
  1183. " key \"%s\" does not allow a value list,"
  1184. " protocol error.\n", param->name);
  1185. return -1;
  1186. }
  1187. if (comma_ptr)
  1188. *comma_ptr = '\0';
  1189. if (strlen(value) > VALUE_MAXLEN) {
  1190. pr_err("Value for key \"%s\" exceeds %d,"
  1191. " protocol error.\n", param->name,
  1192. VALUE_MAXLEN);
  1193. return -1;
  1194. }
  1195. if (IS_TYPE_BOOL_AND(param) || IS_TYPE_BOOL_OR(param)) {
  1196. if (iscsi_check_boolean_value(param, value) < 0)
  1197. return -1;
  1198. } else if (IS_TYPE_NUMBER(param)) {
  1199. if (iscsi_check_numerical_value(param, value) < 0)
  1200. return -1;
  1201. } else if (IS_TYPE_NUMBER_RANGE(param)) {
  1202. if (iscsi_check_numerical_range_value(param, value) < 0)
  1203. return -1;
  1204. } else if (IS_TYPE_STRING(param) || IS_TYPE_VALUE_LIST(param)) {
  1205. if (iscsi_check_string_or_list_value(param, value) < 0)
  1206. return -1;
  1207. } else {
  1208. pr_err("Huh? 0x%02x\n", param->type);
  1209. return -1;
  1210. }
  1211. if (comma_ptr)
  1212. *comma_ptr++ = ',';
  1213. value = comma_ptr;
  1214. } while (value);
  1215. return 0;
  1216. }
  1217. static struct iscsi_param *__iscsi_check_key(
  1218. char *key,
  1219. int sender,
  1220. struct iscsi_param_list *param_list)
  1221. {
  1222. struct iscsi_param *param;
  1223. if (strlen(key) > KEY_MAXLEN) {
  1224. pr_err("Length of key name \"%s\" exceeds %d.\n",
  1225. key, KEY_MAXLEN);
  1226. return NULL;
  1227. }
  1228. param = iscsi_find_param_from_key(key, param_list);
  1229. if (!param)
  1230. return NULL;
  1231. if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
  1232. pr_err("Key \"%s\" may not be sent to %s,"
  1233. " protocol error.\n", param->name,
  1234. (sender & SENDER_RECEIVER) ? "target" : "initiator");
  1235. return NULL;
  1236. }
  1237. if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
  1238. pr_err("Key \"%s\" may not be sent to %s,"
  1239. " protocol error.\n", param->name,
  1240. (sender & SENDER_RECEIVER) ? "initiator" : "target");
  1241. return NULL;
  1242. }
  1243. return param;
  1244. }
  1245. static struct iscsi_param *iscsi_check_key(
  1246. char *key,
  1247. int phase,
  1248. int sender,
  1249. struct iscsi_param_list *param_list)
  1250. {
  1251. struct iscsi_param *param;
  1252. /*
  1253. * Key name length must not exceed 63 bytes. (See iSCSI v20 5.1)
  1254. */
  1255. if (strlen(key) > KEY_MAXLEN) {
  1256. pr_err("Length of key name \"%s\" exceeds %d.\n",
  1257. key, KEY_MAXLEN);
  1258. return NULL;
  1259. }
  1260. param = iscsi_find_param_from_key(key, param_list);
  1261. if (!param)
  1262. return NULL;
  1263. if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
  1264. pr_err("Key \"%s\" may not be sent to %s,"
  1265. " protocol error.\n", param->name,
  1266. (sender & SENDER_RECEIVER) ? "target" : "initiator");
  1267. return NULL;
  1268. }
  1269. if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
  1270. pr_err("Key \"%s\" may not be sent to %s,"
  1271. " protocol error.\n", param->name,
  1272. (sender & SENDER_RECEIVER) ? "initiator" : "target");
  1273. return NULL;
  1274. }
  1275. if (IS_PSTATE_ACCEPTOR(param)) {
  1276. pr_err("Key \"%s\" received twice, protocol error.\n",
  1277. key);
  1278. return NULL;
  1279. }
  1280. if (!phase)
  1281. return param;
  1282. if (!(param->phase & phase)) {
  1283. pr_err("Key \"%s\" may not be negotiated during ",
  1284. param->name);
  1285. switch (phase) {
  1286. case PHASE_SECURITY:
  1287. pr_debug("Security phase.\n");
  1288. break;
  1289. case PHASE_OPERATIONAL:
  1290. pr_debug("Operational phase.\n");
  1291. break;
  1292. default:
  1293. pr_debug("Unknown phase.\n");
  1294. }
  1295. return NULL;
  1296. }
  1297. return param;
  1298. }
  1299. static int iscsi_enforce_integrity_rules(
  1300. u8 phase,
  1301. struct iscsi_param_list *param_list)
  1302. {
  1303. char *tmpptr;
  1304. u8 DataSequenceInOrder = 0;
  1305. u8 ErrorRecoveryLevel = 0, SessionType = 0;
  1306. u8 IFMarker = 0, OFMarker = 0;
  1307. u8 IFMarkInt_Reject = 1, OFMarkInt_Reject = 1;
  1308. u32 FirstBurstLength = 0, MaxBurstLength = 0;
  1309. struct iscsi_param *param = NULL;
  1310. list_for_each_entry(param, &param_list->param_list, p_list) {
  1311. if (!(param->phase & phase))
  1312. continue;
  1313. if (!strcmp(param->name, SESSIONTYPE))
  1314. if (!strcmp(param->value, NORMAL))
  1315. SessionType = 1;
  1316. if (!strcmp(param->name, ERRORRECOVERYLEVEL))
  1317. ErrorRecoveryLevel = simple_strtoul(param->value,
  1318. &tmpptr, 0);
  1319. if (!strcmp(param->name, DATASEQUENCEINORDER))
  1320. if (!strcmp(param->value, YES))
  1321. DataSequenceInOrder = 1;
  1322. if (!strcmp(param->name, MAXBURSTLENGTH))
  1323. MaxBurstLength = simple_strtoul(param->value,
  1324. &tmpptr, 0);
  1325. if (!strcmp(param->name, IFMARKER))
  1326. if (!strcmp(param->value, YES))
  1327. IFMarker = 1;
  1328. if (!strcmp(param->name, OFMARKER))
  1329. if (!strcmp(param->value, YES))
  1330. OFMarker = 1;
  1331. if (!strcmp(param->name, IFMARKINT))
  1332. if (!strcmp(param->value, REJECT))
  1333. IFMarkInt_Reject = 1;
  1334. if (!strcmp(param->name, OFMARKINT))
  1335. if (!strcmp(param->value, REJECT))
  1336. OFMarkInt_Reject = 1;
  1337. }
  1338. list_for_each_entry(param, &param_list->param_list, p_list) {
  1339. if (!(param->phase & phase))
  1340. continue;
  1341. if (!SessionType && (!IS_PSTATE_ACCEPTOR(param) &&
  1342. (strcmp(param->name, IFMARKER) &&
  1343. strcmp(param->name, OFMARKER) &&
  1344. strcmp(param->name, IFMARKINT) &&
  1345. strcmp(param->name, OFMARKINT))))
  1346. continue;
  1347. if (!strcmp(param->name, MAXOUTSTANDINGR2T) &&
  1348. DataSequenceInOrder && (ErrorRecoveryLevel > 0)) {
  1349. if (strcmp(param->value, "1")) {
  1350. if (iscsi_update_param_value(param, "1") < 0)
  1351. return -1;
  1352. pr_debug("Reset \"%s\" to \"%s\".\n",
  1353. param->name, param->value);
  1354. }
  1355. }
  1356. if (!strcmp(param->name, MAXCONNECTIONS) && !SessionType) {
  1357. if (strcmp(param->value, "1")) {
  1358. if (iscsi_update_param_value(param, "1") < 0)
  1359. return -1;
  1360. pr_debug("Reset \"%s\" to \"%s\".\n",
  1361. param->name, param->value);
  1362. }
  1363. }
  1364. if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
  1365. FirstBurstLength = simple_strtoul(param->value,
  1366. &tmpptr, 0);
  1367. if (FirstBurstLength > MaxBurstLength) {
  1368. char tmpbuf[11];
  1369. memset(tmpbuf, 0, sizeof(tmpbuf));
  1370. sprintf(tmpbuf, "%u", MaxBurstLength);
  1371. if (iscsi_update_param_value(param, tmpbuf))
  1372. return -1;
  1373. pr_debug("Reset \"%s\" to \"%s\".\n",
  1374. param->name, param->value);
  1375. }
  1376. }
  1377. if (!strcmp(param->name, IFMARKER) && IFMarkInt_Reject) {
  1378. if (iscsi_update_param_value(param, NO) < 0)
  1379. return -1;
  1380. IFMarker = 0;
  1381. pr_debug("Reset \"%s\" to \"%s\".\n",
  1382. param->name, param->value);
  1383. }
  1384. if (!strcmp(param->name, OFMARKER) && OFMarkInt_Reject) {
  1385. if (iscsi_update_param_value(param, NO) < 0)
  1386. return -1;
  1387. OFMarker = 0;
  1388. pr_debug("Reset \"%s\" to \"%s\".\n",
  1389. param->name, param->value);
  1390. }
  1391. if (!strcmp(param->name, IFMARKINT) && !IFMarker) {
  1392. if (!strcmp(param->value, REJECT))
  1393. continue;
  1394. param->state &= ~PSTATE_NEGOTIATE;
  1395. if (iscsi_update_param_value(param, IRRELEVANT) < 0)
  1396. return -1;
  1397. pr_debug("Reset \"%s\" to \"%s\".\n",
  1398. param->name, param->value);
  1399. }
  1400. if (!strcmp(param->name, OFMARKINT) && !OFMarker) {
  1401. if (!strcmp(param->value, REJECT))
  1402. continue;
  1403. param->state &= ~PSTATE_NEGOTIATE;
  1404. if (iscsi_update_param_value(param, IRRELEVANT) < 0)
  1405. return -1;
  1406. pr_debug("Reset \"%s\" to \"%s\".\n",
  1407. param->name, param->value);
  1408. }
  1409. }
  1410. return 0;
  1411. }
  1412. int iscsi_decode_text_input(
  1413. u8 phase,
  1414. u8 sender,
  1415. char *textbuf,
  1416. u32 length,
  1417. struct iscsi_conn *conn)
  1418. {
  1419. struct iscsi_param_list *param_list = conn->param_list;
  1420. char *tmpbuf, *start = NULL, *end = NULL;
  1421. tmpbuf = kzalloc(length + 1, GFP_KERNEL);
  1422. if (!tmpbuf) {
  1423. pr_err("Unable to allocate memory for tmpbuf.\n");
  1424. return -1;
  1425. }
  1426. memcpy(tmpbuf, textbuf, length);
  1427. tmpbuf[length] = '\0';
  1428. start = tmpbuf;
  1429. end = (start + length);
  1430. while (start < end) {
  1431. char *key, *value;
  1432. struct iscsi_param *param;
  1433. if (iscsi_extract_key_value(start, &key, &value) < 0) {
  1434. kfree(tmpbuf);
  1435. return -1;
  1436. }
  1437. pr_debug("Got key: %s=%s\n", key, value);
  1438. if (phase & PHASE_SECURITY) {
  1439. if (iscsi_check_for_auth_key(key) > 0) {
  1440. kfree(tmpbuf);
  1441. return 1;
  1442. }
  1443. }
  1444. param = iscsi_check_key(key, phase, sender, param_list);
  1445. if (!param) {
  1446. if (iscsi_add_notunderstood_response(key,
  1447. value, param_list) < 0) {
  1448. kfree(tmpbuf);
  1449. return -1;
  1450. }
  1451. start += strlen(key) + strlen(value) + 2;
  1452. continue;
  1453. }
  1454. if (iscsi_check_value(param, value) < 0) {
  1455. kfree(tmpbuf);
  1456. return -1;
  1457. }
  1458. start += strlen(key) + strlen(value) + 2;
  1459. if (IS_PSTATE_PROPOSER(param)) {
  1460. if (iscsi_check_proposer_state(param, value) < 0) {
  1461. kfree(tmpbuf);
  1462. return -1;
  1463. }
  1464. SET_PSTATE_RESPONSE_GOT(param);
  1465. } else {
  1466. if (iscsi_check_acceptor_state(param, value, conn) < 0) {
  1467. kfree(tmpbuf);
  1468. return -1;
  1469. }
  1470. SET_PSTATE_ACCEPTOR(param);
  1471. }
  1472. }
  1473. kfree(tmpbuf);
  1474. return 0;
  1475. }
  1476. int iscsi_encode_text_output(
  1477. u8 phase,
  1478. u8 sender,
  1479. char *textbuf,
  1480. u32 *length,
  1481. struct iscsi_param_list *param_list)
  1482. {
  1483. char *output_buf = NULL;
  1484. struct iscsi_extra_response *er;
  1485. struct iscsi_param *param;
  1486. output_buf = textbuf + *length;
  1487. if (iscsi_enforce_integrity_rules(phase, param_list) < 0)
  1488. return -1;
  1489. list_for_each_entry(param, &param_list->param_list, p_list) {
  1490. if (!(param->sender & sender))
  1491. continue;
  1492. if (IS_PSTATE_ACCEPTOR(param) &&
  1493. !IS_PSTATE_RESPONSE_SENT(param) &&
  1494. !IS_PSTATE_REPLY_OPTIONAL(param) &&
  1495. (param->phase & phase)) {
  1496. *length += sprintf(output_buf, "%s=%s",
  1497. param->name, param->value);
  1498. *length += 1;
  1499. output_buf = textbuf + *length;
  1500. SET_PSTATE_RESPONSE_SENT(param);
  1501. pr_debug("Sending key: %s=%s\n",
  1502. param->name, param->value);
  1503. continue;
  1504. }
  1505. if (IS_PSTATE_NEGOTIATE(param) &&
  1506. !IS_PSTATE_ACCEPTOR(param) &&
  1507. !IS_PSTATE_PROPOSER(param) &&
  1508. (param->phase & phase)) {
  1509. *length += sprintf(output_buf, "%s=%s",
  1510. param->name, param->value);
  1511. *length += 1;
  1512. output_buf = textbuf + *length;
  1513. SET_PSTATE_PROPOSER(param);
  1514. iscsi_check_proposer_for_optional_reply(param);
  1515. pr_debug("Sending key: %s=%s\n",
  1516. param->name, param->value);
  1517. }
  1518. }
  1519. list_for_each_entry(er, &param_list->extra_response_list, er_list) {
  1520. *length += sprintf(output_buf, "%s=%s", er->key, er->value);
  1521. *length += 1;
  1522. output_buf = textbuf + *length;
  1523. pr_debug("Sending key: %s=%s\n", er->key, er->value);
  1524. }
  1525. iscsi_release_extra_responses(param_list);
  1526. return 0;
  1527. }
  1528. int iscsi_check_negotiated_keys(struct iscsi_param_list *param_list)
  1529. {
  1530. int ret = 0;
  1531. struct iscsi_param *param;
  1532. list_for_each_entry(param, &param_list->param_list, p_list) {
  1533. if (IS_PSTATE_NEGOTIATE(param) &&
  1534. IS_PSTATE_PROPOSER(param) &&
  1535. !IS_PSTATE_RESPONSE_GOT(param) &&
  1536. !IS_PSTATE_REPLY_OPTIONAL(param) &&
  1537. !IS_PHASE_DECLARATIVE(param)) {
  1538. pr_err("No response for proposed key \"%s\".\n",
  1539. param->name);
  1540. ret = -1;
  1541. }
  1542. }
  1543. return ret;
  1544. }
  1545. int iscsi_change_param_value(
  1546. char *keyvalue,
  1547. struct iscsi_param_list *param_list,
  1548. int check_key)
  1549. {
  1550. char *key = NULL, *value = NULL;
  1551. struct iscsi_param *param;
  1552. int sender = 0;
  1553. if (iscsi_extract_key_value(keyvalue, &key, &value) < 0)
  1554. return -1;
  1555. if (!check_key) {
  1556. param = __iscsi_check_key(keyvalue, sender, param_list);
  1557. if (!param)
  1558. return -1;
  1559. } else {
  1560. param = iscsi_check_key(keyvalue, 0, sender, param_list);
  1561. if (!param)
  1562. return -1;
  1563. param->set_param = 1;
  1564. if (iscsi_check_value(param, value) < 0) {
  1565. param->set_param = 0;
  1566. return -1;
  1567. }
  1568. param->set_param = 0;
  1569. }
  1570. if (iscsi_update_param_value(param, value) < 0)
  1571. return -1;
  1572. return 0;
  1573. }
  1574. void iscsi_set_connection_parameters(
  1575. struct iscsi_conn_ops *ops,
  1576. struct iscsi_param_list *param_list)
  1577. {
  1578. char *tmpptr;
  1579. struct iscsi_param *param;
  1580. pr_debug("---------------------------------------------------"
  1581. "---------------\n");
  1582. list_for_each_entry(param, &param_list->param_list, p_list) {
  1583. /*
  1584. * Special case to set MAXXMITDATASEGMENTLENGTH from the
  1585. * target requested MaxRecvDataSegmentLength, even though
  1586. * this key is not sent over the wire.
  1587. */
  1588. if (!strcmp(param->name, MAXXMITDATASEGMENTLENGTH)) {
  1589. ops->MaxXmitDataSegmentLength =
  1590. simple_strtoul(param->value, &tmpptr, 0);
  1591. pr_debug("MaxXmitDataSegmentLength: %s\n",
  1592. param->value);
  1593. }
  1594. if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
  1595. continue;
  1596. if (!strcmp(param->name, AUTHMETHOD)) {
  1597. pr_debug("AuthMethod: %s\n",
  1598. param->value);
  1599. } else if (!strcmp(param->name, HEADERDIGEST)) {
  1600. ops->HeaderDigest = !strcmp(param->value, CRC32C);
  1601. pr_debug("HeaderDigest: %s\n",
  1602. param->value);
  1603. } else if (!strcmp(param->name, DATADIGEST)) {
  1604. ops->DataDigest = !strcmp(param->value, CRC32C);
  1605. pr_debug("DataDigest: %s\n",
  1606. param->value);
  1607. } else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
  1608. /*
  1609. * At this point iscsi_check_acceptor_state() will have
  1610. * set ops->MaxRecvDataSegmentLength from the original
  1611. * initiator provided value.
  1612. */
  1613. pr_debug("MaxRecvDataSegmentLength: %u\n",
  1614. ops->MaxRecvDataSegmentLength);
  1615. } else if (!strcmp(param->name, OFMARKER)) {
  1616. ops->OFMarker = !strcmp(param->value, YES);
  1617. pr_debug("OFMarker: %s\n",
  1618. param->value);
  1619. } else if (!strcmp(param->name, IFMARKER)) {
  1620. ops->IFMarker = !strcmp(param->value, YES);
  1621. pr_debug("IFMarker: %s\n",
  1622. param->value);
  1623. } else if (!strcmp(param->name, OFMARKINT)) {
  1624. ops->OFMarkInt =
  1625. simple_strtoul(param->value, &tmpptr, 0);
  1626. pr_debug("OFMarkInt: %s\n",
  1627. param->value);
  1628. } else if (!strcmp(param->name, IFMARKINT)) {
  1629. ops->IFMarkInt =
  1630. simple_strtoul(param->value, &tmpptr, 0);
  1631. pr_debug("IFMarkInt: %s\n",
  1632. param->value);
  1633. } else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH)) {
  1634. ops->InitiatorRecvDataSegmentLength =
  1635. simple_strtoul(param->value, &tmpptr, 0);
  1636. pr_debug("InitiatorRecvDataSegmentLength: %s\n",
  1637. param->value);
  1638. ops->MaxRecvDataSegmentLength =
  1639. ops->InitiatorRecvDataSegmentLength;
  1640. pr_debug("Set MRDSL from InitiatorRecvDataSegmentLength\n");
  1641. } else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH)) {
  1642. ops->TargetRecvDataSegmentLength =
  1643. simple_strtoul(param->value, &tmpptr, 0);
  1644. pr_debug("TargetRecvDataSegmentLength: %s\n",
  1645. param->value);
  1646. ops->MaxXmitDataSegmentLength =
  1647. ops->TargetRecvDataSegmentLength;
  1648. pr_debug("Set MXDSL from TargetRecvDataSegmentLength\n");
  1649. }
  1650. }
  1651. pr_debug("----------------------------------------------------"
  1652. "--------------\n");
  1653. }
  1654. void iscsi_set_session_parameters(
  1655. struct iscsi_sess_ops *ops,
  1656. struct iscsi_param_list *param_list,
  1657. int leading)
  1658. {
  1659. char *tmpptr;
  1660. struct iscsi_param *param;
  1661. pr_debug("----------------------------------------------------"
  1662. "--------------\n");
  1663. list_for_each_entry(param, &param_list->param_list, p_list) {
  1664. if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
  1665. continue;
  1666. if (!strcmp(param->name, INITIATORNAME)) {
  1667. if (!param->value)
  1668. continue;
  1669. if (leading)
  1670. snprintf(ops->InitiatorName,
  1671. sizeof(ops->InitiatorName),
  1672. "%s", param->value);
  1673. pr_debug("InitiatorName: %s\n",
  1674. param->value);
  1675. } else if (!strcmp(param->name, INITIATORALIAS)) {
  1676. if (!param->value)
  1677. continue;
  1678. snprintf(ops->InitiatorAlias,
  1679. sizeof(ops->InitiatorAlias),
  1680. "%s", param->value);
  1681. pr_debug("InitiatorAlias: %s\n",
  1682. param->value);
  1683. } else if (!strcmp(param->name, TARGETNAME)) {
  1684. if (!param->value)
  1685. continue;
  1686. if (leading)
  1687. snprintf(ops->TargetName,
  1688. sizeof(ops->TargetName),
  1689. "%s", param->value);
  1690. pr_debug("TargetName: %s\n",
  1691. param->value);
  1692. } else if (!strcmp(param->name, TARGETALIAS)) {
  1693. if (!param->value)
  1694. continue;
  1695. snprintf(ops->TargetAlias, sizeof(ops->TargetAlias),
  1696. "%s", param->value);
  1697. pr_debug("TargetAlias: %s\n",
  1698. param->value);
  1699. } else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
  1700. ops->TargetPortalGroupTag =
  1701. simple_strtoul(param->value, &tmpptr, 0);
  1702. pr_debug("TargetPortalGroupTag: %s\n",
  1703. param->value);
  1704. } else if (!strcmp(param->name, MAXCONNECTIONS)) {
  1705. ops->MaxConnections =
  1706. simple_strtoul(param->value, &tmpptr, 0);
  1707. pr_debug("MaxConnections: %s\n",
  1708. param->value);
  1709. } else if (!strcmp(param->name, INITIALR2T)) {
  1710. ops->InitialR2T = !strcmp(param->value, YES);
  1711. pr_debug("InitialR2T: %s\n",
  1712. param->value);
  1713. } else if (!strcmp(param->name, IMMEDIATEDATA)) {
  1714. ops->ImmediateData = !strcmp(param->value, YES);
  1715. pr_debug("ImmediateData: %s\n",
  1716. param->value);
  1717. } else if (!strcmp(param->name, MAXBURSTLENGTH)) {
  1718. ops->MaxBurstLength =
  1719. simple_strtoul(param->value, &tmpptr, 0);
  1720. pr_debug("MaxBurstLength: %s\n",
  1721. param->value);
  1722. } else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
  1723. ops->FirstBurstLength =
  1724. simple_strtoul(param->value, &tmpptr, 0);
  1725. pr_debug("FirstBurstLength: %s\n",
  1726. param->value);
  1727. } else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
  1728. ops->DefaultTime2Wait =
  1729. simple_strtoul(param->value, &tmpptr, 0);
  1730. pr_debug("DefaultTime2Wait: %s\n",
  1731. param->value);
  1732. } else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
  1733. ops->DefaultTime2Retain =
  1734. simple_strtoul(param->value, &tmpptr, 0);
  1735. pr_debug("DefaultTime2Retain: %s\n",
  1736. param->value);
  1737. } else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
  1738. ops->MaxOutstandingR2T =
  1739. simple_strtoul(param->value, &tmpptr, 0);
  1740. pr_debug("MaxOutstandingR2T: %s\n",
  1741. param->value);
  1742. } else if (!strcmp(param->name, DATAPDUINORDER)) {
  1743. ops->DataPDUInOrder = !strcmp(param->value, YES);
  1744. pr_debug("DataPDUInOrder: %s\n",
  1745. param->value);
  1746. } else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
  1747. ops->DataSequenceInOrder = !strcmp(param->value, YES);
  1748. pr_debug("DataSequenceInOrder: %s\n",
  1749. param->value);
  1750. } else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
  1751. ops->ErrorRecoveryLevel =
  1752. simple_strtoul(param->value, &tmpptr, 0);
  1753. pr_debug("ErrorRecoveryLevel: %s\n",
  1754. param->value);
  1755. } else if (!strcmp(param->name, SESSIONTYPE)) {
  1756. ops->SessionType = !strcmp(param->value, DISCOVERY);
  1757. pr_debug("SessionType: %s\n",
  1758. param->value);
  1759. } else if (!strcmp(param->name, RDMAEXTENSIONS)) {
  1760. ops->RDMAExtensions = !strcmp(param->value, YES);
  1761. pr_debug("RDMAExtensions: %s\n",
  1762. param->value);
  1763. }
  1764. }
  1765. pr_debug("----------------------------------------------------"
  1766. "--------------\n");
  1767. }