smb2pdu.c 75 KB

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