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