smb2pdu.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182
  1. /*
  2. * fs/cifs/smb2pdu.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2009, 2013
  5. * Etersoft, 2012
  6. * Author(s): Steve French ([email protected])
  7. * Pavel Shilovsky ([email protected]) 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 || smb2_command == SMB2_IOCTL)
  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_STATS
  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. #ifdef CONFIG_CIFS_DFS_UPCALL
  572. req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
  573. #else
  574. req->Capabilities = 0;
  575. #endif /* DFS_UPCALL */
  576. req->Channel = 0; /* MBZ */
  577. sess_data->iov[0].iov_base = (char *)req;
  578. /* 4 for rfc1002 length field and 1 for pad */
  579. sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  580. /*
  581. * This variable will be used to clear the buffer
  582. * allocated above in case of any error in the calling function.
  583. */
  584. sess_data->buf0_type = CIFS_SMALL_BUFFER;
  585. return 0;
  586. }
  587. static void
  588. SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
  589. {
  590. free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
  591. sess_data->buf0_type = CIFS_NO_BUFFER;
  592. }
  593. static int
  594. SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
  595. {
  596. int rc;
  597. struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
  598. /* Testing shows that buffer offset must be at location of Buffer[0] */
  599. req->SecurityBufferOffset =
  600. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  601. 1 /* pad */ - 4 /* rfc1001 len */);
  602. req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
  603. inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
  604. /* BB add code to build os and lm fields */
  605. rc = SendReceive2(sess_data->xid, sess_data->ses,
  606. sess_data->iov, 2,
  607. &sess_data->buf0_type,
  608. CIFS_LOG_ERROR | CIFS_NEG_OP);
  609. return rc;
  610. }
  611. static int
  612. SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
  613. {
  614. int rc = 0;
  615. struct cifs_ses *ses = sess_data->ses;
  616. mutex_lock(&ses->server->srv_mutex);
  617. if (ses->server->ops->generate_signingkey) {
  618. rc = ses->server->ops->generate_signingkey(ses);
  619. if (rc) {
  620. cifs_dbg(FYI,
  621. "SMB3 session key generation failed\n");
  622. mutex_unlock(&ses->server->srv_mutex);
  623. return rc;
  624. }
  625. }
  626. if (!ses->server->session_estab) {
  627. ses->server->sequence_number = 0x2;
  628. ses->server->session_estab = true;
  629. }
  630. mutex_unlock(&ses->server->srv_mutex);
  631. cifs_dbg(FYI, "SMB2/3 session established successfully\n");
  632. spin_lock(&GlobalMid_Lock);
  633. ses->status = CifsGood;
  634. ses->need_reconnect = false;
  635. spin_unlock(&GlobalMid_Lock);
  636. return rc;
  637. }
  638. #ifdef CONFIG_CIFS_UPCALL
  639. static void
  640. SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
  641. {
  642. int rc;
  643. struct cifs_ses *ses = sess_data->ses;
  644. struct cifs_spnego_msg *msg;
  645. struct key *spnego_key = NULL;
  646. struct smb2_sess_setup_rsp *rsp = NULL;
  647. rc = SMB2_sess_alloc_buffer(sess_data);
  648. if (rc)
  649. goto out;
  650. spnego_key = cifs_get_spnego_key(ses);
  651. if (IS_ERR(spnego_key)) {
  652. rc = PTR_ERR(spnego_key);
  653. spnego_key = NULL;
  654. goto out;
  655. }
  656. msg = spnego_key->payload.data[0];
  657. /*
  658. * check version field to make sure that cifs.upcall is
  659. * sending us a response in an expected form
  660. */
  661. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  662. cifs_dbg(VFS,
  663. "bad cifs.upcall version. Expected %d got %d",
  664. CIFS_SPNEGO_UPCALL_VERSION, msg->version);
  665. rc = -EKEYREJECTED;
  666. goto out_put_spnego_key;
  667. }
  668. ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
  669. GFP_KERNEL);
  670. if (!ses->auth_key.response) {
  671. cifs_dbg(VFS,
  672. "Kerberos can't allocate (%u bytes) memory",
  673. msg->sesskey_len);
  674. rc = -ENOMEM;
  675. goto out_put_spnego_key;
  676. }
  677. ses->auth_key.len = msg->sesskey_len;
  678. sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
  679. sess_data->iov[1].iov_len = msg->secblob_len;
  680. rc = SMB2_sess_sendreceive(sess_data);
  681. if (rc)
  682. goto out_put_spnego_key;
  683. rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
  684. ses->Suid = rsp->hdr.SessionId;
  685. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  686. if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
  687. cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
  688. rc = SMB2_sess_establish_session(sess_data);
  689. out_put_spnego_key:
  690. key_invalidate(spnego_key);
  691. key_put(spnego_key);
  692. out:
  693. sess_data->result = rc;
  694. sess_data->func = NULL;
  695. SMB2_sess_free_buffer(sess_data);
  696. }
  697. #else
  698. static void
  699. SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
  700. {
  701. cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
  702. sess_data->result = -EOPNOTSUPP;
  703. sess_data->func = NULL;
  704. }
  705. #endif
  706. static void
  707. SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
  708. static void
  709. SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
  710. {
  711. int rc;
  712. struct cifs_ses *ses = sess_data->ses;
  713. struct smb2_sess_setup_rsp *rsp = NULL;
  714. char *ntlmssp_blob = NULL;
  715. bool use_spnego = false; /* else use raw ntlmssp */
  716. u16 blob_length = 0;
  717. /*
  718. * If memory allocation is successful, caller of this function
  719. * frees it.
  720. */
  721. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  722. if (!ses->ntlmssp) {
  723. rc = -ENOMEM;
  724. goto out_err;
  725. }
  726. ses->ntlmssp->sesskey_per_smbsess = true;
  727. rc = SMB2_sess_alloc_buffer(sess_data);
  728. if (rc)
  729. goto out_err;
  730. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  731. GFP_KERNEL);
  732. if (ntlmssp_blob == NULL) {
  733. rc = -ENOMEM;
  734. goto out;
  735. }
  736. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  737. if (use_spnego) {
  738. /* BB eventually need to add this */
  739. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  740. rc = -EOPNOTSUPP;
  741. goto out;
  742. } else {
  743. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  744. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  745. }
  746. sess_data->iov[1].iov_base = ntlmssp_blob;
  747. sess_data->iov[1].iov_len = blob_length;
  748. rc = SMB2_sess_sendreceive(sess_data);
  749. rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
  750. /* If true, rc here is expected and not an error */
  751. if (sess_data->buf0_type != CIFS_NO_BUFFER &&
  752. rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
  753. rc = 0;
  754. if (rc)
  755. goto out;
  756. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  757. le16_to_cpu(rsp->SecurityBufferOffset)) {
  758. cifs_dbg(VFS, "Invalid security buffer offset %d\n",
  759. le16_to_cpu(rsp->SecurityBufferOffset));
  760. rc = -EIO;
  761. goto out;
  762. }
  763. rc = decode_ntlmssp_challenge(rsp->Buffer,
  764. le16_to_cpu(rsp->SecurityBufferLength), ses);
  765. if (rc)
  766. goto out;
  767. cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
  768. ses->Suid = rsp->hdr.SessionId;
  769. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  770. if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
  771. cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
  772. out:
  773. kfree(ntlmssp_blob);
  774. SMB2_sess_free_buffer(sess_data);
  775. if (!rc) {
  776. sess_data->result = 0;
  777. sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
  778. return;
  779. }
  780. out_err:
  781. kfree(ses->ntlmssp);
  782. ses->ntlmssp = NULL;
  783. sess_data->result = rc;
  784. sess_data->func = NULL;
  785. }
  786. static void
  787. SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
  788. {
  789. int rc;
  790. struct cifs_ses *ses = sess_data->ses;
  791. struct smb2_sess_setup_req *req;
  792. struct smb2_sess_setup_rsp *rsp = NULL;
  793. unsigned char *ntlmssp_blob = NULL;
  794. bool use_spnego = false; /* else use raw ntlmssp */
  795. u16 blob_length = 0;
  796. rc = SMB2_sess_alloc_buffer(sess_data);
  797. if (rc)
  798. goto out;
  799. req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
  800. req->hdr.SessionId = ses->Suid;
  801. rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
  802. sess_data->nls_cp);
  803. if (rc) {
  804. cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
  805. goto out;
  806. }
  807. if (use_spnego) {
  808. /* BB eventually need to add this */
  809. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  810. rc = -EOPNOTSUPP;
  811. goto out;
  812. }
  813. sess_data->iov[1].iov_base = ntlmssp_blob;
  814. sess_data->iov[1].iov_len = blob_length;
  815. rc = SMB2_sess_sendreceive(sess_data);
  816. if (rc)
  817. goto out;
  818. rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
  819. ses->Suid = rsp->hdr.SessionId;
  820. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  821. if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
  822. cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
  823. rc = SMB2_sess_establish_session(sess_data);
  824. out:
  825. kfree(ntlmssp_blob);
  826. SMB2_sess_free_buffer(sess_data);
  827. kfree(ses->ntlmssp);
  828. ses->ntlmssp = NULL;
  829. sess_data->result = rc;
  830. sess_data->func = NULL;
  831. }
  832. static int
  833. SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
  834. {
  835. if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
  836. ses->sectype = RawNTLMSSP;
  837. switch (ses->sectype) {
  838. case Kerberos:
  839. sess_data->func = SMB2_auth_kerberos;
  840. break;
  841. case RawNTLMSSP:
  842. sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
  843. break;
  844. default:
  845. cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
  846. return -EOPNOTSUPP;
  847. }
  848. return 0;
  849. }
  850. int
  851. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  852. const struct nls_table *nls_cp)
  853. {
  854. int rc = 0;
  855. struct TCP_Server_Info *server = ses->server;
  856. struct SMB2_sess_data *sess_data;
  857. cifs_dbg(FYI, "Session Setup\n");
  858. if (!server) {
  859. WARN(1, "%s: server is NULL!\n", __func__);
  860. return -EIO;
  861. }
  862. sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
  863. if (!sess_data)
  864. return -ENOMEM;
  865. rc = SMB2_select_sec(ses, sess_data);
  866. if (rc)
  867. goto out;
  868. sess_data->xid = xid;
  869. sess_data->ses = ses;
  870. sess_data->buf0_type = CIFS_NO_BUFFER;
  871. sess_data->nls_cp = (struct nls_table *) nls_cp;
  872. sess_data->previous_session = ses->Suid;
  873. while (sess_data->func)
  874. sess_data->func(sess_data);
  875. if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
  876. cifs_dbg(VFS, "signing requested but authenticated as guest\n");
  877. rc = sess_data->result;
  878. out:
  879. kfree(sess_data);
  880. return rc;
  881. }
  882. int
  883. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  884. {
  885. struct smb2_logoff_req *req; /* response is also trivial struct */
  886. int rc = 0;
  887. struct TCP_Server_Info *server;
  888. cifs_dbg(FYI, "disconnect session %p\n", ses);
  889. if (ses && (ses->server))
  890. server = ses->server;
  891. else
  892. return -EIO;
  893. /* no need to send SMB logoff if uid already closed due to reconnect */
  894. if (ses->need_reconnect)
  895. goto smb2_session_already_dead;
  896. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  897. if (rc)
  898. return rc;
  899. /* since no tcon, smb2_init can not do this, so do here */
  900. req->hdr.SessionId = ses->Suid;
  901. if (server->sign)
  902. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  903. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  904. /*
  905. * No tcon so can't do
  906. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  907. */
  908. smb2_session_already_dead:
  909. return rc;
  910. }
  911. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  912. {
  913. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  914. }
  915. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  916. /* These are similar values to what Windows uses */
  917. static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
  918. {
  919. tcon->max_chunks = 256;
  920. tcon->max_bytes_chunk = 1048576;
  921. tcon->max_bytes_copy = 16777216;
  922. }
  923. int
  924. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  925. struct cifs_tcon *tcon, const struct nls_table *cp)
  926. {
  927. struct smb2_tree_connect_req *req;
  928. struct smb2_tree_connect_rsp *rsp = NULL;
  929. struct kvec iov[2];
  930. int rc = 0;
  931. int resp_buftype;
  932. int unc_path_len;
  933. struct TCP_Server_Info *server;
  934. __le16 *unc_path = NULL;
  935. cifs_dbg(FYI, "TCON\n");
  936. if ((ses->server) && tree)
  937. server = ses->server;
  938. else
  939. return -EIO;
  940. if ((tcon && tcon->seal) &&
  941. ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
  942. cifs_dbg(VFS, "encryption requested but no server support");
  943. return -EOPNOTSUPP;
  944. }
  945. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  946. if (unc_path == NULL)
  947. return -ENOMEM;
  948. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  949. unc_path_len *= 2;
  950. if (unc_path_len < 2) {
  951. kfree(unc_path);
  952. return -EINVAL;
  953. }
  954. /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
  955. if (tcon)
  956. tcon->tid = 0;
  957. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  958. if (rc) {
  959. kfree(unc_path);
  960. return rc;
  961. }
  962. if (tcon == NULL) {
  963. /* since no tcon, smb2_init can not do this, so do here */
  964. req->hdr.SessionId = ses->Suid;
  965. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  966. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  967. }
  968. iov[0].iov_base = (char *)req;
  969. /* 4 for rfc1002 length field and 1 for pad */
  970. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  971. /* Testing shows that buffer offset must be at location of Buffer[0] */
  972. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  973. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  974. req->PathLength = cpu_to_le16(unc_path_len - 2);
  975. iov[1].iov_base = unc_path;
  976. iov[1].iov_len = unc_path_len;
  977. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  978. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  979. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  980. if (rc != 0) {
  981. if (tcon) {
  982. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  983. tcon->need_reconnect = true;
  984. }
  985. goto tcon_error_exit;
  986. }
  987. if (tcon == NULL) {
  988. ses->ipc_tid = rsp->hdr.TreeId;
  989. goto tcon_exit;
  990. }
  991. switch (rsp->ShareType) {
  992. case SMB2_SHARE_TYPE_DISK:
  993. cifs_dbg(FYI, "connection to disk share\n");
  994. break;
  995. case SMB2_SHARE_TYPE_PIPE:
  996. tcon->ipc = true;
  997. cifs_dbg(FYI, "connection to pipe share\n");
  998. break;
  999. case SMB2_SHARE_TYPE_PRINT:
  1000. tcon->ipc = true;
  1001. cifs_dbg(FYI, "connection to printer\n");
  1002. break;
  1003. default:
  1004. cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
  1005. rc = -EOPNOTSUPP;
  1006. goto tcon_error_exit;
  1007. }
  1008. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  1009. tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
  1010. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  1011. tcon->tidStatus = CifsGood;
  1012. tcon->need_reconnect = false;
  1013. tcon->tid = rsp->hdr.TreeId;
  1014. strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
  1015. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  1016. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  1017. cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
  1018. init_copy_chunk_defaults(tcon);
  1019. if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
  1020. cifs_dbg(VFS, "Encrypted shares not supported");
  1021. if (tcon->ses->server->ops->validate_negotiate)
  1022. rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
  1023. tcon_exit:
  1024. free_rsp_buf(resp_buftype, rsp);
  1025. kfree(unc_path);
  1026. return rc;
  1027. tcon_error_exit:
  1028. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  1029. cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
  1030. }
  1031. goto tcon_exit;
  1032. }
  1033. int
  1034. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  1035. {
  1036. struct smb2_tree_disconnect_req *req; /* response is trivial */
  1037. int rc = 0;
  1038. struct TCP_Server_Info *server;
  1039. struct cifs_ses *ses = tcon->ses;
  1040. cifs_dbg(FYI, "Tree Disconnect\n");
  1041. if (ses && (ses->server))
  1042. server = ses->server;
  1043. else
  1044. return -EIO;
  1045. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  1046. return 0;
  1047. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  1048. if (rc)
  1049. return rc;
  1050. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  1051. if (rc)
  1052. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  1053. return rc;
  1054. }
  1055. static struct create_durable *
  1056. create_durable_buf(void)
  1057. {
  1058. struct create_durable *buf;
  1059. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  1060. if (!buf)
  1061. return NULL;
  1062. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1063. (struct create_durable, Data));
  1064. buf->ccontext.DataLength = cpu_to_le32(16);
  1065. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1066. (struct create_durable, Name));
  1067. buf->ccontext.NameLength = cpu_to_le16(4);
  1068. /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
  1069. buf->Name[0] = 'D';
  1070. buf->Name[1] = 'H';
  1071. buf->Name[2] = 'n';
  1072. buf->Name[3] = 'Q';
  1073. return buf;
  1074. }
  1075. static struct create_durable *
  1076. create_reconnect_durable_buf(struct cifs_fid *fid)
  1077. {
  1078. struct create_durable *buf;
  1079. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  1080. if (!buf)
  1081. return NULL;
  1082. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1083. (struct create_durable, Data));
  1084. buf->ccontext.DataLength = cpu_to_le32(16);
  1085. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1086. (struct create_durable, Name));
  1087. buf->ccontext.NameLength = cpu_to_le16(4);
  1088. buf->Data.Fid.PersistentFileId = fid->persistent_fid;
  1089. buf->Data.Fid.VolatileFileId = fid->volatile_fid;
  1090. /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
  1091. buf->Name[0] = 'D';
  1092. buf->Name[1] = 'H';
  1093. buf->Name[2] = 'n';
  1094. buf->Name[3] = 'C';
  1095. return buf;
  1096. }
  1097. static __u8
  1098. parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
  1099. unsigned int *epoch)
  1100. {
  1101. char *data_offset;
  1102. struct create_context *cc;
  1103. unsigned int next;
  1104. unsigned int remaining;
  1105. char *name;
  1106. data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
  1107. remaining = le32_to_cpu(rsp->CreateContextsLength);
  1108. cc = (struct create_context *)data_offset;
  1109. while (remaining >= sizeof(struct create_context)) {
  1110. name = le16_to_cpu(cc->NameOffset) + (char *)cc;
  1111. if (le16_to_cpu(cc->NameLength) == 4 &&
  1112. strncmp(name, "RqLs", 4) == 0)
  1113. return server->ops->parse_lease_buf(cc, epoch);
  1114. next = le32_to_cpu(cc->Next);
  1115. if (!next)
  1116. break;
  1117. remaining -= next;
  1118. cc = (struct create_context *)((char *)cc + next);
  1119. }
  1120. return 0;
  1121. }
  1122. static int
  1123. add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
  1124. unsigned int *num_iovec, __u8 *oplock)
  1125. {
  1126. struct smb2_create_req *req = iov[0].iov_base;
  1127. unsigned int num = *num_iovec;
  1128. iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
  1129. if (iov[num].iov_base == NULL)
  1130. return -ENOMEM;
  1131. iov[num].iov_len = server->vals->create_lease_size;
  1132. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  1133. if (!req->CreateContextsOffset)
  1134. req->CreateContextsOffset = cpu_to_le32(
  1135. sizeof(struct smb2_create_req) - 4 +
  1136. iov[num - 1].iov_len);
  1137. le32_add_cpu(&req->CreateContextsLength,
  1138. server->vals->create_lease_size);
  1139. inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
  1140. *num_iovec = num + 1;
  1141. return 0;
  1142. }
  1143. static struct create_durable_v2 *
  1144. create_durable_v2_buf(struct cifs_fid *pfid)
  1145. {
  1146. struct create_durable_v2 *buf;
  1147. buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
  1148. if (!buf)
  1149. return NULL;
  1150. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1151. (struct create_durable_v2, dcontext));
  1152. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
  1153. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1154. (struct create_durable_v2, Name));
  1155. buf->ccontext.NameLength = cpu_to_le16(4);
  1156. buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
  1157. buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
  1158. generate_random_uuid(buf->dcontext.CreateGuid);
  1159. memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
  1160. /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
  1161. buf->Name[0] = 'D';
  1162. buf->Name[1] = 'H';
  1163. buf->Name[2] = '2';
  1164. buf->Name[3] = 'Q';
  1165. return buf;
  1166. }
  1167. static struct create_durable_handle_reconnect_v2 *
  1168. create_reconnect_durable_v2_buf(struct cifs_fid *fid)
  1169. {
  1170. struct create_durable_handle_reconnect_v2 *buf;
  1171. buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
  1172. GFP_KERNEL);
  1173. if (!buf)
  1174. return NULL;
  1175. buf->ccontext.DataOffset =
  1176. cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
  1177. dcontext));
  1178. buf->ccontext.DataLength =
  1179. cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
  1180. buf->ccontext.NameOffset =
  1181. cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
  1182. Name));
  1183. buf->ccontext.NameLength = cpu_to_le16(4);
  1184. buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
  1185. buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
  1186. buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
  1187. memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
  1188. /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
  1189. buf->Name[0] = 'D';
  1190. buf->Name[1] = 'H';
  1191. buf->Name[2] = '2';
  1192. buf->Name[3] = 'C';
  1193. return buf;
  1194. }
  1195. static int
  1196. add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
  1197. struct cifs_open_parms *oparms)
  1198. {
  1199. struct smb2_create_req *req = iov[0].iov_base;
  1200. unsigned int num = *num_iovec;
  1201. iov[num].iov_base = create_durable_v2_buf(oparms->fid);
  1202. if (iov[num].iov_base == NULL)
  1203. return -ENOMEM;
  1204. iov[num].iov_len = sizeof(struct create_durable_v2);
  1205. if (!req->CreateContextsOffset)
  1206. req->CreateContextsOffset =
  1207. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  1208. iov[1].iov_len);
  1209. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
  1210. inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
  1211. *num_iovec = num + 1;
  1212. return 0;
  1213. }
  1214. static int
  1215. add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
  1216. struct cifs_open_parms *oparms)
  1217. {
  1218. struct smb2_create_req *req = iov[0].iov_base;
  1219. unsigned int num = *num_iovec;
  1220. /* indicate that we don't need to relock the file */
  1221. oparms->reconnect = false;
  1222. iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
  1223. if (iov[num].iov_base == NULL)
  1224. return -ENOMEM;
  1225. iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
  1226. if (!req->CreateContextsOffset)
  1227. req->CreateContextsOffset =
  1228. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  1229. iov[1].iov_len);
  1230. le32_add_cpu(&req->CreateContextsLength,
  1231. sizeof(struct create_durable_handle_reconnect_v2));
  1232. inc_rfc1001_len(&req->hdr,
  1233. sizeof(struct create_durable_handle_reconnect_v2));
  1234. *num_iovec = num + 1;
  1235. return 0;
  1236. }
  1237. static int
  1238. add_durable_context(struct kvec *iov, unsigned int *num_iovec,
  1239. struct cifs_open_parms *oparms, bool use_persistent)
  1240. {
  1241. struct smb2_create_req *req = iov[0].iov_base;
  1242. unsigned int num = *num_iovec;
  1243. if (use_persistent) {
  1244. if (oparms->reconnect)
  1245. return add_durable_reconnect_v2_context(iov, num_iovec,
  1246. oparms);
  1247. else
  1248. return add_durable_v2_context(iov, num_iovec, oparms);
  1249. }
  1250. if (oparms->reconnect) {
  1251. iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
  1252. /* indicate that we don't need to relock the file */
  1253. oparms->reconnect = false;
  1254. } else
  1255. iov[num].iov_base = create_durable_buf();
  1256. if (iov[num].iov_base == NULL)
  1257. return -ENOMEM;
  1258. iov[num].iov_len = sizeof(struct create_durable);
  1259. if (!req->CreateContextsOffset)
  1260. req->CreateContextsOffset =
  1261. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  1262. iov[1].iov_len);
  1263. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
  1264. inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
  1265. *num_iovec = num + 1;
  1266. return 0;
  1267. }
  1268. int
  1269. SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
  1270. __u8 *oplock, struct smb2_file_all_info *buf,
  1271. struct smb2_err_rsp **err_buf)
  1272. {
  1273. struct smb2_create_req *req;
  1274. struct smb2_create_rsp *rsp;
  1275. struct TCP_Server_Info *server;
  1276. struct cifs_tcon *tcon = oparms->tcon;
  1277. struct cifs_ses *ses = tcon->ses;
  1278. struct kvec iov[4];
  1279. int resp_buftype;
  1280. int uni_path_len;
  1281. __le16 *copy_path = NULL;
  1282. int copy_size;
  1283. int rc = 0;
  1284. unsigned int num_iovecs = 2;
  1285. __u32 file_attributes = 0;
  1286. char *dhc_buf = NULL, *lc_buf = NULL;
  1287. cifs_dbg(FYI, "create/open\n");
  1288. if (ses && (ses->server))
  1289. server = ses->server;
  1290. else
  1291. return -EIO;
  1292. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  1293. if (rc)
  1294. return rc;
  1295. if (oparms->create_options & CREATE_OPTION_READONLY)
  1296. file_attributes |= ATTR_READONLY;
  1297. if (oparms->create_options & CREATE_OPTION_SPECIAL)
  1298. file_attributes |= ATTR_SYSTEM;
  1299. req->ImpersonationLevel = IL_IMPERSONATION;
  1300. req->DesiredAccess = cpu_to_le32(oparms->desired_access);
  1301. /* File attributes ignored on open (used in create though) */
  1302. req->FileAttributes = cpu_to_le32(file_attributes);
  1303. req->ShareAccess = FILE_SHARE_ALL_LE;
  1304. req->CreateDisposition = cpu_to_le32(oparms->disposition);
  1305. req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
  1306. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  1307. /* do not count rfc1001 len field */
  1308. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
  1309. iov[0].iov_base = (char *)req;
  1310. /* 4 for rfc1002 length field */
  1311. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1312. /* MUST set path len (NameLength) to 0 opening root of share */
  1313. req->NameLength = cpu_to_le16(uni_path_len - 2);
  1314. /* -1 since last byte is buf[0] which is sent below (path) */
  1315. iov[0].iov_len--;
  1316. if (uni_path_len % 8 != 0) {
  1317. copy_size = uni_path_len / 8 * 8;
  1318. if (copy_size < uni_path_len)
  1319. copy_size += 8;
  1320. copy_path = kzalloc(copy_size, GFP_KERNEL);
  1321. if (!copy_path)
  1322. return -ENOMEM;
  1323. memcpy((char *)copy_path, (const char *)path,
  1324. uni_path_len);
  1325. uni_path_len = copy_size;
  1326. path = copy_path;
  1327. }
  1328. iov[1].iov_len = uni_path_len;
  1329. iov[1].iov_base = path;
  1330. /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
  1331. inc_rfc1001_len(req, uni_path_len - 1);
  1332. if (!server->oplocks)
  1333. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  1334. if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  1335. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  1336. req->RequestedOplockLevel = *oplock;
  1337. else {
  1338. rc = add_lease_context(server, iov, &num_iovecs, oplock);
  1339. if (rc) {
  1340. cifs_small_buf_release(req);
  1341. kfree(copy_path);
  1342. return rc;
  1343. }
  1344. lc_buf = iov[num_iovecs-1].iov_base;
  1345. }
  1346. if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  1347. /* need to set Next field of lease context if we request it */
  1348. if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
  1349. struct create_context *ccontext =
  1350. (struct create_context *)iov[num_iovecs-1].iov_base;
  1351. ccontext->Next =
  1352. cpu_to_le32(server->vals->create_lease_size);
  1353. }
  1354. rc = add_durable_context(iov, &num_iovecs, oparms,
  1355. tcon->use_persistent);
  1356. if (rc) {
  1357. cifs_small_buf_release(req);
  1358. kfree(copy_path);
  1359. kfree(lc_buf);
  1360. return rc;
  1361. }
  1362. dhc_buf = iov[num_iovecs-1].iov_base;
  1363. }
  1364. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  1365. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  1366. if (rc != 0) {
  1367. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  1368. if (err_buf)
  1369. *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
  1370. GFP_KERNEL);
  1371. goto creat_exit;
  1372. }
  1373. oparms->fid->persistent_fid = rsp->PersistentFileId;
  1374. oparms->fid->volatile_fid = rsp->VolatileFileId;
  1375. if (buf) {
  1376. memcpy(buf, &rsp->CreationTime, 32);
  1377. buf->AllocationSize = rsp->AllocationSize;
  1378. buf->EndOfFile = rsp->EndofFile;
  1379. buf->Attributes = rsp->FileAttributes;
  1380. buf->NumberOfLinks = cpu_to_le32(1);
  1381. buf->DeletePending = 0;
  1382. }
  1383. if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
  1384. *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
  1385. else
  1386. *oplock = rsp->OplockLevel;
  1387. creat_exit:
  1388. kfree(copy_path);
  1389. kfree(lc_buf);
  1390. kfree(dhc_buf);
  1391. free_rsp_buf(resp_buftype, rsp);
  1392. return rc;
  1393. }
  1394. /*
  1395. * SMB2 IOCTL is used for both IOCTLs and FSCTLs
  1396. */
  1397. int
  1398. SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1399. u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
  1400. u32 indatalen, char **out_data, u32 *plen /* returned data len */)
  1401. {
  1402. struct smb2_ioctl_req *req;
  1403. struct smb2_ioctl_rsp *rsp;
  1404. struct TCP_Server_Info *server;
  1405. struct cifs_ses *ses;
  1406. struct kvec iov[2];
  1407. int resp_buftype;
  1408. int num_iovecs;
  1409. int rc = 0;
  1410. cifs_dbg(FYI, "SMB2 IOCTL\n");
  1411. if (out_data != NULL)
  1412. *out_data = NULL;
  1413. /* zero out returned data len, in case of error */
  1414. if (plen)
  1415. *plen = 0;
  1416. if (tcon)
  1417. ses = tcon->ses;
  1418. else
  1419. return -EIO;
  1420. if (ses && (ses->server))
  1421. server = ses->server;
  1422. else
  1423. return -EIO;
  1424. rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
  1425. if (rc)
  1426. return rc;
  1427. req->CtlCode = cpu_to_le32(opcode);
  1428. req->PersistentFileId = persistent_fid;
  1429. req->VolatileFileId = volatile_fid;
  1430. if (indatalen) {
  1431. req->InputCount = cpu_to_le32(indatalen);
  1432. /* do not set InputOffset if no input data */
  1433. req->InputOffset =
  1434. cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
  1435. iov[1].iov_base = in_data;
  1436. iov[1].iov_len = indatalen;
  1437. num_iovecs = 2;
  1438. } else
  1439. num_iovecs = 1;
  1440. req->OutputOffset = 0;
  1441. req->OutputCount = 0; /* MBZ */
  1442. /*
  1443. * Could increase MaxOutputResponse, but that would require more
  1444. * than one credit. Windows typically sets this smaller, but for some
  1445. * ioctls it may be useful to allow server to send more. No point
  1446. * limiting what the server can send as long as fits in one credit
  1447. * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
  1448. * (by default, note that it can be overridden to make max larger)
  1449. * in responses (except for read responses which can be bigger.
  1450. * We may want to bump this limit up
  1451. */
  1452. req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
  1453. if (is_fsctl)
  1454. req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
  1455. else
  1456. req->Flags = 0;
  1457. iov[0].iov_base = (char *)req;
  1458. /*
  1459. * If no input data, the size of ioctl struct in
  1460. * protocol spec still includes a 1 byte data buffer,
  1461. * but if input data passed to ioctl, we do not
  1462. * want to double count this, so we do not send
  1463. * the dummy one byte of data in iovec[0] if sending
  1464. * input data (in iovec[1]). We also must add 4 bytes
  1465. * in first iovec to allow for rfc1002 length field.
  1466. */
  1467. if (indatalen) {
  1468. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1469. inc_rfc1001_len(req, indatalen - 1);
  1470. } else
  1471. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1472. /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
  1473. if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
  1474. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  1475. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  1476. rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
  1477. if ((rc != 0) && (rc != -EINVAL)) {
  1478. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  1479. goto ioctl_exit;
  1480. } else if (rc == -EINVAL) {
  1481. if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
  1482. (opcode != FSCTL_SRV_COPYCHUNK)) {
  1483. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  1484. goto ioctl_exit;
  1485. }
  1486. }
  1487. /* check if caller wants to look at return data or just return rc */
  1488. if ((plen == NULL) || (out_data == NULL))
  1489. goto ioctl_exit;
  1490. *plen = le32_to_cpu(rsp->OutputCount);
  1491. /* We check for obvious errors in the output buffer length and offset */
  1492. if (*plen == 0)
  1493. goto ioctl_exit; /* server returned no data */
  1494. else if (*plen > 0xFF00) {
  1495. cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
  1496. *plen = 0;
  1497. rc = -EIO;
  1498. goto ioctl_exit;
  1499. }
  1500. if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
  1501. cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
  1502. le32_to_cpu(rsp->OutputOffset));
  1503. *plen = 0;
  1504. rc = -EIO;
  1505. goto ioctl_exit;
  1506. }
  1507. *out_data = kmalloc(*plen, GFP_KERNEL);
  1508. if (*out_data == NULL) {
  1509. rc = -ENOMEM;
  1510. goto ioctl_exit;
  1511. }
  1512. memcpy(*out_data,
  1513. (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
  1514. *plen);
  1515. ioctl_exit:
  1516. free_rsp_buf(resp_buftype, rsp);
  1517. return rc;
  1518. }
  1519. /*
  1520. * Individual callers to ioctl worker function follow
  1521. */
  1522. int
  1523. SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  1524. u64 persistent_fid, u64 volatile_fid)
  1525. {
  1526. int rc;
  1527. struct compress_ioctl fsctl_input;
  1528. char *ret_data = NULL;
  1529. fsctl_input.CompressionState =
  1530. cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
  1531. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  1532. FSCTL_SET_COMPRESSION, true /* is_fsctl */,
  1533. (char *)&fsctl_input /* data input */,
  1534. 2 /* in data len */, &ret_data /* out data */, NULL);
  1535. cifs_dbg(FYI, "set compression rc %d\n", rc);
  1536. return rc;
  1537. }
  1538. int
  1539. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  1540. u64 persistent_fid, u64 volatile_fid)
  1541. {
  1542. struct smb2_close_req *req;
  1543. struct smb2_close_rsp *rsp;
  1544. struct TCP_Server_Info *server;
  1545. struct cifs_ses *ses = tcon->ses;
  1546. struct kvec iov[1];
  1547. int resp_buftype;
  1548. int rc = 0;
  1549. cifs_dbg(FYI, "Close\n");
  1550. if (ses && (ses->server))
  1551. server = ses->server;
  1552. else
  1553. return -EIO;
  1554. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  1555. if (rc)
  1556. return rc;
  1557. req->PersistentFileId = persistent_fid;
  1558. req->VolatileFileId = volatile_fid;
  1559. iov[0].iov_base = (char *)req;
  1560. /* 4 for rfc1002 length field */
  1561. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1562. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1563. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  1564. if (rc != 0) {
  1565. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  1566. goto close_exit;
  1567. }
  1568. /* BB FIXME - decode close response, update inode for caching */
  1569. close_exit:
  1570. free_rsp_buf(resp_buftype, rsp);
  1571. return rc;
  1572. }
  1573. static int
  1574. validate_buf(unsigned int offset, unsigned int buffer_length,
  1575. struct smb2_hdr *hdr, unsigned int min_buf_size)
  1576. {
  1577. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  1578. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  1579. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1580. char *end_of_buf = begin_of_buf + buffer_length;
  1581. if (buffer_length < min_buf_size) {
  1582. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  1583. buffer_length, min_buf_size);
  1584. return -EINVAL;
  1585. }
  1586. /* check if beyond RFC1001 maximum length */
  1587. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  1588. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  1589. buffer_length, smb_len);
  1590. return -EINVAL;
  1591. }
  1592. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  1593. cifs_dbg(VFS, "illegal server response, bad offset to data\n");
  1594. return -EINVAL;
  1595. }
  1596. return 0;
  1597. }
  1598. /*
  1599. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  1600. * Caller must free buffer.
  1601. */
  1602. static int
  1603. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  1604. struct smb2_hdr *hdr, unsigned int minbufsize,
  1605. char *data)
  1606. {
  1607. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1608. int rc;
  1609. if (!data)
  1610. return -EINVAL;
  1611. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  1612. if (rc)
  1613. return rc;
  1614. memcpy(data, begin_of_buf, buffer_length);
  1615. return 0;
  1616. }
  1617. static int
  1618. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1619. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  1620. size_t output_len, size_t min_len, void *data)
  1621. {
  1622. struct smb2_query_info_req *req;
  1623. struct smb2_query_info_rsp *rsp = NULL;
  1624. struct kvec iov[2];
  1625. int rc = 0;
  1626. int resp_buftype;
  1627. struct TCP_Server_Info *server;
  1628. struct cifs_ses *ses = tcon->ses;
  1629. cifs_dbg(FYI, "Query Info\n");
  1630. if (ses && (ses->server))
  1631. server = ses->server;
  1632. else
  1633. return -EIO;
  1634. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1635. if (rc)
  1636. return rc;
  1637. req->InfoType = SMB2_O_INFO_FILE;
  1638. req->FileInfoClass = info_class;
  1639. req->PersistentFileId = persistent_fid;
  1640. req->VolatileFileId = volatile_fid;
  1641. /* 4 for rfc1002 length field and 1 for Buffer */
  1642. req->InputBufferOffset =
  1643. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1644. req->OutputBufferLength = cpu_to_le32(output_len);
  1645. iov[0].iov_base = (char *)req;
  1646. /* 4 for rfc1002 length field */
  1647. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1648. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1649. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  1650. if (rc) {
  1651. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1652. goto qinf_exit;
  1653. }
  1654. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1655. le32_to_cpu(rsp->OutputBufferLength),
  1656. &rsp->hdr, min_len, data);
  1657. qinf_exit:
  1658. free_rsp_buf(resp_buftype, rsp);
  1659. return rc;
  1660. }
  1661. int
  1662. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1663. u64 persistent_fid, u64 volatile_fid,
  1664. struct smb2_file_all_info *data)
  1665. {
  1666. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1667. FILE_ALL_INFORMATION,
  1668. sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
  1669. sizeof(struct smb2_file_all_info), data);
  1670. }
  1671. int
  1672. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  1673. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  1674. {
  1675. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1676. FILE_INTERNAL_INFORMATION,
  1677. sizeof(struct smb2_file_internal_info),
  1678. sizeof(struct smb2_file_internal_info), uniqueid);
  1679. }
  1680. /*
  1681. * This is a no-op for now. We're not really interested in the reply, but
  1682. * rather in the fact that the server sent one and that server->lstrp
  1683. * gets updated.
  1684. *
  1685. * FIXME: maybe we should consider checking that the reply matches request?
  1686. */
  1687. static void
  1688. smb2_echo_callback(struct mid_q_entry *mid)
  1689. {
  1690. struct TCP_Server_Info *server = mid->callback_data;
  1691. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  1692. unsigned int credits_received = 1;
  1693. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  1694. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  1695. mutex_lock(&server->srv_mutex);
  1696. DeleteMidQEntry(mid);
  1697. mutex_unlock(&server->srv_mutex);
  1698. add_credits(server, credits_received, CIFS_ECHO_OP);
  1699. }
  1700. void smb2_reconnect_server(struct work_struct *work)
  1701. {
  1702. struct TCP_Server_Info *server = container_of(work,
  1703. struct TCP_Server_Info, reconnect.work);
  1704. struct cifs_ses *ses;
  1705. struct cifs_tcon *tcon, *tcon2;
  1706. struct list_head tmp_list;
  1707. int tcon_exist = false;
  1708. int rc;
  1709. int resched = false;
  1710. /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
  1711. mutex_lock(&server->reconnect_mutex);
  1712. INIT_LIST_HEAD(&tmp_list);
  1713. cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
  1714. spin_lock(&cifs_tcp_ses_lock);
  1715. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  1716. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  1717. if (tcon->need_reconnect || tcon->need_reopen_files) {
  1718. tcon->tc_count++;
  1719. list_add_tail(&tcon->rlist, &tmp_list);
  1720. tcon_exist = true;
  1721. }
  1722. }
  1723. }
  1724. /*
  1725. * Get the reference to server struct to be sure that the last call of
  1726. * cifs_put_tcon() in the loop below won't release the server pointer.
  1727. */
  1728. if (tcon_exist)
  1729. server->srv_count++;
  1730. spin_unlock(&cifs_tcp_ses_lock);
  1731. list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
  1732. rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
  1733. if (!rc)
  1734. cifs_reopen_persistent_handles(tcon);
  1735. else
  1736. resched = true;
  1737. list_del_init(&tcon->rlist);
  1738. cifs_put_tcon(tcon);
  1739. }
  1740. cifs_dbg(FYI, "Reconnecting tcons finished\n");
  1741. if (resched)
  1742. queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
  1743. mutex_unlock(&server->reconnect_mutex);
  1744. /* now we can safely release srv struct */
  1745. if (tcon_exist)
  1746. cifs_put_tcp_session(server, 1);
  1747. }
  1748. int
  1749. SMB2_echo(struct TCP_Server_Info *server)
  1750. {
  1751. struct smb2_echo_req *req;
  1752. int rc = 0;
  1753. struct kvec iov;
  1754. struct smb_rqst rqst = { .rq_iov = &iov,
  1755. .rq_nvec = 1 };
  1756. cifs_dbg(FYI, "In echo request\n");
  1757. if (server->tcpStatus == CifsNeedNegotiate) {
  1758. /* No need to send echo on newly established connections */
  1759. queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
  1760. return rc;
  1761. }
  1762. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1763. if (rc)
  1764. return rc;
  1765. req->hdr.CreditRequest = cpu_to_le16(1);
  1766. iov.iov_base = (char *)req;
  1767. /* 4 for rfc1002 length field */
  1768. iov.iov_len = get_rfc1002_length(req) + 4;
  1769. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
  1770. CIFS_ECHO_OP);
  1771. if (rc)
  1772. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  1773. cifs_small_buf_release(req);
  1774. return rc;
  1775. }
  1776. int
  1777. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1778. u64 volatile_fid)
  1779. {
  1780. struct smb2_flush_req *req;
  1781. struct TCP_Server_Info *server;
  1782. struct cifs_ses *ses = tcon->ses;
  1783. struct kvec iov[1];
  1784. int resp_buftype;
  1785. int rc = 0;
  1786. cifs_dbg(FYI, "Flush\n");
  1787. if (ses && (ses->server))
  1788. server = ses->server;
  1789. else
  1790. return -EIO;
  1791. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1792. if (rc)
  1793. return rc;
  1794. req->PersistentFileId = persistent_fid;
  1795. req->VolatileFileId = volatile_fid;
  1796. iov[0].iov_base = (char *)req;
  1797. /* 4 for rfc1002 length field */
  1798. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1799. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1800. if (rc != 0)
  1801. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1802. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1803. return rc;
  1804. }
  1805. /*
  1806. * To form a chain of read requests, any read requests after the first should
  1807. * have the end_of_chain boolean set to true.
  1808. */
  1809. static int
  1810. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1811. unsigned int remaining_bytes, int request_type)
  1812. {
  1813. int rc = -EACCES;
  1814. struct smb2_read_req *req = NULL;
  1815. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1816. if (rc)
  1817. return rc;
  1818. if (io_parms->tcon->ses->server == NULL)
  1819. return -ECONNABORTED;
  1820. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1821. req->PersistentFileId = io_parms->persistent_fid;
  1822. req->VolatileFileId = io_parms->volatile_fid;
  1823. req->ReadChannelInfoOffset = 0; /* reserved */
  1824. req->ReadChannelInfoLength = 0; /* reserved */
  1825. req->Channel = 0; /* reserved */
  1826. req->MinimumCount = 0;
  1827. req->Length = cpu_to_le32(io_parms->length);
  1828. req->Offset = cpu_to_le64(io_parms->offset);
  1829. if (request_type & CHAINED_REQUEST) {
  1830. if (!(request_type & END_OF_CHAIN)) {
  1831. /* 4 for rfc1002 length field */
  1832. req->hdr.NextCommand =
  1833. cpu_to_le32(get_rfc1002_length(req) + 4);
  1834. } else /* END_OF_CHAIN */
  1835. req->hdr.NextCommand = 0;
  1836. if (request_type & RELATED_REQUEST) {
  1837. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1838. /*
  1839. * Related requests use info from previous read request
  1840. * in chain.
  1841. */
  1842. req->hdr.SessionId = 0xFFFFFFFF;
  1843. req->hdr.TreeId = 0xFFFFFFFF;
  1844. req->PersistentFileId = 0xFFFFFFFF;
  1845. req->VolatileFileId = 0xFFFFFFFF;
  1846. }
  1847. }
  1848. if (remaining_bytes > io_parms->length)
  1849. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1850. else
  1851. req->RemainingBytes = 0;
  1852. iov[0].iov_base = (char *)req;
  1853. /* 4 for rfc1002 length field */
  1854. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1855. return rc;
  1856. }
  1857. static void
  1858. smb2_readv_callback(struct mid_q_entry *mid)
  1859. {
  1860. struct cifs_readdata *rdata = mid->callback_data;
  1861. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1862. struct TCP_Server_Info *server = tcon->ses->server;
  1863. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1864. unsigned int credits_received = 1;
  1865. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1866. .rq_nvec = 1,
  1867. .rq_pages = rdata->pages,
  1868. .rq_npages = rdata->nr_pages,
  1869. .rq_pagesz = rdata->pagesz,
  1870. .rq_tailsz = rdata->tailsz };
  1871. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1872. __func__, mid->mid, mid->mid_state, rdata->result,
  1873. rdata->bytes);
  1874. switch (mid->mid_state) {
  1875. case MID_RESPONSE_RECEIVED:
  1876. credits_received = le16_to_cpu(buf->CreditRequest);
  1877. /* result already set, check signature */
  1878. if (server->sign) {
  1879. int rc;
  1880. rc = smb2_verify_signature(&rqst, server);
  1881. if (rc)
  1882. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1883. rc);
  1884. }
  1885. /* FIXME: should this be counted toward the initiating task? */
  1886. task_io_account_read(rdata->got_bytes);
  1887. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  1888. break;
  1889. case MID_REQUEST_SUBMITTED:
  1890. case MID_RETRY_NEEDED:
  1891. rdata->result = -EAGAIN;
  1892. if (server->sign && rdata->got_bytes)
  1893. /* reset bytes number since we can not check a sign */
  1894. rdata->got_bytes = 0;
  1895. /* FIXME: should this be counted toward the initiating task? */
  1896. task_io_account_read(rdata->got_bytes);
  1897. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  1898. break;
  1899. default:
  1900. if (rdata->result != -ENODATA)
  1901. rdata->result = -EIO;
  1902. }
  1903. if (rdata->result)
  1904. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1905. queue_work(cifsiod_wq, &rdata->work);
  1906. mutex_lock(&server->srv_mutex);
  1907. DeleteMidQEntry(mid);
  1908. mutex_unlock(&server->srv_mutex);
  1909. add_credits(server, credits_received, 0);
  1910. }
  1911. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1912. int
  1913. smb2_async_readv(struct cifs_readdata *rdata)
  1914. {
  1915. int rc, flags = 0;
  1916. struct smb2_hdr *buf;
  1917. struct cifs_io_parms io_parms;
  1918. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1919. .rq_nvec = 1 };
  1920. struct TCP_Server_Info *server;
  1921. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1922. __func__, rdata->offset, rdata->bytes);
  1923. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1924. io_parms.offset = rdata->offset;
  1925. io_parms.length = rdata->bytes;
  1926. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1927. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1928. io_parms.pid = rdata->pid;
  1929. server = io_parms.tcon->ses->server;
  1930. rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
  1931. if (rc) {
  1932. if (rc == -EAGAIN && rdata->credits) {
  1933. /* credits was reset by reconnect */
  1934. rdata->credits = 0;
  1935. /* reduce in_flight value since we won't send the req */
  1936. spin_lock(&server->req_lock);
  1937. server->in_flight--;
  1938. spin_unlock(&server->req_lock);
  1939. }
  1940. return rc;
  1941. }
  1942. buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1943. /* 4 for rfc1002 length field */
  1944. rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
  1945. if (rdata->credits) {
  1946. buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
  1947. SMB2_MAX_BUFFER_SIZE));
  1948. buf->CreditRequest = buf->CreditCharge;
  1949. spin_lock(&server->req_lock);
  1950. server->credits += rdata->credits -
  1951. le16_to_cpu(buf->CreditCharge);
  1952. spin_unlock(&server->req_lock);
  1953. wake_up(&server->request_q);
  1954. flags = CIFS_HAS_CREDITS;
  1955. }
  1956. kref_get(&rdata->refcount);
  1957. rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
  1958. cifs_readv_receive, smb2_readv_callback,
  1959. rdata, flags);
  1960. if (rc) {
  1961. kref_put(&rdata->refcount, cifs_readdata_release);
  1962. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  1963. }
  1964. cifs_small_buf_release(buf);
  1965. return rc;
  1966. }
  1967. int
  1968. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1969. unsigned int *nbytes, char **buf, int *buf_type)
  1970. {
  1971. int resp_buftype, rc = -EACCES;
  1972. struct smb2_read_rsp *rsp = NULL;
  1973. struct kvec iov[1];
  1974. *nbytes = 0;
  1975. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1976. if (rc)
  1977. return rc;
  1978. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1979. &resp_buftype, CIFS_LOG_ERROR);
  1980. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1981. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1982. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1983. return 0;
  1984. }
  1985. if (rc) {
  1986. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1987. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1988. } else {
  1989. *nbytes = le32_to_cpu(rsp->DataLength);
  1990. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1991. (*nbytes > io_parms->length)) {
  1992. cifs_dbg(FYI, "bad length %d for count %d\n",
  1993. *nbytes, io_parms->length);
  1994. rc = -EIO;
  1995. *nbytes = 0;
  1996. }
  1997. }
  1998. if (*buf) {
  1999. memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
  2000. *nbytes);
  2001. free_rsp_buf(resp_buftype, iov[0].iov_base);
  2002. } else if (resp_buftype != CIFS_NO_BUFFER) {
  2003. *buf = iov[0].iov_base;
  2004. if (resp_buftype == CIFS_SMALL_BUFFER)
  2005. *buf_type = CIFS_SMALL_BUFFER;
  2006. else if (resp_buftype == CIFS_LARGE_BUFFER)
  2007. *buf_type = CIFS_LARGE_BUFFER;
  2008. }
  2009. return rc;
  2010. }
  2011. /*
  2012. * Check the mid_state and signature on received buffer (if any), and queue the
  2013. * workqueue completion task.
  2014. */
  2015. static void
  2016. smb2_writev_callback(struct mid_q_entry *mid)
  2017. {
  2018. struct cifs_writedata *wdata = mid->callback_data;
  2019. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  2020. struct TCP_Server_Info *server = tcon->ses->server;
  2021. unsigned int written;
  2022. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  2023. unsigned int credits_received = 1;
  2024. switch (mid->mid_state) {
  2025. case MID_RESPONSE_RECEIVED:
  2026. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  2027. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  2028. if (wdata->result != 0)
  2029. break;
  2030. written = le32_to_cpu(rsp->DataLength);
  2031. /*
  2032. * Mask off high 16 bits when bytes written as returned
  2033. * by the server is greater than bytes requested by the
  2034. * client. OS/2 servers are known to set incorrect
  2035. * CountHigh values.
  2036. */
  2037. if (written > wdata->bytes)
  2038. written &= 0xFFFF;
  2039. if (written < wdata->bytes)
  2040. wdata->result = -ENOSPC;
  2041. else
  2042. wdata->bytes = written;
  2043. break;
  2044. case MID_REQUEST_SUBMITTED:
  2045. case MID_RETRY_NEEDED:
  2046. wdata->result = -EAGAIN;
  2047. break;
  2048. default:
  2049. wdata->result = -EIO;
  2050. break;
  2051. }
  2052. if (wdata->result)
  2053. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  2054. queue_work(cifsiod_wq, &wdata->work);
  2055. mutex_lock(&server->srv_mutex);
  2056. DeleteMidQEntry(mid);
  2057. mutex_unlock(&server->srv_mutex);
  2058. add_credits(tcon->ses->server, credits_received, 0);
  2059. }
  2060. /* smb2_async_writev - send an async write, and set up mid to handle result */
  2061. int
  2062. smb2_async_writev(struct cifs_writedata *wdata,
  2063. void (*release)(struct kref *kref))
  2064. {
  2065. int rc = -EACCES, flags = 0;
  2066. struct smb2_write_req *req = NULL;
  2067. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  2068. struct TCP_Server_Info *server = tcon->ses->server;
  2069. struct kvec iov;
  2070. struct smb_rqst rqst;
  2071. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  2072. if (rc) {
  2073. if (rc == -EAGAIN && wdata->credits) {
  2074. /* credits was reset by reconnect */
  2075. wdata->credits = 0;
  2076. /* reduce in_flight value since we won't send the req */
  2077. spin_lock(&server->req_lock);
  2078. server->in_flight--;
  2079. spin_unlock(&server->req_lock);
  2080. }
  2081. goto async_writev_out;
  2082. }
  2083. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  2084. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  2085. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  2086. req->WriteChannelInfoOffset = 0;
  2087. req->WriteChannelInfoLength = 0;
  2088. req->Channel = 0;
  2089. req->Offset = cpu_to_le64(wdata->offset);
  2090. /* 4 for rfc1002 length field */
  2091. req->DataOffset = cpu_to_le16(
  2092. offsetof(struct smb2_write_req, Buffer) - 4);
  2093. req->RemainingBytes = 0;
  2094. /* 4 for rfc1002 length field and 1 for Buffer */
  2095. iov.iov_len = get_rfc1002_length(req) + 4 - 1;
  2096. iov.iov_base = req;
  2097. rqst.rq_iov = &iov;
  2098. rqst.rq_nvec = 1;
  2099. rqst.rq_pages = wdata->pages;
  2100. rqst.rq_npages = wdata->nr_pages;
  2101. rqst.rq_pagesz = wdata->pagesz;
  2102. rqst.rq_tailsz = wdata->tailsz;
  2103. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  2104. wdata->offset, wdata->bytes);
  2105. req->Length = cpu_to_le32(wdata->bytes);
  2106. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  2107. if (wdata->credits) {
  2108. req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
  2109. SMB2_MAX_BUFFER_SIZE));
  2110. req->hdr.CreditRequest = req->hdr.CreditCharge;
  2111. spin_lock(&server->req_lock);
  2112. server->credits += wdata->credits -
  2113. le16_to_cpu(req->hdr.CreditCharge);
  2114. spin_unlock(&server->req_lock);
  2115. wake_up(&server->request_q);
  2116. flags = CIFS_HAS_CREDITS;
  2117. }
  2118. kref_get(&wdata->refcount);
  2119. rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
  2120. flags);
  2121. if (rc) {
  2122. kref_put(&wdata->refcount, release);
  2123. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  2124. }
  2125. async_writev_out:
  2126. cifs_small_buf_release(req);
  2127. return rc;
  2128. }
  2129. /*
  2130. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  2131. * The length field from io_parms must be at least 1 and indicates a number of
  2132. * elements with data to write that begins with position 1 in iov array. All
  2133. * data length is specified by count.
  2134. */
  2135. int
  2136. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  2137. unsigned int *nbytes, struct kvec *iov, int n_vec)
  2138. {
  2139. int rc = 0;
  2140. struct smb2_write_req *req = NULL;
  2141. struct smb2_write_rsp *rsp = NULL;
  2142. int resp_buftype;
  2143. *nbytes = 0;
  2144. if (n_vec < 1)
  2145. return rc;
  2146. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  2147. if (rc)
  2148. return rc;
  2149. if (io_parms->tcon->ses->server == NULL)
  2150. return -ECONNABORTED;
  2151. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  2152. req->PersistentFileId = io_parms->persistent_fid;
  2153. req->VolatileFileId = io_parms->volatile_fid;
  2154. req->WriteChannelInfoOffset = 0;
  2155. req->WriteChannelInfoLength = 0;
  2156. req->Channel = 0;
  2157. req->Length = cpu_to_le32(io_parms->length);
  2158. req->Offset = cpu_to_le64(io_parms->offset);
  2159. /* 4 for rfc1002 length field */
  2160. req->DataOffset = cpu_to_le16(
  2161. offsetof(struct smb2_write_req, Buffer) - 4);
  2162. req->RemainingBytes = 0;
  2163. iov[0].iov_base = (char *)req;
  2164. /* 4 for rfc1002 length field and 1 for Buffer */
  2165. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  2166. /* length of entire message including data to be written */
  2167. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  2168. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  2169. &resp_buftype, 0);
  2170. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  2171. if (rc) {
  2172. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  2173. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  2174. } else
  2175. *nbytes = le32_to_cpu(rsp->DataLength);
  2176. free_rsp_buf(resp_buftype, rsp);
  2177. return rc;
  2178. }
  2179. static unsigned int
  2180. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  2181. {
  2182. int len;
  2183. unsigned int entrycount = 0;
  2184. unsigned int next_offset = 0;
  2185. char *entryptr;
  2186. FILE_DIRECTORY_INFO *dir_info;
  2187. if (bufstart == NULL)
  2188. return 0;
  2189. entryptr = bufstart;
  2190. while (1) {
  2191. if (entryptr + next_offset < entryptr ||
  2192. entryptr + next_offset > end_of_buf ||
  2193. entryptr + next_offset + size > end_of_buf) {
  2194. cifs_dbg(VFS, "malformed search entry would overflow\n");
  2195. break;
  2196. }
  2197. entryptr = entryptr + next_offset;
  2198. dir_info = (FILE_DIRECTORY_INFO *)entryptr;
  2199. len = le32_to_cpu(dir_info->FileNameLength);
  2200. if (entryptr + len < entryptr ||
  2201. entryptr + len > end_of_buf ||
  2202. entryptr + len + size > end_of_buf) {
  2203. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  2204. end_of_buf);
  2205. break;
  2206. }
  2207. *lastentry = entryptr;
  2208. entrycount++;
  2209. next_offset = le32_to_cpu(dir_info->NextEntryOffset);
  2210. if (!next_offset)
  2211. break;
  2212. }
  2213. return entrycount;
  2214. }
  2215. /*
  2216. * Readdir/FindFirst
  2217. */
  2218. int
  2219. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  2220. u64 persistent_fid, u64 volatile_fid, int index,
  2221. struct cifs_search_info *srch_inf)
  2222. {
  2223. struct smb2_query_directory_req *req;
  2224. struct smb2_query_directory_rsp *rsp = NULL;
  2225. struct kvec iov[2];
  2226. int rc = 0;
  2227. int len;
  2228. int resp_buftype = CIFS_NO_BUFFER;
  2229. unsigned char *bufptr;
  2230. struct TCP_Server_Info *server;
  2231. struct cifs_ses *ses = tcon->ses;
  2232. __le16 asteriks = cpu_to_le16('*');
  2233. char *end_of_smb;
  2234. unsigned int output_size = CIFSMaxBufSize;
  2235. size_t info_buf_size;
  2236. if (ses && (ses->server))
  2237. server = ses->server;
  2238. else
  2239. return -EIO;
  2240. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  2241. if (rc)
  2242. return rc;
  2243. switch (srch_inf->info_level) {
  2244. case SMB_FIND_FILE_DIRECTORY_INFO:
  2245. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  2246. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  2247. break;
  2248. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  2249. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  2250. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  2251. break;
  2252. default:
  2253. cifs_dbg(VFS, "info level %u isn't supported\n",
  2254. srch_inf->info_level);
  2255. rc = -EINVAL;
  2256. goto qdir_exit;
  2257. }
  2258. req->FileIndex = cpu_to_le32(index);
  2259. req->PersistentFileId = persistent_fid;
  2260. req->VolatileFileId = volatile_fid;
  2261. len = 0x2;
  2262. bufptr = req->Buffer;
  2263. memcpy(bufptr, &asteriks, len);
  2264. req->FileNameOffset =
  2265. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  2266. req->FileNameLength = cpu_to_le16(len);
  2267. /*
  2268. * BB could be 30 bytes or so longer if we used SMB2 specific
  2269. * buffer lengths, but this is safe and close enough.
  2270. */
  2271. output_size = min_t(unsigned int, output_size, server->maxBuf);
  2272. output_size = min_t(unsigned int, output_size, 2 << 15);
  2273. req->OutputBufferLength = cpu_to_le32(output_size);
  2274. iov[0].iov_base = (char *)req;
  2275. /* 4 for RFC1001 length and 1 for Buffer */
  2276. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  2277. iov[1].iov_base = (char *)(req->Buffer);
  2278. iov[1].iov_len = len;
  2279. inc_rfc1001_len(req, len - 1 /* Buffer */);
  2280. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  2281. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  2282. if (rc) {
  2283. if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
  2284. srch_inf->endOfSearch = true;
  2285. rc = 0;
  2286. } else
  2287. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  2288. goto qdir_exit;
  2289. }
  2290. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  2291. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  2292. info_buf_size);
  2293. if (rc)
  2294. goto qdir_exit;
  2295. srch_inf->unicode = true;
  2296. if (srch_inf->ntwrk_buf_start) {
  2297. if (srch_inf->smallBuf)
  2298. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  2299. else
  2300. cifs_buf_release(srch_inf->ntwrk_buf_start);
  2301. }
  2302. srch_inf->ntwrk_buf_start = (char *)rsp;
  2303. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  2304. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  2305. /* 4 for rfc1002 length field */
  2306. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  2307. srch_inf->entries_in_buffer =
  2308. num_entries(srch_inf->srch_entries_start, end_of_smb,
  2309. &srch_inf->last_entry, info_buf_size);
  2310. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  2311. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  2312. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  2313. srch_inf->srch_entries_start, srch_inf->last_entry);
  2314. if (resp_buftype == CIFS_LARGE_BUFFER)
  2315. srch_inf->smallBuf = false;
  2316. else if (resp_buftype == CIFS_SMALL_BUFFER)
  2317. srch_inf->smallBuf = true;
  2318. else
  2319. cifs_dbg(VFS, "illegal search buffer type\n");
  2320. return rc;
  2321. qdir_exit:
  2322. free_rsp_buf(resp_buftype, rsp);
  2323. return rc;
  2324. }
  2325. static int
  2326. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  2327. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  2328. unsigned int num, void **data, unsigned int *size)
  2329. {
  2330. struct smb2_set_info_req *req;
  2331. struct smb2_set_info_rsp *rsp = NULL;
  2332. struct kvec *iov;
  2333. int rc = 0;
  2334. int resp_buftype;
  2335. unsigned int i;
  2336. struct TCP_Server_Info *server;
  2337. struct cifs_ses *ses = tcon->ses;
  2338. if (ses && (ses->server))
  2339. server = ses->server;
  2340. else
  2341. return -EIO;
  2342. if (!num)
  2343. return -EINVAL;
  2344. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  2345. if (!iov)
  2346. return -ENOMEM;
  2347. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  2348. if (rc) {
  2349. kfree(iov);
  2350. return rc;
  2351. }
  2352. req->hdr.ProcessId = cpu_to_le32(pid);
  2353. req->InfoType = SMB2_O_INFO_FILE;
  2354. req->FileInfoClass = info_class;
  2355. req->PersistentFileId = persistent_fid;
  2356. req->VolatileFileId = volatile_fid;
  2357. /* 4 for RFC1001 length and 1 for Buffer */
  2358. req->BufferOffset =
  2359. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  2360. req->BufferLength = cpu_to_le32(*size);
  2361. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  2362. memcpy(req->Buffer, *data, *size);
  2363. iov[0].iov_base = (char *)req;
  2364. /* 4 for RFC1001 length */
  2365. iov[0].iov_len = get_rfc1002_length(req) + 4;
  2366. for (i = 1; i < num; i++) {
  2367. inc_rfc1001_len(req, size[i]);
  2368. le32_add_cpu(&req->BufferLength, size[i]);
  2369. iov[i].iov_base = (char *)data[i];
  2370. iov[i].iov_len = size[i];
  2371. }
  2372. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  2373. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  2374. if (rc != 0)
  2375. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  2376. free_rsp_buf(resp_buftype, rsp);
  2377. kfree(iov);
  2378. return rc;
  2379. }
  2380. int
  2381. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  2382. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  2383. {
  2384. struct smb2_file_rename_info info;
  2385. void **data;
  2386. unsigned int size[2];
  2387. int rc;
  2388. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  2389. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  2390. if (!data)
  2391. return -ENOMEM;
  2392. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  2393. /* 0 = fail if target already exists */
  2394. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  2395. info.FileNameLength = cpu_to_le32(len);
  2396. data[0] = &info;
  2397. size[0] = sizeof(struct smb2_file_rename_info);
  2398. data[1] = target_file;
  2399. size[1] = len + 2 /* null */;
  2400. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2401. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  2402. size);
  2403. kfree(data);
  2404. return rc;
  2405. }
  2406. int
  2407. SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
  2408. u64 persistent_fid, u64 volatile_fid)
  2409. {
  2410. __u8 delete_pending = 1;
  2411. void *data;
  2412. unsigned int size;
  2413. data = &delete_pending;
  2414. size = 1; /* sizeof __u8 */
  2415. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2416. current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
  2417. &size);
  2418. }
  2419. int
  2420. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  2421. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  2422. {
  2423. struct smb2_file_link_info info;
  2424. void **data;
  2425. unsigned int size[2];
  2426. int rc;
  2427. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  2428. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  2429. if (!data)
  2430. return -ENOMEM;
  2431. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  2432. /* 0 = fail if link already exists */
  2433. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  2434. info.FileNameLength = cpu_to_le32(len);
  2435. data[0] = &info;
  2436. size[0] = sizeof(struct smb2_file_link_info);
  2437. data[1] = target_file;
  2438. size[1] = len + 2 /* null */;
  2439. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2440. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  2441. kfree(data);
  2442. return rc;
  2443. }
  2444. int
  2445. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  2446. u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
  2447. {
  2448. struct smb2_file_eof_info info;
  2449. void *data;
  2450. unsigned int size;
  2451. info.EndOfFile = *eof;
  2452. data = &info;
  2453. size = sizeof(struct smb2_file_eof_info);
  2454. if (is_falloc)
  2455. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2456. pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
  2457. else
  2458. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2459. pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  2460. }
  2461. int
  2462. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  2463. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  2464. {
  2465. unsigned int size;
  2466. size = sizeof(FILE_BASIC_INFO);
  2467. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  2468. current->tgid, FILE_BASIC_INFORMATION, 1,
  2469. (void **)&buf, &size);
  2470. }
  2471. int
  2472. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  2473. const u64 persistent_fid, const u64 volatile_fid,
  2474. __u8 oplock_level)
  2475. {
  2476. int rc;
  2477. struct smb2_oplock_break *req = NULL;
  2478. cifs_dbg(FYI, "SMB2_oplock_break\n");
  2479. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  2480. if (rc)
  2481. return rc;
  2482. req->VolatileFid = volatile_fid;
  2483. req->PersistentFid = persistent_fid;
  2484. req->OplockLevel = oplock_level;
  2485. req->hdr.CreditRequest = cpu_to_le16(1);
  2486. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  2487. /* SMB2 buffer freed by function above */
  2488. if (rc) {
  2489. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  2490. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  2491. }
  2492. return rc;
  2493. }
  2494. static void
  2495. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  2496. struct kstatfs *kst)
  2497. {
  2498. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  2499. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  2500. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  2501. kst->f_bfree = kst->f_bavail =
  2502. le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  2503. return;
  2504. }
  2505. static int
  2506. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  2507. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  2508. {
  2509. int rc;
  2510. struct smb2_query_info_req *req;
  2511. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  2512. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  2513. return -EIO;
  2514. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  2515. if (rc)
  2516. return rc;
  2517. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  2518. req->FileInfoClass = level;
  2519. req->PersistentFileId = persistent_fid;
  2520. req->VolatileFileId = volatile_fid;
  2521. /* 4 for rfc1002 length field and 1 for pad */
  2522. req->InputBufferOffset =
  2523. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  2524. req->OutputBufferLength = cpu_to_le32(
  2525. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  2526. iov->iov_base = (char *)req;
  2527. /* 4 for rfc1002 length field */
  2528. iov->iov_len = get_rfc1002_length(req) + 4;
  2529. return 0;
  2530. }
  2531. int
  2532. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  2533. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  2534. {
  2535. struct smb2_query_info_rsp *rsp = NULL;
  2536. struct kvec iov;
  2537. int rc = 0;
  2538. int resp_buftype;
  2539. struct cifs_ses *ses = tcon->ses;
  2540. struct smb2_fs_full_size_info *info = NULL;
  2541. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  2542. sizeof(struct smb2_fs_full_size_info),
  2543. persistent_fid, volatile_fid);
  2544. if (rc)
  2545. return rc;
  2546. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  2547. if (rc) {
  2548. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  2549. goto qfsinf_exit;
  2550. }
  2551. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  2552. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  2553. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  2554. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  2555. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  2556. sizeof(struct smb2_fs_full_size_info));
  2557. if (!rc)
  2558. copy_fs_info_to_kstatfs(info, fsdata);
  2559. qfsinf_exit:
  2560. free_rsp_buf(resp_buftype, iov.iov_base);
  2561. return rc;
  2562. }
  2563. int
  2564. SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
  2565. u64 persistent_fid, u64 volatile_fid, int level)
  2566. {
  2567. struct smb2_query_info_rsp *rsp = NULL;
  2568. struct kvec iov;
  2569. int rc = 0;
  2570. int resp_buftype, max_len, min_len;
  2571. struct cifs_ses *ses = tcon->ses;
  2572. unsigned int rsp_len, offset;
  2573. if (level == FS_DEVICE_INFORMATION) {
  2574. max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
  2575. min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
  2576. } else if (level == FS_ATTRIBUTE_INFORMATION) {
  2577. max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
  2578. min_len = MIN_FS_ATTR_INFO_SIZE;
  2579. } else if (level == FS_SECTOR_SIZE_INFORMATION) {
  2580. max_len = sizeof(struct smb3_fs_ss_info);
  2581. min_len = sizeof(struct smb3_fs_ss_info);
  2582. } else {
  2583. cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
  2584. return -EINVAL;
  2585. }
  2586. rc = build_qfs_info_req(&iov, tcon, level, max_len,
  2587. persistent_fid, volatile_fid);
  2588. if (rc)
  2589. return rc;
  2590. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  2591. if (rc) {
  2592. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  2593. goto qfsattr_exit;
  2594. }
  2595. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  2596. rsp_len = le32_to_cpu(rsp->OutputBufferLength);
  2597. offset = le16_to_cpu(rsp->OutputBufferOffset);
  2598. rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
  2599. if (rc)
  2600. goto qfsattr_exit;
  2601. if (level == FS_ATTRIBUTE_INFORMATION)
  2602. memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
  2603. + (char *)&rsp->hdr, min_t(unsigned int,
  2604. rsp_len, max_len));
  2605. else if (level == FS_DEVICE_INFORMATION)
  2606. memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
  2607. + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
  2608. else if (level == FS_SECTOR_SIZE_INFORMATION) {
  2609. struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
  2610. (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
  2611. tcon->ss_flags = le32_to_cpu(ss_info->Flags);
  2612. tcon->perf_sector_size =
  2613. le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
  2614. }
  2615. qfsattr_exit:
  2616. free_rsp_buf(resp_buftype, iov.iov_base);
  2617. return rc;
  2618. }
  2619. int
  2620. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  2621. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  2622. const __u32 num_lock, struct smb2_lock_element *buf)
  2623. {
  2624. int rc = 0;
  2625. struct smb2_lock_req *req = NULL;
  2626. struct kvec iov[2];
  2627. int resp_buf_type;
  2628. unsigned int count;
  2629. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  2630. rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
  2631. if (rc)
  2632. return rc;
  2633. req->hdr.ProcessId = cpu_to_le32(pid);
  2634. req->LockCount = cpu_to_le16(num_lock);
  2635. req->PersistentFileId = persist_fid;
  2636. req->VolatileFileId = volatile_fid;
  2637. count = num_lock * sizeof(struct smb2_lock_element);
  2638. inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
  2639. iov[0].iov_base = (char *)req;
  2640. /* 4 for rfc1002 length field and count for all locks */
  2641. iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
  2642. iov[1].iov_base = (char *)buf;
  2643. iov[1].iov_len = count;
  2644. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  2645. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  2646. if (rc) {
  2647. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  2648. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  2649. }
  2650. return rc;
  2651. }
  2652. int
  2653. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  2654. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  2655. const __u64 length, const __u64 offset, const __u32 lock_flags,
  2656. const bool wait)
  2657. {
  2658. struct smb2_lock_element lock;
  2659. lock.Offset = cpu_to_le64(offset);
  2660. lock.Length = cpu_to_le64(length);
  2661. lock.Flags = cpu_to_le32(lock_flags);
  2662. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  2663. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  2664. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  2665. }
  2666. int
  2667. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  2668. __u8 *lease_key, const __le32 lease_state)
  2669. {
  2670. int rc;
  2671. struct smb2_lease_ack *req = NULL;
  2672. cifs_dbg(FYI, "SMB2_lease_break\n");
  2673. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  2674. if (rc)
  2675. return rc;
  2676. req->hdr.CreditRequest = cpu_to_le16(1);
  2677. req->StructureSize = cpu_to_le16(36);
  2678. inc_rfc1001_len(req, 12);
  2679. memcpy(req->LeaseKey, lease_key, 16);
  2680. req->LeaseState = lease_state;
  2681. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  2682. /* SMB2 buffer freed by function above */
  2683. if (rc) {
  2684. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  2685. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  2686. }
  2687. return rc;
  2688. }