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