smb2pdu.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  1. /*
  2. * fs/cifs/smb2pdu.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2009, 2013
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  8. *
  9. * Contains the routines for constructing the SMB2 PDUs themselves
  10. *
  11. * This library is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published
  13. * by the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  19. * the GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
  26. /* Note that there are handle based routines which must be */
  27. /* treated slightly differently for reconnection purposes since we never */
  28. /* want to reuse a stale file handle and only the caller knows the file info */
  29. #include <linux/fs.h>
  30. #include <linux/kernel.h>
  31. #include <linux/vfs.h>
  32. #include <linux/task_io_accounting_ops.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/xattr.h>
  36. #include "smb2pdu.h"
  37. #include "cifsglob.h"
  38. #include "cifsacl.h"
  39. #include "cifsproto.h"
  40. #include "smb2proto.h"
  41. #include "cifs_unicode.h"
  42. #include "cifs_debug.h"
  43. #include "ntlmssp.h"
  44. #include "smb2status.h"
  45. #include "smb2glob.h"
  46. #include "cifspdu.h"
  47. #include "cifs_spnego.h"
  48. /*
  49. * The following table defines the expected "StructureSize" of SMB2 requests
  50. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
  51. *
  52. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  53. * indexed by command in host byte order.
  54. */
  55. static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  56. /* SMB2_NEGOTIATE */ 36,
  57. /* SMB2_SESSION_SETUP */ 25,
  58. /* SMB2_LOGOFF */ 4,
  59. /* SMB2_TREE_CONNECT */ 9,
  60. /* SMB2_TREE_DISCONNECT */ 4,
  61. /* SMB2_CREATE */ 57,
  62. /* SMB2_CLOSE */ 24,
  63. /* SMB2_FLUSH */ 24,
  64. /* SMB2_READ */ 49,
  65. /* SMB2_WRITE */ 49,
  66. /* SMB2_LOCK */ 48,
  67. /* SMB2_IOCTL */ 57,
  68. /* SMB2_CANCEL */ 4,
  69. /* SMB2_ECHO */ 4,
  70. /* SMB2_QUERY_DIRECTORY */ 33,
  71. /* SMB2_CHANGE_NOTIFY */ 32,
  72. /* SMB2_QUERY_INFO */ 41,
  73. /* SMB2_SET_INFO */ 33,
  74. /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
  75. };
  76. static void
  77. smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
  78. const struct cifs_tcon *tcon)
  79. {
  80. struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
  81. char *temp = (char *)hdr;
  82. /* lookup word count ie StructureSize from table */
  83. __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
  84. /*
  85. * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
  86. * largest operations (Create)
  87. */
  88. memset(temp, 0, 256);
  89. /* Note this is only network field converted to big endian */
  90. hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
  91. - 4 /* RFC 1001 length field itself not counted */);
  92. hdr->ProtocolId = SMB2_PROTO_NUMBER;
  93. hdr->StructureSize = cpu_to_le16(64);
  94. hdr->Command = smb2_cmd;
  95. hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
  96. hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
  97. if (!tcon)
  98. goto out;
  99. /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
  100. /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
  101. if ((tcon->ses) && (tcon->ses->server) &&
  102. (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  103. hdr->CreditCharge = cpu_to_le16(1);
  104. /* else CreditCharge MBZ */
  105. hdr->TreeId = tcon->tid;
  106. /* Uid is not converted */
  107. if (tcon->ses)
  108. hdr->SessionId = tcon->ses->Suid;
  109. /*
  110. * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
  111. * to pass the path on the Open SMB prefixed by \\server\share.
  112. * Not sure when we would need to do the augmented path (if ever) and
  113. * setting this flag breaks the SMB2 open operation since it is
  114. * illegal to send an empty path name (without \\server\share prefix)
  115. * when the DFS flag is set in the SMB open header. We could
  116. * consider setting the flag on all operations other than open
  117. * but it is safer to net set it for now.
  118. */
  119. /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
  120. hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
  121. if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
  122. hdr->Flags |= SMB2_FLAGS_SIGNED;
  123. out:
  124. pdu->StructureSize2 = cpu_to_le16(parmsize);
  125. return;
  126. }
  127. static int
  128. smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
  129. {
  130. int rc = 0;
  131. struct nls_table *nls_codepage;
  132. struct cifs_ses *ses;
  133. struct TCP_Server_Info *server;
  134. /*
  135. * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
  136. * check for tcp and smb session status done differently
  137. * for those three - in the calling routine.
  138. */
  139. if (tcon == NULL)
  140. return rc;
  141. if (smb2_command == SMB2_TREE_CONNECT)
  142. return rc;
  143. if (tcon->tidStatus == CifsExiting) {
  144. /*
  145. * only tree disconnect, open, and write,
  146. * (and ulogoff which does not have tcon)
  147. * are allowed as we start force umount.
  148. */
  149. if ((smb2_command != SMB2_WRITE) &&
  150. (smb2_command != SMB2_CREATE) &&
  151. (smb2_command != SMB2_TREE_DISCONNECT)) {
  152. cifs_dbg(FYI, "can not send cmd %d while umounting\n",
  153. smb2_command);
  154. return -ENODEV;
  155. }
  156. }
  157. if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
  158. (!tcon->ses->server))
  159. return -EIO;
  160. ses = tcon->ses;
  161. server = ses->server;
  162. /*
  163. * Give demultiplex thread up to 10 seconds to reconnect, should be
  164. * greater than cifs socket timeout which is 7 seconds
  165. */
  166. while (server->tcpStatus == CifsNeedReconnect) {
  167. /*
  168. * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
  169. * here since they are implicitly done when session drops.
  170. */
  171. switch (smb2_command) {
  172. /*
  173. * BB Should we keep oplock break and add flush to exceptions?
  174. */
  175. case SMB2_TREE_DISCONNECT:
  176. case SMB2_CANCEL:
  177. case SMB2_CLOSE:
  178. case SMB2_OPLOCK_BREAK:
  179. return -EAGAIN;
  180. }
  181. wait_event_interruptible_timeout(server->response_q,
  182. (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
  183. /* are we still trying to reconnect? */
  184. if (server->tcpStatus != CifsNeedReconnect)
  185. break;
  186. /*
  187. * on "soft" mounts we wait once. Hard mounts keep
  188. * retrying until process is killed or server comes
  189. * back on-line
  190. */
  191. if (!tcon->retry) {
  192. cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
  193. return -EHOSTDOWN;
  194. }
  195. }
  196. if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
  197. return rc;
  198. nls_codepage = load_nls_default();
  199. /*
  200. * need to prevent multiple threads trying to simultaneously reconnect
  201. * the same SMB session
  202. */
  203. mutex_lock(&tcon->ses->session_mutex);
  204. rc = cifs_negotiate_protocol(0, tcon->ses);
  205. if (!rc && tcon->ses->need_reconnect)
  206. rc = cifs_setup_session(0, tcon->ses, nls_codepage);
  207. if (rc || !tcon->need_reconnect) {
  208. mutex_unlock(&tcon->ses->session_mutex);
  209. goto out;
  210. }
  211. cifs_mark_open_files_invalid(tcon);
  212. rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
  213. mutex_unlock(&tcon->ses->session_mutex);
  214. cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
  215. if (rc)
  216. goto out;
  217. atomic_inc(&tconInfoReconnectCount);
  218. out:
  219. /*
  220. * Check if handle based operation so we know whether we can continue
  221. * or not without returning to caller to reset file handle.
  222. */
  223. /*
  224. * BB Is flush done by server on drop of tcp session? Should we special
  225. * case it and skip above?
  226. */
  227. switch (smb2_command) {
  228. case SMB2_FLUSH:
  229. case SMB2_READ:
  230. case SMB2_WRITE:
  231. case SMB2_LOCK:
  232. case SMB2_IOCTL:
  233. case SMB2_QUERY_DIRECTORY:
  234. case SMB2_CHANGE_NOTIFY:
  235. case SMB2_QUERY_INFO:
  236. case SMB2_SET_INFO:
  237. return -EAGAIN;
  238. }
  239. unload_nls(nls_codepage);
  240. return rc;
  241. }
  242. /*
  243. * Allocate and return pointer to an SMB request hdr, and set basic
  244. * SMB information in the SMB header. If the return code is zero, this
  245. * function must have filled in request_buf pointer.
  246. */
  247. static int
  248. small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
  249. void **request_buf)
  250. {
  251. int rc = 0;
  252. rc = smb2_reconnect(smb2_command, tcon);
  253. if (rc)
  254. return rc;
  255. /* BB eventually switch this to SMB2 specific small buf size */
  256. *request_buf = cifs_small_buf_get();
  257. if (*request_buf == NULL) {
  258. /* BB should we add a retry in here if not a writepage? */
  259. return -ENOMEM;
  260. }
  261. smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
  262. if (tcon != NULL) {
  263. #ifdef CONFIG_CIFS_STATS2
  264. uint16_t com_code = le16_to_cpu(smb2_command);
  265. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
  266. #endif
  267. cifs_stats_inc(&tcon->num_smbs_sent);
  268. }
  269. return rc;
  270. }
  271. #ifdef CONFIG_CIFS_SMB311
  272. /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
  273. #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
  274. #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
  275. #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
  276. static void
  277. build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
  278. {
  279. pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
  280. pneg_ctxt->DataLength = cpu_to_le16(38);
  281. pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
  282. pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
  283. get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
  284. pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
  285. }
  286. static void
  287. build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
  288. {
  289. pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
  290. pneg_ctxt->DataLength = cpu_to_le16(6);
  291. pneg_ctxt->CipherCount = cpu_to_le16(2);
  292. pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
  293. pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
  294. }
  295. static void
  296. assemble_neg_contexts(struct smb2_negotiate_req *req)
  297. {
  298. /* +4 is to account for the RFC1001 len field */
  299. char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
  300. build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
  301. /* Add 2 to size to round to 8 byte boundary */
  302. pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
  303. build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
  304. req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
  305. req->NegotiateContextCount = cpu_to_le16(2);
  306. inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
  307. + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
  308. }
  309. #else
  310. static void assemble_neg_contexts(struct smb2_negotiate_req *req)
  311. {
  312. return;
  313. }
  314. #endif /* SMB311 */
  315. /*
  316. *
  317. * SMB2 Worker functions follow:
  318. *
  319. * The general structure of the worker functions is:
  320. * 1) Call smb2_init (assembles SMB2 header)
  321. * 2) Initialize SMB2 command specific fields in fixed length area of SMB
  322. * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
  323. * 4) Decode SMB2 command specific fields in the fixed length area
  324. * 5) Decode variable length data area (if any for this SMB2 command type)
  325. * 6) Call free smb buffer
  326. * 7) return
  327. *
  328. */
  329. int
  330. SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  331. {
  332. struct smb2_negotiate_req *req;
  333. struct smb2_negotiate_rsp *rsp;
  334. struct kvec iov[1];
  335. int rc = 0;
  336. int resp_buftype;
  337. struct TCP_Server_Info *server = ses->server;
  338. int blob_offset, blob_length;
  339. char *security_blob;
  340. int flags = CIFS_NEG_OP;
  341. cifs_dbg(FYI, "Negotiate protocol\n");
  342. if (!server) {
  343. WARN(1, "%s: server is NULL!\n", __func__);
  344. return -EIO;
  345. }
  346. rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
  347. if (rc)
  348. return rc;
  349. req->hdr.SessionId = 0;
  350. req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
  351. req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
  352. inc_rfc1001_len(req, 2);
  353. /* only one of SMB2 signing flags may be set in SMB2 request */
  354. if (ses->sign)
  355. req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
  356. else if (global_secflags & CIFSSEC_MAY_SIGN)
  357. req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
  358. else
  359. req->SecurityMode = 0;
  360. req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
  361. /* ClientGUID must be zero for SMB2.02 dialect */
  362. if (ses->server->vals->protocol_id == SMB20_PROT_ID)
  363. memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
  364. else {
  365. memcpy(req->ClientGUID, server->client_guid,
  366. SMB2_CLIENT_GUID_SIZE);
  367. if (ses->server->vals->protocol_id == SMB311_PROT_ID)
  368. assemble_neg_contexts(req);
  369. }
  370. iov[0].iov_base = (char *)req;
  371. /* 4 for rfc1002 length field */
  372. iov[0].iov_len = get_rfc1002_length(req) + 4;
  373. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
  374. rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
  375. /*
  376. * No tcon so can't do
  377. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  378. */
  379. if (rc != 0)
  380. goto neg_exit;
  381. cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
  382. /* BB we may eventually want to match the negotiated vs. requested
  383. dialect, even though we are only requesting one at a time */
  384. if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
  385. cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
  386. else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
  387. cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
  388. else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
  389. cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
  390. else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
  391. cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
  392. #ifdef CONFIG_CIFS_SMB311
  393. else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
  394. cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
  395. #endif /* SMB311 */
  396. else {
  397. cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
  398. le16_to_cpu(rsp->DialectRevision));
  399. rc = -EIO;
  400. goto neg_exit;
  401. }
  402. server->dialect = le16_to_cpu(rsp->DialectRevision);
  403. /* SMB2 only has an extended negflavor */
  404. server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
  405. /* set it to the maximum buffer size value we can send with 1 credit */
  406. server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
  407. SMB2_MAX_BUFFER_SIZE);
  408. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  409. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  410. /* BB Do we need to validate the SecurityMode? */
  411. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  412. server->capabilities = le32_to_cpu(rsp->Capabilities);
  413. /* Internal types */
  414. server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
  415. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  416. &rsp->hdr);
  417. /*
  418. * See MS-SMB2 section 2.2.4: if no blob, client picks default which
  419. * for us will be
  420. * ses->sectype = RawNTLMSSP;
  421. * but for time being this is our only auth choice so doesn't matter.
  422. * We just found a server which sets blob length to zero expecting raw.
  423. */
  424. if (blob_length == 0)
  425. cifs_dbg(FYI, "missing security blob on negprot\n");
  426. rc = cifs_enable_signing(server, ses->sign);
  427. if (rc)
  428. goto neg_exit;
  429. if (blob_length) {
  430. rc = decode_negTokenInit(security_blob, blob_length, server);
  431. if (rc == 1)
  432. rc = 0;
  433. else if (rc == 0)
  434. rc = -EIO;
  435. }
  436. neg_exit:
  437. free_rsp_buf(resp_buftype, rsp);
  438. return rc;
  439. }
  440. int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
  441. {
  442. int rc = 0;
  443. struct validate_negotiate_info_req vneg_inbuf;
  444. struct validate_negotiate_info_rsp *pneg_rsp;
  445. u32 rsplen;
  446. cifs_dbg(FYI, "validate negotiate\n");
  447. /*
  448. * validation ioctl must be signed, so no point sending this if we
  449. * can not sign it. We could eventually change this to selectively
  450. * sign just this, the first and only signed request on a connection.
  451. * This is good enough for now since a user who wants better security
  452. * would also enable signing on the mount. Having validation of
  453. * negotiate info for signed connections helps reduce attack vectors
  454. */
  455. if (tcon->ses->server->sign == false)
  456. return 0; /* validation requires signing */
  457. vneg_inbuf.Capabilities =
  458. cpu_to_le32(tcon->ses->server->vals->req_capabilities);
  459. memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
  460. SMB2_CLIENT_GUID_SIZE);
  461. if (tcon->ses->sign)
  462. vneg_inbuf.SecurityMode =
  463. cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
  464. else if (global_secflags & CIFSSEC_MAY_SIGN)
  465. vneg_inbuf.SecurityMode =
  466. cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
  467. else
  468. vneg_inbuf.SecurityMode = 0;
  469. vneg_inbuf.DialectCount = cpu_to_le16(1);
  470. vneg_inbuf.Dialects[0] =
  471. cpu_to_le16(tcon->ses->server->vals->protocol_id);
  472. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  473. FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
  474. (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
  475. (char **)&pneg_rsp, &rsplen);
  476. if (rc != 0) {
  477. cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
  478. return -EIO;
  479. }
  480. if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
  481. cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
  482. return -EIO;
  483. }
  484. /* check validate negotiate info response matches what we got earlier */
  485. if (pneg_rsp->Dialect !=
  486. cpu_to_le16(tcon->ses->server->vals->protocol_id))
  487. goto vneg_out;
  488. if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
  489. goto vneg_out;
  490. /* do not validate server guid because not saved at negprot time yet */
  491. if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
  492. SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
  493. goto vneg_out;
  494. /* validate negotiate successful */
  495. cifs_dbg(FYI, "validate negotiate info successful\n");
  496. return 0;
  497. vneg_out:
  498. cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
  499. return -EIO;
  500. }
  501. int
  502. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  503. const struct nls_table *nls_cp)
  504. {
  505. struct smb2_sess_setup_req *req;
  506. struct smb2_sess_setup_rsp *rsp = NULL;
  507. struct kvec iov[2];
  508. int rc = 0;
  509. int resp_buftype = CIFS_NO_BUFFER;
  510. __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
  511. struct TCP_Server_Info *server = ses->server;
  512. u16 blob_length = 0;
  513. struct key *spnego_key = NULL;
  514. char *security_blob = NULL;
  515. char *ntlmssp_blob = NULL;
  516. bool use_spnego = false; /* else use raw ntlmssp */
  517. cifs_dbg(FYI, "Session Setup\n");
  518. if (!server) {
  519. WARN(1, "%s: server is NULL!\n", __func__);
  520. return -EIO;
  521. }
  522. /*
  523. * If we are here due to reconnect, free per-smb session key
  524. * in case signing was required.
  525. */
  526. kfree(ses->auth_key.response);
  527. ses->auth_key.response = NULL;
  528. /*
  529. * If memory allocation is successful, caller of this function
  530. * frees it.
  531. */
  532. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  533. if (!ses->ntlmssp)
  534. return -ENOMEM;
  535. ses->ntlmssp->sesskey_per_smbsess = true;
  536. /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
  537. if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
  538. ses->sectype = RawNTLMSSP;
  539. ssetup_ntlmssp_authenticate:
  540. if (phase == NtLmChallenge)
  541. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  542. rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
  543. if (rc)
  544. return rc;
  545. req->hdr.SessionId = 0; /* First session, not a reauthenticate */
  546. req->Flags = 0; /* MBZ */
  547. /* to enable echos and oplocks */
  548. req->hdr.CreditRequest = cpu_to_le16(3);
  549. /* only one of SMB2 signing flags may be set in SMB2 request */
  550. if (server->sign)
  551. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  552. else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
  553. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
  554. else
  555. req->SecurityMode = 0;
  556. req->Capabilities = 0;
  557. req->Channel = 0; /* MBZ */
  558. iov[0].iov_base = (char *)req;
  559. /* 4 for rfc1002 length field and 1 for pad */
  560. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  561. if (ses->sectype == Kerberos) {
  562. #ifdef CONFIG_CIFS_UPCALL
  563. struct cifs_spnego_msg *msg;
  564. spnego_key = cifs_get_spnego_key(ses);
  565. if (IS_ERR(spnego_key)) {
  566. rc = PTR_ERR(spnego_key);
  567. spnego_key = NULL;
  568. goto ssetup_exit;
  569. }
  570. msg = spnego_key->payload.data[0];
  571. /*
  572. * check version field to make sure that cifs.upcall is
  573. * sending us a response in an expected form
  574. */
  575. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  576. cifs_dbg(VFS,
  577. "bad cifs.upcall version. Expected %d got %d",
  578. CIFS_SPNEGO_UPCALL_VERSION, msg->version);
  579. rc = -EKEYREJECTED;
  580. goto ssetup_exit;
  581. }
  582. ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
  583. GFP_KERNEL);
  584. if (!ses->auth_key.response) {
  585. cifs_dbg(VFS,
  586. "Kerberos can't allocate (%u bytes) memory",
  587. msg->sesskey_len);
  588. rc = -ENOMEM;
  589. goto ssetup_exit;
  590. }
  591. ses->auth_key.len = msg->sesskey_len;
  592. blob_length = msg->secblob_len;
  593. iov[1].iov_base = msg->data + msg->sesskey_len;
  594. iov[1].iov_len = blob_length;
  595. #else
  596. rc = -EOPNOTSUPP;
  597. goto ssetup_exit;
  598. #endif /* CONFIG_CIFS_UPCALL */
  599. } else if (phase == NtLmNegotiate) { /* if not krb5 must be ntlmssp */
  600. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  601. GFP_KERNEL);
  602. if (ntlmssp_blob == NULL) {
  603. rc = -ENOMEM;
  604. goto ssetup_exit;
  605. }
  606. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  607. if (use_spnego) {
  608. /* blob_length = build_spnego_ntlmssp_blob(
  609. &security_blob,
  610. sizeof(struct _NEGOTIATE_MESSAGE),
  611. ntlmssp_blob); */
  612. /* BB eventually need to add this */
  613. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  614. rc = -EOPNOTSUPP;
  615. kfree(ntlmssp_blob);
  616. goto ssetup_exit;
  617. } else {
  618. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  619. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  620. security_blob = ntlmssp_blob;
  621. }
  622. iov[1].iov_base = security_blob;
  623. iov[1].iov_len = blob_length;
  624. } else if (phase == NtLmAuthenticate) {
  625. req->hdr.SessionId = ses->Suid;
  626. ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
  627. GFP_KERNEL);
  628. if (ntlmssp_blob == NULL) {
  629. rc = -ENOMEM;
  630. goto ssetup_exit;
  631. }
  632. rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
  633. nls_cp);
  634. if (rc) {
  635. cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
  636. rc);
  637. goto ssetup_exit; /* BB double check error handling */
  638. }
  639. if (use_spnego) {
  640. /* blob_length = build_spnego_ntlmssp_blob(
  641. &security_blob,
  642. blob_length,
  643. ntlmssp_blob); */
  644. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  645. rc = -EOPNOTSUPP;
  646. kfree(ntlmssp_blob);
  647. goto ssetup_exit;
  648. } else {
  649. security_blob = ntlmssp_blob;
  650. }
  651. iov[1].iov_base = security_blob;
  652. iov[1].iov_len = blob_length;
  653. } else {
  654. cifs_dbg(VFS, "illegal ntlmssp phase\n");
  655. rc = -EIO;
  656. goto ssetup_exit;
  657. }
  658. /* Testing shows that buffer offset must be at location of Buffer[0] */
  659. req->SecurityBufferOffset =
  660. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  661. 1 /* pad */ - 4 /* rfc1001 len */);
  662. req->SecurityBufferLength = cpu_to_le16(blob_length);
  663. inc_rfc1001_len(req, blob_length - 1 /* pad */);
  664. /* BB add code to build os and lm fields */
  665. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
  666. CIFS_LOG_ERROR | CIFS_NEG_OP);
  667. kfree(security_blob);
  668. rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
  669. ses->Suid = rsp->hdr.SessionId;
  670. if (resp_buftype != CIFS_NO_BUFFER &&
  671. rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
  672. if (phase != NtLmNegotiate) {
  673. cifs_dbg(VFS, "Unexpected more processing error\n");
  674. goto ssetup_exit;
  675. }
  676. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  677. le16_to_cpu(rsp->SecurityBufferOffset)) {
  678. cifs_dbg(VFS, "Invalid security buffer offset %d\n",
  679. le16_to_cpu(rsp->SecurityBufferOffset));
  680. rc = -EIO;
  681. goto ssetup_exit;
  682. }
  683. /* NTLMSSP Negotiate sent now processing challenge (response) */
  684. phase = NtLmChallenge; /* process ntlmssp challenge */
  685. rc = 0; /* MORE_PROCESSING is not an error here but expected */
  686. rc = decode_ntlmssp_challenge(rsp->Buffer,
  687. le16_to_cpu(rsp->SecurityBufferLength), ses);
  688. }
  689. /*
  690. * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
  691. * but at least the raw NTLMSSP case works.
  692. */
  693. /*
  694. * No tcon so can't do
  695. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  696. */
  697. if (rc != 0)
  698. goto ssetup_exit;
  699. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  700. if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
  701. cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
  702. ssetup_exit:
  703. free_rsp_buf(resp_buftype, rsp);
  704. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  705. if ((phase == NtLmChallenge) && (rc == 0))
  706. goto ssetup_ntlmssp_authenticate;
  707. if (!rc) {
  708. mutex_lock(&server->srv_mutex);
  709. if (server->sign && server->ops->generate_signingkey) {
  710. rc = server->ops->generate_signingkey(ses);
  711. kfree(ses->auth_key.response);
  712. ses->auth_key.response = NULL;
  713. if (rc) {
  714. cifs_dbg(FYI,
  715. "SMB3 session key generation failed\n");
  716. mutex_unlock(&server->srv_mutex);
  717. goto keygen_exit;
  718. }
  719. }
  720. if (!server->session_estab) {
  721. server->sequence_number = 0x2;
  722. server->session_estab = true;
  723. }
  724. mutex_unlock(&server->srv_mutex);
  725. cifs_dbg(FYI, "SMB2/3 session established successfully\n");
  726. spin_lock(&GlobalMid_Lock);
  727. ses->status = CifsGood;
  728. ses->need_reconnect = false;
  729. spin_unlock(&GlobalMid_Lock);
  730. }
  731. keygen_exit:
  732. if (!server->sign) {
  733. kfree(ses->auth_key.response);
  734. ses->auth_key.response = NULL;
  735. }
  736. if (spnego_key) {
  737. key_invalidate(spnego_key);
  738. key_put(spnego_key);
  739. }
  740. kfree(ses->ntlmssp);
  741. return rc;
  742. }
  743. int
  744. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  745. {
  746. struct smb2_logoff_req *req; /* response is also trivial struct */
  747. int rc = 0;
  748. struct TCP_Server_Info *server;
  749. cifs_dbg(FYI, "disconnect session %p\n", ses);
  750. if (ses && (ses->server))
  751. server = ses->server;
  752. else
  753. return -EIO;
  754. /* no need to send SMB logoff if uid already closed due to reconnect */
  755. if (ses->need_reconnect)
  756. goto smb2_session_already_dead;
  757. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  758. if (rc)
  759. return rc;
  760. /* since no tcon, smb2_init can not do this, so do here */
  761. req->hdr.SessionId = ses->Suid;
  762. if (server->sign)
  763. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  764. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  765. /*
  766. * No tcon so can't do
  767. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  768. */
  769. smb2_session_already_dead:
  770. return rc;
  771. }
  772. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  773. {
  774. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  775. }
  776. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  777. /* These are similar values to what Windows uses */
  778. static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
  779. {
  780. tcon->max_chunks = 256;
  781. tcon->max_bytes_chunk = 1048576;
  782. tcon->max_bytes_copy = 16777216;
  783. }
  784. int
  785. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  786. struct cifs_tcon *tcon, const struct nls_table *cp)
  787. {
  788. struct smb2_tree_connect_req *req;
  789. struct smb2_tree_connect_rsp *rsp = NULL;
  790. struct kvec iov[2];
  791. int rc = 0;
  792. int resp_buftype;
  793. int unc_path_len;
  794. struct TCP_Server_Info *server;
  795. __le16 *unc_path = NULL;
  796. cifs_dbg(FYI, "TCON\n");
  797. if ((ses->server) && tree)
  798. server = ses->server;
  799. else
  800. return -EIO;
  801. if (tcon && tcon->bad_network_name)
  802. return -ENOENT;
  803. if ((tcon && tcon->seal) &&
  804. ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
  805. cifs_dbg(VFS, "encryption requested but no server support");
  806. return -EOPNOTSUPP;
  807. }
  808. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  809. if (unc_path == NULL)
  810. return -ENOMEM;
  811. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  812. unc_path_len *= 2;
  813. if (unc_path_len < 2) {
  814. kfree(unc_path);
  815. return -EINVAL;
  816. }
  817. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  818. if (rc) {
  819. kfree(unc_path);
  820. return rc;
  821. }
  822. if (tcon == NULL) {
  823. /* since no tcon, smb2_init can not do this, so do here */
  824. req->hdr.SessionId = ses->Suid;
  825. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  826. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  827. }
  828. iov[0].iov_base = (char *)req;
  829. /* 4 for rfc1002 length field and 1 for pad */
  830. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  831. /* Testing shows that buffer offset must be at location of Buffer[0] */
  832. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  833. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  834. req->PathLength = cpu_to_le16(unc_path_len - 2);
  835. iov[1].iov_base = unc_path;
  836. iov[1].iov_len = unc_path_len;
  837. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  838. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  839. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  840. if (rc != 0) {
  841. if (tcon) {
  842. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  843. tcon->need_reconnect = true;
  844. }
  845. goto tcon_error_exit;
  846. }
  847. if (tcon == NULL) {
  848. ses->ipc_tid = rsp->hdr.TreeId;
  849. goto tcon_exit;
  850. }
  851. if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
  852. cifs_dbg(FYI, "connection to disk share\n");
  853. else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
  854. tcon->ipc = true;
  855. cifs_dbg(FYI, "connection to pipe share\n");
  856. } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
  857. tcon->print = true;
  858. cifs_dbg(FYI, "connection to printer\n");
  859. } else {
  860. cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
  861. rc = -EOPNOTSUPP;
  862. goto tcon_error_exit;
  863. }
  864. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  865. tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
  866. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  867. tcon->tidStatus = CifsGood;
  868. tcon->need_reconnect = false;
  869. tcon->tid = rsp->hdr.TreeId;
  870. strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
  871. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  872. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  873. cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
  874. init_copy_chunk_defaults(tcon);
  875. if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
  876. cifs_dbg(VFS, "Encrypted shares not supported");
  877. if (tcon->ses->server->ops->validate_negotiate)
  878. rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
  879. tcon_exit:
  880. free_rsp_buf(resp_buftype, rsp);
  881. kfree(unc_path);
  882. return rc;
  883. tcon_error_exit:
  884. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  885. cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
  886. if (tcon)
  887. tcon->bad_network_name = true;
  888. }
  889. goto tcon_exit;
  890. }
  891. int
  892. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  893. {
  894. struct smb2_tree_disconnect_req *req; /* response is trivial */
  895. int rc = 0;
  896. struct TCP_Server_Info *server;
  897. struct cifs_ses *ses = tcon->ses;
  898. cifs_dbg(FYI, "Tree Disconnect\n");
  899. if (ses && (ses->server))
  900. server = ses->server;
  901. else
  902. return -EIO;
  903. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  904. return 0;
  905. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  906. if (rc)
  907. return rc;
  908. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  909. if (rc)
  910. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  911. return rc;
  912. }
  913. static struct create_durable *
  914. create_durable_buf(void)
  915. {
  916. struct create_durable *buf;
  917. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  918. if (!buf)
  919. return NULL;
  920. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  921. (struct create_durable, Data));
  922. buf->ccontext.DataLength = cpu_to_le32(16);
  923. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  924. (struct create_durable, Name));
  925. buf->ccontext.NameLength = cpu_to_le16(4);
  926. /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
  927. buf->Name[0] = 'D';
  928. buf->Name[1] = 'H';
  929. buf->Name[2] = 'n';
  930. buf->Name[3] = 'Q';
  931. return buf;
  932. }
  933. static struct create_durable *
  934. create_reconnect_durable_buf(struct cifs_fid *fid)
  935. {
  936. struct create_durable *buf;
  937. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  938. if (!buf)
  939. return NULL;
  940. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  941. (struct create_durable, Data));
  942. buf->ccontext.DataLength = cpu_to_le32(16);
  943. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  944. (struct create_durable, Name));
  945. buf->ccontext.NameLength = cpu_to_le16(4);
  946. buf->Data.Fid.PersistentFileId = fid->persistent_fid;
  947. buf->Data.Fid.VolatileFileId = fid->volatile_fid;
  948. /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
  949. buf->Name[0] = 'D';
  950. buf->Name[1] = 'H';
  951. buf->Name[2] = 'n';
  952. buf->Name[3] = 'C';
  953. return buf;
  954. }
  955. static __u8
  956. parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
  957. unsigned int *epoch)
  958. {
  959. char *data_offset;
  960. struct create_context *cc;
  961. unsigned int next = 0;
  962. char *name;
  963. data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
  964. cc = (struct create_context *)data_offset;
  965. do {
  966. cc = (struct create_context *)((char *)cc + next);
  967. name = le16_to_cpu(cc->NameOffset) + (char *)cc;
  968. if (le16_to_cpu(cc->NameLength) != 4 ||
  969. strncmp(name, "RqLs", 4)) {
  970. next = le32_to_cpu(cc->Next);
  971. continue;
  972. }
  973. return server->ops->parse_lease_buf(cc, epoch);
  974. } while (next != 0);
  975. return 0;
  976. }
  977. static int
  978. add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
  979. unsigned int *num_iovec, __u8 *oplock)
  980. {
  981. struct smb2_create_req *req = iov[0].iov_base;
  982. unsigned int num = *num_iovec;
  983. iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
  984. if (iov[num].iov_base == NULL)
  985. return -ENOMEM;
  986. iov[num].iov_len = server->vals->create_lease_size;
  987. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  988. if (!req->CreateContextsOffset)
  989. req->CreateContextsOffset = cpu_to_le32(
  990. sizeof(struct smb2_create_req) - 4 +
  991. iov[num - 1].iov_len);
  992. le32_add_cpu(&req->CreateContextsLength,
  993. server->vals->create_lease_size);
  994. inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
  995. *num_iovec = num + 1;
  996. return 0;
  997. }
  998. static struct create_durable_v2 *
  999. create_durable_v2_buf(struct cifs_fid *pfid)
  1000. {
  1001. struct create_durable_v2 *buf;
  1002. buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
  1003. if (!buf)
  1004. return NULL;
  1005. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1006. (struct create_durable_v2, dcontext));
  1007. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
  1008. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1009. (struct create_durable_v2, Name));
  1010. buf->ccontext.NameLength = cpu_to_le16(4);
  1011. buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
  1012. buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
  1013. get_random_bytes(buf->dcontext.CreateGuid, 16);
  1014. memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
  1015. /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
  1016. buf->Name[0] = 'D';
  1017. buf->Name[1] = 'H';
  1018. buf->Name[2] = '2';
  1019. buf->Name[3] = 'Q';
  1020. return buf;
  1021. }
  1022. static struct create_durable_handle_reconnect_v2 *
  1023. create_reconnect_durable_v2_buf(struct cifs_fid *fid)
  1024. {
  1025. struct create_durable_handle_reconnect_v2 *buf;
  1026. buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
  1027. GFP_KERNEL);
  1028. if (!buf)
  1029. return NULL;
  1030. buf->ccontext.DataOffset =
  1031. cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
  1032. dcontext));
  1033. buf->ccontext.DataLength =
  1034. cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
  1035. buf->ccontext.NameOffset =
  1036. cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
  1037. Name));
  1038. buf->ccontext.NameLength = cpu_to_le16(4);
  1039. buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
  1040. buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
  1041. buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
  1042. memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
  1043. /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
  1044. buf->Name[0] = 'D';
  1045. buf->Name[1] = 'H';
  1046. buf->Name[2] = '2';
  1047. buf->Name[3] = 'C';
  1048. return buf;
  1049. }
  1050. static int
  1051. add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
  1052. struct cifs_open_parms *oparms)
  1053. {
  1054. struct smb2_create_req *req = iov[0].iov_base;
  1055. unsigned int num = *num_iovec;
  1056. iov[num].iov_base = create_durable_v2_buf(oparms->fid);
  1057. if (iov[num].iov_base == NULL)
  1058. return -ENOMEM;
  1059. iov[num].iov_len = sizeof(struct create_durable_v2);
  1060. if (!req->CreateContextsOffset)
  1061. req->CreateContextsOffset =
  1062. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  1063. iov[1].iov_len);
  1064. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
  1065. inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
  1066. *num_iovec = num + 1;
  1067. return 0;
  1068. }
  1069. static int
  1070. add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
  1071. struct cifs_open_parms *oparms)
  1072. {
  1073. struct smb2_create_req *req = iov[0].iov_base;
  1074. unsigned int num = *num_iovec;
  1075. /* indicate that we don't need to relock the file */
  1076. oparms->reconnect = false;
  1077. iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
  1078. if (iov[num].iov_base == NULL)
  1079. return -ENOMEM;
  1080. iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
  1081. if (!req->CreateContextsOffset)
  1082. req->CreateContextsOffset =
  1083. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  1084. iov[1].iov_len);
  1085. le32_add_cpu(&req->CreateContextsLength,
  1086. sizeof(struct create_durable_handle_reconnect_v2));
  1087. inc_rfc1001_len(&req->hdr,
  1088. sizeof(struct create_durable_handle_reconnect_v2));
  1089. *num_iovec = num + 1;
  1090. return 0;
  1091. }
  1092. static int
  1093. add_durable_context(struct kvec *iov, unsigned int *num_iovec,
  1094. struct cifs_open_parms *oparms, bool use_persistent)
  1095. {
  1096. struct smb2_create_req *req = iov[0].iov_base;
  1097. unsigned int num = *num_iovec;
  1098. if (use_persistent) {
  1099. if (oparms->reconnect)
  1100. return add_durable_reconnect_v2_context(iov, num_iovec,
  1101. oparms);
  1102. else
  1103. return add_durable_v2_context(iov, num_iovec, oparms);
  1104. }
  1105. if (oparms->reconnect) {
  1106. iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
  1107. /* indicate that we don't need to relock the file */
  1108. oparms->reconnect = false;
  1109. } else
  1110. iov[num].iov_base = create_durable_buf();
  1111. if (iov[num].iov_base == NULL)
  1112. return -ENOMEM;
  1113. iov[num].iov_len = sizeof(struct create_durable);
  1114. if (!req->CreateContextsOffset)
  1115. req->CreateContextsOffset =
  1116. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  1117. iov[1].iov_len);
  1118. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
  1119. inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
  1120. *num_iovec = num + 1;
  1121. return 0;
  1122. }
  1123. int
  1124. SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
  1125. __u8 *oplock, struct smb2_file_all_info *buf,
  1126. struct smb2_err_rsp **err_buf)
  1127. {
  1128. struct smb2_create_req *req;
  1129. struct smb2_create_rsp *rsp;
  1130. struct TCP_Server_Info *server;
  1131. struct cifs_tcon *tcon = oparms->tcon;
  1132. struct cifs_ses *ses = tcon->ses;
  1133. struct kvec iov[4];
  1134. int resp_buftype;
  1135. int uni_path_len;
  1136. __le16 *copy_path = NULL;
  1137. int copy_size;
  1138. int rc = 0;
  1139. unsigned int num_iovecs = 2;
  1140. __u32 file_attributes = 0;
  1141. char *dhc_buf = NULL, *lc_buf = NULL;
  1142. cifs_dbg(FYI, "create/open\n");
  1143. if (ses && (ses->server))
  1144. server = ses->server;
  1145. else
  1146. return -EIO;
  1147. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  1148. if (rc)
  1149. return rc;
  1150. if (oparms->create_options & CREATE_OPTION_READONLY)
  1151. file_attributes |= ATTR_READONLY;
  1152. if (oparms->create_options & CREATE_OPTION_SPECIAL)
  1153. file_attributes |= ATTR_SYSTEM;
  1154. req->ImpersonationLevel = IL_IMPERSONATION;
  1155. req->DesiredAccess = cpu_to_le32(oparms->desired_access);
  1156. /* File attributes ignored on open (used in create though) */
  1157. req->FileAttributes = cpu_to_le32(file_attributes);
  1158. req->ShareAccess = FILE_SHARE_ALL_LE;
  1159. req->CreateDisposition = cpu_to_le32(oparms->disposition);
  1160. req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
  1161. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  1162. /* do not count rfc1001 len field */
  1163. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
  1164. iov[0].iov_base = (char *)req;
  1165. /* 4 for rfc1002 length field */
  1166. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1167. /* MUST set path len (NameLength) to 0 opening root of share */
  1168. req->NameLength = cpu_to_le16(uni_path_len - 2);
  1169. /* -1 since last byte is buf[0] which is sent below (path) */
  1170. iov[0].iov_len--;
  1171. if (uni_path_len % 8 != 0) {
  1172. copy_size = uni_path_len / 8 * 8;
  1173. if (copy_size < uni_path_len)
  1174. copy_size += 8;
  1175. copy_path = kzalloc(copy_size, GFP_KERNEL);
  1176. if (!copy_path)
  1177. return -ENOMEM;
  1178. memcpy((char *)copy_path, (const char *)path,
  1179. uni_path_len);
  1180. uni_path_len = copy_size;
  1181. path = copy_path;
  1182. }
  1183. iov[1].iov_len = uni_path_len;
  1184. iov[1].iov_base = path;
  1185. /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
  1186. inc_rfc1001_len(req, uni_path_len - 1);
  1187. if (!server->oplocks)
  1188. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  1189. if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  1190. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  1191. req->RequestedOplockLevel = *oplock;
  1192. else {
  1193. rc = add_lease_context(server, iov, &num_iovecs, oplock);
  1194. if (rc) {
  1195. cifs_small_buf_release(req);
  1196. kfree(copy_path);
  1197. return rc;
  1198. }
  1199. lc_buf = iov[num_iovecs-1].iov_base;
  1200. }
  1201. if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  1202. /* need to set Next field of lease context if we request it */
  1203. if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
  1204. struct create_context *ccontext =
  1205. (struct create_context *)iov[num_iovecs-1].iov_base;
  1206. ccontext->Next =
  1207. cpu_to_le32(server->vals->create_lease_size);
  1208. }
  1209. rc = add_durable_context(iov, &num_iovecs, oparms,
  1210. tcon->use_persistent);
  1211. if (rc) {
  1212. cifs_small_buf_release(req);
  1213. kfree(copy_path);
  1214. kfree(lc_buf);
  1215. return rc;
  1216. }
  1217. dhc_buf = iov[num_iovecs-1].iov_base;
  1218. }
  1219. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  1220. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  1221. if (rc != 0) {
  1222. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  1223. if (err_buf)
  1224. *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
  1225. GFP_KERNEL);
  1226. goto creat_exit;
  1227. }
  1228. oparms->fid->persistent_fid = rsp->PersistentFileId;
  1229. oparms->fid->volatile_fid = rsp->VolatileFileId;
  1230. if (buf) {
  1231. memcpy(buf, &rsp->CreationTime, 32);
  1232. buf->AllocationSize = rsp->AllocationSize;
  1233. buf->EndOfFile = rsp->EndofFile;
  1234. buf->Attributes = rsp->FileAttributes;
  1235. buf->NumberOfLinks = cpu_to_le32(1);
  1236. buf->DeletePending = 0;
  1237. }
  1238. if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
  1239. *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
  1240. else
  1241. *oplock = rsp->OplockLevel;
  1242. creat_exit:
  1243. kfree(copy_path);
  1244. kfree(lc_buf);
  1245. kfree(dhc_buf);
  1246. free_rsp_buf(resp_buftype, rsp);
  1247. return rc;
  1248. }
  1249. /*
  1250. * SMB2 IOCTL is used for both IOCTLs and FSCTLs
  1251. */
  1252. int
  1253. SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1254. u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
  1255. u32 indatalen, char **out_data, u32 *plen /* returned data len */)
  1256. {
  1257. struct smb2_ioctl_req *req;
  1258. struct smb2_ioctl_rsp *rsp;
  1259. struct TCP_Server_Info *server;
  1260. struct cifs_ses *ses;
  1261. struct kvec iov[2];
  1262. int resp_buftype;
  1263. int num_iovecs;
  1264. int rc = 0;
  1265. cifs_dbg(FYI, "SMB2 IOCTL\n");
  1266. if (out_data != NULL)
  1267. *out_data = NULL;
  1268. /* zero out returned data len, in case of error */
  1269. if (plen)
  1270. *plen = 0;
  1271. if (tcon)
  1272. ses = tcon->ses;
  1273. else
  1274. return -EIO;
  1275. if (ses && (ses->server))
  1276. server = ses->server;
  1277. else
  1278. return -EIO;
  1279. rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
  1280. if (rc)
  1281. return rc;
  1282. req->CtlCode = cpu_to_le32(opcode);
  1283. req->PersistentFileId = persistent_fid;
  1284. req->VolatileFileId = volatile_fid;
  1285. if (indatalen) {
  1286. req->InputCount = cpu_to_le32(indatalen);
  1287. /* do not set InputOffset if no input data */
  1288. req->InputOffset =
  1289. cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
  1290. iov[1].iov_base = in_data;
  1291. iov[1].iov_len = indatalen;
  1292. num_iovecs = 2;
  1293. } else
  1294. num_iovecs = 1;
  1295. req->OutputOffset = 0;
  1296. req->OutputCount = 0; /* MBZ */
  1297. /*
  1298. * Could increase MaxOutputResponse, but that would require more
  1299. * than one credit. Windows typically sets this smaller, but for some
  1300. * ioctls it may be useful to allow server to send more. No point
  1301. * limiting what the server can send as long as fits in one credit
  1302. */
  1303. req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
  1304. if (is_fsctl)
  1305. req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
  1306. else
  1307. req->Flags = 0;
  1308. iov[0].iov_base = (char *)req;
  1309. /*
  1310. * If no input data, the size of ioctl struct in
  1311. * protocol spec still includes a 1 byte data buffer,
  1312. * but if input data passed to ioctl, we do not
  1313. * want to double count this, so we do not send
  1314. * the dummy one byte of data in iovec[0] if sending
  1315. * input data (in iovec[1]). We also must add 4 bytes
  1316. * in first iovec to allow for rfc1002 length field.
  1317. */
  1318. if (indatalen) {
  1319. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1320. inc_rfc1001_len(req, indatalen - 1);
  1321. } else
  1322. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1323. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  1324. rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
  1325. if ((rc != 0) && (rc != -EINVAL)) {
  1326. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  1327. goto ioctl_exit;
  1328. } else if (rc == -EINVAL) {
  1329. if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
  1330. (opcode != FSCTL_SRV_COPYCHUNK)) {
  1331. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  1332. goto ioctl_exit;
  1333. }
  1334. }
  1335. /* check if caller wants to look at return data or just return rc */
  1336. if ((plen == NULL) || (out_data == NULL))
  1337. goto ioctl_exit;
  1338. *plen = le32_to_cpu(rsp->OutputCount);
  1339. /* We check for obvious errors in the output buffer length and offset */
  1340. if (*plen == 0)
  1341. goto ioctl_exit; /* server returned no data */
  1342. else if (*plen > 0xFF00) {
  1343. cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
  1344. *plen = 0;
  1345. rc = -EIO;
  1346. goto ioctl_exit;
  1347. }
  1348. if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
  1349. cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
  1350. le32_to_cpu(rsp->OutputOffset));
  1351. *plen = 0;
  1352. rc = -EIO;
  1353. goto ioctl_exit;
  1354. }
  1355. *out_data = kmalloc(*plen, GFP_KERNEL);
  1356. if (*out_data == NULL) {
  1357. rc = -ENOMEM;
  1358. goto ioctl_exit;
  1359. }
  1360. memcpy(*out_data,
  1361. (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
  1362. *plen);
  1363. ioctl_exit:
  1364. free_rsp_buf(resp_buftype, rsp);
  1365. return rc;
  1366. }
  1367. /*
  1368. * Individual callers to ioctl worker function follow
  1369. */
  1370. int
  1371. SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  1372. u64 persistent_fid, u64 volatile_fid)
  1373. {
  1374. int rc;
  1375. struct compress_ioctl fsctl_input;
  1376. char *ret_data = NULL;
  1377. fsctl_input.CompressionState =
  1378. cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
  1379. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  1380. FSCTL_SET_COMPRESSION, true /* is_fsctl */,
  1381. (char *)&fsctl_input /* data input */,
  1382. 2 /* in data len */, &ret_data /* out data */, NULL);
  1383. cifs_dbg(FYI, "set compression rc %d\n", rc);
  1384. return rc;
  1385. }
  1386. int
  1387. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  1388. u64 persistent_fid, u64 volatile_fid)
  1389. {
  1390. struct smb2_close_req *req;
  1391. struct smb2_close_rsp *rsp;
  1392. struct TCP_Server_Info *server;
  1393. struct cifs_ses *ses = tcon->ses;
  1394. struct kvec iov[1];
  1395. int resp_buftype;
  1396. int rc = 0;
  1397. cifs_dbg(FYI, "Close\n");
  1398. if (ses && (ses->server))
  1399. server = ses->server;
  1400. else
  1401. return -EIO;
  1402. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  1403. if (rc)
  1404. return rc;
  1405. req->PersistentFileId = persistent_fid;
  1406. req->VolatileFileId = volatile_fid;
  1407. iov[0].iov_base = (char *)req;
  1408. /* 4 for rfc1002 length field */
  1409. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1410. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1411. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  1412. if (rc != 0) {
  1413. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  1414. goto close_exit;
  1415. }
  1416. /* BB FIXME - decode close response, update inode for caching */
  1417. close_exit:
  1418. free_rsp_buf(resp_buftype, rsp);
  1419. return rc;
  1420. }
  1421. static int
  1422. validate_buf(unsigned int offset, unsigned int buffer_length,
  1423. struct smb2_hdr *hdr, unsigned int min_buf_size)
  1424. {
  1425. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  1426. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  1427. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1428. char *end_of_buf = begin_of_buf + buffer_length;
  1429. if (buffer_length < min_buf_size) {
  1430. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  1431. buffer_length, min_buf_size);
  1432. return -EINVAL;
  1433. }
  1434. /* check if beyond RFC1001 maximum length */
  1435. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  1436. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  1437. buffer_length, smb_len);
  1438. return -EINVAL;
  1439. }
  1440. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  1441. cifs_dbg(VFS, "illegal server response, bad offset to data\n");
  1442. return -EINVAL;
  1443. }
  1444. return 0;
  1445. }
  1446. /*
  1447. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  1448. * Caller must free buffer.
  1449. */
  1450. static int
  1451. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  1452. struct smb2_hdr *hdr, unsigned int minbufsize,
  1453. char *data)
  1454. {
  1455. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1456. int rc;
  1457. if (!data)
  1458. return -EINVAL;
  1459. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  1460. if (rc)
  1461. return rc;
  1462. memcpy(data, begin_of_buf, buffer_length);
  1463. return 0;
  1464. }
  1465. static int
  1466. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1467. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  1468. size_t output_len, size_t min_len, void *data)
  1469. {
  1470. struct smb2_query_info_req *req;
  1471. struct smb2_query_info_rsp *rsp = NULL;
  1472. struct kvec iov[2];
  1473. int rc = 0;
  1474. int resp_buftype;
  1475. struct TCP_Server_Info *server;
  1476. struct cifs_ses *ses = tcon->ses;
  1477. cifs_dbg(FYI, "Query Info\n");
  1478. if (ses && (ses->server))
  1479. server = ses->server;
  1480. else
  1481. return -EIO;
  1482. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1483. if (rc)
  1484. return rc;
  1485. req->InfoType = SMB2_O_INFO_FILE;
  1486. req->FileInfoClass = info_class;
  1487. req->PersistentFileId = persistent_fid;
  1488. req->VolatileFileId = volatile_fid;
  1489. /* 4 for rfc1002 length field and 1 for Buffer */
  1490. req->InputBufferOffset =
  1491. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1492. req->OutputBufferLength = cpu_to_le32(output_len);
  1493. iov[0].iov_base = (char *)req;
  1494. /* 4 for rfc1002 length field */
  1495. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1496. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1497. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  1498. if (rc) {
  1499. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1500. goto qinf_exit;
  1501. }
  1502. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1503. le32_to_cpu(rsp->OutputBufferLength),
  1504. &rsp->hdr, min_len, data);
  1505. qinf_exit:
  1506. free_rsp_buf(resp_buftype, rsp);
  1507. return rc;
  1508. }
  1509. int
  1510. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1511. u64 persistent_fid, u64 volatile_fid,
  1512. struct smb2_file_all_info *data)
  1513. {
  1514. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1515. FILE_ALL_INFORMATION,
  1516. sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
  1517. sizeof(struct smb2_file_all_info), data);
  1518. }
  1519. int
  1520. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  1521. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  1522. {
  1523. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1524. FILE_INTERNAL_INFORMATION,
  1525. sizeof(struct smb2_file_internal_info),
  1526. sizeof(struct smb2_file_internal_info), uniqueid);
  1527. }
  1528. /*
  1529. * This is a no-op for now. We're not really interested in the reply, but
  1530. * rather in the fact that the server sent one and that server->lstrp
  1531. * gets updated.
  1532. *
  1533. * FIXME: maybe we should consider checking that the reply matches request?
  1534. */
  1535. static void
  1536. smb2_echo_callback(struct mid_q_entry *mid)
  1537. {
  1538. struct TCP_Server_Info *server = mid->callback_data;
  1539. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  1540. unsigned int credits_received = 1;
  1541. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  1542. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  1543. mutex_lock(&server->srv_mutex);
  1544. DeleteMidQEntry(mid);
  1545. mutex_unlock(&server->srv_mutex);
  1546. add_credits(server, credits_received, CIFS_ECHO_OP);
  1547. }
  1548. int
  1549. SMB2_echo(struct TCP_Server_Info *server)
  1550. {
  1551. struct smb2_echo_req *req;
  1552. int rc = 0;
  1553. struct kvec iov;
  1554. struct smb_rqst rqst = { .rq_iov = &iov,
  1555. .rq_nvec = 1 };
  1556. cifs_dbg(FYI, "In echo request\n");
  1557. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1558. if (rc)
  1559. return rc;
  1560. req->hdr.CreditRequest = cpu_to_le16(1);
  1561. iov.iov_base = (char *)req;
  1562. /* 4 for rfc1002 length field */
  1563. iov.iov_len = get_rfc1002_length(req) + 4;
  1564. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
  1565. CIFS_ECHO_OP);
  1566. if (rc)
  1567. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  1568. cifs_small_buf_release(req);
  1569. return rc;
  1570. }
  1571. int
  1572. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1573. u64 volatile_fid)
  1574. {
  1575. struct smb2_flush_req *req;
  1576. struct TCP_Server_Info *server;
  1577. struct cifs_ses *ses = tcon->ses;
  1578. struct kvec iov[1];
  1579. int resp_buftype;
  1580. int rc = 0;
  1581. cifs_dbg(FYI, "Flush\n");
  1582. if (ses && (ses->server))
  1583. server = ses->server;
  1584. else
  1585. return -EIO;
  1586. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1587. if (rc)
  1588. return rc;
  1589. req->PersistentFileId = persistent_fid;
  1590. req->VolatileFileId = volatile_fid;
  1591. iov[0].iov_base = (char *)req;
  1592. /* 4 for rfc1002 length field */
  1593. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1594. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1595. if (rc != 0)
  1596. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1597. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1598. return rc;
  1599. }
  1600. /*
  1601. * To form a chain of read requests, any read requests after the first should
  1602. * have the end_of_chain boolean set to true.
  1603. */
  1604. static int
  1605. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1606. unsigned int remaining_bytes, int request_type)
  1607. {
  1608. int rc = -EACCES;
  1609. struct smb2_read_req *req = NULL;
  1610. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1611. if (rc)
  1612. return rc;
  1613. if (io_parms->tcon->ses->server == NULL)
  1614. return -ECONNABORTED;
  1615. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1616. req->PersistentFileId = io_parms->persistent_fid;
  1617. req->VolatileFileId = io_parms->volatile_fid;
  1618. req->ReadChannelInfoOffset = 0; /* reserved */
  1619. req->ReadChannelInfoLength = 0; /* reserved */
  1620. req->Channel = 0; /* reserved */
  1621. req->MinimumCount = 0;
  1622. req->Length = cpu_to_le32(io_parms->length);
  1623. req->Offset = cpu_to_le64(io_parms->offset);
  1624. if (request_type & CHAINED_REQUEST) {
  1625. if (!(request_type & END_OF_CHAIN)) {
  1626. /* 4 for rfc1002 length field */
  1627. req->hdr.NextCommand =
  1628. cpu_to_le32(get_rfc1002_length(req) + 4);
  1629. } else /* END_OF_CHAIN */
  1630. req->hdr.NextCommand = 0;
  1631. if (request_type & RELATED_REQUEST) {
  1632. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1633. /*
  1634. * Related requests use info from previous read request
  1635. * in chain.
  1636. */
  1637. req->hdr.SessionId = 0xFFFFFFFF;
  1638. req->hdr.TreeId = 0xFFFFFFFF;
  1639. req->PersistentFileId = 0xFFFFFFFF;
  1640. req->VolatileFileId = 0xFFFFFFFF;
  1641. }
  1642. }
  1643. if (remaining_bytes > io_parms->length)
  1644. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1645. else
  1646. req->RemainingBytes = 0;
  1647. iov[0].iov_base = (char *)req;
  1648. /* 4 for rfc1002 length field */
  1649. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1650. return rc;
  1651. }
  1652. static void
  1653. smb2_readv_callback(struct mid_q_entry *mid)
  1654. {
  1655. struct cifs_readdata *rdata = mid->callback_data;
  1656. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1657. struct TCP_Server_Info *server = tcon->ses->server;
  1658. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1659. unsigned int credits_received = 1;
  1660. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1661. .rq_nvec = 1,
  1662. .rq_pages = rdata->pages,
  1663. .rq_npages = rdata->nr_pages,
  1664. .rq_pagesz = rdata->pagesz,
  1665. .rq_tailsz = rdata->tailsz };
  1666. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1667. __func__, mid->mid, mid->mid_state, rdata->result,
  1668. rdata->bytes);
  1669. switch (mid->mid_state) {
  1670. case MID_RESPONSE_RECEIVED:
  1671. credits_received = le16_to_cpu(buf->CreditRequest);
  1672. /* result already set, check signature */
  1673. if (server->sign) {
  1674. int rc;
  1675. rc = smb2_verify_signature(&rqst, server);
  1676. if (rc)
  1677. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1678. rc);
  1679. }
  1680. /* FIXME: should this be counted toward the initiating task? */
  1681. task_io_account_read(rdata->got_bytes);
  1682. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  1683. break;
  1684. case MID_REQUEST_SUBMITTED:
  1685. case MID_RETRY_NEEDED:
  1686. rdata->result = -EAGAIN;
  1687. if (server->sign && rdata->got_bytes)
  1688. /* reset bytes number since we can not check a sign */
  1689. rdata->got_bytes = 0;
  1690. /* FIXME: should this be counted toward the initiating task? */
  1691. task_io_account_read(rdata->got_bytes);
  1692. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  1693. break;
  1694. default:
  1695. if (rdata->result != -ENODATA)
  1696. rdata->result = -EIO;
  1697. }
  1698. if (rdata->result)
  1699. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1700. queue_work(cifsiod_wq, &rdata->work);
  1701. mutex_lock(&server->srv_mutex);
  1702. DeleteMidQEntry(mid);
  1703. mutex_unlock(&server->srv_mutex);
  1704. add_credits(server, credits_received, 0);
  1705. }
  1706. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1707. int
  1708. smb2_async_readv(struct cifs_readdata *rdata)
  1709. {
  1710. int rc, flags = 0;
  1711. struct smb2_hdr *buf;
  1712. struct cifs_io_parms io_parms;
  1713. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1714. .rq_nvec = 1 };
  1715. struct TCP_Server_Info *server;
  1716. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1717. __func__, rdata->offset, rdata->bytes);
  1718. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1719. io_parms.offset = rdata->offset;
  1720. io_parms.length = rdata->bytes;
  1721. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1722. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1723. io_parms.pid = rdata->pid;
  1724. server = io_parms.tcon->ses->server;
  1725. rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
  1726. if (rc) {
  1727. if (rc == -EAGAIN && rdata->credits) {
  1728. /* credits was reset by reconnect */
  1729. rdata->credits = 0;
  1730. /* reduce in_flight value since we won't send the req */
  1731. spin_lock(&server->req_lock);
  1732. server->in_flight--;
  1733. spin_unlock(&server->req_lock);
  1734. }
  1735. return rc;
  1736. }
  1737. buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1738. /* 4 for rfc1002 length field */
  1739. rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
  1740. if (rdata->credits) {
  1741. buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
  1742. SMB2_MAX_BUFFER_SIZE));
  1743. spin_lock(&server->req_lock);
  1744. server->credits += rdata->credits -
  1745. le16_to_cpu(buf->CreditCharge);
  1746. spin_unlock(&server->req_lock);
  1747. wake_up(&server->request_q);
  1748. flags = CIFS_HAS_CREDITS;
  1749. }
  1750. kref_get(&rdata->refcount);
  1751. rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
  1752. cifs_readv_receive, smb2_readv_callback,
  1753. rdata, flags);
  1754. if (rc) {
  1755. kref_put(&rdata->refcount, cifs_readdata_release);
  1756. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  1757. }
  1758. cifs_small_buf_release(buf);
  1759. return rc;
  1760. }
  1761. int
  1762. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1763. unsigned int *nbytes, char **buf, int *buf_type)
  1764. {
  1765. int resp_buftype, rc = -EACCES;
  1766. struct smb2_read_rsp *rsp = NULL;
  1767. struct kvec iov[1];
  1768. *nbytes = 0;
  1769. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1770. if (rc)
  1771. return rc;
  1772. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1773. &resp_buftype, CIFS_LOG_ERROR);
  1774. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1775. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1776. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1777. return 0;
  1778. }
  1779. if (rc) {
  1780. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1781. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1782. } else {
  1783. *nbytes = le32_to_cpu(rsp->DataLength);
  1784. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1785. (*nbytes > io_parms->length)) {
  1786. cifs_dbg(FYI, "bad length %d for count %d\n",
  1787. *nbytes, io_parms->length);
  1788. rc = -EIO;
  1789. *nbytes = 0;
  1790. }
  1791. }
  1792. if (*buf) {
  1793. memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
  1794. *nbytes);
  1795. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1796. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1797. *buf = iov[0].iov_base;
  1798. if (resp_buftype == CIFS_SMALL_BUFFER)
  1799. *buf_type = CIFS_SMALL_BUFFER;
  1800. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1801. *buf_type = CIFS_LARGE_BUFFER;
  1802. }
  1803. return rc;
  1804. }
  1805. /*
  1806. * Check the mid_state and signature on received buffer (if any), and queue the
  1807. * workqueue completion task.
  1808. */
  1809. static void
  1810. smb2_writev_callback(struct mid_q_entry *mid)
  1811. {
  1812. struct cifs_writedata *wdata = mid->callback_data;
  1813. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1814. struct TCP_Server_Info *server = tcon->ses->server;
  1815. unsigned int written;
  1816. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1817. unsigned int credits_received = 1;
  1818. switch (mid->mid_state) {
  1819. case MID_RESPONSE_RECEIVED:
  1820. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1821. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1822. if (wdata->result != 0)
  1823. break;
  1824. written = le32_to_cpu(rsp->DataLength);
  1825. /*
  1826. * Mask off high 16 bits when bytes written as returned
  1827. * by the server is greater than bytes requested by the
  1828. * client. OS/2 servers are known to set incorrect
  1829. * CountHigh values.
  1830. */
  1831. if (written > wdata->bytes)
  1832. written &= 0xFFFF;
  1833. if (written < wdata->bytes)
  1834. wdata->result = -ENOSPC;
  1835. else
  1836. wdata->bytes = written;
  1837. break;
  1838. case MID_REQUEST_SUBMITTED:
  1839. case MID_RETRY_NEEDED:
  1840. wdata->result = -EAGAIN;
  1841. break;
  1842. default:
  1843. wdata->result = -EIO;
  1844. break;
  1845. }
  1846. if (wdata->result)
  1847. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1848. queue_work(cifsiod_wq, &wdata->work);
  1849. mutex_lock(&server->srv_mutex);
  1850. DeleteMidQEntry(mid);
  1851. mutex_unlock(&server->srv_mutex);
  1852. add_credits(tcon->ses->server, credits_received, 0);
  1853. }
  1854. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1855. int
  1856. smb2_async_writev(struct cifs_writedata *wdata,
  1857. void (*release)(struct kref *kref))
  1858. {
  1859. int rc = -EACCES, flags = 0;
  1860. struct smb2_write_req *req = NULL;
  1861. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1862. struct TCP_Server_Info *server = tcon->ses->server;
  1863. struct kvec iov;
  1864. struct smb_rqst rqst;
  1865. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1866. if (rc) {
  1867. if (rc == -EAGAIN && wdata->credits) {
  1868. /* credits was reset by reconnect */
  1869. wdata->credits = 0;
  1870. /* reduce in_flight value since we won't send the req */
  1871. spin_lock(&server->req_lock);
  1872. server->in_flight--;
  1873. spin_unlock(&server->req_lock);
  1874. }
  1875. goto async_writev_out;
  1876. }
  1877. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1878. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1879. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1880. req->WriteChannelInfoOffset = 0;
  1881. req->WriteChannelInfoLength = 0;
  1882. req->Channel = 0;
  1883. req->Offset = cpu_to_le64(wdata->offset);
  1884. /* 4 for rfc1002 length field */
  1885. req->DataOffset = cpu_to_le16(
  1886. offsetof(struct smb2_write_req, Buffer) - 4);
  1887. req->RemainingBytes = 0;
  1888. /* 4 for rfc1002 length field and 1 for Buffer */
  1889. iov.iov_len = get_rfc1002_length(req) + 4 - 1;
  1890. iov.iov_base = req;
  1891. rqst.rq_iov = &iov;
  1892. rqst.rq_nvec = 1;
  1893. rqst.rq_pages = wdata->pages;
  1894. rqst.rq_npages = wdata->nr_pages;
  1895. rqst.rq_pagesz = wdata->pagesz;
  1896. rqst.rq_tailsz = wdata->tailsz;
  1897. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  1898. wdata->offset, wdata->bytes);
  1899. req->Length = cpu_to_le32(wdata->bytes);
  1900. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1901. if (wdata->credits) {
  1902. req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
  1903. SMB2_MAX_BUFFER_SIZE));
  1904. spin_lock(&server->req_lock);
  1905. server->credits += wdata->credits -
  1906. le16_to_cpu(req->hdr.CreditCharge);
  1907. spin_unlock(&server->req_lock);
  1908. wake_up(&server->request_q);
  1909. flags = CIFS_HAS_CREDITS;
  1910. }
  1911. kref_get(&wdata->refcount);
  1912. rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
  1913. flags);
  1914. if (rc) {
  1915. kref_put(&wdata->refcount, release);
  1916. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1917. }
  1918. async_writev_out:
  1919. cifs_small_buf_release(req);
  1920. return rc;
  1921. }
  1922. /*
  1923. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1924. * The length field from io_parms must be at least 1 and indicates a number of
  1925. * elements with data to write that begins with position 1 in iov array. All
  1926. * data length is specified by count.
  1927. */
  1928. int
  1929. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1930. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1931. {
  1932. int rc = 0;
  1933. struct smb2_write_req *req = NULL;
  1934. struct smb2_write_rsp *rsp = NULL;
  1935. int resp_buftype;
  1936. *nbytes = 0;
  1937. if (n_vec < 1)
  1938. return rc;
  1939. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1940. if (rc)
  1941. return rc;
  1942. if (io_parms->tcon->ses->server == NULL)
  1943. return -ECONNABORTED;
  1944. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1945. req->PersistentFileId = io_parms->persistent_fid;
  1946. req->VolatileFileId = io_parms->volatile_fid;
  1947. req->WriteChannelInfoOffset = 0;
  1948. req->WriteChannelInfoLength = 0;
  1949. req->Channel = 0;
  1950. req->Length = cpu_to_le32(io_parms->length);
  1951. req->Offset = cpu_to_le64(io_parms->offset);
  1952. /* 4 for rfc1002 length field */
  1953. req->DataOffset = cpu_to_le16(
  1954. offsetof(struct smb2_write_req, Buffer) - 4);
  1955. req->RemainingBytes = 0;
  1956. iov[0].iov_base = (char *)req;
  1957. /* 4 for rfc1002 length field and 1 for Buffer */
  1958. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1959. /* length of entire message including data to be written */
  1960. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1961. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1962. &resp_buftype, 0);
  1963. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1964. if (rc) {
  1965. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1966. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  1967. } else
  1968. *nbytes = le32_to_cpu(rsp->DataLength);
  1969. free_rsp_buf(resp_buftype, rsp);
  1970. return rc;
  1971. }
  1972. static unsigned int
  1973. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  1974. {
  1975. int len;
  1976. unsigned int entrycount = 0;
  1977. unsigned int next_offset = 0;
  1978. FILE_DIRECTORY_INFO *entryptr;
  1979. if (bufstart == NULL)
  1980. return 0;
  1981. entryptr = (FILE_DIRECTORY_INFO *)bufstart;
  1982. while (1) {
  1983. entryptr = (FILE_DIRECTORY_INFO *)
  1984. ((char *)entryptr + next_offset);
  1985. if ((char *)entryptr + size > end_of_buf) {
  1986. cifs_dbg(VFS, "malformed search entry would overflow\n");
  1987. break;
  1988. }
  1989. len = le32_to_cpu(entryptr->FileNameLength);
  1990. if ((char *)entryptr + len + size > end_of_buf) {
  1991. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  1992. end_of_buf);
  1993. break;
  1994. }
  1995. *lastentry = (char *)entryptr;
  1996. entrycount++;
  1997. next_offset = le32_to_cpu(entryptr->NextEntryOffset);
  1998. if (!next_offset)
  1999. break;
  2000. }
  2001. return entrycount;
  2002. }
  2003. /*
  2004. * Readdir/FindFirst
  2005. */
  2006. int
  2007. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  2008. u64 persistent_fid, u64 volatile_fid, int index,
  2009. struct cifs_search_info *srch_inf)
  2010. {
  2011. struct smb2_query_directory_req *req;
  2012. struct smb2_query_directory_rsp *rsp = NULL;
  2013. struct kvec iov[2];
  2014. int rc = 0;
  2015. int len;
  2016. int resp_buftype = CIFS_NO_BUFFER;
  2017. unsigned char *bufptr;
  2018. struct TCP_Server_Info *server;
  2019. struct cifs_ses *ses = tcon->ses;
  2020. __le16 asteriks = cpu_to_le16('*');
  2021. char *end_of_smb;
  2022. unsigned int output_size = CIFSMaxBufSize;
  2023. size_t info_buf_size;
  2024. if (ses && (ses->server))
  2025. server = ses->server;
  2026. else
  2027. return -EIO;
  2028. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  2029. if (rc)
  2030. return rc;
  2031. switch (srch_inf->info_level) {
  2032. case SMB_FIND_FILE_DIRECTORY_INFO:
  2033. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  2034. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  2035. break;
  2036. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  2037. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  2038. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  2039. break;
  2040. default:
  2041. cifs_dbg(VFS, "info level %u isn't supported\n",
  2042. srch_inf->info_level);
  2043. rc = -EINVAL;
  2044. goto qdir_exit;
  2045. }
  2046. req->FileIndex = cpu_to_le32(index);
  2047. req->PersistentFileId = persistent_fid;
  2048. req->VolatileFileId = volatile_fid;
  2049. len = 0x2;
  2050. bufptr = req->Buffer;
  2051. memcpy(bufptr, &asteriks, len);
  2052. req->FileNameOffset =
  2053. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  2054. req->FileNameLength = cpu_to_le16(len);
  2055. /*
  2056. * BB could be 30 bytes or so longer if we used SMB2 specific
  2057. * buffer lengths, but this is safe and close enough.
  2058. */
  2059. output_size = min_t(unsigned int, output_size, server->maxBuf);
  2060. output_size = min_t(unsigned int, output_size, 2 << 15);
  2061. req->OutputBufferLength = cpu_to_le32(output_size);
  2062. iov[0].iov_base = (char *)req;
  2063. /* 4 for RFC1001 length and 1 for Buffer */
  2064. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  2065. iov[1].iov_base = (char *)(req->Buffer);
  2066. iov[1].iov_len = len;
  2067. inc_rfc1001_len(req, len - 1 /* Buffer */);
  2068. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  2069. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  2070. if (rc) {
  2071. if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
  2072. srch_inf->endOfSearch = true;
  2073. rc = 0;
  2074. }
  2075. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  2076. goto qdir_exit;
  2077. }
  2078. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  2079. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  2080. info_buf_size);
  2081. if (rc)
  2082. goto qdir_exit;
  2083. srch_inf->unicode = true;
  2084. if (srch_inf->ntwrk_buf_start) {
  2085. if (srch_inf->smallBuf)
  2086. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  2087. else
  2088. cifs_buf_release(srch_inf->ntwrk_buf_start);
  2089. }
  2090. srch_inf->ntwrk_buf_start = (char *)rsp;
  2091. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  2092. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  2093. /* 4 for rfc1002 length field */
  2094. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  2095. srch_inf->entries_in_buffer =
  2096. num_entries(srch_inf->srch_entries_start, end_of_smb,
  2097. &srch_inf->last_entry, info_buf_size);
  2098. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  2099. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  2100. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  2101. srch_inf->srch_entries_start, srch_inf->last_entry);
  2102. if (resp_buftype == CIFS_LARGE_BUFFER)
  2103. srch_inf->smallBuf = false;
  2104. else if (resp_buftype == CIFS_SMALL_BUFFER)
  2105. srch_inf->smallBuf = true;
  2106. else
  2107. cifs_dbg(VFS, "illegal search buffer type\n");
  2108. return rc;
  2109. qdir_exit:
  2110. free_rsp_buf(resp_buftype, rsp);
  2111. return rc;
  2112. }
  2113. static int
  2114. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  2115. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  2116. unsigned int num, void **data, unsigned int *size)
  2117. {
  2118. struct smb2_set_info_req *req;
  2119. struct smb2_set_info_rsp *rsp = NULL;
  2120. struct kvec *iov;
  2121. int rc = 0;
  2122. int resp_buftype;
  2123. unsigned int i;
  2124. struct TCP_Server_Info *server;
  2125. struct cifs_ses *ses = tcon->ses;
  2126. if (ses && (ses->server))
  2127. server = ses->server;
  2128. else
  2129. return -EIO;
  2130. if (!num)
  2131. return -EINVAL;
  2132. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  2133. if (!iov)
  2134. return -ENOMEM;
  2135. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  2136. if (rc) {
  2137. kfree(iov);
  2138. return rc;
  2139. }
  2140. req->hdr.ProcessId = cpu_to_le32(pid);
  2141. req->InfoType = SMB2_O_INFO_FILE;
  2142. req->FileInfoClass = info_class;
  2143. req->PersistentFileId = persistent_fid;
  2144. req->VolatileFileId = volatile_fid;
  2145. /* 4 for RFC1001 length and 1 for Buffer */
  2146. req->BufferOffset =
  2147. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  2148. req->BufferLength = cpu_to_le32(*size);
  2149. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  2150. memcpy(req->Buffer, *data, *size);
  2151. iov[0].iov_base = (char *)req;
  2152. /* 4 for RFC1001 length */
  2153. iov[0].iov_len = get_rfc1002_length(req) + 4;
  2154. for (i = 1; i < num; i++) {
  2155. inc_rfc1001_len(req, size[i]);
  2156. le32_add_cpu(&req->BufferLength, size[i]);
  2157. iov[i].iov_base = (char *)data[i];
  2158. iov[i].iov_len = size[i];
  2159. }
  2160. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  2161. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  2162. if (rc != 0)
  2163. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  2164. free_rsp_buf(resp_buftype, rsp);
  2165. kfree(iov);
  2166. return rc;
  2167. }
  2168. int
  2169. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  2170. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  2171. {
  2172. struct smb2_file_rename_info info;
  2173. void **data;
  2174. unsigned int size[2];
  2175. int rc;
  2176. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  2177. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  2178. if (!data)
  2179. return -ENOMEM;
  2180. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  2181. /* 0 = fail if target already exists */
  2182. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  2183. info.FileNameLength = cpu_to_le32(len);
  2184. data[0] = &info;
  2185. size[0] = sizeof(struct smb2_file_rename_info);
  2186. data[1] = target_file;
  2187. size[1] = len + 2 /* null */;
  2188. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2189. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  2190. size);
  2191. kfree(data);
  2192. return rc;
  2193. }
  2194. int
  2195. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  2196. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  2197. {
  2198. struct smb2_file_link_info info;
  2199. void **data;
  2200. unsigned int size[2];
  2201. int rc;
  2202. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  2203. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  2204. if (!data)
  2205. return -ENOMEM;
  2206. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  2207. /* 0 = fail if link already exists */
  2208. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  2209. info.FileNameLength = cpu_to_le32(len);
  2210. data[0] = &info;
  2211. size[0] = sizeof(struct smb2_file_link_info);
  2212. data[1] = target_file;
  2213. size[1] = len + 2 /* null */;
  2214. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2215. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  2216. kfree(data);
  2217. return rc;
  2218. }
  2219. int
  2220. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  2221. u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
  2222. {
  2223. struct smb2_file_eof_info info;
  2224. void *data;
  2225. unsigned int size;
  2226. info.EndOfFile = *eof;
  2227. data = &info;
  2228. size = sizeof(struct smb2_file_eof_info);
  2229. if (is_falloc)
  2230. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2231. pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
  2232. else
  2233. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2234. pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  2235. }
  2236. int
  2237. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  2238. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  2239. {
  2240. unsigned int size;
  2241. size = sizeof(FILE_BASIC_INFO);
  2242. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2243. current->tgid, FILE_BASIC_INFORMATION, 1,
  2244. (void **)&buf, &size);
  2245. }
  2246. int
  2247. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  2248. const u64 persistent_fid, const u64 volatile_fid,
  2249. __u8 oplock_level)
  2250. {
  2251. int rc;
  2252. struct smb2_oplock_break *req = NULL;
  2253. cifs_dbg(FYI, "SMB2_oplock_break\n");
  2254. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  2255. if (rc)
  2256. return rc;
  2257. req->VolatileFid = volatile_fid;
  2258. req->PersistentFid = persistent_fid;
  2259. req->OplockLevel = oplock_level;
  2260. req->hdr.CreditRequest = cpu_to_le16(1);
  2261. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  2262. /* SMB2 buffer freed by function above */
  2263. if (rc) {
  2264. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  2265. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  2266. }
  2267. return rc;
  2268. }
  2269. static void
  2270. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  2271. struct kstatfs *kst)
  2272. {
  2273. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  2274. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  2275. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  2276. kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
  2277. kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  2278. return;
  2279. }
  2280. static int
  2281. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  2282. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  2283. {
  2284. int rc;
  2285. struct smb2_query_info_req *req;
  2286. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  2287. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  2288. return -EIO;
  2289. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  2290. if (rc)
  2291. return rc;
  2292. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  2293. req->FileInfoClass = level;
  2294. req->PersistentFileId = persistent_fid;
  2295. req->VolatileFileId = volatile_fid;
  2296. /* 4 for rfc1002 length field and 1 for pad */
  2297. req->InputBufferOffset =
  2298. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  2299. req->OutputBufferLength = cpu_to_le32(
  2300. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  2301. iov->iov_base = (char *)req;
  2302. /* 4 for rfc1002 length field */
  2303. iov->iov_len = get_rfc1002_length(req) + 4;
  2304. return 0;
  2305. }
  2306. int
  2307. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  2308. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  2309. {
  2310. struct smb2_query_info_rsp *rsp = NULL;
  2311. struct kvec iov;
  2312. int rc = 0;
  2313. int resp_buftype;
  2314. struct cifs_ses *ses = tcon->ses;
  2315. struct smb2_fs_full_size_info *info = NULL;
  2316. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  2317. sizeof(struct smb2_fs_full_size_info),
  2318. persistent_fid, volatile_fid);
  2319. if (rc)
  2320. return rc;
  2321. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  2322. if (rc) {
  2323. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  2324. goto qfsinf_exit;
  2325. }
  2326. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  2327. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  2328. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  2329. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  2330. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  2331. sizeof(struct smb2_fs_full_size_info));
  2332. if (!rc)
  2333. copy_fs_info_to_kstatfs(info, fsdata);
  2334. qfsinf_exit:
  2335. free_rsp_buf(resp_buftype, iov.iov_base);
  2336. return rc;
  2337. }
  2338. int
  2339. SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
  2340. u64 persistent_fid, u64 volatile_fid, int level)
  2341. {
  2342. struct smb2_query_info_rsp *rsp = NULL;
  2343. struct kvec iov;
  2344. int rc = 0;
  2345. int resp_buftype, max_len, min_len;
  2346. struct cifs_ses *ses = tcon->ses;
  2347. unsigned int rsp_len, offset;
  2348. if (level == FS_DEVICE_INFORMATION) {
  2349. max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
  2350. min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
  2351. } else if (level == FS_ATTRIBUTE_INFORMATION) {
  2352. max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
  2353. min_len = MIN_FS_ATTR_INFO_SIZE;
  2354. } else if (level == FS_SECTOR_SIZE_INFORMATION) {
  2355. max_len = sizeof(struct smb3_fs_ss_info);
  2356. min_len = sizeof(struct smb3_fs_ss_info);
  2357. } else {
  2358. cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
  2359. return -EINVAL;
  2360. }
  2361. rc = build_qfs_info_req(&iov, tcon, level, max_len,
  2362. persistent_fid, volatile_fid);
  2363. if (rc)
  2364. return rc;
  2365. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  2366. if (rc) {
  2367. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  2368. goto qfsattr_exit;
  2369. }
  2370. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  2371. rsp_len = le32_to_cpu(rsp->OutputBufferLength);
  2372. offset = le16_to_cpu(rsp->OutputBufferOffset);
  2373. rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
  2374. if (rc)
  2375. goto qfsattr_exit;
  2376. if (level == FS_ATTRIBUTE_INFORMATION)
  2377. memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
  2378. + (char *)&rsp->hdr, min_t(unsigned int,
  2379. rsp_len, max_len));
  2380. else if (level == FS_DEVICE_INFORMATION)
  2381. memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
  2382. + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
  2383. else if (level == FS_SECTOR_SIZE_INFORMATION) {
  2384. struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
  2385. (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
  2386. tcon->ss_flags = le32_to_cpu(ss_info->Flags);
  2387. tcon->perf_sector_size =
  2388. le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
  2389. }
  2390. qfsattr_exit:
  2391. free_rsp_buf(resp_buftype, iov.iov_base);
  2392. return rc;
  2393. }
  2394. int
  2395. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  2396. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  2397. const __u32 num_lock, struct smb2_lock_element *buf)
  2398. {
  2399. int rc = 0;
  2400. struct smb2_lock_req *req = NULL;
  2401. struct kvec iov[2];
  2402. int resp_buf_type;
  2403. unsigned int count;
  2404. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  2405. rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
  2406. if (rc)
  2407. return rc;
  2408. req->hdr.ProcessId = cpu_to_le32(pid);
  2409. req->LockCount = cpu_to_le16(num_lock);
  2410. req->PersistentFileId = persist_fid;
  2411. req->VolatileFileId = volatile_fid;
  2412. count = num_lock * sizeof(struct smb2_lock_element);
  2413. inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
  2414. iov[0].iov_base = (char *)req;
  2415. /* 4 for rfc1002 length field and count for all locks */
  2416. iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
  2417. iov[1].iov_base = (char *)buf;
  2418. iov[1].iov_len = count;
  2419. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  2420. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  2421. if (rc) {
  2422. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  2423. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  2424. }
  2425. return rc;
  2426. }
  2427. int
  2428. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  2429. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  2430. const __u64 length, const __u64 offset, const __u32 lock_flags,
  2431. const bool wait)
  2432. {
  2433. struct smb2_lock_element lock;
  2434. lock.Offset = cpu_to_le64(offset);
  2435. lock.Length = cpu_to_le64(length);
  2436. lock.Flags = cpu_to_le32(lock_flags);
  2437. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  2438. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  2439. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  2440. }
  2441. int
  2442. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  2443. __u8 *lease_key, const __le32 lease_state)
  2444. {
  2445. int rc;
  2446. struct smb2_lease_ack *req = NULL;
  2447. cifs_dbg(FYI, "SMB2_lease_break\n");
  2448. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  2449. if (rc)
  2450. return rc;
  2451. req->hdr.CreditRequest = cpu_to_le16(1);
  2452. req->StructureSize = cpu_to_le16(36);
  2453. inc_rfc1001_len(req, 12);
  2454. memcpy(req->LeaseKey, lease_key, 16);
  2455. req->LeaseState = lease_state;
  2456. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  2457. /* SMB2 buffer freed by function above */
  2458. if (rc) {
  2459. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  2460. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  2461. }
  2462. return rc;
  2463. }