smb2pdu.c 71 KB

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