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 | { |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 88 | if (!tcon) |
| 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, |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 101 | const struct cifs_tcon *tcon) |
| 102 | { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 103 | shdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 104 | shdr->StructureSize = cpu_to_le16(64); |
| 105 | shdr->Command = smb2_cmd; |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 106 | if (tcon && tcon->ses && tcon->ses->server) { |
| 107 | struct TCP_Server_Info *server = tcon->ses->server; |
| 108 | |
| 109 | spin_lock(&server->req_lock); |
Steve French | 69dc4b1 | 2019-03-05 21:04:56 -0600 | [diff] [blame] | 110 | /* Request up to 10 credits but don't go over the limit. */ |
Steve French | 141891f | 2016-09-23 00:44:16 -0500 | [diff] [blame] | 111 | if (server->credits >= server->max_credits) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 112 | shdr->CreditRequest = cpu_to_le16(0); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 113 | else |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 114 | shdr->CreditRequest = cpu_to_le16( |
Steve French | 141891f | 2016-09-23 00:44:16 -0500 | [diff] [blame] | 115 | min_t(int, server->max_credits - |
Steve French | 69dc4b1 | 2019-03-05 21:04:56 -0600 | [diff] [blame] | 116 | server->credits, 10)); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 117 | spin_unlock(&server->req_lock); |
| 118 | } else { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 119 | shdr->CreditRequest = cpu_to_le16(2); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 120 | } |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 121 | shdr->ProcessId = cpu_to_le32((__u16)current->tgid); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 122 | |
| 123 | if (!tcon) |
| 124 | goto out; |
| 125 | |
Steve French | 2b80d04 | 2013-06-23 18:43:37 -0500 | [diff] [blame] | 126 | /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */ |
| 127 | /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */ |
Steve French | 1dc92c4 | 2015-05-20 09:32:21 -0500 | [diff] [blame] | 128 | if ((tcon->ses) && (tcon->ses->server) && |
Steve French | 84ceeb9 | 2013-06-26 17:52:17 -0500 | [diff] [blame] | 129 | (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU)) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 130 | shdr->CreditCharge = cpu_to_le16(1); |
Steve French | 2b80d04 | 2013-06-23 18:43:37 -0500 | [diff] [blame] | 131 | /* else CreditCharge MBZ */ |
| 132 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 133 | shdr->TreeId = tcon->tid; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 134 | /* Uid is not converted */ |
| 135 | if (tcon->ses) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 136 | shdr->SessionId = tcon->ses->Suid; |
Steve French | f87ab88 | 2013-06-26 19:14:55 -0500 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have |
| 140 | * to pass the path on the Open SMB prefixed by \\server\share. |
| 141 | * Not sure when we would need to do the augmented path (if ever) and |
| 142 | * setting this flag breaks the SMB2 open operation since it is |
| 143 | * illegal to send an empty path name (without \\server\share prefix) |
| 144 | * when the DFS flag is set in the SMB open header. We could |
| 145 | * consider setting the flag on all operations other than open |
| 146 | * but it is safer to net set it for now. |
| 147 | */ |
| 148 | /* if (tcon->share_flags & SHI1005_FLAGS_DFS) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 149 | shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */ |
Steve French | f87ab88 | 2013-06-26 19:14:55 -0500 | [diff] [blame] | 150 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 151 | if (tcon->ses && tcon->ses->server && tcon->ses->server->sign && |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 152 | !smb3_encryption_required(tcon)) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 153 | shdr->Flags |= SMB2_FLAGS_SIGNED; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 154 | out: |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 158 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 159 | static int __smb2_reconnect(const struct nls_table *nlsc, |
| 160 | struct cifs_tcon *tcon) |
| 161 | { |
| 162 | int rc; |
| 163 | struct dfs_cache_tgt_list tl; |
| 164 | struct dfs_cache_tgt_iterator *it = NULL; |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 165 | char *tree; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 166 | const char *tcp_host; |
| 167 | size_t tcp_host_len; |
| 168 | const char *dfs_host; |
| 169 | size_t dfs_host_len; |
| 170 | |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 171 | tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL); |
| 172 | if (!tree) |
| 173 | return -ENOMEM; |
| 174 | |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 175 | if (tcon->ipc) { |
Ronnie Sahlberg | 74ea5f9 | 2019-02-09 09:51:11 +1000 | [diff] [blame] | 176 | scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", |
| 177 | tcon->ses->server->hostname); |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 178 | rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc); |
| 179 | goto out; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 180 | } |
| 181 | |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 182 | if (!tcon->dfs_path) { |
| 183 | rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc); |
| 184 | goto out; |
| 185 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 186 | |
| 187 | rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl); |
| 188 | if (rc) |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 189 | goto out; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 190 | |
| 191 | extract_unc_hostname(tcon->ses->server->hostname, &tcp_host, |
| 192 | &tcp_host_len); |
| 193 | |
| 194 | for (it = dfs_cache_get_tgt_iterator(&tl); it; |
| 195 | it = dfs_cache_get_next_tgt(&tl, it)) { |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 196 | const char *share, *prefix; |
| 197 | size_t share_len, prefix_len; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 198 | |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 199 | rc = dfs_cache_get_tgt_share(it, &share, &share_len, &prefix, |
| 200 | &prefix_len); |
| 201 | if (rc) { |
| 202 | cifs_dbg(VFS, "%s: failed to parse target share %d\n", |
| 203 | __func__, rc); |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | extract_unc_hostname(share, &dfs_host, &dfs_host_len); |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 208 | |
| 209 | if (dfs_host_len != tcp_host_len |
| 210 | || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) { |
| 211 | cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s", |
| 212 | __func__, |
| 213 | (int)dfs_host_len, dfs_host, |
| 214 | (int)tcp_host_len, tcp_host); |
| 215 | continue; |
| 216 | } |
| 217 | |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 218 | scnprintf(tree, MAX_TREE_SIZE, "\\%.*s", (int)share_len, share); |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 219 | |
| 220 | rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc); |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 221 | if (!rc) { |
| 222 | rc = update_super_prepath(tcon, prefix, prefix_len); |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 223 | break; |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 224 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 225 | if (rc == -EREMOTE) |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | if (!rc) { |
| 230 | if (it) |
| 231 | rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, |
| 232 | it); |
| 233 | else |
| 234 | rc = -ENOENT; |
| 235 | } |
| 236 | dfs_cache_free_tgts(&tl); |
Aurelien Aptel | 15bc77f | 2019-01-08 13:41:00 +0100 | [diff] [blame] | 237 | out: |
| 238 | kfree(tree); |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 239 | return rc; |
| 240 | } |
| 241 | #else |
| 242 | static inline int __smb2_reconnect(const struct nls_table *nlsc, |
| 243 | struct cifs_tcon *tcon) |
| 244 | { |
| 245 | return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc); |
| 246 | } |
| 247 | #endif |
| 248 | |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 249 | static int |
| 250 | smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon) |
| 251 | { |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 252 | int rc; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 253 | struct nls_table *nls_codepage; |
| 254 | struct cifs_ses *ses; |
| 255 | struct TCP_Server_Info *server; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 256 | int retries; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so |
| 260 | * check for tcp and smb session status done differently |
| 261 | * for those three - in the calling routine. |
| 262 | */ |
| 263 | if (tcon == NULL) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 264 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 265 | |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 266 | if (smb2_command == SMB2_TREE_CONNECT) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 267 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 268 | |
| 269 | if (tcon->tidStatus == CifsExiting) { |
| 270 | /* |
| 271 | * only tree disconnect, open, and write, |
| 272 | * (and ulogoff which does not have tcon) |
| 273 | * are allowed as we start force umount. |
| 274 | */ |
| 275 | if ((smb2_command != SMB2_WRITE) && |
| 276 | (smb2_command != SMB2_CREATE) && |
| 277 | (smb2_command != SMB2_TREE_DISCONNECT)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 278 | cifs_dbg(FYI, "can not send cmd %d while umounting\n", |
| 279 | smb2_command); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 280 | return -ENODEV; |
| 281 | } |
| 282 | } |
| 283 | if ((!tcon->ses) || (tcon->ses->status == CifsExiting) || |
| 284 | (!tcon->ses->server)) |
| 285 | return -EIO; |
| 286 | |
| 287 | ses = tcon->ses; |
| 288 | server = ses->server; |
| 289 | |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 290 | retries = server->nr_targets; |
| 291 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 292 | /* |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 293 | * Give demultiplex thread up to 10 seconds to each target available for |
| 294 | * reconnect -- should be greater than cifs socket timeout which is 7 |
| 295 | * seconds. |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 296 | */ |
| 297 | while (server->tcpStatus == CifsNeedReconnect) { |
| 298 | /* |
| 299 | * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE |
| 300 | * here since they are implicitly done when session drops. |
| 301 | */ |
| 302 | switch (smb2_command) { |
| 303 | /* |
| 304 | * BB Should we keep oplock break and add flush to exceptions? |
| 305 | */ |
| 306 | case SMB2_TREE_DISCONNECT: |
| 307 | case SMB2_CANCEL: |
| 308 | case SMB2_CLOSE: |
| 309 | case SMB2_OPLOCK_BREAK: |
| 310 | return -EAGAIN; |
| 311 | } |
| 312 | |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 313 | rc = wait_event_interruptible_timeout(server->response_q, |
| 314 | (server->tcpStatus != CifsNeedReconnect), |
| 315 | 10 * HZ); |
| 316 | if (rc < 0) { |
| 317 | cifs_dbg(FYI, "%s: aborting reconnect due to a received" |
| 318 | " signal by the process\n", __func__); |
| 319 | return -ERESTARTSYS; |
| 320 | } |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 321 | |
| 322 | /* are we still trying to reconnect? */ |
| 323 | if (server->tcpStatus != CifsNeedReconnect) |
| 324 | break; |
| 325 | |
Ronnie Sahlberg | c54849d | 2020-01-31 05:52:51 +1000 | [diff] [blame] | 326 | if (retries && --retries) |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 327 | continue; |
| 328 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 329 | /* |
| 330 | * on "soft" mounts we wait once. Hard mounts keep |
| 331 | * retrying until process is killed or server comes |
| 332 | * back on-line |
| 333 | */ |
| 334 | if (!tcon->retry) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 335 | cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n"); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 336 | return -EHOSTDOWN; |
| 337 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 338 | retries = server->nr_targets; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | if (!tcon->ses->need_reconnect && !tcon->need_reconnect) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 342 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 343 | |
| 344 | nls_codepage = load_nls_default(); |
| 345 | |
| 346 | /* |
| 347 | * need to prevent multiple threads trying to simultaneously reconnect |
| 348 | * the same SMB session |
| 349 | */ |
| 350 | mutex_lock(&tcon->ses->session_mutex); |
Samuel Cabrero | 76e7527 | 2017-07-11 12:44:39 +0200 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * Recheck after acquire mutex. If another thread is negotiating |
| 354 | * and the server never sends an answer the socket will be closed |
| 355 | * and tcpStatus set to reconnect. |
| 356 | */ |
| 357 | if (server->tcpStatus == CifsNeedReconnect) { |
| 358 | rc = -EHOSTDOWN; |
| 359 | mutex_unlock(&tcon->ses->session_mutex); |
| 360 | goto out; |
| 361 | } |
| 362 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 363 | rc = cifs_negotiate_protocol(0, tcon->ses); |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 364 | if (!rc && tcon->ses->need_reconnect) { |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 365 | rc = cifs_setup_session(0, tcon->ses, nls_codepage); |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 366 | if ((rc == -EACCES) && !tcon->retry) { |
| 367 | rc = -EHOSTDOWN; |
| 368 | mutex_unlock(&tcon->ses->session_mutex); |
| 369 | goto failed; |
| 370 | } |
| 371 | } |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 372 | if (rc || !tcon->need_reconnect) { |
| 373 | mutex_unlock(&tcon->ses->session_mutex); |
| 374 | goto out; |
| 375 | } |
| 376 | |
| 377 | cifs_mark_open_files_invalid(tcon); |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 378 | if (tcon->use_persistent) |
| 379 | tcon->need_reopen_files = true; |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 380 | |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 381 | rc = __smb2_reconnect(nls_codepage, tcon); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 382 | mutex_unlock(&tcon->ses->session_mutex); |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 383 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 384 | cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc); |
Steve French | c318e6c | 2018-04-04 14:08:52 -0500 | [diff] [blame] | 385 | if (rc) { |
| 386 | /* If sess reconnected but tcon didn't, something strange ... */ |
| 387 | printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 388 | goto out; |
Steve French | c318e6c | 2018-04-04 14:08:52 -0500 | [diff] [blame] | 389 | } |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 390 | |
| 391 | if (smb2_command != SMB2_INTERNAL_CMD) |
Stefan Metzmacher | b08484d | 2020-02-24 14:14:59 +0100 | [diff] [blame^] | 392 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 393 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 394 | atomic_inc(&tconInfoReconnectCount); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 395 | out: |
| 396 | /* |
| 397 | * Check if handle based operation so we know whether we can continue |
| 398 | * or not without returning to caller to reset file handle. |
| 399 | */ |
| 400 | /* |
| 401 | * BB Is flush done by server on drop of tcp session? Should we special |
| 402 | * case it and skip above? |
| 403 | */ |
| 404 | switch (smb2_command) { |
| 405 | case SMB2_FLUSH: |
| 406 | case SMB2_READ: |
| 407 | case SMB2_WRITE: |
| 408 | case SMB2_LOCK: |
| 409 | case SMB2_IOCTL: |
| 410 | case SMB2_QUERY_DIRECTORY: |
| 411 | case SMB2_CHANGE_NOTIFY: |
| 412 | case SMB2_QUERY_INFO: |
| 413 | case SMB2_SET_INFO: |
Pavel Shilovsky | 4772c79 | 2016-11-29 11:30:58 -0800 | [diff] [blame] | 414 | rc = -EAGAIN; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 415 | } |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 416 | failed: |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 417 | unload_nls(nls_codepage); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 418 | return rc; |
| 419 | } |
| 420 | |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 421 | static void |
| 422 | fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf, |
| 423 | unsigned int *total_len) |
| 424 | { |
| 425 | struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf; |
| 426 | /* lookup word count ie StructureSize from table */ |
| 427 | __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)]; |
| 428 | |
| 429 | /* |
| 430 | * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of |
| 431 | * largest operations (Create) |
| 432 | */ |
| 433 | memset(buf, 0, 256); |
| 434 | |
| 435 | smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon); |
| 436 | spdu->StructureSize2 = cpu_to_le16(parmsize); |
| 437 | |
| 438 | *total_len = parmsize + sizeof(struct smb2_sync_hdr); |
| 439 | } |
| 440 | |
Ronnie Sahlberg | 305428a | 2017-11-21 11:04:42 +1100 | [diff] [blame] | 441 | /* |
| 442 | * Allocate and return pointer to an SMB request hdr, and set basic |
| 443 | * SMB information in the SMB header. If the return code is zero, this |
| 444 | * function must have filled in request_buf pointer. |
| 445 | */ |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 446 | static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, |
| 447 | void **request_buf, unsigned int *total_len) |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 448 | { |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 449 | /* BB eventually switch this to SMB2 specific small buf size */ |
Stefano Brivio | f46ecbd | 2018-07-05 11:46:42 +0200 | [diff] [blame] | 450 | if (smb2_command == SMB2_SET_INFO) |
| 451 | *request_buf = cifs_buf_get(); |
| 452 | else |
| 453 | *request_buf = cifs_small_buf_get(); |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 454 | if (*request_buf == NULL) { |
| 455 | /* BB should we add a retry in here if not a writepage? */ |
| 456 | return -ENOMEM; |
| 457 | } |
| 458 | |
Ronnie Sahlberg | 305428a | 2017-11-21 11:04:42 +1100 | [diff] [blame] | 459 | fill_small_buf(smb2_command, tcon, |
| 460 | (struct smb2_sync_hdr *)(*request_buf), |
| 461 | total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 462 | |
| 463 | if (tcon != NULL) { |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 464 | uint16_t com_code = le16_to_cpu(smb2_command); |
| 465 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 466 | cifs_stats_inc(&tcon->num_smbs_sent); |
| 467 | } |
| 468 | |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, |
| 473 | void **request_buf, unsigned int *total_len) |
| 474 | { |
| 475 | int rc; |
| 476 | |
| 477 | rc = smb2_reconnect(smb2_command, tcon); |
| 478 | if (rc) |
| 479 | return rc; |
| 480 | |
| 481 | return __smb2_plain_req_init(smb2_command, tcon, request_buf, |
| 482 | total_len); |
| 483 | } |
| 484 | |
| 485 | static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon, |
| 486 | void **request_buf, unsigned int *total_len) |
| 487 | { |
| 488 | /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */ |
| 489 | if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) { |
| 490 | return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, |
| 491 | total_len); |
| 492 | } |
| 493 | return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 494 | } |
| 495 | |
Steve French | d7bef4c | 2019-04-18 11:03:58 -0500 | [diff] [blame] | 496 | /* 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] | 497 | |
| 498 | static void |
| 499 | build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt) |
| 500 | { |
| 501 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 502 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 503 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 504 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 505 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 506 | pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512; |
| 507 | } |
| 508 | |
| 509 | static void |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 510 | build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt) |
| 511 | { |
| 512 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 513 | pneg_ctxt->DataLength = |
| 514 | cpu_to_le16(sizeof(struct smb2_compression_capabilities_context) |
| 515 | - sizeof(struct smb2_neg_context)); |
| 516 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3); |
| 517 | pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77; |
| 518 | pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF; |
| 519 | pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1; |
| 520 | } |
| 521 | |
| 522 | static void |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 523 | build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt) |
| 524 | { |
| 525 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
Steve French | 9ac63ec | 2019-06-07 08:59:40 -0500 | [diff] [blame] | 526 | pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */ |
| 527 | pneg_ctxt->CipherCount = cpu_to_le16(2); |
| 528 | pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM; |
| 529 | pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM; |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 530 | } |
| 531 | |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 532 | static unsigned int |
| 533 | build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname) |
| 534 | { |
| 535 | struct nls_table *cp = load_nls_default(); |
| 536 | |
| 537 | pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID; |
| 538 | |
| 539 | /* 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] | 540 | 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] | 541 | /* context size is DataLength + minimal smb2_neg_context */ |
| 542 | return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) + |
| 543 | sizeof(struct smb2_neg_context), 8) * 8; |
| 544 | } |
| 545 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 546 | static void |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 547 | build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
| 548 | { |
| 549 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 550 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
Steve French | 0d48132 | 2019-02-24 17:56:33 -0600 | [diff] [blame] | 551 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 552 | pneg_ctxt->Name[0] = 0x93; |
| 553 | pneg_ctxt->Name[1] = 0xAD; |
| 554 | pneg_ctxt->Name[2] = 0x25; |
| 555 | pneg_ctxt->Name[3] = 0x50; |
| 556 | pneg_ctxt->Name[4] = 0x9C; |
| 557 | pneg_ctxt->Name[5] = 0xB4; |
| 558 | pneg_ctxt->Name[6] = 0x11; |
| 559 | pneg_ctxt->Name[7] = 0xE7; |
| 560 | pneg_ctxt->Name[8] = 0xB4; |
| 561 | pneg_ctxt->Name[9] = 0x23; |
| 562 | pneg_ctxt->Name[10] = 0x83; |
| 563 | pneg_ctxt->Name[11] = 0xDE; |
| 564 | pneg_ctxt->Name[12] = 0x96; |
| 565 | pneg_ctxt->Name[13] = 0x8B; |
| 566 | pneg_ctxt->Name[14] = 0xCD; |
| 567 | pneg_ctxt->Name[15] = 0x7C; |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static void |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 571 | assemble_neg_contexts(struct smb2_negotiate_req *req, |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 572 | struct TCP_Server_Info *server, unsigned int *total_len) |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 573 | { |
Colin Ian King | a9f76cf | 2019-12-02 18:59:42 +0000 | [diff] [blame] | 574 | char *pneg_ctxt; |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 575 | unsigned int ctxt_len; |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 576 | |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 577 | if (*total_len > 200) { |
| 578 | /* In case length corrupted don't want to overrun smb buffer */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 579 | cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n"); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 580 | return; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * round up total_len of fixed part of SMB3 negotiate request to 8 |
| 585 | * byte boundary before adding negotiate contexts |
| 586 | */ |
| 587 | *total_len = roundup(*total_len, 8); |
| 588 | |
| 589 | pneg_ctxt = (*total_len) + (char *)req; |
| 590 | req->NegotiateContextOffset = cpu_to_le32(*total_len); |
| 591 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 592 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 593 | ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8; |
| 594 | *total_len += ctxt_len; |
| 595 | pneg_ctxt += ctxt_len; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 596 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 597 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 598 | ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8; |
| 599 | *total_len += ctxt_len; |
| 600 | pneg_ctxt += ctxt_len; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 601 | |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 602 | if (server->compress_algorithm) { |
| 603 | build_compression_ctxt((struct smb2_compression_capabilities_context *) |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 604 | pneg_ctxt); |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 605 | ctxt_len = DIV_ROUND_UP( |
| 606 | sizeof(struct smb2_compression_capabilities_context), |
| 607 | 8) * 8; |
| 608 | *total_len += ctxt_len; |
| 609 | pneg_ctxt += ctxt_len; |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 610 | req->NegotiateContextCount = cpu_to_le16(5); |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 611 | } else |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 612 | req->NegotiateContextCount = cpu_to_le16(4); |
| 613 | |
| 614 | ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt, |
| 615 | server->hostname); |
| 616 | *total_len += ctxt_len; |
| 617 | pneg_ctxt += ctxt_len; |
| 618 | |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 619 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 620 | *total_len += sizeof(struct smb2_posix_neg_context); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 621 | } |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 622 | |
| 623 | static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt) |
| 624 | { |
| 625 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 626 | |
| 627 | /* If invalid preauth context warn but use what we requested, SHA-512 */ |
| 628 | if (len < MIN_PREAUTH_CTXT_DATA_LEN) { |
| 629 | printk_once(KERN_WARNING "server sent bad preauth context\n"); |
| 630 | return; |
| 631 | } |
| 632 | if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1) |
| 633 | printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n"); |
| 634 | if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512) |
| 635 | printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n"); |
| 636 | } |
| 637 | |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 638 | static void decode_compress_ctx(struct TCP_Server_Info *server, |
| 639 | struct smb2_compression_capabilities_context *ctxt) |
| 640 | { |
| 641 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 642 | |
| 643 | /* sizeof compress context is a one element compression capbility struct */ |
| 644 | if (len < 10) { |
| 645 | printk_once(KERN_WARNING "server sent bad compression cntxt\n"); |
| 646 | return; |
| 647 | } |
| 648 | if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) { |
| 649 | printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n"); |
| 650 | return; |
| 651 | } |
| 652 | if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) { |
| 653 | printk_once(KERN_WARNING "unknown compression algorithm\n"); |
| 654 | return; |
| 655 | } |
| 656 | server->compress_algorithm = ctxt->CompressionAlgorithms[0]; |
| 657 | } |
| 658 | |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 659 | static int decode_encrypt_ctx(struct TCP_Server_Info *server, |
| 660 | struct smb2_encryption_neg_context *ctxt) |
| 661 | { |
| 662 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 663 | |
| 664 | cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len); |
| 665 | if (len < MIN_ENCRYPT_CTXT_DATA_LEN) { |
| 666 | printk_once(KERN_WARNING "server sent bad crypto ctxt len\n"); |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | |
| 670 | if (le16_to_cpu(ctxt->CipherCount) != 1) { |
| 671 | printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n"); |
| 672 | return -EINVAL; |
| 673 | } |
| 674 | cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0])); |
| 675 | if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) && |
| 676 | (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) { |
| 677 | printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n"); |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | server->cipher_type = ctxt->Ciphers[0]; |
Steve French | 23657ad | 2018-04-22 15:14:58 -0500 | [diff] [blame] | 681 | server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION; |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp, |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 686 | struct TCP_Server_Info *server, |
| 687 | unsigned int len_of_smb) |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 688 | { |
| 689 | struct smb2_neg_context *pctx; |
| 690 | unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset); |
| 691 | unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 692 | unsigned int len_of_ctxts, i; |
| 693 | int rc = 0; |
| 694 | |
| 695 | cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt); |
| 696 | if (len_of_smb <= offset) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 697 | cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 698 | return -EINVAL; |
| 699 | } |
| 700 | |
| 701 | len_of_ctxts = len_of_smb - offset; |
| 702 | |
| 703 | for (i = 0; i < ctxt_cnt; i++) { |
| 704 | int clen; |
| 705 | /* check that offset is not beyond end of SMB */ |
| 706 | if (len_of_ctxts == 0) |
| 707 | break; |
| 708 | |
| 709 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 710 | break; |
| 711 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 712 | pctx = (struct smb2_neg_context *)(offset + (char *)rsp); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 713 | clen = le16_to_cpu(pctx->DataLength); |
| 714 | if (clen > len_of_ctxts) |
| 715 | break; |
| 716 | |
| 717 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) |
| 718 | decode_preauth_context( |
| 719 | (struct smb2_preauth_neg_context *)pctx); |
| 720 | else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) |
| 721 | rc = decode_encrypt_ctx(server, |
| 722 | (struct smb2_encryption_neg_context *)pctx); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 723 | else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) |
| 724 | decode_compress_ctx(server, |
| 725 | (struct smb2_compression_capabilities_context *)pctx); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 726 | else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) |
| 727 | server->posix_ext_supported = true; |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 728 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 729 | cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n", |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 730 | le16_to_cpu(pctx->ContextType)); |
| 731 | |
| 732 | if (rc) |
| 733 | break; |
| 734 | /* offsets must be 8 byte aligned */ |
| 735 | clen = (clen + 7) & ~0x7; |
| 736 | offset += clen + sizeof(struct smb2_neg_context); |
| 737 | len_of_ctxts -= clen; |
| 738 | } |
| 739 | return rc; |
| 740 | } |
| 741 | |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 742 | static struct create_posix * |
| 743 | create_posix_buf(umode_t mode) |
| 744 | { |
| 745 | struct create_posix *buf; |
| 746 | |
| 747 | buf = kzalloc(sizeof(struct create_posix), |
| 748 | GFP_KERNEL); |
| 749 | if (!buf) |
| 750 | return NULL; |
| 751 | |
| 752 | buf->ccontext.DataOffset = |
| 753 | cpu_to_le16(offsetof(struct create_posix, Mode)); |
| 754 | buf->ccontext.DataLength = cpu_to_le32(4); |
| 755 | buf->ccontext.NameOffset = |
| 756 | cpu_to_le16(offsetof(struct create_posix, Name)); |
| 757 | buf->ccontext.NameLength = cpu_to_le16(16); |
| 758 | |
| 759 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 760 | buf->Name[0] = 0x93; |
| 761 | buf->Name[1] = 0xAD; |
| 762 | buf->Name[2] = 0x25; |
| 763 | buf->Name[3] = 0x50; |
| 764 | buf->Name[4] = 0x9C; |
| 765 | buf->Name[5] = 0xB4; |
| 766 | buf->Name[6] = 0x11; |
| 767 | buf->Name[7] = 0xE7; |
| 768 | buf->Name[8] = 0xB4; |
| 769 | buf->Name[9] = 0x23; |
| 770 | buf->Name[10] = 0x83; |
| 771 | buf->Name[11] = 0xDE; |
| 772 | buf->Name[12] = 0x96; |
| 773 | buf->Name[13] = 0x8B; |
| 774 | buf->Name[14] = 0xCD; |
| 775 | buf->Name[15] = 0x7C; |
| 776 | buf->Mode = cpu_to_le32(mode); |
| 777 | cifs_dbg(FYI, "mode on posix create 0%o", mode); |
| 778 | return buf; |
| 779 | } |
| 780 | |
| 781 | static int |
| 782 | add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode) |
| 783 | { |
| 784 | struct smb2_create_req *req = iov[0].iov_base; |
| 785 | unsigned int num = *num_iovec; |
| 786 | |
| 787 | iov[num].iov_base = create_posix_buf(mode); |
Steve French | d0959b0 | 2019-10-05 10:53:58 -0500 | [diff] [blame] | 788 | if (mode == ACL_NO_MODE) |
| 789 | cifs_dbg(FYI, "illegal mode\n"); |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 790 | if (iov[num].iov_base == NULL) |
| 791 | return -ENOMEM; |
| 792 | iov[num].iov_len = sizeof(struct create_posix); |
| 793 | if (!req->CreateContextsOffset) |
| 794 | req->CreateContextsOffset = cpu_to_le32( |
| 795 | sizeof(struct smb2_create_req) + |
| 796 | iov[num - 1].iov_len); |
| 797 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix)); |
| 798 | *num_iovec = num + 1; |
| 799 | return 0; |
| 800 | } |
| 801 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 802 | |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 803 | /* |
| 804 | * |
| 805 | * SMB2 Worker functions follow: |
| 806 | * |
| 807 | * The general structure of the worker functions is: |
| 808 | * 1) Call smb2_init (assembles SMB2 header) |
| 809 | * 2) Initialize SMB2 command specific fields in fixed length area of SMB |
| 810 | * 3) Call smb_sendrcv2 (sends request on socket and waits for response) |
| 811 | * 4) Decode SMB2 command specific fields in the fixed length area |
| 812 | * 5) Decode variable length data area (if any for this SMB2 command type) |
| 813 | * 6) Call free smb buffer |
| 814 | * 7) return |
| 815 | * |
| 816 | */ |
| 817 | |
| 818 | int |
| 819 | SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses) |
| 820 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 821 | struct smb_rqst rqst; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 822 | struct smb2_negotiate_req *req; |
| 823 | struct smb2_negotiate_rsp *rsp; |
| 824 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 825 | struct kvec rsp_iov; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 826 | int rc = 0; |
| 827 | int resp_buftype; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 828 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 829 | int blob_offset, blob_length; |
| 830 | char *security_blob; |
| 831 | int flags = CIFS_NEG_OP; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 832 | unsigned int total_len; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 833 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 834 | cifs_dbg(FYI, "Negotiate protocol\n"); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 835 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 836 | if (!server) { |
| 837 | WARN(1, "%s: server is NULL!\n", __func__); |
| 838 | return -EIO; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 839 | } |
| 840 | |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 841 | rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 842 | if (rc) |
| 843 | return rc; |
| 844 | |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 845 | req->sync_hdr.SessionId = 0; |
Steve French | 0fdfef9 | 2018-06-28 19:30:23 -0500 | [diff] [blame] | 846 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 847 | memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); |
| 848 | memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 849 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 850 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 851 | SMB3ANY_VERSION_STRING) == 0) { |
| 852 | req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); |
| 853 | req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); |
| 854 | req->DialectCount = cpu_to_le16(2); |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 855 | total_len += 4; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 856 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 857 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 858 | req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); |
| 859 | req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); |
| 860 | req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 861 | req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); |
| 862 | req->DialectCount = cpu_to_le16(4); |
| 863 | total_len += 8; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 864 | } else { |
| 865 | /* otherwise send specific dialect */ |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 866 | req->Dialects[0] = cpu_to_le16(server->vals->protocol_id); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 867 | req->DialectCount = cpu_to_le16(1); |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 868 | total_len += 2; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 869 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 870 | |
| 871 | /* only one of SMB2 signing flags may be set in SMB2 request */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 872 | if (ses->sign) |
Steve French | 9cd2e62 | 2013-06-12 19:59:03 -0500 | [diff] [blame] | 873 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 874 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
Steve French | 9cd2e62 | 2013-06-12 19:59:03 -0500 | [diff] [blame] | 875 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 876 | else |
| 877 | req->SecurityMode = 0; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 878 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 879 | req->Capabilities = cpu_to_le32(server->vals->req_capabilities); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 880 | |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 881 | /* ClientGUID must be zero for SMB2.02 dialect */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 882 | if (server->vals->protocol_id == SMB20_PROT_ID) |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 883 | memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 884 | else { |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 885 | memcpy(req->ClientGUID, server->client_guid, |
| 886 | SMB2_CLIENT_GUID_SIZE); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 887 | if ((server->vals->protocol_id == SMB311_PROT_ID) || |
| 888 | (strcmp(server->vals->version_string, |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 889 | SMBDEFAULT_VERSION_STRING) == 0)) |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 890 | assemble_neg_contexts(req, server, &total_len); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 891 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 892 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 893 | iov[0].iov_len = total_len; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 894 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 895 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 896 | rqst.rq_iov = iov; |
| 897 | rqst.rq_nvec = 1; |
| 898 | |
| 899 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 900 | cifs_small_buf_release(req); |
| 901 | rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 902 | /* |
| 903 | * No tcon so can't do |
| 904 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); |
| 905 | */ |
Steve French | 7e682f7 | 2017-08-31 21:34:24 -0500 | [diff] [blame] | 906 | if (rc == -EOPNOTSUPP) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 907 | cifs_server_dbg(VFS, "Dialect not supported by server. Consider " |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 908 | "specifying vers=1.0 or vers=2.0 on mount for accessing" |
Steve French | 7e682f7 | 2017-08-31 21:34:24 -0500 | [diff] [blame] | 909 | " older servers\n"); |
| 910 | goto neg_exit; |
| 911 | } else if (rc != 0) |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 912 | goto neg_exit; |
| 913 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 914 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 915 | SMB3ANY_VERSION_STRING) == 0) { |
| 916 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 917 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 918 | "SMB2 dialect returned but not requested\n"); |
| 919 | return -EIO; |
| 920 | } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 921 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 922 | "SMB2.1 dialect returned but not requested\n"); |
| 923 | return -EIO; |
| 924 | } |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 925 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 926 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 927 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 928 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 929 | "SMB2 dialect returned but not requested\n"); |
| 930 | return -EIO; |
| 931 | } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) { |
| 932 | /* ops set to 3.0 by default for default so update */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 933 | server->ops = &smb21_operations; |
| 934 | server->vals = &smb21_values; |
ZhangXiaoxu | b57a55e | 2019-04-06 15:30:38 +0800 | [diff] [blame] | 935 | } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 936 | server->ops = &smb311_operations; |
| 937 | server->vals = &smb311_values; |
ZhangXiaoxu | b57a55e | 2019-04-06 15:30:38 +0800 | [diff] [blame] | 938 | } |
Steve French | 590d08d | 2017-09-19 11:43:47 -0500 | [diff] [blame] | 939 | } else if (le16_to_cpu(rsp->DialectRevision) != |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 940 | server->vals->protocol_id) { |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 941 | /* if requested single dialect ensure returned dialect matched */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 942 | cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n", |
Steve French | 590d08d | 2017-09-19 11:43:47 -0500 | [diff] [blame] | 943 | le16_to_cpu(rsp->DialectRevision)); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 944 | return -EIO; |
| 945 | } |
| 946 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 947 | cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 948 | |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 949 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 950 | cifs_dbg(FYI, "negotiated smb2.0 dialect\n"); |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 951 | else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 952 | cifs_dbg(FYI, "negotiated smb2.1 dialect\n"); |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 953 | else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 954 | cifs_dbg(FYI, "negotiated smb3.0 dialect\n"); |
Steve French | 20b6d8b | 2013-06-12 22:48:41 -0500 | [diff] [blame] | 955 | else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID)) |
| 956 | cifs_dbg(FYI, "negotiated smb3.02 dialect\n"); |
Steve French | 5f7fbf7 | 2014-12-17 22:52:58 -0600 | [diff] [blame] | 957 | else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) |
| 958 | cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n"); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 959 | else { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 960 | cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 961 | le16_to_cpu(rsp->DialectRevision)); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 962 | rc = -EIO; |
| 963 | goto neg_exit; |
| 964 | } |
| 965 | server->dialect = le16_to_cpu(rsp->DialectRevision); |
| 966 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 967 | /* |
| 968 | * Keep a copy of the hash after negprot. This hash will be |
| 969 | * the starting hash value for all sessions made from this |
| 970 | * server. |
| 971 | */ |
| 972 | memcpy(server->preauth_sha_hash, ses->preauth_sha_hash, |
| 973 | SMB2_PREAUTH_HASH_SIZE); |
Steve French | 0fdfef9 | 2018-06-28 19:30:23 -0500 | [diff] [blame] | 974 | |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 975 | /* SMB2 only has an extended negflavor */ |
| 976 | server->negflavor = CIFS_NEGFLAVOR_EXTENDED; |
Pavel Shilovsky | 2365c4e | 2014-02-14 13:31:02 +0400 | [diff] [blame] | 977 | /* set it to the maximum buffer size value we can send with 1 credit */ |
| 978 | server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize), |
| 979 | SMB2_MAX_BUFFER_SIZE); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 980 | server->max_read = le32_to_cpu(rsp->MaxReadSize); |
| 981 | server->max_write = le32_to_cpu(rsp->MaxWriteSize); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 982 | server->sec_mode = le16_to_cpu(rsp->SecurityMode); |
Steve French | 07108d0 | 2018-04-01 20:15:55 -0500 | [diff] [blame] | 983 | if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode) |
| 984 | cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n", |
| 985 | server->sec_mode); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 986 | server->capabilities = le32_to_cpu(rsp->Capabilities); |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 987 | /* Internal types */ |
| 988 | server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 989 | |
| 990 | security_blob = smb2_get_data_area_len(&blob_offset, &blob_length, |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 991 | (struct smb2_sync_hdr *)rsp); |
Steve French | 5d875cc | 2013-06-25 15:33:41 -0500 | [diff] [blame] | 992 | /* |
| 993 | * See MS-SMB2 section 2.2.4: if no blob, client picks default which |
| 994 | * for us will be |
| 995 | * ses->sectype = RawNTLMSSP; |
| 996 | * but for time being this is our only auth choice so doesn't matter. |
| 997 | * We just found a server which sets blob length to zero expecting raw. |
| 998 | */ |
Pavel Shilovsky | 67dbea2 | 2017-04-12 13:32:07 -0700 | [diff] [blame] | 999 | if (blob_length == 0) { |
Steve French | 5d875cc | 2013-06-25 15:33:41 -0500 | [diff] [blame] | 1000 | cifs_dbg(FYI, "missing security blob on negprot\n"); |
Pavel Shilovsky | 67dbea2 | 2017-04-12 13:32:07 -0700 | [diff] [blame] | 1001 | server->sec_ntlmssp = true; |
| 1002 | } |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1003 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 1004 | rc = cifs_enable_signing(server, ses->sign); |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 1005 | if (rc) |
| 1006 | goto neg_exit; |
Steve French | ceb1b0b | 2015-09-24 00:52:37 -0500 | [diff] [blame] | 1007 | if (blob_length) { |
Steve French | ebdd207 | 2014-10-20 12:48:23 -0500 | [diff] [blame] | 1008 | rc = decode_negTokenInit(security_blob, blob_length, server); |
Steve French | ceb1b0b | 2015-09-24 00:52:37 -0500 | [diff] [blame] | 1009 | if (rc == 1) |
| 1010 | rc = 0; |
| 1011 | else if (rc == 0) |
| 1012 | rc = -EIO; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1013 | } |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1014 | |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1015 | if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) { |
| 1016 | if (rsp->NegotiateContextCount) |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 1017 | rc = smb311_decode_neg_context(rsp, server, |
| 1018 | rsp_iov.iov_len); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1019 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1020 | cifs_server_dbg(VFS, "Missing expected negotiate contexts\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 1021 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 1022 | neg_exit: |
| 1023 | free_rsp_buf(resp_buftype, rsp); |
| 1024 | return rc; |
| 1025 | } |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1026 | |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1027 | int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon) |
| 1028 | { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1029 | int rc; |
| 1030 | struct validate_negotiate_info_req *pneg_inbuf; |
David Disseldorp | fe83bebc | 2017-10-20 14:49:37 +0200 | [diff] [blame] | 1031 | struct validate_negotiate_info_rsp *pneg_rsp = NULL; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1032 | u32 rsplen; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1033 | u32 inbuflen; /* max of 4 dialects */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1034 | struct TCP_Server_Info *server = tcon->ses->server; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1035 | |
| 1036 | cifs_dbg(FYI, "validate negotiate\n"); |
| 1037 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1038 | /* In SMB3.11 preauth integrity supersedes validate negotiate */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1039 | if (server->dialect == SMB311_PROT_ID) |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1040 | return 0; |
| 1041 | |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1042 | /* |
| 1043 | * validation ioctl must be signed, so no point sending this if we |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1044 | * can not sign it (ie are not known user). Even if signing is not |
| 1045 | * required (enabled but not negotiated), in those cases we selectively |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1046 | * sign just this, the first and only signed request on a connection. |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1047 | * Having validation of negotiate info helps reduce attack vectors. |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1048 | */ |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1049 | if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1050 | return 0; /* validation requires signing */ |
| 1051 | |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1052 | if (tcon->ses->user_name == NULL) { |
| 1053 | cifs_dbg(FYI, "Can't validate negotiate: null user mount\n"); |
| 1054 | return 0; /* validation requires signing */ |
| 1055 | } |
| 1056 | |
| 1057 | if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1058 | 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] | 1059 | |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1060 | pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS); |
| 1061 | if (!pneg_inbuf) |
| 1062 | return -ENOMEM; |
| 1063 | |
| 1064 | pneg_inbuf->Capabilities = |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1065 | cpu_to_le32(server->vals->req_capabilities); |
| 1066 | memcpy(pneg_inbuf->Guid, server->client_guid, |
Sachin Prabhu | 39552ea | 2014-05-13 00:48:12 +0100 | [diff] [blame] | 1067 | SMB2_CLIENT_GUID_SIZE); |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1068 | |
| 1069 | if (tcon->ses->sign) |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1070 | pneg_inbuf->SecurityMode = |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1071 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
| 1072 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1073 | pneg_inbuf->SecurityMode = |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1074 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
| 1075 | else |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1076 | pneg_inbuf->SecurityMode = 0; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1077 | |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1078 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1079 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1080 | SMB3ANY_VERSION_STRING) == 0) { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1081 | pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); |
| 1082 | pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); |
| 1083 | pneg_inbuf->DialectCount = cpu_to_le16(2); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1084 | /* structure is big enough for 3 dialects, sending only 2 */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1085 | inbuflen = sizeof(*pneg_inbuf) - |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 1086 | (2 * sizeof(pneg_inbuf->Dialects[0])); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1087 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1088 | SMBDEFAULT_VERSION_STRING) == 0) { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1089 | pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); |
| 1090 | pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); |
| 1091 | pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 1092 | pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); |
| 1093 | pneg_inbuf->DialectCount = cpu_to_le16(4); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1094 | /* structure is big enough for 3 dialects */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1095 | inbuflen = sizeof(*pneg_inbuf); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1096 | } else { |
| 1097 | /* otherwise specific dialect was requested */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1098 | pneg_inbuf->Dialects[0] = |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1099 | cpu_to_le16(server->vals->protocol_id); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1100 | pneg_inbuf->DialectCount = cpu_to_le16(1); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1101 | /* structure is big enough for 3 dialects, sending only 1 */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1102 | inbuflen = sizeof(*pneg_inbuf) - |
| 1103 | sizeof(pneg_inbuf->Dialects[0]) * 2; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1104 | } |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1105 | |
| 1106 | rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID, |
| 1107 | FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 1108 | (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize, |
| 1109 | (char **)&pneg_rsp, &rsplen); |
Namjae Jeon | 969ae8e | 2019-01-22 09:46:45 +0900 | [diff] [blame] | 1110 | if (rc == -EOPNOTSUPP) { |
| 1111 | /* |
| 1112 | * Old Windows versions or Netapp SMB server can return |
| 1113 | * not supported error. Client should accept it. |
| 1114 | */ |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1115 | cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n"); |
Colin Ian King | 2107820 | 2019-05-17 09:12:33 +0100 | [diff] [blame] | 1116 | rc = 0; |
| 1117 | goto out_free_inbuf; |
Namjae Jeon | 969ae8e | 2019-01-22 09:46:45 +0900 | [diff] [blame] | 1118 | } else if (rc != 0) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1119 | cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1120 | rc = -EIO; |
| 1121 | goto out_free_inbuf; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1122 | } |
| 1123 | |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1124 | rc = -EIO; |
| 1125 | if (rsplen != sizeof(*pneg_rsp)) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1126 | cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n", |
Steve French | 7db0a6e | 2017-05-03 21:12:20 -0500 | [diff] [blame] | 1127 | rsplen); |
| 1128 | |
| 1129 | /* relax check since Mac returns max bufsize allowed on ioctl */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1130 | if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp)) |
| 1131 | goto out_free_rsp; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | /* check validate negotiate info response matches what we got earlier */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1135 | if (pneg_rsp->Dialect != cpu_to_le16(server->dialect)) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1136 | goto vneg_out; |
| 1137 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1138 | if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode)) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1139 | goto vneg_out; |
| 1140 | |
| 1141 | /* do not validate server guid because not saved at negprot time yet */ |
| 1142 | |
| 1143 | if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1144 | SMB2_LARGE_FILES) != server->capabilities) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1145 | goto vneg_out; |
| 1146 | |
| 1147 | /* validate negotiate successful */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1148 | rc = 0; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1149 | cifs_dbg(FYI, "validate negotiate info successful\n"); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1150 | goto out_free_rsp; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1151 | |
| 1152 | vneg_out: |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1153 | cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n"); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1154 | out_free_rsp: |
David Disseldorp | fe83bebc | 2017-10-20 14:49:37 +0200 | [diff] [blame] | 1155 | kfree(pneg_rsp); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1156 | out_free_inbuf: |
| 1157 | kfree(pneg_inbuf); |
| 1158 | return rc; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1159 | } |
| 1160 | |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1161 | enum securityEnum |
| 1162 | smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested) |
| 1163 | { |
| 1164 | switch (requested) { |
| 1165 | case Kerberos: |
| 1166 | case RawNTLMSSP: |
| 1167 | return requested; |
| 1168 | case NTLMv2: |
| 1169 | return RawNTLMSSP; |
| 1170 | case Unspecified: |
| 1171 | if (server->sec_ntlmssp && |
| 1172 | (global_secflags & CIFSSEC_MAY_NTLMSSP)) |
| 1173 | return RawNTLMSSP; |
| 1174 | if ((server->sec_kerberos || server->sec_mskerberos) && |
| 1175 | (global_secflags & CIFSSEC_MAY_KRB5)) |
| 1176 | return Kerberos; |
| 1177 | /* Fallthrough */ |
| 1178 | default: |
| 1179 | return Unspecified; |
| 1180 | } |
| 1181 | } |
| 1182 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1183 | struct SMB2_sess_data { |
| 1184 | unsigned int xid; |
| 1185 | struct cifs_ses *ses; |
| 1186 | struct nls_table *nls_cp; |
| 1187 | void (*func)(struct SMB2_sess_data *); |
| 1188 | int result; |
| 1189 | u64 previous_session; |
| 1190 | |
| 1191 | /* we will send the SMB in three pieces: |
| 1192 | * a fixed length beginning part, an optional |
| 1193 | * SPNEGO blob (which can be zero length), and a |
| 1194 | * last part which will include the strings |
| 1195 | * and rest of bcc area. This allows us to avoid |
| 1196 | * a large buffer 17K allocation |
| 1197 | */ |
| 1198 | int buf0_type; |
| 1199 | struct kvec iov[2]; |
| 1200 | }; |
| 1201 | |
| 1202 | static int |
| 1203 | SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data) |
| 1204 | { |
| 1205 | int rc; |
| 1206 | struct cifs_ses *ses = sess_data->ses; |
| 1207 | struct smb2_sess_setup_req *req; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1208 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1209 | unsigned int total_len; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1210 | |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1211 | rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req, |
| 1212 | &total_len); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1213 | if (rc) |
| 1214 | return rc; |
| 1215 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1216 | if (sess_data->ses->binding) { |
| 1217 | req->sync_hdr.SessionId = sess_data->ses->Suid; |
| 1218 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
| 1219 | req->PreviousSessionId = 0; |
| 1220 | req->Flags = SMB2_SESSION_REQ_FLAG_BINDING; |
| 1221 | } else { |
| 1222 | /* First session, not a reauthenticate */ |
| 1223 | req->sync_hdr.SessionId = 0; |
| 1224 | /* |
| 1225 | * if reconnect, we need to send previous sess id |
| 1226 | * otherwise it is 0 |
| 1227 | */ |
| 1228 | req->PreviousSessionId = sess_data->previous_session; |
| 1229 | req->Flags = 0; /* MBZ */ |
| 1230 | } |
Steve French | d409014 | 2018-06-13 17:05:58 -0500 | [diff] [blame] | 1231 | |
| 1232 | /* enough to enable echos and oplocks and one max size write */ |
| 1233 | req->sync_hdr.CreditRequest = cpu_to_le16(130); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1234 | |
| 1235 | /* only one of SMB2 signing flags may be set in SMB2 request */ |
| 1236 | if (server->sign) |
| 1237 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED; |
| 1238 | else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */ |
| 1239 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED; |
| 1240 | else |
| 1241 | req->SecurityMode = 0; |
| 1242 | |
Steve French | 8d33096 | 2019-07-25 18:13:10 -0500 | [diff] [blame] | 1243 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 1244 | req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS); |
| 1245 | #else |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1246 | req->Capabilities = 0; |
Steve French | 8d33096 | 2019-07-25 18:13:10 -0500 | [diff] [blame] | 1247 | #endif /* DFS_UPCALL */ |
| 1248 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1249 | req->Channel = 0; /* MBZ */ |
| 1250 | |
| 1251 | sess_data->iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1252 | /* 1 for pad */ |
| 1253 | sess_data->iov[0].iov_len = total_len - 1; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1254 | /* |
| 1255 | * This variable will be used to clear the buffer |
| 1256 | * allocated above in case of any error in the calling function. |
| 1257 | */ |
| 1258 | sess_data->buf0_type = CIFS_SMALL_BUFFER; |
| 1259 | |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | static void |
| 1264 | SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data) |
| 1265 | { |
| 1266 | free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base); |
| 1267 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1268 | } |
| 1269 | |
| 1270 | static int |
| 1271 | SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data) |
| 1272 | { |
| 1273 | int rc; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1274 | struct smb_rqst rqst; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1275 | struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1276 | struct kvec rsp_iov = { NULL, 0 }; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1277 | |
| 1278 | /* Testing shows that buffer offset must be at location of Buffer[0] */ |
| 1279 | req->SecurityBufferOffset = |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1280 | cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1281 | req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len); |
| 1282 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1283 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1284 | rqst.rq_iov = sess_data->iov; |
| 1285 | rqst.rq_nvec = 2; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1286 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1287 | /* BB add code to build os and lm fields */ |
| 1288 | rc = cifs_send_recv(sess_data->xid, sess_data->ses, |
| 1289 | &rqst, |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1290 | &sess_data->buf0_type, |
| 1291 | CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1292 | cifs_small_buf_release(sess_data->iov[0].iov_base); |
| 1293 | memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1294 | |
| 1295 | return rc; |
| 1296 | } |
| 1297 | |
| 1298 | static int |
| 1299 | SMB2_sess_establish_session(struct SMB2_sess_data *sess_data) |
| 1300 | { |
| 1301 | int rc = 0; |
| 1302 | struct cifs_ses *ses = sess_data->ses; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1303 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1304 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1305 | mutex_lock(&server->srv_mutex); |
| 1306 | if (server->ops->generate_signingkey) { |
| 1307 | rc = server->ops->generate_signingkey(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1308 | if (rc) { |
| 1309 | cifs_dbg(FYI, |
| 1310 | "SMB3 session key generation failed\n"); |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1311 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | cabfb36 | 2016-11-07 18:20:50 -0800 | [diff] [blame] | 1312 | return rc; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1313 | } |
| 1314 | } |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1315 | if (!server->session_estab) { |
| 1316 | server->sequence_number = 0x2; |
| 1317 | server->session_estab = true; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1318 | } |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1319 | mutex_unlock(&server->srv_mutex); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1320 | |
| 1321 | cifs_dbg(FYI, "SMB2/3 session established successfully\n"); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1322 | /* keep existing ses state if binding */ |
| 1323 | if (!ses->binding) { |
| 1324 | spin_lock(&GlobalMid_Lock); |
| 1325 | ses->status = CifsGood; |
| 1326 | ses->need_reconnect = false; |
| 1327 | spin_unlock(&GlobalMid_Lock); |
| 1328 | } |
| 1329 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1330 | return rc; |
| 1331 | } |
| 1332 | |
| 1333 | #ifdef CONFIG_CIFS_UPCALL |
| 1334 | static void |
| 1335 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) |
| 1336 | { |
| 1337 | int rc; |
| 1338 | struct cifs_ses *ses = sess_data->ses; |
| 1339 | struct cifs_spnego_msg *msg; |
| 1340 | struct key *spnego_key = NULL; |
| 1341 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1342 | |
| 1343 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1344 | if (rc) |
| 1345 | goto out; |
| 1346 | |
| 1347 | spnego_key = cifs_get_spnego_key(ses); |
| 1348 | if (IS_ERR(spnego_key)) { |
| 1349 | rc = PTR_ERR(spnego_key); |
| 1350 | spnego_key = NULL; |
| 1351 | goto out; |
| 1352 | } |
| 1353 | |
| 1354 | msg = spnego_key->payload.data[0]; |
| 1355 | /* |
| 1356 | * check version field to make sure that cifs.upcall is |
| 1357 | * sending us a response in an expected form |
| 1358 | */ |
| 1359 | if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { |
| 1360 | cifs_dbg(VFS, |
| 1361 | "bad cifs.upcall version. Expected %d got %d", |
| 1362 | CIFS_SPNEGO_UPCALL_VERSION, msg->version); |
| 1363 | rc = -EKEYREJECTED; |
| 1364 | goto out_put_spnego_key; |
| 1365 | } |
| 1366 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1367 | /* keep session key if binding */ |
| 1368 | if (!ses->binding) { |
| 1369 | ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len, |
| 1370 | GFP_KERNEL); |
| 1371 | if (!ses->auth_key.response) { |
| 1372 | cifs_dbg(VFS, |
| 1373 | "Kerberos can't allocate (%u bytes) memory", |
| 1374 | msg->sesskey_len); |
| 1375 | rc = -ENOMEM; |
| 1376 | goto out_put_spnego_key; |
| 1377 | } |
| 1378 | ses->auth_key.len = msg->sesskey_len; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1379 | } |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1380 | |
| 1381 | sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; |
| 1382 | sess_data->iov[1].iov_len = msg->secblob_len; |
| 1383 | |
| 1384 | rc = SMB2_sess_sendreceive(sess_data); |
| 1385 | if (rc) |
| 1386 | goto out_put_spnego_key; |
| 1387 | |
| 1388 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1389 | /* keep session id and flags if binding */ |
| 1390 | if (!ses->binding) { |
| 1391 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1392 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1393 | } |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1394 | |
| 1395 | rc = SMB2_sess_establish_session(sess_data); |
| 1396 | out_put_spnego_key: |
| 1397 | key_invalidate(spnego_key); |
| 1398 | key_put(spnego_key); |
| 1399 | out: |
| 1400 | sess_data->result = rc; |
| 1401 | sess_data->func = NULL; |
| 1402 | SMB2_sess_free_buffer(sess_data); |
| 1403 | } |
| 1404 | #else |
| 1405 | static void |
| 1406 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) |
| 1407 | { |
| 1408 | cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); |
| 1409 | sess_data->result = -EOPNOTSUPP; |
| 1410 | sess_data->func = NULL; |
| 1411 | } |
| 1412 | #endif |
| 1413 | |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1414 | static void |
| 1415 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data); |
| 1416 | |
| 1417 | static void |
| 1418 | SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data) |
| 1419 | { |
| 1420 | int rc; |
| 1421 | struct cifs_ses *ses = sess_data->ses; |
| 1422 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1423 | char *ntlmssp_blob = NULL; |
| 1424 | bool use_spnego = false; /* else use raw ntlmssp */ |
| 1425 | u16 blob_length = 0; |
| 1426 | |
| 1427 | /* |
| 1428 | * If memory allocation is successful, caller of this function |
| 1429 | * frees it. |
| 1430 | */ |
| 1431 | ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); |
| 1432 | if (!ses->ntlmssp) { |
| 1433 | rc = -ENOMEM; |
| 1434 | goto out_err; |
| 1435 | } |
| 1436 | ses->ntlmssp->sesskey_per_smbsess = true; |
| 1437 | |
| 1438 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1439 | if (rc) |
| 1440 | goto out_err; |
| 1441 | |
| 1442 | ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE), |
| 1443 | GFP_KERNEL); |
| 1444 | if (ntlmssp_blob == NULL) { |
| 1445 | rc = -ENOMEM; |
| 1446 | goto out; |
| 1447 | } |
| 1448 | |
| 1449 | build_ntlmssp_negotiate_blob(ntlmssp_blob, ses); |
| 1450 | if (use_spnego) { |
| 1451 | /* BB eventually need to add this */ |
| 1452 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
| 1453 | rc = -EOPNOTSUPP; |
| 1454 | goto out; |
| 1455 | } else { |
| 1456 | blob_length = sizeof(struct _NEGOTIATE_MESSAGE); |
| 1457 | /* with raw NTLMSSP we don't encapsulate in SPNEGO */ |
| 1458 | } |
| 1459 | sess_data->iov[1].iov_base = ntlmssp_blob; |
| 1460 | sess_data->iov[1].iov_len = blob_length; |
| 1461 | |
| 1462 | rc = SMB2_sess_sendreceive(sess_data); |
| 1463 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
| 1464 | |
| 1465 | /* If true, rc here is expected and not an error */ |
| 1466 | if (sess_data->buf0_type != CIFS_NO_BUFFER && |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1467 | rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1468 | rc = 0; |
| 1469 | |
| 1470 | if (rc) |
| 1471 | goto out; |
| 1472 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 1473 | if (offsetof(struct smb2_sess_setup_rsp, Buffer) != |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1474 | le16_to_cpu(rsp->SecurityBufferOffset)) { |
| 1475 | cifs_dbg(VFS, "Invalid security buffer offset %d\n", |
| 1476 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 1477 | rc = -EIO; |
| 1478 | goto out; |
| 1479 | } |
| 1480 | rc = decode_ntlmssp_challenge(rsp->Buffer, |
| 1481 | le16_to_cpu(rsp->SecurityBufferLength), ses); |
| 1482 | if (rc) |
| 1483 | goto out; |
| 1484 | |
| 1485 | cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); |
| 1486 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1487 | /* keep existing ses id and flags if binding */ |
| 1488 | if (!ses->binding) { |
| 1489 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1490 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1491 | } |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1492 | |
| 1493 | out: |
| 1494 | kfree(ntlmssp_blob); |
| 1495 | SMB2_sess_free_buffer(sess_data); |
| 1496 | if (!rc) { |
| 1497 | sess_data->result = 0; |
| 1498 | sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate; |
| 1499 | return; |
| 1500 | } |
| 1501 | out_err: |
| 1502 | kfree(ses->ntlmssp); |
| 1503 | ses->ntlmssp = NULL; |
| 1504 | sess_data->result = rc; |
| 1505 | sess_data->func = NULL; |
| 1506 | } |
| 1507 | |
| 1508 | static void |
| 1509 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data) |
| 1510 | { |
| 1511 | int rc; |
| 1512 | struct cifs_ses *ses = sess_data->ses; |
| 1513 | struct smb2_sess_setup_req *req; |
| 1514 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1515 | unsigned char *ntlmssp_blob = NULL; |
| 1516 | bool use_spnego = false; /* else use raw ntlmssp */ |
| 1517 | u16 blob_length = 0; |
| 1518 | |
| 1519 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1520 | if (rc) |
| 1521 | goto out; |
| 1522 | |
| 1523 | req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base; |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1524 | req->sync_hdr.SessionId = ses->Suid; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1525 | |
| 1526 | rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses, |
| 1527 | sess_data->nls_cp); |
| 1528 | if (rc) { |
| 1529 | cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc); |
| 1530 | goto out; |
| 1531 | } |
| 1532 | |
| 1533 | if (use_spnego) { |
| 1534 | /* BB eventually need to add this */ |
| 1535 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
| 1536 | rc = -EOPNOTSUPP; |
| 1537 | goto out; |
| 1538 | } |
| 1539 | sess_data->iov[1].iov_base = ntlmssp_blob; |
| 1540 | sess_data->iov[1].iov_len = blob_length; |
| 1541 | |
| 1542 | rc = SMB2_sess_sendreceive(sess_data); |
| 1543 | if (rc) |
| 1544 | goto out; |
| 1545 | |
| 1546 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
| 1547 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1548 | /* keep existing ses id and flags if binding */ |
| 1549 | if (!ses->binding) { |
| 1550 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1551 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1552 | } |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1553 | |
| 1554 | rc = SMB2_sess_establish_session(sess_data); |
| 1555 | out: |
| 1556 | kfree(ntlmssp_blob); |
| 1557 | SMB2_sess_free_buffer(sess_data); |
| 1558 | kfree(ses->ntlmssp); |
| 1559 | ses->ntlmssp = NULL; |
| 1560 | sess_data->result = rc; |
| 1561 | sess_data->func = NULL; |
| 1562 | } |
| 1563 | |
| 1564 | static int |
| 1565 | SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data) |
| 1566 | { |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1567 | int type; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1568 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1569 | type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype); |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1570 | cifs_dbg(FYI, "sess setup type %d\n", type); |
| 1571 | if (type == Unspecified) { |
| 1572 | cifs_dbg(VFS, |
| 1573 | "Unable to select appropriate authentication method!"); |
| 1574 | return -EINVAL; |
| 1575 | } |
| 1576 | |
| 1577 | switch (type) { |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1578 | case Kerberos: |
| 1579 | sess_data->func = SMB2_auth_kerberos; |
| 1580 | break; |
| 1581 | case RawNTLMSSP: |
| 1582 | sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate; |
| 1583 | break; |
| 1584 | default: |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1585 | cifs_dbg(VFS, "secType %d not supported!\n", type); |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1586 | return -EOPNOTSUPP; |
| 1587 | } |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1592 | int |
| 1593 | SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, |
| 1594 | const struct nls_table *nls_cp) |
| 1595 | { |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1596 | int rc = 0; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1597 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1598 | struct SMB2_sess_data *sess_data; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1599 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1600 | cifs_dbg(FYI, "Session Setup\n"); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1601 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 1602 | if (!server) { |
| 1603 | WARN(1, "%s: server is NULL!\n", __func__); |
| 1604 | return -EIO; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1605 | } |
| 1606 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1607 | sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL); |
| 1608 | if (!sess_data) |
| 1609 | return -ENOMEM; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1610 | |
| 1611 | rc = SMB2_select_sec(ses, sess_data); |
| 1612 | if (rc) |
| 1613 | goto out; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1614 | sess_data->xid = xid; |
| 1615 | sess_data->ses = ses; |
| 1616 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1617 | sess_data->nls_cp = (struct nls_table *) nls_cp; |
Steve French | b2adf22f | 2018-05-31 15:19:25 -0500 | [diff] [blame] | 1618 | sess_data->previous_session = ses->Suid; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1619 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1620 | /* |
| 1621 | * Initialize the session hash with the server one. |
| 1622 | */ |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1623 | memcpy(ses->preauth_sha_hash, server->preauth_sha_hash, |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1624 | SMB2_PREAUTH_HASH_SIZE); |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1625 | |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1626 | while (sess_data->func) |
| 1627 | sess_data->func(sess_data); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1628 | |
Steve French | c721c38 | 2017-09-19 18:40:03 -0500 | [diff] [blame] | 1629 | if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign)) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1630 | cifs_server_dbg(VFS, "signing requested but authenticated as guest\n"); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1631 | rc = sess_data->result; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1632 | out: |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1633 | kfree(sess_data); |
| 1634 | return rc; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | int |
| 1638 | SMB2_logoff(const unsigned int xid, struct cifs_ses *ses) |
| 1639 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1640 | struct smb_rqst rqst; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1641 | struct smb2_logoff_req *req; /* response is also trivial struct */ |
| 1642 | int rc = 0; |
| 1643 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1644 | int flags = 0; |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1645 | unsigned int total_len; |
| 1646 | struct kvec iov[1]; |
| 1647 | struct kvec rsp_iov; |
| 1648 | int resp_buf_type; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1649 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1650 | cifs_dbg(FYI, "disconnect session %p\n", ses); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1651 | |
| 1652 | if (ses && (ses->server)) |
| 1653 | server = ses->server; |
| 1654 | else |
| 1655 | return -EIO; |
| 1656 | |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 1657 | /* no need to send SMB logoff if uid already closed due to reconnect */ |
| 1658 | if (ses->need_reconnect) |
| 1659 | goto smb2_session_already_dead; |
| 1660 | |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1661 | rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1662 | if (rc) |
| 1663 | return rc; |
| 1664 | |
| 1665 | /* since no tcon, smb2_init can not do this, so do here */ |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1666 | req->sync_hdr.SessionId = ses->Suid; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1667 | |
| 1668 | if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) |
| 1669 | flags |= CIFS_TRANSFORM_REQ; |
| 1670 | else if (server->sign) |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1671 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1672 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 1673 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1674 | |
| 1675 | iov[0].iov_base = (char *)req; |
| 1676 | iov[0].iov_len = total_len; |
| 1677 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1678 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1679 | rqst.rq_iov = iov; |
| 1680 | rqst.rq_nvec = 1; |
| 1681 | |
| 1682 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1683 | cifs_small_buf_release(req); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1684 | /* |
| 1685 | * No tcon so can't do |
| 1686 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); |
| 1687 | */ |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 1688 | |
| 1689 | smb2_session_already_dead: |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1690 | return rc; |
| 1691 | } |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1692 | |
| 1693 | static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code) |
| 1694 | { |
Pavel Shilovsky | d60622e | 2012-05-28 15:19:39 +0400 | [diff] [blame] | 1695 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */) |
| 1699 | |
Steve French | de9f68df | 2013-11-15 11:26:24 -0600 | [diff] [blame] | 1700 | /* These are similar values to what Windows uses */ |
| 1701 | static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon) |
| 1702 | { |
| 1703 | tcon->max_chunks = 256; |
| 1704 | tcon->max_bytes_chunk = 1048576; |
| 1705 | tcon->max_bytes_copy = 16777216; |
| 1706 | } |
| 1707 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1708 | int |
| 1709 | SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, |
| 1710 | struct cifs_tcon *tcon, const struct nls_table *cp) |
| 1711 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1712 | struct smb_rqst rqst; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1713 | struct smb2_tree_connect_req *req; |
| 1714 | struct smb2_tree_connect_rsp *rsp = NULL; |
| 1715 | struct kvec iov[2]; |
Aurélien Aptel | db3b547 | 2017-10-11 13:23:36 +0200 | [diff] [blame] | 1716 | struct kvec rsp_iov = { NULL, 0 }; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1717 | int rc = 0; |
| 1718 | int resp_buftype; |
| 1719 | int unc_path_len; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1720 | __le16 *unc_path = NULL; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1721 | int flags = 0; |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1722 | unsigned int total_len; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1723 | struct TCP_Server_Info *server = ses->server; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1724 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1725 | cifs_dbg(FYI, "TCON\n"); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1726 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1727 | if (!server || !tree) |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1728 | return -EIO; |
| 1729 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1730 | unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL); |
| 1731 | if (unc_path == NULL) |
| 1732 | return -ENOMEM; |
| 1733 | |
| 1734 | unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1; |
| 1735 | unc_path_len *= 2; |
| 1736 | if (unc_path_len < 2) { |
| 1737 | kfree(unc_path); |
| 1738 | return -EINVAL; |
| 1739 | } |
| 1740 | |
Jan-Marek Glogowski | 806a28e | 2017-02-20 12:25:58 +0100 | [diff] [blame] | 1741 | /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1742 | tcon->tid = 0; |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 1743 | atomic_set(&tcon->num_remote_opens, 0); |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1744 | rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req, |
| 1745 | &total_len); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1746 | if (rc) { |
| 1747 | kfree(unc_path); |
| 1748 | return rc; |
| 1749 | } |
| 1750 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 1751 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1752 | flags |= CIFS_TRANSFORM_REQ; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1753 | |
| 1754 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1755 | /* 1 for pad */ |
| 1756 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1757 | |
| 1758 | /* Testing shows that buffer offset must be at location of Buffer[0] */ |
| 1759 | req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req) |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1760 | - 1 /* pad */); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1761 | req->PathLength = cpu_to_le16(unc_path_len - 2); |
| 1762 | iov[1].iov_base = unc_path; |
| 1763 | iov[1].iov_len = unc_path_len; |
| 1764 | |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1765 | /* |
| 1766 | * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 |
| 1767 | * 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] | 1768 | * (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] | 1769 | */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1770 | if ((server->dialect == SMB311_PROT_ID) && |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1771 | !smb3_encryption_required(tcon) && |
Steve French | 8c11a60 | 2019-03-22 22:31:17 -0500 | [diff] [blame] | 1772 | !(ses->session_flags & |
| 1773 | (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) && |
| 1774 | ((ses->user_name != NULL) || (ses->sectype == Kerberos))) |
Steve French | 6188f28 | 2018-03-13 02:29:36 -0500 | [diff] [blame] | 1775 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
| 1776 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1777 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1778 | rqst.rq_iov = iov; |
| 1779 | rqst.rq_nvec = 2; |
| 1780 | |
Steve French | 4fe75c4 | 2019-02-14 01:19:02 -0600 | [diff] [blame] | 1781 | /* Need 64 for max size write so ask for more in case not there yet */ |
| 1782 | req->sync_hdr.CreditRequest = cpu_to_le16(64); |
| 1783 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1784 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1785 | cifs_small_buf_release(req); |
| 1786 | rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base; |
Steve French | f8af49d | 2018-10-28 00:47:11 -0500 | [diff] [blame] | 1787 | trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1788 | if (rc != 0) { |
| 1789 | if (tcon) { |
| 1790 | cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE); |
| 1791 | tcon->need_reconnect = true; |
| 1792 | } |
| 1793 | goto tcon_error_exit; |
| 1794 | } |
| 1795 | |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1796 | switch (rsp->ShareType) { |
| 1797 | case SMB2_SHARE_TYPE_DISK: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1798 | cifs_dbg(FYI, "connection to disk share\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1799 | break; |
| 1800 | case SMB2_SHARE_TYPE_PIPE: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1801 | tcon->pipe = true; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1802 | cifs_dbg(FYI, "connection to pipe share\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1803 | break; |
| 1804 | case SMB2_SHARE_TYPE_PRINT: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1805 | tcon->print = true; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1806 | cifs_dbg(FYI, "connection to printer\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1807 | break; |
| 1808 | default: |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1809 | cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1810 | rc = -EOPNOTSUPP; |
| 1811 | goto tcon_error_exit; |
| 1812 | } |
| 1813 | |
| 1814 | tcon->share_flags = le32_to_cpu(rsp->ShareFlags); |
Steve French | 769ee6a | 2013-06-19 14:15:30 -0500 | [diff] [blame] | 1815 | tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */ |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1816 | tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1817 | tcon->tidStatus = CifsGood; |
| 1818 | tcon->need_reconnect = false; |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1819 | tcon->tid = rsp->sync_hdr.TreeId; |
Zhao Hongjiang | 46b51d0 | 2013-06-24 01:57:47 -0500 | [diff] [blame] | 1820 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1821 | |
| 1822 | if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) && |
| 1823 | ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0)) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1824 | cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n"); |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1825 | |
| 1826 | if (tcon->seal && |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1827 | !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1828 | cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n"); |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1829 | |
Steve French | de9f68df | 2013-11-15 11:26:24 -0600 | [diff] [blame] | 1830 | init_copy_chunk_defaults(tcon); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1831 | if (server->ops->validate_negotiate) |
| 1832 | rc = server->ops->validate_negotiate(xid, tcon); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1833 | tcon_exit: |
Steve French | f8af49d | 2018-10-28 00:47:11 -0500 | [diff] [blame] | 1834 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1835 | free_rsp_buf(resp_buftype, rsp); |
| 1836 | kfree(unc_path); |
| 1837 | return rc; |
| 1838 | |
| 1839 | tcon_error_exit: |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1840 | if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1841 | cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1842 | } |
| 1843 | goto tcon_exit; |
| 1844 | } |
| 1845 | |
| 1846 | int |
| 1847 | SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) |
| 1848 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1849 | struct smb_rqst rqst; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1850 | struct smb2_tree_disconnect_req *req; /* response is trivial */ |
| 1851 | int rc = 0; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1852 | struct cifs_ses *ses = tcon->ses; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1853 | int flags = 0; |
Ronnie Sahlberg | 4eecf4c | 2017-11-09 12:14:18 +1100 | [diff] [blame] | 1854 | unsigned int total_len; |
| 1855 | struct kvec iov[1]; |
| 1856 | struct kvec rsp_iov; |
| 1857 | int resp_buf_type; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1858 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1859 | cifs_dbg(FYI, "Tree Disconnect\n"); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1860 | |
Christos Gkekas | 68a6afa | 2017-07-09 11:45:04 +0100 | [diff] [blame] | 1861 | if (!ses || !(ses->server)) |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1862 | return -EIO; |
| 1863 | |
| 1864 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) |
| 1865 | return 0; |
| 1866 | |
Pavel Shilovsky | d919131 | 2019-12-10 11:44:52 -0800 | [diff] [blame] | 1867 | close_shroot_lease(&tcon->crfid); |
Ronnie Sahlberg | 72e73c7 | 2019-11-07 17:00:38 +1000 | [diff] [blame] | 1868 | |
Ronnie Sahlberg | 4eecf4c | 2017-11-09 12:14:18 +1100 | [diff] [blame] | 1869 | rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req, |
| 1870 | &total_len); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1871 | if (rc) |
| 1872 | return rc; |
| 1873 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 1874 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1875 | flags |= CIFS_TRANSFORM_REQ; |
| 1876 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 1877 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 4eecf4c | 2017-11-09 12:14:18 +1100 | [diff] [blame] | 1878 | |
| 1879 | iov[0].iov_base = (char *)req; |
| 1880 | iov[0].iov_len = total_len; |
| 1881 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1882 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1883 | rqst.rq_iov = iov; |
| 1884 | rqst.rq_nvec = 1; |
| 1885 | |
| 1886 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1887 | cifs_small_buf_release(req); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1888 | if (rc) |
| 1889 | cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE); |
| 1890 | |
| 1891 | return rc; |
| 1892 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 1893 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1894 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1895 | static struct create_durable * |
| 1896 | create_durable_buf(void) |
| 1897 | { |
| 1898 | struct create_durable *buf; |
| 1899 | |
| 1900 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); |
| 1901 | if (!buf) |
| 1902 | return NULL; |
| 1903 | |
| 1904 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1905 | (struct create_durable, Data)); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1906 | buf->ccontext.DataLength = cpu_to_le32(16); |
| 1907 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1908 | (struct create_durable, Name)); |
| 1909 | buf->ccontext.NameLength = cpu_to_le16(4); |
Steve French | 12197a7 | 2014-05-14 05:29:40 -0700 | [diff] [blame] | 1910 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */ |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1911 | buf->Name[0] = 'D'; |
| 1912 | buf->Name[1] = 'H'; |
| 1913 | buf->Name[2] = 'n'; |
| 1914 | buf->Name[3] = 'Q'; |
| 1915 | return buf; |
| 1916 | } |
| 1917 | |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1918 | static struct create_durable * |
| 1919 | create_reconnect_durable_buf(struct cifs_fid *fid) |
| 1920 | { |
| 1921 | struct create_durable *buf; |
| 1922 | |
| 1923 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); |
| 1924 | if (!buf) |
| 1925 | return NULL; |
| 1926 | |
| 1927 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1928 | (struct create_durable, Data)); |
| 1929 | buf->ccontext.DataLength = cpu_to_le32(16); |
| 1930 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1931 | (struct create_durable, Name)); |
| 1932 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1933 | buf->Data.Fid.PersistentFileId = fid->persistent_fid; |
| 1934 | buf->Data.Fid.VolatileFileId = fid->volatile_fid; |
Steve French | 12197a7 | 2014-05-14 05:29:40 -0700 | [diff] [blame] | 1935 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */ |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1936 | buf->Name[0] = 'D'; |
| 1937 | buf->Name[1] = 'H'; |
| 1938 | buf->Name[2] = 'n'; |
| 1939 | buf->Name[3] = 'C'; |
| 1940 | return buf; |
| 1941 | } |
| 1942 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1943 | static void |
| 1944 | parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf) |
| 1945 | { |
| 1946 | struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc; |
| 1947 | |
| 1948 | cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n", |
| 1949 | pdisk_id->DiskFileId, pdisk_id->VolumeId); |
| 1950 | buf->IndexNumber = pdisk_id->DiskFileId; |
| 1951 | } |
| 1952 | |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1953 | static void |
| 1954 | parse_posix_ctxt(struct create_context *cc, struct smb_posix_info *pposix_inf) |
| 1955 | { |
| 1956 | /* struct smb_posix_info *ppinf = (struct smb_posix_info *)cc; */ |
| 1957 | |
| 1958 | /* TODO: Need to add parsing for the context and return */ |
| 1959 | printk_once(KERN_WARNING |
| 1960 | "SMB3 3.11 POSIX response context not completed yet\n"); |
| 1961 | } |
| 1962 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1963 | void |
| 1964 | smb2_parse_contexts(struct TCP_Server_Info *server, |
Ronnie Sahlberg | b0f6df7 | 2019-03-12 13:58:31 +1000 | [diff] [blame] | 1965 | struct smb2_create_rsp *rsp, |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1966 | unsigned int *epoch, char *lease_key, __u8 *oplock, |
| 1967 | struct smb2_file_all_info *buf) |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1968 | { |
| 1969 | char *data_offset; |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 1970 | struct create_context *cc; |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1971 | unsigned int next; |
| 1972 | unsigned int remaining; |
Pavel Shilovsky | fd55439 | 2013-07-09 19:44:56 +0400 | [diff] [blame] | 1973 | char *name; |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1974 | const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C, |
| 1975 | 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83, |
| 1976 | 0xDE, 0x96, 0x8B, 0xCD, 0x7C}; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1977 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1978 | *oplock = 0; |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 1979 | data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset); |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1980 | remaining = le32_to_cpu(rsp->CreateContextsLength); |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 1981 | cc = (struct create_context *)data_offset; |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1982 | |
| 1983 | /* Initialize inode number to 0 in case no valid data in qfid context */ |
| 1984 | if (buf) |
| 1985 | buf->IndexNumber = 0; |
| 1986 | |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1987 | while (remaining >= sizeof(struct create_context)) { |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 1988 | name = le16_to_cpu(cc->NameOffset) + (char *)cc; |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1989 | if (le16_to_cpu(cc->NameLength) == 4 && |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1990 | strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0) |
| 1991 | *oplock = server->ops->parse_lease_buf(cc, epoch, |
| 1992 | lease_key); |
| 1993 | else if (buf && (le16_to_cpu(cc->NameLength) == 4) && |
| 1994 | strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0) |
| 1995 | parse_query_id_ctxt(cc, buf); |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1996 | else if ((le16_to_cpu(cc->NameLength) == 16)) { |
| 1997 | if (memcmp(name, smb3_create_tag_posix, 16) == 0) |
| 1998 | parse_posix_ctxt(cc, NULL); |
| 1999 | } |
| 2000 | /* else { |
| 2001 | cifs_dbg(FYI, "Context not matched with len %d\n", |
| 2002 | le16_to_cpu(cc->NameLength)); |
| 2003 | cifs_dump_mem("Cctxt name: ", name, 4); |
| 2004 | } */ |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 2005 | |
| 2006 | next = le32_to_cpu(cc->Next); |
| 2007 | if (!next) |
| 2008 | break; |
| 2009 | remaining -= next; |
| 2010 | cc = (struct create_context *)((char *)cc + next); |
| 2011 | } |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2012 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2013 | if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) |
| 2014 | *oplock = rsp->OplockLevel; |
| 2015 | |
| 2016 | return; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2019 | static int |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2020 | add_lease_context(struct TCP_Server_Info *server, struct kvec *iov, |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2021 | unsigned int *num_iovec, u8 *lease_key, __u8 *oplock) |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2022 | { |
| 2023 | struct smb2_create_req *req = iov[0].iov_base; |
| 2024 | unsigned int num = *num_iovec; |
| 2025 | |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2026 | iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock); |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2027 | if (iov[num].iov_base == NULL) |
| 2028 | return -ENOMEM; |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2029 | iov[num].iov_len = server->vals->create_lease_size; |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2030 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 2031 | if (!req->CreateContextsOffset) |
| 2032 | req->CreateContextsOffset = cpu_to_le32( |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2033 | sizeof(struct smb2_create_req) + |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2034 | iov[num - 1].iov_len); |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2035 | le32_add_cpu(&req->CreateContextsLength, |
| 2036 | server->vals->create_lease_size); |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2037 | *num_iovec = num + 1; |
| 2038 | return 0; |
| 2039 | } |
| 2040 | |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2041 | static struct create_durable_v2 * |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2042 | create_durable_v2_buf(struct cifs_open_parms *oparms) |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2043 | { |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2044 | struct cifs_fid *pfid = oparms->fid; |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2045 | struct create_durable_v2 *buf; |
| 2046 | |
| 2047 | buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL); |
| 2048 | if (!buf) |
| 2049 | return NULL; |
| 2050 | |
| 2051 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2052 | (struct create_durable_v2, dcontext)); |
| 2053 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2)); |
| 2054 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2055 | (struct create_durable_v2, Name)); |
| 2056 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2057 | |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2058 | /* |
| 2059 | * NB: Handle timeout defaults to 0, which allows server to choose |
| 2060 | * (most servers default to 120 seconds) and most clients default to 0. |
| 2061 | * This can be overridden at mount ("handletimeout=") if the user wants |
| 2062 | * a different persistent (or resilient) handle timeout for all opens |
| 2063 | * opens on a particular SMB3 mount. |
| 2064 | */ |
| 2065 | buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2066 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); |
Steve French | fa70b87 | 2016-09-22 00:39:34 -0500 | [diff] [blame] | 2067 | generate_random_uuid(buf->dcontext.CreateGuid); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2068 | memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16); |
| 2069 | |
| 2070 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */ |
| 2071 | buf->Name[0] = 'D'; |
| 2072 | buf->Name[1] = 'H'; |
| 2073 | buf->Name[2] = '2'; |
| 2074 | buf->Name[3] = 'Q'; |
| 2075 | return buf; |
| 2076 | } |
| 2077 | |
| 2078 | static struct create_durable_handle_reconnect_v2 * |
| 2079 | create_reconnect_durable_v2_buf(struct cifs_fid *fid) |
| 2080 | { |
| 2081 | struct create_durable_handle_reconnect_v2 *buf; |
| 2082 | |
| 2083 | buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2), |
| 2084 | GFP_KERNEL); |
| 2085 | if (!buf) |
| 2086 | return NULL; |
| 2087 | |
| 2088 | buf->ccontext.DataOffset = |
| 2089 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, |
| 2090 | dcontext)); |
| 2091 | buf->ccontext.DataLength = |
| 2092 | cpu_to_le32(sizeof(struct durable_reconnect_context_v2)); |
| 2093 | buf->ccontext.NameOffset = |
| 2094 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, |
| 2095 | Name)); |
| 2096 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2097 | |
| 2098 | buf->dcontext.Fid.PersistentFileId = fid->persistent_fid; |
| 2099 | buf->dcontext.Fid.VolatileFileId = fid->volatile_fid; |
| 2100 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); |
| 2101 | memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16); |
| 2102 | |
| 2103 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */ |
| 2104 | buf->Name[0] = 'D'; |
| 2105 | buf->Name[1] = 'H'; |
| 2106 | buf->Name[2] = '2'; |
| 2107 | buf->Name[3] = 'C'; |
| 2108 | return buf; |
| 2109 | } |
| 2110 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2111 | static int |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2112 | add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec, |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 2113 | struct cifs_open_parms *oparms) |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2114 | { |
| 2115 | struct smb2_create_req *req = iov[0].iov_base; |
| 2116 | unsigned int num = *num_iovec; |
| 2117 | |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2118 | iov[num].iov_base = create_durable_v2_buf(oparms); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2119 | if (iov[num].iov_base == NULL) |
| 2120 | return -ENOMEM; |
| 2121 | iov[num].iov_len = sizeof(struct create_durable_v2); |
| 2122 | if (!req->CreateContextsOffset) |
| 2123 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2124 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2125 | iov[1].iov_len); |
| 2126 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2)); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2127 | *num_iovec = num + 1; |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
| 2131 | static int |
| 2132 | add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec, |
| 2133 | struct cifs_open_parms *oparms) |
| 2134 | { |
| 2135 | struct smb2_create_req *req = iov[0].iov_base; |
| 2136 | unsigned int num = *num_iovec; |
| 2137 | |
| 2138 | /* indicate that we don't need to relock the file */ |
| 2139 | oparms->reconnect = false; |
| 2140 | |
| 2141 | iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid); |
| 2142 | if (iov[num].iov_base == NULL) |
| 2143 | return -ENOMEM; |
| 2144 | iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2); |
| 2145 | if (!req->CreateContextsOffset) |
| 2146 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2147 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2148 | iov[1].iov_len); |
| 2149 | le32_add_cpu(&req->CreateContextsLength, |
| 2150 | sizeof(struct create_durable_handle_reconnect_v2)); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2151 | *num_iovec = num + 1; |
| 2152 | return 0; |
| 2153 | } |
| 2154 | |
| 2155 | static int |
| 2156 | add_durable_context(struct kvec *iov, unsigned int *num_iovec, |
| 2157 | struct cifs_open_parms *oparms, bool use_persistent) |
| 2158 | { |
| 2159 | struct smb2_create_req *req = iov[0].iov_base; |
| 2160 | unsigned int num = *num_iovec; |
| 2161 | |
| 2162 | if (use_persistent) { |
| 2163 | if (oparms->reconnect) |
| 2164 | return add_durable_reconnect_v2_context(iov, num_iovec, |
| 2165 | oparms); |
| 2166 | else |
| 2167 | return add_durable_v2_context(iov, num_iovec, oparms); |
| 2168 | } |
| 2169 | |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 2170 | if (oparms->reconnect) { |
| 2171 | iov[num].iov_base = create_reconnect_durable_buf(oparms->fid); |
| 2172 | /* indicate that we don't need to relock the file */ |
| 2173 | oparms->reconnect = false; |
| 2174 | } else |
| 2175 | iov[num].iov_base = create_durable_buf(); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2176 | if (iov[num].iov_base == NULL) |
| 2177 | return -ENOMEM; |
| 2178 | iov[num].iov_len = sizeof(struct create_durable); |
| 2179 | if (!req->CreateContextsOffset) |
| 2180 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2181 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2182 | iov[1].iov_len); |
Wei Yongjun | 31f92e9 | 2013-08-26 14:34:46 +0800 | [diff] [blame] | 2183 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable)); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2184 | *num_iovec = num + 1; |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2188 | /* See MS-SMB2 2.2.13.2.7 */ |
| 2189 | static struct crt_twarp_ctxt * |
| 2190 | create_twarp_buf(__u64 timewarp) |
| 2191 | { |
| 2192 | struct crt_twarp_ctxt *buf; |
| 2193 | |
| 2194 | buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL); |
| 2195 | if (!buf) |
| 2196 | return NULL; |
| 2197 | |
| 2198 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2199 | (struct crt_twarp_ctxt, Timestamp)); |
| 2200 | buf->ccontext.DataLength = cpu_to_le32(8); |
| 2201 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2202 | (struct crt_twarp_ctxt, Name)); |
| 2203 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2204 | /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */ |
| 2205 | buf->Name[0] = 'T'; |
| 2206 | buf->Name[1] = 'W'; |
| 2207 | buf->Name[2] = 'r'; |
| 2208 | buf->Name[3] = 'p'; |
| 2209 | buf->Timestamp = cpu_to_le64(timewarp); |
| 2210 | return buf; |
| 2211 | } |
| 2212 | |
| 2213 | /* See MS-SMB2 2.2.13.2.7 */ |
| 2214 | static int |
| 2215 | add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp) |
| 2216 | { |
| 2217 | struct smb2_create_req *req = iov[0].iov_base; |
| 2218 | unsigned int num = *num_iovec; |
| 2219 | |
| 2220 | iov[num].iov_base = create_twarp_buf(timewarp); |
| 2221 | if (iov[num].iov_base == NULL) |
| 2222 | return -ENOMEM; |
| 2223 | iov[num].iov_len = sizeof(struct crt_twarp_ctxt); |
| 2224 | if (!req->CreateContextsOffset) |
| 2225 | req->CreateContextsOffset = cpu_to_le32( |
| 2226 | sizeof(struct smb2_create_req) + |
| 2227 | iov[num - 1].iov_len); |
| 2228 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt)); |
| 2229 | *num_iovec = num + 1; |
| 2230 | return 0; |
| 2231 | } |
| 2232 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2233 | /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */ |
| 2234 | static struct crt_sd_ctxt * |
| 2235 | create_sd_buf(umode_t mode, unsigned int *len) |
| 2236 | { |
| 2237 | struct crt_sd_ctxt *buf; |
| 2238 | struct cifs_ace *pace; |
| 2239 | unsigned int sdlen, acelen; |
| 2240 | |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2241 | *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2, |
| 2242 | 8); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2243 | buf = kzalloc(*len, GFP_KERNEL); |
| 2244 | if (buf == NULL) |
| 2245 | return buf; |
| 2246 | |
| 2247 | sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) + |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2248 | 2 * sizeof(struct cifs_ace); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2249 | |
| 2250 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2251 | (struct crt_sd_ctxt, sd)); |
| 2252 | buf->ccontext.DataLength = cpu_to_le32(sdlen); |
| 2253 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2254 | (struct crt_sd_ctxt, Name)); |
| 2255 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2256 | /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */ |
| 2257 | buf->Name[0] = 'S'; |
| 2258 | buf->Name[1] = 'e'; |
| 2259 | buf->Name[2] = 'c'; |
| 2260 | buf->Name[3] = 'D'; |
| 2261 | buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */ |
| 2262 | /* |
| 2263 | * ACL is "self relative" ie ACL is stored in contiguous block of memory |
| 2264 | * and "DP" ie the DACL is present |
| 2265 | */ |
| 2266 | buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP); |
| 2267 | |
| 2268 | /* offset owner, group and Sbz1 and SACL are all zero */ |
| 2269 | buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd)); |
| 2270 | buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */ |
| 2271 | |
| 2272 | /* create one ACE to hold the mode embedded in reserved special SID */ |
| 2273 | pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf); |
| 2274 | acelen = setup_special_mode_ACE(pace, (__u64)mode); |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2275 | /* and one more ACE to allow access for authenticated users */ |
| 2276 | pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) + |
| 2277 | (char *)buf)); |
| 2278 | acelen += setup_authusers_ACE(pace); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2279 | buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen); |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2280 | buf->acl.AceCount = cpu_to_le16(2); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2281 | return buf; |
| 2282 | } |
| 2283 | |
| 2284 | static int |
| 2285 | add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode) |
| 2286 | { |
| 2287 | struct smb2_create_req *req = iov[0].iov_base; |
| 2288 | unsigned int num = *num_iovec; |
| 2289 | unsigned int len = 0; |
| 2290 | |
| 2291 | iov[num].iov_base = create_sd_buf(mode, &len); |
| 2292 | if (iov[num].iov_base == NULL) |
| 2293 | return -ENOMEM; |
| 2294 | iov[num].iov_len = len; |
| 2295 | if (!req->CreateContextsOffset) |
| 2296 | req->CreateContextsOffset = cpu_to_le32( |
| 2297 | sizeof(struct smb2_create_req) + |
| 2298 | iov[num - 1].iov_len); |
| 2299 | le32_add_cpu(&req->CreateContextsLength, len); |
| 2300 | *num_iovec = num + 1; |
| 2301 | return 0; |
| 2302 | } |
| 2303 | |
Steve French | ff2a09e | 2019-07-06 14:41:38 -0500 | [diff] [blame] | 2304 | static struct crt_query_id_ctxt * |
| 2305 | create_query_id_buf(void) |
| 2306 | { |
| 2307 | struct crt_query_id_ctxt *buf; |
| 2308 | |
| 2309 | buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL); |
| 2310 | if (!buf) |
| 2311 | return NULL; |
| 2312 | |
| 2313 | buf->ccontext.DataOffset = cpu_to_le16(0); |
| 2314 | buf->ccontext.DataLength = cpu_to_le32(0); |
| 2315 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2316 | (struct crt_query_id_ctxt, Name)); |
| 2317 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2318 | /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */ |
| 2319 | buf->Name[0] = 'Q'; |
| 2320 | buf->Name[1] = 'F'; |
| 2321 | buf->Name[2] = 'i'; |
| 2322 | buf->Name[3] = 'd'; |
| 2323 | return buf; |
| 2324 | } |
| 2325 | |
| 2326 | /* See MS-SMB2 2.2.13.2.9 */ |
| 2327 | static int |
| 2328 | add_query_id_context(struct kvec *iov, unsigned int *num_iovec) |
| 2329 | { |
| 2330 | struct smb2_create_req *req = iov[0].iov_base; |
| 2331 | unsigned int num = *num_iovec; |
| 2332 | |
| 2333 | iov[num].iov_base = create_query_id_buf(); |
| 2334 | if (iov[num].iov_base == NULL) |
| 2335 | return -ENOMEM; |
| 2336 | iov[num].iov_len = sizeof(struct crt_query_id_ctxt); |
| 2337 | if (!req->CreateContextsOffset) |
| 2338 | req->CreateContextsOffset = cpu_to_le32( |
| 2339 | sizeof(struct smb2_create_req) + |
| 2340 | iov[num - 1].iov_len); |
| 2341 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt)); |
| 2342 | *num_iovec = num + 1; |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2346 | static int |
| 2347 | alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len, |
| 2348 | const char *treename, const __le16 *path) |
| 2349 | { |
| 2350 | int treename_len, path_len; |
| 2351 | struct nls_table *cp; |
| 2352 | const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)}; |
| 2353 | |
| 2354 | /* |
| 2355 | * skip leading "\\" |
| 2356 | */ |
| 2357 | treename_len = strlen(treename); |
| 2358 | if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\')) |
| 2359 | return -EINVAL; |
| 2360 | |
| 2361 | treename += 2; |
| 2362 | treename_len -= 2; |
| 2363 | |
| 2364 | path_len = UniStrnlen((wchar_t *)path, PATH_MAX); |
| 2365 | |
| 2366 | /* |
| 2367 | * make room for one path separator between the treename and |
| 2368 | * path |
| 2369 | */ |
| 2370 | *out_len = treename_len + 1 + path_len; |
| 2371 | |
| 2372 | /* |
| 2373 | * final path needs to be null-terminated UTF16 with a |
| 2374 | * size aligned to 8 |
| 2375 | */ |
| 2376 | |
| 2377 | *out_size = roundup((*out_len+1)*2, 8); |
| 2378 | *out_path = kzalloc(*out_size, GFP_KERNEL); |
| 2379 | if (!*out_path) |
| 2380 | return -ENOMEM; |
| 2381 | |
| 2382 | cp = load_nls_default(); |
| 2383 | cifs_strtoUTF16(*out_path, treename, treename_len, cp); |
| 2384 | UniStrcat(*out_path, sep); |
| 2385 | UniStrcat(*out_path, path); |
| 2386 | unload_nls(cp); |
| 2387 | |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2391 | int smb311_posix_mkdir(const unsigned int xid, struct inode *inode, |
| 2392 | umode_t mode, struct cifs_tcon *tcon, |
| 2393 | const char *full_path, |
| 2394 | struct cifs_sb_info *cifs_sb) |
| 2395 | { |
| 2396 | struct smb_rqst rqst; |
| 2397 | struct smb2_create_req *req; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2398 | struct smb2_create_rsp *rsp = NULL; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2399 | struct cifs_ses *ses = tcon->ses; |
| 2400 | struct kvec iov[3]; /* make sure at least one for each open context */ |
| 2401 | struct kvec rsp_iov = {NULL, 0}; |
| 2402 | int resp_buftype; |
| 2403 | int uni_path_len; |
| 2404 | __le16 *copy_path = NULL; |
| 2405 | int copy_size; |
| 2406 | int rc = 0; |
| 2407 | unsigned int n_iov = 2; |
| 2408 | __u32 file_attributes = 0; |
| 2409 | char *pc_buf = NULL; |
| 2410 | int flags = 0; |
| 2411 | unsigned int total_len; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2412 | __le16 *utf16_path = NULL; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2413 | |
| 2414 | cifs_dbg(FYI, "mkdir\n"); |
| 2415 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2416 | /* resource #1: path allocation */ |
| 2417 | utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb); |
| 2418 | if (!utf16_path) |
| 2419 | return -ENOMEM; |
| 2420 | |
YueHaibing | 29cbfa1 | 2018-12-18 02:51:51 +0000 | [diff] [blame] | 2421 | if (!ses || !(ses->server)) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2422 | rc = -EIO; |
| 2423 | goto err_free_path; |
| 2424 | } |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2425 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2426 | /* resource #2: request */ |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2427 | rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2428 | if (rc) |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2429 | goto err_free_path; |
| 2430 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2431 | |
| 2432 | if (smb3_encryption_required(tcon)) |
| 2433 | flags |= CIFS_TRANSFORM_REQ; |
| 2434 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2435 | req->ImpersonationLevel = IL_IMPERSONATION; |
| 2436 | req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES); |
| 2437 | /* File attributes ignored on open (used in create though) */ |
| 2438 | req->FileAttributes = cpu_to_le32(file_attributes); |
| 2439 | req->ShareAccess = FILE_SHARE_ALL_LE; |
| 2440 | req->CreateDisposition = cpu_to_le32(FILE_CREATE); |
| 2441 | req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE); |
| 2442 | |
| 2443 | iov[0].iov_base = (char *)req; |
| 2444 | /* -1 since last byte is buf[0] which is sent below (path) */ |
| 2445 | iov[0].iov_len = total_len - 1; |
| 2446 | |
| 2447 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); |
| 2448 | |
| 2449 | /* [MS-SMB2] 2.2.13 NameOffset: |
| 2450 | * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of |
| 2451 | * the SMB2 header, the file name includes a prefix that will |
| 2452 | * be processed during DFS name normalization as specified in |
| 2453 | * section 3.3.5.9. Otherwise, the file name is relative to |
| 2454 | * the share that is identified by the TreeId in the SMB2 |
| 2455 | * header. |
| 2456 | */ |
| 2457 | if (tcon->share_flags & SHI1005_FLAGS_DFS) { |
| 2458 | int name_len; |
| 2459 | |
| 2460 | req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; |
| 2461 | rc = alloc_path_with_tree_prefix(©_path, ©_size, |
| 2462 | &name_len, |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2463 | tcon->treeName, utf16_path); |
| 2464 | if (rc) |
| 2465 | goto err_free_req; |
| 2466 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2467 | req->NameLength = cpu_to_le16(name_len * 2); |
| 2468 | uni_path_len = copy_size; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2469 | /* free before overwriting resource */ |
| 2470 | kfree(utf16_path); |
| 2471 | utf16_path = copy_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2472 | } else { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2473 | uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2474 | /* MUST set path len (NameLength) to 0 opening root of share */ |
| 2475 | req->NameLength = cpu_to_le16(uni_path_len - 2); |
| 2476 | if (uni_path_len % 8 != 0) { |
| 2477 | copy_size = roundup(uni_path_len, 8); |
| 2478 | copy_path = kzalloc(copy_size, GFP_KERNEL); |
| 2479 | if (!copy_path) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2480 | rc = -ENOMEM; |
| 2481 | goto err_free_req; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2482 | } |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2483 | memcpy((char *)copy_path, (const char *)utf16_path, |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2484 | uni_path_len); |
| 2485 | uni_path_len = copy_size; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2486 | /* free before overwriting resource */ |
| 2487 | kfree(utf16_path); |
| 2488 | utf16_path = copy_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | iov[1].iov_len = uni_path_len; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2493 | iov[1].iov_base = utf16_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2494 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
| 2495 | |
| 2496 | if (tcon->posix_extensions) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2497 | /* resource #3: posix buf */ |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2498 | rc = add_posix_context(iov, &n_iov, mode); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2499 | if (rc) |
| 2500 | goto err_free_req; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2501 | pc_buf = iov[n_iov-1].iov_base; |
| 2502 | } |
| 2503 | |
| 2504 | |
| 2505 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 2506 | rqst.rq_iov = iov; |
| 2507 | rqst.rq_nvec = n_iov; |
| 2508 | |
Steve French | d2f1542 | 2019-09-22 00:55:46 -0500 | [diff] [blame] | 2509 | /* 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] | 2510 | trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE, |
| 2511 | FILE_WRITE_ATTRIBUTES); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2512 | /* resource #4: response buffer */ |
| 2513 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
| 2514 | if (rc) { |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2515 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); |
| 2516 | trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid, |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2517 | CREATE_NOT_FILE, |
| 2518 | FILE_WRITE_ATTRIBUTES, rc); |
| 2519 | goto err_free_rsp_buf; |
| 2520 | } |
| 2521 | |
| 2522 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; |
| 2523 | trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, |
| 2524 | ses->Suid, CREATE_NOT_FILE, |
| 2525 | FILE_WRITE_ATTRIBUTES); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2526 | |
| 2527 | SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId); |
| 2528 | |
| 2529 | /* Eventually save off posix specific response info and timestaps */ |
| 2530 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2531 | err_free_rsp_buf: |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2532 | free_rsp_buf(resp_buftype, rsp); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2533 | kfree(pc_buf); |
| 2534 | err_free_req: |
| 2535 | cifs_small_buf_release(req); |
| 2536 | err_free_path: |
| 2537 | kfree(utf16_path); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2538 | return rc; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2539 | } |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2540 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2541 | int |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2542 | SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock, |
| 2543 | struct cifs_open_parms *oparms, __le16 *path) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2544 | { |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2545 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2546 | struct smb2_create_req *req; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2547 | unsigned int n_iov = 2; |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2548 | __u32 file_attributes = 0; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2549 | int copy_size; |
| 2550 | int uni_path_len; |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2551 | unsigned int total_len; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2552 | struct kvec *iov = rqst->rq_iov; |
| 2553 | __le16 *copy_path; |
| 2554 | int rc; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2555 | |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2556 | rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2557 | if (rc) |
| 2558 | return rc; |
| 2559 | |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2560 | iov[0].iov_base = (char *)req; |
| 2561 | /* -1 since last byte is buf[0] which is sent below (path) */ |
| 2562 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 2563 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2564 | if (oparms->create_options & CREATE_OPTION_READONLY) |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2565 | file_attributes |= ATTR_READONLY; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 2566 | if (oparms->create_options & CREATE_OPTION_SPECIAL) |
| 2567 | file_attributes |= ATTR_SYSTEM; |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2568 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2569 | req->ImpersonationLevel = IL_IMPERSONATION; |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2570 | req->DesiredAccess = cpu_to_le32(oparms->desired_access); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2571 | /* File attributes ignored on open (used in create though) */ |
| 2572 | req->FileAttributes = cpu_to_le32(file_attributes); |
| 2573 | req->ShareAccess = FILE_SHARE_ALL_LE; |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2574 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2575 | req->CreateDisposition = cpu_to_le32(oparms->disposition); |
| 2576 | req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK); |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2577 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2578 | |
| 2579 | /* [MS-SMB2] 2.2.13 NameOffset: |
| 2580 | * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of |
| 2581 | * the SMB2 header, the file name includes a prefix that will |
| 2582 | * be processed during DFS name normalization as specified in |
| 2583 | * section 3.3.5.9. Otherwise, the file name is relative to |
| 2584 | * the share that is identified by the TreeId in the SMB2 |
| 2585 | * header. |
| 2586 | */ |
| 2587 | if (tcon->share_flags & SHI1005_FLAGS_DFS) { |
| 2588 | int name_len; |
| 2589 | |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2590 | req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2591 | rc = alloc_path_with_tree_prefix(©_path, ©_size, |
| 2592 | &name_len, |
| 2593 | tcon->treeName, path); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2594 | if (rc) |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2595 | return rc; |
| 2596 | req->NameLength = cpu_to_le16(name_len * 2); |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2597 | uni_path_len = copy_size; |
| 2598 | path = copy_path; |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2599 | } else { |
| 2600 | uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2; |
| 2601 | /* MUST set path len (NameLength) to 0 opening root of share */ |
| 2602 | req->NameLength = cpu_to_le16(uni_path_len - 2); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2603 | copy_size = uni_path_len; |
| 2604 | if (copy_size % 8 != 0) |
| 2605 | copy_size = roundup(copy_size, 8); |
| 2606 | copy_path = kzalloc(copy_size, GFP_KERNEL); |
| 2607 | if (!copy_path) |
| 2608 | return -ENOMEM; |
| 2609 | memcpy((char *)copy_path, (const char *)path, |
| 2610 | uni_path_len); |
| 2611 | uni_path_len = copy_size; |
| 2612 | path = copy_path; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2613 | } |
| 2614 | |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2615 | iov[1].iov_len = uni_path_len; |
| 2616 | iov[1].iov_base = path; |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2617 | |
Steve French | 3e7a02d | 2019-09-11 21:46:20 -0500 | [diff] [blame] | 2618 | if ((!server->oplocks) || (tcon->no_lease)) |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2619 | *oplock = SMB2_OPLOCK_LEVEL_NONE; |
| 2620 | |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2621 | if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) || |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2622 | *oplock == SMB2_OPLOCK_LEVEL_NONE) |
| 2623 | req->RequestedOplockLevel = *oplock; |
Steve French | f801568 | 2018-08-31 15:12:10 -0500 | [diff] [blame] | 2624 | else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) && |
| 2625 | (oparms->create_options & CREATE_NOT_FILE)) |
| 2626 | req->RequestedOplockLevel = *oplock; /* no srv lease support */ |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2627 | else { |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2628 | rc = add_lease_context(server, iov, &n_iov, |
| 2629 | oparms->fid->lease_key, oplock); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2630 | if (rc) |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2631 | return rc; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2632 | } |
| 2633 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2634 | if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) { |
| 2635 | /* need to set Next field of lease context if we request it */ |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2636 | if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) { |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2637 | struct create_context *ccontext = |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2638 | (struct create_context *)iov[n_iov-1].iov_base; |
Steve French | 1c46943 | 2013-07-10 12:50:57 -0500 | [diff] [blame] | 2639 | ccontext->Next = |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2640 | cpu_to_le32(server->vals->create_lease_size); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2641 | } |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2642 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2643 | rc = add_durable_context(iov, &n_iov, oparms, |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2644 | tcon->use_persistent); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2645 | if (rc) |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2646 | return rc; |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2647 | } |
| 2648 | |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2649 | if (tcon->posix_extensions) { |
| 2650 | if (n_iov > 2) { |
| 2651 | struct create_context *ccontext = |
| 2652 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2653 | ccontext->Next = |
| 2654 | cpu_to_le32(iov[n_iov-1].iov_len); |
| 2655 | } |
| 2656 | |
| 2657 | rc = add_posix_context(iov, &n_iov, oparms->mode); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2658 | if (rc) |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2659 | return rc; |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2660 | } |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2661 | |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2662 | if (tcon->snapshot_time) { |
| 2663 | cifs_dbg(FYI, "adding snapshot context\n"); |
| 2664 | if (n_iov > 2) { |
| 2665 | struct create_context *ccontext = |
| 2666 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2667 | ccontext->Next = |
| 2668 | cpu_to_le32(iov[n_iov-1].iov_len); |
| 2669 | } |
| 2670 | |
| 2671 | rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time); |
| 2672 | if (rc) |
| 2673 | return rc; |
| 2674 | } |
| 2675 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2676 | if ((oparms->disposition != FILE_OPEN) && |
Steve French | 231e2a0 | 2019-12-07 17:38:22 -0600 | [diff] [blame] | 2677 | (oparms->cifs_sb) && |
| 2678 | (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) && |
Steve French | d0959b0 | 2019-10-05 10:53:58 -0500 | [diff] [blame] | 2679 | (oparms->mode != ACL_NO_MODE)) { |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2680 | if (n_iov > 2) { |
| 2681 | struct create_context *ccontext = |
| 2682 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2683 | ccontext->Next = |
| 2684 | cpu_to_le32(iov[n_iov-1].iov_len); |
| 2685 | } |
| 2686 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2687 | cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode); |
| 2688 | rc = add_sd_context(iov, &n_iov, oparms->mode); |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2689 | if (rc) |
| 2690 | return rc; |
| 2691 | } |
| 2692 | |
Steve French | ff2a09e | 2019-07-06 14:41:38 -0500 | [diff] [blame] | 2693 | if (n_iov > 2) { |
| 2694 | struct create_context *ccontext = |
| 2695 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2696 | ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len); |
| 2697 | } |
| 2698 | add_query_id_context(iov, &n_iov); |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2699 | |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2700 | rqst->rq_nvec = n_iov; |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
| 2704 | /* rq_iov[0] is the request and is released by cifs_small_buf_release(). |
| 2705 | * All other vectors are freed by kfree(). |
| 2706 | */ |
| 2707 | void |
| 2708 | SMB2_open_free(struct smb_rqst *rqst) |
| 2709 | { |
| 2710 | int i; |
| 2711 | |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 2712 | if (rqst && rqst->rq_iov) { |
| 2713 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); |
| 2714 | for (i = 1; i < rqst->rq_nvec; i++) |
| 2715 | if (rqst->rq_iov[i].iov_base != smb2_padding) |
| 2716 | kfree(rqst->rq_iov[i].iov_base); |
| 2717 | } |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | int |
| 2721 | SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, |
| 2722 | __u8 *oplock, struct smb2_file_all_info *buf, |
| 2723 | struct kvec *err_iov, int *buftype) |
| 2724 | { |
| 2725 | struct smb_rqst rqst; |
| 2726 | struct smb2_create_rsp *rsp = NULL; |
| 2727 | struct TCP_Server_Info *server; |
| 2728 | struct cifs_tcon *tcon = oparms->tcon; |
| 2729 | struct cifs_ses *ses = tcon->ses; |
Ronnie Sahlberg | 4d8dfaf | 2018-08-21 11:49:21 +1000 | [diff] [blame] | 2730 | struct kvec iov[SMB2_CREATE_IOV_SIZE]; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2731 | struct kvec rsp_iov = {NULL, 0}; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 2732 | int resp_buftype = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2733 | int rc = 0; |
| 2734 | int flags = 0; |
| 2735 | |
| 2736 | cifs_dbg(FYI, "create/open\n"); |
| 2737 | if (ses && (ses->server)) |
| 2738 | server = ses->server; |
| 2739 | else |
| 2740 | return -EIO; |
| 2741 | |
| 2742 | if (smb3_encryption_required(tcon)) |
| 2743 | flags |= CIFS_TRANSFORM_REQ; |
| 2744 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2745 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2746 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2747 | rqst.rq_iov = iov; |
Ronnie Sahlberg | 4d8dfaf | 2018-08-21 11:49:21 +1000 | [diff] [blame] | 2748 | rqst.rq_nvec = SMB2_CREATE_IOV_SIZE; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2749 | |
| 2750 | rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path); |
| 2751 | if (rc) |
| 2752 | goto creat_exit; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2753 | |
Steve French | efe2e9f | 2019-02-26 19:08:12 -0600 | [diff] [blame] | 2754 | trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, |
| 2755 | oparms->create_options, oparms->desired_access); |
| 2756 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2757 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2758 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2759 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2760 | |
| 2761 | if (rc != 0) { |
| 2762 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); |
Ronnie Sahlberg | 91cb74f | 2018-04-13 09:03:19 +1000 | [diff] [blame] | 2763 | if (err_iov && rsp) { |
| 2764 | *err_iov = rsp_iov; |
Ronnie Sahlberg | 9d874c3 | 2018-06-08 13:21:18 +1000 | [diff] [blame] | 2765 | *buftype = resp_buftype; |
Ronnie Sahlberg | 91cb74f | 2018-04-13 09:03:19 +1000 | [diff] [blame] | 2766 | resp_buftype = CIFS_NO_BUFFER; |
| 2767 | rsp = NULL; |
| 2768 | } |
Steve French | 28d5936 | 2018-05-30 21:42:34 -0500 | [diff] [blame] | 2769 | trace_smb3_open_err(xid, tcon->tid, ses->Suid, |
| 2770 | oparms->create_options, oparms->desired_access, rc); |
Steve French | 7dcc82c | 2019-09-11 00:07:36 -0500 | [diff] [blame] | 2771 | if (rc == -EREMCHG) { |
| 2772 | printk_once(KERN_WARNING "server share %s deleted\n", |
| 2773 | tcon->treeName); |
| 2774 | tcon->need_reconnect = true; |
| 2775 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2776 | goto creat_exit; |
Steve French | 28d5936 | 2018-05-30 21:42:34 -0500 | [diff] [blame] | 2777 | } else |
| 2778 | trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, |
| 2779 | ses->Suid, oparms->create_options, |
| 2780 | oparms->desired_access); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2781 | |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 2782 | atomic_inc(&tcon->num_remote_opens); |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2783 | oparms->fid->persistent_fid = rsp->PersistentFileId; |
| 2784 | oparms->fid->volatile_fid = rsp->VolatileFileId; |
Aurelien Aptel | 86f740f | 2020-02-21 11:19:06 +0100 | [diff] [blame] | 2785 | oparms->fid->access = oparms->desired_access; |
Steve French | dfe33f9 | 2018-10-30 19:50:31 -0500 | [diff] [blame] | 2786 | #ifdef CONFIG_CIFS_DEBUG2 |
| 2787 | oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId); |
| 2788 | #endif /* CIFS_DEBUG2 */ |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 2789 | |
| 2790 | if (buf) { |
| 2791 | memcpy(buf, &rsp->CreationTime, 32); |
| 2792 | buf->AllocationSize = rsp->AllocationSize; |
| 2793 | buf->EndOfFile = rsp->EndofFile; |
| 2794 | buf->Attributes = rsp->FileAttributes; |
| 2795 | buf->NumberOfLinks = cpu_to_le32(1); |
| 2796 | buf->DeletePending = 0; |
| 2797 | } |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 2798 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2799 | |
| 2800 | smb2_parse_contexts(server, rsp, &oparms->fid->epoch, |
| 2801 | oparms->fid->lease_key, oplock, buf); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2802 | creat_exit: |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2803 | SMB2_open_free(&rqst); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2804 | free_rsp_buf(resp_buftype, rsp); |
| 2805 | return rc; |
| 2806 | } |
| 2807 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2808 | int |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2809 | SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, |
| 2810 | u64 persistent_fid, u64 volatile_fid, u32 opcode, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2811 | bool is_fsctl, char *in_data, u32 indatalen, |
| 2812 | __u32 max_response_size) |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2813 | { |
| 2814 | struct smb2_ioctl_req *req; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2815 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2816 | unsigned int total_len; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2817 | int rc; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2818 | char *in_data_buf; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2819 | |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 2820 | rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2821 | if (rc) |
| 2822 | return rc; |
| 2823 | |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2824 | if (indatalen) { |
| 2825 | /* |
| 2826 | * indatalen is usually small at a couple of bytes max, so |
| 2827 | * just allocate through generic pool |
| 2828 | */ |
YueHaibing | d81f097 | 2019-06-01 03:31:10 +0000 | [diff] [blame] | 2829 | in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS); |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2830 | if (!in_data_buf) { |
| 2831 | cifs_small_buf_release(req); |
| 2832 | return -ENOMEM; |
| 2833 | } |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2836 | req->CtlCode = cpu_to_le32(opcode); |
| 2837 | req->PersistentFileId = persistent_fid; |
| 2838 | req->VolatileFileId = volatile_fid; |
| 2839 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2840 | iov[0].iov_base = (char *)req; |
| 2841 | /* |
| 2842 | * If no input data, the size of ioctl struct in |
| 2843 | * protocol spec still includes a 1 byte data buffer, |
| 2844 | * but if input data passed to ioctl, we do not |
| 2845 | * want to double count this, so we do not send |
| 2846 | * the dummy one byte of data in iovec[0] if sending |
| 2847 | * input data (in iovec[1]). |
| 2848 | */ |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2849 | if (indatalen) { |
| 2850 | req->InputCount = cpu_to_le32(indatalen); |
| 2851 | /* do not set InputOffset if no input data */ |
| 2852 | req->InputOffset = |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2853 | cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer)); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2854 | rqst->rq_nvec = 2; |
| 2855 | iov[0].iov_len = total_len - 1; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2856 | iov[1].iov_base = in_data_buf; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2857 | iov[1].iov_len = indatalen; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2858 | } else { |
| 2859 | rqst->rq_nvec = 1; |
| 2860 | iov[0].iov_len = total_len; |
| 2861 | } |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2862 | |
| 2863 | req->OutputOffset = 0; |
| 2864 | req->OutputCount = 0; /* MBZ */ |
| 2865 | |
| 2866 | /* |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2867 | * In most cases max_response_size is set to 16K (CIFSMaxBufSize) |
| 2868 | * We Could increase default MaxOutputResponse, but that could require |
| 2869 | * more credits. Windows typically sets this smaller, but for some |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2870 | * ioctls it may be useful to allow server to send more. No point |
| 2871 | * 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] | 2872 | * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want |
| 2873 | * to increase this limit up in the future. |
| 2874 | * Note that for snapshot queries that servers like Azure expect that |
| 2875 | * the first query be minimal size (and just used to get the number/size |
| 2876 | * of previous versions) so response size must be specified as EXACTLY |
| 2877 | * sizeof(struct snapshot_array) which is 16 when rounded up to multiple |
| 2878 | * of eight bytes. Currently that is the only case where we set max |
| 2879 | * response size smaller. |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2880 | */ |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2881 | req->MaxOutputResponse = cpu_to_le32(max_response_size); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2882 | |
| 2883 | if (is_fsctl) |
| 2884 | req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL); |
| 2885 | else |
| 2886 | req->Flags = 0; |
| 2887 | |
Steve French | 4587eee | 2017-10-25 15:58:31 -0500 | [diff] [blame] | 2888 | /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */ |
| 2889 | if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2890 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2891 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2892 | return 0; |
| 2893 | } |
| 2894 | |
| 2895 | void |
| 2896 | SMB2_ioctl_free(struct smb_rqst *rqst) |
| 2897 | { |
Murphy Zhou | 6457c20 | 2019-05-23 12:12:43 +0800 | [diff] [blame] | 2898 | int i; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2899 | if (rqst && rqst->rq_iov) { |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2900 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Murphy Zhou | 6457c20 | 2019-05-23 12:12:43 +0800 | [diff] [blame] | 2901 | for (i = 1; i < rqst->rq_nvec; i++) |
| 2902 | if (rqst->rq_iov[i].iov_base != smb2_padding) |
| 2903 | kfree(rqst->rq_iov[i].iov_base); |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2904 | } |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2905 | } |
| 2906 | |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2907 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2908 | /* |
| 2909 | * SMB2 IOCTL is used for both IOCTLs and FSCTLs |
| 2910 | */ |
| 2911 | int |
| 2912 | SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, |
| 2913 | u64 volatile_fid, u32 opcode, bool is_fsctl, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2914 | char *in_data, u32 indatalen, u32 max_out_data_len, |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2915 | char **out_data, u32 *plen /* returned data len */) |
| 2916 | { |
| 2917 | struct smb_rqst rqst; |
| 2918 | struct smb2_ioctl_rsp *rsp = NULL; |
| 2919 | struct cifs_ses *ses; |
Ronnie Sahlberg | 72c419d | 2019-03-13 14:37:49 +1000 | [diff] [blame] | 2920 | struct kvec iov[SMB2_IOCTL_IOV_SIZE]; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2921 | struct kvec rsp_iov = {NULL, 0}; |
| 2922 | int resp_buftype = CIFS_NO_BUFFER; |
| 2923 | int rc = 0; |
| 2924 | int flags = 0; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2925 | struct TCP_Server_Info *server; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2926 | |
| 2927 | cifs_dbg(FYI, "SMB2 IOCTL\n"); |
| 2928 | |
| 2929 | if (out_data != NULL) |
| 2930 | *out_data = NULL; |
| 2931 | |
| 2932 | /* zero out returned data len, in case of error */ |
| 2933 | if (plen) |
| 2934 | *plen = 0; |
| 2935 | |
| 2936 | if (tcon) |
| 2937 | ses = tcon->ses; |
| 2938 | else |
| 2939 | return -EIO; |
| 2940 | |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 2941 | if (!ses) |
| 2942 | return -EIO; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2943 | server = ses->server; |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 2944 | if (!server) |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2945 | return -EIO; |
| 2946 | |
| 2947 | if (smb3_encryption_required(tcon)) |
| 2948 | flags |= CIFS_TRANSFORM_REQ; |
| 2949 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2950 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2951 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2952 | rqst.rq_iov = iov; |
Ronnie Sahlberg | 72c419d | 2019-03-13 14:37:49 +1000 | [diff] [blame] | 2953 | rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2954 | |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2955 | rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode, |
| 2956 | is_fsctl, in_data, indatalen, max_out_data_len); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2957 | if (rc) |
| 2958 | goto ioctl_exit; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2959 | |
| 2960 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2961 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2962 | rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2963 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 2964 | if (rc != 0) |
| 2965 | trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid, |
| 2966 | ses->Suid, 0, opcode, rc); |
| 2967 | |
Ronnie Sahlberg | 2f3ebab | 2019-04-25 16:45:29 +1000 | [diff] [blame] | 2968 | if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) { |
Steve French | 8e35310 | 2015-03-26 19:47:02 -0500 | [diff] [blame] | 2969 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2970 | goto ioctl_exit; |
Steve French | 9bf0c9c | 2013-11-16 18:05:28 -0600 | [diff] [blame] | 2971 | } else if (rc == -EINVAL) { |
| 2972 | if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) && |
| 2973 | (opcode != FSCTL_SRV_COPYCHUNK)) { |
Steve French | 8e35310 | 2015-03-26 19:47:02 -0500 | [diff] [blame] | 2974 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
Steve French | 9bf0c9c | 2013-11-16 18:05:28 -0600 | [diff] [blame] | 2975 | goto ioctl_exit; |
| 2976 | } |
Ronnie Sahlberg | 2f3ebab | 2019-04-25 16:45:29 +1000 | [diff] [blame] | 2977 | } else if (rc == -E2BIG) { |
| 2978 | if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) { |
| 2979 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
| 2980 | goto ioctl_exit; |
| 2981 | } |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | /* check if caller wants to look at return data or just return rc */ |
| 2985 | if ((plen == NULL) || (out_data == NULL)) |
| 2986 | goto ioctl_exit; |
| 2987 | |
| 2988 | *plen = le32_to_cpu(rsp->OutputCount); |
| 2989 | |
| 2990 | /* We check for obvious errors in the output buffer length and offset */ |
| 2991 | if (*plen == 0) |
| 2992 | goto ioctl_exit; /* server returned no data */ |
Dan Carpenter | 2d204ee | 2018-09-10 14:12:07 +0300 | [diff] [blame] | 2993 | else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 2994 | cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2995 | *plen = 0; |
| 2996 | rc = -EIO; |
| 2997 | goto ioctl_exit; |
| 2998 | } |
| 2999 | |
Dan Carpenter | 2d204ee | 2018-09-10 14:12:07 +0300 | [diff] [blame] | 3000 | if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3001 | 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] | 3002 | le32_to_cpu(rsp->OutputOffset)); |
| 3003 | *plen = 0; |
| 3004 | rc = -EIO; |
| 3005 | goto ioctl_exit; |
| 3006 | } |
| 3007 | |
YueHaibing | d034fee | 2018-09-10 01:33:06 +0000 | [diff] [blame] | 3008 | *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset), |
| 3009 | *plen, GFP_KERNEL); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3010 | if (*out_data == NULL) { |
| 3011 | rc = -ENOMEM; |
| 3012 | goto ioctl_exit; |
| 3013 | } |
| 3014 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3015 | ioctl_exit: |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3016 | SMB2_ioctl_free(&rqst); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3017 | free_rsp_buf(resp_buftype, rsp); |
| 3018 | return rc; |
| 3019 | } |
| 3020 | |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3021 | /* |
| 3022 | * Individual callers to ioctl worker function follow |
| 3023 | */ |
| 3024 | |
| 3025 | int |
| 3026 | SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon, |
| 3027 | u64 persistent_fid, u64 volatile_fid) |
| 3028 | { |
| 3029 | int rc; |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3030 | struct compress_ioctl fsctl_input; |
| 3031 | char *ret_data = NULL; |
| 3032 | |
| 3033 | fsctl_input.CompressionState = |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3034 | cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3035 | |
| 3036 | rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid, |
| 3037 | FSCTL_SET_COMPRESSION, true /* is_fsctl */, |
| 3038 | (char *)&fsctl_input /* data input */, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3039 | 2 /* in data len */, CIFSMaxBufSize /* max out data */, |
| 3040 | &ret_data /* out data */, NULL); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3041 | |
| 3042 | cifs_dbg(FYI, "set compression rc %d\n", rc); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3043 | |
| 3044 | return rc; |
| 3045 | } |
| 3046 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3047 | int |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3048 | SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3049 | u64 persistent_fid, u64 volatile_fid, bool query_attrs) |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3050 | { |
| 3051 | struct smb2_close_req *req; |
| 3052 | struct kvec *iov = rqst->rq_iov; |
| 3053 | unsigned int total_len; |
| 3054 | int rc; |
| 3055 | |
| 3056 | rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len); |
| 3057 | if (rc) |
| 3058 | return rc; |
| 3059 | |
| 3060 | req->PersistentFileId = persistent_fid; |
| 3061 | req->VolatileFileId = volatile_fid; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3062 | if (query_attrs) |
| 3063 | req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 3064 | else |
| 3065 | req->Flags = 0; |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3066 | iov[0].iov_base = (char *)req; |
| 3067 | iov[0].iov_len = total_len; |
| 3068 | |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
| 3072 | void |
| 3073 | SMB2_close_free(struct smb_rqst *rqst) |
| 3074 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 3075 | if (rqst && rqst->rq_iov) |
| 3076 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | int |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3080 | __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, |
| 3081 | u64 persistent_fid, u64 volatile_fid, |
| 3082 | struct smb2_file_network_open_info *pbuf) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3083 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3084 | struct smb_rqst rqst; |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3085 | struct smb2_close_rsp *rsp = NULL; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3086 | struct cifs_ses *ses = tcon->ses; |
| 3087 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3088 | struct kvec rsp_iov; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 3089 | int resp_buftype = CIFS_NO_BUFFER; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3090 | int rc = 0; |
Steve French | 9e8fae2 | 2019-12-02 17:55:41 -0600 | [diff] [blame] | 3091 | int flags = 0; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3092 | bool query_attrs = false; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3093 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3094 | cifs_dbg(FYI, "Close\n"); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3095 | |
Christos Gkekas | 68a6afa | 2017-07-09 11:45:04 +0100 | [diff] [blame] | 3096 | if (!ses || !(ses->server)) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3097 | return -EIO; |
| 3098 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 3099 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 3100 | flags |= CIFS_TRANSFORM_REQ; |
| 3101 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3102 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3103 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3104 | rqst.rq_iov = iov; |
| 3105 | rqst.rq_nvec = 1; |
| 3106 | |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3107 | /* check if need to ask server to return timestamps in close response */ |
| 3108 | if (pbuf) |
| 3109 | query_attrs = true; |
| 3110 | |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3111 | trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid); |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3112 | rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid, |
| 3113 | query_attrs); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3114 | if (rc) |
| 3115 | goto close_exit; |
| 3116 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3117 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3118 | rsp = (struct smb2_close_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3119 | |
| 3120 | if (rc != 0) { |
Namjae Jeon | d4a029d | 2014-08-20 19:39:59 +0900 | [diff] [blame] | 3121 | cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3122 | trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3123 | rc); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3124 | goto close_exit; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3125 | } else { |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3126 | trace_smb3_close_done(xid, persistent_fid, tcon->tid, |
| 3127 | ses->Suid); |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3128 | /* |
| 3129 | * Note that have to subtract 4 since struct network_open_info |
| 3130 | * has a final 4 byte pad that close response does not have |
| 3131 | */ |
| 3132 | if (pbuf) |
| 3133 | memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4); |
| 3134 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3135 | |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 3136 | atomic_dec(&tcon->num_remote_opens); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3137 | close_exit: |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3138 | SMB2_close_free(&rqst); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3139 | free_rsp_buf(resp_buftype, rsp); |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3140 | |
| 3141 | /* retry close in a worker thread if this one is interrupted */ |
| 3142 | if (rc == -EINTR) { |
Steve French | 9e8fae2 | 2019-12-02 17:55:41 -0600 | [diff] [blame] | 3143 | int tmp_rc; |
| 3144 | |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3145 | tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid, |
| 3146 | volatile_fid); |
| 3147 | if (tmp_rc) |
| 3148 | cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n", |
| 3149 | persistent_fid, tmp_rc); |
| 3150 | } |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3151 | return rc; |
Ronnie Sahlberg | 97ca176 | 2018-04-26 08:50:49 -0600 | [diff] [blame] | 3152 | } |
| 3153 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3154 | int |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3155 | SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, |
| 3156 | u64 persistent_fid, u64 volatile_fid) |
| 3157 | { |
| 3158 | return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL); |
| 3159 | } |
| 3160 | |
| 3161 | int |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3162 | smb2_validate_iov(unsigned int offset, unsigned int buffer_length, |
| 3163 | struct kvec *iov, unsigned int min_buf_size) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3164 | { |
Ronnie Sahlberg | c1596ff | 2018-04-09 18:06:30 +1000 | [diff] [blame] | 3165 | unsigned int smb_len = iov->iov_len; |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 3166 | char *end_of_smb = smb_len + (char *)iov->iov_base; |
| 3167 | char *begin_of_buf = offset + (char *)iov->iov_base; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3168 | char *end_of_buf = begin_of_buf + buffer_length; |
| 3169 | |
| 3170 | |
| 3171 | if (buffer_length < min_buf_size) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3172 | cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n", |
| 3173 | buffer_length, min_buf_size); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3174 | return -EINVAL; |
| 3175 | } |
| 3176 | |
| 3177 | /* check if beyond RFC1001 maximum length */ |
| 3178 | if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3179 | cifs_dbg(VFS, "buffer length %d or smb length %d too large\n", |
| 3180 | buffer_length, smb_len); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3181 | return -EINVAL; |
| 3182 | } |
| 3183 | |
| 3184 | if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3185 | cifs_dbg(VFS, "illegal server response, bad offset to data\n"); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3186 | return -EINVAL; |
| 3187 | } |
| 3188 | |
| 3189 | return 0; |
| 3190 | } |
| 3191 | |
| 3192 | /* |
| 3193 | * If SMB buffer fields are valid, copy into temporary buffer to hold result. |
| 3194 | * Caller must free buffer. |
| 3195 | */ |
Ronnie Sahlberg | c5a5f38 | 2018-09-03 13:33:41 +1000 | [diff] [blame] | 3196 | int |
| 3197 | smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length, |
| 3198 | struct kvec *iov, unsigned int minbufsize, |
| 3199 | char *data) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3200 | { |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 3201 | char *begin_of_buf = offset + (char *)iov->iov_base; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3202 | int rc; |
| 3203 | |
| 3204 | if (!data) |
| 3205 | return -EINVAL; |
| 3206 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3207 | rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3208 | if (rc) |
| 3209 | return rc; |
| 3210 | |
| 3211 | memcpy(data, begin_of_buf, buffer_length); |
| 3212 | |
| 3213 | return 0; |
| 3214 | } |
| 3215 | |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3216 | int |
| 3217 | SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, |
| 3218 | u64 persistent_fid, u64 volatile_fid, |
| 3219 | u8 info_class, u8 info_type, u32 additional_info, |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3220 | size_t output_len, size_t input_len, void *input) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3221 | { |
| 3222 | struct smb2_query_info_req *req; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3223 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 3224 | unsigned int total_len; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3225 | int rc; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3226 | |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 3227 | rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req, |
| 3228 | &total_len); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3229 | if (rc) |
| 3230 | return rc; |
| 3231 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3232 | req->InfoType = info_type; |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3233 | req->FileInfoClass = info_class; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3234 | req->PersistentFileId = persistent_fid; |
| 3235 | req->VolatileFileId = volatile_fid; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3236 | req->AdditionalInformation = cpu_to_le32(additional_info); |
Aurelien Aptel | 48923d2 | 2017-10-17 14:47:17 +0200 | [diff] [blame] | 3237 | |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3238 | req->OutputBufferLength = cpu_to_le32(output_len); |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3239 | if (input_len) { |
| 3240 | req->InputBufferLength = cpu_to_le32(input_len); |
| 3241 | /* total_len for smb query request never close to le16 max */ |
| 3242 | req->InputBufferOffset = cpu_to_le16(total_len - 1); |
| 3243 | memcpy(req->Buffer, input, input_len); |
| 3244 | } |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3245 | |
| 3246 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 3247 | /* 1 for Buffer */ |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3248 | iov[0].iov_len = total_len - 1 + input_len; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3249 | return 0; |
| 3250 | } |
| 3251 | |
| 3252 | void |
| 3253 | SMB2_query_info_free(struct smb_rqst *rqst) |
| 3254 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 3255 | if (rqst && rqst->rq_iov) |
| 3256 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | static int |
| 3260 | query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3261 | u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type, |
| 3262 | u32 additional_info, size_t output_len, size_t min_len, void **data, |
| 3263 | u32 *dlen) |
| 3264 | { |
| 3265 | struct smb_rqst rqst; |
| 3266 | struct smb2_query_info_rsp *rsp = NULL; |
| 3267 | struct kvec iov[1]; |
| 3268 | struct kvec rsp_iov; |
| 3269 | int rc = 0; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 3270 | int resp_buftype = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3271 | struct cifs_ses *ses = tcon->ses; |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3272 | struct TCP_Server_Info *server; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3273 | int flags = 0; |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3274 | bool allocated = false; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3275 | |
| 3276 | cifs_dbg(FYI, "Query Info\n"); |
| 3277 | |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3278 | if (!ses) |
| 3279 | return -EIO; |
| 3280 | server = ses->server; |
| 3281 | if (!server) |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3282 | return -EIO; |
| 3283 | |
| 3284 | if (smb3_encryption_required(tcon)) |
| 3285 | flags |= CIFS_TRANSFORM_REQ; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3286 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3287 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3288 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3289 | rqst.rq_iov = iov; |
| 3290 | rqst.rq_nvec = 1; |
| 3291 | |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3292 | rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid, |
| 3293 | info_class, info_type, additional_info, |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3294 | output_len, 0, NULL); |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3295 | if (rc) |
| 3296 | goto qinf_exit; |
| 3297 | |
Steve French | d42043a | 2019-02-26 21:58:30 -0600 | [diff] [blame] | 3298 | trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid, |
| 3299 | ses->Suid, info_class, (__u32)info_type); |
| 3300 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3301 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3302 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 3303 | |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3304 | if (rc) { |
| 3305 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3306 | trace_smb3_query_info_err(xid, persistent_fid, tcon->tid, |
| 3307 | ses->Suid, info_class, (__u32)info_type, rc); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3308 | goto qinf_exit; |
| 3309 | } |
| 3310 | |
Steve French | d42043a | 2019-02-26 21:58:30 -0600 | [diff] [blame] | 3311 | trace_smb3_query_info_done(xid, persistent_fid, tcon->tid, |
| 3312 | ses->Suid, info_class, (__u32)info_type); |
| 3313 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3314 | if (dlen) { |
| 3315 | *dlen = le32_to_cpu(rsp->OutputBufferLength); |
| 3316 | if (!*data) { |
| 3317 | *data = kmalloc(*dlen, GFP_KERNEL); |
| 3318 | if (!*data) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3319 | cifs_tcon_dbg(VFS, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3320 | "Error %d allocating memory for acl\n", |
| 3321 | rc); |
| 3322 | *dlen = 0; |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3323 | rc = -ENOMEM; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3324 | goto qinf_exit; |
| 3325 | } |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3326 | allocated = true; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | |
Ronnie Sahlberg | c5a5f38 | 2018-09-03 13:33:41 +1000 | [diff] [blame] | 3330 | rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 3331 | le32_to_cpu(rsp->OutputBufferLength), |
| 3332 | &rsp_iov, min_len, *data); |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3333 | if (rc && allocated) { |
| 3334 | kfree(*data); |
| 3335 | *data = NULL; |
| 3336 | *dlen = 0; |
| 3337 | } |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3338 | |
| 3339 | qinf_exit: |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3340 | SMB2_query_info_free(&rqst); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3341 | free_rsp_buf(resp_buftype, rsp); |
| 3342 | return rc; |
| 3343 | } |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3344 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3345 | int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3346 | u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data) |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3347 | { |
| 3348 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3349 | FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0, |
Pavel Shilovsky | 1bbe499 | 2014-08-22 13:32:11 +0400 | [diff] [blame] | 3350 | sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3351 | sizeof(struct smb2_file_all_info), (void **)&data, |
| 3352 | NULL); |
| 3353 | } |
| 3354 | |
| 3355 | int |
| 3356 | SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon, |
| 3357 | u64 persistent_fid, u64 volatile_fid, |
| 3358 | void **data, u32 *plen) |
| 3359 | { |
| 3360 | __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO; |
| 3361 | *plen = 0; |
| 3362 | |
| 3363 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
| 3364 | 0, SMB2_O_INFO_SECURITY, additional_info, |
Shirish Pargaonkar | ee25c6d | 2018-06-04 06:46:22 -0500 | [diff] [blame] | 3365 | SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | int |
| 3369 | SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon, |
| 3370 | u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid) |
| 3371 | { |
| 3372 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3373 | FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0, |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3374 | sizeof(struct smb2_file_internal_info), |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3375 | sizeof(struct smb2_file_internal_info), |
| 3376 | (void **)&uniqueid, NULL); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3377 | } |
| 3378 | |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3379 | /* |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3380 | * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory |
| 3381 | * See MS-SMB2 2.2.35 and 2.2.36 |
| 3382 | */ |
| 3383 | |
zhengbin | 388962e | 2019-09-23 15:06:18 +0800 | [diff] [blame] | 3384 | static int |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3385 | SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst, |
| 3386 | struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid, |
| 3387 | u32 completion_filter, bool watch_tree) |
| 3388 | { |
| 3389 | struct smb2_change_notify_req *req; |
| 3390 | struct kvec *iov = rqst->rq_iov; |
| 3391 | unsigned int total_len; |
| 3392 | int rc; |
| 3393 | |
| 3394 | rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len); |
| 3395 | if (rc) |
| 3396 | return rc; |
| 3397 | |
| 3398 | req->PersistentFileId = persistent_fid; |
| 3399 | req->VolatileFileId = volatile_fid; |
Steve French | d26c2dd | 2020-02-06 06:00:14 -0600 | [diff] [blame] | 3400 | /* See note 354 of MS-SMB2, 64K max */ |
Steve French | 52870d5 | 2019-10-01 21:25:46 -0500 | [diff] [blame] | 3401 | req->OutputBufferLength = |
| 3402 | cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3403 | req->CompletionFilter = cpu_to_le32(completion_filter); |
| 3404 | if (watch_tree) |
| 3405 | req->Flags = cpu_to_le16(SMB2_WATCH_TREE); |
| 3406 | else |
| 3407 | req->Flags = 0; |
| 3408 | |
| 3409 | iov[0].iov_base = (char *)req; |
| 3410 | iov[0].iov_len = total_len; |
| 3411 | |
| 3412 | return 0; |
| 3413 | } |
| 3414 | |
| 3415 | int |
| 3416 | SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon, |
| 3417 | u64 persistent_fid, u64 volatile_fid, bool watch_tree, |
| 3418 | u32 completion_filter) |
| 3419 | { |
| 3420 | struct cifs_ses *ses = tcon->ses; |
| 3421 | struct smb_rqst rqst; |
| 3422 | struct kvec iov[1]; |
| 3423 | struct kvec rsp_iov = {NULL, 0}; |
| 3424 | int resp_buftype = CIFS_NO_BUFFER; |
| 3425 | int flags = 0; |
| 3426 | int rc = 0; |
| 3427 | |
| 3428 | cifs_dbg(FYI, "change notify\n"); |
| 3429 | if (!ses || !(ses->server)) |
| 3430 | return -EIO; |
| 3431 | |
| 3432 | if (smb3_encryption_required(tcon)) |
| 3433 | flags |= CIFS_TRANSFORM_REQ; |
| 3434 | |
| 3435 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 3436 | memset(&iov, 0, sizeof(iov)); |
| 3437 | rqst.rq_iov = iov; |
| 3438 | rqst.rq_nvec = 1; |
| 3439 | |
| 3440 | rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid, |
| 3441 | completion_filter, watch_tree); |
| 3442 | if (rc) |
| 3443 | goto cnotify_exit; |
| 3444 | |
| 3445 | trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3446 | (u8)watch_tree, completion_filter); |
| 3447 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
| 3448 | |
| 3449 | if (rc != 0) { |
| 3450 | cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE); |
| 3451 | trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3452 | (u8)watch_tree, completion_filter, rc); |
| 3453 | } else |
| 3454 | trace_smb3_notify_done(xid, persistent_fid, tcon->tid, |
| 3455 | ses->Suid, (u8)watch_tree, completion_filter); |
| 3456 | |
| 3457 | cnotify_exit: |
| 3458 | if (rqst.rq_iov) |
| 3459 | cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */ |
| 3460 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
| 3461 | return rc; |
| 3462 | } |
| 3463 | |
| 3464 | |
| 3465 | |
| 3466 | /* |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3467 | * This is a no-op for now. We're not really interested in the reply, but |
| 3468 | * rather in the fact that the server sent one and that server->lstrp |
| 3469 | * gets updated. |
| 3470 | * |
| 3471 | * FIXME: maybe we should consider checking that the reply matches request? |
| 3472 | */ |
| 3473 | static void |
| 3474 | smb2_echo_callback(struct mid_q_entry *mid) |
| 3475 | { |
| 3476 | struct TCP_Server_Info *server = mid->callback_data; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3477 | struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3478 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3479 | |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 3480 | if (mid->mid_state == MID_RESPONSE_RECEIVED |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3481 | || mid->mid_state == MID_RESPONSE_MALFORMED) { |
| 3482 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 3483 | credits.instance = server->reconnect_instance; |
| 3484 | } |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3485 | |
| 3486 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3487 | add_credits(server, &credits, CIFS_ECHO_OP); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3488 | } |
| 3489 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3490 | void smb2_reconnect_server(struct work_struct *work) |
| 3491 | { |
| 3492 | struct TCP_Server_Info *server = container_of(work, |
| 3493 | struct TCP_Server_Info, reconnect.work); |
| 3494 | struct cifs_ses *ses; |
| 3495 | struct cifs_tcon *tcon, *tcon2; |
| 3496 | struct list_head tmp_list; |
| 3497 | int tcon_exist = false; |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3498 | int rc; |
| 3499 | int resched = false; |
| 3500 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3501 | |
| 3502 | /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */ |
| 3503 | mutex_lock(&server->reconnect_mutex); |
| 3504 | |
| 3505 | INIT_LIST_HEAD(&tmp_list); |
| 3506 | cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n"); |
| 3507 | |
| 3508 | spin_lock(&cifs_tcp_ses_lock); |
| 3509 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
| 3510 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 3511 | if (tcon->need_reconnect || tcon->need_reopen_files) { |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3512 | tcon->tc_count++; |
| 3513 | list_add_tail(&tcon->rlist, &tmp_list); |
| 3514 | tcon_exist = true; |
| 3515 | } |
| 3516 | } |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3517 | /* |
| 3518 | * IPC has the same lifetime as its session and uses its |
| 3519 | * refcount. |
| 3520 | */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3521 | if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) { |
| 3522 | list_add_tail(&ses->tcon_ipc->rlist, &tmp_list); |
| 3523 | tcon_exist = true; |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3524 | ses->ses_count++; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3525 | } |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3526 | } |
| 3527 | /* |
| 3528 | * Get the reference to server struct to be sure that the last call of |
| 3529 | * cifs_put_tcon() in the loop below won't release the server pointer. |
| 3530 | */ |
| 3531 | if (tcon_exist) |
| 3532 | server->srv_count++; |
| 3533 | |
| 3534 | spin_unlock(&cifs_tcp_ses_lock); |
| 3535 | |
| 3536 | list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) { |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3537 | rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon); |
| 3538 | if (!rc) |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 3539 | cifs_reopen_persistent_handles(tcon); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3540 | else |
| 3541 | resched = true; |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3542 | list_del_init(&tcon->rlist); |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3543 | if (tcon->ipc) |
| 3544 | cifs_put_smb_ses(tcon->ses); |
| 3545 | else |
| 3546 | cifs_put_tcon(tcon); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | cifs_dbg(FYI, "Reconnecting tcons finished\n"); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3550 | if (resched) |
| 3551 | queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3552 | mutex_unlock(&server->reconnect_mutex); |
| 3553 | |
| 3554 | /* now we can safely release srv struct */ |
| 3555 | if (tcon_exist) |
| 3556 | cifs_put_tcp_session(server, 1); |
| 3557 | } |
| 3558 | |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3559 | int |
| 3560 | SMB2_echo(struct TCP_Server_Info *server) |
| 3561 | { |
| 3562 | struct smb2_echo_req *req; |
| 3563 | int rc = 0; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3564 | struct kvec iov[1]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3565 | struct smb_rqst rqst = { .rq_iov = iov, |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3566 | .rq_nvec = 1 }; |
Ronnie Sahlberg | 7f7ae75 | 2017-11-09 12:14:21 +1100 | [diff] [blame] | 3567 | unsigned int total_len; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3568 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3569 | cifs_dbg(FYI, "In echo request\n"); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3570 | |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 3571 | if (server->tcpStatus == CifsNeedNegotiate) { |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3572 | /* No need to send echo on newly established connections */ |
Stefan Metzmacher | b08484d | 2020-02-24 14:14:59 +0100 | [diff] [blame^] | 3573 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3574 | return rc; |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 3575 | } |
| 3576 | |
Ronnie Sahlberg | 7f7ae75 | 2017-11-09 12:14:21 +1100 | [diff] [blame] | 3577 | rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3578 | if (rc) |
| 3579 | return rc; |
| 3580 | |
Ronnie Sahlberg | 7f7ae75 | 2017-11-09 12:14:21 +1100 | [diff] [blame] | 3581 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3582 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3583 | iov[0].iov_len = total_len; |
| 3584 | iov[0].iov_base = (char *)req; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3585 | |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 3586 | rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 3587 | server, CIFS_ECHO_OP, NULL); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3588 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3589 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3590 | |
| 3591 | cifs_small_buf_release(req); |
| 3592 | return rc; |
| 3593 | } |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3594 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3595 | void |
| 3596 | SMB2_flush_free(struct smb_rqst *rqst) |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3597 | { |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3598 | if (rqst && rqst->rq_iov) |
| 3599 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
| 3600 | } |
| 3601 | |
| 3602 | int |
| 3603 | SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst, |
| 3604 | struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid) |
| 3605 | { |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3606 | struct smb2_flush_req *req; |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3607 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | 1f444e4 | 2017-11-20 11:24:39 +1100 | [diff] [blame] | 3608 | unsigned int total_len; |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3609 | int rc; |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3610 | |
Ronnie Sahlberg | 1f444e4 | 2017-11-20 11:24:39 +1100 | [diff] [blame] | 3611 | rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3612 | if (rc) |
| 3613 | return rc; |
| 3614 | |
| 3615 | req->PersistentFileId = persistent_fid; |
| 3616 | req->VolatileFileId = volatile_fid; |
| 3617 | |
| 3618 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 1f444e4 | 2017-11-20 11:24:39 +1100 | [diff] [blame] | 3619 | iov[0].iov_len = total_len; |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3620 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3621 | return 0; |
| 3622 | } |
| 3623 | |
| 3624 | int |
| 3625 | SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, |
| 3626 | u64 volatile_fid) |
| 3627 | { |
| 3628 | struct cifs_ses *ses = tcon->ses; |
| 3629 | struct smb_rqst rqst; |
| 3630 | struct kvec iov[1]; |
| 3631 | struct kvec rsp_iov = {NULL, 0}; |
| 3632 | int resp_buftype = CIFS_NO_BUFFER; |
| 3633 | int flags = 0; |
| 3634 | int rc = 0; |
| 3635 | |
| 3636 | cifs_dbg(FYI, "flush\n"); |
| 3637 | if (!ses || !(ses->server)) |
| 3638 | return -EIO; |
| 3639 | |
| 3640 | if (smb3_encryption_required(tcon)) |
| 3641 | flags |= CIFS_TRANSFORM_REQ; |
| 3642 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3643 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3644 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3645 | rqst.rq_iov = iov; |
| 3646 | rqst.rq_nvec = 1; |
| 3647 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3648 | rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid); |
| 3649 | if (rc) |
| 3650 | goto flush_exit; |
| 3651 | |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3652 | trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3653 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3654 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3655 | if (rc != 0) { |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3656 | cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3657 | trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3658 | rc); |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3659 | } else |
| 3660 | trace_smb3_flush_done(xid, persistent_fid, tcon->tid, |
| 3661 | ses->Suid); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3662 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3663 | flush_exit: |
| 3664 | SMB2_flush_free(&rqst); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3665 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3666 | return rc; |
| 3667 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3668 | |
| 3669 | /* |
| 3670 | * To form a chain of read requests, any read requests after the first should |
| 3671 | * have the end_of_chain boolean set to true. |
| 3672 | */ |
| 3673 | static int |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3674 | smb2_new_read_req(void **buf, unsigned int *total_len, |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3675 | struct cifs_io_parms *io_parms, struct cifs_readdata *rdata, |
| 3676 | unsigned int remaining_bytes, int request_type) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3677 | { |
| 3678 | int rc = -EACCES; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3679 | struct smb2_read_plain_req *req = NULL; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3680 | struct smb2_sync_hdr *shdr; |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3681 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3682 | |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3683 | rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req, |
| 3684 | total_len); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3685 | if (rc) |
| 3686 | return rc; |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3687 | |
| 3688 | server = io_parms->tcon->ses->server; |
| 3689 | if (server == NULL) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3690 | return -ECONNABORTED; |
| 3691 | |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3692 | shdr = &req->sync_hdr; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3693 | shdr->ProcessId = cpu_to_le32(io_parms->pid); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3694 | |
| 3695 | req->PersistentFileId = io_parms->persistent_fid; |
| 3696 | req->VolatileFileId = io_parms->volatile_fid; |
| 3697 | req->ReadChannelInfoOffset = 0; /* reserved */ |
| 3698 | req->ReadChannelInfoLength = 0; /* reserved */ |
| 3699 | req->Channel = 0; /* reserved */ |
| 3700 | req->MinimumCount = 0; |
| 3701 | req->Length = cpu_to_le32(io_parms->length); |
| 3702 | req->Offset = cpu_to_le64(io_parms->offset); |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 3703 | |
| 3704 | trace_smb3_read_enter(0 /* xid */, |
| 3705 | io_parms->persistent_fid, |
| 3706 | io_parms->tcon->tid, io_parms->tcon->ses->Suid, |
| 3707 | io_parms->offset, io_parms->length); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3708 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3709 | /* |
| 3710 | * If we want to do a RDMA write, fill in and append |
| 3711 | * smbd_buffer_descriptor_v1 to the end of read request |
| 3712 | */ |
Long Li | bb4c041 | 2018-04-17 12:17:08 -0700 | [diff] [blame] | 3713 | if (server->rdma && rdata && !server->sign && |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3714 | rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3715 | |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3716 | struct smbd_buffer_descriptor_v1 *v1; |
| 3717 | bool need_invalidate = |
| 3718 | io_parms->tcon->ses->server->dialect == SMB30_PROT_ID; |
| 3719 | |
| 3720 | rdata->mr = smbd_register_mr( |
| 3721 | server->smbd_conn, rdata->pages, |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 3722 | rdata->nr_pages, rdata->page_offset, |
| 3723 | rdata->tailsz, true, need_invalidate); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3724 | if (!rdata->mr) |
Long Li | b797209 | 2019-04-05 21:36:34 +0000 | [diff] [blame] | 3725 | return -EAGAIN; |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3726 | |
| 3727 | req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; |
| 3728 | if (need_invalidate) |
| 3729 | req->Channel = SMB2_CHANNEL_RDMA_V1; |
| 3730 | req->ReadChannelInfoOffset = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3731 | cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer)); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3732 | req->ReadChannelInfoLength = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3733 | cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1)); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3734 | v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0]; |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3735 | v1->offset = cpu_to_le64(rdata->mr->mr->iova); |
| 3736 | v1->token = cpu_to_le32(rdata->mr->mr->rkey); |
| 3737 | v1->length = cpu_to_le32(rdata->mr->mr->length); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3738 | |
| 3739 | *total_len += sizeof(*v1) - 1; |
| 3740 | } |
| 3741 | #endif |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3742 | if (request_type & CHAINED_REQUEST) { |
| 3743 | if (!(request_type & END_OF_CHAIN)) { |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3744 | /* next 8-byte aligned request */ |
| 3745 | *total_len = DIV_ROUND_UP(*total_len, 8) * 8; |
| 3746 | shdr->NextCommand = cpu_to_le32(*total_len); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3747 | } else /* END_OF_CHAIN */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3748 | shdr->NextCommand = 0; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3749 | if (request_type & RELATED_REQUEST) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3750 | shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3751 | /* |
| 3752 | * Related requests use info from previous read request |
| 3753 | * in chain. |
| 3754 | */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3755 | shdr->SessionId = 0xFFFFFFFF; |
| 3756 | shdr->TreeId = 0xFFFFFFFF; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3757 | req->PersistentFileId = 0xFFFFFFFF; |
| 3758 | req->VolatileFileId = 0xFFFFFFFF; |
| 3759 | } |
| 3760 | } |
| 3761 | if (remaining_bytes > io_parms->length) |
| 3762 | req->RemainingBytes = cpu_to_le32(remaining_bytes); |
| 3763 | else |
| 3764 | req->RemainingBytes = 0; |
| 3765 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3766 | *buf = req; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3767 | return rc; |
| 3768 | } |
| 3769 | |
| 3770 | static void |
| 3771 | smb2_readv_callback(struct mid_q_entry *mid) |
| 3772 | { |
| 3773 | struct cifs_readdata *rdata = mid->callback_data; |
| 3774 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
| 3775 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3776 | struct smb2_sync_hdr *shdr = |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 3777 | (struct smb2_sync_hdr *)rdata->iov[0].iov_base; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3778 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Steve French | 46f17d1 | 2019-09-04 23:07:52 -0500 | [diff] [blame] | 3779 | struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], |
| 3780 | .rq_nvec = 1, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3781 | .rq_pages = rdata->pages, |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3782 | .rq_offset = rdata->page_offset, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3783 | .rq_npages = rdata->nr_pages, |
| 3784 | .rq_pagesz = rdata->pagesz, |
| 3785 | .rq_tailsz = rdata->tailsz }; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3786 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3787 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
| 3788 | __func__, mid->mid, mid->mid_state, rdata->result, |
| 3789 | rdata->bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3790 | |
| 3791 | switch (mid->mid_state) { |
| 3792 | case MID_RESPONSE_RECEIVED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3793 | credits.value = le16_to_cpu(shdr->CreditRequest); |
| 3794 | credits.instance = server->reconnect_instance; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3795 | /* result already set, check signature */ |
Pavel Shilovsky | 4326ed2 | 2016-11-17 15:24:46 -0800 | [diff] [blame] | 3796 | if (server->sign && !mid->decrypted) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3797 | int rc; |
| 3798 | |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 3799 | rc = smb2_verify_signature(&rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3800 | if (rc) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3801 | cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3802 | rc); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3803 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3804 | /* FIXME: should this be counted toward the initiating task? */ |
Pavel Shilovsky | 34a54d6 | 2014-07-10 10:03:29 +0400 | [diff] [blame] | 3805 | task_io_account_read(rdata->got_bytes); |
| 3806 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3807 | break; |
| 3808 | case MID_REQUEST_SUBMITTED: |
| 3809 | case MID_RETRY_NEEDED: |
| 3810 | rdata->result = -EAGAIN; |
Pavel Shilovsky | d913ed1 | 2014-07-10 11:31:48 +0400 | [diff] [blame] | 3811 | if (server->sign && rdata->got_bytes) |
| 3812 | /* reset bytes number since we can not check a sign */ |
| 3813 | rdata->got_bytes = 0; |
| 3814 | /* FIXME: should this be counted toward the initiating task? */ |
| 3815 | task_io_account_read(rdata->got_bytes); |
| 3816 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3817 | break; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 3818 | case MID_RESPONSE_MALFORMED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3819 | credits.value = le16_to_cpu(shdr->CreditRequest); |
| 3820 | credits.instance = server->reconnect_instance; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 3821 | /* fall through */ |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3822 | default: |
Pavel Shilovsky | 6b15eb1 | 2019-01-18 15:46:14 -0800 | [diff] [blame] | 3823 | rdata->result = -EIO; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3824 | } |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3825 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3826 | /* |
| 3827 | * If this rdata has a memmory registered, the MR can be freed |
| 3828 | * MR needs to be freed as soon as I/O finishes to prevent deadlock |
| 3829 | * because they have limited number and are used for future I/Os |
| 3830 | */ |
| 3831 | if (rdata->mr) { |
| 3832 | smbd_deregister_mr(rdata->mr); |
| 3833 | rdata->mr = NULL; |
| 3834 | } |
| 3835 | #endif |
Pavel Shilovsky | 082aaa8 | 2019-01-18 15:54:34 -0800 | [diff] [blame] | 3836 | if (rdata->result && rdata->result != -ENODATA) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3837 | cifs_stats_fail_inc(tcon, SMB2_READ_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 3838 | trace_smb3_read_err(0 /* xid */, |
| 3839 | rdata->cfile->fid.persistent_fid, |
| 3840 | tcon->tid, tcon->ses->Suid, rdata->offset, |
| 3841 | rdata->bytes, rdata->result); |
| 3842 | } else |
| 3843 | trace_smb3_read_done(0 /* xid */, |
| 3844 | rdata->cfile->fid.persistent_fid, |
| 3845 | tcon->tid, tcon->ses->Suid, |
| 3846 | rdata->offset, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3847 | |
| 3848 | queue_work(cifsiod_wq, &rdata->work); |
| 3849 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3850 | add_credits(server, &credits, 0); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3851 | } |
| 3852 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3853 | /* 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] | 3854 | int |
| 3855 | smb2_async_readv(struct cifs_readdata *rdata) |
| 3856 | { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3857 | int rc, flags = 0; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3858 | char *buf; |
| 3859 | struct smb2_sync_hdr *shdr; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3860 | struct cifs_io_parms io_parms; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3861 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3862 | .rq_nvec = 1 }; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3863 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3864 | unsigned int total_len; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3865 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3866 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
| 3867 | __func__, rdata->offset, rdata->bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3868 | |
| 3869 | io_parms.tcon = tlink_tcon(rdata->cfile->tlink); |
| 3870 | io_parms.offset = rdata->offset; |
| 3871 | io_parms.length = rdata->bytes; |
| 3872 | io_parms.persistent_fid = rdata->cfile->fid.persistent_fid; |
| 3873 | io_parms.volatile_fid = rdata->cfile->fid.volatile_fid; |
| 3874 | io_parms.pid = rdata->pid; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3875 | |
| 3876 | server = io_parms.tcon->ses->server; |
| 3877 | |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3878 | rc = smb2_new_read_req( |
| 3879 | (void **) &buf, &total_len, &io_parms, rdata, 0, 0); |
Pavel Shilovsky | f0b93cb | 2019-01-25 11:10:00 -0800 | [diff] [blame] | 3880 | if (rc) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3881 | return rc; |
| 3882 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 3883 | if (smb3_encryption_required(io_parms.tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 3884 | flags |= CIFS_TRANSFORM_REQ; |
| 3885 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3886 | rdata->iov[0].iov_base = buf; |
| 3887 | rdata->iov[0].iov_len = total_len; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3888 | |
| 3889 | shdr = (struct smb2_sync_hdr *)buf; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3890 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 3891 | if (rdata->credits.value > 0) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3892 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes, |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3893 | SMB2_MAX_BUFFER_SIZE)); |
Pavel Shilovsky | b983f7e | 2018-12-19 22:49:09 +0000 | [diff] [blame] | 3894 | shdr->CreditRequest = |
| 3895 | cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 3896 | |
| 3897 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
| 3898 | if (rc) |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 3899 | goto async_readv_out; |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 3900 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 3901 | flags |= CIFS_HAS_CREDITS; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3902 | } |
| 3903 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3904 | kref_get(&rdata->refcount); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 3905 | rc = cifs_call_async(io_parms.tcon->ses->server, &rqst, |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3906 | cifs_readv_receive, smb2_readv_callback, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 3907 | smb3_handle_read_data, rdata, flags, |
| 3908 | &rdata->credits); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 3909 | if (rc) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3910 | kref_put(&rdata->refcount, cifs_readdata_release); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 3911 | cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 3912 | trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid, |
| 3913 | io_parms.tcon->tid, |
| 3914 | io_parms.tcon->ses->Suid, |
| 3915 | io_parms.offset, io_parms.length, rc); |
| 3916 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3917 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 3918 | async_readv_out: |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3919 | cifs_small_buf_release(buf); |
| 3920 | return rc; |
| 3921 | } |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3922 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3923 | int |
| 3924 | SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 3925 | unsigned int *nbytes, char **buf, int *buf_type) |
| 3926 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3927 | struct smb_rqst rqst; |
Colin Ian King | 1efd4fc | 2019-07-31 10:05:26 +0100 | [diff] [blame] | 3928 | int resp_buftype, rc; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3929 | struct smb2_read_plain_req *req = NULL; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3930 | struct smb2_read_rsp *rsp = NULL; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 3931 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3932 | struct kvec rsp_iov; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3933 | unsigned int total_len; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 3934 | int flags = CIFS_LOG_ERROR; |
| 3935 | struct cifs_ses *ses = io_parms->tcon->ses; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3936 | |
| 3937 | *nbytes = 0; |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3938 | 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] | 3939 | if (rc) |
| 3940 | return rc; |
| 3941 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 3942 | if (smb3_encryption_required(io_parms->tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 3943 | flags |= CIFS_TRANSFORM_REQ; |
| 3944 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 3945 | iov[0].iov_base = (char *)req; |
| 3946 | iov[0].iov_len = total_len; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3947 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3948 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 3949 | rqst.rq_iov = iov; |
| 3950 | rqst.rq_nvec = 1; |
| 3951 | |
| 3952 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3953 | rsp = (struct smb2_read_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3954 | |
| 3955 | if (rc) { |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 3956 | if (rc != -ENODATA) { |
| 3957 | cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE); |
| 3958 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 3959 | trace_smb3_read_err(xid, req->PersistentFileId, |
| 3960 | io_parms->tcon->tid, ses->Suid, |
| 3961 | io_parms->offset, io_parms->length, |
| 3962 | rc); |
Steve French | b0a42f2 | 2019-02-25 15:02:58 -0600 | [diff] [blame] | 3963 | } else |
| 3964 | trace_smb3_read_done(xid, req->PersistentFileId, |
| 3965 | io_parms->tcon->tid, ses->Suid, |
| 3966 | io_parms->offset, 0); |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 3967 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Ronnie Sahlberg | 05fd5c2 | 2019-04-23 16:39:45 +1000 | [diff] [blame] | 3968 | cifs_small_buf_release(req); |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 3969 | return rc == -ENODATA ? 0 : rc; |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3970 | } else |
| 3971 | trace_smb3_read_done(xid, req->PersistentFileId, |
| 3972 | io_parms->tcon->tid, ses->Suid, |
| 3973 | io_parms->offset, io_parms->length); |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3974 | |
ZhangXiaoxu | 088aaf1 | 2019-04-06 15:47:39 +0800 | [diff] [blame] | 3975 | cifs_small_buf_release(req); |
| 3976 | |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 3977 | *nbytes = le32_to_cpu(rsp->DataLength); |
| 3978 | if ((*nbytes > CIFS_MAX_MSGSIZE) || |
| 3979 | (*nbytes > io_parms->length)) { |
| 3980 | cifs_dbg(FYI, "bad length %d for count %d\n", |
| 3981 | *nbytes, io_parms->length); |
| 3982 | rc = -EIO; |
| 3983 | *nbytes = 0; |
| 3984 | } |
| 3985 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3986 | if (*buf) { |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 3987 | memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3988 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3989 | } else if (resp_buftype != CIFS_NO_BUFFER) { |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3990 | *buf = rsp_iov.iov_base; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3991 | if (resp_buftype == CIFS_SMALL_BUFFER) |
| 3992 | *buf_type = CIFS_SMALL_BUFFER; |
| 3993 | else if (resp_buftype == CIFS_LARGE_BUFFER) |
| 3994 | *buf_type = CIFS_LARGE_BUFFER; |
| 3995 | } |
| 3996 | return rc; |
| 3997 | } |
| 3998 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3999 | /* |
| 4000 | * Check the mid_state and signature on received buffer (if any), and queue the |
| 4001 | * workqueue completion task. |
| 4002 | */ |
| 4003 | static void |
| 4004 | smb2_writev_callback(struct mid_q_entry *mid) |
| 4005 | { |
| 4006 | struct cifs_writedata *wdata = mid->callback_data; |
| 4007 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4008 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4009 | unsigned int written; |
| 4010 | struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4011 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4012 | |
| 4013 | switch (mid->mid_state) { |
| 4014 | case MID_RESPONSE_RECEIVED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4015 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 4016 | credits.instance = server->reconnect_instance; |
| 4017 | wdata->result = smb2_check_receive(mid, server, 0); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4018 | if (wdata->result != 0) |
| 4019 | break; |
| 4020 | |
| 4021 | written = le32_to_cpu(rsp->DataLength); |
| 4022 | /* |
| 4023 | * Mask off high 16 bits when bytes written as returned |
| 4024 | * by the server is greater than bytes requested by the |
| 4025 | * client. OS/2 servers are known to set incorrect |
| 4026 | * CountHigh values. |
| 4027 | */ |
| 4028 | if (written > wdata->bytes) |
| 4029 | written &= 0xFFFF; |
| 4030 | |
| 4031 | if (written < wdata->bytes) |
| 4032 | wdata->result = -ENOSPC; |
| 4033 | else |
| 4034 | wdata->bytes = written; |
| 4035 | break; |
| 4036 | case MID_REQUEST_SUBMITTED: |
| 4037 | case MID_RETRY_NEEDED: |
| 4038 | wdata->result = -EAGAIN; |
| 4039 | break; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 4040 | case MID_RESPONSE_MALFORMED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4041 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 4042 | credits.instance = server->reconnect_instance; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 4043 | /* fall through */ |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4044 | default: |
| 4045 | wdata->result = -EIO; |
| 4046 | break; |
| 4047 | } |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4048 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4049 | /* |
| 4050 | * If this wdata has a memory registered, the MR can be freed |
| 4051 | * The number of MRs available is limited, it's important to recover |
| 4052 | * used MR as soon as I/O is finished. Hold MR longer in the later |
| 4053 | * I/O process can possibly result in I/O deadlock due to lack of MR |
| 4054 | * to send request on I/O retry |
| 4055 | */ |
| 4056 | if (wdata->mr) { |
| 4057 | smbd_deregister_mr(wdata->mr); |
| 4058 | wdata->mr = NULL; |
| 4059 | } |
| 4060 | #endif |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4061 | if (wdata->result) { |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4062 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4063 | trace_smb3_write_err(0 /* no xid */, |
| 4064 | wdata->cfile->fid.persistent_fid, |
| 4065 | tcon->tid, tcon->ses->Suid, wdata->offset, |
| 4066 | wdata->bytes, wdata->result); |
Steve French | d6fd419 | 2020-02-05 16:52:11 -0600 | [diff] [blame] | 4067 | if (wdata->result == -ENOSPC) |
| 4068 | printk_once(KERN_WARNING "Out of space writing to %s\n", |
| 4069 | tcon->treeName); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4070 | } else |
| 4071 | trace_smb3_write_done(0 /* no xid */, |
| 4072 | wdata->cfile->fid.persistent_fid, |
| 4073 | tcon->tid, tcon->ses->Suid, |
| 4074 | wdata->offset, wdata->bytes); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4075 | |
| 4076 | queue_work(cifsiod_wq, &wdata->work); |
| 4077 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4078 | add_credits(server, &credits, 0); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | /* smb2_async_writev - send an async write, and set up mid to handle result */ |
| 4082 | int |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 4083 | smb2_async_writev(struct cifs_writedata *wdata, |
| 4084 | void (*release)(struct kref *kref)) |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4085 | { |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4086 | int rc = -EACCES, flags = 0; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4087 | struct smb2_write_req *req = NULL; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4088 | struct smb2_sync_hdr *shdr; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4089 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4090 | struct TCP_Server_Info *server = tcon->ses->server; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4091 | struct kvec iov[1]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4092 | struct smb_rqst rqst = { }; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4093 | unsigned int total_len; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4094 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4095 | rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len); |
Pavel Shilovsky | f0b93cb | 2019-01-25 11:10:00 -0800 | [diff] [blame] | 4096 | if (rc) |
| 4097 | return rc; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4098 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4099 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4100 | flags |= CIFS_TRANSFORM_REQ; |
| 4101 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4102 | shdr = (struct smb2_sync_hdr *)req; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4103 | shdr->ProcessId = cpu_to_le32(wdata->cfile->pid); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4104 | |
| 4105 | req->PersistentFileId = wdata->cfile->fid.persistent_fid; |
| 4106 | req->VolatileFileId = wdata->cfile->fid.volatile_fid; |
| 4107 | req->WriteChannelInfoOffset = 0; |
| 4108 | req->WriteChannelInfoLength = 0; |
| 4109 | req->Channel = 0; |
| 4110 | req->Offset = cpu_to_le64(wdata->offset); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4111 | req->DataOffset = cpu_to_le16( |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4112 | offsetof(struct smb2_write_req, Buffer)); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4113 | req->RemainingBytes = 0; |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4114 | |
| 4115 | trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid, |
| 4116 | tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4117 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4118 | /* |
| 4119 | * If we want to do a server RDMA read, fill in and append |
| 4120 | * smbd_buffer_descriptor_v1 to the end of write request |
| 4121 | */ |
Long Li | bb4c041 | 2018-04-17 12:17:08 -0700 | [diff] [blame] | 4122 | if (server->rdma && !server->sign && wdata->bytes >= |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4123 | server->smbd_conn->rdma_readwrite_threshold) { |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4124 | |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4125 | struct smbd_buffer_descriptor_v1 *v1; |
| 4126 | bool need_invalidate = server->dialect == SMB30_PROT_ID; |
| 4127 | |
| 4128 | wdata->mr = smbd_register_mr( |
| 4129 | server->smbd_conn, wdata->pages, |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 4130 | wdata->nr_pages, wdata->page_offset, |
| 4131 | wdata->tailsz, false, need_invalidate); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4132 | if (!wdata->mr) { |
Long Li | b797209 | 2019-04-05 21:36:34 +0000 | [diff] [blame] | 4133 | rc = -EAGAIN; |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4134 | goto async_writev_out; |
| 4135 | } |
| 4136 | req->Length = 0; |
| 4137 | req->DataOffset = 0; |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 4138 | if (wdata->nr_pages > 1) |
| 4139 | req->RemainingBytes = |
| 4140 | cpu_to_le32( |
| 4141 | (wdata->nr_pages - 1) * wdata->pagesz - |
| 4142 | wdata->page_offset + wdata->tailsz |
| 4143 | ); |
| 4144 | else |
| 4145 | req->RemainingBytes = cpu_to_le32(wdata->tailsz); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4146 | req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; |
| 4147 | if (need_invalidate) |
| 4148 | req->Channel = SMB2_CHANNEL_RDMA_V1; |
| 4149 | req->WriteChannelInfoOffset = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4150 | cpu_to_le16(offsetof(struct smb2_write_req, Buffer)); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4151 | req->WriteChannelInfoLength = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4152 | cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1)); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4153 | v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0]; |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4154 | v1->offset = cpu_to_le64(wdata->mr->mr->iova); |
| 4155 | v1->token = cpu_to_le32(wdata->mr->mr->rkey); |
| 4156 | v1->length = cpu_to_le32(wdata->mr->mr->length); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4157 | } |
| 4158 | #endif |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4159 | iov[0].iov_len = total_len - 1; |
| 4160 | iov[0].iov_base = (char *)req; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4161 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4162 | rqst.rq_iov = iov; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4163 | rqst.rq_nvec = 1; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 4164 | rqst.rq_pages = wdata->pages; |
Long Li | 57a929a | 2018-05-30 12:47:53 -0700 | [diff] [blame] | 4165 | rqst.rq_offset = wdata->page_offset; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 4166 | rqst.rq_npages = wdata->nr_pages; |
| 4167 | rqst.rq_pagesz = wdata->pagesz; |
| 4168 | rqst.rq_tailsz = wdata->tailsz; |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4169 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4170 | if (wdata->mr) { |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4171 | iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4172 | rqst.rq_npages = 0; |
| 4173 | } |
| 4174 | #endif |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4175 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
| 4176 | wdata->offset, wdata->bytes); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4177 | |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4178 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4179 | /* For RDMA read, I/O size is in RemainingBytes not in Length */ |
| 4180 | if (!wdata->mr) |
| 4181 | req->Length = cpu_to_le32(wdata->bytes); |
| 4182 | #else |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4183 | req->Length = cpu_to_le32(wdata->bytes); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4184 | #endif |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4185 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4186 | if (wdata->credits.value > 0) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4187 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes, |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4188 | SMB2_MAX_BUFFER_SIZE)); |
Pavel Shilovsky | b983f7e | 2018-12-19 22:49:09 +0000 | [diff] [blame] | 4189 | shdr->CreditRequest = |
| 4190 | cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4191 | |
| 4192 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
| 4193 | if (rc) |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4194 | goto async_writev_out; |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4195 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4196 | flags |= CIFS_HAS_CREDITS; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4197 | } |
| 4198 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4199 | kref_get(&wdata->refcount); |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 4200 | rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 4201 | wdata, flags, &wdata->credits); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4202 | |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4203 | if (rc) { |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4204 | trace_smb3_write_err(0 /* no xid */, req->PersistentFileId, |
| 4205 | tcon->tid, tcon->ses->Suid, wdata->offset, |
| 4206 | wdata->bytes, rc); |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 4207 | kref_put(&wdata->refcount, release); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4208 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4209 | } |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4210 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4211 | async_writev_out: |
| 4212 | cifs_small_buf_release(req); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4213 | return rc; |
| 4214 | } |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4215 | |
| 4216 | /* |
| 4217 | * SMB2_write function gets iov pointer to kvec array with n_vec as a length. |
| 4218 | * The length field from io_parms must be at least 1 and indicates a number of |
| 4219 | * elements with data to write that begins with position 1 in iov array. All |
| 4220 | * data length is specified by count. |
| 4221 | */ |
| 4222 | int |
| 4223 | SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 4224 | unsigned int *nbytes, struct kvec *iov, int n_vec) |
| 4225 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4226 | struct smb_rqst rqst; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4227 | int rc = 0; |
| 4228 | struct smb2_write_req *req = NULL; |
| 4229 | struct smb2_write_rsp *rsp = NULL; |
| 4230 | int resp_buftype; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4231 | struct kvec rsp_iov; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4232 | int flags = 0; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4233 | unsigned int total_len; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4234 | |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4235 | *nbytes = 0; |
| 4236 | |
| 4237 | if (n_vec < 1) |
| 4238 | return rc; |
| 4239 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4240 | rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req, |
| 4241 | &total_len); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4242 | if (rc) |
| 4243 | return rc; |
| 4244 | |
| 4245 | if (io_parms->tcon->ses->server == NULL) |
| 4246 | return -ECONNABORTED; |
| 4247 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4248 | if (smb3_encryption_required(io_parms->tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4249 | flags |= CIFS_TRANSFORM_REQ; |
| 4250 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4251 | req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4252 | |
| 4253 | req->PersistentFileId = io_parms->persistent_fid; |
| 4254 | req->VolatileFileId = io_parms->volatile_fid; |
| 4255 | req->WriteChannelInfoOffset = 0; |
| 4256 | req->WriteChannelInfoLength = 0; |
| 4257 | req->Channel = 0; |
| 4258 | req->Length = cpu_to_le32(io_parms->length); |
| 4259 | req->Offset = cpu_to_le64(io_parms->offset); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4260 | req->DataOffset = cpu_to_le16( |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4261 | offsetof(struct smb2_write_req, Buffer)); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4262 | req->RemainingBytes = 0; |
| 4263 | |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4264 | trace_smb3_write_enter(xid, io_parms->persistent_fid, |
| 4265 | io_parms->tcon->tid, io_parms->tcon->ses->Suid, |
| 4266 | io_parms->offset, io_parms->length); |
| 4267 | |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4268 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4269 | /* 1 for Buffer */ |
| 4270 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4271 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4272 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4273 | rqst.rq_iov = iov; |
| 4274 | rqst.rq_nvec = n_vec + 1; |
| 4275 | |
| 4276 | rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst, |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4277 | &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4278 | rsp = (struct smb2_write_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4279 | |
| 4280 | if (rc) { |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4281 | trace_smb3_write_err(xid, req->PersistentFileId, |
| 4282 | io_parms->tcon->tid, |
| 4283 | io_parms->tcon->ses->Suid, |
| 4284 | io_parms->offset, io_parms->length, rc); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4285 | cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4286 | cifs_dbg(VFS, "Send error in write = %d\n", rc); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4287 | } else { |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4288 | *nbytes = le32_to_cpu(rsp->DataLength); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4289 | trace_smb3_write_done(xid, req->PersistentFileId, |
| 4290 | io_parms->tcon->tid, |
| 4291 | io_parms->tcon->ses->Suid, |
| 4292 | io_parms->offset, *nbytes); |
| 4293 | } |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4294 | |
ZhangXiaoxu | 6a3eb33 | 2019-04-06 15:47:38 +0800 | [diff] [blame] | 4295 | cifs_small_buf_release(req); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4296 | free_rsp_buf(resp_buftype, rsp); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4297 | return rc; |
| 4298 | } |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4299 | |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4300 | static unsigned int |
| 4301 | num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size) |
| 4302 | { |
| 4303 | int len; |
| 4304 | unsigned int entrycount = 0; |
| 4305 | unsigned int next_offset = 0; |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4306 | char *entryptr; |
| 4307 | FILE_DIRECTORY_INFO *dir_info; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4308 | |
| 4309 | if (bufstart == NULL) |
| 4310 | return 0; |
| 4311 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4312 | entryptr = bufstart; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4313 | |
| 4314 | while (1) { |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4315 | if (entryptr + next_offset < entryptr || |
| 4316 | entryptr + next_offset > end_of_buf || |
| 4317 | entryptr + next_offset + size > end_of_buf) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4318 | cifs_dbg(VFS, "malformed search entry would overflow\n"); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4319 | break; |
| 4320 | } |
| 4321 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4322 | entryptr = entryptr + next_offset; |
| 4323 | dir_info = (FILE_DIRECTORY_INFO *)entryptr; |
| 4324 | |
| 4325 | len = le32_to_cpu(dir_info->FileNameLength); |
| 4326 | if (entryptr + len < entryptr || |
| 4327 | entryptr + len > end_of_buf || |
| 4328 | entryptr + len + size > end_of_buf) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4329 | cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", |
| 4330 | end_of_buf); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4331 | break; |
| 4332 | } |
| 4333 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4334 | *lastentry = entryptr; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4335 | entrycount++; |
| 4336 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4337 | next_offset = le32_to_cpu(dir_info->NextEntryOffset); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4338 | if (!next_offset) |
| 4339 | break; |
| 4340 | } |
| 4341 | |
| 4342 | return entrycount; |
| 4343 | } |
| 4344 | |
| 4345 | /* |
| 4346 | * Readdir/FindFirst |
| 4347 | */ |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4348 | int SMB2_query_directory_init(const unsigned int xid, |
| 4349 | struct cifs_tcon *tcon, struct smb_rqst *rqst, |
| 4350 | u64 persistent_fid, u64 volatile_fid, |
| 4351 | int index, int info_level) |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4352 | { |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4353 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4354 | struct smb2_query_directory_req *req; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4355 | unsigned char *bufptr; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4356 | __le16 asteriks = cpu_to_le16('*'); |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4357 | unsigned int output_size = CIFSMaxBufSize - |
| 4358 | MAX_SMB2_CREATE_RESPONSE_SIZE - |
| 4359 | MAX_SMB2_CLOSE_RESPONSE_SIZE; |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4360 | unsigned int total_len; |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4361 | struct kvec *iov = rqst->rq_iov; |
| 4362 | int len, rc; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4363 | |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4364 | rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req, |
| 4365 | &total_len); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4366 | if (rc) |
| 4367 | return rc; |
| 4368 | |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4369 | switch (info_level) { |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4370 | case SMB_FIND_FILE_DIRECTORY_INFO: |
| 4371 | req->FileInformationClass = FILE_DIRECTORY_INFORMATION; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4372 | break; |
| 4373 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: |
| 4374 | req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4375 | break; |
| 4376 | default: |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 4377 | cifs_tcon_dbg(VFS, "info level %u isn't supported\n", |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4378 | info_level); |
| 4379 | return -EINVAL; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | req->FileIndex = cpu_to_le32(index); |
| 4383 | req->PersistentFileId = persistent_fid; |
| 4384 | req->VolatileFileId = volatile_fid; |
| 4385 | |
| 4386 | len = 0x2; |
| 4387 | bufptr = req->Buffer; |
| 4388 | memcpy(bufptr, &asteriks, len); |
| 4389 | |
| 4390 | req->FileNameOffset = |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4391 | cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4392 | req->FileNameLength = cpu_to_le16(len); |
| 4393 | /* |
| 4394 | * BB could be 30 bytes or so longer if we used SMB2 specific |
| 4395 | * buffer lengths, but this is safe and close enough. |
| 4396 | */ |
| 4397 | output_size = min_t(unsigned int, output_size, server->maxBuf); |
| 4398 | output_size = min_t(unsigned int, output_size, 2 << 15); |
| 4399 | req->OutputBufferLength = cpu_to_le32(output_size); |
| 4400 | |
| 4401 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4402 | /* 1 for Buffer */ |
| 4403 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4404 | |
| 4405 | iov[1].iov_base = (char *)(req->Buffer); |
| 4406 | iov[1].iov_len = len; |
| 4407 | |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4408 | trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid, |
| 4409 | tcon->ses->Suid, index, output_size); |
| 4410 | |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4411 | return 0; |
| 4412 | } |
| 4413 | |
| 4414 | void SMB2_query_directory_free(struct smb_rqst *rqst) |
| 4415 | { |
| 4416 | if (rqst && rqst->rq_iov) { |
| 4417 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | int |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4422 | smb2_parse_query_directory(struct cifs_tcon *tcon, |
| 4423 | struct kvec *rsp_iov, |
| 4424 | int resp_buftype, |
| 4425 | struct cifs_search_info *srch_inf) |
| 4426 | { |
| 4427 | struct smb2_query_directory_rsp *rsp; |
| 4428 | size_t info_buf_size; |
| 4429 | char *end_of_smb; |
| 4430 | int rc; |
| 4431 | |
| 4432 | rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base; |
| 4433 | |
| 4434 | switch (srch_inf->info_level) { |
| 4435 | case SMB_FIND_FILE_DIRECTORY_INFO: |
| 4436 | info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1; |
| 4437 | break; |
| 4438 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: |
| 4439 | info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1; |
| 4440 | break; |
| 4441 | default: |
| 4442 | cifs_tcon_dbg(VFS, "info level %u isn't supported\n", |
| 4443 | srch_inf->info_level); |
| 4444 | return -EINVAL; |
| 4445 | } |
| 4446 | |
| 4447 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 4448 | le32_to_cpu(rsp->OutputBufferLength), rsp_iov, |
| 4449 | info_buf_size); |
| 4450 | if (rc) |
| 4451 | return rc; |
| 4452 | |
| 4453 | srch_inf->unicode = true; |
| 4454 | |
| 4455 | if (srch_inf->ntwrk_buf_start) { |
| 4456 | if (srch_inf->smallBuf) |
| 4457 | cifs_small_buf_release(srch_inf->ntwrk_buf_start); |
| 4458 | else |
| 4459 | cifs_buf_release(srch_inf->ntwrk_buf_start); |
| 4460 | } |
| 4461 | srch_inf->ntwrk_buf_start = (char *)rsp; |
| 4462 | srch_inf->srch_entries_start = srch_inf->last_entry = |
| 4463 | (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset); |
| 4464 | end_of_smb = rsp_iov->iov_len + (char *)rsp; |
| 4465 | srch_inf->entries_in_buffer = |
| 4466 | num_entries(srch_inf->srch_entries_start, end_of_smb, |
| 4467 | &srch_inf->last_entry, info_buf_size); |
| 4468 | srch_inf->index_of_last_entry += srch_inf->entries_in_buffer; |
| 4469 | cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n", |
| 4470 | srch_inf->entries_in_buffer, srch_inf->index_of_last_entry, |
| 4471 | srch_inf->srch_entries_start, srch_inf->last_entry); |
| 4472 | if (resp_buftype == CIFS_LARGE_BUFFER) |
| 4473 | srch_inf->smallBuf = false; |
| 4474 | else if (resp_buftype == CIFS_SMALL_BUFFER) |
| 4475 | srch_inf->smallBuf = true; |
| 4476 | else |
| 4477 | cifs_tcon_dbg(VFS, "illegal search buffer type\n"); |
| 4478 | |
| 4479 | return 0; |
| 4480 | } |
| 4481 | |
| 4482 | int |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4483 | SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, |
| 4484 | u64 persistent_fid, u64 volatile_fid, int index, |
| 4485 | struct cifs_search_info *srch_inf) |
| 4486 | { |
| 4487 | struct smb_rqst rqst; |
| 4488 | struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE]; |
| 4489 | struct smb2_query_directory_rsp *rsp = NULL; |
| 4490 | int resp_buftype = CIFS_NO_BUFFER; |
| 4491 | struct kvec rsp_iov; |
| 4492 | int rc = 0; |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4493 | struct cifs_ses *ses = tcon->ses; |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4494 | int flags = 0; |
| 4495 | |
YueHaibing | c4985c3 | 2020-01-17 10:57:17 +0800 | [diff] [blame] | 4496 | if (!ses || !(ses->server)) |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4497 | return -EIO; |
| 4498 | |
| 4499 | if (smb3_encryption_required(tcon)) |
| 4500 | flags |= CIFS_TRANSFORM_REQ; |
| 4501 | |
| 4502 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4503 | memset(&iov, 0, sizeof(iov)); |
| 4504 | rqst.rq_iov = iov; |
| 4505 | rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE; |
| 4506 | |
| 4507 | rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid, |
| 4508 | volatile_fid, index, |
| 4509 | srch_inf->info_level); |
| 4510 | if (rc) |
| 4511 | goto qdir_exit; |
| 4512 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4513 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4514 | rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4515 | |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4516 | if (rc) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4517 | if (rc == -ENODATA && |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 4518 | rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) { |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4519 | trace_smb3_query_dir_done(xid, persistent_fid, |
| 4520 | tcon->tid, tcon->ses->Suid, index, 0); |
Pavel Shilovsky | 5275580 | 2014-08-18 20:49:57 +0400 | [diff] [blame] | 4521 | srch_inf->endOfSearch = true; |
| 4522 | rc = 0; |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4523 | } else { |
| 4524 | trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, |
| 4525 | tcon->ses->Suid, index, 0, rc); |
Pavel Shilovsky | 8e6e72a | 2019-01-26 12:21:32 -0800 | [diff] [blame] | 4526 | cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE); |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4527 | } |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4528 | goto qdir_exit; |
| 4529 | } |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4530 | |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4531 | rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype, |
| 4532 | srch_inf); |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4533 | if (rc) { |
| 4534 | trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, |
| 4535 | tcon->ses->Suid, index, 0, rc); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4536 | goto qdir_exit; |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4537 | } |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4538 | resp_buftype = CIFS_NO_BUFFER; |
| 4539 | |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4540 | trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid, |
| 4541 | tcon->ses->Suid, index, srch_inf->entries_in_buffer); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4542 | |
| 4543 | qdir_exit: |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4544 | SMB2_query_directory_free(&rqst); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4545 | free_rsp_buf(resp_buftype, rsp); |
| 4546 | return rc; |
| 4547 | } |
| 4548 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4549 | int |
| 4550 | SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4551 | u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class, |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4552 | u8 info_type, u32 additional_info, |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4553 | void **data, unsigned int *size) |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4554 | { |
| 4555 | struct smb2_set_info_req *req; |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4556 | struct kvec *iov = rqst->rq_iov; |
| 4557 | unsigned int i, total_len; |
| 4558 | int rc; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4559 | |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4560 | rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4561 | if (rc) |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4562 | return rc; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4563 | |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4564 | req->sync_hdr.ProcessId = cpu_to_le32(pid); |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4565 | req->InfoType = info_type; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4566 | req->FileInfoClass = info_class; |
| 4567 | req->PersistentFileId = persistent_fid; |
| 4568 | req->VolatileFileId = volatile_fid; |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4569 | req->AdditionalInformation = cpu_to_le32(additional_info); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4570 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4571 | req->BufferOffset = |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4572 | cpu_to_le16(sizeof(struct smb2_set_info_req) - 1); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4573 | req->BufferLength = cpu_to_le32(*size); |
| 4574 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4575 | memcpy(req->Buffer, *data, *size); |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4576 | total_len += *size; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4577 | |
| 4578 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4579 | /* 1 for Buffer */ |
| 4580 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4581 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4582 | for (i = 1; i < rqst->rq_nvec; i++) { |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4583 | le32_add_cpu(&req->BufferLength, size[i]); |
| 4584 | iov[i].iov_base = (char *)data[i]; |
| 4585 | iov[i].iov_len = size[i]; |
| 4586 | } |
| 4587 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4588 | return 0; |
| 4589 | } |
| 4590 | |
| 4591 | void |
| 4592 | SMB2_set_info_free(struct smb_rqst *rqst) |
| 4593 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 4594 | if (rqst && rqst->rq_iov) |
| 4595 | cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | static int |
| 4599 | send_set_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 4600 | u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class, |
| 4601 | u8 info_type, u32 additional_info, unsigned int num, |
| 4602 | void **data, unsigned int *size) |
| 4603 | { |
| 4604 | struct smb_rqst rqst; |
| 4605 | struct smb2_set_info_rsp *rsp = NULL; |
| 4606 | struct kvec *iov; |
| 4607 | struct kvec rsp_iov; |
| 4608 | int rc = 0; |
| 4609 | int resp_buftype; |
| 4610 | struct cifs_ses *ses = tcon->ses; |
| 4611 | int flags = 0; |
| 4612 | |
| 4613 | if (!ses || !(ses->server)) |
| 4614 | return -EIO; |
| 4615 | |
| 4616 | if (!num) |
| 4617 | return -EINVAL; |
| 4618 | |
| 4619 | if (smb3_encryption_required(tcon)) |
| 4620 | flags |= CIFS_TRANSFORM_REQ; |
| 4621 | |
| 4622 | iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL); |
| 4623 | if (!iov) |
| 4624 | return -ENOMEM; |
| 4625 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4626 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4627 | rqst.rq_iov = iov; |
| 4628 | rqst.rq_nvec = num; |
| 4629 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4630 | rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid, |
| 4631 | info_class, info_type, additional_info, |
| 4632 | data, size); |
| 4633 | if (rc) { |
| 4634 | kfree(iov); |
| 4635 | return rc; |
| 4636 | } |
| 4637 | |
| 4638 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4639 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4640 | &rsp_iov); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4641 | SMB2_set_info_free(&rqst); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4642 | rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4643 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4644 | if (rc != 0) { |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4645 | cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4646 | trace_smb3_set_info_err(xid, persistent_fid, tcon->tid, |
| 4647 | ses->Suid, info_class, (__u32)info_type, rc); |
| 4648 | } |
Steve French | 7d3fb24 | 2013-11-18 09:56:28 -0600 | [diff] [blame] | 4649 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4650 | free_rsp_buf(resp_buftype, rsp); |
| 4651 | kfree(iov); |
| 4652 | return rc; |
| 4653 | } |
| 4654 | |
| 4655 | int |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4656 | 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] | 4657 | u64 volatile_fid, u32 pid, __le64 *eof) |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4658 | { |
| 4659 | struct smb2_file_eof_info info; |
| 4660 | void *data; |
| 4661 | unsigned int size; |
| 4662 | |
| 4663 | info.EndOfFile = *eof; |
| 4664 | |
| 4665 | data = &info; |
| 4666 | size = sizeof(struct smb2_file_eof_info); |
| 4667 | |
Ronnie Sahlberg | 3764cbd | 2018-09-03 13:33:47 +1000 | [diff] [blame] | 4668 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4669 | pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE, |
| 4670 | 0, 1, &data, &size); |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4671 | } |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4672 | |
| 4673 | int |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4674 | SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon, |
| 4675 | u64 persistent_fid, u64 volatile_fid, |
| 4676 | struct cifs_ntsd *pnntsd, int pacllen, int aclflag) |
| 4677 | { |
| 4678 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
| 4679 | current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag, |
| 4680 | 1, (void **)&pnntsd, &pacllen); |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4681 | } |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4682 | |
| 4683 | int |
Ronnie Sahlberg | 5517554 | 2017-08-24 11:24:56 +1000 | [diff] [blame] | 4684 | SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, |
| 4685 | u64 persistent_fid, u64 volatile_fid, |
| 4686 | struct smb2_file_full_ea_info *buf, int len) |
| 4687 | { |
| 4688 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
| 4689 | current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, |
| 4690 | 0, 1, (void **)&buf, &len); |
| 4691 | } |
| 4692 | |
| 4693 | int |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4694 | SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, |
| 4695 | const u64 persistent_fid, const u64 volatile_fid, |
| 4696 | __u8 oplock_level) |
| 4697 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4698 | struct smb_rqst rqst; |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4699 | int rc; |
Ronnie Sahlberg | 0d5a288 | 2018-06-01 10:53:03 +1000 | [diff] [blame] | 4700 | struct smb2_oplock_break *req = NULL; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4701 | struct cifs_ses *ses = tcon->ses; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4702 | int flags = CIFS_OBREAK_OP; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4703 | unsigned int total_len; |
| 4704 | struct kvec iov[1]; |
| 4705 | struct kvec rsp_iov; |
| 4706 | int resp_buf_type; |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4707 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4708 | cifs_dbg(FYI, "SMB2_oplock_break\n"); |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4709 | rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req, |
| 4710 | &total_len); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4711 | if (rc) |
| 4712 | return rc; |
| 4713 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4714 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4715 | flags |= CIFS_TRANSFORM_REQ; |
| 4716 | |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4717 | req->VolatileFid = volatile_fid; |
| 4718 | req->PersistentFid = persistent_fid; |
| 4719 | req->OplockLevel = oplock_level; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4720 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4721 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 4722 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4723 | |
| 4724 | iov[0].iov_base = (char *)req; |
| 4725 | iov[0].iov_len = total_len; |
| 4726 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4727 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4728 | rqst.rq_iov = iov; |
| 4729 | rqst.rq_nvec = 1; |
| 4730 | |
| 4731 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4732 | cifs_small_buf_release(req); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4733 | |
| 4734 | if (rc) { |
| 4735 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4736 | cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | return rc; |
| 4740 | } |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4741 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 4742 | void |
| 4743 | smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, |
| 4744 | struct kstatfs *kst) |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4745 | { |
| 4746 | kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) * |
| 4747 | le32_to_cpu(pfs_inf->SectorsPerAllocationUnit); |
| 4748 | kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits); |
Sachin Prabhu | 42bec21 | 2017-08-03 13:09:03 +0530 | [diff] [blame] | 4749 | kst->f_bfree = kst->f_bavail = |
| 4750 | le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4751 | return; |
| 4752 | } |
| 4753 | |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 4754 | static void |
| 4755 | copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data, |
| 4756 | struct kstatfs *kst) |
| 4757 | { |
| 4758 | kst->f_bsize = le32_to_cpu(response_data->BlockSize); |
| 4759 | kst->f_blocks = le64_to_cpu(response_data->TotalBlocks); |
| 4760 | kst->f_bfree = le64_to_cpu(response_data->BlocksAvail); |
| 4761 | if (response_data->UserBlocksAvail == cpu_to_le64(-1)) |
| 4762 | kst->f_bavail = kst->f_bfree; |
| 4763 | else |
| 4764 | kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail); |
| 4765 | if (response_data->TotalFileNodes != cpu_to_le64(-1)) |
| 4766 | kst->f_files = le64_to_cpu(response_data->TotalFileNodes); |
| 4767 | if (response_data->FreeFileNodes != cpu_to_le64(-1)) |
| 4768 | kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes); |
| 4769 | |
| 4770 | return; |
| 4771 | } |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 4772 | |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4773 | static int |
| 4774 | build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level, |
| 4775 | int outbuf_len, u64 persistent_fid, u64 volatile_fid) |
| 4776 | { |
| 4777 | int rc; |
| 4778 | struct smb2_query_info_req *req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 4779 | unsigned int total_len; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4780 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4781 | cifs_dbg(FYI, "Query FSInfo level %d\n", level); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4782 | |
| 4783 | if ((tcon->ses == NULL) || (tcon->ses->server == NULL)) |
| 4784 | return -EIO; |
| 4785 | |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 4786 | rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req, |
| 4787 | &total_len); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4788 | if (rc) |
| 4789 | return rc; |
| 4790 | |
| 4791 | req->InfoType = SMB2_O_INFO_FILESYSTEM; |
| 4792 | req->FileInfoClass = level; |
| 4793 | req->PersistentFileId = persistent_fid; |
| 4794 | req->VolatileFileId = volatile_fid; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 4795 | /* 1 for pad */ |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4796 | req->InputBufferOffset = |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 4797 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4798 | req->OutputBufferLength = cpu_to_le32( |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 4799 | outbuf_len + sizeof(struct smb2_query_info_rsp) - 1); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4800 | |
| 4801 | iov->iov_base = (char *)req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 4802 | iov->iov_len = total_len; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4803 | return 0; |
| 4804 | } |
| 4805 | |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 4806 | int |
| 4807 | SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 4808 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) |
| 4809 | { |
| 4810 | struct smb_rqst rqst; |
| 4811 | struct smb2_query_info_rsp *rsp = NULL; |
| 4812 | struct kvec iov; |
| 4813 | struct kvec rsp_iov; |
| 4814 | int rc = 0; |
| 4815 | int resp_buftype; |
| 4816 | struct cifs_ses *ses = tcon->ses; |
| 4817 | FILE_SYSTEM_POSIX_INFO *info = NULL; |
| 4818 | int flags = 0; |
| 4819 | |
| 4820 | rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION, |
| 4821 | sizeof(FILE_SYSTEM_POSIX_INFO), |
| 4822 | persistent_fid, volatile_fid); |
| 4823 | if (rc) |
| 4824 | return rc; |
| 4825 | |
| 4826 | if (smb3_encryption_required(tcon)) |
| 4827 | flags |= CIFS_TRANSFORM_REQ; |
| 4828 | |
| 4829 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4830 | rqst.rq_iov = &iov; |
| 4831 | rqst.rq_nvec = 1; |
| 4832 | |
| 4833 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
| 4834 | cifs_small_buf_release(iov.iov_base); |
| 4835 | if (rc) { |
| 4836 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
| 4837 | goto posix_qfsinf_exit; |
| 4838 | } |
| 4839 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
| 4840 | |
| 4841 | info = (FILE_SYSTEM_POSIX_INFO *)( |
| 4842 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 4843 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 4844 | le32_to_cpu(rsp->OutputBufferLength), &rsp_iov, |
| 4845 | sizeof(FILE_SYSTEM_POSIX_INFO)); |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 4846 | if (!rc) |
| 4847 | copy_posix_fs_info_to_kstatfs(info, fsdata); |
| 4848 | |
| 4849 | posix_qfsinf_exit: |
| 4850 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
| 4851 | return rc; |
| 4852 | } |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 4853 | |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4854 | int |
| 4855 | SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 4856 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) |
| 4857 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4858 | struct smb_rqst rqst; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4859 | struct smb2_query_info_rsp *rsp = NULL; |
| 4860 | struct kvec iov; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4861 | struct kvec rsp_iov; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4862 | int rc = 0; |
| 4863 | int resp_buftype; |
| 4864 | struct cifs_ses *ses = tcon->ses; |
| 4865 | struct smb2_fs_full_size_info *info = NULL; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4866 | int flags = 0; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4867 | |
| 4868 | rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION, |
| 4869 | sizeof(struct smb2_fs_full_size_info), |
| 4870 | persistent_fid, volatile_fid); |
| 4871 | if (rc) |
| 4872 | return rc; |
| 4873 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4874 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4875 | flags |= CIFS_TRANSFORM_REQ; |
| 4876 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4877 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4878 | rqst.rq_iov = &iov; |
| 4879 | rqst.rq_nvec = 1; |
| 4880 | |
| 4881 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4882 | cifs_small_buf_release(iov.iov_base); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4883 | if (rc) { |
| 4884 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4885 | goto qfsinf_exit; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4886 | } |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4887 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4888 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 4889 | info = (struct smb2_fs_full_size_info *)( |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 4890 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 4891 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 4892 | le32_to_cpu(rsp->OutputBufferLength), &rsp_iov, |
| 4893 | sizeof(struct smb2_fs_full_size_info)); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4894 | if (!rc) |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 4895 | smb2_copy_fs_info_to_kstatfs(info, fsdata); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4896 | |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4897 | qfsinf_exit: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4898 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4899 | return rc; |
| 4900 | } |
| 4901 | |
| 4902 | int |
| 4903 | SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 4904 | u64 persistent_fid, u64 volatile_fid, int level) |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4905 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4906 | struct smb_rqst rqst; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4907 | struct smb2_query_info_rsp *rsp = NULL; |
| 4908 | struct kvec iov; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4909 | struct kvec rsp_iov; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4910 | int rc = 0; |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 4911 | int resp_buftype, max_len, min_len; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4912 | struct cifs_ses *ses = tcon->ses; |
| 4913 | unsigned int rsp_len, offset; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4914 | int flags = 0; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4915 | |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 4916 | if (level == FS_DEVICE_INFORMATION) { |
| 4917 | max_len = sizeof(FILE_SYSTEM_DEVICE_INFO); |
| 4918 | min_len = sizeof(FILE_SYSTEM_DEVICE_INFO); |
| 4919 | } else if (level == FS_ATTRIBUTE_INFORMATION) { |
| 4920 | max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO); |
| 4921 | min_len = MIN_FS_ATTR_INFO_SIZE; |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 4922 | } else if (level == FS_SECTOR_SIZE_INFORMATION) { |
| 4923 | max_len = sizeof(struct smb3_fs_ss_info); |
| 4924 | min_len = sizeof(struct smb3_fs_ss_info); |
Steve French | 21ba384 | 2018-06-24 23:18:52 -0500 | [diff] [blame] | 4925 | } else if (level == FS_VOLUME_INFORMATION) { |
| 4926 | max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN; |
| 4927 | min_len = sizeof(struct smb3_fs_vol_info); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 4928 | } else { |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 4929 | cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 4930 | return -EINVAL; |
| 4931 | } |
| 4932 | |
| 4933 | rc = build_qfs_info_req(&iov, tcon, level, max_len, |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4934 | persistent_fid, volatile_fid); |
| 4935 | if (rc) |
| 4936 | return rc; |
| 4937 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4938 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4939 | flags |= CIFS_TRANSFORM_REQ; |
| 4940 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4941 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4942 | rqst.rq_iov = &iov; |
| 4943 | rqst.rq_nvec = 1; |
| 4944 | |
| 4945 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4946 | cifs_small_buf_release(iov.iov_base); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4947 | if (rc) { |
| 4948 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
| 4949 | goto qfsattr_exit; |
| 4950 | } |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4951 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4952 | |
| 4953 | rsp_len = le32_to_cpu(rsp->OutputBufferLength); |
| 4954 | offset = le16_to_cpu(rsp->OutputBufferOffset); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 4955 | rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 4956 | if (rc) |
| 4957 | goto qfsattr_exit; |
| 4958 | |
| 4959 | if (level == FS_ATTRIBUTE_INFORMATION) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 4960 | memcpy(&tcon->fsAttrInfo, offset |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 4961 | + (char *)rsp, min_t(unsigned int, |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 4962 | rsp_len, max_len)); |
| 4963 | else if (level == FS_DEVICE_INFORMATION) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 4964 | memcpy(&tcon->fsDevInfo, offset |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 4965 | + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO)); |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 4966 | else if (level == FS_SECTOR_SIZE_INFORMATION) { |
| 4967 | struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 4968 | (offset + (char *)rsp); |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 4969 | tcon->ss_flags = le32_to_cpu(ss_info->Flags); |
| 4970 | tcon->perf_sector_size = |
| 4971 | le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf); |
Steve French | 21ba384 | 2018-06-24 23:18:52 -0500 | [diff] [blame] | 4972 | } else if (level == FS_VOLUME_INFORMATION) { |
| 4973 | struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *) |
| 4974 | (offset + (char *)rsp); |
| 4975 | tcon->vol_serial_number = vol_info->VolumeSerialNumber; |
| 4976 | tcon->vol_create_time = vol_info->VolumeCreationTime; |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 4977 | } |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 4978 | |
| 4979 | qfsattr_exit: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4980 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 4981 | return rc; |
| 4982 | } |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 4983 | |
| 4984 | int |
| 4985 | smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, |
| 4986 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, |
| 4987 | const __u32 num_lock, struct smb2_lock_element *buf) |
| 4988 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4989 | struct smb_rqst rqst; |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 4990 | int rc = 0; |
| 4991 | struct smb2_lock_req *req = NULL; |
| 4992 | struct kvec iov[2]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4993 | struct kvec rsp_iov; |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 4994 | int resp_buf_type; |
| 4995 | unsigned int count; |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 4996 | int flags = CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 4997 | unsigned int total_len; |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 4998 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4999 | cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5000 | |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5001 | rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5002 | if (rc) |
| 5003 | return rc; |
| 5004 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5005 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5006 | flags |= CIFS_TRANSFORM_REQ; |
| 5007 | |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5008 | req->sync_hdr.ProcessId = cpu_to_le32(pid); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5009 | req->LockCount = cpu_to_le16(num_lock); |
| 5010 | |
| 5011 | req->PersistentFileId = persist_fid; |
| 5012 | req->VolatileFileId = volatile_fid; |
| 5013 | |
| 5014 | count = num_lock * sizeof(struct smb2_lock_element); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5015 | |
| 5016 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5017 | iov[0].iov_len = total_len - sizeof(struct smb2_lock_element); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5018 | iov[1].iov_base = (char *)buf; |
| 5019 | iov[1].iov_len = count; |
| 5020 | |
| 5021 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5022 | |
| 5023 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5024 | rqst.rq_iov = iov; |
| 5025 | rqst.rq_nvec = 2; |
| 5026 | |
| 5027 | rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags, |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5028 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5029 | cifs_small_buf_release(req); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5030 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5031 | cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5032 | cifs_stats_fail_inc(tcon, SMB2_LOCK_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 5033 | trace_smb3_lock_err(xid, persist_fid, tcon->tid, |
| 5034 | tcon->ses->Suid, rc); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | return rc; |
| 5038 | } |
| 5039 | |
| 5040 | int |
| 5041 | SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon, |
| 5042 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, |
| 5043 | const __u64 length, const __u64 offset, const __u32 lock_flags, |
| 5044 | const bool wait) |
| 5045 | { |
| 5046 | struct smb2_lock_element lock; |
| 5047 | |
| 5048 | lock.Offset = cpu_to_le64(offset); |
| 5049 | lock.Length = cpu_to_le64(length); |
| 5050 | lock.Flags = cpu_to_le32(lock_flags); |
| 5051 | if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK) |
| 5052 | lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY); |
| 5053 | |
| 5054 | return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock); |
| 5055 | } |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5056 | |
| 5057 | int |
| 5058 | SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, |
| 5059 | __u8 *lease_key, const __le32 lease_state) |
| 5060 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5061 | struct smb_rqst rqst; |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5062 | int rc; |
| 5063 | struct smb2_lease_ack *req = NULL; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5064 | struct cifs_ses *ses = tcon->ses; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5065 | int flags = CIFS_OBREAK_OP; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5066 | unsigned int total_len; |
| 5067 | struct kvec iov[1]; |
| 5068 | struct kvec rsp_iov; |
| 5069 | int resp_buf_type; |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5070 | __u64 *please_key_high; |
| 5071 | __u64 *please_key_low; |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5072 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5073 | cifs_dbg(FYI, "SMB2_lease_break\n"); |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5074 | rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req, |
| 5075 | &total_len); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5076 | if (rc) |
| 5077 | return rc; |
| 5078 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5079 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5080 | flags |= CIFS_TRANSFORM_REQ; |
| 5081 | |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5082 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5083 | req->StructureSize = cpu_to_le16(36); |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5084 | total_len += 12; |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5085 | |
| 5086 | memcpy(req->LeaseKey, lease_key, 16); |
| 5087 | req->LeaseState = lease_state; |
| 5088 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 5089 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5090 | |
| 5091 | iov[0].iov_base = (char *)req; |
| 5092 | iov[0].iov_len = total_len; |
| 5093 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5094 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5095 | rqst.rq_iov = iov; |
| 5096 | rqst.rq_nvec = 1; |
| 5097 | |
| 5098 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5099 | cifs_small_buf_release(req); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5100 | |
Aurelien Aptel | d339adc | 2019-01-31 13:46:07 +0100 | [diff] [blame] | 5101 | please_key_low = (__u64 *)lease_key; |
| 5102 | please_key_high = (__u64 *)(lease_key+8); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5103 | if (rc) { |
| 5104 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5105 | trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid, |
| 5106 | ses->Suid, *please_key_low, *please_key_high, rc); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5107 | cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc); |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5108 | } else |
| 5109 | trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid, |
| 5110 | ses->Suid, *please_key_low, *please_key_high); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5111 | |
| 5112 | return rc; |
| 5113 | } |