smb2pdu.c 86 KB

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