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