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