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