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