smb2pdu.c 76 KB

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