smb2pdu.c 71 KB

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