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