smb2pdu.c 59 KB

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