1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * Copyright (C) International Business Machines Corp., 2009, 2013 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 */ 12 13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */ 14 /* Note that there are handle based routines which must be */ 15 /* treated slightly differently for reconnection purposes since we never */ 16 /* want to reuse a stale file handle and only the caller knows the file info */ 17 18 #include <linux/fs.h> 19 #include <linux/kernel.h> 20 #include <linux/vfs.h> 21 #include <linux/task_io_accounting_ops.h> 22 #include <linux/uaccess.h> 23 #include <linux/uuid.h> 24 #include <linux/pagemap.h> 25 #include <linux/xattr.h> 26 #include <linux/netfs.h> 27 #include <trace/events/netfs.h> 28 #include "cifsglob.h" 29 #include "cifsproto.h" 30 #include "cifsacl.h" 31 #include "smb2proto.h" 32 #include "cifs_unicode.h" 33 #include "cifs_debug.h" 34 #include "ntlmssp.h" 35 #include "../common/smbfsctl.h" 36 #include "../common/smb2status.h" 37 #include "smb2glob.h" 38 #include "cifs_spnego.h" 39 #include "smbdirect.h" 40 #include "trace.h" 41 #ifdef CONFIG_CIFS_DFS_UPCALL 42 #include "dfs_cache.h" 43 #endif 44 #include "cached_dir.h" 45 #include "compress.h" 46 #include "fs_context.h" 47 48 /* 49 * The following table defines the expected "StructureSize" of SMB2 requests 50 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests. 51 * 52 * Note that commands are defined in smb2pdu.h in le16 but the array below is 53 * indexed by command in host byte order. 54 */ 55 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { 56 /* SMB2_NEGOTIATE */ 36, 57 /* SMB2_SESSION_SETUP */ 25, 58 /* SMB2_LOGOFF */ 4, 59 /* SMB2_TREE_CONNECT */ 9, 60 /* SMB2_TREE_DISCONNECT */ 4, 61 /* SMB2_CREATE */ 57, 62 /* SMB2_CLOSE */ 24, 63 /* SMB2_FLUSH */ 24, 64 /* SMB2_READ */ 49, 65 /* SMB2_WRITE */ 49, 66 /* SMB2_LOCK */ 48, 67 /* SMB2_IOCTL */ 57, 68 /* SMB2_CANCEL */ 4, 69 /* SMB2_ECHO */ 4, 70 /* SMB2_QUERY_DIRECTORY */ 33, 71 /* SMB2_CHANGE_NOTIFY */ 32, 72 /* SMB2_QUERY_INFO */ 41, 73 /* SMB2_SET_INFO */ 33, 74 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */ 75 }; 76 77 int smb3_encryption_required(const struct cifs_tcon *tcon) 78 { 79 if (!tcon || !tcon->ses) 80 return 0; 81 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) || 82 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)) 83 return 1; 84 if (tcon->seal && 85 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) 86 return 1; 87 if (((global_secflags & CIFSSEC_MUST_SEAL) == CIFSSEC_MUST_SEAL) && 88 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) 89 return 1; 90 return 0; 91 } 92 93 static void 94 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd, 95 const struct cifs_tcon *tcon, 96 struct TCP_Server_Info *server) 97 { 98 struct smb3_hdr_req *smb3_hdr; 99 100 shdr->ProtocolId = SMB2_PROTO_NUMBER; 101 shdr->StructureSize = cpu_to_le16(64); 102 shdr->Command = smb2_cmd; 103 104 if (server) { 105 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */ 106 if (server->dialect >= SMB30_PROT_ID) { 107 smb3_hdr = (struct smb3_hdr_req *)shdr; 108 /* 109 * if primary channel is not set yet, use default 110 * channel for chan sequence num 111 */ 112 if (SERVER_IS_CHAN(server)) 113 smb3_hdr->ChannelSequence = 114 cpu_to_le16(server->primary_server->channel_sequence_num); 115 else 116 smb3_hdr->ChannelSequence = 117 cpu_to_le16(server->channel_sequence_num); 118 } 119 spin_lock(&server->req_lock); 120 /* Request up to 10 credits but don't go over the limit. */ 121 if (server->credits >= server->max_credits) 122 shdr->CreditRequest = cpu_to_le16(0); 123 else 124 shdr->CreditRequest = cpu_to_le16( 125 min_t(int, server->max_credits - 126 server->credits, 10)); 127 spin_unlock(&server->req_lock); 128 } else { 129 shdr->CreditRequest = cpu_to_le16(2); 130 } 131 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid); 132 133 if (!tcon) 134 goto out; 135 136 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */ 137 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */ 138 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU)) 139 shdr->CreditCharge = cpu_to_le16(1); 140 /* else CreditCharge MBZ */ 141 142 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid); 143 /* Uid is not converted */ 144 if (tcon->ses) 145 shdr->SessionId = cpu_to_le64(tcon->ses->Suid); 146 147 /* 148 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have 149 * to pass the path on the Open SMB prefixed by \\server\share. 150 * Not sure when we would need to do the augmented path (if ever) and 151 * setting this flag breaks the SMB2 open operation since it is 152 * illegal to send an empty path name (without \\server\share prefix) 153 * when the DFS flag is set in the SMB open header. We could 154 * consider setting the flag on all operations other than open 155 * but it is safer to net set it for now. 156 */ 157 /* if (tcon->share_flags & SHI1005_FLAGS_DFS) 158 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */ 159 160 if (server && server->sign && !smb3_encryption_required(tcon)) 161 shdr->Flags |= SMB2_FLAGS_SIGNED; 162 out: 163 return; 164 } 165 166 /* helper function for code reuse */ 167 static int 168 cifs_chan_skip_or_disable(struct cifs_ses *ses, 169 struct TCP_Server_Info *server, 170 bool from_reconnect, bool disable_mchan) 171 { 172 struct TCP_Server_Info *pserver; 173 unsigned int chan_index; 174 175 if (SERVER_IS_CHAN(server)) { 176 cifs_dbg(VFS, 177 "server %s does not support multichannel anymore. Skip secondary channel\n", 178 ses->server->hostname); 179 180 spin_lock(&ses->chan_lock); 181 chan_index = cifs_ses_get_chan_index(ses, server); 182 if (chan_index == CIFS_INVAL_CHAN_INDEX) { 183 spin_unlock(&ses->chan_lock); 184 goto skip_terminate; 185 } 186 187 ses->chans[chan_index].server = NULL; 188 server->terminate = true; 189 spin_unlock(&ses->chan_lock); 190 191 /* 192 * the above reference of server by channel 193 * needs to be dropped without holding chan_lock 194 * as cifs_put_tcp_session takes a higher lock 195 * i.e. cifs_tcp_ses_lock 196 */ 197 cifs_put_tcp_session(server, from_reconnect); 198 199 cifs_signal_cifsd_for_reconnect(server, false); 200 201 /* mark primary server as needing reconnect */ 202 pserver = server->primary_server; 203 cifs_signal_cifsd_for_reconnect(pserver, false); 204 skip_terminate: 205 return -EHOSTDOWN; 206 } 207 208 cifs_decrease_secondary_channels(ses, disable_mchan); 209 210 return 0; 211 } 212 213 /* 214 * smb3_update_ses_channels - Synchronize session channels with new configuration 215 * @ses: pointer to the CIFS session structure 216 * @server: pointer to the TCP server info structure 217 * @from_reconnect: indicates if called from reconnect context 218 * @disable_mchan: indicates if called from reconnect to disable multichannel 219 * 220 * Returns 0 on success or error code on failure. 221 * 222 * Outside of reconfigure, this function is called from cifs_mount() during mount 223 * and from reconnect scenarios to adjust channel count when the 224 * server's multichannel support changes. 225 */ 226 int smb3_update_ses_channels(struct cifs_ses *ses, struct TCP_Server_Info *server, 227 bool from_reconnect, bool disable_mchan) 228 { 229 int rc = 0; 230 /* 231 * Manage session channels based on current count vs max: 232 * - If disable requested, skip or disable the channel 233 * - If below max channels, attempt to add more 234 * - If above max channels, skip or disable excess channels 235 */ 236 if (disable_mchan) 237 rc = cifs_chan_skip_or_disable(ses, server, from_reconnect, disable_mchan); 238 else { 239 if (ses->chan_count < ses->chan_max) 240 rc = cifs_try_adding_channels(ses); 241 else if (ses->chan_count > ses->chan_max) 242 rc = cifs_chan_skip_or_disable(ses, server, from_reconnect, disable_mchan); 243 } 244 245 return rc; 246 } 247 248 static int 249 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon, 250 struct TCP_Server_Info *server, bool from_reconnect) 251 { 252 struct cifs_ses *ses; 253 int xid; 254 int rc = 0; 255 256 /* 257 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so 258 * check for tcp and smb session status done differently 259 * for those three - in the calling routine. 260 */ 261 if (tcon == NULL) 262 return 0; 263 264 if (smb2_command == SMB2_TREE_CONNECT) 265 return 0; 266 267 spin_lock(&tcon->tc_lock); 268 if (tcon->status == TID_EXITING) { 269 /* 270 * only tree disconnect allowed when disconnecting ... 271 */ 272 if (smb2_command != SMB2_TREE_DISCONNECT) { 273 spin_unlock(&tcon->tc_lock); 274 cifs_tcon_dbg(FYI, "can not send cmd %d while umounting\n", 275 smb2_command); 276 return -ENODEV; 277 } 278 } 279 spin_unlock(&tcon->tc_lock); 280 281 ses = tcon->ses; 282 if (!ses) 283 return smb_EIO(smb_eio_trace_null_pointers); 284 spin_lock(&ses->ses_lock); 285 if (ses->ses_status == SES_EXITING) { 286 spin_unlock(&ses->ses_lock); 287 return smb_EIO(smb_eio_trace_sess_exiting); 288 } 289 spin_unlock(&ses->ses_lock); 290 if (!ses->server || !server) 291 return smb_EIO(smb_eio_trace_null_pointers); 292 293 spin_lock(&server->srv_lock); 294 if (server->tcpStatus == CifsNeedReconnect) { 295 /* 296 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE 297 * here since they are implicitly done when session drops. 298 */ 299 switch (smb2_command) { 300 /* 301 * BB Should we keep oplock break and add flush to exceptions? 302 */ 303 case SMB2_TREE_DISCONNECT: 304 case SMB2_CANCEL: 305 case SMB2_CLOSE: 306 case SMB2_OPLOCK_BREAK: 307 spin_unlock(&server->srv_lock); 308 return -EAGAIN; 309 } 310 } 311 312 /* if server is marked for termination, cifsd will cleanup */ 313 if (server->terminate) { 314 spin_unlock(&server->srv_lock); 315 return -EHOSTDOWN; 316 } 317 spin_unlock(&server->srv_lock); 318 319 again: 320 rc = cifs_wait_for_server_reconnect(server, tcon->retry); 321 if (rc) 322 return rc; 323 324 spin_lock(&ses->chan_lock); 325 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) { 326 spin_unlock(&ses->chan_lock); 327 return 0; 328 } 329 spin_unlock(&ses->chan_lock); 330 cifs_tcon_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d\n", 331 tcon->ses->chans_need_reconnect, 332 tcon->need_reconnect); 333 334 mutex_lock(&ses->session_mutex); 335 /* 336 * Handle the case where a concurrent thread failed to negotiate or 337 * killed a channel. 338 */ 339 spin_lock(&server->srv_lock); 340 switch (server->tcpStatus) { 341 case CifsExiting: 342 spin_unlock(&server->srv_lock); 343 mutex_unlock(&ses->session_mutex); 344 return -EHOSTDOWN; 345 case CifsNeedReconnect: 346 spin_unlock(&server->srv_lock); 347 mutex_unlock(&ses->session_mutex); 348 if (!tcon->retry) 349 return -EHOSTDOWN; 350 goto again; 351 default: 352 break; 353 } 354 spin_unlock(&server->srv_lock); 355 356 /* 357 * need to prevent multiple threads trying to simultaneously 358 * reconnect the same SMB session 359 */ 360 spin_lock(&ses->ses_lock); 361 spin_lock(&ses->chan_lock); 362 if (!cifs_chan_needs_reconnect(ses, server) && 363 ses->ses_status == SES_GOOD) { 364 spin_unlock(&ses->chan_lock); 365 spin_unlock(&ses->ses_lock); 366 /* this means that we only need to tree connect */ 367 if (tcon->need_reconnect) 368 goto skip_sess_setup; 369 370 mutex_unlock(&ses->session_mutex); 371 goto out; 372 } 373 spin_unlock(&ses->chan_lock); 374 spin_unlock(&ses->ses_lock); 375 376 rc = cifs_negotiate_protocol(0, ses, server); 377 if (rc) { 378 mutex_unlock(&ses->session_mutex); 379 if (!tcon->retry) 380 return -EHOSTDOWN; 381 goto again; 382 } 383 /* 384 * if server stopped supporting multichannel 385 * and the first channel reconnected, disable all the others. 386 */ 387 if (ses->chan_count > 1 && 388 !(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) { 389 rc = smb3_update_ses_channels(ses, server, 390 from_reconnect, true /* disable_mchan */); 391 if (rc) { 392 mutex_unlock(&ses->session_mutex); 393 goto out; 394 } 395 } 396 397 rc = cifs_setup_session(0, ses, server, ses->local_nls); 398 if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED)) { 399 /* 400 * Try alternate password for next reconnect (key rotation 401 * could be enabled on the server e.g.) if an alternate 402 * password is available and the current password is expired, 403 * but do not swap on non pwd related errors like host down 404 */ 405 if (ses->password2) 406 swap(ses->password2, ses->password); 407 } 408 if (rc) { 409 mutex_unlock(&ses->session_mutex); 410 if (rc == -EACCES && !tcon->retry) 411 return -EHOSTDOWN; 412 goto out; 413 } 414 415 skip_sess_setup: 416 if (!tcon->need_reconnect) { 417 mutex_unlock(&ses->session_mutex); 418 goto out; 419 } 420 cifs_mark_open_files_invalid(tcon); 421 if (tcon->use_persistent) 422 tcon->need_reopen_files = true; 423 424 rc = cifs_tree_connect(0, tcon); 425 426 cifs_tcon_dbg(FYI, "reconnect tcon rc = %d\n", rc); 427 if (rc) { 428 /* If sess reconnected but tcon didn't, something strange ... */ 429 mutex_unlock(&ses->session_mutex); 430 cifs_tcon_dbg(VFS, "reconnect tcon failed rc = %d\n", rc); 431 goto out; 432 } 433 434 spin_lock(&ses->ses_lock); 435 if (ses->flags & CIFS_SES_FLAG_SCALE_CHANNELS) { 436 spin_unlock(&ses->ses_lock); 437 mutex_unlock(&ses->session_mutex); 438 goto skip_add_channels; 439 } 440 ses->flags |= CIFS_SES_FLAG_SCALE_CHANNELS; 441 spin_unlock(&ses->ses_lock); 442 443 if (!rc && 444 (server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL) && 445 server->ops->query_server_interfaces) { 446 /* 447 * query server network interfaces, in case they change. 448 * Also mark the session as pending this update while the query 449 * is in progress. This will be used to avoid calling 450 * smb2_reconnect recursively. 451 */ 452 ses->flags |= CIFS_SES_FLAGS_PENDING_QUERY_INTERFACES; 453 xid = get_xid(); 454 rc = server->ops->query_server_interfaces(xid, tcon, false); 455 free_xid(xid); 456 ses->flags &= ~CIFS_SES_FLAGS_PENDING_QUERY_INTERFACES; 457 458 if (!tcon->ipc && !tcon->dummy) 459 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces, 460 (SMB_INTERFACE_POLL_INTERVAL * HZ)); 461 462 mutex_unlock(&ses->session_mutex); 463 464 if (rc == -EOPNOTSUPP && ses->chan_count > 1) { 465 /* 466 * some servers like Azure SMB server do not advertise 467 * that multichannel has been disabled with server 468 * capabilities, rather return STATUS_NOT_IMPLEMENTED. 469 * treat this as server not supporting multichannel 470 */ 471 472 rc = smb3_update_ses_channels(ses, server, 473 from_reconnect, 474 true /* disable_mchan */); 475 goto skip_add_channels; 476 } else if (rc) 477 cifs_tcon_dbg(FYI, "%s: failed to query server interfaces: %d\n", 478 __func__, rc); 479 480 if (ses->chan_max > ses->chan_count && 481 ses->iface_count && 482 !SERVER_IS_CHAN(server)) { 483 if (ses->chan_count == 1) 484 cifs_server_dbg(VFS, "supports multichannel now\n"); 485 486 smb3_update_ses_channels(ses, server, from_reconnect, 487 false /* disable_mchan */); 488 } 489 } else { 490 mutex_unlock(&ses->session_mutex); 491 } 492 493 skip_add_channels: 494 spin_lock(&ses->ses_lock); 495 ses->flags &= ~CIFS_SES_FLAG_SCALE_CHANNELS; 496 spin_unlock(&ses->ses_lock); 497 498 if (smb2_command != SMB2_INTERNAL_CMD) 499 cifs_queue_server_reconn(server); 500 501 atomic_inc(&tconInfoReconnectCount); 502 out: 503 /* 504 * Check if handle based operation so we know whether we can continue 505 * or not without returning to caller to reset file handle. 506 */ 507 /* 508 * BB Is flush done by server on drop of tcp session? Should we special 509 * case it and skip above? 510 */ 511 switch (smb2_command) { 512 case SMB2_FLUSH: 513 case SMB2_READ: 514 case SMB2_WRITE: 515 case SMB2_LOCK: 516 case SMB2_QUERY_DIRECTORY: 517 case SMB2_CHANGE_NOTIFY: 518 case SMB2_QUERY_INFO: 519 case SMB2_SET_INFO: 520 case SMB2_IOCTL: 521 rc = -EAGAIN; 522 } 523 return rc; 524 } 525 526 static void 527 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, 528 struct TCP_Server_Info *server, 529 void *buf, 530 unsigned int *total_len) 531 { 532 struct smb2_pdu *spdu = buf; 533 /* lookup word count ie StructureSize from table */ 534 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)]; 535 536 /* 537 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of 538 * largest operations (Create) 539 */ 540 memset(buf, 0, 256); 541 542 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server); 543 spdu->StructureSize2 = cpu_to_le16(parmsize); 544 545 *total_len = parmsize + sizeof(struct smb2_hdr); 546 } 547 548 /* 549 * Allocate and return pointer to an SMB request hdr, and set basic 550 * SMB information in the SMB header. If the return code is zero, this 551 * function must have filled in request_buf pointer. 552 */ 553 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, 554 struct TCP_Server_Info *server, 555 void **request_buf, unsigned int *total_len) 556 { 557 /* BB eventually switch this to SMB2 specific small buf size */ 558 switch (smb2_command) { 559 case SMB2_SET_INFO: 560 case SMB2_QUERY_INFO: 561 *request_buf = cifs_buf_get(); 562 break; 563 default: 564 *request_buf = cifs_small_buf_get(); 565 break; 566 } 567 if (*request_buf == NULL) { 568 /* BB should we add a retry in here if not a writepage? */ 569 return -ENOMEM; 570 } 571 572 fill_small_buf(smb2_command, tcon, server, 573 (struct smb2_hdr *)(*request_buf), 574 total_len); 575 576 if (tcon != NULL) { 577 uint16_t com_code = le16_to_cpu(smb2_command); 578 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]); 579 cifs_stats_inc(&tcon->num_smbs_sent); 580 } 581 582 return 0; 583 } 584 585 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, 586 struct TCP_Server_Info *server, 587 void **request_buf, unsigned int *total_len) 588 { 589 int rc; 590 591 rc = smb2_reconnect(smb2_command, tcon, server, false); 592 if (rc) 593 return rc; 594 595 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf, 596 total_len); 597 } 598 599 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon, 600 struct TCP_Server_Info *server, 601 void **request_buf, unsigned int *total_len) 602 { 603 /* 604 * Skip reconnect in one of the following cases: 605 * 1. For FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs 606 * 2. For FSCTL_QUERY_NETWORK_INTERFACE_INFO IOCTL when called from 607 * smb2_reconnect (indicated by CIFS_SES_FLAG_SCALE_CHANNELS ses flag) 608 */ 609 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO || 610 (opcode == FSCTL_QUERY_NETWORK_INTERFACE_INFO && 611 (tcon->ses->flags & CIFS_SES_FLAGS_PENDING_QUERY_INTERFACES))) 612 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server, 613 request_buf, total_len); 614 615 return smb2_plain_req_init(SMB2_IOCTL, tcon, server, 616 request_buf, total_len); 617 } 618 619 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */ 620 621 static void 622 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt) 623 { 624 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; 625 pneg_ctxt->DataLength = cpu_to_le16(38); 626 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); 627 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); 628 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); 629 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512; 630 } 631 632 static void 633 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt) 634 { 635 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; 636 pneg_ctxt->DataLength = 637 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context) 638 - sizeof(struct smb2_neg_context)); 639 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3); 640 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77; 641 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF; 642 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1; 643 } 644 645 static unsigned int 646 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt) 647 { 648 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities); 649 unsigned short num_algs = 1; /* number of signing algorithms sent */ 650 651 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; 652 /* 653 * Context Data length must be rounded to multiple of 8 for some servers 654 */ 655 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) - 656 sizeof(struct smb2_neg_context) + 657 (num_algs * sizeof(u16)), 8)); 658 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs); 659 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC); 660 661 ctxt_len += sizeof(__le16) * num_algs; 662 ctxt_len = ALIGN(ctxt_len, 8); 663 return ctxt_len; 664 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */ 665 } 666 667 static void 668 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt) 669 { 670 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; 671 if (require_gcm_256) { 672 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */ 673 pneg_ctxt->CipherCount = cpu_to_le16(1); 674 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM; 675 } else if (enable_gcm_256) { 676 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */ 677 pneg_ctxt->CipherCount = cpu_to_le16(3); 678 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM; 679 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM; 680 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM; 681 } else { 682 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */ 683 pneg_ctxt->CipherCount = cpu_to_le16(2); 684 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM; 685 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM; 686 } 687 } 688 689 static unsigned int 690 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname) 691 { 692 struct nls_table *cp = load_nls_default(); 693 694 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID; 695 696 /* copy up to max of first 100 bytes of server name to NetName field */ 697 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp)); 698 /* context size is DataLength + minimal smb2_neg_context */ 699 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8); 700 } 701 702 static void 703 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) 704 { 705 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; 706 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); 707 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ 708 pneg_ctxt->Name[0] = 0x93; 709 pneg_ctxt->Name[1] = 0xAD; 710 pneg_ctxt->Name[2] = 0x25; 711 pneg_ctxt->Name[3] = 0x50; 712 pneg_ctxt->Name[4] = 0x9C; 713 pneg_ctxt->Name[5] = 0xB4; 714 pneg_ctxt->Name[6] = 0x11; 715 pneg_ctxt->Name[7] = 0xE7; 716 pneg_ctxt->Name[8] = 0xB4; 717 pneg_ctxt->Name[9] = 0x23; 718 pneg_ctxt->Name[10] = 0x83; 719 pneg_ctxt->Name[11] = 0xDE; 720 pneg_ctxt->Name[12] = 0x96; 721 pneg_ctxt->Name[13] = 0x8B; 722 pneg_ctxt->Name[14] = 0xCD; 723 pneg_ctxt->Name[15] = 0x7C; 724 } 725 726 static void 727 assemble_neg_contexts(struct smb2_negotiate_req *req, 728 struct TCP_Server_Info *server, unsigned int *total_len) 729 { 730 unsigned int ctxt_len, neg_context_count; 731 struct TCP_Server_Info *pserver; 732 char *pneg_ctxt; 733 char *hostname; 734 735 if (*total_len > 200) { 736 /* In case length corrupted don't want to overrun smb buffer */ 737 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n"); 738 return; 739 } 740 741 /* 742 * round up total_len of fixed part of SMB3 negotiate request to 8 743 * byte boundary before adding negotiate contexts 744 */ 745 *total_len = ALIGN(*total_len, 8); 746 747 pneg_ctxt = (*total_len) + (char *)req; 748 req->NegotiateContextOffset = cpu_to_le32(*total_len); 749 750 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt); 751 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8); 752 *total_len += ctxt_len; 753 pneg_ctxt += ctxt_len; 754 755 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt); 756 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8); 757 *total_len += ctxt_len; 758 pneg_ctxt += ctxt_len; 759 760 /* 761 * secondary channels don't have the hostname field populated 762 * use the hostname field in the primary channel instead 763 */ 764 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 765 cifs_server_lock(pserver); 766 hostname = pserver->hostname; 767 if (hostname && (hostname[0] != 0)) { 768 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt, 769 hostname); 770 *total_len += ctxt_len; 771 pneg_ctxt += ctxt_len; 772 neg_context_count = 3; 773 } else 774 neg_context_count = 2; 775 cifs_server_unlock(pserver); 776 777 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); 778 *total_len += sizeof(struct smb2_posix_neg_context); 779 pneg_ctxt += sizeof(struct smb2_posix_neg_context); 780 neg_context_count++; 781 782 if (server->compression.requested) { 783 build_compression_ctxt((struct smb2_compression_capabilities_context *) 784 pneg_ctxt); 785 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8); 786 *total_len += ctxt_len; 787 pneg_ctxt += ctxt_len; 788 neg_context_count++; 789 } 790 791 if (enable_negotiate_signing) { 792 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *) 793 pneg_ctxt); 794 *total_len += ctxt_len; 795 pneg_ctxt += ctxt_len; 796 neg_context_count++; 797 } 798 799 /* check for and add transport_capabilities and signing capabilities */ 800 req->NegotiateContextCount = cpu_to_le16(neg_context_count); 801 802 } 803 804 /* If invalid preauth context warn but use what we requested, SHA-512 */ 805 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt) 806 { 807 unsigned int len = le16_to_cpu(ctxt->DataLength); 808 809 /* 810 * Caller checked that DataLength remains within SMB boundary. We still 811 * need to confirm that one HashAlgorithms member is accounted for. 812 */ 813 if (len < MIN_PREAUTH_CTXT_DATA_LEN) { 814 pr_warn_once("server sent bad preauth context\n"); 815 return; 816 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) { 817 pr_warn_once("server sent invalid SaltLength\n"); 818 return; 819 } 820 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1) 821 pr_warn_once("Invalid SMB3 hash algorithm count\n"); 822 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512) 823 pr_warn_once("unknown SMB3 hash algorithm\n"); 824 } 825 826 static void decode_compress_ctx(struct TCP_Server_Info *server, 827 struct smb2_compression_capabilities_context *ctxt) 828 { 829 unsigned int len = le16_to_cpu(ctxt->DataLength); 830 __le16 alg; 831 832 server->compression.enabled = false; 833 834 /* 835 * Caller checked that DataLength remains within SMB boundary. We still 836 * need to confirm that one CompressionAlgorithms member is accounted 837 * for. 838 */ 839 if (len < 10) { 840 pr_warn_once("server sent bad compression cntxt\n"); 841 return; 842 } 843 844 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) { 845 pr_warn_once("invalid SMB3 compress algorithm count\n"); 846 return; 847 } 848 849 alg = ctxt->CompressionAlgorithms[0]; 850 851 /* 'NONE' (0) compressor type is never negotiated */ 852 if (alg == 0 || le16_to_cpu(alg) > 3) { 853 pr_warn_once("invalid compression algorithm '%u'\n", alg); 854 return; 855 } 856 857 server->compression.alg = alg; 858 server->compression.enabled = true; 859 } 860 861 static int decode_encrypt_ctx(struct TCP_Server_Info *server, 862 struct smb2_encryption_neg_context *ctxt) 863 { 864 unsigned int len = le16_to_cpu(ctxt->DataLength); 865 866 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len); 867 /* 868 * Caller checked that DataLength remains within SMB boundary. We still 869 * need to confirm that one Cipher flexible array member is accounted 870 * for. 871 */ 872 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) { 873 pr_warn_once("server sent bad crypto ctxt len\n"); 874 return -EINVAL; 875 } 876 877 if (le16_to_cpu(ctxt->CipherCount) != 1) { 878 pr_warn_once("Invalid SMB3.11 cipher count\n"); 879 return -EINVAL; 880 } 881 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0])); 882 if (require_gcm_256) { 883 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) { 884 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n"); 885 return -EOPNOTSUPP; 886 } 887 } else if (ctxt->Ciphers[0] == 0) { 888 /* 889 * e.g. if server only supported AES256_CCM (very unlikely) 890 * or server supported no encryption types or had all disabled. 891 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case 892 * in which mount requested encryption ("seal") checks later 893 * on during tree connection will return proper rc, but if 894 * seal not requested by client, since server is allowed to 895 * return 0 to indicate no supported cipher, we can't fail here 896 */ 897 server->cipher_type = 0; 898 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION; 899 pr_warn_once("Server does not support requested encryption types\n"); 900 return 0; 901 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) && 902 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) && 903 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) { 904 /* server returned a cipher we didn't ask for */ 905 pr_warn_once("Invalid SMB3.11 cipher returned\n"); 906 return -EINVAL; 907 } 908 server->cipher_type = ctxt->Ciphers[0]; 909 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION; 910 return 0; 911 } 912 913 static void decode_signing_ctx(struct TCP_Server_Info *server, 914 struct smb2_signing_capabilities *pctxt) 915 { 916 unsigned int len = le16_to_cpu(pctxt->DataLength); 917 918 /* 919 * Caller checked that DataLength remains within SMB boundary. We still 920 * need to confirm that one SigningAlgorithms flexible array member is 921 * accounted for. 922 */ 923 if ((len < 4) || (len > 16)) { 924 pr_warn_once("server sent bad signing negcontext\n"); 925 return; 926 } 927 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) { 928 pr_warn_once("Invalid signing algorithm count\n"); 929 return; 930 } 931 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) { 932 pr_warn_once("unknown signing algorithm\n"); 933 return; 934 } 935 936 server->signing_negotiated = true; 937 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]); 938 cifs_dbg(FYI, "signing algorithm %d chosen\n", 939 server->signing_algorithm); 940 } 941 942 943 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp, 944 struct TCP_Server_Info *server, 945 unsigned int len_of_smb) 946 { 947 struct smb2_neg_context *pctx; 948 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset); 949 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount); 950 unsigned int len_of_ctxts, i; 951 int rc = 0; 952 953 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt); 954 if (len_of_smb <= offset) { 955 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n"); 956 return -EINVAL; 957 } 958 959 len_of_ctxts = len_of_smb - offset; 960 961 for (i = 0; i < ctxt_cnt; i++) { 962 int clen; 963 /* check that offset is not beyond end of SMB */ 964 if (len_of_ctxts < sizeof(struct smb2_neg_context)) 965 break; 966 967 pctx = (struct smb2_neg_context *)(offset + (char *)rsp); 968 clen = sizeof(struct smb2_neg_context) 969 + le16_to_cpu(pctx->DataLength); 970 /* 971 * 2.2.4 SMB2 NEGOTIATE Response 972 * Subsequent negotiate contexts MUST appear at the first 8-byte 973 * aligned offset following the previous negotiate context. 974 */ 975 if (i + 1 != ctxt_cnt) 976 clen = ALIGN(clen, 8); 977 if (clen > len_of_ctxts) 978 break; 979 980 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) 981 decode_preauth_context( 982 (struct smb2_preauth_neg_context *)pctx); 983 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) 984 rc = decode_encrypt_ctx(server, 985 (struct smb2_encryption_neg_context *)pctx); 986 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) 987 decode_compress_ctx(server, 988 (struct smb2_compression_capabilities_context *)pctx); 989 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) 990 server->posix_ext_supported = true; 991 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) 992 decode_signing_ctx(server, 993 (struct smb2_signing_capabilities *)pctx); 994 else 995 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n", 996 le16_to_cpu(pctx->ContextType)); 997 if (rc) 998 break; 999 1000 offset += clen; 1001 len_of_ctxts -= clen; 1002 } 1003 return rc; 1004 } 1005 1006 static struct create_posix * 1007 create_posix_buf(umode_t mode) 1008 { 1009 struct create_posix *buf; 1010 1011 buf = kzalloc_obj(struct create_posix); 1012 if (!buf) 1013 return NULL; 1014 1015 buf->ccontext.DataOffset = 1016 cpu_to_le16(offsetof(struct create_posix, Mode)); 1017 buf->ccontext.DataLength = cpu_to_le32(4); 1018 buf->ccontext.NameOffset = 1019 cpu_to_le16(offsetof(struct create_posix, Name)); 1020 buf->ccontext.NameLength = cpu_to_le16(16); 1021 1022 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ 1023 buf->Name[0] = 0x93; 1024 buf->Name[1] = 0xAD; 1025 buf->Name[2] = 0x25; 1026 buf->Name[3] = 0x50; 1027 buf->Name[4] = 0x9C; 1028 buf->Name[5] = 0xB4; 1029 buf->Name[6] = 0x11; 1030 buf->Name[7] = 0xE7; 1031 buf->Name[8] = 0xB4; 1032 buf->Name[9] = 0x23; 1033 buf->Name[10] = 0x83; 1034 buf->Name[11] = 0xDE; 1035 buf->Name[12] = 0x96; 1036 buf->Name[13] = 0x8B; 1037 buf->Name[14] = 0xCD; 1038 buf->Name[15] = 0x7C; 1039 buf->Mode = cpu_to_le32(mode); 1040 cifs_dbg(FYI, "mode on posix create 0%o\n", mode); 1041 return buf; 1042 } 1043 1044 static int 1045 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode) 1046 { 1047 unsigned int num = *num_iovec; 1048 1049 iov[num].iov_base = create_posix_buf(mode); 1050 if (mode == ACL_NO_MODE) 1051 cifs_dbg(FYI, "%s: no mode\n", __func__); 1052 if (iov[num].iov_base == NULL) 1053 return -ENOMEM; 1054 iov[num].iov_len = sizeof(struct create_posix); 1055 *num_iovec = num + 1; 1056 return 0; 1057 } 1058 1059 1060 /* 1061 * 1062 * SMB2 Worker functions follow: 1063 * 1064 * The general structure of the worker functions is: 1065 * 1) Call smb2_init (assembles SMB2 header) 1066 * 2) Initialize SMB2 command specific fields in fixed length area of SMB 1067 * 3) Call smb_sendrcv2 (sends request on socket and waits for response) 1068 * 4) Decode SMB2 command specific fields in the fixed length area 1069 * 5) Decode variable length data area (if any for this SMB2 command type) 1070 * 6) Call free smb buffer 1071 * 7) return 1072 * 1073 */ 1074 1075 int 1076 SMB2_negotiate(const unsigned int xid, 1077 struct cifs_ses *ses, 1078 struct TCP_Server_Info *server) 1079 { 1080 struct smb_rqst rqst; 1081 struct smb2_negotiate_req *req; 1082 struct smb2_negotiate_rsp *rsp; 1083 struct kvec iov[1]; 1084 struct kvec rsp_iov; 1085 int rc; 1086 int resp_buftype; 1087 int blob_offset, blob_length; 1088 char *security_blob; 1089 int flags = CIFS_NEG_OP; 1090 unsigned int total_len; 1091 1092 cifs_dbg(FYI, "Negotiate protocol\n"); 1093 1094 if (!server) { 1095 WARN(1, "%s: server is NULL!\n", __func__); 1096 return smb_EIO(smb_eio_trace_null_pointers); 1097 } 1098 1099 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server, 1100 (void **) &req, &total_len); 1101 if (rc) 1102 return rc; 1103 1104 req->hdr.SessionId = 0; 1105 1106 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); 1107 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE); 1108 1109 if (strcmp(server->vals->version_string, 1110 SMB3ANY_VERSION_STRING) == 0) { 1111 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); 1112 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); 1113 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID); 1114 req->DialectCount = cpu_to_le16(3); 1115 total_len += 6; 1116 } else if (strcmp(server->vals->version_string, 1117 SMBDEFAULT_VERSION_STRING) == 0) { 1118 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); 1119 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); 1120 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); 1121 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); 1122 req->DialectCount = cpu_to_le16(4); 1123 total_len += 8; 1124 } else { 1125 /* otherwise send specific dialect */ 1126 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id); 1127 req->DialectCount = cpu_to_le16(1); 1128 total_len += 2; 1129 } 1130 1131 /* only one of SMB2 signing flags may be set in SMB2 request */ 1132 if (ses->sign) 1133 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); 1134 else if (global_secflags & CIFSSEC_MAY_SIGN) 1135 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); 1136 else 1137 req->SecurityMode = 0; 1138 1139 req->Capabilities = cpu_to_le32(server->vals->req_capabilities); 1140 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL); 1141 1142 /* ClientGUID must be zero for SMB2.02 dialect */ 1143 if (server->vals->protocol_id == SMB20_PROT_ID) 1144 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE); 1145 else { 1146 memcpy(req->ClientGUID, server->client_guid, 1147 SMB2_CLIENT_GUID_SIZE); 1148 if ((server->vals->protocol_id == SMB311_PROT_ID) || 1149 (strcmp(server->vals->version_string, 1150 SMB3ANY_VERSION_STRING) == 0) || 1151 (strcmp(server->vals->version_string, 1152 SMBDEFAULT_VERSION_STRING) == 0)) 1153 assemble_neg_contexts(req, server, &total_len); 1154 } 1155 iov[0].iov_base = (char *)req; 1156 iov[0].iov_len = total_len; 1157 1158 memset(&rqst, 0, sizeof(struct smb_rqst)); 1159 rqst.rq_iov = iov; 1160 rqst.rq_nvec = 1; 1161 1162 rc = cifs_send_recv(xid, ses, server, 1163 &rqst, &resp_buftype, flags, &rsp_iov); 1164 cifs_small_buf_release(req); 1165 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base; 1166 /* 1167 * No tcon so can't do 1168 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); 1169 */ 1170 if (rc == -EOPNOTSUPP) { 1171 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n"); 1172 goto neg_exit; 1173 } else if (rc != 0) 1174 goto neg_exit; 1175 1176 u16 dialect = le16_to_cpu(rsp->DialectRevision); 1177 if (strcmp(server->vals->version_string, 1178 SMB3ANY_VERSION_STRING) == 0) { 1179 switch (dialect) { 1180 case SMB20_PROT_ID: 1181 cifs_server_dbg(VFS, 1182 "SMB2 dialect returned but not requested\n"); 1183 rc = smb_EIO2(smb_eio_trace_neg_unreq_dialect, dialect, 3); 1184 goto neg_exit; 1185 case SMB21_PROT_ID: 1186 cifs_server_dbg(VFS, 1187 "SMB2.1 dialect returned but not requested\n"); 1188 rc = smb_EIO2(smb_eio_trace_neg_unreq_dialect, dialect, 3); 1189 goto neg_exit; 1190 case SMB311_PROT_ID: 1191 /* ops set to 3.0 by default for default so update */ 1192 server->ops = &smb311_operations; 1193 server->vals = &smb311_values; 1194 break; 1195 default: 1196 break; 1197 } 1198 } else if (strcmp(server->vals->version_string, 1199 SMBDEFAULT_VERSION_STRING) == 0) { 1200 switch (dialect) { 1201 case SMB20_PROT_ID: 1202 cifs_server_dbg(VFS, 1203 "SMB2 dialect returned but not requested\n"); 1204 rc = smb_EIO2(smb_eio_trace_neg_unreq_dialect, dialect, 0); 1205 goto neg_exit; 1206 case SMB21_PROT_ID: 1207 /* ops set to 3.0 by default for default so update */ 1208 server->ops = &smb21_operations; 1209 server->vals = &smb21_values; 1210 break; 1211 case SMB311_PROT_ID: 1212 server->ops = &smb311_operations; 1213 server->vals = &smb311_values; 1214 break; 1215 default: 1216 break; 1217 } 1218 } else if (dialect != server->vals->protocol_id) { 1219 /* if requested single dialect ensure returned dialect matched */ 1220 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n", 1221 dialect); 1222 rc = smb_EIO2(smb_eio_trace_neg_unreq_dialect, 1223 dialect, server->vals->protocol_id); 1224 goto neg_exit; 1225 } 1226 1227 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode); 1228 1229 switch (dialect) { 1230 case SMB20_PROT_ID: 1231 cifs_dbg(FYI, "negotiated smb2.0 dialect\n"); 1232 break; 1233 case SMB21_PROT_ID: 1234 cifs_dbg(FYI, "negotiated smb2.1 dialect\n"); 1235 break; 1236 case SMB30_PROT_ID: 1237 cifs_dbg(FYI, "negotiated smb3.0 dialect\n"); 1238 break; 1239 case SMB302_PROT_ID: 1240 cifs_dbg(FYI, "negotiated smb3.02 dialect\n"); 1241 break; 1242 case SMB311_PROT_ID: 1243 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n"); 1244 break; 1245 default: 1246 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n", 1247 dialect); 1248 rc = smb_EIO1(smb_eio_trace_neg_inval_dialect, dialect); 1249 goto neg_exit; 1250 } 1251 1252 rc = 0; 1253 server->dialect = dialect; 1254 1255 /* 1256 * Keep a copy of the hash after negprot. This hash will be 1257 * the starting hash value for all sessions made from this 1258 * server. 1259 */ 1260 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash, 1261 SMB2_PREAUTH_HASH_SIZE); 1262 1263 /* SMB2 only has an extended negflavor */ 1264 server->negflavor = CIFS_NEGFLAVOR_EXTENDED; 1265 /* set it to the maximum buffer size value we can send with 1 credit */ 1266 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize), 1267 SMB2_MAX_BUFFER_SIZE); 1268 server->max_read = le32_to_cpu(rsp->MaxReadSize); 1269 server->max_write = le32_to_cpu(rsp->MaxWriteSize); 1270 server->sec_mode = le16_to_cpu(rsp->SecurityMode); 1271 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode) 1272 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n", 1273 server->sec_mode); 1274 server->capabilities = le32_to_cpu(rsp->Capabilities); 1275 /* Internal types */ 1276 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES; 1277 1278 /* 1279 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context 1280 * Set the cipher type manually. 1281 */ 1282 if ((server->dialect == SMB30_PROT_ID || 1283 server->dialect == SMB302_PROT_ID) && 1284 (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) 1285 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM; 1286 1287 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length, 1288 (struct smb2_hdr *)rsp); 1289 /* 1290 * See MS-SMB2 section 2.2.4: if no blob, client picks default which 1291 * for us will be 1292 * ses->sectype = RawNTLMSSP; 1293 * but for time being this is our only auth choice so doesn't matter. 1294 * We just found a server which sets blob length to zero expecting raw. 1295 */ 1296 if (blob_length == 0) { 1297 cifs_dbg(FYI, "missing security blob on negprot\n"); 1298 server->sec_ntlmssp = true; 1299 } 1300 1301 rc = cifs_enable_signing(server, ses->sign); 1302 if (rc) 1303 goto neg_exit; 1304 if (blob_length) { 1305 rc = decode_negTokenInit(security_blob, blob_length, server); 1306 if (rc == 1) 1307 rc = 0; 1308 else if (rc == 0) 1309 rc = smb_EIO1(smb_eio_trace_neg_decode_token, rc); 1310 } 1311 1312 if (server->dialect == SMB311_PROT_ID) { 1313 if (rsp->NegotiateContextCount) 1314 rc = smb311_decode_neg_context(rsp, server, 1315 rsp_iov.iov_len); 1316 else 1317 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n"); 1318 } 1319 1320 if (server->cipher_type && !rc) 1321 rc = smb3_crypto_aead_allocate(server); 1322 neg_exit: 1323 free_rsp_buf(resp_buftype, rsp); 1324 return rc; 1325 } 1326 1327 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon) 1328 { 1329 int rc; 1330 struct validate_negotiate_info_req *pneg_inbuf; 1331 struct validate_negotiate_info_rsp *pneg_rsp = NULL; 1332 u32 rsplen; 1333 u32 inbuflen; /* max of 4 dialects */ 1334 struct TCP_Server_Info *server = tcon->ses->server; 1335 1336 cifs_dbg(FYI, "validate negotiate\n"); 1337 1338 /* In SMB3.11 preauth integrity supersedes validate negotiate */ 1339 if (server->dialect == SMB311_PROT_ID) 1340 return 0; 1341 1342 /* 1343 * validation ioctl must be signed, so no point sending this if we 1344 * can not sign it (ie are not known user). Even if signing is not 1345 * required (enabled but not negotiated), in those cases we selectively 1346 * sign just this, the first and only signed request on a connection. 1347 * Having validation of negotiate info helps reduce attack vectors. 1348 */ 1349 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) 1350 return 0; /* validation requires signing */ 1351 1352 if (tcon->ses->user_name == NULL) { 1353 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n"); 1354 return 0; /* validation requires signing */ 1355 } 1356 1357 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL) 1358 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n"); 1359 1360 pneg_inbuf = kmalloc_obj(*pneg_inbuf, GFP_NOFS); 1361 if (!pneg_inbuf) 1362 return -ENOMEM; 1363 1364 pneg_inbuf->Capabilities = 1365 cpu_to_le32(server->vals->req_capabilities); 1366 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL); 1367 1368 memcpy(pneg_inbuf->Guid, server->client_guid, 1369 SMB2_CLIENT_GUID_SIZE); 1370 1371 if (tcon->ses->sign) 1372 pneg_inbuf->SecurityMode = 1373 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); 1374 else if (global_secflags & CIFSSEC_MAY_SIGN) 1375 pneg_inbuf->SecurityMode = 1376 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); 1377 else 1378 pneg_inbuf->SecurityMode = 0; 1379 1380 1381 if (strcmp(server->vals->version_string, 1382 SMB3ANY_VERSION_STRING) == 0) { 1383 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID); 1384 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID); 1385 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID); 1386 pneg_inbuf->DialectCount = cpu_to_le16(3); 1387 /* SMB 2.1 not included so subtract one dialect from len */ 1388 inbuflen = sizeof(*pneg_inbuf) - 1389 (sizeof(pneg_inbuf->Dialects[0])); 1390 } else if (strcmp(server->vals->version_string, 1391 SMBDEFAULT_VERSION_STRING) == 0) { 1392 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID); 1393 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID); 1394 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID); 1395 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID); 1396 pneg_inbuf->DialectCount = cpu_to_le16(4); 1397 /* structure is big enough for 4 dialects */ 1398 inbuflen = sizeof(*pneg_inbuf); 1399 } else { 1400 /* otherwise specific dialect was requested */ 1401 pneg_inbuf->Dialects[0] = 1402 cpu_to_le16(server->vals->protocol_id); 1403 pneg_inbuf->DialectCount = cpu_to_le16(1); 1404 /* structure is big enough for 4 dialects, sending only 1 */ 1405 inbuflen = sizeof(*pneg_inbuf) - 1406 sizeof(pneg_inbuf->Dialects[0]) * 3; 1407 } 1408 1409 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID, 1410 FSCTL_VALIDATE_NEGOTIATE_INFO, 1411 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize, 1412 (char **)&pneg_rsp, &rsplen); 1413 if (rc == -EOPNOTSUPP) { 1414 /* 1415 * Old Windows versions or Netapp SMB server can return 1416 * not supported error. Client should accept it. 1417 */ 1418 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n"); 1419 rc = 0; 1420 goto out_free_inbuf; 1421 } else if (rc != 0) { 1422 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", 1423 rc); 1424 rc = smb_EIO1(smb_eio_trace_neg_info_fail, rc); 1425 goto out_free_inbuf; 1426 } 1427 1428 if (rsplen != sizeof(*pneg_rsp)) { 1429 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n", 1430 rsplen); 1431 1432 /* relax check since Mac returns max bufsize allowed on ioctl */ 1433 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp)) { 1434 rc = smb_EIO1(smb_eio_trace_neg_bad_rsplen, rsplen); 1435 goto out_free_rsp; 1436 } 1437 } 1438 1439 /* check validate negotiate info response matches what we got earlier */ 1440 u16 dialect = le16_to_cpu(pneg_rsp->Dialect); 1441 1442 if (dialect != server->dialect) { 1443 rc = smb_EIO2(smb_eio_trace_neg_info_dialect, 1444 dialect, server->dialect); 1445 goto vneg_out; 1446 } 1447 1448 u16 sec_mode = le16_to_cpu(pneg_rsp->SecurityMode); 1449 1450 if (sec_mode != server->sec_mode) { 1451 rc = smb_EIO2(smb_eio_trace_neg_info_sec_mode, 1452 sec_mode, server->sec_mode); 1453 goto vneg_out; 1454 } 1455 1456 /* do not validate server guid because not saved at negprot time yet */ 1457 u32 caps = le32_to_cpu(pneg_rsp->Capabilities); 1458 1459 if ((caps | SMB2_NT_FIND | 1460 SMB2_LARGE_FILES) != server->capabilities) { 1461 rc = smb_EIO2(smb_eio_trace_neg_info_caps, 1462 caps, server->capabilities); 1463 goto vneg_out; 1464 } 1465 1466 /* validate negotiate successful */ 1467 rc = 0; 1468 cifs_dbg(FYI, "validate negotiate info successful\n"); 1469 goto out_free_rsp; 1470 1471 vneg_out: 1472 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n"); 1473 out_free_rsp: 1474 kfree(pneg_rsp); 1475 out_free_inbuf: 1476 kfree(pneg_inbuf); 1477 return rc; 1478 } 1479 1480 enum securityEnum 1481 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested) 1482 { 1483 switch (requested) { 1484 case Kerberos: 1485 case RawNTLMSSP: 1486 return requested; 1487 case NTLMv2: 1488 return RawNTLMSSP; 1489 case Unspecified: 1490 if (server->sec_ntlmssp && 1491 (global_secflags & CIFSSEC_MAY_NTLMSSP)) 1492 return RawNTLMSSP; 1493 if ((server->sec_kerberos || server->sec_mskerberos || server->sec_iakerb) && 1494 (global_secflags & CIFSSEC_MAY_KRB5)) 1495 return Kerberos; 1496 fallthrough; 1497 default: 1498 return Unspecified; 1499 } 1500 } 1501 1502 struct SMB2_sess_data { 1503 unsigned int xid; 1504 struct cifs_ses *ses; 1505 struct TCP_Server_Info *server; 1506 struct nls_table *nls_cp; 1507 void (*func)(struct SMB2_sess_data *); 1508 int result; 1509 u64 previous_session; 1510 1511 /* we will send the SMB in three pieces: 1512 * a fixed length beginning part, an optional 1513 * SPNEGO blob (which can be zero length), and a 1514 * last part which will include the strings 1515 * and rest of bcc area. This allows us to avoid 1516 * a large buffer 17K allocation 1517 */ 1518 int buf0_type; 1519 struct kvec iov[2]; 1520 }; 1521 1522 static int 1523 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data) 1524 { 1525 int rc; 1526 struct cifs_ses *ses = sess_data->ses; 1527 struct TCP_Server_Info *server = sess_data->server; 1528 struct smb2_sess_setup_req *req; 1529 unsigned int total_len; 1530 bool is_binding = false; 1531 1532 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server, 1533 (void **) &req, 1534 &total_len); 1535 if (rc) 1536 return rc; 1537 1538 spin_lock(&ses->ses_lock); 1539 is_binding = (ses->ses_status == SES_GOOD); 1540 spin_unlock(&ses->ses_lock); 1541 1542 if (is_binding) { 1543 req->hdr.SessionId = cpu_to_le64(ses->Suid); 1544 req->hdr.Flags |= SMB2_FLAGS_SIGNED; 1545 req->PreviousSessionId = 0; 1546 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING; 1547 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid); 1548 } else { 1549 /* First session, not a reauthenticate */ 1550 req->hdr.SessionId = 0; 1551 /* 1552 * if reconnect, we need to send previous sess id 1553 * otherwise it is 0 1554 */ 1555 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session); 1556 req->Flags = 0; /* MBZ */ 1557 cifs_dbg(FYI, "Fresh session. Previous: %llx\n", 1558 sess_data->previous_session); 1559 } 1560 1561 /* enough to enable echos and oplocks and one max size write */ 1562 if (server->credits >= server->max_credits) 1563 req->hdr.CreditRequest = cpu_to_le16(0); 1564 else 1565 req->hdr.CreditRequest = cpu_to_le16( 1566 min_t(int, server->max_credits - 1567 server->credits, 130)); 1568 1569 /* only one of SMB2 signing flags may be set in SMB2 request */ 1570 if (server->sign) 1571 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED; 1572 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */ 1573 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED; 1574 else 1575 req->SecurityMode = 0; 1576 1577 #ifdef CONFIG_CIFS_DFS_UPCALL 1578 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS); 1579 #else 1580 req->Capabilities = 0; 1581 #endif /* DFS_UPCALL */ 1582 1583 req->Channel = 0; /* MBZ */ 1584 1585 sess_data->iov[0].iov_base = (char *)req; 1586 /* 1 for pad */ 1587 sess_data->iov[0].iov_len = total_len - 1; 1588 /* 1589 * This variable will be used to clear the buffer 1590 * allocated above in case of any error in the calling function. 1591 */ 1592 sess_data->buf0_type = CIFS_SMALL_BUFFER; 1593 1594 return 0; 1595 } 1596 1597 static void 1598 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data) 1599 { 1600 struct kvec *iov = sess_data->iov; 1601 1602 /* iov[1] is already freed by caller */ 1603 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base) 1604 memzero_explicit(iov[0].iov_base, iov[0].iov_len); 1605 1606 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base); 1607 sess_data->buf0_type = CIFS_NO_BUFFER; 1608 } 1609 1610 static int 1611 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data) 1612 { 1613 int rc; 1614 struct smb_rqst rqst; 1615 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base; 1616 struct kvec rsp_iov = { NULL, 0 }; 1617 1618 /* Testing shows that buffer offset must be at location of Buffer[0] */ 1619 req->SecurityBufferOffset = 1620 cpu_to_le16(sizeof(struct smb2_sess_setup_req)); 1621 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len); 1622 1623 memset(&rqst, 0, sizeof(struct smb_rqst)); 1624 rqst.rq_iov = sess_data->iov; 1625 rqst.rq_nvec = 2; 1626 1627 /* BB add code to build os and lm fields */ 1628 rc = cifs_send_recv(sess_data->xid, sess_data->ses, 1629 sess_data->server, 1630 &rqst, 1631 &sess_data->buf0_type, 1632 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov); 1633 cifs_small_buf_release(sess_data->iov[0].iov_base); 1634 if (rc == 0) 1635 sess_data->ses->expired_pwd = false; 1636 else if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED)) { 1637 if (sess_data->ses->expired_pwd == false) 1638 trace_smb3_key_expired(sess_data->server->hostname, 1639 sess_data->ses->user_name, 1640 sess_data->server->conn_id, 1641 &sess_data->server->dstaddr, rc); 1642 sess_data->ses->expired_pwd = true; 1643 } 1644 1645 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); 1646 1647 return rc; 1648 } 1649 1650 static int 1651 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data) 1652 { 1653 int rc = 0; 1654 struct cifs_ses *ses = sess_data->ses; 1655 struct TCP_Server_Info *server = sess_data->server; 1656 1657 cifs_server_lock(server); 1658 if (server->ops->generate_signingkey) { 1659 rc = server->ops->generate_signingkey(ses, server); 1660 if (rc) { 1661 cifs_dbg(FYI, 1662 "SMB3 session key generation failed\n"); 1663 cifs_server_unlock(server); 1664 return rc; 1665 } 1666 } 1667 if (!server->session_estab) { 1668 server->sequence_number = 0x2; 1669 server->session_estab = true; 1670 } 1671 cifs_server_unlock(server); 1672 1673 cifs_dbg(FYI, "SMB2/3 session established successfully\n"); 1674 return rc; 1675 } 1676 1677 #ifdef CONFIG_CIFS_UPCALL 1678 static void 1679 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) 1680 { 1681 int rc; 1682 struct cifs_ses *ses = sess_data->ses; 1683 struct TCP_Server_Info *server = sess_data->server; 1684 struct cifs_spnego_msg *msg; 1685 struct key *spnego_key = NULL; 1686 struct smb2_sess_setup_rsp *rsp = NULL; 1687 bool is_binding = false; 1688 1689 rc = SMB2_sess_alloc_buffer(sess_data); 1690 if (rc) 1691 goto out; 1692 1693 spnego_key = cifs_get_spnego_key(ses, server); 1694 if (IS_ERR(spnego_key)) { 1695 rc = PTR_ERR(spnego_key); 1696 spnego_key = NULL; 1697 goto out; 1698 } 1699 1700 msg = spnego_key->payload.data[0]; 1701 /* 1702 * check version field to make sure that cifs.upcall is 1703 * sending us a response in an expected form 1704 */ 1705 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { 1706 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n", 1707 CIFS_SPNEGO_UPCALL_VERSION, msg->version); 1708 rc = -EKEYREJECTED; 1709 goto out_put_spnego_key; 1710 } 1711 1712 spin_lock(&ses->ses_lock); 1713 is_binding = (ses->ses_status == SES_GOOD); 1714 spin_unlock(&ses->ses_lock); 1715 1716 kfree_sensitive(ses->auth_key.response); 1717 ses->auth_key.response = kmemdup(msg->data, 1718 msg->sesskey_len, 1719 GFP_KERNEL); 1720 if (!ses->auth_key.response) { 1721 cifs_dbg(VFS, "%s: can't allocate (%u bytes) memory\n", 1722 __func__, msg->sesskey_len); 1723 rc = -ENOMEM; 1724 goto out_put_spnego_key; 1725 } 1726 ses->auth_key.len = msg->sesskey_len; 1727 1728 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; 1729 sess_data->iov[1].iov_len = msg->secblob_len; 1730 1731 rc = SMB2_sess_sendreceive(sess_data); 1732 if (rc) 1733 goto out_put_spnego_key; 1734 1735 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; 1736 /* keep session id and flags if binding */ 1737 if (!is_binding) { 1738 ses->Suid = le64_to_cpu(rsp->hdr.SessionId); 1739 ses->session_flags = le16_to_cpu(rsp->SessionFlags); 1740 } 1741 1742 rc = SMB2_sess_establish_session(sess_data); 1743 out_put_spnego_key: 1744 key_invalidate(spnego_key); 1745 key_put(spnego_key); 1746 if (rc) { 1747 kfree_sensitive(ses->auth_key.response); 1748 ses->auth_key.response = NULL; 1749 ses->auth_key.len = 0; 1750 } 1751 out: 1752 sess_data->result = rc; 1753 sess_data->func = NULL; 1754 SMB2_sess_free_buffer(sess_data); 1755 } 1756 #else 1757 static void 1758 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) 1759 { 1760 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); 1761 sess_data->result = -EOPNOTSUPP; 1762 sess_data->func = NULL; 1763 } 1764 #endif 1765 1766 static void 1767 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data); 1768 1769 static void 1770 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data) 1771 { 1772 int rc; 1773 struct cifs_ses *ses = sess_data->ses; 1774 struct TCP_Server_Info *server = sess_data->server; 1775 struct smb2_sess_setup_rsp *rsp = NULL; 1776 unsigned char *ntlmssp_blob = NULL; 1777 bool use_spnego = false; /* else use raw ntlmssp */ 1778 u16 blob_length = 0; 1779 bool is_binding = false; 1780 1781 /* 1782 * If memory allocation is successful, caller of this function 1783 * frees it. 1784 */ 1785 ses->ntlmssp = kmalloc_obj(struct ntlmssp_auth); 1786 if (!ses->ntlmssp) { 1787 rc = -ENOMEM; 1788 goto out_err; 1789 } 1790 ses->ntlmssp->sesskey_per_smbsess = true; 1791 1792 rc = SMB2_sess_alloc_buffer(sess_data); 1793 if (rc) 1794 goto out_err; 1795 1796 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob, 1797 &blob_length, ses, server, 1798 sess_data->nls_cp); 1799 if (rc) 1800 goto out; 1801 1802 if (use_spnego) { 1803 /* BB eventually need to add this */ 1804 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); 1805 rc = -EOPNOTSUPP; 1806 goto out; 1807 } 1808 sess_data->iov[1].iov_base = ntlmssp_blob; 1809 sess_data->iov[1].iov_len = blob_length; 1810 1811 rc = SMB2_sess_sendreceive(sess_data); 1812 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; 1813 1814 /* If true, rc here is expected and not an error */ 1815 if (sess_data->buf0_type != CIFS_NO_BUFFER && 1816 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) 1817 rc = 0; 1818 1819 if (rc) 1820 goto out; 1821 1822 u16 boff = le16_to_cpu(rsp->SecurityBufferOffset); 1823 1824 if (offsetof(struct smb2_sess_setup_rsp, Buffer) != boff) { 1825 cifs_dbg(VFS, "Invalid security buffer offset %d\n", boff); 1826 rc = smb_EIO1(smb_eio_trace_sess_buf_off, boff); 1827 goto out; 1828 } 1829 rc = decode_ntlmssp_challenge(rsp->Buffer, 1830 le16_to_cpu(rsp->SecurityBufferLength), ses); 1831 if (rc) 1832 goto out; 1833 1834 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); 1835 1836 spin_lock(&ses->ses_lock); 1837 is_binding = (ses->ses_status == SES_GOOD); 1838 spin_unlock(&ses->ses_lock); 1839 1840 /* keep existing ses id and flags if binding */ 1841 if (!is_binding) { 1842 ses->Suid = le64_to_cpu(rsp->hdr.SessionId); 1843 ses->session_flags = le16_to_cpu(rsp->SessionFlags); 1844 } 1845 1846 out: 1847 kfree_sensitive(ntlmssp_blob); 1848 SMB2_sess_free_buffer(sess_data); 1849 if (!rc) { 1850 sess_data->result = 0; 1851 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate; 1852 return; 1853 } 1854 out_err: 1855 kfree_sensitive(ses->ntlmssp); 1856 ses->ntlmssp = NULL; 1857 sess_data->result = rc; 1858 sess_data->func = NULL; 1859 } 1860 1861 static void 1862 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data) 1863 { 1864 int rc; 1865 struct cifs_ses *ses = sess_data->ses; 1866 struct TCP_Server_Info *server = sess_data->server; 1867 struct smb2_sess_setup_req *req; 1868 struct smb2_sess_setup_rsp *rsp = NULL; 1869 unsigned char *ntlmssp_blob = NULL; 1870 bool use_spnego = false; /* else use raw ntlmssp */ 1871 u16 blob_length = 0; 1872 bool is_binding = false; 1873 1874 rc = SMB2_sess_alloc_buffer(sess_data); 1875 if (rc) 1876 goto out; 1877 1878 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base; 1879 req->hdr.SessionId = cpu_to_le64(ses->Suid); 1880 1881 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, 1882 ses, server, 1883 sess_data->nls_cp); 1884 if (rc) { 1885 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc); 1886 goto out; 1887 } 1888 1889 if (use_spnego) { 1890 /* BB eventually need to add this */ 1891 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); 1892 rc = -EOPNOTSUPP; 1893 goto out; 1894 } 1895 sess_data->iov[1].iov_base = ntlmssp_blob; 1896 sess_data->iov[1].iov_len = blob_length; 1897 1898 rc = SMB2_sess_sendreceive(sess_data); 1899 if (rc) 1900 goto out; 1901 1902 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; 1903 1904 spin_lock(&ses->ses_lock); 1905 is_binding = (ses->ses_status == SES_GOOD); 1906 spin_unlock(&ses->ses_lock); 1907 1908 /* keep existing ses id and flags if binding */ 1909 if (!is_binding) { 1910 ses->Suid = le64_to_cpu(rsp->hdr.SessionId); 1911 ses->session_flags = le16_to_cpu(rsp->SessionFlags); 1912 } 1913 1914 rc = SMB2_sess_establish_session(sess_data); 1915 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS 1916 if (ses->server->dialect < SMB30_PROT_ID) { 1917 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__); 1918 /* 1919 * The session id is opaque in terms of endianness, so we can't 1920 * print it as a long long. we dump it as we got it on the wire 1921 */ 1922 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid), 1923 &ses->Suid); 1924 cifs_dbg(VFS, "Session Key %*ph\n", 1925 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response); 1926 cifs_dbg(VFS, "Signing Key %*ph\n", 1927 SMB3_SIGN_KEY_SIZE, ses->auth_key.response); 1928 } 1929 #endif 1930 out: 1931 kfree_sensitive(ntlmssp_blob); 1932 SMB2_sess_free_buffer(sess_data); 1933 kfree_sensitive(ses->ntlmssp); 1934 ses->ntlmssp = NULL; 1935 sess_data->result = rc; 1936 sess_data->func = NULL; 1937 } 1938 1939 static int 1940 SMB2_select_sec(struct SMB2_sess_data *sess_data) 1941 { 1942 int type; 1943 struct cifs_ses *ses = sess_data->ses; 1944 struct TCP_Server_Info *server = sess_data->server; 1945 1946 type = smb2_select_sectype(server, ses->sectype); 1947 cifs_dbg(FYI, "sess setup type %d\n", type); 1948 if (type == Unspecified) { 1949 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n"); 1950 return -EINVAL; 1951 } 1952 1953 switch (type) { 1954 case Kerberos: 1955 sess_data->func = SMB2_auth_kerberos; 1956 break; 1957 case RawNTLMSSP: 1958 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate; 1959 break; 1960 default: 1961 cifs_dbg(VFS, "secType %d not supported!\n", type); 1962 return -EOPNOTSUPP; 1963 } 1964 1965 return 0; 1966 } 1967 1968 int 1969 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, 1970 struct TCP_Server_Info *server, 1971 const struct nls_table *nls_cp) 1972 { 1973 int rc = 0; 1974 struct SMB2_sess_data *sess_data; 1975 1976 cifs_dbg(FYI, "Session Setup\n"); 1977 1978 if (!server) { 1979 WARN(1, "%s: server is NULL!\n", __func__); 1980 return smb_EIO(smb_eio_trace_null_pointers); 1981 } 1982 1983 sess_data = kzalloc_obj(struct SMB2_sess_data); 1984 if (!sess_data) 1985 return -ENOMEM; 1986 1987 sess_data->xid = xid; 1988 sess_data->ses = ses; 1989 sess_data->server = server; 1990 sess_data->buf0_type = CIFS_NO_BUFFER; 1991 sess_data->nls_cp = (struct nls_table *) nls_cp; 1992 sess_data->previous_session = ses->Suid; 1993 1994 rc = SMB2_select_sec(sess_data); 1995 if (rc) 1996 goto out; 1997 1998 /* 1999 * Initialize the session hash with the server one. 2000 */ 2001 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash, 2002 SMB2_PREAUTH_HASH_SIZE); 2003 2004 while (sess_data->func) 2005 sess_data->func(sess_data); 2006 2007 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign)) 2008 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n"); 2009 rc = sess_data->result; 2010 out: 2011 kfree_sensitive(sess_data); 2012 return rc; 2013 } 2014 2015 int 2016 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses) 2017 { 2018 struct smb_rqst rqst; 2019 struct smb2_logoff_req *req; /* response is also trivial struct */ 2020 int rc = 0; 2021 struct TCP_Server_Info *server; 2022 int flags = 0; 2023 unsigned int total_len; 2024 struct kvec iov[1]; 2025 struct kvec rsp_iov; 2026 int resp_buf_type; 2027 2028 cifs_dbg(FYI, "disconnect session %p\n", ses); 2029 2030 if (!ses || !ses->server) 2031 return smb_EIO(smb_eio_trace_null_pointers); 2032 server = ses->server; 2033 2034 /* no need to send SMB logoff if uid already closed due to reconnect */ 2035 spin_lock(&ses->chan_lock); 2036 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) { 2037 spin_unlock(&ses->chan_lock); 2038 goto smb2_session_already_dead; 2039 } 2040 spin_unlock(&ses->chan_lock); 2041 2042 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server, 2043 (void **) &req, &total_len); 2044 if (rc) 2045 return rc; 2046 2047 /* since no tcon, smb2_init can not do this, so do here */ 2048 req->hdr.SessionId = cpu_to_le64(ses->Suid); 2049 2050 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) 2051 flags |= CIFS_TRANSFORM_REQ; 2052 else if (server->sign) 2053 req->hdr.Flags |= SMB2_FLAGS_SIGNED; 2054 2055 flags |= CIFS_NO_RSP_BUF; 2056 2057 iov[0].iov_base = (char *)req; 2058 iov[0].iov_len = total_len; 2059 2060 memset(&rqst, 0, sizeof(struct smb_rqst)); 2061 rqst.rq_iov = iov; 2062 rqst.rq_nvec = 1; 2063 2064 rc = cifs_send_recv(xid, ses, ses->server, 2065 &rqst, &resp_buf_type, flags, &rsp_iov); 2066 cifs_small_buf_release(req); 2067 /* 2068 * No tcon so can't do 2069 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); 2070 */ 2071 2072 smb2_session_already_dead: 2073 return rc; 2074 } 2075 2076 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code) 2077 { 2078 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]); 2079 } 2080 2081 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */) 2082 2083 /* These are similar values to what Windows uses */ 2084 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon) 2085 { 2086 tcon->max_chunks = 256; 2087 tcon->max_bytes_chunk = 1048576; 2088 tcon->max_bytes_copy = 16777216; 2089 } 2090 2091 int 2092 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, 2093 struct cifs_tcon *tcon, const struct nls_table *cp) 2094 { 2095 struct smb_rqst rqst; 2096 struct smb2_tree_connect_req *req; 2097 struct smb2_tree_connect_rsp *rsp = NULL; 2098 struct kvec iov[2]; 2099 struct kvec rsp_iov = { NULL, 0 }; 2100 int rc = 0; 2101 int resp_buftype; 2102 int unc_path_len; 2103 __le16 *unc_path = NULL; 2104 int flags = 0; 2105 unsigned int total_len; 2106 struct TCP_Server_Info *server = cifs_pick_channel(ses); 2107 2108 cifs_dbg(FYI, "TCON\n"); 2109 2110 if (!server || !tree) 2111 return smb_EIO(smb_eio_trace_null_pointers); 2112 2113 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL); 2114 if (unc_path == NULL) 2115 return -ENOMEM; 2116 2117 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp); 2118 if (unc_path_len <= 0) { 2119 kfree(unc_path); 2120 return -EINVAL; 2121 } 2122 unc_path_len *= 2; 2123 2124 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */ 2125 tcon->tid = 0; 2126 atomic_set(&tcon->num_remote_opens, 0); 2127 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server, 2128 (void **) &req, &total_len); 2129 if (rc) { 2130 kfree(unc_path); 2131 return rc; 2132 } 2133 2134 if (smb3_encryption_required(tcon)) 2135 flags |= CIFS_TRANSFORM_REQ; 2136 2137 iov[0].iov_base = (char *)req; 2138 /* 1 for pad */ 2139 iov[0].iov_len = total_len - 1; 2140 2141 /* Testing shows that buffer offset must be at location of Buffer[0] */ 2142 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)); 2143 req->PathLength = cpu_to_le16(unc_path_len); 2144 iov[1].iov_base = unc_path; 2145 iov[1].iov_len = unc_path_len; 2146 2147 /* 2148 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 2149 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1 2150 * (Samba servers don't always set the flag so also check if null user) 2151 */ 2152 if ((server->dialect == SMB311_PROT_ID) && 2153 !smb3_encryption_required(tcon) && 2154 !(ses->session_flags & 2155 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) && 2156 ((ses->user_name != NULL) || (ses->sectype == Kerberos))) 2157 req->hdr.Flags |= SMB2_FLAGS_SIGNED; 2158 2159 memset(&rqst, 0, sizeof(struct smb_rqst)); 2160 rqst.rq_iov = iov; 2161 rqst.rq_nvec = 2; 2162 2163 /* Need 64 for max size write so ask for more in case not there yet */ 2164 if (server->credits >= server->max_credits) 2165 req->hdr.CreditRequest = cpu_to_le16(0); 2166 else 2167 req->hdr.CreditRequest = cpu_to_le16( 2168 min_t(int, server->max_credits - 2169 server->credits, 64)); 2170 2171 rc = cifs_send_recv(xid, ses, server, 2172 &rqst, &resp_buftype, flags, &rsp_iov); 2173 cifs_small_buf_release(req); 2174 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base; 2175 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc); 2176 if ((rc != 0) || (rsp == NULL)) { 2177 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE); 2178 tcon->need_reconnect = true; 2179 goto tcon_error_exit; 2180 } 2181 2182 switch (rsp->ShareType) { 2183 case SMB2_SHARE_TYPE_DISK: 2184 cifs_dbg(FYI, "connection to disk share\n"); 2185 break; 2186 case SMB2_SHARE_TYPE_PIPE: 2187 tcon->pipe = true; 2188 cifs_dbg(FYI, "connection to pipe share\n"); 2189 break; 2190 case SMB2_SHARE_TYPE_PRINT: 2191 tcon->print = true; 2192 cifs_dbg(FYI, "connection to printer\n"); 2193 break; 2194 default: 2195 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType); 2196 rc = -EOPNOTSUPP; 2197 goto tcon_error_exit; 2198 } 2199 2200 tcon->share_flags = le32_to_cpu(rsp->ShareFlags); 2201 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */ 2202 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess); 2203 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId); 2204 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name)); 2205 2206 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) && 2207 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0)) 2208 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n"); 2209 2210 if (tcon->seal && 2211 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) 2212 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n"); 2213 2214 init_copy_chunk_defaults(tcon); 2215 if (server->ops->validate_negotiate) 2216 rc = server->ops->validate_negotiate(xid, tcon); 2217 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */ 2218 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT) 2219 server->nosharesock = true; 2220 tcon_exit: 2221 2222 free_rsp_buf(resp_buftype, rsp); 2223 kfree(unc_path); 2224 return rc; 2225 2226 tcon_error_exit: 2227 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) 2228 cifs_dbg(VFS | ONCE, "BAD_NETWORK_NAME: %s\n", tree); 2229 goto tcon_exit; 2230 } 2231 2232 int 2233 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) 2234 { 2235 struct smb_rqst rqst; 2236 struct smb2_tree_disconnect_req *req; /* response is trivial */ 2237 int rc = 0; 2238 struct cifs_ses *ses = tcon->ses; 2239 struct TCP_Server_Info *server = cifs_pick_channel(ses); 2240 int flags = 0; 2241 unsigned int total_len; 2242 struct kvec iov[1]; 2243 struct kvec rsp_iov; 2244 int resp_buf_type; 2245 2246 cifs_dbg(FYI, "Tree Disconnect\n"); 2247 2248 if (!ses || !(ses->server)) 2249 return smb_EIO(smb_eio_trace_null_pointers); 2250 2251 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name); 2252 spin_lock(&ses->chan_lock); 2253 if ((tcon->need_reconnect) || 2254 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) { 2255 spin_unlock(&ses->chan_lock); 2256 return 0; 2257 } 2258 spin_unlock(&ses->chan_lock); 2259 2260 invalidate_all_cached_dirs(tcon); 2261 2262 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server, 2263 (void **) &req, 2264 &total_len); 2265 if (rc) 2266 return rc; 2267 2268 if (smb3_encryption_required(tcon)) 2269 flags |= CIFS_TRANSFORM_REQ; 2270 2271 flags |= CIFS_NO_RSP_BUF; 2272 2273 iov[0].iov_base = (char *)req; 2274 iov[0].iov_len = total_len; 2275 2276 memset(&rqst, 0, sizeof(struct smb_rqst)); 2277 rqst.rq_iov = iov; 2278 rqst.rq_nvec = 1; 2279 2280 rc = cifs_send_recv(xid, ses, server, 2281 &rqst, &resp_buf_type, flags, &rsp_iov); 2282 cifs_small_buf_release(req); 2283 if (rc) { 2284 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE); 2285 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc); 2286 } 2287 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid); 2288 2289 return rc; 2290 } 2291 2292 static create_durable_req_t * 2293 create_durable_buf(void) 2294 { 2295 create_durable_req_t *buf; 2296 2297 buf = kzalloc_obj(create_durable_req_t); 2298 if (!buf) 2299 return NULL; 2300 2301 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2302 (create_durable_req_t, Data)); 2303 buf->ccontext.DataLength = cpu_to_le32(16); 2304 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2305 (create_durable_req_t, Name)); 2306 buf->ccontext.NameLength = cpu_to_le16(4); 2307 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */ 2308 buf->Name[0] = 'D'; 2309 buf->Name[1] = 'H'; 2310 buf->Name[2] = 'n'; 2311 buf->Name[3] = 'Q'; 2312 return buf; 2313 } 2314 2315 static create_durable_req_t * 2316 create_reconnect_durable_buf(struct cifs_fid *fid) 2317 { 2318 create_durable_req_t *buf; 2319 2320 buf = kzalloc_obj(create_durable_req_t); 2321 if (!buf) 2322 return NULL; 2323 2324 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2325 (create_durable_req_t, Data)); 2326 buf->ccontext.DataLength = cpu_to_le32(16); 2327 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2328 (create_durable_req_t, Name)); 2329 buf->ccontext.NameLength = cpu_to_le16(4); 2330 buf->Data.Fid.PersistentFileId = fid->persistent_fid; 2331 buf->Data.Fid.VolatileFileId = fid->volatile_fid; 2332 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */ 2333 buf->Name[0] = 'D'; 2334 buf->Name[1] = 'H'; 2335 buf->Name[2] = 'n'; 2336 buf->Name[3] = 'C'; 2337 return buf; 2338 } 2339 2340 static void 2341 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf) 2342 { 2343 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc; 2344 2345 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n", 2346 pdisk_id->DiskFileId, pdisk_id->VolumeId); 2347 buf->IndexNumber = pdisk_id->DiskFileId; 2348 } 2349 2350 static void 2351 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info, 2352 struct create_posix_rsp *posix) 2353 { 2354 int sid_len; 2355 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset); 2356 u8 *end = beg + le32_to_cpu(cc->DataLength); 2357 u8 *sid; 2358 2359 memset(posix, 0, sizeof(*posix)); 2360 2361 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0)); 2362 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4)); 2363 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8)); 2364 2365 sid = beg + 12; 2366 sid_len = posix_info_sid_size(sid, end); 2367 if (sid_len < 0) { 2368 cifs_dbg(VFS, "bad owner sid in posix create response\n"); 2369 return; 2370 } 2371 memcpy(&posix->owner, sid, sid_len); 2372 2373 sid = sid + sid_len; 2374 sid_len = posix_info_sid_size(sid, end); 2375 if (sid_len < 0) { 2376 cifs_dbg(VFS, "bad group sid in posix create response\n"); 2377 return; 2378 } 2379 memcpy(&posix->group, sid, sid_len); 2380 2381 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n", 2382 posix->nlink, posix->mode, posix->reparse_tag); 2383 } 2384 2385 int smb2_parse_contexts(struct TCP_Server_Info *server, 2386 struct kvec *rsp_iov, 2387 __u16 *epoch, 2388 char *lease_key, __u8 *oplock, 2389 struct smb2_file_all_info *buf, 2390 struct create_posix_rsp *posix) 2391 { 2392 struct smb2_create_rsp *rsp = rsp_iov->iov_base; 2393 struct create_context *cc; 2394 size_t rem, off, len; 2395 size_t doff, dlen; 2396 size_t noff, nlen; 2397 char *name; 2398 static const char smb3_create_tag_posix[] = { 2399 0x93, 0xAD, 0x25, 0x50, 0x9C, 2400 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83, 2401 0xDE, 0x96, 0x8B, 0xCD, 0x7C 2402 }; 2403 2404 *oplock = 0; 2405 2406 off = le32_to_cpu(rsp->CreateContextsOffset); 2407 rem = le32_to_cpu(rsp->CreateContextsLength); 2408 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len) 2409 return -EINVAL; 2410 cc = (struct create_context *)((u8 *)rsp + off); 2411 2412 /* Initialize inode number to 0 in case no valid data in qfid context */ 2413 if (buf) 2414 buf->IndexNumber = 0; 2415 2416 while (rem >= sizeof(*cc)) { 2417 doff = le16_to_cpu(cc->DataOffset); 2418 dlen = le32_to_cpu(cc->DataLength); 2419 if (check_add_overflow(doff, dlen, &len) || len > rem) 2420 return -EINVAL; 2421 2422 noff = le16_to_cpu(cc->NameOffset); 2423 nlen = le16_to_cpu(cc->NameLength); 2424 if (noff + nlen > doff) 2425 return -EINVAL; 2426 2427 name = (char *)cc + noff; 2428 switch (nlen) { 2429 case 4: 2430 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) { 2431 *oplock = server->ops->parse_lease_buf(cc, epoch, 2432 lease_key); 2433 } else if (buf && 2434 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) { 2435 parse_query_id_ctxt(cc, buf); 2436 } 2437 break; 2438 case 16: 2439 if (posix && !memcmp(name, smb3_create_tag_posix, 16)) 2440 parse_posix_ctxt(cc, buf, posix); 2441 break; 2442 default: 2443 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n", 2444 __func__, nlen, dlen); 2445 if (IS_ENABLED(CONFIG_CIFS_DEBUG2)) 2446 cifs_dump_mem("context data: ", cc, dlen); 2447 break; 2448 } 2449 2450 off = le32_to_cpu(cc->Next); 2451 if (!off) 2452 break; 2453 if (check_sub_overflow(rem, off, &rem)) 2454 return -EINVAL; 2455 cc = (struct create_context *)((u8 *)cc + off); 2456 } 2457 2458 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) 2459 *oplock = rsp->OplockLevel; 2460 2461 return 0; 2462 } 2463 2464 static int 2465 add_lease_context(struct TCP_Server_Info *server, 2466 struct smb2_create_req *req, 2467 struct kvec *iov, 2468 unsigned int *num_iovec, 2469 u8 *lease_key, 2470 __u8 *oplock, 2471 u8 *parent_lease_key, 2472 __le32 flags) 2473 { 2474 unsigned int num = *num_iovec; 2475 2476 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock, 2477 parent_lease_key, flags); 2478 if (iov[num].iov_base == NULL) 2479 return -ENOMEM; 2480 iov[num].iov_len = server->vals->create_lease_size; 2481 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE; 2482 *num_iovec = num + 1; 2483 return 0; 2484 } 2485 2486 static struct create_durable_req_v2 * 2487 create_durable_v2_buf(struct cifs_open_parms *oparms) 2488 { 2489 struct cifs_fid *pfid = oparms->fid; 2490 struct create_durable_req_v2 *buf; 2491 2492 buf = kzalloc_obj(struct create_durable_req_v2); 2493 if (!buf) 2494 return NULL; 2495 2496 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2497 (struct create_durable_req_v2, dcontext)); 2498 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2_req)); 2499 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2500 (struct create_durable_req_v2, Name)); 2501 buf->ccontext.NameLength = cpu_to_le16(4); 2502 2503 /* 2504 * NB: Handle timeout defaults to 0, which allows server to choose 2505 * (most servers default to 120 seconds) and most clients default to 0. 2506 * This can be overridden at mount ("handletimeout=") if the user wants 2507 * a different persistent (or resilient) handle timeout for all opens 2508 * on a particular SMB3 mount. 2509 */ 2510 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout); 2511 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); 2512 2513 /* for replay, we should not overwrite the existing create guid */ 2514 if (!oparms->replay) { 2515 generate_random_uuid(buf->dcontext.CreateGuid); 2516 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16); 2517 } else 2518 memcpy(buf->dcontext.CreateGuid, pfid->create_guid, 16); 2519 2520 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */ 2521 buf->Name[0] = 'D'; 2522 buf->Name[1] = 'H'; 2523 buf->Name[2] = '2'; 2524 buf->Name[3] = 'Q'; 2525 return buf; 2526 } 2527 2528 static struct create_durable_handle_reconnect_v2 * 2529 create_reconnect_durable_v2_buf(struct cifs_fid *fid) 2530 { 2531 struct create_durable_handle_reconnect_v2 *buf; 2532 2533 buf = kzalloc_obj(struct create_durable_handle_reconnect_v2); 2534 if (!buf) 2535 return NULL; 2536 2537 buf->ccontext.DataOffset = 2538 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, 2539 dcontext)); 2540 buf->ccontext.DataLength = 2541 cpu_to_le32(sizeof(struct durable_reconnect_context_v2)); 2542 buf->ccontext.NameOffset = 2543 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, 2544 Name)); 2545 buf->ccontext.NameLength = cpu_to_le16(4); 2546 2547 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid; 2548 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid; 2549 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); 2550 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16); 2551 2552 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */ 2553 buf->Name[0] = 'D'; 2554 buf->Name[1] = 'H'; 2555 buf->Name[2] = '2'; 2556 buf->Name[3] = 'C'; 2557 return buf; 2558 } 2559 2560 static int 2561 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec, 2562 struct cifs_open_parms *oparms) 2563 { 2564 unsigned int num = *num_iovec; 2565 2566 iov[num].iov_base = create_durable_v2_buf(oparms); 2567 if (iov[num].iov_base == NULL) 2568 return -ENOMEM; 2569 iov[num].iov_len = sizeof(struct create_durable_req_v2); 2570 *num_iovec = num + 1; 2571 return 0; 2572 } 2573 2574 static int 2575 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec, 2576 struct cifs_open_parms *oparms) 2577 { 2578 unsigned int num = *num_iovec; 2579 2580 /* indicate that we don't need to relock the file */ 2581 oparms->reconnect = false; 2582 2583 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid); 2584 if (iov[num].iov_base == NULL) 2585 return -ENOMEM; 2586 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2); 2587 *num_iovec = num + 1; 2588 return 0; 2589 } 2590 2591 static int 2592 add_durable_context(struct kvec *iov, unsigned int *num_iovec, 2593 struct cifs_open_parms *oparms, bool use_persistent) 2594 { 2595 unsigned int num = *num_iovec; 2596 2597 if (use_persistent) { 2598 if (oparms->reconnect) 2599 return add_durable_reconnect_v2_context(iov, num_iovec, 2600 oparms); 2601 else 2602 return add_durable_v2_context(iov, num_iovec, oparms); 2603 } 2604 2605 if (oparms->reconnect) { 2606 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid); 2607 /* indicate that we don't need to relock the file */ 2608 oparms->reconnect = false; 2609 } else 2610 iov[num].iov_base = create_durable_buf(); 2611 if (iov[num].iov_base == NULL) 2612 return -ENOMEM; 2613 iov[num].iov_len = sizeof(create_durable_req_t); 2614 *num_iovec = num + 1; 2615 return 0; 2616 } 2617 2618 /* See MS-SMB2 2.2.13.2.7 */ 2619 static struct crt_twarp_ctxt * 2620 create_twarp_buf(__u64 timewarp) 2621 { 2622 struct crt_twarp_ctxt *buf; 2623 2624 buf = kzalloc_obj(struct crt_twarp_ctxt); 2625 if (!buf) 2626 return NULL; 2627 2628 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2629 (struct crt_twarp_ctxt, Timestamp)); 2630 buf->ccontext.DataLength = cpu_to_le32(8); 2631 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2632 (struct crt_twarp_ctxt, Name)); 2633 buf->ccontext.NameLength = cpu_to_le16(4); 2634 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */ 2635 buf->Name[0] = 'T'; 2636 buf->Name[1] = 'W'; 2637 buf->Name[2] = 'r'; 2638 buf->Name[3] = 'p'; 2639 buf->Timestamp = cpu_to_le64(timewarp); 2640 return buf; 2641 } 2642 2643 /* See MS-SMB2 2.2.13.2.7 */ 2644 static int 2645 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp) 2646 { 2647 unsigned int num = *num_iovec; 2648 2649 iov[num].iov_base = create_twarp_buf(timewarp); 2650 if (iov[num].iov_base == NULL) 2651 return -ENOMEM; 2652 iov[num].iov_len = sizeof(struct crt_twarp_ctxt); 2653 *num_iovec = num + 1; 2654 return 0; 2655 } 2656 2657 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */ 2658 static void setup_owner_group_sids(char *buf) 2659 { 2660 struct owner_group_sids *sids = (struct owner_group_sids *)buf; 2661 2662 /* Populate the user ownership fields S-1-5-88-1 */ 2663 sids->owner.Revision = 1; 2664 sids->owner.NumAuth = 3; 2665 sids->owner.Authority[5] = 5; 2666 sids->owner.SubAuthorities[0] = cpu_to_le32(88); 2667 sids->owner.SubAuthorities[1] = cpu_to_le32(1); 2668 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val); 2669 2670 /* Populate the group ownership fields S-1-5-88-2 */ 2671 sids->group.Revision = 1; 2672 sids->group.NumAuth = 3; 2673 sids->group.Authority[5] = 5; 2674 sids->group.SubAuthorities[0] = cpu_to_le32(88); 2675 sids->group.SubAuthorities[1] = cpu_to_le32(2); 2676 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val); 2677 2678 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val); 2679 } 2680 2681 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */ 2682 static struct crt_sd_ctxt * 2683 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len) 2684 { 2685 struct crt_sd_ctxt *buf; 2686 __u8 *ptr, *aclptr; 2687 unsigned int acelen, acl_size, ace_count; 2688 unsigned int owner_offset = 0; 2689 unsigned int group_offset = 0; 2690 struct smb3_acl acl = {}; 2691 2692 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct smb_ace) * 4), 8); 2693 2694 if (set_owner) { 2695 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */ 2696 *len += sizeof(struct owner_group_sids); 2697 } 2698 2699 buf = kzalloc(*len, GFP_KERNEL); 2700 if (buf == NULL) 2701 return buf; 2702 2703 ptr = (__u8 *)&buf[1]; 2704 if (set_owner) { 2705 /* offset fields are from beginning of security descriptor not of create context */ 2706 owner_offset = ptr - (__u8 *)&buf->sd; 2707 buf->sd.OffsetOwner = cpu_to_le32(owner_offset); 2708 group_offset = owner_offset + offsetof(struct owner_group_sids, group); 2709 buf->sd.OffsetGroup = cpu_to_le32(group_offset); 2710 2711 setup_owner_group_sids(ptr); 2712 ptr += sizeof(struct owner_group_sids); 2713 } else { 2714 buf->sd.OffsetOwner = 0; 2715 buf->sd.OffsetGroup = 0; 2716 } 2717 2718 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd)); 2719 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name)); 2720 buf->ccontext.NameLength = cpu_to_le16(4); 2721 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */ 2722 buf->Name[0] = 'S'; 2723 buf->Name[1] = 'e'; 2724 buf->Name[2] = 'c'; 2725 buf->Name[3] = 'D'; 2726 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */ 2727 2728 /* 2729 * ACL is "self relative" ie ACL is stored in contiguous block of memory 2730 * and "DP" ie the DACL is present 2731 */ 2732 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP); 2733 2734 /* offset owner, group and Sbz1 and SACL are all zero */ 2735 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd); 2736 /* Ship the ACL for now. we will copy it into buf later. */ 2737 aclptr = ptr; 2738 ptr += sizeof(struct smb3_acl); 2739 2740 /* create one ACE to hold the mode embedded in reserved special SID */ 2741 acelen = setup_special_mode_ACE((struct smb_ace *)ptr, false, (__u64)mode); 2742 ptr += acelen; 2743 acl_size = acelen + sizeof(struct smb3_acl); 2744 ace_count = 1; 2745 2746 if (set_owner) { 2747 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */ 2748 acelen = setup_special_user_owner_ACE((struct smb_ace *)ptr); 2749 ptr += acelen; 2750 acl_size += acelen; 2751 ace_count += 1; 2752 } 2753 2754 /* and one more ACE to allow access for authenticated users */ 2755 acelen = setup_authusers_ACE((struct smb_ace *)ptr); 2756 ptr += acelen; 2757 acl_size += acelen; 2758 ace_count += 1; 2759 2760 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */ 2761 acl.AclSize = cpu_to_le16(acl_size); 2762 acl.AceCount = cpu_to_le16(ace_count); 2763 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */ 2764 memcpy(aclptr, &acl, sizeof(struct smb3_acl)); 2765 2766 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd); 2767 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8); 2768 2769 return buf; 2770 } 2771 2772 static int 2773 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner) 2774 { 2775 unsigned int num = *num_iovec; 2776 unsigned int len = 0; 2777 2778 iov[num].iov_base = create_sd_buf(mode, set_owner, &len); 2779 if (iov[num].iov_base == NULL) 2780 return -ENOMEM; 2781 iov[num].iov_len = len; 2782 *num_iovec = num + 1; 2783 return 0; 2784 } 2785 2786 static struct crt_query_id_ctxt * 2787 create_query_id_buf(void) 2788 { 2789 struct crt_query_id_ctxt *buf; 2790 2791 buf = kzalloc_obj(struct crt_query_id_ctxt); 2792 if (!buf) 2793 return NULL; 2794 2795 buf->ccontext.DataOffset = cpu_to_le16(0); 2796 buf->ccontext.DataLength = cpu_to_le32(0); 2797 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2798 (struct crt_query_id_ctxt, Name)); 2799 buf->ccontext.NameLength = cpu_to_le16(4); 2800 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */ 2801 buf->Name[0] = 'Q'; 2802 buf->Name[1] = 'F'; 2803 buf->Name[2] = 'i'; 2804 buf->Name[3] = 'd'; 2805 return buf; 2806 } 2807 2808 /* See MS-SMB2 2.2.13.2.9 */ 2809 static int 2810 add_query_id_context(struct kvec *iov, unsigned int *num_iovec) 2811 { 2812 unsigned int num = *num_iovec; 2813 2814 iov[num].iov_base = create_query_id_buf(); 2815 if (iov[num].iov_base == NULL) 2816 return -ENOMEM; 2817 iov[num].iov_len = sizeof(struct crt_query_id_ctxt); 2818 *num_iovec = num + 1; 2819 return 0; 2820 } 2821 2822 static void add_ea_context(struct cifs_open_parms *oparms, 2823 struct kvec *rq_iov, unsigned int *num_iovs) 2824 { 2825 struct kvec *iov = oparms->ea_cctx; 2826 2827 if (iov && iov->iov_base && iov->iov_len) { 2828 rq_iov[(*num_iovs)++] = *iov; 2829 memset(iov, 0, sizeof(*iov)); 2830 } 2831 } 2832 2833 static int 2834 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len, 2835 const char *treename, const __le16 *path) 2836 { 2837 int treename_len, path_len; 2838 struct nls_table *cp; 2839 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)}; 2840 2841 /* 2842 * skip leading "\\" 2843 */ 2844 treename_len = strlen(treename); 2845 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\')) 2846 return -EINVAL; 2847 2848 treename += 2; 2849 treename_len -= 2; 2850 2851 path_len = UniStrnlen((wchar_t *)path, PATH_MAX); 2852 2853 /* make room for one path separator only if @path isn't empty */ 2854 *out_len = treename_len + (path[0] ? 1 : 0) + path_len; 2855 2856 /* 2857 * final path needs to be 8-byte aligned as specified in 2858 * MS-SMB2 2.2.13 SMB2 CREATE Request. 2859 */ 2860 *out_size = round_up(*out_len * sizeof(__le16), 8); 2861 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL); 2862 if (!*out_path) 2863 return -ENOMEM; 2864 2865 cp = load_nls_default(); 2866 cifs_strtoUTF16(*out_path, treename, treename_len, cp); 2867 2868 /* Do not append the separator if the path is empty */ 2869 if (path[0] != cpu_to_le16(0x0000)) { 2870 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep); 2871 UniStrcat((wchar_t *)*out_path, (wchar_t *)path); 2872 } 2873 2874 unload_nls(cp); 2875 2876 return 0; 2877 } 2878 2879 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode, 2880 umode_t mode, struct cifs_tcon *tcon, 2881 const char *full_path, 2882 struct cifs_sb_info *cifs_sb) 2883 { 2884 struct smb_rqst rqst; 2885 struct smb2_create_req *req; 2886 struct smb2_create_rsp *rsp = NULL; 2887 struct cifs_ses *ses = tcon->ses; 2888 struct kvec iov[3]; /* make sure at least one for each open context */ 2889 struct kvec rsp_iov = {NULL, 0}; 2890 int resp_buftype; 2891 int uni_path_len; 2892 __le16 *copy_path = NULL; 2893 int copy_size; 2894 int rc = 0; 2895 unsigned int n_iov = 2; 2896 __u32 file_attributes = 0; 2897 char *pc_buf = NULL; 2898 int flags = 0; 2899 unsigned int total_len; 2900 __le16 *utf16_path = NULL; 2901 struct TCP_Server_Info *server; 2902 int retries = 0, cur_sleep = 0; 2903 2904 replay_again: 2905 /* reinitialize for possible replay */ 2906 pc_buf = NULL; 2907 flags = 0; 2908 n_iov = 2; 2909 server = cifs_pick_channel(ses); 2910 2911 cifs_dbg(FYI, "mkdir\n"); 2912 2913 /* resource #1: path allocation */ 2914 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb); 2915 if (!utf16_path) 2916 return -ENOMEM; 2917 2918 if (!ses || !server) { 2919 rc = smb_EIO(smb_eio_trace_null_pointers); 2920 goto err_free_path; 2921 } 2922 2923 /* resource #2: request */ 2924 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server, 2925 (void **) &req, &total_len); 2926 if (rc) 2927 goto err_free_path; 2928 2929 2930 if (smb3_encryption_required(tcon)) 2931 flags |= CIFS_TRANSFORM_REQ; 2932 2933 req->ImpersonationLevel = IL_IMPERSONATION; 2934 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES); 2935 /* File attributes ignored on open (used in create though) */ 2936 req->FileAttributes = cpu_to_le32(file_attributes); 2937 req->ShareAccess = FILE_SHARE_ALL_LE; 2938 req->CreateDisposition = cpu_to_le32(FILE_CREATE); 2939 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE); 2940 2941 iov[0].iov_base = (char *)req; 2942 /* -1 since last byte is buf[0] which is sent below (path) */ 2943 iov[0].iov_len = total_len - 1; 2944 2945 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); 2946 2947 /* [MS-SMB2] 2.2.13 NameOffset: 2948 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of 2949 * the SMB2 header, the file name includes a prefix that will 2950 * be processed during DFS name normalization as specified in 2951 * section 3.3.5.9. Otherwise, the file name is relative to 2952 * the share that is identified by the TreeId in the SMB2 2953 * header. 2954 */ 2955 if (tcon->share_flags & SHI1005_FLAGS_DFS) { 2956 int name_len; 2957 2958 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; 2959 rc = alloc_path_with_tree_prefix(©_path, ©_size, 2960 &name_len, 2961 tcon->tree_name, utf16_path); 2962 if (rc) 2963 goto err_free_req; 2964 2965 req->NameLength = cpu_to_le16(name_len * 2); 2966 uni_path_len = copy_size; 2967 /* free before overwriting resource */ 2968 kfree(utf16_path); 2969 utf16_path = copy_path; 2970 } else { 2971 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2; 2972 /* MUST set path len (NameLength) to 0 opening root of share */ 2973 req->NameLength = cpu_to_le16(uni_path_len - 2); 2974 if (uni_path_len % 8 != 0) { 2975 copy_size = roundup(uni_path_len, 8); 2976 copy_path = kzalloc(copy_size, GFP_KERNEL); 2977 if (!copy_path) { 2978 rc = -ENOMEM; 2979 goto err_free_req; 2980 } 2981 memcpy((char *)copy_path, (const char *)utf16_path, 2982 uni_path_len); 2983 uni_path_len = copy_size; 2984 /* free before overwriting resource */ 2985 kfree(utf16_path); 2986 utf16_path = copy_path; 2987 } 2988 } 2989 2990 iov[1].iov_len = uni_path_len; 2991 iov[1].iov_base = utf16_path; 2992 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE; 2993 2994 if (tcon->posix_extensions) { 2995 /* resource #3: posix buf */ 2996 rc = add_posix_context(iov, &n_iov, mode); 2997 if (rc) 2998 goto err_free_req; 2999 req->CreateContextsOffset = cpu_to_le32( 3000 sizeof(struct smb2_create_req) + 3001 iov[1].iov_len); 3002 le32_add_cpu(&req->CreateContextsLength, iov[n_iov-1].iov_len); 3003 pc_buf = iov[n_iov-1].iov_base; 3004 } 3005 3006 3007 memset(&rqst, 0, sizeof(struct smb_rqst)); 3008 rqst.rq_iov = iov; 3009 rqst.rq_nvec = n_iov; 3010 3011 /* no need to inc num_remote_opens because we close it just below */ 3012 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE, 3013 FILE_WRITE_ATTRIBUTES); 3014 3015 if (retries) { 3016 /* Back-off before retry */ 3017 if (cur_sleep) 3018 msleep(cur_sleep); 3019 smb2_set_replay(server, &rqst); 3020 } 3021 3022 /* resource #4: response buffer */ 3023 rc = cifs_send_recv(xid, ses, server, 3024 &rqst, &resp_buftype, flags, &rsp_iov); 3025 if (rc) { 3026 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); 3027 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid, 3028 CREATE_NOT_FILE, 3029 FILE_WRITE_ATTRIBUTES, rc); 3030 goto err_free_rsp_buf; 3031 } 3032 3033 /* 3034 * Although unlikely to be possible for rsp to be null and rc not set, 3035 * adding check below is slightly safer long term (and quiets Coverity 3036 * warning) 3037 */ 3038 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; 3039 if (rsp == NULL) { 3040 rc = smb_EIO(smb_eio_trace_mkdir_no_rsp); 3041 kfree(pc_buf); 3042 goto err_free_req; 3043 } 3044 3045 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid, 3046 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES, 3047 rsp->OplockLevel); 3048 3049 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId); 3050 3051 /* Eventually save off posix specific response info and timestamps */ 3052 3053 err_free_rsp_buf: 3054 free_rsp_buf(resp_buftype, rsp_iov.iov_base); 3055 kfree(pc_buf); 3056 err_free_req: 3057 cifs_small_buf_release(req); 3058 err_free_path: 3059 kfree(utf16_path); 3060 3061 if (is_replayable_error(rc) && 3062 smb2_should_replay(tcon, &retries, &cur_sleep)) 3063 goto replay_again; 3064 3065 return rc; 3066 } 3067 3068 int 3069 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, 3070 struct smb_rqst *rqst, __u8 *oplock, 3071 struct cifs_open_parms *oparms, __le16 *path) 3072 { 3073 struct smb2_create_req *req; 3074 unsigned int n_iov = 2; 3075 __u32 file_attributes = 0; 3076 int copy_size; 3077 int uni_path_len; 3078 unsigned int total_len; 3079 struct kvec *iov = rqst->rq_iov; 3080 __le16 *copy_path; 3081 int rc; 3082 3083 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server, 3084 (void **) &req, &total_len); 3085 if (rc) 3086 return rc; 3087 3088 iov[0].iov_base = (char *)req; 3089 /* -1 since last byte is buf[0] which is sent below (path) */ 3090 iov[0].iov_len = total_len - 1; 3091 3092 if (oparms->create_options & CREATE_OPTION_READONLY) 3093 file_attributes |= ATTR_READONLY; 3094 if (oparms->create_options & CREATE_OPTION_SPECIAL) 3095 file_attributes |= ATTR_SYSTEM; 3096 3097 req->ImpersonationLevel = IL_IMPERSONATION; 3098 req->DesiredAccess = cpu_to_le32(oparms->desired_access); 3099 /* File attributes ignored on open (used in create though) */ 3100 req->FileAttributes = cpu_to_le32(file_attributes); 3101 req->ShareAccess = FILE_SHARE_ALL_LE; 3102 3103 req->CreateDisposition = cpu_to_le32(oparms->disposition); 3104 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK); 3105 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)); 3106 3107 /* [MS-SMB2] 2.2.13 NameOffset: 3108 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of 3109 * the SMB2 header, the file name includes a prefix that will 3110 * be processed during DFS name normalization as specified in 3111 * section 3.3.5.9. Otherwise, the file name is relative to 3112 * the share that is identified by the TreeId in the SMB2 3113 * header. 3114 */ 3115 if (tcon->share_flags & SHI1005_FLAGS_DFS) { 3116 int name_len; 3117 3118 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; 3119 rc = alloc_path_with_tree_prefix(©_path, ©_size, 3120 &name_len, 3121 tcon->tree_name, path); 3122 if (rc) 3123 return rc; 3124 req->NameLength = cpu_to_le16(name_len * 2); 3125 uni_path_len = copy_size; 3126 path = copy_path; 3127 } else { 3128 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2; 3129 /* MUST set path len (NameLength) to 0 opening root of share */ 3130 req->NameLength = cpu_to_le16(uni_path_len - 2); 3131 copy_size = round_up(uni_path_len, 8); 3132 copy_path = kzalloc(copy_size, GFP_KERNEL); 3133 if (!copy_path) 3134 return -ENOMEM; 3135 memcpy((char *)copy_path, (const char *)path, 3136 uni_path_len); 3137 uni_path_len = copy_size; 3138 path = copy_path; 3139 } 3140 3141 iov[1].iov_len = uni_path_len; 3142 iov[1].iov_base = path; 3143 3144 if ((!server->oplocks) || (tcon->no_lease)) 3145 *oplock = SMB2_OPLOCK_LEVEL_NONE; 3146 3147 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) || 3148 *oplock == SMB2_OPLOCK_LEVEL_NONE) 3149 req->RequestedOplockLevel = *oplock; 3150 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) && 3151 (oparms->create_options & CREATE_NOT_FILE)) 3152 req->RequestedOplockLevel = *oplock; /* no srv lease support */ 3153 else { 3154 rc = add_lease_context(server, req, iov, &n_iov, 3155 oparms->fid->lease_key, oplock, 3156 oparms->fid->parent_lease_key, 3157 oparms->lease_flags); 3158 if (rc) 3159 return rc; 3160 } 3161 3162 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) { 3163 rc = add_durable_context(iov, &n_iov, oparms, 3164 tcon->use_persistent); 3165 if (rc) 3166 return rc; 3167 } 3168 3169 if (tcon->posix_extensions) { 3170 rc = add_posix_context(iov, &n_iov, oparms->mode); 3171 if (rc) 3172 return rc; 3173 } 3174 3175 if (tcon->snapshot_time) { 3176 cifs_dbg(FYI, "adding snapshot context\n"); 3177 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time); 3178 if (rc) 3179 return rc; 3180 } 3181 3182 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) { 3183 unsigned int sbflags = cifs_sb_flags(oparms->cifs_sb); 3184 bool set_mode; 3185 bool set_owner; 3186 3187 if ((sbflags & CIFS_MOUNT_MODE_FROM_SID) && 3188 oparms->mode != ACL_NO_MODE) { 3189 set_mode = true; 3190 } else { 3191 set_mode = false; 3192 oparms->mode = ACL_NO_MODE; 3193 } 3194 3195 set_owner = sbflags & CIFS_MOUNT_UID_FROM_ACL; 3196 if (set_owner | set_mode) { 3197 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode); 3198 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner); 3199 if (rc) 3200 return rc; 3201 } 3202 } 3203 3204 add_query_id_context(iov, &n_iov); 3205 add_ea_context(oparms, iov, &n_iov); 3206 3207 if (n_iov > 2) { 3208 /* 3209 * We have create contexts behind iov[1] (the file 3210 * name), point at them from the main create request 3211 */ 3212 req->CreateContextsOffset = cpu_to_le32( 3213 sizeof(struct smb2_create_req) + 3214 iov[1].iov_len); 3215 req->CreateContextsLength = 0; 3216 3217 for (unsigned int i = 2; i < (n_iov-1); i++) { 3218 struct kvec *v = &iov[i]; 3219 size_t len = v->iov_len; 3220 struct create_context *cctx = 3221 (struct create_context *)v->iov_base; 3222 3223 cctx->Next = cpu_to_le32(len); 3224 le32_add_cpu(&req->CreateContextsLength, len); 3225 } 3226 le32_add_cpu(&req->CreateContextsLength, 3227 iov[n_iov-1].iov_len); 3228 } 3229 3230 rqst->rq_nvec = n_iov; 3231 return 0; 3232 } 3233 3234 /* rq_iov[0] is the request and is released by cifs_small_buf_release(). 3235 * All other vectors are freed by kfree(). 3236 */ 3237 void 3238 SMB2_open_free(struct smb_rqst *rqst) 3239 { 3240 int i; 3241 3242 if (rqst && rqst->rq_iov) { 3243 cifs_small_buf_release(rqst->rq_iov[0].iov_base); 3244 for (i = 1; i < rqst->rq_nvec; i++) 3245 if (rqst->rq_iov[i].iov_base != smb2_padding) 3246 kfree(rqst->rq_iov[i].iov_base); 3247 } 3248 } 3249 3250 int 3251 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, 3252 __u8 *oplock, struct smb2_file_all_info *buf, 3253 struct create_posix_rsp *posix, 3254 struct kvec *err_iov, int *buftype) 3255 { 3256 struct smb_rqst rqst; 3257 struct smb2_create_rsp *rsp = NULL; 3258 struct cifs_tcon *tcon = oparms->tcon; 3259 struct cifs_ses *ses = tcon->ses; 3260 struct TCP_Server_Info *server; 3261 struct kvec iov[SMB2_CREATE_IOV_SIZE]; 3262 struct kvec rsp_iov = {NULL, 0}; 3263 int resp_buftype = CIFS_NO_BUFFER; 3264 int rc = 0; 3265 int flags = 0; 3266 int retries = 0, cur_sleep = 0; 3267 3268 replay_again: 3269 /* reinitialize for possible replay */ 3270 flags = 0; 3271 server = cifs_pick_channel(ses); 3272 oparms->replay = !!(retries); 3273 3274 cifs_dbg(FYI, "create/open\n"); 3275 if (!ses || !server) 3276 return smb_EIO(smb_eio_trace_null_pointers); 3277 3278 if (smb3_encryption_required(tcon)) 3279 flags |= CIFS_TRANSFORM_REQ; 3280 3281 memset(&rqst, 0, sizeof(struct smb_rqst)); 3282 memset(&iov, 0, sizeof(iov)); 3283 rqst.rq_iov = iov; 3284 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE; 3285 3286 rc = SMB2_open_init(tcon, server, 3287 &rqst, oplock, oparms, path); 3288 if (rc) 3289 goto creat_exit; 3290 3291 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path, 3292 oparms->create_options, oparms->desired_access); 3293 3294 if (retries) { 3295 /* Back-off before retry */ 3296 if (cur_sleep) 3297 msleep(cur_sleep); 3298 smb2_set_replay(server, &rqst); 3299 } 3300 3301 rc = cifs_send_recv(xid, ses, server, 3302 &rqst, &resp_buftype, flags, 3303 &rsp_iov); 3304 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; 3305 3306 if (rc != 0) { 3307 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); 3308 if (err_iov && rsp) { 3309 *err_iov = rsp_iov; 3310 *buftype = resp_buftype; 3311 resp_buftype = CIFS_NO_BUFFER; 3312 rsp = NULL; 3313 } 3314 trace_smb3_open_err(xid, tcon->tid, ses->Suid, 3315 oparms->create_options, oparms->desired_access, rc); 3316 if (rc == -EREMCHG) { 3317 pr_warn_once("server share %s deleted\n", 3318 tcon->tree_name); 3319 tcon->need_reconnect = true; 3320 } 3321 goto creat_exit; 3322 } else if (rsp == NULL) /* unlikely to happen, but safer to check */ 3323 goto creat_exit; 3324 3325 atomic_inc(&tcon->num_remote_opens); 3326 oparms->fid->persistent_fid = rsp->PersistentFileId; 3327 oparms->fid->volatile_fid = rsp->VolatileFileId; 3328 oparms->fid->access = oparms->desired_access; 3329 #ifdef CONFIG_CIFS_DEBUG2 3330 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId); 3331 #endif /* CIFS_DEBUG2 */ 3332 3333 if (buf) { 3334 buf->CreationTime = rsp->CreationTime; 3335 buf->LastAccessTime = rsp->LastAccessTime; 3336 buf->LastWriteTime = rsp->LastWriteTime; 3337 buf->ChangeTime = rsp->ChangeTime; 3338 buf->AllocationSize = rsp->AllocationSize; 3339 buf->EndOfFile = rsp->EndofFile; 3340 buf->Attributes = rsp->FileAttributes; 3341 buf->NumberOfLinks = cpu_to_le32(1); 3342 buf->DeletePending = 0; /* successful open = not delete pending */ 3343 } 3344 3345 3346 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch, 3347 oparms->fid->lease_key, oplock, buf, posix); 3348 3349 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid, 3350 oparms->create_options, oparms->desired_access, 3351 *oplock); 3352 creat_exit: 3353 SMB2_open_free(&rqst); 3354 free_rsp_buf(resp_buftype, rsp); 3355 3356 if (is_replayable_error(rc) && 3357 smb2_should_replay(tcon, &retries, &cur_sleep)) 3358 goto replay_again; 3359 3360 return rc; 3361 } 3362 3363 int 3364 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, 3365 struct smb_rqst *rqst, 3366 u64 persistent_fid, u64 volatile_fid, u32 opcode, 3367 char *in_data, u32 indatalen, 3368 __u32 max_response_size) 3369 { 3370 struct smb2_ioctl_req *req; 3371 struct kvec *iov = rqst->rq_iov; 3372 unsigned int total_len; 3373 int rc; 3374 char *in_data_buf; 3375 3376 rc = smb2_ioctl_req_init(opcode, tcon, server, 3377 (void **) &req, &total_len); 3378 if (rc) 3379 return rc; 3380 3381 if (indatalen) { 3382 /* 3383 * indatalen is usually small at a couple of bytes max, so 3384 * just allocate through generic pool 3385 */ 3386 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS); 3387 if (!in_data_buf) { 3388 cifs_small_buf_release(req); 3389 return -ENOMEM; 3390 } 3391 } 3392 3393 req->CtlCode = cpu_to_le32(opcode); 3394 req->PersistentFileId = persistent_fid; 3395 req->VolatileFileId = volatile_fid; 3396 3397 iov[0].iov_base = (char *)req; 3398 /* 3399 * If no input data, the size of ioctl struct in 3400 * protocol spec still includes a 1 byte data buffer, 3401 * but if input data passed to ioctl, we do not 3402 * want to double count this, so we do not send 3403 * the dummy one byte of data in iovec[0] if sending 3404 * input data (in iovec[1]). 3405 */ 3406 if (indatalen) { 3407 req->InputCount = cpu_to_le32(indatalen); 3408 /* do not set InputOffset if no input data */ 3409 req->InputOffset = 3410 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer)); 3411 rqst->rq_nvec = 2; 3412 iov[0].iov_len = total_len - 1; 3413 iov[1].iov_base = in_data_buf; 3414 iov[1].iov_len = indatalen; 3415 } else { 3416 rqst->rq_nvec = 1; 3417 iov[0].iov_len = total_len; 3418 } 3419 3420 req->OutputOffset = 0; 3421 req->OutputCount = 0; /* MBZ */ 3422 3423 /* 3424 * In most cases max_response_size is set to 16K (CIFSMaxBufSize) 3425 * We Could increase default MaxOutputResponse, but that could require 3426 * more credits. Windows typically sets this smaller, but for some 3427 * ioctls it may be useful to allow server to send more. No point 3428 * limiting what the server can send as long as fits in one credit 3429 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want 3430 * to increase this limit up in the future. 3431 * Note that for snapshot queries that servers like Azure expect that 3432 * the first query be minimal size (and just used to get the number/size 3433 * of previous versions) so response size must be specified as EXACTLY 3434 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple 3435 * of eight bytes. Currently that is the only case where we set max 3436 * response size smaller. 3437 */ 3438 req->MaxOutputResponse = cpu_to_le32(max_response_size); 3439 req->hdr.CreditCharge = 3440 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size), 3441 SMB2_MAX_BUFFER_SIZE)); 3442 /* always an FSCTL (for now) */ 3443 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL); 3444 3445 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */ 3446 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) 3447 req->hdr.Flags |= SMB2_FLAGS_SIGNED; 3448 3449 return 0; 3450 } 3451 3452 void 3453 SMB2_ioctl_free(struct smb_rqst *rqst) 3454 { 3455 int i; 3456 3457 if (rqst && rqst->rq_iov) { 3458 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ 3459 for (i = 1; i < rqst->rq_nvec; i++) 3460 if (rqst->rq_iov[i].iov_base != smb2_padding) 3461 kfree(rqst->rq_iov[i].iov_base); 3462 } 3463 } 3464 3465 3466 /* 3467 * SMB2 IOCTL is used for both IOCTLs and FSCTLs 3468 */ 3469 int 3470 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, 3471 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen, 3472 u32 max_out_data_len, char **out_data, 3473 u32 *plen /* returned data len */) 3474 { 3475 struct smb_rqst rqst; 3476 struct smb2_ioctl_rsp *rsp = NULL; 3477 struct cifs_ses *ses; 3478 struct TCP_Server_Info *server; 3479 struct kvec iov[SMB2_IOCTL_IOV_SIZE]; 3480 struct kvec rsp_iov = {NULL, 0}; 3481 int resp_buftype = CIFS_NO_BUFFER; 3482 int rc = 0; 3483 int flags = 0; 3484 int retries = 0, cur_sleep = 0; 3485 3486 if (!tcon) 3487 return smb_EIO(smb_eio_trace_null_pointers); 3488 3489 ses = tcon->ses; 3490 if (!ses) 3491 return smb_EIO(smb_eio_trace_null_pointers); 3492 3493 replay_again: 3494 /* reinitialize for possible replay */ 3495 flags = 0; 3496 server = cifs_pick_channel(ses); 3497 3498 if (!server) 3499 return smb_EIO(smb_eio_trace_null_pointers); 3500 3501 cifs_dbg(FYI, "SMB2 IOCTL\n"); 3502 3503 if (out_data != NULL) 3504 *out_data = NULL; 3505 3506 /* zero out returned data len, in case of error */ 3507 if (plen) 3508 *plen = 0; 3509 3510 if (smb3_encryption_required(tcon)) 3511 flags |= CIFS_TRANSFORM_REQ; 3512 3513 memset(&rqst, 0, sizeof(struct smb_rqst)); 3514 memset(&iov, 0, sizeof(iov)); 3515 rqst.rq_iov = iov; 3516 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE; 3517 3518 rc = SMB2_ioctl_init(tcon, server, 3519 &rqst, persistent_fid, volatile_fid, opcode, 3520 in_data, indatalen, max_out_data_len); 3521 if (rc) 3522 goto ioctl_exit; 3523 3524 if (retries) { 3525 /* Back-off before retry */ 3526 if (cur_sleep) 3527 msleep(cur_sleep); 3528 smb2_set_replay(server, &rqst); 3529 } 3530 3531 rc = cifs_send_recv(xid, ses, server, 3532 &rqst, &resp_buftype, flags, 3533 &rsp_iov); 3534 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base; 3535 3536 if (rc != 0) 3537 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid, 3538 ses->Suid, 0, opcode, rc); 3539 3540 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) { 3541 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); 3542 goto ioctl_exit; 3543 } else if (rc == -EINVAL) { 3544 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) && 3545 (opcode != FSCTL_SRV_COPYCHUNK)) { 3546 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); 3547 goto ioctl_exit; 3548 } 3549 } else if (rc == -E2BIG) { 3550 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) { 3551 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); 3552 goto ioctl_exit; 3553 } 3554 } 3555 3556 /* check if caller wants to look at return data or just return rc */ 3557 if ((plen == NULL) || (out_data == NULL)) 3558 goto ioctl_exit; 3559 3560 /* 3561 * Although unlikely to be possible for rsp to be null and rc not set, 3562 * adding check below is slightly safer long term (and quiets Coverity 3563 * warning) 3564 */ 3565 if (rsp == NULL) { 3566 rc = smb_EIO(smb_eio_trace_ioctl_no_rsp); 3567 goto ioctl_exit; 3568 } 3569 3570 *plen = le32_to_cpu(rsp->OutputCount); 3571 3572 /* We check for obvious errors in the output buffer length and offset */ 3573 if (*plen == 0) 3574 goto ioctl_exit; /* server returned no data */ 3575 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) { 3576 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen); 3577 rc = smb_EIO2(smb_eio_trace_ioctl_data_len, *plen, rsp_iov.iov_len); 3578 *plen = 0; 3579 goto ioctl_exit; 3580 } 3581 3582 u32 outoff = le32_to_cpu(rsp->OutputOffset); 3583 3584 if (rsp_iov.iov_len - *plen < outoff) { 3585 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", 3586 *plen, outoff); 3587 rc = smb_EIO2(smb_eio_trace_ioctl_out_off, rsp_iov.iov_len - *plen, outoff); 3588 *plen = 0; 3589 goto ioctl_exit; 3590 } 3591 3592 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset), 3593 *plen, GFP_KERNEL); 3594 if (*out_data == NULL) { 3595 rc = -ENOMEM; 3596 goto ioctl_exit; 3597 } 3598 3599 ioctl_exit: 3600 SMB2_ioctl_free(&rqst); 3601 free_rsp_buf(resp_buftype, rsp); 3602 3603 if (is_replayable_error(rc) && 3604 smb2_should_replay(tcon, &retries, &cur_sleep)) 3605 goto replay_again; 3606 3607 return rc; 3608 } 3609 3610 /* 3611 * Individual callers to ioctl worker function follow 3612 */ 3613 3614 int 3615 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon, 3616 u64 persistent_fid, u64 volatile_fid) 3617 { 3618 int rc; 3619 struct compress_ioctl fsctl_input; 3620 char *ret_data = NULL; 3621 3622 fsctl_input.CompressionState = 3623 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); 3624 3625 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid, 3626 FSCTL_SET_COMPRESSION, 3627 (char *)&fsctl_input /* data input */, 3628 2 /* in data len */, CIFSMaxBufSize /* max out data */, 3629 &ret_data /* out data */, NULL); 3630 3631 cifs_dbg(FYI, "set compression rc %d\n", rc); 3632 3633 return rc; 3634 } 3635 3636 int 3637 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, 3638 struct smb_rqst *rqst, 3639 u64 persistent_fid, u64 volatile_fid, bool query_attrs) 3640 { 3641 struct smb2_close_req *req; 3642 struct kvec *iov = rqst->rq_iov; 3643 unsigned int total_len; 3644 int rc; 3645 3646 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server, 3647 (void **) &req, &total_len); 3648 if (rc) 3649 return rc; 3650 3651 req->PersistentFileId = persistent_fid; 3652 req->VolatileFileId = volatile_fid; 3653 if (query_attrs) 3654 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; 3655 else 3656 req->Flags = 0; 3657 iov[0].iov_base = (char *)req; 3658 iov[0].iov_len = total_len; 3659 3660 return 0; 3661 } 3662 3663 void 3664 SMB2_close_free(struct smb_rqst *rqst) 3665 { 3666 if (rqst && rqst->rq_iov) 3667 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ 3668 } 3669 3670 int 3671 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, 3672 u64 persistent_fid, u64 volatile_fid, 3673 struct smb2_file_network_open_info *pbuf) 3674 { 3675 struct smb_rqst rqst; 3676 struct smb2_close_rsp *rsp = NULL; 3677 struct cifs_ses *ses = tcon->ses; 3678 struct TCP_Server_Info *server; 3679 struct kvec iov[1]; 3680 struct kvec rsp_iov; 3681 int resp_buftype = CIFS_NO_BUFFER; 3682 int rc = 0; 3683 int flags = 0; 3684 bool query_attrs = false; 3685 int retries = 0, cur_sleep = 0; 3686 3687 replay_again: 3688 /* reinitialize for possible replay */ 3689 flags = 0; 3690 query_attrs = false; 3691 server = cifs_pick_channel(ses); 3692 3693 cifs_dbg(FYI, "Close\n"); 3694 3695 if (!ses || !server) 3696 return smb_EIO(smb_eio_trace_null_pointers); 3697 3698 if (smb3_encryption_required(tcon)) 3699 flags |= CIFS_TRANSFORM_REQ; 3700 3701 memset(&rqst, 0, sizeof(struct smb_rqst)); 3702 memset(&iov, 0, sizeof(iov)); 3703 rqst.rq_iov = iov; 3704 rqst.rq_nvec = 1; 3705 3706 /* check if need to ask server to return timestamps in close response */ 3707 if (pbuf) 3708 query_attrs = true; 3709 3710 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid); 3711 rc = SMB2_close_init(tcon, server, 3712 &rqst, persistent_fid, volatile_fid, 3713 query_attrs); 3714 if (rc) 3715 goto close_exit; 3716 3717 if (retries) { 3718 /* Back-off before retry */ 3719 if (cur_sleep) 3720 msleep(cur_sleep); 3721 smb2_set_replay(server, &rqst); 3722 } 3723 3724 rc = cifs_send_recv(xid, ses, server, 3725 &rqst, &resp_buftype, flags, &rsp_iov); 3726 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base; 3727 3728 if (rc != 0) { 3729 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE); 3730 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid, 3731 rc); 3732 goto close_exit; 3733 } else { 3734 trace_smb3_close_done(xid, persistent_fid, tcon->tid, 3735 ses->Suid); 3736 if (pbuf) 3737 memcpy(&pbuf->network_open_info, 3738 &rsp->network_open_info, 3739 sizeof(pbuf->network_open_info)); 3740 atomic_dec(&tcon->num_remote_opens); 3741 } 3742 3743 close_exit: 3744 SMB2_close_free(&rqst); 3745 free_rsp_buf(resp_buftype, rsp); 3746 3747 /* retry close in a worker thread if this one is interrupted */ 3748 if (is_interrupt_error(rc)) { 3749 int tmp_rc; 3750 3751 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid, 3752 volatile_fid); 3753 if (tmp_rc) 3754 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n", 3755 persistent_fid, tmp_rc); 3756 } 3757 3758 if (is_replayable_error(rc) && 3759 smb2_should_replay(tcon, &retries, &cur_sleep)) 3760 goto replay_again; 3761 3762 return rc; 3763 } 3764 3765 int 3766 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, 3767 u64 persistent_fid, u64 volatile_fid) 3768 { 3769 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL); 3770 } 3771 3772 int 3773 smb2_validate_iov(unsigned int offset, unsigned int buffer_length, 3774 struct kvec *iov, unsigned int min_buf_size) 3775 { 3776 unsigned int smb_len = iov->iov_len; 3777 char *end_of_smb = smb_len + (char *)iov->iov_base; 3778 char *begin_of_buf = offset + (char *)iov->iov_base; 3779 char *end_of_buf = begin_of_buf + buffer_length; 3780 3781 3782 if (buffer_length < min_buf_size) { 3783 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n", 3784 buffer_length, min_buf_size); 3785 return -EINVAL; 3786 } 3787 3788 /* check if beyond RFC1001 maximum length */ 3789 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) { 3790 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n", 3791 buffer_length, smb_len); 3792 return -EINVAL; 3793 } 3794 3795 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) { 3796 cifs_dbg(VFS, "Invalid server response, bad offset to data\n"); 3797 return -EINVAL; 3798 } 3799 3800 return 0; 3801 } 3802 3803 /* 3804 * If SMB buffer fields are valid, copy into temporary buffer to hold result. 3805 * Caller must free buffer. 3806 */ 3807 int 3808 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length, 3809 struct kvec *iov, unsigned int minbufsize, 3810 char *data) 3811 { 3812 char *begin_of_buf = offset + (char *)iov->iov_base; 3813 int rc; 3814 3815 if (!data) 3816 return -EINVAL; 3817 3818 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize); 3819 if (rc) 3820 return rc; 3821 3822 memcpy(data, begin_of_buf, minbufsize); 3823 3824 return 0; 3825 } 3826 3827 int 3828 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, 3829 struct smb_rqst *rqst, 3830 u64 persistent_fid, u64 volatile_fid, 3831 u8 info_class, u8 info_type, u32 additional_info, 3832 size_t output_len, size_t input_len, void *input) 3833 { 3834 struct smb2_query_info_req *req; 3835 struct kvec *iov = rqst->rq_iov; 3836 unsigned int total_len; 3837 size_t len; 3838 int rc; 3839 3840 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) || 3841 len > CIFSMaxBufSize)) 3842 return -EINVAL; 3843 3844 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server, 3845 (void **) &req, &total_len); 3846 if (rc) 3847 return rc; 3848 3849 req->InfoType = info_type; 3850 req->FileInfoClass = info_class; 3851 req->PersistentFileId = persistent_fid; 3852 req->VolatileFileId = volatile_fid; 3853 req->AdditionalInformation = cpu_to_le32(additional_info); 3854 3855 req->OutputBufferLength = cpu_to_le32(output_len); 3856 if (input_len) { 3857 req->InputBufferLength = cpu_to_le32(input_len); 3858 /* total_len for smb query request never close to le16 max */ 3859 req->InputBufferOffset = cpu_to_le16(total_len - 1); 3860 memcpy(req->Buffer, input, input_len); 3861 } 3862 3863 iov[0].iov_base = (char *)req; 3864 /* 1 for Buffer */ 3865 iov[0].iov_len = len; 3866 return 0; 3867 } 3868 3869 void 3870 SMB2_query_info_free(struct smb_rqst *rqst) 3871 { 3872 if (rqst && rqst->rq_iov) 3873 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */ 3874 } 3875 3876 static int 3877 query_info(const unsigned int xid, struct cifs_tcon *tcon, 3878 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type, 3879 u32 additional_info, size_t output_len, size_t min_len, void **data, 3880 u32 *dlen) 3881 { 3882 struct smb_rqst rqst; 3883 struct smb2_query_info_rsp *rsp = NULL; 3884 struct kvec iov[1]; 3885 struct kvec rsp_iov; 3886 int rc = 0; 3887 int resp_buftype = CIFS_NO_BUFFER; 3888 struct cifs_ses *ses = tcon->ses; 3889 struct TCP_Server_Info *server; 3890 int flags = 0; 3891 bool allocated = false; 3892 int retries = 0, cur_sleep = 0; 3893 3894 cifs_dbg(FYI, "Query Info\n"); 3895 3896 if (!ses) 3897 return smb_EIO(smb_eio_trace_null_pointers); 3898 3899 replay_again: 3900 /* reinitialize for possible replay */ 3901 flags = 0; 3902 allocated = false; 3903 server = cifs_pick_channel(ses); 3904 3905 if (!server) 3906 return smb_EIO(smb_eio_trace_null_pointers); 3907 3908 if (smb3_encryption_required(tcon)) 3909 flags |= CIFS_TRANSFORM_REQ; 3910 3911 memset(&rqst, 0, sizeof(struct smb_rqst)); 3912 memset(&iov, 0, sizeof(iov)); 3913 rqst.rq_iov = iov; 3914 rqst.rq_nvec = 1; 3915 3916 rc = SMB2_query_info_init(tcon, server, 3917 &rqst, persistent_fid, volatile_fid, 3918 info_class, info_type, additional_info, 3919 output_len, 0, NULL); 3920 if (rc) 3921 goto qinf_exit; 3922 3923 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid, 3924 ses->Suid, info_class, (__u32)info_type); 3925 3926 if (retries) { 3927 /* Back-off before retry */ 3928 if (cur_sleep) 3929 msleep(cur_sleep); 3930 smb2_set_replay(server, &rqst); 3931 } 3932 3933 rc = cifs_send_recv(xid, ses, server, 3934 &rqst, &resp_buftype, flags, &rsp_iov); 3935 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; 3936 3937 if (rc) { 3938 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); 3939 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid, 3940 ses->Suid, info_class, (__u32)info_type, rc); 3941 goto qinf_exit; 3942 } 3943 3944 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid, 3945 ses->Suid, info_class, (__u32)info_type); 3946 3947 if (dlen) { 3948 *dlen = le32_to_cpu(rsp->OutputBufferLength); 3949 if (!*data) { 3950 *data = kmalloc(*dlen, GFP_KERNEL); 3951 if (!*data) { 3952 cifs_tcon_dbg(VFS, 3953 "Error %d allocating memory for acl\n", 3954 rc); 3955 *dlen = 0; 3956 rc = -ENOMEM; 3957 goto qinf_exit; 3958 } 3959 allocated = true; 3960 } 3961 } 3962 3963 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset), 3964 le32_to_cpu(rsp->OutputBufferLength), 3965 &rsp_iov, dlen ? *dlen : min_len, *data); 3966 if (rc && allocated) { 3967 kfree(*data); 3968 *data = NULL; 3969 *dlen = 0; 3970 } 3971 3972 qinf_exit: 3973 SMB2_query_info_free(&rqst); 3974 free_rsp_buf(resp_buftype, rsp); 3975 3976 if (is_replayable_error(rc) && 3977 smb2_should_replay(tcon, &retries, &cur_sleep)) 3978 goto replay_again; 3979 3980 return rc; 3981 } 3982 3983 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon, 3984 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data) 3985 { 3986 return query_info(xid, tcon, persistent_fid, volatile_fid, 3987 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0, 3988 sizeof(struct smb2_file_all_info) + PATH_MAX * 2, 3989 sizeof(struct smb2_file_all_info), (void **)&data, 3990 NULL); 3991 } 3992 3993 int 3994 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon, 3995 u64 persistent_fid, u64 volatile_fid, 3996 void **data, u32 *plen, u32 extra_info) 3997 { 3998 *plen = 0; 3999 4000 return query_info(xid, tcon, persistent_fid, volatile_fid, 4001 0, SMB2_O_INFO_SECURITY, extra_info, 4002 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen); 4003 } 4004 4005 int 4006 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon, 4007 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid) 4008 { 4009 return query_info(xid, tcon, persistent_fid, volatile_fid, 4010 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0, 4011 sizeof(struct smb2_file_internal_info), 4012 sizeof(struct smb2_file_internal_info), 4013 (void **)&uniqueid, NULL); 4014 } 4015 4016 /* 4017 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory 4018 * See MS-SMB2 2.2.35 and 2.2.36 4019 */ 4020 4021 static int 4022 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst, 4023 struct cifs_tcon *tcon, struct TCP_Server_Info *server, 4024 u64 persistent_fid, u64 volatile_fid, 4025 u32 completion_filter, bool watch_tree) 4026 { 4027 struct smb2_change_notify_req *req; 4028 struct kvec *iov = rqst->rq_iov; 4029 unsigned int total_len; 4030 int rc; 4031 4032 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server, 4033 (void **) &req, &total_len); 4034 if (rc) 4035 return rc; 4036 4037 req->PersistentFileId = persistent_fid; 4038 req->VolatileFileId = volatile_fid; 4039 /* See note 354 of MS-SMB2, 64K max */ 4040 req->OutputBufferLength = 4041 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE); 4042 req->CompletionFilter = cpu_to_le32(completion_filter); 4043 if (watch_tree) 4044 req->Flags = cpu_to_le16(SMB2_WATCH_TREE); 4045 else 4046 req->Flags = 0; 4047 4048 iov[0].iov_base = (char *)req; 4049 iov[0].iov_len = total_len; 4050 4051 return 0; 4052 } 4053 4054 int 4055 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon, 4056 u64 persistent_fid, u64 volatile_fid, bool watch_tree, 4057 u32 completion_filter, u32 max_out_data_len, char **out_data, 4058 u32 *plen /* returned data len */) 4059 { 4060 struct cifs_ses *ses = tcon->ses; 4061 struct TCP_Server_Info *server; 4062 struct smb_rqst rqst; 4063 struct smb2_change_notify_rsp *smb_rsp; 4064 struct kvec iov[1]; 4065 struct kvec rsp_iov = {NULL, 0}; 4066 int resp_buftype = CIFS_NO_BUFFER; 4067 int flags = 0; 4068 int rc = 0; 4069 int retries = 0, cur_sleep = 0; 4070 4071 replay_again: 4072 /* reinitialize for possible replay */ 4073 flags = 0; 4074 server = cifs_pick_channel(ses); 4075 4076 cifs_dbg(FYI, "change notify\n"); 4077 if (!ses || !server) 4078 return smb_EIO(smb_eio_trace_null_pointers); 4079 4080 if (smb3_encryption_required(tcon)) 4081 flags |= CIFS_TRANSFORM_REQ; 4082 4083 memset(&rqst, 0, sizeof(struct smb_rqst)); 4084 memset(&iov, 0, sizeof(iov)); 4085 if (plen) 4086 *plen = 0; 4087 4088 rqst.rq_iov = iov; 4089 rqst.rq_nvec = 1; 4090 4091 rc = SMB2_notify_init(xid, &rqst, tcon, server, 4092 persistent_fid, volatile_fid, 4093 completion_filter, watch_tree); 4094 if (rc) 4095 goto cnotify_exit; 4096 4097 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid, 4098 (u8)watch_tree, completion_filter); 4099 4100 if (retries) { 4101 /* Back-off before retry */ 4102 if (cur_sleep) 4103 msleep(cur_sleep); 4104 smb2_set_replay(server, &rqst); 4105 } 4106 4107 rc = cifs_send_recv(xid, ses, server, 4108 &rqst, &resp_buftype, flags, &rsp_iov); 4109 4110 if (rc != 0) { 4111 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE); 4112 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid, 4113 (u8)watch_tree, completion_filter, rc); 4114 } else { 4115 trace_smb3_notify_done(xid, persistent_fid, tcon->tid, 4116 ses->Suid, (u8)watch_tree, completion_filter); 4117 /* validate that notify information is plausible */ 4118 if ((rsp_iov.iov_base == NULL) || 4119 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1)) 4120 goto cnotify_exit; 4121 4122 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base; 4123 4124 rc = smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset), 4125 le32_to_cpu(smb_rsp->OutputBufferLength), 4126 &rsp_iov, 4127 sizeof(struct file_notify_information)); 4128 if (rc) 4129 goto cnotify_exit; 4130 4131 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset), 4132 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL); 4133 if (*out_data == NULL) { 4134 rc = -ENOMEM; 4135 goto cnotify_exit; 4136 } else if (plen) 4137 *plen = le32_to_cpu(smb_rsp->OutputBufferLength); 4138 } 4139 4140 cnotify_exit: 4141 if (rqst.rq_iov) 4142 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */ 4143 free_rsp_buf(resp_buftype, rsp_iov.iov_base); 4144 4145 if (is_replayable_error(rc) && 4146 smb2_should_replay(tcon, &retries, &cur_sleep)) 4147 goto replay_again; 4148 4149 return rc; 4150 } 4151 4152 4153 4154 /* 4155 * This is a no-op for now. We're not really interested in the reply, but 4156 * rather in the fact that the server sent one and that server->lstrp 4157 * gets updated. 4158 * 4159 * FIXME: maybe we should consider checking that the reply matches request? 4160 */ 4161 static void 4162 smb2_echo_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid) 4163 { 4164 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf; 4165 struct cifs_credits credits = { .value = 0, .instance = 0 }; 4166 4167 if (mid->mid_state == MID_RESPONSE_RECEIVED 4168 || mid->mid_state == MID_RESPONSE_MALFORMED) { 4169 credits.value = le16_to_cpu(rsp->hdr.CreditRequest); 4170 credits.instance = server->reconnect_instance; 4171 } 4172 4173 release_mid(server, mid); 4174 add_credits(server, &credits, CIFS_ECHO_OP); 4175 } 4176 4177 static void cifs_renegotiate_iosize(struct TCP_Server_Info *server, 4178 struct cifs_tcon *tcon) 4179 { 4180 struct cifs_sb_info *cifs_sb; 4181 4182 if (server == NULL || tcon == NULL) 4183 return; 4184 4185 spin_lock(&tcon->sb_list_lock); 4186 list_for_each_entry(cifs_sb, &tcon->cifs_sb_list, tcon_sb_link) 4187 cifs_negotiate_iosize(server, cifs_sb->ctx, tcon); 4188 spin_unlock(&tcon->sb_list_lock); 4189 } 4190 4191 void smb2_reconnect_server(struct work_struct *work) 4192 { 4193 struct TCP_Server_Info *server = container_of(work, 4194 struct TCP_Server_Info, reconnect.work); 4195 struct TCP_Server_Info *pserver; 4196 struct cifs_ses *ses, *ses2; 4197 struct cifs_tcon *tcon, *tcon2; 4198 struct list_head tmp_list, tmp_ses_list; 4199 bool ses_exist = false; 4200 bool tcon_selected = false; 4201 int rc; 4202 bool resched = false; 4203 4204 /* first check if ref count has reached 0, if not inc ref count */ 4205 spin_lock(&cifs_tcp_ses_lock); 4206 if (!server->srv_count) { 4207 spin_unlock(&cifs_tcp_ses_lock); 4208 return; 4209 } 4210 server->srv_count++; 4211 spin_unlock(&cifs_tcp_ses_lock); 4212 4213 /* If server is a channel, select the primary channel */ 4214 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 4215 4216 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */ 4217 mutex_lock(&pserver->reconnect_mutex); 4218 4219 /* if the server is marked for termination, drop the ref count here */ 4220 if (server->terminate) { 4221 cifs_put_tcp_session(server, true); 4222 mutex_unlock(&pserver->reconnect_mutex); 4223 return; 4224 } 4225 4226 INIT_LIST_HEAD(&tmp_list); 4227 INIT_LIST_HEAD(&tmp_ses_list); 4228 cifs_dbg(FYI, "Reconnecting tcons and channels\n"); 4229 4230 spin_lock(&cifs_tcp_ses_lock); 4231 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 4232 spin_lock(&ses->ses_lock); 4233 if (ses->ses_status == SES_EXITING) { 4234 spin_unlock(&ses->ses_lock); 4235 continue; 4236 } 4237 spin_unlock(&ses->ses_lock); 4238 4239 tcon_selected = false; 4240 4241 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 4242 if (tcon->need_reconnect || tcon->need_reopen_files) { 4243 spin_lock(&tcon->tc_lock); 4244 tcon->tc_count++; 4245 spin_unlock(&tcon->tc_lock); 4246 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 4247 netfs_trace_tcon_ref_get_reconnect_server); 4248 list_add_tail(&tcon->rlist, &tmp_list); 4249 tcon_selected = true; 4250 } 4251 } 4252 /* 4253 * IPC has the same lifetime as its session and uses its 4254 * refcount. 4255 */ 4256 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) { 4257 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list); 4258 tcon_selected = true; 4259 cifs_smb_ses_inc_refcount(ses); 4260 } 4261 /* 4262 * handle the case where channel needs to reconnect 4263 * binding session, but tcon is healthy (some other channel 4264 * is active) 4265 */ 4266 spin_lock(&ses->chan_lock); 4267 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) { 4268 list_add_tail(&ses->rlist, &tmp_ses_list); 4269 ses_exist = true; 4270 cifs_smb_ses_inc_refcount(ses); 4271 } 4272 spin_unlock(&ses->chan_lock); 4273 } 4274 spin_unlock(&cifs_tcp_ses_lock); 4275 4276 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) { 4277 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true); 4278 if (!rc) { 4279 cifs_renegotiate_iosize(server, tcon); 4280 cifs_reopen_persistent_handles(tcon); 4281 } else 4282 resched = true; 4283 list_del_init(&tcon->rlist); 4284 if (tcon->ipc) 4285 cifs_put_smb_ses(tcon->ses); 4286 else 4287 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_reconnect_server); 4288 } 4289 4290 if (!ses_exist) 4291 goto done; 4292 4293 /* allocate a dummy tcon struct used for reconnect */ 4294 tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_reconnect_server); 4295 if (!tcon) { 4296 resched = true; 4297 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) { 4298 list_del_init(&ses->rlist); 4299 cifs_put_smb_ses(ses); 4300 } 4301 goto done; 4302 } 4303 tcon->status = TID_GOOD; 4304 tcon->dummy = true; 4305 4306 /* now reconnect sessions for necessary channels */ 4307 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) { 4308 tcon->ses = ses; 4309 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true); 4310 if (rc) 4311 resched = true; 4312 list_del_init(&ses->rlist); 4313 cifs_put_smb_ses(ses); 4314 } 4315 tconInfoFree(tcon, netfs_trace_tcon_ref_free_reconnect_server); 4316 4317 done: 4318 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n"); 4319 if (resched) 4320 cifs_requeue_server_reconn(server); 4321 mutex_unlock(&pserver->reconnect_mutex); 4322 4323 /* now we can safely release srv struct */ 4324 cifs_put_tcp_session(server, true); 4325 } 4326 4327 int 4328 SMB2_echo(struct TCP_Server_Info *server) 4329 { 4330 struct smb2_echo_req *req; 4331 int rc = 0; 4332 struct kvec iov[1]; 4333 struct smb_rqst rqst = { .rq_iov = iov, 4334 .rq_nvec = 1 }; 4335 unsigned int total_len; 4336 4337 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id); 4338 4339 spin_lock(&server->srv_lock); 4340 if (server->ops->need_neg && 4341 server->ops->need_neg(server)) { 4342 spin_unlock(&server->srv_lock); 4343 /* No need to send echo on newly established connections */ 4344 cifs_queue_server_reconn(server); 4345 return rc; 4346 } 4347 spin_unlock(&server->srv_lock); 4348 4349 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server, 4350 (void **)&req, &total_len); 4351 if (rc) 4352 return rc; 4353 4354 req->hdr.CreditRequest = cpu_to_le16(1); 4355 4356 iov[0].iov_len = total_len; 4357 iov[0].iov_base = (char *)req; 4358 4359 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL, 4360 server, CIFS_ECHO_OP, NULL); 4361 if (rc) 4362 cifs_dbg(FYI, "Echo request failed: %d\n", rc); 4363 4364 cifs_small_buf_release(req); 4365 return rc; 4366 } 4367 4368 void 4369 SMB2_flush_free(struct smb_rqst *rqst) 4370 { 4371 if (rqst && rqst->rq_iov) 4372 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ 4373 } 4374 4375 int 4376 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst, 4377 struct cifs_tcon *tcon, struct TCP_Server_Info *server, 4378 u64 persistent_fid, u64 volatile_fid) 4379 { 4380 struct smb2_flush_req *req; 4381 struct kvec *iov = rqst->rq_iov; 4382 unsigned int total_len; 4383 int rc; 4384 4385 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server, 4386 (void **) &req, &total_len); 4387 if (rc) 4388 return rc; 4389 4390 req->PersistentFileId = persistent_fid; 4391 req->VolatileFileId = volatile_fid; 4392 4393 iov[0].iov_base = (char *)req; 4394 iov[0].iov_len = total_len; 4395 4396 return 0; 4397 } 4398 4399 int 4400 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, 4401 u64 volatile_fid) 4402 { 4403 struct cifs_ses *ses = tcon->ses; 4404 struct smb_rqst rqst; 4405 struct kvec iov[1]; 4406 struct kvec rsp_iov = {NULL, 0}; 4407 struct TCP_Server_Info *server; 4408 int resp_buftype = CIFS_NO_BUFFER; 4409 int flags = 0; 4410 int rc = 0; 4411 int retries = 0, cur_sleep = 0; 4412 4413 replay_again: 4414 /* reinitialize for possible replay */ 4415 flags = 0; 4416 server = cifs_pick_channel(ses); 4417 4418 cifs_dbg(FYI, "flush\n"); 4419 if (!ses || !(ses->server)) 4420 return smb_EIO(smb_eio_trace_null_pointers); 4421 4422 if (smb3_encryption_required(tcon)) 4423 flags |= CIFS_TRANSFORM_REQ; 4424 4425 memset(&rqst, 0, sizeof(struct smb_rqst)); 4426 memset(&iov, 0, sizeof(iov)); 4427 rqst.rq_iov = iov; 4428 rqst.rq_nvec = 1; 4429 4430 rc = SMB2_flush_init(xid, &rqst, tcon, server, 4431 persistent_fid, volatile_fid); 4432 if (rc) 4433 goto flush_exit; 4434 4435 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid); 4436 4437 if (retries) { 4438 /* Back-off before retry */ 4439 if (cur_sleep) 4440 msleep(cur_sleep); 4441 smb2_set_replay(server, &rqst); 4442 } 4443 4444 rc = cifs_send_recv(xid, ses, server, 4445 &rqst, &resp_buftype, flags, &rsp_iov); 4446 4447 if (rc != 0) { 4448 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE); 4449 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid, 4450 rc); 4451 } else 4452 trace_smb3_flush_done(xid, persistent_fid, tcon->tid, 4453 ses->Suid); 4454 4455 flush_exit: 4456 SMB2_flush_free(&rqst); 4457 free_rsp_buf(resp_buftype, rsp_iov.iov_base); 4458 4459 if (is_replayable_error(rc) && 4460 smb2_should_replay(tcon, &retries, &cur_sleep)) 4461 goto replay_again; 4462 4463 return rc; 4464 } 4465 4466 #ifdef CONFIG_CIFS_SMB_DIRECT 4467 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms) 4468 { 4469 struct TCP_Server_Info *server = io_parms->server; 4470 struct cifs_tcon *tcon = io_parms->tcon; 4471 4472 /* we can only offload if we're connected */ 4473 if (!server || !tcon) 4474 return false; 4475 4476 /* we can only offload on an rdma connection */ 4477 if (!server->rdma || !server->smbd_conn) 4478 return false; 4479 4480 /* we don't support signed offload yet */ 4481 if (server->sign) 4482 return false; 4483 4484 /* we don't support encrypted offload yet */ 4485 if (smb3_encryption_required(tcon)) 4486 return false; 4487 4488 /* offload also has its overhead, so only do it if desired */ 4489 if (io_parms->length < server->rdma_readwrite_threshold) 4490 return false; 4491 4492 return true; 4493 } 4494 #endif /* CONFIG_CIFS_SMB_DIRECT */ 4495 4496 /* 4497 * To form a chain of read requests, any read requests after the first should 4498 * have the end_of_chain boolean set to true. 4499 */ 4500 static int 4501 smb2_new_read_req(void **buf, unsigned int *total_len, 4502 struct cifs_io_parms *io_parms, struct cifs_io_subrequest *rdata, 4503 unsigned int remaining_bytes, int request_type) 4504 { 4505 int rc = -EACCES; 4506 struct smb2_read_req *req = NULL; 4507 struct smb2_hdr *shdr; 4508 struct TCP_Server_Info *server = io_parms->server; 4509 4510 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server, 4511 (void **) &req, total_len); 4512 if (rc) 4513 return rc; 4514 4515 if (server == NULL) 4516 return -ECONNABORTED; 4517 4518 shdr = &req->hdr; 4519 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid); 4520 4521 req->PersistentFileId = io_parms->persistent_fid; 4522 req->VolatileFileId = io_parms->volatile_fid; 4523 req->ReadChannelInfoOffset = 0; /* reserved */ 4524 req->ReadChannelInfoLength = 0; /* reserved */ 4525 req->Channel = 0; /* reserved */ 4526 req->MinimumCount = 0; 4527 req->Length = cpu_to_le32(io_parms->length); 4528 req->Offset = cpu_to_le64(io_parms->offset); 4529 4530 trace_smb3_read_enter(rdata ? rdata->rreq->debug_id : 0, 4531 rdata ? rdata->subreq.debug_index : 0, 4532 rdata ? rdata->xid : 0, 4533 io_parms->persistent_fid, 4534 io_parms->tcon->tid, io_parms->tcon->ses->Suid, 4535 io_parms->offset, io_parms->length); 4536 #ifdef CONFIG_CIFS_SMB_DIRECT 4537 /* 4538 * If we want to do a RDMA write, fill in and append 4539 * smbdirect_buffer_descriptor_v1 to the end of read request 4540 */ 4541 if (rdata && smb3_use_rdma_offload(io_parms)) { 4542 struct smbdirect_buffer_descriptor_v1 *v1; 4543 bool need_invalidate = server->dialect == SMB30_PROT_ID; 4544 4545 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->subreq.io_iter, 4546 true, need_invalidate); 4547 if (!rdata->mr) 4548 return -EAGAIN; 4549 4550 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; 4551 if (need_invalidate) 4552 req->Channel = SMB2_CHANNEL_RDMA_V1; 4553 req->ReadChannelInfoOffset = 4554 cpu_to_le16(offsetof(struct smb2_read_req, Buffer)); 4555 req->ReadChannelInfoLength = 4556 cpu_to_le16(sizeof(struct smbdirect_buffer_descriptor_v1)); 4557 v1 = (struct smbdirect_buffer_descriptor_v1 *) &req->Buffer[0]; 4558 smbd_mr_fill_buffer_descriptor(rdata->mr, v1); 4559 4560 *total_len += sizeof(*v1) - 1; 4561 } 4562 #endif 4563 if (request_type & CHAINED_REQUEST) { 4564 if (!(request_type & END_OF_CHAIN)) { 4565 /* next 8-byte aligned request */ 4566 *total_len = ALIGN(*total_len, 8); 4567 shdr->NextCommand = cpu_to_le32(*total_len); 4568 } else /* END_OF_CHAIN */ 4569 shdr->NextCommand = 0; 4570 if (request_type & RELATED_REQUEST) { 4571 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS; 4572 /* 4573 * Related requests use info from previous read request 4574 * in chain. 4575 */ 4576 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF); 4577 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF); 4578 req->PersistentFileId = (u64)-1; 4579 req->VolatileFileId = (u64)-1; 4580 } 4581 } 4582 if (remaining_bytes > io_parms->length) 4583 req->RemainingBytes = cpu_to_le32(remaining_bytes); 4584 else 4585 req->RemainingBytes = 0; 4586 4587 *buf = req; 4588 return rc; 4589 } 4590 4591 static void 4592 smb2_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid) 4593 { 4594 struct cifs_io_subrequest *rdata = mid->callback_data; 4595 struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode); 4596 struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink); 4597 struct smb2_hdr *shdr = (struct smb2_hdr *)rdata->iov[0].iov_base; 4598 struct cifs_credits credits = { 4599 .value = 0, 4600 .instance = 0, 4601 .rreq_debug_id = rdata->rreq->debug_id, 4602 .rreq_debug_index = rdata->subreq.debug_index, 4603 }; 4604 struct smb_rqst rqst = { .rq_iov = &rdata->iov[0], .rq_nvec = 1 }; 4605 unsigned int rreq_debug_id = rdata->rreq->debug_id; 4606 unsigned int subreq_debug_index = rdata->subreq.debug_index; 4607 4608 if (rdata->got_bytes) { 4609 rqst.rq_iter = rdata->subreq.io_iter; 4610 } 4611 4612 WARN_ONCE(rdata->server != server, 4613 "rdata server %p != mid server %p", 4614 rdata->server, server); 4615 4616 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%zu/%zu\n", 4617 __func__, mid->mid, mid->mid_state, rdata->result, 4618 rdata->got_bytes, rdata->subreq.len - rdata->subreq.transferred); 4619 4620 switch (mid->mid_state) { 4621 case MID_RESPONSE_RECEIVED: 4622 credits.value = le16_to_cpu(shdr->CreditRequest); 4623 credits.instance = server->reconnect_instance; 4624 /* result already set, check signature */ 4625 if (server->sign && !mid->decrypted) { 4626 int rc; 4627 4628 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes); 4629 rc = smb2_verify_signature(&rqst, server); 4630 if (rc) { 4631 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n", 4632 rc); 4633 rdata->subreq.error = rc; 4634 rdata->result = rc; 4635 4636 if (is_replayable_error(rc)) { 4637 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_retry_needed); 4638 __set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags); 4639 } else 4640 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_bad); 4641 } else 4642 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress); 4643 } 4644 /* FIXME: should this be counted toward the initiating task? */ 4645 task_io_account_read(rdata->got_bytes); 4646 cifs_stats_bytes_read(tcon, rdata->got_bytes); 4647 break; 4648 case MID_REQUEST_SUBMITTED: 4649 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_req_submitted); 4650 goto do_retry; 4651 case MID_RETRY_NEEDED: 4652 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_retry_needed); 4653 do_retry: 4654 __set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags); 4655 rdata->result = -EAGAIN; 4656 if (server->sign && rdata->got_bytes) 4657 /* reset bytes number since we can not check a sign */ 4658 rdata->got_bytes = 0; 4659 /* FIXME: should this be counted toward the initiating task? */ 4660 task_io_account_read(rdata->got_bytes); 4661 cifs_stats_bytes_read(tcon, rdata->got_bytes); 4662 break; 4663 case MID_RESPONSE_MALFORMED: 4664 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_malformed); 4665 credits.value = le16_to_cpu(shdr->CreditRequest); 4666 credits.instance = server->reconnect_instance; 4667 rdata->result = smb_EIO(smb_eio_trace_read_rsp_malformed); 4668 break; 4669 default: 4670 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_unknown); 4671 rdata->result = smb_EIO1(smb_eio_trace_read_mid_state_unknown, 4672 mid->mid_state); 4673 break; 4674 } 4675 #ifdef CONFIG_CIFS_SMB_DIRECT 4676 /* 4677 * If this rdata has a memory registered, the MR can be freed 4678 * MR needs to be freed as soon as I/O finishes to prevent deadlock 4679 * because they have limited number and are used for future I/Os 4680 */ 4681 if (rdata->mr) { 4682 smbd_deregister_mr(rdata->mr); 4683 rdata->mr = NULL; 4684 } 4685 #endif 4686 if (rdata->result && rdata->result != -ENODATA) { 4687 cifs_stats_fail_inc(tcon, SMB2_READ_HE); 4688 trace_smb3_read_err(rdata->rreq->debug_id, 4689 rdata->subreq.debug_index, 4690 rdata->xid, 4691 rdata->req->cfile->fid.persistent_fid, 4692 tcon->tid, tcon->ses->Suid, 4693 rdata->subreq.start + rdata->subreq.transferred, 4694 rdata->subreq.len - rdata->subreq.transferred, 4695 rdata->result); 4696 } else 4697 trace_smb3_read_done(rdata->rreq->debug_id, 4698 rdata->subreq.debug_index, 4699 rdata->xid, 4700 rdata->req->cfile->fid.persistent_fid, 4701 tcon->tid, tcon->ses->Suid, 4702 rdata->subreq.start + rdata->subreq.transferred, 4703 rdata->got_bytes); 4704 4705 if (rdata->result == -ENODATA) { 4706 __set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags); 4707 rdata->result = 0; 4708 } else { 4709 size_t trans = rdata->subreq.transferred + rdata->got_bytes; 4710 if (trans < rdata->subreq.len && 4711 rdata->subreq.start + trans >= ictx->remote_i_size) { 4712 __set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags); 4713 rdata->result = 0; 4714 } 4715 if (rdata->got_bytes) 4716 __set_bit(NETFS_SREQ_MADE_PROGRESS, &rdata->subreq.flags); 4717 } 4718 4719 /* see if we need to retry */ 4720 if (is_replayable_error(rdata->result) && 4721 smb2_should_replay(tcon, 4722 &rdata->retries, 4723 &rdata->cur_sleep)) 4724 rdata->replay = true; 4725 4726 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, rdata->credits.value, 4727 server->credits, server->in_flight, 4728 0, cifs_trace_rw_credits_read_response_clear); 4729 rdata->credits.value = 0; 4730 rdata->subreq.error = rdata->result; 4731 rdata->subreq.transferred += rdata->got_bytes; 4732 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress); 4733 netfs_read_subreq_terminated(&rdata->subreq); 4734 release_mid(server, mid); 4735 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0, 4736 server->credits, server->in_flight, 4737 credits.value, cifs_trace_rw_credits_read_response_add); 4738 add_credits(server, &credits, 0); 4739 } 4740 4741 /* smb2_async_readv - send an async read, and set up mid to handle result */ 4742 int 4743 smb2_async_readv(struct cifs_io_subrequest *rdata) 4744 { 4745 int rc, flags = 0; 4746 char *buf; 4747 struct netfs_io_subrequest *subreq = &rdata->subreq; 4748 struct smb2_hdr *shdr; 4749 struct cifs_io_parms io_parms; 4750 struct smb_rqst rqst = { .rq_iov = rdata->iov, 4751 .rq_nvec = 1 }; 4752 struct TCP_Server_Info *server; 4753 struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink); 4754 unsigned int total_len; 4755 int credit_request; 4756 4757 cifs_dbg(FYI, "%s: offset=%llu bytes=%zu\n", 4758 __func__, subreq->start, subreq->len); 4759 4760 if (!rdata->server) 4761 rdata->server = cifs_pick_channel(tcon->ses); 4762 4763 io_parms.tcon = tlink_tcon(rdata->req->cfile->tlink); 4764 io_parms.server = server = rdata->server; 4765 io_parms.offset = subreq->start + subreq->transferred; 4766 io_parms.length = subreq->len - subreq->transferred; 4767 io_parms.persistent_fid = rdata->req->cfile->fid.persistent_fid; 4768 io_parms.volatile_fid = rdata->req->cfile->fid.volatile_fid; 4769 io_parms.pid = rdata->req->pid; 4770 4771 rc = smb2_new_read_req( 4772 (void **) &buf, &total_len, &io_parms, rdata, 0, 0); 4773 if (rc) 4774 goto out; 4775 4776 if (smb3_encryption_required(io_parms.tcon)) 4777 flags |= CIFS_TRANSFORM_REQ; 4778 4779 rdata->iov[0].iov_base = buf; 4780 rdata->iov[0].iov_len = total_len; 4781 rdata->got_bytes = 0; 4782 rdata->result = 0; 4783 4784 shdr = (struct smb2_hdr *)buf; 4785 4786 if (rdata->replay) { 4787 /* Back-off before retry */ 4788 if (rdata->cur_sleep) 4789 msleep(rdata->cur_sleep); 4790 smb2_set_replay(server, &rqst); 4791 } 4792 4793 if (rdata->credits.value > 0) { 4794 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(io_parms.length, 4795 SMB2_MAX_BUFFER_SIZE)); 4796 credit_request = le16_to_cpu(shdr->CreditCharge) + 8; 4797 if (server->credits >= server->max_credits) 4798 shdr->CreditRequest = cpu_to_le16(0); 4799 else 4800 shdr->CreditRequest = cpu_to_le16( 4801 min_t(int, server->max_credits - 4802 server->credits, credit_request)); 4803 4804 rc = adjust_credits(server, rdata, cifs_trace_rw_credits_call_readv_adjust); 4805 if (rc) 4806 goto async_readv_out; 4807 4808 flags |= CIFS_HAS_CREDITS; 4809 } 4810 4811 rc = cifs_call_async(server, &rqst, 4812 cifs_readv_receive, smb2_readv_callback, 4813 smb3_handle_read_data, rdata, flags, 4814 &rdata->credits); 4815 if (rc) { 4816 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE); 4817 trace_smb3_read_err(rdata->rreq->debug_id, 4818 subreq->debug_index, 4819 rdata->xid, io_parms.persistent_fid, 4820 io_parms.tcon->tid, 4821 io_parms.tcon->ses->Suid, 4822 io_parms.offset, 4823 subreq->len - subreq->transferred, rc); 4824 } 4825 4826 async_readv_out: 4827 cifs_small_buf_release(buf); 4828 4829 out: 4830 /* if the send error is retryable, let netfs know about it */ 4831 if (is_replayable_error(rc) && 4832 smb2_should_replay(tcon, 4833 &rdata->retries, 4834 &rdata->cur_sleep)) { 4835 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_retry_needed); 4836 __set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags); 4837 } 4838 4839 return rc; 4840 } 4841 4842 int 4843 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, 4844 unsigned int *nbytes, char **buf, int *buf_type) 4845 { 4846 struct smb_rqst rqst; 4847 int resp_buftype, rc; 4848 struct smb2_read_req *req = NULL; 4849 struct smb2_read_rsp *rsp = NULL; 4850 struct kvec iov[1]; 4851 struct kvec rsp_iov; 4852 unsigned int total_len; 4853 int flags = CIFS_LOG_ERROR; 4854 struct cifs_ses *ses = io_parms->tcon->ses; 4855 4856 if (!io_parms->server) 4857 io_parms->server = cifs_pick_channel(io_parms->tcon->ses); 4858 4859 *nbytes = 0; 4860 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0); 4861 if (rc) 4862 return rc; 4863 4864 if (smb3_encryption_required(io_parms->tcon)) 4865 flags |= CIFS_TRANSFORM_REQ; 4866 4867 iov[0].iov_base = (char *)req; 4868 iov[0].iov_len = total_len; 4869 4870 memset(&rqst, 0, sizeof(struct smb_rqst)); 4871 rqst.rq_iov = iov; 4872 rqst.rq_nvec = 1; 4873 4874 rc = cifs_send_recv(xid, ses, io_parms->server, 4875 &rqst, &resp_buftype, flags, &rsp_iov); 4876 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base; 4877 4878 if (rc) { 4879 if (rc != -ENODATA) { 4880 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE); 4881 cifs_dbg(VFS, "Send error in read = %d\n", rc); 4882 trace_smb3_read_err(0, 0, xid, 4883 req->PersistentFileId, 4884 io_parms->tcon->tid, ses->Suid, 4885 io_parms->offset, io_parms->length, 4886 rc); 4887 } else 4888 trace_smb3_read_done(0, 0, xid, 4889 req->PersistentFileId, io_parms->tcon->tid, 4890 ses->Suid, io_parms->offset, 0); 4891 free_rsp_buf(resp_buftype, rsp_iov.iov_base); 4892 cifs_small_buf_release(req); 4893 return rc == -ENODATA ? 0 : rc; 4894 } else 4895 trace_smb3_read_done(0, 0, xid, 4896 req->PersistentFileId, 4897 io_parms->tcon->tid, ses->Suid, 4898 io_parms->offset, io_parms->length); 4899 4900 cifs_small_buf_release(req); 4901 4902 *nbytes = le32_to_cpu(rsp->DataLength); 4903 if ((*nbytes > CIFS_MAX_MSGSIZE) || 4904 (*nbytes > io_parms->length)) { 4905 cifs_dbg(FYI, "bad length %d for count %d\n", 4906 *nbytes, io_parms->length); 4907 rc = smb_EIO2(smb_eio_trace_read_overlarge, 4908 *nbytes, io_parms->length); 4909 *nbytes = 0; 4910 } 4911 4912 if (*buf) { 4913 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes); 4914 free_rsp_buf(resp_buftype, rsp_iov.iov_base); 4915 } else if (resp_buftype != CIFS_NO_BUFFER) { 4916 *buf = rsp_iov.iov_base; 4917 if (resp_buftype == CIFS_SMALL_BUFFER) 4918 *buf_type = CIFS_SMALL_BUFFER; 4919 else if (resp_buftype == CIFS_LARGE_BUFFER) 4920 *buf_type = CIFS_LARGE_BUFFER; 4921 } 4922 return rc; 4923 } 4924 4925 /* 4926 * Check the mid_state and signature on received buffer (if any), and queue the 4927 * workqueue completion task. 4928 */ 4929 static void 4930 smb2_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid) 4931 { 4932 struct cifs_io_subrequest *wdata = mid->callback_data; 4933 struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink); 4934 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf; 4935 struct cifs_credits credits = { 4936 .value = 0, 4937 .instance = 0, 4938 .rreq_debug_id = wdata->rreq->debug_id, 4939 .rreq_debug_index = wdata->subreq.debug_index, 4940 }; 4941 unsigned int rreq_debug_id = wdata->rreq->debug_id; 4942 unsigned int subreq_debug_index = wdata->subreq.debug_index; 4943 ssize_t result = 0; 4944 size_t written; 4945 4946 WARN_ONCE(wdata->server != server, 4947 "wdata server %p != mid server %p", 4948 wdata->server, server); 4949 4950 switch (mid->mid_state) { 4951 case MID_RESPONSE_RECEIVED: 4952 credits.value = le16_to_cpu(rsp->hdr.CreditRequest); 4953 credits.instance = server->reconnect_instance; 4954 result = smb2_check_receive(mid, server, 0); 4955 if (result != 0) { 4956 if (is_replayable_error(result)) { 4957 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_retry_needed); 4958 __set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags); 4959 } else { 4960 wdata->subreq.error = result; 4961 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_bad); 4962 } 4963 break; 4964 } 4965 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_progress); 4966 4967 written = le32_to_cpu(rsp->DataLength); 4968 /* 4969 * Mask off high 16 bits when bytes written as returned 4970 * by the server is greater than bytes requested by the 4971 * client. OS/2 servers are known to set incorrect 4972 * CountHigh values. 4973 */ 4974 if (written > wdata->subreq.len) 4975 written &= 0xFFFF; 4976 4977 cifs_stats_bytes_written(tcon, written); 4978 4979 if (written < wdata->subreq.len) { 4980 result = -ENOSPC; 4981 } else if (written > 0) { 4982 wdata->subreq.len = written; 4983 __set_bit(NETFS_SREQ_MADE_PROGRESS, &wdata->subreq.flags); 4984 } 4985 break; 4986 case MID_REQUEST_SUBMITTED: 4987 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_req_submitted); 4988 __set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags); 4989 result = -EAGAIN; 4990 break; 4991 case MID_RETRY_NEEDED: 4992 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_retry_needed); 4993 __set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags); 4994 result = -EAGAIN; 4995 break; 4996 case MID_RESPONSE_MALFORMED: 4997 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_malformed); 4998 credits.value = le16_to_cpu(rsp->hdr.CreditRequest); 4999 credits.instance = server->reconnect_instance; 5000 result = smb_EIO(smb_eio_trace_write_rsp_malformed); 5001 break; 5002 default: 5003 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_unknown); 5004 result = smb_EIO1(smb_eio_trace_write_mid_state_unknown, 5005 mid->mid_state); 5006 break; 5007 } 5008 #ifdef CONFIG_CIFS_SMB_DIRECT 5009 /* 5010 * If this wdata has a memory registered, the MR can be freed 5011 * The number of MRs available is limited, it's important to recover 5012 * used MR as soon as I/O is finished. Hold MR longer in the later 5013 * I/O process can possibly result in I/O deadlock due to lack of MR 5014 * to send request on I/O retry 5015 */ 5016 if (wdata->mr) { 5017 smbd_deregister_mr(wdata->mr); 5018 wdata->mr = NULL; 5019 } 5020 #endif 5021 if (result) { 5022 wdata->result = result; 5023 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); 5024 trace_smb3_write_err(wdata->rreq->debug_id, 5025 wdata->subreq.debug_index, 5026 wdata->xid, 5027 wdata->req->cfile->fid.persistent_fid, 5028 tcon->tid, tcon->ses->Suid, wdata->subreq.start, 5029 wdata->subreq.len, wdata->result); 5030 if (wdata->result == -ENOSPC) 5031 pr_warn_once("Out of space writing to %s\n", 5032 tcon->tree_name); 5033 } else 5034 trace_smb3_write_done(wdata->rreq->debug_id, 5035 wdata->subreq.debug_index, 5036 wdata->xid, 5037 wdata->req->cfile->fid.persistent_fid, 5038 tcon->tid, tcon->ses->Suid, 5039 wdata->subreq.start, wdata->subreq.len); 5040 5041 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, wdata->credits.value, 5042 server->credits, server->in_flight, 5043 0, cifs_trace_rw_credits_write_response_clear); 5044 wdata->credits.value = 0; 5045 5046 /* see if we need to retry */ 5047 if (is_replayable_error(wdata->result) && 5048 smb2_should_replay(tcon, 5049 &wdata->retries, 5050 &wdata->cur_sleep)) 5051 wdata->replay = true; 5052 5053 cifs_write_subrequest_terminated(wdata, result ?: written); 5054 release_mid(server, mid); 5055 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0, 5056 server->credits, server->in_flight, 5057 credits.value, cifs_trace_rw_credits_write_response_add); 5058 add_credits(server, &credits, 0); 5059 } 5060 5061 /* smb2_async_writev - send an async write, and set up mid to handle result */ 5062 void 5063 smb2_async_writev(struct cifs_io_subrequest *wdata) 5064 { 5065 int rc = -EACCES, flags = 0; 5066 struct smb2_write_req *req = NULL; 5067 struct smb2_hdr *shdr; 5068 struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink); 5069 struct TCP_Server_Info *server = wdata->server; 5070 struct kvec iov[1]; 5071 struct smb_rqst rqst = { }; 5072 unsigned int total_len, xid = wdata->xid; 5073 struct cifs_io_parms _io_parms; 5074 struct cifs_io_parms *io_parms = NULL; 5075 int credit_request; 5076 5077 /* 5078 * in future we may get cifs_io_parms passed in from the caller, 5079 * but for now we construct it here... 5080 */ 5081 _io_parms = (struct cifs_io_parms) { 5082 .tcon = tcon, 5083 .server = server, 5084 .offset = wdata->subreq.start, 5085 .length = wdata->subreq.len, 5086 .persistent_fid = wdata->req->cfile->fid.persistent_fid, 5087 .volatile_fid = wdata->req->cfile->fid.volatile_fid, 5088 .pid = wdata->req->pid, 5089 }; 5090 io_parms = &_io_parms; 5091 5092 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server, 5093 (void **) &req, &total_len); 5094 if (rc) 5095 goto out; 5096 5097 rqst.rq_iov = iov; 5098 rqst.rq_iter = wdata->subreq.io_iter; 5099 5100 rqst.rq_iov[0].iov_len = total_len - 1; 5101 rqst.rq_iov[0].iov_base = (char *)req; 5102 rqst.rq_nvec += 1; 5103 5104 if (smb3_encryption_required(tcon)) 5105 flags |= CIFS_TRANSFORM_REQ; 5106 5107 shdr = (struct smb2_hdr *)req; 5108 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid); 5109 5110 req->PersistentFileId = io_parms->persistent_fid; 5111 req->VolatileFileId = io_parms->volatile_fid; 5112 req->WriteChannelInfoOffset = 0; 5113 req->WriteChannelInfoLength = 0; 5114 req->Channel = SMB2_CHANNEL_NONE; 5115 req->Length = cpu_to_le32(io_parms->length); 5116 req->Offset = cpu_to_le64(io_parms->offset); 5117 req->DataOffset = cpu_to_le16( 5118 offsetof(struct smb2_write_req, Buffer)); 5119 req->RemainingBytes = 0; 5120 5121 trace_smb3_write_enter(wdata->rreq->debug_id, 5122 wdata->subreq.debug_index, 5123 wdata->xid, 5124 io_parms->persistent_fid, 5125 io_parms->tcon->tid, 5126 io_parms->tcon->ses->Suid, 5127 io_parms->offset, 5128 io_parms->length); 5129 5130 #ifdef CONFIG_CIFS_SMB_DIRECT 5131 /* 5132 * If we want to do a server RDMA read, fill in and append 5133 * smbdirect_buffer_descriptor_v1 to the end of write request 5134 */ 5135 if (smb3_use_rdma_offload(io_parms)) { 5136 struct smbdirect_buffer_descriptor_v1 *v1; 5137 bool need_invalidate = server->dialect == SMB30_PROT_ID; 5138 5139 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->subreq.io_iter, 5140 false, need_invalidate); 5141 if (!wdata->mr) { 5142 rc = -EAGAIN; 5143 goto async_writev_out; 5144 } 5145 /* For RDMA read, I/O size is in RemainingBytes not in Length */ 5146 req->RemainingBytes = req->Length; 5147 req->Length = 0; 5148 req->DataOffset = 0; 5149 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE; 5150 if (need_invalidate) 5151 req->Channel = SMB2_CHANNEL_RDMA_V1; 5152 req->WriteChannelInfoOffset = 5153 cpu_to_le16(offsetof(struct smb2_write_req, Buffer)); 5154 req->WriteChannelInfoLength = 5155 cpu_to_le16(sizeof(struct smbdirect_buffer_descriptor_v1)); 5156 v1 = (struct smbdirect_buffer_descriptor_v1 *) &req->Buffer[0]; 5157 smbd_mr_fill_buffer_descriptor(wdata->mr, v1); 5158 5159 rqst.rq_iov[0].iov_len += sizeof(*v1); 5160 5161 /* 5162 * We keep wdata->subreq.io_iter, 5163 * but we have to truncate rqst.rq_iter 5164 */ 5165 iov_iter_truncate(&rqst.rq_iter, 0); 5166 } 5167 #endif 5168 5169 if (wdata->replay) { 5170 /* Back-off before retry */ 5171 if (wdata->cur_sleep) 5172 msleep(wdata->cur_sleep); 5173 smb2_set_replay(server, &rqst); 5174 } 5175 5176 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n", 5177 io_parms->offset, io_parms->length, iov_iter_count(&wdata->subreq.io_iter)); 5178 5179 if (wdata->credits.value > 0) { 5180 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->subreq.len, 5181 SMB2_MAX_BUFFER_SIZE)); 5182 credit_request = le16_to_cpu(shdr->CreditCharge) + 8; 5183 if (server->credits >= server->max_credits) 5184 shdr->CreditRequest = cpu_to_le16(0); 5185 else 5186 shdr->CreditRequest = cpu_to_le16( 5187 min_t(int, server->max_credits - 5188 server->credits, credit_request)); 5189 5190 rc = adjust_credits(server, wdata, cifs_trace_rw_credits_call_writev_adjust); 5191 if (rc) 5192 goto async_writev_out; 5193 5194 flags |= CIFS_HAS_CREDITS; 5195 } 5196 5197 /* XXX: compression + encryption is unsupported for now */ 5198 if (((flags & CIFS_TRANSFORM_REQ) != CIFS_TRANSFORM_REQ) && should_compress(tcon, &rqst)) 5199 flags |= CIFS_COMPRESS_REQ; 5200 5201 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL, 5202 wdata, flags, &wdata->credits); 5203 /* Can't touch wdata if rc == 0 */ 5204 if (rc) { 5205 trace_smb3_write_err(wdata->rreq->debug_id, 5206 wdata->subreq.debug_index, 5207 xid, 5208 io_parms->persistent_fid, 5209 io_parms->tcon->tid, 5210 io_parms->tcon->ses->Suid, 5211 io_parms->offset, 5212 io_parms->length, 5213 rc); 5214 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); 5215 } 5216 5217 async_writev_out: 5218 cifs_small_buf_release(req); 5219 out: 5220 /* if the send error is retryable, let netfs know about it */ 5221 if (is_replayable_error(rc) && 5222 smb2_should_replay(tcon, 5223 &wdata->retries, 5224 &wdata->cur_sleep)) { 5225 wdata->replay = true; 5226 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_retry_needed); 5227 __set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags); 5228 } 5229 5230 if (rc) { 5231 trace_smb3_rw_credits(wdata->rreq->debug_id, 5232 wdata->subreq.debug_index, 5233 wdata->credits.value, 5234 server->credits, server->in_flight, 5235 -(int)wdata->credits.value, 5236 cifs_trace_rw_credits_write_response_clear); 5237 add_credits_and_wake_if(wdata->server, &wdata->credits, 0); 5238 cifs_write_subrequest_terminated(wdata, rc); 5239 } 5240 } 5241 5242 /* 5243 * SMB2_write function gets iov pointer to kvec array with n_vec as a length. 5244 * The length field from io_parms must be at least 1 and indicates a number of 5245 * elements with data to write that begins with position 1 in iov array. All 5246 * data length is specified by count. 5247 */ 5248 int 5249 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, 5250 unsigned int *nbytes, struct kvec *iov, int n_vec) 5251 { 5252 struct smb_rqst rqst; 5253 int rc = 0; 5254 struct smb2_write_req *req = NULL; 5255 struct smb2_write_rsp *rsp = NULL; 5256 int resp_buftype; 5257 struct kvec rsp_iov; 5258 int flags = 0; 5259 unsigned int total_len; 5260 struct TCP_Server_Info *server; 5261 int retries = 0, cur_sleep = 0; 5262 5263 replay_again: 5264 /* reinitialize for possible replay */ 5265 flags = 0; 5266 *nbytes = 0; 5267 if (!io_parms->server) 5268 io_parms->server = cifs_pick_channel(io_parms->tcon->ses); 5269 server = io_parms->server; 5270 if (server == NULL) 5271 return -ECONNABORTED; 5272 5273 if (n_vec < 1) 5274 return rc; 5275 5276 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server, 5277 (void **) &req, &total_len); 5278 if (rc) 5279 return rc; 5280 5281 if (smb3_encryption_required(io_parms->tcon)) 5282 flags |= CIFS_TRANSFORM_REQ; 5283 5284 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid); 5285 5286 req->PersistentFileId = io_parms->persistent_fid; 5287 req->VolatileFileId = io_parms->volatile_fid; 5288 req->WriteChannelInfoOffset = 0; 5289 req->WriteChannelInfoLength = 0; 5290 req->Channel = 0; 5291 req->Length = cpu_to_le32(io_parms->length); 5292 req->Offset = cpu_to_le64(io_parms->offset); 5293 req->DataOffset = cpu_to_le16( 5294 offsetof(struct smb2_write_req, Buffer)); 5295 req->RemainingBytes = 0; 5296 5297 trace_smb3_write_enter(0, 0, xid, io_parms->persistent_fid, 5298 io_parms->tcon->tid, io_parms->tcon->ses->Suid, 5299 io_parms->offset, io_parms->length); 5300 5301 iov[0].iov_base = (char *)req; 5302 /* 1 for Buffer */ 5303 iov[0].iov_len = total_len - 1; 5304 5305 memset(&rqst, 0, sizeof(struct smb_rqst)); 5306 rqst.rq_iov = iov; 5307 /* iov[0] is the SMB header; move payload to rq_iter for encryption safety */ 5308 rqst.rq_nvec = 1; 5309 iov_iter_kvec(&rqst.rq_iter, ITER_SOURCE, &iov[1], n_vec, 5310 io_parms->length); 5311 5312 if (retries) { 5313 /* Back-off before retry */ 5314 if (cur_sleep) 5315 msleep(cur_sleep); 5316 smb2_set_replay(server, &rqst); 5317 } 5318 5319 rc = cifs_send_recv(xid, io_parms->tcon->ses, server, 5320 &rqst, 5321 &resp_buftype, flags, &rsp_iov); 5322 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base; 5323 5324 if (rc) { 5325 trace_smb3_write_err(0, 0, xid, 5326 req->PersistentFileId, 5327 io_parms->tcon->tid, 5328 io_parms->tcon->ses->Suid, 5329 io_parms->offset, io_parms->length, rc); 5330 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE); 5331 cifs_dbg(VFS, "Send error in write = %d\n", rc); 5332 } else { 5333 *nbytes = le32_to_cpu(rsp->DataLength); 5334 cifs_stats_bytes_written(io_parms->tcon, *nbytes); 5335 trace_smb3_write_done(0, 0, xid, 5336 req->PersistentFileId, 5337 io_parms->tcon->tid, 5338 io_parms->tcon->ses->Suid, 5339 io_parms->offset, *nbytes); 5340 } 5341 5342 cifs_small_buf_release(req); 5343 free_rsp_buf(resp_buftype, rsp); 5344 5345 if (is_replayable_error(rc) && 5346 smb2_should_replay(io_parms->tcon, &retries, &cur_sleep)) 5347 goto replay_again; 5348 5349 return rc; 5350 } 5351 5352 int posix_info_sid_size(const void *beg, const void *end) 5353 { 5354 size_t subauth; 5355 int total; 5356 5357 if (beg + 1 > end) 5358 return -1; 5359 5360 subauth = *(u8 *)(beg+1); 5361 if (subauth < 1 || subauth > 15) 5362 return -1; 5363 5364 total = 1 + 1 + 6 + 4*subauth; 5365 if (beg + total > end) 5366 return -1; 5367 5368 return total; 5369 } 5370 5371 int posix_info_parse(const void *beg, const void *end, 5372 struct smb2_posix_info_parsed *out) 5373 5374 { 5375 int total_len = 0; 5376 int owner_len, group_len; 5377 int name_len; 5378 const void *owner_sid; 5379 const void *group_sid; 5380 const void *name; 5381 5382 /* if no end bound given, assume payload to be correct */ 5383 if (!end) { 5384 const struct smb2_posix_info *p = beg; 5385 5386 end = beg + le32_to_cpu(p->NextEntryOffset); 5387 /* last element will have a 0 offset, pick a sensible bound */ 5388 if (end == beg) 5389 end += 0xFFFF; 5390 } 5391 5392 /* check base buf */ 5393 if (beg + sizeof(struct smb2_posix_info) > end) 5394 return -1; 5395 total_len = sizeof(struct smb2_posix_info); 5396 5397 /* check owner sid */ 5398 owner_sid = beg + total_len; 5399 owner_len = posix_info_sid_size(owner_sid, end); 5400 if (owner_len < 0) 5401 return -1; 5402 total_len += owner_len; 5403 5404 /* check group sid */ 5405 group_sid = beg + total_len; 5406 group_len = posix_info_sid_size(group_sid, end); 5407 if (group_len < 0) 5408 return -1; 5409 total_len += group_len; 5410 5411 /* check name len */ 5412 if (beg + total_len + 4 > end) 5413 return -1; 5414 name_len = le32_to_cpu(*(__le32 *)(beg + total_len)); 5415 if (name_len < 1 || name_len > 0xFFFF) 5416 return -1; 5417 total_len += 4; 5418 5419 /* check name */ 5420 name = beg + total_len; 5421 if (name + name_len > end) 5422 return -1; 5423 total_len += name_len; 5424 5425 if (out) { 5426 out->base = beg; 5427 out->size = total_len; 5428 out->name_len = name_len; 5429 out->name = name; 5430 memcpy(&out->owner, owner_sid, owner_len); 5431 memcpy(&out->group, group_sid, group_len); 5432 } 5433 return total_len; 5434 } 5435 5436 static int posix_info_extra_size(const void *beg, const void *end) 5437 { 5438 int len = posix_info_parse(beg, end, NULL); 5439 5440 if (len < 0) 5441 return -1; 5442 return len - sizeof(struct smb2_posix_info); 5443 } 5444 5445 static unsigned int 5446 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry, 5447 size_t size) 5448 { 5449 int len; 5450 unsigned int entrycount = 0; 5451 unsigned int next_offset = 0; 5452 char *entryptr; 5453 FILE_DIRECTORY_INFO *dir_info; 5454 5455 if (bufstart == NULL) 5456 return 0; 5457 5458 entryptr = bufstart; 5459 5460 while (1) { 5461 if (entryptr + next_offset < entryptr || 5462 entryptr + next_offset > end_of_buf || 5463 entryptr + next_offset + size > end_of_buf) { 5464 cifs_dbg(VFS, "malformed search entry would overflow\n"); 5465 break; 5466 } 5467 5468 entryptr = entryptr + next_offset; 5469 dir_info = (FILE_DIRECTORY_INFO *)entryptr; 5470 5471 if (infotype == SMB_FIND_FILE_POSIX_INFO) 5472 len = posix_info_extra_size(entryptr, end_of_buf); 5473 else 5474 len = le32_to_cpu(dir_info->FileNameLength); 5475 5476 if (len < 0 || 5477 entryptr + len < entryptr || 5478 entryptr + len > end_of_buf || 5479 entryptr + len + size > end_of_buf) { 5480 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", 5481 end_of_buf); 5482 break; 5483 } 5484 5485 *lastentry = entryptr; 5486 entrycount++; 5487 5488 next_offset = le32_to_cpu(dir_info->NextEntryOffset); 5489 if (!next_offset) 5490 break; 5491 } 5492 5493 return entrycount; 5494 } 5495 5496 /* 5497 * Readdir/FindFirst 5498 */ 5499 int SMB2_query_directory_init(const unsigned int xid, 5500 struct cifs_tcon *tcon, 5501 struct TCP_Server_Info *server, 5502 struct smb_rqst *rqst, 5503 u64 persistent_fid, u64 volatile_fid, 5504 int index, int info_level) 5505 { 5506 struct smb2_query_directory_req *req; 5507 unsigned char *bufptr; 5508 __le16 asteriks = cpu_to_le16('*'); 5509 unsigned int output_size = CIFSMaxBufSize - 5510 MAX_SMB2_CREATE_RESPONSE_SIZE - 5511 MAX_SMB2_CLOSE_RESPONSE_SIZE; 5512 unsigned int total_len; 5513 struct kvec *iov = rqst->rq_iov; 5514 int len, rc; 5515 5516 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server, 5517 (void **) &req, &total_len); 5518 if (rc) 5519 return rc; 5520 5521 switch (info_level) { 5522 case SMB_FIND_FILE_DIRECTORY_INFO: 5523 req->FileInformationClass = FILE_DIRECTORY_INFORMATION; 5524 break; 5525 case SMB_FIND_FILE_ID_FULL_DIR_INFO: 5526 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION; 5527 break; 5528 case SMB_FIND_FILE_POSIX_INFO: 5529 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO; 5530 break; 5531 case SMB_FIND_FILE_FULL_DIRECTORY_INFO: 5532 req->FileInformationClass = FILE_FULL_DIRECTORY_INFORMATION; 5533 break; 5534 default: 5535 cifs_tcon_dbg(VFS, "info level %u isn't supported\n", 5536 info_level); 5537 return -EINVAL; 5538 } 5539 5540 req->FileIndex = cpu_to_le32(index); 5541 req->PersistentFileId = persistent_fid; 5542 req->VolatileFileId = volatile_fid; 5543 5544 len = 0x2; 5545 bufptr = req->Buffer; 5546 memcpy(bufptr, &asteriks, len); 5547 5548 req->FileNameOffset = 5549 cpu_to_le16(sizeof(struct smb2_query_directory_req)); 5550 req->FileNameLength = cpu_to_le16(len); 5551 /* 5552 * BB could be 30 bytes or so longer if we used SMB2 specific 5553 * buffer lengths, but this is safe and close enough. 5554 */ 5555 output_size = min_t(unsigned int, output_size, server->maxBuf); 5556 output_size = min_t(unsigned int, output_size, 2 << 15); 5557 req->OutputBufferLength = cpu_to_le32(output_size); 5558 5559 iov[0].iov_base = (char *)req; 5560 /* 1 for Buffer */ 5561 iov[0].iov_len = total_len - 1; 5562 5563 iov[1].iov_base = (char *)(req->Buffer); 5564 iov[1].iov_len = len; 5565 5566 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid, 5567 tcon->ses->Suid, index, output_size); 5568 5569 return 0; 5570 } 5571 5572 void SMB2_query_directory_free(struct smb_rqst *rqst) 5573 { 5574 if (rqst && rqst->rq_iov) { 5575 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */ 5576 } 5577 } 5578 5579 int 5580 smb2_parse_query_directory(struct cifs_tcon *tcon, 5581 struct kvec *rsp_iov, 5582 int resp_buftype, 5583 struct cifs_search_info *srch_inf) 5584 { 5585 struct smb2_query_directory_rsp *rsp; 5586 size_t info_buf_size; 5587 char *end_of_smb; 5588 int rc; 5589 5590 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base; 5591 5592 switch (srch_inf->info_level) { 5593 case SMB_FIND_FILE_DIRECTORY_INFO: 5594 info_buf_size = sizeof(FILE_DIRECTORY_INFO); 5595 break; 5596 case SMB_FIND_FILE_ID_FULL_DIR_INFO: 5597 info_buf_size = sizeof(FILE_ID_FULL_DIR_INFO); 5598 break; 5599 case SMB_FIND_FILE_POSIX_INFO: 5600 /* note that posix payload are variable size */ 5601 info_buf_size = sizeof(struct smb2_posix_info); 5602 break; 5603 case SMB_FIND_FILE_FULL_DIRECTORY_INFO: 5604 info_buf_size = sizeof(FILE_FULL_DIRECTORY_INFO); 5605 break; 5606 default: 5607 cifs_tcon_dbg(VFS, "info level %u isn't supported\n", 5608 srch_inf->info_level); 5609 return -EINVAL; 5610 } 5611 5612 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), 5613 le32_to_cpu(rsp->OutputBufferLength), rsp_iov, 5614 info_buf_size); 5615 if (rc) { 5616 cifs_tcon_dbg(VFS, "bad info payload"); 5617 return rc; 5618 } 5619 5620 srch_inf->unicode = true; 5621 5622 if (srch_inf->ntwrk_buf_start) { 5623 if (srch_inf->smallBuf) 5624 cifs_small_buf_release(srch_inf->ntwrk_buf_start); 5625 else 5626 cifs_buf_release(srch_inf->ntwrk_buf_start); 5627 } 5628 srch_inf->ntwrk_buf_start = (char *)rsp; 5629 srch_inf->srch_entries_start = srch_inf->last_entry = 5630 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset); 5631 end_of_smb = rsp_iov->iov_len + (char *)rsp; 5632 5633 srch_inf->entries_in_buffer = num_entries( 5634 srch_inf->info_level, 5635 srch_inf->srch_entries_start, 5636 end_of_smb, 5637 &srch_inf->last_entry, 5638 info_buf_size); 5639 5640 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer; 5641 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n", 5642 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry, 5643 srch_inf->srch_entries_start, srch_inf->last_entry); 5644 if (resp_buftype == CIFS_LARGE_BUFFER) 5645 srch_inf->smallBuf = false; 5646 else if (resp_buftype == CIFS_SMALL_BUFFER) 5647 srch_inf->smallBuf = true; 5648 else 5649 cifs_tcon_dbg(VFS, "Invalid search buffer type\n"); 5650 5651 return 0; 5652 } 5653 5654 int 5655 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, 5656 u64 persistent_fid, u64 volatile_fid, int index, 5657 struct cifs_search_info *srch_inf) 5658 { 5659 struct smb_rqst rqst; 5660 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE]; 5661 struct smb2_query_directory_rsp *rsp = NULL; 5662 int resp_buftype = CIFS_NO_BUFFER; 5663 struct kvec rsp_iov; 5664 int rc = 0; 5665 struct cifs_ses *ses = tcon->ses; 5666 struct TCP_Server_Info *server; 5667 int flags = 0; 5668 int retries = 0, cur_sleep = 0; 5669 5670 replay_again: 5671 /* reinitialize for possible replay */ 5672 flags = 0; 5673 server = cifs_pick_channel(ses); 5674 5675 if (!ses || !(ses->server)) 5676 return smb_EIO(smb_eio_trace_null_pointers); 5677 5678 if (smb3_encryption_required(tcon)) 5679 flags |= CIFS_TRANSFORM_REQ; 5680 5681 memset(&rqst, 0, sizeof(struct smb_rqst)); 5682 memset(&iov, 0, sizeof(iov)); 5683 rqst.rq_iov = iov; 5684 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE; 5685 5686 rc = SMB2_query_directory_init(xid, tcon, server, 5687 &rqst, persistent_fid, 5688 volatile_fid, index, 5689 srch_inf->info_level); 5690 if (rc) 5691 goto qdir_exit; 5692 5693 if (retries) { 5694 /* Back-off before retry */ 5695 if (cur_sleep) 5696 msleep(cur_sleep); 5697 smb2_set_replay(server, &rqst); 5698 } 5699 5700 rc = cifs_send_recv(xid, ses, server, 5701 &rqst, &resp_buftype, flags, &rsp_iov); 5702 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base; 5703 5704 if (rc) { 5705 if (rc == -ENODATA && 5706 rsp->hdr.Status == STATUS_NO_MORE_FILES) { 5707 trace_smb3_query_dir_done(xid, persistent_fid, 5708 tcon->tid, tcon->ses->Suid, index, 0); 5709 srch_inf->endOfSearch = true; 5710 rc = 0; 5711 } else { 5712 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, 5713 tcon->ses->Suid, index, 0, rc); 5714 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE); 5715 } 5716 goto qdir_exit; 5717 } 5718 5719 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype, 5720 srch_inf); 5721 if (rc) { 5722 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid, 5723 tcon->ses->Suid, index, 0, rc); 5724 goto qdir_exit; 5725 } 5726 resp_buftype = CIFS_NO_BUFFER; 5727 5728 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid, 5729 tcon->ses->Suid, index, srch_inf->entries_in_buffer); 5730 5731 qdir_exit: 5732 SMB2_query_directory_free(&rqst); 5733 free_rsp_buf(resp_buftype, rsp); 5734 5735 if (is_replayable_error(rc) && 5736 smb2_should_replay(tcon, &retries, &cur_sleep)) 5737 goto replay_again; 5738 5739 return rc; 5740 } 5741 5742 int 5743 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server, 5744 struct smb_rqst *rqst, 5745 u64 persistent_fid, u64 volatile_fid, u32 pid, 5746 u8 info_class, u8 info_type, u32 additional_info, 5747 void **data, unsigned int *size) 5748 { 5749 struct smb2_set_info_req *req; 5750 struct kvec *iov = rqst->rq_iov; 5751 unsigned int i, total_len; 5752 int rc; 5753 5754 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server, 5755 (void **) &req, &total_len); 5756 if (rc) 5757 return rc; 5758 5759 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid); 5760 req->InfoType = info_type; 5761 req->FileInfoClass = info_class; 5762 req->PersistentFileId = persistent_fid; 5763 req->VolatileFileId = volatile_fid; 5764 req->AdditionalInformation = cpu_to_le32(additional_info); 5765 5766 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req)); 5767 req->BufferLength = cpu_to_le32(*size); 5768 5769 memcpy(req->Buffer, *data, *size); 5770 total_len += *size; 5771 5772 iov[0].iov_base = (char *)req; 5773 /* 1 for Buffer */ 5774 iov[0].iov_len = total_len - 1; 5775 5776 for (i = 1; i < rqst->rq_nvec; i++) { 5777 le32_add_cpu(&req->BufferLength, size[i]); 5778 iov[i].iov_base = (char *)data[i]; 5779 iov[i].iov_len = size[i]; 5780 } 5781 5782 return 0; 5783 } 5784 5785 void 5786 SMB2_set_info_free(struct smb_rqst *rqst) 5787 { 5788 if (rqst && rqst->rq_iov) 5789 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */ 5790 } 5791 5792 static int 5793 send_set_info(const unsigned int xid, struct cifs_tcon *tcon, 5794 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class, 5795 u8 info_type, u32 additional_info, unsigned int num, 5796 void **data, unsigned int *size) 5797 { 5798 struct smb_rqst rqst; 5799 struct smb2_set_info_rsp *rsp = NULL; 5800 struct kvec *iov; 5801 struct kvec rsp_iov; 5802 int rc = 0; 5803 int resp_buftype; 5804 struct cifs_ses *ses = tcon->ses; 5805 struct TCP_Server_Info *server; 5806 int flags = 0; 5807 int retries = 0, cur_sleep = 0; 5808 5809 replay_again: 5810 /* reinitialize for possible replay */ 5811 flags = 0; 5812 server = cifs_pick_channel(ses); 5813 5814 if (!ses || !server) 5815 return smb_EIO(smb_eio_trace_null_pointers); 5816 5817 if (!num) 5818 return -EINVAL; 5819 5820 if (smb3_encryption_required(tcon)) 5821 flags |= CIFS_TRANSFORM_REQ; 5822 5823 iov = kmalloc_objs(struct kvec, num); 5824 if (!iov) 5825 return -ENOMEM; 5826 5827 memset(&rqst, 0, sizeof(struct smb_rqst)); 5828 rqst.rq_iov = iov; 5829 rqst.rq_nvec = num; 5830 5831 rc = SMB2_set_info_init(tcon, server, 5832 &rqst, persistent_fid, volatile_fid, pid, 5833 info_class, info_type, additional_info, 5834 data, size); 5835 if (rc) { 5836 kfree(iov); 5837 return rc; 5838 } 5839 5840 if (retries) { 5841 /* Back-off before retry */ 5842 if (cur_sleep) 5843 msleep(cur_sleep); 5844 smb2_set_replay(server, &rqst); 5845 } 5846 5847 rc = cifs_send_recv(xid, ses, server, 5848 &rqst, &resp_buftype, flags, 5849 &rsp_iov); 5850 SMB2_set_info_free(&rqst); 5851 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base; 5852 5853 if (rc != 0) { 5854 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE); 5855 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid, 5856 ses->Suid, info_class, (__u32)info_type, rc); 5857 } 5858 5859 free_rsp_buf(resp_buftype, rsp); 5860 kfree(iov); 5861 5862 if (is_replayable_error(rc) && 5863 smb2_should_replay(tcon, &retries, &cur_sleep)) 5864 goto replay_again; 5865 5866 return rc; 5867 } 5868 5869 int 5870 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, 5871 u64 volatile_fid, u32 pid, loff_t new_eof) 5872 { 5873 struct smb2_file_eof_info info; 5874 void *data; 5875 unsigned int size; 5876 5877 info.EndOfFile = cpu_to_le64(new_eof); 5878 5879 data = &info; 5880 size = sizeof(struct smb2_file_eof_info); 5881 5882 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, new_eof); 5883 5884 return send_set_info(xid, tcon, persistent_fid, volatile_fid, 5885 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE, 5886 0, 1, &data, &size); 5887 } 5888 5889 int 5890 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon, 5891 u64 persistent_fid, u64 volatile_fid, 5892 struct smb_ntsd *pnntsd, int pacllen, int aclflag) 5893 { 5894 return send_set_info(xid, tcon, persistent_fid, volatile_fid, 5895 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag, 5896 1, (void **)&pnntsd, &pacllen); 5897 } 5898 5899 int 5900 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, 5901 u64 persistent_fid, u64 volatile_fid, 5902 struct smb2_file_full_ea_info *buf, int len) 5903 { 5904 return send_set_info(xid, tcon, persistent_fid, volatile_fid, 5905 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 5906 0, 1, (void **)&buf, &len); 5907 } 5908 5909 int 5910 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, 5911 const u64 persistent_fid, const u64 volatile_fid, 5912 __u8 oplock_level) 5913 { 5914 struct smb_rqst rqst; 5915 int rc; 5916 struct smb2_oplock_break *req = NULL; 5917 struct cifs_ses *ses = tcon->ses; 5918 struct TCP_Server_Info *server; 5919 int flags = CIFS_OBREAK_OP; 5920 unsigned int total_len; 5921 struct kvec iov[1]; 5922 struct kvec rsp_iov; 5923 int resp_buf_type; 5924 int retries = 0, cur_sleep = 0; 5925 5926 replay_again: 5927 /* reinitialize for possible replay */ 5928 flags = CIFS_OBREAK_OP; 5929 server = cifs_pick_channel(ses); 5930 5931 cifs_dbg(FYI, "SMB2_oplock_break\n"); 5932 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, 5933 (void **) &req, &total_len); 5934 if (rc) 5935 return rc; 5936 5937 if (smb3_encryption_required(tcon)) 5938 flags |= CIFS_TRANSFORM_REQ; 5939 5940 req->VolatileFid = volatile_fid; 5941 req->PersistentFid = persistent_fid; 5942 req->OplockLevel = oplock_level; 5943 req->hdr.CreditRequest = cpu_to_le16(1); 5944 5945 flags |= CIFS_NO_RSP_BUF; 5946 5947 iov[0].iov_base = (char *)req; 5948 iov[0].iov_len = total_len; 5949 5950 memset(&rqst, 0, sizeof(struct smb_rqst)); 5951 rqst.rq_iov = iov; 5952 rqst.rq_nvec = 1; 5953 5954 if (retries) { 5955 /* Back-off before retry */ 5956 if (cur_sleep) 5957 msleep(cur_sleep); 5958 smb2_set_replay(server, &rqst); 5959 } 5960 5961 rc = cifs_send_recv(xid, ses, server, 5962 &rqst, &resp_buf_type, flags, &rsp_iov); 5963 cifs_small_buf_release(req); 5964 if (rc) { 5965 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); 5966 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc); 5967 } 5968 5969 if (is_replayable_error(rc) && 5970 smb2_should_replay(tcon, &retries, &cur_sleep)) 5971 goto replay_again; 5972 5973 return rc; 5974 } 5975 5976 void 5977 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, 5978 struct kstatfs *kst) 5979 { 5980 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) * 5981 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit); 5982 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits); 5983 kst->f_bfree = kst->f_bavail = 5984 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits); 5985 return; 5986 } 5987 5988 static void 5989 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data, 5990 struct kstatfs *kst) 5991 { 5992 kst->f_bsize = le32_to_cpu(response_data->BlockSize); 5993 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks); 5994 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail); 5995 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) 5996 kst->f_bavail = kst->f_bfree; 5997 else 5998 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail); 5999 if (response_data->TotalFileNodes != cpu_to_le64(-1)) 6000 kst->f_files = le64_to_cpu(response_data->TotalFileNodes); 6001 if (response_data->FreeFileNodes != cpu_to_le64(-1)) 6002 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes); 6003 6004 return; 6005 } 6006 6007 static int 6008 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, 6009 struct TCP_Server_Info *server, 6010 int level, int outbuf_len, u64 persistent_fid, 6011 u64 volatile_fid) 6012 { 6013 int rc; 6014 struct smb2_query_info_req *req; 6015 unsigned int total_len; 6016 6017 cifs_dbg(FYI, "Query FSInfo level %d\n", level); 6018 6019 if ((tcon->ses == NULL) || server == NULL) 6020 return smb_EIO(smb_eio_trace_null_pointers); 6021 6022 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server, 6023 (void **) &req, &total_len); 6024 if (rc) 6025 return rc; 6026 6027 req->InfoType = SMB2_O_INFO_FILESYSTEM; 6028 req->FileInfoClass = level; 6029 req->PersistentFileId = persistent_fid; 6030 req->VolatileFileId = volatile_fid; 6031 /* 1 for pad */ 6032 req->InputBufferOffset = 6033 cpu_to_le16(sizeof(struct smb2_query_info_req)); 6034 req->OutputBufferLength = cpu_to_le32( 6035 outbuf_len + sizeof(struct smb2_query_info_rsp)); 6036 6037 iov->iov_base = (char *)req; 6038 iov->iov_len = total_len; 6039 return 0; 6040 } 6041 6042 static inline void free_qfs_info_req(struct kvec *iov) 6043 { 6044 cifs_buf_release(iov->iov_base); 6045 } 6046 6047 int 6048 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon, 6049 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) 6050 { 6051 struct smb_rqst rqst; 6052 struct smb2_query_info_rsp *rsp = NULL; 6053 struct kvec iov; 6054 struct kvec rsp_iov; 6055 int rc = 0; 6056 int resp_buftype; 6057 struct cifs_ses *ses = tcon->ses; 6058 struct TCP_Server_Info *server; 6059 FILE_SYSTEM_POSIX_INFO *info = NULL; 6060 int flags = 0; 6061 int retries = 0, cur_sleep = 0; 6062 6063 replay_again: 6064 /* reinitialize for possible replay */ 6065 flags = 0; 6066 server = cifs_pick_channel(ses); 6067 6068 rc = build_qfs_info_req(&iov, tcon, server, 6069 FS_POSIX_INFORMATION, 6070 sizeof(FILE_SYSTEM_POSIX_INFO), 6071 persistent_fid, volatile_fid); 6072 if (rc) 6073 return rc; 6074 6075 if (smb3_encryption_required(tcon)) 6076 flags |= CIFS_TRANSFORM_REQ; 6077 6078 memset(&rqst, 0, sizeof(struct smb_rqst)); 6079 rqst.rq_iov = &iov; 6080 rqst.rq_nvec = 1; 6081 6082 if (retries) { 6083 /* Back-off before retry */ 6084 if (cur_sleep) 6085 msleep(cur_sleep); 6086 smb2_set_replay(server, &rqst); 6087 } 6088 6089 rc = cifs_send_recv(xid, ses, server, 6090 &rqst, &resp_buftype, flags, &rsp_iov); 6091 free_qfs_info_req(&iov); 6092 if (rc) { 6093 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); 6094 goto posix_qfsinf_exit; 6095 } 6096 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; 6097 6098 info = (FILE_SYSTEM_POSIX_INFO *)( 6099 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp); 6100 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset), 6101 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov, 6102 sizeof(FILE_SYSTEM_POSIX_INFO)); 6103 if (!rc) 6104 copy_posix_fs_info_to_kstatfs(info, fsdata); 6105 6106 posix_qfsinf_exit: 6107 free_rsp_buf(resp_buftype, rsp_iov.iov_base); 6108 6109 if (is_replayable_error(rc) && 6110 smb2_should_replay(tcon, &retries, &cur_sleep)) 6111 goto replay_again; 6112 6113 return rc; 6114 } 6115 6116 int 6117 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, 6118 u64 persistent_fid, u64 volatile_fid, int level) 6119 { 6120 struct smb_rqst rqst; 6121 struct smb2_query_info_rsp *rsp = NULL; 6122 struct kvec iov; 6123 struct kvec rsp_iov; 6124 int rc = 0; 6125 int resp_buftype, max_len, min_len; 6126 struct cifs_ses *ses = tcon->ses; 6127 struct TCP_Server_Info *server; 6128 unsigned int rsp_len, offset; 6129 int flags = 0; 6130 int retries = 0, cur_sleep = 0; 6131 6132 replay_again: 6133 /* reinitialize for possible replay */ 6134 flags = 0; 6135 server = cifs_pick_channel(ses); 6136 6137 if (level == FS_DEVICE_INFORMATION) { 6138 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO); 6139 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO); 6140 } else if (level == FS_ATTRIBUTE_INFORMATION) { 6141 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO) + MAX_FS_NAME_LEN; 6142 min_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO); 6143 } else if (level == FS_SECTOR_SIZE_INFORMATION) { 6144 max_len = sizeof(struct smb3_fs_ss_info); 6145 min_len = sizeof(struct smb3_fs_ss_info); 6146 } else if (level == FS_VOLUME_INFORMATION) { 6147 max_len = sizeof(struct filesystem_vol_info) + MAX_VOL_LABEL_LEN; 6148 min_len = sizeof(struct filesystem_vol_info); 6149 } else { 6150 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level); 6151 return -EINVAL; 6152 } 6153 6154 rc = build_qfs_info_req(&iov, tcon, server, 6155 level, max_len, 6156 persistent_fid, volatile_fid); 6157 if (rc) 6158 return rc; 6159 6160 if (smb3_encryption_required(tcon)) 6161 flags |= CIFS_TRANSFORM_REQ; 6162 6163 memset(&rqst, 0, sizeof(struct smb_rqst)); 6164 rqst.rq_iov = &iov; 6165 rqst.rq_nvec = 1; 6166 6167 if (retries) { 6168 /* Back-off before retry */ 6169 if (cur_sleep) 6170 msleep(cur_sleep); 6171 smb2_set_replay(server, &rqst); 6172 } 6173 6174 rc = cifs_send_recv(xid, ses, server, 6175 &rqst, &resp_buftype, flags, &rsp_iov); 6176 free_qfs_info_req(&iov); 6177 if (rc) { 6178 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); 6179 goto qfsattr_exit; 6180 } 6181 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; 6182 6183 rsp_len = le32_to_cpu(rsp->OutputBufferLength); 6184 offset = le16_to_cpu(rsp->OutputBufferOffset); 6185 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len); 6186 if (rc) 6187 goto qfsattr_exit; 6188 6189 if (level == FS_ATTRIBUTE_INFORMATION) 6190 memcpy(&tcon->fsAttrInfo, offset 6191 + (char *)rsp, min_t(unsigned int, 6192 rsp_len, min_len)); 6193 else if (level == FS_DEVICE_INFORMATION) 6194 memcpy(&tcon->fsDevInfo, offset 6195 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO)); 6196 else if (level == FS_SECTOR_SIZE_INFORMATION) { 6197 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *) 6198 (offset + (char *)rsp); 6199 tcon->ss_flags = le32_to_cpu(ss_info->Flags); 6200 tcon->perf_sector_size = 6201 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf); 6202 } else if (level == FS_VOLUME_INFORMATION) { 6203 struct filesystem_vol_info *vol_info = (struct filesystem_vol_info *) 6204 (offset + (char *)rsp); 6205 tcon->vol_serial_number = le32_to_cpu(vol_info->VolumeSerialNumber); 6206 tcon->vol_create_time = vol_info->VolumeCreationTime; 6207 } 6208 6209 qfsattr_exit: 6210 free_rsp_buf(resp_buftype, rsp_iov.iov_base); 6211 6212 if (is_replayable_error(rc) && 6213 smb2_should_replay(tcon, &retries, &cur_sleep)) 6214 goto replay_again; 6215 6216 return rc; 6217 } 6218 6219 int 6220 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, 6221 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, 6222 const __u32 num_lock, struct smb2_lock_element *buf) 6223 { 6224 struct smb_rqst rqst; 6225 int rc = 0; 6226 struct smb2_lock_req *req = NULL; 6227 struct kvec iov[2]; 6228 struct kvec rsp_iov; 6229 int resp_buf_type; 6230 unsigned int count; 6231 int flags = CIFS_NO_RSP_BUF; 6232 unsigned int total_len; 6233 struct TCP_Server_Info *server; 6234 int retries = 0, cur_sleep = 0; 6235 6236 replay_again: 6237 /* reinitialize for possible replay */ 6238 flags = CIFS_NO_RSP_BUF; 6239 server = cifs_pick_channel(tcon->ses); 6240 6241 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); 6242 6243 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server, 6244 (void **) &req, &total_len); 6245 if (rc) 6246 return rc; 6247 6248 if (smb3_encryption_required(tcon)) 6249 flags |= CIFS_TRANSFORM_REQ; 6250 6251 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid); 6252 req->LockCount = cpu_to_le16(num_lock); 6253 6254 req->PersistentFileId = persist_fid; 6255 req->VolatileFileId = volatile_fid; 6256 6257 count = num_lock * sizeof(struct smb2_lock_element); 6258 6259 iov[0].iov_base = (char *)req; 6260 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element); 6261 iov[1].iov_base = (char *)buf; 6262 iov[1].iov_len = count; 6263 6264 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); 6265 6266 memset(&rqst, 0, sizeof(struct smb_rqst)); 6267 rqst.rq_iov = iov; 6268 rqst.rq_nvec = 2; 6269 6270 if (retries) { 6271 /* Back-off before retry */ 6272 if (cur_sleep) 6273 msleep(cur_sleep); 6274 smb2_set_replay(server, &rqst); 6275 } 6276 6277 trace_smb3_lock_enter(xid, persist_fid, tcon->tid, tcon->ses->Suid, 6278 le64_to_cpu(buf[0].Offset), 6279 le64_to_cpu(buf[0].Length), 6280 le32_to_cpu(buf[0].Flags), num_lock, 0); 6281 6282 rc = cifs_send_recv(xid, tcon->ses, server, 6283 &rqst, &resp_buf_type, flags, 6284 &rsp_iov); 6285 cifs_small_buf_release(req); 6286 if (rc) { 6287 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc); 6288 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE); 6289 trace_smb3_lock_err(xid, persist_fid, tcon->tid, 6290 tcon->ses->Suid, 6291 le64_to_cpu(buf[0].Offset), 6292 le64_to_cpu(buf[0].Length), 6293 le32_to_cpu(buf[0].Flags), num_lock, rc); 6294 } else { 6295 trace_smb3_lock_done(xid, persist_fid, tcon->tid, tcon->ses->Suid, 6296 le64_to_cpu(buf[0].Offset), 6297 le64_to_cpu(buf[0].Length), 6298 le32_to_cpu(buf[0].Flags), num_lock, 0); 6299 } 6300 6301 if (is_replayable_error(rc) && 6302 smb2_should_replay(tcon, &retries, &cur_sleep)) 6303 goto replay_again; 6304 6305 return rc; 6306 } 6307 6308 int 6309 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon, 6310 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, 6311 const __u64 length, const __u64 offset, const __u32 lock_flags, 6312 const bool wait) 6313 { 6314 struct smb2_lock_element lock; 6315 6316 lock.Offset = cpu_to_le64(offset); 6317 lock.Length = cpu_to_le64(length); 6318 lock.Flags = cpu_to_le32(lock_flags); 6319 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK) 6320 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY); 6321 6322 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock); 6323 } 6324 6325 int 6326 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, 6327 __u8 *lease_key, const __le32 lease_state) 6328 { 6329 struct smb_rqst rqst; 6330 int rc; 6331 struct smb2_lease_ack *req = NULL; 6332 struct cifs_ses *ses = tcon->ses; 6333 int flags = CIFS_OBREAK_OP; 6334 unsigned int total_len; 6335 struct kvec iov[1]; 6336 struct kvec rsp_iov; 6337 int resp_buf_type; 6338 __u64 *please_key_high; 6339 __u64 *please_key_low; 6340 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses); 6341 6342 cifs_dbg(FYI, "SMB2_lease_break\n"); 6343 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, 6344 (void **) &req, &total_len); 6345 if (rc) 6346 return rc; 6347 6348 if (smb3_encryption_required(tcon)) 6349 flags |= CIFS_TRANSFORM_REQ; 6350 6351 req->hdr.CreditRequest = cpu_to_le16(1); 6352 req->StructureSize = cpu_to_le16(36); 6353 total_len += 12; 6354 6355 memcpy(req->LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE); 6356 req->LeaseState = lease_state; 6357 6358 flags |= CIFS_NO_RSP_BUF; 6359 6360 iov[0].iov_base = (char *)req; 6361 iov[0].iov_len = total_len; 6362 6363 memset(&rqst, 0, sizeof(struct smb_rqst)); 6364 rqst.rq_iov = iov; 6365 rqst.rq_nvec = 1; 6366 6367 rc = cifs_send_recv(xid, ses, server, 6368 &rqst, &resp_buf_type, flags, &rsp_iov); 6369 cifs_small_buf_release(req); 6370 6371 please_key_low = (__u64 *)lease_key; 6372 please_key_high = (__u64 *)(lease_key+8); 6373 if (rc) { 6374 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); 6375 trace_smb3_lease_ack_err(le32_to_cpu(lease_state), tcon->tid, 6376 ses->Suid, *please_key_low, *please_key_high, rc); 6377 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc); 6378 } else 6379 trace_smb3_lease_ack_done(le32_to_cpu(lease_state), tcon->tid, 6380 ses->Suid, *please_key_low, *please_key_high); 6381 6382 return rc; 6383 } 6384