smb2pdu.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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. }
  1493. int
  1494. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1495. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  1496. {
  1497. unsigned int size;
  1498. size = sizeof(FILE_BASIC_INFO);
  1499. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1500. current->tgid, FILE_BASIC_INFORMATION, 1,
  1501. (void **)&buf, &size);
  1502. }