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