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 | { |
Steve French | edb1613 | 2020-05-31 14:36:56 -0500 | [diff] [blame] | 88 | if (!tcon || !tcon->ses) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 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, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 101 | const struct cifs_tcon *tcon, |
| 102 | struct TCP_Server_Info *server) |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 103 | { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 104 | shdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 105 | shdr->StructureSize = cpu_to_le16(64); |
| 106 | shdr->Command = smb2_cmd; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 107 | if (server) { |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 108 | spin_lock(&server->req_lock); |
Steve French | 69dc4b1 | 2019-03-05 21:04:56 -0600 | [diff] [blame] | 109 | /* Request up to 10 credits but don't go over the limit. */ |
Steve French | 141891f | 2016-09-23 00:44:16 -0500 | [diff] [blame] | 110 | if (server->credits >= server->max_credits) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 111 | shdr->CreditRequest = cpu_to_le16(0); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 112 | else |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 113 | shdr->CreditRequest = cpu_to_le16( |
Steve French | 141891f | 2016-09-23 00:44:16 -0500 | [diff] [blame] | 114 | min_t(int, server->max_credits - |
Steve French | 69dc4b1 | 2019-03-05 21:04:56 -0600 | [diff] [blame] | 115 | server->credits, 10)); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 116 | spin_unlock(&server->req_lock); |
| 117 | } else { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 118 | shdr->CreditRequest = cpu_to_le16(2); |
Ross Lagerwall | 7d414f3 | 2016-09-20 13:37:13 +0100 | [diff] [blame] | 119 | } |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 120 | shdr->ProcessId = cpu_to_le32((__u16)current->tgid); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 121 | |
| 122 | if (!tcon) |
| 123 | goto out; |
| 124 | |
Steve French | 2b80d04 | 2013-06-23 18:43:37 -0500 | [diff] [blame] | 125 | /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */ |
| 126 | /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */ |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 127 | if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU)) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 128 | shdr->CreditCharge = cpu_to_le16(1); |
Steve French | 2b80d04 | 2013-06-23 18:43:37 -0500 | [diff] [blame] | 129 | /* else CreditCharge MBZ */ |
| 130 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 131 | shdr->TreeId = tcon->tid; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 132 | /* Uid is not converted */ |
| 133 | if (tcon->ses) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 134 | shdr->SessionId = tcon->ses->Suid; |
Steve French | f87ab88 | 2013-06-26 19:14:55 -0500 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have |
| 138 | * to pass the path on the Open SMB prefixed by \\server\share. |
| 139 | * Not sure when we would need to do the augmented path (if ever) and |
| 140 | * setting this flag breaks the SMB2 open operation since it is |
| 141 | * illegal to send an empty path name (without \\server\share prefix) |
| 142 | * when the DFS flag is set in the SMB open header. We could |
| 143 | * consider setting the flag on all operations other than open |
| 144 | * but it is safer to net set it for now. |
| 145 | */ |
| 146 | /* if (tcon->share_flags & SHI1005_FLAGS_DFS) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 147 | shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */ |
Steve French | f87ab88 | 2013-06-26 19:14:55 -0500 | [diff] [blame] | 148 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 149 | if (server && server->sign && !smb3_encryption_required(tcon)) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 150 | shdr->Flags |= SMB2_FLAGS_SIGNED; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 151 | out: |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
| 155 | static int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 156 | smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon, |
| 157 | struct TCP_Server_Info *server) |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 158 | { |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 159 | int rc; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 160 | struct nls_table *nls_codepage; |
| 161 | struct cifs_ses *ses; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 162 | int retries; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so |
| 166 | * check for tcp and smb session status done differently |
| 167 | * for those three - in the calling routine. |
| 168 | */ |
| 169 | if (tcon == NULL) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 170 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 171 | |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 172 | if (smb2_command == SMB2_TREE_CONNECT) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 173 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 174 | |
| 175 | if (tcon->tidStatus == CifsExiting) { |
| 176 | /* |
| 177 | * only tree disconnect, open, and write, |
| 178 | * (and ulogoff which does not have tcon) |
| 179 | * are allowed as we start force umount. |
| 180 | */ |
| 181 | if ((smb2_command != SMB2_WRITE) && |
| 182 | (smb2_command != SMB2_CREATE) && |
| 183 | (smb2_command != SMB2_TREE_DISCONNECT)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 184 | cifs_dbg(FYI, "can not send cmd %d while umounting\n", |
| 185 | smb2_command); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 186 | return -ENODEV; |
| 187 | } |
| 188 | } |
| 189 | if ((!tcon->ses) || (tcon->ses->status == CifsExiting) || |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 190 | (!tcon->ses->server) || !server) |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 191 | return -EIO; |
| 192 | |
| 193 | ses = tcon->ses; |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 194 | retries = server->nr_targets; |
| 195 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 196 | /* |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 197 | * Give demultiplex thread up to 10 seconds to each target available for |
| 198 | * reconnect -- should be greater than cifs socket timeout which is 7 |
| 199 | * seconds. |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 200 | */ |
| 201 | while (server->tcpStatus == CifsNeedReconnect) { |
| 202 | /* |
| 203 | * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE |
| 204 | * here since they are implicitly done when session drops. |
| 205 | */ |
| 206 | switch (smb2_command) { |
| 207 | /* |
| 208 | * BB Should we keep oplock break and add flush to exceptions? |
| 209 | */ |
| 210 | case SMB2_TREE_DISCONNECT: |
| 211 | case SMB2_CANCEL: |
| 212 | case SMB2_CLOSE: |
| 213 | case SMB2_OPLOCK_BREAK: |
| 214 | return -EAGAIN; |
| 215 | } |
| 216 | |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 217 | rc = wait_event_interruptible_timeout(server->response_q, |
| 218 | (server->tcpStatus != CifsNeedReconnect), |
| 219 | 10 * HZ); |
| 220 | if (rc < 0) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 221 | cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n", |
| 222 | __func__); |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 223 | return -ERESTARTSYS; |
| 224 | } |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 225 | |
| 226 | /* are we still trying to reconnect? */ |
| 227 | if (server->tcpStatus != CifsNeedReconnect) |
| 228 | break; |
| 229 | |
Ronnie Sahlberg | c54849d | 2020-01-31 05:52:51 +1000 | [diff] [blame] | 230 | if (retries && --retries) |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 231 | continue; |
| 232 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 233 | /* |
| 234 | * on "soft" mounts we wait once. Hard mounts keep |
| 235 | * retrying until process is killed or server comes |
| 236 | * back on-line |
| 237 | */ |
| 238 | if (!tcon->retry) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 239 | cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n"); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 240 | return -EHOSTDOWN; |
| 241 | } |
Paulo Alcantara | a3a53b7 | 2018-11-14 17:20:31 -0200 | [diff] [blame] | 242 | retries = server->nr_targets; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if (!tcon->ses->need_reconnect && !tcon->need_reconnect) |
Paulo Alcantara | 7ffbe65 | 2018-07-05 13:46:34 -0300 | [diff] [blame] | 246 | return 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 247 | |
| 248 | nls_codepage = load_nls_default(); |
| 249 | |
| 250 | /* |
| 251 | * need to prevent multiple threads trying to simultaneously reconnect |
| 252 | * the same SMB session |
| 253 | */ |
| 254 | mutex_lock(&tcon->ses->session_mutex); |
Samuel Cabrero | 76e7527 | 2017-07-11 12:44:39 +0200 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * Recheck after acquire mutex. If another thread is negotiating |
| 258 | * and the server never sends an answer the socket will be closed |
| 259 | * and tcpStatus set to reconnect. |
| 260 | */ |
| 261 | if (server->tcpStatus == CifsNeedReconnect) { |
| 262 | rc = -EHOSTDOWN; |
| 263 | mutex_unlock(&tcon->ses->session_mutex); |
| 264 | goto out; |
| 265 | } |
| 266 | |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 267 | /* |
| 268 | * If we are reconnecting an extra channel, bind |
| 269 | */ |
| 270 | if (server->is_channel) { |
| 271 | ses->binding = true; |
| 272 | ses->binding_chan = cifs_ses_find_chan(ses, server); |
| 273 | } |
| 274 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 275 | rc = cifs_negotiate_protocol(0, tcon->ses); |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 276 | if (!rc && tcon->ses->need_reconnect) { |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 277 | rc = cifs_setup_session(0, tcon->ses, nls_codepage); |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 278 | if ((rc == -EACCES) && !tcon->retry) { |
| 279 | rc = -EHOSTDOWN; |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 280 | ses->binding = false; |
| 281 | ses->binding_chan = NULL; |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 282 | mutex_unlock(&tcon->ses->session_mutex); |
| 283 | goto failed; |
| 284 | } |
| 285 | } |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 286 | /* |
| 287 | * End of channel binding |
| 288 | */ |
| 289 | ses->binding = false; |
| 290 | ses->binding_chan = NULL; |
| 291 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 292 | if (rc || !tcon->need_reconnect) { |
| 293 | mutex_unlock(&tcon->ses->session_mutex); |
| 294 | goto out; |
| 295 | } |
| 296 | |
| 297 | cifs_mark_open_files_invalid(tcon); |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 298 | if (tcon->use_persistent) |
| 299 | tcon->need_reopen_files = true; |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 300 | |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 301 | rc = cifs_tree_connect(0, tcon, nls_codepage); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 302 | mutex_unlock(&tcon->ses->session_mutex); |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 303 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 304 | cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc); |
Steve French | c318e6c | 2018-04-04 14:08:52 -0500 | [diff] [blame] | 305 | if (rc) { |
| 306 | /* If sess reconnected but tcon didn't, something strange ... */ |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 307 | pr_warn_once("reconnect tcon failed rc = %d\n", rc); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 308 | goto out; |
Steve French | c318e6c | 2018-04-04 14:08:52 -0500 | [diff] [blame] | 309 | } |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 310 | |
| 311 | if (smb2_command != SMB2_INTERNAL_CMD) |
Stefan Metzmacher | b08484d | 2020-02-24 14:14:59 +0100 | [diff] [blame] | 312 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 313 | |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 314 | atomic_inc(&tconInfoReconnectCount); |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 315 | out: |
| 316 | /* |
| 317 | * Check if handle based operation so we know whether we can continue |
| 318 | * or not without returning to caller to reset file handle. |
| 319 | */ |
| 320 | /* |
| 321 | * BB Is flush done by server on drop of tcp session? Should we special |
| 322 | * case it and skip above? |
| 323 | */ |
| 324 | switch (smb2_command) { |
| 325 | case SMB2_FLUSH: |
| 326 | case SMB2_READ: |
| 327 | case SMB2_WRITE: |
| 328 | case SMB2_LOCK: |
| 329 | case SMB2_IOCTL: |
| 330 | case SMB2_QUERY_DIRECTORY: |
| 331 | case SMB2_CHANGE_NOTIFY: |
| 332 | case SMB2_QUERY_INFO: |
| 333 | case SMB2_SET_INFO: |
Pavel Shilovsky | 4772c79 | 2016-11-29 11:30:58 -0800 | [diff] [blame] | 334 | rc = -EAGAIN; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 335 | } |
Ronnie Sahlberg | b0dd940 | 2020-02-05 11:08:01 +1000 | [diff] [blame] | 336 | failed: |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 337 | unload_nls(nls_codepage); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 338 | return rc; |
| 339 | } |
| 340 | |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 341 | static void |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 342 | fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, |
| 343 | struct TCP_Server_Info *server, |
| 344 | void *buf, |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 345 | unsigned int *total_len) |
| 346 | { |
| 347 | struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf; |
| 348 | /* lookup word count ie StructureSize from table */ |
| 349 | __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)]; |
| 350 | |
| 351 | /* |
| 352 | * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of |
| 353 | * largest operations (Create) |
| 354 | */ |
| 355 | memset(buf, 0, 256); |
| 356 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 357 | smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon, server); |
Pavel Shilovsky | cb200bd | 2016-10-24 16:59:57 -0700 | [diff] [blame] | 358 | spdu->StructureSize2 = cpu_to_le16(parmsize); |
| 359 | |
| 360 | *total_len = parmsize + sizeof(struct smb2_sync_hdr); |
| 361 | } |
| 362 | |
Ronnie Sahlberg | 305428a | 2017-11-21 11:04:42 +1100 | [diff] [blame] | 363 | /* |
| 364 | * Allocate and return pointer to an SMB request hdr, and set basic |
| 365 | * SMB information in the SMB header. If the return code is zero, this |
| 366 | * function must have filled in request_buf pointer. |
| 367 | */ |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 368 | static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 369 | struct TCP_Server_Info *server, |
| 370 | void **request_buf, unsigned int *total_len) |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 371 | { |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 372 | /* BB eventually switch this to SMB2 specific small buf size */ |
Stefano Brivio | f46ecbd | 2018-07-05 11:46:42 +0200 | [diff] [blame] | 373 | if (smb2_command == SMB2_SET_INFO) |
| 374 | *request_buf = cifs_buf_get(); |
| 375 | else |
| 376 | *request_buf = cifs_small_buf_get(); |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 377 | if (*request_buf == NULL) { |
| 378 | /* BB should we add a retry in here if not a writepage? */ |
| 379 | return -ENOMEM; |
| 380 | } |
| 381 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 382 | fill_small_buf(smb2_command, tcon, server, |
Ronnie Sahlberg | 305428a | 2017-11-21 11:04:42 +1100 | [diff] [blame] | 383 | (struct smb2_sync_hdr *)(*request_buf), |
| 384 | total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 385 | |
| 386 | if (tcon != NULL) { |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 387 | uint16_t com_code = le16_to_cpu(smb2_command); |
| 388 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 389 | cifs_stats_inc(&tcon->num_smbs_sent); |
| 390 | } |
| 391 | |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 396 | struct TCP_Server_Info *server, |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 397 | void **request_buf, unsigned int *total_len) |
| 398 | { |
| 399 | int rc; |
| 400 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 401 | rc = smb2_reconnect(smb2_command, tcon, server); |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 402 | if (rc) |
| 403 | return rc; |
| 404 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 405 | return __smb2_plain_req_init(smb2_command, tcon, server, request_buf, |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 406 | total_len); |
| 407 | } |
| 408 | |
| 409 | static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 410 | struct TCP_Server_Info *server, |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 411 | void **request_buf, unsigned int *total_len) |
| 412 | { |
| 413 | /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */ |
| 414 | if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) { |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 415 | return __smb2_plain_req_init(SMB2_IOCTL, tcon, server, |
| 416 | request_buf, total_len); |
Paulo Alcantara (SUSE) | 84a1f5b | 2019-11-22 12:30:53 -0300 | [diff] [blame] | 417 | } |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 418 | return smb2_plain_req_init(SMB2_IOCTL, tcon, server, |
| 419 | request_buf, total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 420 | } |
| 421 | |
Steve French | d7bef4c | 2019-04-18 11:03:58 -0500 | [diff] [blame] | 422 | /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */ |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 423 | |
| 424 | static void |
| 425 | build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt) |
| 426 | { |
| 427 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 428 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 429 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 430 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 431 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 432 | pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512; |
| 433 | } |
| 434 | |
| 435 | static void |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 436 | build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt) |
| 437 | { |
| 438 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 439 | pneg_ctxt->DataLength = |
| 440 | cpu_to_le16(sizeof(struct smb2_compression_capabilities_context) |
| 441 | - sizeof(struct smb2_neg_context)); |
| 442 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3); |
| 443 | pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77; |
| 444 | pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF; |
| 445 | pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1; |
| 446 | } |
| 447 | |
| 448 | static void |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 449 | build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt) |
| 450 | { |
| 451 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
Steve French | fbfd0b4 | 2020-09-11 16:19:28 -0500 | [diff] [blame] | 452 | if (require_gcm_256) { |
| 453 | pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */ |
| 454 | pneg_ctxt->CipherCount = cpu_to_le16(1); |
| 455 | pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM; |
Steve French | 29e2792 | 2020-10-14 20:24:09 -0500 | [diff] [blame] | 456 | } else if (enable_gcm_256) { |
| 457 | pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */ |
| 458 | pneg_ctxt->CipherCount = cpu_to_le16(3); |
| 459 | pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM; |
| 460 | pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM; |
| 461 | pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM; |
Steve French | fbfd0b4 | 2020-09-11 16:19:28 -0500 | [diff] [blame] | 462 | } else { |
| 463 | pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */ |
| 464 | pneg_ctxt->CipherCount = cpu_to_le16(2); |
| 465 | pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM; |
| 466 | pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM; |
| 467 | } |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 468 | } |
| 469 | |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 470 | static unsigned int |
| 471 | build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname) |
| 472 | { |
| 473 | struct nls_table *cp = load_nls_default(); |
| 474 | |
| 475 | pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID; |
| 476 | |
| 477 | /* copy up to max of first 100 bytes of server name to NetName field */ |
Steve French | df58fae | 2019-08-05 17:07:26 -0500 | [diff] [blame] | 478 | pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp)); |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 479 | /* context size is DataLength + minimal smb2_neg_context */ |
| 480 | return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) + |
| 481 | sizeof(struct smb2_neg_context), 8) * 8; |
| 482 | } |
| 483 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 484 | static void |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 485 | build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
| 486 | { |
| 487 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 488 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
Steve French | 0d48132 | 2019-02-24 17:56:33 -0600 | [diff] [blame] | 489 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 490 | pneg_ctxt->Name[0] = 0x93; |
| 491 | pneg_ctxt->Name[1] = 0xAD; |
| 492 | pneg_ctxt->Name[2] = 0x25; |
| 493 | pneg_ctxt->Name[3] = 0x50; |
| 494 | pneg_ctxt->Name[4] = 0x9C; |
| 495 | pneg_ctxt->Name[5] = 0xB4; |
| 496 | pneg_ctxt->Name[6] = 0x11; |
| 497 | pneg_ctxt->Name[7] = 0xE7; |
| 498 | pneg_ctxt->Name[8] = 0xB4; |
| 499 | pneg_ctxt->Name[9] = 0x23; |
| 500 | pneg_ctxt->Name[10] = 0x83; |
| 501 | pneg_ctxt->Name[11] = 0xDE; |
| 502 | pneg_ctxt->Name[12] = 0x96; |
| 503 | pneg_ctxt->Name[13] = 0x8B; |
| 504 | pneg_ctxt->Name[14] = 0xCD; |
| 505 | pneg_ctxt->Name[15] = 0x7C; |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 509 | assemble_neg_contexts(struct smb2_negotiate_req *req, |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 510 | struct TCP_Server_Info *server, unsigned int *total_len) |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 511 | { |
Colin Ian King | a9f76cf | 2019-12-02 18:59:42 +0000 | [diff] [blame] | 512 | char *pneg_ctxt; |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 513 | unsigned int ctxt_len; |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 514 | |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 515 | if (*total_len > 200) { |
| 516 | /* In case length corrupted don't want to overrun smb buffer */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 517 | cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n"); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * round up total_len of fixed part of SMB3 negotiate request to 8 |
| 523 | * byte boundary before adding negotiate contexts |
| 524 | */ |
| 525 | *total_len = roundup(*total_len, 8); |
| 526 | |
| 527 | pneg_ctxt = (*total_len) + (char *)req; |
| 528 | req->NegotiateContextOffset = cpu_to_le32(*total_len); |
| 529 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 530 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 531 | ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8; |
| 532 | *total_len += ctxt_len; |
| 533 | pneg_ctxt += ctxt_len; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 534 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 535 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 536 | ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8; |
| 537 | *total_len += ctxt_len; |
| 538 | pneg_ctxt += ctxt_len; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 539 | |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 540 | if (server->compress_algorithm) { |
| 541 | build_compression_ctxt((struct smb2_compression_capabilities_context *) |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 542 | pneg_ctxt); |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 543 | ctxt_len = DIV_ROUND_UP( |
| 544 | sizeof(struct smb2_compression_capabilities_context), |
| 545 | 8) * 8; |
| 546 | *total_len += ctxt_len; |
| 547 | pneg_ctxt += ctxt_len; |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 548 | req->NegotiateContextCount = cpu_to_le16(5); |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 549 | } else |
Steve French | 96d3cca | 2019-06-25 04:39:51 -0500 | [diff] [blame] | 550 | req->NegotiateContextCount = cpu_to_le16(4); |
| 551 | |
| 552 | ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt, |
| 553 | server->hostname); |
| 554 | *total_len += ctxt_len; |
| 555 | pneg_ctxt += ctxt_len; |
| 556 | |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 557 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 558 | *total_len += sizeof(struct smb2_posix_neg_context); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 559 | } |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 560 | |
| 561 | static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt) |
| 562 | { |
| 563 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 564 | |
| 565 | /* If invalid preauth context warn but use what we requested, SHA-512 */ |
| 566 | if (len < MIN_PREAUTH_CTXT_DATA_LEN) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 567 | pr_warn_once("server sent bad preauth context\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 568 | return; |
| 569 | } |
| 570 | if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 571 | pr_warn_once("Invalid SMB3 hash algorithm count\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 572 | if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 573 | pr_warn_once("unknown SMB3 hash algorithm\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 574 | } |
| 575 | |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 576 | static void decode_compress_ctx(struct TCP_Server_Info *server, |
| 577 | struct smb2_compression_capabilities_context *ctxt) |
| 578 | { |
| 579 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 580 | |
| 581 | /* sizeof compress context is a one element compression capbility struct */ |
| 582 | if (len < 10) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 583 | pr_warn_once("server sent bad compression cntxt\n"); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 587 | pr_warn_once("Invalid SMB3 compress algorithm count\n"); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 591 | pr_warn_once("unknown compression algorithm\n"); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 592 | return; |
| 593 | } |
| 594 | server->compress_algorithm = ctxt->CompressionAlgorithms[0]; |
| 595 | } |
| 596 | |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 597 | static int decode_encrypt_ctx(struct TCP_Server_Info *server, |
| 598 | struct smb2_encryption_neg_context *ctxt) |
| 599 | { |
| 600 | unsigned int len = le16_to_cpu(ctxt->DataLength); |
| 601 | |
| 602 | cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len); |
| 603 | if (len < MIN_ENCRYPT_CTXT_DATA_LEN) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 604 | pr_warn_once("server sent bad crypto ctxt len\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 605 | return -EINVAL; |
| 606 | } |
| 607 | |
| 608 | if (le16_to_cpu(ctxt->CipherCount) != 1) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 609 | pr_warn_once("Invalid SMB3.11 cipher count\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 610 | return -EINVAL; |
| 611 | } |
| 612 | cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0])); |
Steve French | 511ac89 | 2020-10-15 00:14:47 -0500 | [diff] [blame] | 613 | if (require_gcm_256) { |
| 614 | if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) { |
| 615 | cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n"); |
| 616 | return -EOPNOTSUPP; |
| 617 | } |
| 618 | } else if (ctxt->Ciphers[0] == 0) { |
Steve French | acf96fe | 2020-10-17 03:54:27 -0500 | [diff] [blame^] | 619 | /* |
| 620 | * e.g. if server only supported AES256_CCM (very unlikely) |
| 621 | * or server supported no encryption types or had all disabled. |
| 622 | * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case |
| 623 | * in which mount requested encryption ("seal") checks later |
| 624 | * on during tree connection will return proper rc, but if |
| 625 | * seal not requested by client, since server is allowed to |
| 626 | * return 0 to indicate no supported cipher, we can't fail here |
| 627 | */ |
| 628 | server->cipher_type = 0; |
| 629 | server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION; |
| 630 | pr_warn_once("Server does not support requested encryption types\n"); |
| 631 | return 0; |
Steve French | 511ac89 | 2020-10-15 00:14:47 -0500 | [diff] [blame] | 632 | } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) && |
| 633 | (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) && |
| 634 | (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) { |
| 635 | /* server returned a cipher we didn't ask for */ |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 636 | pr_warn_once("Invalid SMB3.11 cipher returned\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 637 | return -EINVAL; |
| 638 | } |
| 639 | server->cipher_type = ctxt->Ciphers[0]; |
Steve French | 23657ad | 2018-04-22 15:14:58 -0500 | [diff] [blame] | 640 | server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION; |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp, |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 645 | struct TCP_Server_Info *server, |
| 646 | unsigned int len_of_smb) |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 647 | { |
| 648 | struct smb2_neg_context *pctx; |
| 649 | unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset); |
| 650 | unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 651 | unsigned int len_of_ctxts, i; |
| 652 | int rc = 0; |
| 653 | |
| 654 | cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt); |
| 655 | if (len_of_smb <= offset) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 656 | cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 657 | return -EINVAL; |
| 658 | } |
| 659 | |
| 660 | len_of_ctxts = len_of_smb - offset; |
| 661 | |
| 662 | for (i = 0; i < ctxt_cnt; i++) { |
| 663 | int clen; |
| 664 | /* check that offset is not beyond end of SMB */ |
| 665 | if (len_of_ctxts == 0) |
| 666 | break; |
| 667 | |
| 668 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 669 | break; |
| 670 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 671 | pctx = (struct smb2_neg_context *)(offset + (char *)rsp); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 672 | clen = le16_to_cpu(pctx->DataLength); |
| 673 | if (clen > len_of_ctxts) |
| 674 | break; |
| 675 | |
| 676 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) |
| 677 | decode_preauth_context( |
| 678 | (struct smb2_preauth_neg_context *)pctx); |
| 679 | else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) |
| 680 | rc = decode_encrypt_ctx(server, |
| 681 | (struct smb2_encryption_neg_context *)pctx); |
Steve French | 26ea888 | 2019-04-26 20:36:08 -0700 | [diff] [blame] | 682 | else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) |
| 683 | decode_compress_ctx(server, |
| 684 | (struct smb2_compression_capabilities_context *)pctx); |
Steve French | fcef0db | 2018-05-19 20:45:27 -0500 | [diff] [blame] | 685 | else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) |
| 686 | server->posix_ext_supported = true; |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 687 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 688 | cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n", |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 689 | le16_to_cpu(pctx->ContextType)); |
| 690 | |
| 691 | if (rc) |
| 692 | break; |
| 693 | /* offsets must be 8 byte aligned */ |
| 694 | clen = (clen + 7) & ~0x7; |
| 695 | offset += clen + sizeof(struct smb2_neg_context); |
| 696 | len_of_ctxts -= clen; |
| 697 | } |
| 698 | return rc; |
| 699 | } |
| 700 | |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 701 | static struct create_posix * |
| 702 | create_posix_buf(umode_t mode) |
| 703 | { |
| 704 | struct create_posix *buf; |
| 705 | |
| 706 | buf = kzalloc(sizeof(struct create_posix), |
| 707 | GFP_KERNEL); |
| 708 | if (!buf) |
| 709 | return NULL; |
| 710 | |
| 711 | buf->ccontext.DataOffset = |
| 712 | cpu_to_le16(offsetof(struct create_posix, Mode)); |
| 713 | buf->ccontext.DataLength = cpu_to_le32(4); |
| 714 | buf->ccontext.NameOffset = |
| 715 | cpu_to_le16(offsetof(struct create_posix, Name)); |
| 716 | buf->ccontext.NameLength = cpu_to_le16(16); |
| 717 | |
| 718 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 719 | buf->Name[0] = 0x93; |
| 720 | buf->Name[1] = 0xAD; |
| 721 | buf->Name[2] = 0x25; |
| 722 | buf->Name[3] = 0x50; |
| 723 | buf->Name[4] = 0x9C; |
| 724 | buf->Name[5] = 0xB4; |
| 725 | buf->Name[6] = 0x11; |
| 726 | buf->Name[7] = 0xE7; |
| 727 | buf->Name[8] = 0xB4; |
| 728 | buf->Name[9] = 0x23; |
| 729 | buf->Name[10] = 0x83; |
| 730 | buf->Name[11] = 0xDE; |
| 731 | buf->Name[12] = 0x96; |
| 732 | buf->Name[13] = 0x8B; |
| 733 | buf->Name[14] = 0xCD; |
| 734 | buf->Name[15] = 0x7C; |
| 735 | buf->Mode = cpu_to_le32(mode); |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 736 | cifs_dbg(FYI, "mode on posix create 0%o\n", mode); |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 737 | return buf; |
| 738 | } |
| 739 | |
| 740 | static int |
| 741 | add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode) |
| 742 | { |
| 743 | struct smb2_create_req *req = iov[0].iov_base; |
| 744 | unsigned int num = *num_iovec; |
| 745 | |
| 746 | iov[num].iov_base = create_posix_buf(mode); |
Steve French | d0959b0 | 2019-10-05 10:53:58 -0500 | [diff] [blame] | 747 | if (mode == ACL_NO_MODE) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 748 | cifs_dbg(FYI, "Invalid mode\n"); |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 749 | if (iov[num].iov_base == NULL) |
| 750 | return -ENOMEM; |
| 751 | iov[num].iov_len = sizeof(struct create_posix); |
| 752 | if (!req->CreateContextsOffset) |
| 753 | req->CreateContextsOffset = cpu_to_le32( |
| 754 | sizeof(struct smb2_create_req) + |
| 755 | iov[num - 1].iov_len); |
| 756 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix)); |
| 757 | *num_iovec = num + 1; |
| 758 | return 0; |
| 759 | } |
| 760 | |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 761 | |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 762 | /* |
| 763 | * |
| 764 | * SMB2 Worker functions follow: |
| 765 | * |
| 766 | * The general structure of the worker functions is: |
| 767 | * 1) Call smb2_init (assembles SMB2 header) |
| 768 | * 2) Initialize SMB2 command specific fields in fixed length area of SMB |
| 769 | * 3) Call smb_sendrcv2 (sends request on socket and waits for response) |
| 770 | * 4) Decode SMB2 command specific fields in the fixed length area |
| 771 | * 5) Decode variable length data area (if any for this SMB2 command type) |
| 772 | * 6) Call free smb buffer |
| 773 | * 7) return |
| 774 | * |
| 775 | */ |
| 776 | |
| 777 | int |
| 778 | SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses) |
| 779 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 780 | struct smb_rqst rqst; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 781 | struct smb2_negotiate_req *req; |
| 782 | struct smb2_negotiate_rsp *rsp; |
| 783 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 784 | struct kvec rsp_iov; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 785 | int rc = 0; |
| 786 | int resp_buftype; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 787 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 788 | int blob_offset, blob_length; |
| 789 | char *security_blob; |
| 790 | int flags = CIFS_NEG_OP; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 791 | unsigned int total_len; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 792 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 793 | cifs_dbg(FYI, "Negotiate protocol\n"); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 794 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 795 | if (!server) { |
| 796 | WARN(1, "%s: server is NULL!\n", __func__); |
| 797 | return -EIO; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 798 | } |
| 799 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 800 | rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server, |
| 801 | (void **) &req, &total_len); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 802 | if (rc) |
| 803 | return rc; |
| 804 | |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 805 | req->sync_hdr.SessionId = 0; |
Steve French | 0fdfef9 | 2018-06-28 19:30:23 -0500 | [diff] [blame] | 806 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 807 | memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); |
| 808 | memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 809 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 810 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 811 | SMB3ANY_VERSION_STRING) == 0) { |
| 812 | req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); |
| 813 | req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); |
| 814 | req->DialectCount = cpu_to_le16(2); |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 815 | total_len += 4; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 816 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 817 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 818 | req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); |
| 819 | req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); |
| 820 | req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 821 | req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); |
| 822 | req->DialectCount = cpu_to_le16(4); |
| 823 | total_len += 8; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 824 | } else { |
| 825 | /* otherwise send specific dialect */ |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 826 | req->Dialects[0] = cpu_to_le16(server->vals->protocol_id); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 827 | req->DialectCount = cpu_to_le16(1); |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 828 | total_len += 2; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 829 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 830 | |
| 831 | /* only one of SMB2 signing flags may be set in SMB2 request */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 832 | if (ses->sign) |
Steve French | 9cd2e62 | 2013-06-12 19:59:03 -0500 | [diff] [blame] | 833 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 834 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
Steve French | 9cd2e62 | 2013-06-12 19:59:03 -0500 | [diff] [blame] | 835 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 836 | else |
| 837 | req->SecurityMode = 0; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 838 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 839 | req->Capabilities = cpu_to_le32(server->vals->req_capabilities); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 840 | |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 841 | /* ClientGUID must be zero for SMB2.02 dialect */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 842 | if (server->vals->protocol_id == SMB20_PROT_ID) |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 843 | memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 844 | else { |
Steve French | 3c5f9be1 | 2014-05-13 13:37:45 -0700 | [diff] [blame] | 845 | memcpy(req->ClientGUID, server->client_guid, |
| 846 | SMB2_CLIENT_GUID_SIZE); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 847 | if ((server->vals->protocol_id == SMB311_PROT_ID) || |
| 848 | (strcmp(server->vals->version_string, |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 849 | SMBDEFAULT_VERSION_STRING) == 0)) |
Steve French | 9fe5ff1 | 2019-06-24 20:39:04 -0500 | [diff] [blame] | 850 | assemble_neg_contexts(req, server, &total_len); |
Steve French | ebb3a9d | 2015-06-18 04:49:47 -0500 | [diff] [blame] | 851 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 852 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 13cacea | 2017-11-20 11:24:30 +1100 | [diff] [blame] | 853 | iov[0].iov_len = total_len; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 854 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 855 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 856 | rqst.rq_iov = iov; |
| 857 | rqst.rq_nvec = 1; |
| 858 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 859 | rc = cifs_send_recv(xid, ses, server, |
| 860 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 861 | cifs_small_buf_release(req); |
| 862 | rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 863 | /* |
| 864 | * No tcon so can't do |
| 865 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); |
| 866 | */ |
Steve French | 7e682f7 | 2017-08-31 21:34:24 -0500 | [diff] [blame] | 867 | if (rc == -EOPNOTSUPP) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 868 | cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n"); |
Steve French | 7e682f7 | 2017-08-31 21:34:24 -0500 | [diff] [blame] | 869 | goto neg_exit; |
| 870 | } else if (rc != 0) |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 871 | goto neg_exit; |
| 872 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 873 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 874 | SMB3ANY_VERSION_STRING) == 0) { |
| 875 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 876 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 877 | "SMB2 dialect returned but not requested\n"); |
| 878 | return -EIO; |
| 879 | } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 880 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 881 | "SMB2.1 dialect returned but not requested\n"); |
| 882 | return -EIO; |
| 883 | } |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 884 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 885 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 886 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 887 | cifs_server_dbg(VFS, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 888 | "SMB2 dialect returned but not requested\n"); |
| 889 | return -EIO; |
| 890 | } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) { |
| 891 | /* ops set to 3.0 by default for default so update */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 892 | server->ops = &smb21_operations; |
| 893 | server->vals = &smb21_values; |
ZhangXiaoxu | b57a55e | 2019-04-06 15:30:38 +0800 | [diff] [blame] | 894 | } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 895 | server->ops = &smb311_operations; |
| 896 | server->vals = &smb311_values; |
ZhangXiaoxu | b57a55e | 2019-04-06 15:30:38 +0800 | [diff] [blame] | 897 | } |
Steve French | 590d08d | 2017-09-19 11:43:47 -0500 | [diff] [blame] | 898 | } else if (le16_to_cpu(rsp->DialectRevision) != |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 899 | server->vals->protocol_id) { |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 900 | /* if requested single dialect ensure returned dialect matched */ |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 901 | cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n", |
| 902 | le16_to_cpu(rsp->DialectRevision)); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 903 | return -EIO; |
| 904 | } |
| 905 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 906 | cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 907 | |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 908 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 909 | cifs_dbg(FYI, "negotiated smb2.0 dialect\n"); |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 910 | else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 911 | cifs_dbg(FYI, "negotiated smb2.1 dialect\n"); |
Steve French | e4aa25e | 2012-10-01 12:26:22 -0500 | [diff] [blame] | 912 | else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 913 | cifs_dbg(FYI, "negotiated smb3.0 dialect\n"); |
Steve French | 20b6d8b | 2013-06-12 22:48:41 -0500 | [diff] [blame] | 914 | else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID)) |
| 915 | cifs_dbg(FYI, "negotiated smb3.02 dialect\n"); |
Steve French | 5f7fbf7 | 2014-12-17 22:52:58 -0600 | [diff] [blame] | 916 | else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) |
| 917 | cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n"); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 918 | else { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 919 | cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n", |
| 920 | le16_to_cpu(rsp->DialectRevision)); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 921 | rc = -EIO; |
| 922 | goto neg_exit; |
| 923 | } |
| 924 | server->dialect = le16_to_cpu(rsp->DialectRevision); |
| 925 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 926 | /* |
| 927 | * Keep a copy of the hash after negprot. This hash will be |
| 928 | * the starting hash value for all sessions made from this |
| 929 | * server. |
| 930 | */ |
| 931 | memcpy(server->preauth_sha_hash, ses->preauth_sha_hash, |
| 932 | SMB2_PREAUTH_HASH_SIZE); |
Steve French | 0fdfef9 | 2018-06-28 19:30:23 -0500 | [diff] [blame] | 933 | |
Jeff Layton | e598d1d8 | 2013-05-26 07:00:59 -0400 | [diff] [blame] | 934 | /* SMB2 only has an extended negflavor */ |
| 935 | server->negflavor = CIFS_NEGFLAVOR_EXTENDED; |
Pavel Shilovsky | 2365c4e | 2014-02-14 13:31:02 +0400 | [diff] [blame] | 936 | /* set it to the maximum buffer size value we can send with 1 credit */ |
| 937 | server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize), |
| 938 | SMB2_MAX_BUFFER_SIZE); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 939 | server->max_read = le32_to_cpu(rsp->MaxReadSize); |
| 940 | server->max_write = le32_to_cpu(rsp->MaxWriteSize); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 941 | server->sec_mode = le16_to_cpu(rsp->SecurityMode); |
Steve French | 07108d0 | 2018-04-01 20:15:55 -0500 | [diff] [blame] | 942 | if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode) |
| 943 | cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n", |
| 944 | server->sec_mode); |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 945 | server->capabilities = le32_to_cpu(rsp->Capabilities); |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 946 | /* Internal types */ |
| 947 | server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 948 | |
| 949 | security_blob = smb2_get_data_area_len(&blob_offset, &blob_length, |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 950 | (struct smb2_sync_hdr *)rsp); |
Steve French | 5d875cc | 2013-06-25 15:33:41 -0500 | [diff] [blame] | 951 | /* |
| 952 | * See MS-SMB2 section 2.2.4: if no blob, client picks default which |
| 953 | * for us will be |
| 954 | * ses->sectype = RawNTLMSSP; |
| 955 | * but for time being this is our only auth choice so doesn't matter. |
| 956 | * We just found a server which sets blob length to zero expecting raw. |
| 957 | */ |
Pavel Shilovsky | 67dbea2 | 2017-04-12 13:32:07 -0700 | [diff] [blame] | 958 | if (blob_length == 0) { |
Steve French | 5d875cc | 2013-06-25 15:33:41 -0500 | [diff] [blame] | 959 | cifs_dbg(FYI, "missing security blob on negprot\n"); |
Pavel Shilovsky | 67dbea2 | 2017-04-12 13:32:07 -0700 | [diff] [blame] | 960 | server->sec_ntlmssp = true; |
| 961 | } |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 962 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 963 | rc = cifs_enable_signing(server, ses->sign); |
Jeff Layton | 9ddec56 | 2013-05-26 07:00:58 -0400 | [diff] [blame] | 964 | if (rc) |
| 965 | goto neg_exit; |
Steve French | ceb1b0b | 2015-09-24 00:52:37 -0500 | [diff] [blame] | 966 | if (blob_length) { |
Steve French | ebdd207 | 2014-10-20 12:48:23 -0500 | [diff] [blame] | 967 | rc = decode_negTokenInit(security_blob, blob_length, server); |
Steve French | ceb1b0b | 2015-09-24 00:52:37 -0500 | [diff] [blame] | 968 | if (rc == 1) |
| 969 | rc = 0; |
| 970 | else if (rc == 0) |
| 971 | rc = -EIO; |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 972 | } |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 973 | |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 974 | if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) { |
| 975 | if (rsp->NegotiateContextCount) |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 976 | rc = smb311_decode_neg_context(rsp, server, |
| 977 | rsp_iov.iov_len); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 978 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 979 | cifs_server_dbg(VFS, "Missing expected negotiate contexts\n"); |
Steve French | 5100d8a | 2018-04-09 10:47:14 -0500 | [diff] [blame] | 980 | } |
Pavel Shilovsky | ec2e452 | 2011-12-27 16:12:43 +0400 | [diff] [blame] | 981 | neg_exit: |
| 982 | free_rsp_buf(resp_buftype, rsp); |
| 983 | return rc; |
| 984 | } |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 985 | |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 986 | int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon) |
| 987 | { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 988 | int rc; |
| 989 | struct validate_negotiate_info_req *pneg_inbuf; |
David Disseldorp | fe83bebc | 2017-10-20 14:49:37 +0200 | [diff] [blame] | 990 | struct validate_negotiate_info_rsp *pneg_rsp = NULL; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 991 | u32 rsplen; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 992 | u32 inbuflen; /* max of 4 dialects */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 993 | struct TCP_Server_Info *server = tcon->ses->server; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 994 | |
| 995 | cifs_dbg(FYI, "validate negotiate\n"); |
| 996 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 997 | /* In SMB3.11 preauth integrity supersedes validate negotiate */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 998 | if (server->dialect == SMB311_PROT_ID) |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 999 | return 0; |
| 1000 | |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1001 | /* |
| 1002 | * validation ioctl must be signed, so no point sending this if we |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1003 | * can not sign it (ie are not known user). Even if signing is not |
| 1004 | * required (enabled but not negotiated), in those cases we selectively |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1005 | * sign just this, the first and only signed request on a connection. |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1006 | * Having validation of negotiate info helps reduce attack vectors. |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1007 | */ |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1008 | if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1009 | return 0; /* validation requires signing */ |
| 1010 | |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1011 | if (tcon->ses->user_name == NULL) { |
| 1012 | cifs_dbg(FYI, "Can't validate negotiate: null user mount\n"); |
| 1013 | return 0; /* validation requires signing */ |
| 1014 | } |
| 1015 | |
| 1016 | if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1017 | cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n"); |
Steve French | 0603c96 | 2017-09-20 19:57:18 -0500 | [diff] [blame] | 1018 | |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1019 | pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS); |
| 1020 | if (!pneg_inbuf) |
| 1021 | return -ENOMEM; |
| 1022 | |
| 1023 | pneg_inbuf->Capabilities = |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1024 | cpu_to_le32(server->vals->req_capabilities); |
| 1025 | memcpy(pneg_inbuf->Guid, server->client_guid, |
Sachin Prabhu | 39552ea | 2014-05-13 00:48:12 +0100 | [diff] [blame] | 1026 | SMB2_CLIENT_GUID_SIZE); |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1027 | |
| 1028 | if (tcon->ses->sign) |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1029 | pneg_inbuf->SecurityMode = |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1030 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
| 1031 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1032 | pneg_inbuf->SecurityMode = |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1033 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
| 1034 | else |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1035 | pneg_inbuf->SecurityMode = 0; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1036 | |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1037 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1038 | if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1039 | SMB3ANY_VERSION_STRING) == 0) { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1040 | pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); |
| 1041 | pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); |
| 1042 | pneg_inbuf->DialectCount = cpu_to_le16(2); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1043 | /* structure is big enough for 3 dialects, sending only 2 */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1044 | inbuflen = sizeof(*pneg_inbuf) - |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 1045 | (2 * sizeof(pneg_inbuf->Dialects[0])); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1046 | } else if (strcmp(server->vals->version_string, |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1047 | SMBDEFAULT_VERSION_STRING) == 0) { |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1048 | pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); |
| 1049 | pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); |
| 1050 | pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); |
Steve French | d5c7076 | 2019-01-03 02:37:21 -0600 | [diff] [blame] | 1051 | pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); |
| 1052 | pneg_inbuf->DialectCount = cpu_to_le16(4); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1053 | /* structure is big enough for 3 dialects */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1054 | inbuflen = sizeof(*pneg_inbuf); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1055 | } else { |
| 1056 | /* otherwise specific dialect was requested */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1057 | pneg_inbuf->Dialects[0] = |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1058 | cpu_to_le16(server->vals->protocol_id); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1059 | pneg_inbuf->DialectCount = cpu_to_le16(1); |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1060 | /* structure is big enough for 3 dialects, sending only 1 */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1061 | inbuflen = sizeof(*pneg_inbuf) - |
| 1062 | sizeof(pneg_inbuf->Dialects[0]) * 2; |
Steve French | 9764c02 | 2017-09-17 10:41:35 -0500 | [diff] [blame] | 1063 | } |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1064 | |
| 1065 | rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID, |
| 1066 | FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 1067 | (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize, |
| 1068 | (char **)&pneg_rsp, &rsplen); |
Namjae Jeon | 969ae8e | 2019-01-22 09:46:45 +0900 | [diff] [blame] | 1069 | if (rc == -EOPNOTSUPP) { |
| 1070 | /* |
| 1071 | * Old Windows versions or Netapp SMB server can return |
| 1072 | * not supported error. Client should accept it. |
| 1073 | */ |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1074 | cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n"); |
Colin Ian King | 2107820 | 2019-05-17 09:12:33 +0100 | [diff] [blame] | 1075 | rc = 0; |
| 1076 | goto out_free_inbuf; |
Namjae Jeon | 969ae8e | 2019-01-22 09:46:45 +0900 | [diff] [blame] | 1077 | } else if (rc != 0) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1078 | cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", |
| 1079 | rc); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1080 | rc = -EIO; |
| 1081 | goto out_free_inbuf; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1082 | } |
| 1083 | |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1084 | rc = -EIO; |
| 1085 | if (rsplen != sizeof(*pneg_rsp)) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1086 | cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n", |
| 1087 | rsplen); |
Steve French | 7db0a6e | 2017-05-03 21:12:20 -0500 | [diff] [blame] | 1088 | |
| 1089 | /* relax check since Mac returns max bufsize allowed on ioctl */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1090 | if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp)) |
| 1091 | goto out_free_rsp; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* check validate negotiate info response matches what we got earlier */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1095 | if (pneg_rsp->Dialect != cpu_to_le16(server->dialect)) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1096 | goto vneg_out; |
| 1097 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1098 | if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode)) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1099 | goto vneg_out; |
| 1100 | |
| 1101 | /* do not validate server guid because not saved at negprot time yet */ |
| 1102 | |
| 1103 | if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1104 | SMB2_LARGE_FILES) != server->capabilities) |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1105 | goto vneg_out; |
| 1106 | |
| 1107 | /* validate negotiate successful */ |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1108 | rc = 0; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1109 | cifs_dbg(FYI, "validate negotiate info successful\n"); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1110 | goto out_free_rsp; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1111 | |
| 1112 | vneg_out: |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1113 | cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n"); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1114 | out_free_rsp: |
David Disseldorp | fe83bebc | 2017-10-20 14:49:37 +0200 | [diff] [blame] | 1115 | kfree(pneg_rsp); |
Long Li | 2796d30 | 2018-04-25 11:30:04 -0700 | [diff] [blame] | 1116 | out_free_inbuf: |
| 1117 | kfree(pneg_inbuf); |
| 1118 | return rc; |
Steve French | ff1c038 | 2013-11-19 23:44:46 -0600 | [diff] [blame] | 1119 | } |
| 1120 | |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1121 | enum securityEnum |
| 1122 | smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested) |
| 1123 | { |
| 1124 | switch (requested) { |
| 1125 | case Kerberos: |
| 1126 | case RawNTLMSSP: |
| 1127 | return requested; |
| 1128 | case NTLMv2: |
| 1129 | return RawNTLMSSP; |
| 1130 | case Unspecified: |
| 1131 | if (server->sec_ntlmssp && |
| 1132 | (global_secflags & CIFSSEC_MAY_NTLMSSP)) |
| 1133 | return RawNTLMSSP; |
| 1134 | if ((server->sec_kerberos || server->sec_mskerberos) && |
| 1135 | (global_secflags & CIFSSEC_MAY_KRB5)) |
| 1136 | return Kerberos; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1137 | fallthrough; |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1138 | default: |
| 1139 | return Unspecified; |
| 1140 | } |
| 1141 | } |
| 1142 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1143 | struct SMB2_sess_data { |
| 1144 | unsigned int xid; |
| 1145 | struct cifs_ses *ses; |
| 1146 | struct nls_table *nls_cp; |
| 1147 | void (*func)(struct SMB2_sess_data *); |
| 1148 | int result; |
| 1149 | u64 previous_session; |
| 1150 | |
| 1151 | /* we will send the SMB in three pieces: |
| 1152 | * a fixed length beginning part, an optional |
| 1153 | * SPNEGO blob (which can be zero length), and a |
| 1154 | * last part which will include the strings |
| 1155 | * and rest of bcc area. This allows us to avoid |
| 1156 | * a large buffer 17K allocation |
| 1157 | */ |
| 1158 | int buf0_type; |
| 1159 | struct kvec iov[2]; |
| 1160 | }; |
| 1161 | |
| 1162 | static int |
| 1163 | SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data) |
| 1164 | { |
| 1165 | int rc; |
| 1166 | struct cifs_ses *ses = sess_data->ses; |
| 1167 | struct smb2_sess_setup_req *req; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1168 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1169 | unsigned int total_len; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1170 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1171 | rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server, |
| 1172 | (void **) &req, |
| 1173 | &total_len); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1174 | if (rc) |
| 1175 | return rc; |
| 1176 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1177 | if (sess_data->ses->binding) { |
| 1178 | req->sync_hdr.SessionId = sess_data->ses->Suid; |
| 1179 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
| 1180 | req->PreviousSessionId = 0; |
| 1181 | req->Flags = SMB2_SESSION_REQ_FLAG_BINDING; |
| 1182 | } else { |
| 1183 | /* First session, not a reauthenticate */ |
| 1184 | req->sync_hdr.SessionId = 0; |
| 1185 | /* |
| 1186 | * if reconnect, we need to send previous sess id |
| 1187 | * otherwise it is 0 |
| 1188 | */ |
| 1189 | req->PreviousSessionId = sess_data->previous_session; |
| 1190 | req->Flags = 0; /* MBZ */ |
| 1191 | } |
Steve French | d409014 | 2018-06-13 17:05:58 -0500 | [diff] [blame] | 1192 | |
| 1193 | /* enough to enable echos and oplocks and one max size write */ |
| 1194 | req->sync_hdr.CreditRequest = cpu_to_le16(130); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1195 | |
| 1196 | /* only one of SMB2 signing flags may be set in SMB2 request */ |
| 1197 | if (server->sign) |
| 1198 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED; |
| 1199 | else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */ |
| 1200 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED; |
| 1201 | else |
| 1202 | req->SecurityMode = 0; |
| 1203 | |
Steve French | 8d33096 | 2019-07-25 18:13:10 -0500 | [diff] [blame] | 1204 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 1205 | req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS); |
| 1206 | #else |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1207 | req->Capabilities = 0; |
Steve French | 8d33096 | 2019-07-25 18:13:10 -0500 | [diff] [blame] | 1208 | #endif /* DFS_UPCALL */ |
| 1209 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1210 | req->Channel = 0; /* MBZ */ |
| 1211 | |
| 1212 | sess_data->iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1213 | /* 1 for pad */ |
| 1214 | sess_data->iov[0].iov_len = total_len - 1; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1215 | /* |
| 1216 | * This variable will be used to clear the buffer |
| 1217 | * allocated above in case of any error in the calling function. |
| 1218 | */ |
| 1219 | sess_data->buf0_type = CIFS_SMALL_BUFFER; |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | static void |
| 1225 | SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data) |
| 1226 | { |
| 1227 | free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base); |
| 1228 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1229 | } |
| 1230 | |
| 1231 | static int |
| 1232 | SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data) |
| 1233 | { |
| 1234 | int rc; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1235 | struct smb_rqst rqst; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1236 | struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1237 | struct kvec rsp_iov = { NULL, 0 }; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1238 | |
| 1239 | /* Testing shows that buffer offset must be at location of Buffer[0] */ |
| 1240 | req->SecurityBufferOffset = |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1241 | cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1242 | req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len); |
| 1243 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1244 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1245 | rqst.rq_iov = sess_data->iov; |
| 1246 | rqst.rq_nvec = 2; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1247 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1248 | /* BB add code to build os and lm fields */ |
| 1249 | rc = cifs_send_recv(sess_data->xid, sess_data->ses, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1250 | cifs_ses_server(sess_data->ses), |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1251 | &rqst, |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1252 | &sess_data->buf0_type, |
| 1253 | CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1254 | cifs_small_buf_release(sess_data->iov[0].iov_base); |
| 1255 | memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1256 | |
| 1257 | return rc; |
| 1258 | } |
| 1259 | |
| 1260 | static int |
| 1261 | SMB2_sess_establish_session(struct SMB2_sess_data *sess_data) |
| 1262 | { |
| 1263 | int rc = 0; |
| 1264 | struct cifs_ses *ses = sess_data->ses; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1265 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1266 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1267 | mutex_lock(&server->srv_mutex); |
| 1268 | if (server->ops->generate_signingkey) { |
| 1269 | rc = server->ops->generate_signingkey(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1270 | if (rc) { |
| 1271 | cifs_dbg(FYI, |
| 1272 | "SMB3 session key generation failed\n"); |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1273 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | cabfb36 | 2016-11-07 18:20:50 -0800 | [diff] [blame] | 1274 | return rc; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1275 | } |
| 1276 | } |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1277 | if (!server->session_estab) { |
| 1278 | server->sequence_number = 0x2; |
| 1279 | server->session_estab = true; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1280 | } |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1281 | mutex_unlock(&server->srv_mutex); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1282 | |
| 1283 | cifs_dbg(FYI, "SMB2/3 session established successfully\n"); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1284 | /* keep existing ses state if binding */ |
| 1285 | if (!ses->binding) { |
| 1286 | spin_lock(&GlobalMid_Lock); |
| 1287 | ses->status = CifsGood; |
| 1288 | ses->need_reconnect = false; |
| 1289 | spin_unlock(&GlobalMid_Lock); |
| 1290 | } |
| 1291 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1292 | return rc; |
| 1293 | } |
| 1294 | |
| 1295 | #ifdef CONFIG_CIFS_UPCALL |
| 1296 | static void |
| 1297 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) |
| 1298 | { |
| 1299 | int rc; |
| 1300 | struct cifs_ses *ses = sess_data->ses; |
| 1301 | struct cifs_spnego_msg *msg; |
| 1302 | struct key *spnego_key = NULL; |
| 1303 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1304 | |
| 1305 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1306 | if (rc) |
| 1307 | goto out; |
| 1308 | |
| 1309 | spnego_key = cifs_get_spnego_key(ses); |
| 1310 | if (IS_ERR(spnego_key)) { |
| 1311 | rc = PTR_ERR(spnego_key); |
Steve French | 0a01894 | 2020-07-16 00:34:21 -0500 | [diff] [blame] | 1312 | if (rc == -ENOKEY) |
| 1313 | cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n"); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1314 | spnego_key = NULL; |
| 1315 | goto out; |
| 1316 | } |
| 1317 | |
| 1318 | msg = spnego_key->payload.data[0]; |
| 1319 | /* |
| 1320 | * check version field to make sure that cifs.upcall is |
| 1321 | * sending us a response in an expected form |
| 1322 | */ |
| 1323 | if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1324 | cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n", |
| 1325 | CIFS_SPNEGO_UPCALL_VERSION, msg->version); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1326 | rc = -EKEYREJECTED; |
| 1327 | goto out_put_spnego_key; |
| 1328 | } |
| 1329 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1330 | /* keep session key if binding */ |
| 1331 | if (!ses->binding) { |
| 1332 | ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len, |
| 1333 | GFP_KERNEL); |
| 1334 | if (!ses->auth_key.response) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1335 | cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n", |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1336 | msg->sesskey_len); |
| 1337 | rc = -ENOMEM; |
| 1338 | goto out_put_spnego_key; |
| 1339 | } |
| 1340 | ses->auth_key.len = msg->sesskey_len; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1341 | } |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1342 | |
| 1343 | sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; |
| 1344 | sess_data->iov[1].iov_len = msg->secblob_len; |
| 1345 | |
| 1346 | rc = SMB2_sess_sendreceive(sess_data); |
| 1347 | if (rc) |
| 1348 | goto out_put_spnego_key; |
| 1349 | |
| 1350 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1351 | /* keep session id and flags if binding */ |
| 1352 | if (!ses->binding) { |
| 1353 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1354 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1355 | } |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1356 | |
| 1357 | rc = SMB2_sess_establish_session(sess_data); |
| 1358 | out_put_spnego_key: |
| 1359 | key_invalidate(spnego_key); |
| 1360 | key_put(spnego_key); |
| 1361 | out: |
| 1362 | sess_data->result = rc; |
| 1363 | sess_data->func = NULL; |
| 1364 | SMB2_sess_free_buffer(sess_data); |
| 1365 | } |
| 1366 | #else |
| 1367 | static void |
| 1368 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) |
| 1369 | { |
| 1370 | cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); |
| 1371 | sess_data->result = -EOPNOTSUPP; |
| 1372 | sess_data->func = NULL; |
| 1373 | } |
| 1374 | #endif |
| 1375 | |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1376 | static void |
| 1377 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data); |
| 1378 | |
| 1379 | static void |
| 1380 | SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data) |
| 1381 | { |
| 1382 | int rc; |
| 1383 | struct cifs_ses *ses = sess_data->ses; |
| 1384 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1385 | char *ntlmssp_blob = NULL; |
| 1386 | bool use_spnego = false; /* else use raw ntlmssp */ |
| 1387 | u16 blob_length = 0; |
| 1388 | |
| 1389 | /* |
| 1390 | * If memory allocation is successful, caller of this function |
| 1391 | * frees it. |
| 1392 | */ |
| 1393 | ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); |
| 1394 | if (!ses->ntlmssp) { |
| 1395 | rc = -ENOMEM; |
| 1396 | goto out_err; |
| 1397 | } |
| 1398 | ses->ntlmssp->sesskey_per_smbsess = true; |
| 1399 | |
| 1400 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1401 | if (rc) |
| 1402 | goto out_err; |
| 1403 | |
| 1404 | ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE), |
| 1405 | GFP_KERNEL); |
| 1406 | if (ntlmssp_blob == NULL) { |
| 1407 | rc = -ENOMEM; |
| 1408 | goto out; |
| 1409 | } |
| 1410 | |
| 1411 | build_ntlmssp_negotiate_blob(ntlmssp_blob, ses); |
| 1412 | if (use_spnego) { |
| 1413 | /* BB eventually need to add this */ |
| 1414 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
| 1415 | rc = -EOPNOTSUPP; |
| 1416 | goto out; |
| 1417 | } else { |
| 1418 | blob_length = sizeof(struct _NEGOTIATE_MESSAGE); |
| 1419 | /* with raw NTLMSSP we don't encapsulate in SPNEGO */ |
| 1420 | } |
| 1421 | sess_data->iov[1].iov_base = ntlmssp_blob; |
| 1422 | sess_data->iov[1].iov_len = blob_length; |
| 1423 | |
| 1424 | rc = SMB2_sess_sendreceive(sess_data); |
| 1425 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
| 1426 | |
| 1427 | /* If true, rc here is expected and not an error */ |
| 1428 | if (sess_data->buf0_type != CIFS_NO_BUFFER && |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1429 | rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1430 | rc = 0; |
| 1431 | |
| 1432 | if (rc) |
| 1433 | goto out; |
| 1434 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 1435 | if (offsetof(struct smb2_sess_setup_rsp, Buffer) != |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1436 | le16_to_cpu(rsp->SecurityBufferOffset)) { |
| 1437 | cifs_dbg(VFS, "Invalid security buffer offset %d\n", |
| 1438 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 1439 | rc = -EIO; |
| 1440 | goto out; |
| 1441 | } |
| 1442 | rc = decode_ntlmssp_challenge(rsp->Buffer, |
| 1443 | le16_to_cpu(rsp->SecurityBufferLength), ses); |
| 1444 | if (rc) |
| 1445 | goto out; |
| 1446 | |
| 1447 | cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); |
| 1448 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1449 | /* keep existing ses id and flags if binding */ |
| 1450 | if (!ses->binding) { |
| 1451 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1452 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1453 | } |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1454 | |
| 1455 | out: |
| 1456 | kfree(ntlmssp_blob); |
| 1457 | SMB2_sess_free_buffer(sess_data); |
| 1458 | if (!rc) { |
| 1459 | sess_data->result = 0; |
| 1460 | sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate; |
| 1461 | return; |
| 1462 | } |
| 1463 | out_err: |
| 1464 | kfree(ses->ntlmssp); |
| 1465 | ses->ntlmssp = NULL; |
| 1466 | sess_data->result = rc; |
| 1467 | sess_data->func = NULL; |
| 1468 | } |
| 1469 | |
| 1470 | static void |
| 1471 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data) |
| 1472 | { |
| 1473 | int rc; |
| 1474 | struct cifs_ses *ses = sess_data->ses; |
| 1475 | struct smb2_sess_setup_req *req; |
| 1476 | struct smb2_sess_setup_rsp *rsp = NULL; |
| 1477 | unsigned char *ntlmssp_blob = NULL; |
| 1478 | bool use_spnego = false; /* else use raw ntlmssp */ |
| 1479 | u16 blob_length = 0; |
| 1480 | |
| 1481 | rc = SMB2_sess_alloc_buffer(sess_data); |
| 1482 | if (rc) |
| 1483 | goto out; |
| 1484 | |
| 1485 | req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base; |
Ronnie Sahlberg | 88ea5cb | 2017-11-20 11:24:36 +1100 | [diff] [blame] | 1486 | req->sync_hdr.SessionId = ses->Suid; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1487 | |
| 1488 | rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses, |
| 1489 | sess_data->nls_cp); |
| 1490 | if (rc) { |
| 1491 | cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc); |
| 1492 | goto out; |
| 1493 | } |
| 1494 | |
| 1495 | if (use_spnego) { |
| 1496 | /* BB eventually need to add this */ |
| 1497 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
| 1498 | rc = -EOPNOTSUPP; |
| 1499 | goto out; |
| 1500 | } |
| 1501 | sess_data->iov[1].iov_base = ntlmssp_blob; |
| 1502 | sess_data->iov[1].iov_len = blob_length; |
| 1503 | |
| 1504 | rc = SMB2_sess_sendreceive(sess_data); |
| 1505 | if (rc) |
| 1506 | goto out; |
| 1507 | |
| 1508 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; |
| 1509 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1510 | /* keep existing ses id and flags if binding */ |
| 1511 | if (!ses->binding) { |
| 1512 | ses->Suid = rsp->sync_hdr.SessionId; |
| 1513 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
| 1514 | } |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1515 | |
| 1516 | rc = SMB2_sess_establish_session(sess_data); |
Ronnie Sahlberg | f560cda | 2020-04-12 16:09:26 +1000 | [diff] [blame] | 1517 | #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS |
| 1518 | if (ses->server->dialect < SMB30_PROT_ID) { |
| 1519 | cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__); |
| 1520 | /* |
| 1521 | * The session id is opaque in terms of endianness, so we can't |
| 1522 | * print it as a long long. we dump it as we got it on the wire |
| 1523 | */ |
| 1524 | cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid), |
| 1525 | &ses->Suid); |
| 1526 | cifs_dbg(VFS, "Session Key %*ph\n", |
| 1527 | SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response); |
| 1528 | cifs_dbg(VFS, "Signing Key %*ph\n", |
| 1529 | SMB3_SIGN_KEY_SIZE, ses->auth_key.response); |
| 1530 | } |
| 1531 | #endif |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1532 | out: |
| 1533 | kfree(ntlmssp_blob); |
| 1534 | SMB2_sess_free_buffer(sess_data); |
| 1535 | kfree(ses->ntlmssp); |
| 1536 | ses->ntlmssp = NULL; |
| 1537 | sess_data->result = rc; |
| 1538 | sess_data->func = NULL; |
| 1539 | } |
| 1540 | |
| 1541 | static int |
| 1542 | SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data) |
| 1543 | { |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1544 | int type; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1545 | |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1546 | type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype); |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1547 | cifs_dbg(FYI, "sess setup type %d\n", type); |
| 1548 | if (type == Unspecified) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1549 | cifs_dbg(VFS, "Unable to select appropriate authentication method!\n"); |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1550 | return -EINVAL; |
| 1551 | } |
| 1552 | |
| 1553 | switch (type) { |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1554 | case Kerberos: |
| 1555 | sess_data->func = SMB2_auth_kerberos; |
| 1556 | break; |
| 1557 | case RawNTLMSSP: |
| 1558 | sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate; |
| 1559 | break; |
| 1560 | default: |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1561 | cifs_dbg(VFS, "secType %d not supported!\n", type); |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1562 | return -EOPNOTSUPP; |
| 1563 | } |
| 1564 | |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1568 | int |
| 1569 | SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, |
| 1570 | const struct nls_table *nls_cp) |
| 1571 | { |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1572 | int rc = 0; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1573 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1574 | struct SMB2_sess_data *sess_data; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1575 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1576 | cifs_dbg(FYI, "Session Setup\n"); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1577 | |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 1578 | if (!server) { |
| 1579 | WARN(1, "%s: server is NULL!\n", __func__); |
| 1580 | return -EIO; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1581 | } |
| 1582 | |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1583 | sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL); |
| 1584 | if (!sess_data) |
| 1585 | return -ENOMEM; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1586 | |
| 1587 | rc = SMB2_select_sec(ses, sess_data); |
| 1588 | if (rc) |
| 1589 | goto out; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1590 | sess_data->xid = xid; |
| 1591 | sess_data->ses = ses; |
| 1592 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1593 | sess_data->nls_cp = (struct nls_table *) nls_cp; |
Steve French | b2adf22f | 2018-05-31 15:19:25 -0500 | [diff] [blame] | 1594 | sess_data->previous_session = ses->Suid; |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1595 | |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1596 | /* |
| 1597 | * Initialize the session hash with the server one. |
| 1598 | */ |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 1599 | memcpy(ses->preauth_sha_hash, server->preauth_sha_hash, |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1600 | SMB2_PREAUTH_HASH_SIZE); |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 1601 | |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1602 | while (sess_data->func) |
| 1603 | sess_data->func(sess_data); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1604 | |
Steve French | c721c38 | 2017-09-19 18:40:03 -0500 | [diff] [blame] | 1605 | if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign)) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1606 | cifs_server_dbg(VFS, "signing requested but authenticated as guest\n"); |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1607 | rc = sess_data->result; |
Sachin Prabhu | 166cea4 | 2016-10-07 19:11:22 +0100 | [diff] [blame] | 1608 | out: |
Sachin Prabhu | 3baf1a7 | 2016-10-07 19:11:21 +0100 | [diff] [blame] | 1609 | kfree(sess_data); |
| 1610 | return rc; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | int |
| 1614 | SMB2_logoff(const unsigned int xid, struct cifs_ses *ses) |
| 1615 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1616 | struct smb_rqst rqst; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1617 | struct smb2_logoff_req *req; /* response is also trivial struct */ |
| 1618 | int rc = 0; |
| 1619 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1620 | int flags = 0; |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1621 | unsigned int total_len; |
| 1622 | struct kvec iov[1]; |
| 1623 | struct kvec rsp_iov; |
| 1624 | int resp_buf_type; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1625 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1626 | cifs_dbg(FYI, "disconnect session %p\n", ses); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1627 | |
| 1628 | if (ses && (ses->server)) |
| 1629 | server = ses->server; |
| 1630 | else |
| 1631 | return -EIO; |
| 1632 | |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 1633 | /* no need to send SMB logoff if uid already closed due to reconnect */ |
| 1634 | if (ses->need_reconnect) |
| 1635 | goto smb2_session_already_dead; |
| 1636 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1637 | rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server, |
| 1638 | (void **) &req, &total_len); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1639 | if (rc) |
| 1640 | return rc; |
| 1641 | |
| 1642 | /* since no tcon, smb2_init can not do this, so do here */ |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1643 | req->sync_hdr.SessionId = ses->Suid; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1644 | |
| 1645 | if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) |
| 1646 | flags |= CIFS_TRANSFORM_REQ; |
| 1647 | else if (server->sign) |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1648 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1649 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 1650 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 45305ed | 2017-11-09 12:14:17 +1100 | [diff] [blame] | 1651 | |
| 1652 | iov[0].iov_base = (char *)req; |
| 1653 | iov[0].iov_len = total_len; |
| 1654 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1655 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1656 | rqst.rq_iov = iov; |
| 1657 | rqst.rq_nvec = 1; |
| 1658 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1659 | rc = cifs_send_recv(xid, ses, ses->server, |
| 1660 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1661 | cifs_small_buf_release(req); |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1662 | /* |
| 1663 | * No tcon so can't do |
| 1664 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); |
| 1665 | */ |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 1666 | |
| 1667 | smb2_session_already_dead: |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 1668 | return rc; |
| 1669 | } |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1670 | |
| 1671 | static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code) |
| 1672 | { |
Pavel Shilovsky | d60622e | 2012-05-28 15:19:39 +0400 | [diff] [blame] | 1673 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */) |
| 1677 | |
Steve French | de9f68df | 2013-11-15 11:26:24 -0600 | [diff] [blame] | 1678 | /* These are similar values to what Windows uses */ |
| 1679 | static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon) |
| 1680 | { |
| 1681 | tcon->max_chunks = 256; |
| 1682 | tcon->max_bytes_chunk = 1048576; |
| 1683 | tcon->max_bytes_copy = 16777216; |
| 1684 | } |
| 1685 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1686 | int |
| 1687 | SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, |
| 1688 | struct cifs_tcon *tcon, const struct nls_table *cp) |
| 1689 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1690 | struct smb_rqst rqst; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1691 | struct smb2_tree_connect_req *req; |
| 1692 | struct smb2_tree_connect_rsp *rsp = NULL; |
| 1693 | struct kvec iov[2]; |
Aurélien Aptel | db3b547 | 2017-10-11 13:23:36 +0200 | [diff] [blame] | 1694 | struct kvec rsp_iov = { NULL, 0 }; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1695 | int rc = 0; |
| 1696 | int resp_buftype; |
| 1697 | int unc_path_len; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1698 | __le16 *unc_path = NULL; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1699 | int flags = 0; |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1700 | unsigned int total_len; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1701 | struct TCP_Server_Info *server; |
| 1702 | |
| 1703 | /* always use master channel */ |
| 1704 | server = ses->server; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1705 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1706 | cifs_dbg(FYI, "TCON\n"); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1707 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1708 | if (!server || !tree) |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1709 | return -EIO; |
| 1710 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1711 | unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL); |
| 1712 | if (unc_path == NULL) |
| 1713 | return -ENOMEM; |
| 1714 | |
| 1715 | unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1; |
| 1716 | unc_path_len *= 2; |
| 1717 | if (unc_path_len < 2) { |
| 1718 | kfree(unc_path); |
| 1719 | return -EINVAL; |
| 1720 | } |
| 1721 | |
Jan-Marek Glogowski | 806a28e | 2017-02-20 12:25:58 +0100 | [diff] [blame] | 1722 | /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1723 | tcon->tid = 0; |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 1724 | atomic_set(&tcon->num_remote_opens, 0); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1725 | rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server, |
| 1726 | (void **) &req, &total_len); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1727 | if (rc) { |
| 1728 | kfree(unc_path); |
| 1729 | return rc; |
| 1730 | } |
| 1731 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 1732 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1733 | flags |= CIFS_TRANSFORM_REQ; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1734 | |
| 1735 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1736 | /* 1 for pad */ |
| 1737 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1738 | |
| 1739 | /* Testing shows that buffer offset must be at location of Buffer[0] */ |
| 1740 | req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req) |
Ronnie Sahlberg | 661bb943 | 2017-11-09 12:14:23 +1100 | [diff] [blame] | 1741 | - 1 /* pad */); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1742 | req->PathLength = cpu_to_le16(unc_path_len - 2); |
| 1743 | iov[1].iov_base = unc_path; |
| 1744 | iov[1].iov_len = unc_path_len; |
| 1745 | |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1746 | /* |
| 1747 | * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 |
| 1748 | * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1 |
Steve French | 8c11a60 | 2019-03-22 22:31:17 -0500 | [diff] [blame] | 1749 | * (Samba servers don't always set the flag so also check if null user) |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1750 | */ |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1751 | if ((server->dialect == SMB311_PROT_ID) && |
Ronnie Sahlberg | e71ab2a | 2019-03-21 14:59:02 +1000 | [diff] [blame] | 1752 | !smb3_encryption_required(tcon) && |
Steve French | 8c11a60 | 2019-03-22 22:31:17 -0500 | [diff] [blame] | 1753 | !(ses->session_flags & |
| 1754 | (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) && |
| 1755 | ((ses->user_name != NULL) || (ses->sectype == Kerberos))) |
Steve French | 6188f28 | 2018-03-13 02:29:36 -0500 | [diff] [blame] | 1756 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
| 1757 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1758 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1759 | rqst.rq_iov = iov; |
| 1760 | rqst.rq_nvec = 2; |
| 1761 | |
Steve French | 4fe75c4 | 2019-02-14 01:19:02 -0600 | [diff] [blame] | 1762 | /* Need 64 for max size write so ask for more in case not there yet */ |
| 1763 | req->sync_hdr.CreditRequest = cpu_to_le16(64); |
| 1764 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1765 | rc = cifs_send_recv(xid, ses, server, |
| 1766 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1767 | cifs_small_buf_release(req); |
| 1768 | rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base; |
Steve French | f8af49d | 2018-10-28 00:47:11 -0500 | [diff] [blame] | 1769 | trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1770 | if (rc != 0) { |
| 1771 | if (tcon) { |
| 1772 | cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE); |
| 1773 | tcon->need_reconnect = true; |
| 1774 | } |
| 1775 | goto tcon_error_exit; |
| 1776 | } |
| 1777 | |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1778 | switch (rsp->ShareType) { |
| 1779 | case SMB2_SHARE_TYPE_DISK: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1780 | cifs_dbg(FYI, "connection to disk share\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1781 | break; |
| 1782 | case SMB2_SHARE_TYPE_PIPE: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1783 | tcon->pipe = true; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1784 | cifs_dbg(FYI, "connection to pipe share\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1785 | break; |
| 1786 | case SMB2_SHARE_TYPE_PRINT: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1787 | tcon->print = true; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1788 | cifs_dbg(FYI, "connection to printer\n"); |
Christophe JAILLET | cd12300 | 2017-05-12 17:59:32 +0200 | [diff] [blame] | 1789 | break; |
| 1790 | default: |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1791 | cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1792 | rc = -EOPNOTSUPP; |
| 1793 | goto tcon_error_exit; |
| 1794 | } |
| 1795 | |
| 1796 | tcon->share_flags = le32_to_cpu(rsp->ShareFlags); |
Steve French | 769ee6a | 2013-06-19 14:15:30 -0500 | [diff] [blame] | 1797 | tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */ |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1798 | tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1799 | tcon->tidStatus = CifsGood; |
| 1800 | tcon->need_reconnect = false; |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1801 | tcon->tid = rsp->sync_hdr.TreeId; |
Zhao Hongjiang | 46b51d0 | 2013-06-24 01:57:47 -0500 | [diff] [blame] | 1802 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1803 | |
| 1804 | if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) && |
| 1805 | ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0)) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1806 | cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n"); |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1807 | |
| 1808 | if (tcon->seal && |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1809 | !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1810 | cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n"); |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 1811 | |
Steve French | de9f68df | 2013-11-15 11:26:24 -0600 | [diff] [blame] | 1812 | init_copy_chunk_defaults(tcon); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1813 | if (server->ops->validate_negotiate) |
| 1814 | rc = server->ops->validate_negotiate(xid, tcon); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1815 | tcon_exit: |
Steve French | f8af49d | 2018-10-28 00:47:11 -0500 | [diff] [blame] | 1816 | |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1817 | free_rsp_buf(resp_buftype, rsp); |
| 1818 | kfree(unc_path); |
| 1819 | return rc; |
| 1820 | |
| 1821 | tcon_error_exit: |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 1822 | if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 1823 | cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1824 | } |
| 1825 | goto tcon_exit; |
| 1826 | } |
| 1827 | |
| 1828 | int |
| 1829 | SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) |
| 1830 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1831 | struct smb_rqst rqst; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1832 | struct smb2_tree_disconnect_req *req; /* response is trivial */ |
| 1833 | int rc = 0; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1834 | struct cifs_ses *ses = tcon->ses; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1835 | int flags = 0; |
Ronnie Sahlberg | 4eecf4c | 2017-11-09 12:14:18 +1100 | [diff] [blame] | 1836 | unsigned int total_len; |
| 1837 | struct kvec iov[1]; |
| 1838 | struct kvec rsp_iov; |
| 1839 | int resp_buf_type; |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1840 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1841 | cifs_dbg(FYI, "Tree Disconnect\n"); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1842 | |
Christos Gkekas | 68a6afa | 2017-07-09 11:45:04 +0100 | [diff] [blame] | 1843 | if (!ses || !(ses->server)) |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1844 | return -EIO; |
| 1845 | |
| 1846 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) |
| 1847 | return 0; |
| 1848 | |
Pavel Shilovsky | d919131 | 2019-12-10 11:44:52 -0800 | [diff] [blame] | 1849 | close_shroot_lease(&tcon->crfid); |
Ronnie Sahlberg | 72e73c7 | 2019-11-07 17:00:38 +1000 | [diff] [blame] | 1850 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1851 | rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server, |
| 1852 | (void **) &req, |
| 1853 | &total_len); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1854 | if (rc) |
| 1855 | return rc; |
| 1856 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 1857 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 1858 | flags |= CIFS_TRANSFORM_REQ; |
| 1859 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 1860 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 4eecf4c | 2017-11-09 12:14:18 +1100 | [diff] [blame] | 1861 | |
| 1862 | iov[0].iov_base = (char *)req; |
| 1863 | iov[0].iov_len = total_len; |
| 1864 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 1865 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1866 | rqst.rq_iov = iov; |
| 1867 | rqst.rq_nvec = 1; |
| 1868 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 1869 | rc = cifs_send_recv(xid, ses, ses->server, |
| 1870 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1871 | cifs_small_buf_release(req); |
Pavel Shilovsky | faaf946 | 2011-12-27 16:04:00 +0400 | [diff] [blame] | 1872 | if (rc) |
| 1873 | cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE); |
| 1874 | |
| 1875 | return rc; |
| 1876 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 1877 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1878 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1879 | static struct create_durable * |
| 1880 | create_durable_buf(void) |
| 1881 | { |
| 1882 | struct create_durable *buf; |
| 1883 | |
| 1884 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); |
| 1885 | if (!buf) |
| 1886 | return NULL; |
| 1887 | |
| 1888 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1889 | (struct create_durable, Data)); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1890 | buf->ccontext.DataLength = cpu_to_le32(16); |
| 1891 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1892 | (struct create_durable, Name)); |
| 1893 | buf->ccontext.NameLength = cpu_to_le16(4); |
Steve French | 12197a7 | 2014-05-14 05:29:40 -0700 | [diff] [blame] | 1894 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */ |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 1895 | buf->Name[0] = 'D'; |
| 1896 | buf->Name[1] = 'H'; |
| 1897 | buf->Name[2] = 'n'; |
| 1898 | buf->Name[3] = 'Q'; |
| 1899 | return buf; |
| 1900 | } |
| 1901 | |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1902 | static struct create_durable * |
| 1903 | create_reconnect_durable_buf(struct cifs_fid *fid) |
| 1904 | { |
| 1905 | struct create_durable *buf; |
| 1906 | |
| 1907 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); |
| 1908 | if (!buf) |
| 1909 | return NULL; |
| 1910 | |
| 1911 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1912 | (struct create_durable, Data)); |
| 1913 | buf->ccontext.DataLength = cpu_to_le32(16); |
| 1914 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1915 | (struct create_durable, Name)); |
| 1916 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1917 | buf->Data.Fid.PersistentFileId = fid->persistent_fid; |
| 1918 | buf->Data.Fid.VolatileFileId = fid->volatile_fid; |
Steve French | 12197a7 | 2014-05-14 05:29:40 -0700 | [diff] [blame] | 1919 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */ |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 1920 | buf->Name[0] = 'D'; |
| 1921 | buf->Name[1] = 'H'; |
| 1922 | buf->Name[2] = 'n'; |
| 1923 | buf->Name[3] = 'C'; |
| 1924 | return buf; |
| 1925 | } |
| 1926 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1927 | static void |
| 1928 | parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf) |
| 1929 | { |
| 1930 | struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc; |
| 1931 | |
| 1932 | cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n", |
| 1933 | pdisk_id->DiskFileId, pdisk_id->VolumeId); |
| 1934 | buf->IndexNumber = pdisk_id->DiskFileId; |
| 1935 | } |
| 1936 | |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1937 | static void |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 1938 | parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info, |
| 1939 | struct create_posix_rsp *posix) |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1940 | { |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 1941 | int sid_len; |
| 1942 | u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset); |
| 1943 | u8 *end = beg + le32_to_cpu(cc->DataLength); |
| 1944 | u8 *sid; |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1945 | |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 1946 | memset(posix, 0, sizeof(*posix)); |
Aurelien Aptel | 2e8af97 | 2020-02-08 15:50:56 +0100 | [diff] [blame] | 1947 | |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 1948 | posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0)); |
| 1949 | posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4)); |
| 1950 | posix->mode = le32_to_cpu(*(__le32 *)(beg + 8)); |
| 1951 | |
| 1952 | sid = beg + 12; |
| 1953 | sid_len = posix_info_sid_size(sid, end); |
| 1954 | if (sid_len < 0) { |
| 1955 | cifs_dbg(VFS, "bad owner sid in posix create response\n"); |
| 1956 | return; |
| 1957 | } |
| 1958 | memcpy(&posix->owner, sid, sid_len); |
| 1959 | |
| 1960 | sid = sid + sid_len; |
| 1961 | sid_len = posix_info_sid_size(sid, end); |
| 1962 | if (sid_len < 0) { |
| 1963 | cifs_dbg(VFS, "bad group sid in posix create response\n"); |
| 1964 | return; |
| 1965 | } |
| 1966 | memcpy(&posix->group, sid, sid_len); |
| 1967 | |
| 1968 | cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n", |
| 1969 | posix->nlink, posix->mode, posix->reparse_tag); |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1970 | } |
| 1971 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1972 | void |
| 1973 | smb2_parse_contexts(struct TCP_Server_Info *server, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 1974 | struct smb2_create_rsp *rsp, |
| 1975 | unsigned int *epoch, char *lease_key, __u8 *oplock, |
| 1976 | struct smb2_file_all_info *buf, |
| 1977 | struct create_posix_rsp *posix) |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1978 | { |
| 1979 | char *data_offset; |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 1980 | struct create_context *cc; |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1981 | unsigned int next; |
| 1982 | unsigned int remaining; |
Pavel Shilovsky | fd55439 | 2013-07-09 19:44:56 +0400 | [diff] [blame] | 1983 | char *name; |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 1984 | const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C, |
| 1985 | 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83, |
| 1986 | 0xDE, 0x96, 0x8B, 0xCD, 0x7C}; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1987 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1988 | *oplock = 0; |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 1989 | data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset); |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1990 | remaining = le32_to_cpu(rsp->CreateContextsLength); |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 1991 | cc = (struct create_context *)data_offset; |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 1992 | |
| 1993 | /* Initialize inode number to 0 in case no valid data in qfid context */ |
| 1994 | if (buf) |
| 1995 | buf->IndexNumber = 0; |
| 1996 | |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1997 | while (remaining >= sizeof(struct create_context)) { |
Pavel Shilovsky | b5c7cde | 2013-09-05 20:16:45 +0400 | [diff] [blame] | 1998 | name = le16_to_cpu(cc->NameOffset) + (char *)cc; |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 1999 | if (le16_to_cpu(cc->NameLength) == 4 && |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2000 | strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0) |
| 2001 | *oplock = server->ops->parse_lease_buf(cc, epoch, |
| 2002 | lease_key); |
| 2003 | else if (buf && (le16_to_cpu(cc->NameLength) == 4) && |
| 2004 | strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0) |
| 2005 | parse_query_id_ctxt(cc, buf); |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2006 | else if ((le16_to_cpu(cc->NameLength) == 16)) { |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2007 | if (posix && |
| 2008 | memcmp(name, smb3_create_tag_posix, 16) == 0) |
| 2009 | parse_posix_ctxt(cc, buf, posix); |
Steve French | ab3459d | 2020-02-06 17:31:56 -0600 | [diff] [blame] | 2010 | } |
| 2011 | /* else { |
| 2012 | cifs_dbg(FYI, "Context not matched with len %d\n", |
| 2013 | le16_to_cpu(cc->NameLength)); |
| 2014 | cifs_dump_mem("Cctxt name: ", name, 4); |
| 2015 | } */ |
Justin Maggard | deb7def | 2016-02-09 15:52:08 -0800 | [diff] [blame] | 2016 | |
| 2017 | next = le32_to_cpu(cc->Next); |
| 2018 | if (!next) |
| 2019 | break; |
| 2020 | remaining -= next; |
| 2021 | cc = (struct create_context *)((char *)cc + next); |
| 2022 | } |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2023 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2024 | if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) |
| 2025 | *oplock = rsp->OplockLevel; |
| 2026 | |
| 2027 | return; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2030 | static int |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2031 | add_lease_context(struct TCP_Server_Info *server, struct kvec *iov, |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2032 | unsigned int *num_iovec, u8 *lease_key, __u8 *oplock) |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2033 | { |
| 2034 | struct smb2_create_req *req = iov[0].iov_base; |
| 2035 | unsigned int num = *num_iovec; |
| 2036 | |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2037 | iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock); |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2038 | if (iov[num].iov_base == NULL) |
| 2039 | return -ENOMEM; |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2040 | iov[num].iov_len = server->vals->create_lease_size; |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2041 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 2042 | if (!req->CreateContextsOffset) |
| 2043 | req->CreateContextsOffset = cpu_to_le32( |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2044 | sizeof(struct smb2_create_req) + |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2045 | iov[num - 1].iov_len); |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2046 | le32_add_cpu(&req->CreateContextsLength, |
| 2047 | server->vals->create_lease_size); |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2048 | *num_iovec = num + 1; |
| 2049 | return 0; |
| 2050 | } |
| 2051 | |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2052 | static struct create_durable_v2 * |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2053 | create_durable_v2_buf(struct cifs_open_parms *oparms) |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2054 | { |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2055 | struct cifs_fid *pfid = oparms->fid; |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2056 | struct create_durable_v2 *buf; |
| 2057 | |
| 2058 | buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL); |
| 2059 | if (!buf) |
| 2060 | return NULL; |
| 2061 | |
| 2062 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2063 | (struct create_durable_v2, dcontext)); |
| 2064 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2)); |
| 2065 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2066 | (struct create_durable_v2, Name)); |
| 2067 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2068 | |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2069 | /* |
| 2070 | * NB: Handle timeout defaults to 0, which allows server to choose |
| 2071 | * (most servers default to 120 seconds) and most clients default to 0. |
| 2072 | * This can be overridden at mount ("handletimeout=") if the user wants |
| 2073 | * a different persistent (or resilient) handle timeout for all opens |
| 2074 | * opens on a particular SMB3 mount. |
| 2075 | */ |
| 2076 | buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2077 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); |
Steve French | fa70b87 | 2016-09-22 00:39:34 -0500 | [diff] [blame] | 2078 | generate_random_uuid(buf->dcontext.CreateGuid); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2079 | memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16); |
| 2080 | |
| 2081 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */ |
| 2082 | buf->Name[0] = 'D'; |
| 2083 | buf->Name[1] = 'H'; |
| 2084 | buf->Name[2] = '2'; |
| 2085 | buf->Name[3] = 'Q'; |
| 2086 | return buf; |
| 2087 | } |
| 2088 | |
| 2089 | static struct create_durable_handle_reconnect_v2 * |
| 2090 | create_reconnect_durable_v2_buf(struct cifs_fid *fid) |
| 2091 | { |
| 2092 | struct create_durable_handle_reconnect_v2 *buf; |
| 2093 | |
| 2094 | buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2), |
| 2095 | GFP_KERNEL); |
| 2096 | if (!buf) |
| 2097 | return NULL; |
| 2098 | |
| 2099 | buf->ccontext.DataOffset = |
| 2100 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, |
| 2101 | dcontext)); |
| 2102 | buf->ccontext.DataLength = |
| 2103 | cpu_to_le32(sizeof(struct durable_reconnect_context_v2)); |
| 2104 | buf->ccontext.NameOffset = |
| 2105 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, |
| 2106 | Name)); |
| 2107 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2108 | |
| 2109 | buf->dcontext.Fid.PersistentFileId = fid->persistent_fid; |
| 2110 | buf->dcontext.Fid.VolatileFileId = fid->volatile_fid; |
| 2111 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); |
| 2112 | memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16); |
| 2113 | |
| 2114 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */ |
| 2115 | buf->Name[0] = 'D'; |
| 2116 | buf->Name[1] = 'H'; |
| 2117 | buf->Name[2] = '2'; |
| 2118 | buf->Name[3] = 'C'; |
| 2119 | return buf; |
| 2120 | } |
| 2121 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2122 | static int |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2123 | add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec, |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 2124 | struct cifs_open_parms *oparms) |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2125 | { |
| 2126 | struct smb2_create_req *req = iov[0].iov_base; |
| 2127 | unsigned int num = *num_iovec; |
| 2128 | |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2129 | iov[num].iov_base = create_durable_v2_buf(oparms); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2130 | if (iov[num].iov_base == NULL) |
| 2131 | return -ENOMEM; |
| 2132 | iov[num].iov_len = sizeof(struct create_durable_v2); |
| 2133 | if (!req->CreateContextsOffset) |
| 2134 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2135 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2136 | iov[1].iov_len); |
| 2137 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2)); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2138 | *num_iovec = num + 1; |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
| 2142 | static int |
| 2143 | add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec, |
| 2144 | struct cifs_open_parms *oparms) |
| 2145 | { |
| 2146 | struct smb2_create_req *req = iov[0].iov_base; |
| 2147 | unsigned int num = *num_iovec; |
| 2148 | |
| 2149 | /* indicate that we don't need to relock the file */ |
| 2150 | oparms->reconnect = false; |
| 2151 | |
| 2152 | iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid); |
| 2153 | if (iov[num].iov_base == NULL) |
| 2154 | return -ENOMEM; |
| 2155 | iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2); |
| 2156 | if (!req->CreateContextsOffset) |
| 2157 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2158 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2159 | iov[1].iov_len); |
| 2160 | le32_add_cpu(&req->CreateContextsLength, |
| 2161 | sizeof(struct create_durable_handle_reconnect_v2)); |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2162 | *num_iovec = num + 1; |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | static int |
| 2167 | add_durable_context(struct kvec *iov, unsigned int *num_iovec, |
| 2168 | struct cifs_open_parms *oparms, bool use_persistent) |
| 2169 | { |
| 2170 | struct smb2_create_req *req = iov[0].iov_base; |
| 2171 | unsigned int num = *num_iovec; |
| 2172 | |
| 2173 | if (use_persistent) { |
| 2174 | if (oparms->reconnect) |
| 2175 | return add_durable_reconnect_v2_context(iov, num_iovec, |
| 2176 | oparms); |
| 2177 | else |
| 2178 | return add_durable_v2_context(iov, num_iovec, oparms); |
| 2179 | } |
| 2180 | |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 2181 | if (oparms->reconnect) { |
| 2182 | iov[num].iov_base = create_reconnect_durable_buf(oparms->fid); |
| 2183 | /* indicate that we don't need to relock the file */ |
| 2184 | oparms->reconnect = false; |
| 2185 | } else |
| 2186 | iov[num].iov_base = create_durable_buf(); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2187 | if (iov[num].iov_base == NULL) |
| 2188 | return -ENOMEM; |
| 2189 | iov[num].iov_len = sizeof(struct create_durable); |
| 2190 | if (!req->CreateContextsOffset) |
| 2191 | req->CreateContextsOffset = |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2192 | cpu_to_le32(sizeof(struct smb2_create_req) + |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2193 | iov[1].iov_len); |
Wei Yongjun | 31f92e9 | 2013-08-26 14:34:46 +0800 | [diff] [blame] | 2194 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable)); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2195 | *num_iovec = num + 1; |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2199 | /* See MS-SMB2 2.2.13.2.7 */ |
| 2200 | static struct crt_twarp_ctxt * |
| 2201 | create_twarp_buf(__u64 timewarp) |
| 2202 | { |
| 2203 | struct crt_twarp_ctxt *buf; |
| 2204 | |
| 2205 | buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL); |
| 2206 | if (!buf) |
| 2207 | return NULL; |
| 2208 | |
| 2209 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2210 | (struct crt_twarp_ctxt, Timestamp)); |
| 2211 | buf->ccontext.DataLength = cpu_to_le32(8); |
| 2212 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2213 | (struct crt_twarp_ctxt, Name)); |
| 2214 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2215 | /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */ |
| 2216 | buf->Name[0] = 'T'; |
| 2217 | buf->Name[1] = 'W'; |
| 2218 | buf->Name[2] = 'r'; |
| 2219 | buf->Name[3] = 'p'; |
| 2220 | buf->Timestamp = cpu_to_le64(timewarp); |
| 2221 | return buf; |
| 2222 | } |
| 2223 | |
| 2224 | /* See MS-SMB2 2.2.13.2.7 */ |
| 2225 | static int |
| 2226 | add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp) |
| 2227 | { |
| 2228 | struct smb2_create_req *req = iov[0].iov_base; |
| 2229 | unsigned int num = *num_iovec; |
| 2230 | |
| 2231 | iov[num].iov_base = create_twarp_buf(timewarp); |
| 2232 | if (iov[num].iov_base == NULL) |
| 2233 | return -ENOMEM; |
| 2234 | iov[num].iov_len = sizeof(struct crt_twarp_ctxt); |
| 2235 | if (!req->CreateContextsOffset) |
| 2236 | req->CreateContextsOffset = cpu_to_le32( |
| 2237 | sizeof(struct smb2_create_req) + |
| 2238 | iov[num - 1].iov_len); |
| 2239 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt)); |
| 2240 | *num_iovec = num + 1; |
| 2241 | return 0; |
| 2242 | } |
| 2243 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2244 | /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */ |
| 2245 | static void setup_owner_group_sids(char *buf) |
| 2246 | { |
| 2247 | struct owner_group_sids *sids = (struct owner_group_sids *)buf; |
| 2248 | |
| 2249 | /* Populate the user ownership fields S-1-5-88-1 */ |
| 2250 | sids->owner.Revision = 1; |
| 2251 | sids->owner.NumAuth = 3; |
| 2252 | sids->owner.Authority[5] = 5; |
| 2253 | sids->owner.SubAuthorities[0] = cpu_to_le32(88); |
| 2254 | sids->owner.SubAuthorities[1] = cpu_to_le32(1); |
| 2255 | sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val); |
| 2256 | |
| 2257 | /* Populate the group ownership fields S-1-5-88-2 */ |
| 2258 | sids->group.Revision = 1; |
| 2259 | sids->group.NumAuth = 3; |
| 2260 | sids->group.Authority[5] = 5; |
| 2261 | sids->group.SubAuthorities[0] = cpu_to_le32(88); |
| 2262 | sids->group.SubAuthorities[1] = cpu_to_le32(2); |
| 2263 | sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val); |
Steve French | a7a519a | 2020-06-12 14:49:47 -0500 | [diff] [blame] | 2264 | |
| 2265 | cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2266 | } |
| 2267 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2268 | /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */ |
| 2269 | static struct crt_sd_ctxt * |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2270 | create_sd_buf(umode_t mode, bool set_owner, unsigned int *len) |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2271 | { |
| 2272 | struct crt_sd_ctxt *buf; |
| 2273 | struct cifs_ace *pace; |
| 2274 | unsigned int sdlen, acelen; |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2275 | unsigned int owner_offset = 0; |
| 2276 | unsigned int group_offset = 0; |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2277 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2278 | *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 2), 8); |
| 2279 | |
| 2280 | if (set_owner) { |
| 2281 | /* offset fields are from beginning of security descriptor not of create context */ |
| 2282 | owner_offset = sizeof(struct smb3_acl) + (sizeof(struct cifs_ace) * 2); |
| 2283 | |
| 2284 | /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */ |
| 2285 | *len += sizeof(struct owner_group_sids); |
| 2286 | } |
| 2287 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2288 | buf = kzalloc(*len, GFP_KERNEL); |
| 2289 | if (buf == NULL) |
| 2290 | return buf; |
| 2291 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2292 | if (set_owner) { |
| 2293 | buf->sd.OffsetOwner = cpu_to_le32(owner_offset); |
| 2294 | group_offset = owner_offset + sizeof(struct owner_sid); |
| 2295 | buf->sd.OffsetGroup = cpu_to_le32(group_offset); |
| 2296 | } else { |
| 2297 | buf->sd.OffsetOwner = 0; |
| 2298 | buf->sd.OffsetGroup = 0; |
| 2299 | } |
| 2300 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2301 | sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) + |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2302 | 2 * sizeof(struct cifs_ace); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2303 | if (set_owner) { |
| 2304 | sdlen += sizeof(struct owner_group_sids); |
| 2305 | setup_owner_group_sids(owner_offset + sizeof(struct create_context) + 8 /* name */ |
| 2306 | + (char *)buf); |
| 2307 | } |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2308 | |
| 2309 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 2310 | (struct crt_sd_ctxt, sd)); |
| 2311 | buf->ccontext.DataLength = cpu_to_le32(sdlen); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2312 | buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name)); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2313 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2314 | /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */ |
| 2315 | buf->Name[0] = 'S'; |
| 2316 | buf->Name[1] = 'e'; |
| 2317 | buf->Name[2] = 'c'; |
| 2318 | buf->Name[3] = 'D'; |
| 2319 | buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */ |
| 2320 | /* |
| 2321 | * ACL is "self relative" ie ACL is stored in contiguous block of memory |
| 2322 | * and "DP" ie the DACL is present |
| 2323 | */ |
| 2324 | buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP); |
| 2325 | |
| 2326 | /* offset owner, group and Sbz1 and SACL are all zero */ |
| 2327 | buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd)); |
| 2328 | buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */ |
| 2329 | |
| 2330 | /* create one ACE to hold the mode embedded in reserved special SID */ |
| 2331 | pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf); |
| 2332 | acelen = setup_special_mode_ACE(pace, (__u64)mode); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2333 | |
| 2334 | if (set_owner) { |
| 2335 | /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */ |
| 2336 | pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) + (char *)buf)); |
| 2337 | acelen += setup_special_user_owner_ACE(pace); |
| 2338 | /* it does not appear necessary to add an ACE for the NFS group SID */ |
| 2339 | buf->acl.AceCount = cpu_to_le16(3); |
| 2340 | } else |
| 2341 | buf->acl.AceCount = cpu_to_le16(2); |
| 2342 | |
Steve French | 643fbce | 2020-01-16 19:55:33 -0600 | [diff] [blame] | 2343 | /* and one more ACE to allow access for authenticated users */ |
| 2344 | pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) + |
| 2345 | (char *)buf)); |
| 2346 | acelen += setup_authusers_ACE(pace); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2347 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2348 | buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen); |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2349 | |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2350 | return buf; |
| 2351 | } |
| 2352 | |
| 2353 | static int |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2354 | add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner) |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2355 | { |
| 2356 | struct smb2_create_req *req = iov[0].iov_base; |
| 2357 | unsigned int num = *num_iovec; |
| 2358 | unsigned int len = 0; |
| 2359 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2360 | iov[num].iov_base = create_sd_buf(mode, set_owner, &len); |
Steve French | fdef665 | 2019-12-06 02:02:38 -0600 | [diff] [blame] | 2361 | if (iov[num].iov_base == NULL) |
| 2362 | return -ENOMEM; |
| 2363 | iov[num].iov_len = len; |
| 2364 | if (!req->CreateContextsOffset) |
| 2365 | req->CreateContextsOffset = cpu_to_le32( |
| 2366 | sizeof(struct smb2_create_req) + |
| 2367 | iov[num - 1].iov_len); |
| 2368 | le32_add_cpu(&req->CreateContextsLength, len); |
| 2369 | *num_iovec = num + 1; |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
Steve French | ff2a09e | 2019-07-06 14:41:38 -0500 | [diff] [blame] | 2373 | static struct crt_query_id_ctxt * |
| 2374 | create_query_id_buf(void) |
| 2375 | { |
| 2376 | struct crt_query_id_ctxt *buf; |
| 2377 | |
| 2378 | buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL); |
| 2379 | if (!buf) |
| 2380 | return NULL; |
| 2381 | |
| 2382 | buf->ccontext.DataOffset = cpu_to_le16(0); |
| 2383 | buf->ccontext.DataLength = cpu_to_le32(0); |
| 2384 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 2385 | (struct crt_query_id_ctxt, Name)); |
| 2386 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 2387 | /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */ |
| 2388 | buf->Name[0] = 'Q'; |
| 2389 | buf->Name[1] = 'F'; |
| 2390 | buf->Name[2] = 'i'; |
| 2391 | buf->Name[3] = 'd'; |
| 2392 | return buf; |
| 2393 | } |
| 2394 | |
| 2395 | /* See MS-SMB2 2.2.13.2.9 */ |
| 2396 | static int |
| 2397 | add_query_id_context(struct kvec *iov, unsigned int *num_iovec) |
| 2398 | { |
| 2399 | struct smb2_create_req *req = iov[0].iov_base; |
| 2400 | unsigned int num = *num_iovec; |
| 2401 | |
| 2402 | iov[num].iov_base = create_query_id_buf(); |
| 2403 | if (iov[num].iov_base == NULL) |
| 2404 | return -ENOMEM; |
| 2405 | iov[num].iov_len = sizeof(struct crt_query_id_ctxt); |
| 2406 | if (!req->CreateContextsOffset) |
| 2407 | req->CreateContextsOffset = cpu_to_le32( |
| 2408 | sizeof(struct smb2_create_req) + |
| 2409 | iov[num - 1].iov_len); |
| 2410 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt)); |
| 2411 | *num_iovec = num + 1; |
| 2412 | return 0; |
| 2413 | } |
| 2414 | |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2415 | static int |
| 2416 | alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len, |
| 2417 | const char *treename, const __le16 *path) |
| 2418 | { |
| 2419 | int treename_len, path_len; |
| 2420 | struct nls_table *cp; |
| 2421 | const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)}; |
| 2422 | |
| 2423 | /* |
| 2424 | * skip leading "\\" |
| 2425 | */ |
| 2426 | treename_len = strlen(treename); |
| 2427 | if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\')) |
| 2428 | return -EINVAL; |
| 2429 | |
| 2430 | treename += 2; |
| 2431 | treename_len -= 2; |
| 2432 | |
| 2433 | path_len = UniStrnlen((wchar_t *)path, PATH_MAX); |
| 2434 | |
| 2435 | /* |
| 2436 | * make room for one path separator between the treename and |
| 2437 | * path |
| 2438 | */ |
| 2439 | *out_len = treename_len + 1 + path_len; |
| 2440 | |
| 2441 | /* |
| 2442 | * final path needs to be null-terminated UTF16 with a |
| 2443 | * size aligned to 8 |
| 2444 | */ |
| 2445 | |
| 2446 | *out_size = roundup((*out_len+1)*2, 8); |
| 2447 | *out_path = kzalloc(*out_size, GFP_KERNEL); |
| 2448 | if (!*out_path) |
| 2449 | return -ENOMEM; |
| 2450 | |
| 2451 | cp = load_nls_default(); |
| 2452 | cifs_strtoUTF16(*out_path, treename, treename_len, cp); |
| 2453 | UniStrcat(*out_path, sep); |
| 2454 | UniStrcat(*out_path, path); |
| 2455 | unload_nls(cp); |
| 2456 | |
| 2457 | return 0; |
| 2458 | } |
| 2459 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2460 | int smb311_posix_mkdir(const unsigned int xid, struct inode *inode, |
| 2461 | umode_t mode, struct cifs_tcon *tcon, |
| 2462 | const char *full_path, |
| 2463 | struct cifs_sb_info *cifs_sb) |
| 2464 | { |
| 2465 | struct smb_rqst rqst; |
| 2466 | struct smb2_create_req *req; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2467 | struct smb2_create_rsp *rsp = NULL; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2468 | struct cifs_ses *ses = tcon->ses; |
| 2469 | struct kvec iov[3]; /* make sure at least one for each open context */ |
| 2470 | struct kvec rsp_iov = {NULL, 0}; |
| 2471 | int resp_buftype; |
| 2472 | int uni_path_len; |
| 2473 | __le16 *copy_path = NULL; |
| 2474 | int copy_size; |
| 2475 | int rc = 0; |
| 2476 | unsigned int n_iov = 2; |
| 2477 | __u32 file_attributes = 0; |
| 2478 | char *pc_buf = NULL; |
| 2479 | int flags = 0; |
| 2480 | unsigned int total_len; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2481 | __le16 *utf16_path = NULL; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2482 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2483 | |
| 2484 | cifs_dbg(FYI, "mkdir\n"); |
| 2485 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2486 | /* resource #1: path allocation */ |
| 2487 | utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb); |
| 2488 | if (!utf16_path) |
| 2489 | return -ENOMEM; |
| 2490 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2491 | if (!ses || !server) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2492 | rc = -EIO; |
| 2493 | goto err_free_path; |
| 2494 | } |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2495 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2496 | /* resource #2: request */ |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2497 | rc = smb2_plain_req_init(SMB2_CREATE, tcon, server, |
| 2498 | (void **) &req, &total_len); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2499 | if (rc) |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2500 | goto err_free_path; |
| 2501 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2502 | |
| 2503 | if (smb3_encryption_required(tcon)) |
| 2504 | flags |= CIFS_TRANSFORM_REQ; |
| 2505 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2506 | req->ImpersonationLevel = IL_IMPERSONATION; |
| 2507 | req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES); |
| 2508 | /* File attributes ignored on open (used in create though) */ |
| 2509 | req->FileAttributes = cpu_to_le32(file_attributes); |
| 2510 | req->ShareAccess = FILE_SHARE_ALL_LE; |
| 2511 | req->CreateDisposition = cpu_to_le32(FILE_CREATE); |
| 2512 | req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE); |
| 2513 | |
| 2514 | iov[0].iov_base = (char *)req; |
| 2515 | /* -1 since last byte is buf[0] which is sent below (path) */ |
| 2516 | iov[0].iov_len = total_len - 1; |
| 2517 | |
| 2518 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); |
| 2519 | |
| 2520 | /* [MS-SMB2] 2.2.13 NameOffset: |
| 2521 | * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of |
| 2522 | * the SMB2 header, the file name includes a prefix that will |
| 2523 | * be processed during DFS name normalization as specified in |
| 2524 | * section 3.3.5.9. Otherwise, the file name is relative to |
| 2525 | * the share that is identified by the TreeId in the SMB2 |
| 2526 | * header. |
| 2527 | */ |
| 2528 | if (tcon->share_flags & SHI1005_FLAGS_DFS) { |
| 2529 | int name_len; |
| 2530 | |
| 2531 | req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; |
| 2532 | rc = alloc_path_with_tree_prefix(©_path, ©_size, |
| 2533 | &name_len, |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2534 | tcon->treeName, utf16_path); |
| 2535 | if (rc) |
| 2536 | goto err_free_req; |
| 2537 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2538 | req->NameLength = cpu_to_le16(name_len * 2); |
| 2539 | uni_path_len = copy_size; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2540 | /* free before overwriting resource */ |
| 2541 | kfree(utf16_path); |
| 2542 | utf16_path = copy_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2543 | } else { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2544 | uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2545 | /* MUST set path len (NameLength) to 0 opening root of share */ |
| 2546 | req->NameLength = cpu_to_le16(uni_path_len - 2); |
| 2547 | if (uni_path_len % 8 != 0) { |
| 2548 | copy_size = roundup(uni_path_len, 8); |
| 2549 | copy_path = kzalloc(copy_size, GFP_KERNEL); |
| 2550 | if (!copy_path) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2551 | rc = -ENOMEM; |
| 2552 | goto err_free_req; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2553 | } |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2554 | memcpy((char *)copy_path, (const char *)utf16_path, |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2555 | uni_path_len); |
| 2556 | uni_path_len = copy_size; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2557 | /* free before overwriting resource */ |
| 2558 | kfree(utf16_path); |
| 2559 | utf16_path = copy_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | iov[1].iov_len = uni_path_len; |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2564 | iov[1].iov_base = utf16_path; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2565 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
| 2566 | |
| 2567 | if (tcon->posix_extensions) { |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2568 | /* resource #3: posix buf */ |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2569 | rc = add_posix_context(iov, &n_iov, mode); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2570 | if (rc) |
| 2571 | goto err_free_req; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2572 | pc_buf = iov[n_iov-1].iov_base; |
| 2573 | } |
| 2574 | |
| 2575 | |
| 2576 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 2577 | rqst.rq_iov = iov; |
| 2578 | rqst.rq_nvec = n_iov; |
| 2579 | |
Steve French | d2f1542 | 2019-09-22 00:55:46 -0500 | [diff] [blame] | 2580 | /* no need to inc num_remote_opens because we close it just below */ |
Steve French | efe2e9f | 2019-02-26 19:08:12 -0600 | [diff] [blame] | 2581 | trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE, |
| 2582 | FILE_WRITE_ATTRIBUTES); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2583 | /* resource #4: response buffer */ |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2584 | rc = cifs_send_recv(xid, ses, server, |
| 2585 | &rqst, &resp_buftype, flags, &rsp_iov); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2586 | if (rc) { |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2587 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); |
| 2588 | trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid, |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2589 | CREATE_NOT_FILE, |
| 2590 | FILE_WRITE_ATTRIBUTES, rc); |
| 2591 | goto err_free_rsp_buf; |
| 2592 | } |
| 2593 | |
| 2594 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; |
| 2595 | trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, |
| 2596 | ses->Suid, CREATE_NOT_FILE, |
| 2597 | FILE_WRITE_ATTRIBUTES); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2598 | |
| 2599 | SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId); |
| 2600 | |
| 2601 | /* Eventually save off posix specific response info and timestaps */ |
| 2602 | |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2603 | err_free_rsp_buf: |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2604 | free_rsp_buf(resp_buftype, rsp); |
Aurelien Aptel | 256b4c3 | 2018-06-19 15:18:48 -0700 | [diff] [blame] | 2605 | kfree(pc_buf); |
| 2606 | err_free_req: |
| 2607 | cifs_small_buf_release(req); |
| 2608 | err_free_path: |
| 2609 | kfree(utf16_path); |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2610 | return rc; |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2611 | } |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 2612 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2613 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2614 | SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 2615 | struct smb_rqst *rqst, __u8 *oplock, |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2616 | struct cifs_open_parms *oparms, __le16 *path) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2617 | { |
| 2618 | struct smb2_create_req *req; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2619 | unsigned int n_iov = 2; |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2620 | __u32 file_attributes = 0; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2621 | int copy_size; |
| 2622 | int uni_path_len; |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2623 | unsigned int total_len; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2624 | struct kvec *iov = rqst->rq_iov; |
| 2625 | __le16 *copy_path; |
| 2626 | int rc; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2627 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2628 | rc = smb2_plain_req_init(SMB2_CREATE, tcon, server, |
| 2629 | (void **) &req, &total_len); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2630 | if (rc) |
| 2631 | return rc; |
| 2632 | |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2633 | iov[0].iov_base = (char *)req; |
| 2634 | /* -1 since last byte is buf[0] which is sent below (path) */ |
| 2635 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 2636 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2637 | if (oparms->create_options & CREATE_OPTION_READONLY) |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2638 | file_attributes |= ATTR_READONLY; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 2639 | if (oparms->create_options & CREATE_OPTION_SPECIAL) |
| 2640 | file_attributes |= ATTR_SYSTEM; |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 2641 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2642 | req->ImpersonationLevel = IL_IMPERSONATION; |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2643 | req->DesiredAccess = cpu_to_le32(oparms->desired_access); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2644 | /* File attributes ignored on open (used in create though) */ |
| 2645 | req->FileAttributes = cpu_to_le32(file_attributes); |
| 2646 | req->ShareAccess = FILE_SHARE_ALL_LE; |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2647 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2648 | req->CreateDisposition = cpu_to_le32(oparms->disposition); |
| 2649 | req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK); |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2650 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2651 | |
| 2652 | /* [MS-SMB2] 2.2.13 NameOffset: |
| 2653 | * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of |
| 2654 | * the SMB2 header, the file name includes a prefix that will |
| 2655 | * be processed during DFS name normalization as specified in |
| 2656 | * section 3.3.5.9. Otherwise, the file name is relative to |
| 2657 | * the share that is identified by the TreeId in the SMB2 |
| 2658 | * header. |
| 2659 | */ |
| 2660 | if (tcon->share_flags & SHI1005_FLAGS_DFS) { |
| 2661 | int name_len; |
| 2662 | |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2663 | req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2664 | rc = alloc_path_with_tree_prefix(©_path, ©_size, |
| 2665 | &name_len, |
| 2666 | tcon->treeName, path); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2667 | if (rc) |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2668 | return rc; |
| 2669 | req->NameLength = cpu_to_le16(name_len * 2); |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2670 | uni_path_len = copy_size; |
| 2671 | path = copy_path; |
Aurelien Aptel | f071292 | 2017-02-22 14:47:17 +0100 | [diff] [blame] | 2672 | } else { |
| 2673 | uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2; |
| 2674 | /* MUST set path len (NameLength) to 0 opening root of share */ |
| 2675 | req->NameLength = cpu_to_le16(uni_path_len - 2); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2676 | copy_size = uni_path_len; |
| 2677 | if (copy_size % 8 != 0) |
| 2678 | copy_size = roundup(copy_size, 8); |
| 2679 | copy_path = kzalloc(copy_size, GFP_KERNEL); |
| 2680 | if (!copy_path) |
| 2681 | return -ENOMEM; |
| 2682 | memcpy((char *)copy_path, (const char *)path, |
| 2683 | uni_path_len); |
| 2684 | uni_path_len = copy_size; |
| 2685 | path = copy_path; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2686 | } |
| 2687 | |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2688 | iov[1].iov_len = uni_path_len; |
| 2689 | iov[1].iov_base = path; |
Pavel Shilovsky | 59aa371 | 2013-07-04 19:41:24 +0400 | [diff] [blame] | 2690 | |
Steve French | 3e7a02d | 2019-09-11 21:46:20 -0500 | [diff] [blame] | 2691 | if ((!server->oplocks) || (tcon->no_lease)) |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2692 | *oplock = SMB2_OPLOCK_LEVEL_NONE; |
| 2693 | |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2694 | if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) || |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2695 | *oplock == SMB2_OPLOCK_LEVEL_NONE) |
| 2696 | req->RequestedOplockLevel = *oplock; |
Steve French | f801568 | 2018-08-31 15:12:10 -0500 | [diff] [blame] | 2697 | else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) && |
| 2698 | (oparms->create_options & CREATE_NOT_FILE)) |
| 2699 | req->RequestedOplockLevel = *oplock; /* no srv lease support */ |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2700 | else { |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 2701 | rc = add_lease_context(server, iov, &n_iov, |
| 2702 | oparms->fid->lease_key, oplock); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2703 | if (rc) |
Pavel Shilovsky | d22cbfe | 2013-07-04 19:10:00 +0400 | [diff] [blame] | 2704 | return rc; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2707 | if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) { |
| 2708 | /* need to set Next field of lease context if we request it */ |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2709 | if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) { |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2710 | struct create_context *ccontext = |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2711 | (struct create_context *)iov[n_iov-1].iov_base; |
Steve French | 1c46943 | 2013-07-10 12:50:57 -0500 | [diff] [blame] | 2712 | ccontext->Next = |
Pavel Shilovsky | a41a28b | 2013-09-04 13:07:41 +0400 | [diff] [blame] | 2713 | cpu_to_le32(server->vals->create_lease_size); |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2714 | } |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2715 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2716 | rc = add_durable_context(iov, &n_iov, oparms, |
Steve French | b56eae4 | 2015-11-03 09:26:27 -0600 | [diff] [blame] | 2717 | tcon->use_persistent); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2718 | if (rc) |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2719 | return rc; |
Pavel Shilovsky | 63eb3de | 2013-07-04 18:41:09 +0400 | [diff] [blame] | 2720 | } |
| 2721 | |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2722 | if (tcon->posix_extensions) { |
| 2723 | if (n_iov > 2) { |
| 2724 | struct create_context *ccontext = |
| 2725 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2726 | ccontext->Next = |
| 2727 | cpu_to_le32(iov[n_iov-1].iov_len); |
| 2728 | } |
| 2729 | |
| 2730 | rc = add_posix_context(iov, &n_iov, oparms->mode); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2731 | if (rc) |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2732 | return rc; |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2733 | } |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 2734 | |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2735 | if (tcon->snapshot_time) { |
| 2736 | cifs_dbg(FYI, "adding snapshot context\n"); |
| 2737 | if (n_iov > 2) { |
| 2738 | struct create_context *ccontext = |
| 2739 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2740 | ccontext->Next = |
| 2741 | cpu_to_le32(iov[n_iov-1].iov_len); |
| 2742 | } |
| 2743 | |
| 2744 | rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time); |
| 2745 | if (rc) |
| 2746 | return rc; |
| 2747 | } |
| 2748 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2749 | if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) { |
| 2750 | bool set_mode; |
| 2751 | bool set_owner; |
| 2752 | |
| 2753 | if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) && |
| 2754 | (oparms->mode != ACL_NO_MODE)) |
| 2755 | set_mode = true; |
| 2756 | else { |
| 2757 | set_mode = false; |
| 2758 | oparms->mode = ACL_NO_MODE; |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2759 | } |
| 2760 | |
Steve French | 975221e | 2020-06-12 09:25:21 -0500 | [diff] [blame] | 2761 | if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL) |
| 2762 | set_owner = true; |
| 2763 | else |
| 2764 | set_owner = false; |
| 2765 | |
| 2766 | if (set_owner | set_mode) { |
| 2767 | if (n_iov > 2) { |
| 2768 | struct create_context *ccontext = |
| 2769 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2770 | ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len); |
| 2771 | } |
| 2772 | |
| 2773 | cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode); |
| 2774 | rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner); |
| 2775 | if (rc) |
| 2776 | return rc; |
| 2777 | } |
Steve French | c3ca78e | 2019-09-25 00:32:13 -0500 | [diff] [blame] | 2778 | } |
| 2779 | |
Steve French | ff2a09e | 2019-07-06 14:41:38 -0500 | [diff] [blame] | 2780 | if (n_iov > 2) { |
| 2781 | struct create_context *ccontext = |
| 2782 | (struct create_context *)iov[n_iov-1].iov_base; |
| 2783 | ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len); |
| 2784 | } |
| 2785 | add_query_id_context(iov, &n_iov); |
Steve French | cdeaf9d | 2018-08-10 02:25:06 -0500 | [diff] [blame] | 2786 | |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2787 | rqst->rq_nvec = n_iov; |
| 2788 | return 0; |
| 2789 | } |
| 2790 | |
| 2791 | /* rq_iov[0] is the request and is released by cifs_small_buf_release(). |
| 2792 | * All other vectors are freed by kfree(). |
| 2793 | */ |
| 2794 | void |
| 2795 | SMB2_open_free(struct smb_rqst *rqst) |
| 2796 | { |
| 2797 | int i; |
| 2798 | |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 2799 | if (rqst && rqst->rq_iov) { |
| 2800 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); |
| 2801 | for (i = 1; i < rqst->rq_nvec; i++) |
| 2802 | if (rqst->rq_iov[i].iov_base != smb2_padding) |
| 2803 | kfree(rqst->rq_iov[i].iov_base); |
| 2804 | } |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | int |
| 2808 | SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, |
| 2809 | __u8 *oplock, struct smb2_file_all_info *buf, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2810 | struct create_posix_rsp *posix, |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2811 | struct kvec *err_iov, int *buftype) |
| 2812 | { |
| 2813 | struct smb_rqst rqst; |
| 2814 | struct smb2_create_rsp *rsp = NULL; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2815 | struct cifs_tcon *tcon = oparms->tcon; |
| 2816 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2817 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | 4d8dfaf | 2018-08-21 11:49:21 +1000 | [diff] [blame] | 2818 | struct kvec iov[SMB2_CREATE_IOV_SIZE]; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2819 | struct kvec rsp_iov = {NULL, 0}; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 2820 | int resp_buftype = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2821 | int rc = 0; |
| 2822 | int flags = 0; |
| 2823 | |
| 2824 | cifs_dbg(FYI, "create/open\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2825 | if (!ses || !server) |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2826 | return -EIO; |
| 2827 | |
| 2828 | if (smb3_encryption_required(tcon)) |
| 2829 | flags |= CIFS_TRANSFORM_REQ; |
| 2830 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2831 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2832 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2833 | rqst.rq_iov = iov; |
Ronnie Sahlberg | 4d8dfaf | 2018-08-21 11:49:21 +1000 | [diff] [blame] | 2834 | rqst.rq_nvec = SMB2_CREATE_IOV_SIZE; |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2835 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2836 | rc = SMB2_open_init(tcon, server, |
| 2837 | &rqst, oplock, oparms, path); |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2838 | if (rc) |
| 2839 | goto creat_exit; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 2840 | |
Steve French | efe2e9f | 2019-02-26 19:08:12 -0600 | [diff] [blame] | 2841 | trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, |
| 2842 | oparms->create_options, oparms->desired_access); |
| 2843 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2844 | rc = cifs_send_recv(xid, ses, server, |
| 2845 | &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 4f33bc3 | 2017-11-20 11:24:38 +1100 | [diff] [blame] | 2846 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 2847 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2848 | |
| 2849 | if (rc != 0) { |
| 2850 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); |
Ronnie Sahlberg | 91cb74f | 2018-04-13 09:03:19 +1000 | [diff] [blame] | 2851 | if (err_iov && rsp) { |
| 2852 | *err_iov = rsp_iov; |
Ronnie Sahlberg | 9d874c3 | 2018-06-08 13:21:18 +1000 | [diff] [blame] | 2853 | *buftype = resp_buftype; |
Ronnie Sahlberg | 91cb74f | 2018-04-13 09:03:19 +1000 | [diff] [blame] | 2854 | resp_buftype = CIFS_NO_BUFFER; |
| 2855 | rsp = NULL; |
| 2856 | } |
Steve French | 28d5936 | 2018-05-30 21:42:34 -0500 | [diff] [blame] | 2857 | trace_smb3_open_err(xid, tcon->tid, ses->Suid, |
| 2858 | oparms->create_options, oparms->desired_access, rc); |
Steve French | 7dcc82c | 2019-09-11 00:07:36 -0500 | [diff] [blame] | 2859 | if (rc == -EREMCHG) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 2860 | pr_warn_once("server share %s deleted\n", |
| 2861 | tcon->treeName); |
Steve French | 7dcc82c | 2019-09-11 00:07:36 -0500 | [diff] [blame] | 2862 | tcon->need_reconnect = true; |
| 2863 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2864 | goto creat_exit; |
Steve French | 28d5936 | 2018-05-30 21:42:34 -0500 | [diff] [blame] | 2865 | } else |
| 2866 | trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, |
| 2867 | ses->Suid, oparms->create_options, |
| 2868 | oparms->desired_access); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2869 | |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 2870 | atomic_inc(&tcon->num_remote_opens); |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 2871 | oparms->fid->persistent_fid = rsp->PersistentFileId; |
| 2872 | oparms->fid->volatile_fid = rsp->VolatileFileId; |
Aurelien Aptel | 86f740f | 2020-02-21 11:19:06 +0100 | [diff] [blame] | 2873 | oparms->fid->access = oparms->desired_access; |
Steve French | dfe33f9 | 2018-10-30 19:50:31 -0500 | [diff] [blame] | 2874 | #ifdef CONFIG_CIFS_DEBUG2 |
| 2875 | oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId); |
| 2876 | #endif /* CIFS_DEBUG2 */ |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 2877 | |
| 2878 | if (buf) { |
| 2879 | memcpy(buf, &rsp->CreationTime, 32); |
| 2880 | buf->AllocationSize = rsp->AllocationSize; |
| 2881 | buf->EndOfFile = rsp->EndofFile; |
| 2882 | buf->Attributes = rsp->FileAttributes; |
| 2883 | buf->NumberOfLinks = cpu_to_le32(1); |
| 2884 | buf->DeletePending = 0; |
| 2885 | } |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 2886 | |
Steve French | 89a5bfa | 2019-07-18 17:22:18 -0500 | [diff] [blame] | 2887 | |
| 2888 | smb2_parse_contexts(server, rsp, &oparms->fid->epoch, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 2889 | oparms->fid->lease_key, oplock, buf, posix); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2890 | creat_exit: |
Ronnie Sahlberg | 1eb9fb5 | 2018-08-08 15:07:46 +1000 | [diff] [blame] | 2891 | SMB2_open_free(&rqst); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 2892 | free_rsp_buf(resp_buftype, rsp); |
| 2893 | return rc; |
| 2894 | } |
| 2895 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2896 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2897 | SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 2898 | struct smb_rqst *rqst, |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2899 | u64 persistent_fid, u64 volatile_fid, u32 opcode, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2900 | bool is_fsctl, char *in_data, u32 indatalen, |
| 2901 | __u32 max_response_size) |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2902 | { |
| 2903 | struct smb2_ioctl_req *req; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2904 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2905 | unsigned int total_len; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2906 | int rc; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2907 | char *in_data_buf; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2908 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 2909 | rc = smb2_ioctl_req_init(opcode, tcon, server, |
| 2910 | (void **) &req, &total_len); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2911 | if (rc) |
| 2912 | return rc; |
| 2913 | |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2914 | if (indatalen) { |
| 2915 | /* |
| 2916 | * indatalen is usually small at a couple of bytes max, so |
| 2917 | * just allocate through generic pool |
| 2918 | */ |
YueHaibing | d81f097 | 2019-06-01 03:31:10 +0000 | [diff] [blame] | 2919 | in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS); |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2920 | if (!in_data_buf) { |
| 2921 | cifs_small_buf_release(req); |
| 2922 | return -ENOMEM; |
| 2923 | } |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2924 | } |
| 2925 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2926 | req->CtlCode = cpu_to_le32(opcode); |
| 2927 | req->PersistentFileId = persistent_fid; |
| 2928 | req->VolatileFileId = volatile_fid; |
| 2929 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2930 | iov[0].iov_base = (char *)req; |
| 2931 | /* |
| 2932 | * If no input data, the size of ioctl struct in |
| 2933 | * protocol spec still includes a 1 byte data buffer, |
| 2934 | * but if input data passed to ioctl, we do not |
| 2935 | * want to double count this, so we do not send |
| 2936 | * the dummy one byte of data in iovec[0] if sending |
| 2937 | * input data (in iovec[1]). |
| 2938 | */ |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2939 | if (indatalen) { |
| 2940 | req->InputCount = cpu_to_le32(indatalen); |
| 2941 | /* do not set InputOffset if no input data */ |
| 2942 | req->InputOffset = |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2943 | cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer)); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2944 | rqst->rq_nvec = 2; |
| 2945 | iov[0].iov_len = total_len - 1; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2946 | iov[1].iov_base = in_data_buf; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2947 | iov[1].iov_len = indatalen; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2948 | } else { |
| 2949 | rqst->rq_nvec = 1; |
| 2950 | iov[0].iov_len = total_len; |
| 2951 | } |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2952 | |
| 2953 | req->OutputOffset = 0; |
| 2954 | req->OutputCount = 0; /* MBZ */ |
| 2955 | |
| 2956 | /* |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2957 | * In most cases max_response_size is set to 16K (CIFSMaxBufSize) |
| 2958 | * We Could increase default MaxOutputResponse, but that could require |
| 2959 | * more credits. Windows typically sets this smaller, but for some |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2960 | * ioctls it may be useful to allow server to send more. No point |
| 2961 | * limiting what the server can send as long as fits in one credit |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2962 | * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want |
| 2963 | * to increase this limit up in the future. |
| 2964 | * Note that for snapshot queries that servers like Azure expect that |
| 2965 | * the first query be minimal size (and just used to get the number/size |
| 2966 | * of previous versions) so response size must be specified as EXACTLY |
| 2967 | * sizeof(struct snapshot_array) which is 16 when rounded up to multiple |
| 2968 | * of eight bytes. Currently that is the only case where we set max |
| 2969 | * response size smaller. |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2970 | */ |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2971 | req->MaxOutputResponse = cpu_to_le32(max_response_size); |
Namjae Jeon | ebf5744 | 2020-06-11 11:21:19 +0900 | [diff] [blame] | 2972 | req->sync_hdr.CreditCharge = |
| 2973 | cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size), |
| 2974 | SMB2_MAX_BUFFER_SIZE)); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2975 | if (is_fsctl) |
| 2976 | req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL); |
| 2977 | else |
| 2978 | req->Flags = 0; |
| 2979 | |
Steve French | 4587eee | 2017-10-25 15:58:31 -0500 | [diff] [blame] | 2980 | /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */ |
| 2981 | if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 2982 | req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 2983 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2984 | return 0; |
| 2985 | } |
| 2986 | |
| 2987 | void |
| 2988 | SMB2_ioctl_free(struct smb_rqst *rqst) |
| 2989 | { |
Murphy Zhou | 6457c20 | 2019-05-23 12:12:43 +0800 | [diff] [blame] | 2990 | int i; |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2991 | if (rqst && rqst->rq_iov) { |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2992 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Murphy Zhou | 6457c20 | 2019-05-23 12:12:43 +0800 | [diff] [blame] | 2993 | for (i = 1; i < rqst->rq_nvec; i++) |
| 2994 | if (rqst->rq_iov[i].iov_base != smb2_padding) |
| 2995 | kfree(rqst->rq_iov[i].iov_base); |
Long Li | 2c87d6a | 2019-05-15 14:09:05 -0700 | [diff] [blame] | 2996 | } |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 2997 | } |
| 2998 | |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 2999 | |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3000 | /* |
| 3001 | * SMB2 IOCTL is used for both IOCTLs and FSCTLs |
| 3002 | */ |
| 3003 | int |
| 3004 | SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, |
| 3005 | u64 volatile_fid, u32 opcode, bool is_fsctl, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3006 | char *in_data, u32 indatalen, u32 max_out_data_len, |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3007 | char **out_data, u32 *plen /* returned data len */) |
| 3008 | { |
| 3009 | struct smb_rqst rqst; |
| 3010 | struct smb2_ioctl_rsp *rsp = NULL; |
| 3011 | struct cifs_ses *ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3012 | struct TCP_Server_Info *server; |
Ronnie Sahlberg | 72c419d | 2019-03-13 14:37:49 +1000 | [diff] [blame] | 3013 | struct kvec iov[SMB2_IOCTL_IOV_SIZE]; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3014 | struct kvec rsp_iov = {NULL, 0}; |
| 3015 | int resp_buftype = CIFS_NO_BUFFER; |
| 3016 | int rc = 0; |
| 3017 | int flags = 0; |
| 3018 | |
| 3019 | cifs_dbg(FYI, "SMB2 IOCTL\n"); |
| 3020 | |
| 3021 | if (out_data != NULL) |
| 3022 | *out_data = NULL; |
| 3023 | |
| 3024 | /* zero out returned data len, in case of error */ |
| 3025 | if (plen) |
| 3026 | *plen = 0; |
| 3027 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3028 | if (!tcon) |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3029 | return -EIO; |
| 3030 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3031 | ses = tcon->ses; |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3032 | if (!ses) |
| 3033 | return -EIO; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3034 | |
| 3035 | server = cifs_pick_channel(ses); |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3036 | if (!server) |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3037 | return -EIO; |
| 3038 | |
| 3039 | if (smb3_encryption_required(tcon)) |
| 3040 | flags |= CIFS_TRANSFORM_REQ; |
| 3041 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3042 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3043 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3044 | rqst.rq_iov = iov; |
Ronnie Sahlberg | 72c419d | 2019-03-13 14:37:49 +1000 | [diff] [blame] | 3045 | rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE; |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3046 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3047 | rc = SMB2_ioctl_init(tcon, server, |
| 3048 | &rqst, persistent_fid, volatile_fid, opcode, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3049 | is_fsctl, in_data, indatalen, max_out_data_len); |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3050 | if (rc) |
| 3051 | goto ioctl_exit; |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3052 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3053 | rc = cifs_send_recv(xid, ses, server, |
| 3054 | &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 9775468 | 2017-11-09 12:14:20 +1100 | [diff] [blame] | 3055 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3056 | rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base; |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3057 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3058 | if (rc != 0) |
| 3059 | trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid, |
| 3060 | ses->Suid, 0, opcode, rc); |
| 3061 | |
Ronnie Sahlberg | 2f3ebab | 2019-04-25 16:45:29 +1000 | [diff] [blame] | 3062 | if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) { |
Steve French | 8e35310 | 2015-03-26 19:47:02 -0500 | [diff] [blame] | 3063 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3064 | goto ioctl_exit; |
Steve French | 9bf0c9c | 2013-11-16 18:05:28 -0600 | [diff] [blame] | 3065 | } else if (rc == -EINVAL) { |
| 3066 | if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) && |
| 3067 | (opcode != FSCTL_SRV_COPYCHUNK)) { |
Steve French | 8e35310 | 2015-03-26 19:47:02 -0500 | [diff] [blame] | 3068 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
Steve French | 9bf0c9c | 2013-11-16 18:05:28 -0600 | [diff] [blame] | 3069 | goto ioctl_exit; |
| 3070 | } |
Ronnie Sahlberg | 2f3ebab | 2019-04-25 16:45:29 +1000 | [diff] [blame] | 3071 | } else if (rc == -E2BIG) { |
| 3072 | if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) { |
| 3073 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
| 3074 | goto ioctl_exit; |
| 3075 | } |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | /* check if caller wants to look at return data or just return rc */ |
| 3079 | if ((plen == NULL) || (out_data == NULL)) |
| 3080 | goto ioctl_exit; |
| 3081 | |
| 3082 | *plen = le32_to_cpu(rsp->OutputCount); |
| 3083 | |
| 3084 | /* We check for obvious errors in the output buffer length and offset */ |
| 3085 | if (*plen == 0) |
| 3086 | goto ioctl_exit; /* server returned no data */ |
Dan Carpenter | 2d204ee | 2018-09-10 14:12:07 +0300 | [diff] [blame] | 3087 | else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3088 | cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3089 | *plen = 0; |
| 3090 | rc = -EIO; |
| 3091 | goto ioctl_exit; |
| 3092 | } |
| 3093 | |
Dan Carpenter | 2d204ee | 2018-09-10 14:12:07 +0300 | [diff] [blame] | 3094 | if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3095 | cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen, |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3096 | le32_to_cpu(rsp->OutputOffset)); |
| 3097 | *plen = 0; |
| 3098 | rc = -EIO; |
| 3099 | goto ioctl_exit; |
| 3100 | } |
| 3101 | |
YueHaibing | d034fee | 2018-09-10 01:33:06 +0000 | [diff] [blame] | 3102 | *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset), |
| 3103 | *plen, GFP_KERNEL); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3104 | if (*out_data == NULL) { |
| 3105 | rc = -ENOMEM; |
| 3106 | goto ioctl_exit; |
| 3107 | } |
| 3108 | |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3109 | ioctl_exit: |
Ronnie Sahlberg | ccdc77a | 2019-03-13 14:37:48 +1000 | [diff] [blame] | 3110 | SMB2_ioctl_free(&rqst); |
Steve French | 4a72daf | 2013-06-25 00:20:49 -0500 | [diff] [blame] | 3111 | free_rsp_buf(resp_buftype, rsp); |
| 3112 | return rc; |
| 3113 | } |
| 3114 | |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3115 | /* |
| 3116 | * Individual callers to ioctl worker function follow |
| 3117 | */ |
| 3118 | |
| 3119 | int |
| 3120 | SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon, |
| 3121 | u64 persistent_fid, u64 volatile_fid) |
| 3122 | { |
| 3123 | int rc; |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3124 | struct compress_ioctl fsctl_input; |
| 3125 | char *ret_data = NULL; |
| 3126 | |
| 3127 | fsctl_input.CompressionState = |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 3128 | cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3129 | |
| 3130 | rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid, |
| 3131 | FSCTL_SET_COMPRESSION, true /* is_fsctl */, |
| 3132 | (char *)&fsctl_input /* data input */, |
Steve French | 153322f | 2019-03-28 22:32:49 -0500 | [diff] [blame] | 3133 | 2 /* in data len */, CIFSMaxBufSize /* max out data */, |
| 3134 | &ret_data /* out data */, NULL); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3135 | |
| 3136 | cifs_dbg(FYI, "set compression rc %d\n", rc); |
Steve French | 64a5cfa | 2013-10-14 15:31:32 -0500 | [diff] [blame] | 3137 | |
| 3138 | return rc; |
| 3139 | } |
| 3140 | |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3141 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3142 | SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3143 | struct smb_rqst *rqst, |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3144 | u64 persistent_fid, u64 volatile_fid, bool query_attrs) |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3145 | { |
| 3146 | struct smb2_close_req *req; |
| 3147 | struct kvec *iov = rqst->rq_iov; |
| 3148 | unsigned int total_len; |
| 3149 | int rc; |
| 3150 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3151 | rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server, |
| 3152 | (void **) &req, &total_len); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3153 | if (rc) |
| 3154 | return rc; |
| 3155 | |
| 3156 | req->PersistentFileId = persistent_fid; |
| 3157 | req->VolatileFileId = volatile_fid; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3158 | if (query_attrs) |
| 3159 | req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 3160 | else |
| 3161 | req->Flags = 0; |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3162 | iov[0].iov_base = (char *)req; |
| 3163 | iov[0].iov_len = total_len; |
| 3164 | |
| 3165 | return 0; |
| 3166 | } |
| 3167 | |
| 3168 | void |
| 3169 | SMB2_close_free(struct smb_rqst *rqst) |
| 3170 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 3171 | if (rqst && rqst->rq_iov) |
| 3172 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | int |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3176 | __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, |
| 3177 | u64 persistent_fid, u64 volatile_fid, |
| 3178 | struct smb2_file_network_open_info *pbuf) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3179 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3180 | struct smb_rqst rqst; |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3181 | struct smb2_close_rsp *rsp = NULL; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3182 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3183 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3184 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3185 | struct kvec rsp_iov; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 3186 | int resp_buftype = CIFS_NO_BUFFER; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3187 | int rc = 0; |
Steve French | 9e8fae2 | 2019-12-02 17:55:41 -0600 | [diff] [blame] | 3188 | int flags = 0; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3189 | bool query_attrs = false; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3190 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3191 | cifs_dbg(FYI, "Close\n"); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3192 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3193 | if (!ses || !server) |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3194 | return -EIO; |
| 3195 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 3196 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 3197 | flags |= CIFS_TRANSFORM_REQ; |
| 3198 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3199 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3200 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3201 | rqst.rq_iov = iov; |
| 3202 | rqst.rq_nvec = 1; |
| 3203 | |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3204 | /* check if need to ask server to return timestamps in close response */ |
| 3205 | if (pbuf) |
| 3206 | query_attrs = true; |
| 3207 | |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3208 | trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3209 | rc = SMB2_close_init(tcon, server, |
| 3210 | &rqst, persistent_fid, volatile_fid, |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3211 | query_attrs); |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3212 | if (rc) |
| 3213 | goto close_exit; |
| 3214 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3215 | rc = cifs_send_recv(xid, ses, server, |
| 3216 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3217 | rsp = (struct smb2_close_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3218 | |
| 3219 | if (rc != 0) { |
Namjae Jeon | d4a029d | 2014-08-20 19:39:59 +0900 | [diff] [blame] | 3220 | cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3221 | trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3222 | rc); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3223 | goto close_exit; |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3224 | } else { |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3225 | trace_smb3_close_done(xid, persistent_fid, tcon->tid, |
| 3226 | ses->Suid); |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3227 | /* |
| 3228 | * Note that have to subtract 4 since struct network_open_info |
| 3229 | * has a final 4 byte pad that close response does not have |
| 3230 | */ |
| 3231 | if (pbuf) |
| 3232 | memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4); |
| 3233 | } |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3234 | |
Steve French | fae8044 | 2018-10-19 17:14:32 -0500 | [diff] [blame] | 3235 | atomic_dec(&tcon->num_remote_opens); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3236 | close_exit: |
Ronnie Sahlberg | 8eb4ecf | 2018-08-01 09:26:16 +1000 | [diff] [blame] | 3237 | SMB2_close_free(&rqst); |
Pavel Shilovsky | 2503a0d | 2011-12-26 22:58:46 +0400 | [diff] [blame] | 3238 | free_rsp_buf(resp_buftype, rsp); |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3239 | |
| 3240 | /* retry close in a worker thread if this one is interrupted */ |
| 3241 | if (rc == -EINTR) { |
Steve French | 9e8fae2 | 2019-12-02 17:55:41 -0600 | [diff] [blame] | 3242 | int tmp_rc; |
| 3243 | |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3244 | tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid, |
| 3245 | volatile_fid); |
| 3246 | if (tmp_rc) |
| 3247 | cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n", |
| 3248 | persistent_fid, tmp_rc); |
| 3249 | } |
Pavel Shilovsky | 9150c3a | 2019-11-21 11:35:12 -0800 | [diff] [blame] | 3250 | return rc; |
Ronnie Sahlberg | 97ca176 | 2018-04-26 08:50:49 -0600 | [diff] [blame] | 3251 | } |
| 3252 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3253 | int |
Steve French | 43f8a6a | 2019-12-02 21:46:54 -0600 | [diff] [blame] | 3254 | SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, |
| 3255 | u64 persistent_fid, u64 volatile_fid) |
| 3256 | { |
| 3257 | return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL); |
| 3258 | } |
| 3259 | |
| 3260 | int |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3261 | smb2_validate_iov(unsigned int offset, unsigned int buffer_length, |
| 3262 | struct kvec *iov, unsigned int min_buf_size) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3263 | { |
Ronnie Sahlberg | c1596ff | 2018-04-09 18:06:30 +1000 | [diff] [blame] | 3264 | unsigned int smb_len = iov->iov_len; |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 3265 | char *end_of_smb = smb_len + (char *)iov->iov_base; |
| 3266 | char *begin_of_buf = offset + (char *)iov->iov_base; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3267 | char *end_of_buf = begin_of_buf + buffer_length; |
| 3268 | |
| 3269 | |
| 3270 | if (buffer_length < min_buf_size) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3271 | cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n", |
| 3272 | buffer_length, min_buf_size); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3273 | return -EINVAL; |
| 3274 | } |
| 3275 | |
| 3276 | /* check if beyond RFC1001 maximum length */ |
| 3277 | if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3278 | cifs_dbg(VFS, "buffer length %d or smb length %d too large\n", |
| 3279 | buffer_length, smb_len); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3280 | return -EINVAL; |
| 3281 | } |
| 3282 | |
| 3283 | if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 3284 | cifs_dbg(VFS, "Invalid server response, bad offset to data\n"); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3285 | return -EINVAL; |
| 3286 | } |
| 3287 | |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | * If SMB buffer fields are valid, copy into temporary buffer to hold result. |
| 3293 | * Caller must free buffer. |
| 3294 | */ |
Ronnie Sahlberg | c5a5f38 | 2018-09-03 13:33:41 +1000 | [diff] [blame] | 3295 | int |
| 3296 | smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length, |
| 3297 | struct kvec *iov, unsigned int minbufsize, |
| 3298 | char *data) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3299 | { |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 3300 | char *begin_of_buf = offset + (char *)iov->iov_base; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3301 | int rc; |
| 3302 | |
| 3303 | if (!data) |
| 3304 | return -EINVAL; |
| 3305 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 3306 | rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3307 | if (rc) |
| 3308 | return rc; |
| 3309 | |
| 3310 | memcpy(data, begin_of_buf, buffer_length); |
| 3311 | |
| 3312 | return 0; |
| 3313 | } |
| 3314 | |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3315 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3316 | SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3317 | struct smb_rqst *rqst, |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3318 | u64 persistent_fid, u64 volatile_fid, |
| 3319 | u8 info_class, u8 info_type, u32 additional_info, |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3320 | size_t output_len, size_t input_len, void *input) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3321 | { |
| 3322 | struct smb2_query_info_req *req; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3323 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 3324 | unsigned int total_len; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3325 | int rc; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3326 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3327 | rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server, |
| 3328 | (void **) &req, &total_len); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3329 | if (rc) |
| 3330 | return rc; |
| 3331 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3332 | req->InfoType = info_type; |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3333 | req->FileInfoClass = info_class; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3334 | req->PersistentFileId = persistent_fid; |
| 3335 | req->VolatileFileId = volatile_fid; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3336 | req->AdditionalInformation = cpu_to_le32(additional_info); |
Aurelien Aptel | 48923d2 | 2017-10-17 14:47:17 +0200 | [diff] [blame] | 3337 | |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3338 | req->OutputBufferLength = cpu_to_le32(output_len); |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3339 | if (input_len) { |
| 3340 | req->InputBufferLength = cpu_to_le32(input_len); |
| 3341 | /* total_len for smb query request never close to le16 max */ |
| 3342 | req->InputBufferOffset = cpu_to_le16(total_len - 1); |
| 3343 | memcpy(req->Buffer, input, input_len); |
| 3344 | } |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3345 | |
| 3346 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 3347 | /* 1 for Buffer */ |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3348 | iov[0].iov_len = total_len - 1 + input_len; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3349 | return 0; |
| 3350 | } |
| 3351 | |
| 3352 | void |
| 3353 | SMB2_query_info_free(struct smb_rqst *rqst) |
| 3354 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 3355 | if (rqst && rqst->rq_iov) |
| 3356 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | static int |
| 3360 | query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3361 | u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type, |
| 3362 | u32 additional_info, size_t output_len, size_t min_len, void **data, |
| 3363 | u32 *dlen) |
| 3364 | { |
| 3365 | struct smb_rqst rqst; |
| 3366 | struct smb2_query_info_rsp *rsp = NULL; |
| 3367 | struct kvec iov[1]; |
| 3368 | struct kvec rsp_iov; |
| 3369 | int rc = 0; |
Garry McNulty | ef2298a | 2018-10-03 20:51:21 +0100 | [diff] [blame] | 3370 | int resp_buftype = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3371 | struct cifs_ses *ses = tcon->ses; |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3372 | struct TCP_Server_Info *server; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3373 | int flags = 0; |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3374 | bool allocated = false; |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3375 | |
| 3376 | cifs_dbg(FYI, "Query Info\n"); |
| 3377 | |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3378 | if (!ses) |
| 3379 | return -EIO; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3380 | server = cifs_pick_channel(ses); |
Colin Ian King | ac6ad7a | 2019-09-02 16:10:59 +0100 | [diff] [blame] | 3381 | if (!server) |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3382 | return -EIO; |
| 3383 | |
| 3384 | if (smb3_encryption_required(tcon)) |
| 3385 | flags |= CIFS_TRANSFORM_REQ; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3386 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3387 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3388 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3389 | rqst.rq_iov = iov; |
| 3390 | rqst.rq_nvec = 1; |
| 3391 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3392 | rc = SMB2_query_info_init(tcon, server, |
| 3393 | &rqst, persistent_fid, volatile_fid, |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3394 | info_class, info_type, additional_info, |
Ronnie Sahlberg | f5b05d6 | 2018-10-07 19:19:58 -0500 | [diff] [blame] | 3395 | output_len, 0, NULL); |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3396 | if (rc) |
| 3397 | goto qinf_exit; |
| 3398 | |
Steve French | d42043a | 2019-02-26 21:58:30 -0600 | [diff] [blame] | 3399 | trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid, |
| 3400 | ses->Suid, info_class, (__u32)info_type); |
| 3401 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3402 | rc = cifs_send_recv(xid, ses, server, |
| 3403 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3404 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 3405 | |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3406 | if (rc) { |
| 3407 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3408 | trace_smb3_query_info_err(xid, persistent_fid, tcon->tid, |
| 3409 | ses->Suid, info_class, (__u32)info_type, rc); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3410 | goto qinf_exit; |
| 3411 | } |
| 3412 | |
Steve French | d42043a | 2019-02-26 21:58:30 -0600 | [diff] [blame] | 3413 | trace_smb3_query_info_done(xid, persistent_fid, tcon->tid, |
| 3414 | ses->Suid, info_class, (__u32)info_type); |
| 3415 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3416 | if (dlen) { |
| 3417 | *dlen = le32_to_cpu(rsp->OutputBufferLength); |
| 3418 | if (!*data) { |
| 3419 | *data = kmalloc(*dlen, GFP_KERNEL); |
| 3420 | if (!*data) { |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3421 | cifs_tcon_dbg(VFS, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3422 | "Error %d allocating memory for acl\n", |
| 3423 | rc); |
| 3424 | *dlen = 0; |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3425 | rc = -ENOMEM; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3426 | goto qinf_exit; |
| 3427 | } |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3428 | allocated = true; |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3429 | } |
| 3430 | } |
| 3431 | |
Ronnie Sahlberg | c5a5f38 | 2018-09-03 13:33:41 +1000 | [diff] [blame] | 3432 | rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 3433 | le32_to_cpu(rsp->OutputBufferLength), |
| 3434 | &rsp_iov, min_len, *data); |
Colin Ian King | 73aaf92 | 2019-01-16 16:28:59 +0000 | [diff] [blame] | 3435 | if (rc && allocated) { |
| 3436 | kfree(*data); |
| 3437 | *data = NULL; |
| 3438 | *dlen = 0; |
| 3439 | } |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3440 | |
| 3441 | qinf_exit: |
Ronnie Sahlberg | 296ecba | 2018-08-01 09:26:17 +1000 | [diff] [blame] | 3442 | SMB2_query_info_free(&rqst); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 3443 | free_rsp_buf(resp_buftype, rsp); |
| 3444 | return rc; |
| 3445 | } |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3446 | |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3447 | int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3448 | u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data) |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3449 | { |
| 3450 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3451 | FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0, |
Pavel Shilovsky | 1bbe499 | 2014-08-22 13:32:11 +0400 | [diff] [blame] | 3452 | sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3453 | sizeof(struct smb2_file_all_info), (void **)&data, |
| 3454 | NULL); |
| 3455 | } |
| 3456 | |
| 3457 | int |
Steve French | b1bc187 | 2020-06-11 20:23:38 -0500 | [diff] [blame] | 3458 | SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 3459 | u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen) |
| 3460 | { |
| 3461 | size_t output_len = sizeof(struct smb311_posix_qinfo *) + |
| 3462 | (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2); |
| 3463 | *plen = 0; |
| 3464 | |
| 3465 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
| 3466 | SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0, |
| 3467 | output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen); |
| 3468 | } |
| 3469 | |
| 3470 | int |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3471 | SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon, |
| 3472 | u64 persistent_fid, u64 volatile_fid, |
| 3473 | void **data, u32 *plen) |
| 3474 | { |
| 3475 | __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO; |
| 3476 | *plen = 0; |
| 3477 | |
| 3478 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
| 3479 | 0, SMB2_O_INFO_SECURITY, additional_info, |
Shirish Pargaonkar | ee25c6d | 2018-06-04 06:46:22 -0500 | [diff] [blame] | 3480 | SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | int |
| 3484 | SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon, |
| 3485 | u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid) |
| 3486 | { |
| 3487 | return query_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3488 | FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0, |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3489 | sizeof(struct smb2_file_internal_info), |
Shirish Pargaonkar | 42c493c | 2017-06-22 22:51:31 -0500 | [diff] [blame] | 3490 | sizeof(struct smb2_file_internal_info), |
| 3491 | (void **)&uniqueid, NULL); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3494 | /* |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3495 | * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory |
| 3496 | * See MS-SMB2 2.2.35 and 2.2.36 |
| 3497 | */ |
| 3498 | |
zhengbin | 388962e | 2019-09-23 15:06:18 +0800 | [diff] [blame] | 3499 | static int |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3500 | SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3501 | struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3502 | u64 persistent_fid, u64 volatile_fid, |
| 3503 | u32 completion_filter, bool watch_tree) |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3504 | { |
| 3505 | struct smb2_change_notify_req *req; |
| 3506 | struct kvec *iov = rqst->rq_iov; |
| 3507 | unsigned int total_len; |
| 3508 | int rc; |
| 3509 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3510 | rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server, |
| 3511 | (void **) &req, &total_len); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3512 | if (rc) |
| 3513 | return rc; |
| 3514 | |
| 3515 | req->PersistentFileId = persistent_fid; |
| 3516 | req->VolatileFileId = volatile_fid; |
Steve French | d26c2dd | 2020-02-06 06:00:14 -0600 | [diff] [blame] | 3517 | /* See note 354 of MS-SMB2, 64K max */ |
Steve French | 52870d5 | 2019-10-01 21:25:46 -0500 | [diff] [blame] | 3518 | req->OutputBufferLength = |
| 3519 | cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3520 | req->CompletionFilter = cpu_to_le32(completion_filter); |
| 3521 | if (watch_tree) |
| 3522 | req->Flags = cpu_to_le16(SMB2_WATCH_TREE); |
| 3523 | else |
| 3524 | req->Flags = 0; |
| 3525 | |
| 3526 | iov[0].iov_base = (char *)req; |
| 3527 | iov[0].iov_len = total_len; |
| 3528 | |
| 3529 | return 0; |
| 3530 | } |
| 3531 | |
| 3532 | int |
| 3533 | SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon, |
| 3534 | u64 persistent_fid, u64 volatile_fid, bool watch_tree, |
| 3535 | u32 completion_filter) |
| 3536 | { |
| 3537 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3538 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3539 | struct smb_rqst rqst; |
| 3540 | struct kvec iov[1]; |
| 3541 | struct kvec rsp_iov = {NULL, 0}; |
| 3542 | int resp_buftype = CIFS_NO_BUFFER; |
| 3543 | int flags = 0; |
| 3544 | int rc = 0; |
| 3545 | |
| 3546 | cifs_dbg(FYI, "change notify\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3547 | if (!ses || !server) |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3548 | return -EIO; |
| 3549 | |
| 3550 | if (smb3_encryption_required(tcon)) |
| 3551 | flags |= CIFS_TRANSFORM_REQ; |
| 3552 | |
| 3553 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 3554 | memset(&iov, 0, sizeof(iov)); |
| 3555 | rqst.rq_iov = iov; |
| 3556 | rqst.rq_nvec = 1; |
| 3557 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3558 | rc = SMB2_notify_init(xid, &rqst, tcon, server, |
| 3559 | persistent_fid, volatile_fid, |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3560 | completion_filter, watch_tree); |
| 3561 | if (rc) |
| 3562 | goto cnotify_exit; |
| 3563 | |
| 3564 | trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3565 | (u8)watch_tree, completion_filter); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3566 | rc = cifs_send_recv(xid, ses, server, |
| 3567 | &rqst, &resp_buftype, flags, &rsp_iov); |
Steve French | c349818 | 2019-09-15 22:38:52 -0500 | [diff] [blame] | 3568 | |
| 3569 | if (rc != 0) { |
| 3570 | cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE); |
| 3571 | trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3572 | (u8)watch_tree, completion_filter, rc); |
| 3573 | } else |
| 3574 | trace_smb3_notify_done(xid, persistent_fid, tcon->tid, |
| 3575 | ses->Suid, (u8)watch_tree, completion_filter); |
| 3576 | |
| 3577 | cnotify_exit: |
| 3578 | if (rqst.rq_iov) |
| 3579 | cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */ |
| 3580 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
| 3581 | return rc; |
| 3582 | } |
| 3583 | |
| 3584 | |
| 3585 | |
| 3586 | /* |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3587 | * This is a no-op for now. We're not really interested in the reply, but |
| 3588 | * rather in the fact that the server sent one and that server->lstrp |
| 3589 | * gets updated. |
| 3590 | * |
| 3591 | * FIXME: maybe we should consider checking that the reply matches request? |
| 3592 | */ |
| 3593 | static void |
| 3594 | smb2_echo_callback(struct mid_q_entry *mid) |
| 3595 | { |
| 3596 | struct TCP_Server_Info *server = mid->callback_data; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3597 | struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3598 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3599 | |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 3600 | if (mid->mid_state == MID_RESPONSE_RECEIVED |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3601 | || mid->mid_state == MID_RESPONSE_MALFORMED) { |
| 3602 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 3603 | credits.instance = server->reconnect_instance; |
| 3604 | } |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3605 | |
| 3606 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3607 | add_credits(server, &credits, CIFS_ECHO_OP); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3608 | } |
| 3609 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3610 | void smb2_reconnect_server(struct work_struct *work) |
| 3611 | { |
| 3612 | struct TCP_Server_Info *server = container_of(work, |
| 3613 | struct TCP_Server_Info, reconnect.work); |
| 3614 | struct cifs_ses *ses; |
| 3615 | struct cifs_tcon *tcon, *tcon2; |
| 3616 | struct list_head tmp_list; |
| 3617 | int tcon_exist = false; |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3618 | int rc; |
| 3619 | int resched = false; |
| 3620 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3621 | |
| 3622 | /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */ |
| 3623 | mutex_lock(&server->reconnect_mutex); |
| 3624 | |
| 3625 | INIT_LIST_HEAD(&tmp_list); |
| 3626 | cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n"); |
| 3627 | |
| 3628 | spin_lock(&cifs_tcp_ses_lock); |
| 3629 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
| 3630 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 3631 | if (tcon->need_reconnect || tcon->need_reopen_files) { |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3632 | tcon->tc_count++; |
| 3633 | list_add_tail(&tcon->rlist, &tmp_list); |
| 3634 | tcon_exist = true; |
| 3635 | } |
| 3636 | } |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3637 | /* |
| 3638 | * IPC has the same lifetime as its session and uses its |
| 3639 | * refcount. |
| 3640 | */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3641 | if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) { |
| 3642 | list_add_tail(&ses->tcon_ipc->rlist, &tmp_list); |
| 3643 | tcon_exist = true; |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3644 | ses->ses_count++; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3645 | } |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3646 | } |
| 3647 | /* |
| 3648 | * Get the reference to server struct to be sure that the last call of |
| 3649 | * cifs_put_tcon() in the loop below won't release the server pointer. |
| 3650 | */ |
| 3651 | if (tcon_exist) |
| 3652 | server->srv_count++; |
| 3653 | |
| 3654 | spin_unlock(&cifs_tcp_ses_lock); |
| 3655 | |
| 3656 | list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) { |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3657 | rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3658 | if (!rc) |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 3659 | cifs_reopen_persistent_handles(tcon); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3660 | else |
| 3661 | resched = true; |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3662 | list_del_init(&tcon->rlist); |
Ronnie Sahlberg | 0ff2b01 | 2019-06-05 10:15:34 +1000 | [diff] [blame] | 3663 | if (tcon->ipc) |
| 3664 | cifs_put_smb_ses(tcon->ses); |
| 3665 | else |
| 3666 | cifs_put_tcon(tcon); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | cifs_dbg(FYI, "Reconnecting tcons finished\n"); |
Germano Percossi | 18ea431 | 2017-04-07 12:29:36 +0100 | [diff] [blame] | 3670 | if (resched) |
| 3671 | queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3672 | mutex_unlock(&server->reconnect_mutex); |
| 3673 | |
| 3674 | /* now we can safely release srv struct */ |
| 3675 | if (tcon_exist) |
| 3676 | cifs_put_tcp_session(server, 1); |
| 3677 | } |
| 3678 | |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3679 | int |
| 3680 | SMB2_echo(struct TCP_Server_Info *server) |
| 3681 | { |
| 3682 | struct smb2_echo_req *req; |
| 3683 | int rc = 0; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3684 | struct kvec iov[1]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3685 | struct smb_rqst rqst = { .rq_iov = iov, |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3686 | .rq_nvec = 1 }; |
Ronnie Sahlberg | 7f7ae75 | 2017-11-09 12:14:21 +1100 | [diff] [blame] | 3687 | unsigned int total_len; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3688 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3689 | cifs_dbg(FYI, "In echo request\n"); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3690 | |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 3691 | if (server->tcpStatus == CifsNeedNegotiate) { |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3692 | /* No need to send echo on newly established connections */ |
Stefan Metzmacher | b08484d | 2020-02-24 14:14:59 +0100 | [diff] [blame] | 3693 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3694 | return rc; |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 3695 | } |
| 3696 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3697 | rc = smb2_plain_req_init(SMB2_ECHO, NULL, server, |
| 3698 | (void **)&req, &total_len); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3699 | if (rc) |
| 3700 | return rc; |
| 3701 | |
Ronnie Sahlberg | 7f7ae75 | 2017-11-09 12:14:21 +1100 | [diff] [blame] | 3702 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3703 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3704 | iov[0].iov_len = total_len; |
| 3705 | iov[0].iov_base = (char *)req; |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3706 | |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 3707 | rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 3708 | server, CIFS_ECHO_OP, NULL); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3709 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3710 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
Pavel Shilovsky | 9094fad | 2012-07-12 18:30:44 +0400 | [diff] [blame] | 3711 | |
| 3712 | cifs_small_buf_release(req); |
| 3713 | return rc; |
| 3714 | } |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3715 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3716 | void |
| 3717 | SMB2_flush_free(struct smb_rqst *rqst) |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3718 | { |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3719 | if (rqst && rqst->rq_iov) |
| 3720 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
| 3721 | } |
| 3722 | |
| 3723 | int |
| 3724 | SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3725 | struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 3726 | u64 persistent_fid, u64 volatile_fid) |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3727 | { |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3728 | struct smb2_flush_req *req; |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3729 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | 1f444e4 | 2017-11-20 11:24:39 +1100 | [diff] [blame] | 3730 | unsigned int total_len; |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3731 | int rc; |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3732 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3733 | rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server, |
| 3734 | (void **) &req, &total_len); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3735 | if (rc) |
| 3736 | return rc; |
| 3737 | |
| 3738 | req->PersistentFileId = persistent_fid; |
| 3739 | req->VolatileFileId = volatile_fid; |
| 3740 | |
| 3741 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 1f444e4 | 2017-11-20 11:24:39 +1100 | [diff] [blame] | 3742 | iov[0].iov_len = total_len; |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3743 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3744 | return 0; |
| 3745 | } |
| 3746 | |
| 3747 | int |
| 3748 | SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, |
| 3749 | u64 volatile_fid) |
| 3750 | { |
| 3751 | struct cifs_ses *ses = tcon->ses; |
| 3752 | struct smb_rqst rqst; |
| 3753 | struct kvec iov[1]; |
| 3754 | struct kvec rsp_iov = {NULL, 0}; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3755 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3756 | int resp_buftype = CIFS_NO_BUFFER; |
| 3757 | int flags = 0; |
| 3758 | int rc = 0; |
| 3759 | |
| 3760 | cifs_dbg(FYI, "flush\n"); |
| 3761 | if (!ses || !(ses->server)) |
| 3762 | return -EIO; |
| 3763 | |
| 3764 | if (smb3_encryption_required(tcon)) |
| 3765 | flags |= CIFS_TRANSFORM_REQ; |
| 3766 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3767 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3768 | memset(&iov, 0, sizeof(iov)); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 3769 | rqst.rq_iov = iov; |
| 3770 | rqst.rq_nvec = 1; |
| 3771 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3772 | rc = SMB2_flush_init(xid, &rqst, tcon, server, |
| 3773 | persistent_fid, volatile_fid); |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3774 | if (rc) |
| 3775 | goto flush_exit; |
| 3776 | |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3777 | trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3778 | rc = cifs_send_recv(xid, ses, server, |
| 3779 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3780 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3781 | if (rc != 0) { |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3782 | cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 3783 | trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid, |
| 3784 | rc); |
Steve French | f90f979 | 2019-09-03 18:35:42 -0500 | [diff] [blame] | 3785 | } else |
| 3786 | trace_smb3_flush_done(xid, persistent_fid, tcon->tid, |
| 3787 | ses->Suid); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3788 | |
Ronnie Sahlberg | 86e14e1 | 2019-07-16 15:07:08 +1000 | [diff] [blame] | 3789 | flush_exit: |
| 3790 | SMB2_flush_free(&rqst); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 3791 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | 7a5cfb1 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3792 | return rc; |
| 3793 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3794 | |
| 3795 | /* |
| 3796 | * To form a chain of read requests, any read requests after the first should |
| 3797 | * have the end_of_chain boolean set to true. |
| 3798 | */ |
| 3799 | static int |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3800 | smb2_new_read_req(void **buf, unsigned int *total_len, |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3801 | struct cifs_io_parms *io_parms, struct cifs_readdata *rdata, |
| 3802 | unsigned int remaining_bytes, int request_type) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3803 | { |
| 3804 | int rc = -EACCES; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3805 | struct smb2_read_plain_req *req = NULL; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3806 | struct smb2_sync_hdr *shdr; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3807 | struct TCP_Server_Info *server = io_parms->server; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3808 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3809 | rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server, |
| 3810 | (void **) &req, total_len); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3811 | if (rc) |
| 3812 | return rc; |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3813 | |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 3814 | if (server == NULL) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3815 | return -ECONNABORTED; |
| 3816 | |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3817 | shdr = &req->sync_hdr; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3818 | shdr->ProcessId = cpu_to_le32(io_parms->pid); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3819 | |
| 3820 | req->PersistentFileId = io_parms->persistent_fid; |
| 3821 | req->VolatileFileId = io_parms->volatile_fid; |
| 3822 | req->ReadChannelInfoOffset = 0; /* reserved */ |
| 3823 | req->ReadChannelInfoLength = 0; /* reserved */ |
| 3824 | req->Channel = 0; /* reserved */ |
| 3825 | req->MinimumCount = 0; |
| 3826 | req->Length = cpu_to_le32(io_parms->length); |
| 3827 | req->Offset = cpu_to_le64(io_parms->offset); |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 3828 | |
| 3829 | trace_smb3_read_enter(0 /* xid */, |
| 3830 | io_parms->persistent_fid, |
| 3831 | io_parms->tcon->tid, io_parms->tcon->ses->Suid, |
| 3832 | io_parms->offset, io_parms->length); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3833 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3834 | /* |
| 3835 | * If we want to do a RDMA write, fill in and append |
| 3836 | * smbd_buffer_descriptor_v1 to the end of read request |
| 3837 | */ |
Long Li | bb4c041 | 2018-04-17 12:17:08 -0700 | [diff] [blame] | 3838 | if (server->rdma && rdata && !server->sign && |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3839 | rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3840 | |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3841 | struct smbd_buffer_descriptor_v1 *v1; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3842 | bool need_invalidate = server->dialect == SMB30_PROT_ID; |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3843 | |
| 3844 | rdata->mr = smbd_register_mr( |
| 3845 | server->smbd_conn, rdata->pages, |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 3846 | rdata->nr_pages, rdata->page_offset, |
| 3847 | rdata->tailsz, true, need_invalidate); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3848 | if (!rdata->mr) |
Long Li | b797209 | 2019-04-05 21:36:34 +0000 | [diff] [blame] | 3849 | return -EAGAIN; |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3850 | |
| 3851 | req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; |
| 3852 | if (need_invalidate) |
| 3853 | req->Channel = SMB2_CHANNEL_RDMA_V1; |
| 3854 | req->ReadChannelInfoOffset = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3855 | cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer)); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3856 | req->ReadChannelInfoLength = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3857 | cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1)); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3858 | v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0]; |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 3859 | v1->offset = cpu_to_le64(rdata->mr->mr->iova); |
| 3860 | v1->token = cpu_to_le32(rdata->mr->mr->rkey); |
| 3861 | v1->length = cpu_to_le32(rdata->mr->mr->length); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3862 | |
| 3863 | *total_len += sizeof(*v1) - 1; |
| 3864 | } |
| 3865 | #endif |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3866 | if (request_type & CHAINED_REQUEST) { |
| 3867 | if (!(request_type & END_OF_CHAIN)) { |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 3868 | /* next 8-byte aligned request */ |
| 3869 | *total_len = DIV_ROUND_UP(*total_len, 8) * 8; |
| 3870 | shdr->NextCommand = cpu_to_le32(*total_len); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3871 | } else /* END_OF_CHAIN */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3872 | shdr->NextCommand = 0; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3873 | if (request_type & RELATED_REQUEST) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3874 | shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3875 | /* |
| 3876 | * Related requests use info from previous read request |
| 3877 | * in chain. |
| 3878 | */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3879 | shdr->SessionId = 0xFFFFFFFF; |
| 3880 | shdr->TreeId = 0xFFFFFFFF; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3881 | req->PersistentFileId = 0xFFFFFFFF; |
| 3882 | req->VolatileFileId = 0xFFFFFFFF; |
| 3883 | } |
| 3884 | } |
| 3885 | if (remaining_bytes > io_parms->length) |
| 3886 | req->RemainingBytes = cpu_to_le32(remaining_bytes); |
| 3887 | else |
| 3888 | req->RemainingBytes = 0; |
| 3889 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3890 | *buf = req; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3891 | return rc; |
| 3892 | } |
| 3893 | |
| 3894 | static void |
| 3895 | smb2_readv_callback(struct mid_q_entry *mid) |
| 3896 | { |
| 3897 | struct cifs_readdata *rdata = mid->callback_data; |
| 3898 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3899 | struct TCP_Server_Info *server = rdata->server; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3900 | struct smb2_sync_hdr *shdr = |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 3901 | (struct smb2_sync_hdr *)rdata->iov[0].iov_base; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3902 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Steve French | 46f17d1 | 2019-09-04 23:07:52 -0500 | [diff] [blame] | 3903 | struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], |
| 3904 | .rq_nvec = 1, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3905 | .rq_pages = rdata->pages, |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3906 | .rq_offset = rdata->page_offset, |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3907 | .rq_npages = rdata->nr_pages, |
| 3908 | .rq_pagesz = rdata->pagesz, |
| 3909 | .rq_tailsz = rdata->tailsz }; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3910 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3911 | WARN_ONCE(rdata->server != mid->server, |
| 3912 | "rdata server %p != mid server %p", |
| 3913 | rdata->server, mid->server); |
| 3914 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3915 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
| 3916 | __func__, mid->mid, mid->mid_state, rdata->result, |
| 3917 | rdata->bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3918 | |
| 3919 | switch (mid->mid_state) { |
| 3920 | case MID_RESPONSE_RECEIVED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3921 | credits.value = le16_to_cpu(shdr->CreditRequest); |
| 3922 | credits.instance = server->reconnect_instance; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3923 | /* result already set, check signature */ |
Pavel Shilovsky | 4326ed2 | 2016-11-17 15:24:46 -0800 | [diff] [blame] | 3924 | if (server->sign && !mid->decrypted) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3925 | int rc; |
| 3926 | |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 3927 | rc = smb2_verify_signature(&rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3928 | if (rc) |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 3929 | cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3930 | rc); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 3931 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3932 | /* FIXME: should this be counted toward the initiating task? */ |
Pavel Shilovsky | 34a54d6 | 2014-07-10 10:03:29 +0400 | [diff] [blame] | 3933 | task_io_account_read(rdata->got_bytes); |
| 3934 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3935 | break; |
| 3936 | case MID_REQUEST_SUBMITTED: |
| 3937 | case MID_RETRY_NEEDED: |
| 3938 | rdata->result = -EAGAIN; |
Pavel Shilovsky | d913ed1 | 2014-07-10 11:31:48 +0400 | [diff] [blame] | 3939 | if (server->sign && rdata->got_bytes) |
| 3940 | /* reset bytes number since we can not check a sign */ |
| 3941 | rdata->got_bytes = 0; |
| 3942 | /* FIXME: should this be counted toward the initiating task? */ |
| 3943 | task_io_account_read(rdata->got_bytes); |
| 3944 | cifs_stats_bytes_read(tcon, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3945 | break; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 3946 | case MID_RESPONSE_MALFORMED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3947 | credits.value = le16_to_cpu(shdr->CreditRequest); |
| 3948 | credits.instance = server->reconnect_instance; |
Miaohe Lin | 30b5ae2 | 2020-08-08 16:36:37 +0800 | [diff] [blame] | 3949 | fallthrough; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3950 | default: |
Pavel Shilovsky | 6b15eb1 | 2019-01-18 15:46:14 -0800 | [diff] [blame] | 3951 | rdata->result = -EIO; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3952 | } |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3953 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3954 | /* |
| 3955 | * If this rdata has a memmory registered, the MR can be freed |
| 3956 | * MR needs to be freed as soon as I/O finishes to prevent deadlock |
| 3957 | * because they have limited number and are used for future I/Os |
| 3958 | */ |
| 3959 | if (rdata->mr) { |
| 3960 | smbd_deregister_mr(rdata->mr); |
| 3961 | rdata->mr = NULL; |
| 3962 | } |
| 3963 | #endif |
Pavel Shilovsky | 082aaa8 | 2019-01-18 15:54:34 -0800 | [diff] [blame] | 3964 | if (rdata->result && rdata->result != -ENODATA) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3965 | cifs_stats_fail_inc(tcon, SMB2_READ_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 3966 | trace_smb3_read_err(0 /* xid */, |
| 3967 | rdata->cfile->fid.persistent_fid, |
| 3968 | tcon->tid, tcon->ses->Suid, rdata->offset, |
| 3969 | rdata->bytes, rdata->result); |
| 3970 | } else |
| 3971 | trace_smb3_read_done(0 /* xid */, |
| 3972 | rdata->cfile->fid.persistent_fid, |
| 3973 | tcon->tid, tcon->ses->Suid, |
| 3974 | rdata->offset, rdata->got_bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3975 | |
| 3976 | queue_work(cifsiod_wq, &rdata->work); |
| 3977 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 3978 | add_credits(server, &credits, 0); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3981 | /* 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] | 3982 | int |
| 3983 | smb2_async_readv(struct cifs_readdata *rdata) |
| 3984 | { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3985 | int rc, flags = 0; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 3986 | char *buf; |
| 3987 | struct smb2_sync_hdr *shdr; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3988 | struct cifs_io_parms io_parms; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3989 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 3990 | .rq_nvec = 1 }; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3991 | struct TCP_Server_Info *server; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3992 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 3993 | unsigned int total_len; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3994 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3995 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
| 3996 | __func__, rdata->offset, rdata->bytes); |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3997 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 3998 | if (!rdata->server) |
| 3999 | rdata->server = cifs_pick_channel(tcon->ses); |
| 4000 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4001 | io_parms.tcon = tlink_tcon(rdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4002 | io_parms.server = server = rdata->server; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4003 | io_parms.offset = rdata->offset; |
| 4004 | io_parms.length = rdata->bytes; |
| 4005 | io_parms.persistent_fid = rdata->cfile->fid.persistent_fid; |
| 4006 | io_parms.volatile_fid = rdata->cfile->fid.volatile_fid; |
| 4007 | io_parms.pid = rdata->pid; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4008 | |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 4009 | rc = smb2_new_read_req( |
| 4010 | (void **) &buf, &total_len, &io_parms, rdata, 0, 0); |
Pavel Shilovsky | f0b93cb | 2019-01-25 11:10:00 -0800 | [diff] [blame] | 4011 | if (rc) |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4012 | return rc; |
| 4013 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4014 | if (smb3_encryption_required(io_parms.tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4015 | flags |= CIFS_TRANSFORM_REQ; |
| 4016 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4017 | rdata->iov[0].iov_base = buf; |
| 4018 | rdata->iov[0].iov_len = total_len; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 4019 | |
| 4020 | shdr = (struct smb2_sync_hdr *)buf; |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4021 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4022 | if (rdata->credits.value > 0) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4023 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes, |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4024 | SMB2_MAX_BUFFER_SIZE)); |
Pavel Shilovsky | b983f7e | 2018-12-19 22:49:09 +0000 | [diff] [blame] | 4025 | shdr->CreditRequest = |
| 4026 | cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4027 | |
| 4028 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
| 4029 | if (rc) |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4030 | goto async_readv_out; |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4031 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4032 | flags |= CIFS_HAS_CREDITS; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 4033 | } |
| 4034 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4035 | kref_get(&rdata->refcount); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4036 | rc = cifs_call_async(server, &rqst, |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4037 | cifs_readv_receive, smb2_readv_callback, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 4038 | smb3_handle_read_data, rdata, flags, |
| 4039 | &rdata->credits); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4040 | if (rc) { |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4041 | kref_put(&rdata->refcount, cifs_readdata_release); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4042 | cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4043 | trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid, |
| 4044 | io_parms.tcon->tid, |
| 4045 | io_parms.tcon->ses->Suid, |
| 4046 | io_parms.offset, io_parms.length, rc); |
| 4047 | } |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4048 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4049 | async_readv_out: |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4050 | cifs_small_buf_release(buf); |
| 4051 | return rc; |
| 4052 | } |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4053 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4054 | int |
| 4055 | SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 4056 | unsigned int *nbytes, char **buf, int *buf_type) |
| 4057 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4058 | struct smb_rqst rqst; |
Colin Ian King | 1efd4fc | 2019-07-31 10:05:26 +0100 | [diff] [blame] | 4059 | int resp_buftype, rc; |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 4060 | struct smb2_read_plain_req *req = NULL; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4061 | struct smb2_read_rsp *rsp = NULL; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4062 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4063 | struct kvec rsp_iov; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4064 | unsigned int total_len; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4065 | int flags = CIFS_LOG_ERROR; |
| 4066 | struct cifs_ses *ses = io_parms->tcon->ses; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4067 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4068 | if (!io_parms->server) |
| 4069 | io_parms->server = cifs_pick_channel(io_parms->tcon->ses); |
| 4070 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4071 | *nbytes = 0; |
Long Li | 2dabfd5 | 2017-11-07 01:54:53 -0700 | [diff] [blame] | 4072 | 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] | 4073 | if (rc) |
| 4074 | return rc; |
| 4075 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4076 | if (smb3_encryption_required(io_parms->tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4077 | flags |= CIFS_TRANSFORM_REQ; |
| 4078 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4079 | iov[0].iov_base = (char *)req; |
| 4080 | iov[0].iov_len = total_len; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4081 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4082 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4083 | rqst.rq_iov = iov; |
| 4084 | rqst.rq_nvec = 1; |
| 4085 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4086 | rc = cifs_send_recv(xid, ses, io_parms->server, |
| 4087 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4088 | rsp = (struct smb2_read_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4089 | |
| 4090 | if (rc) { |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4091 | if (rc != -ENODATA) { |
| 4092 | cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE); |
| 4093 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4094 | trace_smb3_read_err(xid, req->PersistentFileId, |
| 4095 | io_parms->tcon->tid, ses->Suid, |
| 4096 | io_parms->offset, io_parms->length, |
| 4097 | rc); |
Steve French | b0a42f2 | 2019-02-25 15:02:58 -0600 | [diff] [blame] | 4098 | } else |
| 4099 | trace_smb3_read_done(xid, req->PersistentFileId, |
| 4100 | io_parms->tcon->tid, ses->Suid, |
| 4101 | io_parms->offset, 0); |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4102 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Ronnie Sahlberg | 05fd5c2 | 2019-04-23 16:39:45 +1000 | [diff] [blame] | 4103 | cifs_small_buf_release(req); |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4104 | return rc == -ENODATA ? 0 : rc; |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4105 | } else |
| 4106 | trace_smb3_read_done(xid, req->PersistentFileId, |
| 4107 | io_parms->tcon->tid, ses->Suid, |
| 4108 | io_parms->offset, io_parms->length); |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4109 | |
ZhangXiaoxu | 088aaf1 | 2019-04-06 15:47:39 +0800 | [diff] [blame] | 4110 | cifs_small_buf_release(req); |
| 4111 | |
Ronnie Sahlberg | a821df3 | 2017-11-21 09:36:33 +1100 | [diff] [blame] | 4112 | *nbytes = le32_to_cpu(rsp->DataLength); |
| 4113 | if ((*nbytes > CIFS_MAX_MSGSIZE) || |
| 4114 | (*nbytes > io_parms->length)) { |
| 4115 | cifs_dbg(FYI, "bad length %d for count %d\n", |
| 4116 | *nbytes, io_parms->length); |
| 4117 | rc = -EIO; |
| 4118 | *nbytes = 0; |
| 4119 | } |
| 4120 | |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4121 | if (*buf) { |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 4122 | memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4123 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4124 | } else if (resp_buftype != CIFS_NO_BUFFER) { |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4125 | *buf = rsp_iov.iov_base; |
Pavel Shilovsky | d8e0503 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4126 | if (resp_buftype == CIFS_SMALL_BUFFER) |
| 4127 | *buf_type = CIFS_SMALL_BUFFER; |
| 4128 | else if (resp_buftype == CIFS_LARGE_BUFFER) |
| 4129 | *buf_type = CIFS_LARGE_BUFFER; |
| 4130 | } |
| 4131 | return rc; |
| 4132 | } |
| 4133 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4134 | /* |
| 4135 | * Check the mid_state and signature on received buffer (if any), and queue the |
| 4136 | * workqueue completion task. |
| 4137 | */ |
| 4138 | static void |
| 4139 | smb2_writev_callback(struct mid_q_entry *mid) |
| 4140 | { |
| 4141 | struct cifs_writedata *wdata = mid->callback_data; |
| 4142 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4143 | struct TCP_Server_Info *server = wdata->server; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4144 | unsigned int written; |
| 4145 | struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4146 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4147 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4148 | WARN_ONCE(wdata->server != mid->server, |
| 4149 | "wdata server %p != mid server %p", |
| 4150 | wdata->server, mid->server); |
| 4151 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4152 | switch (mid->mid_state) { |
| 4153 | case MID_RESPONSE_RECEIVED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4154 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 4155 | credits.instance = server->reconnect_instance; |
| 4156 | wdata->result = smb2_check_receive(mid, server, 0); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4157 | if (wdata->result != 0) |
| 4158 | break; |
| 4159 | |
| 4160 | written = le32_to_cpu(rsp->DataLength); |
| 4161 | /* |
| 4162 | * Mask off high 16 bits when bytes written as returned |
| 4163 | * by the server is greater than bytes requested by the |
| 4164 | * client. OS/2 servers are known to set incorrect |
| 4165 | * CountHigh values. |
| 4166 | */ |
| 4167 | if (written > wdata->bytes) |
| 4168 | written &= 0xFFFF; |
| 4169 | |
| 4170 | if (written < wdata->bytes) |
| 4171 | wdata->result = -ENOSPC; |
| 4172 | else |
| 4173 | wdata->bytes = written; |
| 4174 | break; |
| 4175 | case MID_REQUEST_SUBMITTED: |
| 4176 | case MID_RETRY_NEEDED: |
| 4177 | wdata->result = -EAGAIN; |
| 4178 | break; |
Pavel Shilovsky | 0fd1d37 | 2019-01-15 15:08:48 -0800 | [diff] [blame] | 4179 | case MID_RESPONSE_MALFORMED: |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4180 | credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest); |
| 4181 | credits.instance = server->reconnect_instance; |
Miaohe Lin | 30b5ae2 | 2020-08-08 16:36:37 +0800 | [diff] [blame] | 4182 | fallthrough; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4183 | default: |
| 4184 | wdata->result = -EIO; |
| 4185 | break; |
| 4186 | } |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4187 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4188 | /* |
| 4189 | * If this wdata has a memory registered, the MR can be freed |
| 4190 | * The number of MRs available is limited, it's important to recover |
| 4191 | * used MR as soon as I/O is finished. Hold MR longer in the later |
| 4192 | * I/O process can possibly result in I/O deadlock due to lack of MR |
| 4193 | * to send request on I/O retry |
| 4194 | */ |
| 4195 | if (wdata->mr) { |
| 4196 | smbd_deregister_mr(wdata->mr); |
| 4197 | wdata->mr = NULL; |
| 4198 | } |
| 4199 | #endif |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4200 | if (wdata->result) { |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4201 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4202 | trace_smb3_write_err(0 /* no xid */, |
| 4203 | wdata->cfile->fid.persistent_fid, |
| 4204 | tcon->tid, tcon->ses->Suid, wdata->offset, |
| 4205 | wdata->bytes, wdata->result); |
Steve French | d6fd419 | 2020-02-05 16:52:11 -0600 | [diff] [blame] | 4206 | if (wdata->result == -ENOSPC) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 4207 | pr_warn_once("Out of space writing to %s\n", |
| 4208 | tcon->treeName); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4209 | } else |
| 4210 | trace_smb3_write_done(0 /* no xid */, |
| 4211 | wdata->cfile->fid.persistent_fid, |
| 4212 | tcon->tid, tcon->ses->Suid, |
| 4213 | wdata->offset, wdata->bytes); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4214 | |
| 4215 | queue_work(cifsiod_wq, &wdata->work); |
| 4216 | DeleteMidQEntry(mid); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame] | 4217 | add_credits(server, &credits, 0); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | /* smb2_async_writev - send an async write, and set up mid to handle result */ |
| 4221 | int |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 4222 | smb2_async_writev(struct cifs_writedata *wdata, |
| 4223 | void (*release)(struct kref *kref)) |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4224 | { |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4225 | int rc = -EACCES, flags = 0; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4226 | struct smb2_write_req *req = NULL; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4227 | struct smb2_sync_hdr *shdr; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4228 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4229 | struct TCP_Server_Info *server = wdata->server; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4230 | struct kvec iov[1]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4231 | struct smb_rqst rqst = { }; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4232 | unsigned int total_len; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4233 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4234 | if (!wdata->server) |
| 4235 | server = wdata->server = cifs_pick_channel(tcon->ses); |
| 4236 | |
| 4237 | rc = smb2_plain_req_init(SMB2_WRITE, tcon, server, |
| 4238 | (void **) &req, &total_len); |
Pavel Shilovsky | f0b93cb | 2019-01-25 11:10:00 -0800 | [diff] [blame] | 4239 | if (rc) |
| 4240 | return rc; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4241 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4242 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4243 | flags |= CIFS_TRANSFORM_REQ; |
| 4244 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4245 | shdr = (struct smb2_sync_hdr *)req; |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4246 | shdr->ProcessId = cpu_to_le32(wdata->cfile->pid); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4247 | |
| 4248 | req->PersistentFileId = wdata->cfile->fid.persistent_fid; |
| 4249 | req->VolatileFileId = wdata->cfile->fid.volatile_fid; |
| 4250 | req->WriteChannelInfoOffset = 0; |
| 4251 | req->WriteChannelInfoLength = 0; |
| 4252 | req->Channel = 0; |
| 4253 | req->Offset = cpu_to_le64(wdata->offset); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4254 | req->DataOffset = cpu_to_le16( |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4255 | offsetof(struct smb2_write_req, Buffer)); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4256 | req->RemainingBytes = 0; |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4257 | |
| 4258 | trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid, |
| 4259 | tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4260 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4261 | /* |
| 4262 | * If we want to do a server RDMA read, fill in and append |
| 4263 | * smbd_buffer_descriptor_v1 to the end of write request |
| 4264 | */ |
Long Li | bb4c041 | 2018-04-17 12:17:08 -0700 | [diff] [blame] | 4265 | if (server->rdma && !server->sign && wdata->bytes >= |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4266 | server->smbd_conn->rdma_readwrite_threshold) { |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4267 | |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4268 | struct smbd_buffer_descriptor_v1 *v1; |
| 4269 | bool need_invalidate = server->dialect == SMB30_PROT_ID; |
| 4270 | |
| 4271 | wdata->mr = smbd_register_mr( |
| 4272 | server->smbd_conn, wdata->pages, |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 4273 | wdata->nr_pages, wdata->page_offset, |
| 4274 | wdata->tailsz, false, need_invalidate); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4275 | if (!wdata->mr) { |
Long Li | b797209 | 2019-04-05 21:36:34 +0000 | [diff] [blame] | 4276 | rc = -EAGAIN; |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4277 | goto async_writev_out; |
| 4278 | } |
| 4279 | req->Length = 0; |
| 4280 | req->DataOffset = 0; |
Long Li | 7cf20bc | 2018-05-30 12:48:02 -0700 | [diff] [blame] | 4281 | if (wdata->nr_pages > 1) |
| 4282 | req->RemainingBytes = |
| 4283 | cpu_to_le32( |
| 4284 | (wdata->nr_pages - 1) * wdata->pagesz - |
| 4285 | wdata->page_offset + wdata->tailsz |
| 4286 | ); |
| 4287 | else |
| 4288 | req->RemainingBytes = cpu_to_le32(wdata->tailsz); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4289 | req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; |
| 4290 | if (need_invalidate) |
| 4291 | req->Channel = SMB2_CHANNEL_RDMA_V1; |
| 4292 | req->WriteChannelInfoOffset = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4293 | cpu_to_le16(offsetof(struct smb2_write_req, Buffer)); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4294 | req->WriteChannelInfoLength = |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4295 | cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1)); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4296 | v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0]; |
Steve French | 2026b06 | 2018-01-24 23:07:41 -0600 | [diff] [blame] | 4297 | v1->offset = cpu_to_le64(wdata->mr->mr->iova); |
| 4298 | v1->token = cpu_to_le32(wdata->mr->mr->rkey); |
| 4299 | v1->length = cpu_to_le32(wdata->mr->mr->length); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4300 | } |
| 4301 | #endif |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4302 | iov[0].iov_len = total_len - 1; |
| 4303 | iov[0].iov_base = (char *)req; |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4304 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 4305 | rqst.rq_iov = iov; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4306 | rqst.rq_nvec = 1; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 4307 | rqst.rq_pages = wdata->pages; |
Long Li | 57a929a | 2018-05-30 12:47:53 -0700 | [diff] [blame] | 4308 | rqst.rq_offset = wdata->page_offset; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 4309 | rqst.rq_npages = wdata->nr_pages; |
| 4310 | rqst.rq_pagesz = wdata->pagesz; |
| 4311 | rqst.rq_tailsz = wdata->tailsz; |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4312 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4313 | if (wdata->mr) { |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 4314 | iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4315 | rqst.rq_npages = 0; |
| 4316 | } |
| 4317 | #endif |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4318 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
| 4319 | wdata->offset, wdata->bytes); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4320 | |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4321 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 4322 | /* For RDMA read, I/O size is in RemainingBytes not in Length */ |
| 4323 | if (!wdata->mr) |
| 4324 | req->Length = cpu_to_le32(wdata->bytes); |
| 4325 | #else |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4326 | req->Length = cpu_to_le32(wdata->bytes); |
Long Li | db223a5 | 2017-11-22 17:38:45 -0700 | [diff] [blame] | 4327 | #endif |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4328 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4329 | if (wdata->credits.value > 0) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4330 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes, |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4331 | SMB2_MAX_BUFFER_SIZE)); |
Pavel Shilovsky | b983f7e | 2018-12-19 22:49:09 +0000 | [diff] [blame] | 4332 | shdr->CreditRequest = |
| 4333 | cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4334 | |
| 4335 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
| 4336 | if (rc) |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 4337 | goto async_writev_out; |
Pavel Shilovsky | 9a1c67e | 2019-01-23 18:15:52 -0800 | [diff] [blame] | 4338 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4339 | flags |= CIFS_HAS_CREDITS; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 4340 | } |
| 4341 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4342 | kref_get(&wdata->refcount); |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 4343 | rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL, |
Pavel Shilovsky | 3349c3a | 2019-01-15 15:52:29 -0800 | [diff] [blame] | 4344 | wdata, flags, &wdata->credits); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4345 | |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4346 | if (rc) { |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4347 | trace_smb3_write_err(0 /* no xid */, req->PersistentFileId, |
| 4348 | tcon->tid, tcon->ses->Suid, wdata->offset, |
| 4349 | wdata->bytes, rc); |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 4350 | kref_put(&wdata->refcount, release); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4351 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
Pavel Shilovsky | 7d42e72 | 2019-01-25 11:38:53 -0800 | [diff] [blame] | 4352 | } |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4353 | |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4354 | async_writev_out: |
| 4355 | cifs_small_buf_release(req); |
Pavel Shilovsky | 3331914 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 4356 | return rc; |
| 4357 | } |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4358 | |
| 4359 | /* |
| 4360 | * SMB2_write function gets iov pointer to kvec array with n_vec as a length. |
| 4361 | * The length field from io_parms must be at least 1 and indicates a number of |
| 4362 | * elements with data to write that begins with position 1 in iov array. All |
| 4363 | * data length is specified by count. |
| 4364 | */ |
| 4365 | int |
| 4366 | SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, |
| 4367 | unsigned int *nbytes, struct kvec *iov, int n_vec) |
| 4368 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4369 | struct smb_rqst rqst; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4370 | int rc = 0; |
| 4371 | struct smb2_write_req *req = NULL; |
| 4372 | struct smb2_write_rsp *rsp = NULL; |
| 4373 | int resp_buftype; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4374 | struct kvec rsp_iov; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4375 | int flags = 0; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4376 | unsigned int total_len; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4377 | struct TCP_Server_Info *server; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4378 | |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4379 | *nbytes = 0; |
| 4380 | |
| 4381 | if (n_vec < 1) |
| 4382 | return rc; |
| 4383 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4384 | if (!io_parms->server) |
| 4385 | io_parms->server = cifs_pick_channel(io_parms->tcon->ses); |
| 4386 | server = io_parms->server; |
| 4387 | if (server == NULL) |
| 4388 | return -ECONNABORTED; |
| 4389 | |
| 4390 | rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server, |
| 4391 | (void **) &req, &total_len); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4392 | if (rc) |
| 4393 | return rc; |
| 4394 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4395 | if (smb3_encryption_required(io_parms->tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4396 | flags |= CIFS_TRANSFORM_REQ; |
| 4397 | |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4398 | req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4399 | |
| 4400 | req->PersistentFileId = io_parms->persistent_fid; |
| 4401 | req->VolatileFileId = io_parms->volatile_fid; |
| 4402 | req->WriteChannelInfoOffset = 0; |
| 4403 | req->WriteChannelInfoLength = 0; |
| 4404 | req->Channel = 0; |
| 4405 | req->Length = cpu_to_le32(io_parms->length); |
| 4406 | req->Offset = cpu_to_le64(io_parms->offset); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4407 | req->DataOffset = cpu_to_le16( |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4408 | offsetof(struct smb2_write_req, Buffer)); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4409 | req->RemainingBytes = 0; |
| 4410 | |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4411 | trace_smb3_write_enter(xid, io_parms->persistent_fid, |
| 4412 | io_parms->tcon->tid, io_parms->tcon->ses->Suid, |
| 4413 | io_parms->offset, io_parms->length); |
| 4414 | |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4415 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4416 | /* 1 for Buffer */ |
| 4417 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4418 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4419 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4420 | rqst.rq_iov = iov; |
| 4421 | rqst.rq_nvec = n_vec + 1; |
| 4422 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4423 | rc = cifs_send_recv(xid, io_parms->tcon->ses, server, |
| 4424 | &rqst, |
Ronnie Sahlberg | f5688a6 | 2017-11-20 11:24:41 +1100 | [diff] [blame] | 4425 | &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4426 | rsp = (struct smb2_write_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4427 | |
| 4428 | if (rc) { |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4429 | trace_smb3_write_err(xid, req->PersistentFileId, |
| 4430 | io_parms->tcon->tid, |
| 4431 | io_parms->tcon->ses->Suid, |
| 4432 | io_parms->offset, io_parms->length, rc); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4433 | cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4434 | cifs_dbg(VFS, "Send error in write = %d\n", rc); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4435 | } else { |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4436 | *nbytes = le32_to_cpu(rsp->DataLength); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4437 | trace_smb3_write_done(xid, req->PersistentFileId, |
| 4438 | io_parms->tcon->tid, |
| 4439 | io_parms->tcon->ses->Suid, |
| 4440 | io_parms->offset, *nbytes); |
| 4441 | } |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4442 | |
ZhangXiaoxu | 6a3eb33 | 2019-04-06 15:47:38 +0800 | [diff] [blame] | 4443 | cifs_small_buf_release(req); |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4444 | free_rsp_buf(resp_buftype, rsp); |
Pavel Shilovsky | 009d344 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 4445 | return rc; |
| 4446 | } |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4447 | |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 4448 | int posix_info_sid_size(const void *beg, const void *end) |
Aurelien Aptel | 349e13a | 2020-02-08 15:50:57 +0100 | [diff] [blame] | 4449 | { |
| 4450 | size_t subauth; |
| 4451 | int total; |
| 4452 | |
| 4453 | if (beg + 1 > end) |
| 4454 | return -1; |
| 4455 | |
| 4456 | subauth = *(u8 *)(beg+1); |
| 4457 | if (subauth < 1 || subauth > 15) |
| 4458 | return -1; |
| 4459 | |
| 4460 | total = 1 + 1 + 6 + 4*subauth; |
| 4461 | if (beg + total > end) |
| 4462 | return -1; |
| 4463 | |
| 4464 | return total; |
| 4465 | } |
| 4466 | |
| 4467 | int posix_info_parse(const void *beg, const void *end, |
| 4468 | struct smb2_posix_info_parsed *out) |
| 4469 | |
| 4470 | { |
| 4471 | int total_len = 0; |
| 4472 | int sid_len; |
| 4473 | int name_len; |
| 4474 | const void *owner_sid; |
| 4475 | const void *group_sid; |
| 4476 | const void *name; |
| 4477 | |
| 4478 | /* if no end bound given, assume payload to be correct */ |
| 4479 | if (!end) { |
| 4480 | const struct smb2_posix_info *p = beg; |
| 4481 | |
| 4482 | end = beg + le32_to_cpu(p->NextEntryOffset); |
| 4483 | /* last element will have a 0 offset, pick a sensible bound */ |
| 4484 | if (end == beg) |
| 4485 | end += 0xFFFF; |
| 4486 | } |
| 4487 | |
| 4488 | /* check base buf */ |
| 4489 | if (beg + sizeof(struct smb2_posix_info) > end) |
| 4490 | return -1; |
| 4491 | total_len = sizeof(struct smb2_posix_info); |
| 4492 | |
| 4493 | /* check owner sid */ |
| 4494 | owner_sid = beg + total_len; |
| 4495 | sid_len = posix_info_sid_size(owner_sid, end); |
| 4496 | if (sid_len < 0) |
| 4497 | return -1; |
| 4498 | total_len += sid_len; |
| 4499 | |
| 4500 | /* check group sid */ |
| 4501 | group_sid = beg + total_len; |
| 4502 | sid_len = posix_info_sid_size(group_sid, end); |
| 4503 | if (sid_len < 0) |
| 4504 | return -1; |
| 4505 | total_len += sid_len; |
| 4506 | |
| 4507 | /* check name len */ |
| 4508 | if (beg + total_len + 4 > end) |
| 4509 | return -1; |
| 4510 | name_len = le32_to_cpu(*(__le32 *)(beg + total_len)); |
| 4511 | if (name_len < 1 || name_len > 0xFFFF) |
| 4512 | return -1; |
| 4513 | total_len += 4; |
| 4514 | |
| 4515 | /* check name */ |
| 4516 | name = beg + total_len; |
| 4517 | if (name + name_len > end) |
| 4518 | return -1; |
| 4519 | total_len += name_len; |
| 4520 | |
| 4521 | if (out) { |
| 4522 | out->base = beg; |
| 4523 | out->size = total_len; |
| 4524 | out->name_len = name_len; |
| 4525 | out->name = name; |
| 4526 | memcpy(&out->owner, owner_sid, |
| 4527 | posix_info_sid_size(owner_sid, end)); |
| 4528 | memcpy(&out->group, group_sid, |
| 4529 | posix_info_sid_size(group_sid, end)); |
| 4530 | } |
| 4531 | return total_len; |
| 4532 | } |
| 4533 | |
| 4534 | static int posix_info_extra_size(const void *beg, const void *end) |
| 4535 | { |
| 4536 | int len = posix_info_parse(beg, end, NULL); |
| 4537 | |
| 4538 | if (len < 0) |
| 4539 | return -1; |
| 4540 | return len - sizeof(struct smb2_posix_info); |
| 4541 | } |
| 4542 | |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4543 | static unsigned int |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4544 | num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry, |
| 4545 | size_t size) |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4546 | { |
| 4547 | int len; |
| 4548 | unsigned int entrycount = 0; |
| 4549 | unsigned int next_offset = 0; |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4550 | char *entryptr; |
| 4551 | FILE_DIRECTORY_INFO *dir_info; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4552 | |
| 4553 | if (bufstart == NULL) |
| 4554 | return 0; |
| 4555 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4556 | entryptr = bufstart; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4557 | |
| 4558 | while (1) { |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4559 | if (entryptr + next_offset < entryptr || |
| 4560 | entryptr + next_offset > end_of_buf || |
| 4561 | entryptr + next_offset + size > end_of_buf) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4562 | cifs_dbg(VFS, "malformed search entry would overflow\n"); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4563 | break; |
| 4564 | } |
| 4565 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4566 | entryptr = entryptr + next_offset; |
| 4567 | dir_info = (FILE_DIRECTORY_INFO *)entryptr; |
| 4568 | |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4569 | if (infotype == SMB_FIND_FILE_POSIX_INFO) |
| 4570 | len = posix_info_extra_size(entryptr, end_of_buf); |
| 4571 | else |
| 4572 | len = le32_to_cpu(dir_info->FileNameLength); |
| 4573 | |
| 4574 | if (len < 0 || |
| 4575 | entryptr + len < entryptr || |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4576 | entryptr + len > end_of_buf || |
| 4577 | entryptr + len + size > end_of_buf) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4578 | cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", |
| 4579 | end_of_buf); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4580 | break; |
| 4581 | } |
| 4582 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4583 | *lastentry = entryptr; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4584 | entrycount++; |
| 4585 | |
Dan Carpenter | 56446f2 | 2018-09-06 12:48:22 +0300 | [diff] [blame] | 4586 | next_offset = le32_to_cpu(dir_info->NextEntryOffset); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4587 | if (!next_offset) |
| 4588 | break; |
| 4589 | } |
| 4590 | |
| 4591 | return entrycount; |
| 4592 | } |
| 4593 | |
| 4594 | /* |
| 4595 | * Readdir/FindFirst |
| 4596 | */ |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4597 | int SMB2_query_directory_init(const unsigned int xid, |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4598 | struct cifs_tcon *tcon, |
| 4599 | struct TCP_Server_Info *server, |
| 4600 | struct smb_rqst *rqst, |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4601 | u64 persistent_fid, u64 volatile_fid, |
| 4602 | int index, int info_level) |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4603 | { |
| 4604 | struct smb2_query_directory_req *req; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4605 | unsigned char *bufptr; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4606 | __le16 asteriks = cpu_to_le16('*'); |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4607 | unsigned int output_size = CIFSMaxBufSize - |
| 4608 | MAX_SMB2_CREATE_RESPONSE_SIZE - |
| 4609 | MAX_SMB2_CLOSE_RESPONSE_SIZE; |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4610 | unsigned int total_len; |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4611 | struct kvec *iov = rqst->rq_iov; |
| 4612 | int len, rc; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4613 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4614 | rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server, |
| 4615 | (void **) &req, &total_len); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4616 | if (rc) |
| 4617 | return rc; |
| 4618 | |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4619 | switch (info_level) { |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4620 | case SMB_FIND_FILE_DIRECTORY_INFO: |
| 4621 | req->FileInformationClass = FILE_DIRECTORY_INFORMATION; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4622 | break; |
| 4623 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: |
| 4624 | req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4625 | break; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4626 | case SMB_FIND_FILE_POSIX_INFO: |
| 4627 | req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO; |
| 4628 | break; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4629 | default: |
Ronnie Sahlberg | 3175eb9 | 2019-09-04 12:32:41 +1000 | [diff] [blame] | 4630 | cifs_tcon_dbg(VFS, "info level %u isn't supported\n", |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4631 | info_level); |
| 4632 | return -EINVAL; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | req->FileIndex = cpu_to_le32(index); |
| 4636 | req->PersistentFileId = persistent_fid; |
| 4637 | req->VolatileFileId = volatile_fid; |
| 4638 | |
| 4639 | len = 0x2; |
| 4640 | bufptr = req->Buffer; |
| 4641 | memcpy(bufptr, &asteriks, len); |
| 4642 | |
| 4643 | req->FileNameOffset = |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4644 | cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4645 | req->FileNameLength = cpu_to_le16(len); |
| 4646 | /* |
| 4647 | * BB could be 30 bytes or so longer if we used SMB2 specific |
| 4648 | * buffer lengths, but this is safe and close enough. |
| 4649 | */ |
| 4650 | output_size = min_t(unsigned int, output_size, server->maxBuf); |
| 4651 | output_size = min_t(unsigned int, output_size, 2 << 15); |
| 4652 | req->OutputBufferLength = cpu_to_le32(output_size); |
| 4653 | |
| 4654 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 7c00c3a | 2017-11-20 11:24:45 +1100 | [diff] [blame] | 4655 | /* 1 for Buffer */ |
| 4656 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4657 | |
| 4658 | iov[1].iov_base = (char *)(req->Buffer); |
| 4659 | iov[1].iov_len = len; |
| 4660 | |
Steve French | d323c246 | 2019-02-25 00:52:43 -0600 | [diff] [blame] | 4661 | trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid, |
| 4662 | tcon->ses->Suid, index, output_size); |
| 4663 | |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4664 | return 0; |
| 4665 | } |
| 4666 | |
| 4667 | void SMB2_query_directory_free(struct smb_rqst *rqst) |
| 4668 | { |
| 4669 | if (rqst && rqst->rq_iov) { |
| 4670 | cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | int |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4675 | smb2_parse_query_directory(struct cifs_tcon *tcon, |
| 4676 | struct kvec *rsp_iov, |
| 4677 | int resp_buftype, |
| 4678 | struct cifs_search_info *srch_inf) |
| 4679 | { |
| 4680 | struct smb2_query_directory_rsp *rsp; |
| 4681 | size_t info_buf_size; |
| 4682 | char *end_of_smb; |
| 4683 | int rc; |
| 4684 | |
| 4685 | rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base; |
| 4686 | |
| 4687 | switch (srch_inf->info_level) { |
| 4688 | case SMB_FIND_FILE_DIRECTORY_INFO: |
| 4689 | info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1; |
| 4690 | break; |
| 4691 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: |
| 4692 | info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1; |
| 4693 | break; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4694 | case SMB_FIND_FILE_POSIX_INFO: |
| 4695 | /* note that posix payload are variable size */ |
| 4696 | info_buf_size = sizeof(struct smb2_posix_info); |
| 4697 | break; |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4698 | default: |
| 4699 | cifs_tcon_dbg(VFS, "info level %u isn't supported\n", |
| 4700 | srch_inf->info_level); |
| 4701 | return -EINVAL; |
| 4702 | } |
| 4703 | |
| 4704 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 4705 | le32_to_cpu(rsp->OutputBufferLength), rsp_iov, |
| 4706 | info_buf_size); |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4707 | if (rc) { |
| 4708 | cifs_tcon_dbg(VFS, "bad info payload"); |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4709 | return rc; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4710 | } |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4711 | |
| 4712 | srch_inf->unicode = true; |
| 4713 | |
| 4714 | if (srch_inf->ntwrk_buf_start) { |
| 4715 | if (srch_inf->smallBuf) |
| 4716 | cifs_small_buf_release(srch_inf->ntwrk_buf_start); |
| 4717 | else |
| 4718 | cifs_buf_release(srch_inf->ntwrk_buf_start); |
| 4719 | } |
| 4720 | srch_inf->ntwrk_buf_start = (char *)rsp; |
| 4721 | srch_inf->srch_entries_start = srch_inf->last_entry = |
| 4722 | (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset); |
| 4723 | end_of_smb = rsp_iov->iov_len + (char *)rsp; |
Aurelien Aptel | 3d519bd | 2020-02-08 15:50:58 +0100 | [diff] [blame] | 4724 | |
| 4725 | srch_inf->entries_in_buffer = num_entries( |
| 4726 | srch_inf->info_level, |
| 4727 | srch_inf->srch_entries_start, |
| 4728 | end_of_smb, |
| 4729 | &srch_inf->last_entry, |
| 4730 | info_buf_size); |
| 4731 | |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4732 | srch_inf->index_of_last_entry += srch_inf->entries_in_buffer; |
| 4733 | cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n", |
| 4734 | srch_inf->entries_in_buffer, srch_inf->index_of_last_entry, |
| 4735 | srch_inf->srch_entries_start, srch_inf->last_entry); |
| 4736 | if (resp_buftype == CIFS_LARGE_BUFFER) |
| 4737 | srch_inf->smallBuf = false; |
| 4738 | else if (resp_buftype == CIFS_SMALL_BUFFER) |
| 4739 | srch_inf->smallBuf = true; |
| 4740 | else |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 4741 | cifs_tcon_dbg(VFS, "Invalid search buffer type\n"); |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4742 | |
| 4743 | return 0; |
| 4744 | } |
| 4745 | |
| 4746 | int |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4747 | SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, |
| 4748 | u64 persistent_fid, u64 volatile_fid, int index, |
| 4749 | struct cifs_search_info *srch_inf) |
| 4750 | { |
| 4751 | struct smb_rqst rqst; |
| 4752 | struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE]; |
| 4753 | struct smb2_query_directory_rsp *rsp = NULL; |
| 4754 | int resp_buftype = CIFS_NO_BUFFER; |
| 4755 | struct kvec rsp_iov; |
| 4756 | int rc = 0; |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4757 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4758 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4759 | int flags = 0; |
| 4760 | |
YueHaibing | c4985c3 | 2020-01-17 10:57:17 +0800 | [diff] [blame] | 4761 | if (!ses || !(ses->server)) |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4762 | return -EIO; |
| 4763 | |
| 4764 | if (smb3_encryption_required(tcon)) |
| 4765 | flags |= CIFS_TRANSFORM_REQ; |
| 4766 | |
| 4767 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4768 | memset(&iov, 0, sizeof(iov)); |
| 4769 | rqst.rq_iov = iov; |
| 4770 | rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE; |
| 4771 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4772 | rc = SMB2_query_directory_init(xid, tcon, server, |
| 4773 | &rqst, persistent_fid, |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4774 | volatile_fid, index, |
| 4775 | srch_inf->info_level); |
| 4776 | if (rc) |
| 4777 | goto qdir_exit; |
| 4778 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4779 | rc = cifs_send_recv(xid, ses, server, |
| 4780 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4781 | rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | e5d0488 | 2012-09-19 16:03:26 +0400 | [diff] [blame] | 4782 | |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4783 | if (rc) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 4784 | if (rc == -ENODATA && |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 4785 | rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) { |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4786 | trace_smb3_query_dir_done(xid, persistent_fid, |
| 4787 | tcon->tid, tcon->ses->Suid, index, 0); |
Pavel Shilovsky | 5275580 | 2014-08-18 20:49:57 +0400 | [diff] [blame] | 4788 | srch_inf->endOfSearch = true; |
| 4789 | rc = 0; |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4790 | } else { |
| 4791 | trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, |
| 4792 | tcon->ses->Suid, index, 0, rc); |
Pavel Shilovsky | 8e6e72a | 2019-01-26 12:21:32 -0800 | [diff] [blame] | 4793 | cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE); |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4794 | } |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4795 | goto qdir_exit; |
| 4796 | } |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4797 | |
Ronnie Sahlberg | af08f9e | 2020-01-08 13:08:05 +1000 | [diff] [blame] | 4798 | rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype, |
| 4799 | srch_inf); |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4800 | if (rc) { |
| 4801 | trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, |
| 4802 | tcon->ses->Suid, index, 0, rc); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4803 | goto qdir_exit; |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4804 | } |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4805 | resp_buftype = CIFS_NO_BUFFER; |
| 4806 | |
Steve French | adb3b4e | 2019-02-25 13:51:11 -0600 | [diff] [blame] | 4807 | trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid, |
| 4808 | tcon->ses->Suid, index, srch_inf->entries_in_buffer); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4809 | |
| 4810 | qdir_exit: |
Ronnie Sahlberg | 0a17799 | 2020-01-08 13:08:04 +1000 | [diff] [blame] | 4811 | SMB2_query_directory_free(&rqst); |
Pavel Shilovsky | d324f08d | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4812 | free_rsp_buf(resp_buftype, rsp); |
| 4813 | return rc; |
| 4814 | } |
| 4815 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4816 | int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4817 | SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, |
| 4818 | struct smb_rqst *rqst, |
| 4819 | u64 persistent_fid, u64 volatile_fid, u32 pid, |
| 4820 | u8 info_class, u8 info_type, u32 additional_info, |
| 4821 | void **data, unsigned int *size) |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4822 | { |
| 4823 | struct smb2_set_info_req *req; |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4824 | struct kvec *iov = rqst->rq_iov; |
| 4825 | unsigned int i, total_len; |
| 4826 | int rc; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4827 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4828 | rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server, |
| 4829 | (void **) &req, &total_len); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4830 | if (rc) |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4831 | return rc; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4832 | |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4833 | req->sync_hdr.ProcessId = cpu_to_le32(pid); |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4834 | req->InfoType = info_type; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4835 | req->FileInfoClass = info_class; |
| 4836 | req->PersistentFileId = persistent_fid; |
| 4837 | req->VolatileFileId = volatile_fid; |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4838 | req->AdditionalInformation = cpu_to_le32(additional_info); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4839 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4840 | req->BufferOffset = |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4841 | cpu_to_le16(sizeof(struct smb2_set_info_req) - 1); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4842 | req->BufferLength = cpu_to_le32(*size); |
| 4843 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4844 | memcpy(req->Buffer, *data, *size); |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4845 | total_len += *size; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4846 | |
| 4847 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4848 | /* 1 for Buffer */ |
| 4849 | iov[0].iov_len = total_len - 1; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4850 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4851 | for (i = 1; i < rqst->rq_nvec; i++) { |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4852 | le32_add_cpu(&req->BufferLength, size[i]); |
| 4853 | iov[i].iov_base = (char *)data[i]; |
| 4854 | iov[i].iov_len = size[i]; |
| 4855 | } |
| 4856 | |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4857 | return 0; |
| 4858 | } |
| 4859 | |
| 4860 | void |
| 4861 | SMB2_set_info_free(struct smb_rqst *rqst) |
| 4862 | { |
Ronnie Sahlberg | 32a1fb3 | 2018-10-24 11:50:33 +1000 | [diff] [blame] | 4863 | if (rqst && rqst->rq_iov) |
| 4864 | cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */ |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | static int |
| 4868 | send_set_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 4869 | u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class, |
| 4870 | u8 info_type, u32 additional_info, unsigned int num, |
| 4871 | void **data, unsigned int *size) |
| 4872 | { |
| 4873 | struct smb_rqst rqst; |
| 4874 | struct smb2_set_info_rsp *rsp = NULL; |
| 4875 | struct kvec *iov; |
| 4876 | struct kvec rsp_iov; |
| 4877 | int rc = 0; |
| 4878 | int resp_buftype; |
| 4879 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4880 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4881 | int flags = 0; |
| 4882 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4883 | if (!ses || !server) |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4884 | return -EIO; |
| 4885 | |
| 4886 | if (!num) |
| 4887 | return -EINVAL; |
| 4888 | |
| 4889 | if (smb3_encryption_required(tcon)) |
| 4890 | flags |= CIFS_TRANSFORM_REQ; |
| 4891 | |
| 4892 | iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL); |
| 4893 | if (!iov) |
| 4894 | return -ENOMEM; |
| 4895 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4896 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 4897 | rqst.rq_iov = iov; |
| 4898 | rqst.rq_nvec = num; |
| 4899 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4900 | rc = SMB2_set_info_init(tcon, server, |
| 4901 | &rqst, persistent_fid, volatile_fid, pid, |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4902 | info_class, info_type, additional_info, |
| 4903 | data, size); |
| 4904 | if (rc) { |
| 4905 | kfree(iov); |
| 4906 | return rc; |
| 4907 | } |
| 4908 | |
| 4909 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4910 | rc = cifs_send_recv(xid, ses, server, |
| 4911 | &rqst, &resp_buftype, flags, |
Ronnie Sahlberg | 2fc803e | 2017-11-20 11:24:44 +1100 | [diff] [blame] | 4912 | &rsp_iov); |
Ronnie Sahlberg | ba8ca11 | 2018-09-03 13:33:44 +1000 | [diff] [blame] | 4913 | SMB2_set_info_free(&rqst); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 4914 | rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4915 | |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4916 | if (rc != 0) { |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4917 | cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 4918 | trace_smb3_set_info_err(xid, persistent_fid, tcon->tid, |
| 4919 | ses->Suid, info_class, (__u32)info_type, rc); |
| 4920 | } |
Steve French | 7d3fb24 | 2013-11-18 09:56:28 -0600 | [diff] [blame] | 4921 | |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 4922 | free_rsp_buf(resp_buftype, rsp); |
| 4923 | kfree(iov); |
| 4924 | return rc; |
| 4925 | } |
| 4926 | |
| 4927 | int |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4928 | 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] | 4929 | u64 volatile_fid, u32 pid, __le64 *eof) |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4930 | { |
| 4931 | struct smb2_file_eof_info info; |
| 4932 | void *data; |
| 4933 | unsigned int size; |
| 4934 | |
| 4935 | info.EndOfFile = *eof; |
| 4936 | |
| 4937 | data = &info; |
| 4938 | size = sizeof(struct smb2_file_eof_info); |
| 4939 | |
Ronnie Sahlberg | 3764cbd | 2018-09-03 13:33:47 +1000 | [diff] [blame] | 4940 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4941 | pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE, |
| 4942 | 0, 1, &data, &size); |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4943 | } |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4944 | |
| 4945 | int |
Shirish Pargaonkar | dac9534 | 2017-06-28 22:37:00 -0500 | [diff] [blame] | 4946 | SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon, |
| 4947 | u64 persistent_fid, u64 volatile_fid, |
| 4948 | struct cifs_ntsd *pnntsd, int pacllen, int aclflag) |
| 4949 | { |
| 4950 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
| 4951 | current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag, |
| 4952 | 1, (void **)&pnntsd, &pacllen); |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 4953 | } |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4954 | |
| 4955 | int |
Ronnie Sahlberg | 5517554 | 2017-08-24 11:24:56 +1000 | [diff] [blame] | 4956 | SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, |
| 4957 | u64 persistent_fid, u64 volatile_fid, |
| 4958 | struct smb2_file_full_ea_info *buf, int len) |
| 4959 | { |
| 4960 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, |
| 4961 | current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, |
| 4962 | 0, 1, (void **)&buf, &len); |
| 4963 | } |
| 4964 | |
| 4965 | int |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4966 | SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, |
| 4967 | const u64 persistent_fid, const u64 volatile_fid, |
| 4968 | __u8 oplock_level) |
| 4969 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 4970 | struct smb_rqst rqst; |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4971 | int rc; |
Ronnie Sahlberg | 0d5a288 | 2018-06-01 10:53:03 +1000 | [diff] [blame] | 4972 | struct smb2_oplock_break *req = NULL; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4973 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4974 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4975 | int flags = CIFS_OBREAK_OP; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4976 | unsigned int total_len; |
| 4977 | struct kvec iov[1]; |
| 4978 | struct kvec rsp_iov; |
| 4979 | int resp_buf_type; |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4980 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4981 | cifs_dbg(FYI, "SMB2_oplock_break\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 4982 | rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, |
| 4983 | (void **) &req, &total_len); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4984 | if (rc) |
| 4985 | return rc; |
| 4986 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 4987 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 4988 | flags |= CIFS_TRANSFORM_REQ; |
| 4989 | |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4990 | req->VolatileFid = volatile_fid; |
| 4991 | req->PersistentFid = persistent_fid; |
| 4992 | req->OplockLevel = oplock_level; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4993 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4994 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 4995 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 21ad948 | 2017-11-20 11:24:43 +1100 | [diff] [blame] | 4996 | |
| 4997 | iov[0].iov_base = (char *)req; |
| 4998 | iov[0].iov_len = total_len; |
| 4999 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5000 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5001 | rqst.rq_iov = iov; |
| 5002 | rqst.rq_nvec = 1; |
| 5003 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5004 | rc = cifs_send_recv(xid, ses, server, |
| 5005 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5006 | cifs_small_buf_release(req); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5007 | |
| 5008 | if (rc) { |
| 5009 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5010 | cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc); |
Pavel Shilovsky | 983c88a | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | return rc; |
| 5014 | } |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5015 | |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5016 | void |
| 5017 | smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, |
| 5018 | struct kstatfs *kst) |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5019 | { |
| 5020 | kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) * |
| 5021 | le32_to_cpu(pfs_inf->SectorsPerAllocationUnit); |
| 5022 | kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits); |
Sachin Prabhu | 42bec21 | 2017-08-03 13:09:03 +0530 | [diff] [blame] | 5023 | kst->f_bfree = kst->f_bavail = |
| 5024 | le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5025 | return; |
| 5026 | } |
| 5027 | |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5028 | static void |
| 5029 | copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data, |
| 5030 | struct kstatfs *kst) |
| 5031 | { |
| 5032 | kst->f_bsize = le32_to_cpu(response_data->BlockSize); |
| 5033 | kst->f_blocks = le64_to_cpu(response_data->TotalBlocks); |
| 5034 | kst->f_bfree = le64_to_cpu(response_data->BlocksAvail); |
| 5035 | if (response_data->UserBlocksAvail == cpu_to_le64(-1)) |
| 5036 | kst->f_bavail = kst->f_bfree; |
| 5037 | else |
| 5038 | kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail); |
| 5039 | if (response_data->TotalFileNodes != cpu_to_le64(-1)) |
| 5040 | kst->f_files = le64_to_cpu(response_data->TotalFileNodes); |
| 5041 | if (response_data->FreeFileNodes != cpu_to_le64(-1)) |
| 5042 | kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes); |
| 5043 | |
| 5044 | return; |
| 5045 | } |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5046 | |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5047 | static int |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5048 | build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, |
| 5049 | struct TCP_Server_Info *server, |
| 5050 | int level, int outbuf_len, u64 persistent_fid, |
| 5051 | u64 volatile_fid) |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5052 | { |
| 5053 | int rc; |
| 5054 | struct smb2_query_info_req *req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5055 | unsigned int total_len; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5056 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5057 | cifs_dbg(FYI, "Query FSInfo level %d\n", level); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5058 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5059 | if ((tcon->ses == NULL) || server == NULL) |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5060 | return -EIO; |
| 5061 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5062 | rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server, |
| 5063 | (void **) &req, &total_len); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5064 | if (rc) |
| 5065 | return rc; |
| 5066 | |
| 5067 | req->InfoType = SMB2_O_INFO_FILESYSTEM; |
| 5068 | req->FileInfoClass = level; |
| 5069 | req->PersistentFileId = persistent_fid; |
| 5070 | req->VolatileFileId = volatile_fid; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5071 | /* 1 for pad */ |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5072 | req->InputBufferOffset = |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5073 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5074 | req->OutputBufferLength = cpu_to_le32( |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5075 | outbuf_len + sizeof(struct smb2_query_info_rsp) - 1); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5076 | |
| 5077 | iov->iov_base = (char *)req; |
Ronnie Sahlberg | b2fb7fe | 2017-11-20 11:24:46 +1100 | [diff] [blame] | 5078 | iov->iov_len = total_len; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5079 | return 0; |
| 5080 | } |
| 5081 | |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5082 | int |
| 5083 | SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 5084 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) |
| 5085 | { |
| 5086 | struct smb_rqst rqst; |
| 5087 | struct smb2_query_info_rsp *rsp = NULL; |
| 5088 | struct kvec iov; |
| 5089 | struct kvec rsp_iov; |
| 5090 | int rc = 0; |
| 5091 | int resp_buftype; |
| 5092 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5093 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5094 | FILE_SYSTEM_POSIX_INFO *info = NULL; |
| 5095 | int flags = 0; |
| 5096 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5097 | rc = build_qfs_info_req(&iov, tcon, server, |
| 5098 | FS_POSIX_INFORMATION, |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5099 | sizeof(FILE_SYSTEM_POSIX_INFO), |
| 5100 | persistent_fid, volatile_fid); |
| 5101 | if (rc) |
| 5102 | return rc; |
| 5103 | |
| 5104 | if (smb3_encryption_required(tcon)) |
| 5105 | flags |= CIFS_TRANSFORM_REQ; |
| 5106 | |
| 5107 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5108 | rqst.rq_iov = &iov; |
| 5109 | rqst.rq_nvec = 1; |
| 5110 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5111 | rc = cifs_send_recv(xid, ses, server, |
| 5112 | &rqst, &resp_buftype, flags, &rsp_iov); |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5113 | cifs_small_buf_release(iov.iov_base); |
| 5114 | if (rc) { |
| 5115 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
| 5116 | goto posix_qfsinf_exit; |
| 5117 | } |
| 5118 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
| 5119 | |
| 5120 | info = (FILE_SYSTEM_POSIX_INFO *)( |
| 5121 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5122 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 5123 | le32_to_cpu(rsp->OutputBufferLength), &rsp_iov, |
| 5124 | sizeof(FILE_SYSTEM_POSIX_INFO)); |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5125 | if (!rc) |
| 5126 | copy_posix_fs_info_to_kstatfs(info, fsdata); |
| 5127 | |
| 5128 | posix_qfsinf_exit: |
| 5129 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
| 5130 | return rc; |
| 5131 | } |
Steve French | 2d30421 | 2018-06-24 23:28:12 -0500 | [diff] [blame] | 5132 | |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5133 | int |
| 5134 | SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 5135 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) |
| 5136 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5137 | struct smb_rqst rqst; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5138 | struct smb2_query_info_rsp *rsp = NULL; |
| 5139 | struct kvec iov; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5140 | struct kvec rsp_iov; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5141 | int rc = 0; |
| 5142 | int resp_buftype; |
| 5143 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5144 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5145 | struct smb2_fs_full_size_info *info = NULL; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5146 | int flags = 0; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5147 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5148 | rc = build_qfs_info_req(&iov, tcon, server, |
| 5149 | FS_FULL_SIZE_INFORMATION, |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5150 | sizeof(struct smb2_fs_full_size_info), |
| 5151 | persistent_fid, volatile_fid); |
| 5152 | if (rc) |
| 5153 | return rc; |
| 5154 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5155 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5156 | flags |= CIFS_TRANSFORM_REQ; |
| 5157 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5158 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5159 | rqst.rq_iov = &iov; |
| 5160 | rqst.rq_nvec = 1; |
| 5161 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5162 | rc = cifs_send_recv(xid, ses, server, |
| 5163 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5164 | cifs_small_buf_release(iov.iov_base); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5165 | if (rc) { |
| 5166 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5167 | goto qfsinf_exit; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5168 | } |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5169 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5170 | |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5171 | info = (struct smb2_fs_full_size_info *)( |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 5172 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5173 | rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), |
| 5174 | le32_to_cpu(rsp->OutputBufferLength), &rsp_iov, |
| 5175 | sizeof(struct smb2_fs_full_size_info)); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5176 | if (!rc) |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5177 | smb2_copy_fs_info_to_kstatfs(info, fsdata); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5178 | |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5179 | qfsinf_exit: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5180 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5181 | return rc; |
| 5182 | } |
| 5183 | |
| 5184 | int |
| 5185 | SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5186 | u64 persistent_fid, u64 volatile_fid, int level) |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5187 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5188 | struct smb_rqst rqst; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5189 | struct smb2_query_info_rsp *rsp = NULL; |
| 5190 | struct kvec iov; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5191 | struct kvec rsp_iov; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5192 | int rc = 0; |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5193 | int resp_buftype, max_len, min_len; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5194 | struct cifs_ses *ses = tcon->ses; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5195 | struct TCP_Server_Info *server = cifs_pick_channel(ses); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5196 | unsigned int rsp_len, offset; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5197 | int flags = 0; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5198 | |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5199 | if (level == FS_DEVICE_INFORMATION) { |
| 5200 | max_len = sizeof(FILE_SYSTEM_DEVICE_INFO); |
| 5201 | min_len = sizeof(FILE_SYSTEM_DEVICE_INFO); |
| 5202 | } else if (level == FS_ATTRIBUTE_INFORMATION) { |
| 5203 | max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO); |
| 5204 | min_len = MIN_FS_ATTR_INFO_SIZE; |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5205 | } else if (level == FS_SECTOR_SIZE_INFORMATION) { |
| 5206 | max_len = sizeof(struct smb3_fs_ss_info); |
| 5207 | min_len = sizeof(struct smb3_fs_ss_info); |
Steve French | 21ba384 | 2018-06-24 23:18:52 -0500 | [diff] [blame] | 5208 | } else if (level == FS_VOLUME_INFORMATION) { |
| 5209 | max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN; |
| 5210 | min_len = sizeof(struct smb3_fs_vol_info); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5211 | } else { |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5212 | cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5213 | return -EINVAL; |
| 5214 | } |
| 5215 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5216 | rc = build_qfs_info_req(&iov, tcon, server, |
| 5217 | level, max_len, |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5218 | persistent_fid, volatile_fid); |
| 5219 | if (rc) |
| 5220 | return rc; |
| 5221 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5222 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5223 | flags |= CIFS_TRANSFORM_REQ; |
| 5224 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5225 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5226 | rqst.rq_iov = &iov; |
| 5227 | rqst.rq_nvec = 1; |
| 5228 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5229 | rc = cifs_send_recv(xid, ses, server, |
| 5230 | &rqst, &resp_buftype, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5231 | cifs_small_buf_release(iov.iov_base); |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5232 | if (rc) { |
| 5233 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); |
| 5234 | goto qfsattr_exit; |
| 5235 | } |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5236 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5237 | |
| 5238 | rsp_len = le32_to_cpu(rsp->OutputBufferLength); |
| 5239 | offset = le16_to_cpu(rsp->OutputBufferOffset); |
Ronnie Sahlberg | 730928c | 2018-08-08 15:07:49 +1000 | [diff] [blame] | 5240 | rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len); |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5241 | if (rc) |
| 5242 | goto qfsattr_exit; |
| 5243 | |
| 5244 | if (level == FS_ATTRIBUTE_INFORMATION) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5245 | memcpy(&tcon->fsAttrInfo, offset |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 5246 | + (char *)rsp, min_t(unsigned int, |
Steven French | 2167114 | 2013-10-09 13:36:35 -0500 | [diff] [blame] | 5247 | rsp_len, max_len)); |
| 5248 | else if (level == FS_DEVICE_INFORMATION) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5249 | memcpy(&tcon->fsDevInfo, offset |
Ronnie Sahlberg | 49f466b | 2018-06-01 10:53:06 +1000 | [diff] [blame] | 5250 | + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO)); |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5251 | else if (level == FS_SECTOR_SIZE_INFORMATION) { |
| 5252 | struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *) |
Ronnie Sahlberg | 1fc6ad2 | 2018-06-01 10:53:07 +1000 | [diff] [blame] | 5253 | (offset + (char *)rsp); |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5254 | tcon->ss_flags = le32_to_cpu(ss_info->Flags); |
| 5255 | tcon->perf_sector_size = |
| 5256 | le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf); |
Steve French | 21ba384 | 2018-06-24 23:18:52 -0500 | [diff] [blame] | 5257 | } else if (level == FS_VOLUME_INFORMATION) { |
| 5258 | struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *) |
| 5259 | (offset + (char *)rsp); |
| 5260 | tcon->vol_serial_number = vol_info->VolumeSerialNumber; |
| 5261 | tcon->vol_create_time = vol_info->VolumeCreationTime; |
Steven French | af6a12e | 2013-10-09 20:55:53 -0500 | [diff] [blame] | 5262 | } |
Steve French | 34f6264 | 2013-10-09 02:07:00 -0500 | [diff] [blame] | 5263 | |
| 5264 | qfsattr_exit: |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5265 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
Pavel Shilovsky | 6fc05c2 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 5266 | return rc; |
| 5267 | } |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5268 | |
| 5269 | int |
| 5270 | smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, |
| 5271 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, |
| 5272 | const __u32 num_lock, struct smb2_lock_element *buf) |
| 5273 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5274 | struct smb_rqst rqst; |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5275 | int rc = 0; |
| 5276 | struct smb2_lock_req *req = NULL; |
| 5277 | struct kvec iov[2]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5278 | struct kvec rsp_iov; |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5279 | int resp_buf_type; |
| 5280 | unsigned int count; |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 5281 | int flags = CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5282 | unsigned int total_len; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5283 | struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5284 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5285 | cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5286 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5287 | rc = smb2_plain_req_init(SMB2_LOCK, tcon, server, |
| 5288 | (void **) &req, &total_len); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5289 | if (rc) |
| 5290 | return rc; |
| 5291 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5292 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5293 | flags |= CIFS_TRANSFORM_REQ; |
| 5294 | |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5295 | req->sync_hdr.ProcessId = cpu_to_le32(pid); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5296 | req->LockCount = cpu_to_le16(num_lock); |
| 5297 | |
| 5298 | req->PersistentFileId = persist_fid; |
| 5299 | req->VolatileFileId = volatile_fid; |
| 5300 | |
| 5301 | count = num_lock * sizeof(struct smb2_lock_element); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5302 | |
| 5303 | iov[0].iov_base = (char *)req; |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5304 | iov[0].iov_len = total_len - sizeof(struct smb2_lock_element); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5305 | iov[1].iov_base = (char *)buf; |
| 5306 | iov[1].iov_len = count; |
| 5307 | |
| 5308 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5309 | |
| 5310 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5311 | rqst.rq_iov = iov; |
| 5312 | rqst.rq_nvec = 2; |
| 5313 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5314 | rc = cifs_send_recv(xid, tcon->ses, server, |
| 5315 | &rqst, &resp_buf_type, flags, |
Ronnie Sahlberg | ced9367 | 2017-11-21 10:07:27 +1100 | [diff] [blame] | 5316 | &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5317 | cifs_small_buf_release(req); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5318 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5319 | cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5320 | cifs_stats_fail_inc(tcon, SMB2_LOCK_HE); |
Steve French | eccb442 | 2018-05-17 21:16:55 -0500 | [diff] [blame] | 5321 | trace_smb3_lock_err(xid, persist_fid, tcon->tid, |
| 5322 | tcon->ses->Suid, rc); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | return rc; |
| 5326 | } |
| 5327 | |
| 5328 | int |
| 5329 | SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon, |
| 5330 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, |
| 5331 | const __u64 length, const __u64 offset, const __u32 lock_flags, |
| 5332 | const bool wait) |
| 5333 | { |
| 5334 | struct smb2_lock_element lock; |
| 5335 | |
| 5336 | lock.Offset = cpu_to_le64(offset); |
| 5337 | lock.Length = cpu_to_le64(length); |
| 5338 | lock.Flags = cpu_to_le32(lock_flags); |
| 5339 | if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK) |
| 5340 | lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY); |
| 5341 | |
| 5342 | return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock); |
| 5343 | } |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5344 | |
| 5345 | int |
| 5346 | SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, |
| 5347 | __u8 *lease_key, const __le32 lease_state) |
| 5348 | { |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5349 | struct smb_rqst rqst; |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5350 | int rc; |
| 5351 | struct smb2_lease_ack *req = NULL; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5352 | struct cifs_ses *ses = tcon->ses; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5353 | int flags = CIFS_OBREAK_OP; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5354 | unsigned int total_len; |
| 5355 | struct kvec iov[1]; |
| 5356 | struct kvec rsp_iov; |
| 5357 | int resp_buf_type; |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5358 | __u64 *please_key_high; |
| 5359 | __u64 *please_key_low; |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5360 | struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5361 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5362 | cifs_dbg(FYI, "SMB2_lease_break\n"); |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5363 | rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, |
| 5364 | (void **) &req, &total_len); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5365 | if (rc) |
| 5366 | return rc; |
| 5367 | |
Steve French | 5a77e75 | 2018-05-09 17:43:08 -0500 | [diff] [blame] | 5368 | if (smb3_encryption_required(tcon)) |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 5369 | flags |= CIFS_TRANSFORM_REQ; |
| 5370 | |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5371 | req->sync_hdr.CreditRequest = cpu_to_le16(1); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5372 | req->StructureSize = cpu_to_le16(36); |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5373 | total_len += 12; |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5374 | |
| 5375 | memcpy(req->LeaseKey, lease_key, 16); |
| 5376 | req->LeaseState = lease_state; |
| 5377 | |
Ronnie Sahlberg | 392e1c5 | 2019-05-06 10:00:02 +1000 | [diff] [blame] | 5378 | flags |= CIFS_NO_RSP_BUF; |
Ronnie Sahlberg | 8eb7998 | 2017-11-21 11:04:37 +1100 | [diff] [blame] | 5379 | |
| 5380 | iov[0].iov_base = (char *)req; |
| 5381 | iov[0].iov_len = total_len; |
| 5382 | |
Ronnie Sahlberg | 40eff45 | 2018-06-12 08:00:59 +1000 | [diff] [blame] | 5383 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 5384 | rqst.rq_iov = iov; |
| 5385 | rqst.rq_nvec = 1; |
| 5386 | |
Aurelien Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 5387 | rc = cifs_send_recv(xid, ses, server, |
| 5388 | &rqst, &resp_buf_type, flags, &rsp_iov); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 5389 | cifs_small_buf_release(req); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5390 | |
Aurelien Aptel | d339adc | 2019-01-31 13:46:07 +0100 | [diff] [blame] | 5391 | please_key_low = (__u64 *)lease_key; |
| 5392 | please_key_high = (__u64 *)(lease_key+8); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5393 | if (rc) { |
| 5394 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5395 | trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid, |
| 5396 | ses->Suid, *please_key_low, *please_key_high, rc); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 5397 | cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc); |
Steve French | 179e44d | 2018-09-28 19:44:23 -0500 | [diff] [blame] | 5398 | } else |
| 5399 | trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid, |
| 5400 | ses->Suid, *please_key_low, *please_key_high); |
Pavel Shilovsky | 0822f51 | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 5401 | |
| 5402 | return rc; |
| 5403 | } |