smb2pdu.c 59 KB

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