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