smb2pdu.c 83 KB

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