Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/smb2pdu.c |
| 3 | * |
Steve French | 2b80d04 | 2013-06-23 18:43:37 -0500 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2009, 2013 |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 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 | |
| 26 | /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */ |
| 27 | /* Note that there are handle based routines which must be */ |
| 28 | /* treated slightly differently for reconnection purposes since we never */ |
| 29 | /* want to reuse a stale file handle and only the caller knows the file info */ |
| 30 | |
| 31 | #include <linux/fs.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/vfs.h> |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 34 | #include <linux/task_io_accounting_ops.h> |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 35 | #include <linux/uaccess.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 36 | #include <linux/uuid.h> |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 37 | #include <linux/pagemap.h> |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 38 | #include <linux/xattr.h> |
| 39 | #include "smb2pdu.h" |
| 40 | #include "cifsglob.h" |
| 41 | #include "cifsacl.h" |
| 42 | #include "cifsproto.h" |
| 43 | #include "smb2proto.h" |
| 44 | #include "cifs_unicode.h" |
| 45 | #include "cifs_debug.h" |
| 46 | #include "ntlmssp.h" |
| 47 | #include "smb2status.h" |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 48 | #include "smb2glob.h" |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 49 | #include "cifspdu.h" |
Steve French | ceb1b0b | 2015-09-24 00:52:37 -0500 | [diff] [blame] | 50 | #include "cifs_spnego.h" |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 51 | #include "smbdirect.h" |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 52 | #include "trace.h" |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 53 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 54 | #include "dfs_cache.h" |
| 55 | #endif |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * The following table defines the expected "StructureSize" of SMB2 requests |
| 59 | * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests. |
| 60 | * |
| 61 | * Note that commands are defined in smb2pdu.h in le16 but the array below is |
| 62 | * indexed by command in host byte order. |
| 63 | */ |
| 64 | static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { |
| 65 | /* SMB2_NEGOTIATE */ 36, |
| 66 | /* SMB2_SESSION_SETUP */ 25, |
| 67 | /* SMB2_LOGOFF */ 4, |
| 68 | /* SMB2_TREE_CONNECT */ 9, |
| 69 | /* SMB2_TREE_DISCONNECT */ 4, |
| 70 | /* SMB2_CREATE */ 57, |
| 71 | /* SMB2_CLOSE */ 24, |
| 72 | /* SMB2_FLUSH */ 24, |
| 73 | /* SMB2_READ */ 49, |
| 74 | /* SMB2_WRITE */ 49, |
| 75 | /* SMB2_LOCK */ 48, |
| 76 | /* SMB2_IOCTL */ 57, |
| 77 | /* SMB2_CANCEL */ 4, |
| 78 | /* SMB2_ECHO */ 4, |
| 79 | /* SMB2_QUERY_DIRECTORY */ 33, |
| 80 | /* SMB2_CHANGE_NOTIFY */ 32, |
| 81 | /* SMB2_QUERY_INFO */ 41, |
| 82 | /* SMB2_SET_INFO */ 33, |
| 83 | /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */ |
| 84 | }; |
| 85 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 86 | int smb3_encryption_required(const struct cifs_tcon *tcon) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 87 | { |
Steve French | edb1613 | 2020-05-31 14:36:56 -0500 | [diff] [blame] | 88 | if (!tcon || !tcon->ses) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 89 | return 0; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 90 | if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) || |
| 91 | (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)) |
| 92 | return 1; |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 93 | if (tcon->seal && |
| 94 | (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) |
| 95 | return 1; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 98 | |
| 99 | static void |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 100 | smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 101 | const struct cifs_tcon *tcon, |
| 102 | struct TCP_Server_Info *server) |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 103 | { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 104 | shdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 105 | shdr->StructureSize = cpu_to_le16(64); |
| 106 | shdr->Command = smb2_cmd; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 107 | if (server) { |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 108 | spin_lock(&server->req_lock); |
Steve French | 69dc4b1 | 2019-03-05 21:04:56 -0600 | [diff] [blame] | 109 | /* Request up to 10 credits but don't go over the limit. */ |
Steve French | 141891f | 2016-09-23 00:44:16 -0500 | [diff] [blame] | 110 | if (server->credits >= server->max_credits) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 111 | shdr->CreditRequest = cpu_to_le16(0); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 112 | else |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 113 | shdr->CreditRequest = cpu_to_le16( |
Steve French | 141891f | 2016-09-23 00:44:16 -0500 | [diff] [blame] | 114 | min_t(int, server->max_credits - |
Steve French | 69dc4b1 | 2019-03-05 21:04:56 -0600 | [diff] [blame] | 115 | server->credits, 10)); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 116 | spin_unlock(&server->req_lock); |
| 117 | } else { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 118 | shdr->CreditRequest = cpu_to_le16(2); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 119 | } |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 120 | shdr->ProcessId = cpu_to_le32((__u16)current->tgid); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 121 | |
| 122 | if (!tcon) |
| 123 | goto out; |
| 124 | |
Steve French | 2b80d04 | 2013-06-23 18:43:37 -0500 | [diff] [blame] | 125 | /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */ |
| 126 | /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */ |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 127 | if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU)) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 128 | shdr->CreditCharge = cpu_to_le16(1); |
Steve French | 2b80d04 | 2013-06-23 18:43:37 -0500 | [diff] [blame] | 129 | /* else CreditCharge MBZ */ |
| 130 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 131 | shdr->TreeId = tcon->tid; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 132 | /* Uid is not converted */ |
| 133 | if (tcon->ses) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 134 | shdr->SessionId = tcon->ses->Suid; |
Steve French | f87ab88 | 2013-06-26 19:14:55 -0500 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have |
| 138 | * to pass the path on the Open SMB prefixed by \\server\share. |
| 139 | * Not sure when we would need to do the augmented path (if ever) and |
| 140 | * setting this flag breaks the SMB2 open operation since it is |
| 141 | * illegal to send an empty path name (without \\server\share prefix) |
| 142 | * when the DFS flag is set in the SMB open header. We could |
| 143 | * consider setting the flag on all operations other than open |
| 144 | * but it is safer to net set it for now. |
| 145 | */ |
| 146 | /* if (tcon->share_flags & SHI1005_FLAGS_DFS) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 147 | shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */ |
Steve French | f87ab88 | 2013-06-26 19:14:55 -0500 | [diff] [blame] | 148 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 149 | if (server && server->sign && !smb3_encryption_required(tcon)) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 150 | shdr->Flags |= SMB2_FLAGS_SIGNED; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 151 | out: |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 155 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 156 | static int __smb2_reconnect(const struct nls_table *nlsc, |
| 157 | struct cifs_tcon *tcon) |
| 158 | { |
| 159 | int rc; |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 160 | struct TCP_Server_Info *server = tcon->ses->server; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 161 | struct dfs_cache_tgt_list tl; |
| 162 | struct dfs_cache_tgt_iterator *it = NULL; |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 163 | char *tree; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 164 | const char *tcp_host; |
| 165 | size_t tcp_host_len; |
| 166 | const char *dfs_host; |
| 167 | size_t dfs_host_len; |
| 168 | |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 169 | tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL); |
| 170 | if (!tree) |
| 171 | return -ENOMEM; |
| 172 | |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 173 | if (!tcon->dfs_path) { |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 174 | if (tcon->ipc) { |
| 175 | scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", |
| 176 | server->hostname); |
| 177 | rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc); |
| 178 | } else { |
| 179 | rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, |
| 180 | nlsc); |
| 181 | } |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 182 | goto out; |
| 183 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 184 | |
| 185 | rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl); |
| 186 | if (rc) |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 187 | goto out; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 188 | |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 189 | extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len); |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 190 | |
| 191 | for (it = dfs_cache_get_tgt_iterator(&tl); it; |
| 192 | it = dfs_cache_get_next_tgt(&tl, it)) { |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 193 | const char *share, *prefix; |
| 194 | size_t share_len, prefix_len; |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 195 | bool target_match; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 196 | |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 197 | rc = dfs_cache_get_tgt_share(it, &share, &share_len, &prefix, |
| 198 | &prefix_len); |
| 199 | if (rc) { |
| 200 | cifs_dbg(VFS, "%s: failed to parse target share %d\n", |
| 201 | __func__, rc); |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | extract_unc_hostname(share, &dfs_host, &dfs_host_len); |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 206 | |
| 207 | if (dfs_host_len != tcp_host_len |
| 208 | || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) { |
Steve French | adbb2da | 2020-05-30 16:45:11 -0500 | [diff] [blame] | 209 | cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 210 | __func__, |
| 211 | (int)dfs_host_len, dfs_host, |
| 212 | (int)tcp_host_len, tcp_host); |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 213 | |
| 214 | rc = match_target_ip(server, dfs_host, dfs_host_len, |
| 215 | &target_match); |
| 216 | if (rc) { |
| 217 | cifs_dbg(VFS, "%s: failed to match target ip: %d\n", |
| 218 | __func__, rc); |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | if (!target_match) { |
| 223 | cifs_dbg(FYI, "%s: skipping target\n", __func__); |
| 224 | continue; |
| 225 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 226 | } |
| 227 | |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 228 | if (tcon->ipc) { |
| 229 | scnprintf(tree, MAX_TREE_SIZE, "\\\\%.*s\\IPC$", |
| 230 | (int)share_len, share); |
| 231 | rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc); |
| 232 | } else { |
| 233 | scnprintf(tree, MAX_TREE_SIZE, "\\%.*s", (int)share_len, |
| 234 | share); |
| 235 | rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc); |
| 236 | if (!rc) { |
| 237 | rc = update_super_prepath(tcon, prefix, |
| 238 | prefix_len); |
| 239 | break; |
| 240 | } |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 241 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 242 | if (rc == -EREMOTE) |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | if (!rc) { |
| 247 | if (it) |
| 248 | rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, |
| 249 | it); |
| 250 | else |
| 251 | rc = -ENOENT; |
| 252 | } |
| 253 | dfs_cache_free_tgts(&tl); |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 254 | out: |
| 255 | kfree(tree); |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 256 | return rc; |
| 257 | } |
| 258 | #else |
| 259 | static inline int __smb2_reconnect(const struct nls_table *nlsc, |
| 260 | struct cifs_tcon *tcon) |
| 261 | { |
| 262 | return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc); |
| 263 | } |
| 264 | #endif |
| 265 | |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 266 | static int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 267 | smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon, |
| 268 | struct TCP_Server_Info *server) |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 269 | { |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 270 | int rc; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 271 | struct nls_table *nls_codepage; |
| 272 | struct cifs_ses *ses; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 273 | int retries; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so |
| 277 | * check for tcp and smb session status done differently |
| 278 | * for those three - in the calling routine. |
| 279 | */ |
| 280 | if (tcon == NULL) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 281 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 282 | |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 283 | if (smb2_command == SMB2_TREE_CONNECT) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 284 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 285 | |
| 286 | if (tcon->tidStatus == CifsExiting) { |
| 287 | /* |
| 288 | * only tree disconnect, open, and write, |
| 289 | * (and ulogoff which does not have tcon) |
| 290 | * are allowed as we start force umount. |
| 291 | */ |
| 292 | if ((smb2_command != SMB2_WRITE) && |
| 293 | (smb2_command != SMB2_CREATE) && |
| 294 | (smb2_command != SMB2_TREE_DISCONNECT)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 295 | cifs_dbg(FYI, "can not send cmd %d while umounting\n", |
| 296 | smb2_command); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 297 | return -ENODEV; |
| 298 | } |
| 299 | } |
| 300 | if ((!tcon->ses) || (tcon->ses->status == CifsExiting) || |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 301 | (!tcon->ses->server) || !server) |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 302 | return -EIO; |
| 303 | |
| 304 | ses = tcon->ses; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 305 | retries = server->nr_targets; |
| 306 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 307 | /* |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 308 | * Give demultiplex thread up to 10 seconds to each target available for |
| 309 | * reconnect -- should be greater than cifs socket timeout which is 7 |
| 310 | * seconds. |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 311 | */ |
| 312 | while (server->tcpStatus == CifsNeedReconnect) { |
| 313 | /* |
| 314 | * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE |
| 315 | * here since they are implicitly done when session drops. |
| 316 | */ |
| 317 | switch (smb2_command) { |
| 318 | /* |
| 319 | * BB Should we keep oplock break and add flush to exceptions? |
| 320 | */ |
| 321 | case SMB2_TREE_DISCONNECT: |
| 322 | case SMB2_CANCEL: |
| 323 | case SMB2_CLOSE: |
| 324 | case SMB2_OPLOCK_BREAK: |
| 325 | return -EAGAIN; |
| 326 | } |
| 327 | |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 328 | rc = wait_event_interruptible_timeout(server->response_q, |
| 329 | (server->tcpStatus != CifsNeedReconnect), |
| 330 | 10 * HZ); |
| 331 | if (rc < 0) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 332 | cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n", |
| 333 | __func__); |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 334 | return -ERESTARTSYS; |
| 335 | } |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 336 | |
| 337 | /* are we still trying to reconnect? */ |
| 338 | if (server->tcpStatus != CifsNeedReconnect) |
| 339 | break; |
| 340 | |
Ronnie Sahlberg | c54849d | 2020-01-31 05:52:51 +1000 | [diff] [blame] | 341 | if (retries && --retries) |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 342 | continue; |
| 343 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 344 | /* |
| 345 | * on "soft" mounts we wait once. Hard mounts keep |
| 346 | * retrying until process is killed or server comes |
| 347 | * back on-line |
| 348 | */ |
| 349 | if (!tcon->retry) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 350 | cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n"); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 351 | return -EHOSTDOWN; |
| 352 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 353 | retries = server->nr_targets; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | if (!tcon->ses->need_reconnect && !tcon->need_reconnect) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 357 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 358 | |
| 359 | nls_codepage = load_nls_default(); |
| 360 | |
| 361 | /* |
| 362 | * need to prevent multiple threads trying to simultaneously reconnect |
| 363 | * the same SMB session |
| 364 | */ |
| 365 | mutex_lock(&tcon->ses->session_mutex); |
Samuel Cabrero | 76e7527 | 2017-07-11 12:44:39 +0200 | [diff] [blame] | 366 | |
| 367 | /* |
| 368 | * Recheck after acquire mutex. If another thread is negotiating |
| 369 | * and the server never sends an answer the socket will be closed |
| 370 | * and tcpStatus set to reconnect. |
| 371 | */ |
| 372 | if (server->tcpStatus == CifsNeedReconnect) { |
| 373 | rc = -EHOSTDOWN; |
| 374 | mutex_unlock(&tcon->ses->session_mutex); |
| 375 | goto out; |
| 376 | } |
| 377 | |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 378 | /* |
| 379 | * If we are reconnecting an extra channel, bind |
| 380 | */ |
| 381 | if (server->is_channel) { |
| 382 | ses->binding = true; |
| 383 | ses->binding_chan = cifs_ses_find_chan(ses, server); |
| 384 | } |
| 385 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 386 | rc = cifs_negotiate_protocol(0, tcon->ses); |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 387 | if (!rc && tcon->ses->need_reconnect) { |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 388 | rc = cifs_setup_session(0, tcon->ses, nls_codepage); |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 389 | if ((rc == -EACCES) && !tcon->retry) { |
| 390 | rc = -EHOSTDOWN; |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 391 | ses->binding = false; |
| 392 | ses->binding_chan = NULL; |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 393 | mutex_unlock(&tcon->ses->session_mutex); |
| 394 | goto failed; |
| 395 | } |
| 396 | } |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 397 | /* |
| 398 | * End of channel binding |
| 399 | */ |
| 400 | ses->binding = false; |
| 401 | ses->binding_chan = NULL; |
| 402 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 403 | if (rc || !tcon->need_reconnect) { |
| 404 | mutex_unlock(&tcon->ses->session_mutex); |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | cifs_mark_open_files_invalid(tcon); |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 409 | if (tcon->use_persistent) |
| 410 | tcon->need_reopen_files = true; |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 411 | |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 412 | rc = __smb2_reconnect(nls_codepage, tcon); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 413 | mutex_unlock(&tcon->ses->session_mutex); |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 414 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 415 | cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc); |
Steve French | c318e6c | 2018-04-04 14:08:52 -0500 | [diff] [blame] | 416 | if (rc) { |
| 417 | /* If sess reconnected but tcon didn't, something strange ... */ |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 418 | pr_warn_once("reconnect tcon failed rc = %d\n", rc); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 419 | goto out; |
Steve French | c318e6c | 2018-04-04 14:08:52 -0500 | [diff] [blame] | 420 | } |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 421 | |
| 422 | if (smb2_command != SMB2_INTERNAL_CMD) |
Stefan Metzmacher | b08484d | 2020-02-24 14:14:59 +0100 | [diff] [blame] | 423 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 424 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 425 | atomic_inc(&tconInfoReconnectCount); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 426 | out: |
| 427 | /* |
| 428 | * Check if handle based operation so we know whether we can continue |
| 429 | * or not without returning to caller to reset file handle. |
| 430 | */ |
| 431 | /* |
| 432 | * BB Is flush done by server on drop of tcp session? Should we special |
| 433 | * case it and skip above? |
| 434 | */ |
| 435 | switch (smb2_command) { |
| 436 | case SMB2_FLUSH: |
| 437 | case SMB2_READ: |
| 438 | case SMB2_WRITE: |
| 439 | case SMB2_LOCK: |
| 440 | case SMB2_IOCTL: |
| 441 | case SMB2_QUERY_DIRECTORY: |
| 442 | case SMB2_CHANGE_NOTIFY: |
| 443 | case SMB2_QUERY_INFO: |
| 444 | case SMB2_SET_INFO: |
Pavel Shilovsky | 4772c79 | 2016-11-29 11:30:58 -0800 | [diff] [blame] | 445 | rc = -EAGAIN; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 446 | } |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 447 | failed: |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 448 | unload_nls(nls_codepage); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 449 | return rc; |
| 450 | } |
| 451 | |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 452 | static void |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 453 | fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, |
| 454 | struct TCP_Server_Info *server, |
| 455 | void *buf, |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 456 | unsigned int *total_len) |
| 457 | { |
| 458 | struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf; |
| 459 | /* lookup word count ie StructureSize from table */ |
| 460 | __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)]; |
| 461 | |
| 462 | /* |
| 463 | * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of |
| 464 | * largest operations (Create) |
| 465 | */ |
| 466 | memset(buf, 0, 256); |
| 467 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 468 | smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon, server); |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 469 | spdu->StructureSize2 = cpu_to_le16(parmsize); |
| 470 | |
| 471 | *total_len = parmsize + sizeof(struct smb2_sync_hdr); |
| 472 | } |
| 473 | |
Ronnie Sahlberg | 305428a | 2017-11-21 11:04:42 +1100 | [diff] [blame] | 474 | /* |
| 475 | * Allocate and return pointer to an SMB request hdr, and set basic |
| 476 | * SMB information in the SMB header. If the return code is zero, this |
| 477 | * function must have filled in request_buf pointer. |
| 478 | */ |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 479 | static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 480 | struct TCP_Server_Info *server, |
| 481 | void **request_buf, unsigned int *total_len) |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 482 | { |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 483 | /* BB eventually switch this to SMB2 specific small buf size */ |
Stefano Brivio | f46ecbd | 2018-07-05 11:46:42 +0200 | [diff] [blame] | 484 | if (smb2_command == SMB2_SET_INFO) |
| 485 | *request_buf = cifs_buf_get(); |
| 486 | else |
| 487 | *request_buf = cifs_small_buf_get(); |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 488 | if (*request_buf == NULL) { |
| 489 | /* BB should we add a retry in here if not a writepage? */ |
| 490 | return -ENOMEM; |
| 491 | } |
| 492 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 493 | fill_small_buf(smb2_command, tcon, server, |
Ronnie Sahlberg | 305428a | 2017-11-21 11:04:42 +1100 | [diff] [blame] | 494 | (struct smb2_sync_hdr *)(*request_buf), |
| 495 | total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 496 | |
| 497 | if (tcon != NULL) { |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 498 | uint16_t com_code = le16_to_cpu(smb2_command); |
| 499 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 500 | cifs_stats_inc(&tcon->num_smbs_sent); |
| 501 | } |
| 502 | |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 507 | struct TCP_Server_Info *server, |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 508 | void **request_buf, unsigned int *total_len) |
| 509 | { |
| 510 | int rc; |
| 511 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 512 | rc = smb2_reconnect(smb2_command, tcon, server); |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 513 | if (rc) |
| 514 | return rc; |
| 515 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 516 | return __smb2_plain_req_init(smb2_command, tcon, server, request_buf, |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 517 | total_len); |
| 518 | } |
| 519 | |
| 520 | static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 521 | struct TCP_Server_Info *server, |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 522 | void **request_buf, unsigned int *total_len) |
| 523 | { |
| 524 | /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */ |
| 525 | if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) { |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 526 | return __smb2_plain_req_init(SMB2_IOCTL, tcon, server, |
| 527 | request_buf, total_len); |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 528 | } |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 529 | return smb2_plain_req_init(SMB2_IOCTL, tcon, server, |
| 530 | request_buf, total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 531 | } |
| 532 | |
Steve French | d7bef4c | 2019-04-18 11:03:58 -0500 | [diff] [blame] | 533 | /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */ |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 534 | |
| 535 | static void |
| 536 | build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt) |
| 537 | { |
| 538 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 539 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 540 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 541 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 542 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 543 | pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512; |
| 544 | } |
| 545 | |
| 546 | static void |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 547 | build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt) |
| 548 | { |
| 549 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 550 | pneg_ctxt->DataLength = |
| 551 | cpu_to_le16(sizeof(struct smb2_compression_capabilities_context) |
| 552 | - sizeof(struct smb2_neg_context)); |
| 553 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3); |
| 554 | pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77; |
| 555 | pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF; |
| 556 | pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1; |
| 557 | } |
| 558 | |
| 559 | static void |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 560 | build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt) |
| 561 | { |
| 562 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
Steve French | 9ac63ec | 2019-06-07 08:59:40 -0500 | [diff] [blame] | 563 | pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */ |
| 564 | pneg_ctxt->CipherCount = cpu_to_le16(2); |
| 565 | pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM; |
| 566 | pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM; |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 569 | static unsigned int |
| 570 | build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname) |
| 571 | { |
| 572 | struct nls_table *cp = load_nls_default(); |
| 573 | |
| 574 | pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID; |
| 575 | |
| 576 | /* copy up to max of first 100 bytes of server name to NetName field */ |
Steve French | df58fae | 2019-08-05 17:07:26 -0500 | [diff] [blame] | 577 | pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp)); |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 578 | /* context size is DataLength + minimal smb2_neg_context */ |
| 579 | return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) + |
| 580 | sizeof(struct smb2_neg_context), 8) * 8; |
| 581 | } |
| 582 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 583 | static void |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 584 | build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
| 585 | { |
| 586 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 587 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
Steve French | 0d48132 | 2019-02-24 17:56:33 -0600 | [diff] [blame] | 588 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 589 | pneg_ctxt->Name[0] = 0x93; |
| 590 | pneg_ctxt->Name[1] = 0xAD; |
| 591 | pneg_ctxt->Name[2] = 0x25; |
| 592 | pneg_ctxt->Name[3] = 0x50; |
| 593 | pneg_ctxt->Name[4] = 0x9C; |
| 594 | pneg_ctxt->Name[5] = 0xB4; |
| 595 | pneg_ctxt->Name[6] = 0x11; |
| 596 | pneg_ctxt->Name[7] = 0xE7; |
| 597 | pneg_ctxt->Name[8] = 0xB4; |
| 598 | pneg_ctxt->Name[9] = 0x23; |
| 599 | pneg_ctxt->Name[10] = 0x83; |
| 600 | pneg_ctxt->Name[11] = 0xDE; |
| 601 | pneg_ctxt->Name[12] = 0x96; |
| 602 | pneg_ctxt->Name[13] = 0x8B; |
| 603 | pneg_ctxt->Name[14] = 0xCD; |
| 604 | pneg_ctxt->Name[15] = 0x7C; |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 608 | assemble_neg_contexts(struct smb2_negotiate_req *req, |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 609 | struct TCP_Server_Info *server, unsigned int *total_len) |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 610 | { |
Colin Ian King | a9f76cf | 2019-12-02 18:59:42 +0000 | [diff] [blame] | 611 | char *pneg_ctxt; |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 612 | unsigned int ctxt_len; |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 613 | |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 614 | if (*total_len > 200) { |
| 615 | /* In case length corrupted don't want to overrun smb buffer */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 616 | cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n"); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 617 | return; |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * round up total_len of fixed part of SMB3 negotiate request to 8 |
| 622 | * byte boundary before adding negotiate contexts |
| 623 | */ |
| 624 | *total_len = roundup(*total_len, 8); |
| 625 | |
| 626 | pneg_ctxt = (*total_len) + (char *)req; |
| 627 | req->NegotiateContextOffset = cpu_to_le32(*total_len); |
| 628 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 629 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 630 | ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8; |
| 631 | *total_len += ctxt_len; |
| 632 | pneg_ctxt += ctxt_len; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 633 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 634 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 635 | ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8; |
| 636 | *total_len += ctxt_len; |
| 637 | pneg_ctxt += ctxt_len; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 638 | |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 639 | if (server->compress_algorithm) { |
| 640 | build_compression_ctxt((struct smb2_compression_capabilities_context *) |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 641 | pneg_ctxt); |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 642 | ctxt_len = DIV_ROUND_UP( |
| 643 | sizeof(struct smb2_compression_capabilities_context), |
| 644 | 8) * 8; |
| 645 | *total_len += ctxt_len; |
| 646 | pneg_ctxt += ctxt_len; |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 647 | req->NegotiateContextCount = cpu_to_le16(5); |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 648 | } else |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 649 | req->NegotiateContextCount = cpu_to_le16(4); |
| 650 | |
| 651 | ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt, |
| 652 | server->hostname); |
| 653 | *total_len += ctxt_len; |
| 654 | pneg_ctxt += ctxt_len; |
| 655 | |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 656 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 657 | *total_len += sizeof(struct smb2_posix_neg_context); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 658 | } |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 659 | |
| 660 | static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt) |
| 661 | { |
| 662 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 663 | |
| 664 | /* If invalid preauth context warn but use what we requested, SHA-512 */ |
| 665 | if (len < MIN_PREAUTH_CTXT_DATA_LEN) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 666 | pr_warn_once("server sent bad preauth context\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 667 | return; |
| 668 | } |
| 669 | if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 670 | pr_warn_once("Invalid SMB3 hash algorithm count\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 671 | if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 672 | pr_warn_once("unknown SMB3 hash algorithm\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 673 | } |
| 674 | |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 675 | static void decode_compress_ctx(struct TCP_Server_Info *server, |
| 676 | struct smb2_compression_capabilities_context *ctxt) |
| 677 | { |
| 678 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 679 | |
| 680 | /* sizeof compress context is a one element compression capbility struct */ |
| 681 | if (len < 10) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 682 | pr_warn_once("server sent bad compression cntxt\n"); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 683 | return; |
| 684 | } |
| 685 | if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 686 | pr_warn_once("Invalid SMB3 compress algorithm count\n"); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 687 | return; |
| 688 | } |
| 689 | if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 690 | pr_warn_once("unknown compression algorithm\n"); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 691 | return; |
| 692 | } |
| 693 | server->compress_algorithm = ctxt->CompressionAlgorithms[0]; |
| 694 | } |
| 695 | |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 696 | static int decode_encrypt_ctx(struct TCP_Server_Info *server, |
| 697 | struct smb2_encryption_neg_context *ctxt) |
| 698 | { |
| 699 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 700 | |
| 701 | cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len); |
| 702 | if (len < MIN_ENCRYPT_CTXT_DATA_LEN) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 703 | pr_warn_once("server sent bad crypto ctxt len\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 704 | return -EINVAL; |
| 705 | } |
| 706 | |
| 707 | if (le16_to_cpu(ctxt->CipherCount) != 1) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 708 | pr_warn_once("Invalid SMB3.11 cipher count\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 709 | return -EINVAL; |
| 710 | } |
| 711 | cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0])); |
| 712 | if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) && |
| 713 | (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 714 | pr_warn_once("Invalid SMB3.11 cipher returned\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 715 | return -EINVAL; |
| 716 | } |
| 717 | server->cipher_type = ctxt->Ciphers[0]; |
Steve French | 23657ad | 2018-04-22 15:14:58 -0500 | [diff] [blame] | 718 | server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION; |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp, |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 723 | struct TCP_Server_Info *server, |
| 724 | unsigned int len_of_smb) |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 725 | { |
| 726 | struct smb2_neg_context *pctx; |
| 727 | unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset); |
| 728 | unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 729 | unsigned int len_of_ctxts, i; |
| 730 | int rc = 0; |
| 731 | |
| 732 | cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt); |
| 733 | if (len_of_smb <= offset) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 734 | cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 735 | return -EINVAL; |
| 736 | } |
| 737 | |
| 738 | len_of_ctxts = len_of_smb - offset; |
| 739 | |
| 740 | for (i = 0; i < ctxt_cnt; i++) { |
| 741 | int clen; |
| 742 | /* check that offset is not beyond end of SMB */ |
| 743 | if (len_of_ctxts == 0) |
| 744 | break; |
| 745 | |
| 746 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 747 | break; |
| 748 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 749 | pctx = (struct smb2_neg_context *)(offset + (char *)rsp); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 750 | clen = le16_to_cpu(pctx->DataLength); |
| 751 | if (clen > len_of_ctxts) |
| 752 | break; |
| 753 | |
| 754 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) |
| 755 | decode_preauth_context( |
| 756 | (struct smb2_preauth_neg_context *)pctx); |
| 757 | else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) |
| 758 | rc = decode_encrypt_ctx(server, |
| 759 | (struct smb2_encryption_neg_context *)pctx); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 760 | else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) |
| 761 | decode_compress_ctx(server, |
| 762 | (struct smb2_compression_capabilities_context *)pctx); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 763 | else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) |
| 764 | server->posix_ext_supported = true; |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 765 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 766 | cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n", |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 767 | le16_to_cpu(pctx->ContextType)); |
| 768 | |
| 769 | if (rc) |
| 770 | break; |
| 771 | /* offsets must be 8 byte aligned */ |
| 772 | clen = (clen + 7) & ~0x7; |
| 773 | offset += clen + sizeof(struct smb2_neg_context); |
| 774 | len_of_ctxts -= clen; |
| 775 | } |
| 776 | return rc; |
| 777 | } |
| 778 | |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 779 | static struct create_posix * |
| 780 | create_posix_buf(umode_t mode) |
| 781 | { |
| 782 | struct create_posix *buf; |
| 783 | |
| 784 | buf = kzalloc(sizeof(struct create_posix), |
| 785 | GFP_KERNEL); |
| 786 | if (!buf) |
| 787 | return NULL; |
| 788 | |
| 789 | buf->ccontext.DataOffset = |
| 790 | cpu_to_le16(offsetof(struct create_posix, Mode)); |
| 791 | buf->ccontext.DataLength = cpu_to_le32(4); |
| 792 | buf->ccontext.NameOffset = |
| 793 | cpu_to_le16(offsetof(struct create_posix, Name)); |
| 794 | buf->ccontext.NameLength = cpu_to_le16(16); |
| 795 | |
| 796 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 797 | buf->Name[0] = 0x93; |
| 798 | buf->Name[1] = 0xAD; |
| 799 | buf->Name[2] = 0x25; |
| 800 | buf->Name[3] = 0x50; |
| 801 | buf->Name[4] = 0x9C; |
| 802 | buf->Name[5] = 0xB4; |
| 803 | buf->Name[6] = 0x11; |
| 804 | buf->Name[7] = 0xE7; |
| 805 | buf->Name[8] = 0xB4; |
| 806 | buf->Name[9] = 0x23; |
| 807 | buf->Name[10] = 0x83; |
| 808 | buf->Name[11] = 0xDE; |
| 809 | buf->Name[12] = 0x96; |
| 810 | buf->Name[13] = 0x8B; |
| 811 | buf->Name[14] = 0xCD; |
| 812 | buf->Name[15] = 0x7C; |
| 813 | buf->Mode = cpu_to_le32(mode); |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 814 | cifs_dbg(FYI, "mode on posix create 0%o\n", mode); |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 815 | return buf; |
| 816 | } |
| 817 | |
| 818 | static int |
| 819 | add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode) |
| 820 | { |
| 821 | struct smb2_create_req *req = iov[0].iov_base; |
| 822 | unsigned int num = *num_iovec; |
| 823 | |
| 824 | iov[num].iov_base = create_posix_buf(mode); |
Steve French | d0959b0 | 2019-10-05 10:53:58 -0500 | [diff] [blame] | 825 | if (mode == ACL_NO_MODE) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 826 | cifs_dbg(FYI, "Invalid mode\n"); |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 827 | if (iov[num].iov_base == NULL) |
| 828 | return -ENOMEM; |
| 829 | iov[num].iov_len = sizeof(struct create_posix); |
| 830 | if (!req->CreateContextsOffset) |
| 831 | req->CreateContextsOffset = cpu_to_le32( |
| 832 | sizeof(struct smb2_create_req) + |
| 833 | iov[num - 1].iov_len); |
| 834 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix)); |
| 835 | *num_iovec = num + 1; |
| 836 | return 0; |
| 837 | } |
| 838 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 839 | |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 840 | /* |
| 841 | * |
| 842 | * SMB2 Worker functions follow: |
| 843 | * |
| 844 | * The general structure of the worker functions is: |
| 845 | * 1) Call smb2_init (assembles SMB2 header) |
| 846 | * 2) Initialize SMB2 command specific fields in fixed length area of SMB |
| 847 | * 3) Call smb_sendrcv2 (sends request on socket and waits for response) |
| 848 | * 4) Decode SMB2 command specific fields in the fixed length area |
| 849 | * 5) Decode variable length data area (if any for this SMB2 command type) |
| 850 | * 6) Call free smb buffer |
| 851 | * 7) return |
| 852 | * |
| 853 | */ |
| 854 | |
| 855 | int |
| 856 | SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses) |
| 857 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 858 | struct smb_rqst rqst; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 859 | struct smb2_negotiate_req *req; |
| 860 | struct smb2_negotiate_rsp *rsp; |
| 861 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 862 | struct kvec rsp_iov; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 863 | int rc = 0; |
| 864 | int resp_buftype; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 865 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 866 | int blob_offset, blob_length; |
| 867 | char *security_blob; |
| 868 | int flags = CIFS_NEG_OP; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 869 | unsigned int total_len; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 870 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 871 | cifs_dbg(FYI, "Negotiate protocol\n"); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 872 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 873 | if (!server) { |
| 874 | WARN(1, "%s: server is NULL!\n", __func__); |
| 875 | return -EIO; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 876 | } |
| 877 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 878 | rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server, |
| 879 | (void **) &req, &total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 880 | if (rc) |
| 881 | return rc; |
| 882 | |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 883 | req->sync_hdr.SessionId = 0; |
Steve French | 0fdfef9 | 2018-06-28 19:30:23 -0500 | [diff] [blame] | 884 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 885 | memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); |
| 886 | memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 887 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 888 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 889 | SMB3ANY_VERSION_STRING) == 0) { |
| 890 | req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); |
| 891 | req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); |
| 892 | req->DialectCount = cpu_to_le16(2); |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 893 | total_len += 4; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 894 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 895 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 896 | req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); |
| 897 | req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); |
| 898 | req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 899 | req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); |
| 900 | req->DialectCount = cpu_to_le16(4); |
| 901 | total_len += 8; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 902 | } else { |
| 903 | /* otherwise send specific dialect */ |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 904 | req->Dialects[0] = cpu_to_le16(server->vals->protocol_id); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 905 | req->DialectCount = cpu_to_le16(1); |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 906 | total_len += 2; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 907 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 908 | |
| 909 | /* only one of SMB2 signing flags may be set in SMB2 request */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 910 | if (ses->sign) |
Steve French | 9cd2e62 | 2013-06-12 19:59:03 -0500 | [diff] [blame] | 911 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 912 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
Steve French | 9cd2e62 | 2013-06-12 19:59:03 -0500 | [diff] [blame] | 913 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 914 | else |
| 915 | req->SecurityMode = 0; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 916 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 917 | req->Capabilities = cpu_to_le32(server->vals->req_capabilities); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 918 | |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 919 | /* ClientGUID must be zero for SMB2.02 dialect */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 920 | if (server->vals->protocol_id == SMB20_PROT_ID) |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 921 | memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 922 | else { |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 923 | memcpy(req->ClientGUID, server->client_guid, |
| 924 | SMB2_CLIENT_GUID_SIZE); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 925 | if ((server->vals->protocol_id == SMB311_PROT_ID) || |
| 926 | (strcmp(server->vals->version_string, |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 927 | SMBDEFAULT_VERSION_STRING) == 0)) |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 928 | assemble_neg_contexts(req, server, &total_len); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 929 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 930 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 931 | iov[0].iov_len = total_len; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 932 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 933 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 934 | rqst.rq_iov = iov; |
| 935 | rqst.rq_nvec = 1; |
| 936 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 937 | rc = cifs_send_recv(xid, ses, server, |
| 938 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 939 | cifs_small_buf_release(req); |
| 940 | rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 941 | /* |
| 942 | * No tcon so can't do |
| 943 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); |
| 944 | */ |
Steve French | 7e682f7 | 2017-08-31 21:34:24 -0500 | [diff] [blame] | 945 | if (rc == -EOPNOTSUPP) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 946 | cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n"); |
Steve French | 7e682f7 | 2017-08-31 21:34:24 -0500 | [diff] [blame] | 947 | goto neg_exit; |
| 948 | } else if (rc != 0) |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 949 | goto neg_exit; |
| 950 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 951 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 952 | SMB3ANY_VERSION_STRING) == 0) { |
| 953 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 954 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 955 | "SMB2 dialect returned but not requested\n"); |
| 956 | return -EIO; |
| 957 | } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 958 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 959 | "SMB2.1 dialect returned but not requested\n"); |
| 960 | return -EIO; |
| 961 | } |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 962 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 963 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 964 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 965 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 966 | "SMB2 dialect returned but not requested\n"); |
| 967 | return -EIO; |
| 968 | } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) { |
| 969 | /* ops set to 3.0 by default for default so update */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 970 | server->ops = &smb21_operations; |
| 971 | server->vals = &smb21_values; |
ZhangXiaoxu | b57a55e | 2019-04-06 15:30:38 +0800 | [diff] [blame] | 972 | } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 973 | server->ops = &smb311_operations; |
| 974 | server->vals = &smb311_values; |
ZhangXiaoxu | b57a55e | 2019-04-06 15:30:38 +0800 | [diff] [blame] | 975 | } |
Steve French | 590d08d | 2017-09-19 11:43:47 -0500 | [diff] [blame] | 976 | } else if (le16_to_cpu(rsp->DialectRevision) != |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 977 | server->vals->protocol_id) { |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 978 | /* if requested single dialect ensure returned dialect matched */ |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 979 | cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n", |
| 980 | le16_to_cpu(rsp->DialectRevision)); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 981 | return -EIO; |
| 982 | } |
| 983 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 984 | cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 985 | |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 986 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 987 | cifs_dbg(FYI, "negotiated smb2.0 dialect\n"); |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 988 | else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 989 | cifs_dbg(FYI, "negotiated smb2.1 dialect\n"); |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 990 | else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 991 | cifs_dbg(FYI, "negotiated smb3.0 dialect\n"); |
Steve French | 20b6d8b | 2013-06-12 22:48:41 -0500 | [diff] [blame] | 992 | else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID)) |
| 993 | cifs_dbg(FYI, "negotiated smb3.02 dialect\n"); |
Steve French | 5f7fbf7 | 2014-12-17 22:52:58 -0600 | [diff] [blame] | 994 | else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) |
| 995 | cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n"); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 996 | else { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 997 | cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n", |
| 998 | le16_to_cpu(rsp->DialectRevision)); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 999 | rc = -EIO; |
| 1000 | goto neg_exit; |
| 1001 | } |
| 1002 | server->dialect = le16_to_cpu(rsp->DialectRevision); |
| 1003 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1004 | /* |
| 1005 | * Keep a copy of the hash after negprot. This hash will be |
| 1006 | * the starting hash value for all sessions made from this |
| 1007 | * server. |
| 1008 | */ |
| 1009 | memcpy(server->preauth_sha_hash, ses->preauth_sha_hash, |
| 1010 | SMB2_PREAUTH_HASH_SIZE); |
Steve French | 0fdfef9 | 2018-06-28 19:30:23 -0500 | [diff] [blame] | 1011 | |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 1012 | /* SMB2 only has an extended negflavor */ |
| 1013 | server->negflavor = CIFS_NEGFLAVOR_EXTENDED; |
Pavel Shilovsky | 2365c4e | 2014-02-14 13:31:02 +0400 | [diff] [blame] | 1014 | /* set it to the maximum buffer size value we can send with 1 credit */ |
| 1015 | server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize), |
| 1016 | SMB2_MAX_BUFFER_SIZE); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1017 | server->max_read = le32_to_cpu(rsp->MaxReadSize); |
| 1018 | server->max_write = le32_to_cpu(rsp->MaxWriteSize); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1019 | server->sec_mode = le16_to_cpu(rsp->SecurityMode); |
Steve French | 07108d0 | 2018-04-01 20:15:55 -0500 | [diff] [blame] | 1020 | if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode) |
| 1021 | cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n", |
| 1022 | server->sec_mode); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1023 | server->capabilities = le32_to_cpu(rsp->Capabilities); |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1024 | /* Internal types */ |
| 1025 | server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1026 | |
| 1027 | security_blob = smb2_get_data_area_len(&blob_offset, &blob_length, |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1028 | (struct smb2_sync_hdr *)rsp); |
Steve French | 5d875cc | 2013-06-25 15:33:41 -0500 | [diff] [blame] | 1029 | /* |
| 1030 | * See MS-SMB2 section 2.2.4: if no blob, client picks default which |
| 1031 | * for us will be |
| 1032 | * ses->sectype = RawNTLMSSP; |
| 1033 | * but for time being this is our only auth choice so doesn't matter. |
| 1034 | * We just found a server which sets blob length to zero expecting raw. |
| 1035 | */ |
Pavel Shilovsky | 67dbea2 | 2017-04-12 13:32:07 -0700 | [diff] [blame] | 1036 | if (blob_length == 0) { |
Steve French | 5d875cc | 2013-06-25 15:33:41 -0500 | [diff] [blame] | 1037 | cifs_dbg(FYI, "missing security blob on negprot\n"); |
Pavel Shilovsky | 67dbea2 | 2017-04-12 13:32:07 -0700 | [diff] [blame] | 1038 | server->sec_ntlmssp = true; |
| 1039 | } |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1040 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 1041 | rc = cifs_enable_signing(server, ses->sign); |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 1042 | if (rc) |
| 1043 | goto neg_exit; |
Steve French | ceb1b0b | 2015-09-24 00:52:37 -0500 | [diff] [blame] | 1044 | if (blob_length) { |
Steve French | ebdd207 | 2014-10-20 12:48:23 -0500 | [diff] [blame] | 1045 | rc = decode_negTokenInit(security_blob, blob_length, server); |
Steve French | ceb1b0b | 2015-09-24 00:52:37 -0500 | [diff] [blame] | 1046 | if (rc == 1) |
| 1047 | rc = 0; |
| 1048 | else if (rc == 0) |
| 1049 | rc = -EIO; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1050 | } |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1051 | |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1052 | if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) { |
| 1053 | if (rsp->NegotiateContextCount) |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 1054 | rc = smb311_decode_neg_context(rsp, server, |
| 1055 | rsp_iov.iov_len); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1056 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1057 | cifs_server_dbg(VFS, "Missing expected negotiate contexts\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1058 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1059 | neg_exit: |
| 1060 | free_rsp_buf(resp_buftype, rsp); |
| 1061 | return rc; |
| 1062 | } |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1063 | |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1064 | int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon) |
| 1065 | { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1066 | int rc; |
| 1067 | struct validate_negotiate_info_req *pneg_inbuf; |
David Disseldorp | fe83bebc | 2017-10-20 14:49:37 +0200 | [diff] [blame] | 1068 | struct validate_negotiate_info_rsp *pneg_rsp = NULL; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1069 | u32 rsplen; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1070 | u32 inbuflen; /* max of 4 dialects */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1071 | struct TCP_Server_Info *server = tcon->ses->server; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1072 | |
| 1073 | cifs_dbg(FYI, "validate negotiate\n"); |
| 1074 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1075 | /* In SMB3.11 preauth integrity supersedes validate negotiate */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1076 | if (server->dialect == SMB311_PROT_ID) |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1077 | return 0; |
| 1078 | |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1079 | /* |
| 1080 | * validation ioctl must be signed, so no point sending this if we |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1081 | * can not sign it (ie are not known user). Even if signing is not |
| 1082 | * required (enabled but not negotiated), in those cases we selectively |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1083 | * sign just this, the first and only signed request on a connection. |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1084 | * Having validation of negotiate info helps reduce attack vectors. |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1085 | */ |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1086 | if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1087 | return 0; /* validation requires signing */ |
| 1088 | |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1089 | if (tcon->ses->user_name == NULL) { |
| 1090 | cifs_dbg(FYI, "Can't validate negotiate: null user mount\n"); |
| 1091 | return 0; /* validation requires signing */ |
| 1092 | } |
| 1093 | |
| 1094 | if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1095 | cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n"); |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1096 | |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1097 | pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS); |
| 1098 | if (!pneg_inbuf) |
| 1099 | return -ENOMEM; |
| 1100 | |
| 1101 | pneg_inbuf->Capabilities = |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1102 | cpu_to_le32(server->vals->req_capabilities); |
| 1103 | memcpy(pneg_inbuf->Guid, server->client_guid, |
Sachin Prabhu | 39552ea | 2014-05-13 00:48:12 +0100 | [diff] [blame] | 1104 | SMB2_CLIENT_GUID_SIZE); |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1105 | |
| 1106 | if (tcon->ses->sign) |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1107 | pneg_inbuf->SecurityMode = |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1108 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
| 1109 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1110 | pneg_inbuf->SecurityMode = |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1111 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
| 1112 | else |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1113 | pneg_inbuf->SecurityMode = 0; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1114 | |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1115 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1116 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1117 | SMB3ANY_VERSION_STRING) == 0) { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1118 | pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); |
| 1119 | pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); |
| 1120 | pneg_inbuf->DialectCount = cpu_to_le16(2); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1121 | /* structure is big enough for 3 dialects, sending only 2 */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1122 | inbuflen = sizeof(*pneg_inbuf) - |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 1123 | (2 * sizeof(pneg_inbuf->Dialects[0])); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1124 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1125 | SMBDEFAULT_VERSION_STRING) == 0) { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1126 | pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); |
| 1127 | pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); |
| 1128 | pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 1129 | pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); |
| 1130 | pneg_inbuf->DialectCount = cpu_to_le16(4); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1131 | /* structure is big enough for 3 dialects */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1132 | inbuflen = sizeof(*pneg_inbuf); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1133 | } else { |
| 1134 | /* otherwise specific dialect was requested */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1135 | pneg_inbuf->Dialects[0] = |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1136 | cpu_to_le16(server->vals->protocol_id); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1137 | pneg_inbuf->DialectCount = cpu_to_le16(1); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1138 | /* structure is big enough for 3 dialects, sending only 1 */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1139 | inbuflen = sizeof(*pneg_inbuf) - |
| 1140 | sizeof(pneg_inbuf->Dialects[0]) * 2; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1141 | } |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1142 | |
| 1143 | rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID, |
| 1144 | FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 1145 | (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize, |
| 1146 | (char **)&pneg_rsp, &rsplen); |
Namjae Jeon | 969ae8e | 2019-01-22 09:46:45 +0900 | [diff] [blame] | 1147 | if (rc == -EOPNOTSUPP) { |
| 1148 | /* |
| 1149 | * Old Windows versions or Netapp SMB server can return |
| 1150 | * not supported error. Client should accept it. |
| 1151 | */ |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1152 | cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n"); |
Colin Ian King | 2107820 | 2019-05-17 09:12:33 +0100 | [diff] [blame] | 1153 | rc = 0; |
| 1154 | goto out_free_inbuf; |
Namjae Jeon | 969ae8e | 2019-01-22 09:46:45 +0900 | [diff] [blame] | 1155 | } else if (rc != 0) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1156 | cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", |
| 1157 | rc); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1158 | rc = -EIO; |
| 1159 | goto out_free_inbuf; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1160 | } |
| 1161 | |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1162 | rc = -EIO; |
| 1163 | if (rsplen != sizeof(*pneg_rsp)) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1164 | cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n", |
| 1165 | rsplen); |
Steve French | 7db0a6e | 2017-05-03 21:12:20 -0500 | [diff] [blame] | 1166 | |
| 1167 | /* relax check since Mac returns max bufsize allowed on ioctl */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1168 | if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp)) |
| 1169 | goto out_free_rsp; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /* check validate negotiate info response matches what we got earlier */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1173 | if (pneg_rsp->Dialect != cpu_to_le16(server->dialect)) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1174 | goto vneg_out; |
| 1175 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1176 | if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode)) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1177 | goto vneg_out; |
| 1178 | |
| 1179 | /* do not validate server guid because not saved at negprot time yet */ |
| 1180 | |
| 1181 | if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1182 | SMB2_LARGE_FILES) != server->capabilities) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1183 | goto vneg_out; |
| 1184 | |
| 1185 | /* validate negotiate successful */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1186 | rc = 0; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1187 | cifs_dbg(FYI, "validate negotiate info successful\n"); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1188 | goto out_free_rsp; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1189 | |
| 1190 | vneg_out: |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1191 | cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n"); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1192 | out_free_rsp: |
David Disseldorp | fe83bebc | 2017-10-20 14:49:37 +0200 | [diff] [blame] | 1193 | kfree(pneg_rsp); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1194 | out_free_inbuf: |
| 1195 | kfree(pneg_inbuf); |
| 1196 | return rc; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1197 | } |
| 1198 | |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1199 | enum securityEnum |
| 1200 | smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested) |
| 1201 | { |
| 1202 | switch (requested) { |
| 1203 | case Kerberos: |
| 1204 | case RawNTLMSSP: |
| 1205 | return requested; |
| 1206 | case NTLMv2: |
| 1207 | return RawNTLMSSP; |
| 1208 | case Unspecified: |
| 1209 | if (server->sec_ntlmssp && |
| 1210 | (global_secflags & CIFSSEC_MAY_NTLMSSP)) |
| 1211 | return RawNTLMSSP; |
| 1212 | if ((server->sec_kerberos || server->sec_mskerberos) && |
| 1213 | (global_secflags & CIFSSEC_MAY_KRB5)) |
| 1214 | return Kerberos; |
| 1215 | /* Fallthrough */ |
| 1216 | default: |
| 1217 | return Unspecified; |
| 1218 | } |
| 1219 | } |
| 1220 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1221 | struct SMB2_sess_data { |
| 1222 | unsigned int xid; |
| 1223 | struct cifs_ses *ses; |
| 1224 | struct nls_table *nls_cp; |
| 1225 | void (*func)(struct SMB2_sess_data *); |
| 1226 | int result; |
| 1227 | u64 previous_session; |
| 1228 | |
| 1229 | /* we will send the SMB in three pieces: |
| 1230 | * a fixed length beginning part, an optional |
| 1231 | * SPNEGO blob (which can be zero length), and a |
| 1232 | * last part which will include the strings |
| 1233 | * and rest of bcc area. This allows us to avoid |
| 1234 | * a large buffer 17K allocation |
| 1235 | */ |
| 1236 | int buf0_type; |
| 1237 | struct kvec iov[2]; |
| 1238 | }; |
| 1239 | |
| 1240 | static int |
| 1241 | SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data) |
| 1242 | { |
| 1243 | int rc; |
| 1244 | struct cifs_ses *ses = sess_data->ses; |
| 1245 | struct smb2_sess_setup_req *req; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1246 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1247 | unsigned int total_len; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1248 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1249 | rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server, |
| 1250 | (void **) &req, |
| 1251 | &total_len); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1252 | if (rc) |
| 1253 | return rc; |
| 1254 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1255 | if (sess_data->ses->binding) { |
| 1256 | req->sync_hdr.SessionId = sess_data->ses->Suid; |
| 1257 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
| 1258 | req->PreviousSessionId = 0; |
| 1259 | req->Flags = SMB2_SESSION_REQ_FLAG_BINDING; |
| 1260 | } else { |
| 1261 | /* First session, not a reauthenticate */ |
| 1262 | req->sync_hdr.SessionId = 0; |
| 1263 | /* |
| 1264 | * if reconnect, we need to send previous sess id |
| 1265 | * otherwise it is 0 |
| 1266 | */ |
| 1267 | req->PreviousSessionId = sess_data->previous_session; |
| 1268 | req->Flags = 0; /* MBZ */ |
| 1269 | } |
Steve French | d409014 | 2018-06-13 17:05:58 -0500 | [diff] [blame] | 1270 | |
| 1271 | /* enough to enable echos and oplocks and one max size write */ |
| 1272 | req->sync_hdr.CreditRequest = cpu_to_le16(130); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1273 | |
| 1274 | /* only one of SMB2 signing flags may be set in SMB2 request */ |
| 1275 | if (server->sign) |
| 1276 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED; |
| 1277 | else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */ |
| 1278 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED; |
| 1279 | else |
| 1280 | req->SecurityMode = 0; |
| 1281 | |
Steve French | 8d33096 | 2019-07-25 18:13:10 -0500 | [diff] [blame] | 1282 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 1283 | req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS); |
| 1284 | #else |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1285 | req->Capabilities = 0; |
Steve French | 8d33096 | 2019-07-25 18:13:10 -0500 | [diff] [blame] | 1286 | #endif /* DFS_UPCALL */ |
| 1287 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1288 | req->Channel = 0; /* MBZ */ |
| 1289 | |
| 1290 | sess_data->iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1291 | /* 1 for pad */ |
| 1292 | sess_data->iov[0].iov_len = total_len - 1; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1293 | /* |
| 1294 | * This variable will be used to clear the buffer |
| 1295 | * allocated above in case of any error in the calling function. |
| 1296 | */ |
| 1297 | sess_data->buf0_type = CIFS_SMALL_BUFFER; |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | static void |
| 1303 | SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data) |
| 1304 | { |
| 1305 | free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base); |
| 1306 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1307 | } |
| 1308 | |
| 1309 | static int |
| 1310 | SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data) |
| 1311 | { |
| 1312 | int rc; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1313 | struct smb_rqst rqst; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1314 | struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1315 | struct kvec rsp_iov = { NULL, 0 }; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1316 | |
| 1317 | /* Testing shows that buffer offset must be at location of Buffer[0] */ |
| 1318 | req->SecurityBufferOffset = |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1319 | cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1320 | req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len); |
| 1321 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1322 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1323 | rqst.rq_iov = sess_data->iov; |
| 1324 | rqst.rq_nvec = 2; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1325 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1326 | /* BB add code to build os and lm fields */ |
| 1327 | rc = cifs_send_recv(sess_data->xid, sess_data->ses, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1328 | cifs_ses_server(sess_data->ses), |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1329 | &rqst, |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1330 | &sess_data->buf0_type, |
| 1331 | CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1332 | cifs_small_buf_release(sess_data->iov[0].iov_base); |
| 1333 | memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1334 | |
| 1335 | return rc; |
| 1336 | } |
| 1337 | |
| 1338 | static int |
| 1339 | SMB2_sess_establish_session(struct SMB2_sess_data *sess_data) |
| 1340 | { |
| 1341 | int rc = 0; |
| 1342 | struct cifs_ses *ses = sess_data->ses; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1343 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1344 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1345 | mutex_lock(&server->srv_mutex); |
| 1346 | if (server->ops->generate_signingkey) { |
| 1347 | rc = server->ops->generate_signingkey(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1348 | if (rc) { |
| 1349 | cifs_dbg(FYI, |
| 1350 | "SMB3 session key generation failed\n"); |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1351 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | cabfb36 | 2016-11-07 18:20:50 -0800 | [diff] [blame] | 1352 | return rc; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1353 | } |
| 1354 | } |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1355 | if (!server->session_estab) { |
| 1356 | server->sequence_number = 0x2; |
| 1357 | server->session_estab = true; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1358 | } |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1359 | mutex_unlock(&server->srv_mutex); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1360 | |
| 1361 | cifs_dbg(FYI, "SMB2/3 session established successfully\n"); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1362 | /* keep existing ses state if binding */ |
| 1363 | if (!ses->binding) { |
| 1364 | spin_lock(&GlobalMid_Lock); |
| 1365 | ses->status = CifsGood; |
| 1366 | ses->need_reconnect = false; |
| 1367 | spin_unlock(&GlobalMid_Lock); |
| 1368 | } |
| 1369 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1370 | return rc; |
| 1371 | } |
| 1372 | |
| 1373 | #ifdef CONFIG_CIFS_UPCALL |
| 1374 | static void |
| 1375 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) |
| 1376 | { |
| 1377 | int rc; |
| 1378 | struct cifs_ses *ses = sess_data->ses; |
| 1379 | struct cifs_spnego_msg *msg; |
| 1380 | struct key *spnego_key = NULL; |
| 1381 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1382 | |
| 1383 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1384 | if (rc) |
| 1385 | goto out; |
| 1386 | |
| 1387 | spnego_key = cifs_get_spnego_key(ses); |
| 1388 | if (IS_ERR(spnego_key)) { |
| 1389 | rc = PTR_ERR(spnego_key); |
| 1390 | spnego_key = NULL; |
| 1391 | goto out; |
| 1392 | } |
| 1393 | |
| 1394 | msg = spnego_key->payload.data[0]; |
| 1395 | /* |
| 1396 | * check version field to make sure that cifs.upcall is |
| 1397 | * sending us a response in an expected form |
| 1398 | */ |
| 1399 | if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1400 | cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n", |
| 1401 | CIFS_SPNEGO_UPCALL_VERSION, msg->version); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1402 | rc = -EKEYREJECTED; |
| 1403 | goto out_put_spnego_key; |
| 1404 | } |
| 1405 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1406 | /* keep session key if binding */ |
| 1407 | if (!ses->binding) { |
| 1408 | ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len, |
| 1409 | GFP_KERNEL); |
| 1410 | if (!ses->auth_key.response) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1411 | cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n", |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1412 | msg->sesskey_len); |
| 1413 | rc = -ENOMEM; |
| 1414 | goto out_put_spnego_key; |
| 1415 | } |
| 1416 | ses->auth_key.len = msg->sesskey_len; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1417 | } |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1418 | |
| 1419 | sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; |
| 1420 | sess_data->iov[1].iov_len = msg->secblob_len; |
| 1421 | |
| 1422 | rc = SMB2_sess_sendreceive(sess_data); |
| 1423 | if (rc) |
| 1424 | goto out_put_spnego_key; |
| 1425 | |
| 1426 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1427 | /* keep session id and flags if binding */ |
| 1428 | if (!ses->binding) { |
| 1429 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1430 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1431 | } |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1432 | |
| 1433 | rc = SMB2_sess_establish_session(sess_data); |
| 1434 | out_put_spnego_key: |
| 1435 | key_invalidate(spnego_key); |
| 1436 | key_put(spnego_key); |
| 1437 | out: |
| 1438 | sess_data->result = rc; |
| 1439 | sess_data->func = NULL; |
| 1440 | SMB2_sess_free_buffer(sess_data); |
| 1441 | } |
| 1442 | #else |
| 1443 | static void |
| 1444 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) |
| 1445 | { |
| 1446 | cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); |
| 1447 | sess_data->result = -EOPNOTSUPP; |
| 1448 | sess_data->func = NULL; |
| 1449 | } |
| 1450 | #endif |
| 1451 | |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1452 | static void |
| 1453 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data); |
| 1454 | |
| 1455 | static void |
| 1456 | SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data) |
| 1457 | { |
| 1458 | int rc; |
| 1459 | struct cifs_ses *ses = sess_data->ses; |
| 1460 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1461 | char *ntlmssp_blob = NULL; |
| 1462 | bool use_spnego = false; /* else use raw ntlmssp */ |
| 1463 | u16 blob_length = 0; |
| 1464 | |
| 1465 | /* |
| 1466 | * If memory allocation is successful, caller of this function |
| 1467 | * frees it. |
| 1468 | */ |
| 1469 | ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); |
| 1470 | if (!ses->ntlmssp) { |
| 1471 | rc = -ENOMEM; |
| 1472 | goto out_err; |
| 1473 | } |
| 1474 | ses->ntlmssp->sesskey_per_smbsess = true; |
| 1475 | |
| 1476 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1477 | if (rc) |
| 1478 | goto out_err; |
| 1479 | |
| 1480 | ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE), |
| 1481 | GFP_KERNEL); |
| 1482 | if (ntlmssp_blob == NULL) { |
| 1483 | rc = -ENOMEM; |
| 1484 | goto out; |
| 1485 | } |
| 1486 | |
| 1487 | build_ntlmssp_negotiate_blob(ntlmssp_blob, ses); |
| 1488 | if (use_spnego) { |
| 1489 | /* BB eventually need to add this */ |
| 1490 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
| 1491 | rc = -EOPNOTSUPP; |
| 1492 | goto out; |
| 1493 | } else { |
| 1494 | blob_length = sizeof(struct _NEGOTIATE_MESSAGE); |
| 1495 | /* with raw NTLMSSP we don't encapsulate in SPNEGO */ |
| 1496 | } |
| 1497 | sess_data->iov[1].iov_base = ntlmssp_blob; |
| 1498 | sess_data->iov[1].iov_len = blob_length; |
| 1499 | |
| 1500 | rc = SMB2_sess_sendreceive(sess_data); |
| 1501 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
| 1502 | |
| 1503 | /* If true, rc here is expected and not an error */ |
| 1504 | if (sess_data->buf0_type != CIFS_NO_BUFFER && |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1505 | rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1506 | rc = 0; |
| 1507 | |
| 1508 | if (rc) |
| 1509 | goto out; |
| 1510 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 1511 | if (offsetof(struct smb2_sess_setup_rsp, Buffer) != |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1512 | le16_to_cpu(rsp->SecurityBufferOffset)) { |
| 1513 | cifs_dbg(VFS, "Invalid security buffer offset %d\n", |
| 1514 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 1515 | rc = -EIO; |
| 1516 | goto out; |
| 1517 | } |
| 1518 | rc = decode_ntlmssp_challenge(rsp->Buffer, |
| 1519 | le16_to_cpu(rsp->SecurityBufferLength), ses); |
| 1520 | if (rc) |
| 1521 | goto out; |
| 1522 | |
| 1523 | cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); |
| 1524 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1525 | /* keep existing ses id and flags if binding */ |
| 1526 | if (!ses->binding) { |
| 1527 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1528 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1529 | } |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1530 | |
| 1531 | out: |
| 1532 | kfree(ntlmssp_blob); |
| 1533 | SMB2_sess_free_buffer(sess_data); |
| 1534 | if (!rc) { |
| 1535 | sess_data->result = 0; |
| 1536 | sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate; |
| 1537 | return; |
| 1538 | } |
| 1539 | out_err: |
| 1540 | kfree(ses->ntlmssp); |
| 1541 | ses->ntlmssp = NULL; |
| 1542 | sess_data->result = rc; |
| 1543 | sess_data->func = NULL; |
| 1544 | } |
| 1545 | |
| 1546 | static void |
| 1547 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data) |
| 1548 | { |
| 1549 | int rc; |
| 1550 | struct cifs_ses *ses = sess_data->ses; |
| 1551 | struct smb2_sess_setup_req *req; |
| 1552 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1553 | unsigned char *ntlmssp_blob = NULL; |
| 1554 | bool use_spnego = false; /* else use raw ntlmssp */ |
| 1555 | u16 blob_length = 0; |
| 1556 | |
| 1557 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1558 | if (rc) |
| 1559 | goto out; |
| 1560 | |
| 1561 | req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base; |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1562 | req->sync_hdr.SessionId = ses->Suid; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1563 | |
| 1564 | rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses, |
| 1565 | sess_data->nls_cp); |
| 1566 | if (rc) { |
| 1567 | cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc); |
| 1568 | goto out; |
| 1569 | } |
| 1570 | |
| 1571 | if (use_spnego) { |
| 1572 | /* BB eventually need to add this */ |
| 1573 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
| 1574 | rc = -EOPNOTSUPP; |
| 1575 | goto out; |
| 1576 | } |
| 1577 | sess_data->iov[1].iov_base = ntlmssp_blob; |
| 1578 | sess_data->iov[1].iov_len = blob_length; |
| 1579 | |
| 1580 | rc = SMB2_sess_sendreceive(sess_data); |
| 1581 | if (rc) |
| 1582 | goto out; |
| 1583 | |
| 1584 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
| 1585 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1586 | /* keep existing ses id and flags if binding */ |
| 1587 | if (!ses->binding) { |
| 1588 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1589 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1590 | } |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1591 | |
| 1592 | rc = SMB2_sess_establish_session(sess_data); |
Ronnie Sahlberg | f560cda | 2020-04-12 16:09:26 +1000 | [diff] [blame] | 1593 | #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS |
| 1594 | if (ses->server->dialect < SMB30_PROT_ID) { |
| 1595 | cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__); |
| 1596 | /* |
| 1597 | * The session id is opaque in terms of endianness, so we can't |
| 1598 | * print it as a long long. we dump it as we got it on the wire |
| 1599 | */ |
| 1600 | cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid), |
| 1601 | &ses->Suid); |
| 1602 | cifs_dbg(VFS, "Session Key %*ph\n", |
| 1603 | SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response); |
| 1604 | cifs_dbg(VFS, "Signing Key %*ph\n", |
| 1605 | SMB3_SIGN_KEY_SIZE, ses->auth_key.response); |
| 1606 | } |
| 1607 | #endif |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1608 | out: |
| 1609 | kfree(ntlmssp_blob); |
| 1610 | SMB2_sess_free_buffer(sess_data); |
| 1611 | kfree(ses->ntlmssp); |
| 1612 | ses->ntlmssp = NULL; |
| 1613 | sess_data->result = rc; |
| 1614 | sess_data->func = NULL; |
| 1615 | } |
| 1616 | |
| 1617 | static int |
| 1618 | SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data) |
| 1619 | { |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1620 | int type; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1621 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1622 | type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype); |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1623 | cifs_dbg(FYI, "sess setup type %d\n", type); |
| 1624 | if (type == Unspecified) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1625 | cifs_dbg(VFS, "Unable to select appropriate authentication method!\n"); |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1626 | return -EINVAL; |
| 1627 | } |
| 1628 | |
| 1629 | switch (type) { |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1630 | case Kerberos: |
| 1631 | sess_data->func = SMB2_auth_kerberos; |
| 1632 | break; |
| 1633 | case RawNTLMSSP: |
| 1634 | sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate; |
| 1635 | break; |
| 1636 | default: |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1637 | cifs_dbg(VFS, "secType %d not supported!\n", type); |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1638 | return -EOPNOTSUPP; |
| 1639 | } |
| 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1644 | int |
| 1645 | SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, |
| 1646 | const struct nls_table *nls_cp) |
| 1647 | { |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1648 | int rc = 0; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1649 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1650 | struct SMB2_sess_data *sess_data; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1651 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1652 | cifs_dbg(FYI, "Session Setup\n"); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1653 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 1654 | if (!server) { |
| 1655 | WARN(1, "%s: server is NULL!\n", __func__); |
| 1656 | return -EIO; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1657 | } |
| 1658 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1659 | sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL); |
| 1660 | if (!sess_data) |
| 1661 | return -ENOMEM; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1662 | |
| 1663 | rc = SMB2_select_sec(ses, sess_data); |
| 1664 | if (rc) |
| 1665 | goto out; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1666 | sess_data->xid = xid; |
| 1667 | sess_data->ses = ses; |
| 1668 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1669 | sess_data->nls_cp = (struct nls_table *) nls_cp; |
Steve French | b2adf22f | 2018-05-31 15:19:25 -0500 | [diff] [blame] | 1670 | sess_data->previous_session = ses->Suid; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1671 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1672 | /* |
| 1673 | * Initialize the session hash with the server one. |
| 1674 | */ |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1675 | memcpy(ses->preauth_sha_hash, server->preauth_sha_hash, |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1676 | SMB2_PREAUTH_HASH_SIZE); |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1677 | |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1678 | while (sess_data->func) |
| 1679 | sess_data->func(sess_data); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1680 | |
Steve French | c721c38 | 2017-09-19 18:40:03 -0500 | [diff] [blame] | 1681 | if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign)) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1682 | cifs_server_dbg(VFS, "signing requested but authenticated as guest\n"); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1683 | rc = sess_data->result; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1684 | out: |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1685 | kfree(sess_data); |
| 1686 | return rc; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | int |
| 1690 | SMB2_logoff(const unsigned int xid, struct cifs_ses *ses) |
| 1691 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1692 | struct smb_rqst rqst; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1693 | struct smb2_logoff_req *req; /* response is also trivial struct */ |
| 1694 | int rc = 0; |
| 1695 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1696 | int flags = 0; |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1697 | unsigned int total_len; |
| 1698 | struct kvec iov[1]; |
| 1699 | struct kvec rsp_iov; |
| 1700 | int resp_buf_type; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1701 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1702 | cifs_dbg(FYI, "disconnect session %p\n", ses); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1703 | |
| 1704 | if (ses && (ses->server)) |
| 1705 | server = ses->server; |
| 1706 | else |
| 1707 | return -EIO; |
| 1708 | |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 1709 | /* no need to send SMB logoff if uid already closed due to reconnect */ |
| 1710 | if (ses->need_reconnect) |
| 1711 | goto smb2_session_already_dead; |
| 1712 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1713 | rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server, |
| 1714 | (void **) &req, &total_len); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1715 | if (rc) |
| 1716 | return rc; |
| 1717 | |
| 1718 | /* since no tcon, smb2_init can not do this, so do here */ |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1719 | req->sync_hdr.SessionId = ses->Suid; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1720 | |
| 1721 | if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) |
| 1722 | flags |= CIFS_TRANSFORM_REQ; |
| 1723 | else if (server->sign) |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1724 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1725 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 1726 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1727 | |
| 1728 | iov[0].iov_base = (char *)req; |
| 1729 | iov[0].iov_len = total_len; |
| 1730 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1731 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1732 | rqst.rq_iov = iov; |
| 1733 | rqst.rq_nvec = 1; |
| 1734 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1735 | rc = cifs_send_recv(xid, ses, ses->server, |
| 1736 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1737 | cifs_small_buf_release(req); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1738 | /* |
| 1739 | * No tcon so can't do |
| 1740 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); |
| 1741 | */ |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 1742 | |
| 1743 | smb2_session_already_dead: |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1744 | return rc; |
| 1745 | } |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1746 | |
| 1747 | static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code) |
| 1748 | { |
Pavel Shilovsky | d60622e | 2012-05-28 15:19:39 +0400 | [diff] [blame] | 1749 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */) |
| 1753 | |
Steve French | de9f68df | 2013-11-15 11:26:24 -0600 | [diff] [blame] | 1754 | /* These are similar values to what Windows uses */ |
| 1755 | static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon) |
| 1756 | { |
| 1757 | tcon->max_chunks = 256; |
| 1758 | tcon->max_bytes_chunk = 1048576; |
| 1759 | tcon->max_bytes_copy = 16777216; |
| 1760 | } |
| 1761 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1762 | int |
| 1763 | SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, |
| 1764 | struct cifs_tcon *tcon, const struct nls_table *cp) |
| 1765 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1766 | struct smb_rqst rqst; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1767 | struct smb2_tree_connect_req *req; |
| 1768 | struct smb2_tree_connect_rsp *rsp = NULL; |
| 1769 | struct kvec iov[2]; |
Aurélien Aptel | db3b547 | 2017-10-11 13:23:36 +0200 | [diff] [blame] | 1770 | struct kvec rsp_iov = { NULL, 0 }; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1771 | int rc = 0; |
| 1772 | int resp_buftype; |
| 1773 | int unc_path_len; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1774 | __le16 *unc_path = NULL; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1775 | int flags = 0; |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1776 | unsigned int total_len; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1777 | struct TCP_Server_Info *server; |
| 1778 | |
| 1779 | /* always use master channel */ |
| 1780 | server = ses->server; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1781 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1782 | cifs_dbg(FYI, "TCON\n"); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1783 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1784 | if (!server || !tree) |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1785 | return -EIO; |
| 1786 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1787 | unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL); |
| 1788 | if (unc_path == NULL) |
| 1789 | return -ENOMEM; |
| 1790 | |
| 1791 | unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1; |
| 1792 | unc_path_len *= 2; |
| 1793 | if (unc_path_len < 2) { |
| 1794 | kfree(unc_path); |
| 1795 | return -EINVAL; |
| 1796 | } |
| 1797 | |
Jan-Marek Glogowski | 806a28e | 2017-02-20 12:25:58 +0100 | [diff] [blame] | 1798 | /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1799 | tcon->tid = 0; |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 1800 | atomic_set(&tcon->num_remote_opens, 0); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1801 | rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server, |
| 1802 | (void **) &req, &total_len); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1803 | if (rc) { |
| 1804 | kfree(unc_path); |
| 1805 | return rc; |
| 1806 | } |
| 1807 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 1808 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1809 | flags |= CIFS_TRANSFORM_REQ; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1810 | |
| 1811 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1812 | /* 1 for pad */ |
| 1813 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1814 | |
| 1815 | /* Testing shows that buffer offset must be at location of Buffer[0] */ |
| 1816 | req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req) |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1817 | - 1 /* pad */); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1818 | req->PathLength = cpu_to_le16(unc_path_len - 2); |
| 1819 | iov[1].iov_base = unc_path; |
| 1820 | iov[1].iov_len = unc_path_len; |
| 1821 | |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1822 | /* |
| 1823 | * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 |
| 1824 | * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1 |
Steve French | 8c11a60 | 2019-03-22 22:31:17 -0500 | [diff] [blame] | 1825 | * (Samba servers don't always set the flag so also check if null user) |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1826 | */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1827 | if ((server->dialect == SMB311_PROT_ID) && |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1828 | !smb3_encryption_required(tcon) && |
Steve French | 8c11a60 | 2019-03-22 22:31:17 -0500 | [diff] [blame] | 1829 | !(ses->session_flags & |
| 1830 | (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) && |
| 1831 | ((ses->user_name != NULL) || (ses->sectype == Kerberos))) |
Steve French | 6188f28 | 2018-03-13 02:29:36 -0500 | [diff] [blame] | 1832 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
| 1833 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1834 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1835 | rqst.rq_iov = iov; |
| 1836 | rqst.rq_nvec = 2; |
| 1837 | |
Steve French | 4fe75c4 | 2019-02-14 01:19:02 -0600 | [diff] [blame] | 1838 | /* Need 64 for max size write so ask for more in case not there yet */ |
| 1839 | req->sync_hdr.CreditRequest = cpu_to_le16(64); |
| 1840 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1841 | rc = cifs_send_recv(xid, ses, server, |
| 1842 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1843 | cifs_small_buf_release(req); |
| 1844 | rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base; |
Steve French | f8af49d | 2018-10-28 00:47:11 -0500 | [diff] [blame] | 1845 | trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1846 | if (rc != 0) { |
| 1847 | if (tcon) { |
| 1848 | cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE); |
| 1849 | tcon->need_reconnect = true; |
| 1850 | } |
| 1851 | goto tcon_error_exit; |
| 1852 | } |
| 1853 | |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1854 | switch (rsp->ShareType) { |
| 1855 | case SMB2_SHARE_TYPE_DISK: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1856 | cifs_dbg(FYI, "connection to disk share\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1857 | break; |
| 1858 | case SMB2_SHARE_TYPE_PIPE: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1859 | tcon->pipe = true; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1860 | cifs_dbg(FYI, "connection to pipe share\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1861 | break; |
| 1862 | case SMB2_SHARE_TYPE_PRINT: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1863 | tcon->print = true; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1864 | cifs_dbg(FYI, "connection to printer\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1865 | break; |
| 1866 | default: |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1867 | cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1868 | rc = -EOPNOTSUPP; |
| 1869 | goto tcon_error_exit; |
| 1870 | } |
| 1871 | |
| 1872 | tcon->share_flags = le32_to_cpu(rsp->ShareFlags); |
Steve French | 769ee6a | 2013-06-19 14:15:30 -0500 | [diff] [blame] | 1873 | tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */ |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1874 | tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1875 | tcon->tidStatus = CifsGood; |
| 1876 | tcon->need_reconnect = false; |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1877 | tcon->tid = rsp->sync_hdr.TreeId; |
Zhao Hongjiang | 46b51d0 | 2013-06-24 01:57:47 -0500 | [diff] [blame] | 1878 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1879 | |
| 1880 | if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) && |
| 1881 | ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0)) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1882 | cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n"); |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1883 | |
| 1884 | if (tcon->seal && |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1885 | !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1886 | cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n"); |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1887 | |
Steve French | de9f68df | 2013-11-15 11:26:24 -0600 | [diff] [blame] | 1888 | init_copy_chunk_defaults(tcon); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1889 | if (server->ops->validate_negotiate) |
| 1890 | rc = server->ops->validate_negotiate(xid, tcon); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1891 | tcon_exit: |
Steve French | f8af49d | 2018-10-28 00:47:11 -0500 | [diff] [blame] | 1892 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1893 | free_rsp_buf(resp_buftype, rsp); |
| 1894 | kfree(unc_path); |
| 1895 | return rc; |
| 1896 | |
| 1897 | tcon_error_exit: |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1898 | if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1899 | cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1900 | } |
| 1901 | goto tcon_exit; |
| 1902 | } |
| 1903 | |
| 1904 | int |
| 1905 | SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) |
| 1906 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1907 | struct smb_rqst rqst; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1908 | struct smb2_tree_disconnect_req *req; /* response is trivial */ |
| 1909 | int rc = 0; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1910 | struct cifs_ses *ses = tcon->ses; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1911 | int flags = 0; |
Ronnie Sahlberg | 4eecf4c | 2017-11-09 12:14:18 +1100 | [diff] [blame] | 1912 | unsigned int total_len; |
| 1913 | struct kvec iov[1]; |
| 1914 | struct kvec rsp_iov; |
| 1915 | int resp_buf_type; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1916 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1917 | cifs_dbg(FYI, "Tree Disconnect\n"); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1918 | |
Christos Gkekas | 68a6afa | 2017-07-09 11:45:04 +0100 | [diff] [blame] | 1919 | if (!ses || !(ses->server)) |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1920 | return -EIO; |
| 1921 | |
| 1922 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) |
| 1923 | return 0; |
| 1924 | |
Pavel Shilovsky | d919131 | 2019-12-10 11:44:52 -0800 | [diff] [blame] | 1925 | close_shroot_lease(&tcon->crfid); |
Ronnie Sahlberg | 72e73c7 | 2019-11-07 17:00:38 +1000 | [diff] [blame] | 1926 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1927 | rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server, |
| 1928 | (void **) &req, |
| 1929 | &total_len); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1930 | if (rc) |
| 1931 | return rc; |
| 1932 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 1933 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1934 | flags |= CIFS_TRANSFORM_REQ; |
| 1935 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 1936 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 4eecf4c | 2017-11-09 12:14:18 +1100 | [diff] [blame] | 1937 | |
| 1938 | iov[0].iov_base = (char *)req; |
| 1939 | iov[0].iov_len = total_len; |
| 1940 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1941 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1942 | rqst.rq_iov = iov; |
| 1943 | rqst.rq_nvec = 1; |
| 1944 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1945 | rc = cifs_send_recv(xid, ses, ses->server, |
| 1946 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1947 | cifs_small_buf_release(req); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1948 | if (rc) |
| 1949 | cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE); |
| 1950 | |
| 1951 | return rc; |
| 1952 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 1953 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1954 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1955 | static struct create_durable * |
| 1956 | create_durable_buf(void) |
| 1957 | { |
| 1958 | struct create_durable *buf; |
| 1959 | |
| 1960 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); |
| 1961 | if (!buf) |
| 1962 | return NULL; |
| 1963 | |
| 1964 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1965 | (struct create_durable, Data)); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1966 | buf->ccontext.DataLength = cpu_to_le32(16); |
| 1967 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1968 | (struct create_durable, Name)); |
| 1969 | buf->ccontext.NameLength = cpu_to_le16(4); |
Steve French | 12197a7 | 2014-05-14 05:29:40 -0700 | [diff] [blame] | 1970 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */ |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1971 | buf->Name[0] = 'D'; |
| 1972 | buf->Name[1] = 'H'; |
| 1973 | buf->Name[2] = 'n'; |
| 1974 | buf->Name[3] = 'Q'; |
| 1975 | return buf; |
| 1976 | } |
| 1977 | |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1978 | static struct create_durable * |
| 1979 | create_reconnect_durable_buf(struct cifs_fid *fid) |
| 1980 | { |
| 1981 | struct create_durable *buf; |
| 1982 | |
| 1983 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); |
| 1984 | if (!buf) |
| 1985 | return NULL; |
| 1986 | |
| 1987 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1988 | (struct create_durable, Data)); |
| 1989 | buf->ccontext.DataLength = cpu_to_le32(16); |
| 1990 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1991 | (struct create_durable, Name)); |
| 1992 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1993 | buf->Data.Fid.PersistentFileId = fid->persistent_fid; |
| 1994 | buf->Data.Fid.VolatileFileId = fid->volatile_fid; |
Steve French | 12197a7 | 2014-05-14 05:29:40 -0700 | [diff] [blame] | 1995 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */ |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1996 | buf->Name[0] = 'D'; |
| 1997 | buf->Name[1] = 'H'; |
| 1998 | buf->Name[2] = 'n'; |
| 1999 | buf->Name[3] = 'C'; |
| 2000 | return buf; |
| 2001 | } |
| 2002 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2003 | static void |
| 2004 | parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf) |
| 2005 | { |
| 2006 | struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc; |
| 2007 | |
| 2008 | cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n", |
| 2009 | pdisk_id->DiskFileId, pdisk_id->VolumeId); |
| 2010 | buf->IndexNumber = pdisk_id->DiskFileId; |
| 2011 | } |
| 2012 | |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2013 | static void |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2014 | parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info, |
| 2015 | struct create_posix_rsp *posix) |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2016 | { |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2017 | int sid_len; |
| 2018 | u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset); |
| 2019 | u8 *end = beg + le32_to_cpu(cc->DataLength); |
| 2020 | u8 *sid; |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2021 | |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2022 | memset(posix, 0, sizeof(*posix)); |
Aurelien Aptel | 2e8af97 | 2020-02-08 15:50:56 +0100 | [diff] [blame] | 2023 | |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2024 | posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0)); |
| 2025 | posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4)); |
| 2026 | posix->mode = le32_to_cpu(*(__le32 *)(beg + 8)); |
| 2027 | |
| 2028 | sid = beg + 12; |
| 2029 | sid_len = posix_info_sid_size(sid, end); |
| 2030 | if (sid_len < 0) { |
| 2031 | cifs_dbg(VFS, "bad owner sid in posix create response\n"); |
| 2032 | return; |
| 2033 | } |
| 2034 | memcpy(&posix->owner, sid, sid_len); |
| 2035 | |
| 2036 | sid = sid + sid_len; |
| 2037 | sid_len = posix_info_sid_size(sid, end); |
| 2038 | if (sid_len < 0) { |
| 2039 | cifs_dbg(VFS, "bad group sid in posix create response\n"); |
| 2040 | return; |
| 2041 | } |
| 2042 | memcpy(&posix->group, sid, sid_len); |
| 2043 | |
| 2044 | cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n", |
| 2045 | posix->nlink, posix->mode, posix->reparse_tag); |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2046 | } |
| 2047 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2048 | void |
| 2049 | smb2_parse_contexts(struct TCP_Server_Info *server, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2050 | struct smb2_create_rsp *rsp, |
| 2051 | unsigned int *epoch, char *lease_key, __u8 *oplock, |
| 2052 | struct smb2_file_all_info *buf, |
| 2053 | struct create_posix_rsp *posix) |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2054 | { |
| 2055 | char *data_offset; |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 2056 | struct create_context *cc; |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 2057 | unsigned int next; |
| 2058 | unsigned int remaining; |
Pavel Shilovsky | fd55439 | 2013-07-09 19:44:56 +0400 | [diff] [blame] | 2059 | char *name; |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2060 | const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C, |
| 2061 | 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83, |
| 2062 | 0xDE, 0x96, 0x8B, 0xCD, 0x7C}; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2063 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2064 | *oplock = 0; |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 2065 | data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset); |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 2066 | remaining = le32_to_cpu(rsp->CreateContextsLength); |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 2067 | cc = (struct create_context *)data_offset; |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2068 | |
| 2069 | /* Initialize inode number to 0 in case no valid data in qfid context */ |
| 2070 | if (buf) |
| 2071 | buf->IndexNumber = 0; |
| 2072 | |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 2073 | while (remaining >= sizeof(struct create_context)) { |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 2074 | name = le16_to_cpu(cc->NameOffset) + (char *)cc; |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 2075 | if (le16_to_cpu(cc->NameLength) == 4 && |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2076 | strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0) |
| 2077 | *oplock = server->ops->parse_lease_buf(cc, epoch, |
| 2078 | lease_key); |
| 2079 | else if (buf && (le16_to_cpu(cc->NameLength) == 4) && |
| 2080 | strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0) |
| 2081 | parse_query_id_ctxt(cc, buf); |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2082 | else if ((le16_to_cpu(cc->NameLength) == 16)) { |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2083 | if (posix && |
| 2084 | memcmp(name, smb3_create_tag_posix, 16) == 0) |
| 2085 | parse_posix_ctxt(cc, buf, posix); |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2086 | } |
| 2087 | /* else { |
| 2088 | cifs_dbg(FYI, "Context not matched with len %d\n", |
| 2089 | le16_to_cpu(cc->NameLength)); |
| 2090 | cifs_dump_mem("Cctxt name: ", name, 4); |
| 2091 | } */ |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 2092 | |
| 2093 | next = le32_to_cpu(cc->Next); |
| 2094 | if (!next) |
| 2095 | break; |
| 2096 | remaining -= next; |
| 2097 | cc = (struct create_context *)((char *)cc + next); |
| 2098 | } |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2099 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2100 | if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) |
| 2101 | *oplock = rsp->OplockLevel; |
| 2102 | |
| 2103 | return; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2106 | static int |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2107 | add_lease_context(struct TCP_Server_Info *server, struct kvec *iov, |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2108 | unsigned int *num_iovec, u8 *lease_key, __u8 *oplock) |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2109 | { |
| 2110 | struct smb2_create_req *req = iov[0].iov_base; |
| 2111 | unsigned int num = *num_iovec; |
| 2112 | |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2113 | iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock); |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2114 | if (iov[num].iov_base == NULL) |
| 2115 | return -ENOMEM; |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2116 | iov[num].iov_len = server->vals->create_lease_size; |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2117 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 2118 | if (!req->CreateContextsOffset) |
| 2119 | req->CreateContextsOffset = cpu_to_le32( |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2120 | sizeof(struct smb2_create_req) + |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2121 | iov[num - 1].iov_len); |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2122 | le32_add_cpu(&req->CreateContextsLength, |
| 2123 | server->vals->create_lease_size); |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2124 | *num_iovec = num + 1; |
| 2125 | return 0; |
| 2126 | } |
| 2127 | |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2128 | static struct create_durable_v2 * |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2129 | create_durable_v2_buf(struct cifs_open_parms *oparms) |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2130 | { |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2131 | struct cifs_fid *pfid = oparms->fid; |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2132 | struct create_durable_v2 *buf; |
| 2133 | |
| 2134 | buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL); |
| 2135 | if (!buf) |
| 2136 | return NULL; |
| 2137 | |
| 2138 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2139 | (struct create_durable_v2, dcontext)); |
| 2140 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2)); |
| 2141 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2142 | (struct create_durable_v2, Name)); |
| 2143 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2144 | |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2145 | /* |
| 2146 | * NB: Handle timeout defaults to 0, which allows server to choose |
| 2147 | * (most servers default to 120 seconds) and most clients default to 0. |
| 2148 | * This can be overridden at mount ("handletimeout=") if the user wants |
| 2149 | * a different persistent (or resilient) handle timeout for all opens |
| 2150 | * opens on a particular SMB3 mount. |
| 2151 | */ |
| 2152 | buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2153 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); |
Steve French | fa70b87 | 2016-09-22 00:39:34 -0500 | [diff] [blame] | 2154 | generate_random_uuid(buf->dcontext.CreateGuid); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2155 | memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16); |
| 2156 | |
| 2157 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */ |
| 2158 | buf->Name[0] = 'D'; |
| 2159 | buf->Name[1] = 'H'; |
| 2160 | buf->Name[2] = '2'; |
| 2161 | buf->Name[3] = 'Q'; |
| 2162 | return buf; |
| 2163 | } |
| 2164 | |
| 2165 | static struct create_durable_handle_reconnect_v2 * |
| 2166 | create_reconnect_durable_v2_buf(struct cifs_fid *fid) |
| 2167 | { |
| 2168 | struct create_durable_handle_reconnect_v2 *buf; |
| 2169 | |
| 2170 | buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2), |
| 2171 | GFP_KERNEL); |
| 2172 | if (!buf) |
| 2173 | return NULL; |
| 2174 | |
| 2175 | buf->ccontext.DataOffset = |
| 2176 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, |
| 2177 | dcontext)); |
| 2178 | buf->ccontext.DataLength = |
| 2179 | cpu_to_le32(sizeof(struct durable_reconnect_context_v2)); |
| 2180 | buf->ccontext.NameOffset = |
| 2181 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, |
| 2182 | Name)); |
| 2183 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2184 | |
| 2185 | buf->dcontext.Fid.PersistentFileId = fid->persistent_fid; |
| 2186 | buf->dcontext.Fid.VolatileFileId = fid->volatile_fid; |
| 2187 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); |
| 2188 | memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16); |
| 2189 | |
| 2190 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */ |
| 2191 | buf->Name[0] = 'D'; |
| 2192 | buf->Name[1] = 'H'; |
| 2193 | buf->Name[2] = '2'; |
| 2194 | buf->Name[3] = 'C'; |
| 2195 | return buf; |
| 2196 | } |
| 2197 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2198 | static int |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2199 | add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec, |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 2200 | struct cifs_open_parms *oparms) |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2201 | { |
| 2202 | struct smb2_create_req *req = iov[0].iov_base; |
| 2203 | unsigned int num = *num_iovec; |
| 2204 | |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2205 | iov[num].iov_base = create_durable_v2_buf(oparms); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2206 | if (iov[num].iov_base == NULL) |
| 2207 | return -ENOMEM; |
| 2208 | iov[num].iov_len = sizeof(struct create_durable_v2); |
| 2209 | if (!req->CreateContextsOffset) |
| 2210 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2211 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2212 | iov[1].iov_len); |
| 2213 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2)); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2214 | *num_iovec = num + 1; |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | static int |
| 2219 | add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec, |
| 2220 | struct cifs_open_parms *oparms) |
| 2221 | { |
| 2222 | struct smb2_create_req *req = iov[0].iov_base; |
| 2223 | unsigned int num = *num_iovec; |
| 2224 | |
| 2225 | /* indicate that we don't need to relock the file */ |
| 2226 | oparms->reconnect = false; |
| 2227 | |
| 2228 | iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid); |
| 2229 | if (iov[num].iov_base == NULL) |
| 2230 | return -ENOMEM; |
| 2231 | iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2); |
| 2232 | if (!req->CreateContextsOffset) |
| 2233 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2234 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2235 | iov[1].iov_len); |
| 2236 | le32_add_cpu(&req->CreateContextsLength, |
| 2237 | sizeof(struct create_durable_handle_reconnect_v2)); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2238 | *num_iovec = num + 1; |
| 2239 | return 0; |
| 2240 | } |
| 2241 | |
| 2242 | static int |
| 2243 | add_durable_context(struct kvec *iov, unsigned int *num_iovec, |
| 2244 | struct cifs_open_parms *oparms, bool use_persistent) |
| 2245 | { |
| 2246 | struct smb2_create_req *req = iov[0].iov_base; |
| 2247 | unsigned int num = *num_iovec; |
| 2248 | |
| 2249 | if (use_persistent) { |
| 2250 | if (oparms->reconnect) |
| 2251 | return add_durable_reconnect_v2_context(iov, num_iovec, |
| 2252 | oparms); |
| 2253 | else |
| 2254 | return add_durable_v2_context(iov, num_iovec, oparms); |
| 2255 | } |
| 2256 | |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 2257 | if (oparms->reconnect) { |
| 2258 | iov[num].iov_base = create_reconnect_durable_buf(oparms->fid); |
| 2259 | /* indicate that we don't need to relock the file */ |
| 2260 | oparms->reconnect = false; |
| 2261 | } else |
| 2262 | iov[num].iov_base = create_durable_buf(); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2263 | if (iov[num].iov_base == NULL) |
| 2264 | return -ENOMEM; |
| 2265 | iov[num].iov_len = sizeof(struct create_durable); |
| 2266 | if (!req->CreateContextsOffset) |
| 2267 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2268 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2269 | iov[1].iov_len); |
Wei Yongjun | 31f92e9 | 2013-08-26 14:34:46 +0800 | [diff] [blame] | 2270 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable)); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2271 | *num_iovec = num + 1; |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2275 | /* See MS-SMB2 2.2.13.2.7 */ |
| 2276 | static struct crt_twarp_ctxt * |
| 2277 | create_twarp_buf(__u64 timewarp) |
| 2278 | { |
| 2279 | struct crt_twarp_ctxt *buf; |
| 2280 | |
| 2281 | buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL); |
| 2282 | if (!buf) |
| 2283 | return NULL; |
| 2284 | |
| 2285 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2286 | (struct crt_twarp_ctxt, Timestamp)); |
| 2287 | buf->ccontext.DataLength = cpu_to_le32(8); |
| 2288 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2289 | (struct crt_twarp_ctxt, Name)); |
| 2290 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2291 | /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */ |
| 2292 | buf->Name[0] = 'T'; |
| 2293 | buf->Name[1] = 'W'; |
| 2294 | buf->Name[2] = 'r'; |
| 2295 | buf->Name[3] = 'p'; |
| 2296 | buf->Timestamp = cpu_to_le64(timewarp); |
| 2297 | return buf; |
| 2298 | } |
| 2299 | |
| 2300 | /* See MS-SMB2 2.2.13.2.7 */ |
| 2301 | static int |
| 2302 | add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp) |
| 2303 | { |
| 2304 | struct smb2_create_req *req = iov[0].iov_base; |
| 2305 | unsigned int num = *num_iovec; |
| 2306 | |
| 2307 | iov[num].iov_base = create_twarp_buf(timewarp); |
| 2308 | if (iov[num].iov_base == NULL) |
| 2309 | return -ENOMEM; |
| 2310 | iov[num].iov_len = sizeof(struct crt_twarp_ctxt); |
| 2311 | if (!req->CreateContextsOffset) |
| 2312 | req->CreateContextsOffset = cpu_to_le32( |
| 2313 | sizeof(struct smb2_create_req) + |
| 2314 | iov[num - 1].iov_len); |
| 2315 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt)); |
| 2316 | *num_iovec = num + 1; |
| 2317 | return 0; |
| 2318 | } |
| 2319 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2320 | /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */ |
| 2321 | static void setup_owner_group_sids(char *buf) |
| 2322 | { |
| 2323 | struct owner_group_sids *sids = (struct owner_group_sids *)buf; |
| 2324 | |
| 2325 | /* Populate the user ownership fields S-1-5-88-1 */ |
| 2326 | sids->owner.Revision = 1; |
| 2327 | sids->owner.NumAuth = 3; |
| 2328 | sids->owner.Authority[5] = 5; |
| 2329 | sids->owner.SubAuthorities[0] = cpu_to_le32(88); |
| 2330 | sids->owner.SubAuthorities[1] = cpu_to_le32(1); |
| 2331 | sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val); |
| 2332 | |
| 2333 | /* Populate the group ownership fields S-1-5-88-2 */ |
| 2334 | sids->group.Revision = 1; |
| 2335 | sids->group.NumAuth = 3; |
| 2336 | sids->group.Authority[5] = 5; |
| 2337 | sids->group.SubAuthorities[0] = cpu_to_le32(88); |
| 2338 | sids->group.SubAuthorities[1] = cpu_to_le32(2); |
| 2339 | sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val); |
Steve French | a7a519a | 2020-06-12 14:49:47 -0500 | [diff] [blame] | 2340 | |
| 2341 | cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2342 | } |
| 2343 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2344 | /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */ |
| 2345 | static struct crt_sd_ctxt * |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2346 | create_sd_buf(umode_t mode, bool set_owner, unsigned int *len) |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2347 | { |
| 2348 | struct crt_sd_ctxt *buf; |
| 2349 | struct cifs_ace *pace; |
| 2350 | unsigned int sdlen, acelen; |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2351 | unsigned int owner_offset = 0; |
| 2352 | unsigned int group_offset = 0; |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2353 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2354 | *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 2), 8); |
| 2355 | |
| 2356 | if (set_owner) { |
| 2357 | /* offset fields are from beginning of security descriptor not of create context */ |
| 2358 | owner_offset = sizeof(struct smb3_acl) + (sizeof(struct cifs_ace) * 2); |
| 2359 | |
| 2360 | /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */ |
| 2361 | *len += sizeof(struct owner_group_sids); |
| 2362 | } |
| 2363 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2364 | buf = kzalloc(*len, GFP_KERNEL); |
| 2365 | if (buf == NULL) |
| 2366 | return buf; |
| 2367 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2368 | if (set_owner) { |
| 2369 | buf->sd.OffsetOwner = cpu_to_le32(owner_offset); |
| 2370 | group_offset = owner_offset + sizeof(struct owner_sid); |
| 2371 | buf->sd.OffsetGroup = cpu_to_le32(group_offset); |
| 2372 | } else { |
| 2373 | buf->sd.OffsetOwner = 0; |
| 2374 | buf->sd.OffsetGroup = 0; |
| 2375 | } |
| 2376 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2377 | sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) + |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2378 | 2 * sizeof(struct cifs_ace); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2379 | if (set_owner) { |
| 2380 | sdlen += sizeof(struct owner_group_sids); |
| 2381 | setup_owner_group_sids(owner_offset + sizeof(struct create_context) + 8 /* name */ |
| 2382 | + (char *)buf); |
| 2383 | } |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2384 | |
| 2385 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2386 | (struct crt_sd_ctxt, sd)); |
| 2387 | buf->ccontext.DataLength = cpu_to_le32(sdlen); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2388 | buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name)); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2389 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2390 | /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */ |
| 2391 | buf->Name[0] = 'S'; |
| 2392 | buf->Name[1] = 'e'; |
| 2393 | buf->Name[2] = 'c'; |
| 2394 | buf->Name[3] = 'D'; |
| 2395 | buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */ |
| 2396 | /* |
| 2397 | * ACL is "self relative" ie ACL is stored in contiguous block of memory |
| 2398 | * and "DP" ie the DACL is present |
| 2399 | */ |
| 2400 | buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP); |
| 2401 | |
| 2402 | /* offset owner, group and Sbz1 and SACL are all zero */ |
| 2403 | buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd)); |
| 2404 | buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */ |
| 2405 | |
| 2406 | /* create one ACE to hold the mode embedded in reserved special SID */ |
| 2407 | pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf); |
| 2408 | acelen = setup_special_mode_ACE(pace, (__u64)mode); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2409 | |
| 2410 | if (set_owner) { |
| 2411 | /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */ |
| 2412 | pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) + (char *)buf)); |
| 2413 | acelen += setup_special_user_owner_ACE(pace); |
| 2414 | /* it does not appear necessary to add an ACE for the NFS group SID */ |
| 2415 | buf->acl.AceCount = cpu_to_le16(3); |
| 2416 | } else |
| 2417 | buf->acl.AceCount = cpu_to_le16(2); |
| 2418 | |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2419 | /* and one more ACE to allow access for authenticated users */ |
| 2420 | pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) + |
| 2421 | (char *)buf)); |
| 2422 | acelen += setup_authusers_ACE(pace); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2423 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2424 | buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2425 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2426 | return buf; |
| 2427 | } |
| 2428 | |
| 2429 | static int |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2430 | add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner) |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2431 | { |
| 2432 | struct smb2_create_req *req = iov[0].iov_base; |
| 2433 | unsigned int num = *num_iovec; |
| 2434 | unsigned int len = 0; |
| 2435 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2436 | iov[num].iov_base = create_sd_buf(mode, set_owner, &len); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2437 | if (iov[num].iov_base == NULL) |
| 2438 | return -ENOMEM; |
| 2439 | iov[num].iov_len = len; |
| 2440 | if (!req->CreateContextsOffset) |
| 2441 | req->CreateContextsOffset = cpu_to_le32( |
| 2442 | sizeof(struct smb2_create_req) + |
| 2443 | iov[num - 1].iov_len); |
| 2444 | le32_add_cpu(&req->CreateContextsLength, len); |
| 2445 | *num_iovec = num + 1; |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
Steve French | ff2a09e | 2019-07-06 14:41:38 -0500 | [diff] [blame] | 2449 | static struct crt_query_id_ctxt * |
| 2450 | create_query_id_buf(void) |
| 2451 | { |
| 2452 | struct crt_query_id_ctxt *buf; |
| 2453 | |
| 2454 | buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL); |
| 2455 | if (!buf) |
| 2456 | return NULL; |
| 2457 | |
| 2458 | buf->ccontext.DataOffset = cpu_to_le16(0); |
| 2459 | buf->ccontext.DataLength = cpu_to_le32(0); |
| 2460 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2461 | (struct crt_query_id_ctxt, Name)); |
| 2462 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2463 | /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */ |
| 2464 | buf->Name[0] = 'Q'; |
| 2465 | buf->Name[1] = 'F'; |
| 2466 | buf->Name[2] = 'i'; |
| 2467 | buf->Name[3] = 'd'; |
| 2468 | return buf; |
| 2469 | } |
| 2470 | |
| 2471 | /* See MS-SMB2 2.2.13.2.9 */ |
| 2472 | static int |
| 2473 | add_query_id_context(struct kvec *iov, unsigned int *num_iovec) |
| 2474 | { |
| 2475 | struct smb2_create_req *req = iov[0].iov_base; |
| 2476 | unsigned int num = *num_iovec; |
| 2477 | |
| 2478 | iov[num].iov_base = create_query_id_buf(); |
| 2479 | if (iov[num].iov_base == NULL) |
| 2480 | return -ENOMEM; |
| 2481 | iov[num].iov_len = sizeof(struct crt_query_id_ctxt); |
| 2482 | if (!req->CreateContextsOffset) |
| 2483 | req->CreateContextsOffset = cpu_to_le32( |
| 2484 | sizeof(struct smb2_create_req) + |
| 2485 | iov[num - 1].iov_len); |
| 2486 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt)); |
| 2487 | *num_iovec = num + 1; |
| 2488 | return 0; |
| 2489 | } |
| 2490 | |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2491 | static int |
| 2492 | alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len, |
| 2493 | const char *treename, const __le16 *path) |
| 2494 | { |
| 2495 | int treename_len, path_len; |
| 2496 | struct nls_table *cp; |
| 2497 | const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)}; |
| 2498 | |
| 2499 | /* |
| 2500 | * skip leading "\\" |
| 2501 | */ |
| 2502 | treename_len = strlen(treename); |
| 2503 | if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\')) |
| 2504 | return -EINVAL; |
| 2505 | |
| 2506 | treename += 2; |
| 2507 | treename_len -= 2; |
| 2508 | |
| 2509 | path_len = UniStrnlen((wchar_t *)path, PATH_MAX); |
| 2510 | |
| 2511 | /* |
| 2512 | * make room for one path separator between the treename and |
| 2513 | * path |
| 2514 | */ |
| 2515 | *out_len = treename_len + 1 + path_len; |
| 2516 | |
| 2517 | /* |
| 2518 | * final path needs to be null-terminated UTF16 with a |
| 2519 | * size aligned to 8 |
| 2520 | */ |
| 2521 | |
| 2522 | *out_size = roundup((*out_len+1)*2, 8); |
| 2523 | *out_path = kzalloc(*out_size, GFP_KERNEL); |
| 2524 | if (!*out_path) |
| 2525 | return -ENOMEM; |
| 2526 | |
| 2527 | cp = load_nls_default(); |
| 2528 | cifs_strtoUTF16(*out_path, treename, treename_len, cp); |
| 2529 | UniStrcat(*out_path, sep); |
| 2530 | UniStrcat(*out_path, path); |
| 2531 | unload_nls(cp); |
| 2532 | |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2536 | int smb311_posix_mkdir(const unsigned int xid, struct inode *inode, |
| 2537 | umode_t mode, struct cifs_tcon *tcon, |
| 2538 | const char *full_path, |
| 2539 | struct cifs_sb_info *cifs_sb) |
| 2540 | { |
| 2541 | struct smb_rqst rqst; |
| 2542 | struct smb2_create_req *req; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2543 | struct smb2_create_rsp *rsp = NULL; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2544 | struct cifs_ses *ses = tcon->ses; |
| 2545 | struct kvec iov[3]; /* make sure at least one for each open context */ |
| 2546 | struct kvec rsp_iov = {NULL, 0}; |
| 2547 | int resp_buftype; |
| 2548 | int uni_path_len; |
| 2549 | __le16 *copy_path = NULL; |
| 2550 | int copy_size; |
| 2551 | int rc = 0; |
| 2552 | unsigned int n_iov = 2; |
| 2553 | __u32 file_attributes = 0; |
| 2554 | char *pc_buf = NULL; |
| 2555 | int flags = 0; |
| 2556 | unsigned int total_len; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2557 | __le16 *utf16_path = NULL; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2558 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2559 | |
| 2560 | cifs_dbg(FYI, "mkdir\n"); |
| 2561 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2562 | /* resource #1: path allocation */ |
| 2563 | utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb); |
| 2564 | if (!utf16_path) |
| 2565 | return -ENOMEM; |
| 2566 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2567 | if (!ses || !server) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2568 | rc = -EIO; |
| 2569 | goto err_free_path; |
| 2570 | } |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2571 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2572 | /* resource #2: request */ |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2573 | rc = smb2_plain_req_init(SMB2_CREATE, tcon, server, |
| 2574 | (void **) &req, &total_len); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2575 | if (rc) |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2576 | goto err_free_path; |
| 2577 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2578 | |
| 2579 | if (smb3_encryption_required(tcon)) |
| 2580 | flags |= CIFS_TRANSFORM_REQ; |
| 2581 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2582 | req->ImpersonationLevel = IL_IMPERSONATION; |
| 2583 | req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES); |
| 2584 | /* File attributes ignored on open (used in create though) */ |
| 2585 | req->FileAttributes = cpu_to_le32(file_attributes); |
| 2586 | req->ShareAccess = FILE_SHARE_ALL_LE; |
| 2587 | req->CreateDisposition = cpu_to_le32(FILE_CREATE); |
| 2588 | req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE); |
| 2589 | |
| 2590 | iov[0].iov_base = (char *)req; |
| 2591 | /* -1 since last byte is buf[0] which is sent below (path) */ |
| 2592 | iov[0].iov_len = total_len - 1; |
| 2593 | |
| 2594 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); |
| 2595 | |
| 2596 | /* [MS-SMB2] 2.2.13 NameOffset: |
| 2597 | * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of |
| 2598 | * the SMB2 header, the file name includes a prefix that will |
| 2599 | * be processed during DFS name normalization as specified in |
| 2600 | * section 3.3.5.9. Otherwise, the file name is relative to |
| 2601 | * the share that is identified by the TreeId in the SMB2 |
| 2602 | * header. |
| 2603 | */ |
| 2604 | if (tcon->share_flags & SHI1005_FLAGS_DFS) { |
| 2605 | int name_len; |
| 2606 | |
| 2607 | req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; |
| 2608 | rc = alloc_path_with_tree_prefix(©_path, ©_size, |
| 2609 | &name_len, |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2610 | tcon->treeName, utf16_path); |
| 2611 | if (rc) |
| 2612 | goto err_free_req; |
| 2613 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2614 | req->NameLength = cpu_to_le16(name_len * 2); |
| 2615 | uni_path_len = copy_size; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2616 | /* free before overwriting resource */ |
| 2617 | kfree(utf16_path); |
| 2618 | utf16_path = copy_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2619 | } else { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2620 | uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2621 | /* MUST set path len (NameLength) to 0 opening root of share */ |
| 2622 | req->NameLength = cpu_to_le16(uni_path_len - 2); |
| 2623 | if (uni_path_len % 8 != 0) { |
| 2624 | copy_size = roundup(uni_path_len, 8); |
| 2625 | copy_path = kzalloc(copy_size, GFP_KERNEL); |
| 2626 | if (!copy_path) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2627 | rc = -ENOMEM; |
| 2628 | goto err_free_req; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2629 | } |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2630 | memcpy((char *)copy_path, (const char *)utf16_path, |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2631 | uni_path_len); |
| 2632 | uni_path_len = copy_size; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2633 | /* free before overwriting resource */ |
| 2634 | kfree(utf16_path); |
| 2635 | utf16_path = copy_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | iov[1].iov_len = uni_path_len; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2640 | iov[1].iov_base = utf16_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2641 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
| 2642 | |
| 2643 | if (tcon->posix_extensions) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2644 | /* resource #3: posix buf */ |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2645 | rc = add_posix_context(iov, &n_iov, mode); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2646 | if (rc) |
| 2647 | goto err_free_req; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2648 | pc_buf = iov[n_iov-1].iov_base; |
| 2649 | } |
| 2650 | |
| 2651 | |
| 2652 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 2653 | rqst.rq_iov = iov; |
| 2654 | rqst.rq_nvec = n_iov; |
| 2655 | |
Steve French | d2f1542 | 2019-09-22 00:55:46 -0500 | [diff] [blame] | 2656 | /* no need to inc num_remote_opens because we close it just below */ |
Steve French | efe2e9f | 2019-02-26 19:08:12 -0600 | [diff] [blame] | 2657 | trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE, |
| 2658 | FILE_WRITE_ATTRIBUTES); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2659 | /* resource #4: response buffer */ |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2660 | rc = cifs_send_recv(xid, ses, server, |
| 2661 | &rqst, &resp_buftype, flags, &rsp_iov); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2662 | if (rc) { |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2663 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); |
| 2664 | trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid, |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2665 | CREATE_NOT_FILE, |
| 2666 | FILE_WRITE_ATTRIBUTES, rc); |
| 2667 | goto err_free_rsp_buf; |
| 2668 | } |
| 2669 | |
| 2670 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; |
| 2671 | trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, |
| 2672 | ses->Suid, CREATE_NOT_FILE, |
| 2673 | FILE_WRITE_ATTRIBUTES); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2674 | |
| 2675 | SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId); |
| 2676 | |
| 2677 | /* Eventually save off posix specific response info and timestaps */ |
| 2678 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2679 | err_free_rsp_buf: |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2680 | free_rsp_buf(resp_buftype, rsp); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2681 | kfree(pc_buf); |
| 2682 | err_free_req: |
| 2683 | cifs_small_buf_release(req); |
| 2684 | err_free_path: |
| 2685 | kfree(utf16_path); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2686 | return rc; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2687 | } |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2688 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2689 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2690 | SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 2691 | struct smb_rqst *rqst, __u8 *oplock, |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2692 | struct cifs_open_parms *oparms, __le16 *path) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2693 | { |
| 2694 | struct smb2_create_req *req; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2695 | unsigned int n_iov = 2; |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2696 | __u32 file_attributes = 0; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2697 | int copy_size; |
| 2698 | int uni_path_len; |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2699 | unsigned int total_len; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2700 | struct kvec *iov = rqst->rq_iov; |
| 2701 | __le16 *copy_path; |
| 2702 | int rc; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2703 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2704 | rc = smb2_plain_req_init(SMB2_CREATE, tcon, server, |
| 2705 | (void **) &req, &total_len); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2706 | if (rc) |
| 2707 | return rc; |
| 2708 | |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2709 | iov[0].iov_base = (char *)req; |
| 2710 | /* -1 since last byte is buf[0] which is sent below (path) */ |
| 2711 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 2712 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2713 | if (oparms->create_options & CREATE_OPTION_READONLY) |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2714 | file_attributes |= ATTR_READONLY; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 2715 | if (oparms->create_options & CREATE_OPTION_SPECIAL) |
| 2716 | file_attributes |= ATTR_SYSTEM; |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2717 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2718 | req->ImpersonationLevel = IL_IMPERSONATION; |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2719 | req->DesiredAccess = cpu_to_le32(oparms->desired_access); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2720 | /* File attributes ignored on open (used in create though) */ |
| 2721 | req->FileAttributes = cpu_to_le32(file_attributes); |
| 2722 | req->ShareAccess = FILE_SHARE_ALL_LE; |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2723 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2724 | req->CreateDisposition = cpu_to_le32(oparms->disposition); |
| 2725 | req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK); |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2726 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2727 | |
| 2728 | /* [MS-SMB2] 2.2.13 NameOffset: |
| 2729 | * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of |
| 2730 | * the SMB2 header, the file name includes a prefix that will |
| 2731 | * be processed during DFS name normalization as specified in |
| 2732 | * section 3.3.5.9. Otherwise, the file name is relative to |
| 2733 | * the share that is identified by the TreeId in the SMB2 |
| 2734 | * header. |
| 2735 | */ |
| 2736 | if (tcon->share_flags & SHI1005_FLAGS_DFS) { |
| 2737 | int name_len; |
| 2738 | |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2739 | req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2740 | rc = alloc_path_with_tree_prefix(©_path, ©_size, |
| 2741 | &name_len, |
| 2742 | tcon->treeName, path); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2743 | if (rc) |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2744 | return rc; |
| 2745 | req->NameLength = cpu_to_le16(name_len * 2); |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2746 | uni_path_len = copy_size; |
| 2747 | path = copy_path; |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2748 | } else { |
| 2749 | uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2; |
| 2750 | /* MUST set path len (NameLength) to 0 opening root of share */ |
| 2751 | req->NameLength = cpu_to_le16(uni_path_len - 2); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2752 | copy_size = uni_path_len; |
| 2753 | if (copy_size % 8 != 0) |
| 2754 | copy_size = roundup(copy_size, 8); |
| 2755 | copy_path = kzalloc(copy_size, GFP_KERNEL); |
| 2756 | if (!copy_path) |
| 2757 | return -ENOMEM; |
| 2758 | memcpy((char *)copy_path, (const char *)path, |
| 2759 | uni_path_len); |
| 2760 | uni_path_len = copy_size; |
| 2761 | path = copy_path; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2762 | } |
| 2763 | |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2764 | iov[1].iov_len = uni_path_len; |
| 2765 | iov[1].iov_base = path; |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2766 | |
Steve French | 3e7a02d | 2019-09-11 21:46:20 -0500 | [diff] [blame] | 2767 | if ((!server->oplocks) || (tcon->no_lease)) |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2768 | *oplock = SMB2_OPLOCK_LEVEL_NONE; |
| 2769 | |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2770 | if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) || |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2771 | *oplock == SMB2_OPLOCK_LEVEL_NONE) |
| 2772 | req->RequestedOplockLevel = *oplock; |
Steve French | f801568 | 2018-08-31 15:12:10 -0500 | [diff] [blame] | 2773 | else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) && |
| 2774 | (oparms->create_options & CREATE_NOT_FILE)) |
| 2775 | req->RequestedOplockLevel = *oplock; /* no srv lease support */ |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2776 | else { |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2777 | rc = add_lease_context(server, iov, &n_iov, |
| 2778 | oparms->fid->lease_key, oplock); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2779 | if (rc) |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2780 | return rc; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2783 | if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) { |
| 2784 | /* need to set Next field of lease context if we request it */ |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2785 | if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) { |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2786 | struct create_context *ccontext = |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2787 | (struct create_context *)iov[n_iov-1].iov_base; |
Steve French | 1c46943 | 2013-07-10 12:50:57 -0500 | [diff] [blame] | 2788 | ccontext->Next = |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2789 | cpu_to_le32(server->vals->create_lease_size); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2790 | } |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2791 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2792 | rc = add_durable_context(iov, &n_iov, oparms, |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2793 | tcon->use_persistent); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2794 | if (rc) |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2795 | return rc; |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2796 | } |
| 2797 | |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2798 | if (tcon->posix_extensions) { |
| 2799 | if (n_iov > 2) { |
| 2800 | struct create_context *ccontext = |
| 2801 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2802 | ccontext->Next = |
| 2803 | cpu_to_le32(iov[n_iov-1].iov_len); |
| 2804 | } |
| 2805 | |
| 2806 | rc = add_posix_context(iov, &n_iov, oparms->mode); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2807 | if (rc) |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2808 | return rc; |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2809 | } |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2810 | |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2811 | if (tcon->snapshot_time) { |
| 2812 | cifs_dbg(FYI, "adding snapshot context\n"); |
| 2813 | if (n_iov > 2) { |
| 2814 | struct create_context *ccontext = |
| 2815 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2816 | ccontext->Next = |
| 2817 | cpu_to_le32(iov[n_iov-1].iov_len); |
| 2818 | } |
| 2819 | |
| 2820 | rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time); |
| 2821 | if (rc) |
| 2822 | return rc; |
| 2823 | } |
| 2824 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2825 | if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) { |
| 2826 | bool set_mode; |
| 2827 | bool set_owner; |
| 2828 | |
| 2829 | if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) && |
| 2830 | (oparms->mode != ACL_NO_MODE)) |
| 2831 | set_mode = true; |
| 2832 | else { |
| 2833 | set_mode = false; |
| 2834 | oparms->mode = ACL_NO_MODE; |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2835 | } |
| 2836 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2837 | if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL) |
| 2838 | set_owner = true; |
| 2839 | else |
| 2840 | set_owner = false; |
| 2841 | |
| 2842 | if (set_owner | set_mode) { |
| 2843 | if (n_iov > 2) { |
| 2844 | struct create_context *ccontext = |
| 2845 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2846 | ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len); |
| 2847 | } |
| 2848 | |
| 2849 | cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode); |
| 2850 | rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner); |
| 2851 | if (rc) |
| 2852 | return rc; |
| 2853 | } |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2854 | } |
| 2855 | |
Steve French | ff2a09e | 2019-07-06 14:41:38 -0500 | [diff] [blame] | 2856 | if (n_iov > 2) { |
| 2857 | struct create_context *ccontext = |
| 2858 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2859 | ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len); |
| 2860 | } |
| 2861 | add_query_id_context(iov, &n_iov); |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2862 | |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2863 | rqst->rq_nvec = n_iov; |
| 2864 | return 0; |
| 2865 | } |
| 2866 | |
| 2867 | /* rq_iov[0] is the request and is released by cifs_small_buf_release(). |
| 2868 | * All other vectors are freed by kfree(). |
| 2869 | */ |
| 2870 | void |
| 2871 | SMB2_open_free(struct smb_rqst *rqst) |
| 2872 | { |
| 2873 | int i; |
| 2874 | |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 2875 | if (rqst && rqst->rq_iov) { |
| 2876 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); |
| 2877 | for (i = 1; i < rqst->rq_nvec; i++) |
| 2878 | if (rqst->rq_iov[i].iov_base != smb2_padding) |
| 2879 | kfree(rqst->rq_iov[i].iov_base); |
| 2880 | } |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | int |
| 2884 | SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, |
| 2885 | __u8 *oplock, struct smb2_file_all_info *buf, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2886 | struct create_posix_rsp *posix, |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2887 | struct kvec *err_iov, int *buftype) |
| 2888 | { |
| 2889 | struct smb_rqst rqst; |
| 2890 | struct smb2_create_rsp *rsp = NULL; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2891 | struct cifs_tcon *tcon = oparms->tcon; |
| 2892 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2893 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | 4d8dfaf | 2018-08-21 11:49:21 +1000 | [diff] [blame] | 2894 | struct kvec iov[SMB2_CREATE_IOV_SIZE]; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2895 | struct kvec rsp_iov = {NULL, 0}; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 2896 | int resp_buftype = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2897 | int rc = 0; |
| 2898 | int flags = 0; |
| 2899 | |
| 2900 | cifs_dbg(FYI, "create/open\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2901 | if (!ses || !server) |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2902 | return -EIO; |
| 2903 | |
| 2904 | if (smb3_encryption_required(tcon)) |
| 2905 | flags |= CIFS_TRANSFORM_REQ; |
| 2906 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2907 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2908 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2909 | rqst.rq_iov = iov; |
Ronnie Sahlberg | 4d8dfaf | 2018-08-21 11:49:21 +1000 | [diff] [blame] | 2910 | rqst.rq_nvec = SMB2_CREATE_IOV_SIZE; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2911 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2912 | rc = SMB2_open_init(tcon, server, |
| 2913 | &rqst, oplock, oparms, path); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2914 | if (rc) |
| 2915 | goto creat_exit; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2916 | |
Steve French | efe2e9f | 2019-02-26 19:08:12 -0600 | [diff] [blame] | 2917 | trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, |
| 2918 | oparms->create_options, oparms->desired_access); |
| 2919 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2920 | rc = cifs_send_recv(xid, ses, server, |
| 2921 | &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2922 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2923 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2924 | |
| 2925 | if (rc != 0) { |
| 2926 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); |
Ronnie Sahlberg | 91cb74f | 2018-04-13 09:03:19 +1000 | [diff] [blame] | 2927 | if (err_iov && rsp) { |
| 2928 | *err_iov = rsp_iov; |
Ronnie Sahlberg | 9d874c3 | 2018-06-08 13:21:18 +1000 | [diff] [blame] | 2929 | *buftype = resp_buftype; |
Ronnie Sahlberg | 91cb74f | 2018-04-13 09:03:19 +1000 | [diff] [blame] | 2930 | resp_buftype = CIFS_NO_BUFFER; |
| 2931 | rsp = NULL; |
| 2932 | } |
Steve French | 28d5936 | 2018-05-30 21:42:34 -0500 | [diff] [blame] | 2933 | trace_smb3_open_err(xid, tcon->tid, ses->Suid, |
| 2934 | oparms->create_options, oparms->desired_access, rc); |
Steve French | 7dcc82c | 2019-09-11 00:07:36 -0500 | [diff] [blame] | 2935 | if (rc == -EREMCHG) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 2936 | pr_warn_once("server share %s deleted\n", |
| 2937 | tcon->treeName); |
Steve French | 7dcc82c | 2019-09-11 00:07:36 -0500 | [diff] [blame] | 2938 | tcon->need_reconnect = true; |
| 2939 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2940 | goto creat_exit; |
Steve French | 28d5936 | 2018-05-30 21:42:34 -0500 | [diff] [blame] | 2941 | } else |
| 2942 | trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, |
| 2943 | ses->Suid, oparms->create_options, |
| 2944 | oparms->desired_access); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2945 | |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 2946 | atomic_inc(&tcon->num_remote_opens); |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2947 | oparms->fid->persistent_fid = rsp->PersistentFileId; |
| 2948 | oparms->fid->volatile_fid = rsp->VolatileFileId; |
Aurelien Aptel | 86f740f | 2020-02-21 11:19:06 +0100 | [diff] [blame] | 2949 | oparms->fid->access = oparms->desired_access; |
Steve French | dfe33f9 | 2018-10-30 19:50:31 -0500 | [diff] [blame] | 2950 | #ifdef CONFIG_CIFS_DEBUG2 |
| 2951 | oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId); |
| 2952 | #endif /* CIFS_DEBUG2 */ |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 2953 | |
| 2954 | if (buf) { |
| 2955 | memcpy(buf, &rsp->CreationTime, 32); |
| 2956 | buf->AllocationSize = rsp->AllocationSize; |
| 2957 | buf->EndOfFile = rsp->EndofFile; |
| 2958 | buf->Attributes = rsp->FileAttributes; |
| 2959 | buf->NumberOfLinks = cpu_to_le32(1); |
| 2960 | buf->DeletePending = 0; |
| 2961 | } |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 2962 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2963 | |
| 2964 | smb2_parse_contexts(server, rsp, &oparms->fid->epoch, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2965 | oparms->fid->lease_key, oplock, buf, posix); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2966 | creat_exit: |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2967 | SMB2_open_free(&rqst); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2968 | free_rsp_buf(resp_buftype, rsp); |
| 2969 | return rc; |
| 2970 | } |
| 2971 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2972 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2973 | SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 2974 | struct smb_rqst *rqst, |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2975 | u64 persistent_fid, u64 volatile_fid, u32 opcode, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2976 | bool is_fsctl, char *in_data, u32 indatalen, |
| 2977 | __u32 max_response_size) |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2978 | { |
| 2979 | struct smb2_ioctl_req *req; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2980 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2981 | unsigned int total_len; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2982 | int rc; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2983 | char *in_data_buf; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2984 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2985 | rc = smb2_ioctl_req_init(opcode, tcon, server, |
| 2986 | (void **) &req, &total_len); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2987 | if (rc) |
| 2988 | return rc; |
| 2989 | |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2990 | if (indatalen) { |
| 2991 | /* |
| 2992 | * indatalen is usually small at a couple of bytes max, so |
| 2993 | * just allocate through generic pool |
| 2994 | */ |
YueHaibing | d81f097 | 2019-06-01 03:31:10 +0000 | [diff] [blame] | 2995 | in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS); |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2996 | if (!in_data_buf) { |
| 2997 | cifs_small_buf_release(req); |
| 2998 | return -ENOMEM; |
| 2999 | } |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3002 | req->CtlCode = cpu_to_le32(opcode); |
| 3003 | req->PersistentFileId = persistent_fid; |
| 3004 | req->VolatileFileId = volatile_fid; |
| 3005 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3006 | iov[0].iov_base = (char *)req; |
| 3007 | /* |
| 3008 | * If no input data, the size of ioctl struct in |
| 3009 | * protocol spec still includes a 1 byte data buffer, |
| 3010 | * but if input data passed to ioctl, we do not |
| 3011 | * want to double count this, so we do not send |
| 3012 | * the dummy one byte of data in iovec[0] if sending |
| 3013 | * input data (in iovec[1]). |
| 3014 | */ |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3015 | if (indatalen) { |
| 3016 | req->InputCount = cpu_to_le32(indatalen); |
| 3017 | /* do not set InputOffset if no input data */ |
| 3018 | req->InputOffset = |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 3019 | cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer)); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3020 | rqst->rq_nvec = 2; |
| 3021 | iov[0].iov_len = total_len - 1; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 3022 | iov[1].iov_base = in_data_buf; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3023 | iov[1].iov_len = indatalen; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3024 | } else { |
| 3025 | rqst->rq_nvec = 1; |
| 3026 | iov[0].iov_len = total_len; |
| 3027 | } |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3028 | |
| 3029 | req->OutputOffset = 0; |
| 3030 | req->OutputCount = 0; /* MBZ */ |
| 3031 | |
| 3032 | /* |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3033 | * In most cases max_response_size is set to 16K (CIFSMaxBufSize) |
| 3034 | * We Could increase default MaxOutputResponse, but that could require |
| 3035 | * more credits. Windows typically sets this smaller, but for some |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3036 | * ioctls it may be useful to allow server to send more. No point |
| 3037 | * limiting what the server can send as long as fits in one credit |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3038 | * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want |
| 3039 | * to increase this limit up in the future. |
| 3040 | * Note that for snapshot queries that servers like Azure expect that |
| 3041 | * the first query be minimal size (and just used to get the number/size |
| 3042 | * of previous versions) so response size must be specified as EXACTLY |
| 3043 | * sizeof(struct snapshot_array) which is 16 when rounded up to multiple |
| 3044 | * of eight bytes. Currently that is the only case where we set max |
| 3045 | * response size smaller. |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3046 | */ |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3047 | req->MaxOutputResponse = cpu_to_le32(max_response_size); |
Namjae Jeon | ebf5744 | 2020-06-11 11:21:19 +0900 | [diff] [blame] | 3048 | req->sync_hdr.CreditCharge = |
| 3049 | cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size), |
| 3050 | SMB2_MAX_BUFFER_SIZE)); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3051 | if (is_fsctl) |
| 3052 | req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL); |
| 3053 | else |
| 3054 | req->Flags = 0; |
| 3055 | |
Steve French | 4587eee | 2017-10-25 15:58:31 -0500 | [diff] [blame] | 3056 | /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */ |
| 3057 | if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 3058 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3059 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3060 | return 0; |
| 3061 | } |
| 3062 | |
| 3063 | void |
| 3064 | SMB2_ioctl_free(struct smb_rqst *rqst) |
| 3065 | { |
Murphy Zhou | 6457c20 | 2019-05-23 12:12:43 +0800 | [diff] [blame] | 3066 | int i; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 3067 | if (rqst && rqst->rq_iov) { |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3068 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Murphy Zhou | 6457c20 | 2019-05-23 12:12:43 +0800 | [diff] [blame] | 3069 | for (i = 1; i < rqst->rq_nvec; i++) |
| 3070 | if (rqst->rq_iov[i].iov_base != smb2_padding) |
| 3071 | kfree(rqst->rq_iov[i].iov_base); |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 3072 | } |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3073 | } |
| 3074 | |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3075 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3076 | /* |
| 3077 | * SMB2 IOCTL is used for both IOCTLs and FSCTLs |
| 3078 | */ |
| 3079 | int |
| 3080 | SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, |
| 3081 | u64 volatile_fid, u32 opcode, bool is_fsctl, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3082 | char *in_data, u32 indatalen, u32 max_out_data_len, |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3083 | char **out_data, u32 *plen /* returned data len */) |
| 3084 | { |
| 3085 | struct smb_rqst rqst; |
| 3086 | struct smb2_ioctl_rsp *rsp = NULL; |
| 3087 | struct cifs_ses *ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3088 | struct TCP_Server_Info *server; |
Ronnie Sahlberg | 72c419d | 2019-03-13 14:37:49 +1000 | [diff] [blame] | 3089 | struct kvec iov[SMB2_IOCTL_IOV_SIZE]; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3090 | struct kvec rsp_iov = {NULL, 0}; |
| 3091 | int resp_buftype = CIFS_NO_BUFFER; |
| 3092 | int rc = 0; |
| 3093 | int flags = 0; |
| 3094 | |
| 3095 | cifs_dbg(FYI, "SMB2 IOCTL\n"); |
| 3096 | |
| 3097 | if (out_data != NULL) |
| 3098 | *out_data = NULL; |
| 3099 | |
| 3100 | /* zero out returned data len, in case of error */ |
| 3101 | if (plen) |
| 3102 | *plen = 0; |
| 3103 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3104 | if (!tcon) |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3105 | return -EIO; |
| 3106 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3107 | ses = tcon->ses; |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3108 | if (!ses) |
| 3109 | return -EIO; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3110 | |
| 3111 | server = cifs_pick_channel(ses); |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3112 | if (!server) |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3113 | return -EIO; |
| 3114 | |
| 3115 | if (smb3_encryption_required(tcon)) |
| 3116 | flags |= CIFS_TRANSFORM_REQ; |
| 3117 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3118 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3119 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3120 | rqst.rq_iov = iov; |
Ronnie Sahlberg | 72c419d | 2019-03-13 14:37:49 +1000 | [diff] [blame] | 3121 | rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3122 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3123 | rc = SMB2_ioctl_init(tcon, server, |
| 3124 | &rqst, persistent_fid, volatile_fid, opcode, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3125 | is_fsctl, in_data, indatalen, max_out_data_len); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3126 | if (rc) |
| 3127 | goto ioctl_exit; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3128 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3129 | rc = cifs_send_recv(xid, ses, server, |
| 3130 | &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 3131 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3132 | rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3133 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3134 | if (rc != 0) |
| 3135 | trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid, |
| 3136 | ses->Suid, 0, opcode, rc); |
| 3137 | |
Ronnie Sahlberg | 2f3ebab | 2019-04-25 16:45:29 +1000 | [diff] [blame] | 3138 | if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) { |
Steve French | 8e35310 | 2015-03-26 19:47:02 -0500 | [diff] [blame] | 3139 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3140 | goto ioctl_exit; |
Steve French | 9bf0c9c | 2013-11-16 18:05:28 -0600 | [diff] [blame] | 3141 | } else if (rc == -EINVAL) { |
| 3142 | if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) && |
| 3143 | (opcode != FSCTL_SRV_COPYCHUNK)) { |
Steve French | 8e35310 | 2015-03-26 19:47:02 -0500 | [diff] [blame] | 3144 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
Steve French | 9bf0c9c | 2013-11-16 18:05:28 -0600 | [diff] [blame] | 3145 | goto ioctl_exit; |
| 3146 | } |
Ronnie Sahlberg | 2f3ebab | 2019-04-25 16:45:29 +1000 | [diff] [blame] | 3147 | } else if (rc == -E2BIG) { |
| 3148 | if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) { |
| 3149 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
| 3150 | goto ioctl_exit; |
| 3151 | } |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | /* check if caller wants to look at return data or just return rc */ |
| 3155 | if ((plen == NULL) || (out_data == NULL)) |
| 3156 | goto ioctl_exit; |
| 3157 | |
| 3158 | *plen = le32_to_cpu(rsp->OutputCount); |
| 3159 | |
| 3160 | /* We check for obvious errors in the output buffer length and offset */ |
| 3161 | if (*plen == 0) |
| 3162 | goto ioctl_exit; /* server returned no data */ |
Dan Carpenter | 2d204ee | 2018-09-10 14:12:07 +0300 | [diff] [blame] | 3163 | else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3164 | cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3165 | *plen = 0; |
| 3166 | rc = -EIO; |
| 3167 | goto ioctl_exit; |
| 3168 | } |
| 3169 | |
Dan Carpenter | 2d204ee | 2018-09-10 14:12:07 +0300 | [diff] [blame] | 3170 | if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3171 | cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen, |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3172 | le32_to_cpu(rsp->OutputOffset)); |
| 3173 | *plen = 0; |
| 3174 | rc = -EIO; |
| 3175 | goto ioctl_exit; |
| 3176 | } |
| 3177 | |
YueHaibing | d034fee | 2018-09-10 01:33:06 +0000 | [diff] [blame] | 3178 | *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset), |
| 3179 | *plen, GFP_KERNEL); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3180 | if (*out_data == NULL) { |
| 3181 | rc = -ENOMEM; |
| 3182 | goto ioctl_exit; |
| 3183 | } |
| 3184 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3185 | ioctl_exit: |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3186 | SMB2_ioctl_free(&rqst); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3187 | free_rsp_buf(resp_buftype, rsp); |
| 3188 | return rc; |
| 3189 | } |
| 3190 | |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3191 | /* |
| 3192 | * Individual callers to ioctl worker function follow |
| 3193 | */ |
| 3194 | |
| 3195 | int |
| 3196 | SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon, |
| 3197 | u64 persistent_fid, u64 volatile_fid) |
| 3198 | { |
| 3199 | int rc; |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3200 | struct compress_ioctl fsctl_input; |
| 3201 | char *ret_data = NULL; |
| 3202 | |
| 3203 | fsctl_input.CompressionState = |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3204 | cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3205 | |
| 3206 | rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid, |
| 3207 | FSCTL_SET_COMPRESSION, true /* is_fsctl */, |
| 3208 | (char *)&fsctl_input /* data input */, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3209 | 2 /* in data len */, CIFSMaxBufSize /* max out data */, |
| 3210 | &ret_data /* out data */, NULL); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3211 | |
| 3212 | cifs_dbg(FYI, "set compression rc %d\n", rc); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3213 | |
| 3214 | return rc; |
| 3215 | } |
| 3216 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3217 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3218 | SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3219 | struct smb_rqst *rqst, |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3220 | u64 persistent_fid, u64 volatile_fid, bool query_attrs) |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3221 | { |
| 3222 | struct smb2_close_req *req; |
| 3223 | struct kvec *iov = rqst->rq_iov; |
| 3224 | unsigned int total_len; |
| 3225 | int rc; |
| 3226 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3227 | rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server, |
| 3228 | (void **) &req, &total_len); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3229 | if (rc) |
| 3230 | return rc; |
| 3231 | |
| 3232 | req->PersistentFileId = persistent_fid; |
| 3233 | req->VolatileFileId = volatile_fid; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3234 | if (query_attrs) |
| 3235 | req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 3236 | else |
| 3237 | req->Flags = 0; |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3238 | iov[0].iov_base = (char *)req; |
| 3239 | iov[0].iov_len = total_len; |
| 3240 | |
| 3241 | return 0; |
| 3242 | } |
| 3243 | |
| 3244 | void |
| 3245 | SMB2_close_free(struct smb_rqst *rqst) |
| 3246 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 3247 | if (rqst && rqst->rq_iov) |
| 3248 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | int |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3252 | __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, |
| 3253 | u64 persistent_fid, u64 volatile_fid, |
| 3254 | struct smb2_file_network_open_info *pbuf) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3255 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3256 | struct smb_rqst rqst; |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3257 | struct smb2_close_rsp *rsp = NULL; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3258 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3259 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3260 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3261 | struct kvec rsp_iov; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 3262 | int resp_buftype = CIFS_NO_BUFFER; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3263 | int rc = 0; |
Steve French | 9e8fae2 | 2019-12-02 17:55:41 -0600 | [diff] [blame] | 3264 | int flags = 0; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3265 | bool query_attrs = false; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3266 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3267 | cifs_dbg(FYI, "Close\n"); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3268 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3269 | if (!ses || !server) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3270 | return -EIO; |
| 3271 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 3272 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 3273 | flags |= CIFS_TRANSFORM_REQ; |
| 3274 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3275 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3276 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3277 | rqst.rq_iov = iov; |
| 3278 | rqst.rq_nvec = 1; |
| 3279 | |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3280 | /* check if need to ask server to return timestamps in close response */ |
| 3281 | if (pbuf) |
| 3282 | query_attrs = true; |
| 3283 | |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3284 | trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3285 | rc = SMB2_close_init(tcon, server, |
| 3286 | &rqst, persistent_fid, volatile_fid, |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3287 | query_attrs); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3288 | if (rc) |
| 3289 | goto close_exit; |
| 3290 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3291 | rc = cifs_send_recv(xid, ses, server, |
| 3292 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3293 | rsp = (struct smb2_close_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3294 | |
| 3295 | if (rc != 0) { |
Namjae Jeon | d4a029d | 2014-08-20 19:39:59 +0900 | [diff] [blame] | 3296 | cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3297 | trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3298 | rc); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3299 | goto close_exit; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3300 | } else { |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3301 | trace_smb3_close_done(xid, persistent_fid, tcon->tid, |
| 3302 | ses->Suid); |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3303 | /* |
| 3304 | * Note that have to subtract 4 since struct network_open_info |
| 3305 | * has a final 4 byte pad that close response does not have |
| 3306 | */ |
| 3307 | if (pbuf) |
| 3308 | memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4); |
| 3309 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3310 | |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 3311 | atomic_dec(&tcon->num_remote_opens); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3312 | close_exit: |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3313 | SMB2_close_free(&rqst); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3314 | free_rsp_buf(resp_buftype, rsp); |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3315 | |
| 3316 | /* retry close in a worker thread if this one is interrupted */ |
| 3317 | if (rc == -EINTR) { |
Steve French | 9e8fae2 | 2019-12-02 17:55:41 -0600 | [diff] [blame] | 3318 | int tmp_rc; |
| 3319 | |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3320 | tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid, |
| 3321 | volatile_fid); |
| 3322 | if (tmp_rc) |
| 3323 | cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n", |
| 3324 | persistent_fid, tmp_rc); |
| 3325 | } |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3326 | return rc; |
Ronnie Sahlberg | 97ca176 | 2018-04-26 08:50:49 -0600 | [diff] [blame] | 3327 | } |
| 3328 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3329 | int |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3330 | SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, |
| 3331 | u64 persistent_fid, u64 volatile_fid) |
| 3332 | { |
| 3333 | return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL); |
| 3334 | } |
| 3335 | |
| 3336 | int |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3337 | smb2_validate_iov(unsigned int offset, unsigned int buffer_length, |
| 3338 | struct kvec *iov, unsigned int min_buf_size) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3339 | { |
Ronnie Sahlberg | c1596ff | 2018-04-09 18:06:30 +1000 | [diff] [blame] | 3340 | unsigned int smb_len = iov->iov_len; |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 3341 | char *end_of_smb = smb_len + (char *)iov->iov_base; |
| 3342 | char *begin_of_buf = offset + (char *)iov->iov_base; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3343 | char *end_of_buf = begin_of_buf + buffer_length; |
| 3344 | |
| 3345 | |
| 3346 | if (buffer_length < min_buf_size) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3347 | cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n", |
| 3348 | buffer_length, min_buf_size); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3349 | return -EINVAL; |
| 3350 | } |
| 3351 | |
| 3352 | /* check if beyond RFC1001 maximum length */ |
| 3353 | if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3354 | cifs_dbg(VFS, "buffer length %d or smb length %d too large\n", |
| 3355 | buffer_length, smb_len); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3356 | return -EINVAL; |
| 3357 | } |
| 3358 | |
| 3359 | if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 3360 | cifs_dbg(VFS, "Invalid server response, bad offset to data\n"); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3361 | return -EINVAL; |
| 3362 | } |
| 3363 | |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
| 3367 | /* |
| 3368 | * If SMB buffer fields are valid, copy into temporary buffer to hold result. |
| 3369 | * Caller must free buffer. |
| 3370 | */ |
Ronnie Sahlberg | c5a5f38 | 2018-09-03 13:33:41 +1000 | [diff] [blame] | 3371 | int |
| 3372 | smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length, |
| 3373 | struct kvec *iov, unsigned int minbufsize, |
| 3374 | char *data) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3375 | { |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 3376 | char *begin_of_buf = offset + (char *)iov->iov_base; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3377 | int rc; |
| 3378 | |
| 3379 | if (!data) |
| 3380 | return -EINVAL; |
| 3381 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3382 | rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3383 | if (rc) |
| 3384 | return rc; |
| 3385 | |
| 3386 | memcpy(data, begin_of_buf, buffer_length); |
| 3387 | |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3391 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3392 | SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3393 | struct smb_rqst *rqst, |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3394 | u64 persistent_fid, u64 volatile_fid, |
| 3395 | u8 info_class, u8 info_type, u32 additional_info, |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3396 | size_t output_len, size_t input_len, void *input) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3397 | { |
| 3398 | struct smb2_query_info_req *req; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3399 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 3400 | unsigned int total_len; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3401 | int rc; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3402 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3403 | rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server, |
| 3404 | (void **) &req, &total_len); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3405 | if (rc) |
| 3406 | return rc; |
| 3407 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3408 | req->InfoType = info_type; |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3409 | req->FileInfoClass = info_class; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3410 | req->PersistentFileId = persistent_fid; |
| 3411 | req->VolatileFileId = volatile_fid; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3412 | req->AdditionalInformation = cpu_to_le32(additional_info); |
Aurelien Aptel | 48923d2 | 2017-10-17 14:47:17 +0200 | [diff] [blame] | 3413 | |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3414 | req->OutputBufferLength = cpu_to_le32(output_len); |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3415 | if (input_len) { |
| 3416 | req->InputBufferLength = cpu_to_le32(input_len); |
| 3417 | /* total_len for smb query request never close to le16 max */ |
| 3418 | req->InputBufferOffset = cpu_to_le16(total_len - 1); |
| 3419 | memcpy(req->Buffer, input, input_len); |
| 3420 | } |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3421 | |
| 3422 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 3423 | /* 1 for Buffer */ |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3424 | iov[0].iov_len = total_len - 1 + input_len; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3425 | return 0; |
| 3426 | } |
| 3427 | |
| 3428 | void |
| 3429 | SMB2_query_info_free(struct smb_rqst *rqst) |
| 3430 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 3431 | if (rqst && rqst->rq_iov) |
| 3432 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | static int |
| 3436 | query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3437 | u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type, |
| 3438 | u32 additional_info, size_t output_len, size_t min_len, void **data, |
| 3439 | u32 *dlen) |
| 3440 | { |
| 3441 | struct smb_rqst rqst; |
| 3442 | struct smb2_query_info_rsp *rsp = NULL; |
| 3443 | struct kvec iov[1]; |
| 3444 | struct kvec rsp_iov; |
| 3445 | int rc = 0; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 3446 | int resp_buftype = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3447 | struct cifs_ses *ses = tcon->ses; |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3448 | struct TCP_Server_Info *server; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3449 | int flags = 0; |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3450 | bool allocated = false; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3451 | |
| 3452 | cifs_dbg(FYI, "Query Info\n"); |
| 3453 | |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3454 | if (!ses) |
| 3455 | return -EIO; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3456 | server = cifs_pick_channel(ses); |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3457 | if (!server) |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3458 | return -EIO; |
| 3459 | |
| 3460 | if (smb3_encryption_required(tcon)) |
| 3461 | flags |= CIFS_TRANSFORM_REQ; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3462 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3463 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3464 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3465 | rqst.rq_iov = iov; |
| 3466 | rqst.rq_nvec = 1; |
| 3467 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3468 | rc = SMB2_query_info_init(tcon, server, |
| 3469 | &rqst, persistent_fid, volatile_fid, |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3470 | info_class, info_type, additional_info, |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3471 | output_len, 0, NULL); |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3472 | if (rc) |
| 3473 | goto qinf_exit; |
| 3474 | |
Steve French | d42043a | 2019-02-26 21:58:30 -0600 | [diff] [blame] | 3475 | trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid, |
| 3476 | ses->Suid, info_class, (__u32)info_type); |
| 3477 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3478 | rc = cifs_send_recv(xid, ses, server, |
| 3479 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3480 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 3481 | |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3482 | if (rc) { |
| 3483 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3484 | trace_smb3_query_info_err(xid, persistent_fid, tcon->tid, |
| 3485 | ses->Suid, info_class, (__u32)info_type, rc); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3486 | goto qinf_exit; |
| 3487 | } |
| 3488 | |
Steve French | d42043a | 2019-02-26 21:58:30 -0600 | [diff] [blame] | 3489 | trace_smb3_query_info_done(xid, persistent_fid, tcon->tid, |
| 3490 | ses->Suid, info_class, (__u32)info_type); |
| 3491 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3492 | if (dlen) { |
| 3493 | *dlen = le32_to_cpu(rsp->OutputBufferLength); |
| 3494 | if (!*data) { |
| 3495 | *data = kmalloc(*dlen, GFP_KERNEL); |
| 3496 | if (!*data) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3497 | cifs_tcon_dbg(VFS, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3498 | "Error %d allocating memory for acl\n", |
| 3499 | rc); |
| 3500 | *dlen = 0; |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3501 | rc = -ENOMEM; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3502 | goto qinf_exit; |
| 3503 | } |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3504 | allocated = true; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | |
Ronnie Sahlberg | c5a5f38 | 2018-09-03 13:33:41 +1000 | [diff] [blame] | 3508 | rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 3509 | le32_to_cpu(rsp->OutputBufferLength), |
| 3510 | &rsp_iov, min_len, *data); |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3511 | if (rc && allocated) { |
| 3512 | kfree(*data); |
| 3513 | *data = NULL; |
| 3514 | *dlen = 0; |
| 3515 | } |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3516 | |
| 3517 | qinf_exit: |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3518 | SMB2_query_info_free(&rqst); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3519 | free_rsp_buf(resp_buftype, rsp); |
| 3520 | return rc; |
| 3521 | } |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3522 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3523 | int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3524 | u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data) |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3525 | { |
| 3526 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3527 | FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0, |
Pavel Shilovsky | 1bbe499 | 2014-08-22 13:32:11 +0400 | [diff] [blame] | 3528 | sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3529 | sizeof(struct smb2_file_all_info), (void **)&data, |
| 3530 | NULL); |
| 3531 | } |
| 3532 | |
| 3533 | int |
Steve French | b1bc187 | 2020-06-11 20:23:38 -0500 | [diff] [blame] | 3534 | SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3535 | u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen) |
| 3536 | { |
| 3537 | size_t output_len = sizeof(struct smb311_posix_qinfo *) + |
| 3538 | (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2); |
| 3539 | *plen = 0; |
| 3540 | |
| 3541 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
| 3542 | SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0, |
| 3543 | output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen); |
| 3544 | } |
| 3545 | |
| 3546 | int |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3547 | SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon, |
| 3548 | u64 persistent_fid, u64 volatile_fid, |
| 3549 | void **data, u32 *plen) |
| 3550 | { |
| 3551 | __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO; |
| 3552 | *plen = 0; |
| 3553 | |
| 3554 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
| 3555 | 0, SMB2_O_INFO_SECURITY, additional_info, |
Shirish Pargaonkar | ee25c6d | 2018-06-04 06:46:22 -0500 | [diff] [blame] | 3556 | SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | int |
| 3560 | SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon, |
| 3561 | u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid) |
| 3562 | { |
| 3563 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3564 | FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0, |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3565 | sizeof(struct smb2_file_internal_info), |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3566 | sizeof(struct smb2_file_internal_info), |
| 3567 | (void **)&uniqueid, NULL); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3568 | } |
| 3569 | |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3570 | /* |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3571 | * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory |
| 3572 | * See MS-SMB2 2.2.35 and 2.2.36 |
| 3573 | */ |
| 3574 | |
zhengbin | 388962e | 2019-09-23 15:06:18 +0800 | [diff] [blame] | 3575 | static int |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3576 | SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3577 | struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3578 | u64 persistent_fid, u64 volatile_fid, |
| 3579 | u32 completion_filter, bool watch_tree) |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3580 | { |
| 3581 | struct smb2_change_notify_req *req; |
| 3582 | struct kvec *iov = rqst->rq_iov; |
| 3583 | unsigned int total_len; |
| 3584 | int rc; |
| 3585 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3586 | rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server, |
| 3587 | (void **) &req, &total_len); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3588 | if (rc) |
| 3589 | return rc; |
| 3590 | |
| 3591 | req->PersistentFileId = persistent_fid; |
| 3592 | req->VolatileFileId = volatile_fid; |
Steve French | d26c2dd | 2020-02-06 06:00:14 -0600 | [diff] [blame] | 3593 | /* See note 354 of MS-SMB2, 64K max */ |
Steve French | 52870d5 | 2019-10-01 21:25:46 -0500 | [diff] [blame] | 3594 | req->OutputBufferLength = |
| 3595 | cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3596 | req->CompletionFilter = cpu_to_le32(completion_filter); |
| 3597 | if (watch_tree) |
| 3598 | req->Flags = cpu_to_le16(SMB2_WATCH_TREE); |
| 3599 | else |
| 3600 | req->Flags = 0; |
| 3601 | |
| 3602 | iov[0].iov_base = (char *)req; |
| 3603 | iov[0].iov_len = total_len; |
| 3604 | |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
| 3608 | int |
| 3609 | SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon, |
| 3610 | u64 persistent_fid, u64 volatile_fid, bool watch_tree, |
| 3611 | u32 completion_filter) |
| 3612 | { |
| 3613 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3614 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3615 | struct smb_rqst rqst; |
| 3616 | struct kvec iov[1]; |
| 3617 | struct kvec rsp_iov = {NULL, 0}; |
| 3618 | int resp_buftype = CIFS_NO_BUFFER; |
| 3619 | int flags = 0; |
| 3620 | int rc = 0; |
| 3621 | |
| 3622 | cifs_dbg(FYI, "change notify\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3623 | if (!ses || !server) |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3624 | return -EIO; |
| 3625 | |
| 3626 | if (smb3_encryption_required(tcon)) |
| 3627 | flags |= CIFS_TRANSFORM_REQ; |
| 3628 | |
| 3629 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 3630 | memset(&iov, 0, sizeof(iov)); |
| 3631 | rqst.rq_iov = iov; |
| 3632 | rqst.rq_nvec = 1; |
| 3633 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3634 | rc = SMB2_notify_init(xid, &rqst, tcon, server, |
| 3635 | persistent_fid, volatile_fid, |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3636 | completion_filter, watch_tree); |
| 3637 | if (rc) |
| 3638 | goto cnotify_exit; |
| 3639 | |
| 3640 | trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3641 | (u8)watch_tree, completion_filter); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3642 | rc = cifs_send_recv(xid, ses, server, |
| 3643 | &rqst, &resp_buftype, flags, &rsp_iov); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3644 | |
| 3645 | if (rc != 0) { |
| 3646 | cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE); |
| 3647 | trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3648 | (u8)watch_tree, completion_filter, rc); |
| 3649 | } else |
| 3650 | trace_smb3_notify_done(xid, persistent_fid, tcon->tid, |
| 3651 | ses->Suid, (u8)watch_tree, completion_filter); |
| 3652 | |
| 3653 | cnotify_exit: |
| 3654 | if (rqst.rq_iov) |
| 3655 | cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */ |
| 3656 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
| 3657 | return rc; |
| 3658 | } |
| 3659 | |
| 3660 | |
| 3661 | |
| 3662 | /* |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3663 | * This is a no-op for now. We're not really interested in the reply, but |
| 3664 | * rather in the fact that the server sent one and that server->lstrp |
| 3665 | * gets updated. |
| 3666 | * |
| 3667 | * FIXME: maybe we should consider checking that the reply matches request? |
| 3668 | */ |
| 3669 | static void |
| 3670 | smb2_echo_callback(struct mid_q_entry *mid) |
| 3671 | { |
| 3672 | struct TCP_Server_Info *server = mid->callback_data; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3673 | struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3674 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3675 | |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 3676 | if (mid->mid_state == MID_RESPONSE_RECEIVED |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3677 | || mid->mid_state == MID_RESPONSE_MALFORMED) { |
| 3678 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 3679 | credits.instance = server->reconnect_instance; |
| 3680 | } |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3681 | |
| 3682 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3683 | add_credits(server, &credits, CIFS_ECHO_OP); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3684 | } |
| 3685 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3686 | void smb2_reconnect_server(struct work_struct *work) |
| 3687 | { |
| 3688 | struct TCP_Server_Info *server = container_of(work, |
| 3689 | struct TCP_Server_Info, reconnect.work); |
| 3690 | struct cifs_ses *ses; |
| 3691 | struct cifs_tcon *tcon, *tcon2; |
| 3692 | struct list_head tmp_list; |
| 3693 | int tcon_exist = false; |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3694 | int rc; |
| 3695 | int resched = false; |
| 3696 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3697 | |
| 3698 | /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */ |
| 3699 | mutex_lock(&server->reconnect_mutex); |
| 3700 | |
| 3701 | INIT_LIST_HEAD(&tmp_list); |
| 3702 | cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n"); |
| 3703 | |
| 3704 | spin_lock(&cifs_tcp_ses_lock); |
| 3705 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
| 3706 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 3707 | if (tcon->need_reconnect || tcon->need_reopen_files) { |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3708 | tcon->tc_count++; |
| 3709 | list_add_tail(&tcon->rlist, &tmp_list); |
| 3710 | tcon_exist = true; |
| 3711 | } |
| 3712 | } |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3713 | /* |
| 3714 | * IPC has the same lifetime as its session and uses its |
| 3715 | * refcount. |
| 3716 | */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3717 | if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) { |
| 3718 | list_add_tail(&ses->tcon_ipc->rlist, &tmp_list); |
| 3719 | tcon_exist = true; |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3720 | ses->ses_count++; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3721 | } |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3722 | } |
| 3723 | /* |
| 3724 | * Get the reference to server struct to be sure that the last call of |
| 3725 | * cifs_put_tcon() in the loop below won't release the server pointer. |
| 3726 | */ |
| 3727 | if (tcon_exist) |
| 3728 | server->srv_count++; |
| 3729 | |
| 3730 | spin_unlock(&cifs_tcp_ses_lock); |
| 3731 | |
| 3732 | list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) { |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3733 | rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3734 | if (!rc) |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 3735 | cifs_reopen_persistent_handles(tcon); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3736 | else |
| 3737 | resched = true; |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3738 | list_del_init(&tcon->rlist); |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3739 | if (tcon->ipc) |
| 3740 | cifs_put_smb_ses(tcon->ses); |
| 3741 | else |
| 3742 | cifs_put_tcon(tcon); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | cifs_dbg(FYI, "Reconnecting tcons finished\n"); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3746 | if (resched) |
| 3747 | queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3748 | mutex_unlock(&server->reconnect_mutex); |
| 3749 | |
| 3750 | /* now we can safely release srv struct */ |
| 3751 | if (tcon_exist) |
| 3752 | cifs_put_tcp_session(server, 1); |
| 3753 | } |
| 3754 | |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3755 | int |
| 3756 | SMB2_echo(struct TCP_Server_Info *server) |
| 3757 | { |
| 3758 | struct smb2_echo_req *req; |
| 3759 | int rc = 0; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3760 | struct kvec iov[1]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3761 | struct smb_rqst rqst = { .rq_iov = iov, |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3762 | .rq_nvec = 1 }; |
Ronnie Sahlberg | 7f7ae75 | 2017-11-09 12:14:21 +1100 | [diff] [blame] | 3763 | unsigned int total_len; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3764 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3765 | cifs_dbg(FYI, "In echo request\n"); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3766 | |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 3767 | if (server->tcpStatus == CifsNeedNegotiate) { |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3768 | /* No need to send echo on newly established connections */ |
Stefan Metzmacher | b08484d | 2020-02-24 14:14:59 +0100 | [diff] [blame] | 3769 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3770 | return rc; |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 3771 | } |
| 3772 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3773 | rc = smb2_plain_req_init(SMB2_ECHO, NULL, server, |
| 3774 | (void **)&req, &total_len); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3775 | if (rc) |
| 3776 | return rc; |
| 3777 | |
Ronnie Sahlberg | 7f7ae75 | 2017-11-09 12:14:21 +1100 | [diff] [blame] | 3778 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3779 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3780 | iov[0].iov_len = total_len; |
| 3781 | iov[0].iov_base = (char *)req; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3782 | |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 3783 | rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 3784 | server, CIFS_ECHO_OP, NULL); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3785 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3786 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3787 | |
| 3788 | cifs_small_buf_release(req); |
| 3789 | return rc; |
| 3790 | } |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3791 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3792 | void |
| 3793 | SMB2_flush_free(struct smb_rqst *rqst) |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3794 | { |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3795 | if (rqst && rqst->rq_iov) |
| 3796 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
| 3797 | } |
| 3798 | |
| 3799 | int |
| 3800 | SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3801 | struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3802 | u64 persistent_fid, u64 volatile_fid) |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3803 | { |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3804 | struct smb2_flush_req *req; |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3805 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | 1f444e4 | 2017-11-20 11:24:39 +1100 | [diff] [blame] | 3806 | unsigned int total_len; |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3807 | int rc; |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3808 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3809 | rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server, |
| 3810 | (void **) &req, &total_len); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3811 | if (rc) |
| 3812 | return rc; |
| 3813 | |
| 3814 | req->PersistentFileId = persistent_fid; |
| 3815 | req->VolatileFileId = volatile_fid; |
| 3816 | |
| 3817 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 1f444e4 | 2017-11-20 11:24:39 +1100 | [diff] [blame] | 3818 | iov[0].iov_len = total_len; |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3819 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3820 | return 0; |
| 3821 | } |
| 3822 | |
| 3823 | int |
| 3824 | SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, |
| 3825 | u64 volatile_fid) |
| 3826 | { |
| 3827 | struct cifs_ses *ses = tcon->ses; |
| 3828 | struct smb_rqst rqst; |
| 3829 | struct kvec iov[1]; |
| 3830 | struct kvec rsp_iov = {NULL, 0}; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3831 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3832 | int resp_buftype = CIFS_NO_BUFFER; |
| 3833 | int flags = 0; |
| 3834 | int rc = 0; |
| 3835 | |
| 3836 | cifs_dbg(FYI, "flush\n"); |
| 3837 | if (!ses || !(ses->server)) |
| 3838 | return -EIO; |
| 3839 | |
| 3840 | if (smb3_encryption_required(tcon)) |
| 3841 | flags |= CIFS_TRANSFORM_REQ; |
| 3842 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3843 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3844 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3845 | rqst.rq_iov = iov; |
| 3846 | rqst.rq_nvec = 1; |
| 3847 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3848 | rc = SMB2_flush_init(xid, &rqst, tcon, server, |
| 3849 | persistent_fid, volatile_fid); |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3850 | if (rc) |
| 3851 | goto flush_exit; |
| 3852 | |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3853 | trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3854 | rc = cifs_send_recv(xid, ses, server, |
| 3855 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3856 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3857 | if (rc != 0) { |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3858 | cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3859 | trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3860 | rc); |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3861 | } else |
| 3862 | trace_smb3_flush_done(xid, persistent_fid, tcon->tid, |
| 3863 | ses->Suid); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3864 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3865 | flush_exit: |
| 3866 | SMB2_flush_free(&rqst); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3867 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3868 | return rc; |
| 3869 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3870 | |
| 3871 | /* |
| 3872 | * To form a chain of read requests, any read requests after the first should |
| 3873 | * have the end_of_chain boolean set to true. |
| 3874 | */ |
| 3875 | static int |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3876 | smb2_new_read_req(void **buf, unsigned int *total_len, |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3877 | struct cifs_io_parms *io_parms, struct cifs_readdata *rdata, |
| 3878 | unsigned int remaining_bytes, int request_type) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3879 | { |
| 3880 | int rc = -EACCES; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3881 | struct smb2_read_plain_req *req = NULL; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3882 | struct smb2_sync_hdr *shdr; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3883 | struct TCP_Server_Info *server = io_parms->server; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3884 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3885 | rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server, |
| 3886 | (void **) &req, total_len); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3887 | if (rc) |
| 3888 | return rc; |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3889 | |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3890 | if (server == NULL) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3891 | return -ECONNABORTED; |
| 3892 | |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3893 | shdr = &req->sync_hdr; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3894 | shdr->ProcessId = cpu_to_le32(io_parms->pid); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3895 | |
| 3896 | req->PersistentFileId = io_parms->persistent_fid; |
| 3897 | req->VolatileFileId = io_parms->volatile_fid; |
| 3898 | req->ReadChannelInfoOffset = 0; /* reserved */ |
| 3899 | req->ReadChannelInfoLength = 0; /* reserved */ |
| 3900 | req->Channel = 0; /* reserved */ |
| 3901 | req->MinimumCount = 0; |
| 3902 | req->Length = cpu_to_le32(io_parms->length); |
| 3903 | req->Offset = cpu_to_le64(io_parms->offset); |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 3904 | |
| 3905 | trace_smb3_read_enter(0 /* xid */, |
| 3906 | io_parms->persistent_fid, |
| 3907 | io_parms->tcon->tid, io_parms->tcon->ses->Suid, |
| 3908 | io_parms->offset, io_parms->length); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3909 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3910 | /* |
| 3911 | * If we want to do a RDMA write, fill in and append |
| 3912 | * smbd_buffer_descriptor_v1 to the end of read request |
| 3913 | */ |
Long Li | bb4c041 | 2018-04-17 12:17:08 -0700 | [diff] [blame] | 3914 | if (server->rdma && rdata && !server->sign && |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3915 | rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3916 | |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3917 | struct smbd_buffer_descriptor_v1 *v1; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3918 | bool need_invalidate = server->dialect == SMB30_PROT_ID; |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3919 | |
| 3920 | rdata->mr = smbd_register_mr( |
| 3921 | server->smbd_conn, rdata->pages, |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 3922 | rdata->nr_pages, rdata->page_offset, |
| 3923 | rdata->tailsz, true, need_invalidate); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3924 | if (!rdata->mr) |
Long Li | b797209 | 2019-04-05 21:36:34 +0000 | [diff] [blame] | 3925 | return -EAGAIN; |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3926 | |
| 3927 | req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; |
| 3928 | if (need_invalidate) |
| 3929 | req->Channel = SMB2_CHANNEL_RDMA_V1; |
| 3930 | req->ReadChannelInfoOffset = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3931 | cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer)); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3932 | req->ReadChannelInfoLength = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3933 | cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1)); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3934 | v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0]; |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3935 | v1->offset = cpu_to_le64(rdata->mr->mr->iova); |
| 3936 | v1->token = cpu_to_le32(rdata->mr->mr->rkey); |
| 3937 | v1->length = cpu_to_le32(rdata->mr->mr->length); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3938 | |
| 3939 | *total_len += sizeof(*v1) - 1; |
| 3940 | } |
| 3941 | #endif |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3942 | if (request_type & CHAINED_REQUEST) { |
| 3943 | if (!(request_type & END_OF_CHAIN)) { |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3944 | /* next 8-byte aligned request */ |
| 3945 | *total_len = DIV_ROUND_UP(*total_len, 8) * 8; |
| 3946 | shdr->NextCommand = cpu_to_le32(*total_len); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3947 | } else /* END_OF_CHAIN */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3948 | shdr->NextCommand = 0; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3949 | if (request_type & RELATED_REQUEST) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3950 | shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3951 | /* |
| 3952 | * Related requests use info from previous read request |
| 3953 | * in chain. |
| 3954 | */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3955 | shdr->SessionId = 0xFFFFFFFF; |
| 3956 | shdr->TreeId = 0xFFFFFFFF; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3957 | req->PersistentFileId = 0xFFFFFFFF; |
| 3958 | req->VolatileFileId = 0xFFFFFFFF; |
| 3959 | } |
| 3960 | } |
| 3961 | if (remaining_bytes > io_parms->length) |
| 3962 | req->RemainingBytes = cpu_to_le32(remaining_bytes); |
| 3963 | else |
| 3964 | req->RemainingBytes = 0; |
| 3965 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3966 | *buf = req; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3967 | return rc; |
| 3968 | } |
| 3969 | |
| 3970 | static void |
| 3971 | smb2_readv_callback(struct mid_q_entry *mid) |
| 3972 | { |
| 3973 | struct cifs_readdata *rdata = mid->callback_data; |
| 3974 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3975 | struct TCP_Server_Info *server = rdata->server; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3976 | struct smb2_sync_hdr *shdr = |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 3977 | (struct smb2_sync_hdr *)rdata->iov[0].iov_base; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3978 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Steve French | 46f17d1 | 2019-09-04 23:07:52 -0500 | [diff] [blame] | 3979 | struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], |
| 3980 | .rq_nvec = 1, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3981 | .rq_pages = rdata->pages, |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3982 | .rq_offset = rdata->page_offset, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3983 | .rq_npages = rdata->nr_pages, |
| 3984 | .rq_pagesz = rdata->pagesz, |
| 3985 | .rq_tailsz = rdata->tailsz }; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3986 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3987 | WARN_ONCE(rdata->server != mid->server, |
| 3988 | "rdata server %p != mid server %p", |
| 3989 | rdata->server, mid->server); |
| 3990 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3991 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
| 3992 | __func__, mid->mid, mid->mid_state, rdata->result, |
| 3993 | rdata->bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3994 | |
| 3995 | switch (mid->mid_state) { |
| 3996 | case MID_RESPONSE_RECEIVED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3997 | credits.value = le16_to_cpu(shdr->CreditRequest); |
| 3998 | credits.instance = server->reconnect_instance; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3999 | /* result already set, check signature */ |
Pavel Shilovsky | 4326ed2 | 2016-11-17 15:24:46 -0800 | [diff] [blame] | 4000 | if (server->sign && !mid->decrypted) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4001 | int rc; |
| 4002 | |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4003 | rc = smb2_verify_signature(&rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4004 | if (rc) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 4005 | cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4006 | rc); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4007 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4008 | /* FIXME: should this be counted toward the initiating task? */ |
Pavel Shilovsky | 34a54d6 | 2014-07-10 10:03:29 +0400 | [diff] [blame] | 4009 | task_io_account_read(rdata->got_bytes); |
| 4010 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4011 | break; |
| 4012 | case MID_REQUEST_SUBMITTED: |
| 4013 | case MID_RETRY_NEEDED: |
| 4014 | rdata->result = -EAGAIN; |
Pavel Shilovsky | d913ed1 | 2014-07-10 11:31:48 +0400 | [diff] [blame] | 4015 | if (server->sign && rdata->got_bytes) |
| 4016 | /* reset bytes number since we can not check a sign */ |
| 4017 | rdata->got_bytes = 0; |
| 4018 | /* FIXME: should this be counted toward the initiating task? */ |
| 4019 | task_io_account_read(rdata->got_bytes); |
| 4020 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4021 | break; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 4022 | case MID_RESPONSE_MALFORMED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4023 | credits.value = le16_to_cpu(shdr->CreditRequest); |
| 4024 | credits.instance = server->reconnect_instance; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 4025 | /* fall through */ |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4026 | default: |
Pavel Shilovsky | 6b15eb1 | 2019-01-18 15:46:14 -0800 | [diff] [blame] | 4027 | rdata->result = -EIO; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4028 | } |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 4029 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4030 | /* |
| 4031 | * If this rdata has a memmory registered, the MR can be freed |
| 4032 | * MR needs to be freed as soon as I/O finishes to prevent deadlock |
| 4033 | * because they have limited number and are used for future I/Os |
| 4034 | */ |
| 4035 | if (rdata->mr) { |
| 4036 | smbd_deregister_mr(rdata->mr); |
| 4037 | rdata->mr = NULL; |
| 4038 | } |
| 4039 | #endif |
Pavel Shilovsky | 082aaa8 | 2019-01-18 15:54:34 -0800 | [diff] [blame] | 4040 | if (rdata->result && rdata->result != -ENODATA) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4041 | cifs_stats_fail_inc(tcon, SMB2_READ_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4042 | trace_smb3_read_err(0 /* xid */, |
| 4043 | rdata->cfile->fid.persistent_fid, |
| 4044 | tcon->tid, tcon->ses->Suid, rdata->offset, |
| 4045 | rdata->bytes, rdata->result); |
| 4046 | } else |
| 4047 | trace_smb3_read_done(0 /* xid */, |
| 4048 | rdata->cfile->fid.persistent_fid, |
| 4049 | tcon->tid, tcon->ses->Suid, |
| 4050 | rdata->offset, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4051 | |
| 4052 | queue_work(cifsiod_wq, &rdata->work); |
| 4053 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4054 | add_credits(server, &credits, 0); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4055 | } |
| 4056 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4057 | /* smb2_async_readv - send an async read, and set up mid to handle result */ |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4058 | int |
| 4059 | smb2_async_readv(struct cifs_readdata *rdata) |
| 4060 | { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4061 | int rc, flags = 0; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4062 | char *buf; |
| 4063 | struct smb2_sync_hdr *shdr; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4064 | struct cifs_io_parms io_parms; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4065 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4066 | .rq_nvec = 1 }; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4067 | struct TCP_Server_Info *server; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4068 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4069 | unsigned int total_len; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4070 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4071 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
| 4072 | __func__, rdata->offset, rdata->bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4073 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4074 | if (!rdata->server) |
| 4075 | rdata->server = cifs_pick_channel(tcon->ses); |
| 4076 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4077 | io_parms.tcon = tlink_tcon(rdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4078 | io_parms.server = server = rdata->server; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4079 | io_parms.offset = rdata->offset; |
| 4080 | io_parms.length = rdata->bytes; |
| 4081 | io_parms.persistent_fid = rdata->cfile->fid.persistent_fid; |
| 4082 | io_parms.volatile_fid = rdata->cfile->fid.volatile_fid; |
| 4083 | io_parms.pid = rdata->pid; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4084 | |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 4085 | rc = smb2_new_read_req( |
| 4086 | (void **) &buf, &total_len, &io_parms, rdata, 0, 0); |
Pavel Shilovsky | f0b93cb | 2019-01-25 11:10:00 -0800 | [diff] [blame] | 4087 | if (rc) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4088 | return rc; |
| 4089 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4090 | if (smb3_encryption_required(io_parms.tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4091 | flags |= CIFS_TRANSFORM_REQ; |
| 4092 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4093 | rdata->iov[0].iov_base = buf; |
| 4094 | rdata->iov[0].iov_len = total_len; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 4095 | |
| 4096 | shdr = (struct smb2_sync_hdr *)buf; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4097 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4098 | if (rdata->credits.value > 0) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4099 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes, |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4100 | SMB2_MAX_BUFFER_SIZE)); |
Pavel Shilovsky | b983f7e | 2018-12-19 22:49:09 +0000 | [diff] [blame] | 4101 | shdr->CreditRequest = |
| 4102 | cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4103 | |
| 4104 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
| 4105 | if (rc) |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4106 | goto async_readv_out; |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4107 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4108 | flags |= CIFS_HAS_CREDITS; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4109 | } |
| 4110 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4111 | kref_get(&rdata->refcount); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4112 | rc = cifs_call_async(server, &rqst, |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4113 | cifs_readv_receive, smb2_readv_callback, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 4114 | smb3_handle_read_data, rdata, flags, |
| 4115 | &rdata->credits); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4116 | if (rc) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4117 | kref_put(&rdata->refcount, cifs_readdata_release); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4118 | cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4119 | trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid, |
| 4120 | io_parms.tcon->tid, |
| 4121 | io_parms.tcon->ses->Suid, |
| 4122 | io_parms.offset, io_parms.length, rc); |
| 4123 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4124 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4125 | async_readv_out: |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4126 | cifs_small_buf_release(buf); |
| 4127 | return rc; |
| 4128 | } |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4129 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4130 | int |
| 4131 | SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 4132 | unsigned int *nbytes, char **buf, int *buf_type) |
| 4133 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4134 | struct smb_rqst rqst; |
Colin Ian King | 1efd4fc | 2019-07-31 10:05:26 +0100 | [diff] [blame] | 4135 | int resp_buftype, rc; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 4136 | struct smb2_read_plain_req *req = NULL; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4137 | struct smb2_read_rsp *rsp = NULL; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4138 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4139 | struct kvec rsp_iov; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4140 | unsigned int total_len; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4141 | int flags = CIFS_LOG_ERROR; |
| 4142 | struct cifs_ses *ses = io_parms->tcon->ses; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4143 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4144 | if (!io_parms->server) |
| 4145 | io_parms->server = cifs_pick_channel(io_parms->tcon->ses); |
| 4146 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4147 | *nbytes = 0; |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 4148 | rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0); |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4149 | if (rc) |
| 4150 | return rc; |
| 4151 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4152 | if (smb3_encryption_required(io_parms->tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4153 | flags |= CIFS_TRANSFORM_REQ; |
| 4154 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4155 | iov[0].iov_base = (char *)req; |
| 4156 | iov[0].iov_len = total_len; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4157 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4158 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4159 | rqst.rq_iov = iov; |
| 4160 | rqst.rq_nvec = 1; |
| 4161 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4162 | rc = cifs_send_recv(xid, ses, io_parms->server, |
| 4163 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4164 | rsp = (struct smb2_read_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4165 | |
| 4166 | if (rc) { |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4167 | if (rc != -ENODATA) { |
| 4168 | cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE); |
| 4169 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4170 | trace_smb3_read_err(xid, req->PersistentFileId, |
| 4171 | io_parms->tcon->tid, ses->Suid, |
| 4172 | io_parms->offset, io_parms->length, |
| 4173 | rc); |
Steve French | b0a42f2 | 2019-02-25 15:02:58 -0600 | [diff] [blame] | 4174 | } else |
| 4175 | trace_smb3_read_done(xid, req->PersistentFileId, |
| 4176 | io_parms->tcon->tid, ses->Suid, |
| 4177 | io_parms->offset, 0); |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4178 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Ronnie Sahlberg | 05fd5c2 | 2019-04-23 16:39:45 +1000 | [diff] [blame] | 4179 | cifs_small_buf_release(req); |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4180 | return rc == -ENODATA ? 0 : rc; |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4181 | } else |
| 4182 | trace_smb3_read_done(xid, req->PersistentFileId, |
| 4183 | io_parms->tcon->tid, ses->Suid, |
| 4184 | io_parms->offset, io_parms->length); |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4185 | |
ZhangXiaoxu | 088aaf1 | 2019-04-06 15:47:39 +0800 | [diff] [blame] | 4186 | cifs_small_buf_release(req); |
| 4187 | |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4188 | *nbytes = le32_to_cpu(rsp->DataLength); |
| 4189 | if ((*nbytes > CIFS_MAX_MSGSIZE) || |
| 4190 | (*nbytes > io_parms->length)) { |
| 4191 | cifs_dbg(FYI, "bad length %d for count %d\n", |
| 4192 | *nbytes, io_parms->length); |
| 4193 | rc = -EIO; |
| 4194 | *nbytes = 0; |
| 4195 | } |
| 4196 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4197 | if (*buf) { |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 4198 | memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4199 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4200 | } else if (resp_buftype != CIFS_NO_BUFFER) { |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4201 | *buf = rsp_iov.iov_base; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4202 | if (resp_buftype == CIFS_SMALL_BUFFER) |
| 4203 | *buf_type = CIFS_SMALL_BUFFER; |
| 4204 | else if (resp_buftype == CIFS_LARGE_BUFFER) |
| 4205 | *buf_type = CIFS_LARGE_BUFFER; |
| 4206 | } |
| 4207 | return rc; |
| 4208 | } |
| 4209 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4210 | /* |
| 4211 | * Check the mid_state and signature on received buffer (if any), and queue the |
| 4212 | * workqueue completion task. |
| 4213 | */ |
| 4214 | static void |
| 4215 | smb2_writev_callback(struct mid_q_entry *mid) |
| 4216 | { |
| 4217 | struct cifs_writedata *wdata = mid->callback_data; |
| 4218 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4219 | struct TCP_Server_Info *server = wdata->server; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4220 | unsigned int written; |
| 4221 | struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4222 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4223 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4224 | WARN_ONCE(wdata->server != mid->server, |
| 4225 | "wdata server %p != mid server %p", |
| 4226 | wdata->server, mid->server); |
| 4227 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4228 | switch (mid->mid_state) { |
| 4229 | case MID_RESPONSE_RECEIVED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4230 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 4231 | credits.instance = server->reconnect_instance; |
| 4232 | wdata->result = smb2_check_receive(mid, server, 0); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4233 | if (wdata->result != 0) |
| 4234 | break; |
| 4235 | |
| 4236 | written = le32_to_cpu(rsp->DataLength); |
| 4237 | /* |
| 4238 | * Mask off high 16 bits when bytes written as returned |
| 4239 | * by the server is greater than bytes requested by the |
| 4240 | * client. OS/2 servers are known to set incorrect |
| 4241 | * CountHigh values. |
| 4242 | */ |
| 4243 | if (written > wdata->bytes) |
| 4244 | written &= 0xFFFF; |
| 4245 | |
| 4246 | if (written < wdata->bytes) |
| 4247 | wdata->result = -ENOSPC; |
| 4248 | else |
| 4249 | wdata->bytes = written; |
| 4250 | break; |
| 4251 | case MID_REQUEST_SUBMITTED: |
| 4252 | case MID_RETRY_NEEDED: |
| 4253 | wdata->result = -EAGAIN; |
| 4254 | break; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 4255 | case MID_RESPONSE_MALFORMED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4256 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 4257 | credits.instance = server->reconnect_instance; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 4258 | /* fall through */ |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4259 | default: |
| 4260 | wdata->result = -EIO; |
| 4261 | break; |
| 4262 | } |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4263 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4264 | /* |
| 4265 | * If this wdata has a memory registered, the MR can be freed |
| 4266 | * The number of MRs available is limited, it's important to recover |
| 4267 | * used MR as soon as I/O is finished. Hold MR longer in the later |
| 4268 | * I/O process can possibly result in I/O deadlock due to lack of MR |
| 4269 | * to send request on I/O retry |
| 4270 | */ |
| 4271 | if (wdata->mr) { |
| 4272 | smbd_deregister_mr(wdata->mr); |
| 4273 | wdata->mr = NULL; |
| 4274 | } |
| 4275 | #endif |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4276 | if (wdata->result) { |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4277 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4278 | trace_smb3_write_err(0 /* no xid */, |
| 4279 | wdata->cfile->fid.persistent_fid, |
| 4280 | tcon->tid, tcon->ses->Suid, wdata->offset, |
| 4281 | wdata->bytes, wdata->result); |
Steve French | d6fd419 | 2020-02-05 16:52:11 -0600 | [diff] [blame] | 4282 | if (wdata->result == -ENOSPC) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 4283 | pr_warn_once("Out of space writing to %s\n", |
| 4284 | tcon->treeName); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4285 | } else |
| 4286 | trace_smb3_write_done(0 /* no xid */, |
| 4287 | wdata->cfile->fid.persistent_fid, |
| 4288 | tcon->tid, tcon->ses->Suid, |
| 4289 | wdata->offset, wdata->bytes); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4290 | |
| 4291 | queue_work(cifsiod_wq, &wdata->work); |
| 4292 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4293 | add_credits(server, &credits, 0); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | /* smb2_async_writev - send an async write, and set up mid to handle result */ |
| 4297 | int |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 4298 | smb2_async_writev(struct cifs_writedata *wdata, |
| 4299 | void (*release)(struct kref *kref)) |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4300 | { |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4301 | int rc = -EACCES, flags = 0; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4302 | struct smb2_write_req *req = NULL; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4303 | struct smb2_sync_hdr *shdr; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4304 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4305 | struct TCP_Server_Info *server = wdata->server; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4306 | struct kvec iov[1]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4307 | struct smb_rqst rqst = { }; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4308 | unsigned int total_len; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4309 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4310 | if (!wdata->server) |
| 4311 | server = wdata->server = cifs_pick_channel(tcon->ses); |
| 4312 | |
| 4313 | rc = smb2_plain_req_init(SMB2_WRITE, tcon, server, |
| 4314 | (void **) &req, &total_len); |
Pavel Shilovsky | f0b93cb | 2019-01-25 11:10:00 -0800 | [diff] [blame] | 4315 | if (rc) |
| 4316 | return rc; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4317 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4318 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4319 | flags |= CIFS_TRANSFORM_REQ; |
| 4320 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4321 | shdr = (struct smb2_sync_hdr *)req; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4322 | shdr->ProcessId = cpu_to_le32(wdata->cfile->pid); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4323 | |
| 4324 | req->PersistentFileId = wdata->cfile->fid.persistent_fid; |
| 4325 | req->VolatileFileId = wdata->cfile->fid.volatile_fid; |
| 4326 | req->WriteChannelInfoOffset = 0; |
| 4327 | req->WriteChannelInfoLength = 0; |
| 4328 | req->Channel = 0; |
| 4329 | req->Offset = cpu_to_le64(wdata->offset); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4330 | req->DataOffset = cpu_to_le16( |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4331 | offsetof(struct smb2_write_req, Buffer)); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4332 | req->RemainingBytes = 0; |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4333 | |
| 4334 | trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid, |
| 4335 | tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4336 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4337 | /* |
| 4338 | * If we want to do a server RDMA read, fill in and append |
| 4339 | * smbd_buffer_descriptor_v1 to the end of write request |
| 4340 | */ |
Long Li | bb4c041 | 2018-04-17 12:17:08 -0700 | [diff] [blame] | 4341 | if (server->rdma && !server->sign && wdata->bytes >= |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4342 | server->smbd_conn->rdma_readwrite_threshold) { |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4343 | |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4344 | struct smbd_buffer_descriptor_v1 *v1; |
| 4345 | bool need_invalidate = server->dialect == SMB30_PROT_ID; |
| 4346 | |
| 4347 | wdata->mr = smbd_register_mr( |
| 4348 | server->smbd_conn, wdata->pages, |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 4349 | wdata->nr_pages, wdata->page_offset, |
| 4350 | wdata->tailsz, false, need_invalidate); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4351 | if (!wdata->mr) { |
Long Li | b797209 | 2019-04-05 21:36:34 +0000 | [diff] [blame] | 4352 | rc = -EAGAIN; |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4353 | goto async_writev_out; |
| 4354 | } |
| 4355 | req->Length = 0; |
| 4356 | req->DataOffset = 0; |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 4357 | if (wdata->nr_pages > 1) |
| 4358 | req->RemainingBytes = |
| 4359 | cpu_to_le32( |
| 4360 | (wdata->nr_pages - 1) * wdata->pagesz - |
| 4361 | wdata->page_offset + wdata->tailsz |
| 4362 | ); |
| 4363 | else |
| 4364 | req->RemainingBytes = cpu_to_le32(wdata->tailsz); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4365 | req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; |
| 4366 | if (need_invalidate) |
| 4367 | req->Channel = SMB2_CHANNEL_RDMA_V1; |
| 4368 | req->WriteChannelInfoOffset = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4369 | cpu_to_le16(offsetof(struct smb2_write_req, Buffer)); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4370 | req->WriteChannelInfoLength = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4371 | cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1)); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4372 | v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0]; |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4373 | v1->offset = cpu_to_le64(wdata->mr->mr->iova); |
| 4374 | v1->token = cpu_to_le32(wdata->mr->mr->rkey); |
| 4375 | v1->length = cpu_to_le32(wdata->mr->mr->length); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4376 | } |
| 4377 | #endif |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4378 | iov[0].iov_len = total_len - 1; |
| 4379 | iov[0].iov_base = (char *)req; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4380 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4381 | rqst.rq_iov = iov; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4382 | rqst.rq_nvec = 1; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 4383 | rqst.rq_pages = wdata->pages; |
Long Li | 57a929a | 2018-05-30 12:47:53 -0700 | [diff] [blame] | 4384 | rqst.rq_offset = wdata->page_offset; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 4385 | rqst.rq_npages = wdata->nr_pages; |
| 4386 | rqst.rq_pagesz = wdata->pagesz; |
| 4387 | rqst.rq_tailsz = wdata->tailsz; |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4388 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4389 | if (wdata->mr) { |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4390 | iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4391 | rqst.rq_npages = 0; |
| 4392 | } |
| 4393 | #endif |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4394 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
| 4395 | wdata->offset, wdata->bytes); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4396 | |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4397 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4398 | /* For RDMA read, I/O size is in RemainingBytes not in Length */ |
| 4399 | if (!wdata->mr) |
| 4400 | req->Length = cpu_to_le32(wdata->bytes); |
| 4401 | #else |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4402 | req->Length = cpu_to_le32(wdata->bytes); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4403 | #endif |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4404 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4405 | if (wdata->credits.value > 0) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4406 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes, |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4407 | SMB2_MAX_BUFFER_SIZE)); |
Pavel Shilovsky | b983f7e | 2018-12-19 22:49:09 +0000 | [diff] [blame] | 4408 | shdr->CreditRequest = |
| 4409 | cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4410 | |
| 4411 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
| 4412 | if (rc) |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4413 | goto async_writev_out; |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4414 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4415 | flags |= CIFS_HAS_CREDITS; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4416 | } |
| 4417 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4418 | kref_get(&wdata->refcount); |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 4419 | rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 4420 | wdata, flags, &wdata->credits); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4421 | |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4422 | if (rc) { |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4423 | trace_smb3_write_err(0 /* no xid */, req->PersistentFileId, |
| 4424 | tcon->tid, tcon->ses->Suid, wdata->offset, |
| 4425 | wdata->bytes, rc); |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 4426 | kref_put(&wdata->refcount, release); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4427 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4428 | } |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4429 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4430 | async_writev_out: |
| 4431 | cifs_small_buf_release(req); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4432 | return rc; |
| 4433 | } |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4434 | |
| 4435 | /* |
| 4436 | * SMB2_write function gets iov pointer to kvec array with n_vec as a length. |
| 4437 | * The length field from io_parms must be at least 1 and indicates a number of |
| 4438 | * elements with data to write that begins with position 1 in iov array. All |
| 4439 | * data length is specified by count. |
| 4440 | */ |
| 4441 | int |
| 4442 | SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 4443 | unsigned int *nbytes, struct kvec *iov, int n_vec) |
| 4444 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4445 | struct smb_rqst rqst; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4446 | int rc = 0; |
| 4447 | struct smb2_write_req *req = NULL; |
| 4448 | struct smb2_write_rsp *rsp = NULL; |
| 4449 | int resp_buftype; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4450 | struct kvec rsp_iov; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4451 | int flags = 0; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4452 | unsigned int total_len; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4453 | struct TCP_Server_Info *server; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4454 | |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4455 | *nbytes = 0; |
| 4456 | |
| 4457 | if (n_vec < 1) |
| 4458 | return rc; |
| 4459 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4460 | if (!io_parms->server) |
| 4461 | io_parms->server = cifs_pick_channel(io_parms->tcon->ses); |
| 4462 | server = io_parms->server; |
| 4463 | if (server == NULL) |
| 4464 | return -ECONNABORTED; |
| 4465 | |
| 4466 | rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server, |
| 4467 | (void **) &req, &total_len); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4468 | if (rc) |
| 4469 | return rc; |
| 4470 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4471 | if (smb3_encryption_required(io_parms->tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4472 | flags |= CIFS_TRANSFORM_REQ; |
| 4473 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4474 | req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4475 | |
| 4476 | req->PersistentFileId = io_parms->persistent_fid; |
| 4477 | req->VolatileFileId = io_parms->volatile_fid; |
| 4478 | req->WriteChannelInfoOffset = 0; |
| 4479 | req->WriteChannelInfoLength = 0; |
| 4480 | req->Channel = 0; |
| 4481 | req->Length = cpu_to_le32(io_parms->length); |
| 4482 | req->Offset = cpu_to_le64(io_parms->offset); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4483 | req->DataOffset = cpu_to_le16( |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4484 | offsetof(struct smb2_write_req, Buffer)); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4485 | req->RemainingBytes = 0; |
| 4486 | |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4487 | trace_smb3_write_enter(xid, io_parms->persistent_fid, |
| 4488 | io_parms->tcon->tid, io_parms->tcon->ses->Suid, |
| 4489 | io_parms->offset, io_parms->length); |
| 4490 | |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4491 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4492 | /* 1 for Buffer */ |
| 4493 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4494 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4495 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4496 | rqst.rq_iov = iov; |
| 4497 | rqst.rq_nvec = n_vec + 1; |
| 4498 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4499 | rc = cifs_send_recv(xid, io_parms->tcon->ses, server, |
| 4500 | &rqst, |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4501 | &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4502 | rsp = (struct smb2_write_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4503 | |
| 4504 | if (rc) { |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4505 | trace_smb3_write_err(xid, req->PersistentFileId, |
| 4506 | io_parms->tcon->tid, |
| 4507 | io_parms->tcon->ses->Suid, |
| 4508 | io_parms->offset, io_parms->length, rc); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4509 | cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4510 | cifs_dbg(VFS, "Send error in write = %d\n", rc); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4511 | } else { |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4512 | *nbytes = le32_to_cpu(rsp->DataLength); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4513 | trace_smb3_write_done(xid, req->PersistentFileId, |
| 4514 | io_parms->tcon->tid, |
| 4515 | io_parms->tcon->ses->Suid, |
| 4516 | io_parms->offset, *nbytes); |
| 4517 | } |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4518 | |
ZhangXiaoxu | 6a3eb33 | 2019-04-06 15:47:38 +0800 | [diff] [blame] | 4519 | cifs_small_buf_release(req); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4520 | free_rsp_buf(resp_buftype, rsp); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4521 | return rc; |
| 4522 | } |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4523 | |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 4524 | int posix_info_sid_size(const void *beg, const void *end) |
Aurelien Aptel | 349e13a | 2020-02-08 15:50:57 +0100 | [diff] [blame] | 4525 | { |
| 4526 | size_t subauth; |
| 4527 | int total; |
| 4528 | |
| 4529 | if (beg + 1 > end) |
| 4530 | return -1; |
| 4531 | |
| 4532 | subauth = *(u8 *)(beg+1); |
| 4533 | if (subauth < 1 || subauth > 15) |
| 4534 | return -1; |
| 4535 | |
| 4536 | total = 1 + 1 + 6 + 4*subauth; |
| 4537 | if (beg + total > end) |
| 4538 | return -1; |
| 4539 | |
| 4540 | return total; |
| 4541 | } |
| 4542 | |
| 4543 | int posix_info_parse(const void *beg, const void *end, |
| 4544 | struct smb2_posix_info_parsed *out) |
| 4545 | |
| 4546 | { |
| 4547 | int total_len = 0; |
| 4548 | int sid_len; |
| 4549 | int name_len; |
| 4550 | const void *owner_sid; |
| 4551 | const void *group_sid; |
| 4552 | const void *name; |
| 4553 | |
| 4554 | /* if no end bound given, assume payload to be correct */ |
| 4555 | if (!end) { |
| 4556 | const struct smb2_posix_info *p = beg; |
| 4557 | |
| 4558 | end = beg + le32_to_cpu(p->NextEntryOffset); |
| 4559 | /* last element will have a 0 offset, pick a sensible bound */ |
| 4560 | if (end == beg) |
| 4561 | end += 0xFFFF; |
| 4562 | } |
| 4563 | |
| 4564 | /* check base buf */ |
| 4565 | if (beg + sizeof(struct smb2_posix_info) > end) |
| 4566 | return -1; |
| 4567 | total_len = sizeof(struct smb2_posix_info); |
| 4568 | |
| 4569 | /* check owner sid */ |
| 4570 | owner_sid = beg + total_len; |
| 4571 | sid_len = posix_info_sid_size(owner_sid, end); |
| 4572 | if (sid_len < 0) |
| 4573 | return -1; |
| 4574 | total_len += sid_len; |
| 4575 | |
| 4576 | /* check group sid */ |
| 4577 | group_sid = beg + total_len; |
| 4578 | sid_len = posix_info_sid_size(group_sid, end); |
| 4579 | if (sid_len < 0) |
| 4580 | return -1; |
| 4581 | total_len += sid_len; |
| 4582 | |
| 4583 | /* check name len */ |
| 4584 | if (beg + total_len + 4 > end) |
| 4585 | return -1; |
| 4586 | name_len = le32_to_cpu(*(__le32 *)(beg + total_len)); |
| 4587 | if (name_len < 1 || name_len > 0xFFFF) |
| 4588 | return -1; |
| 4589 | total_len += 4; |
| 4590 | |
| 4591 | /* check name */ |
| 4592 | name = beg + total_len; |
| 4593 | if (name + name_len > end) |
| 4594 | return -1; |
| 4595 | total_len += name_len; |
| 4596 | |
| 4597 | if (out) { |
| 4598 | out->base = beg; |
| 4599 | out->size = total_len; |
| 4600 | out->name_len = name_len; |
| 4601 | out->name = name; |
| 4602 | memcpy(&out->owner, owner_sid, |
| 4603 | posix_info_sid_size(owner_sid, end)); |
| 4604 | memcpy(&out->group, group_sid, |
| 4605 | posix_info_sid_size(group_sid, end)); |
| 4606 | } |
| 4607 | return total_len; |
| 4608 | } |
| 4609 | |
| 4610 | static int posix_info_extra_size(const void *beg, const void *end) |
| 4611 | { |
| 4612 | int len = posix_info_parse(beg, end, NULL); |
| 4613 | |
| 4614 | if (len < 0) |
| 4615 | return -1; |
| 4616 | return len - sizeof(struct smb2_posix_info); |
| 4617 | } |
| 4618 | |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4619 | static unsigned int |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4620 | num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry, |
| 4621 | size_t size) |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4622 | { |
| 4623 | int len; |
| 4624 | unsigned int entrycount = 0; |
| 4625 | unsigned int next_offset = 0; |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4626 | char *entryptr; |
| 4627 | FILE_DIRECTORY_INFO *dir_info; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4628 | |
| 4629 | if (bufstart == NULL) |
| 4630 | return 0; |
| 4631 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4632 | entryptr = bufstart; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4633 | |
| 4634 | while (1) { |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4635 | if (entryptr + next_offset < entryptr || |
| 4636 | entryptr + next_offset > end_of_buf || |
| 4637 | entryptr + next_offset + size > end_of_buf) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4638 | cifs_dbg(VFS, "malformed search entry would overflow\n"); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4639 | break; |
| 4640 | } |
| 4641 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4642 | entryptr = entryptr + next_offset; |
| 4643 | dir_info = (FILE_DIRECTORY_INFO *)entryptr; |
| 4644 | |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4645 | if (infotype == SMB_FIND_FILE_POSIX_INFO) |
| 4646 | len = posix_info_extra_size(entryptr, end_of_buf); |
| 4647 | else |
| 4648 | len = le32_to_cpu(dir_info->FileNameLength); |
| 4649 | |
| 4650 | if (len < 0 || |
| 4651 | entryptr + len < entryptr || |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4652 | entryptr + len > end_of_buf || |
| 4653 | entryptr + len + size > end_of_buf) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4654 | cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", |
| 4655 | end_of_buf); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4656 | break; |
| 4657 | } |
| 4658 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4659 | *lastentry = entryptr; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4660 | entrycount++; |
| 4661 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4662 | next_offset = le32_to_cpu(dir_info->NextEntryOffset); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4663 | if (!next_offset) |
| 4664 | break; |
| 4665 | } |
| 4666 | |
| 4667 | return entrycount; |
| 4668 | } |
| 4669 | |
| 4670 | /* |
| 4671 | * Readdir/FindFirst |
| 4672 | */ |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4673 | int SMB2_query_directory_init(const unsigned int xid, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4674 | struct cifs_tcon *tcon, |
| 4675 | struct TCP_Server_Info *server, |
| 4676 | struct smb_rqst *rqst, |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4677 | u64 persistent_fid, u64 volatile_fid, |
| 4678 | int index, int info_level) |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4679 | { |
| 4680 | struct smb2_query_directory_req *req; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4681 | unsigned char *bufptr; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4682 | __le16 asteriks = cpu_to_le16('*'); |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4683 | unsigned int output_size = CIFSMaxBufSize - |
| 4684 | MAX_SMB2_CREATE_RESPONSE_SIZE - |
| 4685 | MAX_SMB2_CLOSE_RESPONSE_SIZE; |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4686 | unsigned int total_len; |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4687 | struct kvec *iov = rqst->rq_iov; |
| 4688 | int len, rc; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4689 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4690 | rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server, |
| 4691 | (void **) &req, &total_len); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4692 | if (rc) |
| 4693 | return rc; |
| 4694 | |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4695 | switch (info_level) { |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4696 | case SMB_FIND_FILE_DIRECTORY_INFO: |
| 4697 | req->FileInformationClass = FILE_DIRECTORY_INFORMATION; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4698 | break; |
| 4699 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: |
| 4700 | req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4701 | break; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4702 | case SMB_FIND_FILE_POSIX_INFO: |
| 4703 | req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO; |
| 4704 | break; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4705 | default: |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 4706 | cifs_tcon_dbg(VFS, "info level %u isn't supported\n", |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4707 | info_level); |
| 4708 | return -EINVAL; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | req->FileIndex = cpu_to_le32(index); |
| 4712 | req->PersistentFileId = persistent_fid; |
| 4713 | req->VolatileFileId = volatile_fid; |
| 4714 | |
| 4715 | len = 0x2; |
| 4716 | bufptr = req->Buffer; |
| 4717 | memcpy(bufptr, &asteriks, len); |
| 4718 | |
| 4719 | req->FileNameOffset = |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4720 | cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4721 | req->FileNameLength = cpu_to_le16(len); |
| 4722 | /* |
| 4723 | * BB could be 30 bytes or so longer if we used SMB2 specific |
| 4724 | * buffer lengths, but this is safe and close enough. |
| 4725 | */ |
| 4726 | output_size = min_t(unsigned int, output_size, server->maxBuf); |
| 4727 | output_size = min_t(unsigned int, output_size, 2 << 15); |
| 4728 | req->OutputBufferLength = cpu_to_le32(output_size); |
| 4729 | |
| 4730 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4731 | /* 1 for Buffer */ |
| 4732 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4733 | |
| 4734 | iov[1].iov_base = (char *)(req->Buffer); |
| 4735 | iov[1].iov_len = len; |
| 4736 | |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4737 | trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid, |
| 4738 | tcon->ses->Suid, index, output_size); |
| 4739 | |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4740 | return 0; |
| 4741 | } |
| 4742 | |
| 4743 | void SMB2_query_directory_free(struct smb_rqst *rqst) |
| 4744 | { |
| 4745 | if (rqst && rqst->rq_iov) { |
| 4746 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
| 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | int |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4751 | smb2_parse_query_directory(struct cifs_tcon *tcon, |
| 4752 | struct kvec *rsp_iov, |
| 4753 | int resp_buftype, |
| 4754 | struct cifs_search_info *srch_inf) |
| 4755 | { |
| 4756 | struct smb2_query_directory_rsp *rsp; |
| 4757 | size_t info_buf_size; |
| 4758 | char *end_of_smb; |
| 4759 | int rc; |
| 4760 | |
| 4761 | rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base; |
| 4762 | |
| 4763 | switch (srch_inf->info_level) { |
| 4764 | case SMB_FIND_FILE_DIRECTORY_INFO: |
| 4765 | info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1; |
| 4766 | break; |
| 4767 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: |
| 4768 | info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1; |
| 4769 | break; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4770 | case SMB_FIND_FILE_POSIX_INFO: |
| 4771 | /* note that posix payload are variable size */ |
| 4772 | info_buf_size = sizeof(struct smb2_posix_info); |
| 4773 | break; |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4774 | default: |
| 4775 | cifs_tcon_dbg(VFS, "info level %u isn't supported\n", |
| 4776 | srch_inf->info_level); |
| 4777 | return -EINVAL; |
| 4778 | } |
| 4779 | |
| 4780 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 4781 | le32_to_cpu(rsp->OutputBufferLength), rsp_iov, |
| 4782 | info_buf_size); |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4783 | if (rc) { |
| 4784 | cifs_tcon_dbg(VFS, "bad info payload"); |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4785 | return rc; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4786 | } |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4787 | |
| 4788 | srch_inf->unicode = true; |
| 4789 | |
| 4790 | if (srch_inf->ntwrk_buf_start) { |
| 4791 | if (srch_inf->smallBuf) |
| 4792 | cifs_small_buf_release(srch_inf->ntwrk_buf_start); |
| 4793 | else |
| 4794 | cifs_buf_release(srch_inf->ntwrk_buf_start); |
| 4795 | } |
| 4796 | srch_inf->ntwrk_buf_start = (char *)rsp; |
| 4797 | srch_inf->srch_entries_start = srch_inf->last_entry = |
| 4798 | (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset); |
| 4799 | end_of_smb = rsp_iov->iov_len + (char *)rsp; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4800 | |
| 4801 | srch_inf->entries_in_buffer = num_entries( |
| 4802 | srch_inf->info_level, |
| 4803 | srch_inf->srch_entries_start, |
| 4804 | end_of_smb, |
| 4805 | &srch_inf->last_entry, |
| 4806 | info_buf_size); |
| 4807 | |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4808 | srch_inf->index_of_last_entry += srch_inf->entries_in_buffer; |
| 4809 | cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n", |
| 4810 | srch_inf->entries_in_buffer, srch_inf->index_of_last_entry, |
| 4811 | srch_inf->srch_entries_start, srch_inf->last_entry); |
| 4812 | if (resp_buftype == CIFS_LARGE_BUFFER) |
| 4813 | srch_inf->smallBuf = false; |
| 4814 | else if (resp_buftype == CIFS_SMALL_BUFFER) |
| 4815 | srch_inf->smallBuf = true; |
| 4816 | else |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 4817 | cifs_tcon_dbg(VFS, "Invalid search buffer type\n"); |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4818 | |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | int |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4823 | SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, |
| 4824 | u64 persistent_fid, u64 volatile_fid, int index, |
| 4825 | struct cifs_search_info *srch_inf) |
| 4826 | { |
| 4827 | struct smb_rqst rqst; |
| 4828 | struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE]; |
| 4829 | struct smb2_query_directory_rsp *rsp = NULL; |
| 4830 | int resp_buftype = CIFS_NO_BUFFER; |
| 4831 | struct kvec rsp_iov; |
| 4832 | int rc = 0; |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4833 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4834 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4835 | int flags = 0; |
| 4836 | |
YueHaibing | c4985c3 | 2020-01-17 10:57:17 +0800 | [diff] [blame] | 4837 | if (!ses || !(ses->server)) |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4838 | return -EIO; |
| 4839 | |
| 4840 | if (smb3_encryption_required(tcon)) |
| 4841 | flags |= CIFS_TRANSFORM_REQ; |
| 4842 | |
| 4843 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4844 | memset(&iov, 0, sizeof(iov)); |
| 4845 | rqst.rq_iov = iov; |
| 4846 | rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE; |
| 4847 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4848 | rc = SMB2_query_directory_init(xid, tcon, server, |
| 4849 | &rqst, persistent_fid, |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4850 | volatile_fid, index, |
| 4851 | srch_inf->info_level); |
| 4852 | if (rc) |
| 4853 | goto qdir_exit; |
| 4854 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4855 | rc = cifs_send_recv(xid, ses, server, |
| 4856 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4857 | rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4858 | |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4859 | if (rc) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4860 | if (rc == -ENODATA && |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 4861 | rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) { |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4862 | trace_smb3_query_dir_done(xid, persistent_fid, |
| 4863 | tcon->tid, tcon->ses->Suid, index, 0); |
Pavel Shilovsky | 5275580 | 2014-08-18 20:49:57 +0400 | [diff] [blame] | 4864 | srch_inf->endOfSearch = true; |
| 4865 | rc = 0; |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4866 | } else { |
| 4867 | trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, |
| 4868 | tcon->ses->Suid, index, 0, rc); |
Pavel Shilovsky | 8e6e72a | 2019-01-26 12:21:32 -0800 | [diff] [blame] | 4869 | cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE); |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4870 | } |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4871 | goto qdir_exit; |
| 4872 | } |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4873 | |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4874 | rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype, |
| 4875 | srch_inf); |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4876 | if (rc) { |
| 4877 | trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, |
| 4878 | tcon->ses->Suid, index, 0, rc); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4879 | goto qdir_exit; |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4880 | } |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4881 | resp_buftype = CIFS_NO_BUFFER; |
| 4882 | |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4883 | trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid, |
| 4884 | tcon->ses->Suid, index, srch_inf->entries_in_buffer); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4885 | |
| 4886 | qdir_exit: |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4887 | SMB2_query_directory_free(&rqst); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4888 | free_rsp_buf(resp_buftype, rsp); |
| 4889 | return rc; |
| 4890 | } |
| 4891 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4892 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4893 | SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 4894 | struct smb_rqst *rqst, |
| 4895 | u64 persistent_fid, u64 volatile_fid, u32 pid, |
| 4896 | u8 info_class, u8 info_type, u32 additional_info, |
| 4897 | void **data, unsigned int *size) |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4898 | { |
| 4899 | struct smb2_set_info_req *req; |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4900 | struct kvec *iov = rqst->rq_iov; |
| 4901 | unsigned int i, total_len; |
| 4902 | int rc; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4903 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4904 | rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server, |
| 4905 | (void **) &req, &total_len); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4906 | if (rc) |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4907 | return rc; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4908 | |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4909 | req->sync_hdr.ProcessId = cpu_to_le32(pid); |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4910 | req->InfoType = info_type; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4911 | req->FileInfoClass = info_class; |
| 4912 | req->PersistentFileId = persistent_fid; |
| 4913 | req->VolatileFileId = volatile_fid; |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4914 | req->AdditionalInformation = cpu_to_le32(additional_info); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4915 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4916 | req->BufferOffset = |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4917 | cpu_to_le16(sizeof(struct smb2_set_info_req) - 1); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4918 | req->BufferLength = cpu_to_le32(*size); |
| 4919 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4920 | memcpy(req->Buffer, *data, *size); |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4921 | total_len += *size; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4922 | |
| 4923 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4924 | /* 1 for Buffer */ |
| 4925 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4926 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4927 | for (i = 1; i < rqst->rq_nvec; i++) { |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4928 | le32_add_cpu(&req->BufferLength, size[i]); |
| 4929 | iov[i].iov_base = (char *)data[i]; |
| 4930 | iov[i].iov_len = size[i]; |
| 4931 | } |
| 4932 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4933 | return 0; |
| 4934 | } |
| 4935 | |
| 4936 | void |
| 4937 | SMB2_set_info_free(struct smb_rqst *rqst) |
| 4938 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 4939 | if (rqst && rqst->rq_iov) |
| 4940 | cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | static int |
| 4944 | send_set_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 4945 | u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class, |
| 4946 | u8 info_type, u32 additional_info, unsigned int num, |
| 4947 | void **data, unsigned int *size) |
| 4948 | { |
| 4949 | struct smb_rqst rqst; |
| 4950 | struct smb2_set_info_rsp *rsp = NULL; |
| 4951 | struct kvec *iov; |
| 4952 | struct kvec rsp_iov; |
| 4953 | int rc = 0; |
| 4954 | int resp_buftype; |
| 4955 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4956 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4957 | int flags = 0; |
| 4958 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4959 | if (!ses || !server) |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4960 | return -EIO; |
| 4961 | |
| 4962 | if (!num) |
| 4963 | return -EINVAL; |
| 4964 | |
| 4965 | if (smb3_encryption_required(tcon)) |
| 4966 | flags |= CIFS_TRANSFORM_REQ; |
| 4967 | |
| 4968 | iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL); |
| 4969 | if (!iov) |
| 4970 | return -ENOMEM; |
| 4971 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4972 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4973 | rqst.rq_iov = iov; |
| 4974 | rqst.rq_nvec = num; |
| 4975 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4976 | rc = SMB2_set_info_init(tcon, server, |
| 4977 | &rqst, persistent_fid, volatile_fid, pid, |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4978 | info_class, info_type, additional_info, |
| 4979 | data, size); |
| 4980 | if (rc) { |
| 4981 | kfree(iov); |
| 4982 | return rc; |
| 4983 | } |
| 4984 | |
| 4985 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4986 | rc = cifs_send_recv(xid, ses, server, |
| 4987 | &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4988 | &rsp_iov); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4989 | SMB2_set_info_free(&rqst); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4990 | rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4991 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4992 | if (rc != 0) { |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4993 | cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4994 | trace_smb3_set_info_err(xid, persistent_fid, tcon->tid, |
| 4995 | ses->Suid, info_class, (__u32)info_type, rc); |
| 4996 | } |
Steve French | 7d3fb24 | 2013-11-18 09:56:28 -0600 | [diff] [blame] | 4997 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4998 | free_rsp_buf(resp_buftype, rsp); |
| 4999 | kfree(iov); |
| 5000 | return rc; |
| 5001 | } |
| 5002 | |
| 5003 | int |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 5004 | SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, |
Ronnie Sahlberg | 3764cbd | 2018-09-03 13:33:47 +1000 | [diff] [blame] | 5005 | u64 volatile_fid, u32 pid, __le64 *eof) |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 5006 | { |
| 5007 | struct smb2_file_eof_info info; |
| 5008 | void *data; |
| 5009 | unsigned int size; |
| 5010 | |
| 5011 | info.EndOfFile = *eof; |
| 5012 | |
| 5013 | data = &info; |
| 5014 | size = sizeof(struct smb2_file_eof_info); |
| 5015 | |
Ronnie Sahlberg | 3764cbd | 2018-09-03 13:33:47 +1000 | [diff] [blame] | 5016 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 5017 | pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE, |
| 5018 | 0, 1, &data, &size); |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 5019 | } |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 5020 | |
| 5021 | int |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 5022 | SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon, |
| 5023 | u64 persistent_fid, u64 volatile_fid, |
| 5024 | struct cifs_ntsd *pnntsd, int pacllen, int aclflag) |
| 5025 | { |
| 5026 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
| 5027 | current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag, |
| 5028 | 1, (void **)&pnntsd, &pacllen); |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 5029 | } |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5030 | |
| 5031 | int |
Ronnie Sahlberg | 5517554 | 2017-08-24 11:24:56 +1000 | [diff] [blame] | 5032 | SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, |
| 5033 | u64 persistent_fid, u64 volatile_fid, |
| 5034 | struct smb2_file_full_ea_info *buf, int len) |
| 5035 | { |
| 5036 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
| 5037 | current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, |
| 5038 | 0, 1, (void **)&buf, &len); |
| 5039 | } |
| 5040 | |
| 5041 | int |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5042 | SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, |
| 5043 | const u64 persistent_fid, const u64 volatile_fid, |
| 5044 | __u8 oplock_level) |
| 5045 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5046 | struct smb_rqst rqst; |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5047 | int rc; |
Ronnie Sahlberg | 0d5a288 | 2018-06-01 10:53:03 +1000 | [diff] [blame] | 5048 | struct smb2_oplock_break *req = NULL; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 5049 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5050 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5051 | int flags = CIFS_OBREAK_OP; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 5052 | unsigned int total_len; |
| 5053 | struct kvec iov[1]; |
| 5054 | struct kvec rsp_iov; |
| 5055 | int resp_buf_type; |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5056 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5057 | cifs_dbg(FYI, "SMB2_oplock_break\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5058 | rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, |
| 5059 | (void **) &req, &total_len); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5060 | if (rc) |
| 5061 | return rc; |
| 5062 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5063 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5064 | flags |= CIFS_TRANSFORM_REQ; |
| 5065 | |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5066 | req->VolatileFid = volatile_fid; |
| 5067 | req->PersistentFid = persistent_fid; |
| 5068 | req->OplockLevel = oplock_level; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 5069 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5070 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 5071 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 5072 | |
| 5073 | iov[0].iov_base = (char *)req; |
| 5074 | iov[0].iov_len = total_len; |
| 5075 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5076 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5077 | rqst.rq_iov = iov; |
| 5078 | rqst.rq_nvec = 1; |
| 5079 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5080 | rc = cifs_send_recv(xid, ses, server, |
| 5081 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5082 | cifs_small_buf_release(req); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5083 | |
| 5084 | if (rc) { |
| 5085 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5086 | cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | return rc; |
| 5090 | } |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5091 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5092 | void |
| 5093 | smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, |
| 5094 | struct kstatfs *kst) |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5095 | { |
| 5096 | kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) * |
| 5097 | le32_to_cpu(pfs_inf->SectorsPerAllocationUnit); |
| 5098 | kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits); |
Sachin Prabhu | 42bec21 | 2017-08-03 13:09:03 +0530 | [diff] [blame] | 5099 | kst->f_bfree = kst->f_bavail = |
| 5100 | le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5101 | return; |
| 5102 | } |
| 5103 | |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5104 | static void |
| 5105 | copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data, |
| 5106 | struct kstatfs *kst) |
| 5107 | { |
| 5108 | kst->f_bsize = le32_to_cpu(response_data->BlockSize); |
| 5109 | kst->f_blocks = le64_to_cpu(response_data->TotalBlocks); |
| 5110 | kst->f_bfree = le64_to_cpu(response_data->BlocksAvail); |
| 5111 | if (response_data->UserBlocksAvail == cpu_to_le64(-1)) |
| 5112 | kst->f_bavail = kst->f_bfree; |
| 5113 | else |
| 5114 | kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail); |
| 5115 | if (response_data->TotalFileNodes != cpu_to_le64(-1)) |
| 5116 | kst->f_files = le64_to_cpu(response_data->TotalFileNodes); |
| 5117 | if (response_data->FreeFileNodes != cpu_to_le64(-1)) |
| 5118 | kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes); |
| 5119 | |
| 5120 | return; |
| 5121 | } |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5122 | |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5123 | static int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5124 | build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, |
| 5125 | struct TCP_Server_Info *server, |
| 5126 | int level, int outbuf_len, u64 persistent_fid, |
| 5127 | u64 volatile_fid) |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5128 | { |
| 5129 | int rc; |
| 5130 | struct smb2_query_info_req *req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5131 | unsigned int total_len; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5132 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5133 | cifs_dbg(FYI, "Query FSInfo level %d\n", level); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5134 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5135 | if ((tcon->ses == NULL) || server == NULL) |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5136 | return -EIO; |
| 5137 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5138 | rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server, |
| 5139 | (void **) &req, &total_len); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5140 | if (rc) |
| 5141 | return rc; |
| 5142 | |
| 5143 | req->InfoType = SMB2_O_INFO_FILESYSTEM; |
| 5144 | req->FileInfoClass = level; |
| 5145 | req->PersistentFileId = persistent_fid; |
| 5146 | req->VolatileFileId = volatile_fid; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5147 | /* 1 for pad */ |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5148 | req->InputBufferOffset = |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5149 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5150 | req->OutputBufferLength = cpu_to_le32( |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5151 | outbuf_len + sizeof(struct smb2_query_info_rsp) - 1); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5152 | |
| 5153 | iov->iov_base = (char *)req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5154 | iov->iov_len = total_len; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5155 | return 0; |
| 5156 | } |
| 5157 | |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5158 | int |
| 5159 | SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 5160 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) |
| 5161 | { |
| 5162 | struct smb_rqst rqst; |
| 5163 | struct smb2_query_info_rsp *rsp = NULL; |
| 5164 | struct kvec iov; |
| 5165 | struct kvec rsp_iov; |
| 5166 | int rc = 0; |
| 5167 | int resp_buftype; |
| 5168 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5169 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5170 | FILE_SYSTEM_POSIX_INFO *info = NULL; |
| 5171 | int flags = 0; |
| 5172 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5173 | rc = build_qfs_info_req(&iov, tcon, server, |
| 5174 | FS_POSIX_INFORMATION, |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5175 | sizeof(FILE_SYSTEM_POSIX_INFO), |
| 5176 | persistent_fid, volatile_fid); |
| 5177 | if (rc) |
| 5178 | return rc; |
| 5179 | |
| 5180 | if (smb3_encryption_required(tcon)) |
| 5181 | flags |= CIFS_TRANSFORM_REQ; |
| 5182 | |
| 5183 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5184 | rqst.rq_iov = &iov; |
| 5185 | rqst.rq_nvec = 1; |
| 5186 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5187 | rc = cifs_send_recv(xid, ses, server, |
| 5188 | &rqst, &resp_buftype, flags, &rsp_iov); |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5189 | cifs_small_buf_release(iov.iov_base); |
| 5190 | if (rc) { |
| 5191 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
| 5192 | goto posix_qfsinf_exit; |
| 5193 | } |
| 5194 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
| 5195 | |
| 5196 | info = (FILE_SYSTEM_POSIX_INFO *)( |
| 5197 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5198 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 5199 | le32_to_cpu(rsp->OutputBufferLength), &rsp_iov, |
| 5200 | sizeof(FILE_SYSTEM_POSIX_INFO)); |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5201 | if (!rc) |
| 5202 | copy_posix_fs_info_to_kstatfs(info, fsdata); |
| 5203 | |
| 5204 | posix_qfsinf_exit: |
| 5205 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
| 5206 | return rc; |
| 5207 | } |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5208 | |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5209 | int |
| 5210 | SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 5211 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) |
| 5212 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5213 | struct smb_rqst rqst; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5214 | struct smb2_query_info_rsp *rsp = NULL; |
| 5215 | struct kvec iov; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5216 | struct kvec rsp_iov; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5217 | int rc = 0; |
| 5218 | int resp_buftype; |
| 5219 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5220 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5221 | struct smb2_fs_full_size_info *info = NULL; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5222 | int flags = 0; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5223 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5224 | rc = build_qfs_info_req(&iov, tcon, server, |
| 5225 | FS_FULL_SIZE_INFORMATION, |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5226 | sizeof(struct smb2_fs_full_size_info), |
| 5227 | persistent_fid, volatile_fid); |
| 5228 | if (rc) |
| 5229 | return rc; |
| 5230 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5231 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5232 | flags |= CIFS_TRANSFORM_REQ; |
| 5233 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5234 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5235 | rqst.rq_iov = &iov; |
| 5236 | rqst.rq_nvec = 1; |
| 5237 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5238 | rc = cifs_send_recv(xid, ses, server, |
| 5239 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5240 | cifs_small_buf_release(iov.iov_base); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5241 | if (rc) { |
| 5242 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5243 | goto qfsinf_exit; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5244 | } |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5245 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5246 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5247 | info = (struct smb2_fs_full_size_info *)( |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 5248 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5249 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 5250 | le32_to_cpu(rsp->OutputBufferLength), &rsp_iov, |
| 5251 | sizeof(struct smb2_fs_full_size_info)); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5252 | if (!rc) |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5253 | smb2_copy_fs_info_to_kstatfs(info, fsdata); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5254 | |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5255 | qfsinf_exit: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5256 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5257 | return rc; |
| 5258 | } |
| 5259 | |
| 5260 | int |
| 5261 | SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5262 | u64 persistent_fid, u64 volatile_fid, int level) |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5263 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5264 | struct smb_rqst rqst; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5265 | struct smb2_query_info_rsp *rsp = NULL; |
| 5266 | struct kvec iov; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5267 | struct kvec rsp_iov; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5268 | int rc = 0; |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5269 | int resp_buftype, max_len, min_len; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5270 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5271 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5272 | unsigned int rsp_len, offset; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5273 | int flags = 0; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5274 | |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5275 | if (level == FS_DEVICE_INFORMATION) { |
| 5276 | max_len = sizeof(FILE_SYSTEM_DEVICE_INFO); |
| 5277 | min_len = sizeof(FILE_SYSTEM_DEVICE_INFO); |
| 5278 | } else if (level == FS_ATTRIBUTE_INFORMATION) { |
| 5279 | max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO); |
| 5280 | min_len = MIN_FS_ATTR_INFO_SIZE; |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5281 | } else if (level == FS_SECTOR_SIZE_INFORMATION) { |
| 5282 | max_len = sizeof(struct smb3_fs_ss_info); |
| 5283 | min_len = sizeof(struct smb3_fs_ss_info); |
Steve French | 21ba384 | 2018-06-24 23:18:52 -0500 | [diff] [blame] | 5284 | } else if (level == FS_VOLUME_INFORMATION) { |
| 5285 | max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN; |
| 5286 | min_len = sizeof(struct smb3_fs_vol_info); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5287 | } else { |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5288 | cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5289 | return -EINVAL; |
| 5290 | } |
| 5291 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5292 | rc = build_qfs_info_req(&iov, tcon, server, |
| 5293 | level, max_len, |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5294 | persistent_fid, volatile_fid); |
| 5295 | if (rc) |
| 5296 | return rc; |
| 5297 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5298 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5299 | flags |= CIFS_TRANSFORM_REQ; |
| 5300 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5301 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5302 | rqst.rq_iov = &iov; |
| 5303 | rqst.rq_nvec = 1; |
| 5304 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5305 | rc = cifs_send_recv(xid, ses, server, |
| 5306 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5307 | cifs_small_buf_release(iov.iov_base); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5308 | if (rc) { |
| 5309 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
| 5310 | goto qfsattr_exit; |
| 5311 | } |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5312 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5313 | |
| 5314 | rsp_len = le32_to_cpu(rsp->OutputBufferLength); |
| 5315 | offset = le16_to_cpu(rsp->OutputBufferOffset); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5316 | rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5317 | if (rc) |
| 5318 | goto qfsattr_exit; |
| 5319 | |
| 5320 | if (level == FS_ATTRIBUTE_INFORMATION) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5321 | memcpy(&tcon->fsAttrInfo, offset |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 5322 | + (char *)rsp, min_t(unsigned int, |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5323 | rsp_len, max_len)); |
| 5324 | else if (level == FS_DEVICE_INFORMATION) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5325 | memcpy(&tcon->fsDevInfo, offset |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 5326 | + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO)); |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5327 | else if (level == FS_SECTOR_SIZE_INFORMATION) { |
| 5328 | struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5329 | (offset + (char *)rsp); |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5330 | tcon->ss_flags = le32_to_cpu(ss_info->Flags); |
| 5331 | tcon->perf_sector_size = |
| 5332 | le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf); |
Steve French | 21ba384 | 2018-06-24 23:18:52 -0500 | [diff] [blame] | 5333 | } else if (level == FS_VOLUME_INFORMATION) { |
| 5334 | struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *) |
| 5335 | (offset + (char *)rsp); |
| 5336 | tcon->vol_serial_number = vol_info->VolumeSerialNumber; |
| 5337 | tcon->vol_create_time = vol_info->VolumeCreationTime; |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5338 | } |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5339 | |
| 5340 | qfsattr_exit: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5341 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5342 | return rc; |
| 5343 | } |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5344 | |
| 5345 | int |
| 5346 | smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, |
| 5347 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, |
| 5348 | const __u32 num_lock, struct smb2_lock_element *buf) |
| 5349 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5350 | struct smb_rqst rqst; |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5351 | int rc = 0; |
| 5352 | struct smb2_lock_req *req = NULL; |
| 5353 | struct kvec iov[2]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5354 | struct kvec rsp_iov; |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5355 | int resp_buf_type; |
| 5356 | unsigned int count; |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 5357 | int flags = CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5358 | unsigned int total_len; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5359 | struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5360 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5361 | cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5362 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5363 | rc = smb2_plain_req_init(SMB2_LOCK, tcon, server, |
| 5364 | (void **) &req, &total_len); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5365 | if (rc) |
| 5366 | return rc; |
| 5367 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5368 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5369 | flags |= CIFS_TRANSFORM_REQ; |
| 5370 | |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5371 | req->sync_hdr.ProcessId = cpu_to_le32(pid); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5372 | req->LockCount = cpu_to_le16(num_lock); |
| 5373 | |
| 5374 | req->PersistentFileId = persist_fid; |
| 5375 | req->VolatileFileId = volatile_fid; |
| 5376 | |
| 5377 | count = num_lock * sizeof(struct smb2_lock_element); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5378 | |
| 5379 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5380 | iov[0].iov_len = total_len - sizeof(struct smb2_lock_element); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5381 | iov[1].iov_base = (char *)buf; |
| 5382 | iov[1].iov_len = count; |
| 5383 | |
| 5384 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5385 | |
| 5386 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5387 | rqst.rq_iov = iov; |
| 5388 | rqst.rq_nvec = 2; |
| 5389 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5390 | rc = cifs_send_recv(xid, tcon->ses, server, |
| 5391 | &rqst, &resp_buf_type, flags, |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5392 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5393 | cifs_small_buf_release(req); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5394 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5395 | cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5396 | cifs_stats_fail_inc(tcon, SMB2_LOCK_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 5397 | trace_smb3_lock_err(xid, persist_fid, tcon->tid, |
| 5398 | tcon->ses->Suid, rc); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | return rc; |
| 5402 | } |
| 5403 | |
| 5404 | int |
| 5405 | SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon, |
| 5406 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, |
| 5407 | const __u64 length, const __u64 offset, const __u32 lock_flags, |
| 5408 | const bool wait) |
| 5409 | { |
| 5410 | struct smb2_lock_element lock; |
| 5411 | |
| 5412 | lock.Offset = cpu_to_le64(offset); |
| 5413 | lock.Length = cpu_to_le64(length); |
| 5414 | lock.Flags = cpu_to_le32(lock_flags); |
| 5415 | if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK) |
| 5416 | lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY); |
| 5417 | |
| 5418 | return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock); |
| 5419 | } |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5420 | |
| 5421 | int |
| 5422 | SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, |
| 5423 | __u8 *lease_key, const __le32 lease_state) |
| 5424 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5425 | struct smb_rqst rqst; |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5426 | int rc; |
| 5427 | struct smb2_lease_ack *req = NULL; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5428 | struct cifs_ses *ses = tcon->ses; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5429 | int flags = CIFS_OBREAK_OP; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5430 | unsigned int total_len; |
| 5431 | struct kvec iov[1]; |
| 5432 | struct kvec rsp_iov; |
| 5433 | int resp_buf_type; |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5434 | __u64 *please_key_high; |
| 5435 | __u64 *please_key_low; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5436 | struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5437 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5438 | cifs_dbg(FYI, "SMB2_lease_break\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5439 | rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, |
| 5440 | (void **) &req, &total_len); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5441 | if (rc) |
| 5442 | return rc; |
| 5443 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5444 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5445 | flags |= CIFS_TRANSFORM_REQ; |
| 5446 | |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5447 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5448 | req->StructureSize = cpu_to_le16(36); |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5449 | total_len += 12; |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5450 | |
| 5451 | memcpy(req->LeaseKey, lease_key, 16); |
| 5452 | req->LeaseState = lease_state; |
| 5453 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 5454 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5455 | |
| 5456 | iov[0].iov_base = (char *)req; |
| 5457 | iov[0].iov_len = total_len; |
| 5458 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5459 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5460 | rqst.rq_iov = iov; |
| 5461 | rqst.rq_nvec = 1; |
| 5462 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5463 | rc = cifs_send_recv(xid, ses, server, |
| 5464 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5465 | cifs_small_buf_release(req); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5466 | |
Aurelien Aptel | d339adc | 2019-01-31 13:46:07 +0100 | [diff] [blame] | 5467 | please_key_low = (__u64 *)lease_key; |
| 5468 | please_key_high = (__u64 *)(lease_key+8); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5469 | if (rc) { |
| 5470 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5471 | trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid, |
| 5472 | ses->Suid, *please_key_low, *please_key_high, rc); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5473 | cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc); |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5474 | } else |
| 5475 | trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid, |
| 5476 | ses->Suid, *please_key_low, *please_key_high); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5477 | |
| 5478 | return rc; |
| 5479 | } |