smb2pdu.c 58 KB

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