18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * fs/cifs/smb2misc.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) International Business Machines Corp., 2002,2011 58c2ecf20Sopenharmony_ci * Etersoft, 2012 68c2ecf20Sopenharmony_ci * Author(s): Steve French (sfrench@us.ibm.com) 78c2ecf20Sopenharmony_ci * Pavel Shilovsky (pshilovsky@samba.org) 2012 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This library is free software; you can redistribute it and/or modify 108c2ecf20Sopenharmony_ci * it under the terms of the GNU Lesser General Public License as published 118c2ecf20Sopenharmony_ci * by the Free Software Foundation; either version 2.1 of the License, or 128c2ecf20Sopenharmony_ci * (at your option) any later version. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * This library is distributed in the hope that it will be useful, 158c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 168c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 178c2ecf20Sopenharmony_ci * the GNU Lesser General Public License for more details. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * You should have received a copy of the GNU Lesser General Public License 208c2ecf20Sopenharmony_ci * along with this library; if not, write to the Free Software 218c2ecf20Sopenharmony_ci * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_ci#include <linux/ctype.h> 248c2ecf20Sopenharmony_ci#include "smb2pdu.h" 258c2ecf20Sopenharmony_ci#include "cifsglob.h" 268c2ecf20Sopenharmony_ci#include "cifsproto.h" 278c2ecf20Sopenharmony_ci#include "smb2proto.h" 288c2ecf20Sopenharmony_ci#include "cifs_debug.h" 298c2ecf20Sopenharmony_ci#include "cifs_unicode.h" 308c2ecf20Sopenharmony_ci#include "smb2status.h" 318c2ecf20Sopenharmony_ci#include "smb2glob.h" 328c2ecf20Sopenharmony_ci#include "nterr.h" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic int 358c2ecf20Sopenharmony_cicheck_smb2_hdr(struct smb2_sync_hdr *shdr, __u64 mid) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci __u64 wire_mid = le64_to_cpu(shdr->MessageId); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci /* 408c2ecf20Sopenharmony_ci * Make sure that this really is an SMB, that it is a response, 418c2ecf20Sopenharmony_ci * and that the message ids match. 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_ci if ((shdr->ProtocolId == SMB2_PROTO_NUMBER) && 448c2ecf20Sopenharmony_ci (mid == wire_mid)) { 458c2ecf20Sopenharmony_ci if (shdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) 468c2ecf20Sopenharmony_ci return 0; 478c2ecf20Sopenharmony_ci else { 488c2ecf20Sopenharmony_ci /* only one valid case where server sends us request */ 498c2ecf20Sopenharmony_ci if (shdr->Command == SMB2_OPLOCK_BREAK) 508c2ecf20Sopenharmony_ci return 0; 518c2ecf20Sopenharmony_ci else 528c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Received Request not response\n"); 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci } else { /* bad signature or mid */ 558c2ecf20Sopenharmony_ci if (shdr->ProtocolId != SMB2_PROTO_NUMBER) 568c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Bad protocol string signature header %x\n", 578c2ecf20Sopenharmony_ci le32_to_cpu(shdr->ProtocolId)); 588c2ecf20Sopenharmony_ci if (mid != wire_mid) 598c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Mids do not match: %llu and %llu\n", 608c2ecf20Sopenharmony_ci mid, wire_mid); 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Bad SMB detected. The Mid=%llu\n", wire_mid); 638c2ecf20Sopenharmony_ci return 1; 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* 678c2ecf20Sopenharmony_ci * The following table defines the expected "StructureSize" of SMB2 responses 688c2ecf20Sopenharmony_ci * in order by SMB2 command. This is similar to "wct" in SMB/CIFS responses. 698c2ecf20Sopenharmony_ci * 708c2ecf20Sopenharmony_ci * Note that commands are defined in smb2pdu.h in le16 but the array below is 718c2ecf20Sopenharmony_ci * indexed by command in host byte order 728c2ecf20Sopenharmony_ci */ 738c2ecf20Sopenharmony_cistatic const __le16 smb2_rsp_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { 748c2ecf20Sopenharmony_ci /* SMB2_NEGOTIATE */ cpu_to_le16(65), 758c2ecf20Sopenharmony_ci /* SMB2_SESSION_SETUP */ cpu_to_le16(9), 768c2ecf20Sopenharmony_ci /* SMB2_LOGOFF */ cpu_to_le16(4), 778c2ecf20Sopenharmony_ci /* SMB2_TREE_CONNECT */ cpu_to_le16(16), 788c2ecf20Sopenharmony_ci /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4), 798c2ecf20Sopenharmony_ci /* SMB2_CREATE */ cpu_to_le16(89), 808c2ecf20Sopenharmony_ci /* SMB2_CLOSE */ cpu_to_le16(60), 818c2ecf20Sopenharmony_ci /* SMB2_FLUSH */ cpu_to_le16(4), 828c2ecf20Sopenharmony_ci /* SMB2_READ */ cpu_to_le16(17), 838c2ecf20Sopenharmony_ci /* SMB2_WRITE */ cpu_to_le16(17), 848c2ecf20Sopenharmony_ci /* SMB2_LOCK */ cpu_to_le16(4), 858c2ecf20Sopenharmony_ci /* SMB2_IOCTL */ cpu_to_le16(49), 868c2ecf20Sopenharmony_ci /* BB CHECK this ... not listed in documentation */ 878c2ecf20Sopenharmony_ci /* SMB2_CANCEL */ cpu_to_le16(0), 888c2ecf20Sopenharmony_ci /* SMB2_ECHO */ cpu_to_le16(4), 898c2ecf20Sopenharmony_ci /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(9), 908c2ecf20Sopenharmony_ci /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(9), 918c2ecf20Sopenharmony_ci /* SMB2_QUERY_INFO */ cpu_to_le16(9), 928c2ecf20Sopenharmony_ci /* SMB2_SET_INFO */ cpu_to_le16(2), 938c2ecf20Sopenharmony_ci /* BB FIXME can also be 44 for lease break */ 948c2ecf20Sopenharmony_ci /* SMB2_OPLOCK_BREAK */ cpu_to_le16(24) 958c2ecf20Sopenharmony_ci}; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci#define SMB311_NEGPROT_BASE_SIZE (sizeof(struct smb2_sync_hdr) + sizeof(struct smb2_negotiate_rsp)) 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic __u32 get_neg_ctxt_len(struct smb2_sync_hdr *hdr, __u32 len, 1008c2ecf20Sopenharmony_ci __u32 non_ctxlen) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci __u16 neg_count; 1038c2ecf20Sopenharmony_ci __u32 nc_offset, size_of_pad_before_neg_ctxts; 1048c2ecf20Sopenharmony_ci struct smb2_negotiate_rsp *pneg_rsp = (struct smb2_negotiate_rsp *)hdr; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* Negotiate contexts are only valid for latest dialect SMB3.11 */ 1078c2ecf20Sopenharmony_ci neg_count = le16_to_cpu(pneg_rsp->NegotiateContextCount); 1088c2ecf20Sopenharmony_ci if ((neg_count == 0) || 1098c2ecf20Sopenharmony_ci (pneg_rsp->DialectRevision != cpu_to_le16(SMB311_PROT_ID))) 1108c2ecf20Sopenharmony_ci return 0; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci /* Make sure that negotiate contexts start after gss security blob */ 1138c2ecf20Sopenharmony_ci nc_offset = le32_to_cpu(pneg_rsp->NegotiateContextOffset); 1148c2ecf20Sopenharmony_ci if (nc_offset + 1 < non_ctxlen) { 1158c2ecf20Sopenharmony_ci pr_warn_once("Invalid negotiate context offset %d\n", nc_offset); 1168c2ecf20Sopenharmony_ci return 0; 1178c2ecf20Sopenharmony_ci } else if (nc_offset + 1 == non_ctxlen) { 1188c2ecf20Sopenharmony_ci cifs_dbg(FYI, "no SPNEGO security blob in negprot rsp\n"); 1198c2ecf20Sopenharmony_ci size_of_pad_before_neg_ctxts = 0; 1208c2ecf20Sopenharmony_ci } else if (non_ctxlen == SMB311_NEGPROT_BASE_SIZE + 1) 1218c2ecf20Sopenharmony_ci /* has padding, but no SPNEGO blob */ 1228c2ecf20Sopenharmony_ci size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen + 1; 1238c2ecf20Sopenharmony_ci else 1248c2ecf20Sopenharmony_ci size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* Verify that at least minimal negotiate contexts fit within frame */ 1278c2ecf20Sopenharmony_ci if (len < nc_offset + (neg_count * sizeof(struct smb2_neg_context))) { 1288c2ecf20Sopenharmony_ci pr_warn_once("negotiate context goes beyond end\n"); 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci cifs_dbg(FYI, "length of negcontexts %d pad %d\n", 1338c2ecf20Sopenharmony_ci len - nc_offset, size_of_pad_before_neg_ctxts); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* length of negcontexts including pad from end of sec blob to them */ 1368c2ecf20Sopenharmony_ci return (len - nc_offset) + size_of_pad_before_neg_ctxts; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ciint 1408c2ecf20Sopenharmony_cismb2_check_message(char *buf, unsigned int len, struct TCP_Server_Info *srvr) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf; 1438c2ecf20Sopenharmony_ci struct smb2_sync_pdu *pdu = (struct smb2_sync_pdu *)shdr; 1448c2ecf20Sopenharmony_ci __u64 mid; 1458c2ecf20Sopenharmony_ci __u32 clc_len; /* calculated length */ 1468c2ecf20Sopenharmony_ci int command; 1478c2ecf20Sopenharmony_ci int pdu_size = sizeof(struct smb2_sync_pdu); 1488c2ecf20Sopenharmony_ci int hdr_size = sizeof(struct smb2_sync_hdr); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci /* 1518c2ecf20Sopenharmony_ci * Add function to do table lookup of StructureSize by command 1528c2ecf20Sopenharmony_ci * ie Validate the wct via smb2_struct_sizes table above 1538c2ecf20Sopenharmony_ci */ 1548c2ecf20Sopenharmony_ci if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) { 1558c2ecf20Sopenharmony_ci struct smb2_transform_hdr *thdr = 1568c2ecf20Sopenharmony_ci (struct smb2_transform_hdr *)buf; 1578c2ecf20Sopenharmony_ci struct cifs_ses *ses = NULL; 1588c2ecf20Sopenharmony_ci struct list_head *tmp; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* decrypt frame now that it is completely read in */ 1618c2ecf20Sopenharmony_ci spin_lock(&cifs_tcp_ses_lock); 1628c2ecf20Sopenharmony_ci list_for_each(tmp, &srvr->smb_ses_list) { 1638c2ecf20Sopenharmony_ci ses = list_entry(tmp, struct cifs_ses, smb_ses_list); 1648c2ecf20Sopenharmony_ci if (ses->Suid == thdr->SessionId) 1658c2ecf20Sopenharmony_ci break; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci ses = NULL; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 1708c2ecf20Sopenharmony_ci if (ses == NULL) { 1718c2ecf20Sopenharmony_ci cifs_dbg(VFS, "no decryption - session id not found\n"); 1728c2ecf20Sopenharmony_ci return 1; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci mid = le64_to_cpu(shdr->MessageId); 1778c2ecf20Sopenharmony_ci if (len < pdu_size) { 1788c2ecf20Sopenharmony_ci if ((len >= hdr_size) 1798c2ecf20Sopenharmony_ci && (shdr->Status != 0)) { 1808c2ecf20Sopenharmony_ci pdu->StructureSize2 = 0; 1818c2ecf20Sopenharmony_ci /* 1828c2ecf20Sopenharmony_ci * As with SMB/CIFS, on some error cases servers may 1838c2ecf20Sopenharmony_ci * not return wct properly 1848c2ecf20Sopenharmony_ci */ 1858c2ecf20Sopenharmony_ci return 0; 1868c2ecf20Sopenharmony_ci } else { 1878c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Length less than SMB header size\n"); 1888c2ecf20Sopenharmony_ci } 1898c2ecf20Sopenharmony_ci return 1; 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci if (len > CIFSMaxBufSize + MAX_SMB2_HDR_SIZE) { 1928c2ecf20Sopenharmony_ci cifs_dbg(VFS, "SMB length greater than maximum, mid=%llu\n", 1938c2ecf20Sopenharmony_ci mid); 1948c2ecf20Sopenharmony_ci return 1; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci if (check_smb2_hdr(shdr, mid)) 1988c2ecf20Sopenharmony_ci return 1; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci if (shdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) { 2018c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Invalid structure size %u\n", 2028c2ecf20Sopenharmony_ci le16_to_cpu(shdr->StructureSize)); 2038c2ecf20Sopenharmony_ci return 1; 2048c2ecf20Sopenharmony_ci } 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci command = le16_to_cpu(shdr->Command); 2078c2ecf20Sopenharmony_ci if (command >= NUMBER_OF_SMB2_COMMANDS) { 2088c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Invalid SMB2 command %d\n", command); 2098c2ecf20Sopenharmony_ci return 1; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (smb2_rsp_struct_sizes[command] != pdu->StructureSize2) { 2138c2ecf20Sopenharmony_ci if (command != SMB2_OPLOCK_BREAK_HE && (shdr->Status == 0 || 2148c2ecf20Sopenharmony_ci pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2)) { 2158c2ecf20Sopenharmony_ci /* error packets have 9 byte structure size */ 2168c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Invalid response size %u for command %d\n", 2178c2ecf20Sopenharmony_ci le16_to_cpu(pdu->StructureSize2), command); 2188c2ecf20Sopenharmony_ci return 1; 2198c2ecf20Sopenharmony_ci } else if (command == SMB2_OPLOCK_BREAK_HE 2208c2ecf20Sopenharmony_ci && (shdr->Status == 0) 2218c2ecf20Sopenharmony_ci && (le16_to_cpu(pdu->StructureSize2) != 44) 2228c2ecf20Sopenharmony_ci && (le16_to_cpu(pdu->StructureSize2) != 36)) { 2238c2ecf20Sopenharmony_ci /* special case for SMB2.1 lease break message */ 2248c2ecf20Sopenharmony_ci cifs_dbg(VFS, "Invalid response size %d for oplock break\n", 2258c2ecf20Sopenharmony_ci le16_to_cpu(pdu->StructureSize2)); 2268c2ecf20Sopenharmony_ci return 1; 2278c2ecf20Sopenharmony_ci } 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci clc_len = smb2_calc_size(buf, srvr); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci if (shdr->Command == SMB2_NEGOTIATE) 2338c2ecf20Sopenharmony_ci clc_len += get_neg_ctxt_len(shdr, len, clc_len); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci if (len != clc_len) { 2368c2ecf20Sopenharmony_ci cifs_dbg(FYI, "Calculated size %u length %u mismatch mid %llu\n", 2378c2ecf20Sopenharmony_ci clc_len, len, mid); 2388c2ecf20Sopenharmony_ci /* create failed on symlink */ 2398c2ecf20Sopenharmony_ci if (command == SMB2_CREATE_HE && 2408c2ecf20Sopenharmony_ci shdr->Status == STATUS_STOPPED_ON_SYMLINK) 2418c2ecf20Sopenharmony_ci return 0; 2428c2ecf20Sopenharmony_ci /* Windows 7 server returns 24 bytes more */ 2438c2ecf20Sopenharmony_ci if (clc_len + 24 == len && command == SMB2_OPLOCK_BREAK_HE) 2448c2ecf20Sopenharmony_ci return 0; 2458c2ecf20Sopenharmony_ci /* server can return one byte more due to implied bcc[0] */ 2468c2ecf20Sopenharmony_ci if (clc_len == len + 1) 2478c2ecf20Sopenharmony_ci return 0; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci /* 2508c2ecf20Sopenharmony_ci * Some windows servers (win2016) will pad also the final 2518c2ecf20Sopenharmony_ci * PDU in a compound to 8 bytes. 2528c2ecf20Sopenharmony_ci */ 2538c2ecf20Sopenharmony_ci if (((clc_len + 7) & ~7) == len) 2548c2ecf20Sopenharmony_ci return 0; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci /* 2578c2ecf20Sopenharmony_ci * MacOS server pads after SMB2.1 write response with 3 bytes 2588c2ecf20Sopenharmony_ci * of junk. Other servers match RFC1001 len to actual 2598c2ecf20Sopenharmony_ci * SMB2/SMB3 frame length (header + smb2 response specific data) 2608c2ecf20Sopenharmony_ci * Some windows servers also pad up to 8 bytes when compounding. 2618c2ecf20Sopenharmony_ci */ 2628c2ecf20Sopenharmony_ci if (clc_len < len) 2638c2ecf20Sopenharmony_ci return 0; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci pr_warn_once( 2668c2ecf20Sopenharmony_ci "srv rsp too short, len %d not %d. cmd:%d mid:%llu\n", 2678c2ecf20Sopenharmony_ci len, clc_len, command, mid); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci return 1; 2708c2ecf20Sopenharmony_ci } 2718c2ecf20Sopenharmony_ci return 0; 2728c2ecf20Sopenharmony_ci} 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci/* 2758c2ecf20Sopenharmony_ci * The size of the variable area depends on the offset and length fields 2768c2ecf20Sopenharmony_ci * located in different fields for various SMB2 responses. SMB2 responses 2778c2ecf20Sopenharmony_ci * with no variable length info, show an offset of zero for the offset field. 2788c2ecf20Sopenharmony_ci */ 2798c2ecf20Sopenharmony_cistatic const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = { 2808c2ecf20Sopenharmony_ci /* SMB2_NEGOTIATE */ true, 2818c2ecf20Sopenharmony_ci /* SMB2_SESSION_SETUP */ true, 2828c2ecf20Sopenharmony_ci /* SMB2_LOGOFF */ false, 2838c2ecf20Sopenharmony_ci /* SMB2_TREE_CONNECT */ false, 2848c2ecf20Sopenharmony_ci /* SMB2_TREE_DISCONNECT */ false, 2858c2ecf20Sopenharmony_ci /* SMB2_CREATE */ true, 2868c2ecf20Sopenharmony_ci /* SMB2_CLOSE */ false, 2878c2ecf20Sopenharmony_ci /* SMB2_FLUSH */ false, 2888c2ecf20Sopenharmony_ci /* SMB2_READ */ true, 2898c2ecf20Sopenharmony_ci /* SMB2_WRITE */ false, 2908c2ecf20Sopenharmony_ci /* SMB2_LOCK */ false, 2918c2ecf20Sopenharmony_ci /* SMB2_IOCTL */ true, 2928c2ecf20Sopenharmony_ci /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */ 2938c2ecf20Sopenharmony_ci /* SMB2_ECHO */ false, 2948c2ecf20Sopenharmony_ci /* SMB2_QUERY_DIRECTORY */ true, 2958c2ecf20Sopenharmony_ci /* SMB2_CHANGE_NOTIFY */ true, 2968c2ecf20Sopenharmony_ci /* SMB2_QUERY_INFO */ true, 2978c2ecf20Sopenharmony_ci /* SMB2_SET_INFO */ false, 2988c2ecf20Sopenharmony_ci /* SMB2_OPLOCK_BREAK */ false 2998c2ecf20Sopenharmony_ci}; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci/* 3028c2ecf20Sopenharmony_ci * Returns the pointer to the beginning of the data area. Length of the data 3038c2ecf20Sopenharmony_ci * area and the offset to it (from the beginning of the smb are also returned. 3048c2ecf20Sopenharmony_ci */ 3058c2ecf20Sopenharmony_cichar * 3068c2ecf20Sopenharmony_cismb2_get_data_area_len(int *off, int *len, struct smb2_sync_hdr *shdr) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci const int max_off = 4096; 3098c2ecf20Sopenharmony_ci const int max_len = 128 * 1024; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci *off = 0; 3128c2ecf20Sopenharmony_ci *len = 0; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci /* error responses do not have data area */ 3158c2ecf20Sopenharmony_ci if (shdr->Status && shdr->Status != STATUS_MORE_PROCESSING_REQUIRED && 3168c2ecf20Sopenharmony_ci (((struct smb2_err_rsp *)shdr)->StructureSize) == 3178c2ecf20Sopenharmony_ci SMB2_ERROR_STRUCTURE_SIZE2) 3188c2ecf20Sopenharmony_ci return NULL; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci /* 3218c2ecf20Sopenharmony_ci * Following commands have data areas so we have to get the location 3228c2ecf20Sopenharmony_ci * of the data buffer offset and data buffer length for the particular 3238c2ecf20Sopenharmony_ci * command. 3248c2ecf20Sopenharmony_ci */ 3258c2ecf20Sopenharmony_ci switch (shdr->Command) { 3268c2ecf20Sopenharmony_ci case SMB2_NEGOTIATE: 3278c2ecf20Sopenharmony_ci *off = le16_to_cpu( 3288c2ecf20Sopenharmony_ci ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferOffset); 3298c2ecf20Sopenharmony_ci *len = le16_to_cpu( 3308c2ecf20Sopenharmony_ci ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferLength); 3318c2ecf20Sopenharmony_ci break; 3328c2ecf20Sopenharmony_ci case SMB2_SESSION_SETUP: 3338c2ecf20Sopenharmony_ci *off = le16_to_cpu( 3348c2ecf20Sopenharmony_ci ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferOffset); 3358c2ecf20Sopenharmony_ci *len = le16_to_cpu( 3368c2ecf20Sopenharmony_ci ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferLength); 3378c2ecf20Sopenharmony_ci break; 3388c2ecf20Sopenharmony_ci case SMB2_CREATE: 3398c2ecf20Sopenharmony_ci *off = le32_to_cpu( 3408c2ecf20Sopenharmony_ci ((struct smb2_create_rsp *)shdr)->CreateContextsOffset); 3418c2ecf20Sopenharmony_ci *len = le32_to_cpu( 3428c2ecf20Sopenharmony_ci ((struct smb2_create_rsp *)shdr)->CreateContextsLength); 3438c2ecf20Sopenharmony_ci break; 3448c2ecf20Sopenharmony_ci case SMB2_QUERY_INFO: 3458c2ecf20Sopenharmony_ci *off = le16_to_cpu( 3468c2ecf20Sopenharmony_ci ((struct smb2_query_info_rsp *)shdr)->OutputBufferOffset); 3478c2ecf20Sopenharmony_ci *len = le32_to_cpu( 3488c2ecf20Sopenharmony_ci ((struct smb2_query_info_rsp *)shdr)->OutputBufferLength); 3498c2ecf20Sopenharmony_ci break; 3508c2ecf20Sopenharmony_ci case SMB2_READ: 3518c2ecf20Sopenharmony_ci /* TODO: is this a bug ? */ 3528c2ecf20Sopenharmony_ci *off = ((struct smb2_read_rsp *)shdr)->DataOffset; 3538c2ecf20Sopenharmony_ci *len = le32_to_cpu(((struct smb2_read_rsp *)shdr)->DataLength); 3548c2ecf20Sopenharmony_ci break; 3558c2ecf20Sopenharmony_ci case SMB2_QUERY_DIRECTORY: 3568c2ecf20Sopenharmony_ci *off = le16_to_cpu( 3578c2ecf20Sopenharmony_ci ((struct smb2_query_directory_rsp *)shdr)->OutputBufferOffset); 3588c2ecf20Sopenharmony_ci *len = le32_to_cpu( 3598c2ecf20Sopenharmony_ci ((struct smb2_query_directory_rsp *)shdr)->OutputBufferLength); 3608c2ecf20Sopenharmony_ci break; 3618c2ecf20Sopenharmony_ci case SMB2_IOCTL: 3628c2ecf20Sopenharmony_ci *off = le32_to_cpu( 3638c2ecf20Sopenharmony_ci ((struct smb2_ioctl_rsp *)shdr)->OutputOffset); 3648c2ecf20Sopenharmony_ci *len = le32_to_cpu( 3658c2ecf20Sopenharmony_ci ((struct smb2_ioctl_rsp *)shdr)->OutputCount); 3668c2ecf20Sopenharmony_ci break; 3678c2ecf20Sopenharmony_ci case SMB2_CHANGE_NOTIFY: 3688c2ecf20Sopenharmony_ci *off = le16_to_cpu( 3698c2ecf20Sopenharmony_ci ((struct smb2_change_notify_rsp *)shdr)->OutputBufferOffset); 3708c2ecf20Sopenharmony_ci *len = le32_to_cpu( 3718c2ecf20Sopenharmony_ci ((struct smb2_change_notify_rsp *)shdr)->OutputBufferLength); 3728c2ecf20Sopenharmony_ci break; 3738c2ecf20Sopenharmony_ci default: 3748c2ecf20Sopenharmony_ci cifs_dbg(VFS, "no length check for command %d\n", le16_to_cpu(shdr->Command)); 3758c2ecf20Sopenharmony_ci break; 3768c2ecf20Sopenharmony_ci } 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci /* 3798c2ecf20Sopenharmony_ci * Invalid length or offset probably means data area is invalid, but 3808c2ecf20Sopenharmony_ci * we have little choice but to ignore the data area in this case. 3818c2ecf20Sopenharmony_ci */ 3828c2ecf20Sopenharmony_ci if (unlikely(*off < 0 || *off > max_off || 3838c2ecf20Sopenharmony_ci *len < 0 || *len > max_len)) { 3848c2ecf20Sopenharmony_ci cifs_dbg(VFS, "%s: invalid data area (off=%d len=%d)\n", 3858c2ecf20Sopenharmony_ci __func__, *off, *len); 3868c2ecf20Sopenharmony_ci *off = 0; 3878c2ecf20Sopenharmony_ci *len = 0; 3888c2ecf20Sopenharmony_ci } else if (*off == 0) { 3898c2ecf20Sopenharmony_ci *len = 0; 3908c2ecf20Sopenharmony_ci } 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci /* return pointer to beginning of data area, ie offset from SMB start */ 3938c2ecf20Sopenharmony_ci if (*off > 0 && *len > 0) 3948c2ecf20Sopenharmony_ci return (char *)shdr + *off; 3958c2ecf20Sopenharmony_ci return NULL; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci/* 3998c2ecf20Sopenharmony_ci * Calculate the size of the SMB message based on the fixed header 4008c2ecf20Sopenharmony_ci * portion, the number of word parameters and the data portion of the message. 4018c2ecf20Sopenharmony_ci */ 4028c2ecf20Sopenharmony_ciunsigned int 4038c2ecf20Sopenharmony_cismb2_calc_size(void *buf, struct TCP_Server_Info *srvr) 4048c2ecf20Sopenharmony_ci{ 4058c2ecf20Sopenharmony_ci struct smb2_sync_pdu *pdu = (struct smb2_sync_pdu *)buf; 4068c2ecf20Sopenharmony_ci struct smb2_sync_hdr *shdr = &pdu->sync_hdr; 4078c2ecf20Sopenharmony_ci int offset; /* the offset from the beginning of SMB to data area */ 4088c2ecf20Sopenharmony_ci int data_length; /* the length of the variable length data area */ 4098c2ecf20Sopenharmony_ci /* Structure Size has already been checked to make sure it is 64 */ 4108c2ecf20Sopenharmony_ci int len = le16_to_cpu(shdr->StructureSize); 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci /* 4138c2ecf20Sopenharmony_ci * StructureSize2, ie length of fixed parameter area has already 4148c2ecf20Sopenharmony_ci * been checked to make sure it is the correct length. 4158c2ecf20Sopenharmony_ci */ 4168c2ecf20Sopenharmony_ci len += le16_to_cpu(pdu->StructureSize2); 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci if (has_smb2_data_area[le16_to_cpu(shdr->Command)] == false) 4198c2ecf20Sopenharmony_ci goto calc_size_exit; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci smb2_get_data_area_len(&offset, &data_length, shdr); 4228c2ecf20Sopenharmony_ci cifs_dbg(FYI, "SMB2 data length %d offset %d\n", data_length, offset); 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci if (data_length > 0) { 4258c2ecf20Sopenharmony_ci /* 4268c2ecf20Sopenharmony_ci * Check to make sure that data area begins after fixed area, 4278c2ecf20Sopenharmony_ci * Note that last byte of the fixed area is part of data area 4288c2ecf20Sopenharmony_ci * for some commands, typically those with odd StructureSize, 4298c2ecf20Sopenharmony_ci * so we must add one to the calculation. 4308c2ecf20Sopenharmony_ci */ 4318c2ecf20Sopenharmony_ci if (offset + 1 < len) { 4328c2ecf20Sopenharmony_ci cifs_dbg(VFS, "data area offset %d overlaps SMB2 header %d\n", 4338c2ecf20Sopenharmony_ci offset + 1, len); 4348c2ecf20Sopenharmony_ci data_length = 0; 4358c2ecf20Sopenharmony_ci } else { 4368c2ecf20Sopenharmony_ci len = offset + data_length; 4378c2ecf20Sopenharmony_ci } 4388c2ecf20Sopenharmony_ci } 4398c2ecf20Sopenharmony_cicalc_size_exit: 4408c2ecf20Sopenharmony_ci cifs_dbg(FYI, "SMB2 len %d\n", len); 4418c2ecf20Sopenharmony_ci return len; 4428c2ecf20Sopenharmony_ci} 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci/* Note: caller must free return buffer */ 4458c2ecf20Sopenharmony_ci__le16 * 4468c2ecf20Sopenharmony_cicifs_convert_path_to_utf16(const char *from, struct cifs_sb_info *cifs_sb) 4478c2ecf20Sopenharmony_ci{ 4488c2ecf20Sopenharmony_ci int len; 4498c2ecf20Sopenharmony_ci const char *start_of_path; 4508c2ecf20Sopenharmony_ci __le16 *to; 4518c2ecf20Sopenharmony_ci int map_type; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR) 4548c2ecf20Sopenharmony_ci map_type = SFM_MAP_UNI_RSVD; 4558c2ecf20Sopenharmony_ci else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR) 4568c2ecf20Sopenharmony_ci map_type = SFU_MAP_UNI_RSVD; 4578c2ecf20Sopenharmony_ci else 4588c2ecf20Sopenharmony_ci map_type = NO_MAP_UNI_RSVD; 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci /* Windows doesn't allow paths beginning with \ */ 4618c2ecf20Sopenharmony_ci if (from[0] == '\\') 4628c2ecf20Sopenharmony_ci start_of_path = from + 1; 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci /* SMB311 POSIX extensions paths do not include leading slash */ 4658c2ecf20Sopenharmony_ci else if (cifs_sb_master_tlink(cifs_sb) && 4668c2ecf20Sopenharmony_ci cifs_sb_master_tcon(cifs_sb)->posix_extensions && 4678c2ecf20Sopenharmony_ci (from[0] == '/')) { 4688c2ecf20Sopenharmony_ci start_of_path = from + 1; 4698c2ecf20Sopenharmony_ci } else 4708c2ecf20Sopenharmony_ci start_of_path = from; 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci to = cifs_strndup_to_utf16(start_of_path, PATH_MAX, &len, 4738c2ecf20Sopenharmony_ci cifs_sb->local_nls, map_type); 4748c2ecf20Sopenharmony_ci return to; 4758c2ecf20Sopenharmony_ci} 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci__le32 4788c2ecf20Sopenharmony_cismb2_get_lease_state(struct cifsInodeInfo *cinode) 4798c2ecf20Sopenharmony_ci{ 4808c2ecf20Sopenharmony_ci __le32 lease = 0; 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci if (CIFS_CACHE_WRITE(cinode)) 4838c2ecf20Sopenharmony_ci lease |= SMB2_LEASE_WRITE_CACHING; 4848c2ecf20Sopenharmony_ci if (CIFS_CACHE_HANDLE(cinode)) 4858c2ecf20Sopenharmony_ci lease |= SMB2_LEASE_HANDLE_CACHING; 4868c2ecf20Sopenharmony_ci if (CIFS_CACHE_READ(cinode)) 4878c2ecf20Sopenharmony_ci lease |= SMB2_LEASE_READ_CACHING; 4888c2ecf20Sopenharmony_ci return lease; 4898c2ecf20Sopenharmony_ci} 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_cistruct smb2_lease_break_work { 4928c2ecf20Sopenharmony_ci struct work_struct lease_break; 4938c2ecf20Sopenharmony_ci struct tcon_link *tlink; 4948c2ecf20Sopenharmony_ci __u8 lease_key[16]; 4958c2ecf20Sopenharmony_ci __le32 lease_state; 4968c2ecf20Sopenharmony_ci}; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_cistatic void 4998c2ecf20Sopenharmony_cicifs_ses_oplock_break(struct work_struct *work) 5008c2ecf20Sopenharmony_ci{ 5018c2ecf20Sopenharmony_ci struct smb2_lease_break_work *lw = container_of(work, 5028c2ecf20Sopenharmony_ci struct smb2_lease_break_work, lease_break); 5038c2ecf20Sopenharmony_ci int rc = 0; 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci rc = SMB2_lease_break(0, tlink_tcon(lw->tlink), lw->lease_key, 5068c2ecf20Sopenharmony_ci lw->lease_state); 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci cifs_dbg(FYI, "Lease release rc %d\n", rc); 5098c2ecf20Sopenharmony_ci cifs_put_tlink(lw->tlink); 5108c2ecf20Sopenharmony_ci kfree(lw); 5118c2ecf20Sopenharmony_ci} 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_cistatic void 5148c2ecf20Sopenharmony_cismb2_queue_pending_open_break(struct tcon_link *tlink, __u8 *lease_key, 5158c2ecf20Sopenharmony_ci __le32 new_lease_state) 5168c2ecf20Sopenharmony_ci{ 5178c2ecf20Sopenharmony_ci struct smb2_lease_break_work *lw; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci lw = kmalloc(sizeof(struct smb2_lease_break_work), GFP_KERNEL); 5208c2ecf20Sopenharmony_ci if (!lw) { 5218c2ecf20Sopenharmony_ci cifs_put_tlink(tlink); 5228c2ecf20Sopenharmony_ci return; 5238c2ecf20Sopenharmony_ci } 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci INIT_WORK(&lw->lease_break, cifs_ses_oplock_break); 5268c2ecf20Sopenharmony_ci lw->tlink = tlink; 5278c2ecf20Sopenharmony_ci lw->lease_state = new_lease_state; 5288c2ecf20Sopenharmony_ci memcpy(lw->lease_key, lease_key, SMB2_LEASE_KEY_SIZE); 5298c2ecf20Sopenharmony_ci queue_work(cifsiod_wq, &lw->lease_break); 5308c2ecf20Sopenharmony_ci} 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_cistatic bool 5338c2ecf20Sopenharmony_cismb2_tcon_has_lease(struct cifs_tcon *tcon, struct smb2_lease_break *rsp) 5348c2ecf20Sopenharmony_ci{ 5358c2ecf20Sopenharmony_ci __u8 lease_state; 5368c2ecf20Sopenharmony_ci struct list_head *tmp; 5378c2ecf20Sopenharmony_ci struct cifsFileInfo *cfile; 5388c2ecf20Sopenharmony_ci struct cifsInodeInfo *cinode; 5398c2ecf20Sopenharmony_ci int ack_req = le32_to_cpu(rsp->Flags & 5408c2ecf20Sopenharmony_ci SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED); 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci lease_state = le32_to_cpu(rsp->NewLeaseState); 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci list_for_each(tmp, &tcon->openFileList) { 5458c2ecf20Sopenharmony_ci cfile = list_entry(tmp, struct cifsFileInfo, tlist); 5468c2ecf20Sopenharmony_ci cinode = CIFS_I(d_inode(cfile->dentry)); 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci if (memcmp(cinode->lease_key, rsp->LeaseKey, 5498c2ecf20Sopenharmony_ci SMB2_LEASE_KEY_SIZE)) 5508c2ecf20Sopenharmony_ci continue; 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci cifs_dbg(FYI, "found in the open list\n"); 5538c2ecf20Sopenharmony_ci cifs_dbg(FYI, "lease key match, lease break 0x%x\n", 5548c2ecf20Sopenharmony_ci lease_state); 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci if (ack_req) 5578c2ecf20Sopenharmony_ci cfile->oplock_break_cancelled = false; 5588c2ecf20Sopenharmony_ci else 5598c2ecf20Sopenharmony_ci cfile->oplock_break_cancelled = true; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags); 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci cfile->oplock_epoch = le16_to_cpu(rsp->Epoch); 5648c2ecf20Sopenharmony_ci cfile->oplock_level = lease_state; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci cifs_queue_oplock_break(cfile); 5678c2ecf20Sopenharmony_ci return true; 5688c2ecf20Sopenharmony_ci } 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci return false; 5718c2ecf20Sopenharmony_ci} 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_cistatic struct cifs_pending_open * 5748c2ecf20Sopenharmony_cismb2_tcon_find_pending_open_lease(struct cifs_tcon *tcon, 5758c2ecf20Sopenharmony_ci struct smb2_lease_break *rsp) 5768c2ecf20Sopenharmony_ci{ 5778c2ecf20Sopenharmony_ci __u8 lease_state = le32_to_cpu(rsp->NewLeaseState); 5788c2ecf20Sopenharmony_ci int ack_req = le32_to_cpu(rsp->Flags & 5798c2ecf20Sopenharmony_ci SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED); 5808c2ecf20Sopenharmony_ci struct cifs_pending_open *open; 5818c2ecf20Sopenharmony_ci struct cifs_pending_open *found = NULL; 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci list_for_each_entry(open, &tcon->pending_opens, olist) { 5848c2ecf20Sopenharmony_ci if (memcmp(open->lease_key, rsp->LeaseKey, 5858c2ecf20Sopenharmony_ci SMB2_LEASE_KEY_SIZE)) 5868c2ecf20Sopenharmony_ci continue; 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci if (!found && ack_req) { 5898c2ecf20Sopenharmony_ci found = open; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci cifs_dbg(FYI, "found in the pending open list\n"); 5938c2ecf20Sopenharmony_ci cifs_dbg(FYI, "lease key match, lease break 0x%x\n", 5948c2ecf20Sopenharmony_ci lease_state); 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci open->oplock = lease_state; 5978c2ecf20Sopenharmony_ci } 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci return found; 6008c2ecf20Sopenharmony_ci} 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_cistatic bool 6038c2ecf20Sopenharmony_cismb2_is_valid_lease_break(char *buffer) 6048c2ecf20Sopenharmony_ci{ 6058c2ecf20Sopenharmony_ci struct smb2_lease_break *rsp = (struct smb2_lease_break *)buffer; 6068c2ecf20Sopenharmony_ci struct list_head *tmp, *tmp1, *tmp2; 6078c2ecf20Sopenharmony_ci struct TCP_Server_Info *server; 6088c2ecf20Sopenharmony_ci struct cifs_ses *ses; 6098c2ecf20Sopenharmony_ci struct cifs_tcon *tcon; 6108c2ecf20Sopenharmony_ci struct cifs_pending_open *open; 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci cifs_dbg(FYI, "Checking for lease break\n"); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci /* look up tcon based on tid & uid */ 6158c2ecf20Sopenharmony_ci spin_lock(&cifs_tcp_ses_lock); 6168c2ecf20Sopenharmony_ci list_for_each(tmp, &cifs_tcp_ses_list) { 6178c2ecf20Sopenharmony_ci server = list_entry(tmp, struct TCP_Server_Info, tcp_ses_list); 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci list_for_each(tmp1, &server->smb_ses_list) { 6208c2ecf20Sopenharmony_ci ses = list_entry(tmp1, struct cifs_ses, smb_ses_list); 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci list_for_each(tmp2, &ses->tcon_list) { 6238c2ecf20Sopenharmony_ci tcon = list_entry(tmp2, struct cifs_tcon, 6248c2ecf20Sopenharmony_ci tcon_list); 6258c2ecf20Sopenharmony_ci spin_lock(&tcon->open_file_lock); 6268c2ecf20Sopenharmony_ci cifs_stats_inc( 6278c2ecf20Sopenharmony_ci &tcon->stats.cifs_stats.num_oplock_brks); 6288c2ecf20Sopenharmony_ci if (smb2_tcon_has_lease(tcon, rsp)) { 6298c2ecf20Sopenharmony_ci spin_unlock(&tcon->open_file_lock); 6308c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 6318c2ecf20Sopenharmony_ci return true; 6328c2ecf20Sopenharmony_ci } 6338c2ecf20Sopenharmony_ci open = smb2_tcon_find_pending_open_lease(tcon, 6348c2ecf20Sopenharmony_ci rsp); 6358c2ecf20Sopenharmony_ci if (open) { 6368c2ecf20Sopenharmony_ci __u8 lease_key[SMB2_LEASE_KEY_SIZE]; 6378c2ecf20Sopenharmony_ci struct tcon_link *tlink; 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci tlink = cifs_get_tlink(open->tlink); 6408c2ecf20Sopenharmony_ci memcpy(lease_key, open->lease_key, 6418c2ecf20Sopenharmony_ci SMB2_LEASE_KEY_SIZE); 6428c2ecf20Sopenharmony_ci spin_unlock(&tcon->open_file_lock); 6438c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 6448c2ecf20Sopenharmony_ci smb2_queue_pending_open_break(tlink, 6458c2ecf20Sopenharmony_ci lease_key, 6468c2ecf20Sopenharmony_ci rsp->NewLeaseState); 6478c2ecf20Sopenharmony_ci return true; 6488c2ecf20Sopenharmony_ci } 6498c2ecf20Sopenharmony_ci spin_unlock(&tcon->open_file_lock); 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci if (tcon->crfid.is_valid && 6528c2ecf20Sopenharmony_ci !memcmp(rsp->LeaseKey, 6538c2ecf20Sopenharmony_ci tcon->crfid.fid->lease_key, 6548c2ecf20Sopenharmony_ci SMB2_LEASE_KEY_SIZE)) { 6558c2ecf20Sopenharmony_ci INIT_WORK(&tcon->crfid.lease_break, 6568c2ecf20Sopenharmony_ci smb2_cached_lease_break); 6578c2ecf20Sopenharmony_ci queue_work(cifsiod_wq, 6588c2ecf20Sopenharmony_ci &tcon->crfid.lease_break); 6598c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 6608c2ecf20Sopenharmony_ci return true; 6618c2ecf20Sopenharmony_ci } 6628c2ecf20Sopenharmony_ci } 6638c2ecf20Sopenharmony_ci } 6648c2ecf20Sopenharmony_ci } 6658c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 6668c2ecf20Sopenharmony_ci cifs_dbg(FYI, "Can not process lease break - no lease matched\n"); 6678c2ecf20Sopenharmony_ci return false; 6688c2ecf20Sopenharmony_ci} 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_cibool 6718c2ecf20Sopenharmony_cismb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server) 6728c2ecf20Sopenharmony_ci{ 6738c2ecf20Sopenharmony_ci struct smb2_oplock_break *rsp = (struct smb2_oplock_break *)buffer; 6748c2ecf20Sopenharmony_ci struct list_head *tmp, *tmp1, *tmp2; 6758c2ecf20Sopenharmony_ci struct cifs_ses *ses; 6768c2ecf20Sopenharmony_ci struct cifs_tcon *tcon; 6778c2ecf20Sopenharmony_ci struct cifsInodeInfo *cinode; 6788c2ecf20Sopenharmony_ci struct cifsFileInfo *cfile; 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci cifs_dbg(FYI, "Checking for oplock break\n"); 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci if (rsp->sync_hdr.Command != SMB2_OPLOCK_BREAK) 6838c2ecf20Sopenharmony_ci return false; 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci if (rsp->StructureSize != 6868c2ecf20Sopenharmony_ci smb2_rsp_struct_sizes[SMB2_OPLOCK_BREAK_HE]) { 6878c2ecf20Sopenharmony_ci if (le16_to_cpu(rsp->StructureSize) == 44) 6888c2ecf20Sopenharmony_ci return smb2_is_valid_lease_break(buffer); 6898c2ecf20Sopenharmony_ci else 6908c2ecf20Sopenharmony_ci return false; 6918c2ecf20Sopenharmony_ci } 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci cifs_dbg(FYI, "oplock level 0x%x\n", rsp->OplockLevel); 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci /* look up tcon based on tid & uid */ 6968c2ecf20Sopenharmony_ci spin_lock(&cifs_tcp_ses_lock); 6978c2ecf20Sopenharmony_ci list_for_each(tmp, &server->smb_ses_list) { 6988c2ecf20Sopenharmony_ci ses = list_entry(tmp, struct cifs_ses, smb_ses_list); 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci list_for_each(tmp1, &ses->tcon_list) { 7018c2ecf20Sopenharmony_ci tcon = list_entry(tmp1, struct cifs_tcon, tcon_list); 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci spin_lock(&tcon->open_file_lock); 7048c2ecf20Sopenharmony_ci list_for_each(tmp2, &tcon->openFileList) { 7058c2ecf20Sopenharmony_ci cfile = list_entry(tmp2, struct cifsFileInfo, 7068c2ecf20Sopenharmony_ci tlist); 7078c2ecf20Sopenharmony_ci if (rsp->PersistentFid != 7088c2ecf20Sopenharmony_ci cfile->fid.persistent_fid || 7098c2ecf20Sopenharmony_ci rsp->VolatileFid != 7108c2ecf20Sopenharmony_ci cfile->fid.volatile_fid) 7118c2ecf20Sopenharmony_ci continue; 7128c2ecf20Sopenharmony_ci 7138c2ecf20Sopenharmony_ci cifs_dbg(FYI, "file id match, oplock break\n"); 7148c2ecf20Sopenharmony_ci cifs_stats_inc( 7158c2ecf20Sopenharmony_ci &tcon->stats.cifs_stats.num_oplock_brks); 7168c2ecf20Sopenharmony_ci cinode = CIFS_I(d_inode(cfile->dentry)); 7178c2ecf20Sopenharmony_ci spin_lock(&cfile->file_info_lock); 7188c2ecf20Sopenharmony_ci if (!CIFS_CACHE_WRITE(cinode) && 7198c2ecf20Sopenharmony_ci rsp->OplockLevel == SMB2_OPLOCK_LEVEL_NONE) 7208c2ecf20Sopenharmony_ci cfile->oplock_break_cancelled = true; 7218c2ecf20Sopenharmony_ci else 7228c2ecf20Sopenharmony_ci cfile->oplock_break_cancelled = false; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, 7258c2ecf20Sopenharmony_ci &cinode->flags); 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci cfile->oplock_epoch = 0; 7288c2ecf20Sopenharmony_ci cfile->oplock_level = rsp->OplockLevel; 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci spin_unlock(&cfile->file_info_lock); 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci cifs_queue_oplock_break(cfile); 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci spin_unlock(&tcon->open_file_lock); 7358c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 7368c2ecf20Sopenharmony_ci return true; 7378c2ecf20Sopenharmony_ci } 7388c2ecf20Sopenharmony_ci spin_unlock(&tcon->open_file_lock); 7398c2ecf20Sopenharmony_ci } 7408c2ecf20Sopenharmony_ci } 7418c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 7428c2ecf20Sopenharmony_ci cifs_dbg(FYI, "No file id matched, oplock break ignored\n"); 7438c2ecf20Sopenharmony_ci return true; 7448c2ecf20Sopenharmony_ci} 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_civoid 7478c2ecf20Sopenharmony_cismb2_cancelled_close_fid(struct work_struct *work) 7488c2ecf20Sopenharmony_ci{ 7498c2ecf20Sopenharmony_ci struct close_cancelled_open *cancelled = container_of(work, 7508c2ecf20Sopenharmony_ci struct close_cancelled_open, work); 7518c2ecf20Sopenharmony_ci struct cifs_tcon *tcon = cancelled->tcon; 7528c2ecf20Sopenharmony_ci int rc; 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci if (cancelled->mid) 7558c2ecf20Sopenharmony_ci cifs_tcon_dbg(VFS, "Close unmatched open for MID:%llx\n", 7568c2ecf20Sopenharmony_ci cancelled->mid); 7578c2ecf20Sopenharmony_ci else 7588c2ecf20Sopenharmony_ci cifs_tcon_dbg(VFS, "Close interrupted close\n"); 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci rc = SMB2_close(0, tcon, cancelled->fid.persistent_fid, 7618c2ecf20Sopenharmony_ci cancelled->fid.volatile_fid); 7628c2ecf20Sopenharmony_ci if (rc) 7638c2ecf20Sopenharmony_ci cifs_tcon_dbg(VFS, "Close cancelled mid failed rc:%d\n", rc); 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ci cifs_put_tcon(tcon); 7668c2ecf20Sopenharmony_ci kfree(cancelled); 7678c2ecf20Sopenharmony_ci} 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci/* 7708c2ecf20Sopenharmony_ci * Caller should already has an extra reference to @tcon 7718c2ecf20Sopenharmony_ci * This function is used to queue work to close a handle to prevent leaks 7728c2ecf20Sopenharmony_ci * on the server. 7738c2ecf20Sopenharmony_ci * We handle two cases. If an open was interrupted after we sent the 7748c2ecf20Sopenharmony_ci * SMB2_CREATE to the server but before we processed the reply, and second 7758c2ecf20Sopenharmony_ci * if a close was interrupted before we sent the SMB2_CLOSE to the server. 7768c2ecf20Sopenharmony_ci */ 7778c2ecf20Sopenharmony_cistatic int 7788c2ecf20Sopenharmony_ci__smb2_handle_cancelled_cmd(struct cifs_tcon *tcon, __u16 cmd, __u64 mid, 7798c2ecf20Sopenharmony_ci __u64 persistent_fid, __u64 volatile_fid) 7808c2ecf20Sopenharmony_ci{ 7818c2ecf20Sopenharmony_ci struct close_cancelled_open *cancelled; 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_ci cancelled = kzalloc(sizeof(*cancelled), GFP_ATOMIC); 7848c2ecf20Sopenharmony_ci if (!cancelled) 7858c2ecf20Sopenharmony_ci return -ENOMEM; 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci cancelled->fid.persistent_fid = persistent_fid; 7888c2ecf20Sopenharmony_ci cancelled->fid.volatile_fid = volatile_fid; 7898c2ecf20Sopenharmony_ci cancelled->tcon = tcon; 7908c2ecf20Sopenharmony_ci cancelled->cmd = cmd; 7918c2ecf20Sopenharmony_ci cancelled->mid = mid; 7928c2ecf20Sopenharmony_ci INIT_WORK(&cancelled->work, smb2_cancelled_close_fid); 7938c2ecf20Sopenharmony_ci WARN_ON(queue_work(cifsiod_wq, &cancelled->work) == false); 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci return 0; 7968c2ecf20Sopenharmony_ci} 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ciint 7998c2ecf20Sopenharmony_cismb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid, 8008c2ecf20Sopenharmony_ci __u64 volatile_fid) 8018c2ecf20Sopenharmony_ci{ 8028c2ecf20Sopenharmony_ci int rc; 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count); 8058c2ecf20Sopenharmony_ci spin_lock(&cifs_tcp_ses_lock); 8068c2ecf20Sopenharmony_ci if (tcon->tc_count <= 0) { 8078c2ecf20Sopenharmony_ci struct TCP_Server_Info *server = NULL; 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci WARN_ONCE(tcon->tc_count < 0, "tcon refcount is negative"); 8108c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci if (tcon->ses) 8138c2ecf20Sopenharmony_ci server = tcon->ses->server; 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci cifs_server_dbg(FYI, "tid=%u: tcon is closing, skipping async close retry of fid %llu %llu\n", 8168c2ecf20Sopenharmony_ci tcon->tid, persistent_fid, volatile_fid); 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci return 0; 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci tcon->tc_count++; 8218c2ecf20Sopenharmony_ci spin_unlock(&cifs_tcp_ses_lock); 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci rc = __smb2_handle_cancelled_cmd(tcon, SMB2_CLOSE_HE, 0, 8248c2ecf20Sopenharmony_ci persistent_fid, volatile_fid); 8258c2ecf20Sopenharmony_ci if (rc) 8268c2ecf20Sopenharmony_ci cifs_put_tcon(tcon); 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci return rc; 8298c2ecf20Sopenharmony_ci} 8308c2ecf20Sopenharmony_ci 8318c2ecf20Sopenharmony_ciint 8328c2ecf20Sopenharmony_cismb2_handle_cancelled_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server) 8338c2ecf20Sopenharmony_ci{ 8348c2ecf20Sopenharmony_ci struct smb2_sync_hdr *sync_hdr = mid->resp_buf; 8358c2ecf20Sopenharmony_ci struct smb2_create_rsp *rsp = mid->resp_buf; 8368c2ecf20Sopenharmony_ci struct cifs_tcon *tcon; 8378c2ecf20Sopenharmony_ci int rc; 8388c2ecf20Sopenharmony_ci 8398c2ecf20Sopenharmony_ci if ((mid->optype & CIFS_CP_CREATE_CLOSE_OP) || sync_hdr->Command != SMB2_CREATE || 8408c2ecf20Sopenharmony_ci sync_hdr->Status != STATUS_SUCCESS) 8418c2ecf20Sopenharmony_ci return 0; 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci tcon = smb2_find_smb_tcon(server, sync_hdr->SessionId, 8448c2ecf20Sopenharmony_ci sync_hdr->TreeId); 8458c2ecf20Sopenharmony_ci if (!tcon) 8468c2ecf20Sopenharmony_ci return -ENOENT; 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci rc = __smb2_handle_cancelled_cmd(tcon, 8498c2ecf20Sopenharmony_ci le16_to_cpu(sync_hdr->Command), 8508c2ecf20Sopenharmony_ci le64_to_cpu(sync_hdr->MessageId), 8518c2ecf20Sopenharmony_ci rsp->PersistentFileId, 8528c2ecf20Sopenharmony_ci rsp->VolatileFileId); 8538c2ecf20Sopenharmony_ci if (rc) 8548c2ecf20Sopenharmony_ci cifs_put_tcon(tcon); 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_ci return rc; 8578c2ecf20Sopenharmony_ci} 8588c2ecf20Sopenharmony_ci 8598c2ecf20Sopenharmony_ci/** 8608c2ecf20Sopenharmony_ci * smb311_update_preauth_hash - update @ses hash with the packet data in @iov 8618c2ecf20Sopenharmony_ci * 8628c2ecf20Sopenharmony_ci * Assumes @iov does not contain the rfc1002 length and iov[0] has the 8638c2ecf20Sopenharmony_ci * SMB2 header. 8648c2ecf20Sopenharmony_ci */ 8658c2ecf20Sopenharmony_ciint 8668c2ecf20Sopenharmony_cismb311_update_preauth_hash(struct cifs_ses *ses, struct kvec *iov, int nvec) 8678c2ecf20Sopenharmony_ci{ 8688c2ecf20Sopenharmony_ci int i, rc; 8698c2ecf20Sopenharmony_ci struct sdesc *d; 8708c2ecf20Sopenharmony_ci struct smb2_sync_hdr *hdr; 8718c2ecf20Sopenharmony_ci struct TCP_Server_Info *server = cifs_ses_server(ses); 8728c2ecf20Sopenharmony_ci 8738c2ecf20Sopenharmony_ci hdr = (struct smb2_sync_hdr *)iov[0].iov_base; 8748c2ecf20Sopenharmony_ci /* neg prot are always taken */ 8758c2ecf20Sopenharmony_ci if (hdr->Command == SMB2_NEGOTIATE) 8768c2ecf20Sopenharmony_ci goto ok; 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci /* 8798c2ecf20Sopenharmony_ci * If we process a command which wasn't a negprot it means the 8808c2ecf20Sopenharmony_ci * neg prot was already done, so the server dialect was set 8818c2ecf20Sopenharmony_ci * and we can test it. Preauth requires 3.1.1 for now. 8828c2ecf20Sopenharmony_ci */ 8838c2ecf20Sopenharmony_ci if (server->dialect != SMB311_PROT_ID) 8848c2ecf20Sopenharmony_ci return 0; 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci if (hdr->Command != SMB2_SESSION_SETUP) 8878c2ecf20Sopenharmony_ci return 0; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci /* skip last sess setup response */ 8908c2ecf20Sopenharmony_ci if ((hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) 8918c2ecf20Sopenharmony_ci && (hdr->Status == NT_STATUS_OK 8928c2ecf20Sopenharmony_ci || (hdr->Status != 8938c2ecf20Sopenharmony_ci cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED)))) 8948c2ecf20Sopenharmony_ci return 0; 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ciok: 8978c2ecf20Sopenharmony_ci rc = smb311_crypto_shash_allocate(server); 8988c2ecf20Sopenharmony_ci if (rc) 8998c2ecf20Sopenharmony_ci return rc; 9008c2ecf20Sopenharmony_ci 9018c2ecf20Sopenharmony_ci d = server->secmech.sdescsha512; 9028c2ecf20Sopenharmony_ci rc = crypto_shash_init(&d->shash); 9038c2ecf20Sopenharmony_ci if (rc) { 9048c2ecf20Sopenharmony_ci cifs_dbg(VFS, "%s: Could not init sha512 shash\n", __func__); 9058c2ecf20Sopenharmony_ci return rc; 9068c2ecf20Sopenharmony_ci } 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci rc = crypto_shash_update(&d->shash, ses->preauth_sha_hash, 9098c2ecf20Sopenharmony_ci SMB2_PREAUTH_HASH_SIZE); 9108c2ecf20Sopenharmony_ci if (rc) { 9118c2ecf20Sopenharmony_ci cifs_dbg(VFS, "%s: Could not update sha512 shash\n", __func__); 9128c2ecf20Sopenharmony_ci return rc; 9138c2ecf20Sopenharmony_ci } 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci for (i = 0; i < nvec; i++) { 9168c2ecf20Sopenharmony_ci rc = crypto_shash_update(&d->shash, 9178c2ecf20Sopenharmony_ci iov[i].iov_base, iov[i].iov_len); 9188c2ecf20Sopenharmony_ci if (rc) { 9198c2ecf20Sopenharmony_ci cifs_dbg(VFS, "%s: Could not update sha512 shash\n", 9208c2ecf20Sopenharmony_ci __func__); 9218c2ecf20Sopenharmony_ci return rc; 9228c2ecf20Sopenharmony_ci } 9238c2ecf20Sopenharmony_ci } 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci rc = crypto_shash_final(&d->shash, ses->preauth_sha_hash); 9268c2ecf20Sopenharmony_ci if (rc) { 9278c2ecf20Sopenharmony_ci cifs_dbg(VFS, "%s: Could not finalize sha512 shash\n", 9288c2ecf20Sopenharmony_ci __func__); 9298c2ecf20Sopenharmony_ci return rc; 9308c2ecf20Sopenharmony_ci } 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_ci return 0; 9338c2ecf20Sopenharmony_ci} 934