smb2pdu.c 68 KB

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