1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * Copyright (C) International Business Machines Corp., 2002,2011 5 * Author(s): Steve French (sfrench@us.ibm.com) 6 * 7 */ 8 #include <linux/fs.h> 9 #include <linux/net.h> 10 #include <linux/string.h> 11 #include <linux/sched/mm.h> 12 #include <linux/sched/signal.h> 13 #include <linux/list.h> 14 #include <linux/wait.h> 15 #include <linux/slab.h> 16 #include <linux/pagemap.h> 17 #include <linux/ctype.h> 18 #include <linux/utsname.h> 19 #include <linux/mempool.h> 20 #include <linux/delay.h> 21 #include <linux/completion.h> 22 #include <linux/kthread.h> 23 #include <linux/pagevec.h> 24 #include <linux/freezer.h> 25 #include <linux/namei.h> 26 #include <linux/uuid.h> 27 #include <linux/uaccess.h> 28 #include <asm/processor.h> 29 #include <linux/inet.h> 30 #include <linux/module.h> 31 #include <keys/user-type.h> 32 #include <net/ipv6.h> 33 #include <linux/parser.h> 34 #include <linux/bvec.h> 35 #include "cifspdu.h" 36 #include "cifsglob.h" 37 #include "cifsproto.h" 38 #include "cifs_unicode.h" 39 #include "cifs_debug.h" 40 #include "cifs_fs_sb.h" 41 #include "ntlmssp.h" 42 #include "nterr.h" 43 #include "rfc1002pdu.h" 44 #include "fscache.h" 45 #include "smb2proto.h" 46 #include "smbdirect.h" 47 #include "dns_resolve.h" 48 #ifdef CONFIG_CIFS_DFS_UPCALL 49 #include "dfs.h" 50 #include "dfs_cache.h" 51 #endif 52 #include "fs_context.h" 53 #include "cifs_swn.h" 54 55 /* FIXME: should these be tunable? */ 56 #define TLINK_ERROR_EXPIRE (1 * HZ) 57 #define TLINK_IDLE_EXPIRE (600 * HZ) 58 59 /* Drop the connection to not overload the server */ 60 #define MAX_STATUS_IO_TIMEOUT 5 61 62 static int ip_connect(struct TCP_Server_Info *server); 63 static int generic_ip_connect(struct TCP_Server_Info *server); 64 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink); 65 static void cifs_prune_tlinks(struct work_struct *work); 66 67 /* 68 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may 69 * get their ip addresses changed at some point. 70 * 71 * This should be called with server->srv_mutex held. 72 */ 73 static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server) 74 { 75 int rc; 76 int len; 77 char *unc; 78 struct sockaddr_storage ss; 79 80 if (!server->hostname) 81 return -EINVAL; 82 83 /* if server hostname isn't populated, there's nothing to do here */ 84 if (server->hostname[0] == '\0') 85 return 0; 86 87 len = strlen(server->hostname) + 3; 88 89 unc = kmalloc(len, GFP_KERNEL); 90 if (!unc) { 91 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__); 92 return -ENOMEM; 93 } 94 scnprintf(unc, len, "\\\\%s", server->hostname); 95 96 spin_lock(&server->srv_lock); 97 ss = server->dstaddr; 98 spin_unlock(&server->srv_lock); 99 100 rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL); 101 kfree(unc); 102 103 if (rc < 0) { 104 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n", 105 __func__, server->hostname, rc); 106 } else { 107 spin_lock(&server->srv_lock); 108 memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr)); 109 spin_unlock(&server->srv_lock); 110 rc = 0; 111 } 112 113 return rc; 114 } 115 116 static void smb2_query_server_interfaces(struct work_struct *work) 117 { 118 int rc; 119 int xid; 120 struct cifs_tcon *tcon = container_of(work, 121 struct cifs_tcon, 122 query_interfaces.work); 123 struct TCP_Server_Info *server = tcon->ses->server; 124 125 /* 126 * query server network interfaces, in case they change 127 */ 128 if (!server->ops->query_server_interfaces) 129 return; 130 131 xid = get_xid(); 132 rc = server->ops->query_server_interfaces(xid, tcon, false); 133 free_xid(xid); 134 135 if (rc) { 136 if (rc == -EOPNOTSUPP) 137 return; 138 139 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n", 140 __func__, rc); 141 } 142 143 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces, 144 (SMB_INTERFACE_POLL_INTERVAL * HZ)); 145 } 146 147 /* 148 * Update the tcpStatus for the server. 149 * This is used to signal the cifsd thread to call cifs_reconnect 150 * ONLY cifsd thread should call cifs_reconnect. For any other 151 * thread, use this function 152 * 153 * @server: the tcp ses for which reconnect is needed 154 * @all_channels: if this needs to be done for all channels 155 */ 156 void 157 cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server, 158 bool all_channels) 159 { 160 struct TCP_Server_Info *pserver; 161 struct cifs_ses *ses; 162 int i; 163 164 /* If server is a channel, select the primary channel */ 165 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 166 167 /* if we need to signal just this channel */ 168 if (!all_channels) { 169 spin_lock(&server->srv_lock); 170 if (server->tcpStatus != CifsExiting) 171 server->tcpStatus = CifsNeedReconnect; 172 spin_unlock(&server->srv_lock); 173 return; 174 } 175 176 spin_lock(&cifs_tcp_ses_lock); 177 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 178 if (cifs_ses_exiting(ses)) 179 continue; 180 spin_lock(&ses->chan_lock); 181 for (i = 0; i < ses->chan_count; i++) { 182 if (!ses->chans[i].server) 183 continue; 184 185 spin_lock(&ses->chans[i].server->srv_lock); 186 if (ses->chans[i].server->tcpStatus != CifsExiting) 187 ses->chans[i].server->tcpStatus = CifsNeedReconnect; 188 spin_unlock(&ses->chans[i].server->srv_lock); 189 } 190 spin_unlock(&ses->chan_lock); 191 } 192 spin_unlock(&cifs_tcp_ses_lock); 193 } 194 195 /* 196 * Mark all sessions and tcons for reconnect. 197 * IMPORTANT: make sure that this gets called only from 198 * cifsd thread. For any other thread, use 199 * cifs_signal_cifsd_for_reconnect 200 * 201 * @server: the tcp ses for which reconnect is needed 202 * @server needs to be previously set to CifsNeedReconnect. 203 * @mark_smb_session: whether even sessions need to be marked 204 */ 205 void 206 cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server, 207 bool mark_smb_session) 208 { 209 struct TCP_Server_Info *pserver; 210 struct cifs_ses *ses, *nses; 211 struct cifs_tcon *tcon; 212 213 /* 214 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they 215 * are not used until reconnected. 216 */ 217 cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__); 218 219 /* If server is a channel, select the primary channel */ 220 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 221 222 /* 223 * if the server has been marked for termination, there is a 224 * chance that the remaining channels all need reconnect. To be 225 * on the safer side, mark the session and trees for reconnect 226 * for this scenario. This might cause a few redundant session 227 * setup and tree connect requests, but it is better than not doing 228 * a tree connect when needed, and all following requests failing 229 */ 230 if (server->terminate) { 231 mark_smb_session = true; 232 server = pserver; 233 } 234 235 spin_lock(&cifs_tcp_ses_lock); 236 list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) { 237 spin_lock(&ses->ses_lock); 238 if (ses->ses_status == SES_EXITING) { 239 spin_unlock(&ses->ses_lock); 240 continue; 241 } 242 spin_unlock(&ses->ses_lock); 243 244 spin_lock(&ses->chan_lock); 245 if (cifs_ses_get_chan_index(ses, server) == 246 CIFS_INVAL_CHAN_INDEX) { 247 spin_unlock(&ses->chan_lock); 248 continue; 249 } 250 251 if (!cifs_chan_is_iface_active(ses, server)) { 252 spin_unlock(&ses->chan_lock); 253 cifs_chan_update_iface(ses, server); 254 spin_lock(&ses->chan_lock); 255 } 256 257 if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) { 258 spin_unlock(&ses->chan_lock); 259 continue; 260 } 261 262 if (mark_smb_session) 263 CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses); 264 else 265 cifs_chan_set_need_reconnect(ses, server); 266 267 cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n", 268 __func__, ses->chans_need_reconnect); 269 270 /* If all channels need reconnect, then tcon needs reconnect */ 271 if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) { 272 spin_unlock(&ses->chan_lock); 273 continue; 274 } 275 spin_unlock(&ses->chan_lock); 276 277 spin_lock(&ses->ses_lock); 278 ses->ses_status = SES_NEED_RECON; 279 spin_unlock(&ses->ses_lock); 280 281 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 282 tcon->need_reconnect = true; 283 spin_lock(&tcon->tc_lock); 284 tcon->status = TID_NEED_RECON; 285 spin_unlock(&tcon->tc_lock); 286 287 cancel_delayed_work(&tcon->query_interfaces); 288 } 289 if (ses->tcon_ipc) { 290 ses->tcon_ipc->need_reconnect = true; 291 spin_lock(&ses->tcon_ipc->tc_lock); 292 ses->tcon_ipc->status = TID_NEED_RECON; 293 spin_unlock(&ses->tcon_ipc->tc_lock); 294 } 295 } 296 spin_unlock(&cifs_tcp_ses_lock); 297 } 298 299 static void 300 cifs_abort_connection(struct TCP_Server_Info *server) 301 { 302 struct mid_q_entry *mid, *nmid; 303 struct list_head retry_list; 304 305 server->maxBuf = 0; 306 server->max_read = 0; 307 308 /* do not want to be sending data on a socket we are freeing */ 309 cifs_dbg(FYI, "%s: tearing down socket\n", __func__); 310 cifs_server_lock(server); 311 if (server->ssocket) { 312 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state, 313 server->ssocket->flags); 314 kernel_sock_shutdown(server->ssocket, SHUT_WR); 315 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state, 316 server->ssocket->flags); 317 sock_release(server->ssocket); 318 server->ssocket = NULL; 319 } 320 server->sequence_number = 0; 321 server->session_estab = false; 322 kfree_sensitive(server->session_key.response); 323 server->session_key.response = NULL; 324 server->session_key.len = 0; 325 server->lstrp = jiffies; 326 327 /* mark submitted MIDs for retry and issue callback */ 328 INIT_LIST_HEAD(&retry_list); 329 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__); 330 spin_lock(&server->mid_lock); 331 list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) { 332 kref_get(&mid->refcount); 333 if (mid->mid_state == MID_REQUEST_SUBMITTED) 334 mid->mid_state = MID_RETRY_NEEDED; 335 list_move(&mid->qhead, &retry_list); 336 mid->mid_flags |= MID_DELETED; 337 } 338 spin_unlock(&server->mid_lock); 339 cifs_server_unlock(server); 340 341 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__); 342 list_for_each_entry_safe(mid, nmid, &retry_list, qhead) { 343 list_del_init(&mid->qhead); 344 mid->callback(mid); 345 release_mid(mid); 346 } 347 348 if (cifs_rdma_enabled(server)) { 349 cifs_server_lock(server); 350 smbd_destroy(server); 351 cifs_server_unlock(server); 352 } 353 } 354 355 static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets) 356 { 357 spin_lock(&server->srv_lock); 358 server->nr_targets = num_targets; 359 if (server->tcpStatus == CifsExiting) { 360 /* the demux thread will exit normally next time through the loop */ 361 spin_unlock(&server->srv_lock); 362 wake_up(&server->response_q); 363 return false; 364 } 365 366 cifs_dbg(FYI, "Mark tcp session as need reconnect\n"); 367 trace_smb3_reconnect(server->CurrentMid, server->conn_id, 368 server->hostname); 369 server->tcpStatus = CifsNeedReconnect; 370 371 spin_unlock(&server->srv_lock); 372 return true; 373 } 374 375 /* 376 * cifs tcp session reconnection 377 * 378 * mark tcp session as reconnecting so temporarily locked 379 * mark all smb sessions as reconnecting for tcp session 380 * reconnect tcp session 381 * wake up waiters on reconnection? - (not needed currently) 382 * 383 * if mark_smb_session is passed as true, unconditionally mark 384 * the smb session (and tcon) for reconnect as well. This value 385 * doesn't really matter for non-multichannel scenario. 386 * 387 */ 388 static int __cifs_reconnect(struct TCP_Server_Info *server, 389 bool mark_smb_session) 390 { 391 int rc = 0; 392 393 if (!cifs_tcp_ses_needs_reconnect(server, 1)) 394 return 0; 395 396 cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session); 397 398 cifs_abort_connection(server); 399 400 do { 401 try_to_freeze(); 402 cifs_server_lock(server); 403 404 if (!cifs_swn_set_server_dstaddr(server)) { 405 /* resolve the hostname again to make sure that IP address is up-to-date */ 406 rc = reconn_set_ipaddr_from_hostname(server); 407 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); 408 } 409 410 if (cifs_rdma_enabled(server)) 411 rc = smbd_reconnect(server); 412 else 413 rc = generic_ip_connect(server); 414 if (rc) { 415 cifs_server_unlock(server); 416 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); 417 msleep(3000); 418 } else { 419 atomic_inc(&tcpSesReconnectCount); 420 set_credits(server, 1); 421 spin_lock(&server->srv_lock); 422 if (server->tcpStatus != CifsExiting) 423 server->tcpStatus = CifsNeedNegotiate; 424 spin_unlock(&server->srv_lock); 425 cifs_swn_reset_server_dstaddr(server); 426 cifs_server_unlock(server); 427 mod_delayed_work(cifsiod_wq, &server->reconnect, 0); 428 } 429 } while (server->tcpStatus == CifsNeedReconnect); 430 431 spin_lock(&server->srv_lock); 432 if (server->tcpStatus == CifsNeedNegotiate) 433 mod_delayed_work(cifsiod_wq, &server->echo, 0); 434 spin_unlock(&server->srv_lock); 435 436 wake_up(&server->response_q); 437 return rc; 438 } 439 440 #ifdef CONFIG_CIFS_DFS_UPCALL 441 static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target) 442 { 443 int rc; 444 char *hostname; 445 446 if (!cifs_swn_set_server_dstaddr(server)) { 447 if (server->hostname != target) { 448 hostname = extract_hostname(target); 449 if (!IS_ERR(hostname)) { 450 spin_lock(&server->srv_lock); 451 kfree(server->hostname); 452 server->hostname = hostname; 453 spin_unlock(&server->srv_lock); 454 } else { 455 cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n", 456 __func__, PTR_ERR(hostname)); 457 cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__, 458 server->hostname); 459 } 460 } 461 /* resolve the hostname again to make sure that IP address is up-to-date. */ 462 rc = reconn_set_ipaddr_from_hostname(server); 463 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); 464 } 465 /* Reconnect the socket */ 466 if (cifs_rdma_enabled(server)) 467 rc = smbd_reconnect(server); 468 else 469 rc = generic_ip_connect(server); 470 471 return rc; 472 } 473 474 static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl, 475 struct dfs_cache_tgt_iterator **target_hint) 476 { 477 int rc; 478 struct dfs_cache_tgt_iterator *tit; 479 480 *target_hint = NULL; 481 482 /* If dfs target list is empty, then reconnect to last server */ 483 tit = dfs_cache_get_tgt_iterator(tl); 484 if (!tit) 485 return __reconnect_target_unlocked(server, server->hostname); 486 487 /* Otherwise, try every dfs target in @tl */ 488 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) { 489 rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit)); 490 if (!rc) { 491 *target_hint = tit; 492 break; 493 } 494 } 495 return rc; 496 } 497 498 static int reconnect_dfs_server(struct TCP_Server_Info *server) 499 { 500 struct dfs_cache_tgt_iterator *target_hint = NULL; 501 502 DFS_CACHE_TGT_LIST(tl); 503 int num_targets = 0; 504 int rc = 0; 505 506 /* 507 * Determine the number of dfs targets the referral path in @cifs_sb resolves to. 508 * 509 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs 510 * targets (server->nr_targets). It's also possible that the cached referral was cleared 511 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after 512 * refreshing the referral, so, in this case, default it to 1. 513 */ 514 mutex_lock(&server->refpath_lock); 515 if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl)) 516 num_targets = dfs_cache_get_nr_tgts(&tl); 517 mutex_unlock(&server->refpath_lock); 518 if (!num_targets) 519 num_targets = 1; 520 521 if (!cifs_tcp_ses_needs_reconnect(server, num_targets)) 522 return 0; 523 524 /* 525 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a 526 * different server or share during failover. It could be improved by adding some logic to 527 * only do that in case it connects to a different server or share, though. 528 */ 529 cifs_mark_tcp_ses_conns_for_reconnect(server, true); 530 531 cifs_abort_connection(server); 532 533 do { 534 try_to_freeze(); 535 cifs_server_lock(server); 536 537 rc = reconnect_target_unlocked(server, &tl, &target_hint); 538 if (rc) { 539 /* Failed to reconnect socket */ 540 cifs_server_unlock(server); 541 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); 542 msleep(3000); 543 continue; 544 } 545 /* 546 * Socket was created. Update tcp session status to CifsNeedNegotiate so that a 547 * process waiting for reconnect will know it needs to re-establish session and tcon 548 * through the reconnected target server. 549 */ 550 atomic_inc(&tcpSesReconnectCount); 551 set_credits(server, 1); 552 spin_lock(&server->srv_lock); 553 if (server->tcpStatus != CifsExiting) 554 server->tcpStatus = CifsNeedNegotiate; 555 spin_unlock(&server->srv_lock); 556 cifs_swn_reset_server_dstaddr(server); 557 cifs_server_unlock(server); 558 mod_delayed_work(cifsiod_wq, &server->reconnect, 0); 559 } while (server->tcpStatus == CifsNeedReconnect); 560 561 mutex_lock(&server->refpath_lock); 562 dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint); 563 mutex_unlock(&server->refpath_lock); 564 dfs_cache_free_tgts(&tl); 565 566 /* Need to set up echo worker again once connection has been established */ 567 spin_lock(&server->srv_lock); 568 if (server->tcpStatus == CifsNeedNegotiate) 569 mod_delayed_work(cifsiod_wq, &server->echo, 0); 570 spin_unlock(&server->srv_lock); 571 572 wake_up(&server->response_q); 573 return rc; 574 } 575 576 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) 577 { 578 mutex_lock(&server->refpath_lock); 579 if (!server->leaf_fullpath) { 580 mutex_unlock(&server->refpath_lock); 581 return __cifs_reconnect(server, mark_smb_session); 582 } 583 mutex_unlock(&server->refpath_lock); 584 585 return reconnect_dfs_server(server); 586 } 587 #else 588 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) 589 { 590 return __cifs_reconnect(server, mark_smb_session); 591 } 592 #endif 593 594 static void 595 cifs_echo_request(struct work_struct *work) 596 { 597 int rc; 598 struct TCP_Server_Info *server = container_of(work, 599 struct TCP_Server_Info, echo.work); 600 601 /* 602 * We cannot send an echo if it is disabled. 603 * Also, no need to ping if we got a response recently. 604 */ 605 606 if (server->tcpStatus == CifsNeedReconnect || 607 server->tcpStatus == CifsExiting || 608 server->tcpStatus == CifsNew || 609 (server->ops->can_echo && !server->ops->can_echo(server)) || 610 time_before(jiffies, server->lstrp + server->echo_interval - HZ)) 611 goto requeue_echo; 612 613 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS; 614 cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc); 615 616 /* Check witness registrations */ 617 cifs_swn_check(); 618 619 requeue_echo: 620 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval); 621 } 622 623 static bool 624 allocate_buffers(struct TCP_Server_Info *server) 625 { 626 if (!server->bigbuf) { 627 server->bigbuf = (char *)cifs_buf_get(); 628 if (!server->bigbuf) { 629 cifs_server_dbg(VFS, "No memory for large SMB response\n"); 630 msleep(3000); 631 /* retry will check if exiting */ 632 return false; 633 } 634 } else if (server->large_buf) { 635 /* we are reusing a dirty large buf, clear its start */ 636 memset(server->bigbuf, 0, HEADER_SIZE(server)); 637 } 638 639 if (!server->smallbuf) { 640 server->smallbuf = (char *)cifs_small_buf_get(); 641 if (!server->smallbuf) { 642 cifs_server_dbg(VFS, "No memory for SMB response\n"); 643 msleep(1000); 644 /* retry will check if exiting */ 645 return false; 646 } 647 /* beginning of smb buffer is cleared in our buf_get */ 648 } else { 649 /* if existing small buf clear beginning */ 650 memset(server->smallbuf, 0, HEADER_SIZE(server)); 651 } 652 653 return true; 654 } 655 656 static bool 657 server_unresponsive(struct TCP_Server_Info *server) 658 { 659 /* 660 * If we're in the process of mounting a share or reconnecting a session 661 * and the server abruptly shut down (e.g. socket wasn't closed, packet 662 * had been ACK'ed but no SMB response), don't wait longer than 20s to 663 * negotiate protocol. 664 */ 665 spin_lock(&server->srv_lock); 666 if (server->tcpStatus == CifsInNegotiate && 667 time_after(jiffies, server->lstrp + 20 * HZ)) { 668 spin_unlock(&server->srv_lock); 669 cifs_reconnect(server, false); 670 return true; 671 } 672 /* 673 * We need to wait 3 echo intervals to make sure we handle such 674 * situations right: 675 * 1s client sends a normal SMB request 676 * 2s client gets a response 677 * 30s echo workqueue job pops, and decides we got a response recently 678 * and don't need to send another 679 * ... 680 * 65s kernel_recvmsg times out, and we see that we haven't gotten 681 * a response in >60s. 682 */ 683 if ((server->tcpStatus == CifsGood || 684 server->tcpStatus == CifsNeedNegotiate) && 685 (!server->ops->can_echo || server->ops->can_echo(server)) && 686 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) { 687 spin_unlock(&server->srv_lock); 688 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n", 689 (3 * server->echo_interval) / HZ); 690 cifs_reconnect(server, false); 691 return true; 692 } 693 spin_unlock(&server->srv_lock); 694 695 return false; 696 } 697 698 static inline bool 699 zero_credits(struct TCP_Server_Info *server) 700 { 701 int val; 702 703 spin_lock(&server->req_lock); 704 val = server->credits + server->echo_credits + server->oplock_credits; 705 if (server->in_flight == 0 && val == 0) { 706 spin_unlock(&server->req_lock); 707 return true; 708 } 709 spin_unlock(&server->req_lock); 710 return false; 711 } 712 713 static int 714 cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg) 715 { 716 int length = 0; 717 int total_read; 718 719 for (total_read = 0; msg_data_left(smb_msg); total_read += length) { 720 try_to_freeze(); 721 722 /* reconnect if no credits and no requests in flight */ 723 if (zero_credits(server)) { 724 cifs_reconnect(server, false); 725 return -ECONNABORTED; 726 } 727 728 if (server_unresponsive(server)) 729 return -ECONNABORTED; 730 if (cifs_rdma_enabled(server) && server->smbd_conn) 731 length = smbd_recv(server->smbd_conn, smb_msg); 732 else 733 length = sock_recvmsg(server->ssocket, smb_msg, 0); 734 735 spin_lock(&server->srv_lock); 736 if (server->tcpStatus == CifsExiting) { 737 spin_unlock(&server->srv_lock); 738 return -ESHUTDOWN; 739 } 740 741 if (server->tcpStatus == CifsNeedReconnect) { 742 spin_unlock(&server->srv_lock); 743 cifs_reconnect(server, false); 744 return -ECONNABORTED; 745 } 746 spin_unlock(&server->srv_lock); 747 748 if (length == -ERESTARTSYS || 749 length == -EAGAIN || 750 length == -EINTR) { 751 /* 752 * Minimum sleep to prevent looping, allowing socket 753 * to clear and app threads to set tcpStatus 754 * CifsNeedReconnect if server hung. 755 */ 756 usleep_range(1000, 2000); 757 length = 0; 758 continue; 759 } 760 761 if (length <= 0) { 762 cifs_dbg(FYI, "Received no data or error: %d\n", length); 763 cifs_reconnect(server, false); 764 return -ECONNABORTED; 765 } 766 } 767 return total_read; 768 } 769 770 int 771 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf, 772 unsigned int to_read) 773 { 774 struct msghdr smb_msg = {}; 775 struct kvec iov = {.iov_base = buf, .iov_len = to_read}; 776 777 iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read); 778 779 return cifs_readv_from_socket(server, &smb_msg); 780 } 781 782 ssize_t 783 cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read) 784 { 785 struct msghdr smb_msg = {}; 786 787 /* 788 * iov_iter_discard already sets smb_msg.type and count and iov_offset 789 * and cifs_readv_from_socket sets msg_control and msg_controllen 790 * so little to initialize in struct msghdr 791 */ 792 iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read); 793 794 return cifs_readv_from_socket(server, &smb_msg); 795 } 796 797 int 798 cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page, 799 unsigned int page_offset, unsigned int to_read) 800 { 801 struct msghdr smb_msg = {}; 802 struct bio_vec bv; 803 804 bvec_set_page(&bv, page, to_read, page_offset); 805 iov_iter_bvec(&smb_msg.msg_iter, ITER_DEST, &bv, 1, to_read); 806 return cifs_readv_from_socket(server, &smb_msg); 807 } 808 809 int 810 cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter, 811 unsigned int to_read) 812 { 813 struct msghdr smb_msg = { .msg_iter = *iter }; 814 815 iov_iter_truncate(&smb_msg.msg_iter, to_read); 816 return cifs_readv_from_socket(server, &smb_msg); 817 } 818 819 static bool 820 is_smb_response(struct TCP_Server_Info *server, unsigned char type) 821 { 822 /* 823 * The first byte big endian of the length field, 824 * is actually not part of the length but the type 825 * with the most common, zero, as regular data. 826 */ 827 switch (type) { 828 case RFC1002_SESSION_MESSAGE: 829 /* Regular SMB response */ 830 return true; 831 case RFC1002_SESSION_KEEP_ALIVE: 832 cifs_dbg(FYI, "RFC 1002 session keep alive\n"); 833 break; 834 case RFC1002_POSITIVE_SESSION_RESPONSE: 835 cifs_dbg(FYI, "RFC 1002 positive session response\n"); 836 break; 837 case RFC1002_NEGATIVE_SESSION_RESPONSE: 838 /* 839 * We get this from Windows 98 instead of an error on 840 * SMB negprot response. 841 */ 842 cifs_dbg(FYI, "RFC 1002 negative session response\n"); 843 /* give server a second to clean up */ 844 msleep(1000); 845 /* 846 * Always try 445 first on reconnect since we get NACK 847 * on some if we ever connected to port 139 (the NACK 848 * is since we do not begin with RFC1001 session 849 * initialize frame). 850 */ 851 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT); 852 cifs_reconnect(server, true); 853 break; 854 default: 855 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type); 856 cifs_reconnect(server, true); 857 } 858 859 return false; 860 } 861 862 void 863 dequeue_mid(struct mid_q_entry *mid, bool malformed) 864 { 865 #ifdef CONFIG_CIFS_STATS2 866 mid->when_received = jiffies; 867 #endif 868 spin_lock(&mid->server->mid_lock); 869 if (!malformed) 870 mid->mid_state = MID_RESPONSE_RECEIVED; 871 else 872 mid->mid_state = MID_RESPONSE_MALFORMED; 873 /* 874 * Trying to handle/dequeue a mid after the send_recv() 875 * function has finished processing it is a bug. 876 */ 877 if (mid->mid_flags & MID_DELETED) { 878 spin_unlock(&mid->server->mid_lock); 879 pr_warn_once("trying to dequeue a deleted mid\n"); 880 } else { 881 list_del_init(&mid->qhead); 882 mid->mid_flags |= MID_DELETED; 883 spin_unlock(&mid->server->mid_lock); 884 } 885 } 886 887 static unsigned int 888 smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) 889 { 890 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; 891 892 /* 893 * SMB1 does not use credits. 894 */ 895 if (is_smb1(server)) 896 return 0; 897 898 return le16_to_cpu(shdr->CreditRequest); 899 } 900 901 static void 902 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server, 903 char *buf, int malformed) 904 { 905 if (server->ops->check_trans2 && 906 server->ops->check_trans2(mid, server, buf, malformed)) 907 return; 908 mid->credits_received = smb2_get_credits_from_hdr(buf, server); 909 mid->resp_buf = buf; 910 mid->large_buf = server->large_buf; 911 /* Was previous buf put in mpx struct for multi-rsp? */ 912 if (!mid->multiRsp) { 913 /* smb buffer will be freed by user thread */ 914 if (server->large_buf) 915 server->bigbuf = NULL; 916 else 917 server->smallbuf = NULL; 918 } 919 dequeue_mid(mid, malformed); 920 } 921 922 int 923 cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required) 924 { 925 bool srv_sign_required = server->sec_mode & server->vals->signing_required; 926 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled; 927 bool mnt_sign_enabled; 928 929 /* 930 * Is signing required by mnt options? If not then check 931 * global_secflags to see if it is there. 932 */ 933 if (!mnt_sign_required) 934 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) == 935 CIFSSEC_MUST_SIGN); 936 937 /* 938 * If signing is required then it's automatically enabled too, 939 * otherwise, check to see if the secflags allow it. 940 */ 941 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required : 942 (global_secflags & CIFSSEC_MAY_SIGN); 943 944 /* If server requires signing, does client allow it? */ 945 if (srv_sign_required) { 946 if (!mnt_sign_enabled) { 947 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n"); 948 return -EOPNOTSUPP; 949 } 950 server->sign = true; 951 } 952 953 /* If client requires signing, does server allow it? */ 954 if (mnt_sign_required) { 955 if (!srv_sign_enabled) { 956 cifs_dbg(VFS, "Server does not support signing!\n"); 957 return -EOPNOTSUPP; 958 } 959 server->sign = true; 960 } 961 962 if (cifs_rdma_enabled(server) && server->sign) 963 cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n"); 964 965 return 0; 966 } 967 968 static noinline_for_stack void 969 clean_demultiplex_info(struct TCP_Server_Info *server) 970 { 971 int length; 972 973 /* take it off the list, if it's not already */ 974 spin_lock(&server->srv_lock); 975 list_del_init(&server->tcp_ses_list); 976 spin_unlock(&server->srv_lock); 977 978 cancel_delayed_work_sync(&server->echo); 979 980 spin_lock(&server->srv_lock); 981 server->tcpStatus = CifsExiting; 982 spin_unlock(&server->srv_lock); 983 wake_up_all(&server->response_q); 984 985 /* check if we have blocked requests that need to free */ 986 spin_lock(&server->req_lock); 987 if (server->credits <= 0) 988 server->credits = 1; 989 spin_unlock(&server->req_lock); 990 /* 991 * Although there should not be any requests blocked on this queue it 992 * can not hurt to be paranoid and try to wake up requests that may 993 * haven been blocked when more than 50 at time were on the wire to the 994 * same server - they now will see the session is in exit state and get 995 * out of SendReceive. 996 */ 997 wake_up_all(&server->request_q); 998 /* give those requests time to exit */ 999 msleep(125); 1000 if (cifs_rdma_enabled(server)) 1001 smbd_destroy(server); 1002 if (server->ssocket) { 1003 sock_release(server->ssocket); 1004 server->ssocket = NULL; 1005 } 1006 1007 if (!list_empty(&server->pending_mid_q)) { 1008 struct mid_q_entry *mid_entry; 1009 struct list_head *tmp, *tmp2; 1010 LIST_HEAD(dispose_list); 1011 1012 spin_lock(&server->mid_lock); 1013 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) { 1014 mid_entry = list_entry(tmp, struct mid_q_entry, qhead); 1015 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid); 1016 kref_get(&mid_entry->refcount); 1017 mid_entry->mid_state = MID_SHUTDOWN; 1018 list_move(&mid_entry->qhead, &dispose_list); 1019 mid_entry->mid_flags |= MID_DELETED; 1020 } 1021 spin_unlock(&server->mid_lock); 1022 1023 /* now walk dispose list and issue callbacks */ 1024 list_for_each_safe(tmp, tmp2, &dispose_list) { 1025 mid_entry = list_entry(tmp, struct mid_q_entry, qhead); 1026 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid); 1027 list_del_init(&mid_entry->qhead); 1028 mid_entry->callback(mid_entry); 1029 release_mid(mid_entry); 1030 } 1031 /* 1/8th of sec is more than enough time for them to exit */ 1032 msleep(125); 1033 } 1034 1035 if (!list_empty(&server->pending_mid_q)) { 1036 /* 1037 * mpx threads have not exited yet give them at least the smb 1038 * send timeout time for long ops. 1039 * 1040 * Due to delays on oplock break requests, we need to wait at 1041 * least 45 seconds before giving up on a request getting a 1042 * response and going ahead and killing cifsd. 1043 */ 1044 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n"); 1045 msleep(46000); 1046 /* 1047 * If threads still have not exited they are probably never 1048 * coming home not much else we can do but free the memory. 1049 */ 1050 } 1051 1052 kfree(server->leaf_fullpath); 1053 kfree(server); 1054 1055 length = atomic_dec_return(&tcpSesAllocCount); 1056 if (length > 0) 1057 mempool_resize(cifs_req_poolp, length + cifs_min_rcv); 1058 } 1059 1060 static int 1061 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid) 1062 { 1063 int length; 1064 char *buf = server->smallbuf; 1065 unsigned int pdu_length = server->pdu_size; 1066 1067 /* make sure this will fit in a large buffer */ 1068 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 1069 HEADER_PREAMBLE_SIZE(server)) { 1070 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length); 1071 cifs_reconnect(server, true); 1072 return -ECONNABORTED; 1073 } 1074 1075 /* switch to large buffer if too big for a small one */ 1076 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) { 1077 server->large_buf = true; 1078 memcpy(server->bigbuf, buf, server->total_read); 1079 buf = server->bigbuf; 1080 } 1081 1082 /* now read the rest */ 1083 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, 1084 pdu_length - MID_HEADER_SIZE(server)); 1085 1086 if (length < 0) 1087 return length; 1088 server->total_read += length; 1089 1090 dump_smb(buf, server->total_read); 1091 1092 return cifs_handle_standard(server, mid); 1093 } 1094 1095 int 1096 cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid) 1097 { 1098 char *buf = server->large_buf ? server->bigbuf : server->smallbuf; 1099 int rc; 1100 1101 /* 1102 * We know that we received enough to get to the MID as we 1103 * checked the pdu_length earlier. Now check to see 1104 * if the rest of the header is OK. 1105 * 1106 * 48 bytes is enough to display the header and a little bit 1107 * into the payload for debugging purposes. 1108 */ 1109 rc = server->ops->check_message(buf, server->total_read, server); 1110 if (rc) 1111 cifs_dump_mem("Bad SMB: ", buf, 1112 min_t(unsigned int, server->total_read, 48)); 1113 1114 if (server->ops->is_session_expired && 1115 server->ops->is_session_expired(buf)) { 1116 cifs_reconnect(server, true); 1117 return -1; 1118 } 1119 1120 if (server->ops->is_status_pending && 1121 server->ops->is_status_pending(buf, server)) 1122 return -1; 1123 1124 if (!mid) 1125 return rc; 1126 1127 handle_mid(mid, server, buf, rc); 1128 return 0; 1129 } 1130 1131 static void 1132 smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) 1133 { 1134 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; 1135 int scredits, in_flight; 1136 1137 /* 1138 * SMB1 does not use credits. 1139 */ 1140 if (is_smb1(server)) 1141 return; 1142 1143 if (shdr->CreditRequest) { 1144 spin_lock(&server->req_lock); 1145 server->credits += le16_to_cpu(shdr->CreditRequest); 1146 scredits = server->credits; 1147 in_flight = server->in_flight; 1148 spin_unlock(&server->req_lock); 1149 wake_up(&server->request_q); 1150 1151 trace_smb3_hdr_credits(server->CurrentMid, 1152 server->conn_id, server->hostname, scredits, 1153 le16_to_cpu(shdr->CreditRequest), in_flight); 1154 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n", 1155 __func__, le16_to_cpu(shdr->CreditRequest), 1156 scredits); 1157 } 1158 } 1159 1160 1161 static int 1162 cifs_demultiplex_thread(void *p) 1163 { 1164 int i, num_mids, length; 1165 struct TCP_Server_Info *server = p; 1166 unsigned int pdu_length; 1167 unsigned int next_offset; 1168 char *buf = NULL; 1169 struct task_struct *task_to_wake = NULL; 1170 struct mid_q_entry *mids[MAX_COMPOUND]; 1171 char *bufs[MAX_COMPOUND]; 1172 unsigned int noreclaim_flag, num_io_timeout = 0; 1173 bool pending_reconnect = false; 1174 1175 noreclaim_flag = memalloc_noreclaim_save(); 1176 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current)); 1177 1178 length = atomic_inc_return(&tcpSesAllocCount); 1179 if (length > 1) 1180 mempool_resize(cifs_req_poolp, length + cifs_min_rcv); 1181 1182 set_freezable(); 1183 allow_kernel_signal(SIGKILL); 1184 while (server->tcpStatus != CifsExiting) { 1185 if (try_to_freeze()) 1186 continue; 1187 1188 if (!allocate_buffers(server)) 1189 continue; 1190 1191 server->large_buf = false; 1192 buf = server->smallbuf; 1193 pdu_length = 4; /* enough to get RFC1001 header */ 1194 1195 length = cifs_read_from_socket(server, buf, pdu_length); 1196 if (length < 0) 1197 continue; 1198 1199 if (is_smb1(server)) 1200 server->total_read = length; 1201 else 1202 server->total_read = 0; 1203 1204 /* 1205 * The right amount was read from socket - 4 bytes, 1206 * so we can now interpret the length field. 1207 */ 1208 pdu_length = get_rfc1002_length(buf); 1209 1210 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length); 1211 if (!is_smb_response(server, buf[0])) 1212 continue; 1213 1214 pending_reconnect = false; 1215 next_pdu: 1216 server->pdu_size = pdu_length; 1217 1218 /* make sure we have enough to get to the MID */ 1219 if (server->pdu_size < MID_HEADER_SIZE(server)) { 1220 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n", 1221 server->pdu_size); 1222 cifs_reconnect(server, true); 1223 continue; 1224 } 1225 1226 /* read down to the MID */ 1227 length = cifs_read_from_socket(server, 1228 buf + HEADER_PREAMBLE_SIZE(server), 1229 MID_HEADER_SIZE(server)); 1230 if (length < 0) 1231 continue; 1232 server->total_read += length; 1233 1234 if (server->ops->next_header) { 1235 if (server->ops->next_header(server, buf, &next_offset)) { 1236 cifs_dbg(VFS, "%s: malformed response (next_offset=%u)\n", 1237 __func__, next_offset); 1238 cifs_reconnect(server, true); 1239 continue; 1240 } 1241 if (next_offset) 1242 server->pdu_size = next_offset; 1243 } 1244 1245 memset(mids, 0, sizeof(mids)); 1246 memset(bufs, 0, sizeof(bufs)); 1247 num_mids = 0; 1248 1249 if (server->ops->is_transform_hdr && 1250 server->ops->receive_transform && 1251 server->ops->is_transform_hdr(buf)) { 1252 length = server->ops->receive_transform(server, 1253 mids, 1254 bufs, 1255 &num_mids); 1256 } else { 1257 mids[0] = server->ops->find_mid(server, buf); 1258 bufs[0] = buf; 1259 num_mids = 1; 1260 1261 if (!mids[0] || !mids[0]->receive) 1262 length = standard_receive3(server, mids[0]); 1263 else 1264 length = mids[0]->receive(server, mids[0]); 1265 } 1266 1267 if (length < 0) { 1268 for (i = 0; i < num_mids; i++) 1269 if (mids[i]) 1270 release_mid(mids[i]); 1271 continue; 1272 } 1273 1274 if (server->ops->is_status_io_timeout && 1275 server->ops->is_status_io_timeout(buf)) { 1276 num_io_timeout++; 1277 if (num_io_timeout > MAX_STATUS_IO_TIMEOUT) { 1278 cifs_server_dbg(VFS, 1279 "Number of request timeouts exceeded %d. Reconnecting", 1280 MAX_STATUS_IO_TIMEOUT); 1281 1282 pending_reconnect = true; 1283 num_io_timeout = 0; 1284 } 1285 } 1286 1287 server->lstrp = jiffies; 1288 1289 for (i = 0; i < num_mids; i++) { 1290 if (mids[i] != NULL) { 1291 mids[i]->resp_buf_size = server->pdu_size; 1292 1293 if (bufs[i] != NULL) { 1294 if (server->ops->is_network_name_deleted && 1295 server->ops->is_network_name_deleted(bufs[i], 1296 server)) { 1297 cifs_server_dbg(FYI, 1298 "Share deleted. Reconnect needed"); 1299 } 1300 } 1301 1302 if (!mids[i]->multiRsp || mids[i]->multiEnd) 1303 mids[i]->callback(mids[i]); 1304 1305 release_mid(mids[i]); 1306 } else if (server->ops->is_oplock_break && 1307 server->ops->is_oplock_break(bufs[i], 1308 server)) { 1309 smb2_add_credits_from_hdr(bufs[i], server); 1310 cifs_dbg(FYI, "Received oplock break\n"); 1311 } else { 1312 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n", 1313 atomic_read(&mid_count)); 1314 cifs_dump_mem("Received Data is: ", bufs[i], 1315 HEADER_SIZE(server)); 1316 smb2_add_credits_from_hdr(bufs[i], server); 1317 #ifdef CONFIG_CIFS_DEBUG2 1318 if (server->ops->dump_detail) 1319 server->ops->dump_detail(bufs[i], 1320 server); 1321 cifs_dump_mids(server); 1322 #endif /* CIFS_DEBUG2 */ 1323 } 1324 } 1325 1326 if (pdu_length > server->pdu_size) { 1327 if (!allocate_buffers(server)) 1328 continue; 1329 pdu_length -= server->pdu_size; 1330 server->total_read = 0; 1331 server->large_buf = false; 1332 buf = server->smallbuf; 1333 goto next_pdu; 1334 } 1335 1336 /* do this reconnect at the very end after processing all MIDs */ 1337 if (pending_reconnect) 1338 cifs_reconnect(server, true); 1339 1340 } /* end while !EXITING */ 1341 1342 /* buffer usually freed in free_mid - need to free it here on exit */ 1343 cifs_buf_release(server->bigbuf); 1344 if (server->smallbuf) /* no sense logging a debug message if NULL */ 1345 cifs_small_buf_release(server->smallbuf); 1346 1347 task_to_wake = xchg(&server->tsk, NULL); 1348 clean_demultiplex_info(server); 1349 1350 /* if server->tsk was NULL then wait for a signal before exiting */ 1351 if (!task_to_wake) { 1352 set_current_state(TASK_INTERRUPTIBLE); 1353 while (!signal_pending(current)) { 1354 schedule(); 1355 set_current_state(TASK_INTERRUPTIBLE); 1356 } 1357 set_current_state(TASK_RUNNING); 1358 } 1359 1360 memalloc_noreclaim_restore(noreclaim_flag); 1361 module_put_and_kthread_exit(0); 1362 } 1363 1364 int 1365 cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs) 1366 { 1367 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; 1368 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; 1369 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; 1370 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; 1371 1372 switch (srcaddr->sa_family) { 1373 case AF_UNSPEC: 1374 switch (rhs->sa_family) { 1375 case AF_UNSPEC: 1376 return 0; 1377 case AF_INET: 1378 case AF_INET6: 1379 return 1; 1380 default: 1381 return -1; 1382 } 1383 case AF_INET: { 1384 switch (rhs->sa_family) { 1385 case AF_UNSPEC: 1386 return -1; 1387 case AF_INET: 1388 return memcmp(saddr4, vaddr4, 1389 sizeof(struct sockaddr_in)); 1390 case AF_INET6: 1391 return 1; 1392 default: 1393 return -1; 1394 } 1395 } 1396 case AF_INET6: { 1397 switch (rhs->sa_family) { 1398 case AF_UNSPEC: 1399 case AF_INET: 1400 return -1; 1401 case AF_INET6: 1402 return memcmp(saddr6, 1403 vaddr6, 1404 sizeof(struct sockaddr_in6)); 1405 default: 1406 return -1; 1407 } 1408 } 1409 default: 1410 return -1; /* don't expect to be here */ 1411 } 1412 } 1413 1414 /* 1415 * Returns true if srcaddr isn't specified and rhs isn't specified, or 1416 * if srcaddr is specified and matches the IP address of the rhs argument 1417 */ 1418 bool 1419 cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs) 1420 { 1421 switch (srcaddr->sa_family) { 1422 case AF_UNSPEC: 1423 return (rhs->sa_family == AF_UNSPEC); 1424 case AF_INET: { 1425 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; 1426 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; 1427 1428 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr); 1429 } 1430 case AF_INET6: { 1431 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; 1432 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; 1433 1434 return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr) 1435 && saddr6->sin6_scope_id == vaddr6->sin6_scope_id); 1436 } 1437 default: 1438 WARN_ON(1); 1439 return false; /* don't expect to be here */ 1440 } 1441 } 1442 1443 /* 1444 * If no port is specified in addr structure, we try to match with 445 port 1445 * and if it fails - with 139 ports. It should be called only if address 1446 * families of server and addr are equal. 1447 */ 1448 static bool 1449 match_port(struct TCP_Server_Info *server, struct sockaddr *addr) 1450 { 1451 __be16 port, *sport; 1452 1453 /* SMBDirect manages its own ports, don't match it here */ 1454 if (server->rdma) 1455 return true; 1456 1457 switch (addr->sa_family) { 1458 case AF_INET: 1459 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port; 1460 port = ((struct sockaddr_in *) addr)->sin_port; 1461 break; 1462 case AF_INET6: 1463 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port; 1464 port = ((struct sockaddr_in6 *) addr)->sin6_port; 1465 break; 1466 default: 1467 WARN_ON(1); 1468 return false; 1469 } 1470 1471 if (!port) { 1472 port = htons(CIFS_PORT); 1473 if (port == *sport) 1474 return true; 1475 1476 port = htons(RFC1001_PORT); 1477 } 1478 1479 return port == *sport; 1480 } 1481 1482 static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr) 1483 { 1484 if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr)) 1485 return false; 1486 1487 return true; 1488 } 1489 1490 static bool 1491 match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) 1492 { 1493 /* 1494 * The select_sectype function should either return the ctx->sectype 1495 * that was specified, or "Unspecified" if that sectype was not 1496 * compatible with the given NEGOTIATE request. 1497 */ 1498 if (server->ops->select_sectype(server, ctx->sectype) 1499 == Unspecified) 1500 return false; 1501 1502 /* 1503 * Now check if signing mode is acceptable. No need to check 1504 * global_secflags at this point since if MUST_SIGN is set then 1505 * the server->sign had better be too. 1506 */ 1507 if (ctx->sign && !server->sign) 1508 return false; 1509 1510 return true; 1511 } 1512 1513 /* this function must be called with srv_lock held */ 1514 static int match_server(struct TCP_Server_Info *server, 1515 struct smb3_fs_context *ctx, 1516 bool match_super) 1517 { 1518 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr; 1519 1520 lockdep_assert_held(&server->srv_lock); 1521 1522 if (ctx->nosharesock) 1523 return 0; 1524 1525 /* this server does not share socket */ 1526 if (server->nosharesock) 1527 return 0; 1528 1529 if (!match_super && (ctx->dfs_conn || server->dfs_conn)) 1530 return 0; 1531 1532 /* If multidialect negotiation see if existing sessions match one */ 1533 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) { 1534 if (server->vals->protocol_id < SMB30_PROT_ID) 1535 return 0; 1536 } else if (strcmp(ctx->vals->version_string, 1537 SMBDEFAULT_VERSION_STRING) == 0) { 1538 if (server->vals->protocol_id < SMB21_PROT_ID) 1539 return 0; 1540 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops)) 1541 return 0; 1542 1543 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns)) 1544 return 0; 1545 1546 if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr, 1547 (struct sockaddr *)&server->srcaddr)) 1548 return 0; 1549 /* 1550 * When matching cifs.ko superblocks (@match_super == true), we can't 1551 * really match either @server->leaf_fullpath or @server->dstaddr 1552 * directly since this @server might belong to a completely different 1553 * server -- in case of domain-based DFS referrals or DFS links -- as 1554 * provided earlier by mount(2) through 'source' and 'ip' options. 1555 * 1556 * Otherwise, match the DFS referral in @server->leaf_fullpath or the 1557 * destination address in @server->dstaddr. 1558 * 1559 * When using 'nodfs' mount option, we avoid sharing it with DFS 1560 * connections as they might failover. 1561 */ 1562 if (!match_super) { 1563 if (!ctx->nodfs) { 1564 if (server->leaf_fullpath) { 1565 if (!ctx->leaf_fullpath || 1566 strcasecmp(server->leaf_fullpath, 1567 ctx->leaf_fullpath)) 1568 return 0; 1569 } else if (ctx->leaf_fullpath) { 1570 return 0; 1571 } 1572 } else if (server->leaf_fullpath) { 1573 return 0; 1574 } 1575 } 1576 1577 /* 1578 * Match for a regular connection (address/hostname/port) which has no 1579 * DFS referrals set. 1580 */ 1581 if (!server->leaf_fullpath && 1582 (strcasecmp(server->hostname, ctx->server_hostname) || 1583 !match_server_address(server, addr) || 1584 !match_port(server, addr))) 1585 return 0; 1586 1587 if (!match_security(server, ctx)) 1588 return 0; 1589 1590 if (server->echo_interval != ctx->echo_interval * HZ) 1591 return 0; 1592 1593 if (server->rdma != ctx->rdma) 1594 return 0; 1595 1596 if (server->ignore_signature != ctx->ignore_signature) 1597 return 0; 1598 1599 if (server->min_offload != ctx->min_offload) 1600 return 0; 1601 1602 if (server->retrans != ctx->retrans) 1603 return 0; 1604 1605 return 1; 1606 } 1607 1608 struct TCP_Server_Info * 1609 cifs_find_tcp_session(struct smb3_fs_context *ctx) 1610 { 1611 struct TCP_Server_Info *server; 1612 1613 spin_lock(&cifs_tcp_ses_lock); 1614 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) { 1615 spin_lock(&server->srv_lock); 1616 /* 1617 * Skip ses channels since they're only handled in lower layers 1618 * (e.g. cifs_send_recv). 1619 */ 1620 if (SERVER_IS_CHAN(server) || 1621 !match_server(server, ctx, false)) { 1622 spin_unlock(&server->srv_lock); 1623 continue; 1624 } 1625 spin_unlock(&server->srv_lock); 1626 1627 ++server->srv_count; 1628 spin_unlock(&cifs_tcp_ses_lock); 1629 cifs_dbg(FYI, "Existing tcp session with server found\n"); 1630 return server; 1631 } 1632 spin_unlock(&cifs_tcp_ses_lock); 1633 return NULL; 1634 } 1635 1636 void 1637 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect) 1638 { 1639 struct task_struct *task; 1640 1641 spin_lock(&cifs_tcp_ses_lock); 1642 if (--server->srv_count > 0) { 1643 spin_unlock(&cifs_tcp_ses_lock); 1644 return; 1645 } 1646 1647 /* srv_count can never go negative */ 1648 WARN_ON(server->srv_count < 0); 1649 1650 put_net(cifs_net_ns(server)); 1651 1652 list_del_init(&server->tcp_ses_list); 1653 spin_unlock(&cifs_tcp_ses_lock); 1654 1655 cancel_delayed_work_sync(&server->echo); 1656 1657 if (from_reconnect) 1658 /* 1659 * Avoid deadlock here: reconnect work calls 1660 * cifs_put_tcp_session() at its end. Need to be sure 1661 * that reconnect work does nothing with server pointer after 1662 * that step. 1663 */ 1664 cancel_delayed_work(&server->reconnect); 1665 else 1666 cancel_delayed_work_sync(&server->reconnect); 1667 1668 /* For secondary channels, we pick up ref-count on the primary server */ 1669 if (SERVER_IS_CHAN(server)) 1670 cifs_put_tcp_session(server->primary_server, from_reconnect); 1671 1672 spin_lock(&server->srv_lock); 1673 server->tcpStatus = CifsExiting; 1674 spin_unlock(&server->srv_lock); 1675 1676 cifs_crypto_secmech_release(server); 1677 1678 kfree_sensitive(server->session_key.response); 1679 server->session_key.response = NULL; 1680 server->session_key.len = 0; 1681 kfree(server->hostname); 1682 server->hostname = NULL; 1683 1684 task = xchg(&server->tsk, NULL); 1685 if (task) 1686 send_sig(SIGKILL, task, 1); 1687 } 1688 1689 struct TCP_Server_Info * 1690 cifs_get_tcp_session(struct smb3_fs_context *ctx, 1691 struct TCP_Server_Info *primary_server) 1692 { 1693 struct TCP_Server_Info *tcp_ses = NULL; 1694 int rc; 1695 1696 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC); 1697 1698 /* see if we already have a matching tcp_ses */ 1699 tcp_ses = cifs_find_tcp_session(ctx); 1700 if (tcp_ses) 1701 return tcp_ses; 1702 1703 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL); 1704 if (!tcp_ses) { 1705 rc = -ENOMEM; 1706 goto out_err; 1707 } 1708 1709 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL); 1710 if (!tcp_ses->hostname) { 1711 rc = -ENOMEM; 1712 goto out_err; 1713 } 1714 1715 if (ctx->leaf_fullpath) { 1716 tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL); 1717 if (!tcp_ses->leaf_fullpath) { 1718 rc = -ENOMEM; 1719 goto out_err; 1720 } 1721 } 1722 1723 if (ctx->nosharesock) 1724 tcp_ses->nosharesock = true; 1725 tcp_ses->dfs_conn = ctx->dfs_conn; 1726 1727 tcp_ses->ops = ctx->ops; 1728 tcp_ses->vals = ctx->vals; 1729 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns)); 1730 1731 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId); 1732 tcp_ses->noblockcnt = ctx->rootfs; 1733 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs; 1734 tcp_ses->noautotune = ctx->noautotune; 1735 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay; 1736 tcp_ses->rdma = ctx->rdma; 1737 tcp_ses->in_flight = 0; 1738 tcp_ses->max_in_flight = 0; 1739 tcp_ses->credits = 1; 1740 if (primary_server) { 1741 spin_lock(&cifs_tcp_ses_lock); 1742 ++primary_server->srv_count; 1743 spin_unlock(&cifs_tcp_ses_lock); 1744 tcp_ses->primary_server = primary_server; 1745 } 1746 init_waitqueue_head(&tcp_ses->response_q); 1747 init_waitqueue_head(&tcp_ses->request_q); 1748 INIT_LIST_HEAD(&tcp_ses->pending_mid_q); 1749 mutex_init(&tcp_ses->_srv_mutex); 1750 memcpy(tcp_ses->workstation_RFC1001_name, 1751 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); 1752 memcpy(tcp_ses->server_RFC1001_name, 1753 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); 1754 tcp_ses->session_estab = false; 1755 tcp_ses->sequence_number = 0; 1756 tcp_ses->channel_sequence_num = 0; /* only tracked for primary channel */ 1757 tcp_ses->reconnect_instance = 1; 1758 tcp_ses->lstrp = jiffies; 1759 tcp_ses->compression.requested = ctx->compress; 1760 spin_lock_init(&tcp_ses->req_lock); 1761 spin_lock_init(&tcp_ses->srv_lock); 1762 spin_lock_init(&tcp_ses->mid_lock); 1763 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list); 1764 INIT_LIST_HEAD(&tcp_ses->smb_ses_list); 1765 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request); 1766 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server); 1767 mutex_init(&tcp_ses->reconnect_mutex); 1768 #ifdef CONFIG_CIFS_DFS_UPCALL 1769 mutex_init(&tcp_ses->refpath_lock); 1770 #endif 1771 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr, 1772 sizeof(tcp_ses->srcaddr)); 1773 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr, 1774 sizeof(tcp_ses->dstaddr)); 1775 if (ctx->use_client_guid) 1776 memcpy(tcp_ses->client_guid, ctx->client_guid, 1777 SMB2_CLIENT_GUID_SIZE); 1778 else 1779 generate_random_uuid(tcp_ses->client_guid); 1780 /* 1781 * at this point we are the only ones with the pointer 1782 * to the struct since the kernel thread not created yet 1783 * no need to spinlock this init of tcpStatus or srv_count 1784 */ 1785 tcp_ses->tcpStatus = CifsNew; 1786 ++tcp_ses->srv_count; 1787 1788 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN && 1789 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX) 1790 tcp_ses->echo_interval = ctx->echo_interval * HZ; 1791 else 1792 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ; 1793 if (tcp_ses->rdma) { 1794 #ifndef CONFIG_CIFS_SMB_DIRECT 1795 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n"); 1796 rc = -ENOENT; 1797 goto out_err_crypto_release; 1798 #endif 1799 tcp_ses->smbd_conn = smbd_get_connection( 1800 tcp_ses, (struct sockaddr *)&ctx->dstaddr); 1801 if (tcp_ses->smbd_conn) { 1802 cifs_dbg(VFS, "RDMA transport established\n"); 1803 rc = 0; 1804 goto smbd_connected; 1805 } else { 1806 rc = -ENOENT; 1807 goto out_err_crypto_release; 1808 } 1809 } 1810 rc = ip_connect(tcp_ses); 1811 if (rc < 0) { 1812 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n"); 1813 goto out_err_crypto_release; 1814 } 1815 smbd_connected: 1816 /* 1817 * since we're in a cifs function already, we know that 1818 * this will succeed. No need for try_module_get(). 1819 */ 1820 __module_get(THIS_MODULE); 1821 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread, 1822 tcp_ses, "cifsd"); 1823 if (IS_ERR(tcp_ses->tsk)) { 1824 rc = PTR_ERR(tcp_ses->tsk); 1825 cifs_dbg(VFS, "error %d create cifsd thread\n", rc); 1826 module_put(THIS_MODULE); 1827 goto out_err_crypto_release; 1828 } 1829 tcp_ses->min_offload = ctx->min_offload; 1830 tcp_ses->retrans = ctx->retrans; 1831 /* 1832 * at this point we are the only ones with the pointer 1833 * to the struct since the kernel thread not created yet 1834 * no need to spinlock this update of tcpStatus 1835 */ 1836 spin_lock(&tcp_ses->srv_lock); 1837 tcp_ses->tcpStatus = CifsNeedNegotiate; 1838 spin_unlock(&tcp_ses->srv_lock); 1839 1840 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000)) 1841 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE; 1842 else 1843 tcp_ses->max_credits = ctx->max_credits; 1844 1845 tcp_ses->nr_targets = 1; 1846 tcp_ses->ignore_signature = ctx->ignore_signature; 1847 /* thread spawned, put it on the list */ 1848 spin_lock(&cifs_tcp_ses_lock); 1849 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list); 1850 spin_unlock(&cifs_tcp_ses_lock); 1851 1852 /* queue echo request delayed work */ 1853 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval); 1854 1855 return tcp_ses; 1856 1857 out_err_crypto_release: 1858 cifs_crypto_secmech_release(tcp_ses); 1859 1860 put_net(cifs_net_ns(tcp_ses)); 1861 1862 out_err: 1863 if (tcp_ses) { 1864 if (SERVER_IS_CHAN(tcp_ses)) 1865 cifs_put_tcp_session(tcp_ses->primary_server, false); 1866 kfree(tcp_ses->hostname); 1867 kfree(tcp_ses->leaf_fullpath); 1868 if (tcp_ses->ssocket) 1869 sock_release(tcp_ses->ssocket); 1870 kfree(tcp_ses); 1871 } 1872 return ERR_PTR(rc); 1873 } 1874 1875 /* this function must be called with ses_lock and chan_lock held */ 1876 static int match_session(struct cifs_ses *ses, 1877 struct smb3_fs_context *ctx, 1878 bool match_super) 1879 { 1880 if (ctx->sectype != Unspecified && 1881 ctx->sectype != ses->sectype) 1882 return 0; 1883 1884 if (!match_super && ctx->dfs_root_ses != ses->dfs_root_ses) 1885 return 0; 1886 1887 /* 1888 * If an existing session is limited to less channels than 1889 * requested, it should not be reused 1890 */ 1891 if (ses->chan_max < ctx->max_channels) 1892 return 0; 1893 1894 switch (ses->sectype) { 1895 case Kerberos: 1896 if (!uid_eq(ctx->cred_uid, ses->cred_uid)) 1897 return 0; 1898 break; 1899 default: 1900 /* NULL username means anonymous session */ 1901 if (ses->user_name == NULL) { 1902 if (!ctx->nullauth) 1903 return 0; 1904 break; 1905 } 1906 1907 /* anything else takes username/password */ 1908 if (strncmp(ses->user_name, 1909 ctx->username ? ctx->username : "", 1910 CIFS_MAX_USERNAME_LEN)) 1911 return 0; 1912 if ((ctx->username && strlen(ctx->username) != 0) && 1913 ses->password != NULL && 1914 strncmp(ses->password, 1915 ctx->password ? ctx->password : "", 1916 CIFS_MAX_PASSWORD_LEN)) 1917 return 0; 1918 } 1919 1920 if (strcmp(ctx->local_nls->charset, ses->local_nls->charset)) 1921 return 0; 1922 1923 return 1; 1924 } 1925 1926 /** 1927 * cifs_setup_ipc - helper to setup the IPC tcon for the session 1928 * @ses: smb session to issue the request on 1929 * @ctx: the superblock configuration context to use for building the 1930 * new tree connection for the IPC (interprocess communication RPC) 1931 * 1932 * A new IPC connection is made and stored in the session 1933 * tcon_ipc. The IPC tcon has the same lifetime as the session. 1934 */ 1935 static int 1936 cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx) 1937 { 1938 int rc = 0, xid; 1939 struct cifs_tcon *tcon; 1940 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0}; 1941 bool seal = false; 1942 struct TCP_Server_Info *server = ses->server; 1943 1944 /* 1945 * If the mount request that resulted in the creation of the 1946 * session requires encryption, force IPC to be encrypted too. 1947 */ 1948 if (ctx->seal) { 1949 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) 1950 seal = true; 1951 else { 1952 cifs_server_dbg(VFS, 1953 "IPC: server doesn't support encryption\n"); 1954 return -EOPNOTSUPP; 1955 } 1956 } 1957 1958 /* no need to setup directory caching on IPC share, so pass in false */ 1959 tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_ipc); 1960 if (tcon == NULL) 1961 return -ENOMEM; 1962 1963 spin_lock(&server->srv_lock); 1964 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname); 1965 spin_unlock(&server->srv_lock); 1966 1967 xid = get_xid(); 1968 tcon->ses = ses; 1969 tcon->ipc = true; 1970 tcon->seal = seal; 1971 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls); 1972 free_xid(xid); 1973 1974 if (rc) { 1975 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc); 1976 tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc_fail); 1977 goto out; 1978 } 1979 1980 cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid); 1981 1982 spin_lock(&tcon->tc_lock); 1983 tcon->status = TID_GOOD; 1984 spin_unlock(&tcon->tc_lock); 1985 ses->tcon_ipc = tcon; 1986 out: 1987 return rc; 1988 } 1989 1990 static struct cifs_ses * 1991 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) 1992 { 1993 struct cifs_ses *ses, *ret = NULL; 1994 1995 spin_lock(&cifs_tcp_ses_lock); 1996 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { 1997 spin_lock(&ses->ses_lock); 1998 if (ses->ses_status == SES_EXITING) { 1999 spin_unlock(&ses->ses_lock); 2000 continue; 2001 } 2002 spin_lock(&ses->chan_lock); 2003 if (match_session(ses, ctx, false)) { 2004 spin_unlock(&ses->chan_lock); 2005 spin_unlock(&ses->ses_lock); 2006 ret = ses; 2007 break; 2008 } 2009 spin_unlock(&ses->chan_lock); 2010 spin_unlock(&ses->ses_lock); 2011 } 2012 if (ret) 2013 cifs_smb_ses_inc_refcount(ret); 2014 spin_unlock(&cifs_tcp_ses_lock); 2015 return ret; 2016 } 2017 2018 void __cifs_put_smb_ses(struct cifs_ses *ses) 2019 { 2020 struct TCP_Server_Info *server = ses->server; 2021 struct cifs_tcon *tcon; 2022 unsigned int xid; 2023 size_t i; 2024 bool do_logoff; 2025 int rc; 2026 2027 spin_lock(&cifs_tcp_ses_lock); 2028 spin_lock(&ses->ses_lock); 2029 cifs_dbg(FYI, "%s: id=0x%llx ses_count=%d ses_status=%u ipc=%s\n", 2030 __func__, ses->Suid, ses->ses_count, ses->ses_status, 2031 ses->tcon_ipc ? ses->tcon_ipc->tree_name : "none"); 2032 if (ses->ses_status == SES_EXITING || --ses->ses_count > 0) { 2033 spin_unlock(&ses->ses_lock); 2034 spin_unlock(&cifs_tcp_ses_lock); 2035 return; 2036 } 2037 /* ses_count can never go negative */ 2038 WARN_ON(ses->ses_count < 0); 2039 2040 spin_lock(&ses->chan_lock); 2041 cifs_chan_clear_need_reconnect(ses, server); 2042 spin_unlock(&ses->chan_lock); 2043 2044 do_logoff = ses->ses_status == SES_GOOD && server->ops->logoff; 2045 ses->ses_status = SES_EXITING; 2046 tcon = ses->tcon_ipc; 2047 ses->tcon_ipc = NULL; 2048 spin_unlock(&ses->ses_lock); 2049 spin_unlock(&cifs_tcp_ses_lock); 2050 2051 /* 2052 * On session close, the IPC is closed and the server must release all 2053 * tcons of the session. No need to send a tree disconnect here. 2054 * 2055 * Besides, it will make the server to not close durable and resilient 2056 * files on session close, as specified in MS-SMB2 3.3.5.6 Receiving an 2057 * SMB2 LOGOFF Request. 2058 */ 2059 tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc); 2060 if (do_logoff) { 2061 xid = get_xid(); 2062 rc = server->ops->logoff(xid, ses); 2063 cifs_server_dbg(FYI, "%s: Session Logoff: rc=%d\n", 2064 __func__, rc); 2065 _free_xid(xid); 2066 } 2067 2068 spin_lock(&cifs_tcp_ses_lock); 2069 list_del_init(&ses->smb_ses_list); 2070 spin_unlock(&cifs_tcp_ses_lock); 2071 2072 /* close any extra channels */ 2073 for (i = 1; i < ses->chan_count; i++) { 2074 if (ses->chans[i].iface) { 2075 kref_put(&ses->chans[i].iface->refcount, release_iface); 2076 ses->chans[i].iface = NULL; 2077 } 2078 cifs_put_tcp_session(ses->chans[i].server, 0); 2079 ses->chans[i].server = NULL; 2080 } 2081 2082 /* we now account for primary channel in iface->refcount */ 2083 if (ses->chans[0].iface) { 2084 kref_put(&ses->chans[0].iface->refcount, release_iface); 2085 ses->chans[0].server = NULL; 2086 } 2087 2088 sesInfoFree(ses); 2089 cifs_put_tcp_session(server, 0); 2090 } 2091 2092 #ifdef CONFIG_KEYS 2093 2094 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */ 2095 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1) 2096 2097 /* Populate username and pw fields from keyring if possible */ 2098 static int 2099 cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses) 2100 { 2101 int rc = 0; 2102 int is_domain = 0; 2103 const char *delim, *payload; 2104 char *desc; 2105 ssize_t len; 2106 struct key *key; 2107 struct TCP_Server_Info *server = ses->server; 2108 struct sockaddr_in *sa; 2109 struct sockaddr_in6 *sa6; 2110 const struct user_key_payload *upayload; 2111 2112 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL); 2113 if (!desc) 2114 return -ENOMEM; 2115 2116 /* try to find an address key first */ 2117 switch (server->dstaddr.ss_family) { 2118 case AF_INET: 2119 sa = (struct sockaddr_in *)&server->dstaddr; 2120 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr); 2121 break; 2122 case AF_INET6: 2123 sa6 = (struct sockaddr_in6 *)&server->dstaddr; 2124 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr); 2125 break; 2126 default: 2127 cifs_dbg(FYI, "Bad ss_family (%hu)\n", 2128 server->dstaddr.ss_family); 2129 rc = -EINVAL; 2130 goto out_err; 2131 } 2132 2133 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); 2134 key = request_key(&key_type_logon, desc, ""); 2135 if (IS_ERR(key)) { 2136 if (!ses->domainName) { 2137 cifs_dbg(FYI, "domainName is NULL\n"); 2138 rc = PTR_ERR(key); 2139 goto out_err; 2140 } 2141 2142 /* didn't work, try to find a domain key */ 2143 sprintf(desc, "cifs:d:%s", ses->domainName); 2144 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); 2145 key = request_key(&key_type_logon, desc, ""); 2146 if (IS_ERR(key)) { 2147 rc = PTR_ERR(key); 2148 goto out_err; 2149 } 2150 is_domain = 1; 2151 } 2152 2153 down_read(&key->sem); 2154 upayload = user_key_payload_locked(key); 2155 if (IS_ERR_OR_NULL(upayload)) { 2156 rc = upayload ? PTR_ERR(upayload) : -EINVAL; 2157 goto out_key_put; 2158 } 2159 2160 /* find first : in payload */ 2161 payload = upayload->data; 2162 delim = strnchr(payload, upayload->datalen, ':'); 2163 cifs_dbg(FYI, "payload=%s\n", payload); 2164 if (!delim) { 2165 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n", 2166 upayload->datalen); 2167 rc = -EINVAL; 2168 goto out_key_put; 2169 } 2170 2171 len = delim - payload; 2172 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) { 2173 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n", 2174 len); 2175 rc = -EINVAL; 2176 goto out_key_put; 2177 } 2178 2179 ctx->username = kstrndup(payload, len, GFP_KERNEL); 2180 if (!ctx->username) { 2181 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n", 2182 len); 2183 rc = -ENOMEM; 2184 goto out_key_put; 2185 } 2186 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username); 2187 2188 len = key->datalen - (len + 1); 2189 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) { 2190 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len); 2191 rc = -EINVAL; 2192 kfree(ctx->username); 2193 ctx->username = NULL; 2194 goto out_key_put; 2195 } 2196 2197 ++delim; 2198 /* BB consider adding support for password2 (Key Rotation) for multiuser in future */ 2199 ctx->password = kstrndup(delim, len, GFP_KERNEL); 2200 if (!ctx->password) { 2201 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n", 2202 len); 2203 rc = -ENOMEM; 2204 kfree(ctx->username); 2205 ctx->username = NULL; 2206 goto out_key_put; 2207 } 2208 2209 /* 2210 * If we have a domain key then we must set the domainName in the 2211 * for the request. 2212 */ 2213 if (is_domain && ses->domainName) { 2214 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL); 2215 if (!ctx->domainname) { 2216 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n", 2217 len); 2218 rc = -ENOMEM; 2219 kfree(ctx->username); 2220 ctx->username = NULL; 2221 kfree_sensitive(ctx->password); 2222 /* no need to free ctx->password2 since not allocated in this path */ 2223 ctx->password = NULL; 2224 goto out_key_put; 2225 } 2226 } 2227 2228 strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name)); 2229 2230 out_key_put: 2231 up_read(&key->sem); 2232 key_put(key); 2233 out_err: 2234 kfree(desc); 2235 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc); 2236 return rc; 2237 } 2238 #else /* ! CONFIG_KEYS */ 2239 static inline int 2240 cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)), 2241 struct cifs_ses *ses __attribute__((unused))) 2242 { 2243 return -ENOSYS; 2244 } 2245 #endif /* CONFIG_KEYS */ 2246 2247 /** 2248 * cifs_get_smb_ses - get a session matching @ctx data from @server 2249 * @server: server to setup the session to 2250 * @ctx: superblock configuration context to use to setup the session 2251 * 2252 * This function assumes it is being called from cifs_mount() where we 2253 * already got a server reference (server refcount +1). See 2254 * cifs_get_tcon() for refcount explanations. 2255 */ 2256 struct cifs_ses * 2257 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) 2258 { 2259 int rc = 0; 2260 unsigned int xid; 2261 struct cifs_ses *ses; 2262 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; 2263 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; 2264 2265 xid = get_xid(); 2266 2267 ses = cifs_find_smb_ses(server, ctx); 2268 if (ses) { 2269 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n", 2270 ses->ses_status); 2271 2272 spin_lock(&ses->chan_lock); 2273 if (cifs_chan_needs_reconnect(ses, server)) { 2274 spin_unlock(&ses->chan_lock); 2275 cifs_dbg(FYI, "Session needs reconnect\n"); 2276 2277 mutex_lock(&ses->session_mutex); 2278 rc = cifs_negotiate_protocol(xid, ses, server); 2279 if (rc) { 2280 mutex_unlock(&ses->session_mutex); 2281 /* problem -- put our ses reference */ 2282 cifs_put_smb_ses(ses); 2283 free_xid(xid); 2284 return ERR_PTR(rc); 2285 } 2286 2287 rc = cifs_setup_session(xid, ses, server, 2288 ctx->local_nls); 2289 if (rc) { 2290 mutex_unlock(&ses->session_mutex); 2291 /* problem -- put our reference */ 2292 cifs_put_smb_ses(ses); 2293 free_xid(xid); 2294 return ERR_PTR(rc); 2295 } 2296 mutex_unlock(&ses->session_mutex); 2297 2298 spin_lock(&ses->chan_lock); 2299 } 2300 spin_unlock(&ses->chan_lock); 2301 2302 /* existing SMB ses has a server reference already */ 2303 cifs_put_tcp_session(server, 0); 2304 free_xid(xid); 2305 return ses; 2306 } 2307 2308 rc = -ENOMEM; 2309 2310 cifs_dbg(FYI, "Existing smb sess not found\n"); 2311 ses = sesInfoAlloc(); 2312 if (ses == NULL) 2313 goto get_ses_fail; 2314 2315 /* new SMB session uses our server ref */ 2316 ses->server = server; 2317 if (server->dstaddr.ss_family == AF_INET6) 2318 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr); 2319 else 2320 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr); 2321 2322 if (ctx->username) { 2323 ses->user_name = kstrdup(ctx->username, GFP_KERNEL); 2324 if (!ses->user_name) 2325 goto get_ses_fail; 2326 } 2327 2328 /* ctx->password freed at unmount */ 2329 if (ctx->password) { 2330 ses->password = kstrdup(ctx->password, GFP_KERNEL); 2331 if (!ses->password) 2332 goto get_ses_fail; 2333 } 2334 /* ctx->password freed at unmount */ 2335 if (ctx->password2) { 2336 ses->password2 = kstrdup(ctx->password2, GFP_KERNEL); 2337 if (!ses->password2) 2338 goto get_ses_fail; 2339 } 2340 if (ctx->domainname) { 2341 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL); 2342 if (!ses->domainName) 2343 goto get_ses_fail; 2344 } 2345 2346 strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name)); 2347 2348 if (ctx->domainauto) 2349 ses->domainAuto = ctx->domainauto; 2350 ses->cred_uid = ctx->cred_uid; 2351 ses->linux_uid = ctx->linux_uid; 2352 2353 ses->sectype = ctx->sectype; 2354 ses->sign = ctx->sign; 2355 ses->local_nls = load_nls(ctx->local_nls->charset); 2356 2357 /* add server as first channel */ 2358 spin_lock(&ses->chan_lock); 2359 ses->chans[0].server = server; 2360 ses->chan_count = 1; 2361 ses->chan_max = ctx->multichannel ? ctx->max_channels:1; 2362 ses->chans_need_reconnect = 1; 2363 spin_unlock(&ses->chan_lock); 2364 2365 mutex_lock(&ses->session_mutex); 2366 rc = cifs_negotiate_protocol(xid, ses, server); 2367 if (!rc) 2368 rc = cifs_setup_session(xid, ses, server, ctx->local_nls); 2369 mutex_unlock(&ses->session_mutex); 2370 2371 /* each channel uses a different signing key */ 2372 spin_lock(&ses->chan_lock); 2373 memcpy(ses->chans[0].signkey, ses->smb3signingkey, 2374 sizeof(ses->smb3signingkey)); 2375 spin_unlock(&ses->chan_lock); 2376 2377 if (rc) 2378 goto get_ses_fail; 2379 2380 /* 2381 * success, put it on the list and add it as first channel 2382 * note: the session becomes active soon after this. So you'll 2383 * need to lock before changing something in the session. 2384 */ 2385 spin_lock(&cifs_tcp_ses_lock); 2386 ses->dfs_root_ses = ctx->dfs_root_ses; 2387 list_add(&ses->smb_ses_list, &server->smb_ses_list); 2388 spin_unlock(&cifs_tcp_ses_lock); 2389 2390 cifs_setup_ipc(ses, ctx); 2391 2392 free_xid(xid); 2393 2394 return ses; 2395 2396 get_ses_fail: 2397 sesInfoFree(ses); 2398 free_xid(xid); 2399 return ERR_PTR(rc); 2400 } 2401 2402 /* this function must be called with tc_lock held */ 2403 static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx) 2404 { 2405 struct TCP_Server_Info *server = tcon->ses->server; 2406 2407 if (tcon->status == TID_EXITING) 2408 return 0; 2409 2410 if (tcon->origin_fullpath) { 2411 if (!ctx->source || 2412 !dfs_src_pathname_equal(ctx->source, 2413 tcon->origin_fullpath)) 2414 return 0; 2415 } else if (!server->leaf_fullpath && 2416 strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE)) { 2417 return 0; 2418 } 2419 if (tcon->seal != ctx->seal) 2420 return 0; 2421 if (tcon->snapshot_time != ctx->snapshot_time) 2422 return 0; 2423 if (tcon->handle_timeout != ctx->handle_timeout) 2424 return 0; 2425 if (tcon->no_lease != ctx->no_lease) 2426 return 0; 2427 if (tcon->nodelete != ctx->nodelete) 2428 return 0; 2429 return 1; 2430 } 2431 2432 static struct cifs_tcon * 2433 cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) 2434 { 2435 struct cifs_tcon *tcon; 2436 2437 spin_lock(&cifs_tcp_ses_lock); 2438 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 2439 spin_lock(&tcon->tc_lock); 2440 if (!match_tcon(tcon, ctx)) { 2441 spin_unlock(&tcon->tc_lock); 2442 continue; 2443 } 2444 ++tcon->tc_count; 2445 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 2446 netfs_trace_tcon_ref_get_find); 2447 spin_unlock(&tcon->tc_lock); 2448 spin_unlock(&cifs_tcp_ses_lock); 2449 return tcon; 2450 } 2451 spin_unlock(&cifs_tcp_ses_lock); 2452 return NULL; 2453 } 2454 2455 void 2456 cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace) 2457 { 2458 unsigned int xid; 2459 struct cifs_ses *ses; 2460 LIST_HEAD(ses_list); 2461 2462 /* 2463 * IPC tcon share the lifetime of their session and are 2464 * destroyed in the session put function 2465 */ 2466 if (tcon == NULL || tcon->ipc) 2467 return; 2468 2469 ses = tcon->ses; 2470 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count); 2471 spin_lock(&cifs_tcp_ses_lock); 2472 spin_lock(&tcon->tc_lock); 2473 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count - 1, trace); 2474 if (--tcon->tc_count > 0) { 2475 spin_unlock(&tcon->tc_lock); 2476 spin_unlock(&cifs_tcp_ses_lock); 2477 return; 2478 } 2479 2480 /* tc_count can never go negative */ 2481 WARN_ON(tcon->tc_count < 0); 2482 2483 list_del_init(&tcon->tcon_list); 2484 tcon->status = TID_EXITING; 2485 #ifdef CONFIG_CIFS_DFS_UPCALL 2486 list_replace_init(&tcon->dfs_ses_list, &ses_list); 2487 #endif 2488 spin_unlock(&tcon->tc_lock); 2489 spin_unlock(&cifs_tcp_ses_lock); 2490 2491 /* cancel polling of interfaces */ 2492 cancel_delayed_work_sync(&tcon->query_interfaces); 2493 #ifdef CONFIG_CIFS_DFS_UPCALL 2494 cancel_delayed_work_sync(&tcon->dfs_cache_work); 2495 #endif 2496 2497 if (tcon->use_witness) { 2498 int rc; 2499 2500 rc = cifs_swn_unregister(tcon); 2501 if (rc < 0) { 2502 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n", 2503 __func__, rc); 2504 } 2505 } 2506 2507 xid = get_xid(); 2508 if (ses->server->ops->tree_disconnect) 2509 ses->server->ops->tree_disconnect(xid, tcon); 2510 _free_xid(xid); 2511 2512 cifs_fscache_release_super_cookie(tcon); 2513 tconInfoFree(tcon, netfs_trace_tcon_ref_free); 2514 cifs_put_smb_ses(ses); 2515 #ifdef CONFIG_CIFS_DFS_UPCALL 2516 dfs_put_root_smb_sessions(&ses_list); 2517 #endif 2518 } 2519 2520 /** 2521 * cifs_get_tcon - get a tcon matching @ctx data from @ses 2522 * @ses: smb session to issue the request on 2523 * @ctx: the superblock configuration context to use for building the 2524 * 2525 * - tcon refcount is the number of mount points using the tcon. 2526 * - ses refcount is the number of tcon using the session. 2527 * 2528 * 1. This function assumes it is being called from cifs_mount() where 2529 * we already got a session reference (ses refcount +1). 2530 * 2531 * 2. Since we're in the context of adding a mount point, the end 2532 * result should be either: 2533 * 2534 * a) a new tcon already allocated with refcount=1 (1 mount point) and 2535 * its session refcount incremented (1 new tcon). This +1 was 2536 * already done in (1). 2537 * 2538 * b) an existing tcon with refcount+1 (add a mount point to it) and 2539 * identical ses refcount (no new tcon). Because of (1) we need to 2540 * decrement the ses refcount. 2541 */ 2542 static struct cifs_tcon * 2543 cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) 2544 { 2545 struct cifs_tcon *tcon; 2546 bool nohandlecache; 2547 int rc, xid; 2548 2549 tcon = cifs_find_tcon(ses, ctx); 2550 if (tcon) { 2551 /* 2552 * tcon has refcount already incremented but we need to 2553 * decrement extra ses reference gotten by caller (case b) 2554 */ 2555 cifs_dbg(FYI, "Found match on UNC path\n"); 2556 cifs_put_smb_ses(ses); 2557 return tcon; 2558 } 2559 2560 if (!ses->server->ops->tree_connect) { 2561 rc = -ENOSYS; 2562 goto out_fail; 2563 } 2564 2565 if (ses->server->dialect >= SMB20_PROT_ID && 2566 (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)) 2567 nohandlecache = ctx->nohandlecache; 2568 else 2569 nohandlecache = true; 2570 tcon = tcon_info_alloc(!nohandlecache, netfs_trace_tcon_ref_new); 2571 if (tcon == NULL) { 2572 rc = -ENOMEM; 2573 goto out_fail; 2574 } 2575 tcon->nohandlecache = nohandlecache; 2576 2577 if (ctx->snapshot_time) { 2578 if (ses->server->vals->protocol_id == 0) { 2579 cifs_dbg(VFS, 2580 "Use SMB2 or later for snapshot mount option\n"); 2581 rc = -EOPNOTSUPP; 2582 goto out_fail; 2583 } else 2584 tcon->snapshot_time = ctx->snapshot_time; 2585 } 2586 2587 if (ctx->handle_timeout) { 2588 if (ses->server->vals->protocol_id == 0) { 2589 cifs_dbg(VFS, 2590 "Use SMB2.1 or later for handle timeout option\n"); 2591 rc = -EOPNOTSUPP; 2592 goto out_fail; 2593 } else 2594 tcon->handle_timeout = ctx->handle_timeout; 2595 } 2596 2597 tcon->ses = ses; 2598 if (ctx->password) { 2599 tcon->password = kstrdup(ctx->password, GFP_KERNEL); 2600 if (!tcon->password) { 2601 rc = -ENOMEM; 2602 goto out_fail; 2603 } 2604 } 2605 2606 if (ctx->seal) { 2607 if (ses->server->vals->protocol_id == 0) { 2608 cifs_dbg(VFS, 2609 "SMB3 or later required for encryption\n"); 2610 rc = -EOPNOTSUPP; 2611 goto out_fail; 2612 } else if (tcon->ses->server->capabilities & 2613 SMB2_GLOBAL_CAP_ENCRYPTION) 2614 tcon->seal = true; 2615 else { 2616 cifs_dbg(VFS, "Encryption is not supported on share\n"); 2617 rc = -EOPNOTSUPP; 2618 goto out_fail; 2619 } 2620 } 2621 2622 if (ctx->linux_ext) { 2623 if (ses->server->posix_ext_supported) { 2624 tcon->posix_extensions = true; 2625 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n"); 2626 } else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) || 2627 (strcmp(ses->server->vals->version_string, 2628 SMB3ANY_VERSION_STRING) == 0) || 2629 (strcmp(ses->server->vals->version_string, 2630 SMBDEFAULT_VERSION_STRING) == 0)) { 2631 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n"); 2632 rc = -EOPNOTSUPP; 2633 goto out_fail; 2634 } else if (ses->server->vals->protocol_id == SMB10_PROT_ID) 2635 if (cap_unix(ses)) 2636 cifs_dbg(FYI, "Unix Extensions requested on SMB1 mount\n"); 2637 else { 2638 cifs_dbg(VFS, "SMB1 Unix Extensions not supported by server\n"); 2639 rc = -EOPNOTSUPP; 2640 goto out_fail; 2641 } else { 2642 cifs_dbg(VFS, 2643 "Check vers= mount option. SMB3.11 disabled but required for POSIX extensions\n"); 2644 rc = -EOPNOTSUPP; 2645 goto out_fail; 2646 } 2647 } 2648 2649 xid = get_xid(); 2650 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon, 2651 ctx->local_nls); 2652 free_xid(xid); 2653 cifs_dbg(FYI, "Tcon rc = %d\n", rc); 2654 if (rc) 2655 goto out_fail; 2656 2657 tcon->use_persistent = false; 2658 /* check if SMB2 or later, CIFS does not support persistent handles */ 2659 if (ctx->persistent) { 2660 if (ses->server->vals->protocol_id == 0) { 2661 cifs_dbg(VFS, 2662 "SMB3 or later required for persistent handles\n"); 2663 rc = -EOPNOTSUPP; 2664 goto out_fail; 2665 } else if (ses->server->capabilities & 2666 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) 2667 tcon->use_persistent = true; 2668 else /* persistent handles requested but not supported */ { 2669 cifs_dbg(VFS, 2670 "Persistent handles not supported on share\n"); 2671 rc = -EOPNOTSUPP; 2672 goto out_fail; 2673 } 2674 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY) 2675 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) 2676 && (ctx->nopersistent == false)) { 2677 cifs_dbg(FYI, "enabling persistent handles\n"); 2678 tcon->use_persistent = true; 2679 } else if (ctx->resilient) { 2680 if (ses->server->vals->protocol_id == 0) { 2681 cifs_dbg(VFS, 2682 "SMB2.1 or later required for resilient handles\n"); 2683 rc = -EOPNOTSUPP; 2684 goto out_fail; 2685 } 2686 tcon->use_resilient = true; 2687 } 2688 2689 tcon->use_witness = false; 2690 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) { 2691 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) { 2692 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) { 2693 /* 2694 * Set witness in use flag in first place 2695 * to retry registration in the echo task 2696 */ 2697 tcon->use_witness = true; 2698 /* And try to register immediately */ 2699 rc = cifs_swn_register(tcon); 2700 if (rc < 0) { 2701 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc); 2702 goto out_fail; 2703 } 2704 } else { 2705 /* TODO: try to extend for non-cluster uses (eg multichannel) */ 2706 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n"); 2707 rc = -EOPNOTSUPP; 2708 goto out_fail; 2709 } 2710 } else { 2711 cifs_dbg(VFS, "SMB3 or later required for witness option\n"); 2712 rc = -EOPNOTSUPP; 2713 goto out_fail; 2714 } 2715 } 2716 2717 /* If the user really knows what they are doing they can override */ 2718 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) { 2719 if (ctx->cache_ro) 2720 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n"); 2721 else if (ctx->cache_rw) 2722 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n"); 2723 } 2724 2725 if (ctx->no_lease) { 2726 if (ses->server->vals->protocol_id == 0) { 2727 cifs_dbg(VFS, 2728 "SMB2 or later required for nolease option\n"); 2729 rc = -EOPNOTSUPP; 2730 goto out_fail; 2731 } else 2732 tcon->no_lease = ctx->no_lease; 2733 } 2734 2735 /* 2736 * We can have only one retry value for a connection to a share so for 2737 * resources mounted more than once to the same server share the last 2738 * value passed in for the retry flag is used. 2739 */ 2740 tcon->retry = ctx->retry; 2741 tcon->nocase = ctx->nocase; 2742 tcon->broken_sparse_sup = ctx->no_sparse; 2743 tcon->max_cached_dirs = ctx->max_cached_dirs; 2744 tcon->nodelete = ctx->nodelete; 2745 tcon->local_lease = ctx->local_lease; 2746 INIT_LIST_HEAD(&tcon->pending_opens); 2747 tcon->status = TID_GOOD; 2748 2749 INIT_DELAYED_WORK(&tcon->query_interfaces, 2750 smb2_query_server_interfaces); 2751 if (ses->server->dialect >= SMB30_PROT_ID && 2752 (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) { 2753 /* schedule query interfaces poll */ 2754 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces, 2755 (SMB_INTERFACE_POLL_INTERVAL * HZ)); 2756 } 2757 #ifdef CONFIG_CIFS_DFS_UPCALL 2758 INIT_DELAYED_WORK(&tcon->dfs_cache_work, dfs_cache_refresh); 2759 #endif 2760 spin_lock(&cifs_tcp_ses_lock); 2761 list_add(&tcon->tcon_list, &ses->tcon_list); 2762 spin_unlock(&cifs_tcp_ses_lock); 2763 2764 return tcon; 2765 2766 out_fail: 2767 tconInfoFree(tcon, netfs_trace_tcon_ref_free_fail); 2768 return ERR_PTR(rc); 2769 } 2770 2771 void 2772 cifs_put_tlink(struct tcon_link *tlink) 2773 { 2774 if (!tlink || IS_ERR(tlink)) 2775 return; 2776 2777 if (!atomic_dec_and_test(&tlink->tl_count) || 2778 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) { 2779 tlink->tl_time = jiffies; 2780 return; 2781 } 2782 2783 if (!IS_ERR(tlink_tcon(tlink))) 2784 cifs_put_tcon(tlink_tcon(tlink), netfs_trace_tcon_ref_put_tlink); 2785 kfree(tlink); 2786 } 2787 2788 static int 2789 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data) 2790 { 2791 struct cifs_sb_info *old = CIFS_SB(sb); 2792 struct cifs_sb_info *new = mnt_data->cifs_sb; 2793 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK; 2794 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK; 2795 2796 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK)) 2797 return 0; 2798 2799 if (old->mnt_cifs_serverino_autodisabled) 2800 newflags &= ~CIFS_MOUNT_SERVER_INUM; 2801 2802 if (oldflags != newflags) 2803 return 0; 2804 2805 /* 2806 * We want to share sb only if we don't specify an r/wsize or 2807 * specified r/wsize is greater than or equal to existing one. 2808 */ 2809 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize) 2810 return 0; 2811 2812 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize) 2813 return 0; 2814 2815 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) || 2816 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid)) 2817 return 0; 2818 2819 if (old->ctx->file_mode != new->ctx->file_mode || 2820 old->ctx->dir_mode != new->ctx->dir_mode) 2821 return 0; 2822 2823 if (strcmp(old->local_nls->charset, new->local_nls->charset)) 2824 return 0; 2825 2826 if (old->ctx->acregmax != new->ctx->acregmax) 2827 return 0; 2828 if (old->ctx->acdirmax != new->ctx->acdirmax) 2829 return 0; 2830 if (old->ctx->closetimeo != new->ctx->closetimeo) 2831 return 0; 2832 if (old->ctx->reparse_type != new->ctx->reparse_type) 2833 return 0; 2834 2835 return 1; 2836 } 2837 2838 static int match_prepath(struct super_block *sb, 2839 struct cifs_tcon *tcon, 2840 struct cifs_mnt_data *mnt_data) 2841 { 2842 struct smb3_fs_context *ctx = mnt_data->ctx; 2843 struct cifs_sb_info *old = CIFS_SB(sb); 2844 struct cifs_sb_info *new = mnt_data->cifs_sb; 2845 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && 2846 old->prepath; 2847 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && 2848 new->prepath; 2849 2850 if (tcon->origin_fullpath && 2851 dfs_src_pathname_equal(tcon->origin_fullpath, ctx->source)) 2852 return 1; 2853 2854 if (old_set && new_set && !strcmp(new->prepath, old->prepath)) 2855 return 1; 2856 else if (!old_set && !new_set) 2857 return 1; 2858 2859 return 0; 2860 } 2861 2862 int 2863 cifs_match_super(struct super_block *sb, void *data) 2864 { 2865 struct cifs_mnt_data *mnt_data = data; 2866 struct smb3_fs_context *ctx; 2867 struct cifs_sb_info *cifs_sb; 2868 struct TCP_Server_Info *tcp_srv; 2869 struct cifs_ses *ses; 2870 struct cifs_tcon *tcon; 2871 struct tcon_link *tlink; 2872 int rc = 0; 2873 2874 spin_lock(&cifs_tcp_ses_lock); 2875 cifs_sb = CIFS_SB(sb); 2876 2877 /* We do not want to use a superblock that has been shutdown */ 2878 if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) { 2879 spin_unlock(&cifs_tcp_ses_lock); 2880 return 0; 2881 } 2882 2883 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); 2884 if (IS_ERR_OR_NULL(tlink)) { 2885 pr_warn_once("%s: skip super matching due to bad tlink(%p)\n", 2886 __func__, tlink); 2887 spin_unlock(&cifs_tcp_ses_lock); 2888 return 0; 2889 } 2890 tcon = tlink_tcon(tlink); 2891 ses = tcon->ses; 2892 tcp_srv = ses->server; 2893 2894 ctx = mnt_data->ctx; 2895 2896 spin_lock(&tcp_srv->srv_lock); 2897 spin_lock(&ses->ses_lock); 2898 spin_lock(&ses->chan_lock); 2899 spin_lock(&tcon->tc_lock); 2900 if (!match_server(tcp_srv, ctx, true) || 2901 !match_session(ses, ctx, true) || 2902 !match_tcon(tcon, ctx) || 2903 !match_prepath(sb, tcon, mnt_data)) { 2904 rc = 0; 2905 goto out; 2906 } 2907 2908 rc = compare_mount_options(sb, mnt_data); 2909 out: 2910 spin_unlock(&tcon->tc_lock); 2911 spin_unlock(&ses->chan_lock); 2912 spin_unlock(&ses->ses_lock); 2913 spin_unlock(&tcp_srv->srv_lock); 2914 2915 spin_unlock(&cifs_tcp_ses_lock); 2916 cifs_put_tlink(tlink); 2917 return rc; 2918 } 2919 2920 #ifdef CONFIG_DEBUG_LOCK_ALLOC 2921 static struct lock_class_key cifs_key[2]; 2922 static struct lock_class_key cifs_slock_key[2]; 2923 2924 static inline void 2925 cifs_reclassify_socket4(struct socket *sock) 2926 { 2927 struct sock *sk = sock->sk; 2928 2929 BUG_ON(!sock_allow_reclassification(sk)); 2930 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS", 2931 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]); 2932 } 2933 2934 static inline void 2935 cifs_reclassify_socket6(struct socket *sock) 2936 { 2937 struct sock *sk = sock->sk; 2938 2939 BUG_ON(!sock_allow_reclassification(sk)); 2940 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS", 2941 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]); 2942 } 2943 #else 2944 static inline void 2945 cifs_reclassify_socket4(struct socket *sock) 2946 { 2947 } 2948 2949 static inline void 2950 cifs_reclassify_socket6(struct socket *sock) 2951 { 2952 } 2953 #endif 2954 2955 /* See RFC1001 section 14 on representation of Netbios names */ 2956 static void rfc1002mangle(char *target, char *source, unsigned int length) 2957 { 2958 unsigned int i, j; 2959 2960 for (i = 0, j = 0; i < (length); i++) { 2961 /* mask a nibble at a time and encode */ 2962 target[j] = 'A' + (0x0F & (source[i] >> 4)); 2963 target[j+1] = 'A' + (0x0F & source[i]); 2964 j += 2; 2965 } 2966 2967 } 2968 2969 static int 2970 bind_socket(struct TCP_Server_Info *server) 2971 { 2972 int rc = 0; 2973 2974 if (server->srcaddr.ss_family != AF_UNSPEC) { 2975 /* Bind to the specified local IP address */ 2976 struct socket *socket = server->ssocket; 2977 2978 rc = kernel_bind(socket, 2979 (struct sockaddr *) &server->srcaddr, 2980 sizeof(server->srcaddr)); 2981 if (rc < 0) { 2982 struct sockaddr_in *saddr4; 2983 struct sockaddr_in6 *saddr6; 2984 2985 saddr4 = (struct sockaddr_in *)&server->srcaddr; 2986 saddr6 = (struct sockaddr_in6 *)&server->srcaddr; 2987 if (saddr6->sin6_family == AF_INET6) 2988 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n", 2989 &saddr6->sin6_addr, rc); 2990 else 2991 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n", 2992 &saddr4->sin_addr.s_addr, rc); 2993 } 2994 } 2995 return rc; 2996 } 2997 2998 static int 2999 ip_rfc1001_connect(struct TCP_Server_Info *server) 3000 { 3001 int rc = 0; 3002 /* 3003 * some servers require RFC1001 sessinit before sending 3004 * negprot - BB check reconnection in case where second 3005 * sessinit is sent but no second negprot 3006 */ 3007 struct rfc1002_session_packet req = {}; 3008 struct smb_hdr *smb_buf = (struct smb_hdr *)&req; 3009 unsigned int len; 3010 3011 req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name); 3012 3013 if (server->server_RFC1001_name[0] != 0) 3014 rfc1002mangle(req.trailer.session_req.called_name, 3015 server->server_RFC1001_name, 3016 RFC1001_NAME_LEN_WITH_NULL); 3017 else 3018 rfc1002mangle(req.trailer.session_req.called_name, 3019 DEFAULT_CIFS_CALLED_NAME, 3020 RFC1001_NAME_LEN_WITH_NULL); 3021 3022 req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name); 3023 3024 /* calling name ends in null (byte 16) from old smb convention */ 3025 if (server->workstation_RFC1001_name[0] != 0) 3026 rfc1002mangle(req.trailer.session_req.calling_name, 3027 server->workstation_RFC1001_name, 3028 RFC1001_NAME_LEN_WITH_NULL); 3029 else 3030 rfc1002mangle(req.trailer.session_req.calling_name, 3031 "LINUX_CIFS_CLNT", 3032 RFC1001_NAME_LEN_WITH_NULL); 3033 3034 /* 3035 * As per rfc1002, @len must be the number of bytes that follows the 3036 * length field of a rfc1002 session request payload. 3037 */ 3038 len = sizeof(req) - offsetof(struct rfc1002_session_packet, trailer.session_req); 3039 3040 smb_buf->smb_buf_length = cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | len); 3041 rc = smb_send(server, smb_buf, len); 3042 /* 3043 * RFC1001 layer in at least one server requires very short break before 3044 * negprot presumably because not expecting negprot to follow so fast. 3045 * This is a simple solution that works without complicating the code 3046 * and causes no significant slowing down on mount for everyone else 3047 */ 3048 usleep_range(1000, 2000); 3049 3050 return rc; 3051 } 3052 3053 static int 3054 generic_ip_connect(struct TCP_Server_Info *server) 3055 { 3056 struct sockaddr *saddr; 3057 struct socket *socket; 3058 int slen, sfamily; 3059 __be16 sport; 3060 int rc = 0; 3061 3062 saddr = (struct sockaddr *) &server->dstaddr; 3063 3064 if (server->dstaddr.ss_family == AF_INET6) { 3065 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr; 3066 3067 sport = ipv6->sin6_port; 3068 slen = sizeof(struct sockaddr_in6); 3069 sfamily = AF_INET6; 3070 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr, 3071 ntohs(sport)); 3072 } else { 3073 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr; 3074 3075 sport = ipv4->sin_port; 3076 slen = sizeof(struct sockaddr_in); 3077 sfamily = AF_INET; 3078 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr, 3079 ntohs(sport)); 3080 } 3081 3082 if (server->ssocket) { 3083 socket = server->ssocket; 3084 } else { 3085 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM, 3086 IPPROTO_TCP, &server->ssocket, 1); 3087 if (rc < 0) { 3088 cifs_server_dbg(VFS, "Error %d creating socket\n", rc); 3089 return rc; 3090 } 3091 3092 /* BB other socket options to set KEEPALIVE, NODELAY? */ 3093 cifs_dbg(FYI, "Socket created\n"); 3094 socket = server->ssocket; 3095 socket->sk->sk_allocation = GFP_NOFS; 3096 socket->sk->sk_use_task_frag = false; 3097 if (sfamily == AF_INET6) 3098 cifs_reclassify_socket6(socket); 3099 else 3100 cifs_reclassify_socket4(socket); 3101 } 3102 3103 rc = bind_socket(server); 3104 if (rc < 0) 3105 return rc; 3106 3107 /* 3108 * Eventually check for other socket options to change from 3109 * the default. sock_setsockopt not used because it expects 3110 * user space buffer 3111 */ 3112 socket->sk->sk_rcvtimeo = 7 * HZ; 3113 socket->sk->sk_sndtimeo = 5 * HZ; 3114 3115 /* make the bufsizes depend on wsize/rsize and max requests */ 3116 if (server->noautotune) { 3117 if (socket->sk->sk_sndbuf < (200 * 1024)) 3118 socket->sk->sk_sndbuf = 200 * 1024; 3119 if (socket->sk->sk_rcvbuf < (140 * 1024)) 3120 socket->sk->sk_rcvbuf = 140 * 1024; 3121 } 3122 3123 if (server->tcp_nodelay) 3124 tcp_sock_set_nodelay(socket->sk); 3125 3126 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n", 3127 socket->sk->sk_sndbuf, 3128 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo); 3129 3130 rc = kernel_connect(socket, saddr, slen, 3131 server->noblockcnt ? O_NONBLOCK : 0); 3132 /* 3133 * When mounting SMB root file systems, we do not want to block in 3134 * connect. Otherwise bail out and then let cifs_reconnect() perform 3135 * reconnect failover - if possible. 3136 */ 3137 if (server->noblockcnt && rc == -EINPROGRESS) 3138 rc = 0; 3139 if (rc < 0) { 3140 cifs_dbg(FYI, "Error %d connecting to server\n", rc); 3141 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc); 3142 sock_release(socket); 3143 server->ssocket = NULL; 3144 return rc; 3145 } 3146 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr); 3147 if (sport == htons(RFC1001_PORT)) 3148 rc = ip_rfc1001_connect(server); 3149 3150 return rc; 3151 } 3152 3153 static int 3154 ip_connect(struct TCP_Server_Info *server) 3155 { 3156 __be16 *sport; 3157 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; 3158 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; 3159 3160 if (server->dstaddr.ss_family == AF_INET6) 3161 sport = &addr6->sin6_port; 3162 else 3163 sport = &addr->sin_port; 3164 3165 if (*sport == 0) { 3166 int rc; 3167 3168 /* try with 445 port at first */ 3169 *sport = htons(CIFS_PORT); 3170 3171 rc = generic_ip_connect(server); 3172 if (rc >= 0) 3173 return rc; 3174 3175 /* if it failed, try with 139 port */ 3176 *sport = htons(RFC1001_PORT); 3177 } 3178 3179 return generic_ip_connect(server); 3180 } 3181 3182 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 3183 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon, 3184 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) 3185 { 3186 /* 3187 * If we are reconnecting then should we check to see if 3188 * any requested capabilities changed locally e.g. via 3189 * remount but we can not do much about it here 3190 * if they have (even if we could detect it by the following) 3191 * Perhaps we could add a backpointer to array of sb from tcon 3192 * or if we change to make all sb to same share the same 3193 * sb as NFS - then we only have one backpointer to sb. 3194 * What if we wanted to mount the server share twice once with 3195 * and once without posixacls or posix paths? 3196 */ 3197 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability); 3198 3199 if (ctx && ctx->no_linux_ext) { 3200 tcon->fsUnixInfo.Capability = 0; 3201 tcon->unix_ext = 0; /* Unix Extensions disabled */ 3202 cifs_dbg(FYI, "Linux protocol extensions disabled\n"); 3203 return; 3204 } else if (ctx) 3205 tcon->unix_ext = 1; /* Unix Extensions supported */ 3206 3207 if (!tcon->unix_ext) { 3208 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n"); 3209 return; 3210 } 3211 3212 if (!CIFSSMBQFSUnixInfo(xid, tcon)) { 3213 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability); 3214 3215 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap); 3216 /* 3217 * check for reconnect case in which we do not 3218 * want to change the mount behavior if we can avoid it 3219 */ 3220 if (ctx == NULL) { 3221 /* 3222 * turn off POSIX ACL and PATHNAMES if not set 3223 * originally at mount time 3224 */ 3225 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0) 3226 cap &= ~CIFS_UNIX_POSIX_ACL_CAP; 3227 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { 3228 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) 3229 cifs_dbg(VFS, "POSIXPATH support change\n"); 3230 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; 3231 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { 3232 cifs_dbg(VFS, "possible reconnect error\n"); 3233 cifs_dbg(VFS, "server disabled POSIX path support\n"); 3234 } 3235 } 3236 3237 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) 3238 cifs_dbg(VFS, "per-share encryption not supported yet\n"); 3239 3240 cap &= CIFS_UNIX_CAP_MASK; 3241 if (ctx && ctx->no_psx_acl) 3242 cap &= ~CIFS_UNIX_POSIX_ACL_CAP; 3243 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) { 3244 cifs_dbg(FYI, "negotiated posix acl support\n"); 3245 if (cifs_sb) 3246 cifs_sb->mnt_cifs_flags |= 3247 CIFS_MOUNT_POSIXACL; 3248 } 3249 3250 if (ctx && ctx->posix_paths == 0) 3251 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; 3252 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) { 3253 cifs_dbg(FYI, "negotiate posix pathnames\n"); 3254 if (cifs_sb) 3255 cifs_sb->mnt_cifs_flags |= 3256 CIFS_MOUNT_POSIX_PATHS; 3257 } 3258 3259 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap); 3260 #ifdef CONFIG_CIFS_DEBUG2 3261 if (cap & CIFS_UNIX_FCNTL_CAP) 3262 cifs_dbg(FYI, "FCNTL cap\n"); 3263 if (cap & CIFS_UNIX_EXTATTR_CAP) 3264 cifs_dbg(FYI, "EXTATTR cap\n"); 3265 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) 3266 cifs_dbg(FYI, "POSIX path cap\n"); 3267 if (cap & CIFS_UNIX_XATTR_CAP) 3268 cifs_dbg(FYI, "XATTR cap\n"); 3269 if (cap & CIFS_UNIX_POSIX_ACL_CAP) 3270 cifs_dbg(FYI, "POSIX ACL cap\n"); 3271 if (cap & CIFS_UNIX_LARGE_READ_CAP) 3272 cifs_dbg(FYI, "very large read cap\n"); 3273 if (cap & CIFS_UNIX_LARGE_WRITE_CAP) 3274 cifs_dbg(FYI, "very large write cap\n"); 3275 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) 3276 cifs_dbg(FYI, "transport encryption cap\n"); 3277 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) 3278 cifs_dbg(FYI, "mandatory transport encryption cap\n"); 3279 #endif /* CIFS_DEBUG2 */ 3280 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) { 3281 if (ctx == NULL) 3282 cifs_dbg(FYI, "resetting capabilities failed\n"); 3283 else 3284 cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n"); 3285 3286 } 3287 } 3288 } 3289 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 3290 3291 int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb) 3292 { 3293 struct smb3_fs_context *ctx = cifs_sb->ctx; 3294 3295 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks); 3296 3297 spin_lock_init(&cifs_sb->tlink_tree_lock); 3298 cifs_sb->tlink_tree = RB_ROOT; 3299 3300 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n", 3301 ctx->file_mode, ctx->dir_mode); 3302 3303 /* this is needed for ASCII cp to Unicode converts */ 3304 if (ctx->iocharset == NULL) { 3305 /* load_nls_default cannot return null */ 3306 cifs_sb->local_nls = load_nls_default(); 3307 } else { 3308 cifs_sb->local_nls = load_nls(ctx->iocharset); 3309 if (cifs_sb->local_nls == NULL) { 3310 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n", 3311 ctx->iocharset); 3312 return -ELIBACC; 3313 } 3314 } 3315 ctx->local_nls = cifs_sb->local_nls; 3316 3317 smb3_update_mnt_flags(cifs_sb); 3318 3319 if (ctx->direct_io) 3320 cifs_dbg(FYI, "mounting share using direct i/o\n"); 3321 if (ctx->cache_ro) { 3322 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n"); 3323 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE; 3324 } else if (ctx->cache_rw) { 3325 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n"); 3326 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE | 3327 CIFS_MOUNT_RW_CACHE); 3328 } 3329 3330 if ((ctx->cifs_acl) && (ctx->dynperm)) 3331 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n"); 3332 3333 if (ctx->prepath) { 3334 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL); 3335 if (cifs_sb->prepath == NULL) 3336 return -ENOMEM; 3337 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 3338 } 3339 3340 return 0; 3341 } 3342 3343 /* Release all succeed connections */ 3344 void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx) 3345 { 3346 int rc = 0; 3347 3348 if (mnt_ctx->tcon) 3349 cifs_put_tcon(mnt_ctx->tcon, netfs_trace_tcon_ref_put_mnt_ctx); 3350 else if (mnt_ctx->ses) 3351 cifs_put_smb_ses(mnt_ctx->ses); 3352 else if (mnt_ctx->server) 3353 cifs_put_tcp_session(mnt_ctx->server, 0); 3354 mnt_ctx->ses = NULL; 3355 mnt_ctx->tcon = NULL; 3356 mnt_ctx->server = NULL; 3357 mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS; 3358 free_xid(mnt_ctx->xid); 3359 } 3360 3361 int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx) 3362 { 3363 struct TCP_Server_Info *server = NULL; 3364 struct smb3_fs_context *ctx; 3365 struct cifs_ses *ses = NULL; 3366 unsigned int xid; 3367 int rc = 0; 3368 3369 xid = get_xid(); 3370 3371 if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) { 3372 rc = -EINVAL; 3373 goto out; 3374 } 3375 ctx = mnt_ctx->fs_ctx; 3376 3377 /* get a reference to a tcp session */ 3378 server = cifs_get_tcp_session(ctx, NULL); 3379 if (IS_ERR(server)) { 3380 rc = PTR_ERR(server); 3381 server = NULL; 3382 goto out; 3383 } 3384 3385 /* get a reference to a SMB session */ 3386 ses = cifs_get_smb_ses(server, ctx); 3387 if (IS_ERR(ses)) { 3388 rc = PTR_ERR(ses); 3389 ses = NULL; 3390 goto out; 3391 } 3392 3393 if ((ctx->persistent == true) && (!(ses->server->capabilities & 3394 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) { 3395 cifs_server_dbg(VFS, "persistent handles not supported by server\n"); 3396 rc = -EOPNOTSUPP; 3397 } 3398 3399 out: 3400 mnt_ctx->xid = xid; 3401 mnt_ctx->server = server; 3402 mnt_ctx->ses = ses; 3403 mnt_ctx->tcon = NULL; 3404 3405 return rc; 3406 } 3407 3408 int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx) 3409 { 3410 struct TCP_Server_Info *server; 3411 struct cifs_sb_info *cifs_sb; 3412 struct smb3_fs_context *ctx; 3413 struct cifs_tcon *tcon = NULL; 3414 int rc = 0; 3415 3416 if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx || 3417 !mnt_ctx->cifs_sb)) { 3418 rc = -EINVAL; 3419 goto out; 3420 } 3421 server = mnt_ctx->server; 3422 ctx = mnt_ctx->fs_ctx; 3423 cifs_sb = mnt_ctx->cifs_sb; 3424 3425 /* search for existing tcon to this server share */ 3426 tcon = cifs_get_tcon(mnt_ctx->ses, ctx); 3427 if (IS_ERR(tcon)) { 3428 rc = PTR_ERR(tcon); 3429 tcon = NULL; 3430 goto out; 3431 } 3432 3433 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */ 3434 if (tcon->posix_extensions) 3435 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS; 3436 3437 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 3438 /* tell server which Unix caps we support */ 3439 if (cap_unix(tcon->ses)) { 3440 /* 3441 * reset of caps checks mount to see if unix extensions disabled 3442 * for just this mount. 3443 */ 3444 reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx); 3445 spin_lock(&tcon->ses->server->srv_lock); 3446 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) && 3447 (le64_to_cpu(tcon->fsUnixInfo.Capability) & 3448 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) { 3449 spin_unlock(&tcon->ses->server->srv_lock); 3450 rc = -EACCES; 3451 goto out; 3452 } 3453 spin_unlock(&tcon->ses->server->srv_lock); 3454 } else 3455 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 3456 tcon->unix_ext = 0; /* server does not support them */ 3457 3458 /* do not care if a following call succeed - informational */ 3459 if (!tcon->pipe && server->ops->qfs_tcon) { 3460 server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb); 3461 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) { 3462 if (tcon->fsDevInfo.DeviceCharacteristics & 3463 cpu_to_le32(FILE_READ_ONLY_DEVICE)) 3464 cifs_dbg(VFS, "mounted to read only share\n"); 3465 else if ((cifs_sb->mnt_cifs_flags & 3466 CIFS_MOUNT_RW_CACHE) == 0) 3467 cifs_dbg(VFS, "read only mount of RW share\n"); 3468 /* no need to log a RW mount of a typical RW share */ 3469 } 3470 } 3471 3472 /* 3473 * Clamp the rsize/wsize mount arguments if they are too big for the server 3474 * and set the rsize/wsize to the negotiated values if not passed in by 3475 * the user on mount 3476 */ 3477 if ((cifs_sb->ctx->wsize == 0) || 3478 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx))) { 3479 cifs_sb->ctx->wsize = 3480 round_down(server->ops->negotiate_wsize(tcon, ctx), PAGE_SIZE); 3481 /* 3482 * in the very unlikely event that the server sent a max write size under PAGE_SIZE, 3483 * (which would get rounded down to 0) then reset wsize to absolute minimum eg 4096 3484 */ 3485 if (cifs_sb->ctx->wsize == 0) { 3486 cifs_sb->ctx->wsize = PAGE_SIZE; 3487 cifs_dbg(VFS, "wsize too small, reset to minimum ie PAGE_SIZE, usually 4096\n"); 3488 } 3489 } 3490 if ((cifs_sb->ctx->rsize == 0) || 3491 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx))) 3492 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx); 3493 3494 /* 3495 * The cookie is initialized from volume info returned above. 3496 * Inside cifs_fscache_get_super_cookie it checks 3497 * that we do not get super cookie twice. 3498 */ 3499 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE) 3500 cifs_fscache_get_super_cookie(tcon); 3501 3502 out: 3503 mnt_ctx->tcon = tcon; 3504 return rc; 3505 } 3506 3507 static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses, 3508 struct cifs_tcon *tcon) 3509 { 3510 struct tcon_link *tlink; 3511 3512 /* hang the tcon off of the superblock */ 3513 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL); 3514 if (tlink == NULL) 3515 return -ENOMEM; 3516 3517 tlink->tl_uid = ses->linux_uid; 3518 tlink->tl_tcon = tcon; 3519 tlink->tl_time = jiffies; 3520 set_bit(TCON_LINK_MASTER, &tlink->tl_flags); 3521 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); 3522 3523 cifs_sb->master_tlink = tlink; 3524 spin_lock(&cifs_sb->tlink_tree_lock); 3525 tlink_rb_insert(&cifs_sb->tlink_tree, tlink); 3526 spin_unlock(&cifs_sb->tlink_tree_lock); 3527 3528 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, 3529 TLINK_IDLE_EXPIRE); 3530 return 0; 3531 } 3532 3533 static int 3534 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server, 3535 unsigned int xid, 3536 struct cifs_tcon *tcon, 3537 struct cifs_sb_info *cifs_sb, 3538 char *full_path, 3539 int added_treename) 3540 { 3541 int rc; 3542 char *s; 3543 char sep, tmp; 3544 int skip = added_treename ? 1 : 0; 3545 3546 sep = CIFS_DIR_SEP(cifs_sb); 3547 s = full_path; 3548 3549 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, ""); 3550 while (rc == 0) { 3551 /* skip separators */ 3552 while (*s == sep) 3553 s++; 3554 if (!*s) 3555 break; 3556 /* next separator */ 3557 while (*s && *s != sep) 3558 s++; 3559 /* 3560 * if the treename is added, we then have to skip the first 3561 * part within the separators 3562 */ 3563 if (skip) { 3564 skip = 0; 3565 continue; 3566 } 3567 /* 3568 * temporarily null-terminate the path at the end of 3569 * the current component 3570 */ 3571 tmp = *s; 3572 *s = 0; 3573 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, 3574 full_path); 3575 *s = tmp; 3576 } 3577 return rc; 3578 } 3579 3580 /* 3581 * Check if path is remote (i.e. a DFS share). 3582 * 3583 * Return -EREMOTE if it is, otherwise 0 or -errno. 3584 */ 3585 int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx) 3586 { 3587 int rc; 3588 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; 3589 struct TCP_Server_Info *server = mnt_ctx->server; 3590 unsigned int xid = mnt_ctx->xid; 3591 struct cifs_tcon *tcon = mnt_ctx->tcon; 3592 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 3593 char *full_path; 3594 3595 if (!server->ops->is_path_accessible) 3596 return -EOPNOTSUPP; 3597 3598 /* 3599 * cifs_build_path_to_root works only when we have a valid tcon 3600 */ 3601 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon, 3602 tcon->Flags & SMB_SHARE_IS_IN_DFS); 3603 if (full_path == NULL) 3604 return -ENOMEM; 3605 3606 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path); 3607 3608 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, 3609 full_path); 3610 if (rc != 0 && rc != -EREMOTE) 3611 goto out; 3612 3613 if (rc != -EREMOTE) { 3614 rc = cifs_are_all_path_components_accessible(server, xid, tcon, 3615 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS); 3616 if (rc != 0) { 3617 cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n"); 3618 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 3619 rc = 0; 3620 } 3621 } 3622 3623 out: 3624 kfree(full_path); 3625 return rc; 3626 } 3627 3628 #ifdef CONFIG_CIFS_DFS_UPCALL 3629 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) 3630 { 3631 struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; 3632 int rc; 3633 3634 rc = dfs_mount_share(&mnt_ctx); 3635 if (rc) 3636 goto error; 3637 if (!ctx->dfs_conn) 3638 goto out; 3639 3640 /* 3641 * After reconnecting to a different server, unique ids won't match anymore, so we disable 3642 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE). 3643 */ 3644 cifs_autodisable_serverino(cifs_sb); 3645 /* 3646 * Force the use of prefix path to support failover on DFS paths that resolve to targets 3647 * that have different prefix paths. 3648 */ 3649 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 3650 kfree(cifs_sb->prepath); 3651 cifs_sb->prepath = ctx->prepath; 3652 ctx->prepath = NULL; 3653 3654 out: 3655 cifs_try_adding_channels(mnt_ctx.ses); 3656 rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); 3657 if (rc) 3658 goto error; 3659 3660 free_xid(mnt_ctx.xid); 3661 return rc; 3662 3663 error: 3664 cifs_mount_put_conns(&mnt_ctx); 3665 return rc; 3666 } 3667 #else 3668 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) 3669 { 3670 int rc = 0; 3671 struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; 3672 3673 rc = cifs_mount_get_session(&mnt_ctx); 3674 if (rc) 3675 goto error; 3676 3677 rc = cifs_mount_get_tcon(&mnt_ctx); 3678 if (!rc) { 3679 /* 3680 * Prevent superblock from being created with any missing 3681 * connections. 3682 */ 3683 if (WARN_ON(!mnt_ctx.server)) 3684 rc = -EHOSTDOWN; 3685 else if (WARN_ON(!mnt_ctx.ses)) 3686 rc = -EACCES; 3687 else if (WARN_ON(!mnt_ctx.tcon)) 3688 rc = -ENOENT; 3689 } 3690 if (rc) 3691 goto error; 3692 3693 rc = cifs_is_path_remote(&mnt_ctx); 3694 if (rc == -EREMOTE) 3695 rc = -EOPNOTSUPP; 3696 if (rc) 3697 goto error; 3698 3699 rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); 3700 if (rc) 3701 goto error; 3702 3703 free_xid(mnt_ctx.xid); 3704 return rc; 3705 3706 error: 3707 cifs_mount_put_conns(&mnt_ctx); 3708 return rc; 3709 } 3710 #endif 3711 3712 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 3713 /* 3714 * Issue a TREE_CONNECT request. 3715 */ 3716 int 3717 CIFSTCon(const unsigned int xid, struct cifs_ses *ses, 3718 const char *tree, struct cifs_tcon *tcon, 3719 const struct nls_table *nls_codepage) 3720 { 3721 struct smb_hdr *smb_buffer; 3722 struct smb_hdr *smb_buffer_response; 3723 TCONX_REQ *pSMB; 3724 TCONX_RSP *pSMBr; 3725 unsigned char *bcc_ptr; 3726 int rc = 0; 3727 int length; 3728 __u16 bytes_left, count; 3729 3730 if (ses == NULL) 3731 return -EIO; 3732 3733 smb_buffer = cifs_buf_get(); 3734 if (smb_buffer == NULL) 3735 return -ENOMEM; 3736 3737 smb_buffer_response = smb_buffer; 3738 3739 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX, 3740 NULL /*no tid */, 4 /*wct */); 3741 3742 smb_buffer->Mid = get_next_mid(ses->server); 3743 smb_buffer->Uid = ses->Suid; 3744 pSMB = (TCONX_REQ *) smb_buffer; 3745 pSMBr = (TCONX_RSP *) smb_buffer_response; 3746 3747 pSMB->AndXCommand = 0xFF; 3748 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO); 3749 bcc_ptr = &pSMB->Password[0]; 3750 3751 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */ 3752 *bcc_ptr = 0; /* password is null byte */ 3753 bcc_ptr++; /* skip password */ 3754 /* already aligned so no need to do it below */ 3755 3756 if (ses->server->sign) 3757 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; 3758 3759 if (ses->capabilities & CAP_STATUS32) 3760 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS; 3761 3762 if (ses->capabilities & CAP_DFS) 3763 smb_buffer->Flags2 |= SMBFLG2_DFS; 3764 3765 if (ses->capabilities & CAP_UNICODE) { 3766 smb_buffer->Flags2 |= SMBFLG2_UNICODE; 3767 length = 3768 cifs_strtoUTF16((__le16 *) bcc_ptr, tree, 3769 6 /* max utf8 char length in bytes */ * 3770 (/* server len*/ + 256 /* share len */), nls_codepage); 3771 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */ 3772 bcc_ptr += 2; /* skip trailing null */ 3773 } else { /* ASCII */ 3774 strcpy(bcc_ptr, tree); 3775 bcc_ptr += strlen(tree) + 1; 3776 } 3777 strcpy(bcc_ptr, "?????"); 3778 bcc_ptr += strlen("?????"); 3779 bcc_ptr += 1; 3780 count = bcc_ptr - &pSMB->Password[0]; 3781 be32_add_cpu(&pSMB->hdr.smb_buf_length, count); 3782 pSMB->ByteCount = cpu_to_le16(count); 3783 3784 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length, 3785 0); 3786 3787 /* above now done in SendReceive */ 3788 if (rc == 0) { 3789 bool is_unicode; 3790 3791 tcon->tid = smb_buffer_response->Tid; 3792 bcc_ptr = pByteArea(smb_buffer_response); 3793 bytes_left = get_bcc(smb_buffer_response); 3794 length = strnlen(bcc_ptr, bytes_left - 2); 3795 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) 3796 is_unicode = true; 3797 else 3798 is_unicode = false; 3799 3800 3801 /* skip service field (NB: this field is always ASCII) */ 3802 if (length == 3) { 3803 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') && 3804 (bcc_ptr[2] == 'C')) { 3805 cifs_dbg(FYI, "IPC connection\n"); 3806 tcon->ipc = true; 3807 tcon->pipe = true; 3808 } 3809 } else if (length == 2) { 3810 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) { 3811 /* the most common case */ 3812 cifs_dbg(FYI, "disk share connection\n"); 3813 } 3814 } 3815 bcc_ptr += length + 1; 3816 bytes_left -= (length + 1); 3817 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name)); 3818 3819 /* mostly informational -- no need to fail on error here */ 3820 kfree(tcon->nativeFileSystem); 3821 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr, 3822 bytes_left, is_unicode, 3823 nls_codepage); 3824 3825 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem); 3826 3827 if ((smb_buffer_response->WordCount == 3) || 3828 (smb_buffer_response->WordCount == 7)) 3829 /* field is in same location */ 3830 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport); 3831 else 3832 tcon->Flags = 0; 3833 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags); 3834 3835 /* 3836 * reset_cifs_unix_caps calls QFSInfo which requires 3837 * need_reconnect to be false, but we would not need to call 3838 * reset_caps if this were not a reconnect case so must check 3839 * need_reconnect flag here. The caller will also clear 3840 * need_reconnect when tcon was successful but needed to be 3841 * cleared earlier in the case of unix extensions reconnect 3842 */ 3843 if (tcon->need_reconnect && tcon->unix_ext) { 3844 cifs_dbg(FYI, "resetting caps for %s\n", tcon->tree_name); 3845 tcon->need_reconnect = false; 3846 reset_cifs_unix_caps(xid, tcon, NULL, NULL); 3847 } 3848 } 3849 cifs_buf_release(smb_buffer); 3850 return rc; 3851 } 3852 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 3853 3854 static void delayed_free(struct rcu_head *p) 3855 { 3856 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu); 3857 3858 unload_nls(cifs_sb->local_nls); 3859 smb3_cleanup_fs_context(cifs_sb->ctx); 3860 kfree(cifs_sb); 3861 } 3862 3863 void 3864 cifs_umount(struct cifs_sb_info *cifs_sb) 3865 { 3866 struct rb_root *root = &cifs_sb->tlink_tree; 3867 struct rb_node *node; 3868 struct tcon_link *tlink; 3869 3870 cancel_delayed_work_sync(&cifs_sb->prune_tlinks); 3871 3872 spin_lock(&cifs_sb->tlink_tree_lock); 3873 while ((node = rb_first(root))) { 3874 tlink = rb_entry(node, struct tcon_link, tl_rbnode); 3875 cifs_get_tlink(tlink); 3876 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); 3877 rb_erase(node, root); 3878 3879 spin_unlock(&cifs_sb->tlink_tree_lock); 3880 cifs_put_tlink(tlink); 3881 spin_lock(&cifs_sb->tlink_tree_lock); 3882 } 3883 spin_unlock(&cifs_sb->tlink_tree_lock); 3884 3885 kfree(cifs_sb->prepath); 3886 call_rcu(&cifs_sb->rcu, delayed_free); 3887 } 3888 3889 int 3890 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses, 3891 struct TCP_Server_Info *server) 3892 { 3893 int rc = 0; 3894 3895 if (!server->ops->need_neg || !server->ops->negotiate) 3896 return -ENOSYS; 3897 3898 /* only send once per connect */ 3899 spin_lock(&server->srv_lock); 3900 if (server->tcpStatus != CifsGood && 3901 server->tcpStatus != CifsNew && 3902 server->tcpStatus != CifsNeedNegotiate) { 3903 spin_unlock(&server->srv_lock); 3904 return -EHOSTDOWN; 3905 } 3906 3907 if (!server->ops->need_neg(server) && 3908 server->tcpStatus == CifsGood) { 3909 spin_unlock(&server->srv_lock); 3910 return 0; 3911 } 3912 3913 server->tcpStatus = CifsInNegotiate; 3914 spin_unlock(&server->srv_lock); 3915 3916 rc = server->ops->negotiate(xid, ses, server); 3917 if (rc == 0) { 3918 spin_lock(&server->srv_lock); 3919 if (server->tcpStatus == CifsInNegotiate) 3920 server->tcpStatus = CifsGood; 3921 else 3922 rc = -EHOSTDOWN; 3923 spin_unlock(&server->srv_lock); 3924 } else { 3925 spin_lock(&server->srv_lock); 3926 if (server->tcpStatus == CifsInNegotiate) 3927 server->tcpStatus = CifsNeedNegotiate; 3928 spin_unlock(&server->srv_lock); 3929 } 3930 3931 return rc; 3932 } 3933 3934 int 3935 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses, 3936 struct TCP_Server_Info *server, 3937 struct nls_table *nls_info) 3938 { 3939 int rc = -ENOSYS; 3940 struct TCP_Server_Info *pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 3941 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr; 3942 struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr; 3943 bool is_binding = false; 3944 3945 spin_lock(&ses->ses_lock); 3946 cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n", 3947 __func__, ses->chans_need_reconnect); 3948 3949 if (ses->ses_status != SES_GOOD && 3950 ses->ses_status != SES_NEW && 3951 ses->ses_status != SES_NEED_RECON) { 3952 spin_unlock(&ses->ses_lock); 3953 return -EHOSTDOWN; 3954 } 3955 3956 /* only send once per connect */ 3957 spin_lock(&ses->chan_lock); 3958 if (CIFS_ALL_CHANS_GOOD(ses)) { 3959 if (ses->ses_status == SES_NEED_RECON) 3960 ses->ses_status = SES_GOOD; 3961 spin_unlock(&ses->chan_lock); 3962 spin_unlock(&ses->ses_lock); 3963 return 0; 3964 } 3965 3966 cifs_chan_set_in_reconnect(ses, server); 3967 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses); 3968 spin_unlock(&ses->chan_lock); 3969 3970 if (!is_binding) { 3971 ses->ses_status = SES_IN_SETUP; 3972 3973 /* force iface_list refresh */ 3974 ses->iface_last_update = 0; 3975 } 3976 spin_unlock(&ses->ses_lock); 3977 3978 /* update ses ip_addr only for primary chan */ 3979 if (server == pserver) { 3980 if (server->dstaddr.ss_family == AF_INET6) 3981 scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr); 3982 else 3983 scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr); 3984 } 3985 3986 if (!is_binding) { 3987 ses->capabilities = server->capabilities; 3988 if (!linuxExtEnabled) 3989 ses->capabilities &= (~server->vals->cap_unix); 3990 3991 if (ses->auth_key.response) { 3992 cifs_dbg(FYI, "Free previous auth_key.response = %p\n", 3993 ses->auth_key.response); 3994 kfree_sensitive(ses->auth_key.response); 3995 ses->auth_key.response = NULL; 3996 ses->auth_key.len = 0; 3997 } 3998 } 3999 4000 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n", 4001 server->sec_mode, server->capabilities, server->timeAdj); 4002 4003 if (server->ops->sess_setup) 4004 rc = server->ops->sess_setup(xid, ses, server, nls_info); 4005 4006 if (rc) { 4007 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc); 4008 spin_lock(&ses->ses_lock); 4009 if (ses->ses_status == SES_IN_SETUP) 4010 ses->ses_status = SES_NEED_RECON; 4011 spin_lock(&ses->chan_lock); 4012 cifs_chan_clear_in_reconnect(ses, server); 4013 spin_unlock(&ses->chan_lock); 4014 spin_unlock(&ses->ses_lock); 4015 } else { 4016 spin_lock(&ses->ses_lock); 4017 if (ses->ses_status == SES_IN_SETUP) 4018 ses->ses_status = SES_GOOD; 4019 spin_lock(&ses->chan_lock); 4020 cifs_chan_clear_in_reconnect(ses, server); 4021 cifs_chan_clear_need_reconnect(ses, server); 4022 spin_unlock(&ses->chan_lock); 4023 spin_unlock(&ses->ses_lock); 4024 } 4025 4026 return rc; 4027 } 4028 4029 static int 4030 cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses) 4031 { 4032 ctx->sectype = ses->sectype; 4033 4034 /* krb5 is special, since we don't need username or pw */ 4035 if (ctx->sectype == Kerberos) 4036 return 0; 4037 4038 return cifs_set_cifscreds(ctx, ses); 4039 } 4040 4041 static struct cifs_tcon * 4042 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) 4043 { 4044 int rc; 4045 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb); 4046 struct cifs_ses *ses; 4047 struct cifs_tcon *tcon = NULL; 4048 struct smb3_fs_context *ctx; 4049 char *origin_fullpath = NULL; 4050 4051 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 4052 if (ctx == NULL) 4053 return ERR_PTR(-ENOMEM); 4054 4055 ctx->local_nls = cifs_sb->local_nls; 4056 ctx->linux_uid = fsuid; 4057 ctx->cred_uid = fsuid; 4058 ctx->UNC = master_tcon->tree_name; 4059 ctx->retry = master_tcon->retry; 4060 ctx->nocase = master_tcon->nocase; 4061 ctx->nohandlecache = master_tcon->nohandlecache; 4062 ctx->local_lease = master_tcon->local_lease; 4063 ctx->no_lease = master_tcon->no_lease; 4064 ctx->resilient = master_tcon->use_resilient; 4065 ctx->persistent = master_tcon->use_persistent; 4066 ctx->handle_timeout = master_tcon->handle_timeout; 4067 ctx->no_linux_ext = !master_tcon->unix_ext; 4068 ctx->linux_ext = master_tcon->posix_extensions; 4069 ctx->sectype = master_tcon->ses->sectype; 4070 ctx->sign = master_tcon->ses->sign; 4071 ctx->seal = master_tcon->seal; 4072 ctx->witness = master_tcon->use_witness; 4073 ctx->dfs_root_ses = master_tcon->ses->dfs_root_ses; 4074 4075 rc = cifs_set_vol_auth(ctx, master_tcon->ses); 4076 if (rc) { 4077 tcon = ERR_PTR(rc); 4078 goto out; 4079 } 4080 4081 /* get a reference for the same TCP session */ 4082 spin_lock(&cifs_tcp_ses_lock); 4083 ++master_tcon->ses->server->srv_count; 4084 spin_unlock(&cifs_tcp_ses_lock); 4085 4086 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx); 4087 if (IS_ERR(ses)) { 4088 tcon = ERR_CAST(ses); 4089 cifs_put_tcp_session(master_tcon->ses->server, 0); 4090 goto out; 4091 } 4092 4093 #ifdef CONFIG_CIFS_DFS_UPCALL 4094 spin_lock(&master_tcon->tc_lock); 4095 if (master_tcon->origin_fullpath) { 4096 spin_unlock(&master_tcon->tc_lock); 4097 origin_fullpath = dfs_get_path(cifs_sb, cifs_sb->ctx->source); 4098 if (IS_ERR(origin_fullpath)) { 4099 tcon = ERR_CAST(origin_fullpath); 4100 origin_fullpath = NULL; 4101 cifs_put_smb_ses(ses); 4102 goto out; 4103 } 4104 } else { 4105 spin_unlock(&master_tcon->tc_lock); 4106 } 4107 #endif 4108 4109 tcon = cifs_get_tcon(ses, ctx); 4110 if (IS_ERR(tcon)) { 4111 cifs_put_smb_ses(ses); 4112 goto out; 4113 } 4114 4115 #ifdef CONFIG_CIFS_DFS_UPCALL 4116 if (origin_fullpath) { 4117 spin_lock(&tcon->tc_lock); 4118 tcon->origin_fullpath = origin_fullpath; 4119 spin_unlock(&tcon->tc_lock); 4120 origin_fullpath = NULL; 4121 queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work, 4122 dfs_cache_get_ttl() * HZ); 4123 } 4124 #endif 4125 4126 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 4127 if (cap_unix(ses)) 4128 reset_cifs_unix_caps(0, tcon, NULL, ctx); 4129 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 4130 4131 out: 4132 kfree(ctx->username); 4133 kfree_sensitive(ctx->password); 4134 kfree(origin_fullpath); 4135 kfree(ctx); 4136 4137 return tcon; 4138 } 4139 4140 struct cifs_tcon * 4141 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb) 4142 { 4143 return tlink_tcon(cifs_sb_master_tlink(cifs_sb)); 4144 } 4145 4146 /* find and return a tlink with given uid */ 4147 static struct tcon_link * 4148 tlink_rb_search(struct rb_root *root, kuid_t uid) 4149 { 4150 struct rb_node *node = root->rb_node; 4151 struct tcon_link *tlink; 4152 4153 while (node) { 4154 tlink = rb_entry(node, struct tcon_link, tl_rbnode); 4155 4156 if (uid_gt(tlink->tl_uid, uid)) 4157 node = node->rb_left; 4158 else if (uid_lt(tlink->tl_uid, uid)) 4159 node = node->rb_right; 4160 else 4161 return tlink; 4162 } 4163 return NULL; 4164 } 4165 4166 /* insert a tcon_link into the tree */ 4167 static void 4168 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink) 4169 { 4170 struct rb_node **new = &(root->rb_node), *parent = NULL; 4171 struct tcon_link *tlink; 4172 4173 while (*new) { 4174 tlink = rb_entry(*new, struct tcon_link, tl_rbnode); 4175 parent = *new; 4176 4177 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid)) 4178 new = &((*new)->rb_left); 4179 else 4180 new = &((*new)->rb_right); 4181 } 4182 4183 rb_link_node(&new_tlink->tl_rbnode, parent, new); 4184 rb_insert_color(&new_tlink->tl_rbnode, root); 4185 } 4186 4187 /* 4188 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the 4189 * current task. 4190 * 4191 * If the superblock doesn't refer to a multiuser mount, then just return 4192 * the master tcon for the mount. 4193 * 4194 * First, search the rbtree for an existing tcon for this fsuid. If one 4195 * exists, then check to see if it's pending construction. If it is then wait 4196 * for construction to complete. Once it's no longer pending, check to see if 4197 * it failed and either return an error or retry construction, depending on 4198 * the timeout. 4199 * 4200 * If one doesn't exist then insert a new tcon_link struct into the tree and 4201 * try to construct a new one. 4202 * 4203 * REMEMBER to call cifs_put_tlink() after successful calls to cifs_sb_tlink, 4204 * to avoid refcount issues 4205 */ 4206 struct tcon_link * 4207 cifs_sb_tlink(struct cifs_sb_info *cifs_sb) 4208 { 4209 struct tcon_link *tlink, *newtlink; 4210 kuid_t fsuid = current_fsuid(); 4211 int err; 4212 4213 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 4214 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); 4215 4216 spin_lock(&cifs_sb->tlink_tree_lock); 4217 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); 4218 if (tlink) 4219 cifs_get_tlink(tlink); 4220 spin_unlock(&cifs_sb->tlink_tree_lock); 4221 4222 if (tlink == NULL) { 4223 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL); 4224 if (newtlink == NULL) 4225 return ERR_PTR(-ENOMEM); 4226 newtlink->tl_uid = fsuid; 4227 newtlink->tl_tcon = ERR_PTR(-EACCES); 4228 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags); 4229 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags); 4230 cifs_get_tlink(newtlink); 4231 4232 spin_lock(&cifs_sb->tlink_tree_lock); 4233 /* was one inserted after previous search? */ 4234 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); 4235 if (tlink) { 4236 cifs_get_tlink(tlink); 4237 spin_unlock(&cifs_sb->tlink_tree_lock); 4238 kfree(newtlink); 4239 goto wait_for_construction; 4240 } 4241 tlink = newtlink; 4242 tlink_rb_insert(&cifs_sb->tlink_tree, tlink); 4243 spin_unlock(&cifs_sb->tlink_tree_lock); 4244 } else { 4245 wait_for_construction: 4246 err = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING, 4247 TASK_INTERRUPTIBLE); 4248 if (err) { 4249 cifs_put_tlink(tlink); 4250 return ERR_PTR(-ERESTARTSYS); 4251 } 4252 4253 /* if it's good, return it */ 4254 if (!IS_ERR(tlink->tl_tcon)) 4255 return tlink; 4256 4257 /* return error if we tried this already recently */ 4258 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) { 4259 err = PTR_ERR(tlink->tl_tcon); 4260 cifs_put_tlink(tlink); 4261 return ERR_PTR(err); 4262 } 4263 4264 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags)) 4265 goto wait_for_construction; 4266 } 4267 4268 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid); 4269 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags); 4270 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING); 4271 4272 if (IS_ERR(tlink->tl_tcon)) { 4273 err = PTR_ERR(tlink->tl_tcon); 4274 if (err == -ENOKEY) 4275 err = -EACCES; 4276 cifs_put_tlink(tlink); 4277 return ERR_PTR(err); 4278 } 4279 4280 return tlink; 4281 } 4282 4283 /* 4284 * periodic workqueue job that scans tcon_tree for a superblock and closes 4285 * out tcons. 4286 */ 4287 static void 4288 cifs_prune_tlinks(struct work_struct *work) 4289 { 4290 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info, 4291 prune_tlinks.work); 4292 struct rb_root *root = &cifs_sb->tlink_tree; 4293 struct rb_node *node; 4294 struct rb_node *tmp; 4295 struct tcon_link *tlink; 4296 4297 /* 4298 * Because we drop the spinlock in the loop in order to put the tlink 4299 * it's not guarded against removal of links from the tree. The only 4300 * places that remove entries from the tree are this function and 4301 * umounts. Because this function is non-reentrant and is canceled 4302 * before umount can proceed, this is safe. 4303 */ 4304 spin_lock(&cifs_sb->tlink_tree_lock); 4305 node = rb_first(root); 4306 while (node != NULL) { 4307 tmp = node; 4308 node = rb_next(tmp); 4309 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode); 4310 4311 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) || 4312 atomic_read(&tlink->tl_count) != 0 || 4313 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies)) 4314 continue; 4315 4316 cifs_get_tlink(tlink); 4317 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); 4318 rb_erase(tmp, root); 4319 4320 spin_unlock(&cifs_sb->tlink_tree_lock); 4321 cifs_put_tlink(tlink); 4322 spin_lock(&cifs_sb->tlink_tree_lock); 4323 } 4324 spin_unlock(&cifs_sb->tlink_tree_lock); 4325 4326 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, 4327 TLINK_IDLE_EXPIRE); 4328 } 4329 4330 #ifndef CONFIG_CIFS_DFS_UPCALL 4331 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) 4332 { 4333 int rc; 4334 const struct smb_version_operations *ops = tcon->ses->server->ops; 4335 4336 /* only send once per connect */ 4337 spin_lock(&tcon->tc_lock); 4338 4339 /* if tcon is marked for needing reconnect, update state */ 4340 if (tcon->need_reconnect) 4341 tcon->status = TID_NEED_TCON; 4342 4343 if (tcon->status == TID_GOOD) { 4344 spin_unlock(&tcon->tc_lock); 4345 return 0; 4346 } 4347 4348 if (tcon->status != TID_NEW && 4349 tcon->status != TID_NEED_TCON) { 4350 spin_unlock(&tcon->tc_lock); 4351 return -EHOSTDOWN; 4352 } 4353 4354 tcon->status = TID_IN_TCON; 4355 spin_unlock(&tcon->tc_lock); 4356 4357 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc); 4358 if (rc) { 4359 spin_lock(&tcon->tc_lock); 4360 if (tcon->status == TID_IN_TCON) 4361 tcon->status = TID_NEED_TCON; 4362 spin_unlock(&tcon->tc_lock); 4363 } else { 4364 spin_lock(&tcon->tc_lock); 4365 if (tcon->status == TID_IN_TCON) 4366 tcon->status = TID_GOOD; 4367 tcon->need_reconnect = false; 4368 spin_unlock(&tcon->tc_lock); 4369 } 4370 4371 return rc; 4372 } 4373 #endif 4374