smb2pdu.c 68 KB

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