smb2pdu.c 98 KB

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