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