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