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