smb2pdu.c 60 KB

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