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