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