smb2pdu.c 86 KB

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