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