smb2pdu.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
  365. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  366. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  367. /* BB Do we need to validate the SecurityMode? */
  368. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  369. server->capabilities = le32_to_cpu(rsp->Capabilities);
  370. /* Internal types */
  371. server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
  372. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  373. &rsp->hdr);
  374. /*
  375. * See MS-SMB2 section 2.2.4: if no blob, client picks default which
  376. * for us will be
  377. * ses->sectype = RawNTLMSSP;
  378. * but for time being this is our only auth choice so doesn't matter.
  379. * We just found a server which sets blob length to zero expecting raw.
  380. */
  381. if (blob_length == 0)
  382. cifs_dbg(FYI, "missing security blob on negprot\n");
  383. rc = cifs_enable_signing(server, ses->sign);
  384. #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
  385. if (rc)
  386. goto neg_exit;
  387. if (blob_length)
  388. rc = decode_neg_token_init(security_blob, blob_length,
  389. &server->sec_type);
  390. if (rc == 1)
  391. rc = 0;
  392. else if (rc == 0) {
  393. rc = -EIO;
  394. goto neg_exit;
  395. }
  396. #endif
  397. neg_exit:
  398. free_rsp_buf(resp_buftype, rsp);
  399. return rc;
  400. }
  401. int
  402. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  403. const struct nls_table *nls_cp)
  404. {
  405. struct smb2_sess_setup_req *req;
  406. struct smb2_sess_setup_rsp *rsp = NULL;
  407. struct kvec iov[2];
  408. int rc = 0;
  409. int resp_buftype;
  410. __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
  411. struct TCP_Server_Info *server = ses->server;
  412. u16 blob_length = 0;
  413. char *security_blob;
  414. char *ntlmssp_blob = NULL;
  415. bool use_spnego = false; /* else use raw ntlmssp */
  416. cifs_dbg(FYI, "Session Setup\n");
  417. if (!server) {
  418. WARN(1, "%s: server is NULL!\n", __func__);
  419. return -EIO;
  420. }
  421. /*
  422. * If we are here due to reconnect, free per-smb session key
  423. * in case signing was required.
  424. */
  425. kfree(ses->auth_key.response);
  426. ses->auth_key.response = NULL;
  427. /*
  428. * If memory allocation is successful, caller of this function
  429. * frees it.
  430. */
  431. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  432. if (!ses->ntlmssp)
  433. return -ENOMEM;
  434. ses->ntlmssp->sesskey_per_smbsess = true;
  435. /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
  436. ses->sectype = RawNTLMSSP;
  437. ssetup_ntlmssp_authenticate:
  438. if (phase == NtLmChallenge)
  439. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  440. rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
  441. if (rc)
  442. return rc;
  443. req->hdr.SessionId = 0; /* First session, not a reauthenticate */
  444. req->VcNumber = 0; /* MBZ */
  445. /* to enable echos and oplocks */
  446. req->hdr.CreditRequest = cpu_to_le16(3);
  447. /* only one of SMB2 signing flags may be set in SMB2 request */
  448. if (server->sign)
  449. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  450. else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
  451. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
  452. else
  453. req->SecurityMode = 0;
  454. req->Capabilities = 0;
  455. req->Channel = 0; /* MBZ */
  456. iov[0].iov_base = (char *)req;
  457. /* 4 for rfc1002 length field and 1 for pad */
  458. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  459. if (phase == NtLmNegotiate) {
  460. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  461. GFP_KERNEL);
  462. if (ntlmssp_blob == NULL) {
  463. rc = -ENOMEM;
  464. goto ssetup_exit;
  465. }
  466. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  467. if (use_spnego) {
  468. /* blob_length = build_spnego_ntlmssp_blob(
  469. &security_blob,
  470. sizeof(struct _NEGOTIATE_MESSAGE),
  471. ntlmssp_blob); */
  472. /* BB eventually need to add this */
  473. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  474. rc = -EOPNOTSUPP;
  475. kfree(ntlmssp_blob);
  476. goto ssetup_exit;
  477. } else {
  478. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  479. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  480. security_blob = ntlmssp_blob;
  481. }
  482. } else if (phase == NtLmAuthenticate) {
  483. req->hdr.SessionId = ses->Suid;
  484. ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
  485. GFP_KERNEL);
  486. if (ntlmssp_blob == NULL) {
  487. rc = -ENOMEM;
  488. goto ssetup_exit;
  489. }
  490. rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
  491. nls_cp);
  492. if (rc) {
  493. cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
  494. rc);
  495. goto ssetup_exit; /* BB double check error handling */
  496. }
  497. if (use_spnego) {
  498. /* blob_length = build_spnego_ntlmssp_blob(
  499. &security_blob,
  500. blob_length,
  501. ntlmssp_blob); */
  502. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  503. rc = -EOPNOTSUPP;
  504. kfree(ntlmssp_blob);
  505. goto ssetup_exit;
  506. } else {
  507. security_blob = ntlmssp_blob;
  508. }
  509. } else {
  510. cifs_dbg(VFS, "illegal ntlmssp phase\n");
  511. rc = -EIO;
  512. goto ssetup_exit;
  513. }
  514. /* Testing shows that buffer offset must be at location of Buffer[0] */
  515. req->SecurityBufferOffset =
  516. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  517. 1 /* pad */ - 4 /* rfc1001 len */);
  518. req->SecurityBufferLength = cpu_to_le16(blob_length);
  519. iov[1].iov_base = security_blob;
  520. iov[1].iov_len = blob_length;
  521. inc_rfc1001_len(req, blob_length - 1 /* pad */);
  522. /* BB add code to build os and lm fields */
  523. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
  524. CIFS_LOG_ERROR | CIFS_NEG_OP);
  525. kfree(security_blob);
  526. rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
  527. if (resp_buftype != CIFS_NO_BUFFER &&
  528. rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
  529. if (phase != NtLmNegotiate) {
  530. cifs_dbg(VFS, "Unexpected more processing error\n");
  531. goto ssetup_exit;
  532. }
  533. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  534. le16_to_cpu(rsp->SecurityBufferOffset)) {
  535. cifs_dbg(VFS, "Invalid security buffer offset %d\n",
  536. le16_to_cpu(rsp->SecurityBufferOffset));
  537. rc = -EIO;
  538. goto ssetup_exit;
  539. }
  540. /* NTLMSSP Negotiate sent now processing challenge (response) */
  541. phase = NtLmChallenge; /* process ntlmssp challenge */
  542. rc = 0; /* MORE_PROCESSING is not an error here but expected */
  543. ses->Suid = rsp->hdr.SessionId;
  544. rc = decode_ntlmssp_challenge(rsp->Buffer,
  545. le16_to_cpu(rsp->SecurityBufferLength), ses);
  546. }
  547. /*
  548. * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
  549. * but at least the raw NTLMSSP case works.
  550. */
  551. /*
  552. * No tcon so can't do
  553. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  554. */
  555. if (rc != 0)
  556. goto ssetup_exit;
  557. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  558. ssetup_exit:
  559. free_rsp_buf(resp_buftype, rsp);
  560. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  561. if ((phase == NtLmChallenge) && (rc == 0))
  562. goto ssetup_ntlmssp_authenticate;
  563. if (!rc) {
  564. mutex_lock(&server->srv_mutex);
  565. if (server->sign && server->ops->generate_signingkey) {
  566. rc = server->ops->generate_signingkey(ses);
  567. kfree(ses->auth_key.response);
  568. ses->auth_key.response = NULL;
  569. if (rc) {
  570. cifs_dbg(FYI,
  571. "SMB3 session key generation failed\n");
  572. mutex_unlock(&server->srv_mutex);
  573. goto keygen_exit;
  574. }
  575. }
  576. if (!server->session_estab) {
  577. server->sequence_number = 0x2;
  578. server->session_estab = true;
  579. }
  580. mutex_unlock(&server->srv_mutex);
  581. cifs_dbg(FYI, "SMB2/3 session established successfully\n");
  582. spin_lock(&GlobalMid_Lock);
  583. ses->status = CifsGood;
  584. ses->need_reconnect = false;
  585. spin_unlock(&GlobalMid_Lock);
  586. }
  587. keygen_exit:
  588. if (!server->sign) {
  589. kfree(ses->auth_key.response);
  590. ses->auth_key.response = NULL;
  591. }
  592. kfree(ses->ntlmssp);
  593. return rc;
  594. }
  595. int
  596. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  597. {
  598. struct smb2_logoff_req *req; /* response is also trivial struct */
  599. int rc = 0;
  600. struct TCP_Server_Info *server;
  601. cifs_dbg(FYI, "disconnect session %p\n", ses);
  602. if (ses && (ses->server))
  603. server = ses->server;
  604. else
  605. return -EIO;
  606. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  607. if (rc)
  608. return rc;
  609. /* since no tcon, smb2_init can not do this, so do here */
  610. req->hdr.SessionId = ses->Suid;
  611. if (server->sign)
  612. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  613. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  614. /*
  615. * No tcon so can't do
  616. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  617. */
  618. return rc;
  619. }
  620. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  621. {
  622. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  623. }
  624. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  625. int
  626. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  627. struct cifs_tcon *tcon, const struct nls_table *cp)
  628. {
  629. struct smb2_tree_connect_req *req;
  630. struct smb2_tree_connect_rsp *rsp = NULL;
  631. struct kvec iov[2];
  632. int rc = 0;
  633. int resp_buftype;
  634. int unc_path_len;
  635. struct TCP_Server_Info *server;
  636. __le16 *unc_path = NULL;
  637. cifs_dbg(FYI, "TCON\n");
  638. if ((ses->server) && tree)
  639. server = ses->server;
  640. else
  641. return -EIO;
  642. if (tcon && tcon->bad_network_name)
  643. return -ENOENT;
  644. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  645. if (unc_path == NULL)
  646. return -ENOMEM;
  647. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  648. unc_path_len *= 2;
  649. if (unc_path_len < 2) {
  650. kfree(unc_path);
  651. return -EINVAL;
  652. }
  653. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  654. if (rc) {
  655. kfree(unc_path);
  656. return rc;
  657. }
  658. if (tcon == NULL) {
  659. /* since no tcon, smb2_init can not do this, so do here */
  660. req->hdr.SessionId = ses->Suid;
  661. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  662. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  663. }
  664. iov[0].iov_base = (char *)req;
  665. /* 4 for rfc1002 length field and 1 for pad */
  666. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  667. /* Testing shows that buffer offset must be at location of Buffer[0] */
  668. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  669. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  670. req->PathLength = cpu_to_le16(unc_path_len - 2);
  671. iov[1].iov_base = unc_path;
  672. iov[1].iov_len = unc_path_len;
  673. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  674. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  675. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  676. if (rc != 0) {
  677. if (tcon) {
  678. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  679. tcon->need_reconnect = true;
  680. }
  681. goto tcon_error_exit;
  682. }
  683. if (tcon == NULL) {
  684. ses->ipc_tid = rsp->hdr.TreeId;
  685. goto tcon_exit;
  686. }
  687. if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
  688. cifs_dbg(FYI, "connection to disk share\n");
  689. else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
  690. tcon->ipc = true;
  691. cifs_dbg(FYI, "connection to pipe share\n");
  692. } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
  693. tcon->print = true;
  694. cifs_dbg(FYI, "connection to printer\n");
  695. } else {
  696. cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
  697. rc = -EOPNOTSUPP;
  698. goto tcon_error_exit;
  699. }
  700. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  701. tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
  702. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  703. tcon->tidStatus = CifsGood;
  704. tcon->need_reconnect = false;
  705. tcon->tid = rsp->hdr.TreeId;
  706. strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
  707. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  708. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  709. cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
  710. tcon_exit:
  711. free_rsp_buf(resp_buftype, rsp);
  712. kfree(unc_path);
  713. return rc;
  714. tcon_error_exit:
  715. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  716. cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
  717. tcon->bad_network_name = true;
  718. }
  719. goto tcon_exit;
  720. }
  721. int
  722. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  723. {
  724. struct smb2_tree_disconnect_req *req; /* response is trivial */
  725. int rc = 0;
  726. struct TCP_Server_Info *server;
  727. struct cifs_ses *ses = tcon->ses;
  728. cifs_dbg(FYI, "Tree Disconnect\n");
  729. if (ses && (ses->server))
  730. server = ses->server;
  731. else
  732. return -EIO;
  733. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  734. return 0;
  735. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  736. if (rc)
  737. return rc;
  738. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  739. if (rc)
  740. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  741. return rc;
  742. }
  743. static struct create_durable *
  744. create_durable_buf(void)
  745. {
  746. struct create_durable *buf;
  747. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  748. if (!buf)
  749. return NULL;
  750. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  751. (struct create_durable, Data));
  752. buf->ccontext.DataLength = cpu_to_le32(16);
  753. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  754. (struct create_durable, Name));
  755. buf->ccontext.NameLength = cpu_to_le16(4);
  756. buf->Name[0] = 'D';
  757. buf->Name[1] = 'H';
  758. buf->Name[2] = 'n';
  759. buf->Name[3] = 'Q';
  760. return buf;
  761. }
  762. static struct create_durable *
  763. create_reconnect_durable_buf(struct cifs_fid *fid)
  764. {
  765. struct create_durable *buf;
  766. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  767. if (!buf)
  768. return NULL;
  769. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  770. (struct create_durable, Data));
  771. buf->ccontext.DataLength = cpu_to_le32(16);
  772. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  773. (struct create_durable, Name));
  774. buf->ccontext.NameLength = cpu_to_le16(4);
  775. buf->Data.Fid.PersistentFileId = fid->persistent_fid;
  776. buf->Data.Fid.VolatileFileId = fid->volatile_fid;
  777. buf->Name[0] = 'D';
  778. buf->Name[1] = 'H';
  779. buf->Name[2] = 'n';
  780. buf->Name[3] = 'C';
  781. return buf;
  782. }
  783. static __u8
  784. parse_lease_state(struct smb2_create_rsp *rsp)
  785. {
  786. char *data_offset;
  787. struct create_lease *lc;
  788. bool found = false;
  789. unsigned int next = 0;
  790. char *name;
  791. data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
  792. lc = (struct create_lease *)data_offset;
  793. do {
  794. lc = (struct create_lease *)((char *)lc + next);
  795. name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc;
  796. if (le16_to_cpu(lc->ccontext.NameLength) != 4 ||
  797. strncmp(name, "RqLs", 4)) {
  798. next = le32_to_cpu(lc->ccontext.Next);
  799. continue;
  800. }
  801. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  802. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  803. found = true;
  804. break;
  805. } while (next != 0);
  806. if (!found)
  807. return 0;
  808. return le32_to_cpu(lc->lcontext.LeaseState);
  809. }
  810. static int
  811. add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
  812. unsigned int *num_iovec, __u8 *oplock)
  813. {
  814. struct smb2_create_req *req = iov[0].iov_base;
  815. unsigned int num = *num_iovec;
  816. iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
  817. if (iov[num].iov_base == NULL)
  818. return -ENOMEM;
  819. iov[num].iov_len = server->vals->create_lease_size;
  820. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  821. if (!req->CreateContextsOffset)
  822. req->CreateContextsOffset = cpu_to_le32(
  823. sizeof(struct smb2_create_req) - 4 +
  824. iov[num - 1].iov_len);
  825. le32_add_cpu(&req->CreateContextsLength,
  826. server->vals->create_lease_size);
  827. inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
  828. *num_iovec = num + 1;
  829. return 0;
  830. }
  831. static int
  832. add_durable_context(struct kvec *iov, unsigned int *num_iovec,
  833. struct cifs_open_parms *oparms)
  834. {
  835. struct smb2_create_req *req = iov[0].iov_base;
  836. unsigned int num = *num_iovec;
  837. if (oparms->reconnect) {
  838. iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
  839. /* indicate that we don't need to relock the file */
  840. oparms->reconnect = false;
  841. } else
  842. iov[num].iov_base = create_durable_buf();
  843. if (iov[num].iov_base == NULL)
  844. return -ENOMEM;
  845. iov[num].iov_len = sizeof(struct create_durable);
  846. if (!req->CreateContextsOffset)
  847. req->CreateContextsOffset =
  848. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  849. iov[1].iov_len);
  850. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
  851. inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
  852. *num_iovec = num + 1;
  853. return 0;
  854. }
  855. int
  856. SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
  857. __u8 *oplock, struct smb2_file_all_info *buf,
  858. struct smb2_err_rsp **err_buf)
  859. {
  860. struct smb2_create_req *req;
  861. struct smb2_create_rsp *rsp;
  862. struct TCP_Server_Info *server;
  863. struct cifs_tcon *tcon = oparms->tcon;
  864. struct cifs_ses *ses = tcon->ses;
  865. struct kvec iov[4];
  866. int resp_buftype;
  867. int uni_path_len;
  868. __le16 *copy_path = NULL;
  869. int copy_size;
  870. int rc = 0;
  871. unsigned int num_iovecs = 2;
  872. __u32 file_attributes = 0;
  873. cifs_dbg(FYI, "create/open\n");
  874. if (ses && (ses->server))
  875. server = ses->server;
  876. else
  877. return -EIO;
  878. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  879. if (rc)
  880. return rc;
  881. if (oparms->create_options & CREATE_OPTION_READONLY)
  882. file_attributes |= ATTR_READONLY;
  883. req->ImpersonationLevel = IL_IMPERSONATION;
  884. req->DesiredAccess = cpu_to_le32(oparms->desired_access);
  885. /* File attributes ignored on open (used in create though) */
  886. req->FileAttributes = cpu_to_le32(file_attributes);
  887. req->ShareAccess = FILE_SHARE_ALL_LE;
  888. req->CreateDisposition = cpu_to_le32(oparms->disposition);
  889. req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
  890. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  891. /* do not count rfc1001 len field */
  892. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
  893. iov[0].iov_base = (char *)req;
  894. /* 4 for rfc1002 length field */
  895. iov[0].iov_len = get_rfc1002_length(req) + 4;
  896. /* MUST set path len (NameLength) to 0 opening root of share */
  897. req->NameLength = cpu_to_le16(uni_path_len - 2);
  898. /* -1 since last byte is buf[0] which is sent below (path) */
  899. iov[0].iov_len--;
  900. if (uni_path_len % 8 != 0) {
  901. copy_size = uni_path_len / 8 * 8;
  902. if (copy_size < uni_path_len)
  903. copy_size += 8;
  904. copy_path = kzalloc(copy_size, GFP_KERNEL);
  905. if (!copy_path)
  906. return -ENOMEM;
  907. memcpy((char *)copy_path, (const char *)path,
  908. uni_path_len);
  909. uni_path_len = copy_size;
  910. path = copy_path;
  911. }
  912. iov[1].iov_len = uni_path_len;
  913. iov[1].iov_base = path;
  914. /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
  915. inc_rfc1001_len(req, uni_path_len - 1);
  916. if (!server->oplocks)
  917. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  918. if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  919. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  920. req->RequestedOplockLevel = *oplock;
  921. else {
  922. rc = add_lease_context(server, iov, &num_iovecs, oplock);
  923. if (rc) {
  924. cifs_small_buf_release(req);
  925. kfree(copy_path);
  926. return rc;
  927. }
  928. }
  929. if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  930. /* need to set Next field of lease context if we request it */
  931. if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
  932. struct create_context *ccontext =
  933. (struct create_context *)iov[num_iovecs-1].iov_base;
  934. ccontext->Next =
  935. cpu_to_le32(server->vals->create_lease_size);
  936. }
  937. rc = add_durable_context(iov, &num_iovecs, oparms);
  938. if (rc) {
  939. cifs_small_buf_release(req);
  940. kfree(copy_path);
  941. kfree(iov[num_iovecs-1].iov_base);
  942. return rc;
  943. }
  944. }
  945. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  946. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  947. if (rc != 0) {
  948. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  949. if (err_buf)
  950. *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
  951. GFP_KERNEL);
  952. goto creat_exit;
  953. }
  954. oparms->fid->persistent_fid = rsp->PersistentFileId;
  955. oparms->fid->volatile_fid = rsp->VolatileFileId;
  956. if (buf) {
  957. memcpy(buf, &rsp->CreationTime, 32);
  958. buf->AllocationSize = rsp->AllocationSize;
  959. buf->EndOfFile = rsp->EndofFile;
  960. buf->Attributes = rsp->FileAttributes;
  961. buf->NumberOfLinks = cpu_to_le32(1);
  962. buf->DeletePending = 0;
  963. }
  964. if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
  965. *oplock = parse_lease_state(rsp);
  966. else
  967. *oplock = rsp->OplockLevel;
  968. creat_exit:
  969. kfree(copy_path);
  970. free_rsp_buf(resp_buftype, rsp);
  971. return rc;
  972. }
  973. /*
  974. * SMB2 IOCTL is used for both IOCTLs and FSCTLs
  975. */
  976. int
  977. SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  978. u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
  979. u32 indatalen, char **out_data, u32 *plen /* returned data len */)
  980. {
  981. struct smb2_ioctl_req *req;
  982. struct smb2_ioctl_rsp *rsp;
  983. struct TCP_Server_Info *server;
  984. struct cifs_ses *ses = tcon->ses;
  985. struct kvec iov[2];
  986. int resp_buftype;
  987. int num_iovecs;
  988. int rc = 0;
  989. cifs_dbg(FYI, "SMB2 IOCTL\n");
  990. /* zero out returned data len, in case of error */
  991. if (plen)
  992. *plen = 0;
  993. if (ses && (ses->server))
  994. server = ses->server;
  995. else
  996. return -EIO;
  997. rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
  998. if (rc)
  999. return rc;
  1000. req->CtlCode = cpu_to_le32(opcode);
  1001. req->PersistentFileId = persistent_fid;
  1002. req->VolatileFileId = volatile_fid;
  1003. if (indatalen) {
  1004. req->InputCount = cpu_to_le32(indatalen);
  1005. /* do not set InputOffset if no input data */
  1006. req->InputOffset =
  1007. cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
  1008. iov[1].iov_base = in_data;
  1009. iov[1].iov_len = indatalen;
  1010. num_iovecs = 2;
  1011. } else
  1012. num_iovecs = 1;
  1013. req->OutputOffset = 0;
  1014. req->OutputCount = 0; /* MBZ */
  1015. /*
  1016. * Could increase MaxOutputResponse, but that would require more
  1017. * than one credit. Windows typically sets this smaller, but for some
  1018. * ioctls it may be useful to allow server to send more. No point
  1019. * limiting what the server can send as long as fits in one credit
  1020. */
  1021. req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
  1022. if (is_fsctl)
  1023. req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
  1024. else
  1025. req->Flags = 0;
  1026. iov[0].iov_base = (char *)req;
  1027. /* 4 for rfc1002 length field */
  1028. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1029. if (indatalen)
  1030. inc_rfc1001_len(req, indatalen);
  1031. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  1032. rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
  1033. if (rc != 0) {
  1034. if (tcon)
  1035. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  1036. goto ioctl_exit;
  1037. }
  1038. /* check if caller wants to look at return data or just return rc */
  1039. if ((plen == NULL) || (out_data == NULL))
  1040. goto ioctl_exit;
  1041. *plen = le32_to_cpu(rsp->OutputCount);
  1042. /* We check for obvious errors in the output buffer length and offset */
  1043. if (*plen == 0)
  1044. goto ioctl_exit; /* server returned no data */
  1045. else if (*plen > 0xFF00) {
  1046. cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
  1047. *plen = 0;
  1048. rc = -EIO;
  1049. goto ioctl_exit;
  1050. }
  1051. if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
  1052. cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
  1053. le32_to_cpu(rsp->OutputOffset));
  1054. *plen = 0;
  1055. rc = -EIO;
  1056. goto ioctl_exit;
  1057. }
  1058. *out_data = kmalloc(*plen, GFP_KERNEL);
  1059. if (*out_data == NULL) {
  1060. rc = -ENOMEM;
  1061. goto ioctl_exit;
  1062. }
  1063. memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
  1064. *plen);
  1065. ioctl_exit:
  1066. free_rsp_buf(resp_buftype, rsp);
  1067. return rc;
  1068. }
  1069. int
  1070. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  1071. u64 persistent_fid, u64 volatile_fid)
  1072. {
  1073. struct smb2_close_req *req;
  1074. struct smb2_close_rsp *rsp;
  1075. struct TCP_Server_Info *server;
  1076. struct cifs_ses *ses = tcon->ses;
  1077. struct kvec iov[1];
  1078. int resp_buftype;
  1079. int rc = 0;
  1080. cifs_dbg(FYI, "Close\n");
  1081. if (ses && (ses->server))
  1082. server = ses->server;
  1083. else
  1084. return -EIO;
  1085. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  1086. if (rc)
  1087. return rc;
  1088. req->PersistentFileId = persistent_fid;
  1089. req->VolatileFileId = volatile_fid;
  1090. iov[0].iov_base = (char *)req;
  1091. /* 4 for rfc1002 length field */
  1092. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1093. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1094. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  1095. if (rc != 0) {
  1096. if (tcon)
  1097. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  1098. goto close_exit;
  1099. }
  1100. /* BB FIXME - decode close response, update inode for caching */
  1101. close_exit:
  1102. free_rsp_buf(resp_buftype, rsp);
  1103. return rc;
  1104. }
  1105. static int
  1106. validate_buf(unsigned int offset, unsigned int buffer_length,
  1107. struct smb2_hdr *hdr, unsigned int min_buf_size)
  1108. {
  1109. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  1110. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  1111. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1112. char *end_of_buf = begin_of_buf + buffer_length;
  1113. if (buffer_length < min_buf_size) {
  1114. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  1115. buffer_length, min_buf_size);
  1116. return -EINVAL;
  1117. }
  1118. /* check if beyond RFC1001 maximum length */
  1119. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  1120. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  1121. buffer_length, smb_len);
  1122. return -EINVAL;
  1123. }
  1124. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  1125. cifs_dbg(VFS, "illegal server response, bad offset to data\n");
  1126. return -EINVAL;
  1127. }
  1128. return 0;
  1129. }
  1130. /*
  1131. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  1132. * Caller must free buffer.
  1133. */
  1134. static int
  1135. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  1136. struct smb2_hdr *hdr, unsigned int minbufsize,
  1137. char *data)
  1138. {
  1139. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1140. int rc;
  1141. if (!data)
  1142. return -EINVAL;
  1143. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  1144. if (rc)
  1145. return rc;
  1146. memcpy(data, begin_of_buf, buffer_length);
  1147. return 0;
  1148. }
  1149. static int
  1150. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1151. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  1152. size_t output_len, size_t min_len, void *data)
  1153. {
  1154. struct smb2_query_info_req *req;
  1155. struct smb2_query_info_rsp *rsp = NULL;
  1156. struct kvec iov[2];
  1157. int rc = 0;
  1158. int resp_buftype;
  1159. struct TCP_Server_Info *server;
  1160. struct cifs_ses *ses = tcon->ses;
  1161. cifs_dbg(FYI, "Query Info\n");
  1162. if (ses && (ses->server))
  1163. server = ses->server;
  1164. else
  1165. return -EIO;
  1166. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1167. if (rc)
  1168. return rc;
  1169. req->InfoType = SMB2_O_INFO_FILE;
  1170. req->FileInfoClass = info_class;
  1171. req->PersistentFileId = persistent_fid;
  1172. req->VolatileFileId = volatile_fid;
  1173. /* 4 for rfc1002 length field and 1 for Buffer */
  1174. req->InputBufferOffset =
  1175. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1176. req->OutputBufferLength = cpu_to_le32(output_len);
  1177. iov[0].iov_base = (char *)req;
  1178. /* 4 for rfc1002 length field */
  1179. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1180. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1181. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  1182. if (rc) {
  1183. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1184. goto qinf_exit;
  1185. }
  1186. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1187. le32_to_cpu(rsp->OutputBufferLength),
  1188. &rsp->hdr, min_len, data);
  1189. qinf_exit:
  1190. free_rsp_buf(resp_buftype, rsp);
  1191. return rc;
  1192. }
  1193. int
  1194. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1195. u64 persistent_fid, u64 volatile_fid,
  1196. struct smb2_file_all_info *data)
  1197. {
  1198. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1199. FILE_ALL_INFORMATION,
  1200. sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  1201. sizeof(struct smb2_file_all_info), data);
  1202. }
  1203. int
  1204. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  1205. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  1206. {
  1207. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1208. FILE_INTERNAL_INFORMATION,
  1209. sizeof(struct smb2_file_internal_info),
  1210. sizeof(struct smb2_file_internal_info), uniqueid);
  1211. }
  1212. /*
  1213. * This is a no-op for now. We're not really interested in the reply, but
  1214. * rather in the fact that the server sent one and that server->lstrp
  1215. * gets updated.
  1216. *
  1217. * FIXME: maybe we should consider checking that the reply matches request?
  1218. */
  1219. static void
  1220. smb2_echo_callback(struct mid_q_entry *mid)
  1221. {
  1222. struct TCP_Server_Info *server = mid->callback_data;
  1223. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  1224. unsigned int credits_received = 1;
  1225. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  1226. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  1227. DeleteMidQEntry(mid);
  1228. add_credits(server, credits_received, CIFS_ECHO_OP);
  1229. }
  1230. int
  1231. SMB2_echo(struct TCP_Server_Info *server)
  1232. {
  1233. struct smb2_echo_req *req;
  1234. int rc = 0;
  1235. struct kvec iov;
  1236. struct smb_rqst rqst = { .rq_iov = &iov,
  1237. .rq_nvec = 1 };
  1238. cifs_dbg(FYI, "In echo request\n");
  1239. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1240. if (rc)
  1241. return rc;
  1242. req->hdr.CreditRequest = cpu_to_le16(1);
  1243. iov.iov_base = (char *)req;
  1244. /* 4 for rfc1002 length field */
  1245. iov.iov_len = get_rfc1002_length(req) + 4;
  1246. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
  1247. CIFS_ECHO_OP);
  1248. if (rc)
  1249. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  1250. cifs_small_buf_release(req);
  1251. return rc;
  1252. }
  1253. int
  1254. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1255. u64 volatile_fid)
  1256. {
  1257. struct smb2_flush_req *req;
  1258. struct TCP_Server_Info *server;
  1259. struct cifs_ses *ses = tcon->ses;
  1260. struct kvec iov[1];
  1261. int resp_buftype;
  1262. int rc = 0;
  1263. cifs_dbg(FYI, "Flush\n");
  1264. if (ses && (ses->server))
  1265. server = ses->server;
  1266. else
  1267. return -EIO;
  1268. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1269. if (rc)
  1270. return rc;
  1271. req->PersistentFileId = persistent_fid;
  1272. req->VolatileFileId = volatile_fid;
  1273. iov[0].iov_base = (char *)req;
  1274. /* 4 for rfc1002 length field */
  1275. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1276. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1277. if ((rc != 0) && tcon)
  1278. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1279. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1280. return rc;
  1281. }
  1282. /*
  1283. * To form a chain of read requests, any read requests after the first should
  1284. * have the end_of_chain boolean set to true.
  1285. */
  1286. static int
  1287. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1288. unsigned int remaining_bytes, int request_type)
  1289. {
  1290. int rc = -EACCES;
  1291. struct smb2_read_req *req = NULL;
  1292. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1293. if (rc)
  1294. return rc;
  1295. if (io_parms->tcon->ses->server == NULL)
  1296. return -ECONNABORTED;
  1297. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1298. req->PersistentFileId = io_parms->persistent_fid;
  1299. req->VolatileFileId = io_parms->volatile_fid;
  1300. req->ReadChannelInfoOffset = 0; /* reserved */
  1301. req->ReadChannelInfoLength = 0; /* reserved */
  1302. req->Channel = 0; /* reserved */
  1303. req->MinimumCount = 0;
  1304. req->Length = cpu_to_le32(io_parms->length);
  1305. req->Offset = cpu_to_le64(io_parms->offset);
  1306. if (request_type & CHAINED_REQUEST) {
  1307. if (!(request_type & END_OF_CHAIN)) {
  1308. /* 4 for rfc1002 length field */
  1309. req->hdr.NextCommand =
  1310. cpu_to_le32(get_rfc1002_length(req) + 4);
  1311. } else /* END_OF_CHAIN */
  1312. req->hdr.NextCommand = 0;
  1313. if (request_type & RELATED_REQUEST) {
  1314. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1315. /*
  1316. * Related requests use info from previous read request
  1317. * in chain.
  1318. */
  1319. req->hdr.SessionId = 0xFFFFFFFF;
  1320. req->hdr.TreeId = 0xFFFFFFFF;
  1321. req->PersistentFileId = 0xFFFFFFFF;
  1322. req->VolatileFileId = 0xFFFFFFFF;
  1323. }
  1324. }
  1325. if (remaining_bytes > io_parms->length)
  1326. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1327. else
  1328. req->RemainingBytes = 0;
  1329. iov[0].iov_base = (char *)req;
  1330. /* 4 for rfc1002 length field */
  1331. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1332. return rc;
  1333. }
  1334. static void
  1335. smb2_readv_callback(struct mid_q_entry *mid)
  1336. {
  1337. struct cifs_readdata *rdata = mid->callback_data;
  1338. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1339. struct TCP_Server_Info *server = tcon->ses->server;
  1340. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1341. unsigned int credits_received = 1;
  1342. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1343. .rq_nvec = 1,
  1344. .rq_pages = rdata->pages,
  1345. .rq_npages = rdata->nr_pages,
  1346. .rq_pagesz = rdata->pagesz,
  1347. .rq_tailsz = rdata->tailsz };
  1348. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1349. __func__, mid->mid, mid->mid_state, rdata->result,
  1350. rdata->bytes);
  1351. switch (mid->mid_state) {
  1352. case MID_RESPONSE_RECEIVED:
  1353. credits_received = le16_to_cpu(buf->CreditRequest);
  1354. /* result already set, check signature */
  1355. if (server->sign) {
  1356. int rc;
  1357. rc = smb2_verify_signature(&rqst, server);
  1358. if (rc)
  1359. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1360. rc);
  1361. }
  1362. /* FIXME: should this be counted toward the initiating task? */
  1363. task_io_account_read(rdata->bytes);
  1364. cifs_stats_bytes_read(tcon, rdata->bytes);
  1365. break;
  1366. case MID_REQUEST_SUBMITTED:
  1367. case MID_RETRY_NEEDED:
  1368. rdata->result = -EAGAIN;
  1369. break;
  1370. default:
  1371. if (rdata->result != -ENODATA)
  1372. rdata->result = -EIO;
  1373. }
  1374. if (rdata->result)
  1375. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1376. queue_work(cifsiod_wq, &rdata->work);
  1377. DeleteMidQEntry(mid);
  1378. add_credits(server, credits_received, 0);
  1379. }
  1380. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1381. int
  1382. smb2_async_readv(struct cifs_readdata *rdata)
  1383. {
  1384. int rc;
  1385. struct smb2_hdr *buf;
  1386. struct cifs_io_parms io_parms;
  1387. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1388. .rq_nvec = 1 };
  1389. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1390. __func__, rdata->offset, rdata->bytes);
  1391. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1392. io_parms.offset = rdata->offset;
  1393. io_parms.length = rdata->bytes;
  1394. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1395. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1396. io_parms.pid = rdata->pid;
  1397. rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
  1398. if (rc)
  1399. return rc;
  1400. buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1401. /* 4 for rfc1002 length field */
  1402. rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
  1403. kref_get(&rdata->refcount);
  1404. rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
  1405. cifs_readv_receive, smb2_readv_callback,
  1406. rdata, 0);
  1407. if (rc) {
  1408. kref_put(&rdata->refcount, cifs_readdata_release);
  1409. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  1410. }
  1411. cifs_small_buf_release(buf);
  1412. return rc;
  1413. }
  1414. int
  1415. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1416. unsigned int *nbytes, char **buf, int *buf_type)
  1417. {
  1418. int resp_buftype, rc = -EACCES;
  1419. struct smb2_read_rsp *rsp = NULL;
  1420. struct kvec iov[1];
  1421. *nbytes = 0;
  1422. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1423. if (rc)
  1424. return rc;
  1425. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1426. &resp_buftype, CIFS_LOG_ERROR);
  1427. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1428. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1429. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1430. return 0;
  1431. }
  1432. if (rc) {
  1433. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1434. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1435. } else {
  1436. *nbytes = le32_to_cpu(rsp->DataLength);
  1437. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1438. (*nbytes > io_parms->length)) {
  1439. cifs_dbg(FYI, "bad length %d for count %d\n",
  1440. *nbytes, io_parms->length);
  1441. rc = -EIO;
  1442. *nbytes = 0;
  1443. }
  1444. }
  1445. if (*buf) {
  1446. memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
  1447. *nbytes);
  1448. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1449. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1450. *buf = iov[0].iov_base;
  1451. if (resp_buftype == CIFS_SMALL_BUFFER)
  1452. *buf_type = CIFS_SMALL_BUFFER;
  1453. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1454. *buf_type = CIFS_LARGE_BUFFER;
  1455. }
  1456. return rc;
  1457. }
  1458. /*
  1459. * Check the mid_state and signature on received buffer (if any), and queue the
  1460. * workqueue completion task.
  1461. */
  1462. static void
  1463. smb2_writev_callback(struct mid_q_entry *mid)
  1464. {
  1465. struct cifs_writedata *wdata = mid->callback_data;
  1466. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1467. unsigned int written;
  1468. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1469. unsigned int credits_received = 1;
  1470. switch (mid->mid_state) {
  1471. case MID_RESPONSE_RECEIVED:
  1472. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1473. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1474. if (wdata->result != 0)
  1475. break;
  1476. written = le32_to_cpu(rsp->DataLength);
  1477. /*
  1478. * Mask off high 16 bits when bytes written as returned
  1479. * by the server is greater than bytes requested by the
  1480. * client. OS/2 servers are known to set incorrect
  1481. * CountHigh values.
  1482. */
  1483. if (written > wdata->bytes)
  1484. written &= 0xFFFF;
  1485. if (written < wdata->bytes)
  1486. wdata->result = -ENOSPC;
  1487. else
  1488. wdata->bytes = written;
  1489. break;
  1490. case MID_REQUEST_SUBMITTED:
  1491. case MID_RETRY_NEEDED:
  1492. wdata->result = -EAGAIN;
  1493. break;
  1494. default:
  1495. wdata->result = -EIO;
  1496. break;
  1497. }
  1498. if (wdata->result)
  1499. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1500. queue_work(cifsiod_wq, &wdata->work);
  1501. DeleteMidQEntry(mid);
  1502. add_credits(tcon->ses->server, credits_received, 0);
  1503. }
  1504. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1505. int
  1506. smb2_async_writev(struct cifs_writedata *wdata)
  1507. {
  1508. int rc = -EACCES;
  1509. struct smb2_write_req *req = NULL;
  1510. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1511. struct kvec iov;
  1512. struct smb_rqst rqst;
  1513. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1514. if (rc)
  1515. goto async_writev_out;
  1516. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1517. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1518. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1519. req->WriteChannelInfoOffset = 0;
  1520. req->WriteChannelInfoLength = 0;
  1521. req->Channel = 0;
  1522. req->Offset = cpu_to_le64(wdata->offset);
  1523. /* 4 for rfc1002 length field */
  1524. req->DataOffset = cpu_to_le16(
  1525. offsetof(struct smb2_write_req, Buffer) - 4);
  1526. req->RemainingBytes = 0;
  1527. /* 4 for rfc1002 length field and 1 for Buffer */
  1528. iov.iov_len = get_rfc1002_length(req) + 4 - 1;
  1529. iov.iov_base = req;
  1530. rqst.rq_iov = &iov;
  1531. rqst.rq_nvec = 1;
  1532. rqst.rq_pages = wdata->pages;
  1533. rqst.rq_npages = wdata->nr_pages;
  1534. rqst.rq_pagesz = wdata->pagesz;
  1535. rqst.rq_tailsz = wdata->tailsz;
  1536. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  1537. wdata->offset, wdata->bytes);
  1538. req->Length = cpu_to_le32(wdata->bytes);
  1539. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1540. kref_get(&wdata->refcount);
  1541. rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
  1542. smb2_writev_callback, wdata, 0);
  1543. if (rc) {
  1544. kref_put(&wdata->refcount, cifs_writedata_release);
  1545. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1546. }
  1547. async_writev_out:
  1548. cifs_small_buf_release(req);
  1549. return rc;
  1550. }
  1551. /*
  1552. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1553. * The length field from io_parms must be at least 1 and indicates a number of
  1554. * elements with data to write that begins with position 1 in iov array. All
  1555. * data length is specified by count.
  1556. */
  1557. int
  1558. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1559. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1560. {
  1561. int rc = 0;
  1562. struct smb2_write_req *req = NULL;
  1563. struct smb2_write_rsp *rsp = NULL;
  1564. int resp_buftype;
  1565. *nbytes = 0;
  1566. if (n_vec < 1)
  1567. return rc;
  1568. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1569. if (rc)
  1570. return rc;
  1571. if (io_parms->tcon->ses->server == NULL)
  1572. return -ECONNABORTED;
  1573. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1574. req->PersistentFileId = io_parms->persistent_fid;
  1575. req->VolatileFileId = io_parms->volatile_fid;
  1576. req->WriteChannelInfoOffset = 0;
  1577. req->WriteChannelInfoLength = 0;
  1578. req->Channel = 0;
  1579. req->Length = cpu_to_le32(io_parms->length);
  1580. req->Offset = cpu_to_le64(io_parms->offset);
  1581. /* 4 for rfc1002 length field */
  1582. req->DataOffset = cpu_to_le16(
  1583. offsetof(struct smb2_write_req, Buffer) - 4);
  1584. req->RemainingBytes = 0;
  1585. iov[0].iov_base = (char *)req;
  1586. /* 4 for rfc1002 length field and 1 for Buffer */
  1587. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1588. /* length of entire message including data to be written */
  1589. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1590. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1591. &resp_buftype, 0);
  1592. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1593. if (rc) {
  1594. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1595. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  1596. } else
  1597. *nbytes = le32_to_cpu(rsp->DataLength);
  1598. free_rsp_buf(resp_buftype, rsp);
  1599. return rc;
  1600. }
  1601. static unsigned int
  1602. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  1603. {
  1604. int len;
  1605. unsigned int entrycount = 0;
  1606. unsigned int next_offset = 0;
  1607. FILE_DIRECTORY_INFO *entryptr;
  1608. if (bufstart == NULL)
  1609. return 0;
  1610. entryptr = (FILE_DIRECTORY_INFO *)bufstart;
  1611. while (1) {
  1612. entryptr = (FILE_DIRECTORY_INFO *)
  1613. ((char *)entryptr + next_offset);
  1614. if ((char *)entryptr + size > end_of_buf) {
  1615. cifs_dbg(VFS, "malformed search entry would overflow\n");
  1616. break;
  1617. }
  1618. len = le32_to_cpu(entryptr->FileNameLength);
  1619. if ((char *)entryptr + len + size > end_of_buf) {
  1620. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  1621. end_of_buf);
  1622. break;
  1623. }
  1624. *lastentry = (char *)entryptr;
  1625. entrycount++;
  1626. next_offset = le32_to_cpu(entryptr->NextEntryOffset);
  1627. if (!next_offset)
  1628. break;
  1629. }
  1630. return entrycount;
  1631. }
  1632. /*
  1633. * Readdir/FindFirst
  1634. */
  1635. int
  1636. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  1637. u64 persistent_fid, u64 volatile_fid, int index,
  1638. struct cifs_search_info *srch_inf)
  1639. {
  1640. struct smb2_query_directory_req *req;
  1641. struct smb2_query_directory_rsp *rsp = NULL;
  1642. struct kvec iov[2];
  1643. int rc = 0;
  1644. int len;
  1645. int resp_buftype;
  1646. unsigned char *bufptr;
  1647. struct TCP_Server_Info *server;
  1648. struct cifs_ses *ses = tcon->ses;
  1649. __le16 asteriks = cpu_to_le16('*');
  1650. char *end_of_smb;
  1651. unsigned int output_size = CIFSMaxBufSize;
  1652. size_t info_buf_size;
  1653. if (ses && (ses->server))
  1654. server = ses->server;
  1655. else
  1656. return -EIO;
  1657. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  1658. if (rc)
  1659. return rc;
  1660. switch (srch_inf->info_level) {
  1661. case SMB_FIND_FILE_DIRECTORY_INFO:
  1662. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  1663. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  1664. break;
  1665. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  1666. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  1667. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  1668. break;
  1669. default:
  1670. cifs_dbg(VFS, "info level %u isn't supported\n",
  1671. srch_inf->info_level);
  1672. rc = -EINVAL;
  1673. goto qdir_exit;
  1674. }
  1675. req->FileIndex = cpu_to_le32(index);
  1676. req->PersistentFileId = persistent_fid;
  1677. req->VolatileFileId = volatile_fid;
  1678. len = 0x2;
  1679. bufptr = req->Buffer;
  1680. memcpy(bufptr, &asteriks, len);
  1681. req->FileNameOffset =
  1682. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  1683. req->FileNameLength = cpu_to_le16(len);
  1684. /*
  1685. * BB could be 30 bytes or so longer if we used SMB2 specific
  1686. * buffer lengths, but this is safe and close enough.
  1687. */
  1688. output_size = min_t(unsigned int, output_size, server->maxBuf);
  1689. output_size = min_t(unsigned int, output_size, 2 << 15);
  1690. req->OutputBufferLength = cpu_to_le32(output_size);
  1691. iov[0].iov_base = (char *)req;
  1692. /* 4 for RFC1001 length and 1 for Buffer */
  1693. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1694. iov[1].iov_base = (char *)(req->Buffer);
  1695. iov[1].iov_len = len;
  1696. inc_rfc1001_len(req, len - 1 /* Buffer */);
  1697. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  1698. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  1699. if (rc) {
  1700. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  1701. goto qdir_exit;
  1702. }
  1703. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1704. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1705. info_buf_size);
  1706. if (rc)
  1707. goto qdir_exit;
  1708. srch_inf->unicode = true;
  1709. if (srch_inf->ntwrk_buf_start) {
  1710. if (srch_inf->smallBuf)
  1711. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  1712. else
  1713. cifs_buf_release(srch_inf->ntwrk_buf_start);
  1714. }
  1715. srch_inf->ntwrk_buf_start = (char *)rsp;
  1716. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  1717. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  1718. /* 4 for rfc1002 length field */
  1719. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  1720. srch_inf->entries_in_buffer =
  1721. num_entries(srch_inf->srch_entries_start, end_of_smb,
  1722. &srch_inf->last_entry, info_buf_size);
  1723. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  1724. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  1725. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  1726. srch_inf->srch_entries_start, srch_inf->last_entry);
  1727. if (resp_buftype == CIFS_LARGE_BUFFER)
  1728. srch_inf->smallBuf = false;
  1729. else if (resp_buftype == CIFS_SMALL_BUFFER)
  1730. srch_inf->smallBuf = true;
  1731. else
  1732. cifs_dbg(VFS, "illegal search buffer type\n");
  1733. if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
  1734. srch_inf->endOfSearch = 1;
  1735. else
  1736. srch_inf->endOfSearch = 0;
  1737. return rc;
  1738. qdir_exit:
  1739. free_rsp_buf(resp_buftype, rsp);
  1740. return rc;
  1741. }
  1742. static int
  1743. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1744. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  1745. unsigned int num, void **data, unsigned int *size)
  1746. {
  1747. struct smb2_set_info_req *req;
  1748. struct smb2_set_info_rsp *rsp = NULL;
  1749. struct kvec *iov;
  1750. int rc = 0;
  1751. int resp_buftype;
  1752. unsigned int i;
  1753. struct TCP_Server_Info *server;
  1754. struct cifs_ses *ses = tcon->ses;
  1755. if (ses && (ses->server))
  1756. server = ses->server;
  1757. else
  1758. return -EIO;
  1759. if (!num)
  1760. return -EINVAL;
  1761. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  1762. if (!iov)
  1763. return -ENOMEM;
  1764. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  1765. if (rc) {
  1766. kfree(iov);
  1767. return rc;
  1768. }
  1769. req->hdr.ProcessId = cpu_to_le32(pid);
  1770. req->InfoType = SMB2_O_INFO_FILE;
  1771. req->FileInfoClass = info_class;
  1772. req->PersistentFileId = persistent_fid;
  1773. req->VolatileFileId = volatile_fid;
  1774. /* 4 for RFC1001 length and 1 for Buffer */
  1775. req->BufferOffset =
  1776. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  1777. req->BufferLength = cpu_to_le32(*size);
  1778. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  1779. memcpy(req->Buffer, *data, *size);
  1780. iov[0].iov_base = (char *)req;
  1781. /* 4 for RFC1001 length */
  1782. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1783. for (i = 1; i < num; i++) {
  1784. inc_rfc1001_len(req, size[i]);
  1785. le32_add_cpu(&req->BufferLength, size[i]);
  1786. iov[i].iov_base = (char *)data[i];
  1787. iov[i].iov_len = size[i];
  1788. }
  1789. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  1790. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  1791. if (rc != 0) {
  1792. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  1793. goto out;
  1794. }
  1795. out:
  1796. free_rsp_buf(resp_buftype, rsp);
  1797. kfree(iov);
  1798. return rc;
  1799. }
  1800. int
  1801. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  1802. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1803. {
  1804. struct smb2_file_rename_info info;
  1805. void **data;
  1806. unsigned int size[2];
  1807. int rc;
  1808. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1809. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1810. if (!data)
  1811. return -ENOMEM;
  1812. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  1813. /* 0 = fail if target already exists */
  1814. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1815. info.FileNameLength = cpu_to_le32(len);
  1816. data[0] = &info;
  1817. size[0] = sizeof(struct smb2_file_rename_info);
  1818. data[1] = target_file;
  1819. size[1] = len + 2 /* null */;
  1820. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1821. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  1822. size);
  1823. kfree(data);
  1824. return rc;
  1825. }
  1826. int
  1827. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  1828. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1829. {
  1830. struct smb2_file_link_info info;
  1831. void **data;
  1832. unsigned int size[2];
  1833. int rc;
  1834. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1835. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1836. if (!data)
  1837. return -ENOMEM;
  1838. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  1839. /* 0 = fail if link already exists */
  1840. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1841. info.FileNameLength = cpu_to_le32(len);
  1842. data[0] = &info;
  1843. size[0] = sizeof(struct smb2_file_link_info);
  1844. data[1] = target_file;
  1845. size[1] = len + 2 /* null */;
  1846. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1847. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  1848. kfree(data);
  1849. return rc;
  1850. }
  1851. int
  1852. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1853. u64 volatile_fid, u32 pid, __le64 *eof)
  1854. {
  1855. struct smb2_file_eof_info info;
  1856. void *data;
  1857. unsigned int size;
  1858. info.EndOfFile = *eof;
  1859. data = &info;
  1860. size = sizeof(struct smb2_file_eof_info);
  1861. return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
  1862. FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  1863. }
  1864. int
  1865. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1866. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  1867. {
  1868. unsigned int size;
  1869. size = sizeof(FILE_BASIC_INFO);
  1870. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1871. current->tgid, FILE_BASIC_INFORMATION, 1,
  1872. (void **)&buf, &size);
  1873. }
  1874. int
  1875. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  1876. const u64 persistent_fid, const u64 volatile_fid,
  1877. __u8 oplock_level)
  1878. {
  1879. int rc;
  1880. struct smb2_oplock_break *req = NULL;
  1881. cifs_dbg(FYI, "SMB2_oplock_break\n");
  1882. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1883. if (rc)
  1884. return rc;
  1885. req->VolatileFid = volatile_fid;
  1886. req->PersistentFid = persistent_fid;
  1887. req->OplockLevel = oplock_level;
  1888. req->hdr.CreditRequest = cpu_to_le16(1);
  1889. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1890. /* SMB2 buffer freed by function above */
  1891. if (rc) {
  1892. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1893. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  1894. }
  1895. return rc;
  1896. }
  1897. static void
  1898. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  1899. struct kstatfs *kst)
  1900. {
  1901. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  1902. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  1903. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  1904. kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
  1905. kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  1906. return;
  1907. }
  1908. static int
  1909. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  1910. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  1911. {
  1912. int rc;
  1913. struct smb2_query_info_req *req;
  1914. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  1915. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  1916. return -EIO;
  1917. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1918. if (rc)
  1919. return rc;
  1920. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  1921. req->FileInfoClass = level;
  1922. req->PersistentFileId = persistent_fid;
  1923. req->VolatileFileId = volatile_fid;
  1924. /* 4 for rfc1002 length field and 1 for pad */
  1925. req->InputBufferOffset =
  1926. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1927. req->OutputBufferLength = cpu_to_le32(
  1928. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  1929. iov->iov_base = (char *)req;
  1930. /* 4 for rfc1002 length field */
  1931. iov->iov_len = get_rfc1002_length(req) + 4;
  1932. return 0;
  1933. }
  1934. int
  1935. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  1936. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  1937. {
  1938. struct smb2_query_info_rsp *rsp = NULL;
  1939. struct kvec iov;
  1940. int rc = 0;
  1941. int resp_buftype;
  1942. struct cifs_ses *ses = tcon->ses;
  1943. struct smb2_fs_full_size_info *info = NULL;
  1944. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  1945. sizeof(struct smb2_fs_full_size_info),
  1946. persistent_fid, volatile_fid);
  1947. if (rc)
  1948. return rc;
  1949. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  1950. if (rc) {
  1951. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1952. goto qinf_exit;
  1953. }
  1954. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  1955. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  1956. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  1957. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1958. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1959. sizeof(struct smb2_fs_full_size_info));
  1960. if (!rc)
  1961. copy_fs_info_to_kstatfs(info, fsdata);
  1962. qinf_exit:
  1963. free_rsp_buf(resp_buftype, iov.iov_base);
  1964. return rc;
  1965. }
  1966. int
  1967. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  1968. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1969. const __u32 num_lock, struct smb2_lock_element *buf)
  1970. {
  1971. int rc = 0;
  1972. struct smb2_lock_req *req = NULL;
  1973. struct kvec iov[2];
  1974. int resp_buf_type;
  1975. unsigned int count;
  1976. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  1977. rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
  1978. if (rc)
  1979. return rc;
  1980. req->hdr.ProcessId = cpu_to_le32(pid);
  1981. req->LockCount = cpu_to_le16(num_lock);
  1982. req->PersistentFileId = persist_fid;
  1983. req->VolatileFileId = volatile_fid;
  1984. count = num_lock * sizeof(struct smb2_lock_element);
  1985. inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
  1986. iov[0].iov_base = (char *)req;
  1987. /* 4 for rfc1002 length field and count for all locks */
  1988. iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
  1989. iov[1].iov_base = (char *)buf;
  1990. iov[1].iov_len = count;
  1991. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  1992. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  1993. if (rc) {
  1994. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  1995. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  1996. }
  1997. return rc;
  1998. }
  1999. int
  2000. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  2001. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  2002. const __u64 length, const __u64 offset, const __u32 lock_flags,
  2003. const bool wait)
  2004. {
  2005. struct smb2_lock_element lock;
  2006. lock.Offset = cpu_to_le64(offset);
  2007. lock.Length = cpu_to_le64(length);
  2008. lock.Flags = cpu_to_le32(lock_flags);
  2009. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  2010. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  2011. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  2012. }
  2013. int
  2014. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  2015. __u8 *lease_key, const __le32 lease_state)
  2016. {
  2017. int rc;
  2018. struct smb2_lease_ack *req = NULL;
  2019. cifs_dbg(FYI, "SMB2_lease_break\n");
  2020. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  2021. if (rc)
  2022. return rc;
  2023. req->hdr.CreditRequest = cpu_to_le16(1);
  2024. req->StructureSize = cpu_to_le16(36);
  2025. inc_rfc1001_len(req, 12);
  2026. memcpy(req->LeaseKey, lease_key, 16);
  2027. req->LeaseState = lease_state;
  2028. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  2029. /* SMB2 buffer freed by function above */
  2030. if (rc) {
  2031. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  2032. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  2033. }
  2034. return rc;
  2035. }