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