smb2pdu.c 60 KB

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