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