smb2pdu.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. /*
  2. * fs/cifs/smb2pdu.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2009, 2011
  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. hdr->TreeId = tcon->tid;
  102. /* Uid is not converted */
  103. if (tcon->ses)
  104. hdr->SessionId = tcon->ses->Suid;
  105. /* BB check following DFS flags BB */
  106. /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */
  107. if (tcon->share_flags & SHI1005_FLAGS_DFS)
  108. hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS;
  109. /* BB how does SMB2 do case sensitive? */
  110. /* if (tcon->nocase)
  111. hdr->Flags |= SMBFLG_CASELESS; */
  112. if (tcon->ses && tcon->ses->server &&
  113. (tcon->ses->server->sec_mode & SECMODE_SIGN_REQUIRED))
  114. hdr->Flags |= SMB2_FLAGS_SIGNED;
  115. out:
  116. pdu->StructureSize2 = cpu_to_le16(parmsize);
  117. return;
  118. }
  119. static int
  120. smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
  121. {
  122. int rc = 0;
  123. struct nls_table *nls_codepage;
  124. struct cifs_ses *ses;
  125. struct TCP_Server_Info *server;
  126. /*
  127. * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
  128. * check for tcp and smb session status done differently
  129. * for those three - in the calling routine.
  130. */
  131. if (tcon == NULL)
  132. return rc;
  133. if (smb2_command == SMB2_TREE_CONNECT)
  134. return rc;
  135. if (tcon->tidStatus == CifsExiting) {
  136. /*
  137. * only tree disconnect, open, and write,
  138. * (and ulogoff which does not have tcon)
  139. * are allowed as we start force umount.
  140. */
  141. if ((smb2_command != SMB2_WRITE) &&
  142. (smb2_command != SMB2_CREATE) &&
  143. (smb2_command != SMB2_TREE_DISCONNECT)) {
  144. cFYI(1, "can not send cmd %d while umounting",
  145. smb2_command);
  146. return -ENODEV;
  147. }
  148. }
  149. if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
  150. (!tcon->ses->server))
  151. return -EIO;
  152. ses = tcon->ses;
  153. server = ses->server;
  154. /*
  155. * Give demultiplex thread up to 10 seconds to reconnect, should be
  156. * greater than cifs socket timeout which is 7 seconds
  157. */
  158. while (server->tcpStatus == CifsNeedReconnect) {
  159. /*
  160. * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
  161. * here since they are implicitly done when session drops.
  162. */
  163. switch (smb2_command) {
  164. /*
  165. * BB Should we keep oplock break and add flush to exceptions?
  166. */
  167. case SMB2_TREE_DISCONNECT:
  168. case SMB2_CANCEL:
  169. case SMB2_CLOSE:
  170. case SMB2_OPLOCK_BREAK:
  171. return -EAGAIN;
  172. }
  173. wait_event_interruptible_timeout(server->response_q,
  174. (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
  175. /* are we still trying to reconnect? */
  176. if (server->tcpStatus != CifsNeedReconnect)
  177. break;
  178. /*
  179. * on "soft" mounts we wait once. Hard mounts keep
  180. * retrying until process is killed or server comes
  181. * back on-line
  182. */
  183. if (!tcon->retry) {
  184. cFYI(1, "gave up waiting on reconnect in smb_init");
  185. return -EHOSTDOWN;
  186. }
  187. }
  188. if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
  189. return rc;
  190. nls_codepage = load_nls_default();
  191. /*
  192. * need to prevent multiple threads trying to simultaneously reconnect
  193. * the same SMB session
  194. */
  195. mutex_lock(&tcon->ses->session_mutex);
  196. rc = cifs_negotiate_protocol(0, tcon->ses);
  197. if (!rc && tcon->ses->need_reconnect)
  198. rc = cifs_setup_session(0, tcon->ses, nls_codepage);
  199. if (rc || !tcon->need_reconnect) {
  200. mutex_unlock(&tcon->ses->session_mutex);
  201. goto out;
  202. }
  203. cifs_mark_open_files_invalid(tcon);
  204. rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
  205. mutex_unlock(&tcon->ses->session_mutex);
  206. cFYI(1, "reconnect tcon rc = %d", rc);
  207. if (rc)
  208. goto out;
  209. atomic_inc(&tconInfoReconnectCount);
  210. /*
  211. * BB FIXME add code to check if wsize needs update due to negotiated
  212. * smb buffer size shrinking.
  213. */
  214. out:
  215. /*
  216. * Check if handle based operation so we know whether we can continue
  217. * or not without returning to caller to reset file handle.
  218. */
  219. /*
  220. * BB Is flush done by server on drop of tcp session? Should we special
  221. * case it and skip above?
  222. */
  223. switch (smb2_command) {
  224. case SMB2_FLUSH:
  225. case SMB2_READ:
  226. case SMB2_WRITE:
  227. case SMB2_LOCK:
  228. case SMB2_IOCTL:
  229. case SMB2_QUERY_DIRECTORY:
  230. case SMB2_CHANGE_NOTIFY:
  231. case SMB2_QUERY_INFO:
  232. case SMB2_SET_INFO:
  233. return -EAGAIN;
  234. }
  235. unload_nls(nls_codepage);
  236. return rc;
  237. }
  238. /*
  239. * Allocate and return pointer to an SMB request hdr, and set basic
  240. * SMB information in the SMB header. If the return code is zero, this
  241. * function must have filled in request_buf pointer.
  242. */
  243. static int
  244. small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
  245. void **request_buf)
  246. {
  247. int rc = 0;
  248. rc = smb2_reconnect(smb2_command, tcon);
  249. if (rc)
  250. return rc;
  251. /* BB eventually switch this to SMB2 specific small buf size */
  252. *request_buf = cifs_small_buf_get();
  253. if (*request_buf == NULL) {
  254. /* BB should we add a retry in here if not a writepage? */
  255. return -ENOMEM;
  256. }
  257. smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
  258. if (tcon != NULL) {
  259. #ifdef CONFIG_CIFS_STATS2
  260. uint16_t com_code = le16_to_cpu(smb2_command);
  261. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
  262. #endif
  263. cifs_stats_inc(&tcon->num_smbs_sent);
  264. }
  265. return rc;
  266. }
  267. static void
  268. free_rsp_buf(int resp_buftype, void *rsp)
  269. {
  270. if (resp_buftype == CIFS_SMALL_BUFFER)
  271. cifs_small_buf_release(rsp);
  272. else if (resp_buftype == CIFS_LARGE_BUFFER)
  273. cifs_buf_release(rsp);
  274. }
  275. #define SMB2_NUM_PROT 1
  276. #define SMB2_PROT 0
  277. #define SMB21_PROT 1
  278. #define BAD_PROT 0xFFFF
  279. #define SMB2_PROT_ID 0x0202
  280. #define SMB21_PROT_ID 0x0210
  281. #define BAD_PROT_ID 0xFFFF
  282. static struct {
  283. int index;
  284. __le16 name;
  285. } smb2protocols[] = {
  286. {SMB2_PROT, cpu_to_le16(SMB2_PROT_ID)},
  287. {SMB21_PROT, cpu_to_le16(SMB21_PROT_ID)},
  288. {BAD_PROT, cpu_to_le16(BAD_PROT_ID)}
  289. };
  290. /*
  291. *
  292. * SMB2 Worker functions follow:
  293. *
  294. * The general structure of the worker functions is:
  295. * 1) Call smb2_init (assembles SMB2 header)
  296. * 2) Initialize SMB2 command specific fields in fixed length area of SMB
  297. * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
  298. * 4) Decode SMB2 command specific fields in the fixed length area
  299. * 5) Decode variable length data area (if any for this SMB2 command type)
  300. * 6) Call free smb buffer
  301. * 7) return
  302. *
  303. */
  304. int
  305. SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  306. {
  307. struct smb2_negotiate_req *req;
  308. struct smb2_negotiate_rsp *rsp;
  309. struct kvec iov[1];
  310. int rc = 0;
  311. int resp_buftype;
  312. struct TCP_Server_Info *server;
  313. unsigned int sec_flags;
  314. u16 i;
  315. u16 temp = 0;
  316. int blob_offset, blob_length;
  317. char *security_blob;
  318. int flags = CIFS_NEG_OP;
  319. cFYI(1, "Negotiate protocol");
  320. if (ses->server)
  321. server = ses->server;
  322. else {
  323. rc = -EIO;
  324. return rc;
  325. }
  326. rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
  327. if (rc)
  328. return rc;
  329. /* if any of auth flags (ie not sign or seal) are overriden use them */
  330. if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  331. sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
  332. else /* if override flags set only sign/seal OR them with global auth */
  333. sec_flags = global_secflags | ses->overrideSecFlg;
  334. cFYI(1, "sec_flags 0x%x", sec_flags);
  335. req->hdr.SessionId = 0;
  336. for (i = 0; i < SMB2_NUM_PROT; i++)
  337. req->Dialects[i] = smb2protocols[i].name;
  338. req->DialectCount = cpu_to_le16(i);
  339. inc_rfc1001_len(req, i * 2);
  340. /* only one of SMB2 signing flags may be set in SMB2 request */
  341. if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
  342. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  343. else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
  344. temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
  345. req->SecurityMode = cpu_to_le16(temp);
  346. req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
  347. iov[0].iov_base = (char *)req;
  348. /* 4 for rfc1002 length field */
  349. iov[0].iov_len = get_rfc1002_length(req) + 4;
  350. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
  351. rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
  352. /*
  353. * No tcon so can't do
  354. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  355. */
  356. if (rc != 0)
  357. goto neg_exit;
  358. if (rsp == NULL) {
  359. rc = -EIO;
  360. goto neg_exit;
  361. }
  362. cFYI(1, "mode 0x%x", rsp->SecurityMode);
  363. if (rsp->DialectRevision == smb2protocols[SMB21_PROT].name)
  364. cFYI(1, "negotiated smb2.1 dialect");
  365. else if (rsp->DialectRevision == smb2protocols[SMB2_PROT].name)
  366. cFYI(1, "negotiated smb2 dialect");
  367. else {
  368. cERROR(1, "Illegal dialect returned by server %d",
  369. le16_to_cpu(rsp->DialectRevision));
  370. rc = -EIO;
  371. goto neg_exit;
  372. }
  373. server->dialect = le16_to_cpu(rsp->DialectRevision);
  374. server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
  375. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  376. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  377. /* BB Do we need to validate the SecurityMode? */
  378. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  379. server->capabilities = le32_to_cpu(rsp->Capabilities);
  380. /* Internal types */
  381. server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
  382. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  383. &rsp->hdr);
  384. if (blob_length == 0) {
  385. cERROR(1, "missing security blob on negprot");
  386. rc = -EIO;
  387. goto neg_exit;
  388. }
  389. cFYI(1, "sec_flags 0x%x", sec_flags);
  390. if (sec_flags & CIFSSEC_MUST_SIGN) {
  391. cFYI(1, "Signing required");
  392. if (!(server->sec_mode & (SMB2_NEGOTIATE_SIGNING_REQUIRED |
  393. SMB2_NEGOTIATE_SIGNING_ENABLED))) {
  394. cERROR(1, "signing required but server lacks support");
  395. rc = -EOPNOTSUPP;
  396. goto neg_exit;
  397. }
  398. server->sec_mode |= SECMODE_SIGN_REQUIRED;
  399. } else if (sec_flags & CIFSSEC_MAY_SIGN) {
  400. cFYI(1, "Signing optional");
  401. if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
  402. cFYI(1, "Server requires signing");
  403. server->sec_mode |= SECMODE_SIGN_REQUIRED;
  404. } else {
  405. server->sec_mode &=
  406. ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
  407. }
  408. } else {
  409. cFYI(1, "Signing disabled");
  410. if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
  411. cERROR(1, "Server requires packet signing to be enabled"
  412. " in /proc/fs/cifs/SecurityFlags.");
  413. rc = -EOPNOTSUPP;
  414. goto neg_exit;
  415. }
  416. server->sec_mode &=
  417. ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
  418. }
  419. #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
  420. rc = decode_neg_token_init(security_blob, blob_length,
  421. &server->sec_type);
  422. if (rc == 1)
  423. rc = 0;
  424. else if (rc == 0) {
  425. rc = -EIO;
  426. goto neg_exit;
  427. }
  428. #endif
  429. neg_exit:
  430. free_rsp_buf(resp_buftype, rsp);
  431. return rc;
  432. }
  433. int
  434. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  435. const struct nls_table *nls_cp)
  436. {
  437. struct smb2_sess_setup_req *req;
  438. struct smb2_sess_setup_rsp *rsp = NULL;
  439. struct kvec iov[2];
  440. int rc = 0;
  441. int resp_buftype;
  442. __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
  443. struct TCP_Server_Info *server;
  444. unsigned int sec_flags;
  445. u8 temp = 0;
  446. u16 blob_length = 0;
  447. char *security_blob;
  448. char *ntlmssp_blob = NULL;
  449. bool use_spnego = false; /* else use raw ntlmssp */
  450. cFYI(1, "Session Setup");
  451. if (ses->server)
  452. server = ses->server;
  453. else {
  454. rc = -EIO;
  455. return rc;
  456. }
  457. /*
  458. * If memory allocation is successful, caller of this function
  459. * frees it.
  460. */
  461. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  462. if (!ses->ntlmssp)
  463. return -ENOMEM;
  464. ses->server->secType = RawNTLMSSP;
  465. ssetup_ntlmssp_authenticate:
  466. if (phase == NtLmChallenge)
  467. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  468. rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
  469. if (rc)
  470. return rc;
  471. /* if any of auth flags (ie not sign or seal) are overriden use them */
  472. if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  473. sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
  474. else /* if override flags set only sign/seal OR them with global auth */
  475. sec_flags = global_secflags | ses->overrideSecFlg;
  476. cFYI(1, "sec_flags 0x%x", sec_flags);
  477. req->hdr.SessionId = 0; /* First session, not a reauthenticate */
  478. req->VcNumber = 0; /* MBZ */
  479. /* to enable echos and oplocks */
  480. req->hdr.CreditRequest = cpu_to_le16(3);
  481. /* only one of SMB2 signing flags may be set in SMB2 request */
  482. if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
  483. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  484. else if (ses->server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED)
  485. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  486. else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
  487. temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
  488. req->SecurityMode = temp;
  489. req->Capabilities = 0;
  490. req->Channel = 0; /* MBZ */
  491. iov[0].iov_base = (char *)req;
  492. /* 4 for rfc1002 length field and 1 for pad */
  493. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  494. if (phase == NtLmNegotiate) {
  495. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  496. GFP_KERNEL);
  497. if (ntlmssp_blob == NULL) {
  498. rc = -ENOMEM;
  499. goto ssetup_exit;
  500. }
  501. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  502. if (use_spnego) {
  503. /* blob_length = build_spnego_ntlmssp_blob(
  504. &security_blob,
  505. sizeof(struct _NEGOTIATE_MESSAGE),
  506. ntlmssp_blob); */
  507. /* BB eventually need to add this */
  508. cERROR(1, "spnego not supported for SMB2 yet");
  509. rc = -EOPNOTSUPP;
  510. kfree(ntlmssp_blob);
  511. goto ssetup_exit;
  512. } else {
  513. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  514. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  515. security_blob = ntlmssp_blob;
  516. }
  517. } else if (phase == NtLmAuthenticate) {
  518. req->hdr.SessionId = ses->Suid;
  519. ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
  520. GFP_KERNEL);
  521. if (ntlmssp_blob == NULL) {
  522. cERROR(1, "failed to malloc ntlmssp blob");
  523. rc = -ENOMEM;
  524. goto ssetup_exit;
  525. }
  526. rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
  527. nls_cp);
  528. if (rc) {
  529. cFYI(1, "build_ntlmssp_auth_blob failed %d", rc);
  530. goto ssetup_exit; /* BB double check error handling */
  531. }
  532. if (use_spnego) {
  533. /* blob_length = build_spnego_ntlmssp_blob(
  534. &security_blob,
  535. blob_length,
  536. ntlmssp_blob); */
  537. cERROR(1, "spnego not supported for SMB2 yet");
  538. rc = -EOPNOTSUPP;
  539. kfree(ntlmssp_blob);
  540. goto ssetup_exit;
  541. } else {
  542. security_blob = ntlmssp_blob;
  543. }
  544. } else {
  545. cERROR(1, "illegal ntlmssp phase");
  546. rc = -EIO;
  547. goto ssetup_exit;
  548. }
  549. /* Testing shows that buffer offset must be at location of Buffer[0] */
  550. req->SecurityBufferOffset =
  551. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  552. 1 /* pad */ - 4 /* rfc1001 len */);
  553. req->SecurityBufferLength = cpu_to_le16(blob_length);
  554. iov[1].iov_base = security_blob;
  555. iov[1].iov_len = blob_length;
  556. inc_rfc1001_len(req, blob_length - 1 /* pad */);
  557. /* BB add code to build os and lm fields */
  558. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, CIFS_LOG_ERROR);
  559. kfree(security_blob);
  560. rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
  561. if (rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
  562. if (phase != NtLmNegotiate) {
  563. cERROR(1, "Unexpected more processing error");
  564. goto ssetup_exit;
  565. }
  566. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  567. le16_to_cpu(rsp->SecurityBufferOffset)) {
  568. cERROR(1, "Invalid security buffer offset %d",
  569. le16_to_cpu(rsp->SecurityBufferOffset));
  570. rc = -EIO;
  571. goto ssetup_exit;
  572. }
  573. /* NTLMSSP Negotiate sent now processing challenge (response) */
  574. phase = NtLmChallenge; /* process ntlmssp challenge */
  575. rc = 0; /* MORE_PROCESSING is not an error here but expected */
  576. ses->Suid = rsp->hdr.SessionId;
  577. rc = decode_ntlmssp_challenge(rsp->Buffer,
  578. le16_to_cpu(rsp->SecurityBufferLength), ses);
  579. }
  580. /*
  581. * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
  582. * but at least the raw NTLMSSP case works.
  583. */
  584. /*
  585. * No tcon so can't do
  586. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  587. */
  588. if (rc != 0)
  589. goto ssetup_exit;
  590. if (rsp == NULL) {
  591. rc = -EIO;
  592. goto ssetup_exit;
  593. }
  594. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  595. ssetup_exit:
  596. free_rsp_buf(resp_buftype, rsp);
  597. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  598. if ((phase == NtLmChallenge) && (rc == 0))
  599. goto ssetup_ntlmssp_authenticate;
  600. return rc;
  601. }
  602. int
  603. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  604. {
  605. struct smb2_logoff_req *req; /* response is also trivial struct */
  606. int rc = 0;
  607. struct TCP_Server_Info *server;
  608. cFYI(1, "disconnect session %p", ses);
  609. if (ses && (ses->server))
  610. server = ses->server;
  611. else
  612. return -EIO;
  613. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  614. if (rc)
  615. return rc;
  616. /* since no tcon, smb2_init can not do this, so do here */
  617. req->hdr.SessionId = ses->Suid;
  618. if (server->sec_mode & SECMODE_SIGN_REQUIRED)
  619. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  620. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  621. /*
  622. * No tcon so can't do
  623. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  624. */
  625. return rc;
  626. }
  627. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  628. {
  629. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  630. }
  631. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  632. int
  633. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  634. struct cifs_tcon *tcon, const struct nls_table *cp)
  635. {
  636. struct smb2_tree_connect_req *req;
  637. struct smb2_tree_connect_rsp *rsp = NULL;
  638. struct kvec iov[2];
  639. int rc = 0;
  640. int resp_buftype;
  641. int unc_path_len;
  642. struct TCP_Server_Info *server;
  643. __le16 *unc_path = NULL;
  644. cFYI(1, "TCON");
  645. if ((ses->server) && tree)
  646. server = ses->server;
  647. else
  648. return -EIO;
  649. if (tcon && tcon->bad_network_name)
  650. return -ENOENT;
  651. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  652. if (unc_path == NULL)
  653. return -ENOMEM;
  654. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  655. unc_path_len *= 2;
  656. if (unc_path_len < 2) {
  657. kfree(unc_path);
  658. return -EINVAL;
  659. }
  660. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  661. if (rc) {
  662. kfree(unc_path);
  663. return rc;
  664. }
  665. if (tcon == NULL) {
  666. /* since no tcon, smb2_init can not do this, so do here */
  667. req->hdr.SessionId = ses->Suid;
  668. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  669. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  670. }
  671. iov[0].iov_base = (char *)req;
  672. /* 4 for rfc1002 length field and 1 for pad */
  673. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  674. /* Testing shows that buffer offset must be at location of Buffer[0] */
  675. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  676. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  677. req->PathLength = cpu_to_le16(unc_path_len - 2);
  678. iov[1].iov_base = unc_path;
  679. iov[1].iov_len = unc_path_len;
  680. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  681. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  682. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  683. if (rc != 0) {
  684. if (tcon) {
  685. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  686. tcon->need_reconnect = true;
  687. }
  688. goto tcon_error_exit;
  689. }
  690. if (rsp == NULL) {
  691. rc = -EIO;
  692. goto tcon_exit;
  693. }
  694. if (tcon == NULL) {
  695. ses->ipc_tid = rsp->hdr.TreeId;
  696. goto tcon_exit;
  697. }
  698. if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
  699. cFYI(1, "connection to disk share");
  700. else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
  701. tcon->ipc = true;
  702. cFYI(1, "connection to pipe share");
  703. } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
  704. tcon->print = true;
  705. cFYI(1, "connection to printer");
  706. } else {
  707. cERROR(1, "unknown share type %d", rsp->ShareType);
  708. rc = -EOPNOTSUPP;
  709. goto tcon_error_exit;
  710. }
  711. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  712. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  713. tcon->tidStatus = CifsGood;
  714. tcon->need_reconnect = false;
  715. tcon->tid = rsp->hdr.TreeId;
  716. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  717. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  718. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  719. cERROR(1, "DFS capability contradicts DFS flag");
  720. tcon_exit:
  721. free_rsp_buf(resp_buftype, rsp);
  722. kfree(unc_path);
  723. return rc;
  724. tcon_error_exit:
  725. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  726. cERROR(1, "BAD_NETWORK_NAME: %s", tree);
  727. tcon->bad_network_name = true;
  728. }
  729. goto tcon_exit;
  730. }
  731. int
  732. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  733. {
  734. struct smb2_tree_disconnect_req *req; /* response is trivial */
  735. int rc = 0;
  736. struct TCP_Server_Info *server;
  737. struct cifs_ses *ses = tcon->ses;
  738. cFYI(1, "Tree Disconnect");
  739. if (ses && (ses->server))
  740. server = ses->server;
  741. else
  742. return -EIO;
  743. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  744. return 0;
  745. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  746. if (rc)
  747. return rc;
  748. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  749. if (rc)
  750. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  751. return rc;
  752. }
  753. int
  754. SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
  755. u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access,
  756. __u32 create_disposition, __u32 file_attributes, __u32 create_options,
  757. __u8 *oplock, struct smb2_file_all_info *buf)
  758. {
  759. struct smb2_create_req *req;
  760. struct smb2_create_rsp *rsp;
  761. struct TCP_Server_Info *server;
  762. struct cifs_ses *ses = tcon->ses;
  763. struct kvec iov[2];
  764. int resp_buftype;
  765. int uni_path_len;
  766. int rc = 0;
  767. int num_iovecs = 2;
  768. cFYI(1, "create/open");
  769. if (ses && (ses->server))
  770. server = ses->server;
  771. else
  772. return -EIO;
  773. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  774. if (rc)
  775. return rc;
  776. if (server->oplocks)
  777. req->RequestedOplockLevel = *oplock;
  778. else
  779. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
  780. req->ImpersonationLevel = IL_IMPERSONATION;
  781. req->DesiredAccess = cpu_to_le32(desired_access);
  782. /* File attributes ignored on open (used in create though) */
  783. req->FileAttributes = cpu_to_le32(file_attributes);
  784. req->ShareAccess = FILE_SHARE_ALL_LE;
  785. req->CreateDisposition = cpu_to_le32(create_disposition);
  786. req->CreateOptions = cpu_to_le32(create_options);
  787. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  788. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)
  789. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  790. iov[0].iov_base = (char *)req;
  791. /* 4 for rfc1002 length field */
  792. iov[0].iov_len = get_rfc1002_length(req) + 4;
  793. /* MUST set path len (NameLength) to 0 opening root of share */
  794. if (uni_path_len >= 4) {
  795. req->NameLength = cpu_to_le16(uni_path_len - 2);
  796. /* -1 since last byte is buf[0] which is sent below (path) */
  797. iov[0].iov_len--;
  798. iov[1].iov_len = uni_path_len;
  799. iov[1].iov_base = path;
  800. /*
  801. * -1 since last byte is buf[0] which was counted in
  802. * smb2_buf_len.
  803. */
  804. inc_rfc1001_len(req, uni_path_len - 1);
  805. } else {
  806. num_iovecs = 1;
  807. req->NameLength = 0;
  808. }
  809. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  810. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  811. if (rc != 0) {
  812. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  813. goto creat_exit;
  814. }
  815. if (rsp == NULL) {
  816. rc = -EIO;
  817. goto creat_exit;
  818. }
  819. *persistent_fid = rsp->PersistentFileId;
  820. *volatile_fid = rsp->VolatileFileId;
  821. if (buf) {
  822. memcpy(buf, &rsp->CreationTime, 32);
  823. buf->AllocationSize = rsp->AllocationSize;
  824. buf->EndOfFile = rsp->EndofFile;
  825. buf->Attributes = rsp->FileAttributes;
  826. buf->NumberOfLinks = cpu_to_le32(1);
  827. buf->DeletePending = 0;
  828. }
  829. *oplock = rsp->OplockLevel;
  830. creat_exit:
  831. free_rsp_buf(resp_buftype, rsp);
  832. return rc;
  833. }
  834. int
  835. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  836. u64 persistent_fid, u64 volatile_fid)
  837. {
  838. struct smb2_close_req *req;
  839. struct smb2_close_rsp *rsp;
  840. struct TCP_Server_Info *server;
  841. struct cifs_ses *ses = tcon->ses;
  842. struct kvec iov[1];
  843. int resp_buftype;
  844. int rc = 0;
  845. cFYI(1, "Close");
  846. if (ses && (ses->server))
  847. server = ses->server;
  848. else
  849. return -EIO;
  850. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  851. if (rc)
  852. return rc;
  853. req->PersistentFileId = persistent_fid;
  854. req->VolatileFileId = volatile_fid;
  855. iov[0].iov_base = (char *)req;
  856. /* 4 for rfc1002 length field */
  857. iov[0].iov_len = get_rfc1002_length(req) + 4;
  858. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  859. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  860. if (rc != 0) {
  861. if (tcon)
  862. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  863. goto close_exit;
  864. }
  865. if (rsp == NULL) {
  866. rc = -EIO;
  867. goto close_exit;
  868. }
  869. /* BB FIXME - decode close response, update inode for caching */
  870. close_exit:
  871. free_rsp_buf(resp_buftype, rsp);
  872. return rc;
  873. }
  874. static int
  875. validate_buf(unsigned int offset, unsigned int buffer_length,
  876. struct smb2_hdr *hdr, unsigned int min_buf_size)
  877. {
  878. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  879. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  880. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  881. char *end_of_buf = begin_of_buf + buffer_length;
  882. if (buffer_length < min_buf_size) {
  883. cERROR(1, "buffer length %d smaller than minimum size %d",
  884. buffer_length, min_buf_size);
  885. return -EINVAL;
  886. }
  887. /* check if beyond RFC1001 maximum length */
  888. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  889. cERROR(1, "buffer length %d or smb length %d too large",
  890. buffer_length, smb_len);
  891. return -EINVAL;
  892. }
  893. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  894. cERROR(1, "illegal server response, bad offset to data");
  895. return -EINVAL;
  896. }
  897. return 0;
  898. }
  899. /*
  900. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  901. * Caller must free buffer.
  902. */
  903. static int
  904. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  905. struct smb2_hdr *hdr, unsigned int minbufsize,
  906. char *data)
  907. {
  908. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  909. int rc;
  910. if (!data)
  911. return -EINVAL;
  912. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  913. if (rc)
  914. return rc;
  915. memcpy(data, begin_of_buf, buffer_length);
  916. return 0;
  917. }
  918. static int
  919. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  920. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  921. size_t output_len, size_t min_len, void *data)
  922. {
  923. struct smb2_query_info_req *req;
  924. struct smb2_query_info_rsp *rsp = NULL;
  925. struct kvec iov[2];
  926. int rc = 0;
  927. int resp_buftype;
  928. struct TCP_Server_Info *server;
  929. struct cifs_ses *ses = tcon->ses;
  930. cFYI(1, "Query Info");
  931. if (ses && (ses->server))
  932. server = ses->server;
  933. else
  934. return -EIO;
  935. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  936. if (rc)
  937. return rc;
  938. req->InfoType = SMB2_O_INFO_FILE;
  939. req->FileInfoClass = info_class;
  940. req->PersistentFileId = persistent_fid;
  941. req->VolatileFileId = volatile_fid;
  942. /* 4 for rfc1002 length field and 1 for Buffer */
  943. req->InputBufferOffset =
  944. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  945. req->OutputBufferLength = cpu_to_le32(output_len);
  946. iov[0].iov_base = (char *)req;
  947. /* 4 for rfc1002 length field */
  948. iov[0].iov_len = get_rfc1002_length(req) + 4;
  949. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  950. if (rc) {
  951. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  952. goto qinf_exit;
  953. }
  954. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  955. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  956. le32_to_cpu(rsp->OutputBufferLength),
  957. &rsp->hdr, min_len, data);
  958. qinf_exit:
  959. free_rsp_buf(resp_buftype, rsp);
  960. return rc;
  961. }
  962. int
  963. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  964. u64 persistent_fid, u64 volatile_fid,
  965. struct smb2_file_all_info *data)
  966. {
  967. return query_info(xid, tcon, persistent_fid, volatile_fid,
  968. FILE_ALL_INFORMATION,
  969. sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  970. sizeof(struct smb2_file_all_info), data);
  971. }
  972. int
  973. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  974. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  975. {
  976. return query_info(xid, tcon, persistent_fid, volatile_fid,
  977. FILE_INTERNAL_INFORMATION,
  978. sizeof(struct smb2_file_internal_info),
  979. sizeof(struct smb2_file_internal_info), uniqueid);
  980. }
  981. /*
  982. * This is a no-op for now. We're not really interested in the reply, but
  983. * rather in the fact that the server sent one and that server->lstrp
  984. * gets updated.
  985. *
  986. * FIXME: maybe we should consider checking that the reply matches request?
  987. */
  988. static void
  989. smb2_echo_callback(struct mid_q_entry *mid)
  990. {
  991. struct TCP_Server_Info *server = mid->callback_data;
  992. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  993. unsigned int credits_received = 1;
  994. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  995. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  996. DeleteMidQEntry(mid);
  997. add_credits(server, credits_received, CIFS_ECHO_OP);
  998. }
  999. int
  1000. SMB2_echo(struct TCP_Server_Info *server)
  1001. {
  1002. struct smb2_echo_req *req;
  1003. int rc = 0;
  1004. struct kvec iov;
  1005. cFYI(1, "In echo request");
  1006. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1007. if (rc)
  1008. return rc;
  1009. req->hdr.CreditRequest = cpu_to_le16(1);
  1010. iov.iov_base = (char *)req;
  1011. /* 4 for rfc1002 length field */
  1012. iov.iov_len = get_rfc1002_length(req) + 4;
  1013. rc = cifs_call_async(server, &iov, 1, NULL, smb2_echo_callback, server,
  1014. CIFS_ECHO_OP);
  1015. if (rc)
  1016. cFYI(1, "Echo request failed: %d", rc);
  1017. cifs_small_buf_release(req);
  1018. return rc;
  1019. }
  1020. int
  1021. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1022. u64 volatile_fid)
  1023. {
  1024. struct smb2_flush_req *req;
  1025. struct TCP_Server_Info *server;
  1026. struct cifs_ses *ses = tcon->ses;
  1027. struct kvec iov[1];
  1028. int resp_buftype;
  1029. int rc = 0;
  1030. cFYI(1, "Flush");
  1031. if (ses && (ses->server))
  1032. server = ses->server;
  1033. else
  1034. return -EIO;
  1035. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1036. if (rc)
  1037. return rc;
  1038. req->PersistentFileId = persistent_fid;
  1039. req->VolatileFileId = volatile_fid;
  1040. iov[0].iov_base = (char *)req;
  1041. /* 4 for rfc1002 length field */
  1042. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1043. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1044. if ((rc != 0) && tcon)
  1045. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1046. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1047. return rc;
  1048. }
  1049. /*
  1050. * To form a chain of read requests, any read requests after the first should
  1051. * have the end_of_chain boolean set to true.
  1052. */
  1053. static int
  1054. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1055. unsigned int remaining_bytes, int request_type)
  1056. {
  1057. int rc = -EACCES;
  1058. struct smb2_read_req *req = NULL;
  1059. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1060. if (rc)
  1061. return rc;
  1062. if (io_parms->tcon->ses->server == NULL)
  1063. return -ECONNABORTED;
  1064. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1065. req->PersistentFileId = io_parms->persistent_fid;
  1066. req->VolatileFileId = io_parms->volatile_fid;
  1067. req->ReadChannelInfoOffset = 0; /* reserved */
  1068. req->ReadChannelInfoLength = 0; /* reserved */
  1069. req->Channel = 0; /* reserved */
  1070. req->MinimumCount = 0;
  1071. req->Length = cpu_to_le32(io_parms->length);
  1072. req->Offset = cpu_to_le64(io_parms->offset);
  1073. if (request_type & CHAINED_REQUEST) {
  1074. if (!(request_type & END_OF_CHAIN)) {
  1075. /* 4 for rfc1002 length field */
  1076. req->hdr.NextCommand =
  1077. cpu_to_le32(get_rfc1002_length(req) + 4);
  1078. } else /* END_OF_CHAIN */
  1079. req->hdr.NextCommand = 0;
  1080. if (request_type & RELATED_REQUEST) {
  1081. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1082. /*
  1083. * Related requests use info from previous read request
  1084. * in chain.
  1085. */
  1086. req->hdr.SessionId = 0xFFFFFFFF;
  1087. req->hdr.TreeId = 0xFFFFFFFF;
  1088. req->PersistentFileId = 0xFFFFFFFF;
  1089. req->VolatileFileId = 0xFFFFFFFF;
  1090. }
  1091. }
  1092. if (remaining_bytes > io_parms->length)
  1093. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1094. else
  1095. req->RemainingBytes = 0;
  1096. iov[0].iov_base = (char *)req;
  1097. /* 4 for rfc1002 length field */
  1098. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1099. return rc;
  1100. }
  1101. static void
  1102. smb2_readv_callback(struct mid_q_entry *mid)
  1103. {
  1104. struct cifs_readdata *rdata = mid->callback_data;
  1105. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1106. struct TCP_Server_Info *server = tcon->ses->server;
  1107. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov[0].iov_base;
  1108. unsigned int credits_received = 1;
  1109. struct smb_rqst rqst = { .rq_iov = rdata->iov,
  1110. .rq_nvec = rdata->nr_iov };
  1111. cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
  1112. mid->mid, mid->mid_state, rdata->result, rdata->bytes);
  1113. switch (mid->mid_state) {
  1114. case MID_RESPONSE_RECEIVED:
  1115. credits_received = le16_to_cpu(buf->CreditRequest);
  1116. /* result already set, check signature */
  1117. if (server->sec_mode &
  1118. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
  1119. int rc;
  1120. rc = smb2_verify_signature(&rqst, server);
  1121. if (rc)
  1122. cERROR(1, "SMB signature verification returned "
  1123. "error = %d", rc);
  1124. }
  1125. /* FIXME: should this be counted toward the initiating task? */
  1126. task_io_account_read(rdata->bytes);
  1127. cifs_stats_bytes_read(tcon, rdata->bytes);
  1128. break;
  1129. case MID_REQUEST_SUBMITTED:
  1130. case MID_RETRY_NEEDED:
  1131. rdata->result = -EAGAIN;
  1132. break;
  1133. default:
  1134. if (rdata->result != -ENODATA)
  1135. rdata->result = -EIO;
  1136. }
  1137. if (rdata->result)
  1138. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1139. queue_work(cifsiod_wq, &rdata->work);
  1140. DeleteMidQEntry(mid);
  1141. add_credits(server, credits_received, 0);
  1142. }
  1143. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1144. int
  1145. smb2_async_readv(struct cifs_readdata *rdata)
  1146. {
  1147. int rc;
  1148. struct smb2_hdr *buf;
  1149. struct cifs_io_parms io_parms;
  1150. cFYI(1, "%s: offset=%llu bytes=%u", __func__,
  1151. rdata->offset, rdata->bytes);
  1152. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1153. io_parms.offset = rdata->offset;
  1154. io_parms.length = rdata->bytes;
  1155. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1156. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1157. io_parms.pid = rdata->pid;
  1158. rc = smb2_new_read_req(&rdata->iov[0], &io_parms, 0, 0);
  1159. if (rc)
  1160. return rc;
  1161. buf = (struct smb2_hdr *)rdata->iov[0].iov_base;
  1162. /* 4 for rfc1002 length field */
  1163. rdata->iov[0].iov_len = get_rfc1002_length(rdata->iov[0].iov_base) + 4;
  1164. kref_get(&rdata->refcount);
  1165. rc = cifs_call_async(io_parms.tcon->ses->server, rdata->iov, 1,
  1166. cifs_readv_receive, smb2_readv_callback,
  1167. rdata, 0);
  1168. if (rc)
  1169. kref_put(&rdata->refcount, cifs_readdata_release);
  1170. cifs_small_buf_release(buf);
  1171. return rc;
  1172. }
  1173. int
  1174. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1175. unsigned int *nbytes, char **buf, int *buf_type)
  1176. {
  1177. int resp_buftype, rc = -EACCES;
  1178. struct smb2_read_rsp *rsp = NULL;
  1179. struct kvec iov[1];
  1180. *nbytes = 0;
  1181. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1182. if (rc)
  1183. return rc;
  1184. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1185. &resp_buftype, CIFS_LOG_ERROR);
  1186. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1187. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1188. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1189. return 0;
  1190. }
  1191. if (rc) {
  1192. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1193. cERROR(1, "Send error in read = %d", rc);
  1194. } else {
  1195. *nbytes = le32_to_cpu(rsp->DataLength);
  1196. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1197. (*nbytes > io_parms->length)) {
  1198. cFYI(1, "bad length %d for count %d", *nbytes,
  1199. io_parms->length);
  1200. rc = -EIO;
  1201. *nbytes = 0;
  1202. }
  1203. }
  1204. if (*buf) {
  1205. memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
  1206. *nbytes);
  1207. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1208. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1209. *buf = iov[0].iov_base;
  1210. if (resp_buftype == CIFS_SMALL_BUFFER)
  1211. *buf_type = CIFS_SMALL_BUFFER;
  1212. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1213. *buf_type = CIFS_LARGE_BUFFER;
  1214. }
  1215. return rc;
  1216. }
  1217. /*
  1218. * Check the mid_state and signature on received buffer (if any), and queue the
  1219. * workqueue completion task.
  1220. */
  1221. static void
  1222. smb2_writev_callback(struct mid_q_entry *mid)
  1223. {
  1224. struct cifs_writedata *wdata = mid->callback_data;
  1225. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1226. unsigned int written;
  1227. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1228. unsigned int credits_received = 1;
  1229. switch (mid->mid_state) {
  1230. case MID_RESPONSE_RECEIVED:
  1231. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1232. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1233. if (wdata->result != 0)
  1234. break;
  1235. written = le32_to_cpu(rsp->DataLength);
  1236. /*
  1237. * Mask off high 16 bits when bytes written as returned
  1238. * by the server is greater than bytes requested by the
  1239. * client. OS/2 servers are known to set incorrect
  1240. * CountHigh values.
  1241. */
  1242. if (written > wdata->bytes)
  1243. written &= 0xFFFF;
  1244. if (written < wdata->bytes)
  1245. wdata->result = -ENOSPC;
  1246. else
  1247. wdata->bytes = written;
  1248. break;
  1249. case MID_REQUEST_SUBMITTED:
  1250. case MID_RETRY_NEEDED:
  1251. wdata->result = -EAGAIN;
  1252. break;
  1253. default:
  1254. wdata->result = -EIO;
  1255. break;
  1256. }
  1257. if (wdata->result)
  1258. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1259. queue_work(cifsiod_wq, &wdata->work);
  1260. DeleteMidQEntry(mid);
  1261. add_credits(tcon->ses->server, credits_received, 0);
  1262. }
  1263. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1264. int
  1265. smb2_async_writev(struct cifs_writedata *wdata)
  1266. {
  1267. int i, rc = -EACCES;
  1268. struct smb2_write_req *req = NULL;
  1269. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1270. struct kvec *iov = NULL;
  1271. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1272. if (rc)
  1273. goto async_writev_out;
  1274. /* 1 iov per page + 1 for header */
  1275. iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
  1276. if (iov == NULL) {
  1277. rc = -ENOMEM;
  1278. goto async_writev_out;
  1279. }
  1280. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1281. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1282. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1283. req->WriteChannelInfoOffset = 0;
  1284. req->WriteChannelInfoLength = 0;
  1285. req->Channel = 0;
  1286. req->Offset = cpu_to_le64(wdata->offset);
  1287. /* 4 for rfc1002 length field */
  1288. req->DataOffset = cpu_to_le16(
  1289. offsetof(struct smb2_write_req, Buffer) - 4);
  1290. req->RemainingBytes = 0;
  1291. /* 4 for rfc1002 length field and 1 for Buffer */
  1292. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1293. iov[0].iov_base = (char *)req;
  1294. /*
  1295. * This function should marshal up the page array into the kvec
  1296. * array, reserving [0] for the header. It should kmap the pages
  1297. * and set the iov_len properly for each one. It may also set
  1298. * wdata->bytes too.
  1299. */
  1300. cifs_kmap_lock();
  1301. wdata->marshal_iov(iov, wdata);
  1302. cifs_kmap_unlock();
  1303. cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
  1304. req->Length = cpu_to_le32(wdata->bytes);
  1305. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1306. kref_get(&wdata->refcount);
  1307. rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
  1308. NULL, smb2_writev_callback, wdata, 0);
  1309. if (rc)
  1310. kref_put(&wdata->refcount, cifs_writedata_release);
  1311. /* send is done, unmap pages */
  1312. for (i = 0; i < wdata->nr_pages; i++)
  1313. kunmap(wdata->pages[i]);
  1314. async_writev_out:
  1315. cifs_small_buf_release(req);
  1316. kfree(iov);
  1317. return rc;
  1318. }
  1319. /*
  1320. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1321. * The length field from io_parms must be at least 1 and indicates a number of
  1322. * elements with data to write that begins with position 1 in iov array. All
  1323. * data length is specified by count.
  1324. */
  1325. int
  1326. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1327. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1328. {
  1329. int rc = 0;
  1330. struct smb2_write_req *req = NULL;
  1331. struct smb2_write_rsp *rsp = NULL;
  1332. int resp_buftype;
  1333. *nbytes = 0;
  1334. if (n_vec < 1)
  1335. return rc;
  1336. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1337. if (rc)
  1338. return rc;
  1339. if (io_parms->tcon->ses->server == NULL)
  1340. return -ECONNABORTED;
  1341. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1342. req->PersistentFileId = io_parms->persistent_fid;
  1343. req->VolatileFileId = io_parms->volatile_fid;
  1344. req->WriteChannelInfoOffset = 0;
  1345. req->WriteChannelInfoLength = 0;
  1346. req->Channel = 0;
  1347. req->Length = cpu_to_le32(io_parms->length);
  1348. req->Offset = cpu_to_le64(io_parms->offset);
  1349. /* 4 for rfc1002 length field */
  1350. req->DataOffset = cpu_to_le16(
  1351. offsetof(struct smb2_write_req, Buffer) - 4);
  1352. req->RemainingBytes = 0;
  1353. iov[0].iov_base = (char *)req;
  1354. /* 4 for rfc1002 length field and 1 for Buffer */
  1355. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1356. /* length of entire message including data to be written */
  1357. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1358. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1359. &resp_buftype, 0);
  1360. if (rc) {
  1361. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1362. cERROR(1, "Send error in write = %d", rc);
  1363. } else {
  1364. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1365. *nbytes = le32_to_cpu(rsp->DataLength);
  1366. free_rsp_buf(resp_buftype, rsp);
  1367. }
  1368. return rc;
  1369. }
  1370. static unsigned int
  1371. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  1372. {
  1373. int len;
  1374. unsigned int entrycount = 0;
  1375. unsigned int next_offset = 0;
  1376. FILE_DIRECTORY_INFO *entryptr;
  1377. if (bufstart == NULL)
  1378. return 0;
  1379. entryptr = (FILE_DIRECTORY_INFO *)bufstart;
  1380. while (1) {
  1381. entryptr = (FILE_DIRECTORY_INFO *)
  1382. ((char *)entryptr + next_offset);
  1383. if ((char *)entryptr + size > end_of_buf) {
  1384. cERROR(1, "malformed search entry would overflow");
  1385. break;
  1386. }
  1387. len = le32_to_cpu(entryptr->FileNameLength);
  1388. if ((char *)entryptr + len + size > end_of_buf) {
  1389. cERROR(1, "directory entry name would overflow frame "
  1390. "end of buf %p", end_of_buf);
  1391. break;
  1392. }
  1393. *lastentry = (char *)entryptr;
  1394. entrycount++;
  1395. next_offset = le32_to_cpu(entryptr->NextEntryOffset);
  1396. if (!next_offset)
  1397. break;
  1398. }
  1399. return entrycount;
  1400. }
  1401. /*
  1402. * Readdir/FindFirst
  1403. */
  1404. int
  1405. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  1406. u64 persistent_fid, u64 volatile_fid, int index,
  1407. struct cifs_search_info *srch_inf)
  1408. {
  1409. struct smb2_query_directory_req *req;
  1410. struct smb2_query_directory_rsp *rsp = NULL;
  1411. struct kvec iov[2];
  1412. int rc = 0;
  1413. int len;
  1414. int resp_buftype;
  1415. unsigned char *bufptr;
  1416. struct TCP_Server_Info *server;
  1417. struct cifs_ses *ses = tcon->ses;
  1418. __le16 asteriks = cpu_to_le16('*');
  1419. char *end_of_smb;
  1420. unsigned int output_size = CIFSMaxBufSize;
  1421. size_t info_buf_size;
  1422. if (ses && (ses->server))
  1423. server = ses->server;
  1424. else
  1425. return -EIO;
  1426. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  1427. if (rc)
  1428. return rc;
  1429. switch (srch_inf->info_level) {
  1430. case SMB_FIND_FILE_DIRECTORY_INFO:
  1431. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  1432. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  1433. break;
  1434. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  1435. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  1436. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  1437. break;
  1438. default:
  1439. cERROR(1, "info level %u isn't supported",
  1440. srch_inf->info_level);
  1441. rc = -EINVAL;
  1442. goto qdir_exit;
  1443. }
  1444. req->FileIndex = cpu_to_le32(index);
  1445. req->PersistentFileId = persistent_fid;
  1446. req->VolatileFileId = volatile_fid;
  1447. len = 0x2;
  1448. bufptr = req->Buffer;
  1449. memcpy(bufptr, &asteriks, len);
  1450. req->FileNameOffset =
  1451. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  1452. req->FileNameLength = cpu_to_le16(len);
  1453. /*
  1454. * BB could be 30 bytes or so longer if we used SMB2 specific
  1455. * buffer lengths, but this is safe and close enough.
  1456. */
  1457. output_size = min_t(unsigned int, output_size, server->maxBuf);
  1458. output_size = min_t(unsigned int, output_size, 2 << 15);
  1459. req->OutputBufferLength = cpu_to_le32(output_size);
  1460. iov[0].iov_base = (char *)req;
  1461. /* 4 for RFC1001 length and 1 for Buffer */
  1462. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1463. iov[1].iov_base = (char *)(req->Buffer);
  1464. iov[1].iov_len = len;
  1465. inc_rfc1001_len(req, len - 1 /* Buffer */);
  1466. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  1467. if (rc) {
  1468. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  1469. goto qdir_exit;
  1470. }
  1471. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  1472. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1473. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1474. info_buf_size);
  1475. if (rc)
  1476. goto qdir_exit;
  1477. srch_inf->unicode = true;
  1478. if (srch_inf->ntwrk_buf_start) {
  1479. if (srch_inf->smallBuf)
  1480. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  1481. else
  1482. cifs_buf_release(srch_inf->ntwrk_buf_start);
  1483. }
  1484. srch_inf->ntwrk_buf_start = (char *)rsp;
  1485. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  1486. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  1487. /* 4 for rfc1002 length field */
  1488. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  1489. srch_inf->entries_in_buffer =
  1490. num_entries(srch_inf->srch_entries_start, end_of_smb,
  1491. &srch_inf->last_entry, info_buf_size);
  1492. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  1493. cFYI(1, "num entries %d last_index %lld srch start %p srch end %p",
  1494. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  1495. srch_inf->srch_entries_start, srch_inf->last_entry);
  1496. if (resp_buftype == CIFS_LARGE_BUFFER)
  1497. srch_inf->smallBuf = false;
  1498. else if (resp_buftype == CIFS_SMALL_BUFFER)
  1499. srch_inf->smallBuf = true;
  1500. else
  1501. cERROR(1, "illegal search buffer type");
  1502. if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
  1503. srch_inf->endOfSearch = 1;
  1504. else
  1505. srch_inf->endOfSearch = 0;
  1506. return rc;
  1507. qdir_exit:
  1508. free_rsp_buf(resp_buftype, rsp);
  1509. return rc;
  1510. }
  1511. static int
  1512. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1513. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  1514. unsigned int num, void **data, unsigned int *size)
  1515. {
  1516. struct smb2_set_info_req *req;
  1517. struct smb2_set_info_rsp *rsp = NULL;
  1518. struct kvec *iov;
  1519. int rc = 0;
  1520. int resp_buftype;
  1521. unsigned int i;
  1522. struct TCP_Server_Info *server;
  1523. struct cifs_ses *ses = tcon->ses;
  1524. if (ses && (ses->server))
  1525. server = ses->server;
  1526. else
  1527. return -EIO;
  1528. if (!num)
  1529. return -EINVAL;
  1530. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  1531. if (!iov)
  1532. return -ENOMEM;
  1533. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  1534. if (rc) {
  1535. kfree(iov);
  1536. return rc;
  1537. }
  1538. req->hdr.ProcessId = cpu_to_le32(pid);
  1539. req->InfoType = SMB2_O_INFO_FILE;
  1540. req->FileInfoClass = info_class;
  1541. req->PersistentFileId = persistent_fid;
  1542. req->VolatileFileId = volatile_fid;
  1543. /* 4 for RFC1001 length and 1 for Buffer */
  1544. req->BufferOffset =
  1545. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  1546. req->BufferLength = cpu_to_le32(*size);
  1547. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  1548. memcpy(req->Buffer, *data, *size);
  1549. iov[0].iov_base = (char *)req;
  1550. /* 4 for RFC1001 length */
  1551. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1552. for (i = 1; i < num; i++) {
  1553. inc_rfc1001_len(req, size[i]);
  1554. le32_add_cpu(&req->BufferLength, size[i]);
  1555. iov[i].iov_base = (char *)data[i];
  1556. iov[i].iov_len = size[i];
  1557. }
  1558. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  1559. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  1560. if (rc != 0) {
  1561. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  1562. goto out;
  1563. }
  1564. if (rsp == NULL) {
  1565. rc = -EIO;
  1566. goto out;
  1567. }
  1568. out:
  1569. free_rsp_buf(resp_buftype, rsp);
  1570. kfree(iov);
  1571. return rc;
  1572. }
  1573. int
  1574. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  1575. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1576. {
  1577. struct smb2_file_rename_info info;
  1578. void **data;
  1579. unsigned int size[2];
  1580. int rc;
  1581. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1582. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1583. if (!data)
  1584. return -ENOMEM;
  1585. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  1586. /* 0 = fail if target already exists */
  1587. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1588. info.FileNameLength = cpu_to_le32(len);
  1589. data[0] = &info;
  1590. size[0] = sizeof(struct smb2_file_rename_info);
  1591. data[1] = target_file;
  1592. size[1] = len + 2 /* null */;
  1593. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1594. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  1595. size);
  1596. kfree(data);
  1597. return rc;
  1598. }
  1599. int
  1600. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  1601. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1602. {
  1603. struct smb2_file_link_info info;
  1604. void **data;
  1605. unsigned int size[2];
  1606. int rc;
  1607. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1608. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1609. if (!data)
  1610. return -ENOMEM;
  1611. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  1612. /* 0 = fail if link already exists */
  1613. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1614. info.FileNameLength = cpu_to_le32(len);
  1615. data[0] = &info;
  1616. size[0] = sizeof(struct smb2_file_link_info);
  1617. data[1] = target_file;
  1618. size[1] = len + 2 /* null */;
  1619. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1620. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  1621. kfree(data);
  1622. return rc;
  1623. }
  1624. int
  1625. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1626. u64 volatile_fid, u32 pid, __le64 *eof)
  1627. {
  1628. struct smb2_file_eof_info info;
  1629. void *data;
  1630. unsigned int size;
  1631. info.EndOfFile = *eof;
  1632. data = &info;
  1633. size = sizeof(struct smb2_file_eof_info);
  1634. return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
  1635. FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  1636. }
  1637. int
  1638. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1639. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  1640. {
  1641. unsigned int size;
  1642. size = sizeof(FILE_BASIC_INFO);
  1643. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1644. current->tgid, FILE_BASIC_INFORMATION, 1,
  1645. (void **)&buf, &size);
  1646. }
  1647. int
  1648. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  1649. const u64 persistent_fid, const u64 volatile_fid,
  1650. __u8 oplock_level)
  1651. {
  1652. int rc;
  1653. struct smb2_oplock_break *req = NULL;
  1654. cFYI(1, "SMB2_oplock_break");
  1655. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1656. if (rc)
  1657. return rc;
  1658. req->VolatileFid = volatile_fid;
  1659. req->PersistentFid = persistent_fid;
  1660. req->OplockLevel = oplock_level;
  1661. req->hdr.CreditRequest = cpu_to_le16(1);
  1662. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1663. /* SMB2 buffer freed by function above */
  1664. if (rc) {
  1665. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1666. cFYI(1, "Send error in Oplock Break = %d", rc);
  1667. }
  1668. return rc;
  1669. }
  1670. static void
  1671. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  1672. struct kstatfs *kst)
  1673. {
  1674. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  1675. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  1676. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  1677. kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
  1678. kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  1679. return;
  1680. }
  1681. static int
  1682. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  1683. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  1684. {
  1685. int rc;
  1686. struct smb2_query_info_req *req;
  1687. cFYI(1, "Query FSInfo level %d", level);
  1688. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  1689. return -EIO;
  1690. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1691. if (rc)
  1692. return rc;
  1693. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  1694. req->FileInfoClass = level;
  1695. req->PersistentFileId = persistent_fid;
  1696. req->VolatileFileId = volatile_fid;
  1697. /* 4 for rfc1002 length field and 1 for pad */
  1698. req->InputBufferOffset =
  1699. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1700. req->OutputBufferLength = cpu_to_le32(
  1701. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  1702. iov->iov_base = (char *)req;
  1703. /* 4 for rfc1002 length field */
  1704. iov->iov_len = get_rfc1002_length(req) + 4;
  1705. return 0;
  1706. }
  1707. int
  1708. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  1709. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  1710. {
  1711. struct smb2_query_info_rsp *rsp = NULL;
  1712. struct kvec iov;
  1713. int rc = 0;
  1714. int resp_buftype;
  1715. struct cifs_ses *ses = tcon->ses;
  1716. struct smb2_fs_full_size_info *info = NULL;
  1717. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  1718. sizeof(struct smb2_fs_full_size_info),
  1719. persistent_fid, volatile_fid);
  1720. if (rc)
  1721. return rc;
  1722. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  1723. if (rc) {
  1724. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1725. goto qinf_exit;
  1726. }
  1727. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  1728. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  1729. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  1730. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1731. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1732. sizeof(struct smb2_fs_full_size_info));
  1733. if (!rc)
  1734. copy_fs_info_to_kstatfs(info, fsdata);
  1735. qinf_exit:
  1736. free_rsp_buf(resp_buftype, iov.iov_base);
  1737. return rc;
  1738. }