smb2pdu.c 73 KB

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