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