smb2misc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * fs/cifs/smb2misc.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2011
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/ctype.h>
  24. #include "smb2pdu.h"
  25. #include "cifsglob.h"
  26. #include "cifsproto.h"
  27. #include "smb2proto.h"
  28. #include "cifs_debug.h"
  29. #include "cifs_unicode.h"
  30. #include "smb2status.h"
  31. #include "smb2glob.h"
  32. static int
  33. check_smb2_hdr(struct smb2_sync_hdr *shdr, __u64 mid)
  34. {
  35. __u64 wire_mid = le64_to_cpu(shdr->MessageId);
  36. /*
  37. * Make sure that this really is an SMB, that it is a response,
  38. * and that the message ids match.
  39. */
  40. if ((shdr->ProtocolId == SMB2_PROTO_NUMBER) &&
  41. (mid == wire_mid)) {
  42. if (shdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
  43. return 0;
  44. else {
  45. /* only one valid case where server sends us request */
  46. if (shdr->Command == SMB2_OPLOCK_BREAK)
  47. return 0;
  48. else
  49. cifs_dbg(VFS, "Received Request not response\n");
  50. }
  51. } else { /* bad signature or mid */
  52. if (shdr->ProtocolId != SMB2_PROTO_NUMBER)
  53. cifs_dbg(VFS, "Bad protocol string signature header %x\n",
  54. le32_to_cpu(shdr->ProtocolId));
  55. if (mid != wire_mid)
  56. cifs_dbg(VFS, "Mids do not match: %llu and %llu\n",
  57. mid, wire_mid);
  58. }
  59. cifs_dbg(VFS, "Bad SMB detected. The Mid=%llu\n", wire_mid);
  60. return 1;
  61. }
  62. /*
  63. * The following table defines the expected "StructureSize" of SMB2 responses
  64. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS responses.
  65. *
  66. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  67. * indexed by command in host byte order
  68. */
  69. static const __le16 smb2_rsp_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  70. /* SMB2_NEGOTIATE */ cpu_to_le16(65),
  71. /* SMB2_SESSION_SETUP */ cpu_to_le16(9),
  72. /* SMB2_LOGOFF */ cpu_to_le16(4),
  73. /* SMB2_TREE_CONNECT */ cpu_to_le16(16),
  74. /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4),
  75. /* SMB2_CREATE */ cpu_to_le16(89),
  76. /* SMB2_CLOSE */ cpu_to_le16(60),
  77. /* SMB2_FLUSH */ cpu_to_le16(4),
  78. /* SMB2_READ */ cpu_to_le16(17),
  79. /* SMB2_WRITE */ cpu_to_le16(17),
  80. /* SMB2_LOCK */ cpu_to_le16(4),
  81. /* SMB2_IOCTL */ cpu_to_le16(49),
  82. /* BB CHECK this ... not listed in documentation */
  83. /* SMB2_CANCEL */ cpu_to_le16(0),
  84. /* SMB2_ECHO */ cpu_to_le16(4),
  85. /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(9),
  86. /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(9),
  87. /* SMB2_QUERY_INFO */ cpu_to_le16(9),
  88. /* SMB2_SET_INFO */ cpu_to_le16(2),
  89. /* BB FIXME can also be 44 for lease break */
  90. /* SMB2_OPLOCK_BREAK */ cpu_to_le16(24)
  91. };
  92. #ifdef CONFIG_CIFS_SMB311
  93. static __u32 get_neg_ctxt_len(struct smb2_sync_hdr *hdr, __u32 len,
  94. __u32 non_ctxlen)
  95. {
  96. __u16 neg_count;
  97. __u32 nc_offset, size_of_pad_before_neg_ctxts;
  98. struct smb2_negotiate_rsp *pneg_rsp = (struct smb2_negotiate_rsp *)hdr;
  99. /* Negotiate contexts are only valid for latest dialect SMB3.11 */
  100. neg_count = le16_to_cpu(pneg_rsp->NegotiateContextCount);
  101. if ((neg_count == 0) ||
  102. (pneg_rsp->DialectRevision != cpu_to_le16(SMB311_PROT_ID)))
  103. return 0;
  104. /* Make sure that negotiate contexts start after gss security blob */
  105. nc_offset = le32_to_cpu(pneg_rsp->NegotiateContextOffset);
  106. if (nc_offset < non_ctxlen) {
  107. printk_once(KERN_WARNING "invalid negotiate context offset\n");
  108. return 0;
  109. }
  110. size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen;
  111. /* Verify that at least minimal negotiate contexts fit within frame */
  112. if (len < nc_offset + (neg_count * sizeof(struct smb2_neg_context))) {
  113. printk_once(KERN_WARNING "negotiate context goes beyond end\n");
  114. return 0;
  115. }
  116. cifs_dbg(FYI, "length of negcontexts %d pad %d\n",
  117. len - nc_offset, size_of_pad_before_neg_ctxts);
  118. /* length of negcontexts including pad from end of sec blob to them */
  119. return (len - nc_offset) + size_of_pad_before_neg_ctxts;
  120. }
  121. #endif /* CIFS_SMB311 */
  122. int
  123. smb2_check_message(char *buf, unsigned int len, struct TCP_Server_Info *srvr)
  124. {
  125. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
  126. struct smb2_sync_pdu *pdu = (struct smb2_sync_pdu *)shdr;
  127. __u64 mid;
  128. __u32 clc_len; /* calculated length */
  129. int command;
  130. int pdu_size = sizeof(struct smb2_sync_pdu);
  131. int hdr_size = sizeof(struct smb2_sync_hdr);
  132. /*
  133. * Add function to do table lookup of StructureSize by command
  134. * ie Validate the wct via smb2_struct_sizes table above
  135. */
  136. if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
  137. struct smb2_transform_hdr *thdr =
  138. (struct smb2_transform_hdr *)buf;
  139. struct cifs_ses *ses = NULL;
  140. struct list_head *tmp;
  141. /* decrypt frame now that it is completely read in */
  142. spin_lock(&cifs_tcp_ses_lock);
  143. list_for_each(tmp, &srvr->smb_ses_list) {
  144. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  145. if (ses->Suid == thdr->SessionId)
  146. break;
  147. ses = NULL;
  148. }
  149. spin_unlock(&cifs_tcp_ses_lock);
  150. if (ses == NULL) {
  151. cifs_dbg(VFS, "no decryption - session id not found\n");
  152. return 1;
  153. }
  154. }
  155. mid = le64_to_cpu(shdr->MessageId);
  156. if (len < pdu_size) {
  157. if ((len >= hdr_size)
  158. && (shdr->Status != 0)) {
  159. pdu->StructureSize2 = 0;
  160. /*
  161. * As with SMB/CIFS, on some error cases servers may
  162. * not return wct properly
  163. */
  164. return 0;
  165. } else {
  166. cifs_dbg(VFS, "Length less than SMB header size\n");
  167. }
  168. return 1;
  169. }
  170. if (len > CIFSMaxBufSize + MAX_SMB2_HDR_SIZE) {
  171. cifs_dbg(VFS, "SMB length greater than maximum, mid=%llu\n",
  172. mid);
  173. return 1;
  174. }
  175. if (check_smb2_hdr(shdr, mid))
  176. return 1;
  177. if (shdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
  178. cifs_dbg(VFS, "Illegal structure size %u\n",
  179. le16_to_cpu(shdr->StructureSize));
  180. return 1;
  181. }
  182. command = le16_to_cpu(shdr->Command);
  183. if (command >= NUMBER_OF_SMB2_COMMANDS) {
  184. cifs_dbg(VFS, "Illegal SMB2 command %d\n", command);
  185. return 1;
  186. }
  187. if (smb2_rsp_struct_sizes[command] != pdu->StructureSize2) {
  188. if (command != SMB2_OPLOCK_BREAK_HE && (shdr->Status == 0 ||
  189. pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2)) {
  190. /* error packets have 9 byte structure size */
  191. cifs_dbg(VFS, "Illegal response size %u for command %d\n",
  192. le16_to_cpu(pdu->StructureSize2), command);
  193. return 1;
  194. } else if (command == SMB2_OPLOCK_BREAK_HE
  195. && (shdr->Status == 0)
  196. && (le16_to_cpu(pdu->StructureSize2) != 44)
  197. && (le16_to_cpu(pdu->StructureSize2) != 36)) {
  198. /* special case for SMB2.1 lease break message */
  199. cifs_dbg(VFS, "Illegal response size %d for oplock break\n",
  200. le16_to_cpu(pdu->StructureSize2));
  201. return 1;
  202. }
  203. }
  204. clc_len = smb2_calc_size(buf, srvr);
  205. #ifdef CONFIG_CIFS_SMB311
  206. if (shdr->Command == SMB2_NEGOTIATE)
  207. clc_len += get_neg_ctxt_len(shdr, len, clc_len);
  208. #endif /* SMB311 */
  209. if (len != clc_len) {
  210. cifs_dbg(FYI, "Calculated size %u length %u mismatch mid %llu\n",
  211. clc_len, len, mid);
  212. /* create failed on symlink */
  213. if (command == SMB2_CREATE_HE &&
  214. shdr->Status == STATUS_STOPPED_ON_SYMLINK)
  215. return 0;
  216. /* Windows 7 server returns 24 bytes more */
  217. if (clc_len + 24 == len && command == SMB2_OPLOCK_BREAK_HE)
  218. return 0;
  219. /* server can return one byte more due to implied bcc[0] */
  220. if (clc_len == len + 1)
  221. return 0;
  222. /*
  223. * MacOS server pads after SMB2.1 write response with 3 bytes
  224. * of junk. Other servers match RFC1001 len to actual
  225. * SMB2/SMB3 frame length (header + smb2 response specific data)
  226. * Some windows servers do too when compounding is used.
  227. * Log the server error (once), but allow it and continue
  228. * since the frame is parseable.
  229. */
  230. if (clc_len < len) {
  231. printk_once(KERN_WARNING
  232. "SMB2 server sent bad RFC1001 len %d not %d\n",
  233. len, clc_len);
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. /*
  241. * The size of the variable area depends on the offset and length fields
  242. * located in different fields for various SMB2 responses. SMB2 responses
  243. * with no variable length info, show an offset of zero for the offset field.
  244. */
  245. static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = {
  246. /* SMB2_NEGOTIATE */ true,
  247. /* SMB2_SESSION_SETUP */ true,
  248. /* SMB2_LOGOFF */ false,
  249. /* SMB2_TREE_CONNECT */ false,
  250. /* SMB2_TREE_DISCONNECT */ false,
  251. /* SMB2_CREATE */ true,
  252. /* SMB2_CLOSE */ false,
  253. /* SMB2_FLUSH */ false,
  254. /* SMB2_READ */ true,
  255. /* SMB2_WRITE */ false,
  256. /* SMB2_LOCK */ false,
  257. /* SMB2_IOCTL */ true,
  258. /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */
  259. /* SMB2_ECHO */ false,
  260. /* SMB2_QUERY_DIRECTORY */ true,
  261. /* SMB2_CHANGE_NOTIFY */ true,
  262. /* SMB2_QUERY_INFO */ true,
  263. /* SMB2_SET_INFO */ false,
  264. /* SMB2_OPLOCK_BREAK */ false
  265. };
  266. /*
  267. * Returns the pointer to the beginning of the data area. Length of the data
  268. * area and the offset to it (from the beginning of the smb are also returned.
  269. */
  270. char *
  271. smb2_get_data_area_len(int *off, int *len, struct smb2_sync_hdr *shdr)
  272. {
  273. *off = 0;
  274. *len = 0;
  275. /* error responses do not have data area */
  276. if (shdr->Status && shdr->Status != STATUS_MORE_PROCESSING_REQUIRED &&
  277. (((struct smb2_err_rsp *)shdr)->StructureSize) ==
  278. SMB2_ERROR_STRUCTURE_SIZE2)
  279. return NULL;
  280. /*
  281. * Following commands have data areas so we have to get the location
  282. * of the data buffer offset and data buffer length for the particular
  283. * command.
  284. */
  285. switch (shdr->Command) {
  286. case SMB2_NEGOTIATE:
  287. *off = le16_to_cpu(
  288. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferOffset);
  289. *len = le16_to_cpu(
  290. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferLength);
  291. break;
  292. case SMB2_SESSION_SETUP:
  293. *off = le16_to_cpu(
  294. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferOffset);
  295. *len = le16_to_cpu(
  296. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferLength);
  297. break;
  298. case SMB2_CREATE:
  299. *off = le32_to_cpu(
  300. ((struct smb2_create_rsp *)shdr)->CreateContextsOffset);
  301. *len = le32_to_cpu(
  302. ((struct smb2_create_rsp *)shdr)->CreateContextsLength);
  303. break;
  304. case SMB2_QUERY_INFO:
  305. *off = le16_to_cpu(
  306. ((struct smb2_query_info_rsp *)shdr)->OutputBufferOffset);
  307. *len = le32_to_cpu(
  308. ((struct smb2_query_info_rsp *)shdr)->OutputBufferLength);
  309. break;
  310. case SMB2_READ:
  311. /* TODO: is this a bug ? */
  312. *off = ((struct smb2_read_rsp *)shdr)->DataOffset;
  313. *len = le32_to_cpu(((struct smb2_read_rsp *)shdr)->DataLength);
  314. break;
  315. case SMB2_QUERY_DIRECTORY:
  316. *off = le16_to_cpu(
  317. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferOffset);
  318. *len = le32_to_cpu(
  319. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferLength);
  320. break;
  321. case SMB2_IOCTL:
  322. *off = le32_to_cpu(
  323. ((struct smb2_ioctl_rsp *)shdr)->OutputOffset);
  324. *len = le32_to_cpu(
  325. ((struct smb2_ioctl_rsp *)shdr)->OutputCount);
  326. break;
  327. case SMB2_CHANGE_NOTIFY:
  328. default:
  329. /* BB FIXME for unimplemented cases above */
  330. cifs_dbg(VFS, "no length check for command\n");
  331. break;
  332. }
  333. /*
  334. * Invalid length or offset probably means data area is invalid, but
  335. * we have little choice but to ignore the data area in this case.
  336. */
  337. if (*off > 4096) {
  338. cifs_dbg(VFS, "offset %d too large, data area ignored\n", *off);
  339. *len = 0;
  340. *off = 0;
  341. } else if (*off < 0) {
  342. cifs_dbg(VFS, "negative offset %d to data invalid ignore data area\n",
  343. *off);
  344. *off = 0;
  345. *len = 0;
  346. } else if (*len < 0) {
  347. cifs_dbg(VFS, "negative data length %d invalid, data area ignored\n",
  348. *len);
  349. *len = 0;
  350. } else if (*len > 128 * 1024) {
  351. cifs_dbg(VFS, "data area larger than 128K: %d\n", *len);
  352. *len = 0;
  353. }
  354. /* return pointer to beginning of data area, ie offset from SMB start */
  355. if ((*off != 0) && (*len != 0))
  356. return (char *)shdr + *off;
  357. else
  358. return NULL;
  359. }
  360. /*
  361. * Calculate the size of the SMB message based on the fixed header
  362. * portion, the number of word parameters and the data portion of the message.
  363. */
  364. unsigned int
  365. smb2_calc_size(void *buf, struct TCP_Server_Info *srvr)
  366. {
  367. struct smb2_sync_pdu *pdu = (struct smb2_sync_pdu *)buf;
  368. struct smb2_sync_hdr *shdr = &pdu->sync_hdr;
  369. int offset; /* the offset from the beginning of SMB to data area */
  370. int data_length; /* the length of the variable length data area */
  371. /* Structure Size has already been checked to make sure it is 64 */
  372. int len = le16_to_cpu(shdr->StructureSize);
  373. /*
  374. * StructureSize2, ie length of fixed parameter area has already
  375. * been checked to make sure it is the correct length.
  376. */
  377. len += le16_to_cpu(pdu->StructureSize2);
  378. if (has_smb2_data_area[le16_to_cpu(shdr->Command)] == false)
  379. goto calc_size_exit;
  380. smb2_get_data_area_len(&offset, &data_length, shdr);
  381. cifs_dbg(FYI, "SMB2 data length %d offset %d\n", data_length, offset);
  382. if (data_length > 0) {
  383. /*
  384. * Check to make sure that data area begins after fixed area,
  385. * Note that last byte of the fixed area is part of data area
  386. * for some commands, typically those with odd StructureSize,
  387. * so we must add one to the calculation.
  388. */
  389. if (offset + 1 < len) {
  390. cifs_dbg(VFS, "data area offset %d overlaps SMB2 header %d\n",
  391. offset + 1, len);
  392. data_length = 0;
  393. } else {
  394. len = offset + data_length;
  395. }
  396. }
  397. calc_size_exit:
  398. cifs_dbg(FYI, "SMB2 len %d\n", len);
  399. return len;
  400. }
  401. /* Note: caller must free return buffer */
  402. __le16 *
  403. cifs_convert_path_to_utf16(const char *from, struct cifs_sb_info *cifs_sb)
  404. {
  405. int len;
  406. const char *start_of_path;
  407. __le16 *to;
  408. int map_type;
  409. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
  410. map_type = SFM_MAP_UNI_RSVD;
  411. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  412. map_type = SFU_MAP_UNI_RSVD;
  413. else
  414. map_type = NO_MAP_UNI_RSVD;
  415. /* Windows doesn't allow paths beginning with \ */
  416. if (from[0] == '\\')
  417. start_of_path = from + 1;
  418. #ifdef CONFIG_CIFS_SMB311
  419. /* SMB311 POSIX extensions paths do not include leading slash */
  420. else if (cifs_sb_master_tlink(cifs_sb) &&
  421. cifs_sb_master_tcon(cifs_sb)->posix_extensions &&
  422. (from[0] == '/')) {
  423. start_of_path = from + 1;
  424. }
  425. #endif /* 311 */
  426. else
  427. start_of_path = from;
  428. to = cifs_strndup_to_utf16(start_of_path, PATH_MAX, &len,
  429. cifs_sb->local_nls, map_type);
  430. return to;
  431. }
  432. __le32
  433. smb2_get_lease_state(struct cifsInodeInfo *cinode)
  434. {
  435. __le32 lease = 0;
  436. if (CIFS_CACHE_WRITE(cinode))
  437. lease |= SMB2_LEASE_WRITE_CACHING;
  438. if (CIFS_CACHE_HANDLE(cinode))
  439. lease |= SMB2_LEASE_HANDLE_CACHING;
  440. if (CIFS_CACHE_READ(cinode))
  441. lease |= SMB2_LEASE_READ_CACHING;
  442. return lease;
  443. }
  444. struct smb2_lease_break_work {
  445. struct work_struct lease_break;
  446. struct tcon_link *tlink;
  447. __u8 lease_key[16];
  448. __le32 lease_state;
  449. };
  450. static void
  451. cifs_ses_oplock_break(struct work_struct *work)
  452. {
  453. struct smb2_lease_break_work *lw = container_of(work,
  454. struct smb2_lease_break_work, lease_break);
  455. int rc = 0;
  456. rc = SMB2_lease_break(0, tlink_tcon(lw->tlink), lw->lease_key,
  457. lw->lease_state);
  458. cifs_dbg(FYI, "Lease release rc %d\n", rc);
  459. cifs_put_tlink(lw->tlink);
  460. kfree(lw);
  461. }
  462. static bool
  463. smb2_tcon_has_lease(struct cifs_tcon *tcon, struct smb2_lease_break *rsp,
  464. struct smb2_lease_break_work *lw)
  465. {
  466. bool found;
  467. __u8 lease_state;
  468. struct list_head *tmp;
  469. struct cifsFileInfo *cfile;
  470. struct TCP_Server_Info *server = tcon->ses->server;
  471. struct cifs_pending_open *open;
  472. struct cifsInodeInfo *cinode;
  473. int ack_req = le32_to_cpu(rsp->Flags &
  474. SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);
  475. lease_state = le32_to_cpu(rsp->NewLeaseState);
  476. list_for_each(tmp, &tcon->openFileList) {
  477. cfile = list_entry(tmp, struct cifsFileInfo, tlist);
  478. cinode = CIFS_I(d_inode(cfile->dentry));
  479. if (memcmp(cinode->lease_key, rsp->LeaseKey,
  480. SMB2_LEASE_KEY_SIZE))
  481. continue;
  482. cifs_dbg(FYI, "found in the open list\n");
  483. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  484. le32_to_cpu(rsp->NewLeaseState));
  485. server->ops->set_oplock_level(cinode, lease_state, 0, NULL);
  486. if (ack_req)
  487. cfile->oplock_break_cancelled = false;
  488. else
  489. cfile->oplock_break_cancelled = true;
  490. queue_work(cifsoplockd_wq, &cfile->oplock_break);
  491. kfree(lw);
  492. return true;
  493. }
  494. found = false;
  495. list_for_each_entry(open, &tcon->pending_opens, olist) {
  496. if (memcmp(open->lease_key, rsp->LeaseKey,
  497. SMB2_LEASE_KEY_SIZE))
  498. continue;
  499. if (!found && ack_req) {
  500. found = true;
  501. memcpy(lw->lease_key, open->lease_key,
  502. SMB2_LEASE_KEY_SIZE);
  503. lw->tlink = cifs_get_tlink(open->tlink);
  504. queue_work(cifsiod_wq, &lw->lease_break);
  505. }
  506. cifs_dbg(FYI, "found in the pending open list\n");
  507. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  508. le32_to_cpu(rsp->NewLeaseState));
  509. open->oplock = lease_state;
  510. }
  511. return found;
  512. }
  513. static bool
  514. smb2_is_valid_lease_break(char *buffer)
  515. {
  516. struct smb2_lease_break *rsp = (struct smb2_lease_break *)buffer;
  517. struct list_head *tmp, *tmp1, *tmp2;
  518. struct TCP_Server_Info *server;
  519. struct cifs_ses *ses;
  520. struct cifs_tcon *tcon;
  521. struct smb2_lease_break_work *lw;
  522. lw = kmalloc(sizeof(struct smb2_lease_break_work), GFP_KERNEL);
  523. if (!lw)
  524. return false;
  525. INIT_WORK(&lw->lease_break, cifs_ses_oplock_break);
  526. lw->lease_state = rsp->NewLeaseState;
  527. cifs_dbg(FYI, "Checking for lease break\n");
  528. /* look up tcon based on tid & uid */
  529. spin_lock(&cifs_tcp_ses_lock);
  530. list_for_each(tmp, &cifs_tcp_ses_list) {
  531. server = list_entry(tmp, struct TCP_Server_Info, tcp_ses_list);
  532. list_for_each(tmp1, &server->smb_ses_list) {
  533. ses = list_entry(tmp1, struct cifs_ses, smb_ses_list);
  534. list_for_each(tmp2, &ses->tcon_list) {
  535. tcon = list_entry(tmp2, struct cifs_tcon,
  536. tcon_list);
  537. spin_lock(&tcon->open_file_lock);
  538. cifs_stats_inc(
  539. &tcon->stats.cifs_stats.num_oplock_brks);
  540. if (smb2_tcon_has_lease(tcon, rsp, lw)) {
  541. spin_unlock(&tcon->open_file_lock);
  542. spin_unlock(&cifs_tcp_ses_lock);
  543. return true;
  544. }
  545. spin_unlock(&tcon->open_file_lock);
  546. if (tcon->crfid.is_valid &&
  547. !memcmp(rsp->LeaseKey,
  548. tcon->crfid.fid->lease_key,
  549. SMB2_LEASE_KEY_SIZE)) {
  550. INIT_WORK(&tcon->crfid.lease_break,
  551. smb2_cached_lease_break);
  552. queue_work(cifsiod_wq,
  553. &tcon->crfid.lease_break);
  554. spin_unlock(&cifs_tcp_ses_lock);
  555. return true;
  556. }
  557. }
  558. }
  559. }
  560. spin_unlock(&cifs_tcp_ses_lock);
  561. kfree(lw);
  562. cifs_dbg(FYI, "Can not process lease break - no lease matched\n");
  563. return false;
  564. }
  565. bool
  566. smb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server)
  567. {
  568. struct smb2_oplock_break *rsp = (struct smb2_oplock_break *)buffer;
  569. struct list_head *tmp, *tmp1, *tmp2;
  570. struct cifs_ses *ses;
  571. struct cifs_tcon *tcon;
  572. struct cifsInodeInfo *cinode;
  573. struct cifsFileInfo *cfile;
  574. cifs_dbg(FYI, "Checking for oplock break\n");
  575. if (rsp->sync_hdr.Command != SMB2_OPLOCK_BREAK)
  576. return false;
  577. if (rsp->StructureSize !=
  578. smb2_rsp_struct_sizes[SMB2_OPLOCK_BREAK_HE]) {
  579. if (le16_to_cpu(rsp->StructureSize) == 44)
  580. return smb2_is_valid_lease_break(buffer);
  581. else
  582. return false;
  583. }
  584. cifs_dbg(FYI, "oplock level 0x%x\n", rsp->OplockLevel);
  585. /* look up tcon based on tid & uid */
  586. spin_lock(&cifs_tcp_ses_lock);
  587. list_for_each(tmp, &server->smb_ses_list) {
  588. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  589. list_for_each(tmp1, &ses->tcon_list) {
  590. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  591. cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
  592. spin_lock(&tcon->open_file_lock);
  593. list_for_each(tmp2, &tcon->openFileList) {
  594. cfile = list_entry(tmp2, struct cifsFileInfo,
  595. tlist);
  596. if (rsp->PersistentFid !=
  597. cfile->fid.persistent_fid ||
  598. rsp->VolatileFid !=
  599. cfile->fid.volatile_fid)
  600. continue;
  601. cifs_dbg(FYI, "file id match, oplock break\n");
  602. cinode = CIFS_I(d_inode(cfile->dentry));
  603. spin_lock(&cfile->file_info_lock);
  604. if (!CIFS_CACHE_WRITE(cinode) &&
  605. rsp->OplockLevel == SMB2_OPLOCK_LEVEL_NONE)
  606. cfile->oplock_break_cancelled = true;
  607. else
  608. cfile->oplock_break_cancelled = false;
  609. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  610. &cinode->flags);
  611. /*
  612. * Set flag if the server downgrades the oplock
  613. * to L2 else clear.
  614. */
  615. if (rsp->OplockLevel)
  616. set_bit(
  617. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  618. &cinode->flags);
  619. else
  620. clear_bit(
  621. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  622. &cinode->flags);
  623. spin_unlock(&cfile->file_info_lock);
  624. queue_work(cifsoplockd_wq,
  625. &cfile->oplock_break);
  626. spin_unlock(&tcon->open_file_lock);
  627. spin_unlock(&cifs_tcp_ses_lock);
  628. return true;
  629. }
  630. spin_unlock(&tcon->open_file_lock);
  631. spin_unlock(&cifs_tcp_ses_lock);
  632. cifs_dbg(FYI, "No matching file for oplock break\n");
  633. return true;
  634. }
  635. }
  636. spin_unlock(&cifs_tcp_ses_lock);
  637. cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
  638. return false;
  639. }
  640. void
  641. smb2_cancelled_close_fid(struct work_struct *work)
  642. {
  643. struct close_cancelled_open *cancelled = container_of(work,
  644. struct close_cancelled_open, work);
  645. cifs_dbg(VFS, "Close unmatched open\n");
  646. SMB2_close(0, cancelled->tcon, cancelled->fid.persistent_fid,
  647. cancelled->fid.volatile_fid);
  648. cifs_put_tcon(cancelled->tcon);
  649. kfree(cancelled);
  650. }
  651. int
  652. smb2_handle_cancelled_mid(char *buffer, struct TCP_Server_Info *server)
  653. {
  654. struct smb2_sync_hdr *sync_hdr = (struct smb2_sync_hdr *)buffer;
  655. struct smb2_create_rsp *rsp = (struct smb2_create_rsp *)buffer;
  656. struct cifs_tcon *tcon;
  657. struct close_cancelled_open *cancelled;
  658. if (sync_hdr->Command != SMB2_CREATE ||
  659. sync_hdr->Status != STATUS_SUCCESS)
  660. return 0;
  661. cancelled = kzalloc(sizeof(*cancelled), GFP_KERNEL);
  662. if (!cancelled)
  663. return -ENOMEM;
  664. tcon = smb2_find_smb_tcon(server, sync_hdr->SessionId,
  665. sync_hdr->TreeId);
  666. if (!tcon) {
  667. kfree(cancelled);
  668. return -ENOENT;
  669. }
  670. cancelled->fid.persistent_fid = rsp->PersistentFileId;
  671. cancelled->fid.volatile_fid = rsp->VolatileFileId;
  672. cancelled->tcon = tcon;
  673. INIT_WORK(&cancelled->work, smb2_cancelled_close_fid);
  674. queue_work(cifsiod_wq, &cancelled->work);
  675. return 0;
  676. }
  677. #ifdef CONFIG_CIFS_SMB311
  678. /**
  679. * smb311_update_preauth_hash - update @ses hash with the packet data in @iov
  680. *
  681. * Assumes @iov does not contain the rfc1002 length and iov[0] has the
  682. * SMB2 header.
  683. */
  684. int
  685. smb311_update_preauth_hash(struct cifs_ses *ses, struct kvec *iov, int nvec)
  686. {
  687. int i, rc;
  688. struct sdesc *d;
  689. struct smb2_sync_hdr *hdr;
  690. if (ses->server->tcpStatus == CifsGood) {
  691. /* skip non smb311 connections */
  692. if (ses->server->dialect != SMB311_PROT_ID)
  693. return 0;
  694. /* skip last sess setup response */
  695. hdr = (struct smb2_sync_hdr *)iov[0].iov_base;
  696. if (hdr->Flags & SMB2_FLAGS_SIGNED)
  697. return 0;
  698. }
  699. rc = smb311_crypto_shash_allocate(ses->server);
  700. if (rc)
  701. return rc;
  702. d = ses->server->secmech.sdescsha512;
  703. rc = crypto_shash_init(&d->shash);
  704. if (rc) {
  705. cifs_dbg(VFS, "%s: could not init sha512 shash\n", __func__);
  706. return rc;
  707. }
  708. rc = crypto_shash_update(&d->shash, ses->preauth_sha_hash,
  709. SMB2_PREAUTH_HASH_SIZE);
  710. if (rc) {
  711. cifs_dbg(VFS, "%s: could not update sha512 shash\n", __func__);
  712. return rc;
  713. }
  714. for (i = 0; i < nvec; i++) {
  715. rc = crypto_shash_update(&d->shash,
  716. iov[i].iov_base, iov[i].iov_len);
  717. if (rc) {
  718. cifs_dbg(VFS, "%s: could not update sha512 shash\n",
  719. __func__);
  720. return rc;
  721. }
  722. }
  723. rc = crypto_shash_final(&d->shash, ses->preauth_sha_hash);
  724. if (rc) {
  725. cifs_dbg(VFS, "%s: could not finalize sha512 shash\n",
  726. __func__);
  727. return rc;
  728. }
  729. return 0;
  730. }
  731. #endif