smb2pdu.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  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. /*
  47. * The following table defines the expected "StructureSize" of SMB2 requests
  48. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
  49. *
  50. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  51. * indexed by command in host byte order.
  52. */
  53. static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  54. /* SMB2_NEGOTIATE */ 36,
  55. /* SMB2_SESSION_SETUP */ 25,
  56. /* SMB2_LOGOFF */ 4,
  57. /* SMB2_TREE_CONNECT */ 9,
  58. /* SMB2_TREE_DISCONNECT */ 4,
  59. /* SMB2_CREATE */ 57,
  60. /* SMB2_CLOSE */ 24,
  61. /* SMB2_FLUSH */ 24,
  62. /* SMB2_READ */ 49,
  63. /* SMB2_WRITE */ 49,
  64. /* SMB2_LOCK */ 48,
  65. /* SMB2_IOCTL */ 57,
  66. /* SMB2_CANCEL */ 4,
  67. /* SMB2_ECHO */ 4,
  68. /* SMB2_QUERY_DIRECTORY */ 33,
  69. /* SMB2_CHANGE_NOTIFY */ 32,
  70. /* SMB2_QUERY_INFO */ 41,
  71. /* SMB2_SET_INFO */ 33,
  72. /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
  73. };
  74. static void
  75. smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
  76. const struct cifs_tcon *tcon)
  77. {
  78. struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
  79. char *temp = (char *)hdr;
  80. /* lookup word count ie StructureSize from table */
  81. __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
  82. /*
  83. * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
  84. * largest operations (Create)
  85. */
  86. memset(temp, 0, 256);
  87. /* Note this is only network field converted to big endian */
  88. hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
  89. - 4 /* RFC 1001 length field itself not counted */);
  90. hdr->ProtocolId[0] = 0xFE;
  91. hdr->ProtocolId[1] = 'S';
  92. hdr->ProtocolId[2] = 'M';
  93. hdr->ProtocolId[3] = 'B';
  94. hdr->StructureSize = cpu_to_le16(64);
  95. hdr->Command = smb2_cmd;
  96. hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
  97. hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
  98. if (!tcon)
  99. goto out;
  100. hdr->TreeId = tcon->tid;
  101. /* Uid is not converted */
  102. if (tcon->ses)
  103. hdr->SessionId = tcon->ses->Suid;
  104. /* BB check following DFS flags BB */
  105. /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */
  106. if (tcon->share_flags & SHI1005_FLAGS_DFS)
  107. hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS;
  108. /* BB how does SMB2 do case sensitive? */
  109. /* if (tcon->nocase)
  110. hdr->Flags |= SMBFLG_CASELESS; */
  111. if (tcon->ses && tcon->ses->server &&
  112. (tcon->ses->server->sec_mode & SECMODE_SIGN_REQUIRED))
  113. hdr->Flags |= SMB2_FLAGS_SIGNED;
  114. out:
  115. pdu->StructureSize2 = cpu_to_le16(parmsize);
  116. return;
  117. }
  118. static int
  119. smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
  120. {
  121. int rc = 0;
  122. struct nls_table *nls_codepage;
  123. struct cifs_ses *ses;
  124. struct TCP_Server_Info *server;
  125. /*
  126. * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
  127. * check for tcp and smb session status done differently
  128. * for those three - in the calling routine.
  129. */
  130. if (tcon == NULL)
  131. return rc;
  132. if (smb2_command == SMB2_TREE_CONNECT)
  133. return rc;
  134. if (tcon->tidStatus == CifsExiting) {
  135. /*
  136. * only tree disconnect, open, and write,
  137. * (and ulogoff which does not have tcon)
  138. * are allowed as we start force umount.
  139. */
  140. if ((smb2_command != SMB2_WRITE) &&
  141. (smb2_command != SMB2_CREATE) &&
  142. (smb2_command != SMB2_TREE_DISCONNECT)) {
  143. cFYI(1, "can not send cmd %d while umounting",
  144. smb2_command);
  145. return -ENODEV;
  146. }
  147. }
  148. if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
  149. (!tcon->ses->server))
  150. return -EIO;
  151. ses = tcon->ses;
  152. server = ses->server;
  153. /*
  154. * Give demultiplex thread up to 10 seconds to reconnect, should be
  155. * greater than cifs socket timeout which is 7 seconds
  156. */
  157. while (server->tcpStatus == CifsNeedReconnect) {
  158. /*
  159. * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
  160. * here since they are implicitly done when session drops.
  161. */
  162. switch (smb2_command) {
  163. /*
  164. * BB Should we keep oplock break and add flush to exceptions?
  165. */
  166. case SMB2_TREE_DISCONNECT:
  167. case SMB2_CANCEL:
  168. case SMB2_CLOSE:
  169. case SMB2_OPLOCK_BREAK:
  170. return -EAGAIN;
  171. }
  172. wait_event_interruptible_timeout(server->response_q,
  173. (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
  174. /* are we still trying to reconnect? */
  175. if (server->tcpStatus != CifsNeedReconnect)
  176. break;
  177. /*
  178. * on "soft" mounts we wait once. Hard mounts keep
  179. * retrying until process is killed or server comes
  180. * back on-line
  181. */
  182. if (!tcon->retry) {
  183. cFYI(1, "gave up waiting on reconnect in smb_init");
  184. return -EHOSTDOWN;
  185. }
  186. }
  187. if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
  188. return rc;
  189. nls_codepage = load_nls_default();
  190. /*
  191. * need to prevent multiple threads trying to simultaneously reconnect
  192. * the same SMB session
  193. */
  194. mutex_lock(&tcon->ses->session_mutex);
  195. rc = cifs_negotiate_protocol(0, tcon->ses);
  196. if (!rc && tcon->ses->need_reconnect)
  197. rc = cifs_setup_session(0, tcon->ses, nls_codepage);
  198. if (rc || !tcon->need_reconnect) {
  199. mutex_unlock(&tcon->ses->session_mutex);
  200. goto out;
  201. }
  202. cifs_mark_open_files_invalid(tcon);
  203. rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
  204. mutex_unlock(&tcon->ses->session_mutex);
  205. cFYI(1, "reconnect tcon rc = %d", rc);
  206. if (rc)
  207. goto out;
  208. atomic_inc(&tconInfoReconnectCount);
  209. /*
  210. * BB FIXME add code to check if wsize needs update due to negotiated
  211. * smb buffer size shrinking.
  212. */
  213. out:
  214. /*
  215. * Check if handle based operation so we know whether we can continue
  216. * or not without returning to caller to reset file handle.
  217. */
  218. /*
  219. * BB Is flush done by server on drop of tcp session? Should we special
  220. * case it and skip above?
  221. */
  222. switch (smb2_command) {
  223. case SMB2_FLUSH:
  224. case SMB2_READ:
  225. case SMB2_WRITE:
  226. case SMB2_LOCK:
  227. case SMB2_IOCTL:
  228. case SMB2_QUERY_DIRECTORY:
  229. case SMB2_CHANGE_NOTIFY:
  230. case SMB2_QUERY_INFO:
  231. case SMB2_SET_INFO:
  232. return -EAGAIN;
  233. }
  234. unload_nls(nls_codepage);
  235. return rc;
  236. }
  237. /*
  238. * Allocate and return pointer to an SMB request hdr, and set basic
  239. * SMB information in the SMB header. If the return code is zero, this
  240. * function must have filled in request_buf pointer.
  241. */
  242. static int
  243. small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
  244. void **request_buf)
  245. {
  246. int rc = 0;
  247. rc = smb2_reconnect(smb2_command, tcon);
  248. if (rc)
  249. return rc;
  250. /* BB eventually switch this to SMB2 specific small buf size */
  251. *request_buf = cifs_small_buf_get();
  252. if (*request_buf == NULL) {
  253. /* BB should we add a retry in here if not a writepage? */
  254. return -ENOMEM;
  255. }
  256. smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
  257. if (tcon != NULL) {
  258. #ifdef CONFIG_CIFS_STATS2
  259. uint16_t com_code = le16_to_cpu(smb2_command);
  260. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
  261. #endif
  262. cifs_stats_inc(&tcon->num_smbs_sent);
  263. }
  264. return rc;
  265. }
  266. static void
  267. free_rsp_buf(int resp_buftype, void *rsp)
  268. {
  269. if (resp_buftype == CIFS_SMALL_BUFFER)
  270. cifs_small_buf_release(rsp);
  271. else if (resp_buftype == CIFS_LARGE_BUFFER)
  272. cifs_buf_release(rsp);
  273. }
  274. #define SMB2_NUM_PROT 1
  275. #define SMB2_PROT 0
  276. #define SMB21_PROT 1
  277. #define BAD_PROT 0xFFFF
  278. #define SMB2_PROT_ID 0x0202
  279. #define SMB21_PROT_ID 0x0210
  280. #define BAD_PROT_ID 0xFFFF
  281. static struct {
  282. int index;
  283. __le16 name;
  284. } smb2protocols[] = {
  285. {SMB2_PROT, cpu_to_le16(SMB2_PROT_ID)},
  286. {SMB21_PROT, cpu_to_le16(SMB21_PROT_ID)},
  287. {BAD_PROT, cpu_to_le16(BAD_PROT_ID)}
  288. };
  289. /*
  290. *
  291. * SMB2 Worker functions follow:
  292. *
  293. * The general structure of the worker functions is:
  294. * 1) Call smb2_init (assembles SMB2 header)
  295. * 2) Initialize SMB2 command specific fields in fixed length area of SMB
  296. * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
  297. * 4) Decode SMB2 command specific fields in the fixed length area
  298. * 5) Decode variable length data area (if any for this SMB2 command type)
  299. * 6) Call free smb buffer
  300. * 7) return
  301. *
  302. */
  303. int
  304. SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  305. {
  306. struct smb2_negotiate_req *req;
  307. struct smb2_negotiate_rsp *rsp;
  308. struct kvec iov[1];
  309. int rc = 0;
  310. int resp_buftype;
  311. struct TCP_Server_Info *server;
  312. unsigned int sec_flags;
  313. u16 i;
  314. u16 temp = 0;
  315. int blob_offset, blob_length;
  316. char *security_blob;
  317. int flags = CIFS_NEG_OP;
  318. cFYI(1, "Negotiate protocol");
  319. if (ses->server)
  320. server = ses->server;
  321. else {
  322. rc = -EIO;
  323. return rc;
  324. }
  325. rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
  326. if (rc)
  327. return rc;
  328. /* if any of auth flags (ie not sign or seal) are overriden use them */
  329. if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  330. sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
  331. else /* if override flags set only sign/seal OR them with global auth */
  332. sec_flags = global_secflags | ses->overrideSecFlg;
  333. cFYI(1, "sec_flags 0x%x", sec_flags);
  334. req->hdr.SessionId = 0;
  335. for (i = 0; i < SMB2_NUM_PROT; i++)
  336. req->Dialects[i] = smb2protocols[i].name;
  337. req->DialectCount = cpu_to_le16(i);
  338. inc_rfc1001_len(req, i * 2);
  339. /* only one of SMB2 signing flags may be set in SMB2 request */
  340. if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
  341. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  342. else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
  343. temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
  344. req->SecurityMode = cpu_to_le16(temp);
  345. req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
  346. iov[0].iov_base = (char *)req;
  347. /* 4 for rfc1002 length field */
  348. iov[0].iov_len = get_rfc1002_length(req) + 4;
  349. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
  350. rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
  351. /*
  352. * No tcon so can't do
  353. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  354. */
  355. if (rc != 0)
  356. goto neg_exit;
  357. if (rsp == NULL) {
  358. rc = -EIO;
  359. goto neg_exit;
  360. }
  361. cFYI(1, "mode 0x%x", rsp->SecurityMode);
  362. if (rsp->DialectRevision == smb2protocols[SMB21_PROT].name)
  363. cFYI(1, "negotiated smb2.1 dialect");
  364. else if (rsp->DialectRevision == smb2protocols[SMB2_PROT].name)
  365. cFYI(1, "negotiated smb2 dialect");
  366. else {
  367. cERROR(1, "Illegal dialect returned by server %d",
  368. le16_to_cpu(rsp->DialectRevision));
  369. rc = -EIO;
  370. goto neg_exit;
  371. }
  372. server->dialect = le16_to_cpu(rsp->DialectRevision);
  373. server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
  374. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  375. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  376. /* BB Do we need to validate the SecurityMode? */
  377. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  378. server->capabilities = le32_to_cpu(rsp->Capabilities);
  379. /* Internal types */
  380. server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
  381. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  382. &rsp->hdr);
  383. if (blob_length == 0) {
  384. cERROR(1, "missing security blob on negprot");
  385. rc = -EIO;
  386. goto neg_exit;
  387. }
  388. cFYI(1, "sec_flags 0x%x", sec_flags);
  389. if (sec_flags & CIFSSEC_MUST_SIGN) {
  390. cFYI(1, "Signing required");
  391. if (!(server->sec_mode & (SMB2_NEGOTIATE_SIGNING_REQUIRED |
  392. SMB2_NEGOTIATE_SIGNING_ENABLED))) {
  393. cERROR(1, "signing required but server lacks support");
  394. rc = -EOPNOTSUPP;
  395. goto neg_exit;
  396. }
  397. server->sec_mode |= SECMODE_SIGN_REQUIRED;
  398. } else if (sec_flags & CIFSSEC_MAY_SIGN) {
  399. cFYI(1, "Signing optional");
  400. if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
  401. cFYI(1, "Server requires signing");
  402. server->sec_mode |= SECMODE_SIGN_REQUIRED;
  403. } else {
  404. server->sec_mode &=
  405. ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
  406. }
  407. } else {
  408. cFYI(1, "Signing disabled");
  409. if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
  410. cERROR(1, "Server requires packet signing to be enabled"
  411. " in /proc/fs/cifs/SecurityFlags.");
  412. rc = -EOPNOTSUPP;
  413. goto neg_exit;
  414. }
  415. server->sec_mode &=
  416. ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
  417. }
  418. #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
  419. rc = decode_neg_token_init(security_blob, blob_length,
  420. &server->sec_type);
  421. if (rc == 1)
  422. rc = 0;
  423. else if (rc == 0) {
  424. rc = -EIO;
  425. goto neg_exit;
  426. }
  427. #endif
  428. neg_exit:
  429. free_rsp_buf(resp_buftype, rsp);
  430. return rc;
  431. }
  432. int
  433. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  434. const struct nls_table *nls_cp)
  435. {
  436. struct smb2_sess_setup_req *req;
  437. struct smb2_sess_setup_rsp *rsp = NULL;
  438. struct kvec iov[2];
  439. int rc = 0;
  440. int resp_buftype;
  441. __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
  442. struct TCP_Server_Info *server;
  443. unsigned int sec_flags;
  444. u8 temp = 0;
  445. u16 blob_length = 0;
  446. char *security_blob;
  447. char *ntlmssp_blob = NULL;
  448. bool use_spnego = false; /* else use raw ntlmssp */
  449. cFYI(1, "Session Setup");
  450. if (ses->server)
  451. server = ses->server;
  452. else {
  453. rc = -EIO;
  454. return rc;
  455. }
  456. /*
  457. * If memory allocation is successful, caller of this function
  458. * frees it.
  459. */
  460. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  461. if (!ses->ntlmssp)
  462. return -ENOMEM;
  463. ses->server->secType = RawNTLMSSP;
  464. ssetup_ntlmssp_authenticate:
  465. if (phase == NtLmChallenge)
  466. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  467. rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
  468. if (rc)
  469. return rc;
  470. /* if any of auth flags (ie not sign or seal) are overriden use them */
  471. if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  472. sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
  473. else /* if override flags set only sign/seal OR them with global auth */
  474. sec_flags = global_secflags | ses->overrideSecFlg;
  475. cFYI(1, "sec_flags 0x%x", sec_flags);
  476. req->hdr.SessionId = 0; /* First session, not a reauthenticate */
  477. req->VcNumber = 0; /* MBZ */
  478. /* to enable echos and oplocks */
  479. req->hdr.CreditRequest = cpu_to_le16(3);
  480. /* only one of SMB2 signing flags may be set in SMB2 request */
  481. if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
  482. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  483. else if (ses->server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED)
  484. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  485. else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
  486. temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
  487. req->SecurityMode = temp;
  488. req->Capabilities = 0;
  489. req->Channel = 0; /* MBZ */
  490. iov[0].iov_base = (char *)req;
  491. /* 4 for rfc1002 length field and 1 for pad */
  492. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  493. if (phase == NtLmNegotiate) {
  494. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  495. GFP_KERNEL);
  496. if (ntlmssp_blob == NULL) {
  497. rc = -ENOMEM;
  498. goto ssetup_exit;
  499. }
  500. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  501. if (use_spnego) {
  502. /* blob_length = build_spnego_ntlmssp_blob(
  503. &security_blob,
  504. sizeof(struct _NEGOTIATE_MESSAGE),
  505. ntlmssp_blob); */
  506. /* BB eventually need to add this */
  507. cERROR(1, "spnego not supported for SMB2 yet");
  508. rc = -EOPNOTSUPP;
  509. kfree(ntlmssp_blob);
  510. goto ssetup_exit;
  511. } else {
  512. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  513. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  514. security_blob = ntlmssp_blob;
  515. }
  516. } else if (phase == NtLmAuthenticate) {
  517. req->hdr.SessionId = ses->Suid;
  518. ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
  519. GFP_KERNEL);
  520. if (ntlmssp_blob == NULL) {
  521. cERROR(1, "failed to malloc ntlmssp blob");
  522. rc = -ENOMEM;
  523. goto ssetup_exit;
  524. }
  525. rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
  526. nls_cp);
  527. if (rc) {
  528. cFYI(1, "build_ntlmssp_auth_blob failed %d", rc);
  529. goto ssetup_exit; /* BB double check error handling */
  530. }
  531. if (use_spnego) {
  532. /* blob_length = build_spnego_ntlmssp_blob(
  533. &security_blob,
  534. blob_length,
  535. ntlmssp_blob); */
  536. cERROR(1, "spnego not supported for SMB2 yet");
  537. rc = -EOPNOTSUPP;
  538. kfree(ntlmssp_blob);
  539. goto ssetup_exit;
  540. } else {
  541. security_blob = ntlmssp_blob;
  542. }
  543. } else {
  544. cERROR(1, "illegal ntlmssp phase");
  545. rc = -EIO;
  546. goto ssetup_exit;
  547. }
  548. /* Testing shows that buffer offset must be at location of Buffer[0] */
  549. req->SecurityBufferOffset =
  550. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  551. 1 /* pad */ - 4 /* rfc1001 len */);
  552. req->SecurityBufferLength = cpu_to_le16(blob_length);
  553. iov[1].iov_base = security_blob;
  554. iov[1].iov_len = blob_length;
  555. inc_rfc1001_len(req, blob_length - 1 /* pad */);
  556. /* BB add code to build os and lm fields */
  557. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, CIFS_LOG_ERROR);
  558. kfree(security_blob);
  559. rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
  560. if (rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
  561. if (phase != NtLmNegotiate) {
  562. cERROR(1, "Unexpected more processing error");
  563. goto ssetup_exit;
  564. }
  565. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  566. le16_to_cpu(rsp->SecurityBufferOffset)) {
  567. cERROR(1, "Invalid security buffer offset %d",
  568. le16_to_cpu(rsp->SecurityBufferOffset));
  569. rc = -EIO;
  570. goto ssetup_exit;
  571. }
  572. /* NTLMSSP Negotiate sent now processing challenge (response) */
  573. phase = NtLmChallenge; /* process ntlmssp challenge */
  574. rc = 0; /* MORE_PROCESSING is not an error here but expected */
  575. ses->Suid = rsp->hdr.SessionId;
  576. rc = decode_ntlmssp_challenge(rsp->Buffer,
  577. le16_to_cpu(rsp->SecurityBufferLength), ses);
  578. }
  579. /*
  580. * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
  581. * but at least the raw NTLMSSP case works.
  582. */
  583. /*
  584. * No tcon so can't do
  585. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  586. */
  587. if (rc != 0)
  588. goto ssetup_exit;
  589. if (rsp == NULL) {
  590. rc = -EIO;
  591. goto ssetup_exit;
  592. }
  593. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  594. ssetup_exit:
  595. free_rsp_buf(resp_buftype, rsp);
  596. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  597. if ((phase == NtLmChallenge) && (rc == 0))
  598. goto ssetup_ntlmssp_authenticate;
  599. return rc;
  600. }
  601. int
  602. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  603. {
  604. struct smb2_logoff_req *req; /* response is also trivial struct */
  605. int rc = 0;
  606. struct TCP_Server_Info *server;
  607. cFYI(1, "disconnect session %p", ses);
  608. if (ses && (ses->server))
  609. server = ses->server;
  610. else
  611. return -EIO;
  612. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  613. if (rc)
  614. return rc;
  615. /* since no tcon, smb2_init can not do this, so do here */
  616. req->hdr.SessionId = ses->Suid;
  617. if (server->sec_mode & SECMODE_SIGN_REQUIRED)
  618. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  619. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  620. /*
  621. * No tcon so can't do
  622. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  623. */
  624. return rc;
  625. }
  626. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  627. {
  628. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  629. }
  630. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  631. int
  632. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  633. struct cifs_tcon *tcon, const struct nls_table *cp)
  634. {
  635. struct smb2_tree_connect_req *req;
  636. struct smb2_tree_connect_rsp *rsp = NULL;
  637. struct kvec iov[2];
  638. int rc = 0;
  639. int resp_buftype;
  640. int unc_path_len;
  641. struct TCP_Server_Info *server;
  642. __le16 *unc_path = NULL;
  643. cFYI(1, "TCON");
  644. if ((ses->server) && tree)
  645. server = ses->server;
  646. else
  647. return -EIO;
  648. if (tcon && tcon->bad_network_name)
  649. return -ENOENT;
  650. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  651. if (unc_path == NULL)
  652. return -ENOMEM;
  653. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  654. unc_path_len *= 2;
  655. if (unc_path_len < 2) {
  656. kfree(unc_path);
  657. return -EINVAL;
  658. }
  659. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  660. if (rc) {
  661. kfree(unc_path);
  662. return rc;
  663. }
  664. if (tcon == NULL) {
  665. /* since no tcon, smb2_init can not do this, so do here */
  666. req->hdr.SessionId = ses->Suid;
  667. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  668. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  669. }
  670. iov[0].iov_base = (char *)req;
  671. /* 4 for rfc1002 length field and 1 for pad */
  672. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  673. /* Testing shows that buffer offset must be at location of Buffer[0] */
  674. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  675. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  676. req->PathLength = cpu_to_le16(unc_path_len - 2);
  677. iov[1].iov_base = unc_path;
  678. iov[1].iov_len = unc_path_len;
  679. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  680. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  681. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  682. if (rc != 0) {
  683. if (tcon) {
  684. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  685. tcon->need_reconnect = true;
  686. }
  687. goto tcon_error_exit;
  688. }
  689. if (rsp == NULL) {
  690. rc = -EIO;
  691. goto tcon_exit;
  692. }
  693. if (tcon == NULL) {
  694. ses->ipc_tid = rsp->hdr.TreeId;
  695. goto tcon_exit;
  696. }
  697. if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
  698. cFYI(1, "connection to disk share");
  699. else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
  700. tcon->ipc = true;
  701. cFYI(1, "connection to pipe share");
  702. } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
  703. tcon->print = true;
  704. cFYI(1, "connection to printer");
  705. } else {
  706. cERROR(1, "unknown share type %d", rsp->ShareType);
  707. rc = -EOPNOTSUPP;
  708. goto tcon_error_exit;
  709. }
  710. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  711. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  712. tcon->tidStatus = CifsGood;
  713. tcon->need_reconnect = false;
  714. tcon->tid = rsp->hdr.TreeId;
  715. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  716. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  717. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  718. cERROR(1, "DFS capability contradicts DFS flag");
  719. tcon_exit:
  720. free_rsp_buf(resp_buftype, rsp);
  721. kfree(unc_path);
  722. return rc;
  723. tcon_error_exit:
  724. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  725. cERROR(1, "BAD_NETWORK_NAME: %s", tree);
  726. tcon->bad_network_name = true;
  727. }
  728. goto tcon_exit;
  729. }
  730. int
  731. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  732. {
  733. struct smb2_tree_disconnect_req *req; /* response is trivial */
  734. int rc = 0;
  735. struct TCP_Server_Info *server;
  736. struct cifs_ses *ses = tcon->ses;
  737. cFYI(1, "Tree Disconnect");
  738. if (ses && (ses->server))
  739. server = ses->server;
  740. else
  741. return -EIO;
  742. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  743. return 0;
  744. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  745. if (rc)
  746. return rc;
  747. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  748. if (rc)
  749. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  750. return rc;
  751. }
  752. int
  753. SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
  754. u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access,
  755. __u32 create_disposition, __u32 file_attributes, __u32 create_options,
  756. struct smb2_file_all_info *buf)
  757. {
  758. struct smb2_create_req *req;
  759. struct smb2_create_rsp *rsp;
  760. struct TCP_Server_Info *server;
  761. struct cifs_ses *ses = tcon->ses;
  762. struct kvec iov[2];
  763. int resp_buftype;
  764. int uni_path_len;
  765. int rc = 0;
  766. int num_iovecs = 2;
  767. cFYI(1, "create/open");
  768. if (ses && (ses->server))
  769. server = ses->server;
  770. else
  771. return -EIO;
  772. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  773. if (rc)
  774. return rc;
  775. /* if (server->oplocks)
  776. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_BATCH;
  777. else */
  778. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
  779. req->ImpersonationLevel = IL_IMPERSONATION;
  780. req->DesiredAccess = cpu_to_le32(desired_access);
  781. /* File attributes ignored on open (used in create though) */
  782. req->FileAttributes = cpu_to_le32(file_attributes);
  783. req->ShareAccess = FILE_SHARE_ALL_LE;
  784. req->CreateDisposition = cpu_to_le32(create_disposition);
  785. req->CreateOptions = cpu_to_le32(create_options);
  786. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  787. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)
  788. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  789. iov[0].iov_base = (char *)req;
  790. /* 4 for rfc1002 length field */
  791. iov[0].iov_len = get_rfc1002_length(req) + 4;
  792. /* MUST set path len (NameLength) to 0 opening root of share */
  793. if (uni_path_len >= 4) {
  794. req->NameLength = cpu_to_le16(uni_path_len - 2);
  795. /* -1 since last byte is buf[0] which is sent below (path) */
  796. iov[0].iov_len--;
  797. iov[1].iov_len = uni_path_len;
  798. iov[1].iov_base = path;
  799. /*
  800. * -1 since last byte is buf[0] which was counted in
  801. * smb2_buf_len.
  802. */
  803. inc_rfc1001_len(req, uni_path_len - 1);
  804. } else {
  805. num_iovecs = 1;
  806. req->NameLength = 0;
  807. }
  808. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  809. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  810. if (rc != 0) {
  811. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  812. goto creat_exit;
  813. }
  814. if (rsp == NULL) {
  815. rc = -EIO;
  816. goto creat_exit;
  817. }
  818. *persistent_fid = rsp->PersistentFileId;
  819. *volatile_fid = rsp->VolatileFileId;
  820. if (buf) {
  821. memcpy(buf, &rsp->CreationTime, 32);
  822. buf->AllocationSize = rsp->AllocationSize;
  823. buf->EndOfFile = rsp->EndofFile;
  824. buf->Attributes = rsp->FileAttributes;
  825. buf->NumberOfLinks = cpu_to_le32(1);
  826. buf->DeletePending = 0;
  827. }
  828. creat_exit:
  829. free_rsp_buf(resp_buftype, rsp);
  830. return rc;
  831. }
  832. int
  833. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  834. u64 persistent_fid, u64 volatile_fid)
  835. {
  836. struct smb2_close_req *req;
  837. struct smb2_close_rsp *rsp;
  838. struct TCP_Server_Info *server;
  839. struct cifs_ses *ses = tcon->ses;
  840. struct kvec iov[1];
  841. int resp_buftype;
  842. int rc = 0;
  843. cFYI(1, "Close");
  844. if (ses && (ses->server))
  845. server = ses->server;
  846. else
  847. return -EIO;
  848. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  849. if (rc)
  850. return rc;
  851. req->PersistentFileId = persistent_fid;
  852. req->VolatileFileId = volatile_fid;
  853. iov[0].iov_base = (char *)req;
  854. /* 4 for rfc1002 length field */
  855. iov[0].iov_len = get_rfc1002_length(req) + 4;
  856. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  857. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  858. if (rc != 0) {
  859. if (tcon)
  860. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  861. goto close_exit;
  862. }
  863. if (rsp == NULL) {
  864. rc = -EIO;
  865. goto close_exit;
  866. }
  867. /* BB FIXME - decode close response, update inode for caching */
  868. close_exit:
  869. free_rsp_buf(resp_buftype, rsp);
  870. return rc;
  871. }
  872. static int
  873. validate_buf(unsigned int offset, unsigned int buffer_length,
  874. struct smb2_hdr *hdr, unsigned int min_buf_size)
  875. {
  876. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  877. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  878. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  879. char *end_of_buf = begin_of_buf + buffer_length;
  880. if (buffer_length < min_buf_size) {
  881. cERROR(1, "buffer length %d smaller than minimum size %d",
  882. buffer_length, min_buf_size);
  883. return -EINVAL;
  884. }
  885. /* check if beyond RFC1001 maximum length */
  886. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  887. cERROR(1, "buffer length %d or smb length %d too large",
  888. buffer_length, smb_len);
  889. return -EINVAL;
  890. }
  891. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  892. cERROR(1, "illegal server response, bad offset to data");
  893. return -EINVAL;
  894. }
  895. return 0;
  896. }
  897. /*
  898. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  899. * Caller must free buffer.
  900. */
  901. static int
  902. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  903. struct smb2_hdr *hdr, unsigned int minbufsize,
  904. char *data)
  905. {
  906. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  907. int rc;
  908. if (!data)
  909. return -EINVAL;
  910. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  911. if (rc)
  912. return rc;
  913. memcpy(data, begin_of_buf, buffer_length);
  914. return 0;
  915. }
  916. static int
  917. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  918. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  919. size_t output_len, size_t min_len, void *data)
  920. {
  921. struct smb2_query_info_req *req;
  922. struct smb2_query_info_rsp *rsp = NULL;
  923. struct kvec iov[2];
  924. int rc = 0;
  925. int resp_buftype;
  926. struct TCP_Server_Info *server;
  927. struct cifs_ses *ses = tcon->ses;
  928. cFYI(1, "Query Info");
  929. if (ses && (ses->server))
  930. server = ses->server;
  931. else
  932. return -EIO;
  933. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  934. if (rc)
  935. return rc;
  936. req->InfoType = SMB2_O_INFO_FILE;
  937. req->FileInfoClass = info_class;
  938. req->PersistentFileId = persistent_fid;
  939. req->VolatileFileId = volatile_fid;
  940. /* 4 for rfc1002 length field and 1 for Buffer */
  941. req->InputBufferOffset =
  942. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  943. req->OutputBufferLength = cpu_to_le32(output_len);
  944. iov[0].iov_base = (char *)req;
  945. /* 4 for rfc1002 length field */
  946. iov[0].iov_len = get_rfc1002_length(req) + 4;
  947. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  948. if (rc) {
  949. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  950. goto qinf_exit;
  951. }
  952. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  953. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  954. le32_to_cpu(rsp->OutputBufferLength),
  955. &rsp->hdr, min_len, data);
  956. qinf_exit:
  957. free_rsp_buf(resp_buftype, rsp);
  958. return rc;
  959. }
  960. int
  961. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  962. u64 persistent_fid, u64 volatile_fid,
  963. struct smb2_file_all_info *data)
  964. {
  965. return query_info(xid, tcon, persistent_fid, volatile_fid,
  966. FILE_ALL_INFORMATION,
  967. sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  968. sizeof(struct smb2_file_all_info), data);
  969. }
  970. int
  971. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  972. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  973. {
  974. return query_info(xid, tcon, persistent_fid, volatile_fid,
  975. FILE_INTERNAL_INFORMATION,
  976. sizeof(struct smb2_file_internal_info),
  977. sizeof(struct smb2_file_internal_info), uniqueid);
  978. }
  979. /*
  980. * This is a no-op for now. We're not really interested in the reply, but
  981. * rather in the fact that the server sent one and that server->lstrp
  982. * gets updated.
  983. *
  984. * FIXME: maybe we should consider checking that the reply matches request?
  985. */
  986. static void
  987. smb2_echo_callback(struct mid_q_entry *mid)
  988. {
  989. struct TCP_Server_Info *server = mid->callback_data;
  990. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  991. unsigned int credits_received = 1;
  992. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  993. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  994. DeleteMidQEntry(mid);
  995. add_credits(server, credits_received, CIFS_ECHO_OP);
  996. }
  997. int
  998. SMB2_echo(struct TCP_Server_Info *server)
  999. {
  1000. struct smb2_echo_req *req;
  1001. int rc = 0;
  1002. struct kvec iov;
  1003. cFYI(1, "In echo request");
  1004. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1005. if (rc)
  1006. return rc;
  1007. req->hdr.CreditRequest = cpu_to_le16(1);
  1008. iov.iov_base = (char *)req;
  1009. /* 4 for rfc1002 length field */
  1010. iov.iov_len = get_rfc1002_length(req) + 4;
  1011. rc = cifs_call_async(server, &iov, 1, NULL, smb2_echo_callback, server,
  1012. CIFS_ECHO_OP);
  1013. if (rc)
  1014. cFYI(1, "Echo request failed: %d", rc);
  1015. cifs_small_buf_release(req);
  1016. return rc;
  1017. }
  1018. int
  1019. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1020. u64 volatile_fid)
  1021. {
  1022. struct smb2_flush_req *req;
  1023. struct TCP_Server_Info *server;
  1024. struct cifs_ses *ses = tcon->ses;
  1025. struct kvec iov[1];
  1026. int resp_buftype;
  1027. int rc = 0;
  1028. cFYI(1, "Flush");
  1029. if (ses && (ses->server))
  1030. server = ses->server;
  1031. else
  1032. return -EIO;
  1033. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1034. if (rc)
  1035. return rc;
  1036. req->PersistentFileId = persistent_fid;
  1037. req->VolatileFileId = volatile_fid;
  1038. iov[0].iov_base = (char *)req;
  1039. /* 4 for rfc1002 length field */
  1040. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1041. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1042. if ((rc != 0) && tcon)
  1043. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1044. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1045. return rc;
  1046. }
  1047. /*
  1048. * To form a chain of read requests, any read requests after the first should
  1049. * have the end_of_chain boolean set to true.
  1050. */
  1051. static int
  1052. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1053. unsigned int remaining_bytes, int request_type)
  1054. {
  1055. int rc = -EACCES;
  1056. struct smb2_read_req *req = NULL;
  1057. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1058. if (rc)
  1059. return rc;
  1060. if (io_parms->tcon->ses->server == NULL)
  1061. return -ECONNABORTED;
  1062. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1063. req->PersistentFileId = io_parms->persistent_fid;
  1064. req->VolatileFileId = io_parms->volatile_fid;
  1065. req->ReadChannelInfoOffset = 0; /* reserved */
  1066. req->ReadChannelInfoLength = 0; /* reserved */
  1067. req->Channel = 0; /* reserved */
  1068. req->MinimumCount = 0;
  1069. req->Length = cpu_to_le32(io_parms->length);
  1070. req->Offset = cpu_to_le64(io_parms->offset);
  1071. if (request_type & CHAINED_REQUEST) {
  1072. if (!(request_type & END_OF_CHAIN)) {
  1073. /* 4 for rfc1002 length field */
  1074. req->hdr.NextCommand =
  1075. cpu_to_le32(get_rfc1002_length(req) + 4);
  1076. } else /* END_OF_CHAIN */
  1077. req->hdr.NextCommand = 0;
  1078. if (request_type & RELATED_REQUEST) {
  1079. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1080. /*
  1081. * Related requests use info from previous read request
  1082. * in chain.
  1083. */
  1084. req->hdr.SessionId = 0xFFFFFFFF;
  1085. req->hdr.TreeId = 0xFFFFFFFF;
  1086. req->PersistentFileId = 0xFFFFFFFF;
  1087. req->VolatileFileId = 0xFFFFFFFF;
  1088. }
  1089. }
  1090. if (remaining_bytes > io_parms->length)
  1091. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1092. else
  1093. req->RemainingBytes = 0;
  1094. iov[0].iov_base = (char *)req;
  1095. /* 4 for rfc1002 length field */
  1096. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1097. return rc;
  1098. }
  1099. static void
  1100. smb2_readv_callback(struct mid_q_entry *mid)
  1101. {
  1102. struct cifs_readdata *rdata = mid->callback_data;
  1103. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1104. struct TCP_Server_Info *server = tcon->ses->server;
  1105. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov[0].iov_base;
  1106. unsigned int credits_received = 1;
  1107. cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
  1108. mid->mid, mid->mid_state, rdata->result, rdata->bytes);
  1109. switch (mid->mid_state) {
  1110. case MID_RESPONSE_RECEIVED:
  1111. credits_received = le16_to_cpu(buf->CreditRequest);
  1112. /* result already set, check signature */
  1113. if (server->sec_mode &
  1114. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
  1115. int rc;
  1116. rc = smb2_verify_signature2(rdata->iov, rdata->nr_iov,
  1117. server);
  1118. if (rc)
  1119. cERROR(1, "SMB signature verification returned "
  1120. "error = %d", rc);
  1121. }
  1122. /* FIXME: should this be counted toward the initiating task? */
  1123. task_io_account_read(rdata->bytes);
  1124. cifs_stats_bytes_read(tcon, rdata->bytes);
  1125. break;
  1126. case MID_REQUEST_SUBMITTED:
  1127. case MID_RETRY_NEEDED:
  1128. rdata->result = -EAGAIN;
  1129. break;
  1130. default:
  1131. if (rdata->result != -ENODATA)
  1132. rdata->result = -EIO;
  1133. }
  1134. if (rdata->result)
  1135. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1136. queue_work(cifsiod_wq, &rdata->work);
  1137. DeleteMidQEntry(mid);
  1138. add_credits(server, credits_received, 0);
  1139. }
  1140. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1141. int
  1142. smb2_async_readv(struct cifs_readdata *rdata)
  1143. {
  1144. int rc;
  1145. struct smb2_hdr *buf;
  1146. struct cifs_io_parms io_parms;
  1147. cFYI(1, "%s: offset=%llu bytes=%u", __func__,
  1148. rdata->offset, rdata->bytes);
  1149. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1150. io_parms.offset = rdata->offset;
  1151. io_parms.length = rdata->bytes;
  1152. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1153. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1154. io_parms.pid = rdata->pid;
  1155. rc = smb2_new_read_req(&rdata->iov[0], &io_parms, 0, 0);
  1156. if (rc)
  1157. return rc;
  1158. buf = (struct smb2_hdr *)rdata->iov[0].iov_base;
  1159. /* 4 for rfc1002 length field */
  1160. rdata->iov[0].iov_len = get_rfc1002_length(rdata->iov[0].iov_base) + 4;
  1161. kref_get(&rdata->refcount);
  1162. rc = cifs_call_async(io_parms.tcon->ses->server, rdata->iov, 1,
  1163. cifs_readv_receive, smb2_readv_callback,
  1164. rdata, 0);
  1165. if (rc)
  1166. kref_put(&rdata->refcount, cifs_readdata_release);
  1167. cifs_small_buf_release(buf);
  1168. return rc;
  1169. }
  1170. int
  1171. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1172. unsigned int *nbytes, char **buf, int *buf_type)
  1173. {
  1174. int resp_buftype, rc = -EACCES;
  1175. struct smb2_read_rsp *rsp = NULL;
  1176. struct kvec iov[1];
  1177. *nbytes = 0;
  1178. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1179. if (rc)
  1180. return rc;
  1181. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1182. &resp_buftype, CIFS_LOG_ERROR);
  1183. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1184. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1185. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1186. return 0;
  1187. }
  1188. if (rc) {
  1189. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1190. cERROR(1, "Send error in read = %d", rc);
  1191. } else {
  1192. *nbytes = le32_to_cpu(rsp->DataLength);
  1193. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1194. (*nbytes > io_parms->length)) {
  1195. cFYI(1, "bad length %d for count %d", *nbytes,
  1196. io_parms->length);
  1197. rc = -EIO;
  1198. *nbytes = 0;
  1199. }
  1200. }
  1201. if (*buf) {
  1202. memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
  1203. *nbytes);
  1204. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1205. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1206. *buf = iov[0].iov_base;
  1207. if (resp_buftype == CIFS_SMALL_BUFFER)
  1208. *buf_type = CIFS_SMALL_BUFFER;
  1209. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1210. *buf_type = CIFS_LARGE_BUFFER;
  1211. }
  1212. return rc;
  1213. }
  1214. /*
  1215. * Check the mid_state and signature on received buffer (if any), and queue the
  1216. * workqueue completion task.
  1217. */
  1218. static void
  1219. smb2_writev_callback(struct mid_q_entry *mid)
  1220. {
  1221. struct cifs_writedata *wdata = mid->callback_data;
  1222. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1223. unsigned int written;
  1224. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1225. unsigned int credits_received = 1;
  1226. switch (mid->mid_state) {
  1227. case MID_RESPONSE_RECEIVED:
  1228. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1229. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1230. if (wdata->result != 0)
  1231. break;
  1232. written = le32_to_cpu(rsp->DataLength);
  1233. /*
  1234. * Mask off high 16 bits when bytes written as returned
  1235. * by the server is greater than bytes requested by the
  1236. * client. OS/2 servers are known to set incorrect
  1237. * CountHigh values.
  1238. */
  1239. if (written > wdata->bytes)
  1240. written &= 0xFFFF;
  1241. if (written < wdata->bytes)
  1242. wdata->result = -ENOSPC;
  1243. else
  1244. wdata->bytes = written;
  1245. break;
  1246. case MID_REQUEST_SUBMITTED:
  1247. case MID_RETRY_NEEDED:
  1248. wdata->result = -EAGAIN;
  1249. break;
  1250. default:
  1251. wdata->result = -EIO;
  1252. break;
  1253. }
  1254. if (wdata->result)
  1255. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1256. queue_work(cifsiod_wq, &wdata->work);
  1257. DeleteMidQEntry(mid);
  1258. add_credits(tcon->ses->server, credits_received, 0);
  1259. }
  1260. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1261. int
  1262. smb2_async_writev(struct cifs_writedata *wdata)
  1263. {
  1264. int i, rc = -EACCES;
  1265. struct smb2_write_req *req = NULL;
  1266. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1267. struct kvec *iov = NULL;
  1268. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1269. if (rc)
  1270. goto async_writev_out;
  1271. /* 1 iov per page + 1 for header */
  1272. iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
  1273. if (iov == NULL) {
  1274. rc = -ENOMEM;
  1275. goto async_writev_out;
  1276. }
  1277. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1278. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1279. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1280. req->WriteChannelInfoOffset = 0;
  1281. req->WriteChannelInfoLength = 0;
  1282. req->Channel = 0;
  1283. req->Offset = cpu_to_le64(wdata->offset);
  1284. /* 4 for rfc1002 length field */
  1285. req->DataOffset = cpu_to_le16(
  1286. offsetof(struct smb2_write_req, Buffer) - 4);
  1287. req->RemainingBytes = 0;
  1288. /* 4 for rfc1002 length field and 1 for Buffer */
  1289. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1290. iov[0].iov_base = (char *)req;
  1291. /*
  1292. * This function should marshal up the page array into the kvec
  1293. * array, reserving [0] for the header. It should kmap the pages
  1294. * and set the iov_len properly for each one. It may also set
  1295. * wdata->bytes too.
  1296. */
  1297. cifs_kmap_lock();
  1298. wdata->marshal_iov(iov, wdata);
  1299. cifs_kmap_unlock();
  1300. cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
  1301. req->Length = cpu_to_le32(wdata->bytes);
  1302. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1303. kref_get(&wdata->refcount);
  1304. rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
  1305. NULL, smb2_writev_callback, wdata, 0);
  1306. if (rc)
  1307. kref_put(&wdata->refcount, cifs_writedata_release);
  1308. /* send is done, unmap pages */
  1309. for (i = 0; i < wdata->nr_pages; i++)
  1310. kunmap(wdata->pages[i]);
  1311. async_writev_out:
  1312. cifs_small_buf_release(req);
  1313. kfree(iov);
  1314. return rc;
  1315. }
  1316. /*
  1317. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1318. * The length field from io_parms must be at least 1 and indicates a number of
  1319. * elements with data to write that begins with position 1 in iov array. All
  1320. * data length is specified by count.
  1321. */
  1322. int
  1323. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1324. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1325. {
  1326. int rc = 0;
  1327. struct smb2_write_req *req = NULL;
  1328. struct smb2_write_rsp *rsp = NULL;
  1329. int resp_buftype;
  1330. *nbytes = 0;
  1331. if (n_vec < 1)
  1332. return rc;
  1333. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1334. if (rc)
  1335. return rc;
  1336. if (io_parms->tcon->ses->server == NULL)
  1337. return -ECONNABORTED;
  1338. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1339. req->PersistentFileId = io_parms->persistent_fid;
  1340. req->VolatileFileId = io_parms->volatile_fid;
  1341. req->WriteChannelInfoOffset = 0;
  1342. req->WriteChannelInfoLength = 0;
  1343. req->Channel = 0;
  1344. req->Length = cpu_to_le32(io_parms->length);
  1345. req->Offset = cpu_to_le64(io_parms->offset);
  1346. /* 4 for rfc1002 length field */
  1347. req->DataOffset = cpu_to_le16(
  1348. offsetof(struct smb2_write_req, Buffer) - 4);
  1349. req->RemainingBytes = 0;
  1350. iov[0].iov_base = (char *)req;
  1351. /* 4 for rfc1002 length field and 1 for Buffer */
  1352. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1353. /* length of entire message including data to be written */
  1354. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1355. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1356. &resp_buftype, 0);
  1357. if (rc) {
  1358. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1359. cERROR(1, "Send error in write = %d", rc);
  1360. } else {
  1361. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1362. *nbytes = le32_to_cpu(rsp->DataLength);
  1363. free_rsp_buf(resp_buftype, rsp);
  1364. }
  1365. return rc;
  1366. }
  1367. static int
  1368. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1369. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  1370. unsigned int num, void **data, unsigned int *size)
  1371. {
  1372. struct smb2_set_info_req *req;
  1373. struct smb2_set_info_rsp *rsp = NULL;
  1374. struct kvec *iov;
  1375. int rc = 0;
  1376. int resp_buftype;
  1377. unsigned int i;
  1378. struct TCP_Server_Info *server;
  1379. struct cifs_ses *ses = tcon->ses;
  1380. if (ses && (ses->server))
  1381. server = ses->server;
  1382. else
  1383. return -EIO;
  1384. if (!num)
  1385. return -EINVAL;
  1386. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  1387. if (!iov)
  1388. return -ENOMEM;
  1389. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  1390. if (rc) {
  1391. kfree(iov);
  1392. return rc;
  1393. }
  1394. req->hdr.ProcessId = cpu_to_le32(pid);
  1395. req->InfoType = SMB2_O_INFO_FILE;
  1396. req->FileInfoClass = info_class;
  1397. req->PersistentFileId = persistent_fid;
  1398. req->VolatileFileId = volatile_fid;
  1399. /* 4 for RFC1001 length and 1 for Buffer */
  1400. req->BufferOffset =
  1401. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  1402. req->BufferLength = cpu_to_le32(*size);
  1403. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  1404. memcpy(req->Buffer, *data, *size);
  1405. iov[0].iov_base = (char *)req;
  1406. /* 4 for RFC1001 length */
  1407. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1408. for (i = 1; i < num; i++) {
  1409. inc_rfc1001_len(req, size[i]);
  1410. le32_add_cpu(&req->BufferLength, size[i]);
  1411. iov[i].iov_base = (char *)data[i];
  1412. iov[i].iov_len = size[i];
  1413. }
  1414. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  1415. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  1416. if (rc != 0) {
  1417. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  1418. goto out;
  1419. }
  1420. if (rsp == NULL) {
  1421. rc = -EIO;
  1422. goto out;
  1423. }
  1424. out:
  1425. free_rsp_buf(resp_buftype, rsp);
  1426. kfree(iov);
  1427. return rc;
  1428. }
  1429. int
  1430. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  1431. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1432. {
  1433. struct smb2_file_rename_info info;
  1434. void **data;
  1435. unsigned int size[2];
  1436. int rc;
  1437. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1438. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1439. if (!data)
  1440. return -ENOMEM;
  1441. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  1442. /* 0 = fail if target already exists */
  1443. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1444. info.FileNameLength = cpu_to_le32(len);
  1445. data[0] = &info;
  1446. size[0] = sizeof(struct smb2_file_rename_info);
  1447. data[1] = target_file;
  1448. size[1] = len + 2 /* null */;
  1449. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1450. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  1451. size);
  1452. kfree(data);
  1453. return rc;
  1454. }
  1455. int
  1456. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  1457. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1458. {
  1459. struct smb2_file_link_info info;
  1460. void **data;
  1461. unsigned int size[2];
  1462. int rc;
  1463. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1464. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1465. if (!data)
  1466. return -ENOMEM;
  1467. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  1468. /* 0 = fail if link already exists */
  1469. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1470. info.FileNameLength = cpu_to_le32(len);
  1471. data[0] = &info;
  1472. size[0] = sizeof(struct smb2_file_link_info);
  1473. data[1] = target_file;
  1474. size[1] = len + 2 /* null */;
  1475. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1476. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  1477. kfree(data);
  1478. return rc;
  1479. }
  1480. int
  1481. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1482. u64 volatile_fid, u32 pid, __le64 *eof)
  1483. {
  1484. struct smb2_file_eof_info info;
  1485. void *data;
  1486. unsigned int size;
  1487. info.EndOfFile = *eof;
  1488. data = &info;
  1489. size = sizeof(struct smb2_file_eof_info);
  1490. return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
  1491. FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  1492. }