iscsi_target_parameters.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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, RDMAEXTENTIONS, INITIAL_RDMAEXTENTIONS,
  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, RDMAEXTENTIONS)) {
  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, RDMAEXTENTIONS))
  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. strncpy(extra_response->key, key, strlen(key) + 1);
  665. strncpy(extra_response->value, NOTUNDERSTOOD,
  666. strlen(NOTUNDERSTOOD) + 1);
  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. char *tmpptr = key + strlen(key);
  1441. *tmpptr = '=';
  1442. kfree(tmpbuf);
  1443. return 1;
  1444. }
  1445. }
  1446. param = iscsi_check_key(key, phase, sender, param_list);
  1447. if (!param) {
  1448. if (iscsi_add_notunderstood_response(key,
  1449. value, param_list) < 0) {
  1450. kfree(tmpbuf);
  1451. return -1;
  1452. }
  1453. start += strlen(key) + strlen(value) + 2;
  1454. continue;
  1455. }
  1456. if (iscsi_check_value(param, value) < 0) {
  1457. kfree(tmpbuf);
  1458. return -1;
  1459. }
  1460. start += strlen(key) + strlen(value) + 2;
  1461. if (IS_PSTATE_PROPOSER(param)) {
  1462. if (iscsi_check_proposer_state(param, value) < 0) {
  1463. kfree(tmpbuf);
  1464. return -1;
  1465. }
  1466. SET_PSTATE_RESPONSE_GOT(param);
  1467. } else {
  1468. if (iscsi_check_acceptor_state(param, value, conn) < 0) {
  1469. kfree(tmpbuf);
  1470. return -1;
  1471. }
  1472. SET_PSTATE_ACCEPTOR(param);
  1473. }
  1474. }
  1475. kfree(tmpbuf);
  1476. return 0;
  1477. }
  1478. int iscsi_encode_text_output(
  1479. u8 phase,
  1480. u8 sender,
  1481. char *textbuf,
  1482. u32 *length,
  1483. struct iscsi_param_list *param_list)
  1484. {
  1485. char *output_buf = NULL;
  1486. struct iscsi_extra_response *er;
  1487. struct iscsi_param *param;
  1488. output_buf = textbuf + *length;
  1489. if (iscsi_enforce_integrity_rules(phase, param_list) < 0)
  1490. return -1;
  1491. list_for_each_entry(param, &param_list->param_list, p_list) {
  1492. if (!(param->sender & sender))
  1493. continue;
  1494. if (IS_PSTATE_ACCEPTOR(param) &&
  1495. !IS_PSTATE_RESPONSE_SENT(param) &&
  1496. !IS_PSTATE_REPLY_OPTIONAL(param) &&
  1497. (param->phase & phase)) {
  1498. *length += sprintf(output_buf, "%s=%s",
  1499. param->name, param->value);
  1500. *length += 1;
  1501. output_buf = textbuf + *length;
  1502. SET_PSTATE_RESPONSE_SENT(param);
  1503. pr_debug("Sending key: %s=%s\n",
  1504. param->name, param->value);
  1505. continue;
  1506. }
  1507. if (IS_PSTATE_NEGOTIATE(param) &&
  1508. !IS_PSTATE_ACCEPTOR(param) &&
  1509. !IS_PSTATE_PROPOSER(param) &&
  1510. (param->phase & phase)) {
  1511. *length += sprintf(output_buf, "%s=%s",
  1512. param->name, param->value);
  1513. *length += 1;
  1514. output_buf = textbuf + *length;
  1515. SET_PSTATE_PROPOSER(param);
  1516. iscsi_check_proposer_for_optional_reply(param);
  1517. pr_debug("Sending key: %s=%s\n",
  1518. param->name, param->value);
  1519. }
  1520. }
  1521. list_for_each_entry(er, &param_list->extra_response_list, er_list) {
  1522. *length += sprintf(output_buf, "%s=%s", er->key, er->value);
  1523. *length += 1;
  1524. output_buf = textbuf + *length;
  1525. pr_debug("Sending key: %s=%s\n", er->key, er->value);
  1526. }
  1527. iscsi_release_extra_responses(param_list);
  1528. return 0;
  1529. }
  1530. int iscsi_check_negotiated_keys(struct iscsi_param_list *param_list)
  1531. {
  1532. int ret = 0;
  1533. struct iscsi_param *param;
  1534. list_for_each_entry(param, &param_list->param_list, p_list) {
  1535. if (IS_PSTATE_NEGOTIATE(param) &&
  1536. IS_PSTATE_PROPOSER(param) &&
  1537. !IS_PSTATE_RESPONSE_GOT(param) &&
  1538. !IS_PSTATE_REPLY_OPTIONAL(param) &&
  1539. !IS_PHASE_DECLARATIVE(param)) {
  1540. pr_err("No response for proposed key \"%s\".\n",
  1541. param->name);
  1542. ret = -1;
  1543. }
  1544. }
  1545. return ret;
  1546. }
  1547. int iscsi_change_param_value(
  1548. char *keyvalue,
  1549. struct iscsi_param_list *param_list,
  1550. int check_key)
  1551. {
  1552. char *key = NULL, *value = NULL;
  1553. struct iscsi_param *param;
  1554. int sender = 0;
  1555. if (iscsi_extract_key_value(keyvalue, &key, &value) < 0)
  1556. return -1;
  1557. if (!check_key) {
  1558. param = __iscsi_check_key(keyvalue, sender, param_list);
  1559. if (!param)
  1560. return -1;
  1561. } else {
  1562. param = iscsi_check_key(keyvalue, 0, sender, param_list);
  1563. if (!param)
  1564. return -1;
  1565. param->set_param = 1;
  1566. if (iscsi_check_value(param, value) < 0) {
  1567. param->set_param = 0;
  1568. return -1;
  1569. }
  1570. param->set_param = 0;
  1571. }
  1572. if (iscsi_update_param_value(param, value) < 0)
  1573. return -1;
  1574. return 0;
  1575. }
  1576. void iscsi_set_connection_parameters(
  1577. struct iscsi_conn_ops *ops,
  1578. struct iscsi_param_list *param_list)
  1579. {
  1580. char *tmpptr;
  1581. struct iscsi_param *param;
  1582. pr_debug("---------------------------------------------------"
  1583. "---------------\n");
  1584. list_for_each_entry(param, &param_list->param_list, p_list) {
  1585. /*
  1586. * Special case to set MAXXMITDATASEGMENTLENGTH from the
  1587. * target requested MaxRecvDataSegmentLength, even though
  1588. * this key is not sent over the wire.
  1589. */
  1590. if (!strcmp(param->name, MAXXMITDATASEGMENTLENGTH)) {
  1591. if (param_list->iser == true)
  1592. continue;
  1593. ops->MaxXmitDataSegmentLength =
  1594. simple_strtoul(param->value, &tmpptr, 0);
  1595. pr_debug("MaxXmitDataSegmentLength: %s\n",
  1596. param->value);
  1597. }
  1598. if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
  1599. continue;
  1600. if (!strcmp(param->name, AUTHMETHOD)) {
  1601. pr_debug("AuthMethod: %s\n",
  1602. param->value);
  1603. } else if (!strcmp(param->name, HEADERDIGEST)) {
  1604. ops->HeaderDigest = !strcmp(param->value, CRC32C);
  1605. pr_debug("HeaderDigest: %s\n",
  1606. param->value);
  1607. } else if (!strcmp(param->name, DATADIGEST)) {
  1608. ops->DataDigest = !strcmp(param->value, CRC32C);
  1609. pr_debug("DataDigest: %s\n",
  1610. param->value);
  1611. } else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
  1612. /*
  1613. * At this point iscsi_check_acceptor_state() will have
  1614. * set ops->MaxRecvDataSegmentLength from the original
  1615. * initiator provided value.
  1616. */
  1617. pr_debug("MaxRecvDataSegmentLength: %u\n",
  1618. ops->MaxRecvDataSegmentLength);
  1619. } else if (!strcmp(param->name, OFMARKER)) {
  1620. ops->OFMarker = !strcmp(param->value, YES);
  1621. pr_debug("OFMarker: %s\n",
  1622. param->value);
  1623. } else if (!strcmp(param->name, IFMARKER)) {
  1624. ops->IFMarker = !strcmp(param->value, YES);
  1625. pr_debug("IFMarker: %s\n",
  1626. param->value);
  1627. } else if (!strcmp(param->name, OFMARKINT)) {
  1628. ops->OFMarkInt =
  1629. simple_strtoul(param->value, &tmpptr, 0);
  1630. pr_debug("OFMarkInt: %s\n",
  1631. param->value);
  1632. } else if (!strcmp(param->name, IFMARKINT)) {
  1633. ops->IFMarkInt =
  1634. simple_strtoul(param->value, &tmpptr, 0);
  1635. pr_debug("IFMarkInt: %s\n",
  1636. param->value);
  1637. } else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH)) {
  1638. ops->InitiatorRecvDataSegmentLength =
  1639. simple_strtoul(param->value, &tmpptr, 0);
  1640. pr_debug("InitiatorRecvDataSegmentLength: %s\n",
  1641. param->value);
  1642. ops->MaxRecvDataSegmentLength =
  1643. ops->InitiatorRecvDataSegmentLength;
  1644. pr_debug("Set MRDSL from InitiatorRecvDataSegmentLength\n");
  1645. } else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH)) {
  1646. ops->TargetRecvDataSegmentLength =
  1647. simple_strtoul(param->value, &tmpptr, 0);
  1648. pr_debug("TargetRecvDataSegmentLength: %s\n",
  1649. param->value);
  1650. ops->MaxXmitDataSegmentLength =
  1651. ops->TargetRecvDataSegmentLength;
  1652. pr_debug("Set MXDSL from TargetRecvDataSegmentLength\n");
  1653. }
  1654. }
  1655. pr_debug("----------------------------------------------------"
  1656. "--------------\n");
  1657. }
  1658. void iscsi_set_session_parameters(
  1659. struct iscsi_sess_ops *ops,
  1660. struct iscsi_param_list *param_list,
  1661. int leading)
  1662. {
  1663. char *tmpptr;
  1664. struct iscsi_param *param;
  1665. pr_debug("----------------------------------------------------"
  1666. "--------------\n");
  1667. list_for_each_entry(param, &param_list->param_list, p_list) {
  1668. if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
  1669. continue;
  1670. if (!strcmp(param->name, INITIATORNAME)) {
  1671. if (!param->value)
  1672. continue;
  1673. if (leading)
  1674. snprintf(ops->InitiatorName,
  1675. sizeof(ops->InitiatorName),
  1676. "%s", param->value);
  1677. pr_debug("InitiatorName: %s\n",
  1678. param->value);
  1679. } else if (!strcmp(param->name, INITIATORALIAS)) {
  1680. if (!param->value)
  1681. continue;
  1682. snprintf(ops->InitiatorAlias,
  1683. sizeof(ops->InitiatorAlias),
  1684. "%s", param->value);
  1685. pr_debug("InitiatorAlias: %s\n",
  1686. param->value);
  1687. } else if (!strcmp(param->name, TARGETNAME)) {
  1688. if (!param->value)
  1689. continue;
  1690. if (leading)
  1691. snprintf(ops->TargetName,
  1692. sizeof(ops->TargetName),
  1693. "%s", param->value);
  1694. pr_debug("TargetName: %s\n",
  1695. param->value);
  1696. } else if (!strcmp(param->name, TARGETALIAS)) {
  1697. if (!param->value)
  1698. continue;
  1699. snprintf(ops->TargetAlias, sizeof(ops->TargetAlias),
  1700. "%s", param->value);
  1701. pr_debug("TargetAlias: %s\n",
  1702. param->value);
  1703. } else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
  1704. ops->TargetPortalGroupTag =
  1705. simple_strtoul(param->value, &tmpptr, 0);
  1706. pr_debug("TargetPortalGroupTag: %s\n",
  1707. param->value);
  1708. } else if (!strcmp(param->name, MAXCONNECTIONS)) {
  1709. ops->MaxConnections =
  1710. simple_strtoul(param->value, &tmpptr, 0);
  1711. pr_debug("MaxConnections: %s\n",
  1712. param->value);
  1713. } else if (!strcmp(param->name, INITIALR2T)) {
  1714. ops->InitialR2T = !strcmp(param->value, YES);
  1715. pr_debug("InitialR2T: %s\n",
  1716. param->value);
  1717. } else if (!strcmp(param->name, IMMEDIATEDATA)) {
  1718. ops->ImmediateData = !strcmp(param->value, YES);
  1719. pr_debug("ImmediateData: %s\n",
  1720. param->value);
  1721. } else if (!strcmp(param->name, MAXBURSTLENGTH)) {
  1722. ops->MaxBurstLength =
  1723. simple_strtoul(param->value, &tmpptr, 0);
  1724. pr_debug("MaxBurstLength: %s\n",
  1725. param->value);
  1726. } else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
  1727. ops->FirstBurstLength =
  1728. simple_strtoul(param->value, &tmpptr, 0);
  1729. pr_debug("FirstBurstLength: %s\n",
  1730. param->value);
  1731. } else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
  1732. ops->DefaultTime2Wait =
  1733. simple_strtoul(param->value, &tmpptr, 0);
  1734. pr_debug("DefaultTime2Wait: %s\n",
  1735. param->value);
  1736. } else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
  1737. ops->DefaultTime2Retain =
  1738. simple_strtoul(param->value, &tmpptr, 0);
  1739. pr_debug("DefaultTime2Retain: %s\n",
  1740. param->value);
  1741. } else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
  1742. ops->MaxOutstandingR2T =
  1743. simple_strtoul(param->value, &tmpptr, 0);
  1744. pr_debug("MaxOutstandingR2T: %s\n",
  1745. param->value);
  1746. } else if (!strcmp(param->name, DATAPDUINORDER)) {
  1747. ops->DataPDUInOrder = !strcmp(param->value, YES);
  1748. pr_debug("DataPDUInOrder: %s\n",
  1749. param->value);
  1750. } else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
  1751. ops->DataSequenceInOrder = !strcmp(param->value, YES);
  1752. pr_debug("DataSequenceInOrder: %s\n",
  1753. param->value);
  1754. } else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
  1755. ops->ErrorRecoveryLevel =
  1756. simple_strtoul(param->value, &tmpptr, 0);
  1757. pr_debug("ErrorRecoveryLevel: %s\n",
  1758. param->value);
  1759. } else if (!strcmp(param->name, SESSIONTYPE)) {
  1760. ops->SessionType = !strcmp(param->value, DISCOVERY);
  1761. pr_debug("SessionType: %s\n",
  1762. param->value);
  1763. } else if (!strcmp(param->name, RDMAEXTENTIONS)) {
  1764. ops->RDMAExtensions = !strcmp(param->value, YES);
  1765. pr_debug("RDMAExtensions: %s\n",
  1766. param->value);
  1767. }
  1768. }
  1769. pr_debug("----------------------------------------------------"
  1770. "--------------\n");
  1771. }