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